~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to modules/aaa/mod_auth_basic.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
 
2
 * contributor license agreements.  See the NOTICE file distributed with
 
3
 * this work for additional information regarding copyright ownership.
 
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
5
 * (the "License"); you may not use this file except in compliance with
 
6
 * the License.  You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "apr_strings.h"
 
18
#include "apr_md5.h"            /* for apr_password_validate */
 
19
#include "apr_lib.h"            /* for apr_isspace */
 
20
#include "apr_base64.h"         /* for apr_base64_decode et al */
 
21
#define APR_WANT_STRFUNC        /* for strcasecmp */
 
22
#include "apr_want.h"
 
23
 
 
24
#include "ap_config.h"
 
25
#include "httpd.h"
 
26
#include "http_config.h"
 
27
#include "http_core.h"
 
28
#include "http_log.h"
 
29
#include "http_protocol.h"
 
30
#include "http_request.h"
 
31
#include "ap_provider.h"
 
32
 
 
33
#include "mod_auth.h"
 
34
 
 
35
typedef struct {
 
36
    authn_provider_list *providers;
 
37
    char *dir;
 
38
    int authoritative;
 
39
} auth_basic_config_rec;
 
40
 
 
41
static void *create_auth_basic_dir_config(apr_pool_t *p, char *d)
 
42
{
 
43
    auth_basic_config_rec *conf = apr_pcalloc(p, sizeof(*conf));
 
44
 
 
45
    conf->dir = d;
 
46
    /* Any failures are fatal. */
 
47
    conf->authoritative = 1;
 
48
 
 
49
    return conf;
 
50
}
 
51
 
 
52
static const char *add_authn_provider(cmd_parms *cmd, void *config,
 
53
                                      const char *arg)
 
54
{
 
55
    auth_basic_config_rec *conf = (auth_basic_config_rec*)config;
 
56
    authn_provider_list *newp;
 
57
 
 
58
    newp = apr_pcalloc(cmd->pool, sizeof(authn_provider_list));
 
59
    newp->provider_name = apr_pstrdup(cmd->pool, arg);
 
60
 
 
61
    /* lookup and cache the actual provider now */
 
62
    newp->provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
 
63
                                        newp->provider_name, "0");
 
64
 
 
65
    if (newp->provider == NULL) {
 
66
        /* by the time they use it, the provider should be loaded and
 
67
           registered with us. */
 
68
        return apr_psprintf(cmd->pool,
 
69
                            "Unknown Authn provider: %s",
 
70
                            newp->provider_name);
 
71
    }
 
72
 
 
73
    if (!newp->provider->check_password) {
 
74
        /* if it doesn't provide the appropriate function, reject it */
 
75
        return apr_psprintf(cmd->pool,
 
76
                            "The '%s' Authn provider doesn't support "
 
77
                            "Basic Authentication", newp->provider_name);
 
78
    }
 
79
 
 
80
    /* Add it to the list now. */
 
81
    if (!conf->providers) {
 
82
        conf->providers = newp;
 
83
    }
 
84
    else {
 
85
        authn_provider_list *last = conf->providers;
 
86
 
 
87
        while (last->next) {
 
88
            last = last->next;
 
89
        }
 
90
        last->next = newp;
 
91
    }
 
92
 
 
93
    return NULL;
 
94
}
 
95
 
 
96
static const command_rec auth_basic_cmds[] =
 
97
{
 
98
    AP_INIT_ITERATE("AuthBasicProvider", add_authn_provider, NULL, OR_AUTHCFG,
 
99
                    "specify the auth providers for a directory or location"),
 
100
    AP_INIT_FLAG("AuthBasicAuthoritative", ap_set_flag_slot,
 
101
                 (void *)APR_OFFSETOF(auth_basic_config_rec, authoritative),
 
102
                 OR_AUTHCFG,
 
103
                 "Set to 'Off' to allow access control to be passed along to "
 
104
                 "lower modules if the UserID is not known to this module"),
 
105
    {NULL}
 
106
};
 
107
 
 
108
module AP_MODULE_DECLARE_DATA auth_basic_module;
 
