842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * mod_auth_digest: MD5 digest authentication
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Originally by Alexei Kosut <akosut@nueva.pvt.k12.ca.us>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Updated to RFC-2617 by Ronald Tschal�r <ronald@innovation.ch>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * based on mod_auth, by Rob McCool and Robert S. Thau
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * This module an updated version of modules/standard/mod_digest.c
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * It is still fairly new and problems may turn up - submit problem
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * reports to the Apache bug-database, or send them directly to me
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * at ronald@innovation.ch.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Open Issues:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * - qop=auth-int (when streams and trailer support available)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * - nonce-format configurability
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * - Proxy-Authorization-Info header is set by this module, but is
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * currently ignored by mod_proxy (needs patch to mod_proxy)
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * - The source of the secret should be run-time directive (with server
f7376afc33a9e035921be9114c0e246820d7c8besf * scope: RSRC_CONF)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * - shared-mem not completely tested yet. Seems to work ok for me,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * but... (definitely won't work on Windoze)
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * - Sharing a realm among multiple servers has following problems:
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * o Server name and port can't be included in nonce-hash
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * (we need two nonce formats, which must be configured explicitly)
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * o Nonce-count check can't be for equal, or then nonce-count checking
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * must be disabled. What we could do is the following:
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * (expected < received) ? set expected = received : issue error
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * The only problem is that it allows replay attacks when somebody
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * captures a packet sent to one server and sends it to another
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * one. Should we add "AuthDigestNcCheck Strict"?
e8f95a682820a599fe41b22977010636be5c2717jim * - expired nonces give amaya fits.
f7376afc33a9e035921be9114c0e246820d7c8besf * - MD5-sess and auth-int are not yet implemented. An incomplete
f7376afc33a9e035921be9114c0e246820d7c8besf * implementation has been removed and can be retrieved from svn history.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* struct to hold the configuration info */
ec486beb201583aafddf7c7ee9009727a3ade0aafielding#define NONCE_LEN (int )(NONCE_TIME_LEN + NONCE_HASH_LEN)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* client list definitions */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron struct hash_entry *next; /* next entry in the bucket */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron unsigned long nonce_count; /* for nonce-count checking */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron char last_nonce[NONCE_LEN+1]; /* for one-time nonce's */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* struct to hold a parsed Authorization header */
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingenum hdr_sts { NO_HEADER, NOT_DIGEST, INVALID, VALID };
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* the following fields are not (directly) from the header */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* (mostly) nonce stuff */
dc80439e9fba60c753cd145cb6799409ffea9b71ronald/* client-list, opaque, and one-time-nonce stuff */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaronstatic apr_time_t *otn_counter; /* one-time-nonce counter */
43997561b2302d13dee973998e77743a3ddd2374trawickstatic const char *client_mutex_type = "authdigest-client";
43997561b2302d13dee973998e77743a3ddd2374trawickstatic const char *opaque_mutex_type = "authdigest-opaque";
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * initialization code
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL, APLOGNO(01756)
678a15e91d6a44569c956445442731bb64a98a63sf "cleaning up shared memory");
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void log_error_and_cleanup(char *msg, apr_status_t sts, server_rec *s)
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, sts, s, APLOGNO(01760)
58c74a790988c0c63b08d15f9af6908b36f3efd8jailletc "%s - all nonce-count checking and one-time nonces "
97d20d37d21b8d427a920e211858172f0a82427epoirierstatic int initialize_tables(server_rec *s, apr_pool_t *ctx)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* set up client list */
97d20d37d21b8d427a920e211858172f0a82427epoirier /* Create the shared memory segment */
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * Create a unique filename using our pid. This information is
97d20d37d21b8d427a920e211858172f0a82427epoirier * stashed in the global variable so the children inherit it.
1124a56faf0228410656abbe08451d7330d906e8trawick client_shm_filename = ap_runtime_dir_relative(ctx, "authdigest_shm");
1124a56faf0228410656abbe08451d7330d906e8trawick client_shm_filename = ap_append_pid(ctx, client_shm_filename, ".");
97d20d37d21b8d427a920e211858172f0a82427epoirier /* Now create that segment */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_ERR, sts, s, APLOGNO(01762)
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim "Failed to create shared memory segment on file %s",
97d20d37d21b8d427a920e211858172f0a82427epoirier log_error_and_cleanup("failed to initialize shm", sts, s);
97d20d37d21b8d427a920e211858172f0a82427epoirier log_error_and_cleanup("failed to initialize rmm", sts, s);
97d20d37d21b8d427a920e211858172f0a82427epoirier client_list = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(*client_list) +
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron log_error_and_cleanup("failed to allocate shared memory", -1, s);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding client_list->table = (client_entry**) (client_list + 1);
024e70e05386a6367eb45d0d1cc406764520ff4cwrowe sts = ap_global_mutex_create(&client_lock, NULL, client_mutex_type, NULL,
29c30db45f6a469017e16b606611e460cc1a1f2caaron log_error_and_cleanup("failed to create lock (client_lock)", sts, s);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* setup opaque */
97d20d37d21b8d427a920e211858172f0a82427epoirier opaque_cntr = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(*opaque_cntr)));
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron log_error_and_cleanup("failed to allocate shared memory", -1, s);
024e70e05386a6367eb45d0d1cc406764520ff4cwrowe sts = ap_global_mutex_create(&opaque_lock, NULL, opaque_mutex_type, NULL,
29c30db45f6a469017e16b606611e460cc1a1f2caaron log_error_and_cleanup("failed to create lock (opaque_lock)", sts, s);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* setup one-time-nonce counter */
97d20d37d21b8d427a920e211858172f0a82427epoirier otn_counter = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(*otn_counter)));
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron log_error_and_cleanup("failed to allocate shared memory", -1, s);
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* no lock here */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* success */
a6b9ed64fdf548c61de9714e2cfb999ec59d149cgstein#endif /* APR_HAS_SHARED_MEMORY */
11f2c481e1d57bedb3f758565307501e9a2730ddtrawickstatic int pre_init(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
43997561b2302d13dee973998e77743a3ddd2374trawick rv = ap_mutex_register(pconf, client_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
f7376afc33a9e035921be9114c0e246820d7c8besf rv = ap_mutex_register(pconf, opaque_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
8ea968d7e798635ab742673e5d5eef35798259c9sf retained = ap_retained_data_create(RETAINED_DATA_ID, SECRET_LEN);
8ea968d7e798635ab742673e5d5eef35798259c9sf ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, APLOGNO(01757)
8ea968d7e798635ab742673e5d5eef35798259c9sf "generating secret for digest authentication");
8ea968d7e798635ab742673e5d5eef35798259c9sf ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL, APLOGNO(01758)
8ea968d7e798635ab742673e5d5eef35798259c9sf "error generating secret");
9d0665da83d1e22c0ea0e5f6f940f70f75bf5237ianhstatic int initialize_module(apr_pool_t *p, apr_pool_t *plog,
e1753aabf5df187b5b04e72a958af4b65b1a125daaron /* initialize_module() will be called twice, and if it's a DSO
e1753aabf5df187b5b04e72a958af4b65b1a125daaron * then all static data from the first call will be lost. Only
e1753aabf5df187b5b04e72a958af4b65b1a125daaron * set up our static data on the second call. */
59d316b83d42d2a07e25c20d8c35a07b369618bdsf if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Note: this stuff is currently fixed for the lifetime of the server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * i.e. even across restarts. This means that A) any shmem-size
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * configuration changes are ignored, and B) certain optimizations,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * such as only allocating the smallest necessary entry for each
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * client, can't be done. However, the alternative is a nightmare:
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm * we can't call apr_shm_destroy on a graceful restart because there
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * will be children using the tables, and we also don't know when the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * last child dies. Therefore we can never clean up the old stuff,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * creating a creeping memory leak.
97d20d37d21b8d427a920e211858172f0a82427epoirier /* Call cleanup_tables on exit or restart */
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_pool_cleanup_register(p, NULL, cleanup_tables, apr_pool_cleanup_null);
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron#endif /* APR_HAS_SHARED_MEMORY */
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void initialize_child(apr_pool_t *p, server_rec *s)
97d20d37d21b8d427a920e211858172f0a82427epoirier /* Get access to rmm in child */
97d20d37d21b8d427a920e211858172f0a82427epoirier log_error_and_cleanup("failed to attach to rmm", sts, s);
29c30db45f6a469017e16b606611e460cc1a1f2caaron log_error_and_cleanup("failed to create lock (client_lock)", sts, s);
29c30db45f6a469017e16b606611e460cc1a1f2caaron log_error_and_cleanup("failed to create lock (opaque_lock)", sts, s);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * configuration code
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *create_digest_dir_config(apr_pool_t *p, char *dir)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm conf = (digest_config_rec *) apr_pcalloc(p, sizeof(digest_config_rec));
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *set_realm(cmd_parms *cmd, void *config, const char *realm)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding digest_config_rec *conf = (digest_config_rec *) config;
c7de1955eb0eaeabf7042902476397692672d549sf /* check that we got random numbers */
c7de1955eb0eaeabf7042902476397692672d549sf for (i = 0; i < SECRET_LEN; i++) {
c7de1955eb0eaeabf7042902476397692672d549sf if (secret[i] != 0)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* The core already handles the realm, but it's just too convenient to
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * grab it ourselves too and cache some setups. However, we need to
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * let the core get at it too, which is why we decline at the end -
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * this relies on the fact that http_core is last in the list.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* we precompute the part of the nonce hash that is constant (well,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the host:port would be too, but that varies for .htaccess files
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * and directives outside a virtual host section)
c7de1955eb0eaeabf7042902476397692672d549sf apr_sha1_update_binary(&conf->nonce_ctx, secret, SECRET_LEN);
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_sha1_update_binary(&conf->nonce_ctx, (const unsigned char *) realm,
322b350d0f1ac750b112ec15481a33efc92d182cjerenkrantzstatic const char *add_authn_provider(cmd_parms *cmd, void *config,
322b350d0f1ac750b112ec15481a33efc92d182cjerenkrantz const char *arg)
322b350d0f1ac750b112ec15481a33efc92d182cjerenkrantz digest_config_rec *conf = (digest_config_rec*)config;
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz newp = apr_pcalloc(cmd->pool, sizeof(authn_provider_list));
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz /* lookup and cache the actual provider now */
e9f8410b788ef1e6f1baed6c706ffdf3da395a16jerenkrantz newp->provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz /* by the time they use it, the provider should be loaded and
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz registered with us. */
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz "Unknown Authn provider: %s",
6e45872b4a23d493887830d82a2759b4c00b10b2wsanchez /* if it doesn't provide the appropriate function, reject it */
6e45872b4a23d493887830d82a2759b4c00b10b2wsanchez "The '%s' Authn provider doesn't support "
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz /* Add it to the list now. */
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *set_qop(cmd_parms *cmd, void *config, const char *op)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding digest_config_rec *conf = (digest_config_rec *) config;
44a02fb58380bb801585f586ad3c7569de11840dpoirier return "AuthDigestQop auth-int is not implemented";
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron return apr_pstrcat(cmd->pool, "Unrecognized qop: ", op, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *set_nonce_lifetime(cmd_parms *cmd, void *config,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron const char *t)
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (endptr < (t+strlen(t)) && !apr_isspace(*endptr)) {
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron "Invalid time in AuthDigestNonceLifetime: ",
e0fe4de2016336428729a620ac0034cd1198ad7awrowe ((digest_config_rec *) config)->nonce_lifetime = apr_time_from_sec(lifetime);
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *set_nonce_format(cmd_parms *cmd, void *config,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron const char *fmt)
f7376afc33a9e035921be9114c0e246820d7c8besf return "AuthDigestNonceFormat is not implemented";
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *set_nc_check(cmd_parms *cmd, void *config, int flag)
e5db2522dbe503cbf5399094b6239c88c246a8c5poirier return "AuthDigestNcCheck: ERROR: nonce-count checking "
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron "is not supported on platforms without shared-memory "
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *set_algorithm(cmd_parms *cmd, void *config, const char *alg)
e5db2522dbe503cbf5399094b6239c88c246a8c5poirier return "AuthDigestAlgorithm: ERROR: algorithm `MD5-sess' "
f7376afc33a9e035921be9114c0e246820d7c8besf "is not implemented";
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron return apr_pstrcat(cmd->pool, "Invalid algorithm in AuthDigestAlgorithm: ", alg, NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic const char *set_uri_list(cmd_parms *cmd, void *config, const char *uri)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding digest_config_rec *c = (digest_config_rec *) config;
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron c->uri_list = apr_pstrcat(cmd->pool, c->uri_list, " ", uri, "\"", NULL);
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron c->uri_list = apr_pstrcat(cmd->pool, ", domain=\"", uri, "\"", NULL);
dc80439e9fba60c753cd145cb6799409ffea9b71ronaldstatic const char *set_shmem_size(cmd_parms *cmd, void *config,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron const char *size_str)
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (*endptr == '\0' || *endptr == 'b' || *endptr == 'B') {
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron return apr_pstrcat(cmd->pool, "Invalid size in AuthDigestShmemSize: ",
dc80439e9fba60c753cd145cb6799409ffea9b71ronald min = sizeof(*client_list) + sizeof(client_entry*) + sizeof(client_entry);
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron return apr_psprintf(cmd->pool, "size in AuthDigestShmemSize too small: "
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron (sizeof(client_entry*) + HASH_DEPTH * sizeof(client_entry));
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, APLOGNO(01763)
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "The authentication realm (e.g. \"Members Only\")"),
1df208eeb79b689410598b0f6fd1ac3fb2401c44nd AP_INIT_ITERATE("AuthDigestProvider", add_authn_provider, NULL, OR_AUTHCFG,
322b350d0f1ac750b112ec15481a33efc92d182cjerenkrantz "specify the auth providers for a directory or location"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_ITERATE("AuthDigestQop", set_qop, NULL, OR_AUTHCFG,
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "A list of quality-of-protection options"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_TAKE1("AuthDigestNonceLifetime", set_nonce_lifetime, NULL, OR_AUTHCFG,
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "Maximum lifetime of the server nonce (seconds)"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_TAKE1("AuthDigestNonceFormat", set_nonce_format, NULL, OR_AUTHCFG,
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "The format to use when generating the server nonce"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_FLAG("AuthDigestNcCheck", set_nc_check, NULL, OR_AUTHCFG,
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "Whether or not to check the nonce-count sent by the client"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_TAKE1("AuthDigestAlgorithm", set_algorithm, NULL, OR_AUTHCFG,
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "The algorithm used for the hash calculation"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_ITERATE("AuthDigestDomain", set_uri_list, NULL, OR_AUTHCFG,
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "A list of URI's which belong to the same protection space as the current URI"),
e8f95a682820a599fe41b22977010636be5c2717jim AP_INIT_TAKE1("AuthDigestShmemSize", set_shmem_size, NULL, RSRC_CONF,
090a954a1fe65b29a6f4a696f0136ef12ea0f1b1rbb "The amount of shared memory to allocate for keeping track of clients"),
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * client list code
b6832863054a2d09233ce92945e0faceb932a620jwoolley * Each client is assigned a number, which is transferred in the opaque
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * field of the WWW-Authenticate and Authorization headers. The number
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * is just a simple counter which is incremented for each new client.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Clients can't forge this number because it is hashed up into the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * server nonce, and that is checked.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * The clients are kept in a simple hash table, which consists of an
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * array of client_entry's, each with a linked list of entries hanging
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * off it. The client's number modulo the size of the array gives the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * bucket number.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * The clients are garbage collected whenever a new client is allocated
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * but there is not enough space left in the shared memory segment. A
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * simple semi-LRU is used for this: whenever a client entry is accessed
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * it is moved to the beginning of the linked list in its bucket (this
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * also makes for faster lookups for current clients). The garbage
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * collecter then just removes the oldest entry (i.e. the one at the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * end of the list) in each bucket.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * The main advantages of the above scheme are that it's easy to implement
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * and it keeps the hash table evenly balanced (i.e. same number of entries
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * in each bucket). The major disadvantage is that you may be throwing
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * entries out which are in active use. This is not tragic, as these
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * clients will just be sent a new client id (opaque field) and nonce
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * with a stale=true (i.e. it will just look like the nonce expired,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * thereby forcing an extra round trip). If the shared memory segment
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * has enough headroom over the current client set size then this should
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * not occur too often.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * To help tune the size of the shared memory segment (and see if the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * above algorithm is really sufficient) a set of counters is kept
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * indicating the number of clients held, the number of garbage collected
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * clients, and the number of erroneously purged clients. These are printed
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * out at each garbage collection run. Note that access to the counters is
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * not synchronized because they are just indicaters, and whether they are
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * off by a few doesn't matter; and for the same reason no attempt is made
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * to guarantee the num_renewed is correct in the face of clients spoofing
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the opaque field.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Get the client given its client number (the key). Returns the entry,
b29f87f4b6c6886a04dccc296177a7033f70dfedtrawick * or NULL if it's not found.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Access to the list itself is synchronized via locks. However, access
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * to the entry returned by get_client() is NOT synchronized. This means
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * that there are potentially problems if a client uses multiple,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * simultaneous connections to access url's within the same protection
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * space. However, these problems are not new: when using multiple
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * connections you have no guarantee of the order the requests are
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * processed anyway, so you have problems with the nonce-count and
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * one-time nonces anyway.
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic client_entry *get_client(unsigned long key, const request_rec *r)
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (entry && prev) { /* move entry to front of list */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01764)
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01765)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* A simple garbage-collecter to remove unused clients. It removes the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * last entry in each bucket and updates the counters. Returns the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * number of removed entries.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* garbage collect all last entries */
97d20d37d21b8d427a920e211858172f0a82427epoirier apr_rmm_free(client_rmm, apr_rmm_offset_get(client_rmm, entry));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* update counters and log */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Add a new client to the list. Returns the entry if successful, NULL
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * otherwise. This triggers the garbage collection if memory is low.
dc80439e9fba60c753cd145cb6799409ffea9b71ronaldstatic client_entry *add_client(unsigned long key, client_entry *info,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* try to allocate a new entry */
97d20d37d21b8d427a920e211858172f0a82427epoirier entry = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(client_entry)));
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(01766)
678a15e91d6a44569c956445442731bb64a98a63sf "gc'd %ld client entries. Total new clients: "
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron "%ld; Total removed clients: %ld; Total renewed clients: "
97d20d37d21b8d427a920e211858172f0a82427epoirier entry = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(client_entry)));
97d20d37d21b8d427a920e211858172f0a82427epoirier "unable to allocate new auth_digest client");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* now add the entry */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01768)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Authorization header parser code
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* Parse the Authorization header, if it exists */
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int get_digest_rec(request_rec *r, digest_header_rec *resp)
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron ? "Proxy-Authorization"
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron : "Authorization");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding resp->scheme = ap_getword_white(r->pool, &auth_line);
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* find key */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron && auth_line[0] != '\0' && !apr_isspace(auth_line[0])) {
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* find value */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron while (auth_line[0] != '\"' && auth_line[0] != '\0') {
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron else { /* token */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (!resp->username || !resp->realm || !resp->nonce || !resp->uri
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron || (resp->message_qop && (!resp->cnonce || !resp->nonce_count))) {
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron resp->opaque_num = (unsigned long) strtol(resp->opaque, NULL, 16);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* Because the browser may preemptively send auth info, incrementing the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * nonce-count when it does, and because the client does not get notified
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * if the URI didn't need authentication after all, we need to be sure to
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * update the nonce-count each time we receive an Authorization header no
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * matter what the final outcome of the request. Furthermore this is a
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * convenient place to get the request-uri (before any subrequests etc
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * are initiated) and to initialize the request_config.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Note that this must be called after mod_proxy had its go so that
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * r->proxyreq is set correctly.
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm resp = apr_pcalloc(r->pool, sizeof(digest_header_rec));
138c8f7cb8254e035c6f45288e3909cd9c21be5cmartin ap_set_module_config(r->request_config, &auth_digest_module, resp);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Nonce generation code
dc80439e9fba60c753cd145cb6799409ffea9b71ronald/* The hash part of the nonce is a SHA-1 hash of the time, realm, server host
dc80439e9fba60c753cd145cb6799409ffea9b71ronald * and port, opaque, and our secret.
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic void gen_nonce_hash(char *hash, const char *timestr, const char *opaque,
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_sha1_update_binary(&ctx, (const unsigned char *) server->server_hostname,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron strlen(server->server_hostname));
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_sha1_update_binary(&ctx, (const unsigned char *) &server->port,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron sizeof(server->port));
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_sha1_update_binary(&ctx, (const unsigned char *) timestr, strlen(timestr));
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron apr_sha1_update_binary(&ctx, (const unsigned char *) opaque,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* The nonce has the format b64(time)+hash .
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic const char *gen_nonce(apr_pool_t *p, apr_time_t now, const char *opaque,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* this counter is not synch'd, because it doesn't really matter
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron * if it counts exactly.
953ffc322984217eb04f911e041b40b7a91b54aawrowe /* XXX: WHAT IS THIS CONSTANT? */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding gen_nonce_hash(nonce+NONCE_TIME_LEN, nonce, opaque, server, conf);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Opaque and hash-table management
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Generate a new client entry, add it to the list, and return the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * entry. Returns NULL if failed.
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic client_entry *gen_client(const request_rec *r)
dc80439e9fba60c753cd145cb6799409ffea9b71ronald if (!(entry = add_client(op, &new_entry, r->server))) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01769)
678a15e91d6a44569c956445442731bb64a98a63sf "failed to allocate client entry - ignoring client");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Authorization challenge generation code (for WWW-Authenticate)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic const char *ltox(apr_pool_t *p, unsigned long num)
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (num != 0) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic void note_digest_auth_failure(request_rec *r,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *qop, *opaque, *opaque_param, *domain, *nonce;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Setup qop */
fd9b4a0b713bc4816b9b80f52d567f5c2ac06bafsf else if (!strcasecmp(*(const char **)(conf->qop_list->elts), "none")) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Setup opaque */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* new client */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* client info was gc'd */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* we're generating a new nonce, so reset the nonce-count */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron opaque_param = apr_pstrcat(r->pool, ", opaque=\"", opaque, "\"", NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Setup nonce */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding nonce = gen_nonce(r->pool, r->request_time, opaque, r->server, conf);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* setup domain attribute. We want to send this attribute wherever
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * possible so that the client won't send the Authorization header
b76a31daaa6e83bb0fd627a04f20e82bffcf1df4poirier * unnecessarily (it's usually > 200 bytes!).
820be4f3a1a5b0565a072b0bf582d19fb791b68dnd /* don't send domain
820be4f3a1a5b0565a072b0bf582d19fb791b68dnd * - for proxy requests
7af19efc4667363f74d332a8d010b49e88d56fd5trawick * - if it's not specified
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron "nonce=\"%s\", algorithm=%s%s%s%s%s",
a44d29a3794110c558c940bd903a1930d717a7d7sfstatic int hook_note_digest_auth_failure(request_rec *r, const char *auth_type)
a44d29a3794110c558c940bd903a1930d717a7d7sf /* get the client response and mark */
a44d29a3794110c558c940bd903a1930d717a7d7sf resp = (digest_header_rec *) ap_get_module_config(mainreq->request_config,
a44d29a3794110c558c940bd903a1930d717a7d7sf /* get our conf */
a44d29a3794110c558c940bd903a1930d717a7d7sf conf = (digest_config_rec *) ap_get_module_config(r->per_dir_config,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Authorization header verification code
f78505c7d260473bf11002f5028186f27d0ed8a0geoffstatic authn_status get_hash(request_rec *r, const char *user,
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz /* For now, if a provider isn't set, we'll be nice and use the file
e9f8410b788ef1e6f1baed6c706ffdf3da395a16jerenkrantz provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01770)
6e45872b4a23d493887830d82a2759b4c00b10b2wsanchez "No Authn provider configured");
33b444dee16ba708960701c5b1ceb69df1548017pquerna apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, AUTHN_DEFAULT_PROVIDER);
33b444dee16ba708960701c5b1ceb69df1548017pquerna apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, current_provider->provider_name);
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz /* We expect the password to be md5 hash of user:realm:password */
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz auth_result = provider->get_realm_hash(r, user, conf->realm,
69c1e98e10528ac12ed3f56a811c57fc1c9e31e5bnicholes apr_table_unset(r->notes, AUTHN_PROVIDER_NAME_NOTE);
4d4953238984d603c574511f35ef8d5bdbc70862wsanchez /* Something occured. Stop checking. */
bdfba727693ab86e9914ca90af68e62896946755jerenkrantz /* If we're not really configured for providers, stop now. */
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int check_nc(const request_rec *r, const digest_header_rec *resp,
e5db2522dbe503cbf5399094b6239c88c246a8c5poirier /* Shouldn't happen, but just in case... */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01771)
678a15e91d6a44569c956445442731bb64a98a63sf "cannot check nonce count without shared memory");
fd9b4a0b713bc4816b9b80f52d567f5c2ac06bafsf !strcasecmp(*(const char **)(conf->qop_list->elts), "none")) {
ec8b1faa56744b338f6d6421144b56c2bb3faae6poirier /* qop is none, client must not send a nonce count */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01772)
678a15e91d6a44569c956445442731bb64a98a63sf "invalid nc %s received - no nonce count allowed when qop=none",
ec8b1faa56744b338f6d6421144b56c2bb3faae6poirier /* qop is none, cannot check nonce count */
10a4cdd68ef1ca0e54af296fe1d08ac00150c90bwrowe if (endptr < (snc+strlen(snc)) && !apr_isspace(*endptr)) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01773)
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01774)
678a15e91d6a44569c956445442731bb64a98a63sf "Warning, possible replay attack: nonce-count "
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int check_nonce(request_rec *r, digest_header_rec *resp,
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01775)
678a15e91d6a44569c956445442731bb64a98a63sf "invalid nonce %s received - length is not %d",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding gen_nonce_hash(hash, resp->nonce, resp->opaque, r->server, conf);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01776)
678a15e91d6a44569c956445442731bb64a98a63sf "invalid nonce %s received - hash is not %s",
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01777)
678a15e91d6a44569c956445442731bb64a98a63sf "invalid nonce %s received - user attempted "
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_INFO, 0,r, APLOGNO(01778)
678a15e91d6a44569c956445442731bb64a98a63sf "user %s: nonce expired (%.2f seconds old "
e8f95a682820a599fe41b22977010636be5c2717jim "- max lifetime %.2f) - sending new nonce",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (conf->nonce_lifetime == 0 && resp->client) {
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (memcmp(resp->client->last_nonce, resp->nonce, NONCE_LEN)) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01779)
678a15e91d6a44569c956445442731bb64a98a63sf "user %s: one-time-nonce mismatch - sending "
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* else (lifetime < 0) => never expires */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* The actual MD5 code... whee */
dc80439e9fba60c753cd145cb6799409ffea9b71ronald/* RFC-2069 */
7ddfd45e4d3d13de264931df8eb27ee7619fdb0ejerenkrantz ha2 = ap_md5(r->pool, (unsigned char *)apr_pstrcat(r->pool, resp->method, ":",
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron (unsigned char *)apr_pstrcat(r->pool, ha1, ":", resp->nonce,
dc80439e9fba60c753cd145cb6799409ffea9b71ronald/* RFC-2617 */
f7376afc33a9e035921be9114c0e246820d7c8besf a2 = apr_pstrcat(r->pool, resp->method, ":", resp->uri, NULL);
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron (unsigned char *)apr_pstrcat(r->pool, ha1, ":", resp->nonce,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* These functions return 0 if client is OK, and proper error status
000b67449410515eac43e76ef6667915bfd4d2abgstein * if not... either HTTP_UNAUTHORIZED, if we made a check, and it failed, or
000b67449410515eac43e76ef6667915bfd4d2abgstein * HTTP_INTERNAL_SERVER_ERROR, if things are so totally confused that we
000b67449410515eac43e76ef6667915bfd4d2abgstein * couldn't figure out how to tell if the client is authorized or not.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * If they return DECLINED, and all other modules also decline, that's
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * treated by the server core as a configuration error, logged and
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * reported as such.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* Determine user ID, and check if the attributes are correct, if it
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * really is that user, if the nonce is correct, etc.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *t;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* do we require Digest auth for this URI? */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (!(t = ap_auth_type(r)) || strcasecmp(t, "Digest")) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01780)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* get the client response and mark */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding resp = (digest_header_rec *) ap_get_module_config(mainreq->request_config,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* get our conf */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conf = (digest_config_rec *) ap_get_module_config(r->per_dir_config,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* check for existence and syntax of Auth header */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01781)
678a15e91d6a44569c956445442731bb64a98a63sf "client used wrong authentication scheme `%s': %s",
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01782)
678a15e91d6a44569c956445442731bb64a98a63sf "missing user, realm, nonce, uri, digest, "
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron "cnonce, or nonce_count in authorization header: %s",
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* else (resp->auth_hdr_sts == NO_HEADER) */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* check the auth attributes */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* Hmm, the simple match didn't work (probably a proxy modified the
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron * request-uri), so lets do a more sophisticated match
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron copy_uri_components(&r_uri, resp->psd_request_uri, r);
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (apr_uri_parse(r->pool, resp->uri, &d_uri) != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01783)
678a15e91d6a44569c956445442731bb64a98a63sf "invalid uri <%s> in Authorization header",
e8f95a682820a599fe41b22977010636be5c2717jim /* MSIE compatibility hack. MSIE has some RFC issues - doesn't
781888a651637edc0b043a6787cb0c2acf30a187geoff * include the query string in the uri Authorization component
781888a651637edc0b043a6787cb0c2acf30a187geoff * or when computing the response component. the second part
781888a651637edc0b043a6787cb0c2acf30a187geoff * works out ok, since we can hash the header and get the same
781888a651637edc0b043a6787cb0c2acf30a187geoff * result. however, the uri from the request line won't match
e8f95a682820a599fe41b22977010636be5c2717jim * the uri Authorization component since the header lacks the
781888a651637edc0b043a6787cb0c2acf30a187geoff * query string, leaving us incompatable with a (broken) MSIE.
781888a651637edc0b043a6787cb0c2acf30a187geoff * the workaround is to fake a query string match if in the proper
781888a651637edc0b043a6787cb0c2acf30a187geoff * environment - BrowserMatch MSIE, for example. the cool thing
e8f95a682820a599fe41b22977010636be5c2717jim * is that if MSIE ever fixes itself the simple match ought to
781888a651637edc0b043a6787cb0c2acf30a187geoff * work and this code won't be reached anyway, even if the
781888a651637edc0b043a6787cb0c2acf30a187geoff * environment is set.
781888a651637edc0b043a6787cb0c2acf30a187geoff "AuthDigestEnableQueryStringHack")) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01784)
781888a651637edc0b043a6787cb0c2acf30a187geoff "applying AuthDigestEnableQueryStringHack "
60f406441b71be7779bd5794e9745ab0c60bc336jorton if (!r_uri.hostinfo || strcmp(resp->uri, r_uri.hostinfo)) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01785)
678a15e91d6a44569c956445442731bb64a98a63sf "uri mismatch - <%s> does not match "
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* check hostname matches, if present */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* check port matches, if present */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* check that server-port is default port if no port present */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron && !d_uri.port_str && r_uri.port != ap_default_port(r))
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* check that path matches */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* either exact match */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* or '*' matches empty path in scheme://host */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron && !(d_uri.path && !r_uri.path && resp->psd_request_uri->hostname
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* check that query matches */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01786)
678a15e91d6a44569c956445442731bb64a98a63sf "uri mismatch - <%s> does not match "
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron "request-uri <%s>", resp->uri, resp->raw_request_uri);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01787)
678a15e91d6a44569c956445442731bb64a98a63sf "received invalid opaque - got `%s'",
aa6cb33e0279a72e5b5f482871844ebd85dfd527minfrin ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02533)
aa6cb33e0279a72e5b5f482871844ebd85dfd527minfrin "realm mismatch - got `%s' but no realm specified",
aa6cb33e0279a72e5b5f482871844ebd85dfd527minfrin if (!resp->realm || strcmp(resp->realm, conf->realm)) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01788)
678a15e91d6a44569c956445442731bb64a98a63sf "realm mismatch - got `%s' but expected `%s'",
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01789)
678a15e91d6a44569c956445442731bb64a98a63sf "unknown algorithm `%s' received: %s",
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01790)
678a15e91d6a44569c956445442731bb64a98a63sf "user `%s' in realm `%s' not found: %s",
f78505c7d260473bf11002f5028186f27d0ed8a0geoff /* we have a password, so continue */
f769c33501f474aed3e0f7c769477c8c4f478783geoff /* authentication denied in the provider before attempting a match */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01791)
678a15e91d6a44569c956445442731bb64a98a63sf "user `%s' in realm `%s' denied by provider: %s",
f78505c7d260473bf11002f5028186f27d0ed8a0geoff /* AUTH_GENERAL_ERROR (or worse)
f78505c7d260473bf11002f5028186f27d0ed8a0geoff * We'll assume that the module has already said what its error
f78505c7d260473bf11002f5028186f27d0ed8a0geoff * was in the logs.
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* old (rfc-2069) style digest */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (strcmp(resp->digest, old_digest(r, resp, conf->ha1))) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01792)
fd9b4a0b713bc4816b9b80f52d567f5c2ac06bafsf const char **tmp = (const char **)(conf->qop_list->elts);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01793)
678a15e91d6a44569c956445442731bb64a98a63sf "invalid qop `%s' received: %s",
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* we failed to allocate a client struct */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01794)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Note: this check is done last so that a "stale=true" can be
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding generated if the nonce is old */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Authorization-Info header code
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron (digest_config_rec *) ap_get_module_config(r->per_dir_config,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron (digest_header_rec *) ap_get_module_config(r->request_config,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if (resp == NULL || !resp->needed_auth || conf == NULL) {
00231c86206cfcb141a0a21fa44ff7d4c590208ejorton /* 2069-style entity-digest is not supported (it's too hard, and
00231c86206cfcb141a0a21fa44ff7d4c590208ejorton * there are no clients which support 2069 but not 2617). */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* setup nextnonce
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* send nextnonce if current nonce will expire in less than 30 secs */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron if ((r->request_time - resp->nonce_time) > (conf->nonce_lifetime-NEXTNONCE_DELTA)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else if (conf->nonce_lifetime == 0 && resp->client) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding const char *nonce = gen_nonce(r->pool, 0, resp->opaque, r->server,
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron nextnonce = apr_pstrcat(r->pool, ", nextnonce=\"", nonce, "\"", NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* else nonce never expires, hence no nextnonce */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* do rfc-2069 digest
fd9b4a0b713bc4816b9b80f52d567f5c2ac06bafsf !strcasecmp(*(const char **)(conf->qop_list->elts), "none")
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* use only RFC-2069 format */
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* calculate rspauth attribute
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron /* assemble Authentication-Info header
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron ? "Proxy-Authentication-Info"
0ec6007a40ac877a7c8d87767ca8e306d89f6595aaron : "Authentication-Info",
d0a225bdac006f3361e80bfc1be7e6f9b0e81f80ronald static const char * const cfgPost[]={ "http_core.c", NULL };
d0a225bdac006f3361e80bfc1be7e6f9b0e81f80ronald static const char * const parsePre[]={ "mod_proxy.c", NULL };
11f2c481e1d57bedb3f758565307501e9a2730ddtrawick ap_hook_pre_config(pre_init, NULL, NULL, APR_HOOK_MIDDLE);
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe ap_hook_post_config(initialize_module, NULL, cfgPost, APR_HOOK_MIDDLE);
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe ap_hook_child_init(initialize_child, NULL, NULL, APR_HOOK_MIDDLE);
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe ap_hook_post_read_request(parse_hdr_and_update_nc, parsePre, NULL, APR_HOOK_MIDDLE);
a72211e92bab814bfa28ee086ca9b2a1a6095c92chrisd ap_hook_check_authn(authenticate_digest_user, NULL, NULL, APR_HOOK_MIDDLE,
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe ap_hook_fixups(add_auth_info, NULL, NULL, APR_HOOK_MIDDLE);