1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.84 2008/05/26 16:16:55 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.84 2008/05/26 16:16:55 fabiankeil
40 * Spell error correctly.
42 * Revision 1.83 2008/05/12 14:51:30 fabiankeil
43 * Don't complain about an invalid URL if show-url-info is requested
44 * without parameters. Regression introduced in 1.81 by yours truly.
46 * Revision 1.82 2008/05/10 20:01:47 fabiankeil
47 * Fix an assertion that could erroneously
48 * trigger in case of memory shortage.
50 * Revision 1.81 2008/05/05 09:54:39 fabiankeil
51 * In cgi_show_url_info(), make sure ftp URLs are
52 * declared invalid. Also simplify the code that adds
53 * "http://" if no protocol has been specified.
55 * Revision 1.80 2008/05/04 16:18:32 fabiankeil
56 * Provide parse_http_url() with a third parameter to specify
57 * whether or not URLs without protocol are acceptable.
59 * Revision 1.79 2008/05/04 13:30:56 fabiankeil
60 * Streamline parse_http_url()'s prototype.
62 * Revision 1.78 2008/05/03 16:50:11 fabiankeil
63 * Leverage content_filters_enabled() in cgi_show_url_info().
65 * Revision 1.77 2008/05/02 09:47:48 fabiankeil
66 * In cgi_show_url_info, pass an initialized http structure
67 * to parse_http_url() as that will be required soonish and
68 * assert that https URLs are recognized correctly.
70 * Revision 1.76 2008/04/28 09:13:30 fabiankeil
71 * In load_file(), remember the error reason and fclose()
72 * and return later on instead of right away.
74 * Revision 1.75 2008/04/27 13:52:52 fabiankeil
75 * Move CGI file loading code into load_file() and
76 * add checks for unexpected errors.
78 * Revision 1.74 2008/04/26 15:50:56 fabiankeil
79 * Fix macro name in cgi_show_file() error path.
81 * Revision 1.73 2008/04/26 12:21:55 fabiankeil
82 * Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
84 * Revision 1.72 2008/04/26 10:34:15 fabiankeil
85 * If zlib support is unavailable and there are content filters active
86 * but the prevent-compression action is disabled, include a warning
87 * on the show-url-info page that compression might prevent filtering.
89 * Revision 1.71 2008/04/25 13:33:56 fabiankeil
90 * - Factor cgi_show_file() out of cgi_show_status().
91 * - Adjust cgi_show_status()'s parameter description to match reality.
93 * Revision 1.70 2008/04/24 16:12:38 fabiankeil
94 * In cgi_show_status(), load the requested file at once.
95 * Using string_join() for every line really doesn't scale.
97 * Revision 1.69 2008/04/17 14:40:48 fabiankeil
98 * Provide get_http_time() with the buffer size so it doesn't
99 * have to blindly assume that the buffer is big enough.
101 * Revision 1.68 2008/03/27 18:27:21 fabiankeil
102 * Remove kill-popups action.
104 * Revision 1.67 2008/03/27 17:00:05 fabiankeil
105 * Turn the favicon blobs into locals.
107 * Revision 1.66 2008/02/23 16:57:12 fabiankeil
108 * Rename url_actions() to get_url_actions() and let it
109 * use the standard parameter ordering.
111 * Revision 1.65 2008/02/23 16:33:43 fabiankeil
112 * Let forward_url() use the standard parameter ordering
113 * and mark its second parameter immutable.
115 * Revision 1.64 2008/02/03 13:56:07 fabiankeil
116 * Add SOCKS5 support for show-url-info CGI page.
118 * Revision 1.63 2008/02/01 06:04:31 fabiankeil
119 * If edit buttons on the show-url-info CGI page are hidden, explain why.
121 * Revision 1.62 2008/02/01 05:52:40 fabiankeil
122 * Hide edit buttons on the show-url-info CGI page if enable-edit-action
123 * is disabled. Patch by Lee with additional white space adjustments.
125 * Revision 1.61 2008/01/26 11:13:25 fabiankeil
126 * If enable-edit-actions is disabled, hide the edit buttons and explain why.
128 * Revision 1.60 2007/10/27 13:12:13 fabiankeil
129 * Finish 1.49 and check write access before
130 * showing edit buttons on show-url-info page.
132 * Revision 1.59 2007/10/19 16:42:36 fabiankeil
133 * Plug memory leak I introduced five months ago.
134 * Yay Valgrind and Privoxy-Regression-Test.
136 * Revision 1.58 2007/07/21 12:19:50 fabiankeil
137 * If show-url-info is called with an URL that Privoxy
138 * would reject as invalid, don't show unresolved forwarding
139 * variables, "final matches" or claim the site's secure.
141 * Revision 1.57 2007/06/01 16:53:05 fabiankeil
142 * Adjust cgi_show_url_info() to show what forward-override{}
143 * would do with the requested URL (instead of showing how the
144 * request for the CGI page would be forwarded if it wasn't a
147 * Revision 1.56 2007/05/21 10:50:35 fabiankeil
148 * - Use strlcpy() instead of strcpy().
149 * - Stop treating actions files special. Expect a complete file name
150 * (with or without path) like it's done for the rest of the files.
152 * - Don't rerun sed() in cgi_show_request().
154 * Revision 1.55 2007/04/13 13:36:46 fabiankeil
155 * Reference action files in CGI URLs by id instead
156 * of using the first part of the file name.
157 * Fixes BR 1694250 and BR 1590556.
159 * Revision 1.54 2007/04/09 18:11:35 fabiankeil
160 * Don't mistake VC++'s _snprintf() for a snprintf() replacement.
162 * Revision 1.53 2007/04/08 13:21:04 fabiankeil
163 * Reference action files in CGI URLs by id instead
164 * of using the first part of the file name.
165 * Fixes BR 1694250 and BR 1590556.
167 * Revision 1.52 2007/02/13 15:10:26 fabiankeil
168 * Apparently fopen()ing in "binary" mode doesn't require
169 * #ifdefs, it's already done without them in cgiedit.c.
171 * Revision 1.51 2007/02/10 16:55:22 fabiankeil
172 * - Show forwarding settings on the show-url-info page
173 * - Fix some HTML syntax errors.
175 * Revision 1.50 2007/01/23 15:51:17 fabiankeil
176 * Add favicon delivery functions.
178 * Revision 1.49 2007/01/20 16:29:38 fabiankeil
179 * Suppress edit buttons for action files if Privoxy has
180 * no write access. Suggested by Roland in PR 1564026.
182 * Revision 1.48 2007/01/20 15:31:31 fabiankeil
183 * Display warning if show-url-info CGI page
184 * is used while Privoxy is toggled off.
186 * Revision 1.47 2007/01/12 15:07:10 fabiankeil
187 * Use zalloc in cgi_send_user_manual.
189 * Revision 1.46 2007/01/02 12:49:46 fabiankeil
190 * Add FEATURE_ZLIB to the list of conditional
191 * defines at the show-status page.
193 * Revision 1.45 2006/12/28 18:16:41 fabiankeil
194 * Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
195 * body memory before using it, replaced sprintf calls with snprintf.
197 * Revision 1.44 2006/12/22 14:19:27 fabiankeil
198 * Removed checks whether or not AF_FILES have
199 * data structures associated with them in cgi_show_status.
200 * It doesn't matter as we're only interested in the file names.
202 * For the action files the checks were always true,
203 * but they prevented empty filter files from being
204 * listed. Fixes parts of BR 1619208.
206 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
207 * - Added FEATURE_GRACEFUL_TERMINATION to the
208 * "conditional #defines" section
209 * - Escaped ampersands in generated HTML.
210 * - Renamed re-filter-filename to re-filter-filenames
212 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
213 * Add special treatment for WIN32 to make sure
214 * cgi_send_user_manual opens the files in binary mode.
215 * Fixes BR 1600411 and unbreaks image delivery.
217 * Remove outdated comment.
219 * Revision 1.41 2006/10/09 19:18:28 roro
220 * Redirect http://p.p/user-manual (without trailing slash) to
221 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
223 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
224 * Modified cgi_send_user_manual to serve binary
225 * content without destroying it first. Should also be
226 * faster now. Added ".jpg" check for Content-Type guessing.
228 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
229 * Deliver documents in the user-manual directory
230 * with "Content-Type text/css" if their filename
233 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
234 * Incorporate modified version of Roland Rosenfeld's patch to
235 * optionally access the user-manual via Privoxy. Closes patch 679075.
237 * Formatting changed to Privoxy style, added call to
238 * cgi_error_no_template if the requested file doesn't
239 * exist and modified check whether or not Privoxy itself
240 * should serve the manual. Should work cross-platform now.
242 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
243 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
244 * with what was really the latest development (the v_3_0_branch branch)
246 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
247 * Multiple filter file support
249 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
250 * Undo some damaging memory leak patches
252 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
253 * A few memory leaks plugged (mostly on error paths)
255 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
256 * Another instance of:
257 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
258 * Thanks to Magnus Holmgren for the patch
260 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
261 * - Prevent line wrap beween "View/Edit" link buttons on status page
262 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
264 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
265 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
267 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
268 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
270 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
271 * Applying patch from Andreas as provided by Hal on the list.
272 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
274 * Revision 1.35 2002/05/12 21:44:44 jongfoster
275 * Adding amiga.[ch] revision information, if on an amiga.
277 * Revision 1.34 2002/04/30 12:06:12 oes
278 * Deleted unused code from default_cgi
280 * Revision 1.33 2002/04/30 11:14:52 oes
281 * Made csp the first parameter in *action_to_html
283 * Revision 1.32 2002/04/26 18:29:13 jongfoster
284 * Fixing this Visual C++ warning:
285 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
287 * Revision 1.31 2002/04/26 12:54:36 oes
288 * - Kill obsolete REDIRECT_URL code
289 * - Error handling fixes
290 * - Style sheet related HTML snipplet changes
291 * - cgi_show_url_info:
292 * - Matches now in table, actions on single lines,
294 * - standard.action suppressed
295 * - Buttons to View and Edit AFs
297 * Revision 1.30 2002/04/24 02:18:08 oes
298 * - show-status is now the starting point for editing
299 * the actions files, generate list of all AFs with buttons
300 * for viewing and editing, new look for file list (Jon:
301 * buttons now aligned ;-P ), view mode now supports multiple
302 * AFs, name changes, no view links for unspecified files,
303 * no edit link for standard.action.
305 * - Jon's multiple AF patch: cgi_show_url_info now uses all
306 * AFs and marks the output accordingly
308 * Revision 1.29 2002/04/10 13:38:35 oes
309 * load_template signature changed
311 * Revision 1.28 2002/04/07 15:42:12 jongfoster
312 * Fixing send-banner?type=auto when the image-blocker is
313 * a redirect to send-banner
315 * Revision 1.27 2002/04/05 15:50:48 oes
316 * added send-stylesheet CGI
318 * Revision 1.26 2002/04/04 00:36:36 gliptak
319 * always use pcre for matching
321 * Revision 1.25 2002/04/03 22:28:03 gliptak
322 * Removed references to gnu_regex
324 * Revision 1.24 2002/04/02 16:12:47 oes
325 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
327 * Revision 1.23 2002/03/26 22:29:54 swa
328 * we have a new homepage!
330 * Revision 1.22 2002/03/24 16:18:15 jongfoster
333 * Revision 1.21 2002/03/24 15:23:33 jongfoster
336 * Revision 1.20 2002/03/24 13:25:43 swa
337 * name change related issues
339 * Revision 1.19 2002/03/16 23:54:06 jongfoster
340 * Adding graceful termination feature, to help look for memory leaks.
341 * If you enable this (which, by design, has to be done by hand
342 * editing config.h) and then go to http://i.j.b/die, then the program
343 * will exit cleanly after the *next* request. It should free all the
344 * memory that was used.
346 * Revision 1.18 2002/03/12 01:44:49 oes
347 * Changed default for "blocked" image from jb logo to checkboard pattern
349 * Revision 1.17 2002/03/08 16:43:18 oes
350 * Added choice beween GIF and PNG built-in images
352 * Revision 1.16 2002/03/07 03:48:38 oes
353 * - Changed built-in images from GIF to PNG
354 * (with regard to Unisys patent issue)
355 * - Added a 4x4 pattern PNG which is less intrusive
356 * than the logo but also clearly marks the deleted banners
358 * Revision 1.15 2002/03/06 22:54:35 jongfoster
359 * Automated function-comment nitpicking.
361 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
362 * Clean up a little CRLF unpleasantness that suddenly appeared
364 * Revision 1.13 2002/02/21 00:10:37 jongfoster
365 * Adding send-banner?type=auto option
367 * Revision 1.12 2002/01/23 01:03:32 jongfoster
368 * Fixing gcc [CygWin] compiler warnings
370 * Revision 1.11 2002/01/23 00:01:04 jongfoster
371 * Adding cgi_transparent_gif() for http://i.j.b/t
372 * Adding missing html_encode() to many CGI functions.
373 * Adding urlmatch.[ch] to http://i.j.b/show-version
375 * Revision 1.10 2002/01/17 21:10:37 jongfoster
376 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
377 * Also fixing a problem in the same function with improperly quoted URLs
378 * in output HTML, and adding code to handle https:// URLs correctly.
380 * Revision 1.9 2001/11/30 23:09:15 jongfoster
381 * Now reports on FEATURE_CGI_EDIT_ACTIONS
382 * Removing FEATURE_DENY_GZIP from template
384 * Revision 1.8 2001/11/13 00:14:07 jongfoster
385 * Fixing stupid bug now I've figured out what || means.
386 * (It always returns 0 or 1, not one of it's paramaters.)
388 * Revision 1.7 2001/10/23 21:48:19 jongfoster
389 * Cleaning up error handling in CGI functions - they now send back
390 * a HTML error page and should never cause a FATAL error. (Fixes one
391 * potential source of "denial of service" attacks).
393 * CGI actions file editor that works and is actually useful.
395 * Ability to toggle JunkBuster remotely using a CGI call.
397 * You can turn off both the above features in the main configuration
398 * file, e.g. if you are running a multi-user proxy.
400 * Revision 1.6 2001/10/14 22:00:32 jongfoster
401 * Adding support for a 404 error when an invalid CGI page is requested.
403 * Revision 1.5 2001/10/07 15:30:41 oes
404 * Removed FEATURE_DENY_GZIP
406 * Revision 1.4 2001/10/02 15:31:12 oes
407 * Introduced show-request cgi
409 * Revision 1.3 2001/09/22 16:34:44 jongfoster
410 * Removing unneeded #includes
412 * Revision 1.2 2001/09/19 18:01:11 oes
413 * Fixed comments; cosmetics
415 * Revision 1.1 2001/09/16 17:08:54 jongfoster
416 * Moving simple CGI functions from cgi.c to new file cgisimple.c
419 **********************************************************************/
425 #include <sys/types.h>
433 #endif /* def HAVE_ACCESS */
437 #include "cgisimple.h"
443 #include "miscutil.h"
446 #include "urlmatch.h"
449 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
451 static char *show_rcs(void);
452 static jb_err show_defines(struct map *exports);
453 static jb_err cgi_show_file(struct client_state *csp,
454 struct http_response *rsp,
455 const struct map *parameters);
456 static jb_err load_file(const char *filename, char **buffer, size_t *length);
458 /*********************************************************************
460 * Function : cgi_default
462 * Description : CGI function that is called for the CGI_SITE_1_HOST
463 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
464 * Boring - only exports the default exports.
467 * 1 : csp = Current client state (buffers, headers, etc...)
468 * 2 : rsp = http_response data structure for output
469 * 3 : parameters = map of cgi parameters
471 * CGI Parameters : none
473 * Returns : JB_ERR_OK on success
474 * JB_ERR_MEMORY on out-of-memory
476 *********************************************************************/
477 jb_err cgi_default(struct client_state *csp,
478 struct http_response *rsp,
479 const struct map *parameters)
486 if (NULL == (exports = default_exports(csp, "")))
488 return JB_ERR_MEMORY;
491 return template_fill_for_cgi(csp, "default", exports, rsp);
495 /*********************************************************************
497 * Function : cgi_error_404
499 * Description : CGI function that is called if an unknown action was
503 * 1 : csp = Current client state (buffers, headers, etc...)
504 * 2 : rsp = http_response data structure for output
505 * 3 : parameters = map of cgi parameters
507 * CGI Parameters : none
509 * Returns : JB_ERR_OK on success
510 * JB_ERR_MEMORY on out-of-memory error.
512 *********************************************************************/
513 jb_err cgi_error_404(struct client_state *csp,
514 struct http_response *rsp,
515 const struct map *parameters)
523 if (NULL == (exports = default_exports(csp, NULL)))
525 return JB_ERR_MEMORY;
528 rsp->status = strdup("404 Privoxy configuration page not found");
529 if (rsp->status == NULL)
532 return JB_ERR_MEMORY;
535 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
539 #ifdef FEATURE_GRACEFUL_TERMINATION
540 /*********************************************************************
544 * Description : CGI function to shut down Privoxy.
545 * NOTE: Turning this on in a production build
546 * would be a BAD idea. An EXTREMELY BAD idea.
547 * In short, don't do it.
550 * 1 : csp = Current client state (buffers, headers, etc...)
551 * 2 : rsp = http_response data structure for output
552 * 3 : parameters = map of cgi parameters
554 * CGI Parameters : none
556 * Returns : JB_ERR_OK on success
557 * JB_ERR_MEMORY on out-of-memory error.
559 *********************************************************************/
560 jb_err cgi_die (struct client_state *csp,
561 struct http_response *rsp,
562 const struct map *parameters)
572 * I don't really care what gets sent back to the browser.
573 * Take the easy option - "out of memory" page.
576 return JB_ERR_MEMORY;
578 #endif /* def FEATURE_GRACEFUL_TERMINATION */
581 /*********************************************************************
583 * Function : cgi_show_request
585 * Description : Show the client's request and what sed() would have
589 * 1 : csp = Current client state (buffers, headers, etc...)
590 * 2 : rsp = http_response data structure for output
591 * 3 : parameters = map of cgi parameters
593 * CGI Parameters : none
595 * Returns : JB_ERR_OK on success
596 * JB_ERR_MEMORY on out-of-memory error.
598 *********************************************************************/
599 jb_err cgi_show_request(struct client_state *csp,
600 struct http_response *rsp,
601 const struct map *parameters)
610 if (NULL == (exports = default_exports(csp, "show-request")))
612 return JB_ERR_MEMORY;
616 * Repair the damage done to the IOB by get_header()
618 for (p = csp->iob->buf; p < csp->iob->eod; p++)
620 if (*p == '\0') *p = '\n';
624 * Export the original client's request and the one we would
625 * be sending to the server if this wasn't a CGI call
628 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
631 return JB_ERR_MEMORY;
634 if (map(exports, "processed-request", 1,
635 html_encode_and_free_original(list_to_text(csp->headers)), 0))
638 return JB_ERR_MEMORY;
641 return template_fill_for_cgi(csp, "show-request", exports, rsp);
645 /*********************************************************************
647 * Function : cgi_send_banner
649 * Description : CGI function that returns a banner.
652 * 1 : csp = Current client state (buffers, headers, etc...)
653 * 2 : rsp = http_response data structure for output
654 * 3 : parameters = map of cgi parameters
657 * type : Selects the type of banner between "trans", "logo",
658 * and "auto". Defaults to "logo" if absent or invalid.
659 * "auto" means to select as if we were image-blocking.
660 * (Only the first character really counts; b and t are
663 * Returns : JB_ERR_OK on success
664 * JB_ERR_MEMORY on out-of-memory error.
666 *********************************************************************/
667 jb_err cgi_send_banner(struct client_state *csp,
668 struct http_response *rsp,
669 const struct map *parameters)
671 char imagetype = lookup(parameters, "type")[0];
674 * If type is auto, then determine the right thing
675 * to do from the set-image-blocker action
677 if (imagetype == 'a')
684 #ifdef FEATURE_IMAGE_BLOCKING
685 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
687 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
688 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
689 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
693 /* Use default - nothing to do here. */
695 else if (0 == strcmpic(p, "blank"))
699 else if (0 == strcmpic(p, "pattern"))
705 * If the action is to call this CGI, determine
708 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
710 imagetype = p[sizeof(prefix1) - 1];
712 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
714 imagetype = p[sizeof(prefix2) - 1];
718 * Everything else must (should) be a URL to
726 #endif /* def FEATURE_IMAGE_BLOCKING */
730 * Now imagetype is either the non-auto type we were called with,
731 * or it was auto and has since been determined. In any case, we
732 * can proceed to actually answering the request by sending a redirect
733 * or an image as appropriate:
735 if (imagetype == 'r')
737 rsp->status = strdup("302 Local Redirect from Privoxy");
738 if (rsp->status == NULL)
740 return JB_ERR_MEMORY;
742 if (enlist_unique_header(rsp->headers, "Location",
743 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
745 return JB_ERR_MEMORY;
750 if ((imagetype == 'b') || (imagetype == 't'))
752 rsp->body = bindup(image_blank_data, image_blank_length);
753 rsp->content_length = image_blank_length;
757 rsp->body = bindup(image_pattern_data, image_pattern_length);
758 rsp->content_length = image_pattern_length;
761 if (rsp->body == NULL)
763 return JB_ERR_MEMORY;
765 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
767 return JB_ERR_MEMORY;
778 /*********************************************************************
780 * Function : cgi_transparent_image
782 * Description : CGI function that sends a 1x1 transparent image.
785 * 1 : csp = Current client state (buffers, headers, etc...)
786 * 2 : rsp = http_response data structure for output
787 * 3 : parameters = map of cgi parameters
789 * CGI Parameters : None
791 * Returns : JB_ERR_OK on success
792 * JB_ERR_MEMORY on out-of-memory error.
794 *********************************************************************/
795 jb_err cgi_transparent_image(struct client_state *csp,
796 struct http_response *rsp,
797 const struct map *parameters)
799 rsp->body = bindup(image_blank_data, image_blank_length);
800 rsp->content_length = image_blank_length;
802 if (rsp->body == NULL)
804 return JB_ERR_MEMORY;
807 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
809 return JB_ERR_MEMORY;
819 /*********************************************************************
821 * Function : cgi_send_default_favicon
823 * Description : CGI function that sends the standard favicon.
826 * 1 : csp = Current client state (buffers, headers, etc...)
827 * 2 : rsp = http_response data structure for output
828 * 3 : parameters = map of cgi parameters
830 * CGI Parameters : None
832 * Returns : JB_ERR_OK on success
833 * JB_ERR_MEMORY on out-of-memory error.
835 *********************************************************************/
836 jb_err cgi_send_default_favicon(struct client_state *csp,
837 struct http_response *rsp,
838 const struct map *parameters)
840 static const char default_favicon_data[] =
841 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
842 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
843 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
844 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
845 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
846 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
847 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
848 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
849 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
850 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
851 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
852 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
853 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
855 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
857 rsp->body = bindup(default_favicon_data, favicon_length);
858 rsp->content_length = favicon_length;
860 if (rsp->body == NULL)
862 return JB_ERR_MEMORY;
865 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
867 return JB_ERR_MEMORY;
877 /*********************************************************************
879 * Function : cgi_send_error_favicon
881 * Description : CGI function that sends the favicon for error pages.
884 * 1 : csp = Current client state (buffers, headers, etc...)
885 * 2 : rsp = http_response data structure for output
886 * 3 : parameters = map of cgi parameters
888 * CGI Parameters : None
890 * Returns : JB_ERR_OK on success
891 * JB_ERR_MEMORY on out-of-memory error.
893 *********************************************************************/
894 jb_err cgi_send_error_favicon(struct client_state *csp,
895 struct http_response *rsp,
896 const struct map *parameters)
898 static const char error_favicon_data[] =
899 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
900 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
901 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
902 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
903 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
904 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
905 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
906 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
907 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
908 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
909 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
910 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
911 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
913 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
915 rsp->body = bindup(error_favicon_data, favicon_length);
916 rsp->content_length = favicon_length;
918 if (rsp->body == NULL)
920 return JB_ERR_MEMORY;
923 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
925 return JB_ERR_MEMORY;
935 /*********************************************************************
937 * Function : cgi_send_stylesheet
939 * Description : CGI function that sends a css stylesheet found
940 * in the cgi-style.css template
943 * 1 : csp = Current client state (buffers, headers, etc...)
944 * 2 : rsp = http_response data structure for output
945 * 3 : parameters = map of cgi parameters
947 * CGI Parameters : None
949 * Returns : JB_ERR_OK on success
950 * JB_ERR_MEMORY on out-of-memory error.
952 *********************************************************************/
953 jb_err cgi_send_stylesheet(struct client_state *csp,
954 struct http_response *rsp,
955 const struct map *parameters)
962 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
964 if (err == JB_ERR_FILE)
967 * No way to tell user; send empty stylesheet
969 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
973 return err; /* JB_ERR_MEMORY */
976 if (enlist(rsp->headers, "Content-Type: text/css"))
978 return JB_ERR_MEMORY;
986 /*********************************************************************
988 * Function : cgi_send_url_info_osd
990 * Description : CGI function that sends the OpenSearch Description
991 * template for the show-url-info page. It allows to
992 * access the page through "search engine plugins".
995 * 1 : csp = Current client state (buffers, headers, etc...)
996 * 2 : rsp = http_response data structure for output
997 * 3 : parameters = map of cgi parameters
999 * CGI Parameters : None
1001 * Returns : JB_ERR_OK on success
1002 * JB_ERR_MEMORY on out-of-memory error.
1004 *********************************************************************/
1005 jb_err cgi_send_url_info_osd(struct client_state *csp,
1006 struct http_response *rsp,
1007 const struct map *parameters)
1009 jb_err err = JB_ERR_MEMORY;
1010 struct map *exports = default_exports(csp, NULL);
1012 if (NULL != exports)
1014 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1015 if (JB_ERR_OK == err)
1017 err = enlist(rsp->headers,
1018 "Content-Type: application/opensearchdescription+xml");
1027 /*********************************************************************
1029 * Function : cgi_send_user_manual
1031 * Description : CGI function that sends a file in the user
1035 * 1 : csp = Current client state (buffers, headers, etc...)
1036 * 2 : rsp = http_response data structure for output
1037 * 3 : parameters = map of cgi parameters
1039 * CGI Parameters : file=name.html, the name of the HTML file
1040 * (relative to user-manual from config)
1042 * Returns : JB_ERR_OK on success
1043 * JB_ERR_MEMORY on out-of-memory error.
1045 *********************************************************************/
1046 jb_err cgi_send_user_manual(struct client_state *csp,
1047 struct http_response *rsp,
1048 const struct map *parameters)
1050 const char * filename;
1052 jb_err err = JB_ERR_OK;
1059 if (!parameters->first)
1061 /* requested http://p.p/user-manual (without trailing slash) */
1062 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1065 get_string_param(parameters, "file", &filename);
1066 /* Check paramter for hack attempts */
1067 if (filename && strchr(filename, '/'))
1069 return JB_ERR_CGI_PARAMS;
1071 if (filename && strstr(filename, ".."))
1073 return JB_ERR_CGI_PARAMS;
1076 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1077 if (full_path == NULL)
1079 return JB_ERR_MEMORY;
1082 err = load_file(full_path, &rsp->body, &rsp->content_length);
1083 if (JB_ERR_OK != err)
1085 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1086 if (JB_ERR_FILE == err)
1088 err = cgi_error_no_template(csp, rsp, full_path);
1095 /* Guess correct Content-Type based on the filename's ending */
1098 length = strlen(filename);
1104 if((length>=4) && !strcmp(&filename[length-4], ".css"))
1106 err = enlist(rsp->headers, "Content-Type: text/css");
1108 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1110 err = enlist(rsp->headers, "Content-Type: image/jpeg");
1114 err = enlist(rsp->headers, "Content-Type: text/html");
1121 /*********************************************************************
1123 * Function : cgi_show_version
1125 * Description : CGI function that returns a a web page describing the
1126 * file versions of Privoxy.
1129 * 1 : csp = Current client state (buffers, headers, etc...)
1130 * 2 : rsp = http_response data structure for output
1131 * 3 : parameters = map of cgi parameters
1133 * CGI Parameters : none
1135 * Returns : JB_ERR_OK on success
1136 * JB_ERR_MEMORY on out-of-memory error.
1138 *********************************************************************/
1139 jb_err cgi_show_version(struct client_state *csp,
1140 struct http_response *rsp,
1141 const struct map *parameters)
1143 struct map *exports;
1149 if (NULL == (exports = default_exports(csp, "show-version")))
1151 return JB_ERR_MEMORY;
1154 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1157 return JB_ERR_MEMORY;
1160 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1164 /*********************************************************************
1166 * Function : cgi_show_status
1168 * Description : CGI function that returns a web page describing the
1169 * current status of Privoxy.
1172 * 1 : csp = Current client state (buffers, headers, etc...)
1173 * 2 : rsp = http_response data structure for output
1174 * 3 : parameters = map of cgi parameters
1177 * file : Which file to show. Only first letter is checked,
1182 * Default is to show menu and other information.
1184 * Returns : JB_ERR_OK on success
1185 * JB_ERR_MEMORY on out-of-memory error.
1187 *********************************************************************/
1188 jb_err cgi_show_status(struct client_state *csp,
1189 struct http_response *rsp,
1190 const struct map *parameters)
1196 char buf[BUFFER_SIZE];
1197 #ifdef FEATURE_STATISTICS
1198 float perc_rej; /* Percentage of http requests rejected */
1199 int local_urls_read;
1200 int local_urls_rejected;
1201 #endif /* ndef FEATURE_STATISTICS */
1202 jb_err err = JB_ERR_OK;
1204 struct map *exports;
1210 if ('\0' != *(lookup(parameters, "file")))
1212 return cgi_show_file(csp, rsp, parameters);
1215 if (NULL == (exports = default_exports(csp, "show-status")))
1217 return JB_ERR_MEMORY;
1221 for (j = 0; (s != NULL) && (j < Argc); j++)
1223 if (!err) err = string_join (&s, html_encode(Argv[j]));
1224 if (!err) err = string_append(&s, " ");
1226 if (!err) err = map(exports, "invocation", 1, s, 0);
1228 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1229 if (!err) err = show_defines(exports);
1234 return JB_ERR_MEMORY;
1237 #ifdef FEATURE_STATISTICS
1238 local_urls_read = urls_read;
1239 local_urls_rejected = urls_rejected;
1242 * Need to alter the stats not to include the fetch of this
1245 * Can't do following thread safely! doh!
1248 * urls_rejected--; * This will be incremented subsequently *
1251 if (local_urls_read == 0)
1253 if (!err) err = map_block_killer(exports, "have-stats");
1257 if (!err) err = map_block_killer(exports, "have-no-stats");
1259 perc_rej = (float)local_urls_rejected * 100.0F /
1260 (float)local_urls_read;
1262 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1263 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1265 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1266 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1268 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1269 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1272 #else /* ndef FEATURE_STATISTICS */
1273 err = err || map_block_killer(exports, "statistics");
1274 #endif /* ndef FEATURE_STATISTICS */
1277 * List all action files in use, together with view and edit links,
1278 * except for standard.action, which should only be viewable. (Not
1279 * enforced in the editor itself)
1280 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1283 for (i = 0; i < MAX_AF_FILES; i++)
1285 if (csp->actions_list[i] != NULL)
1287 if (!err) err = string_append(&s, "<tr><td>");
1288 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1289 snprintf(buf, sizeof(buf),
1290 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1291 if (!err) err = string_append(&s, buf);
1293 #ifdef FEATURE_CGI_EDIT_ACTIONS
1294 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1295 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1296 && (NULL != csp->config->actions_file_short[i]))
1299 if (access(csp->config->actions_file[i], W_OK) == 0)
1301 #endif /* def HAVE_ACCESS */
1302 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%d\">Edit</a>", i);
1303 if (!err) err = string_append(&s, buf);
1308 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1310 #endif /* def HAVE_ACCESS */
1314 if (!err) err = string_append(&s, "</td></tr>\n");
1319 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1323 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1327 * List all re_filterfiles in use, together with view options.
1328 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1331 for (i = 0; i < MAX_AF_FILES; i++)
1333 if (csp->rlist[i] != NULL)
1335 if (!err) err = string_append(&s, "<tr><td>");
1336 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1338 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1339 if (!err) err = string_append(&s, buf);
1340 if (!err) err = string_append(&s, "</td></tr>\n");
1345 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1349 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1350 if (!err) err = map_block_killer(exports, "have-filterfile");
1353 #ifdef FEATURE_TRUST
1356 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1360 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1361 if (!err) err = map_block_killer(exports, "have-trustfile");
1364 if (!err) err = map_block_killer(exports, "trust-support");
1365 #endif /* ndef FEATURE_TRUST */
1367 #ifdef FEATURE_CGI_EDIT_ACTIONS
1368 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1370 err = map_block_killer(exports, "cgi-editor-is-disabled");
1372 #endif /* ndef CGI_EDIT_ACTIONS */
1377 return JB_ERR_MEMORY;
1380 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1384 /*********************************************************************
1386 * Function : cgi_show_url_info
1388 * Description : CGI function that determines and shows which actions
1389 * Privoxy will perform for a given url, and which
1390 * matches starting from the defaults have lead to that.
1393 * 1 : csp = Current client state (buffers, headers, etc...)
1394 * 2 : rsp = http_response data structure for output
1395 * 3 : parameters = map of cgi parameters
1398 * url : The url whose actions are to be determined.
1399 * If url is unset, the url-given conditional will be
1400 * set, so that all but the form can be suppressed in
1403 * Returns : JB_ERR_OK on success
1404 * JB_ERR_MEMORY on out-of-memory error.
1406 *********************************************************************/
1407 jb_err cgi_show_url_info(struct client_state *csp,
1408 struct http_response *rsp,
1409 const struct map *parameters)
1412 struct map *exports;
1419 if (NULL == (exports = default_exports(csp, "show-url-info")))
1421 return JB_ERR_MEMORY;
1425 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1427 url_param = strdup(lookup(parameters, "url"));
1428 if (url_param == NULL)
1431 return JB_ERR_MEMORY;
1436 * Handle prefixes. 4 possibilities:
1437 * 1) "http://" or "https://" prefix present and followed by URL - OK
1438 * 2) Only the "http://" or "https://" part is present, no URL - change
1439 * to empty string so it will be detected later as "no URL".
1440 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1442 * 4) Parameter not specified or is empty string - let this fall through
1443 * for now, next block of code will handle it.
1445 if (0 == strncmp(url_param, "http://", 7))
1447 if (url_param[7] == '\0')
1450 * Empty URL (just prefix).
1451 * Make it totally empty so it's caught by the next if()
1453 url_param[0] = '\0';
1456 else if (0 == strncmp(url_param, "https://", 8))
1458 if (url_param[8] == '\0')
1461 * Empty URL (just prefix).
1462 * Make it totally empty so it's caught by the next if()
1464 url_param[0] = '\0';
1467 else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1469 /* No prefix - assume http:// */
1470 char *url_param_prefixed = strdup("http://");
1472 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1475 return JB_ERR_MEMORY;
1477 url_param = url_param_prefixed;
1481 * Hide "toggle off" warning if Privoxy is toggled on.
1484 #ifdef FEATURE_TOGGLE
1485 (global_toggle_state == 1) &&
1486 #endif /* def FEATURE_TOGGLE */
1487 map_block_killer(exports, "privoxy-is-toggled-off")
1491 return JB_ERR_MEMORY;
1494 if (url_param[0] == '\0')
1496 /* URL paramater not specified, display query form only. */
1498 if (map_block_killer(exports, "url-given")
1499 || map(exports, "url", 1, "", 1))
1502 return JB_ERR_MEMORY;
1507 /* Given a URL, so query it. */
1512 struct file_list *fl;
1513 struct url_actions *b;
1514 struct http_request url_to_query[1];
1515 struct current_action_spec action[1];
1518 if (map(exports, "url", 1, html_encode(url_param), 0))
1522 return JB_ERR_MEMORY;
1525 init_current_action(action);
1527 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1529 free_current_action(action);
1532 return JB_ERR_MEMORY;
1535 memset(url_to_query, '\0', sizeof(url_to_query));
1536 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1537 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmp(url_param, "https://", 8)));
1541 if (err == JB_ERR_MEMORY)
1543 free_http_request(url_to_query);
1544 free_current_action(action);
1546 return JB_ERR_MEMORY;
1552 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1553 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1554 if (!err) err = map_block_killer(exports, "valid-url");
1556 free_current_action(action);
1557 free_http_request(url_to_query);
1562 return JB_ERR_MEMORY;
1565 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1569 * We have a warning about SSL paths. Hide it for unencrypted sites.
1571 if (!url_to_query->ssl)
1573 if (map_block_killer(exports, "https"))
1575 free_current_action(action);
1577 free_http_request(url_to_query);
1578 return JB_ERR_MEMORY;
1582 matches = strdup("<table summary=\"\" class=\"transparent\">");
1584 for (i = 0; i < MAX_AF_FILES; i++)
1586 if (NULL == csp->config->actions_file_short[i]
1587 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1591 if ((fl = csp->actions_list[i]) != NULL)
1593 if ((b = fl->f) != NULL)
1595 /* FIXME: Hardcoded HTML! */
1596 string_append(&matches, "<tr><th>In file: ");
1597 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1598 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1599 string_append(&matches, buf);
1600 string_append(&matches, "View</a>");
1601 #ifdef FEATURE_CGI_EDIT_ACTIONS
1602 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1605 if (access(csp->config->actions_file[i], W_OK) == 0)
1607 #endif /* def HAVE_ACCESS */
1608 snprintf(buf, sizeof(buf),
1609 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1610 string_append(&matches, buf);
1611 string_append(&matches, "Edit</a>");
1616 string_append(&matches, " <strong>No write access.</strong>");
1618 #endif /* def HAVE_ACCESS */
1620 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1622 string_append(&matches, "</th></tr>\n");
1629 for (; (b != NULL) && (matches != NULL); b = b->next)
1631 if (url_match(b->url, url_to_query))
1633 string_append(&matches, "<tr><td>{");
1634 string_join (&matches, actions_to_html(csp, b->action));
1635 string_append(&matches, " }<br>\n<code>");
1636 string_join (&matches, html_encode(b->url->spec));
1637 string_append(&matches, "</code></td></tr>\n");
1639 if (merge_current_action(action, b->action))
1642 free_http_request(url_to_query);
1643 free_current_action(action);
1645 return JB_ERR_MEMORY;
1653 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1656 string_append(&matches, "</table>\n");
1659 * XXX: Kludge to make sure the "Forward settings" section
1660 * shows what forward-override{} would do with the requested URL.
1661 * No one really cares how the CGI request would be forwarded
1662 * if it wasn't intercepted as CGI request in the first place.
1664 * From here on the action bitmask will no longer reflect
1665 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1666 * but luckily it's no longer required later on anyway.
1668 free_current_action(csp->action);
1669 get_url_actions(csp, url_to_query);
1672 * Fill in forwarding settings.
1674 * The possibilities are:
1676 * - http forwarding only
1677 * - socks4(a) forwarding only
1678 * - socks4(a) and http forwarding.
1680 * XXX: Parts of this code could be reused for the
1681 * "forwarding-failed" template which currently doesn't
1682 * display the proxy port and an eventual second forwarder.
1685 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1687 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1689 if (!err) err = map_block_killer(exports, "socks-forwarder");
1690 if (!err) err = map_block_killer(exports, "http-forwarder");
1694 char port[10]; /* We save proxy ports as int but need a string here */
1696 if (!err) err = map_block_killer(exports, "no-forwarder");
1698 if (fwd->gateway_host != NULL)
1700 char *socks_type = NULL;
1705 socks_type = "socks4";
1708 socks_type = "socks4a";
1711 socks_type = "socks5";
1714 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1717 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1718 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1719 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1720 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1724 if (!err) err = map_block_killer(exports, "socks-forwarder");
1727 if (fwd->forward_host != NULL)
1729 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1730 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1731 if (!err) err = map(exports, "forward-port", 1, port, 1);
1735 if (!err) err = map_block_killer(exports, "http-forwarder");
1740 free_http_request(url_to_query);
1742 if (err || matches == NULL)
1744 free_current_action(action);
1746 return JB_ERR_MEMORY;
1749 #ifdef FEATURE_CGI_EDIT_ACTIONS
1750 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1752 err = map_block_killer(exports, "cgi-editor-is-disabled");
1754 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1757 * If zlib support is available, if no content filters
1758 * are enabled or if the prevent-compression action is enabled,
1759 * suppress the "compression could prevent filtering" warning.
1761 #ifndef FEATURE_ZLIB
1762 if (!content_filters_enabled(action) ||
1763 (action->flags & ACTION_NO_COMPRESSION))
1766 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1769 if (err || map(exports, "matches", 1, matches , 0))
1771 free_current_action(action);
1773 return JB_ERR_MEMORY;
1776 s = current_action_to_html(csp, action);
1778 free_current_action(action);
1780 if (map(exports, "final", 1, s, 0))
1783 return JB_ERR_MEMORY;
1787 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1791 /*********************************************************************
1793 * Function : cgi_robots_txt
1795 * Description : CGI function to return "/robots.txt".
1798 * 1 : csp = Current client state (buffers, headers, etc...)
1799 * 2 : rsp = http_response data structure for output
1800 * 3 : parameters = map of cgi parameters
1802 * CGI Parameters : None
1804 * Returns : JB_ERR_OK on success
1805 * JB_ERR_MEMORY on out-of-memory error.
1807 *********************************************************************/
1808 jb_err cgi_robots_txt(struct client_state *csp,
1809 struct http_response *rsp,
1810 const struct map *parameters)
1816 "# This is the Privoxy control interface.\n"
1817 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1823 if (rsp->body == NULL)
1825 return JB_ERR_MEMORY;
1828 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1832 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1833 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1835 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1839 /*********************************************************************
1841 * Function : show_defines
1843 * Description : Add to a map the state od all conditional #defines
1844 * used when building
1847 * 1 : exports = map to extend
1849 * Returns : JB_ERR_OK on success
1850 * JB_ERR_MEMORY on out-of-memory error.
1852 *********************************************************************/
1853 static jb_err show_defines(struct map *exports)
1855 jb_err err = JB_ERR_OK;
1858 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1859 #else /* ifndef FEATURE_ACL */
1860 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1861 #endif /* ndef FEATURE_ACL */
1863 #ifdef FEATURE_CGI_EDIT_ACTIONS
1864 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1865 #else /* ifndef FEATURE_COOKIE_JAR */
1866 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1867 #endif /* ndef FEATURE_COOKIE_JAR */
1869 #ifdef FEATURE_COOKIE_JAR
1870 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1871 #else /* ifndef FEATURE_COOKIE_JAR */
1872 if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1873 #endif /* ndef FEATURE_COOKIE_JAR */
1875 #ifdef FEATURE_FAST_REDIRECTS
1876 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1877 #else /* ifndef FEATURE_FAST_REDIRECTS */
1878 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1879 #endif /* ndef FEATURE_FAST_REDIRECTS */
1881 #ifdef FEATURE_FORCE_LOAD
1882 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1883 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1884 #else /* ifndef FEATURE_FORCE_LOAD */
1885 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1886 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1887 #endif /* ndef FEATURE_FORCE_LOAD */
1889 #ifdef FEATURE_GRACEFUL_TERMINATION
1890 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1891 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1892 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1893 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1895 #ifdef FEATURE_IMAGE_BLOCKING
1896 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1897 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1898 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1899 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1901 #ifdef FEATURE_IMAGE_DETECT_MSIE
1902 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1903 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1904 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1905 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1907 #ifdef FEATURE_NO_GIFS
1908 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1909 #else /* ifndef FEATURE_NO_GIFS */
1910 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1911 #endif /* ndef FEATURE_NO_GIFS */
1913 #ifdef FEATURE_PTHREAD
1914 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1915 #else /* ifndef FEATURE_PTHREAD */
1916 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1917 #endif /* ndef FEATURE_PTHREAD */
1919 #ifdef FEATURE_STATISTICS
1920 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1921 #else /* ifndef FEATURE_STATISTICS */
1922 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1923 #endif /* ndef FEATURE_STATISTICS */
1925 #ifdef FEATURE_TOGGLE
1926 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1927 #else /* ifndef FEATURE_TOGGLE */
1928 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1929 #endif /* ndef FEATURE_TOGGLE */
1931 #ifdef FEATURE_TRUST
1932 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1933 #else /* ifndef FEATURE_TRUST */
1934 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1935 #endif /* ndef FEATURE_TRUST */
1938 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1939 #else /* ifndef FEATURE_ZLIB */
1940 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1941 #endif /* ndef FEATURE_ZLIB */
1944 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1945 #else /* ifndef STATIC_PCRE */
1946 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1947 #endif /* ndef STATIC_PCRE */
1950 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1951 #else /* ifndef STATIC_PCRS */
1952 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1953 #endif /* ndef STATIC_PCRS */
1959 /*********************************************************************
1961 * Function : show_rcs
1963 * Description : Create a string with the rcs info for all sourcefiles
1967 * Returns : A string, or NULL on out-of-memory.
1969 *********************************************************************/
1970 static char *show_rcs(void)
1972 char *result = strdup("");
1973 char buf[BUFFER_SIZE];
1975 /* Instead of including *all* dot h's in the project (thus creating a
1976 * tremendous amount of dependencies), I will concede to declaring them
1977 * as extern's. This forces the developer to add to this list, but oh well.
1980 #define SHOW_RCS(__x) \
1982 extern const char __x[]; \
1983 snprintf(buf, sizeof(buf), " %s\n", __x); \
1984 string_append(&result, buf); \
1987 /* In alphabetical order */
1988 SHOW_RCS(actions_h_rcs)
1989 SHOW_RCS(actions_rcs)
1991 SHOW_RCS(amiga_h_rcs)
1993 #endif /* def AMIGA */
1996 #ifdef FEATURE_CGI_EDIT_ACTIONS
1997 SHOW_RCS(cgiedit_h_rcs)
1998 SHOW_RCS(cgiedit_rcs)
1999 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
2000 SHOW_RCS(cgisimple_h_rcs)
2001 SHOW_RCS(cgisimple_rcs)
2003 SHOW_RCS(cygwin_h_rcs)
2005 SHOW_RCS(deanimate_h_rcs)
2006 SHOW_RCS(deanimate_rcs)
2007 SHOW_RCS(encode_h_rcs)
2008 SHOW_RCS(encode_rcs)
2009 SHOW_RCS(errlog_h_rcs)
2010 SHOW_RCS(errlog_rcs)
2011 SHOW_RCS(filters_h_rcs)
2012 SHOW_RCS(filters_rcs)
2013 SHOW_RCS(gateway_h_rcs)
2014 SHOW_RCS(gateway_rcs)
2015 SHOW_RCS(jbsockets_h_rcs)
2016 SHOW_RCS(jbsockets_rcs)
2019 SHOW_RCS(list_h_rcs)
2021 SHOW_RCS(loadcfg_h_rcs)
2022 SHOW_RCS(loadcfg_rcs)
2023 SHOW_RCS(loaders_h_rcs)
2024 SHOW_RCS(loaders_rcs)
2025 SHOW_RCS(miscutil_h_rcs)
2026 SHOW_RCS(miscutil_rcs)
2027 SHOW_RCS(parsers_h_rcs)
2028 SHOW_RCS(parsers_rcs)
2030 SHOW_RCS(pcrs_h_rcs)
2031 SHOW_RCS(project_h_rcs)
2032 SHOW_RCS(ssplit_h_rcs)
2033 SHOW_RCS(ssplit_rcs)
2034 SHOW_RCS(urlmatch_h_rcs)
2035 SHOW_RCS(urlmatch_rcs)
2037 #ifndef _WIN_CONSOLE
2038 SHOW_RCS(w32log_h_rcs)
2039 SHOW_RCS(w32log_rcs)
2040 SHOW_RCS(w32res_h_rcs)
2041 SHOW_RCS(w32taskbar_h_rcs)
2042 SHOW_RCS(w32taskbar_rcs)
2043 #endif /* ndef _WIN_CONSOLE */
2044 SHOW_RCS(win32_h_rcs)
2046 #endif /* def _WIN32 */
2055 /*********************************************************************
2057 * Function : cgi_show_file
2059 * Description : CGI function that shows the content of a
2060 * configuration file.
2063 * 1 : csp = Current client state (buffers, headers, etc...)
2064 * 2 : rsp = http_response data structure for output
2065 * 3 : parameters = map of cgi parameters
2068 * file : Which file to show. Only first letter is checked,
2073 * Default is to show menu and other information.
2075 * Returns : JB_ERR_OK on success
2076 * JB_ERR_MEMORY on out-of-memory error.
2078 *********************************************************************/
2079 static jb_err cgi_show_file(struct client_state *csp,
2080 struct http_response *rsp,
2081 const struct map *parameters)
2084 const char * filename = NULL;
2085 char * file_description = NULL;
2091 switch (*(lookup(parameters, "file")))
2094 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2096 filename = csp->actions_list[i]->filename;
2097 file_description = "Actions File";
2102 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2104 filename = csp->rlist[i]->filename;
2105 file_description = "Filter File";
2109 #ifdef FEATURE_TRUST
2113 filename = csp->tlist->filename;
2114 file_description = "Trust File";
2117 #endif /* def FEATURE_TRUST */
2120 if (NULL != filename)
2122 struct map *exports;
2127 exports = default_exports(csp, "show-status");
2128 if (NULL == exports)
2130 return JB_ERR_MEMORY;
2133 if ( map(exports, "file-description", 1, file_description, 1)
2134 || map(exports, "filepath", 1, html_encode(filename), 0) )
2137 return JB_ERR_MEMORY;
2140 err = load_file(filename, &s, &length);
2141 if (JB_ERR_OK != err)
2143 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2146 return JB_ERR_MEMORY;
2151 s = html_encode_and_free_original(s);
2154 return JB_ERR_MEMORY;
2157 if (map(exports, "contents", 1, s, 0))
2160 return JB_ERR_MEMORY;
2164 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2167 return JB_ERR_CGI_PARAMS;
2171 /*********************************************************************
2173 * Function : load_file
2175 * Description : Loads a file into a buffer.
2178 * 1 : filename = Name of the file to be loaded.
2179 * 2 : buffer = Used to return the file's content.
2180 * 3 : length = Used to return the size of the file.
2182 * Returns : JB_ERR_OK in case of success,
2183 * JB_ERR_FILE in case of ordinary file loading errors
2184 * (fseek() and ftell() errors are fatal)
2185 * JB_ERR_MEMORY in case of out-of-memory.
2187 *********************************************************************/
2188 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2192 jb_err err = JB_ERR_OK;
2194 fp = fopen(filename, "rb");
2200 /* Get file length */
2201 if (fseek(fp, 0, SEEK_END))
2203 log_error(LOG_LEVEL_FATAL,
2204 "Unexpected error while fseek()ing to the end of %s: %E",
2210 log_error(LOG_LEVEL_FATAL,
2211 "Unexpected ftell() error while loading %s: %E",
2214 *length = (size_t)ret;
2216 /* Go back to the beginning. */
2217 if (fseek(fp, 0, SEEK_SET))
2219 log_error(LOG_LEVEL_FATAL,
2220 "Unexpected error while fseek()ing to the beginning of %s: %E",
2224 *buffer = (char *)zalloc(*length + 1);
2225 if (NULL == *buffer)
2227 err = JB_ERR_MEMORY;
2229 else if (!fread(*buffer, *length, 1, fp))
2232 * May happen if the file size changes between fseek() and
2233 * fread(). If it does, we just log it and serve what we got.
2235 log_error(LOG_LEVEL_ERROR,
2236 "Couldn't completely read file %s.", filename);