Source code of Windows XP (NT5)
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.

224 lines
5.8 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: security.c
  3. *
  4. * Copyright (c) 1991, Microsoft Corporation
  5. *
  6. * Handles security aspects of winlogon operation.
  7. *
  8. * History:
  9. * 12-05-91 Davidc Created - mostly taken from old winlogon.c
  10. \***************************************************************************/
  11. #include "sec.h"
  12. #include <winuserp.h>
  13. #include <string.h>
  14. #include <fcntl.h>
  15. #include <io.h>
  16. #include <stdio.h>
  17. /***************************************************************************\
  18. * SetMyAce
  19. *
  20. * Helper routine that fills in a MYACE structure.
  21. *
  22. * History:
  23. * 02-06-92 Davidc Created
  24. \***************************************************************************/
  25. VOID
  26. SetMyAce(
  27. PMYACE MyAce,
  28. PSID Sid,
  29. ACCESS_MASK Mask,
  30. UCHAR InheritFlags
  31. )
  32. {
  33. MyAce->Sid = Sid;
  34. MyAce->AccessMask= Mask;
  35. MyAce->InheritFlags = InheritFlags;
  36. }
  37. /***************************************************************************\
  38. * SetWorldSecurity
  39. *
  40. * Sets the security given the logon sid passed.
  41. *
  42. * If the UserSid = NULL, no access is given to anyone other than world
  43. *
  44. * Returns TRUE on success, FALSE on failure
  45. *
  46. * History:
  47. * 12-05-91 Davidc Created
  48. \***************************************************************************/
  49. BOOL
  50. SetWorldSecurity(
  51. PSID UserSid,
  52. PSECURITY_DESCRIPTOR *pSecDesc,
  53. BOOL bCommonGroupAccess
  54. )
  55. {
  56. MYACE Ace[4];
  57. ACEINDEX AceCount = 0;
  58. PSECURITY_DESCRIPTOR SecurityDescriptor;
  59. PSID WorldSid = NULL;
  60. PSID AdminAliasSid = NULL;
  61. PSID PowerUserAliasSid = NULL;
  62. PSID SystemOpsAliasSid = NULL;
  63. SID_IDENTIFIER_AUTHORITY WorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  64. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  65. NTSTATUS Status;
  66. ACCESS_MASK AccessMask;
  67. // Create the world Sid
  68. Status = RtlAllocateAndInitializeSid(
  69. &WorldSidAuthority,
  70. 1, // Sub authority count
  71. SECURITY_WORLD_RID, // Sub authorities
  72. 0, 0, 0, 0, 0, 0, 0,
  73. &WorldSid);
  74. if (!NT_SUCCESS(Status)) {
  75. DbgOnlyPrint("progman failed to allocate memory for world sid\n");
  76. return(FALSE);
  77. }
  78. Status = RtlAllocateAndInitializeSid(
  79. &NtAuthority,
  80. 2, // Sub authority count
  81. SECURITY_BUILTIN_DOMAIN_RID, // Sub authority[0]
  82. DOMAIN_ALIAS_RID_ADMINS, // Sub authority[1]
  83. 0, 0, 0, 0, 0, 0,
  84. &AdminAliasSid);
  85. Status = RtlAllocateAndInitializeSid(
  86. &NtAuthority,
  87. 2, // Sub authority count
  88. SECURITY_BUILTIN_DOMAIN_RID, // Sub authority[0]
  89. DOMAIN_ALIAS_RID_POWER_USERS, // Sub authority[1]
  90. 0, 0, 0, 0, 0, 0,
  91. &PowerUserAliasSid);
  92. Status = RtlAllocateAndInitializeSid(
  93. &NtAuthority,
  94. 2, // Sub authority count
  95. SECURITY_BUILTIN_DOMAIN_RID, // Sub authority[0]
  96. DOMAIN_ALIAS_RID_SYSTEM_OPS, // Sub authority[1]
  97. 0, 0, 0, 0, 0, 0,
  98. &SystemOpsAliasSid);
  99. if (!NT_SUCCESS(Status)) {
  100. DbgOnlyPrint("progman failed to allocate memory for admin sid\n");
  101. return(FALSE);
  102. }
  103. //
  104. // Define the World ACEs
  105. //
  106. if (bCommonGroupAccess) {
  107. AccessMask = KEY_READ;
  108. }
  109. else {
  110. AccessMask = KEY_READ | KEY_WRITE | DELETE;
  111. }
  112. SetMyAce(&(Ace[AceCount++]),
  113. WorldSid,
  114. AccessMask,
  115. NO_PROPAGATE_INHERIT_ACE
  116. );
  117. //
  118. // Define the Admins ACEs
  119. //
  120. SetMyAce(&(Ace[AceCount++]),
  121. AdminAliasSid,
  122. GENERIC_ALL,
  123. NO_PROPAGATE_INHERIT_ACE
  124. );
  125. //
  126. // Define the Power Users ACEs
  127. //
  128. SetMyAce(&(Ace[AceCount++]),
  129. PowerUserAliasSid,
  130. GENERIC_ALL,
  131. NO_PROPAGATE_INHERIT_ACE
  132. );
  133. //
  134. // Define the System Operators ACEs
  135. //
  136. SetMyAce(&(Ace[AceCount++]),
  137. SystemOpsAliasSid,
  138. GENERIC_ALL,
  139. NO_PROPAGATE_INHERIT_ACE
  140. );
  141. // Check we didn't goof
  142. ASSERT((sizeof(Ace) / sizeof(MYACE)) >= AceCount);
  143. //
  144. // Create the security descriptor
  145. //
  146. SecurityDescriptor = CreateSecurityDescriptor(Ace, AceCount);
  147. if (SecurityDescriptor == NULL) {
  148. DbgOnlyPrint("Progman failed to create security descriptor\n\r");
  149. return(FALSE);
  150. }
  151. #if 0
  152. // Keep security descriptor global
  153. // delete only when exiting the program
  154. //
  155. // Free up the security descriptor
  156. //
  157. DeleteSecurityDescriptor(SecurityDescriptor);
  158. #endif
  159. //
  160. // Return success status
  161. //
  162. *pSecDesc = SecurityDescriptor;
  163. return(TRUE);
  164. }
  165. /***************************************************************************\
  166. * InitializeSecurityAttributes
  167. *
  168. *
  169. * Returns TRUE on success, FALSE on failure
  170. *
  171. * History:
  172. * 04-14092 JohanneC Created
  173. \***************************************************************************/
  174. BOOL InitializeSecurityAttributes
  175. (
  176. PSECURITY_ATTRIBUTES pSecurityAttributes,
  177. BOOL bCommonGroupAccess
  178. )
  179. {
  180. PSECURITY_DESCRIPTOR pSecDesc;
  181. if (!SetWorldSecurity(NULL, &pSecDesc, bCommonGroupAccess)) {
  182. return(FALSE);
  183. }
  184. pSecurityAttributes->nLength = sizeof(SECURITY_ATTRIBUTES);
  185. pSecurityAttributes->lpSecurityDescriptor = pSecDesc;
  186. pSecurityAttributes->bInheritHandle = TRUE;
  187. return(TRUE);
  188. }