1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.89 2008/10/11 11:31:14 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.89 2008/10/11 11:31:14 fabiankeil
40 * Add FEATURE_CONNECTION_KEEP_ALIVE to the list
41 * of conditional defines on the show-status page.
43 * Revision 1.88 2008/08/30 12:03:07 fabiankeil
44 * Remove FEATURE_COOKIE_JAR.
46 * Revision 1.87 2008/08/29 15:59:22 fabiankeil
49 * Revision 1.86 2008/06/28 14:19:05 fabiankeil
50 * Protocol detection is done case-insensitive, fix assertion
51 * to do the same. Yay for Privoxy-Regression-Test and zzuf.
53 * Revision 1.85 2008/05/26 17:30:55 fabiankeil
54 * Provide an OpenSearch Description to access the
55 * show-url-info page through "search engine plugins".
57 * Revision 1.84 2008/05/26 16:16:55 fabiankeil
58 * Spell error correctly.
60 * Revision 1.83 2008/05/12 14:51:30 fabiankeil
61 * Don't complain about an invalid URL if show-url-info is requested
62 * without parameters. Regression introduced in 1.81 by yours truly.
64 * Revision 1.82 2008/05/10 20:01:47 fabiankeil
65 * Fix an assertion that could erroneously
66 * trigger in case of memory shortage.
68 * Revision 1.81 2008/05/05 09:54:39 fabiankeil
69 * In cgi_show_url_info(), make sure ftp URLs are
70 * declared invalid. Also simplify the code that adds
71 * "http://" if no protocol has been specified.
73 * Revision 1.80 2008/05/04 16:18:32 fabiankeil
74 * Provide parse_http_url() with a third parameter to specify
75 * whether or not URLs without protocol are acceptable.
77 * Revision 1.79 2008/05/04 13:30:56 fabiankeil
78 * Streamline parse_http_url()'s prototype.
80 * Revision 1.78 2008/05/03 16:50:11 fabiankeil
81 * Leverage content_filters_enabled() in cgi_show_url_info().
83 * Revision 1.77 2008/05/02 09:47:48 fabiankeil
84 * In cgi_show_url_info, pass an initialized http structure
85 * to parse_http_url() as that will be required soonish and
86 * assert that https URLs are recognized correctly.
88 * Revision 1.76 2008/04/28 09:13:30 fabiankeil
89 * In load_file(), remember the error reason and fclose()
90 * and return later on instead of right away.
92 * Revision 1.75 2008/04/27 13:52:52 fabiankeil
93 * Move CGI file loading code into load_file() and
94 * add checks for unexpected errors.
96 * Revision 1.74 2008/04/26 15:50:56 fabiankeil
97 * Fix macro name in cgi_show_file() error path.
99 * Revision 1.73 2008/04/26 12:21:55 fabiankeil
100 * Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
102 * Revision 1.72 2008/04/26 10:34:15 fabiankeil
103 * If zlib support is unavailable and there are content filters active
104 * but the prevent-compression action is disabled, include a warning
105 * on the show-url-info page that compression might prevent filtering.
107 * Revision 1.71 2008/04/25 13:33:56 fabiankeil
108 * - Factor cgi_show_file() out of cgi_show_status().
109 * - Adjust cgi_show_status()'s parameter description to match reality.
111 * Revision 1.70 2008/04/24 16:12:38 fabiankeil
112 * In cgi_show_status(), load the requested file at once.
113 * Using string_join() for every line really doesn't scale.
115 * Revision 1.69 2008/04/17 14:40:48 fabiankeil
116 * Provide get_http_time() with the buffer size so it doesn't
117 * have to blindly assume that the buffer is big enough.
119 * Revision 1.68 2008/03/27 18:27:21 fabiankeil
120 * Remove kill-popups action.
122 * Revision 1.67 2008/03/27 17:00:05 fabiankeil
123 * Turn the favicon blobs into locals.
125 * Revision 1.66 2008/02/23 16:57:12 fabiankeil
126 * Rename url_actions() to get_url_actions() and let it
127 * use the standard parameter ordering.
129 * Revision 1.65 2008/02/23 16:33:43 fabiankeil
130 * Let forward_url() use the standard parameter ordering
131 * and mark its second parameter immutable.
133 * Revision 1.64 2008/02/03 13:56:07 fabiankeil
134 * Add SOCKS5 support for show-url-info CGI page.
136 * Revision 1.63 2008/02/01 06:04:31 fabiankeil
137 * If edit buttons on the show-url-info CGI page are hidden, explain why.
139 * Revision 1.62 2008/02/01 05:52:40 fabiankeil
140 * Hide edit buttons on the show-url-info CGI page if enable-edit-action
141 * is disabled. Patch by Lee with additional white space adjustments.
143 * Revision 1.61 2008/01/26 11:13:25 fabiankeil
144 * If enable-edit-actions is disabled, hide the edit buttons and explain why.
146 * Revision 1.60 2007/10/27 13:12:13 fabiankeil
147 * Finish 1.49 and check write access before
148 * showing edit buttons on show-url-info page.
150 * Revision 1.59 2007/10/19 16:42:36 fabiankeil
151 * Plug memory leak I introduced five months ago.
152 * Yay Valgrind and Privoxy-Regression-Test.
154 * Revision 1.58 2007/07/21 12:19:50 fabiankeil
155 * If show-url-info is called with an URL that Privoxy
156 * would reject as invalid, don't show unresolved forwarding
157 * variables, "final matches" or claim the site's secure.
159 * Revision 1.57 2007/06/01 16:53:05 fabiankeil
160 * Adjust cgi_show_url_info() to show what forward-override{}
161 * would do with the requested URL (instead of showing how the
162 * request for the CGI page would be forwarded if it wasn't a
165 * Revision 1.56 2007/05/21 10:50:35 fabiankeil
166 * - Use strlcpy() instead of strcpy().
167 * - Stop treating actions files special. Expect a complete file name
168 * (with or without path) like it's done for the rest of the files.
170 * - Don't rerun sed() in cgi_show_request().
172 * Revision 1.55 2007/04/13 13:36:46 fabiankeil
173 * Reference action files in CGI URLs by id instead
174 * of using the first part of the file name.
175 * Fixes BR 1694250 and BR 1590556.
177 * Revision 1.54 2007/04/09 18:11:35 fabiankeil
178 * Don't mistake VC++'s _snprintf() for a snprintf() replacement.
180 * Revision 1.53 2007/04/08 13:21:04 fabiankeil
181 * Reference action files in CGI URLs by id instead
182 * of using the first part of the file name.
183 * Fixes BR 1694250 and BR 1590556.
185 * Revision 1.52 2007/02/13 15:10:26 fabiankeil
186 * Apparently fopen()ing in "binary" mode doesn't require
187 * #ifdefs, it's already done without them in cgiedit.c.
189 * Revision 1.51 2007/02/10 16:55:22 fabiankeil
190 * - Show forwarding settings on the show-url-info page
191 * - Fix some HTML syntax errors.
193 * Revision 1.50 2007/01/23 15:51:17 fabiankeil
194 * Add favicon delivery functions.
196 * Revision 1.49 2007/01/20 16:29:38 fabiankeil
197 * Suppress edit buttons for action files if Privoxy has
198 * no write access. Suggested by Roland in PR 1564026.
200 * Revision 1.48 2007/01/20 15:31:31 fabiankeil
201 * Display warning if show-url-info CGI page
202 * is used while Privoxy is toggled off.
204 * Revision 1.47 2007/01/12 15:07:10 fabiankeil
205 * Use zalloc in cgi_send_user_manual.
207 * Revision 1.46 2007/01/02 12:49:46 fabiankeil
208 * Add FEATURE_ZLIB to the list of conditional
209 * defines at the show-status page.
211 * Revision 1.45 2006/12/28 18:16:41 fabiankeil
212 * Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
213 * body memory before using it, replaced sprintf calls with snprintf.
215 * Revision 1.44 2006/12/22 14:19:27 fabiankeil
216 * Removed checks whether or not AF_FILES have
217 * data structures associated with them in cgi_show_status.
218 * It doesn't matter as we're only interested in the file names.
220 * For the action files the checks were always true,
221 * but they prevented empty filter files from being
222 * listed. Fixes parts of BR 1619208.
224 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
225 * - Added FEATURE_GRACEFUL_TERMINATION to the
226 * "conditional #defines" section
227 * - Escaped ampersands in generated HTML.
228 * - Renamed re-filter-filename to re-filter-filenames
230 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
231 * Add special treatment for WIN32 to make sure
232 * cgi_send_user_manual opens the files in binary mode.
233 * Fixes BR 1600411 and unbreaks image delivery.
235 * Remove outdated comment.
237 * Revision 1.41 2006/10/09 19:18:28 roro
238 * Redirect http://p.p/user-manual (without trailing slash) to
239 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
241 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
242 * Modified cgi_send_user_manual to serve binary
243 * content without destroying it first. Should also be
244 * faster now. Added ".jpg" check for Content-Type guessing.
246 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
247 * Deliver documents in the user-manual directory
248 * with "Content-Type text/css" if their filename
251 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
252 * Incorporate modified version of Roland Rosenfeld's patch to
253 * optionally access the user-manual via Privoxy. Closes patch 679075.
255 * Formatting changed to Privoxy style, added call to
256 * cgi_error_no_template if the requested file doesn't
257 * exist and modified check whether or not Privoxy itself
258 * should serve the manual. Should work cross-platform now.
260 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
261 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
262 * with what was really the latest development (the v_3_0_branch branch)
264 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
265 * Multiple filter file support
267 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
268 * Undo some damaging memory leak patches
270 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
271 * A few memory leaks plugged (mostly on error paths)
273 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
274 * Another instance of:
275 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
276 * Thanks to Magnus Holmgren for the patch
278 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
279 * - Prevent line wrap beween "View/Edit" link buttons on status page
280 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
282 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
283 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
285 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
286 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
288 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
289 * Applying patch from Andreas as provided by Hal on the list.
290 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
292 * Revision 1.35 2002/05/12 21:44:44 jongfoster
293 * Adding amiga.[ch] revision information, if on an amiga.
295 * Revision 1.34 2002/04/30 12:06:12 oes
296 * Deleted unused code from default_cgi
298 * Revision 1.33 2002/04/30 11:14:52 oes
299 * Made csp the first parameter in *action_to_html
301 * Revision 1.32 2002/04/26 18:29:13 jongfoster
302 * Fixing this Visual C++ warning:
303 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
305 * Revision 1.31 2002/04/26 12:54:36 oes
306 * - Kill obsolete REDIRECT_URL code
307 * - Error handling fixes
308 * - Style sheet related HTML snipplet changes
309 * - cgi_show_url_info:
310 * - Matches now in table, actions on single lines,
312 * - standard.action suppressed
313 * - Buttons to View and Edit AFs
315 * Revision 1.30 2002/04/24 02:18:08 oes
316 * - show-status is now the starting point for editing
317 * the actions files, generate list of all AFs with buttons
318 * for viewing and editing, new look for file list (Jon:
319 * buttons now aligned ;-P ), view mode now supports multiple
320 * AFs, name changes, no view links for unspecified files,
321 * no edit link for standard.action.
323 * - Jon's multiple AF patch: cgi_show_url_info now uses all
324 * AFs and marks the output accordingly
326 * Revision 1.29 2002/04/10 13:38:35 oes
327 * load_template signature changed
329 * Revision 1.28 2002/04/07 15:42:12 jongfoster
330 * Fixing send-banner?type=auto when the image-blocker is
331 * a redirect to send-banner
333 * Revision 1.27 2002/04/05 15:50:48 oes
334 * added send-stylesheet CGI
336 * Revision 1.26 2002/04/04 00:36:36 gliptak
337 * always use pcre for matching
339 * Revision 1.25 2002/04/03 22:28:03 gliptak
340 * Removed references to gnu_regex
342 * Revision 1.24 2002/04/02 16:12:47 oes
343 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
345 * Revision 1.23 2002/03/26 22:29:54 swa
346 * we have a new homepage!
348 * Revision 1.22 2002/03/24 16:18:15 jongfoster
351 * Revision 1.21 2002/03/24 15:23:33 jongfoster
354 * Revision 1.20 2002/03/24 13:25:43 swa
355 * name change related issues
357 * Revision 1.19 2002/03/16 23:54:06 jongfoster
358 * Adding graceful termination feature, to help look for memory leaks.
359 * If you enable this (which, by design, has to be done by hand
360 * editing config.h) and then go to http://i.j.b/die, then the program
361 * will exit cleanly after the *next* request. It should free all the
362 * memory that was used.
364 * Revision 1.18 2002/03/12 01:44:49 oes
365 * Changed default for "blocked" image from jb logo to checkboard pattern
367 * Revision 1.17 2002/03/08 16:43:18 oes
368 * Added choice beween GIF and PNG built-in images
370 * Revision 1.16 2002/03/07 03:48:38 oes
371 * - Changed built-in images from GIF to PNG
372 * (with regard to Unisys patent issue)
373 * - Added a 4x4 pattern PNG which is less intrusive
374 * than the logo but also clearly marks the deleted banners
376 * Revision 1.15 2002/03/06 22:54:35 jongfoster
377 * Automated function-comment nitpicking.
379 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
380 * Clean up a little CRLF unpleasantness that suddenly appeared
382 * Revision 1.13 2002/02/21 00:10:37 jongfoster
383 * Adding send-banner?type=auto option
385 * Revision 1.12 2002/01/23 01:03:32 jongfoster
386 * Fixing gcc [CygWin] compiler warnings
388 * Revision 1.11 2002/01/23 00:01:04 jongfoster
389 * Adding cgi_transparent_gif() for http://i.j.b/t
390 * Adding missing html_encode() to many CGI functions.
391 * Adding urlmatch.[ch] to http://i.j.b/show-version
393 * Revision 1.10 2002/01/17 21:10:37 jongfoster
394 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
395 * Also fixing a problem in the same function with improperly quoted URLs
396 * in output HTML, and adding code to handle https:// URLs correctly.
398 * Revision 1.9 2001/11/30 23:09:15 jongfoster
399 * Now reports on FEATURE_CGI_EDIT_ACTIONS
400 * Removing FEATURE_DENY_GZIP from template
402 * Revision 1.8 2001/11/13 00:14:07 jongfoster
403 * Fixing stupid bug now I've figured out what || means.
404 * (It always returns 0 or 1, not one of it's paramaters.)
406 * Revision 1.7 2001/10/23 21:48:19 jongfoster
407 * Cleaning up error handling in CGI functions - they now send back
408 * a HTML error page and should never cause a FATAL error. (Fixes one
409 * potential source of "denial of service" attacks).
411 * CGI actions file editor that works and is actually useful.
413 * Ability to toggle JunkBuster remotely using a CGI call.
415 * You can turn off both the above features in the main configuration
416 * file, e.g. if you are running a multi-user proxy.
418 * Revision 1.6 2001/10/14 22:00:32 jongfoster
419 * Adding support for a 404 error when an invalid CGI page is requested.
421 * Revision 1.5 2001/10/07 15:30:41 oes
422 * Removed FEATURE_DENY_GZIP
424 * Revision 1.4 2001/10/02 15:31:12 oes
425 * Introduced show-request cgi
427 * Revision 1.3 2001/09/22 16:34:44 jongfoster
428 * Removing unneeded #includes
430 * Revision 1.2 2001/09/19 18:01:11 oes
431 * Fixed comments; cosmetics
433 * Revision 1.1 2001/09/16 17:08:54 jongfoster
434 * Moving simple CGI functions from cgi.c to new file cgisimple.c
437 **********************************************************************/
443 #include <sys/types.h>
451 #endif /* def HAVE_ACCESS */
455 #include "cgisimple.h"
461 #include "miscutil.h"
464 #include "urlmatch.h"
467 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
469 static char *show_rcs(void);
470 static jb_err show_defines(struct map *exports);
471 static jb_err cgi_show_file(struct client_state *csp,
472 struct http_response *rsp,
473 const struct map *parameters);
474 static jb_err load_file(const char *filename, char **buffer, size_t *length);
476 /*********************************************************************
478 * Function : cgi_default
480 * Description : CGI function that is called for the CGI_SITE_1_HOST
481 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
482 * Boring - only exports the default exports.
485 * 1 : csp = Current client state (buffers, headers, etc...)
486 * 2 : rsp = http_response data structure for output
487 * 3 : parameters = map of cgi parameters
489 * CGI Parameters : none
491 * Returns : JB_ERR_OK on success
492 * JB_ERR_MEMORY on out-of-memory
494 *********************************************************************/
495 jb_err cgi_default(struct client_state *csp,
496 struct http_response *rsp,
497 const struct map *parameters)
506 if (NULL == (exports = default_exports(csp, "")))
508 return JB_ERR_MEMORY;
511 return template_fill_for_cgi(csp, "default", exports, rsp);
515 /*********************************************************************
517 * Function : cgi_error_404
519 * Description : CGI function that is called if an unknown action was
523 * 1 : csp = Current client state (buffers, headers, etc...)
524 * 2 : rsp = http_response data structure for output
525 * 3 : parameters = map of cgi parameters
527 * CGI Parameters : none
529 * Returns : JB_ERR_OK on success
530 * JB_ERR_MEMORY on out-of-memory error.
532 *********************************************************************/
533 jb_err cgi_error_404(struct client_state *csp,
534 struct http_response *rsp,
535 const struct map *parameters)
543 if (NULL == (exports = default_exports(csp, NULL)))
545 return JB_ERR_MEMORY;
548 rsp->status = strdup("404 Privoxy configuration page not found");
549 if (rsp->status == NULL)
552 return JB_ERR_MEMORY;
555 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
559 #ifdef FEATURE_GRACEFUL_TERMINATION
560 /*********************************************************************
564 * Description : CGI function to shut down Privoxy.
565 * NOTE: Turning this on in a production build
566 * would be a BAD idea. An EXTREMELY BAD idea.
567 * In short, don't do it.
570 * 1 : csp = Current client state (buffers, headers, etc...)
571 * 2 : rsp = http_response data structure for output
572 * 3 : parameters = map of cgi parameters
574 * CGI Parameters : none
576 * Returns : JB_ERR_OK on success
577 * JB_ERR_MEMORY on out-of-memory error.
579 *********************************************************************/
580 jb_err cgi_die (struct client_state *csp,
581 struct http_response *rsp,
582 const struct map *parameters)
592 * I don't really care what gets sent back to the browser.
593 * Take the easy option - "out of memory" page.
596 return JB_ERR_MEMORY;
598 #endif /* def FEATURE_GRACEFUL_TERMINATION */
601 /*********************************************************************
603 * Function : cgi_show_request
605 * Description : Show the client's request and what sed() would have
609 * 1 : csp = Current client state (buffers, headers, etc...)
610 * 2 : rsp = http_response data structure for output
611 * 3 : parameters = map of cgi parameters
613 * CGI Parameters : none
615 * Returns : JB_ERR_OK on success
616 * JB_ERR_MEMORY on out-of-memory error.
618 *********************************************************************/
619 jb_err cgi_show_request(struct client_state *csp,
620 struct http_response *rsp,
621 const struct map *parameters)
630 if (NULL == (exports = default_exports(csp, "show-request")))
632 return JB_ERR_MEMORY;
636 * Repair the damage done to the IOB by get_header()
638 for (p = csp->iob->buf; p < csp->iob->eod; p++)
640 if (*p == '\0') *p = '\n';
644 * Export the original client's request and the one we would
645 * be sending to the server if this wasn't a CGI call
648 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
651 return JB_ERR_MEMORY;
654 if (map(exports, "processed-request", 1,
655 html_encode_and_free_original(list_to_text(csp->headers)), 0))
658 return JB_ERR_MEMORY;
661 return template_fill_for_cgi(csp, "show-request", exports, rsp);
665 /*********************************************************************
667 * Function : cgi_send_banner
669 * Description : CGI function that returns a banner.
672 * 1 : csp = Current client state (buffers, headers, etc...)
673 * 2 : rsp = http_response data structure for output
674 * 3 : parameters = map of cgi parameters
677 * type : Selects the type of banner between "trans", "logo",
678 * and "auto". Defaults to "logo" if absent or invalid.
679 * "auto" means to select as if we were image-blocking.
680 * (Only the first character really counts; b and t are
683 * Returns : JB_ERR_OK on success
684 * JB_ERR_MEMORY on out-of-memory error.
686 *********************************************************************/
687 jb_err cgi_send_banner(struct client_state *csp,
688 struct http_response *rsp,
689 const struct map *parameters)
691 char imagetype = lookup(parameters, "type")[0];
694 * If type is auto, then determine the right thing
695 * to do from the set-image-blocker action
697 if (imagetype == 'a')
704 #ifdef FEATURE_IMAGE_BLOCKING
705 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
707 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
708 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
709 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
713 /* Use default - nothing to do here. */
715 else if (0 == strcmpic(p, "blank"))
719 else if (0 == strcmpic(p, "pattern"))
725 * If the action is to call this CGI, determine
728 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
730 imagetype = p[sizeof(prefix1) - 1];
732 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
734 imagetype = p[sizeof(prefix2) - 1];
738 * Everything else must (should) be a URL to
746 #endif /* def FEATURE_IMAGE_BLOCKING */
750 * Now imagetype is either the non-auto type we were called with,
751 * or it was auto and has since been determined. In any case, we
752 * can proceed to actually answering the request by sending a redirect
753 * or an image as appropriate:
755 if (imagetype == 'r')
757 rsp->status = strdup("302 Local Redirect from Privoxy");
758 if (rsp->status == NULL)
760 return JB_ERR_MEMORY;
762 if (enlist_unique_header(rsp->headers, "Location",
763 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
765 return JB_ERR_MEMORY;
770 if ((imagetype == 'b') || (imagetype == 't'))
772 rsp->body = bindup(image_blank_data, image_blank_length);
773 rsp->content_length = image_blank_length;
777 rsp->body = bindup(image_pattern_data, image_pattern_length);
778 rsp->content_length = image_pattern_length;
781 if (rsp->body == NULL)
783 return JB_ERR_MEMORY;
785 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
787 return JB_ERR_MEMORY;
798 /*********************************************************************
800 * Function : cgi_transparent_image
802 * Description : CGI function that sends a 1x1 transparent image.
805 * 1 : csp = Current client state (buffers, headers, etc...)
806 * 2 : rsp = http_response data structure for output
807 * 3 : parameters = map of cgi parameters
809 * CGI Parameters : None
811 * Returns : JB_ERR_OK on success
812 * JB_ERR_MEMORY on out-of-memory error.
814 *********************************************************************/
815 jb_err cgi_transparent_image(struct client_state *csp,
816 struct http_response *rsp,
817 const struct map *parameters)
822 rsp->body = bindup(image_blank_data, image_blank_length);
823 rsp->content_length = image_blank_length;
825 if (rsp->body == NULL)
827 return JB_ERR_MEMORY;
830 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
832 return JB_ERR_MEMORY;
842 /*********************************************************************
844 * Function : cgi_send_default_favicon
846 * Description : CGI function that sends the standard favicon.
849 * 1 : csp = Current client state (buffers, headers, etc...)
850 * 2 : rsp = http_response data structure for output
851 * 3 : parameters = map of cgi parameters
853 * CGI Parameters : None
855 * Returns : JB_ERR_OK on success
856 * JB_ERR_MEMORY on out-of-memory error.
858 *********************************************************************/
859 jb_err cgi_send_default_favicon(struct client_state *csp,
860 struct http_response *rsp,
861 const struct map *parameters)
863 static const char default_favicon_data[] =
864 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
865 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
866 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
867 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
868 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
869 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
870 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
871 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
872 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
873 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
874 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
875 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
876 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
878 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
883 rsp->body = bindup(default_favicon_data, favicon_length);
884 rsp->content_length = favicon_length;
886 if (rsp->body == NULL)
888 return JB_ERR_MEMORY;
891 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
893 return JB_ERR_MEMORY;
903 /*********************************************************************
905 * Function : cgi_send_error_favicon
907 * Description : CGI function that sends the favicon for error pages.
910 * 1 : csp = Current client state (buffers, headers, etc...)
911 * 2 : rsp = http_response data structure for output
912 * 3 : parameters = map of cgi parameters
914 * CGI Parameters : None
916 * Returns : JB_ERR_OK on success
917 * JB_ERR_MEMORY on out-of-memory error.
919 *********************************************************************/
920 jb_err cgi_send_error_favicon(struct client_state *csp,
921 struct http_response *rsp,
922 const struct map *parameters)
924 static const char error_favicon_data[] =
925 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
926 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
927 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
928 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
929 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
930 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
931 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
932 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
933 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
934 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
935 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
936 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
937 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
939 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
944 rsp->body = bindup(error_favicon_data, favicon_length);
945 rsp->content_length = favicon_length;
947 if (rsp->body == NULL)
949 return JB_ERR_MEMORY;
952 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
954 return JB_ERR_MEMORY;
964 /*********************************************************************
966 * Function : cgi_send_stylesheet
968 * Description : CGI function that sends a css stylesheet found
969 * in the cgi-style.css template
972 * 1 : csp = Current client state (buffers, headers, etc...)
973 * 2 : rsp = http_response data structure for output
974 * 3 : parameters = map of cgi parameters
976 * CGI Parameters : None
978 * Returns : JB_ERR_OK on success
979 * JB_ERR_MEMORY on out-of-memory error.
981 *********************************************************************/
982 jb_err cgi_send_stylesheet(struct client_state *csp,
983 struct http_response *rsp,
984 const struct map *parameters)
993 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
995 if (err == JB_ERR_FILE)
998 * No way to tell user; send empty stylesheet
1000 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
1004 return err; /* JB_ERR_MEMORY */
1007 if (enlist(rsp->headers, "Content-Type: text/css"))
1009 return JB_ERR_MEMORY;
1017 /*********************************************************************
1019 * Function : cgi_send_url_info_osd
1021 * Description : CGI function that sends the OpenSearch Description
1022 * template for the show-url-info page. It allows to
1023 * access the page through "search engine plugins".
1026 * 1 : csp = Current client state (buffers, headers, etc...)
1027 * 2 : rsp = http_response data structure for output
1028 * 3 : parameters = map of cgi parameters
1030 * CGI Parameters : None
1032 * Returns : JB_ERR_OK on success
1033 * JB_ERR_MEMORY on out-of-memory error.
1035 *********************************************************************/
1036 jb_err cgi_send_url_info_osd(struct client_state *csp,
1037 struct http_response *rsp,
1038 const struct map *parameters)
1040 jb_err err = JB_ERR_MEMORY;
1041 struct map *exports = default_exports(csp, NULL);
1046 if (NULL != exports)
1048 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1049 if (JB_ERR_OK == err)
1051 err = enlist(rsp->headers,
1052 "Content-Type: application/opensearchdescription+xml");
1061 /*********************************************************************
1063 * Function : cgi_send_user_manual
1065 * Description : CGI function that sends a file in the user
1069 * 1 : csp = Current client state (buffers, headers, etc...)
1070 * 2 : rsp = http_response data structure for output
1071 * 3 : parameters = map of cgi parameters
1073 * CGI Parameters : file=name.html, the name of the HTML file
1074 * (relative to user-manual from config)
1076 * Returns : JB_ERR_OK on success
1077 * JB_ERR_MEMORY on out-of-memory error.
1079 *********************************************************************/
1080 jb_err cgi_send_user_manual(struct client_state *csp,
1081 struct http_response *rsp,
1082 const struct map *parameters)
1084 const char * filename;
1086 jb_err err = JB_ERR_OK;
1093 if (!parameters->first)
1095 /* requested http://p.p/user-manual (without trailing slash) */
1096 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1099 get_string_param(parameters, "file", &filename);
1100 /* Check paramter for hack attempts */
1101 if (filename && strchr(filename, '/'))
1103 return JB_ERR_CGI_PARAMS;
1105 if (filename && strstr(filename, ".."))
1107 return JB_ERR_CGI_PARAMS;
1110 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1111 if (full_path == NULL)
1113 return JB_ERR_MEMORY;
1116 err = load_file(full_path, &rsp->body, &rsp->content_length);
1117 if (JB_ERR_OK != err)
1119 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1120 if (JB_ERR_FILE == err)
1122 err = cgi_error_no_template(csp, rsp, full_path);
1129 /* Guess correct Content-Type based on the filename's ending */
1132 length = strlen(filename);
1138 if((length>=4) && !strcmp(&filename[length-4], ".css"))
1140 err = enlist(rsp->headers, "Content-Type: text/css");
1142 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1144 err = enlist(rsp->headers, "Content-Type: image/jpeg");
1148 err = enlist(rsp->headers, "Content-Type: text/html");
1155 /*********************************************************************
1157 * Function : cgi_show_version
1159 * Description : CGI function that returns a a web page describing the
1160 * file versions of Privoxy.
1163 * 1 : csp = Current client state (buffers, headers, etc...)
1164 * 2 : rsp = http_response data structure for output
1165 * 3 : parameters = map of cgi parameters
1167 * CGI Parameters : none
1169 * Returns : JB_ERR_OK on success
1170 * JB_ERR_MEMORY on out-of-memory error.
1172 *********************************************************************/
1173 jb_err cgi_show_version(struct client_state *csp,
1174 struct http_response *rsp,
1175 const struct map *parameters)
1177 struct map *exports;
1183 if (NULL == (exports = default_exports(csp, "show-version")))
1185 return JB_ERR_MEMORY;
1188 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1191 return JB_ERR_MEMORY;
1194 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1198 /*********************************************************************
1200 * Function : cgi_show_status
1202 * Description : CGI function that returns a web page describing the
1203 * current status of Privoxy.
1206 * 1 : csp = Current client state (buffers, headers, etc...)
1207 * 2 : rsp = http_response data structure for output
1208 * 3 : parameters = map of cgi parameters
1211 * file : Which file to show. Only first letter is checked,
1216 * Default is to show menu and other information.
1218 * Returns : JB_ERR_OK on success
1219 * JB_ERR_MEMORY on out-of-memory error.
1221 *********************************************************************/
1222 jb_err cgi_show_status(struct client_state *csp,
1223 struct http_response *rsp,
1224 const struct map *parameters)
1230 char buf[BUFFER_SIZE];
1231 #ifdef FEATURE_STATISTICS
1232 float perc_rej; /* Percentage of http requests rejected */
1233 int local_urls_read;
1234 int local_urls_rejected;
1235 #endif /* ndef FEATURE_STATISTICS */
1236 jb_err err = JB_ERR_OK;
1238 struct map *exports;
1244 if ('\0' != *(lookup(parameters, "file")))
1246 return cgi_show_file(csp, rsp, parameters);
1249 if (NULL == (exports = default_exports(csp, "show-status")))
1251 return JB_ERR_MEMORY;
1255 for (j = 0; (s != NULL) && (j < Argc); j++)
1257 if (!err) err = string_join (&s, html_encode(Argv[j]));
1258 if (!err) err = string_append(&s, " ");
1260 if (!err) err = map(exports, "invocation", 1, s, 0);
1262 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1263 if (!err) err = show_defines(exports);
1268 return JB_ERR_MEMORY;
1271 #ifdef FEATURE_STATISTICS
1272 local_urls_read = urls_read;
1273 local_urls_rejected = urls_rejected;
1276 * Need to alter the stats not to include the fetch of this
1279 * Can't do following thread safely! doh!
1282 * urls_rejected--; * This will be incremented subsequently *
1285 if (local_urls_read == 0)
1287 if (!err) err = map_block_killer(exports, "have-stats");
1291 if (!err) err = map_block_killer(exports, "have-no-stats");
1293 perc_rej = (float)local_urls_rejected * 100.0F /
1294 (float)local_urls_read;
1296 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1297 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1299 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1300 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1302 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1303 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1306 #else /* ndef FEATURE_STATISTICS */
1307 err = err || map_block_killer(exports, "statistics");
1308 #endif /* ndef FEATURE_STATISTICS */
1311 * List all action files in use, together with view and edit links,
1312 * except for standard.action, which should only be viewable. (Not
1313 * enforced in the editor itself)
1314 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1317 for (i = 0; i < MAX_AF_FILES; i++)
1319 if (csp->actions_list[i] != NULL)
1321 if (!err) err = string_append(&s, "<tr><td>");
1322 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1323 snprintf(buf, sizeof(buf),
1324 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1325 if (!err) err = string_append(&s, buf);
1327 #ifdef FEATURE_CGI_EDIT_ACTIONS
1328 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1329 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1330 && (NULL != csp->config->actions_file_short[i]))
1333 if (access(csp->config->actions_file[i], W_OK) == 0)
1335 #endif /* def HAVE_ACCESS */
1336 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1337 if (!err) err = string_append(&s, buf);
1342 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1344 #endif /* def HAVE_ACCESS */
1348 if (!err) err = string_append(&s, "</td></tr>\n");
1353 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1357 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1361 * List all re_filterfiles in use, together with view options.
1362 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1365 for (i = 0; i < MAX_AF_FILES; i++)
1367 if (csp->rlist[i] != NULL)
1369 if (!err) err = string_append(&s, "<tr><td>");
1370 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1371 snprintf(buf, sizeof(buf),
1372 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1373 if (!err) err = string_append(&s, buf);
1374 if (!err) err = string_append(&s, "</td></tr>\n");
1379 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1383 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1384 if (!err) err = map_block_killer(exports, "have-filterfile");
1387 #ifdef FEATURE_TRUST
1390 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1394 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1395 if (!err) err = map_block_killer(exports, "have-trustfile");
1398 if (!err) err = map_block_killer(exports, "trust-support");
1399 #endif /* ndef FEATURE_TRUST */
1401 #ifdef FEATURE_CGI_EDIT_ACTIONS
1402 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1404 err = map_block_killer(exports, "cgi-editor-is-disabled");
1406 #endif /* ndef CGI_EDIT_ACTIONS */
1411 return JB_ERR_MEMORY;
1414 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1418 /*********************************************************************
1420 * Function : cgi_show_url_info
1422 * Description : CGI function that determines and shows which actions
1423 * Privoxy will perform for a given url, and which
1424 * matches starting from the defaults have lead to that.
1427 * 1 : csp = Current client state (buffers, headers, etc...)
1428 * 2 : rsp = http_response data structure for output
1429 * 3 : parameters = map of cgi parameters
1432 * url : The url whose actions are to be determined.
1433 * If url is unset, the url-given conditional will be
1434 * set, so that all but the form can be suppressed in
1437 * Returns : JB_ERR_OK on success
1438 * JB_ERR_MEMORY on out-of-memory error.
1440 *********************************************************************/
1441 jb_err cgi_show_url_info(struct client_state *csp,
1442 struct http_response *rsp,
1443 const struct map *parameters)
1446 struct map *exports;
1453 if (NULL == (exports = default_exports(csp, "show-url-info")))
1455 return JB_ERR_MEMORY;
1459 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1461 url_param = strdup(lookup(parameters, "url"));
1462 if (url_param == NULL)
1465 return JB_ERR_MEMORY;
1470 * Handle prefixes. 4 possibilities:
1471 * 1) "http://" or "https://" prefix present and followed by URL - OK
1472 * 2) Only the "http://" or "https://" part is present, no URL - change
1473 * to empty string so it will be detected later as "no URL".
1474 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1476 * 4) Parameter not specified or is empty string - let this fall through
1477 * for now, next block of code will handle it.
1479 if (0 == strncmp(url_param, "http://", 7))
1481 if (url_param[7] == '\0')
1484 * Empty URL (just prefix).
1485 * Make it totally empty so it's caught by the next if()
1487 url_param[0] = '\0';
1490 else if (0 == strncmp(url_param, "https://", 8))
1492 if (url_param[8] == '\0')
1495 * Empty URL (just prefix).
1496 * Make it totally empty so it's caught by the next if()
1498 url_param[0] = '\0';
1501 else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1503 /* No prefix - assume http:// */
1504 char *url_param_prefixed = strdup("http://");
1506 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1509 return JB_ERR_MEMORY;
1511 url_param = url_param_prefixed;
1515 * Hide "toggle off" warning if Privoxy is toggled on.
1518 #ifdef FEATURE_TOGGLE
1519 (global_toggle_state == 1) &&
1520 #endif /* def FEATURE_TOGGLE */
1521 map_block_killer(exports, "privoxy-is-toggled-off")
1525 return JB_ERR_MEMORY;
1528 if (url_param[0] == '\0')
1530 /* URL paramater not specified, display query form only. */
1532 if (map_block_killer(exports, "url-given")
1533 || map(exports, "url", 1, "", 1))
1536 return JB_ERR_MEMORY;
1541 /* Given a URL, so query it. */
1546 struct file_list *fl;
1547 struct url_actions *b;
1548 struct http_request url_to_query[1];
1549 struct current_action_spec action[1];
1552 if (map(exports, "url", 1, html_encode(url_param), 0))
1556 return JB_ERR_MEMORY;
1559 init_current_action(action);
1561 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1563 free_current_action(action);
1566 return JB_ERR_MEMORY;
1569 memset(url_to_query, '\0', sizeof(url_to_query));
1570 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1571 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1575 if (err == JB_ERR_MEMORY)
1577 free_http_request(url_to_query);
1578 free_current_action(action);
1580 return JB_ERR_MEMORY;
1586 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1587 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1588 if (!err) err = map_block_killer(exports, "valid-url");
1590 free_current_action(action);
1591 free_http_request(url_to_query);
1596 return JB_ERR_MEMORY;
1599 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1603 * We have a warning about SSL paths. Hide it for unencrypted sites.
1605 if (!url_to_query->ssl)
1607 if (map_block_killer(exports, "https"))
1609 free_current_action(action);
1611 free_http_request(url_to_query);
1612 return JB_ERR_MEMORY;
1616 matches = strdup("<table summary=\"\" class=\"transparent\">");
1618 for (i = 0; i < MAX_AF_FILES; i++)
1620 if (NULL == csp->config->actions_file_short[i]
1621 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1625 if ((fl = csp->actions_list[i]) != NULL)
1627 if ((b = fl->f) != NULL)
1629 /* FIXME: Hardcoded HTML! */
1630 string_append(&matches, "<tr><th>In file: ");
1631 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1632 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1633 string_append(&matches, buf);
1634 string_append(&matches, "View</a>");
1635 #ifdef FEATURE_CGI_EDIT_ACTIONS
1636 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1639 if (access(csp->config->actions_file[i], W_OK) == 0)
1641 #endif /* def HAVE_ACCESS */
1642 snprintf(buf, sizeof(buf),
1643 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1644 string_append(&matches, buf);
1645 string_append(&matches, "Edit</a>");
1650 string_append(&matches, " <strong>No write access.</strong>");
1652 #endif /* def HAVE_ACCESS */
1654 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1656 string_append(&matches, "</th></tr>\n");
1663 for (; (b != NULL) && (matches != NULL); b = b->next)
1665 if (url_match(b->url, url_to_query))
1667 string_append(&matches, "<tr><td>{");
1668 string_join (&matches, actions_to_html(csp, b->action));
1669 string_append(&matches, " }<br>\n<code>");
1670 string_join (&matches, html_encode(b->url->spec));
1671 string_append(&matches, "</code></td></tr>\n");
1673 if (merge_current_action(action, b->action))
1676 free_http_request(url_to_query);
1677 free_current_action(action);
1679 return JB_ERR_MEMORY;
1687 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1690 string_append(&matches, "</table>\n");
1693 * XXX: Kludge to make sure the "Forward settings" section
1694 * shows what forward-override{} would do with the requested URL.
1695 * No one really cares how the CGI request would be forwarded
1696 * if it wasn't intercepted as CGI request in the first place.
1698 * From here on the action bitmask will no longer reflect
1699 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1700 * but luckily it's no longer required later on anyway.
1702 free_current_action(csp->action);
1703 get_url_actions(csp, url_to_query);
1706 * Fill in forwarding settings.
1708 * The possibilities are:
1710 * - http forwarding only
1711 * - socks4(a) forwarding only
1712 * - socks4(a) and http forwarding.
1714 * XXX: Parts of this code could be reused for the
1715 * "forwarding-failed" template which currently doesn't
1716 * display the proxy port and an eventual second forwarder.
1719 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1721 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1723 if (!err) err = map_block_killer(exports, "socks-forwarder");
1724 if (!err) err = map_block_killer(exports, "http-forwarder");
1728 char port[10]; /* We save proxy ports as int but need a string here */
1730 if (!err) err = map_block_killer(exports, "no-forwarder");
1732 if (fwd->gateway_host != NULL)
1734 char *socks_type = NULL;
1739 socks_type = "socks4";
1742 socks_type = "socks4a";
1745 socks_type = "socks5";
1748 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1751 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1752 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1753 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1754 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1758 if (!err) err = map_block_killer(exports, "socks-forwarder");
1761 if (fwd->forward_host != NULL)
1763 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1764 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1765 if (!err) err = map(exports, "forward-port", 1, port, 1);
1769 if (!err) err = map_block_killer(exports, "http-forwarder");
1774 free_http_request(url_to_query);
1776 if (err || matches == NULL)
1778 free_current_action(action);
1780 return JB_ERR_MEMORY;
1783 #ifdef FEATURE_CGI_EDIT_ACTIONS
1784 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1786 err = map_block_killer(exports, "cgi-editor-is-disabled");
1788 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1791 * If zlib support is available, if no content filters
1792 * are enabled or if the prevent-compression action is enabled,
1793 * suppress the "compression could prevent filtering" warning.
1795 #ifndef FEATURE_ZLIB
1796 if (!content_filters_enabled(action) ||
1797 (action->flags & ACTION_NO_COMPRESSION))
1800 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1803 if (err || map(exports, "matches", 1, matches , 0))
1805 free_current_action(action);
1807 return JB_ERR_MEMORY;
1810 s = current_action_to_html(csp, action);
1812 free_current_action(action);
1814 if (map(exports, "final", 1, s, 0))
1817 return JB_ERR_MEMORY;
1821 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1825 /*********************************************************************
1827 * Function : cgi_robots_txt
1829 * Description : CGI function to return "/robots.txt".
1832 * 1 : csp = Current client state (buffers, headers, etc...)
1833 * 2 : rsp = http_response data structure for output
1834 * 3 : parameters = map of cgi parameters
1836 * CGI Parameters : None
1838 * Returns : JB_ERR_OK on success
1839 * JB_ERR_MEMORY on out-of-memory error.
1841 *********************************************************************/
1842 jb_err cgi_robots_txt(struct client_state *csp,
1843 struct http_response *rsp,
1844 const struct map *parameters)
1853 "# This is the Privoxy control interface.\n"
1854 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1860 if (rsp->body == NULL)
1862 return JB_ERR_MEMORY;
1865 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1869 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1870 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1872 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1876 /*********************************************************************
1878 * Function : show_defines
1880 * Description : Add to a map the state od all conditional #defines
1881 * used when building
1884 * 1 : exports = map to extend
1886 * Returns : JB_ERR_OK on success
1887 * JB_ERR_MEMORY on out-of-memory error.
1889 *********************************************************************/
1890 static jb_err show_defines(struct map *exports)
1892 jb_err err = JB_ERR_OK;
1895 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1896 #else /* ifndef FEATURE_ACL */
1897 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1898 #endif /* ndef FEATURE_ACL */
1900 #ifdef FEATURE_CGI_EDIT_ACTIONS
1901 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1902 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1903 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1904 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1906 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1907 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1908 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1909 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1910 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1912 #ifdef FEATURE_FAST_REDIRECTS
1913 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1914 #else /* ifndef FEATURE_FAST_REDIRECTS */
1915 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1916 #endif /* ndef FEATURE_FAST_REDIRECTS */
1918 #ifdef FEATURE_FORCE_LOAD
1919 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1920 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1921 #else /* ifndef FEATURE_FORCE_LOAD */
1922 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1923 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1924 #endif /* ndef FEATURE_FORCE_LOAD */
1926 #ifdef FEATURE_GRACEFUL_TERMINATION
1927 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1928 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1929 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1930 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1932 #ifdef FEATURE_IMAGE_BLOCKING
1933 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1934 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1935 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1936 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1938 #ifdef FEATURE_IMAGE_DETECT_MSIE
1939 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1940 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1941 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1942 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1944 #ifdef FEATURE_NO_GIFS
1945 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1946 #else /* ifndef FEATURE_NO_GIFS */
1947 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1948 #endif /* ndef FEATURE_NO_GIFS */
1950 #ifdef FEATURE_PTHREAD
1951 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1952 #else /* ifndef FEATURE_PTHREAD */
1953 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1954 #endif /* ndef FEATURE_PTHREAD */
1956 #ifdef FEATURE_STATISTICS
1957 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1958 #else /* ifndef FEATURE_STATISTICS */
1959 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1960 #endif /* ndef FEATURE_STATISTICS */
1962 #ifdef FEATURE_TOGGLE
1963 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1964 #else /* ifndef FEATURE_TOGGLE */
1965 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1966 #endif /* ndef FEATURE_TOGGLE */
1968 #ifdef FEATURE_TRUST
1969 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1970 #else /* ifndef FEATURE_TRUST */
1971 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1972 #endif /* ndef FEATURE_TRUST */
1975 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1976 #else /* ifndef FEATURE_ZLIB */
1977 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1978 #endif /* ndef FEATURE_ZLIB */
1981 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1982 #else /* ifndef STATIC_PCRE */
1983 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1984 #endif /* ndef STATIC_PCRE */
1987 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1988 #else /* ifndef STATIC_PCRS */
1989 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1990 #endif /* ndef STATIC_PCRS */
1996 /*********************************************************************
1998 * Function : show_rcs
2000 * Description : Create a string with the rcs info for all sourcefiles
2004 * Returns : A string, or NULL on out-of-memory.
2006 *********************************************************************/
2007 static char *show_rcs(void)
2009 char *result = strdup("");
2010 char buf[BUFFER_SIZE];
2012 /* Instead of including *all* dot h's in the project (thus creating a
2013 * tremendous amount of dependencies), I will concede to declaring them
2014 * as extern's. This forces the developer to add to this list, but oh well.
2017 #define SHOW_RCS(__x) \
2019 extern const char __x[]; \
2020 snprintf(buf, sizeof(buf), " %s\n", __x); \
2021 string_append(&result, buf); \
2024 /* In alphabetical order */
2025 SHOW_RCS(actions_h_rcs)
2026 SHOW_RCS(actions_rcs)
2028 SHOW_RCS(amiga_h_rcs)
2030 #endif /* def AMIGA */
2033 #ifdef FEATURE_CGI_EDIT_ACTIONS
2034 SHOW_RCS(cgiedit_h_rcs)
2035 SHOW_RCS(cgiedit_rcs)
2036 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
2037 SHOW_RCS(cgisimple_h_rcs)
2038 SHOW_RCS(cgisimple_rcs)
2040 SHOW_RCS(cygwin_h_rcs)
2042 SHOW_RCS(deanimate_h_rcs)
2043 SHOW_RCS(deanimate_rcs)
2044 SHOW_RCS(encode_h_rcs)
2045 SHOW_RCS(encode_rcs)
2046 SHOW_RCS(errlog_h_rcs)
2047 SHOW_RCS(errlog_rcs)
2048 SHOW_RCS(filters_h_rcs)
2049 SHOW_RCS(filters_rcs)
2050 SHOW_RCS(gateway_h_rcs)
2051 SHOW_RCS(gateway_rcs)
2052 SHOW_RCS(jbsockets_h_rcs)
2053 SHOW_RCS(jbsockets_rcs)
2056 SHOW_RCS(list_h_rcs)
2058 SHOW_RCS(loadcfg_h_rcs)
2059 SHOW_RCS(loadcfg_rcs)
2060 SHOW_RCS(loaders_h_rcs)
2061 SHOW_RCS(loaders_rcs)
2062 SHOW_RCS(miscutil_h_rcs)
2063 SHOW_RCS(miscutil_rcs)
2064 SHOW_RCS(parsers_h_rcs)
2065 SHOW_RCS(parsers_rcs)
2067 SHOW_RCS(pcrs_h_rcs)
2068 SHOW_RCS(project_h_rcs)
2069 SHOW_RCS(ssplit_h_rcs)
2070 SHOW_RCS(ssplit_rcs)
2071 SHOW_RCS(urlmatch_h_rcs)
2072 SHOW_RCS(urlmatch_rcs)
2074 #ifndef _WIN_CONSOLE
2075 SHOW_RCS(w32log_h_rcs)
2076 SHOW_RCS(w32log_rcs)
2077 SHOW_RCS(w32res_h_rcs)
2078 SHOW_RCS(w32taskbar_h_rcs)
2079 SHOW_RCS(w32taskbar_rcs)
2080 #endif /* ndef _WIN_CONSOLE */
2081 SHOW_RCS(win32_h_rcs)
2083 #endif /* def _WIN32 */
2092 /*********************************************************************
2094 * Function : cgi_show_file
2096 * Description : CGI function that shows the content of a
2097 * configuration file.
2100 * 1 : csp = Current client state (buffers, headers, etc...)
2101 * 2 : rsp = http_response data structure for output
2102 * 3 : parameters = map of cgi parameters
2105 * file : Which file to show. Only first letter is checked,
2110 * Default is to show menu and other information.
2112 * Returns : JB_ERR_OK on success
2113 * JB_ERR_MEMORY on out-of-memory error.
2115 *********************************************************************/
2116 static jb_err cgi_show_file(struct client_state *csp,
2117 struct http_response *rsp,
2118 const struct map *parameters)
2121 const char * filename = NULL;
2122 char * file_description = NULL;
2128 switch (*(lookup(parameters, "file")))
2131 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2133 filename = csp->actions_list[i]->filename;
2134 file_description = "Actions File";
2139 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2141 filename = csp->rlist[i]->filename;
2142 file_description = "Filter File";
2146 #ifdef FEATURE_TRUST
2150 filename = csp->tlist->filename;
2151 file_description = "Trust File";
2154 #endif /* def FEATURE_TRUST */
2157 if (NULL != filename)
2159 struct map *exports;
2164 exports = default_exports(csp, "show-status");
2165 if (NULL == exports)
2167 return JB_ERR_MEMORY;
2170 if ( map(exports, "file-description", 1, file_description, 1)
2171 || map(exports, "filepath", 1, html_encode(filename), 0) )
2174 return JB_ERR_MEMORY;
2177 err = load_file(filename, &s, &length);
2178 if (JB_ERR_OK != err)
2180 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2183 return JB_ERR_MEMORY;
2188 s = html_encode_and_free_original(s);
2191 return JB_ERR_MEMORY;
2194 if (map(exports, "contents", 1, s, 0))
2197 return JB_ERR_MEMORY;
2201 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2204 return JB_ERR_CGI_PARAMS;
2208 /*********************************************************************
2210 * Function : load_file
2212 * Description : Loads a file into a buffer.
2215 * 1 : filename = Name of the file to be loaded.
2216 * 2 : buffer = Used to return the file's content.
2217 * 3 : length = Used to return the size of the file.
2219 * Returns : JB_ERR_OK in case of success,
2220 * JB_ERR_FILE in case of ordinary file loading errors
2221 * (fseek() and ftell() errors are fatal)
2222 * JB_ERR_MEMORY in case of out-of-memory.
2224 *********************************************************************/
2225 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2229 jb_err err = JB_ERR_OK;
2231 fp = fopen(filename, "rb");
2237 /* Get file length */
2238 if (fseek(fp, 0, SEEK_END))
2240 log_error(LOG_LEVEL_FATAL,
2241 "Unexpected error while fseek()ing to the end of %s: %E",
2247 log_error(LOG_LEVEL_FATAL,
2248 "Unexpected ftell() error while loading %s: %E",
2251 *length = (size_t)ret;
2253 /* Go back to the beginning. */
2254 if (fseek(fp, 0, SEEK_SET))
2256 log_error(LOG_LEVEL_FATAL,
2257 "Unexpected error while fseek()ing to the beginning of %s: %E",
2261 *buffer = (char *)zalloc(*length + 1);
2262 if (NULL == *buffer)
2264 err = JB_ERR_MEMORY;
2266 else if (!fread(*buffer, *length, 1, fp))
2269 * May happen if the file size changes between fseek() and
2270 * fread(). If it does, we just log it and serve what we got.
2272 log_error(LOG_LEVEL_ERROR,
2273 "Couldn't completely read file %s.", filename);