]> granicus.if.org Git - apache/blob - modules/aaa/mod_authz_owner.c
update license to 2004.
[apache] / modules / aaa / mod_authz_owner.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2004 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 /*
60  * http_auth: authentication
61  * 
62  * Rob McCool
63  * 
64  * Adapted to Apache by rst.
65  *
66  * dirkx - Added Authoritative control to allow passing on to lower
67  *         modules if and only if the userid is not known to this
68  *         module. A known user with a faulty or absent password still
69  *         causes an AuthRequired. The default is 'Authoritative', i.e.
70  *         no control is passed along.
71  */
72
73 #include "apr_strings.h"
74 #include "apr_file_info.h"
75 #include "apr_user.h"
76
77 #include "ap_config.h"
78 #include "httpd.h"
79 #include "http_config.h"
80 #include "http_core.h"
81 #include "http_log.h"
82 #include "http_protocol.h"
83 #include "http_request.h"
84
85 #include "mod_auth.h"     /* for AUTHZ_GROUP_NOTE */
86
87 typedef struct {
88     int authoritative;
89 } authz_owner_config_rec;
90
91 static void *create_authz_owner_dir_config(apr_pool_t *p, char *d)
92 {
93     authz_owner_config_rec *conf = apr_palloc(p, sizeof(*conf));
94
95     conf->authoritative = 1; /* keep the fortress secure by default */
96     return conf;
97 }
98
99 static const command_rec authz_owner_cmds[] =
100 {
101     AP_INIT_FLAG("AuthzOwnerAuthoritative", ap_set_flag_slot,
102                  (void *)APR_OFFSETOF(authz_owner_config_rec, authoritative),
103                  OR_AUTHCFG,
104                  "Set to 'Off' to allow access control to be passed along to "
105                  "lower modules. (default is On.)"),
106     {NULL}
107 };
108
109 module AP_MODULE_DECLARE_DATA authz_owner_module;
110
111 static int check_file_owner(request_rec *r)
112 {
113     authz_owner_config_rec *conf = ap_get_module_config(r->per_dir_config,
114                                                         &authz_owner_module);
115     int m = r->method_number;
116     register int x;
117     const char *t, *w;
118     const apr_array_header_t *reqs_arr = ap_requires(r);
119     require_line *reqs;
120     int required_owner = 0;
121     apr_status_t status = 0;
122     char *reason = NULL;
123
124     if (!reqs_arr) {
125         return DECLINED;
126     }
127
128     reqs = (require_line *)reqs_arr->elts;
129     for (x = 0; x < reqs_arr->nelts; x++) {
130
131         /* if authoritative = On then break if a require already failed. */
132         if (reason && conf->authoritative) {
133             break;
134         }
135
136         if (!(reqs[x].method_mask & (AP_METHOD_BIT << m))) {
137             continue;
138         }
139
140         t = reqs[x].requirement;
141         w = ap_getword_white(r->pool, &t);
142
143         if (!strcmp(w, "file-owner")) {
144 #if !APR_HAS_USER
145             if ((required_owner & ~1) && conf->authoritative) {
146                 break;
147             }
148
149             required_owner |= 1; /* remember the requirement */
150             reason = "'Require file-owner' is not supported on this platform.";
151             continue;
152 #else  /* APR_HAS_USER */
153             char *owner = NULL;
154             apr_finfo_t finfo;
155
156             if ((required_owner & ~1) && conf->authoritative) {
157                 break;
158             }
159
160             required_owner |= 1; /* remember the requirement */
161
162             if (!r->filename) {
163                 reason = "no filename available";
164                 continue;
165             }
166
167             status = apr_stat(&finfo, r->filename, APR_FINFO_USER, r->pool);
168             if (status != APR_SUCCESS) {
169                 reason = apr_pstrcat(r->pool, "could not stat file ",
170                                      r->filename, NULL);
171                 continue;
172             }
173
174             if (!(finfo.valid & APR_FINFO_USER)) {
175                 reason = "no file owner information available";
176                 continue;
177             }
178
179             status = apr_uid_name_get(&owner, finfo.user, r->pool);
180             if (status != APR_SUCCESS || !owner) {
181                 reason = "could not get name of file owner";
182                 continue;
183             }
184
185             if (strcmp(owner, r->user)) {
186                 reason = apr_psprintf(r->pool, "file owner %s does not match.",
187                                       owner);
188                 continue;
189             }
190
191             /* this user is authorized */
192             return OK;
193 #endif /* APR_HAS_USER */
194         }
195
196         /* file-group only figures out the file's group and lets
197          * other modules do the actual authorization (against a group file/db).
198          * Thus, these modules have to hook themselves after
199          * mod_authz_owner and of course recognize 'file-group', too.
200          */
201         if (!strcmp(w, "file-group")) {
202 #if !APR_HAS_USER
203             if ((required_owner & ~6) && conf->authoritative) {
204                 break;
205             }
206
207             required_owner |= 2; /* remember the requirement */
208             reason = "'Require file-group' is not supported on this platform.";
209             continue;
210 #else  /* APR_HAS_USER */
211             char *group = NULL;
212             apr_finfo_t finfo;
213
214             if ((required_owner & ~6) && conf->authoritative) {
215                 break;
216             }
217
218             required_owner |= 2; /* remember the requirement */
219
220             if (!r->filename) {
221                 reason = "no filename available";
222                 continue;
223             }
224
225             status = apr_stat(&finfo, r->filename, APR_FINFO_GROUP, r->pool);
226             if (status != APR_SUCCESS) {
227                 reason = apr_pstrcat(r->pool, "could not stat file ",
228                                      r->filename, NULL);
229                 continue;
230             }
231
232             if (!(finfo.valid & APR_FINFO_GROUP)) {
233                 reason = "no file group information available";
234                 continue;
235             }
236
237             status = apr_gid_name_get(&group, finfo.group, r->pool);
238             if (status != APR_SUCCESS || !group) {
239                 reason = "could not get name of file group";
240                 continue;
241             }
242
243             /* store group name in a note and let others decide... */
244             apr_table_setn(r->notes, AUTHZ_GROUP_NOTE, group);
245             required_owner |= 4;
246             continue;
247 #endif /* APR_HAS_USER */
248         }
249     }
250
251     if (!required_owner || !conf->authoritative) {
252         return DECLINED;
253     }
254     
255     /* allow file-group passed to group db modules either if this is the
256      * only applicable requirement here or if a file-owner failed but we're
257      * not authoritative.
258      * This allows configurations like:
259      *
260      * AuthzOwnerAuthoritative Off
261      * require file-owner
262      * require file-group
263      *
264      * with the semantical meaning of "either owner or group must match"
265      * (inclusive or)
266      *
267      * [ 6 == 2 | 4; 7 == 1 | 2 | 4 ] should I use #defines instead?
268      */
269     if (required_owner == 6 || (required_owner == 7 && !conf->authoritative)) {
270         return DECLINED;
271     }
272
273     ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
274                   "Authorization of user %s to access %s failed, reason: %s",
275                   r->user, r->uri, reason ? reason : "unknown");
276
277     ap_note_auth_failure(r);
278     return HTTP_UNAUTHORIZED;
279 }
280
281 static void register_hooks(apr_pool_t *p)
282 {
283     ap_hook_auth_checker(check_file_owner, NULL, NULL, APR_HOOK_MIDDLE);
284 }
285
286 module AP_MODULE_DECLARE_DATA authz_owner_module =
287 {
288     STANDARD20_MODULE_STUFF,
289     create_authz_owner_dir_config, /* dir config creater */
290     NULL,                          /* dir merger --- default is to override */
291     NULL,                          /* server config */
292     NULL,                          /* merge server config */
293     authz_owner_cmds,              /* command apr_table_t */
294     register_hooks                 /* register hooks */
295 };