1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.91 2009/03/08 14:19:23 fabiankeil Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
6 * Purpose : Simple CGIs to get information about Privoxy's
9 * Functions declared include:
12 * Copyright : Written by and Copyright (C) 2001-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.91 2009/03/08 14:19:23 fabiankeil
40 * Fix justified (but harmless) compiler warnings
41 * on platforms where sizeof(int) < sizeof(long).
43 * Revision 1.90 2009/03/01 18:43:09 fabiankeil
44 * Fix cparser warnings.
46 * Revision 1.89 2008/10/11 11:31:14 fabiankeil
47 * Add FEATURE_CONNECTION_KEEP_ALIVE to the list
48 * of conditional defines on the show-status page.
50 * Revision 1.88 2008/08/30 12:03:07 fabiankeil
51 * Remove FEATURE_COOKIE_JAR.
53 * Revision 1.87 2008/08/29 15:59:22 fabiankeil
56 * Revision 1.86 2008/06/28 14:19:05 fabiankeil
57 * Protocol detection is done case-insensitive, fix assertion
58 * to do the same. Yay for Privoxy-Regression-Test and zzuf.
60 * Revision 1.85 2008/05/26 17:30:55 fabiankeil
61 * Provide an OpenSearch Description to access the
62 * show-url-info page through "search engine plugins".
64 * Revision 1.84 2008/05/26 16:16:55 fabiankeil
65 * Spell error correctly.
67 * Revision 1.83 2008/05/12 14:51:30 fabiankeil
68 * Don't complain about an invalid URL if show-url-info is requested
69 * without parameters. Regression introduced in 1.81 by yours truly.
71 * Revision 1.82 2008/05/10 20:01:47 fabiankeil
72 * Fix an assertion that could erroneously
73 * trigger in case of memory shortage.
75 * Revision 1.81 2008/05/05 09:54:39 fabiankeil
76 * In cgi_show_url_info(), make sure ftp URLs are
77 * declared invalid. Also simplify the code that adds
78 * "http://" if no protocol has been specified.
80 * Revision 1.80 2008/05/04 16:18:32 fabiankeil
81 * Provide parse_http_url() with a third parameter to specify
82 * whether or not URLs without protocol are acceptable.
84 * Revision 1.79 2008/05/04 13:30:56 fabiankeil
85 * Streamline parse_http_url()'s prototype.
87 * Revision 1.78 2008/05/03 16:50:11 fabiankeil
88 * Leverage content_filters_enabled() in cgi_show_url_info().
90 * Revision 1.77 2008/05/02 09:47:48 fabiankeil
91 * In cgi_show_url_info, pass an initialized http structure
92 * to parse_http_url() as that will be required soonish and
93 * assert that https URLs are recognized correctly.
95 * Revision 1.76 2008/04/28 09:13:30 fabiankeil
96 * In load_file(), remember the error reason and fclose()
97 * and return later on instead of right away.
99 * Revision 1.75 2008/04/27 13:52:52 fabiankeil
100 * Move CGI file loading code into load_file() and
101 * add checks for unexpected errors.
103 * Revision 1.74 2008/04/26 15:50:56 fabiankeil
104 * Fix macro name in cgi_show_file() error path.
106 * Revision 1.73 2008/04/26 12:21:55 fabiankeil
107 * Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
109 * Revision 1.72 2008/04/26 10:34:15 fabiankeil
110 * If zlib support is unavailable and there are content filters active
111 * but the prevent-compression action is disabled, include a warning
112 * on the show-url-info page that compression might prevent filtering.
114 * Revision 1.71 2008/04/25 13:33:56 fabiankeil
115 * - Factor cgi_show_file() out of cgi_show_status().
116 * - Adjust cgi_show_status()'s parameter description to match reality.
118 * Revision 1.70 2008/04/24 16:12:38 fabiankeil
119 * In cgi_show_status(), load the requested file at once.
120 * Using string_join() for every line really doesn't scale.
122 * Revision 1.69 2008/04/17 14:40:48 fabiankeil
123 * Provide get_http_time() with the buffer size so it doesn't
124 * have to blindly assume that the buffer is big enough.
126 * Revision 1.68 2008/03/27 18:27:21 fabiankeil
127 * Remove kill-popups action.
129 * Revision 1.67 2008/03/27 17:00:05 fabiankeil
130 * Turn the favicon blobs into locals.
132 * Revision 1.66 2008/02/23 16:57:12 fabiankeil
133 * Rename url_actions() to get_url_actions() and let it
134 * use the standard parameter ordering.
136 * Revision 1.65 2008/02/23 16:33:43 fabiankeil
137 * Let forward_url() use the standard parameter ordering
138 * and mark its second parameter immutable.
140 * Revision 1.64 2008/02/03 13:56:07 fabiankeil
141 * Add SOCKS5 support for show-url-info CGI page.
143 * Revision 1.63 2008/02/01 06:04:31 fabiankeil
144 * If edit buttons on the show-url-info CGI page are hidden, explain why.
146 * Revision 1.62 2008/02/01 05:52:40 fabiankeil
147 * Hide edit buttons on the show-url-info CGI page if enable-edit-action
148 * is disabled. Patch by Lee with additional white space adjustments.
150 * Revision 1.61 2008/01/26 11:13:25 fabiankeil
151 * If enable-edit-actions is disabled, hide the edit buttons and explain why.
153 * Revision 1.60 2007/10/27 13:12:13 fabiankeil
154 * Finish 1.49 and check write access before
155 * showing edit buttons on show-url-info page.
157 * Revision 1.59 2007/10/19 16:42:36 fabiankeil
158 * Plug memory leak I introduced five months ago.
159 * Yay Valgrind and Privoxy-Regression-Test.
161 * Revision 1.58 2007/07/21 12:19:50 fabiankeil
162 * If show-url-info is called with an URL that Privoxy
163 * would reject as invalid, don't show unresolved forwarding
164 * variables, "final matches" or claim the site's secure.
166 * Revision 1.57 2007/06/01 16:53:05 fabiankeil
167 * Adjust cgi_show_url_info() to show what forward-override{}
168 * would do with the requested URL (instead of showing how the
169 * request for the CGI page would be forwarded if it wasn't a
172 * Revision 1.56 2007/05/21 10:50:35 fabiankeil
173 * - Use strlcpy() instead of strcpy().
174 * - Stop treating actions files special. Expect a complete file name
175 * (with or without path) like it's done for the rest of the files.
177 * - Don't rerun sed() in cgi_show_request().
179 * Revision 1.55 2007/04/13 13:36:46 fabiankeil
180 * Reference action files in CGI URLs by id instead
181 * of using the first part of the file name.
182 * Fixes BR 1694250 and BR 1590556.
184 * Revision 1.54 2007/04/09 18:11:35 fabiankeil
185 * Don't mistake VC++'s _snprintf() for a snprintf() replacement.
187 * Revision 1.53 2007/04/08 13:21:04 fabiankeil
188 * Reference action files in CGI URLs by id instead
189 * of using the first part of the file name.
190 * Fixes BR 1694250 and BR 1590556.
192 * Revision 1.52 2007/02/13 15:10:26 fabiankeil
193 * Apparently fopen()ing in "binary" mode doesn't require
194 * #ifdefs, it's already done without them in cgiedit.c.
196 * Revision 1.51 2007/02/10 16:55:22 fabiankeil
197 * - Show forwarding settings on the show-url-info page
198 * - Fix some HTML syntax errors.
200 * Revision 1.50 2007/01/23 15:51:17 fabiankeil
201 * Add favicon delivery functions.
203 * Revision 1.49 2007/01/20 16:29:38 fabiankeil
204 * Suppress edit buttons for action files if Privoxy has
205 * no write access. Suggested by Roland in PR 1564026.
207 * Revision 1.48 2007/01/20 15:31:31 fabiankeil
208 * Display warning if show-url-info CGI page
209 * is used while Privoxy is toggled off.
211 * Revision 1.47 2007/01/12 15:07:10 fabiankeil
212 * Use zalloc in cgi_send_user_manual.
214 * Revision 1.46 2007/01/02 12:49:46 fabiankeil
215 * Add FEATURE_ZLIB to the list of conditional
216 * defines at the show-status page.
218 * Revision 1.45 2006/12/28 18:16:41 fabiankeil
219 * Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
220 * body memory before using it, replaced sprintf calls with snprintf.
222 * Revision 1.44 2006/12/22 14:19:27 fabiankeil
223 * Removed checks whether or not AF_FILES have
224 * data structures associated with them in cgi_show_status.
225 * It doesn't matter as we're only interested in the file names.
227 * For the action files the checks were always true,
228 * but they prevented empty filter files from being
229 * listed. Fixes parts of BR 1619208.
231 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
232 * - Added FEATURE_GRACEFUL_TERMINATION to the
233 * "conditional #defines" section
234 * - Escaped ampersands in generated HTML.
235 * - Renamed re-filter-filename to re-filter-filenames
237 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
238 * Add special treatment for WIN32 to make sure
239 * cgi_send_user_manual opens the files in binary mode.
240 * Fixes BR 1600411 and unbreaks image delivery.
242 * Remove outdated comment.
244 * Revision 1.41 2006/10/09 19:18:28 roro
245 * Redirect http://p.p/user-manual (without trailing slash) to
246 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
248 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
249 * Modified cgi_send_user_manual to serve binary
250 * content without destroying it first. Should also be
251 * faster now. Added ".jpg" check for Content-Type guessing.
253 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
254 * Deliver documents in the user-manual directory
255 * with "Content-Type text/css" if their filename
258 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
259 * Incorporate modified version of Roland Rosenfeld's patch to
260 * optionally access the user-manual via Privoxy. Closes patch 679075.
262 * Formatting changed to Privoxy style, added call to
263 * cgi_error_no_template if the requested file doesn't
264 * exist and modified check whether or not Privoxy itself
265 * should serve the manual. Should work cross-platform now.
267 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
268 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
269 * with what was really the latest development (the v_3_0_branch branch)
271 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
272 * Multiple filter file support
274 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
275 * Undo some damaging memory leak patches
277 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
278 * A few memory leaks plugged (mostly on error paths)
280 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
281 * Another instance of:
282 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
283 * Thanks to Magnus Holmgren for the patch
285 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
286 * - Prevent line wrap beween "View/Edit" link buttons on status page
287 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
289 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
290 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
292 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
293 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
295 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
296 * Applying patch from Andreas as provided by Hal on the list.
297 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
299 * Revision 1.35 2002/05/12 21:44:44 jongfoster
300 * Adding amiga.[ch] revision information, if on an amiga.
302 * Revision 1.34 2002/04/30 12:06:12 oes
303 * Deleted unused code from default_cgi
305 * Revision 1.33 2002/04/30 11:14:52 oes
306 * Made csp the first parameter in *action_to_html
308 * Revision 1.32 2002/04/26 18:29:13 jongfoster
309 * Fixing this Visual C++ warning:
310 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
312 * Revision 1.31 2002/04/26 12:54:36 oes
313 * - Kill obsolete REDIRECT_URL code
314 * - Error handling fixes
315 * - Style sheet related HTML snipplet changes
316 * - cgi_show_url_info:
317 * - Matches now in table, actions on single lines,
319 * - standard.action suppressed
320 * - Buttons to View and Edit AFs
322 * Revision 1.30 2002/04/24 02:18:08 oes
323 * - show-status is now the starting point for editing
324 * the actions files, generate list of all AFs with buttons
325 * for viewing and editing, new look for file list (Jon:
326 * buttons now aligned ;-P ), view mode now supports multiple
327 * AFs, name changes, no view links for unspecified files,
328 * no edit link for standard.action.
330 * - Jon's multiple AF patch: cgi_show_url_info now uses all
331 * AFs and marks the output accordingly
333 * Revision 1.29 2002/04/10 13:38:35 oes
334 * load_template signature changed
336 * Revision 1.28 2002/04/07 15:42:12 jongfoster
337 * Fixing send-banner?type=auto when the image-blocker is
338 * a redirect to send-banner
340 * Revision 1.27 2002/04/05 15:50:48 oes
341 * added send-stylesheet CGI
343 * Revision 1.26 2002/04/04 00:36:36 gliptak
344 * always use pcre for matching
346 * Revision 1.25 2002/04/03 22:28:03 gliptak
347 * Removed references to gnu_regex
349 * Revision 1.24 2002/04/02 16:12:47 oes
350 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
352 * Revision 1.23 2002/03/26 22:29:54 swa
353 * we have a new homepage!
355 * Revision 1.22 2002/03/24 16:18:15 jongfoster
358 * Revision 1.21 2002/03/24 15:23:33 jongfoster
361 * Revision 1.20 2002/03/24 13:25:43 swa
362 * name change related issues
364 * Revision 1.19 2002/03/16 23:54:06 jongfoster
365 * Adding graceful termination feature, to help look for memory leaks.
366 * If you enable this (which, by design, has to be done by hand
367 * editing config.h) and then go to http://i.j.b/die, then the program
368 * will exit cleanly after the *next* request. It should free all the
369 * memory that was used.
371 * Revision 1.18 2002/03/12 01:44:49 oes
372 * Changed default for "blocked" image from jb logo to checkboard pattern
374 * Revision 1.17 2002/03/08 16:43:18 oes
375 * Added choice beween GIF and PNG built-in images
377 * Revision 1.16 2002/03/07 03:48:38 oes
378 * - Changed built-in images from GIF to PNG
379 * (with regard to Unisys patent issue)
380 * - Added a 4x4 pattern PNG which is less intrusive
381 * than the logo but also clearly marks the deleted banners
383 * Revision 1.15 2002/03/06 22:54:35 jongfoster
384 * Automated function-comment nitpicking.
386 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
387 * Clean up a little CRLF unpleasantness that suddenly appeared
389 * Revision 1.13 2002/02/21 00:10:37 jongfoster
390 * Adding send-banner?type=auto option
392 * Revision 1.12 2002/01/23 01:03:32 jongfoster
393 * Fixing gcc [CygWin] compiler warnings
395 * Revision 1.11 2002/01/23 00:01:04 jongfoster
396 * Adding cgi_transparent_gif() for http://i.j.b/t
397 * Adding missing html_encode() to many CGI functions.
398 * Adding urlmatch.[ch] to http://i.j.b/show-version
400 * Revision 1.10 2002/01/17 21:10:37 jongfoster
401 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
402 * Also fixing a problem in the same function with improperly quoted URLs
403 * in output HTML, and adding code to handle https:// URLs correctly.
405 * Revision 1.9 2001/11/30 23:09:15 jongfoster
406 * Now reports on FEATURE_CGI_EDIT_ACTIONS
407 * Removing FEATURE_DENY_GZIP from template
409 * Revision 1.8 2001/11/13 00:14:07 jongfoster
410 * Fixing stupid bug now I've figured out what || means.
411 * (It always returns 0 or 1, not one of it's paramaters.)
413 * Revision 1.7 2001/10/23 21:48:19 jongfoster
414 * Cleaning up error handling in CGI functions - they now send back
415 * a HTML error page and should never cause a FATAL error. (Fixes one
416 * potential source of "denial of service" attacks).
418 * CGI actions file editor that works and is actually useful.
420 * Ability to toggle JunkBuster remotely using a CGI call.
422 * You can turn off both the above features in the main configuration
423 * file, e.g. if you are running a multi-user proxy.
425 * Revision 1.6 2001/10/14 22:00:32 jongfoster
426 * Adding support for a 404 error when an invalid CGI page is requested.
428 * Revision 1.5 2001/10/07 15:30:41 oes
429 * Removed FEATURE_DENY_GZIP
431 * Revision 1.4 2001/10/02 15:31:12 oes
432 * Introduced show-request cgi
434 * Revision 1.3 2001/09/22 16:34:44 jongfoster
435 * Removing unneeded #includes
437 * Revision 1.2 2001/09/19 18:01:11 oes
438 * Fixed comments; cosmetics
440 * Revision 1.1 2001/09/16 17:08:54 jongfoster
441 * Moving simple CGI functions from cgi.c to new file cgisimple.c
444 **********************************************************************/
450 #include <sys/types.h>
458 #endif /* def HAVE_ACCESS */
462 #include "cgisimple.h"
468 #include "miscutil.h"
471 #include "urlmatch.h"
474 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
476 static char *show_rcs(void);
477 static jb_err show_defines(struct map *exports);
478 static jb_err cgi_show_file(struct client_state *csp,
479 struct http_response *rsp,
480 const struct map *parameters);
481 static jb_err load_file(const char *filename, char **buffer, size_t *length);
483 /*********************************************************************
485 * Function : cgi_default
487 * Description : CGI function that is called for the CGI_SITE_1_HOST
488 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
489 * Boring - only exports the default exports.
492 * 1 : csp = Current client state (buffers, headers, etc...)
493 * 2 : rsp = http_response data structure for output
494 * 3 : parameters = map of cgi parameters
496 * CGI Parameters : none
498 * Returns : JB_ERR_OK on success
499 * JB_ERR_MEMORY on out-of-memory
501 *********************************************************************/
502 jb_err cgi_default(struct client_state *csp,
503 struct http_response *rsp,
504 const struct map *parameters)
513 if (NULL == (exports = default_exports(csp, "")))
515 return JB_ERR_MEMORY;
518 return template_fill_for_cgi(csp, "default", exports, rsp);
522 /*********************************************************************
524 * Function : cgi_error_404
526 * Description : CGI function that is called if an unknown action was
530 * 1 : csp = Current client state (buffers, headers, etc...)
531 * 2 : rsp = http_response data structure for output
532 * 3 : parameters = map of cgi parameters
534 * CGI Parameters : none
536 * Returns : JB_ERR_OK on success
537 * JB_ERR_MEMORY on out-of-memory error.
539 *********************************************************************/
540 jb_err cgi_error_404(struct client_state *csp,
541 struct http_response *rsp,
542 const struct map *parameters)
550 if (NULL == (exports = default_exports(csp, NULL)))
552 return JB_ERR_MEMORY;
555 rsp->status = strdup("404 Privoxy configuration page not found");
556 if (rsp->status == NULL)
559 return JB_ERR_MEMORY;
562 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
566 #ifdef FEATURE_GRACEFUL_TERMINATION
567 /*********************************************************************
571 * Description : CGI function to shut down Privoxy.
572 * NOTE: Turning this on in a production build
573 * would be a BAD idea. An EXTREMELY BAD idea.
574 * In short, don't do it.
577 * 1 : csp = Current client state (buffers, headers, etc...)
578 * 2 : rsp = http_response data structure for output
579 * 3 : parameters = map of cgi parameters
581 * CGI Parameters : none
583 * Returns : JB_ERR_OK on success
584 * JB_ERR_MEMORY on out-of-memory error.
586 *********************************************************************/
587 jb_err cgi_die (struct client_state *csp,
588 struct http_response *rsp,
589 const struct map *parameters)
599 * I don't really care what gets sent back to the browser.
600 * Take the easy option - "out of memory" page.
603 return JB_ERR_MEMORY;
605 #endif /* def FEATURE_GRACEFUL_TERMINATION */
608 /*********************************************************************
610 * Function : cgi_show_request
612 * Description : Show the client's request and what sed() would have
616 * 1 : csp = Current client state (buffers, headers, etc...)
617 * 2 : rsp = http_response data structure for output
618 * 3 : parameters = map of cgi parameters
620 * CGI Parameters : none
622 * Returns : JB_ERR_OK on success
623 * JB_ERR_MEMORY on out-of-memory error.
625 *********************************************************************/
626 jb_err cgi_show_request(struct client_state *csp,
627 struct http_response *rsp,
628 const struct map *parameters)
637 if (NULL == (exports = default_exports(csp, "show-request")))
639 return JB_ERR_MEMORY;
643 * Repair the damage done to the IOB by get_header()
645 for (p = csp->iob->buf; p < csp->iob->eod; p++)
647 if (*p == '\0') *p = '\n';
651 * Export the original client's request and the one we would
652 * be sending to the server if this wasn't a CGI call
655 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
658 return JB_ERR_MEMORY;
661 if (map(exports, "processed-request", 1,
662 html_encode_and_free_original(list_to_text(csp->headers)), 0))
665 return JB_ERR_MEMORY;
668 return template_fill_for_cgi(csp, "show-request", exports, rsp);
672 /*********************************************************************
674 * Function : cgi_send_banner
676 * Description : CGI function that returns a banner.
679 * 1 : csp = Current client state (buffers, headers, etc...)
680 * 2 : rsp = http_response data structure for output
681 * 3 : parameters = map of cgi parameters
684 * type : Selects the type of banner between "trans", "logo",
685 * and "auto". Defaults to "logo" if absent or invalid.
686 * "auto" means to select as if we were image-blocking.
687 * (Only the first character really counts; b and t are
690 * Returns : JB_ERR_OK on success
691 * JB_ERR_MEMORY on out-of-memory error.
693 *********************************************************************/
694 jb_err cgi_send_banner(struct client_state *csp,
695 struct http_response *rsp,
696 const struct map *parameters)
698 char imagetype = lookup(parameters, "type")[0];
701 * If type is auto, then determine the right thing
702 * to do from the set-image-blocker action
704 if (imagetype == 'a')
711 #ifdef FEATURE_IMAGE_BLOCKING
712 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
714 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
715 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
716 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
720 /* Use default - nothing to do here. */
722 else if (0 == strcmpic(p, "blank"))
726 else if (0 == strcmpic(p, "pattern"))
732 * If the action is to call this CGI, determine
735 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
737 imagetype = p[sizeof(prefix1) - 1];
739 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
741 imagetype = p[sizeof(prefix2) - 1];
745 * Everything else must (should) be a URL to
753 #endif /* def FEATURE_IMAGE_BLOCKING */
757 * Now imagetype is either the non-auto type we were called with,
758 * or it was auto and has since been determined. In any case, we
759 * can proceed to actually answering the request by sending a redirect
760 * or an image as appropriate:
762 if (imagetype == 'r')
764 rsp->status = strdup("302 Local Redirect from Privoxy");
765 if (rsp->status == NULL)
767 return JB_ERR_MEMORY;
769 if (enlist_unique_header(rsp->headers, "Location",
770 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
772 return JB_ERR_MEMORY;
777 if ((imagetype == 'b') || (imagetype == 't'))
779 rsp->body = bindup(image_blank_data, image_blank_length);
780 rsp->content_length = image_blank_length;
784 rsp->body = bindup(image_pattern_data, image_pattern_length);
785 rsp->content_length = image_pattern_length;
788 if (rsp->body == NULL)
790 return JB_ERR_MEMORY;
792 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
794 return JB_ERR_MEMORY;
805 /*********************************************************************
807 * Function : cgi_transparent_image
809 * Description : CGI function that sends a 1x1 transparent image.
812 * 1 : csp = Current client state (buffers, headers, etc...)
813 * 2 : rsp = http_response data structure for output
814 * 3 : parameters = map of cgi parameters
816 * CGI Parameters : None
818 * Returns : JB_ERR_OK on success
819 * JB_ERR_MEMORY on out-of-memory error.
821 *********************************************************************/
822 jb_err cgi_transparent_image(struct client_state *csp,
823 struct http_response *rsp,
824 const struct map *parameters)
829 rsp->body = bindup(image_blank_data, image_blank_length);
830 rsp->content_length = image_blank_length;
832 if (rsp->body == NULL)
834 return JB_ERR_MEMORY;
837 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
839 return JB_ERR_MEMORY;
849 /*********************************************************************
851 * Function : cgi_send_default_favicon
853 * Description : CGI function that sends the standard favicon.
856 * 1 : csp = Current client state (buffers, headers, etc...)
857 * 2 : rsp = http_response data structure for output
858 * 3 : parameters = map of cgi parameters
860 * CGI Parameters : None
862 * Returns : JB_ERR_OK on success
863 * JB_ERR_MEMORY on out-of-memory error.
865 *********************************************************************/
866 jb_err cgi_send_default_favicon(struct client_state *csp,
867 struct http_response *rsp,
868 const struct map *parameters)
870 static const char default_favicon_data[] =
871 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
872 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
873 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
874 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
875 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
876 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
877 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
878 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
879 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
880 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
881 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
882 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
883 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
885 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
890 rsp->body = bindup(default_favicon_data, favicon_length);
891 rsp->content_length = favicon_length;
893 if (rsp->body == NULL)
895 return JB_ERR_MEMORY;
898 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
900 return JB_ERR_MEMORY;
910 /*********************************************************************
912 * Function : cgi_send_error_favicon
914 * Description : CGI function that sends the favicon for error pages.
917 * 1 : csp = Current client state (buffers, headers, etc...)
918 * 2 : rsp = http_response data structure for output
919 * 3 : parameters = map of cgi parameters
921 * CGI Parameters : None
923 * Returns : JB_ERR_OK on success
924 * JB_ERR_MEMORY on out-of-memory error.
926 *********************************************************************/
927 jb_err cgi_send_error_favicon(struct client_state *csp,
928 struct http_response *rsp,
929 const struct map *parameters)
931 static const char error_favicon_data[] =
932 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
933 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
934 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
935 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
936 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
937 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
938 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
939 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
940 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
941 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
942 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
943 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
944 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
946 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
951 rsp->body = bindup(error_favicon_data, favicon_length);
952 rsp->content_length = favicon_length;
954 if (rsp->body == NULL)
956 return JB_ERR_MEMORY;
959 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
961 return JB_ERR_MEMORY;
971 /*********************************************************************
973 * Function : cgi_send_stylesheet
975 * Description : CGI function that sends a css stylesheet found
976 * in the cgi-style.css template
979 * 1 : csp = Current client state (buffers, headers, etc...)
980 * 2 : rsp = http_response data structure for output
981 * 3 : parameters = map of cgi parameters
983 * CGI Parameters : None
985 * Returns : JB_ERR_OK on success
986 * JB_ERR_MEMORY on out-of-memory error.
988 *********************************************************************/
989 jb_err cgi_send_stylesheet(struct client_state *csp,
990 struct http_response *rsp,
991 const struct map *parameters)
1000 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
1002 if (err == JB_ERR_FILE)
1005 * No way to tell user; send empty stylesheet
1007 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
1011 return err; /* JB_ERR_MEMORY */
1014 if (enlist(rsp->headers, "Content-Type: text/css"))
1016 return JB_ERR_MEMORY;
1024 /*********************************************************************
1026 * Function : cgi_send_url_info_osd
1028 * Description : CGI function that sends the OpenSearch Description
1029 * template for the show-url-info page. It allows to
1030 * access the page through "search engine plugins".
1033 * 1 : csp = Current client state (buffers, headers, etc...)
1034 * 2 : rsp = http_response data structure for output
1035 * 3 : parameters = map of cgi parameters
1037 * CGI Parameters : None
1039 * Returns : JB_ERR_OK on success
1040 * JB_ERR_MEMORY on out-of-memory error.
1042 *********************************************************************/
1043 jb_err cgi_send_url_info_osd(struct client_state *csp,
1044 struct http_response *rsp,
1045 const struct map *parameters)
1047 jb_err err = JB_ERR_MEMORY;
1048 struct map *exports = default_exports(csp, NULL);
1053 if (NULL != exports)
1055 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1056 if (JB_ERR_OK == err)
1058 err = enlist(rsp->headers,
1059 "Content-Type: application/opensearchdescription+xml");
1068 /*********************************************************************
1070 * Function : cgi_send_user_manual
1072 * Description : CGI function that sends a file in the user
1076 * 1 : csp = Current client state (buffers, headers, etc...)
1077 * 2 : rsp = http_response data structure for output
1078 * 3 : parameters = map of cgi parameters
1080 * CGI Parameters : file=name.html, the name of the HTML file
1081 * (relative to user-manual from config)
1083 * Returns : JB_ERR_OK on success
1084 * JB_ERR_MEMORY on out-of-memory error.
1086 *********************************************************************/
1087 jb_err cgi_send_user_manual(struct client_state *csp,
1088 struct http_response *rsp,
1089 const struct map *parameters)
1091 const char * filename;
1093 jb_err err = JB_ERR_OK;
1100 if (!parameters->first)
1102 /* requested http://p.p/user-manual (without trailing slash) */
1103 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1106 get_string_param(parameters, "file", &filename);
1107 /* Check paramter for hack attempts */
1108 if (filename && strchr(filename, '/'))
1110 return JB_ERR_CGI_PARAMS;
1112 if (filename && strstr(filename, ".."))
1114 return JB_ERR_CGI_PARAMS;
1117 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1118 if (full_path == NULL)
1120 return JB_ERR_MEMORY;
1123 err = load_file(full_path, &rsp->body, &rsp->content_length);
1124 if (JB_ERR_OK != err)
1126 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1127 if (JB_ERR_FILE == err)
1129 err = cgi_error_no_template(csp, rsp, full_path);
1136 /* Guess correct Content-Type based on the filename's ending */
1139 length = strlen(filename);
1145 if((length>=4) && !strcmp(&filename[length-4], ".css"))
1147 err = enlist(rsp->headers, "Content-Type: text/css");
1149 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1151 err = enlist(rsp->headers, "Content-Type: image/jpeg");
1155 err = enlist(rsp->headers, "Content-Type: text/html");
1162 /*********************************************************************
1164 * Function : cgi_show_version
1166 * Description : CGI function that returns a a web page describing the
1167 * file versions of Privoxy.
1170 * 1 : csp = Current client state (buffers, headers, etc...)
1171 * 2 : rsp = http_response data structure for output
1172 * 3 : parameters = map of cgi parameters
1174 * CGI Parameters : none
1176 * Returns : JB_ERR_OK on success
1177 * JB_ERR_MEMORY on out-of-memory error.
1179 *********************************************************************/
1180 jb_err cgi_show_version(struct client_state *csp,
1181 struct http_response *rsp,
1182 const struct map *parameters)
1184 struct map *exports;
1190 if (NULL == (exports = default_exports(csp, "show-version")))
1192 return JB_ERR_MEMORY;
1195 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1198 return JB_ERR_MEMORY;
1201 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1205 /*********************************************************************
1207 * Function : cgi_show_status
1209 * Description : CGI function that returns a web page describing the
1210 * current status of Privoxy.
1213 * 1 : csp = Current client state (buffers, headers, etc...)
1214 * 2 : rsp = http_response data structure for output
1215 * 3 : parameters = map of cgi parameters
1218 * file : Which file to show. Only first letter is checked,
1223 * Default is to show menu and other information.
1225 * Returns : JB_ERR_OK on success
1226 * JB_ERR_MEMORY on out-of-memory error.
1228 *********************************************************************/
1229 jb_err cgi_show_status(struct client_state *csp,
1230 struct http_response *rsp,
1231 const struct map *parameters)
1237 char buf[BUFFER_SIZE];
1238 #ifdef FEATURE_STATISTICS
1239 float perc_rej; /* Percentage of http requests rejected */
1240 int local_urls_read;
1241 int local_urls_rejected;
1242 #endif /* ndef FEATURE_STATISTICS */
1243 jb_err err = JB_ERR_OK;
1245 struct map *exports;
1251 if ('\0' != *(lookup(parameters, "file")))
1253 return cgi_show_file(csp, rsp, parameters);
1256 if (NULL == (exports = default_exports(csp, "show-status")))
1258 return JB_ERR_MEMORY;
1262 for (j = 0; (s != NULL) && (j < Argc); j++)
1264 if (!err) err = string_join (&s, html_encode(Argv[j]));
1265 if (!err) err = string_append(&s, " ");
1267 if (!err) err = map(exports, "invocation", 1, s, 0);
1269 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1270 if (!err) err = show_defines(exports);
1275 return JB_ERR_MEMORY;
1278 #ifdef FEATURE_STATISTICS
1279 local_urls_read = urls_read;
1280 local_urls_rejected = urls_rejected;
1283 * Need to alter the stats not to include the fetch of this
1286 * Can't do following thread safely! doh!
1289 * urls_rejected--; * This will be incremented subsequently *
1292 if (local_urls_read == 0)
1294 if (!err) err = map_block_killer(exports, "have-stats");
1298 if (!err) err = map_block_killer(exports, "have-no-stats");
1300 perc_rej = (float)local_urls_rejected * 100.0F /
1301 (float)local_urls_read;
1303 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1304 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1306 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1307 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1309 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1310 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1313 #else /* ndef FEATURE_STATISTICS */
1314 err = err || map_block_killer(exports, "statistics");
1315 #endif /* ndef FEATURE_STATISTICS */
1318 * List all action files in use, together with view and edit links,
1319 * except for standard.action, which should only be viewable. (Not
1320 * enforced in the editor itself)
1321 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1324 for (i = 0; i < MAX_AF_FILES; i++)
1326 if (csp->actions_list[i] != NULL)
1328 if (!err) err = string_append(&s, "<tr><td>");
1329 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1330 snprintf(buf, sizeof(buf),
1331 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1332 if (!err) err = string_append(&s, buf);
1334 #ifdef FEATURE_CGI_EDIT_ACTIONS
1335 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1336 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1337 && (NULL != csp->config->actions_file_short[i]))
1340 if (access(csp->config->actions_file[i], W_OK) == 0)
1342 #endif /* def HAVE_ACCESS */
1343 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1344 if (!err) err = string_append(&s, buf);
1349 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1351 #endif /* def HAVE_ACCESS */
1355 if (!err) err = string_append(&s, "</td></tr>\n");
1360 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1364 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1368 * List all re_filterfiles in use, together with view options.
1369 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1372 for (i = 0; i < MAX_AF_FILES; i++)
1374 if (csp->rlist[i] != NULL)
1376 if (!err) err = string_append(&s, "<tr><td>");
1377 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1378 snprintf(buf, sizeof(buf),
1379 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1380 if (!err) err = string_append(&s, buf);
1381 if (!err) err = string_append(&s, "</td></tr>\n");
1386 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1390 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1391 if (!err) err = map_block_killer(exports, "have-filterfile");
1394 #ifdef FEATURE_TRUST
1397 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1401 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1402 if (!err) err = map_block_killer(exports, "have-trustfile");
1405 if (!err) err = map_block_killer(exports, "trust-support");
1406 #endif /* ndef FEATURE_TRUST */
1408 #ifdef FEATURE_CGI_EDIT_ACTIONS
1409 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1411 err = map_block_killer(exports, "cgi-editor-is-disabled");
1413 #endif /* ndef CGI_EDIT_ACTIONS */
1418 return JB_ERR_MEMORY;
1421 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1425 /*********************************************************************
1427 * Function : cgi_show_url_info
1429 * Description : CGI function that determines and shows which actions
1430 * Privoxy will perform for a given url, and which
1431 * matches starting from the defaults have lead to that.
1434 * 1 : csp = Current client state (buffers, headers, etc...)
1435 * 2 : rsp = http_response data structure for output
1436 * 3 : parameters = map of cgi parameters
1439 * url : The url whose actions are to be determined.
1440 * If url is unset, the url-given conditional will be
1441 * set, so that all but the form can be suppressed in
1444 * Returns : JB_ERR_OK on success
1445 * JB_ERR_MEMORY on out-of-memory error.
1447 *********************************************************************/
1448 jb_err cgi_show_url_info(struct client_state *csp,
1449 struct http_response *rsp,
1450 const struct map *parameters)
1453 struct map *exports;
1460 if (NULL == (exports = default_exports(csp, "show-url-info")))
1462 return JB_ERR_MEMORY;
1466 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1468 url_param = strdup(lookup(parameters, "url"));
1469 if (url_param == NULL)
1472 return JB_ERR_MEMORY;
1477 * Handle prefixes. 4 possibilities:
1478 * 1) "http://" or "https://" prefix present and followed by URL - OK
1479 * 2) Only the "http://" or "https://" part is present, no URL - change
1480 * to empty string so it will be detected later as "no URL".
1481 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1483 * 4) Parameter not specified or is empty string - let this fall through
1484 * for now, next block of code will handle it.
1486 if (0 == strncmp(url_param, "http://", 7))
1488 if (url_param[7] == '\0')
1491 * Empty URL (just prefix).
1492 * Make it totally empty so it's caught by the next if()
1494 url_param[0] = '\0';
1497 else if (0 == strncmp(url_param, "https://", 8))
1499 if (url_param[8] == '\0')
1502 * Empty URL (just prefix).
1503 * Make it totally empty so it's caught by the next if()
1505 url_param[0] = '\0';
1508 else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1510 /* No prefix - assume http:// */
1511 char *url_param_prefixed = strdup("http://");
1513 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1516 return JB_ERR_MEMORY;
1518 url_param = url_param_prefixed;
1522 * Hide "toggle off" warning if Privoxy is toggled on.
1525 #ifdef FEATURE_TOGGLE
1526 (global_toggle_state == 1) &&
1527 #endif /* def FEATURE_TOGGLE */
1528 map_block_killer(exports, "privoxy-is-toggled-off")
1532 return JB_ERR_MEMORY;
1535 if (url_param[0] == '\0')
1537 /* URL paramater not specified, display query form only. */
1539 if (map_block_killer(exports, "url-given")
1540 || map(exports, "url", 1, "", 1))
1543 return JB_ERR_MEMORY;
1548 /* Given a URL, so query it. */
1553 struct file_list *fl;
1554 struct url_actions *b;
1555 struct http_request url_to_query[1];
1556 struct current_action_spec action[1];
1559 if (map(exports, "url", 1, html_encode(url_param), 0))
1563 return JB_ERR_MEMORY;
1566 init_current_action(action);
1568 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1570 free_current_action(action);
1573 return JB_ERR_MEMORY;
1576 memset(url_to_query, '\0', sizeof(url_to_query));
1577 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1578 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1582 if (err == JB_ERR_MEMORY)
1584 free_http_request(url_to_query);
1585 free_current_action(action);
1587 return JB_ERR_MEMORY;
1593 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1594 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1595 if (!err) err = map_block_killer(exports, "valid-url");
1597 free_current_action(action);
1598 free_http_request(url_to_query);
1603 return JB_ERR_MEMORY;
1606 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1610 * We have a warning about SSL paths. Hide it for unencrypted sites.
1612 if (!url_to_query->ssl)
1614 if (map_block_killer(exports, "https"))
1616 free_current_action(action);
1618 free_http_request(url_to_query);
1619 return JB_ERR_MEMORY;
1623 matches = strdup("<table summary=\"\" class=\"transparent\">");
1625 for (i = 0; i < MAX_AF_FILES; i++)
1627 if (NULL == csp->config->actions_file_short[i]
1628 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1632 if ((fl = csp->actions_list[i]) != NULL)
1634 if ((b = fl->f) != NULL)
1636 /* FIXME: Hardcoded HTML! */
1637 string_append(&matches, "<tr><th>In file: ");
1638 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1639 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1640 string_append(&matches, buf);
1641 string_append(&matches, "View</a>");
1642 #ifdef FEATURE_CGI_EDIT_ACTIONS
1643 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1646 if (access(csp->config->actions_file[i], W_OK) == 0)
1648 #endif /* def HAVE_ACCESS */
1649 snprintf(buf, sizeof(buf),
1650 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1651 string_append(&matches, buf);
1652 string_append(&matches, "Edit</a>");
1657 string_append(&matches, " <strong>No write access.</strong>");
1659 #endif /* def HAVE_ACCESS */
1661 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1663 string_append(&matches, "</th></tr>\n");
1670 for (; (b != NULL) && (matches != NULL); b = b->next)
1672 if (url_match(b->url, url_to_query))
1674 string_append(&matches, "<tr><td>{");
1675 string_join (&matches, actions_to_html(csp, b->action));
1676 string_append(&matches, " }<br>\n<code>");
1677 string_join (&matches, html_encode(b->url->spec));
1678 string_append(&matches, "</code></td></tr>\n");
1680 if (merge_current_action(action, b->action))
1683 free_http_request(url_to_query);
1684 free_current_action(action);
1686 return JB_ERR_MEMORY;
1694 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1697 string_append(&matches, "</table>\n");
1700 * XXX: Kludge to make sure the "Forward settings" section
1701 * shows what forward-override{} would do with the requested URL.
1702 * No one really cares how the CGI request would be forwarded
1703 * if it wasn't intercepted as CGI request in the first place.
1705 * From here on the action bitmask will no longer reflect
1706 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1707 * but luckily it's no longer required later on anyway.
1709 free_current_action(csp->action);
1710 get_url_actions(csp, url_to_query);
1713 * Fill in forwarding settings.
1715 * The possibilities are:
1717 * - http forwarding only
1718 * - socks4(a) forwarding only
1719 * - socks4(a) and http forwarding.
1721 * XXX: Parts of this code could be reused for the
1722 * "forwarding-failed" template which currently doesn't
1723 * display the proxy port and an eventual second forwarder.
1726 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1728 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1730 if (!err) err = map_block_killer(exports, "socks-forwarder");
1731 if (!err) err = map_block_killer(exports, "http-forwarder");
1735 char port[10]; /* We save proxy ports as int but need a string here */
1737 if (!err) err = map_block_killer(exports, "no-forwarder");
1739 if (fwd->gateway_host != NULL)
1741 char *socks_type = NULL;
1746 socks_type = "socks4";
1749 socks_type = "socks4a";
1752 socks_type = "socks5";
1755 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1758 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1759 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1760 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1761 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1765 if (!err) err = map_block_killer(exports, "socks-forwarder");
1768 if (fwd->forward_host != NULL)
1770 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1771 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1772 if (!err) err = map(exports, "forward-port", 1, port, 1);
1776 if (!err) err = map_block_killer(exports, "http-forwarder");
1781 free_http_request(url_to_query);
1783 if (err || matches == NULL)
1785 free_current_action(action);
1787 return JB_ERR_MEMORY;
1790 #ifdef FEATURE_CGI_EDIT_ACTIONS
1791 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1793 err = map_block_killer(exports, "cgi-editor-is-disabled");
1795 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1798 * If zlib support is available, if no content filters
1799 * are enabled or if the prevent-compression action is enabled,
1800 * suppress the "compression could prevent filtering" warning.
1802 #ifndef FEATURE_ZLIB
1803 if (!content_filters_enabled(action) ||
1804 (action->flags & ACTION_NO_COMPRESSION))
1807 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1810 if (err || map(exports, "matches", 1, matches , 0))
1812 free_current_action(action);
1814 return JB_ERR_MEMORY;
1817 s = current_action_to_html(csp, action);
1819 free_current_action(action);
1821 if (map(exports, "final", 1, s, 0))
1824 return JB_ERR_MEMORY;
1828 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1832 /*********************************************************************
1834 * Function : cgi_robots_txt
1836 * Description : CGI function to return "/robots.txt".
1839 * 1 : csp = Current client state (buffers, headers, etc...)
1840 * 2 : rsp = http_response data structure for output
1841 * 3 : parameters = map of cgi parameters
1843 * CGI Parameters : None
1845 * Returns : JB_ERR_OK on success
1846 * JB_ERR_MEMORY on out-of-memory error.
1848 *********************************************************************/
1849 jb_err cgi_robots_txt(struct client_state *csp,
1850 struct http_response *rsp,
1851 const struct map *parameters)
1860 "# This is the Privoxy control interface.\n"
1861 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1867 if (rsp->body == NULL)
1869 return JB_ERR_MEMORY;
1872 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1876 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1877 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1879 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1883 /*********************************************************************
1885 * Function : show_defines
1887 * Description : Add to a map the state od all conditional #defines
1888 * used when building
1891 * 1 : exports = map to extend
1893 * Returns : JB_ERR_OK on success
1894 * JB_ERR_MEMORY on out-of-memory error.
1896 *********************************************************************/
1897 static jb_err show_defines(struct map *exports)
1899 jb_err err = JB_ERR_OK;
1902 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1903 #else /* ifndef FEATURE_ACL */
1904 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1905 #endif /* ndef FEATURE_ACL */
1907 #ifdef FEATURE_CGI_EDIT_ACTIONS
1908 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1909 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1910 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1911 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1913 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1914 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1915 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1916 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1917 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1919 #ifdef FEATURE_FAST_REDIRECTS
1920 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1921 #else /* ifndef FEATURE_FAST_REDIRECTS */
1922 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1923 #endif /* ndef FEATURE_FAST_REDIRECTS */
1925 #ifdef FEATURE_FORCE_LOAD
1926 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1927 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1928 #else /* ifndef FEATURE_FORCE_LOAD */
1929 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1930 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1931 #endif /* ndef FEATURE_FORCE_LOAD */
1933 #ifdef FEATURE_GRACEFUL_TERMINATION
1934 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1935 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1936 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1937 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1939 #ifdef FEATURE_IMAGE_BLOCKING
1940 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1941 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1942 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1943 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1945 #ifdef FEATURE_IMAGE_DETECT_MSIE
1946 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1947 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1948 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1949 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1952 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1953 #else /* ifndef HAVE_RFC2553 */
1954 if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1955 #endif /* ndef HAVE_RFC2553 */
1957 #ifdef FEATURE_NO_GIFS
1958 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1959 #else /* ifndef FEATURE_NO_GIFS */
1960 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1961 #endif /* ndef FEATURE_NO_GIFS */
1963 #ifdef FEATURE_PTHREAD
1964 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1965 #else /* ifndef FEATURE_PTHREAD */
1966 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1967 #endif /* ndef FEATURE_PTHREAD */
1969 #ifdef FEATURE_STATISTICS
1970 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1971 #else /* ifndef FEATURE_STATISTICS */
1972 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1973 #endif /* ndef FEATURE_STATISTICS */
1975 #ifdef FEATURE_TOGGLE
1976 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1977 #else /* ifndef FEATURE_TOGGLE */
1978 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1979 #endif /* ndef FEATURE_TOGGLE */
1981 #ifdef FEATURE_TRUST
1982 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1983 #else /* ifndef FEATURE_TRUST */
1984 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1985 #endif /* ndef FEATURE_TRUST */
1988 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1989 #else /* ifndef FEATURE_ZLIB */
1990 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1991 #endif /* ndef FEATURE_ZLIB */
1994 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1995 #else /* ifndef STATIC_PCRE */
1996 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1997 #endif /* ndef STATIC_PCRE */
2000 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
2001 #else /* ifndef STATIC_PCRS */
2002 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
2003 #endif /* ndef STATIC_PCRS */
2009 /*********************************************************************
2011 * Function : show_rcs
2013 * Description : Create a string with the rcs info for all sourcefiles
2017 * Returns : A string, or NULL on out-of-memory.
2019 *********************************************************************/
2020 static char *show_rcs(void)
2022 char *result = strdup("");
2023 char buf[BUFFER_SIZE];
2025 /* Instead of including *all* dot h's in the project (thus creating a
2026 * tremendous amount of dependencies), I will concede to declaring them
2027 * as extern's. This forces the developer to add to this list, but oh well.
2030 #define SHOW_RCS(__x) \
2032 extern const char __x[]; \
2033 snprintf(buf, sizeof(buf), " %s\n", __x); \
2034 string_append(&result, buf); \
2037 /* In alphabetical order */
2038 SHOW_RCS(actions_h_rcs)
2039 SHOW_RCS(actions_rcs)
2041 SHOW_RCS(amiga_h_rcs)
2043 #endif /* def AMIGA */
2046 #ifdef FEATURE_CGI_EDIT_ACTIONS
2047 SHOW_RCS(cgiedit_h_rcs)
2048 SHOW_RCS(cgiedit_rcs)
2049 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
2050 SHOW_RCS(cgisimple_h_rcs)
2051 SHOW_RCS(cgisimple_rcs)
2053 SHOW_RCS(cygwin_h_rcs)
2055 SHOW_RCS(deanimate_h_rcs)
2056 SHOW_RCS(deanimate_rcs)
2057 SHOW_RCS(encode_h_rcs)
2058 SHOW_RCS(encode_rcs)
2059 SHOW_RCS(errlog_h_rcs)
2060 SHOW_RCS(errlog_rcs)
2061 SHOW_RCS(filters_h_rcs)
2062 SHOW_RCS(filters_rcs)
2063 SHOW_RCS(gateway_h_rcs)
2064 SHOW_RCS(gateway_rcs)
2065 SHOW_RCS(jbsockets_h_rcs)
2066 SHOW_RCS(jbsockets_rcs)
2069 SHOW_RCS(list_h_rcs)
2071 SHOW_RCS(loadcfg_h_rcs)
2072 SHOW_RCS(loadcfg_rcs)
2073 SHOW_RCS(loaders_h_rcs)
2074 SHOW_RCS(loaders_rcs)
2075 SHOW_RCS(miscutil_h_rcs)
2076 SHOW_RCS(miscutil_rcs)
2077 SHOW_RCS(parsers_h_rcs)
2078 SHOW_RCS(parsers_rcs)
2080 SHOW_RCS(pcrs_h_rcs)
2081 SHOW_RCS(project_h_rcs)
2082 SHOW_RCS(ssplit_h_rcs)
2083 SHOW_RCS(ssplit_rcs)
2084 SHOW_RCS(urlmatch_h_rcs)
2085 SHOW_RCS(urlmatch_rcs)
2087 #ifndef _WIN_CONSOLE
2088 SHOW_RCS(w32log_h_rcs)
2089 SHOW_RCS(w32log_rcs)
2090 SHOW_RCS(w32res_h_rcs)
2091 SHOW_RCS(w32taskbar_h_rcs)
2092 SHOW_RCS(w32taskbar_rcs)
2093 #endif /* ndef _WIN_CONSOLE */
2094 SHOW_RCS(win32_h_rcs)
2096 #endif /* def _WIN32 */
2105 /*********************************************************************
2107 * Function : cgi_show_file
2109 * Description : CGI function that shows the content of a
2110 * configuration file.
2113 * 1 : csp = Current client state (buffers, headers, etc...)
2114 * 2 : rsp = http_response data structure for output
2115 * 3 : parameters = map of cgi parameters
2118 * file : Which file to show. Only first letter is checked,
2123 * Default is to show menu and other information.
2125 * Returns : JB_ERR_OK on success
2126 * JB_ERR_MEMORY on out-of-memory error.
2128 *********************************************************************/
2129 static jb_err cgi_show_file(struct client_state *csp,
2130 struct http_response *rsp,
2131 const struct map *parameters)
2134 const char * filename = NULL;
2135 char * file_description = NULL;
2141 switch (*(lookup(parameters, "file")))
2144 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2146 filename = csp->actions_list[i]->filename;
2147 file_description = "Actions File";
2152 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2154 filename = csp->rlist[i]->filename;
2155 file_description = "Filter File";
2159 #ifdef FEATURE_TRUST
2163 filename = csp->tlist->filename;
2164 file_description = "Trust File";
2167 #endif /* def FEATURE_TRUST */
2170 if (NULL != filename)
2172 struct map *exports;
2177 exports = default_exports(csp, "show-status");
2178 if (NULL == exports)
2180 return JB_ERR_MEMORY;
2183 if ( map(exports, "file-description", 1, file_description, 1)
2184 || map(exports, "filepath", 1, html_encode(filename), 0) )
2187 return JB_ERR_MEMORY;
2190 err = load_file(filename, &s, &length);
2191 if (JB_ERR_OK != err)
2193 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2196 return JB_ERR_MEMORY;
2201 s = html_encode_and_free_original(s);
2204 return JB_ERR_MEMORY;
2207 if (map(exports, "contents", 1, s, 0))
2210 return JB_ERR_MEMORY;
2214 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2217 return JB_ERR_CGI_PARAMS;
2221 /*********************************************************************
2223 * Function : load_file
2225 * Description : Loads a file into a buffer.
2228 * 1 : filename = Name of the file to be loaded.
2229 * 2 : buffer = Used to return the file's content.
2230 * 3 : length = Used to return the size of the file.
2232 * Returns : JB_ERR_OK in case of success,
2233 * JB_ERR_FILE in case of ordinary file loading errors
2234 * (fseek() and ftell() errors are fatal)
2235 * JB_ERR_MEMORY in case of out-of-memory.
2237 *********************************************************************/
2238 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2242 jb_err err = JB_ERR_OK;
2244 fp = fopen(filename, "rb");
2250 /* Get file length */
2251 if (fseek(fp, 0, SEEK_END))
2253 log_error(LOG_LEVEL_FATAL,
2254 "Unexpected error while fseek()ing to the end of %s: %E",
2260 log_error(LOG_LEVEL_FATAL,
2261 "Unexpected ftell() error while loading %s: %E",
2264 *length = (size_t)ret;
2266 /* Go back to the beginning. */
2267 if (fseek(fp, 0, SEEK_SET))
2269 log_error(LOG_LEVEL_FATAL,
2270 "Unexpected error while fseek()ing to the beginning of %s: %E",
2274 *buffer = (char *)zalloc(*length + 1);
2275 if (NULL == *buffer)
2277 err = JB_ERR_MEMORY;
2279 else if (!fread(*buffer, *length, 1, fp))
2282 * May happen if the file size changes between fseek() and
2283 * fread(). If it does, we just log it and serve what we got.
2285 log_error(LOG_LEVEL_ERROR,
2286 "Couldn't completely read file %s.", filename);