1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.2 2001/09/19 18:01:11 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.2 2001/09/19 18:01:11 oes
40 * Fixed comments; cosmetics
42 * Revision 1.1 2001/09/16 17:08:54 jongfoster
43 * Moving simple CGI functions from cgi.c to new file cgisimple.c
46 **********************************************************************/
52 #include <sys/types.h>
59 #define snprintf _snprintf
60 #endif /* def _WIN32 */
64 #include "cgisimple.h"
73 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
76 static char *show_rcs(void);
77 static void show_defines(struct map *exports);
80 /*********************************************************************
82 * Function : cgi_default
84 * Description : CGI function that is called if no action was given.
85 * Lists menu of available unhidden CGIs.
88 * 1 : csp = Current client state (buffers, headers, etc...)
89 * 2 : rsp = http_response data structure for output
90 * 3 : parameters = map of cgi parameters
94 *********************************************************************/
95 int cgi_default(struct client_state *csp, struct http_response *rsp,
96 struct map *parameters)
100 struct map * exports = default_exports(csp, "");
102 /* If there were other parameters, export a dump as "cgi-parameters" */
105 p = dump_map(parameters);
106 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
107 "Anyway, here they are, in case you're interested:</p>\n");
108 tmp = strsav(tmp, p);
109 map(exports, "cgi-parameters", 1, tmp, 0);
114 map(exports, "cgi-parameters", 1, "", 1);
117 rsp->body = template_load(csp, "default");
118 template_fill(&rsp->body, exports);
125 /*********************************************************************
127 * Function : cgi_send_banner
129 * Description : CGI function that returns a banner.
132 * 1 : csp = Current client state (buffers, headers, etc...)
133 * 2 : rsp = http_response data structure for output
134 * 3 : parameters = map of cgi parameters
137 * type : Selects the type of banner between "trans" and "jb".
138 * Defaults to "jb" if absent or != "trans".
142 *********************************************************************/
143 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
144 struct map *parameters)
146 if(strcmp(lookup(parameters, "type"), "trans"))
148 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
149 rsp->content_length = image_junkbuster_gif_length;
153 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
154 rsp->content_length = image_blank_gif_length;
157 enlist(rsp->headers, "Content-Type: image/gif");
165 /*********************************************************************
167 * Function : cgi_show_version
169 * Description : CGI function that returns a a web page describing the
170 * file versions of IJB.
173 * 1 : csp = Current client state (buffers, headers, etc...)
174 * 2 : rsp = http_response data structure for output
175 * 3 : parameters = map of cgi parameters
177 * CGI Parameters : none
181 *********************************************************************/
182 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
183 struct map *parameters)
185 struct map * exports = default_exports(csp, "show-version");
187 map(exports, "sourceversions", 1, show_rcs(), 0);
189 rsp->body = template_load(csp, "show-version");
190 template_fill(&rsp->body, exports);
198 /*********************************************************************
200 * Function : cgi_show_status
202 * Description : CGI function that returns a a web page describing the
203 * current status of IJB.
206 * 1 : csp = Current client state (buffers, headers, etc...)
207 * 2 : rsp = http_response data structure for output
208 * 3 : parameters = map of cgi parameters
210 * CGI Parameters : none
214 *********************************************************************/
215 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
216 struct map *parameters)
222 char buf[BUFFER_SIZE];
224 const char * filename = NULL;
225 char * file_description = NULL;
226 #ifdef FEATURE_STATISTICS
227 float perc_rej; /* Percentage of http requests rejected */
229 int local_urls_rejected;
230 #endif /* ndef FEATURE_STATISTICS */
232 struct map * exports = default_exports(csp, "show-status");
234 switch (*(lookup(parameters, "file")))
237 if (csp->actions_list)
239 filename = csp->actions_list->filename;
240 file_description = "Actions List";
247 filename = csp->rlist->filename;
248 file_description = "Regex Filter List";
256 filename = csp->tlist->filename;
257 file_description = "Trust List";
260 #endif /* def FEATURE_TRUST */
263 if (NULL != filename)
265 map(exports, "file-description", 1, file_description, 1);
266 map(exports, "filepath", 1, html_encode(filename), 0);
268 if ((fp = fopen(filename, "r")) == NULL)
270 map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
274 while (fgets(buf, sizeof(buf), fp))
276 p = html_encode(buf);
281 s = strsav(s, "<br>");
285 map(exports, "contents", 1, s, 0);
287 rsp->body = template_load(csp, "show-status-file");
288 template_fill(&rsp->body, exports);
294 map(exports, "redirect-url", 1, REDIRECT_URL, 1);
297 for (i=0; i < Argc; i++)
299 s = strsav(s, Argv[i]);
302 map(exports, "invocation", 1, s, 0);
304 map(exports, "options", 1, csp->config->proxy_args, 1);
305 show_defines(exports);
307 #ifdef FEATURE_STATISTICS
308 local_urls_read = urls_read;
309 local_urls_rejected = urls_rejected;
312 * Need to alter the stats not to include the fetch of this
315 * Can't do following thread safely! doh!
318 * urls_rejected--; * This will be incremented subsequently *
321 if (local_urls_read == 0)
323 map_block_killer(exports, "have-stats");
327 map_block_killer(exports, "have-no-stats");
329 perc_rej = (float)local_urls_rejected * 100.0F /
330 (float)local_urls_read;
332 sprintf(buf, "%d", local_urls_read);
333 map(exports, "requests-received", 1, buf, 1);
335 sprintf(buf, "%d", local_urls_rejected);
336 map(exports, "requests-blocked", 1, buf, 1);
338 sprintf(buf, "%6.2f", perc_rej);
339 map(exports, "percent-blocked", 1, buf, 1);
342 #else /* ndef FEATURE_STATISTICS */
343 map_block_killer(exports, "statistics");
344 #endif /* ndef FEATURE_STATISTICS */
346 if (csp->actions_list)
348 map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
352 map(exports, "actions-filename", 1, "None specified", 1);
357 map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
361 map(exports, "re-filter-filename", 1, "None specified", 1);
367 map(exports, "trust-filename", 1, csp->tlist->filename, 1);
371 map(exports, "trust-filename", 1, "None specified", 1);
374 map_block_killer(exports, "trust-support");
375 #endif /* ndef FEATURE_TRUST */
377 rsp->body = template_load(csp, "show-status");
378 template_fill(&rsp->body, exports);
385 /*********************************************************************
387 * Function : cgi_show_url_info
389 * Description : CGI function that determines and shows which actions
390 * junkbuster will perform for a given url, and which
391 * matches starting from the defaults have lead to that.
394 * 1 : csp = Current client state (buffers, headers, etc...)
395 * 2 : rsp = http_response data structure for output
396 * 3 : parameters = map of cgi parameters
399 * url : The url whose actions are to be determined.
400 * If url is unset, the url-given conditional will be
401 * set, so that all but the form can be suppressed in
406 *********************************************************************/
407 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
408 struct map *parameters)
412 struct map * exports = default_exports(csp, "show-url-info");
414 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
416 map_block_killer(exports, "url-given");
417 map(exports, "url", 1, "", 1);
421 char *matches = NULL;
426 struct file_list *fl;
427 struct url_actions *b;
428 struct url_spec url[1];
429 struct current_action_spec action[1];
432 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
434 map(exports, "url", 1, host, 1);
435 map(exports, "url-html", 1, html_encode(host), 0);
437 init_current_action(action);
439 s = current_action_to_text(action);
440 map(exports, "default", 1, s , 0);
442 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
444 map(exports, "matches", 1, "none" , 1);
445 map(exports, "final", 1, lookup(exports, "default"), 1);
448 free_current_action(action);
450 rsp->body = template_load(csp, "show-url-info");
451 template_fill(&rsp->body, exports);
457 s = strchr(host, '/');
467 s = strchr(host, ':');
477 /* if splitting the domain fails, punt */
478 if (url->dbuf == NULL)
480 map(exports, "matches", 1, "none" , 1);
481 map(exports, "final", 1, lookup(exports, "default"), 1);
485 free_current_action(action);
487 rsp->body = template_load(csp, "show-url-info");
488 template_fill(&rsp->body, exports);
494 for (b = b->next; NULL != b; b = b->next)
496 if ((b->url->port == 0) || (b->url->port == port))
498 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
500 if ((b->url->path == NULL) ||
502 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
504 (strncmp(b->url->path, path, b->url->pathlen) == 0)
508 s = actions_to_text(b->action);
509 matches = strsav(matches, "<b>{");
510 matches = strsav(matches, s);
511 matches = strsav(matches, " }</b><br>\n<code>");
512 matches = strsav(matches, b->url->spec);
513 matches = strsav(matches, "</code><br>\n<br>\n");
516 merge_current_action(action, b->action);
525 map(exports, "matches", 1, matches , 0);
529 map(exports, "matches", 1, "none", 1);
539 s = current_action_to_text(action);
540 map(exports, "final", 1, s, 0);
543 free_current_action(action);
546 rsp->body = template_load(csp, "show-url-info");
547 template_fill(&rsp->body, exports);
554 /*********************************************************************
556 * Function : cgi_robots_txt
558 * Description : CGI function to return "/robots.txt".
561 * 1 : csp = Current client state (buffers, headers, etc...)
562 * 2 : rsp = http_response data structure for output
563 * 3 : parameters = map of cgi parameters
565 * CGI Parameters : None
569 *********************************************************************/
570 int cgi_robots_txt(struct client_state *csp, struct http_response *rsp,
571 struct map *parameters)
576 "# This is the Internet Junkbuster control interface.\n"
577 "# It isn't very useful to index it, and you're likely to break stuff.\n"
584 enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
588 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
589 enlist_unique_header(rsp->headers, "Expires", buf);
596 /*********************************************************************
598 * Function : show_defines
600 * Description : Create a string with all conditional #defines used
607 *********************************************************************/
608 static void show_defines(struct map *exports)
612 map_conditional(exports, "FEATURE_ACL", 1);
613 #else /* ifndef FEATURE_ACL */
614 map_conditional(exports, "FEATURE_ACL", 0);
615 #endif /* ndef FEATURE_ACL */
617 #ifdef FEATURE_COOKIE_JAR
618 map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
619 #else /* ifndef FEATURE_COOKIE_JAR */
620 map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
621 #endif /* ndef FEATURE_COOKIE_JAR */
623 #ifdef FEATURE_DENY_GZIP
624 map_conditional(exports, "FEATURE_DENY_GZIP", 1);
625 #else /* ifndef FEATURE_DENY_GZIP */
626 map_conditional(exports, "FEATURE_DENY_GZIP", 0);
627 #endif /* ndef FEATURE_DENY_GZIP */
629 #ifdef FEATURE_FAST_REDIRECTS
630 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
631 #else /* ifndef FEATURE_FAST_REDIRECTS */
632 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
633 #endif /* ndef FEATURE_FAST_REDIRECTS */
635 #ifdef FEATURE_FORCE_LOAD
636 map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
637 #else /* ifndef FEATURE_FORCE_LOAD */
638 map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
639 #endif /* ndef FEATURE_FORCE_LOAD */
641 #ifdef FEATURE_IMAGE_BLOCKING
642 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
643 #else /* ifndef FEATURE_IMAGE_BLOCKING */
644 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
645 #endif /* ndef FEATURE_IMAGE_BLOCKING */
647 #ifdef FEATURE_IMAGE_DETECT_MSIE
648 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
649 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
650 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
651 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
653 #ifdef FEATURE_KILL_POPUPS
654 map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
655 #else /* ifndef FEATURE_KILL_POPUPS */
656 map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
657 #endif /* ndef FEATURE_KILL_POPUPS */
659 #ifdef FEATURE_PTHREAD
660 map_conditional(exports, "FEATURE_PTHREAD", 1);
661 #else /* ifndef FEATURE_PTHREAD */
662 map_conditional(exports, "FEATURE_PTHREAD", 0);
663 #endif /* ndef FEATURE_PTHREAD */
665 #ifdef FEATURE_STATISTICS
666 map_conditional(exports, "FEATURE_STATISTICS", 1);
667 #else /* ifndef FEATURE_STATISTICS */
668 map_conditional(exports, "FEATURE_STATISTICS", 0);
669 #endif /* ndef FEATURE_STATISTICS */
671 #ifdef FEATURE_TOGGLE
672 map_conditional(exports, "FEATURE_TOGGLE", 1);
673 #else /* ifndef FEATURE_TOGGLE */
674 map_conditional(exports, "FEATURE_TOGGLE", 0);
675 #endif /* ndef FEATURE_TOGGLE */
678 map_conditional(exports, "FEATURE_TRUST", 1);
679 #else /* ifndef FEATURE_TRUST */
680 map_conditional(exports, "FEATURE_TRUST", 0);
681 #endif /* ndef FEATURE_TRUST */
684 map_conditional(exports, "REGEX_GNU", 1);
685 #else /* ifndef REGEX_GNU */
686 map_conditional(exports, "REGEX_GNU", 0);
687 #endif /* def REGEX_GNU */
690 map_conditional(exports, "REGEX_PCRE", 1);
691 #else /* ifndef REGEX_PCRE */
692 map_conditional(exports, "REGEX_PCRE", 0);
693 #endif /* def REGEX_PCRE */
696 map_conditional(exports, "STATIC_PCRE", 1);
697 #else /* ifndef STATIC_PCRE */
698 map_conditional(exports, "STATIC_PCRE", 0);
699 #endif /* ndef STATIC_PCRE */
702 map_conditional(exports, "STATIC_PCRS", 1);
703 #else /* ifndef STATIC_PCRS */
704 map_conditional(exports, "STATIC_PCRS", 0);
705 #endif /* ndef STATIC_PCRS */
707 map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
712 /*********************************************************************
714 * Function : show_rcs
716 * Description : Create a string with the rcs info for all sourcefiles
722 *********************************************************************/
723 static char *show_rcs(void)
726 char buf[BUFFER_SIZE];
728 /* Instead of including *all* dot h's in the project (thus creating a
729 * tremendous amount of dependencies), I will concede to declaring them
730 * as extern's. This forces the developer to add to this list, but oh well.
733 #define SHOW_RCS(__x) \
735 extern const char __x[]; \
736 sprintf(buf, "%s\n", __x); \
737 b = strsav(b, buf); \
740 /* In alphabetical order */
741 SHOW_RCS(actions_h_rcs)
742 SHOW_RCS(actions_rcs)
745 #ifdef FEATURE_CGI_EDIT_ACTIONS
746 SHOW_RCS(cgiedit_h_rcs)
747 SHOW_RCS(cgiedit_rcs)
748 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
749 SHOW_RCS(cgisimple_h_rcs)
750 SHOW_RCS(cgisimple_rcs)
752 SHOW_RCS(cygwin_h_rcs)
754 SHOW_RCS(deanimate_h_rcs)
755 SHOW_RCS(deanimate_rcs)
756 SHOW_RCS(encode_h_rcs)
758 SHOW_RCS(errlog_h_rcs)
760 SHOW_RCS(filters_h_rcs)
761 SHOW_RCS(filters_rcs)
762 SHOW_RCS(gateway_h_rcs)
763 SHOW_RCS(gateway_rcs)
765 SHOW_RCS(gnu_regex_h_rcs)
766 SHOW_RCS(gnu_regex_rcs)
767 #endif /* def GNU_REGEX */
768 SHOW_RCS(jbsockets_h_rcs)
769 SHOW_RCS(jbsockets_rcs)
772 #ifdef FEATURE_KILL_POPUPS
773 SHOW_RCS(killpopup_h_rcs)
774 SHOW_RCS(killpopup_rcs)
775 #endif /* def FEATURE_KILL_POPUPS */
778 SHOW_RCS(loadcfg_h_rcs)
779 SHOW_RCS(loadcfg_rcs)
780 SHOW_RCS(loaders_h_rcs)
781 SHOW_RCS(loaders_rcs)
782 SHOW_RCS(miscutil_h_rcs)
783 SHOW_RCS(miscutil_rcs)
784 SHOW_RCS(parsers_h_rcs)
785 SHOW_RCS(parsers_rcs)
788 SHOW_RCS(project_h_rcs)
789 SHOW_RCS(ssplit_h_rcs)
793 SHOW_RCS(w32log_h_rcs)
795 SHOW_RCS(w32res_h_rcs)
796 SHOW_RCS(w32taskbar_h_rcs)
797 SHOW_RCS(w32taskbar_rcs)
798 #endif /* ndef _WIN_CONSOLE */
799 SHOW_RCS(win32_h_rcs)
801 #endif /* def _WIN32 */