-
Notifications
You must be signed in to change notification settings - Fork 15
/
xoauth2_server.c
504 lines (456 loc) · 15.3 KB
/
xoauth2_server.c
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
/*
* Copyright (c) 2016 Moriyoshi Koizumi
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xoauth2_plugin.h"
static int xoauth2_plugin_server_mech_new(
void *glob_context,
sasl_server_params_t *params,
UNUSED(const char *challenge),
UNUSED(unsigned challenge_len),
void **pcontext)
{
int err;
const sasl_utils_t *utils = params->utils;
xoauth2_plugin_server_context_t *context;
context = SASL_malloc(sizeof(*context));
if (!context) {
SASL_seterror((utils->conn, 0, "Failed to allocate memory"));
return SASL_NOMEM;
}
context->settings = (xoauth2_plugin_server_settings_t *)glob_context;
context->state = 0;
context->resp.buf = NULL;
err = xoauth2_plugin_str_init(utils, &context->outbuf);
if (err != SASL_OK) {
SASL_free(context);
SASL_log((utils->conn, SASL_LOG_ERR, "failed to allocate buffer"));
return err;
}
*pcontext = context;
return SASL_OK;
}
static int append_string(const sasl_utils_t *utils, xoauth2_plugin_str_t *outbuf, const char *v, unsigned vlen)
{
int err;
const char *p;
const char *e = v + vlen;
err = xoauth2_plugin_str_alloc(utils, outbuf, outbuf->len + 2 + vlen * 2);
if (err != SASL_OK) {
return err;
}
outbuf->buf[outbuf->len++] = '"';
for (p = v; p < e; ++p) {
switch (*p) {
case 8:
outbuf->buf[outbuf->len++] = '\\';
outbuf->buf[outbuf->len++] = 'b';
break;
case 9:
outbuf->buf[outbuf->len++] = '\\';
outbuf->buf[outbuf->len++] = 't';
break;
case 10:
outbuf->buf[outbuf->len++] = '\\';
outbuf->buf[outbuf->len++] = 'n';
break;
case 12:
outbuf->buf[outbuf->len++] = '\\';
outbuf->buf[outbuf->len++] = 'f';
break;
case 13:
outbuf->buf[outbuf->len++] = '\\';
outbuf->buf[outbuf->len++] = 'r';
break;
case '"': case '\\':
outbuf->buf[outbuf->len++] = '\\';
/* fall-through */
default:
outbuf->buf[outbuf->len++] = *p;
break;
}
}
outbuf->buf[outbuf->len++] = '"';
return SASL_OK;
}
static int append_int(const sasl_utils_t *utils, xoauth2_plugin_str_t *outbuf, int n)
{
int err;
char buf[1024];
int len = snprintf(buf, sizeof(buf) - 1, "%d", n);
if (len < 0) {
return SASL_NOMEM;
}
return xoauth2_plugin_str_append(utils, outbuf, buf, (unsigned)len);
}
static int build_json_response(const sasl_utils_t *utils, xoauth2_plugin_str_t *outbuf, const char *status, xoauth2_plugin_server_settings_t *settings, xoauth2_plugin_auth_response_t *resp)
{
int err;
err = xoauth2_plugin_str_append(utils, outbuf, "{", 1);
if (err != SASL_OK) {
return err;
}
err = append_string(utils, outbuf, "status", 6);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, ":", 1);
if (err != SASL_OK) {
return err;
}
err = append_string(utils, outbuf, status, strlen(status));
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, ",", 1);
if (err != SASL_OK) {
return err;
}
err = append_string(utils, outbuf, "schemes", 6);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, ":", 1);
if (err != SASL_OK) {
return err;
}
err = append_string(utils, outbuf, resp->token_type, resp->token_type_len);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, ",", 1);
if (err != SASL_OK) {
return err;
}
err = append_string(utils, outbuf, "scope", 5);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, ":", 1);
if (err != SASL_OK) {
return err;
}
err = append_string(utils, outbuf, settings->scope, settings->scope_len);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, "}", 1);
if (err != SASL_OK) {
return err;
}
return SASL_OK;
}
static int xoauth2_plugin_server_mech_step1(
void *_context,
sasl_server_params_t *params,
const char *clientin,
unsigned clientin_len,
const char **serverout,
unsigned *serverout_len,
sasl_out_params_t *oparams)
{
const sasl_utils_t *utils = params->utils;
xoauth2_plugin_server_context_t *context = _context;
int err = SASL_OK;
xoauth2_plugin_auth_response_t resp;
int token_is_valid = 0;
*serverout = NULL;
*serverout_len = 0;
SASL_log((utils->conn, SASL_LOG_DEBUG, "xoauth2: step1"));
if (!context) {
err = SASL_BADPROT;
goto out;
}
if (!clientin) {
err = SASL_BADPROT;
goto out;
}
{
char *p, *e, *token_e;
resp.buf = SASL_malloc(clientin_len + 1);
if (!resp.buf) {
SASL_seterror((utils->conn, 0, "Failed to allocate memory"));
err = SASL_NOMEM;
goto out;
}
memcpy(resp.buf, clientin, clientin_len);
resp.buf[clientin_len] = '\0';
resp.buf_size = clientin_len;
p = resp.buf, e = resp.buf + resp.buf_size;
if (e - p < 5 || strncasecmp(p, "user=", 5) != 0) {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
p += 5;
resp.authid = p;
for (;;) {
if (p >= e) {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
if (*p == '\001') {
break;
}
++p;
}
*p = '\0';
resp.authid_len = p - resp.authid;
++p;
if (e - p < 5 || strncasecmp(p, "auth=", 5) != 0) {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
p += 5;
resp.token_type = p;
for (;;) {
if (p >= e) {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
if (*p == '\001') {
break;
}
++p;
}
*p = '\0';
token_e = p;
if (*++p != '\001') {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
if (p + 1 != e) {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
p = resp.token_type;
for (;;) {
if (p >= token_e) {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
if (*p == ' ') {
break;
}
++p;
}
*p = '\0';
resp.token_type_len = p - resp.token_type;
++p;
for (;;) {
if (p >= token_e) {
SASL_seterror((utils->conn, 0, "Failed to parse authentication information"));
err = SASL_BADPROT;
goto out;
}
if (*p != ' ') {
break;
}
++p;
}
resp.token = p;
resp.token_len = token_e - resp.token;
}
if (resp.token_type_len != 6 || strncasecmp(resp.token_type, "bearer", 6) != 0) {
/* not sure if we can return a plain error instead of a challange-impersonated error */
err = SASL_BADPROT;
SASL_seterror((utils->conn, 0, "unsupported token type: %s", resp.token_type));
goto out;
}
{
const char *requests[] = { SASL_AUX_OAUTH2_BEARER_TOKENS, NULL };
struct propval vals[1];
const char **p;
int nprops;
err = utils->prop_request(params->propctx, requests);
if (err != SASL_OK) {
/* not sure if we can return a plain error instead of a challange-impersonated error at this point */
SASL_seterror((utils->conn, 0, "failed to retrieve bearer tokens for the user %s", resp.authid));
goto out;
}
err = params->canon_user(utils->conn, resp.authid, 0, SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
if (err == SASL_OK) {
nprops = utils->prop_getnames(params->propctx, requests, vals);
if (nprops == sizeof(vals) / sizeof(*vals) && vals[0].name && vals[0].values) {
for (p = vals[0].values; *p; ++p) {
if (strlen(*p) == resp.token_len && strncmp(*p, resp.token, resp.token_len) == 0) {
token_is_valid = 1;
break;
}
}
} else {
SASL_log((utils->conn, SASL_LOG_ERR, "no bearer token found for user %s", resp.authid));
}
} else {
SASL_log((utils->conn, SASL_LOG_ERR, "failed to canonify user and get auxprops for user %s", resp.authid));
}
}
if (!token_is_valid) {
err = build_json_response(utils, &context->outbuf, "401", context->settings, &resp);
if (err != SASL_OK) {
SASL_log((utils->conn, SASL_LOG_ERR, "failed to allocate buffer"));
goto out;
}
context->state = 1;
context->resp = resp, resp.buf = NULL;
*serverout = context->outbuf.buf;
*serverout_len = context->outbuf.len;
err = SASL_CONTINUE;
goto out;
}
out:
if (resp.buf != NULL) {
memset(resp.buf, 0, resp.buf_size);
SASL_free(resp.buf);
}
return err;
}
static int xoauth2_plugin_server_mech_step2(
void *_context,
sasl_server_params_t *params,
const char *clientin,
unsigned clientin_len,
const char **serverout,
unsigned *serverout_len,
sasl_out_params_t *oparams)
{
const sasl_utils_t *utils = params->utils;
xoauth2_plugin_server_context_t *context = _context;
*serverout = NULL;
*serverout_len = 0;
SASL_log((utils->conn, SASL_LOG_DEBUG, "xoauth2: step2"));
if (!context) {
return SASL_BADPROT;
}
SASL_seterror((utils->conn, 0, "bearer token is not valid: %s", context->resp.token));
return params->transition ? SASL_TRANS: SASL_NOUSER;
}
static int xoauth2_plugin_server_mech_step(
void *_context,
sasl_server_params_t *params,
const char *clientin,
unsigned clientin_len,
const char **serverout,
unsigned *serverout_len,
sasl_out_params_t *oparams)
{
const sasl_utils_t *utils = params->utils;
xoauth2_plugin_server_context_t *context = _context;
switch (context->state) {
case 0:
return xoauth2_plugin_server_mech_step1(
context, params,
clientin, clientin_len,
serverout, serverout_len,
oparams);
case 1:
return xoauth2_plugin_server_mech_step2(
context, params,
clientin, clientin_len,
serverout, serverout_len,
oparams);
default:
return SASL_BADPROT;
}
}
static void xoauth2_plugin_server_mech_dispose(void *_context, const sasl_utils_t *utils)
{
xoauth2_plugin_server_context_t *context = _context;
if (!context) {
return;
}
if (context->resp.buf) {
memset(context->resp.buf, 0, context->resp.buf_size);
SASL_free(context->resp.buf);
context->resp.buf = NULL;
}
xoauth2_plugin_str_free(utils, &context->outbuf);
SASL_free(context);
}
static int xoauth2_server_plug_get_options(sasl_utils_t *utils, xoauth2_plugin_server_settings_t *settings)
{
int err;
err = utils->getopt(
utils->getopt_context,
"XOAUTH2",
"xoauth2_scope",
&settings->scope, &settings->scope_len);
if (err != SASL_OK || !settings->scope) {
SASL_log((utils->conn, SASL_LOG_NOTE, "xoauth2_scope is not set"));
settings->scope = "";
settings->scope_len = 0;
}
return SASL_OK;
}
static xoauth2_plugin_server_settings_t xoauth2_server_settings;
static sasl_server_plug_t xoauth2_server_plugins[] =
{
{
"XOAUTH2", /* mech_name */
0, /* max_ssf */
SASL_SEC_NOANONYMOUS
| SASL_SEC_PASS_CREDENTIALS, /* security_flags */
SASL_FEAT_WANT_CLIENT_FIRST
| SASL_FEAT_ALLOWS_PROXY, /* features */
NULL, /* glob_context */
&xoauth2_plugin_server_mech_new, /* mech_new */
&xoauth2_plugin_server_mech_step, /* mech_step */
&xoauth2_plugin_server_mech_dispose, /* mech_dispose */
NULL, /* mech_free */
NULL, /* setpass */
NULL, /* user_query */
NULL, /* idle */
NULL, /* mech_avail */
NULL /* spare */
}
};
int xoauth2_server_plug_init(
sasl_utils_t *utils,
int maxversion,
int *out_version,
sasl_server_plug_t **pluglist,
int *plugcount)
{
int err;
if (maxversion < SASL_SERVER_PLUG_VERSION) {
SASL_seterror((utils->conn, 0, "xoauth2: version mismatch"));
return SASL_BADVERS;
}
err = xoauth2_server_plug_get_options(utils, &xoauth2_server_settings);
if (err != SASL_OK) {
return err;
}
xoauth2_server_plugins[0].glob_context = &xoauth2_server_settings;
*out_version = SASL_SERVER_PLUG_VERSION;
*pluglist = xoauth2_server_plugins;
*plugcount = sizeof(xoauth2_server_plugins) / sizeof(*xoauth2_server_plugins);
return SASL_OK;
}