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.

105 lines
3.0 KiB

  1. #include <windows.h>
  2. #include <objbase.h>
  3. #include "sdhelper.h"
  4. #include "commalog.hpp"
  5. #define ADMINISTRATORS 1
  6. #define ACCOUNT_OPERATORS 2
  7. #define BACKUP_OPERATORS 3
  8. #define DOMAIN_ADMINS 4
  9. #define CREATOR_OWNER 5
  10. #define USERS 6
  11. #define SYSTEM 7
  12. PSID ConvertSID(PSID originalSid)
  13. {
  14. DWORD sidLen = GetLengthSid(originalSid);
  15. PSID copiedSid = (PSID) malloc(sidLen);
  16. if (copiedSid)
  17. {
  18. if (!CopySid(sidLen, copiedSid, originalSid))
  19. {
  20. free(copiedSid);
  21. copiedSid = NULL;
  22. }
  23. }
  24. return copiedSid;
  25. }
  26. TSD* BuildAdminsAndSystemSDForCOM()
  27. {
  28. return BuildAdminsAndSystemSD(COM_RIGHTS_EXECUTE);
  29. }
  30. TSD* BuildAdminsAndSystemSD(DWORD accessMask)
  31. {
  32. TSD* builtSD = NULL;
  33. PSID adminsSid = GetWellKnownSid(ADMINISTRATORS);
  34. PSID systemSid = GetWellKnownSid(SYSTEM);
  35. if (adminsSid && systemSid)
  36. {
  37. PSID copiedAdminsSid = ConvertSID(adminsSid);
  38. PSID groupSid = ConvertSID(adminsSid);
  39. builtSD = new TSD(McsUnknownSD);
  40. BOOL bSuccess = FALSE;
  41. if (copiedAdminsSid && groupSid && builtSD)
  42. {
  43. TACE adminsAce(ACCESS_ALLOWED_ACE_TYPE,0,accessMask,copiedAdminsSid);
  44. TACE systemAce(ACCESS_ALLOWED_ACE_TYPE,0,accessMask,systemSid);
  45. // see if both TACE objects get allocated properly
  46. if (adminsAce.GetBuffer() && systemAce.GetBuffer())
  47. {
  48. PACL acl = NULL; // start with an empty ACL
  49. PACL tempAcl;
  50. builtSD->ACLAddAce(&acl,&adminsAce,-1);
  51. if (acl != NULL)
  52. {
  53. tempAcl = acl;
  54. builtSD->ACLAddAce(&acl,&systemAce,-1);
  55. if (acl != tempAcl)
  56. free(tempAcl);
  57. }
  58. if (acl != NULL)
  59. {
  60. // need to set the owner
  61. builtSD->SetOwner(copiedAdminsSid);
  62. copiedAdminsSid = NULL; // memory is taken care of by builtSD destructor
  63. // need to set the group
  64. builtSD->SetGroup(groupSid);
  65. groupSid = NULL; // memory is taken care of by builtSD destructor
  66. // set the DACL part
  67. builtSD->SetDacl(acl,TRUE); // builtSD destructor will take care of acl
  68. bSuccess = TRUE;
  69. }
  70. }
  71. }
  72. if (!bSuccess)
  73. {
  74. if (copiedAdminsSid)
  75. free(copiedAdminsSid);
  76. if (groupSid)
  77. free(groupSid);
  78. if (builtSD)
  79. {
  80. delete builtSD;
  81. builtSD = NULL;
  82. }
  83. }
  84. }
  85. if (adminsSid)
  86. FreeSid(adminsSid);
  87. if (systemSid)
  88. FreeSid(systemSid);
  89. return builtSD;
  90. }