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

« back to all changes in this revision

Viewing changes to modules/aaa/mod_authn_file.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
 
 
20
#include "ap_config.h"
 
21
#include "ap_provider.h"
 
22
#include "httpd.h"
 
23
#include "http_config.h"
 
24
#include "http_core.h"
 
25
#include "http_log.h"
 
26
#include "http_protocol.h"
 
27
#include "http_request.h"
 
28
 
 
29
#include "mod_auth.h"
 
30
 
 
31
typedef struct {
 
32
    char *pwfile;
 
33
} authn_file_config_rec;
 
34
 
 
35
static void *create_authn_file_dir_config(apr_pool_t *p, char *d)
 
36
{
 
37
    authn_file_config_rec *conf = apr_palloc(p, sizeof(*conf));
 
38
 
 
39
    conf->pwfile = NULL;     /* just to illustrate the default really */
 
40
    return conf;
 
41
}
 
42
 
 
43
static const char *set_authn_file_slot(cmd_parms *cmd, void *offset,
 
44
                                       const char *f, const char *t)
 
45
{
 
46
    if (t && strcmp(t, "standard")) {
 
47
        return apr_pstrcat(cmd->pool, "Invalid auth file type: ", t, NULL);
 
48
    }
 
49
 
 
50
    return ap_set_file_slot(cmd, offset, f);
 
51
}
 
52
 
 
53
static const command_rec authn_file_cmds[] =
 
54
{
 
55
    AP_INIT_TAKE12("AuthUserFile", set_authn_file_slot,
 
56
                   (void *)APR_OFFSETOF(authn_file_config_rec, pwfile),
 
57
                   OR_AUTHCFG, "text file containing user IDs and passwords"),
 
58
    {NULL}
 
59
};
 
60
 
 
61
module AP_MODULE_DECLARE_DATA authn_file_module;
 
62
 
 
63
static authn_status check_password(request_rec *r, const char *user,
 
64
                                   const char *password)
 
65
{
 
66
    authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
 
67
                                                       &authn_file_module);
 
68
    ap_configfile_t *f;
 
69
    char l[MAX_STRING_LEN];
 
70
    apr_status_t status;
 
71
    char *file_password = NULL;
 
72
 
 
73
    status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
 
74
 
 
75
    if (status != APR_SUCCESS) {
 
76
        ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
 
77
                      "Could not open password file: %s", conf->pwfile);
 
78
        return AUTH_GENERAL_ERROR;
 
79
    }
 
80
 
 
81
    while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
 
82
        const char *rpw, *w;
 
83
 
 
84
        /* Skip # or blank lines. */
 
85
        if ((l[0] == '#') || (!l[0])) {
 
86
            continue;
 
87
        }
 
88
 
 
89
        rpw = l;
 
90
        w = ap_getword(r->pool, &rpw, ':');
 
91
 
 
92
        if (!strcmp(user, w)) {
 
93
            file_password = ap_getword(r->pool, &rpw, ':');
 
94
            break;
 
95
        }
 
96
    }
 
97
    ap_cfg_closefile(f);
 
98
 
 
99
    if (!file_password) {
 
100
        return AUTH_USER_NOT_FOUND;
 
101
    }
 
102
 
 
103
    status = apr_password_validate(password, file_password);
 
104
    if (status != APR_SUCCESS) {
 
105
        return AUTH_DENIED;
 
106
    }
 
107
 
 
108
    return AUTH_GRANTED;
 
109
}
 
110
 
 
111
static authn_status get_realm_hash(request_rec *r, const char *user,
 
112
                                   const char *realm, char **rethash)
 
113
{
 
114
    authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
 
115
                                                       &authn_file_module);
 
116
    ap_configfile_t *f;
 
117
    char l[MAX_STRING_LEN];
 
118
    apr_status_t status;
 
119
    char *file_hash = NULL;
 
120
 
 
121
    status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
 
122
 
 
123
    if (status != APR_SUCCESS) {
 
124
        ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
 
125
                      "Could not open password file: %s", conf->pwfile);
 
126
        return AUTH_GENERAL_ERROR;
 
127
    }
 
128
 
 
129
    while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
 
130
        const char *rpw, *w, *x;
 
131
 
 
132
        /* Skip # or blank lines. */
 
133
        if ((l[0] == '#') || (!l[0])) {
 
134
            continue;
 
135
        }
 
136
 
 
137
        rpw = l;
 
138
        w = ap_getword(r->pool, &rpw, ':');
 
139
        x = ap_getword(r->pool, &rpw, ':');
 
140
 
 
141
        if (x && w && !strcmp(user, w) && !strcmp(realm, x)) {
 
142
            /* Remember that this is a md5 hash of user:realm:password.  */
 
143
            file_hash = ap_getword(r->pool, &rpw, ':');
 
144
            break;
 
145
        }
 
146
    }
 
147
    ap_cfg_closefile(f);
 
148
 
 
149
    if (!file_hash) {
 
150
        return AUTH_USER_NOT_FOUND;
 
151
    }
 
152
 
 
153
    *rethash = file_hash;
 
154
 
 
155
    return AUTH_USER_FOUND;
 
156
}
 
157
 
 
158
static const authn_provider authn_file_provider =
 
159
{
 
160
    &check_password,
 
161
    &get_realm_hash,
 
162
};
 
163
 
 
164
static void register_hooks(apr_pool_t *p)
 
165
{
 
166
    ap_register_provider(p, AUTHN_PROVIDER_GROUP, "file", "0",
 
167
                         &authn_file_provider);
 
168
}
 
169
 
 
170
module AP_MODULE_DECLARE_DATA authn_file_module =
 
171
{
 
172
    STANDARD20_MODULE_STUFF,
 
173
    create_authn_file_dir_config,    /* dir config creater */
 
174
    NULL,                            /* dir merger --- default is to override */
 
175
    NULL,                            /* server config */
 
176
    NULL,                            /* merge server config */
 
177
    authn_file_cmds,                 /* command apr_table_t */
 
178
    register_hooks                   /* register hooks */
 
179
};