xref: /lighttpd1.4/src/mod_auth.c (revision d6f692f1)
18abd06a7SGlenn Strauss #include "first.h"
28abd06a7SGlenn Strauss 
31fc8a3e1SGlenn Strauss #include <stdlib.h>
41fc8a3e1SGlenn Strauss #include <string.h>
51fc8a3e1SGlenn Strauss 
61fc8a3e1SGlenn Strauss #include "sys-crypto-md.h" /* USE_LIB_CRYPTO */
71fc8a3e1SGlenn Strauss 
804d76e7aSGlenn Strauss #include "base.h"
922e8b456SStefan Bühler #include "plugin.h"
10*d6f692f1SGlenn Strauss #include "plugin_config.h"
1122e8b456SStefan Bühler #include "http_auth.h"
123dd3cde9SGlenn Strauss #include "http_header.h"
1322e8b456SStefan Bühler #include "log.h"
14e11514b0SGlenn Strauss #include "safe_memclear.h"
15156e1da2SGlenn Strauss #include "algo_splaytree.h"
16bcdc6a3bSJan Kneschke 
17bcdc6a3bSJan Kneschke /**
187ba06c71SGlenn Strauss  * auth framework
19bcdc6a3bSJan Kneschke  */
20bcdc6a3bSJan Kneschke 
214b3a91e6SGlenn Strauss typedef struct {
22e11514b0SGlenn Strauss     splay_tree *sptree; /* data in nodes of tree are (http_auth_cache_entry *)*/
23e11514b0SGlenn Strauss     time_t max_age;
24e11514b0SGlenn Strauss } http_auth_cache;
25e11514b0SGlenn Strauss 
26e11514b0SGlenn Strauss typedef struct {
274b3a91e6SGlenn Strauss     const http_auth_backend_t *auth_backend;
288e713130SGlenn Strauss     const array *auth_require;
29e11514b0SGlenn Strauss     http_auth_cache *auth_cache;
308e713130SGlenn Strauss     unsigned int auth_extern_authn;
317ba06c71SGlenn Strauss } plugin_config;
324b3a91e6SGlenn Strauss 
334b3a91e6SGlenn Strauss typedef struct {
344b3a91e6SGlenn Strauss     PLUGIN_DATA;
358e713130SGlenn Strauss     plugin_config defaults;
367ba06c71SGlenn Strauss     plugin_config conf;
377ba06c71SGlenn Strauss } plugin_data;
387ba06c71SGlenn Strauss 
39e11514b0SGlenn Strauss typedef struct {
40e11514b0SGlenn Strauss     const struct http_auth_require_t *require;
41e11514b0SGlenn Strauss     time_t ctime;
42e11514b0SGlenn Strauss     int dalgo;
43e11514b0SGlenn Strauss     uint32_t dlen;
44e11514b0SGlenn Strauss     uint32_t ulen;
45e11514b0SGlenn Strauss     char *username;
46e11514b0SGlenn Strauss     char *pwdigest;
47e11514b0SGlenn Strauss } http_auth_cache_entry;
48e11514b0SGlenn Strauss 
49e11514b0SGlenn Strauss static http_auth_cache_entry *
50e11514b0SGlenn Strauss http_auth_cache_entry_init (const struct http_auth_require_t * const require, const int dalgo, const char *username, const uint32_t ulen, const char *pw, const uint32_t pwlen)
51e11514b0SGlenn Strauss {
52e11514b0SGlenn Strauss     /*(similar to buffer_copy_string_len() for each element,
53e11514b0SGlenn Strauss      * but allocate exact lengths in single chunk of memory
54e11514b0SGlenn Strauss      * for cache to avoid wasting space and for memory locality)*/
55e11514b0SGlenn Strauss     /* http_auth_require_t is stored instead of copying realm
56e11514b0SGlenn Strauss      *(store pointer to http_auth_require_t, which is persistent
57e11514b0SGlenn Strauss      * and will be different for each realm + permissions combo)*/
58e11514b0SGlenn Strauss     http_auth_cache_entry * const ae =
59e11514b0SGlenn Strauss       malloc(sizeof(http_auth_cache_entry) + ulen + pwlen);
60e11514b0SGlenn Strauss     force_assert(ae);
61e11514b0SGlenn Strauss     ae->require = require;
62e11514b0SGlenn Strauss     ae->ctime = log_epoch_secs;
63e11514b0SGlenn Strauss     ae->dalgo = dalgo;
64e11514b0SGlenn Strauss     ae->ulen = ulen;
65e11514b0SGlenn Strauss     ae->dlen = pwlen;
66e11514b0SGlenn Strauss     ae->username = (char *)(ae + 1);
67e11514b0SGlenn Strauss     ae->pwdigest = ae->username + ulen;
68e11514b0SGlenn Strauss     memcpy(ae->username, username, ulen);
69e11514b0SGlenn Strauss     memcpy(ae->pwdigest, pw, pwlen);
70e11514b0SGlenn Strauss     return ae;
71e11514b0SGlenn Strauss }
72e11514b0SGlenn Strauss 
73e11514b0SGlenn Strauss static void
74e11514b0SGlenn Strauss http_auth_cache_entry_free (void *data)
75e11514b0SGlenn Strauss {
76e11514b0SGlenn Strauss     http_auth_cache_entry * const ae = data;
77e11514b0SGlenn Strauss     safe_memclear(ae->pwdigest, ae->dlen);
78e11514b0SGlenn Strauss     free(ae);
79e11514b0SGlenn Strauss }
80e11514b0SGlenn Strauss 
81e11514b0SGlenn Strauss static void
82e11514b0SGlenn Strauss http_auth_cache_free (http_auth_cache *ac)
83e11514b0SGlenn Strauss {
84e11514b0SGlenn Strauss     splay_tree *sptree = ac->sptree;
85e11514b0SGlenn Strauss     while (sptree) {
86e11514b0SGlenn Strauss         http_auth_cache_entry_free(sptree->data);
87e11514b0SGlenn Strauss         sptree = splaytree_delete(sptree, sptree->key);
88e11514b0SGlenn Strauss     }
89e11514b0SGlenn Strauss     free(ac);
90e11514b0SGlenn Strauss }
91e11514b0SGlenn Strauss 
92e11514b0SGlenn Strauss static http_auth_cache *
93e11514b0SGlenn Strauss http_auth_cache_init (const array *opts)
94e11514b0SGlenn Strauss {
95e11514b0SGlenn Strauss     http_auth_cache *ac = malloc(sizeof(http_auth_cache));
96e11514b0SGlenn Strauss     force_assert(ac);
97e11514b0SGlenn Strauss     ac->sptree = NULL;
98e11514b0SGlenn Strauss     ac->max_age = 600; /* 10 mins */
99e11514b0SGlenn Strauss     for (uint32_t i = 0, used = opts->used; i < used; ++i) {
100*d6f692f1SGlenn Strauss         data_unset *du = opts->data[i];
101*d6f692f1SGlenn Strauss         if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age")))
102*d6f692f1SGlenn Strauss             ac->max_age = (time_t)config_plugin_value_to_int32(du, ac->max_age);
103e11514b0SGlenn Strauss     }
104e11514b0SGlenn Strauss     return ac;
105e11514b0SGlenn Strauss }
106e11514b0SGlenn Strauss 
107e11514b0SGlenn Strauss static int
108e11514b0SGlenn Strauss http_auth_cache_hash (const struct http_auth_require_t * const require, const char *username, const uint32_t ulen)
109e11514b0SGlenn Strauss {
110e11514b0SGlenn Strauss     uint32_t h = /*(hash pointer value, which includes realm and permissions)*/
111e11514b0SGlenn Strauss       djbhash((char *)(intptr_t)require, sizeof(intptr_t), DJBHASH_INIT);
112e11514b0SGlenn Strauss     h = djbhash(username, ulen, h);
113e11514b0SGlenn Strauss     /* strip highest bit of hash value for splaytree (see splaytree_djbhash())*/
114e11514b0SGlenn Strauss     return (int32_t)(h & ~(((uint32_t)1) << 31));
115e11514b0SGlenn Strauss }
116e11514b0SGlenn Strauss 
117e11514b0SGlenn Strauss static http_auth_cache_entry *
118e11514b0SGlenn Strauss http_auth_cache_query (splay_tree ** const sptree, const int ndx)
119e11514b0SGlenn Strauss {
120e11514b0SGlenn Strauss     *sptree = splaytree_splay(*sptree, ndx);
121e11514b0SGlenn Strauss     return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL;
122e11514b0SGlenn Strauss }
123e11514b0SGlenn Strauss 
124e11514b0SGlenn Strauss static void
125e11514b0SGlenn Strauss http_auth_cache_insert (splay_tree ** const sptree, const int ndx, void * const data, void(data_free_fn)(void *))
126e11514b0SGlenn Strauss {
127e11514b0SGlenn Strauss     /*(not necessary to re-splay (with current usage) since single-threaded
128e11514b0SGlenn Strauss      * and splaytree has not been modified since http_auth_cache_query())*/
129e11514b0SGlenn Strauss     /* *sptree = splaytree_splay(*sptree, ndx); */
130e11514b0SGlenn Strauss     if (NULL == *sptree || (*sptree)->key != ndx)
131e11514b0SGlenn Strauss         *sptree = splaytree_insert(*sptree, ndx, data);
132e11514b0SGlenn Strauss     else { /* collision; replace old entry */
133e11514b0SGlenn Strauss         data_free_fn((*sptree)->data);
134e11514b0SGlenn Strauss         (*sptree)->data = data;
135e11514b0SGlenn Strauss     }
136e11514b0SGlenn Strauss }
137e11514b0SGlenn Strauss 
138e11514b0SGlenn Strauss /* walk though cache, collect expired ids, and remove them in a second loop */
139e11514b0SGlenn Strauss static void
140e11514b0SGlenn Strauss mod_auth_tag_old_entries (splay_tree * const t, int * const keys, int * const ndx, const time_t max_age, const time_t cur_ts)
141e11514b0SGlenn Strauss {
142e11514b0SGlenn Strauss     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
143e11514b0SGlenn Strauss     if (t->left)
144e11514b0SGlenn Strauss         mod_auth_tag_old_entries(t->left, keys, ndx, max_age, cur_ts);
145e11514b0SGlenn Strauss     if (t->right)
146e11514b0SGlenn Strauss         mod_auth_tag_old_entries(t->right, keys, ndx, max_age, cur_ts);
147e11514b0SGlenn Strauss     if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
148e11514b0SGlenn Strauss 
149e11514b0SGlenn Strauss     const http_auth_cache_entry * const ae = t->data;
150e11514b0SGlenn Strauss     if (cur_ts - ae->ctime > max_age)
151e11514b0SGlenn Strauss         keys[(*ndx)++] = t->key;
152e11514b0SGlenn Strauss }
153e11514b0SGlenn Strauss 
154e11514b0SGlenn Strauss __attribute_noinline__
155e11514b0SGlenn Strauss static void
156e11514b0SGlenn Strauss mod_auth_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const time_t cur_ts)
157e11514b0SGlenn Strauss {
158e11514b0SGlenn Strauss     splay_tree *sptree = *sptree_ptr;
159e11514b0SGlenn Strauss     int max_ndx, i;
160e11514b0SGlenn Strauss     int keys[8192]; /* 32k size on stack */
161e11514b0SGlenn Strauss     do {
162e11514b0SGlenn Strauss         if (!sptree) break;
163e11514b0SGlenn Strauss         max_ndx = 0;
164e11514b0SGlenn Strauss         mod_auth_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts);
165e11514b0SGlenn Strauss         for (i = 0; i < max_ndx; ++i) {
166e11514b0SGlenn Strauss             int ndx = keys[i];
167e11514b0SGlenn Strauss             sptree = splaytree_splay(sptree, ndx);
168e11514b0SGlenn Strauss             if (sptree && sptree->key == ndx) {
169e11514b0SGlenn Strauss                 http_auth_cache_entry_free(sptree->data);
170e11514b0SGlenn Strauss                 sptree = splaytree_delete(sptree, ndx);
171e11514b0SGlenn Strauss             }
172e11514b0SGlenn Strauss         }
173e11514b0SGlenn Strauss     } while (max_ndx == sizeof(keys)/sizeof(int));
174e11514b0SGlenn Strauss     *sptree_ptr = sptree;
175e11514b0SGlenn Strauss }
176e11514b0SGlenn Strauss 
177e11514b0SGlenn Strauss TRIGGER_FUNC(mod_auth_periodic)
178e11514b0SGlenn Strauss {
179e11514b0SGlenn Strauss     const plugin_data * const p = p_d;
180e11514b0SGlenn Strauss     const time_t cur_ts = log_epoch_secs;
181e11514b0SGlenn Strauss     if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/
182e11514b0SGlenn Strauss     UNUSED(srv);
183e11514b0SGlenn Strauss 
184e11514b0SGlenn Strauss     /* future: might construct array of (http_auth_cache *) at startup
185e11514b0SGlenn Strauss      *         to avoid the need to search for them here */
186e11514b0SGlenn Strauss     for (int i = 0, used = p->nconfig; i < used; ++i) {
187e11514b0SGlenn Strauss         const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
188e11514b0SGlenn Strauss         for (; cpv->k_id != -1; ++cpv) {
189e11514b0SGlenn Strauss             if (cpv->k_id != 3) continue; /* k_id == 3 for auth.cache */
190e11514b0SGlenn Strauss             if (cpv->vtype != T_CONFIG_LOCAL) continue;
191e11514b0SGlenn Strauss             http_auth_cache *ac = cpv->v.v;
192e11514b0SGlenn Strauss             mod_auth_periodic_cleanup(&ac->sptree, ac->max_age, cur_ts);
193e11514b0SGlenn Strauss         }
194e11514b0SGlenn Strauss     }
195e11514b0SGlenn Strauss 
196e11514b0SGlenn Strauss     return HANDLER_GO_ON;
197e11514b0SGlenn Strauss }
198e11514b0SGlenn Strauss 
199e11514b0SGlenn Strauss 
200e11514b0SGlenn Strauss 
201e11514b0SGlenn Strauss 
2027c7f8c46SGlenn Strauss static handler_t mod_auth_check_basic(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
2037c7f8c46SGlenn Strauss static handler_t mod_auth_check_digest(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
2047c7f8c46SGlenn Strauss static handler_t mod_auth_check_extern(request_st *r, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend);
2054b3a91e6SGlenn Strauss 
206bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) {
207e11514b0SGlenn Strauss 	static http_auth_scheme_t http_auth_scheme_basic  = { "basic",  mod_auth_check_basic,  NULL };
208e11514b0SGlenn Strauss 	static http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL };
2097ba06c71SGlenn Strauss 	static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL };
210e11514b0SGlenn Strauss 	plugin_data *p = calloc(1, sizeof(*p));
211e11514b0SGlenn Strauss 	force_assert(p);
2127ba06c71SGlenn Strauss 
2137ba06c71SGlenn Strauss 	/* register http_auth_scheme_* */
214e11514b0SGlenn Strauss 	http_auth_scheme_basic.p_d = p;
2157ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_basic);
216e11514b0SGlenn Strauss 	http_auth_scheme_digest.p_d = p;
2177ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_digest);
2187ba06c71SGlenn Strauss 	http_auth_scheme_set(&http_auth_scheme_extern);
219bcdc6a3bSJan Kneschke 
220bcdc6a3bSJan Kneschke 	return p;
221bcdc6a3bSJan Kneschke }
222bcdc6a3bSJan Kneschke 
223b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) {
224b73949e0SGlenn Strauss     plugin_data * const p = p_d;
2258e713130SGlenn Strauss     if (NULL == p->cvlist) return;
2268e713130SGlenn Strauss     /* (init i to 0 if global context; to 1 to skip empty global context) */
2278e713130SGlenn Strauss     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
2288e713130SGlenn Strauss         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
2298e713130SGlenn Strauss         for (; -1 != cpv->k_id; ++cpv) {
2308e713130SGlenn Strauss             if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
2318e713130SGlenn Strauss             switch (cpv->k_id) {
2328e713130SGlenn Strauss               case 1: /* auth.require */
2338e713130SGlenn Strauss                 array_free(cpv->v.v);
2348e713130SGlenn Strauss                 break;
235e11514b0SGlenn Strauss               case 3: /* auth.cache */
236e11514b0SGlenn Strauss                 http_auth_cache_free(cpv->v.v);
237e11514b0SGlenn Strauss                 break;
2388e713130SGlenn Strauss               default:
2398e713130SGlenn Strauss                 break;
2408e713130SGlenn Strauss             }
2418e713130SGlenn Strauss         }
2428e713130SGlenn Strauss     }
2438e713130SGlenn Strauss }
2448e713130SGlenn Strauss 
2457ba06c71SGlenn Strauss /* data type for mod_auth structured data
2467ba06c71SGlenn Strauss  * (parsed from auth.require array of strings) */
2477ba06c71SGlenn Strauss typedef struct {
2487ba06c71SGlenn Strauss     DATA_UNSET;
2497ba06c71SGlenn Strauss     http_auth_require_t *require;
2507ba06c71SGlenn Strauss } data_auth;
2517ba06c71SGlenn Strauss 
2527ba06c71SGlenn Strauss static void data_auth_free(data_unset *d)
2537ba06c71SGlenn Strauss {
2547ba06c71SGlenn Strauss     data_auth * const dauth = (data_auth *)d;
255ad9b7e00SGlenn Strauss     free(dauth->key.ptr);
2567ba06c71SGlenn Strauss     http_auth_require_free(dauth->require);
2577ba06c71SGlenn Strauss     free(dauth);
2587ba06c71SGlenn Strauss }
2597ba06c71SGlenn Strauss 
2607ba06c71SGlenn Strauss static data_auth *data_auth_init(void)
2617ba06c71SGlenn Strauss {
2628c7f1dfbSGlenn Strauss     static const struct data_methods fn = {
2638c7f1dfbSGlenn Strauss       NULL, /* copy must not be called on this data */
2648c7f1dfbSGlenn Strauss       data_auth_free,
2658c7f1dfbSGlenn Strauss       NULL, /* insert_dup must not be called on this data */
2668c7f1dfbSGlenn Strauss       NULL  /* print must not be called on this data */
2678c7f1dfbSGlenn Strauss     };
2687ba06c71SGlenn Strauss     data_auth * const dauth = calloc(1, sizeof(*dauth));
2697ba06c71SGlenn Strauss     force_assert(NULL != dauth);
2707ba06c71SGlenn Strauss     dauth->type       = TYPE_OTHER;
2718c7f1dfbSGlenn Strauss     dauth->fn         = &fn;
2727ba06c71SGlenn Strauss 
2737ba06c71SGlenn Strauss     dauth->require = http_auth_require_init();
2747ba06c71SGlenn Strauss 
2757ba06c71SGlenn Strauss     return dauth;
2767ba06c71SGlenn Strauss }
2777ba06c71SGlenn Strauss 
278b9e2be50SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s) {
279b9e2be50SGlenn Strauss     size_t len;
280b9e2be50SGlenn Strauss     if (NULL == s) {
281b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_MD5;
282b9e2be50SGlenn Strauss         ai->dlen  = HTTP_AUTH_DIGEST_MD5_BINLEN;
283b9e2be50SGlenn Strauss         return 1;
284b9e2be50SGlenn Strauss     }
285b9e2be50SGlenn Strauss 
286b9e2be50SGlenn Strauss     len = strlen(s);
287b9e2be50SGlenn Strauss     if (len > 5
288b9e2be50SGlenn Strauss         && (s[len-5]       ) == '-'
289b9e2be50SGlenn Strauss         && (s[len-4] | 0x20) == 's'
290b9e2be50SGlenn Strauss         && (s[len-3] | 0x20) == 'e'
291b9e2be50SGlenn Strauss         && (s[len-2] | 0x20) == 's'
292b9e2be50SGlenn Strauss         && (s[len-1] | 0x20) == 's') {
293b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_SESS;
294b9e2be50SGlenn Strauss         len -= 5;
295b9e2be50SGlenn Strauss     }
296b9e2be50SGlenn Strauss     else {
297b9e2be50SGlenn Strauss         ai->dalgo = HTTP_AUTH_DIGEST_NONE;
298b9e2be50SGlenn Strauss     }
299b9e2be50SGlenn Strauss 
300b9e2be50SGlenn Strauss     if (3 == len
301b9e2be50SGlenn Strauss         && 'm' == (s[0] | 0x20)
302b9e2be50SGlenn Strauss         && 'd' == (s[1] | 0x20)
303b9e2be50SGlenn Strauss         && '5' == (s[2]       )) {
304b9e2be50SGlenn Strauss         ai->dalgo |= HTTP_AUTH_DIGEST_MD5;
305b9e2be50SGlenn Strauss         ai->dlen   = HTTP_AUTH_DIGEST_MD5_BINLEN;
306b9e2be50SGlenn Strauss         return 1;
307b9e2be50SGlenn Strauss     }
308b28a3714SGlenn Strauss   #ifdef USE_LIB_CRYPTO
309b9e2be50SGlenn Strauss     else if (len >= 7
310b9e2be50SGlenn Strauss              && 's' == (s[0] | 0x20)
311b9e2be50SGlenn Strauss              && 'h' == (s[1] | 0x20)
312b9e2be50SGlenn Strauss              && 'a' == (s[2] | 0x20)
313b9e2be50SGlenn Strauss              && '-' == (s[3]       )) {
314b9e2be50SGlenn Strauss         if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') {
315b9e2be50SGlenn Strauss             ai->dalgo |= HTTP_AUTH_DIGEST_SHA256;
316b9e2be50SGlenn Strauss             ai->dlen   = HTTP_AUTH_DIGEST_SHA256_BINLEN;
317b9e2be50SGlenn Strauss             return 1;
318b9e2be50SGlenn Strauss         }
3191fc8a3e1SGlenn Strauss       #ifdef USE_LIB_CRYPTO_SHA512_256
320b9e2be50SGlenn Strauss         if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) {
321b9e2be50SGlenn Strauss             ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256;
322b9e2be50SGlenn Strauss             ai->dlen   = HTTP_AUTH_DIGEST_SHA512_256_BINLEN;
323b9e2be50SGlenn Strauss             return 1;
324b9e2be50SGlenn Strauss         }
325b9e2be50SGlenn Strauss       #endif
326b9e2be50SGlenn Strauss     }
327b9e2be50SGlenn Strauss   #endif
328b9e2be50SGlenn Strauss     return 0; /*(error)*/
329b9e2be50SGlenn Strauss }
330b9e2be50SGlenn Strauss 
331b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) {
332b9e2be50SGlenn Strauss     for (char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) {
333b9e2be50SGlenn Strauss         http_auth_info_t ai;
334b9e2be50SGlenn Strauss         int rc;
335b9e2be50SGlenn Strauss         p = strchr(s, '|');
336b9e2be50SGlenn Strauss         if (p) *p = '\0';
337b9e2be50SGlenn Strauss         rc = mod_auth_algorithm_parse(&ai, s);
338b9e2be50SGlenn Strauss         if (p) *p = '|';
339b9e2be50SGlenn Strauss         if (!rc) return 0;
340b9e2be50SGlenn Strauss         *algorithm |= ai.dalgo;
341b9e2be50SGlenn Strauss     }
342b9e2be50SGlenn Strauss     return 1;
343b9e2be50SGlenn Strauss }
344b9e2be50SGlenn Strauss 
345b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh)
3467ba06c71SGlenn Strauss {
3477ba06c71SGlenn Strauss     /* user=name1|user=name2|group=name3|host=name4 */
3487ba06c71SGlenn Strauss 
3497ba06c71SGlenn Strauss     const char *str = b->ptr;
3507ba06c71SGlenn Strauss     const char *p;
3517ba06c71SGlenn Strauss 
3527ba06c71SGlenn Strauss     if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) {
3537ba06c71SGlenn Strauss         require->valid_user = 1;
3547ba06c71SGlenn Strauss         return 1; /* success */
3557ba06c71SGlenn Strauss     }
3567ba06c71SGlenn Strauss 
3577ba06c71SGlenn Strauss     do {
3587ba06c71SGlenn Strauss         const char *eq;
3597ba06c71SGlenn Strauss         size_t len;
3607ba06c71SGlenn Strauss         p = strchr(str, '|');
3617ba06c71SGlenn Strauss         len = NULL != p ? (size_t)(p - str) : strlen(str);
3627ba06c71SGlenn Strauss         eq = memchr(str, '=', len);
3637ba06c71SGlenn Strauss         if (NULL == eq) {
364b5775b99SGlenn Strauss             log_error(errh, __FILE__, __LINE__,
365010c2894SGlenn Strauss               "error parsing auth.require 'require' field: missing '=' "
366010c2894SGlenn Strauss               "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
367010c2894SGlenn Strauss               "error value: %s error near: %s", b->ptr, str);
3687ba06c71SGlenn Strauss             return 0;
3697ba06c71SGlenn Strauss         }
370b2c18f48SGlenn Strauss         if (eq[1] == '|' || eq[1] == '\0') {
371b5775b99SGlenn Strauss             log_error(errh, __FILE__, __LINE__,
372010c2894SGlenn Strauss               "error parsing auth.require 'require' field: "
373010c2894SGlenn Strauss               "missing token after '=' "
374010c2894SGlenn Strauss               "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
375010c2894SGlenn Strauss               "error value: %s error near: %s", b->ptr, str);
3767ba06c71SGlenn Strauss             return 0;
3777ba06c71SGlenn Strauss         }
3787ba06c71SGlenn Strauss 
3797ba06c71SGlenn Strauss         switch ((int)(eq - str)) {
3807ba06c71SGlenn Strauss           case 4:
3817ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("user"))) {
382810109ccSGlenn Strauss                 /*("user=" is 5)*/
383a03afc90SGlenn Strauss                 array_set_key_value(&require->user, str+5, len-5, CONST_STR_LEN(""));
3847ba06c71SGlenn Strauss                 continue;
3857ba06c71SGlenn Strauss             }
3867ba06c71SGlenn Strauss             else if (0 == memcmp(str, CONST_STR_LEN("host"))) {
387810109ccSGlenn Strauss                 /*("host=" is 5)*/
388a03afc90SGlenn Strauss                 array_set_key_value(&require->host, str+5, len-5, CONST_STR_LEN(""));
389b5775b99SGlenn Strauss                 log_error(errh, __FILE__, __LINE__,
390010c2894SGlenn Strauss                   "warning parsing auth.require 'require' field: "
391010c2894SGlenn Strauss                   "'host' not implemented; field value: %s", b->ptr);
3927ba06c71SGlenn Strauss                 continue;
3937ba06c71SGlenn Strauss             }
3947ba06c71SGlenn Strauss             break; /* to error */
3957ba06c71SGlenn Strauss           case 5:
3967ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("group"))) {
397810109ccSGlenn Strauss                 /*("group=" is 6)*/
398a03afc90SGlenn Strauss                 array_set_key_value(&require->group, str+6, len-6, CONST_STR_LEN(""));
3999c91af0cSGlenn Strauss               #if 0/*(supported by mod_authn_ldap, but not all other backends)*/
400b5775b99SGlenn Strauss                 log_error(errh, __FILE__, __LINE__,
401010c2894SGlenn Strauss                   "warning parsing auth.require 'require' field: "
402010c2894SGlenn Strauss                   "'group' not implemented; field value: %s", b->ptr);
4039c91af0cSGlenn Strauss               #endif
4047ba06c71SGlenn Strauss                 continue;
4057ba06c71SGlenn Strauss             }
4067ba06c71SGlenn Strauss             break; /* to error */
4077ba06c71SGlenn Strauss           case 10:
4087ba06c71SGlenn Strauss             if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) {
409b5775b99SGlenn Strauss                 log_error(errh, __FILE__, __LINE__,
410010c2894SGlenn Strauss                   "error parsing auth.require 'require' field: "
411010c2894SGlenn Strauss                   "valid user can not be combined with other require rules "
412010c2894SGlenn Strauss                   "(expecting \"valid-user\" or "
413010c2894SGlenn Strauss                   "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr);
4147ba06c71SGlenn Strauss                 return 0;
4157ba06c71SGlenn Strauss             }
4167ba06c71SGlenn Strauss             break; /* to error */
4177ba06c71SGlenn Strauss           default:
4187ba06c71SGlenn Strauss             break; /* to error */
4197ba06c71SGlenn Strauss         }
4207ba06c71SGlenn Strauss 
421b5775b99SGlenn Strauss         log_error(errh, __FILE__, __LINE__,
422010c2894SGlenn Strauss           "error parsing auth.require 'require' field: "
423010c2894SGlenn Strauss           "invalid/unsupported token "
424010c2894SGlenn Strauss           "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
425010c2894SGlenn Strauss           "error value: %s error near: %s", b->ptr, str);
4267ba06c71SGlenn Strauss         return 0;
4277ba06c71SGlenn Strauss 
4287ba06c71SGlenn Strauss     } while (p && *((str = p+1)));
4297ba06c71SGlenn Strauss 
4307ba06c71SGlenn Strauss     return 1; /* success */
4317ba06c71SGlenn Strauss }
4327ba06c71SGlenn Strauss 
433b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh)
4348e713130SGlenn Strauss {
4358e713130SGlenn Strauss 		for (uint32_t n = 0; n < value->used; ++n) {
4367ba06c71SGlenn Strauss 			size_t m;
4378e713130SGlenn Strauss 			data_array *da_file = (data_array *)value->data[n];
4387ba06c71SGlenn Strauss 			const buffer *method = NULL, *realm = NULL, *require = NULL;
439e1188e77SGlenn Strauss 			const buffer *nonce_secret = NULL;
4407ba06c71SGlenn Strauss 			const http_auth_scheme_t *auth_scheme;
441b9e2be50SGlenn Strauss 			buffer *algos = NULL;
442b9e2be50SGlenn Strauss 			int algorithm = HTTP_AUTH_DIGEST_SESS;
4437ba06c71SGlenn Strauss 
444c2238256SGlenn Strauss 			if (!array_is_kvstring(&da_file->value)) {
445b5775b99SGlenn Strauss 				log_error(errh, __FILE__, __LINE__,
446010c2894SGlenn Strauss 				  "unexpected value for auth.require; expected "
447bd77abe0SGlenn Strauss 				  "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )");
4487ba06c71SGlenn Strauss 
4497ba06c71SGlenn Strauss 				return HANDLER_ERROR;
4507ba06c71SGlenn Strauss 			}
4517ba06c71SGlenn Strauss 
452c2238256SGlenn Strauss 			for (m = 0; m < da_file->value.used; m++) {
453c2238256SGlenn Strauss 				if (da_file->value.data[m]->type == TYPE_STRING) {
454c2238256SGlenn Strauss 					data_string *ds = (data_string *)da_file->value.data[m];
455ad9b7e00SGlenn Strauss 					if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) {
456601c572cSGlenn Strauss 						method = &ds->value;
457ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) {
458601c572cSGlenn Strauss 						realm = &ds->value;
459ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) {
460601c572cSGlenn Strauss 						require = &ds->value;
461ad9b7e00SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) {
462601c572cSGlenn Strauss 						algos = &ds->value;
46333ae8302SGlenn Strauss 					} else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce_secret"))
46433ae8302SGlenn Strauss 					           || buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce-secret"))) {
465e1188e77SGlenn Strauss 						nonce_secret = &ds->value;
4667ba06c71SGlenn Strauss 					} else {
467b5775b99SGlenn Strauss 						log_error(errh, __FILE__, __LINE__,
468010c2894SGlenn Strauss 						  "the field is unknown in: "
469010c2894SGlenn Strauss 						  "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )",
470010c2894SGlenn Strauss 						  da_file->value.data[m]->key.ptr);
4717ba06c71SGlenn Strauss 
4727ba06c71SGlenn Strauss 						return HANDLER_ERROR;
4737ba06c71SGlenn Strauss 					}
4747ba06c71SGlenn Strauss 				} else {
475b5775b99SGlenn Strauss 					log_error(errh, __FILE__, __LINE__,
476010c2894SGlenn Strauss 					  "a string was expected for: "
477010c2894SGlenn Strauss 					  "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )",
478010c2894SGlenn Strauss 					  da_file->value.data[m]->key.ptr);
4797ba06c71SGlenn Strauss 
4807ba06c71SGlenn Strauss 					return HANDLER_ERROR;
4817ba06c71SGlenn Strauss 				}
4827ba06c71SGlenn Strauss 			}
4837ba06c71SGlenn Strauss 
4847ba06c71SGlenn Strauss 			if (buffer_string_is_empty(method)) {
485b5775b99SGlenn Strauss 				log_error(errh, __FILE__, __LINE__,
486010c2894SGlenn Strauss 				  "the method field is missing or blank in: "
4877ba06c71SGlenn Strauss 				  "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )");
4887ba06c71SGlenn Strauss 				return HANDLER_ERROR;
4897ba06c71SGlenn Strauss 			} else {
4907ba06c71SGlenn Strauss 				auth_scheme = http_auth_scheme_get(method);
4917ba06c71SGlenn Strauss 				if (NULL == auth_scheme) {
492b5775b99SGlenn Strauss 					log_error(errh, __FILE__, __LINE__,
493010c2894SGlenn Strauss 					  "unknown method %s (e.g. \"basic\", \"digest\" or \"extern\") in "
494010c2894SGlenn Strauss 					  "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )", method->ptr);
4957ba06c71SGlenn Strauss 					return HANDLER_ERROR;
4967ba06c71SGlenn Strauss 				}
4977ba06c71SGlenn Strauss 			}
4987ba06c71SGlenn Strauss 
4997ba06c71SGlenn Strauss 			if (buffer_is_empty(realm)) {
500b5775b99SGlenn Strauss 				log_error(errh, __FILE__, __LINE__,
501010c2894SGlenn Strauss 				  "the realm field is missing in: "
5027ba06c71SGlenn Strauss 				  "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )");
5037ba06c71SGlenn Strauss 				return HANDLER_ERROR;
5047ba06c71SGlenn Strauss 			}
5057ba06c71SGlenn Strauss 
5067ba06c71SGlenn Strauss 			if (buffer_string_is_empty(require)) {
507b5775b99SGlenn Strauss 				log_error(errh, __FILE__, __LINE__,
508010c2894SGlenn Strauss 				  "the require field is missing or blank in: "
5097ba06c71SGlenn Strauss 				  "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )");
5107ba06c71SGlenn Strauss 				return HANDLER_ERROR;
5117ba06c71SGlenn Strauss 			}
5127ba06c71SGlenn Strauss 
513b9e2be50SGlenn Strauss 			if (buffer_string_is_empty(algos)) {
514b9e2be50SGlenn Strauss 				algorithm |= HTTP_AUTH_DIGEST_MD5;
515b9e2be50SGlenn Strauss 			} else if (!mod_auth_algorithms_parse(&algorithm, algos)) {
516b5775b99SGlenn Strauss 				log_error(errh, __FILE__, __LINE__,
517010c2894SGlenn Strauss 				  "invalid algorithm in: "
518b9e2be50SGlenn Strauss 				  "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )");
519b9e2be50SGlenn Strauss 				return HANDLER_ERROR;
520b9e2be50SGlenn Strauss 			}
521b9e2be50SGlenn Strauss 
522609e9a50SGlenn Strauss 			if (require) { /*(always true at this point)*/
5237ba06c71SGlenn Strauss 				data_auth * const dauth = data_auth_init();
524ad9b7e00SGlenn Strauss 				buffer_copy_buffer(&dauth->key, &da_file->key);
5257ba06c71SGlenn Strauss 				dauth->require->scheme = auth_scheme;
526b9e2be50SGlenn Strauss 				dauth->require->algorithm = algorithm;
527a03afc90SGlenn Strauss 				dauth->require->realm = realm;
528e1188e77SGlenn Strauss 				dauth->require->nonce_secret = nonce_secret; /*(NULL is ok)*/
529b5775b99SGlenn Strauss 				if (!mod_auth_require_parse(dauth->require, require, errh)) {
5308c7f1dfbSGlenn Strauss 					dauth->fn->free((data_unset *)dauth);
5317ba06c71SGlenn Strauss 					return HANDLER_ERROR;
5327ba06c71SGlenn Strauss 				}
5338e713130SGlenn Strauss 				array_insert_unique(auth_require, (data_unset *)dauth);
5347ba06c71SGlenn Strauss 			}
5357ba06c71SGlenn Strauss 		}
5367ba06c71SGlenn Strauss 
5377ba06c71SGlenn Strauss 	return HANDLER_GO_ON;
5387ba06c71SGlenn Strauss }
5397ba06c71SGlenn Strauss 
5408e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
5418e713130SGlenn Strauss     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
5428e713130SGlenn Strauss       case 0: /* auth.backend */
5438e713130SGlenn Strauss         if (cpv->vtype == T_CONFIG_LOCAL)
5448e713130SGlenn Strauss             pconf->auth_backend = cpv->v.v;
5458e713130SGlenn Strauss         break;
5468e713130SGlenn Strauss       case 1: /* auth.require */
5478e713130SGlenn Strauss         if (cpv->vtype == T_CONFIG_LOCAL)
5488e713130SGlenn Strauss             pconf->auth_require = cpv->v.v;
5498e713130SGlenn Strauss         break;
5508e713130SGlenn Strauss       case 2: /* auth.extern-authn */
5518e713130SGlenn Strauss         pconf->auth_extern_authn = cpv->v.u;
552e11514b0SGlenn Strauss         break;
553e11514b0SGlenn Strauss       case 3: /* auth.cache */
554e11514b0SGlenn Strauss         if (cpv->vtype == T_CONFIG_LOCAL)
555e11514b0SGlenn Strauss             pconf->auth_cache = cpv->v.v;
556e11514b0SGlenn Strauss         break;
5578e713130SGlenn Strauss       default:/* should not happen */
5588e713130SGlenn Strauss         return;
5598e713130SGlenn Strauss     }
5608e713130SGlenn Strauss }
5614a6ce4aeSJan Kneschke 
5628e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
5638e713130SGlenn Strauss     do {
5648e713130SGlenn Strauss         mod_auth_merge_config_cpv(pconf, cpv);
5658e713130SGlenn Strauss     } while ((++cpv)->k_id != -1);
5668e713130SGlenn Strauss }
567bcdc6a3bSJan Kneschke 
5687c7f8c46SGlenn Strauss static void mod_auth_patch_config(request_st * const r, plugin_data * const p) {
569cc2134c8SGlenn Strauss     p->conf = p->defaults; /* copy small struct instead of memcpy() */
570cc2134c8SGlenn Strauss     /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
5718e713130SGlenn Strauss     for (int i = 1, used = p->nconfig; i < used; ++i) {
5727c7f8c46SGlenn Strauss         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
5738e713130SGlenn Strauss             mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
5748e713130SGlenn Strauss     }
5758e713130SGlenn Strauss }
57636f64b26SGlenn Strauss 
5778e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) {
5788e713130SGlenn Strauss     static const config_plugin_keys_t cpk[] = {
5798e713130SGlenn Strauss       { CONST_STR_LEN("auth.backend"),
5808e713130SGlenn Strauss         T_CONFIG_STRING,
5818e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
5828e713130SGlenn Strauss      ,{ CONST_STR_LEN("auth.require"),
58303b4c993SGlenn Strauss         T_CONFIG_ARRAY_KVARRAY,
5848e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
5858e713130SGlenn Strauss      ,{ CONST_STR_LEN("auth.extern-authn"),
5868e713130SGlenn Strauss         T_CONFIG_BOOL,
5878e713130SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
588e11514b0SGlenn Strauss      ,{ CONST_STR_LEN("auth.cache"),
589e11514b0SGlenn Strauss         T_CONFIG_ARRAY,
590e11514b0SGlenn Strauss         T_CONFIG_SCOPE_CONNECTION }
5918e713130SGlenn Strauss      ,{ NULL, 0,
5928e713130SGlenn Strauss         T_CONFIG_UNSET,
5938e713130SGlenn Strauss         T_CONFIG_SCOPE_UNSET }
5948e713130SGlenn Strauss     };
595bcdc6a3bSJan Kneschke 
5968e713130SGlenn Strauss     plugin_data * const p = p_d;
5978e713130SGlenn Strauss     if (!config_plugin_values_init(srv, p, cpk, "mod_auth"))
5988e713130SGlenn Strauss         return HANDLER_ERROR;
599bcdc6a3bSJan Kneschke 
6008e713130SGlenn Strauss     /* process and validate config directives
6018e713130SGlenn Strauss      * (init i to 0 if global context; to 1 to skip empty global context) */
6028e713130SGlenn Strauss     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
6038e713130SGlenn Strauss         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
6048e713130SGlenn Strauss         for (; -1 != cpv->k_id; ++cpv) {
6058e713130SGlenn Strauss             switch (cpv->k_id) {
6068e713130SGlenn Strauss               case 0: /* auth.backend */
6078e713130SGlenn Strauss                 if (!buffer_string_is_empty(cpv->v.b)) {
6088e713130SGlenn Strauss                     const http_auth_backend_t * const auth_backend =
6098e713130SGlenn Strauss                       http_auth_backend_get(cpv->v.b);
6108e713130SGlenn Strauss                     if (NULL == auth_backend) {
6118e713130SGlenn Strauss                         log_error(srv->errh, __FILE__, __LINE__,
6128e713130SGlenn Strauss                           "auth.backend not supported: %s", cpv->v.b->ptr);
6138e713130SGlenn Strauss                         return HANDLER_ERROR;
6148e713130SGlenn Strauss                     }
6158e713130SGlenn Strauss                     *(const http_auth_backend_t **)&cpv->v.v = auth_backend;
6168e713130SGlenn Strauss                     cpv->vtype = T_CONFIG_LOCAL;
6178e713130SGlenn Strauss                 }
6188e713130SGlenn Strauss                 break;
6198e713130SGlenn Strauss               case 1: /* auth.require */
62003b4c993SGlenn Strauss                 {
62124680a91SGlenn Strauss                     array * const a = array_init(4);
6228e713130SGlenn Strauss                     if (HANDLER_GO_ON !=
623b5775b99SGlenn Strauss                         mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) {
6248e713130SGlenn Strauss                         array_free(a);
6258e713130SGlenn Strauss                         return HANDLER_ERROR;
6268e713130SGlenn Strauss                     }
6278e713130SGlenn Strauss                     cpv->v.a = a;
6288e713130SGlenn Strauss                     cpv->vtype = T_CONFIG_LOCAL;
6298e713130SGlenn Strauss                 }
6308e713130SGlenn Strauss                 break;
6318e713130SGlenn Strauss               case 2: /* auth.extern-authn */
6328e713130SGlenn Strauss                 break;
633e11514b0SGlenn Strauss               case 3: /* auth.cache */
634e11514b0SGlenn Strauss                 cpv->v.v = http_auth_cache_init(cpv->v.a);
635e11514b0SGlenn Strauss                 cpv->vtype = T_CONFIG_LOCAL;
636e11514b0SGlenn Strauss                 break;
6378e713130SGlenn Strauss               default:/* should not happen */
6388e713130SGlenn Strauss                 break;
639bcdc6a3bSJan Kneschke             }
640bcdc6a3bSJan Kneschke         }
641bcdc6a3bSJan Kneschke     }
642bcdc6a3bSJan Kneschke 
6438e713130SGlenn Strauss     /* initialize p->defaults from global config context */
6448e713130SGlenn Strauss     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
6458e713130SGlenn Strauss         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
6468e713130SGlenn Strauss         if (-1 != cpv->k_id)
6478e713130SGlenn Strauss             mod_auth_merge_config(&p->defaults, cpv);
648bcdc6a3bSJan Kneschke     }
6498e713130SGlenn Strauss 
6508e713130SGlenn Strauss     return HANDLER_GO_ON;
6518e713130SGlenn Strauss }
652bcdc6a3bSJan Kneschke 
6537c7f8c46SGlenn Strauss static handler_t mod_auth_uri_handler(request_st * const r, void *p_d) {
6547ba06c71SGlenn Strauss 	plugin_data *p = p_d;
655d61f3381SGlenn Strauss 	data_auth *dauth;
65681b2d1f0SGlenn Strauss 
6577c7f8c46SGlenn Strauss 	mod_auth_patch_config(r, p);
65881b2d1f0SGlenn Strauss 
6597ba06c71SGlenn Strauss 	if (p->conf.auth_require == NULL) return HANDLER_GO_ON;
66081b2d1f0SGlenn Strauss 
6617ba06c71SGlenn Strauss 	/* search auth directives for first prefix match against URL path */
6627ba06c71SGlenn Strauss 	/* if we have a case-insensitive FS we have to lower-case the URI here too */
6637c7f8c46SGlenn Strauss 	dauth = (!r->conf.force_lowercase_filenames)
6647c7f8c46SGlenn Strauss 	   ? (data_auth *)array_match_key_prefix(p->conf.auth_require, &r->uri.path)
6657c7f8c46SGlenn Strauss 	   : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, &r->uri.path);
666d61f3381SGlenn Strauss 	if (NULL == dauth) return HANDLER_GO_ON;
6677ba06c71SGlenn Strauss 
668d61f3381SGlenn Strauss 	{
6697ba06c71SGlenn Strauss 			const http_auth_scheme_t * const scheme = dauth->require->scheme;
670f54d628cSGlenn Strauss 			if (p->conf.auth_extern_authn) {
6717c7f8c46SGlenn Strauss 				const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER"));
6723dd3cde9SGlenn Strauss 				if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) {
673f54d628cSGlenn Strauss 					return HANDLER_GO_ON;
674f54d628cSGlenn Strauss 				}
675f54d628cSGlenn Strauss 			}
6767c7f8c46SGlenn Strauss 			return scheme->checkfn(r, scheme->p_d, dauth->require, p->conf.auth_backend);
6777ba06c71SGlenn Strauss 	}
6787ba06c71SGlenn Strauss }
6797ba06c71SGlenn Strauss 
680e11514b0SGlenn Strauss 
6817ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p);
6827ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) {
6837ba06c71SGlenn Strauss 	p->version     = LIGHTTPD_VERSION_ID;
684e2de4e58SGlenn Strauss 	p->name        = "auth";
6857ba06c71SGlenn Strauss 	p->init        = mod_auth_init;
6867ba06c71SGlenn Strauss 	p->set_defaults = mod_auth_set_defaults;
687e11514b0SGlenn Strauss 	p->handle_trigger = mod_auth_periodic;
6887ba06c71SGlenn Strauss 	p->handle_uri_clean = mod_auth_uri_handler;
6897ba06c71SGlenn Strauss 	p->cleanup     = mod_auth_free;
6907ba06c71SGlenn Strauss 
69181b2d1f0SGlenn Strauss 	return 0;
69281b2d1f0SGlenn Strauss }
69381b2d1f0SGlenn Strauss 
69481b2d1f0SGlenn Strauss 
69581b2d1f0SGlenn Strauss 
69681b2d1f0SGlenn Strauss 
6977ba06c71SGlenn Strauss /*
6987ba06c71SGlenn Strauss  * auth schemes (basic, digest, extern)
6997ba06c71SGlenn Strauss  *
7007ba06c71SGlenn Strauss  * (could be in separate file from mod_auth.c as long as registration occurs)
7017ba06c71SGlenn Strauss  */
70281b2d1f0SGlenn Strauss 
7031fc8a3e1SGlenn Strauss #include "sys-crypto-md.h"
7047ba06c71SGlenn Strauss #include "base64.h"
7057f4e156eSGlenn Strauss #include "rand.h"
7063dd3cde9SGlenn Strauss #include "http_header.h"
70781b2d1f0SGlenn Strauss 
708010c2894SGlenn Strauss __attribute_cold__
7097c7f8c46SGlenn Strauss static handler_t mod_auth_send_400_bad_request(request_st * const r) {
7104b3a91e6SGlenn Strauss 
7114b3a91e6SGlenn Strauss 	/* a field was missing or invalid */
7127c7f8c46SGlenn Strauss 	r->http_status = 400; /* Bad Request */
7137c7f8c46SGlenn Strauss 	r->handler_module = NULL;
7144b3a91e6SGlenn Strauss 
7154b3a91e6SGlenn Strauss 	return HANDLER_FINISHED;
7164b3a91e6SGlenn Strauss }
7174b3a91e6SGlenn Strauss 
7187c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(request_st * const r, const buffer * const realm) {
7197c7f8c46SGlenn Strauss 	r->http_status = 401;
7207c7f8c46SGlenn Strauss 	r->handler_module = NULL;
7214b3a91e6SGlenn Strauss 
7227c7f8c46SGlenn Strauss 	buffer * const tb = r->tmp_buf;
723010c2894SGlenn Strauss 	buffer_copy_string_len(tb, CONST_STR_LEN("Basic realm=\""));
724010c2894SGlenn Strauss 	buffer_append_string_buffer(tb, realm);
725010c2894SGlenn Strauss 	buffer_append_string_len(tb, CONST_STR_LEN("\", charset=\"UTF-8\""));
7264b3a91e6SGlenn Strauss 
727367f30a6SGlenn Strauss 	http_header_response_set(r, HTTP_HEADER_WWW_AUTHENTICATE,
728367f30a6SGlenn Strauss 	                         CONST_STR_LEN("WWW-Authenticate"),
729367f30a6SGlenn Strauss 	                         CONST_BUF_LEN(tb));
7304b3a91e6SGlenn Strauss 
7314b3a91e6SGlenn Strauss 	return HANDLER_FINISHED;
7324b3a91e6SGlenn Strauss }
7334b3a91e6SGlenn Strauss 
7347c7f8c46SGlenn Strauss static handler_t mod_auth_check_basic(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) {
7357c7f8c46SGlenn Strauss 	const buffer *b = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization"));
73681b2d1f0SGlenn Strauss 	buffer *username;
73781b2d1f0SGlenn Strauss 	char *pw;
7387ba06c71SGlenn Strauss 	handler_t rc = HANDLER_UNSET;
73981b2d1f0SGlenn Strauss 
740cf0098eaSGlenn Strauss 	if (NULL == backend || NULL == backend->basic) {
741cf0098eaSGlenn Strauss 		if (NULL == backend)
742cf0098eaSGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
743cf0098eaSGlenn Strauss 			  "auth.backend not configured for %s", r->uri.path.ptr);
744cf0098eaSGlenn Strauss 		else
745cf0098eaSGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
746cf0098eaSGlenn Strauss 			  "auth.require \"method\" => \"basic\" invalid "
747cf0098eaSGlenn Strauss 			  "(try \"digest\"?) for %s",
748cf0098eaSGlenn Strauss 			  r->uri.path.ptr);
7497c7f8c46SGlenn Strauss 		r->http_status = 500;
7507c7f8c46SGlenn Strauss 		r->handler_module = NULL;
7517ba06c71SGlenn Strauss 		return HANDLER_FINISHED;
7527ba06c71SGlenn Strauss 	}
7537ba06c71SGlenn Strauss 
7543dd3cde9SGlenn Strauss 	if (NULL == b) {
7557c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_basic(r, require->realm);
7567ba06c71SGlenn Strauss 	}
7577ba06c71SGlenn Strauss 
75813008156SGlenn Strauss 	if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) {
7597c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_basic(r, require->realm);
7604b3a91e6SGlenn Strauss 	}
7619bc61f16SGlenn Strauss       #ifdef __COVERITY__
7623dd3cde9SGlenn Strauss 	if (buffer_string_length(b) < sizeof("Basic ")-1) {
7637c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
7649bc61f16SGlenn Strauss 	}
7659bc61f16SGlenn Strauss       #endif
76681b2d1f0SGlenn Strauss 
76781b2d1f0SGlenn Strauss 	username = buffer_init();
76881b2d1f0SGlenn Strauss 
7698047c2f4SGlenn Strauss 	/* coverity[overflow_sink : FALSE] */
7707ba06c71SGlenn Strauss 	if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_string_length(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) {
7717c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__, "decoding base64-string failed %s", b->ptr+sizeof("Basic ")-1);
77281b2d1f0SGlenn Strauss 
77381b2d1f0SGlenn Strauss 		buffer_free(username);
7747c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
77581b2d1f0SGlenn Strauss 	}
77681b2d1f0SGlenn Strauss 
77781b2d1f0SGlenn Strauss 	/* r2 == user:password */
77881b2d1f0SGlenn Strauss 	if (NULL == (pw = strchr(username->ptr, ':'))) {
7797c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__, "missing ':' in %s", username->ptr);
78081b2d1f0SGlenn Strauss 
78181b2d1f0SGlenn Strauss 		buffer_free(username);
7827c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
78381b2d1f0SGlenn Strauss 	}
78481b2d1f0SGlenn Strauss 
785e11514b0SGlenn Strauss 	uint32_t pwlen = buffer_string_length(username);
78681b2d1f0SGlenn Strauss 	buffer_string_set_length(username, pw - username->ptr);
78781b2d1f0SGlenn Strauss 	pw++;
788e11514b0SGlenn Strauss 	pwlen -= (pw - username->ptr);
78981b2d1f0SGlenn Strauss 
790e11514b0SGlenn Strauss 	plugin_data * const p = p_d;
791e11514b0SGlenn Strauss 	splay_tree ** sptree = p->conf.auth_cache
792e11514b0SGlenn Strauss 	  ? &p->conf.auth_cache->sptree
793e11514b0SGlenn Strauss 	  : NULL;
794e11514b0SGlenn Strauss 	http_auth_cache_entry *ae = NULL;
795e11514b0SGlenn Strauss 	int ndx = -1;
796e11514b0SGlenn Strauss 	if (sptree) {
797e11514b0SGlenn Strauss 		ndx = http_auth_cache_hash(require, CONST_BUF_LEN(username));
798e11514b0SGlenn Strauss 		ae = http_auth_cache_query(sptree, ndx);
799e11514b0SGlenn Strauss 		if (ae && ae->require == require
800e11514b0SGlenn Strauss 		    && buffer_is_equal_string(username, ae->username, ae->ulen))
801e11514b0SGlenn Strauss 			rc = http_auth_const_time_memeq_pad(ae->pwdigest, ae->dlen,
802e11514b0SGlenn Strauss 			                                    pw, pwlen)
803e11514b0SGlenn Strauss 			  ? HANDLER_GO_ON
804e11514b0SGlenn Strauss 			  : HANDLER_ERROR;
805e11514b0SGlenn Strauss 		else /*(not found or hash collision)*/
806e11514b0SGlenn Strauss 			ae = NULL;
807e11514b0SGlenn Strauss 	}
808e11514b0SGlenn Strauss 
809e11514b0SGlenn Strauss 	if (NULL == ae) /* (HANDLER_UNSET == rc) */
8107c7f8c46SGlenn Strauss 		rc = backend->basic(r, backend->p_d, require, username, pw);
811e11514b0SGlenn Strauss 
8127ba06c71SGlenn Strauss 	switch (rc) {
8134b3a91e6SGlenn Strauss 	case HANDLER_GO_ON:
8147c7f8c46SGlenn Strauss 		http_auth_setenv(r, CONST_BUF_LEN(username), CONST_STR_LEN("Basic"));
815e11514b0SGlenn Strauss 		if (sptree && NULL == ae) { /*(cache (new) successful result)*/
816e11514b0SGlenn Strauss 			ae = http_auth_cache_entry_init(require, 0, CONST_BUF_LEN(username),
817e11514b0SGlenn Strauss 			                                pw, pwlen);
818e11514b0SGlenn Strauss 			http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free);
819e11514b0SGlenn Strauss 		}
8204b3a91e6SGlenn Strauss 		break;
8214b3a91e6SGlenn Strauss 	case HANDLER_WAIT_FOR_EVENT:
8224b3a91e6SGlenn Strauss 	case HANDLER_FINISHED:
8237ba06c71SGlenn Strauss 		break;
8244b3a91e6SGlenn Strauss 	case HANDLER_ERROR:
8254b3a91e6SGlenn Strauss 	default:
8267c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
827010c2894SGlenn Strauss 		  "password doesn't match for %s username: %s IP: %s",
8287c7f8c46SGlenn Strauss 		  r->uri.path.ptr, username->ptr, r->con->dst_addr_buf->ptr);
8297c7f8c46SGlenn Strauss 		r->keep_alive = 0; /*(disable keep-alive if bad password)*/
8307ba06c71SGlenn Strauss 		rc = HANDLER_UNSET;
8317ba06c71SGlenn Strauss 		break;
83281b2d1f0SGlenn Strauss 	}
83381b2d1f0SGlenn Strauss 
8344b3a91e6SGlenn Strauss 	buffer_free(username);
8357c7f8c46SGlenn Strauss 	return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(r, require->realm);
83681b2d1f0SGlenn Strauss }
83781b2d1f0SGlenn Strauss 
83881b2d1f0SGlenn Strauss 
839b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO
840b9e2be50SGlenn Strauss 
841b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_sha256(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) {
842b9e2be50SGlenn Strauss     SHA256_CTX ctx;
843b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
844b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
845b9e2be50SGlenn Strauss 
846b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
847b9e2be50SGlenn Strauss         SHA256_Init(&ctx);
848b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
849b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
850b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
851b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
852b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
853b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
854b9e2be50SGlenn Strauss         SHA256_Final(ai->digest, &ctx);
855b9e2be50SGlenn Strauss     }
856b9e2be50SGlenn Strauss 
857b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
858b9e2be50SGlenn Strauss 
859b9e2be50SGlenn Strauss     /* calculate H(A2) */
860b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
861b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)m, strlen(m));
862b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
863b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri));
864b9e2be50SGlenn Strauss   #if 0
865b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
866fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
867b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
868b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2);
869b9e2be50SGlenn Strauss     }
870b9e2be50SGlenn Strauss   #endif
871b9e2be50SGlenn Strauss     SHA256_Final(ai->digest, &ctx);
872b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
873b9e2be50SGlenn Strauss 
874b9e2be50SGlenn Strauss     /* calculate response */
875b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
876b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
877b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
878b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
879b9e2be50SGlenn Strauss     SHA256_Update(&ctx, CONST_STR_LEN(":"));
880b9e2be50SGlenn Strauss     if (qop && *qop) {
881b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc));
882b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
883b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
884b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
885b9e2be50SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop));
886b9e2be50SGlenn Strauss         SHA256_Update(&ctx, CONST_STR_LEN(":"));
887b9e2be50SGlenn Strauss     }
888b9e2be50SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
889b9e2be50SGlenn Strauss     SHA256_Final(ai->digest, &ctx);
890b9e2be50SGlenn Strauss }
891b9e2be50SGlenn Strauss 
892e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) {
893b9e2be50SGlenn Strauss     SHA256_CTX ctx;
894f24e6d69SGlenn Strauss     size_t len;
895b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN];
896b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1];
897b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
898b9e2be50SGlenn Strauss     SHA256_Init(&ctx);
899f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
900f24e6d69SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)hh, len);
901f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
902f24e6d69SGlenn Strauss     SHA256_Update(&ctx, (unsigned char *)hh, len);
903e1188e77SGlenn Strauss     if (secret) {
904e1188e77SGlenn Strauss         len = buffer_string_length(secret);
905e1188e77SGlenn Strauss         SHA256_Update(&ctx, (unsigned char *)secret->ptr, len);
906e1188e77SGlenn Strauss     }
907b9e2be50SGlenn Strauss     SHA256_Final(h, &ctx);
908b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
909b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
910b9e2be50SGlenn Strauss }
911b9e2be50SGlenn Strauss 
9121fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256
913b9e2be50SGlenn Strauss 
914b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_sha512_256(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) {
915b9e2be50SGlenn Strauss     SHA512_CTX ctx;
916b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
917b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
918b9e2be50SGlenn Strauss 
919b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
920b9e2be50SGlenn Strauss         SHA512_256_Init(&ctx);
921b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
922b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
923b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
924b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
925b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
926b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
927b9e2be50SGlenn Strauss         SHA512_256_Final(ai->digest, &ctx);
928b9e2be50SGlenn Strauss     }
929b9e2be50SGlenn Strauss 
930b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
931b9e2be50SGlenn Strauss 
932b9e2be50SGlenn Strauss     /* calculate H(A2) */
933b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
934b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m));
935b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
936b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri));
937b9e2be50SGlenn Strauss   #if 0
938b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
939fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
940b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
941b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2);
942b9e2be50SGlenn Strauss     }
943b9e2be50SGlenn Strauss   #endif
944b9e2be50SGlenn Strauss     SHA512_256_Final(ai->digest, &ctx);
945b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
946b9e2be50SGlenn Strauss 
947b9e2be50SGlenn Strauss     /* calculate response */
948b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
949b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
950b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
951b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
952b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
953b9e2be50SGlenn Strauss     if (qop && *qop) {
954b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc));
955b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
956b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
957b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
958b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop));
959b9e2be50SGlenn Strauss         SHA512_256_Update(&ctx, CONST_STR_LEN(":"));
960b9e2be50SGlenn Strauss     }
961b9e2be50SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
962b9e2be50SGlenn Strauss     SHA512_256_Final(ai->digest, &ctx);
963b9e2be50SGlenn Strauss }
964b9e2be50SGlenn Strauss 
965e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) {
966b9e2be50SGlenn Strauss     SHA512_CTX ctx;
967f24e6d69SGlenn Strauss     size_t len;
968b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN];
969b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1];
970b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
971b9e2be50SGlenn Strauss     SHA512_256_Init(&ctx);
972f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
973f24e6d69SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)hh, len);
974f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
975f24e6d69SGlenn Strauss     SHA512_256_Update(&ctx, (unsigned char *)hh, len);
976e1188e77SGlenn Strauss     if (secret) {
977e1188e77SGlenn Strauss         len = buffer_string_length(secret);
978e1188e77SGlenn Strauss         SHA512_256_Update(&ctx, (unsigned char *)secret->ptr, len);
979e1188e77SGlenn Strauss     }
980b9e2be50SGlenn Strauss     SHA512_256_Final(h, &ctx);
981b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
982b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
983b9e2be50SGlenn Strauss }
984b9e2be50SGlenn Strauss 
9851fc8a3e1SGlenn Strauss #endif /* USE_LIB_CRYPTO_SHA512_256 */
986b9e2be50SGlenn Strauss 
987b28a3714SGlenn Strauss #endif /* USE_LIB_CRYPTO */
988b9e2be50SGlenn Strauss 
989b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_md5(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) {
990b9e2be50SGlenn Strauss     li_MD5_CTX ctx;
991b9e2be50SGlenn Strauss     char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
992b9e2be50SGlenn Strauss     char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
993b9e2be50SGlenn Strauss 
994b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
995b9e2be50SGlenn Strauss         li_MD5_Init(&ctx);
996b9e2be50SGlenn Strauss         /* http://www.rfc-editor.org/errata_search.php?rfc=2617
997b9e2be50SGlenn Strauss          * Errata ID: 1649 */
998b9e2be50SGlenn Strauss         li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
999b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
1000b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1001b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
1002b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1003b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
1004b9e2be50SGlenn Strauss         li_MD5_Final(ai->digest, &ctx);
1005b9e2be50SGlenn Strauss     }
1006b9e2be50SGlenn Strauss 
1007b9e2be50SGlenn Strauss     li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
1008b9e2be50SGlenn Strauss 
1009b9e2be50SGlenn Strauss     /* calculate H(A2) */
1010b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
1011b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)m, strlen(m));
1012b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1013b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri));
1014b9e2be50SGlenn Strauss   #if 0
1015b9e2be50SGlenn Strauss     /* qop=auth-int not supported, already checked in caller */
1016fc42ec12SGlenn Strauss     if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
1017b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1018b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2);
1019b9e2be50SGlenn Strauss     }
1020b9e2be50SGlenn Strauss   #endif
1021b9e2be50SGlenn Strauss     li_MD5_Final(ai->digest, &ctx);
1022b9e2be50SGlenn Strauss     li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
1023b9e2be50SGlenn Strauss 
1024b9e2be50SGlenn Strauss     /* calculate response */
1025b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
1026b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1);
1027b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1028b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce));
1029b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1030b9e2be50SGlenn Strauss     if (qop && *qop) {
1031b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc));
1032b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1033b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce));
1034b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1035b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop));
1036b9e2be50SGlenn Strauss         li_MD5_Update(&ctx, CONST_STR_LEN(":"));
1037b9e2be50SGlenn Strauss     }
1038b9e2be50SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1);
1039b9e2be50SGlenn Strauss     li_MD5_Final(ai->digest, &ctx);
1040b9e2be50SGlenn Strauss }
1041b9e2be50SGlenn Strauss 
1042e1188e77SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd, const buffer *secret) {
1043b9e2be50SGlenn Strauss     li_MD5_CTX ctx;
1044f24e6d69SGlenn Strauss     size_t len;
1045b9e2be50SGlenn Strauss     unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN];
1046b9e2be50SGlenn Strauss     char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1];
1047b9e2be50SGlenn Strauss     force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH);
1048b9e2be50SGlenn Strauss     li_MD5_Init(&ctx);
1049f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), cur_ts);
1050f24e6d69SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)hh, len);
1051f24e6d69SGlenn Strauss     len = li_itostrn(hh, sizeof(hh), rnd);
1052f24e6d69SGlenn Strauss     li_MD5_Update(&ctx, (unsigned char *)hh, len);
1053e1188e77SGlenn Strauss     if (secret) {
1054e1188e77SGlenn Strauss         len = buffer_string_length(secret);
1055e1188e77SGlenn Strauss         li_MD5_Update(&ctx, (unsigned char *)secret->ptr, len);
1056e1188e77SGlenn Strauss     }
1057b9e2be50SGlenn Strauss     li_MD5_Final(h, &ctx);
1058b9e2be50SGlenn Strauss     li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h));
1059b9e2be50SGlenn Strauss     buffer_append_string_len(b, hh, sizeof(hh)-1);
1060b9e2be50SGlenn Strauss }
1061b9e2be50SGlenn Strauss 
1062b9e2be50SGlenn Strauss static void mod_auth_digest_mutate(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) {
1063b9e2be50SGlenn Strauss     if (ai->dalgo & HTTP_AUTH_DIGEST_MD5)
1064b9e2be50SGlenn Strauss         mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop);
1065b28a3714SGlenn Strauss   #ifdef USE_LIB_CRYPTO
1066b9e2be50SGlenn Strauss     else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256)
1067b9e2be50SGlenn Strauss         mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop);
10681fc8a3e1SGlenn Strauss    #ifdef USE_LIB_CRYPTO_SHA512_256
1069b9e2be50SGlenn Strauss     else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256)
1070b9e2be50SGlenn Strauss         mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop);
1071b9e2be50SGlenn Strauss    #endif
1072b9e2be50SGlenn Strauss   #endif
1073b9e2be50SGlenn Strauss }
1074b9e2be50SGlenn Strauss 
1075e1188e77SGlenn Strauss static void mod_auth_append_nonce(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo, int *rndptr) {
1076e1188e77SGlenn Strauss     buffer_append_uint_hex(b, (uintmax_t)cur_ts);
1077e1188e77SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN(":"));
1078e1188e77SGlenn Strauss     const buffer * const nonce_secret = require->nonce_secret;
1079e1188e77SGlenn Strauss     int rnd;
1080e1188e77SGlenn Strauss     if (NULL == nonce_secret)
1081e1188e77SGlenn Strauss         rnd = rndptr ? *rndptr : li_rand_pseudo();
1082e1188e77SGlenn Strauss     else { /*(do not directly expose random number generator single value)*/
1083e1188e77SGlenn Strauss         rndptr
1084e1188e77SGlenn Strauss           ? (void)(rnd = *rndptr)
1085e1188e77SGlenn Strauss           : li_rand_pseudo_bytes((unsigned char *)&rnd, sizeof(rnd));
1086e1188e77SGlenn Strauss         buffer_append_uint_hex(b, (uintmax_t)rnd);
1087e1188e77SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN(":"));
1088e1188e77SGlenn Strauss     }
1089e1188e77SGlenn Strauss     switch (dalgo) {
1090b28a3714SGlenn Strauss      #ifdef USE_LIB_CRYPTO
10911fc8a3e1SGlenn Strauss       #ifdef USE_LIB_CRYPTO_SHA512_256
1092e1188e77SGlenn Strauss       case HTTP_AUTH_DIGEST_SHA512_256:
1093e1188e77SGlenn Strauss         mod_auth_digest_nonce_sha512_256(b, cur_ts, rnd, nonce_secret);
1094e1188e77SGlenn Strauss         break;
1095e1188e77SGlenn Strauss       #endif
1096e1188e77SGlenn Strauss       case HTTP_AUTH_DIGEST_SHA256:
1097e1188e77SGlenn Strauss         mod_auth_digest_nonce_sha256(b, cur_ts, rnd, nonce_secret);
1098e1188e77SGlenn Strauss         break;
1099e1188e77SGlenn Strauss      #endif
1100e1188e77SGlenn Strauss       /*case HTTP_AUTH_DIGEST_MD5:*/
1101e1188e77SGlenn Strauss       default:
1102e1188e77SGlenn Strauss         mod_auth_digest_nonce_md5(b, cur_ts, rnd, nonce_secret);
1103e1188e77SGlenn Strauss         break;
1104e1188e77SGlenn Strauss     }
1105e1188e77SGlenn Strauss }
1106e1188e77SGlenn Strauss 
1107b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) {
1108b9e2be50SGlenn Strauss     int algos = nonce_stale ? nonce_stale : require->algorithm;
1109b9e2be50SGlenn Strauss     int n = 0;
1110e1188e77SGlenn Strauss     int algoid[3];
1111b9e2be50SGlenn Strauss     unsigned int algolen[3];
1112b9e2be50SGlenn Strauss     const char *algoname[3];
1113b28a3714SGlenn Strauss   #ifdef USE_LIB_CRYPTO
11141fc8a3e1SGlenn Strauss    #ifdef USE_LIB_CRYPTO_SHA512_256
1115b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_SHA512_256) {
1116e1188e77SGlenn Strauss         algoid[n] = HTTP_AUTH_DIGEST_SHA512_256;
1117b9e2be50SGlenn Strauss         algoname[n] = "SHA-512-256";
1118b9e2be50SGlenn Strauss         algolen[n] = sizeof("SHA-512-256")-1;
1119b9e2be50SGlenn Strauss         ++n;
1120b9e2be50SGlenn Strauss     }
1121b9e2be50SGlenn Strauss    #endif
1122b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_SHA256) {
1123e1188e77SGlenn Strauss         algoid[n] = HTTP_AUTH_DIGEST_SHA256;
1124b9e2be50SGlenn Strauss         algoname[n] = "SHA-256";
1125b9e2be50SGlenn Strauss         algolen[n] = sizeof("SHA-256")-1;
1126b9e2be50SGlenn Strauss         ++n;
1127b9e2be50SGlenn Strauss     }
1128b9e2be50SGlenn Strauss   #endif
1129b9e2be50SGlenn Strauss     if (algos & HTTP_AUTH_DIGEST_MD5) {
1130e1188e77SGlenn Strauss         algoid[n] = HTTP_AUTH_DIGEST_MD5;
1131b9e2be50SGlenn Strauss         algoname[n] = "MD5";
1132b9e2be50SGlenn Strauss         algolen[n] = sizeof("MD5")-1;
1133b9e2be50SGlenn Strauss         ++n;
1134b9e2be50SGlenn Strauss     }
1135b9e2be50SGlenn Strauss 
1136b9e2be50SGlenn Strauss     buffer_clear(b);
1137b9e2be50SGlenn Strauss     for (int i = 0; i < n; ++i) {
1138b9e2be50SGlenn Strauss         if (i > 0) {
1139b9e2be50SGlenn Strauss             buffer_append_string_len(b,CONST_STR_LEN("\r\nWWW-Authenticate: "));
1140b9e2be50SGlenn Strauss         }
1141b9e2be50SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("Digest realm=\""));
1142b9e2be50SGlenn Strauss         buffer_append_string_buffer(b, require->realm);
11431799e4c3SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("\", charset=\"UTF-8\", algorithm="));
1144b9e2be50SGlenn Strauss         buffer_append_string_len(b, algoname[i], algolen[i]);
11451799e4c3SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN(", nonce=\""));
1146e1188e77SGlenn Strauss         mod_auth_append_nonce(b, cur_ts, require, algoid[i], NULL);
1147b9e2be50SGlenn Strauss         buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\""));
1148b9e2be50SGlenn Strauss         if (nonce_stale) {
1149b9e2be50SGlenn Strauss             buffer_append_string_len(b, CONST_STR_LEN(", stale=true"));
1150b9e2be50SGlenn Strauss         }
1151b9e2be50SGlenn Strauss     }
115281b2d1f0SGlenn Strauss }
115381b2d1f0SGlenn Strauss 
1154e1188e77SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo) {
11556ad325c6SGlenn Strauss     buffer_clear(b);
11566ad325c6SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\""));
1157e1188e77SGlenn Strauss     mod_auth_append_nonce(b, cur_ts, require, dalgo, NULL);
11586ad325c6SGlenn Strauss     buffer_append_string_len(b, CONST_STR_LEN("\""));
11596ad325c6SGlenn Strauss }
11606ad325c6SGlenn Strauss 
116181b2d1f0SGlenn Strauss typedef struct {
116281b2d1f0SGlenn Strauss 	const char *key;
116381b2d1f0SGlenn Strauss 	int key_len;
116481b2d1f0SGlenn Strauss 	char **ptr;
116581b2d1f0SGlenn Strauss } digest_kv;
116681b2d1f0SGlenn Strauss 
11677c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st *r, const struct http_auth_require_t *require, int nonce_stale);
11687ba06c71SGlenn Strauss 
11697c7f8c46SGlenn Strauss static handler_t mod_auth_check_digest(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) {
11707c7f8c46SGlenn Strauss 	const buffer *vb = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization"));
11717ba06c71SGlenn Strauss 
117281b2d1f0SGlenn Strauss 	char *username = NULL;
117381b2d1f0SGlenn Strauss 	char *realm = NULL;
117481b2d1f0SGlenn Strauss 	char *nonce = NULL;
117581b2d1f0SGlenn Strauss 	char *uri = NULL;
117681b2d1f0SGlenn Strauss 	char *algorithm = NULL;
117781b2d1f0SGlenn Strauss 	char *qop = NULL;
117881b2d1f0SGlenn Strauss 	char *cnonce = NULL;
117981b2d1f0SGlenn Strauss 	char *nc = NULL;
118081b2d1f0SGlenn Strauss 	char *respons = NULL;
118181b2d1f0SGlenn Strauss 
118281b2d1f0SGlenn Strauss 	char *e, *c;
118381b2d1f0SGlenn Strauss 	int i;
118481b2d1f0SGlenn Strauss 	buffer *b;
118560f4cf3aSGlenn Strauss 	http_auth_info_t ai;
1186b9e2be50SGlenn Strauss 	unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN];
118781b2d1f0SGlenn Strauss 
118881b2d1f0SGlenn Strauss 
118981b2d1f0SGlenn Strauss 	/* init pointers */
119081b2d1f0SGlenn Strauss #define S(x) \
119181b2d1f0SGlenn Strauss 	x, sizeof(x)-1, NULL
119281b2d1f0SGlenn Strauss 	digest_kv dkv[10] = {
119381b2d1f0SGlenn Strauss 		{ S("username=") },
119481b2d1f0SGlenn Strauss 		{ S("realm=") },
119581b2d1f0SGlenn Strauss 		{ S("nonce=") },
119681b2d1f0SGlenn Strauss 		{ S("uri=") },
119781b2d1f0SGlenn Strauss 		{ S("algorithm=") },
119881b2d1f0SGlenn Strauss 		{ S("qop=") },
119981b2d1f0SGlenn Strauss 		{ S("cnonce=") },
120081b2d1f0SGlenn Strauss 		{ S("nc=") },
120181b2d1f0SGlenn Strauss 		{ S("response=") },
120281b2d1f0SGlenn Strauss 
120381b2d1f0SGlenn Strauss 		{ NULL, 0, NULL }
120481b2d1f0SGlenn Strauss 	};
120581b2d1f0SGlenn Strauss #undef S
120681b2d1f0SGlenn Strauss 
120781b2d1f0SGlenn Strauss 	dkv[0].ptr = &username;
120881b2d1f0SGlenn Strauss 	dkv[1].ptr = &realm;
120981b2d1f0SGlenn Strauss 	dkv[2].ptr = &nonce;
121081b2d1f0SGlenn Strauss 	dkv[3].ptr = &uri;
121181b2d1f0SGlenn Strauss 	dkv[4].ptr = &algorithm;
121281b2d1f0SGlenn Strauss 	dkv[5].ptr = &qop;
121381b2d1f0SGlenn Strauss 	dkv[6].ptr = &cnonce;
121481b2d1f0SGlenn Strauss 	dkv[7].ptr = &nc;
121581b2d1f0SGlenn Strauss 	dkv[8].ptr = &respons;
121681b2d1f0SGlenn Strauss 
1217cf0098eaSGlenn Strauss 	if (NULL == backend || NULL == backend->digest) {
1218cf0098eaSGlenn Strauss 		if (NULL == backend)
12197c7f8c46SGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
12207c7f8c46SGlenn Strauss 			  "auth.backend not configured for %s", r->uri.path.ptr);
1221cf0098eaSGlenn Strauss 		else
1222cf0098eaSGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
1223cf0098eaSGlenn Strauss 			  "auth.require \"method\" => \"digest\" invalid "
1224cf0098eaSGlenn Strauss 			  "(try \"basic\"?) for %s",
1225cf0098eaSGlenn Strauss 			  r->uri.path.ptr);
12267c7f8c46SGlenn Strauss 		r->http_status = 500;
12277c7f8c46SGlenn Strauss 		r->handler_module = NULL;
12287ba06c71SGlenn Strauss 		return HANDLER_FINISHED;
12297ba06c71SGlenn Strauss 	}
12307ba06c71SGlenn Strauss 
12313dd3cde9SGlenn Strauss 	if (NULL == vb) {
12327c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
12337ba06c71SGlenn Strauss 	}
12347ba06c71SGlenn Strauss 
123513008156SGlenn Strauss 	if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) {
12367c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
1237dadfb5fcSGlenn Strauss 	} else {
12383dd3cde9SGlenn Strauss 		size_t n = buffer_string_length(vb);
12399bc61f16SGlenn Strauss 	      #ifdef __COVERITY__
1240dadfb5fcSGlenn Strauss 		if (n < sizeof("Digest ")-1) {
12417c7f8c46SGlenn Strauss 			return mod_auth_send_400_bad_request(r);
12429bc61f16SGlenn Strauss 		}
12439bc61f16SGlenn Strauss 	      #endif
1244dadfb5fcSGlenn Strauss 		n -= (sizeof("Digest ")-1);
12457ba06c71SGlenn Strauss 		b = buffer_init();
12463dd3cde9SGlenn Strauss 		buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n);
1247dadfb5fcSGlenn Strauss 	}
124881b2d1f0SGlenn Strauss 
124981b2d1f0SGlenn Strauss 	/* parse credentials from client */
125081b2d1f0SGlenn Strauss 	for (c = b->ptr; *c; c++) {
125181b2d1f0SGlenn Strauss 		/* skip whitespaces */
125281b2d1f0SGlenn Strauss 		while (*c == ' ' || *c == '\t') c++;
125381b2d1f0SGlenn Strauss 		if (!*c) break;
125481b2d1f0SGlenn Strauss 
125581b2d1f0SGlenn Strauss 		for (i = 0; dkv[i].key; i++) {
125681b2d1f0SGlenn Strauss 			if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) {
125781b2d1f0SGlenn Strauss 				if ((c[dkv[i].key_len] == '"') &&
125881b2d1f0SGlenn Strauss 				    (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) {
125981b2d1f0SGlenn Strauss 					/* value with "..." */
126081b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len + 1;
126181b2d1f0SGlenn Strauss 					c = e;
126281b2d1f0SGlenn Strauss 
126381b2d1f0SGlenn Strauss 					*e = '\0';
126481b2d1f0SGlenn Strauss 				} else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) {
126581b2d1f0SGlenn Strauss 					/* value without "...", terminated by ',' */
126681b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len;
126781b2d1f0SGlenn Strauss 					c = e;
126881b2d1f0SGlenn Strauss 
126981b2d1f0SGlenn Strauss 					*e = '\0';
127081b2d1f0SGlenn Strauss 				} else {
127181b2d1f0SGlenn Strauss 					/* value without "...", terminated by EOL */
127281b2d1f0SGlenn Strauss 					*(dkv[i].ptr) = c + dkv[i].key_len;
127381b2d1f0SGlenn Strauss 					c += strlen(c) - 1;
127481b2d1f0SGlenn Strauss 				}
127581b2d1f0SGlenn Strauss 				break;
127681b2d1f0SGlenn Strauss 			}
127781b2d1f0SGlenn Strauss 		}
127881b2d1f0SGlenn Strauss 	}
127981b2d1f0SGlenn Strauss 
128081b2d1f0SGlenn Strauss 	/* check if everything is transmitted */
128181b2d1f0SGlenn Strauss 	if (!username ||
128281b2d1f0SGlenn Strauss 	    !realm ||
128381b2d1f0SGlenn Strauss 	    !nonce ||
128481b2d1f0SGlenn Strauss 	    !uri ||
128581b2d1f0SGlenn Strauss 	    (qop && (!nc || !cnonce)) ||
128681b2d1f0SGlenn Strauss 	    !respons ) {
128781b2d1f0SGlenn Strauss 		/* missing field */
128881b2d1f0SGlenn Strauss 
12897c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
129081b2d1f0SGlenn Strauss 		  "digest: missing field");
129181b2d1f0SGlenn Strauss 
129281b2d1f0SGlenn Strauss 		buffer_free(b);
12937c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
129481b2d1f0SGlenn Strauss 	}
129581b2d1f0SGlenn Strauss 
129660f4cf3aSGlenn Strauss 	ai.username = username;
129760f4cf3aSGlenn Strauss 	ai.ulen     = strlen(username);
129860f4cf3aSGlenn Strauss 	ai.realm    = realm;
129960f4cf3aSGlenn Strauss 	ai.rlen     = strlen(realm);
130060f4cf3aSGlenn Strauss 
130160f4cf3aSGlenn Strauss 	if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) {
13027c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
13035a6e4df8SGlenn Strauss 		  "digest: realm mismatch");
13045a6e4df8SGlenn Strauss 		buffer_free(b);
13057c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
13065a6e4df8SGlenn Strauss 	}
13075a6e4df8SGlenn Strauss 
1308b9e2be50SGlenn Strauss 	if (!mod_auth_algorithm_parse(&ai, algorithm)
1309b9e2be50SGlenn Strauss 	    || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) {
13107c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
1311010c2894SGlenn Strauss 		  "digest: (%s): invalid", algorithm);
1312b9e2be50SGlenn Strauss 		buffer_free(b);
13137c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
1314b9e2be50SGlenn Strauss 	}
1315b9e2be50SGlenn Strauss 
131681b2d1f0SGlenn Strauss 	/**
131781b2d1f0SGlenn Strauss 	 * protect the md5-sess against missing cnonce and nonce
131881b2d1f0SGlenn Strauss 	 */
1319b9e2be50SGlenn Strauss 	if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) {
13207c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
1321010c2894SGlenn Strauss 		  "digest: (%s): missing field", algorithm);
132281b2d1f0SGlenn Strauss 
132381b2d1f0SGlenn Strauss 		buffer_free(b);
13247c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
132581b2d1f0SGlenn Strauss 	}
132681b2d1f0SGlenn Strauss 
1327b9e2be50SGlenn Strauss 	{
1328b9e2be50SGlenn Strauss 		size_t resplen = strlen(respons);
1329b9e2be50SGlenn Strauss 		if (0 != http_auth_digest_hex2bin(respons, resplen,
1330b9e2be50SGlenn Strauss 						  rdigest, sizeof(rdigest))
1331b9e2be50SGlenn Strauss 		    || resplen != (ai.dlen << 1)) {
13327c7f8c46SGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
1333010c2894SGlenn Strauss 			  "digest: (%s): invalid format", respons);
1334b9e2be50SGlenn Strauss 			buffer_free(b);
13357c7f8c46SGlenn Strauss 			return mod_auth_send_400_bad_request(r);
1336b9e2be50SGlenn Strauss 		}
1337b9e2be50SGlenn Strauss 	}
1338b9e2be50SGlenn Strauss 
1339fc42ec12SGlenn Strauss 	if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){
13407c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
134181b2d1f0SGlenn Strauss 		  "digest: qop=auth-int not supported");
134281b2d1f0SGlenn Strauss 
134381b2d1f0SGlenn Strauss 		buffer_free(b);
13447c7f8c46SGlenn Strauss 		return mod_auth_send_400_bad_request(r);
134581b2d1f0SGlenn Strauss 	}
134681b2d1f0SGlenn Strauss 
134781b2d1f0SGlenn Strauss 	/* detect if attacker is attempting to reuse valid digest for one uri
134881b2d1f0SGlenn Strauss 	 * on a different request uri.  Might also happen if intermediate proxy
134981b2d1f0SGlenn Strauss 	 * altered client request line.  (Altered request would not result in
135081b2d1f0SGlenn Strauss 	 * the same digest as that calculated by the client.)
135181b2d1f0SGlenn Strauss 	 * Internal redirects such as with mod_rewrite will modify request uri.
135281b2d1f0SGlenn Strauss 	 * Reauthentication is done to detect crossing auth realms, but this
13537c7f8c46SGlenn Strauss 	 * uri validation step is bypassed.  r->target_orig is
13548131e439SGlenn Strauss 	 * original request-target sent in client request. */
135581b2d1f0SGlenn Strauss 	{
135681b2d1f0SGlenn Strauss 		const size_t ulen = strlen(uri);
13577c7f8c46SGlenn Strauss 		if (!buffer_is_equal_string(&r->target_orig, uri, ulen)) {
13587c7f8c46SGlenn Strauss 			log_error(r->conf.errh, __FILE__, __LINE__,
1359010c2894SGlenn Strauss 			  "digest: auth failed: uri mismatch (%s != %s), IP: %s",
13607c7f8c46SGlenn Strauss 			  r->target_orig.ptr, uri, r->con->dst_addr_buf->ptr);
136181b2d1f0SGlenn Strauss 			buffer_free(b);
13627c7f8c46SGlenn Strauss 			return mod_auth_send_400_bad_request(r);
136381b2d1f0SGlenn Strauss 		}
136481b2d1f0SGlenn Strauss 	}
136581b2d1f0SGlenn Strauss 
1366e1188e77SGlenn Strauss 	/* check age of nonce.  Note, random data is used in nonce generation
1367e1188e77SGlenn Strauss 	 * in mod_auth_send_401_unauthorized_digest().  If that were replaced
1368e1188e77SGlenn Strauss 	 * with nanosecond time, then nonce secret would remain unique enough
1369e1188e77SGlenn Strauss 	 * for the purposes of Digest auth, and would be reproducible (and
1370e1188e77SGlenn Strauss 	 * verifiable) if nanoseconds were included with seconds as part of the
1371e1188e77SGlenn Strauss 	 * nonce "timestamp:secret".  However, doing so would expose a high
1372e1188e77SGlenn Strauss 	 * precision timestamp of the system to attackers.  timestamp in nonce
1373e1188e77SGlenn Strauss 	 * could theoretically be modified and still produce same md5sum, but
1374e1188e77SGlenn Strauss 	 * that is highly unlikely within a 10 min (moving) window of valid
1375e1188e77SGlenn Strauss 	 * time relative to current time (now).
1376e1188e77SGlenn Strauss 	 * If it is desired to validate that nonces were generated by server,
1377e1188e77SGlenn Strauss 	 * then specify auth.require = ( ... => ( "secret" => "..." ) )
1378e1188e77SGlenn Strauss 	 * When secret is specified, then instead of nanoseconds, the random
1379e1188e77SGlenn Strauss 	 * data value (included for unique nonces) will be exposed in the nonce
1380e1188e77SGlenn Strauss 	 * along with the timestamp, and the additional secret will be used to
1381e1188e77SGlenn Strauss 	 * validate that the server generated the nonce using that secret. */
1382e1188e77SGlenn Strauss 	int send_nextnonce;
1383e1188e77SGlenn Strauss 	{
1384e1188e77SGlenn Strauss 		time_t ts = 0;
1385e1188e77SGlenn Strauss 		const unsigned char * const nonce_uns = (unsigned char *)nonce;
1386e1188e77SGlenn Strauss 		for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) {
1387e1188e77SGlenn Strauss 			ts = (ts << 4) + hex2int(nonce_uns[i]);
1388e1188e77SGlenn Strauss 		}
1389e1188e77SGlenn Strauss 		const time_t cur_ts = log_epoch_secs;
1390e1188e77SGlenn Strauss 		if (nonce[i] != ':'
1391e1188e77SGlenn Strauss 		    || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/
1392e1188e77SGlenn Strauss 			/* nonce is stale; have client regenerate digest */
1393e1188e77SGlenn Strauss 			buffer_free(b);
1394e1188e77SGlenn Strauss 			return mod_auth_send_401_unauthorized_digest(r, require, ai.dalgo);
1395e1188e77SGlenn Strauss 		}
1396e1188e77SGlenn Strauss 
1397e1188e77SGlenn Strauss 		send_nextnonce = (cur_ts - ts > 540); /*(9 mins)*/
1398e1188e77SGlenn Strauss 
1399e1188e77SGlenn Strauss 		if (require->nonce_secret) {
1400e1188e77SGlenn Strauss 			unsigned int rnd = 0;
1401e1188e77SGlenn Strauss 			for (int j = i+8; i < j && light_isxdigit(nonce_uns[i]); ++i) {
1402e1188e77SGlenn Strauss 				rnd = (rnd << 4) + hex2int(nonce_uns[i]);
1403e1188e77SGlenn Strauss 			}
1404e1188e77SGlenn Strauss 			if (nonce[i] != ':') {
1405e1188e77SGlenn Strauss 				/* nonce is invalid;
1406e1188e77SGlenn Strauss 				 * expect extra field w/ require->nonce_secret */
1407e1188e77SGlenn Strauss 				log_error(r->conf.errh, __FILE__, __LINE__,
1408e1188e77SGlenn Strauss 				  "digest: nonce invalid");
1409e1188e77SGlenn Strauss 				buffer_free(b);
1410e1188e77SGlenn Strauss 				return mod_auth_send_400_bad_request(r);
1411e1188e77SGlenn Strauss 			}
1412e1188e77SGlenn Strauss 			buffer * const tb = r->tmp_buf;
1413e1188e77SGlenn Strauss 			buffer_clear(tb);
1414e1188e77SGlenn Strauss 			mod_auth_append_nonce(tb, cur_ts, require, ai.dalgo, (int *)&rnd);
1415e1188e77SGlenn Strauss 			if (!buffer_eq_slen(tb, nonce, strlen(nonce))) {
1416e1188e77SGlenn Strauss 				/* nonce not generated using current require->nonce_secret */
1417e1188e77SGlenn Strauss 				log_error(r->conf.errh, __FILE__, __LINE__,
1418e1188e77SGlenn Strauss 				  "digest: nonce mismatch");
1419e1188e77SGlenn Strauss 				buffer_free(b);
1420e1188e77SGlenn Strauss 				return mod_auth_send_401_unauthorized_digest(r, require, 0);
1421e1188e77SGlenn Strauss 			}
1422e1188e77SGlenn Strauss 		}
1423e1188e77SGlenn Strauss 	}
1424e1188e77SGlenn Strauss 
1425e11514b0SGlenn Strauss 	handler_t rc = HANDLER_UNSET;
1426e11514b0SGlenn Strauss 
1427e11514b0SGlenn Strauss 	plugin_data * const p = p_d;
1428e11514b0SGlenn Strauss 	splay_tree ** sptree = p->conf.auth_cache
1429e11514b0SGlenn Strauss 	  ? &p->conf.auth_cache->sptree
1430e11514b0SGlenn Strauss 	  : NULL;
1431e11514b0SGlenn Strauss 	http_auth_cache_entry *ae = NULL;
1432e11514b0SGlenn Strauss 	int ndx = -1;
1433e11514b0SGlenn Strauss 	if (sptree) {
1434e11514b0SGlenn Strauss 		ndx = http_auth_cache_hash(require, ai.username, ai.ulen);
1435e11514b0SGlenn Strauss 		ae = http_auth_cache_query(sptree, ndx);
1436e11514b0SGlenn Strauss 		if (ae && ae->require == require
1437e11514b0SGlenn Strauss 		    && ae->dalgo == ai.dalgo
1438e11514b0SGlenn Strauss 		    && ae->dlen == ai.dlen
1439e11514b0SGlenn Strauss 		    && ae->ulen == ai.ulen
1440e11514b0SGlenn Strauss 		    && 0 == memcmp(ae->username, ai.username, ai.ulen)) {
1441e11514b0SGlenn Strauss 			rc = HANDLER_GO_ON;
1442e11514b0SGlenn Strauss 			memcpy(ai.digest, ae->pwdigest, ai.dlen);
1443e11514b0SGlenn Strauss 		}
1444e11514b0SGlenn Strauss 		else /*(not found or hash collision)*/
1445e11514b0SGlenn Strauss 			ae = NULL;
1446e11514b0SGlenn Strauss 	}
1447e11514b0SGlenn Strauss 
1448e11514b0SGlenn Strauss 	if (HANDLER_UNSET == rc)
1449e11514b0SGlenn Strauss 		rc = backend->digest(r, backend->p_d, &ai);
1450e11514b0SGlenn Strauss 
1451e11514b0SGlenn Strauss 	switch (rc) {
14524b3a91e6SGlenn Strauss 	case HANDLER_GO_ON:
14534b3a91e6SGlenn Strauss 		break;
14544b3a91e6SGlenn Strauss 	case HANDLER_WAIT_FOR_EVENT:
145581b2d1f0SGlenn Strauss 		buffer_free(b);
14564b3a91e6SGlenn Strauss 		return HANDLER_WAIT_FOR_EVENT;
14574b3a91e6SGlenn Strauss 	case HANDLER_FINISHED:
14584b3a91e6SGlenn Strauss 		buffer_free(b);
14594b3a91e6SGlenn Strauss 		return HANDLER_FINISHED;
14604b3a91e6SGlenn Strauss 	case HANDLER_ERROR:
14614b3a91e6SGlenn Strauss 	default:
14627c7f8c46SGlenn Strauss 		r->keep_alive = 0; /*(disable keep-alive if unknown user)*/
14634b3a91e6SGlenn Strauss 		buffer_free(b);
14647c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
146581b2d1f0SGlenn Strauss 	}
146681b2d1f0SGlenn Strauss 
1467e11514b0SGlenn Strauss 	if (sptree && NULL == ae) { /*(cache digest from backend)*/
1468e11514b0SGlenn Strauss 		ae = http_auth_cache_entry_init(require, ai.dalgo, ai.username, ai.ulen,
1469e11514b0SGlenn Strauss 		                                (char *)ai.digest, ai.dlen);
1470e11514b0SGlenn Strauss 		http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free);
1471e11514b0SGlenn Strauss 	}
1472e11514b0SGlenn Strauss 
1473e1188e77SGlenn Strauss 	const char *m = get_http_method_name(r->http_method);
1474e1188e77SGlenn Strauss 	force_assert(m);
1475e1188e77SGlenn Strauss 
1476b9e2be50SGlenn Strauss 	mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop);
147781b2d1f0SGlenn Strauss 
14780e749c1cSGlenn Strauss 	if (!http_auth_const_time_memeq(rdigest, ai.digest, ai.dlen)) {
147981b2d1f0SGlenn Strauss 		/* digest not ok */
14807c7f8c46SGlenn Strauss 		log_error(r->conf.errh, __FILE__, __LINE__,
1481010c2894SGlenn Strauss 		  "digest: auth failed for %s: wrong password, IP: %s",
14827c7f8c46SGlenn Strauss 		  username, r->con->dst_addr_buf->ptr);
14837c7f8c46SGlenn Strauss 		r->keep_alive = 0; /*(disable keep-alive if bad password)*/
148481b2d1f0SGlenn Strauss 
148581b2d1f0SGlenn Strauss 		buffer_free(b);
14867c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
148781b2d1f0SGlenn Strauss 	}
148881b2d1f0SGlenn Strauss 
148981b2d1f0SGlenn Strauss 	/* value is our allow-rules */
14907ba06c71SGlenn Strauss 	if (!http_auth_match_rules(require, username, NULL, NULL)) {
14914b3a91e6SGlenn Strauss 		buffer_free(b);
14927c7f8c46SGlenn Strauss 		return mod_auth_send_401_unauthorized_digest(r, require, 0);
149381b2d1f0SGlenn Strauss 	}
149481b2d1f0SGlenn Strauss 
1495e1188e77SGlenn Strauss 	if (send_nextnonce) {
14966ad325c6SGlenn Strauss 			/*(send nextnonce when expiration is approaching)*/
14977c7f8c46SGlenn Strauss 			buffer * const tb = r->tmp_buf;
1498e1188e77SGlenn Strauss 			const time_t cur_ts = log_epoch_secs;
1499e1188e77SGlenn Strauss 			mod_auth_digest_authentication_info(tb, cur_ts, require, ai.dalgo);
15007c7f8c46SGlenn Strauss 			http_header_response_set(r, HTTP_HEADER_OTHER,
1501409bba80SGlenn Strauss 			                         CONST_STR_LEN("Authentication-Info"),
1502409bba80SGlenn Strauss 			                         CONST_BUF_LEN(tb));
15036ad325c6SGlenn Strauss 	}
150481b2d1f0SGlenn Strauss 
15057c7f8c46SGlenn Strauss 	http_auth_setenv(r, ai.username, ai.ulen, CONST_STR_LEN("Digest"));
150681b2d1f0SGlenn Strauss 
150781b2d1f0SGlenn Strauss 	buffer_free(b);
150881b2d1f0SGlenn Strauss 
15094b3a91e6SGlenn Strauss 	return HANDLER_GO_ON;
151081b2d1f0SGlenn Strauss }
151181b2d1f0SGlenn Strauss 
15127c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st * const r, const struct http_auth_require_t * const require, int nonce_stale) {
15137c7f8c46SGlenn Strauss 	buffer * const tb = r->tmp_buf;
1514b5775b99SGlenn Strauss 	mod_auth_digest_www_authenticate(tb, log_epoch_secs, require, nonce_stale);
1515367f30a6SGlenn Strauss 	http_header_response_set(r, HTTP_HEADER_WWW_AUTHENTICATE,
1516367f30a6SGlenn Strauss 	                         CONST_STR_LEN("WWW-Authenticate"),
1517367f30a6SGlenn Strauss 	                         CONST_BUF_LEN(tb));
151881b2d1f0SGlenn Strauss 
15197c7f8c46SGlenn Strauss 	r->http_status = 401;
15207c7f8c46SGlenn Strauss 	r->handler_module = NULL;
1521f9d58670SStefan Bühler 	return HANDLER_FINISHED;
15227ba06c71SGlenn Strauss }
15237ba06c71SGlenn Strauss 
15247c7f8c46SGlenn Strauss static handler_t mod_auth_check_extern(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) {
15257ba06c71SGlenn Strauss 	/* require REMOTE_USER already set */
15267c7f8c46SGlenn Strauss 	const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER"));
15277ba06c71SGlenn Strauss 	UNUSED(p_d);
15287ba06c71SGlenn Strauss 	UNUSED(backend);
15293dd3cde9SGlenn Strauss 	if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) {
1530f9d58670SStefan Bühler 		return HANDLER_GO_ON;
1531bcdc6a3bSJan Kneschke 	} else {
15327c7f8c46SGlenn Strauss 		r->http_status = 401;
15337c7f8c46SGlenn Strauss 		r->handler_module = NULL;
1534bcdc6a3bSJan Kneschke 		return HANDLER_FINISHED;
15357187271fSStefan Bühler 	}
1536bcdc6a3bSJan Kneschke }
1537