1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.1 2001/09/16 17:08:54 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgisimple.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.
38 * $Log: cgisimple.c,v $
39 * Revision 1.1 2001/09/16 17:08:54 jongfoster
40 * Moving simple CGI functions from cgi.c to new file cgisimple.c
43 **********************************************************************/
49 #include <sys/types.h>
56 #define snprintf _snprintf
57 #endif /* def _WIN32 */
61 #include "cgisimple.h"
72 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
75 static char *show_rcs(void);
76 static void show_defines(struct map *exports);
79 /*********************************************************************
81 * Function : cgi_default
83 * Description : CGI function that is called if no action was given.
84 * Lists menu of available unhidden CGIs.
87 * 1 : csp = Current client state (buffers, headers, etc...)
88 * 2 : rsp = http_response data structure for output
89 * 3 : parameters = map of cgi parameters
93 *********************************************************************/
94 int cgi_default(struct client_state *csp, struct http_response *rsp,
95 struct map *parameters)
99 struct map * exports = default_exports(csp, "");
101 /* If there were other parameters, export a dump as "cgi-parameters" */
104 p = dump_map(parameters);
105 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
106 "Anyway, here they are, in case you're interested:</p>\n");
107 tmp = strsav(tmp, p);
108 map(exports, "cgi-parameters", 1, tmp, 0);
113 map(exports, "cgi-parameters", 1, "", 1);
116 rsp->body = template_load(csp, "default");
117 template_fill(&rsp->body, exports);
124 /*********************************************************************
126 * Function : cgi_send_banner
128 * Description : CGI function that returns a banner.
131 * 1 : csp = Current client state (buffers, headers, etc...)
132 * 2 : rsp = http_response data structure for output
133 * 3 : parameters = map of cgi parameters
136 * type : Selects the type of banner between "trans" and "jb".
137 * Defaults to "jb" if absent or != "trans".
141 *********************************************************************/
142 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
143 struct map *parameters)
145 if(strcmp(lookup(parameters, "type"), "trans"))
147 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
148 rsp->content_length = image_junkbuster_gif_length;
152 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
153 rsp->content_length = image_blank_gif_length;
156 enlist(rsp->headers, "Content-Type: image/gif");
164 /*********************************************************************
166 * Function : cgi_show_version
168 * Description : CGI function that returns a a web page describing the
169 * file versions of IJB.
172 * 1 : csp = Current client state (buffers, headers, etc...)
173 * 2 : rsp = http_response data structure for output
174 * 3 : parameters = map of cgi parameters
176 * CGI Parameters : none
180 *********************************************************************/
181 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
182 struct map *parameters)
184 struct map * exports = default_exports(csp, "show-version");
186 map(exports, "sourceversions", 1, show_rcs(), 0);
188 rsp->body = template_load(csp, "show-version");
189 template_fill(&rsp->body, exports);
197 /*********************************************************************
199 * Function : cgi_show_status
201 * Description : CGI function that returns a a web page describing the
202 * current status of IJB.
205 * 1 : csp = Current client state (buffers, headers, etc...)
206 * 2 : rsp = http_response data structure for output
207 * 3 : parameters = map of cgi parameters
209 * CGI Parameters : none
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);
595 /*********************************************************************
597 * Function : show_defines
599 * Description : Create a string with all conditional #defines used
606 *********************************************************************/
607 static void show_defines(struct map *exports)
611 map_conditional(exports, "FEATURE_ACL", 1);
612 #else /* ifndef FEATURE_ACL */
613 map_conditional(exports, "FEATURE_ACL", 0);
614 #endif /* ndef FEATURE_ACL */
616 #ifdef FEATURE_COOKIE_JAR
617 map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
618 #else /* ifndef FEATURE_COOKIE_JAR */
619 map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
620 #endif /* ndef FEATURE_COOKIE_JAR */
622 #ifdef FEATURE_DENY_GZIP
623 map_conditional(exports, "FEATURE_DENY_GZIP", 1);
624 #else /* ifndef FEATURE_DENY_GZIP */
625 map_conditional(exports, "FEATURE_DENY_GZIP", 0);
626 #endif /* ndef FEATURE_DENY_GZIP */
628 #ifdef FEATURE_FAST_REDIRECTS
629 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
630 #else /* ifndef FEATURE_FAST_REDIRECTS */
631 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
632 #endif /* ndef FEATURE_FAST_REDIRECTS */
634 #ifdef FEATURE_FORCE_LOAD
635 map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
636 #else /* ifndef FEATURE_FORCE_LOAD */
637 map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
638 #endif /* ndef FEATURE_FORCE_LOAD */
640 #ifdef FEATURE_IMAGE_BLOCKING
641 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
642 #else /* ifndef FEATURE_IMAGE_BLOCKING */
643 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
644 #endif /* ndef FEATURE_IMAGE_BLOCKING */
646 #ifdef FEATURE_IMAGE_DETECT_MSIE
647 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
648 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
649 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
650 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
652 #ifdef FEATURE_KILL_POPUPS
653 map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
654 #else /* ifndef FEATURE_KILL_POPUPS */
655 map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
656 #endif /* ndef FEATURE_KILL_POPUPS */
658 #ifdef FEATURE_PTHREAD
659 map_conditional(exports, "FEATURE_PTHREAD", 1);
660 #else /* ifndef FEATURE_PTHREAD */
661 map_conditional(exports, "FEATURE_PTHREAD", 0);
662 #endif /* ndef FEATURE_PTHREAD */
664 #ifdef FEATURE_STATISTICS
665 map_conditional(exports, "FEATURE_STATISTICS", 1);
666 #else /* ifndef FEATURE_STATISTICS */
667 map_conditional(exports, "FEATURE_STATISTICS", 0);
668 #endif /* ndef FEATURE_STATISTICS */
670 #ifdef FEATURE_TOGGLE
671 map_conditional(exports, "FEATURE_TOGGLE", 1);
672 #else /* ifndef FEATURE_TOGGLE */
673 map_conditional(exports, "FEATURE_TOGGLE", 0);
674 #endif /* ndef FEATURE_TOGGLE */
677 map_conditional(exports, "FEATURE_TRUST", 1);
678 #else /* ifndef FEATURE_TRUST */
679 map_conditional(exports, "FEATURE_TRUST", 0);
680 #endif /* ndef FEATURE_TRUST */
683 map_conditional(exports, "REGEX_GNU", 1);
684 #else /* ifndef REGEX_GNU */
685 map_conditional(exports, "REGEX_GNU", 0);
686 #endif /* def REGEX_GNU */
689 map_conditional(exports, "REGEX_PCRE", 1);
690 #else /* ifndef REGEX_PCRE */
691 map_conditional(exports, "REGEX_PCRE", 0);
692 #endif /* def REGEX_PCRE */
695 map_conditional(exports, "STATIC_PCRE", 1);
696 #else /* ifndef STATIC_PCRE */
697 map_conditional(exports, "STATIC_PCRE", 0);
698 #endif /* ndef STATIC_PCRE */
701 map_conditional(exports, "STATIC_PCRS", 1);
702 #else /* ifndef STATIC_PCRS */
703 map_conditional(exports, "STATIC_PCRS", 0);
704 #endif /* ndef STATIC_PCRS */
706 map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
711 /*********************************************************************
713 * Function : show_rcs
715 * Description : Create a string with the rcs info for all sourcefiles
721 *********************************************************************/
722 static char *show_rcs(void)
725 char buf[BUFFER_SIZE];
727 /* Instead of including *all* dot h's in the project (thus creating a
728 * tremendous amount of dependencies), I will concede to declaring them
729 * as extern's. This forces the developer to add to this list, but oh well.
732 #define SHOW_RCS(__x) \
734 extern const char __x[]; \
735 sprintf(buf, "%s\n", __x); \
736 b = strsav(b, buf); \
739 /* In alphabetical order */
740 SHOW_RCS(actions_h_rcs)
741 SHOW_RCS(actions_rcs)
744 #ifdef FEATURE_CGI_EDIT_ACTIONS
745 SHOW_RCS(cgiedit_h_rcs)
746 SHOW_RCS(cgiedit_rcs)
747 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
748 SHOW_RCS(cgisimple_h_rcs)
749 SHOW_RCS(cgisimple_rcs)
751 SHOW_RCS(cygwin_h_rcs)
753 SHOW_RCS(deanimate_h_rcs)
754 SHOW_RCS(deanimate_rcs)
755 SHOW_RCS(encode_h_rcs)
757 SHOW_RCS(errlog_h_rcs)
759 SHOW_RCS(filters_h_rcs)
760 SHOW_RCS(filters_rcs)
761 SHOW_RCS(gateway_h_rcs)
762 SHOW_RCS(gateway_rcs)
764 SHOW_RCS(gnu_regex_h_rcs)
765 SHOW_RCS(gnu_regex_rcs)
766 #endif /* def GNU_REGEX */
767 SHOW_RCS(jbsockets_h_rcs)
768 SHOW_RCS(jbsockets_rcs)
771 #ifdef FEATURE_KILL_POPUPS
772 SHOW_RCS(killpopup_h_rcs)
773 SHOW_RCS(killpopup_rcs)
774 #endif /* def FEATURE_KILL_POPUPS */
777 SHOW_RCS(loadcfg_h_rcs)
778 SHOW_RCS(loadcfg_rcs)
779 SHOW_RCS(loaders_h_rcs)
780 SHOW_RCS(loaders_rcs)
781 SHOW_RCS(miscutil_h_rcs)
782 SHOW_RCS(miscutil_rcs)
783 SHOW_RCS(parsers_h_rcs)
784 SHOW_RCS(parsers_rcs)
787 SHOW_RCS(project_h_rcs)
788 SHOW_RCS(ssplit_h_rcs)
792 SHOW_RCS(w32log_h_rcs)
794 SHOW_RCS(w32res_h_rcs)
795 SHOW_RCS(w32taskbar_h_rcs)
796 SHOW_RCS(w32taskbar_rcs)
797 #endif /* ndef _WIN_CONSOLE */
798 SHOW_RCS(win32_h_rcs)
800 #endif /* def _WIN32 */