Klaus Demo nginx / 85e989e
auth_basic_user_file supports variables Igor Sysoev 13 years ago
1 changed file(s) with 102 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
1212
1313
1414 typedef struct {
15 ngx_str_t passwd;
15 ngx_str_t passwd;
1616 } ngx_http_auth_basic_ctx_t;
1717
1818
1919 typedef struct {
20 ngx_str_t realm;
21 ngx_str_t user_file;
20 ngx_str_t realm;
21 ngx_str_t user_file;
22 ngx_array_t *user_file_lengths;
23 ngx_array_t *user_file_values;
2224 } ngx_http_auth_basic_loc_conf_t;
2325
2426
3335 void *parent, void *child);
3436 static ngx_int_t ngx_http_auth_basic_init(ngx_conf_t *cf);
3537 static char *ngx_http_auth_basic(ngx_conf_t *cf, void *post, void *data);
38 static char *ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd,
39 void *conf);
3640
3741
3842 static ngx_conf_post_handler_pt ngx_http_auth_basic_p = ngx_http_auth_basic;
5054 { ngx_string("auth_basic_user_file"),
5155 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
5256 |NGX_CONF_TAKE1,
53 ngx_conf_set_str_slot,
57 ngx_http_auth_basic_user_file,
5458 NGX_HTTP_LOC_CONF_OFFSET,
5559 offsetof(ngx_http_auth_basic_loc_conf_t, user_file),
5660 NULL },
97101 ssize_t n;
98102 ngx_fd_t fd;
99103 ngx_int_t rc;
100 ngx_str_t pwd;
101 ngx_uint_t i, login, left, passwd;
104 ngx_err_t err;
105 ngx_str_t pwd, user_file;
106 ngx_uint_t i, level, login, left, passwd;
102107 ngx_file_t file;
103108 ngx_http_auth_basic_ctx_t *ctx;
104109 ngx_http_auth_basic_loc_conf_t *alcf;
136141 return NGX_HTTP_INTERNAL_SERVER_ERROR;
137142 }
138143
139 fd = ngx_open_file(alcf->user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
144 if (alcf->user_file_lengths) {
145 if (ngx_http_script_run(r, &user_file, alcf->user_file_lengths->elts, 1,
146 alcf->user_file_values->elts)
147 == NULL)
148 {
149 return NGX_ERROR;
150 }
151
152 user_file.data[--user_file.len] = '\0';
153
154 } else {
155 user_file = alcf->user_file;
156 }
157
158 fd = ngx_open_file(user_file.data, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
140159
141160 if (fd == NGX_INVALID_FILE) {
142 ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
143 ngx_open_file_n " \"%s\" failed", alcf->user_file.data);
144 return NGX_HTTP_INTERNAL_SERVER_ERROR;
161 err = ngx_errno;
162
163 if (err == NGX_ENOENT) {
164 level = NGX_LOG_ERR;
165 rc = NGX_HTTP_FORBIDDEN;
166
167 } else {
168 level = NGX_LOG_CRIT;
169 rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
170 }
171
172 ngx_log_error(level, r->connection->log, err,
173 ngx_open_file_n " \"%s\" failed", user_file.data);
174
175 return rc;
145176 }
146177
147178 ngx_memzero(&file, sizeof(ngx_file_t));
148179
149180 file.fd = fd;
150 file.name = alcf->user_file;
181 file.name = user_file;
151182 file.log = r->connection->log;
152183
153184 state = sw_login;
254285
255286 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
256287 "user \"%V\" was not found in \"%V\"",
257 &r->headers_in.user, &alcf->user_file);
288 &r->headers_in.user, &user_file);
258289
259290 return ngx_http_auth_basic_set_realm(r, &alcf->realm);
260291 }
369400 conf->realm = prev->realm;
370401 }
371402
372 if (conf->user_file.data) {
373 if (ngx_conf_full_name(cf->cycle, &conf->user_file, 1) != NGX_OK) {
374 return NGX_CONF_ERROR;
375 }
376
377 } else {
403 if (conf->user_file.data == NULL) {
378404 conf->user_file = prev->user_file;
405 conf->user_file_lengths = prev->user_file_lengths;
406 conf->user_file_values = prev->user_file_values;
379407 }
380408
381409 return NGX_CONF_OK;
432460
433461 return NGX_CONF_OK;
434462 }
463
464
465 static char *
466 ngx_http_auth_basic_user_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
467 {
468 ngx_http_auth_basic_loc_conf_t *alcf = conf;
469
470 ngx_str_t *value;
471 ngx_uint_t n;
472 ngx_http_core_loc_conf_t *clcf;
473 ngx_http_script_compile_t sc;
474
475 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
476
477 if (alcf->user_file.data) {
478 return "is duplicate";
479 }
480
481 value = cf->args->elts;
482
483 alcf->user_file = value[1];
484
485 if (alcf->user_file.len == 0) {
486 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
487 "invalid parameter \"%V\"", &alcf->user_file);
488 return NGX_CONF_ERROR;
489 }
490
491 if (alcf->user_file.data[0] != '$') {
492 if (ngx_conf_full_name(cf->cycle, &alcf->user_file, 1) != NGX_OK) {
493 return NGX_CONF_ERROR;
494 }
495 }
496
497 n = ngx_http_script_variables_count(&alcf->user_file);
498
499 if (n == 0) {
500 return NGX_CONF_OK;
501 }
502
503 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
504
505 sc.cf = cf;
506 sc.source = &alcf->user_file;
507 sc.lengths = &alcf->user_file_lengths;
508 sc.values = &alcf->user_file_values;
509 sc.variables = n;
510 sc.complete_lengths = 1;
511 sc.complete_values = 1;
512
513 if (ngx_http_script_compile(&sc) != NGX_OK) {
514 return NGX_CONF_ERROR;
515 }
516
517 return NGX_CONF_OK;
518 }