1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.35.2.7 2006/01/29 23:10:56 david__schmidt Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/Attic/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 1.35.2.7 2006/01/29 23:10:56 david__schmidt
40 * Multiple filter file support
42 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
43 * Undo some damaging memory leak patches
45 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
46 * A few memory leaks plugged (mostly on error paths)
48 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
49 * Another instance of:
50 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
51 * Thanks to Magnus Holmgren for the patch
53 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
54 * - Prevent line wrap beween "View/Edit" link buttons on status page
55 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
57 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
58 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
60 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
61 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
63 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
64 * Applying patch from Andreas as provided by Hal on the list.
65 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
67 * Revision 1.35 2002/05/12 21:44:44 jongfoster
68 * Adding amiga.[ch] revision information, if on an amiga.
70 * Revision 1.34 2002/04/30 12:06:12 oes
71 * Deleted unused code from default_cgi
73 * Revision 1.33 2002/04/30 11:14:52 oes
74 * Made csp the first parameter in *action_to_html
76 * Revision 1.32 2002/04/26 18:29:13 jongfoster
77 * Fixing this Visual C++ warning:
78 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
80 * Revision 1.31 2002/04/26 12:54:36 oes
81 * - Kill obsolete REDIRECT_URL code
82 * - Error handling fixes
83 * - Style sheet related HTML snipplet changes
84 * - cgi_show_url_info:
85 * - Matches now in table, actions on single lines,
87 * - standard.action suppressed
88 * - Buttons to View and Edit AFs
90 * Revision 1.30 2002/04/24 02:18:08 oes
91 * - show-status is now the starting point for editing
92 * the actions files, generate list of all AFs with buttons
93 * for viewing and editing, new look for file list (Jon:
94 * buttons now aligned ;-P ), view mode now supports multiple
95 * AFs, name changes, no view links for unspecified files,
96 * no edit link for standard.action.
98 * - Jon's multiple AF patch: cgi_show_url_info now uses all
99 * AFs and marks the output accordingly
101 * Revision 1.29 2002/04/10 13:38:35 oes
102 * load_template signature changed
104 * Revision 1.28 2002/04/07 15:42:12 jongfoster
105 * Fixing send-banner?type=auto when the image-blocker is
106 * a redirect to send-banner
108 * Revision 1.27 2002/04/05 15:50:48 oes
109 * added send-stylesheet CGI
111 * Revision 1.26 2002/04/04 00:36:36 gliptak
112 * always use pcre for matching
114 * Revision 1.25 2002/04/03 22:28:03 gliptak
115 * Removed references to gnu_regex
117 * Revision 1.24 2002/04/02 16:12:47 oes
118 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
120 * Revision 1.23 2002/03/26 22:29:54 swa
121 * we have a new homepage!
123 * Revision 1.22 2002/03/24 16:18:15 jongfoster
126 * Revision 1.21 2002/03/24 15:23:33 jongfoster
129 * Revision 1.20 2002/03/24 13:25:43 swa
130 * name change related issues
132 * Revision 1.19 2002/03/16 23:54:06 jongfoster
133 * Adding graceful termination feature, to help look for memory leaks.
134 * If you enable this (which, by design, has to be done by hand
135 * editing config.h) and then go to http://i.j.b/die, then the program
136 * will exit cleanly after the *next* request. It should free all the
137 * memory that was used.
139 * Revision 1.18 2002/03/12 01:44:49 oes
140 * Changed default for "blocked" image from jb logo to checkboard pattern
142 * Revision 1.17 2002/03/08 16:43:18 oes
143 * Added choice beween GIF and PNG built-in images
145 * Revision 1.16 2002/03/07 03:48:38 oes
146 * - Changed built-in images from GIF to PNG
147 * (with regard to Unisys patent issue)
148 * - Added a 4x4 pattern PNG which is less intrusive
149 * than the logo but also clearly marks the deleted banners
151 * Revision 1.15 2002/03/06 22:54:35 jongfoster
152 * Automated function-comment nitpicking.
154 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
155 * Clean up a little CRLF unpleasantness that suddenly appeared
157 * Revision 1.13 2002/02/21 00:10:37 jongfoster
158 * Adding send-banner?type=auto option
160 * Revision 1.12 2002/01/23 01:03:32 jongfoster
161 * Fixing gcc [CygWin] compiler warnings
163 * Revision 1.11 2002/01/23 00:01:04 jongfoster
164 * Adding cgi_transparent_gif() for http://i.j.b/t
165 * Adding missing html_encode() to many CGI functions.
166 * Adding urlmatch.[ch] to http://i.j.b/show-version
168 * Revision 1.10 2002/01/17 21:10:37 jongfoster
169 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
170 * Also fixing a problem in the same function with improperly quoted URLs
171 * in output HTML, and adding code to handle https:// URLs correctly.
173 * Revision 1.9 2001/11/30 23:09:15 jongfoster
174 * Now reports on FEATURE_CGI_EDIT_ACTIONS
175 * Removing FEATURE_DENY_GZIP from template
177 * Revision 1.8 2001/11/13 00:14:07 jongfoster
178 * Fixing stupid bug now I've figured out what || means.
179 * (It always returns 0 or 1, not one of it's paramaters.)
181 * Revision 1.7 2001/10/23 21:48:19 jongfoster
182 * Cleaning up error handling in CGI functions - they now send back
183 * a HTML error page and should never cause a FATAL error. (Fixes one
184 * potential source of "denial of service" attacks).
186 * CGI actions file editor that works and is actually useful.
188 * Ability to toggle JunkBuster remotely using a CGI call.
190 * You can turn off both the above features in the main configuration
191 * file, e.g. if you are running a multi-user proxy.
193 * Revision 1.6 2001/10/14 22:00:32 jongfoster
194 * Adding support for a 404 error when an invalid CGI page is requested.
196 * Revision 1.5 2001/10/07 15:30:41 oes
197 * Removed FEATURE_DENY_GZIP
199 * Revision 1.4 2001/10/02 15:31:12 oes
200 * Introduced show-request cgi
202 * Revision 1.3 2001/09/22 16:34:44 jongfoster
203 * Removing unneeded #includes
205 * Revision 1.2 2001/09/19 18:01:11 oes
206 * Fixed comments; cosmetics
208 * Revision 1.1 2001/09/16 17:08:54 jongfoster
209 * Moving simple CGI functions from cgi.c to new file cgisimple.c
212 **********************************************************************/
218 #include <sys/types.h>
225 #define snprintf _snprintf
226 #endif /* def _WIN32 */
230 #include "cgisimple.h"
236 #include "miscutil.h"
239 #include "urlmatch.h"
242 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
245 static char *show_rcs(void);
246 static jb_err show_defines(struct map *exports);
249 /*********************************************************************
251 * Function : cgi_default
253 * Description : CGI function that is called for the CGI_SITE_1_HOST
254 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
255 * Boring - only exports the default exports.
258 * 1 : csp = Current client state (buffers, headers, etc...)
259 * 2 : rsp = http_response data structure for output
260 * 3 : parameters = map of cgi parameters
262 * CGI Parameters : none
264 * Returns : JB_ERR_OK on success
265 * JB_ERR_MEMORY on out-of-memory
267 *********************************************************************/
268 jb_err cgi_default(struct client_state *csp,
269 struct http_response *rsp,
270 const struct map *parameters)
277 if (NULL == (exports = default_exports(csp, "")))
279 return JB_ERR_MEMORY;
282 return template_fill_for_cgi(csp, "default", exports, rsp);
286 /*********************************************************************
288 * Function : cgi_error_404
290 * Description : CGI function that is called if an unknown action was
294 * 1 : csp = Current client state (buffers, headers, etc...)
295 * 2 : rsp = http_response data structure for output
296 * 3 : parameters = map of cgi parameters
298 * CGI Parameters : none
300 * Returns : JB_ERR_OK on success
301 * JB_ERR_MEMORY on out-of-memory error.
303 *********************************************************************/
304 jb_err cgi_error_404(struct client_state *csp,
305 struct http_response *rsp,
306 const struct map *parameters)
314 if (NULL == (exports = default_exports(csp, NULL)))
316 return JB_ERR_MEMORY;
319 rsp->status = strdup("404 Privoxy configuration page not found");
320 if (rsp->status == NULL)
323 return JB_ERR_MEMORY;
326 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
330 #ifdef FEATURE_GRACEFUL_TERMINATION
331 /*********************************************************************
335 * Description : CGI function to shut down Privoxy.
336 * NOTE: Turning this on in a production build
337 * would be a BAD idea. An EXTREMELY BAD idea.
338 * In short, don't do it.
341 * 1 : csp = Current client state (buffers, headers, etc...)
342 * 2 : rsp = http_response data structure for output
343 * 3 : parameters = map of cgi parameters
345 * CGI Parameters : none
347 * Returns : JB_ERR_OK on success
348 * JB_ERR_MEMORY on out-of-memory error.
350 *********************************************************************/
351 jb_err cgi_die (struct client_state *csp,
352 struct http_response *rsp,
353 const struct map *parameters)
363 * I don't really care what gets sent back to the browser.
364 * Take the easy option - "out of memory" page.
367 return JB_ERR_MEMORY;
369 #endif /* def FEATURE_GRACEFUL_TERMINATION */
372 /*********************************************************************
374 * Function : cgi_show_request
376 * Description : Show the client's request and what sed() would have
380 * 1 : csp = Current client state (buffers, headers, etc...)
381 * 2 : rsp = http_response data structure for output
382 * 3 : parameters = map of cgi parameters
384 * CGI Parameters : none
386 * Returns : JB_ERR_OK on success
387 * JB_ERR_MEMORY on out-of-memory error.
389 *********************************************************************/
390 jb_err cgi_show_request(struct client_state *csp,
391 struct http_response *rsp,
392 const struct map *parameters)
401 if (NULL == (exports = default_exports(csp, "show-request")))
403 return JB_ERR_MEMORY;
407 * Repair the damage done to the IOB by get_header()
409 for (p = csp->iob->buf; p < csp->iob->eod; p++)
411 if (*p == '\0') *p = '\n';
415 * Export the original client's request and the one we would
416 * be sending to the server if this wasn't a CGI call
419 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
422 return JB_ERR_MEMORY;
425 if (map(exports, "processed-request", 1, html_encode_and_free_original(
426 sed(client_patterns, add_client_headers, csp)), 0))
429 return JB_ERR_MEMORY;
432 return template_fill_for_cgi(csp, "show-request", exports, rsp);
436 /*********************************************************************
438 * Function : cgi_send_banner
440 * Description : CGI function that returns a banner.
443 * 1 : csp = Current client state (buffers, headers, etc...)
444 * 2 : rsp = http_response data structure for output
445 * 3 : parameters = map of cgi parameters
448 * type : Selects the type of banner between "trans", "logo",
449 * and "auto". Defaults to "logo" if absent or invalid.
450 * "auto" means to select as if we were image-blocking.
451 * (Only the first character really counts; b and t are
454 * Returns : JB_ERR_OK on success
455 * JB_ERR_MEMORY on out-of-memory error.
457 *********************************************************************/
458 jb_err cgi_send_banner(struct client_state *csp,
459 struct http_response *rsp,
460 const struct map *parameters)
462 char imagetype = lookup(parameters, "type")[0];
465 * If type is auto, then determine the right thing
466 * to do from the set-image-blocker action
468 if (imagetype == 'a')
475 #ifdef FEATURE_IMAGE_BLOCKING
476 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
478 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
479 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
480 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
484 /* Use default - nothing to do here. */
486 else if (0 == strcmpic(p, "blank"))
490 else if (0 == strcmpic(p, "pattern"))
496 * If the action is to call this CGI, determine
499 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
501 imagetype = p[sizeof(prefix1) - 1];
503 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
505 imagetype = p[sizeof(prefix2) - 1];
509 * Everything else must (should) be a URL to
517 #endif /* def FEATURE_IMAGE_BLOCKING */
521 * Now imagetype is either the non-auto type we were called with,
522 * or it was auto and has since been determined. In any case, we
523 * can proceed to actually answering the request by sending a redirect
524 * or an image as appropriate:
526 if (imagetype == 'r')
528 rsp->status = strdup("302 Local Redirect from Privoxy");
529 if (rsp->status == NULL)
531 return JB_ERR_MEMORY;
533 if (enlist_unique_header(rsp->headers, "Location",
534 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
536 return JB_ERR_MEMORY;
541 if ((imagetype == 'b') || (imagetype == 't'))
543 rsp->body = bindup(image_blank_data, image_blank_length);
544 rsp->content_length = image_blank_length;
548 rsp->body = bindup(image_pattern_data, image_pattern_length);
549 rsp->content_length = image_pattern_length;
552 if (rsp->body == NULL)
554 return JB_ERR_MEMORY;
556 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
558 return JB_ERR_MEMORY;
569 /*********************************************************************
571 * Function : cgi_transparent_image
573 * Description : CGI function that sends a 1x1 transparent image.
576 * 1 : csp = Current client state (buffers, headers, etc...)
577 * 2 : rsp = http_response data structure for output
578 * 3 : parameters = map of cgi parameters
580 * CGI Parameters : None
582 * Returns : JB_ERR_OK on success
583 * JB_ERR_MEMORY on out-of-memory error.
585 *********************************************************************/
586 jb_err cgi_transparent_image(struct client_state *csp,
587 struct http_response *rsp,
588 const struct map *parameters)
590 rsp->body = bindup(image_blank_data, image_blank_length);
591 rsp->content_length = image_blank_length;
593 if (rsp->body == NULL)
595 return JB_ERR_MEMORY;
598 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
600 return JB_ERR_MEMORY;
610 /*********************************************************************
612 * Function : cgi_send_stylesheet
614 * Description : CGI function that sends a css stylesheet found
615 * in the cgi-style.css template
618 * 1 : csp = Current client state (buffers, headers, etc...)
619 * 2 : rsp = http_response data structure for output
620 * 3 : parameters = map of cgi parameters
622 * CGI Parameters : None
624 * Returns : JB_ERR_OK on success
625 * JB_ERR_MEMORY on out-of-memory error.
627 *********************************************************************/
628 jb_err cgi_send_stylesheet(struct client_state *csp,
629 struct http_response *rsp,
630 const struct map *parameters)
637 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
639 if (err == JB_ERR_FILE)
642 * No way to tell user; send empty stylesheet
644 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
648 return err; /* JB_ERR_MEMORY */
651 if (enlist(rsp->headers, "Content-Type: text/css"))
653 return JB_ERR_MEMORY;
661 /*********************************************************************
663 * Function : cgi_show_version
665 * Description : CGI function that returns a a web page describing the
666 * file versions of Privoxy.
669 * 1 : csp = Current client state (buffers, headers, etc...)
670 * 2 : rsp = http_response data structure for output
671 * 3 : parameters = map of cgi parameters
673 * CGI Parameters : none
675 * Returns : JB_ERR_OK on success
676 * JB_ERR_MEMORY on out-of-memory error.
678 *********************************************************************/
679 jb_err cgi_show_version(struct client_state *csp,
680 struct http_response *rsp,
681 const struct map *parameters)
689 if (NULL == (exports = default_exports(csp, "show-version")))
691 return JB_ERR_MEMORY;
694 if (map(exports, "sourceversions", 1, show_rcs(), 0))
697 return JB_ERR_MEMORY;
700 return template_fill_for_cgi(csp, "show-version", exports, rsp);
704 /*********************************************************************
706 * Function : cgi_show_status
708 * Description : CGI function that returns a a web page describing the
709 * current status of Privoxy.
712 * 1 : csp = Current client state (buffers, headers, etc...)
713 * 2 : rsp = http_response data structure for output
714 * 3 : parameters = map of cgi parameters
717 * file : Which file to show. Only first letter is checked,
719 * - "p"ermissions (actions) file
722 * Default is to show menu and other information.
724 * Returns : JB_ERR_OK on success
725 * JB_ERR_MEMORY on out-of-memory error.
727 *********************************************************************/
728 jb_err cgi_show_status(struct client_state *csp,
729 struct http_response *rsp,
730 const struct map *parameters)
737 char buf[BUFFER_SIZE];
738 const char * filename = NULL;
739 char * file_description = NULL;
740 #ifdef FEATURE_STATISTICS
741 float perc_rej; /* Percentage of http requests rejected */
743 int local_urls_rejected;
744 #endif /* ndef FEATURE_STATISTICS */
745 struct file_list * fl;
746 struct url_actions * b;
747 jb_err err = JB_ERR_OK;
755 if (NULL == (exports = default_exports(csp, "show-status")))
757 return JB_ERR_MEMORY;
760 switch (*(lookup(parameters, "file")))
763 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
765 filename = csp->actions_list[i]->filename;
766 file_description = "Actions File";
771 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
773 filename = csp->rlist[i]->filename;
774 file_description = "Filter File";
782 filename = csp->tlist->filename;
783 file_description = "Trust File";
786 #endif /* def FEATURE_TRUST */
789 if (NULL != filename)
791 if ( map(exports, "file-description", 1, file_description, 1)
792 || map(exports, "filepath", 1, html_encode(filename), 0) )
795 return JB_ERR_MEMORY;
798 if ((fp = fopen(filename, "r")) == NULL)
800 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
803 return JB_ERR_MEMORY;
809 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
811 string_join (&s, html_encode(buf));
815 if (map(exports, "contents", 1, s, 0))
818 return JB_ERR_MEMORY;
822 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
826 for (j = 0; (s != NULL) && (j < Argc); j++)
828 if (!err) err = string_join (&s, html_encode(Argv[j]));
829 if (!err) err = string_append(&s, " ");
831 if (!err) err = map(exports, "invocation", 1, s, 0);
833 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
834 if (!err) err = show_defines(exports);
839 return JB_ERR_MEMORY;
842 #ifdef FEATURE_STATISTICS
843 local_urls_read = urls_read;
844 local_urls_rejected = urls_rejected;
847 * Need to alter the stats not to include the fetch of this
850 * Can't do following thread safely! doh!
853 * urls_rejected--; * This will be incremented subsequently *
856 if (local_urls_read == 0)
858 if (!err) err = map_block_killer(exports, "have-stats");
862 if (!err) err = map_block_killer(exports, "have-no-stats");
864 perc_rej = (float)local_urls_rejected * 100.0F /
865 (float)local_urls_read;
867 sprintf(buf, "%d", local_urls_read);
868 if (!err) err = map(exports, "requests-received", 1, buf, 1);
870 sprintf(buf, "%d", local_urls_rejected);
871 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
873 sprintf(buf, "%6.2f", perc_rej);
874 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
877 #else /* ndef FEATURE_STATISTICS */
878 err = err || map_block_killer(exports, "statistics");
879 #endif /* ndef FEATURE_STATISTICS */
882 * List all action files in use, together with view and edit links,
883 * except for standard.action, which should only be viewable. (Not
884 * enforced in the editor itself)
885 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
888 for (i = 0; i < MAX_AF_FILES; i++)
890 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
892 if (!err) err = string_append(&s, "<tr><td>");
893 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
894 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
895 if (!err) err = string_append(&s, buf);
897 #ifdef FEATURE_CGI_EDIT_ACTIONS
898 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
900 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
901 if (!err) err = string_append(&s, buf);
905 if (!err) err = string_append(&s, "</td></tr>\n");
910 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
914 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
918 * List all re_filterfiles in use, together with view options.
919 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
922 for (i = 0; i < MAX_AF_FILES; i++)
924 if (((fl = csp->rlist[i]) != NULL) && ((b = fl->f) != NULL))
926 if (!err) err = string_append(&s, "<tr><td>");
927 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
928 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
929 if (!err) err = string_append(&s, buf);
930 if (!err) err = string_append(&s, "</td></tr>\n");
935 if (!err) err = map(exports, "re-filter-filename", 1, s, 0);
939 if (!err) err = map(exports, "re-filter-filename", 1, "<tr><td>None specified</td></tr>", 1);
940 if (!err) err = map_block_killer(exports, "have-filterfile");
946 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
950 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
951 if (!err) err = map_block_killer(exports, "have-trustfile");
954 if (!err) err = map_block_killer(exports, "trust-support");
955 #endif /* ndef FEATURE_TRUST */
960 return JB_ERR_MEMORY;
963 return template_fill_for_cgi(csp, "show-status", exports, rsp);
967 /*********************************************************************
969 * Function : cgi_show_url_info
971 * Description : CGI function that determines and shows which actions
972 * Privoxy will perform for a given url, and which
973 * matches starting from the defaults have lead to that.
976 * 1 : csp = Current client state (buffers, headers, etc...)
977 * 2 : rsp = http_response data structure for output
978 * 3 : parameters = map of cgi parameters
981 * url : The url whose actions are to be determined.
982 * If url is unset, the url-given conditional will be
983 * set, so that all but the form can be suppressed in
986 * Returns : JB_ERR_OK on success
987 * JB_ERR_MEMORY on out-of-memory error.
989 *********************************************************************/
990 jb_err cgi_show_url_info(struct client_state *csp,
991 struct http_response *rsp,
992 const struct map *parameters)
1002 if (NULL == (exports = default_exports(csp, "show-url-info")))
1004 return JB_ERR_MEMORY;
1008 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1010 url_param = strdup(lookup(parameters, "url"));
1011 if (url_param == NULL)
1014 return JB_ERR_MEMORY;
1019 * Handle prefixes. 4 possibilities:
1020 * 1) "http://" or "https://" prefix present and followed by URL - OK
1021 * 2) Only the "http://" or "https://" part is present, no URL - change
1022 * to empty string so it will be detected later as "no URL".
1023 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1025 * 4) Parameter not specified or is empty string - let this fall through
1026 * for now, next block of code will handle it.
1028 if (0 == strncmp(url_param, "http://", 7))
1030 if (url_param[7] == '\0')
1033 * Empty URL (just prefix).
1034 * Make it totally empty so it's caught by the next if()
1036 url_param[0] = '\0';
1039 else if (0 == strncmp(url_param, "https://", 8))
1041 if (url_param[8] == '\0')
1044 * Empty URL (just prefix).
1045 * Make it totally empty so it's caught by the next if()
1047 url_param[0] = '\0';
1050 else if (url_param[0] != '\0')
1053 * Unknown prefix - assume http://
1055 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1056 if (NULL == url_param_prefixed)
1060 return JB_ERR_MEMORY;
1062 strcpy(url_param_prefixed, "http://");
1063 strcpy(url_param_prefixed + 7, url_param);
1065 url_param = url_param_prefixed;
1069 if (url_param[0] == '\0')
1071 /* URL paramater not specified, display query form only. */
1073 if (map_block_killer(exports, "url-given")
1074 || map(exports, "url", 1, "", 1))
1077 return JB_ERR_MEMORY;
1082 /* Given a URL, so query it. */
1087 struct file_list *fl;
1088 struct url_actions *b;
1089 struct http_request url_to_query[1];
1090 struct current_action_spec action[1];
1093 if (map(exports, "url", 1, html_encode(url_param), 0))
1097 return JB_ERR_MEMORY;
1100 init_current_action(action);
1102 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1104 free_current_action(action);
1107 return JB_ERR_MEMORY;
1110 err = parse_http_url(url_param, url_to_query, csp);
1114 if (err == JB_ERR_MEMORY)
1116 free_http_request(url_to_query);
1117 free_current_action(action);
1119 return JB_ERR_MEMORY;
1125 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1126 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1128 free_current_action(action);
1129 free_http_request(url_to_query);
1134 return JB_ERR_MEMORY;
1137 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1141 * We have a warning about SSL paths. Hide it for insecure sites.
1143 if (!url_to_query->ssl)
1145 if (map_block_killer(exports, "https"))
1147 free_current_action(action);
1149 free_http_request(url_to_query);
1150 return JB_ERR_MEMORY;
1154 matches = strdup("<table class=\"transparent\">");
1156 for (i = 0; i < MAX_AF_FILES; i++)
1158 if (NULL == csp->config->actions_file_short[i]
1159 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1163 if ((fl = csp->actions_list[i]) != NULL)
1165 if ((b = fl->f) != NULL)
1167 /* FIXME: Hardcoded HTML! */
1168 string_append(&matches, "<tr><th>In file: ");
1169 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1170 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1171 string_append(&matches, buf);
1172 string_append(&matches, "View</a>");
1173 #ifdef FEATURE_CGI_EDIT_ACTIONS
1174 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1175 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1176 string_append(&matches, "\">Edit</a>");
1178 string_append(&matches, "</th></tr>\n");
1185 for (; (b != NULL) && (matches != NULL); b = b->next)
1187 if (url_match(b->url, url_to_query))
1189 string_append(&matches, "<tr><td>{");
1190 string_join (&matches, actions_to_html(csp, b->action));
1191 string_append(&matches, " }</b><br>\n<code>");
1192 string_join (&matches, html_encode(b->url->spec));
1193 string_append(&matches, "</code></td></tr>\n");
1195 if (merge_current_action(action, b->action))
1198 free_http_request(url_to_query);
1199 free_current_action(action);
1201 return JB_ERR_MEMORY;
1209 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1212 string_append(&matches, "</table>\n");
1214 free_http_request(url_to_query);
1216 if (matches == NULL)
1218 free_current_action(action);
1220 return JB_ERR_MEMORY;
1223 if (map(exports, "matches", 1, matches , 0))
1225 free_current_action(action);
1227 return JB_ERR_MEMORY;
1230 s = current_action_to_html(csp, action);
1232 free_current_action(action);
1234 if (map(exports, "final", 1, s, 0))
1237 return JB_ERR_MEMORY;
1241 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1245 /*********************************************************************
1247 * Function : cgi_robots_txt
1249 * Description : CGI function to return "/robots.txt".
1252 * 1 : csp = Current client state (buffers, headers, etc...)
1253 * 2 : rsp = http_response data structure for output
1254 * 3 : parameters = map of cgi parameters
1256 * CGI Parameters : None
1258 * Returns : JB_ERR_OK on success
1259 * JB_ERR_MEMORY on out-of-memory error.
1261 *********************************************************************/
1262 jb_err cgi_robots_txt(struct client_state *csp,
1263 struct http_response *rsp,
1264 const struct map *parameters)
1270 "# This is the Privoxy control interface.\n"
1271 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1277 if (rsp->body == NULL)
1279 return JB_ERR_MEMORY;
1282 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1286 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1287 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1289 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1293 /*********************************************************************
1295 * Function : show_defines
1297 * Description : Add to a map the state od all conditional #defines
1298 * used when building
1301 * 1 : exports = map to extend
1303 * Returns : JB_ERR_OK on success
1304 * JB_ERR_MEMORY on out-of-memory error.
1306 *********************************************************************/
1307 static jb_err show_defines(struct map *exports)
1309 jb_err err = JB_ERR_OK;
1312 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1313 #else /* ifndef FEATURE_ACL */
1314 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1315 #endif /* ndef FEATURE_ACL */
1317 #ifdef FEATURE_CGI_EDIT_ACTIONS
1318 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1319 #else /* ifndef FEATURE_COOKIE_JAR */
1320 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1321 #endif /* ndef FEATURE_COOKIE_JAR */
1323 #ifdef FEATURE_COOKIE_JAR
1324 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1325 #else /* ifndef FEATURE_COOKIE_JAR */
1326 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1327 #endif /* ndef FEATURE_COOKIE_JAR */
1329 #ifdef FEATURE_FAST_REDIRECTS
1330 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1331 #else /* ifndef FEATURE_FAST_REDIRECTS */
1332 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1333 #endif /* ndef FEATURE_FAST_REDIRECTS */
1335 #ifdef FEATURE_FORCE_LOAD
1336 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1337 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1338 #else /* ifndef FEATURE_FORCE_LOAD */
1339 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1340 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1341 #endif /* ndef FEATURE_FORCE_LOAD */
1343 #ifdef FEATURE_IMAGE_BLOCKING
1344 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1345 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1346 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1347 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1349 #ifdef FEATURE_IMAGE_DETECT_MSIE
1350 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1351 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1352 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1353 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1355 #ifdef FEATURE_KILL_POPUPS
1356 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1357 #else /* ifndef FEATURE_KILL_POPUPS */
1358 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1359 #endif /* ndef FEATURE_KILL_POPUPS */
1361 #ifdef FEATURE_NO_GIFS
1362 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1363 #else /* ifndef FEATURE_NO_GIFS */
1364 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1365 #endif /* ndef FEATURE_NO_GIFS */
1367 #ifdef FEATURE_PTHREAD
1368 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1369 #else /* ifndef FEATURE_PTHREAD */
1370 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1371 #endif /* ndef FEATURE_PTHREAD */
1373 #ifdef FEATURE_STATISTICS
1374 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1375 #else /* ifndef FEATURE_STATISTICS */
1376 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1377 #endif /* ndef FEATURE_STATISTICS */
1379 #ifdef FEATURE_TOGGLE
1380 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1381 #else /* ifndef FEATURE_TOGGLE */
1382 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1383 #endif /* ndef FEATURE_TOGGLE */
1385 #ifdef FEATURE_TRUST
1386 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1387 #else /* ifndef FEATURE_TRUST */
1388 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1389 #endif /* ndef FEATURE_TRUST */
1392 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1393 #else /* ifndef STATIC_PCRE */
1394 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1395 #endif /* ndef STATIC_PCRE */
1398 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1399 #else /* ifndef STATIC_PCRS */
1400 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1401 #endif /* ndef STATIC_PCRS */
1407 /*********************************************************************
1409 * Function : show_rcs
1411 * Description : Create a string with the rcs info for all sourcefiles
1415 * Returns : A string, or NULL on out-of-memory.
1417 *********************************************************************/
1418 static char *show_rcs(void)
1420 char *result = strdup("");
1421 char buf[BUFFER_SIZE];
1423 /* Instead of including *all* dot h's in the project (thus creating a
1424 * tremendous amount of dependencies), I will concede to declaring them
1425 * as extern's. This forces the developer to add to this list, but oh well.
1428 #define SHOW_RCS(__x) \
1430 extern const char __x[]; \
1431 sprintf(buf, "%s\n", __x); \
1432 string_append(&result, buf); \
1435 /* In alphabetical order */
1436 SHOW_RCS(actions_h_rcs)
1437 SHOW_RCS(actions_rcs)
1439 SHOW_RCS(amiga_h_rcs)
1441 #endif /* def AMIGA */
1444 #ifdef FEATURE_CGI_EDIT_ACTIONS
1445 SHOW_RCS(cgiedit_h_rcs)
1446 SHOW_RCS(cgiedit_rcs)
1447 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1448 SHOW_RCS(cgisimple_h_rcs)
1449 SHOW_RCS(cgisimple_rcs)
1451 SHOW_RCS(cygwin_h_rcs)
1453 SHOW_RCS(deanimate_h_rcs)
1454 SHOW_RCS(deanimate_rcs)
1455 SHOW_RCS(encode_h_rcs)
1456 SHOW_RCS(encode_rcs)
1457 SHOW_RCS(errlog_h_rcs)
1458 SHOW_RCS(errlog_rcs)
1459 SHOW_RCS(filters_h_rcs)
1460 SHOW_RCS(filters_rcs)
1461 SHOW_RCS(gateway_h_rcs)
1462 SHOW_RCS(gateway_rcs)
1463 SHOW_RCS(jbsockets_h_rcs)
1464 SHOW_RCS(jbsockets_rcs)
1467 #ifdef FEATURE_KILL_POPUPS
1468 SHOW_RCS(killpopup_h_rcs)
1469 SHOW_RCS(killpopup_rcs)
1470 #endif /* def FEATURE_KILL_POPUPS */
1471 SHOW_RCS(list_h_rcs)
1473 SHOW_RCS(loadcfg_h_rcs)
1474 SHOW_RCS(loadcfg_rcs)
1475 SHOW_RCS(loaders_h_rcs)
1476 SHOW_RCS(loaders_rcs)
1477 SHOW_RCS(miscutil_h_rcs)
1478 SHOW_RCS(miscutil_rcs)
1479 SHOW_RCS(parsers_h_rcs)
1480 SHOW_RCS(parsers_rcs)
1482 SHOW_RCS(pcrs_h_rcs)
1483 SHOW_RCS(project_h_rcs)
1484 SHOW_RCS(ssplit_h_rcs)
1485 SHOW_RCS(ssplit_rcs)
1486 SHOW_RCS(urlmatch_h_rcs)
1487 SHOW_RCS(urlmatch_rcs)
1489 #ifndef _WIN_CONSOLE
1490 SHOW_RCS(w32log_h_rcs)
1491 SHOW_RCS(w32log_rcs)
1492 SHOW_RCS(w32res_h_rcs)
1493 SHOW_RCS(w32taskbar_h_rcs)
1494 SHOW_RCS(w32taskbar_rcs)
1495 #endif /* ndef _WIN_CONSOLE */
1496 SHOW_RCS(win32_h_rcs)
1498 #endif /* def _WIN32 */