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.

277 lines
6.8 KiB

  1. // Copyright (c) 1997-2002 Microsoft Corporation
  2. //
  3. // Module:
  4. //
  5. // Network Security Utilities
  6. //
  7. // Abstract:
  8. //
  9. // Acl API's
  10. //
  11. // Authors:
  12. //
  13. // pmay 2/5/02
  14. // raymonds 03/20/02
  15. //
  16. // Environment:
  17. //
  18. // User mode
  19. //
  20. // Revision History:
  21. //
  22. #include <precomp.h>
  23. // Private declarations
  24. //
  25. // Maximum string security descriptor length
  26. //
  27. #define MAX_STR_SD_LEN 128
  28. // TBD: Remove these when incorporated into main NSU utilities
  29. #define CLEANUP Cleanup
  30. #define BAIL_ON_ERROR(err) if((err) != ERROR_SUCCESS) {goto CLEANUP;}
  31. #define BAIL_ON_NULL(ptr, err) if ((ptr) == NULL) {(err) = ERROR_NOT_ENOUGH_MEMORY; goto CLEANUP;}
  32. #define BAIL_OUT {goto CLEANUP;}
  33. // Description:
  34. //
  35. // Allocates and initializes a SECURITY_ATTRIBUTES structure that gives
  36. // access according to the flags passed in. (contained SD is self-relative).
  37. //
  38. // Arguments:
  39. //
  40. // ppSecurityAttributes - pointer to SECURITY_ATTRIBUTES created.
  41. // Use NsuAclAttributesDestroy to destroy.
  42. // dwFlags - see NSU_ACL_F_* values
  43. //
  44. // Return Value:
  45. //
  46. // An allocated security attributes structure or NULL if out of memory.
  47. //
  48. //
  49. // TBD: use NsuString and Nsu mem functions
  50. DWORD
  51. NsuAclAttributesCreate(
  52. OUT PSECURITY_ATTRIBUTES* ppSecurityAttributes,
  53. IN DWORD dwFlags)
  54. {
  55. DWORD dwError = ERROR_SUCCESS;
  56. SECURITY_ATTRIBUTES *pSecurityAttributes = NULL;
  57. pSecurityAttributes = LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
  58. BAIL_ON_NULL(pSecurityAttributes, dwError);
  59. dwError = NsuAclDescriptorCreate(
  60. (PSECURITY_DESCRIPTOR*) &pSecurityAttributes->lpSecurityDescriptor,
  61. dwFlags
  62. );
  63. BAIL_ON_ERROR(dwError);
  64. pSecurityAttributes->nLength = sizeof(SECURITY_ATTRIBUTES);
  65. pSecurityAttributes->bInheritHandle = FALSE;
  66. *ppSecurityAttributes = pSecurityAttributes;
  67. return dwError;
  68. CLEANUP:
  69. if (pSecurityAttributes) {
  70. NsuAclAttributesDestroy(&pSecurityAttributes);
  71. }
  72. *ppSecurityAttributes = NULL;
  73. return dwError;
  74. }
  75. // Description:
  76. //
  77. // Deallocates return value of NsuAclCreateAttributes.
  78. //
  79. DWORD
  80. NsuAclAttributesDestroy(
  81. IN OUT PSECURITY_ATTRIBUTES* ppSecurityAttributes)
  82. {
  83. DWORD dwError = ERROR_SUCCESS;
  84. if (!ppSecurityAttributes) {
  85. BAIL_OUT;
  86. }
  87. // Destroy Security descriptor, ignoring any errors, since there's not much we
  88. // can do and want to clean up the rest of the attributes as much as possible.
  89. //
  90. (VOID) NsuAclDescriptorDestroy((*ppSecurityAttributes)->lpSecurityDescriptor);
  91. (VOID) LocalFree(*ppSecurityAttributes);
  92. *ppSecurityAttributes = NULL;
  93. return dwError;
  94. CLEANUP:
  95. return dwError;
  96. }
  97. // Description:
  98. //
  99. // Allocates and initializes a self-relative SECURITY_DESCRIPTOR structure that gives
  100. // access according to the flags passed in.
  101. //
  102. // Arguments:
  103. //
  104. // ppSecurityDescriptor - security descriptor created. Use NsuAclDescriptorDestroy
  105. // to destroy.
  106. // dwFlags - see NSU_ACL_F_* values
  107. //
  108. // Return Value:
  109. //
  110. // An allocated security attributes structure or NULL if out of memory.
  111. //
  112. DWORD
  113. NsuAclDescriptorCreate (
  114. OUT PSECURITY_DESCRIPTOR* ppSecurityDescriptor,
  115. IN DWORD dwFlags)
  116. {
  117. DWORD dwError = ERROR_SUCCESS;
  118. BOOL fSucceeded = TRUE;
  119. WCHAR szStringSecurityDescriptor[MAX_STR_SD_LEN] = {0};
  120. PSECURITY_DESCRIPTOR pSecurityDescriptor = NULL;
  121. wcscpy(szStringSecurityDescriptor, L"D:AIAR");
  122. if (dwFlags & NSU_ACL_F_AdminFull) {
  123. wcscat(szStringSecurityDescriptor, L"(A;OICI;GA;;;BA)");
  124. }
  125. if (dwFlags & NSU_ACL_F_LocalSystemFull) {
  126. wcscat(szStringSecurityDescriptor, L"(A;OICI;GA;;;SY)");
  127. }
  128. fSucceeded = ConvertStringSecurityDescriptorToSecurityDescriptorW(
  129. szStringSecurityDescriptor,
  130. SDDL_REVISION_1,
  131. &pSecurityDescriptor,
  132. NULL
  133. );
  134. if (!fSucceeded) {
  135. dwError = GetLastError();
  136. BAIL_OUT;
  137. }
  138. *ppSecurityDescriptor = pSecurityDescriptor;
  139. return dwError;
  140. CLEANUP:
  141. NsuAclDescriptorDestroy(&pSecurityDescriptor);
  142. *ppSecurityDescriptor = NULL;
  143. return dwError;
  144. }
  145. // Description:
  146. //
  147. // Deallocates return value of NsuAclCreateDescriptor.
  148. //
  149. DWORD
  150. NsuAclDescriptorDestroy(
  151. IN OUT PSECURITY_DESCRIPTOR* ppDescriptor)
  152. {
  153. DWORD dwError = ERROR_SUCCESS;
  154. if (!ppDescriptor) {
  155. BAIL_OUT;
  156. }
  157. (VOID) LocalFree(*ppDescriptor);
  158. *ppDescriptor = NULL;
  159. return dwError;
  160. CLEANUP:
  161. return dwError;
  162. }
  163. // Description:
  164. //
  165. // Used to determine whether a given security descriptor grants
  166. // full access to everyone.
  167. //
  168. // Arguments:
  169. //
  170. // pSD - the security descriptor
  171. // pbRestricts - TRUE if non-Everyone-full-access, FALSE otherwise
  172. //
  173. // Return Value:
  174. //
  175. // Standard win32 error
  176. //
  177. DWORD
  178. NsuAclDescriptorRestricts(
  179. IN CONST PSECURITY_DESCRIPTOR pSD,
  180. OUT BOOL* pbRestricts)
  181. {
  182. return ERROR_CALL_NOT_IMPLEMENTED;
  183. }
  184. // Description:
  185. //
  186. // Gets security descriptor of a regkey.
  187. //
  188. //
  189. // Arguments:
  190. //
  191. // ppSecurityDescriptor - security descriptor returned. Use NsuAclDescriptorDestroy
  192. // to destroy.
  193. // hKey - open handle of registry key
  194. //
  195. // Return Value:
  196. //
  197. // An allocated security attributes structure or NULL if out of memory.
  198. //
  199. DWORD
  200. NsuAclGetRegKeyDescriptor(
  201. IN HKEY hKey,
  202. OUT PSECURITY_DESCRIPTOR* ppSecurityDescriptor
  203. )
  204. {
  205. PSECURITY_DESCRIPTOR pSecurityDescriptor = 0;
  206. DWORD dwError = ERROR_SUCCESS;
  207. DWORD cbSecurityDescriptor = 0;
  208. cbSecurityDescriptor = 0;
  209. dwError = RegGetKeySecurity(
  210. hKey,
  211. DACL_SECURITY_INFORMATION,
  212. NULL,
  213. &cbSecurityDescriptor
  214. );
  215. if (dwError != ERROR_INSUFFICIENT_BUFFER) {
  216. BAIL_ON_ERROR(dwError);
  217. }
  218. pSecurityDescriptor = LocalAlloc(LPTR, cbSecurityDescriptor);
  219. BAIL_ON_NULL(pSecurityDescriptor, dwError);
  220. dwError = RegGetKeySecurity(
  221. hKey,
  222. DACL_SECURITY_INFORMATION,
  223. pSecurityDescriptor,
  224. &cbSecurityDescriptor
  225. );
  226. BAIL_ON_ERROR(dwError);
  227. *ppSecurityDescriptor = pSecurityDescriptor;
  228. CLEANUP:
  229. if (dwError) {
  230. if (pSecurityDescriptor) {
  231. LocalFree(pSecurityDescriptor);
  232. }
  233. *ppSecurityDescriptor = NULL;
  234. }
  235. return dwError;
  236. }