]> granicus.if.org Git - apache/blob - modules/aaa/mod_authz_owner.c
Allow authz providers to check args while reading the config and allow
[apache] / modules / aaa / mod_authz_owner.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 #include "apr_file_info.h"
19 #include "apr_user.h"
20
21 #include "ap_config.h"
22 #include "ap_provider.h"
23 #include "httpd.h"
24 #include "http_config.h"
25 #include "http_core.h"
26 #include "http_log.h"
27 #include "http_protocol.h"
28 #include "http_request.h"
29
30 #include "mod_auth.h"
31
32 APR_DECLARE_OPTIONAL_FN(char*, authz_owner_get_file_group, (request_rec *r));
33
34 static const command_rec authz_owner_cmds[] =
35 {
36     {NULL}
37 };
38
39 module AP_MODULE_DECLARE_DATA authz_owner_module;
40
41 static authz_status fileowner_check_authorization(request_rec *r,
42                                                   const char *require_args,
43                                                   const void *parsed_require_args)
44 {
45     char *reason = NULL;
46     apr_status_t status = 0;
47
48 #if !APR_HAS_USER
49     reason = "'Require file-owner' is not supported on this platform.";
50     ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
51                   "Authorization of user %s to access %s failed, reason: %s",
52                   r->user, r->uri, reason ? reason : "unknown");
53     return AUTHZ_DENIED;
54 #else  /* APR_HAS_USER */
55     char *owner = NULL;
56     apr_finfo_t finfo;
57
58     if (!r->user) {
59         return AUTHZ_DENIED_NO_USER;
60     }
61
62     if (!r->filename) {
63         reason = "no filename available";
64         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
65                       "Authorization of user %s to access %s failed, reason: %s",
66                       r->user, r->uri, reason ? reason : "unknown");
67         return AUTHZ_DENIED;
68     }
69
70     status = apr_stat(&finfo, r->filename, APR_FINFO_USER, r->pool);
71     if (status != APR_SUCCESS) {
72         reason = apr_pstrcat(r->pool, "could not stat file ",
73                                 r->filename, NULL);
74         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
75                       "Authorization of user %s to access %s failed, reason: %s",
76                       r->user, r->uri, reason ? reason : "unknown");
77         return AUTHZ_DENIED;
78     }
79
80     if (!(finfo.valid & APR_FINFO_USER)) {
81         reason = "no file owner information available";
82         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
83                       "Authorization of user %s to access %s failed, reason: %s",
84                       r->user, r->uri, reason ? reason : "unknown");
85         return AUTHZ_DENIED;
86     }
87
88     status = apr_uid_name_get(&owner, finfo.user, r->pool);
89     if (status != APR_SUCCESS || !owner) {
90         reason = "could not get name of file owner";
91         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
92                       "Authorization of user %s to access %s failed, reason: %s",
93                       r->user, r->uri, reason ? reason : "unknown");
94         return AUTHZ_DENIED;
95     }
96
97     if (strcmp(owner, r->user)) {
98         reason = apr_psprintf(r->pool, "file owner %s does not match.",
99                                 owner);
100         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
101                       "Authorization of user %s to access %s failed, reason: %s",
102                       r->user, r->uri, reason ? reason : "unknown");
103         return AUTHZ_DENIED;
104     }
105
106     /* this user is authorized */
107     return AUTHZ_GRANTED;
108 #endif /* APR_HAS_USER */
109 }
110
111 static char *authz_owner_get_file_group(request_rec *r)
112 {
113     char *reason = NULL;
114
115     /* file-group only figures out the file's group and lets
116     * other modules do the actual authorization (against a group file/db).
117     * Thus, these modules have to hook themselves after
118     * mod_authz_owner and of course recognize 'file-group', too.
119     */
120 #if !APR_HAS_USER
121     return NULL;
122 #else  /* APR_HAS_USER */
123     char *group = NULL;
124     apr_finfo_t finfo;
125     apr_status_t status = 0;
126
127     if (!r->filename) {
128         reason = "no filename available";
129         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
130                       "Authorization of user %s to access %s failed, reason: %s",
131                       r->user, r->uri, reason ? reason : "unknown");
132         return NULL;
133     }
134
135     status = apr_stat(&finfo, r->filename, APR_FINFO_GROUP, r->pool);
136     if (status != APR_SUCCESS) {
137         reason = apr_pstrcat(r->pool, "could not stat file ",
138                                 r->filename, NULL);
139         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
140                       "Authorization of user %s to access %s failed, reason: %s",
141                       r->user, r->uri, reason ? reason : "unknown");
142         return NULL;
143     }
144
145     if (!(finfo.valid & APR_FINFO_GROUP)) {
146         reason = "no file group information available";
147         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
148                       "Authorization of user %s to access %s failed, reason: %s",
149                       r->user, r->uri, reason ? reason : "unknown");
150         return NULL;
151     }
152
153     status = apr_gid_name_get(&group, finfo.group, r->pool);
154     if (status != APR_SUCCESS || !group) {
155         reason = "could not get name of file group";
156         ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
157                       "Authorization of user %s to access %s failed, reason: %s",
158                       r->user, r->uri, reason ? reason : "unknown");
159         return NULL;
160     }
161
162     return group;
163 #endif /* APR_HAS_USER */
164 }
165
166 static const authz_provider authz_fileowner_provider =
167 {
168     &fileowner_check_authorization,
169     NULL,
170 };
171
172 static void register_hooks(apr_pool_t *p)
173 {
174     APR_REGISTER_OPTIONAL_FN(authz_owner_get_file_group);
175
176     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "file-owner",
177                               AUTHZ_PROVIDER_VERSION,
178                               &authz_fileowner_provider,
179                               AP_AUTH_INTERNAL_PER_CONF);
180 }
181
182 AP_DECLARE_MODULE(authz_owner) =
183 {
184     STANDARD20_MODULE_STUFF,
185     NULL,                          /* dir config creater */
186     NULL,                          /* dir merger --- default is to override */
187     NULL,                          /* server config */
188     NULL,                          /* merge server config */
189     authz_owner_cmds,              /* command apr_table_t */
190     register_hooks                 /* register hooks */
191 };