Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
9.0 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. // Remove redundent check sums. Use the imagehelp one and delete mikes.
  3. /*****************************************************************************
  4. *
  5. * REGISTER.C for Windows NT
  6. *
  7. * Description:
  8. *
  9. * Register USER/SYSTEM global
  10. *
  11. *
  12. ****************************************************************************/
  13. /* include files */
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <imagehlp.h>
  19. #include <winsta.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <locale.h>
  24. #include <utilsub.h>
  25. #include <utildll.h>
  26. #include <syslib.h>
  27. #include <winnlsp.h>
  28. #include "register.h"
  29. #include "printfoa.h"
  30. // max length of the locale string
  31. #define MAX_LOCALE_STRING 64
  32. /*
  33. * Local variables
  34. */
  35. WCHAR fileW[MAX_PATH + 1];
  36. USHORT system_flag = FALSE;
  37. USHORT user_flag = FALSE;
  38. USHORT help_flag = FALSE;
  39. USHORT v_flag = FALSE;
  40. USHORT d_flag = FALSE;
  41. /*
  42. * Command line parsing strucutre
  43. */
  44. TOKMAP ptm[] =
  45. {
  46. {L" ", TMFLAG_REQUIRED, TMFORM_STRING, MAX_PATH, fileW},
  47. {L"/SYSTEM", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &system_flag},
  48. {L"/USER", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &user_flag},
  49. {L"/?", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &help_flag},
  50. {L"/v", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &v_flag},
  51. {L"/d", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &d_flag},
  52. {0, 0, 0, 0, 0}
  53. };
  54. /*
  55. * Local function prototypes
  56. */
  57. USHORT ChkSum( ULONG PartialSum, PUSHORT Source, ULONG Length );
  58. VOID Usage(BOOL);
  59. BOOLEAN Is_X86_OS()
  60. {
  61. SYSTEM_INFO SystemInfo;
  62. BOOLEAN bReturn = FALSE;
  63. ZeroMemory(&SystemInfo, sizeof(SystemInfo));
  64. GetSystemInfo(&SystemInfo);
  65. if(SystemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL )
  66. {
  67. bReturn = TRUE;
  68. }
  69. return bReturn;
  70. }
  71. /*******************************************************************************
  72. *
  73. * main
  74. *
  75. ******************************************************************************/
  76. INT __cdecl
  77. main( int argc, char *argv[] )
  78. {
  79. INT i;
  80. DWORD rc;
  81. CHAR *pFileView;
  82. HANDLE FileHandle;
  83. ULONG FileLength;
  84. HANDLE Handle;
  85. OFSTRUCT OpenBuff;
  86. PIMAGE_NT_HEADERS pImageNtHeader;
  87. WCHAR *CmdLine;
  88. WCHAR **argvW;
  89. ULONG BytesOut;
  90. BOOL readOnly = TRUE;
  91. WCHAR wszString[MAX_LOCALE_STRING + 1];
  92. setlocale(LC_ALL, ".OCP");
  93. // We don't want LC_CTYPE set the same as the others or else we will see
  94. // garbage output in the localized version, so we need to explicitly
  95. // set it to correct console output code page
  96. _snwprintf(wszString, sizeof(wszString)/sizeof(WCHAR), L".%d", GetConsoleOutputCP());
  97. wszString[sizeof(wszString)/sizeof(WCHAR) - 1] = L'\0';
  98. _wsetlocale(LC_CTYPE, wszString);
  99. SetThreadUILanguage(0);
  100. /*
  101. * Massage the command line.
  102. */
  103. if ( !Is_X86_OS() )
  104. {
  105. ErrorPrintf( IDS_X86_ONLY );
  106. return(FAILURE);
  107. }
  108. argvW = MassageCommandLine((DWORD)argc);
  109. if (argvW == NULL) {
  110. ErrorPrintf(IDS_ERROR_MALLOC);
  111. return(FAILURE);
  112. }
  113. /*
  114. * parse the cmd line without parsing the program name (argc-1, argv+1)
  115. */
  116. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  117. /*
  118. * Check for error from ParseCommandLine
  119. */
  120. if (rc && (rc & PARSE_FLAG_NO_PARMS) )
  121. help_flag = TRUE;
  122. if ( help_flag || rc ) {
  123. if ( !help_flag ) {
  124. Usage(TRUE);
  125. return rc;
  126. } else {
  127. Usage(FALSE);
  128. return ERROR_SUCCESS;
  129. }
  130. }
  131. else if ( system_flag && user_flag ) {
  132. Usage(TRUE);
  133. return ERROR_INVALID_PARAMETER;
  134. }
  135. if (!TestUserForAdmin(FALSE)) {
  136. ErrorPrintf(IDS_ERROR_NOT_ADMIN);
  137. return 1;
  138. }
  139. readOnly = !(system_flag || user_flag );
  140. /*
  141. * Open file
  142. */
  143. FileHandle = CreateFile(
  144. fileW,
  145. readOnly ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
  146. 0,
  147. NULL,
  148. OPEN_EXISTING,
  149. FILE_ATTRIBUTE_NORMAL,
  150. NULL
  151. );
  152. if (FileHandle == INVALID_HANDLE_VALUE) {
  153. ErrorPrintf(IDS_ERROR_OPEN, (rc = GetLastError()));
  154. PutStdErr(rc, 0);
  155. goto done;
  156. }
  157. /*
  158. * Create mapping
  159. */
  160. if ( (Handle = CreateFileMapping( FileHandle, NULL,
  161. readOnly ? PAGE_READONLY : PAGE_READWRITE, 0, 0, NULL )) == NULL ) {
  162. ErrorPrintf(IDS_ERROR_CREATE, (rc=GetLastError()));
  163. PutStdErr( rc, 0 );
  164. goto closefile;
  165. }
  166. /*
  167. * Get file size
  168. */
  169. if ( (FileLength = GetFileSize( FileHandle, NULL )) == 0xffffffff ) {
  170. ErrorPrintf(IDS_ERROR_SIZE, (rc=GetLastError()));
  171. PutStdErr( rc, 0 );
  172. goto closefile;
  173. }
  174. /*
  175. * Map file view into our address space
  176. */
  177. if ( (pFileView = MapViewOfFile( Handle,
  178. readOnly ? FILE_MAP_READ : FILE_MAP_WRITE, 0, 0, 0 )) == NULL ) {
  179. ErrorPrintf(IDS_ERROR_MAP, (rc=GetLastError()));
  180. PutStdErr( rc, 0 );
  181. goto closefile;
  182. }
  183. /*
  184. * Find and validate NT image header
  185. */
  186. if ( ((pImageNtHeader = RtlImageNtHeader( pFileView )) == NULL) ||
  187. (pImageNtHeader->Signature != IMAGE_NT_SIGNATURE) ) {
  188. ErrorPrintf(IDS_ERROR_SIGNATURE);
  189. rc = ERROR_BAD_FORMAT;
  190. goto closefile;
  191. }
  192. /*
  193. * Process query
  194. */
  195. if ( !system_flag && !user_flag ) {
  196. /*
  197. * Check for System Global Flag
  198. */
  199. if ( (pImageNtHeader->OptionalHeader.LoaderFlags & IMAGE_LOADER_FLAGS_SYSTEM_GLOBAL) )
  200. StringMessage(IDS_REGISTER_SYSTEM_GLOBAL, fileW);
  201. else
  202. StringMessage(IDS_REGISTER_USER_GLOBAL, fileW);
  203. }
  204. else {
  205. /*
  206. * Set SYSTEM/USER bit
  207. */
  208. if ( system_flag ) {
  209. /*
  210. * Mask in the load flag
  211. */
  212. pImageNtHeader->OptionalHeader.LoaderFlags |= IMAGE_LOADER_FLAGS_SYSTEM_GLOBAL;
  213. StringMessage(IDS_REGISTER_SYSTEM_GLOBAL, fileW);
  214. }
  215. else if ( user_flag ) {
  216. /*
  217. * Mask out the load flag
  218. */
  219. pImageNtHeader->OptionalHeader.LoaderFlags &= ~(IMAGE_LOADER_FLAGS_SYSTEM_GLOBAL);
  220. StringMessage(IDS_REGISTER_USER_GLOBAL, fileW);
  221. }
  222. /*
  223. * Zero out current check sum and calculate new one
  224. */
  225. pImageNtHeader->OptionalHeader.CheckSum = 0;
  226. pImageNtHeader->OptionalHeader.CheckSum =
  227. ChkSum( 0, (PUSHORT)pFileView, (FileLength + 1) >> 1 );
  228. pImageNtHeader->OptionalHeader.CheckSum += FileLength;
  229. }
  230. /*
  231. * Close image file when finished
  232. */
  233. closefile:
  234. CloseHandle( FileHandle );
  235. done:
  236. return rc;
  237. }
  238. /*******************************************************************************
  239. *
  240. * ChkSum
  241. *
  242. ******************************************************************************/
  243. USHORT
  244. ChkSum(
  245. ULONG PartialSum,
  246. PUSHORT Source,
  247. ULONG Length
  248. )
  249. /*++
  250. Routine Description:
  251. Compute a partial checksum on a portion of an imagefile.
  252. Arguments:
  253. PartialSum - Supplies the initial checksum value.
  254. Sources - Supplies a pointer to the array of words for which the
  255. checksum is computed.
  256. Length - Supplies the length of the array in words.
  257. Return Value:
  258. The computed checksum value is returned as the function value.
  259. --*/
  260. {
  261. //
  262. // Compute the word wise checksum allowing carries to occur into the
  263. // high order half of the checksum longword.
  264. //
  265. while (Length--) {
  266. PartialSum += *Source++;
  267. PartialSum = (PartialSum >> 16) + (PartialSum & 0xffff);
  268. }
  269. //
  270. // Fold final carry into a single word result and return the resultant
  271. // value.
  272. //
  273. return (USHORT)(((PartialSum >> 16) + PartialSum) & 0xffff);
  274. }
  275. /*******************************************************************************
  276. *
  277. * Usage
  278. *
  279. ******************************************************************************/
  280. VOID
  281. Usage( BOOL bError )
  282. {
  283. if ( !Is_X86_OS() )
  284. {
  285. ErrorPrintf( IDS_X86_ONLY );
  286. return;
  287. }
  288. if ( bError )
  289. {
  290. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  291. ErrorPrintf(IDS_USAGE1);
  292. ErrorPrintf(IDS_USAGE2);
  293. ErrorPrintf(IDS_USAGE3);
  294. ErrorPrintf(IDS_USAGE4);
  295. ErrorPrintf(IDS_USAGE7);
  296. }
  297. else {
  298. Message(IDS_USAGE1);
  299. Message(IDS_USAGE2);
  300. Message(IDS_USAGE3);
  301. Message(IDS_USAGE4);
  302. Message(IDS_USAGE7);
  303. }
  304. }