1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.4 2001/10/02 15:31:12 oes 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.4 2001/10/02 15:31:12 oes
40 * Introduced show-request cgi
42 * Revision 1.3 2001/09/22 16:34:44 jongfoster
43 * Removing unneeded #includes
45 * Revision 1.2 2001/09/19 18:01:11 oes
46 * Fixed comments; cosmetics
48 * Revision 1.1 2001/09/16 17:08:54 jongfoster
49 * Moving simple CGI functions from cgi.c to new file cgisimple.c
52 **********************************************************************/
58 #include <sys/types.h>
65 #define snprintf _snprintf
66 #endif /* def _WIN32 */
70 #include "cgisimple.h"
80 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
83 static char *show_rcs(void);
84 static void show_defines(struct map *exports);
87 /*********************************************************************
89 * Function : cgi_default
91 * Description : CGI function that is called if no action was given.
92 * Lists menu of available unhidden CGIs.
95 * 1 : csp = Current client state (buffers, headers, etc...)
96 * 2 : rsp = http_response data structure for output
97 * 3 : parameters = map of cgi parameters
101 *********************************************************************/
102 int cgi_default(struct client_state *csp, struct http_response *rsp,
103 struct map *parameters)
107 struct map *exports = default_exports(csp, "");
109 /* If there were other parameters, export a dump as "cgi-parameters" */
112 p = dump_map(parameters);
113 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
114 "Anyway, here they are, in case you're interested:</p>\n");
115 tmp = strsav(tmp, p);
116 map(exports, "cgi-parameters", 1, tmp, 0);
121 map(exports, "cgi-parameters", 1, "", 1);
124 rsp->body = template_load(csp, "default");
125 template_fill(&rsp->body, exports);
132 /*********************************************************************
134 * Function : cgi_show_request
136 * Description : Show the client's request and what sed() would have
140 * 1 : csp = Current client state (buffers, headers, etc...)
141 * 2 : rsp = http_response data structure for output
142 * 3 : parameters = map of cgi parameters
146 *********************************************************************/
147 int cgi_show_request(struct client_state *csp, struct http_response *rsp,
148 struct map *parameters)
151 struct map *exports = default_exports(csp, "show-request");
154 * Repair the damage done to the IOB by get_header()
156 for (p = csp->iob->buf; p < csp->iob->eod; p++)
158 if (*p == '\0') *p = '\n';
162 * Export the original client's request and the one we would
163 * be sending to the server if this wasn't a CGI call
165 map(exports, "client-request", 1, csp->iob->buf, 1);
166 map(exports, "processed-request", 1, sed(client_patterns, add_client_headers, csp), 0);
168 rsp->body = template_load(csp, "show-request");
169 template_fill(&rsp->body, exports);
176 /*********************************************************************
178 * Function : cgi_send_banner
180 * Description : CGI function that returns a banner.
183 * 1 : csp = Current client state (buffers, headers, etc...)
184 * 2 : rsp = http_response data structure for output
185 * 3 : parameters = map of cgi parameters
188 * type : Selects the type of banner between "trans" and "jb".
189 * Defaults to "jb" if absent or != "trans".
193 *********************************************************************/
194 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
195 struct map *parameters)
197 if(strcmp(lookup(parameters, "type"), "trans"))
199 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
200 rsp->content_length = image_junkbuster_gif_length;
204 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
205 rsp->content_length = image_blank_gif_length;
208 enlist(rsp->headers, "Content-Type: image/gif");
216 /*********************************************************************
218 * Function : cgi_show_version
220 * Description : CGI function that returns a a web page describing the
221 * file versions of IJB.
224 * 1 : csp = Current client state (buffers, headers, etc...)
225 * 2 : rsp = http_response data structure for output
226 * 3 : parameters = map of cgi parameters
228 * CGI Parameters : none
232 *********************************************************************/
233 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
234 struct map *parameters)
236 struct map * exports = default_exports(csp, "show-version");
238 map(exports, "sourceversions", 1, show_rcs(), 0);
240 rsp->body = template_load(csp, "show-version");
241 template_fill(&rsp->body, exports);
249 /*********************************************************************
251 * Function : cgi_show_status
253 * Description : CGI function that returns a a web page describing the
254 * current status of IJB.
257 * 1 : csp = Current client state (buffers, headers, etc...)
258 * 2 : rsp = http_response data structure for output
259 * 3 : parameters = map of cgi parameters
261 * CGI Parameters : none
265 *********************************************************************/
266 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
267 struct map *parameters)
273 char buf[BUFFER_SIZE];
275 const char * filename = NULL;
276 char * file_description = NULL;
277 #ifdef FEATURE_STATISTICS
278 float perc_rej; /* Percentage of http requests rejected */
280 int local_urls_rejected;
281 #endif /* ndef FEATURE_STATISTICS */
283 struct map * exports = default_exports(csp, "show-status");
285 switch (*(lookup(parameters, "file")))
288 if (csp->actions_list)
290 filename = csp->actions_list->filename;
291 file_description = "Actions List";
298 filename = csp->rlist->filename;
299 file_description = "Regex Filter List";
307 filename = csp->tlist->filename;
308 file_description = "Trust List";
311 #endif /* def FEATURE_TRUST */
314 if (NULL != filename)
316 map(exports, "file-description", 1, file_description, 1);
317 map(exports, "filepath", 1, html_encode(filename), 0);
319 if ((fp = fopen(filename, "r")) == NULL)
321 map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
325 while (fgets(buf, sizeof(buf), fp))
327 p = html_encode(buf);
332 s = strsav(s, "<br>");
336 map(exports, "contents", 1, s, 0);
338 rsp->body = template_load(csp, "show-status-file");
339 template_fill(&rsp->body, exports);
345 map(exports, "redirect-url", 1, REDIRECT_URL, 1);
348 for (i=0; i < Argc; i++)
350 s = strsav(s, Argv[i]);
353 map(exports, "invocation", 1, s, 0);
355 map(exports, "options", 1, csp->config->proxy_args, 1);
356 show_defines(exports);
358 #ifdef FEATURE_STATISTICS
359 local_urls_read = urls_read;
360 local_urls_rejected = urls_rejected;
363 * Need to alter the stats not to include the fetch of this
366 * Can't do following thread safely! doh!
369 * urls_rejected--; * This will be incremented subsequently *
372 if (local_urls_read == 0)
374 map_block_killer(exports, "have-stats");
378 map_block_killer(exports, "have-no-stats");
380 perc_rej = (float)local_urls_rejected * 100.0F /
381 (float)local_urls_read;
383 sprintf(buf, "%d", local_urls_read);
384 map(exports, "requests-received", 1, buf, 1);
386 sprintf(buf, "%d", local_urls_rejected);
387 map(exports, "requests-blocked", 1, buf, 1);
389 sprintf(buf, "%6.2f", perc_rej);
390 map(exports, "percent-blocked", 1, buf, 1);
393 #else /* ndef FEATURE_STATISTICS */
394 map_block_killer(exports, "statistics");
395 #endif /* ndef FEATURE_STATISTICS */
397 if (csp->actions_list)
399 map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
403 map(exports, "actions-filename", 1, "None specified", 1);
408 map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
412 map(exports, "re-filter-filename", 1, "None specified", 1);
418 map(exports, "trust-filename", 1, csp->tlist->filename, 1);
422 map(exports, "trust-filename", 1, "None specified", 1);
425 map_block_killer(exports, "trust-support");
426 #endif /* ndef FEATURE_TRUST */
428 rsp->body = template_load(csp, "show-status");
429 template_fill(&rsp->body, exports);
436 /*********************************************************************
438 * Function : cgi_show_url_info
440 * Description : CGI function that determines and shows which actions
441 * junkbuster will perform for a given url, and which
442 * matches starting from the defaults have lead to that.
445 * 1 : csp = Current client state (buffers, headers, etc...)
446 * 2 : rsp = http_response data structure for output
447 * 3 : parameters = map of cgi parameters
450 * url : The url whose actions are to be determined.
451 * If url is unset, the url-given conditional will be
452 * set, so that all but the form can be suppressed in
457 *********************************************************************/
458 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
459 struct map *parameters)
463 struct map * exports = default_exports(csp, "show-url-info");
465 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
467 map_block_killer(exports, "url-given");
468 map(exports, "url", 1, "", 1);
472 char *matches = NULL;
477 struct file_list *fl;
478 struct url_actions *b;
479 struct url_spec url[1];
480 struct current_action_spec action[1];
483 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
485 map(exports, "url", 1, host, 1);
486 map(exports, "url-html", 1, html_encode(host), 0);
488 init_current_action(action);
490 s = current_action_to_text(action);
491 map(exports, "default", 1, s , 0);
493 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
495 map(exports, "matches", 1, "none" , 1);
496 map(exports, "final", 1, lookup(exports, "default"), 1);
499 free_current_action(action);
501 rsp->body = template_load(csp, "show-url-info");
502 template_fill(&rsp->body, exports);
508 s = strchr(host, '/');
518 s = strchr(host, ':');
528 /* if splitting the domain fails, punt */
529 if (url->dbuf == NULL)
531 map(exports, "matches", 1, "none" , 1);
532 map(exports, "final", 1, lookup(exports, "default"), 1);
536 free_current_action(action);
538 rsp->body = template_load(csp, "show-url-info");
539 template_fill(&rsp->body, exports);
545 for (b = b->next; NULL != b; b = b->next)
547 if ((b->url->port == 0) || (b->url->port == port))
549 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
551 if ((b->url->path == NULL) ||
553 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
555 (strncmp(b->url->path, path, b->url->pathlen) == 0)
559 s = actions_to_text(b->action);
560 matches = strsav(matches, "<b>{");
561 matches = strsav(matches, s);
562 matches = strsav(matches, " }</b><br>\n<code>");
563 matches = strsav(matches, b->url->spec);
564 matches = strsav(matches, "</code><br>\n<br>\n");
567 merge_current_action(action, b->action);
576 map(exports, "matches", 1, matches , 0);
580 map(exports, "matches", 1, "none", 1);
590 s = current_action_to_text(action);
591 map(exports, "final", 1, s, 0);
594 free_current_action(action);
597 rsp->body = template_load(csp, "show-url-info");
598 template_fill(&rsp->body, exports);
605 /*********************************************************************
607 * Function : cgi_robots_txt
609 * Description : CGI function to return "/robots.txt".
612 * 1 : csp = Current client state (buffers, headers, etc...)
613 * 2 : rsp = http_response data structure for output
614 * 3 : parameters = map of cgi parameters
616 * CGI Parameters : None
620 *********************************************************************/
621 int cgi_robots_txt(struct client_state *csp, struct http_response *rsp,
622 struct map *parameters)
627 "# This is the Internet Junkbuster control interface.\n"
628 "# It isn't very useful to index it, and you're likely to break stuff.\n"
635 enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
639 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
640 enlist_unique_header(rsp->headers, "Expires", buf);
647 /*********************************************************************
649 * Function : show_defines
651 * Description : Create a string with all conditional #defines used
658 *********************************************************************/
659 static void show_defines(struct map *exports)
663 map_conditional(exports, "FEATURE_ACL", 1);
664 #else /* ifndef FEATURE_ACL */
665 map_conditional(exports, "FEATURE_ACL", 0);
666 #endif /* ndef FEATURE_ACL */
668 #ifdef FEATURE_COOKIE_JAR
669 map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
670 #else /* ifndef FEATURE_COOKIE_JAR */
671 map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
672 #endif /* ndef FEATURE_COOKIE_JAR */
674 #ifdef FEATURE_FAST_REDIRECTS
675 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
676 #else /* ifndef FEATURE_FAST_REDIRECTS */
677 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
678 #endif /* ndef FEATURE_FAST_REDIRECTS */
680 #ifdef FEATURE_FORCE_LOAD
681 map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
682 #else /* ifndef FEATURE_FORCE_LOAD */
683 map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
684 #endif /* ndef FEATURE_FORCE_LOAD */
686 #ifdef FEATURE_IMAGE_BLOCKING
687 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
688 #else /* ifndef FEATURE_IMAGE_BLOCKING */
689 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
690 #endif /* ndef FEATURE_IMAGE_BLOCKING */
692 #ifdef FEATURE_IMAGE_DETECT_MSIE
693 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
694 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
695 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
696 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
698 #ifdef FEATURE_KILL_POPUPS
699 map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
700 #else /* ifndef FEATURE_KILL_POPUPS */
701 map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
702 #endif /* ndef FEATURE_KILL_POPUPS */
704 #ifdef FEATURE_PTHREAD
705 map_conditional(exports, "FEATURE_PTHREAD", 1);
706 #else /* ifndef FEATURE_PTHREAD */
707 map_conditional(exports, "FEATURE_PTHREAD", 0);
708 #endif /* ndef FEATURE_PTHREAD */
710 #ifdef FEATURE_STATISTICS
711 map_conditional(exports, "FEATURE_STATISTICS", 1);
712 #else /* ifndef FEATURE_STATISTICS */
713 map_conditional(exports, "FEATURE_STATISTICS", 0);
714 #endif /* ndef FEATURE_STATISTICS */
716 #ifdef FEATURE_TOGGLE
717 map_conditional(exports, "FEATURE_TOGGLE", 1);
718 #else /* ifndef FEATURE_TOGGLE */
719 map_conditional(exports, "FEATURE_TOGGLE", 0);
720 #endif /* ndef FEATURE_TOGGLE */
723 map_conditional(exports, "FEATURE_TRUST", 1);
724 #else /* ifndef FEATURE_TRUST */
725 map_conditional(exports, "FEATURE_TRUST", 0);
726 #endif /* ndef FEATURE_TRUST */
729 map_conditional(exports, "REGEX_GNU", 1);
730 #else /* ifndef REGEX_GNU */
731 map_conditional(exports, "REGEX_GNU", 0);
732 #endif /* def REGEX_GNU */
735 map_conditional(exports, "REGEX_PCRE", 1);
736 #else /* ifndef REGEX_PCRE */
737 map_conditional(exports, "REGEX_PCRE", 0);
738 #endif /* def REGEX_PCRE */
741 map_conditional(exports, "STATIC_PCRE", 1);
742 #else /* ifndef STATIC_PCRE */
743 map_conditional(exports, "STATIC_PCRE", 0);
744 #endif /* ndef STATIC_PCRE */
747 map_conditional(exports, "STATIC_PCRS", 1);
748 #else /* ifndef STATIC_PCRS */
749 map_conditional(exports, "STATIC_PCRS", 0);
750 #endif /* ndef STATIC_PCRS */
752 map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
757 /*********************************************************************
759 * Function : show_rcs
761 * Description : Create a string with the rcs info for all sourcefiles
767 *********************************************************************/
768 static char *show_rcs(void)
771 char buf[BUFFER_SIZE];
773 /* Instead of including *all* dot h's in the project (thus creating a
774 * tremendous amount of dependencies), I will concede to declaring them
775 * as extern's. This forces the developer to add to this list, but oh well.
778 #define SHOW_RCS(__x) \
780 extern const char __x[]; \
781 sprintf(buf, "%s\n", __x); \
782 b = strsav(b, buf); \
785 /* In alphabetical order */
786 SHOW_RCS(actions_h_rcs)
787 SHOW_RCS(actions_rcs)
790 #ifdef FEATURE_CGI_EDIT_ACTIONS
791 SHOW_RCS(cgiedit_h_rcs)
792 SHOW_RCS(cgiedit_rcs)
793 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
794 SHOW_RCS(cgisimple_h_rcs)
795 SHOW_RCS(cgisimple_rcs)
797 SHOW_RCS(cygwin_h_rcs)
799 SHOW_RCS(deanimate_h_rcs)
800 SHOW_RCS(deanimate_rcs)
801 SHOW_RCS(encode_h_rcs)
803 SHOW_RCS(errlog_h_rcs)
805 SHOW_RCS(filters_h_rcs)
806 SHOW_RCS(filters_rcs)
807 SHOW_RCS(gateway_h_rcs)
808 SHOW_RCS(gateway_rcs)
810 SHOW_RCS(gnu_regex_h_rcs)
811 SHOW_RCS(gnu_regex_rcs)
812 #endif /* def GNU_REGEX */
813 SHOW_RCS(jbsockets_h_rcs)
814 SHOW_RCS(jbsockets_rcs)
817 #ifdef FEATURE_KILL_POPUPS
818 SHOW_RCS(killpopup_h_rcs)
819 SHOW_RCS(killpopup_rcs)
820 #endif /* def FEATURE_KILL_POPUPS */
823 SHOW_RCS(loadcfg_h_rcs)
824 SHOW_RCS(loadcfg_rcs)
825 SHOW_RCS(loaders_h_rcs)
826 SHOW_RCS(loaders_rcs)
827 SHOW_RCS(miscutil_h_rcs)
828 SHOW_RCS(miscutil_rcs)
829 SHOW_RCS(parsers_h_rcs)
830 SHOW_RCS(parsers_rcs)
833 SHOW_RCS(project_h_rcs)
834 SHOW_RCS(ssplit_h_rcs)
838 SHOW_RCS(w32log_h_rcs)
840 SHOW_RCS(w32res_h_rcs)
841 SHOW_RCS(w32taskbar_h_rcs)
842 SHOW_RCS(w32taskbar_rcs)
843 #endif /* ndef _WIN_CONSOLE */
844 SHOW_RCS(win32_h_rcs)
846 #endif /* def _WIN32 */