52
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
626
|
10 #include <ngx_crypt.h>
|
52
|
11
|
|
12
|
|
13 #define NGX_HTTP_AUTH_BUF_SIZE 2048
|
|
14
|
|
15
|
|
16 typedef struct {
|
464
|
17 ngx_str_t passwd;
|
52
|
18 } ngx_http_auth_basic_ctx_t;
|
|
19
|
|
20
|
|
21 typedef struct {
|
464
|
22 ngx_str_t realm;
|
|
23 ngx_http_complex_value_t user_file;
|
52
|
24 } ngx_http_auth_basic_loc_conf_t;
|
|
25
|
|
26
|
|
27 static ngx_int_t ngx_http_auth_basic_handler(ngx_http_request_t *r);
|
|
28 static ngx_int_t ngx_http_auth_basic_crypt_handler(ngx_http_request_t *r,
|
|
29 ngx_http_auth_basic_ctx_t *ctx, ngx_str_t *passwd, ngx_str_t *realm);
|
|
30 static ngx_int_t ngx_http_auth_basic_set_realm(ngx_http_request_t *r,
|
|
31 ngx_str_t *realm);
|
|
32 static void ngx_http_auth_basic_close(ngx_file_t *file);
|
|
33 static void *ngx_http_auth_basic_create_loc_conf(ngx_conf_t *cf);
|
|
34 static char *ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf,
|
|
35 void *parent, void *child);
|
230
|
36 static ngx_int_t ngx_http_auth_basic_init(ngx_conf_t *cf);
|
52
|
37 static char *ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data);
|
460
|
38 static char *ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
39 void *conf);
|
52
|
40
|
|
41
|
|
42 static ngx_conf_post_handler_pt ngx_http_auth_basic_p = ngx_http_auth_basic;
|
|
43
|
|
44 static ngx_command_t ngx_http_auth_basic_commands[] = {
|
|
45
|
|
46 { ngx_string("auth_basic"),
|
180
|
47 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
|
|
48 |NGX_CONF_TAKE1,
|
52
|
49 ngx_conf_set_str_slot,
|
|
50 NGX_HTTP_LOC_CONF_OFFSET,
|
|
51 offsetof(ngx_http_auth_basic_loc_conf_t, realm),
|
|
52 &ngx_http_auth_basic_p },
|
|
53
|
|
54 { ngx_string("auth_basic_user_file"),
|
180
|
55 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
|
|
56 |NGX_CONF_TAKE1,
|
460
|
57 ngx_http_auth_basic_user_file,
|
52
|
58 NGX_HTTP_LOC_CONF_OFFSET,
|
|
59 offsetof(ngx_http_auth_basic_loc_conf_t, user_file),
|
|
60 NULL },
|
|
61
|
|
62 ngx_null_command
|
|
63 };
|
|
64
|
|
65
|
216
|
66 static ngx_http_module_t ngx_http_auth_basic_module_ctx = {
|
58
|
67 NULL, /* preconfiguration */
|
230
|
68 ngx_http_auth_basic_init, /* postconfiguration */
|
52
|
69
|
|
70 NULL, /* create main configuration */
|
|
71 NULL, /* init main configuration */
|
|
72
|
|
73 NULL, /* create server configuration */
|
|
74 NULL, /* merge server configuration */
|
|
75
|
|
76 ngx_http_auth_basic_create_loc_conf, /* create location configuration */
|
|
77 ngx_http_auth_basic_merge_loc_conf /* merge location configuration */
|
|
78 };
|
|
79
|
|
80
|
|
81 ngx_module_t ngx_http_auth_basic_module = {
|
58
|
82 NGX_MODULE_V1,
|
52
|
83 &ngx_http_auth_basic_module_ctx, /* module context */
|
|
84 ngx_http_auth_basic_commands, /* module directives */
|
|
85 NGX_HTTP_MODULE, /* module type */
|
90
|
86 NULL, /* init master */
|
230
|
87 NULL, /* init module */
|
90
|
88 NULL, /* init process */
|
|
89 NULL, /* init thread */
|
|
90 NULL, /* exit thread */
|
|
91 NULL, /* exit process */
|
|
92 NULL, /* exit master */
|
|
93 NGX_MODULE_V1_PADDING
|
52
|
94 };
|
|
95
|
|
96
|
|
97 static ngx_int_t
|
|
98 ngx_http_auth_basic_handler(ngx_http_request_t *r)
|
|
99 {
|
|
100 off_t offset;
|
|
101 ssize_t n;
|
|
102 ngx_fd_t fd;
|
88
|
103 ngx_int_t rc;
|
460
|
104 ngx_err_t err;
|
|
105 ngx_str_t pwd, user_file;
|
|
106 ngx_uint_t i, level, login, left, passwd;
|
52
|
107 ngx_file_t file;
|
|
108 ngx_http_auth_basic_ctx_t *ctx;
|
|
109 ngx_http_auth_basic_loc_conf_t *alcf;
|
|
110 u_char buf[NGX_HTTP_AUTH_BUF_SIZE];
|
|
111 enum {
|
|
112 sw_login,
|
|
113 sw_passwd,
|
|
114 sw_skip
|
|
115 } state;
|
|
116
|
|
117 alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_basic_module);
|
|
118
|
464
|
119 if (alcf->realm.len == 0 || alcf->user_file.value.len == 0) {
|
358
|
120 return NGX_DECLINED;
|
52
|
121 }
|
|
122
|
|
123 ctx = ngx_http_get_module_ctx(r, ngx_http_auth_basic_module);
|
|
124
|
|
125 if (ctx) {
|
|
126 return ngx_http_auth_basic_crypt_handler(r, ctx, &ctx->passwd,
|
|
127 &alcf->realm);
|
|
128 }
|
|
129
|
88
|
130 rc = ngx_http_auth_basic_user(r);
|
52
|
131
|
88
|
132 if (rc == NGX_DECLINED) {
|
452
|
133
|
|
134 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
135 "no user/password was provided for basic authentication");
|
|
136
|
52
|
137 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
138 }
|
|
139
|
88
|
140 if (rc == NGX_ERROR) {
|
52
|
141 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
142 }
|
|
143
|
464
|
144 if (ngx_http_complex_value(r, &alcf->user_file, &user_file) != NGX_OK) {
|
|
145 return NGX_ERROR;
|
460
|
146 }
|
|
147
|
|
148 fd = ngx_open_file(user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
|
52
|
149
|
|
150 if (fd == NGX_INVALID_FILE) {
|
460
|
151 err = ngx_errno;
|
|
152
|
|
153 if (err == NGX_ENOENT) {
|
|
154 level = NGX_LOG_ERR;
|
|
155 rc = NGX_HTTP_FORBIDDEN;
|
|
156
|
|
157 } else {
|
|
158 level = NGX_LOG_CRIT;
|
|
159 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
160 }
|
|
161
|
|
162 ngx_log_error(level, r->connection->log, err,
|
|
163 ngx_open_file_n " \"%s\" failed", user_file.data);
|
|
164
|
|
165 return rc;
|
52
|
166 }
|
|
167
|
|
168 ngx_memzero(&file, sizeof(ngx_file_t));
|
|
169
|
|
170 file.fd = fd;
|
460
|
171 file.name = user_file;
|
52
|
172 file.log = r->connection->log;
|
|
173
|
|
174 state = sw_login;
|
|
175 passwd = 0;
|
|
176 login = 0;
|
|
177 left = 0;
|
|
178 offset = 0;
|
|
179
|
|
180 for ( ;; ) {
|
262
|
181 i = left;
|
|
182
|
52
|
183 n = ngx_read_file(&file, buf + left, NGX_HTTP_AUTH_BUF_SIZE - left,
|
|
184 offset);
|
|
185
|
|
186 if (n == NGX_ERROR) {
|
|
187 ngx_http_auth_basic_close(&file);
|
|
188 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
189 }
|
|
190
|
|
191 if (n == 0) {
|
|
192 break;
|
|
193 }
|
|
194
|
|
195 for (i = left; i < left + n; i++) {
|
|
196 switch (state) {
|
|
197
|
|
198 case sw_login:
|
452
|
199 if (login == 0) {
|
|
200
|
|
201 if (buf[i] == '#' || buf[i] == CR) {
|
|
202 state = sw_skip;
|
|
203 break;
|
|
204 }
|
|
205
|
|
206 if (buf[i] == LF) {
|
|
207 break;
|
|
208 }
|
52
|
209 }
|
|
210
|
88
|
211 if (buf[i] != r->headers_in.user.data[login]) {
|
52
|
212 state = sw_skip;
|
|
213 break;
|
|
214 }
|
|
215
|
88
|
216 if (login == r->headers_in.user.len) {
|
52
|
217 state = sw_passwd;
|
|
218 passwd = i + 1;
|
|
219 }
|
|
220
|
|
221 login++;
|
|
222
|
|
223 break;
|
|
224
|
|
225 case sw_passwd:
|
|
226 if (buf[i] == LF || buf[i] == CR || buf[i] == ':') {
|
|
227 buf[i] = '\0';
|
|
228
|
|
229 ngx_http_auth_basic_close(&file);
|
|
230
|
|
231 pwd.len = i - passwd;
|
|
232 pwd.data = &buf[passwd];
|
|
233
|
|
234 return ngx_http_auth_basic_crypt_handler(r, NULL, &pwd,
|
|
235 &alcf->realm);
|
|
236 }
|
|
237
|
|
238 break;
|
|
239
|
|
240 case sw_skip:
|
|
241 if (buf[i] == LF) {
|
|
242 state = sw_login;
|
|
243 login = 0;
|
|
244 }
|
|
245
|
|
246 break;
|
|
247 }
|
|
248 }
|
|
249
|
|
250 if (state == sw_passwd) {
|
|
251 left = left + n - passwd;
|
620
|
252 ngx_memmove(buf, &buf[passwd], left);
|
52
|
253 passwd = 0;
|
|
254
|
|
255 } else {
|
|
256 left = 0;
|
|
257 }
|
|
258
|
|
259 offset += n;
|
|
260 }
|
|
261
|
|
262 ngx_http_auth_basic_close(&file);
|
|
263
|
262
|
264 if (state == sw_passwd) {
|
|
265 pwd.len = i - passwd;
|
382
|
266 pwd.data = ngx_pnalloc(r->pool, pwd.len + 1);
|
262
|
267 if (pwd.data == NULL) {
|
|
268 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
269 }
|
|
270
|
|
271 ngx_cpystrn(pwd.data, &buf[passwd], pwd.len + 1);
|
|
272
|
|
273 return ngx_http_auth_basic_crypt_handler(r, NULL, &pwd, &alcf->realm);
|
|
274 }
|
|
275
|
452
|
276 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
277 "user \"%V\" was not found in \"%V\"",
|
460
|
278 &r->headers_in.user, &user_file);
|
452
|
279
|
52
|
280 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
281 }
|
|
282
|
|
283
|
|
284 static ngx_int_t
|
|
285 ngx_http_auth_basic_crypt_handler(ngx_http_request_t *r,
|
|
286 ngx_http_auth_basic_ctx_t *ctx, ngx_str_t *passwd, ngx_str_t *realm)
|
|
287 {
|
|
288 ngx_int_t rc;
|
|
289 u_char *encrypted;
|
|
290
|
|
291 rc = ngx_crypt(r->pool, r->headers_in.passwd.data, passwd->data,
|
|
292 &encrypted);
|
|
293
|
|
294 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
452
|
295 "rc: %d user: \"%V\" salt: \"%s\"",
|
|
296 rc, &r->headers_in.user, passwd->data);
|
52
|
297
|
|
298 if (rc == NGX_OK) {
|
|
299 if (ngx_strcmp(encrypted, passwd->data) == 0) {
|
|
300 return NGX_OK;
|
|
301 }
|
|
302
|
|
303 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
304 "encrypted: \"%s\"", encrypted);
|
|
305
|
452
|
306 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
307 "user \"%V\": password mismatch",
|
|
308 &r->headers_in.user);
|
|
309
|
52
|
310 return ngx_http_auth_basic_set_realm(r, realm);
|
|
311 }
|
|
312
|
|
313 if (rc == NGX_ERROR) {
|
|
314 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
315 }
|
|
316
|
|
317 /* rc == NGX_AGAIN */
|
|
318
|
|
319 if (ctx == NULL) {
|
|
320 ctx = ngx_palloc(r->pool, sizeof(ngx_http_auth_basic_ctx_t));
|
|
321 if (ctx == NULL) {
|
|
322 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
323 }
|
|
324
|
|
325 ngx_http_set_ctx(r, ctx, ngx_http_auth_basic_module);
|
|
326
|
|
327 ctx->passwd.len = passwd->len;
|
|
328 passwd->len++;
|
|
329
|
|
330 ctx->passwd.data = ngx_pstrdup(r->pool, passwd);
|
|
331 if (ctx->passwd.data == NULL) {
|
|
332 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
333 }
|
|
334
|
|
335 }
|
|
336
|
|
337 /* TODO: add mutex event */
|
|
338
|
|
339 return rc;
|
|
340 }
|
|
341
|
|
342
|
|
343 static ngx_int_t
|
|
344 ngx_http_auth_basic_set_realm(ngx_http_request_t *r, ngx_str_t *realm)
|
|
345 {
|
|
346 r->headers_out.www_authenticate = ngx_list_push(&r->headers_out.headers);
|
|
347 if (r->headers_out.www_authenticate == NULL) {
|
|
348 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
349 }
|
|
350
|
58
|
351 r->headers_out.www_authenticate->hash = 1;
|
570
|
352 ngx_str_set(&r->headers_out.www_authenticate->key, "WWW-Authenticate");
|
52
|
353 r->headers_out.www_authenticate->value = *realm;
|
|
354
|
|
355 return NGX_HTTP_UNAUTHORIZED;
|
|
356 }
|
|
357
|
|
358 static void
|
|
359 ngx_http_auth_basic_close(ngx_file_t *file)
|
|
360 {
|
|
361 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
|
|
362 ngx_log_error(NGX_LOG_ALERT, file->log, ngx_errno,
|
|
363 ngx_close_file_n " \"%s\" failed", file->name.data);
|
|
364 }
|
|
365 }
|
|
366
|
|
367
|
|
368 static void *
|
|
369 ngx_http_auth_basic_create_loc_conf(ngx_conf_t *cf)
|
|
370 {
|
|
371 ngx_http_auth_basic_loc_conf_t *conf;
|
|
372
|
|
373 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_basic_loc_conf_t));
|
|
374 if (conf == NULL) {
|
496
|
375 return NULL;
|
52
|
376 }
|
|
377
|
|
378 return conf;
|
|
379 }
|
|
380
|
|
381
|
|
382 static char *
|
|
383 ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
384 {
|
|
385 ngx_http_auth_basic_loc_conf_t *prev = parent;
|
|
386 ngx_http_auth_basic_loc_conf_t *conf = child;
|
|
387
|
130
|
388 if (conf->realm.data == NULL) {
|
52
|
389 conf->realm = prev->realm;
|
|
390 }
|
|
391
|
464
|
392 if (conf->user_file.value.len == 0) {
|
52
|
393 conf->user_file = prev->user_file;
|
|
394 }
|
|
395
|
|
396 return NGX_CONF_OK;
|
|
397 }
|
|
398
|
|
399
|
|
400 static ngx_int_t
|
230
|
401 ngx_http_auth_basic_init(ngx_conf_t *cf)
|
52
|
402 {
|
|
403 ngx_http_handler_pt *h;
|
|
404 ngx_http_core_main_conf_t *cmcf;
|
|
405
|
230
|
406 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
52
|
407
|
|
408 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
|
|
409 if (h == NULL) {
|
|
410 return NGX_ERROR;
|
|
411 }
|
|
412
|
|
413 *h = ngx_http_auth_basic_handler;
|
|
414
|
|
415 return NGX_OK;
|
|
416 }
|
|
417
|
|
418
|
|
419 static char *
|
|
420 ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data)
|
|
421 {
|
|
422 ngx_str_t *realm = data;
|
|
423
|
130
|
424 size_t len;
|
|
425 u_char *basic, *p;
|
|
426
|
52
|
427 if (ngx_strcmp(realm->data, "off") == 0) {
|
570
|
428 ngx_str_set(realm, "");
|
132
|
429 return NGX_CONF_OK;
|
52
|
430 }
|
|
431
|
130
|
432 len = sizeof("Basic realm=\"") - 1 + realm->len + 1;
|
|
433
|
382
|
434 basic = ngx_pnalloc(cf->pool, len);
|
130
|
435 if (basic == NULL) {
|
|
436 return NGX_CONF_ERROR;
|
|
437 }
|
|
438
|
|
439 p = ngx_cpymem(basic, "Basic realm=\"", sizeof("Basic realm=\"") - 1);
|
|
440 p = ngx_cpymem(p, realm->data, realm->len);
|
|
441 *p = '"';
|
|
442
|
|
443 realm->len = len;
|
|
444 realm->data = basic;
|
|
445
|
52
|
446 return NGX_CONF_OK;
|
|
447 }
|
460
|
448
|
|
449
|
|
450 static char *
|
|
451 ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
452 {
|
|
453 ngx_http_auth_basic_loc_conf_t *alcf = conf;
|
|
454
|
464
|
455 ngx_str_t *value;
|
|
456 ngx_http_compile_complex_value_t ccv;
|
460
|
457
|
464
|
458 if (alcf->user_file.value.len) {
|
460
|
459 return "is duplicate";
|
|
460 }
|
|
461
|
|
462 value = cf->args->elts;
|
|
463
|
464
|
464 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
460
|
465
|
464
|
466 ccv.cf = cf;
|
|
467 ccv.value = &value[1];
|
|
468 ccv.complex_value = &alcf->user_file;
|
|
469 ccv.zero = 1;
|
|
470 ccv.conf_prefix = 1;
|
460
|
471
|
464
|
472 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
460
|
473 return NGX_CONF_ERROR;
|
|
474 }
|
|
475
|
|
476 return NGX_CONF_OK;
|
|
477 }
|