18abd06a7SGlenn Strauss #include "first.h" 28abd06a7SGlenn Strauss 322e8b456SStefan Bühler #include "plugin.h" 422e8b456SStefan Bühler #include "http_auth.h" 522e8b456SStefan Bühler #include "log.h" 6bcdc6a3bSJan Kneschke 7bcdc6a3bSJan Kneschke #include <stdlib.h> 8bcdc6a3bSJan Kneschke #include <string.h> 9bcdc6a3bSJan Kneschke 10bcdc6a3bSJan Kneschke /** 117ba06c71SGlenn Strauss * auth framework 12bcdc6a3bSJan Kneschke */ 13bcdc6a3bSJan Kneschke 144b3a91e6SGlenn Strauss typedef struct { 154b3a91e6SGlenn Strauss /* auth */ 164b3a91e6SGlenn Strauss array *auth_require; 174b3a91e6SGlenn Strauss buffer *auth_backend_conf; 18f54d628cSGlenn Strauss unsigned short auth_extern_authn; 194b3a91e6SGlenn Strauss 204b3a91e6SGlenn Strauss /* generated */ 214b3a91e6SGlenn Strauss const http_auth_backend_t *auth_backend; 227ba06c71SGlenn Strauss } plugin_config; 234b3a91e6SGlenn Strauss 244b3a91e6SGlenn Strauss typedef struct { 254b3a91e6SGlenn Strauss PLUGIN_DATA; 264b3a91e6SGlenn Strauss 277ba06c71SGlenn Strauss plugin_config **config_storage; 284b3a91e6SGlenn Strauss 297ba06c71SGlenn Strauss plugin_config conf; 307ba06c71SGlenn Strauss } plugin_data; 317ba06c71SGlenn Strauss 327ba06c71SGlenn Strauss static handler_t mod_auth_check_basic(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 337ba06c71SGlenn Strauss static handler_t mod_auth_check_digest(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 347ba06c71SGlenn Strauss static handler_t mod_auth_check_extern(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend); 354b3a91e6SGlenn Strauss 36bcdc6a3bSJan Kneschke INIT_FUNC(mod_auth_init) { 377ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_basic = { "basic", mod_auth_check_basic, NULL }; 387ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_digest = { "digest", mod_auth_check_digest, NULL }; 397ba06c71SGlenn Strauss static const http_auth_scheme_t http_auth_scheme_extern = { "extern", mod_auth_check_extern, NULL }; 407ba06c71SGlenn Strauss plugin_data *p; 417ba06c71SGlenn Strauss 427ba06c71SGlenn Strauss /* register http_auth_scheme_* */ 437ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_basic); 447ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_digest); 457ba06c71SGlenn Strauss http_auth_scheme_set(&http_auth_scheme_extern); 46bcdc6a3bSJan Kneschke 47bcdc6a3bSJan Kneschke p = calloc(1, sizeof(*p)); 48bcdc6a3bSJan Kneschke 49bcdc6a3bSJan Kneschke return p; 50bcdc6a3bSJan Kneschke } 51bcdc6a3bSJan Kneschke 52bcdc6a3bSJan Kneschke FREE_FUNC(mod_auth_free) { 537ba06c71SGlenn Strauss plugin_data *p = p_d; 54bcdc6a3bSJan Kneschke 55bcdc6a3bSJan Kneschke UNUSED(srv); 56bcdc6a3bSJan Kneschke 57bcdc6a3bSJan Kneschke if (!p) return HANDLER_GO_ON; 58bcdc6a3bSJan Kneschke 59bcdc6a3bSJan Kneschke if (p->config_storage) { 60bcdc6a3bSJan Kneschke size_t i; 61bcdc6a3bSJan Kneschke for (i = 0; i < srv->config_context->used; i++) { 627ba06c71SGlenn Strauss plugin_config *s = p->config_storage[i]; 63bcdc6a3bSJan Kneschke 6433cebeb0SStefan Bühler if (NULL == s) continue; 654a6ce4aeSJan Kneschke 66bcdc6a3bSJan Kneschke array_free(s->auth_require); 67bcdc6a3bSJan Kneschke buffer_free(s->auth_backend_conf); 68bcdc6a3bSJan Kneschke 69bcdc6a3bSJan Kneschke free(s); 70bcdc6a3bSJan Kneschke } 71bcdc6a3bSJan Kneschke free(p->config_storage); 72bcdc6a3bSJan Kneschke } 73bcdc6a3bSJan Kneschke 74bcdc6a3bSJan Kneschke free(p); 75bcdc6a3bSJan Kneschke 76bcdc6a3bSJan Kneschke return HANDLER_GO_ON; 77bcdc6a3bSJan Kneschke } 78bcdc6a3bSJan Kneschke 797ba06c71SGlenn Strauss /* data type for mod_auth structured data 807ba06c71SGlenn Strauss * (parsed from auth.require array of strings) */ 817ba06c71SGlenn Strauss typedef struct { 827ba06c71SGlenn Strauss DATA_UNSET; 837ba06c71SGlenn Strauss http_auth_require_t *require; 847ba06c71SGlenn Strauss } data_auth; 857ba06c71SGlenn Strauss 867ba06c71SGlenn Strauss static void data_auth_free(data_unset *d) 877ba06c71SGlenn Strauss { 887ba06c71SGlenn Strauss data_auth * const dauth = (data_auth *)d; 897ba06c71SGlenn Strauss buffer_free(dauth->key); 907ba06c71SGlenn Strauss http_auth_require_free(dauth->require); 917ba06c71SGlenn Strauss free(dauth); 927ba06c71SGlenn Strauss } 937ba06c71SGlenn Strauss 947ba06c71SGlenn Strauss static data_auth *data_auth_init(void) 957ba06c71SGlenn Strauss { 967ba06c71SGlenn Strauss data_auth * const dauth = calloc(1, sizeof(*dauth)); 977ba06c71SGlenn Strauss force_assert(NULL != dauth); 987ba06c71SGlenn Strauss dauth->copy = NULL; /* must not be called on this data */ 997ba06c71SGlenn Strauss dauth->free = data_auth_free; 1007ba06c71SGlenn Strauss dauth->reset = NULL; /* must not be called on this data */ 1017ba06c71SGlenn Strauss dauth->insert_dup = NULL; /* must not be called on this data */ 1027ba06c71SGlenn Strauss dauth->print = NULL; /* must not be called on this data */ 1037ba06c71SGlenn Strauss dauth->type = TYPE_OTHER; 1047ba06c71SGlenn Strauss 1057ba06c71SGlenn Strauss dauth->key = buffer_init(); 1067ba06c71SGlenn Strauss dauth->require = http_auth_require_init(); 1077ba06c71SGlenn Strauss 1087ba06c71SGlenn Strauss return dauth; 1097ba06c71SGlenn Strauss } 1107ba06c71SGlenn Strauss 1117ba06c71SGlenn Strauss static int mod_auth_require_parse (server *srv, http_auth_require_t * const require, const buffer *b) 1127ba06c71SGlenn Strauss { 1137ba06c71SGlenn Strauss /* user=name1|user=name2|group=name3|host=name4 */ 1147ba06c71SGlenn Strauss 1157ba06c71SGlenn Strauss const char *str = b->ptr; 1167ba06c71SGlenn Strauss const char *p; 1177ba06c71SGlenn Strauss 1187ba06c71SGlenn Strauss if (buffer_is_equal_string(b, CONST_STR_LEN("valid-user"))) { 1197ba06c71SGlenn Strauss require->valid_user = 1; 1207ba06c71SGlenn Strauss return 1; /* success */ 1217ba06c71SGlenn Strauss } 1227ba06c71SGlenn Strauss 1237ba06c71SGlenn Strauss do { 1247ba06c71SGlenn Strauss const char *eq; 1257ba06c71SGlenn Strauss size_t len; 1267ba06c71SGlenn Strauss p = strchr(str, '|'); 1277ba06c71SGlenn Strauss len = NULL != p ? (size_t)(p - str) : strlen(str); 1287ba06c71SGlenn Strauss eq = memchr(str, '=', len); 1297ba06c71SGlenn Strauss if (NULL == eq) { 1307ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssbss", 1317ba06c71SGlenn Strauss "error parsing auth.require 'require' field: missing '='", 1327ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 1337ba06c71SGlenn Strauss "error value:", b, "error near:", str); 1347ba06c71SGlenn Strauss return 0; 1357ba06c71SGlenn Strauss } 1367ba06c71SGlenn Strauss if (p-1 == eq) { 1377ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssbss", 1387ba06c71SGlenn Strauss "error parsing auth.require 'require' field: missing token after '='", 1397ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 1407ba06c71SGlenn Strauss "error value:", b, "error near:", str); 1417ba06c71SGlenn Strauss return 0; 1427ba06c71SGlenn Strauss } 1437ba06c71SGlenn Strauss 1447ba06c71SGlenn Strauss switch ((int)(eq - str)) { 1457ba06c71SGlenn Strauss case 4: 1467ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("user"))) { 1477ba06c71SGlenn Strauss data_string *ds = data_string_init(); 1487ba06c71SGlenn Strauss buffer_copy_string_len(ds->key,str+5,len-5); /*("user=" is 5)*/ 1497ba06c71SGlenn Strauss array_insert_unique(require->user, (data_unset *)ds); 1507ba06c71SGlenn Strauss continue; 1517ba06c71SGlenn Strauss } 1527ba06c71SGlenn Strauss else if (0 == memcmp(str, CONST_STR_LEN("host"))) { 1537ba06c71SGlenn Strauss data_string *ds = data_string_init(); 1547ba06c71SGlenn Strauss buffer_copy_string_len(ds->key,str+5,len-5); /*("host=" is 5)*/ 1557ba06c71SGlenn Strauss array_insert_unique(require->host, (data_unset *)ds); 1567ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssb", 1577ba06c71SGlenn Strauss "warning parsing auth.require 'require' field: 'host' not implemented;", 1587ba06c71SGlenn Strauss "field value:", b); 1597ba06c71SGlenn Strauss continue; 1607ba06c71SGlenn Strauss } 1617ba06c71SGlenn Strauss break; /* to error */ 1627ba06c71SGlenn Strauss case 5: 1637ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("group"))) { 1647ba06c71SGlenn Strauss data_string *ds = data_string_init(); 1657ba06c71SGlenn Strauss buffer_copy_string_len(ds->key,str+6,len-6); /*("group=" is 6)*/ 1667ba06c71SGlenn Strauss array_insert_unique(require->group, (data_unset *)ds); 1679c91af0cSGlenn Strauss #if 0/*(supported by mod_authn_ldap, but not all other backends)*/ 1687ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssb", 1697ba06c71SGlenn Strauss "warning parsing auth.require 'require' field: 'group' not implemented;", 1707ba06c71SGlenn Strauss "field value:", b); 1719c91af0cSGlenn Strauss #endif 1727ba06c71SGlenn Strauss continue; 1737ba06c71SGlenn Strauss } 1747ba06c71SGlenn Strauss break; /* to error */ 1757ba06c71SGlenn Strauss case 10: 1767ba06c71SGlenn Strauss if (0 == memcmp(str, CONST_STR_LEN("valid-user"))) { 1777ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssb", 1787ba06c71SGlenn Strauss "error parsing auth.require 'require' field: valid user can not be combined with other require rules", 1797ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 1807ba06c71SGlenn Strauss "error value:", b); 1817ba06c71SGlenn Strauss return 0; 1827ba06c71SGlenn Strauss } 1837ba06c71SGlenn Strauss break; /* to error */ 1847ba06c71SGlenn Strauss default: 1857ba06c71SGlenn Strauss break; /* to error */ 1867ba06c71SGlenn Strauss } 1877ba06c71SGlenn Strauss 1887ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssbss", 1897ba06c71SGlenn Strauss "error parsing auth.require 'require' field: invalid/unsupported token", 1907ba06c71SGlenn Strauss "(expecting \"valid-user\" or \"user=a|user=b|group=g|host=h\").", 1917ba06c71SGlenn Strauss "error value:", b, "error near:", str); 1927ba06c71SGlenn Strauss return 0; 1937ba06c71SGlenn Strauss 1947ba06c71SGlenn Strauss } while (p && *((str = p+1))); 1957ba06c71SGlenn Strauss 1967ba06c71SGlenn Strauss return 1; /* success */ 1977ba06c71SGlenn Strauss } 1987ba06c71SGlenn Strauss 1997ba06c71SGlenn Strauss SETDEFAULTS_FUNC(mod_auth_set_defaults) { 2007ba06c71SGlenn Strauss plugin_data *p = p_d; 2017ba06c71SGlenn Strauss size_t i; 2027ba06c71SGlenn Strauss 2037ba06c71SGlenn Strauss config_values_t cv[] = { 2047ba06c71SGlenn Strauss { "auth.backend", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */ 2057ba06c71SGlenn Strauss { "auth.require", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 1 */ 206f54d628cSGlenn Strauss { "auth.extern-authn", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },/* 2 */ 2077ba06c71SGlenn Strauss { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } 2087ba06c71SGlenn Strauss }; 2097ba06c71SGlenn Strauss 2107ba06c71SGlenn Strauss p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *)); 2117ba06c71SGlenn Strauss 2127ba06c71SGlenn Strauss for (i = 0; i < srv->config_context->used; i++) { 2137ba06c71SGlenn Strauss data_config const* config = (data_config const*)srv->config_context->data[i]; 2147ba06c71SGlenn Strauss plugin_config *s; 2157ba06c71SGlenn Strauss size_t n; 2167ba06c71SGlenn Strauss data_array *da; 2177ba06c71SGlenn Strauss 2187ba06c71SGlenn Strauss s = calloc(1, sizeof(plugin_config)); 2197ba06c71SGlenn Strauss s->auth_backend_conf = buffer_init(); 2207ba06c71SGlenn Strauss 2217ba06c71SGlenn Strauss s->auth_require = array_init(); 2227ba06c71SGlenn Strauss 2237ba06c71SGlenn Strauss cv[0].destination = s->auth_backend_conf; 2247ba06c71SGlenn Strauss cv[1].destination = s->auth_require; /* T_CONFIG_LOCAL; not modified by config_insert_values_global() */ 225f54d628cSGlenn Strauss cv[2].destination = &s->auth_extern_authn; 2267ba06c71SGlenn Strauss 2277ba06c71SGlenn Strauss p->config_storage[i] = s; 2287ba06c71SGlenn Strauss 2297ba06c71SGlenn Strauss if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { 2307ba06c71SGlenn Strauss return HANDLER_ERROR; 2317ba06c71SGlenn Strauss } 2327ba06c71SGlenn Strauss 2337ba06c71SGlenn Strauss if (!buffer_string_is_empty(s->auth_backend_conf)) { 2347ba06c71SGlenn Strauss s->auth_backend = http_auth_backend_get(s->auth_backend_conf); 2357ba06c71SGlenn Strauss if (NULL == s->auth_backend) { 2367ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "auth.backend not supported:", s->auth_backend_conf); 2377ba06c71SGlenn Strauss 2387ba06c71SGlenn Strauss return HANDLER_ERROR; 2397ba06c71SGlenn Strauss } 2407ba06c71SGlenn Strauss } 2417ba06c71SGlenn Strauss 2427ba06c71SGlenn Strauss /* no auth.require for this section */ 2437ba06c71SGlenn Strauss if (NULL == (da = (data_array *)array_get_element(config->value, "auth.require"))) continue; 2447ba06c71SGlenn Strauss 245bd77abe0SGlenn Strauss if (da->type != TYPE_ARRAY || !array_is_kvarray(da->value)) { 246bd77abe0SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 247bd77abe0SGlenn Strauss "unexpected value for auth.require; expected ", 248bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 249bd77abe0SGlenn Strauss return HANDLER_ERROR; 250bd77abe0SGlenn Strauss } 251bd77abe0SGlenn Strauss 2527ba06c71SGlenn Strauss 2537ba06c71SGlenn Strauss for (n = 0; n < da->value->used; n++) { 2547ba06c71SGlenn Strauss size_t m; 2557ba06c71SGlenn Strauss data_array *da_file = (data_array *)da->value->data[n]; 2567ba06c71SGlenn Strauss const buffer *method = NULL, *realm = NULL, *require = NULL; 2577ba06c71SGlenn Strauss const http_auth_scheme_t *auth_scheme; 2587ba06c71SGlenn Strauss 259bd77abe0SGlenn Strauss if (!array_is_kvstring(da_file->value)) { 2607ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 261bd77abe0SGlenn Strauss "unexpected value for auth.require; expected ", 262bd77abe0SGlenn Strauss "auth.require = ( \"urlpath\" => ( \"option\" => \"value\" ) )"); 2637ba06c71SGlenn Strauss 2647ba06c71SGlenn Strauss return HANDLER_ERROR; 2657ba06c71SGlenn Strauss } 2667ba06c71SGlenn Strauss 2677ba06c71SGlenn Strauss for (m = 0; m < da_file->value->used; m++) { 2687ba06c71SGlenn Strauss if (da_file->value->data[m]->type == TYPE_STRING) { 2697ba06c71SGlenn Strauss data_string *ds = (data_string *)da_file->value->data[m]; 2707ba06c71SGlenn Strauss if (buffer_is_equal_string(ds->key, CONST_STR_LEN("method"))) { 2717ba06c71SGlenn Strauss method = ds->value; 2727ba06c71SGlenn Strauss } else if (buffer_is_equal_string(ds->key, CONST_STR_LEN("realm"))) { 2737ba06c71SGlenn Strauss realm = ds->value; 2747ba06c71SGlenn Strauss } else if (buffer_is_equal_string(ds->key, CONST_STR_LEN("require"))) { 2757ba06c71SGlenn Strauss require = ds->value; 2767ba06c71SGlenn Strauss } else { 2777ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssbs", 2787ba06c71SGlenn Strauss "the field is unknown in:", 2797ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"", 2807ba06c71SGlenn Strauss da_file->value->data[m]->key, 2817ba06c71SGlenn Strauss "\" <- => \"...\" ) )"); 2827ba06c71SGlenn Strauss 2837ba06c71SGlenn Strauss return HANDLER_ERROR; 2847ba06c71SGlenn Strauss } 2857ba06c71SGlenn Strauss } else { 2867ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ssbs", 2877ba06c71SGlenn Strauss "a string was expected for:", 2887ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., -> \"", 2897ba06c71SGlenn Strauss da_file->value->data[m]->key, 2907ba06c71SGlenn Strauss "\" <- => \"...\" ) )"); 2917ba06c71SGlenn Strauss 2927ba06c71SGlenn Strauss return HANDLER_ERROR; 2937ba06c71SGlenn Strauss } 2947ba06c71SGlenn Strauss } 2957ba06c71SGlenn Strauss 2967ba06c71SGlenn Strauss if (buffer_string_is_empty(method)) { 2977ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 2987ba06c71SGlenn Strauss "the method field is missing or blank in:", 2997ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\" ) )"); 3007ba06c71SGlenn Strauss return HANDLER_ERROR; 3017ba06c71SGlenn Strauss } else { 3027ba06c71SGlenn Strauss auth_scheme = http_auth_scheme_get(method); 3037ba06c71SGlenn Strauss if (NULL == auth_scheme) { 3047ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sbss", 3057ba06c71SGlenn Strauss "unknown method", method, "(e.g. \"basic\", \"digest\" or \"extern\") in", 3067ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"method\" => \"...\") )"); 3077ba06c71SGlenn Strauss return HANDLER_ERROR; 3087ba06c71SGlenn Strauss } 3097ba06c71SGlenn Strauss } 3107ba06c71SGlenn Strauss 3117ba06c71SGlenn Strauss if (buffer_is_empty(realm)) { 3127ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 3137ba06c71SGlenn Strauss "the realm field is missing in:", 3147ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"realm\" => \"...\" ) )"); 3157ba06c71SGlenn Strauss return HANDLER_ERROR; 3167ba06c71SGlenn Strauss } 3177ba06c71SGlenn Strauss 3187ba06c71SGlenn Strauss if (buffer_string_is_empty(require)) { 3197ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "ss", 3207ba06c71SGlenn Strauss "the require field is missing or blank in:", 3217ba06c71SGlenn Strauss "auth.require = ( \"...\" => ( ..., \"require\" => \"...\" ) )"); 3227ba06c71SGlenn Strauss return HANDLER_ERROR; 3237ba06c71SGlenn Strauss } 3247ba06c71SGlenn Strauss 325609e9a50SGlenn Strauss if (require) { /*(always true at this point)*/ 3267ba06c71SGlenn Strauss data_auth * const dauth = data_auth_init(); 3277ba06c71SGlenn Strauss buffer_copy_buffer(dauth->key, da_file->key); 3287ba06c71SGlenn Strauss dauth->require->scheme = auth_scheme; 3297ba06c71SGlenn Strauss buffer_copy_buffer(dauth->require->realm, realm); 3307ba06c71SGlenn Strauss if (!mod_auth_require_parse(srv, dauth->require, require)) { 3317ba06c71SGlenn Strauss dauth->free((data_unset *)dauth); 3327ba06c71SGlenn Strauss return HANDLER_ERROR; 3337ba06c71SGlenn Strauss } 3347ba06c71SGlenn Strauss array_insert_unique(s->auth_require, (data_unset *)dauth); 3357ba06c71SGlenn Strauss } 3367ba06c71SGlenn Strauss } 3377ba06c71SGlenn Strauss } 3387ba06c71SGlenn Strauss 3397ba06c71SGlenn Strauss return HANDLER_GO_ON; 3407ba06c71SGlenn Strauss } 3417ba06c71SGlenn Strauss 342bcdc6a3bSJan Kneschke #define PATCH(x) \ 343bcdc6a3bSJan Kneschke p->conf.x = s->x; 3447ba06c71SGlenn Strauss static int mod_auth_patch_connection(server *srv, connection *con, plugin_data *p) { 345bcdc6a3bSJan Kneschke size_t i, j; 3467ba06c71SGlenn Strauss plugin_config *s = p->config_storage[0]; 3474a6ce4aeSJan Kneschke 3484a6ce4aeSJan Kneschke PATCH(auth_backend); 3494a6ce4aeSJan Kneschke PATCH(auth_require); 350f54d628cSGlenn Strauss PATCH(auth_extern_authn); 351bcdc6a3bSJan Kneschke 352bcdc6a3bSJan Kneschke /* skip the first, the global context */ 353bcdc6a3bSJan Kneschke for (i = 1; i < srv->config_context->used; i++) { 354bcdc6a3bSJan Kneschke data_config *dc = (data_config *)srv->config_context->data[i]; 3554a6ce4aeSJan Kneschke s = p->config_storage[i]; 356bcdc6a3bSJan Kneschke 357bcdc6a3bSJan Kneschke /* condition didn't match */ 358bcdc6a3bSJan Kneschke if (!config_check_cond(srv, con, dc)) continue; 359bcdc6a3bSJan Kneschke 360bcdc6a3bSJan Kneschke /* merge config */ 361bcdc6a3bSJan Kneschke for (j = 0; j < dc->value->used; j++) { 362bcdc6a3bSJan Kneschke data_unset *du = dc->value->data[j]; 363bcdc6a3bSJan Kneschke 364bcdc6a3bSJan Kneschke if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.backend"))) { 365bcdc6a3bSJan Kneschke PATCH(auth_backend); 366bcdc6a3bSJan Kneschke } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.require"))) { 367bcdc6a3bSJan Kneschke PATCH(auth_require); 368f54d628cSGlenn Strauss } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("auth.extern-authn"))) { 369f54d628cSGlenn Strauss PATCH(auth_extern_authn); 370bcdc6a3bSJan Kneschke } 371bcdc6a3bSJan Kneschke } 372bcdc6a3bSJan Kneschke } 373bcdc6a3bSJan Kneschke 374bcdc6a3bSJan Kneschke return 0; 375bcdc6a3bSJan Kneschke } 376bcdc6a3bSJan Kneschke #undef PATCH 377bcdc6a3bSJan Kneschke 3787ba06c71SGlenn Strauss static handler_t mod_auth_uri_handler(server *srv, connection *con, void *p_d) { 3797ba06c71SGlenn Strauss size_t k; 3807ba06c71SGlenn Strauss plugin_data *p = p_d; 38181b2d1f0SGlenn Strauss 3827ba06c71SGlenn Strauss mod_auth_patch_connection(srv, con, p); 38381b2d1f0SGlenn Strauss 3847ba06c71SGlenn Strauss if (p->conf.auth_require == NULL) return HANDLER_GO_ON; 38581b2d1f0SGlenn Strauss 3867ba06c71SGlenn Strauss /* search auth directives for first prefix match against URL path */ 3877ba06c71SGlenn Strauss for (k = 0; k < p->conf.auth_require->used; k++) { 3887ba06c71SGlenn Strauss const data_auth * const dauth = (data_auth *)p->conf.auth_require->data[k]; 3897ba06c71SGlenn Strauss const buffer *path = dauth->key; 3907ba06c71SGlenn Strauss 3917ba06c71SGlenn Strauss if (buffer_string_length(con->uri.path) < buffer_string_length(path)) continue; 3927ba06c71SGlenn Strauss 3937ba06c71SGlenn Strauss /* if we have a case-insensitive FS we have to lower-case the URI here too */ 3947ba06c71SGlenn Strauss 3957ba06c71SGlenn Strauss if (!con->conf.force_lowercase_filenames 3967ba06c71SGlenn Strauss ? 0 == strncmp(con->uri.path->ptr, path->ptr, buffer_string_length(path)) 3977ba06c71SGlenn Strauss : 0 == strncasecmp(con->uri.path->ptr, path->ptr, buffer_string_length(path))) { 3987ba06c71SGlenn Strauss const http_auth_scheme_t * const scheme = dauth->require->scheme; 399f54d628cSGlenn Strauss if (p->conf.auth_extern_authn) { 400f54d628cSGlenn Strauss data_string *ds = (data_string *)array_get_element(con->environment, "REMOTE_USER"); 401f54d628cSGlenn Strauss if (NULL != ds && http_auth_match_rules(dauth->require, ds->value->ptr, NULL, NULL)) { 402f54d628cSGlenn Strauss return HANDLER_GO_ON; 403f54d628cSGlenn Strauss } 404f54d628cSGlenn Strauss } 4057ba06c71SGlenn Strauss return scheme->checkfn(srv, con, scheme->p_d, dauth->require, p->conf.auth_backend); 4067ba06c71SGlenn Strauss } 4077ba06c71SGlenn Strauss } 4087ba06c71SGlenn Strauss 4097ba06c71SGlenn Strauss /* nothing to do for us */ 4107ba06c71SGlenn Strauss return HANDLER_GO_ON; 4117ba06c71SGlenn Strauss } 4127ba06c71SGlenn Strauss 4137ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p); 4147ba06c71SGlenn Strauss int mod_auth_plugin_init(plugin *p) { 4157ba06c71SGlenn Strauss p->version = LIGHTTPD_VERSION_ID; 4167ba06c71SGlenn Strauss p->name = buffer_init_string("auth"); 4177ba06c71SGlenn Strauss p->init = mod_auth_init; 4187ba06c71SGlenn Strauss p->set_defaults = mod_auth_set_defaults; 4197ba06c71SGlenn Strauss p->handle_uri_clean = mod_auth_uri_handler; 4207ba06c71SGlenn Strauss p->cleanup = mod_auth_free; 4217ba06c71SGlenn Strauss 4227ba06c71SGlenn Strauss p->data = NULL; 4237ba06c71SGlenn Strauss 42481b2d1f0SGlenn Strauss return 0; 42581b2d1f0SGlenn Strauss } 42681b2d1f0SGlenn Strauss 42781b2d1f0SGlenn Strauss 42881b2d1f0SGlenn Strauss 42981b2d1f0SGlenn Strauss 4307ba06c71SGlenn Strauss /* 4317ba06c71SGlenn Strauss * auth schemes (basic, digest, extern) 4327ba06c71SGlenn Strauss * 4337ba06c71SGlenn Strauss * (could be in separate file from mod_auth.c as long as registration occurs) 4347ba06c71SGlenn Strauss */ 43581b2d1f0SGlenn Strauss 4367ba06c71SGlenn Strauss #include "response.h" 4377ba06c71SGlenn Strauss #include "base64.h" 4387ba06c71SGlenn Strauss #include "md5.h" 4397f4e156eSGlenn Strauss #include "rand.h" 44081b2d1f0SGlenn Strauss 4414b3a91e6SGlenn Strauss static handler_t mod_auth_send_400_bad_request(server *srv, connection *con) { 4424b3a91e6SGlenn Strauss UNUSED(srv); 4434b3a91e6SGlenn Strauss 4444b3a91e6SGlenn Strauss /* a field was missing or invalid */ 4454b3a91e6SGlenn Strauss con->http_status = 400; /* Bad Request */ 4464b3a91e6SGlenn Strauss con->mode = DIRECT; 4474b3a91e6SGlenn Strauss 4484b3a91e6SGlenn Strauss return HANDLER_FINISHED; 4494b3a91e6SGlenn Strauss } 4504b3a91e6SGlenn Strauss 4517ba06c71SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_basic(server *srv, connection *con, buffer *realm) { 4524b3a91e6SGlenn Strauss con->http_status = 401; 4534b3a91e6SGlenn Strauss con->mode = DIRECT; 4544b3a91e6SGlenn Strauss 4557ba06c71SGlenn Strauss buffer_copy_string_len(srv->tmp_buf, CONST_STR_LEN("Basic realm=\"")); 4567ba06c71SGlenn Strauss buffer_append_string_buffer(srv->tmp_buf, realm); 4577ba06c71SGlenn Strauss buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN("\", charset=\"UTF-8\"")); 4584b3a91e6SGlenn Strauss 4597ba06c71SGlenn Strauss response_header_insert(srv, con, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(srv->tmp_buf)); 4604b3a91e6SGlenn Strauss 4614b3a91e6SGlenn Strauss return HANDLER_FINISHED; 4624b3a91e6SGlenn Strauss } 4634b3a91e6SGlenn Strauss 4647ba06c71SGlenn Strauss static handler_t mod_auth_check_basic(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 4657ba06c71SGlenn Strauss data_string *ds = (data_string *)array_get_element(con->request.headers, "Authorization"); 46681b2d1f0SGlenn Strauss buffer *username; 4677ba06c71SGlenn Strauss buffer *b; 46881b2d1f0SGlenn Strauss char *pw; 4697ba06c71SGlenn Strauss handler_t rc = HANDLER_UNSET; 47081b2d1f0SGlenn Strauss 4717ba06c71SGlenn Strauss UNUSED(p_d); 47281b2d1f0SGlenn Strauss 4737ba06c71SGlenn Strauss if (NULL == backend) { 4747ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "auth.backend not configured for", con->uri.path); 4757ba06c71SGlenn Strauss con->http_status = 500; 4767ba06c71SGlenn Strauss con->mode = DIRECT; 4777ba06c71SGlenn Strauss return HANDLER_FINISHED; 4787ba06c71SGlenn Strauss } 4797ba06c71SGlenn Strauss 4807ba06c71SGlenn Strauss if (NULL == ds || buffer_is_empty(ds->value)) { 4817ba06c71SGlenn Strauss return mod_auth_send_401_unauthorized_basic(srv, con, require->realm); 4827ba06c71SGlenn Strauss } 4837ba06c71SGlenn Strauss 4847ba06c71SGlenn Strauss if (0 != strncasecmp(ds->value->ptr, "Basic ", sizeof("Basic ")-1)) { 4854b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 4864b3a91e6SGlenn Strauss } 48781b2d1f0SGlenn Strauss 48881b2d1f0SGlenn Strauss username = buffer_init(); 48981b2d1f0SGlenn Strauss 4907ba06c71SGlenn Strauss b = ds->value; 4918047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 4927ba06c71SGlenn Strauss if (!buffer_append_base64_decode(username, b->ptr+sizeof("Basic ")-1, buffer_string_length(b)-(sizeof("Basic ")-1), BASE64_STANDARD)) { 4937ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "decoding base64-string failed", username); 49481b2d1f0SGlenn Strauss 49581b2d1f0SGlenn Strauss buffer_free(username); 4964b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 49781b2d1f0SGlenn Strauss } 49881b2d1f0SGlenn Strauss 49981b2d1f0SGlenn Strauss /* r2 == user:password */ 50081b2d1f0SGlenn Strauss if (NULL == (pw = strchr(username->ptr, ':'))) { 5017ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "missing ':' in", username); 50281b2d1f0SGlenn Strauss 50381b2d1f0SGlenn Strauss buffer_free(username); 5044b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 50581b2d1f0SGlenn Strauss } 50681b2d1f0SGlenn Strauss 50781b2d1f0SGlenn Strauss buffer_string_set_length(username, pw - username->ptr); 50881b2d1f0SGlenn Strauss pw++; 50981b2d1f0SGlenn Strauss 510a401c946SGlenn Strauss rc = backend->basic(srv, con, backend->p_d, require, username, pw); 5117ba06c71SGlenn Strauss switch (rc) { 5124b3a91e6SGlenn Strauss case HANDLER_GO_ON: 5137ba06c71SGlenn Strauss http_auth_setenv(con->environment, CONST_BUF_LEN(username), CONST_STR_LEN("Basic")); 5144b3a91e6SGlenn Strauss break; 5154b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 5164b3a91e6SGlenn Strauss case HANDLER_FINISHED: 5177ba06c71SGlenn Strauss break; 5184b3a91e6SGlenn Strauss case HANDLER_ERROR: 5194b3a91e6SGlenn Strauss default: 52028d12134SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sbsBsB", "password doesn't match for", con->uri.path, "username:", username, ", IP:", con->dst_addr_buf); 5217ba06c71SGlenn Strauss rc = HANDLER_UNSET; 5227ba06c71SGlenn Strauss break; 52381b2d1f0SGlenn Strauss } 52481b2d1f0SGlenn Strauss 5254b3a91e6SGlenn Strauss buffer_free(username); 5267ba06c71SGlenn Strauss return (HANDLER_UNSET != rc) ? rc : mod_auth_send_401_unauthorized_basic(srv, con, require->realm); 52781b2d1f0SGlenn Strauss } 52881b2d1f0SGlenn Strauss 52981b2d1f0SGlenn Strauss #define HASHLEN 16 53081b2d1f0SGlenn Strauss #define HASHHEXLEN 32 53181b2d1f0SGlenn Strauss typedef unsigned char HASH[HASHLEN]; 53281b2d1f0SGlenn Strauss typedef char HASHHEX[HASHHEXLEN+1]; 53381b2d1f0SGlenn Strauss 53481b2d1f0SGlenn Strauss static void CvtHex(const HASH Bin, char (*Hex)[33]) { 53581b2d1f0SGlenn Strauss li_tohex(*Hex, sizeof(*Hex), (const char*) Bin, 16); 53681b2d1f0SGlenn Strauss } 53781b2d1f0SGlenn Strauss 53881b2d1f0SGlenn Strauss typedef struct { 53981b2d1f0SGlenn Strauss const char *key; 54081b2d1f0SGlenn Strauss int key_len; 54181b2d1f0SGlenn Strauss char **ptr; 54281b2d1f0SGlenn Strauss } digest_kv; 54381b2d1f0SGlenn Strauss 5447ba06c71SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(server *srv, connection *con, buffer *realm, int nonce_stale); 5457ba06c71SGlenn Strauss 5467ba06c71SGlenn Strauss static handler_t mod_auth_check_digest(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 5477ba06c71SGlenn Strauss data_string *ds = (data_string *)array_get_element(con->request.headers, "Authorization"); 5487ba06c71SGlenn Strauss 54981b2d1f0SGlenn Strauss char a1[33]; 55081b2d1f0SGlenn Strauss char a2[33]; 55181b2d1f0SGlenn Strauss 55281b2d1f0SGlenn Strauss char *username = NULL; 55381b2d1f0SGlenn Strauss char *realm = NULL; 55481b2d1f0SGlenn Strauss char *nonce = NULL; 55581b2d1f0SGlenn Strauss char *uri = NULL; 55681b2d1f0SGlenn Strauss char *algorithm = NULL; 55781b2d1f0SGlenn Strauss char *qop = NULL; 55881b2d1f0SGlenn Strauss char *cnonce = NULL; 55981b2d1f0SGlenn Strauss char *nc = NULL; 56081b2d1f0SGlenn Strauss char *respons = NULL; 56181b2d1f0SGlenn Strauss 56281b2d1f0SGlenn Strauss char *e, *c; 56381b2d1f0SGlenn Strauss const char *m = NULL; 56481b2d1f0SGlenn Strauss int i; 56581b2d1f0SGlenn Strauss buffer *b; 56681b2d1f0SGlenn Strauss 56781b2d1f0SGlenn Strauss li_MD5_CTX Md5Ctx; 56881b2d1f0SGlenn Strauss HASH HA1; 56981b2d1f0SGlenn Strauss HASH HA2; 57081b2d1f0SGlenn Strauss HASH RespHash; 57181b2d1f0SGlenn Strauss HASHHEX HA2Hex; 57281b2d1f0SGlenn Strauss 57381b2d1f0SGlenn Strauss 57481b2d1f0SGlenn Strauss /* init pointers */ 57581b2d1f0SGlenn Strauss #define S(x) \ 57681b2d1f0SGlenn Strauss x, sizeof(x)-1, NULL 57781b2d1f0SGlenn Strauss digest_kv dkv[10] = { 57881b2d1f0SGlenn Strauss { S("username=") }, 57981b2d1f0SGlenn Strauss { S("realm=") }, 58081b2d1f0SGlenn Strauss { S("nonce=") }, 58181b2d1f0SGlenn Strauss { S("uri=") }, 58281b2d1f0SGlenn Strauss { S("algorithm=") }, 58381b2d1f0SGlenn Strauss { S("qop=") }, 58481b2d1f0SGlenn Strauss { S("cnonce=") }, 58581b2d1f0SGlenn Strauss { S("nc=") }, 58681b2d1f0SGlenn Strauss { S("response=") }, 58781b2d1f0SGlenn Strauss 58881b2d1f0SGlenn Strauss { NULL, 0, NULL } 58981b2d1f0SGlenn Strauss }; 59081b2d1f0SGlenn Strauss #undef S 59181b2d1f0SGlenn Strauss 59281b2d1f0SGlenn Strauss dkv[0].ptr = &username; 59381b2d1f0SGlenn Strauss dkv[1].ptr = &realm; 59481b2d1f0SGlenn Strauss dkv[2].ptr = &nonce; 59581b2d1f0SGlenn Strauss dkv[3].ptr = &uri; 59681b2d1f0SGlenn Strauss dkv[4].ptr = &algorithm; 59781b2d1f0SGlenn Strauss dkv[5].ptr = &qop; 59881b2d1f0SGlenn Strauss dkv[6].ptr = &cnonce; 59981b2d1f0SGlenn Strauss dkv[7].ptr = &nc; 60081b2d1f0SGlenn Strauss dkv[8].ptr = &respons; 60181b2d1f0SGlenn Strauss 6027ba06c71SGlenn Strauss UNUSED(p_d); 6037ba06c71SGlenn Strauss 6047ba06c71SGlenn Strauss if (NULL == backend) { 6057ba06c71SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sb", "auth.backend not configured for", con->uri.path); 6067ba06c71SGlenn Strauss con->http_status = 500; 6077ba06c71SGlenn Strauss con->mode = DIRECT; 6087ba06c71SGlenn Strauss return HANDLER_FINISHED; 6097ba06c71SGlenn Strauss } 6107ba06c71SGlenn Strauss 6117ba06c71SGlenn Strauss if (NULL == ds || buffer_is_empty(ds->value)) { 6127ba06c71SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require->realm, 0); 6137ba06c71SGlenn Strauss } 6147ba06c71SGlenn Strauss 6157ba06c71SGlenn Strauss if (0 != strncasecmp(ds->value->ptr, "Digest ", sizeof("Digest ")-1)) { 6167ba06c71SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 6177ba06c71SGlenn Strauss } 6187ba06c71SGlenn Strauss 6197ba06c71SGlenn Strauss b = buffer_init(); 6208047c2f4SGlenn Strauss /* coverity[overflow_sink : FALSE] */ 6217ba06c71SGlenn Strauss buffer_copy_string_len(b, ds->value->ptr+sizeof("Digest ")-1, buffer_string_length(ds->value)-(sizeof("Digest ")-1)); 62281b2d1f0SGlenn Strauss 62381b2d1f0SGlenn Strauss /* parse credentials from client */ 62481b2d1f0SGlenn Strauss for (c = b->ptr; *c; c++) { 62581b2d1f0SGlenn Strauss /* skip whitespaces */ 62681b2d1f0SGlenn Strauss while (*c == ' ' || *c == '\t') c++; 62781b2d1f0SGlenn Strauss if (!*c) break; 62881b2d1f0SGlenn Strauss 62981b2d1f0SGlenn Strauss for (i = 0; dkv[i].key; i++) { 63081b2d1f0SGlenn Strauss if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) { 63181b2d1f0SGlenn Strauss if ((c[dkv[i].key_len] == '"') && 63281b2d1f0SGlenn Strauss (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) { 63381b2d1f0SGlenn Strauss /* value with "..." */ 63481b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len + 1; 63581b2d1f0SGlenn Strauss c = e; 63681b2d1f0SGlenn Strauss 63781b2d1f0SGlenn Strauss *e = '\0'; 63881b2d1f0SGlenn Strauss } else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) { 63981b2d1f0SGlenn Strauss /* value without "...", terminated by ',' */ 64081b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 64181b2d1f0SGlenn Strauss c = e; 64281b2d1f0SGlenn Strauss 64381b2d1f0SGlenn Strauss *e = '\0'; 64481b2d1f0SGlenn Strauss } else { 64581b2d1f0SGlenn Strauss /* value without "...", terminated by EOL */ 64681b2d1f0SGlenn Strauss *(dkv[i].ptr) = c + dkv[i].key_len; 64781b2d1f0SGlenn Strauss c += strlen(c) - 1; 64881b2d1f0SGlenn Strauss } 64981b2d1f0SGlenn Strauss break; 65081b2d1f0SGlenn Strauss } 65181b2d1f0SGlenn Strauss } 65281b2d1f0SGlenn Strauss } 65381b2d1f0SGlenn Strauss 65481b2d1f0SGlenn Strauss /* check if everything is transmitted */ 65581b2d1f0SGlenn Strauss if (!username || 65681b2d1f0SGlenn Strauss !realm || 65781b2d1f0SGlenn Strauss !nonce || 65881b2d1f0SGlenn Strauss !uri || 65981b2d1f0SGlenn Strauss (qop && (!nc || !cnonce)) || 66081b2d1f0SGlenn Strauss !respons ) { 66181b2d1f0SGlenn Strauss /* missing field */ 66281b2d1f0SGlenn Strauss 66381b2d1f0SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "s", 66481b2d1f0SGlenn Strauss "digest: missing field"); 66581b2d1f0SGlenn Strauss 66681b2d1f0SGlenn Strauss buffer_free(b); 6674b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 66881b2d1f0SGlenn Strauss } 66981b2d1f0SGlenn Strauss 67081b2d1f0SGlenn Strauss /** 67181b2d1f0SGlenn Strauss * protect the md5-sess against missing cnonce and nonce 67281b2d1f0SGlenn Strauss */ 67381b2d1f0SGlenn Strauss if (algorithm && 67481b2d1f0SGlenn Strauss 0 == strcasecmp(algorithm, "md5-sess") && 67581b2d1f0SGlenn Strauss (!nonce || !cnonce)) { 67681b2d1f0SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "s", 67781b2d1f0SGlenn Strauss "digest: (md5-sess: missing field"); 67881b2d1f0SGlenn Strauss 67981b2d1f0SGlenn Strauss buffer_free(b); 6804b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 68181b2d1f0SGlenn Strauss } 68281b2d1f0SGlenn Strauss 68381b2d1f0SGlenn Strauss if (qop && strcasecmp(qop, "auth-int") == 0) { 68481b2d1f0SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "s", 68581b2d1f0SGlenn Strauss "digest: qop=auth-int not supported"); 68681b2d1f0SGlenn Strauss 68781b2d1f0SGlenn Strauss buffer_free(b); 6884b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 68981b2d1f0SGlenn Strauss } 69081b2d1f0SGlenn Strauss 69181b2d1f0SGlenn Strauss m = get_http_method_name(con->request.http_method); 69281b2d1f0SGlenn Strauss force_assert(m); 69381b2d1f0SGlenn Strauss 69481b2d1f0SGlenn Strauss /* detect if attacker is attempting to reuse valid digest for one uri 69581b2d1f0SGlenn Strauss * on a different request uri. Might also happen if intermediate proxy 69681b2d1f0SGlenn Strauss * altered client request line. (Altered request would not result in 69781b2d1f0SGlenn Strauss * the same digest as that calculated by the client.) 69881b2d1f0SGlenn Strauss * Internal redirects such as with mod_rewrite will modify request uri. 69981b2d1f0SGlenn Strauss * Reauthentication is done to detect crossing auth realms, but this 70081b2d1f0SGlenn Strauss * uri validation step is bypassed. con->request.orig_uri is original 70181b2d1f0SGlenn Strauss * uri sent in client request. */ 70281b2d1f0SGlenn Strauss { 70381b2d1f0SGlenn Strauss const size_t ulen = strlen(uri); 70481b2d1f0SGlenn Strauss const size_t rlen = buffer_string_length(con->request.orig_uri); 70581b2d1f0SGlenn Strauss if (!buffer_is_equal_string(con->request.orig_uri, uri, ulen) 70681b2d1f0SGlenn Strauss && !(rlen < ulen && 0 == memcmp(con->request.orig_uri->ptr, uri, rlen) && uri[rlen] == '?')) { 70728d12134SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sbsssB", 7087ba06c71SGlenn Strauss "digest: auth failed: uri mismatch (", con->request.orig_uri, "!=", uri, "), IP:", con->dst_addr_buf); 70981b2d1f0SGlenn Strauss buffer_free(b); 7104b3a91e6SGlenn Strauss return mod_auth_send_400_bad_request(srv, con); 71181b2d1f0SGlenn Strauss } 71281b2d1f0SGlenn Strauss } 71381b2d1f0SGlenn Strauss 71481b2d1f0SGlenn Strauss /* password-string == HA1 */ 7157ba06c71SGlenn Strauss switch (backend->digest(srv, con, backend->p_d, username, realm, HA1)) { 7164b3a91e6SGlenn Strauss case HANDLER_GO_ON: 7174b3a91e6SGlenn Strauss break; 7184b3a91e6SGlenn Strauss case HANDLER_WAIT_FOR_EVENT: 71981b2d1f0SGlenn Strauss buffer_free(b); 7204b3a91e6SGlenn Strauss return HANDLER_WAIT_FOR_EVENT; 7214b3a91e6SGlenn Strauss case HANDLER_FINISHED: 7224b3a91e6SGlenn Strauss buffer_free(b); 7234b3a91e6SGlenn Strauss return HANDLER_FINISHED; 7244b3a91e6SGlenn Strauss case HANDLER_ERROR: 7254b3a91e6SGlenn Strauss default: 7264b3a91e6SGlenn Strauss buffer_free(b); 7277ba06c71SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require->realm, 0); 72881b2d1f0SGlenn Strauss } 72981b2d1f0SGlenn Strauss 73081b2d1f0SGlenn Strauss if (algorithm && 73181b2d1f0SGlenn Strauss strcasecmp(algorithm, "md5-sess") == 0) { 73281b2d1f0SGlenn Strauss li_MD5_Init(&Md5Ctx); 73381b2d1f0SGlenn Strauss /* Errata ID 1649: http://www.rfc-editor.org/errata_search.php?rfc=2617 */ 73481b2d1f0SGlenn Strauss CvtHex(HA1, &a1); 73581b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)a1, HASHHEXLEN); 73681b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 73781b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce)); 73881b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 73981b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce)); 74081b2d1f0SGlenn Strauss li_MD5_Final(HA1, &Md5Ctx); 74181b2d1f0SGlenn Strauss } 74281b2d1f0SGlenn Strauss 74381b2d1f0SGlenn Strauss CvtHex(HA1, &a1); 74481b2d1f0SGlenn Strauss 74581b2d1f0SGlenn Strauss /* calculate H(A2) */ 74681b2d1f0SGlenn Strauss li_MD5_Init(&Md5Ctx); 74781b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)m, strlen(m)); 74881b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 74981b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)uri, strlen(uri)); 75081b2d1f0SGlenn Strauss /* qop=auth-int not supported, already checked above */ 75181b2d1f0SGlenn Strauss /* 75281b2d1f0SGlenn Strauss if (qop && strcasecmp(qop, "auth-int") == 0) { 75381b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 75481b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *) [body checksum], HASHHEXLEN); 75581b2d1f0SGlenn Strauss } 75681b2d1f0SGlenn Strauss */ 75781b2d1f0SGlenn Strauss li_MD5_Final(HA2, &Md5Ctx); 75881b2d1f0SGlenn Strauss CvtHex(HA2, &HA2Hex); 75981b2d1f0SGlenn Strauss 76081b2d1f0SGlenn Strauss /* calculate response */ 76181b2d1f0SGlenn Strauss li_MD5_Init(&Md5Ctx); 76281b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)a1, HASHHEXLEN); 76381b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 76481b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce)); 76581b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 76681b2d1f0SGlenn Strauss if (qop && *qop) { 76781b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)nc, strlen(nc)); 76881b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 76981b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce)); 77081b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 77181b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)qop, strlen(qop)); 77281b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":")); 77381b2d1f0SGlenn Strauss }; 77481b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)HA2Hex, HASHHEXLEN); 77581b2d1f0SGlenn Strauss li_MD5_Final(RespHash, &Md5Ctx); 77681b2d1f0SGlenn Strauss CvtHex(RespHash, &a2); 77781b2d1f0SGlenn Strauss 77881b2d1f0SGlenn Strauss if (0 != strcmp(a2, respons)) { 77981b2d1f0SGlenn Strauss /* digest not ok */ 78028d12134SGlenn Strauss log_error_write(srv, __FILE__, __LINE__, "sssB", 7817ba06c71SGlenn Strauss "digest: auth failed for ", username, ": wrong password, IP:", con->dst_addr_buf); 78281b2d1f0SGlenn Strauss 78381b2d1f0SGlenn Strauss buffer_free(b); 7847ba06c71SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require->realm, 0); 78581b2d1f0SGlenn Strauss } 78681b2d1f0SGlenn Strauss 78781b2d1f0SGlenn Strauss /* value is our allow-rules */ 7887ba06c71SGlenn Strauss if (!http_auth_match_rules(require, username, NULL, NULL)) { 7894b3a91e6SGlenn Strauss buffer_free(b); 7907ba06c71SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require->realm, 0); 79181b2d1f0SGlenn Strauss } 79281b2d1f0SGlenn Strauss 7937f4e156eSGlenn Strauss /* check age of nonce. Note, random data is used in nonce generation 7947ba06c71SGlenn Strauss * in mod_auth_send_401_unauthorized_digest(). If that were replaced 79581b2d1f0SGlenn Strauss * with nanosecond time, then nonce secret would remain unique enough 79681b2d1f0SGlenn Strauss * for the purposes of Digest auth, and would be reproducible (and 79781b2d1f0SGlenn Strauss * verifiable) if nanoseconds were inclued with seconds as part of the 79881b2d1f0SGlenn Strauss * nonce "timestamp:secret". Since that is not done, timestamp in 79981b2d1f0SGlenn Strauss * nonce could theoretically be modified and still produce same md5sum, 80081b2d1f0SGlenn Strauss * but that is highly unlikely within a 10 min (moving) window of valid 80181b2d1f0SGlenn Strauss * time relative to current time (now) */ 80281b2d1f0SGlenn Strauss { 80381b2d1f0SGlenn Strauss time_t ts = 0; 80481b2d1f0SGlenn Strauss const unsigned char * const nonce_uns = (unsigned char *)nonce; 80581b2d1f0SGlenn Strauss for (i = 0; i < 8 && light_isxdigit(nonce_uns[i]); ++i) { 80681b2d1f0SGlenn Strauss ts = (ts << 4) + hex2int(nonce_uns[i]); 80781b2d1f0SGlenn Strauss } 808*83759aedSGlenn Strauss if (nonce[i] != ':' 80981b2d1f0SGlenn Strauss || ts > srv->cur_ts || srv->cur_ts - ts > 600) { /*(10 mins)*/ 8104b3a91e6SGlenn Strauss /* nonce is stale; have client regenerate digest */ 81181b2d1f0SGlenn Strauss buffer_free(b); 8127ba06c71SGlenn Strauss return mod_auth_send_401_unauthorized_digest(srv, con, require->realm, 1); 81381b2d1f0SGlenn Strauss } /*(future: might send nextnonce when expiration is imminent)*/ 81481b2d1f0SGlenn Strauss } 81581b2d1f0SGlenn Strauss 8167ba06c71SGlenn Strauss http_auth_setenv(con->environment, username, strlen(username), CONST_STR_LEN("Digest")); 81781b2d1f0SGlenn Strauss 81881b2d1f0SGlenn Strauss buffer_free(b); 81981b2d1f0SGlenn Strauss 8204b3a91e6SGlenn Strauss return HANDLER_GO_ON; 82181b2d1f0SGlenn Strauss } 82281b2d1f0SGlenn Strauss 8237ba06c71SGlenn Strauss static handler_t mod_auth_send_401_unauthorized_digest(server *srv, connection *con, buffer *realm, int nonce_stale) { 82481b2d1f0SGlenn Strauss li_MD5_CTX Md5Ctx; 8257ba06c71SGlenn Strauss HASH h; 8267ba06c71SGlenn Strauss char hh[33]; 82781b2d1f0SGlenn Strauss 8287ba06c71SGlenn Strauss force_assert(33 >= LI_ITOSTRING_LENGTH); /*(buffer used for both li_itostrn() and CvtHex())*/ 8297ba06c71SGlenn Strauss 8307ba06c71SGlenn Strauss /* generate nonce */ 8317ba06c71SGlenn Strauss 83281b2d1f0SGlenn Strauss /* generate shared-secret */ 83381b2d1f0SGlenn Strauss li_MD5_Init(&Md5Ctx); 83481b2d1f0SGlenn Strauss 83581b2d1f0SGlenn Strauss li_itostrn(hh, sizeof(hh), srv->cur_ts); 83681b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh)); 837c64c2173SGlenn Strauss li_itostrn(hh, sizeof(hh), li_rand_pseudo_bytes()); 83881b2d1f0SGlenn Strauss li_MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh)); 83981b2d1f0SGlenn Strauss 84081b2d1f0SGlenn Strauss li_MD5_Final(h, &Md5Ctx); 84181b2d1f0SGlenn Strauss 8427ba06c71SGlenn Strauss CvtHex(h, &hh); 84381b2d1f0SGlenn Strauss 8447ba06c71SGlenn Strauss /* generate WWW-Authenticate */ 84581b2d1f0SGlenn Strauss 846f9d58670SStefan Bühler con->http_status = 401; 847f9d58670SStefan Bühler con->mode = DIRECT; 8487ba06c71SGlenn Strauss 8497ba06c71SGlenn Strauss buffer_copy_string_len(srv->tmp_buf, CONST_STR_LEN("Digest realm=\"")); 8507ba06c71SGlenn Strauss buffer_append_string_buffer(srv->tmp_buf, realm); 8517ba06c71SGlenn Strauss buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN("\", charset=\"UTF-8\", nonce=\"")); 8527ba06c71SGlenn Strauss buffer_append_uint_hex(srv->tmp_buf, (uintmax_t)srv->cur_ts); 8537ba06c71SGlenn Strauss buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN(":")); 8547ba06c71SGlenn Strauss buffer_append_string(srv->tmp_buf, hh); 8557ba06c71SGlenn Strauss buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN("\", qop=\"auth\"")); 8567ba06c71SGlenn Strauss if (nonce_stale) { 8577ba06c71SGlenn Strauss buffer_append_string_len(srv->tmp_buf, CONST_STR_LEN(", stale=true")); 8587ba06c71SGlenn Strauss } 8597ba06c71SGlenn Strauss 8607ba06c71SGlenn Strauss response_header_insert(srv, con, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(srv->tmp_buf)); 8617ba06c71SGlenn Strauss 862f9d58670SStefan Bühler return HANDLER_FINISHED; 8637ba06c71SGlenn Strauss } 8647ba06c71SGlenn Strauss 8657ba06c71SGlenn Strauss static handler_t mod_auth_check_extern(server *srv, connection *con, void *p_d, const struct http_auth_require_t *require, const struct http_auth_backend_t *backend) { 8667ba06c71SGlenn Strauss /* require REMOTE_USER already set */ 8677ba06c71SGlenn Strauss data_string *ds = (data_string *)array_get_element(con->environment, "REMOTE_USER"); 8687ba06c71SGlenn Strauss UNUSED(srv); 8697ba06c71SGlenn Strauss UNUSED(p_d); 8707ba06c71SGlenn Strauss UNUSED(backend); 8717ba06c71SGlenn Strauss if (NULL != ds && http_auth_match_rules(require, ds->value->ptr, NULL, NULL)) { 872f9d58670SStefan Bühler return HANDLER_GO_ON; 873bcdc6a3bSJan Kneschke } else { 8744b3a91e6SGlenn Strauss con->http_status = 401; 8754b3a91e6SGlenn Strauss con->mode = DIRECT; 876bcdc6a3bSJan Kneschke return HANDLER_FINISHED; 8777187271fSStefan Bühler } 878bcdc6a3bSJan Kneschke } 879