3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 #define NGX_HTTP_AUTH_BUF_SIZE 2048
17 } ngx_http_auth_basic_ctx_t;
23 ngx_array_t *user_file_lengths;
24 ngx_array_t *user_file_values;
25 } ngx_http_auth_basic_loc_conf_t;
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,
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);
37 static ngx_int_t ngx_http_auth_basic_init(ngx_conf_t *cf);
38 static char *ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data);
39 static char *ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd,
43 static ngx_conf_post_handler_pt ngx_http_auth_basic_p = ngx_http_auth_basic;
45 static ngx_command_t ngx_http_auth_basic_commands[] = {
47 { ngx_string("auth_basic"),
48 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
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 },
55 { ngx_string("auth_basic_user_file"),
56 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
58 ngx_http_auth_basic_user_file,
59 NGX_HTTP_LOC_CONF_OFFSET,
60 offsetof(ngx_http_auth_basic_loc_conf_t, user_file),
67 static ngx_http_module_t ngx_http_auth_basic_module_ctx = {
68 NULL, /* preconfiguration */
69 ngx_http_auth_basic_init, /* postconfiguration */
71 NULL, /* create main configuration */
72 NULL, /* init main configuration */
74 NULL, /* create server configuration */
75 NULL, /* merge server configuration */
77 ngx_http_auth_basic_create_loc_conf, /* create location configuration */
78 ngx_http_auth_basic_merge_loc_conf /* merge location configuration */
82 ngx_module_t ngx_http_auth_basic_module = {
84 &ngx_http_auth_basic_module_ctx, /* module context */
85 ngx_http_auth_basic_commands, /* module directives */
86 NGX_HTTP_MODULE, /* module type */
87 NULL, /* init master */
88 NULL, /* init module */
89 NULL, /* init process */
90 NULL, /* init thread */
91 NULL, /* exit thread */
92 NULL, /* exit process */
93 NULL, /* exit master */
99 ngx_http_auth_basic_handler(ngx_http_request_t *r)
106 ngx_str_t pwd, user_file;
107 ngx_uint_t i, level, login, left, passwd;
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];
118 alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_basic_module);
120 if (alcf->realm.len == 0 || alcf->user_file.len == 0) {
124 ctx = ngx_http_get_module_ctx(r, ngx_http_auth_basic_module);
127 return ngx_http_auth_basic_crypt_handler(r, ctx, &ctx->passwd,
131 rc = ngx_http_auth_basic_user(r);
133 if (rc == NGX_DECLINED) {
135 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
136 "no user/password was provided for basic authentication");
138 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
141 if (rc == NGX_ERROR) {
142 return NGX_HTTP_INTERNAL_SERVER_ERROR;
145 if (alcf->user_file_lengths) {
146 if (ngx_http_script_run(r, &user_file, alcf->user_file_lengths->elts, 1,
147 alcf->user_file_values->elts)
153 user_file.data[--user_file.len] = '\0';
156 user_file = alcf->user_file;
159 fd = ngx_open_file(user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
161 if (fd == NGX_INVALID_FILE) {
164 if (err == NGX_ENOENT) {
166 rc = NGX_HTTP_FORBIDDEN;
169 level = NGX_LOG_CRIT;
170 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
173 ngx_log_error(level, r->connection->log, err,
174 ngx_open_file_n " \"%s\" failed", user_file.data);
179 ngx_memzero(&file, sizeof(ngx_file_t));
182 file.name = user_file;
183 file.log = r->connection->log;
194 n = ngx_read_file(&file, buf + left, NGX_HTTP_AUTH_BUF_SIZE - left,
197 if (n == NGX_ERROR) {
198 ngx_http_auth_basic_close(&file);
199 return NGX_HTTP_INTERNAL_SERVER_ERROR;
206 for (i = left; i < left + n; i++) {
212 if (buf[i] == '#' || buf[i] == CR) {
222 if (buf[i] != r->headers_in.user.data[login]) {
227 if (login == r->headers_in.user.len) {
237 if (buf[i] == LF || buf[i] == CR || buf[i] == ':') {
240 ngx_http_auth_basic_close(&file);
242 pwd.len = i - passwd;
243 pwd.data = &buf[passwd];
245 return ngx_http_auth_basic_crypt_handler(r, NULL, &pwd,
261 if (state == sw_passwd) {
262 left = left + n - passwd;
263 ngx_memcpy(buf, &buf[passwd], left);
273 ngx_http_auth_basic_close(&file);
275 if (state == sw_passwd) {
276 pwd.len = i - passwd;
277 pwd.data = ngx_pnalloc(r->pool, pwd.len + 1);
278 if (pwd.data == NULL) {
279 return NGX_HTTP_INTERNAL_SERVER_ERROR;
282 ngx_cpystrn(pwd.data, &buf[passwd], pwd.len + 1);
284 return ngx_http_auth_basic_crypt_handler(r, NULL, &pwd, &alcf->realm);
287 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
288 "user \"%V\" was not found in \"%V\"",
289 &r->headers_in.user, &user_file);
291 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
296 ngx_http_auth_basic_crypt_handler(ngx_http_request_t *r,
297 ngx_http_auth_basic_ctx_t *ctx, ngx_str_t *passwd, ngx_str_t *realm)
302 rc = ngx_crypt(r->pool, r->headers_in.passwd.data, passwd->data,
305 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
306 "rc: %d user: \"%V\" salt: \"%s\"",
307 rc, &r->headers_in.user, passwd->data);
310 if (ngx_strcmp(encrypted, passwd->data) == 0) {
314 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
315 "encrypted: \"%s\"", encrypted);
317 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
318 "user \"%V\": password mismatch",
319 &r->headers_in.user);
321 return ngx_http_auth_basic_set_realm(r, realm);
324 if (rc == NGX_ERROR) {
325 return NGX_HTTP_INTERNAL_SERVER_ERROR;
328 /* rc == NGX_AGAIN */
331 ctx = ngx_palloc(r->pool, sizeof(ngx_http_auth_basic_ctx_t));
333 return NGX_HTTP_INTERNAL_SERVER_ERROR;
336 ngx_http_set_ctx(r, ctx, ngx_http_auth_basic_module);
338 ctx->passwd.len = passwd->len;
341 ctx->passwd.data = ngx_pstrdup(r->pool, passwd);
342 if (ctx->passwd.data == NULL) {
343 return NGX_HTTP_INTERNAL_SERVER_ERROR;
348 /* TODO: add mutex event */
355 ngx_http_auth_basic_set_realm(ngx_http_request_t *r, ngx_str_t *realm)
357 r->headers_out.www_authenticate = ngx_list_push(&r->headers_out.headers);
358 if (r->headers_out.www_authenticate == NULL) {
359 return NGX_HTTP_INTERNAL_SERVER_ERROR;
362 r->headers_out.www_authenticate->hash = 1;
363 r->headers_out.www_authenticate->key.len = sizeof("WWW-Authenticate") - 1;
364 r->headers_out.www_authenticate->key.data = (u_char *) "WWW-Authenticate";
365 r->headers_out.www_authenticate->value = *realm;
367 return NGX_HTTP_UNAUTHORIZED;
371 ngx_http_auth_basic_close(ngx_file_t *file)
373 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
374 ngx_log_error(NGX_LOG_ALERT, file->log, ngx_errno,
375 ngx_close_file_n " \"%s\" failed", file->name.data);
381 ngx_http_auth_basic_create_loc_conf(ngx_conf_t *cf)
383 ngx_http_auth_basic_loc_conf_t *conf;
385 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_basic_loc_conf_t));
387 return NGX_CONF_ERROR;
395 ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
397 ngx_http_auth_basic_loc_conf_t *prev = parent;
398 ngx_http_auth_basic_loc_conf_t *conf = child;
400 if (conf->realm.data == NULL) {
401 conf->realm = prev->realm;
404 if (conf->user_file.data == NULL) {
405 conf->user_file = prev->user_file;
406 conf->user_file_lengths = prev->user_file_lengths;
407 conf->user_file_values = prev->user_file_values;
415 ngx_http_auth_basic_init(ngx_conf_t *cf)
417 ngx_http_handler_pt *h;
418 ngx_http_core_main_conf_t *cmcf;
420 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
422 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
427 *h = ngx_http_auth_basic_handler;
434 ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data)
436 ngx_str_t *realm = data;
441 if (ngx_strcmp(realm->data, "off") == 0) {
443 realm->data = (u_char *) "";
448 len = sizeof("Basic realm=\"") - 1 + realm->len + 1;
450 basic = ngx_pnalloc(cf->pool, len);
452 return NGX_CONF_ERROR;
455 p = ngx_cpymem(basic, "Basic realm=\"", sizeof("Basic realm=\"") - 1);
456 p = ngx_cpymem(p, realm->data, realm->len);
467 ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
469 ngx_http_auth_basic_loc_conf_t *alcf = conf;
473 ngx_http_script_compile_t sc;
475 if (alcf->user_file.data) {
476 return "is duplicate";
479 value = cf->args->elts;
481 alcf->user_file = value[1];
483 if (alcf->user_file.len == 0) {
484 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
485 "invalid parameter \"%V\"", &alcf->user_file);
486 return NGX_CONF_ERROR;
489 if (alcf->user_file.data[0] != '$') {
490 if (ngx_conf_full_name(cf->cycle, &alcf->user_file, 1) != NGX_OK) {
491 return NGX_CONF_ERROR;
495 n = ngx_http_script_variables_count(&alcf->user_file);
501 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
504 sc.source = &alcf->user_file;
505 sc.lengths = &alcf->user_file_lengths;
506 sc.values = &alcf->user_file_values;
508 sc.complete_lengths = 1;
509 sc.complete_values = 1;
511 if (ngx_http_script_compile(&sc) != NGX_OK) {
512 return NGX_CONF_ERROR;