upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / aaa / mod_auth.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 /*
18  * http_auth: authentication
19  * 
20  * Rob McCool
21  * 
22  * Adapted to Apache by rst.
23  *
24  * dirkx - Added Authoritative control to allow passing on to lower
25  *         modules if and only if the userid is not known to this
26  *         module. A known user with a faulty or absent password still
27  *         causes an AuthRequired. The default is 'Authoritative', i.e.
28  *         no control is passed along.
29  */
30
31 #include "apr_strings.h"
32 #include "apr_md5.h"            /* for apr_password_validate */
33
34 #include "ap_config.h"
35 #include "httpd.h"
36 #include "http_config.h"
37 #include "http_core.h"
38 #include "http_log.h"
39 #include "http_protocol.h"
40 #include "http_request.h"
41
42
43 typedef struct {
44     char *auth_pwfile;
45     char *auth_grpfile;
46     int auth_authoritative;
47 } auth_config_rec;
48
49 static void *create_auth_dir_config(apr_pool_t *p, char *d)
50 {
51     auth_config_rec *conf = apr_palloc(p, sizeof(*conf));
52
53     conf->auth_pwfile = NULL;     /* just to illustrate the default really */
54     conf->auth_grpfile = NULL;    /* unless you have a broken HP cc */
55     conf->auth_authoritative = 1; /* keep the fortress secure by default */
56     return conf;
57 }
58
59 static const char *set_auth_slot(cmd_parms *cmd, void *offset, const char *f, 
60                                  const char *t)
61 {
62     if (t && strcmp(t, "standard")) {
63         return apr_pstrcat(cmd->pool, "Invalid auth file type: ", t, NULL);
64     }
65
66     return ap_set_file_slot(cmd, offset, f);
67 }
68
69 static const command_rec auth_cmds[] =
70 {
71     AP_INIT_TAKE12("AuthUserFile", set_auth_slot,
72                    (void *)APR_OFFSETOF(auth_config_rec, auth_pwfile),
73                    OR_AUTHCFG, "text file containing user IDs and passwords"),
74     AP_INIT_TAKE12("AuthGroupFile", set_auth_slot,
75                    (void *)APR_OFFSETOF(auth_config_rec, auth_grpfile),
76                    OR_AUTHCFG,
77                    "text file containing group names and member user IDs"),
78     AP_INIT_FLAG("AuthAuthoritative", ap_set_flag_slot,
79                  (void *)APR_OFFSETOF(auth_config_rec, auth_authoritative),
80                  OR_AUTHCFG,
81                  "Set to 'no' to allow access control to be passed along to "
82                  "lower modules if the UserID is not known to this module"),
83     {NULL}
84 };
85
86 module AP_MODULE_DECLARE_DATA auth_module;
87
88 static char *get_pw(request_rec *r, char *user, char *auth_pwfile)
89 {
90     ap_configfile_t *f;
91     char l[MAX_STRING_LEN];
92     const char *rpw, *w;
93     apr_status_t status;
94
95     if ((status = ap_pcfg_openfile(&f, r->pool, auth_pwfile)) != APR_SUCCESS) {
96         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
97                       "Could not open password file: %s", auth_pwfile);
98         return NULL;
99     }
100     while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
101         if ((l[0] == '#') || (!l[0])) {
102             continue;
103         }
104         rpw = l;
105         w = ap_getword(r->pool, &rpw, ':');
106
107         if (!strcmp(user, w)) {
108             ap_cfg_closefile(f);
109             return ap_getword(r->pool, &rpw, ':');
110         }
111     }
112     ap_cfg_closefile(f);
113     return NULL;
114 }
115
116 static apr_table_t *groups_for_user(request_rec *r, char *user, char *grpfile)
117 {
118     apr_pool_t *p = r->pool;
119     ap_configfile_t *f;
120     apr_table_t *grps = apr_table_make(p, 15);
121     apr_pool_t *sp;
122     char l[MAX_STRING_LEN];
123     const char *group_name, *ll, *w;
124     apr_status_t status;
125
126     if ((status = ap_pcfg_openfile(&f, p, grpfile)) != APR_SUCCESS) {
127          ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
128                        "Could not open group file: %s", grpfile);
129         return NULL;
130     }
131
132     apr_pool_create(&sp, p);
133
134     while (!(ap_cfg_getline(l, MAX_STRING_LEN, f))) {
135         if ((l[0] == '#') || (!l[0])) {
136             continue;
137         }
138         ll = l;
139         apr_pool_clear(sp);
140
141         group_name = ap_getword(sp, &ll, ':');
142
143         while (ll[0]) {
144             w = ap_getword_conf(sp, &ll);
145             if (!strcmp(w, user)) {
146                 apr_table_setn(grps, apr_pstrdup(p, group_name), "in");
147                 break;
148             }
149         }
150     }
151     ap_cfg_closefile(f);
152     apr_pool_destroy(sp);
153     return grps;
154 }
155
156 /* These functions return 0 if client is OK, and proper error status
157  * if not... either HTTP_UNAUTHORIZED, if we made a check, and it failed, or
158  * HTTP_INTERNAL_SERVER_ERROR, if things are so totally confused that we
159  * couldn't figure out how to tell if the client is authorized or not.
160  *
161  * If they return DECLINED, and all other modules also decline, that's
162  * treated by the server core as a configuration error, logged and
163  * reported as such.
164  */
165
166 /* Determine user ID, and check if it really is that user, for HTTP
167  * basic authentication...
168  */
169
170 static int authenticate_basic_user(request_rec *r)
171 {
172     auth_config_rec *conf = ap_get_module_config(r->per_dir_config,
173                                                  &auth_module);
174     const char *sent_pw;
175     char *real_pw;
176     apr_status_t invalid_pw;
177     int res;
178
179     if ((res = ap_get_basic_auth_pw(r, &sent_pw))) {
180         return res;
181     }
182
183     if (!conf->auth_pwfile) {
184         return DECLINED;
185     }
186
187     if (!(real_pw = get_pw(r, r->user, conf->auth_pwfile))) {
188         if (!(conf->auth_authoritative)) {
189             return DECLINED;
190         }
191         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
192                       "user %s not found: %s", r->user, r->uri);
193         ap_note_basic_auth_failure(r);
194         return HTTP_UNAUTHORIZED;
195     }
196     invalid_pw = apr_password_validate(sent_pw, real_pw);
197     if (invalid_pw != APR_SUCCESS) {
198         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
199                       "user %s: authentication failure for \"%s\": "
200                       "Password Mismatch",
201                       r->user, r->uri);
202         ap_note_basic_auth_failure(r);
203         return HTTP_UNAUTHORIZED;
204     }
205     return OK;
206 }
207
208 /* Checking ID */
209
210 static int check_user_access(request_rec *r)
211 {
212     auth_config_rec *conf = ap_get_module_config(r->per_dir_config,
213                                                  &auth_module);
214     char *user = r->user;
215     int m = r->method_number;
216     int method_restricted = 0;
217     register int x;
218     const char *t, *w;
219     apr_table_t *grpstatus;
220     const apr_array_header_t *reqs_arr = ap_requires(r);
221     require_line *reqs;
222
223     /* BUG FIX: tadc, 11-Nov-1995.  If there is no "requires" directive, 
224      * then any user will do.
225      */
226     if (!reqs_arr) {
227         return OK;
228     }
229     reqs = (require_line *)reqs_arr->elts;
230
231     if (conf->auth_grpfile) {
232         grpstatus = groups_for_user(r, user, conf->auth_grpfile);
233     }
234     else {
235         grpstatus = NULL;
236     }
237
238     for (x = 0; x < reqs_arr->nelts; x++) {
239
240         if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
241             continue;
242         }
243
244         method_restricted = 1;
245
246         t = reqs[x].requirement;
247         w = ap_getword_white(r->pool, &t);
248         if (!strcmp(w, "valid-user")) {
249             return OK;
250         }
251         if (!strcmp(w, "user")) {
252             while (t[0]) {
253                 w = ap_getword_conf(r->pool, &t);
254                 if (!strcmp(user, w)) {
255                     return OK;
256                 }
257             }
258         }
259         else if (!strcmp(w, "group")) {
260             if (!grpstatus) {
261                 return DECLINED;        /* DBM group?  Something else? */
262             }
263
264             while (t[0]) {
265                 w = ap_getword_conf(r->pool, &t);
266                 if (apr_table_get(grpstatus, w)) {
267                     return OK;
268                 }
269             }
270         }
271         else if (conf->auth_authoritative) {
272             /* if we aren't authoritative, any require directive could be
273              * valid even if we don't grok it.  However, if we are 
274              * authoritative, we can warn the user they did something wrong.
275              * That something could be a missing "AuthAuthoritative off", but
276              * more likely is a typo in the require directive.
277              */
278             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
279                           "access to %s failed, reason: unknown require "
280                           "directive:\"%s\"", r->uri, reqs[x].requirement);
281         }
282     }
283
284     if (!method_restricted) {
285         return OK;
286     }
287
288     if (!(conf->auth_authoritative)) {
289         return DECLINED;
290     }
291
292     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
293                   "access to %s failed, reason: user %s not allowed access",
294                   r->uri, user);
295         
296     ap_note_basic_auth_failure(r);
297     return HTTP_UNAUTHORIZED;
298 }
299
300 static void register_hooks(apr_pool_t *p)
301 {
302     ap_hook_check_user_id(authenticate_basic_user,NULL,NULL,APR_HOOK_MIDDLE);
303     ap_hook_auth_checker(check_user_access,NULL,NULL,APR_HOOK_MIDDLE);
304 }
305
306 module AP_MODULE_DECLARE_DATA auth_module =
307 {
308     STANDARD20_MODULE_STUFF,
309     create_auth_dir_config,     /* dir config creater */
310     NULL,                       /* dir merger --- default is to override */
311     NULL,                       /* server config */
312     NULL,                       /* merge server config */
313     auth_cmds,                  /* command apr_table_t */
314     register_hooks              /* register hooks */
315 };