1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.3 2001/09/22 16:34:44 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.3 2001/09/22 16:34:44 jongfoster
40 * Removing unneeded #includes
42 * Revision 1.2 2001/09/19 18:01:11 oes
43 * Fixed comments; cosmetics
45 * Revision 1.1 2001/09/16 17:08:54 jongfoster
46 * Moving simple CGI functions from cgi.c to new file cgisimple.c
49 **********************************************************************/
55 #include <sys/types.h>
62 #define snprintf _snprintf
63 #endif /* def _WIN32 */
67 #include "cgisimple.h"
77 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
80 static char *show_rcs(void);
81 static void show_defines(struct map *exports);
84 /*********************************************************************
86 * Function : cgi_default
88 * Description : CGI function that is called if no action was given.
89 * Lists menu of available unhidden CGIs.
92 * 1 : csp = Current client state (buffers, headers, etc...)
93 * 2 : rsp = http_response data structure for output
94 * 3 : parameters = map of cgi parameters
98 *********************************************************************/
99 int cgi_default(struct client_state *csp, struct http_response *rsp,
100 struct map *parameters)
104 struct map *exports = default_exports(csp, "");
106 /* If there were other parameters, export a dump as "cgi-parameters" */
109 p = dump_map(parameters);
110 tmp = strsav(tmp, "<p>What made you think this cgi takes parameters?\n"
111 "Anyway, here they are, in case you're interested:</p>\n");
112 tmp = strsav(tmp, p);
113 map(exports, "cgi-parameters", 1, tmp, 0);
118 map(exports, "cgi-parameters", 1, "", 1);
121 rsp->body = template_load(csp, "default");
122 template_fill(&rsp->body, exports);
129 /*********************************************************************
131 * Function : cgi_show_request
133 * Description : Show the client's request and what sed() would have
137 * 1 : csp = Current client state (buffers, headers, etc...)
138 * 2 : rsp = http_response data structure for output
139 * 3 : parameters = map of cgi parameters
143 *********************************************************************/
144 int cgi_show_request(struct client_state *csp, struct http_response *rsp,
145 struct map *parameters)
148 struct map *exports = default_exports(csp, "show-request");
151 * Repair the damage done to the IOB by get_header()
153 for (p = csp->iob->buf; p < csp->iob->eod; p++)
155 if (*p == '\0') *p = '\n';
159 * Export the original client's request and the one we would
160 * be sending to the server if this wasn't a CGI call
162 map(exports, "client-request", 1, csp->iob->buf, 1);
163 map(exports, "processed-request", 1, sed(client_patterns, add_client_headers, csp), 0);
165 rsp->body = template_load(csp, "show-request");
166 template_fill(&rsp->body, exports);
173 /*********************************************************************
175 * Function : cgi_send_banner
177 * Description : CGI function that returns a banner.
180 * 1 : csp = Current client state (buffers, headers, etc...)
181 * 2 : rsp = http_response data structure for output
182 * 3 : parameters = map of cgi parameters
185 * type : Selects the type of banner between "trans" and "jb".
186 * Defaults to "jb" if absent or != "trans".
190 *********************************************************************/
191 int cgi_send_banner(struct client_state *csp, struct http_response *rsp,
192 struct map *parameters)
194 if(strcmp(lookup(parameters, "type"), "trans"))
196 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
197 rsp->content_length = image_junkbuster_gif_length;
201 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
202 rsp->content_length = image_blank_gif_length;
205 enlist(rsp->headers, "Content-Type: image/gif");
213 /*********************************************************************
215 * Function : cgi_show_version
217 * Description : CGI function that returns a a web page describing the
218 * file versions of IJB.
221 * 1 : csp = Current client state (buffers, headers, etc...)
222 * 2 : rsp = http_response data structure for output
223 * 3 : parameters = map of cgi parameters
225 * CGI Parameters : none
229 *********************************************************************/
230 int cgi_show_version(struct client_state *csp, struct http_response *rsp,
231 struct map *parameters)
233 struct map * exports = default_exports(csp, "show-version");
235 map(exports, "sourceversions", 1, show_rcs(), 0);
237 rsp->body = template_load(csp, "show-version");
238 template_fill(&rsp->body, exports);
246 /*********************************************************************
248 * Function : cgi_show_status
250 * Description : CGI function that returns a a web page describing the
251 * current status of IJB.
254 * 1 : csp = Current client state (buffers, headers, etc...)
255 * 2 : rsp = http_response data structure for output
256 * 3 : parameters = map of cgi parameters
258 * CGI Parameters : none
262 *********************************************************************/
263 int cgi_show_status(struct client_state *csp, struct http_response *rsp,
264 struct map *parameters)
270 char buf[BUFFER_SIZE];
272 const char * filename = NULL;
273 char * file_description = NULL;
274 #ifdef FEATURE_STATISTICS
275 float perc_rej; /* Percentage of http requests rejected */
277 int local_urls_rejected;
278 #endif /* ndef FEATURE_STATISTICS */
280 struct map * exports = default_exports(csp, "show-status");
282 switch (*(lookup(parameters, "file")))
285 if (csp->actions_list)
287 filename = csp->actions_list->filename;
288 file_description = "Actions List";
295 filename = csp->rlist->filename;
296 file_description = "Regex Filter List";
304 filename = csp->tlist->filename;
305 file_description = "Trust List";
308 #endif /* def FEATURE_TRUST */
311 if (NULL != filename)
313 map(exports, "file-description", 1, file_description, 1);
314 map(exports, "filepath", 1, html_encode(filename), 0);
316 if ((fp = fopen(filename, "r")) == NULL)
318 map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1);
322 while (fgets(buf, sizeof(buf), fp))
324 p = html_encode(buf);
329 s = strsav(s, "<br>");
333 map(exports, "contents", 1, s, 0);
335 rsp->body = template_load(csp, "show-status-file");
336 template_fill(&rsp->body, exports);
342 map(exports, "redirect-url", 1, REDIRECT_URL, 1);
345 for (i=0; i < Argc; i++)
347 s = strsav(s, Argv[i]);
350 map(exports, "invocation", 1, s, 0);
352 map(exports, "options", 1, csp->config->proxy_args, 1);
353 show_defines(exports);
355 #ifdef FEATURE_STATISTICS
356 local_urls_read = urls_read;
357 local_urls_rejected = urls_rejected;
360 * Need to alter the stats not to include the fetch of this
363 * Can't do following thread safely! doh!
366 * urls_rejected--; * This will be incremented subsequently *
369 if (local_urls_read == 0)
371 map_block_killer(exports, "have-stats");
375 map_block_killer(exports, "have-no-stats");
377 perc_rej = (float)local_urls_rejected * 100.0F /
378 (float)local_urls_read;
380 sprintf(buf, "%d", local_urls_read);
381 map(exports, "requests-received", 1, buf, 1);
383 sprintf(buf, "%d", local_urls_rejected);
384 map(exports, "requests-blocked", 1, buf, 1);
386 sprintf(buf, "%6.2f", perc_rej);
387 map(exports, "percent-blocked", 1, buf, 1);
390 #else /* ndef FEATURE_STATISTICS */
391 map_block_killer(exports, "statistics");
392 #endif /* ndef FEATURE_STATISTICS */
394 if (csp->actions_list)
396 map(exports, "actions-filename", 1, csp->actions_list->filename, 1);
400 map(exports, "actions-filename", 1, "None specified", 1);
405 map(exports, "re-filter-filename", 1, csp->rlist->filename, 1);
409 map(exports, "re-filter-filename", 1, "None specified", 1);
415 map(exports, "trust-filename", 1, csp->tlist->filename, 1);
419 map(exports, "trust-filename", 1, "None specified", 1);
422 map_block_killer(exports, "trust-support");
423 #endif /* ndef FEATURE_TRUST */
425 rsp->body = template_load(csp, "show-status");
426 template_fill(&rsp->body, exports);
433 /*********************************************************************
435 * Function : cgi_show_url_info
437 * Description : CGI function that determines and shows which actions
438 * junkbuster will perform for a given url, and which
439 * matches starting from the defaults have lead to that.
442 * 1 : csp = Current client state (buffers, headers, etc...)
443 * 2 : rsp = http_response data structure for output
444 * 3 : parameters = map of cgi parameters
447 * url : The url whose actions are to be determined.
448 * If url is unset, the url-given conditional will be
449 * set, so that all but the form can be suppressed in
454 *********************************************************************/
455 int cgi_show_url_info(struct client_state *csp, struct http_response *rsp,
456 struct map *parameters)
460 struct map * exports = default_exports(csp, "show-url-info");
462 if (NULL == (url_param = strdup(lookup(parameters, "url"))) || *url_param == '\0')
464 map_block_killer(exports, "url-given");
465 map(exports, "url", 1, "", 1);
469 char *matches = NULL;
474 struct file_list *fl;
475 struct url_actions *b;
476 struct url_spec url[1];
477 struct current_action_spec action[1];
480 host += (strncmp(url_param, "http://", 7)) ? 0 : 7;
482 map(exports, "url", 1, host, 1);
483 map(exports, "url-html", 1, html_encode(host), 0);
485 init_current_action(action);
487 s = current_action_to_text(action);
488 map(exports, "default", 1, s , 0);
490 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
492 map(exports, "matches", 1, "none" , 1);
493 map(exports, "final", 1, lookup(exports, "default"), 1);
496 free_current_action(action);
498 rsp->body = template_load(csp, "show-url-info");
499 template_fill(&rsp->body, exports);
505 s = strchr(host, '/');
515 s = strchr(host, ':');
525 /* if splitting the domain fails, punt */
526 if (url->dbuf == NULL)
528 map(exports, "matches", 1, "none" , 1);
529 map(exports, "final", 1, lookup(exports, "default"), 1);
533 free_current_action(action);
535 rsp->body = template_load(csp, "show-url-info");
536 template_fill(&rsp->body, exports);
542 for (b = b->next; NULL != b; b = b->next)
544 if ((b->url->port == 0) || (b->url->port == port))
546 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
548 if ((b->url->path == NULL) ||
550 (regexec(b->url->preg, path, 0, NULL, 0) == 0)
552 (strncmp(b->url->path, path, b->url->pathlen) == 0)
556 s = actions_to_text(b->action);
557 matches = strsav(matches, "<b>{");
558 matches = strsav(matches, s);
559 matches = strsav(matches, " }</b><br>\n<code>");
560 matches = strsav(matches, b->url->spec);
561 matches = strsav(matches, "</code><br>\n<br>\n");
564 merge_current_action(action, b->action);
573 map(exports, "matches", 1, matches , 0);
577 map(exports, "matches", 1, "none", 1);
587 s = current_action_to_text(action);
588 map(exports, "final", 1, s, 0);
591 free_current_action(action);
594 rsp->body = template_load(csp, "show-url-info");
595 template_fill(&rsp->body, exports);
602 /*********************************************************************
604 * Function : cgi_robots_txt
606 * Description : CGI function to return "/robots.txt".
609 * 1 : csp = Current client state (buffers, headers, etc...)
610 * 2 : rsp = http_response data structure for output
611 * 3 : parameters = map of cgi parameters
613 * CGI Parameters : None
617 *********************************************************************/
618 int cgi_robots_txt(struct client_state *csp, struct http_response *rsp,
619 struct map *parameters)
624 "# This is the Internet Junkbuster control interface.\n"
625 "# It isn't very useful to index it, and you're likely to break stuff.\n"
632 enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
636 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
637 enlist_unique_header(rsp->headers, "Expires", buf);
644 /*********************************************************************
646 * Function : show_defines
648 * Description : Create a string with all conditional #defines used
655 *********************************************************************/
656 static void show_defines(struct map *exports)
660 map_conditional(exports, "FEATURE_ACL", 1);
661 #else /* ifndef FEATURE_ACL */
662 map_conditional(exports, "FEATURE_ACL", 0);
663 #endif /* ndef FEATURE_ACL */
665 #ifdef FEATURE_COOKIE_JAR
666 map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
667 #else /* ifndef FEATURE_COOKIE_JAR */
668 map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
669 #endif /* ndef FEATURE_COOKIE_JAR */
671 #ifdef FEATURE_DENY_GZIP
672 map_conditional(exports, "FEATURE_DENY_GZIP", 1);
673 #else /* ifndef FEATURE_DENY_GZIP */
674 map_conditional(exports, "FEATURE_DENY_GZIP", 0);
675 #endif /* ndef FEATURE_DENY_GZIP */
677 #ifdef FEATURE_FAST_REDIRECTS
678 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
679 #else /* ifndef FEATURE_FAST_REDIRECTS */
680 map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
681 #endif /* ndef FEATURE_FAST_REDIRECTS */
683 #ifdef FEATURE_FORCE_LOAD
684 map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
685 #else /* ifndef FEATURE_FORCE_LOAD */
686 map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
687 #endif /* ndef FEATURE_FORCE_LOAD */
689 #ifdef FEATURE_IMAGE_BLOCKING
690 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
691 #else /* ifndef FEATURE_IMAGE_BLOCKING */
692 map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
693 #endif /* ndef FEATURE_IMAGE_BLOCKING */
695 #ifdef FEATURE_IMAGE_DETECT_MSIE
696 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
697 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
698 map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
699 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
701 #ifdef FEATURE_KILL_POPUPS
702 map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
703 #else /* ifndef FEATURE_KILL_POPUPS */
704 map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
705 #endif /* ndef FEATURE_KILL_POPUPS */
707 #ifdef FEATURE_PTHREAD
708 map_conditional(exports, "FEATURE_PTHREAD", 1);
709 #else /* ifndef FEATURE_PTHREAD */
710 map_conditional(exports, "FEATURE_PTHREAD", 0);
711 #endif /* ndef FEATURE_PTHREAD */
713 #ifdef FEATURE_STATISTICS
714 map_conditional(exports, "FEATURE_STATISTICS", 1);
715 #else /* ifndef FEATURE_STATISTICS */
716 map_conditional(exports, "FEATURE_STATISTICS", 0);
717 #endif /* ndef FEATURE_STATISTICS */
719 #ifdef FEATURE_TOGGLE
720 map_conditional(exports, "FEATURE_TOGGLE", 1);
721 #else /* ifndef FEATURE_TOGGLE */
722 map_conditional(exports, "FEATURE_TOGGLE", 0);
723 #endif /* ndef FEATURE_TOGGLE */
726 map_conditional(exports, "FEATURE_TRUST", 1);
727 #else /* ifndef FEATURE_TRUST */
728 map_conditional(exports, "FEATURE_TRUST", 0);
729 #endif /* ndef FEATURE_TRUST */
732 map_conditional(exports, "REGEX_GNU", 1);
733 #else /* ifndef REGEX_GNU */
734 map_conditional(exports, "REGEX_GNU", 0);
735 #endif /* def REGEX_GNU */
738 map_conditional(exports, "REGEX_PCRE", 1);
739 #else /* ifndef REGEX_PCRE */
740 map_conditional(exports, "REGEX_PCRE", 0);
741 #endif /* def REGEX_PCRE */
744 map_conditional(exports, "STATIC_PCRE", 1);
745 #else /* ifndef STATIC_PCRE */
746 map_conditional(exports, "STATIC_PCRE", 0);
747 #endif /* ndef STATIC_PCRE */
750 map_conditional(exports, "STATIC_PCRS", 1);
751 #else /* ifndef STATIC_PCRS */
752 map_conditional(exports, "STATIC_PCRS", 0);
753 #endif /* ndef STATIC_PCRS */
755 map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
760 /*********************************************************************
762 * Function : show_rcs
764 * Description : Create a string with the rcs info for all sourcefiles
770 *********************************************************************/
771 static char *show_rcs(void)
774 char buf[BUFFER_SIZE];
776 /* Instead of including *all* dot h's in the project (thus creating a
777 * tremendous amount of dependencies), I will concede to declaring them
778 * as extern's. This forces the developer to add to this list, but oh well.
781 #define SHOW_RCS(__x) \
783 extern const char __x[]; \
784 sprintf(buf, "%s\n", __x); \
785 b = strsav(b, buf); \
788 /* In alphabetical order */
789 SHOW_RCS(actions_h_rcs)
790 SHOW_RCS(actions_rcs)
793 #ifdef FEATURE_CGI_EDIT_ACTIONS
794 SHOW_RCS(cgiedit_h_rcs)
795 SHOW_RCS(cgiedit_rcs)
796 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
797 SHOW_RCS(cgisimple_h_rcs)
798 SHOW_RCS(cgisimple_rcs)
800 SHOW_RCS(cygwin_h_rcs)
802 SHOW_RCS(deanimate_h_rcs)
803 SHOW_RCS(deanimate_rcs)
804 SHOW_RCS(encode_h_rcs)
806 SHOW_RCS(errlog_h_rcs)
808 SHOW_RCS(filters_h_rcs)
809 SHOW_RCS(filters_rcs)
810 SHOW_RCS(gateway_h_rcs)
811 SHOW_RCS(gateway_rcs)
813 SHOW_RCS(gnu_regex_h_rcs)
814 SHOW_RCS(gnu_regex_rcs)
815 #endif /* def GNU_REGEX */
816 SHOW_RCS(jbsockets_h_rcs)
817 SHOW_RCS(jbsockets_rcs)
820 #ifdef FEATURE_KILL_POPUPS
821 SHOW_RCS(killpopup_h_rcs)
822 SHOW_RCS(killpopup_rcs)
823 #endif /* def FEATURE_KILL_POPUPS */
826 SHOW_RCS(loadcfg_h_rcs)
827 SHOW_RCS(loadcfg_rcs)
828 SHOW_RCS(loaders_h_rcs)
829 SHOW_RCS(loaders_rcs)
830 SHOW_RCS(miscutil_h_rcs)
831 SHOW_RCS(miscutil_rcs)
832 SHOW_RCS(parsers_h_rcs)
833 SHOW_RCS(parsers_rcs)
836 SHOW_RCS(project_h_rcs)
837 SHOW_RCS(ssplit_h_rcs)
841 SHOW_RCS(w32log_h_rcs)
843 SHOW_RCS(w32res_h_rcs)
844 SHOW_RCS(w32taskbar_h_rcs)
845 SHOW_RCS(w32taskbar_rcs)
846 #endif /* ndef _WIN_CONSOLE */
847 SHOW_RCS(win32_h_rcs)
849 #endif /* def _WIN32 */