1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * http_include.c: Handles the server-parsed HTML documents
62 * Original by Rob McCool; substantial fixups by David Robinson;
63 * incorporated into the Apache module framework by rst.
67 * sub key may be anything a Perl*Handler can be:
68 * subroutine name, package name (defaults to package::handler),
69 * Class->method call or anoymous sub {}
71 * Child <!--#perl sub="sub {print $$}" --> accessed
72 * <!--#perl sub="sub {print ++$Access::Cnt }" --> times. <br>
74 * <!--#perl arg="one" sub="mymod::includer" -->
88 #include "modules/perl/mod_perl.h"
90 #include "apr_strings.h"
91 #include "ap_config.h"
92 #include "util_filter.h"
94 #include "http_config.h"
95 #include "http_request.h"
96 #include "http_core.h"
97 #include "http_protocol.h"
99 #include "http_main.h"
100 #include "util_script.h"
101 #include "http_core.h"
105 #ifdef HAVE_STRINGS_H
112 #include "util_ebcdic.h"
114 #define STARTING_SEQUENCE "<!--#"
115 #define ENDING_SEQUENCE "-->"
117 #define DEFAULT_ERROR_MSG "[an error occurred while processing this directive]"
118 #define DEFAULT_TIME_FORMAT "%A, %d-%b-%Y %H:%M:%S %Z"
119 #define SIZEFMT_BYTES 0
120 #define SIZEFMT_KMG 1
121 #ifdef CHARSET_EBCDIC
122 #define RAW_ASCII_CHAR(ch) apr_xlate_conv_byte(ap_hdrs_from_ascii, (unsigned char)ch)
123 #else /*CHARSET_EBCDIC*/
124 #define RAW_ASCII_CHAR(ch) (ch)
125 #endif /*CHARSET_EBCDIC*/
127 module MODULE_VAR_EXPORT includes_module;
129 /* just need some arbitrary non-NULL pointer which can't also be a request_rec */
130 #define NESTED_INCLUDE_MAGIC (&includes_module)
132 /* TODO: changing directory should be handled by CreateProcess */
133 #define ap_chdir_file(x) do {} while(0)
135 /* ------------------------ Environment function -------------------------- */
137 /* XXX: could use ap_table_overlap here */
138 static void add_include_vars(request_rec *r, char *timefmt)
142 #endif /* ndef WIN32 */
143 apr_table_t *e = r->subprocess_env;
145 apr_time_t date = r->request_time;
147 apr_table_setn(e, "DATE_LOCAL", ap_ht_time(r->pool, date, timefmt, 0));
148 apr_table_setn(e, "DATE_GMT", ap_ht_time(r->pool, date, timefmt, 1));
149 apr_table_setn(e, "LAST_MODIFIED",
150 ap_ht_time(r->pool, r->finfo.mtime, timefmt, 0));
151 apr_table_setn(e, "DOCUMENT_URI", r->uri);
152 apr_table_setn(e, "DOCUMENT_PATH_INFO", r->path_info);
154 pw = getpwuid(r->finfo.user);
156 apr_table_setn(e, "USER_NAME", apr_pstrdup(r->pool, pw->pw_name));
159 apr_table_setn(e, "USER_NAME", apr_psprintf(r->pool, "user#%lu",
160 (unsigned long) r->finfo.user));
162 #endif /* ndef WIN32 */
164 if ((t = strrchr(r->filename, '/'))) {
165 apr_table_setn(e, "DOCUMENT_NAME", ++t);
168 apr_table_setn(e, "DOCUMENT_NAME", r->uri);
171 char *arg_copy = apr_pstrdup(r->pool, r->args);
173 ap_unescape_url(arg_copy);
174 apr_table_setn(e, "QUERY_STRING_UNESCAPED",
175 ap_escape_shell_cmd(r->pool, arg_copy));
181 /* --------------------------- Parser functions --------------------------- */
183 #define OUTBUFSIZE 4096
185 static ap_bucket *find_string(ap_bucket *dptr, const char *str, ap_bucket *end)
193 if (dptr->type == ap_eos_type()) {
196 ap_bucket_read(dptr, &buf, &len, 0);
197 /* XXX handle retcodes */
198 if (len == 0) { /* end of pipe? */
202 while (c - buf != len) {
203 if (*c == str[state]) {
207 if (str[state] == '\0') {
208 /* We want to split the bucket at the '<' and '>'
209 * respectively. That means adjusting where we split based
210 * on what we are searching for.
213 ap_bucket_split(dptr, c - buf - strlen(str));
216 ap_bucket_split(dptr, c - buf);
218 return AP_BUCKET_NEXT(dptr);
222 /* The reason for this, is that we need to make sure
223 * that we catch cases like <<--#. This makes the
224 * second check after the original check fails.
226 if (*c == buf[state]) {
233 dptr = AP_BUCKET_NEXT(dptr);
234 } while (AP_BUCKET_PREV(dptr) != end);
239 * decodes a string containing html entities or numeric character references.
240 * 's' is overwritten with the decoded string.
241 * If 's' is syntatically incorrect, then the followed fixups will be made:
242 * unknown entities will be left undecoded;
243 * references to unused numeric characters will be deleted.
244 * In particular, � will not be decoded, but will be deleted.
249 /* maximum length of any ISO-LATIN-1 HTML entity name. */
250 #define MAXENTLEN (6)
252 /* The following is a shrinking transformation, therefore safe. */
254 static void decodehtml(char *s)
259 static const char * const entlist[MAXENTLEN + 1] =
263 "lt\074gt\076", /* 2 */
264 "amp\046ETH\320eth\360", /* 3 */
265 "quot\042Auml\304Euml\313Iuml\317Ouml\326Uuml\334auml\344euml\353\
266 iuml\357ouml\366uuml\374yuml\377", /* 4 */
267 "Acirc\302Aring\305AElig\306Ecirc\312Icirc\316Ocirc\324Ucirc\333\
268 THORN\336szlig\337acirc\342aring\345aelig\346ecirc\352icirc\356ocirc\364\
269 ucirc\373thorn\376", /* 5 */
270 "Agrave\300Aacute\301Atilde\303Ccedil\307Egrave\310Eacute\311\
271 Igrave\314Iacute\315Ntilde\321Ograve\322Oacute\323Otilde\325Oslash\330\
272 Ugrave\331Uacute\332Yacute\335agrave\340aacute\341atilde\343ccedil\347\
273 egrave\350eacute\351igrave\354iacute\355ntilde\361ograve\362oacute\363\
274 otilde\365oslash\370ugrave\371uacute\372yacute\375" /* 6 */
277 for (; *s != '\0'; s++, p++) {
282 /* find end of entity */
283 for (i = 1; s[i] != ';' && s[i] != '\0'; i++) {
287 if (s[i] == '\0') { /* treat as normal data */
292 /* is it numeric ? */
294 for (j = 2, val = 0; j < i && apr_isdigit(s[j]); j++) {
295 val = val * 10 + s[j] - '0';
298 if (j < i || val <= 8 || (val >= 11 && val <= 31) ||
299 (val >= 127 && val <= 160) || val >= 256) {
300 p--; /* no data to output */
303 *p = RAW_ASCII_CHAR(val);
308 if (j > MAXENTLEN || entlist[j] == NULL) {
311 continue; /* skip it */
313 for (ents = entlist[j]; *ents != '\0'; ents += i) {
314 if (strncmp(s + 1, ents, j) == 0) {
320 *p = '&'; /* unknown */
323 *p = RAW_ASCII_CHAR(((const unsigned char *) ents)[j]);
333 * extract the next tag name and value.
334 * if there are no more tags, set the tag name to 'done'
335 * the tag value is html decoded if dodecode is non-zero
338 static char *get_tag(apr_pool_t *p, ap_bucket *in, char *tag, int tagbuf_len, int dodecode, apr_off_t *offset)
340 ap_bucket *dptr = in;
344 char *t = tag, *tag_val, term;
346 /* makes code below a little less cluttered */
349 /* Remove all whitespace */
351 ap_bucket_read(dptr, &str, &length, 0);
353 while (c - str < length) {
354 if (!apr_isspace(*c)) {
358 if (!apr_isspace(*c)) {
361 dptr = AP_BUCKET_NEXT(dptr);
364 /* tags can't start with - */
368 ap_bucket_read(dptr, &str, &length, 0);
375 ap_bucket_read(dptr, &str, &length, 0);
378 } while (apr_isspace(*c));
380 apr_cpystrn(tag, "done", tagbuf_len);
385 return NULL; /* failed */
388 /* find end of tag name */
390 if (t - tag == tagbuf_len) {
394 if (*c == '=' || apr_isspace(*c)) {
397 *(t++) = apr_tolower(*c);
400 ap_bucket_read(dptr, &str, &length, 0);
408 while (apr_isspace(*c)) {
411 ap_bucket_read(dptr, &str, &length, 0);
416 /* XXX may need to ungetc() here (see pre-bucketized code) */
423 ap_bucket_read(dptr, &str, &length, 0);
426 } while (apr_isspace(*c));
428 /* we should allow a 'name' as a value */
430 if (*c != '"' && *c != '\'') {
437 ap_bucket_read(dptr, &str, &length, 0);
440 if (t - tag == tagbuf_len) {
444 /* Want to accept \" as a valid character within a string. */
446 *(t++) = *c; /* Add backslash */
449 ap_bucket_read(dptr, &str, &length, 0);
452 if (*c == term) { /* Only if */
453 *(--t) = *c; /* Replace backslash ONLY for terminator */
456 else if (*c == term) {
466 return apr_pstrdup(p, tag_val);
469 static int get_directive(ap_bucket *in, char *dest, size_t len, apr_pool_t *p)
471 ap_bucket *dptr = in;
477 /* make room for nul terminator */
481 ap_bucket_read(dptr, &str, &length, 0);
482 /* need to start past the <!--#
484 c = str + strlen(STARTING_SEQUENCE);
485 while (c - str < length) {
486 if (!apr_isspace(*c)) {
490 if (!apr_isspace(*c)) {
493 dptr = AP_BUCKET_NEXT(dptr);
496 /* now get directive */
498 if (c - str >= length) {
499 ap_bucket_read(dptr, &str, &length, 0);
501 while (c - str < length) {
502 if (d - dest == (int)len) {
505 *d++ = apr_tolower(*c);
507 if (apr_isspace(*c)) {
511 if (apr_isspace(*c)) {
514 dptr = AP_BUCKET_NEXT(dptr);
521 * Do variable substitution on strings
523 static void parse_string(request_rec *r, const char *in, char *out,
524 size_t length, int leave_name)
530 /* leave room for nul terminator */
531 end_out = out + length - 1;
533 while ((ch = *in++) != '\0') {
536 if (next == end_out) {
550 char var[MAX_STRING_LEN];
551 const char *start_of_var_name;
552 const char *end_of_var_name; /* end of var name + 1 */
553 const char *expansion;
557 /* guess that the expansion won't happen */
561 start_of_var_name = in;
562 in = ap_strchr_c(in, '}');
564 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
565 0, r, "Missing '}' on variable \"%s\"",
570 end_of_var_name = in;
574 start_of_var_name = in;
575 while (apr_isalnum(*in) || *in == '_') {
578 end_of_var_name = in;
580 /* what a pain, too bad there's no table_getn where you can
581 * pass a non-nul terminated string */
582 l = end_of_var_name - start_of_var_name;
584 l = (l > sizeof(var) - 1) ? (sizeof(var) - 1) : l;
585 memcpy(var, start_of_var_name, l);
588 val = apr_table_get(r->subprocess_env, var);
591 l = strlen(expansion);
593 else if (leave_name) {
597 break; /* no expansion to be done */
601 /* zero-length variable name causes just the $ to be copied */
604 l = ((int)l > end_out - next) ? (end_out - next) : l;
605 memcpy(next, expansion, l);
610 if (next == end_out) {
623 /* --------------------------- Action handlers ---------------------------- */
625 static int include_cgi(char *s, request_rec *r, ap_filter_t *next)
627 request_rec *rr = ap_sub_req_lookup_uri(s, r);
630 if (rr->status != HTTP_OK) {
634 /* No hardwired path info or query allowed */
636 if ((rr->path_info && rr->path_info[0]) || rr->args) {
639 if (rr->finfo.protection == 0) {
643 /* Script gets parameters of the *document*, for back compatibility */
645 rr->path_info = r->path_info; /* hard to get right; see mod_cgi.c */
648 /* Force sub_req to be treated as a CGI request, even if ordinary
649 * typing rules would have called it something else.
652 rr->content_type = CGI_MAGIC_TYPE;
654 /* The subrequest should inherit the remaining filters from this request. */
655 rr->output_filters = next;
659 rr_status = ap_run_sub_req(rr);
660 if (ap_is_HTTP_REDIRECT(rr_status)) {
661 const char *location = apr_table_get(rr->headers_out, "Location");
662 location = ap_escape_html(rr->pool, location);
663 ap_rvputs(r, "<A HREF=\"", location, "\">", location, "</A>", NULL);
666 ap_destroy_sub_req(rr);
667 ap_chdir_file(r->filename);
672 /* ensure that path is relative, and does not contain ".." elements
673 * ensentially ensure that it does not match the regex:
674 * (^/|(^|/)\.\.(/|$))
675 * XXX: this needs os abstraction... consider c:..\foo in win32
677 static int is_only_below(const char *path)
679 #ifdef HAVE_DRIVE_LETTERS
683 if (path[0] == '/') {
686 if (path[0] == '.' && path[1] == '.'
687 && (path[2] == '\0' || path[2] == '/')) {
691 if (*path == '/' && path[1] == '.' && path[2] == '.'
692 && (path[3] == '\0' || path[3] == '/')) {
700 static int handle_include(ap_bucket *in, request_rec *r, ap_filter_t *next,
701 const char *error, int noexec)
703 char tag[MAX_STRING_LEN];
704 char parsed_string[MAX_STRING_LEN];
706 apr_off_t offset = strlen("include ") + strlen(STARTING_SEQUENCE);
709 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
712 if (!strcmp(tag, "file") || !strcmp(tag, "virtual")) {
713 request_rec *rr = NULL;
714 char *error_fmt = NULL;
716 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
718 /* be safe; only files in this directory or below allowed */
719 if (!is_only_below(parsed_string)) {
720 error_fmt = "unable to include file \"%s\" "
724 rr = ap_sub_req_lookup_file(parsed_string, r);
728 rr = ap_sub_req_lookup_uri(parsed_string, r);
731 if (!error_fmt && rr->status != HTTP_OK) {
732 error_fmt = "unable to include \"%s\" in parsed file %s";
735 if (!error_fmt && noexec && rr->content_type
736 && (strncmp(rr->content_type, "text/", 5))) {
737 error_fmt = "unable to include potential exec \"%s\" "
740 if (error_fmt == NULL) {
741 /* try to avoid recursive includes. We do this by walking
742 * up the r->main list of subrequests, and at each level
743 * walking back through any internal redirects. At each
744 * step, we compare the filenames and the URIs.
746 * The filename comparison catches a recursive include
747 * with an ever-changing URL, eg.
748 * <!--#include virtual=
749 * "$REQUEST_URI/$QUERY_STRING?$QUERY_STRING/x"-->
750 * which, although they would eventually be caught because
751 * we have a limit on the length of files, etc., can
752 * recurse for a while.
754 * The URI comparison catches the case where the filename
755 * is changed while processing the request, so the
756 * current name is never the same as any previous one.
757 * This can happen with "DocumentRoot /foo" when you
758 * request "/" on the server and it includes "/".
759 * This only applies to modules such as mod_dir that
760 * (somewhat improperly) mess with r->filename outside
761 * of a filename translation phase.
765 for (p = r; p != NULL && !founddupe; p = p->main) {
767 for (q = p; q != NULL; q = q->prev) {
768 if ( (strcmp(q->filename, rr->filename) == 0) ||
769 (strcmp(q->uri, rr->uri) == 0) ){
777 error_fmt = "Recursive include of \"%s\" "
782 /* see the Kludge in send_parsed_file for why */
784 ap_set_module_config(rr->request_config, &includes_module, r);
787 /* The subrequest should inherit the remaining filters from
789 rr->output_filters = next;
790 if (ap_run_sub_req(rr)) {
791 error_fmt = "unable to include \"%s\" in parsed file %s";
794 ap_chdir_file(r->filename);
796 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
797 0, r, error_fmt, tag_val, r->filename);
801 /* destroy the sub request if it's not a nested include */
803 && ap_get_module_config(rr->request_config, &includes_module)
804 != NESTED_INCLUDE_MAGIC) {
805 ap_destroy_sub_req(rr);
808 else if (!strcmp(tag, "done")) {
812 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
813 "unknown parameter \"%s\" to tag include in %s",
830 static apr_status_t build_argv_list(char ***argv, request_rec *r, apr_pool_t *p)
832 int numwords, x, idx;
834 const char *args = r->args;
836 if (!args || !args[0] || ap_strchr_c(args, '=')) {
840 /* count the number of keywords */
841 for (x = 0, numwords = 1; args[x]; x++) {
842 if (args[x] == '+') {
847 /* Everything is - 1 to account for the first parameter which is the
848 * program name. We didn't used to have to do this, but APR wants it.
850 if (numwords > APACHE_ARG_MAX - 1) {
851 numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
853 *argv = (char **) apr_palloc(p, (numwords + 2) * sizeof(char *));
855 for (x = 1, idx = 1; x < numwords; x++) {
856 w = ap_getword_nulls(p, &args, '+');
858 (*argv)[idx++] = ap_escape_shell_cmd(p, w);
867 static int include_cmd(char *s, request_rec *r, ap_filter_t *next)
870 apr_procattr_t *procattr;
873 apr_table_t *env = r->subprocess_env;
875 apr_file_t *file = NULL;
876 #if defined(RLIMIT_CPU) || defined(RLIMIT_NPROC) || \
877 defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined (RLIMIT_AS)
878 core_dir_config *conf;
879 conf = (core_dir_config *) ap_get_module_config(r->per_dir_config,
886 arg.t.filename = r->filename;
887 arg.t.subprocess_env = r->subprocess_env;
888 arg.t.prog_type = FORK_FILE;
891 if (r->path_info && r->path_info[0] != '\0') {
894 apr_table_setn(env, "PATH_INFO", ap_escape_shell_cmd(r->pool, r->path_info));
896 pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r);
897 if (pa_req->filename) {
898 apr_table_setn(env, "PATH_TRANSLATED",
899 apr_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
905 char *arg_copy = apr_pstrdup(r->pool, r->args);
907 apr_table_setn(env, "QUERY_STRING", r->args);
908 ap_unescape_url(arg_copy);
909 apr_table_setn(env, "QUERY_STRING_UNESCAPED",
910 ap_escape_shell_cmd(r->pool, arg_copy));
913 if ((apr_createprocattr_init(&procattr, r->pool) != APR_SUCCESS) ||
914 (apr_setprocattr_io(procattr, APR_NO_PIPE,
915 APR_FULL_BLOCK, APR_NO_PIPE) != APR_SUCCESS) ||
916 (apr_setprocattr_dir(procattr, ap_make_dirstr_parent(r->pool, r->filename)) != APR_SUCCESS) ||
918 ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_CPU, conf->limit_cpu)) != APR_SUCCESS) ||
920 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
921 ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_MEM, conf->limit_mem)) != APR_SUCCESS) ||
924 ((rc = apr_setprocattr_limit(procattr, APR_LIMIT_NPROC, conf->limit_nproc)) != APR_SUCCESS) ||
926 (apr_setprocattr_cmdtype(procattr, APR_SHELLCMD) != APR_SUCCESS)) {
927 /* Something bad happened, tell the world. */
928 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
929 "couldn't initialize proc attributes: %s %s", r->filename, s);
933 build_argv_list(&argv, r, r->pool);
934 argv[0] = apr_pstrdup(r->pool, s);
935 procnew = apr_pcalloc(r->pool, sizeof(*procnew));
936 rc = apr_create_process(procnew, s, argv, ap_create_environment(r->pool, env), procattr, r->pool);
938 if (rc != APR_SUCCESS) {
939 /* Bad things happened. Everyone should have cleaned up. */
940 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
941 "couldn't create child process: %d: %s", rc, s);
944 ap_bucket_brigade *bcgi;
947 apr_note_subprocess(r->pool, procnew, kill_after_timeout);
948 /* Fill in BUFF structure for parents pipe to child's stdout */
952 bcgi = ap_brigade_create(r->pool);
953 b = ap_bucket_create_pipe(file);
954 AP_BRIGADE_INSERT_TAIL(bcgi, b);
955 ap_pass_brigade(next, bcgi);
957 /* We can't close the pipe here, because we may return before the
958 * full CGI has been sent to the network. That's okay though,
959 * because we can rely on the pool to close the pipe for us.
967 static int handle_exec(ap_bucket *in, request_rec *r, const char *error,
970 char tag[MAX_STRING_LEN];
972 char *file = r->filename;
973 char parsed_string[MAX_STRING_LEN];
974 apr_off_t offset = strlen("exec ") + strlen(STARTING_SEQUENCE);
977 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
980 if (!strcmp(tag, "cmd")) {
981 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 1);
982 if (include_cmd(parsed_string, r, next) == -1) {
983 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
984 "execution failure for parameter \"%s\" "
985 "to tag exec in file %s",
989 /* just in case some stooge changed directories */
990 ap_chdir_file(r->filename);
992 else if (!strcmp(tag, "cgi")) {
993 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
994 if (include_cgi(parsed_string, r, next) == -1) {
995 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
996 "invalid CGI ref \"%s\" in %s", tag_val, file);
999 ap_chdir_file(r->filename);
1001 else if (!strcmp(tag, "done")) {
1005 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1006 "unknown parameter \"%s\" to tag exec in %s",
1014 static int handle_echo(ap_bucket *in, request_rec *r, const char *error)
1016 char tag[MAX_STRING_LEN];
1018 enum {E_NONE, E_URL, E_ENTITY} encode;
1019 apr_off_t offset = strlen("echo ") + strlen(STARTING_SEQUENCE);
1024 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
1027 if (!strcmp(tag, "var")) {
1028 const char *val = apr_table_get(r->subprocess_env, tag_val);
1031 if (encode == E_NONE) {
1034 else if (encode == E_URL) {
1035 ap_rputs(ap_escape_uri(r->pool, val), r);
1037 else if (encode == E_ENTITY) {
1038 ap_rputs(ap_escape_html(r->pool, val), r);
1042 ap_rputs("(none)", r);
1045 else if (!strcmp(tag, "done")) {
1048 else if (!strcmp(tag, "encoding")) {
1049 if (!strcasecmp(tag_val, "none")) encode = E_NONE;
1050 else if (!strcasecmp(tag_val, "url")) encode = E_URL;
1051 else if (!strcasecmp(tag_val, "entity")) encode = E_ENTITY;
1053 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1054 "unknown value \"%s\" to parameter \"encoding\" of "
1056 tag_val, r->filename);
1062 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1063 "unknown parameter \"%s\" to tag echo in %s",
1071 static int handle_perl(ap_bucket *in, request_rec *r, const char *error)
1073 char tag[MAX_STRING_LEN];
1074 char parsed_string[MAX_STRING_LEN];
1078 apr_off_t offset = strlen("perl ") + strlen(STARTING_SEQUENCE);
1080 if (ap_allow_options(r) & OPT_INCNOEXEC) {
1081 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1082 "#perl SSI disallowed by IncludesNoExec in %s",
1087 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
1090 if (strnEQ(tag, "sub", 3)) {
1091 sub = newSVpv(tag_val, 0);
1093 else if (strnEQ(tag, "arg", 3)) {
1094 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1095 av_push(av, newSVpv(parsed_string, 0));
1097 else if (strnEQ(tag, "done", 4)) {
1101 perl_stdout2client(r);
1103 perl_call_handler(sub, r, av);
1108 /* error and tf must point to a string with room for at
1109 * least MAX_STRING_LEN characters
1111 static int handle_config(ap_bucket *in, request_rec *r, char *error, char *tf,
1114 char tag[MAX_STRING_LEN];
1116 char parsed_string[MAX_STRING_LEN];
1117 apr_table_t *env = r->subprocess_env;
1118 apr_off_t offset = strlen("config ") + strlen(STARTING_SEQUENCE);
1121 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 0, &offset))) {
1124 if (!strcmp(tag, "errmsg")) {
1125 parse_string(r, tag_val, error, MAX_STRING_LEN, 0);
1127 else if (!strcmp(tag, "timefmt")) {
1128 apr_time_t date = r->request_time;
1130 parse_string(r, tag_val, tf, MAX_STRING_LEN, 0);
1131 apr_table_setn(env, "DATE_LOCAL", ap_ht_time(r->pool, date, tf, 0));
1132 apr_table_setn(env, "DATE_GMT", ap_ht_time(r->pool, date, tf, 1));
1133 apr_table_setn(env, "LAST_MODIFIED",
1134 ap_ht_time(r->pool, r->finfo.mtime, tf, 0));
1136 else if (!strcmp(tag, "sizefmt")) {
1137 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1138 decodehtml(parsed_string);
1139 if (!strcmp(parsed_string, "bytes")) {
1140 *sizefmt = SIZEFMT_BYTES;
1142 else if (!strcmp(parsed_string, "abbrev")) {
1143 *sizefmt = SIZEFMT_KMG;
1146 else if (!strcmp(tag, "done")) {
1150 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1151 "unknown parameter \"%s\" to tag config in %s",
1159 static int find_file(request_rec *r, const char *directive, const char *tag,
1160 char *tag_val, apr_finfo_t *finfo, const char *error)
1162 char *to_send = tag_val;
1163 request_rec *rr = NULL;
1165 char *error_fmt = NULL;
1167 if (!strcmp(tag, "file")) {
1168 /* be safe; only files in this directory or below allowed */
1169 if (!is_only_below(tag_val)) {
1170 error_fmt = "unable to access file \"%s\" "
1171 "in parsed file %s";
1174 ap_getparents(tag_val); /* get rid of any nasties */
1175 rr = ap_sub_req_lookup_file(tag_val, r);
1177 if (rr->status == HTTP_OK && rr->finfo.protection != 0) {
1178 to_send = rr->filename;
1179 if (apr_stat(finfo, to_send, rr->pool) != APR_SUCCESS) {
1180 error_fmt = "unable to get information about \"%s\" "
1181 "in parsed file %s";
1185 error_fmt = "unable to lookup information about \"%s\" "
1186 "in parsed file %s";
1192 /* TODO: pass APLOG_NOERRNO if no apr_stat() failure; pass rv from apr_stat()
1195 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, error_fmt, to_send, r->filename);
1199 if (rr) ap_destroy_sub_req(rr);
1203 else if (!strcmp(tag, "virtual")) {
1204 rr = ap_sub_req_lookup_uri(tag_val, r);
1206 if (rr->status == HTTP_OK && rr->finfo.protection != 0) {
1207 memcpy((char *) finfo, (const char *) &rr->finfo,
1209 ap_destroy_sub_req(rr);
1213 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1214 "unable to get information about \"%s\" "
1215 "in parsed file %s",
1216 tag_val, r->filename);
1218 ap_destroy_sub_req(rr);
1223 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1224 "unknown parameter \"%s\" to tag %s in %s",
1225 tag, directive, r->filename);
1232 static int handle_fsize(ap_bucket *in, request_rec *r, const char *error, int sizefmt)
1234 char tag[MAX_STRING_LEN];
1237 char parsed_string[MAX_STRING_LEN];
1238 apr_off_t offset = strlen("fsize ") + strlen(STARTING_SEQUENCE);
1241 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
1244 else if (!strcmp(tag, "done")) {
1248 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1249 if (!find_file(r, "fsize", tag, parsed_string, &finfo, error)) {
1250 if (sizefmt == SIZEFMT_KMG) {
1251 ap_send_size(finfo.size, r);
1255 apr_snprintf(tag, sizeof(tag), "%" APR_OFF_T_FMT, finfo.size);
1256 l = strlen(tag); /* grrr */
1257 for (x = 0; x < l; x++) {
1258 if (x && (!((l - x) % 3))) {
1261 ap_rputc(tag[x], r);
1269 static int handle_flastmod(ap_bucket *in, request_rec *r, const char *error, const char *tf)
1271 char tag[MAX_STRING_LEN];
1274 char parsed_string[MAX_STRING_LEN];
1275 apr_off_t offset = strlen("flastmod ") + strlen(STARTING_SEQUENCE);
1278 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
1281 else if (!strcmp(tag, "done")) {
1285 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1286 if (!find_file(r, "flastmod", tag, parsed_string, &finfo, error)) {
1287 ap_rputs(ap_ht_time(r->pool, finfo.mtime, tf, 0), r);
1293 static int re_check(request_rec *r, char *string, char *rexp)
1298 compiled = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_NOSUB);
1299 if (compiled == NULL) {
1300 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1301 "unable to compile pattern \"%s\"", rexp);
1304 regex_error = ap_regexec(compiled, string, 0, (regmatch_t *) NULL, 0);
1305 ap_pregfree(r->pool, compiled);
1306 return (!regex_error);
1311 token_and, token_or, token_not, token_eq, token_ne,
1312 token_rbrace, token_lbrace, token_group,
1313 token_ge, token_le, token_gt, token_lt
1316 enum token_type type;
1317 char value[MAX_STRING_LEN];
1320 /* there is an implicit assumption here that string is at most MAX_STRING_LEN-1
1321 * characters long...
1323 static const char *get_ptoken(request_rec *r, const char *string, struct token *token)
1329 /* Skip leading white space */
1330 if (string == (char *) NULL) {
1331 return (char *) NULL;
1333 while ((ch = *string++)) {
1334 if (!apr_isspace(ch)) {
1339 return (char *) NULL;
1342 token->type = token_string; /* the default type */
1345 token->type = token_lbrace;
1348 token->type = token_rbrace;
1351 token->type = token_eq;
1354 if (*string == '=') {
1355 token->type = token_ne;
1356 return (string + 1);
1359 token->type = token_not;
1363 token->type = token_string;
1367 if (*string == '|') {
1368 token->type = token_or;
1369 return (string + 1);
1373 if (*string == '&') {
1374 token->type = token_and;
1375 return (string + 1);
1379 if (*string == '=') {
1380 token->type = token_ge;
1381 return (string + 1);
1384 token->type = token_gt;
1388 if (*string == '=') {
1389 token->type = token_le;
1390 return (string + 1);
1393 token->type = token_lt;
1397 token->type = token_string;
1400 /* We should only be here if we are in a string */
1402 token->value[next++] = ch;
1406 * Yes I know that goto's are BAD. But, c doesn't allow me to
1407 * exit a loop from a switch statement. Yes, I could use a flag,
1408 * but that is (IMHO) even less readable/maintainable than the goto.
1411 * I used the ++string throughout this section so that string
1412 * ends up pointing to the next token and I can just return it
1414 for (ch = *string; ch != '\0'; ch = *++string) {
1416 if ((ch = *++string) == '\0') {
1419 token->value[next++] = ch;
1423 if (apr_isspace(ch)) {
1436 if (*(string + 1) == '|') {
1441 if (*(string + 1) == '&') {
1450 token->value[next++] = ch;
1458 token->value[next++] = ch;
1462 /* If qs is still set, I have an unmatched ' */
1464 ap_rputs("\nUnmatched '\n", r);
1467 token->value[next] = '\0';
1473 * Hey I still know that goto's are BAD. I don't think that I've ever
1474 * used two in the same project, let alone the same file before. But,
1475 * I absolutely want to make sure that I clean up the memory in all
1476 * cases. And, without rewriting this completely, the easiest way
1477 * is to just branch to the return code which cleans it up.
1479 /* there is an implicit assumption here that expr is at most MAX_STRING_LEN-1
1480 * characters long...
1482 static int parse_expr(request_rec *r, const char *expr, const char *error)
1485 struct parse_node *left, *right, *parent;
1488 } *root, *current, *new;
1490 char buffer[MAX_STRING_LEN];
1491 apr_pool_t *expr_pool;
1494 if ((parse = expr) == (char *) NULL) {
1497 root = current = (struct parse_node *) NULL;
1498 if (apr_create_pool(&expr_pool, r->pool) != APR_SUCCESS)
1501 /* Create Parse Tree */
1503 new = (struct parse_node *) apr_palloc(expr_pool,
1504 sizeof(struct parse_node));
1505 new->parent = new->left = new->right = (struct parse_node *) NULL;
1507 if ((parse = get_ptoken(r, parse, &new->token)) == (char *) NULL) {
1510 switch (new->token.type) {
1513 #ifdef DEBUG_INCLUDE
1514 ap_rvputs(r, " Token: string (", new->token.value, ")\n", NULL);
1516 if (current == (struct parse_node *) NULL) {
1517 root = current = new;
1520 switch (current->token.type) {
1522 if (current->token.value[0] != '\0') {
1523 strncat(current->token.value, " ",
1524 sizeof(current->token.value)
1525 - strlen(current->token.value) - 1);
1527 strncat(current->token.value, new->token.value,
1528 sizeof(current->token.value)
1529 - strlen(current->token.value) - 1);
1530 current->token.value[sizeof(current->token.value) - 1] = '\0';
1542 new->parent = current;
1543 current = current->right = new;
1546 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1547 "Invalid expression \"%s\" in file %s",
1556 #ifdef DEBUG_INCLUDE
1557 ap_rputs(" Token: and/or\n", r);
1559 if (current == (struct parse_node *) NULL) {
1560 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1561 "Invalid expression \"%s\" in file %s",
1566 /* Percolate upwards */
1567 while (current != (struct parse_node *) NULL) {
1568 switch (current->token.type) {
1580 current = current->parent;
1585 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1586 "Invalid expression \"%s\" in file %s",
1593 if (current == (struct parse_node *) NULL) {
1595 new->left->parent = new;
1596 new->parent = (struct parse_node *) NULL;
1600 new->left = current->right;
1601 current->right = new;
1602 new->parent = current;
1608 #ifdef DEBUG_INCLUDE
1609 ap_rputs(" Token: not\n", r);
1611 if (current == (struct parse_node *) NULL) {
1612 root = current = new;
1615 /* Percolate upwards */
1616 while (current != (struct parse_node *) NULL) {
1617 switch (current->token.type) {
1630 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1631 "Invalid expression \"%s\" in file %s",
1638 if (current == (struct parse_node *) NULL) {
1640 new->left->parent = new;
1641 new->parent = (struct parse_node *) NULL;
1645 new->left = current->right;
1646 current->right = new;
1647 new->parent = current;
1658 #ifdef DEBUG_INCLUDE
1659 ap_rputs(" Token: eq/ne/ge/gt/le/lt\n", r);
1661 if (current == (struct parse_node *) NULL) {
1662 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1663 "Invalid expression \"%s\" in file %s",
1668 /* Percolate upwards */
1669 while (current != (struct parse_node *) NULL) {
1670 switch (current->token.type) {
1673 current = current->parent;
1687 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1688 "Invalid expression \"%s\" in file %s",
1695 if (current == (struct parse_node *) NULL) {
1697 new->left->parent = new;
1698 new->parent = (struct parse_node *) NULL;
1702 new->left = current->right;
1703 current->right = new;
1704 new->parent = current;
1710 #ifdef DEBUG_INCLUDE
1711 ap_rputs(" Token: rbrace\n", r);
1713 while (current != (struct parse_node *) NULL) {
1714 if (current->token.type == token_lbrace) {
1715 current->token.type = token_group;
1718 current = current->parent;
1720 if (current == (struct parse_node *) NULL) {
1721 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1722 "Unmatched ')' in \"%s\" in file %s",
1730 #ifdef DEBUG_INCLUDE
1731 ap_rputs(" Token: lbrace\n", r);
1733 if (current == (struct parse_node *) NULL) {
1734 root = current = new;
1737 /* Percolate upwards */
1738 while (current != (struct parse_node *) NULL) {
1739 switch (current->token.type) {
1754 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1755 "Invalid expression \"%s\" in file %s",
1762 if (current == (struct parse_node *) NULL) {
1764 new->left->parent = new;
1765 new->parent = (struct parse_node *) NULL;
1769 new->left = current->right;
1770 current->right = new;
1771 new->parent = current;
1780 /* Evaluate Parse Tree */
1782 while (current != (struct parse_node *) NULL) {
1783 switch (current->token.type) {
1785 #ifdef DEBUG_INCLUDE
1786 ap_rputs(" Evaluate string\n", r);
1788 parse_string(r, current->token.value, buffer, sizeof(buffer), 0);
1789 apr_cpystrn(current->token.value, buffer, sizeof(current->token.value));
1790 current->value = (current->token.value[0] != '\0');
1792 current = current->parent;
1797 #ifdef DEBUG_INCLUDE
1798 ap_rputs(" Evaluate and/or\n", r);
1800 if (current->left == (struct parse_node *) NULL ||
1801 current->right == (struct parse_node *) NULL) {
1802 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1803 "Invalid expression \"%s\" in file %s",
1808 if (!current->left->done) {
1809 switch (current->left->token.type) {
1811 parse_string(r, current->left->token.value,
1812 buffer, sizeof(buffer), 0);
1813 apr_cpystrn(current->left->token.value, buffer,
1814 sizeof(current->left->token.value));
1815 current->left->value = (current->left->token.value[0] != '\0');
1816 current->left->done = 1;
1819 current = current->left;
1823 if (!current->right->done) {
1824 switch (current->right->token.type) {
1826 parse_string(r, current->right->token.value,
1827 buffer, sizeof(buffer), 0);
1828 apr_cpystrn(current->right->token.value, buffer,
1829 sizeof(current->right->token.value));
1830 current->right->value = (current->right->token.value[0] != '\0');
1831 current->right->done = 1;
1834 current = current->right;
1838 #ifdef DEBUG_INCLUDE
1839 ap_rvputs(r, " Left: ", current->left->value ? "1" : "0",
1841 ap_rvputs(r, " Right: ", current->right->value ? "1" : "0",
1844 if (current->token.type == token_and) {
1845 current->value = current->left->value && current->right->value;
1848 current->value = current->left->value || current->right->value;
1850 #ifdef DEBUG_INCLUDE
1851 ap_rvputs(r, " Returning ", current->value ? "1" : "0",
1855 current = current->parent;
1860 #ifdef DEBUG_INCLUDE
1861 ap_rputs(" Evaluate eq/ne\n", r);
1863 if ((current->left == (struct parse_node *) NULL) ||
1864 (current->right == (struct parse_node *) NULL) ||
1865 (current->left->token.type != token_string) ||
1866 (current->right->token.type != token_string)) {
1867 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1868 "Invalid expression \"%s\" in file %s",
1873 parse_string(r, current->left->token.value,
1874 buffer, sizeof(buffer), 0);
1875 apr_cpystrn(current->left->token.value, buffer,
1876 sizeof(current->left->token.value));
1877 parse_string(r, current->right->token.value,
1878 buffer, sizeof(buffer), 0);
1879 apr_cpystrn(current->right->token.value, buffer,
1880 sizeof(current->right->token.value));
1881 if (current->right->token.value[0] == '/') {
1883 len = strlen(current->right->token.value);
1884 if (current->right->token.value[len - 1] == '/') {
1885 current->right->token.value[len - 1] = '\0';
1888 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1889 "Invalid rexp \"%s\" in file %s",
1890 current->right->token.value, r->filename);
1894 #ifdef DEBUG_INCLUDE
1895 ap_rvputs(r, " Re Compare (", current->left->token.value,
1896 ") with /", ¤t->right->token.value[1], "/\n", NULL);
1899 re_check(r, current->left->token.value,
1900 ¤t->right->token.value[1]);
1903 #ifdef DEBUG_INCLUDE
1904 ap_rvputs(r, " Compare (", current->left->token.value,
1905 ") with (", current->right->token.value, ")\n", NULL);
1908 (strcmp(current->left->token.value,
1909 current->right->token.value) == 0);
1911 if (current->token.type == token_ne) {
1912 current->value = !current->value;
1914 #ifdef DEBUG_INCLUDE
1915 ap_rvputs(r, " Returning ", current->value ? "1" : "0",
1919 current = current->parent;
1925 #ifdef DEBUG_INCLUDE
1926 ap_rputs(" Evaluate ge/gt/le/lt\n", r);
1928 if ((current->left == (struct parse_node *) NULL) ||
1929 (current->right == (struct parse_node *) NULL) ||
1930 (current->left->token.type != token_string) ||
1931 (current->right->token.type != token_string)) {
1932 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1933 "Invalid expression \"%s\" in file %s",
1938 parse_string(r, current->left->token.value,
1939 buffer, sizeof(buffer), 0);
1940 apr_cpystrn(current->left->token.value, buffer,
1941 sizeof(current->left->token.value));
1942 parse_string(r, current->right->token.value,
1943 buffer, sizeof(buffer), 0);
1944 apr_cpystrn(current->right->token.value, buffer,
1945 sizeof(current->right->token.value));
1946 #ifdef DEBUG_INCLUDE
1947 ap_rvputs(r, " Compare (", current->left->token.value,
1948 ") with (", current->right->token.value, ")\n", NULL);
1951 strcmp(current->left->token.value,
1952 current->right->token.value);
1953 if (current->token.type == token_ge) {
1954 current->value = current->value >= 0;
1956 else if (current->token.type == token_gt) {
1957 current->value = current->value > 0;
1959 else if (current->token.type == token_le) {
1960 current->value = current->value <= 0;
1962 else if (current->token.type == token_lt) {
1963 current->value = current->value < 0;
1966 current->value = 0; /* Don't return -1 if unknown token */
1968 #ifdef DEBUG_INCLUDE
1969 ap_rvputs(r, " Returning ", current->value ? "1" : "0",
1973 current = current->parent;
1977 if (current->right != (struct parse_node *) NULL) {
1978 if (!current->right->done) {
1979 current = current->right;
1982 current->value = !current->right->value;
1987 #ifdef DEBUG_INCLUDE
1988 ap_rvputs(r, " Evaluate !: ", current->value ? "1" : "0",
1992 current = current->parent;
1996 if (current->right != (struct parse_node *) NULL) {
1997 if (!current->right->done) {
1998 current = current->right;
2001 current->value = current->right->value;
2006 #ifdef DEBUG_INCLUDE
2007 ap_rvputs(r, " Evaluate (): ", current->value ? "1" : "0",
2011 current = current->parent;
2015 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2016 "Unmatched '(' in \"%s\" in file %s",
2022 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2023 "Unmatched ')' in \"%s\" in file %s",
2029 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2036 retval = (root == (struct parse_node *) NULL) ? 0 : root->value;
2038 apr_destroy_pool(expr_pool);
2042 static int handle_if(ap_bucket *in, request_rec *r, const char *error,
2043 int *conditional_status, int *printing)
2045 char tag[MAX_STRING_LEN];
2048 apr_off_t offset = strlen("if ") + strlen(STARTING_SEQUENCE);
2052 tag_val = get_tag(r->pool, in, tag, sizeof(tag), 0, &offset);
2056 else if (!strcmp(tag, "done")) {
2058 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2059 "missing expr in if statement: %s",
2064 *printing = *conditional_status = parse_expr(r, expr, error);
2065 #ifdef DEBUG_INCLUDE
2066 ap_rvputs(r, "**** if conditional_status=\"",
2067 *conditional_status ? "1" : "0", "\"\n", NULL);
2071 else if (!strcmp(tag, "expr")) {
2073 #ifdef DEBUG_INCLUDE
2074 ap_rvputs(r, "**** if expr=\"", expr, "\"\n", NULL);
2078 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2079 "unknown parameter \"%s\" to tag if in %s",
2086 static int handle_elif(ap_bucket *in, request_rec *r, const char *error,
2087 int *conditional_status, int *printing)
2089 char tag[MAX_STRING_LEN];
2092 apr_off_t offset = strlen("elif ") + strlen(STARTING_SEQUENCE);
2096 tag_val = get_tag(r->pool, in, tag, sizeof(tag), 0, &offset);
2100 else if (!strcmp(tag, "done")) {
2101 #ifdef DEBUG_INCLUDE
2102 ap_rvputs(r, "**** elif conditional_status=\"",
2103 *conditional_status ? "1" : "0", "\"\n", NULL);
2105 if (*conditional_status) {
2110 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2111 "missing expr in elif statement: %s",
2116 *printing = *conditional_status = parse_expr(r, expr, error);
2117 #ifdef DEBUG_INCLUDE
2118 ap_rvputs(r, "**** elif conditional_status=\"",
2119 *conditional_status ? "1" : "0", "\"\n", NULL);
2123 else if (!strcmp(tag, "expr")) {
2125 #ifdef DEBUG_INCLUDE
2126 ap_rvputs(r, "**** if expr=\"", expr, "\"\n", NULL);
2130 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2131 "unknown parameter \"%s\" to tag if in %s",
2138 static int handle_else(ap_bucket *in, request_rec *r, const char *error,
2139 int *conditional_status, int *printing)
2141 char tag[MAX_STRING_LEN];
2142 apr_off_t offset = strlen("else ") + strlen(STARTING_SEQUENCE);
2144 if (!get_tag(r->pool, in, tag, sizeof(tag), 1, &offset)) {
2147 else if (!strcmp(tag, "done")) {
2148 #ifdef DEBUG_INCLUDE
2149 ap_rvputs(r, "**** else conditional_status=\"",
2150 *conditional_status ? "1" : "0", "\"\n", NULL);
2152 *printing = !(*conditional_status);
2153 *conditional_status = 1;
2157 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2158 "else directive does not take tags in %s",
2167 static int handle_endif(ap_bucket *in, request_rec *r, const char *error,
2168 int *conditional_status, int *printing)
2170 char tag[MAX_STRING_LEN];
2171 apr_off_t offset = strlen("endif ") + strlen(STARTING_SEQUENCE);
2173 if (!get_tag(r->pool, in, tag, sizeof(tag), 1, &offset)) {
2176 else if (!strcmp(tag, "done")) {
2177 #ifdef DEBUG_INCLUDE
2178 ap_rvputs(r, "**** endif conditional_status=\"",
2179 *conditional_status ? "1" : "0", "\"\n", NULL);
2182 *conditional_status = 1;
2186 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2187 "endif directive does not take tags in %s",
2194 static int handle_set(ap_bucket *in, request_rec *r, const char *error)
2196 char tag[MAX_STRING_LEN];
2197 char parsed_string[MAX_STRING_LEN];
2200 apr_off_t offset = strlen("set ") + strlen(STARTING_SEQUENCE);
2202 var = (char *) NULL;
2204 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
2207 else if (!strcmp(tag, "done")) {
2210 else if (!strcmp(tag, "var")) {
2213 else if (!strcmp(tag, "value")) {
2214 if (var == (char *) NULL) {
2215 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2216 "variable must precede value in set directive in %s",
2221 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
2222 apr_table_setn(r->subprocess_env, var, apr_pstrdup(r->pool, parsed_string));
2225 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2226 "Invalid tag for set directive in %s", r->filename);
2233 static int handle_printenv(ap_bucket *in, request_rec *r, const char *error)
2235 char tag[MAX_STRING_LEN];
2237 apr_array_header_t *arr = apr_table_elts(r->subprocess_env);
2238 apr_table_entry_t *elts = (apr_table_entry_t *)arr->elts;
2240 apr_off_t offset = strlen("printenv ") + strlen(STARTING_SEQUENCE);
2242 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1, &offset))) {
2245 else if (!strcmp(tag, "done")) {
2246 for (i = 0; i < arr->nelts; ++i) {
2247 ap_rvputs(r, ap_escape_html(r->pool, elts[i].key), "=",
2248 ap_escape_html(r->pool, elts[i].val), "\n", NULL);
2253 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2254 "printenv directive does not take tags in %s",
2263 /* -------------------------- The main function --------------------------- */
2265 /* This is a stub which parses a file descriptor. */
2267 static void send_parsed_content(ap_bucket_brigade **bb, request_rec *r,
2270 char directive[MAX_STRING_LEN], error[MAX_STRING_LEN];
2271 char timefmt[MAX_STRING_LEN];
2272 int noexec = ap_allow_options(r) & OPT_INCNOEXEC;
2276 int conditional_status;
2277 ap_bucket *dptr = AP_BRIGADE_FIRST(*bb);
2278 ap_bucket *tagbuck, *dptr2;
2280 ap_bucket_brigade *tag_and_after;
2283 apr_cpystrn(error, DEFAULT_ERROR_MSG, sizeof(error));
2284 apr_cpystrn(timefmt, DEFAULT_TIME_FORMAT, sizeof(timefmt));
2285 sizefmt = SIZEFMT_KMG;
2287 /* Turn printing on */
2288 printing = conditional_status = 1;
2291 ap_chdir_file(r->filename);
2292 if (r->args) { /* add QUERY stuff to env cause it ain't yet */
2293 char *arg_copy = apr_pstrdup(r->pool, r->args);
2295 apr_table_setn(r->subprocess_env, "QUERY_STRING", r->args);
2296 ap_unescape_url(arg_copy);
2297 apr_table_setn(r->subprocess_env, "QUERY_STRING_UNESCAPED",
2298 ap_escape_shell_cmd(r->pool, arg_copy));
2301 AP_BRIGADE_FOREACH(dptr, *bb) {
2302 if ((tagbuck = find_string(dptr, STARTING_SEQUENCE, AP_BRIGADE_LAST(*bb))) != NULL) {
2305 endsec = find_string(dptr2, ENDING_SEQUENCE, AP_BRIGADE_LAST(*bb));
2306 if (endsec == NULL) {
2307 /** XXX No ending tag, needs to become an error bucket
2308 ** Tag could come in the next brigade (unless we've
2309 ** received eos in this brigade).
2311 ** We're about to segfault.
2315 /* At this point, everything between tagbuck and endsec is an SSI
2316 * directive, we just have to deal with it now.
2318 if (get_directive(tagbuck, directive, sizeof(directive), r->pool)) {
2319 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2320 "mod_include: error reading directive in %s",
2325 tag_and_after = ap_brigade_split(*bb, dptr);
2326 ap_pass_brigade(f->next, *bb); /* process what came before the tag */
2327 *bb = tag_and_after;
2328 if (!strcmp(directive, "if")) {
2333 ret = handle_if(tagbuck, r, error, &conditional_status,
2339 else if (!strcmp(directive, "else")) {
2341 ret = handle_else(tagbuck, r, error, &conditional_status,
2346 else if (!strcmp(directive, "elif")) {
2348 ret = handle_elif(tagbuck, r, error, &conditional_status,
2353 else if (!strcmp(directive, "endif")) {
2355 ret = handle_endif(tagbuck, r, error, &conditional_status,
2366 if (!strcmp(directive, "exec")) {
2368 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2369 "exec used but not allowed in %s",
2376 ret = handle_exec(tagbuck, r, error, f->next);
2379 else if (!strcmp(directive, "config")) {
2380 ret = handle_config(tagbuck, r, error, timefmt, &sizefmt);
2382 else if (!strcmp(directive, "set")) {
2383 ret = handle_set(tagbuck, r, error);
2385 else if (!strcmp(directive, "include")) {
2386 ret = handle_include(tagbuck, r, f->next, error, noexec);
2388 else if (!strcmp(directive, "echo")) {
2389 ret = handle_echo(tagbuck, r, error);
2391 else if (!strcmp(directive, "fsize")) {
2392 ret = handle_fsize(tagbuck, r, error, sizefmt);
2394 else if (!strcmp(directive, "flastmod")) {
2395 ret = handle_flastmod(tagbuck, r, error, timefmt);
2397 else if (!strcmp(directive, "printenv")) {
2398 ret = handle_printenv(tagbuck, r, error);
2401 else if (!strcmp(directive, "perl")) {
2402 ret = handle_perl(tagbuck, r, error);
2406 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2407 "unknown directive \"%s\" "
2409 directive, r->filename);
2414 /* AP_BRIGADE_UNSPLICE(dptr, AP_BUCKET_PREV(endsec)); */
2415 dptr = AP_BUCKET_PREV(endsec);
2423 /*****************************************************************
2425 * XBITHACK. Sigh... NB it's configurable per-directory; the compile-time
2426 * option only changes the default.
2429 module includes_module;
2431 xbithack_off, xbithack_on, xbithack_full
2435 #define DEFAULT_XBITHACK xbithack_full
2437 #define DEFAULT_XBITHACK xbithack_off
2440 static void *create_includes_dir_config(apr_pool_t *p, char *dummy)
2442 enum xbithack *result = (enum xbithack *) apr_palloc(p, sizeof(enum xbithack));
2443 *result = DEFAULT_XBITHACK;
2447 static const char *set_xbithack(cmd_parms *cmd, void *xbp, const char *arg)
2449 enum xbithack *state = (enum xbithack *) xbp;
2451 if (!strcasecmp(arg, "off")) {
2452 *state = xbithack_off;
2454 else if (!strcasecmp(arg, "on")) {
2455 *state = xbithack_on;
2457 else if (!strcasecmp(arg, "full")) {
2458 *state = xbithack_full;
2461 return "XBitHack must be set to Off, On, or Full";
2467 static int includes_filter(ap_filter_t *f, ap_bucket_brigade *b)
2469 request_rec *r = f->r;
2470 enum xbithack *state =
2471 (enum xbithack *) ap_get_module_config(r->per_dir_config, &includes_module);
2472 request_rec *parent;
2474 if (!(ap_allow_options(r) & OPT_INCLUDES)) {
2475 return ap_pass_brigade(f->next, b);
2477 r->allowed |= (1 << M_GET);
2478 if (r->method_number != M_GET) {
2479 return ap_pass_brigade(f->next, b);
2482 if ((*state == xbithack_full)
2483 #if !defined(OS2) && !defined(WIN32)
2484 /* OS/2 dosen't support Groups. */
2485 && (r->finfo.protection & APR_GEXECUTE)
2488 ap_update_mtime(r, r->finfo.mtime);
2489 ap_set_last_modified(r);
2492 if ((parent = ap_get_module_config(r->request_config, &includes_module))) {
2493 /* Kludge --- for nested includes, we want to keep the subprocess
2494 * environment of the base document (for compatibility); that means
2495 * torquing our own last_modified date as well so that the
2496 * LAST_MODIFIED variable gets reset to the proper value if the
2497 * nested document resets <!--#config timefmt-->.
2498 * We also insist that the memory for this subrequest not be
2499 * destroyed, that's dealt with in handle_include().
2501 r->subprocess_env = parent->subprocess_env;
2502 apr_pool_join(parent->pool, r->pool);
2503 r->finfo.mtime = parent->finfo.mtime;
2506 /* we're not a nested include, so we create an initial
2508 ap_add_common_vars(r);
2510 add_include_vars(r, DEFAULT_TIME_FORMAT);
2512 /* XXX: this is bogus, at some point we're going to do a subrequest,
2513 * and when we do it we're going to be subjecting code that doesn't
2514 * expect to be signal-ready to SIGALRM. There is no clean way to
2515 * fix this, except to put alarm support into BUFF. -djg
2518 send_parsed_content(&b, r, f);
2519 ap_pass_brigade(f->next, b);
2522 /* signify that the sub request should not be killed */
2523 ap_set_module_config(r->request_config, &includes_module,
2524 NESTED_INCLUDE_MAGIC);
2530 static const command_rec includes_cmds[] =
2532 AP_INIT_TAKE1("XBitHack", set_xbithack, NULL, OR_OPTIONS,
2533 "Off, On, or Full"),
2537 static void register_hooks(void)
2539 ap_register_output_filter("INCLUDES", includes_filter, AP_FTYPE_CONTENT);
2542 module MODULE_VAR_EXPORT includes_module =
2544 STANDARD20_MODULE_STUFF,
2545 create_includes_dir_config, /* dir config creater */
2546 NULL, /* dir merger --- default is to override */
2547 NULL, /* server config */
2548 NULL, /* merge server config */
2549 includes_cmds, /* command apr_table_t */
2551 includes_handlers, /* handlers */
2553 NULL, /* handlers */
2555 register_hooks /* register hooks */