1 const char cgisimple_rcs[] = "$Id: cgi.c,v 1.26 2001/09/16 15:47:37 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $
6 * Purpose : Simple CGIs to get information about JunkBuster's
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.
40 **********************************************************************/
46 #include <sys/types.h>
53 #define snprintf _snprintf
54 #endif /* def _WIN32 */
58 #include "cgisimple.h"
69 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
72 static char *show_rcs(void);
73 static void show_defines(struct map *exports);
76 /*********************************************************************
78 * Function : cgi_default
80 * Description : CGI function that is called if no action was given.
81 * Lists menu of available unhidden CGIs.
84 * 1 : csp = Current client state (buffers, headers, etc...)
85 * 2 : rsp = http_response data structure for output
86 * 3 : parameters = map of cgi parameters
90 *********************************************************************/
91 int cgi_default(struct client_state *csp, struct http_response *rsp,
92 struct map *parameters)
96 struct map * exports = default_exports(csp, "");
98 /* If there were other parameters, export a dump as "cgi-parameters" */
101 p = dump_map(parameters);
102 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
103 "Anyway, here they are, in case you're interested:</p>\n");
104 tmp = strsav(tmp, p);
105 map(exports, "cgi-parameters", 1, tmp, 0);
110 map(exports, "cgi-parameters", 1, "", 1);
113 rsp->body = template_load(csp, "default");
114 template_fill(&rsp->body, exports);
121 /*********************************************************************
123 * Function : cgi_send_banner
125 * Description : CGI function that returns a banner.
128 * 1 : csp = Current client state (buffers, headers, etc...)
129 * 2 : rsp = http_response data structure for output
130 * 3 : parameters = map of cgi parameters
133 * type : Selects the type of banner between "trans" and "jb".
134 * Defaults to "jb" if absent or != "trans".
138 *********************************************************************/
139 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
140 struct map *parameters)
142 if(strcmp(lookup(parameters, "type"), "trans"))
144 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
145 rsp->content_length = image_junkbuster_gif_length;
149 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
150 rsp->content_length = image_blank_gif_length;
153 enlist(rsp->headers, "Content-Type: image/gif");
161 /*********************************************************************
163 * Function : cgi_show_version
165 * Description : CGI function that returns a a web page describing the
166 * file versions of IJB.
169 * 1 : csp = Current client state (buffers, headers, etc...)
170 * 2 : rsp = http_response data structure for output
171 * 3 : parameters = map of cgi parameters
174 * type : Selects the type of banner between "trans" and "jb".
175 * Defaults to "jb" if absent or != "trans".
179 *********************************************************************/
180 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
181 struct map *parameters)
183 struct map * exports = default_exports(csp, "show-version");
185 map(exports, "sourceversions", 1, show_rcs(), 0);
187 rsp->body = template_load(csp, "show-version");
188 template_fill(&rsp->body, exports);
195 /*********************************************************************
197 * Function : cgi_show_status
199 * Description : CGI function that returns a a web page describing the
200 * current status of IJB.
203 * 1 : csp = Current client state (buffers, headers, etc...)
204 * 2 : rsp = http_response data structure for output
205 * 3 : parameters = map of cgi parameters
208 * type : Selects the type of banner between "trans" and "jb".
209 * Defaults to "jb" if absent or != "trans".
213 *********************************************************************/
214 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
215 struct map *parameters)
221 char buf[BUFFER_SIZE];
223 const char * filename = NULL;
224 char * file_description = NULL;
225 #ifdef FEATURE_STATISTICS
226 float perc_rej; /* Percentage of http requests rejected */
228 int local_urls_rejected;
229 #endif /* ndef FEATURE_STATISTICS */
231 struct map * exports = default_exports(csp, "show-status");
233 switch (*(lookup(parameters, "file")))
236 if (csp->actions_list)
238 filename = csp->actions_list->filename;
239 file_description = "Actions List";
246 filename = csp->rlist->filename;
247 file_description = "Regex Filter List";
255 filename = csp->tlist->filename;
256 file_description = "Trust List";
259 #endif /* def FEATURE_TRUST */
262 if (NULL != filename)
264 map(exports, "file-description", 1, file_description, 1);
265 map(exports, "filepath", 1, html_encode(filename), 0);
267 if ((fp = fopen(filename, "r")) == NULL)
269 map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
273 while (fgets(buf, sizeof(buf), fp))
275 p = html_encode(buf);
280 s = strsav(s, "<br>");
284 map(exports, "contents", 1, s, 0);
286 rsp->body = template_load(csp, "show-status-file");
287 template_fill(&rsp->body, exports);
293 map(exports, "redirect-url", 1, REDIRECT_URL, 1);
296 for (i=0; i < Argc; i++)
298 s = strsav(s, Argv[i]);
301 map(exports, "invocation", 1, s, 0);
303 map(exports, "options", 1, csp->config->proxy_args, 1);
304 show_defines(exports);
306 #ifdef FEATURE_STATISTICS
307 local_urls_read = urls_read;
308 local_urls_rejected = urls_rejected;
311 * Need to alter the stats not to include the fetch of this
314 * Can't do following thread safely! doh!
317 * urls_rejected--; * This will be incremented subsequently *
320 if (local_urls_read == 0)
322 map_block_killer(exports, "have-stats");
326 map_block_killer(exports, "have-no-stats");
328 perc_rej = (float)local_urls_rejected * 100.0F /
329 (float)local_urls_read;
331 sprintf(buf, "%d", local_urls_read);
332 map(exports, "requests-received", 1, buf, 1);
334 sprintf(buf, "%d", local_urls_rejected);
335 map(exports, "requests-blocked", 1, buf, 1);
337 sprintf(buf, "%6.2f", perc_rej);
338 map(exports, "percent-blocked", 1, buf, 1);
341 #else /* ndef FEATURE_STATISTICS */
342 map_block_killer(exports, "statistics");
343 #endif /* ndef FEATURE_STATISTICS */
345 if (csp->actions_list)
347 map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
351 map(exports, "actions-filename", 1, "None specified", 1);
356 map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
360 map(exports, "re-filter-filename", 1, "None specified", 1);
366 map(exports, "trust-filename", 1, csp->tlist->filename, 1);
370 map(exports, "trust-filename", 1, "None specified", 1);
373 map_block_killer(exports, "trust-support");
374 #endif /* ndef FEATURE_TRUST */
376 rsp->body = template_load(csp, "show-status");
377 template_fill(&rsp->body, exports);
384 /*********************************************************************
386 * Function : cgi_show_url_info
388 * Description : CGI function that determines and shows which actions
389 * junkbuster will perform for a given url, and which
390 * matches starting from the defaults have lead to that.
393 * 1 : csp = Current client state (buffers, headers, etc...)
394 * 2 : rsp = http_response data structure for output
395 * 3 : parameters = map of cgi parameters
398 * url : The url whose actions are to be determined.
399 * If url is unset, the url-given conditional will be
400 * set, so that all but the form can be suppressed in
405 *********************************************************************/
406 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
407 struct map *parameters)
411 struct map * exports = default_exports(csp, "show-url-info");
413 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
415 map_block_killer(exports, "url-given");
416 map(exports, "url", 1, "", 1);
420 char *matches = NULL;
425 struct file_list *fl;
426 struct url_actions *b;
427 struct url_spec url[1];
428 struct current_action_spec action[1];
431 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
433 map(exports, "url", 1, host, 1);
434 map(exports, "url-html", 1, html_encode(host), 0);
436 init_current_action(action);
438 s = current_action_to_text(action);
439 map(exports, "default", 1, s , 0);
441 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
443 map(exports, "matches", 1, "none" , 1);
444 map(exports, "final", 1, lookup(exports, "default"), 1);
447 free_current_action(action);
449 rsp->body = template_load(csp, "show-url-info");
450 template_fill(&rsp->body, exports);
456 s = strchr(host, '/');
466 s = strchr(host, ':');
476 /* if splitting the domain fails, punt */
477 if (url->dbuf == NULL)
479 map(exports, "matches", 1, "none" , 1);
480 map(exports, "final", 1, lookup(exports, "default"), 1);
484 free_current_action(action);
486 rsp->body = template_load(csp, "show-url-info");
487 template_fill(&rsp->body, exports);
493 for (b = b->next; NULL != b; b = b->next)
495 if ((b->url->port == 0) || (b->url->port == port))
497 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
499 if ((b->url->path == NULL) ||
501 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
503 (strncmp(b->url->path, path, b->url->pathlen) == 0)
507 s = actions_to_text(b->action);
508 matches = strsav(matches, "<b>{");
509 matches = strsav(matches, s);
510 matches = strsav(matches, " }</b><br>\n<code>");
511 matches = strsav(matches, b->url->spec);
512 matches = strsav(matches, "</code><br>\n<br>\n");
515 merge_current_action(action, b->action);
524 map(exports, "matches", 1, matches , 0);
528 map(exports, "matches", 1, "none", 1);
538 s = current_action_to_text(action);
539 map(exports, "final", 1, s, 0);
542 free_current_action(action);
545 rsp->body = template_load(csp, "show-url-info");
546 template_fill(&rsp->body, exports);
553 /*********************************************************************
555 * Function : cgi_robots_txt
557 * Description : CGI function to return "/robots.txt".
560 * 1 : csp = Current client state (buffers, headers, etc...)
561 * 2 : rsp = http_response data structure for output
562 * 3 : parameters = map of cgi parameters
564 * CGI Parameters : None
568 *********************************************************************/
569 int cgi_robots_txt(struct client_state *csp, struct http_response *rsp,
570 struct map *parameters)
575 "# This is the Internet Junkbuster control interface.\n"
576 "# It isn't very useful to index it, and you're likely to break stuff.\n"
583 enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
587 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
588 enlist_unique_header(rsp->headers, "Expires", buf);
594 /*********************************************************************
596 * Function : show_defines
598 * Description : Create a string with all conditional #defines used
605 *********************************************************************/
606 static void show_defines(struct map *exports)
610 map_conditional(exports, "FEATURE_ACL", 1);
611 #else /* ifndef FEATURE_ACL */
612 map_conditional(exports, "FEATURE_ACL", 0);
613 #endif /* ndef FEATURE_ACL */
615 #ifdef FEATURE_COOKIE_JAR
616 map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
617 #else /* ifndef FEATURE_COOKIE_JAR */
618 map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
619 #endif /* ndef FEATURE_COOKIE_JAR */
621 #ifdef FEATURE_DENY_GZIP
622 map_conditional(exports, "FEATURE_DENY_GZIP", 1);
623 #else /* ifndef FEATURE_DENY_GZIP */
624 map_conditional(exports, "FEATURE_DENY_GZIP", 0);
625 #endif /* ndef FEATURE_DENY_GZIP */
627 #ifdef FEATURE_FAST_REDIRECTS
628 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
629 #else /* ifndef FEATURE_FAST_REDIRECTS */
630 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
631 #endif /* ndef FEATURE_FAST_REDIRECTS */
633 #ifdef FEATURE_FORCE_LOAD
634 map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
635 #else /* ifndef FEATURE_FORCE_LOAD */
636 map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
637 #endif /* ndef FEATURE_FORCE_LOAD */
639 #ifdef FEATURE_IMAGE_BLOCKING
640 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
641 #else /* ifndef FEATURE_IMAGE_BLOCKING */
642 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
643 #endif /* ndef FEATURE_IMAGE_BLOCKING */
645 #ifdef FEATURE_IMAGE_DETECT_MSIE
646 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
647 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
648 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
649 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
651 #ifdef FEATURE_KILL_POPUPS
652 map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
653 #else /* ifndef FEATURE_KILL_POPUPS */
654 map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
655 #endif /* ndef FEATURE_KILL_POPUPS */
657 #ifdef FEATURE_PTHREAD
658 map_conditional(exports, "FEATURE_PTHREAD", 1);
659 #else /* ifndef FEATURE_PTHREAD */
660 map_conditional(exports, "FEATURE_PTHREAD", 0);
661 #endif /* ndef FEATURE_PTHREAD */
663 #ifdef FEATURE_STATISTICS
664 map_conditional(exports, "FEATURE_STATISTICS", 1);
665 #else /* ifndef FEATURE_STATISTICS */
666 map_conditional(exports, "FEATURE_STATISTICS", 0);
667 #endif /* ndef FEATURE_STATISTICS */
669 #ifdef FEATURE_TOGGLE
670 map_conditional(exports, "FEATURE_TOGGLE", 1);
671 #else /* ifndef FEATURE_TOGGLE */
672 map_conditional(exports, "FEATURE_TOGGLE", 0);
673 #endif /* ndef FEATURE_TOGGLE */
676 map_conditional(exports, "FEATURE_TRUST", 1);
677 #else /* ifndef FEATURE_TRUST */
678 map_conditional(exports, "FEATURE_TRUST", 0);
679 #endif /* ndef FEATURE_TRUST */
682 map_conditional(exports, "REGEX_GNU", 1);
683 #else /* ifndef REGEX_GNU */
684 map_conditional(exports, "REGEX_GNU", 0);
685 #endif /* def REGEX_GNU */
688 map_conditional(exports, "REGEX_PCRE", 1);
689 #else /* ifndef REGEX_PCRE */
690 map_conditional(exports, "REGEX_PCRE", 0);
691 #endif /* def REGEX_PCRE */
694 map_conditional(exports, "STATIC_PCRE", 1);
695 #else /* ifndef STATIC_PCRE */
696 map_conditional(exports, "STATIC_PCRE", 0);
697 #endif /* ndef STATIC_PCRE */
700 map_conditional(exports, "STATIC_PCRS", 1);
701 #else /* ifndef STATIC_PCRS */
702 map_conditional(exports, "STATIC_PCRS", 0);
703 #endif /* ndef STATIC_PCRS */
705 map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
709 /*********************************************************************
711 * Function : show_rcs
713 * Description : Create a string with the rcs info for all sourcefiles
719 *********************************************************************/
720 static char *show_rcs(void)
723 char buf[BUFFER_SIZE];
725 /* Instead of including *all* dot h's in the project (thus creating a
726 * tremendous amount of dependencies), I will concede to declaring them
727 * as extern's. This forces the developer to add to this list, but oh well.
730 #define SHOW_RCS(__x) \
732 extern const char __x[]; \
733 sprintf(buf, "%s\n", __x); \
734 b = strsav(b, buf); \
737 /* In alphabetical order */
738 SHOW_RCS(actions_h_rcs)
739 SHOW_RCS(actions_rcs)
742 #ifdef FEATURE_CGI_EDIT_ACTIONS
743 SHOW_RCS(cgiedit_h_rcs)
744 SHOW_RCS(cgiedit_rcs)
745 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
746 SHOW_RCS(cgisimple_h_rcs)
747 SHOW_RCS(cgisimple_rcs)
749 SHOW_RCS(cygwin_h_rcs)
751 SHOW_RCS(deanimate_h_rcs)
752 SHOW_RCS(deanimate_rcs)
753 SHOW_RCS(encode_h_rcs)
755 SHOW_RCS(errlog_h_rcs)
757 SHOW_RCS(filters_h_rcs)
758 SHOW_RCS(filters_rcs)
759 SHOW_RCS(gateway_h_rcs)
760 SHOW_RCS(gateway_rcs)
762 SHOW_RCS(gnu_regex_h_rcs)
763 SHOW_RCS(gnu_regex_rcs)
764 #endif /* def GNU_REGEX */
765 SHOW_RCS(jbsockets_h_rcs)
766 SHOW_RCS(jbsockets_rcs)
769 #ifdef FEATURE_KILL_POPUPS
770 SHOW_RCS(killpopup_h_rcs)
771 SHOW_RCS(killpopup_rcs)
772 #endif /* def FEATURE_KILL_POPUPS */
775 SHOW_RCS(loadcfg_h_rcs)
776 SHOW_RCS(loadcfg_rcs)
777 SHOW_RCS(loaders_h_rcs)
778 SHOW_RCS(loaders_rcs)
779 SHOW_RCS(miscutil_h_rcs)
780 SHOW_RCS(miscutil_rcs)
781 SHOW_RCS(parsers_h_rcs)
782 SHOW_RCS(parsers_rcs)
785 SHOW_RCS(project_h_rcs)
786 SHOW_RCS(ssplit_h_rcs)
790 SHOW_RCS(w32log_h_rcs)
792 SHOW_RCS(w32res_h_rcs)
793 SHOW_RCS(w32taskbar_h_rcs)
794 SHOW_RCS(w32taskbar_rcs)
795 #endif /* ndef _WIN_CONSOLE */
796 SHOW_RCS(win32_h_rcs)
798 #endif /* def _WIN32 */