1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 2.2 2002/12/28 03:58:19 david__schmidt Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/cgisimple.c,v $
6 * Purpose : Simple CGIs to get information about Privoxy's
9 * Functions declared include:
12 * Copyright : Written by and Copyright (C) 2001 the SourceForge
13 * Privoxy team. http://www.privoxy.org/
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 2.2 2002/12/28 03:58:19 david__schmidt
40 * Initial drop of dashboard instrumentation - enabled with
41 * --enable-activity-console
43 * Revision 2.1 2002/07/04 14:35:05 oes
44 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
46 * Revision 2.0 2002/06/04 14:34:21 jongfoster
47 * Moving source files to src/
49 * Revision 1.35 2002/05/12 21:44:44 jongfoster
50 * Adding amiga.[ch] revision information, if on an amiga.
52 * Revision 1.34 2002/04/30 12:06:12 oes
53 * Deleted unused code from default_cgi
55 * Revision 1.33 2002/04/30 11:14:52 oes
56 * Made csp the first parameter in *action_to_html
58 * Revision 1.32 2002/04/26 18:29:13 jongfoster
59 * Fixing this Visual C++ warning:
60 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
62 * Revision 1.31 2002/04/26 12:54:36 oes
63 * - Kill obsolete REDIRECT_URL code
64 * - Error handling fixes
65 * - Style sheet related HTML snipplet changes
66 * - cgi_show_url_info:
67 * - Matches now in table, actions on single lines,
69 * - standard.action suppressed
70 * - Buttons to View and Edit AFs
72 * Revision 1.30 2002/04/24 02:18:08 oes
73 * - show-status is now the starting point for editing
74 * the actions files, generate list of all AFs with buttons
75 * for viewing and editing, new look for file list (Jon:
76 * buttons now aligned ;-P ), view mode now supports multiple
77 * AFs, name changes, no view links for unspecified files,
78 * no edit link for standard.action.
80 * - Jon's multiple AF patch: cgi_show_url_info now uses all
81 * AFs and marks the output accordingly
83 * Revision 1.29 2002/04/10 13:38:35 oes
84 * load_template signature changed
86 * Revision 1.28 2002/04/07 15:42:12 jongfoster
87 * Fixing send-banner?type=auto when the image-blocker is
88 * a redirect to send-banner
90 * Revision 1.27 2002/04/05 15:50:48 oes
91 * added send-stylesheet CGI
93 * Revision 1.26 2002/04/04 00:36:36 gliptak
94 * always use pcre for matching
96 * Revision 1.25 2002/04/03 22:28:03 gliptak
97 * Removed references to gnu_regex
99 * Revision 1.24 2002/04/02 16:12:47 oes
100 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
102 * Revision 1.23 2002/03/26 22:29:54 swa
103 * we have a new homepage!
105 * Revision 1.22 2002/03/24 16:18:15 jongfoster
108 * Revision 1.21 2002/03/24 15:23:33 jongfoster
111 * Revision 1.20 2002/03/24 13:25:43 swa
112 * name change related issues
114 * Revision 1.19 2002/03/16 23:54:06 jongfoster
115 * Adding graceful termination feature, to help look for memory leaks.
116 * If you enable this (which, by design, has to be done by hand
117 * editing config.h) and then go to http://i.j.b/die, then the program
118 * will exit cleanly after the *next* request. It should free all the
119 * memory that was used.
121 * Revision 1.18 2002/03/12 01:44:49 oes
122 * Changed default for "blocked" image from jb logo to checkboard pattern
124 * Revision 1.17 2002/03/08 16:43:18 oes
125 * Added choice beween GIF and PNG built-in images
127 * Revision 1.16 2002/03/07 03:48:38 oes
128 * - Changed built-in images from GIF to PNG
129 * (with regard to Unisys patent issue)
130 * - Added a 4x4 pattern PNG which is less intrusive
131 * than the logo but also clearly marks the deleted banners
133 * Revision 1.15 2002/03/06 22:54:35 jongfoster
134 * Automated function-comment nitpicking.
136 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
137 * Clean up a little CRLF unpleasantness that suddenly appeared
139 * Revision 1.13 2002/02/21 00:10:37 jongfoster
140 * Adding send-banner?type=auto option
142 * Revision 1.12 2002/01/23 01:03:32 jongfoster
143 * Fixing gcc [CygWin] compiler warnings
145 * Revision 1.11 2002/01/23 00:01:04 jongfoster
146 * Adding cgi_transparent_gif() for http://i.j.b/t
147 * Adding missing html_encode() to many CGI functions.
148 * Adding urlmatch.[ch] to http://i.j.b/show-version
150 * Revision 1.10 2002/01/17 21:10:37 jongfoster
151 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
152 * Also fixing a problem in the same function with improperly quoted URLs
153 * in output HTML, and adding code to handle https:// URLs correctly.
155 * Revision 1.9 2001/11/30 23:09:15 jongfoster
156 * Now reports on FEATURE_CGI_EDIT_ACTIONS
157 * Removing FEATURE_DENY_GZIP from template
159 * Revision 1.8 2001/11/13 00:14:07 jongfoster
160 * Fixing stupid bug now I've figured out what || means.
161 * (It always returns 0 or 1, not one of it's paramaters.)
163 * Revision 1.7 2001/10/23 21:48:19 jongfoster
164 * Cleaning up error handling in CGI functions - they now send back
165 * a HTML error page and should never cause a FATAL error. (Fixes one
166 * potential source of "denial of service" attacks).
168 * CGI actions file editor that works and is actually useful.
170 * Ability to toggle JunkBuster remotely using a CGI call.
172 * You can turn off both the above features in the main configuration
173 * file, e.g. if you are running a multi-user proxy.
175 * Revision 1.6 2001/10/14 22:00:32 jongfoster
176 * Adding support for a 404 error when an invalid CGI page is requested.
178 * Revision 1.5 2001/10/07 15:30:41 oes
179 * Removed FEATURE_DENY_GZIP
181 * Revision 1.4 2001/10/02 15:31:12 oes
182 * Introduced show-request cgi
184 * Revision 1.3 2001/09/22 16:34:44 jongfoster
185 * Removing unneeded #includes
187 * Revision 1.2 2001/09/19 18:01:11 oes
188 * Fixed comments; cosmetics
190 * Revision 1.1 2001/09/16 17:08:54 jongfoster
191 * Moving simple CGI functions from cgi.c to new file cgisimple.c
194 **********************************************************************/
200 #include <sys/types.h>
207 #define snprintf _snprintf
208 #endif /* def _WIN32 */
212 #include "cgisimple.h"
218 #include "miscutil.h"
221 #include "urlmatch.h"
224 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
227 static char *show_rcs(void);
228 static jb_err show_defines(struct map *exports);
231 /*********************************************************************
233 * Function : cgi_default
235 * Description : CGI function that is called for the CGI_SITE_1_HOST
236 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
237 * Boring - only exports the default exports.
240 * 1 : csp = Current client state (buffers, headers, etc...)
241 * 2 : rsp = http_response data structure for output
242 * 3 : parameters = map of cgi parameters
244 * CGI Parameters : none
246 * Returns : JB_ERR_OK on success
247 * JB_ERR_MEMORY on out-of-memory
249 *********************************************************************/
250 jb_err cgi_default(struct client_state *csp,
251 struct http_response *rsp,
252 const struct map *parameters)
259 if (NULL == (exports = default_exports(csp, "")))
261 return JB_ERR_MEMORY;
264 return template_fill_for_cgi(csp, "default", exports, rsp);
268 /*********************************************************************
270 * Function : cgi_error_404
272 * Description : CGI function that is called if an unknown action was
276 * 1 : csp = Current client state (buffers, headers, etc...)
277 * 2 : rsp = http_response data structure for output
278 * 3 : parameters = map of cgi parameters
280 * CGI Parameters : none
282 * Returns : JB_ERR_OK on success
283 * JB_ERR_MEMORY on out-of-memory error.
285 *********************************************************************/
286 jb_err cgi_error_404(struct client_state *csp,
287 struct http_response *rsp,
288 const struct map *parameters)
296 if (NULL == (exports = default_exports(csp, NULL)))
298 return JB_ERR_MEMORY;
301 rsp->status = strdup("404 Privoxy configuration page not found");
302 if (rsp->status == NULL)
305 return JB_ERR_MEMORY;
308 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
312 #ifdef FEATURE_GRACEFUL_TERMINATION
313 /*********************************************************************
317 * Description : CGI function to shut down Privoxy.
318 * NOTE: Turning this on in a production build
319 * would be a BAD idea. An EXTREMELY BAD idea.
320 * In short, don't do it.
323 * 1 : csp = Current client state (buffers, headers, etc...)
324 * 2 : rsp = http_response data structure for output
325 * 3 : parameters = map of cgi parameters
327 * CGI Parameters : none
329 * Returns : JB_ERR_OK on success
330 * JB_ERR_MEMORY on out-of-memory error.
332 *********************************************************************/
333 jb_err cgi_die (struct client_state *csp,
334 struct http_response *rsp,
335 const struct map *parameters)
345 * I don't really care what gets sent back to the browser.
346 * Take the easy option - "out of memory" page.
349 return JB_ERR_MEMORY;
351 #endif /* def FEATURE_GRACEFUL_TERMINATION */
354 /*********************************************************************
356 * Function : cgi_show_request
358 * Description : Show the client's request and what sed() would have
362 * 1 : csp = Current client state (buffers, headers, etc...)
363 * 2 : rsp = http_response data structure for output
364 * 3 : parameters = map of cgi parameters
366 * CGI Parameters : none
368 * Returns : JB_ERR_OK on success
369 * JB_ERR_MEMORY on out-of-memory error.
371 *********************************************************************/
372 jb_err cgi_show_request(struct client_state *csp,
373 struct http_response *rsp,
374 const struct map *parameters)
383 if (NULL == (exports = default_exports(csp, "show-request")))
385 return JB_ERR_MEMORY;
389 * Repair the damage done to the IOB by get_header()
391 for (p = csp->iob->buf; p < csp->iob->eod; p++)
393 if (*p == '\0') *p = '\n';
397 * Export the original client's request and the one we would
398 * be sending to the server if this wasn't a CGI call
401 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
404 return JB_ERR_MEMORY;
407 if (map(exports, "processed-request", 1, html_encode_and_free_original(
408 sed(client_patterns, add_client_headers, csp)), 0))
411 return JB_ERR_MEMORY;
414 return template_fill_for_cgi(csp, "show-request", exports, rsp);
418 /*********************************************************************
420 * Function : cgi_send_banner
422 * Description : CGI function that returns a banner.
425 * 1 : csp = Current client state (buffers, headers, etc...)
426 * 2 : rsp = http_response data structure for output
427 * 3 : parameters = map of cgi parameters
430 * type : Selects the type of banner between "trans", "logo",
431 * and "auto". Defaults to "logo" if absent or invalid.
432 * "auto" means to select as if we were image-blocking.
433 * (Only the first character really counts; b and t are
436 * Returns : JB_ERR_OK on success
437 * JB_ERR_MEMORY on out-of-memory error.
439 *********************************************************************/
440 jb_err cgi_send_banner(struct client_state *csp,
441 struct http_response *rsp,
442 const struct map *parameters)
444 char imagetype = lookup(parameters, "type")[0];
445 char *image_mimetype = BUILTIN_IMAGE_MIMETYPE;
448 * If type is auto, then determine the right thing
449 * to do from the set-image-blocker action
451 if (imagetype == 'a')
458 #ifdef FEATURE_IMAGE_BLOCKING
459 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
461 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
462 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
463 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
467 /* Use default - nothing to do here. */
469 else if (0 == strcmpic(p, "blank"))
473 else if (0 == strcmpic(p, "pattern"))
477 else if (0 == strcmpic(p, "custom"))
483 * If the action is to call this CGI, determine
486 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
488 imagetype = p[sizeof(prefix1) - 1];
490 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
492 imagetype = p[sizeof(prefix2) - 1];
496 * Everything else must (should) be a URL to
504 #endif /* def FEATURE_IMAGE_BLOCKING */
508 * Now imagetype is either the non-auto type we were called with,
509 * or it was auto and has since been determined. In any case, we
510 * can proceed to actually answering the request by sending a redirect
511 * or an image as appropriate:
513 if (imagetype == 'r')
515 rsp->status = strdup("302 Local Redirect from Privoxy");
516 if (rsp->status == NULL)
518 return JB_ERR_MEMORY;
520 if (enlist_unique_header(rsp->headers, "Location",
521 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
523 return JB_ERR_MEMORY;
528 if ((imagetype == 'b') || (imagetype == 't'))
530 rsp->body = bindup(image_blank_data, image_blank_length);
531 rsp->content_length = image_blank_length;
533 else if (imagetype == 'c')
535 rsp->body = bindup(csp->config->image_blocker_data, csp->config->image_blocker_length);
536 rsp->content_length = csp->config->image_blocker_length;
537 image_mimetype = csp->config->image_blocker_format;
541 rsp->body = bindup(image_pattern_data, image_pattern_length);
542 rsp->content_length = image_pattern_length;
545 if (rsp->body == NULL)
547 return JB_ERR_MEMORY;
549 if (enlist(rsp->headers, "Content-Type: "))
551 return JB_ERR_MEMORY;
553 if (enlist(rsp->headers, image_mimetype))
555 return JB_ERR_MEMORY;
566 /*********************************************************************
568 * Function : cgi_transparent_image
570 * Description : CGI function that sends a 1x1 transparent image.
573 * 1 : csp = Current client state (buffers, headers, etc...)
574 * 2 : rsp = http_response data structure for output
575 * 3 : parameters = map of cgi parameters
577 * CGI Parameters : None
579 * Returns : JB_ERR_OK on success
580 * JB_ERR_MEMORY on out-of-memory error.
582 *********************************************************************/
583 jb_err cgi_transparent_image(struct client_state *csp,
584 struct http_response *rsp,
585 const struct map *parameters)
587 rsp->body = bindup(image_blank_data, image_blank_length);
588 rsp->content_length = image_blank_length;
590 if (rsp->body == NULL)
592 return JB_ERR_MEMORY;
595 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
597 return JB_ERR_MEMORY;
607 /*********************************************************************
609 * Function : cgi_send_stylesheet
611 * Description : CGI function that sends a css stylesheet found
612 * in the cgi-style.css template
615 * 1 : csp = Current client state (buffers, headers, etc...)
616 * 2 : rsp = http_response data structure for output
617 * 3 : parameters = map of cgi parameters
619 * CGI Parameters : None
621 * Returns : JB_ERR_OK on success
622 * JB_ERR_MEMORY on out-of-memory error.
624 *********************************************************************/
625 jb_err cgi_send_stylesheet(struct client_state *csp,
626 struct http_response *rsp,
627 const struct map *parameters)
634 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
636 if (err == JB_ERR_FILE)
639 * No way to tell user; send empty stylesheet
641 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
645 return err; /* JB_ERR_MEMORY */
648 if (enlist(rsp->headers, "Content-Type: text/css"))
650 return JB_ERR_MEMORY;
658 /*********************************************************************
660 * Function : cgi_show_version
662 * Description : CGI function that returns a a web page describing the
663 * file versions of Privoxy.
666 * 1 : csp = Current client state (buffers, headers, etc...)
667 * 2 : rsp = http_response data structure for output
668 * 3 : parameters = map of cgi parameters
670 * CGI Parameters : none
672 * Returns : JB_ERR_OK on success
673 * JB_ERR_MEMORY on out-of-memory error.
675 *********************************************************************/
676 jb_err cgi_show_version(struct client_state *csp,
677 struct http_response *rsp,
678 const struct map *parameters)
686 if (NULL == (exports = default_exports(csp, "show-version")))
688 return JB_ERR_MEMORY;
691 if (map(exports, "sourceversions", 1, show_rcs(), 0))
694 return JB_ERR_MEMORY;
697 return template_fill_for_cgi(csp, "show-version", exports, rsp);
701 /*********************************************************************
703 * Function : cgi_show_status
705 * Description : CGI function that returns a a web page describing the
706 * current status of Privoxy.
709 * 1 : csp = Current client state (buffers, headers, etc...)
710 * 2 : rsp = http_response data structure for output
711 * 3 : parameters = map of cgi parameters
714 * file : Which file to show. Only first letter is checked,
716 * - "p"ermissions (actions) file
719 * Default is to show menu and other information.
721 * Returns : JB_ERR_OK on success
722 * JB_ERR_MEMORY on out-of-memory error.
724 *********************************************************************/
725 jb_err cgi_show_status(struct client_state *csp,
726 struct http_response *rsp,
727 const struct map *parameters)
734 char buf[BUFFER_SIZE];
735 const char * filename = NULL;
736 char * file_description = NULL;
737 #ifdef FEATURE_STATISTICS
738 float perc_rej; /* Percentage of http requests rejected */
740 int local_urls_rejected;
741 #endif /* ndef FEATURE_STATISTICS */
742 struct file_list * fl;
743 struct url_actions * b;
744 jb_err err = JB_ERR_OK;
752 if (NULL == (exports = default_exports(csp, "show-status")))
754 return JB_ERR_MEMORY;
757 switch (*(lookup(parameters, "file")))
760 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_ACTION_FILES && csp->actions_list[i])
762 filename = csp->actions_list[i]->filename;
763 file_description = "Actions File";
770 filename = csp->rlist->filename;
771 file_description = "Filter File";
779 filename = csp->tlist->filename;
780 file_description = "Trust File";
783 #endif /* def FEATURE_TRUST */
786 if (NULL != filename)
788 if ( map(exports, "file-description", 1, file_description, 1)
789 || map(exports, "filepath", 1, html_encode(filename), 0) )
792 return JB_ERR_MEMORY;
795 if ((fp = fopen(filename, "r")) == NULL)
797 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
800 return JB_ERR_MEMORY;
806 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
808 string_join (&s, html_encode(buf));
812 if (map(exports, "contents", 1, s, 0))
815 return JB_ERR_MEMORY;
819 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
823 for (j = 0; (s != NULL) && (j < Argc); j++)
825 if (!err) err = string_join (&s, html_encode(Argv[j]));
826 if (!err) err = string_append(&s, " ");
828 if (!err) err = map(exports, "invocation", 1, s, 0);
830 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
831 if (!err) err = show_defines(exports);
836 return JB_ERR_MEMORY;
839 #ifdef FEATURE_STATISTICS
840 local_urls_read = urls_read;
841 local_urls_rejected = urls_rejected;
844 * Need to alter the stats not to include the fetch of this
847 * Can't do following thread safely! doh!
850 * urls_rejected--; * This will be incremented subsequently *
853 if (local_urls_read == 0)
855 if (!err) err = map_block_killer(exports, "have-stats");
859 if (!err) err = map_block_killer(exports, "have-no-stats");
861 perc_rej = (float)local_urls_rejected * 100.0F /
862 (float)local_urls_read;
864 sprintf(buf, "%d", local_urls_read);
865 if (!err) err = map(exports, "requests-received", 1, buf, 1);
867 sprintf(buf, "%d", local_urls_rejected);
868 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
870 sprintf(buf, "%6.2f", perc_rej);
871 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
874 #else /* ndef FEATURE_STATISTICS */
875 err = err || map_block_killer(exports, "statistics");
876 #endif /* ndef FEATURE_STATISTICS */
879 * List all action files in use, together with view and edit links,
880 * except for standard.action, which should only be viewable. (Not
881 * enforced in the editor itself)
882 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
885 for (i = 0; i < MAX_ACTION_FILES; i++)
887 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
889 if (!err) err = string_append(&s, "<tr><td>");
890 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
891 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a> ", i);
892 if (!err) err = string_append(&s, buf);
894 #ifdef FEATURE_CGI_EDIT_ACTIONS
895 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
897 snprintf(buf, 100, "<a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
898 if (!err) err = string_append(&s, buf);
902 if (!err) err = string_append(&s, "</td></tr>\n");
907 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
911 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
916 if (!err) err = map(exports, "re-filter-filename", 1, html_encode(csp->rlist->filename), 0);
920 if (!err) err = map(exports, "re-filter-filename", 1, "None specified", 1);
921 if (!err) err = map_block_killer(exports, "have-filterfile");
927 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
931 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
932 if (!err) err = map_block_killer(exports, "have-trustfile");
935 if (!err) err = map_block_killer(exports, "trust-support");
936 #endif /* ndef FEATURE_TRUST */
941 return JB_ERR_MEMORY;
944 return template_fill_for_cgi(csp, "show-status", exports, rsp);
948 /*********************************************************************
950 * Function : cgi_show_url_info
952 * Description : CGI function that determines and shows which actions
953 * Privoxy will perform for a given url, and which
954 * matches starting from the defaults have lead to that.
957 * 1 : csp = Current client state (buffers, headers, etc...)
958 * 2 : rsp = http_response data structure for output
959 * 3 : parameters = map of cgi parameters
962 * url : The url whose actions are to be determined.
963 * If url is unset, the url-given conditional will be
964 * set, so that all but the form can be suppressed in
967 * Returns : JB_ERR_OK on success
968 * JB_ERR_MEMORY on out-of-memory error.
970 *********************************************************************/
971 jb_err cgi_show_url_info(struct client_state *csp,
972 struct http_response *rsp,
973 const struct map *parameters)
983 if (NULL == (exports = default_exports(csp, "show-url-info")))
985 return JB_ERR_MEMORY;
989 * Get the url= parameter (if present) and remove any leading/trailing spaces.
991 url_param = strdup(lookup(parameters, "url"));
992 if (url_param == NULL)
995 return JB_ERR_MEMORY;
1000 * Handle prefixes. 4 possibilities:
1001 * 1) "http://" or "https://" prefix present and followed by URL - OK
1002 * 2) Only the "http://" or "https://" part is present, no URL - change
1003 * to empty string so it will be detected later as "no URL".
1004 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1006 * 4) Parameter not specified or is empty string - let this fall through
1007 * for now, next block of code will handle it.
1009 if (0 == strncmp(url_param, "http://", 7))
1011 if (url_param[7] == '\0')
1014 * Empty URL (just prefix).
1015 * Make it totally empty so it's caught by the next if()
1017 url_param[0] = '\0';
1020 else if (0 == strncmp(url_param, "https://", 8))
1022 if (url_param[8] == '\0')
1025 * Empty URL (just prefix).
1026 * Make it totally empty so it's caught by the next if()
1028 url_param[0] = '\0';
1031 else if (url_param[0] != '\0')
1034 * Unknown prefix - assume http://
1036 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1037 if (NULL == url_param_prefixed)
1041 return JB_ERR_MEMORY;
1043 strcpy(url_param_prefixed, "http://");
1044 strcpy(url_param_prefixed + 7, url_param);
1046 url_param = url_param_prefixed;
1050 if (url_param[0] == '\0')
1052 /* URL paramater not specified, display query form only. */
1054 if (map_block_killer(exports, "url-given")
1055 || map(exports, "url", 1, "", 1))
1058 return JB_ERR_MEMORY;
1063 /* Given a URL, so query it. */
1068 struct file_list *fl;
1069 struct url_actions *b;
1070 struct http_request url_to_query[1];
1071 struct current_action_spec action[1];
1074 if (map(exports, "url", 1, html_encode(url_param), 0))
1078 return JB_ERR_MEMORY;
1081 init_current_action(action);
1083 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1085 free_current_action(action);
1088 return JB_ERR_MEMORY;
1091 err = parse_http_url(url_param, url_to_query, csp);
1095 if (err == JB_ERR_MEMORY)
1097 free_current_action(action);
1099 return JB_ERR_MEMORY;
1105 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1106 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1108 free_current_action(action);
1113 return JB_ERR_MEMORY;
1116 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1120 * We have a warning about SSL paths. Hide it for insecure sites.
1122 if (!url_to_query->ssl)
1124 if (map_block_killer(exports, "https"))
1126 free_current_action(action);
1128 return JB_ERR_MEMORY;
1132 matches = strdup("<table class=\"transparent\">");
1134 for (i = 0; i < MAX_ACTION_FILES; i++)
1136 if (NULL == csp->config->actions_file_short[i]
1137 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1141 if ((fl = csp->actions_list[i]) != NULL)
1143 if ((b = fl->f) != NULL)
1145 /* FIXME: Hardcoded HTML! */
1146 string_append(&matches, "<tr><th>In file: ");
1147 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1148 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1149 string_append(&matches, buf);
1150 string_append(&matches, "View</a> <a class=\"cmd\" href=\"/edit-actions-list?f=");
1151 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1152 string_append(&matches, "\">Edit</a></th></tr>\n");
1159 for (; (b != NULL) && (matches != NULL); b = b->next)
1161 if (url_match(b->url, url_to_query))
1163 string_append(&matches, "<tr><td>{");
1164 string_join (&matches, actions_to_html(csp, b->action));
1165 string_append(&matches, " }</b><br>\n<code>");
1166 string_join (&matches, html_encode(b->url->spec));
1167 string_append(&matches, "</code></td></tr>\n");
1169 if (merge_current_action(action, b->action))
1172 free_http_request(url_to_query);
1173 free_current_action(action);
1175 return JB_ERR_MEMORY;
1183 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1186 string_append(&matches, "</table>\n");
1188 free_http_request(url_to_query);
1190 if (matches == NULL)
1192 free_current_action(action);
1194 return JB_ERR_MEMORY;
1197 if (map(exports, "matches", 1, matches , 0))
1199 free_current_action(action);
1201 return JB_ERR_MEMORY;
1204 s = current_action_to_html(csp, action);
1206 free_current_action(action);
1208 if (map(exports, "final", 1, s, 0))
1211 return JB_ERR_MEMORY;
1215 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1219 /*********************************************************************
1221 * Function : cgi_robots_txt
1223 * Description : CGI function to return "/robots.txt".
1226 * 1 : csp = Current client state (buffers, headers, etc...)
1227 * 2 : rsp = http_response data structure for output
1228 * 3 : parameters = map of cgi parameters
1230 * CGI Parameters : None
1232 * Returns : JB_ERR_OK on success
1233 * JB_ERR_MEMORY on out-of-memory error.
1235 *********************************************************************/
1236 jb_err cgi_robots_txt(struct client_state *csp,
1237 struct http_response *rsp,
1238 const struct map *parameters)
1244 "# This is the Privoxy control interface.\n"
1245 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1251 if (rsp->body == NULL)
1253 return JB_ERR_MEMORY;
1256 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1260 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1261 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1263 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1267 /*********************************************************************
1269 * Function : show_defines
1271 * Description : Add to a map the state od all conditional #defines
1272 * used when building
1275 * 1 : exports = map to extend
1277 * Returns : JB_ERR_OK on success
1278 * JB_ERR_MEMORY on out-of-memory error.
1280 *********************************************************************/
1281 static jb_err show_defines(struct map *exports)
1283 jb_err err = JB_ERR_OK;
1286 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1287 #else /* ifndef FEATURE_ACL */
1288 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1289 #endif /* ndef FEATURE_ACL */
1291 #ifdef FEATURE_CGI_EDIT_ACTIONS
1292 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1293 #else /* ifndef FEATURE_COOKIE_JAR */
1294 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1295 #endif /* ndef FEATURE_COOKIE_JAR */
1297 #ifdef FEATURE_COOKIE_JAR
1298 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1299 #else /* ifndef FEATURE_COOKIE_JAR */
1300 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1301 #endif /* ndef FEATURE_COOKIE_JAR */
1303 #ifdef FEATURE_FAST_REDIRECTS
1304 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1305 #else /* ifndef FEATURE_FAST_REDIRECTS */
1306 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1307 #endif /* ndef FEATURE_FAST_REDIRECTS */
1309 #ifdef FEATURE_FORCE_LOAD
1310 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1311 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1312 #else /* ifndef FEATURE_FORCE_LOAD */
1313 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1314 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1315 #endif /* ndef FEATURE_FORCE_LOAD */
1317 #ifdef FEATURE_IMAGE_BLOCKING
1318 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1319 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1320 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1321 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1323 #ifdef FEATURE_IMAGE_DETECT_MSIE
1324 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1325 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1326 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1327 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1329 #ifdef FEATURE_KILL_POPUPS
1330 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1331 #else /* ifndef FEATURE_KILL_POPUPS */
1332 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1333 #endif /* ndef FEATURE_KILL_POPUPS */
1335 #ifdef FEATURE_NO_GIFS
1336 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1337 #else /* ifndef FEATURE_NO_GIFS */
1338 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1339 #endif /* ndef FEATURE_NO_GIFS */
1341 #ifdef FEATURE_PTHREAD
1342 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1343 #else /* ifndef FEATURE_PTHREAD */
1344 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1345 #endif /* ndef FEATURE_PTHREAD */
1347 #ifdef FEATURE_STATISTICS
1348 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1349 #else /* ifndef FEATURE_STATISTICS */
1350 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1351 #endif /* ndef FEATURE_STATISTICS */
1353 #ifdef FEATURE_TOGGLE
1354 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1355 #else /* ifndef FEATURE_TOGGLE */
1356 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1357 #endif /* ndef FEATURE_TOGGLE */
1359 #ifdef FEATURE_TRUST
1360 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1361 #else /* ifndef FEATURE_TRUST */
1362 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1363 #endif /* ndef FEATURE_TRUST */
1366 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1367 #else /* ifndef STATIC_PCRE */
1368 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1369 #endif /* ndef STATIC_PCRE */
1372 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1373 #else /* ifndef STATIC_PCRS */
1374 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1375 #endif /* ndef STATIC_PCRS */
1381 /*********************************************************************
1383 * Function : show_rcs
1385 * Description : Create a string with the rcs info for all sourcefiles
1389 * Returns : A string, or NULL on out-of-memory.
1391 *********************************************************************/
1392 static char *show_rcs(void)
1394 char *result = strdup("");
1395 char buf[BUFFER_SIZE];
1397 /* Instead of including *all* dot h's in the project (thus creating a
1398 * tremendous amount of dependencies), I will concede to declaring them
1399 * as extern's. This forces the developer to add to this list, but oh well.
1402 #define SHOW_RCS(__x) \
1404 extern const char __x[]; \
1405 sprintf(buf, "%s\n", __x); \
1406 string_append(&result, buf); \
1409 /* In alphabetical order */
1410 SHOW_RCS(actions_h_rcs)
1411 SHOW_RCS(actions_rcs)
1413 SHOW_RCS(amiga_h_rcs)
1415 #endif /* def AMIGA */
1418 #ifdef FEATURE_CGI_EDIT_ACTIONS
1419 SHOW_RCS(cgiedit_h_rcs)
1420 SHOW_RCS(cgiedit_rcs)
1421 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1422 SHOW_RCS(cgisimple_h_rcs)
1423 SHOW_RCS(cgisimple_rcs)
1425 SHOW_RCS(cygwin_h_rcs)
1427 SHOW_RCS(deanimate_h_rcs)
1428 SHOW_RCS(deanimate_rcs)
1429 SHOW_RCS(encode_h_rcs)
1430 SHOW_RCS(encode_rcs)
1431 SHOW_RCS(errlog_h_rcs)
1432 SHOW_RCS(errlog_rcs)
1433 SHOW_RCS(filters_h_rcs)
1434 SHOW_RCS(filters_rcs)
1435 SHOW_RCS(gateway_h_rcs)
1436 SHOW_RCS(gateway_rcs)
1437 #ifdef FEATURE_ACTIVITY_CONSOLE
1439 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1440 SHOW_RCS(jbsockets_h_rcs)
1441 SHOW_RCS(jbsockets_rcs)
1444 #ifdef FEATURE_KILL_POPUPS
1445 SHOW_RCS(killpopup_h_rcs)
1446 SHOW_RCS(killpopup_rcs)
1447 #endif /* def FEATURE_KILL_POPUPS */
1448 SHOW_RCS(list_h_rcs)
1450 SHOW_RCS(loadcfg_h_rcs)
1451 SHOW_RCS(loadcfg_rcs)
1452 SHOW_RCS(loaders_h_rcs)
1453 SHOW_RCS(loaders_rcs)
1454 SHOW_RCS(miscutil_h_rcs)
1455 SHOW_RCS(miscutil_rcs)
1456 SHOW_RCS(parsers_h_rcs)
1457 SHOW_RCS(parsers_rcs)
1459 SHOW_RCS(pcrs_h_rcs)
1460 SHOW_RCS(project_h_rcs)
1461 SHOW_RCS(ssplit_h_rcs)
1462 SHOW_RCS(ssplit_rcs)
1463 #ifdef FEATURE_ACTIVITY_CONSOLE
1464 SHOW_RCS(stats_h_rcs)
1466 #endif /* def FEATURE_ACTIVITY_CONSOLE */
1467 SHOW_RCS(urlmatch_h_rcs)
1468 SHOW_RCS(urlmatch_rcs)
1470 #ifndef _WIN_CONSOLE
1471 SHOW_RCS(w32log_h_rcs)
1472 SHOW_RCS(w32log_rcs)
1473 SHOW_RCS(w32res_h_rcs)
1474 SHOW_RCS(w32taskbar_h_rcs)
1475 SHOW_RCS(w32taskbar_rcs)
1476 #endif /* ndef _WIN_CONSOLE */
1477 SHOW_RCS(win32_h_rcs)
1479 #endif /* def _WIN32 */