]> granicus.if.org Git - apache/blob - modules/aaa/mod_authn_file.c
Added many log numbers to log statements that
[apache] / modules / aaa / mod_authn_file.c
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
19 #include "ap_config.h"
20 #include "ap_provider.h"
21 #include "httpd.h"
22 #include "http_config.h"
23 #include "http_core.h"
24 #include "http_log.h"
25 #include "http_protocol.h"
26 #include "http_request.h"
27
28 #include "mod_auth.h"
29
30 typedef struct {
31     char *pwfile;
32 } authn_file_config_rec;
33
34 static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL;
35 #define AUTHN_CACHE_STORE(r,user,realm,data) \
36     if (authn_cache_store != NULL) \
37         authn_cache_store((r), "file", (user), (realm), (data))
38
39 static void *create_authn_file_dir_config(apr_pool_t *p, char *d)
40 {
41     authn_file_config_rec *conf = apr_palloc(p, sizeof(*conf));
42
43     conf->pwfile = NULL;     /* just to illustrate the default really */
44     return conf;
45 }
46
47 static const command_rec authn_file_cmds[] =
48 {
49     AP_INIT_TAKE1("AuthUserFile", ap_set_file_slot,
50                   (void *)APR_OFFSETOF(authn_file_config_rec, pwfile),
51                   OR_AUTHCFG, "text file containing user IDs and passwords"),
52     {NULL}
53 };
54
55 module AP_MODULE_DECLARE_DATA authn_file_module;
56
57 static authn_status check_password(request_rec *r, const char *user,
58                                    const char *password)
59 {
60     authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
61                                                        &authn_file_module);
62     ap_configfile_t *f;
63     char l[MAX_STRING_LEN];
64     apr_status_t status;
65     char *file_password = NULL;
66
67     if (!conf->pwfile) {
68         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01619)
69                       "AuthUserFile not specified in the configuration");
70         return AUTH_GENERAL_ERROR;
71     }
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, APLOGNO(01620)
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     AUTHN_CACHE_STORE(r, user, NULL, file_password);
103
104     status = ap_password_validate(r, user, password, file_password);
105     if (status != APR_SUCCESS) {
106         return AUTH_DENIED;
107     }
108
109     return AUTH_GRANTED;
110 }
111
112 static authn_status get_realm_hash(request_rec *r, const char *user,
113                                    const char *realm, char **rethash)
114 {
115     authn_file_config_rec *conf = ap_get_module_config(r->per_dir_config,
116                                                        &authn_file_module);
117     ap_configfile_t *f;
118     char l[MAX_STRING_LEN];
119     apr_status_t status;
120     char *file_hash = NULL;
121
122     if (!conf->pwfile) {
123         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01621)
124                       "AuthUserFile not specified in the configuration");
125         return AUTH_GENERAL_ERROR;
126     }
127
128     status = ap_pcfg_openfile(&f, r->pool, conf->pwfile);
129
130     if (status != APR_SUCCESS) {
131         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(01622)
132                       "Could not open password file: %s", conf->pwfile);
133         return AUTH_GENERAL_ERROR;
134     }
135
136     while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
137         const char *rpw, *w, *x;
138
139         /* Skip # or blank lines. */
140         if ((l[0] == '#') || (!l[0])) {
141             continue;
142         }
143
144         rpw = l;
145         w = ap_getword(r->pool, &rpw, ':');
146         x = ap_getword(r->pool, &rpw, ':');
147
148         if (x && w && !strcmp(user, w) && !strcmp(realm, x)) {
149             /* Remember that this is a md5 hash of user:realm:password.  */
150             file_hash = ap_getword(r->pool, &rpw, ':');
151             break;
152         }
153     }
154     ap_cfg_closefile(f);
155
156     if (!file_hash) {
157         return AUTH_USER_NOT_FOUND;
158     }
159
160     *rethash = file_hash;
161     AUTHN_CACHE_STORE(r, user, realm, file_hash);
162
163     return AUTH_USER_FOUND;
164 }
165
166 static const authn_provider authn_file_provider =
167 {
168     &check_password,
169     &get_realm_hash,
170 };
171
172 static void opt_retr(void)
173 {
174     authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store);
175 }
176 static void register_hooks(apr_pool_t *p)
177 {
178     ap_register_auth_provider(p, AUTHN_PROVIDER_GROUP, "file",
179                               AUTHN_PROVIDER_VERSION,
180                               &authn_file_provider, AP_AUTH_INTERNAL_PER_CONF);
181     ap_hook_optional_fn_retrieve(opt_retr, NULL, NULL, APR_HOOK_MIDDLE);
182 }
183
184 AP_DECLARE_MODULE(authn_file) =
185 {
186     STANDARD20_MODULE_STUFF,
187     create_authn_file_dir_config,    /* dir config creater */
188     NULL,                            /* dir merger --- default is to override */
189     NULL,                            /* server config */
190     NULL,                            /* merge server config */
191     authn_file_cmds,                 /* command apr_table_t */
192     register_hooks                   /* register hooks */
193 };