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; 48e11514b0SGlenn Strauss time_t ctime; 49e11514b0SGlenn Strauss int dalgo; 50e11514b0SGlenn Strauss uint32_t dlen; 51e11514b0SGlenn Strauss uint32_t ulen; 52e11514b0SGlenn Strauss char *username; 53e11514b0SGlenn Strauss char *pwdigest; 54e11514b0SGlenn Strauss } http_auth_cache_entry; 55e11514b0SGlenn Strauss 56e11514b0SGlenn Strauss static http_auth_cache_entry * 57e11514b0SGlenn Strauss http_auth_cache_entry_init (const struct http_auth_require_t * const require, const int dalgo, const char *username, const uint32_t ulen, const char *pw, const uint32_t pwlen) 58e11514b0SGlenn Strauss { 59e11514b0SGlenn Strauss /*(similar to buffer_copy_string_len() for each element, 60e11514b0SGlenn Strauss * but allocate exact lengths in single chunk of memory 61e11514b0SGlenn Strauss * for cache to avoid wasting space and for memory locality)*/ 62e11514b0SGlenn Strauss /* http_auth_require_t is stored instead of copying realm 63e11514b0SGlenn Strauss *(store pointer to http_auth_require_t, which is persistent 64e11514b0SGlenn Strauss * and will be different for each realm + permissions combo)*/ 65e11514b0SGlenn Strauss http_auth_cache_entry * const ae = 66e11514b0SGlenn Strauss malloc(sizeof(http_auth_cache_entry) + ulen + pwlen); 67e11514b0SGlenn Strauss force_assert(ae); 68e11514b0SGlenn Strauss ae->require = require; 69dbe3e236SGlenn Strauss ae->ctime = log_monotonic_secs; 70e11514b0SGlenn Strauss ae->dalgo = dalgo; 71e11514b0SGlenn Strauss ae->ulen = ulen; 72e11514b0SGlenn Strauss ae->dlen = pwlen; 73e11514b0SGlenn Strauss ae->username = (char *)(ae + 1); 74e11514b0SGlenn Strauss ae->pwdigest = ae->username + ulen; 75e11514b0SGlenn Strauss memcpy(ae->username, username, ulen); 76e11514b0SGlenn Strauss memcpy(ae->pwdigest, pw, pwlen); 77e11514b0SGlenn Strauss return ae; 78e11514b0SGlenn Strauss } 79e11514b0SGlenn Strauss 80e11514b0SGlenn Strauss static void 81e11514b0SGlenn Strauss http_auth_cache_entry_free (void *data) 82e11514b0SGlenn Strauss { 83e11514b0SGlenn Strauss http_auth_cache_entry * const ae = data; 8408c03cd4SGlenn Strauss ck_memzero(ae->pwdigest, ae->dlen); 85e11514b0SGlenn Strauss free(ae); 86e11514b0SGlenn Strauss } 87e11514b0SGlenn Strauss 88e11514b0SGlenn Strauss static void 89e11514b0SGlenn Strauss http_auth_cache_free (http_auth_cache *ac) 90e11514b0SGlenn Strauss { 91e11514b0SGlenn Strauss splay_tree *sptree = ac->sptree; 92e11514b0SGlenn Strauss while (sptree) { 93e11514b0SGlenn Strauss http_auth_cache_entry_free(sptree->data); 94e11514b0SGlenn Strauss sptree = splaytree_delete(sptree, sptree->key); 95e11514b0SGlenn Strauss } 96e11514b0SGlenn Strauss free(ac); 97e11514b0SGlenn Strauss } 98e11514b0SGlenn Strauss 99e11514b0SGlenn Strauss static http_auth_cache * 100e11514b0SGlenn Strauss http_auth_cache_init (const array *opts) 101e11514b0SGlenn Strauss { 102e11514b0SGlenn Strauss http_auth_cache *ac = malloc(sizeof(http_auth_cache)); 103e11514b0SGlenn Strauss force_assert(ac); 104e11514b0SGlenn Strauss ac->sptree = NULL; 105e11514b0SGlenn Strauss ac->max_age = 600; /* 10 mins */ 106e11514b0SGlenn Strauss for (uint32_t i = 0, used = opts->used; i < used; ++i) { 107d6f692f1SGlenn Strauss data_unset *du = opts->data[i]; 108d6f692f1SGlenn Strauss if (buffer_is_equal_string(&du->key, CONST_STR_LEN("max-age"))) 109d6f692f1SGlenn Strauss ac->max_age = (time_t)config_plugin_value_to_int32(du, ac->max_age); 110e11514b0SGlenn Strauss } 111e11514b0SGlenn Strauss return ac; 112e11514b0SGlenn Strauss } 113e11514b0SGlenn Strauss 114e11514b0SGlenn Strauss static int 115e11514b0SGlenn Strauss http_auth_cache_hash (const struct http_auth_require_t * const require, const char *username, const uint32_t ulen) 116e11514b0SGlenn Strauss { 117e11514b0SGlenn Strauss uint32_t h = /*(hash pointer value, which includes realm and permissions)*/ 118e11514b0SGlenn Strauss djbhash((char *)(intptr_t)require, sizeof(intptr_t), DJBHASH_INIT); 119e11514b0SGlenn Strauss h = djbhash(username, ulen, h); 120e11514b0SGlenn Strauss /* strip highest bit of hash value for splaytree (see splaytree_djbhash())*/ 121e11514b0SGlenn Strauss return (int32_t)(h & ~(((uint32_t)1) << 31)); 122e11514b0SGlenn Strauss } 123e11514b0SGlenn Strauss 124e11514b0SGlenn Strauss static http_auth_cache_entry * 125e11514b0SGlenn Strauss http_auth_cache_query (splay_tree ** const sptree, const int ndx) 126e11514b0SGlenn Strauss { 127e11514b0SGlenn Strauss *sptree = splaytree_splay(*sptree, ndx); 128e11514b0SGlenn Strauss return (*sptree && (*sptree)->key == ndx) ? (*sptree)->data : NULL; 129e11514b0SGlenn Strauss } 130e11514b0SGlenn Strauss 131e11514b0SGlenn Strauss static void 132e11514b0SGlenn Strauss http_auth_cache_insert (splay_tree ** const sptree, const int ndx, void * const data, void(data_free_fn)(void *)) 133e11514b0SGlenn Strauss { 134e11514b0SGlenn Strauss /*(not necessary to re-splay (with current usage) since single-threaded 135e11514b0SGlenn Strauss * and splaytree has not been modified since http_auth_cache_query())*/ 136e11514b0SGlenn Strauss /* *sptree = splaytree_splay(*sptree, ndx); */ 137e11514b0SGlenn Strauss if (NULL == *sptree || (*sptree)->key != ndx) 138e11514b0SGlenn Strauss *sptree = splaytree_insert(*sptree, ndx, data); 139e11514b0SGlenn Strauss else { /* collision; replace old entry */ 140e11514b0SGlenn Strauss data_free_fn((*sptree)->data); 141e11514b0SGlenn Strauss (*sptree)->data = data; 142e11514b0SGlenn Strauss } 143e11514b0SGlenn Strauss } 144e11514b0SGlenn Strauss 145e11514b0SGlenn Strauss /* walk though cache, collect expired ids, and remove them in a second loop */ 146e11514b0SGlenn Strauss static void 147e11514b0SGlenn Strauss mod_auth_tag_old_entries (splay_tree * const t, int * const keys, int * const ndx, const time_t max_age, const time_t cur_ts) 148e11514b0SGlenn Strauss { 149e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/ 150e11514b0SGlenn Strauss if (t->left) 151e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->left, keys, ndx, max_age, cur_ts); 152e11514b0SGlenn Strauss if (t->right) 153e11514b0SGlenn Strauss mod_auth_tag_old_entries(t->right, keys, ndx, max_age, cur_ts); 154e11514b0SGlenn Strauss if (*ndx == 8192) return; /*(must match num array entries in keys[])*/ 155e11514b0SGlenn Strauss 156e11514b0SGlenn Strauss const http_auth_cache_entry * const ae = t->data; 157e11514b0SGlenn Strauss if (cur_ts - ae->ctime > max_age) 158e11514b0SGlenn Strauss keys[(*ndx)++] = t->key; 159e11514b0SGlenn Strauss } 160e11514b0SGlenn Strauss 161e11514b0SGlenn Strauss __attribute_noinline__ 162e11514b0SGlenn Strauss static void 163e11514b0SGlenn Strauss mod_auth_periodic_cleanup(splay_tree **sptree_ptr, const time_t max_age, const time_t cur_ts) 164e11514b0SGlenn Strauss { 165e11514b0SGlenn Strauss splay_tree *sptree = *sptree_ptr; 166e11514b0SGlenn Strauss int max_ndx, i; 167e11514b0SGlenn Strauss int keys[8192]; /* 32k size on stack */ 168e11514b0SGlenn Strauss do { 169e11514b0SGlenn Strauss if (!sptree) break; 170e11514b0SGlenn Strauss max_ndx = 0; 171e11514b0SGlenn Strauss mod_auth_tag_old_entries(sptree, keys, &max_ndx, max_age, cur_ts); 172e11514b0SGlenn Strauss for (i = 0; i < max_ndx; ++i) { 173e11514b0SGlenn Strauss int ndx = keys[i]; 174e11514b0SGlenn Strauss sptree = splaytree_splay(sptree, ndx); 175e11514b0SGlenn Strauss if (sptree && sptree->key == ndx) { 176e11514b0SGlenn Strauss http_auth_cache_entry_free(sptree->data); 177e11514b0SGlenn Strauss sptree = splaytree_delete(sptree, ndx); 178e11514b0SGlenn Strauss } 179e11514b0SGlenn Strauss } 180e11514b0SGlenn Strauss } while (max_ndx == sizeof(keys)/sizeof(int)); 181e11514b0SGlenn Strauss *sptree_ptr = sptree; 182e11514b0SGlenn Strauss } 183e11514b0SGlenn Strauss 184e11514b0SGlenn Strauss TRIGGER_FUNC(mod_auth_periodic) 185e11514b0SGlenn Strauss { 186e11514b0SGlenn Strauss const plugin_data * const p = p_d; 187dbe3e236SGlenn Strauss const time_t cur_ts = log_monotonic_secs; 188e11514b0SGlenn Strauss if (cur_ts & 0x7) return HANDLER_GO_ON; /*(continue once each 8 sec)*/ 189e11514b0SGlenn Strauss UNUSED(srv); 190e11514b0SGlenn Strauss 191e11514b0SGlenn Strauss /* future: might construct array of (http_auth_cache *) at startup 192e11514b0SGlenn Strauss * to avoid the need to search for them here */ 19333e400b4SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 19433e400b4SGlenn Strauss if (NULL == p->cvlist) return HANDLER_GO_ON; 19533e400b4SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 196e11514b0SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 197e11514b0SGlenn Strauss for (; cpv->k_id != -1; ++cpv) { 198e11514b0SGlenn Strauss if (cpv->k_id != 3) continue; /* k_id == 3 for auth.cache */ 199e11514b0SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL) continue; 200e11514b0SGlenn Strauss http_auth_cache *ac = cpv->v.v; 201e11514b0SGlenn Strauss mod_auth_periodic_cleanup(&ac->sptree, ac->max_age, cur_ts); 202e11514b0SGlenn Strauss } 203e11514b0SGlenn Strauss } 204e11514b0SGlenn Strauss 205e11514b0SGlenn Strauss return HANDLER_GO_ON; 206e11514b0SGlenn Strauss } 207e11514b0SGlenn Strauss 208e11514b0SGlenn Strauss 209e11514b0SGlenn Strauss 210e11514b0SGlenn Strauss 2117c7f8c46SGlenn 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); 2127c7f8c46SGlenn 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); 2137c7f8c46SGlenn 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); 2144b3a91e6SGlenn Strauss 215bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) { 216e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL }; 217e11514b0SGlenn Strauss static http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL }; 2187ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL }; 219e11514b0SGlenn Strauss plugin_data *p = calloc(1, sizeof(*p)); 220e11514b0SGlenn Strauss force_assert(p); 2217ba06c71SGlenn Strauss 2227ba06c71SGlenn Strauss /* register http_auth_scheme_* */ 223e11514b0SGlenn Strauss http_auth_scheme_basic.p_d = p; 2247ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic); 225e11514b0SGlenn Strauss http_auth_scheme_digest.p_d = p; 2267ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest); 2277ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern); 228bcdc6a3bSJan Kneschke 229bcdc6a3bSJan Kneschke return p; 230bcdc6a3bSJan Kneschke } 231bcdc6a3bSJan Kneschke 232b73949e0SGlenn Strauss FREE_FUNC(mod_auth_free) { 233b73949e0SGlenn Strauss plugin_data * const p = p_d; 2348e713130SGlenn Strauss if (NULL == p->cvlist) return; 2358e713130SGlenn Strauss /* (init i to 0 if global context; to 1 to skip empty global context) */ 2368e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) { 2378e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 2388e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 2398e713130SGlenn Strauss if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue; 2408e713130SGlenn Strauss switch (cpv->k_id) { 2418e713130SGlenn Strauss case 1: /* auth.require */ 2428e713130SGlenn Strauss array_free(cpv->v.v); 2438e713130SGlenn Strauss break; 244e11514b0SGlenn Strauss case 3: /* auth.cache */ 245e11514b0SGlenn Strauss http_auth_cache_free(cpv->v.v); 246e11514b0SGlenn Strauss break; 2478e713130SGlenn Strauss default: 2488e713130SGlenn Strauss break; 2498e713130SGlenn Strauss } 2508e713130SGlenn Strauss } 2518e713130SGlenn Strauss } 2521a8ac120SGlenn Strauss 2531a8ac120SGlenn Strauss http_auth_dumbdata_reset(); 2548e713130SGlenn Strauss } 2558e713130SGlenn Strauss 2567ba06c71SGlenn Strauss /* data type for mod_auth structured data 2577ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */ 2587ba06c71SGlenn Strauss typedef struct { 2597ba06c71SGlenn Strauss DATA_UNSET; 2607ba06c71SGlenn Strauss http_auth_require_t *require; 2617ba06c71SGlenn Strauss } data_auth; 2627ba06c71SGlenn Strauss 2637ba06c71SGlenn Strauss static void data_auth_free(data_unset *d) 2647ba06c71SGlenn Strauss { 2657ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d; 266ad9b7e00SGlenn Strauss free(dauth->key.ptr); 2677ba06c71SGlenn Strauss http_auth_require_free(dauth->require); 2687ba06c71SGlenn Strauss free(dauth); 2697ba06c71SGlenn Strauss } 2707ba06c71SGlenn Strauss 2717ba06c71SGlenn Strauss static data_auth *data_auth_init(void) 2727ba06c71SGlenn Strauss { 2738c7f1dfbSGlenn Strauss static const struct data_methods fn = { 2748c7f1dfbSGlenn Strauss NULL, /* copy must not be called on this data */ 2758c7f1dfbSGlenn Strauss data_auth_free, 2768c7f1dfbSGlenn Strauss NULL, /* insert_dup must not be called on this data */ 2778c7f1dfbSGlenn Strauss }; 2787ba06c71SGlenn Strauss data_auth * const dauth = calloc(1, sizeof(*dauth)); 2797ba06c71SGlenn Strauss force_assert(NULL != dauth); 2807ba06c71SGlenn Strauss dauth->type = TYPE_OTHER; 2818c7f1dfbSGlenn Strauss dauth->fn = &fn; 2827ba06c71SGlenn Strauss 2837ba06c71SGlenn Strauss dauth->require = http_auth_require_init(); 2847ba06c71SGlenn Strauss 2857ba06c71SGlenn Strauss return dauth; 2867ba06c71SGlenn Strauss } 2877ba06c71SGlenn Strauss 288b9e2be50SGlenn Strauss static int mod_auth_algorithm_parse(http_auth_info_t *ai, const char *s) { 289b9e2be50SGlenn Strauss size_t len; 290b9e2be50SGlenn Strauss if (NULL == s) { 291b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_MD5; 292b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 293b9e2be50SGlenn Strauss return 1; 294b9e2be50SGlenn Strauss } 295b9e2be50SGlenn Strauss 296b9e2be50SGlenn Strauss len = strlen(s); 297b9e2be50SGlenn Strauss if (len > 5 298b9e2be50SGlenn Strauss && (s[len-5] ) == '-' 299b9e2be50SGlenn Strauss && (s[len-4] | 0x20) == 's' 300b9e2be50SGlenn Strauss && (s[len-3] | 0x20) == 'e' 301b9e2be50SGlenn Strauss && (s[len-2] | 0x20) == 's' 302b9e2be50SGlenn Strauss && (s[len-1] | 0x20) == 's') { 303b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_SESS; 304b9e2be50SGlenn Strauss len -= 5; 305b9e2be50SGlenn Strauss } 306b9e2be50SGlenn Strauss else { 307b9e2be50SGlenn Strauss ai->dalgo = HTTP_AUTH_DIGEST_NONE; 308b9e2be50SGlenn Strauss } 309b9e2be50SGlenn Strauss 310b9e2be50SGlenn Strauss if (3 == len 311b9e2be50SGlenn Strauss && 'm' == (s[0] | 0x20) 312b9e2be50SGlenn Strauss && 'd' == (s[1] | 0x20) 313b9e2be50SGlenn Strauss && '5' == (s[2] )) { 314b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_MD5; 315b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_MD5_BINLEN; 316b9e2be50SGlenn Strauss return 1; 317b9e2be50SGlenn Strauss } 318b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 319b9e2be50SGlenn Strauss else if (len >= 7 320b9e2be50SGlenn Strauss && 's' == (s[0] | 0x20) 321b9e2be50SGlenn Strauss && 'h' == (s[1] | 0x20) 322b9e2be50SGlenn Strauss && 'a' == (s[2] | 0x20) 323b9e2be50SGlenn Strauss && '-' == (s[3] )) { 324b9e2be50SGlenn Strauss if (len == 7 && s[4] == '2' && s[5] == '5' && s[6] == '6') { 325b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA256; 326b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA256_BINLEN; 327b9e2be50SGlenn Strauss return 1; 328b9e2be50SGlenn Strauss } 3291fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 330b9e2be50SGlenn Strauss if (len == 11 && 0 == memcmp(s+4, "512-256", 7)) { 331b9e2be50SGlenn Strauss ai->dalgo |= HTTP_AUTH_DIGEST_SHA512_256; 332b9e2be50SGlenn Strauss ai->dlen = HTTP_AUTH_DIGEST_SHA512_256_BINLEN; 333b9e2be50SGlenn Strauss return 1; 334b9e2be50SGlenn Strauss } 335b9e2be50SGlenn Strauss #endif 336b9e2be50SGlenn Strauss } 337b9e2be50SGlenn Strauss #endif 338b9e2be50SGlenn Strauss return 0; /*(error)*/ 339b9e2be50SGlenn Strauss } 340b9e2be50SGlenn Strauss 341b9e2be50SGlenn Strauss static int mod_auth_algorithms_parse(int *algorithm, buffer *algos) { 342b9e2be50SGlenn Strauss for (char *s = algos->ptr, *p; s; s = p ? p+1 : NULL) { 343b9e2be50SGlenn Strauss http_auth_info_t ai; 344b9e2be50SGlenn Strauss int rc; 345b9e2be50SGlenn Strauss p = strchr(s, '|'); 346b9e2be50SGlenn Strauss if (p) *p = '\0'; 347b9e2be50SGlenn Strauss rc = mod_auth_algorithm_parse(&ai, s); 348b9e2be50SGlenn Strauss if (p) *p = '|'; 349b9e2be50SGlenn Strauss if (!rc) return 0; 350b9e2be50SGlenn Strauss *algorithm |= ai.dalgo; 351b9e2be50SGlenn Strauss } 352b9e2be50SGlenn Strauss return 1; 353b9e2be50SGlenn Strauss } 354b9e2be50SGlenn Strauss 355b5775b99SGlenn Strauss static int mod_auth_require_parse (http_auth_require_t * const require, const buffer *b, log_error_st *errh) 3567ba06c71SGlenn Strauss { 3577ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */ 3587ba06c71SGlenn Strauss 3597ba06c71SGlenn Strauss const char *str = b->ptr; 3607ba06c71SGlenn Strauss const char *p; 3617ba06c71SGlenn Strauss 3627ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) { 3637ba06c71SGlenn Strauss require->valid_user = 1; 3647ba06c71SGlenn Strauss return 1; /* success */ 3657ba06c71SGlenn Strauss } 3667ba06c71SGlenn Strauss 3677ba06c71SGlenn Strauss do { 3687ba06c71SGlenn Strauss const char *eq; 3697ba06c71SGlenn Strauss size_t len; 3707ba06c71SGlenn Strauss p = strchr(str, '|'); 3717ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str); 3727ba06c71SGlenn Strauss eq = memchr(str, '=', len); 3737ba06c71SGlenn Strauss if (NULL == eq) { 374b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 375010c2894SGlenn Strauss "error parsing auth.require 'require' field: missing '=' " 376010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 377010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 3787ba06c71SGlenn Strauss return 0; 3797ba06c71SGlenn Strauss } 380b2c18f48SGlenn Strauss if (eq[1] == '|' || eq[1] == '\0') { 381b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 382010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 383010c2894SGlenn Strauss "missing token after '=' " 384010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 385010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 3867ba06c71SGlenn Strauss return 0; 3877ba06c71SGlenn Strauss } 3887ba06c71SGlenn Strauss 3897ba06c71SGlenn Strauss switch ((int)(eq - str)) { 3907ba06c71SGlenn Strauss case 4: 3917ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) { 392810109ccSGlenn Strauss /*("user=" is 5)*/ 393a03afc90SGlenn Strauss array_set_key_value(&require->user, str+5, len-5, CONST_STR_LEN("")); 3947ba06c71SGlenn Strauss continue; 3957ba06c71SGlenn Strauss } 3967ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) { 397810109ccSGlenn Strauss /*("host=" is 5)*/ 398a03afc90SGlenn Strauss array_set_key_value(&require->host, str+5, len-5, CONST_STR_LEN("")); 399b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 400010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 401010c2894SGlenn Strauss "'host' not implemented; field value: %s", b->ptr); 4027ba06c71SGlenn Strauss continue; 4037ba06c71SGlenn Strauss } 4047ba06c71SGlenn Strauss break; /* to error */ 4057ba06c71SGlenn Strauss case 5: 4067ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) { 407810109ccSGlenn Strauss /*("group=" is 6)*/ 408a03afc90SGlenn Strauss array_set_key_value(&require->group, str+6, len-6, CONST_STR_LEN("")); 4099c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/ 410b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 411010c2894SGlenn Strauss "warning parsing auth.require 'require' field: " 412010c2894SGlenn Strauss "'group' not implemented; field value: %s", b->ptr); 4139c91af0cSGlenn Strauss #endif 4147ba06c71SGlenn Strauss continue; 4157ba06c71SGlenn Strauss } 4167ba06c71SGlenn Strauss break; /* to error */ 4177ba06c71SGlenn Strauss case 10: 4187ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) { 419b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 420010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 421010c2894SGlenn Strauss "valid user can not be combined with other require rules " 422010c2894SGlenn Strauss "(expecting \"valid-user\" or " 423010c2894SGlenn Strauss "\"user=a|user=b|group=g|host=h\"). error value: %s", b->ptr); 4247ba06c71SGlenn Strauss return 0; 4257ba06c71SGlenn Strauss } 4267ba06c71SGlenn Strauss break; /* to error */ 4277ba06c71SGlenn Strauss default: 4287ba06c71SGlenn Strauss break; /* to error */ 4297ba06c71SGlenn Strauss } 4307ba06c71SGlenn Strauss 431b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 432010c2894SGlenn Strauss "error parsing auth.require 'require' field: " 433010c2894SGlenn Strauss "invalid/unsupported token " 434010c2894SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\"). " 435010c2894SGlenn Strauss "error value: %s error near: %s", b->ptr, str); 4367ba06c71SGlenn Strauss return 0; 4377ba06c71SGlenn Strauss 4387ba06c71SGlenn Strauss } while (p && *((str = p+1))); 4397ba06c71SGlenn Strauss 4407ba06c71SGlenn Strauss return 1; /* success */ 4417ba06c71SGlenn Strauss } 4427ba06c71SGlenn Strauss 443b5775b99SGlenn Strauss static handler_t mod_auth_require_parse_array(const array *value, array * const auth_require, log_error_st *errh) 4448e713130SGlenn Strauss { 4458e713130SGlenn Strauss for (uint32_t n = 0; n < value->used; ++n) { 4467ba06c71SGlenn Strauss size_t m; 4478e713130SGlenn Strauss data_array *da_file = (data_array *)value->data[n]; 4487ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL; 449e1188e77SGlenn Strauss const buffer *nonce_secret = NULL; 4507ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme; 451b9e2be50SGlenn Strauss buffer *algos = NULL; 452b9e2be50SGlenn Strauss int algorithm = HTTP_AUTH_DIGEST_SESS; 4537ba06c71SGlenn Strauss 454c2238256SGlenn Strauss if (!array_is_kvstring(&da_file->value)) { 455b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 456010c2894SGlenn Strauss "unexpected value for auth.require; expected " 457bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 4587ba06c71SGlenn Strauss 4597ba06c71SGlenn Strauss return HANDLER_ERROR; 4607ba06c71SGlenn Strauss } 4617ba06c71SGlenn Strauss 462c2238256SGlenn Strauss for (m = 0; m < da_file->value.used; m++) { 463c2238256SGlenn Strauss if (da_file->value.data[m]->type == TYPE_STRING) { 464c2238256SGlenn Strauss data_string *ds = (data_string *)da_file->value.data[m]; 465ad9b7e00SGlenn Strauss if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("method"))) { 466601c572cSGlenn Strauss method = &ds->value; 467ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("realm"))) { 468601c572cSGlenn Strauss realm = &ds->value; 469ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("require"))) { 470601c572cSGlenn Strauss require = &ds->value; 471ad9b7e00SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("algorithm"))) { 472601c572cSGlenn Strauss algos = &ds->value; 47333ae8302SGlenn Strauss } else if (buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce_secret")) 47433ae8302SGlenn Strauss || buffer_is_equal_string(&ds->key, CONST_STR_LEN("nonce-secret"))) { 475e1188e77SGlenn Strauss nonce_secret = &ds->value; 4767ba06c71SGlenn Strauss } else { 477b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 478010c2894SGlenn Strauss "the field is unknown in: " 479010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 480010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 4817ba06c71SGlenn Strauss 4827ba06c71SGlenn Strauss return HANDLER_ERROR; 4837ba06c71SGlenn Strauss } 4847ba06c71SGlenn Strauss } else { 485b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 486010c2894SGlenn Strauss "a string was expected for: " 487010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"%s\" <- => \"...\" ) )", 488010c2894SGlenn Strauss da_file->value.data[m]->key.ptr); 4897ba06c71SGlenn Strauss 4907ba06c71SGlenn Strauss return HANDLER_ERROR; 4917ba06c71SGlenn Strauss } 4927ba06c71SGlenn Strauss } 4937ba06c71SGlenn Strauss 494*af3df29aSGlenn Strauss if (!method || buffer_is_blank(method)) { 495b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 496010c2894SGlenn Strauss "the method field is missing or blank in: " 4977ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )"); 4987ba06c71SGlenn Strauss return HANDLER_ERROR; 4997ba06c71SGlenn Strauss } else { 5007ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method); 5017ba06c71SGlenn Strauss if (NULL == auth_scheme) { 502b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 503010c2894SGlenn Strauss "unknown method %s (e.g. \"basic\", \"digest\" or \"extern\") in " 504010c2894SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )", method->ptr); 5057ba06c71SGlenn Strauss return HANDLER_ERROR; 5067ba06c71SGlenn Strauss } 5077ba06c71SGlenn Strauss } 5087ba06c71SGlenn Strauss 509*af3df29aSGlenn Strauss if (!realm) { 510b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 511010c2894SGlenn Strauss "the realm field is missing in: " 5127ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )"); 5137ba06c71SGlenn Strauss return HANDLER_ERROR; 5147ba06c71SGlenn Strauss } 5157ba06c71SGlenn Strauss 516*af3df29aSGlenn Strauss if (!require || buffer_is_blank(require)) { 517b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 518010c2894SGlenn Strauss "the require field is missing or blank in: " 5197ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )"); 5207ba06c71SGlenn Strauss return HANDLER_ERROR; 5217ba06c71SGlenn Strauss } 5227ba06c71SGlenn Strauss 523*af3df29aSGlenn Strauss if (!algos || buffer_is_blank(algos)) { 524b9e2be50SGlenn Strauss algorithm |= HTTP_AUTH_DIGEST_MD5; 525b9e2be50SGlenn Strauss } else if (!mod_auth_algorithms_parse(&algorithm, algos)) { 526b5775b99SGlenn Strauss log_error(errh, __FILE__, __LINE__, 527010c2894SGlenn Strauss "invalid algorithm in: " 528b9e2be50SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"algorithm\" => \"...\" ) )"); 529b9e2be50SGlenn Strauss return HANDLER_ERROR; 530b9e2be50SGlenn Strauss } 531b9e2be50SGlenn Strauss 532609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/ 5337ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init(); 534ad9b7e00SGlenn Strauss buffer_copy_buffer(&dauth->key, &da_file->key); 5357ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme; 536b9e2be50SGlenn Strauss dauth->require->algorithm = algorithm; 537a03afc90SGlenn Strauss dauth->require->realm = realm; 538e1188e77SGlenn Strauss dauth->require->nonce_secret = nonce_secret; /*(NULL is ok)*/ 539b5775b99SGlenn Strauss if (!mod_auth_require_parse(dauth->require, require, errh)) { 5408c7f1dfbSGlenn Strauss dauth->fn->free((data_unset *)dauth); 5417ba06c71SGlenn Strauss return HANDLER_ERROR; 5427ba06c71SGlenn Strauss } 5438e713130SGlenn Strauss array_insert_unique(auth_require, (data_unset *)dauth); 5447ba06c71SGlenn Strauss } 5457ba06c71SGlenn Strauss } 5467ba06c71SGlenn Strauss 5477ba06c71SGlenn Strauss return HANDLER_GO_ON; 5487ba06c71SGlenn Strauss } 5497ba06c71SGlenn Strauss 5508e713130SGlenn Strauss static void mod_auth_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) { 5518e713130SGlenn Strauss switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */ 5528e713130SGlenn Strauss case 0: /* auth.backend */ 5538e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 5548e713130SGlenn Strauss pconf->auth_backend = cpv->v.v; 5558e713130SGlenn Strauss break; 5568e713130SGlenn Strauss case 1: /* auth.require */ 5578e713130SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 5588e713130SGlenn Strauss pconf->auth_require = cpv->v.v; 5598e713130SGlenn Strauss break; 5608e713130SGlenn Strauss case 2: /* auth.extern-authn */ 5618e713130SGlenn Strauss pconf->auth_extern_authn = cpv->v.u; 562e11514b0SGlenn Strauss break; 563e11514b0SGlenn Strauss case 3: /* auth.cache */ 564e11514b0SGlenn Strauss if (cpv->vtype == T_CONFIG_LOCAL) 565e11514b0SGlenn Strauss pconf->auth_cache = cpv->v.v; 566e11514b0SGlenn Strauss break; 5678e713130SGlenn Strauss default:/* should not happen */ 5688e713130SGlenn Strauss return; 5698e713130SGlenn Strauss } 5708e713130SGlenn Strauss } 5714a6ce4aeSJan Kneschke 5728e713130SGlenn Strauss static void mod_auth_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) { 5738e713130SGlenn Strauss do { 5748e713130SGlenn Strauss mod_auth_merge_config_cpv(pconf, cpv); 5758e713130SGlenn Strauss } while ((++cpv)->k_id != -1); 5768e713130SGlenn Strauss } 577bcdc6a3bSJan Kneschke 5787c7f8c46SGlenn Strauss static void mod_auth_patch_config(request_st * const r, plugin_data * const p) { 579cc2134c8SGlenn Strauss p->conf = p->defaults; /* copy small struct instead of memcpy() */ 580cc2134c8SGlenn Strauss /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/ 5818e713130SGlenn Strauss for (int i = 1, used = p->nconfig; i < used; ++i) { 5827c7f8c46SGlenn Strauss if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id)) 5838e713130SGlenn Strauss mod_auth_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]); 5848e713130SGlenn Strauss } 5858e713130SGlenn Strauss } 58636f64b26SGlenn Strauss 5878e713130SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 5888e713130SGlenn Strauss static const config_plugin_keys_t cpk[] = { 5898e713130SGlenn Strauss { CONST_STR_LEN("auth.backend"), 5908e713130SGlenn Strauss T_CONFIG_STRING, 5918e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 5928e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.require"), 59303b4c993SGlenn Strauss T_CONFIG_ARRAY_KVARRAY, 5948e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 5958e713130SGlenn Strauss ,{ CONST_STR_LEN("auth.extern-authn"), 5968e713130SGlenn Strauss T_CONFIG_BOOL, 5978e713130SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 598e11514b0SGlenn Strauss ,{ CONST_STR_LEN("auth.cache"), 599e11514b0SGlenn Strauss T_CONFIG_ARRAY, 600e11514b0SGlenn Strauss T_CONFIG_SCOPE_CONNECTION } 6018e713130SGlenn Strauss ,{ NULL, 0, 6028e713130SGlenn Strauss T_CONFIG_UNSET, 6038e713130SGlenn Strauss T_CONFIG_SCOPE_UNSET } 6048e713130SGlenn Strauss }; 605bcdc6a3bSJan Kneschke 6068e713130SGlenn Strauss plugin_data * const p = p_d; 6078e713130SGlenn Strauss if (!config_plugin_values_init(srv, p, cpk, "mod_auth")) 6088e713130SGlenn Strauss return HANDLER_ERROR; 609bcdc6a3bSJan Kneschke 6108e713130SGlenn Strauss /* process and validate config directives 6118e713130SGlenn Strauss * (init i to 0 if global context; to 1 to skip empty global context) */ 6128e713130SGlenn Strauss for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) { 6138e713130SGlenn Strauss config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0]; 6148e713130SGlenn Strauss for (; -1 != cpv->k_id; ++cpv) { 6158e713130SGlenn Strauss switch (cpv->k_id) { 6168e713130SGlenn Strauss case 0: /* auth.backend */ 617*af3df29aSGlenn Strauss if (!buffer_is_blank(cpv->v.b)) { 6188e713130SGlenn Strauss const http_auth_backend_t * const auth_backend = 6198e713130SGlenn Strauss http_auth_backend_get(cpv->v.b); 6208e713130SGlenn Strauss if (NULL == auth_backend) { 6218e713130SGlenn Strauss log_error(srv->errh, __FILE__, __LINE__, 6228e713130SGlenn Strauss "auth.backend not supported: %s", cpv->v.b->ptr); 6238e713130SGlenn Strauss return HANDLER_ERROR; 6248e713130SGlenn Strauss } 6258e713130SGlenn Strauss *(const http_auth_backend_t **)&cpv->v.v = auth_backend; 6268e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 6278e713130SGlenn Strauss } 6288e713130SGlenn Strauss break; 6298e713130SGlenn Strauss case 1: /* auth.require */ 63003b4c993SGlenn Strauss { 63124680a91SGlenn Strauss array * const a = array_init(4); 6328e713130SGlenn Strauss if (HANDLER_GO_ON != 633b5775b99SGlenn Strauss mod_auth_require_parse_array(cpv->v.a, a, srv->errh)) { 6348e713130SGlenn Strauss array_free(a); 6358e713130SGlenn Strauss return HANDLER_ERROR; 6368e713130SGlenn Strauss } 6378e713130SGlenn Strauss cpv->v.a = a; 6388e713130SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 6398e713130SGlenn Strauss } 6408e713130SGlenn Strauss break; 6418e713130SGlenn Strauss case 2: /* auth.extern-authn */ 6428e713130SGlenn Strauss break; 643e11514b0SGlenn Strauss case 3: /* auth.cache */ 644e11514b0SGlenn Strauss cpv->v.v = http_auth_cache_init(cpv->v.a); 645e11514b0SGlenn Strauss cpv->vtype = T_CONFIG_LOCAL; 646e11514b0SGlenn Strauss break; 6478e713130SGlenn Strauss default:/* should not happen */ 6488e713130SGlenn Strauss break; 649bcdc6a3bSJan Kneschke } 650bcdc6a3bSJan Kneschke } 651bcdc6a3bSJan Kneschke } 652bcdc6a3bSJan Kneschke 6538e713130SGlenn Strauss /* initialize p->defaults from global config context */ 6548e713130SGlenn Strauss if (p->nconfig > 0 && p->cvlist->v.u2[1]) { 6558e713130SGlenn Strauss const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0]; 6568e713130SGlenn Strauss if (-1 != cpv->k_id) 6578e713130SGlenn Strauss mod_auth_merge_config(&p->defaults, cpv); 658bcdc6a3bSJan Kneschke } 6598e713130SGlenn Strauss 6608e713130SGlenn Strauss return HANDLER_GO_ON; 6618e713130SGlenn Strauss } 662bcdc6a3bSJan Kneschke 6637c7f8c46SGlenn Strauss static handler_t mod_auth_uri_handler(request_st * const r, void *p_d) { 6647ba06c71SGlenn Strauss plugin_data *p = p_d; 665d61f3381SGlenn Strauss data_auth *dauth; 66681b2d1f0SGlenn Strauss 6677c7f8c46SGlenn Strauss mod_auth_patch_config(r, p); 66881b2d1f0SGlenn Strauss 6697ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON; 67081b2d1f0SGlenn Strauss 6717ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */ 6727ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */ 6737c7f8c46SGlenn Strauss dauth = (!r->conf.force_lowercase_filenames) 6747c7f8c46SGlenn Strauss ? (data_auth *)array_match_key_prefix(p->conf.auth_require, &r->uri.path) 6757c7f8c46SGlenn Strauss : (data_auth *)array_match_key_prefix_nc(p->conf.auth_require, &r->uri.path); 676d61f3381SGlenn Strauss if (NULL == dauth) return HANDLER_GO_ON; 6777ba06c71SGlenn Strauss 678d61f3381SGlenn Strauss { 6797ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme; 680f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) { 6817c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 6823dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(dauth->require, vb->ptr, NULL, NULL)) { 683f54d628cSGlenn Strauss return HANDLER_GO_ON; 684f54d628cSGlenn Strauss } 685f54d628cSGlenn Strauss } 6867c7f8c46SGlenn Strauss return scheme->checkfn(r, scheme->p_d, dauth->require, p->conf.auth_backend); 6877ba06c71SGlenn Strauss } 6887ba06c71SGlenn Strauss } 6897ba06c71SGlenn Strauss 690e11514b0SGlenn Strauss 6917ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p); 6927ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) { 6937ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID; 694e2de4e58SGlenn Strauss p->name = "auth"; 6957ba06c71SGlenn Strauss p->init = mod_auth_init; 6967ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults; 697e11514b0SGlenn Strauss p->handle_trigger = mod_auth_periodic; 6987ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler; 6997ba06c71SGlenn Strauss p->cleanup = mod_auth_free; 7007ba06c71SGlenn Strauss 70181b2d1f0SGlenn Strauss return 0; 70281b2d1f0SGlenn Strauss } 70381b2d1f0SGlenn Strauss 70481b2d1f0SGlenn Strauss 70581b2d1f0SGlenn Strauss 70681b2d1f0SGlenn Strauss 7077ba06c71SGlenn Strauss /* 7087ba06c71SGlenn Strauss * auth schemes (basic, digest, extern) 7097ba06c71SGlenn Strauss * 7107ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs) 7117ba06c71SGlenn Strauss */ 71281b2d1f0SGlenn Strauss 7131fc8a3e1SGlenn Strauss #include "sys-crypto-md.h" 7147ba06c71SGlenn Strauss #include "base64.h" 7157f4e156eSGlenn Strauss #include "rand.h" 7163dd3cde9SGlenn Strauss #include "http_header.h" 71781b2d1f0SGlenn Strauss 718010c2894SGlenn Strauss __attribute_cold__ 7197c7f8c46SGlenn Strauss static handler_t mod_auth_send_400_bad_request(request_st * const r) { 7204b3a91e6SGlenn Strauss 7214b3a91e6SGlenn Strauss /* a field was missing or invalid */ 7227c7f8c46SGlenn Strauss r->http_status = 400; /* Bad Request */ 7237c7f8c46SGlenn Strauss r->handler_module = NULL; 7244b3a91e6SGlenn Strauss 7254b3a91e6SGlenn Strauss return HANDLER_FINISHED; 7264b3a91e6SGlenn Strauss } 7274b3a91e6SGlenn Strauss 7287c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(request_st * const r, const buffer * const realm) { 7297c7f8c46SGlenn Strauss r->http_status = 401; 7307c7f8c46SGlenn Strauss r->handler_module = NULL; 731dc01487eSGlenn Strauss buffer_append_str3( 73226f354cbSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE, 733dc01487eSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")), 734dc01487eSGlenn Strauss CONST_STR_LEN("Basic realm=\""), 735*af3df29aSGlenn Strauss BUF_PTR_LEN(realm), 736dc01487eSGlenn Strauss CONST_STR_LEN("\", charset=\"UTF-8\"")); 7374b3a91e6SGlenn Strauss return HANDLER_FINISHED; 7384b3a91e6SGlenn Strauss } 7394b3a91e6SGlenn Strauss 7407c7f8c46SGlenn Strauss static handler_t mod_auth_check_basic(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) { 7417c7f8c46SGlenn Strauss const buffer *b = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 74281b2d1f0SGlenn Strauss buffer *username; 74381b2d1f0SGlenn Strauss char *pw; 74481b2d1f0SGlenn Strauss 745cf0098eaSGlenn Strauss if (NULL == backend || NULL == backend->basic) { 746cf0098eaSGlenn Strauss if (NULL == backend) 747cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 748cf0098eaSGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr); 749cf0098eaSGlenn Strauss else 750cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 751cf0098eaSGlenn Strauss "auth.require \"method\" => \"basic\" invalid " 752cf0098eaSGlenn Strauss "(try \"digest\"?) for %s", 753cf0098eaSGlenn Strauss r->uri.path.ptr); 7547c7f8c46SGlenn Strauss r->http_status = 500; 7557c7f8c46SGlenn Strauss r->handler_module = NULL; 7567ba06c71SGlenn Strauss return HANDLER_FINISHED; 7577ba06c71SGlenn Strauss } 7587ba06c71SGlenn Strauss 7593dd3cde9SGlenn Strauss if (NULL == b) { 7607c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 7617ba06c71SGlenn Strauss } 7627ba06c71SGlenn Strauss 76313008156SGlenn Strauss if (!buffer_eq_icase_ssn(b->ptr, CONST_STR_LEN("Basic "))) { 7647c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_basic(r, require->realm); 7654b3a91e6SGlenn Strauss } 7669bc61f16SGlenn Strauss #ifdef __COVERITY__ 767*af3df29aSGlenn Strauss if (buffer_clen(b) < sizeof("Basic ")-1) { 7687c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 7699bc61f16SGlenn Strauss } 7709bc61f16SGlenn Strauss #endif 77181b2d1f0SGlenn Strauss 77281b2d1f0SGlenn Strauss username = buffer_init(); 77381b2d1f0SGlenn Strauss 7748047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 775*af3df29aSGlenn Strauss if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_clen(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) { 7767c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "decoding base64-string failed %s", b->ptr+sizeof("Basic ")-1); 77781b2d1f0SGlenn Strauss 77881b2d1f0SGlenn Strauss buffer_free(username); 7797c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 78081b2d1f0SGlenn Strauss } 78181b2d1f0SGlenn Strauss 78281b2d1f0SGlenn Strauss /* r2 == user:password */ 78381b2d1f0SGlenn Strauss if (NULL == (pw = strchr(username->ptr, ':'))) { 7847c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, "missing ':' in %s", username->ptr); 78581b2d1f0SGlenn Strauss 78681b2d1f0SGlenn Strauss buffer_free(username); 7877c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 78881b2d1f0SGlenn Strauss } 78981b2d1f0SGlenn Strauss 790*af3df29aSGlenn Strauss uint32_t pwlen = buffer_clen(username); 791*af3df29aSGlenn Strauss buffer_truncate(username, pw - username->ptr); 79281b2d1f0SGlenn Strauss pw++; 793e11514b0SGlenn Strauss pwlen -= (pw - username->ptr); 79481b2d1f0SGlenn Strauss 795e11514b0SGlenn Strauss plugin_data * const p = p_d; 796e11514b0SGlenn Strauss splay_tree ** sptree = p->conf.auth_cache 797e11514b0SGlenn Strauss ? &p->conf.auth_cache->sptree 798e11514b0SGlenn Strauss : NULL; 799e11514b0SGlenn Strauss http_auth_cache_entry *ae = NULL; 800fd0cc468SGlenn Strauss handler_t rc = HANDLER_ERROR; 801e11514b0SGlenn Strauss int ndx = -1; 802e11514b0SGlenn Strauss if (sptree) { 803*af3df29aSGlenn Strauss ndx = http_auth_cache_hash(require, BUF_PTR_LEN(username)); 804e11514b0SGlenn Strauss ae = http_auth_cache_query(sptree, ndx); 805e11514b0SGlenn Strauss if (ae && ae->require == require 806e11514b0SGlenn Strauss && buffer_is_equal_string(username, ae->username, ae->ulen)) 80762ccda85SGlenn Strauss rc = ck_memeq_const_time(ae->pwdigest, ae->dlen, pw, pwlen) 808e11514b0SGlenn Strauss ? HANDLER_GO_ON 809e11514b0SGlenn Strauss : HANDLER_ERROR; 810e11514b0SGlenn Strauss else /*(not found or hash collision)*/ 811e11514b0SGlenn Strauss ae = NULL; 812e11514b0SGlenn Strauss } 813e11514b0SGlenn Strauss 814fd0cc468SGlenn Strauss if (NULL == ae) 8157c7f8c46SGlenn Strauss rc = backend->basic(r, backend->p_d, require, username, pw); 816e11514b0SGlenn Strauss 8177ba06c71SGlenn Strauss switch (rc) { 8184b3a91e6SGlenn Strauss case HANDLER_GO_ON: 819*af3df29aSGlenn Strauss http_auth_setenv(r, BUF_PTR_LEN(username), CONST_STR_LEN("Basic")); 820e11514b0SGlenn Strauss if (sptree && NULL == ae) { /*(cache (new) successful result)*/ 821*af3df29aSGlenn Strauss ae = http_auth_cache_entry_init(require, 0, BUF_PTR_LEN(username), 822e11514b0SGlenn Strauss pw, pwlen); 823e11514b0SGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free); 824e11514b0SGlenn Strauss } 8254b3a91e6SGlenn Strauss break; 8264b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 8274b3a91e6SGlenn Strauss case HANDLER_FINISHED: 8287ba06c71SGlenn Strauss break; 8294b3a91e6SGlenn Strauss case HANDLER_ERROR: 8304b3a91e6SGlenn Strauss default: 8317c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 832010c2894SGlenn Strauss "password doesn't match for %s username: %s IP: %s", 8337c7f8c46SGlenn Strauss r->uri.path.ptr, username->ptr, r->con->dst_addr_buf->ptr); 8344a600dabSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/ 835fd0cc468SGlenn Strauss rc = mod_auth_send_401_unauthorized_basic(r, require->realm); 8367ba06c71SGlenn Strauss break; 83781b2d1f0SGlenn Strauss } 83881b2d1f0SGlenn Strauss 83908c03cd4SGlenn Strauss ck_memzero(pw, pwlen); 8404b3a91e6SGlenn Strauss buffer_free(username); 841fd0cc468SGlenn Strauss return rc; 84281b2d1f0SGlenn Strauss } 84381b2d1f0SGlenn Strauss 84481b2d1f0SGlenn Strauss 845b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 846b9e2be50SGlenn Strauss 847b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_sha256(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 848b9e2be50SGlenn Strauss SHA256_CTX ctx; 849b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 850b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 851b9e2be50SGlenn Strauss 852b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 853b9e2be50SGlenn Strauss SHA256_Init(&ctx); 854b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 855b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 856b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 857b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 858b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 859b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 860b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 861b9e2be50SGlenn Strauss } 862b9e2be50SGlenn Strauss 863b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 864b9e2be50SGlenn Strauss 865b9e2be50SGlenn Strauss /* calculate H(A2) */ 866b9e2be50SGlenn Strauss SHA256_Init(&ctx); 867b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)m, strlen(m)); 868b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 869b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 870b9e2be50SGlenn Strauss #if 0 871b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 872fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 873b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 874b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 875b9e2be50SGlenn Strauss } 876b9e2be50SGlenn Strauss #endif 877b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 878b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 879b9e2be50SGlenn Strauss 880b9e2be50SGlenn Strauss /* calculate response */ 881b9e2be50SGlenn Strauss SHA256_Init(&ctx); 882b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 883b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 884b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 885b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 886b9e2be50SGlenn Strauss if (qop && *qop) { 887b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 888b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 889b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 890b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 891b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 892b9e2be50SGlenn Strauss SHA256_Update(&ctx, CONST_STR_LEN(":")); 893b9e2be50SGlenn Strauss } 894b9e2be50SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 895b9e2be50SGlenn Strauss SHA256_Final(ai->digest, &ctx); 896b9e2be50SGlenn Strauss } 897b9e2be50SGlenn Strauss 898e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) { 899b9e2be50SGlenn Strauss SHA256_CTX ctx; 900f24e6d69SGlenn Strauss size_t len; 901b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 902b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA256_BINLEN*2+1]; 903b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 904b9e2be50SGlenn Strauss SHA256_Init(&ctx); 905f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 906f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 907f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 908f24e6d69SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)hh, len); 909e1188e77SGlenn Strauss if (secret) { 910*af3df29aSGlenn Strauss len = buffer_clen(secret); 911e1188e77SGlenn Strauss SHA256_Update(&ctx, (unsigned char *)secret->ptr, len); 912e1188e77SGlenn Strauss } 913b9e2be50SGlenn Strauss SHA256_Final(h, &ctx); 914b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 915b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 916b9e2be50SGlenn Strauss } 917b9e2be50SGlenn Strauss 9181fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 919b9e2be50SGlenn Strauss 920b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_sha512_256(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 921b9e2be50SGlenn Strauss SHA512_CTX ctx; 922b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 923b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 924b9e2be50SGlenn Strauss 925b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 926b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 927b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 928b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 929b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 930b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 931b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 932b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 933b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 934b9e2be50SGlenn Strauss } 935b9e2be50SGlenn Strauss 936b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 937b9e2be50SGlenn Strauss 938b9e2be50SGlenn Strauss /* calculate H(A2) */ 939b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 940b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)m, strlen(m)); 941b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 942b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)uri, strlen(uri)); 943b9e2be50SGlenn Strauss #if 0 944b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 945fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 946b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 947b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)[body checksum], ai->dlen*2); 948b9e2be50SGlenn Strauss } 949b9e2be50SGlenn Strauss #endif 950b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 951b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 952b9e2be50SGlenn Strauss 953b9e2be50SGlenn Strauss /* calculate response */ 954b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 955b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 956b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 957b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 958b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 959b9e2be50SGlenn Strauss if (qop && *qop) { 960b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)nc, strlen(nc)); 961b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 962b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 963b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 964b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)qop, strlen(qop)); 965b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, CONST_STR_LEN(":")); 966b9e2be50SGlenn Strauss } 967b9e2be50SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 968b9e2be50SGlenn Strauss SHA512_256_Final(ai->digest, &ctx); 969b9e2be50SGlenn Strauss } 970b9e2be50SGlenn Strauss 971e1188e77SGlenn Strauss static void mod_auth_digest_nonce_sha512_256(buffer *b, time_t cur_ts, int rnd, const buffer *secret) { 972b9e2be50SGlenn Strauss SHA512_CTX ctx; 973f24e6d69SGlenn Strauss size_t len; 974b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_SHA512_256_BINLEN]; 975b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_SHA512_256_BINLEN*2+1]; 976b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 977b9e2be50SGlenn Strauss SHA512_256_Init(&ctx); 978f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 979f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 980f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 981f24e6d69SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)hh, len); 982e1188e77SGlenn Strauss if (secret) { 983*af3df29aSGlenn Strauss len = buffer_clen(secret); 984e1188e77SGlenn Strauss SHA512_256_Update(&ctx, (unsigned char *)secret->ptr, len); 985e1188e77SGlenn Strauss } 986b9e2be50SGlenn Strauss SHA512_256_Final(h, &ctx); 987b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 988b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 989b9e2be50SGlenn Strauss } 990b9e2be50SGlenn Strauss 9911fc8a3e1SGlenn Strauss #endif /* USE_LIB_CRYPTO_SHA512_256 */ 992b9e2be50SGlenn Strauss 993b28a3714SGlenn Strauss #endif /* USE_LIB_CRYPTO */ 994b9e2be50SGlenn Strauss 995b9e2be50SGlenn Strauss static void mod_auth_digest_mutate_md5(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 996b9e2be50SGlenn Strauss li_MD5_CTX ctx; 997b9e2be50SGlenn Strauss char a1[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 998b9e2be50SGlenn Strauss char a2[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 999b9e2be50SGlenn Strauss 1000b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_SESS) { 1001b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 1002b9e2be50SGlenn Strauss /* http://www.rfc-editor.org/errata_search.php?rfc=2617 1003b9e2be50SGlenn Strauss * Errata ID: 1649 */ 1004b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 1005b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 1006b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1007b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 1008b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1009b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 1010b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 1011b9e2be50SGlenn Strauss } 1012b9e2be50SGlenn Strauss 1013b9e2be50SGlenn Strauss li_tohex(a1, sizeof(a1), (const char *)ai->digest, ai->dlen); 1014b9e2be50SGlenn Strauss 1015b9e2be50SGlenn Strauss /* calculate H(A2) */ 1016b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 1017b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)m, strlen(m)); 1018b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1019b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)uri, strlen(uri)); 1020b9e2be50SGlenn Strauss #if 0 1021b9e2be50SGlenn Strauss /* qop=auth-int not supported, already checked in caller */ 1022fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 1023b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1024b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *) [body checksum], ai->dlen*2); 1025b9e2be50SGlenn Strauss } 1026b9e2be50SGlenn Strauss #endif 1027b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 1028b9e2be50SGlenn Strauss li_tohex(a2, sizeof(a2), (const char *)ai->digest, ai->dlen); 1029b9e2be50SGlenn Strauss 1030b9e2be50SGlenn Strauss /* calculate response */ 1031b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 1032b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a1, sizeof(a1)-1); 1033b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1034b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nonce, strlen(nonce)); 1035b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1036b9e2be50SGlenn Strauss if (qop && *qop) { 1037b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)nc, strlen(nc)); 1038b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1039b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)cnonce, strlen(cnonce)); 1040b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1041b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)qop, strlen(qop)); 1042b9e2be50SGlenn Strauss li_MD5_Update(&ctx, CONST_STR_LEN(":")); 1043b9e2be50SGlenn Strauss } 1044b9e2be50SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)a2, sizeof(a2)-1); 1045b9e2be50SGlenn Strauss li_MD5_Final(ai->digest, &ctx); 1046b9e2be50SGlenn Strauss } 1047b9e2be50SGlenn Strauss 1048e1188e77SGlenn Strauss static void mod_auth_digest_nonce_md5(buffer *b, time_t cur_ts, int rnd, const buffer *secret) { 1049b9e2be50SGlenn Strauss li_MD5_CTX ctx; 1050f24e6d69SGlenn Strauss size_t len; 1051b9e2be50SGlenn Strauss unsigned char h[HTTP_AUTH_DIGEST_MD5_BINLEN]; 1052b9e2be50SGlenn Strauss char hh[HTTP_AUTH_DIGEST_MD5_BINLEN*2+1]; 1053b9e2be50SGlenn Strauss force_assert(sizeof(hh) >= LI_ITOSTRING_LENGTH); 1054b9e2be50SGlenn Strauss li_MD5_Init(&ctx); 1055f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), cur_ts); 1056f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 1057f24e6d69SGlenn Strauss len = li_itostrn(hh, sizeof(hh), rnd); 1058f24e6d69SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)hh, len); 1059e1188e77SGlenn Strauss if (secret) { 1060*af3df29aSGlenn Strauss len = buffer_clen(secret); 1061e1188e77SGlenn Strauss li_MD5_Update(&ctx, (unsigned char *)secret->ptr, len); 1062e1188e77SGlenn Strauss } 1063b9e2be50SGlenn Strauss li_MD5_Final(h, &ctx); 1064b9e2be50SGlenn Strauss li_tohex(hh, sizeof(hh), (const char *)h, sizeof(h)); 1065b9e2be50SGlenn Strauss buffer_append_string_len(b, hh, sizeof(hh)-1); 1066b9e2be50SGlenn Strauss } 1067b9e2be50SGlenn Strauss 1068b9e2be50SGlenn Strauss static void mod_auth_digest_mutate(http_auth_info_t *ai, const char *m, const char *uri, const char *nonce, const char *cnonce, const char *nc, const char *qop) { 1069b9e2be50SGlenn Strauss if (ai->dalgo & HTTP_AUTH_DIGEST_MD5) 1070b9e2be50SGlenn Strauss mod_auth_digest_mutate_md5(ai, m, uri, nonce, cnonce, nc, qop); 1071b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 1072b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA256) 1073b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha256(ai, m, uri, nonce, cnonce, nc, qop); 10741fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 1075b9e2be50SGlenn Strauss else if (ai->dalgo & HTTP_AUTH_DIGEST_SHA512_256) 1076b9e2be50SGlenn Strauss mod_auth_digest_mutate_sha512_256(ai, m, uri, nonce, cnonce, nc, qop); 1077b9e2be50SGlenn Strauss #endif 1078b9e2be50SGlenn Strauss #endif 1079b9e2be50SGlenn Strauss } 1080b9e2be50SGlenn Strauss 1081e1188e77SGlenn Strauss static void mod_auth_append_nonce(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo, int *rndptr) { 1082e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)cur_ts); 1083e1188e77SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 1084e1188e77SGlenn Strauss const buffer * const nonce_secret = require->nonce_secret; 1085e1188e77SGlenn Strauss int rnd; 1086e1188e77SGlenn Strauss if (NULL == nonce_secret) 1087e1188e77SGlenn Strauss rnd = rndptr ? *rndptr : li_rand_pseudo(); 1088e1188e77SGlenn Strauss else { /*(do not directly expose random number generator single value)*/ 1089e1188e77SGlenn Strauss rndptr 1090e1188e77SGlenn Strauss ? (void)(rnd = *rndptr) 1091e1188e77SGlenn Strauss : li_rand_pseudo_bytes((unsigned char *)&rnd, sizeof(rnd)); 1092e1188e77SGlenn Strauss buffer_append_uint_hex(b, (uintmax_t)rnd); 1093e1188e77SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(":")); 1094e1188e77SGlenn Strauss } 1095e1188e77SGlenn Strauss switch (dalgo) { 1096b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 10971fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 1098e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA512_256: 1099e1188e77SGlenn Strauss mod_auth_digest_nonce_sha512_256(b, cur_ts, rnd, nonce_secret); 1100e1188e77SGlenn Strauss break; 1101e1188e77SGlenn Strauss #endif 1102e1188e77SGlenn Strauss case HTTP_AUTH_DIGEST_SHA256: 1103e1188e77SGlenn Strauss mod_auth_digest_nonce_sha256(b, cur_ts, rnd, nonce_secret); 1104e1188e77SGlenn Strauss break; 1105e1188e77SGlenn Strauss #endif 1106e1188e77SGlenn Strauss /*case HTTP_AUTH_DIGEST_MD5:*/ 1107e1188e77SGlenn Strauss default: 1108e1188e77SGlenn Strauss mod_auth_digest_nonce_md5(b, cur_ts, rnd, nonce_secret); 1109e1188e77SGlenn Strauss break; 1110e1188e77SGlenn Strauss } 1111e1188e77SGlenn Strauss } 1112e1188e77SGlenn Strauss 1113b9e2be50SGlenn Strauss static void mod_auth_digest_www_authenticate(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int nonce_stale) { 1114b9e2be50SGlenn Strauss int algos = nonce_stale ? nonce_stale : require->algorithm; 1115b9e2be50SGlenn Strauss int n = 0; 1116e1188e77SGlenn Strauss int algoid[3]; 1117b9e2be50SGlenn Strauss unsigned int algolen[3]; 1118b9e2be50SGlenn Strauss const char *algoname[3]; 1119b28a3714SGlenn Strauss #ifdef USE_LIB_CRYPTO 11201fc8a3e1SGlenn Strauss #ifdef USE_LIB_CRYPTO_SHA512_256 1121b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA512_256) { 1122e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA512_256; 1123b9e2be50SGlenn Strauss algoname[n] = "SHA-512-256"; 1124b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-512-256")-1; 1125b9e2be50SGlenn Strauss ++n; 1126b9e2be50SGlenn Strauss } 1127b9e2be50SGlenn Strauss #endif 1128b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_SHA256) { 1129e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_SHA256; 1130b9e2be50SGlenn Strauss algoname[n] = "SHA-256"; 1131b9e2be50SGlenn Strauss algolen[n] = sizeof("SHA-256")-1; 1132b9e2be50SGlenn Strauss ++n; 1133b9e2be50SGlenn Strauss } 1134b9e2be50SGlenn Strauss #endif 1135b9e2be50SGlenn Strauss if (algos & HTTP_AUTH_DIGEST_MD5) { 1136e1188e77SGlenn Strauss algoid[n] = HTTP_AUTH_DIGEST_MD5; 1137b9e2be50SGlenn Strauss algoname[n] = "MD5"; 1138b9e2be50SGlenn Strauss algolen[n] = sizeof("MD5")-1; 1139b9e2be50SGlenn Strauss ++n; 1140b9e2be50SGlenn Strauss } 1141b9e2be50SGlenn Strauss 1142b9e2be50SGlenn Strauss buffer_clear(b); 1143b9e2be50SGlenn Strauss for (int i = 0; i < n; ++i) { 1144dc01487eSGlenn Strauss struct const_iovec iov[] = { 1145dc01487eSGlenn Strauss { CONST_STR_LEN("\r\nWWW-Authenticate: ") } 1146dc01487eSGlenn Strauss ,{ CONST_STR_LEN("Digest realm=\"") } 1147*af3df29aSGlenn Strauss ,{ BUF_PTR_LEN(require->realm) } 1148dc01487eSGlenn Strauss ,{ CONST_STR_LEN("\", charset=\"UTF-8\", algorithm=") } 1149dc01487eSGlenn Strauss ,{ algoname[i], algolen[i] } 1150dc01487eSGlenn Strauss ,{ CONST_STR_LEN(", nonce=\"") } 1151dc01487eSGlenn Strauss }; 1152dc01487eSGlenn Strauss buffer_append_iovec(b, iov+(0==i), sizeof(iov)/sizeof(*iov)-(0==i)); 1153e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, algoid[i], NULL); 1154b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\", qop=\"auth\"")); 1155b9e2be50SGlenn Strauss if (nonce_stale) { 1156b9e2be50SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN(", stale=true")); 1157b9e2be50SGlenn Strauss } 1158b9e2be50SGlenn Strauss } 115981b2d1f0SGlenn Strauss } 116081b2d1f0SGlenn Strauss 1161e1188e77SGlenn Strauss static void mod_auth_digest_authentication_info(buffer *b, time_t cur_ts, const struct http_auth_require_t *require, int dalgo) { 11626ad325c6SGlenn Strauss buffer_clear(b); 11636ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("nextnonce=\"")); 1164e1188e77SGlenn Strauss mod_auth_append_nonce(b, cur_ts, require, dalgo, NULL); 11656ad325c6SGlenn Strauss buffer_append_string_len(b, CONST_STR_LEN("\"")); 11666ad325c6SGlenn Strauss } 11676ad325c6SGlenn Strauss 116881b2d1f0SGlenn Strauss typedef struct { 116981b2d1f0SGlenn Strauss const char *key; 117081b2d1f0SGlenn Strauss int key_len; 117181b2d1f0SGlenn Strauss char **ptr; 117281b2d1f0SGlenn Strauss } digest_kv; 117381b2d1f0SGlenn Strauss 11747c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st *r, const struct http_auth_require_t *require, int nonce_stale); 11757ba06c71SGlenn Strauss 11767c7f8c46SGlenn Strauss static handler_t mod_auth_check_digest(request_st * const r, void *p_d, const struct http_auth_require_t * const require, const struct http_auth_backend_t * const backend) { 11777c7f8c46SGlenn Strauss const buffer *vb = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization")); 11787ba06c71SGlenn Strauss 117981b2d1f0SGlenn Strauss char *username = NULL; 118081b2d1f0SGlenn Strauss char *realm = NULL; 118181b2d1f0SGlenn Strauss char *nonce = NULL; 118281b2d1f0SGlenn Strauss char *uri = NULL; 118381b2d1f0SGlenn Strauss char *algorithm = NULL; 118481b2d1f0SGlenn Strauss char *qop = NULL; 118581b2d1f0SGlenn Strauss char *cnonce = NULL; 118681b2d1f0SGlenn Strauss char *nc = NULL; 118781b2d1f0SGlenn Strauss char *respons = NULL; 118881b2d1f0SGlenn Strauss 118981b2d1f0SGlenn Strauss char *e, *c; 119081b2d1f0SGlenn Strauss int i; 119181b2d1f0SGlenn Strauss buffer *b; 119260f4cf3aSGlenn Strauss http_auth_info_t ai; 1193b9e2be50SGlenn Strauss unsigned char rdigest[HTTP_AUTH_DIGEST_SHA256_BINLEN]; 119481b2d1f0SGlenn Strauss 119581b2d1f0SGlenn Strauss 119681b2d1f0SGlenn Strauss /* init pointers */ 119781b2d1f0SGlenn Strauss #define S(x) \ 119881b2d1f0SGlenn Strauss x, sizeof(x)-1, NULL 119981b2d1f0SGlenn Strauss digest_kv dkv[10] = { 120081b2d1f0SGlenn Strauss { S("username=") }, 120181b2d1f0SGlenn Strauss { S("realm=") }, 120281b2d1f0SGlenn Strauss { S("nonce=") }, 120381b2d1f0SGlenn Strauss { S("uri=") }, 120481b2d1f0SGlenn Strauss { S("algorithm=") }, 120581b2d1f0SGlenn Strauss { S("qop=") }, 120681b2d1f0SGlenn Strauss { S("cnonce=") }, 120781b2d1f0SGlenn Strauss { S("nc=") }, 120881b2d1f0SGlenn Strauss { S("response=") }, 120981b2d1f0SGlenn Strauss 121081b2d1f0SGlenn Strauss { NULL, 0, NULL } 121181b2d1f0SGlenn Strauss }; 121281b2d1f0SGlenn Strauss #undef S 121381b2d1f0SGlenn Strauss 121481b2d1f0SGlenn Strauss dkv[0].ptr = &username; 121581b2d1f0SGlenn Strauss dkv[1].ptr = &realm; 121681b2d1f0SGlenn Strauss dkv[2].ptr = &nonce; 121781b2d1f0SGlenn Strauss dkv[3].ptr = &uri; 121881b2d1f0SGlenn Strauss dkv[4].ptr = &algorithm; 121981b2d1f0SGlenn Strauss dkv[5].ptr = &qop; 122081b2d1f0SGlenn Strauss dkv[6].ptr = &cnonce; 122181b2d1f0SGlenn Strauss dkv[7].ptr = &nc; 122281b2d1f0SGlenn Strauss dkv[8].ptr = &respons; 122381b2d1f0SGlenn Strauss 1224cf0098eaSGlenn Strauss if (NULL == backend || NULL == backend->digest) { 1225cf0098eaSGlenn Strauss if (NULL == backend) 12267c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 12277c7f8c46SGlenn Strauss "auth.backend not configured for %s", r->uri.path.ptr); 1228cf0098eaSGlenn Strauss else 1229cf0098eaSGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1230cf0098eaSGlenn Strauss "auth.require \"method\" => \"digest\" invalid " 1231cf0098eaSGlenn Strauss "(try \"basic\"?) for %s", 1232cf0098eaSGlenn Strauss r->uri.path.ptr); 12337c7f8c46SGlenn Strauss r->http_status = 500; 12347c7f8c46SGlenn Strauss r->handler_module = NULL; 12357ba06c71SGlenn Strauss return HANDLER_FINISHED; 12367ba06c71SGlenn Strauss } 12377ba06c71SGlenn Strauss 12383dd3cde9SGlenn Strauss if (NULL == vb) { 12397c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 12407ba06c71SGlenn Strauss } 12417ba06c71SGlenn Strauss 124213008156SGlenn Strauss if (!buffer_eq_icase_ssn(vb->ptr, CONST_STR_LEN("Digest "))) { 12437c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1244dadfb5fcSGlenn Strauss } else { 1245*af3df29aSGlenn Strauss size_t n = buffer_clen(vb); 12469bc61f16SGlenn Strauss #ifdef __COVERITY__ 1247dadfb5fcSGlenn Strauss if (n < sizeof("Digest ")-1) { 12487c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 12499bc61f16SGlenn Strauss } 12509bc61f16SGlenn Strauss #endif 1251dadfb5fcSGlenn Strauss n -= (sizeof("Digest ")-1); 12527ba06c71SGlenn Strauss b = buffer_init(); 12533dd3cde9SGlenn Strauss buffer_copy_string_len(b,vb->ptr+sizeof("Digest ")-1,n); 1254dadfb5fcSGlenn Strauss } 125581b2d1f0SGlenn Strauss 125681b2d1f0SGlenn Strauss /* parse credentials from client */ 125781b2d1f0SGlenn Strauss for (c = b->ptr; *c; c++) { 125881b2d1f0SGlenn Strauss /* skip whitespaces */ 125981b2d1f0SGlenn Strauss while (*c == ' ' || *c == '\t') c++; 126081b2d1f0SGlenn Strauss if (!*c) break; 126181b2d1f0SGlenn Strauss 126281b2d1f0SGlenn Strauss for (i = 0; dkv[i].key; i++) { 126381b2d1f0SGlenn Strauss if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) { 126481b2d1f0SGlenn Strauss if ((c[dkv[i].key_len] == '"') && 126581b2d1f0SGlenn Strauss (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) { 126681b2d1f0SGlenn Strauss /* value with "..." */ 126781b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len + 1; 126881b2d1f0SGlenn Strauss c = e; 126981b2d1f0SGlenn Strauss 127081b2d1f0SGlenn Strauss *e = '\0'; 127181b2d1f0SGlenn Strauss } else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) { 127281b2d1f0SGlenn Strauss /* value without "...", terminated by ',' */ 127381b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 127481b2d1f0SGlenn Strauss c = e; 127581b2d1f0SGlenn Strauss 127681b2d1f0SGlenn Strauss *e = '\0'; 127781b2d1f0SGlenn Strauss } else { 127881b2d1f0SGlenn Strauss /* value without "...", terminated by EOL */ 127981b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 128081b2d1f0SGlenn Strauss c += strlen(c) - 1; 128181b2d1f0SGlenn Strauss } 128281b2d1f0SGlenn Strauss break; 128381b2d1f0SGlenn Strauss } 128481b2d1f0SGlenn Strauss } 128581b2d1f0SGlenn Strauss } 128681b2d1f0SGlenn Strauss 128781b2d1f0SGlenn Strauss /* check if everything is transmitted */ 128881b2d1f0SGlenn Strauss if (!username || 128981b2d1f0SGlenn Strauss !realm || 129081b2d1f0SGlenn Strauss !nonce || 129181b2d1f0SGlenn Strauss !uri || 129281b2d1f0SGlenn Strauss (qop && (!nc || !cnonce)) || 129381b2d1f0SGlenn Strauss !respons ) { 129481b2d1f0SGlenn Strauss /* missing field */ 129581b2d1f0SGlenn Strauss 12967c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 129781b2d1f0SGlenn Strauss "digest: missing field"); 129881b2d1f0SGlenn Strauss 129981b2d1f0SGlenn Strauss buffer_free(b); 13007c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 130181b2d1f0SGlenn Strauss } 130281b2d1f0SGlenn Strauss 130360f4cf3aSGlenn Strauss ai.username = username; 130460f4cf3aSGlenn Strauss ai.ulen = strlen(username); 130560f4cf3aSGlenn Strauss ai.realm = realm; 130660f4cf3aSGlenn Strauss ai.rlen = strlen(realm); 130760f4cf3aSGlenn Strauss 130860f4cf3aSGlenn Strauss if (!buffer_is_equal_string(require->realm, ai.realm, ai.rlen)) { 13097c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 13105a6e4df8SGlenn Strauss "digest: realm mismatch"); 13115a6e4df8SGlenn Strauss buffer_free(b); 13127c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 13135a6e4df8SGlenn Strauss } 13145a6e4df8SGlenn Strauss 1315b9e2be50SGlenn Strauss if (!mod_auth_algorithm_parse(&ai, algorithm) 1316b9e2be50SGlenn Strauss || !(require->algorithm & ai.dalgo & ~HTTP_AUTH_DIGEST_SESS)) { 13177c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1318010c2894SGlenn Strauss "digest: (%s): invalid", algorithm); 1319b9e2be50SGlenn Strauss buffer_free(b); 132012ac70d0SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1321b9e2be50SGlenn Strauss } 1322b9e2be50SGlenn Strauss 132381b2d1f0SGlenn Strauss /** 132481b2d1f0SGlenn Strauss * protect the md5-sess against missing cnonce and nonce 132581b2d1f0SGlenn Strauss */ 1326b9e2be50SGlenn Strauss if ((ai.dalgo & HTTP_AUTH_DIGEST_SESS) && (!nonce || !cnonce)) { 13277c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1328010c2894SGlenn Strauss "digest: (%s): missing field", algorithm); 132981b2d1f0SGlenn Strauss 133081b2d1f0SGlenn Strauss buffer_free(b); 13317c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 133281b2d1f0SGlenn Strauss } 133381b2d1f0SGlenn Strauss 1334b9e2be50SGlenn Strauss { 1335b9e2be50SGlenn Strauss size_t resplen = strlen(respons); 1336e110b062SGlenn Strauss if (0 != li_hex2bin(rdigest, sizeof(rdigest), respons, resplen) 1337b9e2be50SGlenn Strauss || resplen != (ai.dlen << 1)) { 13387c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1339010c2894SGlenn Strauss "digest: (%s): invalid format", respons); 1340b9e2be50SGlenn Strauss buffer_free(b); 13417c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 1342b9e2be50SGlenn Strauss } 1343b9e2be50SGlenn Strauss } 1344b9e2be50SGlenn Strauss 1345fc42ec12SGlenn Strauss if (qop && buffer_eq_icase_ss(qop, strlen(qop), CONST_STR_LEN("auth-int"))){ 13467c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 134781b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 134881b2d1f0SGlenn Strauss 134981b2d1f0SGlenn Strauss buffer_free(b); 13507c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 135181b2d1f0SGlenn Strauss } 135281b2d1f0SGlenn Strauss 135381b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 135481b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 135581b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 135681b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 135781b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 135881b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 13597c7f8c46SGlenn Strauss * uri validation step is bypassed. r->target_orig is 13608131e439SGlenn Strauss * original request-target sent in client request. */ 136181b2d1f0SGlenn Strauss { 136281b2d1f0SGlenn Strauss const size_t ulen = strlen(uri); 13637c7f8c46SGlenn Strauss if (!buffer_is_equal_string(&r->target_orig, uri, ulen)) { 13647c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1365010c2894SGlenn Strauss "digest: auth failed: uri mismatch (%s != %s), IP: %s", 13667c7f8c46SGlenn Strauss r->target_orig.ptr, uri, r->con->dst_addr_buf->ptr); 136781b2d1f0SGlenn Strauss buffer_free(b); 13687c7f8c46SGlenn Strauss return mod_auth_send_400_bad_request(r); 136981b2d1f0SGlenn Strauss } 137081b2d1f0SGlenn Strauss } 137181b2d1f0SGlenn Strauss 1372e1188e77SGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 1373e1188e77SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 1374e1188e77SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 1375e1188e77SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 1376e1188e77SGlenn Strauss * verifiable) if nanoseconds were included with seconds as part of the 1377e1188e77SGlenn Strauss * nonce "timestamp:secret". However, doing so would expose a high 1378e1188e77SGlenn Strauss * precision timestamp of the system to attackers. timestamp in nonce 1379e1188e77SGlenn Strauss * could theoretically be modified and still produce same md5sum, but 1380e1188e77SGlenn Strauss * that is highly unlikely within a 10 min (moving) window of valid 1381e1188e77SGlenn Strauss * time relative to current time (now). 1382e1188e77SGlenn Strauss * If it is desired to validate that nonces were generated by server, 1383e1188e77SGlenn Strauss * then specify auth.require = ( ... => ( "secret" => "..." ) ) 1384e1188e77SGlenn Strauss * When secret is specified, then instead of nanoseconds, the random 1385e1188e77SGlenn Strauss * data value (included for unique nonces) will be exposed in the nonce 1386e1188e77SGlenn Strauss * along with the timestamp, and the additional secret will be used to 1387e1188e77SGlenn Strauss * validate that the server generated the nonce using that secret. */ 138826f354cbSGlenn Strauss time_t send_nextnonce; 1389e1188e77SGlenn Strauss { 1390e1188e77SGlenn Strauss time_t ts = 0; 1391e1188e77SGlenn Strauss const unsigned char * const nonce_uns = (unsigned char *)nonce; 1392e1188e77SGlenn Strauss for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) { 13938b52995cSGlenn Strauss ts =(time_t)((uint32_t)ts << 4) | hex2int(nonce_uns[i]); 1394e1188e77SGlenn Strauss } 1395e1188e77SGlenn Strauss const time_t cur_ts = log_epoch_secs; 1396e1188e77SGlenn Strauss if (nonce[i] != ':' 1397e1188e77SGlenn Strauss || ts > cur_ts || cur_ts - ts > 600) { /*(10 mins)*/ 1398e1188e77SGlenn Strauss /* nonce is stale; have client regenerate digest */ 1399e1188e77SGlenn Strauss buffer_free(b); 1400e1188e77SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, ai.dalgo); 1401e1188e77SGlenn Strauss } 1402e1188e77SGlenn Strauss 140326f354cbSGlenn Strauss send_nextnonce = (cur_ts - ts > 540) ? cur_ts : 0; /*(9 mins)*/ 1404e1188e77SGlenn Strauss 1405e1188e77SGlenn Strauss if (require->nonce_secret) { 1406e1188e77SGlenn Strauss unsigned int rnd = 0; 1407e1188e77SGlenn Strauss for (int j = i+8; i < j && light_isxdigit(nonce_uns[i]); ++i) { 1408e1188e77SGlenn Strauss rnd = (rnd << 4) + hex2int(nonce_uns[i]); 1409e1188e77SGlenn Strauss } 1410e1188e77SGlenn Strauss if (nonce[i] != ':') { 1411e1188e77SGlenn Strauss /* nonce is invalid; 1412e1188e77SGlenn Strauss * expect extra field w/ require->nonce_secret */ 1413e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1414e1188e77SGlenn Strauss "digest: nonce invalid"); 1415e1188e77SGlenn Strauss buffer_free(b); 1416e1188e77SGlenn Strauss return mod_auth_send_400_bad_request(r); 1417e1188e77SGlenn Strauss } 1418e1188e77SGlenn Strauss buffer * const tb = r->tmp_buf; 1419e1188e77SGlenn Strauss buffer_clear(tb); 1420e1188e77SGlenn Strauss mod_auth_append_nonce(tb, cur_ts, require, ai.dalgo, (int *)&rnd); 1421e1188e77SGlenn Strauss if (!buffer_eq_slen(tb, nonce, strlen(nonce))) { 1422e1188e77SGlenn Strauss /* nonce not generated using current require->nonce_secret */ 1423e1188e77SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1424e1188e77SGlenn Strauss "digest: nonce mismatch"); 1425e1188e77SGlenn Strauss buffer_free(b); 1426e1188e77SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 1427e1188e77SGlenn Strauss } 1428e1188e77SGlenn Strauss } 1429e1188e77SGlenn Strauss } 1430e1188e77SGlenn Strauss 1431e11514b0SGlenn Strauss plugin_data * const p = p_d; 1432e11514b0SGlenn Strauss splay_tree ** sptree = p->conf.auth_cache 1433e11514b0SGlenn Strauss ? &p->conf.auth_cache->sptree 1434e11514b0SGlenn Strauss : NULL; 1435e11514b0SGlenn Strauss http_auth_cache_entry *ae = NULL; 1436fd0cc468SGlenn Strauss handler_t rc = HANDLER_ERROR; 1437e11514b0SGlenn Strauss int ndx = -1; 1438e11514b0SGlenn Strauss if (sptree) { 1439e11514b0SGlenn Strauss ndx = http_auth_cache_hash(require, ai.username, ai.ulen); 1440e11514b0SGlenn Strauss ae = http_auth_cache_query(sptree, ndx); 1441e11514b0SGlenn Strauss if (ae && ae->require == require 1442e11514b0SGlenn Strauss && ae->dalgo == ai.dalgo 1443e11514b0SGlenn Strauss && ae->dlen == ai.dlen 1444e11514b0SGlenn Strauss && ae->ulen == ai.ulen 1445e11514b0SGlenn Strauss && 0 == memcmp(ae->username, ai.username, ai.ulen)) { 1446e11514b0SGlenn Strauss rc = HANDLER_GO_ON; 1447e11514b0SGlenn Strauss memcpy(ai.digest, ae->pwdigest, ai.dlen); 1448e11514b0SGlenn Strauss } 1449e11514b0SGlenn Strauss else /*(not found or hash collision)*/ 1450e11514b0SGlenn Strauss ae = NULL; 1451e11514b0SGlenn Strauss } 1452e11514b0SGlenn Strauss 1453fd0cc468SGlenn Strauss if (NULL == ae) 1454e11514b0SGlenn Strauss rc = backend->digest(r, backend->p_d, &ai); 1455e11514b0SGlenn Strauss 1456e11514b0SGlenn Strauss switch (rc) { 14574b3a91e6SGlenn Strauss case HANDLER_GO_ON: 14584b3a91e6SGlenn Strauss break; 14594b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 146081b2d1f0SGlenn Strauss buffer_free(b); 14614b3a91e6SGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 14624b3a91e6SGlenn Strauss case HANDLER_FINISHED: 14634b3a91e6SGlenn Strauss buffer_free(b); 14644b3a91e6SGlenn Strauss return HANDLER_FINISHED; 14654b3a91e6SGlenn Strauss case HANDLER_ERROR: 14664b3a91e6SGlenn Strauss default: 14674a600dabSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if unknown user)*/ 14684b3a91e6SGlenn Strauss buffer_free(b); 14697c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 147081b2d1f0SGlenn Strauss } 147181b2d1f0SGlenn Strauss 1472e11514b0SGlenn Strauss if (sptree && NULL == ae) { /*(cache digest from backend)*/ 1473e11514b0SGlenn Strauss ae = http_auth_cache_entry_init(require, ai.dalgo, ai.username, ai.ulen, 1474e11514b0SGlenn Strauss (char *)ai.digest, ai.dlen); 1475e11514b0SGlenn Strauss http_auth_cache_insert(sptree, ndx, ae, http_auth_cache_entry_free); 1476e11514b0SGlenn Strauss } 1477e11514b0SGlenn Strauss 1478e1188e77SGlenn Strauss const char *m = get_http_method_name(r->http_method); 1479e1188e77SGlenn Strauss force_assert(m); 1480e1188e77SGlenn Strauss 1481b9e2be50SGlenn Strauss mod_auth_digest_mutate(&ai,m,uri,nonce,cnonce,nc,qop); 148281b2d1f0SGlenn Strauss 148362ccda85SGlenn Strauss if (!ck_memeq_const_time_fixed_len(rdigest, ai.digest, ai.dlen)) { 148408c03cd4SGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//*skip clear since mutated*/ 148581b2d1f0SGlenn Strauss /* digest not ok */ 14867c7f8c46SGlenn Strauss log_error(r->conf.errh, __FILE__, __LINE__, 1487010c2894SGlenn Strauss "digest: auth failed for %s: wrong password, IP: %s", 14887c7f8c46SGlenn Strauss username, r->con->dst_addr_buf->ptr); 14894a600dabSGlenn Strauss r->keep_alive = -1; /*(disable keep-alive if bad password)*/ 149081b2d1f0SGlenn Strauss 149181b2d1f0SGlenn Strauss buffer_free(b); 14927c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 149381b2d1f0SGlenn Strauss } 149408c03cd4SGlenn Strauss /*ck_memzero(ai.digest, ai.dlen);*//* skip clear since mutated */ 149581b2d1f0SGlenn Strauss 149681b2d1f0SGlenn Strauss /* value is our allow-rules */ 14977ba06c71SGlenn Strauss if (!http_auth_match_rules(require, username, NULL, NULL)) { 14984b3a91e6SGlenn Strauss buffer_free(b); 14997c7f8c46SGlenn Strauss return mod_auth_send_401_unauthorized_digest(r, require, 0); 150081b2d1f0SGlenn Strauss } 150181b2d1f0SGlenn Strauss 1502e1188e77SGlenn Strauss if (send_nextnonce) { 15036ad325c6SGlenn Strauss /*(send nextnonce when expiration is approaching)*/ 150426f354cbSGlenn Strauss mod_auth_digest_authentication_info( 150526f354cbSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_OTHER, 150626f354cbSGlenn Strauss CONST_STR_LEN("Authentication-Info")), 150726f354cbSGlenn Strauss send_nextnonce /*(cur_ts)*/, require, ai.dalgo); 15086ad325c6SGlenn Strauss } 150981b2d1f0SGlenn Strauss 15107c7f8c46SGlenn Strauss http_auth_setenv(r, ai.username, ai.ulen, CONST_STR_LEN("Digest")); 151181b2d1f0SGlenn Strauss 151281b2d1f0SGlenn Strauss buffer_free(b); 151381b2d1f0SGlenn Strauss 15144b3a91e6SGlenn Strauss return HANDLER_GO_ON; 151581b2d1f0SGlenn Strauss } 151681b2d1f0SGlenn Strauss 15177c7f8c46SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(request_st * const r, const struct http_auth_require_t * const require, int nonce_stale) { 15187c7f8c46SGlenn Strauss r->http_status = 401; 15197c7f8c46SGlenn Strauss r->handler_module = NULL; 152026f354cbSGlenn Strauss mod_auth_digest_www_authenticate( 152126f354cbSGlenn Strauss http_header_response_set_ptr(r, HTTP_HEADER_WWW_AUTHENTICATE, 152226f354cbSGlenn Strauss CONST_STR_LEN("WWW-Authenticate")), 152326f354cbSGlenn Strauss log_epoch_secs, require, nonce_stale); 1524f9d58670SStefan Bühler return HANDLER_FINISHED; 15257ba06c71SGlenn Strauss } 15267ba06c71SGlenn Strauss 15277c7f8c46SGlenn 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) { 15287ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 15297c7f8c46SGlenn Strauss const buffer *vb = http_header_env_get(r, CONST_STR_LEN("REMOTE_USER")); 15307ba06c71SGlenn Strauss UNUSED(p_d); 15317ba06c71SGlenn Strauss UNUSED(backend); 15323dd3cde9SGlenn Strauss if (NULL != vb && http_auth_match_rules(require, vb->ptr, NULL, NULL)) { 1533f9d58670SStefan Bühler return HANDLER_GO_ON; 1534bcdc6a3bSJan Kneschke } else { 15357c7f8c46SGlenn Strauss r->http_status = 401; 15367c7f8c46SGlenn Strauss r->handler_module = NULL; 1537bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 15387187271fSStefan Bühler } 1539bcdc6a3bSJan Kneschke } 1540