1 const char cgi_rcs[] = "$Id: cgi.c,v 1.5 2001/06/05 19:59:16 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $
6 * Purpose : Declares functions to intercept request, generate
7 * html or gif answers, and to compose HTTP resonses.
9 * Functions declared include:
12 * Copyright : Written by and Copyright (C) 2001 the SourceForge
13 * IJBSWA team. http://ijbswa.sourceforge.net
15 * Based on the Internet Junkbuster originally written
16 * by and Copyright (C) 1997 Anonymous Coders and
17 * Junkbusters Corporation. http://www.junkbusters.com
19 * This program is free software; you can redistribute it
20 * and/or modify it under the terms of the GNU General
21 * Public License as published by the Free Software
22 * Foundation; either version 2 of the License, or (at
23 * your option) any later version.
25 * This program is distributed in the hope that it will
26 * be useful, but WITHOUT ANY WARRANTY; without even the
27 * implied warranty of MERCHANTABILITY or FITNESS FOR A
28 * PARTICULAR PURPOSE. See the GNU General Public
29 * License for more details.
31 * The GNU General Public License should be included with
32 * this file. If not, you can view it at
33 * http://www.gnu.org/copyleft/gpl.html
34 * or write to the Free Software Foundation, Inc., 59
35 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
39 * Revision 1.5 2001/06/05 19:59:16 jongfoster
40 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
42 * Revision 1.4 2001/06/04 10:41:52 swa
43 * show version string of cgi.h and cgi.c
45 * Revision 1.3 2001/06/03 19:12:16 oes
46 * introduced new cgi handling
48 * Revision 1.1 2001/06/03 11:03:48 oes
55 * adapted to new enlist_unique arg format
59 * introduced confdir option
61 * filters.c filtrers.h
63 * extracted-CGI relevant stuff
71 * support for new cgi mechansim
75 * functions for new list type: "map"
76 * extended enlist_unique
83 * deleted const struct interceptors
91 * added struct http_response
92 * changes struct interceptors to struct cgi_dispatcher
93 * moved HTML stuff to cgi.h
104 **********************************************************************/
110 #include <sys/types.h>
116 #define snprintf _snprintf
117 #endif /* def _WIN32 */
129 #include "miscutil.h"
130 #include "showargs.h"
132 const char cgi_h_rcs[] = CGI_H_VERSION;
134 const struct cgi_dispatcher cgi_dispatchers[] = {
137 "Show information about the version and configuration" },
138 /* { "show-url-info",
139 13, cgi_show_url_info,
140 "Show which actions apply to a URL and why" },*/
143 "HIDE Send the transparent or \"Junkbuster\" gif" },
145 /* { "untrusted-url",
146 15, ij_untrusted_url,
147 "HIDE Show why a URL was not trusted" }, */
148 #endif /* def TRUST_FILES */
151 "HIDE Send a page linking to all unhidden CGIs" },
152 { NULL, 0, NULL, NULL }
156 /*********************************************************************
158 * Function : dispatch_cgi
160 * Description : Checks if a request URL has either the magical hostname
161 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
162 * the (rest of the) path as a cgi name plus query string,
163 * prepares a map that maps CGI parameter names to their values,
164 * initializes the http_response struct, and calls the
165 * relevant CGI handler function.
168 * 1 : csp = Current client state (buffers, headers, etc...)
170 * Returns : http_response if match, NULL if nonmatch or handler fail
172 *********************************************************************/
173 struct http_response *cgi_dispatch(struct client_state *csp)
175 char *argstring = NULL;
176 const struct cgi_dispatcher *d;
177 struct map *param_list;
178 struct http_response *response;
181 * Should we intercept ?
184 /* Either the host matches CGI_PREFIX_HOST ..*/
185 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
187 /* ..then the path will all be for us */
188 argstring = csp->http->path;
190 /* Or it's the host part of HOME_PAGE_URL ? */
191 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
192 && (0 == strncmpic(csp->http->path,"/config", 7))
193 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
195 /* then it's everything following "/config" */
196 argstring = csp->http->path + 7;
204 * We have intercepted it.
207 /* Get mem for response */
208 if (NULL == ( response = zalloc(sizeof(*response))))
213 /* remove any leading slash */
214 if (*argstring == '/')
219 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
220 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
221 csp->ip_addr_str, csp->http->cmd);
223 for (d = cgi_dispatchers; d->handler; d++)
225 if (strncmp(argstring, d->name, d->name_length) == 0)
227 param_list = parse_cgi(argstring + d->name_length);
228 if ((d->handler)(csp, response, param_list))
233 free_map(param_list);
244 /*********************************************************************
246 * Function : parse_cgi
248 * Description : Parse a URL-encoded argument string into name/value
249 * pairs and store them in a struct map list.
252 * 1 : string = string to be parsed
254 * Returns : poniter to param list, or NULL if failiure
256 *********************************************************************/
257 struct map *parse_cgi(char *argstring)
260 char *vector[BUFSIZ];
262 struct map *cgi_params = NULL;
264 if(*argstring == '?') argstring++;
265 tmp = strdup(argstring);
267 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
269 for (i = 0; i < pairs; i++)
271 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
274 cgi_params = map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
284 /*********************************************************************
286 * Function : make_http_response
288 * Description : Fill in the missing headers in an http response,
289 * and flatten the headers to an http head.
292 * 1 : rsp = pointer to http_response to be processed
294 * Returns : length of http head, or 0 on failiure
296 *********************************************************************/
297 int make_http_response(struct http_response *rsp)
301 /* Fill in the HTTP Status */
302 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
303 enlist_first(rsp->headers, buf);
305 /* Set the Content-Length */
306 if (rsp->content_length == 0)
308 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
312 sprintf(buf, "Content-Length: %d", rsp->content_length);
313 enlist(rsp->headers, buf);
315 /* Fill in the default headers FIXME: Are these correct? sequence OK? check rfc! */
316 enlist_unique(rsp->headers, "Pragma: no-cache", 7);
317 enlist_unique(rsp->headers, "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT", 14);
318 enlist_unique(rsp->headers, "Expires: Thu Jul 31, 1997 07:42:22 pm GMT", 8);
319 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
320 enlist(rsp->headers, "");
324 if (NULL == (rsp->head = list_to_text(rsp->headers)))
326 free_http_response(rsp);
330 return(strlen(rsp->head));
334 /*********************************************************************
336 * Function : free_http_response
338 * Description : Free the memory occupied by an http_response
339 * and its depandant structures.
342 * 1 : rsp = pointer to http_response to be freed
346 *********************************************************************/
347 void free_http_response(struct http_response *rsp)
354 destroy_list(rsp->headers);
359 /*********************************************************************
361 * Function : fill_template
363 * Description : CGI support function that loads a given HTML
364 * template from the confdir, and fills it in
365 * by replacing @name@ with value using pcrs,
366 * for each item in the output map.
369 * 1 : csp = Current client state (buffers, headers, etc...)
370 * 3 : template = name of the HTML template to be used
371 * 2 : answers = map with fill in symbol -> name pairs
372 * FIXME: needs better name!
374 * Returns : char * with filled out form, or NULL if failiure
376 *********************************************************************/
377 char *fill_template(struct client_state *csp, char *template, struct map *answers)
380 pcrs_job *job, *joblist = NULL;
382 char *new, *old = NULL;
387 * Open template file or fail
389 snprintf(buf, BUFSIZ, "%s/templates/%s", csp->config->confdir, template);
391 if(NULL == (fp = fopen(buf, "r")))
393 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
398 * Assemble pcrs joblist from answers map
400 for (m = answers; m; m = m->next)
404 snprintf(buf, BUFSIZ, "s°@%s@°%s°ig", m->name, m->value);
406 if(NULL == (job = pcrs_make_job(buf, &error)))
408 log_error(LOG_LEVEL_ERROR, "Adding template fill job %s failed with error %d",
410 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
421 * Read the file, ignoring comments
423 while (fgets(buf, BUFSIZ, fp))
425 /* skip lines starting with '#' */
426 if(*buf == '#') continue;
428 old = strsav(old, buf);
435 size = strlen(old) + 1;
438 for (job = joblist; NULL != job; job = job->next)
440 pcrs_exec_substitution(job, old, size, &new, &size);
441 if (old != buf) free(old);
446 * Free the jobs & return
448 while ( NULL != (joblist = pcrs_free_job(joblist)) ) {};
454 /*********************************************************************
456 * Function : dump_map
458 * Description : HTML-dump a map for debugging
461 * 1 : map = map to dump
463 * Returns : string with HTML
465 *********************************************************************/
466 char *dump_map(struct map *map)
472 ret = strsav(ret, "<table>\n");
476 ret = strsav(ret, "<tr><td><b>");
477 ret = strsav(ret, p->name);
478 ret = strsav(ret, "</b></td><td>");
479 ret = strsav(ret, p->value);
480 ret = strsav(ret, "</td></tr>\n");
484 ret = strsav(ret, "</table>\n");
489 /*********************************************************************
491 * Function : cgi_default
493 * Description : CGI function that is called if no action was given
494 * lists menu of available unhidden CGIs.
497 * 1 : csp = Current client state (buffers, headers, etc...)
498 * 2 : rsp = http_response data structure for output
499 * 3 : parameters = map of cgi parameters
503 *********************************************************************/
504 int cgi_default(struct client_state *csp, struct http_response *rsp,
505 struct map *parameters)
507 char *p, *tmp = NULL;
509 const struct cgi_dispatcher *d;
510 struct map *exports = NULL;
512 /* List available unhidden CGI's and export as "other-cgis" */
513 for (d = cgi_dispatchers; d->handler; d++)
515 if (strncmp(d->description, "HIDE", 4))
517 snprintf(buf, BUFSIZ, "<li><a href=%s/config/%s>%s</a></li>",
518 HOME_PAGE_URL, d->name, d->description);
519 tmp = strsav(tmp, buf);
522 exports = map(exports, "other-cgis", 1, tmp, 0);
524 /* If there were other parameters, export a dump as "cgi-parameters" */
527 p = dump_map(parameters);
528 tmp = strsav(tmp, "<p>What made you think this cgi takes options?\n"
529 "Anyway, here they are, in case you're interested:</p>\n");
530 tmp = strsav(tmp, p);
531 exports = map(exports, "cgi-parameters", 1, tmp, 0);
536 exports = map(exports, "cgi-parameters", 1, "", 1);
539 rsp->body = fill_template(csp, "default", exports);
547 /*********************************************************************
549 * Function : cgi_send_banner
551 * Description : CGI function that returns a banner.
554 * 1 : csp = Current client state (buffers, headers, etc...)
555 * 2 : rsp = http_response data structure for output
556 * 3 : parameters = map of cgi parameters
559 * type : Selects the type of banner between "trans" and "jb".
560 * Defaults to "jb" if absent or != "trans".
564 *********************************************************************/
565 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
566 struct map *parameters)
568 if(strcmp(lookup(parameters, "type"), "trans"))
570 rsp->body = bindup(CJBGIF, sizeof(CJBGIF));
571 rsp->content_length = sizeof(CJBGIF);
575 rsp->body = bindup(CBLANKGIF, sizeof(CBLANKGIF));
576 rsp->content_length = sizeof(CBLANKGIF);
579 enlist(rsp->headers, "Content-Type: image/gif");
585 #ifdef FAST_REDIRECTS
586 /*********************************************************************
588 * Function : redirect_url
590 * Description : Checks for redirection URLs and returns a HTTP redirect
591 * to the destination URL.
594 * 1 : http = http_request request, check `basename's of blocklist
595 * 2 : csp = Current client state (buffers, headers, etc...)
597 * Returns : NULL if URL was clean, HTTP redirect otherwise.
599 *********************************************************************/
600 char *redirect_url(struct http_request *http, struct client_state *csp)
604 p = q = csp->http->path;
605 log_error(LOG_LEVEL_REDIRECTS, "checking path: %s", p);
607 /* find the last URL encoded in the request */
608 while (p = strstr(p, "http://"))
613 /* if there was any, generate and return a HTTP redirect */
614 if (q != csp->http->path)
616 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
618 p = (char *)malloc(strlen(HTTP_REDIRECT_TEMPLATE) + strlen(q));
619 sprintf(p, HTTP_REDIRECT_TEMPLATE, q);
628 #endif /* def FAST_REDIRECTS */
632 /*********************************************************************
634 * Function : cgi_show_status
636 * Description : CGI function that returns a a web page describing the
637 * current status of IJB.
640 * 1 : csp = Current client state (buffers, headers, etc...)
641 * 2 : rsp = http_response data structure for output
642 * 3 : parameters = map of cgi parameters
645 * type : Selects the type of banner between "trans" and "jb".
646 * Defaults to "jb" if absent or != "trans".
650 *********************************************************************/
651 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
652 struct map *parameters)
655 struct map *exports = NULL;
657 #ifdef SPLIT_PROXY_ARGS
661 const char * filename = NULL;
662 char * file_description = NULL;
665 p = lookup(parameters, "file");
669 if (csp->actions_list)
671 filename = csp->actions_list->filename;
672 file_description = "Actions List";
680 filename = csp->rlist->filename;
681 file_description = "Regex Filter List";
684 #endif /* def PCRS */
690 filename = csp->tlist->filename;
691 file_description = "Trust List";
694 #endif /* def TRUST_FILES */
697 if (NULL != filename)
699 exports = map(exports, "filename", 1, file_description, 1);
700 exports = map(exports, "filepath", 1, html_encode(filename), 0);
702 if ((fp = fopen(filename, "r")) == NULL)
704 exports = map(exports, "content", 1, "</pre><h1>ERROR OPENING FILE!</h1><pre>", 1);
708 while (fgets(buf, sizeof(buf), fp))
710 p = html_encode(buf);
715 s = strsav(s, "<br>");
719 exports = map(exports, "contents", 1, s, 0);
721 rsp->body = fill_template(csp, "show-status-file", exports);;
727 #endif /* def SPLIT_PROXY_ARGS */
729 exports = map(exports, "redirect-url", 1, REDIRECT_URL, 1);
730 exports = map(exports, "version", 1, VERSION, 1);
731 exports = map(exports, "home-page", 1, HOME_PAGE_URL, 1);
732 exports = map(exports, "invocation-args", 1, csp->config->proxy_args_header, 1);
736 exports = map(exports, "statistics", 1, add_stats(NULL), 0);
738 exports = map(exports, "statistics", 1, "", 1);
739 #endif /* ndef STATISTICS */
741 #ifdef SPLIT_PROXY_ARGS
742 if (csp->actions_list)
744 exports = map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
748 exports = map(exports, "actions-filename", 1, "None specified", 1);
754 exports = map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
758 exports = map(exports, "re-filter-filename", 1, "None specified", 1);
761 exports = map(exports, "re-filter-killer-start.*re-filter-killer-end", 1, "", 1);
762 #endif /* ndef PCRS */
767 exports = map(exports, "trust-filename", 1, csp->tlist->filename, 1);
771 exports = map(exports, "trust-filename", 1, "None specified", 1);
774 exports = map(exports, "acl-killer-start.*acl-killer-end", 1, "", 1);
775 #endif /* ndef TRUST_FILES */
777 exports = map(exports, ".list", 1, "" , 1);
779 #else /* ifndef SPLIT_PROXY_ARGS */
780 exports = map(exports, "magic-eliminator-start.*magic-eliminator-end", 1, "", 1);
784 map(exports, "clist", 1, csp->clist->proxy_args , 1);
790 map(exports, "rlist", 1, csp->rlist->proxy_args , 1);
792 #endif /* def PCRS */
797 map(exports, "tlist", 1, csp->tlist->proxy_args , 1);
799 #endif /* def TRUST_FILES */
801 #endif /* ndef SPLIT_PROXY_ARGS */
803 s = end_proxy_args(csp->config);
804 exports = map(exports, "rcs-and-defines", 1, s , 0);
807 rsp->body = fill_template(csp, "show-status", exports);
814 /*********************************************************************
816 * Function : cgi_show_url_info
818 * Description : (please fill me in)
821 * 1 : http = http_request request for crunched URL
822 * 2 : csp = Current client state (buffers, headers, etc...)
826 *********************************************************************/
827 char *cgi_show_url_info(struct http_request *http, struct client_state *csp)
829 char * query_string = strchr(http->path, '?');
832 if (query_string != NULL)
834 query_string = url_decode(query_string + 1);
835 if (strncmpic(query_string, "url=", 4) == 0)
837 host = strdup(query_string + 4);
847 struct file_list *fl;
848 struct url_actions *b;
849 struct url_spec url[1];
850 struct current_action_spec action[1];
852 init_current_action(action);
854 result = (char *)malloc(sizeof(C_URL_INFO_HEADER) + 2 * strlen(host));
855 sprintf(result, C_URL_INFO_HEADER, host, host);
857 s = current_action_to_text(action);
858 result = strsav(result, "<h3>Defaults:</h3>\n<p><b>{");
859 result = strsav(result, s);
860 result = strsav(result, " }</b></p>\n<h3>Patterns affecting the URL:</h3>\n<p>\n");
863 s = strchr(host, '/');
873 s = strchr(host, ':');
880 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
884 result = strsav(result, C_URL_INFO_FOOTER);
890 /* if splitting the domain fails, punt */
891 if (url->dbuf == NULL)
895 result = strsav(result, C_URL_INFO_FOOTER);
899 for (b = b->next; NULL != b; b = b->next)
901 if ((b->url->port == 0) || (b->url->port == port))
903 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
905 if ((b->url->path == NULL) ||
907 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
909 (strncmp(b->url->path, path, b->url->pathlen) == 0)
913 s = actions_to_text(b->action);
914 result = strsav(result, "<b>{");
915 result = strsav(result, s);
916 result = strsav(result, " }</b><br>\n<code>");
917 result = strsav(result, b->url->spec);
918 result = strsav(result, "</code><br>\n<br>\n");
921 merge_current_action(action, b->action);
933 s = current_action_to_text(action);
934 result = strsav(result, "</p>\n<h2>Final Results:</h2>\n<p><b>{");
935 result = strsav(result, s);
936 result = strsav(result, " }</b><br>\n<br>\n");
939 free_current_action(action);
941 result = strsav(result, C_URL_INFO_FOOTER);
946 return strdup(C_URL_INFO_FORM);
953 /*********************************************************************
955 * Function : ij_untrusted_url
957 * Description : This "crunch"es "http:/any.thing/ij-untrusted-url" and
958 * returns a web page describing why it was untrusted.
961 * 1 : http = http_request request for crunched URL
962 * 2 : csp = Current client state (buffers, headers, etc...)
964 * Returns : A string that contains why this was untrusted.
966 *********************************************************************/
967 char *ij_untrusted_url(struct http_request *http, struct client_state *csp)
970 char *hostport, *path, *refer, *p, *v[9];
972 struct url_spec **tl, *t;
975 static const char format[] =
976 "HTTP/1.0 200 OK\r\n"
978 "Last-Modified: Thu Jul 31, 1997 07:42:22 pm GMT\n"
979 "Expires: Thu Jul 31, 1997 07:42:22 pm GMT\n"
980 "Content-Type: text/html\n\n"
983 "<title>Internet Junkbuster: Request for untrusted URL</title>\n"
989 "The " BANNER " Proxy "
990 "<A href=\"" HOME_PAGE_URL "\">"
991 "(" HOME_PAGE_URL ") </A>"
992 "intercepted the request for %s%s\n"
993 "because the URL is not trusted.\n"
996 if ((n = ssplit(http->path, "?+", v, SZ(v), 0, 0)) == 4)
998 hostport = url_decode(v[1]);
999 path = url_decode(v[2]);
1000 refer = url_decode(v[3]);
1004 hostport = strdup("undefined_host");
1005 path = strdup("/undefined_path");
1006 refer = strdup("undefined");
1010 n += strlen(hostport);
1013 if ((p = (char *)malloc(n)))
1015 sprintf(p, format, hostport, path);
1018 strsav(p, "The referrer in this request was <strong>");
1020 strsav(p, "</strong><br>\n");
1026 p = strsav(p, "<h3>The following referrers are trusted</h3>\n");
1028 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
1030 sprintf(buf, "%s<br>\n", t->spec);
1034 if (csp->config->trust_info->next)
1040 "You can learn more about what this means "
1041 "and what you may be able to do about it by "
1042 "reading the following documents:<br>\n"
1048 for (l = csp->config->trust_info->next; l ; l = l->next)
1051 "<li> <a href=%s>%s</a><br>\n",
1056 p = strsav(p, "</ol>\n");
1059 p = strsav(p, "</body>\n" "</html>\n");
1064 #endif /* def TRUST_FILES */
1068 /*********************************************************************
1070 * Function : add_stats
1072 * Description : Statistics function of JB. Called by `show_proxy_args'.
1075 * 1 : s = string that holds the proxy args description page
1077 * Returns : A pointer to the descriptive status web page.
1079 *********************************************************************/
1080 char *add_stats(char *s)
1083 * Output details of the number of requests rejected and
1084 * accepted. This is switchable in the junkbuster config.
1085 * Does nothing if this option is not enabled.
1088 float perc_rej; /* Percentage of http requests rejected */
1090 int local_urls_read = urls_read;
1091 int local_urls_rejected = urls_rejected;
1094 * Need to alter the stats not to include the fetch of this
1097 * Can't do following thread safely! doh!
1100 * urls_rejected--; * This will be incremented subsequently *
1103 s = strsav(s,"<h2>Statistics for this " BANNER ":</h2>\n");
1105 if (local_urls_read == 0)
1108 s = strsav(s,"No activity so far!\n");
1114 perc_rej = (float)local_urls_rejected * 100.0F /
1115 (float)local_urls_read;
1118 "%d requests received, %d filtered "
1121 local_urls_rejected, perc_rej);
1123 s = strsav(s,out_str);
1128 #endif /* def STATISTICS */