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_alias.c: Stuff for dealing with directory aliases 19 * 20 * Original by Rob McCool, rewritten in succession by David Robinson 21 * and rst. 22 * 23 */ 24 25#include "apr_strings.h" 26#include "apr_lib.h" 27 28#define APR_WANT_STRFUNC 29#include "apr_want.h" 30 31#include "ap_config.h" 32#include "httpd.h" 33#include "http_core.h" 34#include "http_config.h" 35#include "http_request.h" 36#include "http_log.h" 37 38 39typedef struct { 40 const char *real; 41 const char *fake; 42 char *handler; 43 ap_regex_t *regexp; 44 int redir_status; /* 301, 302, 303, 410, etc */ 45} alias_entry; 46 47typedef struct { 48 apr_array_header_t *aliases; 49 apr_array_header_t *redirects; 50} alias_server_conf; 51 52typedef struct { 53 apr_array_header_t *redirects; 54} alias_dir_conf; 55 56module AP_MODULE_DECLARE_DATA alias_module; 57 58static void *create_alias_config(apr_pool_t *p, server_rec *s) 59{ 60 alias_server_conf *a = 61 (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf)); 62 63 a->aliases = apr_array_make(p, 20, sizeof(alias_entry)); 64 a->redirects = apr_array_make(p, 20, sizeof(alias_entry)); 65 return a; 66} 67 68static void *create_alias_dir_config(apr_pool_t *p, char *d) 69{ 70 alias_dir_conf *a = 71 (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf)); 72 a->redirects = apr_array_make(p, 2, sizeof(alias_entry)); 73 return a; 74} 75 76static void *merge_alias_config(apr_pool_t *p, void *basev, void *overridesv) 77{ 78 alias_server_conf *a = 79 (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf)); 80 alias_server_conf *base = (alias_server_conf *) basev; 81 alias_server_conf *overrides = (alias_server_conf *) overridesv; 82 83 a->aliases = apr_array_append(p, overrides->aliases, base->aliases); 84 a->redirects = apr_array_append(p, overrides->redirects, base->redirects); 85 return a; 86} 87 88static void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv) 89{ 90 alias_dir_conf *a = 91 (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf)); 92 alias_dir_conf *base = (alias_dir_conf *) basev; 93 alias_dir_conf *overrides = (alias_dir_conf *) overridesv; 94 a->redirects = apr_array_append(p, overrides->redirects, base->redirects); 95 return a; 96} 97 98/* need prototype for overlap check */ 99static int alias_matches(const char *uri, const char *alias_fakename); 100 101static const char *add_alias_internal(cmd_parms *cmd, void *dummy, 102 const char *fake, const char *real, 103 int use_regex) 104{ 105 server_rec *s = cmd->server; 106 alias_server_conf *conf = ap_get_module_config(s->module_config, 107 &alias_module); 108 alias_entry *new = apr_array_push(conf->aliases); 109 alias_entry *entries = (alias_entry *)conf->aliases->elts; 110 int i; 111 112 /* XXX: real can NOT be relative to DocumentRoot here... compat bug. */ 113 114 if (use_regex) { 115 new->regexp = ap_pregcomp(cmd->pool, fake, AP_REG_EXTENDED); 116 if (new->regexp == NULL) 117 return "Regular expression could not be compiled."; 118 new->real = real; 119 } 120 else { 121 /* XXX This may be optimized, but we must know that new->real 122 * exists. If so, we can dir merge later, trusing new->real 123 * and just canonicalizing the remainder. Not till I finish 124 * cleaning out the old ap_canonical stuff first. 125 */ 126 new->real = real; 127 } 128 new->fake = fake; 129 new->handler = cmd->info; 130 131 /* check for overlapping (Script)Alias directives 132 * and throw a warning if found one 133 */ 134 if (!use_regex) { 135 for (i = 0; i < conf->aliases->nelts - 1; ++i) { 136 alias_entry *alias = &entries[i]; 137 138 if ( (!alias->regexp && alias_matches(fake, alias->fake) > 0) 139 || (alias->regexp && !ap_regexec(alias->regexp, fake, 0, NULL, 0))) { 140 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00671) 141 "The %s directive in %s at line %d will probably " 142 "never match because it overlaps an earlier " 143 "%sAlias%s.", 144 cmd->cmd->name, cmd->directive->filename, 145 cmd->directive->line_num, 146 alias->handler ? "Script" : "", 147 alias->regexp ? "Match" : ""); 148 149 break; /* one warning per alias should be sufficient */ 150 } 151 } 152 } 153 154 return NULL; 155} 156 157static const char *add_alias(cmd_parms *cmd, void *dummy, const char *fake, 158 const char *real) 159{ 160 return add_alias_internal(cmd, dummy, fake, real, 0); 161} 162 163static const char *add_alias_regex(cmd_parms *cmd, void *dummy, 164 const char *fake, const char *real) 165{ 166 return add_alias_internal(cmd, dummy, fake, real, 1); 167} 168 169static const char *add_redirect_internal(cmd_parms *cmd, 170 alias_dir_conf *dirconf, 171 const char *arg1, const char *arg2, 172 const char *arg3, int use_regex) 173{ 174 alias_entry *new; 175 server_rec *s = cmd->server; 176 alias_server_conf *serverconf = ap_get_module_config(s->module_config, 177 &alias_module); 178 int status = (int) (long) cmd->info; 179 int grokarg1 = 1; 180 ap_regex_t *regex = NULL; 181 const char *fake = arg2; 182 const char *url = arg3; 183 184 /* 185 * Logic flow: 186 * Go ahead and try to grok the 1st arg, in case it is a 187 * Redirect status. Now if we have 3 args, we expect that 188 * we were able to understand that 1st argument (it's something 189 * we expected, so if not, then we bail 190 */ 191 if (!strcasecmp(arg1, "permanent")) 192 status = HTTP_MOVED_PERMANENTLY; 193 else if (!strcasecmp(arg1, "temp")) 194 status = HTTP_MOVED_TEMPORARILY; 195 else if (!strcasecmp(arg1, "seeother")) 196 status = HTTP_SEE_OTHER; 197 else if (!strcasecmp(arg1, "gone")) 198 status = HTTP_GONE; 199 else if (apr_isdigit(*arg1)) 200 status = atoi(arg1); 201 else 202 grokarg1 = 0; 203 204 if (arg3 && !grokarg1) 205 return "Redirect: invalid first argument (of three)"; 206 207 /* 208 * if we don't have the 3rd arg and we didn't understand the 1st 209 * one, then assume URL-path URL. This also handles case, eg, GONE 210 * we even though we don't have a 3rd arg, we did understand the 1st 211 * one, so we don't want to re-arrange 212 */ 213 if (!arg3 && !grokarg1) { 214 fake = arg1; 215 url = arg2; 216 } 217 218 if (use_regex) { 219 regex = ap_pregcomp(cmd->pool, fake, AP_REG_EXTENDED); 220 if (regex == NULL) 221 return "Regular expression could not be compiled."; 222 } 223 224 if (ap_is_HTTP_REDIRECT(status)) { 225 if (!url) 226 return "URL to redirect to is missing"; 227 /* PR#35314: we can allow path components here; 228 * they get correctly resolved to full URLs. 229 */ 230 if (!use_regex && !ap_is_url(url) && (url[0] != '/')) 231 return "Redirect to non-URL"; 232 } 233 else { 234 if (url) 235 return "Redirect URL not valid for this status"; 236 } 237 238 if (cmd->path) 239 new = apr_array_push(dirconf->redirects); 240 else 241 new = apr_array_push(serverconf->redirects); 242 243 new->fake = fake; 244 new->real = url; 245 new->regexp = regex; 246 new->redir_status = status; 247 return NULL; 248} 249 250static const char *add_redirect(cmd_parms *cmd, void *dirconf, 251 const char *arg1, const char *arg2, 252 const char *arg3) 253{ 254 return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0); 255} 256 257static const char *add_redirect2(cmd_parms *cmd, void *dirconf, 258 const char *arg1, const char *arg2) 259{ 260 return add_redirect_internal(cmd, dirconf, arg1, arg2, NULL, 0); 261} 262 263static const char *add_redirect_regex(cmd_parms *cmd, void *dirconf, 264 const char *arg1, const char *arg2, 265 const char *arg3) 266{ 267 return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1); 268} 269 270static const command_rec alias_cmds[] = 271{ 272 AP_INIT_TAKE2("Alias", add_alias, NULL, RSRC_CONF, 273 "a fakename and a realname"), 274 AP_INIT_TAKE2("ScriptAlias", add_alias, "cgi-script", RSRC_CONF, 275 "a fakename and a realname"), 276 AP_INIT_TAKE23("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY, 277 OR_FILEINFO, 278 "an optional status, then document to be redirected and " 279 "destination URL"), 280 AP_INIT_TAKE2("AliasMatch", add_alias_regex, NULL, RSRC_CONF, 281 "a regular expression and a filename"), 282 AP_INIT_TAKE2("ScriptAliasMatch", add_alias_regex, "cgi-script", RSRC_CONF, 283 "a regular expression and a filename"), 284 AP_INIT_TAKE23("RedirectMatch", add_redirect_regex, 285 (void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO, 286 "an optional status, then a regular expression and " 287 "destination URL"), 288 AP_INIT_TAKE2("RedirectTemp", add_redirect2, 289 (void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO, 290 "a document to be redirected, then the destination URL"), 291 AP_INIT_TAKE2("RedirectPermanent", add_redirect2, 292 (void *) HTTP_MOVED_PERMANENTLY, OR_FILEINFO, 293 "a document to be redirected, then the destination URL"), 294 {NULL} 295}; 296 297static int alias_matches(const char *uri, const char *alias_fakename) 298{ 299 const char *aliasp = alias_fakename, *urip = uri; 300 301 while (*aliasp) { 302 if (*aliasp == '/') { 303 /* any number of '/' in the alias matches any number in 304 * the supplied URI, but there must be at least one... 305 */ 306 if (*urip != '/') 307 return 0; 308 309 do { 310 ++aliasp; 311 } while (*aliasp == '/'); 312 do { 313 ++urip; 314 } while (*urip == '/'); 315 } 316 else { 317 /* Other characters are compared literally */ 318 if (*urip++ != *aliasp++) 319 return 0; 320 } 321 } 322 323 /* Check last alias path component matched all the way */ 324 325 if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/') 326 return 0; 327 328 /* Return number of characters from URI which matched (may be 329 * greater than length of alias, since we may have matched 330 * doubled slashes) 331 */ 332 333 return urip - uri; 334} 335 336static char magic_error_value; 337#define PREGSUB_ERROR (&magic_error_value) 338 339static char *try_alias_list(request_rec *r, apr_array_header_t *aliases, 340 int is_redir, int *status) 341{ 342 alias_entry *entries = (alias_entry *) aliases->elts; 343 ap_regmatch_t regm[AP_MAX_REG_MATCH]; 344 char *found = NULL; 345 int i; 346 347 for (i = 0; i < aliases->nelts; ++i) { 348 alias_entry *alias = &entries[i]; 349 int l; 350 351 if (alias->regexp) { 352 if (!ap_regexec(alias->regexp, r->uri, AP_MAX_REG_MATCH, regm, 0)) { 353 if (alias->real) { 354 found = ap_pregsub(r->pool, alias->real, r->uri, 355 AP_MAX_REG_MATCH, regm); 356 if (found) { 357 if (is_redir) { 358 apr_uri_t uri; 359 apr_uri_parse(r->pool, found, &uri); 360 /* Do not escape the query string or fragment. */ 361 found = apr_uri_unparse(r->pool, &uri, 362 APR_URI_UNP_OMITQUERY); 363 found = ap_escape_uri(r->pool, found); 364 if (uri.query) { 365 found = apr_pstrcat(r->pool, found, "?", 366 uri.query, NULL); 367 } 368 if (uri.fragment) { 369 found = apr_pstrcat(r->pool, found, "#", 370 uri.fragment, NULL); 371 } 372 } 373 else { 374 int pathlen = strlen(found) - 375 (strlen(r->uri + regm[0].rm_eo)); 376 AP_DEBUG_ASSERT(pathlen >= 0); 377 AP_DEBUG_ASSERT(pathlen <= strlen(found)); 378 ap_set_context_info(r, 379 apr_pstrmemdup(r->pool, r->uri, 380 regm[0].rm_eo), 381 apr_pstrmemdup(r->pool, found, 382 pathlen)); 383 } 384 } 385 else { 386 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00672) 387 "Regex substitution in '%s' failed. " 388 "Replacement too long?", alias->real); 389 return PREGSUB_ERROR; 390 } 391 } 392 else { 393 /* need something non-null */ 394 found = ""; 395 } 396 } 397 } 398 else { 399 l = alias_matches(r->uri, alias->fake); 400 401 if (l > 0) { 402 ap_set_context_info(r, alias->fake, alias->real); 403 if (is_redir) { 404 char *escurl; 405 escurl = ap_os_escape_path(r->pool, r->uri + l, 1); 406 407 found = apr_pstrcat(r->pool, alias->real, escurl, NULL); 408 } 409 else 410 found = apr_pstrcat(r->pool, alias->real, r->uri + l, NULL); 411 } 412 } 413 414 if (found) { 415 if (alias->handler) { /* Set handler, and leave a note for mod_cgi */ 416 r->handler = alias->handler; 417 apr_table_setn(r->notes, "alias-forced-type", r->handler); 418 } 419 /* XXX This is as SLOW as can be, next step, we optimize 420 * and merge to whatever part of the found path was already 421 * canonicalized. After I finish eliminating os canonical. 422 * Better fail test for ap_server_root_relative needed here. 423 */ 424 if (!is_redir) { 425 found = ap_server_root_relative(r->pool, found); 426 } 427 if (found) { 428 *status = alias->redir_status; 429 } 430 return found; 431 } 432 433 } 434 435 return NULL; 436} 437 438static int translate_alias_redir(request_rec *r) 439{ 440 ap_conf_vector_t *sconf = r->server->module_config; 441 alias_server_conf *serverconf = ap_get_module_config(sconf, &alias_module); 442 char *ret; 443 int status; 444 445 if (r->uri[0] != '/' && r->uri[0] != '\0') { 446 return DECLINED; 447 } 448 449 if ((ret = try_alias_list(r, serverconf->redirects, 1, &status)) != NULL) { 450 if (ret == PREGSUB_ERROR) 451 return HTTP_INTERNAL_SERVER_ERROR; 452 if (ap_is_HTTP_REDIRECT(status)) { 453 if (ret[0] == '/') { 454 char *orig_target = ret; 455 456 ret = ap_construct_url(r->pool, ret, r); 457 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00673) 458 "incomplete redirection target of '%s' for " 459 "URI '%s' modified to '%s'", 460 orig_target, r->uri, ret); 461 } 462 if (!ap_is_url(ret)) { 463 status = HTTP_INTERNAL_SERVER_ERROR; 464 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00674) 465 "cannot redirect '%s' to '%s'; " 466 "target is not a valid absoluteURI or abs_path", 467 r->uri, ret); 468 } 469 else { 470 /* append requested query only, if the config didn't 471 * supply its own. 472 */ 473 if (r->args && !ap_strchr(ret, '?')) { 474 ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL); 475 } 476 apr_table_setn(r->headers_out, "Location", ret); 477 } 478 } 479 return status; 480 } 481 482 if ((ret = try_alias_list(r, serverconf->aliases, 0, &status)) != NULL) { 483 r->filename = ret; 484 return OK; 485 } 486 487 return DECLINED; 488} 489 490static int fixup_redir(request_rec *r) 491{ 492 void *dconf = r->per_dir_config; 493 alias_dir_conf *dirconf = 494 (alias_dir_conf *) ap_get_module_config(dconf, &alias_module); 495 char *ret; 496 int status; 497 498 /* It may have changed since last time, so try again */ 499 500 if ((ret = try_alias_list(r, dirconf->redirects, 1, &status)) != NULL) { 501 if (ret == PREGSUB_ERROR) 502 return HTTP_INTERNAL_SERVER_ERROR; 503 if (ap_is_HTTP_REDIRECT(status)) { 504 if (ret[0] == '/') { 505 char *orig_target = ret; 506 507 ret = ap_construct_url(r->pool, ret, r); 508 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00675) 509 "incomplete redirection target of '%s' for " 510 "URI '%s' modified to '%s'", 511 orig_target, r->uri, ret); 512 } 513 if (!ap_is_url(ret)) { 514 status = HTTP_INTERNAL_SERVER_ERROR; 515 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00676) 516 "cannot redirect '%s' to '%s'; " 517 "target is not a valid absoluteURI or abs_path", 518 r->uri, ret); 519 } 520 else { 521 /* append requested query only, if the config didn't 522 * supply its own. 523 */ 524 if (r->args && !ap_strchr(ret, '?')) { 525 ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL); 526 } 527 apr_table_setn(r->headers_out, "Location", ret); 528 } 529 } 530 return status; 531 } 532 533 return DECLINED; 534} 535 536static void register_hooks(apr_pool_t *p) 537{ 538 static const char * const aszSucc[]={ "mod_userdir.c", 539 "mod_vhost_alias.c",NULL }; 540 541 ap_hook_translate_name(translate_alias_redir,NULL,aszSucc,APR_HOOK_MIDDLE); 542 ap_hook_fixups(fixup_redir,NULL,NULL,APR_HOOK_MIDDLE); 543} 544 545AP_DECLARE_MODULE(alias) = 546{ 547 STANDARD20_MODULE_STUFF, 548 create_alias_dir_config, /* dir config creater */ 549 merge_alias_dir_config, /* dir merger --- default is to override */ 550 create_alias_config, /* server config */ 551 merge_alias_config, /* merge server configs */ 552 alias_cmds, /* command apr_table_t */ 553 register_hooks /* register hooks */ 554}; 555