1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.37 2006/07/18 14:48:45 david__schmidt 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.37 2006/07/18 14:48:45 david__schmidt
40 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
41 * with what was really the latest development (the v_3_0_branch branch)
43 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
44 * Multiple filter file support
46 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
47 * Undo some damaging memory leak patches
49 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
50 * A few memory leaks plugged (mostly on error paths)
52 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
53 * Another instance of:
54 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
55 * Thanks to Magnus Holmgren for the patch
57 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
58 * - Prevent line wrap beween "View/Edit" link buttons on status page
59 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
61 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
62 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
64 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
65 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
67 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
68 * Applying patch from Andreas as provided by Hal on the list.
69 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
71 * Revision 1.35 2002/05/12 21:44:44 jongfoster
72 * Adding amiga.[ch] revision information, if on an amiga.
74 * Revision 1.34 2002/04/30 12:06:12 oes
75 * Deleted unused code from default_cgi
77 * Revision 1.33 2002/04/30 11:14:52 oes
78 * Made csp the first parameter in *action_to_html
80 * Revision 1.32 2002/04/26 18:29:13 jongfoster
81 * Fixing this Visual C++ warning:
82 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
84 * Revision 1.31 2002/04/26 12:54:36 oes
85 * - Kill obsolete REDIRECT_URL code
86 * - Error handling fixes
87 * - Style sheet related HTML snipplet changes
88 * - cgi_show_url_info:
89 * - Matches now in table, actions on single lines,
91 * - standard.action suppressed
92 * - Buttons to View and Edit AFs
94 * Revision 1.30 2002/04/24 02:18:08 oes
95 * - show-status is now the starting point for editing
96 * the actions files, generate list of all AFs with buttons
97 * for viewing and editing, new look for file list (Jon:
98 * buttons now aligned ;-P ), view mode now supports multiple
99 * AFs, name changes, no view links for unspecified files,
100 * no edit link for standard.action.
102 * - Jon's multiple AF patch: cgi_show_url_info now uses all
103 * AFs and marks the output accordingly
105 * Revision 1.29 2002/04/10 13:38:35 oes
106 * load_template signature changed
108 * Revision 1.28 2002/04/07 15:42:12 jongfoster
109 * Fixing send-banner?type=auto when the image-blocker is
110 * a redirect to send-banner
112 * Revision 1.27 2002/04/05 15:50:48 oes
113 * added send-stylesheet CGI
115 * Revision 1.26 2002/04/04 00:36:36 gliptak
116 * always use pcre for matching
118 * Revision 1.25 2002/04/03 22:28:03 gliptak
119 * Removed references to gnu_regex
121 * Revision 1.24 2002/04/02 16:12:47 oes
122 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
124 * Revision 1.23 2002/03/26 22:29:54 swa
125 * we have a new homepage!
127 * Revision 1.22 2002/03/24 16:18:15 jongfoster
130 * Revision 1.21 2002/03/24 15:23:33 jongfoster
133 * Revision 1.20 2002/03/24 13:25:43 swa
134 * name change related issues
136 * Revision 1.19 2002/03/16 23:54:06 jongfoster
137 * Adding graceful termination feature, to help look for memory leaks.
138 * If you enable this (which, by design, has to be done by hand
139 * editing config.h) and then go to http://i.j.b/die, then the program
140 * will exit cleanly after the *next* request. It should free all the
141 * memory that was used.
143 * Revision 1.18 2002/03/12 01:44:49 oes
144 * Changed default for "blocked" image from jb logo to checkboard pattern
146 * Revision 1.17 2002/03/08 16:43:18 oes
147 * Added choice beween GIF and PNG built-in images
149 * Revision 1.16 2002/03/07 03:48:38 oes
150 * - Changed built-in images from GIF to PNG
151 * (with regard to Unisys patent issue)
152 * - Added a 4x4 pattern PNG which is less intrusive
153 * than the logo but also clearly marks the deleted banners
155 * Revision 1.15 2002/03/06 22:54:35 jongfoster
156 * Automated function-comment nitpicking.
158 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
159 * Clean up a little CRLF unpleasantness that suddenly appeared
161 * Revision 1.13 2002/02/21 00:10:37 jongfoster
162 * Adding send-banner?type=auto option
164 * Revision 1.12 2002/01/23 01:03:32 jongfoster
165 * Fixing gcc [CygWin] compiler warnings
167 * Revision 1.11 2002/01/23 00:01:04 jongfoster
168 * Adding cgi_transparent_gif() for http://i.j.b/t
169 * Adding missing html_encode() to many CGI functions.
170 * Adding urlmatch.[ch] to http://i.j.b/show-version
172 * Revision 1.10 2002/01/17 21:10:37 jongfoster
173 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
174 * Also fixing a problem in the same function with improperly quoted URLs
175 * in output HTML, and adding code to handle https:// URLs correctly.
177 * Revision 1.9 2001/11/30 23:09:15 jongfoster
178 * Now reports on FEATURE_CGI_EDIT_ACTIONS
179 * Removing FEATURE_DENY_GZIP from template
181 * Revision 1.8 2001/11/13 00:14:07 jongfoster
182 * Fixing stupid bug now I've figured out what || means.
183 * (It always returns 0 or 1, not one of it's paramaters.)
185 * Revision 1.7 2001/10/23 21:48:19 jongfoster
186 * Cleaning up error handling in CGI functions - they now send back
187 * a HTML error page and should never cause a FATAL error. (Fixes one
188 * potential source of "denial of service" attacks).
190 * CGI actions file editor that works and is actually useful.
192 * Ability to toggle JunkBuster remotely using a CGI call.
194 * You can turn off both the above features in the main configuration
195 * file, e.g. if you are running a multi-user proxy.
197 * Revision 1.6 2001/10/14 22:00:32 jongfoster
198 * Adding support for a 404 error when an invalid CGI page is requested.
200 * Revision 1.5 2001/10/07 15:30:41 oes
201 * Removed FEATURE_DENY_GZIP
203 * Revision 1.4 2001/10/02 15:31:12 oes
204 * Introduced show-request cgi
206 * Revision 1.3 2001/09/22 16:34:44 jongfoster
207 * Removing unneeded #includes
209 * Revision 1.2 2001/09/19 18:01:11 oes
210 * Fixed comments; cosmetics
212 * Revision 1.1 2001/09/16 17:08:54 jongfoster
213 * Moving simple CGI functions from cgi.c to new file cgisimple.c
216 **********************************************************************/
222 #include <sys/types.h>
229 #define snprintf _snprintf
230 #endif /* def _WIN32 */
234 #include "cgisimple.h"
240 #include "miscutil.h"
243 #include "urlmatch.h"
246 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
249 static char *show_rcs(void);
250 static jb_err show_defines(struct map *exports);
253 /*********************************************************************
255 * Function : cgi_default
257 * Description : CGI function that is called for the CGI_SITE_1_HOST
258 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
259 * Boring - only exports the default exports.
262 * 1 : csp = Current client state (buffers, headers, etc...)
263 * 2 : rsp = http_response data structure for output
264 * 3 : parameters = map of cgi parameters
266 * CGI Parameters : none
268 * Returns : JB_ERR_OK on success
269 * JB_ERR_MEMORY on out-of-memory
271 *********************************************************************/
272 jb_err cgi_default(struct client_state *csp,
273 struct http_response *rsp,
274 const struct map *parameters)
281 if (NULL == (exports = default_exports(csp, "")))
283 return JB_ERR_MEMORY;
286 return template_fill_for_cgi(csp, "default", exports, rsp);
290 /*********************************************************************
292 * Function : cgi_error_404
294 * Description : CGI function that is called if an unknown action was
298 * 1 : csp = Current client state (buffers, headers, etc...)
299 * 2 : rsp = http_response data structure for output
300 * 3 : parameters = map of cgi parameters
302 * CGI Parameters : none
304 * Returns : JB_ERR_OK on success
305 * JB_ERR_MEMORY on out-of-memory error.
307 *********************************************************************/
308 jb_err cgi_error_404(struct client_state *csp,
309 struct http_response *rsp,
310 const struct map *parameters)
318 if (NULL == (exports = default_exports(csp, NULL)))
320 return JB_ERR_MEMORY;
323 rsp->status = strdup("404 Privoxy configuration page not found");
324 if (rsp->status == NULL)
327 return JB_ERR_MEMORY;
330 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
334 #ifdef FEATURE_GRACEFUL_TERMINATION
335 /*********************************************************************
339 * Description : CGI function to shut down Privoxy.
340 * NOTE: Turning this on in a production build
341 * would be a BAD idea. An EXTREMELY BAD idea.
342 * In short, don't do it.
345 * 1 : csp = Current client state (buffers, headers, etc...)
346 * 2 : rsp = http_response data structure for output
347 * 3 : parameters = map of cgi parameters
349 * CGI Parameters : none
351 * Returns : JB_ERR_OK on success
352 * JB_ERR_MEMORY on out-of-memory error.
354 *********************************************************************/
355 jb_err cgi_die (struct client_state *csp,
356 struct http_response *rsp,
357 const struct map *parameters)
367 * I don't really care what gets sent back to the browser.
368 * Take the easy option - "out of memory" page.
371 return JB_ERR_MEMORY;
373 #endif /* def FEATURE_GRACEFUL_TERMINATION */
376 /*********************************************************************
378 * Function : cgi_show_request
380 * Description : Show the client's request and what sed() would have
384 * 1 : csp = Current client state (buffers, headers, etc...)
385 * 2 : rsp = http_response data structure for output
386 * 3 : parameters = map of cgi parameters
388 * CGI Parameters : none
390 * Returns : JB_ERR_OK on success
391 * JB_ERR_MEMORY on out-of-memory error.
393 *********************************************************************/
394 jb_err cgi_show_request(struct client_state *csp,
395 struct http_response *rsp,
396 const struct map *parameters)
405 if (NULL == (exports = default_exports(csp, "show-request")))
407 return JB_ERR_MEMORY;
411 * Repair the damage done to the IOB by get_header()
413 for (p = csp->iob->buf; p < csp->iob->eod; p++)
415 if (*p == '\0') *p = '\n';
419 * Export the original client's request and the one we would
420 * be sending to the server if this wasn't a CGI call
423 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
426 return JB_ERR_MEMORY;
429 if (map(exports, "processed-request", 1, html_encode_and_free_original(
430 sed(client_patterns, add_client_headers, csp)), 0))
433 return JB_ERR_MEMORY;
436 return template_fill_for_cgi(csp, "show-request", exports, rsp);
440 /*********************************************************************
442 * Function : cgi_send_banner
444 * Description : CGI function that returns a banner.
447 * 1 : csp = Current client state (buffers, headers, etc...)
448 * 2 : rsp = http_response data structure for output
449 * 3 : parameters = map of cgi parameters
452 * type : Selects the type of banner between "trans", "logo",
453 * and "auto". Defaults to "logo" if absent or invalid.
454 * "auto" means to select as if we were image-blocking.
455 * (Only the first character really counts; b and t are
458 * Returns : JB_ERR_OK on success
459 * JB_ERR_MEMORY on out-of-memory error.
461 *********************************************************************/
462 jb_err cgi_send_banner(struct client_state *csp,
463 struct http_response *rsp,
464 const struct map *parameters)
466 char imagetype = lookup(parameters, "type")[0];
469 * If type is auto, then determine the right thing
470 * to do from the set-image-blocker action
472 if (imagetype == 'a')
479 #ifdef FEATURE_IMAGE_BLOCKING
480 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
482 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
483 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
484 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
488 /* Use default - nothing to do here. */
490 else if (0 == strcmpic(p, "blank"))
494 else if (0 == strcmpic(p, "pattern"))
500 * If the action is to call this CGI, determine
503 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
505 imagetype = p[sizeof(prefix1) - 1];
507 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
509 imagetype = p[sizeof(prefix2) - 1];
513 * Everything else must (should) be a URL to
521 #endif /* def FEATURE_IMAGE_BLOCKING */
525 * Now imagetype is either the non-auto type we were called with,
526 * or it was auto and has since been determined. In any case, we
527 * can proceed to actually answering the request by sending a redirect
528 * or an image as appropriate:
530 if (imagetype == 'r')
532 rsp->status = strdup("302 Local Redirect from Privoxy");
533 if (rsp->status == NULL)
535 return JB_ERR_MEMORY;
537 if (enlist_unique_header(rsp->headers, "Location",
538 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
540 return JB_ERR_MEMORY;
545 if ((imagetype == 'b') || (imagetype == 't'))
547 rsp->body = bindup(image_blank_data, image_blank_length);
548 rsp->content_length = image_blank_length;
552 rsp->body = bindup(image_pattern_data, image_pattern_length);
553 rsp->content_length = image_pattern_length;
556 if (rsp->body == NULL)
558 return JB_ERR_MEMORY;
560 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
562 return JB_ERR_MEMORY;
573 /*********************************************************************
575 * Function : cgi_transparent_image
577 * Description : CGI function that sends a 1x1 transparent image.
580 * 1 : csp = Current client state (buffers, headers, etc...)
581 * 2 : rsp = http_response data structure for output
582 * 3 : parameters = map of cgi parameters
584 * CGI Parameters : None
586 * Returns : JB_ERR_OK on success
587 * JB_ERR_MEMORY on out-of-memory error.
589 *********************************************************************/
590 jb_err cgi_transparent_image(struct client_state *csp,
591 struct http_response *rsp,
592 const struct map *parameters)
594 rsp->body = bindup(image_blank_data, image_blank_length);
595 rsp->content_length = image_blank_length;
597 if (rsp->body == NULL)
599 return JB_ERR_MEMORY;
602 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
604 return JB_ERR_MEMORY;
614 /*********************************************************************
616 * Function : cgi_send_stylesheet
618 * Description : CGI function that sends a css stylesheet found
619 * in the cgi-style.css template
622 * 1 : csp = Current client state (buffers, headers, etc...)
623 * 2 : rsp = http_response data structure for output
624 * 3 : parameters = map of cgi parameters
626 * CGI Parameters : None
628 * Returns : JB_ERR_OK on success
629 * JB_ERR_MEMORY on out-of-memory error.
631 *********************************************************************/
632 jb_err cgi_send_stylesheet(struct client_state *csp,
633 struct http_response *rsp,
634 const struct map *parameters)
641 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
643 if (err == JB_ERR_FILE)
646 * No way to tell user; send empty stylesheet
648 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
652 return err; /* JB_ERR_MEMORY */
655 if (enlist(rsp->headers, "Content-Type: text/css"))
657 return JB_ERR_MEMORY;
663 /*********************************************************************
665 * Function : cgi_send_user_manual
667 * Description : CGI function that sends a user manual HTML file
670 * 1 : csp = Current client state (buffers, headers, etc...)
671 * 2 : rsp = http_response data structure for output
672 * 3 : parameters = map of cgi parameters
674 * CGI Parameters : file=name.html, the name of the HTML file
675 * (relative to user-manual from config)
677 * Returns : JB_ERR_OK on success
678 * JB_ERR_MEMORY on out-of-memory error.
680 *********************************************************************/
681 jb_err cgi_send_user_manual(struct client_state *csp,
682 struct http_response *rsp,
683 const struct map *parameters)
685 const char * filename;
688 char buf[BUFFER_SIZE];
689 jb_err err = JB_ERR_OK;
695 get_string_param(parameters, "file", &filename);
696 /* Check paramter for hack attempts */
697 if (filename && strchr(filename, '/'))
699 return JB_ERR_CGI_PARAMS;
701 if (filename && strstr(filename, ".."))
703 return JB_ERR_CGI_PARAMS;
706 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
707 if (full_path == NULL)
709 return JB_ERR_MEMORY;
712 /* Allocate buffer */
713 rsp->body = strdup("");
714 if (rsp->body == NULL)
717 return JB_ERR_MEMORY;
720 /* Open user-manual file */
721 if (NULL == (fp = fopen(full_path, "r")))
723 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
724 err = cgi_error_no_template(csp, rsp, full_path);
730 /* Read file and write it out */
731 while (fgets(buf, BUFFER_SIZE, fp))
733 if (string_append(&rsp->body, buf))
736 return JB_ERR_MEMORY;
741 if (enlist(rsp->headers, "Content-Type: text/html"))
743 return JB_ERR_MEMORY;
750 /*********************************************************************
752 * Function : cgi_show_version
754 * Description : CGI function that returns a a web page describing the
755 * file versions of Privoxy.
758 * 1 : csp = Current client state (buffers, headers, etc...)
759 * 2 : rsp = http_response data structure for output
760 * 3 : parameters = map of cgi parameters
762 * CGI Parameters : none
764 * Returns : JB_ERR_OK on success
765 * JB_ERR_MEMORY on out-of-memory error.
767 *********************************************************************/
768 jb_err cgi_show_version(struct client_state *csp,
769 struct http_response *rsp,
770 const struct map *parameters)
778 if (NULL == (exports = default_exports(csp, "show-version")))
780 return JB_ERR_MEMORY;
783 if (map(exports, "sourceversions", 1, show_rcs(), 0))
786 return JB_ERR_MEMORY;
789 return template_fill_for_cgi(csp, "show-version", exports, rsp);
793 /*********************************************************************
795 * Function : cgi_show_status
797 * Description : CGI function that returns a a web page describing the
798 * current status of Privoxy.
801 * 1 : csp = Current client state (buffers, headers, etc...)
802 * 2 : rsp = http_response data structure for output
803 * 3 : parameters = map of cgi parameters
806 * file : Which file to show. Only first letter is checked,
808 * - "p"ermissions (actions) file
811 * Default is to show menu and other information.
813 * Returns : JB_ERR_OK on success
814 * JB_ERR_MEMORY on out-of-memory error.
816 *********************************************************************/
817 jb_err cgi_show_status(struct client_state *csp,
818 struct http_response *rsp,
819 const struct map *parameters)
826 char buf[BUFFER_SIZE];
827 const char * filename = NULL;
828 char * file_description = NULL;
829 #ifdef FEATURE_STATISTICS
830 float perc_rej; /* Percentage of http requests rejected */
832 int local_urls_rejected;
833 #endif /* ndef FEATURE_STATISTICS */
834 struct file_list * fl;
835 struct url_actions * b;
836 jb_err err = JB_ERR_OK;
844 if (NULL == (exports = default_exports(csp, "show-status")))
846 return JB_ERR_MEMORY;
849 switch (*(lookup(parameters, "file")))
852 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
854 filename = csp->actions_list[i]->filename;
855 file_description = "Actions File";
860 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
862 filename = csp->rlist[i]->filename;
863 file_description = "Filter File";
871 filename = csp->tlist->filename;
872 file_description = "Trust File";
875 #endif /* def FEATURE_TRUST */
878 if (NULL != filename)
880 if ( map(exports, "file-description", 1, file_description, 1)
881 || map(exports, "filepath", 1, html_encode(filename), 0) )
884 return JB_ERR_MEMORY;
887 if ((fp = fopen(filename, "r")) == NULL)
889 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
892 return JB_ERR_MEMORY;
898 while ((s != NULL) && fgets(buf, sizeof(buf), fp))
900 string_join (&s, html_encode(buf));
904 if (map(exports, "contents", 1, s, 0))
907 return JB_ERR_MEMORY;
911 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
915 for (j = 0; (s != NULL) && (j < Argc); j++)
917 if (!err) err = string_join (&s, html_encode(Argv[j]));
918 if (!err) err = string_append(&s, " ");
920 if (!err) err = map(exports, "invocation", 1, s, 0);
922 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
923 if (!err) err = show_defines(exports);
928 return JB_ERR_MEMORY;
931 #ifdef FEATURE_STATISTICS
932 local_urls_read = urls_read;
933 local_urls_rejected = urls_rejected;
936 * Need to alter the stats not to include the fetch of this
939 * Can't do following thread safely! doh!
942 * urls_rejected--; * This will be incremented subsequently *
945 if (local_urls_read == 0)
947 if (!err) err = map_block_killer(exports, "have-stats");
951 if (!err) err = map_block_killer(exports, "have-no-stats");
953 perc_rej = (float)local_urls_rejected * 100.0F /
954 (float)local_urls_read;
956 sprintf(buf, "%d", local_urls_read);
957 if (!err) err = map(exports, "requests-received", 1, buf, 1);
959 sprintf(buf, "%d", local_urls_rejected);
960 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
962 sprintf(buf, "%6.2f", perc_rej);
963 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
966 #else /* ndef FEATURE_STATISTICS */
967 err = err || map_block_killer(exports, "statistics");
968 #endif /* ndef FEATURE_STATISTICS */
971 * List all action files in use, together with view and edit links,
972 * except for standard.action, which should only be viewable. (Not
973 * enforced in the editor itself)
974 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
977 for (i = 0; i < MAX_AF_FILES; i++)
979 if (((fl = csp->actions_list[i]) != NULL) && ((b = fl->f) != NULL))
981 if (!err) err = string_append(&s, "<tr><td>");
982 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
983 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
984 if (!err) err = string_append(&s, buf);
986 #ifdef FEATURE_CGI_EDIT_ACTIONS
987 if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
989 snprintf(buf, 100, " <a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
990 if (!err) err = string_append(&s, buf);
994 if (!err) err = string_append(&s, "</td></tr>\n");
999 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1003 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1007 * List all re_filterfiles in use, together with view options.
1008 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1011 for (i = 0; i < MAX_AF_FILES; i++)
1013 if (((fl = csp->rlist[i]) != NULL) && ((b = fl->f) != NULL))
1015 if (!err) err = string_append(&s, "<tr><td>");
1016 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1017 snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1018 if (!err) err = string_append(&s, buf);
1019 if (!err) err = string_append(&s, "</td></tr>\n");
1024 if (!err) err = map(exports, "re-filter-filename", 1, s, 0);
1028 if (!err) err = map(exports, "re-filter-filename", 1, "<tr><td>None specified</td></tr>", 1);
1029 if (!err) err = map_block_killer(exports, "have-filterfile");
1032 #ifdef FEATURE_TRUST
1035 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1039 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1040 if (!err) err = map_block_killer(exports, "have-trustfile");
1043 if (!err) err = map_block_killer(exports, "trust-support");
1044 #endif /* ndef FEATURE_TRUST */
1049 return JB_ERR_MEMORY;
1052 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1056 /*********************************************************************
1058 * Function : cgi_show_url_info
1060 * Description : CGI function that determines and shows which actions
1061 * Privoxy will perform for a given url, and which
1062 * matches starting from the defaults have lead to that.
1065 * 1 : csp = Current client state (buffers, headers, etc...)
1066 * 2 : rsp = http_response data structure for output
1067 * 3 : parameters = map of cgi parameters
1070 * url : The url whose actions are to be determined.
1071 * If url is unset, the url-given conditional will be
1072 * set, so that all but the form can be suppressed in
1075 * Returns : JB_ERR_OK on success
1076 * JB_ERR_MEMORY on out-of-memory error.
1078 *********************************************************************/
1079 jb_err cgi_show_url_info(struct client_state *csp,
1080 struct http_response *rsp,
1081 const struct map *parameters)
1084 struct map *exports;
1091 if (NULL == (exports = default_exports(csp, "show-url-info")))
1093 return JB_ERR_MEMORY;
1097 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1099 url_param = strdup(lookup(parameters, "url"));
1100 if (url_param == NULL)
1103 return JB_ERR_MEMORY;
1108 * Handle prefixes. 4 possibilities:
1109 * 1) "http://" or "https://" prefix present and followed by URL - OK
1110 * 2) Only the "http://" or "https://" part is present, no URL - change
1111 * to empty string so it will be detected later as "no URL".
1112 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1114 * 4) Parameter not specified or is empty string - let this fall through
1115 * for now, next block of code will handle it.
1117 if (0 == strncmp(url_param, "http://", 7))
1119 if (url_param[7] == '\0')
1122 * Empty URL (just prefix).
1123 * Make it totally empty so it's caught by the next if()
1125 url_param[0] = '\0';
1128 else if (0 == strncmp(url_param, "https://", 8))
1130 if (url_param[8] == '\0')
1133 * Empty URL (just prefix).
1134 * Make it totally empty so it's caught by the next if()
1136 url_param[0] = '\0';
1139 else if (url_param[0] != '\0')
1142 * Unknown prefix - assume http://
1144 char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1145 if (NULL == url_param_prefixed)
1149 return JB_ERR_MEMORY;
1151 strcpy(url_param_prefixed, "http://");
1152 strcpy(url_param_prefixed + 7, url_param);
1154 url_param = url_param_prefixed;
1158 if (url_param[0] == '\0')
1160 /* URL paramater not specified, display query form only. */
1162 if (map_block_killer(exports, "url-given")
1163 || map(exports, "url", 1, "", 1))
1166 return JB_ERR_MEMORY;
1171 /* Given a URL, so query it. */
1176 struct file_list *fl;
1177 struct url_actions *b;
1178 struct http_request url_to_query[1];
1179 struct current_action_spec action[1];
1182 if (map(exports, "url", 1, html_encode(url_param), 0))
1186 return JB_ERR_MEMORY;
1189 init_current_action(action);
1191 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1193 free_current_action(action);
1196 return JB_ERR_MEMORY;
1199 err = parse_http_url(url_param, url_to_query, csp);
1203 if (err == JB_ERR_MEMORY)
1205 free_http_request(url_to_query);
1206 free_current_action(action);
1208 return JB_ERR_MEMORY;
1214 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1215 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1217 free_current_action(action);
1218 free_http_request(url_to_query);
1223 return JB_ERR_MEMORY;
1226 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1230 * We have a warning about SSL paths. Hide it for insecure sites.
1232 if (!url_to_query->ssl)
1234 if (map_block_killer(exports, "https"))
1236 free_current_action(action);
1238 free_http_request(url_to_query);
1239 return JB_ERR_MEMORY;
1243 matches = strdup("<table class=\"transparent\">");
1245 for (i = 0; i < MAX_AF_FILES; i++)
1247 if (NULL == csp->config->actions_file_short[i]
1248 || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1252 if ((fl = csp->actions_list[i]) != NULL)
1254 if ((b = fl->f) != NULL)
1256 /* FIXME: Hardcoded HTML! */
1257 string_append(&matches, "<tr><th>In file: ");
1258 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1259 snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1260 string_append(&matches, buf);
1261 string_append(&matches, "View</a>");
1262 #ifdef FEATURE_CGI_EDIT_ACTIONS
1263 string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1264 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1265 string_append(&matches, "\">Edit</a>");
1267 string_append(&matches, "</th></tr>\n");
1274 for (; (b != NULL) && (matches != NULL); b = b->next)
1276 if (url_match(b->url, url_to_query))
1278 string_append(&matches, "<tr><td>{");
1279 string_join (&matches, actions_to_html(csp, b->action));
1280 string_append(&matches, " }</b><br>\n<code>");
1281 string_join (&matches, html_encode(b->url->spec));
1282 string_append(&matches, "</code></td></tr>\n");
1284 if (merge_current_action(action, b->action))
1287 free_http_request(url_to_query);
1288 free_current_action(action);
1290 return JB_ERR_MEMORY;
1298 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1301 string_append(&matches, "</table>\n");
1303 free_http_request(url_to_query);
1305 if (matches == NULL)
1307 free_current_action(action);
1309 return JB_ERR_MEMORY;
1312 if (map(exports, "matches", 1, matches , 0))
1314 free_current_action(action);
1316 return JB_ERR_MEMORY;
1319 s = current_action_to_html(csp, action);
1321 free_current_action(action);
1323 if (map(exports, "final", 1, s, 0))
1326 return JB_ERR_MEMORY;
1330 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1334 /*********************************************************************
1336 * Function : cgi_robots_txt
1338 * Description : CGI function to return "/robots.txt".
1341 * 1 : csp = Current client state (buffers, headers, etc...)
1342 * 2 : rsp = http_response data structure for output
1343 * 3 : parameters = map of cgi parameters
1345 * CGI Parameters : None
1347 * Returns : JB_ERR_OK on success
1348 * JB_ERR_MEMORY on out-of-memory error.
1350 *********************************************************************/
1351 jb_err cgi_robots_txt(struct client_state *csp,
1352 struct http_response *rsp,
1353 const struct map *parameters)
1359 "# This is the Privoxy control interface.\n"
1360 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1366 if (rsp->body == NULL)
1368 return JB_ERR_MEMORY;
1371 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1375 get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1376 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1378 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1382 /*********************************************************************
1384 * Function : show_defines
1386 * Description : Add to a map the state od all conditional #defines
1387 * used when building
1390 * 1 : exports = map to extend
1392 * Returns : JB_ERR_OK on success
1393 * JB_ERR_MEMORY on out-of-memory error.
1395 *********************************************************************/
1396 static jb_err show_defines(struct map *exports)
1398 jb_err err = JB_ERR_OK;
1401 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1402 #else /* ifndef FEATURE_ACL */
1403 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1404 #endif /* ndef FEATURE_ACL */
1406 #ifdef FEATURE_CGI_EDIT_ACTIONS
1407 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1408 #else /* ifndef FEATURE_COOKIE_JAR */
1409 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1410 #endif /* ndef FEATURE_COOKIE_JAR */
1412 #ifdef FEATURE_COOKIE_JAR
1413 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1414 #else /* ifndef FEATURE_COOKIE_JAR */
1415 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1416 #endif /* ndef FEATURE_COOKIE_JAR */
1418 #ifdef FEATURE_FAST_REDIRECTS
1419 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1420 #else /* ifndef FEATURE_FAST_REDIRECTS */
1421 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1422 #endif /* ndef FEATURE_FAST_REDIRECTS */
1424 #ifdef FEATURE_FORCE_LOAD
1425 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1426 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1427 #else /* ifndef FEATURE_FORCE_LOAD */
1428 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1429 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1430 #endif /* ndef FEATURE_FORCE_LOAD */
1432 #ifdef FEATURE_IMAGE_BLOCKING
1433 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1434 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1435 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1436 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1438 #ifdef FEATURE_IMAGE_DETECT_MSIE
1439 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1440 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1441 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1442 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1444 #ifdef FEATURE_KILL_POPUPS
1445 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1446 #else /* ifndef FEATURE_KILL_POPUPS */
1447 if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1448 #endif /* ndef FEATURE_KILL_POPUPS */
1450 #ifdef FEATURE_NO_GIFS
1451 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1452 #else /* ifndef FEATURE_NO_GIFS */
1453 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1454 #endif /* ndef FEATURE_NO_GIFS */
1456 #ifdef FEATURE_PTHREAD
1457 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1458 #else /* ifndef FEATURE_PTHREAD */
1459 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1460 #endif /* ndef FEATURE_PTHREAD */
1462 #ifdef FEATURE_STATISTICS
1463 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1464 #else /* ifndef FEATURE_STATISTICS */
1465 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1466 #endif /* ndef FEATURE_STATISTICS */
1468 #ifdef FEATURE_TOGGLE
1469 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1470 #else /* ifndef FEATURE_TOGGLE */
1471 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1472 #endif /* ndef FEATURE_TOGGLE */
1474 #ifdef FEATURE_TRUST
1475 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1476 #else /* ifndef FEATURE_TRUST */
1477 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1478 #endif /* ndef FEATURE_TRUST */
1481 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1482 #else /* ifndef STATIC_PCRE */
1483 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1484 #endif /* ndef STATIC_PCRE */
1487 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1488 #else /* ifndef STATIC_PCRS */
1489 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1490 #endif /* ndef STATIC_PCRS */
1496 /*********************************************************************
1498 * Function : show_rcs
1500 * Description : Create a string with the rcs info for all sourcefiles
1504 * Returns : A string, or NULL on out-of-memory.
1506 *********************************************************************/
1507 static char *show_rcs(void)
1509 char *result = strdup("");
1510 char buf[BUFFER_SIZE];
1512 /* Instead of including *all* dot h's in the project (thus creating a
1513 * tremendous amount of dependencies), I will concede to declaring them
1514 * as extern's. This forces the developer to add to this list, but oh well.
1517 #define SHOW_RCS(__x) \
1519 extern const char __x[]; \
1520 sprintf(buf, "%s\n", __x); \
1521 string_append(&result, buf); \
1524 /* In alphabetical order */
1525 SHOW_RCS(actions_h_rcs)
1526 SHOW_RCS(actions_rcs)
1528 SHOW_RCS(amiga_h_rcs)
1530 #endif /* def AMIGA */
1533 #ifdef FEATURE_CGI_EDIT_ACTIONS
1534 SHOW_RCS(cgiedit_h_rcs)
1535 SHOW_RCS(cgiedit_rcs)
1536 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1537 SHOW_RCS(cgisimple_h_rcs)
1538 SHOW_RCS(cgisimple_rcs)
1540 SHOW_RCS(cygwin_h_rcs)
1542 SHOW_RCS(deanimate_h_rcs)
1543 SHOW_RCS(deanimate_rcs)
1544 SHOW_RCS(encode_h_rcs)
1545 SHOW_RCS(encode_rcs)
1546 SHOW_RCS(errlog_h_rcs)
1547 SHOW_RCS(errlog_rcs)
1548 SHOW_RCS(filters_h_rcs)
1549 SHOW_RCS(filters_rcs)
1550 SHOW_RCS(gateway_h_rcs)
1551 SHOW_RCS(gateway_rcs)
1552 SHOW_RCS(jbsockets_h_rcs)
1553 SHOW_RCS(jbsockets_rcs)
1556 #ifdef FEATURE_KILL_POPUPS
1557 SHOW_RCS(killpopup_h_rcs)
1558 SHOW_RCS(killpopup_rcs)
1559 #endif /* def FEATURE_KILL_POPUPS */
1560 SHOW_RCS(list_h_rcs)
1562 SHOW_RCS(loadcfg_h_rcs)
1563 SHOW_RCS(loadcfg_rcs)
1564 SHOW_RCS(loaders_h_rcs)
1565 SHOW_RCS(loaders_rcs)
1566 SHOW_RCS(miscutil_h_rcs)
1567 SHOW_RCS(miscutil_rcs)
1568 SHOW_RCS(parsers_h_rcs)
1569 SHOW_RCS(parsers_rcs)
1571 SHOW_RCS(pcrs_h_rcs)
1572 SHOW_RCS(project_h_rcs)
1573 SHOW_RCS(ssplit_h_rcs)
1574 SHOW_RCS(ssplit_rcs)
1575 SHOW_RCS(urlmatch_h_rcs)
1576 SHOW_RCS(urlmatch_rcs)
1578 #ifndef _WIN_CONSOLE
1579 SHOW_RCS(w32log_h_rcs)
1580 SHOW_RCS(w32log_rcs)
1581 SHOW_RCS(w32res_h_rcs)
1582 SHOW_RCS(w32taskbar_h_rcs)
1583 SHOW_RCS(w32taskbar_rcs)
1584 #endif /* ndef _WIN_CONSOLE */
1585 SHOW_RCS(win32_h_rcs)
1587 #endif /* def _WIN32 */