1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.42 2006/11/21 15:43:12 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.42 2006/11/21 15:43:12 fabiankeil
40 * Add special treatment for WIN32 to make sure
41 * cgi_send_user_manual opens the files in binary mode.
42 * Fixes BR 1600411 and unbreaks image delivery.
44 * Remove outdated comment.
46 * Revision 1.41 2006/10/09 19:18:28 roro
47 * Redirect http://p.p/user-manual (without trailing slash) to
48 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
50 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
51 * Modified cgi_send_user_manual to serve binary
52 * content without destroying it first. Should also be
53 * faster now. Added ".jpg" check for Content-Type guessing.
55 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
56 * Deliver documents in the user-manual directory
57 * with "Content-Type text/css" if their filename
60 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
61 * Incorporate modified version of Roland Rosenfeld's patch to
62 * optionally access the user-manual via Privoxy. Closes patch 679075.
64 * Formatting changed to Privoxy style, added call to
65 * cgi_error_no_template if the requested file doesn't
66 * exist and modified check whether or not Privoxy itself
67 * should serve the manual. Should work cross-platform now.
69 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
70 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
71 * with what was really the latest development (the v_3_0_branch branch)
73 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
74 * Multiple filter file support
76 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
77 * Undo some damaging memory leak patches
79 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
80 * A few memory leaks plugged (mostly on error paths)
82 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
83 * Another instance of:
84 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
85 * Thanks to Magnus Holmgren for the patch
87 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
88 * - Prevent line wrap beween "View/Edit" link buttons on status page
89 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
91 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
92 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
94 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
95 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
97 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
98 * Applying patch from Andreas as provided by Hal on the list.
99 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
101 * Revision 1.35 2002/05/12 21:44:44 jongfoster
102 * Adding amiga.[ch] revision information, if on an amiga.
104 * Revision 1.34 2002/04/30 12:06:12 oes
105 * Deleted unused code from default_cgi
107 * Revision 1.33 2002/04/30 11:14:52 oes
108 * Made csp the first parameter in *action_to_html
110 * Revision 1.32 2002/04/26 18:29:13 jongfoster
111 * Fixing this Visual C++ warning:
112 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
114 * Revision 1.31 2002/04/26 12:54:36 oes
115 * - Kill obsolete REDIRECT_URL code
116 * - Error handling fixes
117 * - Style sheet related HTML snipplet changes
118 * - cgi_show_url_info:
119 * - Matches now in table, actions on single lines,
121 * - standard.action suppressed
122 * - Buttons to View and Edit AFs
124 * Revision 1.30 2002/04/24 02:18:08 oes
125 * - show-status is now the starting point for editing
126 * the actions files, generate list of all AFs with buttons
127 * for viewing and editing, new look for file list (Jon:
128 * buttons now aligned ;-P ), view mode now supports multiple
129 * AFs, name changes, no view links for unspecified files,
130 * no edit link for standard.action.
132 * - Jon's multiple AF patch: cgi_show_url_info now uses all
133 * AFs and marks the output accordingly
135 * Revision 1.29 2002/04/10 13:38:35 oes
136 * load_template signature changed
138 * Revision 1.28 2002/04/07 15:42:12 jongfoster
139 * Fixing send-banner?type=auto when the image-blocker is
140 * a redirect to send-banner
142 * Revision 1.27 2002/04/05 15:50:48 oes
143 * added send-stylesheet CGI
145 * Revision 1.26 2002/04/04 00:36:36 gliptak
146 * always use pcre for matching
148 * Revision 1.25 2002/04/03 22:28:03 gliptak
149 * Removed references to gnu_regex
151 * Revision 1.24 2002/04/02 16:12:47 oes
152 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
154 * Revision 1.23 2002/03/26 22:29:54 swa
155 * we have a new homepage!
157 * Revision 1.22 2002/03/24 16:18:15 jongfoster
160 * Revision 1.21 2002/03/24 15:23:33 jongfoster
163 * Revision 1.20 2002/03/24 13:25:43 swa
164 * name change related issues
166 * Revision 1.19 2002/03/16 23:54:06 jongfoster
167 * Adding graceful termination feature, to help look for memory leaks.
168 * If you enable this (which, by design, has to be done by hand
169 * editing config.h) and then go to http://i.j.b/die, then the program
170 * will exit cleanly after the *next* request. It should free all the
171 * memory that was used.
173 * Revision 1.18 2002/03/12 01:44:49 oes
174 * Changed default for "blocked" image from jb logo to checkboard pattern
176 * Revision 1.17 2002/03/08 16:43:18 oes
177 * Added choice beween GIF and PNG built-in images
179 * Revision 1.16 2002/03/07 03:48:38 oes
180 * - Changed built-in images from GIF to PNG
181 * (with regard to Unisys patent issue)
182 * - Added a 4x4 pattern PNG which is less intrusive
183 * than the logo but also clearly marks the deleted banners
185 * Revision 1.15 2002/03/06 22:54:35 jongfoster
186 * Automated function-comment nitpicking.
188 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
189 * Clean up a little CRLF unpleasantness that suddenly appeared
191 * Revision 1.13 2002/02/21 00:10:37 jongfoster
192 * Adding send-banner?type=auto option
194 * Revision 1.12 2002/01/23 01:03:32 jongfoster
195 * Fixing gcc [CygWin] compiler warnings
197 * Revision 1.11 2002/01/23 00:01:04 jongfoster
198 * Adding cgi_transparent_gif() for http://i.j.b/t
199 * Adding missing html_encode() to many CGI functions.
200 * Adding urlmatch.[ch] to http://i.j.b/show-version
202 * Revision 1.10 2002/01/17 21:10:37 jongfoster
203 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
204 * Also fixing a problem in the same function with improperly quoted URLs
205 * in output HTML, and adding code to handle https:// URLs correctly.
207 * Revision 1.9 2001/11/30 23:09:15 jongfoster
208 * Now reports on FEATURE_CGI_EDIT_ACTIONS
209 * Removing FEATURE_DENY_GZIP from template
211 * Revision 1.8 2001/11/13 00:14:07 jongfoster
212 * Fixing stupid bug now I've figured out what || means.
213 * (It always returns 0 or 1, not one of it's paramaters.)
215 * Revision 1.7 2001/10/23 21:48:19 jongfoster
216 * Cleaning up error handling in CGI functions - they now send back
217 * a HTML error page and should never cause a FATAL error. (Fixes one
218 * potential source of "denial of service" attacks).
220 * CGI actions file editor that works and is actually useful.
222 * Ability to toggle JunkBuster remotely using a CGI call.
224 * You can turn off both the above features in the main configuration
225 * file, e.g. if you are running a multi-user proxy.
227 * Revision 1.6 2001/10/14 22:00:32 jongfoster
228 * Adding support for a 404 error when an invalid CGI page is requested.
230 * Revision 1.5 2001/10/07 15:30:41 oes
231 * Removed FEATURE_DENY_GZIP
233 * Revision 1.4 2001/10/02 15:31:12 oes
234 * Introduced show-request cgi
236 * Revision 1.3 2001/09/22 16:34:44 jongfoster
237 * Removing unneeded #includes
239 * Revision 1.2 2001/09/19 18:01:11 oes
240 * Fixed comments; cosmetics
242 * Revision 1.1 2001/09/16 17:08:54 jongfoster
243 * Moving simple CGI functions from cgi.c to new file cgisimple.c
246 **********************************************************************/
252 #include <sys/types.h>
259 #define snprintf _snprintf
260 #endif /* def _WIN32 */
264 #include "cgisimple.h"
270 #include "miscutil.h"
273 #include "urlmatch.h"
276 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
279 static char *show_rcs(void);
280 static jb_err show_defines(struct map *exports);
283 /*********************************************************************
285 * Function : cgi_default
287 * Description : CGI function that is called for the CGI_SITE_1_HOST
288 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
289 * Boring - only exports the default exports.
292 * 1 : csp = Current client state (buffers, headers, etc...)
293 * 2 : rsp = http_response data structure for output
294 * 3 : parameters = map of cgi parameters
296 * CGI Parameters : none
298 * Returns : JB_ERR_OK on success
299 * JB_ERR_MEMORY on out-of-memory
301 *********************************************************************/
302 jb_err cgi_default(struct client_state *csp,
303 struct http_response *rsp,
304 const struct map *parameters)
311 if (NULL == (exports = default_exports(csp, "")))
313 return JB_ERR_MEMORY;
316 return template_fill_for_cgi(csp, "default", exports, rsp);
320 /*********************************************************************
322 * Function : cgi_error_404
324 * Description : CGI function that is called if an unknown action was
328 * 1 : csp = Current client state (buffers, headers, etc...)
329 * 2 : rsp = http_response data structure for output
330 * 3 : parameters = map of cgi parameters
332 * CGI Parameters : none
334 * Returns : JB_ERR_OK on success
335 * JB_ERR_MEMORY on out-of-memory error.
337 *********************************************************************/
338 jb_err cgi_error_404(struct client_state *csp,
339 struct http_response *rsp,
340 const struct map *parameters)
348 if (NULL == (exports = default_exports(csp, NULL)))
350 return JB_ERR_MEMORY;
353 rsp->status = strdup("404 Privoxy configuration page not found");
354 if (rsp->status == NULL)
357 return JB_ERR_MEMORY;
360 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
364 #ifdef FEATURE_GRACEFUL_TERMINATION
365 /*********************************************************************
369 * Description : CGI function to shut down Privoxy.
370 * NOTE: Turning this on in a production build
371 * would be a BAD idea. An EXTREMELY BAD idea.
372 * In short, don't do it.
375 * 1 : csp = Current client state (buffers, headers, etc...)
376 * 2 : rsp = http_response data structure for output
377 * 3 : parameters = map of cgi parameters
379 * CGI Parameters : none
381 * Returns : JB_ERR_OK on success
382 * JB_ERR_MEMORY on out-of-memory error.
384 *********************************************************************/
385 jb_err cgi_die (struct client_state *csp,
386 struct http_response *rsp,
387 const struct map *parameters)
397 * I don't really care what gets sent back to the browser.
398 * Take the easy option - "out of memory" page.
401 return JB_ERR_MEMORY;
403 #endif /* def FEATURE_GRACEFUL_TERMINATION */
406 /*********************************************************************
408 * Function : cgi_show_request
410 * Description : Show the client's request and what sed() would have
414 * 1 : csp = Current client state (buffers, headers, etc...)
415 * 2 : rsp = http_response data structure for output
416 * 3 : parameters = map of cgi parameters
418 * CGI Parameters : none
420 * Returns : JB_ERR_OK on success
421 * JB_ERR_MEMORY on out-of-memory error.
423 *********************************************************************/
424 jb_err cgi_show_request(struct client_state *csp,
425 struct http_response *rsp,
426 const struct map *parameters)
435 if (NULL == (exports = default_exports(csp, "show-request")))
437 return JB_ERR_MEMORY;
441 * Repair the damage done to the IOB by get_header()
443 for (p = csp->iob->buf; p < csp->iob->eod; p++)
445 if (*p == '\0') *p = '\n';
449 * Export the original client's request and the one we would
450 * be sending to the server if this wasn't a CGI call
453 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
456 return JB_ERR_MEMORY;
459 if (map(exports, "processed-request", 1, html_encode_and_free_original(
460 sed(client_patterns, add_client_headers, csp)), 0))
463 return JB_ERR_MEMORY;
466 return template_fill_for_cgi(csp, "show-request", exports, rsp);
470 /*********************************************************************
472 * Function : cgi_send_banner
474 * Description : CGI function that returns a banner.
477 * 1 : csp = Current client state (buffers, headers, etc...)
478 * 2 : rsp = http_response data structure for output
479 * 3 : parameters = map of cgi parameters
482 * type : Selects the type of banner between "trans", "logo",
483 * and "auto". Defaults to "logo" if absent or invalid.
484 * "auto" means to select as if we were image-blocking.
485 * (Only the first character really counts; b and t are
488 * Returns : JB_ERR_OK on success
489 * JB_ERR_MEMORY on out-of-memory error.
491 *********************************************************************/
492 jb_err cgi_send_banner(struct client_state *csp,
493 struct http_response *rsp,
494 const struct map *parameters)
496 char imagetype = lookup(parameters, "type")[0];
499 * If type is auto, then determine the right thing
500 * to do from the set-image-blocker action
502 if (imagetype == 'a')
509 #ifdef FEATURE_IMAGE_BLOCKING
510 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
512 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
513 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
514 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
518 /* Use default - nothing to do here. */
520 else if (0 == strcmpic(p, "blank"))
524 else if (0 == strcmpic(p, "pattern"))
530 * If the action is to call this CGI, determine
533 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
535 imagetype = p[sizeof(prefix1) - 1];
537 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
539 imagetype = p[sizeof(prefix2) - 1];
543 * Everything else must (should) be a URL to
551 #endif /* def FEATURE_IMAGE_BLOCKING */
555 * Now imagetype is either the non-auto type we were called with,
556 * or it was auto and has since been determined. In any case, we
557 * can proceed to actually answering the request by sending a redirect
558 * or an image as appropriate:
560 if (imagetype == 'r')
562 rsp->status = strdup("302 Local Redirect from Privoxy");
563 if (rsp->status == NULL)
565 return JB_ERR_MEMORY;
567 if (enlist_unique_header(rsp->headers, "Location",
568 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
570 return JB_ERR_MEMORY;
575 if ((imagetype == 'b') || (imagetype == 't'))
577 rsp->body = bindup(image_blank_data, image_blank_length);
578 rsp->content_length = image_blank_length;
582 rsp->body = bindup(image_pattern_data, image_pattern_length);
583 rsp->content_length = image_pattern_length;
586 if (rsp->body == NULL)
588 return JB_ERR_MEMORY;
590 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
592 return JB_ERR_MEMORY;
603 /*********************************************************************
605 * Function : cgi_transparent_image
607 * Description : CGI function that sends a 1x1 transparent image.
610 * 1 : csp = Current client state (buffers, headers, etc...)
611 * 2 : rsp = http_response data structure for output
612 * 3 : parameters = map of cgi parameters
614 * CGI Parameters : None
616 * Returns : JB_ERR_OK on success
617 * JB_ERR_MEMORY on out-of-memory error.
619 *********************************************************************/
620 jb_err cgi_transparent_image(struct client_state *csp,
621 struct http_response *rsp,
622 const struct map *parameters)
624 rsp->body = bindup(image_blank_data, image_blank_length);
625 rsp->content_length = image_blank_length;
627 if (rsp->body == NULL)
629 return JB_ERR_MEMORY;
632 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
634 return JB_ERR_MEMORY;
644 /*********************************************************************
646 * Function : cgi_send_stylesheet
648 * Description : CGI function that sends a css stylesheet found
649 * in the cgi-style.css template
652 * 1 : csp = Current client state (buffers, headers, etc...)
653 * 2 : rsp = http_response data structure for output
654 * 3 : parameters = map of cgi parameters
656 * CGI Parameters : None
658 * Returns : JB_ERR_OK on success
659 * JB_ERR_MEMORY on out-of-memory error.
661 *********************************************************************/
662 jb_err cgi_send_stylesheet(struct client_state *csp,
663 struct http_response *rsp,
664 const struct map *parameters)
671 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
673 if (err == JB_ERR_FILE)
676 * No way to tell user; send empty stylesheet
678 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
682 return err; /* JB_ERR_MEMORY */
685 if (enlist(rsp->headers, "Content-Type: text/css"))
687 return JB_ERR_MEMORY;
693 /*********************************************************************
695 * Function : cgi_send_user_manual
697 * Description : CGI function that sends a file in the user
701 * 1 : csp = Current client state (buffers, headers, etc...)
702 * 2 : rsp = http_response data structure for output
703 * 3 : parameters = map of cgi parameters
705 * CGI Parameters : file=name.html, the name of the HTML file
706 * (relative to user-manual from config)
708 * Returns : JB_ERR_OK on success
709 * JB_ERR_MEMORY on out-of-memory error.
711 *********************************************************************/
712 jb_err cgi_send_user_manual(struct client_state *csp,
713 struct http_response *rsp,
714 const struct map *parameters)
716 const char * filename;
719 jb_err err = JB_ERR_OK;
726 if (!parameters->first)
728 /* requested http://p.p/user-manual (without trailing slash) */
729 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
732 get_string_param(parameters, "file", &filename);
733 /* Check paramter for hack attempts */
734 if (filename && strchr(filename, '/'))
736 return JB_ERR_CGI_PARAMS;
738 if (filename && strstr(filename, ".."))
740 return JB_ERR_CGI_PARAMS;
743 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
744 if (full_path == NULL)
746 return JB_ERR_MEMORY;
749 /* Open user-manual file */
752 * XXX: Do we support other operating systems that
753 * require special treatment to fopen in binary mode?
755 if (NULL == (fp = fopen(full_path, "rb")))
757 if (NULL == (fp = fopen(full_path, "r")))
758 #endif /* def WIN32 */
760 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
761 err = cgi_error_no_template(csp, rsp, full_path);
766 /* Get file length */
767 fseek(fp, 0, SEEK_END);
769 fseek(fp, 0, SEEK_SET);
771 /* Allocate memory and load the file directly into the body */
772 rsp->body = (char *)malloc(length+1);
777 return JB_ERR_MEMORY;
779 if (!fread(rsp->body, length, 1, fp))
782 * This happens if we didn't fopen in binary mode.
783 * If it does, we just log it and serve what we got,
784 * most likely padded with garbage.
786 log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
791 rsp->content_length = (int)length;
793 /* Guess correct Content-Type based on the filename's ending */
796 length = strlen(filename);
802 if((length>=4) && !strcmp(&filename[length-4], ".css"))
804 err = enlist(rsp->headers, "Content-Type: text/css");
806 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
808 err = enlist(rsp->headers, "Content-Type: image/jpeg");
812 err = enlist(rsp->headers, "Content-Type: text/html");
819 /*********************************************************************
821 * Function : cgi_show_version
823 * Description : CGI function that returns a a web page describing the
824 * file versions of Privoxy.
827 * 1 : csp = Current client state (buffers, headers, etc...)
828 * 2 : rsp = http_response data structure for output
829 * 3 : parameters = map of cgi parameters
831 * CGI Parameters : none
833 * Returns : JB_ERR_OK on success
834 * JB_ERR_MEMORY on out-of-memory error.
836 *********************************************************************/
837 jb_err cgi_show_version(struct client_state *csp,
838 struct http_response *rsp,
839 const struct map *parameters)
847 if (NULL == (exports = default_exports(csp, "show-version")))
849 return JB_ERR_MEMORY;
852 if (map(exports, "sourceversions", 1, show_rcs(), 0))
855 return JB_ERR_MEMORY;
858 return template_fill_for_cgi(csp, "show-version", exports, rsp);
862 /*********************************************************************
864 * Function : cgi_show_status
866 * Description : CGI function that returns a a web page describing the
867 * current status of Privoxy.
870 * 1 : csp = Current client state (buffers, headers, etc...)
871 * 2 : rsp = http_response data structure for output
872 * 3 : parameters = map of cgi parameters
875 * file : Which file to show. Only first letter is checked,
877 * - "p"ermissions (actions) file
880 * Default is to show menu and other information.
882 * Returns : JB_ERR_OK on success
883 * JB_ERR_MEMORY on out-of-memory error.
885 *********************************************************************/
886 jb_err cgi_show_status(struct client_state *csp,
887 struct http_response *rsp,
888 const struct map *parameters)
895 char buf[BUFFER_SIZE];
896 const char * filename = NULL;
897 char * file_description = NULL;
898 #ifdef FEATURE_STATISTICS
899 float perc_rej; /* Percentage of http requests rejected */
901 int local_urls_rejected;
902 #endif /* ndef FEATURE_STATISTICS */
903 struct file_list * fl;
904 struct url_actions * b;
905 jb_err err = JB_ERR_OK;
913 if (NULL == (exports = default_exports(csp, "show-status")))
915 return JB_ERR_MEMORY;
918 switch (*(lookup(parameters, "file")))
921 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
923 filename = csp->actions_list[i]->filename;
924 file_description = "Actions File";
929 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
931 filename = csp->rlist[i]->filename;
932 file_description = "Filter File";
940 filename = csp->tlist->filename;
941 file_description = "Trust File";
944 #endif /* def FEATURE_TRUST */
947 if (NULL != filename)
949 if ( map(exports, "file-description", 1, file_description, 1)
950 || map(exports, "filepath", 1, html_encode(filename), 0) )
953 return JB_ERR_MEMORY;
956 if ((fp = fopen(filename, "r")) == NULL)
958 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
961 return JB_ERR_MEMORY;
967 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
969 string_join (&s, html_encode(buf));
973 if (map(exports, "contents", 1, s, 0))
976 return JB_ERR_MEMORY;
980 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
984 for (j = 0; (s != NULL) && (j < Argc); j++)
986 if (!err) err = string_join (&s, html_encode(Argv[j]));
987 if (!err) err = string_append(&s, " ");
989 if (!err) err = map(exports, "invocation", 1, s, 0);
991 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
992 if (!err) err = show_defines(exports);
997 return JB_ERR_MEMORY;
1000 #ifdef FEATURE_STATISTICS
1001 local_urls_read = urls_read;
1002 local_urls_rejected = urls_rejected;
1005 * Need to alter the stats not to include the fetch of this
1008 * Can't do following thread safely! doh!
1011 * urls_rejected--; * This will be incremented subsequently *
1014 if (local_urls_read == 0)
1016 if (!err) err = map_block_killer(exports, "have-stats");
1020 if (!err) err = map_block_killer(exports, "have-no-stats");
1022 perc_rej = (float)local_urls_rejected * 100.0F /
1023 (float)local_urls_read;
1025 sprintf(buf, "%d", local_urls_read);
1026 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1028 sprintf(buf, "%d", local_urls_rejected);
1029 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1031 sprintf(buf, "%6.2f", perc_rej);
1032 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1035 #else /* ndef FEATURE_STATISTICS */
1036 err = err || map_block_killer(exports, "statistics");
1037 #endif /* ndef FEATURE_STATISTICS */
1040 * List all action files in use, together with view and edit links,
1041 * except for standard.action, which should only be viewable. (Not
1042 * enforced in the editor itself)
1043 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1046 for (i = 0; i < MAX_AF_FILES; i++)
1048 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
1050 if (!err) err = string_append(&s, "<tr><td>");
1051 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1052 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1053 if (!err) err = string_append(&s, buf);
1055 #ifdef FEATURE_CGI_EDIT_ACTIONS
1056 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1058 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1059 if (!err) err = string_append(&s, buf);
1063 if (!err) err = string_append(&s, "</td></tr>\n");
1068 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1072 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1076 * List all re_filterfiles in use, together with view options.
1077 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1080 for (i = 0; i < MAX_AF_FILES; i++)
1082 if (((fl = csp->rlist[i]) != NULL) && ((b = fl->f) != NULL))
1084 if (!err) err = string_append(&s, "<tr><td>");
1085 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1086 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1087 if (!err) err = string_append(&s, buf);
1088 if (!err) err = string_append(&s, "</td></tr>\n");
1093 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1097 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1098 if (!err) err = map_block_killer(exports, "have-filterfile");
1101 #ifdef FEATURE_TRUST
1104 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1108 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1109 if (!err) err = map_block_killer(exports, "have-trustfile");
1112 if (!err) err = map_block_killer(exports, "trust-support");
1113 #endif /* ndef FEATURE_TRUST */
1118 return JB_ERR_MEMORY;
1121 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1125 /*********************************************************************
1127 * Function : cgi_show_url_info
1129 * Description : CGI function that determines and shows which actions
1130 * Privoxy will perform for a given url, and which
1131 * matches starting from the defaults have lead to that.
1134 * 1 : csp = Current client state (buffers, headers, etc...)
1135 * 2 : rsp = http_response data structure for output
1136 * 3 : parameters = map of cgi parameters
1139 * url : The url whose actions are to be determined.
1140 * If url is unset, the url-given conditional will be
1141 * set, so that all but the form can be suppressed in
1144 * Returns : JB_ERR_OK on success
1145 * JB_ERR_MEMORY on out-of-memory error.
1147 *********************************************************************/
1148 jb_err cgi_show_url_info(struct client_state *csp,
1149 struct http_response *rsp,
1150 const struct map *parameters)
1153 struct map *exports;
1160 if (NULL == (exports = default_exports(csp, "show-url-info")))
1162 return JB_ERR_MEMORY;
1166 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1168 url_param = strdup(lookup(parameters, "url"));
1169 if (url_param == NULL)
1172 return JB_ERR_MEMORY;
1177 * Handle prefixes. 4 possibilities:
1178 * 1) "http://" or "https://" prefix present and followed by URL - OK
1179 * 2) Only the "http://" or "https://" part is present, no URL - change
1180 * to empty string so it will be detected later as "no URL".
1181 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1183 * 4) Parameter not specified or is empty string - let this fall through
1184 * for now, next block of code will handle it.
1186 if (0 == strncmp(url_param, "http://", 7))
1188 if (url_param[7] == '\0')
1191 * Empty URL (just prefix).
1192 * Make it totally empty so it's caught by the next if()
1194 url_param[0] = '\0';
1197 else if (0 == strncmp(url_param, "https://", 8))
1199 if (url_param[8] == '\0')
1202 * Empty URL (just prefix).
1203 * Make it totally empty so it's caught by the next if()
1205 url_param[0] = '\0';
1208 else if (url_param[0] != '\0')
1211 * Unknown prefix - assume http://
1213 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1214 if (NULL == url_param_prefixed)
1218 return JB_ERR_MEMORY;
1220 strcpy(url_param_prefixed, "http://");
1221 strcpy(url_param_prefixed + 7, url_param);
1223 url_param = url_param_prefixed;
1227 if (url_param[0] == '\0')
1229 /* URL paramater not specified, display query form only. */
1231 if (map_block_killer(exports, "url-given")
1232 || map(exports, "url", 1, "", 1))
1235 return JB_ERR_MEMORY;
1240 /* Given a URL, so query it. */
1245 struct file_list *fl;
1246 struct url_actions *b;
1247 struct http_request url_to_query[1];
1248 struct current_action_spec action[1];
1251 if (map(exports, "url", 1, html_encode(url_param), 0))
1255 return JB_ERR_MEMORY;
1258 init_current_action(action);
1260 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1262 free_current_action(action);
1265 return JB_ERR_MEMORY;
1268 err = parse_http_url(url_param, url_to_query, csp);
1272 if (err == JB_ERR_MEMORY)
1274 free_http_request(url_to_query);
1275 free_current_action(action);
1277 return JB_ERR_MEMORY;
1283 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1284 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1286 free_current_action(action);
1287 free_http_request(url_to_query);
1292 return JB_ERR_MEMORY;
1295 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1299 * We have a warning about SSL paths. Hide it for insecure sites.
1301 if (!url_to_query->ssl)
1303 if (map_block_killer(exports, "https"))
1305 free_current_action(action);
1307 free_http_request(url_to_query);
1308 return JB_ERR_MEMORY;
1312 matches = strdup("<table class=\"transparent\">");
1314 for (i = 0; i < MAX_AF_FILES; i++)
1316 if (NULL == csp->config->actions_file_short[i]
1317 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1321 if ((fl = csp->actions_list[i]) != NULL)
1323 if ((b = fl->f) != NULL)
1325 /* FIXME: Hardcoded HTML! */
1326 string_append(&matches, "<tr><th>In file: ");
1327 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1328 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1329 string_append(&matches, buf);
1330 string_append(&matches, "View</a>");
1331 #ifdef FEATURE_CGI_EDIT_ACTIONS
1332 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1333 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1334 string_append(&matches, "\">Edit</a>");
1336 string_append(&matches, "</th></tr>\n");
1343 for (; (b != NULL) && (matches != NULL); b = b->next)
1345 if (url_match(b->url, url_to_query))
1347 string_append(&matches, "<tr><td>{");
1348 string_join (&matches, actions_to_html(csp, b->action));
1349 string_append(&matches, " }</b><br>\n<code>");
1350 string_join (&matches, html_encode(b->url->spec));
1351 string_append(&matches, "</code></td></tr>\n");
1353 if (merge_current_action(action, b->action))
1356 free_http_request(url_to_query);
1357 free_current_action(action);
1359 return JB_ERR_MEMORY;
1367 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1370 string_append(&matches, "</table>\n");
1372 free_http_request(url_to_query);
1374 if (matches == NULL)
1376 free_current_action(action);
1378 return JB_ERR_MEMORY;
1381 if (map(exports, "matches", 1, matches , 0))
1383 free_current_action(action);
1385 return JB_ERR_MEMORY;
1388 s = current_action_to_html(csp, action);
1390 free_current_action(action);
1392 if (map(exports, "final", 1, s, 0))
1395 return JB_ERR_MEMORY;
1399 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1403 /*********************************************************************
1405 * Function : cgi_robots_txt
1407 * Description : CGI function to return "/robots.txt".
1410 * 1 : csp = Current client state (buffers, headers, etc...)
1411 * 2 : rsp = http_response data structure for output
1412 * 3 : parameters = map of cgi parameters
1414 * CGI Parameters : None
1416 * Returns : JB_ERR_OK on success
1417 * JB_ERR_MEMORY on out-of-memory error.
1419 *********************************************************************/
1420 jb_err cgi_robots_txt(struct client_state *csp,
1421 struct http_response *rsp,
1422 const struct map *parameters)
1428 "# This is the Privoxy control interface.\n"
1429 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1435 if (rsp->body == NULL)
1437 return JB_ERR_MEMORY;
1440 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1444 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1445 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1447 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1451 /*********************************************************************
1453 * Function : show_defines
1455 * Description : Add to a map the state od all conditional #defines
1456 * used when building
1459 * 1 : exports = map to extend
1461 * Returns : JB_ERR_OK on success
1462 * JB_ERR_MEMORY on out-of-memory error.
1464 *********************************************************************/
1465 static jb_err show_defines(struct map *exports)
1467 jb_err err = JB_ERR_OK;
1470 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1471 #else /* ifndef FEATURE_ACL */
1472 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1473 #endif /* ndef FEATURE_ACL */
1475 #ifdef FEATURE_CGI_EDIT_ACTIONS
1476 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1477 #else /* ifndef FEATURE_COOKIE_JAR */
1478 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1479 #endif /* ndef FEATURE_COOKIE_JAR */
1481 #ifdef FEATURE_COOKIE_JAR
1482 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1483 #else /* ifndef FEATURE_COOKIE_JAR */
1484 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1485 #endif /* ndef FEATURE_COOKIE_JAR */
1487 #ifdef FEATURE_FAST_REDIRECTS
1488 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1489 #else /* ifndef FEATURE_FAST_REDIRECTS */
1490 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1491 #endif /* ndef FEATURE_FAST_REDIRECTS */
1493 #ifdef FEATURE_FORCE_LOAD
1494 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1495 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1496 #else /* ifndef FEATURE_FORCE_LOAD */
1497 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1498 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1499 #endif /* ndef FEATURE_FORCE_LOAD */
1501 #ifdef FEATURE_GRACEFUL_TERMINATION
1502 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1503 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1504 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1505 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1507 #ifdef FEATURE_IMAGE_BLOCKING
1508 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1509 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1510 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1511 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1513 #ifdef FEATURE_IMAGE_DETECT_MSIE
1514 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1515 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1516 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1517 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1519 #ifdef FEATURE_KILL_POPUPS
1520 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1521 #else /* ifndef FEATURE_KILL_POPUPS */
1522 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1523 #endif /* ndef FEATURE_KILL_POPUPS */
1525 #ifdef FEATURE_NO_GIFS
1526 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1527 #else /* ifndef FEATURE_NO_GIFS */
1528 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1529 #endif /* ndef FEATURE_NO_GIFS */
1531 #ifdef FEATURE_PTHREAD
1532 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1533 #else /* ifndef FEATURE_PTHREAD */
1534 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1535 #endif /* ndef FEATURE_PTHREAD */
1537 #ifdef FEATURE_STATISTICS
1538 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1539 #else /* ifndef FEATURE_STATISTICS */
1540 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1541 #endif /* ndef FEATURE_STATISTICS */
1543 #ifdef FEATURE_TOGGLE
1544 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1545 #else /* ifndef FEATURE_TOGGLE */
1546 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1547 #endif /* ndef FEATURE_TOGGLE */
1549 #ifdef FEATURE_TRUST
1550 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1551 #else /* ifndef FEATURE_TRUST */
1552 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1553 #endif /* ndef FEATURE_TRUST */
1556 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1557 #else /* ifndef STATIC_PCRE */
1558 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1559 #endif /* ndef STATIC_PCRE */
1562 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1563 #else /* ifndef STATIC_PCRS */
1564 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1565 #endif /* ndef STATIC_PCRS */
1571 /*********************************************************************
1573 * Function : show_rcs
1575 * Description : Create a string with the rcs info for all sourcefiles
1579 * Returns : A string, or NULL on out-of-memory.
1581 *********************************************************************/
1582 static char *show_rcs(void)
1584 char *result = strdup("");
1585 char buf[BUFFER_SIZE];
1587 /* Instead of including *all* dot h's in the project (thus creating a
1588 * tremendous amount of dependencies), I will concede to declaring them
1589 * as extern's. This forces the developer to add to this list, but oh well.
1592 #define SHOW_RCS(__x) \
1594 extern const char __x[]; \
1595 sprintf(buf, "%s\n", __x); \
1596 string_append(&result, buf); \
1599 /* In alphabetical order */
1600 SHOW_RCS(actions_h_rcs)
1601 SHOW_RCS(actions_rcs)
1603 SHOW_RCS(amiga_h_rcs)
1605 #endif /* def AMIGA */
1608 #ifdef FEATURE_CGI_EDIT_ACTIONS
1609 SHOW_RCS(cgiedit_h_rcs)
1610 SHOW_RCS(cgiedit_rcs)
1611 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1612 SHOW_RCS(cgisimple_h_rcs)
1613 SHOW_RCS(cgisimple_rcs)
1615 SHOW_RCS(cygwin_h_rcs)
1617 SHOW_RCS(deanimate_h_rcs)
1618 SHOW_RCS(deanimate_rcs)
1619 SHOW_RCS(encode_h_rcs)
1620 SHOW_RCS(encode_rcs)
1621 SHOW_RCS(errlog_h_rcs)
1622 SHOW_RCS(errlog_rcs)
1623 SHOW_RCS(filters_h_rcs)
1624 SHOW_RCS(filters_rcs)
1625 SHOW_RCS(gateway_h_rcs)
1626 SHOW_RCS(gateway_rcs)
1627 SHOW_RCS(jbsockets_h_rcs)
1628 SHOW_RCS(jbsockets_rcs)
1631 #ifdef FEATURE_KILL_POPUPS
1632 SHOW_RCS(killpopup_h_rcs)
1633 SHOW_RCS(killpopup_rcs)
1634 #endif /* def FEATURE_KILL_POPUPS */
1635 SHOW_RCS(list_h_rcs)
1637 SHOW_RCS(loadcfg_h_rcs)
1638 SHOW_RCS(loadcfg_rcs)
1639 SHOW_RCS(loaders_h_rcs)
1640 SHOW_RCS(loaders_rcs)
1641 SHOW_RCS(miscutil_h_rcs)
1642 SHOW_RCS(miscutil_rcs)
1643 SHOW_RCS(parsers_h_rcs)
1644 SHOW_RCS(parsers_rcs)
1646 SHOW_RCS(pcrs_h_rcs)
1647 SHOW_RCS(project_h_rcs)
1648 SHOW_RCS(ssplit_h_rcs)
1649 SHOW_RCS(ssplit_rcs)
1650 SHOW_RCS(urlmatch_h_rcs)
1651 SHOW_RCS(urlmatch_rcs)
1653 #ifndef _WIN_CONSOLE
1654 SHOW_RCS(w32log_h_rcs)
1655 SHOW_RCS(w32log_rcs)
1656 SHOW_RCS(w32res_h_rcs)
1657 SHOW_RCS(w32taskbar_h_rcs)
1658 SHOW_RCS(w32taskbar_rcs)
1659 #endif /* ndef _WIN_CONSOLE */
1660 SHOW_RCS(win32_h_rcs)
1662 #endif /* def _WIN32 */