1 /* ============================================================
2 * Copyright (c) 2003-2006, Ondrej Sury, Piotr Wadas
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 * NOTE: only static members must be "used" to build,
14 * so for time-to-time used routines we don't declare static
15 * mod_vhost_ldap.c --- read virtual host config from LDAP directory
16 * version 2.0 - included ldap-based basic auth & authz
17 * remember to add "-lcrypt" in Makefile if there's a need to generate new password
18 * for now not needed (validation only), this below is almost copy-paste from apache source, htpasswd.c
21 #include "mod_vhost_ldap.h"
23 module AP_MODULE_DECLARE_DATA vhost_ldap_module;
24 /******************************************************************/
25 char *pw_encrypt (const char *clear, const char *salt)
27 //this function encrypts password in unix crypt md5 way
28 extern char *crypt (__const char *__key, __const char *__salt);
29 static char cipher[128];
30 char *cp = crypt (clear, salt);
34 /******************************************************************/
35 static int strschrcount(apr_pool_t * p, const char *src, const char *delim)
40 if(strcasecmp(apr_pstrndup(p, src, i), (char *) delim) == 0) {
46 /******************************************************************/
47 static void *mvhl_dump_config_request ( mvhl_config *currentconf, request_rec *r) {
48 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "BaseDn: %s", currentconf->basedn);
49 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "BindDn: %s", currentconf->binddn);
50 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "BindPw: %s", currentconf->bindpw);
51 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Deref: %d", currentconf->deref);
52 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Enabled: %d", currentconf->enabled);
53 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "UserFilter: %s", currentconf->filter);
54 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "HaveLdapUrl: %d", currentconf->have_ldap_url);
55 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "HaveDeref: %d", currentconf->have_deref);
56 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Url: %s", currentconf->url);
57 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Port: %d", currentconf->port);
58 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Scope: %d", currentconf->scope);
59 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Fallback: %s", currentconf->fallback);
60 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "WucBaseDn: %s", currentconf->wucbasedn);
61 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "WlcBaseDn: %s", currentconf->wlcbasedn);
62 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "AliasesBaseDn: %s", currentconf->aliasesbasedn);
65 /******************************************************************/
66 int header_trace(void *data, const char *key, const char *val)
69 //apr_table_do(header_trace, r, r->headers_out);
70 //apr_table_do(header_trace, (void*)r, r->headers_out,"Content-type", "Content-length", NULL);
71 request_rec *r = (request_rec *)data;
72 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Header Field %s == %s", key, val);
75 /******************************************************************/
76 void log_dump_apr_array(request_rec * r, apr_array_header_t * arr, const char *prefix)
78 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Entering log_dump_apr_array");
80 char **aliases = (char **) arr->elts;
81 for (x = 0; x < arr->nelts; x++) {
82 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " log_dump_apr_array val %d %s %s", x, prefix, aliases[x]);
84 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Leaving log_dump_apr_array");
86 /******************************************************************/
87 static apr_array_header_t *get_parsed_string_atrr_arr(request_rec * r, const char *server_alias_attrvar_line, const char *delim)
90 * This little piece of code creates apr_array from the string seperated by delim.
91 * It's primary usage is to get array of ldap one attribute values, which is
92 * retrieved (when multi-value) as ";" separated string.
94 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Entering get_parsed_string_atrr_arr |%s|", server_alias_attrvar_line);
95 if(server_alias_attrvar_line) {
97 apr_collapse_spaces((char *) server_alias_attrvar_line, server_alias_attrvar_line);
98 int ccount = strschrcount(r->pool, server_alias_attrvar_line, delim) + 1;
100 apr_array_header_t *aliases_arr = apr_array_make(r->pool, ccount, sizeof(char *));
102 curralias = (char **) apr_array_push(aliases_arr);
104 char *curr_server_alias = ap_getword(r->pool, &server_alias_attrvar_line, ';');
105 char *tmp = apr_pstrdup(r->pool, (char *) curr_server_alias);;
109 while(server_alias_attrvar_line[0]) {
110 curr_server_alias = ap_getword(r->pool, &server_alias_attrvar_line, ';');
111 curralias = (char **) apr_array_push(aliases_arr);
112 tmp = apr_pstrdup(r->pool, (char *) curr_server_alias);
115 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Leaving get_parsed_string_atrr_arr OK");
119 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Leaving get_parsed_string_atrr_arr NULL");
122 /******************************************************************/
123 static apr_array_header_t *get_ap_reqs(apr_pool_t * p, mvhl_extconfig_object * extreqc, char *mainservername, char *userlist)
126 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL," get_ap_reqs Entering");
128 apr_array_header_t *res = apr_array_make(p, 2, sizeof(require_line));
131 apr_int64_t limited = -1;
133 rline = (require_line *) apr_array_push(res);
135 //currently we don't support playing with request types
136 rline->method_mask = limited;
138 if(extreqc->extconftype == 1) {
139 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, " get_ap_reqs require valid-user = TRUE server %s",mainservername);
140 rline->requirement = apr_pstrdup(p, (char *) "valid-user");
143 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, " get_ap_reqs require valid-user = FALSE server %s",mainservername);
145 rline->requirement = apr_pstrdup(p, userlist);
147 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL," Leaving get_ap_reqs, returning require line |require %s|", rline->requirement);
150 /******************************************************************/
151 static void mvhl_dovhostconfig(request_rec * r, char *attributes[], const char **vals, mvhl_request * reqc)
153 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Entering vhost configuration");
155 * we got 10 attributes to search for, counting from 0 to 9
158 for ( i = 0; i <= 9; i++ ) {
159 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " VhostConfig Iteration %d :: will get %s", i, attributes[i]);
161 /* 0 apacheServerName */
162 case 0: reqc->name = apr_pstrdup(r->pool, vals[i]); break;
163 /* 1 apacheServerAlias - may be null */
164 case 1: reqc->serveralias = (vals[i]) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, vals[i],(const char *) ";") : NULL; break;
165 /* 2 apacheDocumentRoot */
166 case 2: reqc->docroot = apr_pstrdup(r->pool, vals[i]); break;
167 /* 3 apacheSuexecUid */
168 case 3: reqc->uid = (vals[i]) ? apr_pstrdup(r->pool, vals[i]) : reqc->uid ; break;
169 /* 4 apacheSuexecGid */
170 case 4: reqc->gid = (vals[i]) ? apr_pstrdup(r->pool, vals[i]): reqc->gid ; break;
171 /* 5 apacheServerAdmin */
172 case 5: reqc->admin = (vals[i]) ? apr_pstrdup(r->pool, vals[i]) : NULL ; break;
173 /* 6 apacheExtConfigHasRequireLine */
174 //if there's no HasRequireLine attribute set we assume we don't have reqlines
175 case 6: reqc->has_reqlines = (vals[i] && strcasecmp("TRUE", apr_pstrdup(r->pool, vals[i])) == 0) ? 1 : 0; break;
176 /* 7 apacheLocationOptionsDn */
177 case 7: reqc->rqlocationlines = (vals[i]) ? reqc->rqlocationlines = (apr_array_header_t *) get_parsed_string_atrr_arr(r, vals[i], (const char *) ";") : NULL ; break;
178 /* 8 apacheAliasesConfigEnabled */
179 case 8: reqc->has_aliaslines = ( vals[i] && strcasecmp("TRUE", apr_pstrdup(r->pool, vals[i])) == 0 ) ? 1 : 0; break;
180 /* 9 apacheAliasConfigOptionsDn */
181 case 9: reqc->aliaseslines = (vals[i]) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, vals[i], (const char *) ";") : NULL;
184 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Leaving vhost configuration , exit assignments: ");
185 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, " apacheServerName \'%s\'", reqc->name);
186 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, " apacheServerAdmin \'%s\'", reqc->admin);
187 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, " apacheDocumentRoot \'%s\'", reqc->docroot);
188 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, " apacheSuexecUid \'%s\'", reqc->uid);
189 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r, " apacheSuexecGid \'%s\'", reqc->gid);
191 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
192 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
193 (reqc->has_reqlines && reqc->rqlocationlines ) ?
194 apr_pstrdup(r->pool, "vhost \'%s\' has access control") :
195 apr_pstrdup(r->pool, " vhost \'%s\' doesn't have access control"),
198 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
199 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
200 (reqc->has_aliaslines && reqc->has_aliaslines ) ?
201 apr_pstrdup(r->pool, "vhost \'%s\' has dir aliases") :
202 apr_pstrdup(r->pool, " vhost \'%s\' doesn't have dir aliases"),
205 /******************************************************************/
206 static void mvhl_doextconfig(request_rec * r, char *extconfigattributes[], const char **extconfvals, mvhl_extconfig_object * extreqc)
208 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Entering Access Control config");
210 * we got 6 attributes to search for, counting from 0 to 5
213 for ( i = 0; i <= 5; i++ ) {
214 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Vhost Access Control Iteration %d :: will get %s", i, extconfigattributes[i]);
216 /* 0 apacheExtConfigUri */
217 case 0: extreqc->exturi = (extconfvals[i]) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extconfvals[i], (const char *) ";") : NULL ; break;
218 /* 1 apacheExtConfigRequireValidUser
219 * this value determines whether we have "require valid-user" object (TRUE) ,
220 * or (FALSE) object "require user johny mary dorothy witch"
221 * here set retrieved value, regardless what it is, to play with it later.
223 case 1: extreqc->extconftype = (extconfvals[i] && strcasecmp("TRUE", apr_pstrdup(r->pool, extconfvals[i])) == 0) ? 1 : 0; break;
224 /* 2 apacheExtConfigServerName */
225 case 2: extreqc->extservername = (extconfvals[i]) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extconfvals[i], (const char *) ";") : NULL ; break;
226 /* 3 apacheExtConfigObjectName */
227 case 3: extreqc->extconfname = apr_pstrdup(r->pool, extconfvals[i]); break;
228 /* 4 apacheExtConfigUserDn */
230 extreqc->extusers = (extconfvals[i]) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extconfvals[i], (const char *) ";") : NULL; break;
231 /* 5 apacheExtConfigPath */
233 extreqc->extdir = (extconfvals[i]) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extconfvals[i], (const char *) ";") : NULL ; break;
237 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Leaving Access Control config :: exit assignments");
239 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " This Access Object prompt is \'%s\'", extreqc->extconfname);
240 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,
242 apr_pstrdup(r->pool, " \'%s\' extConfigUri has at least one URI value") :
243 apr_pstrdup(r->pool, " \'%s\' has no extConfigUri assigned"),
244 extreqc->extconfname);
245 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,
247 apr_pstrdup(r->pool, " \'%s\' extConfigPath has at least one directory value") :
248 apr_pstrdup(r->pool, " \'%s\' has no extConfig assigned"),
249 extreqc->extconfname);
250 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
251 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
252 (extreqc->extconftype) ?
253 apr_pstrdup(r->pool, " \'%s\' requires valid-user") :
254 apr_pstrdup(r->pool, " \'%s\' requires user from userlist"),
255 extreqc->extconfname) ;
256 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
257 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
258 (extreqc->extservername ) ?
259 apr_pstrdup(r->pool, " \'%s\' has at least one serverName assigned") :
260 apr_pstrdup(r->pool, " \'%s\' has no assigned serverNames"),
261 extreqc->extconfname);
262 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
263 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
264 (extreqc->extusers ) ?
265 apr_pstrdup(r->pool, " \'%s\' has at least one webuser object assigned") :
266 apr_pstrdup(r->pool, " \'%s\' has no assigned webusers objects"),
267 extreqc->extconfname );
270 /******************************************************************/
271 static void mvhl_doextuserconfig(request_rec * r, char *ldap_webuser_attributes[], const char **extuservals, mvhl_webuser * extuserreqc, char * prefix)
273 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Entering extwebuser Config");
275 * we got 5 attributes to search for, counting from 0 to 4
278 for ( i = 0; i <= 4; i++ ) {
279 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "%s Web user Iteration %d :: will get %s", prefix, i, ldap_webuser_attributes[i]);
281 /* 0 apacheExtConfigUserName */
283 extuserreqc->webusername = apr_pstrdup(r->pool, extuservals[i]);
285 /* 1 apacheExtConfigUserServerName */
287 extuserreqc->webuserserver = ( extuservals[i] ) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extuservals[i], (const char *) ";") : NULL;
289 /* 2 apacheExtConfigUserDirectoryName */
291 extuserreqc->webuserdirectory = ( extuservals[i] ) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extuservals[i], (const char *) ";") : NULL;
294 /* 3 apacheExtConfigUserLocationUri */
295 extuserreqc->webuserlocationuri = ( extuservals[i] ) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extuservals[i], (const char *) ";") : NULL;
299 extuserreqc->webuserpassword = ( extuservals[i] ) ? (apr_array_header_t *) get_parsed_string_atrr_arr(r, extuservals[i], (const char *) ";") : NULL;
303 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Leaving extwebuser Config :: exit assignments");
304 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " This webuser name is \'%s\'", extuserreqc->webusername);
305 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,
306 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
307 ( extuserreqc->webuserserver ) ?
308 apr_pstrdup(r->pool, " \'%s\' has at least one server assigned") :
309 apr_pstrdup(r->pool, " \'%s\' has no serverNames assigned"),
310 extuserreqc->webusername );
311 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
312 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
313 ( extuserreqc->webuserpassword ) ?
314 apr_pstrdup(r->pool, " \'%s\' has at least one password assigned") :
315 apr_pstrdup(r->pool, " \'%s\' has no passwords assigned"),
316 extuserreqc->webusername );
317 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
318 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
319 ( extuserreqc->webuserdirectory ) ?
320 apr_pstrdup(r->pool, " \'%s\' has at least one physical directory assigned") :
321 apr_pstrdup(r->pool, " \'%s\' has no physical directories assigned"),
322 extuserreqc->webusername );
323 ap_log_rerror(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, r,
324 /* following three lines evaluates to one string in which final argument (fourth line) is substituted */
325 ( extuserreqc->webuserlocationuri ) ?
326 apr_pstrdup(r->pool, " \'%s\' has at least one location assigned") :
327 apr_pstrdup(r->pool, " \'%s\' has no locations assigned"),
328 extuserreqc->webusername );
331 /******************************************************************/
332 static void mvhl_doaliasesconfig(request_rec * r, char *aliases_attributes[], const char **aliasesvals, mvhl_aliasconf_object * aliasreqc){
334 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Entering aliasObject Config");
336 * we got 4 attributes to search for, counting from 0 to 3
340 for (i = 0; i <= 3; i++ ) {
341 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Alias config Iteration %d :: will get %s", i, aliases_attributes[i]);
343 /* 0 apacheAliasConfigSourceUri */
345 aliasreqc->aliassourceuri = (apr_array_header_t *) get_parsed_string_atrr_arr(r, aliasesvals[i], (const char *) ";");
346 //aliasreqc->aliassourceuri = apr_pstrdup(r->pool, aliasesvals[i]);
348 /* 1 apacheAliasConfigServerName - MULTI-VALUE */
350 aliasreqc->aliasconfservername = (apr_array_header_t *) get_parsed_string_atrr_arr(r, aliasesvals[i], (const char *) ";");
352 /* 2 apacheAliasConfigTargetDir */
354 aliasreqc->aliastargetdir = apr_pstrdup(r->pool, aliasesvals[i]);
356 /* 3 apacheAliasConfigObjectName */
358 aliasreqc->aliasconfname = apr_pstrdup(r->pool, aliasesvals[i]);
362 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Leaving alias Config :: exit assignments");
363 /* defined ldap schema force alias object to have all of these attributes, so there's no way for existing object without any of them*/
364 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " This alias config object name is \'%s\'", aliasreqc->aliasconfname);
365 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " \'%s\' has at least one SourceUri assigned", aliasreqc->aliasconfname);
366 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " \'%s\' target dir is \'%s\'", aliasreqc->aliasconfname, aliasreqc->aliastargetdir);
367 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, apr_pstrdup(r->pool, " \'%s\' has at least one serverName assigned"), aliasreqc->aliasconfname );
369 /******************************************************************/
370 static int mvhl_authenticate_basic_user(request_rec * r)
372 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " Entering mvhl_authenticate_basic_user");
374 mvhl_webuser *extuserreqc = (mvhl_webuser *) apr_pcalloc(r->pool, sizeof(mvhl_webuser));
375 int rc = ap_get_basic_auth_pw(r, &sent_pw);
376 if(rc != OK) return rc;
378 if(strtrue(r->user) && strtrue(sent_pw)) {
380 char userfilter[FILTER_LENGTH];
381 const char *dn = NULL;
382 const char **extuservals = NULL;
383 util_ldap_connection_t *ldc = NULL;
384 char *prefix = "mvhl_authenticate.. :";
385 char *ldap_webuser_attributes[] = { "apacheExtConfigUserName","apacheExtConfigUserServerName","apacheExtConfigUserDirectoryName","apacheExtConfigUserLocationUri","userPassword",0};
386 mvhl_config *conf = (mvhl_config *) ap_get_module_config(r->server->module_config, &vhost_ldap_module);
388 apr_snprintf(userfilter, FILTER_LENGTH, "(&(%s)(objectClass=apacheExtendedConfigUserObject)(apacheExtConfigUserName=%s))", conf->filter, r->user);
390 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " AuthUser search filter: %s", userfilter);
391 ldc = util_ldap_connection_find(r, conf->host, conf->port, conf->binddn, conf->bindpw, conf->deref, conf->secure);
392 result = util_ldap_cache_getuserdn(r, ldc, conf->url, conf->wucbasedn, conf->scope, ldap_webuser_attributes, userfilter, &dn, &extuservals);
393 util_ldap_connection_close(ldc);
396 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, " User %s found.", r->user);
398 mvhl_doextuserconfig(r, ldap_webuser_attributes, extuservals, extuserreqc, prefix);
401 //we're checking for each password - if any matches, then we return immediately
402 char **passwords = (char **) extuserreqc->webuserpassword->elts;
404 for (x = 0; x < extuserreqc->webuserpassword->nelts; x++) {
405 if ( apr_password_validate(sent_pw, passwords[x]) == OK )
407 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, " APR authentication for user %s at %s successful.", extuserreqc->webusername, r->server->server_hostname);
411 char *prefix = "{CRYPT}";
413 if ( strcasecmp( apr_pstrndup(r->pool, passwords[x], prefixlen ), prefix ) == 0 ) {
414 char *stripped = passwords[x] + prefixlen;
415 char *userinputhash = pw_encrypt (sent_pw, stripped );
416 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, " User entered: \'%s\' ", sent_pw );
417 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, " Retrieved value: \'%s\'", passwords[x] );
418 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, " Retrieved value (stripped): \'%s\'", stripped );
419 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, " User input hash: \'%s\'", userinputhash );
420 if ( strcasecmp( userinputhash , stripped ) == 0 )
422 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, " Unix authentication for user %s at %s successful.", extuserreqc->webusername, r->server->server_hostname);
427 if ( strcasecmp(sent_pw,passwords[x]) == 0 )
429 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, " Clear text authentication for user %s at %s successful.", extuserreqc->webusername, r->server->server_hostname);
435 ap_log_rerror(APLOG_MARK, APLOG_NOTICE | APLOG_NOERRNO, 0, r, " User %s at %s not found", extuserreqc->webusername, r->server->server_hostname);
436 return HTTP_UNAUTHORIZED;
440 ap_note_basic_auth_failure(r);
441 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r,
442 ": Both a username and password must be provided : authentication for user %s at %s failed.",
443 extuserreqc->webusername, r->server->server_hostname);
444 return HTTP_UNAUTHORIZED;
446 ap_note_basic_auth_failure(r);
447 ap_log_rerror(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, r, "Authentication for user %s at %s failed.",
448 extuserreqc->webusername, r->server->server_hostname);
449 return HTTP_UNAUTHORIZED;
452 /******************************************************************/
453 static int check_mvhl_auth_require(char *user, const char *t, request_rec * r)
456 w = ap_getword(r->pool, &t, ' ');
457 if(!strcmp(w, "valid-user")) {
462 if(!strcmp(w, "user")) {
464 w = ap_getword_conf(r->pool, &t);
465 if(!strcmp(user, w)) {
471 return HTTP_UNAUTHORIZED;
475 return HTTP_INTERNAL_SERVER_ERROR;
477 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,
478 ": %s : Reached end of check_mvhl_auth_require!", r->server->server_hostname);
479 return HTTP_INTERNAL_SERVER_ERROR;
481 /******************************************************************/
482 static int mvhl_check_auth(request_rec * r)
484 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,
485 ": mvhl_check_auth, parsing existing ap_requires for %s at %s ", r->user, r->server->server_hostname);
486 char *user = r->user;
490 const apr_array_header_t *reqs_arr = ap_requires(r);
493 reqs = (require_line *) reqs_arr->elts;
495 for (x = 0; x < reqs_arr->nelts; x++) {
496 t = reqs[x].requirement;
497 if((rv = check_mvhl_auth_require(user, t, r)) != HTTP_UNAUTHORIZED) {
503 ap_note_basic_auth_failure(r);
504 return HTTP_UNAUTHORIZED;
506 /******************************************************************/
507 static void *mvhl_create_sconfig(apr_pool_t * p, server_rec * s)
509 //ldap://host[:port]/basedn[?attrib[?scope[?filter]]]
510 mvhl_config *conf = (mvhl_config *) apr_pcalloc(p, sizeof(mvhl_config));
512 conf->have_ldap_url = 0;
513 conf->have_deref = 0;
514 conf->scope = LDAP_SCOPE_SUBTREE;
523 conf->wlcbasedn = NULL;
524 conf->wucbasedn = NULL;
525 conf->aliasesbasedn = NULL;
526 conf->fallback = NULL;
528 conf->alias_enabled = 0;
529 conf->loc_auth_enabled = 0;
530 conf->dir_auth_enabled = 0;
533 /******************************************************************/
534 static void *mvhl_merge_sconfig(apr_pool_t * p, void *parentv, void *childv)
536 mvhl_config *parent = (mvhl_config *) parentv;
537 mvhl_config *child = (mvhl_config *) childv;
538 mvhl_config *conf = (mvhl_config *) apr_pcalloc(p, sizeof(mvhl_config));
540 conf->enabled = (child->enabled ? child->enabled : parent->enabled);
541 conf->binddn = (child->binddn ? child->binddn : parent->binddn);
542 conf->bindpw = (child->bindpw ? child->bindpw : parent->bindpw);
543 conf->fallback = (child->fallback ? child->fallback : parent->fallback);
544 conf->alias_enabled = (child->alias_enabled ? child->alias_enabled : parent->alias_enabled);
545 conf->loc_auth_enabled = (child->loc_auth_enabled ? child->loc_auth_enabled : parent->loc_auth_enabled);
546 conf->dir_auth_enabled = (child->dir_auth_enabled ? child->dir_auth_enabled : parent->dir_auth_enabled);
548 if(child->have_ldap_url) {
549 conf->have_ldap_url = child->have_ldap_url;
550 conf->url = child->url;
551 conf->host = child->host;
552 conf->port = child->port;
553 conf->basedn = child->basedn;
554 conf->scope = child->scope;
555 conf->filter = child->filter;
556 conf->secure = child->secure;
557 conf->wlcbasedn = child->wlcbasedn;
558 conf->wucbasedn = child->wucbasedn;
559 conf->aliasesbasedn = child->aliasesbasedn;
563 conf->have_ldap_url = parent->have_ldap_url;
564 conf->url = parent->url;
565 conf->host = parent->host;
566 conf->port = parent->port;
567 conf->basedn = parent->basedn;
568 conf->scope = parent->scope;
569 conf->filter = parent->filter;
570 conf->secure = parent->secure;
571 conf->wlcbasedn = parent->wlcbasedn;
572 conf->wucbasedn = parent->wucbasedn;
573 conf->aliasesbasedn = parent->aliasesbasedn;
575 if(child->have_deref) {
576 conf->have_deref = child->have_deref;
577 conf->deref = child->deref;
581 conf->have_deref = parent->have_deref;
582 conf->deref = parent->deref;
586 /******************************************************************/
587 static const char *conf_mvhl_url(cmd_parms * cmd, void *dummy, const char *url)
590 apr_ldap_url_desc_t *urld;
592 mvhl_config *conf = (mvhl_config *) ap_get_module_config(cmd->server->module_config, &vhost_ldap_module);
593 result = apr_ldap_url_parse(url, &(urld));
595 if(result != LDAP_SUCCESS) {
597 case LDAP_URL_ERR_MEM: return "0x01 can't allocate memory space";
598 case LDAP_URL_ERR_PARAM: return "0x02 parameter is bad";
599 case LDAP_URL_ERR_BADSCHEME: return "0x03 URL doesn't begin with ldap[si]://";
600 case LDAP_URL_ERR_BADENCLOSURE: return "0x04 URL is missing trailing >";
601 case LDAP_URL_ERR_BADURL: return "0x05 URL is bad";
602 case LDAP_URL_ERR_BADHOST: return "0x06 host port is bad";
603 case LDAP_URL_ERR_BADATTRS: return "0x07 bad (or missing) attributes";
604 case LDAP_URL_ERR_BADSCOPE: return "0x08 scope string is invalid (or missing)";
605 case LDAP_URL_ERR_BADFILTER: return "0x09 bad or missing filter";
606 case LDAP_URL_ERR_BADEXTS: return "0x0a bad or missing extensions";
607 default: return "Could not parse LDAP URL";
610 conf->url = apr_pstrdup(cmd->pool, url);
612 /* Set all the values, or at least some sane defaults */
614 char *p = apr_palloc(cmd->pool, strlen(conf->host) + strlen(urld->lud_host) + 2);
615 strcpy(p, urld->lud_host);
617 strcat(p, conf->host);
621 conf->host = urld->lud_host ? apr_pstrdup(cmd->pool, urld->lud_host) : "localhost";
624 conf->basedn = urld->lud_dn ? apr_pstrdup(cmd->pool, urld->lud_dn) : "";
626 conf->scope = urld->lud_scope == LDAP_SCOPE_ONELEVEL ? LDAP_SCOPE_ONELEVEL : LDAP_SCOPE_SUBTREE;
628 if(urld->lud_filter) {
629 if(urld->lud_filter[0] == '(') {
631 * Get rid of the surrounding parentheses;
632 * later on when generating the
633 * filter, they'll be put back.
635 conf->filter = apr_pstrdup(cmd->pool, urld->lud_filter + 1);
636 conf->filter[strlen(conf->filter) - 1] = '\0';
639 conf->filter = apr_pstrdup(cmd->pool, urld->lud_filter);
643 conf->filter = "objectClass=apacheConfig";
646 /* "ldaps" indicates secure ldap connections desired
648 if(strncasecmp(url, "ldaps", 5) == 0) {
650 conf->port = urld->lud_port ? urld->lud_port : LDAPS_PORT;
655 conf->port = urld->lud_port ? urld->lud_port : LDAP_PORT;
658 conf->have_ldap_url = 1;
659 apr_ldap_free_urldesc(urld);
662 /******************************************************************/
663 static const char *conf_mvhl(cmd_parms *cmd, void *dummy, const char *confval) {
665 if ( strlen(confval) == 0 ) { return NULL; }
668 //we're getting currently set (or default from mvhl_create_config if not set already)
669 mvhl_config *conf = (mvhl_config *) ap_get_module_config(cmd->server->module_config, &vhost_ldap_module);
670 int *cmdtype = (int *) cmd->info;
671 char *currval = apr_pstrdup(cmd->pool, confval);
674 conf->enabled = ( strcmp(currval,"On") == 0 )? 1 : 0;
677 conf->binddn = currval;
680 conf->bindpw = currval;
683 //const values: never = 0, searching = 1, finding = 2, always = 3
684 if ( strcmp(currval,"never") == 0 ) { conf->deref = never; conf->have_deref = 1;}
685 if ( strcmp(currval,"searching")== 0 ) { conf->deref = searching; conf->have_deref = 1;}
686 if ( strcmp(currval,"finding") == 0 ) { conf->deref = finding; conf->have_deref = 1;}
687 if ( strcmp(currval,"always") == 0 ) { conf->deref = always; conf->have_deref = 1;}
690 conf->wlcbasedn = currval;
693 conf->wucbasedn = currval;
696 conf->fallback = currval;
699 conf->aliasesbasedn = currval;
702 conf->alias_enabled = ( strcmp(currval,"On") == 0 )? 1 : 0 ;
705 conf->loc_auth_enabled = ( strcmp(currval,"On") == 0 )? 1 : 0;
708 conf->dir_auth_enabled = ( strcmp(currval,"On") == 0 )? 1 : 0;
715 /******************************************************************/
716 static int apply_vhost(mvhl_config *conf, const char *hostname, request_rec * r, util_ldap_connection_t *ldc, const char *dn, mvhl_request *reqc ) {
718 char filtbuf[FILTER_LENGTH];
719 apr_snprintf(filtbuf, FILTER_LENGTH, "(&(%s)(|(apacheServerName=%s)(apacheServerAlias=%s)))", conf->filter, hostname, hostname);
720 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"Server search filter: %s", filtbuf);
721 const char **vals = NULL;
722 char *attributes[] = { "apacheServerName", "apacheServerAlias", "apacheDocumentRoot", "apacheSuexecUid","apacheSuexecGid","apacheServerAdmin","apacheExtConfigHasRequireLine","apacheLocationOptionsDn","apacheAliasesConfigEnabled","apacheAliasConfigOptionsDn",0 };
724 int result = LDAP_SERVER_DOWN;
725 while(failures++ <= 5 && result == LDAP_SERVER_DOWN) {
726 result = util_ldap_cache_getuserdn(r, ldc, conf->url, conf->basedn, conf->scope, attributes, filtbuf, &dn, &vals);
730 * we don't test conf->host nor connection, because if these failed, declined was already returned
731 * instead, we do a search for specified fallback vhost
733 if ( result == LDAP_NO_SUCH_OBJECT )
735 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Vhost %s not found, will try to fall-back to defined vhost: %s", hostname, conf->fallback);
736 hostname = conf->fallback;
737 apr_snprintf(filtbuf, FILTER_LENGTH, "(&(%s)(|(apacheServerName=%s)(apacheServerAlias=%s)))", conf->filter, hostname, hostname);
738 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"Server search filter: %s", filtbuf);
739 result = util_ldap_cache_getuserdn(r, ldc, conf->url, conf->basedn, conf->scope, attributes, filtbuf, &dn, &vals);
742 if(result != LDAP_SUCCESS) {
743 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"Vhost %s defined as fallback not found, err %s", hostname, ldap_err2string(result));
747 reqc->dn = apr_pstrdup(r->pool, dn);
748 if (vals) { mvhl_dovhostconfig(r, attributes, vals, reqc); }
750 if( (reqc->name == NULL) || (reqc->docroot == NULL) || ! (r->uri[0] == '/') ) return 0;
751 r->filename = apr_pstrcat(r->pool, reqc->docroot, r->uri, NULL);
755 /******************************************************************/
756 static void apply_aliasing(mvhl_request *reqc, request_rec * r, mvhl_config *conf, util_ldap_connection_t *ldc, const char *dn) {
758 if(reqc->has_aliaslines == 1 && reqc->aliaseslines ) {
759 const char **aliasesconfvals = NULL;
760 char aliasesfilter[FILTER_LENGTH];
761 char *aliases_attributes[] = { "apacheAliasConfigSourceUri", "apacheAliasConfigServerName", "apacheAliasConfigTargetDir", "apacheAliasConfigObjectName",0 };
762 mvhl_aliasconf_object *aliasreqc = (mvhl_aliasconf_object *) apr_pcalloc(r->pool, sizeof(mvhl_aliasconf_object));
763 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"This vhost has alias configuration, need to check if for current uri");
764 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Original r->filename: %s", r->filename);
765 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Original r->uri: %s", r->uri);
769 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Entering Alias Objects search");
770 while(i <= strlen(apr_pstrdup(r->pool, r->uri)) && !aliasesconfvals ) {
772 char *aliasbuff = apr_pstrndup(r->pool, r->uri, i);
773 apr_snprintf(aliasesfilter, FILTER_LENGTH,"(&(%s)(apacheAliasConfigServerName=%s)(apacheAliasConfigSourceUri=%s))", conf->filter, reqc->name, aliasbuff);
774 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"Alias Object search filter: %s", aliasesfilter);
775 /* we reuse ldap connection opened previously with alias entries,
776 * access control entries and webusers entries searches changing used filter as needed (!!) */
777 result = util_ldap_cache_getuserdn(r, ldc, conf->url, conf->aliasesbasedn, conf->scope, aliases_attributes, aliasesfilter, &dn, &aliasesconfvals);
780 if(result != LDAP_SUCCESS) {
781 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"This vhost aliases config, but probably not for this URI, alias config entry not found");
782 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"Tried with ldap search filter: %s", aliasesfilter);
785 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"This uri has aliases config, configuration object found");
786 if(aliasesconfvals) { mvhl_doaliasesconfig(r, aliases_attributes, aliasesconfvals, aliasreqc); }
787 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "Entering alias substitution");
788 r->filename = apr_pstrcat (r->pool, aliasreqc->aliastargetdir , r->uri + i, NULL);
789 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Final filename r->filename: %s", r->filename);
790 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Final uri (unchanged) r->uri: %s", r->uri);
796 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "This vhost has no aliases, or it is disabled via apacheAliasesConfigEnabled = (FALSE|not set) skipping..");
799 /******************************************************************/
800 static void apply_location_access_control(mvhl_request *reqc, request_rec * r, mvhl_config *conf, util_ldap_connection_t *ldc, const char *dn) {
802 if(reqc->has_reqlines == 1 && reqc->rqlocationlines) {
803 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"LAC: This vhost has location access control configured, need to check if it's enabled for current uri");
806 char extconffiltbuf[FILTER_LENGTH];
807 const char **extconfvals = NULL;
808 char *extconfigattributes[] = { "apacheExtConfigUri","apacheExtConfigRequireValidUser","apacheExtConfigServerName","apacheExtConfigObjectName","apacheExtConfigUserDn","apacheExtConfigPath",0};
809 mvhl_extconfig_object *extreqc = (mvhl_extconfig_object *) apr_pcalloc(r->pool, sizeof(mvhl_extconfig_object));
811 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: Entering extConfig Location Objects search");
812 while(i <= strlen(apr_pstrdup(r->pool, r->uri)) && !extconfvals) {
814 buff = apr_pstrndup(r->pool, r->uri, i);
816 * ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"Searching for hostname %s and URI %s, origname is %s", hostname, buff, reqc->name);
817 * uncomment this, if You'd like to see in log how uri gets checked
818 * ap_log_error(APLOG_MARK,APLOG_DEBUG,OK,NULL,"%s", buff);
820 * well, we must had been connecting already, so we don't do more ldap server connection checks,
821 * and we're doing a search with cache_getuser instead of using extConfigObject dn apacheConfig object attribute value(s),
822 * because there's no convenient function in apr api.
823 * vhost location RDN attribute is used actually by some GUI to make things easier
824 * TODO: use some generic ldap functions (?) classic search or implement more ldap routines for apr
826 * so, we do a search below locationDnBase for config object with matches current hostname and uri..
827 * note, that we took our current uri, and we're searching starting from / adding one by one chararacter
828 * to match config object - access config is always the same as first matching upper url access config.
829 * and more - if someone defined accessobject for /main and /main/subdir, the first one is used.
830 * when upper is deleted - next below is returned, and so far..
831 * and more - if there are two or more extConfig object for the same combination of server/uri,
832 * then first found is returned and search isn't processed further.
834 * we do a search based on original reqc->name instead of current hostname, to apply rules even if we're accessing
835 * site via ServerAlias name
837 apr_snprintf(extconffiltbuf, FILTER_LENGTH,"(&(%s)(apacheExtConfigServerName=%s)(apacheExtConfigUri=%s))", conf->filter, reqc->name, buff);
838 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"LAC: ExtConfig Location Object search filter: %s", extconffiltbuf);
839 result = util_ldap_cache_getuserdn(r, ldc, conf->url, conf->wlcbasedn, conf->scope, extconfigattributes, extconffiltbuf, &dn, &extconfvals);
840 //matched URI, if found, is returned anyway with extconfvals as ldap attribute value.
843 if(result != LDAP_SUCCESS) {
844 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"LAC: This vhost has access control, but probably not for this URI, access config entry not found");
845 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"LAC: Tried with ldap search filter: %s", extconffiltbuf);
850 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"LAC: This uri has access control, configuration object is found");
851 //we set all into extreqc struct
852 //ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Entering extconfig buffer fill");
853 if(extconfvals) { mvhl_doextconfig(r, extconfigattributes, extconfvals, extreqc); }
855 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "LAC: Entering ap_requires generation process");
856 core_dir_config *coredirconf = (core_dir_config *) ap_get_module_config(r->per_dir_config, &core_module);
857 coredirconf->ap_auth_name = extreqc->extconfname;
858 coredirconf->ap_auth_type = (char *) "basic";
859 char *userlist = "user nobody";
862 st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
863 st->search_cache_ttl = 0;
865 char userfilter[FILTER_LENGTH];
866 /* we'll search for user object with custom filter applied, which has assigned matched location name and which has assigned current servername */
867 apr_snprintf(userfilter, FILTER_LENGTH, "(&(%s)(objectClass=apacheExtendedConfigUserObject)(apacheExtConfigUserServerName=%s)(apacheExtConfigUserLocationUri=%s))", conf->filter, reqc->name, buff);
868 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"LAC: User search filter: %s", userfilter);
870 mvhl_webuser *extuserreqc = (mvhl_webuser *) apr_pcalloc(r->pool, sizeof(mvhl_webuser));
872 if(extreqc->extusers) {
873 log_dump_apr_array(r,extreqc->extusers,"extUser");
874 char **extuserdns = (char **) extreqc->extusers->elts;
875 for (i = 0; i < extreqc->extusers->nelts; i++) {
876 const char **extuservals = NULL;
878 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: User search basedn: %s", extuserdns[i]);
879 //we don't use wucbasedn as we already know what webuser distinguishedname can be
880 char *ldap_webuser_attributes[] = { "apacheExtConfigUserName","apacheExtConfigUserServerName","apacheExtConfigUserDirectoryName","apacheExtConfigUserLocationUri","userPassword",0};
881 result = util_ldap_cache_getuserdn(r, ldc, conf->url, extuserdns[i], LDAP_SCOPE_BASE, ldap_webuser_attributes, userfilter, &dn, &extuservals);
883 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: Val 0: %s", extuservals[0]);
884 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: Val 1: %s", extuservals[1]);
885 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: Val 2: %s", extuservals[2]);
886 char *prefix = "LAC: ";
887 mvhl_doextuserconfig(r, ldap_webuser_attributes, extuservals, extuserreqc,prefix);
888 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: current username: %s", extuserreqc->webusername);
889 userlist = apr_pstrcat(r->pool, userlist, " ", extuserreqc->webusername, NULL);
890 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: current userlist: %s", userlist);
894 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: final userlist: %s ", userlist);
895 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "LAC: AuthName set to %s", coredirconf->ap_auth_name);
896 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "LAC: AuthType set to %s", coredirconf->ap_auth_type);
897 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "LAC: Preparing access control line");
898 coredirconf->ap_requires = (apr_array_header_t *) get_ap_reqs(r->pool, extreqc, reqc->name, userlist);
902 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "LAC: This vhost is not configured for access control, or it is disabled via apacheExtConfigHasRequireLine = ( FALSE|not set) skipping..");
905 /******************************************************************/
906 static void apply_directory_access_control(mvhl_request *reqc, request_rec * r, mvhl_config *conf, util_ldap_connection_t *ldc, const char *dn) {
910 char extconffiltbuf[FILTER_LENGTH];
911 const char **extconfvals = NULL;
912 char *extconfigattributes[] = { "apacheExtConfigUri","apacheExtConfigRequireValidUser","apacheExtConfigServerName","apacheExtConfigObjectName","apacheExtConfigUserDn","apacheExtConfigPath",0};
913 mvhl_extconfig_object *extreqc = (mvhl_extconfig_object *) apr_pcalloc(r->pool, sizeof(mvhl_extconfig_object));
916 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "DAC: Entering extConfig Directory Objects search");
917 while(i <= strlen(apr_pstrdup(r->pool, r->filename)) && !extconfvals) {
919 buff = apr_pstrndup(r->pool, r->filename, i);
921 * ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"Searching for hostname %s and URI %s, origname is %s", hostname, buff, reqc->name);
922 * uncomment this, if You'd like to see in log how uri gets checked
923 * ap_log_error(APLOG_MARK,APLOG_DEBUG,OK,NULL,"%s", buff);
925 * well, we must had been connecting already, so we don't do more ldap server connection checks,
926 * and we're doing a search with cache_getuser instead of using extConfigObject dn apacheConfig object attribute value(s),
927 * because there's no convenient function in apr api.
928 * vhost location RDN attribute is used actually by some GUI to make things easier
929 * TODO: use some generic ldap functions (?) classic search or implement more ldap routines for apr
931 * so, we do a search below locationDnBase for config object with matches current hostname and uri..
932 * note, that we took our current uri, and we're searching starting from / adding one by one chararacter
933 * to match config object - access config is always the same as first matching upper url access config.
934 * and more - if someone defined accessobject for /main and /main/subdir, the first one is used.
935 * when upper is deleted - next below is returned, and so far..
936 * and more - if there are two or more extConfig object for the same combination of server/uri,
937 * then first found is returned and search isn't processed further.
939 * we do a search based on original reqc->name instead of current hostname, to apply rules even if we're accessing
940 * site via ServerAlias name
942 apr_snprintf(extconffiltbuf, FILTER_LENGTH,"(&(%s)(apacheExtConfigPath=%s))", conf->filter, buff);
943 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"DAC: ExtConfig Directory Object search filter: %s", extconffiltbuf);
944 result = util_ldap_cache_getuserdn(r, ldc, conf->url, conf->wlcbasedn, conf->scope, extconfigattributes, extconffiltbuf, &dn, &extconfvals);
945 //matched dir, if found, is returned anyway with extconfvals as ldap attribute value.
948 if(result != LDAP_SUCCESS) {
949 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"DAC: For current directory access config entry is not found");
950 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"DAC: Tried with ldap search filter: %s", extconffiltbuf);
955 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"DAC: This directory has access control, configuration object is found");
956 //we set all into extreqc struct
957 //ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Entering extconfig buffer fill");
958 if(extconfvals) { mvhl_doextconfig(r, extconfigattributes, extconfvals, extreqc); }
960 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "DAC: Entering ap_requires generation process");
961 core_dir_config *coredirconf = (core_dir_config *) ap_get_module_config(r->per_dir_config, &core_module);
962 coredirconf->ap_auth_name = extreqc->extconfname;
963 coredirconf->ap_auth_type = (char *) "basic";
964 char *userlist = "user nobody";
967 st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
968 st->search_cache_ttl = 0;
970 char userfilter[FILTER_LENGTH];
971 /* we'll search for user object with custom filter applied, which has assigned matched directory name */
972 apr_snprintf(userfilter, FILTER_LENGTH, "(&(%s)(objectClass=apacheExtendedConfigUserObject)(apacheExtConfigUserDirectoryName=%s))", conf->filter, buff);
973 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r,"DAC: User search filter: %s", userfilter);
975 mvhl_webuser *extuserreqc = (mvhl_webuser *) apr_pcalloc(r->pool, sizeof(mvhl_webuser));
976 char *ldap_webuser_attributes[] = { "apacheExtConfigUserName","apacheExtConfigUserServerName","apacheExtConfigUserDirectoryName","apacheExtConfigUserLocationUri","userPassword",0};
979 if(extreqc->extusers) {
980 log_dump_apr_array(r,extreqc->extusers,"extUser");
981 char **extuserdns = (char **) extreqc->extusers->elts;
982 for (i = 0; i < extreqc->extusers->nelts; i++) {
983 const char **extuservals = NULL;
985 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "DAC: User search basedn: %s", extuserdns[i]);
986 //we don't use wucbasedn as we already know what webuser distinguishedname can be
988 result = util_ldap_cache_getuserdn(r, ldc, conf->url, extuserdns[i], LDAP_SCOPE_BASE, ldap_webuser_attributes, userfilter, &dn, &extuservals);
990 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "DAC: Val 0: %s", extuservals[0]);
991 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "DAC: Val 1: %s", extuservals[1]);
992 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "DAC: Val 2: %s", extuservals[2]);
993 char *prefix = "DAC: ";
994 mvhl_doextuserconfig(r, ldap_webuser_attributes, extuservals, extuserreqc, prefix);
995 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "DAC: current username: %s", extuserreqc->webusername);
996 userlist = apr_pstrcat(r->pool, userlist, " ", extuserreqc->webusername, NULL);
997 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "DAC: current userlist: %s", userlist);
1001 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "final userlist: %s ", userlist);
1002 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "DAC: AuthName set to %s", coredirconf->ap_auth_name);
1003 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "DAC: AuthType set to %s", coredirconf->ap_auth_type);
1004 ap_log_error(APLOG_MARK, APLOG_DEBUG, OK, NULL, "DAC: Preparing access control line");
1005 coredirconf->ap_requires = (apr_array_header_t *) get_ap_reqs(r->pool, extreqc, reqc->name, userlist);
1010 /******************************************************************/
1011 static int mvhl_translate_name(request_rec * r)
1013 request_rec *top = r->main ? r->main : r;
1014 apr_table_t *e = top->subprocess_env;
1015 apr_table_do(header_trace, r, r->headers_in, NULL);
1017 //module config and current request config objects
1018 mvhl_config *conf = (mvhl_config *) ap_get_module_config(r->server->module_config, &vhost_ldap_module);
1019 mvhl_dump_config_request(conf, r); //debug
1020 // mod_vhost_ldap is disabled or we don't have LDAP Url, we check these as soon as we can (after debugging dump)
1021 if(( conf->enabled == 0) || (! conf->have_ldap_url) || ( ! conf->host ) ) return DECLINED;
1023 //get core config and current request config, then set current request config
1024 core_server_config *core = (core_server_config *) ap_get_module_config(r->server->module_config, &core_module);
1025 mvhl_request *reqc = (mvhl_request *) apr_pcalloc(r->pool, sizeof(mvhl_request));
1026 ap_set_module_config(r->request_config, &vhost_ldap_module, reqc);
1028 const char *hostname = r->hostname;
1029 const char *dn = NULL;
1031 //we'll reuse ldap connection opened here
1032 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Opening LDAP Connection");
1033 util_ldap_connection_t *ldc = util_ldap_connection_find(r, conf->host, conf->port, conf->binddn, conf->bindpw,conf->deref, conf->secure);
1035 //heart of the system :)
1036 if ( conf->enabled == 0 || ! apply_vhost(conf, hostname, r, ldc, dn, reqc ) ) return DECLINED;
1037 if (conf->alias_enabled > 0 ) apply_aliasing(reqc, r, conf, ldc, dn);
1039 if (conf->loc_auth_enabled > 0 ) apply_location_access_control(reqc, r, conf, ldc, dn);
1040 if (conf->dir_auth_enabled > 0 ) apply_directory_access_control(reqc, r, conf, ldc, dn);
1042 ap_log_rerror(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r, "Closing LDAP Connection");
1043 util_ldap_connection_close(ldc);
1045 /* finished with Access control for location **********************************/
1046 top->server->server_hostname = apr_pstrdup(top->pool, reqc->name);
1047 if(reqc->admin) {top->server->server_admin = apr_pstrdup(top->pool, reqc->admin); }
1049 // set environment variables
1050 apr_table_addn(e, "SERVER_ROOT", reqc->docroot);
1051 core->ap_document_root = apr_pstrdup(top->pool, reqc->docroot);
1053 //apr_table_set(r->err_headers_out, "Content-type", "Content-type: text/html; charset=UTF-8");
1054 //apr_table_set(r->headers_out, "Content-type", "Content-type: text/html; charset=UTF-8");
1058 /******************************************************************/
1059 static ap_unix_identity_t *mvhl_suexec_doer(const request_rec * r)
1061 ap_unix_identity_t *ugid = NULL;
1062 mvhl_config *conf = (mvhl_config *) ap_get_module_config(r->server->module_config, &vhost_ldap_module);
1063 mvhl_request *req = (mvhl_request *) ap_get_module_config(r->request_config, &vhost_ldap_module);
1068 uid = (uid_t) atoll(req->uid);
1069 gid = (gid_t) atoll(req->gid);
1071 // mod_vhost_ldap is disabled or we don't have LDAP Url
1075 ( conf->enabled == 0 ) ||
1076 (!conf->have_ldap_url) ||
1078 (req->uid == NULL) ||
1079 (req->gid == NULL) ||
1080 (ugid = apr_palloc(r->pool, sizeof(ap_unix_identity_t))) == NULL
1088 /******************************************************************/
1089 static int mvhl_post_config(apr_pool_t * p, apr_pool_t * plog, apr_pool_t * ptemp, server_rec * s)
1091 /* make sure that mod_ldap (util_ldap) is loaded */
1092 if(ap_find_linked_module("util_ldap.c") == NULL) {return HTTP_INTERNAL_SERVER_ERROR;}
1093 ap_add_version_component(p, MOD_VHOST_LDAP_VERSION);
1096 /******************************************************************/
1097 static const command_rec mvhl_cmds[] = {
1098 AP_INIT_TAKE1("VhostLdapUrl", conf_mvhl_url, NULL, RSRC_CONF,"RFC 2255 URL of the form ldap://host[:port]/basedn[?attrib[?scope[?filter]]]."),
1099 AP_INIT_TAKE1("VhostLdapEnabled", conf_mvhl, MVHL_ENABLED, RSRC_CONF,"Set to off or unset to disable vhost_ldap module completely"),
1100 AP_INIT_TAKE1("VhostAliasesEnabled", conf_mvhl, MVHL_ALIASENABLED, RSRC_CONF,"Set to off or unset to disable ldap-based dir aliases"),
1101 AP_INIT_TAKE1("VhostLocAuthEnabled", conf_mvhl, MVHL_LAUTHENABLED, RSRC_CONF,"Set to off or unset to disable per-location authentication"),
1102 AP_INIT_TAKE1("VhostDirAuthEnabled", conf_mvhl, MVHL_DAUTHENABLED, RSRC_CONF,"Set to off or unset to disable per-location authentication"),
1103 AP_INIT_TAKE1("VhostLdapBindDn", conf_mvhl, MVHL_BINDDN, RSRC_CONF,"DN to use to bind to LDAP server"),
1104 AP_INIT_TAKE1("VhostLdapBindPw", conf_mvhl, MVHL_BINDPW, RSRC_CONF,"Password to use to bind to LDAP server"),
1105 AP_INIT_TAKE1("VhostLdapWlcBaseDn", conf_mvhl, MVHL_WLCBASEDN, RSRC_CONF,"Base DN to do all access control config searches."),
1106 AP_INIT_TAKE1("VhostLdapWucBaseDn", conf_mvhl, MVHL_WUCBASEDN, RSRC_CONF,"Base DN to do all user config searches"),
1107 AP_INIT_TAKE1("VhostLdapAliasesBaseDn", conf_mvhl, MVHL_ALIASBASEDN, RSRC_CONF,"Base DN to do all aliases config searches"),
1108 AP_INIT_TAKE1("VhostLdapFallback", conf_mvhl, MVHL_FALLBACK, RSRC_CONF,"Fallback vhost server name to use - to display not-found info"),
1109 AP_INIT_TAKE1("VhostLdapDeref", conf_mvhl, MVHL_DEREF, RSRC_CONF,"values: never, searching, finding, always"),
1112 /******************************************************************/
1113 static void mvhl_register_hooks(apr_pool_t * p)
1115 ap_hook_post_config(mvhl_post_config, NULL, NULL, APR_HOOK_MIDDLE);
1116 ap_hook_translate_name(mvhl_translate_name, NULL, NULL, APR_HOOK_MIDDLE);
1117 ap_hook_get_suexec_identity(mvhl_suexec_doer, NULL, NULL, APR_HOOK_MIDDLE);
1118 ap_hook_check_user_id(mvhl_authenticate_basic_user, NULL, NULL, APR_HOOK_MIDDLE);
1119 ap_hook_auth_checker(mvhl_check_auth, NULL, NULL, APR_HOOK_MIDDLE);
1121 /******************************************************************/
1122 module AP_MODULE_DECLARE_DATA vhost_ldap_module = {
1123 STANDARD20_MODULE_STUFF,
1124 NULL, // create per-directory config structure
1125 NULL, // merge per-directory config structures, default is to override
1126 mvhl_create_sconfig,// called when module configuration data needs to be created/allocated.
1127 mvhl_merge_sconfig, // merge per-server config structures
1128 mvhl_cmds, // Here we pass in the list of new configuration directives.
1129 mvhl_register_hooks,// register me in apache core