2005-03-23 00:02:46 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define NGX_HTTP_AUTH_BUF_SIZE 2048
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_str_t passwd;
|
|
|
|
} ngx_http_auth_basic_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_str_t realm;
|
|
|
|
ngx_str_t user_file;
|
|
|
|
} ngx_http_auth_basic_loc_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t ngx_http_auth_basic_handler(ngx_http_request_t *r);
|
|
|
|
static ngx_int_t ngx_http_auth_basic_crypt_handler(ngx_http_request_t *r,
|
|
|
|
ngx_http_auth_basic_ctx_t *ctx, ngx_str_t *passwd, ngx_str_t *realm);
|
|
|
|
static ngx_int_t ngx_http_auth_basic_set_realm(ngx_http_request_t *r,
|
|
|
|
ngx_str_t *realm);
|
|
|
|
static void ngx_http_auth_basic_close(ngx_file_t *file);
|
|
|
|
static void *ngx_http_auth_basic_create_loc_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf,
|
|
|
|
void *parent, void *child);
|
|
|
|
static ngx_int_t ngx_http_auth_basic_init(ngx_cycle_t *cycle);
|
|
|
|
static char *ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data);
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_conf_post_handler_pt ngx_http_auth_basic_p = ngx_http_auth_basic;
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_auth_basic_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("auth_basic"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_str_slot,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_auth_basic_loc_conf_t, realm),
|
|
|
|
&ngx_http_auth_basic_p },
|
|
|
|
|
|
|
|
{ ngx_string("auth_basic_user_file"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_str_slot,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_auth_basic_loc_conf_t, user_file),
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngx_http_module_t ngx_http_auth_basic_module_ctx = {
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
NULL, /* preconfiguration */
|
|
|
|
NULL, /* postconfiguration */
|
2005-03-23 00:02:46 +08:00
|
|
|
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
ngx_http_auth_basic_create_loc_conf, /* create location configuration */
|
|
|
|
ngx_http_auth_basic_merge_loc_conf /* merge location configuration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_auth_basic_module = {
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
NGX_MODULE_V1,
|
2005-03-23 00:02:46 +08:00
|
|
|
&ngx_http_auth_basic_module_ctx, /* module context */
|
|
|
|
ngx_http_auth_basic_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE, /* module type */
|
|
|
|
ngx_http_auth_basic_init, /* init module */
|
|
|
|
NULL /* init process */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_auth_basic_handler(ngx_http_request_t *r)
|
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
ssize_t n;
|
|
|
|
ngx_fd_t fd;
|
2005-09-07 00:09:32 +08:00
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t pwd;
|
|
|
|
ngx_uint_t i, login, left, passwd;
|
2005-03-23 00:02:46 +08:00
|
|
|
ngx_file_t file;
|
|
|
|
ngx_http_auth_basic_ctx_t *ctx;
|
|
|
|
ngx_http_auth_basic_loc_conf_t *alcf;
|
|
|
|
u_char buf[NGX_HTTP_AUTH_BUF_SIZE];
|
|
|
|
enum {
|
|
|
|
sw_login,
|
|
|
|
sw_passwd,
|
|
|
|
sw_skip
|
|
|
|
} state;
|
|
|
|
|
|
|
|
alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_basic_module);
|
|
|
|
|
|
|
|
if (alcf->realm.len == 0 || alcf->user_file.len == 0) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = ngx_http_get_module_ctx(r, ngx_http_auth_basic_module);
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
return ngx_http_auth_basic_crypt_handler(r, ctx, &ctx->passwd,
|
|
|
|
&alcf->realm);
|
|
|
|
}
|
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
rc = ngx_http_auth_basic_user(r);
|
2005-03-23 00:02:46 +08:00
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
if (rc == NGX_DECLINED) {
|
2005-03-23 00:02:46 +08:00
|
|
|
return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
|
|
}
|
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
2005-03-23 00:02:46 +08:00
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = ngx_open_file(alcf->user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN);
|
|
|
|
|
|
|
|
if (fd == NGX_INVALID_FILE) {
|
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
|
|
|
|
ngx_open_file_n " \"%s\" failed", alcf->user_file.data);
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memzero(&file, sizeof(ngx_file_t));
|
|
|
|
|
|
|
|
file.fd = fd;
|
|
|
|
file.name = alcf->user_file;
|
|
|
|
file.log = r->connection->log;
|
|
|
|
|
|
|
|
state = sw_login;
|
|
|
|
passwd = 0;
|
|
|
|
login = 0;
|
|
|
|
left = 0;
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
n = ngx_read_file(&file, buf + left, NGX_HTTP_AUTH_BUF_SIZE - left,
|
|
|
|
offset);
|
|
|
|
|
|
|
|
if (n == NGX_ERROR) {
|
|
|
|
ngx_http_auth_basic_close(&file);
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = left; i < left + n; i++) {
|
|
|
|
switch (state) {
|
|
|
|
|
|
|
|
case sw_login:
|
|
|
|
if (login == 0 && buf[i] == '#') {
|
|
|
|
state = sw_skip;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
if (buf[i] != r->headers_in.user.data[login]) {
|
2005-03-23 00:02:46 +08:00
|
|
|
state = sw_skip;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
if (login == r->headers_in.user.len) {
|
2005-03-23 00:02:46 +08:00
|
|
|
state = sw_passwd;
|
|
|
|
passwd = i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
login++;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sw_passwd:
|
|
|
|
if (buf[i] == LF || buf[i] == CR || buf[i] == ':') {
|
|
|
|
buf[i] = '\0';
|
|
|
|
|
|
|
|
ngx_http_auth_basic_close(&file);
|
|
|
|
|
|
|
|
pwd.len = i - passwd;
|
|
|
|
pwd.data = &buf[passwd];
|
|
|
|
|
|
|
|
return ngx_http_auth_basic_crypt_handler(r, NULL, &pwd,
|
|
|
|
&alcf->realm);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case sw_skip:
|
|
|
|
if (buf[i] == LF) {
|
|
|
|
state = sw_login;
|
|
|
|
login = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == sw_passwd) {
|
|
|
|
left = left + n - passwd;
|
|
|
|
ngx_memcpy(buf, &buf[passwd], left);
|
|
|
|
passwd = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_http_auth_basic_close(&file);
|
|
|
|
|
|
|
|
return ngx_http_auth_basic_set_realm(r, &alcf->realm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_auth_basic_crypt_handler(ngx_http_request_t *r,
|
|
|
|
ngx_http_auth_basic_ctx_t *ctx, ngx_str_t *passwd, ngx_str_t *realm)
|
|
|
|
{
|
|
|
|
ngx_int_t rc;
|
|
|
|
u_char *encrypted;
|
|
|
|
|
|
|
|
rc = ngx_crypt(r->pool, r->headers_in.passwd.data, passwd->data,
|
|
|
|
&encrypted);
|
|
|
|
|
|
|
|
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"rc: %d user: \"%V\" salt: \"%s\"",
|
|
|
|
rc, &r->headers_in.user, passwd->data);
|
|
|
|
|
|
|
|
if (rc == NGX_OK) {
|
|
|
|
if (ngx_strcmp(encrypted, passwd->data) == 0) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"encrypted: \"%s\"", encrypted);
|
|
|
|
|
|
|
|
return ngx_http_auth_basic_set_realm(r, realm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rc == NGX_AGAIN */
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
ctx = ngx_palloc(r->pool, sizeof(ngx_http_auth_basic_ctx_t));
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_http_set_ctx(r, ctx, ngx_http_auth_basic_module);
|
|
|
|
|
|
|
|
ctx->passwd.len = passwd->len;
|
|
|
|
passwd->len++;
|
|
|
|
|
|
|
|
ctx->passwd.data = ngx_pstrdup(r->pool, passwd);
|
|
|
|
if (ctx->passwd.data == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: add mutex event */
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_auth_basic_set_realm(ngx_http_request_t *r, ngx_str_t *realm)
|
|
|
|
{
|
|
|
|
r->headers_out.www_authenticate = ngx_list_push(&r->headers_out.headers);
|
|
|
|
if (r->headers_out.www_authenticate == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
r->headers_out.www_authenticate->hash = 1;
|
2005-03-23 00:02:46 +08:00
|
|
|
r->headers_out.www_authenticate->key.len = sizeof("WWW-Authenticate") - 1;
|
|
|
|
r->headers_out.www_authenticate->key.data = (u_char *) "WWW-Authenticate";
|
|
|
|
r->headers_out.www_authenticate->value = *realm;
|
|
|
|
|
|
|
|
return NGX_HTTP_UNAUTHORIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_auth_basic_close(ngx_file_t *file)
|
|
|
|
{
|
|
|
|
if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, file->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", file->name.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_http_auth_basic_create_loc_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_auth_basic_loc_conf_t *conf;
|
|
|
|
|
|
|
|
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_basic_loc_conf_t));
|
|
|
|
if (conf == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_auth_basic_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_http_auth_basic_loc_conf_t *prev = parent;
|
|
|
|
ngx_http_auth_basic_loc_conf_t *conf = child;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
u_char *realm, *p;
|
|
|
|
|
|
|
|
if (conf->realm.data) {
|
|
|
|
if (conf->realm.len) {
|
|
|
|
len = sizeof("Basic realm=\"") - 1 + conf->realm.len + 1;
|
|
|
|
|
|
|
|
realm = ngx_palloc(cf->pool, len);
|
|
|
|
if (realm == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ngx_cpymem(realm, "Basic realm=\"",
|
|
|
|
sizeof("Basic realm=\"") - 1);
|
|
|
|
p = ngx_cpymem(p, conf->realm.data, conf->realm.len);
|
|
|
|
*p = '"';
|
|
|
|
|
|
|
|
conf->realm.len = len;
|
|
|
|
conf->realm.data = realm;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
conf->realm = prev->realm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (conf->user_file.data) {
|
|
|
|
if (ngx_conf_full_name(cf->cycle, &conf->user_file) != NGX_OK) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
conf->user_file = prev->user_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_auth_basic_init(ngx_cycle_t *cycle)
|
|
|
|
{
|
|
|
|
ngx_http_handler_pt *h;
|
|
|
|
ngx_http_core_main_conf_t *cmcf;
|
|
|
|
|
|
|
|
cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module);
|
|
|
|
|
|
|
|
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
|
|
|
|
if (h == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*h = ngx_http_auth_basic_handler;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data)
|
|
|
|
{
|
|
|
|
ngx_str_t *realm = data;
|
|
|
|
|
|
|
|
if (ngx_strcmp(realm->data, "off") == 0) {
|
|
|
|
realm->len = 0;
|
|
|
|
realm->data = (u_char *) "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|