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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "apr_strings.h"
18 #include "apr_file_info.h"
21 #include "ap_config.h"
22 #include "ap_provider.h"
24 #include "http_config.h"
25 #include "http_core.h"
27 #include "http_protocol.h"
28 #include "http_request.h"
32 APR_DECLARE_OPTIONAL_FN(char*, authz_owner_get_file_group, (request_rec *r));
34 static const command_rec authz_owner_cmds[] =
39 module AP_MODULE_DECLARE_DATA authz_owner_module;
41 static authz_status fileowner_check_authorization(request_rec *r,
42 const char *require_args,
43 const void *parsed_require_args)
46 apr_status_t status = 0;
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");
54 #else /* APR_HAS_USER */
59 return AUTHZ_DENIED_NO_USER;
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");
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 ",
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");
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");
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");
97 if (strcmp(owner, r->user)) {
98 reason = apr_psprintf(r->pool, "file owner %s does not match.",
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");
106 /* this user is authorized */
107 return AUTHZ_GRANTED;
108 #endif /* APR_HAS_USER */
111 static char *authz_owner_get_file_group(request_rec *r)
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.
122 #else /* APR_HAS_USER */
125 apr_status_t status = 0;
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");
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 ",
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");
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");
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");
163 #endif /* APR_HAS_USER */
166 static const authz_provider authz_fileowner_provider =
168 &fileowner_check_authorization,
172 static void register_hooks(apr_pool_t *p)
174 APR_REGISTER_OPTIONAL_FN(authz_owner_get_file_group);
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);
182 AP_DECLARE_MODULE(authz_owner) =
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 */