Debian Codegraph @

src/openmsx/SspiUtils.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/src/openmsx/SspiUtils.cpp`/ifdef _WIN32
2
3 #include "SspiUtils.h"
4 #include <sddl.h>
5 #include <cassert>
6
7 //
8 // NOTE: This file MUST be kept in sync between the openmsx and openmsx-debugger projects
9 //
10
11 namespace openmsx {
12 namespace sspiutils {
13
14 SspiPackageBase::SspiPackageBase(StreamWrapper& userStream, const SEC_WCHAR* securityPackage)
15 : stream(userStream)
16 , cbMaxTokenSize(GetPackageMaxTokenSize(securityPackage))
17 {
18 memset(&hCreds, 0, sizeof(hCreds));
19 memset(&hContext, 0, sizeof(hContext));
20
21 if (!cbMaxTokenSize) {
22 // GetPackageMaxTokenSize failed
23 assert(false);
24 }
25 }
26
27 SspiPackageBase::~SspiPackageBase()
28 {
29 DeleteSecurityContext(&hContext);
30 FreeCredentialsHandle(&hCreds);
31 }
32
33 void InitTokenContextBuffer(PSecBufferDesc pSecBufferDesc, PSecBuffer pSecBuffer)
34 {
35 pSecBuffer->BufferType = SECBUFFER_TOKEN;
36 pSecBuffer->cbBuffer = 0;
37 pSecBuffer->pvBuffer = nullptr;
38
39 pSecBufferDesc->ulVersion = SECBUFFER_VERSION;
40 pSecBufferDesc->cBuffers = 1;
41 pSecBufferDesc->pBuffers = pSecBuffer;
42 }
43
44 void ClearContextBuffers(PSecBufferDesc pSecBufferDesc)
45 {
46 for (ULONG i = 0; i < pSecBufferDesc->cBuffers; i ++)
47 {
48 FreeContextBuffer(pSecBufferDesc->pBuffers[i].pvBuffer);
49 pSecBufferDesc->pBuffers[i].cbBuffer = 0;
50 pSecBufferDesc->pBuffers[i].pvBuffer = nullptr;
51 }
52 }
53
54 void DebugPrintSecurityStatus(const char* context, SECURITY_STATUS ss)
55 {
56 (void)&context;
57 (void)&ss;
58 #ifdef DEBUG
59 switch (ss)
60 {
61 case SEC_E_OK:
62 PRT_DEBUG(context << ": SEC_E_OK");
63 break;
64 case SEC_I_CONTINUE_NEEDED:
65 PRT_DEBUG(context << ": SEC_I_CONTINUE_NEEDED");
66 break;
67 case SEC_E_INVALID_TOKEN:
68 PRT_DEBUG(context << ": SEC_E_INVALID_TOKEN");
69 break;
70 case SEC_E_BUFFER_TOO_SMALL:
71 PRT_DEBUG(context << ": SEC_E_BUFFER_TOO_SMALL");
72 break;
73 case SEC_E_INVALID_HANDLE:
74 PRT_DEBUG(context << ": SEC_E_INVALID_HANDLE");
75 break;
76 case SEC_E_WRONG_PRINCIPAL:
77 PRT_DEBUG(context << ": SEC_E_WRONG_PRINCIPAL");
78 break;
79 default:
80 PRT_DEBUG(context << ": " << ss);
81 break;
82 }
83 #endif
84 }
85
86 void DebugPrintSecurityBool(const char* context, BOOL ret)
87 {
88 (void)&context;
89 (void)&ret;
90 #ifdef DEBUG
91 if (ret) {
92 PRT_DEBUG(context << ": true");
93 } else {
94 PRT_DEBUG(context << ": false - " << GetLastError());
95 }
96 #endif
97 }
98
99 void DebugPrintSecurityPackageName(PCtxtHandle phContext)
100 {
101 (void)&phContext;
102 #ifdef DEBUG
103 SecPkgContext_PackageInfoA package;
104 SECURITY_STATUS ss = QueryContextAttributesA(phContext, SECPKG_ATTR_PACKAGE_INFO, &package);
105 if (ss == SEC_E_OK) {
106 PRT_DEBUG("Using " << package.PackageInfo->Name << " package");
107 }
108 #endif
109 }
110
111 void DebugPrintSecurityPrincipalName(PCtxtHandle phContext)
112 {
113 (void)&phContext;
114 #ifdef DEBUG
115 SecPkgContext_NamesA name;
116 SECURITY_STATUS ss = QueryContextAttributesA(phContext, SECPKG_ATTR_NAMES, &name);
117 if (ss == SEC_E_OK) {
118 PRT_DEBUG("Client principal " << name.sUserName);
119 }
120 #endif
121 }
122
123 void DebugPrintSecurityDescriptor(PSECURITY_DESCRIPTOR psd)
124 {
125 (void)&psd;
126 #ifdef DEBUG
127 char* sddl;
128 BOOL ret = ConvertSecurityDescriptorToStringSecurityDescriptorA(
129 psd,
130 SDDL_REVISION,
131 OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
132 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION | LABEL_SECURITY_INFORMATION,
133 &sddl,
134 nullptr);
135 if (ret) {
136 PRT_DEBUG("SecurityDescriptor: " << sddl);
137 LocalFree(sddl);
138 }
139 #endif
140 }
141
142 // If successful, caller must free the results with LocalFree()
143 // If unsuccessful, returns null
144 PTOKEN_USER GetProcessToken()
145 {
146 PTOKEN_USER pToken = nullptr;
147
148 HANDLE hProcessToken;
149 BOOL ret = OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hProcessToken);
150 DebugPrintSecurityBool("OpenProcessToken", ret);
151 if (ret) {
152
153 DWORD cbToken;
154 ret = GetTokenInformation(hProcessToken, TokenUser, nullptr, 0, &cbToken);
155 assert(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER && cbToken);
156
157 pToken = (TOKEN_USER*)LocalAlloc(LMEM_ZEROINIT, cbToken);
158 if (pToken) {
159 ret = GetTokenInformation(hProcessToken, TokenUser, pToken, cbToken, &cbToken);
160 DebugPrintSecurityBool("GetTokenInformation", ret);
161 if (!ret) {
162 LocalFree(pToken);
163 pToken = nullptr;
164 }
165 }
166
167 CloseHandle(hProcessToken);
168 }
169
170 return pToken;
171 }
172
173 // If successful, caller must free the results with LocalFree()
174 // If unsuccessful, returns null
175 PSECURITY_DESCRIPTOR CreateCurrentUserSecurityDescriptor()
176 {
177 PSECURITY_DESCRIPTOR psd = nullptr;
178 PTOKEN_USER pToken = GetProcessToken();
179 if (pToken) {
180 PSID pUserSid = pToken->User.Sid;
181 const DWORD cbEachAce = sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD);
182 const DWORD cbACL = sizeof(ACL) + cbEachAce + GetLengthSid(pUserSid);
183
184 // Allocate the SD and the ACL in one allocation, so we only have one buffer to manage
185 // The SD structure ends with a pointer, so the start of the ACL will be well aligned
186 BYTE* buffer = (BYTE*)LocalAlloc(LMEM_ZEROINIT, SECURITY_DESCRIPTOR_MIN_LENGTH + cbACL);
187 if (buffer) {
188 psd = (PSECURITY_DESCRIPTOR)buffer;
189 PACL pacl = (PACL)(buffer + SECURITY_DESCRIPTOR_MIN_LENGTH);
190 PACCESS_ALLOWED_ACE pUserAce;
191 if (InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION) &&
192 InitializeAcl(pacl, cbACL, ACL_REVISION) &&
193 AddAccessAllowedAce(pacl, ACL_REVISION, ACCESS_ALL, pUserSid) &&
194 SetSecurityDescriptorDacl(psd, TRUE, pacl, FALSE) &&
195 // Need to set the Group and Owner on the SD in order to use it with AccessCheck()
196 GetAce(pacl, 0, (void**)&pUserAce) &&
197 SetSecurityDescriptorGroup(psd, &pUserAce->SidStart, FALSE) &&
198 SetSecurityDescriptorOwner(psd, &pUserAce->SidStart, FALSE))
199 {
200 buffer = nullptr;
201 } else {
202 psd = nullptr;
203 }
204
205 LocalFree(buffer);
206 }
207
208 LocalFree(pToken);
209 }
210
211 if (psd) {
212 assert(IsValidSecurityDescriptor(psd));
213 DebugPrintSecurityDescriptor(psd);
214 }
215
216 return psd;
217 }
218
219 unsigned long GetPackageMaxTokenSize(const SEC_WCHAR* package)
220 {
221 PSecPkgInfoW pkgInfo;
222 SECURITY_STATUS ss = QuerySecurityPackageInfoW(const_cast<SEC_WCHAR*>(package), &pkgInfo);
223 DebugPrintSecurityStatus("QuerySecurityPackageInfoW", ss);
224 if (ss != SEC_E_OK) {
225 return 0;
226 }
227
228 unsigned long cbMaxToken = pkgInfo->cbMaxToken;
229 FreeContextBuffer(pkgInfo);
230 return cbMaxToken;
231 }
232
233 bool Send(StreamWrapper& stream, void* buffer, uint32_t cb)
234 {
235 uint32_t sent = 0;
236 while (sent < cb)
237 {
238 uint32_t ret = stream.Write((char*)buffer + sent, cb - sent);
239 if (ret == STREAM_ERROR) {
240 return false;
241 }
242 sent += ret;
243 }
244 return true;
245 }
246
247 bool SendChunk(StreamWrapper& stream, void* buffer, uint32_t cb)
248 {
249 uint32_t nl = htonl(cb);
250 if (!Send(stream, &nl, sizeof(nl))) {
251 return false;
252 }
253 return Send(stream, buffer, cb);
254 }
255
256 bool Recv(StreamWrapper& stream, void* buffer, uint32_t cb)
257 {
258 uint32_t recvd = 0;
259 while (recvd < cb) {
260 uint32_t ret = stream.Read((char*)buffer + recvd, cb - recvd);
261 if (ret == STREAM_ERROR) {
262 return false;
263 }
264 recvd += ret;
265 }
266
267 return true;
268 }
269
270 bool RecvChunkSize(StreamWrapper& stream, uint32_t* pcb)
271 {
272 uint32_t cb;
273 bool ret = Recv(stream, &cb, sizeof(cb));
274 if (ret) {
275 *pcb = ntohl(cb);
276 }
277 return ret;
278 }
279
280 bool RecvChunk(StreamWrapper& stream, std::vector<char>& buffer, uint32_t cbMaxSize)
281 {
282 uint32_t cb;
283 if (!RecvChunkSize(stream, &cb) || cb > cbMaxSize) {
284 return false;
285 }
286 buffer.resize(cb);
287 if (!Recv(stream, &buffer[0], cb)) {
288 return false;
289 }
290 return true;
291 }
292
293 } // namespace sspiutils
294 } // namespace openmsx
295
296 #endif