blob: 985f00cbb17d0246adff3de338917b108672f3dd [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Security.cxx
20
21#include <rfb_win32/Security.h>
22#include <rfb_win32/DynamicFn.h>
23#include <rfb/LogWriter.h>
24
25#include <lmcons.h>
26#include <Accctrl.h>
27#include <list>
28
29using namespace rfb;
30using namespace rfb::win32;
31
32static LogWriter vlog("SecurityWin32");
33
34
35Trustee::Trustee(const TCHAR* name,
36 TRUSTEE_FORM form,
37 TRUSTEE_TYPE type) {
38 pMultipleTrustee = 0;
39 MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
40 TrusteeForm = form;
41 TrusteeType = type;
42 ptstrName = (TCHAR*)name;
43}
44
45
46ExplicitAccess::ExplicitAccess(const TCHAR* name,
47 TRUSTEE_FORM type,
48 DWORD perms,
49 ACCESS_MODE mode,
50 DWORD inherit) {
51 Trustee = rfb::win32::Trustee(name, type);
52 grfAccessPermissions = perms;
53 grfAccessMode = mode;
54 grfInheritance = inherit;
55}
56
57
58AccessEntries::AccessEntries() : entries(0), entry_count(0) {}
59
60AccessEntries::~AccessEntries() {
61 delete [] entries;
62}
63
64void AccessEntries::allocMinEntries(int count) {
65 if (count > entry_count) {
66 EXPLICIT_ACCESS* new_entries = new EXPLICIT_ACCESS[entry_count+1];
67 if (entries) {
68 memcpy(new_entries, entries, sizeof(EXPLICIT_ACCESS) * entry_count);
69 delete entries;
70 }
71 entries = new_entries;
72 }
73}
74
75void AccessEntries::addEntry(const TCHAR* trusteeName,
76 DWORD permissions,
77 ACCESS_MODE mode) {
78 allocMinEntries(entry_count+1);
79 ZeroMemory(&entries[entry_count], sizeof(EXPLICIT_ACCESS));
80 entries[entry_count] = ExplicitAccess(trusteeName, TRUSTEE_IS_NAME, permissions, mode);
81 entry_count++;
82}
83
84void AccessEntries::addEntry(const PSID sid,
85 DWORD permissions,
86 ACCESS_MODE mode) {
87 allocMinEntries(entry_count+1);
88 ZeroMemory(&entries[entry_count], sizeof(EXPLICIT_ACCESS));
89 entries[entry_count] = ExplicitAccess((TCHAR*)sid, TRUSTEE_IS_SID, permissions, mode);
90 entry_count++;
91}
92
93
94PSID Sid::copySID(const PSID sid) {
95 if (!IsValidSid(sid))
96 throw rdr::Exception("invalid SID in copyPSID");
97 PSID buf = (PSID)new rdr::U8[GetLengthSid(sid)];
98 if (!CopySid(GetLengthSid(sid), buf, sid))
99 throw rdr::SystemException("CopySid failed", GetLastError());
100 return buf;
101}
102
103void Sid::setSID(const PSID sid) {
104 delete [] buf;
105 buf = (rdr::U8*)copySID(sid);
106}
107
108void Sid::getUserNameAndDomain(TCHAR** name, TCHAR** domain) {
109 DWORD nameLen = 0;
110 DWORD domainLen = 0;
111 SID_NAME_USE use;
112 LookupAccountSid(0, (PSID)buf, 0, &nameLen, 0, &domainLen, &use);
113 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
114 throw rdr::SystemException("Unable to determine SID name lengths", GetLastError());
115 vlog.info("nameLen=%d, domainLen=%d, use=%d", nameLen, domainLen, use);
116 *name = new TCHAR[nameLen];
117 *domain = new TCHAR[domainLen];
118 if (!LookupAccountSid(0, (PSID)buf, *name, &nameLen, *domain, &domainLen, &use))
119 throw rdr::SystemException("Unable to lookup account SID", GetLastError());
120}
121
122
123Sid::Administrators::Administrators() {
124 PSID sid = 0;
125 SID_IDENTIFIER_AUTHORITY ntAuth = SECURITY_NT_AUTHORITY;
126 if (!AllocateAndInitializeSid(&ntAuth, 2,
127 SECURITY_BUILTIN_DOMAIN_RID,
128 DOMAIN_ALIAS_RID_ADMINS,
129 0, 0, 0, 0, 0, 0, &sid))
130 throw rdr::SystemException("Sid::Administrators", GetLastError());
131 setSID(sid);
132 FreeSid(sid);
133}
134
135Sid::SYSTEM::SYSTEM() {
136 PSID sid = 0;
137 SID_IDENTIFIER_AUTHORITY ntAuth = SECURITY_NT_AUTHORITY;
138 if (!AllocateAndInitializeSid(&ntAuth, 1,
139 SECURITY_LOCAL_SYSTEM_RID,
140 0, 0, 0, 0, 0, 0, 0, &sid))
141 throw rdr::SystemException("Sid::SYSTEM", GetLastError());
142 setSID(sid);
143 FreeSid(sid);
144}
145
146Sid::FromToken::FromToken(HANDLE h) {
147 DWORD required = 0;
148 GetTokenInformation(h, TokenUser, 0, 0, &required);
149 rdr::U8Array tmp(required);
150 if (!GetTokenInformation(h, TokenUser, tmp.buf, required, &required))
151 throw rdr::SystemException("GetTokenInformation", GetLastError());
152 TOKEN_USER* tokenUser = (TOKEN_USER*)tmp.buf;
153 setSID(tokenUser->User.Sid);
154}
155
156
157PACL rfb::win32::CreateACL(const AccessEntries& ae, PACL existing_acl) {
158 typedef DWORD (WINAPI *_SetEntriesInAcl_proto) (ULONG, PEXPLICIT_ACCESS, PACL, PACL*);
159#ifdef UNICODE
160 const char* fnName = "SetEntriesInAclW";
161#else
162 const char* fnName = "SetEntriesInAclA";
163#endif
164 DynamicFn<_SetEntriesInAcl_proto> _SetEntriesInAcl(_T("advapi32.dll"), fnName);
165 if (!_SetEntriesInAcl.isValid())
166 throw rdr::SystemException("CreateACL failed; no SetEntriesInAcl", ERROR_CALL_NOT_IMPLEMENTED);
167 PACL new_dacl;
168 DWORD result;
169 if ((result = (*_SetEntriesInAcl)(ae.entry_count, ae.entries, existing_acl, &new_dacl)) != ERROR_SUCCESS)
170 throw rdr::SystemException("SetEntriesInAcl", result);
171 return new_dacl;
172}
173
174
175PSECURITY_DESCRIPTOR rfb::win32::CreateSdWithDacl(const PACL dacl) {
176 SECURITY_DESCRIPTOR absSD;
177 if (!InitializeSecurityDescriptor(&absSD, SECURITY_DESCRIPTOR_REVISION))
178 throw rdr::SystemException("InitializeSecurityDescriptor", GetLastError());
179 Sid::SYSTEM owner;
180 if (!SetSecurityDescriptorOwner(&absSD, owner, FALSE))
181 throw rdr::SystemException("SetSecurityDescriptorOwner", GetLastError());
182 Sid::Administrators group;
183 if (!SetSecurityDescriptorGroup(&absSD, group, FALSE))
184 throw rdr::SystemException("SetSecurityDescriptorGroupp", GetLastError());
185 if (!SetSecurityDescriptorDacl(&absSD, TRUE, dacl, FALSE))
186 throw rdr::SystemException("SetSecurityDescriptorDacl", GetLastError());
187 DWORD sdSize = GetSecurityDescriptorLength(&absSD);
188 SecurityDescriptorPtr sd(sdSize);
189 if (!MakeSelfRelativeSD(&absSD, sd, &sdSize))
190 throw rdr::SystemException("MakeSelfRelativeSD", GetLastError());
191 return sd.takeSD();
192}