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>
|
|
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);
|
|
35 static ngx_int_t ngx_http_auth_basic_init(ngx_cycle_t *cycle);
|
|
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"),
|
|
44 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
45 ngx_conf_set_str_slot,
|
|
46 NGX_HTTP_LOC_CONF_OFFSET,
|
|
47 offsetof(ngx_http_auth_basic_loc_conf_t, realm),
|
|
48 &ngx_http_auth_basic_p },
|
|
49
|
|
50 { ngx_string("auth_basic_user_file"),
|
|
51 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
52 ngx_conf_set_str_slot,
|
|
53 NGX_HTTP_LOC_CONF_OFFSET,
|
|
54 offsetof(ngx_http_auth_basic_loc_conf_t, user_file),
|
|
55 NULL },
|
|
56
|
|
57 ngx_null_command
|
|
58 };
|
|
59
|
|
60
|
|
61
|
|
62 ngx_http_module_t ngx_http_auth_basic_module_ctx = {
|
58
|
63 NULL, /* preconfiguration */
|
|
64 NULL, /* postconfiguration */
|
52
|
65
|
|
66 NULL, /* create main configuration */
|
|
67 NULL, /* init main configuration */
|
|
68
|
|
69 NULL, /* create server configuration */
|
|
70 NULL, /* merge server configuration */
|
|
71
|
|
72 ngx_http_auth_basic_create_loc_conf, /* create location configuration */
|
|
73 ngx_http_auth_basic_merge_loc_conf /* merge location configuration */
|
|
74 };
|
|
75
|
|
76
|
|
77 ngx_module_t ngx_http_auth_basic_module = {
|
58
|
78 NGX_MODULE_V1,
|
52
|
79 &ngx_http_auth_basic_module_ctx, /* module context */
|
|
80 ngx_http_auth_basic_commands, /* module directives */
|
|
81 NGX_HTTP_MODULE, /* module type */
|
|
82 ngx_http_auth_basic_init, /* init module */
|
|
83 NULL /* init process */
|
|
84 };
|
|
85
|
|
86
|
|
87 static ngx_int_t
|
|
88 ngx_http_auth_basic_handler(ngx_http_request_t *r)
|
|
89 {
|
|
90 off_t offset;
|
|
91 ssize_t n;
|
|
92 ngx_fd_t fd;
|
|
93 ngx_str_t auth, encoded, pwd;
|
|
94 ngx_uint_t i, login, len, left, passwd;
|
|
95 ngx_file_t file;
|
|
96 ngx_http_auth_basic_ctx_t *ctx;
|
|
97 ngx_http_auth_basic_loc_conf_t *alcf;
|
|
98 u_char buf[NGX_HTTP_AUTH_BUF_SIZE];
|
|
99 enum {
|
|
100 sw_login,
|
|
101 sw_passwd,
|
|
102 sw_skip
|
|
103 } state;
|
|
104
|
|
105 alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_basic_module);
|
|
106
|
|
107 if (alcf->realm.len == 0 || alcf->user_file.len == 0) {
|
|
108 return NGX_OK;
|
|
109 }
|
|
110
|
|
111 ctx = ngx_http_get_module_ctx(r, ngx_http_auth_basic_module);
|
|
112
|
|
113 if (ctx) {
|
|
114 return ngx_http_auth_basic_crypt_handler(r, ctx, &ctx->passwd,
|
|
115 &alcf->realm);
|
|
116 }
|
|
117
|
|
118 if (r->headers_in.authorization == NULL) {
|
|
119 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
120 }
|
|
121
|
|
122 encoded = r->headers_in.authorization->value;
|
|
123
|
|
124 if (encoded.len < sizeof("Basic ") - 1
|
|
125 || ngx_strncasecmp(encoded.data, "Basic ", sizeof("Basic ") - 1) != 0)
|
|
126 {
|
|
127 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
128 }
|
|
129
|
|
130 encoded.len -= sizeof("Basic ") - 1;
|
|
131 encoded.data += sizeof("Basic ") - 1;
|
|
132
|
|
133 while (encoded.len && encoded.data[0] == ' ') {
|
|
134 encoded.len--;
|
|
135 encoded.data++;
|
|
136 }
|
|
137
|
|
138 if (encoded.len == 0) {
|
|
139 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
140 }
|
|
141
|
|
142 auth.len = ngx_base64_decoded_length(encoded.len);
|
|
143 auth.data = ngx_palloc(r->pool, auth.len + 1);
|
|
144 if (auth.data == NULL) {
|
|
145 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
146 }
|
|
147
|
|
148 if (ngx_decode_base64(&auth, &encoded) != NGX_OK) {
|
|
149 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
150 }
|
|
151
|
|
152 auth.data[auth.len] = '\0';
|
|
153
|
|
154 for (len = 0; len < auth.len; len++) {
|
|
155 if (auth.data[len] == ':') {
|
|
156 break;
|
|
157 }
|
|
158 }
|
|
159
|
|
160 if (len == auth.len) {
|
|
161 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
162 }
|
|
163
|
|
164 r->headers_in.user.len = len;
|
|
165 r->headers_in.user.data = auth.data;
|
|
166 r->headers_in.passwd.len = auth.len - len - 1;
|
|
167 r->headers_in.passwd.data = &auth.data[len + 1];
|
|
168
|
|
169 fd = ngx_open_file(alcf->user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
|
170
|
|
171 if (fd == NGX_INVALID_FILE) {
|
|
172 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
|
|
173 ngx_open_file_n " \"%s\" failed", alcf->user_file.data);
|
|
174 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
175 }
|
|
176
|
|
177 ngx_memzero(&file, sizeof(ngx_file_t));
|
|
178
|
|
179 file.fd = fd;
|
|
180 file.name = alcf->user_file;
|
|
181 file.log = r->connection->log;
|
|
182
|
|
183 state = sw_login;
|
|
184 passwd = 0;
|
|
185 login = 0;
|
|
186 left = 0;
|
|
187 offset = 0;
|
|
188
|
|
189 for ( ;; ) {
|
|
190 n = ngx_read_file(&file, buf + left, NGX_HTTP_AUTH_BUF_SIZE - left,
|
|
191 offset);
|
|
192
|
|
193 if (n == NGX_ERROR) {
|
|
194 ngx_http_auth_basic_close(&file);
|
|
195 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
196 }
|
|
197
|
|
198 if (n == 0) {
|
|
199 break;
|
|
200 }
|
|
201
|
|
202 for (i = left; i < left + n; i++) {
|
|
203 switch (state) {
|
|
204
|
|
205 case sw_login:
|
|
206 if (login == 0 && buf[i] == '#') {
|
|
207 state = sw_skip;
|
|
208 break;
|
|
209 }
|
|
210
|
|
211 if (buf[i] != auth.data[login]) {
|
|
212 state = sw_skip;
|
|
213 break;
|
|
214 }
|
|
215
|
|
216 if (login == len) {
|
|
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;
|
|
252 ngx_memcpy(buf, &buf[passwd], left);
|
|
253 passwd = 0;
|
|
254
|
|
255 } else {
|
|
256 left = 0;
|
|
257 }
|
|
258
|
|
259 offset += n;
|
|
260 }
|
|
261
|
|
262 ngx_http_auth_basic_close(&file);
|
|
263
|
|
264 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
265 }
|
|
266
|
|
267
|
|
268 static ngx_int_t
|
|
269 ngx_http_auth_basic_crypt_handler(ngx_http_request_t *r,
|
|
270 ngx_http_auth_basic_ctx_t *ctx, ngx_str_t *passwd, ngx_str_t *realm)
|
|
271 {
|
|
272 ngx_int_t rc;
|
|
273 u_char *encrypted;
|
|
274
|
|
275 rc = ngx_crypt(r->pool, r->headers_in.passwd.data, passwd->data,
|
|
276 &encrypted);
|
|
277
|
|
278 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
279 "rc: %d user: \"%V\" salt: \"%s\"",
|
|
280 rc, &r->headers_in.user, passwd->data);
|
|
281
|
|
282 if (rc == NGX_OK) {
|
|
283 if (ngx_strcmp(encrypted, passwd->data) == 0) {
|
|
284 return NGX_OK;
|
|
285 }
|
|
286
|
|
287 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
288 "encrypted: \"%s\"", encrypted);
|
|
289
|
|
290 return ngx_http_auth_basic_set_realm(r, realm);
|
|
291 }
|
|
292
|
|
293 if (rc == NGX_ERROR) {
|
|
294 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
295 }
|
|
296
|
|
297 /* rc == NGX_AGAIN */
|
|
298
|
|
299 if (ctx == NULL) {
|
|
300 ctx = ngx_palloc(r->pool, sizeof(ngx_http_auth_basic_ctx_t));
|
|
301 if (ctx == NULL) {
|
|
302 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
303 }
|
|
304
|
|
305 ngx_http_set_ctx(r, ctx, ngx_http_auth_basic_module);
|
|
306
|
|
307 ctx->passwd.len = passwd->len;
|
|
308 passwd->len++;
|
|
309
|
|
310 ctx->passwd.data = ngx_pstrdup(r->pool, passwd);
|
|
311 if (ctx->passwd.data == NULL) {
|
|
312 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
313 }
|
|
314
|
|
315 }
|
|
316
|
|
317 /* TODO: add mutex event */
|
|
318
|
|
319 return rc;
|
|
320 }
|
|
321
|
|
322
|
|
323 static ngx_int_t
|
|
324 ngx_http_auth_basic_set_realm(ngx_http_request_t *r, ngx_str_t *realm)
|
|
325 {
|
|
326 r->headers_out.www_authenticate = ngx_list_push(&r->headers_out.headers);
|
|
327 if (r->headers_out.www_authenticate == NULL) {
|
|
328 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
329 }
|
|
330
|
58
|
331 r->headers_out.www_authenticate->hash = 1;
|
52
|
332 r->headers_out.www_authenticate->key.len = sizeof("WWW-Authenticate") - 1;
|
|
333 r->headers_out.www_authenticate->key.data = (u_char *) "WWW-Authenticate";
|
|
334 r->headers_out.www_authenticate->value = *realm;
|
|
335
|
|
336 return NGX_HTTP_UNAUTHORIZED;
|
|
337 }
|
|
338
|
|
339 static void
|
|
340 ngx_http_auth_basic_close(ngx_file_t *file)
|
|
341 {
|
|
342 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
|
|
343 ngx_log_error(NGX_LOG_ALERT, file->log, ngx_errno,
|
|
344 ngx_close_file_n " \"%s\" failed", file->name.data);
|
|
345 }
|
|
346 }
|
|
347
|
|
348
|
|
349 static void *
|
|
350 ngx_http_auth_basic_create_loc_conf(ngx_conf_t *cf)
|
|
351 {
|
|
352 ngx_http_auth_basic_loc_conf_t *conf;
|
|
353
|
|
354 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_basic_loc_conf_t));
|
|
355 if (conf == NULL) {
|
|
356 return NGX_CONF_ERROR;
|
|
357 }
|
|
358
|
|
359 return conf;
|
|
360 }
|
|
361
|
|
362
|
|
363 static char *
|
|
364 ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
365 {
|
|
366 ngx_http_auth_basic_loc_conf_t *prev = parent;
|
|
367 ngx_http_auth_basic_loc_conf_t *conf = child;
|
|
368
|
|
369 size_t len;
|
|
370 u_char *realm, *p;
|
|
371
|
|
372 if (conf->realm.data) {
|
|
373 if (conf->realm.len) {
|
|
374 len = sizeof("Basic realm=\"") - 1 + conf->realm.len + 1;
|
|
375
|
|
376 realm = ngx_palloc(cf->pool, len);
|
|
377 if (realm == NULL) {
|
|
378 return NGX_CONF_ERROR;
|
|
379 }
|
|
380
|
|
381 p = ngx_cpymem(realm, "Basic realm=\"",
|
|
382 sizeof("Basic realm=\"") - 1);
|
|
383 p = ngx_cpymem(p, conf->realm.data, conf->realm.len);
|
|
384 *p = '"';
|
|
385
|
|
386 conf->realm.len = len;
|
|
387 conf->realm.data = realm;
|
|
388 }
|
|
389
|
|
390 } else {
|
|
391 conf->realm = prev->realm;
|
|
392 }
|
|
393
|
|
394
|
|
395 if (conf->user_file.data) {
|
|
396 if (ngx_conf_full_name(cf->cycle, &conf->user_file) != NGX_OK) {
|
|
397 return NGX_CONF_ERROR;
|
|
398 }
|
|
399
|
|
400 } else {
|
|
401 conf->user_file = prev->user_file;
|
|
402 }
|
|
403
|
|
404 return NGX_CONF_OK;
|
|
405 }
|
|
406
|
|
407
|
|
408 static ngx_int_t
|
|
409 ngx_http_auth_basic_init(ngx_cycle_t *cycle)
|
|
410 {
|
|
411 ngx_http_handler_pt *h;
|
|
412 ngx_http_core_main_conf_t *cmcf;
|
|
413
|
|
414 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
|
415
|
|
416 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
|
|
417 if (h == NULL) {
|
|
418 return NGX_ERROR;
|
|
419 }
|
|
420
|
|
421 *h = ngx_http_auth_basic_handler;
|
|
422
|
|
423 return NGX_OK;
|
|
424 }
|
|
425
|
|
426
|
|
427 static char *
|
|
428 ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data)
|
|
429 {
|
|
430 ngx_str_t *realm = data;
|
|
431
|
|
432 if (ngx_strcmp(realm->data, "off") == 0) {
|
|
433 realm->len = 0;
|
|
434 realm->data = (u_char *) "";
|
|
435 }
|
|
436
|
|
437 return NGX_CONF_OK;
|
|
438 }
|