1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.41 2006/10/09 19:18:28 roro 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.41 2006/10/09 19:18:28 roro
40 * Redirect http://p.p/user-manual (without trailing slash) to
41 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
43 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
44 * Modified cgi_send_user_manual to serve binary
45 * content without destroying it first. Should also be
46 * faster now. Added ".jpg" check for Content-Type guessing.
48 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
49 * Deliver documents in the user-manual directory
50 * with "Content-Type text/css" if their filename
53 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
54 * Incorporate modified version of Roland Rosenfeld's patch to
55 * optionally access the user-manual via Privoxy. Closes patch 679075.
57 * Formatting changed to Privoxy style, added call to
58 * cgi_error_no_template if the requested file doesn't
59 * exist and modified check whether or not Privoxy itself
60 * should serve the manual. Should work cross-platform now.
62 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
63 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
64 * with what was really the latest development (the v_3_0_branch branch)
66 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
67 * Multiple filter file support
69 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
70 * Undo some damaging memory leak patches
72 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
73 * A few memory leaks plugged (mostly on error paths)
75 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
76 * Another instance of:
77 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
78 * Thanks to Magnus Holmgren for the patch
80 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
81 * - Prevent line wrap beween "View/Edit" link buttons on status page
82 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
84 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
85 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
87 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
88 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
90 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
91 * Applying patch from Andreas as provided by Hal on the list.
92 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
94 * Revision 1.35 2002/05/12 21:44:44 jongfoster
95 * Adding amiga.[ch] revision information, if on an amiga.
97 * Revision 1.34 2002/04/30 12:06:12 oes
98 * Deleted unused code from default_cgi
100 * Revision 1.33 2002/04/30 11:14:52 oes
101 * Made csp the first parameter in *action_to_html
103 * Revision 1.32 2002/04/26 18:29:13 jongfoster
104 * Fixing this Visual C++ warning:
105 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
107 * Revision 1.31 2002/04/26 12:54:36 oes
108 * - Kill obsolete REDIRECT_URL code
109 * - Error handling fixes
110 * - Style sheet related HTML snipplet changes
111 * - cgi_show_url_info:
112 * - Matches now in table, actions on single lines,
114 * - standard.action suppressed
115 * - Buttons to View and Edit AFs
117 * Revision 1.30 2002/04/24 02:18:08 oes
118 * - show-status is now the starting point for editing
119 * the actions files, generate list of all AFs with buttons
120 * for viewing and editing, new look for file list (Jon:
121 * buttons now aligned ;-P ), view mode now supports multiple
122 * AFs, name changes, no view links for unspecified files,
123 * no edit link for standard.action.
125 * - Jon's multiple AF patch: cgi_show_url_info now uses all
126 * AFs and marks the output accordingly
128 * Revision 1.29 2002/04/10 13:38:35 oes
129 * load_template signature changed
131 * Revision 1.28 2002/04/07 15:42:12 jongfoster
132 * Fixing send-banner?type=auto when the image-blocker is
133 * a redirect to send-banner
135 * Revision 1.27 2002/04/05 15:50:48 oes
136 * added send-stylesheet CGI
138 * Revision 1.26 2002/04/04 00:36:36 gliptak
139 * always use pcre for matching
141 * Revision 1.25 2002/04/03 22:28:03 gliptak
142 * Removed references to gnu_regex
144 * Revision 1.24 2002/04/02 16:12:47 oes
145 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
147 * Revision 1.23 2002/03/26 22:29:54 swa
148 * we have a new homepage!
150 * Revision 1.22 2002/03/24 16:18:15 jongfoster
153 * Revision 1.21 2002/03/24 15:23:33 jongfoster
156 * Revision 1.20 2002/03/24 13:25:43 swa
157 * name change related issues
159 * Revision 1.19 2002/03/16 23:54:06 jongfoster
160 * Adding graceful termination feature, to help look for memory leaks.
161 * If you enable this (which, by design, has to be done by hand
162 * editing config.h) and then go to http://i.j.b/die, then the program
163 * will exit cleanly after the *next* request. It should free all the
164 * memory that was used.
166 * Revision 1.18 2002/03/12 01:44:49 oes
167 * Changed default for "blocked" image from jb logo to checkboard pattern
169 * Revision 1.17 2002/03/08 16:43:18 oes
170 * Added choice beween GIF and PNG built-in images
172 * Revision 1.16 2002/03/07 03:48:38 oes
173 * - Changed built-in images from GIF to PNG
174 * (with regard to Unisys patent issue)
175 * - Added a 4x4 pattern PNG which is less intrusive
176 * than the logo but also clearly marks the deleted banners
178 * Revision 1.15 2002/03/06 22:54:35 jongfoster
179 * Automated function-comment nitpicking.
181 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
182 * Clean up a little CRLF unpleasantness that suddenly appeared
184 * Revision 1.13 2002/02/21 00:10:37 jongfoster
185 * Adding send-banner?type=auto option
187 * Revision 1.12 2002/01/23 01:03:32 jongfoster
188 * Fixing gcc [CygWin] compiler warnings
190 * Revision 1.11 2002/01/23 00:01:04 jongfoster
191 * Adding cgi_transparent_gif() for http://i.j.b/t
192 * Adding missing html_encode() to many CGI functions.
193 * Adding urlmatch.[ch] to http://i.j.b/show-version
195 * Revision 1.10 2002/01/17 21:10:37 jongfoster
196 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
197 * Also fixing a problem in the same function with improperly quoted URLs
198 * in output HTML, and adding code to handle https:// URLs correctly.
200 * Revision 1.9 2001/11/30 23:09:15 jongfoster
201 * Now reports on FEATURE_CGI_EDIT_ACTIONS
202 * Removing FEATURE_DENY_GZIP from template
204 * Revision 1.8 2001/11/13 00:14:07 jongfoster
205 * Fixing stupid bug now I've figured out what || means.
206 * (It always returns 0 or 1, not one of it's paramaters.)
208 * Revision 1.7 2001/10/23 21:48:19 jongfoster
209 * Cleaning up error handling in CGI functions - they now send back
210 * a HTML error page and should never cause a FATAL error. (Fixes one
211 * potential source of "denial of service" attacks).
213 * CGI actions file editor that works and is actually useful.
215 * Ability to toggle JunkBuster remotely using a CGI call.
217 * You can turn off both the above features in the main configuration
218 * file, e.g. if you are running a multi-user proxy.
220 * Revision 1.6 2001/10/14 22:00:32 jongfoster
221 * Adding support for a 404 error when an invalid CGI page is requested.
223 * Revision 1.5 2001/10/07 15:30:41 oes
224 * Removed FEATURE_DENY_GZIP
226 * Revision 1.4 2001/10/02 15:31:12 oes
227 * Introduced show-request cgi
229 * Revision 1.3 2001/09/22 16:34:44 jongfoster
230 * Removing unneeded #includes
232 * Revision 1.2 2001/09/19 18:01:11 oes
233 * Fixed comments; cosmetics
235 * Revision 1.1 2001/09/16 17:08:54 jongfoster
236 * Moving simple CGI functions from cgi.c to new file cgisimple.c
239 **********************************************************************/
245 #include <sys/types.h>
252 #define snprintf _snprintf
253 #endif /* def _WIN32 */
257 #include "cgisimple.h"
263 #include "miscutil.h"
266 #include "urlmatch.h"
269 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
272 static char *show_rcs(void);
273 static jb_err show_defines(struct map *exports);
276 /*********************************************************************
278 * Function : cgi_default
280 * Description : CGI function that is called for the CGI_SITE_1_HOST
281 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
282 * Boring - only exports the default exports.
285 * 1 : csp = Current client state (buffers, headers, etc...)
286 * 2 : rsp = http_response data structure for output
287 * 3 : parameters = map of cgi parameters
289 * CGI Parameters : none
291 * Returns : JB_ERR_OK on success
292 * JB_ERR_MEMORY on out-of-memory
294 *********************************************************************/
295 jb_err cgi_default(struct client_state *csp,
296 struct http_response *rsp,
297 const struct map *parameters)
304 if (NULL == (exports = default_exports(csp, "")))
306 return JB_ERR_MEMORY;
309 return template_fill_for_cgi(csp, "default", exports, rsp);
313 /*********************************************************************
315 * Function : cgi_error_404
317 * Description : CGI function that is called if an unknown action was
321 * 1 : csp = Current client state (buffers, headers, etc...)
322 * 2 : rsp = http_response data structure for output
323 * 3 : parameters = map of cgi parameters
325 * CGI Parameters : none
327 * Returns : JB_ERR_OK on success
328 * JB_ERR_MEMORY on out-of-memory error.
330 *********************************************************************/
331 jb_err cgi_error_404(struct client_state *csp,
332 struct http_response *rsp,
333 const struct map *parameters)
341 if (NULL == (exports = default_exports(csp, NULL)))
343 return JB_ERR_MEMORY;
346 rsp->status = strdup("404 Privoxy configuration page not found");
347 if (rsp->status == NULL)
350 return JB_ERR_MEMORY;
353 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
357 #ifdef FEATURE_GRACEFUL_TERMINATION
358 /*********************************************************************
362 * Description : CGI function to shut down Privoxy.
363 * NOTE: Turning this on in a production build
364 * would be a BAD idea. An EXTREMELY BAD idea.
365 * In short, don't do it.
368 * 1 : csp = Current client state (buffers, headers, etc...)
369 * 2 : rsp = http_response data structure for output
370 * 3 : parameters = map of cgi parameters
372 * CGI Parameters : none
374 * Returns : JB_ERR_OK on success
375 * JB_ERR_MEMORY on out-of-memory error.
377 *********************************************************************/
378 jb_err cgi_die (struct client_state *csp,
379 struct http_response *rsp,
380 const struct map *parameters)
390 * I don't really care what gets sent back to the browser.
391 * Take the easy option - "out of memory" page.
394 return JB_ERR_MEMORY;
396 #endif /* def FEATURE_GRACEFUL_TERMINATION */
399 /*********************************************************************
401 * Function : cgi_show_request
403 * Description : Show the client's request and what sed() would have
407 * 1 : csp = Current client state (buffers, headers, etc...)
408 * 2 : rsp = http_response data structure for output
409 * 3 : parameters = map of cgi parameters
411 * CGI Parameters : none
413 * Returns : JB_ERR_OK on success
414 * JB_ERR_MEMORY on out-of-memory error.
416 *********************************************************************/
417 jb_err cgi_show_request(struct client_state *csp,
418 struct http_response *rsp,
419 const struct map *parameters)
428 if (NULL == (exports = default_exports(csp, "show-request")))
430 return JB_ERR_MEMORY;
434 * Repair the damage done to the IOB by get_header()
436 for (p = csp->iob->buf; p < csp->iob->eod; p++)
438 if (*p == '\0') *p = '\n';
442 * Export the original client's request and the one we would
443 * be sending to the server if this wasn't a CGI call
446 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
449 return JB_ERR_MEMORY;
452 if (map(exports, "processed-request", 1, html_encode_and_free_original(
453 sed(client_patterns, add_client_headers, csp)), 0))
456 return JB_ERR_MEMORY;
459 return template_fill_for_cgi(csp, "show-request", exports, rsp);
463 /*********************************************************************
465 * Function : cgi_send_banner
467 * Description : CGI function that returns a banner.
470 * 1 : csp = Current client state (buffers, headers, etc...)
471 * 2 : rsp = http_response data structure for output
472 * 3 : parameters = map of cgi parameters
475 * type : Selects the type of banner between "trans", "logo",
476 * and "auto". Defaults to "logo" if absent or invalid.
477 * "auto" means to select as if we were image-blocking.
478 * (Only the first character really counts; b and t are
481 * Returns : JB_ERR_OK on success
482 * JB_ERR_MEMORY on out-of-memory error.
484 *********************************************************************/
485 jb_err cgi_send_banner(struct client_state *csp,
486 struct http_response *rsp,
487 const struct map *parameters)
489 char imagetype = lookup(parameters, "type")[0];
492 * If type is auto, then determine the right thing
493 * to do from the set-image-blocker action
495 if (imagetype == 'a')
502 #ifdef FEATURE_IMAGE_BLOCKING
503 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
505 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
506 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
507 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
511 /* Use default - nothing to do here. */
513 else if (0 == strcmpic(p, "blank"))
517 else if (0 == strcmpic(p, "pattern"))
523 * If the action is to call this CGI, determine
526 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
528 imagetype = p[sizeof(prefix1) - 1];
530 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
532 imagetype = p[sizeof(prefix2) - 1];
536 * Everything else must (should) be a URL to
544 #endif /* def FEATURE_IMAGE_BLOCKING */
548 * Now imagetype is either the non-auto type we were called with,
549 * or it was auto and has since been determined. In any case, we
550 * can proceed to actually answering the request by sending a redirect
551 * or an image as appropriate:
553 if (imagetype == 'r')
555 rsp->status = strdup("302 Local Redirect from Privoxy");
556 if (rsp->status == NULL)
558 return JB_ERR_MEMORY;
560 if (enlist_unique_header(rsp->headers, "Location",
561 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
563 return JB_ERR_MEMORY;
568 if ((imagetype == 'b') || (imagetype == 't'))
570 rsp->body = bindup(image_blank_data, image_blank_length);
571 rsp->content_length = image_blank_length;
575 rsp->body = bindup(image_pattern_data, image_pattern_length);
576 rsp->content_length = image_pattern_length;
579 if (rsp->body == NULL)
581 return JB_ERR_MEMORY;
583 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
585 return JB_ERR_MEMORY;
596 /*********************************************************************
598 * Function : cgi_transparent_image
600 * Description : CGI function that sends a 1x1 transparent image.
603 * 1 : csp = Current client state (buffers, headers, etc...)
604 * 2 : rsp = http_response data structure for output
605 * 3 : parameters = map of cgi parameters
607 * CGI Parameters : None
609 * Returns : JB_ERR_OK on success
610 * JB_ERR_MEMORY on out-of-memory error.
612 *********************************************************************/
613 jb_err cgi_transparent_image(struct client_state *csp,
614 struct http_response *rsp,
615 const struct map *parameters)
617 rsp->body = bindup(image_blank_data, image_blank_length);
618 rsp->content_length = image_blank_length;
620 if (rsp->body == NULL)
622 return JB_ERR_MEMORY;
625 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
627 return JB_ERR_MEMORY;
637 /*********************************************************************
639 * Function : cgi_send_stylesheet
641 * Description : CGI function that sends a css stylesheet found
642 * in the cgi-style.css template
645 * 1 : csp = Current client state (buffers, headers, etc...)
646 * 2 : rsp = http_response data structure for output
647 * 3 : parameters = map of cgi parameters
649 * CGI Parameters : None
651 * Returns : JB_ERR_OK on success
652 * JB_ERR_MEMORY on out-of-memory error.
654 *********************************************************************/
655 jb_err cgi_send_stylesheet(struct client_state *csp,
656 struct http_response *rsp,
657 const struct map *parameters)
664 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
666 if (err == JB_ERR_FILE)
669 * No way to tell user; send empty stylesheet
671 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
675 return err; /* JB_ERR_MEMORY */
678 if (enlist(rsp->headers, "Content-Type: text/css"))
680 return JB_ERR_MEMORY;
686 /*********************************************************************
688 * Function : cgi_send_user_manual
690 * Description : CGI function that sends a file in the user
694 * 1 : csp = Current client state (buffers, headers, etc...)
695 * 2 : rsp = http_response data structure for output
696 * 3 : parameters = map of cgi parameters
698 * CGI Parameters : file=name.html, the name of the HTML file
699 * (relative to user-manual from config)
701 * Returns : JB_ERR_OK on success
702 * JB_ERR_MEMORY on out-of-memory error.
704 *********************************************************************/
705 jb_err cgi_send_user_manual(struct client_state *csp,
706 struct http_response *rsp,
707 const struct map *parameters)
709 const char * filename;
712 jb_err err = JB_ERR_OK;
719 if (!parameters->first)
721 /* requested http://p.p/user-manual (without trailing slash) */
722 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
725 get_string_param(parameters, "file", &filename);
726 /* Check paramter for hack attempts */
727 if (filename && strchr(filename, '/'))
729 return JB_ERR_CGI_PARAMS;
731 if (filename && strstr(filename, ".."))
733 return JB_ERR_CGI_PARAMS;
736 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
737 if (full_path == NULL)
739 return JB_ERR_MEMORY;
742 /* Open user-manual file */
745 * XXX: Do we support other operating systems that
746 * require special treatment to fopen in binary mode?
748 if (NULL == (fp = fopen(full_path, "rb")))
750 if (NULL == (fp = fopen(full_path, "r")))
751 #endif /* def WIN32 */
753 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
754 err = cgi_error_no_template(csp, rsp, full_path);
759 /* Get file length */
760 fseek(fp, 0, SEEK_END);
762 fseek(fp, 0, SEEK_SET);
764 /* Allocate memory and load the file directly into the body */
765 rsp->body = (char *)malloc(length+1);
770 return JB_ERR_MEMORY;
772 if (!fread(rsp->body, length, 1, fp))
775 * This happens if we didn't fopen in binary mode.
776 * If it does, we just log it and serve what we got,
777 * most likely padded with garbage.
779 log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
784 rsp->content_length = (int)length;
786 /* Guess correct Content-Type based on the filename's ending */
789 length = strlen(filename);
795 if((length>=4) && !strcmp(&filename[length-4], ".css"))
797 err = enlist(rsp->headers, "Content-Type: text/css");
799 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
801 err = enlist(rsp->headers, "Content-Type: image/jpeg");
805 err = enlist(rsp->headers, "Content-Type: text/html");
812 /*********************************************************************
814 * Function : cgi_show_version
816 * Description : CGI function that returns a a web page describing the
817 * file versions of Privoxy.
820 * 1 : csp = Current client state (buffers, headers, etc...)
821 * 2 : rsp = http_response data structure for output
822 * 3 : parameters = map of cgi parameters
824 * CGI Parameters : none
826 * Returns : JB_ERR_OK on success
827 * JB_ERR_MEMORY on out-of-memory error.
829 *********************************************************************/
830 jb_err cgi_show_version(struct client_state *csp,
831 struct http_response *rsp,
832 const struct map *parameters)
840 if (NULL == (exports = default_exports(csp, "show-version")))
842 return JB_ERR_MEMORY;
845 if (map(exports, "sourceversions", 1, show_rcs(), 0))
848 return JB_ERR_MEMORY;
851 return template_fill_for_cgi(csp, "show-version", exports, rsp);
855 /*********************************************************************
857 * Function : cgi_show_status
859 * Description : CGI function that returns a a web page describing the
860 * current status of Privoxy.
863 * 1 : csp = Current client state (buffers, headers, etc...)
864 * 2 : rsp = http_response data structure for output
865 * 3 : parameters = map of cgi parameters
868 * file : Which file to show. Only first letter is checked,
870 * - "p"ermissions (actions) file
873 * Default is to show menu and other information.
875 * Returns : JB_ERR_OK on success
876 * JB_ERR_MEMORY on out-of-memory error.
878 *********************************************************************/
879 jb_err cgi_show_status(struct client_state *csp,
880 struct http_response *rsp,
881 const struct map *parameters)
888 char buf[BUFFER_SIZE];
889 const char * filename = NULL;
890 char * file_description = NULL;
891 #ifdef FEATURE_STATISTICS
892 float perc_rej; /* Percentage of http requests rejected */
894 int local_urls_rejected;
895 #endif /* ndef FEATURE_STATISTICS */
896 struct file_list * fl;
897 struct url_actions * b;
898 jb_err err = JB_ERR_OK;
906 if (NULL == (exports = default_exports(csp, "show-status")))
908 return JB_ERR_MEMORY;
911 switch (*(lookup(parameters, "file")))
914 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
916 filename = csp->actions_list[i]->filename;
917 file_description = "Actions File";
922 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
924 filename = csp->rlist[i]->filename;
925 file_description = "Filter File";
933 filename = csp->tlist->filename;
934 file_description = "Trust File";
937 #endif /* def FEATURE_TRUST */
940 if (NULL != filename)
942 if ( map(exports, "file-description", 1, file_description, 1)
943 || map(exports, "filepath", 1, html_encode(filename), 0) )
946 return JB_ERR_MEMORY;
949 if ((fp = fopen(filename, "r")) == NULL)
951 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
954 return JB_ERR_MEMORY;
960 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
962 string_join (&s, html_encode(buf));
966 if (map(exports, "contents", 1, s, 0))
969 return JB_ERR_MEMORY;
973 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
977 for (j = 0; (s != NULL) && (j < Argc); j++)
979 if (!err) err = string_join (&s, html_encode(Argv[j]));
980 if (!err) err = string_append(&s, " ");
982 if (!err) err = map(exports, "invocation", 1, s, 0);
984 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
985 if (!err) err = show_defines(exports);
990 return JB_ERR_MEMORY;
993 #ifdef FEATURE_STATISTICS
994 local_urls_read = urls_read;
995 local_urls_rejected = urls_rejected;
998 * Need to alter the stats not to include the fetch of this
1001 * Can't do following thread safely! doh!
1004 * urls_rejected--; * This will be incremented subsequently *
1007 if (local_urls_read == 0)
1009 if (!err) err = map_block_killer(exports, "have-stats");
1013 if (!err) err = map_block_killer(exports, "have-no-stats");
1015 perc_rej = (float)local_urls_rejected * 100.0F /
1016 (float)local_urls_read;
1018 sprintf(buf, "%d", local_urls_read);
1019 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1021 sprintf(buf, "%d", local_urls_rejected);
1022 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1024 sprintf(buf, "%6.2f", perc_rej);
1025 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1028 #else /* ndef FEATURE_STATISTICS */
1029 err = err || map_block_killer(exports, "statistics");
1030 #endif /* ndef FEATURE_STATISTICS */
1033 * List all action files in use, together with view and edit links,
1034 * except for standard.action, which should only be viewable. (Not
1035 * enforced in the editor itself)
1036 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1039 for (i = 0; i < MAX_AF_FILES; i++)
1041 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
1043 if (!err) err = string_append(&s, "<tr><td>");
1044 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1045 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1046 if (!err) err = string_append(&s, buf);
1048 #ifdef FEATURE_CGI_EDIT_ACTIONS
1049 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1051 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1052 if (!err) err = string_append(&s, buf);
1056 if (!err) err = string_append(&s, "</td></tr>\n");
1061 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1065 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1069 * List all re_filterfiles in use, together with view options.
1070 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1073 for (i = 0; i < MAX_AF_FILES; i++)
1075 if (((fl = csp->rlist[i]) != NULL) && ((b = fl->f) != NULL))
1077 if (!err) err = string_append(&s, "<tr><td>");
1078 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1079 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1080 if (!err) err = string_append(&s, buf);
1081 if (!err) err = string_append(&s, "</td></tr>\n");
1086 if (!err) err = map(exports, "re-filter-filename", 1, s, 0);
1090 if (!err) err = map(exports, "re-filter-filename", 1, "<tr><td>None specified</td></tr>", 1);
1091 if (!err) err = map_block_killer(exports, "have-filterfile");
1094 #ifdef FEATURE_TRUST
1097 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1101 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1102 if (!err) err = map_block_killer(exports, "have-trustfile");
1105 if (!err) err = map_block_killer(exports, "trust-support");
1106 #endif /* ndef FEATURE_TRUST */
1111 return JB_ERR_MEMORY;
1114 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1118 /*********************************************************************
1120 * Function : cgi_show_url_info
1122 * Description : CGI function that determines and shows which actions
1123 * Privoxy will perform for a given url, and which
1124 * matches starting from the defaults have lead to that.
1127 * 1 : csp = Current client state (buffers, headers, etc...)
1128 * 2 : rsp = http_response data structure for output
1129 * 3 : parameters = map of cgi parameters
1132 * url : The url whose actions are to be determined.
1133 * If url is unset, the url-given conditional will be
1134 * set, so that all but the form can be suppressed in
1137 * Returns : JB_ERR_OK on success
1138 * JB_ERR_MEMORY on out-of-memory error.
1140 *********************************************************************/
1141 jb_err cgi_show_url_info(struct client_state *csp,
1142 struct http_response *rsp,
1143 const struct map *parameters)
1146 struct map *exports;
1153 if (NULL == (exports = default_exports(csp, "show-url-info")))
1155 return JB_ERR_MEMORY;
1159 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1161 url_param = strdup(lookup(parameters, "url"));
1162 if (url_param == NULL)
1165 return JB_ERR_MEMORY;
1170 * Handle prefixes. 4 possibilities:
1171 * 1) "http://" or "https://" prefix present and followed by URL - OK
1172 * 2) Only the "http://" or "https://" part is present, no URL - change
1173 * to empty string so it will be detected later as "no URL".
1174 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1176 * 4) Parameter not specified or is empty string - let this fall through
1177 * for now, next block of code will handle it.
1179 if (0 == strncmp(url_param, "http://", 7))
1181 if (url_param[7] == '\0')
1184 * Empty URL (just prefix).
1185 * Make it totally empty so it's caught by the next if()
1187 url_param[0] = '\0';
1190 else if (0 == strncmp(url_param, "https://", 8))
1192 if (url_param[8] == '\0')
1195 * Empty URL (just prefix).
1196 * Make it totally empty so it's caught by the next if()
1198 url_param[0] = '\0';
1201 else if (url_param[0] != '\0')
1204 * Unknown prefix - assume http://
1206 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1207 if (NULL == url_param_prefixed)
1211 return JB_ERR_MEMORY;
1213 strcpy(url_param_prefixed, "http://");
1214 strcpy(url_param_prefixed + 7, url_param);
1216 url_param = url_param_prefixed;
1220 if (url_param[0] == '\0')
1222 /* URL paramater not specified, display query form only. */
1224 if (map_block_killer(exports, "url-given")
1225 || map(exports, "url", 1, "", 1))
1228 return JB_ERR_MEMORY;
1233 /* Given a URL, so query it. */
1238 struct file_list *fl;
1239 struct url_actions *b;
1240 struct http_request url_to_query[1];
1241 struct current_action_spec action[1];
1244 if (map(exports, "url", 1, html_encode(url_param), 0))
1248 return JB_ERR_MEMORY;
1251 init_current_action(action);
1253 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1255 free_current_action(action);
1258 return JB_ERR_MEMORY;
1261 err = parse_http_url(url_param, url_to_query, csp);
1265 if (err == JB_ERR_MEMORY)
1267 free_http_request(url_to_query);
1268 free_current_action(action);
1270 return JB_ERR_MEMORY;
1276 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1277 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1279 free_current_action(action);
1280 free_http_request(url_to_query);
1285 return JB_ERR_MEMORY;
1288 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1292 * We have a warning about SSL paths. Hide it for insecure sites.
1294 if (!url_to_query->ssl)
1296 if (map_block_killer(exports, "https"))
1298 free_current_action(action);
1300 free_http_request(url_to_query);
1301 return JB_ERR_MEMORY;
1305 matches = strdup("<table class=\"transparent\">");
1307 for (i = 0; i < MAX_AF_FILES; i++)
1309 if (NULL == csp->config->actions_file_short[i]
1310 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1314 if ((fl = csp->actions_list[i]) != NULL)
1316 if ((b = fl->f) != NULL)
1318 /* FIXME: Hardcoded HTML! */
1319 string_append(&matches, "<tr><th>In file: ");
1320 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1321 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1322 string_append(&matches, buf);
1323 string_append(&matches, "View</a>");
1324 #ifdef FEATURE_CGI_EDIT_ACTIONS
1325 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1326 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1327 string_append(&matches, "\">Edit</a>");
1329 string_append(&matches, "</th></tr>\n");
1336 for (; (b != NULL) && (matches != NULL); b = b->next)
1338 if (url_match(b->url, url_to_query))
1340 string_append(&matches, "<tr><td>{");
1341 string_join (&matches, actions_to_html(csp, b->action));
1342 string_append(&matches, " }</b><br>\n<code>");
1343 string_join (&matches, html_encode(b->url->spec));
1344 string_append(&matches, "</code></td></tr>\n");
1346 if (merge_current_action(action, b->action))
1349 free_http_request(url_to_query);
1350 free_current_action(action);
1352 return JB_ERR_MEMORY;
1360 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1363 string_append(&matches, "</table>\n");
1365 free_http_request(url_to_query);
1367 if (matches == NULL)
1369 free_current_action(action);
1371 return JB_ERR_MEMORY;
1374 if (map(exports, "matches", 1, matches , 0))
1376 free_current_action(action);
1378 return JB_ERR_MEMORY;
1381 s = current_action_to_html(csp, action);
1383 free_current_action(action);
1385 if (map(exports, "final", 1, s, 0))
1388 return JB_ERR_MEMORY;
1392 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1396 /*********************************************************************
1398 * Function : cgi_robots_txt
1400 * Description : CGI function to return "/robots.txt".
1403 * 1 : csp = Current client state (buffers, headers, etc...)
1404 * 2 : rsp = http_response data structure for output
1405 * 3 : parameters = map of cgi parameters
1407 * CGI Parameters : None
1409 * Returns : JB_ERR_OK on success
1410 * JB_ERR_MEMORY on out-of-memory error.
1412 *********************************************************************/
1413 jb_err cgi_robots_txt(struct client_state *csp,
1414 struct http_response *rsp,
1415 const struct map *parameters)
1421 "# This is the Privoxy control interface.\n"
1422 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1428 if (rsp->body == NULL)
1430 return JB_ERR_MEMORY;
1433 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1437 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1438 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1440 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1444 /*********************************************************************
1446 * Function : show_defines
1448 * Description : Add to a map the state od all conditional #defines
1449 * used when building
1452 * 1 : exports = map to extend
1454 * Returns : JB_ERR_OK on success
1455 * JB_ERR_MEMORY on out-of-memory error.
1457 *********************************************************************/
1458 static jb_err show_defines(struct map *exports)
1460 jb_err err = JB_ERR_OK;
1463 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1464 #else /* ifndef FEATURE_ACL */
1465 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1466 #endif /* ndef FEATURE_ACL */
1468 #ifdef FEATURE_CGI_EDIT_ACTIONS
1469 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1470 #else /* ifndef FEATURE_COOKIE_JAR */
1471 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1472 #endif /* ndef FEATURE_COOKIE_JAR */
1474 #ifdef FEATURE_COOKIE_JAR
1475 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1476 #else /* ifndef FEATURE_COOKIE_JAR */
1477 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1478 #endif /* ndef FEATURE_COOKIE_JAR */
1480 #ifdef FEATURE_FAST_REDIRECTS
1481 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1482 #else /* ifndef FEATURE_FAST_REDIRECTS */
1483 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1484 #endif /* ndef FEATURE_FAST_REDIRECTS */
1486 #ifdef FEATURE_FORCE_LOAD
1487 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1488 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1489 #else /* ifndef FEATURE_FORCE_LOAD */
1490 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1491 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1492 #endif /* ndef FEATURE_FORCE_LOAD */
1494 #ifdef FEATURE_IMAGE_BLOCKING
1495 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1496 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1497 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1498 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1500 #ifdef FEATURE_IMAGE_DETECT_MSIE
1501 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1502 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1503 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1504 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1506 #ifdef FEATURE_KILL_POPUPS
1507 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1508 #else /* ifndef FEATURE_KILL_POPUPS */
1509 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1510 #endif /* ndef FEATURE_KILL_POPUPS */
1512 #ifdef FEATURE_NO_GIFS
1513 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1514 #else /* ifndef FEATURE_NO_GIFS */
1515 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1516 #endif /* ndef FEATURE_NO_GIFS */
1518 #ifdef FEATURE_PTHREAD
1519 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1520 #else /* ifndef FEATURE_PTHREAD */
1521 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1522 #endif /* ndef FEATURE_PTHREAD */
1524 #ifdef FEATURE_STATISTICS
1525 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1526 #else /* ifndef FEATURE_STATISTICS */
1527 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1528 #endif /* ndef FEATURE_STATISTICS */
1530 #ifdef FEATURE_TOGGLE
1531 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1532 #else /* ifndef FEATURE_TOGGLE */
1533 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1534 #endif /* ndef FEATURE_TOGGLE */
1536 #ifdef FEATURE_TRUST
1537 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1538 #else /* ifndef FEATURE_TRUST */
1539 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1540 #endif /* ndef FEATURE_TRUST */
1543 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1544 #else /* ifndef STATIC_PCRE */
1545 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1546 #endif /* ndef STATIC_PCRE */
1549 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1550 #else /* ifndef STATIC_PCRS */
1551 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1552 #endif /* ndef STATIC_PCRS */
1558 /*********************************************************************
1560 * Function : show_rcs
1562 * Description : Create a string with the rcs info for all sourcefiles
1566 * Returns : A string, or NULL on out-of-memory.
1568 *********************************************************************/
1569 static char *show_rcs(void)
1571 char *result = strdup("");
1572 char buf[BUFFER_SIZE];
1574 /* Instead of including *all* dot h's in the project (thus creating a
1575 * tremendous amount of dependencies), I will concede to declaring them
1576 * as extern's. This forces the developer to add to this list, but oh well.
1579 #define SHOW_RCS(__x) \
1581 extern const char __x[]; \
1582 sprintf(buf, "%s\n", __x); \
1583 string_append(&result, buf); \
1586 /* In alphabetical order */
1587 SHOW_RCS(actions_h_rcs)
1588 SHOW_RCS(actions_rcs)
1590 SHOW_RCS(amiga_h_rcs)
1592 #endif /* def AMIGA */
1595 #ifdef FEATURE_CGI_EDIT_ACTIONS
1596 SHOW_RCS(cgiedit_h_rcs)
1597 SHOW_RCS(cgiedit_rcs)
1598 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1599 SHOW_RCS(cgisimple_h_rcs)
1600 SHOW_RCS(cgisimple_rcs)
1602 SHOW_RCS(cygwin_h_rcs)
1604 SHOW_RCS(deanimate_h_rcs)
1605 SHOW_RCS(deanimate_rcs)
1606 SHOW_RCS(encode_h_rcs)
1607 SHOW_RCS(encode_rcs)
1608 SHOW_RCS(errlog_h_rcs)
1609 SHOW_RCS(errlog_rcs)
1610 SHOW_RCS(filters_h_rcs)
1611 SHOW_RCS(filters_rcs)
1612 SHOW_RCS(gateway_h_rcs)
1613 SHOW_RCS(gateway_rcs)
1614 SHOW_RCS(jbsockets_h_rcs)
1615 SHOW_RCS(jbsockets_rcs)
1618 #ifdef FEATURE_KILL_POPUPS
1619 SHOW_RCS(killpopup_h_rcs)
1620 SHOW_RCS(killpopup_rcs)
1621 #endif /* def FEATURE_KILL_POPUPS */
1622 SHOW_RCS(list_h_rcs)
1624 SHOW_RCS(loadcfg_h_rcs)
1625 SHOW_RCS(loadcfg_rcs)
1626 SHOW_RCS(loaders_h_rcs)
1627 SHOW_RCS(loaders_rcs)
1628 SHOW_RCS(miscutil_h_rcs)
1629 SHOW_RCS(miscutil_rcs)
1630 SHOW_RCS(parsers_h_rcs)
1631 SHOW_RCS(parsers_rcs)
1633 SHOW_RCS(pcrs_h_rcs)
1634 SHOW_RCS(project_h_rcs)
1635 SHOW_RCS(ssplit_h_rcs)
1636 SHOW_RCS(ssplit_rcs)
1637 SHOW_RCS(urlmatch_h_rcs)
1638 SHOW_RCS(urlmatch_rcs)
1640 #ifndef _WIN_CONSOLE
1641 SHOW_RCS(w32log_h_rcs)
1642 SHOW_RCS(w32log_rcs)
1643 SHOW_RCS(w32res_h_rcs)
1644 SHOW_RCS(w32taskbar_h_rcs)
1645 SHOW_RCS(w32taskbar_rcs)
1646 #endif /* ndef _WIN_CONSOLE */
1647 SHOW_RCS(win32_h_rcs)
1649 #endif /* def _WIN32 */