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_http_auth_basic_loc_conf_t;
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,
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_conf_t *cf);
36 static char *ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data);
39 static ngx_conf_post_handler_pt ngx_http_auth_basic_p = ngx_http_auth_basic;
41 static ngx_command_t ngx_http_auth_basic_commands[] = {
43 { ngx_string("auth_basic"),
44 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
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 },
51 { ngx_string("auth_basic_user_file"),
52 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
54 ngx_conf_set_str_slot,
55 NGX_HTTP_LOC_CONF_OFFSET,
56 offsetof(ngx_http_auth_basic_loc_conf_t, user_file),
63 static ngx_http_module_t ngx_http_auth_basic_module_ctx = {
64 NULL, /* preconfiguration */
65 ngx_http_auth_basic_init, /* postconfiguration */
67 NULL, /* create main configuration */
68 NULL, /* init main configuration */
70 NULL, /* create server configuration */
71 NULL, /* merge server configuration */
73 ngx_http_auth_basic_create_loc_conf, /* create location configuration */
74 ngx_http_auth_basic_merge_loc_conf /* merge location configuration */
78 ngx_module_t ngx_http_auth_basic_module = {
80 &ngx_http_auth_basic_module_ctx, /* module context */
81 ngx_http_auth_basic_commands, /* module directives */
82 NGX_HTTP_MODULE, /* module type */
83 NULL, /* init master */
84 NULL, /* init module */
85 NULL, /* init process */
86 NULL, /* init thread */
87 NULL, /* exit thread */
88 NULL, /* exit process */
89 NULL, /* exit master */
95 ngx_http_auth_basic_handler(ngx_http_request_t *r)
102 ngx_uint_t i, login, left, passwd;
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];
113 alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_basic_module);
115 if (alcf->realm.len == 0 || alcf->user_file.len == 0) {
119 ctx = ngx_http_get_module_ctx(r, ngx_http_auth_basic_module);
122 return ngx_http_auth_basic_crypt_handler(r, ctx, &ctx->passwd,
126 rc = ngx_http_auth_basic_user(r);
128 if (rc == NGX_DECLINED) {
130 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
131 "no user/password was provided for basic authentication");
133 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
136 if (rc == NGX_ERROR) {
137 return NGX_HTTP_INTERNAL_SERVER_ERROR;
140 fd = ngx_open_file(alcf->user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
142 if (fd == NGX_INVALID_FILE) {
143 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
144 ngx_open_file_n " \"%s\" failed", alcf->user_file.data);
145 return NGX_HTTP_INTERNAL_SERVER_ERROR;
148 ngx_memzero(&file, sizeof(ngx_file_t));
151 file.name = alcf->user_file;
152 file.log = r->connection->log;
163 n = ngx_read_file(&file, buf + left, NGX_HTTP_AUTH_BUF_SIZE - left,
166 if (n == NGX_ERROR) {
167 ngx_http_auth_basic_close(&file);
168 return NGX_HTTP_INTERNAL_SERVER_ERROR;
175 for (i = left; i < left + n; i++) {
181 if (buf[i] == '#' || buf[i] == CR) {
191 if (buf[i] != r->headers_in.user.data[login]) {
196 if (login == r->headers_in.user.len) {
206 if (buf[i] == LF || buf[i] == CR || buf[i] == ':') {
209 ngx_http_auth_basic_close(&file);
211 pwd.len = i - passwd;
212 pwd.data = &buf[passwd];
214 return ngx_http_auth_basic_crypt_handler(r, NULL, &pwd,
230 if (state == sw_passwd) {
231 left = left + n - passwd;
232 ngx_memcpy(buf, &buf[passwd], left);
242 ngx_http_auth_basic_close(&file);
244 if (state == sw_passwd) {
245 pwd.len = i - passwd;
246 pwd.data = ngx_pnalloc(r->pool, pwd.len + 1);
247 if (pwd.data == NULL) {
248 return NGX_HTTP_INTERNAL_SERVER_ERROR;
251 ngx_cpystrn(pwd.data, &buf[passwd], pwd.len + 1);
253 return ngx_http_auth_basic_crypt_handler(r, NULL, &pwd, &alcf->realm);
256 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
257 "user \"%V\" was not found in \"%V\"",
258 &r->headers_in.user, &alcf->user_file);
260 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
265 ngx_http_auth_basic_crypt_handler(ngx_http_request_t *r,
266 ngx_http_auth_basic_ctx_t *ctx, ngx_str_t *passwd, ngx_str_t *realm)
271 rc = ngx_crypt(r->pool, r->headers_in.passwd.data, passwd->data,
274 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
275 "rc: %d user: \"%V\" salt: \"%s\"",
276 rc, &r->headers_in.user, passwd->data);
279 if (ngx_strcmp(encrypted, passwd->data) == 0) {
283 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
284 "encrypted: \"%s\"", encrypted);
286 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
287 "user \"%V\": password mismatch",
288 &r->headers_in.user);
290 return ngx_http_auth_basic_set_realm(r, realm);
293 if (rc == NGX_ERROR) {
294 return NGX_HTTP_INTERNAL_SERVER_ERROR;
297 /* rc == NGX_AGAIN */
300 ctx = ngx_palloc(r->pool, sizeof(ngx_http_auth_basic_ctx_t));
302 return NGX_HTTP_INTERNAL_SERVER_ERROR;
305 ngx_http_set_ctx(r, ctx, ngx_http_auth_basic_module);
307 ctx->passwd.len = passwd->len;
310 ctx->passwd.data = ngx_pstrdup(r->pool, passwd);
311 if (ctx->passwd.data == NULL) {
312 return NGX_HTTP_INTERNAL_SERVER_ERROR;
317 /* TODO: add mutex event */
324 ngx_http_auth_basic_set_realm(ngx_http_request_t *r, ngx_str_t *realm)
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;
331 r->headers_out.www_authenticate->hash = 1;
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;
336 return NGX_HTTP_UNAUTHORIZED;
340 ngx_http_auth_basic_close(ngx_file_t *file)
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);
350 ngx_http_auth_basic_create_loc_conf(ngx_conf_t *cf)
352 ngx_http_auth_basic_loc_conf_t *conf;
354 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_basic_loc_conf_t));
356 return NGX_CONF_ERROR;
364 ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
366 ngx_http_auth_basic_loc_conf_t *prev = parent;
367 ngx_http_auth_basic_loc_conf_t *conf = child;
369 if (conf->realm.data == NULL) {
370 conf->realm = prev->realm;
373 if (conf->user_file.data) {
374 if (ngx_conf_full_name(cf->cycle, &conf->user_file, 1) != NGX_OK) {
375 return NGX_CONF_ERROR;
379 conf->user_file = prev->user_file;
387 ngx_http_auth_basic_init(ngx_conf_t *cf)
389 ngx_http_handler_pt *h;
390 ngx_http_core_main_conf_t *cmcf;
392 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
394 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
399 *h = ngx_http_auth_basic_handler;
406 ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data)
408 ngx_str_t *realm = data;
413 if (ngx_strcmp(realm->data, "off") == 0) {
415 realm->data = (u_char *) "";
420 len = sizeof("Basic realm=\"") - 1 + realm->len + 1;
422 basic = ngx_pnalloc(cf->pool, len);
424 return NGX_CONF_ERROR;
427 p = ngx_cpymem(basic, "Basic realm=\"", sizeof("Basic realm=\"") - 1);
428 p = ngx_cpymem(p, realm->data, realm->len);