sid.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_SID_H_
  5. #define BASE_WIN_SID_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/base_export.h"
  9. #include "base/win/windows_types.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace base {
  12. namespace win {
  13. // Known capabilities defined in Windows 8.
  14. enum class WellKnownCapability {
  15. kInternetClient,
  16. kInternetClientServer,
  17. kPrivateNetworkClientServer,
  18. kPicturesLibrary,
  19. kVideosLibrary,
  20. kMusicLibrary,
  21. kDocumentsLibrary,
  22. kEnterpriseAuthentication,
  23. kSharedUserCertificates,
  24. kRemovableStorage,
  25. kAppointments,
  26. kContacts
  27. };
  28. // A subset of well known SIDs to create.
  29. enum class WellKnownSid {
  30. kNull,
  31. kWorld,
  32. kCreatorOwner,
  33. kNetwork,
  34. kBatch,
  35. kInteractive,
  36. kService,
  37. kAnonymous,
  38. kSelf,
  39. kAuthenticatedUser,
  40. kRestricted,
  41. kLocalSystem,
  42. kLocalService,
  43. kNetworkService,
  44. kBuiltinAdministrators,
  45. kBuiltinUsers,
  46. kBuiltinGuests,
  47. kUntrustedLabel,
  48. kLowLabel,
  49. kMediumLabel,
  50. kHighLabel,
  51. kSystemLabel,
  52. kWriteRestricted,
  53. kCreatorOwnerRights,
  54. kAllApplicationPackages,
  55. kAllRestrictedApplicationPackages
  56. };
  57. // This class is used to hold and generate SIDs.
  58. class BASE_EXPORT Sid {
  59. public:
  60. // Create a Sid from an AppContainer capability name. The name can be
  61. // completely arbitrary. Only available on Windows 10 and above.
  62. static absl::optional<Sid> FromNamedCapability(
  63. const wchar_t* capability_name);
  64. // Create a Sid from a known capability enumeration value. The Sids
  65. // match with the list defined in Windows 8.
  66. static absl::optional<Sid> FromKnownCapability(
  67. WellKnownCapability capability);
  68. // Create a SID from a well-known type.
  69. static absl::optional<Sid> FromKnownSid(WellKnownSid type);
  70. // Create a Sid from a SDDL format string, such as S-1-1-0.
  71. static absl::optional<Sid> FromSddlString(const wchar_t* sddl_sid);
  72. // Create a Sid from a PSID pointer.
  73. static absl::optional<Sid> FromPSID(const PSID sid);
  74. // Generate a random SID value.
  75. static absl::optional<Sid> GenerateRandomSid();
  76. // Create a SID for an integrity level RID.
  77. static absl::optional<Sid> FromIntegrityLevel(DWORD integrity_level);
  78. // Create a vector of SIDs from a vector of SDDL format strings.
  79. static absl::optional<std::vector<Sid>> FromSddlStringVector(
  80. const std::vector<const wchar_t*>& sddl_sids);
  81. // Create a vector of SIDs from a vector of capability names.
  82. static absl::optional<std::vector<Sid>> FromNamedCapabilityVector(
  83. const std::vector<const wchar_t*>& capability_names);
  84. // Create a vector of SIDs from a vector of well-known capability.
  85. static absl::optional<std::vector<Sid>> FromKnownCapabilityVector(
  86. const std::vector<WellKnownCapability>& capabilities);
  87. // Create a vector of SIDs from a vector of well-known sids.
  88. static absl::optional<std::vector<Sid>> FromKnownSidVector(
  89. const std::vector<WellKnownSid>& sids);
  90. Sid(const Sid&) = delete;
  91. Sid& operator=(const Sid&) = delete;
  92. Sid(Sid&& sid);
  93. Sid& operator=(Sid&&);
  94. ~Sid();
  95. // Returns sid as a PSID. This should only be used temporarily while the Sid
  96. // is still within scope.
  97. PSID GetPSID() const;
  98. // Converts the SID to a SDDL format string.
  99. absl::optional<std::wstring> ToSddlString() const;
  100. // Make a clone of the current Sid object.
  101. Sid Clone() const;
  102. // Is this Sid equal to another raw PSID?
  103. bool Equal(PSID sid) const;
  104. // Is this Sid equal to another Sid?
  105. bool operator==(const Sid& sid) const;
  106. // Is this Sid not equal to another Sid?
  107. bool operator!=(const Sid& sid) const;
  108. private:
  109. Sid(const void* sid, size_t length);
  110. std::vector<char> sid_;
  111. };
  112. } // namespace win
  113. } // namespace base
  114. #endif // BASE_WIN_SID_H_