1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.40 2006/09/09 13:05:33 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.40 2006/09/09 13:05:33 fabiankeil
40 * Modified cgi_send_user_manual to serve binary
41 * content without destroying it first. Should also be
42 * faster now. Added ".jpg" check for Content-Type guessing.
44 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
45 * Deliver documents in the user-manual directory
46 * with "Content-Type text/css" if their filename
49 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
50 * Incorporate modified version of Roland Rosenfeld's patch to
51 * optionally access the user-manual via Privoxy. Closes patch 679075.
53 * Formatting changed to Privoxy style, added call to
54 * cgi_error_no_template if the requested file doesn't
55 * exist and modified check whether or not Privoxy itself
56 * should serve the manual. Should work cross-platform now.
58 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
59 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
60 * with what was really the latest development (the v_3_0_branch branch)
62 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
63 * Multiple filter file support
65 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
66 * Undo some damaging memory leak patches
68 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
69 * A few memory leaks plugged (mostly on error paths)
71 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
72 * Another instance of:
73 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
74 * Thanks to Magnus Holmgren for the patch
76 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
77 * - Prevent line wrap beween "View/Edit" link buttons on status page
78 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
80 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
81 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
83 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
84 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
86 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
87 * Applying patch from Andreas as provided by Hal on the list.
88 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
90 * Revision 1.35 2002/05/12 21:44:44 jongfoster
91 * Adding amiga.[ch] revision information, if on an amiga.
93 * Revision 1.34 2002/04/30 12:06:12 oes
94 * Deleted unused code from default_cgi
96 * Revision 1.33 2002/04/30 11:14:52 oes
97 * Made csp the first parameter in *action_to_html
99 * Revision 1.32 2002/04/26 18:29:13 jongfoster
100 * Fixing this Visual C++ warning:
101 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
103 * Revision 1.31 2002/04/26 12:54:36 oes
104 * - Kill obsolete REDIRECT_URL code
105 * - Error handling fixes
106 * - Style sheet related HTML snipplet changes
107 * - cgi_show_url_info:
108 * - Matches now in table, actions on single lines,
110 * - standard.action suppressed
111 * - Buttons to View and Edit AFs
113 * Revision 1.30 2002/04/24 02:18:08 oes
114 * - show-status is now the starting point for editing
115 * the actions files, generate list of all AFs with buttons
116 * for viewing and editing, new look for file list (Jon:
117 * buttons now aligned ;-P ), view mode now supports multiple
118 * AFs, name changes, no view links for unspecified files,
119 * no edit link for standard.action.
121 * - Jon's multiple AF patch: cgi_show_url_info now uses all
122 * AFs and marks the output accordingly
124 * Revision 1.29 2002/04/10 13:38:35 oes
125 * load_template signature changed
127 * Revision 1.28 2002/04/07 15:42:12 jongfoster
128 * Fixing send-banner?type=auto when the image-blocker is
129 * a redirect to send-banner
131 * Revision 1.27 2002/04/05 15:50:48 oes
132 * added send-stylesheet CGI
134 * Revision 1.26 2002/04/04 00:36:36 gliptak
135 * always use pcre for matching
137 * Revision 1.25 2002/04/03 22:28:03 gliptak
138 * Removed references to gnu_regex
140 * Revision 1.24 2002/04/02 16:12:47 oes
141 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
143 * Revision 1.23 2002/03/26 22:29:54 swa
144 * we have a new homepage!
146 * Revision 1.22 2002/03/24 16:18:15 jongfoster
149 * Revision 1.21 2002/03/24 15:23:33 jongfoster
152 * Revision 1.20 2002/03/24 13:25:43 swa
153 * name change related issues
155 * Revision 1.19 2002/03/16 23:54:06 jongfoster
156 * Adding graceful termination feature, to help look for memory leaks.
157 * If you enable this (which, by design, has to be done by hand
158 * editing config.h) and then go to http://i.j.b/die, then the program
159 * will exit cleanly after the *next* request. It should free all the
160 * memory that was used.
162 * Revision 1.18 2002/03/12 01:44:49 oes
163 * Changed default for "blocked" image from jb logo to checkboard pattern
165 * Revision 1.17 2002/03/08 16:43:18 oes
166 * Added choice beween GIF and PNG built-in images
168 * Revision 1.16 2002/03/07 03:48:38 oes
169 * - Changed built-in images from GIF to PNG
170 * (with regard to Unisys patent issue)
171 * - Added a 4x4 pattern PNG which is less intrusive
172 * than the logo but also clearly marks the deleted banners
174 * Revision 1.15 2002/03/06 22:54:35 jongfoster
175 * Automated function-comment nitpicking.
177 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
178 * Clean up a little CRLF unpleasantness that suddenly appeared
180 * Revision 1.13 2002/02/21 00:10:37 jongfoster
181 * Adding send-banner?type=auto option
183 * Revision 1.12 2002/01/23 01:03:32 jongfoster
184 * Fixing gcc [CygWin] compiler warnings
186 * Revision 1.11 2002/01/23 00:01:04 jongfoster
187 * Adding cgi_transparent_gif() for http://i.j.b/t
188 * Adding missing html_encode() to many CGI functions.
189 * Adding urlmatch.[ch] to http://i.j.b/show-version
191 * Revision 1.10 2002/01/17 21:10:37 jongfoster
192 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
193 * Also fixing a problem in the same function with improperly quoted URLs
194 * in output HTML, and adding code to handle https:// URLs correctly.
196 * Revision 1.9 2001/11/30 23:09:15 jongfoster
197 * Now reports on FEATURE_CGI_EDIT_ACTIONS
198 * Removing FEATURE_DENY_GZIP from template
200 * Revision 1.8 2001/11/13 00:14:07 jongfoster
201 * Fixing stupid bug now I've figured out what || means.
202 * (It always returns 0 or 1, not one of it's paramaters.)
204 * Revision 1.7 2001/10/23 21:48:19 jongfoster
205 * Cleaning up error handling in CGI functions - they now send back
206 * a HTML error page and should never cause a FATAL error. (Fixes one
207 * potential source of "denial of service" attacks).
209 * CGI actions file editor that works and is actually useful.
211 * Ability to toggle JunkBuster remotely using a CGI call.
213 * You can turn off both the above features in the main configuration
214 * file, e.g. if you are running a multi-user proxy.
216 * Revision 1.6 2001/10/14 22:00:32 jongfoster
217 * Adding support for a 404 error when an invalid CGI page is requested.
219 * Revision 1.5 2001/10/07 15:30:41 oes
220 * Removed FEATURE_DENY_GZIP
222 * Revision 1.4 2001/10/02 15:31:12 oes
223 * Introduced show-request cgi
225 * Revision 1.3 2001/09/22 16:34:44 jongfoster
226 * Removing unneeded #includes
228 * Revision 1.2 2001/09/19 18:01:11 oes
229 * Fixed comments; cosmetics
231 * Revision 1.1 2001/09/16 17:08:54 jongfoster
232 * Moving simple CGI functions from cgi.c to new file cgisimple.c
235 **********************************************************************/
241 #include <sys/types.h>
248 #define snprintf _snprintf
249 #endif /* def _WIN32 */
253 #include "cgisimple.h"
259 #include "miscutil.h"
262 #include "urlmatch.h"
265 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
268 static char *show_rcs(void);
269 static jb_err show_defines(struct map *exports);
272 /*********************************************************************
274 * Function : cgi_default
276 * Description : CGI function that is called for the CGI_SITE_1_HOST
277 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
278 * Boring - only exports the default exports.
281 * 1 : csp = Current client state (buffers, headers, etc...)
282 * 2 : rsp = http_response data structure for output
283 * 3 : parameters = map of cgi parameters
285 * CGI Parameters : none
287 * Returns : JB_ERR_OK on success
288 * JB_ERR_MEMORY on out-of-memory
290 *********************************************************************/
291 jb_err cgi_default(struct client_state *csp,
292 struct http_response *rsp,
293 const struct map *parameters)
300 if (NULL == (exports = default_exports(csp, "")))
302 return JB_ERR_MEMORY;
305 return template_fill_for_cgi(csp, "default", exports, rsp);
309 /*********************************************************************
311 * Function : cgi_error_404
313 * Description : CGI function that is called if an unknown action was
317 * 1 : csp = Current client state (buffers, headers, etc...)
318 * 2 : rsp = http_response data structure for output
319 * 3 : parameters = map of cgi parameters
321 * CGI Parameters : none
323 * Returns : JB_ERR_OK on success
324 * JB_ERR_MEMORY on out-of-memory error.
326 *********************************************************************/
327 jb_err cgi_error_404(struct client_state *csp,
328 struct http_response *rsp,
329 const struct map *parameters)
337 if (NULL == (exports = default_exports(csp, NULL)))
339 return JB_ERR_MEMORY;
342 rsp->status = strdup("404 Privoxy configuration page not found");
343 if (rsp->status == NULL)
346 return JB_ERR_MEMORY;
349 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
353 #ifdef FEATURE_GRACEFUL_TERMINATION
354 /*********************************************************************
358 * Description : CGI function to shut down Privoxy.
359 * NOTE: Turning this on in a production build
360 * would be a BAD idea. An EXTREMELY BAD idea.
361 * In short, don't do it.
364 * 1 : csp = Current client state (buffers, headers, etc...)
365 * 2 : rsp = http_response data structure for output
366 * 3 : parameters = map of cgi parameters
368 * CGI Parameters : none
370 * Returns : JB_ERR_OK on success
371 * JB_ERR_MEMORY on out-of-memory error.
373 *********************************************************************/
374 jb_err cgi_die (struct client_state *csp,
375 struct http_response *rsp,
376 const struct map *parameters)
386 * I don't really care what gets sent back to the browser.
387 * Take the easy option - "out of memory" page.
390 return JB_ERR_MEMORY;
392 #endif /* def FEATURE_GRACEFUL_TERMINATION */
395 /*********************************************************************
397 * Function : cgi_show_request
399 * Description : Show the client's request and what sed() would have
403 * 1 : csp = Current client state (buffers, headers, etc...)
404 * 2 : rsp = http_response data structure for output
405 * 3 : parameters = map of cgi parameters
407 * CGI Parameters : none
409 * Returns : JB_ERR_OK on success
410 * JB_ERR_MEMORY on out-of-memory error.
412 *********************************************************************/
413 jb_err cgi_show_request(struct client_state *csp,
414 struct http_response *rsp,
415 const struct map *parameters)
424 if (NULL == (exports = default_exports(csp, "show-request")))
426 return JB_ERR_MEMORY;
430 * Repair the damage done to the IOB by get_header()
432 for (p = csp->iob->buf; p < csp->iob->eod; p++)
434 if (*p == '\0') *p = '\n';
438 * Export the original client's request and the one we would
439 * be sending to the server if this wasn't a CGI call
442 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
445 return JB_ERR_MEMORY;
448 if (map(exports, "processed-request", 1, html_encode_and_free_original(
449 sed(client_patterns, add_client_headers, csp)), 0))
452 return JB_ERR_MEMORY;
455 return template_fill_for_cgi(csp, "show-request", exports, rsp);
459 /*********************************************************************
461 * Function : cgi_send_banner
463 * Description : CGI function that returns a banner.
466 * 1 : csp = Current client state (buffers, headers, etc...)
467 * 2 : rsp = http_response data structure for output
468 * 3 : parameters = map of cgi parameters
471 * type : Selects the type of banner between "trans", "logo",
472 * and "auto". Defaults to "logo" if absent or invalid.
473 * "auto" means to select as if we were image-blocking.
474 * (Only the first character really counts; b and t are
477 * Returns : JB_ERR_OK on success
478 * JB_ERR_MEMORY on out-of-memory error.
480 *********************************************************************/
481 jb_err cgi_send_banner(struct client_state *csp,
482 struct http_response *rsp,
483 const struct map *parameters)
485 char imagetype = lookup(parameters, "type")[0];
488 * If type is auto, then determine the right thing
489 * to do from the set-image-blocker action
491 if (imagetype == 'a')
498 #ifdef FEATURE_IMAGE_BLOCKING
499 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
501 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
502 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
503 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
507 /* Use default - nothing to do here. */
509 else if (0 == strcmpic(p, "blank"))
513 else if (0 == strcmpic(p, "pattern"))
519 * If the action is to call this CGI, determine
522 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
524 imagetype = p[sizeof(prefix1) - 1];
526 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
528 imagetype = p[sizeof(prefix2) - 1];
532 * Everything else must (should) be a URL to
540 #endif /* def FEATURE_IMAGE_BLOCKING */
544 * Now imagetype is either the non-auto type we were called with,
545 * or it was auto and has since been determined. In any case, we
546 * can proceed to actually answering the request by sending a redirect
547 * or an image as appropriate:
549 if (imagetype == 'r')
551 rsp->status = strdup("302 Local Redirect from Privoxy");
552 if (rsp->status == NULL)
554 return JB_ERR_MEMORY;
556 if (enlist_unique_header(rsp->headers, "Location",
557 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
559 return JB_ERR_MEMORY;
564 if ((imagetype == 'b') || (imagetype == 't'))
566 rsp->body = bindup(image_blank_data, image_blank_length);
567 rsp->content_length = image_blank_length;
571 rsp->body = bindup(image_pattern_data, image_pattern_length);
572 rsp->content_length = image_pattern_length;
575 if (rsp->body == NULL)
577 return JB_ERR_MEMORY;
579 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
581 return JB_ERR_MEMORY;
592 /*********************************************************************
594 * Function : cgi_transparent_image
596 * Description : CGI function that sends a 1x1 transparent image.
599 * 1 : csp = Current client state (buffers, headers, etc...)
600 * 2 : rsp = http_response data structure for output
601 * 3 : parameters = map of cgi parameters
603 * CGI Parameters : None
605 * Returns : JB_ERR_OK on success
606 * JB_ERR_MEMORY on out-of-memory error.
608 *********************************************************************/
609 jb_err cgi_transparent_image(struct client_state *csp,
610 struct http_response *rsp,
611 const struct map *parameters)
613 rsp->body = bindup(image_blank_data, image_blank_length);
614 rsp->content_length = image_blank_length;
616 if (rsp->body == NULL)
618 return JB_ERR_MEMORY;
621 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
623 return JB_ERR_MEMORY;
633 /*********************************************************************
635 * Function : cgi_send_stylesheet
637 * Description : CGI function that sends a css stylesheet found
638 * in the cgi-style.css template
641 * 1 : csp = Current client state (buffers, headers, etc...)
642 * 2 : rsp = http_response data structure for output
643 * 3 : parameters = map of cgi parameters
645 * CGI Parameters : None
647 * Returns : JB_ERR_OK on success
648 * JB_ERR_MEMORY on out-of-memory error.
650 *********************************************************************/
651 jb_err cgi_send_stylesheet(struct client_state *csp,
652 struct http_response *rsp,
653 const struct map *parameters)
660 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
662 if (err == JB_ERR_FILE)
665 * No way to tell user; send empty stylesheet
667 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
671 return err; /* JB_ERR_MEMORY */
674 if (enlist(rsp->headers, "Content-Type: text/css"))
676 return JB_ERR_MEMORY;
682 /*********************************************************************
684 * Function : cgi_send_user_manual
686 * Description : CGI function that sends a file in the user
690 * 1 : csp = Current client state (buffers, headers, etc...)
691 * 2 : rsp = http_response data structure for output
692 * 3 : parameters = map of cgi parameters
694 * CGI Parameters : file=name.html, the name of the HTML file
695 * (relative to user-manual from config)
697 * Returns : JB_ERR_OK on success
698 * JB_ERR_MEMORY on out-of-memory error.
700 *********************************************************************/
701 jb_err cgi_send_user_manual(struct client_state *csp,
702 struct http_response *rsp,
703 const struct map *parameters)
705 const char * filename;
708 jb_err err = JB_ERR_OK;
715 if (!parameters->first)
717 /* requested http://p.p/user-manual (without trailing slash) */
718 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
721 get_string_param(parameters, "file", &filename);
722 /* Check paramter for hack attempts */
723 if (filename && strchr(filename, '/'))
725 return JB_ERR_CGI_PARAMS;
727 if (filename && strstr(filename, ".."))
729 return JB_ERR_CGI_PARAMS;
732 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
733 if (full_path == NULL)
735 return JB_ERR_MEMORY;
738 /* Open user-manual file */
739 if (NULL == (fp = fopen(full_path, "r")))
741 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
742 err = cgi_error_no_template(csp, rsp, full_path);
747 /* Get file length */
748 fseek(fp, 0, SEEK_END);
750 fseek(fp, 0, SEEK_SET);
752 /* Allocate memory and load the file directly into the body */
753 rsp->body = (char *)malloc(length+1);
758 return JB_ERR_MEMORY;
760 if (!fread(rsp->body, length, 1, fp))
763 * Why should this happen? If it does, we just log
764 * it and serve what we got, most likely padded with garbage.
766 log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
771 /* Privoxy only gets it right for non-binary content. */
772 rsp->content_length = (int)length;
774 /* Guess correct Content-Type based on the filename's ending */
777 length = strlen(filename);
783 if((length>=4) && !strcmp(&filename[length-4], ".css"))
785 err = enlist(rsp->headers, "Content-Type: text/css");
787 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
789 err = enlist(rsp->headers, "Content-Type: image/jpeg");
793 err = enlist(rsp->headers, "Content-Type: text/html");
800 /*********************************************************************
802 * Function : cgi_show_version
804 * Description : CGI function that returns a a web page describing the
805 * file versions of Privoxy.
808 * 1 : csp = Current client state (buffers, headers, etc...)
809 * 2 : rsp = http_response data structure for output
810 * 3 : parameters = map of cgi parameters
812 * CGI Parameters : none
814 * Returns : JB_ERR_OK on success
815 * JB_ERR_MEMORY on out-of-memory error.
817 *********************************************************************/
818 jb_err cgi_show_version(struct client_state *csp,
819 struct http_response *rsp,
820 const struct map *parameters)
828 if (NULL == (exports = default_exports(csp, "show-version")))
830 return JB_ERR_MEMORY;
833 if (map(exports, "sourceversions", 1, show_rcs(), 0))
836 return JB_ERR_MEMORY;
839 return template_fill_for_cgi(csp, "show-version", exports, rsp);
843 /*********************************************************************
845 * Function : cgi_show_status
847 * Description : CGI function that returns a a web page describing the
848 * current status of Privoxy.
851 * 1 : csp = Current client state (buffers, headers, etc...)
852 * 2 : rsp = http_response data structure for output
853 * 3 : parameters = map of cgi parameters
856 * file : Which file to show. Only first letter is checked,
858 * - "p"ermissions (actions) file
861 * Default is to show menu and other information.
863 * Returns : JB_ERR_OK on success
864 * JB_ERR_MEMORY on out-of-memory error.
866 *********************************************************************/
867 jb_err cgi_show_status(struct client_state *csp,
868 struct http_response *rsp,
869 const struct map *parameters)
876 char buf[BUFFER_SIZE];
877 const char * filename = NULL;
878 char * file_description = NULL;
879 #ifdef FEATURE_STATISTICS
880 float perc_rej; /* Percentage of http requests rejected */
882 int local_urls_rejected;
883 #endif /* ndef FEATURE_STATISTICS */
884 struct file_list * fl;
885 struct url_actions * b;
886 jb_err err = JB_ERR_OK;
894 if (NULL == (exports = default_exports(csp, "show-status")))
896 return JB_ERR_MEMORY;
899 switch (*(lookup(parameters, "file")))
902 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
904 filename = csp->actions_list[i]->filename;
905 file_description = "Actions File";
910 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
912 filename = csp->rlist[i]->filename;
913 file_description = "Filter File";
921 filename = csp->tlist->filename;
922 file_description = "Trust File";
925 #endif /* def FEATURE_TRUST */
928 if (NULL != filename)
930 if ( map(exports, "file-description", 1, file_description, 1)
931 || map(exports, "filepath", 1, html_encode(filename), 0) )
934 return JB_ERR_MEMORY;
937 if ((fp = fopen(filename, "r")) == NULL)
939 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
942 return JB_ERR_MEMORY;
948 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
950 string_join (&s, html_encode(buf));
954 if (map(exports, "contents", 1, s, 0))
957 return JB_ERR_MEMORY;
961 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
965 for (j = 0; (s != NULL) && (j < Argc); j++)
967 if (!err) err = string_join (&s, html_encode(Argv[j]));
968 if (!err) err = string_append(&s, " ");
970 if (!err) err = map(exports, "invocation", 1, s, 0);
972 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
973 if (!err) err = show_defines(exports);
978 return JB_ERR_MEMORY;
981 #ifdef FEATURE_STATISTICS
982 local_urls_read = urls_read;
983 local_urls_rejected = urls_rejected;
986 * Need to alter the stats not to include the fetch of this
989 * Can't do following thread safely! doh!
992 * urls_rejected--; * This will be incremented subsequently *
995 if (local_urls_read == 0)
997 if (!err) err = map_block_killer(exports, "have-stats");
1001 if (!err) err = map_block_killer(exports, "have-no-stats");
1003 perc_rej = (float)local_urls_rejected * 100.0F /
1004 (float)local_urls_read;
1006 sprintf(buf, "%d", local_urls_read);
1007 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1009 sprintf(buf, "%d", local_urls_rejected);
1010 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1012 sprintf(buf, "%6.2f", perc_rej);
1013 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1016 #else /* ndef FEATURE_STATISTICS */
1017 err = err || map_block_killer(exports, "statistics");
1018 #endif /* ndef FEATURE_STATISTICS */
1021 * List all action files in use, together with view and edit links,
1022 * except for standard.action, which should only be viewable. (Not
1023 * enforced in the editor itself)
1024 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1027 for (i = 0; i < MAX_AF_FILES; i++)
1029 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
1031 if (!err) err = string_append(&s, "<tr><td>");
1032 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1033 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1034 if (!err) err = string_append(&s, buf);
1036 #ifdef FEATURE_CGI_EDIT_ACTIONS
1037 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1039 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1040 if (!err) err = string_append(&s, buf);
1044 if (!err) err = string_append(&s, "</td></tr>\n");
1049 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1053 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1057 * List all re_filterfiles in use, together with view options.
1058 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1061 for (i = 0; i < MAX_AF_FILES; i++)
1063 if (((fl = csp->rlist[i]) != NULL) && ((b = fl->f) != NULL))
1065 if (!err) err = string_append(&s, "<tr><td>");
1066 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1067 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1068 if (!err) err = string_append(&s, buf);
1069 if (!err) err = string_append(&s, "</td></tr>\n");
1074 if (!err) err = map(exports, "re-filter-filename", 1, s, 0);
1078 if (!err) err = map(exports, "re-filter-filename", 1, "<tr><td>None specified</td></tr>", 1);
1079 if (!err) err = map_block_killer(exports, "have-filterfile");
1082 #ifdef FEATURE_TRUST
1085 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1089 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1090 if (!err) err = map_block_killer(exports, "have-trustfile");
1093 if (!err) err = map_block_killer(exports, "trust-support");
1094 #endif /* ndef FEATURE_TRUST */
1099 return JB_ERR_MEMORY;
1102 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1106 /*********************************************************************
1108 * Function : cgi_show_url_info
1110 * Description : CGI function that determines and shows which actions
1111 * Privoxy will perform for a given url, and which
1112 * matches starting from the defaults have lead to that.
1115 * 1 : csp = Current client state (buffers, headers, etc...)
1116 * 2 : rsp = http_response data structure for output
1117 * 3 : parameters = map of cgi parameters
1120 * url : The url whose actions are to be determined.
1121 * If url is unset, the url-given conditional will be
1122 * set, so that all but the form can be suppressed in
1125 * Returns : JB_ERR_OK on success
1126 * JB_ERR_MEMORY on out-of-memory error.
1128 *********************************************************************/
1129 jb_err cgi_show_url_info(struct client_state *csp,
1130 struct http_response *rsp,
1131 const struct map *parameters)
1134 struct map *exports;
1141 if (NULL == (exports = default_exports(csp, "show-url-info")))
1143 return JB_ERR_MEMORY;
1147 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1149 url_param = strdup(lookup(parameters, "url"));
1150 if (url_param == NULL)
1153 return JB_ERR_MEMORY;
1158 * Handle prefixes. 4 possibilities:
1159 * 1) "http://" or "https://" prefix present and followed by URL - OK
1160 * 2) Only the "http://" or "https://" part is present, no URL - change
1161 * to empty string so it will be detected later as "no URL".
1162 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1164 * 4) Parameter not specified or is empty string - let this fall through
1165 * for now, next block of code will handle it.
1167 if (0 == strncmp(url_param, "http://", 7))
1169 if (url_param[7] == '\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 (0 == strncmp(url_param, "https://", 8))
1180 if (url_param[8] == '\0')
1183 * Empty URL (just prefix).
1184 * Make it totally empty so it's caught by the next if()
1186 url_param[0] = '\0';
1189 else if (url_param[0] != '\0')
1192 * Unknown prefix - assume http://
1194 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1195 if (NULL == url_param_prefixed)
1199 return JB_ERR_MEMORY;
1201 strcpy(url_param_prefixed, "http://");
1202 strcpy(url_param_prefixed + 7, url_param);
1204 url_param = url_param_prefixed;
1208 if (url_param[0] == '\0')
1210 /* URL paramater not specified, display query form only. */
1212 if (map_block_killer(exports, "url-given")
1213 || map(exports, "url", 1, "", 1))
1216 return JB_ERR_MEMORY;
1221 /* Given a URL, so query it. */
1226 struct file_list *fl;
1227 struct url_actions *b;
1228 struct http_request url_to_query[1];
1229 struct current_action_spec action[1];
1232 if (map(exports, "url", 1, html_encode(url_param), 0))
1236 return JB_ERR_MEMORY;
1239 init_current_action(action);
1241 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1243 free_current_action(action);
1246 return JB_ERR_MEMORY;
1249 err = parse_http_url(url_param, url_to_query, csp);
1253 if (err == JB_ERR_MEMORY)
1255 free_http_request(url_to_query);
1256 free_current_action(action);
1258 return JB_ERR_MEMORY;
1264 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1265 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1267 free_current_action(action);
1268 free_http_request(url_to_query);
1273 return JB_ERR_MEMORY;
1276 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1280 * We have a warning about SSL paths. Hide it for insecure sites.
1282 if (!url_to_query->ssl)
1284 if (map_block_killer(exports, "https"))
1286 free_current_action(action);
1288 free_http_request(url_to_query);
1289 return JB_ERR_MEMORY;
1293 matches = strdup("<table class=\"transparent\">");
1295 for (i = 0; i < MAX_AF_FILES; i++)
1297 if (NULL == csp->config->actions_file_short[i]
1298 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1302 if ((fl = csp->actions_list[i]) != NULL)
1304 if ((b = fl->f) != NULL)
1306 /* FIXME: Hardcoded HTML! */
1307 string_append(&matches, "<tr><th>In file: ");
1308 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1309 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1310 string_append(&matches, buf);
1311 string_append(&matches, "View</a>");
1312 #ifdef FEATURE_CGI_EDIT_ACTIONS
1313 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1314 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1315 string_append(&matches, "\">Edit</a>");
1317 string_append(&matches, "</th></tr>\n");
1324 for (; (b != NULL) && (matches != NULL); b = b->next)
1326 if (url_match(b->url, url_to_query))
1328 string_append(&matches, "<tr><td>{");
1329 string_join (&matches, actions_to_html(csp, b->action));
1330 string_append(&matches, " }</b><br>\n<code>");
1331 string_join (&matches, html_encode(b->url->spec));
1332 string_append(&matches, "</code></td></tr>\n");
1334 if (merge_current_action(action, b->action))
1337 free_http_request(url_to_query);
1338 free_current_action(action);
1340 return JB_ERR_MEMORY;
1348 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1351 string_append(&matches, "</table>\n");
1353 free_http_request(url_to_query);
1355 if (matches == NULL)
1357 free_current_action(action);
1359 return JB_ERR_MEMORY;
1362 if (map(exports, "matches", 1, matches , 0))
1364 free_current_action(action);
1366 return JB_ERR_MEMORY;
1369 s = current_action_to_html(csp, action);
1371 free_current_action(action);
1373 if (map(exports, "final", 1, s, 0))
1376 return JB_ERR_MEMORY;
1380 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1384 /*********************************************************************
1386 * Function : cgi_robots_txt
1388 * Description : CGI function to return "/robots.txt".
1391 * 1 : csp = Current client state (buffers, headers, etc...)
1392 * 2 : rsp = http_response data structure for output
1393 * 3 : parameters = map of cgi parameters
1395 * CGI Parameters : None
1397 * Returns : JB_ERR_OK on success
1398 * JB_ERR_MEMORY on out-of-memory error.
1400 *********************************************************************/
1401 jb_err cgi_robots_txt(struct client_state *csp,
1402 struct http_response *rsp,
1403 const struct map *parameters)
1409 "# This is the Privoxy control interface.\n"
1410 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1416 if (rsp->body == NULL)
1418 return JB_ERR_MEMORY;
1421 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1425 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1426 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1428 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1432 /*********************************************************************
1434 * Function : show_defines
1436 * Description : Add to a map the state od all conditional #defines
1437 * used when building
1440 * 1 : exports = map to extend
1442 * Returns : JB_ERR_OK on success
1443 * JB_ERR_MEMORY on out-of-memory error.
1445 *********************************************************************/
1446 static jb_err show_defines(struct map *exports)
1448 jb_err err = JB_ERR_OK;
1451 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1452 #else /* ifndef FEATURE_ACL */
1453 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1454 #endif /* ndef FEATURE_ACL */
1456 #ifdef FEATURE_CGI_EDIT_ACTIONS
1457 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1458 #else /* ifndef FEATURE_COOKIE_JAR */
1459 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1460 #endif /* ndef FEATURE_COOKIE_JAR */
1462 #ifdef FEATURE_COOKIE_JAR
1463 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1464 #else /* ifndef FEATURE_COOKIE_JAR */
1465 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1466 #endif /* ndef FEATURE_COOKIE_JAR */
1468 #ifdef FEATURE_FAST_REDIRECTS
1469 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1470 #else /* ifndef FEATURE_FAST_REDIRECTS */
1471 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1472 #endif /* ndef FEATURE_FAST_REDIRECTS */
1474 #ifdef FEATURE_FORCE_LOAD
1475 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1476 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1477 #else /* ifndef FEATURE_FORCE_LOAD */
1478 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1479 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1480 #endif /* ndef FEATURE_FORCE_LOAD */
1482 #ifdef FEATURE_IMAGE_BLOCKING
1483 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1484 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1485 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1486 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1488 #ifdef FEATURE_IMAGE_DETECT_MSIE
1489 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1490 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1491 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1492 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1494 #ifdef FEATURE_KILL_POPUPS
1495 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1496 #else /* ifndef FEATURE_KILL_POPUPS */
1497 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1498 #endif /* ndef FEATURE_KILL_POPUPS */
1500 #ifdef FEATURE_NO_GIFS
1501 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1502 #else /* ifndef FEATURE_NO_GIFS */
1503 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1504 #endif /* ndef FEATURE_NO_GIFS */
1506 #ifdef FEATURE_PTHREAD
1507 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1508 #else /* ifndef FEATURE_PTHREAD */
1509 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1510 #endif /* ndef FEATURE_PTHREAD */
1512 #ifdef FEATURE_STATISTICS
1513 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1514 #else /* ifndef FEATURE_STATISTICS */
1515 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1516 #endif /* ndef FEATURE_STATISTICS */
1518 #ifdef FEATURE_TOGGLE
1519 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1520 #else /* ifndef FEATURE_TOGGLE */
1521 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1522 #endif /* ndef FEATURE_TOGGLE */
1524 #ifdef FEATURE_TRUST
1525 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1526 #else /* ifndef FEATURE_TRUST */
1527 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1528 #endif /* ndef FEATURE_TRUST */
1531 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1532 #else /* ifndef STATIC_PCRE */
1533 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1534 #endif /* ndef STATIC_PCRE */
1537 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1538 #else /* ifndef STATIC_PCRS */
1539 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1540 #endif /* ndef STATIC_PCRS */
1546 /*********************************************************************
1548 * Function : show_rcs
1550 * Description : Create a string with the rcs info for all sourcefiles
1554 * Returns : A string, or NULL on out-of-memory.
1556 *********************************************************************/
1557 static char *show_rcs(void)
1559 char *result = strdup("");
1560 char buf[BUFFER_SIZE];
1562 /* Instead of including *all* dot h's in the project (thus creating a
1563 * tremendous amount of dependencies), I will concede to declaring them
1564 * as extern's. This forces the developer to add to this list, but oh well.
1567 #define SHOW_RCS(__x) \
1569 extern const char __x[]; \
1570 sprintf(buf, "%s\n", __x); \
1571 string_append(&result, buf); \
1574 /* In alphabetical order */
1575 SHOW_RCS(actions_h_rcs)
1576 SHOW_RCS(actions_rcs)
1578 SHOW_RCS(amiga_h_rcs)
1580 #endif /* def AMIGA */
1583 #ifdef FEATURE_CGI_EDIT_ACTIONS
1584 SHOW_RCS(cgiedit_h_rcs)
1585 SHOW_RCS(cgiedit_rcs)
1586 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1587 SHOW_RCS(cgisimple_h_rcs)
1588 SHOW_RCS(cgisimple_rcs)
1590 SHOW_RCS(cygwin_h_rcs)
1592 SHOW_RCS(deanimate_h_rcs)
1593 SHOW_RCS(deanimate_rcs)
1594 SHOW_RCS(encode_h_rcs)
1595 SHOW_RCS(encode_rcs)
1596 SHOW_RCS(errlog_h_rcs)
1597 SHOW_RCS(errlog_rcs)
1598 SHOW_RCS(filters_h_rcs)
1599 SHOW_RCS(filters_rcs)
1600 SHOW_RCS(gateway_h_rcs)
1601 SHOW_RCS(gateway_rcs)
1602 SHOW_RCS(jbsockets_h_rcs)
1603 SHOW_RCS(jbsockets_rcs)
1606 #ifdef FEATURE_KILL_POPUPS
1607 SHOW_RCS(killpopup_h_rcs)
1608 SHOW_RCS(killpopup_rcs)
1609 #endif /* def FEATURE_KILL_POPUPS */
1610 SHOW_RCS(list_h_rcs)
1612 SHOW_RCS(loadcfg_h_rcs)
1613 SHOW_RCS(loadcfg_rcs)
1614 SHOW_RCS(loaders_h_rcs)
1615 SHOW_RCS(loaders_rcs)
1616 SHOW_RCS(miscutil_h_rcs)
1617 SHOW_RCS(miscutil_rcs)
1618 SHOW_RCS(parsers_h_rcs)
1619 SHOW_RCS(parsers_rcs)
1621 SHOW_RCS(pcrs_h_rcs)
1622 SHOW_RCS(project_h_rcs)
1623 SHOW_RCS(ssplit_h_rcs)
1624 SHOW_RCS(ssplit_rcs)
1625 SHOW_RCS(urlmatch_h_rcs)
1626 SHOW_RCS(urlmatch_rcs)
1628 #ifndef _WIN_CONSOLE
1629 SHOW_RCS(w32log_h_rcs)
1630 SHOW_RCS(w32log_rcs)
1631 SHOW_RCS(w32res_h_rcs)
1632 SHOW_RCS(w32taskbar_h_rcs)
1633 SHOW_RCS(w32taskbar_rcs)
1634 #endif /* ndef _WIN_CONSOLE */
1635 SHOW_RCS(win32_h_rcs)
1637 #endif /* def _WIN32 */