~ubuntu-branches/ubuntu/utopic/openmsx-debugger/utopic

« back to all changes in this revision

Viewing changes to src/openmsx/SspiUtils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Joost Yervante Damad
  • Date: 2009-12-06 07:40:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091206074002-kssfkg1d6xbp6w9e
Tags: 0.0.0.svn20091206-1
New svn snapshot (Closes: #559612)

Show diffs side-by-side

added added

removed removed

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