1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.39 2006/09/08 09:49:23 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.39 2006/09/08 09:49:23 fabiankeil
40 * Deliver documents in the user-manual directory
41 * with "Content-Type text/css" if their filename
44 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
45 * Incorporate modified version of Roland Rosenfeld's patch to
46 * optionally access the user-manual via Privoxy. Closes patch 679075.
48 * Formatting changed to Privoxy style, added call to
49 * cgi_error_no_template if the requested file doesn't
50 * exist and modified check whether or not Privoxy itself
51 * should serve the manual. Should work cross-platform now.
53 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
54 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
55 * with what was really the latest development (the v_3_0_branch branch)
57 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
58 * Multiple filter file support
60 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
61 * Undo some damaging memory leak patches
63 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
64 * A few memory leaks plugged (mostly on error paths)
66 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
67 * Another instance of:
68 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
69 * Thanks to Magnus Holmgren for the patch
71 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
72 * - Prevent line wrap beween "View/Edit" link buttons on status page
73 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
75 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
76 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
78 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
79 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
81 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
82 * Applying patch from Andreas as provided by Hal on the list.
83 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
85 * Revision 1.35 2002/05/12 21:44:44 jongfoster
86 * Adding amiga.[ch] revision information, if on an amiga.
88 * Revision 1.34 2002/04/30 12:06:12 oes
89 * Deleted unused code from default_cgi
91 * Revision 1.33 2002/04/30 11:14:52 oes
92 * Made csp the first parameter in *action_to_html
94 * Revision 1.32 2002/04/26 18:29:13 jongfoster
95 * Fixing this Visual C++ warning:
96 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
98 * Revision 1.31 2002/04/26 12:54:36 oes
99 * - Kill obsolete REDIRECT_URL code
100 * - Error handling fixes
101 * - Style sheet related HTML snipplet changes
102 * - cgi_show_url_info:
103 * - Matches now in table, actions on single lines,
105 * - standard.action suppressed
106 * - Buttons to View and Edit AFs
108 * Revision 1.30 2002/04/24 02:18:08 oes
109 * - show-status is now the starting point for editing
110 * the actions files, generate list of all AFs with buttons
111 * for viewing and editing, new look for file list (Jon:
112 * buttons now aligned ;-P ), view mode now supports multiple
113 * AFs, name changes, no view links for unspecified files,
114 * no edit link for standard.action.
116 * - Jon's multiple AF patch: cgi_show_url_info now uses all
117 * AFs and marks the output accordingly
119 * Revision 1.29 2002/04/10 13:38:35 oes
120 * load_template signature changed
122 * Revision 1.28 2002/04/07 15:42:12 jongfoster
123 * Fixing send-banner?type=auto when the image-blocker is
124 * a redirect to send-banner
126 * Revision 1.27 2002/04/05 15:50:48 oes
127 * added send-stylesheet CGI
129 * Revision 1.26 2002/04/04 00:36:36 gliptak
130 * always use pcre for matching
132 * Revision 1.25 2002/04/03 22:28:03 gliptak
133 * Removed references to gnu_regex
135 * Revision 1.24 2002/04/02 16:12:47 oes
136 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
138 * Revision 1.23 2002/03/26 22:29:54 swa
139 * we have a new homepage!
141 * Revision 1.22 2002/03/24 16:18:15 jongfoster
144 * Revision 1.21 2002/03/24 15:23:33 jongfoster
147 * Revision 1.20 2002/03/24 13:25:43 swa
148 * name change related issues
150 * Revision 1.19 2002/03/16 23:54:06 jongfoster
151 * Adding graceful termination feature, to help look for memory leaks.
152 * If you enable this (which, by design, has to be done by hand
153 * editing config.h) and then go to http://i.j.b/die, then the program
154 * will exit cleanly after the *next* request. It should free all the
155 * memory that was used.
157 * Revision 1.18 2002/03/12 01:44:49 oes
158 * Changed default for "blocked" image from jb logo to checkboard pattern
160 * Revision 1.17 2002/03/08 16:43:18 oes
161 * Added choice beween GIF and PNG built-in images
163 * Revision 1.16 2002/03/07 03:48:38 oes
164 * - Changed built-in images from GIF to PNG
165 * (with regard to Unisys patent issue)
166 * - Added a 4x4 pattern PNG which is less intrusive
167 * than the logo but also clearly marks the deleted banners
169 * Revision 1.15 2002/03/06 22:54:35 jongfoster
170 * Automated function-comment nitpicking.
172 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
173 * Clean up a little CRLF unpleasantness that suddenly appeared
175 * Revision 1.13 2002/02/21 00:10:37 jongfoster
176 * Adding send-banner?type=auto option
178 * Revision 1.12 2002/01/23 01:03:32 jongfoster
179 * Fixing gcc [CygWin] compiler warnings
181 * Revision 1.11 2002/01/23 00:01:04 jongfoster
182 * Adding cgi_transparent_gif() for http://i.j.b/t
183 * Adding missing html_encode() to many CGI functions.
184 * Adding urlmatch.[ch] to http://i.j.b/show-version
186 * Revision 1.10 2002/01/17 21:10:37 jongfoster
187 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
188 * Also fixing a problem in the same function with improperly quoted URLs
189 * in output HTML, and adding code to handle https:// URLs correctly.
191 * Revision 1.9 2001/11/30 23:09:15 jongfoster
192 * Now reports on FEATURE_CGI_EDIT_ACTIONS
193 * Removing FEATURE_DENY_GZIP from template
195 * Revision 1.8 2001/11/13 00:14:07 jongfoster
196 * Fixing stupid bug now I've figured out what || means.
197 * (It always returns 0 or 1, not one of it's paramaters.)
199 * Revision 1.7 2001/10/23 21:48:19 jongfoster
200 * Cleaning up error handling in CGI functions - they now send back
201 * a HTML error page and should never cause a FATAL error. (Fixes one
202 * potential source of "denial of service" attacks).
204 * CGI actions file editor that works and is actually useful.
206 * Ability to toggle JunkBuster remotely using a CGI call.
208 * You can turn off both the above features in the main configuration
209 * file, e.g. if you are running a multi-user proxy.
211 * Revision 1.6 2001/10/14 22:00:32 jongfoster
212 * Adding support for a 404 error when an invalid CGI page is requested.
214 * Revision 1.5 2001/10/07 15:30:41 oes
215 * Removed FEATURE_DENY_GZIP
217 * Revision 1.4 2001/10/02 15:31:12 oes
218 * Introduced show-request cgi
220 * Revision 1.3 2001/09/22 16:34:44 jongfoster
221 * Removing unneeded #includes
223 * Revision 1.2 2001/09/19 18:01:11 oes
224 * Fixed comments; cosmetics
226 * Revision 1.1 2001/09/16 17:08:54 jongfoster
227 * Moving simple CGI functions from cgi.c to new file cgisimple.c
230 **********************************************************************/
236 #include <sys/types.h>
243 #define snprintf _snprintf
244 #endif /* def _WIN32 */
248 #include "cgisimple.h"
254 #include "miscutil.h"
257 #include "urlmatch.h"
260 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
263 static char *show_rcs(void);
264 static jb_err show_defines(struct map *exports);
267 /*********************************************************************
269 * Function : cgi_default
271 * Description : CGI function that is called for the CGI_SITE_1_HOST
272 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
273 * Boring - only exports the default exports.
276 * 1 : csp = Current client state (buffers, headers, etc...)
277 * 2 : rsp = http_response data structure for output
278 * 3 : parameters = map of cgi parameters
280 * CGI Parameters : none
282 * Returns : JB_ERR_OK on success
283 * JB_ERR_MEMORY on out-of-memory
285 *********************************************************************/
286 jb_err cgi_default(struct client_state *csp,
287 struct http_response *rsp,
288 const struct map *parameters)
295 if (NULL == (exports = default_exports(csp, "")))
297 return JB_ERR_MEMORY;
300 return template_fill_for_cgi(csp, "default", exports, rsp);
304 /*********************************************************************
306 * Function : cgi_error_404
308 * Description : CGI function that is called if an unknown action was
312 * 1 : csp = Current client state (buffers, headers, etc...)
313 * 2 : rsp = http_response data structure for output
314 * 3 : parameters = map of cgi parameters
316 * CGI Parameters : none
318 * Returns : JB_ERR_OK on success
319 * JB_ERR_MEMORY on out-of-memory error.
321 *********************************************************************/
322 jb_err cgi_error_404(struct client_state *csp,
323 struct http_response *rsp,
324 const struct map *parameters)
332 if (NULL == (exports = default_exports(csp, NULL)))
334 return JB_ERR_MEMORY;
337 rsp->status = strdup("404 Privoxy configuration page not found");
338 if (rsp->status == NULL)
341 return JB_ERR_MEMORY;
344 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
348 #ifdef FEATURE_GRACEFUL_TERMINATION
349 /*********************************************************************
353 * Description : CGI function to shut down Privoxy.
354 * NOTE: Turning this on in a production build
355 * would be a BAD idea. An EXTREMELY BAD idea.
356 * In short, don't do it.
359 * 1 : csp = Current client state (buffers, headers, etc...)
360 * 2 : rsp = http_response data structure for output
361 * 3 : parameters = map of cgi parameters
363 * CGI Parameters : none
365 * Returns : JB_ERR_OK on success
366 * JB_ERR_MEMORY on out-of-memory error.
368 *********************************************************************/
369 jb_err cgi_die (struct client_state *csp,
370 struct http_response *rsp,
371 const struct map *parameters)
381 * I don't really care what gets sent back to the browser.
382 * Take the easy option - "out of memory" page.
385 return JB_ERR_MEMORY;
387 #endif /* def FEATURE_GRACEFUL_TERMINATION */
390 /*********************************************************************
392 * Function : cgi_show_request
394 * Description : Show the client's request and what sed() would have
398 * 1 : csp = Current client state (buffers, headers, etc...)
399 * 2 : rsp = http_response data structure for output
400 * 3 : parameters = map of cgi parameters
402 * CGI Parameters : none
404 * Returns : JB_ERR_OK on success
405 * JB_ERR_MEMORY on out-of-memory error.
407 *********************************************************************/
408 jb_err cgi_show_request(struct client_state *csp,
409 struct http_response *rsp,
410 const struct map *parameters)
419 if (NULL == (exports = default_exports(csp, "show-request")))
421 return JB_ERR_MEMORY;
425 * Repair the damage done to the IOB by get_header()
427 for (p = csp->iob->buf; p < csp->iob->eod; p++)
429 if (*p == '\0') *p = '\n';
433 * Export the original client's request and the one we would
434 * be sending to the server if this wasn't a CGI call
437 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
440 return JB_ERR_MEMORY;
443 if (map(exports, "processed-request", 1, html_encode_and_free_original(
444 sed(client_patterns, add_client_headers, csp)), 0))
447 return JB_ERR_MEMORY;
450 return template_fill_for_cgi(csp, "show-request", exports, rsp);
454 /*********************************************************************
456 * Function : cgi_send_banner
458 * Description : CGI function that returns a banner.
461 * 1 : csp = Current client state (buffers, headers, etc...)
462 * 2 : rsp = http_response data structure for output
463 * 3 : parameters = map of cgi parameters
466 * type : Selects the type of banner between "trans", "logo",
467 * and "auto". Defaults to "logo" if absent or invalid.
468 * "auto" means to select as if we were image-blocking.
469 * (Only the first character really counts; b and t are
472 * Returns : JB_ERR_OK on success
473 * JB_ERR_MEMORY on out-of-memory error.
475 *********************************************************************/
476 jb_err cgi_send_banner(struct client_state *csp,
477 struct http_response *rsp,
478 const struct map *parameters)
480 char imagetype = lookup(parameters, "type")[0];
483 * If type is auto, then determine the right thing
484 * to do from the set-image-blocker action
486 if (imagetype == 'a')
493 #ifdef FEATURE_IMAGE_BLOCKING
494 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
496 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
497 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
498 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
502 /* Use default - nothing to do here. */
504 else if (0 == strcmpic(p, "blank"))
508 else if (0 == strcmpic(p, "pattern"))
514 * If the action is to call this CGI, determine
517 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
519 imagetype = p[sizeof(prefix1) - 1];
521 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
523 imagetype = p[sizeof(prefix2) - 1];
527 * Everything else must (should) be a URL to
535 #endif /* def FEATURE_IMAGE_BLOCKING */
539 * Now imagetype is either the non-auto type we were called with,
540 * or it was auto and has since been determined. In any case, we
541 * can proceed to actually answering the request by sending a redirect
542 * or an image as appropriate:
544 if (imagetype == 'r')
546 rsp->status = strdup("302 Local Redirect from Privoxy");
547 if (rsp->status == NULL)
549 return JB_ERR_MEMORY;
551 if (enlist_unique_header(rsp->headers, "Location",
552 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
554 return JB_ERR_MEMORY;
559 if ((imagetype == 'b') || (imagetype == 't'))
561 rsp->body = bindup(image_blank_data, image_blank_length);
562 rsp->content_length = image_blank_length;
566 rsp->body = bindup(image_pattern_data, image_pattern_length);
567 rsp->content_length = image_pattern_length;
570 if (rsp->body == NULL)
572 return JB_ERR_MEMORY;
574 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
576 return JB_ERR_MEMORY;
587 /*********************************************************************
589 * Function : cgi_transparent_image
591 * Description : CGI function that sends a 1x1 transparent image.
594 * 1 : csp = Current client state (buffers, headers, etc...)
595 * 2 : rsp = http_response data structure for output
596 * 3 : parameters = map of cgi parameters
598 * CGI Parameters : None
600 * Returns : JB_ERR_OK on success
601 * JB_ERR_MEMORY on out-of-memory error.
603 *********************************************************************/
604 jb_err cgi_transparent_image(struct client_state *csp,
605 struct http_response *rsp,
606 const struct map *parameters)
608 rsp->body = bindup(image_blank_data, image_blank_length);
609 rsp->content_length = image_blank_length;
611 if (rsp->body == NULL)
613 return JB_ERR_MEMORY;
616 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
618 return JB_ERR_MEMORY;
628 /*********************************************************************
630 * Function : cgi_send_stylesheet
632 * Description : CGI function that sends a css stylesheet found
633 * in the cgi-style.css template
636 * 1 : csp = Current client state (buffers, headers, etc...)
637 * 2 : rsp = http_response data structure for output
638 * 3 : parameters = map of cgi parameters
640 * CGI Parameters : None
642 * Returns : JB_ERR_OK on success
643 * JB_ERR_MEMORY on out-of-memory error.
645 *********************************************************************/
646 jb_err cgi_send_stylesheet(struct client_state *csp,
647 struct http_response *rsp,
648 const struct map *parameters)
655 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
657 if (err == JB_ERR_FILE)
660 * No way to tell user; send empty stylesheet
662 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
666 return err; /* JB_ERR_MEMORY */
669 if (enlist(rsp->headers, "Content-Type: text/css"))
671 return JB_ERR_MEMORY;
677 /*********************************************************************
679 * Function : cgi_send_user_manual
681 * Description : CGI function that sends a file in the user
685 * 1 : csp = Current client state (buffers, headers, etc...)
686 * 2 : rsp = http_response data structure for output
687 * 3 : parameters = map of cgi parameters
689 * CGI Parameters : file=name.html, the name of the HTML file
690 * (relative to user-manual from config)
692 * Returns : JB_ERR_OK on success
693 * JB_ERR_MEMORY on out-of-memory error.
695 *********************************************************************/
696 jb_err cgi_send_user_manual(struct client_state *csp,
697 struct http_response *rsp,
698 const struct map *parameters)
700 const char * filename;
703 jb_err err = JB_ERR_OK;
710 get_string_param(parameters, "file", &filename);
711 /* Check paramter for hack attempts */
712 if (filename && strchr(filename, '/'))
714 return JB_ERR_CGI_PARAMS;
716 if (filename && strstr(filename, ".."))
718 return JB_ERR_CGI_PARAMS;
721 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
722 if (full_path == NULL)
724 return JB_ERR_MEMORY;
727 /* Open user-manual file */
728 if (NULL == (fp = fopen(full_path, "r")))
730 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
731 err = cgi_error_no_template(csp, rsp, full_path);
736 /* Get file length */
737 fseek(fp, 0, SEEK_END);
739 fseek(fp, 0, SEEK_SET);
741 /* Allocate memory and load the file directly into the body */
742 rsp->body = (char *)malloc(length+1);
747 return JB_ERR_MEMORY;
749 if (!fread(rsp->body, length, 1, fp))
752 * Why should this happen? If it does, we just log
753 * it and serve what we got, most likely padded with garbage.
755 log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
760 /* Privoxy only gets it right for non-binary content. */
761 rsp->content_length = (int)length;
763 /* Guess correct Content-Type based on the filename's ending */
766 length = strlen(filename);
772 if((length>=4) && !strcmp(&filename[length-4], ".css"))
774 err = enlist(rsp->headers, "Content-Type: text/css");
776 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
778 err = enlist(rsp->headers, "Content-Type: image/jpeg");
782 err = enlist(rsp->headers, "Content-Type: text/html");
789 /*********************************************************************
791 * Function : cgi_show_version
793 * Description : CGI function that returns a a web page describing the
794 * file versions of Privoxy.
797 * 1 : csp = Current client state (buffers, headers, etc...)
798 * 2 : rsp = http_response data structure for output
799 * 3 : parameters = map of cgi parameters
801 * CGI Parameters : none
803 * Returns : JB_ERR_OK on success
804 * JB_ERR_MEMORY on out-of-memory error.
806 *********************************************************************/
807 jb_err cgi_show_version(struct client_state *csp,
808 struct http_response *rsp,
809 const struct map *parameters)
817 if (NULL == (exports = default_exports(csp, "show-version")))
819 return JB_ERR_MEMORY;
822 if (map(exports, "sourceversions", 1, show_rcs(), 0))
825 return JB_ERR_MEMORY;
828 return template_fill_for_cgi(csp, "show-version", exports, rsp);
832 /*********************************************************************
834 * Function : cgi_show_status
836 * Description : CGI function that returns a a web page describing the
837 * current status of Privoxy.
840 * 1 : csp = Current client state (buffers, headers, etc...)
841 * 2 : rsp = http_response data structure for output
842 * 3 : parameters = map of cgi parameters
845 * file : Which file to show. Only first letter is checked,
847 * - "p"ermissions (actions) file
850 * Default is to show menu and other information.
852 * Returns : JB_ERR_OK on success
853 * JB_ERR_MEMORY on out-of-memory error.
855 *********************************************************************/
856 jb_err cgi_show_status(struct client_state *csp,
857 struct http_response *rsp,
858 const struct map *parameters)
865 char buf[BUFFER_SIZE];
866 const char * filename = NULL;
867 char * file_description = NULL;
868 #ifdef FEATURE_STATISTICS
869 float perc_rej; /* Percentage of http requests rejected */
871 int local_urls_rejected;
872 #endif /* ndef FEATURE_STATISTICS */
873 struct file_list * fl;
874 struct url_actions * b;
875 jb_err err = JB_ERR_OK;
883 if (NULL == (exports = default_exports(csp, "show-status")))
885 return JB_ERR_MEMORY;
888 switch (*(lookup(parameters, "file")))
891 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
893 filename = csp->actions_list[i]->filename;
894 file_description = "Actions File";
899 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
901 filename = csp->rlist[i]->filename;
902 file_description = "Filter File";
910 filename = csp->tlist->filename;
911 file_description = "Trust File";
914 #endif /* def FEATURE_TRUST */
917 if (NULL != filename)
919 if ( map(exports, "file-description", 1, file_description, 1)
920 || map(exports, "filepath", 1, html_encode(filename), 0) )
923 return JB_ERR_MEMORY;
926 if ((fp = fopen(filename, "r")) == NULL)
928 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
931 return JB_ERR_MEMORY;
937 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
939 string_join (&s, html_encode(buf));
943 if (map(exports, "contents", 1, s, 0))
946 return JB_ERR_MEMORY;
950 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
954 for (j = 0; (s != NULL) && (j < Argc); j++)
956 if (!err) err = string_join (&s, html_encode(Argv[j]));
957 if (!err) err = string_append(&s, " ");
959 if (!err) err = map(exports, "invocation", 1, s, 0);
961 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
962 if (!err) err = show_defines(exports);
967 return JB_ERR_MEMORY;
970 #ifdef FEATURE_STATISTICS
971 local_urls_read = urls_read;
972 local_urls_rejected = urls_rejected;
975 * Need to alter the stats not to include the fetch of this
978 * Can't do following thread safely! doh!
981 * urls_rejected--; * This will be incremented subsequently *
984 if (local_urls_read == 0)
986 if (!err) err = map_block_killer(exports, "have-stats");
990 if (!err) err = map_block_killer(exports, "have-no-stats");
992 perc_rej = (float)local_urls_rejected * 100.0F /
993 (float)local_urls_read;
995 sprintf(buf, "%d", local_urls_read);
996 if (!err) err = map(exports, "requests-received", 1, buf, 1);
998 sprintf(buf, "%d", local_urls_rejected);
999 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1001 sprintf(buf, "%6.2f", perc_rej);
1002 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1005 #else /* ndef FEATURE_STATISTICS */
1006 err = err || map_block_killer(exports, "statistics");
1007 #endif /* ndef FEATURE_STATISTICS */
1010 * List all action files in use, together with view and edit links,
1011 * except for standard.action, which should only be viewable. (Not
1012 * enforced in the editor itself)
1013 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1016 for (i = 0; i < MAX_AF_FILES; i++)
1018 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
1020 if (!err) err = string_append(&s, "<tr><td>");
1021 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1022 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1023 if (!err) err = string_append(&s, buf);
1025 #ifdef FEATURE_CGI_EDIT_ACTIONS
1026 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1028 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1029 if (!err) err = string_append(&s, buf);
1033 if (!err) err = string_append(&s, "</td></tr>\n");
1038 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1042 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1046 * List all re_filterfiles in use, together with view options.
1047 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1050 for (i = 0; i < MAX_AF_FILES; i++)
1052 if (((fl = csp->rlist[i]) != NULL) && ((b = fl->f) != NULL))
1054 if (!err) err = string_append(&s, "<tr><td>");
1055 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1056 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1057 if (!err) err = string_append(&s, buf);
1058 if (!err) err = string_append(&s, "</td></tr>\n");
1063 if (!err) err = map(exports, "re-filter-filename", 1, s, 0);
1067 if (!err) err = map(exports, "re-filter-filename", 1, "<tr><td>None specified</td></tr>", 1);
1068 if (!err) err = map_block_killer(exports, "have-filterfile");
1071 #ifdef FEATURE_TRUST
1074 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1078 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1079 if (!err) err = map_block_killer(exports, "have-trustfile");
1082 if (!err) err = map_block_killer(exports, "trust-support");
1083 #endif /* ndef FEATURE_TRUST */
1088 return JB_ERR_MEMORY;
1091 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1095 /*********************************************************************
1097 * Function : cgi_show_url_info
1099 * Description : CGI function that determines and shows which actions
1100 * Privoxy will perform for a given url, and which
1101 * matches starting from the defaults have lead to that.
1104 * 1 : csp = Current client state (buffers, headers, etc...)
1105 * 2 : rsp = http_response data structure for output
1106 * 3 : parameters = map of cgi parameters
1109 * url : The url whose actions are to be determined.
1110 * If url is unset, the url-given conditional will be
1111 * set, so that all but the form can be suppressed in
1114 * Returns : JB_ERR_OK on success
1115 * JB_ERR_MEMORY on out-of-memory error.
1117 *********************************************************************/
1118 jb_err cgi_show_url_info(struct client_state *csp,
1119 struct http_response *rsp,
1120 const struct map *parameters)
1123 struct map *exports;
1130 if (NULL == (exports = default_exports(csp, "show-url-info")))
1132 return JB_ERR_MEMORY;
1136 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1138 url_param = strdup(lookup(parameters, "url"));
1139 if (url_param == NULL)
1142 return JB_ERR_MEMORY;
1147 * Handle prefixes. 4 possibilities:
1148 * 1) "http://" or "https://" prefix present and followed by URL - OK
1149 * 2) Only the "http://" or "https://" part is present, no URL - change
1150 * to empty string so it will be detected later as "no URL".
1151 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1153 * 4) Parameter not specified or is empty string - let this fall through
1154 * for now, next block of code will handle it.
1156 if (0 == strncmp(url_param, "http://", 7))
1158 if (url_param[7] == '\0')
1161 * Empty URL (just prefix).
1162 * Make it totally empty so it's caught by the next if()
1164 url_param[0] = '\0';
1167 else if (0 == strncmp(url_param, "https://", 8))
1169 if (url_param[8] == '\0')
1172 * Empty URL (just prefix).
1173 * Make it totally empty so it's caught by the next if()
1175 url_param[0] = '\0';
1178 else if (url_param[0] != '\0')
1181 * Unknown prefix - assume http://
1183 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1184 if (NULL == url_param_prefixed)
1188 return JB_ERR_MEMORY;
1190 strcpy(url_param_prefixed, "http://");
1191 strcpy(url_param_prefixed + 7, url_param);
1193 url_param = url_param_prefixed;
1197 if (url_param[0] == '\0')
1199 /* URL paramater not specified, display query form only. */
1201 if (map_block_killer(exports, "url-given")
1202 || map(exports, "url", 1, "", 1))
1205 return JB_ERR_MEMORY;
1210 /* Given a URL, so query it. */
1215 struct file_list *fl;
1216 struct url_actions *b;
1217 struct http_request url_to_query[1];
1218 struct current_action_spec action[1];
1221 if (map(exports, "url", 1, html_encode(url_param), 0))
1225 return JB_ERR_MEMORY;
1228 init_current_action(action);
1230 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1232 free_current_action(action);
1235 return JB_ERR_MEMORY;
1238 err = parse_http_url(url_param, url_to_query, csp);
1242 if (err == JB_ERR_MEMORY)
1244 free_http_request(url_to_query);
1245 free_current_action(action);
1247 return JB_ERR_MEMORY;
1253 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1254 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1256 free_current_action(action);
1257 free_http_request(url_to_query);
1262 return JB_ERR_MEMORY;
1265 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1269 * We have a warning about SSL paths. Hide it for insecure sites.
1271 if (!url_to_query->ssl)
1273 if (map_block_killer(exports, "https"))
1275 free_current_action(action);
1277 free_http_request(url_to_query);
1278 return JB_ERR_MEMORY;
1282 matches = strdup("<table class=\"transparent\">");
1284 for (i = 0; i < MAX_AF_FILES; i++)
1286 if (NULL == csp->config->actions_file_short[i]
1287 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1291 if ((fl = csp->actions_list[i]) != NULL)
1293 if ((b = fl->f) != NULL)
1295 /* FIXME: Hardcoded HTML! */
1296 string_append(&matches, "<tr><th>In file: ");
1297 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1298 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1299 string_append(&matches, buf);
1300 string_append(&matches, "View</a>");
1301 #ifdef FEATURE_CGI_EDIT_ACTIONS
1302 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1303 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1304 string_append(&matches, "\">Edit</a>");
1306 string_append(&matches, "</th></tr>\n");
1313 for (; (b != NULL) && (matches != NULL); b = b->next)
1315 if (url_match(b->url, url_to_query))
1317 string_append(&matches, "<tr><td>{");
1318 string_join (&matches, actions_to_html(csp, b->action));
1319 string_append(&matches, " }</b><br>\n<code>");
1320 string_join (&matches, html_encode(b->url->spec));
1321 string_append(&matches, "</code></td></tr>\n");
1323 if (merge_current_action(action, b->action))
1326 free_http_request(url_to_query);
1327 free_current_action(action);
1329 return JB_ERR_MEMORY;
1337 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1340 string_append(&matches, "</table>\n");
1342 free_http_request(url_to_query);
1344 if (matches == NULL)
1346 free_current_action(action);
1348 return JB_ERR_MEMORY;
1351 if (map(exports, "matches", 1, matches , 0))
1353 free_current_action(action);
1355 return JB_ERR_MEMORY;
1358 s = current_action_to_html(csp, action);
1360 free_current_action(action);
1362 if (map(exports, "final", 1, s, 0))
1365 return JB_ERR_MEMORY;
1369 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1373 /*********************************************************************
1375 * Function : cgi_robots_txt
1377 * Description : CGI function to return "/robots.txt".
1380 * 1 : csp = Current client state (buffers, headers, etc...)
1381 * 2 : rsp = http_response data structure for output
1382 * 3 : parameters = map of cgi parameters
1384 * CGI Parameters : None
1386 * Returns : JB_ERR_OK on success
1387 * JB_ERR_MEMORY on out-of-memory error.
1389 *********************************************************************/
1390 jb_err cgi_robots_txt(struct client_state *csp,
1391 struct http_response *rsp,
1392 const struct map *parameters)
1398 "# This is the Privoxy control interface.\n"
1399 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1405 if (rsp->body == NULL)
1407 return JB_ERR_MEMORY;
1410 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1414 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1415 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1417 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1421 /*********************************************************************
1423 * Function : show_defines
1425 * Description : Add to a map the state od all conditional #defines
1426 * used when building
1429 * 1 : exports = map to extend
1431 * Returns : JB_ERR_OK on success
1432 * JB_ERR_MEMORY on out-of-memory error.
1434 *********************************************************************/
1435 static jb_err show_defines(struct map *exports)
1437 jb_err err = JB_ERR_OK;
1440 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1441 #else /* ifndef FEATURE_ACL */
1442 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1443 #endif /* ndef FEATURE_ACL */
1445 #ifdef FEATURE_CGI_EDIT_ACTIONS
1446 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1447 #else /* ifndef FEATURE_COOKIE_JAR */
1448 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1449 #endif /* ndef FEATURE_COOKIE_JAR */
1451 #ifdef FEATURE_COOKIE_JAR
1452 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1453 #else /* ifndef FEATURE_COOKIE_JAR */
1454 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1455 #endif /* ndef FEATURE_COOKIE_JAR */
1457 #ifdef FEATURE_FAST_REDIRECTS
1458 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1459 #else /* ifndef FEATURE_FAST_REDIRECTS */
1460 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1461 #endif /* ndef FEATURE_FAST_REDIRECTS */
1463 #ifdef FEATURE_FORCE_LOAD
1464 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1465 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1466 #else /* ifndef FEATURE_FORCE_LOAD */
1467 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1468 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1469 #endif /* ndef FEATURE_FORCE_LOAD */
1471 #ifdef FEATURE_IMAGE_BLOCKING
1472 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1473 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1474 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1475 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1477 #ifdef FEATURE_IMAGE_DETECT_MSIE
1478 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1479 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1480 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1481 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1483 #ifdef FEATURE_KILL_POPUPS
1484 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1485 #else /* ifndef FEATURE_KILL_POPUPS */
1486 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1487 #endif /* ndef FEATURE_KILL_POPUPS */
1489 #ifdef FEATURE_NO_GIFS
1490 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1491 #else /* ifndef FEATURE_NO_GIFS */
1492 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1493 #endif /* ndef FEATURE_NO_GIFS */
1495 #ifdef FEATURE_PTHREAD
1496 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1497 #else /* ifndef FEATURE_PTHREAD */
1498 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1499 #endif /* ndef FEATURE_PTHREAD */
1501 #ifdef FEATURE_STATISTICS
1502 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1503 #else /* ifndef FEATURE_STATISTICS */
1504 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1505 #endif /* ndef FEATURE_STATISTICS */
1507 #ifdef FEATURE_TOGGLE
1508 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1509 #else /* ifndef FEATURE_TOGGLE */
1510 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1511 #endif /* ndef FEATURE_TOGGLE */
1513 #ifdef FEATURE_TRUST
1514 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1515 #else /* ifndef FEATURE_TRUST */
1516 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1517 #endif /* ndef FEATURE_TRUST */
1520 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1521 #else /* ifndef STATIC_PCRE */
1522 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1523 #endif /* ndef STATIC_PCRE */
1526 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1527 #else /* ifndef STATIC_PCRS */
1528 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1529 #endif /* ndef STATIC_PCRS */
1535 /*********************************************************************
1537 * Function : show_rcs
1539 * Description : Create a string with the rcs info for all sourcefiles
1543 * Returns : A string, or NULL on out-of-memory.
1545 *********************************************************************/
1546 static char *show_rcs(void)
1548 char *result = strdup("");
1549 char buf[BUFFER_SIZE];
1551 /* Instead of including *all* dot h's in the project (thus creating a
1552 * tremendous amount of dependencies), I will concede to declaring them
1553 * as extern's. This forces the developer to add to this list, but oh well.
1556 #define SHOW_RCS(__x) \
1558 extern const char __x[]; \
1559 sprintf(buf, "%s\n", __x); \
1560 string_append(&result, buf); \
1563 /* In alphabetical order */
1564 SHOW_RCS(actions_h_rcs)
1565 SHOW_RCS(actions_rcs)
1567 SHOW_RCS(amiga_h_rcs)
1569 #endif /* def AMIGA */
1572 #ifdef FEATURE_CGI_EDIT_ACTIONS
1573 SHOW_RCS(cgiedit_h_rcs)
1574 SHOW_RCS(cgiedit_rcs)
1575 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1576 SHOW_RCS(cgisimple_h_rcs)
1577 SHOW_RCS(cgisimple_rcs)
1579 SHOW_RCS(cygwin_h_rcs)
1581 SHOW_RCS(deanimate_h_rcs)
1582 SHOW_RCS(deanimate_rcs)
1583 SHOW_RCS(encode_h_rcs)
1584 SHOW_RCS(encode_rcs)
1585 SHOW_RCS(errlog_h_rcs)
1586 SHOW_RCS(errlog_rcs)
1587 SHOW_RCS(filters_h_rcs)
1588 SHOW_RCS(filters_rcs)
1589 SHOW_RCS(gateway_h_rcs)
1590 SHOW_RCS(gateway_rcs)
1591 SHOW_RCS(jbsockets_h_rcs)
1592 SHOW_RCS(jbsockets_rcs)
1595 #ifdef FEATURE_KILL_POPUPS
1596 SHOW_RCS(killpopup_h_rcs)
1597 SHOW_RCS(killpopup_rcs)
1598 #endif /* def FEATURE_KILL_POPUPS */
1599 SHOW_RCS(list_h_rcs)
1601 SHOW_RCS(loadcfg_h_rcs)
1602 SHOW_RCS(loadcfg_rcs)
1603 SHOW_RCS(loaders_h_rcs)
1604 SHOW_RCS(loaders_rcs)
1605 SHOW_RCS(miscutil_h_rcs)
1606 SHOW_RCS(miscutil_rcs)
1607 SHOW_RCS(parsers_h_rcs)
1608 SHOW_RCS(parsers_rcs)
1610 SHOW_RCS(pcrs_h_rcs)
1611 SHOW_RCS(project_h_rcs)
1612 SHOW_RCS(ssplit_h_rcs)
1613 SHOW_RCS(ssplit_rcs)
1614 SHOW_RCS(urlmatch_h_rcs)
1615 SHOW_RCS(urlmatch_rcs)
1617 #ifndef _WIN_CONSOLE
1618 SHOW_RCS(w32log_h_rcs)
1619 SHOW_RCS(w32log_rcs)
1620 SHOW_RCS(w32res_h_rcs)
1621 SHOW_RCS(w32taskbar_h_rcs)
1622 SHOW_RCS(w32taskbar_rcs)
1623 #endif /* ndef _WIN_CONSOLE */
1624 SHOW_RCS(win32_h_rcs)
1626 #endif /* def _WIN32 */