109
 
 
110
/* These functions return 0 if client is OK, and proper error status
 
111
 * if not... either HTTP_UNAUTHORIZED, if we made a check, and it failed, or
 
112
 * HTTP_INTERNAL_SERVER_ERROR, if things are so totally confused that we
 
113
 * couldn't figure out how to tell if the client is authorized or not.
 
114
 *
 
115
 * If they return DECLINED, and all other modules also decline, that's
 
116
 * treated by the server core as a configuration error, logged and
 
117
 * reported as such.
 
118
 */
 
119
 
 
120
static void note_basic_auth_failure(request_rec *r)
 
121
{
 
122
    apr_table_setn(r->err_headers_out,
 
123
                   (PROXYREQ_PROXY == r->proxyreq) ? "Proxy-Authenticate"
 
124
                                                   : "WWW-Authenticate",
 
125
                   apr_pstrcat(r->pool, "Basic realm=\"", ap_auth_name(r),
 
126
                               "\"", NULL));
 
127
}
 
128
 
 
129
static int get_basic_auth(request_rec *r, const char **user,
 
130
                          const char **pw)
 
131
{
 
132
    const char *auth_line;
 
133
    char *decoded_line;
 
134
    int length;
 
135
 
 
136
    /* Get the appropriate header */
 
137
    auth_line = apr_table_get(r->headers_in, (PROXYREQ_PROXY == r->proxyreq)
 
138
                                              ? "Proxy-Authorization"
 
139
                                              : "Authorization");
 
140
 
 
141
    if (!auth_line) {
 
142
        note_basic_auth_failure(r);
 
143
        return HTTP_UNAUTHORIZED;
 
144
    }
 
145
 
 
146
    if (strcasecmp(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
 
147
        /* Client tried to authenticate using wrong auth scheme */
 
148
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
149
                      "client used wrong authentication scheme: %s", r->uri);
 
150
        note_basic_auth_failure(r);
 
151
        return HTTP_UNAUTHORIZED;
 
152
    }
 
153
 
 
154
    /* Skip leading spaces. */
 
155
    while (apr_isspace(*auth_line)) {
 
156
        auth_line++;
 
157
    }
 
158
 
 
159
    decoded_line = apr_palloc(r->pool, apr_base64_decode_len(auth_line) + 1);
 
160
    length = apr_base64_decode(decoded_line, auth_line);
 
161
    /* Null-terminate the string. */
 
162
    decoded_line[length] = '\0';
 
163
 
 
164
    *user = ap_getword_nulls(r->pool, (const char**)&decoded_line, ':');
 
165
    *pw = decoded_line;
 
166
 
 
167
    /* set the user, even though the user is unauthenticated at this point */
 
168
    r->user = (char *) *user;
 
169
 
 
170
    return OK;
 
171
}
 
172
 
 
173
/* Determine user ID, and check if it really is that user, for HTTP
 
174
 * basic authentication...
 
175
 */
 
176
static int authenticate_basic_user(request_rec *r)
 
177
{
 
178
    auth_basic_config_rec *conf = ap_get_module_config(r->per_dir_config,
 
179
                                                       &auth_basic_module);
 
180
    const char *sent_user, *sent_pw, *current_auth;
 
181
    int res;
 
182
    authn_status auth_result;
 
183
    authn_provider_list *current_provider;
 
184
 
 
185
    /* Are we configured to be Basic auth? */
 
186
    current_auth = ap_auth_type(r);
 
187
    if (!current_auth || strcasecmp(current_auth, "Basic")) {
 
188
        return DECLINED;
 
189
    }
 
190
 
 
191
    /* We need an authentication realm. */
 
192
    if (!ap_auth_name(r)) {
 
193
        ap_log_rerror(APLOG_MARK, APLOG_ERR,
 
194
                      0, r, "need AuthName: %s", r->uri);
 
195
        return HTTP_INTERNAL_SERVER_ERROR;
 
196
    }
 
197
 
 
198
    r->ap_auth_type = "Basic";
 
199
 
 
200
    res = get_basic_auth(r, &sent_user, &sent_pw);
 
201
    if (res) {
 
202
        return res;
 
203
    }
 
204
 
 
205
    current_provider = conf->providers;
 
206
    do {
 
207
        const authn_provider *provider;
 
208
 
 
209
        /* For now, if a provider isn't set, we'll be nice and use the file
 
210
         * provider.
 
211
         */
 
212
        if (!current_provider) {
 
213
            provider = ap_lookup_provider(AUTHN_PROVIDER_GROUP,
 
214
                                          AUTHN_DEFAULT_PROVIDER, "0");
 
215
 
 
216
            if (!provider || !provider->check_password) {
 
217
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
218
                              "No Authn provider configured");
 
219
                auth_result = AUTH_GENERAL_ERROR;
 
220
                break;
 
221
            }
 
222
            apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, AUTHN_DEFAULT_PROVIDER);
 
223
        }
 
