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 * 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 = 6871175df1SGlenn Strauss malloc(sizeof(http_auth_cache_entry) + ulen + pwlen 6971175df1SGlenn Strauss + (k == username ? 0 : klen)); 70e11514b0SGlenn Strauss force_assert(ae); 71e11514b0SGlenn Strauss ae->require = require; 72dbe3e236SGlenn Strauss ae->ctime = log_monotonic_secs; 73e11514b0SGlenn Strauss ae->dalgo = dalgo; 74e11514b0SGlenn Strauss ae->ulen = ulen; 75e11514b0SGlenn Strauss ae->dlen = pwlen; 7671175df1SGlenn Strauss ae->klen = klen; 77e11514b0SGlenn Strauss ae->username = (char *)(ae + 1); 78e11514b0SGlenn Strauss ae->pwdigest = ae->username + ulen; 7971175df1SGlenn Strauss ae->k = (k == username) 8071175df1SGlenn Strauss ? ae->username 8171175df1SGlenn Strauss : memcpy(ae->pwdigest + pwlen, k, klen); 82e11514b0SGlenn Strauss memcpy(ae->username, username, ulen); 83e11514b0SGlenn Strauss memcpy(ae->pwdigest, pw, pwlen); 84e11514b0SGlenn Strauss return ae; 85e11514b0SGlenn Strauss } 86e11514b0SGlenn Strauss 87e11514b0SGlenn Strauss static void 88e11514b0SGlenn Strauss http_auth_cache_entry_free (void *data) 89e11514b0SGlenn Strauss { 90e11514b0SGlenn Strauss http_auth_cache_entry * const ae = data; 9108c03cd4SGlenn Strauss ck_memzero(ae->pwdigest, ae->dlen); 92e11514b0SGlenn Strauss free(ae); 93e11514b0SGlenn Strauss } 94e11514b0SGlenn Strauss 95e11514b0SGlenn Strauss static void 96e11514b0SGlenn Strauss http_auth_cache_free (http_auth_cache *ac) 97e11514b0SGlenn Strauss { 98e11514b0SGlenn Strauss splay_tree *sptree = ac->sptree; 99e11514b0SGlenn Strauss while (sptree) { 100e11514b0SGlenn Strauss http_auth_cache_entry_free(sptree->data); 101e11514b0SGlenn Strauss sptree = splaytree_delete(sptree, sptree->key); 102e11514b0SGlenn Strauss } 103e11514b0SGlenn Strauss free(ac); 104e11514b0SGlenn Strauss } 105e11514b0SGlenn Strauss 106e11514b0SGlenn Strauss static http_auth_cache * 107e11514b0SGlenn Strauss http_auth_cache_init (const array *opts) 108e11514b0SGlenn Strauss { 109e11514b0SGlenn Strauss http_auth_cache *ac = malloc(sizeof(http_auth_cache)); 110e11514b0SGlenn Strauss force_assert(ac); 111e11514b0SGlenn Strauss ac->sptree = NULL; 112e11514b0SGlenn Strauss ac->max_age = 600; /* 10 mins */ 113e11514b0SGlenn Strauss for (uint32_t i = 0, used = opts->used; i < used; ++i) { 114d6f692f1SGlenn Strauss data_unset *du = opts->data[i]; 115d6f692f1SGlenn Strauss if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age"))) 116*18bbdbbeSGlenn Strauss ac->max_age = (time_t) 117*18bbdbbeSGlenn Strauss config_plugin_value_to_int32(du, (int32_t)ac->max_age); 118e11514b0SGlenn Strauss } 119e11514b0SGlenn Strauss return ac; 120e11514b0SGlenn Strauss } 121e11514b0SGlenn Strauss 122e11514b0SGlenn Strauss static int 123e11514b0SGlenn Strauss http_auth_cache_hash (const struct http_auth_require_t * const require, const char *username, const uint32_t ulen) 124e11514b0SGlenn Strauss { 125e11514b0SGlenn Strauss uint32_t h = /*(hash pointer value, which includes realm and permissions)*/ 126e11514b0SGlenn Strauss djbhash((char *)(intptr_t)require, sizeof(intptr_t), DJBHASH_INIT); 127e11514b0SGlenn Strauss h = djbhash(username, ulen, h); 128e11514b0SGlenn Strauss /* strip highest bit of hash value for splaytree (see splaytree_djbhash())*/ 129e11514b0SGlenn Strauss return (int32_t)(h & ~(((uint32_t)1) << 31)); 130e11514b0SGlenn Strauss } 131e11514b0SGlenn Strauss 132e11514b0SGlenn Strauss static http_auth_cache_entry * 133e11514b0SGlenn Strauss http_auth_cache_query (splay_tree ** const sptree, const int ndx) 134e11514b0SGlenn Strauss { 135e11514b0SGlenn Strauss *sptree = splaytree_splay(*sptree, ndx); 136e11514b0SGlenn Strauss return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL; 137e11514b0SGlenn Strauss } 138e11514b0SGlenn Strauss 139e11514b0SGlenn Strauss static void 140e11514b0SGlenn Strauss http_auth_cache_insert (splay_tree ** const sptree, const int ndx, void * const data, void(data_free_fn)(void *)) 141e11514b0SGlenn Strauss { 142e11514b0SGlenn Strauss /*(not necessary to re-splay (with current usage) since single-threaded 143e11514b0SGlenn Strauss * and splaytree has not been modified since http_auth_cache_query())*/ 144e11514b0SGlenn Strauss /* *sptree = splaytree_splay(*sptree, ndx); */ 145e11514b0SGlenn Strauss if (NULL == *sptree || (*sptree)->key != ndx) 146e11514b0SGlenn Strauss *sptree = splaytree_insert(*sptree, ndx, data); 147e11514b0SGlenn Strauss else { /* collision; replace old entry */ 148e11514b0SGlenn Strauss data_free_fn((*sptree)->data); 149e11514b0SGlenn Strauss (*sptree)->data = data; 150e11514b0SGlenn Strauss } 151e11514b0SGlenn Strauss } 152e11514b0SGlenn Strauss 153e11514b0SGlenn Strauss /* walk though cache, collect expired ids, and remove them in a second loop */ 154e11514b0SGlenn Strauss static void 155309c1693SGlenn 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) 156e11514b0SGlenn Strauss { 157e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/ 158e11514b0SGlenn Strauss if (t->left) 159e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->left, keys, ndx, max_age, cur_ts); 160e11514b0SGlenn Strauss if (t->right) 161e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->right, keys, ndx, max_age, cur_ts); 162e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/ 163e11514b0SGlenn Strauss 164e11514b0SGlenn Strauss const http_auth_cache_entry * const ae = t->data; 165e11514b0SGlenn Strauss if (cur_ts - ae->ctime > max_age) 166e11514b0SGlenn Strauss keys[(*ndx)++] = t->key; 167e11514b0SGlenn Strauss } 168e11514b0SGlenn Strauss 169e11514b0SGlenn Strauss __attribute_noinline__ 170e11514b0SGlenn Strauss static void 171309c1693SGlenn Strauss mod_auth_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const unix_time64_t cur_ts) 172e11514b0SGlenn Strauss { 173e11514b0SGlenn Strauss splay_tree *sptree = *sptree_ptr; 174e11514b0SGlenn Strauss int max_ndx, i; 175e11514b0SGlenn Strauss int keys[8192]; /* 32k size on stack */ 176e11514b0SGlenn Strauss do { 177e11514b0SGlenn Strauss if (!sptree) break; 178e11514b0SGlenn Strauss max_ndx = 0; 179e11514b0SGlenn Strauss mod_auth_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts); 180e11514b0SGlenn Strauss for (i = 0; i < max_ndx; ++i) { 181e11514b0SGlenn Strauss int ndx = keys[i]; 182e11514b0SGlenn Strauss sptree = splaytree_splay(sptree, ndx); 183e11514b0SGlenn Strauss if (sptree && sptree->key == ndx) { 184e11514b0SGlenn Strauss http_auth_cache_entry_free(sptree->data); 185e11514b0SGlenn Strauss sptree = splaytree_delete(sptree, ndx); 186e11514b0SGlenn Strauss } 187e11514b0SGlenn Strauss } 188e11514b0SGlenn Strauss } while (max_ndx == sizeof(keys)/sizeof(int)); 189e11514b0SGlenn Strauss *sptree_ptr = sptree; 190e11514b0SGlenn Strauss } 191e11514b0SGlenn Strauss 192e11514b0SGlenn Strauss TRIGGER_FUNC(mod_auth_periodic) 193e11514b0SGlenn Strauss { 194e11514b0SGlenn Strauss const plugin_data * const p = p_d; 195309c1693SGlenn Strauss const unix_time64_t cur_ts = log_monotonic_secs; 196e11514b0SGlenn Strauss if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/ 197e11514b0SGlenn Strauss UNUSED(srv); 198e11514b0SGlenn Strauss 199e11514b0SGlenn Strauss /* future: might construct array of (http_auth_cache *) at startup 200e11514b0SGlenn Strauss * to avoid the need to search for them here */ 20133e400b4SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 20233e400b4SGlenn Strauss if (NULL == p->cvlist) return HANDLER_GO_ON; 20333e400b4SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 204e11514b0SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 205e11514b0SGlenn Strauss for (; cpv->k_id != -1; ++cpv) { 206e11514b0SGlenn Strauss if (cpv->k_id != 3) continue; /* k_id == 3 for auth.cache */ 207e11514b0SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL) continue; 208e11514b0SGlenn Strauss http_auth_cache *ac = cpv->v.v; 209e11514b0SGlenn Strauss mod_auth_periodic_cleanup(&ac->sptree, ac->max_age, cur_ts); 210e11514b0SGlenn Strauss } 211e11514b0SGlenn Strauss } 212e11514b0SGlenn Strauss 213e11514b0SGlenn Strauss return HANDLER_GO_ON; 214e11514b0SGlenn Strauss } 215e11514b0SGlenn Strauss 216e11514b0SGlenn Strauss 217e11514b0SGlenn Strauss 218e11514b0SGlenn Strauss 2197c7f8c46SGlenn 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); 2207c7f8c46SGlenn 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); 2217c7f8c46SGlenn 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); 2224b3a91e6SGlenn Strauss 223bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) { 224e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL }; 225e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL }; 2267ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL }; 227e11514b0SGlenn Strauss plugin_data *p = calloc(1, sizeof(*p)); 228e11514b0SGlenn Strauss force_assert(p); 2297ba06c71SGlenn Strauss 2307ba06c71SGlenn Strauss /* register http_auth_scheme_* */ 231e11514b0SGlenn Strauss http_auth_scheme_basic.p_d = p; 2327ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic); 233e11514b0SGlenn Strauss http_auth_scheme_digest.p_d = p; 2347ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest); 2357ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern); 236bcdc6a3bSJan Kneschke 237bcdc6a3bSJan Kneschke return p; 238bcdc6a3bSJan Kneschke } 239bcdc6a3bSJan Kneschke 240b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) { 241b73949e0SGlenn Strauss plugin_data * const p = p_d; 2428e713130SGlenn Strauss if (NULL == p->cvlist) return; 2438e713130SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 2448e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 2458e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 2468e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 2478e713130SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 2488e713130SGlenn Strauss switch (cpv->k_id) { 2498e713130SGlenn Strauss case 1: /* auth.require */ 2508e713130SGlenn Strauss array_free(cpv->v.v); 2518e713130SGlenn Strauss break; 252e11514b0SGlenn Strauss case 3: /* auth.cache */ 253e11514b0SGlenn Strauss http_auth_cache_free(cpv->v.v); 254e11514b0SGlenn Strauss break; 2558e713130SGlenn Strauss default: 2568e713130SGlenn Strauss break; 2578e713130SGlenn Strauss } 2588e713130SGlenn Strauss } 2598e713130SGlenn Strauss } 2601a8ac120SGlenn Strauss 2611a8ac120SGlenn Strauss http_auth_dumbdata_reset(); 2628e713130SGlenn Strauss } 2638e713130SGlenn Strauss 2647ba06c71SGlenn Strauss /* data type for mod_auth structured data 2657ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */ 2667ba06c71SGlenn Strauss typedef struct { 2677ba06c71SGlenn Strauss DATA_UNSET; 2687ba06c71SGlenn Strauss http_auth_require_t *require; 2697ba06c71SGlenn Strauss } data_auth; 2707ba06c71SGlenn Strauss 2717ba06c71SGlenn Strauss static void data_auth_free(data_unset *d) 2727ba06c71SGlenn Strauss { 2737ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d; 274ad9b7e00SGlenn Strauss free(dauth->key.ptr); 2757ba06c71SGlenn Strauss http_auth_require_free(dauth->require); 2767ba06c71SGlenn Strauss free(dauth); 2777ba06c71SGlenn Strauss } 2787ba06c71SGlenn Strauss 2797ba06c71SGlenn Strauss static data_auth *data_auth_init(void) 2807ba06c71SGlenn Strauss { 2818c7f1dfbSGlenn Strauss static const struct data_methods fn = { 2828c7f1dfbSGlenn Strauss NULL, /* copy must not be called on this data */ 2838c7f1dfbSGlenn Strauss data_auth_free, 2848c7f1dfbSGlenn Strauss NULL, /* insert_dup must not be called on this data */ 2858c7f1dfbSGlenn Strauss }; 2867ba06c71SGlenn Strauss data_auth * const dauth = calloc(1, sizeof(*dauth)); 2877ba06c71SGlenn Strauss force_assert(NULL != dauth); 2887ba06c71SGlenn Strauss dauth->type = TYPE_OTHER; 2898c7f1dfbSGlenn Strauss dauth->fn = &fn; 2907ba06c71SGlenn Strauss 2917ba06c71SGlenn Strauss dauth->require = http_auth_require_init(); 2927ba06c71SGlenn Strauss 2937ba06c71SGlenn Strauss return dauth; 2947ba06c71SGlenn Strauss } 2957ba06c71SGlenn Strauss 296870b0c13SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s, size_t len) { 297870b0c13SGlenn Strauss if (0 == len) { 298b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_MD5; 299b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 300b9e2be50SGlenn Strauss return 1; 301b9e2be50SGlenn Strauss } 302b9e2be50SGlenn Strauss 303b9e2be50SGlenn Strauss if (len > 5 304b9e2be50SGlenn Strauss && (s[len-5] ) == '-' 305b9e2be50SGlenn Strauss && (s[len-4] | 0x20) == 's' 306b9e2be50SGlenn Strauss && (s[len-3] | 0x20) == 'e' 307b9e2be50SGlenn Strauss && (s[len-2] | 0x20) == 's' 308b9e2be50SGlenn Strauss && (s[len-1] | 0x20) == 's') { 309b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_SESS; 310b9e2be50SGlenn Strauss len -= 5; 311b9e2be50SGlenn Strauss } 312b9e2be50SGlenn Strauss else { 313b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_NONE; 314b9e2be50SGlenn Strauss } 315b9e2be50SGlenn Strauss 316b9e2be50SGlenn Strauss if (3 == len 317b9e2be50SGlenn Strauss && 'm' == (s[0] | 0x20) 318b9e2be50SGlenn Strauss && 'd' == (s[1] | 0x20) 319b9e2be50SGlenn Strauss && '5' == (s[2] )) { 320b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_MD5; 321b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 322b9e2be50SGlenn Strauss return 1; 323b9e2be50SGlenn Strauss } 324b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 325b9e2be50SGlenn Strauss else if (len >= 7 326b9e2be50SGlenn Strauss && 's' == (s[0] | 0x20) 327b9e2be50SGlenn Strauss && 'h' == (s[1] | 0x20) 328b9e2be50SGlenn Strauss && 'a' == (s[2] | 0x20) 329b9e2be50SGlenn Strauss && '-' == (s[3] )) { 330b9e2be50SGlenn Strauss if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') { 331b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA256; 332b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA256_BINLEN; 333b9e2be50SGlenn Strauss return 1; 334b9e2be50SGlenn Strauss } 3351fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 336b9e2be50SGlenn Strauss if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) { 337b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256; 338b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 339b9e2be50SGlenn Strauss return 1; 340b9e2be50SGlenn Strauss } 341b9e2be50SGlenn Strauss #endif 342b9e2be50SGlenn Strauss } 343b9e2be50SGlenn Strauss #endif 344b9e2be50SGlenn Strauss return 0; /*(error)*/ 345b9e2be50SGlenn Strauss } 346b9e2be50SGlenn Strauss 347b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) { 348870b0c13SGlenn Strauss for (const char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) { 349b9e2be50SGlenn Strauss http_auth_info_t ai; 350b9e2be50SGlenn Strauss p = strchr(s, '|'); 351870b0c13SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, s, p ? (size_t)(p - s) : strlen(s))) 352870b0c13SGlenn Strauss return 0; 353b9e2be50SGlenn Strauss *algorithm |= ai.dalgo; 354b9e2be50SGlenn Strauss } 355b9e2be50SGlenn Strauss return 1; 356b9e2be50SGlenn Strauss } 357b9e2be50SGlenn Strauss 358b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh) 3597ba06c71SGlenn Strauss { 3607ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */ 3617ba06c71SGlenn Strauss 3627ba06c71SGlenn Strauss const char *str = b->ptr; 3637ba06c71SGlenn Strauss const char *p; 3647ba06c71SGlenn Strauss 3657ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) { 3667ba06c71SGlenn Strauss require->valid_user = 1; 3677ba06c71SGlenn Strauss return 1; /* success */ 3687ba06c71SGlenn Strauss } 3697ba06c71SGlenn Strauss 3707ba06c71SGlenn Strauss do { 3717ba06c71SGlenn Strauss const char *eq; 3727ba06c71SGlenn Strauss size_t len; 3737ba06c71SGlenn Strauss p = strchr(str, '|'); 3747ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str); 3757ba06c71SGlenn Strauss eq = memchr(str, '=', len); 3767ba06c71SGlenn Strauss if (NULL == eq) { 377b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 378010c2894SGlenn Strauss "error parsing auth.require 'require' field: missing '=' " 379010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 380010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 3817ba06c71SGlenn Strauss return 0; 3827ba06c71SGlenn Strauss } 383b2c18f48SGlenn Strauss if (eq[1] == '|' || eq[1] == '\0') { 384b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 385010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 386010c2894SGlenn Strauss "missing token after '=' " 387010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 388010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 3897ba06c71SGlenn Strauss return 0; 3907ba06c71SGlenn Strauss } 3917ba06c71SGlenn Strauss 3927ba06c71SGlenn Strauss switch ((int)(eq - str)) { 3937ba06c71SGlenn Strauss case 4: 3947ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) { 395810109ccSGlenn Strauss /*("user=" is 5)*/ 3968b296531SGlenn Strauss array_insert_value(&require->user, str+5, len-5); 3977ba06c71SGlenn Strauss continue; 3987ba06c71SGlenn Strauss } 3997ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) { 400810109ccSGlenn Strauss /*("host=" is 5)*/ 4018b296531SGlenn Strauss array_insert_value(&require->host, str+5, len-5); 402b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 403010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 404010c2894SGlenn Strauss "'host' not implemented; field value: %s", b->ptr); 4057ba06c71SGlenn Strauss continue; 4067ba06c71SGlenn Strauss } 4077ba06c71SGlenn Strauss break; /* to error */ 4087ba06c71SGlenn Strauss case 5: 4097ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) { 410810109ccSGlenn Strauss /*("group=" is 6)*/ 4118b296531SGlenn Strauss array_insert_value(&require->group, str+6, len-6); 4129c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/ 413b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 414010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 415010c2894SGlenn Strauss "'group' not implemented; field value: %s", b->ptr); 4169c91af0cSGlenn Strauss #endif 4177ba06c71SGlenn Strauss continue; 4187ba06c71SGlenn Strauss } 4197ba06c71SGlenn Strauss break; /* to error */ 4207ba06c71SGlenn Strauss case 10: 4217ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) { 422b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 423010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 424010c2894SGlenn Strauss "valid user can not be combined with other require rules " 425010c2894SGlenn Strauss "(expecting \"valid-user\" or " 426010c2894SGlenn Strauss "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr); 4277ba06c71SGlenn Strauss return 0; 4287ba06c71SGlenn Strauss } 4297ba06c71SGlenn Strauss break; /* to error */ 4307ba06c71SGlenn Strauss default: 4317ba06c71SGlenn Strauss break; /* to error */ 4327ba06c71SGlenn Strauss } 4337ba06c71SGlenn Strauss 434b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 435010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 436010c2894SGlenn Strauss "invalid/unsupported token " 437010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 438010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 4397ba06c71SGlenn Strauss return 0; 4407ba06c71SGlenn Strauss 4417ba06c71SGlenn Strauss } while (p && *((str = p+1))); 4427ba06c71SGlenn Strauss 4437ba06c71SGlenn Strauss return 1; /* success */ 4447ba06c71SGlenn Strauss } 4457ba06c71SGlenn Strauss 446b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh) 4478e713130SGlenn Strauss { 4488e713130SGlenn Strauss for (uint32_t n = 0; n < value->used; ++n) { 4497ba06c71SGlenn Strauss size_t m; 4508e713130SGlenn Strauss data_array *da_file = (data_array *)value->data[n]; 4517ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL; 452e1188e77SGlenn Strauss const buffer *nonce_secret = NULL; 45371175df1SGlenn Strauss data_unset *userhash = NULL; 4547ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme; 455b9e2be50SGlenn Strauss buffer *algos = NULL; 456b9e2be50SGlenn Strauss int algorithm = HTTP_AUTH_DIGEST_SESS; 4577ba06c71SGlenn Strauss 458c2238256SGlenn Strauss if (!array_is_kvstring(&da_file->value)) { 459b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 460010c2894SGlenn Strauss "unexpected value for auth.require; expected " 461bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 4627ba06c71SGlenn Strauss 4637ba06c71SGlenn Strauss return HANDLER_ERROR; 4647ba06c71SGlenn Strauss } 4657ba06c71SGlenn Strauss 466c2238256SGlenn Strauss for (m = 0; m < da_file->value.used; m++) { 467c2238256SGlenn Strauss if (da_file->value.data[m]->type == TYPE_STRING) { 468c2238256SGlenn Strauss data_string *ds = (data_string *)da_file->value.data[m]; 469ad9b7e00SGlenn Strauss if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) { 470601c572cSGlenn Strauss method = &ds->value; 471ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) { 472601c572cSGlenn Strauss realm = &ds->value; 473ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) { 474601c572cSGlenn Strauss require = &ds->value; 475ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) { 476601c572cSGlenn Strauss algos = &ds->value; 47733ae8302SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce_secret")) 47833ae8302SGlenn Strauss || buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce-secret"))) { 479e1188e77SGlenn Strauss nonce_secret = &ds->value; 48071175df1SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("userhash"))) { 48171175df1SGlenn Strauss userhash = (data_unset *)ds; 4827ba06c71SGlenn Strauss } else { 483b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 484010c2894SGlenn Strauss "the field is unknown in: " 485010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 486010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 4877ba06c71SGlenn Strauss 4887ba06c71SGlenn Strauss return HANDLER_ERROR; 4897ba06c71SGlenn Strauss } 4907ba06c71SGlenn Strauss } else { 491b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 492010c2894SGlenn Strauss "a string was expected for: " 493010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 494010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 4957ba06c71SGlenn Strauss 4967ba06c71SGlenn Strauss return HANDLER_ERROR; 4977ba06c71SGlenn Strauss } 4987ba06c71SGlenn Strauss } 4997ba06c71SGlenn Strauss 500af3df29aSGlenn Strauss if (!method || buffer_is_blank(method)) { 501b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 502010c2894SGlenn Strauss "the method field is missing or blank in: " 5037ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )"); 5047ba06c71SGlenn Strauss return HANDLER_ERROR; 5057ba06c71SGlenn Strauss } else { 5067ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method); 5077ba06c71SGlenn Strauss if (NULL == auth_scheme) { 508b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 509010c2894SGlenn Strauss "unknown method %s (e.g. \"basic\", \"digest\" or \"extern\") in " 510010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )", method->ptr); 5117ba06c71SGlenn Strauss return HANDLER_ERROR; 5127ba06c71SGlenn Strauss } 5137ba06c71SGlenn Strauss } 5147ba06c71SGlenn Strauss 515af3df29aSGlenn Strauss if (!realm) { 516b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 517010c2894SGlenn Strauss "the realm field is missing in: " 5187ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )"); 5197ba06c71SGlenn Strauss return HANDLER_ERROR; 5207ba06c71SGlenn Strauss } 5217ba06c71SGlenn Strauss 522af3df29aSGlenn Strauss if (!require || buffer_is_blank(require)) { 523b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 524010c2894SGlenn Strauss "the require field is missing or blank in: " 5257ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )"); 5267ba06c71SGlenn Strauss return HANDLER_ERROR; 5277ba06c71SGlenn Strauss } 5287ba06c71SGlenn Strauss 529af3df29aSGlenn Strauss if (!algos || buffer_is_blank(algos)) { 530b9e2be50SGlenn Strauss algorithm |= HTTP_AUTH_DIGEST_MD5; 531b9e2be50SGlenn Strauss } else if (!mod_auth_algorithms_parse(&algorithm, algos)) { 532b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 533010c2894SGlenn Strauss "invalid algorithm in: " 534b9e2be50SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )"); 535b9e2be50SGlenn Strauss return HANDLER_ERROR; 536b9e2be50SGlenn Strauss } 537b9e2be50SGlenn Strauss 538609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/ 5397ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init(); 540ad9b7e00SGlenn Strauss buffer_copy_buffer(&dauth->key, &da_file->key); 5417ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme; 542b9e2be50SGlenn Strauss dauth->require->algorithm = algorithm; 543a03afc90SGlenn Strauss dauth->require->realm = realm; 544e1188e77SGlenn Strauss dauth->require->nonce_secret = nonce_secret; /*(NULL is ok)*/ 54571175df1SGlenn Strauss dauth->require->userhash = config_plugin_value_tobool(userhash, 0); 546b5775b99SGlenn Strauss if (!mod_auth_require_parse(dauth->require, require, errh)) { 5478c7f1dfbSGlenn Strauss dauth->fn->free((data_unset *)dauth); 5487ba06c71SGlenn Strauss return HANDLER_ERROR; 5497ba06c71SGlenn Strauss } 5508e713130SGlenn Strauss array_insert_unique(auth_require, (data_unset *)dauth); 5517ba06c71SGlenn Strauss } 5527ba06c71SGlenn Strauss } 5537ba06c71SGlenn Strauss 5547ba06c71SGlenn Strauss return HANDLER_GO_ON; 5557ba06c71SGlenn Strauss } 5567ba06c71SGlenn Strauss 5578e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 5588e713130SGlenn Strauss switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 5598e713130SGlenn Strauss case 0: /* auth.backend */ 5608e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 5618e713130SGlenn Strauss pconf->auth_backend = cpv->v.v; 5628e713130SGlenn Strauss break; 5638e713130SGlenn Strauss case 1: /* auth.require */ 5648e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 5658e713130SGlenn Strauss pconf->auth_require = cpv->v.v; 5668e713130SGlenn Strauss break; 5678e713130SGlenn Strauss case 2: /* auth.extern-authn */ 5688e713130SGlenn Strauss pconf->auth_extern_authn = cpv->v.u; 569e11514b0SGlenn Strauss break; 570e11514b0SGlenn Strauss case 3: /* auth.cache */ 571e11514b0SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 572e11514b0SGlenn Strauss pconf->auth_cache = cpv->v.v; 573e11514b0SGlenn Strauss break; 5748e713130SGlenn Strauss default:/* should not happen */ 5758e713130SGlenn Strauss return; 5768e713130SGlenn Strauss } 5778e713130SGlenn Strauss } 5784a6ce4aeSJan Kneschke 5798e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 5808e713130SGlenn Strauss do { 5818e713130SGlenn Strauss mod_auth_merge_config_cpv(pconf, cpv); 5828e713130SGlenn Strauss } while ((++cpv)->k_id != -1); 5838e713130SGlenn Strauss } 584bcdc6a3bSJan Kneschke 5857c7f8c46SGlenn Strauss static void mod_auth_patch_config(request_st * const r, plugin_data * const p) { 586cc2134c8SGlenn Strauss p->conf = p->defaults; /* copy small struct instead of memcpy() */ 587cc2134c8SGlenn Strauss /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/ 5888e713130SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) { 5897c7f8c46SGlenn Strauss if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 5908e713130SGlenn Strauss mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); 5918e713130SGlenn Strauss } 5928e713130SGlenn Strauss } 59336f64b26SGlenn Strauss 5948e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 5958e713130SGlenn Strauss static const config_plugin_keys_t cpk[] = { 5968e713130SGlenn Strauss { CONST_STR_LEN("auth.backend"), 5978e713130SGlenn Strauss T_CONFIG_STRING, 5988e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 5998e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.require"), 60003b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY, 6018e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 6028e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.extern-authn"), 6038e713130SGlenn Strauss T_CONFIG_BOOL, 6048e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 605e11514b0SGlenn Strauss ,{ CONST_STR_LEN("auth.cache"), 606e11514b0SGlenn Strauss T_CONFIG_ARRAY, 607e11514b0SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 6088e713130SGlenn Strauss ,{ NULL, 0, 6098e713130SGlenn Strauss T_CONFIG_UNSET, 6108e713130SGlenn Strauss T_CONFIG_SCOPE_UNSET } 6118e713130SGlenn Strauss }; 612bcdc6a3bSJan Kneschke 6138e713130SGlenn Strauss plugin_data * const p = p_d; 6148e713130SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_auth")) 6158e713130SGlenn Strauss return HANDLER_ERROR; 616bcdc6a3bSJan Kneschke 6178e713130SGlenn Strauss /* process and validate config directives 6188e713130SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */ 6198e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 6208e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 6218e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 6228e713130SGlenn Strauss switch (cpv->k_id) { 6238e713130SGlenn Strauss case 0: /* auth.backend */ 624af3df29aSGlenn Strauss if (!buffer_is_blank(cpv->v.b)) { 6258e713130SGlenn Strauss const http_auth_backend_t * const auth_backend = 6268e713130SGlenn Strauss http_auth_backend_get(cpv->v.b); 6278e713130SGlenn Strauss if (NULL == auth_backend) { 6288e713130SGlenn Strauss log_error(srv->errh, __FILE__, __LINE__, 6298e713130SGlenn Strauss "auth.backend not supported: %s", cpv->v.b->ptr); 6308e713130SGlenn Strauss return HANDLER_ERROR; 6318e713130SGlenn Strauss } 6328e713130SGlenn Strauss *(const http_auth_backend_t **)&cpv->v.v = auth_backend; 6338e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 6348e713130SGlenn Strauss } 6358e713130SGlenn Strauss break; 6368e713130SGlenn Strauss case 1: /* auth.require */ 63703b4c993SGlenn Strauss { 63824680a91SGlenn Strauss array * const a = array_init(4); 6398e713130SGlenn Strauss if (HANDLER_GO_ON != 640b5775b99SGlenn Strauss mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) { 6418e713130SGlenn Strauss array_free(a); 6428e713130SGlenn Strauss return HANDLER_ERROR; 6438e713130SGlenn Strauss } 6448e713130SGlenn Strauss cpv->v.a = a; 6458e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 6468e713130SGlenn Strauss } 6478e713130SGlenn Strauss break; 6488e713130SGlenn Strauss case 2: /* auth.extern-authn */ 6498e713130SGlenn Strauss break; 650e11514b0SGlenn Strauss case 3: /* auth.cache */ 651e11514b0SGlenn Strauss cpv->v.v = http_auth_cache_init(cpv->v.a); 652e11514b0SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 653e11514b0SGlenn Strauss break; 6548e713130SGlenn Strauss default:/* should not happen */ 6558e713130SGlenn Strauss break; 656bcdc6a3bSJan Kneschke } 657bcdc6a3bSJan Kneschke } 658bcdc6a3bSJan Kneschke } 659bcdc6a3bSJan Kneschke 6608e713130SGlenn Strauss /* initialize p->defaults from global config context */ 6618e713130SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 6628e713130SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 6638e713130SGlenn Strauss if (-1 != cpv->k_id) 6648e713130SGlenn Strauss mod_auth_merge_config(&p->defaults, cpv); 665bcdc6a3bSJan Kneschke } 6668e713130SGlenn Strauss 6678e713130SGlenn Strauss return HANDLER_GO_ON; 6688e713130SGlenn Strauss } 669bcdc6a3bSJan Kneschke 6707c7f8c46SGlenn Strauss static handler_t mod_auth_uri_handler(request_st * const r, void *p_d) { 6717ba06c71SGlenn Strauss plugin_data *p = p_d; 672d61f3381SGlenn Strauss data_auth *dauth; 67381b2d1f0SGlenn Strauss 6747c7f8c46SGlenn Strauss mod_auth_patch_config(r, p); 67581b2d1f0SGlenn Strauss 6767ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON; 67781b2d1f0SGlenn Strauss 6787ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */ 6797ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */ 6807c7f8c46SGlenn Strauss dauth = (!r->conf.force_lowercase_filenames) 6817c7f8c46SGlenn Strauss ? (data_auth *)array_match_key_prefix(p->conf.auth_require, &r->uri.path) 6827c7f8c46SGlenn Strauss : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, &r->uri.path); 683d61f3381SGlenn Strauss if (NULL == dauth) return HANDLER_GO_ON; 6847ba06c71SGlenn Strauss 685d61f3381SGlenn Strauss { 6867ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme; 687f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) { 6887c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 6893dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) { 690f54d628cSGlenn Strauss return HANDLER_GO_ON; 691f54d628cSGlenn Strauss } 692f54d628cSGlenn Strauss } 6937c7f8c46SGlenn Strauss return scheme->checkfn(r, scheme->p_d, dauth->require, p->conf.auth_backend); 6947ba06c71SGlenn Strauss } 6957ba06c71SGlenn Strauss } 6967ba06c71SGlenn Strauss 697e11514b0SGlenn Strauss 6987ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p); 6997ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) { 7007ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID; 701e2de4e58SGlenn Strauss p->name = "auth"; 7027ba06c71SGlenn Strauss p->init = mod_auth_init; 7037ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults; 704e11514b0SGlenn Strauss p->handle_trigger = mod_auth_periodic; 7057ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler; 7067ba06c71SGlenn Strauss p->cleanup = mod_auth_free; 7077ba06c71SGlenn Strauss 70881b2d1f0SGlenn Strauss return 0; 70981b2d1f0SGlenn Strauss } 71081b2d1f0SGlenn Strauss 71181b2d1f0SGlenn Strauss 71281b2d1f0SGlenn Strauss 71381b2d1f0SGlenn Strauss 7147ba06c71SGlenn Strauss /* 7157ba06c71SGlenn Strauss * auth schemes (basic, digest, extern) 7167ba06c71SGlenn Strauss * 7177ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs) 7187ba06c71SGlenn Strauss */ 71981b2d1f0SGlenn Strauss 7201fc8a3e1SGlenn Strauss #include "sys-crypto-md.h" 7217ba06c71SGlenn Strauss #include "base64.h" 7227f4e156eSGlenn Strauss #include "rand.h" 7233dd3cde9SGlenn Strauss #include "http_header.h" 72481b2d1f0SGlenn Strauss 725010c2894SGlenn Strauss __attribute_cold__ 726ffc7a0bfSGlenn Strauss __attribute_noinline__ 72756e2881cSGlenn Strauss static handler_t 72856e2881cSGlenn Strauss mod_auth_send_400_bad_request (request_st * const r) 72956e2881cSGlenn Strauss { 7304b3a91e6SGlenn Strauss /* a field was missing or invalid */ 7317c7f8c46SGlenn Strauss r->http_status = 400; /* Bad Request */ 7327c7f8c46SGlenn Strauss r->handler_module = NULL; 7334b3a91e6SGlenn Strauss return HANDLER_FINISHED; 7344b3a91e6SGlenn Strauss } 7354b3a91e6SGlenn Strauss 73620c56cecSGlenn Strauss 73720c56cecSGlenn Strauss 738ffc7a0bfSGlenn Strauss __attribute_noinline__ 73956e2881cSGlenn Strauss static handler_t 74056e2881cSGlenn Strauss mod_auth_send_401_unauthorized_basic (request_st * const r, const buffer * const realm) 74156e2881cSGlenn Strauss { 7427c7f8c46SGlenn Strauss r->http_status = 401; 7437c7f8c46SGlenn Strauss r->handler_module = NULL; 744dc01487eSGlenn Strauss buffer_append_str3( 74526f354cbSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE, 746dc01487eSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")), 747dc01487eSGlenn Strauss CONST_STR_LEN("Basic realm=\""), 748af3df29aSGlenn Strauss BUF_PTR_LEN(realm), 749dc01487eSGlenn Strauss CONST_STR_LEN("\", charset=\"UTF-8\"")); 7504b3a91e6SGlenn Strauss return HANDLER_FINISHED; 7514b3a91e6SGlenn Strauss } 7524b3a91e6SGlenn Strauss 75381b2d1f0SGlenn Strauss 75456e2881cSGlenn Strauss __attribute_cold__ 75556e2881cSGlenn Strauss static handler_t 75656e2881cSGlenn Strauss mod_auth_basic_misconfigured (request_st * const r, const struct http_auth_backend_t * const backend) 75756e2881cSGlenn Strauss { 758cf0098eaSGlenn Strauss if (NULL == backend) 759cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 760cf0098eaSGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr); 761cf0098eaSGlenn Strauss else 762cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 763cf0098eaSGlenn Strauss "auth.require \"method\" => \"basic\" invalid " 76456e2881cSGlenn Strauss "(try \"digest\"?) for %s", r->uri.path.ptr); 76556e2881cSGlenn Strauss 7667c7f8c46SGlenn Strauss r->http_status = 500; 7677c7f8c46SGlenn Strauss r->handler_module = NULL; 7687ba06c71SGlenn Strauss return HANDLER_FINISHED; 7697ba06c71SGlenn Strauss } 7707ba06c71SGlenn Strauss 77156e2881cSGlenn Strauss 77256e2881cSGlenn Strauss static handler_t 77356e2881cSGlenn 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) 77456e2881cSGlenn Strauss { 77556e2881cSGlenn Strauss if (NULL == backend || NULL == backend->basic) 77656e2881cSGlenn Strauss return mod_auth_basic_misconfigured(r, backend); 77756e2881cSGlenn Strauss 77856e2881cSGlenn Strauss const buffer * const vb = 779ffc7a0bfSGlenn Strauss http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, 780ffc7a0bfSGlenn Strauss CONST_STR_LEN("Authorization")); 78156e2881cSGlenn Strauss if (NULL == vb || !buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Basic "))) 7827c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 7839bc61f16SGlenn Strauss #ifdef __COVERITY__ 78456e2881cSGlenn Strauss if (buffer_clen(vb) < sizeof("Basic ")-1) 7857c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 7869bc61f16SGlenn Strauss #endif 78781b2d1f0SGlenn Strauss 78856e2881cSGlenn Strauss size_t ulen = buffer_clen(vb) - (sizeof("Basic ")-1); 78956e2881cSGlenn Strauss size_t pwlen; 79056e2881cSGlenn Strauss char *pw; 79156e2881cSGlenn Strauss char user[1024]; 79281b2d1f0SGlenn Strauss 79356e2881cSGlenn Strauss /* base64-decode Authorization into username:password string; 79456e2881cSGlenn Strauss * limit base64-decoded username:password string to fit into 1k buf */ 79556e2881cSGlenn Strauss if (ulen > 1363) /*(1363/4*3+3 = 1023)*/ 79656e2881cSGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 7978047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 79856e2881cSGlenn Strauss ulen = li_base64_dec((unsigned char *)user, sizeof(user), 79956e2881cSGlenn Strauss vb->ptr+sizeof("Basic ")-1, ulen, BASE64_STANDARD); 80056e2881cSGlenn Strauss if (0 == ulen) { 80156e2881cSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 80256e2881cSGlenn Strauss "decoding base64-string failed %s", vb->ptr+sizeof("Basic ")-1); 8037c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 80481b2d1f0SGlenn Strauss } 80556e2881cSGlenn Strauss user[ulen] = '\0'; 80656e2881cSGlenn Strauss pw = memchr(user, ':', ulen); 80756e2881cSGlenn Strauss if (NULL == pw) { 80856e2881cSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "missing ':' in %s", user); 8097c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 81081b2d1f0SGlenn Strauss } 81156e2881cSGlenn Strauss *pw++ = '\0'; 81256e2881cSGlenn Strauss pwlen = (size_t)(user + ulen - pw); 81356e2881cSGlenn Strauss ulen = (size_t)(pw - 1 - user); 81481b2d1f0SGlenn Strauss 815e11514b0SGlenn Strauss plugin_data * const p = p_d; 816e11514b0SGlenn Strauss splay_tree ** sptree = p->conf.auth_cache 817e11514b0SGlenn Strauss ? &p->conf.auth_cache->sptree 818e11514b0SGlenn Strauss : NULL; 819e11514b0SGlenn Strauss http_auth_cache_entry *ae = NULL; 820fd0cc468SGlenn Strauss handler_t rc = HANDLER_ERROR; 821e11514b0SGlenn Strauss int ndx = -1; 822e11514b0SGlenn Strauss if (sptree) { 82356e2881cSGlenn Strauss ndx = http_auth_cache_hash(require, user, ulen); 824e11514b0SGlenn Strauss ae = http_auth_cache_query(sptree, ndx); 825e11514b0SGlenn Strauss if (ae && ae->require == require 82656e2881cSGlenn Strauss && ulen == ae->ulen && 0 == memcmp(user, ae->username, ulen)) 82762ccda85SGlenn Strauss rc = ck_memeq_const_time(ae->pwdigest, ae->dlen, pw, pwlen) 828e11514b0SGlenn Strauss ? HANDLER_GO_ON 829e11514b0SGlenn Strauss : HANDLER_ERROR; 830e11514b0SGlenn Strauss else /*(not found or hash collision)*/ 831e11514b0SGlenn Strauss ae = NULL; 832e11514b0SGlenn Strauss } 833e11514b0SGlenn Strauss 83456e2881cSGlenn Strauss if (NULL == ae) { 83556e2881cSGlenn Strauss const buffer userb = { user, ulen+1, 0 }; 83656e2881cSGlenn Strauss rc = backend->basic(r, backend->p_d, require, &userb, pw); 83756e2881cSGlenn Strauss } 838e11514b0SGlenn Strauss 8397ba06c71SGlenn Strauss switch (rc) { 8404b3a91e6SGlenn Strauss case HANDLER_GO_ON: 84156e2881cSGlenn Strauss http_auth_setenv(r, user, ulen, CONST_STR_LEN("Basic")); 842e11514b0SGlenn Strauss if (sptree && NULL == ae) { /*(cache (new) successful result)*/ 84371175df1SGlenn Strauss ae = http_auth_cache_entry_init(require, 0, user, ulen, user, ulen, 84471175df1SGlenn Strauss pw, pwlen); 845e11514b0SGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free); 846e11514b0SGlenn Strauss } 8474b3a91e6SGlenn Strauss break; 8484b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 8494b3a91e6SGlenn Strauss case HANDLER_FINISHED: 8507ba06c71SGlenn Strauss break; 8514b3a91e6SGlenn Strauss case HANDLER_ERROR: 8524b3a91e6SGlenn Strauss default: 8537c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 854010c2894SGlenn Strauss "password doesn't match for %s username: %s IP: %s", 855f1e8a82fSGlenn Strauss r->uri.path.ptr, user, r->con->dst_addr_buf.ptr); 8564a600dabSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/ 857fd0cc468SGlenn Strauss rc = mod_auth_send_401_unauthorized_basic(r, require->realm); 8587ba06c71SGlenn Strauss break; 85981b2d1f0SGlenn Strauss } 86081b2d1f0SGlenn Strauss 86108c03cd4SGlenn Strauss ck_memzero(pw, pwlen); 862fd0cc468SGlenn Strauss return rc; 86381b2d1f0SGlenn Strauss } 86481b2d1f0SGlenn Strauss 86581b2d1f0SGlenn Strauss 86620c56cecSGlenn Strauss 86720c56cecSGlenn Strauss enum http_auth_digest_params_e { 86820c56cecSGlenn Strauss e_username = 0 86920c56cecSGlenn Strauss ,e_realm 87020c56cecSGlenn Strauss ,e_nonce 87120c56cecSGlenn Strauss ,e_uri 87220c56cecSGlenn Strauss ,e_algorithm 87320c56cecSGlenn Strauss ,e_qop 87420c56cecSGlenn Strauss ,e_cnonce 87520c56cecSGlenn Strauss ,e_nc 87620c56cecSGlenn Strauss ,e_response 87771175df1SGlenn Strauss ,e_userstar 87871175df1SGlenn Strauss ,e_userhash 87920c56cecSGlenn Strauss ,http_auth_digest_params_sz /*(last item)*/ 88020c56cecSGlenn Strauss }; 88120c56cecSGlenn Strauss 88220c56cecSGlenn Strauss typedef struct http_auth_digest_params_t { 88320c56cecSGlenn Strauss const char *ptr[http_auth_digest_params_sz]; 88420c56cecSGlenn Strauss uint16_t len[http_auth_digest_params_sz]; 885309c1693SGlenn Strauss unix_time64_t send_nextnonce_ts; 88671175df1SGlenn Strauss unsigned char rdigest[MD_DIGEST_LENGTH_MAX];/*(earlier members get 0-init)*/ 88720c56cecSGlenn Strauss } http_auth_digest_params_t; 88820c56cecSGlenn Strauss 88920c56cecSGlenn Strauss 89020c56cecSGlenn Strauss static void 8919fe8fbaaSGlenn Strauss mod_auth_digest_mutate (http_auth_info_t * const ai, const http_auth_digest_params_t * const dp, const buffer * const method) 89220c56cecSGlenn Strauss { 89320c56cecSGlenn Strauss force_assert(method); 894c987bec1SGlenn Strauss li_md_iov_fn digest_iov = MD5_iov; 895c987bec1SGlenn Strauss /* (ai->dalgo & HTTP_AUTH_DIGEST_MD5) default */ 896b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 897c987bec1SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256) 898c987bec1SGlenn Strauss digest_iov = SHA256_iov; 8991fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 900b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256) 901c987bec1SGlenn Strauss digest_iov = SHA512_256_iov; 902b9e2be50SGlenn Strauss #endif 903b9e2be50SGlenn Strauss #endif 904c987bec1SGlenn Strauss size_t n; 905c987bec1SGlenn Strauss struct const_iovec iov[11]; 906c987bec1SGlenn Strauss char a1[MD_DIGEST_LENGTH_MAX*2+8]; /*(+1 for li_tohex(); +8 for align)*/ 907c987bec1SGlenn Strauss char a2[MD_DIGEST_LENGTH_MAX*2+8]; /*(+1 for li_tohex(); +8 for align)*/ 908c987bec1SGlenn Strauss 909c987bec1SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 910c987bec1SGlenn Strauss 911c987bec1SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 912c987bec1SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617 913c987bec1SGlenn Strauss * Errata ID: 1649 */ 914c987bec1SGlenn Strauss iov[0].iov_base = a1; 915c987bec1SGlenn Strauss iov[0].iov_len = ai->dlen*2; 916c987bec1SGlenn Strauss iov[1].iov_base = ":"; 917c987bec1SGlenn Strauss iov[1].iov_len = 1; 91820c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_nonce]; 91920c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_nonce]; 920c987bec1SGlenn Strauss iov[3].iov_base = ":"; 921c987bec1SGlenn Strauss iov[3].iov_len = 1; 92220c56cecSGlenn Strauss iov[4].iov_base = dp->ptr[e_cnonce]; 92320c56cecSGlenn Strauss iov[4].iov_len = dp->len[e_cnonce]; 924c987bec1SGlenn Strauss digest_iov(ai->digest, iov, 5); 925c987bec1SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 926c987bec1SGlenn Strauss } 927c987bec1SGlenn Strauss 928c987bec1SGlenn Strauss /* calculate H(A2) */ 9299fe8fbaaSGlenn Strauss iov[0].iov_base = method->ptr; 9309fe8fbaaSGlenn Strauss iov[0].iov_len = buffer_clen(method); 931c987bec1SGlenn Strauss iov[1].iov_base = ":"; 932c987bec1SGlenn Strauss iov[1].iov_len = 1; 93320c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_uri]; 93420c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_uri]; 935c987bec1SGlenn Strauss n = 3; 936c987bec1SGlenn Strauss #if 0 937c987bec1SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 93820c56cecSGlenn Strauss if (dp->ptr[e_qop] && buffer_eq_icase_ss(dp->ptr[e_qop], dp->len[e_qop], 93920c56cecSGlenn Strauss CONST_STR_LEN("auth-int"))) { 940c987bec1SGlenn Strauss iov[3].iov_base = ":"; 941c987bec1SGlenn Strauss iov[3].iov_len = 1; 942c987bec1SGlenn Strauss iov[4].iov_base = [body checksum]; 943c987bec1SGlenn Strauss iov[4].iov_len = ai->dlen*2; 944c987bec1SGlenn Strauss n = 5; 945c987bec1SGlenn Strauss } 946c987bec1SGlenn Strauss #endif 947c987bec1SGlenn Strauss digest_iov(ai->digest, iov, n); 948c987bec1SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 949c987bec1SGlenn Strauss 950c987bec1SGlenn Strauss /* calculate response */ 951c987bec1SGlenn Strauss iov[0].iov_base = a1; 952c987bec1SGlenn Strauss iov[0].iov_len = ai->dlen*2; 953c987bec1SGlenn Strauss iov[1].iov_base = ":"; 954c987bec1SGlenn Strauss iov[1].iov_len = 1; 95520c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_nonce]; 95620c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_nonce]; 957c987bec1SGlenn Strauss iov[3].iov_base = ":"; 958c987bec1SGlenn Strauss iov[3].iov_len = 1; 959c987bec1SGlenn Strauss n = 4; 96020c56cecSGlenn Strauss if (dp->len[e_qop]) { 96120c56cecSGlenn Strauss iov[4].iov_base = dp->ptr[e_nc]; 96220c56cecSGlenn Strauss iov[4].iov_len = dp->len[e_nc]; 963c987bec1SGlenn Strauss iov[5].iov_base = ":"; 964c987bec1SGlenn Strauss iov[5].iov_len = 1; 96520c56cecSGlenn Strauss iov[6].iov_base = dp->ptr[e_cnonce]; 96620c56cecSGlenn Strauss iov[6].iov_len = dp->len[e_cnonce]; 967c987bec1SGlenn Strauss iov[7].iov_base = ":"; 968c987bec1SGlenn Strauss iov[7].iov_len = 1; 96920c56cecSGlenn Strauss iov[8].iov_base = dp->ptr[e_qop]; 97020c56cecSGlenn Strauss iov[8].iov_len = dp->len[e_qop]; 971c987bec1SGlenn Strauss iov[9].iov_base = ":"; 972c987bec1SGlenn Strauss iov[9].iov_len = 1; 973c987bec1SGlenn Strauss n = 10; 974c987bec1SGlenn Strauss } 975c987bec1SGlenn Strauss iov[n].iov_base = a2; 976c987bec1SGlenn Strauss iov[n].iov_len = ai->dlen*2; 977c987bec1SGlenn Strauss digest_iov(ai->digest, iov, n+1); 978b9e2be50SGlenn Strauss } 979b9e2be50SGlenn Strauss 98020c56cecSGlenn Strauss 98120c56cecSGlenn Strauss static void 982309c1693SGlenn Strauss mod_auth_append_nonce (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int dalgo, int *rndptr) 98320c56cecSGlenn Strauss { 984e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 985f2610d23SGlenn Strauss buffer_append_char(b, ':'); 986e1188e77SGlenn Strauss const buffer * const nonce_secret = require->nonce_secret; 987e1188e77SGlenn Strauss int rnd; 988e1188e77SGlenn Strauss if (NULL == nonce_secret) 989e1188e77SGlenn Strauss rnd = rndptr ? *rndptr : li_rand_pseudo(); 990e1188e77SGlenn Strauss else { /*(do not directly expose random number generator single value)*/ 991e1188e77SGlenn Strauss rndptr 992e1188e77SGlenn Strauss ? (void)(rnd = *rndptr) 993e1188e77SGlenn Strauss : li_rand_pseudo_bytes((unsigned char *)&rnd, sizeof(rnd)); 994e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)rnd); 995f2610d23SGlenn Strauss buffer_append_char(b, ':'); 996e1188e77SGlenn Strauss } 997c987bec1SGlenn Strauss 998c987bec1SGlenn Strauss size_t n; 999c987bec1SGlenn Strauss struct const_iovec iov[3]; 1000c987bec1SGlenn Strauss 1001c987bec1SGlenn Strauss #if 0 1002c987bec1SGlenn Strauss char a1[LI_ITOSTRING_LENGTH]; 1003c987bec1SGlenn Strauss char a2[LI_ITOSTRING_LENGTH]; 1004c987bec1SGlenn Strauss iov[0].iov_base = a1; 1005c987bec1SGlenn Strauss iov[0].iov_len = li_itostrn(a1, sizeof(a1), cur_ts); 1006c987bec1SGlenn Strauss iov[1].iov_base = a2; 1007c987bec1SGlenn Strauss iov[1].iov_len = li_itostrn(a2, sizeof(a2), rnd); 1008c987bec1SGlenn Strauss #else 1009c987bec1SGlenn Strauss iov[0].iov_base = &cur_ts; 1010c987bec1SGlenn Strauss iov[0].iov_len = sizeof(cur_ts); 1011c987bec1SGlenn Strauss iov[1].iov_base = &rnd; 1012c987bec1SGlenn Strauss iov[1].iov_len = sizeof(rnd); 1013c987bec1SGlenn Strauss #endif 1014c987bec1SGlenn Strauss n = 2; 1015c987bec1SGlenn Strauss if (nonce_secret) { 1016c987bec1SGlenn Strauss iov[2].iov_base = nonce_secret->ptr; 1017c987bec1SGlenn Strauss iov[2].iov_len = buffer_clen(nonce_secret); 1018c987bec1SGlenn Strauss n = 3; 1019c987bec1SGlenn Strauss } 1020c987bec1SGlenn Strauss 1021c987bec1SGlenn Strauss unsigned char h[MD_DIGEST_LENGTH_MAX]; 1022e1188e77SGlenn Strauss switch (dalgo) { 1023b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 10241fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 1025e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256: 1026c987bec1SGlenn Strauss SHA512_256_iov(h, iov, n); 1027c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 1028e1188e77SGlenn Strauss break; 1029e1188e77SGlenn Strauss #endif 1030e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256: 1031c987bec1SGlenn Strauss SHA256_iov(h, iov, n); 1032c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_SHA256_BINLEN; 1033e1188e77SGlenn Strauss break; 1034e1188e77SGlenn Strauss #endif 1035e1188e77SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/ 1036e1188e77SGlenn Strauss default: 1037c987bec1SGlenn Strauss MD5_iov(h, iov, n); 1038c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_MD5_BINLEN; 1039e1188e77SGlenn Strauss break; 1040e1188e77SGlenn Strauss } 1041c987bec1SGlenn Strauss li_tohex(buffer_extend(b, n*2), n*2+1, (const char *)h, n); 1042e1188e77SGlenn Strauss } 1043e1188e77SGlenn Strauss 104420c56cecSGlenn Strauss 104520c56cecSGlenn Strauss static void 1046309c1693SGlenn Strauss mod_auth_digest_www_authenticate (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) 104720c56cecSGlenn Strauss { 1048b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm; 1049b9e2be50SGlenn Strauss int n = 0; 1050e1188e77SGlenn Strauss int algoid[3]; 1051b9e2be50SGlenn Strauss unsigned int algolen[3]; 1052b9e2be50SGlenn Strauss const char *algoname[3]; 1053b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 10541fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 1055b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) { 1056e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA512_256; 1057b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256"; 1058b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1; 1059b9e2be50SGlenn Strauss ++n; 1060b9e2be50SGlenn Strauss } 1061b9e2be50SGlenn Strauss #endif 1062b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) { 1063e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA256; 1064b9e2be50SGlenn Strauss algoname[n] = "SHA-256"; 1065b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1; 1066b9e2be50SGlenn Strauss ++n; 1067b9e2be50SGlenn Strauss } 1068b9e2be50SGlenn Strauss #endif 1069b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) { 1070e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_MD5; 1071b9e2be50SGlenn Strauss algoname[n] = "MD5"; 1072b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1; 1073b9e2be50SGlenn Strauss ++n; 1074b9e2be50SGlenn Strauss } 1075b9e2be50SGlenn Strauss 1076b9e2be50SGlenn Strauss buffer_clear(b); 1077b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) { 1078dc01487eSGlenn Strauss struct const_iovec iov[] = { 1079dc01487eSGlenn Strauss { CONST_STR_LEN("\r\nWWW-Authenticate: ") } 1080dc01487eSGlenn Strauss ,{ CONST_STR_LEN("Digest realm=\"") } 1081af3df29aSGlenn Strauss ,{ BUF_PTR_LEN(require->realm) } 1082dc01487eSGlenn Strauss ,{ CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=") } 1083dc01487eSGlenn Strauss ,{ algoname[i], algolen[i] } 1084dc01487eSGlenn Strauss ,{ CONST_STR_LEN(", nonce=\"") } 1085dc01487eSGlenn Strauss }; 1086dc01487eSGlenn Strauss buffer_append_iovec(b, iov+(0==i), sizeof(iov)/sizeof(*iov)-(0==i)); 1087e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, algoid[i], NULL); 1088b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\"")); 108971175df1SGlenn Strauss if (require->userhash) { 109071175df1SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", userhash=true")); 109171175df1SGlenn Strauss } 1092b9e2be50SGlenn Strauss if (nonce_stale) { 1093b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true")); 1094b9e2be50SGlenn Strauss } 1095b9e2be50SGlenn Strauss } 109681b2d1f0SGlenn Strauss } 109781b2d1f0SGlenn Strauss 1098c841ce3bSGlenn Strauss 109920c56cecSGlenn Strauss __attribute_noinline__ 110020c56cecSGlenn Strauss static handler_t 110120c56cecSGlenn Strauss mod_auth_send_401_unauthorized_digest(request_st * const r, const struct http_auth_require_t * const require, int nonce_stale) 110220c56cecSGlenn Strauss { 110320c56cecSGlenn Strauss r->http_status = 401; 110420c56cecSGlenn Strauss r->handler_module = NULL; 110520c56cecSGlenn Strauss mod_auth_digest_www_authenticate( 110620c56cecSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE, 110720c56cecSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")), 110820c56cecSGlenn Strauss log_epoch_secs, require, nonce_stale); 110920c56cecSGlenn Strauss return HANDLER_FINISHED; 111020c56cecSGlenn Strauss } 111120c56cecSGlenn Strauss 111220c56cecSGlenn Strauss 111320c56cecSGlenn Strauss static void 1114309c1693SGlenn Strauss mod_auth_digest_authentication_info (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int dalgo) 111520c56cecSGlenn Strauss { 11166ad325c6SGlenn Strauss buffer_clear(b); 11176ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\"")); 1118e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, dalgo, NULL); 1119f2610d23SGlenn Strauss buffer_append_char(b, '"'); 11206ad325c6SGlenn Strauss } 11216ad325c6SGlenn Strauss 112220c56cecSGlenn Strauss 1123c841ce3bSGlenn Strauss static handler_t 1124c841ce3bSGlenn 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) 1125c841ce3bSGlenn Strauss { 1126c841ce3bSGlenn Strauss plugin_data * const p = p_d; 1127c841ce3bSGlenn Strauss splay_tree **sptree = p->conf.auth_cache 1128c841ce3bSGlenn Strauss ? &p->conf.auth_cache->sptree 1129c841ce3bSGlenn Strauss : NULL; 1130c841ce3bSGlenn Strauss http_auth_cache_entry *ae = NULL; 1131c841ce3bSGlenn Strauss handler_t rc = HANDLER_GO_ON; 1132c841ce3bSGlenn Strauss int ndx = -1; 113371175df1SGlenn Strauss 113471175df1SGlenn Strauss const char *user = ai->username; 113571175df1SGlenn Strauss const uint32_t ulen = ai->ulen; 113671175df1SGlenn Strauss char userbuf[sizeof(ai->userbuf)]; 113771175df1SGlenn Strauss if (ai->userhash && ulen <= sizeof(userbuf)) { 113871175df1SGlenn Strauss /*(lowercase hex in userhash for consistency)*/ 113971175df1SGlenn Strauss const char * const restrict s = ai->username; 114071175df1SGlenn Strauss for (uint_fast32_t i = 0; i < ulen; ++i) 114171175df1SGlenn Strauss userbuf[i] = !light_isupper(s[i]) ? s[i] : (s[i] | 0x20); 114271175df1SGlenn Strauss user = userbuf; 114371175df1SGlenn Strauss } 114471175df1SGlenn Strauss 1145c841ce3bSGlenn Strauss if (sptree) { 114671175df1SGlenn Strauss ndx = http_auth_cache_hash(require, user, ulen); 1147c841ce3bSGlenn Strauss ae = http_auth_cache_query(sptree, ndx); 1148c841ce3bSGlenn Strauss if (ae && ae->require == require 1149c841ce3bSGlenn Strauss && ae->dalgo == ai->dalgo 1150c841ce3bSGlenn Strauss && ae->dlen == ai->dlen 115171175df1SGlenn Strauss && ae->klen == ulen 115271175df1SGlenn Strauss && 0 == memcmp(ae->k, user, ulen) 115371175df1SGlenn Strauss && (ae->k == ae->username || ai->userhash)) { 1154c841ce3bSGlenn Strauss memcpy(ai->digest, ae->pwdigest, ai->dlen); 115571175df1SGlenn Strauss if (ae->k != ae->username) { /*(userhash was key; copy username)*/ 115671175df1SGlenn Strauss if (__builtin_expect( (ae->ulen <= sizeof(ai->userbuf)), 1)) { 115771175df1SGlenn Strauss ai->ulen = ae->ulen; 115871175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, ae->username, ae->ulen); 115971175df1SGlenn Strauss } 116071175df1SGlenn Strauss } 1161c841ce3bSGlenn Strauss } 1162c841ce3bSGlenn Strauss else /*(not found or hash collision)*/ 1163c841ce3bSGlenn Strauss ae = NULL; 1164c841ce3bSGlenn Strauss } 1165c841ce3bSGlenn Strauss 116671175df1SGlenn Strauss if (NULL == ae) { 116771175df1SGlenn Strauss if (ai->userhash && ulen <= sizeof(ai->userbuf)) 116871175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, userbuf, ulen); 116971175df1SGlenn Strauss /* ai->username (lowercase userhash) will be replaced by username */ 1170c841ce3bSGlenn Strauss rc = backend->digest(r, backend->p_d, ai); 117171175df1SGlenn Strauss } 1172c841ce3bSGlenn Strauss 1173c841ce3bSGlenn Strauss switch (rc) { 1174c841ce3bSGlenn Strauss case HANDLER_GO_ON: 1175c841ce3bSGlenn Strauss break; 1176c841ce3bSGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 1177c841ce3bSGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 1178c841ce3bSGlenn Strauss case HANDLER_FINISHED: 1179c841ce3bSGlenn Strauss return HANDLER_FINISHED; 1180c841ce3bSGlenn Strauss case HANDLER_ERROR: 1181c841ce3bSGlenn Strauss default: 1182c841ce3bSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if unknown user)*/ 1183c841ce3bSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1184c841ce3bSGlenn Strauss } 1185c841ce3bSGlenn Strauss 1186c841ce3bSGlenn Strauss if (sptree && NULL == ae) { /*(cache digest from backend)*/ 118771175df1SGlenn Strauss ae = http_auth_cache_entry_init(require, ai->dalgo, user, ulen, 118871175df1SGlenn Strauss ai->username, ai->ulen, 118971175df1SGlenn Strauss (char *)ai->digest, ai->dlen); 1190c841ce3bSGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free); 1191c841ce3bSGlenn Strauss } 1192c841ce3bSGlenn Strauss 1193c841ce3bSGlenn Strauss return rc; 1194c841ce3bSGlenn Strauss } 1195c841ce3bSGlenn Strauss 119681b2d1f0SGlenn Strauss 119720c56cecSGlenn Strauss __attribute_cold__ 119820c56cecSGlenn Strauss static handler_t 119920c56cecSGlenn Strauss mod_auth_digest_misconfigured (request_st * const r, const struct http_auth_backend_t * const backend) 120020c56cecSGlenn Strauss { 1201cf0098eaSGlenn Strauss if (NULL == backend) 12027c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 12037c7f8c46SGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr); 1204cf0098eaSGlenn Strauss else 1205cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1206cf0098eaSGlenn Strauss "auth.require \"method\" => \"digest\" invalid " 120720c56cecSGlenn Strauss "(try \"basic\"?) for %s", r->uri.path.ptr); 120820c56cecSGlenn Strauss 12097c7f8c46SGlenn Strauss r->http_status = 500; 12107c7f8c46SGlenn Strauss r->handler_module = NULL; 12117ba06c71SGlenn Strauss return HANDLER_FINISHED; 12127ba06c71SGlenn Strauss } 12137ba06c71SGlenn Strauss 12147ba06c71SGlenn Strauss 121520c56cecSGlenn Strauss static void 12166881f79cSGlenn Strauss mod_auth_digest_parse_authorization (http_auth_digest_params_t * const dp, const char *c) 121720c56cecSGlenn Strauss { 121820c56cecSGlenn Strauss struct digest_kv { 121920c56cecSGlenn Strauss const char *key; 122020c56cecSGlenn Strauss uint32_t klen; 122120c56cecSGlenn Strauss enum http_auth_digest_params_e id; 122220c56cecSGlenn Strauss }; 122320c56cecSGlenn Strauss 122420c56cecSGlenn Strauss static const struct digest_kv dkv[] = { 12256881f79cSGlenn Strauss { CONST_STR_LEN("username"), e_username }, 12266881f79cSGlenn Strauss { CONST_STR_LEN("realm"), e_realm }, 12276881f79cSGlenn Strauss { CONST_STR_LEN("nonce"), e_nonce }, 12286881f79cSGlenn Strauss { CONST_STR_LEN("uri"), e_uri }, 12296881f79cSGlenn Strauss { CONST_STR_LEN("algorithm"), e_algorithm }, 12306881f79cSGlenn Strauss { CONST_STR_LEN("qop"), e_qop }, 12316881f79cSGlenn Strauss { CONST_STR_LEN("cnonce"), e_cnonce }, 12326881f79cSGlenn Strauss { CONST_STR_LEN("nc"), e_nc }, 12336881f79cSGlenn Strauss { CONST_STR_LEN("response"), e_response }, 123471175df1SGlenn Strauss { CONST_STR_LEN("username*"), e_userstar }, 123571175df1SGlenn Strauss { CONST_STR_LEN("userhash"), e_userhash }, 123620c56cecSGlenn Strauss 123720c56cecSGlenn Strauss { NULL, 0, http_auth_digest_params_sz } 123820c56cecSGlenn Strauss }; 123981b2d1f0SGlenn Strauss 124081b2d1f0SGlenn Strauss /* parse credentials from client */ 12416881f79cSGlenn Strauss /* (caller must pass c pointing to string after "Digest ") */ 12426881f79cSGlenn Strauss for (const char *e; *c; c++) { 124381b2d1f0SGlenn Strauss /* skip whitespaces */ 12446881f79cSGlenn Strauss while (*c == ' ' || *c == '\t' || *c == ',') ++c; 124581b2d1f0SGlenn Strauss if (!*c) break; 12466881f79cSGlenn Strauss for (e = c; *e!='=' && *e!=' ' && *e!='\t' && *e!='\0'; ++e) ; 12476881f79cSGlenn Strauss const uint32_t tlen = (uint32_t)(e - c); 124881b2d1f0SGlenn Strauss 124920c56cecSGlenn Strauss for (int i = 0; dkv[i].key; ++i) { 12506881f79cSGlenn Strauss if (tlen != dkv[i].klen || 0 != memcmp(c, dkv[i].key, tlen)) 125120c56cecSGlenn Strauss continue; 12526881f79cSGlenn Strauss c += tlen; 12536881f79cSGlenn Strauss /* detect and step over '='; ignore BWS (bad whitespace) */ 12546881f79cSGlenn Strauss if (__builtin_expect( (*c != '='), 0)) { 12556881f79cSGlenn Strauss while (*c == ' ' || *c == '\t') ++c; 12566881f79cSGlenn Strauss if (*c != '=') return; /*(including '\0')*/ 125720c56cecSGlenn Strauss } 12586881f79cSGlenn Strauss do { ++c; } while (*c == ' ' || *c == '\t'); 12596881f79cSGlenn Strauss 12606881f79cSGlenn Strauss if (*c == '"') { 12616881f79cSGlenn Strauss for (e = ++c; *e != '"' && *e != '\0'; ++e) { 12626881f79cSGlenn Strauss if (*e == '\\' && *++e == '\0') return; 12636881f79cSGlenn Strauss } 12646881f79cSGlenn Strauss if (*e != '"') return; 12656881f79cSGlenn Strauss /* value with "..." *//*(XXX: quoted value not unescaped)*/ 126620c56cecSGlenn Strauss } 126720c56cecSGlenn Strauss else { 12686881f79cSGlenn Strauss for (e = c; *e!=',' && *e!=' ' && *e!='\t' && *e!='\0'; ++e) ; 12696881f79cSGlenn Strauss /* value without "..." */ 12706881f79cSGlenn Strauss } 127120c56cecSGlenn Strauss dp->ptr[dkv[i].id] = c; 12726881f79cSGlenn Strauss dp->len[dkv[i].id] = (uint16_t)(e - c); 12736881f79cSGlenn Strauss c = e; 12746881f79cSGlenn Strauss if (*c != ',') { 12756881f79cSGlenn Strauss /*(could more strictly check for linear whitespace)*/ 12766881f79cSGlenn Strauss c = strchr(c, ','); 12776881f79cSGlenn Strauss if (!c) return; 127881b2d1f0SGlenn Strauss } 127981b2d1f0SGlenn Strauss break; 128081b2d1f0SGlenn Strauss } 128181b2d1f0SGlenn Strauss } 128281b2d1f0SGlenn Strauss } 128381b2d1f0SGlenn Strauss 128481b2d1f0SGlenn Strauss 128520c56cecSGlenn Strauss static handler_t 128671175df1SGlenn Strauss mod_auth_digest_validate_userstar (request_st * const r, http_auth_digest_params_t * const dp, http_auth_info_t * const ai) 128771175df1SGlenn Strauss { 128871175df1SGlenn Strauss /*assert(dp->ptr[e_userstar]);*/ 128971175df1SGlenn Strauss 129071175df1SGlenn Strauss if (dp->len[e_userhash] == 4) { /*("true")*/ 129171175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 129271175df1SGlenn Strauss "digest: invalid \"username*\" with \"userhash\" = true"); 129371175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 129471175df1SGlenn Strauss } 129571175df1SGlenn Strauss 129671175df1SGlenn Strauss /* "username*" RFC5987 ext-value 129771175df1SGlenn Strauss * ext-value = charset "'" [ language ] "'" value-chars */ 129871175df1SGlenn Strauss const char *ptr = dp->ptr[e_userstar]; 129971175df1SGlenn Strauss uint32_t len = dp->len[e_userstar]; 130071175df1SGlenn Strauss /* validate and step over charset... */ 130171175df1SGlenn Strauss if ((*ptr | 0x20) == 'u' && len > 5 130271175df1SGlenn Strauss && buffer_eq_icase_ssn(ptr, "utf-8", 5)) 130371175df1SGlenn Strauss ptr += 5; 130471175df1SGlenn Strauss else if ((*ptr | 0x20) == 'i' && len > 10 130571175df1SGlenn Strauss && buffer_eq_icase_ssn(ptr, "iso-8859-1", 10)) 130671175df1SGlenn Strauss ptr += 10; 130771175df1SGlenn Strauss else 130860c9612aSGlenn Strauss ptr = "\n"; /*(invalid char; (not '\''); error below)*/ 130971175df1SGlenn Strauss /* step over ...'language'... */ 131071175df1SGlenn Strauss if (*ptr++ != '\'' 131171175df1SGlenn Strauss || !(ptr = memchr(ptr, '\'', 131271175df1SGlenn Strauss len - (uint32_t)(ptr - dp->ptr[e_userstar])))) { 131371175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 131471175df1SGlenn Strauss "digest: invalid \"username*\" ext-value"); 131571175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 131671175df1SGlenn Strauss } 131771175df1SGlenn Strauss ++ptr; 131871175df1SGlenn Strauss 131971175df1SGlenn Strauss /* decode %XX encodings (could be more efficient by combining tests) */ 132071175df1SGlenn Strauss buffer * const tb = r->tmp_buf; 132171175df1SGlenn Strauss buffer_copy_string_len(tb, ptr, len-(uint32_t)(ptr - dp->ptr[e_userstar])); 132271175df1SGlenn Strauss buffer_urldecode_path(tb); 132371175df1SGlenn Strauss if (dp->ptr[e_userstar][0] == 'u' && !buffer_is_valid_UTF8(tb)) { 132471175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 132571175df1SGlenn Strauss "digest: invalid \"username*\" invalid UTF-8"); 132671175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 132771175df1SGlenn Strauss } 132871175df1SGlenn Strauss len = buffer_clen(tb); 132971175df1SGlenn Strauss if (len > sizeof(ai->userbuf)) { 133071175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 133171175df1SGlenn Strauss "digest: invalid \"username*\" too long"); 133271175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 133371175df1SGlenn Strauss } 133471175df1SGlenn Strauss for (ptr = tb->ptr; *ptr; ++ptr) { 133571175df1SGlenn Strauss /* prohibit decoded control chars, including '\0','\r','\n' */ 133671175df1SGlenn Strauss /* (theoretically could permit '\t', but not currently done) */ 133771175df1SGlenn Strauss if (*(unsigned char *)ptr < 0x20 || *ptr == 127) { /* iscntrl() */ 133871175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 133971175df1SGlenn Strauss "digest: invalid \"username*\" contains ctrl chars"); 134071175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 134171175df1SGlenn Strauss } 134271175df1SGlenn Strauss } 134371175df1SGlenn Strauss 134471175df1SGlenn Strauss ai->ulen = len; 134571175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, tb->ptr, len); 134671175df1SGlenn Strauss return HANDLER_GO_ON; 134771175df1SGlenn Strauss } 134871175df1SGlenn Strauss 134971175df1SGlenn Strauss 135071175df1SGlenn Strauss static handler_t 135120c56cecSGlenn 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) 135220c56cecSGlenn Strauss { 135320c56cecSGlenn Strauss /* check for required parameters */ 135420c56cecSGlenn Strauss if ((!dp->ptr[e_qop] || (dp->ptr[e_nc] && dp->ptr[e_cnonce])) 135571175df1SGlenn Strauss && ((NULL != dp->ptr[e_username]) ^ (NULL != dp->ptr[e_userstar])) 135620c56cecSGlenn Strauss && dp->ptr[e_realm] 135720c56cecSGlenn Strauss && dp->ptr[e_nonce] 135820c56cecSGlenn Strauss && dp->ptr[e_uri] 135920c56cecSGlenn Strauss && dp->ptr[e_response]) { 136020c56cecSGlenn Strauss ai->username = dp->ptr[e_username]; 136120c56cecSGlenn Strauss ai->ulen = dp->len[e_username]; 136220c56cecSGlenn Strauss ai->realm = dp->ptr[e_realm]; 136320c56cecSGlenn Strauss ai->rlen = dp->len[e_realm]; 136471175df1SGlenn Strauss ai->userhash = (dp->len[e_userhash]==4);/*("true", not "false",absent)*/ 136571175df1SGlenn Strauss if (!ai->username) { /* (dp->ptr[e_userstar]) */ 136671175df1SGlenn Strauss if (HANDLER_GO_ON != mod_auth_digest_validate_userstar(r, dp, ai)) 136771175df1SGlenn Strauss return HANDLER_FINISHED; 136871175df1SGlenn Strauss } 136920c56cecSGlenn Strauss } 137020c56cecSGlenn Strauss else { 13717c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 137281b2d1f0SGlenn Strauss "digest: missing field"); 13737c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 137481b2d1f0SGlenn Strauss } 137581b2d1f0SGlenn Strauss 137620c56cecSGlenn Strauss if (!buffer_eq_slen(require->realm, ai->realm, ai->rlen)) { 13777c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 13785a6e4df8SGlenn Strauss "digest: realm mismatch"); 13797c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 13805a6e4df8SGlenn Strauss } 13815a6e4df8SGlenn Strauss 138220c56cecSGlenn Strauss if (!mod_auth_algorithm_parse(ai,dp->ptr[e_algorithm],dp->len[e_algorithm]) 138320c56cecSGlenn Strauss || !(require->algorithm & ai->dalgo & ~HTTP_AUTH_DIGEST_SESS)) { 13847c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 138520c56cecSGlenn Strauss "digest: (%.*s): invalid", 138620c56cecSGlenn Strauss (int)dp->len[e_algorithm], dp->ptr[e_algorithm]); 138712ac70d0SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1388b9e2be50SGlenn Strauss } 1389b9e2be50SGlenn Strauss 139020c56cecSGlenn Strauss /* *-sess requires nonce and cnonce */ 139120c56cecSGlenn Strauss if ((ai->dalgo & HTTP_AUTH_DIGEST_SESS) 139220c56cecSGlenn Strauss && (!dp->ptr[e_nonce] || !dp->ptr[e_cnonce])) { 13937c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 139420c56cecSGlenn Strauss "digest: (%.*s): missing field", 139520c56cecSGlenn Strauss (int)dp->len[e_algorithm], dp->ptr[e_algorithm]); 13967c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 139781b2d1f0SGlenn Strauss } 139881b2d1f0SGlenn Strauss 139920c56cecSGlenn Strauss if (0 != li_hex2bin(dp->rdigest, sizeof(dp->rdigest), 140020c56cecSGlenn Strauss dp->ptr[e_response], dp->len[e_response]) 140120c56cecSGlenn Strauss || dp->len[e_response] != (ai->dlen << 1)) { 14027c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 140320c56cecSGlenn Strauss "digest: (%s): invalid format", dp->ptr[e_response]); 14047c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 1405b9e2be50SGlenn Strauss } 1406b9e2be50SGlenn Strauss 140720c56cecSGlenn Strauss if (dp->ptr[e_qop]&& buffer_eq_icase_ss(dp->ptr[e_qop], dp->len[e_qop], 140820c56cecSGlenn Strauss CONST_STR_LEN("auth-int"))) { 14097c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 141081b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 14117c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 141281b2d1f0SGlenn Strauss } 141381b2d1f0SGlenn Strauss 141481b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 141581b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 141681b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 141781b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 141881b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 141981b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 14207c7f8c46SGlenn Strauss * uri validation step is bypassed. r->target_orig is 14218131e439SGlenn Strauss * original request-target sent in client request. */ 142220c56cecSGlenn Strauss if (!buffer_eq_slen(&r->target_orig, dp->ptr[e_uri], dp->len[e_uri])) { 14237c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 142420c56cecSGlenn Strauss "digest: auth failed: uri mismatch (%s != %.*s), IP: %s", 142520c56cecSGlenn Strauss r->target_orig.ptr, (int)dp->len[e_uri], dp->ptr[e_uri], 1426f1e8a82fSGlenn Strauss r->con->dst_addr_buf.ptr); 14277c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 142881b2d1f0SGlenn Strauss } 142920c56cecSGlenn Strauss 143020c56cecSGlenn Strauss return HANDLER_GO_ON; 143181b2d1f0SGlenn Strauss } 143281b2d1f0SGlenn Strauss 143320c56cecSGlenn Strauss 143420c56cecSGlenn Strauss static handler_t 143520c56cecSGlenn 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) 143620c56cecSGlenn Strauss { 1437e1188e77SGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 1438e1188e77SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 1439e1188e77SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 1440e1188e77SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 1441e1188e77SGlenn Strauss * verifiable) if nanoseconds were included with seconds as part of the 1442e1188e77SGlenn Strauss * nonce "timestamp:secret". However, doing so would expose a high 1443e1188e77SGlenn Strauss * precision timestamp of the system to attackers. timestamp in nonce 1444e1188e77SGlenn Strauss * could theoretically be modified and still produce same md5sum, but 1445e1188e77SGlenn Strauss * that is highly unlikely within a 10 min (moving) window of valid 1446e1188e77SGlenn Strauss * time relative to current time (now). 1447e1188e77SGlenn Strauss * If it is desired to validate that nonces were generated by server, 1448e1188e77SGlenn Strauss * then specify auth.require = ( ... => ( "secret" => "..." ) ) 1449e1188e77SGlenn Strauss * When secret is specified, then instead of nanoseconds, the random 1450e1188e77SGlenn Strauss * data value (included for unique nonces) will be exposed in the nonce 1451e1188e77SGlenn Strauss * along with the timestamp, and the additional secret will be used to 1452e1188e77SGlenn Strauss * validate that the server generated the nonce using that secret. */ 1453309c1693SGlenn Strauss unix_time64_t ts = 0; 145420c56cecSGlenn Strauss const unsigned char * const nonce = (unsigned char *)dp->ptr[e_nonce]; 145520c56cecSGlenn Strauss int i; 1456309c1693SGlenn Strauss for (i = 0; i < 16 && light_isxdigit(nonce[i]); ++i) 1457309c1693SGlenn Strauss ts = (unix_time64_t)((uint64_t)ts << 4) | hex2int(nonce[i]); 145820c56cecSGlenn Strauss 1459309c1693SGlenn Strauss const unix_time64_t cur_ts = log_epoch_secs; 146020c56cecSGlenn Strauss if (nonce[i] != ':' || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/ 1461e1188e77SGlenn Strauss /* nonce is stale; have client regenerate digest */ 146220c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, ai->dalgo); 1463e1188e77SGlenn Strauss } 1464e1188e77SGlenn Strauss 146520c56cecSGlenn Strauss if (cur_ts - ts > 540) /*(9 mins)*/ 146620c56cecSGlenn Strauss dp->send_nextnonce_ts = cur_ts; 1467e1188e77SGlenn Strauss 1468e1188e77SGlenn Strauss if (require->nonce_secret) { 1469e1188e77SGlenn Strauss unsigned int rnd = 0; 147020c56cecSGlenn Strauss for (int j = i+8; i < j && light_isxdigit(nonce[i]); ++i) { 147120c56cecSGlenn Strauss rnd = (rnd << 4) + hex2int(nonce[i]); 1472e1188e77SGlenn Strauss } 1473e1188e77SGlenn Strauss if (nonce[i] != ':') { 1474e1188e77SGlenn Strauss /* nonce is invalid; 1475e1188e77SGlenn Strauss * expect extra field w/ require->nonce_secret */ 1476e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1477e1188e77SGlenn Strauss "digest: nonce invalid"); 1478e1188e77SGlenn Strauss return mod_auth_send_400_bad_request(r); 1479e1188e77SGlenn Strauss } 1480e1188e77SGlenn Strauss buffer * const tb = r->tmp_buf; 1481e1188e77SGlenn Strauss buffer_clear(tb); 148220c56cecSGlenn Strauss mod_auth_append_nonce(tb, cur_ts, require, ai->dalgo, (int *)&rnd); 148320c56cecSGlenn Strauss if (!buffer_eq_slen(tb, dp->ptr[e_nonce], dp->len[e_nonce])) { 1484e1188e77SGlenn Strauss /* nonce not generated using current require->nonce_secret */ 1485e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1486e1188e77SGlenn Strauss "digest: nonce mismatch"); 1487e1188e77SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1488e1188e77SGlenn Strauss } 1489e1188e77SGlenn Strauss } 149081b2d1f0SGlenn Strauss 14914b3a91e6SGlenn Strauss return HANDLER_GO_ON; 149281b2d1f0SGlenn Strauss } 149381b2d1f0SGlenn Strauss 149420c56cecSGlenn Strauss 149520c56cecSGlenn Strauss static handler_t 149620c56cecSGlenn 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) 149720c56cecSGlenn Strauss { 149820c56cecSGlenn Strauss if (NULL == backend || NULL == backend->digest) 149920c56cecSGlenn Strauss return mod_auth_digest_misconfigured(r, backend); 150020c56cecSGlenn Strauss 150120c56cecSGlenn Strauss const buffer * const vb = 150220c56cecSGlenn Strauss http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, 150320c56cecSGlenn Strauss CONST_STR_LEN("Authorization")); 150420c56cecSGlenn Strauss if (NULL == vb || !buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) 150520c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 150620c56cecSGlenn Strauss #ifdef __COVERITY__ 150720c56cecSGlenn Strauss if (buffer_clen(vb) < sizeof("Digest ")-1) 150820c56cecSGlenn Strauss return mod_auth_send_400_bad_request(r); 150920c56cecSGlenn Strauss #endif 151020c56cecSGlenn Strauss 151120c56cecSGlenn Strauss http_auth_digest_params_t dp; 151220c56cecSGlenn Strauss http_auth_info_t ai; 151320c56cecSGlenn Strauss handler_t rc; 151420c56cecSGlenn Strauss 151571175df1SGlenn Strauss /* XXX: should use offsetof() (if portable enough) */ 151620c56cecSGlenn Strauss memset(&dp, 0, sizeof(dp) - sizeof(dp.rdigest)); 151720c56cecSGlenn Strauss 15186881f79cSGlenn Strauss mod_auth_digest_parse_authorization(&dp, vb->ptr + sizeof("Digest ")-1); 151920c56cecSGlenn Strauss 152020c56cecSGlenn Strauss rc = mod_auth_digest_validate_params(r, require, &dp, &ai); 152120c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0)) 152220c56cecSGlenn Strauss return rc; 152320c56cecSGlenn Strauss 152420c56cecSGlenn Strauss rc = mod_auth_digest_validate_nonce(r, require, &dp, &ai); 152520c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0)) 152620c56cecSGlenn Strauss return rc; 152720c56cecSGlenn Strauss 152820c56cecSGlenn Strauss rc = mod_auth_digest_get(r, p_d, require, backend, &ai); 152920c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0)) 153020c56cecSGlenn Strauss return rc; 153120c56cecSGlenn Strauss 15329fe8fbaaSGlenn Strauss mod_auth_digest_mutate(&ai, &dp, http_method_buf(r->http_method)); 153320c56cecSGlenn Strauss 153420c56cecSGlenn Strauss if (!ck_memeq_const_time_fixed_len(dp.rdigest, ai.digest, ai.dlen)) { 153520c56cecSGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//*skip clear since mutated*/ 153620c56cecSGlenn Strauss /* digest not ok */ 153720c56cecSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 153820c56cecSGlenn Strauss "digest: auth failed for %.*s: wrong password, IP: %s", 1539f1e8a82fSGlenn Strauss (int)ai.ulen, ai.username, r->con->dst_addr_buf.ptr); 154020c56cecSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/ 154120c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 15427ba06c71SGlenn Strauss } 154320c56cecSGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//* skip clear since mutated */ 154420c56cecSGlenn Strauss 154520c56cecSGlenn Strauss /* check authorization (authz); string args must be '\0'-terminated) */ 154620c56cecSGlenn Strauss buffer * const tb = r->tmp_buf; 154720c56cecSGlenn Strauss buffer_copy_string_len(tb, ai.username, ai.ulen); 154820c56cecSGlenn Strauss if (!http_auth_match_rules(require, tb->ptr, NULL, NULL)) 154920c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 155020c56cecSGlenn Strauss 155120c56cecSGlenn Strauss if (dp.send_nextnonce_ts) { 155220c56cecSGlenn Strauss /*(send nextnonce when expiration is approaching)*/ 155320c56cecSGlenn Strauss mod_auth_digest_authentication_info( 155420c56cecSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_OTHER, 155520c56cecSGlenn Strauss CONST_STR_LEN("Authentication-Info")), 155620c56cecSGlenn Strauss dp.send_nextnonce_ts /*(cur_ts)*/, require, ai.dalgo); 155720c56cecSGlenn Strauss } 155820c56cecSGlenn Strauss 155920c56cecSGlenn Strauss http_auth_setenv(r, ai.username, ai.ulen, CONST_STR_LEN("Digest")); 156020c56cecSGlenn Strauss return HANDLER_GO_ON; 156120c56cecSGlenn Strauss } 156220c56cecSGlenn Strauss 156320c56cecSGlenn Strauss 15647ba06c71SGlenn Strauss 15657c7f8c46SGlenn 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) { 15667ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 15677c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 15687ba06c71SGlenn Strauss UNUSED(p_d); 15697ba06c71SGlenn Strauss UNUSED(backend); 15703dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) { 1571f9d58670SStefan Bühler return HANDLER_GO_ON; 1572bcdc6a3bSJan Kneschke } else { 15737c7f8c46SGlenn Strauss r->http_status = 401; 15747c7f8c46SGlenn Strauss r->handler_module = NULL; 1575bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 15767187271fSStefan Bühler } 1577bcdc6a3bSJan Kneschke } 1578