mod_vhost_alias.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. * mod_vhost_alias.c: support for dynamically configured mass virtual hosting
  18. *
  19. * Copyright (c) 1998-1999 Demon Internet Ltd.
  20. *
  21. * This software was submitted by Demon Internet to the Apache Software Foundation
  22. * in May 1999. Future revisions and derivatives of this source code
  23. * must acknowledge Demon Internet as the original contributor of
  24. * this module. All other licensing and usage conditions are those
  25. * of the Apache Software Foundation.
  26. *
  27. * Originally written by Tony Finch <fanf@demon.net> <dot@dotat.at>.
  28. *
  29. * Implementation ideas were taken from mod_alias.c. The overall
  30. * concept is derived from the OVERRIDE_DOC_ROOT/OVERRIDE_CGIDIR
  31. * patch to Apache 1.3b3 and a similar feature in Demon's thttpd,
  32. * both written by James Grinter <jrg@blodwen.demon.co.uk>.
  33. */
  34. #include "apr.h"
  35. #include "apr_strings.h"
  36. #include "apr_hooks.h"
  37. #include "apr_lib.h"
  38. #define APR_WANT_STRFUNC
  39. #include "apr_want.h"
  40. #include "httpd.h"
  41. #include "http_config.h"
  42. #include "http_core.h"
  43. #include "http_request.h" /* for ap_hook_translate_name */
  44. module AP_MODULE_DECLARE_DATA vhost_alias_module;
  45. /*
  46. * basic configuration things
  47. * we abbreviate "mod_vhost_alias" to "mva" for shorter names
  48. */
  49. typedef enum {
  50. VHOST_ALIAS_UNSET, VHOST_ALIAS_NONE, VHOST_ALIAS_NAME, VHOST_ALIAS_IP
  51. } mva_mode_e;
  52. /*
  53. * Per-server module config record.
  54. */
  55. typedef struct mva_sconf_t {
  56. const char *doc_root;
  57. const char *cgi_root;
  58. mva_mode_e doc_root_mode;
  59. mva_mode_e cgi_root_mode;
  60. } mva_sconf_t;
  61. static void *mva_create_server_config(apr_pool_t *p, server_rec *s)
  62. {
  63. mva_sconf_t *conf;
  64. conf = (mva_sconf_t *) apr_pcalloc(p, sizeof(mva_sconf_t));
  65. conf->doc_root = NULL;
  66. conf->cgi_root = NULL;
  67. conf->doc_root_mode = VHOST_ALIAS_UNSET;
  68. conf->cgi_root_mode = VHOST_ALIAS_UNSET;
  69. return conf;
  70. }
  71. static void *mva_merge_server_config(apr_pool_t *p, void *parentv, void *childv)
  72. {
  73. mva_sconf_t *parent = (mva_sconf_t *) parentv;
  74. mva_sconf_t *child = (mva_sconf_t *) childv;
  75. mva_sconf_t *conf;
  76. conf = (mva_sconf_t *) apr_pcalloc(p, sizeof(*conf));
  77. if (child->doc_root_mode == VHOST_ALIAS_UNSET) {
  78. conf->doc_root_mode = parent->doc_root_mode;
  79. conf->doc_root = parent->doc_root;
  80. }
  81. else {
  82. conf->doc_root_mode = child->doc_root_mode;
  83. conf->doc_root = child->doc_root;
  84. }
  85. if (child->cgi_root_mode == VHOST_ALIAS_UNSET) {
  86. conf->cgi_root_mode = parent->cgi_root_mode;
  87. conf->cgi_root = parent->cgi_root;
  88. }
  89. else {
  90. conf->cgi_root_mode = child->cgi_root_mode;
  91. conf->cgi_root = child->cgi_root;
  92. }
  93. return conf;
  94. }
  95. /*
  96. * These are just here to tell us what vhost_alias_set should do.
  97. * We don't put anything into them; we just use the cell addresses.
  98. */
  99. static int vhost_alias_set_doc_root_ip,
  100. vhost_alias_set_cgi_root_ip,
  101. vhost_alias_set_doc_root_name,
  102. vhost_alias_set_cgi_root_name;
  103. static const char *vhost_alias_set(cmd_parms *cmd, void *dummy, const char *map)
  104. {
  105. mva_sconf_t *conf;
  106. mva_mode_e mode, *pmode;
  107. const char **pmap;
  108. const char *p;
  109. conf = (mva_sconf_t *) ap_get_module_config(cmd->server->module_config,
  110. &vhost_alias_module);
  111. /* there ought to be a better way of doing this */
  112. if (&vhost_alias_set_doc_root_ip == cmd->info) {
  113. mode = VHOST_ALIAS_IP;
  114. pmap = &conf->doc_root;
  115. pmode = &conf->doc_root_mode;
  116. }
  117. else if (&vhost_alias_set_cgi_root_ip == cmd->info) {
  118. mode = VHOST_ALIAS_IP;
  119. pmap = &conf->cgi_root;
  120. pmode = &conf->cgi_root_mode;
  121. }
  122. else if (&vhost_alias_set_doc_root_name == cmd->info) {
  123. mode = VHOST_ALIAS_NAME;
  124. pmap = &conf->doc_root;
  125. pmode = &conf->doc_root_mode;
  126. }
  127. else if (&vhost_alias_set_cgi_root_name == cmd->info) {
  128. mode = VHOST_ALIAS_NAME;
  129. pmap = &conf->cgi_root;
  130. pmode = &conf->cgi_root_mode;
  131. }
  132. else {
  133. return "INTERNAL ERROR: unknown command info";
  134. }
  135. if (!ap_os_is_path_absolute(cmd->pool, map)) {
  136. if (strcasecmp(map, "none")) {
  137. return "format string must be an absolute path, or 'none'";
  138. }
  139. *pmap = NULL;
  140. *pmode = VHOST_ALIAS_NONE;
  141. return NULL;
  142. }
  143. /* sanity check */
  144. p = map;
  145. while (*p != '\0') {
  146. if (*p++ != '%') {
  147. continue;
  148. }
  149. /* we just found a '%' */
  150. if (*p == 'p' || *p == '%') {
  151. ++p;
  152. continue;
  153. }
  154. /* optional dash */
  155. if (*p == '-') {
  156. ++p;
  157. }
  158. /* digit N */
  159. if (apr_isdigit(*p)) {
  160. ++p;
  161. }
  162. else {
  163. return "syntax error in format string";
  164. }
  165. /* optional plus */
  166. if (*p == '+') {
  167. ++p;
  168. }
  169. /* do we end here? */
  170. if (*p != '.') {
  171. continue;
  172. }
  173. ++p;
  174. /* optional dash */
  175. if (*p == '-') {
  176. ++p;
  177. }
  178. /* digit M */
  179. if (apr_isdigit(*p)) {
  180. ++p;
  181. }
  182. else {
  183. return "syntax error in format string";
  184. }
  185. /* optional plus */
  186. if (*p == '+') {
  187. ++p;
  188. }
  189. }
  190. *pmap = map;
  191. *pmode = mode;
  192. return NULL;
  193. }
  194. static const command_rec mva_commands[] =
  195. {
  196. AP_INIT_TAKE1("VirtualScriptAlias", vhost_alias_set,
  197. &vhost_alias_set_cgi_root_name, RSRC_CONF,
  198. "how to create a ScriptAlias based on the host"),
  199. AP_INIT_TAKE1("VirtualDocumentRoot", vhost_alias_set,
  200. &vhost_alias_set_doc_root_name, RSRC_CONF,
  201. "how to create the DocumentRoot based on the host"),
  202. AP_INIT_TAKE1("VirtualScriptAliasIP", vhost_alias_set,
  203. &vhost_alias_set_cgi_root_ip, RSRC_CONF,
  204. "how to create a ScriptAlias based on the host"),
  205. AP_INIT_TAKE1("VirtualDocumentRootIP", vhost_alias_set,
  206. &vhost_alias_set_doc_root_ip, RSRC_CONF,
  207. "how to create the DocumentRoot based on the host"),
  208. { NULL }
  209. };
  210. /*
  211. * This really wants to be a nested function
  212. * but C is too feeble to support them.
  213. */
  214. static APR_INLINE void vhost_alias_checkspace(request_rec *r, char *buf,
  215. char **pdest, int size)
  216. {
  217. /* XXX: what if size > HUGE_STRING_LEN? */
  218. if (*pdest + size > buf + HUGE_STRING_LEN) {
  219. **pdest = '\0';
  220. if (r->filename) {
  221. r->filename = apr_pstrcat(r->pool, r->filename, buf, NULL);
  222. }
  223. else {
  224. r->filename = apr_pstrdup(r->pool, buf);
  225. }
  226. *pdest = buf;
  227. }
  228. }
  229. static void vhost_alias_interpolate(request_rec *r, const char *name,
  230. const char *map, const char *uri)
  231. {
  232. /* 0..9 9..0 */
  233. enum { MAXDOTS = 19 };
  234. const char *dots[MAXDOTS+1];
  235. int ndots;
  236. char buf[HUGE_STRING_LEN];
  237. char *dest, last;
  238. int N, M, Np, Mp, Nd, Md;
  239. const char *start, *end;
  240. const char *p;
  241. ndots = 0;
  242. dots[ndots++] = name-1; /* slightly naughty */
  243. for (p = name; *p; ++p){
  244. if (*p == '.' && ndots < MAXDOTS) {
  245. dots[ndots++] = p;
  246. }
  247. }
  248. dots[ndots] = p;
  249. r->filename = NULL;
  250. dest = buf;
  251. last = '\0';
  252. while (*map) {
  253. if (*map != '%') {
  254. /* normal characters */
  255. vhost_alias_checkspace(r, buf, &dest, 1);
  256. last = *dest++ = *map++;
  257. continue;
  258. }
  259. /* we are in a format specifier */
  260. ++map;
  261. /* can't be a slash */
  262. last = '\0';
  263. /* %% -> % */
  264. if (*map == '%') {
  265. ++map;
  266. vhost_alias_checkspace(r, buf, &dest, 1);
  267. *dest++ = '%';
  268. continue;
  269. }
  270. /* port number */
  271. if (*map == 'p') {
  272. ++map;
  273. /* no. of decimal digits in a short plus one */
  274. vhost_alias_checkspace(r, buf, &dest, 7);
  275. dest += apr_snprintf(dest, 7, "%d", ap_get_server_port(r));
  276. continue;
  277. }
  278. /* deal with %-N+.-M+ -- syntax is already checked */
  279. N = M = 0; /* value */
  280. Np = Mp = 0; /* is there a plus? */
  281. Nd = Md = 0; /* is there a dash? */
  282. if (*map == '-') ++map, Nd = 1;
  283. N = *map++ - '0';
  284. if (*map == '+') ++map, Np = 1;
  285. if (*map == '.') {
  286. ++map;
  287. if (*map == '-') {
  288. ++map, Md = 1;
  289. }
  290. M = *map++ - '0';
  291. if (*map == '+') {
  292. ++map, Mp = 1;
  293. }
  294. }
  295. /* note that N and M are one-based indices, not zero-based */
  296. start = dots[0]+1; /* ptr to the first character */
  297. end = dots[ndots]; /* ptr to the character after the last one */
  298. if (N != 0) {
  299. if (N > ndots) {
  300. start = "_";
  301. end = start+1;
  302. }
  303. else if (!Nd) {
  304. start = dots[N-1]+1;
  305. if (!Np) {
  306. end = dots[N];
  307. }
  308. }
  309. else {
  310. if (!Np) {
  311. start = dots[ndots-N]+1;
  312. }
  313. end = dots[ndots-N+1];
  314. }
  315. }
  316. if (M != 0) {
  317. if (M > end - start) {
  318. start = "_";
  319. end = start+1;
  320. }
  321. else if (!Md) {
  322. start = start+M-1;
  323. if (!Mp) {
  324. end = start+1;
  325. }
  326. }
  327. else {
  328. if (!Mp) {
  329. start = end-M;
  330. }
  331. end = end-M+1;
  332. }
  333. }
  334. vhost_alias_checkspace(r, buf, &dest, end - start);
  335. for (p = start; p < end; ++p) {
  336. *dest++ = apr_tolower(*p);
  337. }
  338. }
  339. *dest = '\0';
  340. /* no double slashes */
  341. if (last == '/') {
  342. ++uri;
  343. }
  344. if (r->filename) {
  345. r->filename = apr_pstrcat(r->pool, r->filename, buf, uri, NULL);
  346. }
  347. else {
  348. r->filename = apr_pstrcat(r->pool, buf, uri, NULL);
  349. }
  350. }
  351. static int mva_translate(request_rec *r)
  352. {
  353. mva_sconf_t *conf;
  354. const char *name, *map, *uri;
  355. mva_mode_e mode;
  356. const char *cgi;
  357. conf = (mva_sconf_t *) ap_get_module_config(r->server->module_config,
  358. &vhost_alias_module);
  359. cgi = NULL;
  360. if (conf->cgi_root) {
  361. cgi = strstr(r->uri, "cgi-bin/");
  362. if (cgi && (cgi != r->uri + strspn(r->uri, "/"))) {
  363. cgi = NULL;
  364. }
  365. }
  366. if (cgi) {
  367. mode = conf->cgi_root_mode;
  368. map = conf->cgi_root;
  369. uri = cgi + strlen("cgi-bin");
  370. }
  371. else if (r->uri[0] == '/') {
  372. mode = conf->doc_root_mode;
  373. map = conf->doc_root;
  374. uri = r->uri;
  375. }
  376. else {
  377. return DECLINED;
  378. }
  379. if (mode == VHOST_ALIAS_NAME) {
  380. name = ap_get_server_name(r);
  381. }
  382. else if (mode == VHOST_ALIAS_IP) {
  383. name = r->connection->local_ip;
  384. }
  385. else {
  386. return DECLINED;
  387. }
  388. /* ### There is an optimization available here to determine the
  389. * absolute portion of the path from the server config phase,
  390. * through the first % segment, and note that portion of the path
  391. * canonical_path buffer.
  392. */
  393. r->canonical_filename = "";
  394. vhost_alias_interpolate(r, name, map, uri);
  395. if (cgi) {
  396. /* see is_scriptaliased() in mod_cgi */
  397. r->handler = "cgi-script";
  398. apr_table_setn(r->notes, "alias-forced-type", r->handler);
  399. }
  400. return OK;
  401. }
  402. static void register_hooks(apr_pool_t *p)
  403. {
  404. static const char * const aszPre[]={ "mod_alias.c","mod_userdir.c",NULL };
  405. ap_hook_translate_name(mva_translate, aszPre, NULL, APR_HOOK_MIDDLE);
  406. }
  407. module AP_MODULE_DECLARE_DATA vhost_alias_module =
  408. {
  409. STANDARD20_MODULE_STUFF,
  410. NULL, /* dir config creater */
  411. NULL, /* dir merger --- default is to override */
  412. mva_create_server_config, /* server config */
  413. mva_merge_server_config, /* merge server configs */
  414. mva_commands, /* command apr_table_t */
  415. register_hooks /* register hooks */
  416. };