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