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.

292 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 1990 - 1995 Microsoft Corporation
  3. Module Name:
  4. mapsd.c
  5. Abstract:
  6. Mapping Security Descriptors
  7. Author:
  8. Dave Snipp (DaveSn) 15-Mar-1991
  9. Revision History:
  10. --*/
  11. #include <precomp.h>
  12. // Object types
  13. //
  14. extern GENERIC_MAPPING GenericMapping[];
  15. PSECURITY_DESCRIPTOR
  16. MapPrinterSDToShareSD(
  17. PSECURITY_DESCRIPTOR pPrinterSD
  18. );
  19. BOOL
  20. ProcessSecurityDescriptorDacl(
  21. PSECURITY_DESCRIPTOR pSourceSD,
  22. PACL *ppDacl,
  23. LPBOOL pDefaulted
  24. );
  25. DWORD
  26. MapPrinterMaskToShareMask(
  27. DWORD PrinterMask
  28. );
  29. PSECURITY_DESCRIPTOR
  30. MapPrinterSDToShareSD(
  31. PSECURITY_DESCRIPTOR pPrinterSD
  32. )
  33. {
  34. SECURITY_DESCRIPTOR AbsoluteSD;
  35. PSECURITY_DESCRIPTOR pRelative;
  36. BOOL Defaulted = FALSE;
  37. PSID pOwnerSid = NULL;
  38. PSID pGroupSid = NULL;
  39. PACL pDacl = NULL;
  40. BOOL ErrorOccurred = FALSE;
  41. DWORD SDLength = 0;
  42. if (!IsValidSecurityDescriptor(pPrinterSD)) {
  43. return(NULL);
  44. }
  45. if (!InitializeSecurityDescriptor (&AbsoluteSD ,SECURITY_DESCRIPTOR_REVISION1)) {
  46. return(NULL);
  47. }
  48. if(GetSecurityDescriptorOwner( pPrinterSD,
  49. &pOwnerSid, &Defaulted ) )
  50. SetSecurityDescriptorOwner( &AbsoluteSD,
  51. pOwnerSid, Defaulted );
  52. else
  53. ErrorOccurred = TRUE;
  54. if( GetSecurityDescriptorGroup( pPrinterSD,
  55. &pGroupSid, &Defaulted ) )
  56. SetSecurityDescriptorGroup( &AbsoluteSD,
  57. pGroupSid, Defaulted );
  58. else
  59. ErrorOccurred = TRUE;
  60. if (ProcessSecurityDescriptorDacl(pPrinterSD, &pDacl, &Defaulted)) {
  61. (VOID)SetSecurityDescriptorDacl (&AbsoluteSD, TRUE, pDacl, FALSE );
  62. }
  63. else
  64. ErrorOccurred = TRUE;
  65. if (ErrorOccurred) {
  66. if (pDacl) {
  67. LocalFree(pDacl);
  68. }
  69. return(NULL);
  70. }
  71. SDLength = GetSecurityDescriptorLength( &AbsoluteSD);
  72. pRelative = LocalAlloc(LPTR, SDLength);
  73. if (!pRelative) {
  74. LocalFree(pDacl);
  75. return(NULL);
  76. }
  77. if (!MakeSelfRelativeSD (&AbsoluteSD, pRelative, &SDLength)) {
  78. LocalFree(pRelative);
  79. LocalFree(pDacl);
  80. return(NULL);
  81. }
  82. LocalFree(pDacl);
  83. return(pRelative);
  84. }
  85. BOOL
  86. ProcessSecurityDescriptorDacl(
  87. PSECURITY_DESCRIPTOR pSourceSD,
  88. PACL *ppDacl,
  89. LPBOOL pDefaulted
  90. )
  91. {
  92. BOOL DaclPresent = FALSE;
  93. BOOL bRet = FALSE;
  94. DWORD DestAceCount = 0;
  95. DWORD DaclLength = 0;
  96. PACL TmpAcl = NULL;
  97. PACL pDacl = NULL;
  98. PSID *ppSid = NULL;
  99. ACCESS_MASK *pAccessMask = NULL;
  100. BYTE *pInheritFlags = NULL;
  101. UCHAR *pAceType = NULL;
  102. PACCESS_ALLOWED_ACE pAce = NULL;
  103. DWORD dwLengthSid = 0;
  104. PSID pSourceSid = NULL;
  105. PSID pDestSid = NULL;
  106. DWORD i = 0;
  107. PACCESS_ALLOWED_ACE TmpAce = NULL;
  108. ACL_SIZE_INFORMATION AclSizeInfo;
  109. *ppDacl = NULL;
  110. bRet = GetSecurityDescriptorDacl( pSourceSD, &DaclPresent, &pDacl, pDefaulted );
  111. if (bRet) {
  112. bRet = DaclPresent;
  113. }
  114. if (bRet) {
  115. GetAclInformation(pDacl, &AclSizeInfo, sizeof(ACL_SIZE_INFORMATION), AclSizeInformation);
  116. ppSid = LocalAlloc(LPTR, sizeof(PSID)* AclSizeInfo.AceCount);
  117. pAccessMask = LocalAlloc(LPTR, sizeof(ACCESS_MASK)* AclSizeInfo.AceCount);
  118. pInheritFlags = LocalAlloc(LPTR, sizeof(BYTE)*AclSizeInfo.AceCount);
  119. pAceType = LocalAlloc(LPTR, sizeof(UCHAR)*AclSizeInfo.AceCount);
  120. bRet = ppSid && pAccessMask && pInheritFlags && pAceType;
  121. }
  122. for (i = 0 ; bRet && i < AclSizeInfo.AceCount; i++) {
  123. GetAce(pDacl, i, (LPVOID *)&pAce);
  124. //
  125. // Skip the Ace if it is inherit only
  126. //
  127. if ( ((PACE_HEADER)pAce)->AceFlags & INHERIT_ONLY_ACE ) {
  128. continue;
  129. }
  130. *(pAceType + DestAceCount) = ((PACE_HEADER)pAce)->AceType;
  131. *(pAccessMask + DestAceCount) = MapPrinterMaskToShareMask(((PACCESS_ALLOWED_ACE)pAce)->Mask);
  132. *(pInheritFlags + DestAceCount) = ((PACE_HEADER)pAce)->AceFlags;
  133. //
  134. // Copy the sid information
  135. //
  136. pSourceSid = (PSID)(&(((PACCESS_ALLOWED_ACE)pAce)->SidStart));
  137. dwLengthSid = GetLengthSid(pSourceSid);
  138. pDestSid = (LPBYTE)LocalAlloc(LPTR, dwLengthSid);
  139. if (pDestSid) {
  140. CopySid(dwLengthSid, pDestSid, pSourceSid);
  141. *(ppSid + DestAceCount) = pDestSid;
  142. } else {
  143. //
  144. // We failed to allocate the memory, we signal that we failed and
  145. // fall down to the cleanup code below.
  146. //
  147. bRet = FALSE;
  148. break;
  149. }
  150. DestAceCount++;
  151. }
  152. //
  153. // Compute size of the Dacl
  154. //
  155. if (bRet) {
  156. DaclLength = (DWORD)sizeof(ACL);
  157. for (i = 0; i < DestAceCount; i++) {
  158. DaclLength += GetLengthSid( *(ppSid + i)) +
  159. (DWORD)sizeof(ACCESS_ALLOWED_ACE) -
  160. (DWORD)sizeof(DWORD); //Subtract out SidStart field length
  161. }
  162. TmpAcl = LocalAlloc(LPTR, DaclLength);
  163. bRet = (TmpAcl != NULL);
  164. }
  165. if (bRet) {
  166. bRet = InitializeAcl(TmpAcl, DaclLength, ACL_REVISION2);
  167. }
  168. for (i = 0; bRet && i < DestAceCount; i++) {
  169. if( *(pAceType +i) == ACCESS_ALLOWED_ACE_TYPE )
  170. (VOID)AddAccessAllowedAce ( TmpAcl, ACL_REVISION2, *(pAccessMask + i), *(ppSid + i));
  171. else
  172. (VOID)AddAccessDeniedAce ( TmpAcl, ACL_REVISION2, *(pAccessMask + i), *(ppSid + i));
  173. if (*(pInheritFlags + i) != 0) {
  174. (VOID)GetAce( TmpAcl, i, (LPVOID *)&TmpAce );
  175. TmpAce->Header.AceFlags = *(pInheritFlags + i);
  176. }
  177. }
  178. //
  179. // Write the Dacl back from the TmpAcl.
  180. //
  181. if (bRet) {
  182. *ppDacl = TmpAcl;
  183. TmpAcl = NULL;
  184. }
  185. //
  186. // Free all of our temporary space.
  187. //
  188. if (ppSid) {
  189. for (i = 0; i < DestAceCount; i++) {
  190. LocalFree(*(ppSid + i));
  191. }
  192. LocalFree(ppSid);
  193. }
  194. if (pAccessMask) {
  195. LocalFree(pAccessMask);
  196. }
  197. if (pInheritFlags) {
  198. LocalFree(pInheritFlags);
  199. }
  200. if (pAceType) {
  201. LocalFree(pAceType);
  202. }
  203. if (TmpAcl) {
  204. LocalFree(TmpAcl);
  205. }
  206. return bRet;
  207. }
  208. DWORD
  209. MapPrinterMaskToShareMask(
  210. DWORD PrinterMask
  211. )
  212. {
  213. DWORD ReturnMask = 0;
  214. MapGenericMask(&PrinterMask, &GenericMapping[SPOOLER_OBJECT_PRINTER]);
  215. if ((PrinterMask & PRINTER_ACCESS_ADMINISTER)
  216. || (PrinterMask & PRINTER_ACCESS_USE)) {
  217. ReturnMask |= GENERIC_ALL;
  218. }
  219. return(ReturnMask);
  220. }