1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
/*
* Copyright (C) 2013 10gen, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/
#ifdef _WIN32
#define SECURITY_WIN32 1 // Required for SSPI support.
#include "mongo/platform/basic.h"
#include <sasl/sasl.h>
#include <sasl/saslplug.h>
#include <sspi.h>
#include "mongo/base/init.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/base/status.h"
#include "mongo/util/scopeguard.h"
#include "mongo/util/text.h"
extern "C" int plain_client_plug_init(const sasl_utils_t *utils,
int maxversion,
int *out_version,
sasl_client_plug_t **pluglist,
int *plugcount);
extern "C" int crammd5_client_plug_init(const sasl_utils_t *utils,
int maxversion,
int *out_version,
sasl_client_plug_t **pluglist,
int *plugcount);
namespace mongo {
namespace {
/*
* SSPI client plugin impl
*/
// The SSPI plugin implements the GSSAPI interface.
char sspiPluginName[] = "GSSAPI";
// This structure is passed through each callback to us by the sasl glue code.
struct SspiConnContext {
CredHandle cred;
bool haveCred;
CtxtHandle ctx;
bool haveCtxt;
bool authComplete;
std::wstring nameToken;
std::string userPlusRealm;
SspiConnContext() :
haveCred(false),
haveCtxt(false),
authComplete(false)
{}
~SspiConnContext() {
if (haveCtxt) {
DeleteSecurityContext(&ctx);
}
if (haveCred) {
FreeCredentialsHandle(&cred);
}
}
};
// Utility function for fetching error text from Windows API function calls.
void HandleLastError(const sasl_utils_t* utils, DWORD errCode, const char* msg) {
char *err;
if (!FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR) &err,
0,
NULL)) {
return;
}
std::string buffer(mongoutils::str::stream() << "SSPI: " << msg << ": " << err);
utils->seterror(utils->conn, 0, "%s", buffer.c_str());
LocalFree(err);
}
int sspiClientMechNew(void *glob_context,
sasl_client_params_t *cparams,
void **conn_context) throw() {
// Prepare auth identity to pass to AcquireCredentialsHandle
SEC_WINNT_AUTH_IDENTITY authIdentity;
authIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
// Fetch username@realm.
sasl_getsimple_t *user_cb;
void *user_context;
int ret = cparams->utils->getcallback(cparams->utils->conn,
SASL_CB_USER,
(sasl_callback_ft *)&user_cb,
&user_context);
if (ret != SASL_OK) {
cparams->utils->seterror(cparams->utils->conn, 0, "getcallback user failed");
return ret;
}
const char *rawUserPlusRealm;
unsigned rawUserPlusRealmLength = 0;
ret = user_cb(user_context, SASL_CB_USER, &rawUserPlusRealm, &rawUserPlusRealmLength);
if (ret != SASL_OK) {
cparams->utils->seterror(cparams->utils->conn, 0, "user callback failed");
return ret;
}
std::string userPlusRealm(rawUserPlusRealm, rawUserPlusRealmLength);
// Parse out the username and realm.
size_t atSign = userPlusRealm.find('@');
if (atSign == std::string::npos) {
cparams->utils->seterror(cparams->utils->conn, 0, "no @REALM found in username");
return SASL_BADPARAM;
}
std::string utf8Username(userPlusRealm, 0, atSign);
std::wstring utf16Username(toWideString(utf8Username.c_str()));
authIdentity.UserLength = utf16Username.length();
authIdentity.User = reinterpret_cast<unsigned short*>(const_cast<wchar_t*>(
utf16Username.c_str()));
std::string utf8Domain(userPlusRealm, atSign + 1);
std::wstring utf16Domain(toWideString(utf8Domain.c_str()));
authIdentity.DomainLength = utf16Domain.length();
authIdentity.Domain = reinterpret_cast<unsigned short*>(const_cast<wchar_t*>(
utf16Domain.c_str()));
// Fetch password, if available.
authIdentity.PasswordLength = 0;
authIdentity.Password = NULL;
std::wstring utf16Password;
sasl_secret_t* password = NULL;
sasl_getsecret_t *pass_cb;
void *pass_context;
ret = cparams->utils->getcallback(cparams->utils->conn,
SASL_CB_PASS,
(sasl_callback_ft *)&pass_cb,
&pass_context);
if ((ret == SASL_OK) && pass_cb) {
ret = pass_cb(cparams->utils->conn, pass_context, SASL_CB_PASS, &password);
if ((ret == SASL_OK) && password) {
std::string utf8Password(reinterpret_cast<char*>(password->data), password->len);
utf16Password = toWideString(utf8Password.c_str());
authIdentity.PasswordLength = utf16Password.length();
authIdentity.Password = reinterpret_cast<unsigned short*>(
const_cast<wchar_t*>(
utf16Password.c_str()));
}
}
// Actually acquire the handle to the client credentials.
std::unique_ptr<SspiConnContext> pcctx(new SspiConnContext());
pcctx->userPlusRealm = userPlusRealm;
TimeStamp ignored;
SECURITY_STATUS status = AcquireCredentialsHandleW(NULL, // principal
L"kerberos",
SECPKG_CRED_OUTBOUND,
NULL, // LOGON id
&authIdentity, // auth data
NULL, // get key fn
NULL, // get key arg
&pcctx->cred,
&ignored);
if (status != SEC_E_OK) {
HandleLastError(cparams->utils, status, "AcquireCredentialsHandle");
return SASL_FAIL;
}
pcctx->haveCred = true;
// Compose target name token.
if (cparams->serverFQDN == NULL || strlen(cparams->serverFQDN) == 0) {
cparams->utils->seterror(cparams->utils->conn, 0, "SSPI: no serverFQDN");
return SASL_FAIL;
}
pcctx->nameToken = toWideString((std::string(cparams->service) +
"/" +
cparams->serverFQDN).c_str());
*conn_context = pcctx.release();
return SASL_OK;
}
int sspiValidateServerSecurityLayerOffering(SspiConnContext* pcctx,
sasl_client_params_t* cparams,
const char *serverin,
unsigned serverinlen) {
std::unique_ptr<char[]> message(new char[serverinlen]);
memcpy(message.get(), serverin, serverinlen);
SecBuffer wrapBufs[2];
SecBufferDesc wrapBufDesc;
wrapBufDesc.cBuffers = 2;
wrapBufDesc.pBuffers = wrapBufs;
wrapBufDesc.ulVersion = SECBUFFER_VERSION;
wrapBufs[0].cbBuffer = serverinlen;
wrapBufs[0].BufferType = SECBUFFER_STREAM;
wrapBufs[0].pvBuffer = message.get();
wrapBufs[1].cbBuffer = 0;
wrapBufs[1].BufferType = SECBUFFER_DATA;
wrapBufs[1].pvBuffer = NULL;
SECURITY_STATUS status = DecryptMessage(&pcctx->ctx,
&wrapBufDesc,
0,
NULL);
if (status != SEC_E_OK) {
HandleLastError(cparams->utils, status, "DecryptMessage");
return SASL_FAIL;
}
// Validate the server's plaintext message.
// Length (as per RFC 4752)
if (wrapBufs[1].cbBuffer < 4) {
cparams->utils->seterror(cparams->utils->conn, 0, "SSPI: server message is too short");
return SASL_FAIL;
}
// First bit of first byte set, indicating that the client may elect to use no
// security layer. As a client we are uninterested in any of the other features the
// server offers and thus we ignore the other bits.
if (!(static_cast<char*>(wrapBufs[1].pvBuffer)[0] & 1)) {
cparams->utils->seterror(cparams->utils->conn, 0,
"SSPI: server does not support the required security layer");
return SASL_BADAUTH;
}
return SASL_OK;
}
int sspiSendClientAuthzId(SspiConnContext* pcctx,
sasl_client_params_t* cparams,
const char *serverin,
unsigned serverinlen,
const char **clientout,
unsigned *clientoutlen,
sasl_out_params_t* oparams) {
// Ensure server response is decryptable.
int decryptStatus = sspiValidateServerSecurityLayerOffering(pcctx,
cparams,
serverin,
serverinlen);
if (decryptStatus != SASL_OK) {
return decryptStatus;
}
// Fill in AUTHID and AUTHZID fields in oparams.
int ret = cparams->canon_user(cparams->utils->conn,
pcctx->userPlusRealm.c_str(),
0,
SASL_CU_AUTHID | SASL_CU_AUTHZID,
oparams);
// Reply to server with security capability and authz name.
SecPkgContext_Sizes sizes;
SECURITY_STATUS status = QueryContextAttributes(&pcctx->ctx,
SECPKG_ATTR_SIZES,
&sizes);
if (status != SEC_E_OK) {
HandleLastError(cparams->utils, status, "QueryContextAttributes(sizes)");
return SASL_FAIL;
}
// See RFC4752.
int plaintextMessageSize = 4 + pcctx->userPlusRealm.size();
std::unique_ptr<char[]> message(new char[sizes.cbSecurityTrailer +
plaintextMessageSize +
sizes.cbBlockSize]);
char* plaintextMessage = message.get() + sizes.cbSecurityTrailer;
plaintextMessage[0] = 1; // LAYER_NONE
plaintextMessage[1] = 0;
plaintextMessage[2] = 0;
plaintextMessage[3] = 0;
memcpy(&plaintextMessage[4], pcctx->userPlusRealm.c_str(), pcctx->userPlusRealm.size());
SecBuffer wrapBufs[3];
SecBufferDesc wrapBufDesc;
wrapBufDesc.cBuffers = 3;
wrapBufDesc.pBuffers = wrapBufs;
wrapBufDesc.ulVersion = SECBUFFER_VERSION;
wrapBufs[0].cbBuffer = sizes.cbSecurityTrailer;
wrapBufs[0].BufferType = SECBUFFER_TOKEN;
wrapBufs[0].pvBuffer = message.get();
wrapBufs[1].cbBuffer = plaintextMessageSize;
wrapBufs[1].BufferType = SECBUFFER_DATA;
wrapBufs[1].pvBuffer = message.get() + sizes.cbSecurityTrailer;
wrapBufs[2].cbBuffer = sizes.cbBlockSize;
wrapBufs[2].BufferType = SECBUFFER_PADDING;
wrapBufs[2].pvBuffer = message.get() + sizes.cbSecurityTrailer + plaintextMessageSize;
status = EncryptMessage(&pcctx->ctx,
SECQOP_WRAP_NO_ENCRYPT,
&wrapBufDesc,
0);
if (status != SEC_E_OK) {
HandleLastError(cparams->utils, status, "EncryptMessage");
return SASL_FAIL;
}
// Create the message to send to server.
*clientoutlen = wrapBufs[0].cbBuffer + wrapBufs[1].cbBuffer + wrapBufs[2].cbBuffer;
char *newoutbuf = static_cast<char*>(cparams->utils->malloc(*clientoutlen));
memcpy(newoutbuf,
wrapBufs[0].pvBuffer,
wrapBufs[0].cbBuffer);
memcpy(newoutbuf + wrapBufs[0].cbBuffer,
wrapBufs[1].pvBuffer,
wrapBufs[1].cbBuffer);
memcpy(newoutbuf + wrapBufs[0].cbBuffer + wrapBufs[1].cbBuffer,
wrapBufs[2].pvBuffer,
wrapBufs[2].cbBuffer);
*clientout = newoutbuf;
return SASL_OK;
}
int sspiClientMechStep(void *conn_context,
sasl_client_params_t *cparams,
const char *serverin,
unsigned serverinlen,
sasl_interact_t **prompt_need,
const char **clientout,
unsigned *clientoutlen,
sasl_out_params_t *oparams) throw() {
SspiConnContext* pcctx = static_cast<SspiConnContext*>(conn_context);
*clientout = NULL;
*clientoutlen = 0;
if (pcctx->authComplete) {
return sspiSendClientAuthzId(pcctx, cparams,
serverin, serverinlen,
clientout, clientoutlen,
oparams);
}
SecBufferDesc inbuf;
SecBuffer inBufs[1];
SecBufferDesc outbuf;
SecBuffer outBufs[1];
if (pcctx->haveCtxt) {
// If we already have a context, we now have data to send.
// Put this data in an inbuf.
inbuf.ulVersion = SECBUFFER_VERSION;
inbuf.cBuffers = 1;
inbuf.pBuffers = inBufs;
inBufs[0].pvBuffer = const_cast<char*>(serverin);
inBufs[0].cbBuffer = serverinlen;
inBufs[0].BufferType = SECBUFFER_TOKEN;
}
outbuf.ulVersion = SECBUFFER_VERSION;
outbuf.cBuffers = 1;
outbuf.pBuffers = outBufs;
outBufs[0].pvBuffer = NULL;
outBufs[0].cbBuffer = 0;
outBufs[0].BufferType = SECBUFFER_TOKEN;
ULONG contextAttr = 0;
SECURITY_STATUS status = InitializeSecurityContextW(&pcctx->cred,
pcctx->haveCtxt ? &pcctx->ctx : NULL,
const_cast<wchar_t*>(
pcctx->nameToken.c_str()),
ISC_REQ_ALLOCATE_MEMORY |
ISC_REQ_MUTUAL_AUTH,
0,
SECURITY_NETWORK_DREP,
(pcctx->haveCtxt ? &inbuf : NULL),
0,
&pcctx->ctx,
&outbuf,
&contextAttr,
NULL);
if (status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
HandleLastError(cparams->utils, status, "InitializeSecurityContext");
return SASL_FAIL;
}
ON_BLOCK_EXIT(FreeContextBuffer, outbuf.pBuffers[0].pvBuffer);
pcctx->haveCtxt = true;
if (status == SEC_E_OK) {
// Send back nothing and wait for the server to reply with the security capabilities
*clientout = NULL;
*clientoutlen = 0;
pcctx->authComplete = true;
return SASL_CONTINUE;
}
char *newoutbuf = static_cast<char*>(cparams->utils->malloc(outBufs[0].cbBuffer));
*clientoutlen = outBufs[0].cbBuffer;
memcpy(newoutbuf, outBufs[0].pvBuffer, *clientoutlen);
*clientout = newoutbuf;
return SASL_CONTINUE;
}
void sspiClientMechDispose(void *conn_context, const sasl_utils_t *utils) {
SspiConnContext* pcctx = static_cast<SspiConnContext*>(conn_context);
delete pcctx;
}
void sspiClientMechFree(void *glob_context, const sasl_utils_t *utils) {
}
sasl_client_plug_t sspiClientPlugin[] = {
{
sspiPluginName, /* mechanism name */
112, /* TODO: (taken from gssapi) best mech additional security layer strength factor */
SASL_SEC_NOPLAINTEXT /* eam: copied from gssapi */
| SASL_SEC_NOACTIVE
| SASL_SEC_NOANONYMOUS
| SASL_SEC_MUTUAL_AUTH
| SASL_SEC_PASS_CREDENTIALS, /* security_flags */
SASL_FEAT_NEEDSERVERFQDN
| SASL_FEAT_WANT_CLIENT_FIRST
| SASL_FEAT_ALLOWS_PROXY,
NULL, /* required prompt ids, NULL = user/pass only */
NULL, /* global state for mechanism */
sspiClientMechNew,
sspiClientMechStep,
sspiClientMechDispose,
sspiClientMechFree,
NULL,
NULL,
NULL
}
};
int sspiClientPluginInit(const sasl_utils_t *utils,
int max_version,
int *out_version,
sasl_client_plug_t **pluglist,
int *plugcount) {
if (max_version < SASL_CLIENT_PLUG_VERSION) {
utils->seterror(utils->conn, 0, "Wrong SSPI version");
return SASL_BADVERS;
}
*out_version = SASL_CLIENT_PLUG_VERSION;
*pluglist = sspiClientPlugin;
*plugcount = 1;
return SASL_OK;
}
/**
* Registers the plugin at process initialization time.
* Must be run after the AllocatorsAndMutexes are registered, but before the ClientContext is
* created.
*/
MONGO_INITIALIZER_WITH_PREREQUISITES(SaslSspiClientPlugin,
("CyrusSaslAllocatorsAndMutexes",
"CyrusSaslClientContext"))
(InitializerContext*) {
int ret = sasl_client_add_plugin(sspiPluginName,
sspiClientPluginInit);
if (SASL_OK != ret) {
return Status(ErrorCodes::UnknownError,
mongoutils::str::stream() << "could not add SASL Client SSPI plugin "
<< sspiPluginName << ": " << sasl_errstring(ret, NULL, NULL));
}
return Status::OK();
}
MONGO_INITIALIZER_WITH_PREREQUISITES(SaslCramClientPlugin,
("CyrusSaslAllocatorsAndMutexes",
"CyrusSaslClientContext"))
(InitializerContext*) {
int ret = sasl_client_add_plugin("CRAMMD5",
crammd5_client_plug_init);
if (SASL_OK != ret) {
return Status(ErrorCodes::UnknownError,
mongoutils::str::stream() << "Could not add SASL Client CRAM-MD5 plugin "
<< sspiPluginName << ": " << sasl_errstring(ret, NULL, NULL));
}
return Status::OK();
}
MONGO_INITIALIZER_WITH_PREREQUISITES(SaslPlainClientPlugin,
("CyrusSaslAllocatorsAndMutexes",
"CyrusSaslClientContext"))
(InitializerContext*) {
int ret = sasl_client_add_plugin("PLAIN",
plain_client_plug_init);
if (SASL_OK != ret) {
return Status(ErrorCodes::UnknownError,
mongoutils::str::stream() << "Could not add SASL Client PLAIN plugin "
<< sspiPluginName << ": " << sasl_errstring(ret, NULL, NULL));
}
return Status::OK();
}
} // namespace
} // namespace mongo
#endif // ifdef _WIN32
|