224
        else {
 
225
            provider = current_provider->provider;
 
226
            apr_table_setn(r->notes, AUTHN_PROVIDER_NAME_NOTE, current_provider->provider_name);
 
227
        }
 
228
 
 
229
 
 
230
        auth_result = provider->check_password(r, sent_user, sent_pw);
 
231
 
 
232
        apr_table_unset(r->notes, AUTHN_PROVIDER_NAME_NOTE);
 
233
 
 
234
        /* Something occured. Stop checking. */
 
235
        if (auth_result != AUTH_USER_NOT_FOUND) {
 
236
            break;
 
237
        }
 
238
 
 
239
        /* If we're not really configured for providers, stop now. */
 
240
        if (!conf->providers) {
 
241
            break;
 
242
        }
 
243
 
 
244
        current_provider = current_provider->next;
 
245
    } while (current_provider);
 
246
 
 
247
    if (auth_result != AUTH_GRANTED) {
 
248
        int return_code;
 
249
 
 
250
        /* If we're not authoritative, then any error is ignored. */
 
251
        if (!(conf->authoritative) && auth_result != AUTH_DENIED) {
 
252
            return DECLINED;
 
253
        }
 
254
 
 
255
        switch (auth_result) {
 
256
        case AUTH_DENIED:
 
257
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
258
                      "user %s: authentication failure for \"%s\": "
 
259
                      "Password Mismatch",
 
260
                      sent_user, r->uri);
 
261
            return_code = HTTP_UNAUTHORIZED;
 
262
            break;
 
263
        case AUTH_USER_NOT_FOUND:
 
264
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
265
                      "user %s not found: %s", sent_user, r->uri);
 
266
            return_code = HTTP_UNAUTHORIZED;
 
267
            break;
 
268
        case AUTH_GENERAL_ERROR:
 
269
        default:
 
270
            /* We'll assume that the module has already said what its error
 
271
             * was in the logs.
 
272
             */
 
273
            return_code = HTTP_INTERNAL_SERVER_ERROR;
 
274
            break;
 
275
        }
 
276
 
 
277
        /* If we're returning 403, tell them to try again. */
 
278
        if (return_code == HTTP_UNAUTHORIZED) {
 
279
            note_basic_auth_failure(r);
 
280
        }
 
281
        return return_code;
 
282
    }
 
283
 
 
284
    return OK;
 
285
}
 
286
 
 
287
static void register_hooks(apr_pool_t *p)
 
288
{
 
289
    ap_hook_check_user_id(authenticate_basic_user,NULL,NULL,APR_HOOK_MIDDLE);
 
290
}
 
291
 
 
292
module AP_MODULE_DECLARE_DATA auth_basic_module =
 
293
{
 
294
    STANDARD20_MODULE_STUFF,
 
295
    create_auth_basic_dir_config,  /* dir config creater */
 
296
    NULL,                          /* dir merger --- default is to override */
 
297
    NULL,                          /* server config */
 
298
    NULL,                          /* merge server config */
 
299
    auth_basic_cmds,               /* command apr_table_t */
 
300
    register_hooks                 /* register hooks */
 
301
};