Windows NT 4.0 source code leak
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.

527 lines
12 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. Global.c
  5. Abstract:
  6. This module contains global variables available throughout
  7. SecMgr. It also contains a routine to initialize those
  8. variables.
  9. Author:
  10. Jim Kelly (JimK) 22-Sep-1994
  11. Revision History:
  12. --*/
  13. #include "secmgrp.h"
  14. ///////////////////////////////////////////////////////////////////////
  15. // //
  16. // Global Variables //
  17. // //
  18. ///////////////////////////////////////////////////////////////////////
  19. //
  20. // This variable indicates whether or not the invoking user
  21. // is an administrator.
  22. //
  23. BOOL
  24. SecMgrpAdminUser = TRUE;
  25. //
  26. // Type of product installed and running on this machine
  27. //
  28. NT_PRODUCT_TYPE
  29. SecMgrpProductType;
  30. //
  31. // This boolean will be set to TRUE if any changes have been
  32. // made to the security settings.
  33. //
  34. BOOLEAN
  35. SecMgrpChangesMade = FALSE;
  36. //
  37. // These variables contain the security level that was
  38. // in force when the utility was activated, and what the
  39. // current applied level is (according to the radio buttons).
  40. // These are used to grey and ungrey the [Apply] and [Check]
  41. // buttons. In dialoges that check and apply, CurrentLevel
  42. // is the level being checked or applied.
  43. //
  44. ULONG
  45. SecMgrpCurrentLevel,
  46. SecMgrpOriginalLevel;
  47. //
  48. // Some changes don't take effect until a re-boot has been
  49. // performed. If any of these changes are made while applying
  50. // a change, then this variable will be set to TRUE. This will
  51. // then be used to decide whether to display a message to the
  52. // user upon utility exit.
  53. //
  54. BOOLEAN
  55. SecMgrpRebootRequired = FALSE;
  56. //
  57. // Handle to our own module
  58. //
  59. HINSTANCE
  60. SecMgrphInstance = NULL;
  61. //
  62. // Name of our application
  63. // (localization must live within 100 bytes)
  64. //
  65. TCHAR
  66. SecMgrpApplicationName[100];
  67. //
  68. // Well known sids that we use
  69. //
  70. PSID
  71. SecMgrpWorldSid,
  72. SecMgrpAccountOpsSid,
  73. SecMgrpBackupOpsSid,
  74. SecMgrpPrintOpsSid,
  75. SecMgrpServerOpsSid,
  76. SecMgrpAdminsSid;
  77. //
  78. // Grouping of well-known sids by type
  79. //
  80. SECMGRP_ACCOUNTS
  81. SecMgrpAnyoneSids,
  82. SecMgrpOperatorSids,
  83. SecMgrpOpersAndAdminsSids,
  84. SecMgrpAdminsSids;
  85. ///////////////////////////////////////////////////////////////////////
  86. // //
  87. // Module-Private Prototypes //
  88. // //
  89. ///////////////////////////////////////////////////////////////////////
  90. BOOL
  91. TestTokenForAdmin( HANDLE Token );
  92. BOOL
  93. SecMgrpIsUserAdmin( VOID );
  94. ///////////////////////////////////////////////////////////////////////
  95. // //
  96. // Externally callable routines //
  97. // //
  98. ///////////////////////////////////////////////////////////////////////
  99. VOID
  100. SecMgrpInitializeGlobals(
  101. HINSTANCE hInstance
  102. )
  103. /*++
  104. Routine Description:
  105. Initialize global variables.
  106. Arguments
  107. hInstance - the HINSTANCE of our app.
  108. Return Values:
  109. None.
  110. --*/
  111. {
  112. NTSTATUS
  113. NtStatus;
  114. ULONG
  115. Index;
  116. BOOLEAN
  117. IgnoreBoolean;
  118. SID_IDENTIFIER_AUTHORITY
  119. WorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY,
  120. NtAuthority = SECURITY_NT_AUTHORITY;
  121. //
  122. // Save away our hInstance
  123. //
  124. SecMgrphInstance = hInstance;
  125. //
  126. // Get our application's name
  127. //
  128. LoadString( SecMgrphInstance,
  129. SECMGRP_STRING_SECURITY_MANAGER,
  130. &SecMgrpApplicationName[0],
  131. 100
  132. );
  133. //
  134. // Initialize the sids
  135. //
  136. NtStatus = RtlAllocateAndInitializeSid(
  137. &WorldSidAuthority,
  138. 1,
  139. SECURITY_WORLD_RID,
  140. 0, 0, 0, 0, 0, 0, 0,
  141. &SecMgrpWorldSid
  142. );
  143. if (!NT_SUCCESS(NtStatus)) {
  144. KdPrint(("SecMgr: Failed to initialize world sid, status = 0x%lx", NtStatus));
  145. return;
  146. }
  147. NtStatus = RtlAllocateAndInitializeSid(
  148. &NtAuthority,
  149. 2,
  150. SECURITY_BUILTIN_DOMAIN_RID,
  151. DOMAIN_ALIAS_RID_ACCOUNT_OPS,
  152. 0, 0, 0, 0, 0, 0,
  153. &SecMgrpAccountOpsSid
  154. );
  155. if (!NT_SUCCESS(NtStatus)) {
  156. KdPrint(("SecMgr: Failed to initialize admin AcountOps sid, status = 0x%lx", NtStatus));
  157. return;
  158. }
  159. NtStatus = RtlAllocateAndInitializeSid(
  160. &NtAuthority,
  161. 2,
  162. SECURITY_BUILTIN_DOMAIN_RID,
  163. DOMAIN_ALIAS_RID_SYSTEM_OPS,
  164. 0, 0, 0, 0, 0, 0,
  165. &SecMgrpServerOpsSid
  166. );
  167. if (!NT_SUCCESS(NtStatus)) {
  168. KdPrint(("SecMgr: Failed to initialize admin ServerOps sid, status = 0x%lx", NtStatus));
  169. return;
  170. }
  171. NtStatus = RtlAllocateAndInitializeSid(
  172. &NtAuthority,
  173. 2,
  174. SECURITY_BUILTIN_DOMAIN_RID,
  175. DOMAIN_ALIAS_RID_PRINT_OPS,
  176. 0, 0, 0, 0, 0, 0,
  177. &SecMgrpPrintOpsSid
  178. );
  179. if (!NT_SUCCESS(NtStatus)) {
  180. KdPrint(("SecMgr: Failed to initialize admin PrintOps sid, status = 0x%lx", NtStatus));
  181. return;
  182. }
  183. NtStatus = RtlAllocateAndInitializeSid(
  184. &NtAuthority,
  185. 2,
  186. SECURITY_BUILTIN_DOMAIN_RID,
  187. DOMAIN_ALIAS_RID_BACKUP_OPS,
  188. 0, 0, 0, 0, 0, 0,
  189. &SecMgrpBackupOpsSid
  190. );
  191. if (!NT_SUCCESS(NtStatus)) {
  192. KdPrint(("SecMgr: Failed to initialize admin BackupOps sid, status = 0x%lx", NtStatus));
  193. return;
  194. }
  195. NtStatus = RtlAllocateAndInitializeSid(
  196. &NtAuthority,
  197. 2,
  198. SECURITY_BUILTIN_DOMAIN_RID,
  199. DOMAIN_ALIAS_RID_ADMINS,
  200. 0, 0, 0, 0, 0, 0,
  201. &SecMgrpAdminsSid
  202. );
  203. if (!NT_SUCCESS(NtStatus)) {
  204. KdPrint(("SecMgr: Failed to initialize admin alias sid, status = 0x%lx", NtStatus));
  205. return;
  206. }
  207. //
  208. // Group the sids by type
  209. //
  210. Index=0;
  211. SecMgrpAnyoneSids.Sid[Index++] = SecMgrpWorldSid;
  212. SecMgrpAnyoneSids.Accounts = Index;
  213. ASSERT(Index < SECMGRP_MAX_WELL_KNOWN_ACCOUNTS);
  214. Index=0;
  215. SecMgrpOperatorSids.Sid[Index++] = SecMgrpAccountOpsSid;
  216. SecMgrpOperatorSids.Sid[Index++] = SecMgrpBackupOpsSid;
  217. SecMgrpOperatorSids.Sid[Index++] = SecMgrpPrintOpsSid;
  218. SecMgrpOperatorSids.Sid[Index++] = SecMgrpServerOpsSid;
  219. SecMgrpOperatorSids.Accounts = Index;
  220. ASSERT(Index < SECMGRP_MAX_WELL_KNOWN_ACCOUNTS);
  221. Index=0;
  222. SecMgrpOpersAndAdminsSids.Sid[Index++] = SecMgrpAccountOpsSid;
  223. SecMgrpOpersAndAdminsSids.Sid[Index++] = SecMgrpBackupOpsSid;
  224. SecMgrpOpersAndAdminsSids.Sid[Index++] = SecMgrpPrintOpsSid;
  225. SecMgrpOpersAndAdminsSids.Sid[Index++] = SecMgrpServerOpsSid;
  226. SecMgrpOpersAndAdminsSids.Sid[Index++] = SecMgrpAdminsSid;
  227. SecMgrpOpersAndAdminsSids.Accounts = Index;
  228. ASSERT(Index < SECMGRP_MAX_WELL_KNOWN_ACCOUNTS);
  229. Index=0;
  230. SecMgrpAdminsSids.Sid[Index++] = SecMgrpAdminsSid;
  231. SecMgrpAdminsSids.Accounts = Index;
  232. ASSERT(Index < SECMGRP_MAX_WELL_KNOWN_ACCOUNTS);
  233. //
  234. // Make sure we are an admin
  235. //
  236. if (!SecMgrpIsUserAdmin()) {
  237. //
  238. // The invoking user is not an admin.
  239. // This utility is only for use by administrators.
  240. //
  241. SecMgrpAdminUser = FALSE;
  242. }
  243. //
  244. // Get the product type
  245. //
  246. IgnoreBoolean = RtlGetNtProductType( &SecMgrpProductType );
  247. ASSERT(IgnoreBoolean == TRUE);
  248. //
  249. // We don't yet know our security level, so set it to zero.
  250. //
  251. SecMgrpCurrentLevel = 0;
  252. SecMgrpOriginalLevel = 0;
  253. }
  254. ///////////////////////////////////////////////////////////////////////
  255. // //
  256. // Module-Private Functions //
  257. // //
  258. ///////////////////////////////////////////////////////////////////////
  259. BOOL
  260. SecMgrpIsUserAdmin( VOID )
  261. /*++
  262. Routine Description:
  263. Checks to see if the current process has administrator
  264. capabilities.
  265. Arguments
  266. None.
  267. Return Values:
  268. TRUE - The process has and administrator capabilities.
  269. FALSE - The process does NOT have administrator capabilities.
  270. --*/
  271. {
  272. NTSTATUS
  273. NtStatus;
  274. HANDLE
  275. Token;
  276. BOOL
  277. IsAdmin;
  278. NtStatus = NtOpenProcessToken( NtCurrentProcess(),
  279. TOKEN_QUERY,
  280. &Token
  281. );
  282. if (!NT_SUCCESS(NtStatus)) {
  283. return(FALSE);
  284. }
  285. IsAdmin = TestTokenForAdmin( Token );
  286. NtStatus = NtClose( Token );
  287. return( IsAdmin );
  288. }
  289. BOOL
  290. TestTokenForAdmin(
  291. HANDLE Token
  292. )
  293. /*++
  294. Routine Description:
  295. Checks to see if the passed token is an administrator's token
  296. or not. A token is an administrator's token if it contains
  297. the Administrators local group.
  298. Arguments
  299. Token - Handle to token to test for admin.
  300. Return Values:
  301. TRUE - The token IS and administrator's.
  302. FALSE - The token is NOT an administrator's
  303. --*/
  304. {
  305. NTSTATUS
  306. NtStatus;
  307. ULONG
  308. InfoLength;
  309. PTOKEN_GROUPS
  310. TokenGroupList;
  311. ULONG
  312. GroupIndex;
  313. BOOL
  314. FoundAdmin;
  315. //
  316. // Get a list of groups in the token
  317. //
  318. NtStatus = NtQueryInformationToken(
  319. Token, // Handle
  320. TokenGroups, // TokenInformationClass
  321. NULL, // TokenInformation
  322. 0, // TokenInformationLength
  323. &InfoLength // ReturnLength
  324. );
  325. if ((NtStatus != STATUS_SUCCESS) && (NtStatus != STATUS_BUFFER_TOO_SMALL)) {
  326. KdPrint(("SecMgr: failed to get group info for admin token, status = 0x%lx", NtStatus));
  327. return(FALSE);
  328. }
  329. TokenGroupList = LocalAlloc( LPTR, InfoLength);
  330. if (TokenGroupList == NULL) {
  331. KdPrint(("SecMgr: unable to allocate memory for token groups"));
  332. return(FALSE);
  333. }
  334. NtStatus = NtQueryInformationToken(
  335. Token, // Handle
  336. TokenGroups, // TokenInformationClass
  337. TokenGroupList, // TokenInformation
  338. InfoLength, // TokenInformationLength
  339. &InfoLength // ReturnLength
  340. );
  341. if (!NT_SUCCESS(NtStatus)) {
  342. KdPrint(("SecMgr: failed to query groups for admin token, status = 0x%lx", NtStatus));
  343. LocalFree(TokenGroupList);
  344. return(FALSE);
  345. }
  346. //
  347. // Search group list for admin alias
  348. //
  349. FoundAdmin = FALSE;
  350. for (GroupIndex=0; GroupIndex < TokenGroupList->GroupCount; GroupIndex++ ) {
  351. if (RtlEqualSid(TokenGroupList->Groups[GroupIndex].Sid, SecMgrpAdminsSid)) {
  352. FoundAdmin = TRUE;
  353. break;
  354. }
  355. }
  356. //
  357. // Tidy up
  358. //
  359. LocalFree(TokenGroupList);
  360. return(FoundAdmin);
  361. }