1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.43 2006/12/17 17:57:56 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.43 2006/12/17 17:57:56 fabiankeil
40 * - Added FEATURE_GRACEFUL_TERMINATION to the
41 * "conditional #defines" section
42 * - Escaped ampersands in generated HTML.
43 * - Renamed re-filter-filename to re-filter-filenames
45 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
46 * Add special treatment for WIN32 to make sure
47 * cgi_send_user_manual opens the files in binary mode.
48 * Fixes BR 1600411 and unbreaks image delivery.
50 * Remove outdated comment.
52 * Revision 1.41 2006/10/09 19:18:28 roro
53 * Redirect http://p.p/user-manual (without trailing slash) to
54 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
56 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
57 * Modified cgi_send_user_manual to serve binary
58 * content without destroying it first. Should also be
59 * faster now. Added ".jpg" check for Content-Type guessing.
61 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
62 * Deliver documents in the user-manual directory
63 * with "Content-Type text/css" if their filename
66 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
67 * Incorporate modified version of Roland Rosenfeld's patch to
68 * optionally access the user-manual via Privoxy. Closes patch 679075.
70 * Formatting changed to Privoxy style, added call to
71 * cgi_error_no_template if the requested file doesn't
72 * exist and modified check whether or not Privoxy itself
73 * should serve the manual. Should work cross-platform now.
75 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
76 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
77 * with what was really the latest development (the v_3_0_branch branch)
79 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
80 * Multiple filter file support
82 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
83 * Undo some damaging memory leak patches
85 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
86 * A few memory leaks plugged (mostly on error paths)
88 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
89 * Another instance of:
90 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
91 * Thanks to Magnus Holmgren for the patch
93 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
94 * - Prevent line wrap beween "View/Edit" link buttons on status page
95 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
97 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
98 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
100 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
101 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
103 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
104 * Applying patch from Andreas as provided by Hal on the list.
105 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
107 * Revision 1.35 2002/05/12 21:44:44 jongfoster
108 * Adding amiga.[ch] revision information, if on an amiga.
110 * Revision 1.34 2002/04/30 12:06:12 oes
111 * Deleted unused code from default_cgi
113 * Revision 1.33 2002/04/30 11:14:52 oes
114 * Made csp the first parameter in *action_to_html
116 * Revision 1.32 2002/04/26 18:29:13 jongfoster
117 * Fixing this Visual C++ warning:
118 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
120 * Revision 1.31 2002/04/26 12:54:36 oes
121 * - Kill obsolete REDIRECT_URL code
122 * - Error handling fixes
123 * - Style sheet related HTML snipplet changes
124 * - cgi_show_url_info:
125 * - Matches now in table, actions on single lines,
127 * - standard.action suppressed
128 * - Buttons to View and Edit AFs
130 * Revision 1.30 2002/04/24 02:18:08 oes
131 * - show-status is now the starting point for editing
132 * the actions files, generate list of all AFs with buttons
133 * for viewing and editing, new look for file list (Jon:
134 * buttons now aligned ;-P ), view mode now supports multiple
135 * AFs, name changes, no view links for unspecified files,
136 * no edit link for standard.action.
138 * - Jon's multiple AF patch: cgi_show_url_info now uses all
139 * AFs and marks the output accordingly
141 * Revision 1.29 2002/04/10 13:38:35 oes
142 * load_template signature changed
144 * Revision 1.28 2002/04/07 15:42:12 jongfoster
145 * Fixing send-banner?type=auto when the image-blocker is
146 * a redirect to send-banner
148 * Revision 1.27 2002/04/05 15:50:48 oes
149 * added send-stylesheet CGI
151 * Revision 1.26 2002/04/04 00:36:36 gliptak
152 * always use pcre for matching
154 * Revision 1.25 2002/04/03 22:28:03 gliptak
155 * Removed references to gnu_regex
157 * Revision 1.24 2002/04/02 16:12:47 oes
158 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
160 * Revision 1.23 2002/03/26 22:29:54 swa
161 * we have a new homepage!
163 * Revision 1.22 2002/03/24 16:18:15 jongfoster
166 * Revision 1.21 2002/03/24 15:23:33 jongfoster
169 * Revision 1.20 2002/03/24 13:25:43 swa
170 * name change related issues
172 * Revision 1.19 2002/03/16 23:54:06 jongfoster
173 * Adding graceful termination feature, to help look for memory leaks.
174 * If you enable this (which, by design, has to be done by hand
175 * editing config.h) and then go to http://i.j.b/die, then the program
176 * will exit cleanly after the *next* request. It should free all the
177 * memory that was used.
179 * Revision 1.18 2002/03/12 01:44:49 oes
180 * Changed default for "blocked" image from jb logo to checkboard pattern
182 * Revision 1.17 2002/03/08 16:43:18 oes
183 * Added choice beween GIF and PNG built-in images
185 * Revision 1.16 2002/03/07 03:48:38 oes
186 * - Changed built-in images from GIF to PNG
187 * (with regard to Unisys patent issue)
188 * - Added a 4x4 pattern PNG which is less intrusive
189 * than the logo but also clearly marks the deleted banners
191 * Revision 1.15 2002/03/06 22:54:35 jongfoster
192 * Automated function-comment nitpicking.
194 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
195 * Clean up a little CRLF unpleasantness that suddenly appeared
197 * Revision 1.13 2002/02/21 00:10:37 jongfoster
198 * Adding send-banner?type=auto option
200 * Revision 1.12 2002/01/23 01:03:32 jongfoster
201 * Fixing gcc [CygWin] compiler warnings
203 * Revision 1.11 2002/01/23 00:01:04 jongfoster
204 * Adding cgi_transparent_gif() for http://i.j.b/t
205 * Adding missing html_encode() to many CGI functions.
206 * Adding urlmatch.[ch] to http://i.j.b/show-version
208 * Revision 1.10 2002/01/17 21:10:37 jongfoster
209 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
210 * Also fixing a problem in the same function with improperly quoted URLs
211 * in output HTML, and adding code to handle https:// URLs correctly.
213 * Revision 1.9 2001/11/30 23:09:15 jongfoster
214 * Now reports on FEATURE_CGI_EDIT_ACTIONS
215 * Removing FEATURE_DENY_GZIP from template
217 * Revision 1.8 2001/11/13 00:14:07 jongfoster
218 * Fixing stupid bug now I've figured out what || means.
219 * (It always returns 0 or 1, not one of it's paramaters.)
221 * Revision 1.7 2001/10/23 21:48:19 jongfoster
222 * Cleaning up error handling in CGI functions - they now send back
223 * a HTML error page and should never cause a FATAL error. (Fixes one
224 * potential source of "denial of service" attacks).
226 * CGI actions file editor that works and is actually useful.
228 * Ability to toggle JunkBuster remotely using a CGI call.
230 * You can turn off both the above features in the main configuration
231 * file, e.g. if you are running a multi-user proxy.
233 * Revision 1.6 2001/10/14 22:00:32 jongfoster
234 * Adding support for a 404 error when an invalid CGI page is requested.
236 * Revision 1.5 2001/10/07 15:30:41 oes
237 * Removed FEATURE_DENY_GZIP
239 * Revision 1.4 2001/10/02 15:31:12 oes
240 * Introduced show-request cgi
242 * Revision 1.3 2001/09/22 16:34:44 jongfoster
243 * Removing unneeded #includes
245 * Revision 1.2 2001/09/19 18:01:11 oes
246 * Fixed comments; cosmetics
248 * Revision 1.1 2001/09/16 17:08:54 jongfoster
249 * Moving simple CGI functions from cgi.c to new file cgisimple.c
252 **********************************************************************/
258 #include <sys/types.h>
265 #define snprintf _snprintf
266 #endif /* def _WIN32 */
270 #include "cgisimple.h"
276 #include "miscutil.h"
279 #include "urlmatch.h"
282 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
285 static char *show_rcs(void);
286 static jb_err show_defines(struct map *exports);
289 /*********************************************************************
291 * Function : cgi_default
293 * Description : CGI function that is called for the CGI_SITE_1_HOST
294 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
295 * Boring - only exports the default exports.
298 * 1 : csp = Current client state (buffers, headers, etc...)
299 * 2 : rsp = http_response data structure for output
300 * 3 : parameters = map of cgi parameters
302 * CGI Parameters : none
304 * Returns : JB_ERR_OK on success
305 * JB_ERR_MEMORY on out-of-memory
307 *********************************************************************/
308 jb_err cgi_default(struct client_state *csp,
309 struct http_response *rsp,
310 const struct map *parameters)
317 if (NULL == (exports = default_exports(csp, "")))
319 return JB_ERR_MEMORY;
322 return template_fill_for_cgi(csp, "default", exports, rsp);
326 /*********************************************************************
328 * Function : cgi_error_404
330 * Description : CGI function that is called if an unknown action was
334 * 1 : csp = Current client state (buffers, headers, etc...)
335 * 2 : rsp = http_response data structure for output
336 * 3 : parameters = map of cgi parameters
338 * CGI Parameters : none
340 * Returns : JB_ERR_OK on success
341 * JB_ERR_MEMORY on out-of-memory error.
343 *********************************************************************/
344 jb_err cgi_error_404(struct client_state *csp,
345 struct http_response *rsp,
346 const struct map *parameters)
354 if (NULL == (exports = default_exports(csp, NULL)))
356 return JB_ERR_MEMORY;
359 rsp->status = strdup("404 Privoxy configuration page not found");
360 if (rsp->status == NULL)
363 return JB_ERR_MEMORY;
366 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
370 #ifdef FEATURE_GRACEFUL_TERMINATION
371 /*********************************************************************
375 * Description : CGI function to shut down Privoxy.
376 * NOTE: Turning this on in a production build
377 * would be a BAD idea. An EXTREMELY BAD idea.
378 * In short, don't do it.
381 * 1 : csp = Current client state (buffers, headers, etc...)
382 * 2 : rsp = http_response data structure for output
383 * 3 : parameters = map of cgi parameters
385 * CGI Parameters : none
387 * Returns : JB_ERR_OK on success
388 * JB_ERR_MEMORY on out-of-memory error.
390 *********************************************************************/
391 jb_err cgi_die (struct client_state *csp,
392 struct http_response *rsp,
393 const struct map *parameters)
403 * I don't really care what gets sent back to the browser.
404 * Take the easy option - "out of memory" page.
407 return JB_ERR_MEMORY;
409 #endif /* def FEATURE_GRACEFUL_TERMINATION */
412 /*********************************************************************
414 * Function : cgi_show_request
416 * Description : Show the client's request and what sed() would have
420 * 1 : csp = Current client state (buffers, headers, etc...)
421 * 2 : rsp = http_response data structure for output
422 * 3 : parameters = map of cgi parameters
424 * CGI Parameters : none
426 * Returns : JB_ERR_OK on success
427 * JB_ERR_MEMORY on out-of-memory error.
429 *********************************************************************/
430 jb_err cgi_show_request(struct client_state *csp,
431 struct http_response *rsp,
432 const struct map *parameters)
441 if (NULL == (exports = default_exports(csp, "show-request")))
443 return JB_ERR_MEMORY;
447 * Repair the damage done to the IOB by get_header()
449 for (p = csp->iob->buf; p < csp->iob->eod; p++)
451 if (*p == '\0') *p = '\n';
455 * Export the original client's request and the one we would
456 * be sending to the server if this wasn't a CGI call
459 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
462 return JB_ERR_MEMORY;
465 if (map(exports, "processed-request", 1, html_encode_and_free_original(
466 sed(client_patterns, add_client_headers, csp)), 0))
469 return JB_ERR_MEMORY;
472 return template_fill_for_cgi(csp, "show-request", exports, rsp);
476 /*********************************************************************
478 * Function : cgi_send_banner
480 * Description : CGI function that returns a banner.
483 * 1 : csp = Current client state (buffers, headers, etc...)
484 * 2 : rsp = http_response data structure for output
485 * 3 : parameters = map of cgi parameters
488 * type : Selects the type of banner between "trans", "logo",
489 * and "auto". Defaults to "logo" if absent or invalid.
490 * "auto" means to select as if we were image-blocking.
491 * (Only the first character really counts; b and t are
494 * Returns : JB_ERR_OK on success
495 * JB_ERR_MEMORY on out-of-memory error.
497 *********************************************************************/
498 jb_err cgi_send_banner(struct client_state *csp,
499 struct http_response *rsp,
500 const struct map *parameters)
502 char imagetype = lookup(parameters, "type")[0];
505 * If type is auto, then determine the right thing
506 * to do from the set-image-blocker action
508 if (imagetype == 'a')
515 #ifdef FEATURE_IMAGE_BLOCKING
516 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
518 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
519 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
520 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
524 /* Use default - nothing to do here. */
526 else if (0 == strcmpic(p, "blank"))
530 else if (0 == strcmpic(p, "pattern"))
536 * If the action is to call this CGI, determine
539 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
541 imagetype = p[sizeof(prefix1) - 1];
543 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
545 imagetype = p[sizeof(prefix2) - 1];
549 * Everything else must (should) be a URL to
557 #endif /* def FEATURE_IMAGE_BLOCKING */
561 * Now imagetype is either the non-auto type we were called with,
562 * or it was auto and has since been determined. In any case, we
563 * can proceed to actually answering the request by sending a redirect
564 * or an image as appropriate:
566 if (imagetype == 'r')
568 rsp->status = strdup("302 Local Redirect from Privoxy");
569 if (rsp->status == NULL)
571 return JB_ERR_MEMORY;
573 if (enlist_unique_header(rsp->headers, "Location",
574 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
576 return JB_ERR_MEMORY;
581 if ((imagetype == 'b') || (imagetype == 't'))
583 rsp->body = bindup(image_blank_data, image_blank_length);
584 rsp->content_length = image_blank_length;
588 rsp->body = bindup(image_pattern_data, image_pattern_length);
589 rsp->content_length = image_pattern_length;
592 if (rsp->body == NULL)
594 return JB_ERR_MEMORY;
596 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
598 return JB_ERR_MEMORY;
609 /*********************************************************************
611 * Function : cgi_transparent_image
613 * Description : CGI function that sends a 1x1 transparent image.
616 * 1 : csp = Current client state (buffers, headers, etc...)
617 * 2 : rsp = http_response data structure for output
618 * 3 : parameters = map of cgi parameters
620 * CGI Parameters : None
622 * Returns : JB_ERR_OK on success
623 * JB_ERR_MEMORY on out-of-memory error.
625 *********************************************************************/
626 jb_err cgi_transparent_image(struct client_state *csp,
627 struct http_response *rsp,
628 const struct map *parameters)
630 rsp->body = bindup(image_blank_data, image_blank_length);
631 rsp->content_length = image_blank_length;
633 if (rsp->body == NULL)
635 return JB_ERR_MEMORY;
638 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
640 return JB_ERR_MEMORY;
650 /*********************************************************************
652 * Function : cgi_send_stylesheet
654 * Description : CGI function that sends a css stylesheet found
655 * in the cgi-style.css template
658 * 1 : csp = Current client state (buffers, headers, etc...)
659 * 2 : rsp = http_response data structure for output
660 * 3 : parameters = map of cgi parameters
662 * CGI Parameters : None
664 * Returns : JB_ERR_OK on success
665 * JB_ERR_MEMORY on out-of-memory error.
667 *********************************************************************/
668 jb_err cgi_send_stylesheet(struct client_state *csp,
669 struct http_response *rsp,
670 const struct map *parameters)
677 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
679 if (err == JB_ERR_FILE)
682 * No way to tell user; send empty stylesheet
684 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
688 return err; /* JB_ERR_MEMORY */
691 if (enlist(rsp->headers, "Content-Type: text/css"))
693 return JB_ERR_MEMORY;
699 /*********************************************************************
701 * Function : cgi_send_user_manual
703 * Description : CGI function that sends a file in the user
707 * 1 : csp = Current client state (buffers, headers, etc...)
708 * 2 : rsp = http_response data structure for output
709 * 3 : parameters = map of cgi parameters
711 * CGI Parameters : file=name.html, the name of the HTML file
712 * (relative to user-manual from config)
714 * Returns : JB_ERR_OK on success
715 * JB_ERR_MEMORY on out-of-memory error.
717 *********************************************************************/
718 jb_err cgi_send_user_manual(struct client_state *csp,
719 struct http_response *rsp,
720 const struct map *parameters)
722 const char * filename;
725 jb_err err = JB_ERR_OK;
732 if (!parameters->first)
734 /* requested http://p.p/user-manual (without trailing slash) */
735 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
738 get_string_param(parameters, "file", &filename);
739 /* Check paramter for hack attempts */
740 if (filename && strchr(filename, '/'))
742 return JB_ERR_CGI_PARAMS;
744 if (filename && strstr(filename, ".."))
746 return JB_ERR_CGI_PARAMS;
749 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
750 if (full_path == NULL)
752 return JB_ERR_MEMORY;
755 /* Open user-manual file */
758 * XXX: Do we support other operating systems that
759 * require special treatment to fopen in binary mode?
761 if (NULL == (fp = fopen(full_path, "rb")))
763 if (NULL == (fp = fopen(full_path, "r")))
764 #endif /* def WIN32 */
766 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
767 err = cgi_error_no_template(csp, rsp, full_path);
772 /* Get file length */
773 fseek(fp, 0, SEEK_END);
775 fseek(fp, 0, SEEK_SET);
777 /* Allocate memory and load the file directly into the body */
778 rsp->body = (char *)malloc(length+1);
783 return JB_ERR_MEMORY;
785 if (!fread(rsp->body, length, 1, fp))
788 * This happens if we didn't fopen in binary mode.
789 * If it does, we just log it and serve what we got,
790 * most likely padded with garbage.
792 log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
797 rsp->content_length = (int)length;
799 /* Guess correct Content-Type based on the filename's ending */
802 length = strlen(filename);
808 if((length>=4) && !strcmp(&filename[length-4], ".css"))
810 err = enlist(rsp->headers, "Content-Type: text/css");
812 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
814 err = enlist(rsp->headers, "Content-Type: image/jpeg");
818 err = enlist(rsp->headers, "Content-Type: text/html");
825 /*********************************************************************
827 * Function : cgi_show_version
829 * Description : CGI function that returns a a web page describing the
830 * file versions of Privoxy.
833 * 1 : csp = Current client state (buffers, headers, etc...)
834 * 2 : rsp = http_response data structure for output
835 * 3 : parameters = map of cgi parameters
837 * CGI Parameters : none
839 * Returns : JB_ERR_OK on success
840 * JB_ERR_MEMORY on out-of-memory error.
842 *********************************************************************/
843 jb_err cgi_show_version(struct client_state *csp,
844 struct http_response *rsp,
845 const struct map *parameters)
853 if (NULL == (exports = default_exports(csp, "show-version")))
855 return JB_ERR_MEMORY;
858 if (map(exports, "sourceversions", 1, show_rcs(), 0))
861 return JB_ERR_MEMORY;
864 return template_fill_for_cgi(csp, "show-version", exports, rsp);
868 /*********************************************************************
870 * Function : cgi_show_status
872 * Description : CGI function that returns a web page describing the
873 * current status of Privoxy.
876 * 1 : csp = Current client state (buffers, headers, etc...)
877 * 2 : rsp = http_response data structure for output
878 * 3 : parameters = map of cgi parameters
881 * file : Which file to show. Only first letter is checked,
883 * - "p"ermissions (actions) file
886 * Default is to show menu and other information.
888 * Returns : JB_ERR_OK on success
889 * JB_ERR_MEMORY on out-of-memory error.
891 *********************************************************************/
892 jb_err cgi_show_status(struct client_state *csp,
893 struct http_response *rsp,
894 const struct map *parameters)
901 char buf[BUFFER_SIZE];
902 const char * filename = NULL;
903 char * file_description = NULL;
904 #ifdef FEATURE_STATISTICS
905 float perc_rej; /* Percentage of http requests rejected */
907 int local_urls_rejected;
908 #endif /* ndef FEATURE_STATISTICS */
909 jb_err err = JB_ERR_OK;
917 if (NULL == (exports = default_exports(csp, "show-status")))
919 return JB_ERR_MEMORY;
922 switch (*(lookup(parameters, "file")))
925 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
927 filename = csp->actions_list[i]->filename;
928 file_description = "Actions File";
933 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
935 filename = csp->rlist[i]->filename;
936 file_description = "Filter File";
944 filename = csp->tlist->filename;
945 file_description = "Trust File";
948 #endif /* def FEATURE_TRUST */
951 if (NULL != filename)
953 if ( map(exports, "file-description", 1, file_description, 1)
954 || map(exports, "filepath", 1, html_encode(filename), 0) )
957 return JB_ERR_MEMORY;
960 if ((fp = fopen(filename, "r")) == NULL)
962 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
965 return JB_ERR_MEMORY;
971 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
973 string_join (&s, html_encode(buf));
977 if (map(exports, "contents", 1, s, 0))
980 return JB_ERR_MEMORY;
984 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
988 for (j = 0; (s != NULL) && (j < Argc); j++)
990 if (!err) err = string_join (&s, html_encode(Argv[j]));
991 if (!err) err = string_append(&s, " ");
993 if (!err) err = map(exports, "invocation", 1, s, 0);
995 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
996 if (!err) err = show_defines(exports);
1001 return JB_ERR_MEMORY;
1004 #ifdef FEATURE_STATISTICS
1005 local_urls_read = urls_read;
1006 local_urls_rejected = urls_rejected;
1009 * Need to alter the stats not to include the fetch of this
1012 * Can't do following thread safely! doh!
1015 * urls_rejected--; * This will be incremented subsequently *
1018 if (local_urls_read == 0)
1020 if (!err) err = map_block_killer(exports, "have-stats");
1024 if (!err) err = map_block_killer(exports, "have-no-stats");
1026 perc_rej = (float)local_urls_rejected * 100.0F /
1027 (float)local_urls_read;
1029 sprintf(buf, "%d", local_urls_read);
1030 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1032 sprintf(buf, "%d", local_urls_rejected);
1033 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1035 sprintf(buf, "%6.2f", perc_rej);
1036 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1039 #else /* ndef FEATURE_STATISTICS */
1040 err = err || map_block_killer(exports, "statistics");
1041 #endif /* ndef FEATURE_STATISTICS */
1044 * List all action files in use, together with view and edit links,
1045 * except for standard.action, which should only be viewable. (Not
1046 * enforced in the editor itself)
1047 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1050 for (i = 0; i < MAX_AF_FILES; i++)
1052 if (csp->actions_list[i] != NULL)
1054 if (!err) err = string_append(&s, "<tr><td>");
1055 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1056 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1057 if (!err) err = string_append(&s, buf);
1059 #ifdef FEATURE_CGI_EDIT_ACTIONS
1060 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1062 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1063 if (!err) err = string_append(&s, buf);
1067 if (!err) err = string_append(&s, "</td></tr>\n");
1072 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1076 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1080 * List all re_filterfiles in use, together with view options.
1081 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1084 for (i = 0; i < MAX_AF_FILES; i++)
1086 if (csp->rlist[i] != NULL)
1088 if (!err) err = string_append(&s, "<tr><td>");
1089 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1091 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1092 if (!err) err = string_append(&s, buf);
1093 if (!err) err = string_append(&s, "</td></tr>\n");
1098 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1102 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1103 if (!err) err = map_block_killer(exports, "have-filterfile");
1106 #ifdef FEATURE_TRUST
1109 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1113 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1114 if (!err) err = map_block_killer(exports, "have-trustfile");
1117 if (!err) err = map_block_killer(exports, "trust-support");
1118 #endif /* ndef FEATURE_TRUST */
1123 return JB_ERR_MEMORY;
1126 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1130 /*********************************************************************
1132 * Function : cgi_show_url_info
1134 * Description : CGI function that determines and shows which actions
1135 * Privoxy will perform for a given url, and which
1136 * matches starting from the defaults have lead to that.
1139 * 1 : csp = Current client state (buffers, headers, etc...)
1140 * 2 : rsp = http_response data structure for output
1141 * 3 : parameters = map of cgi parameters
1144 * url : The url whose actions are to be determined.
1145 * If url is unset, the url-given conditional will be
1146 * set, so that all but the form can be suppressed in
1149 * Returns : JB_ERR_OK on success
1150 * JB_ERR_MEMORY on out-of-memory error.
1152 *********************************************************************/
1153 jb_err cgi_show_url_info(struct client_state *csp,
1154 struct http_response *rsp,
1155 const struct map *parameters)
1158 struct map *exports;
1165 if (NULL == (exports = default_exports(csp, "show-url-info")))
1167 return JB_ERR_MEMORY;
1171 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1173 url_param = strdup(lookup(parameters, "url"));
1174 if (url_param == NULL)
1177 return JB_ERR_MEMORY;
1182 * Handle prefixes. 4 possibilities:
1183 * 1) "http://" or "https://" prefix present and followed by URL - OK
1184 * 2) Only the "http://" or "https://" part is present, no URL - change
1185 * to empty string so it will be detected later as "no URL".
1186 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1188 * 4) Parameter not specified or is empty string - let this fall through
1189 * for now, next block of code will handle it.
1191 if (0 == strncmp(url_param, "http://", 7))
1193 if (url_param[7] == '\0')
1196 * Empty URL (just prefix).
1197 * Make it totally empty so it's caught by the next if()
1199 url_param[0] = '\0';
1202 else if (0 == strncmp(url_param, "https://", 8))
1204 if (url_param[8] == '\0')
1207 * Empty URL (just prefix).
1208 * Make it totally empty so it's caught by the next if()
1210 url_param[0] = '\0';
1213 else if (url_param[0] != '\0')
1216 * Unknown prefix - assume http://
1218 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1219 if (NULL == url_param_prefixed)
1223 return JB_ERR_MEMORY;
1225 strcpy(url_param_prefixed, "http://");
1226 strcpy(url_param_prefixed + 7, url_param);
1228 url_param = url_param_prefixed;
1232 if (url_param[0] == '\0')
1234 /* URL paramater not specified, display query form only. */
1236 if (map_block_killer(exports, "url-given")
1237 || map(exports, "url", 1, "", 1))
1240 return JB_ERR_MEMORY;
1245 /* Given a URL, so query it. */
1250 struct file_list *fl;
1251 struct url_actions *b;
1252 struct http_request url_to_query[1];
1253 struct current_action_spec action[1];
1256 if (map(exports, "url", 1, html_encode(url_param), 0))
1260 return JB_ERR_MEMORY;
1263 init_current_action(action);
1265 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1267 free_current_action(action);
1270 return JB_ERR_MEMORY;
1273 err = parse_http_url(url_param, url_to_query, csp);
1277 if (err == JB_ERR_MEMORY)
1279 free_http_request(url_to_query);
1280 free_current_action(action);
1282 return JB_ERR_MEMORY;
1288 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1289 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1291 free_current_action(action);
1292 free_http_request(url_to_query);
1297 return JB_ERR_MEMORY;
1300 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1304 * We have a warning about SSL paths. Hide it for insecure sites.
1306 if (!url_to_query->ssl)
1308 if (map_block_killer(exports, "https"))
1310 free_current_action(action);
1312 free_http_request(url_to_query);
1313 return JB_ERR_MEMORY;
1317 matches = strdup("<table class=\"transparent\">");
1319 for (i = 0; i < MAX_AF_FILES; i++)
1321 if (NULL == csp->config->actions_file_short[i]
1322 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1326 if ((fl = csp->actions_list[i]) != NULL)
1328 if ((b = fl->f) != NULL)
1330 /* FIXME: Hardcoded HTML! */
1331 string_append(&matches, "<tr><th>In file: ");
1332 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1333 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1334 string_append(&matches, buf);
1335 string_append(&matches, "View</a>");
1336 #ifdef FEATURE_CGI_EDIT_ACTIONS
1337 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1338 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1339 string_append(&matches, "\">Edit</a>");
1341 string_append(&matches, "</th></tr>\n");
1348 for (; (b != NULL) && (matches != NULL); b = b->next)
1350 if (url_match(b->url, url_to_query))
1352 string_append(&matches, "<tr><td>{");
1353 string_join (&matches, actions_to_html(csp, b->action));
1354 string_append(&matches, " }</b><br>\n<code>");
1355 string_join (&matches, html_encode(b->url->spec));
1356 string_append(&matches, "</code></td></tr>\n");
1358 if (merge_current_action(action, b->action))
1361 free_http_request(url_to_query);
1362 free_current_action(action);
1364 return JB_ERR_MEMORY;
1372 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1375 string_append(&matches, "</table>\n");
1377 free_http_request(url_to_query);
1379 if (matches == NULL)
1381 free_current_action(action);
1383 return JB_ERR_MEMORY;
1386 if (map(exports, "matches", 1, matches , 0))
1388 free_current_action(action);
1390 return JB_ERR_MEMORY;
1393 s = current_action_to_html(csp, action);
1395 free_current_action(action);
1397 if (map(exports, "final", 1, s, 0))
1400 return JB_ERR_MEMORY;
1404 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1408 /*********************************************************************
1410 * Function : cgi_robots_txt
1412 * Description : CGI function to return "/robots.txt".
1415 * 1 : csp = Current client state (buffers, headers, etc...)
1416 * 2 : rsp = http_response data structure for output
1417 * 3 : parameters = map of cgi parameters
1419 * CGI Parameters : None
1421 * Returns : JB_ERR_OK on success
1422 * JB_ERR_MEMORY on out-of-memory error.
1424 *********************************************************************/
1425 jb_err cgi_robots_txt(struct client_state *csp,
1426 struct http_response *rsp,
1427 const struct map *parameters)
1433 "# This is the Privoxy control interface.\n"
1434 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1440 if (rsp->body == NULL)
1442 return JB_ERR_MEMORY;
1445 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1449 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1450 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1452 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1456 /*********************************************************************
1458 * Function : show_defines
1460 * Description : Add to a map the state od all conditional #defines
1461 * used when building
1464 * 1 : exports = map to extend
1466 * Returns : JB_ERR_OK on success
1467 * JB_ERR_MEMORY on out-of-memory error.
1469 *********************************************************************/
1470 static jb_err show_defines(struct map *exports)
1472 jb_err err = JB_ERR_OK;
1475 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1476 #else /* ifndef FEATURE_ACL */
1477 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1478 #endif /* ndef FEATURE_ACL */
1480 #ifdef FEATURE_CGI_EDIT_ACTIONS
1481 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1482 #else /* ifndef FEATURE_COOKIE_JAR */
1483 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1484 #endif /* ndef FEATURE_COOKIE_JAR */
1486 #ifdef FEATURE_COOKIE_JAR
1487 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1488 #else /* ifndef FEATURE_COOKIE_JAR */
1489 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1490 #endif /* ndef FEATURE_COOKIE_JAR */
1492 #ifdef FEATURE_FAST_REDIRECTS
1493 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1494 #else /* ifndef FEATURE_FAST_REDIRECTS */
1495 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1496 #endif /* ndef FEATURE_FAST_REDIRECTS */
1498 #ifdef FEATURE_FORCE_LOAD
1499 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1500 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1501 #else /* ifndef FEATURE_FORCE_LOAD */
1502 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1503 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1504 #endif /* ndef FEATURE_FORCE_LOAD */
1506 #ifdef FEATURE_GRACEFUL_TERMINATION
1507 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1508 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1509 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1510 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1512 #ifdef FEATURE_IMAGE_BLOCKING
1513 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1514 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1515 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1516 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1518 #ifdef FEATURE_IMAGE_DETECT_MSIE
1519 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1520 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1521 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1522 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1524 #ifdef FEATURE_KILL_POPUPS
1525 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1526 #else /* ifndef FEATURE_KILL_POPUPS */
1527 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1528 #endif /* ndef FEATURE_KILL_POPUPS */
1530 #ifdef FEATURE_NO_GIFS
1531 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1532 #else /* ifndef FEATURE_NO_GIFS */
1533 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1534 #endif /* ndef FEATURE_NO_GIFS */
1536 #ifdef FEATURE_PTHREAD
1537 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1538 #else /* ifndef FEATURE_PTHREAD */
1539 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1540 #endif /* ndef FEATURE_PTHREAD */
1542 #ifdef FEATURE_STATISTICS
1543 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1544 #else /* ifndef FEATURE_STATISTICS */
1545 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1546 #endif /* ndef FEATURE_STATISTICS */
1548 #ifdef FEATURE_TOGGLE
1549 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1550 #else /* ifndef FEATURE_TOGGLE */
1551 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1552 #endif /* ndef FEATURE_TOGGLE */
1554 #ifdef FEATURE_TRUST
1555 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1556 #else /* ifndef FEATURE_TRUST */
1557 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1558 #endif /* ndef FEATURE_TRUST */
1561 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1562 #else /* ifndef STATIC_PCRE */
1563 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1564 #endif /* ndef STATIC_PCRE */
1567 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1568 #else /* ifndef STATIC_PCRS */
1569 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1570 #endif /* ndef STATIC_PCRS */
1576 /*********************************************************************
1578 * Function : show_rcs
1580 * Description : Create a string with the rcs info for all sourcefiles
1584 * Returns : A string, or NULL on out-of-memory.
1586 *********************************************************************/
1587 static char *show_rcs(void)
1589 char *result = strdup("");
1590 char buf[BUFFER_SIZE];
1592 /* Instead of including *all* dot h's in the project (thus creating a
1593 * tremendous amount of dependencies), I will concede to declaring them
1594 * as extern's. This forces the developer to add to this list, but oh well.
1597 #define SHOW_RCS(__x) \
1599 extern const char __x[]; \
1600 sprintf(buf, "%s\n", __x); \
1601 string_append(&result, buf); \
1604 /* In alphabetical order */
1605 SHOW_RCS(actions_h_rcs)
1606 SHOW_RCS(actions_rcs)
1608 SHOW_RCS(amiga_h_rcs)
1610 #endif /* def AMIGA */
1613 #ifdef FEATURE_CGI_EDIT_ACTIONS
1614 SHOW_RCS(cgiedit_h_rcs)
1615 SHOW_RCS(cgiedit_rcs)
1616 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1617 SHOW_RCS(cgisimple_h_rcs)
1618 SHOW_RCS(cgisimple_rcs)
1620 SHOW_RCS(cygwin_h_rcs)
1622 SHOW_RCS(deanimate_h_rcs)
1623 SHOW_RCS(deanimate_rcs)
1624 SHOW_RCS(encode_h_rcs)
1625 SHOW_RCS(encode_rcs)
1626 SHOW_RCS(errlog_h_rcs)
1627 SHOW_RCS(errlog_rcs)
1628 SHOW_RCS(filters_h_rcs)
1629 SHOW_RCS(filters_rcs)
1630 SHOW_RCS(gateway_h_rcs)
1631 SHOW_RCS(gateway_rcs)
1632 SHOW_RCS(jbsockets_h_rcs)
1633 SHOW_RCS(jbsockets_rcs)
1636 #ifdef FEATURE_KILL_POPUPS
1637 SHOW_RCS(killpopup_h_rcs)
1638 SHOW_RCS(killpopup_rcs)
1639 #endif /* def FEATURE_KILL_POPUPS */
1640 SHOW_RCS(list_h_rcs)
1642 SHOW_RCS(loadcfg_h_rcs)
1643 SHOW_RCS(loadcfg_rcs)
1644 SHOW_RCS(loaders_h_rcs)
1645 SHOW_RCS(loaders_rcs)
1646 SHOW_RCS(miscutil_h_rcs)
1647 SHOW_RCS(miscutil_rcs)
1648 SHOW_RCS(parsers_h_rcs)
1649 SHOW_RCS(parsers_rcs)
1651 SHOW_RCS(pcrs_h_rcs)
1652 SHOW_RCS(project_h_rcs)
1653 SHOW_RCS(ssplit_h_rcs)
1654 SHOW_RCS(ssplit_rcs)
1655 SHOW_RCS(urlmatch_h_rcs)
1656 SHOW_RCS(urlmatch_rcs)
1658 #ifndef _WIN_CONSOLE
1659 SHOW_RCS(w32log_h_rcs)
1660 SHOW_RCS(w32log_rcs)
1661 SHOW_RCS(w32res_h_rcs)
1662 SHOW_RCS(w32taskbar_h_rcs)
1663 SHOW_RCS(w32taskbar_rcs)
1664 #endif /* ndef _WIN_CONSOLE */
1665 SHOW_RCS(win32_h_rcs)
1667 #endif /* def _WIN32 */