1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.71 2008/04/25 13:33:56 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-2008 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.71 2008/04/25 13:33:56 fabiankeil
40 * - Factor cgi_show_file() out of cgi_show_status().
41 * - Adjust cgi_show_status()'s parameter description to match reality.
43 * Revision 1.70 2008/04/24 16:12:38 fabiankeil
44 * In cgi_show_status(), load the requested file at once.
45 * Using string_join() for every line really doesn't scale.
47 * Revision 1.69 2008/04/17 14:40:48 fabiankeil
48 * Provide get_http_time() with the buffer size so it doesn't
49 * have to blindly assume that the buffer is big enough.
51 * Revision 1.68 2008/03/27 18:27:21 fabiankeil
52 * Remove kill-popups action.
54 * Revision 1.67 2008/03/27 17:00:05 fabiankeil
55 * Turn the favicon blobs into locals.
57 * Revision 1.66 2008/02/23 16:57:12 fabiankeil
58 * Rename url_actions() to get_url_actions() and let it
59 * use the standard parameter ordering.
61 * Revision 1.65 2008/02/23 16:33:43 fabiankeil
62 * Let forward_url() use the standard parameter ordering
63 * and mark its second parameter immutable.
65 * Revision 1.64 2008/02/03 13:56:07 fabiankeil
66 * Add SOCKS5 support for show-url-info CGI page.
68 * Revision 1.63 2008/02/01 06:04:31 fabiankeil
69 * If edit buttons on the show-url-info CGI page are hidden, explain why.
71 * Revision 1.62 2008/02/01 05:52:40 fabiankeil
72 * Hide edit buttons on the show-url-info CGI page if enable-edit-action
73 * is disabled. Patch by Lee with additional white space adjustments.
75 * Revision 1.61 2008/01/26 11:13:25 fabiankeil
76 * If enable-edit-actions is disabled, hide the edit buttons and explain why.
78 * Revision 1.60 2007/10/27 13:12:13 fabiankeil
79 * Finish 1.49 and check write access before
80 * showing edit buttons on show-url-info page.
82 * Revision 1.59 2007/10/19 16:42:36 fabiankeil
83 * Plug memory leak I introduced five months ago.
84 * Yay Valgrind and Privoxy-Regression-Test.
86 * Revision 1.58 2007/07/21 12:19:50 fabiankeil
87 * If show-url-info is called with an URL that Privoxy
88 * would reject as invalid, don't show unresolved forwarding
89 * variables, "final matches" or claim the site's secure.
91 * Revision 1.57 2007/06/01 16:53:05 fabiankeil
92 * Adjust cgi_show_url_info() to show what forward-override{}
93 * would do with the requested URL (instead of showing how the
94 * request for the CGI page would be forwarded if it wasn't a
97 * Revision 1.56 2007/05/21 10:50:35 fabiankeil
98 * - Use strlcpy() instead of strcpy().
99 * - Stop treating actions files special. Expect a complete file name
100 * (with or without path) like it's done for the rest of the files.
102 * - Don't rerun sed() in cgi_show_request().
104 * Revision 1.55 2007/04/13 13:36:46 fabiankeil
105 * Reference action files in CGI URLs by id instead
106 * of using the first part of the file name.
107 * Fixes BR 1694250 and BR 1590556.
109 * Revision 1.54 2007/04/09 18:11:35 fabiankeil
110 * Don't mistake VC++'s _snprintf() for a snprintf() replacement.
112 * Revision 1.53 2007/04/08 13:21:04 fabiankeil
113 * Reference action files in CGI URLs by id instead
114 * of using the first part of the file name.
115 * Fixes BR 1694250 and BR 1590556.
117 * Revision 1.52 2007/02/13 15:10:26 fabiankeil
118 * Apparently fopen()ing in "binary" mode doesn't require
119 * #ifdefs, it's already done without them in cgiedit.c.
121 * Revision 1.51 2007/02/10 16:55:22 fabiankeil
122 * - Show forwarding settings on the show-url-info page
123 * - Fix some HTML syntax errors.
125 * Revision 1.50 2007/01/23 15:51:17 fabiankeil
126 * Add favicon delivery functions.
128 * Revision 1.49 2007/01/20 16:29:38 fabiankeil
129 * Suppress edit buttons for action files if Privoxy has
130 * no write access. Suggested by Roland in PR 1564026.
132 * Revision 1.48 2007/01/20 15:31:31 fabiankeil
133 * Display warning if show-url-info CGI page
134 * is used while Privoxy is toggled off.
136 * Revision 1.47 2007/01/12 15:07:10 fabiankeil
137 * Use zalloc in cgi_send_user_manual.
139 * Revision 1.46 2007/01/02 12:49:46 fabiankeil
140 * Add FEATURE_ZLIB to the list of conditional
141 * defines at the show-status page.
143 * Revision 1.45 2006/12/28 18:16:41 fabiankeil
144 * Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
145 * body memory before using it, replaced sprintf calls with snprintf.
147 * Revision 1.44 2006/12/22 14:19:27 fabiankeil
148 * Removed checks whether or not AF_FILES have
149 * data structures associated with them in cgi_show_status.
150 * It doesn't matter as we're only interested in the file names.
152 * For the action files the checks were always true,
153 * but they prevented empty filter files from being
154 * listed. Fixes parts of BR 1619208.
156 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
157 * - Added FEATURE_GRACEFUL_TERMINATION to the
158 * "conditional #defines" section
159 * - Escaped ampersands in generated HTML.
160 * - Renamed re-filter-filename to re-filter-filenames
162 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
163 * Add special treatment for WIN32 to make sure
164 * cgi_send_user_manual opens the files in binary mode.
165 * Fixes BR 1600411 and unbreaks image delivery.
167 * Remove outdated comment.
169 * Revision 1.41 2006/10/09 19:18:28 roro
170 * Redirect http://p.p/user-manual (without trailing slash) to
171 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
173 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
174 * Modified cgi_send_user_manual to serve binary
175 * content without destroying it first. Should also be
176 * faster now. Added ".jpg" check for Content-Type guessing.
178 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
179 * Deliver documents in the user-manual directory
180 * with "Content-Type text/css" if their filename
183 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
184 * Incorporate modified version of Roland Rosenfeld's patch to
185 * optionally access the user-manual via Privoxy. Closes patch 679075.
187 * Formatting changed to Privoxy style, added call to
188 * cgi_error_no_template if the requested file doesn't
189 * exist and modified check whether or not Privoxy itself
190 * should serve the manual. Should work cross-platform now.
192 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
193 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
194 * with what was really the latest development (the v_3_0_branch branch)
196 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
197 * Multiple filter file support
199 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
200 * Undo some damaging memory leak patches
202 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
203 * A few memory leaks plugged (mostly on error paths)
205 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
206 * Another instance of:
207 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
208 * Thanks to Magnus Holmgren for the patch
210 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
211 * - Prevent line wrap beween "View/Edit" link buttons on status page
212 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
214 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
215 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
217 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
218 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
220 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
221 * Applying patch from Andreas as provided by Hal on the list.
222 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
224 * Revision 1.35 2002/05/12 21:44:44 jongfoster
225 * Adding amiga.[ch] revision information, if on an amiga.
227 * Revision 1.34 2002/04/30 12:06:12 oes
228 * Deleted unused code from default_cgi
230 * Revision 1.33 2002/04/30 11:14:52 oes
231 * Made csp the first parameter in *action_to_html
233 * Revision 1.32 2002/04/26 18:29:13 jongfoster
234 * Fixing this Visual C++ warning:
235 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
237 * Revision 1.31 2002/04/26 12:54:36 oes
238 * - Kill obsolete REDIRECT_URL code
239 * - Error handling fixes
240 * - Style sheet related HTML snipplet changes
241 * - cgi_show_url_info:
242 * - Matches now in table, actions on single lines,
244 * - standard.action suppressed
245 * - Buttons to View and Edit AFs
247 * Revision 1.30 2002/04/24 02:18:08 oes
248 * - show-status is now the starting point for editing
249 * the actions files, generate list of all AFs with buttons
250 * for viewing and editing, new look for file list (Jon:
251 * buttons now aligned ;-P ), view mode now supports multiple
252 * AFs, name changes, no view links for unspecified files,
253 * no edit link for standard.action.
255 * - Jon's multiple AF patch: cgi_show_url_info now uses all
256 * AFs and marks the output accordingly
258 * Revision 1.29 2002/04/10 13:38:35 oes
259 * load_template signature changed
261 * Revision 1.28 2002/04/07 15:42:12 jongfoster
262 * Fixing send-banner?type=auto when the image-blocker is
263 * a redirect to send-banner
265 * Revision 1.27 2002/04/05 15:50:48 oes
266 * added send-stylesheet CGI
268 * Revision 1.26 2002/04/04 00:36:36 gliptak
269 * always use pcre for matching
271 * Revision 1.25 2002/04/03 22:28:03 gliptak
272 * Removed references to gnu_regex
274 * Revision 1.24 2002/04/02 16:12:47 oes
275 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
277 * Revision 1.23 2002/03/26 22:29:54 swa
278 * we have a new homepage!
280 * Revision 1.22 2002/03/24 16:18:15 jongfoster
283 * Revision 1.21 2002/03/24 15:23:33 jongfoster
286 * Revision 1.20 2002/03/24 13:25:43 swa
287 * name change related issues
289 * Revision 1.19 2002/03/16 23:54:06 jongfoster
290 * Adding graceful termination feature, to help look for memory leaks.
291 * If you enable this (which, by design, has to be done by hand
292 * editing config.h) and then go to http://i.j.b/die, then the program
293 * will exit cleanly after the *next* request. It should free all the
294 * memory that was used.
296 * Revision 1.18 2002/03/12 01:44:49 oes
297 * Changed default for "blocked" image from jb logo to checkboard pattern
299 * Revision 1.17 2002/03/08 16:43:18 oes
300 * Added choice beween GIF and PNG built-in images
302 * Revision 1.16 2002/03/07 03:48:38 oes
303 * - Changed built-in images from GIF to PNG
304 * (with regard to Unisys patent issue)
305 * - Added a 4x4 pattern PNG which is less intrusive
306 * than the logo but also clearly marks the deleted banners
308 * Revision 1.15 2002/03/06 22:54:35 jongfoster
309 * Automated function-comment nitpicking.
311 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
312 * Clean up a little CRLF unpleasantness that suddenly appeared
314 * Revision 1.13 2002/02/21 00:10:37 jongfoster
315 * Adding send-banner?type=auto option
317 * Revision 1.12 2002/01/23 01:03:32 jongfoster
318 * Fixing gcc [CygWin] compiler warnings
320 * Revision 1.11 2002/01/23 00:01:04 jongfoster
321 * Adding cgi_transparent_gif() for http://i.j.b/t
322 * Adding missing html_encode() to many CGI functions.
323 * Adding urlmatch.[ch] to http://i.j.b/show-version
325 * Revision 1.10 2002/01/17 21:10:37 jongfoster
326 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
327 * Also fixing a problem in the same function with improperly quoted URLs
328 * in output HTML, and adding code to handle https:// URLs correctly.
330 * Revision 1.9 2001/11/30 23:09:15 jongfoster
331 * Now reports on FEATURE_CGI_EDIT_ACTIONS
332 * Removing FEATURE_DENY_GZIP from template
334 * Revision 1.8 2001/11/13 00:14:07 jongfoster
335 * Fixing stupid bug now I've figured out what || means.
336 * (It always returns 0 or 1, not one of it's paramaters.)
338 * Revision 1.7 2001/10/23 21:48:19 jongfoster
339 * Cleaning up error handling in CGI functions - they now send back
340 * a HTML error page and should never cause a FATAL error. (Fixes one
341 * potential source of "denial of service" attacks).
343 * CGI actions file editor that works and is actually useful.
345 * Ability to toggle JunkBuster remotely using a CGI call.
347 * You can turn off both the above features in the main configuration
348 * file, e.g. if you are running a multi-user proxy.
350 * Revision 1.6 2001/10/14 22:00:32 jongfoster
351 * Adding support for a 404 error when an invalid CGI page is requested.
353 * Revision 1.5 2001/10/07 15:30:41 oes
354 * Removed FEATURE_DENY_GZIP
356 * Revision 1.4 2001/10/02 15:31:12 oes
357 * Introduced show-request cgi
359 * Revision 1.3 2001/09/22 16:34:44 jongfoster
360 * Removing unneeded #includes
362 * Revision 1.2 2001/09/19 18:01:11 oes
363 * Fixed comments; cosmetics
365 * Revision 1.1 2001/09/16 17:08:54 jongfoster
366 * Moving simple CGI functions from cgi.c to new file cgisimple.c
369 **********************************************************************/
375 #include <sys/types.h>
383 #endif /* def HAVE_ACCESS */
387 #include "cgisimple.h"
393 #include "miscutil.h"
396 #include "urlmatch.h"
399 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
401 static char *show_rcs(void);
402 static jb_err show_defines(struct map *exports);
403 static jb_err cgi_show_file(struct client_state *csp,
404 struct http_response *rsp,
405 const struct map *parameters);
407 /*********************************************************************
409 * Function : cgi_default
411 * Description : CGI function that is called for the CGI_SITE_1_HOST
412 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
413 * Boring - only exports the default exports.
416 * 1 : csp = Current client state (buffers, headers, etc...)
417 * 2 : rsp = http_response data structure for output
418 * 3 : parameters = map of cgi parameters
420 * CGI Parameters : none
422 * Returns : JB_ERR_OK on success
423 * JB_ERR_MEMORY on out-of-memory
425 *********************************************************************/
426 jb_err cgi_default(struct client_state *csp,
427 struct http_response *rsp,
428 const struct map *parameters)
435 if (NULL == (exports = default_exports(csp, "")))
437 return JB_ERR_MEMORY;
440 return template_fill_for_cgi(csp, "default", exports, rsp);
444 /*********************************************************************
446 * Function : cgi_error_404
448 * Description : CGI function that is called if an unknown action was
452 * 1 : csp = Current client state (buffers, headers, etc...)
453 * 2 : rsp = http_response data structure for output
454 * 3 : parameters = map of cgi parameters
456 * CGI Parameters : none
458 * Returns : JB_ERR_OK on success
459 * JB_ERR_MEMORY on out-of-memory error.
461 *********************************************************************/
462 jb_err cgi_error_404(struct client_state *csp,
463 struct http_response *rsp,
464 const struct map *parameters)
472 if (NULL == (exports = default_exports(csp, NULL)))
474 return JB_ERR_MEMORY;
477 rsp->status = strdup("404 Privoxy configuration page not found");
478 if (rsp->status == NULL)
481 return JB_ERR_MEMORY;
484 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
488 #ifdef FEATURE_GRACEFUL_TERMINATION
489 /*********************************************************************
493 * Description : CGI function to shut down Privoxy.
494 * NOTE: Turning this on in a production build
495 * would be a BAD idea. An EXTREMELY BAD idea.
496 * In short, don't do it.
499 * 1 : csp = Current client state (buffers, headers, etc...)
500 * 2 : rsp = http_response data structure for output
501 * 3 : parameters = map of cgi parameters
503 * CGI Parameters : none
505 * Returns : JB_ERR_OK on success
506 * JB_ERR_MEMORY on out-of-memory error.
508 *********************************************************************/
509 jb_err cgi_die (struct client_state *csp,
510 struct http_response *rsp,
511 const struct map *parameters)
521 * I don't really care what gets sent back to the browser.
522 * Take the easy option - "out of memory" page.
525 return JB_ERR_MEMORY;
527 #endif /* def FEATURE_GRACEFUL_TERMINATION */
530 /*********************************************************************
532 * Function : cgi_show_request
534 * Description : Show the client's request and what sed() would have
538 * 1 : csp = Current client state (buffers, headers, etc...)
539 * 2 : rsp = http_response data structure for output
540 * 3 : parameters = map of cgi parameters
542 * CGI Parameters : none
544 * Returns : JB_ERR_OK on success
545 * JB_ERR_MEMORY on out-of-memory error.
547 *********************************************************************/
548 jb_err cgi_show_request(struct client_state *csp,
549 struct http_response *rsp,
550 const struct map *parameters)
559 if (NULL == (exports = default_exports(csp, "show-request")))
561 return JB_ERR_MEMORY;
565 * Repair the damage done to the IOB by get_header()
567 for (p = csp->iob->buf; p < csp->iob->eod; p++)
569 if (*p == '\0') *p = '\n';
573 * Export the original client's request and the one we would
574 * be sending to the server if this wasn't a CGI call
577 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
580 return JB_ERR_MEMORY;
583 if (map(exports, "processed-request", 1,
584 html_encode_and_free_original(list_to_text(csp->headers)), 0))
587 return JB_ERR_MEMORY;
590 return template_fill_for_cgi(csp, "show-request", exports, rsp);
594 /*********************************************************************
596 * Function : cgi_send_banner
598 * Description : CGI function that returns a banner.
601 * 1 : csp = Current client state (buffers, headers, etc...)
602 * 2 : rsp = http_response data structure for output
603 * 3 : parameters = map of cgi parameters
606 * type : Selects the type of banner between "trans", "logo",
607 * and "auto". Defaults to "logo" if absent or invalid.
608 * "auto" means to select as if we were image-blocking.
609 * (Only the first character really counts; b and t are
612 * Returns : JB_ERR_OK on success
613 * JB_ERR_MEMORY on out-of-memory error.
615 *********************************************************************/
616 jb_err cgi_send_banner(struct client_state *csp,
617 struct http_response *rsp,
618 const struct map *parameters)
620 char imagetype = lookup(parameters, "type")[0];
623 * If type is auto, then determine the right thing
624 * to do from the set-image-blocker action
626 if (imagetype == 'a')
633 #ifdef FEATURE_IMAGE_BLOCKING
634 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
636 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
637 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
638 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
642 /* Use default - nothing to do here. */
644 else if (0 == strcmpic(p, "blank"))
648 else if (0 == strcmpic(p, "pattern"))
654 * If the action is to call this CGI, determine
657 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
659 imagetype = p[sizeof(prefix1) - 1];
661 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
663 imagetype = p[sizeof(prefix2) - 1];
667 * Everything else must (should) be a URL to
675 #endif /* def FEATURE_IMAGE_BLOCKING */
679 * Now imagetype is either the non-auto type we were called with,
680 * or it was auto and has since been determined. In any case, we
681 * can proceed to actually answering the request by sending a redirect
682 * or an image as appropriate:
684 if (imagetype == 'r')
686 rsp->status = strdup("302 Local Redirect from Privoxy");
687 if (rsp->status == NULL)
689 return JB_ERR_MEMORY;
691 if (enlist_unique_header(rsp->headers, "Location",
692 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
694 return JB_ERR_MEMORY;
699 if ((imagetype == 'b') || (imagetype == 't'))
701 rsp->body = bindup(image_blank_data, image_blank_length);
702 rsp->content_length = image_blank_length;
706 rsp->body = bindup(image_pattern_data, image_pattern_length);
707 rsp->content_length = image_pattern_length;
710 if (rsp->body == NULL)
712 return JB_ERR_MEMORY;
714 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
716 return JB_ERR_MEMORY;
727 /*********************************************************************
729 * Function : cgi_transparent_image
731 * Description : CGI function that sends a 1x1 transparent image.
734 * 1 : csp = Current client state (buffers, headers, etc...)
735 * 2 : rsp = http_response data structure for output
736 * 3 : parameters = map of cgi parameters
738 * CGI Parameters : None
740 * Returns : JB_ERR_OK on success
741 * JB_ERR_MEMORY on out-of-memory error.
743 *********************************************************************/
744 jb_err cgi_transparent_image(struct client_state *csp,
745 struct http_response *rsp,
746 const struct map *parameters)
748 rsp->body = bindup(image_blank_data, image_blank_length);
749 rsp->content_length = image_blank_length;
751 if (rsp->body == NULL)
753 return JB_ERR_MEMORY;
756 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
758 return JB_ERR_MEMORY;
768 /*********************************************************************
770 * Function : cgi_send_default_favicon
772 * Description : CGI function that sends the standard favicon.
775 * 1 : csp = Current client state (buffers, headers, etc...)
776 * 2 : rsp = http_response data structure for output
777 * 3 : parameters = map of cgi parameters
779 * CGI Parameters : None
781 * Returns : JB_ERR_OK on success
782 * JB_ERR_MEMORY on out-of-memory error.
784 *********************************************************************/
785 jb_err cgi_send_default_favicon(struct client_state *csp,
786 struct http_response *rsp,
787 const struct map *parameters)
789 static const char default_favicon_data[] =
790 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
791 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
792 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
793 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
794 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
795 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
796 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
797 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
798 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
799 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
800 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
801 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
802 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
804 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
806 rsp->body = bindup(default_favicon_data, favicon_length);
807 rsp->content_length = favicon_length;
809 if (rsp->body == NULL)
811 return JB_ERR_MEMORY;
814 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
816 return JB_ERR_MEMORY;
826 /*********************************************************************
828 * Function : cgi_send_error_favicon
830 * Description : CGI function that sends the favicon for error pages.
833 * 1 : csp = Current client state (buffers, headers, etc...)
834 * 2 : rsp = http_response data structure for output
835 * 3 : parameters = map of cgi parameters
837 * CGI Parameters : None
839 * Returns : JB_ERR_OK on success
840 * JB_ERR_MEMORY on out-of-memory error.
842 *********************************************************************/
843 jb_err cgi_send_error_favicon(struct client_state *csp,
844 struct http_response *rsp,
845 const struct map *parameters)
847 static const char error_favicon_data[] =
848 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
849 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
850 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
851 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
852 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
853 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
854 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
855 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
856 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
857 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
858 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
859 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
860 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
862 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
864 rsp->body = bindup(error_favicon_data, favicon_length);
865 rsp->content_length = favicon_length;
867 if (rsp->body == NULL)
869 return JB_ERR_MEMORY;
872 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
874 return JB_ERR_MEMORY;
884 /*********************************************************************
886 * Function : cgi_send_stylesheet
888 * Description : CGI function that sends a css stylesheet found
889 * in the cgi-style.css template
892 * 1 : csp = Current client state (buffers, headers, etc...)
893 * 2 : rsp = http_response data structure for output
894 * 3 : parameters = map of cgi parameters
896 * CGI Parameters : None
898 * Returns : JB_ERR_OK on success
899 * JB_ERR_MEMORY on out-of-memory error.
901 *********************************************************************/
902 jb_err cgi_send_stylesheet(struct client_state *csp,
903 struct http_response *rsp,
904 const struct map *parameters)
911 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
913 if (err == JB_ERR_FILE)
916 * No way to tell user; send empty stylesheet
918 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
922 return err; /* JB_ERR_MEMORY */
925 if (enlist(rsp->headers, "Content-Type: text/css"))
927 return JB_ERR_MEMORY;
933 /*********************************************************************
935 * Function : cgi_send_user_manual
937 * Description : CGI function that sends a file in the user
941 * 1 : csp = Current client state (buffers, headers, etc...)
942 * 2 : rsp = http_response data structure for output
943 * 3 : parameters = map of cgi parameters
945 * CGI Parameters : file=name.html, the name of the HTML file
946 * (relative to user-manual from config)
948 * Returns : JB_ERR_OK on success
949 * JB_ERR_MEMORY on out-of-memory error.
951 *********************************************************************/
952 jb_err cgi_send_user_manual(struct client_state *csp,
953 struct http_response *rsp,
954 const struct map *parameters)
956 const char * filename;
959 jb_err err = JB_ERR_OK;
966 if (!parameters->first)
968 /* requested http://p.p/user-manual (without trailing slash) */
969 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
972 get_string_param(parameters, "file", &filename);
973 /* Check paramter for hack attempts */
974 if (filename && strchr(filename, '/'))
976 return JB_ERR_CGI_PARAMS;
978 if (filename && strstr(filename, ".."))
980 return JB_ERR_CGI_PARAMS;
983 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
984 if (full_path == NULL)
986 return JB_ERR_MEMORY;
989 /* Open user-manual file */
990 if (NULL == (fp = fopen(full_path, "rb")))
992 log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
993 err = cgi_error_no_template(csp, rsp, full_path);
998 /* Get file length */
999 fseek(fp, 0, SEEK_END);
1000 length = (size_t)ftell(fp);
1001 fseek(fp, 0, SEEK_SET);
1003 /* Allocate memory and load the file directly into the body */
1004 rsp->body = (char *)zalloc(length+1);
1009 return JB_ERR_MEMORY;
1011 if (!fread(rsp->body, length, 1, fp))
1014 * May happen if the file size changes between fseek() and fread().
1015 * If it does, we just log it and serve what we got.
1017 log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
1022 rsp->content_length = length;
1024 /* Guess correct Content-Type based on the filename's ending */
1027 length = strlen(filename);
1033 if((length>=4) && !strcmp(&filename[length-4], ".css"))
1035 err = enlist(rsp->headers, "Content-Type: text/css");
1037 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1039 err = enlist(rsp->headers, "Content-Type: image/jpeg");
1043 err = enlist(rsp->headers, "Content-Type: text/html");
1050 /*********************************************************************
1052 * Function : cgi_show_version
1054 * Description : CGI function that returns a a web page describing the
1055 * file versions of Privoxy.
1058 * 1 : csp = Current client state (buffers, headers, etc...)
1059 * 2 : rsp = http_response data structure for output
1060 * 3 : parameters = map of cgi parameters
1062 * CGI Parameters : none
1064 * Returns : JB_ERR_OK on success
1065 * JB_ERR_MEMORY on out-of-memory error.
1067 *********************************************************************/
1068 jb_err cgi_show_version(struct client_state *csp,
1069 struct http_response *rsp,
1070 const struct map *parameters)
1072 struct map *exports;
1078 if (NULL == (exports = default_exports(csp, "show-version")))
1080 return JB_ERR_MEMORY;
1083 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1086 return JB_ERR_MEMORY;
1089 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1093 /*********************************************************************
1095 * Function : cgi_show_status
1097 * Description : CGI function that returns a web page describing the
1098 * current status of Privoxy.
1101 * 1 : csp = Current client state (buffers, headers, etc...)
1102 * 2 : rsp = http_response data structure for output
1103 * 3 : parameters = map of cgi parameters
1106 * file : Which file to show. Only first letter is checked,
1111 * Default is to show menu and other information.
1113 * Returns : JB_ERR_OK on success
1114 * JB_ERR_MEMORY on out-of-memory error.
1116 *********************************************************************/
1117 jb_err cgi_show_status(struct client_state *csp,
1118 struct http_response *rsp,
1119 const struct map *parameters)
1125 char buf[BUFFER_SIZE];
1126 #ifdef FEATURE_STATISTICS
1127 float perc_rej; /* Percentage of http requests rejected */
1128 int local_urls_read;
1129 int local_urls_rejected;
1130 #endif /* ndef FEATURE_STATISTICS */
1131 jb_err err = JB_ERR_OK;
1133 struct map *exports;
1139 if ('\0' != *(lookup(parameters, "file")))
1141 return cgi_show_file(csp, rsp, parameters);
1144 if (NULL == (exports = default_exports(csp, "show-status")))
1146 return JB_ERR_MEMORY;
1150 for (j = 0; (s != NULL) && (j < Argc); j++)
1152 if (!err) err = string_join (&s, html_encode(Argv[j]));
1153 if (!err) err = string_append(&s, " ");
1155 if (!err) err = map(exports, "invocation", 1, s, 0);
1157 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1158 if (!err) err = show_defines(exports);
1163 return JB_ERR_MEMORY;
1166 #ifdef FEATURE_STATISTICS
1167 local_urls_read = urls_read;
1168 local_urls_rejected = urls_rejected;
1171 * Need to alter the stats not to include the fetch of this
1174 * Can't do following thread safely! doh!
1177 * urls_rejected--; * This will be incremented subsequently *
1180 if (local_urls_read == 0)
1182 if (!err) err = map_block_killer(exports, "have-stats");
1186 if (!err) err = map_block_killer(exports, "have-no-stats");
1188 perc_rej = (float)local_urls_rejected * 100.0F /
1189 (float)local_urls_read;
1191 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1192 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1194 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1195 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1197 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1198 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1201 #else /* ndef FEATURE_STATISTICS */
1202 err = err || map_block_killer(exports, "statistics");
1203 #endif /* ndef FEATURE_STATISTICS */
1206 * List all action files in use, together with view and edit links,
1207 * except for standard.action, which should only be viewable. (Not
1208 * enforced in the editor itself)
1209 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1212 for (i = 0; i < MAX_AF_FILES; i++)
1214 if (csp->actions_list[i] != NULL)
1216 if (!err) err = string_append(&s, "<tr><td>");
1217 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1218 snprintf(buf, sizeof(buf),
1219 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1220 if (!err) err = string_append(&s, buf);
1222 #ifdef FEATURE_CGI_EDIT_ACTIONS
1223 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1224 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1225 && (NULL != csp->config->actions_file_short[i]))
1228 if (access(csp->config->actions_file[i], W_OK) == 0)
1230 #endif /* def HAVE_ACCESS */
1231 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%d\">Edit</a>", i);
1232 if (!err) err = string_append(&s, buf);
1237 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1239 #endif /* def HAVE_ACCESS */
1243 if (!err) err = string_append(&s, "</td></tr>\n");
1248 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1252 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1256 * List all re_filterfiles in use, together with view options.
1257 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1260 for (i = 0; i < MAX_AF_FILES; i++)
1262 if (csp->rlist[i] != NULL)
1264 if (!err) err = string_append(&s, "<tr><td>");
1265 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1267 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1268 if (!err) err = string_append(&s, buf);
1269 if (!err) err = string_append(&s, "</td></tr>\n");
1274 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1278 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1279 if (!err) err = map_block_killer(exports, "have-filterfile");
1282 #ifdef FEATURE_TRUST
1285 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1289 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1290 if (!err) err = map_block_killer(exports, "have-trustfile");
1293 if (!err) err = map_block_killer(exports, "trust-support");
1294 #endif /* ndef FEATURE_TRUST */
1296 #ifdef FEATURE_CGI_EDIT_ACTIONS
1297 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1299 err = map_block_killer(exports, "cgi-editor-is-disabled");
1301 #endif /* ndef CGI_EDIT_ACTIONS */
1306 return JB_ERR_MEMORY;
1309 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1313 /*********************************************************************
1315 * Function : cgi_show_url_info
1317 * Description : CGI function that determines and shows which actions
1318 * Privoxy will perform for a given url, and which
1319 * matches starting from the defaults have lead to that.
1322 * 1 : csp = Current client state (buffers, headers, etc...)
1323 * 2 : rsp = http_response data structure for output
1324 * 3 : parameters = map of cgi parameters
1327 * url : The url whose actions are to be determined.
1328 * If url is unset, the url-given conditional will be
1329 * set, so that all but the form can be suppressed in
1332 * Returns : JB_ERR_OK on success
1333 * JB_ERR_MEMORY on out-of-memory error.
1335 *********************************************************************/
1336 jb_err cgi_show_url_info(struct client_state *csp,
1337 struct http_response *rsp,
1338 const struct map *parameters)
1341 struct map *exports;
1348 if (NULL == (exports = default_exports(csp, "show-url-info")))
1350 return JB_ERR_MEMORY;
1354 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1356 url_param = strdup(lookup(parameters, "url"));
1357 if (url_param == NULL)
1360 return JB_ERR_MEMORY;
1365 * Handle prefixes. 4 possibilities:
1366 * 1) "http://" or "https://" prefix present and followed by URL - OK
1367 * 2) Only the "http://" or "https://" part is present, no URL - change
1368 * to empty string so it will be detected later as "no URL".
1369 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1371 * 4) Parameter not specified or is empty string - let this fall through
1372 * for now, next block of code will handle it.
1374 if (0 == strncmp(url_param, "http://", 7))
1376 if (url_param[7] == '\0')
1379 * Empty URL (just prefix).
1380 * Make it totally empty so it's caught by the next if()
1382 url_param[0] = '\0';
1385 else if (0 == strncmp(url_param, "https://", 8))
1387 if (url_param[8] == '\0')
1390 * Empty URL (just prefix).
1391 * Make it totally empty so it's caught by the next if()
1393 url_param[0] = '\0';
1396 else if (url_param[0] != '\0')
1399 * Unknown prefix - assume http://
1401 const size_t url_param_prefixed_size = 7 + 1 + strlen(url_param);
1402 char * url_param_prefixed = malloc(url_param_prefixed_size);
1403 if (NULL == url_param_prefixed)
1407 return JB_ERR_MEMORY;
1409 strlcpy(url_param_prefixed, "http://", url_param_prefixed_size);
1410 strlcat(url_param_prefixed, url_param, url_param_prefixed_size);
1412 url_param = url_param_prefixed;
1416 * Hide "toggle off" warning if Privoxy is toggled on.
1419 #ifdef FEATURE_TOGGLE
1420 (global_toggle_state == 1) &&
1421 #endif /* def FEATURE_TOGGLE */
1422 map_block_killer(exports, "privoxy-is-toggled-off")
1426 return JB_ERR_MEMORY;
1429 if (url_param[0] == '\0')
1431 /* URL paramater not specified, display query form only. */
1433 if (map_block_killer(exports, "url-given")
1434 || map(exports, "url", 1, "", 1))
1437 return JB_ERR_MEMORY;
1442 /* Given a URL, so query it. */
1447 struct file_list *fl;
1448 struct url_actions *b;
1449 struct http_request url_to_query[1];
1450 struct current_action_spec action[1];
1453 if (map(exports, "url", 1, html_encode(url_param), 0))
1457 return JB_ERR_MEMORY;
1460 init_current_action(action);
1462 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1464 free_current_action(action);
1467 return JB_ERR_MEMORY;
1470 err = parse_http_url(url_param, url_to_query, csp);
1474 if (err == JB_ERR_MEMORY)
1476 free_http_request(url_to_query);
1477 free_current_action(action);
1479 return JB_ERR_MEMORY;
1485 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1486 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1487 if (!err) err = map_block_killer(exports, "valid-url");
1489 free_current_action(action);
1490 free_http_request(url_to_query);
1495 return JB_ERR_MEMORY;
1498 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1502 * We have a warning about SSL paths. Hide it for unencrypted sites.
1504 if (!url_to_query->ssl)
1506 if (map_block_killer(exports, "https"))
1508 free_current_action(action);
1510 free_http_request(url_to_query);
1511 return JB_ERR_MEMORY;
1515 matches = strdup("<table summary=\"\" class=\"transparent\">");
1517 for (i = 0; i < MAX_AF_FILES; i++)
1519 if (NULL == csp->config->actions_file_short[i]
1520 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1524 if ((fl = csp->actions_list[i]) != NULL)
1526 if ((b = fl->f) != NULL)
1528 /* FIXME: Hardcoded HTML! */
1529 string_append(&matches, "<tr><th>In file: ");
1530 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1531 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1532 string_append(&matches, buf);
1533 string_append(&matches, "View</a>");
1534 #ifdef FEATURE_CGI_EDIT_ACTIONS
1535 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1538 if (access(csp->config->actions_file[i], W_OK) == 0)
1540 #endif /* def HAVE_ACCESS */
1541 snprintf(buf, sizeof(buf),
1542 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1543 string_append(&matches, buf);
1544 string_append(&matches, "Edit</a>");
1549 string_append(&matches, " <strong>No write access.</strong>");
1551 #endif /* def HAVE_ACCESS */
1553 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1555 string_append(&matches, "</th></tr>\n");
1562 for (; (b != NULL) && (matches != NULL); b = b->next)
1564 if (url_match(b->url, url_to_query))
1566 string_append(&matches, "<tr><td>{");
1567 string_join (&matches, actions_to_html(csp, b->action));
1568 string_append(&matches, " }<br>\n<code>");
1569 string_join (&matches, html_encode(b->url->spec));
1570 string_append(&matches, "</code></td></tr>\n");
1572 if (merge_current_action(action, b->action))
1575 free_http_request(url_to_query);
1576 free_current_action(action);
1578 return JB_ERR_MEMORY;
1586 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1589 string_append(&matches, "</table>\n");
1592 * XXX: Kludge to make sure the "Forward settings" section
1593 * shows what forward-override{} would do with the requested URL.
1594 * No one really cares how the CGI request would be forwarded
1595 * if it wasn't intercepted as CGI request in the first place.
1597 * From here on the action bitmask will no longer reflect
1598 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1599 * but luckily it's no longer required later on anyway.
1601 free_current_action(csp->action);
1602 get_url_actions(csp, url_to_query);
1605 * Fill in forwarding settings.
1607 * The possibilities are:
1609 * - http forwarding only
1610 * - socks4(a) forwarding only
1611 * - socks4(a) and http forwarding.
1613 * XXX: Parts of this code could be reused for the
1614 * "forwarding-failed" template which currently doesn't
1615 * display the proxy port and an eventual second forwarder.
1618 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1620 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1622 if (!err) err = map_block_killer(exports, "socks-forwarder");
1623 if (!err) err = map_block_killer(exports, "http-forwarder");
1627 char port[10]; /* We save proxy ports as int but need a string here */
1629 if (!err) err = map_block_killer(exports, "no-forwarder");
1631 if (fwd->gateway_host != NULL)
1633 char *socks_type = NULL;
1638 socks_type = "socks4";
1641 socks_type = "socks4a";
1644 socks_type = "socks5";
1647 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1650 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1651 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1652 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1653 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1657 if (!err) err = map_block_killer(exports, "socks-forwarder");
1660 if (fwd->forward_host != NULL)
1662 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1663 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1664 if (!err) err = map(exports, "forward-port", 1, port, 1);
1668 if (!err) err = map_block_killer(exports, "http-forwarder");
1673 free_http_request(url_to_query);
1675 if (err || matches == NULL)
1677 free_current_action(action);
1679 return JB_ERR_MEMORY;
1682 #ifdef FEATURE_CGI_EDIT_ACTIONS
1683 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1685 err = map_block_killer(exports, "cgi-editor-is-disabled");
1687 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1690 * If zlib support is available, if no content filters
1691 * are enabled or if the prevent-compression action is enabled,
1692 * suppress the "compression could prevent filtering" warning.
1694 * XXX: Change content_filters_enabled()'s prototype so we can
1697 #ifndef FEATURE_ZLIB
1698 if ((list_is_empty(action->multi[ACTION_MULTI_FILTER])
1699 && !(action->flags & ACTION_DEANIMATE))
1700 || (action->flags & ACTION_NO_COMPRESSION))
1703 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1706 if (err || map(exports, "matches", 1, matches , 0))
1708 free_current_action(action);
1710 return JB_ERR_MEMORY;
1713 s = current_action_to_html(csp, action);
1715 free_current_action(action);
1717 if (map(exports, "final", 1, s, 0))
1720 return JB_ERR_MEMORY;
1724 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1728 /*********************************************************************
1730 * Function : cgi_robots_txt
1732 * Description : CGI function to return "/robots.txt".
1735 * 1 : csp = Current client state (buffers, headers, etc...)
1736 * 2 : rsp = http_response data structure for output
1737 * 3 : parameters = map of cgi parameters
1739 * CGI Parameters : None
1741 * Returns : JB_ERR_OK on success
1742 * JB_ERR_MEMORY on out-of-memory error.
1744 *********************************************************************/
1745 jb_err cgi_robots_txt(struct client_state *csp,
1746 struct http_response *rsp,
1747 const struct map *parameters)
1753 "# This is the Privoxy control interface.\n"
1754 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1760 if (rsp->body == NULL)
1762 return JB_ERR_MEMORY;
1765 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1769 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1770 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1772 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1776 /*********************************************************************
1778 * Function : show_defines
1780 * Description : Add to a map the state od all conditional #defines
1781 * used when building
1784 * 1 : exports = map to extend
1786 * Returns : JB_ERR_OK on success
1787 * JB_ERR_MEMORY on out-of-memory error.
1789 *********************************************************************/
1790 static jb_err show_defines(struct map *exports)
1792 jb_err err = JB_ERR_OK;
1795 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1796 #else /* ifndef FEATURE_ACL */
1797 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1798 #endif /* ndef FEATURE_ACL */
1800 #ifdef FEATURE_CGI_EDIT_ACTIONS
1801 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1802 #else /* ifndef FEATURE_COOKIE_JAR */
1803 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1804 #endif /* ndef FEATURE_COOKIE_JAR */
1806 #ifdef FEATURE_COOKIE_JAR
1807 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1808 #else /* ifndef FEATURE_COOKIE_JAR */
1809 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1810 #endif /* ndef FEATURE_COOKIE_JAR */
1812 #ifdef FEATURE_FAST_REDIRECTS
1813 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1814 #else /* ifndef FEATURE_FAST_REDIRECTS */
1815 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1816 #endif /* ndef FEATURE_FAST_REDIRECTS */
1818 #ifdef FEATURE_FORCE_LOAD
1819 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1820 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1821 #else /* ifndef FEATURE_FORCE_LOAD */
1822 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1823 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1824 #endif /* ndef FEATURE_FORCE_LOAD */
1826 #ifdef FEATURE_GRACEFUL_TERMINATION
1827 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1828 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1829 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1830 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1832 #ifdef FEATURE_IMAGE_BLOCKING
1833 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1834 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1835 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1836 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1838 #ifdef FEATURE_IMAGE_DETECT_MSIE
1839 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1840 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1841 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1842 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1844 #ifdef FEATURE_NO_GIFS
1845 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1846 #else /* ifndef FEATURE_NO_GIFS */
1847 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1848 #endif /* ndef FEATURE_NO_GIFS */
1850 #ifdef FEATURE_PTHREAD
1851 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1852 #else /* ifndef FEATURE_PTHREAD */
1853 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1854 #endif /* ndef FEATURE_PTHREAD */
1856 #ifdef FEATURE_STATISTICS
1857 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1858 #else /* ifndef FEATURE_STATISTICS */
1859 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1860 #endif /* ndef FEATURE_STATISTICS */
1862 #ifdef FEATURE_TOGGLE
1863 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1864 #else /* ifndef FEATURE_TOGGLE */
1865 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1866 #endif /* ndef FEATURE_TOGGLE */
1868 #ifdef FEATURE_TRUST
1869 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1870 #else /* ifndef FEATURE_TRUST */
1871 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1872 #endif /* ndef FEATURE_TRUST */
1875 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1876 #else /* ifndef FEATURE_ZLIB */
1877 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1878 #endif /* ndef FEATURE_ZLIB */
1881 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1882 #else /* ifndef STATIC_PCRE */
1883 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1884 #endif /* ndef STATIC_PCRE */
1887 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1888 #else /* ifndef STATIC_PCRS */
1889 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1890 #endif /* ndef STATIC_PCRS */
1896 /*********************************************************************
1898 * Function : show_rcs
1900 * Description : Create a string with the rcs info for all sourcefiles
1904 * Returns : A string, or NULL on out-of-memory.
1906 *********************************************************************/
1907 static char *show_rcs(void)
1909 char *result = strdup("");
1910 char buf[BUFFER_SIZE];
1912 /* Instead of including *all* dot h's in the project (thus creating a
1913 * tremendous amount of dependencies), I will concede to declaring them
1914 * as extern's. This forces the developer to add to this list, but oh well.
1917 #define SHOW_RCS(__x) \
1919 extern const char __x[]; \
1920 snprintf(buf, sizeof(buf), " %s\n", __x); \
1921 string_append(&result, buf); \
1924 /* In alphabetical order */
1925 SHOW_RCS(actions_h_rcs)
1926 SHOW_RCS(actions_rcs)
1928 SHOW_RCS(amiga_h_rcs)
1930 #endif /* def AMIGA */
1933 #ifdef FEATURE_CGI_EDIT_ACTIONS
1934 SHOW_RCS(cgiedit_h_rcs)
1935 SHOW_RCS(cgiedit_rcs)
1936 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1937 SHOW_RCS(cgisimple_h_rcs)
1938 SHOW_RCS(cgisimple_rcs)
1940 SHOW_RCS(cygwin_h_rcs)
1942 SHOW_RCS(deanimate_h_rcs)
1943 SHOW_RCS(deanimate_rcs)
1944 SHOW_RCS(encode_h_rcs)
1945 SHOW_RCS(encode_rcs)
1946 SHOW_RCS(errlog_h_rcs)
1947 SHOW_RCS(errlog_rcs)
1948 SHOW_RCS(filters_h_rcs)
1949 SHOW_RCS(filters_rcs)
1950 SHOW_RCS(gateway_h_rcs)
1951 SHOW_RCS(gateway_rcs)
1952 SHOW_RCS(jbsockets_h_rcs)
1953 SHOW_RCS(jbsockets_rcs)
1956 SHOW_RCS(list_h_rcs)
1958 SHOW_RCS(loadcfg_h_rcs)
1959 SHOW_RCS(loadcfg_rcs)
1960 SHOW_RCS(loaders_h_rcs)
1961 SHOW_RCS(loaders_rcs)
1962 SHOW_RCS(miscutil_h_rcs)
1963 SHOW_RCS(miscutil_rcs)
1964 SHOW_RCS(parsers_h_rcs)
1965 SHOW_RCS(parsers_rcs)
1967 SHOW_RCS(pcrs_h_rcs)
1968 SHOW_RCS(project_h_rcs)
1969 SHOW_RCS(ssplit_h_rcs)
1970 SHOW_RCS(ssplit_rcs)
1971 SHOW_RCS(urlmatch_h_rcs)
1972 SHOW_RCS(urlmatch_rcs)
1974 #ifndef _WIN_CONSOLE
1975 SHOW_RCS(w32log_h_rcs)
1976 SHOW_RCS(w32log_rcs)
1977 SHOW_RCS(w32res_h_rcs)
1978 SHOW_RCS(w32taskbar_h_rcs)
1979 SHOW_RCS(w32taskbar_rcs)
1980 #endif /* ndef _WIN_CONSOLE */
1981 SHOW_RCS(win32_h_rcs)
1983 #endif /* def _WIN32 */
1992 /*********************************************************************
1994 * Function : cgi_show_file
1996 * Description : CGI function that shows the content of a
1997 * configuration file.
2000 * 1 : csp = Current client state (buffers, headers, etc...)
2001 * 2 : rsp = http_response data structure for output
2002 * 3 : parameters = map of cgi parameters
2005 * file : Which file to show. Only first letter is checked,
2010 * Default is to show menu and other information.
2012 * Returns : JB_ERR_OK on success
2013 * JB_ERR_MEMORY on out-of-memory error.
2015 *********************************************************************/
2016 static jb_err cgi_show_file(struct client_state *csp,
2017 struct http_response *rsp,
2018 const struct map *parameters)
2021 const char * filename = NULL;
2022 char * file_description = NULL;
2028 switch (*(lookup(parameters, "file")))
2031 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2033 filename = csp->actions_list[i]->filename;
2034 file_description = "Actions File";
2039 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2041 filename = csp->rlist[i]->filename;
2042 file_description = "Filter File";
2046 #ifdef FEATURE_TRUST
2050 filename = csp->tlist->filename;
2051 file_description = "Trust File";
2054 #endif /* def FEATURE_TRUST */
2057 if (NULL != filename)
2059 struct map *exports;
2063 exports = default_exports(csp, "show-status");
2064 if (NULL == exports)
2066 return JB_ERR_MEMORY;
2069 if ( map(exports, "file-description", 1, file_description, 1)
2070 || map(exports, "filepath", 1, html_encode(filename), 0) )
2073 return JB_ERR_MEMORY;
2076 if ((fp = fopen(filename, "rb")) == NULL)
2078 if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2081 return JB_ERR_MEMORY;
2087 * XXX: this code is "quite similar" to the one
2088 * in cgi_send_user_manual() and should be refactored.
2089 * While at it, the return codes for ftell() and fseek
2090 * should be verified.
2093 /* Get file length */
2094 fseek(fp, 0, SEEK_END);
2095 length = (size_t)ftell(fp);
2096 fseek(fp, 0, SEEK_SET);
2098 s = (char *)zalloc(length+1);
2102 return JB_ERR_MEMORY;
2104 if (!fread(s, length, 1, fp))
2107 * May happen if the file size changes between fseek() and fread().
2108 * If it does, we just log it and serve what we got.
2110 log_error(LOG_LEVEL_ERROR, "Couldn't completely read file %s.", filename);
2114 s = html_encode_and_free_original(s);
2117 return JB_ERR_MEMORY;
2120 if (map(exports, "contents", 1, s, 0))
2123 return JB_ERR_MEMORY;
2127 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2131 * XXX: should return JB_ERR_PARSE but CGI handlers
2132 * currently aren't expected to do that.