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 "cloud_print/service/win/local_security_policy.h"
7 #include <atlsecurity.h>
11 #include "base/logging.h"
12 #include "base/string_util.h"
14 const wchar_t kSeServiceLogonRight
[] = L
"SeServiceLogonRight";
16 #ifndef STATUS_SUCCESS
17 #define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
23 class ScopedLsaMemory
{
25 ScopedLsaMemory() : lsa_memory_(NULL
) {
34 LsaFreeMemory(lsa_memory_
);
50 DISALLOW_COPY_AND_ASSIGN(ScopedLsaMemory
);
55 LocalSecurityPolicy::LocalSecurityPolicy() : policy_(NULL
) {
58 LocalSecurityPolicy::~LocalSecurityPolicy() {
62 void LocalSecurityPolicy::Close() {
69 bool LocalSecurityPolicy::Open() {
72 LSA_OBJECT_ATTRIBUTES attributes
= {0};
73 return STATUS_SUCCESS
==
74 ::LsaOpenPolicy(NULL
, &attributes
,
75 POLICY_CREATE_ACCOUNT
| POLICY_LOOKUP_NAMES
,
79 bool LocalSecurityPolicy::IsPrivilegeSet(const string16
& username
,
80 const string16
& privilage
) const {
83 if (!user_sid
.LoadAccount(username
.c_str())) {
84 LOG(ERROR
) << "Unable to load Sid for" << username
;
87 ScopedLsaMemory
<LSA_UNICODE_STRING
> rights
;
89 NTSTATUS status
= ::LsaEnumerateAccountRights(
90 policy_
, const_cast<SID
*>(user_sid
.GetPSID()), rights
.Receive(), &count
);
91 if (STATUS_SUCCESS
!= status
|| !rights
.Get())
93 for (size_t i
= 0; i
< count
; ++i
) {
94 if (privilage
== rights
.Get()[i
].Buffer
)
100 bool LocalSecurityPolicy::SetPrivilege(const string16
& username
,
101 const string16
& privilage
) {
104 if (!user_sid
.LoadAccount(username
.c_str())) {
105 LOG(ERROR
) << "Unable to load Sid for" << username
;
108 LSA_UNICODE_STRING privilege_string
;
109 string16
privilage_copy(privilage
);
110 privilege_string
.Buffer
= &privilage_copy
[0];
111 privilege_string
.Length
= wcslen(privilege_string
.Buffer
) *
112 sizeof(privilege_string
.Buffer
[0]);
113 privilege_string
.MaximumLength
= privilege_string
.Length
+
114 sizeof(privilege_string
.Buffer
[0]);
115 return STATUS_SUCCESS
==
116 ::LsaAddAccountRights(policy_
, const_cast<SID
*>(user_sid
.GetPSID()),
117 &privilege_string
, 1);