~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to modules/mappers/mod_vhost_alias.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

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