1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.88 2008/08/30 12:03:07 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.88 2008/08/30 12:03:07 fabiankeil
40 * Remove FEATURE_COOKIE_JAR.
42 * Revision 1.87 2008/08/29 15:59:22 fabiankeil
45 * Revision 1.86 2008/06/28 14:19:05 fabiankeil
46 * Protocol detection is done case-insensitive, fix assertion
47 * to do the same. Yay for Privoxy-Regression-Test and zzuf.
49 * Revision 1.85 2008/05/26 17:30:55 fabiankeil
50 * Provide an OpenSearch Description to access the
51 * show-url-info page through "search engine plugins".
53 * Revision 1.84 2008/05/26 16:16:55 fabiankeil
54 * Spell error correctly.
56 * Revision 1.83 2008/05/12 14:51:30 fabiankeil
57 * Don't complain about an invalid URL if show-url-info is requested
58 * without parameters. Regression introduced in 1.81 by yours truly.
60 * Revision 1.82 2008/05/10 20:01:47 fabiankeil
61 * Fix an assertion that could erroneously
62 * trigger in case of memory shortage.
64 * Revision 1.81 2008/05/05 09:54:39 fabiankeil
65 * In cgi_show_url_info(), make sure ftp URLs are
66 * declared invalid. Also simplify the code that adds
67 * "http://" if no protocol has been specified.
69 * Revision 1.80 2008/05/04 16:18:32 fabiankeil
70 * Provide parse_http_url() with a third parameter to specify
71 * whether or not URLs without protocol are acceptable.
73 * Revision 1.79 2008/05/04 13:30:56 fabiankeil
74 * Streamline parse_http_url()'s prototype.
76 * Revision 1.78 2008/05/03 16:50:11 fabiankeil
77 * Leverage content_filters_enabled() in cgi_show_url_info().
79 * Revision 1.77 2008/05/02 09:47:48 fabiankeil
80 * In cgi_show_url_info, pass an initialized http structure
81 * to parse_http_url() as that will be required soonish and
82 * assert that https URLs are recognized correctly.
84 * Revision 1.76 2008/04/28 09:13:30 fabiankeil
85 * In load_file(), remember the error reason and fclose()
86 * and return later on instead of right away.
88 * Revision 1.75 2008/04/27 13:52:52 fabiankeil
89 * Move CGI file loading code into load_file() and
90 * add checks for unexpected errors.
92 * Revision 1.74 2008/04/26 15:50:56 fabiankeil
93 * Fix macro name in cgi_show_file() error path.
95 * Revision 1.73 2008/04/26 12:21:55 fabiankeil
96 * Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
98 * Revision 1.72 2008/04/26 10:34:15 fabiankeil
99 * If zlib support is unavailable and there are content filters active
100 * but the prevent-compression action is disabled, include a warning
101 * on the show-url-info page that compression might prevent filtering.
103 * Revision 1.71 2008/04/25 13:33:56 fabiankeil
104 * - Factor cgi_show_file() out of cgi_show_status().
105 * - Adjust cgi_show_status()'s parameter description to match reality.
107 * Revision 1.70 2008/04/24 16:12:38 fabiankeil
108 * In cgi_show_status(), load the requested file at once.
109 * Using string_join() for every line really doesn't scale.
111 * Revision 1.69 2008/04/17 14:40:48 fabiankeil
112 * Provide get_http_time() with the buffer size so it doesn't
113 * have to blindly assume that the buffer is big enough.
115 * Revision 1.68 2008/03/27 18:27:21 fabiankeil
116 * Remove kill-popups action.
118 * Revision 1.67 2008/03/27 17:00:05 fabiankeil
119 * Turn the favicon blobs into locals.
121 * Revision 1.66 2008/02/23 16:57:12 fabiankeil
122 * Rename url_actions() to get_url_actions() and let it
123 * use the standard parameter ordering.
125 * Revision 1.65 2008/02/23 16:33:43 fabiankeil
126 * Let forward_url() use the standard parameter ordering
127 * and mark its second parameter immutable.
129 * Revision 1.64 2008/02/03 13:56:07 fabiankeil
130 * Add SOCKS5 support for show-url-info CGI page.
132 * Revision 1.63 2008/02/01 06:04:31 fabiankeil
133 * If edit buttons on the show-url-info CGI page are hidden, explain why.
135 * Revision 1.62 2008/02/01 05:52:40 fabiankeil
136 * Hide edit buttons on the show-url-info CGI page if enable-edit-action
137 * is disabled. Patch by Lee with additional white space adjustments.
139 * Revision 1.61 2008/01/26 11:13:25 fabiankeil
140 * If enable-edit-actions is disabled, hide the edit buttons and explain why.
142 * Revision 1.60 2007/10/27 13:12:13 fabiankeil
143 * Finish 1.49 and check write access before
144 * showing edit buttons on show-url-info page.
146 * Revision 1.59 2007/10/19 16:42:36 fabiankeil
147 * Plug memory leak I introduced five months ago.
148 * Yay Valgrind and Privoxy-Regression-Test.
150 * Revision 1.58 2007/07/21 12:19:50 fabiankeil
151 * If show-url-info is called with an URL that Privoxy
152 * would reject as invalid, don't show unresolved forwarding
153 * variables, "final matches" or claim the site's secure.
155 * Revision 1.57 2007/06/01 16:53:05 fabiankeil
156 * Adjust cgi_show_url_info() to show what forward-override{}
157 * would do with the requested URL (instead of showing how the
158 * request for the CGI page would be forwarded if it wasn't a
161 * Revision 1.56 2007/05/21 10:50:35 fabiankeil
162 * - Use strlcpy() instead of strcpy().
163 * - Stop treating actions files special. Expect a complete file name
164 * (with or without path) like it's done for the rest of the files.
166 * - Don't rerun sed() in cgi_show_request().
168 * Revision 1.55 2007/04/13 13:36:46 fabiankeil
169 * Reference action files in CGI URLs by id instead
170 * of using the first part of the file name.
171 * Fixes BR 1694250 and BR 1590556.
173 * Revision 1.54 2007/04/09 18:11:35 fabiankeil
174 * Don't mistake VC++'s _snprintf() for a snprintf() replacement.
176 * Revision 1.53 2007/04/08 13:21:04 fabiankeil
177 * Reference action files in CGI URLs by id instead
178 * of using the first part of the file name.
179 * Fixes BR 1694250 and BR 1590556.
181 * Revision 1.52 2007/02/13 15:10:26 fabiankeil
182 * Apparently fopen()ing in "binary" mode doesn't require
183 * #ifdefs, it's already done without them in cgiedit.c.
185 * Revision 1.51 2007/02/10 16:55:22 fabiankeil
186 * - Show forwarding settings on the show-url-info page
187 * - Fix some HTML syntax errors.
189 * Revision 1.50 2007/01/23 15:51:17 fabiankeil
190 * Add favicon delivery functions.
192 * Revision 1.49 2007/01/20 16:29:38 fabiankeil
193 * Suppress edit buttons for action files if Privoxy has
194 * no write access. Suggested by Roland in PR 1564026.
196 * Revision 1.48 2007/01/20 15:31:31 fabiankeil
197 * Display warning if show-url-info CGI page
198 * is used while Privoxy is toggled off.
200 * Revision 1.47 2007/01/12 15:07:10 fabiankeil
201 * Use zalloc in cgi_send_user_manual.
203 * Revision 1.46 2007/01/02 12:49:46 fabiankeil
204 * Add FEATURE_ZLIB to the list of conditional
205 * defines at the show-status page.
207 * Revision 1.45 2006/12/28 18:16:41 fabiankeil
208 * Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
209 * body memory before using it, replaced sprintf calls with snprintf.
211 * Revision 1.44 2006/12/22 14:19:27 fabiankeil
212 * Removed checks whether or not AF_FILES have
213 * data structures associated with them in cgi_show_status.
214 * It doesn't matter as we're only interested in the file names.
216 * For the action files the checks were always true,
217 * but they prevented empty filter files from being
218 * listed. Fixes parts of BR 1619208.
220 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
221 * - Added FEATURE_GRACEFUL_TERMINATION to the
222 * "conditional #defines" section
223 * - Escaped ampersands in generated HTML.
224 * - Renamed re-filter-filename to re-filter-filenames
226 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
227 * Add special treatment for WIN32 to make sure
228 * cgi_send_user_manual opens the files in binary mode.
229 * Fixes BR 1600411 and unbreaks image delivery.
231 * Remove outdated comment.
233 * Revision 1.41 2006/10/09 19:18:28 roro
234 * Redirect http://p.p/user-manual (without trailing slash) to
235 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
237 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
238 * Modified cgi_send_user_manual to serve binary
239 * content without destroying it first. Should also be
240 * faster now. Added ".jpg" check for Content-Type guessing.
242 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
243 * Deliver documents in the user-manual directory
244 * with "Content-Type text/css" if their filename
247 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
248 * Incorporate modified version of Roland Rosenfeld's patch to
249 * optionally access the user-manual via Privoxy. Closes patch 679075.
251 * Formatting changed to Privoxy style, added call to
252 * cgi_error_no_template if the requested file doesn't
253 * exist and modified check whether or not Privoxy itself
254 * should serve the manual. Should work cross-platform now.
256 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
257 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
258 * with what was really the latest development (the v_3_0_branch branch)
260 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
261 * Multiple filter file support
263 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
264 * Undo some damaging memory leak patches
266 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
267 * A few memory leaks plugged (mostly on error paths)
269 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
270 * Another instance of:
271 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
272 * Thanks to Magnus Holmgren for the patch
274 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
275 * - Prevent line wrap beween "View/Edit" link buttons on status page
276 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
278 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
279 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
281 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
282 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
284 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
285 * Applying patch from Andreas as provided by Hal on the list.
286 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
288 * Revision 1.35 2002/05/12 21:44:44 jongfoster
289 * Adding amiga.[ch] revision information, if on an amiga.
291 * Revision 1.34 2002/04/30 12:06:12 oes
292 * Deleted unused code from default_cgi
294 * Revision 1.33 2002/04/30 11:14:52 oes
295 * Made csp the first parameter in *action_to_html
297 * Revision 1.32 2002/04/26 18:29:13 jongfoster
298 * Fixing this Visual C++ warning:
299 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
301 * Revision 1.31 2002/04/26 12:54:36 oes
302 * - Kill obsolete REDIRECT_URL code
303 * - Error handling fixes
304 * - Style sheet related HTML snipplet changes
305 * - cgi_show_url_info:
306 * - Matches now in table, actions on single lines,
308 * - standard.action suppressed
309 * - Buttons to View and Edit AFs
311 * Revision 1.30 2002/04/24 02:18:08 oes
312 * - show-status is now the starting point for editing
313 * the actions files, generate list of all AFs with buttons
314 * for viewing and editing, new look for file list (Jon:
315 * buttons now aligned ;-P ), view mode now supports multiple
316 * AFs, name changes, no view links for unspecified files,
317 * no edit link for standard.action.
319 * - Jon's multiple AF patch: cgi_show_url_info now uses all
320 * AFs and marks the output accordingly
322 * Revision 1.29 2002/04/10 13:38:35 oes
323 * load_template signature changed
325 * Revision 1.28 2002/04/07 15:42:12 jongfoster
326 * Fixing send-banner?type=auto when the image-blocker is
327 * a redirect to send-banner
329 * Revision 1.27 2002/04/05 15:50:48 oes
330 * added send-stylesheet CGI
332 * Revision 1.26 2002/04/04 00:36:36 gliptak
333 * always use pcre for matching
335 * Revision 1.25 2002/04/03 22:28:03 gliptak
336 * Removed references to gnu_regex
338 * Revision 1.24 2002/04/02 16:12:47 oes
339 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
341 * Revision 1.23 2002/03/26 22:29:54 swa
342 * we have a new homepage!
344 * Revision 1.22 2002/03/24 16:18:15 jongfoster
347 * Revision 1.21 2002/03/24 15:23:33 jongfoster
350 * Revision 1.20 2002/03/24 13:25:43 swa
351 * name change related issues
353 * Revision 1.19 2002/03/16 23:54:06 jongfoster
354 * Adding graceful termination feature, to help look for memory leaks.
355 * If you enable this (which, by design, has to be done by hand
356 * editing config.h) and then go to http://i.j.b/die, then the program
357 * will exit cleanly after the *next* request. It should free all the
358 * memory that was used.
360 * Revision 1.18 2002/03/12 01:44:49 oes
361 * Changed default for "blocked" image from jb logo to checkboard pattern
363 * Revision 1.17 2002/03/08 16:43:18 oes
364 * Added choice beween GIF and PNG built-in images
366 * Revision 1.16 2002/03/07 03:48:38 oes
367 * - Changed built-in images from GIF to PNG
368 * (with regard to Unisys patent issue)
369 * - Added a 4x4 pattern PNG which is less intrusive
370 * than the logo but also clearly marks the deleted banners
372 * Revision 1.15 2002/03/06 22:54:35 jongfoster
373 * Automated function-comment nitpicking.
375 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
376 * Clean up a little CRLF unpleasantness that suddenly appeared
378 * Revision 1.13 2002/02/21 00:10:37 jongfoster
379 * Adding send-banner?type=auto option
381 * Revision 1.12 2002/01/23 01:03:32 jongfoster
382 * Fixing gcc [CygWin] compiler warnings
384 * Revision 1.11 2002/01/23 00:01:04 jongfoster
385 * Adding cgi_transparent_gif() for http://i.j.b/t
386 * Adding missing html_encode() to many CGI functions.
387 * Adding urlmatch.[ch] to http://i.j.b/show-version
389 * Revision 1.10 2002/01/17 21:10:37 jongfoster
390 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
391 * Also fixing a problem in the same function with improperly quoted URLs
392 * in output HTML, and adding code to handle https:// URLs correctly.
394 * Revision 1.9 2001/11/30 23:09:15 jongfoster
395 * Now reports on FEATURE_CGI_EDIT_ACTIONS
396 * Removing FEATURE_DENY_GZIP from template
398 * Revision 1.8 2001/11/13 00:14:07 jongfoster
399 * Fixing stupid bug now I've figured out what || means.
400 * (It always returns 0 or 1, not one of it's paramaters.)
402 * Revision 1.7 2001/10/23 21:48:19 jongfoster
403 * Cleaning up error handling in CGI functions - they now send back
404 * a HTML error page and should never cause a FATAL error. (Fixes one
405 * potential source of "denial of service" attacks).
407 * CGI actions file editor that works and is actually useful.
409 * Ability to toggle JunkBuster remotely using a CGI call.
411 * You can turn off both the above features in the main configuration
412 * file, e.g. if you are running a multi-user proxy.
414 * Revision 1.6 2001/10/14 22:00:32 jongfoster
415 * Adding support for a 404 error when an invalid CGI page is requested.
417 * Revision 1.5 2001/10/07 15:30:41 oes
418 * Removed FEATURE_DENY_GZIP
420 * Revision 1.4 2001/10/02 15:31:12 oes
421 * Introduced show-request cgi
423 * Revision 1.3 2001/09/22 16:34:44 jongfoster
424 * Removing unneeded #includes
426 * Revision 1.2 2001/09/19 18:01:11 oes
427 * Fixed comments; cosmetics
429 * Revision 1.1 2001/09/16 17:08:54 jongfoster
430 * Moving simple CGI functions from cgi.c to new file cgisimple.c
433 **********************************************************************/
439 #include <sys/types.h>
447 #endif /* def HAVE_ACCESS */
451 #include "cgisimple.h"
457 #include "miscutil.h"
460 #include "urlmatch.h"
463 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
465 static char *show_rcs(void);
466 static jb_err show_defines(struct map *exports);
467 static jb_err cgi_show_file(struct client_state *csp,
468 struct http_response *rsp,
469 const struct map *parameters);
470 static jb_err load_file(const char *filename, char **buffer, size_t *length);
472 /*********************************************************************
474 * Function : cgi_default
476 * Description : CGI function that is called for the CGI_SITE_1_HOST
477 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
478 * Boring - only exports the default exports.
481 * 1 : csp = Current client state (buffers, headers, etc...)
482 * 2 : rsp = http_response data structure for output
483 * 3 : parameters = map of cgi parameters
485 * CGI Parameters : none
487 * Returns : JB_ERR_OK on success
488 * JB_ERR_MEMORY on out-of-memory
490 *********************************************************************/
491 jb_err cgi_default(struct client_state *csp,
492 struct http_response *rsp,
493 const struct map *parameters)
500 if (NULL == (exports = default_exports(csp, "")))
502 return JB_ERR_MEMORY;
505 return template_fill_for_cgi(csp, "default", exports, rsp);
509 /*********************************************************************
511 * Function : cgi_error_404
513 * Description : CGI function that is called if an unknown action was
517 * 1 : csp = Current client state (buffers, headers, etc...)
518 * 2 : rsp = http_response data structure for output
519 * 3 : parameters = map of cgi parameters
521 * CGI Parameters : none
523 * Returns : JB_ERR_OK on success
524 * JB_ERR_MEMORY on out-of-memory error.
526 *********************************************************************/
527 jb_err cgi_error_404(struct client_state *csp,
528 struct http_response *rsp,
529 const struct map *parameters)
537 if (NULL == (exports = default_exports(csp, NULL)))
539 return JB_ERR_MEMORY;
542 rsp->status = strdup("404 Privoxy configuration page not found");
543 if (rsp->status == NULL)
546 return JB_ERR_MEMORY;
549 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
553 #ifdef FEATURE_GRACEFUL_TERMINATION
554 /*********************************************************************
558 * Description : CGI function to shut down Privoxy.
559 * NOTE: Turning this on in a production build
560 * would be a BAD idea. An EXTREMELY BAD idea.
561 * In short, don't do it.
564 * 1 : csp = Current client state (buffers, headers, etc...)
565 * 2 : rsp = http_response data structure for output
566 * 3 : parameters = map of cgi parameters
568 * CGI Parameters : none
570 * Returns : JB_ERR_OK on success
571 * JB_ERR_MEMORY on out-of-memory error.
573 *********************************************************************/
574 jb_err cgi_die (struct client_state *csp,
575 struct http_response *rsp,
576 const struct map *parameters)
586 * I don't really care what gets sent back to the browser.
587 * Take the easy option - "out of memory" page.
590 return JB_ERR_MEMORY;
592 #endif /* def FEATURE_GRACEFUL_TERMINATION */
595 /*********************************************************************
597 * Function : cgi_show_request
599 * Description : Show the client's request and what sed() would have
603 * 1 : csp = Current client state (buffers, headers, etc...)
604 * 2 : rsp = http_response data structure for output
605 * 3 : parameters = map of cgi parameters
607 * CGI Parameters : none
609 * Returns : JB_ERR_OK on success
610 * JB_ERR_MEMORY on out-of-memory error.
612 *********************************************************************/
613 jb_err cgi_show_request(struct client_state *csp,
614 struct http_response *rsp,
615 const struct map *parameters)
624 if (NULL == (exports = default_exports(csp, "show-request")))
626 return JB_ERR_MEMORY;
630 * Repair the damage done to the IOB by get_header()
632 for (p = csp->iob->buf; p < csp->iob->eod; p++)
634 if (*p == '\0') *p = '\n';
638 * Export the original client's request and the one we would
639 * be sending to the server if this wasn't a CGI call
642 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
645 return JB_ERR_MEMORY;
648 if (map(exports, "processed-request", 1,
649 html_encode_and_free_original(list_to_text(csp->headers)), 0))
652 return JB_ERR_MEMORY;
655 return template_fill_for_cgi(csp, "show-request", exports, rsp);
659 /*********************************************************************
661 * Function : cgi_send_banner
663 * Description : CGI function that returns a banner.
666 * 1 : csp = Current client state (buffers, headers, etc...)
667 * 2 : rsp = http_response data structure for output
668 * 3 : parameters = map of cgi parameters
671 * type : Selects the type of banner between "trans", "logo",
672 * and "auto". Defaults to "logo" if absent or invalid.
673 * "auto" means to select as if we were image-blocking.
674 * (Only the first character really counts; b and t are
677 * Returns : JB_ERR_OK on success
678 * JB_ERR_MEMORY on out-of-memory error.
680 *********************************************************************/
681 jb_err cgi_send_banner(struct client_state *csp,
682 struct http_response *rsp,
683 const struct map *parameters)
685 char imagetype = lookup(parameters, "type")[0];
688 * If type is auto, then determine the right thing
689 * to do from the set-image-blocker action
691 if (imagetype == 'a')
698 #ifdef FEATURE_IMAGE_BLOCKING
699 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
701 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
702 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
703 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
707 /* Use default - nothing to do here. */
709 else if (0 == strcmpic(p, "blank"))
713 else if (0 == strcmpic(p, "pattern"))
719 * If the action is to call this CGI, determine
722 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
724 imagetype = p[sizeof(prefix1) - 1];
726 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
728 imagetype = p[sizeof(prefix2) - 1];
732 * Everything else must (should) be a URL to
740 #endif /* def FEATURE_IMAGE_BLOCKING */
744 * Now imagetype is either the non-auto type we were called with,
745 * or it was auto and has since been determined. In any case, we
746 * can proceed to actually answering the request by sending a redirect
747 * or an image as appropriate:
749 if (imagetype == 'r')
751 rsp->status = strdup("302 Local Redirect from Privoxy");
752 if (rsp->status == NULL)
754 return JB_ERR_MEMORY;
756 if (enlist_unique_header(rsp->headers, "Location",
757 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
759 return JB_ERR_MEMORY;
764 if ((imagetype == 'b') || (imagetype == 't'))
766 rsp->body = bindup(image_blank_data, image_blank_length);
767 rsp->content_length = image_blank_length;
771 rsp->body = bindup(image_pattern_data, image_pattern_length);
772 rsp->content_length = image_pattern_length;
775 if (rsp->body == NULL)
777 return JB_ERR_MEMORY;
779 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
781 return JB_ERR_MEMORY;
792 /*********************************************************************
794 * Function : cgi_transparent_image
796 * Description : CGI function that sends a 1x1 transparent image.
799 * 1 : csp = Current client state (buffers, headers, etc...)
800 * 2 : rsp = http_response data structure for output
801 * 3 : parameters = map of cgi parameters
803 * CGI Parameters : None
805 * Returns : JB_ERR_OK on success
806 * JB_ERR_MEMORY on out-of-memory error.
808 *********************************************************************/
809 jb_err cgi_transparent_image(struct client_state *csp,
810 struct http_response *rsp,
811 const struct map *parameters)
813 rsp->body = bindup(image_blank_data, image_blank_length);
814 rsp->content_length = image_blank_length;
816 if (rsp->body == NULL)
818 return JB_ERR_MEMORY;
821 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
823 return JB_ERR_MEMORY;
833 /*********************************************************************
835 * Function : cgi_send_default_favicon
837 * Description : CGI function that sends the standard favicon.
840 * 1 : csp = Current client state (buffers, headers, etc...)
841 * 2 : rsp = http_response data structure for output
842 * 3 : parameters = map of cgi parameters
844 * CGI Parameters : None
846 * Returns : JB_ERR_OK on success
847 * JB_ERR_MEMORY on out-of-memory error.
849 *********************************************************************/
850 jb_err cgi_send_default_favicon(struct client_state *csp,
851 struct http_response *rsp,
852 const struct map *parameters)
854 static const char default_favicon_data[] =
855 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
856 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
857 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
858 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
859 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
860 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
861 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
862 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
863 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
864 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
865 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
866 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
867 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
869 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
871 rsp->body = bindup(default_favicon_data, favicon_length);
872 rsp->content_length = favicon_length;
874 if (rsp->body == NULL)
876 return JB_ERR_MEMORY;
879 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
881 return JB_ERR_MEMORY;
891 /*********************************************************************
893 * Function : cgi_send_error_favicon
895 * Description : CGI function that sends the favicon for error pages.
898 * 1 : csp = Current client state (buffers, headers, etc...)
899 * 2 : rsp = http_response data structure for output
900 * 3 : parameters = map of cgi parameters
902 * CGI Parameters : None
904 * Returns : JB_ERR_OK on success
905 * JB_ERR_MEMORY on out-of-memory error.
907 *********************************************************************/
908 jb_err cgi_send_error_favicon(struct client_state *csp,
909 struct http_response *rsp,
910 const struct map *parameters)
912 static const char error_favicon_data[] =
913 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
914 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
915 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
916 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
917 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
918 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
919 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
920 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
921 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
922 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
923 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
924 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
925 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
927 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
929 rsp->body = bindup(error_favicon_data, favicon_length);
930 rsp->content_length = favicon_length;
932 if (rsp->body == NULL)
934 return JB_ERR_MEMORY;
937 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
939 return JB_ERR_MEMORY;
949 /*********************************************************************
951 * Function : cgi_send_stylesheet
953 * Description : CGI function that sends a css stylesheet found
954 * in the cgi-style.css template
957 * 1 : csp = Current client state (buffers, headers, etc...)
958 * 2 : rsp = http_response data structure for output
959 * 3 : parameters = map of cgi parameters
961 * CGI Parameters : None
963 * Returns : JB_ERR_OK on success
964 * JB_ERR_MEMORY on out-of-memory error.
966 *********************************************************************/
967 jb_err cgi_send_stylesheet(struct client_state *csp,
968 struct http_response *rsp,
969 const struct map *parameters)
976 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
978 if (err == JB_ERR_FILE)
981 * No way to tell user; send empty stylesheet
983 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
987 return err; /* JB_ERR_MEMORY */
990 if (enlist(rsp->headers, "Content-Type: text/css"))
992 return JB_ERR_MEMORY;
1000 /*********************************************************************
1002 * Function : cgi_send_url_info_osd
1004 * Description : CGI function that sends the OpenSearch Description
1005 * template for the show-url-info page. It allows to
1006 * access the page through "search engine plugins".
1009 * 1 : csp = Current client state (buffers, headers, etc...)
1010 * 2 : rsp = http_response data structure for output
1011 * 3 : parameters = map of cgi parameters
1013 * CGI Parameters : None
1015 * Returns : JB_ERR_OK on success
1016 * JB_ERR_MEMORY on out-of-memory error.
1018 *********************************************************************/
1019 jb_err cgi_send_url_info_osd(struct client_state *csp,
1020 struct http_response *rsp,
1021 const struct map *parameters)
1023 jb_err err = JB_ERR_MEMORY;
1024 struct map *exports = default_exports(csp, NULL);
1026 if (NULL != exports)
1028 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1029 if (JB_ERR_OK == err)
1031 err = enlist(rsp->headers,
1032 "Content-Type: application/opensearchdescription+xml");
1041 /*********************************************************************
1043 * Function : cgi_send_user_manual
1045 * Description : CGI function that sends a file in the user
1049 * 1 : csp = Current client state (buffers, headers, etc...)
1050 * 2 : rsp = http_response data structure for output
1051 * 3 : parameters = map of cgi parameters
1053 * CGI Parameters : file=name.html, the name of the HTML file
1054 * (relative to user-manual from config)
1056 * Returns : JB_ERR_OK on success
1057 * JB_ERR_MEMORY on out-of-memory error.
1059 *********************************************************************/
1060 jb_err cgi_send_user_manual(struct client_state *csp,
1061 struct http_response *rsp,
1062 const struct map *parameters)
1064 const char * filename;
1066 jb_err err = JB_ERR_OK;
1073 if (!parameters->first)
1075 /* requested http://p.p/user-manual (without trailing slash) */
1076 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1079 get_string_param(parameters, "file", &filename);
1080 /* Check paramter for hack attempts */
1081 if (filename && strchr(filename, '/'))
1083 return JB_ERR_CGI_PARAMS;
1085 if (filename && strstr(filename, ".."))
1087 return JB_ERR_CGI_PARAMS;
1090 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1091 if (full_path == NULL)
1093 return JB_ERR_MEMORY;
1096 err = load_file(full_path, &rsp->body, &rsp->content_length);
1097 if (JB_ERR_OK != err)
1099 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1100 if (JB_ERR_FILE == err)
1102 err = cgi_error_no_template(csp, rsp, full_path);
1109 /* Guess correct Content-Type based on the filename's ending */
1112 length = strlen(filename);
1118 if((length>=4) && !strcmp(&filename[length-4], ".css"))
1120 err = enlist(rsp->headers, "Content-Type: text/css");
1122 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1124 err = enlist(rsp->headers, "Content-Type: image/jpeg");
1128 err = enlist(rsp->headers, "Content-Type: text/html");
1135 /*********************************************************************
1137 * Function : cgi_show_version
1139 * Description : CGI function that returns a a web page describing the
1140 * file versions of Privoxy.
1143 * 1 : csp = Current client state (buffers, headers, etc...)
1144 * 2 : rsp = http_response data structure for output
1145 * 3 : parameters = map of cgi parameters
1147 * CGI Parameters : none
1149 * Returns : JB_ERR_OK on success
1150 * JB_ERR_MEMORY on out-of-memory error.
1152 *********************************************************************/
1153 jb_err cgi_show_version(struct client_state *csp,
1154 struct http_response *rsp,
1155 const struct map *parameters)
1157 struct map *exports;
1163 if (NULL == (exports = default_exports(csp, "show-version")))
1165 return JB_ERR_MEMORY;
1168 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1171 return JB_ERR_MEMORY;
1174 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1178 /*********************************************************************
1180 * Function : cgi_show_status
1182 * Description : CGI function that returns a web page describing the
1183 * current status of Privoxy.
1186 * 1 : csp = Current client state (buffers, headers, etc...)
1187 * 2 : rsp = http_response data structure for output
1188 * 3 : parameters = map of cgi parameters
1191 * file : Which file to show. Only first letter is checked,
1196 * Default is to show menu and other information.
1198 * Returns : JB_ERR_OK on success
1199 * JB_ERR_MEMORY on out-of-memory error.
1201 *********************************************************************/
1202 jb_err cgi_show_status(struct client_state *csp,
1203 struct http_response *rsp,
1204 const struct map *parameters)
1210 char buf[BUFFER_SIZE];
1211 #ifdef FEATURE_STATISTICS
1212 float perc_rej; /* Percentage of http requests rejected */
1213 int local_urls_read;
1214 int local_urls_rejected;
1215 #endif /* ndef FEATURE_STATISTICS */
1216 jb_err err = JB_ERR_OK;
1218 struct map *exports;
1224 if ('\0' != *(lookup(parameters, "file")))
1226 return cgi_show_file(csp, rsp, parameters);
1229 if (NULL == (exports = default_exports(csp, "show-status")))
1231 return JB_ERR_MEMORY;
1235 for (j = 0; (s != NULL) && (j < Argc); j++)
1237 if (!err) err = string_join (&s, html_encode(Argv[j]));
1238 if (!err) err = string_append(&s, " ");
1240 if (!err) err = map(exports, "invocation", 1, s, 0);
1242 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1243 if (!err) err = show_defines(exports);
1248 return JB_ERR_MEMORY;
1251 #ifdef FEATURE_STATISTICS
1252 local_urls_read = urls_read;
1253 local_urls_rejected = urls_rejected;
1256 * Need to alter the stats not to include the fetch of this
1259 * Can't do following thread safely! doh!
1262 * urls_rejected--; * This will be incremented subsequently *
1265 if (local_urls_read == 0)
1267 if (!err) err = map_block_killer(exports, "have-stats");
1271 if (!err) err = map_block_killer(exports, "have-no-stats");
1273 perc_rej = (float)local_urls_rejected * 100.0F /
1274 (float)local_urls_read;
1276 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1277 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1279 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1280 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1282 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1283 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1286 #else /* ndef FEATURE_STATISTICS */
1287 err = err || map_block_killer(exports, "statistics");
1288 #endif /* ndef FEATURE_STATISTICS */
1291 * List all action files in use, together with view and edit links,
1292 * except for standard.action, which should only be viewable. (Not
1293 * enforced in the editor itself)
1294 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1297 for (i = 0; i < MAX_AF_FILES; i++)
1299 if (csp->actions_list[i] != NULL)
1301 if (!err) err = string_append(&s, "<tr><td>");
1302 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1303 snprintf(buf, sizeof(buf),
1304 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%d\">View</a>", i);
1305 if (!err) err = string_append(&s, buf);
1307 #ifdef FEATURE_CGI_EDIT_ACTIONS
1308 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1309 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1310 && (NULL != csp->config->actions_file_short[i]))
1313 if (access(csp->config->actions_file[i], W_OK) == 0)
1315 #endif /* def HAVE_ACCESS */
1316 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%d\">Edit</a>", i);
1317 if (!err) err = string_append(&s, buf);
1322 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1324 #endif /* def HAVE_ACCESS */
1328 if (!err) err = string_append(&s, "</td></tr>\n");
1333 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1337 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1341 * List all re_filterfiles in use, together with view options.
1342 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1345 for (i = 0; i < MAX_AF_FILES; i++)
1347 if (csp->rlist[i] != NULL)
1349 if (!err) err = string_append(&s, "<tr><td>");
1350 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1352 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%d\">View</a>", i);
1353 if (!err) err = string_append(&s, buf);
1354 if (!err) err = string_append(&s, "</td></tr>\n");
1359 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1363 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1364 if (!err) err = map_block_killer(exports, "have-filterfile");
1367 #ifdef FEATURE_TRUST
1370 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1374 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1375 if (!err) err = map_block_killer(exports, "have-trustfile");
1378 if (!err) err = map_block_killer(exports, "trust-support");
1379 #endif /* ndef FEATURE_TRUST */
1381 #ifdef FEATURE_CGI_EDIT_ACTIONS
1382 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1384 err = map_block_killer(exports, "cgi-editor-is-disabled");
1386 #endif /* ndef CGI_EDIT_ACTIONS */
1391 return JB_ERR_MEMORY;
1394 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1398 /*********************************************************************
1400 * Function : cgi_show_url_info
1402 * Description : CGI function that determines and shows which actions
1403 * Privoxy will perform for a given url, and which
1404 * matches starting from the defaults have lead to that.
1407 * 1 : csp = Current client state (buffers, headers, etc...)
1408 * 2 : rsp = http_response data structure for output
1409 * 3 : parameters = map of cgi parameters
1412 * url : The url whose actions are to be determined.
1413 * If url is unset, the url-given conditional will be
1414 * set, so that all but the form can be suppressed in
1417 * Returns : JB_ERR_OK on success
1418 * JB_ERR_MEMORY on out-of-memory error.
1420 *********************************************************************/
1421 jb_err cgi_show_url_info(struct client_state *csp,
1422 struct http_response *rsp,
1423 const struct map *parameters)
1426 struct map *exports;
1433 if (NULL == (exports = default_exports(csp, "show-url-info")))
1435 return JB_ERR_MEMORY;
1439 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1441 url_param = strdup(lookup(parameters, "url"));
1442 if (url_param == NULL)
1445 return JB_ERR_MEMORY;
1450 * Handle prefixes. 4 possibilities:
1451 * 1) "http://" or "https://" prefix present and followed by URL - OK
1452 * 2) Only the "http://" or "https://" part is present, no URL - change
1453 * to empty string so it will be detected later as "no URL".
1454 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1456 * 4) Parameter not specified or is empty string - let this fall through
1457 * for now, next block of code will handle it.
1459 if (0 == strncmp(url_param, "http://", 7))
1461 if (url_param[7] == '\0')
1464 * Empty URL (just prefix).
1465 * Make it totally empty so it's caught by the next if()
1467 url_param[0] = '\0';
1470 else if (0 == strncmp(url_param, "https://", 8))
1472 if (url_param[8] == '\0')
1475 * Empty URL (just prefix).
1476 * Make it totally empty so it's caught by the next if()
1478 url_param[0] = '\0';
1481 else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1483 /* No prefix - assume http:// */
1484 char *url_param_prefixed = strdup("http://");
1486 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1489 return JB_ERR_MEMORY;
1491 url_param = url_param_prefixed;
1495 * Hide "toggle off" warning if Privoxy is toggled on.
1498 #ifdef FEATURE_TOGGLE
1499 (global_toggle_state == 1) &&
1500 #endif /* def FEATURE_TOGGLE */
1501 map_block_killer(exports, "privoxy-is-toggled-off")
1505 return JB_ERR_MEMORY;
1508 if (url_param[0] == '\0')
1510 /* URL paramater not specified, display query form only. */
1512 if (map_block_killer(exports, "url-given")
1513 || map(exports, "url", 1, "", 1))
1516 return JB_ERR_MEMORY;
1521 /* Given a URL, so query it. */
1526 struct file_list *fl;
1527 struct url_actions *b;
1528 struct http_request url_to_query[1];
1529 struct current_action_spec action[1];
1532 if (map(exports, "url", 1, html_encode(url_param), 0))
1536 return JB_ERR_MEMORY;
1539 init_current_action(action);
1541 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1543 free_current_action(action);
1546 return JB_ERR_MEMORY;
1549 memset(url_to_query, '\0', sizeof(url_to_query));
1550 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1551 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1555 if (err == JB_ERR_MEMORY)
1557 free_http_request(url_to_query);
1558 free_current_action(action);
1560 return JB_ERR_MEMORY;
1566 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1567 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1568 if (!err) err = map_block_killer(exports, "valid-url");
1570 free_current_action(action);
1571 free_http_request(url_to_query);
1576 return JB_ERR_MEMORY;
1579 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1583 * We have a warning about SSL paths. Hide it for unencrypted sites.
1585 if (!url_to_query->ssl)
1587 if (map_block_killer(exports, "https"))
1589 free_current_action(action);
1591 free_http_request(url_to_query);
1592 return JB_ERR_MEMORY;
1596 matches = strdup("<table summary=\"\" class=\"transparent\">");
1598 for (i = 0; i < MAX_AF_FILES; i++)
1600 if (NULL == csp->config->actions_file_short[i]
1601 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1605 if ((fl = csp->actions_list[i]) != NULL)
1607 if ((b = fl->f) != NULL)
1609 /* FIXME: Hardcoded HTML! */
1610 string_append(&matches, "<tr><th>In file: ");
1611 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1612 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1613 string_append(&matches, buf);
1614 string_append(&matches, "View</a>");
1615 #ifdef FEATURE_CGI_EDIT_ACTIONS
1616 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1619 if (access(csp->config->actions_file[i], W_OK) == 0)
1621 #endif /* def HAVE_ACCESS */
1622 snprintf(buf, sizeof(buf),
1623 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1624 string_append(&matches, buf);
1625 string_append(&matches, "Edit</a>");
1630 string_append(&matches, " <strong>No write access.</strong>");
1632 #endif /* def HAVE_ACCESS */
1634 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1636 string_append(&matches, "</th></tr>\n");
1643 for (; (b != NULL) && (matches != NULL); b = b->next)
1645 if (url_match(b->url, url_to_query))
1647 string_append(&matches, "<tr><td>{");
1648 string_join (&matches, actions_to_html(csp, b->action));
1649 string_append(&matches, " }<br>\n<code>");
1650 string_join (&matches, html_encode(b->url->spec));
1651 string_append(&matches, "</code></td></tr>\n");
1653 if (merge_current_action(action, b->action))
1656 free_http_request(url_to_query);
1657 free_current_action(action);
1659 return JB_ERR_MEMORY;
1667 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1670 string_append(&matches, "</table>\n");
1673 * XXX: Kludge to make sure the "Forward settings" section
1674 * shows what forward-override{} would do with the requested URL.
1675 * No one really cares how the CGI request would be forwarded
1676 * if it wasn't intercepted as CGI request in the first place.
1678 * From here on the action bitmask will no longer reflect
1679 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1680 * but luckily it's no longer required later on anyway.
1682 free_current_action(csp->action);
1683 get_url_actions(csp, url_to_query);
1686 * Fill in forwarding settings.
1688 * The possibilities are:
1690 * - http forwarding only
1691 * - socks4(a) forwarding only
1692 * - socks4(a) and http forwarding.
1694 * XXX: Parts of this code could be reused for the
1695 * "forwarding-failed" template which currently doesn't
1696 * display the proxy port and an eventual second forwarder.
1699 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1701 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1703 if (!err) err = map_block_killer(exports, "socks-forwarder");
1704 if (!err) err = map_block_killer(exports, "http-forwarder");
1708 char port[10]; /* We save proxy ports as int but need a string here */
1710 if (!err) err = map_block_killer(exports, "no-forwarder");
1712 if (fwd->gateway_host != NULL)
1714 char *socks_type = NULL;
1719 socks_type = "socks4";
1722 socks_type = "socks4a";
1725 socks_type = "socks5";
1728 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1731 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1732 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1733 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1734 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1738 if (!err) err = map_block_killer(exports, "socks-forwarder");
1741 if (fwd->forward_host != NULL)
1743 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1744 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1745 if (!err) err = map(exports, "forward-port", 1, port, 1);
1749 if (!err) err = map_block_killer(exports, "http-forwarder");
1754 free_http_request(url_to_query);
1756 if (err || matches == NULL)
1758 free_current_action(action);
1760 return JB_ERR_MEMORY;
1763 #ifdef FEATURE_CGI_EDIT_ACTIONS
1764 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1766 err = map_block_killer(exports, "cgi-editor-is-disabled");
1768 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1771 * If zlib support is available, if no content filters
1772 * are enabled or if the prevent-compression action is enabled,
1773 * suppress the "compression could prevent filtering" warning.
1775 #ifndef FEATURE_ZLIB
1776 if (!content_filters_enabled(action) ||
1777 (action->flags & ACTION_NO_COMPRESSION))
1780 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1783 if (err || map(exports, "matches", 1, matches , 0))
1785 free_current_action(action);
1787 return JB_ERR_MEMORY;
1790 s = current_action_to_html(csp, action);
1792 free_current_action(action);
1794 if (map(exports, "final", 1, s, 0))
1797 return JB_ERR_MEMORY;
1801 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1805 /*********************************************************************
1807 * Function : cgi_robots_txt
1809 * Description : CGI function to return "/robots.txt".
1812 * 1 : csp = Current client state (buffers, headers, etc...)
1813 * 2 : rsp = http_response data structure for output
1814 * 3 : parameters = map of cgi parameters
1816 * CGI Parameters : None
1818 * Returns : JB_ERR_OK on success
1819 * JB_ERR_MEMORY on out-of-memory error.
1821 *********************************************************************/
1822 jb_err cgi_robots_txt(struct client_state *csp,
1823 struct http_response *rsp,
1824 const struct map *parameters)
1830 "# This is the Privoxy control interface.\n"
1831 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1837 if (rsp->body == NULL)
1839 return JB_ERR_MEMORY;
1842 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1846 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1847 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1849 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1853 /*********************************************************************
1855 * Function : show_defines
1857 * Description : Add to a map the state od all conditional #defines
1858 * used when building
1861 * 1 : exports = map to extend
1863 * Returns : JB_ERR_OK on success
1864 * JB_ERR_MEMORY on out-of-memory error.
1866 *********************************************************************/
1867 static jb_err show_defines(struct map *exports)
1869 jb_err err = JB_ERR_OK;
1872 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1873 #else /* ifndef FEATURE_ACL */
1874 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1875 #endif /* ndef FEATURE_ACL */
1877 #ifdef FEATURE_CGI_EDIT_ACTIONS
1878 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1879 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1880 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1881 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1883 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1884 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1885 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1886 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1887 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1889 #ifdef FEATURE_FAST_REDIRECTS
1890 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1891 #else /* ifndef FEATURE_FAST_REDIRECTS */
1892 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1893 #endif /* ndef FEATURE_FAST_REDIRECTS */
1895 #ifdef FEATURE_FORCE_LOAD
1896 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1897 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1898 #else /* ifndef FEATURE_FORCE_LOAD */
1899 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1900 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1901 #endif /* ndef FEATURE_FORCE_LOAD */
1903 #ifdef FEATURE_GRACEFUL_TERMINATION
1904 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1905 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1906 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1907 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1909 #ifdef FEATURE_IMAGE_BLOCKING
1910 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1911 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1912 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1913 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1915 #ifdef FEATURE_IMAGE_DETECT_MSIE
1916 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1917 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1918 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1919 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1921 #ifdef FEATURE_NO_GIFS
1922 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1923 #else /* ifndef FEATURE_NO_GIFS */
1924 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1925 #endif /* ndef FEATURE_NO_GIFS */
1927 #ifdef FEATURE_PTHREAD
1928 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1929 #else /* ifndef FEATURE_PTHREAD */
1930 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1931 #endif /* ndef FEATURE_PTHREAD */
1933 #ifdef FEATURE_STATISTICS
1934 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1935 #else /* ifndef FEATURE_STATISTICS */
1936 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1937 #endif /* ndef FEATURE_STATISTICS */
1939 #ifdef FEATURE_TOGGLE
1940 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1941 #else /* ifndef FEATURE_TOGGLE */
1942 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1943 #endif /* ndef FEATURE_TOGGLE */
1945 #ifdef FEATURE_TRUST
1946 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1947 #else /* ifndef FEATURE_TRUST */
1948 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1949 #endif /* ndef FEATURE_TRUST */
1952 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1953 #else /* ifndef FEATURE_ZLIB */
1954 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1955 #endif /* ndef FEATURE_ZLIB */
1958 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1959 #else /* ifndef STATIC_PCRE */
1960 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1961 #endif /* ndef STATIC_PCRE */
1964 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1965 #else /* ifndef STATIC_PCRS */
1966 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1967 #endif /* ndef STATIC_PCRS */
1973 /*********************************************************************
1975 * Function : show_rcs
1977 * Description : Create a string with the rcs info for all sourcefiles
1981 * Returns : A string, or NULL on out-of-memory.
1983 *********************************************************************/
1984 static char *show_rcs(void)
1986 char *result = strdup("");
1987 char buf[BUFFER_SIZE];
1989 /* Instead of including *all* dot h's in the project (thus creating a
1990 * tremendous amount of dependencies), I will concede to declaring them
1991 * as extern's. This forces the developer to add to this list, but oh well.
1994 #define SHOW_RCS(__x) \
1996 extern const char __x[]; \
1997 snprintf(buf, sizeof(buf), " %s\n", __x); \
1998 string_append(&result, buf); \
2001 /* In alphabetical order */
2002 SHOW_RCS(actions_h_rcs)
2003 SHOW_RCS(actions_rcs)
2005 SHOW_RCS(amiga_h_rcs)
2007 #endif /* def AMIGA */
2010 #ifdef FEATURE_CGI_EDIT_ACTIONS
2011 SHOW_RCS(cgiedit_h_rcs)
2012 SHOW_RCS(cgiedit_rcs)
2013 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
2014 SHOW_RCS(cgisimple_h_rcs)
2015 SHOW_RCS(cgisimple_rcs)
2017 SHOW_RCS(cygwin_h_rcs)
2019 SHOW_RCS(deanimate_h_rcs)
2020 SHOW_RCS(deanimate_rcs)
2021 SHOW_RCS(encode_h_rcs)
2022 SHOW_RCS(encode_rcs)
2023 SHOW_RCS(errlog_h_rcs)
2024 SHOW_RCS(errlog_rcs)
2025 SHOW_RCS(filters_h_rcs)
2026 SHOW_RCS(filters_rcs)
2027 SHOW_RCS(gateway_h_rcs)
2028 SHOW_RCS(gateway_rcs)
2029 SHOW_RCS(jbsockets_h_rcs)
2030 SHOW_RCS(jbsockets_rcs)
2033 SHOW_RCS(list_h_rcs)
2035 SHOW_RCS(loadcfg_h_rcs)
2036 SHOW_RCS(loadcfg_rcs)
2037 SHOW_RCS(loaders_h_rcs)
2038 SHOW_RCS(loaders_rcs)
2039 SHOW_RCS(miscutil_h_rcs)
2040 SHOW_RCS(miscutil_rcs)
2041 SHOW_RCS(parsers_h_rcs)
2042 SHOW_RCS(parsers_rcs)
2044 SHOW_RCS(pcrs_h_rcs)
2045 SHOW_RCS(project_h_rcs)
2046 SHOW_RCS(ssplit_h_rcs)
2047 SHOW_RCS(ssplit_rcs)
2048 SHOW_RCS(urlmatch_h_rcs)
2049 SHOW_RCS(urlmatch_rcs)
2051 #ifndef _WIN_CONSOLE
2052 SHOW_RCS(w32log_h_rcs)
2053 SHOW_RCS(w32log_rcs)
2054 SHOW_RCS(w32res_h_rcs)
2055 SHOW_RCS(w32taskbar_h_rcs)
2056 SHOW_RCS(w32taskbar_rcs)
2057 #endif /* ndef _WIN_CONSOLE */
2058 SHOW_RCS(win32_h_rcs)
2060 #endif /* def _WIN32 */
2069 /*********************************************************************
2071 * Function : cgi_show_file
2073 * Description : CGI function that shows the content of a
2074 * configuration file.
2077 * 1 : csp = Current client state (buffers, headers, etc...)
2078 * 2 : rsp = http_response data structure for output
2079 * 3 : parameters = map of cgi parameters
2082 * file : Which file to show. Only first letter is checked,
2087 * Default is to show menu and other information.
2089 * Returns : JB_ERR_OK on success
2090 * JB_ERR_MEMORY on out-of-memory error.
2092 *********************************************************************/
2093 static jb_err cgi_show_file(struct client_state *csp,
2094 struct http_response *rsp,
2095 const struct map *parameters)
2098 const char * filename = NULL;
2099 char * file_description = NULL;
2105 switch (*(lookup(parameters, "file")))
2108 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2110 filename = csp->actions_list[i]->filename;
2111 file_description = "Actions File";
2116 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2118 filename = csp->rlist[i]->filename;
2119 file_description = "Filter File";
2123 #ifdef FEATURE_TRUST
2127 filename = csp->tlist->filename;
2128 file_description = "Trust File";
2131 #endif /* def FEATURE_TRUST */
2134 if (NULL != filename)
2136 struct map *exports;
2141 exports = default_exports(csp, "show-status");
2142 if (NULL == exports)
2144 return JB_ERR_MEMORY;
2147 if ( map(exports, "file-description", 1, file_description, 1)
2148 || map(exports, "filepath", 1, html_encode(filename), 0) )
2151 return JB_ERR_MEMORY;
2154 err = load_file(filename, &s, &length);
2155 if (JB_ERR_OK != err)
2157 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2160 return JB_ERR_MEMORY;
2165 s = html_encode_and_free_original(s);
2168 return JB_ERR_MEMORY;
2171 if (map(exports, "contents", 1, s, 0))
2174 return JB_ERR_MEMORY;
2178 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2181 return JB_ERR_CGI_PARAMS;
2185 /*********************************************************************
2187 * Function : load_file
2189 * Description : Loads a file into a buffer.
2192 * 1 : filename = Name of the file to be loaded.
2193 * 2 : buffer = Used to return the file's content.
2194 * 3 : length = Used to return the size of the file.
2196 * Returns : JB_ERR_OK in case of success,
2197 * JB_ERR_FILE in case of ordinary file loading errors
2198 * (fseek() and ftell() errors are fatal)
2199 * JB_ERR_MEMORY in case of out-of-memory.
2201 *********************************************************************/
2202 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2206 jb_err err = JB_ERR_OK;
2208 fp = fopen(filename, "rb");
2214 /* Get file length */
2215 if (fseek(fp, 0, SEEK_END))
2217 log_error(LOG_LEVEL_FATAL,
2218 "Unexpected error while fseek()ing to the end of %s: %E",
2224 log_error(LOG_LEVEL_FATAL,
2225 "Unexpected ftell() error while loading %s: %E",
2228 *length = (size_t)ret;
2230 /* Go back to the beginning. */
2231 if (fseek(fp, 0, SEEK_SET))
2233 log_error(LOG_LEVEL_FATAL,
2234 "Unexpected error while fseek()ing to the beginning of %s: %E",
2238 *buffer = (char *)zalloc(*length + 1);
2239 if (NULL == *buffer)
2241 err = JB_ERR_MEMORY;
2243 else if (!fread(*buffer, *length, 1, fp))
2246 * May happen if the file size changes between fseek() and
2247 * fread(). If it does, we just log it and serve what we got.
2249 log_error(LOG_LEVEL_ERROR,
2250 "Couldn't completely read file %s.", filename);