1 // Copyright (c) 2012 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.
5 #include "remoting/host/win/security_descriptor.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
14 ScopedSd
ConvertSddlToSd(const std::string
& sddl
) {
15 PSECURITY_DESCRIPTOR raw_sd
= nullptr;
17 if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
18 base::UTF8ToUTF16(sddl
).c_str(), SDDL_REVISION_1
, &raw_sd
, &length
)) {
23 memcpy(sd
.get(), raw_sd
, length
);
29 // Converts a SID into a text string.
30 std::string
ConvertSidToString(SID
* sid
) {
31 base::char16
* c_sid_string
= nullptr;
32 if (!ConvertSidToStringSid(sid
, &c_sid_string
))
35 base::string16
sid_string(c_sid_string
);
36 LocalFree(c_sid_string
);
37 return base::UTF16ToUTF8(sid_string
);
40 // Returns the logon SID of a token. Returns nullptr if the token does not
41 // specify a logon SID or in case of an error.
42 ScopedSid
GetLogonSid(HANDLE token
) {
44 if (GetTokenInformation(token
, TokenGroups
, nullptr, 0, &length
) ||
45 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
49 TypedBuffer
<TOKEN_GROUPS
> groups(length
);
50 if (!GetTokenInformation(token
, TokenGroups
, groups
.get(), length
, &length
))
53 for (uint32 i
= 0; i
< groups
->GroupCount
; ++i
) {
54 if ((groups
->Groups
[i
].Attributes
& SE_GROUP_LOGON_ID
) ==
56 length
= GetLengthSid(groups
->Groups
[i
].Sid
);
57 ScopedSid
logon_sid(length
);
58 if (!CopySid(length
, logon_sid
.get(), groups
->Groups
[i
].Sid
))
61 return logon_sid
.Pass();
68 bool MakeScopedAbsoluteSd(const ScopedSd
& relative_sd
,
69 ScopedSd
* absolute_sd
,
75 DWORD absolute_sd_size
= 0;
80 if (MakeAbsoluteSD(relative_sd
.get(),
91 GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
96 ScopedSd
local_absolute_sd(absolute_sd_size
);
97 ScopedAcl
local_dacl(dacl_size
);
98 ScopedSid
local_group(group_size
);
99 ScopedSid
local_owner(owner_size
);
100 ScopedAcl
local_sacl(sacl_size
);
102 // Do the conversion.
103 if (!MakeAbsoluteSD(relative_sd
.get(),
104 local_absolute_sd
.get(),
117 absolute_sd
->Swap(local_absolute_sd
);
118 dacl
->Swap(local_dacl
);
119 group
->Swap(local_group
);
120 owner
->Swap(local_owner
);
121 sacl
->Swap(local_sacl
);
125 } // namespace remoting