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" -->
86 #include "modules/perl/mod_perl.h"
88 #include "ap_config.h"
90 #include "http_config.h"
91 #include "http_request.h"
92 #include "http_core.h"
93 #include "http_protocol.h"
95 #include "http_main.h"
96 #include "util_script.h"
103 #define STARTING_SEQUENCE "<!--#"
104 #define ENDING_SEQUENCE "-->"
105 #define DEFAULT_ERROR_MSG "[an error occurred while processing this directive]"
106 #define DEFAULT_TIME_FORMAT "%A, %d-%b-%Y %H:%M:%S %Z"
107 #define SIZEFMT_BYTES 0
108 #define SIZEFMT_KMG 1
109 #ifdef CHARSET_EBCDIC
110 #define RAW_ASCII_CHAR(ch) os_toebcdic[(unsigned char)ch]
111 #else /*CHARSET_EBCDIC*/
112 #define RAW_ASCII_CHAR(ch) (ch)
113 #endif /*CHARSET_EBCDIC*/
115 module MODULE_VAR_EXPORT includes_module;
117 /* just need some arbitrary non-NULL pointer which can't also be a request_rec */
118 #define NESTED_INCLUDE_MAGIC (&includes_module)
120 /* TODO: changing directory should be handled by CreateProcess */
121 #define ap_chdir_file(x) do {} while(0)
123 /* ------------------------ Environment function -------------------------- */
125 /* XXX: could use ap_table_overlap here */
126 static void add_include_vars(request_rec *r, char *timefmt)
130 #endif /* ndef WIN32 */
131 ap_table_t *e = r->subprocess_env;
133 ap_time_t date = r->request_time;
135 ap_table_setn(e, "DATE_LOCAL", ap_ht_time(r->pool, date, timefmt, 0));
136 ap_table_setn(e, "DATE_GMT", ap_ht_time(r->pool, date, timefmt, 1));
137 ap_table_setn(e, "LAST_MODIFIED",
138 ap_ht_time(r->pool, r->finfo.mtime, timefmt, 0));
139 ap_table_setn(e, "DOCUMENT_URI", r->uri);
140 ap_table_setn(e, "DOCUMENT_PATH_INFO", r->path_info);
142 pw = getpwuid(r->finfo.user);
144 ap_table_setn(e, "USER_NAME", ap_pstrdup(r->pool, pw->pw_name));
147 ap_table_setn(e, "USER_NAME", ap_psprintf(r->pool, "user#%lu",
148 (unsigned long) r->finfo.user));
150 #endif /* ndef WIN32 */
152 if ((t = strrchr(r->filename, '/'))) {
153 ap_table_setn(e, "DOCUMENT_NAME", ++t);
156 ap_table_setn(e, "DOCUMENT_NAME", r->uri);
159 char *arg_copy = ap_pstrdup(r->pool, r->args);
161 ap_unescape_url(arg_copy);
162 ap_table_setn(e, "QUERY_STRING_UNESCAPED",
163 ap_escape_shell_cmd(r->pool, arg_copy));
169 /* --------------------------- Parser functions --------------------------- */
171 #define OUTBUFSIZE 4096
172 /* PUT_CHAR and FLUSH_BUF currently only work within the scope of
173 * find_string(); they are hacks to avoid calling rputc for each and
174 * every character output. A common set of buffering calls for this
175 * type of output SHOULD be implemented.
177 #define PUT_CHAR(c,r) \
179 outbuf[outind++] = c; \
180 if (outind == OUTBUFSIZE) { \
185 /* there SHOULD be some error checking on the return value of
186 * rwrite, however it is unclear what the API for rwrite returning
187 * errors is and little can really be done to help the error in
190 #define FLUSH_BUF(r) \
192 ap_rwrite(outbuf, outind, r); \
197 * f: file handle being read from
198 * c: character to read into
199 * ret: return value to use if input fails
200 * r: current request_rec
202 * This macro is redefined after find_string() for historical reasons
203 * to avoid too many code changes. This is one of the many things
204 * that should be fixed.
206 #define GET_CHAR(f,c,ret,r) \
208 ap_status_t status = ap_getc(&c, f); \
209 if (status != APR_SUCCESS) { /* either EOF or error -- needs error handling if latter */ \
210 if (status != APR_EOF) { \
211 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, \
212 "encountered error in GET_CHAR macro, " \
221 static int find_string(ap_file_t *in, const char *str, request_rec *r, int printing)
223 int x, l = strlen(str), p;
224 char outbuf[OUTBUFSIZE];
230 GET_CHAR(in, c, 1, r);
239 for (x = 0; x < p; x++) {
252 #define GET_CHAR(f,c,r,p) \
254 ap_status_t status = ap_getc(&c, f); \
255 if (status != APR_SUCCESS) { /* either EOF or error -- needs error handling if latter */ \
256 if (status != APR_EOF) { \
257 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, \
258 "encountered error in GET_CHAR macro, " \
267 * decodes a string containing html entities or numeric character references.
268 * 's' is overwritten with the decoded string.
269 * If 's' is syntatically incorrect, then the followed fixups will be made:
270 * unknown entities will be left undecoded;
271 * references to unused numeric characters will be deleted.
272 * In particular, � will not be decoded, but will be deleted.
277 /* maximum length of any ISO-LATIN-1 HTML entity name. */
278 #define MAXENTLEN (6)
280 /* The following is a shrinking transformation, therefore safe. */
282 static void decodehtml(char *s)
287 static const char * const entlist[MAXENTLEN + 1] =
291 "lt\074gt\076", /* 2 */
292 "amp\046ETH\320eth\360", /* 3 */
293 "quot\042Auml\304Euml\313Iuml\317Ouml\326Uuml\334auml\344euml\353\
294 iuml\357ouml\366uuml\374yuml\377", /* 4 */
295 "Acirc\302Aring\305AElig\306Ecirc\312Icirc\316Ocirc\324Ucirc\333\
296 THORN\336szlig\337acirc\342aring\345aelig\346ecirc\352icirc\356ocirc\364\
297 ucirc\373thorn\376", /* 5 */
298 "Agrave\300Aacute\301Atilde\303Ccedil\307Egrave\310Eacute\311\
299 Igrave\314Iacute\315Ntilde\321Ograve\322Oacute\323Otilde\325Oslash\330\
300 Ugrave\331Uacute\332Yacute\335agrave\340aacute\341atilde\343ccedil\347\
301 egrave\350eacute\351igrave\354iacute\355ntilde\361ograve\362oacute\363\
302 otilde\365oslash\370ugrave\371uacute\372yacute\375" /* 6 */
305 for (; *s != '\0'; s++, p++) {
310 /* find end of entity */
311 for (i = 1; s[i] != ';' && s[i] != '\0'; i++) {
315 if (s[i] == '\0') { /* treat as normal data */
320 /* is it numeric ? */
322 for (j = 2, val = 0; j < i && ap_isdigit(s[j]); j++) {
323 val = val * 10 + s[j] - '0';
326 if (j < i || val <= 8 || (val >= 11 && val <= 31) ||
327 (val >= 127 && val <= 160) || val >= 256) {
328 p--; /* no data to output */
331 *p = RAW_ASCII_CHAR(val);
336 if (j > MAXENTLEN || entlist[j] == NULL) {
339 continue; /* skip it */
341 for (ents = entlist[j]; *ents != '\0'; ents += i) {
342 if (strncmp(s + 1, ents, j) == 0) {
348 *p = '&'; /* unknown */
351 *p = RAW_ASCII_CHAR(((const unsigned char *) ents)[j]);
361 * extract the next tag name and value.
362 * if there are no more tags, set the tag name to 'done'
363 * the tag value is html decoded if dodecode is non-zero
366 static char *get_tag(ap_pool_t *p, ap_file_t *in, char *tag, int tagbuf_len, int dodecode)
368 char *t = tag, *tag_val, c, term;
370 /* makes code below a little less cluttered */
373 do { /* skip whitespace */
374 GET_CHAR(in, c, NULL, p);
375 } while (ap_isspace(c));
377 /* tags can't start with - */
379 GET_CHAR(in, c, NULL, p);
382 GET_CHAR(in, c, NULL, p);
383 } while (ap_isspace(c));
385 ap_cpystrn(tag, "done", tagbuf_len);
389 return NULL; /* failed */
392 /* find end of tag name */
394 if (t - tag == tagbuf_len) {
398 if (c == '=' || ap_isspace(c)) {
401 *(t++) = ap_tolower(c);
402 GET_CHAR(in, c, NULL, p);
408 while (ap_isspace(c)) {
409 GET_CHAR(in, c, NULL, p); /* space before = */
417 GET_CHAR(in, c, NULL, p); /* space after = */
418 } while (ap_isspace(c));
420 /* we should allow a 'name' as a value */
422 if (c != '"' && c != '\'') {
427 GET_CHAR(in, c, NULL, p);
428 if (t - tag == tagbuf_len) {
432 /* Want to accept \" as a valid character within a string. */
434 *(t++) = c; /* Add backslash */
435 GET_CHAR(in, c, NULL, p);
436 if (c == term) { /* Only if */
437 *(--t) = c; /* Replace backslash ONLY for terminator */
440 else if (c == term) {
449 return ap_pstrdup(p, tag_val);
452 static int get_directive(ap_file_t *in, char *dest, size_t len, ap_pool_t *p)
457 /* make room for nul terminator */
460 /* skip initial whitespace */
462 GET_CHAR(in, c, 1, p);
463 if (!ap_isspace(c)) {
467 /* now get directive */
469 if (d - dest == len) {
472 *d++ = ap_tolower(c);
473 GET_CHAR(in, c, 1, p);
483 * Do variable substitution on strings
485 static void parse_string(request_rec *r, const char *in, char *out,
486 size_t length, int leave_name)
492 /* leave room for nul terminator */
493 end_out = out + length - 1;
495 while ((ch = *in++) != '\0') {
498 if (next == end_out) {
512 char var[MAX_STRING_LEN];
513 const char *start_of_var_name;
514 const char *end_of_var_name; /* end of var name + 1 */
515 const char *expansion;
519 /* guess that the expansion won't happen */
523 start_of_var_name = in;
524 in = strchr(in, '}');
526 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
527 0, r, "Missing '}' on variable \"%s\"",
532 end_of_var_name = in;
536 start_of_var_name = in;
537 while (ap_isalnum(*in) || *in == '_') {
540 end_of_var_name = in;
542 /* what a pain, too bad there's no table_getn where you can
543 * pass a non-nul terminated string */
544 l = end_of_var_name - start_of_var_name;
546 l = (l > sizeof(var) - 1) ? (sizeof(var) - 1) : l;
547 memcpy(var, start_of_var_name, l);
550 val = ap_table_get(r->subprocess_env, var);
553 l = strlen(expansion);
555 else if (leave_name) {
559 break; /* no expansion to be done */
563 /* zero-length variable name causes just the $ to be copied */
566 l = (l > end_out - next) ? (end_out - next) : l;
567 memcpy(next, expansion, l);
572 if (next == end_out) {
585 /* --------------------------- Action handlers ---------------------------- */
587 static int include_cgi(char *s, request_rec *r)
589 request_rec *rr = ap_sub_req_lookup_uri(s, r);
592 if (rr->status != HTTP_OK) {
596 /* No hardwired path info or query allowed */
598 if ((rr->path_info && rr->path_info[0]) || rr->args) {
601 if (rr->finfo.protection == 0) {
605 /* Script gets parameters of the *document*, for back compatibility */
607 rr->path_info = r->path_info; /* hard to get right; see mod_cgi.c */
610 /* Force sub_req to be treated as a CGI request, even if ordinary
611 * typing rules would have called it something else.
614 rr->content_type = CGI_MAGIC_TYPE;
618 rr_status = ap_run_sub_req(rr);
619 if (ap_is_HTTP_REDIRECT(rr_status)) {
620 const char *location = ap_table_get(rr->headers_out, "Location");
621 location = ap_escape_html(rr->pool, location);
622 ap_rvputs(r, "<A HREF=\"", location, "\">", location, "</A>", NULL);
625 ap_destroy_sub_req(rr);
626 ap_chdir_file(r->filename);
631 /* ensure that path is relative, and does not contain ".." elements
632 * ensentially ensure that it does not match the regex:
633 * (^/|(^|/)\.\.(/|$))
634 * XXX: this needs os abstraction... consider c:..\foo in win32
636 static int is_only_below(const char *path)
638 #ifdef HAVE_DRIVE_LETTERS
642 if (path[0] == '/') {
645 if (path[0] == '.' && path[1] == '.'
646 && (path[2] == '\0' || path[2] == '/')) {
650 if (*path == '/' && path[1] == '.' && path[2] == '.'
651 && (path[3] == '\0' || path[3] == '/')) {
659 static int handle_include(ap_file_t *in, request_rec *r, const char *error, int noexec)
661 char tag[MAX_STRING_LEN];
662 char parsed_string[MAX_STRING_LEN];
666 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
669 if (!strcmp(tag, "file") || !strcmp(tag, "virtual")) {
670 request_rec *rr = NULL;
671 char *error_fmt = NULL;
673 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
675 /* be safe; only files in this directory or below allowed */
676 if (!is_only_below(parsed_string)) {
677 error_fmt = "unable to include file \"%s\" "
681 rr = ap_sub_req_lookup_file(parsed_string, r);
685 rr = ap_sub_req_lookup_uri(parsed_string, r);
688 if (!error_fmt && rr->status != HTTP_OK) {
689 error_fmt = "unable to include \"%s\" in parsed file %s";
692 if (!error_fmt && noexec && rr->content_type
693 && (strncmp(rr->content_type, "text/", 5))) {
694 error_fmt = "unable to include potential exec \"%s\" "
697 if (error_fmt == NULL) {
698 /* try to avoid recursive includes. We do this by walking
699 * up the r->main list of subrequests, and at each level
700 * walking back through any internal redirects. At each
701 * step, we compare the filenames and the URIs.
703 * The filename comparison catches a recursive include
704 * with an ever-changing URL, eg.
705 * <!--#include virtual=
706 * "$REQUEST_URI/$QUERY_STRING?$QUERY_STRING/x"-->
707 * which, although they would eventually be caught because
708 * we have a limit on the length of files, etc., can
709 * recurse for a while.
711 * The URI comparison catches the case where the filename
712 * is changed while processing the request, so the
713 * current name is never the same as any previous one.
714 * This can happen with "DocumentRoot /foo" when you
715 * request "/" on the server and it includes "/".
716 * This only applies to modules such as mod_dir that
717 * (somewhat improperly) mess with r->filename outside
718 * of a filename translation phase.
722 for (p = r; p != NULL && !founddupe; p = p->main) {
724 for (q = p; q != NULL; q = q->prev) {
725 if ( (strcmp(q->filename, rr->filename) == 0) ||
726 (strcmp(q->uri, rr->uri) == 0) ){
734 error_fmt = "Recursive include of \"%s\" "
739 /* see the Kludge in send_parsed_file for why */
741 ap_set_module_config(rr->request_config, &includes_module, r);
743 if (!error_fmt && ap_run_sub_req(rr)) {
744 error_fmt = "unable to include \"%s\" in parsed file %s";
746 ap_chdir_file(r->filename);
748 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
749 0, r, error_fmt, tag_val, r->filename);
753 /* destroy the sub request if it's not a nested include */
755 && ap_get_module_config(rr->request_config, &includes_module)
756 != NESTED_INCLUDE_MAGIC) {
757 ap_destroy_sub_req(rr);
760 else if (!strcmp(tag, "done")) {
764 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
765 "unknown parameter \"%s\" to tag include in %s",
782 static ap_status_t build_argv_list(char ***argv, request_rec *r, ap_pool_t *p)
784 int numwords, x, idx;
786 const char *args = r->args;
788 if (!args || !args[0] || strchr(args, '=')) {
792 /* count the number of keywords */
793 for (x = 0, numwords = 1; args[x]; x++) {
794 if (args[x] == '+') {
799 /* Everything is - 1 to account for the first parameter which is the
800 * program name. We didn't used to have to do this, but APR wants it.
802 if (numwords > APACHE_ARG_MAX - 1) {
803 numwords = APACHE_ARG_MAX - 1; /* Truncate args to prevent overrun */
805 *argv = (char **) ap_palloc(p, (numwords + 2) * sizeof(char *));
807 for (x = 1, idx = 1; x < numwords; x++) {
808 w = ap_getword_nulls(p, &args, '+');
810 (*argv)[idx++] = ap_escape_shell_cmd(p, w);
819 static int include_cmd(char *s, request_rec *r)
823 ap_procattr_t *procattr;
826 ap_table_t *env = r->subprocess_env;
828 ap_file_t *file = NULL;
834 arg.t.filename = r->filename;
835 arg.t.subprocess_env = r->subprocess_env;
836 arg.t.prog_type = FORK_FILE;
839 if (r->path_info && r->path_info[0] != '\0') {
842 ap_table_setn(env, "PATH_INFO", ap_escape_shell_cmd(r->pool, r->path_info));
844 pa_req = ap_sub_req_lookup_uri(ap_escape_uri(r->pool, r->path_info), r);
845 if (pa_req->filename) {
846 ap_table_setn(env, "PATH_TRANSLATED",
847 ap_pstrcat(r->pool, pa_req->filename, pa_req->path_info,
853 char *arg_copy = ap_pstrdup(r->pool, r->args);
855 ap_table_setn(env, "QUERY_STRING", r->args);
856 ap_unescape_url(arg_copy);
857 ap_table_setn(env, "QUERY_STRING_UNESCAPED",
858 ap_escape_shell_cmd(r->pool, arg_copy));
861 if ((ap_createprocattr_init(&procattr, r->pool) != APR_SUCCESS) ||
862 (ap_setprocattr_io(procattr, APR_NO_PIPE,
863 APR_FULL_BLOCK, APR_NO_PIPE) != APR_SUCCESS) ||
864 (ap_setprocattr_dir(procattr, ap_make_dirstr_parent(r->pool, r->filename)) != APR_SUCCESS) ||
865 (ap_setprocattr_cmdtype(procattr, APR_SHELLCMD) != APR_SUCCESS)) {
866 /* Something bad happened, tell the world. */
867 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
868 "couldn't initialize proc attributes: %s %s", r->filename, s);
872 build_argv_list(&argv, r, r->pool);
873 argv[0] = ap_pstrdup(r->pool, s);
874 rc = ap_create_process(&procnew, s, argv, ap_create_environment(r->pool, env), procattr, r->pool);
876 if (rc != APR_SUCCESS) {
877 /* Bad things happened. Everyone should have cleaned up. */
878 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
879 "couldn't create child process: %d: %s", rc, s);
882 ap_note_subprocess(r->pool, procnew, kill_after_timeout);
883 /* Fill in BUFF structure for parents pipe to child's stdout */
884 ap_get_childout(&file, procnew);
885 iol = ap_create_file_iol(file);
888 script_in = ap_bcreate(r->pool, B_RD);
889 ap_bpush_iol(script_in, iol);
890 ap_send_fb(script_in, r);
891 ap_bclose(script_in);
898 static int handle_exec(ap_file_t *in, request_rec *r, const char *error)
900 char tag[MAX_STRING_LEN];
902 char *file = r->filename;
903 char parsed_string[MAX_STRING_LEN];
906 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
909 if (!strcmp(tag, "cmd")) {
910 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 1);
911 if (include_cmd(parsed_string, r) == -1) {
912 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
913 "execution failure for parameter \"%s\" "
914 "to tag exec in file %s",
918 /* just in case some stooge changed directories */
919 ap_chdir_file(r->filename);
921 else if (!strcmp(tag, "cgi")) {
922 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
923 if (include_cgi(parsed_string, r) == -1) {
924 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
925 "invalid CGI ref \"%s\" in %s", tag_val, file);
928 ap_chdir_file(r->filename);
930 else if (!strcmp(tag, "done")) {
934 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
935 "unknown parameter \"%s\" to tag exec in %s",
943 static int handle_echo(ap_file_t *in, request_rec *r, const char *error)
945 char tag[MAX_STRING_LEN];
947 enum {E_NONE, E_URL, E_ENTITY} encode;
952 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
955 if (!strcmp(tag, "var")) {
956 const char *val = ap_table_get(r->subprocess_env, tag_val);
959 if (encode == E_NONE) {
962 else if (encode == E_URL) {
963 ap_rputs(ap_escape_uri(r->pool, val), r);
965 else if (encode == E_ENTITY) {
966 ap_rputs(ap_escape_html(r->pool, val), r);
970 ap_rputs("(none)", r);
973 else if (!strcmp(tag, "done")) {
976 else if (!strcmp(tag, "encoding")) {
977 if (!strcasecmp(tag_val, "none")) encode = E_NONE;
978 else if (!strcasecmp(tag_val, "url")) encode = E_URL;
979 else if (!strcasecmp(tag_val, "entity")) encode = E_ENTITY;
981 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
982 "unknown value \"%s\" to parameter \"encoding\" of "
984 tag_val, r->filename);
990 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
991 "unknown parameter \"%s\" to tag echo in %s",
999 static int handle_perl(ap_file_t *in, request_rec *r, const char *error)
1001 char tag[MAX_STRING_LEN];
1002 char parsed_string[MAX_STRING_LEN];
1007 if (ap_allow_options(r) & OPT_INCNOEXEC) {
1008 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1009 "#perl SSI disallowed by IncludesNoExec in %s",
1014 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
1017 if (strnEQ(tag, "sub", 3)) {
1018 sub = newSVpv(tag_val, 0);
1020 else if (strnEQ(tag, "arg", 3)) {
1021 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1022 av_push(av, newSVpv(parsed_string, 0));
1024 else if (strnEQ(tag, "done", 4)) {
1028 perl_stdout2client(r);
1030 perl_call_handler(sub, r, av);
1035 /* error and tf must point to a string with room for at
1036 * least MAX_STRING_LEN characters
1038 static int handle_config(ap_file_t *in, request_rec *r, char *error, char *tf,
1041 char tag[MAX_STRING_LEN];
1043 char parsed_string[MAX_STRING_LEN];
1044 ap_table_t *env = r->subprocess_env;
1047 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 0))) {
1050 if (!strcmp(tag, "errmsg")) {
1051 parse_string(r, tag_val, error, MAX_STRING_LEN, 0);
1053 else if (!strcmp(tag, "timefmt")) {
1054 ap_time_t date = r->request_time;
1056 parse_string(r, tag_val, tf, MAX_STRING_LEN, 0);
1057 ap_table_setn(env, "DATE_LOCAL", ap_ht_time(r->pool, date, tf, 0));
1058 ap_table_setn(env, "DATE_GMT", ap_ht_time(r->pool, date, tf, 1));
1059 ap_table_setn(env, "LAST_MODIFIED",
1060 ap_ht_time(r->pool, r->finfo.mtime, tf, 0));
1062 else if (!strcmp(tag, "sizefmt")) {
1063 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1064 decodehtml(parsed_string);
1065 if (!strcmp(parsed_string, "bytes")) {
1066 *sizefmt = SIZEFMT_BYTES;
1068 else if (!strcmp(parsed_string, "abbrev")) {
1069 *sizefmt = SIZEFMT_KMG;
1072 else if (!strcmp(tag, "done")) {
1076 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1077 "unknown parameter \"%s\" to tag config in %s",
1085 static int find_file(request_rec *r, const char *directive, const char *tag,
1086 char *tag_val, ap_finfo_t *finfo, const char *error)
1088 char *to_send = tag_val;
1089 request_rec *rr = NULL;
1091 char *error_fmt = NULL;
1093 if (!strcmp(tag, "file")) {
1094 /* be safe; only files in this directory or below allowed */
1095 if (!is_only_below(tag_val)) {
1096 error_fmt = "unable to access file \"%s\" "
1097 "in parsed file %s";
1100 ap_getparents(tag_val); /* get rid of any nasties */
1101 rr = ap_sub_req_lookup_file(tag_val, r);
1103 if (rr->status == HTTP_OK && rr->finfo.protection != 0) {
1104 to_send = rr->filename;
1105 if (ap_stat(finfo, to_send, rr->pool) != APR_SUCCESS) {
1106 error_fmt = "unable to get information about \"%s\" "
1107 "in parsed file %s";
1111 error_fmt = "unable to lookup information about \"%s\" "
1112 "in parsed file %s";
1118 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, error_fmt, to_send, r->filename);
1122 if (rr) ap_destroy_sub_req(rr);
1126 else if (!strcmp(tag, "virtual")) {
1127 rr = ap_sub_req_lookup_uri(tag_val, r);
1129 if (rr->status == HTTP_OK && rr->finfo.protection != 0) {
1130 memcpy((char *) finfo, (const char *) &rr->finfo,
1131 sizeof(struct stat));
1132 ap_destroy_sub_req(rr);
1136 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1137 "unable to get information about \"%s\" "
1138 "in parsed file %s",
1139 tag_val, r->filename);
1141 ap_destroy_sub_req(rr);
1146 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1147 "unknown parameter \"%s\" to tag %s in %s",
1148 tag, directive, r->filename);
1155 static int handle_fsize(ap_file_t *in, request_rec *r, const char *error, int sizefmt)
1157 char tag[MAX_STRING_LEN];
1160 char parsed_string[MAX_STRING_LEN];
1163 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
1166 else if (!strcmp(tag, "done")) {
1170 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1171 if (!find_file(r, "fsize", tag, parsed_string, &finfo, error)) {
1172 if (sizefmt == SIZEFMT_KMG) {
1173 ap_send_size(finfo.size, r);
1177 #if defined(AP_OFF_T_IS_QUAD)
1178 ap_snprintf(tag, sizeof(tag), "%qd", finfo.size);
1180 ap_snprintf(tag, sizeof(tag), "%ld", finfo.size);
1182 l = strlen(tag); /* grrr */
1183 for (x = 0; x < l; x++) {
1184 if (x && (!((l - x) % 3))) {
1187 ap_rputc(tag[x], r);
1195 static int handle_flastmod(ap_file_t *in, request_rec *r, const char *error, const char *tf)
1197 char tag[MAX_STRING_LEN];
1200 char parsed_string[MAX_STRING_LEN];
1203 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
1206 else if (!strcmp(tag, "done")) {
1210 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
1211 if (!find_file(r, "flastmod", tag, parsed_string, &finfo, error)) {
1212 ap_rputs(ap_ht_time(r->pool, finfo.mtime, tf, 0), r);
1218 static int re_check(request_rec *r, char *string, char *rexp)
1223 compiled = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_NOSUB);
1224 if (compiled == NULL) {
1225 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1226 "unable to compile pattern \"%s\"", rexp);
1229 regex_error = ap_regexec(compiled, string, 0, (regmatch_t *) NULL, 0);
1230 ap_pregfree(r->pool, compiled);
1231 return (!regex_error);
1236 token_and, token_or, token_not, token_eq, token_ne,
1237 token_rbrace, token_lbrace, token_group,
1238 token_ge, token_le, token_gt, token_lt
1241 enum token_type type;
1242 char value[MAX_STRING_LEN];
1245 /* there is an implicit assumption here that string is at most MAX_STRING_LEN-1
1246 * characters long...
1248 static const char *get_ptoken(request_rec *r, const char *string, struct token *token)
1254 /* Skip leading white space */
1255 if (string == (char *) NULL) {
1256 return (char *) NULL;
1258 while ((ch = *string++)) {
1259 if (!ap_isspace(ch)) {
1264 return (char *) NULL;
1267 token->type = token_string; /* the default type */
1270 token->type = token_lbrace;
1273 token->type = token_rbrace;
1276 token->type = token_eq;
1279 if (*string == '=') {
1280 token->type = token_ne;
1281 return (string + 1);
1284 token->type = token_not;
1288 token->type = token_string;
1292 if (*string == '|') {
1293 token->type = token_or;
1294 return (string + 1);
1298 if (*string == '&') {
1299 token->type = token_and;
1300 return (string + 1);
1304 if (*string == '=') {
1305 token->type = token_ge;
1306 return (string + 1);
1309 token->type = token_gt;
1313 if (*string == '=') {
1314 token->type = token_le;
1315 return (string + 1);
1318 token->type = token_lt;
1322 token->type = token_string;
1325 /* We should only be here if we are in a string */
1327 token->value[next++] = ch;
1331 * Yes I know that goto's are BAD. But, c doesn't allow me to
1332 * exit a loop from a switch statement. Yes, I could use a flag,
1333 * but that is (IMHO) even less readable/maintainable than the goto.
1336 * I used the ++string throughout this section so that string
1337 * ends up pointing to the next token and I can just return it
1339 for (ch = *string; ch != '\0'; ch = *++string) {
1341 if ((ch = *++string) == '\0') {
1344 token->value[next++] = ch;
1348 if (ap_isspace(ch)) {
1361 if (*(string + 1) == '|') {
1366 if (*(string + 1) == '&') {
1375 token->value[next++] = ch;
1383 token->value[next++] = ch;
1387 /* If qs is still set, I have an unmatched ' */
1389 ap_rputs("\nUnmatched '\n", r);
1392 token->value[next] = '\0';
1398 * Hey I still know that goto's are BAD. I don't think that I've ever
1399 * used two in the same project, let alone the same file before. But,
1400 * I absolutely want to make sure that I clean up the memory in all
1401 * cases. And, without rewriting this completely, the easiest way
1402 * is to just branch to the return code which cleans it up.
1404 /* there is an implicit assumption here that expr is at most MAX_STRING_LEN-1
1405 * characters long...
1407 static int parse_expr(request_rec *r, const char *expr, const char *error)
1410 struct parse_node *left, *right, *parent;
1413 } *root, *current, *new;
1415 char buffer[MAX_STRING_LEN];
1416 ap_pool_t *expr_pool;
1419 if ((parse = expr) == (char *) NULL) {
1422 root = current = (struct parse_node *) NULL;
1423 if (ap_create_pool(&expr_pool, r->pool) != APR_SUCCESS)
1426 /* Create Parse Tree */
1428 new = (struct parse_node *) ap_palloc(expr_pool,
1429 sizeof(struct parse_node));
1430 new->parent = new->left = new->right = (struct parse_node *) NULL;
1432 if ((parse = get_ptoken(r, parse, &new->token)) == (char *) NULL) {
1435 switch (new->token.type) {
1438 #ifdef DEBUG_INCLUDE
1439 ap_rvputs(r, " Token: string (", new->token.value, ")\n", NULL);
1441 if (current == (struct parse_node *) NULL) {
1442 root = current = new;
1445 switch (current->token.type) {
1447 if (current->token.value[0] != '\0') {
1448 strncat(current->token.value, " ",
1449 sizeof(current->token.value)
1450 - strlen(current->token.value) - 1);
1452 strncat(current->token.value, new->token.value,
1453 sizeof(current->token.value)
1454 - strlen(current->token.value) - 1);
1455 current->token.value[sizeof(current->token.value) - 1] = '\0';
1467 new->parent = current;
1468 current = current->right = new;
1471 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1472 "Invalid expression \"%s\" in file %s",
1481 #ifdef DEBUG_INCLUDE
1482 ap_rputs(" Token: and/or\n", r);
1484 if (current == (struct parse_node *) NULL) {
1485 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1486 "Invalid expression \"%s\" in file %s",
1491 /* Percolate upwards */
1492 while (current != (struct parse_node *) NULL) {
1493 switch (current->token.type) {
1505 current = current->parent;
1510 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1511 "Invalid expression \"%s\" in file %s",
1518 if (current == (struct parse_node *) NULL) {
1520 new->left->parent = new;
1521 new->parent = (struct parse_node *) NULL;
1525 new->left = current->right;
1526 current->right = new;
1527 new->parent = current;
1533 #ifdef DEBUG_INCLUDE
1534 ap_rputs(" Token: not\n", r);
1536 if (current == (struct parse_node *) NULL) {
1537 root = current = new;
1540 /* Percolate upwards */
1541 while (current != (struct parse_node *) NULL) {
1542 switch (current->token.type) {
1555 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1556 "Invalid expression \"%s\" in file %s",
1563 if (current == (struct parse_node *) NULL) {
1565 new->left->parent = new;
1566 new->parent = (struct parse_node *) NULL;
1570 new->left = current->right;
1571 current->right = new;
1572 new->parent = current;
1583 #ifdef DEBUG_INCLUDE
1584 ap_rputs(" Token: eq/ne/ge/gt/le/lt\n", r);
1586 if (current == (struct parse_node *) NULL) {
1587 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1588 "Invalid expression \"%s\" in file %s",
1593 /* Percolate upwards */
1594 while (current != (struct parse_node *) NULL) {
1595 switch (current->token.type) {
1598 current = current->parent;
1612 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1613 "Invalid expression \"%s\" in file %s",
1620 if (current == (struct parse_node *) NULL) {
1622 new->left->parent = new;
1623 new->parent = (struct parse_node *) NULL;
1627 new->left = current->right;
1628 current->right = new;
1629 new->parent = current;
1635 #ifdef DEBUG_INCLUDE
1636 ap_rputs(" Token: rbrace\n", r);
1638 while (current != (struct parse_node *) NULL) {
1639 if (current->token.type == token_lbrace) {
1640 current->token.type = token_group;
1643 current = current->parent;
1645 if (current == (struct parse_node *) NULL) {
1646 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1647 "Unmatched ')' in \"%s\" in file %s",
1655 #ifdef DEBUG_INCLUDE
1656 ap_rputs(" Token: lbrace\n", r);
1658 if (current == (struct parse_node *) NULL) {
1659 root = current = new;
1662 /* Percolate upwards */
1663 while (current != (struct parse_node *) NULL) {
1664 switch (current->token.type) {
1679 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1680 "Invalid expression \"%s\" in file %s",
1687 if (current == (struct parse_node *) NULL) {
1689 new->left->parent = new;
1690 new->parent = (struct parse_node *) NULL;
1694 new->left = current->right;
1695 current->right = new;
1696 new->parent = current;
1705 /* Evaluate Parse Tree */
1707 while (current != (struct parse_node *) NULL) {
1708 switch (current->token.type) {
1710 #ifdef DEBUG_INCLUDE
1711 ap_rputs(" Evaluate string\n", r);
1713 parse_string(r, current->token.value, buffer, sizeof(buffer), 0);
1714 ap_cpystrn(current->token.value, buffer, sizeof(current->token.value));
1715 current->value = (current->token.value[0] != '\0');
1717 current = current->parent;
1722 #ifdef DEBUG_INCLUDE
1723 ap_rputs(" Evaluate and/or\n", r);
1725 if (current->left == (struct parse_node *) NULL ||
1726 current->right == (struct parse_node *) NULL) {
1727 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1728 "Invalid expression \"%s\" in file %s",
1733 if (!current->left->done) {
1734 switch (current->left->token.type) {
1736 parse_string(r, current->left->token.value,
1737 buffer, sizeof(buffer), 0);
1738 ap_cpystrn(current->left->token.value, buffer,
1739 sizeof(current->left->token.value));
1740 current->left->value = (current->left->token.value[0] != '\0');
1741 current->left->done = 1;
1744 current = current->left;
1748 if (!current->right->done) {
1749 switch (current->right->token.type) {
1751 parse_string(r, current->right->token.value,
1752 buffer, sizeof(buffer), 0);
1753 ap_cpystrn(current->right->token.value, buffer,
1754 sizeof(current->right->token.value));
1755 current->right->value = (current->right->token.value[0] != '\0');
1756 current->right->done = 1;
1759 current = current->right;
1763 #ifdef DEBUG_INCLUDE
1764 ap_rvputs(r, " Left: ", current->left->value ? "1" : "0",
1766 ap_rvputs(r, " Right: ", current->right->value ? "1" : "0",
1769 if (current->token.type == token_and) {
1770 current->value = current->left->value && current->right->value;
1773 current->value = current->left->value || current->right->value;
1775 #ifdef DEBUG_INCLUDE
1776 ap_rvputs(r, " Returning ", current->value ? "1" : "0",
1780 current = current->parent;
1785 #ifdef DEBUG_INCLUDE
1786 ap_rputs(" Evaluate eq/ne\n", r);
1788 if ((current->left == (struct parse_node *) NULL) ||
1789 (current->right == (struct parse_node *) NULL) ||
1790 (current->left->token.type != token_string) ||
1791 (current->right->token.type != token_string)) {
1792 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1793 "Invalid expression \"%s\" in file %s",
1798 parse_string(r, current->left->token.value,
1799 buffer, sizeof(buffer), 0);
1800 ap_cpystrn(current->left->token.value, buffer,
1801 sizeof(current->left->token.value));
1802 parse_string(r, current->right->token.value,
1803 buffer, sizeof(buffer), 0);
1804 ap_cpystrn(current->right->token.value, buffer,
1805 sizeof(current->right->token.value));
1806 if (current->right->token.value[0] == '/') {
1808 len = strlen(current->right->token.value);
1809 if (current->right->token.value[len - 1] == '/') {
1810 current->right->token.value[len - 1] = '\0';
1813 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1814 "Invalid rexp \"%s\" in file %s",
1815 current->right->token.value, r->filename);
1819 #ifdef DEBUG_INCLUDE
1820 ap_rvputs(r, " Re Compare (", current->left->token.value,
1821 ") with /", ¤t->right->token.value[1], "/\n", NULL);
1824 re_check(r, current->left->token.value,
1825 ¤t->right->token.value[1]);
1828 #ifdef DEBUG_INCLUDE
1829 ap_rvputs(r, " Compare (", current->left->token.value,
1830 ") with (", current->right->token.value, ")\n", NULL);
1833 (strcmp(current->left->token.value,
1834 current->right->token.value) == 0);
1836 if (current->token.type == token_ne) {
1837 current->value = !current->value;
1839 #ifdef DEBUG_INCLUDE
1840 ap_rvputs(r, " Returning ", current->value ? "1" : "0",
1844 current = current->parent;
1850 #ifdef DEBUG_INCLUDE
1851 ap_rputs(" Evaluate ge/gt/le/lt\n", r);
1853 if ((current->left == (struct parse_node *) NULL) ||
1854 (current->right == (struct parse_node *) NULL) ||
1855 (current->left->token.type != token_string) ||
1856 (current->right->token.type != token_string)) {
1857 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1858 "Invalid expression \"%s\" in file %s",
1863 parse_string(r, current->left->token.value,
1864 buffer, sizeof(buffer), 0);
1865 ap_cpystrn(current->left->token.value, buffer,
1866 sizeof(current->left->token.value));
1867 parse_string(r, current->right->token.value,
1868 buffer, sizeof(buffer), 0);
1869 ap_cpystrn(current->right->token.value, buffer,
1870 sizeof(current->right->token.value));
1871 #ifdef DEBUG_INCLUDE
1872 ap_rvputs(r, " Compare (", current->left->token.value,
1873 ") with (", current->right->token.value, ")\n", NULL);
1876 strcmp(current->left->token.value,
1877 current->right->token.value);
1878 if (current->token.type == token_ge) {
1879 current->value = current->value >= 0;
1881 else if (current->token.type == token_gt) {
1882 current->value = current->value > 0;
1884 else if (current->token.type == token_le) {
1885 current->value = current->value <= 0;
1887 else if (current->token.type == token_lt) {
1888 current->value = current->value < 0;
1891 current->value = 0; /* Don't return -1 if unknown token */
1893 #ifdef DEBUG_INCLUDE
1894 ap_rvputs(r, " Returning ", current->value ? "1" : "0",
1898 current = current->parent;
1902 if (current->right != (struct parse_node *) NULL) {
1903 if (!current->right->done) {
1904 current = current->right;
1907 current->value = !current->right->value;
1912 #ifdef DEBUG_INCLUDE
1913 ap_rvputs(r, " Evaluate !: ", current->value ? "1" : "0",
1917 current = current->parent;
1921 if (current->right != (struct parse_node *) NULL) {
1922 if (!current->right->done) {
1923 current = current->right;
1926 current->value = current->right->value;
1931 #ifdef DEBUG_INCLUDE
1932 ap_rvputs(r, " Evaluate (): ", current->value ? "1" : "0",
1936 current = current->parent;
1940 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1941 "Unmatched '(' in \"%s\" in file %s",
1947 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1948 "Unmatched ')' in \"%s\" in file %s",
1954 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1961 retval = (root == (struct parse_node *) NULL) ? 0 : root->value;
1963 ap_destroy_pool(expr_pool);
1967 static int handle_if(ap_file_t *in, request_rec *r, const char *error,
1968 int *conditional_status, int *printing)
1970 char tag[MAX_STRING_LEN];
1976 tag_val = get_tag(r->pool, in, tag, sizeof(tag), 0);
1980 else if (!strcmp(tag, "done")) {
1982 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1983 "missing expr in if statement: %s",
1988 *printing = *conditional_status = parse_expr(r, expr, error);
1989 #ifdef DEBUG_INCLUDE
1990 ap_rvputs(r, "**** if conditional_status=\"",
1991 *conditional_status ? "1" : "0", "\"\n", NULL);
1995 else if (!strcmp(tag, "expr")) {
1997 #ifdef DEBUG_INCLUDE
1998 ap_rvputs(r, "**** if expr=\"", expr, "\"\n", NULL);
2002 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2003 "unknown parameter \"%s\" to tag if in %s",
2010 static int handle_elif(ap_file_t *in, request_rec *r, const char *error,
2011 int *conditional_status, int *printing)
2013 char tag[MAX_STRING_LEN];
2019 tag_val = get_tag(r->pool, in, tag, sizeof(tag), 0);
2023 else if (!strcmp(tag, "done")) {
2024 #ifdef DEBUG_INCLUDE
2025 ap_rvputs(r, "**** elif conditional_status=\"",
2026 *conditional_status ? "1" : "0", "\"\n", NULL);
2028 if (*conditional_status) {
2033 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2034 "missing expr in elif statement: %s",
2039 *printing = *conditional_status = parse_expr(r, expr, error);
2040 #ifdef DEBUG_INCLUDE
2041 ap_rvputs(r, "**** elif conditional_status=\"",
2042 *conditional_status ? "1" : "0", "\"\n", NULL);
2046 else if (!strcmp(tag, "expr")) {
2048 #ifdef DEBUG_INCLUDE
2049 ap_rvputs(r, "**** if expr=\"", expr, "\"\n", NULL);
2053 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2054 "unknown parameter \"%s\" to tag if in %s",
2061 static int handle_else(ap_file_t *in, request_rec *r, const char *error,
2062 int *conditional_status, int *printing)
2064 char tag[MAX_STRING_LEN];
2066 if (!get_tag(r->pool, in, tag, sizeof(tag), 1)) {
2069 else if (!strcmp(tag, "done")) {
2070 #ifdef DEBUG_INCLUDE
2071 ap_rvputs(r, "**** else conditional_status=\"",
2072 *conditional_status ? "1" : "0", "\"\n", NULL);
2074 *printing = !(*conditional_status);
2075 *conditional_status = 1;
2079 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2080 "else directive does not take tags in %s",
2089 static int handle_endif(ap_file_t *in, request_rec *r, const char *error,
2090 int *conditional_status, int *printing)
2092 char tag[MAX_STRING_LEN];
2094 if (!get_tag(r->pool, in, tag, sizeof(tag), 1)) {
2097 else if (!strcmp(tag, "done")) {
2098 #ifdef DEBUG_INCLUDE
2099 ap_rvputs(r, "**** endif conditional_status=\"",
2100 *conditional_status ? "1" : "0", "\"\n", NULL);
2103 *conditional_status = 1;
2107 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2108 "endif directive does not take tags in %s",
2115 static int handle_set(ap_file_t *in, request_rec *r, const char *error)
2117 char tag[MAX_STRING_LEN];
2118 char parsed_string[MAX_STRING_LEN];
2122 var = (char *) NULL;
2124 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
2127 else if (!strcmp(tag, "done")) {
2130 else if (!strcmp(tag, "var")) {
2133 else if (!strcmp(tag, "value")) {
2134 if (var == (char *) NULL) {
2135 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2136 "variable must precede value in set directive in %s",
2141 parse_string(r, tag_val, parsed_string, sizeof(parsed_string), 0);
2142 ap_table_setn(r->subprocess_env, var, ap_pstrdup(r->pool, parsed_string));
2145 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2146 "Invalid tag for set directive in %s", r->filename);
2153 static int handle_printenv(ap_file_t *in, request_rec *r, const char *error)
2155 char tag[MAX_STRING_LEN];
2157 ap_array_header_t *arr = ap_table_elts(r->subprocess_env);
2158 ap_table_entry_t *elts = (ap_table_entry_t *)arr->elts;
2161 if (!(tag_val = get_tag(r->pool, in, tag, sizeof(tag), 1))) {
2164 else if (!strcmp(tag, "done")) {
2165 for (i = 0; i < arr->nelts; ++i) {
2166 ap_rvputs(r, ap_escape_html(r->pool, elts[i].key), "=",
2167 ap_escape_html(r->pool, elts[i].val), "\n", NULL);
2172 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2173 "printenv directive does not take tags in %s",
2182 /* -------------------------- The main function --------------------------- */
2184 /* This is a stub which parses a file descriptor. */
2186 static void send_parsed_content(ap_file_t *f, request_rec *r)
2188 char directive[MAX_STRING_LEN], error[MAX_STRING_LEN];
2189 char timefmt[MAX_STRING_LEN];
2190 int noexec = ap_allow_options(r) & OPT_INCNOEXEC;
2194 int conditional_status;
2196 ap_cpystrn(error, DEFAULT_ERROR_MSG, sizeof(error));
2197 ap_cpystrn(timefmt, DEFAULT_TIME_FORMAT, sizeof(timefmt));
2198 sizefmt = SIZEFMT_KMG;
2200 /* Turn printing on */
2201 printing = conditional_status = 1;
2204 ap_chdir_file(r->filename);
2205 if (r->args) { /* add QUERY stuff to env cause it ain't yet */
2206 char *arg_copy = ap_pstrdup(r->pool, r->args);
2208 ap_table_setn(r->subprocess_env, "QUERY_STRING", r->args);
2209 ap_unescape_url(arg_copy);
2210 ap_table_setn(r->subprocess_env, "QUERY_STRING_UNESCAPED",
2211 ap_escape_shell_cmd(r->pool, arg_copy));
2215 if (!find_string(f, STARTING_SEQUENCE, r, printing)) {
2216 if (get_directive(f, directive, sizeof(directive), r->pool)) {
2217 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2218 "mod_include: error reading directive in %s",
2223 if (!strcmp(directive, "if")) {
2228 ret = handle_if(f, r, error, &conditional_status,
2234 else if (!strcmp(directive, "else")) {
2236 ret = handle_else(f, r, error, &conditional_status,
2241 else if (!strcmp(directive, "elif")) {
2243 ret = handle_elif(f, r, error, &conditional_status,
2248 else if (!strcmp(directive, "endif")) {
2250 ret = handle_endif(f, r, error, &conditional_status,
2261 if (!strcmp(directive, "exec")) {
2263 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2264 "exec used but not allowed in %s",
2269 ret = find_string(f, ENDING_SEQUENCE, r, 0);
2272 ret = handle_exec(f, r, error);
2275 else if (!strcmp(directive, "config")) {
2276 ret = handle_config(f, r, error, timefmt, &sizefmt);
2278 else if (!strcmp(directive, "set")) {
2279 ret = handle_set(f, r, error);
2281 else if (!strcmp(directive, "include")) {
2282 ret = handle_include(f, r, error, noexec);
2284 else if (!strcmp(directive, "echo")) {
2285 ret = handle_echo(f, r, error);
2287 else if (!strcmp(directive, "fsize")) {
2288 ret = handle_fsize(f, r, error, sizefmt);
2290 else if (!strcmp(directive, "flastmod")) {
2291 ret = handle_flastmod(f, r, error, timefmt);
2293 else if (!strcmp(directive, "printenv")) {
2294 ret = handle_printenv(f, r, error);
2297 else if (!strcmp(directive, "perl")) {
2298 ret = handle_perl(f, r, error);
2302 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2303 "unknown directive \"%s\" "
2305 directive, r->filename);
2309 ret = find_string(f, ENDING_SEQUENCE, r, 0);
2312 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2313 "premature EOF in parsed file %s",
2324 /*****************************************************************
2326 * XBITHACK. Sigh... NB it's configurable per-directory; the compile-time
2327 * option only changes the default.
2330 module includes_module;
2332 xbithack_off, xbithack_on, xbithack_full
2336 #define DEFAULT_XBITHACK xbithack_full
2338 #define DEFAULT_XBITHACK xbithack_off
2341 static void *create_includes_dir_config(ap_pool_t *p, char *dummy)
2343 enum xbithack *result = (enum xbithack *) ap_palloc(p, sizeof(enum xbithack));
2344 *result = DEFAULT_XBITHACK;
2348 static const char *set_xbithack(cmd_parms *cmd, void *xbp, char *arg)
2350 enum xbithack *state = (enum xbithack *) xbp;
2352 if (!strcasecmp(arg, "off")) {
2353 *state = xbithack_off;
2355 else if (!strcasecmp(arg, "on")) {
2356 *state = xbithack_on;
2358 else if (!strcasecmp(arg, "full")) {
2359 *state = xbithack_full;
2362 return "XBitHack must be set to Off, On, or Full";
2368 static int send_parsed_file(request_rec *r)
2370 ap_file_t *f = NULL;
2371 enum xbithack *state =
2372 (enum xbithack *) ap_get_module_config(r->per_dir_config, &includes_module);
2374 request_rec *parent;
2376 if (!(ap_allow_options(r) & OPT_INCLUDES)) {
2379 r->allowed |= (1 << M_GET);
2380 if (r->method_number != M_GET) {
2383 if (r->finfo.protection == 0) {
2384 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
2385 "File does not exist: %s",
2387 ? ap_pstrcat(r->pool, r->filename, r->path_info, NULL)
2389 return HTTP_NOT_FOUND;
2392 errstatus = ap_open(&f, r->filename, APR_READ, 0, r->pool);
2394 if (errstatus != APR_SUCCESS) {
2395 ap_log_rerror(APLOG_MARK, APLOG_ERR, errstatus, r,
2396 "file permissions deny server access: %s", r->filename);
2397 return HTTP_FORBIDDEN;
2400 if ((*state == xbithack_full)
2401 #if !defined(OS2) && !defined(WIN32)
2402 /* OS/2 dosen't support Groups. */
2403 && (r->finfo.protection & S_IXGRP)
2406 ap_update_mtime(r, r->finfo.mtime);
2407 ap_set_last_modified(r);
2409 if ((errstatus = ap_meets_conditions(r)) != OK) {
2413 ap_send_http_header(r);
2415 if (r->header_only) {
2420 if ((parent = ap_get_module_config(r->request_config, &includes_module))) {
2421 /* Kludge --- for nested includes, we want to keep the subprocess
2422 * environment of the base document (for compatibility); that means
2423 * torquing our own last_modified date as well so that the
2424 * LAST_MODIFIED variable gets reset to the proper value if the
2425 * nested document resets <!--#config timefmt-->.
2426 * We also insist that the memory for this subrequest not be
2427 * destroyed, that's dealt with in handle_include().
2429 r->subprocess_env = parent->subprocess_env;
2430 ap_pool_join(parent->pool, r->pool);
2431 r->finfo.mtime = parent->finfo.mtime;
2434 /* we're not a nested include, so we create an initial
2436 ap_add_common_vars(r);
2438 add_include_vars(r, DEFAULT_TIME_FORMAT);
2440 /* XXX: this is bogus, at some point we're going to do a subrequest,
2441 * and when we do it we're going to be subjecting code that doesn't
2442 * expect to be signal-ready to SIGALRM. There is no clean way to
2443 * fix this, except to put alarm support into BUFF. -djg
2445 #ifdef CHARSET_EBCDIC
2446 /* XXX:@@@ Is the generated/included output ALWAYS in text/ebcdic format? */
2447 ap_bsetflag(r->connection->client, B_EBCDIC2ASCII, 1);
2450 send_parsed_content(f, r);
2453 /* signify that the sub request should not be killed */
2454 ap_set_module_config(r->request_config, &includes_module,
2455 NESTED_INCLUDE_MAGIC);
2461 static int send_shtml_file(request_rec *r)
2463 r->content_type = "text/html";
2464 return send_parsed_file(r);
2467 static int xbithack_handler(request_rec *r)
2469 #if defined(OS2) || defined(WIN32)
2470 /* OS/2 dosen't currently support the xbithack. This is being worked on. */
2473 enum xbithack *state;
2475 if (!(r->finfo.protection & S_IXUSR)) {
2479 state = (enum xbithack *) ap_get_module_config(r->per_dir_config,
2482 if (*state == xbithack_off) {
2485 return send_parsed_file(r);
2489 static const command_rec includes_cmds[] =
2491 {"XBitHack", set_xbithack, NULL, OR_OPTIONS, TAKE1, "Off, On, or Full"},
2495 static const handler_rec includes_handlers[] =
2497 {INCLUDES_MAGIC_TYPE, send_shtml_file},
2498 {INCLUDES_MAGIC_TYPE3, send_shtml_file},
2499 {"server-parsed", send_parsed_file},
2500 {"text/html", xbithack_handler},
2504 module MODULE_VAR_EXPORT includes_module =
2506 STANDARD20_MODULE_STUFF,
2507 create_includes_dir_config, /* dir config creater */
2508 NULL, /* dir merger --- default is to override */
2509 NULL, /* server config */
2510 NULL, /* merge server config */
2511 includes_cmds, /* command ap_table_t */
2512 includes_handlers, /* handlers */
2513 NULL /* register hooks */