upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / metadata / mod_setenvif.c
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_setenvif.c
19  * Set environment variables based on matching request headers or
20  * attributes against regex strings
21  * 
22  * Paul Sutton <paul@ukweb.com> 27 Oct 1996
23  * Based on mod_browser by Alexei Kosut <akosut@organic.com>
24  */
25
26 /*
27  * Used to set environment variables based on the incoming request headers,
28  * or some selected other attributes of the request (e.g., the remote host
29  * name).
30  *
31  * Usage:
32  *
33  *   SetEnvIf name regex var ...
34  *
35  * where name is either a HTTP request header name, or one of the
36  * special values (see below). 'name' may be a regex when it is used
37  * to specify an HTTP request header name. The 'value' of the header 
38  & (or the value of the special value from below) are compared against
39  * the regex argument. If this is a simple string, a simple sub-string
40  * match is performed. Otherwise, a request expression match is
41  * done. If the value matches the string or regular expression, the
42  * environment variables listed as var ... are set. Each var can 
43  * be in one of three formats: var, which sets the named variable
44  * (the value value "1"); var=value, which sets the variable to
45  * the given value; or !var, which unsets the variable is it has
46  * been previously set.
47  *
48  * Normally the strings are compared with regard to case. To ignore
49  * case, use the directive SetEnvIfNoCase instead.
50  *
51  * Special values for 'name' are:
52  *
53  *   server_addr        IP address of interface on which request arrived
54  *                      (analogous to SERVER_ADDR set in ap_add_common_vars())
55  *   remote_host        Remote host name (if available)
56  *   remote_addr        Remote IP address
57  *   request_method     Request method (GET, POST, etc)
58  *   request_uri        Requested URI
59  *
60  * Examples:
61  *
62  * To set the enviroment variable LOCALHOST if the client is the local
63  * machine:
64  *
65  *    SetEnvIf remote_addr 127.0.0.1 LOCALHOST
66  *
67  * To set LOCAL if the client is the local host, or within our company's
68  * domain (192.168.10):
69  *
70  *    SetEnvIf remote_addr 192.168.10. LOCAL
71  *    SetEnvIf remote_addr 127.0.0.1   LOCALHOST
72  *
73  * This could be written as:
74  *
75  *    SetEnvIf remote_addr (127.0.0.1|192.168.10.) LOCAL
76  *
77  * To set HAVE_TS if the client request contains any header beginning
78  * with "TS" with a value beginning with a lower case alphabet:
79  *
80  *    SetEnvIf ^TS* ^[a-z].* HAVE_TS
81  */
82
83 #include "apr.h"
84 #include "apr_strings.h"
85 #include "apr_strmatch.h"
86
87 #define APR_WANT_STRFUNC
88 #include "apr_want.h"
89
90 #include "ap_config.h"
91 #include "httpd.h"
92 #include "http_config.h"
93 #include "http_core.h"
94 #include "http_log.h"
95 #include "http_protocol.h"
96
97
98 enum special {
99     SPECIAL_NOT,
100     SPECIAL_REMOTE_ADDR,
101     SPECIAL_REMOTE_HOST,
102     SPECIAL_REQUEST_URI,
103     SPECIAL_REQUEST_METHOD,
104     SPECIAL_REQUEST_PROTOCOL,
105     SPECIAL_SERVER_ADDR
106 };
107 typedef struct {
108     char *name;                 /* header name */
109     regex_t *pnamereg;          /* compiled header name regex */
110     char *regex;                /* regex to match against */
111     regex_t *preg;              /* compiled regex */
112     const apr_strmatch_pattern *pattern; /* non-regex pattern to match */
113     apr_table_t *features;      /* env vars to set (or unset) */
114     enum special special_type;  /* is it a "special" header ? */
115     int icase;                  /* ignoring case? */
116 } sei_entry;
117
118 typedef struct {
119     apr_array_header_t *conditionals;
120 } sei_cfg_rec;
121
122 module AP_MODULE_DECLARE_DATA setenvif_module;
123
124 /*
125  * These routines, the create- and merge-config functions, are called
126  * for both the server-wide and the per-directory contexts.  This is
127  * because the different definitions are used at different times; the
128  * server-wide ones are used in the post-read-request phase, and the
129  * per-directory ones are used during the header-parse phase (after
130  * the URI has been mapped to a file and we have anything from the
131  * .htaccess file and <Directory> and <Files> containers).
132  */
133 static void *create_setenvif_config(apr_pool_t *p)
134 {
135     sei_cfg_rec *new = (sei_cfg_rec *) apr_palloc(p, sizeof(sei_cfg_rec));
136
137     new->conditionals = apr_array_make(p, 20, sizeof(sei_entry));
138     return (void *) new;
139 }
140
141 static void *create_setenvif_config_svr(apr_pool_t *p, server_rec *dummy)
142 {
143     return create_setenvif_config(p);
144 }
145
146 static void *create_setenvif_config_dir(apr_pool_t *p, char *dummy)
147 {
148     return create_setenvif_config(p);
149 }
150
151 static void *merge_setenvif_config(apr_pool_t *p, void *basev, void *overridesv)
152 {
153     sei_cfg_rec *a = apr_pcalloc(p, sizeof(sei_cfg_rec));
154     sei_cfg_rec *base = basev, *overrides = overridesv;
155
156     a->conditionals = apr_array_append(p, base->conditionals,
157                                        overrides->conditionals);
158     return a;
159 }
160
161 /*
162  * any non-NULL magic constant will do... used to indicate if REG_ICASE should
163  * be used
164  */
165 #define ICASE_MAGIC     ((void *)(&setenvif_module))
166 #define SEI_MAGIC_HEIRLOOM "setenvif-phase-flag"
167
168 static int is_header_regex(apr_pool_t *p, const char* name) 
169 {
170     /* If a Header name contains characters other than:
171      *    -,_,[A-Z\, [a-z] and [0-9].
172      * assume the header name is a regular expression.
173      */
174     regex_t *preg = ap_pregcomp(p, "^[-A-Za-z0-9_]*$",
175                                 (REG_EXTENDED | REG_NOSUB ));
176     ap_assert(preg != NULL);
177
178     if (ap_regexec(preg, name, 0, NULL, 0)) {
179         return 1;
180     }
181
182     return 0;
183 }
184
185 /* If the input string does not take advantage of regular
186  * expression metacharacters, return a pointer to an equivalent
187  * string that can be searched using apr_strmatch().  (The
188  * returned string will often be the input string.  But if
189  * the input string contains escaped characters, the returned
190  * string will be a copy with the escapes removed.)
191  */
192 static const char *non_regex_pattern(apr_pool_t *p, const char *s)
193 {
194     const char *src = s;
195     int escapes_found = 0;
196     int in_escape = 0;
197
198     while (*src) {
199         switch (*src) {
200         case '^':
201         case '.':
202         case '$':
203         case '|':
204         case '(':
205         case ')':
206         case '[':
207         case ']':
208         case '*':
209         case '+':
210         case '?':
211         case '{':
212         case '}':
213             if (!in_escape) {
214                 return NULL;
215             }
216             in_escape = 0;
217             break;
218         case '\\':
219             if (!in_escape) {
220                 in_escape = 1;
221                 escapes_found = 1;
222             }
223             else {
224                 in_escape = 0;
225             }
226             break;
227         default:
228             if (in_escape) {
229                 return NULL;
230             }
231             break;
232         }
233         src++;
234     }
235     if (!escapes_found) {
236         return s;
237     }
238     else {
239         char *unescaped = (char *)apr_palloc(p, src - s + 1);
240         char *dst = unescaped;
241         src = s;
242         do {
243             if (*src == '\\') {
244                 src++;
245             }
246         } while ((*dst++ = *src++));
247         return unescaped;
248     }
249 }
250
251 static const char *add_setenvif_core(cmd_parms *cmd, void *mconfig,
252                                      char *fname, const char *args)
253 {
254     char *regex;
255     const char *simple_pattern;
256     const char *feature;
257     sei_cfg_rec *sconf;
258     sei_entry *new;
259     sei_entry *entries;
260     char *var;
261     int i;
262     int beenhere = 0;
263     int icase;
264
265     /*
266      * Determine from our context into which record to put the entry.
267      * cmd->path == NULL means we're in server-wide context; otherwise,
268      * we're dealing with a per-directory setting.
269      */
270     sconf = (cmd->path != NULL)
271       ? (sei_cfg_rec *) mconfig
272       : (sei_cfg_rec *) ap_get_module_config(cmd->server->module_config,
273                                                &setenvif_module);
274     entries = (sei_entry *) sconf->conditionals->elts;
275     /* get regex */
276     regex = ap_getword_conf(cmd->pool, &args);
277     if (!*regex) {
278         return apr_pstrcat(cmd->pool, "Missing regular expression for ",
279                            cmd->cmd->name, NULL);
280     }
281
282     /*
283      * If we've already got a sei_entry with the same name we want to
284      * just copy the name pointer... so that later on we can compare
285      * two header names just by comparing the pointers.
286      */
287     for (i = 0; i < sconf->conditionals->nelts; ++i) {
288         new = &entries[i];
289         if (!strcasecmp(new->name, fname)) {
290             fname = new->name;
291             break;
292         }
293     }
294
295     /* if the last entry has an identical headername and regex then
296      * merge with it
297      */
298     i = sconf->conditionals->nelts - 1;
299     icase = cmd->info == ICASE_MAGIC;
300     if (i < 0
301         || entries[i].name != fname
302         || entries[i].icase != icase
303         || strcmp(entries[i].regex, regex)) {
304
305         /* no match, create a new entry */
306         new = apr_array_push(sconf->conditionals);
307         new->name = fname;
308         new->regex = regex;
309         new->icase = icase;
310         if ((simple_pattern = non_regex_pattern(cmd->pool, regex))) {
311             new->pattern = apr_strmatch_precompile(cmd->pool,
312                                                    simple_pattern, !icase);
313             if (new->pattern == NULL) {
314                 return apr_pstrcat(cmd->pool, cmd->cmd->name,
315                                    " pattern could not be compiled.", NULL);
316             }
317             new->preg = NULL;
318         }
319         else {
320             new->preg = ap_pregcomp(cmd->pool, regex,
321                                     (REG_EXTENDED | (icase ? REG_ICASE : 0)));
322             if (new->preg == NULL) {
323                 return apr_pstrcat(cmd->pool, cmd->cmd->name,
324                                    " regex could not be compiled.", NULL);
325             }
326             new->pattern = NULL;
327         }
328         new->features = apr_table_make(cmd->pool, 2);
329
330         if (!strcasecmp(fname, "remote_addr")) {
331             new->special_type = SPECIAL_REMOTE_ADDR;
332         }
333         else if (!strcasecmp(fname, "remote_host")) {
334             new->special_type = SPECIAL_REMOTE_HOST;
335         }
336         else if (!strcasecmp(fname, "request_uri")) {
337             new->special_type = SPECIAL_REQUEST_URI;
338         }
339         else if (!strcasecmp(fname, "request_method")) {
340             new->special_type = SPECIAL_REQUEST_METHOD;
341         }
342         else if (!strcasecmp(fname, "request_protocol")) {
343             new->special_type = SPECIAL_REQUEST_PROTOCOL;
344         }
345         else if (!strcasecmp(fname, "server_addr")) {
346             new->special_type = SPECIAL_SERVER_ADDR;
347         }
348         else {
349             new->special_type = SPECIAL_NOT;
350             /* Handle fname as a regular expression.
351              * If fname a simple header string, identify as such
352              * (new->pnamereg = NULL) to avoid the overhead of searching
353              * through headers_in for a regex match.
354              */
355             if (is_header_regex(cmd->pool, fname)) {
356                 new->pnamereg = ap_pregcomp(cmd->pool, fname,
357                                             (REG_EXTENDED | REG_NOSUB
358                                              | (icase ? REG_ICASE : 0)));
359                 if (new->pnamereg == NULL)
360                     return apr_pstrcat(cmd->pool, cmd->cmd->name,
361                                        "Header name regex could not be "
362                                        "compiled.", NULL);
363             }
364             else {
365                 new->pnamereg = NULL;
366             }
367         }
368     }
369     else {
370         new = &entries[i];
371     }
372
373     for ( ; ; ) {
374         feature = ap_getword_conf(cmd->pool, &args);
375         if (!*feature) {
376             break;
377         }
378         beenhere++;
379
380         var = ap_getword(cmd->pool, &feature, '=');
381         if (*feature) {
382             apr_table_setn(new->features, var, feature);
383         }
384         else if (*var == '!') {
385             apr_table_setn(new->features, var + 1, "!");
386         }
387         else {
388             apr_table_setn(new->features, var, "1");
389         }
390     }
391
392     if (!beenhere) {
393         return apr_pstrcat(cmd->pool, "Missing envariable expression for ",
394                            cmd->cmd->name, NULL);
395     }
396
397     return NULL;
398 }
399
400 static const char *add_setenvif(cmd_parms *cmd, void *mconfig,
401                                 const char *args)
402 {
403     char *fname;
404
405     /* get header name */
406     fname = ap_getword_conf(cmd->pool, &args);
407     if (!*fname) {
408         return apr_pstrcat(cmd->pool, "Missing header-field name for ",
409                            cmd->cmd->name, NULL);
410     }
411     return add_setenvif_core(cmd, mconfig, fname, args);
412 }
413
414 /*
415  * This routine handles the BrowserMatch* directives.  It simply turns around
416  * and feeds them, with the appropriate embellishments, to the general-purpose
417  * command handler.
418  */
419 static const char *add_browser(cmd_parms *cmd, void *mconfig, const char *args)
420 {
421     return add_setenvif_core(cmd, mconfig, "User-Agent", args);
422 }
423
424 static const command_rec setenvif_module_cmds[] =
425 {
426     AP_INIT_RAW_ARGS("SetEnvIf", add_setenvif, NULL, OR_FILEINFO,
427                      "A header-name, regex and a list of variables."),
428     AP_INIT_RAW_ARGS("SetEnvIfNoCase", add_setenvif, ICASE_MAGIC, OR_FILEINFO,
429                      "a header-name, regex and a list of variables."),
430     AP_INIT_RAW_ARGS("BrowserMatch", add_browser, NULL, OR_FILEINFO,
431                      "A browser regex and a list of variables."),
432     AP_INIT_RAW_ARGS("BrowserMatchNoCase", add_browser, ICASE_MAGIC,
433                      OR_FILEINFO,
434                      "A browser regex and a list of variables."),
435     { NULL },
436 };
437
438 /*
439  * This routine gets called at two different points in request processing:
440  * once before the URI has been translated (during the post-read-request
441  * phase) and once after (during the header-parse phase).  We use different
442  * config records for the two different calls to reduce overhead (by not
443  * re-doing the server-wide settings during directory processing), and
444  * signal which call it is by having the earlier one pass a flag to the
445  * later one.
446  */
447 static int match_headers(request_rec *r)
448 {
449     sei_cfg_rec *sconf;
450     sei_entry *entries;
451     const apr_table_entry_t *elts;
452     const char *val;
453     apr_size_t val_len = 0;
454     int i, j;
455     char *last_name;
456     regmatch_t regm[AP_MAX_REG_MATCH];
457
458     if (!ap_get_module_config(r->request_config, &setenvif_module)) {
459         ap_set_module_config(r->request_config, &setenvif_module,
460                              SEI_MAGIC_HEIRLOOM);
461         sconf  = (sei_cfg_rec *) ap_get_module_config(r->server->module_config,
462                                                       &setenvif_module);
463     }
464     else {
465         sconf = (sei_cfg_rec *) ap_get_module_config(r->per_dir_config,
466                                                      &setenvif_module);
467     }
468     entries = (sei_entry *) sconf->conditionals->elts;
469     last_name = NULL;
470     val = NULL;
471     for (i = 0; i < sconf->conditionals->nelts; ++i) {
472         sei_entry *b = &entries[i];
473
474         /* Optimize the case where a bunch of directives in a row use the
475          * same header.  Remember we don't need to strcmp the two header
476          * names because we made sure the pointers were equal during
477          * configuration.
478          */
479         if (b->name != last_name) {
480             last_name = b->name;
481             switch (b->special_type) {
482             case SPECIAL_REMOTE_ADDR:
483                 val = r->connection->remote_ip;
484                 break;
485             case SPECIAL_SERVER_ADDR:
486                 val = r->connection->local_ip;
487                 break;
488             case SPECIAL_REMOTE_HOST:
489                 val =  ap_get_remote_host(r->connection, r->per_dir_config,
490                                           REMOTE_NAME, NULL);
491                 break;
492             case SPECIAL_REQUEST_URI:
493                 val = r->uri;
494                 break;
495             case SPECIAL_REQUEST_METHOD:
496                 val = r->method;
497                 break;
498             case SPECIAL_REQUEST_PROTOCOL:
499                 val = r->protocol;
500                 break;
501             case SPECIAL_NOT:
502                 if (b->pnamereg) {
503                     /* Matching headers_in against a regex. Iterate through
504                      * the headers_in until we find a match or run out of
505                      * headers.
506                      */
507                     const apr_array_header_t
508                         *arr = apr_table_elts(r->headers_in);
509
510                     elts = (const apr_table_entry_t *) arr->elts;
511                     val = NULL;
512                     for (j = 0; j < arr->nelts; ++j) {
513                         if (!ap_regexec(b->pnamereg, elts[j].key, 0, NULL, 0)) { 
514                             val = elts[j].val;
515                         }
516                     }
517                 }
518                 else {
519                     /* Not matching against a regex */
520                     val = apr_table_get(r->headers_in, b->name);
521                     if (val == NULL) {
522                         val = apr_table_get(r->subprocess_env, b->name);
523                     }
524                 }
525             }
526             val_len = val ? strlen(val) : 0;
527         }
528
529         /*
530          * A NULL value indicates that the header field or special entity
531          * wasn't present or is undefined.  Represent that as an empty string
532          * so that REs like "^$" will work and allow envariable setting
533          * based on missing or empty field.
534          */
535         if (val == NULL) {
536             val = "";
537             val_len = 0;
538         }
539
540         if ((b->pattern && apr_strmatch(b->pattern, val, val_len)) ||
541             (!b->pattern && !ap_regexec(b->preg, val, AP_MAX_REG_MATCH, regm,
542                                         0))) {
543             const apr_array_header_t *arr = apr_table_elts(b->features);
544             elts = (const apr_table_entry_t *) arr->elts;
545
546             for (j = 0; j < arr->nelts; ++j) {
547                 if (*(elts[j].val) == '!') {
548                     apr_table_unset(r->subprocess_env, elts[j].key);
549                 }
550                 else {
551                     if (!b->pattern) {
552                         char *replaced = ap_pregsub(r->pool, elts[j].val, val,
553                                                     AP_MAX_REG_MATCH, regm);
554                         if (replaced) {
555                             apr_table_setn(r->subprocess_env, elts[j].key,
556                                            replaced);
557                         }
558                     }
559                     else {
560                         apr_table_setn(r->subprocess_env, elts[j].key,
561                                        elts[j].val);
562                     }
563                 }
564             }
565         }
566     }
567
568     return DECLINED;
569 }
570
571 static void register_hooks(apr_pool_t *p)
572 {
573     ap_hook_header_parser(match_headers, NULL, NULL, APR_HOOK_MIDDLE);
574     ap_hook_post_read_request(match_headers, NULL, NULL, APR_HOOK_MIDDLE);
575 }
576
577 module AP_MODULE_DECLARE_DATA setenvif_module =
578 {
579     STANDARD20_MODULE_STUFF,
580     create_setenvif_config_dir, /* dir config creater */
581     merge_setenvif_config,      /* dir merger --- default is to override */
582     create_setenvif_config_svr, /* server config */
583     merge_setenvif_config,      /* merge server configs */
584     setenvif_module_cmds,       /* command apr_table_t */
585     register_hooks              /* register hooks */
586 };