1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.44 2006/12/22 14:19:27 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.44 2006/12/22 14:19:27 fabiankeil
40 * Removed checks whether or not AF_FILES have
41 * data structures associated with them in cgi_show_status.
42 * It doesn't matter as we're only interested in the file names.
44 * For the action files the checks were always true,
45 * but they prevented empty filter files from being
46 * listed. Fixes parts of BR 1619208.
48 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
49 * - Added FEATURE_GRACEFUL_TERMINATION to the
50 * "conditional #defines" section
51 * - Escaped ampersands in generated HTML.
52 * - Renamed re-filter-filename to re-filter-filenames
54 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
55 * Add special treatment for WIN32 to make sure
56 * cgi_send_user_manual opens the files in binary mode.
57 * Fixes BR 1600411 and unbreaks image delivery.
59 * Remove outdated comment.
61 * Revision 1.41 2006/10/09 19:18:28 roro
62 * Redirect http://p.p/user-manual (without trailing slash) to
63 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
65 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
66 * Modified cgi_send_user_manual to serve binary
67 * content without destroying it first. Should also be
68 * faster now. Added ".jpg" check for Content-Type guessing.
70 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
71 * Deliver documents in the user-manual directory
72 * with "Content-Type text/css" if their filename
75 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
76 * Incorporate modified version of Roland Rosenfeld's patch to
77 * optionally access the user-manual via Privoxy. Closes patch 679075.
79 * Formatting changed to Privoxy style, added call to
80 * cgi_error_no_template if the requested file doesn't
81 * exist and modified check whether or not Privoxy itself
82 * should serve the manual. Should work cross-platform now.
84 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
85 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
86 * with what was really the latest development (the v_3_0_branch branch)
88 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
89 * Multiple filter file support
91 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
92 * Undo some damaging memory leak patches
94 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
95 * A few memory leaks plugged (mostly on error paths)
97 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
98 * Another instance of:
99 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
100 * Thanks to Magnus Holmgren for the patch
102 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
103 * - Prevent line wrap beween "View/Edit" link buttons on status page
104 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
106 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
107 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
109 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
110 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
112 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
113 * Applying patch from Andreas as provided by Hal on the list.
114 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
116 * Revision 1.35 2002/05/12 21:44:44 jongfoster
117 * Adding amiga.[ch] revision information, if on an amiga.
119 * Revision 1.34 2002/04/30 12:06:12 oes
120 * Deleted unused code from default_cgi
122 * Revision 1.33 2002/04/30 11:14:52 oes
123 * Made csp the first parameter in *action_to_html
125 * Revision 1.32 2002/04/26 18:29:13 jongfoster
126 * Fixing this Visual C++ warning:
127 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
129 * Revision 1.31 2002/04/26 12:54:36 oes
130 * - Kill obsolete REDIRECT_URL code
131 * - Error handling fixes
132 * - Style sheet related HTML snipplet changes
133 * - cgi_show_url_info:
134 * - Matches now in table, actions on single lines,
136 * - standard.action suppressed
137 * - Buttons to View and Edit AFs
139 * Revision 1.30 2002/04/24 02:18:08 oes
140 * - show-status is now the starting point for editing
141 * the actions files, generate list of all AFs with buttons
142 * for viewing and editing, new look for file list (Jon:
143 * buttons now aligned ;-P ), view mode now supports multiple
144 * AFs, name changes, no view links for unspecified files,
145 * no edit link for standard.action.
147 * - Jon's multiple AF patch: cgi_show_url_info now uses all
148 * AFs and marks the output accordingly
150 * Revision 1.29 2002/04/10 13:38:35 oes
151 * load_template signature changed
153 * Revision 1.28 2002/04/07 15:42:12 jongfoster
154 * Fixing send-banner?type=auto when the image-blocker is
155 * a redirect to send-banner
157 * Revision 1.27 2002/04/05 15:50:48 oes
158 * added send-stylesheet CGI
160 * Revision 1.26 2002/04/04 00:36:36 gliptak
161 * always use pcre for matching
163 * Revision 1.25 2002/04/03 22:28:03 gliptak
164 * Removed references to gnu_regex
166 * Revision 1.24 2002/04/02 16:12:47 oes
167 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
169 * Revision 1.23 2002/03/26 22:29:54 swa
170 * we have a new homepage!
172 * Revision 1.22 2002/03/24 16:18:15 jongfoster
175 * Revision 1.21 2002/03/24 15:23:33 jongfoster
178 * Revision 1.20 2002/03/24 13:25:43 swa
179 * name change related issues
181 * Revision 1.19 2002/03/16 23:54:06 jongfoster
182 * Adding graceful termination feature, to help look for memory leaks.
183 * If you enable this (which, by design, has to be done by hand
184 * editing config.h) and then go to http://i.j.b/die, then the program
185 * will exit cleanly after the *next* request. It should free all the
186 * memory that was used.
188 * Revision 1.18 2002/03/12 01:44:49 oes
189 * Changed default for "blocked" image from jb logo to checkboard pattern
191 * Revision 1.17 2002/03/08 16:43:18 oes
192 * Added choice beween GIF and PNG built-in images
194 * Revision 1.16 2002/03/07 03:48:38 oes
195 * - Changed built-in images from GIF to PNG
196 * (with regard to Unisys patent issue)
197 * - Added a 4x4 pattern PNG which is less intrusive
198 * than the logo but also clearly marks the deleted banners
200 * Revision 1.15 2002/03/06 22:54:35 jongfoster
201 * Automated function-comment nitpicking.
203 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
204 * Clean up a little CRLF unpleasantness that suddenly appeared
206 * Revision 1.13 2002/02/21 00:10:37 jongfoster
207 * Adding send-banner?type=auto option
209 * Revision 1.12 2002/01/23 01:03:32 jongfoster
210 * Fixing gcc [CygWin] compiler warnings
212 * Revision 1.11 2002/01/23 00:01:04 jongfoster
213 * Adding cgi_transparent_gif() for http://i.j.b/t
214 * Adding missing html_encode() to many CGI functions.
215 * Adding urlmatch.[ch] to http://i.j.b/show-version
217 * Revision 1.10 2002/01/17 21:10:37 jongfoster
218 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
219 * Also fixing a problem in the same function with improperly quoted URLs
220 * in output HTML, and adding code to handle https:// URLs correctly.
222 * Revision 1.9 2001/11/30 23:09:15 jongfoster
223 * Now reports on FEATURE_CGI_EDIT_ACTIONS
224 * Removing FEATURE_DENY_GZIP from template
226 * Revision 1.8 2001/11/13 00:14:07 jongfoster
227 * Fixing stupid bug now I've figured out what || means.
228 * (It always returns 0 or 1, not one of it's paramaters.)
230 * Revision 1.7 2001/10/23 21:48:19 jongfoster
231 * Cleaning up error handling in CGI functions - they now send back
232 * a HTML error page and should never cause a FATAL error. (Fixes one
233 * potential source of "denial of service" attacks).
235 * CGI actions file editor that works and is actually useful.
237 * Ability to toggle JunkBuster remotely using a CGI call.
239 * You can turn off both the above features in the main configuration
240 * file, e.g. if you are running a multi-user proxy.
242 * Revision 1.6 2001/10/14 22:00:32 jongfoster
243 * Adding support for a 404 error when an invalid CGI page is requested.
245 * Revision 1.5 2001/10/07 15:30:41 oes
246 * Removed FEATURE_DENY_GZIP
248 * Revision 1.4 2001/10/02 15:31:12 oes
249 * Introduced show-request cgi
251 * Revision 1.3 2001/09/22 16:34:44 jongfoster
252 * Removing unneeded #includes
254 * Revision 1.2 2001/09/19 18:01:11 oes
255 * Fixed comments; cosmetics
257 * Revision 1.1 2001/09/16 17:08:54 jongfoster
258 * Moving simple CGI functions from cgi.c to new file cgisimple.c
261 **********************************************************************/
267 #include <sys/types.h>
274 #define snprintf _snprintf
275 #endif /* def _WIN32 */
279 #include "cgisimple.h"
285 #include "miscutil.h"
288 #include "urlmatch.h"
291 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
294 static char *show_rcs(void);
295 static jb_err show_defines(struct map *exports);
298 /*********************************************************************
300 * Function : cgi_default
302 * Description : CGI function that is called for the CGI_SITE_1_HOST
303 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
304 * Boring - only exports the default exports.
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
316 *********************************************************************/
317 jb_err cgi_default(struct client_state *csp,
318 struct http_response *rsp,
319 const struct map *parameters)
326 if (NULL == (exports = default_exports(csp, "")))
328 return JB_ERR_MEMORY;
331 return template_fill_for_cgi(csp, "default", exports, rsp);
335 /*********************************************************************
337 * Function : cgi_error_404
339 * Description : CGI function that is called if an unknown action was
343 * 1 : csp = Current client state (buffers, headers, etc...)
344 * 2 : rsp = http_response data structure for output
345 * 3 : parameters = map of cgi parameters
347 * CGI Parameters : none
349 * Returns : JB_ERR_OK on success
350 * JB_ERR_MEMORY on out-of-memory error.
352 *********************************************************************/
353 jb_err cgi_error_404(struct client_state *csp,
354 struct http_response *rsp,
355 const struct map *parameters)
363 if (NULL == (exports = default_exports(csp, NULL)))
365 return JB_ERR_MEMORY;
368 rsp->status = strdup("404 Privoxy configuration page not found");
369 if (rsp->status == NULL)
372 return JB_ERR_MEMORY;
375 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
379 #ifdef FEATURE_GRACEFUL_TERMINATION
380 /*********************************************************************
384 * Description : CGI function to shut down Privoxy.
385 * NOTE: Turning this on in a production build
386 * would be a BAD idea. An EXTREMELY BAD idea.
387 * In short, don't do it.
390 * 1 : csp = Current client state (buffers, headers, etc...)
391 * 2 : rsp = http_response data structure for output
392 * 3 : parameters = map of cgi parameters
394 * CGI Parameters : none
396 * Returns : JB_ERR_OK on success
397 * JB_ERR_MEMORY on out-of-memory error.
399 *********************************************************************/
400 jb_err cgi_die (struct client_state *csp,
401 struct http_response *rsp,
402 const struct map *parameters)
412 * I don't really care what gets sent back to the browser.
413 * Take the easy option - "out of memory" page.
416 return JB_ERR_MEMORY;
418 #endif /* def FEATURE_GRACEFUL_TERMINATION */
421 /*********************************************************************
423 * Function : cgi_show_request
425 * Description : Show the client's request and what sed() would have
429 * 1 : csp = Current client state (buffers, headers, etc...)
430 * 2 : rsp = http_response data structure for output
431 * 3 : parameters = map of cgi parameters
433 * CGI Parameters : none
435 * Returns : JB_ERR_OK on success
436 * JB_ERR_MEMORY on out-of-memory error.
438 *********************************************************************/
439 jb_err cgi_show_request(struct client_state *csp,
440 struct http_response *rsp,
441 const struct map *parameters)
450 if (NULL == (exports = default_exports(csp, "show-request")))
452 return JB_ERR_MEMORY;
456 * Repair the damage done to the IOB by get_header()
458 for (p = csp->iob->buf; p < csp->iob->eod; p++)
460 if (*p == '\0') *p = '\n';
464 * Export the original client's request and the one we would
465 * be sending to the server if this wasn't a CGI call
468 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
471 return JB_ERR_MEMORY;
474 if (map(exports, "processed-request", 1, html_encode_and_free_original(
475 sed(client_patterns, add_client_headers, csp)), 0))
478 return JB_ERR_MEMORY;
481 return template_fill_for_cgi(csp, "show-request", exports, rsp);
485 /*********************************************************************
487 * Function : cgi_send_banner
489 * Description : CGI function that returns a banner.
492 * 1 : csp = Current client state (buffers, headers, etc...)
493 * 2 : rsp = http_response data structure for output
494 * 3 : parameters = map of cgi parameters
497 * type : Selects the type of banner between "trans", "logo",
498 * and "auto". Defaults to "logo" if absent or invalid.
499 * "auto" means to select as if we were image-blocking.
500 * (Only the first character really counts; b and t are
503 * Returns : JB_ERR_OK on success
504 * JB_ERR_MEMORY on out-of-memory error.
506 *********************************************************************/
507 jb_err cgi_send_banner(struct client_state *csp,
508 struct http_response *rsp,
509 const struct map *parameters)
511 char imagetype = lookup(parameters, "type")[0];
514 * If type is auto, then determine the right thing
515 * to do from the set-image-blocker action
517 if (imagetype == 'a')
524 #ifdef FEATURE_IMAGE_BLOCKING
525 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
527 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
528 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
529 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
533 /* Use default - nothing to do here. */
535 else if (0 == strcmpic(p, "blank"))
539 else if (0 == strcmpic(p, "pattern"))
545 * If the action is to call this CGI, determine
548 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
550 imagetype = p[sizeof(prefix1) - 1];
552 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
554 imagetype = p[sizeof(prefix2) - 1];
558 * Everything else must (should) be a URL to
566 #endif /* def FEATURE_IMAGE_BLOCKING */
570 * Now imagetype is either the non-auto type we were called with,
571 * or it was auto and has since been determined. In any case, we
572 * can proceed to actually answering the request by sending a redirect
573 * or an image as appropriate:
575 if (imagetype == 'r')
577 rsp->status = strdup("302 Local Redirect from Privoxy");
578 if (rsp->status == NULL)
580 return JB_ERR_MEMORY;
582 if (enlist_unique_header(rsp->headers, "Location",
583 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
585 return JB_ERR_MEMORY;
590 if ((imagetype == 'b') || (imagetype == 't'))
592 rsp->body = bindup(image_blank_data, image_blank_length);
593 rsp->content_length = image_blank_length;
597 rsp->body = bindup(image_pattern_data, image_pattern_length);
598 rsp->content_length = image_pattern_length;
601 if (rsp->body == NULL)
603 return JB_ERR_MEMORY;
605 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
607 return JB_ERR_MEMORY;
618 /*********************************************************************
620 * Function : cgi_transparent_image
622 * Description : CGI function that sends a 1x1 transparent image.
625 * 1 : csp = Current client state (buffers, headers, etc...)
626 * 2 : rsp = http_response data structure for output
627 * 3 : parameters = map of cgi parameters
629 * CGI Parameters : None
631 * Returns : JB_ERR_OK on success
632 * JB_ERR_MEMORY on out-of-memory error.
634 *********************************************************************/
635 jb_err cgi_transparent_image(struct client_state *csp,
636 struct http_response *rsp,
637 const struct map *parameters)
639 rsp->body = bindup(image_blank_data, image_blank_length);
640 rsp->content_length = image_blank_length;
642 if (rsp->body == NULL)
644 return JB_ERR_MEMORY;
647 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
649 return JB_ERR_MEMORY;
659 /*********************************************************************
661 * Function : cgi_send_stylesheet
663 * Description : CGI function that sends a css stylesheet found
664 * in the cgi-style.css template
667 * 1 : csp = Current client state (buffers, headers, etc...)
668 * 2 : rsp = http_response data structure for output
669 * 3 : parameters = map of cgi parameters
671 * CGI Parameters : None
673 * Returns : JB_ERR_OK on success
674 * JB_ERR_MEMORY on out-of-memory error.
676 *********************************************************************/
677 jb_err cgi_send_stylesheet(struct client_state *csp,
678 struct http_response *rsp,
679 const struct map *parameters)
686 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
688 if (err == JB_ERR_FILE)
691 * No way to tell user; send empty stylesheet
693 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
697 return err; /* JB_ERR_MEMORY */
700 if (enlist(rsp->headers, "Content-Type: text/css"))
702 return JB_ERR_MEMORY;
708 /*********************************************************************
710 * Function : cgi_send_user_manual
712 * Description : CGI function that sends a file in the user
716 * 1 : csp = Current client state (buffers, headers, etc...)
717 * 2 : rsp = http_response data structure for output
718 * 3 : parameters = map of cgi parameters
720 * CGI Parameters : file=name.html, the name of the HTML file
721 * (relative to user-manual from config)
723 * Returns : JB_ERR_OK on success
724 * JB_ERR_MEMORY on out-of-memory error.
726 *********************************************************************/
727 jb_err cgi_send_user_manual(struct client_state *csp,
728 struct http_response *rsp,
729 const struct map *parameters)
731 const char * filename;
734 jb_err err = JB_ERR_OK;
741 if (!parameters->first)
743 /* requested http://p.p/user-manual (without trailing slash) */
744 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
747 get_string_param(parameters, "file", &filename);
748 /* Check paramter for hack attempts */
749 if (filename && strchr(filename, '/'))
751 return JB_ERR_CGI_PARAMS;
753 if (filename && strstr(filename, ".."))
755 return JB_ERR_CGI_PARAMS;
758 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
759 if (full_path == NULL)
761 return JB_ERR_MEMORY;
764 /* Open user-manual file */
767 * XXX: Do we support other operating systems that
768 * require special treatment to fopen in binary mode?
770 if (NULL == (fp = fopen(full_path, "rb")))
772 if (NULL == (fp = fopen(full_path, "r")))
773 #endif /* def WIN32 */
775 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
776 err = cgi_error_no_template(csp, rsp, full_path);
781 /* Get file length */
782 fseek(fp, 0, SEEK_END);
783 length = (size_t)ftell(fp);
784 fseek(fp, 0, SEEK_SET);
786 /* Allocate memory and load the file directly into the body */
787 rsp->body = (char *)malloc(length+1);
792 return JB_ERR_MEMORY;
794 memset(rsp->body, '\0', length+1);
795 if (!fread(rsp->body, length, 1, fp))
798 * This happens if we didn't fopen in binary mode.
799 * If it does, we just log it and serve what we got.
801 log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
806 rsp->content_length = length;
808 /* Guess correct Content-Type based on the filename's ending */
811 length = strlen(filename);
817 if((length>=4) && !strcmp(&filename[length-4], ".css"))
819 err = enlist(rsp->headers, "Content-Type: text/css");
821 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
823 err = enlist(rsp->headers, "Content-Type: image/jpeg");
827 err = enlist(rsp->headers, "Content-Type: text/html");
834 /*********************************************************************
836 * Function : cgi_show_version
838 * Description : CGI function that returns a a web page describing the
839 * file versions of Privoxy.
842 * 1 : csp = Current client state (buffers, headers, etc...)
843 * 2 : rsp = http_response data structure for output
844 * 3 : parameters = map of cgi parameters
846 * CGI Parameters : none
848 * Returns : JB_ERR_OK on success
849 * JB_ERR_MEMORY on out-of-memory error.
851 *********************************************************************/
852 jb_err cgi_show_version(struct client_state *csp,
853 struct http_response *rsp,
854 const struct map *parameters)
862 if (NULL == (exports = default_exports(csp, "show-version")))
864 return JB_ERR_MEMORY;
867 if (map(exports, "sourceversions", 1, show_rcs(), 0))
870 return JB_ERR_MEMORY;
873 return template_fill_for_cgi(csp, "show-version", exports, rsp);
877 /*********************************************************************
879 * Function : cgi_show_status
881 * Description : CGI function that returns a web page describing the
882 * current status of Privoxy.
885 * 1 : csp = Current client state (buffers, headers, etc...)
886 * 2 : rsp = http_response data structure for output
887 * 3 : parameters = map of cgi parameters
890 * file : Which file to show. Only first letter is checked,
892 * - "p"ermissions (actions) file
895 * Default is to show menu and other information.
897 * Returns : JB_ERR_OK on success
898 * JB_ERR_MEMORY on out-of-memory error.
900 *********************************************************************/
901 jb_err cgi_show_status(struct client_state *csp,
902 struct http_response *rsp,
903 const struct map *parameters)
910 char buf[BUFFER_SIZE];
911 const char * filename = NULL;
912 char * file_description = NULL;
913 #ifdef FEATURE_STATISTICS
914 float perc_rej; /* Percentage of http requests rejected */
916 int local_urls_rejected;
917 #endif /* ndef FEATURE_STATISTICS */
918 jb_err err = JB_ERR_OK;
926 if (NULL == (exports = default_exports(csp, "show-status")))
928 return JB_ERR_MEMORY;
931 switch (*(lookup(parameters, "file")))
934 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
936 filename = csp->actions_list[i]->filename;
937 file_description = "Actions File";
942 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
944 filename = csp->rlist[i]->filename;
945 file_description = "Filter File";
953 filename = csp->tlist->filename;
954 file_description = "Trust File";
957 #endif /* def FEATURE_TRUST */
960 if (NULL != filename)
962 if ( map(exports, "file-description", 1, file_description, 1)
963 || map(exports, "filepath", 1, html_encode(filename), 0) )
966 return JB_ERR_MEMORY;
969 if ((fp = fopen(filename, "r")) == NULL)
971 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
974 return JB_ERR_MEMORY;
980 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
982 string_join (&s, html_encode(buf));
986 if (map(exports, "contents", 1, s, 0))
989 return JB_ERR_MEMORY;
993 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
997 for (j = 0; (s != NULL) && (j < Argc); j++)
999 if (!err) err = string_join (&s, html_encode(Argv[j]));
1000 if (!err) err = string_append(&s, " ");
1002 if (!err) err = map(exports, "invocation", 1, s, 0);
1004 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1005 if (!err) err = show_defines(exports);
1010 return JB_ERR_MEMORY;
1013 #ifdef FEATURE_STATISTICS
1014 local_urls_read = urls_read;
1015 local_urls_rejected = urls_rejected;
1018 * Need to alter the stats not to include the fetch of this
1021 * Can't do following thread safely! doh!
1024 * urls_rejected--; * This will be incremented subsequently *
1027 if (local_urls_read == 0)
1029 if (!err) err = map_block_killer(exports, "have-stats");
1033 if (!err) err = map_block_killer(exports, "have-no-stats");
1035 perc_rej = (float)local_urls_rejected * 100.0F /
1036 (float)local_urls_read;
1038 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1039 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1041 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1042 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1044 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1045 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1048 #else /* ndef FEATURE_STATISTICS */
1049 err = err || map_block_killer(exports, "statistics");
1050 #endif /* ndef FEATURE_STATISTICS */
1053 * List all action files in use, together with view and edit links,
1054 * except for standard.action, which should only be viewable. (Not
1055 * enforced in the editor itself)
1056 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1059 for (i = 0; i < MAX_AF_FILES; i++)
1061 if (csp->actions_list[i] != NULL)
1063 if (!err) err = string_append(&s, "<tr><td>");
1064 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1065 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1066 if (!err) err = string_append(&s, buf);
1068 #ifdef FEATURE_CGI_EDIT_ACTIONS
1069 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1071 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1072 if (!err) err = string_append(&s, buf);
1076 if (!err) err = string_append(&s, "</td></tr>\n");
1081 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1085 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1089 * List all re_filterfiles in use, together with view options.
1090 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1093 for (i = 0; i < MAX_AF_FILES; i++)
1095 if (csp->rlist[i] != NULL)
1097 if (!err) err = string_append(&s, "<tr><td>");
1098 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1100 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1101 if (!err) err = string_append(&s, buf);
1102 if (!err) err = string_append(&s, "</td></tr>\n");
1107 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1111 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1112 if (!err) err = map_block_killer(exports, "have-filterfile");
1115 #ifdef FEATURE_TRUST
1118 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1122 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1123 if (!err) err = map_block_killer(exports, "have-trustfile");
1126 if (!err) err = map_block_killer(exports, "trust-support");
1127 #endif /* ndef FEATURE_TRUST */
1132 return JB_ERR_MEMORY;
1135 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1139 /*********************************************************************
1141 * Function : cgi_show_url_info
1143 * Description : CGI function that determines and shows which actions
1144 * Privoxy will perform for a given url, and which
1145 * matches starting from the defaults have lead to that.
1148 * 1 : csp = Current client state (buffers, headers, etc...)
1149 * 2 : rsp = http_response data structure for output
1150 * 3 : parameters = map of cgi parameters
1153 * url : The url whose actions are to be determined.
1154 * If url is unset, the url-given conditional will be
1155 * set, so that all but the form can be suppressed in
1158 * Returns : JB_ERR_OK on success
1159 * JB_ERR_MEMORY on out-of-memory error.
1161 *********************************************************************/
1162 jb_err cgi_show_url_info(struct client_state *csp,
1163 struct http_response *rsp,
1164 const struct map *parameters)
1167 struct map *exports;
1174 if (NULL == (exports = default_exports(csp, "show-url-info")))
1176 return JB_ERR_MEMORY;
1180 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1182 url_param = strdup(lookup(parameters, "url"));
1183 if (url_param == NULL)
1186 return JB_ERR_MEMORY;
1191 * Handle prefixes. 4 possibilities:
1192 * 1) "http://" or "https://" prefix present and followed by URL - OK
1193 * 2) Only the "http://" or "https://" part is present, no URL - change
1194 * to empty string so it will be detected later as "no URL".
1195 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1197 * 4) Parameter not specified or is empty string - let this fall through
1198 * for now, next block of code will handle it.
1200 if (0 == strncmp(url_param, "http://", 7))
1202 if (url_param[7] == '\0')
1205 * Empty URL (just prefix).
1206 * Make it totally empty so it's caught by the next if()
1208 url_param[0] = '\0';
1211 else if (0 == strncmp(url_param, "https://", 8))
1213 if (url_param[8] == '\0')
1216 * Empty URL (just prefix).
1217 * Make it totally empty so it's caught by the next if()
1219 url_param[0] = '\0';
1222 else if (url_param[0] != '\0')
1225 * Unknown prefix - assume http://
1227 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1228 if (NULL == url_param_prefixed)
1232 return JB_ERR_MEMORY;
1234 strcpy(url_param_prefixed, "http://");
1235 strcpy(url_param_prefixed + 7, url_param);
1237 url_param = url_param_prefixed;
1241 if (url_param[0] == '\0')
1243 /* URL paramater not specified, display query form only. */
1245 if (map_block_killer(exports, "url-given")
1246 || map(exports, "url", 1, "", 1))
1249 return JB_ERR_MEMORY;
1254 /* Given a URL, so query it. */
1259 struct file_list *fl;
1260 struct url_actions *b;
1261 struct http_request url_to_query[1];
1262 struct current_action_spec action[1];
1265 if (map(exports, "url", 1, html_encode(url_param), 0))
1269 return JB_ERR_MEMORY;
1272 init_current_action(action);
1274 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1276 free_current_action(action);
1279 return JB_ERR_MEMORY;
1282 err = parse_http_url(url_param, url_to_query, csp);
1286 if (err == JB_ERR_MEMORY)
1288 free_http_request(url_to_query);
1289 free_current_action(action);
1291 return JB_ERR_MEMORY;
1297 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1298 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1300 free_current_action(action);
1301 free_http_request(url_to_query);
1306 return JB_ERR_MEMORY;
1309 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1313 * We have a warning about SSL paths. Hide it for insecure sites.
1315 if (!url_to_query->ssl)
1317 if (map_block_killer(exports, "https"))
1319 free_current_action(action);
1321 free_http_request(url_to_query);
1322 return JB_ERR_MEMORY;
1326 matches = strdup("<table class=\"transparent\">");
1328 for (i = 0; i < MAX_AF_FILES; i++)
1330 if (NULL == csp->config->actions_file_short[i]
1331 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1335 if ((fl = csp->actions_list[i]) != NULL)
1337 if ((b = fl->f) != NULL)
1339 /* FIXME: Hardcoded HTML! */
1340 string_append(&matches, "<tr><th>In file: ");
1341 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1342 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1343 string_append(&matches, buf);
1344 string_append(&matches, "View</a>");
1345 #ifdef FEATURE_CGI_EDIT_ACTIONS
1346 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1347 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1348 string_append(&matches, "\">Edit</a>");
1350 string_append(&matches, "</th></tr>\n");
1357 for (; (b != NULL) && (matches != NULL); b = b->next)
1359 if (url_match(b->url, url_to_query))
1361 string_append(&matches, "<tr><td>{");
1362 string_join (&matches, actions_to_html(csp, b->action));
1363 string_append(&matches, " }</b><br>\n<code>");
1364 string_join (&matches, html_encode(b->url->spec));
1365 string_append(&matches, "</code></td></tr>\n");
1367 if (merge_current_action(action, b->action))
1370 free_http_request(url_to_query);
1371 free_current_action(action);
1373 return JB_ERR_MEMORY;
1381 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1384 string_append(&matches, "</table>\n");
1386 free_http_request(url_to_query);
1388 if (matches == NULL)
1390 free_current_action(action);
1392 return JB_ERR_MEMORY;
1395 if (map(exports, "matches", 1, matches , 0))
1397 free_current_action(action);
1399 return JB_ERR_MEMORY;
1402 s = current_action_to_html(csp, action);
1404 free_current_action(action);
1406 if (map(exports, "final", 1, s, 0))
1409 return JB_ERR_MEMORY;
1413 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1417 /*********************************************************************
1419 * Function : cgi_robots_txt
1421 * Description : CGI function to return "/robots.txt".
1424 * 1 : csp = Current client state (buffers, headers, etc...)
1425 * 2 : rsp = http_response data structure for output
1426 * 3 : parameters = map of cgi parameters
1428 * CGI Parameters : None
1430 * Returns : JB_ERR_OK on success
1431 * JB_ERR_MEMORY on out-of-memory error.
1433 *********************************************************************/
1434 jb_err cgi_robots_txt(struct client_state *csp,
1435 struct http_response *rsp,
1436 const struct map *parameters)
1442 "# This is the Privoxy control interface.\n"
1443 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1449 if (rsp->body == NULL)
1451 return JB_ERR_MEMORY;
1454 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1458 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1459 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1461 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1465 /*********************************************************************
1467 * Function : show_defines
1469 * Description : Add to a map the state od all conditional #defines
1470 * used when building
1473 * 1 : exports = map to extend
1475 * Returns : JB_ERR_OK on success
1476 * JB_ERR_MEMORY on out-of-memory error.
1478 *********************************************************************/
1479 static jb_err show_defines(struct map *exports)
1481 jb_err err = JB_ERR_OK;
1484 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1485 #else /* ifndef FEATURE_ACL */
1486 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1487 #endif /* ndef FEATURE_ACL */
1489 #ifdef FEATURE_CGI_EDIT_ACTIONS
1490 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1491 #else /* ifndef FEATURE_COOKIE_JAR */
1492 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1493 #endif /* ndef FEATURE_COOKIE_JAR */
1495 #ifdef FEATURE_COOKIE_JAR
1496 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1497 #else /* ifndef FEATURE_COOKIE_JAR */
1498 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1499 #endif /* ndef FEATURE_COOKIE_JAR */
1501 #ifdef FEATURE_FAST_REDIRECTS
1502 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1503 #else /* ifndef FEATURE_FAST_REDIRECTS */
1504 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1505 #endif /* ndef FEATURE_FAST_REDIRECTS */
1507 #ifdef FEATURE_FORCE_LOAD
1508 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1509 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1510 #else /* ifndef FEATURE_FORCE_LOAD */
1511 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1512 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1513 #endif /* ndef FEATURE_FORCE_LOAD */
1515 #ifdef FEATURE_GRACEFUL_TERMINATION
1516 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1517 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1518 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1519 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1521 #ifdef FEATURE_IMAGE_BLOCKING
1522 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1523 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1524 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1525 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1527 #ifdef FEATURE_IMAGE_DETECT_MSIE
1528 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1529 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1530 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1531 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1533 #ifdef FEATURE_KILL_POPUPS
1534 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1535 #else /* ifndef FEATURE_KILL_POPUPS */
1536 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1537 #endif /* ndef FEATURE_KILL_POPUPS */
1539 #ifdef FEATURE_NO_GIFS
1540 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1541 #else /* ifndef FEATURE_NO_GIFS */
1542 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1543 #endif /* ndef FEATURE_NO_GIFS */
1545 #ifdef FEATURE_PTHREAD
1546 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1547 #else /* ifndef FEATURE_PTHREAD */
1548 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1549 #endif /* ndef FEATURE_PTHREAD */
1551 #ifdef FEATURE_STATISTICS
1552 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1553 #else /* ifndef FEATURE_STATISTICS */
1554 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1555 #endif /* ndef FEATURE_STATISTICS */
1557 #ifdef FEATURE_TOGGLE
1558 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1559 #else /* ifndef FEATURE_TOGGLE */
1560 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1561 #endif /* ndef FEATURE_TOGGLE */
1563 #ifdef FEATURE_TRUST
1564 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1565 #else /* ifndef FEATURE_TRUST */
1566 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1567 #endif /* ndef FEATURE_TRUST */
1570 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1571 #else /* ifndef STATIC_PCRE */
1572 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1573 #endif /* ndef STATIC_PCRE */
1576 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1577 #else /* ifndef STATIC_PCRS */
1578 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1579 #endif /* ndef STATIC_PCRS */
1585 /*********************************************************************
1587 * Function : show_rcs
1589 * Description : Create a string with the rcs info for all sourcefiles
1593 * Returns : A string, or NULL on out-of-memory.
1595 *********************************************************************/
1596 static char *show_rcs(void)
1598 char *result = strdup("");
1599 char buf[BUFFER_SIZE];
1601 /* Instead of including *all* dot h's in the project (thus creating a
1602 * tremendous amount of dependencies), I will concede to declaring them
1603 * as extern's. This forces the developer to add to this list, but oh well.
1606 #define SHOW_RCS(__x) \
1608 extern const char __x[]; \
1609 snprintf(buf, sizeof(buf), " %s\n", __x); \
1610 string_append(&result, buf); \
1613 /* In alphabetical order */
1614 SHOW_RCS(actions_h_rcs)
1615 SHOW_RCS(actions_rcs)
1617 SHOW_RCS(amiga_h_rcs)
1619 #endif /* def AMIGA */
1622 #ifdef FEATURE_CGI_EDIT_ACTIONS
1623 SHOW_RCS(cgiedit_h_rcs)
1624 SHOW_RCS(cgiedit_rcs)
1625 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1626 SHOW_RCS(cgisimple_h_rcs)
1627 SHOW_RCS(cgisimple_rcs)
1629 SHOW_RCS(cygwin_h_rcs)
1631 SHOW_RCS(deanimate_h_rcs)
1632 SHOW_RCS(deanimate_rcs)
1633 SHOW_RCS(encode_h_rcs)
1634 SHOW_RCS(encode_rcs)
1635 SHOW_RCS(errlog_h_rcs)
1636 SHOW_RCS(errlog_rcs)
1637 SHOW_RCS(filters_h_rcs)
1638 SHOW_RCS(filters_rcs)
1639 SHOW_RCS(gateway_h_rcs)
1640 SHOW_RCS(gateway_rcs)
1641 SHOW_RCS(jbsockets_h_rcs)
1642 SHOW_RCS(jbsockets_rcs)
1645 #ifdef FEATURE_KILL_POPUPS
1646 SHOW_RCS(killpopup_h_rcs)
1647 SHOW_RCS(killpopup_rcs)
1648 #endif /* def FEATURE_KILL_POPUPS */
1649 SHOW_RCS(list_h_rcs)
1651 SHOW_RCS(loadcfg_h_rcs)
1652 SHOW_RCS(loadcfg_rcs)
1653 SHOW_RCS(loaders_h_rcs)
1654 SHOW_RCS(loaders_rcs)
1655 SHOW_RCS(miscutil_h_rcs)
1656 SHOW_RCS(miscutil_rcs)
1657 SHOW_RCS(parsers_h_rcs)
1658 SHOW_RCS(parsers_rcs)
1660 SHOW_RCS(pcrs_h_rcs)
1661 SHOW_RCS(project_h_rcs)
1662 SHOW_RCS(ssplit_h_rcs)
1663 SHOW_RCS(ssplit_rcs)
1664 SHOW_RCS(urlmatch_h_rcs)
1665 SHOW_RCS(urlmatch_rcs)
1667 #ifndef _WIN_CONSOLE
1668 SHOW_RCS(w32log_h_rcs)
1669 SHOW_RCS(w32log_rcs)
1670 SHOW_RCS(w32res_h_rcs)
1671 SHOW_RCS(w32taskbar_h_rcs)
1672 SHOW_RCS(w32taskbar_rcs)
1673 #endif /* ndef _WIN_CONSOLE */
1674 SHOW_RCS(win32_h_rcs)
1676 #endif /* def _WIN32 */