-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngx_http_auth_saslauthd_module.c
499 lines (384 loc) · 14 KB
/
ngx_http_auth_saslauthd_module.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
/*
* Copyright Alexandre Jousset & others (see README.md)
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_http_complex_value_t *realm;
ngx_http_complex_value_t socket;
} ngx_http_auth_saslauthd_loc_conf_t;
static ngx_int_t ngx_http_auth_saslauthd_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_auth_saslauthd_set_realm(ngx_http_request_t *r, ngx_str_t *realm);
static void *ngx_http_auth_saslauthd_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_auth_saslauthd_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_auth_saslauthd_init(ngx_conf_t *cf);
static char *ngx_http_auth_saslauthd_socket(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_command_t ngx_http_auth_saslauthd_commands[] = {
{ ngx_string("auth_saslauthd"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
ngx_http_set_complex_value_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_auth_saslauthd_loc_conf_t, realm),
NULL },
{ ngx_string("auth_saslauthd_socket"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
ngx_http_auth_saslauthd_socket,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_auth_saslauthd_loc_conf_t, socket),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_auth_saslauthd_module_ctx = {
NULL, /* preconfiguration */
ngx_http_auth_saslauthd_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_auth_saslauthd_create_loc_conf, /* create location configuration */
ngx_http_auth_saslauthd_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_auth_saslauthd_module = {
NGX_MODULE_V1,
&ngx_http_auth_saslauthd_module_ctx, /* module context */
ngx_http_auth_saslauthd_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
/**************************************************************
* I/O wrapper to attempt to write out the specified vector.
* data, without any guarantees. If the function returns
* -1, the vector wasn't completely written.
**************************************************************/
static int
retry_writev(int fd, struct iovec *iov, int iovcnt) {
int n; /* return value from writev() */
int i; /* loop counter */
int written; /* bytes written so far */
static int iov_max; /* max number of iovec entries */
iov_max = IOV_MAX;
written = 0;
for (;;) {
while (iovcnt && iov[0].iov_len == 0) {
iov++;
iovcnt--;
}
if (!iovcnt) {
return written;
}
n = writev(fd, iov, iovcnt > iov_max ? iov_max : iovcnt);
if (n == -1) {
if (errno == EINVAL && iov_max > 10) {
iov_max /= 2;
continue;
}
if (errno == EINTR) {
continue;
}
return -1;
} else {
written += n;
}
for (i = 0; i < iovcnt; i++) {
if ((int) iov[i].iov_len > n) {
iov[i].iov_base = (char *)iov[i].iov_base + n;
iov[i].iov_len -= n;
break;
}
n -= iov[i].iov_len;
iov[i].iov_len = 0;
}
if (i == iovcnt) {
return written;
}
}
}
/*
* Keep calling the read() system call with 'fd', 'buf', and 'nbyte'
* until all the data is read in or an error occurs.
*/
static int
retry_read(int fd, void *inbuf, unsigned nbyte)
{
int n;
int nread = 0;
char *buf = (char *)inbuf;
if (nbyte == 0) return 0;
for (;;) {
n = ngx_read_fd(fd, buf, nbyte);
if (n == -1 || n == 0) {
if (errno == EINTR || errno == EAGAIN) continue;
return -1;
}
nread += n;
if (n >= (int) nbyte) return nread;
buf += n;
nbyte -= n;
}
}
/* saslauthd-authenticated login */
static ngx_int_t
saslauthd_verify_password(
ngx_http_request_t *req,
const ngx_str_t *sck,
const ngx_str_t *userid,
const ngx_str_t *passwd,
const ngx_str_t *service,
const ngx_str_t *user_realm,
const ngx_str_t *client_addr)
{
u_char response[1024];
u_char query[8192];
u_char *query_end = query;
ngx_socket_t s;
int r;
unsigned short count;
struct sockaddr_un srvaddr;
ngx_str_t null_str = ngx_null_string;
if(!userid || !passwd) return NGX_DECLINED;
if(!service) service = &null_str;
if(!user_realm) user_realm = &null_str;
if(!client_addr) client_addr = &null_str;
if(!sck)
return NGX_ERROR;
if (sck->len + 1 > sizeof(srvaddr.sun_path))
return NGX_ERROR;
if (userid->len + passwd->len + service->len + user_realm->len + client_addr->len + 5 * sizeof(unsigned short) > sizeof(query))
return NGX_ERROR;
/*
* build request of the form:
*
* count authid count password count service count realm
*/
{
unsigned short u_len, p_len, s_len, r_len, c_len;
u_len = htons(userid->len);
p_len = htons(passwd->len);
s_len = htons(service->len);
r_len = htons(user_realm->len);
c_len = htons(client_addr->len);
ngx_memcpy(query_end, &u_len, sizeof(unsigned short));
query_end += sizeof(unsigned short);
ngx_memcpy(query_end, userid->data, userid->len);
query_end += userid->len;
ngx_memcpy(query_end, &p_len, sizeof(unsigned short));
query_end += sizeof(unsigned short);
ngx_memcpy(query_end, passwd->data, passwd->len);
query_end += passwd->len;
ngx_memcpy(query_end, &s_len, sizeof(unsigned short));
query_end += sizeof(unsigned short);
ngx_memcpy(query_end, service->data, service->len);
query_end += service->len;
ngx_memcpy(query_end, &r_len, sizeof(unsigned short));
query_end += sizeof(unsigned short);
ngx_memcpy(query_end, user_realm->data, user_realm->len);
query_end += user_realm->len;
ngx_memcpy(query_end, &c_len, sizeof(unsigned short));
query_end += sizeof(unsigned short);
ngx_memcpy(query_end, client_addr->data, client_addr->len);
query_end += client_addr->len;
}
s = ngx_socket(AF_UNIX, SOCK_STREAM, 0);
if (s == (ngx_socket_t) -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
ngx_socket_n " failed");
return NGX_ERROR;
}
memset((char *)&srvaddr, 0, sizeof(srvaddr));
srvaddr.sun_family = AF_UNIX;
ngx_memcpy(srvaddr.sun_path, sck->data, sck->len + 1);
r = connect(s, (struct sockaddr *) &srvaddr, sizeof(srvaddr));
if (r == -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
"connect() failed");
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
return NGX_ERROR;
}
{
struct iovec iov[8];
iov[0].iov_len = query_end - query;
iov[0].iov_base = query;
if (retry_writev(s, iov, 1) == -1) {
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
return NGX_ERROR;
}
}
/*
* read response of the form:
*
* count result
*/
if (retry_read(s, &count, sizeof(count)) < (int) sizeof(count)) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
"size read failed\n");
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
return NGX_ERROR;
}
count = ntohs(count);
if (count < 2) { /* MUST have at least "OK" or "NO" */
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
"bad response from saslauthd\n");
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
return NGX_ERROR;
}
count = (int)sizeof(response) < count ? sizeof(response) : count;
if (retry_read(s, response, count) < count) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
"read failed\n");
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
return NGX_ERROR;
}
response[count] = '\0';
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, req->connection->log, ngx_socket_errno,
ngx_close_socket_n " failed");
}
if (!ngx_strncmp(response, "OK", 2))
return NGX_OK;
return NGX_ERROR;
}
static ngx_int_t
ngx_http_auth_saslauthd_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_str_t realm, sck;
ngx_http_auth_saslauthd_loc_conf_t *alcf;
alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_saslauthd_module);
if (alcf->realm == NULL || alcf->socket.value.data == NULL) {
return NGX_DECLINED;
}
if (ngx_http_complex_value(r, alcf->realm, &realm) != NGX_OK) {
return NGX_ERROR;
}
if (realm.len == 3 && ngx_strncmp(realm.data, "off", 3) == 0) {
return NGX_DECLINED;
}
rc = ngx_http_auth_basic_user(r);
if (rc == NGX_DECLINED) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"no user/password was provided for basic authentication");
return ngx_http_auth_saslauthd_set_realm(r, &realm);
}
if (rc == NGX_ERROR) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_http_complex_value(r, &alcf->socket, &sck) != NGX_OK) {
return NGX_ERROR;
}
rc = saslauthd_verify_password(
r,
&sck,
&r->headers_in.user,
&r->headers_in.passwd,
NULL,
NULL,
NULL);
if (rc != NGX_OK)
return ngx_http_auth_saslauthd_set_realm(r, &realm);
return NGX_OK;
}
static ngx_int_t
ngx_http_auth_saslauthd_set_realm(ngx_http_request_t *r, ngx_str_t *realm)
{
size_t len;
u_char *basic, *p;
r->headers_out.www_authenticate = ngx_list_push(&r->headers_out.headers);
if (r->headers_out.www_authenticate == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
len = sizeof("Basic realm=\"\"") - 1 + realm->len;
basic = ngx_pnalloc(r->pool, len);
if (basic == NULL) {
r->headers_out.www_authenticate->hash = 0;
r->headers_out.www_authenticate = NULL;
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
p = ngx_cpymem(basic, "Basic realm=\"", sizeof("Basic realm=\"") - 1);
p = ngx_cpymem(p, realm->data, realm->len);
*p = '"';
r->headers_out.www_authenticate->hash = 1;
ngx_str_set(&r->headers_out.www_authenticate->key, "WWW-Authenticate");
r->headers_out.www_authenticate->value.data = basic;
r->headers_out.www_authenticate->value.len = len;
return NGX_HTTP_UNAUTHORIZED;
}
static void *
ngx_http_auth_saslauthd_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_auth_saslauthd_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_saslauthd_loc_conf_t));
if (conf == NULL) {
return NULL;
}
return conf;
}
static char *
ngx_http_auth_saslauthd_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_auth_saslauthd_loc_conf_t *prev = parent;
ngx_http_auth_saslauthd_loc_conf_t *conf = child;
if (conf->realm == NULL) {
conf->realm = prev->realm;
}
if (conf->socket.value.data == NULL) {
conf->socket = prev->socket;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_auth_saslauthd_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_auth_saslauthd_handler;
return NGX_OK;
}
static char *
ngx_http_auth_saslauthd_socket(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_auth_saslauthd_loc_conf_t *alcf = conf;
ngx_str_t *value;
ngx_http_compile_complex_value_t ccv;
if (alcf->socket.value.data) {
return "is duplicate";
}
value = cf->args->elts;
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = &alcf->socket;
ccv.zero = 1;
ccv.conf_prefix = 1;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}