1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.38 2006/09/06 18:45:03 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/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.38 2006/09/06 18:45:03 fabiankeil
40 * Incorporate modified version of Roland Rosenfeld's patch to
41 * optionally access the user-manual via Privoxy. Closes patch 679075.
43 * Formatting changed to Privoxy style, added call to
44 * cgi_error_no_template if the requested file doesn't
45 * exist and modified check whether or not Privoxy itself
46 * should serve the manual. Should work cross-platform now.
48 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
49 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
50 * with what was really the latest development (the v_3_0_branch branch)
52 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
53 * Multiple filter file support
55 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
56 * Undo some damaging memory leak patches
58 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
59 * A few memory leaks plugged (mostly on error paths)
61 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
62 * Another instance of:
63 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
64 * Thanks to Magnus Holmgren for the patch
66 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
67 * - Prevent line wrap beween "View/Edit" link buttons on status page
68 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
70 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
71 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
73 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
74 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
76 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
77 * Applying patch from Andreas as provided by Hal on the list.
78 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
80 * Revision 1.35 2002/05/12 21:44:44 jongfoster
81 * Adding amiga.[ch] revision information, if on an amiga.
83 * Revision 1.34 2002/04/30 12:06:12 oes
84 * Deleted unused code from default_cgi
86 * Revision 1.33 2002/04/30 11:14:52 oes
87 * Made csp the first parameter in *action_to_html
89 * Revision 1.32 2002/04/26 18:29:13 jongfoster
90 * Fixing this Visual C++ warning:
91 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
93 * Revision 1.31 2002/04/26 12:54:36 oes
94 * - Kill obsolete REDIRECT_URL code
95 * - Error handling fixes
96 * - Style sheet related HTML snipplet changes
97 * - cgi_show_url_info:
98 * - Matches now in table, actions on single lines,
100 * - standard.action suppressed
101 * - Buttons to View and Edit AFs
103 * Revision 1.30 2002/04/24 02:18:08 oes
104 * - show-status is now the starting point for editing
105 * the actions files, generate list of all AFs with buttons
106 * for viewing and editing, new look for file list (Jon:
107 * buttons now aligned ;-P ), view mode now supports multiple
108 * AFs, name changes, no view links for unspecified files,
109 * no edit link for standard.action.
111 * - Jon's multiple AF patch: cgi_show_url_info now uses all
112 * AFs and marks the output accordingly
114 * Revision 1.29 2002/04/10 13:38:35 oes
115 * load_template signature changed
117 * Revision 1.28 2002/04/07 15:42:12 jongfoster
118 * Fixing send-banner?type=auto when the image-blocker is
119 * a redirect to send-banner
121 * Revision 1.27 2002/04/05 15:50:48 oes
122 * added send-stylesheet CGI
124 * Revision 1.26 2002/04/04 00:36:36 gliptak
125 * always use pcre for matching
127 * Revision 1.25 2002/04/03 22:28:03 gliptak
128 * Removed references to gnu_regex
130 * Revision 1.24 2002/04/02 16:12:47 oes
131 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
133 * Revision 1.23 2002/03/26 22:29:54 swa
134 * we have a new homepage!
136 * Revision 1.22 2002/03/24 16:18:15 jongfoster
139 * Revision 1.21 2002/03/24 15:23:33 jongfoster
142 * Revision 1.20 2002/03/24 13:25:43 swa
143 * name change related issues
145 * Revision 1.19 2002/03/16 23:54:06 jongfoster
146 * Adding graceful termination feature, to help look for memory leaks.
147 * If you enable this (which, by design, has to be done by hand
148 * editing config.h) and then go to http://i.j.b/die, then the program
149 * will exit cleanly after the *next* request. It should free all the
150 * memory that was used.
152 * Revision 1.18 2002/03/12 01:44:49 oes
153 * Changed default for "blocked" image from jb logo to checkboard pattern
155 * Revision 1.17 2002/03/08 16:43:18 oes
156 * Added choice beween GIF and PNG built-in images
158 * Revision 1.16 2002/03/07 03:48:38 oes
159 * - Changed built-in images from GIF to PNG
160 * (with regard to Unisys patent issue)
161 * - Added a 4x4 pattern PNG which is less intrusive
162 * than the logo but also clearly marks the deleted banners
164 * Revision 1.15 2002/03/06 22:54:35 jongfoster
165 * Automated function-comment nitpicking.
167 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
168 * Clean up a little CRLF unpleasantness that suddenly appeared
170 * Revision 1.13 2002/02/21 00:10:37 jongfoster
171 * Adding send-banner?type=auto option
173 * Revision 1.12 2002/01/23 01:03:32 jongfoster
174 * Fixing gcc [CygWin] compiler warnings
176 * Revision 1.11 2002/01/23 00:01:04 jongfoster
177 * Adding cgi_transparent_gif() for http://i.j.b/t
178 * Adding missing html_encode() to many CGI functions.
179 * Adding urlmatch.[ch] to http://i.j.b/show-version
181 * Revision 1.10 2002/01/17 21:10:37 jongfoster
182 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
183 * Also fixing a problem in the same function with improperly quoted URLs
184 * in output HTML, and adding code to handle https:// URLs correctly.
186 * Revision 1.9 2001/11/30 23:09:15 jongfoster
187 * Now reports on FEATURE_CGI_EDIT_ACTIONS
188 * Removing FEATURE_DENY_GZIP from template
190 * Revision 1.8 2001/11/13 00:14:07 jongfoster
191 * Fixing stupid bug now I've figured out what || means.
192 * (It always returns 0 or 1, not one of it's paramaters.)
194 * Revision 1.7 2001/10/23 21:48:19 jongfoster
195 * Cleaning up error handling in CGI functions - they now send back
196 * a HTML error page and should never cause a FATAL error. (Fixes one
197 * potential source of "denial of service" attacks).
199 * CGI actions file editor that works and is actually useful.
201 * Ability to toggle JunkBuster remotely using a CGI call.
203 * You can turn off both the above features in the main configuration
204 * file, e.g. if you are running a multi-user proxy.
206 * Revision 1.6 2001/10/14 22:00:32 jongfoster
207 * Adding support for a 404 error when an invalid CGI page is requested.
209 * Revision 1.5 2001/10/07 15:30:41 oes
210 * Removed FEATURE_DENY_GZIP
212 * Revision 1.4 2001/10/02 15:31:12 oes
213 * Introduced show-request cgi
215 * Revision 1.3 2001/09/22 16:34:44 jongfoster
216 * Removing unneeded #includes
218 * Revision 1.2 2001/09/19 18:01:11 oes
219 * Fixed comments; cosmetics
221 * Revision 1.1 2001/09/16 17:08:54 jongfoster
222 * Moving simple CGI functions from cgi.c to new file cgisimple.c
225 **********************************************************************/
231 #include <sys/types.h>
238 #define snprintf _snprintf
239 #endif /* def _WIN32 */
243 #include "cgisimple.h"
249 #include "miscutil.h"
252 #include "urlmatch.h"
255 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
258 static char *show_rcs(void);
259 static jb_err show_defines(struct map *exports);
262 /*********************************************************************
264 * Function : cgi_default
266 * Description : CGI function that is called for the CGI_SITE_1_HOST
267 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
268 * Boring - only exports the default exports.
271 * 1 : csp = Current client state (buffers, headers, etc...)
272 * 2 : rsp = http_response data structure for output
273 * 3 : parameters = map of cgi parameters
275 * CGI Parameters : none
277 * Returns : JB_ERR_OK on success
278 * JB_ERR_MEMORY on out-of-memory
280 *********************************************************************/
281 jb_err cgi_default(struct client_state *csp,
282 struct http_response *rsp,
283 const struct map *parameters)
290 if (NULL == (exports = default_exports(csp, "")))
292 return JB_ERR_MEMORY;
295 return template_fill_for_cgi(csp, "default", exports, rsp);
299 /*********************************************************************
301 * Function : cgi_error_404
303 * Description : CGI function that is called if an unknown action was
307 * 1 : csp = Current client state (buffers, headers, etc...)
308 * 2 : rsp = http_response data structure for output
309 * 3 : parameters = map of cgi parameters
311 * CGI Parameters : none
313 * Returns : JB_ERR_OK on success
314 * JB_ERR_MEMORY on out-of-memory error.
316 *********************************************************************/
317 jb_err cgi_error_404(struct client_state *csp,
318 struct http_response *rsp,
319 const struct map *parameters)
327 if (NULL == (exports = default_exports(csp, NULL)))
329 return JB_ERR_MEMORY;
332 rsp->status = strdup("404 Privoxy configuration page not found");
333 if (rsp->status == NULL)
336 return JB_ERR_MEMORY;
339 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
343 #ifdef FEATURE_GRACEFUL_TERMINATION
344 /*********************************************************************
348 * Description : CGI function to shut down Privoxy.
349 * NOTE: Turning this on in a production build
350 * would be a BAD idea. An EXTREMELY BAD idea.
351 * In short, don't do it.
354 * 1 : csp = Current client state (buffers, headers, etc...)
355 * 2 : rsp = http_response data structure for output
356 * 3 : parameters = map of cgi parameters
358 * CGI Parameters : none
360 * Returns : JB_ERR_OK on success
361 * JB_ERR_MEMORY on out-of-memory error.
363 *********************************************************************/
364 jb_err cgi_die (struct client_state *csp,
365 struct http_response *rsp,
366 const struct map *parameters)
376 * I don't really care what gets sent back to the browser.
377 * Take the easy option - "out of memory" page.
380 return JB_ERR_MEMORY;
382 #endif /* def FEATURE_GRACEFUL_TERMINATION */
385 /*********************************************************************
387 * Function : cgi_show_request
389 * Description : Show the client's request and what sed() would have
393 * 1 : csp = Current client state (buffers, headers, etc...)
394 * 2 : rsp = http_response data structure for output
395 * 3 : parameters = map of cgi parameters
397 * CGI Parameters : none
399 * Returns : JB_ERR_OK on success
400 * JB_ERR_MEMORY on out-of-memory error.
402 *********************************************************************/
403 jb_err cgi_show_request(struct client_state *csp,
404 struct http_response *rsp,
405 const struct map *parameters)
414 if (NULL == (exports = default_exports(csp, "show-request")))
416 return JB_ERR_MEMORY;
420 * Repair the damage done to the IOB by get_header()
422 for (p = csp->iob->buf; p < csp->iob->eod; p++)
424 if (*p == '\0') *p = '\n';
428 * Export the original client's request and the one we would
429 * be sending to the server if this wasn't a CGI call
432 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
435 return JB_ERR_MEMORY;
438 if (map(exports, "processed-request", 1, html_encode_and_free_original(
439 sed(client_patterns, add_client_headers, csp)), 0))
442 return JB_ERR_MEMORY;
445 return template_fill_for_cgi(csp, "show-request", exports, rsp);
449 /*********************************************************************
451 * Function : cgi_send_banner
453 * Description : CGI function that returns a banner.
456 * 1 : csp = Current client state (buffers, headers, etc...)
457 * 2 : rsp = http_response data structure for output
458 * 3 : parameters = map of cgi parameters
461 * type : Selects the type of banner between "trans", "logo",
462 * and "auto". Defaults to "logo" if absent or invalid.
463 * "auto" means to select as if we were image-blocking.
464 * (Only the first character really counts; b and t are
467 * Returns : JB_ERR_OK on success
468 * JB_ERR_MEMORY on out-of-memory error.
470 *********************************************************************/
471 jb_err cgi_send_banner(struct client_state *csp,
472 struct http_response *rsp,
473 const struct map *parameters)
475 char imagetype = lookup(parameters, "type")[0];
478 * If type is auto, then determine the right thing
479 * to do from the set-image-blocker action
481 if (imagetype == 'a')
488 #ifdef FEATURE_IMAGE_BLOCKING
489 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
491 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
492 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
493 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
497 /* Use default - nothing to do here. */
499 else if (0 == strcmpic(p, "blank"))
503 else if (0 == strcmpic(p, "pattern"))
509 * If the action is to call this CGI, determine
512 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
514 imagetype = p[sizeof(prefix1) - 1];
516 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
518 imagetype = p[sizeof(prefix2) - 1];
522 * Everything else must (should) be a URL to
530 #endif /* def FEATURE_IMAGE_BLOCKING */
534 * Now imagetype is either the non-auto type we were called with,
535 * or it was auto and has since been determined. In any case, we
536 * can proceed to actually answering the request by sending a redirect
537 * or an image as appropriate:
539 if (imagetype == 'r')
541 rsp->status = strdup("302 Local Redirect from Privoxy");
542 if (rsp->status == NULL)
544 return JB_ERR_MEMORY;
546 if (enlist_unique_header(rsp->headers, "Location",
547 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
549 return JB_ERR_MEMORY;
554 if ((imagetype == 'b') || (imagetype == 't'))
556 rsp->body = bindup(image_blank_data, image_blank_length);
557 rsp->content_length = image_blank_length;
561 rsp->body = bindup(image_pattern_data, image_pattern_length);
562 rsp->content_length = image_pattern_length;
565 if (rsp->body == NULL)
567 return JB_ERR_MEMORY;
569 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
571 return JB_ERR_MEMORY;
582 /*********************************************************************
584 * Function : cgi_transparent_image
586 * Description : CGI function that sends a 1x1 transparent image.
589 * 1 : csp = Current client state (buffers, headers, etc...)
590 * 2 : rsp = http_response data structure for output
591 * 3 : parameters = map of cgi parameters
593 * CGI Parameters : None
595 * Returns : JB_ERR_OK on success
596 * JB_ERR_MEMORY on out-of-memory error.
598 *********************************************************************/
599 jb_err cgi_transparent_image(struct client_state *csp,
600 struct http_response *rsp,
601 const struct map *parameters)
603 rsp->body = bindup(image_blank_data, image_blank_length);
604 rsp->content_length = image_blank_length;
606 if (rsp->body == NULL)
608 return JB_ERR_MEMORY;
611 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
613 return JB_ERR_MEMORY;
623 /*********************************************************************
625 * Function : cgi_send_stylesheet
627 * Description : CGI function that sends a css stylesheet found
628 * in the cgi-style.css template
631 * 1 : csp = Current client state (buffers, headers, etc...)
632 * 2 : rsp = http_response data structure for output
633 * 3 : parameters = map of cgi parameters
635 * CGI Parameters : None
637 * Returns : JB_ERR_OK on success
638 * JB_ERR_MEMORY on out-of-memory error.
640 *********************************************************************/
641 jb_err cgi_send_stylesheet(struct client_state *csp,
642 struct http_response *rsp,
643 const struct map *parameters)
650 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
652 if (err == JB_ERR_FILE)
655 * No way to tell user; send empty stylesheet
657 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
661 return err; /* JB_ERR_MEMORY */
664 if (enlist(rsp->headers, "Content-Type: text/css"))
666 return JB_ERR_MEMORY;
672 /*********************************************************************
674 * Function : cgi_send_user_manual
676 * Description : CGI function that sends a user manual HTML file
679 * 1 : csp = Current client state (buffers, headers, etc...)
680 * 2 : rsp = http_response data structure for output
681 * 3 : parameters = map of cgi parameters
683 * CGI Parameters : file=name.html, the name of the HTML file
684 * (relative to user-manual from config)
686 * Returns : JB_ERR_OK on success
687 * JB_ERR_MEMORY on out-of-memory error.
689 *********************************************************************/
690 jb_err cgi_send_user_manual(struct client_state *csp,
691 struct http_response *rsp,
692 const struct map *parameters)
694 const char * filename;
697 char buf[BUFFER_SIZE];
698 jb_err err = JB_ERR_OK;
705 get_string_param(parameters, "file", &filename);
706 /* Check paramter for hack attempts */
707 if (filename && strchr(filename, '/'))
709 return JB_ERR_CGI_PARAMS;
711 if (filename && strstr(filename, ".."))
713 return JB_ERR_CGI_PARAMS;
716 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
717 if (full_path == NULL)
719 return JB_ERR_MEMORY;
722 /* Allocate buffer */
723 rsp->body = strdup("");
724 if (rsp->body == NULL)
727 return JB_ERR_MEMORY;
730 /* Open user-manual file */
731 if (NULL == (fp = fopen(full_path, "r")))
733 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
734 err = cgi_error_no_template(csp, rsp, full_path);
740 /* Read file and write it out */
741 while (fgets(buf, BUFFER_SIZE, fp))
743 if (string_append(&rsp->body, buf))
746 return JB_ERR_MEMORY;
751 /* Guess correct Content-Type based on the filename's ending */
754 length = strlen(filename);
756 if((length>=4) && !strcmp(&filename[length-4], ".css"))
758 err = enlist(rsp->headers, "Content-Type: text/css");
762 err = enlist(rsp->headers, "Content-Type: text/html");
769 /*********************************************************************
771 * Function : cgi_show_version
773 * Description : CGI function that returns a a web page describing the
774 * file versions of Privoxy.
777 * 1 : csp = Current client state (buffers, headers, etc...)
778 * 2 : rsp = http_response data structure for output
779 * 3 : parameters = map of cgi parameters
781 * CGI Parameters : none
783 * Returns : JB_ERR_OK on success
784 * JB_ERR_MEMORY on out-of-memory error.
786 *********************************************************************/
787 jb_err cgi_show_version(struct client_state *csp,
788 struct http_response *rsp,
789 const struct map *parameters)
797 if (NULL == (exports = default_exports(csp, "show-version")))
799 return JB_ERR_MEMORY;
802 if (map(exports, "sourceversions", 1, show_rcs(), 0))
805 return JB_ERR_MEMORY;
808 return template_fill_for_cgi(csp, "show-version", exports, rsp);
812 /*********************************************************************
814 * Function : cgi_show_status
816 * Description : CGI function that returns a a web page describing the
817 * current status of Privoxy.
820 * 1 : csp = Current client state (buffers, headers, etc...)
821 * 2 : rsp = http_response data structure for output
822 * 3 : parameters = map of cgi parameters
825 * file : Which file to show. Only first letter is checked,
827 * - "p"ermissions (actions) file
830 * Default is to show menu and other information.
832 * Returns : JB_ERR_OK on success
833 * JB_ERR_MEMORY on out-of-memory error.
835 *********************************************************************/
836 jb_err cgi_show_status(struct client_state *csp,
837 struct http_response *rsp,
838 const struct map *parameters)
845 char buf[BUFFER_SIZE];
846 const char * filename = NULL;
847 char * file_description = NULL;
848 #ifdef FEATURE_STATISTICS
849 float perc_rej; /* Percentage of http requests rejected */
851 int local_urls_rejected;
852 #endif /* ndef FEATURE_STATISTICS */
853 struct file_list * fl;
854 struct url_actions * b;
855 jb_err err = JB_ERR_OK;
863 if (NULL == (exports = default_exports(csp, "show-status")))
865 return JB_ERR_MEMORY;
868 switch (*(lookup(parameters, "file")))
871 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
873 filename = csp->actions_list[i]->filename;
874 file_description = "Actions File";
879 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
881 filename = csp->rlist[i]->filename;
882 file_description = "Filter File";
890 filename = csp->tlist->filename;
891 file_description = "Trust File";
894 #endif /* def FEATURE_TRUST */
897 if (NULL != filename)
899 if ( map(exports, "file-description", 1, file_description, 1)
900 || map(exports, "filepath", 1, html_encode(filename), 0) )
903 return JB_ERR_MEMORY;
906 if ((fp = fopen(filename, "r")) == NULL)
908 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
911 return JB_ERR_MEMORY;
917 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
919 string_join (&s, html_encode(buf));
923 if (map(exports, "contents", 1, s, 0))
926 return JB_ERR_MEMORY;
930 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
934 for (j = 0; (s != NULL) && (j < Argc); j++)
936 if (!err) err = string_join (&s, html_encode(Argv[j]));
937 if (!err) err = string_append(&s, " ");
939 if (!err) err = map(exports, "invocation", 1, s, 0);
941 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
942 if (!err) err = show_defines(exports);
947 return JB_ERR_MEMORY;
950 #ifdef FEATURE_STATISTICS
951 local_urls_read = urls_read;
952 local_urls_rejected = urls_rejected;
955 * Need to alter the stats not to include the fetch of this
958 * Can't do following thread safely! doh!
961 * urls_rejected--; * This will be incremented subsequently *
964 if (local_urls_read == 0)
966 if (!err) err = map_block_killer(exports, "have-stats");
970 if (!err) err = map_block_killer(exports, "have-no-stats");
972 perc_rej = (float)local_urls_rejected * 100.0F /
973 (float)local_urls_read;
975 sprintf(buf, "%d", local_urls_read);
976 if (!err) err = map(exports, "requests-received", 1, buf, 1);
978 sprintf(buf, "%d", local_urls_rejected);
979 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
981 sprintf(buf, "%6.2f", perc_rej);
982 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
985 #else /* ndef FEATURE_STATISTICS */
986 err = err || map_block_killer(exports, "statistics");
987 #endif /* ndef FEATURE_STATISTICS */
990 * List all action files in use, together with view and edit links,
991 * except for standard.action, which should only be viewable. (Not
992 * enforced in the editor itself)
993 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
996 for (i = 0; i < MAX_AF_FILES; i++)
998 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
1000 if (!err) err = string_append(&s, "<tr><td>");
1001 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1002 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1003 if (!err) err = string_append(&s, buf);
1005 #ifdef FEATURE_CGI_EDIT_ACTIONS
1006 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1008 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1009 if (!err) err = string_append(&s, buf);
1013 if (!err) err = string_append(&s, "</td></tr>\n");
1018 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1022 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1026 * List all re_filterfiles in use, together with view options.
1027 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1030 for (i = 0; i < MAX_AF_FILES; i++)
1032 if (((fl = csp->rlist[i]) != NULL) && ((b = fl->f) != NULL))
1034 if (!err) err = string_append(&s, "<tr><td>");
1035 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1036 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1037 if (!err) err = string_append(&s, buf);
1038 if (!err) err = string_append(&s, "</td></tr>\n");
1043 if (!err) err = map(exports, "re-filter-filename", 1, s, 0);
1047 if (!err) err = map(exports, "re-filter-filename", 1, "<tr><td>None specified</td></tr>", 1);
1048 if (!err) err = map_block_killer(exports, "have-filterfile");
1051 #ifdef FEATURE_TRUST
1054 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1058 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1059 if (!err) err = map_block_killer(exports, "have-trustfile");
1062 if (!err) err = map_block_killer(exports, "trust-support");
1063 #endif /* ndef FEATURE_TRUST */
1068 return JB_ERR_MEMORY;
1071 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1075 /*********************************************************************
1077 * Function : cgi_show_url_info
1079 * Description : CGI function that determines and shows which actions
1080 * Privoxy will perform for a given url, and which
1081 * matches starting from the defaults have lead to that.
1084 * 1 : csp = Current client state (buffers, headers, etc...)
1085 * 2 : rsp = http_response data structure for output
1086 * 3 : parameters = map of cgi parameters
1089 * url : The url whose actions are to be determined.
1090 * If url is unset, the url-given conditional will be
1091 * set, so that all but the form can be suppressed in
1094 * Returns : JB_ERR_OK on success
1095 * JB_ERR_MEMORY on out-of-memory error.
1097 *********************************************************************/
1098 jb_err cgi_show_url_info(struct client_state *csp,
1099 struct http_response *rsp,
1100 const struct map *parameters)
1103 struct map *exports;
1110 if (NULL == (exports = default_exports(csp, "show-url-info")))
1112 return JB_ERR_MEMORY;
1116 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1118 url_param = strdup(lookup(parameters, "url"));
1119 if (url_param == NULL)
1122 return JB_ERR_MEMORY;
1127 * Handle prefixes. 4 possibilities:
1128 * 1) "http://" or "https://" prefix present and followed by URL - OK
1129 * 2) Only the "http://" or "https://" part is present, no URL - change
1130 * to empty string so it will be detected later as "no URL".
1131 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1133 * 4) Parameter not specified or is empty string - let this fall through
1134 * for now, next block of code will handle it.
1136 if (0 == strncmp(url_param, "http://", 7))
1138 if (url_param[7] == '\0')
1141 * Empty URL (just prefix).
1142 * Make it totally empty so it's caught by the next if()
1144 url_param[0] = '\0';
1147 else if (0 == strncmp(url_param, "https://", 8))
1149 if (url_param[8] == '\0')
1152 * Empty URL (just prefix).
1153 * Make it totally empty so it's caught by the next if()
1155 url_param[0] = '\0';
1158 else if (url_param[0] != '\0')
1161 * Unknown prefix - assume http://
1163 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1164 if (NULL == url_param_prefixed)
1168 return JB_ERR_MEMORY;
1170 strcpy(url_param_prefixed, "http://");
1171 strcpy(url_param_prefixed + 7, url_param);
1173 url_param = url_param_prefixed;
1177 if (url_param[0] == '\0')
1179 /* URL paramater not specified, display query form only. */
1181 if (map_block_killer(exports, "url-given")
1182 || map(exports, "url", 1, "", 1))
1185 return JB_ERR_MEMORY;
1190 /* Given a URL, so query it. */
1195 struct file_list *fl;
1196 struct url_actions *b;
1197 struct http_request url_to_query[1];
1198 struct current_action_spec action[1];
1201 if (map(exports, "url", 1, html_encode(url_param), 0))
1205 return JB_ERR_MEMORY;
1208 init_current_action(action);
1210 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1212 free_current_action(action);
1215 return JB_ERR_MEMORY;
1218 err = parse_http_url(url_param, url_to_query, csp);
1222 if (err == JB_ERR_MEMORY)
1224 free_http_request(url_to_query);
1225 free_current_action(action);
1227 return JB_ERR_MEMORY;
1233 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1234 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1236 free_current_action(action);
1237 free_http_request(url_to_query);
1242 return JB_ERR_MEMORY;
1245 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1249 * We have a warning about SSL paths. Hide it for insecure sites.
1251 if (!url_to_query->ssl)
1253 if (map_block_killer(exports, "https"))
1255 free_current_action(action);
1257 free_http_request(url_to_query);
1258 return JB_ERR_MEMORY;
1262 matches = strdup("<table class=\"transparent\">");
1264 for (i = 0; i < MAX_AF_FILES; i++)
1266 if (NULL == csp->config->actions_file_short[i]
1267 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1271 if ((fl = csp->actions_list[i]) != NULL)
1273 if ((b = fl->f) != NULL)
1275 /* FIXME: Hardcoded HTML! */
1276 string_append(&matches, "<tr><th>In file: ");
1277 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1278 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1279 string_append(&matches, buf);
1280 string_append(&matches, "View</a>");
1281 #ifdef FEATURE_CGI_EDIT_ACTIONS
1282 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1283 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1284 string_append(&matches, "\">Edit</a>");
1286 string_append(&matches, "</th></tr>\n");
1293 for (; (b != NULL) && (matches != NULL); b = b->next)
1295 if (url_match(b->url, url_to_query))
1297 string_append(&matches, "<tr><td>{");
1298 string_join (&matches, actions_to_html(csp, b->action));
1299 string_append(&matches, " }</b><br>\n<code>");
1300 string_join (&matches, html_encode(b->url->spec));
1301 string_append(&matches, "</code></td></tr>\n");
1303 if (merge_current_action(action, b->action))
1306 free_http_request(url_to_query);
1307 free_current_action(action);
1309 return JB_ERR_MEMORY;
1317 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1320 string_append(&matches, "</table>\n");
1322 free_http_request(url_to_query);
1324 if (matches == NULL)
1326 free_current_action(action);
1328 return JB_ERR_MEMORY;
1331 if (map(exports, "matches", 1, matches , 0))
1333 free_current_action(action);
1335 return JB_ERR_MEMORY;
1338 s = current_action_to_html(csp, action);
1340 free_current_action(action);
1342 if (map(exports, "final", 1, s, 0))
1345 return JB_ERR_MEMORY;
1349 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1353 /*********************************************************************
1355 * Function : cgi_robots_txt
1357 * Description : CGI function to return "/robots.txt".
1360 * 1 : csp = Current client state (buffers, headers, etc...)
1361 * 2 : rsp = http_response data structure for output
1362 * 3 : parameters = map of cgi parameters
1364 * CGI Parameters : None
1366 * Returns : JB_ERR_OK on success
1367 * JB_ERR_MEMORY on out-of-memory error.
1369 *********************************************************************/
1370 jb_err cgi_robots_txt(struct client_state *csp,
1371 struct http_response *rsp,
1372 const struct map *parameters)
1378 "# This is the Privoxy control interface.\n"
1379 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1385 if (rsp->body == NULL)
1387 return JB_ERR_MEMORY;
1390 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1394 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1395 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1397 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1401 /*********************************************************************
1403 * Function : show_defines
1405 * Description : Add to a map the state od all conditional #defines
1406 * used when building
1409 * 1 : exports = map to extend
1411 * Returns : JB_ERR_OK on success
1412 * JB_ERR_MEMORY on out-of-memory error.
1414 *********************************************************************/
1415 static jb_err show_defines(struct map *exports)
1417 jb_err err = JB_ERR_OK;
1420 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1421 #else /* ifndef FEATURE_ACL */
1422 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1423 #endif /* ndef FEATURE_ACL */
1425 #ifdef FEATURE_CGI_EDIT_ACTIONS
1426 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1427 #else /* ifndef FEATURE_COOKIE_JAR */
1428 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1429 #endif /* ndef FEATURE_COOKIE_JAR */
1431 #ifdef FEATURE_COOKIE_JAR
1432 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1433 #else /* ifndef FEATURE_COOKIE_JAR */
1434 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1435 #endif /* ndef FEATURE_COOKIE_JAR */
1437 #ifdef FEATURE_FAST_REDIRECTS
1438 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1439 #else /* ifndef FEATURE_FAST_REDIRECTS */
1440 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1441 #endif /* ndef FEATURE_FAST_REDIRECTS */
1443 #ifdef FEATURE_FORCE_LOAD
1444 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1445 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1446 #else /* ifndef FEATURE_FORCE_LOAD */
1447 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1448 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1449 #endif /* ndef FEATURE_FORCE_LOAD */
1451 #ifdef FEATURE_IMAGE_BLOCKING
1452 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1453 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1454 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1455 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1457 #ifdef FEATURE_IMAGE_DETECT_MSIE
1458 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1459 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1460 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1461 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1463 #ifdef FEATURE_KILL_POPUPS
1464 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1465 #else /* ifndef FEATURE_KILL_POPUPS */
1466 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1467 #endif /* ndef FEATURE_KILL_POPUPS */
1469 #ifdef FEATURE_NO_GIFS
1470 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1471 #else /* ifndef FEATURE_NO_GIFS */
1472 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1473 #endif /* ndef FEATURE_NO_GIFS */
1475 #ifdef FEATURE_PTHREAD
1476 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1477 #else /* ifndef FEATURE_PTHREAD */
1478 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1479 #endif /* ndef FEATURE_PTHREAD */
1481 #ifdef FEATURE_STATISTICS
1482 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1483 #else /* ifndef FEATURE_STATISTICS */
1484 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1485 #endif /* ndef FEATURE_STATISTICS */
1487 #ifdef FEATURE_TOGGLE
1488 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1489 #else /* ifndef FEATURE_TOGGLE */
1490 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1491 #endif /* ndef FEATURE_TOGGLE */
1493 #ifdef FEATURE_TRUST
1494 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1495 #else /* ifndef FEATURE_TRUST */
1496 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1497 #endif /* ndef FEATURE_TRUST */
1500 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1501 #else /* ifndef STATIC_PCRE */
1502 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1503 #endif /* ndef STATIC_PCRE */
1506 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1507 #else /* ifndef STATIC_PCRS */
1508 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1509 #endif /* ndef STATIC_PCRS */
1515 /*********************************************************************
1517 * Function : show_rcs
1519 * Description : Create a string with the rcs info for all sourcefiles
1523 * Returns : A string, or NULL on out-of-memory.
1525 *********************************************************************/
1526 static char *show_rcs(void)
1528 char *result = strdup("");
1529 char buf[BUFFER_SIZE];
1531 /* Instead of including *all* dot h's in the project (thus creating a
1532 * tremendous amount of dependencies), I will concede to declaring them
1533 * as extern's. This forces the developer to add to this list, but oh well.
1536 #define SHOW_RCS(__x) \
1538 extern const char __x[]; \
1539 sprintf(buf, "%s\n", __x); \
1540 string_append(&result, buf); \
1543 /* In alphabetical order */
1544 SHOW_RCS(actions_h_rcs)
1545 SHOW_RCS(actions_rcs)
1547 SHOW_RCS(amiga_h_rcs)
1549 #endif /* def AMIGA */
1552 #ifdef FEATURE_CGI_EDIT_ACTIONS
1553 SHOW_RCS(cgiedit_h_rcs)
1554 SHOW_RCS(cgiedit_rcs)
1555 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1556 SHOW_RCS(cgisimple_h_rcs)
1557 SHOW_RCS(cgisimple_rcs)
1559 SHOW_RCS(cygwin_h_rcs)
1561 SHOW_RCS(deanimate_h_rcs)
1562 SHOW_RCS(deanimate_rcs)
1563 SHOW_RCS(encode_h_rcs)
1564 SHOW_RCS(encode_rcs)
1565 SHOW_RCS(errlog_h_rcs)
1566 SHOW_RCS(errlog_rcs)
1567 SHOW_RCS(filters_h_rcs)
1568 SHOW_RCS(filters_rcs)
1569 SHOW_RCS(gateway_h_rcs)
1570 SHOW_RCS(gateway_rcs)
1571 SHOW_RCS(jbsockets_h_rcs)
1572 SHOW_RCS(jbsockets_rcs)
1575 #ifdef FEATURE_KILL_POPUPS
1576 SHOW_RCS(killpopup_h_rcs)
1577 SHOW_RCS(killpopup_rcs)
1578 #endif /* def FEATURE_KILL_POPUPS */
1579 SHOW_RCS(list_h_rcs)
1581 SHOW_RCS(loadcfg_h_rcs)
1582 SHOW_RCS(loadcfg_rcs)
1583 SHOW_RCS(loaders_h_rcs)
1584 SHOW_RCS(loaders_rcs)
1585 SHOW_RCS(miscutil_h_rcs)
1586 SHOW_RCS(miscutil_rcs)
1587 SHOW_RCS(parsers_h_rcs)
1588 SHOW_RCS(parsers_rcs)
1590 SHOW_RCS(pcrs_h_rcs)
1591 SHOW_RCS(project_h_rcs)
1592 SHOW_RCS(ssplit_h_rcs)
1593 SHOW_RCS(ssplit_rcs)
1594 SHOW_RCS(urlmatch_h_rcs)
1595 SHOW_RCS(urlmatch_rcs)
1597 #ifndef _WIN_CONSOLE
1598 SHOW_RCS(w32log_h_rcs)
1599 SHOW_RCS(w32log_rcs)
1600 SHOW_RCS(w32res_h_rcs)
1601 SHOW_RCS(w32taskbar_h_rcs)
1602 SHOW_RCS(w32taskbar_rcs)
1603 #endif /* ndef _WIN_CONSOLE */
1604 SHOW_RCS(win32_h_rcs)
1606 #endif /* def _WIN32 */