1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.87 2008/08/29 15:59:22 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.87 2008/08/29 15:59:22 fabiankeil
42 * Revision 1.86 2008/06/28 14:19:05 fabiankeil
43 * Protocol detection is done case-insensitive, fix assertion
44 * to do the same. Yay for Privoxy-Regression-Test and zzuf.
46 * Revision 1.85 2008/05/26 17:30:55 fabiankeil
47 * Provide an OpenSearch Description to access the
48 * show-url-info page through "search engine plugins".
50 * Revision 1.84 2008/05/26 16:16:55 fabiankeil
51 * Spell error correctly.
53 * Revision 1.83 2008/05/12 14:51:30 fabiankeil
54 * Don't complain about an invalid URL if show-url-info is requested
55 * without parameters. Regression introduced in 1.81 by yours truly.
57 * Revision 1.82 2008/05/10 20:01:47 fabiankeil
58 * Fix an assertion that could erroneously
59 * trigger in case of memory shortage.
61 * Revision 1.81 2008/05/05 09:54:39 fabiankeil
62 * In cgi_show_url_info(), make sure ftp URLs are
63 * declared invalid. Also simplify the code that adds
64 * "http://" if no protocol has been specified.
66 * Revision 1.80 2008/05/04 16:18:32 fabiankeil
67 * Provide parse_http_url() with a third parameter to specify
68 * whether or not URLs without protocol are acceptable.
70 * Revision 1.79 2008/05/04 13:30:56 fabiankeil
71 * Streamline parse_http_url()'s prototype.
73 * Revision 1.78 2008/05/03 16:50:11 fabiankeil
74 * Leverage content_filters_enabled() in cgi_show_url_info().
76 * Revision 1.77 2008/05/02 09:47:48 fabiankeil
77 * In cgi_show_url_info, pass an initialized http structure
78 * to parse_http_url() as that will be required soonish and
79 * assert that https URLs are recognized correctly.
81 * Revision 1.76 2008/04/28 09:13:30 fabiankeil
82 * In load_file(), remember the error reason and fclose()
83 * and return later on instead of right away.
85 * Revision 1.75 2008/04/27 13:52:52 fabiankeil
86 * Move CGI file loading code into load_file() and
87 * add checks for unexpected errors.
89 * Revision 1.74 2008/04/26 15:50:56 fabiankeil
90 * Fix macro name in cgi_show_file() error path.
92 * Revision 1.73 2008/04/26 12:21:55 fabiankeil
93 * Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
95 * Revision 1.72 2008/04/26 10:34:15 fabiankeil
96 * If zlib support is unavailable and there are content filters active
97 * but the prevent-compression action is disabled, include a warning
98 * on the show-url-info page that compression might prevent filtering.
100 * Revision 1.71 2008/04/25 13:33:56 fabiankeil
101 * - Factor cgi_show_file() out of cgi_show_status().
102 * - Adjust cgi_show_status()'s parameter description to match reality.
104 * Revision 1.70 2008/04/24 16:12:38 fabiankeil
105 * In cgi_show_status(), load the requested file at once.
106 * Using string_join() for every line really doesn't scale.
108 * Revision 1.69 2008/04/17 14:40:48 fabiankeil
109 * Provide get_http_time() with the buffer size so it doesn't
110 * have to blindly assume that the buffer is big enough.
112 * Revision 1.68 2008/03/27 18:27:21 fabiankeil
113 * Remove kill-popups action.
115 * Revision 1.67 2008/03/27 17:00:05 fabiankeil
116 * Turn the favicon blobs into locals.
118 * Revision 1.66 2008/02/23 16:57:12 fabiankeil
119 * Rename url_actions() to get_url_actions() and let it
120 * use the standard parameter ordering.
122 * Revision 1.65 2008/02/23 16:33:43 fabiankeil
123 * Let forward_url() use the standard parameter ordering
124 * and mark its second parameter immutable.
126 * Revision 1.64 2008/02/03 13:56:07 fabiankeil
127 * Add SOCKS5 support for show-url-info CGI page.
129 * Revision 1.63 2008/02/01 06:04:31 fabiankeil
130 * If edit buttons on the show-url-info CGI page are hidden, explain why.
132 * Revision 1.62 2008/02/01 05:52:40 fabiankeil
133 * Hide edit buttons on the show-url-info CGI page if enable-edit-action
134 * is disabled. Patch by Lee with additional white space adjustments.
136 * Revision 1.61 2008/01/26 11:13:25 fabiankeil
137 * If enable-edit-actions is disabled, hide the edit buttons and explain why.
139 * Revision 1.60 2007/10/27 13:12:13 fabiankeil
140 * Finish 1.49 and check write access before
141 * showing edit buttons on show-url-info page.
143 * Revision 1.59 2007/10/19 16:42:36 fabiankeil
144 * Plug memory leak I introduced five months ago.
145 * Yay Valgrind and Privoxy-Regression-Test.
147 * Revision 1.58 2007/07/21 12:19:50 fabiankeil
148 * If show-url-info is called with an URL that Privoxy
149 * would reject as invalid, don't show unresolved forwarding
150 * variables, "final matches" or claim the site's secure.
152 * Revision 1.57 2007/06/01 16:53:05 fabiankeil
153 * Adjust cgi_show_url_info() to show what forward-override{}
154 * would do with the requested URL (instead of showing how the
155 * request for the CGI page would be forwarded if it wasn't a
158 * Revision 1.56 2007/05/21 10:50:35 fabiankeil
159 * - Use strlcpy() instead of strcpy().
160 * - Stop treating actions files special. Expect a complete file name
161 * (with or without path) like it's done for the rest of the files.
163 * - Don't rerun sed() in cgi_show_request().
165 * Revision 1.55 2007/04/13 13:36:46 fabiankeil
166 * Reference action files in CGI URLs by id instead
167 * of using the first part of the file name.
168 * Fixes BR 1694250 and BR 1590556.
170 * Revision 1.54 2007/04/09 18:11:35 fabiankeil
171 * Don't mistake VC++'s _snprintf() for a snprintf() replacement.
173 * Revision 1.53 2007/04/08 13:21:04 fabiankeil
174 * Reference action files in CGI URLs by id instead
175 * of using the first part of the file name.
176 * Fixes BR 1694250 and BR 1590556.
178 * Revision 1.52 2007/02/13 15:10:26 fabiankeil
179 * Apparently fopen()ing in "binary" mode doesn't require
180 * #ifdefs, it's already done without them in cgiedit.c.
182 * Revision 1.51 2007/02/10 16:55:22 fabiankeil
183 * - Show forwarding settings on the show-url-info page
184 * - Fix some HTML syntax errors.
186 * Revision 1.50 2007/01/23 15:51:17 fabiankeil
187 * Add favicon delivery functions.
189 * Revision 1.49 2007/01/20 16:29:38 fabiankeil
190 * Suppress edit buttons for action files if Privoxy has
191 * no write access. Suggested by Roland in PR 1564026.
193 * Revision 1.48 2007/01/20 15:31:31 fabiankeil
194 * Display warning if show-url-info CGI page
195 * is used while Privoxy is toggled off.
197 * Revision 1.47 2007/01/12 15:07:10 fabiankeil
198 * Use zalloc in cgi_send_user_manual.
200 * Revision 1.46 2007/01/02 12:49:46 fabiankeil
201 * Add FEATURE_ZLIB to the list of conditional
202 * defines at the show-status page.
204 * Revision 1.45 2006/12/28 18:16:41 fabiankeil
205 * Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
206 * body memory before using it, replaced sprintf calls with snprintf.
208 * Revision 1.44 2006/12/22 14:19:27 fabiankeil
209 * Removed checks whether or not AF_FILES have
210 * data structures associated with them in cgi_show_status.
211 * It doesn't matter as we're only interested in the file names.
213 * For the action files the checks were always true,
214 * but they prevented empty filter files from being
215 * listed. Fixes parts of BR 1619208.
217 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
218 * - Added FEATURE_GRACEFUL_TERMINATION to the
219 * "conditional #defines" section
220 * - Escaped ampersands in generated HTML.
221 * - Renamed re-filter-filename to re-filter-filenames
223 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
224 * Add special treatment for WIN32 to make sure
225 * cgi_send_user_manual opens the files in binary mode.
226 * Fixes BR 1600411 and unbreaks image delivery.
228 * Remove outdated comment.
230 * Revision 1.41 2006/10/09 19:18:28 roro
231 * Redirect http://p.p/user-manual (without trailing slash) to
232 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
234 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
235 * Modified cgi_send_user_manual to serve binary
236 * content without destroying it first. Should also be
237 * faster now. Added ".jpg" check for Content-Type guessing.
239 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
240 * Deliver documents in the user-manual directory
241 * with "Content-Type text/css" if their filename
244 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
245 * Incorporate modified version of Roland Rosenfeld's patch to
246 * optionally access the user-manual via Privoxy. Closes patch 679075.
248 * Formatting changed to Privoxy style, added call to
249 * cgi_error_no_template if the requested file doesn't
250 * exist and modified check whether or not Privoxy itself
251 * should serve the manual. Should work cross-platform now.
253 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
254 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
255 * with what was really the latest development (the v_3_0_branch branch)
257 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
258 * Multiple filter file support
260 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
261 * Undo some damaging memory leak patches
263 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
264 * A few memory leaks plugged (mostly on error paths)
266 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
267 * Another instance of:
268 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
269 * Thanks to Magnus Holmgren for the patch
271 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
272 * - Prevent line wrap beween "View/Edit" link buttons on status page
273 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
275 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
276 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
278 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
279 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
281 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
282 * Applying patch from Andreas as provided by Hal on the list.
283 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
285 * Revision 1.35 2002/05/12 21:44:44 jongfoster
286 * Adding amiga.[ch] revision information, if on an amiga.
288 * Revision 1.34 2002/04/30 12:06:12 oes
289 * Deleted unused code from default_cgi
291 * Revision 1.33 2002/04/30 11:14:52 oes
292 * Made csp the first parameter in *action_to_html
294 * Revision 1.32 2002/04/26 18:29:13 jongfoster
295 * Fixing this Visual C++ warning:
296 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
298 * Revision 1.31 2002/04/26 12:54:36 oes
299 * - Kill obsolete REDIRECT_URL code
300 * - Error handling fixes
301 * - Style sheet related HTML snipplet changes
302 * - cgi_show_url_info:
303 * - Matches now in table, actions on single lines,
305 * - standard.action suppressed
306 * - Buttons to View and Edit AFs
308 * Revision 1.30 2002/04/24 02:18:08 oes
309 * - show-status is now the starting point for editing
310 * the actions files, generate list of all AFs with buttons
311 * for viewing and editing, new look for file list (Jon:
312 * buttons now aligned ;-P ), view mode now supports multiple
313 * AFs, name changes, no view links for unspecified files,
314 * no edit link for standard.action.
316 * - Jon's multiple AF patch: cgi_show_url_info now uses all
317 * AFs and marks the output accordingly
319 * Revision 1.29 2002/04/10 13:38:35 oes
320 * load_template signature changed
322 * Revision 1.28 2002/04/07 15:42:12 jongfoster
323 * Fixing send-banner?type=auto when the image-blocker is
324 * a redirect to send-banner
326 * Revision 1.27 2002/04/05 15:50:48 oes
327 * added send-stylesheet CGI
329 * Revision 1.26 2002/04/04 00:36:36 gliptak
330 * always use pcre for matching
332 * Revision 1.25 2002/04/03 22:28:03 gliptak
333 * Removed references to gnu_regex
335 * Revision 1.24 2002/04/02 16:12:47 oes
336 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
338 * Revision 1.23 2002/03/26 22:29:54 swa
339 * we have a new homepage!
341 * Revision 1.22 2002/03/24 16:18:15 jongfoster
344 * Revision 1.21 2002/03/24 15:23:33 jongfoster
347 * Revision 1.20 2002/03/24 13:25:43 swa
348 * name change related issues
350 * Revision 1.19 2002/03/16 23:54:06 jongfoster
351 * Adding graceful termination feature, to help look for memory leaks.
352 * If you enable this (which, by design, has to be done by hand
353 * editing config.h) and then go to http://i.j.b/die, then the program
354 * will exit cleanly after the *next* request. It should free all the
355 * memory that was used.
357 * Revision 1.18 2002/03/12 01:44:49 oes
358 * Changed default for "blocked" image from jb logo to checkboard pattern
360 * Revision 1.17 2002/03/08 16:43:18 oes
361 * Added choice beween GIF and PNG built-in images
363 * Revision 1.16 2002/03/07 03:48:38 oes
364 * - Changed built-in images from GIF to PNG
365 * (with regard to Unisys patent issue)
366 * - Added a 4x4 pattern PNG which is less intrusive
367 * than the logo but also clearly marks the deleted banners
369 * Revision 1.15 2002/03/06 22:54:35 jongfoster
370 * Automated function-comment nitpicking.
372 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
373 * Clean up a little CRLF unpleasantness that suddenly appeared
375 * Revision 1.13 2002/02/21 00:10:37 jongfoster
376 * Adding send-banner?type=auto option
378 * Revision 1.12 2002/01/23 01:03:32 jongfoster
379 * Fixing gcc [CygWin] compiler warnings
381 * Revision 1.11 2002/01/23 00:01:04 jongfoster
382 * Adding cgi_transparent_gif() for http://i.j.b/t
383 * Adding missing html_encode() to many CGI functions.
384 * Adding urlmatch.[ch] to http://i.j.b/show-version
386 * Revision 1.10 2002/01/17 21:10:37 jongfoster
387 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
388 * Also fixing a problem in the same function with improperly quoted URLs
389 * in output HTML, and adding code to handle https:// URLs correctly.
391 * Revision 1.9 2001/11/30 23:09:15 jongfoster
392 * Now reports on FEATURE_CGI_EDIT_ACTIONS
393 * Removing FEATURE_DENY_GZIP from template
395 * Revision 1.8 2001/11/13 00:14:07 jongfoster
396 * Fixing stupid bug now I've figured out what || means.
397 * (It always returns 0 or 1, not one of it's paramaters.)
399 * Revision 1.7 2001/10/23 21:48:19 jongfoster
400 * Cleaning up error handling in CGI functions - they now send back
401 * a HTML error page and should never cause a FATAL error. (Fixes one
402 * potential source of "denial of service" attacks).
404 * CGI actions file editor that works and is actually useful.
406 * Ability to toggle JunkBuster remotely using a CGI call.
408 * You can turn off both the above features in the main configuration
409 * file, e.g. if you are running a multi-user proxy.
411 * Revision 1.6 2001/10/14 22:00:32 jongfoster
412 * Adding support for a 404 error when an invalid CGI page is requested.
414 * Revision 1.5 2001/10/07 15:30:41 oes
415 * Removed FEATURE_DENY_GZIP
417 * Revision 1.4 2001/10/02 15:31:12 oes
418 * Introduced show-request cgi
420 * Revision 1.3 2001/09/22 16:34:44 jongfoster
421 * Removing unneeded #includes
423 * Revision 1.2 2001/09/19 18:01:11 oes
424 * Fixed comments; cosmetics
426 * Revision 1.1 2001/09/16 17:08:54 jongfoster
427 * Moving simple CGI functions from cgi.c to new file cgisimple.c
430 **********************************************************************/
436 #include <sys/types.h>
444 #endif /* def HAVE_ACCESS */
448 #include "cgisimple.h"
454 #include "miscutil.h"
457 #include "urlmatch.h"
460 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
462 static char *show_rcs(void);
463 static jb_err show_defines(struct map *exports);
464 static jb_err cgi_show_file(struct client_state *csp,
465 struct http_response *rsp,
466 const struct map *parameters);
467 static jb_err load_file(const char *filename, char **buffer, size_t *length);
469 /*********************************************************************
471 * Function : cgi_default
473 * Description : CGI function that is called for the CGI_SITE_1_HOST
474 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
475 * Boring - only exports the default exports.
478 * 1 : csp = Current client state (buffers, headers, etc...)
479 * 2 : rsp = http_response data structure for output
480 * 3 : parameters = map of cgi parameters
482 * CGI Parameters : none
484 * Returns : JB_ERR_OK on success
485 * JB_ERR_MEMORY on out-of-memory
487 *********************************************************************/
488 jb_err cgi_default(struct client_state *csp,
489 struct http_response *rsp,
490 const struct map *parameters)
497 if (NULL == (exports = default_exports(csp, "")))
499 return JB_ERR_MEMORY;
502 return template_fill_for_cgi(csp, "default", exports, rsp);
506 /*********************************************************************
508 * Function : cgi_error_404
510 * Description : CGI function that is called if an unknown action was
514 * 1 : csp = Current client state (buffers, headers, etc...)
515 * 2 : rsp = http_response data structure for output
516 * 3 : parameters = map of cgi parameters
518 * CGI Parameters : none
520 * Returns : JB_ERR_OK on success
521 * JB_ERR_MEMORY on out-of-memory error.
523 *********************************************************************/
524 jb_err cgi_error_404(struct client_state *csp,
525 struct http_response *rsp,
526 const struct map *parameters)
534 if (NULL == (exports = default_exports(csp, NULL)))
536 return JB_ERR_MEMORY;
539 rsp->status = strdup("404 Privoxy configuration page not found");
540 if (rsp->status == NULL)
543 return JB_ERR_MEMORY;
546 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
550 #ifdef FEATURE_GRACEFUL_TERMINATION
551 /*********************************************************************
555 * Description : CGI function to shut down Privoxy.
556 * NOTE: Turning this on in a production build
557 * would be a BAD idea. An EXTREMELY BAD idea.
558 * In short, don't do it.
561 * 1 : csp = Current client state (buffers, headers, etc...)
562 * 2 : rsp = http_response data structure for output
563 * 3 : parameters = map of cgi parameters
565 * CGI Parameters : none
567 * Returns : JB_ERR_OK on success
568 * JB_ERR_MEMORY on out-of-memory error.
570 *********************************************************************/
571 jb_err cgi_die (struct client_state *csp,
572 struct http_response *rsp,
573 const struct map *parameters)
583 * I don't really care what gets sent back to the browser.
584 * Take the easy option - "out of memory" page.
587 return JB_ERR_MEMORY;
589 #endif /* def FEATURE_GRACEFUL_TERMINATION */
592 /*********************************************************************
594 * Function : cgi_show_request
596 * Description : Show the client's request and what sed() would have
600 * 1 : csp = Current client state (buffers, headers, etc...)
601 * 2 : rsp = http_response data structure for output
602 * 3 : parameters = map of cgi parameters
604 * CGI Parameters : none
606 * Returns : JB_ERR_OK on success
607 * JB_ERR_MEMORY on out-of-memory error.
609 *********************************************************************/
610 jb_err cgi_show_request(struct client_state *csp,
611 struct http_response *rsp,
612 const struct map *parameters)
621 if (NULL == (exports = default_exports(csp, "show-request")))
623 return JB_ERR_MEMORY;
627 * Repair the damage done to the IOB by get_header()
629 for (p = csp->iob->buf; p < csp->iob->eod; p++)
631 if (*p == '\0') *p = '\n';
635 * Export the original client's request and the one we would
636 * be sending to the server if this wasn't a CGI call
639 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
642 return JB_ERR_MEMORY;
645 if (map(exports, "processed-request", 1,
646 html_encode_and_free_original(list_to_text(csp->headers)), 0))
649 return JB_ERR_MEMORY;
652 return template_fill_for_cgi(csp, "show-request", exports, rsp);
656 /*********************************************************************
658 * Function : cgi_send_banner
660 * Description : CGI function that returns a banner.
663 * 1 : csp = Current client state (buffers, headers, etc...)
664 * 2 : rsp = http_response data structure for output
665 * 3 : parameters = map of cgi parameters
668 * type : Selects the type of banner between "trans", "logo",
669 * and "auto". Defaults to "logo" if absent or invalid.
670 * "auto" means to select as if we were image-blocking.
671 * (Only the first character really counts; b and t are
674 * Returns : JB_ERR_OK on success
675 * JB_ERR_MEMORY on out-of-memory error.
677 *********************************************************************/
678 jb_err cgi_send_banner(struct client_state *csp,
679 struct http_response *rsp,
680 const struct map *parameters)
682 char imagetype = lookup(parameters, "type")[0];
685 * If type is auto, then determine the right thing
686 * to do from the set-image-blocker action
688 if (imagetype == 'a')
695 #ifdef FEATURE_IMAGE_BLOCKING
696 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
698 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
699 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
700 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
704 /* Use default - nothing to do here. */
706 else if (0 == strcmpic(p, "blank"))
710 else if (0 == strcmpic(p, "pattern"))
716 * If the action is to call this CGI, determine
719 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
721 imagetype = p[sizeof(prefix1) - 1];
723 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
725 imagetype = p[sizeof(prefix2) - 1];
729 * Everything else must (should) be a URL to
737 #endif /* def FEATURE_IMAGE_BLOCKING */
741 * Now imagetype is either the non-auto type we were called with,
742 * or it was auto and has since been determined. In any case, we
743 * can proceed to actually answering the request by sending a redirect
744 * or an image as appropriate:
746 if (imagetype == 'r')
748 rsp->status = strdup("302 Local Redirect from Privoxy");
749 if (rsp->status == NULL)
751 return JB_ERR_MEMORY;
753 if (enlist_unique_header(rsp->headers, "Location",
754 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
756 return JB_ERR_MEMORY;
761 if ((imagetype == 'b') || (imagetype == 't'))
763 rsp->body = bindup(image_blank_data, image_blank_length);
764 rsp->content_length = image_blank_length;
768 rsp->body = bindup(image_pattern_data, image_pattern_length);
769 rsp->content_length = image_pattern_length;
772 if (rsp->body == NULL)
774 return JB_ERR_MEMORY;
776 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
778 return JB_ERR_MEMORY;
789 /*********************************************************************
791 * Function : cgi_transparent_image
793 * Description : CGI function that sends a 1x1 transparent image.
796 * 1 : csp = Current client state (buffers, headers, etc...)
797 * 2 : rsp = http_response data structure for output
798 * 3 : parameters = map of cgi parameters
800 * CGI Parameters : None
802 * Returns : JB_ERR_OK on success
803 * JB_ERR_MEMORY on out-of-memory error.
805 *********************************************************************/
806 jb_err cgi_transparent_image(struct client_state *csp,
807 struct http_response *rsp,
808 const struct map *parameters)
810 rsp->body = bindup(image_blank_data, image_blank_length);
811 rsp->content_length = image_blank_length;
813 if (rsp->body == NULL)
815 return JB_ERR_MEMORY;
818 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
820 return JB_ERR_MEMORY;
830 /*********************************************************************
832 * Function : cgi_send_default_favicon
834 * Description : CGI function that sends the standard favicon.
837 * 1 : csp = Current client state (buffers, headers, etc...)
838 * 2 : rsp = http_response data structure for output
839 * 3 : parameters = map of cgi parameters
841 * CGI Parameters : None
843 * Returns : JB_ERR_OK on success
844 * JB_ERR_MEMORY on out-of-memory error.
846 *********************************************************************/
847 jb_err cgi_send_default_favicon(struct client_state *csp,
848 struct http_response *rsp,
849 const struct map *parameters)
851 static const char default_favicon_data[] =
852 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
853 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
854 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
855 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
856 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
857 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
858 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
859 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
860 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
861 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
862 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
863 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
864 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
866 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
868 rsp->body = bindup(default_favicon_data, favicon_length);
869 rsp->content_length = favicon_length;
871 if (rsp->body == NULL)
873 return JB_ERR_MEMORY;
876 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
878 return JB_ERR_MEMORY;
888 /*********************************************************************
890 * Function : cgi_send_error_favicon
892 * Description : CGI function that sends the favicon for error pages.
895 * 1 : csp = Current client state (buffers, headers, etc...)
896 * 2 : rsp = http_response data structure for output
897 * 3 : parameters = map of cgi parameters
899 * CGI Parameters : None
901 * Returns : JB_ERR_OK on success
902 * JB_ERR_MEMORY on out-of-memory error.
904 *********************************************************************/
905 jb_err cgi_send_error_favicon(struct client_state *csp,
906 struct http_response *rsp,
907 const struct map *parameters)
909 static const char error_favicon_data[] =
910 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
911 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
912 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
913 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
914 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
915 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
916 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
917 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
918 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
919 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
920 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
921 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
922 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
924 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
926 rsp->body = bindup(error_favicon_data, favicon_length);
927 rsp->content_length = favicon_length;
929 if (rsp->body == NULL)
931 return JB_ERR_MEMORY;
934 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
936 return JB_ERR_MEMORY;
946 /*********************************************************************
948 * Function : cgi_send_stylesheet
950 * Description : CGI function that sends a css stylesheet found
951 * in the cgi-style.css template
954 * 1 : csp = Current client state (buffers, headers, etc...)
955 * 2 : rsp = http_response data structure for output
956 * 3 : parameters = map of cgi parameters
958 * CGI Parameters : None
960 * Returns : JB_ERR_OK on success
961 * JB_ERR_MEMORY on out-of-memory error.
963 *********************************************************************/
964 jb_err cgi_send_stylesheet(struct client_state *csp,
965 struct http_response *rsp,
966 const struct map *parameters)
973 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
975 if (err == JB_ERR_FILE)
978 * No way to tell user; send empty stylesheet
980 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
984 return err; /* JB_ERR_MEMORY */
987 if (enlist(rsp->headers, "Content-Type: text/css"))
989 return JB_ERR_MEMORY;
997 /*********************************************************************
999 * Function : cgi_send_url_info_osd
1001 * Description : CGI function that sends the OpenSearch Description
1002 * template for the show-url-info page. It allows to
1003 * access the page through "search engine plugins".
1006 * 1 : csp = Current client state (buffers, headers, etc...)
1007 * 2 : rsp = http_response data structure for output
1008 * 3 : parameters = map of cgi parameters
1010 * CGI Parameters : None
1012 * Returns : JB_ERR_OK on success
1013 * JB_ERR_MEMORY on out-of-memory error.
1015 *********************************************************************/
1016 jb_err cgi_send_url_info_osd(struct client_state *csp,
1017 struct http_response *rsp,
1018 const struct map *parameters)
1020 jb_err err = JB_ERR_MEMORY;
1021 struct map *exports = default_exports(csp, NULL);
1023 if (NULL != exports)
1025 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1026 if (JB_ERR_OK == err)
1028 err = enlist(rsp->headers,
1029 "Content-Type: application/opensearchdescription+xml");
1038 /*********************************************************************
1040 * Function : cgi_send_user_manual
1042 * Description : CGI function that sends a file in the user
1046 * 1 : csp = Current client state (buffers, headers, etc...)
1047 * 2 : rsp = http_response data structure for output
1048 * 3 : parameters = map of cgi parameters
1050 * CGI Parameters : file=name.html, the name of the HTML file
1051 * (relative to user-manual from config)
1053 * Returns : JB_ERR_OK on success
1054 * JB_ERR_MEMORY on out-of-memory error.
1056 *********************************************************************/
1057 jb_err cgi_send_user_manual(struct client_state *csp,
1058 struct http_response *rsp,
1059 const struct map *parameters)
1061 const char * filename;
1063 jb_err err = JB_ERR_OK;
1070 if (!parameters->first)
1072 /* requested http://p.p/user-manual (without trailing slash) */
1073 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1076 get_string_param(parameters, "file", &filename);
1077 /* Check paramter for hack attempts */
1078 if (filename && strchr(filename, '/'))
1080 return JB_ERR_CGI_PARAMS;
1082 if (filename && strstr(filename, ".."))
1084 return JB_ERR_CGI_PARAMS;
1087 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1088 if (full_path == NULL)
1090 return JB_ERR_MEMORY;
1093 err = load_file(full_path, &rsp->body, &rsp->content_length);
1094 if (JB_ERR_OK != err)
1096 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1097 if (JB_ERR_FILE == err)
1099 err = cgi_error_no_template(csp, rsp, full_path);
1106 /* Guess correct Content-Type based on the filename's ending */
1109 length = strlen(filename);
1115 if((length>=4) && !strcmp(&filename[length-4], ".css"))
1117 err = enlist(rsp->headers, "Content-Type: text/css");
1119 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1121 err = enlist(rsp->headers, "Content-Type: image/jpeg");
1125 err = enlist(rsp->headers, "Content-Type: text/html");
1132 /*********************************************************************
1134 * Function : cgi_show_version
1136 * Description : CGI function that returns a a web page describing the
1137 * file versions of Privoxy.
1140 * 1 : csp = Current client state (buffers, headers, etc...)
1141 * 2 : rsp = http_response data structure for output
1142 * 3 : parameters = map of cgi parameters
1144 * CGI Parameters : none
1146 * Returns : JB_ERR_OK on success
1147 * JB_ERR_MEMORY on out-of-memory error.
1149 *********************************************************************/
1150 jb_err cgi_show_version(struct client_state *csp,
1151 struct http_response *rsp,
1152 const struct map *parameters)
1154 struct map *exports;
1160 if (NULL == (exports = default_exports(csp, "show-version")))
1162 return JB_ERR_MEMORY;
1165 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1168 return JB_ERR_MEMORY;
1171 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1175 /*********************************************************************
1177 * Function : cgi_show_status
1179 * Description : CGI function that returns a web page describing the
1180 * current status of Privoxy.
1183 * 1 : csp = Current client state (buffers, headers, etc...)
1184 * 2 : rsp = http_response data structure for output
1185 * 3 : parameters = map of cgi parameters
1188 * file : Which file to show. Only first letter is checked,
1193 * Default is to show menu and other information.
1195 * Returns : JB_ERR_OK on success
1196 * JB_ERR_MEMORY on out-of-memory error.
1198 *********************************************************************/
1199 jb_err cgi_show_status(struct client_state *csp,
1200 struct http_response *rsp,
1201 const struct map *parameters)
1207 char buf[BUFFER_SIZE];
1208 #ifdef FEATURE_STATISTICS
1209 float perc_rej; /* Percentage of http requests rejected */
1210 int local_urls_read;
1211 int local_urls_rejected;
1212 #endif /* ndef FEATURE_STATISTICS */
1213 jb_err err = JB_ERR_OK;
1215 struct map *exports;
1221 if ('\0' != *(lookup(parameters, "file")))
1223 return cgi_show_file(csp, rsp, parameters);
1226 if (NULL == (exports = default_exports(csp, "show-status")))
1228 return JB_ERR_MEMORY;
1232 for (j = 0; (s != NULL) && (j < Argc); j++)
1234 if (!err) err = string_join (&s, html_encode(Argv[j]));
1235 if (!err) err = string_append(&s, " ");
1237 if (!err) err = map(exports, "invocation", 1, s, 0);
1239 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1240 if (!err) err = show_defines(exports);
1245 return JB_ERR_MEMORY;
1248 #ifdef FEATURE_STATISTICS
1249 local_urls_read = urls_read;
1250 local_urls_rejected = urls_rejected;
1253 * Need to alter the stats not to include the fetch of this
1256 * Can't do following thread safely! doh!
1259 * urls_rejected--; * This will be incremented subsequently *
1262 if (local_urls_read == 0)
1264 if (!err) err = map_block_killer(exports, "have-stats");
1268 if (!err) err = map_block_killer(exports, "have-no-stats");
1270 perc_rej = (float)local_urls_rejected * 100.0F /
1271 (float)local_urls_read;
1273 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1274 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1276 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1277 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1279 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1280 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1283 #else /* ndef FEATURE_STATISTICS */
1284 err = err || map_block_killer(exports, "statistics");
1285 #endif /* ndef FEATURE_STATISTICS */
1288 * List all action files in use, together with view and edit links,
1289 * except for standard.action, which should only be viewable. (Not
1290 * enforced in the editor itself)
1291 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1294 for (i = 0; i < MAX_AF_FILES; i++)
1296 if (csp->actions_list[i] != NULL)
1298 if (!err) err = string_append(&s, "<tr><td>");
1299 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1300 snprintf(buf, sizeof(buf),
1301 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1302 if (!err) err = string_append(&s, buf);
1304 #ifdef FEATURE_CGI_EDIT_ACTIONS
1305 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1306 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1307 && (NULL != csp->config->actions_file_short[i]))
1310 if (access(csp->config->actions_file[i], W_OK) == 0)
1312 #endif /* def HAVE_ACCESS */
1313 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%d\">Edit</a>", i);
1314 if (!err) err = string_append(&s, buf);
1319 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1321 #endif /* def HAVE_ACCESS */
1325 if (!err) err = string_append(&s, "</td></tr>\n");
1330 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1334 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1338 * List all re_filterfiles in use, together with view options.
1339 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1342 for (i = 0; i < MAX_AF_FILES; i++)
1344 if (csp->rlist[i] != NULL)
1346 if (!err) err = string_append(&s, "<tr><td>");
1347 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1349 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1350 if (!err) err = string_append(&s, buf);
1351 if (!err) err = string_append(&s, "</td></tr>\n");
1356 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1360 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1361 if (!err) err = map_block_killer(exports, "have-filterfile");
1364 #ifdef FEATURE_TRUST
1367 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1371 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1372 if (!err) err = map_block_killer(exports, "have-trustfile");
1375 if (!err) err = map_block_killer(exports, "trust-support");
1376 #endif /* ndef FEATURE_TRUST */
1378 #ifdef FEATURE_CGI_EDIT_ACTIONS
1379 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1381 err = map_block_killer(exports, "cgi-editor-is-disabled");
1383 #endif /* ndef CGI_EDIT_ACTIONS */
1388 return JB_ERR_MEMORY;
1391 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1395 /*********************************************************************
1397 * Function : cgi_show_url_info
1399 * Description : CGI function that determines and shows which actions
1400 * Privoxy will perform for a given url, and which
1401 * matches starting from the defaults have lead to that.
1404 * 1 : csp = Current client state (buffers, headers, etc...)
1405 * 2 : rsp = http_response data structure for output
1406 * 3 : parameters = map of cgi parameters
1409 * url : The url whose actions are to be determined.
1410 * If url is unset, the url-given conditional will be
1411 * set, so that all but the form can be suppressed in
1414 * Returns : JB_ERR_OK on success
1415 * JB_ERR_MEMORY on out-of-memory error.
1417 *********************************************************************/
1418 jb_err cgi_show_url_info(struct client_state *csp,
1419 struct http_response *rsp,
1420 const struct map *parameters)
1423 struct map *exports;
1430 if (NULL == (exports = default_exports(csp, "show-url-info")))
1432 return JB_ERR_MEMORY;
1436 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1438 url_param = strdup(lookup(parameters, "url"));
1439 if (url_param == NULL)
1442 return JB_ERR_MEMORY;
1447 * Handle prefixes. 4 possibilities:
1448 * 1) "http://" or "https://" prefix present and followed by URL - OK
1449 * 2) Only the "http://" or "https://" part is present, no URL - change
1450 * to empty string so it will be detected later as "no URL".
1451 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1453 * 4) Parameter not specified or is empty string - let this fall through
1454 * for now, next block of code will handle it.
1456 if (0 == strncmp(url_param, "http://", 7))
1458 if (url_param[7] == '\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 (0 == strncmp(url_param, "https://", 8))
1469 if (url_param[8] == '\0')
1472 * Empty URL (just prefix).
1473 * Make it totally empty so it's caught by the next if()
1475 url_param[0] = '\0';
1478 else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1480 /* No prefix - assume http:// */
1481 char *url_param_prefixed = strdup("http://");
1483 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1486 return JB_ERR_MEMORY;
1488 url_param = url_param_prefixed;
1492 * Hide "toggle off" warning if Privoxy is toggled on.
1495 #ifdef FEATURE_TOGGLE
1496 (global_toggle_state == 1) &&
1497 #endif /* def FEATURE_TOGGLE */
1498 map_block_killer(exports, "privoxy-is-toggled-off")
1502 return JB_ERR_MEMORY;
1505 if (url_param[0] == '\0')
1507 /* URL paramater not specified, display query form only. */
1509 if (map_block_killer(exports, "url-given")
1510 || map(exports, "url", 1, "", 1))
1513 return JB_ERR_MEMORY;
1518 /* Given a URL, so query it. */
1523 struct file_list *fl;
1524 struct url_actions *b;
1525 struct http_request url_to_query[1];
1526 struct current_action_spec action[1];
1529 if (map(exports, "url", 1, html_encode(url_param), 0))
1533 return JB_ERR_MEMORY;
1536 init_current_action(action);
1538 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1540 free_current_action(action);
1543 return JB_ERR_MEMORY;
1546 memset(url_to_query, '\0', sizeof(url_to_query));
1547 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1548 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1552 if (err == JB_ERR_MEMORY)
1554 free_http_request(url_to_query);
1555 free_current_action(action);
1557 return JB_ERR_MEMORY;
1563 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1564 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1565 if (!err) err = map_block_killer(exports, "valid-url");
1567 free_current_action(action);
1568 free_http_request(url_to_query);
1573 return JB_ERR_MEMORY;
1576 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1580 * We have a warning about SSL paths. Hide it for unencrypted sites.
1582 if (!url_to_query->ssl)
1584 if (map_block_killer(exports, "https"))
1586 free_current_action(action);
1588 free_http_request(url_to_query);
1589 return JB_ERR_MEMORY;
1593 matches = strdup("<table summary=\"\" class=\"transparent\">");
1595 for (i = 0; i < MAX_AF_FILES; i++)
1597 if (NULL == csp->config->actions_file_short[i]
1598 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1602 if ((fl = csp->actions_list[i]) != NULL)
1604 if ((b = fl->f) != NULL)
1606 /* FIXME: Hardcoded HTML! */
1607 string_append(&matches, "<tr><th>In file: ");
1608 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1609 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1610 string_append(&matches, buf);
1611 string_append(&matches, "View</a>");
1612 #ifdef FEATURE_CGI_EDIT_ACTIONS
1613 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1616 if (access(csp->config->actions_file[i], W_OK) == 0)
1618 #endif /* def HAVE_ACCESS */
1619 snprintf(buf, sizeof(buf),
1620 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1621 string_append(&matches, buf);
1622 string_append(&matches, "Edit</a>");
1627 string_append(&matches, " <strong>No write access.</strong>");
1629 #endif /* def HAVE_ACCESS */
1631 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1633 string_append(&matches, "</th></tr>\n");
1640 for (; (b != NULL) && (matches != NULL); b = b->next)
1642 if (url_match(b->url, url_to_query))
1644 string_append(&matches, "<tr><td>{");
1645 string_join (&matches, actions_to_html(csp, b->action));
1646 string_append(&matches, " }<br>\n<code>");
1647 string_join (&matches, html_encode(b->url->spec));
1648 string_append(&matches, "</code></td></tr>\n");
1650 if (merge_current_action(action, b->action))
1653 free_http_request(url_to_query);
1654 free_current_action(action);
1656 return JB_ERR_MEMORY;
1664 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1667 string_append(&matches, "</table>\n");
1670 * XXX: Kludge to make sure the "Forward settings" section
1671 * shows what forward-override{} would do with the requested URL.
1672 * No one really cares how the CGI request would be forwarded
1673 * if it wasn't intercepted as CGI request in the first place.
1675 * From here on the action bitmask will no longer reflect
1676 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1677 * but luckily it's no longer required later on anyway.
1679 free_current_action(csp->action);
1680 get_url_actions(csp, url_to_query);
1683 * Fill in forwarding settings.
1685 * The possibilities are:
1687 * - http forwarding only
1688 * - socks4(a) forwarding only
1689 * - socks4(a) and http forwarding.
1691 * XXX: Parts of this code could be reused for the
1692 * "forwarding-failed" template which currently doesn't
1693 * display the proxy port and an eventual second forwarder.
1696 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1698 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1700 if (!err) err = map_block_killer(exports, "socks-forwarder");
1701 if (!err) err = map_block_killer(exports, "http-forwarder");
1705 char port[10]; /* We save proxy ports as int but need a string here */
1707 if (!err) err = map_block_killer(exports, "no-forwarder");
1709 if (fwd->gateway_host != NULL)
1711 char *socks_type = NULL;
1716 socks_type = "socks4";
1719 socks_type = "socks4a";
1722 socks_type = "socks5";
1725 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1728 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1729 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1730 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1731 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1735 if (!err) err = map_block_killer(exports, "socks-forwarder");
1738 if (fwd->forward_host != NULL)
1740 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1741 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1742 if (!err) err = map(exports, "forward-port", 1, port, 1);
1746 if (!err) err = map_block_killer(exports, "http-forwarder");
1751 free_http_request(url_to_query);
1753 if (err || matches == NULL)
1755 free_current_action(action);
1757 return JB_ERR_MEMORY;
1760 #ifdef FEATURE_CGI_EDIT_ACTIONS
1761 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1763 err = map_block_killer(exports, "cgi-editor-is-disabled");
1765 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1768 * If zlib support is available, if no content filters
1769 * are enabled or if the prevent-compression action is enabled,
1770 * suppress the "compression could prevent filtering" warning.
1772 #ifndef FEATURE_ZLIB
1773 if (!content_filters_enabled(action) ||
1774 (action->flags & ACTION_NO_COMPRESSION))
1777 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1780 if (err || map(exports, "matches", 1, matches , 0))
1782 free_current_action(action);
1784 return JB_ERR_MEMORY;
1787 s = current_action_to_html(csp, action);
1789 free_current_action(action);
1791 if (map(exports, "final", 1, s, 0))
1794 return JB_ERR_MEMORY;
1798 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1802 /*********************************************************************
1804 * Function : cgi_robots_txt
1806 * Description : CGI function to return "/robots.txt".
1809 * 1 : csp = Current client state (buffers, headers, etc...)
1810 * 2 : rsp = http_response data structure for output
1811 * 3 : parameters = map of cgi parameters
1813 * CGI Parameters : None
1815 * Returns : JB_ERR_OK on success
1816 * JB_ERR_MEMORY on out-of-memory error.
1818 *********************************************************************/
1819 jb_err cgi_robots_txt(struct client_state *csp,
1820 struct http_response *rsp,
1821 const struct map *parameters)
1827 "# This is the Privoxy control interface.\n"
1828 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1834 if (rsp->body == NULL)
1836 return JB_ERR_MEMORY;
1839 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1843 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1844 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1846 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1850 /*********************************************************************
1852 * Function : show_defines
1854 * Description : Add to a map the state od all conditional #defines
1855 * used when building
1858 * 1 : exports = map to extend
1860 * Returns : JB_ERR_OK on success
1861 * JB_ERR_MEMORY on out-of-memory error.
1863 *********************************************************************/
1864 static jb_err show_defines(struct map *exports)
1866 jb_err err = JB_ERR_OK;
1869 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1870 #else /* ifndef FEATURE_ACL */
1871 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1872 #endif /* ndef FEATURE_ACL */
1874 #ifdef FEATURE_CGI_EDIT_ACTIONS
1875 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1876 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1877 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1878 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1880 #ifdef FEATURE_FAST_REDIRECTS
1881 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1882 #else /* ifndef FEATURE_FAST_REDIRECTS */
1883 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1884 #endif /* ndef FEATURE_FAST_REDIRECTS */
1886 #ifdef FEATURE_FORCE_LOAD
1887 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1888 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1889 #else /* ifndef FEATURE_FORCE_LOAD */
1890 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1891 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1892 #endif /* ndef FEATURE_FORCE_LOAD */
1894 #ifdef FEATURE_GRACEFUL_TERMINATION
1895 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1896 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1897 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1898 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1900 #ifdef FEATURE_IMAGE_BLOCKING
1901 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1902 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1903 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1904 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1906 #ifdef FEATURE_IMAGE_DETECT_MSIE
1907 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1908 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1909 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1910 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1912 #ifdef FEATURE_NO_GIFS
1913 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1914 #else /* ifndef FEATURE_NO_GIFS */
1915 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1916 #endif /* ndef FEATURE_NO_GIFS */
1918 #ifdef FEATURE_PTHREAD
1919 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1920 #else /* ifndef FEATURE_PTHREAD */
1921 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1922 #endif /* ndef FEATURE_PTHREAD */
1924 #ifdef FEATURE_STATISTICS
1925 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1926 #else /* ifndef FEATURE_STATISTICS */
1927 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1928 #endif /* ndef FEATURE_STATISTICS */
1930 #ifdef FEATURE_TOGGLE
1931 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1932 #else /* ifndef FEATURE_TOGGLE */
1933 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1934 #endif /* ndef FEATURE_TOGGLE */
1936 #ifdef FEATURE_TRUST
1937 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1938 #else /* ifndef FEATURE_TRUST */
1939 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1940 #endif /* ndef FEATURE_TRUST */
1943 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1944 #else /* ifndef FEATURE_ZLIB */
1945 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1946 #endif /* ndef FEATURE_ZLIB */
1949 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1950 #else /* ifndef STATIC_PCRE */
1951 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1952 #endif /* ndef STATIC_PCRE */
1955 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1956 #else /* ifndef STATIC_PCRS */
1957 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1958 #endif /* ndef STATIC_PCRS */
1964 /*********************************************************************
1966 * Function : show_rcs
1968 * Description : Create a string with the rcs info for all sourcefiles
1972 * Returns : A string, or NULL on out-of-memory.
1974 *********************************************************************/
1975 static char *show_rcs(void)
1977 char *result = strdup("");
1978 char buf[BUFFER_SIZE];
1980 /* Instead of including *all* dot h's in the project (thus creating a
1981 * tremendous amount of dependencies), I will concede to declaring them
1982 * as extern's. This forces the developer to add to this list, but oh well.
1985 #define SHOW_RCS(__x) \
1987 extern const char __x[]; \
1988 snprintf(buf, sizeof(buf), " %s\n", __x); \
1989 string_append(&result, buf); \
1992 /* In alphabetical order */
1993 SHOW_RCS(actions_h_rcs)
1994 SHOW_RCS(actions_rcs)
1996 SHOW_RCS(amiga_h_rcs)
1998 #endif /* def AMIGA */
2001 #ifdef FEATURE_CGI_EDIT_ACTIONS
2002 SHOW_RCS(cgiedit_h_rcs)
2003 SHOW_RCS(cgiedit_rcs)
2004 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
2005 SHOW_RCS(cgisimple_h_rcs)
2006 SHOW_RCS(cgisimple_rcs)
2008 SHOW_RCS(cygwin_h_rcs)
2010 SHOW_RCS(deanimate_h_rcs)
2011 SHOW_RCS(deanimate_rcs)
2012 SHOW_RCS(encode_h_rcs)
2013 SHOW_RCS(encode_rcs)
2014 SHOW_RCS(errlog_h_rcs)
2015 SHOW_RCS(errlog_rcs)
2016 SHOW_RCS(filters_h_rcs)
2017 SHOW_RCS(filters_rcs)
2018 SHOW_RCS(gateway_h_rcs)
2019 SHOW_RCS(gateway_rcs)
2020 SHOW_RCS(jbsockets_h_rcs)
2021 SHOW_RCS(jbsockets_rcs)
2024 SHOW_RCS(list_h_rcs)
2026 SHOW_RCS(loadcfg_h_rcs)
2027 SHOW_RCS(loadcfg_rcs)
2028 SHOW_RCS(loaders_h_rcs)
2029 SHOW_RCS(loaders_rcs)
2030 SHOW_RCS(miscutil_h_rcs)
2031 SHOW_RCS(miscutil_rcs)
2032 SHOW_RCS(parsers_h_rcs)
2033 SHOW_RCS(parsers_rcs)
2035 SHOW_RCS(pcrs_h_rcs)
2036 SHOW_RCS(project_h_rcs)
2037 SHOW_RCS(ssplit_h_rcs)
2038 SHOW_RCS(ssplit_rcs)
2039 SHOW_RCS(urlmatch_h_rcs)
2040 SHOW_RCS(urlmatch_rcs)
2042 #ifndef _WIN_CONSOLE
2043 SHOW_RCS(w32log_h_rcs)
2044 SHOW_RCS(w32log_rcs)
2045 SHOW_RCS(w32res_h_rcs)
2046 SHOW_RCS(w32taskbar_h_rcs)
2047 SHOW_RCS(w32taskbar_rcs)
2048 #endif /* ndef _WIN_CONSOLE */
2049 SHOW_RCS(win32_h_rcs)
2051 #endif /* def _WIN32 */
2060 /*********************************************************************
2062 * Function : cgi_show_file
2064 * Description : CGI function that shows the content of a
2065 * configuration file.
2068 * 1 : csp = Current client state (buffers, headers, etc...)
2069 * 2 : rsp = http_response data structure for output
2070 * 3 : parameters = map of cgi parameters
2073 * file : Which file to show. Only first letter is checked,
2078 * Default is to show menu and other information.
2080 * Returns : JB_ERR_OK on success
2081 * JB_ERR_MEMORY on out-of-memory error.
2083 *********************************************************************/
2084 static jb_err cgi_show_file(struct client_state *csp,
2085 struct http_response *rsp,
2086 const struct map *parameters)
2089 const char * filename = NULL;
2090 char * file_description = NULL;
2096 switch (*(lookup(parameters, "file")))
2099 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2101 filename = csp->actions_list[i]->filename;
2102 file_description = "Actions File";
2107 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2109 filename = csp->rlist[i]->filename;
2110 file_description = "Filter File";
2114 #ifdef FEATURE_TRUST
2118 filename = csp->tlist->filename;
2119 file_description = "Trust File";
2122 #endif /* def FEATURE_TRUST */
2125 if (NULL != filename)
2127 struct map *exports;
2132 exports = default_exports(csp, "show-status");
2133 if (NULL == exports)
2135 return JB_ERR_MEMORY;
2138 if ( map(exports, "file-description", 1, file_description, 1)
2139 || map(exports, "filepath", 1, html_encode(filename), 0) )
2142 return JB_ERR_MEMORY;
2145 err = load_file(filename, &s, &length);
2146 if (JB_ERR_OK != err)
2148 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2151 return JB_ERR_MEMORY;
2156 s = html_encode_and_free_original(s);
2159 return JB_ERR_MEMORY;
2162 if (map(exports, "contents", 1, s, 0))
2165 return JB_ERR_MEMORY;
2169 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2172 return JB_ERR_CGI_PARAMS;
2176 /*********************************************************************
2178 * Function : load_file
2180 * Description : Loads a file into a buffer.
2183 * 1 : filename = Name of the file to be loaded.
2184 * 2 : buffer = Used to return the file's content.
2185 * 3 : length = Used to return the size of the file.
2187 * Returns : JB_ERR_OK in case of success,
2188 * JB_ERR_FILE in case of ordinary file loading errors
2189 * (fseek() and ftell() errors are fatal)
2190 * JB_ERR_MEMORY in case of out-of-memory.
2192 *********************************************************************/
2193 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2197 jb_err err = JB_ERR_OK;
2199 fp = fopen(filename, "rb");
2205 /* Get file length */
2206 if (fseek(fp, 0, SEEK_END))
2208 log_error(LOG_LEVEL_FATAL,
2209 "Unexpected error while fseek()ing to the end of %s: %E",
2215 log_error(LOG_LEVEL_FATAL,
2216 "Unexpected ftell() error while loading %s: %E",
2219 *length = (size_t)ret;
2221 /* Go back to the beginning. */
2222 if (fseek(fp, 0, SEEK_SET))
2224 log_error(LOG_LEVEL_FATAL,
2225 "Unexpected error while fseek()ing to the beginning of %s: %E",
2229 *buffer = (char *)zalloc(*length + 1);
2230 if (NULL == *buffer)
2232 err = JB_ERR_MEMORY;
2234 else if (!fread(*buffer, *length, 1, fp))
2237 * May happen if the file size changes between fseek() and
2238 * fread(). If it does, we just log it and serve what we got.
2240 log_error(LOG_LEVEL_ERROR,
2241 "Couldn't completely read file %s.", filename);