-
Notifications
You must be signed in to change notification settings - Fork 15
/
xoauth2_client.c
415 lines (378 loc) · 11.7 KB
/
xoauth2_client.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
/*
* 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 <string.h>
#include <limits.h>
#include "xoauth2_plugin.h"
static int xoauth2_plugin_client_mech_new(
UNUSED(void *glob_context),
sasl_client_params_t *params,
void **pcontext)
{
int err;
const sasl_utils_t *utils = params->utils;
xoauth2_plugin_client_context_t *context;
context = SASL_malloc(sizeof(*context));
if (!context) {
SASL_seterror((utils->conn, 0, "Failed to allocate memory"));
return SASL_NOMEM;
}
context->state = 0;
context->resp.buf = NULL;
err = xoauth2_plugin_str_init(utils, &context->outbuf);
if (err != SASL_OK) {
SASL_free(context);
return err;
}
*pcontext = context;
return SASL_OK;
}
static int build_client_response(const sasl_utils_t *utils, xoauth2_plugin_str_t *outbuf, xoauth2_plugin_auth_response_t *resp)
{
int err;
err = xoauth2_plugin_str_append(utils, outbuf, "user=", 5);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, resp->authid, resp->authid_len);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, "\1", 1);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, "auth=", 5);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(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 = xoauth2_plugin_str_append(utils, outbuf, resp->token, resp->token_len);
if (err != SASL_OK) {
return err;
}
err = xoauth2_plugin_str_append(utils, outbuf, "\1\1", 2);
if (err != SASL_OK) {
return err;
}
return SASL_OK;
}
static sasl_interact_t *find_prompt(sasl_interact_t *prompts, unsigned id)
{
sasl_interact_t *p;
for (p = prompts; p->id != SASL_CB_LIST_END; ++p) {
if (p->id == id) {
return p;
}
}
return NULL;
}
static int get_prompt_value(sasl_interact_t *prompts, unsigned id, const char **result, unsigned *result_len)
{
int err;
sasl_interact_t *prompt;
prompt = find_prompt(prompts, id);
if (!prompt) {
return SASL_FAIL;
}
*result = (const char *)prompt->result;
*result_len = prompt->len;
return SASL_OK;
}
static int get_cb_value(const sasl_utils_t *utils, unsigned id, const char **result, unsigned *result_len)
{
int err;
switch (id) {
case SASL_CB_PASS:
{
sasl_getsecret_t *cb;
void *cb_ctx;
sasl_secret_t *secret;
err = utils->getcallback(utils->conn, id, (sasl_callback_ft *)&cb, &cb_ctx);
if (err != SASL_OK) {
return err;
}
err = cb(utils->conn, cb_ctx, id, &secret);
if (err != SASL_OK) {
return err;
}
if (secret->len >= UINT_MAX) {
return SASL_BADPROT;
}
*result = secret->data;
*result_len = secret->len;
}
break;
case SASL_CB_USER:
case SASL_CB_AUTHNAME:
case SASL_CB_LANGUAGE:
case SASL_CB_CNONCE:
{
sasl_getsimple_t *cb;
void *cb_ctx;
err = utils->getcallback(utils->conn, id, (sasl_callback_ft *)&cb, &cb_ctx);
if (err != SASL_OK) {
return err;
}
err = cb(cb_ctx, id, result, result_len);
}
break;
default:
err = SASL_FAIL;
}
return err;
}
static int xoauth2_plugin_client_mech_step1(
void *_context,
sasl_client_params_t *params,
const char *serverin,
unsigned serverin_len,
sasl_interact_t **prompt_need,
const char **clientout,
unsigned *clientout_len,
sasl_out_params_t *oparams)
{
const sasl_utils_t *utils = params->utils;
xoauth2_plugin_client_context_t *context = _context;
int err = SASL_OK;
xoauth2_plugin_auth_response_t resp;
int authid_wanted = 1;
int password_wanted = 1;
sasl_interact_t *prompt_returned = NULL;
*clientout = NULL;
*clientout_len = 0;
SASL_log((utils->conn, SASL_LOG_DEBUG, "xoauth2: step1"));
if (!context) {
return SASL_BADPROT;
}
if (prompt_need && *prompt_need) {
if (SASL_OK == get_prompt_value(*prompt_need, SASL_CB_AUTHNAME, (const char **)&resp.authid, &resp.authid_len)) {
authid_wanted = 0;
}
}
if (authid_wanted) {
err = get_cb_value(utils, SASL_CB_AUTHNAME, (const char **)&resp.authid, &resp.authid_len);
switch (err) {
case SASL_OK:
authid_wanted = 0;
break;
case SASL_INTERACT:
break;
default:
goto out;
}
}
if (prompt_need && *prompt_need) {
if (SASL_OK == get_prompt_value(*prompt_need, SASL_CB_PASS, (const char **)&resp.token, &resp.token_len)) {
password_wanted = 0;
}
}
if (password_wanted) {
err = get_cb_value(utils, SASL_CB_PASS, (const char **)&resp.token, &resp.token_len);
switch (err) {
case SASL_OK:
password_wanted = 0;
break;
case SASL_INTERACT:
break;
default:
goto out;
}
}
if (!authid_wanted && !password_wanted) {
err = params->canon_user(
utils->conn, resp.authid, resp.authid_len,
SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
if (err != SASL_OK) {
goto out;
}
resp.token_type = "Bearer";
resp.token_type_len = 6;
err = build_client_response(utils, &context->outbuf, &resp);
if (err != SASL_OK) {
goto out;
}
*clientout = context->outbuf.buf;
*clientout_len = context->outbuf.len;
context->state = 1;
err = SASL_CONTINUE;
} else {
const size_t prompts = authid_wanted + password_wanted + 1;
sasl_interact_t *p;
prompt_returned = SASL_malloc(sizeof(sasl_interact_t) * prompts);
if (!prompt_returned) {
SASL_log((utils->conn, SASL_LOG_ERR, "failed to allocate buffer"));
err = SASL_NOMEM;
goto out;
}
memset(prompt_returned, 0, sizeof(sasl_interact_t) * prompts);
p = prompt_returned;
if (authid_wanted) {
p->id = SASL_CB_AUTHNAME;
p->challenge = "Authentication Name";
p->prompt = "Authentication ID";
p->defresult = NULL;
++p;
}
if (password_wanted) {
p->id = SASL_CB_PASS;
p->challenge = "Password";
p->prompt = "Password";
p->defresult = NULL;
++p;
}
p->id = SASL_CB_LIST_END;
p->challenge = NULL;
p->prompt = NULL;
p->defresult = NULL;
err = SASL_INTERACT;
}
out:
if (prompt_need) {
if (*prompt_need) {
SASL_free(*prompt_need);
*prompt_need = NULL;
}
if (prompt_returned) {
*prompt_need = prompt_returned;
}
}
return err;
}
static int xoauth2_plugin_client_mech_step2(
void *_context,
sasl_client_params_t *params,
const char *serverin,
unsigned serverin_len,
sasl_interact_t **prompt_need,
const char **clientout,
unsigned *clientout_len,
sasl_out_params_t *oparams)
{
const sasl_utils_t *utils = params->utils;
xoauth2_plugin_client_context_t *context = _context;
int err = SASL_OK;
xoauth2_plugin_auth_response_t resp;
*clientout = NULL;
*clientout_len = 0;
SASL_log((utils->conn, SASL_LOG_DEBUG, "xoauth2: step2"));
if (!context) {
return SASL_BADPROT;
}
*clientout = "";
*clientout_len = 0;
context->state = 2;
return SASL_OK;
}
static int xoauth2_plugin_client_mech_step(
void *_context,
sasl_client_params_t *params,
const char *serverin,
unsigned serverin_len,
sasl_interact_t **prompt_need,
const char **clientout,
unsigned *clientout_len,
sasl_out_params_t *oparams)
{
xoauth2_plugin_client_context_t *context = _context;
switch (context->state) {
case 0:
return xoauth2_plugin_client_mech_step1(
context,
params,
serverin,
serverin_len,
prompt_need,
clientout,
clientout_len,
oparams
);
case 1:
return xoauth2_plugin_client_mech_step2(
context,
params,
serverin,
serverin_len,
prompt_need,
clientout,
clientout_len,
oparams
);
}
return SASL_BADPROT;
}
static void xoauth2_plugin_client_mech_dispose(
void *_context,
const sasl_utils_t *utils)
{
xoauth2_plugin_client_context_t *context = _context;
if (!context) {
return;
}
xoauth2_plugin_str_free(utils, &context->outbuf);
SASL_free(context);
}
static sasl_client_plug_t xoauth2_client_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, /* required_prompts */
NULL, /* glob_context */
&xoauth2_plugin_client_mech_new, /* mech_new */
&xoauth2_plugin_client_mech_step, /* mech_step */
&xoauth2_plugin_client_mech_dispose,/* mech_dispose */
NULL, /* mech_free */
NULL, /* idle */
NULL, /* spare */
NULL /* spare */
}
};
int xoauth2_client_plug_init(
sasl_utils_t *utils,
int maxversion,
int *out_version,
sasl_client_plug_t **pluglist,
int *plugcount)
{
if (maxversion < SASL_CLIENT_PLUG_VERSION) {
SASL_seterror((utils->conn, 0, "xoauth2: version mismatch"));
return SASL_BADVERS;
}
*out_version = SASL_CLIENT_PLUG_VERSION;
*pluglist = xoauth2_client_plugins;
*plugcount = sizeof(xoauth2_client_plugins) / sizeof(*xoauth2_client_plugins);
return SASL_OK;
}