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"))) 116d6f692f1SGlenn Strauss ac->max_age = (time_t)config_plugin_value_to_int32(du, ac->max_age); 117e11514b0SGlenn Strauss } 118e11514b0SGlenn Strauss return ac; 119e11514b0SGlenn Strauss } 120e11514b0SGlenn Strauss 121e11514b0SGlenn Strauss static int 122e11514b0SGlenn Strauss http_auth_cache_hash (const struct http_auth_require_t * const require, const char *username, const uint32_t ulen) 123e11514b0SGlenn Strauss { 124e11514b0SGlenn Strauss uint32_t h = /*(hash pointer value, which includes realm and permissions)*/ 125e11514b0SGlenn Strauss djbhash((char *)(intptr_t)require, sizeof(intptr_t), DJBHASH_INIT); 126e11514b0SGlenn Strauss h = djbhash(username, ulen, h); 127e11514b0SGlenn Strauss /* strip highest bit of hash value for splaytree (see splaytree_djbhash())*/ 128e11514b0SGlenn Strauss return (int32_t)(h & ~(((uint32_t)1) << 31)); 129e11514b0SGlenn Strauss } 130e11514b0SGlenn Strauss 131e11514b0SGlenn Strauss static http_auth_cache_entry * 132e11514b0SGlenn Strauss http_auth_cache_query (splay_tree ** const sptree, const int ndx) 133e11514b0SGlenn Strauss { 134e11514b0SGlenn Strauss *sptree = splaytree_splay(*sptree, ndx); 135e11514b0SGlenn Strauss return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL; 136e11514b0SGlenn Strauss } 137e11514b0SGlenn Strauss 138e11514b0SGlenn Strauss static void 139e11514b0SGlenn Strauss http_auth_cache_insert (splay_tree ** const sptree, const int ndx, void * const data, void(data_free_fn)(void *)) 140e11514b0SGlenn Strauss { 141e11514b0SGlenn Strauss /*(not necessary to re-splay (with current usage) since single-threaded 142e11514b0SGlenn Strauss * and splaytree has not been modified since http_auth_cache_query())*/ 143e11514b0SGlenn Strauss /* *sptree = splaytree_splay(*sptree, ndx); */ 144e11514b0SGlenn Strauss if (NULL == *sptree || (*sptree)->key != ndx) 145e11514b0SGlenn Strauss *sptree = splaytree_insert(*sptree, ndx, data); 146e11514b0SGlenn Strauss else { /* collision; replace old entry */ 147e11514b0SGlenn Strauss data_free_fn((*sptree)->data); 148e11514b0SGlenn Strauss (*sptree)->data = data; 149e11514b0SGlenn Strauss } 150e11514b0SGlenn Strauss } 151e11514b0SGlenn Strauss 152e11514b0SGlenn Strauss /* walk though cache, collect expired ids, and remove them in a second loop */ 153e11514b0SGlenn Strauss static void 154309c1693SGlenn 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) 155e11514b0SGlenn Strauss { 156e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/ 157e11514b0SGlenn Strauss if (t->left) 158e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->left, keys, ndx, max_age, cur_ts); 159e11514b0SGlenn Strauss if (t->right) 160e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->right, keys, ndx, max_age, cur_ts); 161e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/ 162e11514b0SGlenn Strauss 163e11514b0SGlenn Strauss const http_auth_cache_entry * const ae = t->data; 164e11514b0SGlenn Strauss if (cur_ts - ae->ctime > max_age) 165e11514b0SGlenn Strauss keys[(*ndx)++] = t->key; 166e11514b0SGlenn Strauss } 167e11514b0SGlenn Strauss 168e11514b0SGlenn Strauss __attribute_noinline__ 169e11514b0SGlenn Strauss static void 170309c1693SGlenn Strauss mod_auth_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const unix_time64_t cur_ts) 171e11514b0SGlenn Strauss { 172e11514b0SGlenn Strauss splay_tree *sptree = *sptree_ptr; 173e11514b0SGlenn Strauss int max_ndx, i; 174e11514b0SGlenn Strauss int keys[8192]; /* 32k size on stack */ 175e11514b0SGlenn Strauss do { 176e11514b0SGlenn Strauss if (!sptree) break; 177e11514b0SGlenn Strauss max_ndx = 0; 178e11514b0SGlenn Strauss mod_auth_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts); 179e11514b0SGlenn Strauss for (i = 0; i < max_ndx; ++i) { 180e11514b0SGlenn Strauss int ndx = keys[i]; 181e11514b0SGlenn Strauss sptree = splaytree_splay(sptree, ndx); 182e11514b0SGlenn Strauss if (sptree && sptree->key == ndx) { 183e11514b0SGlenn Strauss http_auth_cache_entry_free(sptree->data); 184e11514b0SGlenn Strauss sptree = splaytree_delete(sptree, ndx); 185e11514b0SGlenn Strauss } 186e11514b0SGlenn Strauss } 187e11514b0SGlenn Strauss } while (max_ndx == sizeof(keys)/sizeof(int)); 188e11514b0SGlenn Strauss *sptree_ptr = sptree; 189e11514b0SGlenn Strauss } 190e11514b0SGlenn Strauss 191e11514b0SGlenn Strauss TRIGGER_FUNC(mod_auth_periodic) 192e11514b0SGlenn Strauss { 193e11514b0SGlenn Strauss const plugin_data * const p = p_d; 194309c1693SGlenn Strauss const unix_time64_t cur_ts = log_monotonic_secs; 195e11514b0SGlenn Strauss if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/ 196e11514b0SGlenn Strauss UNUSED(srv); 197e11514b0SGlenn Strauss 198e11514b0SGlenn Strauss /* future: might construct array of (http_auth_cache *) at startup 199e11514b0SGlenn Strauss * to avoid the need to search for them here */ 20033e400b4SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 20133e400b4SGlenn Strauss if (NULL == p->cvlist) return HANDLER_GO_ON; 20233e400b4SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 203e11514b0SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 204e11514b0SGlenn Strauss for (; cpv->k_id != -1; ++cpv) { 205e11514b0SGlenn Strauss if (cpv->k_id != 3) continue; /* k_id == 3 for auth.cache */ 206e11514b0SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL) continue; 207e11514b0SGlenn Strauss http_auth_cache *ac = cpv->v.v; 208e11514b0SGlenn Strauss mod_auth_periodic_cleanup(&ac->sptree, ac->max_age, cur_ts); 209e11514b0SGlenn Strauss } 210e11514b0SGlenn Strauss } 211e11514b0SGlenn Strauss 212e11514b0SGlenn Strauss return HANDLER_GO_ON; 213e11514b0SGlenn Strauss } 214e11514b0SGlenn Strauss 215e11514b0SGlenn Strauss 216e11514b0SGlenn Strauss 217e11514b0SGlenn Strauss 2187c7f8c46SGlenn 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); 2197c7f8c46SGlenn 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); 2207c7f8c46SGlenn 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); 2214b3a91e6SGlenn Strauss 222bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) { 223e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL }; 224e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL }; 2257ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL }; 226e11514b0SGlenn Strauss plugin_data *p = calloc(1, sizeof(*p)); 227e11514b0SGlenn Strauss force_assert(p); 2287ba06c71SGlenn Strauss 2297ba06c71SGlenn Strauss /* register http_auth_scheme_* */ 230e11514b0SGlenn Strauss http_auth_scheme_basic.p_d = p; 2317ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic); 232e11514b0SGlenn Strauss http_auth_scheme_digest.p_d = p; 2337ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest); 2347ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern); 235bcdc6a3bSJan Kneschke 236bcdc6a3bSJan Kneschke return p; 237bcdc6a3bSJan Kneschke } 238bcdc6a3bSJan Kneschke 239b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) { 240b73949e0SGlenn Strauss plugin_data * const p = p_d; 2418e713130SGlenn Strauss if (NULL == p->cvlist) return; 2428e713130SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 2438e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 2448e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 2458e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 2468e713130SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 2478e713130SGlenn Strauss switch (cpv->k_id) { 2488e713130SGlenn Strauss case 1: /* auth.require */ 2498e713130SGlenn Strauss array_free(cpv->v.v); 2508e713130SGlenn Strauss break; 251e11514b0SGlenn Strauss case 3: /* auth.cache */ 252e11514b0SGlenn Strauss http_auth_cache_free(cpv->v.v); 253e11514b0SGlenn Strauss break; 2548e713130SGlenn Strauss default: 2558e713130SGlenn Strauss break; 2568e713130SGlenn Strauss } 2578e713130SGlenn Strauss } 2588e713130SGlenn Strauss } 2591a8ac120SGlenn Strauss 2601a8ac120SGlenn Strauss http_auth_dumbdata_reset(); 2618e713130SGlenn Strauss } 2628e713130SGlenn Strauss 2637ba06c71SGlenn Strauss /* data type for mod_auth structured data 2647ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */ 2657ba06c71SGlenn Strauss typedef struct { 2667ba06c71SGlenn Strauss DATA_UNSET; 2677ba06c71SGlenn Strauss http_auth_require_t *require; 2687ba06c71SGlenn Strauss } data_auth; 2697ba06c71SGlenn Strauss 2707ba06c71SGlenn Strauss static void data_auth_free(data_unset *d) 2717ba06c71SGlenn Strauss { 2727ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d; 273ad9b7e00SGlenn Strauss free(dauth->key.ptr); 2747ba06c71SGlenn Strauss http_auth_require_free(dauth->require); 2757ba06c71SGlenn Strauss free(dauth); 2767ba06c71SGlenn Strauss } 2777ba06c71SGlenn Strauss 2787ba06c71SGlenn Strauss static data_auth *data_auth_init(void) 2797ba06c71SGlenn Strauss { 2808c7f1dfbSGlenn Strauss static const struct data_methods fn = { 2818c7f1dfbSGlenn Strauss NULL, /* copy must not be called on this data */ 2828c7f1dfbSGlenn Strauss data_auth_free, 2838c7f1dfbSGlenn Strauss NULL, /* insert_dup must not be called on this data */ 2848c7f1dfbSGlenn Strauss }; 2857ba06c71SGlenn Strauss data_auth * const dauth = calloc(1, sizeof(*dauth)); 2867ba06c71SGlenn Strauss force_assert(NULL != dauth); 2877ba06c71SGlenn Strauss dauth->type = TYPE_OTHER; 2888c7f1dfbSGlenn Strauss dauth->fn = &fn; 2897ba06c71SGlenn Strauss 2907ba06c71SGlenn Strauss dauth->require = http_auth_require_init(); 2917ba06c71SGlenn Strauss 2927ba06c71SGlenn Strauss return dauth; 2937ba06c71SGlenn Strauss } 2947ba06c71SGlenn Strauss 295870b0c13SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s, size_t len) { 296870b0c13SGlenn Strauss if (0 == len) { 297b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_MD5; 298b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 299b9e2be50SGlenn Strauss return 1; 300b9e2be50SGlenn Strauss } 301b9e2be50SGlenn Strauss 302b9e2be50SGlenn Strauss if (len > 5 303b9e2be50SGlenn Strauss && (s[len-5] ) == '-' 304b9e2be50SGlenn Strauss && (s[len-4] | 0x20) == 's' 305b9e2be50SGlenn Strauss && (s[len-3] | 0x20) == 'e' 306b9e2be50SGlenn Strauss && (s[len-2] | 0x20) == 's' 307b9e2be50SGlenn Strauss && (s[len-1] | 0x20) == 's') { 308b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_SESS; 309b9e2be50SGlenn Strauss len -= 5; 310b9e2be50SGlenn Strauss } 311b9e2be50SGlenn Strauss else { 312b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_NONE; 313b9e2be50SGlenn Strauss } 314b9e2be50SGlenn Strauss 315b9e2be50SGlenn Strauss if (3 == len 316b9e2be50SGlenn Strauss && 'm' == (s[0] | 0x20) 317b9e2be50SGlenn Strauss && 'd' == (s[1] | 0x20) 318b9e2be50SGlenn Strauss && '5' == (s[2] )) { 319b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_MD5; 320b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 321b9e2be50SGlenn Strauss return 1; 322b9e2be50SGlenn Strauss } 323b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 324b9e2be50SGlenn Strauss else if (len >= 7 325b9e2be50SGlenn Strauss && 's' == (s[0] | 0x20) 326b9e2be50SGlenn Strauss && 'h' == (s[1] | 0x20) 327b9e2be50SGlenn Strauss && 'a' == (s[2] | 0x20) 328b9e2be50SGlenn Strauss && '-' == (s[3] )) { 329b9e2be50SGlenn Strauss if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') { 330b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA256; 331b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA256_BINLEN; 332b9e2be50SGlenn Strauss return 1; 333b9e2be50SGlenn Strauss } 3341fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 335b9e2be50SGlenn Strauss if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) { 336b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256; 337b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 338b9e2be50SGlenn Strauss return 1; 339b9e2be50SGlenn Strauss } 340b9e2be50SGlenn Strauss #endif 341b9e2be50SGlenn Strauss } 342b9e2be50SGlenn Strauss #endif 343b9e2be50SGlenn Strauss return 0; /*(error)*/ 344b9e2be50SGlenn Strauss } 345b9e2be50SGlenn Strauss 346b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) { 347870b0c13SGlenn Strauss for (const char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) { 348b9e2be50SGlenn Strauss http_auth_info_t ai; 349b9e2be50SGlenn Strauss p = strchr(s, '|'); 350870b0c13SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, s, p ? (size_t)(p - s) : strlen(s))) 351870b0c13SGlenn Strauss return 0; 352b9e2be50SGlenn Strauss *algorithm |= ai.dalgo; 353b9e2be50SGlenn Strauss } 354b9e2be50SGlenn Strauss return 1; 355b9e2be50SGlenn Strauss } 356b9e2be50SGlenn Strauss 357b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh) 3587ba06c71SGlenn Strauss { 3597ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */ 3607ba06c71SGlenn Strauss 3617ba06c71SGlenn Strauss const char *str = b->ptr; 3627ba06c71SGlenn Strauss const char *p; 3637ba06c71SGlenn Strauss 3647ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) { 3657ba06c71SGlenn Strauss require->valid_user = 1; 3667ba06c71SGlenn Strauss return 1; /* success */ 3677ba06c71SGlenn Strauss } 3687ba06c71SGlenn Strauss 3697ba06c71SGlenn Strauss do { 3707ba06c71SGlenn Strauss const char *eq; 3717ba06c71SGlenn Strauss size_t len; 3727ba06c71SGlenn Strauss p = strchr(str, '|'); 3737ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str); 3747ba06c71SGlenn Strauss eq = memchr(str, '=', len); 3757ba06c71SGlenn Strauss if (NULL == eq) { 376b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 377010c2894SGlenn Strauss "error parsing auth.require 'require' field: missing '=' " 378010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 379010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 3807ba06c71SGlenn Strauss return 0; 3817ba06c71SGlenn Strauss } 382b2c18f48SGlenn Strauss if (eq[1] == '|' || eq[1] == '\0') { 383b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 384010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 385010c2894SGlenn Strauss "missing token after '=' " 386010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 387010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 3887ba06c71SGlenn Strauss return 0; 3897ba06c71SGlenn Strauss } 3907ba06c71SGlenn Strauss 3917ba06c71SGlenn Strauss switch ((int)(eq - str)) { 3927ba06c71SGlenn Strauss case 4: 3937ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) { 394810109ccSGlenn Strauss /*("user=" is 5)*/ 3958b296531SGlenn Strauss array_insert_value(&require->user, str+5, len-5); 3967ba06c71SGlenn Strauss continue; 3977ba06c71SGlenn Strauss } 3987ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) { 399810109ccSGlenn Strauss /*("host=" is 5)*/ 4008b296531SGlenn Strauss array_insert_value(&require->host, str+5, len-5); 401b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 402010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 403010c2894SGlenn Strauss "'host' not implemented; field value: %s", b->ptr); 4047ba06c71SGlenn Strauss continue; 4057ba06c71SGlenn Strauss } 4067ba06c71SGlenn Strauss break; /* to error */ 4077ba06c71SGlenn Strauss case 5: 4087ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) { 409810109ccSGlenn Strauss /*("group=" is 6)*/ 4108b296531SGlenn Strauss array_insert_value(&require->group, str+6, len-6); 4119c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/ 412b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 413010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 414010c2894SGlenn Strauss "'group' not implemented; field value: %s", b->ptr); 4159c91af0cSGlenn Strauss #endif 4167ba06c71SGlenn Strauss continue; 4177ba06c71SGlenn Strauss } 4187ba06c71SGlenn Strauss break; /* to error */ 4197ba06c71SGlenn Strauss case 10: 4207ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) { 421b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 422010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 423010c2894SGlenn Strauss "valid user can not be combined with other require rules " 424010c2894SGlenn Strauss "(expecting \"valid-user\" or " 425010c2894SGlenn Strauss "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr); 4267ba06c71SGlenn Strauss return 0; 4277ba06c71SGlenn Strauss } 4287ba06c71SGlenn Strauss break; /* to error */ 4297ba06c71SGlenn Strauss default: 4307ba06c71SGlenn Strauss break; /* to error */ 4317ba06c71SGlenn Strauss } 4327ba06c71SGlenn Strauss 433b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 434010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 435010c2894SGlenn Strauss "invalid/unsupported token " 436010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 437010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 4387ba06c71SGlenn Strauss return 0; 4397ba06c71SGlenn Strauss 4407ba06c71SGlenn Strauss } while (p && *((str = p+1))); 4417ba06c71SGlenn Strauss 4427ba06c71SGlenn Strauss return 1; /* success */ 4437ba06c71SGlenn Strauss } 4447ba06c71SGlenn Strauss 445b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh) 4468e713130SGlenn Strauss { 4478e713130SGlenn Strauss for (uint32_t n = 0; n < value->used; ++n) { 4487ba06c71SGlenn Strauss size_t m; 4498e713130SGlenn Strauss data_array *da_file = (data_array *)value->data[n]; 4507ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL; 451e1188e77SGlenn Strauss const buffer *nonce_secret = NULL; 45271175df1SGlenn Strauss data_unset *userhash = NULL; 4537ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme; 454b9e2be50SGlenn Strauss buffer *algos = NULL; 455b9e2be50SGlenn Strauss int algorithm = HTTP_AUTH_DIGEST_SESS; 4567ba06c71SGlenn Strauss 457c2238256SGlenn Strauss if (!array_is_kvstring(&da_file->value)) { 458b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 459010c2894SGlenn Strauss "unexpected value for auth.require; expected " 460bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 4617ba06c71SGlenn Strauss 4627ba06c71SGlenn Strauss return HANDLER_ERROR; 4637ba06c71SGlenn Strauss } 4647ba06c71SGlenn Strauss 465c2238256SGlenn Strauss for (m = 0; m < da_file->value.used; m++) { 466c2238256SGlenn Strauss if (da_file->value.data[m]->type == TYPE_STRING) { 467c2238256SGlenn Strauss data_string *ds = (data_string *)da_file->value.data[m]; 468ad9b7e00SGlenn Strauss if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) { 469601c572cSGlenn Strauss method = &ds->value; 470ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) { 471601c572cSGlenn Strauss realm = &ds->value; 472ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) { 473601c572cSGlenn Strauss require = &ds->value; 474ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) { 475601c572cSGlenn Strauss algos = &ds->value; 47633ae8302SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce_secret")) 47733ae8302SGlenn Strauss || buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce-secret"))) { 478e1188e77SGlenn Strauss nonce_secret = &ds->value; 47971175df1SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("userhash"))) { 48071175df1SGlenn Strauss userhash = (data_unset *)ds; 4817ba06c71SGlenn Strauss } else { 482b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 483010c2894SGlenn Strauss "the field is unknown in: " 484010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 485010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 4867ba06c71SGlenn Strauss 4877ba06c71SGlenn Strauss return HANDLER_ERROR; 4887ba06c71SGlenn Strauss } 4897ba06c71SGlenn Strauss } else { 490b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 491010c2894SGlenn Strauss "a string was expected for: " 492010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 493010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 4947ba06c71SGlenn Strauss 4957ba06c71SGlenn Strauss return HANDLER_ERROR; 4967ba06c71SGlenn Strauss } 4977ba06c71SGlenn Strauss } 4987ba06c71SGlenn Strauss 499af3df29aSGlenn Strauss if (!method || buffer_is_blank(method)) { 500b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 501010c2894SGlenn Strauss "the method field is missing or blank in: " 5027ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )"); 5037ba06c71SGlenn Strauss return HANDLER_ERROR; 5047ba06c71SGlenn Strauss } else { 5057ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method); 5067ba06c71SGlenn Strauss if (NULL == auth_scheme) { 507b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 508010c2894SGlenn Strauss "unknown method %s (e.g. \"basic\", \"digest\" or \"extern\") in " 509010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )", method->ptr); 5107ba06c71SGlenn Strauss return HANDLER_ERROR; 5117ba06c71SGlenn Strauss } 5127ba06c71SGlenn Strauss } 5137ba06c71SGlenn Strauss 514af3df29aSGlenn Strauss if (!realm) { 515b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 516010c2894SGlenn Strauss "the realm field is missing in: " 5177ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )"); 5187ba06c71SGlenn Strauss return HANDLER_ERROR; 5197ba06c71SGlenn Strauss } 5207ba06c71SGlenn Strauss 521af3df29aSGlenn Strauss if (!require || buffer_is_blank(require)) { 522b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 523010c2894SGlenn Strauss "the require field is missing or blank in: " 5247ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )"); 5257ba06c71SGlenn Strauss return HANDLER_ERROR; 5267ba06c71SGlenn Strauss } 5277ba06c71SGlenn Strauss 528af3df29aSGlenn Strauss if (!algos || buffer_is_blank(algos)) { 529b9e2be50SGlenn Strauss algorithm |= HTTP_AUTH_DIGEST_MD5; 530b9e2be50SGlenn Strauss } else if (!mod_auth_algorithms_parse(&algorithm, algos)) { 531b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 532010c2894SGlenn Strauss "invalid algorithm in: " 533b9e2be50SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )"); 534b9e2be50SGlenn Strauss return HANDLER_ERROR; 535b9e2be50SGlenn Strauss } 536b9e2be50SGlenn Strauss 537609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/ 5387ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init(); 539ad9b7e00SGlenn Strauss buffer_copy_buffer(&dauth->key, &da_file->key); 5407ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme; 541b9e2be50SGlenn Strauss dauth->require->algorithm = algorithm; 542a03afc90SGlenn Strauss dauth->require->realm = realm; 543e1188e77SGlenn Strauss dauth->require->nonce_secret = nonce_secret; /*(NULL is ok)*/ 54471175df1SGlenn Strauss dauth->require->userhash = config_plugin_value_tobool(userhash, 0); 545b5775b99SGlenn Strauss if (!mod_auth_require_parse(dauth->require, require, errh)) { 5468c7f1dfbSGlenn Strauss dauth->fn->free((data_unset *)dauth); 5477ba06c71SGlenn Strauss return HANDLER_ERROR; 5487ba06c71SGlenn Strauss } 5498e713130SGlenn Strauss array_insert_unique(auth_require, (data_unset *)dauth); 5507ba06c71SGlenn Strauss } 5517ba06c71SGlenn Strauss } 5527ba06c71SGlenn Strauss 5537ba06c71SGlenn Strauss return HANDLER_GO_ON; 5547ba06c71SGlenn Strauss } 5557ba06c71SGlenn Strauss 5568e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 5578e713130SGlenn Strauss switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 5588e713130SGlenn Strauss case 0: /* auth.backend */ 5598e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 5608e713130SGlenn Strauss pconf->auth_backend = cpv->v.v; 5618e713130SGlenn Strauss break; 5628e713130SGlenn Strauss case 1: /* auth.require */ 5638e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 5648e713130SGlenn Strauss pconf->auth_require = cpv->v.v; 5658e713130SGlenn Strauss break; 5668e713130SGlenn Strauss case 2: /* auth.extern-authn */ 5678e713130SGlenn Strauss pconf->auth_extern_authn = cpv->v.u; 568e11514b0SGlenn Strauss break; 569e11514b0SGlenn Strauss case 3: /* auth.cache */ 570e11514b0SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 571e11514b0SGlenn Strauss pconf->auth_cache = cpv->v.v; 572e11514b0SGlenn Strauss break; 5738e713130SGlenn Strauss default:/* should not happen */ 5748e713130SGlenn Strauss return; 5758e713130SGlenn Strauss } 5768e713130SGlenn Strauss } 5774a6ce4aeSJan Kneschke 5788e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 5798e713130SGlenn Strauss do { 5808e713130SGlenn Strauss mod_auth_merge_config_cpv(pconf, cpv); 5818e713130SGlenn Strauss } while ((++cpv)->k_id != -1); 5828e713130SGlenn Strauss } 583bcdc6a3bSJan Kneschke 5847c7f8c46SGlenn Strauss static void mod_auth_patch_config(request_st * const r, plugin_data * const p) { 585cc2134c8SGlenn Strauss p->conf = p->defaults; /* copy small struct instead of memcpy() */ 586cc2134c8SGlenn Strauss /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/ 5878e713130SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) { 5887c7f8c46SGlenn Strauss if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 5898e713130SGlenn Strauss mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); 5908e713130SGlenn Strauss } 5918e713130SGlenn Strauss } 59236f64b26SGlenn Strauss 5938e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 5948e713130SGlenn Strauss static const config_plugin_keys_t cpk[] = { 5958e713130SGlenn Strauss { CONST_STR_LEN("auth.backend"), 5968e713130SGlenn Strauss T_CONFIG_STRING, 5978e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 5988e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.require"), 59903b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY, 6008e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 6018e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.extern-authn"), 6028e713130SGlenn Strauss T_CONFIG_BOOL, 6038e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 604e11514b0SGlenn Strauss ,{ CONST_STR_LEN("auth.cache"), 605e11514b0SGlenn Strauss T_CONFIG_ARRAY, 606e11514b0SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 6078e713130SGlenn Strauss ,{ NULL, 0, 6088e713130SGlenn Strauss T_CONFIG_UNSET, 6098e713130SGlenn Strauss T_CONFIG_SCOPE_UNSET } 6108e713130SGlenn Strauss }; 611bcdc6a3bSJan Kneschke 6128e713130SGlenn Strauss plugin_data * const p = p_d; 6138e713130SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_auth")) 6148e713130SGlenn Strauss return HANDLER_ERROR; 615bcdc6a3bSJan Kneschke 6168e713130SGlenn Strauss /* process and validate config directives 6178e713130SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */ 6188e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 6198e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 6208e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 6218e713130SGlenn Strauss switch (cpv->k_id) { 6228e713130SGlenn Strauss case 0: /* auth.backend */ 623af3df29aSGlenn Strauss if (!buffer_is_blank(cpv->v.b)) { 6248e713130SGlenn Strauss const http_auth_backend_t * const auth_backend = 6258e713130SGlenn Strauss http_auth_backend_get(cpv->v.b); 6268e713130SGlenn Strauss if (NULL == auth_backend) { 6278e713130SGlenn Strauss log_error(srv->errh, __FILE__, __LINE__, 6288e713130SGlenn Strauss "auth.backend not supported: %s", cpv->v.b->ptr); 6298e713130SGlenn Strauss return HANDLER_ERROR; 6308e713130SGlenn Strauss } 6318e713130SGlenn Strauss *(const http_auth_backend_t **)&cpv->v.v = auth_backend; 6328e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 6338e713130SGlenn Strauss } 6348e713130SGlenn Strauss break; 6358e713130SGlenn Strauss case 1: /* auth.require */ 63603b4c993SGlenn Strauss { 63724680a91SGlenn Strauss array * const a = array_init(4); 6388e713130SGlenn Strauss if (HANDLER_GO_ON != 639b5775b99SGlenn Strauss mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) { 6408e713130SGlenn Strauss array_free(a); 6418e713130SGlenn Strauss return HANDLER_ERROR; 6428e713130SGlenn Strauss } 6438e713130SGlenn Strauss cpv->v.a = a; 6448e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 6458e713130SGlenn Strauss } 6468e713130SGlenn Strauss break; 6478e713130SGlenn Strauss case 2: /* auth.extern-authn */ 6488e713130SGlenn Strauss break; 649e11514b0SGlenn Strauss case 3: /* auth.cache */ 650e11514b0SGlenn Strauss cpv->v.v = http_auth_cache_init(cpv->v.a); 651e11514b0SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 652e11514b0SGlenn Strauss break; 6538e713130SGlenn Strauss default:/* should not happen */ 6548e713130SGlenn Strauss break; 655bcdc6a3bSJan Kneschke } 656bcdc6a3bSJan Kneschke } 657bcdc6a3bSJan Kneschke } 658bcdc6a3bSJan Kneschke 6598e713130SGlenn Strauss /* initialize p->defaults from global config context */ 6608e713130SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 6618e713130SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 6628e713130SGlenn Strauss if (-1 != cpv->k_id) 6638e713130SGlenn Strauss mod_auth_merge_config(&p->defaults, cpv); 664bcdc6a3bSJan Kneschke } 6658e713130SGlenn Strauss 6668e713130SGlenn Strauss return HANDLER_GO_ON; 6678e713130SGlenn Strauss } 668bcdc6a3bSJan Kneschke 6697c7f8c46SGlenn Strauss static handler_t mod_auth_uri_handler(request_st * const r, void *p_d) { 6707ba06c71SGlenn Strauss plugin_data *p = p_d; 671d61f3381SGlenn Strauss data_auth *dauth; 67281b2d1f0SGlenn Strauss 6737c7f8c46SGlenn Strauss mod_auth_patch_config(r, p); 67481b2d1f0SGlenn Strauss 6757ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON; 67681b2d1f0SGlenn Strauss 6777ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */ 6787ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */ 6797c7f8c46SGlenn Strauss dauth = (!r->conf.force_lowercase_filenames) 6807c7f8c46SGlenn Strauss ? (data_auth *)array_match_key_prefix(p->conf.auth_require, &r->uri.path) 6817c7f8c46SGlenn Strauss : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, &r->uri.path); 682d61f3381SGlenn Strauss if (NULL == dauth) return HANDLER_GO_ON; 6837ba06c71SGlenn Strauss 684d61f3381SGlenn Strauss { 6857ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme; 686f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) { 6877c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 6883dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) { 689f54d628cSGlenn Strauss return HANDLER_GO_ON; 690f54d628cSGlenn Strauss } 691f54d628cSGlenn Strauss } 6927c7f8c46SGlenn Strauss return scheme->checkfn(r, scheme->p_d, dauth->require, p->conf.auth_backend); 6937ba06c71SGlenn Strauss } 6947ba06c71SGlenn Strauss } 6957ba06c71SGlenn Strauss 696e11514b0SGlenn Strauss 6977ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p); 6987ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) { 6997ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID; 700e2de4e58SGlenn Strauss p->name = "auth"; 7017ba06c71SGlenn Strauss p->init = mod_auth_init; 7027ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults; 703e11514b0SGlenn Strauss p->handle_trigger = mod_auth_periodic; 7047ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler; 7057ba06c71SGlenn Strauss p->cleanup = mod_auth_free; 7067ba06c71SGlenn Strauss 70781b2d1f0SGlenn Strauss return 0; 70881b2d1f0SGlenn Strauss } 70981b2d1f0SGlenn Strauss 71081b2d1f0SGlenn Strauss 71181b2d1f0SGlenn Strauss 71281b2d1f0SGlenn Strauss 7137ba06c71SGlenn Strauss /* 7147ba06c71SGlenn Strauss * auth schemes (basic, digest, extern) 7157ba06c71SGlenn Strauss * 7167ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs) 7177ba06c71SGlenn Strauss */ 71881b2d1f0SGlenn Strauss 7191fc8a3e1SGlenn Strauss #include "sys-crypto-md.h" 7207ba06c71SGlenn Strauss #include "base64.h" 7217f4e156eSGlenn Strauss #include "rand.h" 7223dd3cde9SGlenn Strauss #include "http_header.h" 72381b2d1f0SGlenn Strauss 724010c2894SGlenn Strauss __attribute_cold__ 725ffc7a0bfSGlenn Strauss __attribute_noinline__ 72656e2881cSGlenn Strauss static handler_t 72756e2881cSGlenn Strauss mod_auth_send_400_bad_request (request_st * const r) 72856e2881cSGlenn Strauss { 7294b3a91e6SGlenn Strauss /* a field was missing or invalid */ 7307c7f8c46SGlenn Strauss r->http_status = 400; /* Bad Request */ 7317c7f8c46SGlenn Strauss r->handler_module = NULL; 7324b3a91e6SGlenn Strauss return HANDLER_FINISHED; 7334b3a91e6SGlenn Strauss } 7344b3a91e6SGlenn Strauss 73520c56cecSGlenn Strauss 73620c56cecSGlenn Strauss 737ffc7a0bfSGlenn Strauss __attribute_noinline__ 73856e2881cSGlenn Strauss static handler_t 73956e2881cSGlenn Strauss mod_auth_send_401_unauthorized_basic (request_st * const r, const buffer * const realm) 74056e2881cSGlenn Strauss { 7417c7f8c46SGlenn Strauss r->http_status = 401; 7427c7f8c46SGlenn Strauss r->handler_module = NULL; 743dc01487eSGlenn Strauss buffer_append_str3( 74426f354cbSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE, 745dc01487eSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")), 746dc01487eSGlenn Strauss CONST_STR_LEN("Basic realm=\""), 747af3df29aSGlenn Strauss BUF_PTR_LEN(realm), 748dc01487eSGlenn Strauss CONST_STR_LEN("\", charset=\"UTF-8\"")); 7494b3a91e6SGlenn Strauss return HANDLER_FINISHED; 7504b3a91e6SGlenn Strauss } 7514b3a91e6SGlenn Strauss 75281b2d1f0SGlenn Strauss 75356e2881cSGlenn Strauss __attribute_cold__ 75456e2881cSGlenn Strauss static handler_t 75556e2881cSGlenn Strauss mod_auth_basic_misconfigured (request_st * const r, const struct http_auth_backend_t * const backend) 75656e2881cSGlenn Strauss { 757cf0098eaSGlenn Strauss if (NULL == backend) 758cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 759cf0098eaSGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr); 760cf0098eaSGlenn Strauss else 761cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 762cf0098eaSGlenn Strauss "auth.require \"method\" => \"basic\" invalid " 76356e2881cSGlenn Strauss "(try \"digest\"?) for %s", r->uri.path.ptr); 76456e2881cSGlenn Strauss 7657c7f8c46SGlenn Strauss r->http_status = 500; 7667c7f8c46SGlenn Strauss r->handler_module = NULL; 7677ba06c71SGlenn Strauss return HANDLER_FINISHED; 7687ba06c71SGlenn Strauss } 7697ba06c71SGlenn Strauss 77056e2881cSGlenn Strauss 77156e2881cSGlenn Strauss static handler_t 77256e2881cSGlenn 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) 77356e2881cSGlenn Strauss { 77456e2881cSGlenn Strauss if (NULL == backend || NULL == backend->basic) 77556e2881cSGlenn Strauss return mod_auth_basic_misconfigured(r, backend); 77656e2881cSGlenn Strauss 77756e2881cSGlenn Strauss const buffer * const vb = 778ffc7a0bfSGlenn Strauss http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, 779ffc7a0bfSGlenn Strauss CONST_STR_LEN("Authorization")); 78056e2881cSGlenn Strauss if (NULL == vb || !buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Basic "))) 7817c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 7829bc61f16SGlenn Strauss #ifdef __COVERITY__ 78356e2881cSGlenn Strauss if (buffer_clen(vb) < sizeof("Basic ")-1) 7847c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 7859bc61f16SGlenn Strauss #endif 78681b2d1f0SGlenn Strauss 78756e2881cSGlenn Strauss size_t ulen = buffer_clen(vb) - (sizeof("Basic ")-1); 78856e2881cSGlenn Strauss size_t pwlen; 78956e2881cSGlenn Strauss char *pw; 79056e2881cSGlenn Strauss char user[1024]; 79181b2d1f0SGlenn Strauss 79256e2881cSGlenn Strauss /* base64-decode Authorization into username:password string; 79356e2881cSGlenn Strauss * limit base64-decoded username:password string to fit into 1k buf */ 79456e2881cSGlenn Strauss if (ulen > 1363) /*(1363/4*3+3 = 1023)*/ 79556e2881cSGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 7968047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 79756e2881cSGlenn Strauss ulen = li_base64_dec((unsigned char *)user, sizeof(user), 79856e2881cSGlenn Strauss vb->ptr+sizeof("Basic ")-1, ulen, BASE64_STANDARD); 79956e2881cSGlenn Strauss if (0 == ulen) { 80056e2881cSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 80156e2881cSGlenn Strauss "decoding base64-string failed %s", vb->ptr+sizeof("Basic ")-1); 8027c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 80381b2d1f0SGlenn Strauss } 80456e2881cSGlenn Strauss user[ulen] = '\0'; 80556e2881cSGlenn Strauss pw = memchr(user, ':', ulen); 80656e2881cSGlenn Strauss if (NULL == pw) { 80756e2881cSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "missing ':' in %s", user); 8087c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 80981b2d1f0SGlenn Strauss } 81056e2881cSGlenn Strauss *pw++ = '\0'; 81156e2881cSGlenn Strauss pwlen = (size_t)(user + ulen - pw); 81256e2881cSGlenn Strauss ulen = (size_t)(pw - 1 - user); 81381b2d1f0SGlenn Strauss 814e11514b0SGlenn Strauss plugin_data * const p = p_d; 815e11514b0SGlenn Strauss splay_tree ** sptree = p->conf.auth_cache 816e11514b0SGlenn Strauss ? &p->conf.auth_cache->sptree 817e11514b0SGlenn Strauss : NULL; 818e11514b0SGlenn Strauss http_auth_cache_entry *ae = NULL; 819fd0cc468SGlenn Strauss handler_t rc = HANDLER_ERROR; 820e11514b0SGlenn Strauss int ndx = -1; 821e11514b0SGlenn Strauss if (sptree) { 82256e2881cSGlenn Strauss ndx = http_auth_cache_hash(require, user, ulen); 823e11514b0SGlenn Strauss ae = http_auth_cache_query(sptree, ndx); 824e11514b0SGlenn Strauss if (ae && ae->require == require 82556e2881cSGlenn Strauss && ulen == ae->ulen && 0 == memcmp(user, ae->username, ulen)) 82662ccda85SGlenn Strauss rc = ck_memeq_const_time(ae->pwdigest, ae->dlen, pw, pwlen) 827e11514b0SGlenn Strauss ? HANDLER_GO_ON 828e11514b0SGlenn Strauss : HANDLER_ERROR; 829e11514b0SGlenn Strauss else /*(not found or hash collision)*/ 830e11514b0SGlenn Strauss ae = NULL; 831e11514b0SGlenn Strauss } 832e11514b0SGlenn Strauss 83356e2881cSGlenn Strauss if (NULL == ae) { 83456e2881cSGlenn Strauss const buffer userb = { user, ulen+1, 0 }; 83556e2881cSGlenn Strauss rc = backend->basic(r, backend->p_d, require, &userb, pw); 83656e2881cSGlenn Strauss } 837e11514b0SGlenn Strauss 8387ba06c71SGlenn Strauss switch (rc) { 8394b3a91e6SGlenn Strauss case HANDLER_GO_ON: 84056e2881cSGlenn Strauss http_auth_setenv(r, user, ulen, CONST_STR_LEN("Basic")); 841e11514b0SGlenn Strauss if (sptree && NULL == ae) { /*(cache (new) successful result)*/ 84271175df1SGlenn Strauss ae = http_auth_cache_entry_init(require, 0, user, ulen, user, ulen, 84371175df1SGlenn Strauss pw, pwlen); 844e11514b0SGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free); 845e11514b0SGlenn Strauss } 8464b3a91e6SGlenn Strauss break; 8474b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 8484b3a91e6SGlenn Strauss case HANDLER_FINISHED: 8497ba06c71SGlenn Strauss break; 8504b3a91e6SGlenn Strauss case HANDLER_ERROR: 8514b3a91e6SGlenn Strauss default: 8527c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 853010c2894SGlenn Strauss "password doesn't match for %s username: %s IP: %s", 854f1e8a82fSGlenn Strauss r->uri.path.ptr, user, r->con->dst_addr_buf.ptr); 8554a600dabSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/ 856fd0cc468SGlenn Strauss rc = mod_auth_send_401_unauthorized_basic(r, require->realm); 8577ba06c71SGlenn Strauss break; 85881b2d1f0SGlenn Strauss } 85981b2d1f0SGlenn Strauss 86008c03cd4SGlenn Strauss ck_memzero(pw, pwlen); 861fd0cc468SGlenn Strauss return rc; 86281b2d1f0SGlenn Strauss } 86381b2d1f0SGlenn Strauss 86481b2d1f0SGlenn Strauss 86520c56cecSGlenn Strauss 86620c56cecSGlenn Strauss enum http_auth_digest_params_e { 86720c56cecSGlenn Strauss e_username = 0 86820c56cecSGlenn Strauss ,e_realm 86920c56cecSGlenn Strauss ,e_nonce 87020c56cecSGlenn Strauss ,e_uri 87120c56cecSGlenn Strauss ,e_algorithm 87220c56cecSGlenn Strauss ,e_qop 87320c56cecSGlenn Strauss ,e_cnonce 87420c56cecSGlenn Strauss ,e_nc 87520c56cecSGlenn Strauss ,e_response 87671175df1SGlenn Strauss ,e_userstar 87771175df1SGlenn Strauss ,e_userhash 87820c56cecSGlenn Strauss ,http_auth_digest_params_sz /*(last item)*/ 87920c56cecSGlenn Strauss }; 88020c56cecSGlenn Strauss 88120c56cecSGlenn Strauss typedef struct http_auth_digest_params_t { 88220c56cecSGlenn Strauss const char *ptr[http_auth_digest_params_sz]; 88320c56cecSGlenn Strauss uint16_t len[http_auth_digest_params_sz]; 884309c1693SGlenn Strauss unix_time64_t send_nextnonce_ts; 88571175df1SGlenn Strauss unsigned char rdigest[MD_DIGEST_LENGTH_MAX];/*(earlier members get 0-init)*/ 88620c56cecSGlenn Strauss } http_auth_digest_params_t; 88720c56cecSGlenn Strauss 88820c56cecSGlenn Strauss 88920c56cecSGlenn Strauss static void 8909fe8fbaaSGlenn Strauss mod_auth_digest_mutate (http_auth_info_t * const ai, const http_auth_digest_params_t * const dp, const buffer * const method) 89120c56cecSGlenn Strauss { 89220c56cecSGlenn Strauss force_assert(method); 893c987bec1SGlenn Strauss li_md_iov_fn digest_iov = MD5_iov; 894c987bec1SGlenn Strauss /* (ai->dalgo & HTTP_AUTH_DIGEST_MD5) default */ 895b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 896c987bec1SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256) 897c987bec1SGlenn Strauss digest_iov = SHA256_iov; 8981fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 899b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256) 900c987bec1SGlenn Strauss digest_iov = SHA512_256_iov; 901b9e2be50SGlenn Strauss #endif 902b9e2be50SGlenn Strauss #endif 903c987bec1SGlenn Strauss size_t n; 904c987bec1SGlenn Strauss struct const_iovec iov[11]; 905c987bec1SGlenn Strauss char a1[MD_DIGEST_LENGTH_MAX*2+8]; /*(+1 for li_tohex(); +8 for align)*/ 906c987bec1SGlenn Strauss char a2[MD_DIGEST_LENGTH_MAX*2+8]; /*(+1 for li_tohex(); +8 for align)*/ 907c987bec1SGlenn Strauss 908c987bec1SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 909c987bec1SGlenn Strauss 910c987bec1SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 911c987bec1SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617 912c987bec1SGlenn Strauss * Errata ID: 1649 */ 913c987bec1SGlenn Strauss iov[0].iov_base = a1; 914c987bec1SGlenn Strauss iov[0].iov_len = ai->dlen*2; 915c987bec1SGlenn Strauss iov[1].iov_base = ":"; 916c987bec1SGlenn Strauss iov[1].iov_len = 1; 91720c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_nonce]; 91820c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_nonce]; 919c987bec1SGlenn Strauss iov[3].iov_base = ":"; 920c987bec1SGlenn Strauss iov[3].iov_len = 1; 92120c56cecSGlenn Strauss iov[4].iov_base = dp->ptr[e_cnonce]; 92220c56cecSGlenn Strauss iov[4].iov_len = dp->len[e_cnonce]; 923c987bec1SGlenn Strauss digest_iov(ai->digest, iov, 5); 924c987bec1SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 925c987bec1SGlenn Strauss } 926c987bec1SGlenn Strauss 927c987bec1SGlenn Strauss /* calculate H(A2) */ 9289fe8fbaaSGlenn Strauss iov[0].iov_base = method->ptr; 9299fe8fbaaSGlenn Strauss iov[0].iov_len = buffer_clen(method); 930c987bec1SGlenn Strauss iov[1].iov_base = ":"; 931c987bec1SGlenn Strauss iov[1].iov_len = 1; 93220c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_uri]; 93320c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_uri]; 934c987bec1SGlenn Strauss n = 3; 935c987bec1SGlenn Strauss #if 0 936c987bec1SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 93720c56cecSGlenn Strauss if (dp->ptr[e_qop] && buffer_eq_icase_ss(dp->ptr[e_qop], dp->len[e_qop], 93820c56cecSGlenn Strauss CONST_STR_LEN("auth-int"))) { 939c987bec1SGlenn Strauss iov[3].iov_base = ":"; 940c987bec1SGlenn Strauss iov[3].iov_len = 1; 941c987bec1SGlenn Strauss iov[4].iov_base = [body checksum]; 942c987bec1SGlenn Strauss iov[4].iov_len = ai->dlen*2; 943c987bec1SGlenn Strauss n = 5; 944c987bec1SGlenn Strauss } 945c987bec1SGlenn Strauss #endif 946c987bec1SGlenn Strauss digest_iov(ai->digest, iov, n); 947c987bec1SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 948c987bec1SGlenn Strauss 949c987bec1SGlenn Strauss /* calculate response */ 950c987bec1SGlenn Strauss iov[0].iov_base = a1; 951c987bec1SGlenn Strauss iov[0].iov_len = ai->dlen*2; 952c987bec1SGlenn Strauss iov[1].iov_base = ":"; 953c987bec1SGlenn Strauss iov[1].iov_len = 1; 95420c56cecSGlenn Strauss iov[2].iov_base = dp->ptr[e_nonce]; 95520c56cecSGlenn Strauss iov[2].iov_len = dp->len[e_nonce]; 956c987bec1SGlenn Strauss iov[3].iov_base = ":"; 957c987bec1SGlenn Strauss iov[3].iov_len = 1; 958c987bec1SGlenn Strauss n = 4; 95920c56cecSGlenn Strauss if (dp->len[e_qop]) { 96020c56cecSGlenn Strauss iov[4].iov_base = dp->ptr[e_nc]; 96120c56cecSGlenn Strauss iov[4].iov_len = dp->len[e_nc]; 962c987bec1SGlenn Strauss iov[5].iov_base = ":"; 963c987bec1SGlenn Strauss iov[5].iov_len = 1; 96420c56cecSGlenn Strauss iov[6].iov_base = dp->ptr[e_cnonce]; 96520c56cecSGlenn Strauss iov[6].iov_len = dp->len[e_cnonce]; 966c987bec1SGlenn Strauss iov[7].iov_base = ":"; 967c987bec1SGlenn Strauss iov[7].iov_len = 1; 96820c56cecSGlenn Strauss iov[8].iov_base = dp->ptr[e_qop]; 96920c56cecSGlenn Strauss iov[8].iov_len = dp->len[e_qop]; 970c987bec1SGlenn Strauss iov[9].iov_base = ":"; 971c987bec1SGlenn Strauss iov[9].iov_len = 1; 972c987bec1SGlenn Strauss n = 10; 973c987bec1SGlenn Strauss } 974c987bec1SGlenn Strauss iov[n].iov_base = a2; 975c987bec1SGlenn Strauss iov[n].iov_len = ai->dlen*2; 976c987bec1SGlenn Strauss digest_iov(ai->digest, iov, n+1); 977b9e2be50SGlenn Strauss } 978b9e2be50SGlenn Strauss 97920c56cecSGlenn Strauss 98020c56cecSGlenn Strauss static void 981309c1693SGlenn Strauss mod_auth_append_nonce (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int dalgo, int *rndptr) 98220c56cecSGlenn Strauss { 983e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 984*f2610d23SGlenn Strauss buffer_append_char(b, ':'); 985e1188e77SGlenn Strauss const buffer * const nonce_secret = require->nonce_secret; 986e1188e77SGlenn Strauss int rnd; 987e1188e77SGlenn Strauss if (NULL == nonce_secret) 988e1188e77SGlenn Strauss rnd = rndptr ? *rndptr : li_rand_pseudo(); 989e1188e77SGlenn Strauss else { /*(do not directly expose random number generator single value)*/ 990e1188e77SGlenn Strauss rndptr 991e1188e77SGlenn Strauss ? (void)(rnd = *rndptr) 992e1188e77SGlenn Strauss : li_rand_pseudo_bytes((unsigned char *)&rnd, sizeof(rnd)); 993e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)rnd); 994*f2610d23SGlenn Strauss buffer_append_char(b, ':'); 995e1188e77SGlenn Strauss } 996c987bec1SGlenn Strauss 997c987bec1SGlenn Strauss size_t n; 998c987bec1SGlenn Strauss struct const_iovec iov[3]; 999c987bec1SGlenn Strauss 1000c987bec1SGlenn Strauss #if 0 1001c987bec1SGlenn Strauss char a1[LI_ITOSTRING_LENGTH]; 1002c987bec1SGlenn Strauss char a2[LI_ITOSTRING_LENGTH]; 1003c987bec1SGlenn Strauss iov[0].iov_base = a1; 1004c987bec1SGlenn Strauss iov[0].iov_len = li_itostrn(a1, sizeof(a1), cur_ts); 1005c987bec1SGlenn Strauss iov[1].iov_base = a2; 1006c987bec1SGlenn Strauss iov[1].iov_len = li_itostrn(a2, sizeof(a2), rnd); 1007c987bec1SGlenn Strauss #else 1008c987bec1SGlenn Strauss iov[0].iov_base = &cur_ts; 1009c987bec1SGlenn Strauss iov[0].iov_len = sizeof(cur_ts); 1010c987bec1SGlenn Strauss iov[1].iov_base = &rnd; 1011c987bec1SGlenn Strauss iov[1].iov_len = sizeof(rnd); 1012c987bec1SGlenn Strauss #endif 1013c987bec1SGlenn Strauss n = 2; 1014c987bec1SGlenn Strauss if (nonce_secret) { 1015c987bec1SGlenn Strauss iov[2].iov_base = nonce_secret->ptr; 1016c987bec1SGlenn Strauss iov[2].iov_len = buffer_clen(nonce_secret); 1017c987bec1SGlenn Strauss n = 3; 1018c987bec1SGlenn Strauss } 1019c987bec1SGlenn Strauss 1020c987bec1SGlenn Strauss unsigned char h[MD_DIGEST_LENGTH_MAX]; 1021e1188e77SGlenn Strauss switch (dalgo) { 1022b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 10231fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 1024e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256: 1025c987bec1SGlenn Strauss SHA512_256_iov(h, iov, n); 1026c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 1027e1188e77SGlenn Strauss break; 1028e1188e77SGlenn Strauss #endif 1029e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256: 1030c987bec1SGlenn Strauss SHA256_iov(h, iov, n); 1031c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_SHA256_BINLEN; 1032e1188e77SGlenn Strauss break; 1033e1188e77SGlenn Strauss #endif 1034e1188e77SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/ 1035e1188e77SGlenn Strauss default: 1036c987bec1SGlenn Strauss MD5_iov(h, iov, n); 1037c987bec1SGlenn Strauss n = HTTP_AUTH_DIGEST_MD5_BINLEN; 1038e1188e77SGlenn Strauss break; 1039e1188e77SGlenn Strauss } 1040c987bec1SGlenn Strauss li_tohex(buffer_extend(b, n*2), n*2+1, (const char *)h, n); 1041e1188e77SGlenn Strauss } 1042e1188e77SGlenn Strauss 104320c56cecSGlenn Strauss 104420c56cecSGlenn Strauss static void 1045309c1693SGlenn Strauss mod_auth_digest_www_authenticate (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) 104620c56cecSGlenn Strauss { 1047b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm; 1048b9e2be50SGlenn Strauss int n = 0; 1049e1188e77SGlenn Strauss int algoid[3]; 1050b9e2be50SGlenn Strauss unsigned int algolen[3]; 1051b9e2be50SGlenn Strauss const char *algoname[3]; 1052b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 10531fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 1054b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) { 1055e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA512_256; 1056b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256"; 1057b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1; 1058b9e2be50SGlenn Strauss ++n; 1059b9e2be50SGlenn Strauss } 1060b9e2be50SGlenn Strauss #endif 1061b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) { 1062e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA256; 1063b9e2be50SGlenn Strauss algoname[n] = "SHA-256"; 1064b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1; 1065b9e2be50SGlenn Strauss ++n; 1066b9e2be50SGlenn Strauss } 1067b9e2be50SGlenn Strauss #endif 1068b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) { 1069e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_MD5; 1070b9e2be50SGlenn Strauss algoname[n] = "MD5"; 1071b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1; 1072b9e2be50SGlenn Strauss ++n; 1073b9e2be50SGlenn Strauss } 1074b9e2be50SGlenn Strauss 1075b9e2be50SGlenn Strauss buffer_clear(b); 1076b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) { 1077dc01487eSGlenn Strauss struct const_iovec iov[] = { 1078dc01487eSGlenn Strauss { CONST_STR_LEN("\r\nWWW-Authenticate: ") } 1079dc01487eSGlenn Strauss ,{ CONST_STR_LEN("Digest realm=\"") } 1080af3df29aSGlenn Strauss ,{ BUF_PTR_LEN(require->realm) } 1081dc01487eSGlenn Strauss ,{ CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=") } 1082dc01487eSGlenn Strauss ,{ algoname[i], algolen[i] } 1083dc01487eSGlenn Strauss ,{ CONST_STR_LEN(", nonce=\"") } 1084dc01487eSGlenn Strauss }; 1085dc01487eSGlenn Strauss buffer_append_iovec(b, iov+(0==i), sizeof(iov)/sizeof(*iov)-(0==i)); 1086e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, algoid[i], NULL); 1087b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\"")); 108871175df1SGlenn Strauss if (require->userhash) { 108971175df1SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", userhash=true")); 109071175df1SGlenn Strauss } 1091b9e2be50SGlenn Strauss if (nonce_stale) { 1092b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true")); 1093b9e2be50SGlenn Strauss } 1094b9e2be50SGlenn Strauss } 109581b2d1f0SGlenn Strauss } 109681b2d1f0SGlenn Strauss 1097c841ce3bSGlenn Strauss 109820c56cecSGlenn Strauss __attribute_noinline__ 109920c56cecSGlenn Strauss static handler_t 110020c56cecSGlenn Strauss mod_auth_send_401_unauthorized_digest(request_st * const r, const struct http_auth_require_t * const require, int nonce_stale) 110120c56cecSGlenn Strauss { 110220c56cecSGlenn Strauss r->http_status = 401; 110320c56cecSGlenn Strauss r->handler_module = NULL; 110420c56cecSGlenn Strauss mod_auth_digest_www_authenticate( 110520c56cecSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE, 110620c56cecSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")), 110720c56cecSGlenn Strauss log_epoch_secs, require, nonce_stale); 110820c56cecSGlenn Strauss return HANDLER_FINISHED; 110920c56cecSGlenn Strauss } 111020c56cecSGlenn Strauss 111120c56cecSGlenn Strauss 111220c56cecSGlenn Strauss static void 1113309c1693SGlenn Strauss mod_auth_digest_authentication_info (buffer *b, unix_time64_t cur_ts, const struct http_auth_require_t *require, int dalgo) 111420c56cecSGlenn Strauss { 11156ad325c6SGlenn Strauss buffer_clear(b); 11166ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\"")); 1117e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, dalgo, NULL); 1118*f2610d23SGlenn Strauss buffer_append_char(b, '"'); 11196ad325c6SGlenn Strauss } 11206ad325c6SGlenn Strauss 112120c56cecSGlenn Strauss 1122c841ce3bSGlenn Strauss static handler_t 1123c841ce3bSGlenn 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) 1124c841ce3bSGlenn Strauss { 1125c841ce3bSGlenn Strauss plugin_data * const p = p_d; 1126c841ce3bSGlenn Strauss splay_tree **sptree = p->conf.auth_cache 1127c841ce3bSGlenn Strauss ? &p->conf.auth_cache->sptree 1128c841ce3bSGlenn Strauss : NULL; 1129c841ce3bSGlenn Strauss http_auth_cache_entry *ae = NULL; 1130c841ce3bSGlenn Strauss handler_t rc = HANDLER_GO_ON; 1131c841ce3bSGlenn Strauss int ndx = -1; 113271175df1SGlenn Strauss 113371175df1SGlenn Strauss const char *user = ai->username; 113471175df1SGlenn Strauss const uint32_t ulen = ai->ulen; 113571175df1SGlenn Strauss char userbuf[sizeof(ai->userbuf)]; 113671175df1SGlenn Strauss if (ai->userhash && ulen <= sizeof(userbuf)) { 113771175df1SGlenn Strauss /*(lowercase hex in userhash for consistency)*/ 113871175df1SGlenn Strauss const char * const restrict s = ai->username; 113971175df1SGlenn Strauss for (uint_fast32_t i = 0; i < ulen; ++i) 114071175df1SGlenn Strauss userbuf[i] = !light_isupper(s[i]) ? s[i] : (s[i] | 0x20); 114171175df1SGlenn Strauss user = userbuf; 114271175df1SGlenn Strauss } 114371175df1SGlenn Strauss 1144c841ce3bSGlenn Strauss if (sptree) { 114571175df1SGlenn Strauss ndx = http_auth_cache_hash(require, user, ulen); 1146c841ce3bSGlenn Strauss ae = http_auth_cache_query(sptree, ndx); 1147c841ce3bSGlenn Strauss if (ae && ae->require == require 1148c841ce3bSGlenn Strauss && ae->dalgo == ai->dalgo 1149c841ce3bSGlenn Strauss && ae->dlen == ai->dlen 115071175df1SGlenn Strauss && ae->klen == ulen 115171175df1SGlenn Strauss && 0 == memcmp(ae->k, user, ulen) 115271175df1SGlenn Strauss && (ae->k == ae->username || ai->userhash)) { 1153c841ce3bSGlenn Strauss memcpy(ai->digest, ae->pwdigest, ai->dlen); 115471175df1SGlenn Strauss if (ae->k != ae->username) { /*(userhash was key; copy username)*/ 115571175df1SGlenn Strauss if (__builtin_expect( (ae->ulen <= sizeof(ai->userbuf)), 1)) { 115671175df1SGlenn Strauss ai->ulen = ae->ulen; 115771175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, ae->username, ae->ulen); 115871175df1SGlenn Strauss } 115971175df1SGlenn Strauss } 1160c841ce3bSGlenn Strauss } 1161c841ce3bSGlenn Strauss else /*(not found or hash collision)*/ 1162c841ce3bSGlenn Strauss ae = NULL; 1163c841ce3bSGlenn Strauss } 1164c841ce3bSGlenn Strauss 116571175df1SGlenn Strauss if (NULL == ae) { 116671175df1SGlenn Strauss if (ai->userhash && ulen <= sizeof(ai->userbuf)) 116771175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, userbuf, ulen); 116871175df1SGlenn Strauss /* ai->username (lowercase userhash) will be replaced by username */ 1169c841ce3bSGlenn Strauss rc = backend->digest(r, backend->p_d, ai); 117071175df1SGlenn Strauss } 1171c841ce3bSGlenn Strauss 1172c841ce3bSGlenn Strauss switch (rc) { 1173c841ce3bSGlenn Strauss case HANDLER_GO_ON: 1174c841ce3bSGlenn Strauss break; 1175c841ce3bSGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 1176c841ce3bSGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 1177c841ce3bSGlenn Strauss case HANDLER_FINISHED: 1178c841ce3bSGlenn Strauss return HANDLER_FINISHED; 1179c841ce3bSGlenn Strauss case HANDLER_ERROR: 1180c841ce3bSGlenn Strauss default: 1181c841ce3bSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if unknown user)*/ 1182c841ce3bSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1183c841ce3bSGlenn Strauss } 1184c841ce3bSGlenn Strauss 1185c841ce3bSGlenn Strauss if (sptree && NULL == ae) { /*(cache digest from backend)*/ 118671175df1SGlenn Strauss ae = http_auth_cache_entry_init(require, ai->dalgo, user, ulen, 118771175df1SGlenn Strauss ai->username, ai->ulen, 118871175df1SGlenn Strauss (char *)ai->digest, ai->dlen); 1189c841ce3bSGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free); 1190c841ce3bSGlenn Strauss } 1191c841ce3bSGlenn Strauss 1192c841ce3bSGlenn Strauss return rc; 1193c841ce3bSGlenn Strauss } 1194c841ce3bSGlenn Strauss 119581b2d1f0SGlenn Strauss 119620c56cecSGlenn Strauss __attribute_cold__ 119720c56cecSGlenn Strauss static handler_t 119820c56cecSGlenn Strauss mod_auth_digest_misconfigured (request_st * const r, const struct http_auth_backend_t * const backend) 119920c56cecSGlenn Strauss { 1200cf0098eaSGlenn Strauss if (NULL == backend) 12017c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 12027c7f8c46SGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr); 1203cf0098eaSGlenn Strauss else 1204cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1205cf0098eaSGlenn Strauss "auth.require \"method\" => \"digest\" invalid " 120620c56cecSGlenn Strauss "(try \"basic\"?) for %s", r->uri.path.ptr); 120720c56cecSGlenn Strauss 12087c7f8c46SGlenn Strauss r->http_status = 500; 12097c7f8c46SGlenn Strauss r->handler_module = NULL; 12107ba06c71SGlenn Strauss return HANDLER_FINISHED; 12117ba06c71SGlenn Strauss } 12127ba06c71SGlenn Strauss 12137ba06c71SGlenn Strauss 121420c56cecSGlenn Strauss static void 12156881f79cSGlenn Strauss mod_auth_digest_parse_authorization (http_auth_digest_params_t * const dp, const char *c) 121620c56cecSGlenn Strauss { 121720c56cecSGlenn Strauss struct digest_kv { 121820c56cecSGlenn Strauss const char *key; 121920c56cecSGlenn Strauss uint32_t klen; 122020c56cecSGlenn Strauss enum http_auth_digest_params_e id; 122120c56cecSGlenn Strauss }; 122220c56cecSGlenn Strauss 122320c56cecSGlenn Strauss static const struct digest_kv dkv[] = { 12246881f79cSGlenn Strauss { CONST_STR_LEN("username"), e_username }, 12256881f79cSGlenn Strauss { CONST_STR_LEN("realm"), e_realm }, 12266881f79cSGlenn Strauss { CONST_STR_LEN("nonce"), e_nonce }, 12276881f79cSGlenn Strauss { CONST_STR_LEN("uri"), e_uri }, 12286881f79cSGlenn Strauss { CONST_STR_LEN("algorithm"), e_algorithm }, 12296881f79cSGlenn Strauss { CONST_STR_LEN("qop"), e_qop }, 12306881f79cSGlenn Strauss { CONST_STR_LEN("cnonce"), e_cnonce }, 12316881f79cSGlenn Strauss { CONST_STR_LEN("nc"), e_nc }, 12326881f79cSGlenn Strauss { CONST_STR_LEN("response"), e_response }, 123371175df1SGlenn Strauss { CONST_STR_LEN("username*"), e_userstar }, 123471175df1SGlenn Strauss { CONST_STR_LEN("userhash"), e_userhash }, 123520c56cecSGlenn Strauss 123620c56cecSGlenn Strauss { NULL, 0, http_auth_digest_params_sz } 123720c56cecSGlenn Strauss }; 123881b2d1f0SGlenn Strauss 123981b2d1f0SGlenn Strauss /* parse credentials from client */ 12406881f79cSGlenn Strauss /* (caller must pass c pointing to string after "Digest ") */ 12416881f79cSGlenn Strauss for (const char *e; *c; c++) { 124281b2d1f0SGlenn Strauss /* skip whitespaces */ 12436881f79cSGlenn Strauss while (*c == ' ' || *c == '\t' || *c == ',') ++c; 124481b2d1f0SGlenn Strauss if (!*c) break; 12456881f79cSGlenn Strauss for (e = c; *e!='=' && *e!=' ' && *e!='\t' && *e!='\0'; ++e) ; 12466881f79cSGlenn Strauss const uint32_t tlen = (uint32_t)(e - c); 124781b2d1f0SGlenn Strauss 124820c56cecSGlenn Strauss for (int i = 0; dkv[i].key; ++i) { 12496881f79cSGlenn Strauss if (tlen != dkv[i].klen || 0 != memcmp(c, dkv[i].key, tlen)) 125020c56cecSGlenn Strauss continue; 12516881f79cSGlenn Strauss c += tlen; 12526881f79cSGlenn Strauss /* detect and step over '='; ignore BWS (bad whitespace) */ 12536881f79cSGlenn Strauss if (__builtin_expect( (*c != '='), 0)) { 12546881f79cSGlenn Strauss while (*c == ' ' || *c == '\t') ++c; 12556881f79cSGlenn Strauss if (*c != '=') return; /*(including '\0')*/ 125620c56cecSGlenn Strauss } 12576881f79cSGlenn Strauss do { ++c; } while (*c == ' ' || *c == '\t'); 12586881f79cSGlenn Strauss 12596881f79cSGlenn Strauss if (*c == '"') { 12606881f79cSGlenn Strauss for (e = ++c; *e != '"' && *e != '\0'; ++e) { 12616881f79cSGlenn Strauss if (*e == '\\' && *++e == '\0') return; 12626881f79cSGlenn Strauss } 12636881f79cSGlenn Strauss if (*e != '"') return; 12646881f79cSGlenn Strauss /* value with "..." *//*(XXX: quoted value not unescaped)*/ 126520c56cecSGlenn Strauss } 126620c56cecSGlenn Strauss else { 12676881f79cSGlenn Strauss for (e = c; *e!=',' && *e!=' ' && *e!='\t' && *e!='\0'; ++e) ; 12686881f79cSGlenn Strauss /* value without "..." */ 12696881f79cSGlenn Strauss } 127020c56cecSGlenn Strauss dp->ptr[dkv[i].id] = c; 12716881f79cSGlenn Strauss dp->len[dkv[i].id] = (uint16_t)(e - c); 12726881f79cSGlenn Strauss c = e; 12736881f79cSGlenn Strauss if (*c != ',') { 12746881f79cSGlenn Strauss /*(could more strictly check for linear whitespace)*/ 12756881f79cSGlenn Strauss c = strchr(c, ','); 12766881f79cSGlenn Strauss if (!c) return; 127781b2d1f0SGlenn Strauss } 127881b2d1f0SGlenn Strauss break; 127981b2d1f0SGlenn Strauss } 128081b2d1f0SGlenn Strauss } 128181b2d1f0SGlenn Strauss } 128281b2d1f0SGlenn Strauss 128381b2d1f0SGlenn Strauss 128420c56cecSGlenn Strauss static handler_t 128571175df1SGlenn Strauss mod_auth_digest_validate_userstar (request_st * const r, http_auth_digest_params_t * const dp, http_auth_info_t * const ai) 128671175df1SGlenn Strauss { 128771175df1SGlenn Strauss /*assert(dp->ptr[e_userstar]);*/ 128871175df1SGlenn Strauss 128971175df1SGlenn Strauss if (dp->len[e_userhash] == 4) { /*("true")*/ 129071175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 129171175df1SGlenn Strauss "digest: invalid \"username*\" with \"userhash\" = true"); 129271175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 129371175df1SGlenn Strauss } 129471175df1SGlenn Strauss 129571175df1SGlenn Strauss /* "username*" RFC5987 ext-value 129671175df1SGlenn Strauss * ext-value = charset "'" [ language ] "'" value-chars */ 129771175df1SGlenn Strauss const char *ptr = dp->ptr[e_userstar]; 129871175df1SGlenn Strauss uint32_t len = dp->len[e_userstar]; 129971175df1SGlenn Strauss /* validate and step over charset... */ 130071175df1SGlenn Strauss if ((*ptr | 0x20) == 'u' && len > 5 130171175df1SGlenn Strauss && buffer_eq_icase_ssn(ptr, "utf-8", 5)) 130271175df1SGlenn Strauss ptr += 5; 130371175df1SGlenn Strauss else if ((*ptr | 0x20) == 'i' && len > 10 130471175df1SGlenn Strauss && buffer_eq_icase_ssn(ptr, "iso-8859-1", 10)) 130571175df1SGlenn Strauss ptr += 10; 130671175df1SGlenn Strauss else 130760c9612aSGlenn Strauss ptr = "\n"; /*(invalid char; (not '\''); error below)*/ 130871175df1SGlenn Strauss /* step over ...'language'... */ 130971175df1SGlenn Strauss if (*ptr++ != '\'' 131071175df1SGlenn Strauss || !(ptr = memchr(ptr, '\'', 131171175df1SGlenn Strauss len - (uint32_t)(ptr - dp->ptr[e_userstar])))) { 131271175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 131371175df1SGlenn Strauss "digest: invalid \"username*\" ext-value"); 131471175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 131571175df1SGlenn Strauss } 131671175df1SGlenn Strauss ++ptr; 131771175df1SGlenn Strauss 131871175df1SGlenn Strauss /* decode %XX encodings (could be more efficient by combining tests) */ 131971175df1SGlenn Strauss buffer * const tb = r->tmp_buf; 132071175df1SGlenn Strauss buffer_copy_string_len(tb, ptr, len-(uint32_t)(ptr - dp->ptr[e_userstar])); 132171175df1SGlenn Strauss buffer_urldecode_path(tb); 132271175df1SGlenn Strauss if (dp->ptr[e_userstar][0] == 'u' && !buffer_is_valid_UTF8(tb)) { 132371175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 132471175df1SGlenn Strauss "digest: invalid \"username*\" invalid UTF-8"); 132571175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 132671175df1SGlenn Strauss } 132771175df1SGlenn Strauss len = buffer_clen(tb); 132871175df1SGlenn Strauss if (len > sizeof(ai->userbuf)) { 132971175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 133071175df1SGlenn Strauss "digest: invalid \"username*\" too long"); 133171175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 133271175df1SGlenn Strauss } 133371175df1SGlenn Strauss for (ptr = tb->ptr; *ptr; ++ptr) { 133471175df1SGlenn Strauss /* prohibit decoded control chars, including '\0','\r','\n' */ 133571175df1SGlenn Strauss /* (theoretically could permit '\t', but not currently done) */ 133671175df1SGlenn Strauss if (*(unsigned char *)ptr < 0x20 || *ptr == 127) { /* iscntrl() */ 133771175df1SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 133871175df1SGlenn Strauss "digest: invalid \"username*\" contains ctrl chars"); 133971175df1SGlenn Strauss return mod_auth_send_400_bad_request(r); 134071175df1SGlenn Strauss } 134171175df1SGlenn Strauss } 134271175df1SGlenn Strauss 134371175df1SGlenn Strauss ai->ulen = len; 134471175df1SGlenn Strauss ai->username = memcpy(ai->userbuf, tb->ptr, len); 134571175df1SGlenn Strauss return HANDLER_GO_ON; 134671175df1SGlenn Strauss } 134771175df1SGlenn Strauss 134871175df1SGlenn Strauss 134971175df1SGlenn Strauss static handler_t 135020c56cecSGlenn 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) 135120c56cecSGlenn Strauss { 135220c56cecSGlenn Strauss /* check for required parameters */ 135320c56cecSGlenn Strauss if ((!dp->ptr[e_qop] || (dp->ptr[e_nc] && dp->ptr[e_cnonce])) 135471175df1SGlenn Strauss && ((NULL != dp->ptr[e_username]) ^ (NULL != dp->ptr[e_userstar])) 135520c56cecSGlenn Strauss && dp->ptr[e_realm] 135620c56cecSGlenn Strauss && dp->ptr[e_nonce] 135720c56cecSGlenn Strauss && dp->ptr[e_uri] 135820c56cecSGlenn Strauss && dp->ptr[e_response]) { 135920c56cecSGlenn Strauss ai->username = dp->ptr[e_username]; 136020c56cecSGlenn Strauss ai->ulen = dp->len[e_username]; 136120c56cecSGlenn Strauss ai->realm = dp->ptr[e_realm]; 136220c56cecSGlenn Strauss ai->rlen = dp->len[e_realm]; 136371175df1SGlenn Strauss ai->userhash = (dp->len[e_userhash]==4);/*("true", not "false",absent)*/ 136471175df1SGlenn Strauss if (!ai->username) { /* (dp->ptr[e_userstar]) */ 136571175df1SGlenn Strauss if (HANDLER_GO_ON != mod_auth_digest_validate_userstar(r, dp, ai)) 136671175df1SGlenn Strauss return HANDLER_FINISHED; 136771175df1SGlenn Strauss } 136820c56cecSGlenn Strauss } 136920c56cecSGlenn Strauss else { 13707c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 137181b2d1f0SGlenn Strauss "digest: missing field"); 13727c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 137381b2d1f0SGlenn Strauss } 137481b2d1f0SGlenn Strauss 137520c56cecSGlenn Strauss if (!buffer_eq_slen(require->realm, ai->realm, ai->rlen)) { 13767c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 13775a6e4df8SGlenn Strauss "digest: realm mismatch"); 13787c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 13795a6e4df8SGlenn Strauss } 13805a6e4df8SGlenn Strauss 138120c56cecSGlenn Strauss if (!mod_auth_algorithm_parse(ai,dp->ptr[e_algorithm],dp->len[e_algorithm]) 138220c56cecSGlenn Strauss || !(require->algorithm & ai->dalgo & ~HTTP_AUTH_DIGEST_SESS)) { 13837c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 138420c56cecSGlenn Strauss "digest: (%.*s): invalid", 138520c56cecSGlenn Strauss (int)dp->len[e_algorithm], dp->ptr[e_algorithm]); 138612ac70d0SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1387b9e2be50SGlenn Strauss } 1388b9e2be50SGlenn Strauss 138920c56cecSGlenn Strauss /* *-sess requires nonce and cnonce */ 139020c56cecSGlenn Strauss if ((ai->dalgo & HTTP_AUTH_DIGEST_SESS) 139120c56cecSGlenn Strauss && (!dp->ptr[e_nonce] || !dp->ptr[e_cnonce])) { 13927c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 139320c56cecSGlenn Strauss "digest: (%.*s): missing field", 139420c56cecSGlenn Strauss (int)dp->len[e_algorithm], dp->ptr[e_algorithm]); 13957c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 139681b2d1f0SGlenn Strauss } 139781b2d1f0SGlenn Strauss 139820c56cecSGlenn Strauss if (0 != li_hex2bin(dp->rdigest, sizeof(dp->rdigest), 139920c56cecSGlenn Strauss dp->ptr[e_response], dp->len[e_response]) 140020c56cecSGlenn Strauss || dp->len[e_response] != (ai->dlen << 1)) { 14017c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 140220c56cecSGlenn Strauss "digest: (%s): invalid format", dp->ptr[e_response]); 14037c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 1404b9e2be50SGlenn Strauss } 1405b9e2be50SGlenn Strauss 140620c56cecSGlenn Strauss if (dp->ptr[e_qop]&& buffer_eq_icase_ss(dp->ptr[e_qop], dp->len[e_qop], 140720c56cecSGlenn Strauss CONST_STR_LEN("auth-int"))) { 14087c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 140981b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 14107c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 141181b2d1f0SGlenn Strauss } 141281b2d1f0SGlenn Strauss 141381b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 141481b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 141581b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 141681b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 141781b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 141881b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 14197c7f8c46SGlenn Strauss * uri validation step is bypassed. r->target_orig is 14208131e439SGlenn Strauss * original request-target sent in client request. */ 142120c56cecSGlenn Strauss if (!buffer_eq_slen(&r->target_orig, dp->ptr[e_uri], dp->len[e_uri])) { 14227c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 142320c56cecSGlenn Strauss "digest: auth failed: uri mismatch (%s != %.*s), IP: %s", 142420c56cecSGlenn Strauss r->target_orig.ptr, (int)dp->len[e_uri], dp->ptr[e_uri], 1425f1e8a82fSGlenn Strauss r->con->dst_addr_buf.ptr); 14267c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 142781b2d1f0SGlenn Strauss } 142820c56cecSGlenn Strauss 142920c56cecSGlenn Strauss return HANDLER_GO_ON; 143081b2d1f0SGlenn Strauss } 143181b2d1f0SGlenn Strauss 143220c56cecSGlenn Strauss 143320c56cecSGlenn Strauss static handler_t 143420c56cecSGlenn 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) 143520c56cecSGlenn Strauss { 1436e1188e77SGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 1437e1188e77SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 1438e1188e77SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 1439e1188e77SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 1440e1188e77SGlenn Strauss * verifiable) if nanoseconds were included with seconds as part of the 1441e1188e77SGlenn Strauss * nonce "timestamp:secret". However, doing so would expose a high 1442e1188e77SGlenn Strauss * precision timestamp of the system to attackers. timestamp in nonce 1443e1188e77SGlenn Strauss * could theoretically be modified and still produce same md5sum, but 1444e1188e77SGlenn Strauss * that is highly unlikely within a 10 min (moving) window of valid 1445e1188e77SGlenn Strauss * time relative to current time (now). 1446e1188e77SGlenn Strauss * If it is desired to validate that nonces were generated by server, 1447e1188e77SGlenn Strauss * then specify auth.require = ( ... => ( "secret" => "..." ) ) 1448e1188e77SGlenn Strauss * When secret is specified, then instead of nanoseconds, the random 1449e1188e77SGlenn Strauss * data value (included for unique nonces) will be exposed in the nonce 1450e1188e77SGlenn Strauss * along with the timestamp, and the additional secret will be used to 1451e1188e77SGlenn Strauss * validate that the server generated the nonce using that secret. */ 1452309c1693SGlenn Strauss unix_time64_t ts = 0; 145320c56cecSGlenn Strauss const unsigned char * const nonce = (unsigned char *)dp->ptr[e_nonce]; 145420c56cecSGlenn Strauss int i; 1455309c1693SGlenn Strauss for (i = 0; i < 16 && light_isxdigit(nonce[i]); ++i) 1456309c1693SGlenn Strauss ts = (unix_time64_t)((uint64_t)ts << 4) | hex2int(nonce[i]); 145720c56cecSGlenn Strauss 1458309c1693SGlenn Strauss const unix_time64_t cur_ts = log_epoch_secs; 145920c56cecSGlenn Strauss if (nonce[i] != ':' || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/ 1460e1188e77SGlenn Strauss /* nonce is stale; have client regenerate digest */ 146120c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, ai->dalgo); 1462e1188e77SGlenn Strauss } 1463e1188e77SGlenn Strauss 146420c56cecSGlenn Strauss if (cur_ts - ts > 540) /*(9 mins)*/ 146520c56cecSGlenn Strauss dp->send_nextnonce_ts = cur_ts; 1466e1188e77SGlenn Strauss 1467e1188e77SGlenn Strauss if (require->nonce_secret) { 1468e1188e77SGlenn Strauss unsigned int rnd = 0; 146920c56cecSGlenn Strauss for (int j = i+8; i < j && light_isxdigit(nonce[i]); ++i) { 147020c56cecSGlenn Strauss rnd = (rnd << 4) + hex2int(nonce[i]); 1471e1188e77SGlenn Strauss } 1472e1188e77SGlenn Strauss if (nonce[i] != ':') { 1473e1188e77SGlenn Strauss /* nonce is invalid; 1474e1188e77SGlenn Strauss * expect extra field w/ require->nonce_secret */ 1475e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1476e1188e77SGlenn Strauss "digest: nonce invalid"); 1477e1188e77SGlenn Strauss return mod_auth_send_400_bad_request(r); 1478e1188e77SGlenn Strauss } 1479e1188e77SGlenn Strauss buffer * const tb = r->tmp_buf; 1480e1188e77SGlenn Strauss buffer_clear(tb); 148120c56cecSGlenn Strauss mod_auth_append_nonce(tb, cur_ts, require, ai->dalgo, (int *)&rnd); 148220c56cecSGlenn Strauss if (!buffer_eq_slen(tb, dp->ptr[e_nonce], dp->len[e_nonce])) { 1483e1188e77SGlenn Strauss /* nonce not generated using current require->nonce_secret */ 1484e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1485e1188e77SGlenn Strauss "digest: nonce mismatch"); 1486e1188e77SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1487e1188e77SGlenn Strauss } 1488e1188e77SGlenn Strauss } 148981b2d1f0SGlenn Strauss 14904b3a91e6SGlenn Strauss return HANDLER_GO_ON; 149181b2d1f0SGlenn Strauss } 149281b2d1f0SGlenn Strauss 149320c56cecSGlenn Strauss 149420c56cecSGlenn Strauss static handler_t 149520c56cecSGlenn 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) 149620c56cecSGlenn Strauss { 149720c56cecSGlenn Strauss if (NULL == backend || NULL == backend->digest) 149820c56cecSGlenn Strauss return mod_auth_digest_misconfigured(r, backend); 149920c56cecSGlenn Strauss 150020c56cecSGlenn Strauss const buffer * const vb = 150120c56cecSGlenn Strauss http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, 150220c56cecSGlenn Strauss CONST_STR_LEN("Authorization")); 150320c56cecSGlenn Strauss if (NULL == vb || !buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) 150420c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 150520c56cecSGlenn Strauss #ifdef __COVERITY__ 150620c56cecSGlenn Strauss if (buffer_clen(vb) < sizeof("Digest ")-1) 150720c56cecSGlenn Strauss return mod_auth_send_400_bad_request(r); 150820c56cecSGlenn Strauss #endif 150920c56cecSGlenn Strauss 151020c56cecSGlenn Strauss http_auth_digest_params_t dp; 151120c56cecSGlenn Strauss http_auth_info_t ai; 151220c56cecSGlenn Strauss handler_t rc; 151320c56cecSGlenn Strauss 151471175df1SGlenn Strauss /* XXX: should use offsetof() (if portable enough) */ 151520c56cecSGlenn Strauss memset(&dp, 0, sizeof(dp) - sizeof(dp.rdigest)); 151620c56cecSGlenn Strauss 15176881f79cSGlenn Strauss mod_auth_digest_parse_authorization(&dp, vb->ptr + sizeof("Digest ")-1); 151820c56cecSGlenn Strauss 151920c56cecSGlenn Strauss rc = mod_auth_digest_validate_params(r, require, &dp, &ai); 152020c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0)) 152120c56cecSGlenn Strauss return rc; 152220c56cecSGlenn Strauss 152320c56cecSGlenn Strauss rc = mod_auth_digest_validate_nonce(r, require, &dp, &ai); 152420c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0)) 152520c56cecSGlenn Strauss return rc; 152620c56cecSGlenn Strauss 152720c56cecSGlenn Strauss rc = mod_auth_digest_get(r, p_d, require, backend, &ai); 152820c56cecSGlenn Strauss if (__builtin_expect( (HANDLER_GO_ON != rc), 0)) 152920c56cecSGlenn Strauss return rc; 153020c56cecSGlenn Strauss 15319fe8fbaaSGlenn Strauss mod_auth_digest_mutate(&ai, &dp, http_method_buf(r->http_method)); 153220c56cecSGlenn Strauss 153320c56cecSGlenn Strauss if (!ck_memeq_const_time_fixed_len(dp.rdigest, ai.digest, ai.dlen)) { 153420c56cecSGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//*skip clear since mutated*/ 153520c56cecSGlenn Strauss /* digest not ok */ 153620c56cecSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 153720c56cecSGlenn Strauss "digest: auth failed for %.*s: wrong password, IP: %s", 1538f1e8a82fSGlenn Strauss (int)ai.ulen, ai.username, r->con->dst_addr_buf.ptr); 153920c56cecSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/ 154020c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 15417ba06c71SGlenn Strauss } 154220c56cecSGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//* skip clear since mutated */ 154320c56cecSGlenn Strauss 154420c56cecSGlenn Strauss /* check authorization (authz); string args must be '\0'-terminated) */ 154520c56cecSGlenn Strauss buffer * const tb = r->tmp_buf; 154620c56cecSGlenn Strauss buffer_copy_string_len(tb, ai.username, ai.ulen); 154720c56cecSGlenn Strauss if (!http_auth_match_rules(require, tb->ptr, NULL, NULL)) 154820c56cecSGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 154920c56cecSGlenn Strauss 155020c56cecSGlenn Strauss if (dp.send_nextnonce_ts) { 155120c56cecSGlenn Strauss /*(send nextnonce when expiration is approaching)*/ 155220c56cecSGlenn Strauss mod_auth_digest_authentication_info( 155320c56cecSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_OTHER, 155420c56cecSGlenn Strauss CONST_STR_LEN("Authentication-Info")), 155520c56cecSGlenn Strauss dp.send_nextnonce_ts /*(cur_ts)*/, require, ai.dalgo); 155620c56cecSGlenn Strauss } 155720c56cecSGlenn Strauss 155820c56cecSGlenn Strauss http_auth_setenv(r, ai.username, ai.ulen, CONST_STR_LEN("Digest")); 155920c56cecSGlenn Strauss return HANDLER_GO_ON; 156020c56cecSGlenn Strauss } 156120c56cecSGlenn Strauss 156220c56cecSGlenn Strauss 15637ba06c71SGlenn Strauss 15647c7f8c46SGlenn 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) { 15657ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 15667c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 15677ba06c71SGlenn Strauss UNUSED(p_d); 15687ba06c71SGlenn Strauss UNUSED(backend); 15693dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) { 1570f9d58670SStefan Bühler return HANDLER_GO_ON; 1571bcdc6a3bSJan Kneschke } else { 15727c7f8c46SGlenn Strauss r->http_status = 401; 15737c7f8c46SGlenn Strauss r->handler_module = NULL; 1574bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 15757187271fSStefan Bühler } 1576bcdc6a3bSJan Kneschke } 1577