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.

216 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. adtcomn.c
  5. Abstract:
  6. AdminTools common Routines.
  7. This file contains the calls to GetFileSecurity and
  8. SetFileSecurity that is used on both the client and server
  9. sides of this RPC server.
  10. Author:
  11. Dan Lafferty (danl) 23-Mar-1993
  12. Environment:
  13. User Mode - Win32
  14. Revision History:
  15. 23-Mar-1993 danl
  16. Created
  17. --*/
  18. //
  19. // Includes
  20. //
  21. #include <nt.h>
  22. #include <ntrtl.h>
  23. #include <nturtl.h>
  24. #include <windows.h>
  25. #include <lmcons.h>
  26. #include <lmerr.h>
  27. #include <rpc.h>
  28. #include <srvsvc.h>
  29. #include <netlibnt.h> // NetpNtStatusToApiStatus
  30. #include "adtcomn.h"
  31. //
  32. // LOCAL FUNCTIONS
  33. //
  34. DWORD
  35. PrivateGetFileSecurity (
  36. LPWSTR FileName,
  37. SECURITY_INFORMATION RequestedInfo,
  38. PSECURITY_DESCRIPTOR *pSDBuffer,
  39. LPDWORD pBufSize
  40. )
  41. /*++
  42. Routine Description:
  43. This function returns to the caller a copy of the security descriptor
  44. protecting a file or directory. It calls GetFileSecurity. The
  45. Security Descriptor is always returned in the self-relative format.
  46. NOTE: This function allocates storage for the pSDBuffer. Therefore,
  47. this pointer must be free'd by the caller.
  48. Arguments:
  49. FileName - A pointer to the name fo the file or directory whose
  50. security is being retrieved.
  51. RequestedInfo - The type of security information being requested.
  52. pSDBuffer - A pointer to a location where a pointer for the
  53. security descriptor and a length field for the security descriptor.
  54. pBufSize - A pointer to the location where the size, in bytes, of
  55. the returned security descriptor is to be placed.
  56. Return Value:
  57. NERR_Success - The operation was successful.
  58. ERROR_NOT_ENOUGH_MEMORY - Unable to allocate memory for the security
  59. descriptor.
  60. This function can also return any error that GetFileSecurity can
  61. return.
  62. --*/
  63. {
  64. NET_API_STATUS status;
  65. DWORD sizeNeeded;
  66. *pSDBuffer = NULL;
  67. //
  68. // Determine the buffer size for the Descriptor
  69. //
  70. if (GetFileSecurityW(
  71. FileName, // File whose security is being retrieved
  72. RequestedInfo, // security info being requested
  73. *pSDBuffer, // buffer to receive security descriptor
  74. 0, // size of the buffer
  75. &sizeNeeded)) { // size of buffer required
  76. //
  77. // We should have a failed due to a buffer size being too small.
  78. //
  79. status = ERROR_INVALID_PARAMETER;
  80. goto CleanExit;
  81. }
  82. status = GetLastError();
  83. if ((status == ERROR_INSUFFICIENT_BUFFER) && (sizeNeeded > 0)) {
  84. *pSDBuffer = MIDL_user_allocate(sizeNeeded);
  85. if (pSDBuffer == NULL) {
  86. status = GetLastError();
  87. ADT_LOG1(ERROR,"NetrpGetFileSecurity:MIDL_user_alloc1 failed %d\n",status);
  88. goto CleanExit;
  89. }
  90. *pBufSize = sizeNeeded;
  91. if (!GetFileSecurityW(
  92. FileName, // File whose security is being retrieved
  93. RequestedInfo, // security info being requested
  94. *pSDBuffer, // buffer to receive security descriptor
  95. sizeNeeded, // size of the buffer
  96. &sizeNeeded)) { // size of buffer required
  97. //
  98. // The call with the proper buffer size failed.
  99. //
  100. status = GetLastError();
  101. ADT_LOG1(ERROR, "GetFileSecurity Failed %d\n", status);
  102. MIDL_user_free(*pSDBuffer);
  103. goto CleanExit;
  104. }
  105. ADT_LOG0(TRACE,"NetrpGetFileSecurity:GetFileSecurity Success\n");
  106. if (!IsValidSecurityDescriptor(*pSDBuffer)) {
  107. ADT_LOG0(TRACE,"FAILURE: SECURITY DESCRIPTOR IS INVALID\n");
  108. }
  109. else {
  110. ADT_LOG0(TRACE,"SUCCESS: SECURITY DESCRIPTOR IS GOOD\n");
  111. }
  112. status = NO_ERROR;
  113. }
  114. CleanExit:
  115. return(status);
  116. }
  117. DWORD
  118. PrivateSetFileSecurity (
  119. LPWSTR FileName,
  120. SECURITY_INFORMATION SecurityInfo,
  121. PSECURITY_DESCRIPTOR pSecurityDescriptor
  122. )
  123. /*++
  124. Routine Description:
  125. This function can be used to set the security of a file or directory.
  126. It calls SetFileSecurity().
  127. Arguments:
  128. FileName - A pointer to the name of the file or directory whose
  129. security is being changed.
  130. SecurityInfo - Information describing the contents
  131. of the Security Descriptor.
  132. pSecurityDescriptor - A pointer to a structure that contains a
  133. self-relative security descriptor and a length.
  134. Return Value:
  135. NERR_Success - The operation was successful.
  136. This function can also return any error that GetFileSecurity can
  137. return.
  138. --*/
  139. {
  140. DWORD status=NO_ERROR;
  141. //
  142. // Call SetFileSecurity
  143. //
  144. if (!SetFileSecurityW (
  145. FileName,
  146. SecurityInfo,
  147. pSecurityDescriptor)) {
  148. status = GetLastError();
  149. return(status);
  150. }
  151. return(NO_ERROR);
  152. }