13538f8f2SGlenn Strauss /*
23538f8f2SGlenn Strauss * http_auth - HTTP authentication and authorization
33538f8f2SGlenn Strauss *
43538f8f2SGlenn Strauss * Largely-rewritten from original
53538f8f2SGlenn Strauss * Copyright(c) 2016,2021 Glenn Strauss gstrauss()gluelogic.com All rights reserved
63538f8f2SGlenn Strauss * License: BSD 3-clause (same as lighttpd)
73538f8f2SGlenn Strauss */
88abd06a7SGlenn Strauss #include "first.h"
98abd06a7SGlenn Strauss
101fc8a3e1SGlenn Strauss #include <stdlib.h>
111fc8a3e1SGlenn Strauss #include <string.h>
121fc8a3e1SGlenn Strauss
133538f8f2SGlenn Strauss #include "mod_auth_api.h"
141fc8a3e1SGlenn Strauss #include "sys-crypto-md.h" /* USE_LIB_CRYPTO */
151fc8a3e1SGlenn Strauss
1604d76e7aSGlenn Strauss #include "base.h"
1762ccda85SGlenn Strauss #include "ck.h"
183dd3cde9SGlenn Strauss #include "http_header.h"
1922e8b456SStefan Bühler #include "log.h"
20156e1da2SGlenn Strauss #include "algo_splaytree.h"
213538f8f2SGlenn Strauss #include "plugin.h"
223538f8f2SGlenn Strauss #include "plugin_config.h"
23bcdc6a3bSJan Kneschke
24bcdc6a3bSJan Kneschke /**
257ba06c71SGlenn Strauss * auth framework
26bcdc6a3bSJan Kneschke */
27bcdc6a3bSJan Kneschke
284b3a91e6SGlenn Strauss typedef struct {
29e11514b0SGlenn Strauss splay_tree *sptree; /* data in nodes of tree are (http_auth_cache_entry *)*/
30e11514b0SGlenn Strauss time_t max_age;
31e11514b0SGlenn Strauss } http_auth_cache;
32e11514b0SGlenn Strauss
33e11514b0SGlenn Strauss typedef struct {
344b3a91e6SGlenn Strauss const http_auth_backend_t *auth_backend;
358e713130SGlenn Strauss const array *auth_require;
36e11514b0SGlenn Strauss http_auth_cache *auth_cache;
378e713130SGlenn Strauss unsigned int auth_extern_authn;
387ba06c71SGlenn Strauss } plugin_config;
394b3a91e6SGlenn Strauss
404b3a91e6SGlenn Strauss typedef struct {
414b3a91e6SGlenn Strauss PLUGIN_DATA;
428e713130SGlenn Strauss plugin_config defaults;
437ba06c71SGlenn Strauss plugin_config conf;
447ba06c71SGlenn Strauss } plugin_data;
457ba06c71SGlenn Strauss
46e11514b0SGlenn Strauss typedef struct {
47e11514b0SGlenn Strauss const struct http_auth_require_t *require;
48309c1693SGlenn Strauss unix_time64_t ctime;
49e11514b0SGlenn Strauss int dalgo;
50e11514b0SGlenn Strauss uint32_t dlen;
51e11514b0SGlenn Strauss uint32_t ulen;
5271175df1SGlenn Strauss uint32_t klen;
5371175df1SGlenn Strauss char *k;
54e11514b0SGlenn Strauss char *username;
55e11514b0SGlenn Strauss char *pwdigest;
56e11514b0SGlenn Strauss } http_auth_cache_entry;
57e11514b0SGlenn Strauss
58e11514b0SGlenn Strauss static http_auth_cache_entry *
http_auth_cache_entry_init(const struct http_auth_require_t * const require,const int dalgo,const char * k,const uint32_t klen,const char * username,const uint32_t ulen,const char * pw,const uint32_t pwlen)5971175df1SGlenn Strauss http_auth_cache_entry_init (const struct http_auth_require_t * const require, const int dalgo, const char *k, const uint32_t klen, const char *username, const uint32_t ulen, const char *pw, const uint32_t pwlen)
60e11514b0SGlenn Strauss {
61e11514b0SGlenn Strauss /*(similar to buffer_copy_string_len() for each element,
62e11514b0SGlenn Strauss * but allocate exact lengths in single chunk of memory
63e11514b0SGlenn Strauss * for cache to avoid wasting space and for memory locality)*/
64e11514b0SGlenn Strauss /* http_auth_require_t is stored instead of copying realm
65e11514b0SGlenn Strauss *(store pointer to http_auth_require_t, which is persistent
66e11514b0SGlenn Strauss * and will be different for each realm + permissions combo)*/
67e11514b0SGlenn Strauss http_auth_cache_entry * const ae =
685e14db43SGlenn Strauss ck_malloc(sizeof(http_auth_cache_entry) + ulen + pwlen
6971175df1SGlenn Strauss + (k == username ? 0 : klen));
70e11514b0SGlenn Strauss ae->require = require;
71dbe3e236SGlenn Strauss ae->ctime = log_monotonic_secs;
72e11514b0SGlenn Strauss ae->dalgo = dalgo;
73e11514b0SGlenn Strauss ae->ulen = ulen;
74e11514b0SGlenn Strauss ae->dlen = pwlen;
7571175df1SGlenn Strauss ae->klen = klen;
76e11514b0SGlenn Strauss ae->username = (char *)(ae + 1);
77e11514b0SGlenn Strauss ae->pwdigest = ae->username + ulen;
7871175df1SGlenn Strauss ae->k = (k == username)
7971175df1SGlenn Strauss ? ae->username
8071175df1SGlenn Strauss : memcpy(ae->pwdigest + pwlen, k, klen);
81e11514b0SGlenn Strauss memcpy(ae->username, username, ulen);
82e11514b0SGlenn Strauss memcpy(ae->pwdigest, pw, pwlen);
83e11514b0SGlenn Strauss return ae;
84e11514b0SGlenn Strauss }
85e11514b0SGlenn Strauss
86e11514b0SGlenn Strauss static void
http_auth_cache_entry_free(void * data)87e11514b0SGlenn Strauss http_auth_cache_entry_free (void *data)
88e11514b0SGlenn Strauss {
89e11514b0SGlenn Strauss http_auth_cache_entry * const ae = data;
9008c03cd4SGlenn Strauss ck_memzero(ae->pwdigest, ae->dlen);
91e11514b0SGlenn Strauss free(ae);
92e11514b0SGlenn Strauss }
93e11514b0SGlenn Strauss
94e11514b0SGlenn Strauss static void
http_auth_cache_free(http_auth_cache * ac)95e11514b0SGlenn Strauss http_auth_cache_free (http_auth_cache *ac)
96e11514b0SGlenn Strauss {
97e11514b0SGlenn Strauss splay_tree *sptree = ac->sptree;
98e11514b0SGlenn Strauss while (sptree) {
99e11514b0SGlenn Strauss http_auth_cache_entry_free(sptree->data);
100e11514b0SGlenn Strauss sptree = splaytree_delete(sptree, sptree->key);
101e11514b0SGlenn Strauss }
102e11514b0SGlenn Strauss free(ac);
103e11514b0SGlenn Strauss }
104e11514b0SGlenn Strauss
105e11514b0SGlenn Strauss static http_auth_cache *
http_auth_cache_init(const array * opts)106e11514b0SGlenn Strauss http_auth_cache_init (const array *opts)
107e11514b0SGlenn Strauss {
1085e14db43SGlenn Strauss http_auth_cache *ac = ck_malloc(sizeof(http_auth_cache));
109e11514b0SGlenn Strauss ac->sptree = NULL;
110e11514b0SGlenn Strauss ac->max_age = 600; /* 10 mins */
111e11514b0SGlenn Strauss for (uint32_t i = 0, used = opts->used; i < used; ++i) {
112d6f692f1SGlenn Strauss data_unset *du = opts->data[i];
113d6f692f1SGlenn Strauss if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age")))
11418bbdbbeSGlenn Strauss ac->max_age = (time_t)
1158f84c7beSGlenn Strauss config_plugin_value_to_int32(du, 600); /* 10 min if invalid num */
116e11514b0SGlenn Strauss }
117e11514b0SGlenn Strauss return ac;
118e11514b0SGlenn Strauss }
119e11514b0SGlenn Strauss
120e11514b0SGlenn Strauss static int
http_auth_cache_hash(const struct http_auth_require_t * const require,const char * username,const uint32_t ulen)121e11514b0SGlenn Strauss http_auth_cache_hash (const struct http_auth_require_t * const require, const char *username, const uint32_t ulen)
122e11514b0SGlenn Strauss {
123e11514b0SGlenn Strauss uint32_t h = /*(hash pointer value, which includes realm and permissions)*/
124e11514b0SGlenn Strauss djbhash((char *)(intptr_t)require, sizeof(intptr_t), DJBHASH_INIT);
125e11514b0SGlenn Strauss h = djbhash(username, ulen, h);
126e11514b0SGlenn Strauss /* strip highest bit of hash value for splaytree (see splaytree_djbhash())*/
127e11514b0SGlenn Strauss return (int32_t)(h & ~(((uint32_t)1) << 31));
128e11514b0SGlenn Strauss }
129e11514b0SGlenn Strauss
130e11514b0SGlenn Strauss static http_auth_cache_entry *
http_auth_cache_query(splay_tree ** const sptree,const int ndx)131e11514b0SGlenn Strauss http_auth_cache_query (splay_tree ** const sptree, const int ndx)
132e11514b0SGlenn Strauss {
133e11514b0SGlenn Strauss *sptree = splaytree_splay(*sptree, ndx);
134e11514b0SGlenn Strauss return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL;
135e11514b0SGlenn Strauss }
136e11514b0SGlenn Strauss
137e11514b0SGlenn Strauss static void
http_auth_cache_insert(splay_tree ** const sptree,const int ndx,void * const data,void (data_free_fn)(void *))138e11514b0SGlenn Strauss http_auth_cache_insert (splay_tree ** const sptree, const int ndx, void * const data, void(data_free_fn)(void *))
139e11514b0SGlenn Strauss {
140e11514b0SGlenn Strauss /*(not necessary to re-splay (with current usage) since single-threaded
141e11514b0SGlenn Strauss * and splaytree has not been modified since http_auth_cache_query())*/
142e11514b0SGlenn Strauss /* *sptree = splaytree_splay(*sptree, ndx); */
143e11514b0SGlenn Strauss if (NULL == *sptree || (*sptree)->key != ndx)
144e11514b0SGlenn Strauss *sptree = splaytree_insert(*sptree, ndx, data);
145e11514b0SGlenn Strauss else { /* collision; replace old entry */
146e11514b0SGlenn Strauss data_free_fn((*sptree)->data);
147e11514b0SGlenn Strauss (*sptree)->data = data;
148e11514b0SGlenn Strauss }
149e11514b0SGlenn Strauss }
150e11514b0SGlenn Strauss
151e11514b0SGlenn Strauss /* walk though cache, collect expired ids, and remove them in a second loop */
152e11514b0SGlenn Strauss static void
mod_auth_tag_old_entries(splay_tree * const t,int * const keys,int * const ndx,const time_t max_age,const unix_time64_t cur_ts)153309c1693SGlenn Strauss mod_auth_tag_old_entries (splay_tree * const t, int * const keys, int * const ndx, const time_t max_age, const unix_time64_t cur_ts)
154e11514b0SGlenn Strauss {
155e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
156e11514b0SGlenn Strauss if (t->left)
157e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->left, keys, ndx, max_age, cur_ts);
158e11514b0SGlenn Strauss if (t->right)
159e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->right, keys, ndx, max_age, cur_ts);
160e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/
161e11514b0SGlenn Strauss
162e11514b0SGlenn Strauss const http_auth_cache_entry * const ae = t->data;
163e11514b0SGlenn Strauss if (cur_ts - ae->ctime > max_age)
164e11514b0SGlenn Strauss keys[(*ndx)++] = t->key;
165e11514b0SGlenn Strauss }
166e11514b0SGlenn Strauss
167e11514b0SGlenn Strauss __attribute_noinline__
168e11514b0SGlenn Strauss static void
mod_auth_periodic_cleanup(splay_tree ** sptree_ptr,const time_t max_age,const unix_time64_t cur_ts)169309c1693SGlenn Strauss mod_auth_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const unix_time64_t cur_ts)
170e11514b0SGlenn Strauss {
171e11514b0SGlenn Strauss splay_tree *sptree = *sptree_ptr;
172e11514b0SGlenn Strauss int max_ndx, i;
173e11514b0SGlenn Strauss int keys[8192]; /* 32k size on stack */
174e11514b0SGlenn Strauss do {
175e11514b0SGlenn Strauss if (!sptree) break;
176e11514b0SGlenn Strauss max_ndx = 0;
177e11514b0SGlenn Strauss mod_auth_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts);
178e11514b0SGlenn Strauss for (i = 0; i < max_ndx; ++i) {
179e11514b0SGlenn Strauss int ndx = keys[i];
180e11514b0SGlenn Strauss sptree = splaytree_splay(sptree, ndx);
181e11514b0SGlenn Strauss if (sptree && sptree->key == ndx) {
182e11514b0SGlenn Strauss http_auth_cache_entry_free(sptree->data);
183e11514b0SGlenn Strauss sptree = splaytree_delete(sptree, ndx);
184e11514b0SGlenn Strauss }
185e11514b0SGlenn Strauss }
186e11514b0SGlenn Strauss } while (max_ndx == sizeof(keys)/sizeof(int));
187e11514b0SGlenn Strauss *sptree_ptr = sptree;
188e11514b0SGlenn Strauss }
189e11514b0SGlenn Strauss
TRIGGER_FUNC(mod_auth_periodic)190e11514b0SGlenn Strauss TRIGGER_FUNC(mod_auth_periodic)
191e11514b0SGlenn Strauss {
192e11514b0SGlenn Strauss const plugin_data * const p = p_d;
193309c1693SGlenn Strauss const unix_time64_t cur_ts = log_monotonic_secs;
194e11514b0SGlenn Strauss if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/
195e11514b0SGlenn Strauss UNUSED(srv);
196e11514b0SGlenn Strauss
197e11514b0SGlenn Strauss /* future: might construct array of (http_auth_cache *) at startup
198e11514b0SGlenn Strauss * to avoid the need to search for them here */
19933e400b4SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */
20033e400b4SGlenn Strauss if (NULL == p->cvlist) return HANDLER_GO_ON;
20133e400b4SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
202e11514b0SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
203e11514b0SGlenn Strauss for (; cpv->k_id != -1; ++cpv) {
204e11514b0SGlenn Strauss if (cpv->k_id != 3) continue; /* k_id == 3 for auth.cache */
205e11514b0SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL) continue;
206e11514b0SGlenn Strauss http_auth_cache *ac = cpv->v.v;
207e11514b0SGlenn Strauss mod_auth_periodic_cleanup(&ac->sptree, ac->max_age, cur_ts);
208e11514b0SGlenn Strauss }
209e11514b0SGlenn Strauss }
210e11514b0SGlenn Strauss
211e11514b0SGlenn Strauss return HANDLER_GO_ON;
212e11514b0SGlenn Strauss }
213e11514b0SGlenn Strauss
214e11514b0SGlenn Strauss
215e11514b0SGlenn Strauss
216e11514b0SGlenn Strauss
2177c7f8c46SGlenn 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);
2187c7f8c46SGlenn 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);
2197c7f8c46SGlenn 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);
2204b3a91e6SGlenn Strauss
INIT_FUNC(mod_auth_init)221bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) {
222e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL };
223e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL };
2247ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL };
2255e14db43SGlenn Strauss plugin_data *p = ck_calloc(1, sizeof(*p));
2267ba06c71SGlenn Strauss
2277ba06c71SGlenn Strauss /* register http_auth_scheme_* */
228e11514b0SGlenn Strauss http_auth_scheme_basic.p_d = p;
2297ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic);
230e11514b0SGlenn Strauss http_auth_scheme_digest.p_d = p;
2317ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest);
2327ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern);
233bcdc6a3bSJan Kneschke
234bcdc6a3bSJan Kneschke return p;
235bcdc6a3bSJan Kneschke }
236bcdc6a3bSJan Kneschke
FREE_FUNC(mod_auth_free)237b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) {
238b73949e0SGlenn Strauss plugin_data * const p = p_d;
2398e713130SGlenn Strauss if (NULL == p->cvlist) return;
2408e713130SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */
2418e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
2428e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
2438e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) {
2448e713130SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
2458e713130SGlenn Strauss switch (cpv->k_id) {
2468e713130SGlenn Strauss case 1: /* auth.require */
2478e713130SGlenn Strauss array_free(cpv->v.v);
2488e713130SGlenn Strauss break;
249e11514b0SGlenn Strauss case 3: /* auth.cache */
250e11514b0SGlenn Strauss http_auth_cache_free(cpv->v.v);
251e11514b0SGlenn Strauss break;
2528e713130SGlenn Strauss default:
2538e713130SGlenn Strauss break;
2548e713130SGlenn Strauss }
2558e713130SGlenn Strauss }
2568e713130SGlenn Strauss }
2571a8ac120SGlenn Strauss
2581a8ac120SGlenn Strauss http_auth_dumbdata_reset();
2598e713130SGlenn Strauss }
2608e713130SGlenn Strauss
2617ba06c71SGlenn Strauss /* data type for mod_auth structured data
2627ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */
2637ba06c71SGlenn Strauss typedef struct {
2647ba06c71SGlenn Strauss DATA_UNSET;
2657ba06c71SGlenn Strauss http_auth_require_t *require;
2667ba06c71SGlenn Strauss } data_auth;
2677ba06c71SGlenn Strauss
data_auth_free(data_unset * d)2687ba06c71SGlenn Strauss static void data_auth_free(data_unset *d)
2697ba06c71SGlenn Strauss {
2707ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d;
271ad9b7e00SGlenn Strauss free(dauth->key.ptr);
2727ba06c71SGlenn Strauss http_auth_require_free(dauth->require);
2737ba06c71SGlenn Strauss free(dauth);
2747ba06c71SGlenn Strauss }
2757ba06c71SGlenn Strauss
data_auth_init(void)2767ba06c71SGlenn Strauss static data_auth *data_auth_init(void)
2777ba06c71SGlenn Strauss {
2788c7f1dfbSGlenn Strauss static const struct data_methods fn = {
2798c7f1dfbSGlenn Strauss NULL, /* copy must not be called on this data */
2808c7f1dfbSGlenn Strauss data_auth_free,
2818c7f1dfbSGlenn Strauss NULL, /* insert_dup must not be called on this data */
2828c7f1dfbSGlenn Strauss };
2835e14db43SGlenn Strauss data_auth * const dauth = ck_calloc(1, sizeof(*dauth));
2847ba06c71SGlenn Strauss dauth->type = TYPE_OTHER;
2858c7f1dfbSGlenn Strauss dauth->fn = &fn;
2867ba06c71SGlenn Strauss
2877ba06c71SGlenn Strauss dauth->require = http_auth_require_init();
2887ba06c71SGlenn Strauss
2897ba06c71SGlenn Strauss return dauth;
2907ba06c71SGlenn Strauss }
2917ba06c71SGlenn Strauss
mod_auth_algorithm_parse(http_auth_info_t * ai,const char * s,size_t len)292870b0c13SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s, size_t len) {
293870b0c13SGlenn Strauss if (0 == len) {
294b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_MD5;
295b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN;
296b9e2be50SGlenn Strauss return 1;
297b9e2be50SGlenn Strauss }
298b9e2be50SGlenn Strauss
299b9e2be50SGlenn Strauss if (len > 5
300b9e2be50SGlenn Strauss && (s[len-5] ) == '-'
301b9e2be50SGlenn Strauss && (s[len-4] | 0x20) == 's'
302b9e2be50SGlenn Strauss && (s[len-3] | 0x20) == 'e'
303b9e2be50SGlenn Strauss && (s[len-2] | 0x20) == 's'
304b9e2be50SGlenn Strauss && (s[len-1] | 0x20) == 's') {
305b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_SESS;
306b9e2be50SGlenn Strauss len -= 5;
307b9e2be50SGlenn Strauss }
308b9e2be50SGlenn Strauss else {
309b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_NONE;
310b9e2be50SGlenn Strauss }
311b9e2be50SGlenn Strauss
312b9e2be50SGlenn Strauss if (3 == len
313b9e2be50SGlenn Strauss && 'm' == (s[0] | 0x20)
314b9e2be50SGlenn Strauss && 'd' == (s[1] | 0x20)
315b9e2be50SGlenn Strauss && '5' == (s[2] )) {
316b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_MD5;
317b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN;
318b9e2be50SGlenn Strauss return 1;
319b9e2be50SGlenn Strauss }
320b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO
321b9e2be50SGlenn Strauss else if (len >= 7
322b9e2be50SGlenn Strauss && 's' == (s[0] | 0x20)
323b9e2be50SGlenn Strauss && 'h' == (s[1] | 0x20)
324b9e2be50SGlenn Strauss && 'a' == (s[2] | 0x20)
325b9e2be50SGlenn Strauss && '-' == (s[3] )) {
326b9e2be50SGlenn Strauss if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') {
327b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA256;
328b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA256_BINLEN;
329b9e2be50SGlenn Strauss return 1;
330b9e2be50SGlenn Strauss }
3311fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256
332b9e2be50SGlenn Strauss if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) {
333b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256;
334b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA512_256_BINLEN;
335b9e2be50SGlenn Strauss return 1;
336b9e2be50SGlenn Strauss }
337b9e2be50SGlenn Strauss #endif
338b9e2be50SGlenn Strauss }
339b9e2be50SGlenn Strauss #endif
340b9e2be50SGlenn Strauss return 0; /*(error)*/
341b9e2be50SGlenn Strauss }
342b9e2be50SGlenn Strauss
mod_auth_algorithms_parse(int * algorithm,buffer * algos)343b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) {
344870b0c13SGlenn Strauss for (const char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) {
345b9e2be50SGlenn Strauss http_auth_info_t ai;
346b9e2be50SGlenn Strauss p = strchr(s, '|');
347870b0c13SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, s, p ? (size_t)(p - s) : strlen(s)))
348870b0c13SGlenn Strauss return 0;
349b9e2be50SGlenn Strauss *algorithm |= ai.dalgo;
350b9e2be50SGlenn Strauss }
351b9e2be50SGlenn Strauss return 1;
352b9e2be50SGlenn Strauss }
353b9e2be50SGlenn Strauss
mod_auth_require_parse(http_auth_require_t * const require,const buffer * b,log_error_st * errh)354b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh)
3557ba06c71SGlenn Strauss {
3567ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */
3577ba06c71SGlenn Strauss
3587ba06c71SGlenn Strauss const char *str = b->ptr;
3597ba06c71SGlenn Strauss const char *p;
3607ba06c71SGlenn Strauss
3617ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) {
3627ba06c71SGlenn Strauss require->valid_user = 1;
3637ba06c71SGlenn Strauss return 1; /* success */
3647ba06c71SGlenn Strauss }
3657ba06c71SGlenn Strauss
3667ba06c71SGlenn Strauss do {
3677ba06c71SGlenn Strauss const char *eq;
3687ba06c71SGlenn Strauss size_t len;
3697ba06c71SGlenn Strauss p = strchr(str, '|');
3707ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str);
3717ba06c71SGlenn Strauss eq = memchr(str, '=', len);
3727ba06c71SGlenn Strauss if (NULL == eq) {
373b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
374010c2894SGlenn Strauss "error parsing auth.require 'require' field: missing '=' "
375010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
376010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str);
3777ba06c71SGlenn Strauss return 0;
3787ba06c71SGlenn Strauss }
379b2c18f48SGlenn Strauss if (eq[1] == '|' || eq[1] == '\0') {
380b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
381010c2894SGlenn Strauss "error parsing auth.require 'require' field: "
382010c2894SGlenn Strauss "missing token after '=' "
383010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
384010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str);
3857ba06c71SGlenn Strauss return 0;
3867ba06c71SGlenn Strauss }
3877ba06c71SGlenn Strauss
3887ba06c71SGlenn Strauss switch ((int)(eq - str)) {
3897ba06c71SGlenn Strauss case 4:
3907ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) {
391810109ccSGlenn Strauss /*("user=" is 5)*/
3928b296531SGlenn Strauss array_insert_value(&require->user, str+5, len-5);
3937ba06c71SGlenn Strauss continue;
3947ba06c71SGlenn Strauss }
3957ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) {
396810109ccSGlenn Strauss /*("host=" is 5)*/
3978b296531SGlenn Strauss array_insert_value(&require->host, str+5, len-5);
398b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
399010c2894SGlenn Strauss "warning parsing auth.require 'require' field: "
400010c2894SGlenn Strauss "'host' not implemented; field value: %s", b->ptr);
4017ba06c71SGlenn Strauss continue;
4027ba06c71SGlenn Strauss }
4037ba06c71SGlenn Strauss break; /* to error */
4047ba06c71SGlenn Strauss case 5:
4057ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) {
406810109ccSGlenn Strauss /*("group=" is 6)*/
4078b296531SGlenn Strauss array_insert_value(&require->group, str+6, len-6);
4089c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/
409b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
410010c2894SGlenn Strauss "warning parsing auth.require 'require' field: "
411010c2894SGlenn Strauss "'group' not implemented; field value: %s", b->ptr);
4129c91af0cSGlenn Strauss #endif
4137ba06c71SGlenn Strauss continue;
4147ba06c71SGlenn Strauss }
4157ba06c71SGlenn Strauss break; /* to error */
4167ba06c71SGlenn Strauss case 10:
4177ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) {
418b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
419010c2894SGlenn Strauss "error parsing auth.require 'require' field: "
420010c2894SGlenn Strauss "valid user can not be combined with other require rules "
421010c2894SGlenn Strauss "(expecting \"valid-user\" or "
422010c2894SGlenn Strauss "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr);
4237ba06c71SGlenn Strauss return 0;
4247ba06c71SGlenn Strauss }
4257ba06c71SGlenn Strauss break; /* to error */
4267ba06c71SGlenn Strauss default:
4277ba06c71SGlenn Strauss break; /* to error */
4287ba06c71SGlenn Strauss }
4297ba06c71SGlenn Strauss
430b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
431010c2894SGlenn Strauss "error parsing auth.require 'require' field: "
432010c2894SGlenn Strauss "invalid/unsupported token "
433010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). "
434010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str);
4357ba06c71SGlenn Strauss return 0;
4367ba06c71SGlenn Strauss
4377ba06c71SGlenn Strauss } while (p && *((str = p+1)));
4387ba06c71SGlenn Strauss
4397ba06c71SGlenn Strauss return 1; /* success */
4407ba06c71SGlenn Strauss }
4417ba06c71SGlenn Strauss
mod_auth_require_parse_array(const array * value,array * const auth_require,log_error_st * errh)442b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh)
4438e713130SGlenn Strauss {
4448e713130SGlenn Strauss for (uint32_t n = 0; n < value->used; ++n) {
4457ba06c71SGlenn Strauss size_t m;
4468e713130SGlenn Strauss data_array *da_file = (data_array *)value->data[n];
4477ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL;
448e1188e77SGlenn Strauss const buffer *nonce_secret = NULL;
44971175df1SGlenn Strauss data_unset *userhash = NULL;
4507ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme;
451b9e2be50SGlenn Strauss buffer *algos = NULL;
452b9e2be50SGlenn Strauss int algorithm = HTTP_AUTH_DIGEST_SESS;
4537ba06c71SGlenn Strauss
454c2238256SGlenn Strauss if (!array_is_kvstring(&da_file->value)) {
455b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
456010c2894SGlenn Strauss "unexpected value for auth.require; expected "
457bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )");
4587ba06c71SGlenn Strauss
4597ba06c71SGlenn Strauss return HANDLER_ERROR;
4607ba06c71SGlenn Strauss }
4617ba06c71SGlenn Strauss
462c2238256SGlenn Strauss for (m = 0; m < da_file->value.used; m++) {
463c2238256SGlenn Strauss if (da_file->value.data[m]->type == TYPE_STRING) {
464c2238256SGlenn Strauss data_string *ds = (data_string *)da_file->value.data[m];
465ad9b7e00SGlenn Strauss if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) {
466601c572cSGlenn Strauss method = &ds->value;
467ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) {
468601c572cSGlenn Strauss realm = &ds->value;
469ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) {
470601c572cSGlenn Strauss require = &ds->value;
471ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) {
472601c572cSGlenn Strauss algos = &ds->value;
47333ae8302SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce_secret"))
47433ae8302SGlenn Strauss || buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce-secret"))) {
475e1188e77SGlenn Strauss nonce_secret = &ds->value;
47671175df1SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("userhash"))) {
47771175df1SGlenn Strauss userhash = (data_unset *)ds;
4787ba06c71SGlenn Strauss } else {
479b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
480010c2894SGlenn Strauss "the field is unknown in: "
481010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )",
482010c2894SGlenn Strauss da_file->value.data[m]->key.ptr);
4837ba06c71SGlenn Strauss
4847ba06c71SGlenn Strauss return HANDLER_ERROR;
4857ba06c71SGlenn Strauss }
4867ba06c71SGlenn Strauss } else {
487b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
488010c2894SGlenn Strauss "a string was expected for: "
489010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )",
490010c2894SGlenn Strauss da_file->value.data[m]->key.ptr);
4917ba06c71SGlenn Strauss
4927ba06c71SGlenn Strauss return HANDLER_ERROR;
4937ba06c71SGlenn Strauss }
4947ba06c71SGlenn Strauss }
4957ba06c71SGlenn Strauss
496af3df29aSGlenn Strauss if (!method || buffer_is_blank(method)) {
497b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
498010c2894SGlenn Strauss "the method field is missing or blank in: "
4997ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )");
5007ba06c71SGlenn Strauss return HANDLER_ERROR;
5017ba06c71SGlenn Strauss } else {
5027ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method);
5037ba06c71SGlenn Strauss if (NULL == auth_scheme) {
504b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
505010c2894SGlenn Strauss "unknown method %s (e.g. \"basic\", \"digest\" or \"extern\") in "
506010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )", method->ptr);
5077ba06c71SGlenn Strauss return HANDLER_ERROR;
5087ba06c71SGlenn Strauss }
5097ba06c71SGlenn Strauss }
5107ba06c71SGlenn Strauss
511af3df29aSGlenn Strauss if (!realm) {
512b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
513010c2894SGlenn Strauss "the realm field is missing in: "
5147ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )");
5157ba06c71SGlenn Strauss return HANDLER_ERROR;
5167ba06c71SGlenn Strauss }
5177ba06c71SGlenn Strauss
518af3df29aSGlenn Strauss if (!require || buffer_is_blank(require)) {
519b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
520010c2894SGlenn Strauss "the require field is missing or blank in: "
5217ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )");
5227ba06c71SGlenn Strauss return HANDLER_ERROR;
5237ba06c71SGlenn Strauss }
5247ba06c71SGlenn Strauss
525af3df29aSGlenn Strauss if (!algos || buffer_is_blank(algos)) {
526b9e2be50SGlenn Strauss algorithm |= HTTP_AUTH_DIGEST_MD5;
527b9e2be50SGlenn Strauss } else if (!mod_auth_algorithms_parse(&algorithm, algos)) {
528b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__,
529010c2894SGlenn Strauss "invalid algorithm in: "
530b9e2be50SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )");
531b9e2be50SGlenn Strauss return HANDLER_ERROR;
532b9e2be50SGlenn Strauss }
533b9e2be50SGlenn Strauss
534609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/
5357ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init();
536ad9b7e00SGlenn Strauss buffer_copy_buffer(&dauth->key, &da_file->key);
5377ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme;
538b9e2be50SGlenn Strauss dauth->require->algorithm = algorithm;
539a03afc90SGlenn Strauss dauth->require->realm = realm;
540e1188e77SGlenn Strauss dauth->require->nonce_secret = nonce_secret; /*(NULL is ok)*/
54171175df1SGlenn Strauss dauth->require->userhash = config_plugin_value_tobool(userhash, 0);
542b5775b99SGlenn Strauss if (!mod_auth_require_parse(dauth->require, require, errh)) {
5438c7f1dfbSGlenn Strauss dauth->fn->free((data_unset *)dauth);
5447ba06c71SGlenn Strauss return HANDLER_ERROR;
5457ba06c71SGlenn Strauss }
5468e713130SGlenn Strauss array_insert_unique(auth_require, (data_unset *)dauth);
5477ba06c71SGlenn Strauss }
5487ba06c71SGlenn Strauss }
5497ba06c71SGlenn Strauss
5507ba06c71SGlenn Strauss return HANDLER_GO_ON;
5517ba06c71SGlenn Strauss }
5527ba06c71SGlenn Strauss
mod_auth_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)5538e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
5548e713130SGlenn Strauss switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
5558e713130SGlenn Strauss case 0: /* auth.backend */
5568e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL)
5578e713130SGlenn Strauss pconf->auth_backend = cpv->v.v;
5588e713130SGlenn Strauss break;
5598e713130SGlenn Strauss case 1: /* auth.require */
5608e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL)
5618e713130SGlenn Strauss pconf->auth_require = cpv->v.v;
5628e713130SGlenn Strauss break;
5638e713130SGlenn Strauss case 2: /* auth.extern-authn */
5648e713130SGlenn Strauss pconf->auth_extern_authn = cpv->v.u;
565e11514b0SGlenn Strauss break;
566e11514b0SGlenn Strauss case 3: /* auth.cache */
567e11514b0SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL)
568e11514b0SGlenn Strauss pconf->auth_cache = cpv->v.v;
569e11514b0SGlenn Strauss break;
5708e713130SGlenn Strauss default:/* should not happen */
5718e713130SGlenn Strauss return;
5728e713130SGlenn Strauss }
5738e713130SGlenn Strauss }
5744a6ce4aeSJan Kneschke
mod_auth_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)5758e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
5768e713130SGlenn Strauss do {
5778e713130SGlenn Strauss mod_auth_merge_config_cpv(pconf, cpv);
5788e713130SGlenn Strauss } while ((++cpv)->k_id != -1);
5798e713130SGlenn Strauss }
580bcdc6a3bSJan Kneschke
mod_auth_patch_config(request_st * const r,plugin_data * const p)5817c7f8c46SGlenn Strauss static void mod_auth_patch_config(request_st * const r, plugin_data * const p) {
582cc2134c8SGlenn Strauss p->conf = p->defaults; /* copy small struct instead of memcpy() */
583cc2134c8SGlenn Strauss /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
5848e713130SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) {
5857c7f8c46SGlenn Strauss if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
5868e713130SGlenn Strauss mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
5878e713130SGlenn Strauss }
5888e713130SGlenn Strauss }
58936f64b26SGlenn Strauss
SETDEFAULTS_FUNC(mod_auth_set_defaults)5908e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) {
5918e713130SGlenn Strauss static const config_plugin_keys_t cpk[] = {
5928e713130SGlenn Strauss { CONST_STR_LEN("auth.backend"),
5938e713130SGlenn Strauss T_CONFIG_STRING,
5948e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
5958e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.require"),
59603b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY,
5978e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
5988e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.extern-authn"),
5998e713130SGlenn Strauss T_CONFIG_BOOL,
6008e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
601e11514b0SGlenn Strauss ,{ CONST_STR_LEN("auth.cache"),
602e11514b0SGlenn Strauss T_CONFIG_ARRAY,
603e11514b0SGlenn Strauss T_CONFIG_SCOPE_CONNECTION }
6048e713130SGlenn Strauss ,{ NULL, 0,
6058e713130SGlenn Strauss T_CONFIG_UNSET,
6068e713130SGlenn Strauss T_CONFIG_SCOPE_UNSET }
6078e713130SGlenn Strauss };
608bcdc6a3bSJan Kneschke
6098e713130SGlenn Strauss plugin_data * const p = p_d;
6108e713130SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_auth"))
6118e713130SGlenn Strauss return HANDLER_ERROR;
612bcdc6a3bSJan Kneschke
6138e713130SGlenn Strauss /* process and validate config directives
6148e713130SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */
6158e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
6168e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
6178e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) {
6188e713130SGlenn Strauss switch (cpv->k_id) {
6198e713130SGlenn Strauss case 0: /* auth.backend */
620af3df29aSGlenn Strauss if (!buffer_is_blank(cpv->v.b)) {
6218e713130SGlenn Strauss const http_auth_backend_t * const auth_backend =
6228e713130SGlenn Strauss http_auth_backend_get(cpv->v.b);
6238e713130SGlenn Strauss if (NULL == auth_backend) {
6248e713130SGlenn Strauss log_error(srv->errh, __FILE__, __LINE__,
6258e713130SGlenn Strauss "auth.backend not supported: %s", cpv->v.b->ptr);
6268e713130SGlenn Strauss return HANDLER_ERROR;
6278e713130SGlenn Strauss }
6288e713130SGlenn Strauss *(const http_auth_backend_t **)&cpv->v.v = auth_backend;
6298e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL;
6308e713130SGlenn Strauss }
6318e713130SGlenn Strauss break;
6328e713130SGlenn Strauss case 1: /* auth.require */
63303b4c993SGlenn Strauss {
63424680a91SGlenn Strauss array * const a = array_init(4);
6358e713130SGlenn Strauss if (HANDLER_GO_ON !=
636b5775b99SGlenn Strauss mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) {
6378e713130SGlenn Strauss array_free(a);
6388e713130SGlenn Strauss return HANDLER_ERROR;
6398e713130SGlenn Strauss }
6408e713130SGlenn Strauss cpv->v.a = a;
6418e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL;
6428e713130SGlenn Strauss }
6438e713130SGlenn Strauss break;
6448e713130SGlenn Strauss case 2: /* auth.extern-authn */
6458e713130SGlenn Strauss break;
646e11514b0SGlenn Strauss case 3: /* auth.cache */
647e11514b0SGlenn Strauss cpv->v.v = http_auth_cache_init(cpv->v.a);
648e11514b0SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL;
649e11514b0SGlenn Strauss break;
6508e713130SGlenn Strauss default:/* should not happen */
6518e713130SGlenn Strauss break;
652bcdc6a3bSJan Kneschke }
653bcdc6a3bSJan Kneschke }
654bcdc6a3bSJan Kneschke }
655bcdc6a3bSJan Kneschke
6568e713130SGlenn Strauss /* initialize p->defaults from global config context */
6578e713130SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
6588e713130SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
6598e713130SGlenn Strauss if (-1 != cpv->k_id)
6608e713130SGlenn Strauss mod_auth_merge_config(&p->defaults, cpv);
661bcdc6a3bSJan Kneschke }
6628e713130SGlenn Strauss
6638e713130SGlenn Strauss return HANDLER_GO_ON;
6648e713130SGlenn Strauss }
665bcdc6a3bSJan Kneschke
mod_auth_uri_handler(request_st * const r,void * p_d)6667c7f8c46SGlenn Strauss static handler_t mod_auth_uri_handler(request_st * const r, void *p_d) {
6677ba06c71SGlenn Strauss plugin_data *p = p_d;
668d61f3381SGlenn Strauss data_auth *dauth;
66981b2d1f0SGlenn Strauss
6707c7f8c46SGlenn Strauss mod_auth_patch_config(r, p);
67181b2d1f0SGlenn Strauss
6727ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON;
67381b2d1f0SGlenn Strauss
6747ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */
6757ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */
6767c7f8c46SGlenn Strauss dauth = (!r->conf.force_lowercase_filenames)
6777c7f8c46SGlenn Strauss ? (data_auth *)array_match_key_prefix(p->conf.auth_require, &r->uri.path)
6787c7f8c46SGlenn Strauss : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, &r->uri.path);
679d61f3381SGlenn Strauss if (NULL == dauth) return HANDLER_GO_ON;
6807ba06c71SGlenn Strauss
681d61f3381SGlenn Strauss {
6827ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme;
683f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) {
6847c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER"));
6853dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) {
686f54d628cSGlenn Strauss return HANDLER_GO_ON;
687f54d628cSGlenn Strauss }
688f54d628cSGlenn Strauss }
6897c7f8c46SGlenn Strauss return scheme->checkfn(r, scheme->p_d, dauth->require, p->conf.auth_backend);
6907ba06c71SGlenn Strauss }
6917ba06c71SGlenn Strauss }
6927ba06c71SGlenn Strauss
693e11514b0SGlenn Strauss
694b82d7b8aSGlenn Strauss __attribute_cold__
6957ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p);
mod_auth_plugin_init(plugin * p)6967ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) {
6977ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID;
698e2de4e58SGlenn Strauss p->name = "auth";
6997ba06c71SGlenn Strauss p->init = mod_auth_init;
7007ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults;
701e11514b0SGlenn Strauss p->handle_trigger = mod_auth_periodic;
7027ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler;
7037ba06c71SGlenn Strauss p->cleanup = mod_auth_free;
7047ba06c71SGlenn Strauss
70581b2d1f0SGlenn Strauss return 0;
70681b2d1f0SGlenn Strauss }
70781b2d1f0SGlenn Strauss
70881b2d1f0SGlenn Strauss
70981b2d1f0SGlenn Strauss
71081b2d1f0SGlenn Strauss
7117ba06c71SGlenn Strauss /*
7127ba06c71SGlenn Strauss * auth schemes (basic, digest, extern)
7137ba06c71SGlenn Strauss *
7147ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs)
7157ba06c71SGlenn Strauss */
71681b2d1f0SGlenn Strauss
7171fc8a3e1SGlenn Strauss #include "sys-crypto-md.h"
7187ba06c71SGlenn Strauss #include "base64.h"
7197f4e156eSGlenn Strauss #include "rand.h"
7203dd3cde9SGlenn Strauss #include "http_header.h"
72181b2d1f0SGlenn Strauss
722010c2894SGlenn Strauss __attribute_cold__
723ffc7a0bfSGlenn Strauss __attribute_noinline__
72456e2881cSGlenn Strauss static handler_t
mod_auth_send_400_bad_request(request_st * const r)72556e2881cSGlenn Strauss mod_auth_send_400_bad_request (request_st * const r)
72656e2881cSGlenn Strauss {
7274b3a91e6SGlenn Strauss /* a field was missing or invalid */
7287c7f8c46SGlenn Strauss r->http_status = 400; /* Bad Request */
7297c7f8c46SGlenn Strauss r->handler_module = NULL;
7304b3a91e6SGlenn Strauss return HANDLER_FINISHED;
7314b3a91e6SGlenn Strauss }
7324b3a91e6SGlenn Strauss
73320c56cecSGlenn Strauss
73420c56cecSGlenn Strauss
735ffc7a0bfSGlenn Strauss __attribute_noinline__
73656e2881cSGlenn Strauss static handler_t
mod_auth_send_401_unauthorized_basic(request_st * const r,const buffer * const realm)73756e2881cSGlenn Strauss mod_auth_send_401_unauthorized_basic (request_st * const r, const buffer * const realm)
73856e2881cSGlenn Strauss {
7397c7f8c46SGlenn Strauss r->http_status = 401;
7407c7f8c46SGlenn Strauss r->handler_module = NULL;
741dc01487eSGlenn Strauss buffer_append_str3(
74226f354cbSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE,
743dc01487eSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")),
744dc01487eSGlenn Strauss CONST_STR_LEN("Basic realm=\""),
745af3df29aSGlenn Strauss BUF_PTR_LEN(realm),
746dc01487eSGlenn Strauss CONST_STR_LEN("\", charset=\"UTF-8\""));
7474b3a91e6SGlenn Strauss return HANDLER_FINISHED;
7484b3a91e6SGlenn Strauss }
7494b3a91e6SGlenn Strauss
75081b2d1f0SGlenn Strauss
75156e2881cSGlenn Strauss __attribute_cold__
75256e2881cSGlenn Strauss static handler_t
mod_auth_basic_misconfigured(request_st * const r,const struct http_auth_backend_t * const backend)75356e2881cSGlenn Strauss mod_auth_basic_misconfigured (request_st * const r, const struct http_auth_backend_t * const backend)
75456e2881cSGlenn Strauss {
755cf0098eaSGlenn Strauss if (NULL == backend)
756cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
757cf0098eaSGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr);
758cf0098eaSGlenn Strauss else
759cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
760cf0098eaSGlenn Strauss "auth.require \"method\" => \"basic\" invalid "
76156e2881cSGlenn Strauss "(try \"digest\"?) for %s", r->uri.path.ptr);
76256e2881cSGlenn Strauss
7637c7f8c46SGlenn Strauss r->http_status = 500;
7647c7f8c46SGlenn Strauss r->handler_module = NULL;
7657ba06c71SGlenn Strauss return HANDLER_FINISHED;
7667ba06c71SGlenn Strauss }
7677ba06c71SGlenn Strauss
76856e2881cSGlenn Strauss
76956e2881cSGlenn 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)77056e2881cSGlenn Strauss 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)
77156e2881cSGlenn Strauss {
77256e2881cSGlenn Strauss if (NULL == backend || NULL == backend->basic)
77356e2881cSGlenn Strauss return mod_auth_basic_misconfigured(r, backend);
77456e2881cSGlenn Strauss
77556e2881cSGlenn Strauss const buffer * const vb =
776ffc7a0bfSGlenn Strauss http_header_request_get(r, HTTP_HEADER_AUTHORIZATION,
777ffc7a0bfSGlenn Strauss CONST_STR_LEN("Authorization"));
77856e2881cSGlenn Strauss if (NULL == vb || !buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Basic ")))
7797c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm);
7809bc61f16SGlenn Strauss #ifdef __COVERITY__
78156e2881cSGlenn Strauss if (buffer_clen(vb) < sizeof("Basic ")-1)
7827c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
7839bc61f16SGlenn Strauss #endif
78481b2d1f0SGlenn Strauss
78556e2881cSGlenn Strauss size_t ulen = buffer_clen(vb) - (sizeof("Basic ")-1);
78656e2881cSGlenn Strauss size_t pwlen;
78756e2881cSGlenn Strauss char *pw;
78856e2881cSGlenn Strauss char user[1024];
78981b2d1f0SGlenn Strauss
79056e2881cSGlenn Strauss /* base64-decode Authorization into username:password string;
79156e2881cSGlenn Strauss * limit base64-decoded username:password string to fit into 1k buf */
79256e2881cSGlenn Strauss if (ulen > 1363) /*(1363/4*3+3 = 1023)*/
79356e2881cSGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm);
7948047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */
79556e2881cSGlenn Strauss ulen = li_base64_dec((unsigned char *)user, sizeof(user),
79656e2881cSGlenn Strauss vb->ptr+sizeof("Basic ")-1, ulen, BASE64_STANDARD);
79756e2881cSGlenn Strauss if (0 == ulen) {
79856e2881cSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
79956e2881cSGlenn Strauss "decoding base64-string failed %s", vb->ptr+sizeof("Basic ")-1);
8007c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
80181b2d1f0SGlenn Strauss }
80256e2881cSGlenn Strauss user[ulen] = '\0';
80356e2881cSGlenn Strauss pw = memchr(user, ':', ulen);
80456e2881cSGlenn Strauss if (NULL == pw) {
80556e2881cSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "missing ':' in %s", user);
8067c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
80781b2d1f0SGlenn Strauss }
80856e2881cSGlenn Strauss *pw++ = '\0';
80956e2881cSGlenn Strauss pwlen = (size_t)(user + ulen - pw);
81056e2881cSGlenn Strauss ulen = (size_t)(pw - 1 - user);
81181b2d1f0SGlenn Strauss
812e11514b0SGlenn Strauss plugin_data * const p = p_d;
813e11514b0SGlenn Strauss splay_tree ** sptree = p->conf.auth_cache
814e11514b0SGlenn Strauss ? &p->conf.auth_cache->sptree
815e11514b0SGlenn Strauss : NULL;
816e11514b0SGlenn Strauss http_auth_cache_entry *ae = NULL;
817fd0cc468SGlenn Strauss handler_t rc = HANDLER_ERROR;
818e11514b0SGlenn Strauss int ndx = -1;
819e11514b0SGlenn Strauss if (sptree) {
82056e2881cSGlenn Strauss ndx = http_auth_cache_hash(require, user, ulen);
821e11514b0SGlenn Strauss ae = http_auth_cache_query(sptree, ndx);
822e11514b0SGlenn Strauss if (ae && ae->require == require
82356e2881cSGlenn Strauss && ulen == ae->ulen && 0 == memcmp(user, ae->username, ulen))
82462ccda85SGlenn Strauss rc = ck_memeq_const_time(ae->pwdigest, ae->dlen, pw, pwlen)
825e11514b0SGlenn Strauss ? HANDLER_GO_ON
826e11514b0SGlenn Strauss : HANDLER_ERROR;
827e11514b0SGlenn Strauss else /*(not found or hash collision)*/
828e11514b0SGlenn Strauss ae = NULL;
829e11514b0SGlenn Strauss }
830e11514b0SGlenn Strauss
83156e2881cSGlenn Strauss if (NULL == ae) {
83256e2881cSGlenn Strauss const buffer userb = { user, ulen+1, 0 };
83356e2881cSGlenn Strauss rc = backend->basic(r, backend->p_d, require, &userb, pw);
83456e2881cSGlenn Strauss }
835e11514b0SGlenn Strauss
8367ba06c71SGlenn Strauss switch (rc) {
8374b3a91e6SGlenn Strauss case HANDLER_GO_ON:
83856e2881cSGlenn Strauss http_auth_setenv(r, user, ulen, CONST_STR_LEN("Basic"));
839e11514b0SGlenn Strauss if (sptree && NULL == ae) { /*(cache (new) successful result)*/
84071175df1SGlenn Strauss ae = http_auth_cache_entry_init(require, 0, user, ulen, user, ulen,
84171175df1SGlenn Strauss pw, pwlen);
842e11514b0SGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free);
843e11514b0SGlenn Strauss }
8444b3a91e6SGlenn Strauss break;
8454b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT:
8464b3a91e6SGlenn Strauss case HANDLER_FINISHED:
8477ba06c71SGlenn Strauss break;
8484b3a91e6SGlenn Strauss case HANDLER_ERROR:
8494b3a91e6SGlenn Strauss default:
8507c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
851010c2894SGlenn Strauss "password doesn't match for %s username: %s IP: %s",
852*3a8fc4bcSGlenn Strauss r->uri.path.ptr, user, r->dst_addr_buf->ptr);
8534a600dabSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/
854fd0cc468SGlenn Strauss rc = mod_auth_send_401_unauthorized_basic(r, require->realm);
8557ba06c71SGlenn Strauss break;
85681b2d1f0SGlenn Strauss }
85781b2d1f0SGlenn Strauss
85808c03cd4SGlenn Strauss ck_memzero(pw, pwlen);
859fd0cc468SGlenn Strauss return rc;
86081b2d1f0SGlenn Strauss }
86181b2d1f0SGlenn Strauss
86281b2d1f0SGlenn Strauss
86320c56cecSGlenn Strauss
86420c56cecSGlenn Strauss enum http_auth_digest_params_e {
86520c56cecSGlenn Strauss e_username = 0
86620c56cecSGlenn Strauss ,e_realm
86720c56cecSGlenn Strauss ,e_nonce
86820c56cecSGlenn Strauss ,e_uri
86920c56cecSGlenn Strauss ,e_algorithm
87020c56cecSGlenn Strauss ,e_qop
87120c56cecSGlenn Strauss ,e_cnonce
87220c56cecSGlenn Strauss ,e_nc
87320c56cecSGlenn Strauss ,e_response
87471175df1SGlenn Strauss ,e_userstar
87571175df1SGlenn Strauss ,e_userhash
87620c56cecSGlenn Strauss ,http_auth_digest_params_sz /*(last item)*/
87720c56cecSGlenn Strauss };
87820c56cecSGlenn Strauss
87920c56cecSGlenn Strauss typedef struct http_auth_digest_params_t {
88020c56cecSGlenn Strauss const char *ptr[http_auth_digest_params_sz];
88120c56cecSGlenn Strauss uint16_t len[http_auth_digest_params_sz];
882309c1693SGlenn Strauss unix_time64_t send_nextnonce_ts;
88371175df1SGlenn Strauss unsigned char rdigest[MD_DIGEST_LENGTH_MAX];/*(earlier members get 0-init)*/
88420c56cecSGlenn Strauss } http_auth_digest_params_t;
88520c56cecSGlenn Strauss
88620c56cecSGlenn Strauss
88720c56cecSGlenn Strauss static void
mod_auth_digest_mutate(http_auth_info_t * const ai,const http_auth_digest_params_t * const dp,const buffer * const method)8889fe8fbaaSGlenn Strauss mod_auth_digest_mutate (http_auth_info_t * const ai, const http_auth_digest_params_t * const dp, const buffer * const method)
88920c56cecSGlenn Strauss {
89020c56cecSGlenn Strauss force_assert(method);
891c987bec1SGlenn Strauss li_md_iov_fn digest_iov = MD5_iov;
892c987bec1SGlenn Strauss /* (ai->dalgo & HTTP_AUTH_DIGEST_MD5) default */
893b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO
894c987bec1SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256)
895c987bec1SGlenn Strauss digest_iov = SHA256_iov;
8961fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256
897b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256)
898c987bec1SGlenn Strauss digest_iov = SHA512_256_iov;
899b9e2be50SGlenn Strauss #endif
900b9e2be50SGlenn Strauss #endif
901c987bec1SGlenn Strauss size_t n;
902c987bec1SGlenn Strauss struct const_iovec iov[11];
903c987bec1SGlenn Strauss char a1[MD_DIGEST_LENGTH_MAX*2+8]; /*(+1 for li_tohex(); +8 for align)*/
904c987bec1SGlenn Strauss char a2[MD_DIGEST_LENGTH_MAX*2+8]; /*(+1 for li_tohex(); +8 for align)*/
905c987bec1SGlenn Strauss
906c987bec1SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
907c987bec1SGlenn Strauss
908c987bec1SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) {
909c987bec1SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617
910c987bec1SGlenn Strauss * Errata ID: 1649 */
911c987bec1SGlenn Strauss iov[0].iov_base = a1;
912c987bec1SGlenn Strauss iov[0].iov_len = ai->dlen*2;
913c987bec1SGlenn Strauss iov[1].iov_base = ":";
914c987bec1SGlenn Strauss iov[1].iov_len = 1;
91520c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_nonce];
91620c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_nonce];
917c987bec1SGlenn Strauss iov[3].iov_base = ":";
918c987bec1SGlenn Strauss iov[3].iov_len = 1;
91920c56cecSGlenn Strauss iov[4].iov_base = dp->ptr[e_cnonce];
92020c56cecSGlenn Strauss iov[4].iov_len = dp->len[e_cnonce];
921c987bec1SGlenn Strauss digest_iov(ai->digest, iov, 5);
922c987bec1SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen);
923c987bec1SGlenn Strauss }
924c987bec1SGlenn Strauss
925c987bec1SGlenn Strauss /* calculate H(A2) */
9269fe8fbaaSGlenn Strauss iov[0].iov_base = method->ptr;
9279fe8fbaaSGlenn Strauss iov[0].iov_len = buffer_clen(method);
928c987bec1SGlenn Strauss iov[1].iov_base = ":";
929c987bec1SGlenn Strauss iov[1].iov_len = 1;
93020c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_uri];
93120c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_uri];
932c987bec1SGlenn Strauss n = 3;
933c987bec1SGlenn Strauss #if 0
934c987bec1SGlenn Strauss /* qop=auth-int not supported, already checked in caller */
93520c56cecSGlenn Strauss if (dp->ptr[e_qop] && buffer_eq_icase_ss(dp->ptr[e_qop], dp->len[e_qop],
93620c56cecSGlenn Strauss CONST_STR_LEN("auth-int"))) {
937c987bec1SGlenn Strauss iov[3].iov_base = ":";
938c987bec1SGlenn Strauss iov[3].iov_len = 1;
939c987bec1SGlenn Strauss iov[4].iov_base = [body checksum];
940c987bec1SGlenn Strauss iov[4].iov_len = ai->dlen*2;
941c987bec1SGlenn Strauss n = 5;
942c987bec1SGlenn Strauss }
943c987bec1SGlenn Strauss #endif
944c987bec1SGlenn Strauss digest_iov(ai->digest, iov, n);
945c987bec1SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen);
946c987bec1SGlenn Strauss
947c987bec1SGlenn Strauss /* calculate response */
948c987bec1SGlenn Strauss iov[0].iov_base = a1;
949c987bec1SGlenn Strauss iov[0].iov_len = ai->dlen*2;
950c987bec1SGlenn Strauss iov[1].iov_base = ":";
951c987bec1SGlenn Strauss iov[1].iov_len = 1;
95220c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_nonce];
95320c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_nonce];
954c987bec1SGlenn Strauss iov[3].iov_base = ":";
955c987bec1SGlenn Strauss iov[3].iov_len = 1;
956c987bec1SGlenn Strauss n = 4;
95720c56cecSGlenn Strauss if (dp->len[e_qop]) {
95820c56cecSGlenn Strauss iov[4].iov_base = dp->ptr[e_nc];
95920c56cecSGlenn Strauss iov[4].iov_len = dp->len[e_nc];
960c987bec1SGlenn Strauss iov[5].iov_base = ":";
961c987bec1SGlenn Strauss iov[5].iov_len = 1;
96220c56cecSGlenn Strauss iov[6].iov_base = dp->ptr[e_cnonce];
96320c56cecSGlenn Strauss iov[6].iov_len = dp->len[e_cnonce];
964c987bec1SGlenn Strauss iov[7].iov_base = ":";
965c987bec1SGlenn Strauss iov[7].iov_len = 1;
96620c56cecSGlenn Strauss iov[8].iov_base = dp->ptr[e_qop];
96720c56cecSGlenn Strauss iov[8].iov_len = dp->len[e_qop];
968c987bec1SGlenn Strauss iov[9].iov_base = ":";
969c987bec1SGlenn Strauss iov[9].iov_len = 1;
970c987bec1SGlenn Strauss n = 10;
971c987bec1SGlenn Strauss }
972c987bec1SGlenn Strauss iov[n].iov_base = a2;
973c987bec1SGlenn Strauss iov[n].iov_len = ai->dlen*2;
974c987bec1SGlenn Strauss digest_iov(ai->digest, iov, n+1);
975b9e2be50SGlenn Strauss }
976b9e2be50SGlenn Strauss
97720c56cecSGlenn Strauss
97820c56cecSGlenn Strauss static void
mod_auth_append_nonce(buffer * b,unix_time64_t cur_ts,const struct http_auth_require_t * require,int dalgo,int * rndptr)979309c1693SGlenn Strauss mod_auth_append_nonce (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int dalgo, int *rndptr)
98020c56cecSGlenn Strauss {
981e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts);
982f2610d23SGlenn Strauss buffer_append_char(b, ':');
983e1188e77SGlenn Strauss const buffer * const nonce_secret = require->nonce_secret;
984e1188e77SGlenn Strauss int rnd;
985e1188e77SGlenn Strauss if (NULL == nonce_secret)
986e1188e77SGlenn Strauss rnd = rndptr ? *rndptr : li_rand_pseudo();
987e1188e77SGlenn Strauss else { /*(do not directly expose random number generator single value)*/
988e1188e77SGlenn Strauss rndptr
989e1188e77SGlenn Strauss ? (void)(rnd = *rndptr)
990e1188e77SGlenn Strauss : li_rand_pseudo_bytes((unsigned char *)&rnd, sizeof(rnd));
991e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)rnd);
992f2610d23SGlenn Strauss buffer_append_char(b, ':');
993e1188e77SGlenn Strauss }
994c987bec1SGlenn Strauss
995c987bec1SGlenn Strauss size_t n;
996c987bec1SGlenn Strauss struct const_iovec iov[3];
997c987bec1SGlenn Strauss
998c987bec1SGlenn Strauss #if 0
999c987bec1SGlenn Strauss char a1[LI_ITOSTRING_LENGTH];
1000c987bec1SGlenn Strauss char a2[LI_ITOSTRING_LENGTH];
1001c987bec1SGlenn Strauss iov[0].iov_base = a1;
1002c987bec1SGlenn Strauss iov[0].iov_len = li_itostrn(a1, sizeof(a1), cur_ts);
1003c987bec1SGlenn Strauss iov[1].iov_base = a2;
1004c987bec1SGlenn Strauss iov[1].iov_len = li_itostrn(a2, sizeof(a2), rnd);
1005c987bec1SGlenn Strauss #else
1006c987bec1SGlenn Strauss iov[0].iov_base = &cur_ts;
1007c987bec1SGlenn Strauss iov[0].iov_len = sizeof(cur_ts);
1008c987bec1SGlenn Strauss iov[1].iov_base = &rnd;
1009c987bec1SGlenn Strauss iov[1].iov_len = sizeof(rnd);
1010c987bec1SGlenn Strauss #endif
1011c987bec1SGlenn Strauss n = 2;
1012c987bec1SGlenn Strauss if (nonce_secret) {
1013c987bec1SGlenn Strauss iov[2].iov_base = nonce_secret->ptr;
1014c987bec1SGlenn Strauss iov[2].iov_len = buffer_clen(nonce_secret);
1015c987bec1SGlenn Strauss n = 3;
1016c987bec1SGlenn Strauss }
1017c987bec1SGlenn Strauss
1018c987bec1SGlenn Strauss unsigned char h[MD_DIGEST_LENGTH_MAX];
1019e1188e77SGlenn Strauss switch (dalgo) {
1020b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO
10211fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256
1022e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256:
1023c987bec1SGlenn Strauss SHA512_256_iov(h, iov, n);
1024c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_SHA512_256_BINLEN;
1025e1188e77SGlenn Strauss break;
1026e1188e77SGlenn Strauss #endif
1027e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256:
1028c987bec1SGlenn Strauss SHA256_iov(h, iov, n);
1029c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_SHA256_BINLEN;
1030e1188e77SGlenn Strauss break;
1031e1188e77SGlenn Strauss #endif
1032e1188e77SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/
1033e1188e77SGlenn Strauss default:
1034c987bec1SGlenn Strauss MD5_iov(h, iov, n);
1035c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_MD5_BINLEN;
1036e1188e77SGlenn Strauss break;
1037e1188e77SGlenn Strauss }
1038c987bec1SGlenn Strauss li_tohex(buffer_extend(b, n*2), n*2+1, (const char *)h, n);
1039e1188e77SGlenn Strauss }
1040e1188e77SGlenn Strauss
104120c56cecSGlenn Strauss
104220c56cecSGlenn Strauss static void
mod_auth_digest_www_authenticate(buffer * b,unix_time64_t cur_ts,const struct http_auth_require_t * require,int nonce_stale)1043309c1693SGlenn Strauss mod_auth_digest_www_authenticate (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int nonce_stale)
104420c56cecSGlenn Strauss {
1045b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm;
1046b9e2be50SGlenn Strauss int n = 0;
1047e1188e77SGlenn Strauss int algoid[3];
1048b9e2be50SGlenn Strauss unsigned int algolen[3];
1049b9e2be50SGlenn Strauss const char *algoname[3];
1050b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO
10511fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256
1052b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) {
1053e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA512_256;
1054b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256";
1055b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1;
1056b9e2be50SGlenn Strauss ++n;
1057b9e2be50SGlenn Strauss }
1058b9e2be50SGlenn Strauss #endif
1059b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) {
1060e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA256;
1061b9e2be50SGlenn Strauss algoname[n] = "SHA-256";
1062b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1;
1063b9e2be50SGlenn Strauss ++n;
1064b9e2be50SGlenn Strauss }
1065b9e2be50SGlenn Strauss #endif
1066b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) {
1067e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_MD5;
1068b9e2be50SGlenn Strauss algoname[n] = "MD5";
1069b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1;
1070b9e2be50SGlenn Strauss ++n;
1071b9e2be50SGlenn Strauss }
1072b9e2be50SGlenn Strauss
1073b9e2be50SGlenn Strauss buffer_clear(b);
1074b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) {
1075dc01487eSGlenn Strauss struct const_iovec iov[] = {
1076dc01487eSGlenn Strauss { CONST_STR_LEN("\r\nWWW-Authenticate: ") }
1077dc01487eSGlenn Strauss ,{ CONST_STR_LEN("Digest realm=\"") }
1078af3df29aSGlenn Strauss ,{ BUF_PTR_LEN(require->realm) }
1079dc01487eSGlenn Strauss ,{ CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=") }
1080dc01487eSGlenn Strauss ,{ algoname[i], algolen[i] }
1081dc01487eSGlenn Strauss ,{ CONST_STR_LEN(", nonce=\"") }
1082dc01487eSGlenn Strauss };
1083dc01487eSGlenn Strauss buffer_append_iovec(b, iov+(0==i), sizeof(iov)/sizeof(*iov)-(0==i));
1084e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, algoid[i], NULL);
1085b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\""));
108671175df1SGlenn Strauss if (require->userhash) {
108771175df1SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", userhash=true"));
108871175df1SGlenn Strauss }
1089b9e2be50SGlenn Strauss if (nonce_stale) {
1090b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true"));
1091b9e2be50SGlenn Strauss }
1092b9e2be50SGlenn Strauss }
109381b2d1f0SGlenn Strauss }
109481b2d1f0SGlenn Strauss
1095c841ce3bSGlenn Strauss
109620c56cecSGlenn Strauss __attribute_noinline__
109720c56cecSGlenn 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)109820c56cecSGlenn Strauss mod_auth_send_401_unauthorized_digest(request_st * const r, const struct http_auth_require_t * const require, int nonce_stale)
109920c56cecSGlenn Strauss {
110020c56cecSGlenn Strauss r->http_status = 401;
110120c56cecSGlenn Strauss r->handler_module = NULL;
110220c56cecSGlenn Strauss mod_auth_digest_www_authenticate(
110320c56cecSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE,
110420c56cecSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")),
110520c56cecSGlenn Strauss log_epoch_secs, require, nonce_stale);
110620c56cecSGlenn Strauss return HANDLER_FINISHED;
110720c56cecSGlenn Strauss }
110820c56cecSGlenn Strauss
110920c56cecSGlenn Strauss
111020c56cecSGlenn Strauss static void
mod_auth_digest_authentication_info(buffer * b,unix_time64_t cur_ts,const struct http_auth_require_t * require,int dalgo)1111309c1693SGlenn Strauss mod_auth_digest_authentication_info (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int dalgo)
111220c56cecSGlenn Strauss {
11136ad325c6SGlenn Strauss buffer_clear(b);
11146ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\""));
1115e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, dalgo, NULL);
1116f2610d23SGlenn Strauss buffer_append_char(b, '"');
11176ad325c6SGlenn Strauss }
11186ad325c6SGlenn Strauss
111920c56cecSGlenn Strauss
1120c841ce3bSGlenn Strauss static handler_t
mod_auth_digest_get(request_st * const r,void * p_d,const struct http_auth_require_t * const require,const struct http_auth_backend_t * const backend,http_auth_info_t * const ai)1121c841ce3bSGlenn Strauss mod_auth_digest_get (request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend, http_auth_info_t * const ai)
1122c841ce3bSGlenn Strauss {
1123c841ce3bSGlenn Strauss plugin_data * const p = p_d;
1124c841ce3bSGlenn Strauss splay_tree **sptree = p->conf.auth_cache
1125c841ce3bSGlenn Strauss ? &p->conf.auth_cache->sptree
1126c841ce3bSGlenn Strauss : NULL;
1127c841ce3bSGlenn Strauss http_auth_cache_entry *ae = NULL;
1128c841ce3bSGlenn Strauss handler_t rc = HANDLER_GO_ON;
1129c841ce3bSGlenn Strauss int ndx = -1;
113071175df1SGlenn Strauss
113171175df1SGlenn Strauss const char *user = ai->username;
113271175df1SGlenn Strauss const uint32_t ulen = ai->ulen;
113371175df1SGlenn Strauss char userbuf[sizeof(ai->userbuf)];
113471175df1SGlenn Strauss if (ai->userhash && ulen <= sizeof(userbuf)) {
113571175df1SGlenn Strauss /*(lowercase hex in userhash for consistency)*/
113671175df1SGlenn Strauss const char * const restrict s = ai->username;
113771175df1SGlenn Strauss for (uint_fast32_t i = 0; i < ulen; ++i)
113871175df1SGlenn Strauss userbuf[i] = !light_isupper(s[i]) ? s[i] : (s[i] | 0x20);
113971175df1SGlenn Strauss user = userbuf;
114071175df1SGlenn Strauss }
114171175df1SGlenn Strauss
1142c841ce3bSGlenn Strauss if (sptree) {
114371175df1SGlenn Strauss ndx = http_auth_cache_hash(require, user, ulen);
1144c841ce3bSGlenn Strauss ae = http_auth_cache_query(sptree, ndx);
1145c841ce3bSGlenn Strauss if (ae && ae->require == require
1146c841ce3bSGlenn Strauss && ae->dalgo == ai->dalgo
1147c841ce3bSGlenn Strauss && ae->dlen == ai->dlen
114871175df1SGlenn Strauss && ae->klen == ulen
114971175df1SGlenn Strauss && 0 == memcmp(ae->k, user, ulen)
115071175df1SGlenn Strauss && (ae->k == ae->username || ai->userhash)) {
1151c841ce3bSGlenn Strauss memcpy(ai->digest, ae->pwdigest, ai->dlen);
115271175df1SGlenn Strauss if (ae->k != ae->username) { /*(userhash was key; copy username)*/
115371175df1SGlenn Strauss if (__builtin_expect( (ae->ulen <= sizeof(ai->userbuf)), 1)) {
115471175df1SGlenn Strauss ai->ulen = ae->ulen;
115571175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, ae->username, ae->ulen);
115671175df1SGlenn Strauss }
115771175df1SGlenn Strauss }
1158c841ce3bSGlenn Strauss }
1159c841ce3bSGlenn Strauss else /*(not found or hash collision)*/
1160c841ce3bSGlenn Strauss ae = NULL;
1161c841ce3bSGlenn Strauss }
1162c841ce3bSGlenn Strauss
116371175df1SGlenn Strauss if (NULL == ae) {
116471175df1SGlenn Strauss if (ai->userhash && ulen <= sizeof(ai->userbuf))
116571175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, userbuf, ulen);
116671175df1SGlenn Strauss /* ai->username (lowercase userhash) will be replaced by username */
1167c841ce3bSGlenn Strauss rc = backend->digest(r, backend->p_d, ai);
116871175df1SGlenn Strauss }
1169c841ce3bSGlenn Strauss
1170c841ce3bSGlenn Strauss switch (rc) {
1171c841ce3bSGlenn Strauss case HANDLER_GO_ON:
1172c841ce3bSGlenn Strauss break;
1173c841ce3bSGlenn Strauss case HANDLER_WAIT_FOR_EVENT:
1174c841ce3bSGlenn Strauss return HANDLER_WAIT_FOR_EVENT;
1175c841ce3bSGlenn Strauss case HANDLER_FINISHED:
1176c841ce3bSGlenn Strauss return HANDLER_FINISHED;
1177c841ce3bSGlenn Strauss case HANDLER_ERROR:
1178c841ce3bSGlenn Strauss default:
1179c841ce3bSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if unknown user)*/
1180c841ce3bSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0);
1181c841ce3bSGlenn Strauss }
1182c841ce3bSGlenn Strauss
1183c841ce3bSGlenn Strauss if (sptree && NULL == ae) { /*(cache digest from backend)*/
118471175df1SGlenn Strauss ae = http_auth_cache_entry_init(require, ai->dalgo, user, ulen,
118571175df1SGlenn Strauss ai->username, ai->ulen,
118671175df1SGlenn Strauss (char *)ai->digest, ai->dlen);
1187c841ce3bSGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free);
1188c841ce3bSGlenn Strauss }
1189c841ce3bSGlenn Strauss
1190c841ce3bSGlenn Strauss return rc;
1191c841ce3bSGlenn Strauss }
1192c841ce3bSGlenn Strauss
119381b2d1f0SGlenn Strauss
119420c56cecSGlenn Strauss __attribute_cold__
119520c56cecSGlenn Strauss static handler_t
mod_auth_digest_misconfigured(request_st * const r,const struct http_auth_backend_t * const backend)119620c56cecSGlenn Strauss mod_auth_digest_misconfigured (request_st * const r, const struct http_auth_backend_t * const backend)
119720c56cecSGlenn Strauss {
1198cf0098eaSGlenn Strauss if (NULL == backend)
11997c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
12007c7f8c46SGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr);
1201cf0098eaSGlenn Strauss else
1202cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
1203cf0098eaSGlenn Strauss "auth.require \"method\" => \"digest\" invalid "
120420c56cecSGlenn Strauss "(try \"basic\"?) for %s", r->uri.path.ptr);
120520c56cecSGlenn Strauss
12067c7f8c46SGlenn Strauss r->http_status = 500;
12077c7f8c46SGlenn Strauss r->handler_module = NULL;
12087ba06c71SGlenn Strauss return HANDLER_FINISHED;
12097ba06c71SGlenn Strauss }
12107ba06c71SGlenn Strauss
12117ba06c71SGlenn Strauss
121220c56cecSGlenn Strauss static void
mod_auth_digest_parse_authorization(http_auth_digest_params_t * const dp,const char * c)12136881f79cSGlenn Strauss mod_auth_digest_parse_authorization (http_auth_digest_params_t * const dp, const char *c)
121420c56cecSGlenn Strauss {
121520c56cecSGlenn Strauss struct digest_kv {
121620c56cecSGlenn Strauss const char *key;
121720c56cecSGlenn Strauss uint32_t klen;
121820c56cecSGlenn Strauss enum http_auth_digest_params_e id;
121920c56cecSGlenn Strauss };
122020c56cecSGlenn Strauss
122120c56cecSGlenn Strauss static const struct digest_kv dkv[] = {
12226881f79cSGlenn Strauss { CONST_STR_LEN("username"), e_username },
12236881f79cSGlenn Strauss { CONST_STR_LEN("realm"), e_realm },
12246881f79cSGlenn Strauss { CONST_STR_LEN("nonce"), e_nonce },
12256881f79cSGlenn Strauss { CONST_STR_LEN("uri"), e_uri },
12266881f79cSGlenn Strauss { CONST_STR_LEN("algorithm"), e_algorithm },
12276881f79cSGlenn Strauss { CONST_STR_LEN("qop"), e_qop },
12286881f79cSGlenn Strauss { CONST_STR_LEN("cnonce"), e_cnonce },
12296881f79cSGlenn Strauss { CONST_STR_LEN("nc"), e_nc },
12306881f79cSGlenn Strauss { CONST_STR_LEN("response"), e_response },
123171175df1SGlenn Strauss { CONST_STR_LEN("username*"), e_userstar },
123271175df1SGlenn Strauss { CONST_STR_LEN("userhash"), e_userhash },
123320c56cecSGlenn Strauss
123420c56cecSGlenn Strauss { NULL, 0, http_auth_digest_params_sz }
123520c56cecSGlenn Strauss };
123681b2d1f0SGlenn Strauss
123781b2d1f0SGlenn Strauss /* parse credentials from client */
12386881f79cSGlenn Strauss /* (caller must pass c pointing to string after "Digest ") */
12396881f79cSGlenn Strauss for (const char *e; *c; c++) {
124081b2d1f0SGlenn Strauss /* skip whitespaces */
12416881f79cSGlenn Strauss while (*c == ' ' || *c == '\t' || *c == ',') ++c;
124281b2d1f0SGlenn Strauss if (!*c) break;
12436881f79cSGlenn Strauss for (e = c; *e!='=' && *e!=' ' && *e!='\t' && *e!='\0'; ++e) ;
12446881f79cSGlenn Strauss const uint32_t tlen = (uint32_t)(e - c);
124581b2d1f0SGlenn Strauss
124620c56cecSGlenn Strauss for (int i = 0; dkv[i].key; ++i) {
12476881f79cSGlenn Strauss if (tlen != dkv[i].klen || 0 != memcmp(c, dkv[i].key, tlen))
124820c56cecSGlenn Strauss continue;
12496881f79cSGlenn Strauss c += tlen;
12506881f79cSGlenn Strauss /* detect and step over '='; ignore BWS (bad whitespace) */
12516881f79cSGlenn Strauss if (__builtin_expect( (*c != '='), 0)) {
12526881f79cSGlenn Strauss while (*c == ' ' || *c == '\t') ++c;
12536881f79cSGlenn Strauss if (*c != '=') return; /*(including '\0')*/
125420c56cecSGlenn Strauss }
12556881f79cSGlenn Strauss do { ++c; } while (*c == ' ' || *c == '\t');
12566881f79cSGlenn Strauss
12576881f79cSGlenn Strauss if (*c == '"') {
12586881f79cSGlenn Strauss for (e = ++c; *e != '"' && *e != '\0'; ++e) {
12596881f79cSGlenn Strauss if (*e == '\\' && *++e == '\0') return;
12606881f79cSGlenn Strauss }
12616881f79cSGlenn Strauss if (*e != '"') return;
12626881f79cSGlenn Strauss /* value with "..." *//*(XXX: quoted value not unescaped)*/
126320c56cecSGlenn Strauss }
126420c56cecSGlenn Strauss else {
12656881f79cSGlenn Strauss for (e = c; *e!=',' && *e!=' ' && *e!='\t' && *e!='\0'; ++e) ;
12666881f79cSGlenn Strauss /* value without "..." */
12676881f79cSGlenn Strauss }
126820c56cecSGlenn Strauss dp->ptr[dkv[i].id] = c;
12696881f79cSGlenn Strauss dp->len[dkv[i].id] = (uint16_t)(e - c);
12706881f79cSGlenn Strauss c = e;
12716881f79cSGlenn Strauss if (*c != ',') {
12726881f79cSGlenn Strauss /*(could more strictly check for linear whitespace)*/
12736881f79cSGlenn Strauss c = strchr(c, ',');
12746881f79cSGlenn Strauss if (!c) return;
127581b2d1f0SGlenn Strauss }
127681b2d1f0SGlenn Strauss break;
127781b2d1f0SGlenn Strauss }
127881b2d1f0SGlenn Strauss }
127981b2d1f0SGlenn Strauss }
128081b2d1f0SGlenn Strauss
128181b2d1f0SGlenn Strauss
128220c56cecSGlenn Strauss static handler_t
mod_auth_digest_validate_userstar(request_st * const r,http_auth_digest_params_t * const dp,http_auth_info_t * const ai)128371175df1SGlenn Strauss mod_auth_digest_validate_userstar (request_st * const r, http_auth_digest_params_t * const dp, http_auth_info_t * const ai)
128471175df1SGlenn Strauss {
128571175df1SGlenn Strauss /*assert(dp->ptr[e_userstar]);*/
128671175df1SGlenn Strauss
128771175df1SGlenn Strauss if (dp->len[e_userhash] == 4) { /*("true")*/
128871175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
128971175df1SGlenn Strauss "digest: invalid \"username*\" with \"userhash\" = true");
129071175df1SGlenn Strauss return mod_auth_send_400_bad_request(r);
129171175df1SGlenn Strauss }
129271175df1SGlenn Strauss
129371175df1SGlenn Strauss /* "username*" RFC5987 ext-value
129471175df1SGlenn Strauss * ext-value = charset "'" [ language ] "'" value-chars */
129571175df1SGlenn Strauss const char *ptr = dp->ptr[e_userstar];
129671175df1SGlenn Strauss uint32_t len = dp->len[e_userstar];
129771175df1SGlenn Strauss /* validate and step over charset... */
129871175df1SGlenn Strauss if ((*ptr | 0x20) == 'u' && len > 5
129971175df1SGlenn Strauss && buffer_eq_icase_ssn(ptr, "utf-8", 5))
130071175df1SGlenn Strauss ptr += 5;
130171175df1SGlenn Strauss else if ((*ptr | 0x20) == 'i' && len > 10
130271175df1SGlenn Strauss && buffer_eq_icase_ssn(ptr, "iso-8859-1", 10))
130371175df1SGlenn Strauss ptr += 10;
130471175df1SGlenn Strauss else
130560c9612aSGlenn Strauss ptr = "\n"; /*(invalid char; (not '\''); error below)*/
130671175df1SGlenn Strauss /* step over ...'language'... */
130771175df1SGlenn Strauss if (*ptr++ != '\''
130871175df1SGlenn Strauss || !(ptr = memchr(ptr, '\'',
130971175df1SGlenn Strauss len - (uint32_t)(ptr - dp->ptr[e_userstar])))) {
131071175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
131171175df1SGlenn Strauss "digest: invalid \"username*\" ext-value");
131271175df1SGlenn Strauss return mod_auth_send_400_bad_request(r);
131371175df1SGlenn Strauss }
131471175df1SGlenn Strauss ++ptr;
131571175df1SGlenn Strauss
131671175df1SGlenn Strauss /* decode %XX encodings (could be more efficient by combining tests) */
131771175df1SGlenn Strauss buffer * const tb = r->tmp_buf;
131871175df1SGlenn Strauss buffer_copy_string_len(tb, ptr, len-(uint32_t)(ptr - dp->ptr[e_userstar]));
131971175df1SGlenn Strauss buffer_urldecode_path(tb);
132071175df1SGlenn Strauss if (dp->ptr[e_userstar][0] == 'u' && !buffer_is_valid_UTF8(tb)) {
132171175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
132271175df1SGlenn Strauss "digest: invalid \"username*\" invalid UTF-8");
132371175df1SGlenn Strauss return mod_auth_send_400_bad_request(r);
132471175df1SGlenn Strauss }
132571175df1SGlenn Strauss len = buffer_clen(tb);
132671175df1SGlenn Strauss if (len > sizeof(ai->userbuf)) {
132771175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
132871175df1SGlenn Strauss "digest: invalid \"username*\" too long");
132971175df1SGlenn Strauss return mod_auth_send_400_bad_request(r);
133071175df1SGlenn Strauss }
133171175df1SGlenn Strauss for (ptr = tb->ptr; *ptr; ++ptr) {
133271175df1SGlenn Strauss /* prohibit decoded control chars, including '\0','\r','\n' */
133371175df1SGlenn Strauss /* (theoretically could permit '\t', but not currently done) */
133471175df1SGlenn Strauss if (*(unsigned char *)ptr < 0x20 || *ptr == 127) { /* iscntrl() */
133571175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
133671175df1SGlenn Strauss "digest: invalid \"username*\" contains ctrl chars");
133771175df1SGlenn Strauss return mod_auth_send_400_bad_request(r);
133871175df1SGlenn Strauss }
133971175df1SGlenn Strauss }
134071175df1SGlenn Strauss
134171175df1SGlenn Strauss ai->ulen = len;
134271175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, tb->ptr, len);
134371175df1SGlenn Strauss return HANDLER_GO_ON;
134471175df1SGlenn Strauss }
134571175df1SGlenn Strauss
134671175df1SGlenn Strauss
134771175df1SGlenn Strauss static handler_t
mod_auth_digest_validate_params(request_st * const r,const struct http_auth_require_t * const require,http_auth_digest_params_t * const dp,http_auth_info_t * const ai)134820c56cecSGlenn Strauss mod_auth_digest_validate_params (request_st * const r, const struct http_auth_require_t * const require, http_auth_digest_params_t * const dp, http_auth_info_t * const ai)
134920c56cecSGlenn Strauss {
135020c56cecSGlenn Strauss /* check for required parameters */
135120c56cecSGlenn Strauss if ((!dp->ptr[e_qop] || (dp->ptr[e_nc] && dp->ptr[e_cnonce]))
135271175df1SGlenn Strauss && ((NULL != dp->ptr[e_username]) ^ (NULL != dp->ptr[e_userstar]))
135320c56cecSGlenn Strauss && dp->ptr[e_realm]
135420c56cecSGlenn Strauss && dp->ptr[e_nonce]
135520c56cecSGlenn Strauss && dp->ptr[e_uri]
135620c56cecSGlenn Strauss && dp->ptr[e_response]) {
135720c56cecSGlenn Strauss ai->username = dp->ptr[e_username];
135820c56cecSGlenn Strauss ai->ulen = dp->len[e_username];
135920c56cecSGlenn Strauss ai->realm = dp->ptr[e_realm];
136020c56cecSGlenn Strauss ai->rlen = dp->len[e_realm];
136171175df1SGlenn Strauss ai->userhash = (dp->len[e_userhash]==4);/*("true", not "false",absent)*/
136271175df1SGlenn Strauss if (!ai->username) { /* (dp->ptr[e_userstar]) */
136371175df1SGlenn Strauss if (HANDLER_GO_ON != mod_auth_digest_validate_userstar(r, dp, ai))
136471175df1SGlenn Strauss return HANDLER_FINISHED;
136571175df1SGlenn Strauss }
136620c56cecSGlenn Strauss }
136720c56cecSGlenn Strauss else {
13687c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
136981b2d1f0SGlenn Strauss "digest: missing field");
13707c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
137181b2d1f0SGlenn Strauss }
137281b2d1f0SGlenn Strauss
137320c56cecSGlenn Strauss if (!buffer_eq_slen(require->realm, ai->realm, ai->rlen)) {
13747c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
13755a6e4df8SGlenn Strauss "digest: realm mismatch");
13767c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0);
13775a6e4df8SGlenn Strauss }
13785a6e4df8SGlenn Strauss
137920c56cecSGlenn Strauss if (!mod_auth_algorithm_parse(ai,dp->ptr[e_algorithm],dp->len[e_algorithm])
138020c56cecSGlenn Strauss || !(require->algorithm & ai->dalgo & ~HTTP_AUTH_DIGEST_SESS)) {
13817c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
138220c56cecSGlenn Strauss "digest: (%.*s): invalid",
138320c56cecSGlenn Strauss (int)dp->len[e_algorithm], dp->ptr[e_algorithm]);
138412ac70d0SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0);
1385b9e2be50SGlenn Strauss }
1386b9e2be50SGlenn Strauss
138720c56cecSGlenn Strauss /* *-sess requires nonce and cnonce */
138820c56cecSGlenn Strauss if ((ai->dalgo & HTTP_AUTH_DIGEST_SESS)
138920c56cecSGlenn Strauss && (!dp->ptr[e_nonce] || !dp->ptr[e_cnonce])) {
13907c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
139120c56cecSGlenn Strauss "digest: (%.*s): missing field",
139220c56cecSGlenn Strauss (int)dp->len[e_algorithm], dp->ptr[e_algorithm]);
13937c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
139481b2d1f0SGlenn Strauss }
139581b2d1f0SGlenn Strauss
139620c56cecSGlenn Strauss if (0 != li_hex2bin(dp->rdigest, sizeof(dp->rdigest),
139720c56cecSGlenn Strauss dp->ptr[e_response], dp->len[e_response])
139820c56cecSGlenn Strauss || dp->len[e_response] != (ai->dlen << 1)) {
13997c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
140020c56cecSGlenn Strauss "digest: (%s): invalid format", dp->ptr[e_response]);
14017c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
1402b9e2be50SGlenn Strauss }
1403b9e2be50SGlenn Strauss
140420c56cecSGlenn Strauss if (dp->ptr[e_qop]&& buffer_eq_icase_ss(dp->ptr[e_qop], dp->len[e_qop],
140520c56cecSGlenn Strauss CONST_STR_LEN("auth-int"))) {
14067c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
140781b2d1f0SGlenn Strauss "digest: qop=auth-int not supported");
14087c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
140981b2d1f0SGlenn Strauss }
141081b2d1f0SGlenn Strauss
141181b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri
141281b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy
141381b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in
141481b2d1f0SGlenn Strauss * the same digest as that calculated by the client.)
141581b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri.
141681b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this
14177c7f8c46SGlenn Strauss * uri validation step is bypassed. r->target_orig is
14188131e439SGlenn Strauss * original request-target sent in client request. */
141920c56cecSGlenn Strauss if (!buffer_eq_slen(&r->target_orig, dp->ptr[e_uri], dp->len[e_uri])) {
14207c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
142120c56cecSGlenn Strauss "digest: auth failed: uri mismatch (%s != %.*s), IP: %s",
142220c56cecSGlenn Strauss r->target_orig.ptr, (int)dp->len[e_uri], dp->ptr[e_uri],
1423*3a8fc4bcSGlenn Strauss r->dst_addr_buf->ptr);
14247c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r);
142581b2d1f0SGlenn Strauss }
142620c56cecSGlenn Strauss
142720c56cecSGlenn Strauss return HANDLER_GO_ON;
142881b2d1f0SGlenn Strauss }
142981b2d1f0SGlenn Strauss
143020c56cecSGlenn Strauss
143120c56cecSGlenn Strauss static handler_t
mod_auth_digest_validate_nonce(request_st * const r,const struct http_auth_require_t * const require,http_auth_digest_params_t * const dp,http_auth_info_t * const ai)143220c56cecSGlenn Strauss mod_auth_digest_validate_nonce (request_st * const r, const struct http_auth_require_t * const require, http_auth_digest_params_t * const dp, http_auth_info_t * const ai)
143320c56cecSGlenn Strauss {
1434e1188e77SGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation
1435e1188e77SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced
1436e1188e77SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough
1437e1188e77SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and
1438e1188e77SGlenn Strauss * verifiable) if nanoseconds were included with seconds as part of the
1439e1188e77SGlenn Strauss * nonce "timestamp:secret". However, doing so would expose a high
1440e1188e77SGlenn Strauss * precision timestamp of the system to attackers. timestamp in nonce
1441e1188e77SGlenn Strauss * could theoretically be modified and still produce same md5sum, but
1442e1188e77SGlenn Strauss * that is highly unlikely within a 10 min (moving) window of valid
1443e1188e77SGlenn Strauss * time relative to current time (now).
1444e1188e77SGlenn Strauss * If it is desired to validate that nonces were generated by server,
1445e1188e77SGlenn Strauss * then specify auth.require = ( ... => ( "secret" => "..." ) )
1446e1188e77SGlenn Strauss * When secret is specified, then instead of nanoseconds, the random
1447e1188e77SGlenn Strauss * data value (included for unique nonces) will be exposed in the nonce
1448e1188e77SGlenn Strauss * along with the timestamp, and the additional secret will be used to
1449e1188e77SGlenn Strauss * validate that the server generated the nonce using that secret. */
1450309c1693SGlenn Strauss unix_time64_t ts = 0;
145120c56cecSGlenn Strauss const unsigned char * const nonce = (unsigned char *)dp->ptr[e_nonce];
145220c56cecSGlenn Strauss int i;
1453309c1693SGlenn Strauss for (i = 0; i < 16 && light_isxdigit(nonce[i]); ++i)
1454309c1693SGlenn Strauss ts = (unix_time64_t)((uint64_t)ts << 4) | hex2int(nonce[i]);
145520c56cecSGlenn Strauss
1456309c1693SGlenn Strauss const unix_time64_t cur_ts = log_epoch_secs;
145720c56cecSGlenn Strauss if (nonce[i] != ':' || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/
1458e1188e77SGlenn Strauss /* nonce is stale; have client regenerate digest */
145920c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, ai->dalgo);
1460e1188e77SGlenn Strauss }
1461e1188e77SGlenn Strauss
146220c56cecSGlenn Strauss if (cur_ts - ts > 540) /*(9 mins)*/
146320c56cecSGlenn Strauss dp->send_nextnonce_ts = cur_ts;
1464e1188e77SGlenn Strauss
1465e1188e77SGlenn Strauss if (require->nonce_secret) {
1466e1188e77SGlenn Strauss unsigned int rnd = 0;
146720c56cecSGlenn Strauss for (int j = i+8; i < j && light_isxdigit(nonce[i]); ++i) {
146820c56cecSGlenn Strauss rnd = (rnd << 4) + hex2int(nonce[i]);
1469e1188e77SGlenn Strauss }
1470e1188e77SGlenn Strauss if (nonce[i] != ':') {
1471e1188e77SGlenn Strauss /* nonce is invalid;
1472e1188e77SGlenn Strauss * expect extra field w/ require->nonce_secret */
1473e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
1474e1188e77SGlenn Strauss "digest: nonce invalid");
1475e1188e77SGlenn Strauss return mod_auth_send_400_bad_request(r);
1476e1188e77SGlenn Strauss }
1477e1188e77SGlenn Strauss buffer * const tb = r->tmp_buf;
1478e1188e77SGlenn Strauss buffer_clear(tb);
147920c56cecSGlenn Strauss mod_auth_append_nonce(tb, cur_ts, require, ai->dalgo, (int *)&rnd);
148020c56cecSGlenn Strauss if (!buffer_eq_slen(tb, dp->ptr[e_nonce], dp->len[e_nonce])) {
1481e1188e77SGlenn Strauss /* nonce not generated using current require->nonce_secret */
1482e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
1483e1188e77SGlenn Strauss "digest: nonce mismatch");
1484e1188e77SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0);
1485e1188e77SGlenn Strauss }
1486e1188e77SGlenn Strauss }
148781b2d1f0SGlenn Strauss
14884b3a91e6SGlenn Strauss return HANDLER_GO_ON;
148981b2d1f0SGlenn Strauss }
149081b2d1f0SGlenn Strauss
149120c56cecSGlenn Strauss
149220c56cecSGlenn 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)149320c56cecSGlenn Strauss 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)
149420c56cecSGlenn Strauss {
149520c56cecSGlenn Strauss if (NULL == backend || NULL == backend->digest)
149620c56cecSGlenn Strauss return mod_auth_digest_misconfigured(r, backend);
149720c56cecSGlenn Strauss
149820c56cecSGlenn Strauss const buffer * const vb =
149920c56cecSGlenn Strauss http_header_request_get(r, HTTP_HEADER_AUTHORIZATION,
150020c56cecSGlenn Strauss CONST_STR_LEN("Authorization"));
150120c56cecSGlenn Strauss if (NULL == vb || !buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest ")))
150220c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0);
150320c56cecSGlenn Strauss #ifdef __COVERITY__
150420c56cecSGlenn Strauss if (buffer_clen(vb) < sizeof("Digest ")-1)
150520c56cecSGlenn Strauss return mod_auth_send_400_bad_request(r);
150620c56cecSGlenn Strauss #endif
150720c56cecSGlenn Strauss
150820c56cecSGlenn Strauss http_auth_digest_params_t dp;
150920c56cecSGlenn Strauss http_auth_info_t ai;
151020c56cecSGlenn Strauss handler_t rc;
151120c56cecSGlenn Strauss
151271175df1SGlenn Strauss /* XXX: should use offsetof() (if portable enough) */
151320c56cecSGlenn Strauss memset(&dp, 0, sizeof(dp) - sizeof(dp.rdigest));
151420c56cecSGlenn Strauss
15156881f79cSGlenn Strauss mod_auth_digest_parse_authorization(&dp, vb->ptr + sizeof("Digest ")-1);
151620c56cecSGlenn Strauss
151720c56cecSGlenn Strauss rc = mod_auth_digest_validate_params(r, require, &dp, &ai);
151820c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0))
151920c56cecSGlenn Strauss return rc;
152020c56cecSGlenn Strauss
152120c56cecSGlenn Strauss rc = mod_auth_digest_validate_nonce(r, require, &dp, &ai);
152220c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0))
152320c56cecSGlenn Strauss return rc;
152420c56cecSGlenn Strauss
152520c56cecSGlenn Strauss rc = mod_auth_digest_get(r, p_d, require, backend, &ai);
152620c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0))
152720c56cecSGlenn Strauss return rc;
152820c56cecSGlenn Strauss
15299fe8fbaaSGlenn Strauss mod_auth_digest_mutate(&ai, &dp, http_method_buf(r->http_method));
153020c56cecSGlenn Strauss
153120c56cecSGlenn Strauss if (!ck_memeq_const_time_fixed_len(dp.rdigest, ai.digest, ai.dlen)) {
153220c56cecSGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//*skip clear since mutated*/
153320c56cecSGlenn Strauss /* digest not ok */
153420c56cecSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__,
153520c56cecSGlenn Strauss "digest: auth failed for %.*s: wrong password, IP: %s",
1536*3a8fc4bcSGlenn Strauss (int)ai.ulen, ai.username, r->dst_addr_buf->ptr);
153720c56cecSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/
153820c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0);
15397ba06c71SGlenn Strauss }
154020c56cecSGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//* skip clear since mutated */
154120c56cecSGlenn Strauss
154220c56cecSGlenn Strauss /* check authorization (authz); string args must be '\0'-terminated) */
154320c56cecSGlenn Strauss buffer * const tb = r->tmp_buf;
154420c56cecSGlenn Strauss buffer_copy_string_len(tb, ai.username, ai.ulen);
154520c56cecSGlenn Strauss if (!http_auth_match_rules(require, tb->ptr, NULL, NULL))
154620c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0);
154720c56cecSGlenn Strauss
154820c56cecSGlenn Strauss if (dp.send_nextnonce_ts) {
154920c56cecSGlenn Strauss /*(send nextnonce when expiration is approaching)*/
155020c56cecSGlenn Strauss mod_auth_digest_authentication_info(
155120c56cecSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_OTHER,
155220c56cecSGlenn Strauss CONST_STR_LEN("Authentication-Info")),
155320c56cecSGlenn Strauss dp.send_nextnonce_ts /*(cur_ts)*/, require, ai.dalgo);
155420c56cecSGlenn Strauss }
155520c56cecSGlenn Strauss
155620c56cecSGlenn Strauss http_auth_setenv(r, ai.username, ai.ulen, CONST_STR_LEN("Digest"));
155720c56cecSGlenn Strauss return HANDLER_GO_ON;
155820c56cecSGlenn Strauss }
155920c56cecSGlenn Strauss
156020c56cecSGlenn Strauss
15617ba06c71SGlenn Strauss
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)15627c7f8c46SGlenn 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) {
15637ba06c71SGlenn Strauss /* require REMOTE_USER already set */
15647c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER"));
15657ba06c71SGlenn Strauss UNUSED(p_d);
15667ba06c71SGlenn Strauss UNUSED(backend);
15673dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) {
1568f9d58670SStefan Bühler return HANDLER_GO_ON;
1569bcdc6a3bSJan Kneschke } else {
15707c7f8c46SGlenn Strauss r->http_status = 401;
15717c7f8c46SGlenn Strauss r->handler_module = NULL;
1572bcdc6a3bSJan Kneschke return HANDLER_FINISHED;
15737187271fSStefan Bühler }
1574bcdc6a3bSJan Kneschke }
1575