Move generic_handler.* to content\browser\webui since it's needed by all webui pages.
[chromium-blink-merge.git] / remoting / host / win / security_descriptor.cc
blob60e10124f7c77567b646fc272eddee476c1b044b
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"
7 #include <sddl.h>
9 #include "base/string16.h"
10 #include "base/utf_string_conversions.h"
12 namespace remoting {
14 ScopedSd ConvertSddlToSd(const std::string& sddl) {
15 PSECURITY_DESCRIPTOR raw_sd = NULL;
16 ULONG length = 0;
17 if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
18 UTF8ToUTF16(sddl).c_str(), SDDL_REVISION_1, &raw_sd, &length)) {
19 return ScopedSd();
22 ScopedSd sd(length);
23 memcpy(sd.get(), raw_sd, length);
25 LocalFree(raw_sd);
26 return sd.Pass();
29 // Converts a SID into a text string.
30 std::string ConvertSidToString(SID* sid) {
31 char16* c_sid_string = NULL;
32 if (!ConvertSidToStringSid(sid, &c_sid_string))
33 return std::string();
35 string16 sid_string(c_sid_string);
36 LocalFree(c_sid_string);
37 return UTF16ToUTF8(sid_string);
40 // Returns the logon SID of a token. Returns NULL if the token does not specify
41 // a logon SID or in case of an error.
42 ScopedSid GetLogonSid(HANDLE token) {
43 DWORD length = 0;
44 if (GetTokenInformation(token, TokenGroups, NULL, 0, &length) ||
45 GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
46 return ScopedSid();
49 TypedBuffer<TOKEN_GROUPS> groups(length);
50 if (!GetTokenInformation(token, TokenGroups, groups.get(), length, &length))
51 return ScopedSid();
53 for (uint32 i = 0; i < groups->GroupCount; ++i) {
54 if ((groups->Groups[i].Attributes & SE_GROUP_LOGON_ID) ==
55 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))
59 return ScopedSid();
61 return logon_sid.Pass();
65 return ScopedSid();
68 } // namespace remoting