mod_auth_digest.c revision 58c74a790988c0c63b08d15f9af6908b36f3efd8
1982fbecb99335961659b9961d47f2425bf142abgryzor/* Licensed to the Apache Software Foundation (ASF) under one or more
1982fbecb99335961659b9961d47f2425bf142abgryzor * contributor license agreements. See the NOTICE file distributed with
1982fbecb99335961659b9961d47f2425bf142abgryzor * this work for additional information regarding copyright ownership.
a99c5d4cc3cab6a62b04d52000dbc22ce1fa2d94coar * The ASF licenses this file to You under the Apache License, Version 2.0
1982fbecb99335961659b9961d47f2425bf142abgryzor * (the "License"); you may not use this file except in compliance with
1982fbecb99335961659b9961d47f2425bf142abgryzor * the License. You may obtain a copy of the License at
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * http://www.apache.org/licenses/LICENSE-2.0
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * Unless required by applicable law or agreed to in writing, software
1982fbecb99335961659b9961d47f2425bf142abgryzor * distributed under the License is distributed on an "AS IS" BASIS,
1982fbecb99335961659b9961d47f2425bf142abgryzor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1982fbecb99335961659b9961d47f2425bf142abgryzor * See the License for the specific language governing permissions and
1982fbecb99335961659b9961d47f2425bf142abgryzor * limitations under the License.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
1982fbecb99335961659b9961d47f2425bf142abgryzor * mod_auth_digest: MD5 digest authentication
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * Originally by Alexei Kosut <akosut@nueva.pvt.k12.ca.us>
1982fbecb99335961659b9961d47f2425bf142abgryzor * Updated to RFC-2617 by Ronald Tschal�r <ronald@innovation.ch>
1982fbecb99335961659b9961d47f2425bf142abgryzor * based on mod_auth, by Rob McCool and Robert S. Thau
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * This module an updated version of modules/standard/mod_digest.c
1982fbecb99335961659b9961d47f2425bf142abgryzor * It is still fairly new and problems may turn up - submit problem
1982fbecb99335961659b9961d47f2425bf142abgryzor * reports to the Apache bug-database, or send them directly to me
1982fbecb99335961659b9961d47f2425bf142abgryzor * at ronald@innovation.ch.
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * Open Issues:
1982fbecb99335961659b9961d47f2425bf142abgryzor * - qop=auth-int (when streams and trailer support available)
1982fbecb99335961659b9961d47f2425bf142abgryzor * - nonce-format configurability
1982fbecb99335961659b9961d47f2425bf142abgryzor * - Proxy-Authorization-Info header is set by this module, but is
1982fbecb99335961659b9961d47f2425bf142abgryzor * currently ignored by mod_proxy (needs patch to mod_proxy)
1982fbecb99335961659b9961d47f2425bf142abgryzor * - The source of the secret should be run-time directive (with server
1982fbecb99335961659b9961d47f2425bf142abgryzor * scope: RSRC_CONF)
1982fbecb99335961659b9961d47f2425bf142abgryzor * - shared-mem not completely tested yet. Seems to work ok for me,
1982fbecb99335961659b9961d47f2425bf142abgryzor * but... (definitely won't work on Windoze)
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis * - Sharing a realm among multiple servers has following problems:
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis * o Server name and port can't be included in nonce-hash
1982fbecb99335961659b9961d47f2425bf142abgryzor * (we need two nonce formats, which must be configured explicitly)
1982fbecb99335961659b9961d47f2425bf142abgryzor * o Nonce-count check can't be for equal, or then nonce-count checking
1982fbecb99335961659b9961d47f2425bf142abgryzor * must be disabled. What we could do is the following:
1982fbecb99335961659b9961d47f2425bf142abgryzor * (expected < received) ? set expected = received : issue error
1982fbecb99335961659b9961d47f2425bf142abgryzor * The only problem is that it allows replay attacks when somebody
1982fbecb99335961659b9961d47f2425bf142abgryzor * captures a packet sent to one server and sends it to another
1982fbecb99335961659b9961d47f2425bf142abgryzor * one. Should we add "AuthDigestNcCheck Strict"?
1982fbecb99335961659b9961d47f2425bf142abgryzor * - expired nonces give amaya fits.
1982fbecb99335961659b9961d47f2425bf142abgryzor * - MD5-sess and auth-int are not yet implemented. An incomplete
1982fbecb99335961659b9961d47f2425bf142abgryzor * implementation has been removed and can be retrieved from svn history.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_sha1.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_base64.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_lib.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_time.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_errno.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_global_mutex.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_strings.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#define APR_WANT_STRFUNC
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_want.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "ap_config.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "httpd.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "http_config.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "http_core.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "http_request.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "http_log.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "http_protocol.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "apr_uri.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "util_md5.h"
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis#include "util_mutex.h"
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis#include "apr_shm.h"
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis#include "apr_rmm.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "ap_provider.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#include "mod_auth.h"
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#if APR_HAVE_UNISTD_H
1982fbecb99335961659b9961d47f2425bf142abgryzor#include <unistd.h>
1982fbecb99335961659b9961d47f2425bf142abgryzor#endif
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* struct to hold the configuration info */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzortypedef struct digest_config_struct {
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *dir_name;
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis authn_provider_list *providers;
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis const char *realm;
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis apr_array_header_t *qop_list;
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis apr_sha1_ctx_t nonce_ctx;
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis apr_time_t nonce_lifetime;
1982fbecb99335961659b9961d47f2425bf142abgryzor int check_nc;
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis const char *algorithm;
1982fbecb99335961659b9961d47f2425bf142abgryzor char *uri_list;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *ha1;
1982fbecb99335961659b9961d47f2425bf142abgryzor} digest_config_rec;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#define DFLT_ALGORITHM "MD5"
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#define DFLT_NONCE_LIFE apr_time_from_sec(300)
1982fbecb99335961659b9961d47f2425bf142abgryzor#define NEXTNONCE_DELTA apr_time_from_sec(30)
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#define NONCE_TIME_LEN (((sizeof(apr_time_t)+2)/3)*4)
1982fbecb99335961659b9961d47f2425bf142abgryzor#define NONCE_HASH_LEN (2*APR_SHA1_DIGESTSIZE)
1982fbecb99335961659b9961d47f2425bf142abgryzor#define NONCE_LEN (int )(NONCE_TIME_LEN + NONCE_HASH_LEN)
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#define SECRET_LEN 20
1982fbecb99335961659b9961d47f2425bf142abgryzor#define RETAINED_DATA_ID "mod_auth_digest"
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* client list definitions */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzortypedef struct hash_entry {
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long key; /* the key for this entry */
1982fbecb99335961659b9961d47f2425bf142abgryzor struct hash_entry *next; /* next entry in the bucket */
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long nonce_count; /* for nonce-count checking */
1982fbecb99335961659b9961d47f2425bf142abgryzor char last_nonce[NONCE_LEN+1]; /* for one-time nonce's */
1982fbecb99335961659b9961d47f2425bf142abgryzor} client_entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic struct hash_table {
1982fbecb99335961659b9961d47f2425bf142abgryzor client_entry **table;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long tbl_len;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long num_entries;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long num_created;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long num_removed;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long num_renewed;
1982fbecb99335961659b9961d47f2425bf142abgryzor} *client_list;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* struct to hold a parsed Authorization header */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorenum hdr_sts { NO_HEADER, NOT_DIGEST, INVALID, VALID };
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzortypedef struct digest_header_struct {
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *scheme;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *realm;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *username;
1982fbecb99335961659b9961d47f2425bf142abgryzor char *nonce;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *uri;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *method;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *digest;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *algorithm;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *cnonce;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *opaque;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long opaque_num;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *message_qop;
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *nonce_count;
1982fbecb99335961659b9961d47f2425bf142abgryzor /* the following fields are not (directly) from the header */
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *raw_request_uri;
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_uri_t *psd_request_uri;
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_time_t nonce_time;
1982fbecb99335961659b9961d47f2425bf142abgryzor enum hdr_sts auth_hdr_sts;
1982fbecb99335961659b9961d47f2425bf142abgryzor int needed_auth;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_entry *client;
1982fbecb99335961659b9961d47f2425bf142abgryzor} digest_header_rec;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* (mostly) nonce stuff */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzortypedef union time_union {
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_time_t time;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned char arr[sizeof(apr_time_t)];
1982fbecb99335961659b9961d47f2425bf142abgryzor} time_rec;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic unsigned char *secret;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* client-list, opaque, and one-time-nonce stuff */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic apr_shm_t *client_shm = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic apr_rmm_t *client_rmm = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic unsigned long *opaque_cntr;
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic apr_time_t *otn_counter; /* one-time-nonce counter */
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic apr_global_mutex_t *client_lock = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic apr_global_mutex_t *opaque_lock = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *client_mutex_type = "authdigest-client";
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *opaque_mutex_type = "authdigest-opaque";
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *client_shm_filename;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#define DEF_SHMEM_SIZE 1000L /* ~ 12 entries */
1982fbecb99335961659b9961d47f2425bf142abgryzor#define DEF_NUM_BUCKETS 15L
1982fbecb99335961659b9961d47f2425bf142abgryzor#define HASH_DEPTH 5
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic apr_size_t shmem_size = DEF_SHMEM_SIZE;
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic unsigned long num_buckets = DEF_NUM_BUCKETS;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzormodule AP_MODULE_DECLARE_DATA auth_digest_module;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
1982fbecb99335961659b9961d47f2425bf142abgryzor * initialization code
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic apr_status_t cleanup_tables(void *not_used)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL, APLOGNO(01756)
1982fbecb99335961659b9961d47f2425bf142abgryzor "cleaning up shared memory");
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (client_rmm) {
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_rmm_destroy(client_rmm);
1982fbecb99335961659b9961d47f2425bf142abgryzor client_rmm = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (client_shm) {
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_shm_destroy(client_shm);
1982fbecb99335961659b9961d47f2425bf142abgryzor client_shm = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (client_lock) {
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_destroy(client_lock);
1982fbecb99335961659b9961d47f2425bf142abgryzor client_lock = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (opaque_lock) {
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_destroy(opaque_lock);
1982fbecb99335961659b9961d47f2425bf142abgryzor opaque_lock = NULL;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis client_list = NULL;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
1982fbecb99335961659b9961d47f2425bf142abgryzor return APR_SUCCESS;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic void log_error_and_cleanup(char *msg, apr_status_t sts, server_rec *s)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_error(APLOG_MARK, APLOG_ERR, sts, s, APLOGNO(01760)
1982fbecb99335961659b9961d47f2425bf142abgryzor "%s - all nonce-count checking and one-time nonces "
1982fbecb99335961659b9961d47f2425bf142abgryzor "disabled", msg);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor cleanup_tables(NULL);
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor#if APR_HAS_SHARED_MEMORY
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic int initialize_tables(server_rec *s, apr_pool_t *ctx)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long idx;
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_status_t sts;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* set up client list */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* Create the shared memory segment */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /*
1982fbecb99335961659b9961d47f2425bf142abgryzor * Create a unique filename using our pid. This information is
1982fbecb99335961659b9961d47f2425bf142abgryzor * stashed in the global variable so the children inherit it.
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis */
1982fbecb99335961659b9961d47f2425bf142abgryzor client_shm_filename = ap_runtime_dir_relative(ctx, "authdigest_shm");
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis client_shm_filename = ap_append_pid(ctx, client_shm_filename, ".");
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* Now create that segment */
1982fbecb99335961659b9961d47f2425bf142abgryzor sts = apr_shm_create(&client_shm, shmem_size,
1982fbecb99335961659b9961d47f2425bf142abgryzor client_shm_filename, ctx);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (APR_SUCCESS != sts) {
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_error(APLOG_MARK, APLOG_ERR, sts, s, APLOGNO(01762)
1982fbecb99335961659b9961d47f2425bf142abgryzor "Failed to create shared memory segment on file %s",
1982fbecb99335961659b9961d47f2425bf142abgryzor client_shm_filename);
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to initialize shm", sts, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return HTTP_INTERNAL_SERVER_ERROR;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor sts = apr_rmm_init(&client_rmm,
1982fbecb99335961659b9961d47f2425bf142abgryzor NULL, /* no lock, we'll do the locking ourselves */
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_shm_baseaddr_get(client_shm),
1982fbecb99335961659b9961d47f2425bf142abgryzor shmem_size, ctx);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (sts != APR_SUCCESS) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to initialize rmm", sts, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(*client_list) +
1982fbecb99335961659b9961d47f2425bf142abgryzor sizeof(client_entry*)*num_buckets));
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!client_list) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to allocate shared memory", -1, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->table = (client_entry**) (client_list + 1);
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis for (idx = 0; idx < num_buckets; idx++) {
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->table[idx] = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->tbl_len = num_buckets;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_entries = 0;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor sts = ap_global_mutex_create(&client_lock, NULL, client_mutex_type, NULL,
1982fbecb99335961659b9961d47f2425bf142abgryzor s, ctx, 0);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (sts != APR_SUCCESS) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to create lock (client_lock)", sts, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* setup opaque */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor opaque_cntr = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(*opaque_cntr)));
1982fbecb99335961659b9961d47f2425bf142abgryzor if (opaque_cntr == NULL) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to allocate shared memory", -1, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor *opaque_cntr = 1UL;
1982fbecb99335961659b9961d47f2425bf142abgryzor
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis sts = ap_global_mutex_create(&opaque_lock, NULL, opaque_mutex_type, NULL,
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis s, ctx, 0);
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis if (sts != APR_SUCCESS) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis log_error_and_cleanup("failed to create lock (opaque_lock)", sts, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* setup one-time-nonce counter */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor otn_counter = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(*otn_counter)));
1982fbecb99335961659b9961d47f2425bf142abgryzor if (otn_counter == NULL) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to allocate shared memory", -1, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
2f44538c88c5310bebb17a39b171487315a45624lgentis *otn_counter = 0;
2f44538c88c5310bebb17a39b171487315a45624lgentis /* no lock here */
2f44538c88c5310bebb17a39b171487315a45624lgentis
2f44538c88c5310bebb17a39b171487315a45624lgentis
2f44538c88c5310bebb17a39b171487315a45624lgentis /* success */
2f44538c88c5310bebb17a39b171487315a45624lgentis return OK;
2f44538c88c5310bebb17a39b171487315a45624lgentis}
2f44538c88c5310bebb17a39b171487315a45624lgentis
2f44538c88c5310bebb17a39b171487315a45624lgentis#endif /* APR_HAS_SHARED_MEMORY */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic int pre_init(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_status_t rv;
1982fbecb99335961659b9961d47f2425bf142abgryzor void *retained;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor rv = ap_mutex_register(pconf, client_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (rv != APR_SUCCESS)
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor rv = ap_mutex_register(pconf, opaque_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (rv != APR_SUCCESS)
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor retained = ap_retained_data_get(RETAINED_DATA_ID);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (retained == NULL) {
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis retained = ap_retained_data_create(RETAINED_DATA_ID, SECRET_LEN);
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, APLOGNO(01757)
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis "generating secret for digest authentication");
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis#if APR_HAS_RANDOM
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis rv = apr_generate_random_bytes(retained, SECRET_LEN);
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis#else
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis#error APR random number support is missing
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis#endif
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis if (rv != APR_SUCCESS) {
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL, APLOGNO(01758)
1982fbecb99335961659b9961d47f2425bf142abgryzor "error generating secret");
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor secret = retained;
1982fbecb99335961659b9961d47f2425bf142abgryzor return OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic int initialize_module(apr_pool_t *p, apr_pool_t *plog,
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_pool_t *ptemp, server_rec *s)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor /* initialize_module() will be called twice, and if it's a DSO
1982fbecb99335961659b9961d47f2425bf142abgryzor * then all static data from the first call will be lost. Only
1982fbecb99335961659b9961d47f2425bf142abgryzor * set up our static data on the second call. */
1982fbecb99335961659b9961d47f2425bf142abgryzor if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG)
1982fbecb99335961659b9961d47f2425bf142abgryzor return OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis#if APR_HAS_SHARED_MEMORY
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis /* Note: this stuff is currently fixed for the lifetime of the server,
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis * i.e. even across restarts. This means that A) any shmem-size
1982fbecb99335961659b9961d47f2425bf142abgryzor * configuration changes are ignored, and B) certain optimizations,
1982fbecb99335961659b9961d47f2425bf142abgryzor * such as only allocating the smallest necessary entry for each
1982fbecb99335961659b9961d47f2425bf142abgryzor * client, can't be done. However, the alternative is a nightmare:
1982fbecb99335961659b9961d47f2425bf142abgryzor * we can't call apr_shm_destroy on a graceful restart because there
1982fbecb99335961659b9961d47f2425bf142abgryzor * will be children using the tables, and we also don't know when the
1982fbecb99335961659b9961d47f2425bf142abgryzor * last child dies. Therefore we can never clean up the old stuff,
1982fbecb99335961659b9961d47f2425bf142abgryzor * creating a creeping memory leak.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor if (initialize_tables(s, p) != OK) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis }
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis /* Call cleanup_tables on exit or restart */
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis apr_pool_cleanup_register(p, NULL, cleanup_tables, apr_pool_cleanup_null);
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis#endif /* APR_HAS_SHARED_MEMORY */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis return OK;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis}
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentisstatic void initialize_child(apr_pool_t *p, server_rec *s)
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis{
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis apr_status_t sts;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!client_shm) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* Get access to rmm in child */
1982fbecb99335961659b9961d47f2425bf142abgryzor sts = apr_rmm_attach(&client_rmm,
1982fbecb99335961659b9961d47f2425bf142abgryzor NULL,
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_shm_baseaddr_get(client_shm),
1982fbecb99335961659b9961d47f2425bf142abgryzor p);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (sts != APR_SUCCESS) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to attach to rmm", sts, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor sts = apr_global_mutex_child_init(&client_lock,
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_lockfile(client_lock),
1982fbecb99335961659b9961d47f2425bf142abgryzor p);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (sts != APR_SUCCESS) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to create lock (client_lock)", sts, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis sts = apr_global_mutex_child_init(&opaque_lock,
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis apr_global_mutex_lockfile(opaque_lock),
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis p);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (sts != APR_SUCCESS) {
1982fbecb99335961659b9961d47f2425bf142abgryzor log_error_and_cleanup("failed to create lock (opaque_lock)", sts, s);
1982fbecb99335961659b9961d47f2425bf142abgryzor return;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * configuration code
4721c0c3564368123b87ba96db0bde5e3dd44357lgentis */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic void *create_digest_dir_config(apr_pool_t *p, char *dir)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor digest_config_rec *conf;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (dir == NULL) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor conf = (digest_config_rec *) apr_pcalloc(p, sizeof(digest_config_rec));
1982fbecb99335961659b9961d47f2425bf142abgryzor if (conf) {
1982fbecb99335961659b9961d47f2425bf142abgryzor conf->qop_list = apr_array_make(p, 2, sizeof(char *));
1982fbecb99335961659b9961d47f2425bf142abgryzor conf->nonce_lifetime = DFLT_NONCE_LIFE;
1982fbecb99335961659b9961d47f2425bf142abgryzor conf->dir_name = apr_pstrdup(p, dir);
1982fbecb99335961659b9961d47f2425bf142abgryzor conf->algorithm = DFLT_ALGORITHM;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return conf;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentisstatic const char *set_realm(cmd_parms *cmd, void *config, const char *realm)
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis{
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis digest_config_rec *conf = (digest_config_rec *) config;
1982fbecb99335961659b9961d47f2425bf142abgryzor#ifdef AP_DEBUG
1982fbecb99335961659b9961d47f2425bf142abgryzor int i;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* check that we got random numbers */
1982fbecb99335961659b9961d47f2425bf142abgryzor for (i = 0; i < SECRET_LEN; i++) {
1982fbecb99335961659b9961d47f2425bf142abgryzor if (secret[i] != 0)
1982fbecb99335961659b9961d47f2425bf142abgryzor break;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
4721c0c3564368123b87ba96db0bde5e3dd44357lgentis ap_assert(i < SECRET_LEN);
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis#endif
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* The core already handles the realm, but it's just too convenient to
1982fbecb99335961659b9961d47f2425bf142abgryzor * grab it ourselves too and cache some setups. However, we need to
1982fbecb99335961659b9961d47f2425bf142abgryzor * let the core get at it too, which is why we decline at the end -
1982fbecb99335961659b9961d47f2425bf142abgryzor * this relies on the fact that http_core is last in the list.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor conf->realm = realm;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* we precompute the part of the nonce hash that is constant (well,
1982fbecb99335961659b9961d47f2425bf142abgryzor * the host:port would be too, but that varies for .htaccess files
1982fbecb99335961659b9961d47f2425bf142abgryzor * and directives outside a virtual host section)
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_init(&conf->nonce_ctx);
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_update_binary(&conf->nonce_ctx, secret, SECRET_LEN);
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_update_binary(&conf->nonce_ctx, (const unsigned char *) realm,
1982fbecb99335961659b9961d47f2425bf142abgryzor strlen(realm));
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return DECLINE_CMD;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *add_authn_provider(cmd_parms *cmd, void *config,
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis const char *arg)
4721c0c3564368123b87ba96db0bde5e3dd44357lgentis{
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis digest_config_rec *conf = (digest_config_rec*)config;
1982fbecb99335961659b9961d47f2425bf142abgryzor authn_provider_list *newp;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor newp = apr_pcalloc(cmd->pool, sizeof(authn_provider_list));
1982fbecb99335961659b9961d47f2425bf142abgryzor newp->provider_name = arg;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* lookup and cache the actual provider now */
1982fbecb99335961659b9961d47f2425bf142abgryzor newp->provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
1982fbecb99335961659b9961d47f2425bf142abgryzor newp->provider_name,
1982fbecb99335961659b9961d47f2425bf142abgryzor AUTHN_PROVIDER_VERSION);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (newp->provider == NULL) {
1982fbecb99335961659b9961d47f2425bf142abgryzor /* by the time they use it, the provider should be loaded and
1982fbecb99335961659b9961d47f2425bf142abgryzor registered with us. */
1982fbecb99335961659b9961d47f2425bf142abgryzor return apr_psprintf(cmd->pool,
1982fbecb99335961659b9961d47f2425bf142abgryzor "Unknown Authn provider: %s",
1982fbecb99335961659b9961d47f2425bf142abgryzor newp->provider_name);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis if (!newp->provider->get_realm_hash) {
1982fbecb99335961659b9961d47f2425bf142abgryzor /* if it doesn't provide the appropriate function, reject it */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis return apr_psprintf(cmd->pool,
1982fbecb99335961659b9961d47f2425bf142abgryzor "The '%s' Authn provider doesn't support "
1982fbecb99335961659b9961d47f2425bf142abgryzor "Digest Authentication", newp->provider_name);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* Add it to the list now. */
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!conf->providers) {
1982fbecb99335961659b9961d47f2425bf142abgryzor conf->providers = newp;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
1982fbecb99335961659b9961d47f2425bf142abgryzor authn_provider_list *last = conf->providers;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
1982fbecb99335961659b9961d47f2425bf142abgryzor while (last->next) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis last = last->next;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor last->next = newp;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *set_qop(cmd_parms *cmd, void *config, const char *op)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis digest_config_rec *conf = (digest_config_rec *) config;
1982fbecb99335961659b9961d47f2425bf142abgryzor
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis if (!strcasecmp(op, "none")) {
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_array_clear(conf->qop_list);
1982fbecb99335961659b9961d47f2425bf142abgryzor *(const char **)apr_array_push(conf->qop_list) = "none";
1982fbecb99335961659b9961d47f2425bf142abgryzor return NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!strcasecmp(op, "auth-int")) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return "AuthDigestQop auth-int is not implemented";
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (strcasecmp(op, "auth")) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return apr_pstrcat(cmd->pool, "Unrecognized qop: ", op, NULL);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor *(const char **)apr_array_push(conf->qop_list) = op;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *set_nonce_lifetime(cmd_parms *cmd, void *config,
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *t)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor char *endptr;
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis long lifetime;
9712e2af82f08159755667b0e00e4e48caf97d7ahumbedooh
1982fbecb99335961659b9961d47f2425bf142abgryzor lifetime = strtol(t, &endptr, 10);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (endptr < (t+strlen(t)) && !apr_isspace(*endptr)) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return apr_pstrcat(cmd->pool,
1982fbecb99335961659b9961d47f2425bf142abgryzor "Invalid time in AuthDigestNonceLifetime: ",
1982fbecb99335961659b9961d47f2425bf142abgryzor t, NULL);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis ((digest_config_rec *) config)->nonce_lifetime = apr_time_from_sec(lifetime);
1982fbecb99335961659b9961d47f2425bf142abgryzor return NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *set_nonce_format(cmd_parms *cmd, void *config,
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *fmt)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor return "AuthDigestNonceFormat is not implemented";
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *set_nc_check(cmd_parms *cmd, void *config, int flag)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor#if !APR_HAS_SHARED_MEMORY
027a412efb8b93639a95a64712f9a9c9958fe066lgentis if (flag) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return "AuthDigestNcCheck: ERROR: nonce-count checking "
1982fbecb99335961659b9961d47f2425bf142abgryzor "is not supported on platforms without shared-memory "
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis "support";
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis#endif
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis ((digest_config_rec *) config)->check_nc = flag;
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis return NULL;
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis}
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentisstatic const char *set_algorithm(cmd_parms *cmd, void *config, const char *alg)
00ce3c4e13e755c33b63f45c5d3ae69eccd977b1lgentis{
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!strcasecmp(alg, "MD5-sess")) {
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis return "AuthDigestAlgorithm: ERROR: algorithm `MD5-sess' "
1982fbecb99335961659b9961d47f2425bf142abgryzor "is not implemented";
1982fbecb99335961659b9961d47f2425bf142abgryzor }
db158bb207b0f01432bfc00b3df488226b87532dlgentis else if (strcasecmp(alg, "MD5")) {
db158bb207b0f01432bfc00b3df488226b87532dlgentis return apr_pstrcat(cmd->pool, "Invalid algorithm in AuthDigestAlgorithm: ", alg, NULL);
db158bb207b0f01432bfc00b3df488226b87532dlgentis }
db158bb207b0f01432bfc00b3df488226b87532dlgentis
db158bb207b0f01432bfc00b3df488226b87532dlgentis ((digest_config_rec *) config)->algorithm = alg;
db158bb207b0f01432bfc00b3df488226b87532dlgentis return NULL;
db158bb207b0f01432bfc00b3df488226b87532dlgentis}
db158bb207b0f01432bfc00b3df488226b87532dlgentis
db158bb207b0f01432bfc00b3df488226b87532dlgentisstatic const char *set_uri_list(cmd_parms *cmd, void *config, const char *uri)
db158bb207b0f01432bfc00b3df488226b87532dlgentis{
db158bb207b0f01432bfc00b3df488226b87532dlgentis digest_config_rec *c = (digest_config_rec *) config;
db158bb207b0f01432bfc00b3df488226b87532dlgentis if (c->uri_list) {
db158bb207b0f01432bfc00b3df488226b87532dlgentis c->uri_list[strlen(c->uri_list)-1] = '\0';
db158bb207b0f01432bfc00b3df488226b87532dlgentis c->uri_list = apr_pstrcat(cmd->pool, c->uri_list, " ", uri, "\"", NULL);
db158bb207b0f01432bfc00b3df488226b87532dlgentis }
db158bb207b0f01432bfc00b3df488226b87532dlgentis else {
db158bb207b0f01432bfc00b3df488226b87532dlgentis c->uri_list = apr_pstrcat(cmd->pool, ", domain=\"", uri, "\"", NULL);
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis return NULL;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis}
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentisstatic const char *set_shmem_size(cmd_parms *cmd, void *config,
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis const char *size_str)
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis{
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis char *endptr;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis long size, min;
db158bb207b0f01432bfc00b3df488226b87532dlgentis
db158bb207b0f01432bfc00b3df488226b87532dlgentis size = strtol(size_str, &endptr, 10);
db158bb207b0f01432bfc00b3df488226b87532dlgentis while (apr_isspace(*endptr)) endptr++;
db158bb207b0f01432bfc00b3df488226b87532dlgentis if (*endptr == '\0' || *endptr == 'b' || *endptr == 'B') {
db158bb207b0f01432bfc00b3df488226b87532dlgentis ;
db158bb207b0f01432bfc00b3df488226b87532dlgentis }
db158bb207b0f01432bfc00b3df488226b87532dlgentis else if (*endptr == 'k' || *endptr == 'K') {
1982fbecb99335961659b9961d47f2425bf142abgryzor size *= 1024;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (*endptr == 'm' || *endptr == 'M') {
1982fbecb99335961659b9961d47f2425bf142abgryzor size *= 1048576;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis return apr_pstrcat(cmd->pool, "Invalid size in AuthDigestShmemSize: ",
1982fbecb99335961659b9961d47f2425bf142abgryzor size_str, NULL);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor min = sizeof(*client_list) + sizeof(client_entry*) + sizeof(client_entry);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (size < min) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return apr_psprintf(cmd->pool, "size in AuthDigestShmemSize too small: "
1982fbecb99335961659b9961d47f2425bf142abgryzor "%ld < %ld", size, min);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor shmem_size = size;
1982fbecb99335961659b9961d47f2425bf142abgryzor num_buckets = (size - sizeof(*client_list)) /
1982fbecb99335961659b9961d47f2425bf142abgryzor (sizeof(client_entry*) + HASH_DEPTH * sizeof(client_entry));
020366f830905b6b5dfccfa03373379ae6a13e7blgentis if (num_buckets == 0) {
020366f830905b6b5dfccfa03373379ae6a13e7blgentis num_buckets = 1;
020366f830905b6b5dfccfa03373379ae6a13e7blgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, APLOGNO(01763)
020366f830905b6b5dfccfa03373379ae6a13e7blgentis "Set shmem-size: %" APR_SIZE_T_FMT ", num-buckets: %ld",
020366f830905b6b5dfccfa03373379ae6a13e7blgentis shmem_size, num_buckets);
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentis return NULL;
020366f830905b6b5dfccfa03373379ae6a13e7blgentis}
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentisstatic const command_rec digest_cmds[] =
020366f830905b6b5dfccfa03373379ae6a13e7blgentis{
020366f830905b6b5dfccfa03373379ae6a13e7blgentis AP_INIT_TAKE1("AuthName", set_realm, NULL, OR_AUTHCFG,
020366f830905b6b5dfccfa03373379ae6a13e7blgentis "The authentication realm (e.g. \"Members Only\")"),
020366f830905b6b5dfccfa03373379ae6a13e7blgentis AP_INIT_ITERATE("AuthDigestProvider", add_authn_provider, NULL, OR_AUTHCFG,
09796a508c72a6aba33aa486753bb8cdea806d43lgentis "specify the auth providers for a directory or location"),
1982fbecb99335961659b9961d47f2425bf142abgryzor AP_INIT_ITERATE("AuthDigestQop", set_qop, NULL, OR_AUTHCFG,
1982fbecb99335961659b9961d47f2425bf142abgryzor "A list of quality-of-protection options"),
1982fbecb99335961659b9961d47f2425bf142abgryzor AP_INIT_TAKE1("AuthDigestNonceLifetime", set_nonce_lifetime, NULL, OR_AUTHCFG,
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentis "Maximum lifetime of the server nonce (seconds)"),
1982fbecb99335961659b9961d47f2425bf142abgryzor AP_INIT_TAKE1("AuthDigestNonceFormat", set_nonce_format, NULL, OR_AUTHCFG,
1982fbecb99335961659b9961d47f2425bf142abgryzor "The format to use when generating the server nonce"),
1982fbecb99335961659b9961d47f2425bf142abgryzor AP_INIT_FLAG("AuthDigestNcCheck", set_nc_check, NULL, OR_AUTHCFG,
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis "Whether or not to check the nonce-count sent by the client"),
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis AP_INIT_TAKE1("AuthDigestAlgorithm", set_algorithm, NULL, OR_AUTHCFG,
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis "The algorithm used for the hash calculation"),
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis AP_INIT_ITERATE("AuthDigestDomain", set_uri_list, NULL, OR_AUTHCFG,
1982fbecb99335961659b9961d47f2425bf142abgryzor "A list of URI's which belong to the same protection space as the current URI"),
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis AP_INIT_TAKE1("AuthDigestShmemSize", set_shmem_size, NULL, RSRC_CONF,
1982fbecb99335961659b9961d47f2425bf142abgryzor "The amount of shared memory to allocate for keeping track of clients"),
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis {NULL}
1982fbecb99335961659b9961d47f2425bf142abgryzor};
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis * client list code
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * Each client is assigned a number, which is transferred in the opaque
1982fbecb99335961659b9961d47f2425bf142abgryzor * field of the WWW-Authenticate and Authorization headers. The number
1982fbecb99335961659b9961d47f2425bf142abgryzor * is just a simple counter which is incremented for each new client.
1982fbecb99335961659b9961d47f2425bf142abgryzor * Clients can't forge this number because it is hashed up into the
1982fbecb99335961659b9961d47f2425bf142abgryzor * server nonce, and that is checked.
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * The clients are kept in a simple hash table, which consists of an
1982fbecb99335961659b9961d47f2425bf142abgryzor * array of client_entry's, each with a linked list of entries hanging
1982fbecb99335961659b9961d47f2425bf142abgryzor * off it. The client's number modulo the size of the array gives the
1982fbecb99335961659b9961d47f2425bf142abgryzor * bucket number.
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * The clients are garbage collected whenever a new client is allocated
1982fbecb99335961659b9961d47f2425bf142abgryzor * but there is not enough space left in the shared memory segment. A
1982fbecb99335961659b9961d47f2425bf142abgryzor * simple semi-LRU is used for this: whenever a client entry is accessed
1982fbecb99335961659b9961d47f2425bf142abgryzor * it is moved to the beginning of the linked list in its bucket (this
1982fbecb99335961659b9961d47f2425bf142abgryzor * also makes for faster lookups for current clients). The garbage
1982fbecb99335961659b9961d47f2425bf142abgryzor * collecter then just removes the oldest entry (i.e. the one at the
1982fbecb99335961659b9961d47f2425bf142abgryzor * end of the list) in each bucket.
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * The main advantages of the above scheme are that it's easy to implement
1982fbecb99335961659b9961d47f2425bf142abgryzor * and it keeps the hash table evenly balanced (i.e. same number of entries
1982fbecb99335961659b9961d47f2425bf142abgryzor * in each bucket). The major disadvantage is that you may be throwing
1982fbecb99335961659b9961d47f2425bf142abgryzor * entries out which are in active use. This is not tragic, as these
1982fbecb99335961659b9961d47f2425bf142abgryzor * clients will just be sent a new client id (opaque field) and nonce
1982fbecb99335961659b9961d47f2425bf142abgryzor * with a stale=true (i.e. it will just look like the nonce expired,
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis * thereby forcing an extra round trip). If the shared memory segment
1982fbecb99335961659b9961d47f2425bf142abgryzor * has enough headroom over the current client set size then this should
1982fbecb99335961659b9961d47f2425bf142abgryzor * not occur too often.
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * To help tune the size of the shared memory segment (and see if the
1982fbecb99335961659b9961d47f2425bf142abgryzor * above algorithm is really sufficient) a set of counters is kept
1982fbecb99335961659b9961d47f2425bf142abgryzor * indicating the number of clients held, the number of garbage collected
1982fbecb99335961659b9961d47f2425bf142abgryzor * clients, and the number of erroneously purged clients. These are printed
1982fbecb99335961659b9961d47f2425bf142abgryzor * out at each garbage collection run. Note that access to the counters is
1982fbecb99335961659b9961d47f2425bf142abgryzor * not synchronized because they are just indicaters, and whether they are
1982fbecb99335961659b9961d47f2425bf142abgryzor * off by a few doesn't matter; and for the same reason no attempt is made
1982fbecb99335961659b9961d47f2425bf142abgryzor * to guarantee the num_renewed is correct in the face of clients spoofing
1982fbecb99335961659b9961d47f2425bf142abgryzor * the opaque field.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
1982fbecb99335961659b9961d47f2425bf142abgryzor * Get the client given its client number (the key). Returns the entry,
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis * or NULL if it's not found.
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1bd10e3ff5b2478df23af4caa517b489df38a82clgentis * Access to the list itself is synchronized via locks. However, access
1982fbecb99335961659b9961d47f2425bf142abgryzor * to the entry returned by get_client() is NOT synchronized. This means
1bd10e3ff5b2478df23af4caa517b489df38a82clgentis * that there are potentially problems if a client uses multiple,
1bd10e3ff5b2478df23af4caa517b489df38a82clgentis * simultaneous connections to access url's within the same protection
1bd10e3ff5b2478df23af4caa517b489df38a82clgentis * space. However, these problems are not new: when using multiple
1982fbecb99335961659b9961d47f2425bf142abgryzor * connections you have no guarantee of the order the requests are
1bd10e3ff5b2478df23af4caa517b489df38a82clgentis * processed anyway, so you have problems with the nonce-count and
1982fbecb99335961659b9961d47f2425bf142abgryzor * one-time nonces anyway.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic client_entry *get_client(unsigned long key, const request_rec *r)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor int bucket;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_entry *entry, *prev = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!key || !client_shm) return NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor bucket = key % client_list->tbl_len;
1982fbecb99335961659b9961d47f2425bf142abgryzor entry = client_list->table[bucket];
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_lock(client_lock);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor while (entry && key != entry->key) {
1982fbecb99335961659b9961d47f2425bf142abgryzor prev = entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor entry = entry->next;
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentis }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (entry && prev) { /* move entry to front of list */
1982fbecb99335961659b9961d47f2425bf142abgryzor prev->next = entry->next;
1982fbecb99335961659b9961d47f2425bf142abgryzor entry->next = client_list->table[bucket];
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->table[bucket] = entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_unlock(client_lock);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (entry) {
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01764)
1982fbecb99335961659b9961d47f2425bf142abgryzor "get_client(): client %lu found", key);
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentis }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01765)
1982fbecb99335961659b9961d47f2425bf142abgryzor "get_client(): client %lu not found", key);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* A simple garbage-collecter to remove unused clients. It removes the
1982fbecb99335961659b9961d47f2425bf142abgryzor * last entry in each bucket and updates the counters. Returns the
1982fbecb99335961659b9961d47f2425bf142abgryzor * number of removed entries.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic long gc(void)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor client_entry *entry, *prev;
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long num_removed = 0, idx;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* garbage collect all last entries */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor for (idx = 0; idx < client_list->tbl_len; idx++) {
1982fbecb99335961659b9961d47f2425bf142abgryzor entry = client_list->table[idx];
1982fbecb99335961659b9961d47f2425bf142abgryzor prev = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor while (entry->next) { /* find last entry */
1982fbecb99335961659b9961d47f2425bf142abgryzor prev = entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor entry = entry->next;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor if (prev) {
1982fbecb99335961659b9961d47f2425bf142abgryzor prev->next = NULL; /* cut list */
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
2f44538c88c5310bebb17a39b171487315a45624lgentis client_list->table[idx] = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor if (entry) { /* remove entry */
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_rmm_free(client_rmm, apr_rmm_offset_get(client_rmm, entry));
1982fbecb99335961659b9961d47f2425bf142abgryzor num_removed++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* update counters and log */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_entries -= num_removed;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_removed += num_removed;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return num_removed;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
1982fbecb99335961659b9961d47f2425bf142abgryzor * Add a new client to the list. Returns the entry if successful, NULL
1982fbecb99335961659b9961d47f2425bf142abgryzor * otherwise. This triggers the garbage collection if memory is low.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentisstatic client_entry *add_client(unsigned long key, client_entry *info,
1982fbecb99335961659b9961d47f2425bf142abgryzor server_rec *s)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor int bucket;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_entry *entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!key || !client_shm) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return NULL;
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentis }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor bucket = key % client_list->tbl_len;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_lock(client_lock);
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis /* try to allocate a new entry */
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis
1982fbecb99335961659b9961d47f2425bf142abgryzor entry = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(client_entry)));
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!entry) {
1982fbecb99335961659b9961d47f2425bf142abgryzor long num_removed = gc();
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(01766)
1982fbecb99335961659b9961d47f2425bf142abgryzor "gc'd %ld client entries. Total new clients: "
1982fbecb99335961659b9961d47f2425bf142abgryzor "%ld; Total removed clients: %ld; Total renewed clients: "
1982fbecb99335961659b9961d47f2425bf142abgryzor "%ld", num_removed,
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_created - client_list->num_renewed,
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_removed, client_list->num_renewed);
1982fbecb99335961659b9961d47f2425bf142abgryzor entry = apr_rmm_addr_get(client_rmm, apr_rmm_malloc(client_rmm, sizeof(client_entry)));
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!entry) {
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(01767)
1982fbecb99335961659b9961d47f2425bf142abgryzor "unable to allocate new auth_digest client");
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_unlock(client_lock);
1982fbecb99335961659b9961d47f2425bf142abgryzor return NULL; /* give up */
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* now add the entry */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor memcpy(entry, info, sizeof(client_entry));
1982fbecb99335961659b9961d47f2425bf142abgryzor entry->key = key;
1982fbecb99335961659b9961d47f2425bf142abgryzor entry->next = client_list->table[bucket];
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->table[bucket] = entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_created++;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_entries++;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_global_mutex_unlock(client_lock);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01768)
1982fbecb99335961659b9961d47f2425bf142abgryzor "allocated new client %lu", key);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis/*
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis * Authorization header parser code
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis */
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis
b228078ccfac4cc568b6723f4d1b9a2317d67d5elgentis/* Parse the Authorization header, if it exists */
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic int get_digest_rec(request_rec *r, digest_header_rec *resp)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis const char *auth_line;
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_size_t l;
1982fbecb99335961659b9961d47f2425bf142abgryzor int vk = 0, vv = 0;
1982fbecb99335961659b9961d47f2425bf142abgryzor char *key, *value;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line = apr_table_get(r->headers_in,
1982fbecb99335961659b9961d47f2425bf142abgryzor (PROXYREQ_PROXY == r->proxyreq)
1982fbecb99335961659b9961d47f2425bf142abgryzor ? "Proxy-Authorization"
1982fbecb99335961659b9961d47f2425bf142abgryzor : "Authorization");
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!auth_line) {
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->auth_hdr_sts = NO_HEADER;
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->scheme = ap_getword_white(r->pool, &auth_line);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (strcasecmp(resp->scheme, "Digest")) {
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->auth_hdr_sts = NOT_DIGEST;
1982fbecb99335961659b9961d47f2425bf142abgryzor return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis
1982fbecb99335961659b9961d47f2425bf142abgryzor l = strlen(auth_line);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor key = apr_palloc(r->pool, l+1);
1982fbecb99335961659b9961d47f2425bf142abgryzor value = apr_palloc(r->pool, l+1);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor while (auth_line[0] != '\0') {
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* find key */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor while (apr_isspace(auth_line[0])) {
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor vk = 0;
1982fbecb99335961659b9961d47f2425bf142abgryzor while (auth_line[0] != '=' && auth_line[0] != ','
1982fbecb99335961659b9961d47f2425bf142abgryzor && auth_line[0] != '\0' && !apr_isspace(auth_line[0])) {
1982fbecb99335961659b9961d47f2425bf142abgryzor key[vk++] = *auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis key[vk] = '\0';
1982fbecb99335961659b9961d47f2425bf142abgryzor while (apr_isspace(auth_line[0])) {
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* find value */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (auth_line[0] == '=') {
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor while (apr_isspace(auth_line[0])) {
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor vv = 0;
1982fbecb99335961659b9961d47f2425bf142abgryzor if (auth_line[0] == '\"') { /* quoted string */
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor while (auth_line[0] != '\"' && auth_line[0] != '\0') {
1982fbecb99335961659b9961d47f2425bf142abgryzor if (auth_line[0] == '\\' && auth_line[1] != '\0') {
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++; /* escaped char */
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor value[vv++] = *auth_line++;
020366f830905b6b5dfccfa03373379ae6a13e7blgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis if (auth_line[0] != '\0') {
020366f830905b6b5dfccfa03373379ae6a13e7blgentis auth_line++;
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis else { /* token */
020366f830905b6b5dfccfa03373379ae6a13e7blgentis while (auth_line[0] != ',' && auth_line[0] != '\0'
020366f830905b6b5dfccfa03373379ae6a13e7blgentis && !apr_isspace(auth_line[0])) {
020366f830905b6b5dfccfa03373379ae6a13e7blgentis value[vv++] = *auth_line++;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis value[vv] = '\0';
020366f830905b6b5dfccfa03373379ae6a13e7blgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentis while (auth_line[0] != ',' && auth_line[0] != '\0') {
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentis if (auth_line[0] != '\0') {
1982fbecb99335961659b9961d47f2425bf142abgryzor auth_line++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!strcasecmp(key, "username"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->username = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "realm"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->realm = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "nonce"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->nonce = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "uri"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->uri = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "response"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->digest = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "algorithm"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->algorithm = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "cnonce"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->cnonce = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "opaque"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->opaque = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "qop"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->message_qop = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(key, "nc"))
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->nonce_count = apr_pstrdup(r->pool, value);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!resp->username || !resp->realm || !resp->nonce || !resp->uri
1982fbecb99335961659b9961d47f2425bf142abgryzor || !resp->digest
1982fbecb99335961659b9961d47f2425bf142abgryzor || (resp->message_qop && (!resp->cnonce || !resp->nonce_count))) {
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->auth_hdr_sts = INVALID;
576c49cd335618ad4b5351bd1c5f2cfd7584dba4lgentis return !OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (resp->opaque) {
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->opaque_num = (unsigned long) strtol(resp->opaque, NULL, 16);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->auth_hdr_sts = VALID;
1982fbecb99335961659b9961d47f2425bf142abgryzor return OK;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* Because the browser may preemptively send auth info, incrementing the
4721c0c3564368123b87ba96db0bde5e3dd44357lgentis * nonce-count when it does, and because the client does not get notified
4721c0c3564368123b87ba96db0bde5e3dd44357lgentis * if the URI didn't need authentication after all, we need to be sure to
1982fbecb99335961659b9961d47f2425bf142abgryzor * update the nonce-count each time we receive an Authorization header no
1982fbecb99335961659b9961d47f2425bf142abgryzor * matter what the final outcome of the request. Furthermore this is a
1982fbecb99335961659b9961d47f2425bf142abgryzor * convenient place to get the request-uri (before any subrequests etc
1982fbecb99335961659b9961d47f2425bf142abgryzor * are initiated) and to initialize the request_config.
1982fbecb99335961659b9961d47f2425bf142abgryzor *
1982fbecb99335961659b9961d47f2425bf142abgryzor * Note that this must be called after mod_proxy had its go so that
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * r->proxyreq is set correctly.
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentisstatic int parse_hdr_and_update_nc(request_rec *r)
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis{
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis digest_header_rec *resp;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis int res;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!ap_is_initial_req(r)) {
1982fbecb99335961659b9961d47f2425bf142abgryzor return DECLINED;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
1982fbecb99335961659b9961d47f2425bf142abgryzor
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis resp = apr_pcalloc(r->pool, sizeof(digest_header_rec));
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->raw_request_uri = r->unparsed_uri;
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->psd_request_uri = &r->parsed_uri;
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->needed_auth = 0;
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->method = r->method;
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_set_module_config(r->request_config, &auth_digest_module, resp);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor res = get_digest_rec(r, resp);
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->client = get_client(resp->opaque_num, r);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (res == OK && resp->client) {
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->client->nonce_count++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor return DECLINED;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis/*
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis * Nonce generation code
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* The hash part of the nonce is a SHA-1 hash of the time, realm, server host
1982fbecb99335961659b9961d47f2425bf142abgryzor * and port, opaque, and our secret.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic void gen_nonce_hash(char *hash, const char *timestr, const char *opaque,
1982fbecb99335961659b9961d47f2425bf142abgryzor const server_rec *server,
1982fbecb99335961659b9961d47f2425bf142abgryzor const digest_config_rec *conf)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned char sha1[APR_SHA1_DIGESTSIZE];
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_ctx_t ctx;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor memcpy(&ctx, &conf->nonce_ctx, sizeof(ctx));
1982fbecb99335961659b9961d47f2425bf142abgryzor /*
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_update_binary(&ctx, (const unsigned char *) server->server_hostname,
1982fbecb99335961659b9961d47f2425bf142abgryzor strlen(server->server_hostname));
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_update_binary(&ctx, (const unsigned char *) &server->port,
1982fbecb99335961659b9961d47f2425bf142abgryzor sizeof(server->port));
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_update_binary(&ctx, (const unsigned char *) timestr, strlen(timestr));
1982fbecb99335961659b9961d47f2425bf142abgryzor if (opaque) {
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_update_binary(&ctx, (const unsigned char *) opaque,
1982fbecb99335961659b9961d47f2425bf142abgryzor strlen(opaque));
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor apr_sha1_final(sha1, &ctx);
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor ap_bin2hex(sha1, APR_SHA1_DIGESTSIZE, hash);
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/* The nonce has the format b64(time)+hash .
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic const char *gen_nonce(apr_pool_t *p, apr_time_t now, const char *opaque,
1982fbecb99335961659b9961d47f2425bf142abgryzor const server_rec *server,
1982fbecb99335961659b9961d47f2425bf142abgryzor const digest_config_rec *conf)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor char *nonce = apr_palloc(p, NONCE_LEN+1);
1982fbecb99335961659b9961d47f2425bf142abgryzor time_rec t;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (conf->nonce_lifetime != 0) {
1982fbecb99335961659b9961d47f2425bf142abgryzor t.time = now;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (otn_counter) {
1982fbecb99335961659b9961d47f2425bf142abgryzor /* this counter is not synch'd, because it doesn't really matter
1982fbecb99335961659b9961d47f2425bf142abgryzor * if it counts exactly.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis t.time = (*otn_counter)++;
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis }
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis else {
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis /* XXX: WHAT IS THIS CONSTANT? */
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis t.time = 42;
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis }
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis apr_base64_encode_binary(nonce, t.arr, sizeof(t.arr));
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis gen_nonce_hash(nonce+NONCE_TIME_LEN, nonce, opaque, server, conf);
fb25b82560b7fcaffa006cb4738d86acc561b6f4lgentis
1982fbecb99335961659b9961d47f2425bf142abgryzor return nonce;
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
1982fbecb99335961659b9961d47f2425bf142abgryzor * Opaque and hash-table management
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor/*
1982fbecb99335961659b9961d47f2425bf142abgryzor * Generate a new client entry, add it to the list, and return the
1982fbecb99335961659b9961d47f2425bf142abgryzor * entry. Returns NULL if failed.
1982fbecb99335961659b9961d47f2425bf142abgryzor */
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic client_entry *gen_client(const request_rec *r)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor unsigned long op;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_entry new_entry = { 0, NULL, 0, "" }, *entry;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (!opaque_cntr) {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis return NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
2f44538c88c5310bebb17a39b171487315a45624lgentis apr_global_mutex_lock(opaque_lock);
2f44538c88c5310bebb17a39b171487315a45624lgentis op = (*opaque_cntr)++;
020366f830905b6b5dfccfa03373379ae6a13e7blgentis apr_global_mutex_unlock(opaque_lock);
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentis if (!(entry = add_client(op, &new_entry, r->server))) {
020366f830905b6b5dfccfa03373379ae6a13e7blgentis ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01769)
020366f830905b6b5dfccfa03373379ae6a13e7blgentis "failed to allocate client entry - ignoring client");
72a5bd633c0fa12e2bf77189ef9ed443ffb0cd66lgentis return NULL;
020366f830905b6b5dfccfa03373379ae6a13e7blgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentis return entry;
020366f830905b6b5dfccfa03373379ae6a13e7blgentis}
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentis/*
020366f830905b6b5dfccfa03373379ae6a13e7blgentis * Authorization challenge generation code (for WWW-Authenticate)
020366f830905b6b5dfccfa03373379ae6a13e7blgentis */
020366f830905b6b5dfccfa03373379ae6a13e7blgentis
020366f830905b6b5dfccfa03373379ae6a13e7blgentisstatic const char *ltox(apr_pool_t *p, unsigned long num)
020366f830905b6b5dfccfa03373379ae6a13e7blgentis{
020366f830905b6b5dfccfa03373379ae6a13e7blgentis if (num != 0) {
020366f830905b6b5dfccfa03373379ae6a13e7blgentis return apr_psprintf(p, "%lx", num);
020366f830905b6b5dfccfa03373379ae6a13e7blgentis }
020366f830905b6b5dfccfa03373379ae6a13e7blgentis else {
020366f830905b6b5dfccfa03373379ae6a13e7blgentis return "";
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor}
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzorstatic void note_digest_auth_failure(request_rec *r,
1982fbecb99335961659b9961d47f2425bf142abgryzor const digest_config_rec *conf,
1982fbecb99335961659b9961d47f2425bf142abgryzor digest_header_rec *resp, int stale)
1982fbecb99335961659b9961d47f2425bf142abgryzor{
1982fbecb99335961659b9961d47f2425bf142abgryzor const char *qop, *opaque, *opaque_param, *domain, *nonce;
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* Setup qop */
1982fbecb99335961659b9961d47f2425bf142abgryzor if (apr_is_empty_array(conf->qop_list)) {
1982fbecb99335961659b9961d47f2425bf142abgryzor qop = ", qop=\"auth\"";
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (!strcasecmp(*(const char **)(conf->qop_list->elts), "none")) {
1982fbecb99335961659b9961d47f2425bf142abgryzor qop = "";
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
1982fbecb99335961659b9961d47f2425bf142abgryzor qop = apr_pstrcat(r->pool, ", qop=\"",
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis apr_array_pstrcat(r->pool, conf->qop_list, ','),
1982fbecb99335961659b9961d47f2425bf142abgryzor "\"",
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis NULL);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* Setup opaque */
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor if (resp->opaque == NULL) {
1982fbecb99335961659b9961d47f2425bf142abgryzor /* new client */
1982fbecb99335961659b9961d47f2425bf142abgryzor if ((conf->check_nc || conf->nonce_lifetime == 0)
1982fbecb99335961659b9961d47f2425bf142abgryzor && (resp->client = gen_client(r)) != NULL) {
1982fbecb99335961659b9961d47f2425bf142abgryzor opaque = ltox(r->pool, resp->client->key);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
1982fbecb99335961659b9961d47f2425bf142abgryzor opaque = ""; /* opaque not needed */
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else if (resp->client == NULL) {
1982fbecb99335961659b9961d47f2425bf142abgryzor /* client info was gc'd */
1982fbecb99335961659b9961d47f2425bf142abgryzor resp->client = gen_client(r);
1982fbecb99335961659b9961d47f2425bf142abgryzor if (resp->client != NULL) {
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis opaque = ltox(r->pool, resp->client->key);
1ef910e5e65cfbfc2455c230d8ddd6453a31a427lgentis stale = 1;
1982fbecb99335961659b9961d47f2425bf142abgryzor client_list->num_renewed++;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
1982fbecb99335961659b9961d47f2425bf142abgryzor opaque = ""; /* ??? */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis else {
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis opaque = resp->opaque;
1982fbecb99335961659b9961d47f2425bf142abgryzor /* we're generating a new nonce, so reset the nonce-count */
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis resp->client->nonce_count = 0;
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis }
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis
06e80fa0dffc17ae61bca1715c96e08ea90d53cblgentis if (opaque[0]) {
1982fbecb99335961659b9961d47f2425bf142abgryzor opaque_param = apr_pstrcat(r->pool, ", opaque=\"", opaque, "\"", NULL);
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor else {
1982fbecb99335961659b9961d47f2425bf142abgryzor opaque_param = NULL;
1982fbecb99335961659b9961d47f2425bf142abgryzor }
1982fbecb99335961659b9961d47f2425bf142abgryzor
1982fbecb99335961659b9961d47f2425bf142abgryzor /* Setup nonce */
1982fbecb99335961659b9961d47f2425bf142abgryzor
nonce = gen_nonce(r->pool, r->request_time, opaque, r->server, conf);
if (resp->client && conf->nonce_lifetime == 0) {
memcpy(resp->client->last_nonce, nonce, NONCE_LEN+1);
}
/* setup domain attribute. We want to send this attribute wherever
* possible so that the client won't send the Authorization header
* unnecessarily (it's usually > 200 bytes!).
*/
/* don't send domain
* - for proxy requests
* - if it's not specified
*/
if (r->proxyreq || !conf->uri_list) {
domain = NULL;
}
else {
domain = conf->uri_list;
}
apr_table_mergen(r->err_headers_out,
(PROXYREQ_PROXY == r->proxyreq)
? "Proxy-Authenticate" : "WWW-Authenticate",
apr_psprintf(r->pool, "Digest realm=\"%s\", "
"nonce=\"%s\", algorithm=%s%s%s%s%s",
ap_auth_name(r), nonce, conf->algorithm,
opaque_param ? opaque_param : "",
domain ? domain : "",
stale ? ", stale=true" : "", qop));
}
static int hook_note_digest_auth_failure(request_rec *r, const char *auth_type)
{
request_rec *mainreq;
digest_header_rec *resp;
digest_config_rec *conf;
if (strcasecmp(auth_type, "Digest"))
return DECLINED;
/* get the client response and mark */
mainreq = r;
while (mainreq->main != NULL) {
mainreq = mainreq->main;
}
while (mainreq->prev != NULL) {
mainreq = mainreq->prev;
}
resp = (digest_header_rec *) ap_get_module_config(mainreq->request_config,
&auth_digest_module);
resp->needed_auth = 1;
/* get our conf */
conf = (digest_config_rec *) ap_get_module_config(r->per_dir_config,
&auth_digest_module);
note_digest_auth_failure(r, conf, resp, 0);
return OK;
}
/*
* Authorization header verification code
*/
static authn_status get_hash(request_rec *r, const char *user,
digest_config_rec *conf)
{
authn_status auth_result;
char *password;
authn_provider_list *current_provider;
current_provider = conf->providers;
do {
const authn_provider *provider;
/* For now, if a provider isn't set, we'll be nice and use the file
* provider.
*/
if (!current_provider) {
provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
AUTHN_DEFAULT_PROVIDER,
AUTHN_PROVIDER_VERSION);
if (!provider || !provider->get_realm_hash) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01770)
"No Authn provider configured");
auth_result = AUTH_GENERAL_ERROR;
break;
}
apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, AUTHN_DEFAULT_PROVIDER);
}
else {
provider = current_provider->provider;
apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, current_provider->provider_name);
}
/* We expect the password to be md5 hash of user:realm:password */
auth_result = provider->get_realm_hash(r, user, conf->realm,
&password);
apr_table_unset(r->notes, AUTHN_PROVIDER_NAME_NOTE);
/* Something occured. Stop checking. */
if (auth_result != AUTH_USER_NOT_FOUND) {
break;
}
/* If we're not really configured for providers, stop now. */
if (!conf->providers) {
break;
}
current_provider = current_provider->next;
} while (current_provider);
if (auth_result == AUTH_USER_FOUND) {
conf->ha1 = password;
}
return auth_result;
}
static int check_nc(const request_rec *r, const digest_header_rec *resp,
const digest_config_rec *conf)
{
unsigned long nc;
const char *snc = resp->nonce_count;
char *endptr;
if (conf->check_nc && !client_shm) {
/* Shouldn't happen, but just in case... */
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(01771)
"cannot check nonce count without shared memory");
return OK;
}
if (!conf->check_nc || !client_shm) {
return OK;
}
if (!apr_is_empty_array(conf->qop_list) &&
!strcasecmp(*(const char **)(conf->qop_list->elts), "none")) {
/* qop is none, client must not send a nonce count */
if (snc != NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01772)
"invalid nc %s received - no nonce count allowed when qop=none",
snc);
return !OK;
}
/* qop is none, cannot check nonce count */
return OK;
}
nc = strtol(snc, &endptr, 16);
if (endptr < (snc+strlen(snc)) && !apr_isspace(*endptr)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01773)
"invalid nc %s received - not a number", snc);
return !OK;
}
if (!resp->client) {
return !OK;
}
if (nc != resp->client->nonce_count) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01774)
"Warning, possible replay attack: nonce-count "
"check failed: %lu != %lu", nc,
resp->client->nonce_count);
return !OK;
}
return OK;
}
static int check_nonce(request_rec *r, digest_header_rec *resp,
const digest_config_rec *conf)
{
apr_time_t dt;
time_rec nonce_time;
char tmp, hash[NONCE_HASH_LEN+1];
if (strlen(resp->nonce) != NONCE_LEN) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01775)
"invalid nonce %s received - length is not %d",
resp->nonce, NONCE_LEN);
note_digest_auth_failure(r, conf, resp, 1);
return HTTP_UNAUTHORIZED;
}
tmp = resp->nonce[NONCE_TIME_LEN];
resp->nonce[NONCE_TIME_LEN] = '\0';
apr_base64_decode_binary(nonce_time.arr, resp->nonce);
gen_nonce_hash(hash, resp->nonce, resp->opaque, r->server, conf);
resp->nonce[NONCE_TIME_LEN] = tmp;
resp->nonce_time = nonce_time.time;
if (strcmp(hash, resp->nonce+NONCE_TIME_LEN)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01776)
"invalid nonce %s received - hash is not %s",
resp->nonce, hash);
note_digest_auth_failure(r, conf, resp, 1);
return HTTP_UNAUTHORIZED;
}
dt = r->request_time - nonce_time.time;
if (conf->nonce_lifetime > 0 && dt < 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01777)
"invalid nonce %s received - user attempted "
"time travel", resp->nonce);
note_digest_auth_failure(r, conf, resp, 1);
return HTTP_UNAUTHORIZED;
}
if (conf->nonce_lifetime > 0) {
if (dt > conf->nonce_lifetime) {
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0,r, APLOGNO(01778)
"user %s: nonce expired (%.2f seconds old "
"- max lifetime %.2f) - sending new nonce",
r->user, (double)apr_time_sec(dt),
(double)apr_time_sec(conf->nonce_lifetime));
note_digest_auth_failure(r, conf, resp, 1);
return HTTP_UNAUTHORIZED;
}
}
else if (conf->nonce_lifetime == 0 && resp->client) {
if (memcmp(resp->client->last_nonce, resp->nonce, NONCE_LEN)) {
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01779)
"user %s: one-time-nonce mismatch - sending "
"new nonce", r->user);
note_digest_auth_failure(r, conf, resp, 1);
return HTTP_UNAUTHORIZED;
}
}
/* else (lifetime < 0) => never expires */
return OK;
}
/* The actual MD5 code... whee */
/* RFC-2069 */
static const char *old_digest(const request_rec *r,
const digest_header_rec *resp, const char *ha1)
{
const char *ha2;
ha2 = ap_md5(r->pool, (unsigned char *)apr_pstrcat(r->pool, resp->method, ":",
resp->uri, NULL));
return ap_md5(r->pool,
(unsigned char *)apr_pstrcat(r->pool, ha1, ":", resp->nonce,
":", ha2, NULL));
}
/* RFC-2617 */
static const char *new_digest(const request_rec *r,
digest_header_rec *resp,
const digest_config_rec *conf)
{
const char *ha1, *ha2, *a2;
ha1 = conf->ha1;
a2 = apr_pstrcat(r->pool, resp->method, ":", resp->uri, NULL);
ha2 = ap_md5(r->pool, (const unsigned char *)a2);
return ap_md5(r->pool,
(unsigned char *)apr_pstrcat(r->pool, ha1, ":", resp->nonce,
":", resp->nonce_count, ":",
resp->cnonce, ":",
resp->message_qop, ":", ha2,
NULL));
}
static void copy_uri_components(apr_uri_t *dst,
apr_uri_t *src, request_rec *r) {
if (src->scheme && src->scheme[0] != '\0') {
dst->scheme = src->scheme;
}
else {
dst->scheme = (char *) "http";
}
if (src->hostname && src->hostname[0] != '\0') {
dst->hostname = apr_pstrdup(r->pool, src->hostname);
ap_unescape_url(dst->hostname);
}
else {
dst->hostname = (char *) ap_get_server_name(r);
}
if (src->port_str && src->port_str[0] != '\0') {
dst->port = src->port;
}
else {
dst->port = ap_get_server_port(r);
}
if (src->path && src->path[0] != '\0') {
dst->path = apr_pstrdup(r->pool, src->path);
ap_unescape_url(dst->path);
}
else {
dst->path = src->path;
}
if (src->query && src->query[0] != '\0') {
dst->query = apr_pstrdup(r->pool, src->query);
ap_unescape_url(dst->query);
}
else {
dst->query = src->query;
}
dst->hostinfo = src->hostinfo;
}
/* These functions return 0 if client is OK, and proper error status
* if not... either HTTP_UNAUTHORIZED, if we made a check, and it failed, or
* HTTP_INTERNAL_SERVER_ERROR, if things are so totally confused that we
* couldn't figure out how to tell if the client is authorized or not.
*
* If they return DECLINED, and all other modules also decline, that's
* treated by the server core as a configuration error, logged and
* reported as such.
*/
/* Determine user ID, and check if the attributes are correct, if it
* really is that user, if the nonce is correct, etc.
*/
static int authenticate_digest_user(request_rec *r)
{
digest_config_rec *conf;
digest_header_rec *resp;
request_rec *mainreq;
const char *t;
int res;
authn_status return_code;
/* do we require Digest auth for this URI? */
if (!(t = ap_auth_type(r)) || strcasecmp(t, "Digest")) {
return DECLINED;
}
if (!ap_auth_name(r)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01780)
"need AuthName: %s", r->uri);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* get the client response and mark */
mainreq = r;
while (mainreq->main != NULL) {
mainreq = mainreq->main;
}
while (mainreq->prev != NULL) {
mainreq = mainreq->prev;
}
resp = (digest_header_rec *) ap_get_module_config(mainreq->request_config,
&auth_digest_module);
resp->needed_auth = 1;
/* get our conf */
conf = (digest_config_rec *) ap_get_module_config(r->per_dir_config,
&auth_digest_module);
/* check for existence and syntax of Auth header */
if (resp->auth_hdr_sts != VALID) {
if (resp->auth_hdr_sts == NOT_DIGEST) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01781)
"client used wrong authentication scheme `%s': %s",
resp->scheme, r->uri);
}
else if (resp->auth_hdr_sts == INVALID) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01782)
"missing user, realm, nonce, uri, digest, "
"cnonce, or nonce_count in authorization header: %s",
r->uri);
}
/* else (resp->auth_hdr_sts == NO_HEADER) */
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
r->user = (char *) resp->username;
r->ap_auth_type = (char *) "Digest";
/* check the auth attributes */
if (strcmp(resp->uri, resp->raw_request_uri)) {
/* Hmm, the simple match didn't work (probably a proxy modified the
* request-uri), so lets do a more sophisticated match
*/
apr_uri_t r_uri, d_uri;
copy_uri_components(&r_uri, resp->psd_request_uri, r);
if (apr_uri_parse(r->pool, resp->uri, &d_uri) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01783)
"invalid uri <%s> in Authorization header",
resp->uri);
return HTTP_BAD_REQUEST;
}
if (d_uri.hostname) {
ap_unescape_url(d_uri.hostname);
}
if (d_uri.path) {
ap_unescape_url(d_uri.path);
}
if (d_uri.query) {
ap_unescape_url(d_uri.query);
}
else if (r_uri.query) {
/* MSIE compatibility hack. MSIE has some RFC issues - doesn't
* include the query string in the uri Authorization component
* or when computing the response component. the second part
* works out ok, since we can hash the header and get the same
* result. however, the uri from the request line won't match
* the uri Authorization component since the header lacks the
* query string, leaving us incompatable with a (broken) MSIE.
*
* the workaround is to fake a query string match if in the proper
* environment - BrowserMatch MSIE, for example. the cool thing
* is that if MSIE ever fixes itself the simple match ought to
* work and this code won't be reached anyway, even if the
* environment is set.
*/
if (apr_table_get(r->subprocess_env,
"AuthDigestEnableQueryStringHack")) {
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(01784)
"applying AuthDigestEnableQueryStringHack "
"to uri <%s>", resp->raw_request_uri);
d_uri.query = r_uri.query;
}
}
if (r->method_number == M_CONNECT) {
if (!r_uri.hostinfo || strcmp(resp->uri, r_uri.hostinfo)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01785)
"uri mismatch - <%s> does not match "
"request-uri <%s>", resp->uri, r_uri.hostinfo);
return HTTP_BAD_REQUEST;
}
}
else if (
/* check hostname matches, if present */
(d_uri.hostname && d_uri.hostname[0] != '\0'
&& strcasecmp(d_uri.hostname, r_uri.hostname))
/* check port matches, if present */
|| (d_uri.port_str && d_uri.port != r_uri.port)
/* check that server-port is default port if no port present */
|| (d_uri.hostname && d_uri.hostname[0] != '\0'
&& !d_uri.port_str && r_uri.port != ap_default_port(r))
/* check that path matches */
|| (d_uri.path != r_uri.path
/* either exact match */
&& (!d_uri.path || !r_uri.path
|| strcmp(d_uri.path, r_uri.path))
/* or '*' matches empty path in scheme://host */
&& !(d_uri.path && !r_uri.path && resp->psd_request_uri->hostname
&& d_uri.path[0] == '*' && d_uri.path[1] == '\0'))
/* check that query matches */
|| (d_uri.query != r_uri.query
&& (!d_uri.query || !r_uri.query
|| strcmp(d_uri.query, r_uri.query)))
) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01786)
"uri mismatch - <%s> does not match "
"request-uri <%s>", resp->uri, resp->raw_request_uri);
return HTTP_BAD_REQUEST;
}
}
if (resp->opaque && resp->opaque_num == 0) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01787)
"received invalid opaque - got `%s'",
resp->opaque);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
if (!conf->realm) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02533)
"realm mismatch - got `%s' but no realm specified",
resp->realm);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
if (!resp->realm || strcmp(resp->realm, conf->realm)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01788)
"realm mismatch - got `%s' but expected `%s'",
resp->realm, conf->realm);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
if (resp->algorithm != NULL
&& strcasecmp(resp->algorithm, "MD5")) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01789)
"unknown algorithm `%s' received: %s",
resp->algorithm, r->uri);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
return_code = get_hash(r, r->user, conf);
if (return_code == AUTH_USER_NOT_FOUND) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01790)
"user `%s' in realm `%s' not found: %s",
r->user, conf->realm, r->uri);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
else if (return_code == AUTH_USER_FOUND) {
/* we have a password, so continue */
}
else if (return_code == AUTH_DENIED) {
/* authentication denied in the provider before attempting a match */
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01791)
"user `%s' in realm `%s' denied by provider: %s",
r->user, conf->realm, r->uri);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
else if (return_code == AUTH_HANDLED) {
return r->status;
}
else {
/* AUTH_GENERAL_ERROR (or worse)
* We'll assume that the module has already said what its error
* was in the logs.
*/
return HTTP_INTERNAL_SERVER_ERROR;
}
if (resp->message_qop == NULL) {
/* old (rfc-2069) style digest */
if (strcmp(resp->digest, old_digest(r, resp, conf->ha1))) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01792)
"user %s: password mismatch: %s", r->user,
r->uri);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
}
else {
const char *exp_digest;
int match = 0, idx;
const char **tmp = (const char **)(conf->qop_list->elts);
for (idx = 0; idx < conf->qop_list->nelts; idx++) {
if (!strcasecmp(*tmp, resp->message_qop)) {
match = 1;
break;
}
++tmp;
}
if (!match
&& !(apr_is_empty_array(conf->qop_list)
&& !strcasecmp(resp->message_qop, "auth"))) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01793)
"invalid qop `%s' received: %s",
resp->message_qop, r->uri);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
exp_digest = new_digest(r, resp, conf);
if (!exp_digest) {
/* we failed to allocate a client struct */
return HTTP_INTERNAL_SERVER_ERROR;
}
if (strcmp(resp->digest, exp_digest)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01794)
"user %s: password mismatch: %s", r->user,
r->uri);
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
}
if (check_nc(r, resp, conf) != OK) {
note_digest_auth_failure(r, conf, resp, 0);
return HTTP_UNAUTHORIZED;
}
/* Note: this check is done last so that a "stale=true" can be
generated if the nonce is old */
if ((res = check_nonce(r, resp, conf))) {
return res;
}
return OK;
}
/*
* Authorization-Info header code
*/
static int add_auth_info(request_rec *r)
{
const digest_config_rec *conf =
(digest_config_rec *) ap_get_module_config(r->per_dir_config,
&auth_digest_module);
digest_header_rec *resp =
(digest_header_rec *) ap_get_module_config(r->request_config,
&auth_digest_module);
const char *ai = NULL, *nextnonce = "";
if (resp == NULL || !resp->needed_auth || conf == NULL) {
return OK;
}
/* 2069-style entity-digest is not supported (it's too hard, and
* there are no clients which support 2069 but not 2617). */
/* setup nextnonce
*/
if (conf->nonce_lifetime > 0) {
/* send nextnonce if current nonce will expire in less than 30 secs */
if ((r->request_time - resp->nonce_time) > (conf->nonce_lifetime-NEXTNONCE_DELTA)) {
nextnonce = apr_pstrcat(r->pool, ", nextnonce=\"",
gen_nonce(r->pool, r->request_time,
resp->opaque, r->server, conf),
"\"", NULL);
if (resp->client)
resp->client->nonce_count = 0;
}
}
else if (conf->nonce_lifetime == 0 && resp->client) {
const char *nonce = gen_nonce(r->pool, 0, resp->opaque, r->server,
conf);
nextnonce = apr_pstrcat(r->pool, ", nextnonce=\"", nonce, "\"", NULL);
memcpy(resp->client->last_nonce, nonce, NONCE_LEN+1);
}
/* else nonce never expires, hence no nextnonce */
/* do rfc-2069 digest
*/
if (!apr_is_empty_array(conf->qop_list) &&
!strcasecmp(*(const char **)(conf->qop_list->elts), "none")
&& resp->message_qop == NULL) {
/* use only RFC-2069 format */
ai = nextnonce;
}
else {
const char *resp_dig, *ha1, *a2, *ha2;
/* calculate rspauth attribute
*/
ha1 = conf->ha1;
a2 = apr_pstrcat(r->pool, ":", resp->uri, NULL);
ha2 = ap_md5(r->pool, (const unsigned char *)a2);
resp_dig = ap_md5(r->pool,
(unsigned char *)apr_pstrcat(r->pool, ha1, ":",
resp->nonce, ":",
resp->nonce_count, ":",
resp->cnonce, ":",
resp->message_qop ?
resp->message_qop : "",
":", ha2, NULL));
/* assemble Authentication-Info header
*/
ai = apr_pstrcat(r->pool,
"rspauth=\"", resp_dig, "\"",
nextnonce,
resp->cnonce ? ", cnonce=\"" : "",
resp->cnonce
? ap_escape_quotes(r->pool, resp->cnonce)
: "",
resp->cnonce ? "\"" : "",
resp->nonce_count ? ", nc=" : "",
resp->nonce_count ? resp->nonce_count : "",
resp->message_qop ? ", qop=" : "",
resp->message_qop ? resp->message_qop : "",
NULL);
}
if (ai && ai[0]) {
apr_table_mergen(r->headers_out,
(PROXYREQ_PROXY == r->proxyreq)
? "Proxy-Authentication-Info"
: "Authentication-Info",
ai);
}
return OK;
}
static void register_hooks(apr_pool_t *p)
{
static const char * const cfgPost[]={ "http_core.c", NULL };
static const char * const parsePre[]={ "mod_proxy.c", NULL };
ap_hook_pre_config(pre_init, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(initialize_module, NULL, cfgPost, APR_HOOK_MIDDLE);
ap_hook_child_init(initialize_child, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_read_request(parse_hdr_and_update_nc, parsePre, NULL, APR_HOOK_MIDDLE);
ap_hook_check_authn(authenticate_digest_user, NULL, NULL, APR_HOOK_MIDDLE,
AP_AUTH_INTERNAL_PER_CONF);
ap_hook_fixups(add_auth_info, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_note_auth_failure(hook_note_digest_auth_failure, NULL, NULL,
APR_HOOK_MIDDLE);
}
AP_DECLARE_MODULE(auth_digest) =
{
STANDARD20_MODULE_STUFF,
create_digest_dir_config, /* dir config creater */
NULL, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server config */
digest_cmds, /* command table */
register_hooks /* register hooks */
};