1 const char cgi_rcs[] = "$Id: cgi.c,v 1.20 2001/09/13 23:40:36 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.20 2001/09/13 23:40:36 jongfoster
40 * (Cosmetic only) Indentation correction
42 * Revision 1.19 2001/09/13 23:31:25 jongfoster
43 * Moving image data to cgi.c rather than cgi.h.
45 * Revision 1.18 2001/08/05 16:06:20 jongfoster
46 * Modifiying "struct map" so that there are now separate header and
47 * "map_entry" structures. This means that functions which modify a
48 * map no longer need to return a pointer to the modified map.
49 * Also, it no longer reverses the order of the entries (which may be
50 * important with some advanced template substitutions).
52 * Revision 1.17 2001/08/05 15:57:38 oes
53 * Adapted finish_http_response to new list_to_text
55 * Revision 1.16 2001/08/01 21:33:18 jongfoster
56 * Changes to fill_template() that reduce memory usage without having
57 * an impact on performance. I also renamed some variables so as not
58 * to clash with the C++ keywords "new" and "template".
60 * Revision 1.15 2001/08/01 21:19:22 jongfoster
61 * Moving file version information to a separate CGI page.
63 * Revision 1.14 2001/08/01 00:19:03 jongfoster
64 * New function: map_conditional() for an if-then-else syntax.
65 * Changing to use new version of show_defines()
67 * Revision 1.13 2001/07/30 22:08:36 jongfoster
68 * Tidying up #defines:
69 * - All feature #defines are now of the form FEATURE_xxx
70 * - Permanently turned off WIN_GUI_EDIT
71 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
73 * Revision 1.12 2001/07/29 18:47:05 jongfoster
74 * Adding missing #include "loadcfg.h"
76 * Revision 1.11 2001/07/18 17:24:37 oes
77 * Changed to conform to new pcrs interface
79 * Revision 1.10 2001/07/13 13:53:13 oes
80 * Removed all #ifdef PCRS and related code
82 * Revision 1.9 2001/06/29 21:45:41 oes
83 * Indentation, CRLF->LF, Tab-> Space
85 * Revision 1.8 2001/06/29 13:21:46 oes
86 * - Cosmetics: renamed and reordered functions, variables,
87 * texts, improved comments etc
89 * - Removed ij_untrusted_url() The relevant
90 * info is now part of the "untrusted" page,
91 * which is generated by filters.c:trust_url()
93 * - Generators of content now call finish_http_response()
94 * themselves, making jcc.c:chat() a little less
97 * - Removed obsolete "Pragma: no-cache" from our headers
99 * - http_responses now know their head length
101 * - fill_template now uses the new interface to pcrs, so that
102 * - long jobs (like whole files) no longer have to be assembled
103 * in a fixed size buffer
104 * - the new T (trivial) option is used, and the replacement may
105 * contain Perl syntax backrefs without confusing pcrs
107 * - Introduced default_exports() which generates a set of exports
108 * common to all CGIs and other content generators
110 * - Introduced convenience function map_block_killer()
112 * - Introduced convenience function make_menu()
114 * - Introduced CGI-like function error_response() which generates
115 * the "No such domain" and "Connect failed" messages using the
118 * - cgi_show_url_info:
119 * - adapted to new CGI features
120 * - form and answers now generated from same template
121 * - http:// prefix in URL now OK
124 * - adapted to new CGI features
125 * - no longer uses csp->init_proxy_args
128 * - moved menu generation to make_menu()
130 * - add_stats now writes single export map entries instead
133 * - Moved redirect_url() to filters.c
135 * - Fixed mem leak in free_http_response(), map_block_killer(),
137 * - Removed logentry from cancelled commit
139 * Revision 1.7 2001/06/09 10:51:58 jongfoster
140 * Changing "show URL info" handler to new style.
141 * Changing BUFSIZ ==> BUFFER_SIZE
143 * Revision 1.6 2001/06/07 23:05:19 jongfoster
144 * Removing code related to old forward and ACL files.
146 * Revision 1.5 2001/06/05 19:59:16 jongfoster
147 * Fixing multiline character string (a GCC-only "feature"), and snprintf (it's _snprintf under VC++).
149 * Revision 1.4 2001/06/04 10:41:52 swa
150 * show version string of cgi.h and cgi.c
152 * Revision 1.3 2001/06/03 19:12:16 oes
153 * introduced new cgi handling
155 * No revisions before 1.3
157 **********************************************************************/
163 #include <sys/types.h>
170 #define snprintf _snprintf
171 #endif /* def _WIN32 */
182 #include "miscutil.h"
183 #include "showargs.h"
186 const char cgi_h_rcs[] = CGI_H_VERSION;
188 const struct cgi_dispatcher cgi_dispatcher[] = {
191 "Show information about the current configuration" },
193 13, cgi_show_url_info,
194 "Show which actions apply to a URL and why" },
196 12, cgi_show_version,
197 "Show the source code version numbers" },
200 "HIDE Send the transparent or \"Junkbuster\" gif" },
203 "Junkbuster main page" },
204 { NULL, 0, NULL, NULL }
211 * Hint: You can encode your own GIFs like that:
212 * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }'
215 const char image_junkbuster_gif_data[] =
216 "GIF89aD\000\013\000\360\000\000\000\000\000\377\377\377!"
217 "\371\004\001\000\000\001\000,\000\000\000\000D\000\013\000"
218 "\000\002a\214\217\251\313\355\277\000\200G&K\025\316hC\037"
219 "\200\234\230Y\2309\235S\230\266\206\372J\253<\3131\253\271"
220 "\270\215\342\254\013\203\371\202\264\334P\207\332\020o\266"
221 "N\215I\332=\211\312\3513\266:\026AK)\364\370\365aobr\305"
222 "\372\003S\275\274k2\354\254z\347?\335\274x\306^9\374\276"
225 const int image_junkbuster_gif_length = sizeof(image_junkbuster_gif_data) - 1;
228 const char image_blank_gif_data[] =
229 "GIF89a\001\000\001\000\200\000\000\377\377\377\000\000"
230 "\000!\371\004\001\000\000\000\000,\000\000\000\000\001"
231 "\000\001\000\000\002\002D\001\000;";
233 const int image_blank_gif_length = sizeof(image_blank_gif_data) - 1;
236 /*********************************************************************
238 * Function : dispatch_cgi
240 * Description : Checks if a request URL has either the magical hostname
241 * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses
242 * the (rest of the) path as a cgi name plus query string,
243 * prepares a map that maps CGI parameter names to their values,
244 * initializes the http_response struct, and calls the
245 * relevant CGI handler function.
248 * 1 : csp = Current client state (buffers, headers, etc...)
250 * Returns : http_response if match, NULL if nonmatch or handler fail
252 *********************************************************************/
253 struct http_response *dispatch_cgi(struct client_state *csp)
255 char *argstring = NULL;
256 const struct cgi_dispatcher *d;
257 struct map *param_list;
258 struct http_response *rsp;
261 * Should we intercept ?
264 /* Either the host matches CGI_PREFIX_HOST ..*/
265 if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST))
267 /* ..then the path will all be for us */
268 argstring = csp->http->path;
270 /* Or it's the host part HOME_PAGE_URL, and the path /config ? */
271 else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 ))
272 && (0 == strncmpic(csp->http->path,"/config", 7))
273 && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0')))
275 /* then it's everything following "/config" */
276 argstring = csp->http->path + 7;
284 * This is a CGI call.
287 /* Get mem for response or fail*/
288 if (NULL == ( rsp = zalloc(sizeof(*rsp))))
293 /* Remove leading slash */
294 if (*argstring == '/')
299 log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path);
300 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 3",
301 csp->ip_addr_str, csp->http->cmd);
303 /* Find and start the right CGI function*/
304 for (d = cgi_dispatcher; d->handler; d++)
306 if (strncmp(argstring, d->name, d->name_length) == 0)
308 param_list = parse_cgi_parameters(argstring + d->name_length);
309 if ((d->handler)(csp, rsp, param_list))
314 free_map(param_list);
315 return(finish_http_response(rsp));
319 /* Can't get here, since cgi_default will match all requests */
326 /*********************************************************************
328 * Function : parse_cgi_parameters
330 * Description : Parse a URL-encoded argument string into name/value
331 * pairs and store them in a struct map list.
334 * 1 : string = string to be parsed
336 * Returns : pointer to param list
338 *********************************************************************/
339 struct map *parse_cgi_parameters(char *argstring)
342 char *vector[BUFFER_SIZE];
344 struct map *cgi_params = new_map();
346 if(*argstring == '?')
350 tmp = strdup(argstring);
352 pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1);
354 for (i = 0; i < pairs; i++)
356 if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0'))
359 map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0);
369 /*********************************************************************
371 * Function : cgi_default
373 * Description : CGI function that is called if no action was given.
374 * Lists menu of available unhidden CGIs.
377 * 1 : csp = Current client state (buffers, headers, etc...)
378 * 2 : rsp = http_response data structure for output
379 * 3 : parameters = map of cgi parameters
383 *********************************************************************/
384 int cgi_default(struct client_state *csp, struct http_response *rsp,
385 struct map *parameters)
389 struct map * exports = default_exports(csp, "");
391 /* If there were other parameters, export a dump as "cgi-parameters" */
394 p = dump_map(parameters);
395 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
396 "Anyway, here they are, in case you're interested:</p>\n");
397 tmp = strsav(tmp, p);
398 map(exports, "cgi-parameters", 1, tmp, 0);
403 map(exports, "cgi-parameters", 1, "", 1);
406 rsp->body = fill_template(csp, "default", exports);
413 /*********************************************************************
415 * Function : cgi_send_banner
417 * Description : CGI function that returns a banner.
420 * 1 : csp = Current client state (buffers, headers, etc...)
421 * 2 : rsp = http_response data structure for output
422 * 3 : parameters = map of cgi parameters
425 * type : Selects the type of banner between "trans" and "jb".
426 * Defaults to "jb" if absent or != "trans".
430 *********************************************************************/
431 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
432 struct map *parameters)
434 if(strcmp(lookup(parameters, "type"), "trans"))
436 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
437 rsp->content_length = image_junkbuster_gif_length;
441 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
442 rsp->content_length = image_blank_gif_length;
445 enlist(rsp->headers, "Content-Type: image/gif");
453 /*********************************************************************
455 * Function : cgi_show_version
457 * Description : CGI function that returns a a web page describing the
458 * file versions of IJB.
461 * 1 : csp = Current client state (buffers, headers, etc...)
462 * 2 : rsp = http_response data structure for output
463 * 3 : parameters = map of cgi parameters
466 * type : Selects the type of banner between "trans" and "jb".
467 * Defaults to "jb" if absent or != "trans".
471 *********************************************************************/
472 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
473 struct map *parameters)
475 struct map * exports = default_exports(csp, "show-version");
477 map(exports, "sourceversions", 1, show_rcs(), 0);
479 rsp->body = fill_template(csp, "show-version", exports);
486 /*********************************************************************
488 * Function : cgi_show_status
490 * Description : CGI function that returns a a web page describing the
491 * current status of IJB.
494 * 1 : csp = Current client state (buffers, headers, etc...)
495 * 2 : rsp = http_response data structure for output
496 * 3 : parameters = map of cgi parameters
499 * type : Selects the type of banner between "trans" and "jb".
500 * Defaults to "jb" if absent or != "trans".
504 *********************************************************************/
505 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
506 struct map *parameters)
512 char buf[BUFFER_SIZE];
514 const char * filename = NULL;
515 char * file_description = NULL;
517 struct map * exports = default_exports(csp, "show-status");
519 switch (*(lookup(parameters, "file")))
522 if (csp->actions_list)
524 filename = csp->actions_list->filename;
525 file_description = "Actions List";
532 filename = csp->rlist->filename;
533 file_description = "Regex Filter List";
541 filename = csp->tlist->filename;
542 file_description = "Trust List";
545 #endif /* def FEATURE_TRUST */
548 if (NULL != filename)
550 map(exports, "file-description", 1, file_description, 1);
551 map(exports, "filepath", 1, html_encode(filename), 0);
553 if ((fp = fopen(filename, "r")) == NULL)
555 map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
559 while (fgets(buf, sizeof(buf), fp))
561 p = html_encode(buf);
566 s = strsav(s, "<br>");
570 map(exports, "contents", 1, s, 0);
572 rsp->body = fill_template(csp, "show-status-file", exports);
578 map(exports, "redirect-url", 1, REDIRECT_URL, 1);
581 for (i=0; i < Argc; i++)
583 s = strsav(s, Argv[i]);
586 map(exports, "invocation", 1, s, 0);
588 map(exports, "options", 1, csp->config->proxy_args, 1);
589 show_defines(exports);
591 #ifdef FEATURE_STATISTICS
593 #else /* ndef FEATURE_STATISTICS */
594 map_block_killer(exports, "statistics");
595 #endif /* ndef FEATURE_STATISTICS */
597 if (csp->actions_list)
599 map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
603 map(exports, "actions-filename", 1, "None specified", 1);
608 map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
612 map(exports, "re-filter-filename", 1, "None specified", 1);
618 map(exports, "trust-filename", 1, csp->tlist->filename, 1);
622 map(exports, "trust-filename", 1, "None specified", 1);
625 map_block_killer(exports, "trust-support");
626 #endif /* ndef FEATURE_TRUST */
628 rsp->body = fill_template(csp, "show-status", exports);
635 /*********************************************************************
637 * Function : cgi_show_url_info
639 * Description : CGI function that determines and shows which actions
640 * junkbuster will perform for a given url, and which
641 * matches starting from the defaults have lead to that.
644 * 1 : csp = Current client state (buffers, headers, etc...)
645 * 2 : rsp = http_response data structure for output
646 * 3 : parameters = map of cgi parameters
649 * url : The url whose actions are to be determined.
650 * If url is unset, the url-given conditional will be
651 * set, so that all but the form can be suppressed in
656 *********************************************************************/
657 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
658 struct map *parameters)
662 struct map * exports = default_exports(csp, "show-url-info");
664 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
666 map_block_killer(exports, "url-given");
667 map(exports, "url", 1, "", 1);
671 char *matches = NULL;
676 struct file_list *fl;
677 struct url_actions *b;
678 struct url_spec url[1];
679 struct current_action_spec action[1];
682 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
684 map(exports, "url", 1, host, 1);
685 map(exports, "url-html", 1, html_encode(host), 0);
687 init_current_action(action);
689 s = current_action_to_text(action);
690 map(exports, "default", 1, s , 0);
692 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
694 map(exports, "matches", 1, "none" , 1);
695 map(exports, "final", 1, lookup(exports, "default"), 1);
698 free_current_action(action);
700 rsp->body = fill_template(csp, "show-url-info", exports);
706 s = strchr(host, '/');
716 s = strchr(host, ':');
726 /* if splitting the domain fails, punt */
727 if (url->dbuf == NULL)
729 map(exports, "matches", 1, "none" , 1);
730 map(exports, "final", 1, lookup(exports, "default"), 1);
734 free_current_action(action);
736 rsp->body = fill_template(csp, "show-url-info", exports);
742 for (b = b->next; NULL != b; b = b->next)
744 if ((b->url->port == 0) || (b->url->port == port))
746 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
748 if ((b->url->path == NULL) ||
750 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
752 (strncmp(b->url->path, path, b->url->pathlen) == 0)
756 s = actions_to_text(b->action);
757 matches = strsav(matches, "<b>{");
758 matches = strsav(matches, s);
759 matches = strsav(matches, " }</b><br>\n<code>");
760 matches = strsav(matches, b->url->spec);
761 matches = strsav(matches, "</code><br>\n<br>\n");
764 merge_current_action(action, b->action);
773 map(exports, "matches", 1, matches , 0);
777 map(exports, "matches", 1, "none", 1);
787 s = current_action_to_text(action);
788 map(exports, "final", 1, s, 0);
791 free_current_action(action);
794 rsp->body = fill_template(csp, "show-url-info", exports);
801 /*********************************************************************
803 * Function : error_response
805 * Description : returns an http_response that explains the reason
806 * why a request failed.
809 * 1 : csp = Current client state (buffers, headers, etc...)
810 * 2 : templatename = Which template should be used for the answer
811 * 3 : errno = system error number
813 * Returns : NULL if no memory, else http_response
815 *********************************************************************/
816 struct http_response *error_response(struct client_state *csp, const char *templatename, int err)
818 struct http_response *rsp;
819 struct map * exports = default_exports(csp, NULL);
821 if (NULL == ( rsp = (struct http_response *)zalloc(sizeof(*rsp))))
826 map(exports, "host-html", 1, html_encode(csp->http->host), 0);
827 map(exports, "hostport", 1, csp->http->hostport, 1);
828 map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
829 map(exports, "path", 1, csp->http->path, 1);
830 map(exports, "path-html", 1, html_encode(csp->http->path), 0);
831 map(exports, "error", 1, safe_strerror(err), 0);
832 map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1);
834 rsp->body = fill_template(csp, templatename, exports);
837 if (!strcmp(templatename, "no-such-domain"))
839 rsp->status = strdup("404 No such domain");
841 else if (!strcmp(templatename, "connect-failed"))
843 rsp->status = strdup("503 Connect failed");
846 return(finish_http_response(rsp));
850 /*********************************************************************
852 * Function : get_http_time
854 * Description : Get the time in a format suitable for use in a
855 * HTTP header - e.g.:
856 * "Sun, 06 Nov 1994 08:49:37 GMT"
859 * 1 : time_offset = Time returned will be current time
860 * plus this number of seconds.
861 * 2 : buf = Destination for result. Must be long enough
862 * to hold 29 characters plus a trailing zero.
866 *********************************************************************/
867 static void get_http_time(int time_offset, char * buf)
869 static const char day_names[7][4] =
870 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
871 static const char month_names[12][4] =
872 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
873 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
880 time(¤t_time); /* get current time */
882 /* FIXME: is this needed? time() can't fail on Win32. What about Linux?
883 if(current_time <= 0)
889 current_time += time_offset;
891 /* get and save the gmt */
892 t = gmtime(¤t_time);
894 /* Format: "Sun, 06 Nov 1994 08:49:37 GMT" */
896 "%s, %02d %s %4d %02d:%02d:%02d GMT",
897 day_names[t->tm_wday],
899 month_names[t->tm_mon],
909 /*********************************************************************
911 * Function : finish_http_response
913 * Description : Fill in the missing headers in an http response,
914 * and flatten the headers to an http head.
917 * 1 : rsp = pointer to http_response to be processed
919 * Returns : http_response, or NULL on failiure
921 *********************************************************************/
922 struct http_response *finish_http_response(struct http_response *rsp)
924 char buf[BUFFER_SIZE];
927 * Fill in the HTTP Status
929 sprintf(buf, "HTTP/1.0 %s", rsp->status ? rsp->status : "200 OK");
930 enlist_first(rsp->headers, buf);
933 * Set the Content-Length
935 if (rsp->content_length == 0)
937 rsp->content_length = rsp->body ? strlen(rsp->body) : 0;
939 sprintf(buf, "Content-Length: %d", rsp->content_length);
940 enlist(rsp->headers, buf);
943 * Fill in the default headers:
945 * Content-Type: default to text/html if not already specified.
946 * Date: set to current date/time.
947 * Last-Modified: set to date/time the page was last changed.
948 * Expires: set to date/time page next needs reloading.
949 * Cache-Control: set to "no-cache" if applicable.
951 * See http://www.w3.org/Protocols/rfc2068/rfc2068
953 enlist_unique(rsp->headers, "Content-Type: text/html", 13);
958 * Set Expires to about 10 min into the future so it'll get reloaded
959 * occasionally, e.g. if IJB gets upgraded.
962 get_http_time(0, buf);
963 enlist_unique_header(rsp->headers, "Date", buf);
965 /* Some date in the past. */
966 enlist_unique_header(rsp->headers, "Last-Modified", "Sat, 17 Jun 2000 12:00:00 GMT");
968 get_http_time(10 * 60, buf); /* 10 * 60sec = 10 minutes */
969 enlist_unique_header(rsp->headers, "Expires", buf);
974 * Compliant browsers should not cache this due to the "Cache-Control"
975 * setting. However, to be certain, we also set both "Last-Modified"
976 * and "Expires" to the current time.
978 enlist_unique_header(rsp->headers, "Cache-Control", "no-cache");
979 get_http_time(0, buf);
980 enlist_unique_header(rsp->headers, "Date", buf);
981 enlist_unique_header(rsp->headers, "Last-Modified", buf);
982 enlist_unique_header(rsp->headers, "Expires", buf);
989 if (NULL == (rsp->head = list_to_text(rsp->headers)))
991 free_http_response(rsp);
994 rsp->head_length = strlen(rsp->head);
1001 /*********************************************************************
1003 * Function : free_http_response
1005 * Description : Free the memory occupied by an http_response
1006 * and its depandant structures.
1009 * 1 : rsp = pointer to http_response to be freed
1013 *********************************************************************/
1014 void free_http_response(struct http_response *rsp)
1021 destroy_list(rsp->headers);
1028 /*********************************************************************
1030 * Function : fill_template
1032 * Description : CGI support function that loads a given HTML
1033 * template from the confdir, and fills it in
1034 * by replacing @name@ with value using pcrs,
1035 * for each item in the output map.
1038 * 1 : csp = Current client state (buffers, headers, etc...)
1039 * 3 : template = name of the HTML template to be used
1040 * 2 : exports = map with fill in symbol -> name pairs
1042 * Returns : char * with filled out form, or NULL if failiure
1044 *********************************************************************/
1045 char *fill_template(struct client_state *csp, const char *templatename, struct map *exports)
1047 struct map_entry *m;
1049 char buf[BUFFER_SIZE];
1050 char *tmp_out_buffer;
1051 char *file_buffer = NULL;
1058 * Open template file or fail
1060 snprintf(buf, BUFFER_SIZE, "%s/templates/%s", csp->config->confdir, templatename);
1062 if(NULL == (fp = fopen(buf, "r")))
1064 log_error(LOG_LEVEL_ERROR, "error loading template %s: %E", buf);
1070 * Read the file, ignoring comments
1072 while (fgets(buf, BUFFER_SIZE, fp))
1074 /* skip lines starting with '#' */
1075 if(*buf == '#') continue;
1077 file_buffer = strsav(file_buffer, buf);
1085 size = strlen(file_buffer) + 1;
1088 * Assemble pcrs joblist from exports map
1090 for (m = exports->first; m != NULL; m = m->next)
1092 /* Enclose name in @@ */
1093 snprintf(buf, BUFFER_SIZE, "@%s@", m->name);
1095 /* Make and run job. */
1096 job = pcrs_compile(buf, m->value, "sigTU", &error);
1099 log_error(LOG_LEVEL_ERROR, "Error compiling template fill job %s: %d", m->name, error);
1103 pcrs_execute(job, file_buffer, size, &tmp_out_buffer, &size);
1104 if (file_buffer != tmp_out_buffer)
1107 file_buffer = tmp_out_buffer;
1117 return(file_buffer);
1122 /*********************************************************************
1124 * Function : default_exports
1126 * Description : returns a struct map list that contains exports
1127 * which are common to all CGI functions.
1130 * 1 : exports = Structure to write output to. This
1131 * structure should be newly allocated and will be
1133 * 1 : csp = Current client state (buffers, headers, etc...)
1134 * 2 : caller = name of CGI who calls us and which should
1135 * be excluded from the generated menu.
1136 * Returns : NULL if no memory, else map
1138 *********************************************************************/
1139 struct map * default_exports(const struct client_state *csp, const char *caller)
1142 struct map * exports = new_map();
1144 map(exports, "version", 1, VERSION, 1);
1145 map(exports, "my-ip-address", 1, csp->my_ip_addr_str ? csp->my_ip_addr_str : "unknown", 1);
1146 map(exports, "my-hostname", 1, csp->my_hostname ? csp->my_hostname : "unknown", 1);
1147 map(exports, "admin-address", 1, csp->config->admin_address ? csp->config->admin_address : "fill@me.in.please", 1);
1148 map(exports, "homepage", 1, HOME_PAGE_URL, 1);
1149 map(exports, "default-cgi", 1, HOME_PAGE_URL "/config", 1);
1150 map(exports, "menu", 1, make_menu(caller), 0);
1151 map(exports, "code-status", 1, CODE_STATUS, 1);
1153 snprintf(buf, 20, "%d", csp->config->hport);
1154 map(exports, "my-port", 1, buf, 1);
1156 if(!strcmp(CODE_STATUS, "stable"))
1158 map_block_killer(exports, "unstable");
1161 if(csp->config->proxy_info_url != NULL)
1163 map(exports, "proxy-info-url", 1, csp->config->proxy_info_url, 1);
1167 map_block_killer(exports, "have-proxy-info");
1174 /*********************************************************************
1176 * Function : map_block_killer
1178 * Description : Convenience function.
1179 * Adds a "killer" for the conditional HTML-template
1180 * block <name>, i.e. a substitution of the regex
1181 * "if-<name>-start.*if-<name>-end" to the given
1185 * 1 : exports = map to extend
1186 * 2 : name = name of conditional block
1188 * Returns : extended map
1190 *********************************************************************/
1191 void map_block_killer(struct map *exports, const char *name)
1193 char buf[1000]; /* Will do, since the names are hardwired */
1195 snprintf(buf, 1000, "if-%s-start.*if-%s-end", name, name);
1196 map(exports, buf, 1, "", 1);
1200 /*********************************************************************
1202 * Function : map_conditional
1204 * Description : Convenience function.
1205 * Adds an "if-then-else" for the conditional HTML-template
1206 * block <name>, i.e. a substitution of the form:
1213 * The control structure and one of the alternatives
1217 * 1 : exports = map to extend
1218 * 2 : name = name of conditional block
1219 * 3 : choose_first = nonzero for first, zero for second.
1221 * Returns : extended map
1223 *********************************************************************/
1224 void map_conditional(struct map *exports, const char *name, int choose_first)
1226 char buf[1000]; /* Will do, since the names are hardwired */
1228 snprintf(buf, 1000, (choose_first
1229 ? "else-not-%s@.*@endif-%s"
1230 : "if-%s-then@.*@else-not-%s"),
1232 map(exports, buf, 1, "", 1);
1234 snprintf(buf, 1000, (choose_first ? "if-%s-then" : "endif-%s"), name);
1235 map(exports, buf, 1, "", 1);
1239 /*********************************************************************
1241 * Function : make_menu
1243 * Description : Returns an HTML-formatted menu of the available
1244 * unhidden CGIs, excluding the one given in <self>.
1246 * Parameters : self = name of CGI to leave out, can be NULL
1248 * Returns : menu string
1250 *********************************************************************/
1251 char *make_menu(const char *self)
1253 const struct cgi_dispatcher *d;
1254 char buf[BUFFER_SIZE];
1255 char *result = NULL;
1259 self = "NO-SUCH-CGI!";
1262 /* List available unhidden CGI's and export as "other-cgis" */
1263 for (d = cgi_dispatcher; d->handler; d++)
1265 if (strncmp(d->description, "HIDE", 4) && strcmp(d->name, self))
1267 snprintf(buf, BUFFER_SIZE, "<li><a href=%s/config/%s>%s</a></li>\n",
1268 HOME_PAGE_URL, d->name, d->description);
1269 result = strsav(result, buf);
1277 /*********************************************************************
1279 * Function : dump_map
1281 * Description : HTML-dump a map for debugging
1284 * 1 : the_map = map to dump
1286 * Returns : string with HTML
1288 *********************************************************************/
1289 char *dump_map(const struct map *the_map)
1291 struct map_entry *cur_entry = the_map->first;
1294 ret = strsav(ret, "<table>\n");
1298 ret = strsav(ret, "<tr><td><b>");
1299 ret = strsav(ret, cur_entry->name);
1300 ret = strsav(ret, "</b></td><td>");
1301 ret = strsav(ret, cur_entry->value);
1302 ret = strsav(ret, "</td></tr>\n");
1303 cur_entry = cur_entry->next;
1306 ret = strsav(ret, "</table>\n");
1312 #ifdef FEATURE_STATISTICS
1313 /*********************************************************************
1315 * Function : add_stats
1317 * Description : Add the blocking statistics to a given map.
1320 * 1 : exports = map to write to.
1322 * Returns : pointer to extended map
1324 *********************************************************************/
1325 struct map *add_stats(struct map *exports)
1327 float perc_rej; /* Percentage of http requests rejected */
1329 int local_urls_read = urls_read;
1330 int local_urls_rejected = urls_rejected;
1333 * Need to alter the stats not to include the fetch of this
1336 * Can't do following thread safely! doh!
1339 * urls_rejected--; * This will be incremented subsequently *
1342 if (local_urls_read == 0)
1344 map_block_killer(exports, "have-stats");
1348 map_block_killer(exports, "have-no-stats");
1350 perc_rej = (float)local_urls_rejected * 100.0F /
1351 (float)local_urls_read;
1353 sprintf(buf, "%d", local_urls_read);
1354 map(exports, "requests-received", 1, buf, 1);
1356 sprintf(buf, "%d", local_urls_rejected);
1357 map(exports, "requests-blocked", 1, buf, 1);
1359 sprintf(buf, "%6.2f", perc_rej);
1360 map(exports, "percent-blocked", 1, buf, 1);
1366 #endif /* def FEATURE_STATISTICS */