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