1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.90 2009/03/01 18:43:09 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.90 2009/03/01 18:43:09 fabiankeil
40 * Fix cparser warnings.
42 * Revision 1.89 2008/10/11 11:31:14 fabiankeil
43 * Add FEATURE_CONNECTION_KEEP_ALIVE to the list
44 * of conditional defines on the show-status page.
46 * Revision 1.88 2008/08/30 12:03:07 fabiankeil
47 * Remove FEATURE_COOKIE_JAR.
49 * Revision 1.87 2008/08/29 15:59:22 fabiankeil
52 * Revision 1.86 2008/06/28 14:19:05 fabiankeil
53 * Protocol detection is done case-insensitive, fix assertion
54 * to do the same. Yay for Privoxy-Regression-Test and zzuf.
56 * Revision 1.85 2008/05/26 17:30:55 fabiankeil
57 * Provide an OpenSearch Description to access the
58 * show-url-info page through "search engine plugins".
60 * Revision 1.84 2008/05/26 16:16:55 fabiankeil
61 * Spell error correctly.
63 * Revision 1.83 2008/05/12 14:51:30 fabiankeil
64 * Don't complain about an invalid URL if show-url-info is requested
65 * without parameters. Regression introduced in 1.81 by yours truly.
67 * Revision 1.82 2008/05/10 20:01:47 fabiankeil
68 * Fix an assertion that could erroneously
69 * trigger in case of memory shortage.
71 * Revision 1.81 2008/05/05 09:54:39 fabiankeil
72 * In cgi_show_url_info(), make sure ftp URLs are
73 * declared invalid. Also simplify the code that adds
74 * "http://" if no protocol has been specified.
76 * Revision 1.80 2008/05/04 16:18:32 fabiankeil
77 * Provide parse_http_url() with a third parameter to specify
78 * whether or not URLs without protocol are acceptable.
80 * Revision 1.79 2008/05/04 13:30:56 fabiankeil
81 * Streamline parse_http_url()'s prototype.
83 * Revision 1.78 2008/05/03 16:50:11 fabiankeil
84 * Leverage content_filters_enabled() in cgi_show_url_info().
86 * Revision 1.77 2008/05/02 09:47:48 fabiankeil
87 * In cgi_show_url_info, pass an initialized http structure
88 * to parse_http_url() as that will be required soonish and
89 * assert that https URLs are recognized correctly.
91 * Revision 1.76 2008/04/28 09:13:30 fabiankeil
92 * In load_file(), remember the error reason and fclose()
93 * and return later on instead of right away.
95 * Revision 1.75 2008/04/27 13:52:52 fabiankeil
96 * Move CGI file loading code into load_file() and
97 * add checks for unexpected errors.
99 * Revision 1.74 2008/04/26 15:50:56 fabiankeil
100 * Fix macro name in cgi_show_file() error path.
102 * Revision 1.73 2008/04/26 12:21:55 fabiankeil
103 * Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
105 * Revision 1.72 2008/04/26 10:34:15 fabiankeil
106 * If zlib support is unavailable and there are content filters active
107 * but the prevent-compression action is disabled, include a warning
108 * on the show-url-info page that compression might prevent filtering.
110 * Revision 1.71 2008/04/25 13:33:56 fabiankeil
111 * - Factor cgi_show_file() out of cgi_show_status().
112 * - Adjust cgi_show_status()'s parameter description to match reality.
114 * Revision 1.70 2008/04/24 16:12:38 fabiankeil
115 * In cgi_show_status(), load the requested file at once.
116 * Using string_join() for every line really doesn't scale.
118 * Revision 1.69 2008/04/17 14:40:48 fabiankeil
119 * Provide get_http_time() with the buffer size so it doesn't
120 * have to blindly assume that the buffer is big enough.
122 * Revision 1.68 2008/03/27 18:27:21 fabiankeil
123 * Remove kill-popups action.
125 * Revision 1.67 2008/03/27 17:00:05 fabiankeil
126 * Turn the favicon blobs into locals.
128 * Revision 1.66 2008/02/23 16:57:12 fabiankeil
129 * Rename url_actions() to get_url_actions() and let it
130 * use the standard parameter ordering.
132 * Revision 1.65 2008/02/23 16:33:43 fabiankeil
133 * Let forward_url() use the standard parameter ordering
134 * and mark its second parameter immutable.
136 * Revision 1.64 2008/02/03 13:56:07 fabiankeil
137 * Add SOCKS5 support for show-url-info CGI page.
139 * Revision 1.63 2008/02/01 06:04:31 fabiankeil
140 * If edit buttons on the show-url-info CGI page are hidden, explain why.
142 * Revision 1.62 2008/02/01 05:52:40 fabiankeil
143 * Hide edit buttons on the show-url-info CGI page if enable-edit-action
144 * is disabled. Patch by Lee with additional white space adjustments.
146 * Revision 1.61 2008/01/26 11:13:25 fabiankeil
147 * If enable-edit-actions is disabled, hide the edit buttons and explain why.
149 * Revision 1.60 2007/10/27 13:12:13 fabiankeil
150 * Finish 1.49 and check write access before
151 * showing edit buttons on show-url-info page.
153 * Revision 1.59 2007/10/19 16:42:36 fabiankeil
154 * Plug memory leak I introduced five months ago.
155 * Yay Valgrind and Privoxy-Regression-Test.
157 * Revision 1.58 2007/07/21 12:19:50 fabiankeil
158 * If show-url-info is called with an URL that Privoxy
159 * would reject as invalid, don't show unresolved forwarding
160 * variables, "final matches" or claim the site's secure.
162 * Revision 1.57 2007/06/01 16:53:05 fabiankeil
163 * Adjust cgi_show_url_info() to show what forward-override{}
164 * would do with the requested URL (instead of showing how the
165 * request for the CGI page would be forwarded if it wasn't a
168 * Revision 1.56 2007/05/21 10:50:35 fabiankeil
169 * - Use strlcpy() instead of strcpy().
170 * - Stop treating actions files special. Expect a complete file name
171 * (with or without path) like it's done for the rest of the files.
173 * - Don't rerun sed() in cgi_show_request().
175 * Revision 1.55 2007/04/13 13:36:46 fabiankeil
176 * Reference action files in CGI URLs by id instead
177 * of using the first part of the file name.
178 * Fixes BR 1694250 and BR 1590556.
180 * Revision 1.54 2007/04/09 18:11:35 fabiankeil
181 * Don't mistake VC++'s _snprintf() for a snprintf() replacement.
183 * Revision 1.53 2007/04/08 13:21:04 fabiankeil
184 * Reference action files in CGI URLs by id instead
185 * of using the first part of the file name.
186 * Fixes BR 1694250 and BR 1590556.
188 * Revision 1.52 2007/02/13 15:10:26 fabiankeil
189 * Apparently fopen()ing in "binary" mode doesn't require
190 * #ifdefs, it's already done without them in cgiedit.c.
192 * Revision 1.51 2007/02/10 16:55:22 fabiankeil
193 * - Show forwarding settings on the show-url-info page
194 * - Fix some HTML syntax errors.
196 * Revision 1.50 2007/01/23 15:51:17 fabiankeil
197 * Add favicon delivery functions.
199 * Revision 1.49 2007/01/20 16:29:38 fabiankeil
200 * Suppress edit buttons for action files if Privoxy has
201 * no write access. Suggested by Roland in PR 1564026.
203 * Revision 1.48 2007/01/20 15:31:31 fabiankeil
204 * Display warning if show-url-info CGI page
205 * is used while Privoxy is toggled off.
207 * Revision 1.47 2007/01/12 15:07:10 fabiankeil
208 * Use zalloc in cgi_send_user_manual.
210 * Revision 1.46 2007/01/02 12:49:46 fabiankeil
211 * Add FEATURE_ZLIB to the list of conditional
212 * defines at the show-status page.
214 * Revision 1.45 2006/12/28 18:16:41 fabiankeil
215 * Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
216 * body memory before using it, replaced sprintf calls with snprintf.
218 * Revision 1.44 2006/12/22 14:19:27 fabiankeil
219 * Removed checks whether or not AF_FILES have
220 * data structures associated with them in cgi_show_status.
221 * It doesn't matter as we're only interested in the file names.
223 * For the action files the checks were always true,
224 * but they prevented empty filter files from being
225 * listed. Fixes parts of BR 1619208.
227 * Revision 1.43 2006/12/17 17:57:56 fabiankeil
228 * - Added FEATURE_GRACEFUL_TERMINATION to the
229 * "conditional #defines" section
230 * - Escaped ampersands in generated HTML.
231 * - Renamed re-filter-filename to re-filter-filenames
233 * Revision 1.42 2006/11/21 15:43:12 fabiankeil
234 * Add special treatment for WIN32 to make sure
235 * cgi_send_user_manual opens the files in binary mode.
236 * Fixes BR 1600411 and unbreaks image delivery.
238 * Remove outdated comment.
240 * Revision 1.41 2006/10/09 19:18:28 roro
241 * Redirect http://p.p/user-manual (without trailing slash) to
242 * http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
244 * Revision 1.40 2006/09/09 13:05:33 fabiankeil
245 * Modified cgi_send_user_manual to serve binary
246 * content without destroying it first. Should also be
247 * faster now. Added ".jpg" check for Content-Type guessing.
249 * Revision 1.39 2006/09/08 09:49:23 fabiankeil
250 * Deliver documents in the user-manual directory
251 * with "Content-Type text/css" if their filename
254 * Revision 1.38 2006/09/06 18:45:03 fabiankeil
255 * Incorporate modified version of Roland Rosenfeld's patch to
256 * optionally access the user-manual via Privoxy. Closes patch 679075.
258 * Formatting changed to Privoxy style, added call to
259 * cgi_error_no_template if the requested file doesn't
260 * exist and modified check whether or not Privoxy itself
261 * should serve the manual. Should work cross-platform now.
263 * Revision 1.37 2006/07/18 14:48:45 david__schmidt
264 * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
265 * with what was really the latest development (the v_3_0_branch branch)
267 * Revision 1.35.2.7 2006/01/29 23:10:56 david__schmidt
268 * Multiple filter file support
270 * Revision 1.35.2.6 2005/07/04 03:13:43 david__schmidt
271 * Undo some damaging memory leak patches
273 * Revision 1.35.2.5 2005/05/07 21:50:55 david__schmidt
274 * A few memory leaks plugged (mostly on error paths)
276 * Revision 1.35.2.4 2005/04/04 02:21:24 david__schmidt
277 * Another instance of:
278 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
279 * Thanks to Magnus Holmgren for the patch
281 * Revision 1.35.2.3 2003/12/17 16:34:15 oes
282 * - Prevent line wrap beween "View/Edit" link buttons on status page
283 * - Some (mostly irrelevant) fixes for Out-of-mem-case handling
285 * Revision 1.35.2.2 2003/04/03 13:48:28 oes
286 * Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
288 * Revision 1.35.2.1 2002/07/04 15:02:38 oes
289 * Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
291 * Revision 1.35.2.1 2002/07/01 17:32:04 morcego
292 * Applying patch from Andreas as provided by Hal on the list.
293 * Message-ID: <20020701121218.V1606@feenix.burgiss.net>
295 * Revision 1.35 2002/05/12 21:44:44 jongfoster
296 * Adding amiga.[ch] revision information, if on an amiga.
298 * Revision 1.34 2002/04/30 12:06:12 oes
299 * Deleted unused code from default_cgi
301 * Revision 1.33 2002/04/30 11:14:52 oes
302 * Made csp the first parameter in *action_to_html
304 * Revision 1.32 2002/04/26 18:29:13 jongfoster
305 * Fixing this Visual C++ warning:
306 * cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
308 * Revision 1.31 2002/04/26 12:54:36 oes
309 * - Kill obsolete REDIRECT_URL code
310 * - Error handling fixes
311 * - Style sheet related HTML snipplet changes
312 * - cgi_show_url_info:
313 * - Matches now in table, actions on single lines,
315 * - standard.action suppressed
316 * - Buttons to View and Edit AFs
318 * Revision 1.30 2002/04/24 02:18:08 oes
319 * - show-status is now the starting point for editing
320 * the actions files, generate list of all AFs with buttons
321 * for viewing and editing, new look for file list (Jon:
322 * buttons now aligned ;-P ), view mode now supports multiple
323 * AFs, name changes, no view links for unspecified files,
324 * no edit link for standard.action.
326 * - Jon's multiple AF patch: cgi_show_url_info now uses all
327 * AFs and marks the output accordingly
329 * Revision 1.29 2002/04/10 13:38:35 oes
330 * load_template signature changed
332 * Revision 1.28 2002/04/07 15:42:12 jongfoster
333 * Fixing send-banner?type=auto when the image-blocker is
334 * a redirect to send-banner
336 * Revision 1.27 2002/04/05 15:50:48 oes
337 * added send-stylesheet CGI
339 * Revision 1.26 2002/04/04 00:36:36 gliptak
340 * always use pcre for matching
342 * Revision 1.25 2002/04/03 22:28:03 gliptak
343 * Removed references to gnu_regex
345 * Revision 1.24 2002/04/02 16:12:47 oes
346 * Fix: moving misplaced lines into #ifdef FEATURE_FORCE
348 * Revision 1.23 2002/03/26 22:29:54 swa
349 * we have a new homepage!
351 * Revision 1.22 2002/03/24 16:18:15 jongfoster
354 * Revision 1.21 2002/03/24 15:23:33 jongfoster
357 * Revision 1.20 2002/03/24 13:25:43 swa
358 * name change related issues
360 * Revision 1.19 2002/03/16 23:54:06 jongfoster
361 * Adding graceful termination feature, to help look for memory leaks.
362 * If you enable this (which, by design, has to be done by hand
363 * editing config.h) and then go to http://i.j.b/die, then the program
364 * will exit cleanly after the *next* request. It should free all the
365 * memory that was used.
367 * Revision 1.18 2002/03/12 01:44:49 oes
368 * Changed default for "blocked" image from jb logo to checkboard pattern
370 * Revision 1.17 2002/03/08 16:43:18 oes
371 * Added choice beween GIF and PNG built-in images
373 * Revision 1.16 2002/03/07 03:48:38 oes
374 * - Changed built-in images from GIF to PNG
375 * (with regard to Unisys patent issue)
376 * - Added a 4x4 pattern PNG which is less intrusive
377 * than the logo but also clearly marks the deleted banners
379 * Revision 1.15 2002/03/06 22:54:35 jongfoster
380 * Automated function-comment nitpicking.
382 * Revision 1.14 2002/03/02 04:14:50 david__schmidt
383 * Clean up a little CRLF unpleasantness that suddenly appeared
385 * Revision 1.13 2002/02/21 00:10:37 jongfoster
386 * Adding send-banner?type=auto option
388 * Revision 1.12 2002/01/23 01:03:32 jongfoster
389 * Fixing gcc [CygWin] compiler warnings
391 * Revision 1.11 2002/01/23 00:01:04 jongfoster
392 * Adding cgi_transparent_gif() for http://i.j.b/t
393 * Adding missing html_encode() to many CGI functions.
394 * Adding urlmatch.[ch] to http://i.j.b/show-version
396 * Revision 1.10 2002/01/17 21:10:37 jongfoster
397 * Changes to cgi_show_url_info to use new matching code from urlmatch.c.
398 * Also fixing a problem in the same function with improperly quoted URLs
399 * in output HTML, and adding code to handle https:// URLs correctly.
401 * Revision 1.9 2001/11/30 23:09:15 jongfoster
402 * Now reports on FEATURE_CGI_EDIT_ACTIONS
403 * Removing FEATURE_DENY_GZIP from template
405 * Revision 1.8 2001/11/13 00:14:07 jongfoster
406 * Fixing stupid bug now I've figured out what || means.
407 * (It always returns 0 or 1, not one of it's paramaters.)
409 * Revision 1.7 2001/10/23 21:48:19 jongfoster
410 * Cleaning up error handling in CGI functions - they now send back
411 * a HTML error page and should never cause a FATAL error. (Fixes one
412 * potential source of "denial of service" attacks).
414 * CGI actions file editor that works and is actually useful.
416 * Ability to toggle JunkBuster remotely using a CGI call.
418 * You can turn off both the above features in the main configuration
419 * file, e.g. if you are running a multi-user proxy.
421 * Revision 1.6 2001/10/14 22:00:32 jongfoster
422 * Adding support for a 404 error when an invalid CGI page is requested.
424 * Revision 1.5 2001/10/07 15:30:41 oes
425 * Removed FEATURE_DENY_GZIP
427 * Revision 1.4 2001/10/02 15:31:12 oes
428 * Introduced show-request cgi
430 * Revision 1.3 2001/09/22 16:34:44 jongfoster
431 * Removing unneeded #includes
433 * Revision 1.2 2001/09/19 18:01:11 oes
434 * Fixed comments; cosmetics
436 * Revision 1.1 2001/09/16 17:08:54 jongfoster
437 * Moving simple CGI functions from cgi.c to new file cgisimple.c
440 **********************************************************************/
446 #include <sys/types.h>
454 #endif /* def HAVE_ACCESS */
458 #include "cgisimple.h"
464 #include "miscutil.h"
467 #include "urlmatch.h"
470 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
472 static char *show_rcs(void);
473 static jb_err show_defines(struct map *exports);
474 static jb_err cgi_show_file(struct client_state *csp,
475 struct http_response *rsp,
476 const struct map *parameters);
477 static jb_err load_file(const char *filename, char **buffer, size_t *length);
479 /*********************************************************************
481 * Function : cgi_default
483 * Description : CGI function that is called for the CGI_SITE_1_HOST
484 * and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
485 * Boring - only exports the default exports.
488 * 1 : csp = Current client state (buffers, headers, etc...)
489 * 2 : rsp = http_response data structure for output
490 * 3 : parameters = map of cgi parameters
492 * CGI Parameters : none
494 * Returns : JB_ERR_OK on success
495 * JB_ERR_MEMORY on out-of-memory
497 *********************************************************************/
498 jb_err cgi_default(struct client_state *csp,
499 struct http_response *rsp,
500 const struct map *parameters)
509 if (NULL == (exports = default_exports(csp, "")))
511 return JB_ERR_MEMORY;
514 return template_fill_for_cgi(csp, "default", exports, rsp);
518 /*********************************************************************
520 * Function : cgi_error_404
522 * Description : CGI function that is called if an unknown action was
526 * 1 : csp = Current client state (buffers, headers, etc...)
527 * 2 : rsp = http_response data structure for output
528 * 3 : parameters = map of cgi parameters
530 * CGI Parameters : none
532 * Returns : JB_ERR_OK on success
533 * JB_ERR_MEMORY on out-of-memory error.
535 *********************************************************************/
536 jb_err cgi_error_404(struct client_state *csp,
537 struct http_response *rsp,
538 const struct map *parameters)
546 if (NULL == (exports = default_exports(csp, NULL)))
548 return JB_ERR_MEMORY;
551 rsp->status = strdup("404 Privoxy configuration page not found");
552 if (rsp->status == NULL)
555 return JB_ERR_MEMORY;
558 return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
562 #ifdef FEATURE_GRACEFUL_TERMINATION
563 /*********************************************************************
567 * Description : CGI function to shut down Privoxy.
568 * NOTE: Turning this on in a production build
569 * would be a BAD idea. An EXTREMELY BAD idea.
570 * In short, don't do it.
573 * 1 : csp = Current client state (buffers, headers, etc...)
574 * 2 : rsp = http_response data structure for output
575 * 3 : parameters = map of cgi parameters
577 * CGI Parameters : none
579 * Returns : JB_ERR_OK on success
580 * JB_ERR_MEMORY on out-of-memory error.
582 *********************************************************************/
583 jb_err cgi_die (struct client_state *csp,
584 struct http_response *rsp,
585 const struct map *parameters)
595 * I don't really care what gets sent back to the browser.
596 * Take the easy option - "out of memory" page.
599 return JB_ERR_MEMORY;
601 #endif /* def FEATURE_GRACEFUL_TERMINATION */
604 /*********************************************************************
606 * Function : cgi_show_request
608 * Description : Show the client's request and what sed() would have
612 * 1 : csp = Current client state (buffers, headers, etc...)
613 * 2 : rsp = http_response data structure for output
614 * 3 : parameters = map of cgi parameters
616 * CGI Parameters : none
618 * Returns : JB_ERR_OK on success
619 * JB_ERR_MEMORY on out-of-memory error.
621 *********************************************************************/
622 jb_err cgi_show_request(struct client_state *csp,
623 struct http_response *rsp,
624 const struct map *parameters)
633 if (NULL == (exports = default_exports(csp, "show-request")))
635 return JB_ERR_MEMORY;
639 * Repair the damage done to the IOB by get_header()
641 for (p = csp->iob->buf; p < csp->iob->eod; p++)
643 if (*p == '\0') *p = '\n';
647 * Export the original client's request and the one we would
648 * be sending to the server if this wasn't a CGI call
651 if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
654 return JB_ERR_MEMORY;
657 if (map(exports, "processed-request", 1,
658 html_encode_and_free_original(list_to_text(csp->headers)), 0))
661 return JB_ERR_MEMORY;
664 return template_fill_for_cgi(csp, "show-request", exports, rsp);
668 /*********************************************************************
670 * Function : cgi_send_banner
672 * Description : CGI function that returns a banner.
675 * 1 : csp = Current client state (buffers, headers, etc...)
676 * 2 : rsp = http_response data structure for output
677 * 3 : parameters = map of cgi parameters
680 * type : Selects the type of banner between "trans", "logo",
681 * and "auto". Defaults to "logo" if absent or invalid.
682 * "auto" means to select as if we were image-blocking.
683 * (Only the first character really counts; b and t are
686 * Returns : JB_ERR_OK on success
687 * JB_ERR_MEMORY on out-of-memory error.
689 *********************************************************************/
690 jb_err cgi_send_banner(struct client_state *csp,
691 struct http_response *rsp,
692 const struct map *parameters)
694 char imagetype = lookup(parameters, "type")[0];
697 * If type is auto, then determine the right thing
698 * to do from the set-image-blocker action
700 if (imagetype == 'a')
707 #ifdef FEATURE_IMAGE_BLOCKING
708 if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
710 static const char prefix1[] = CGI_PREFIX "send-banner?type=";
711 static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
712 const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
716 /* Use default - nothing to do here. */
718 else if (0 == strcmpic(p, "blank"))
722 else if (0 == strcmpic(p, "pattern"))
728 * If the action is to call this CGI, determine
731 else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
733 imagetype = p[sizeof(prefix1) - 1];
735 else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
737 imagetype = p[sizeof(prefix2) - 1];
741 * Everything else must (should) be a URL to
749 #endif /* def FEATURE_IMAGE_BLOCKING */
753 * Now imagetype is either the non-auto type we were called with,
754 * or it was auto and has since been determined. In any case, we
755 * can proceed to actually answering the request by sending a redirect
756 * or an image as appropriate:
758 if (imagetype == 'r')
760 rsp->status = strdup("302 Local Redirect from Privoxy");
761 if (rsp->status == NULL)
763 return JB_ERR_MEMORY;
765 if (enlist_unique_header(rsp->headers, "Location",
766 csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
768 return JB_ERR_MEMORY;
773 if ((imagetype == 'b') || (imagetype == 't'))
775 rsp->body = bindup(image_blank_data, image_blank_length);
776 rsp->content_length = image_blank_length;
780 rsp->body = bindup(image_pattern_data, image_pattern_length);
781 rsp->content_length = image_pattern_length;
784 if (rsp->body == NULL)
786 return JB_ERR_MEMORY;
788 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
790 return JB_ERR_MEMORY;
801 /*********************************************************************
803 * Function : cgi_transparent_image
805 * Description : CGI function that sends a 1x1 transparent image.
808 * 1 : csp = Current client state (buffers, headers, etc...)
809 * 2 : rsp = http_response data structure for output
810 * 3 : parameters = map of cgi parameters
812 * CGI Parameters : None
814 * Returns : JB_ERR_OK on success
815 * JB_ERR_MEMORY on out-of-memory error.
817 *********************************************************************/
818 jb_err cgi_transparent_image(struct client_state *csp,
819 struct http_response *rsp,
820 const struct map *parameters)
825 rsp->body = bindup(image_blank_data, image_blank_length);
826 rsp->content_length = image_blank_length;
828 if (rsp->body == NULL)
830 return JB_ERR_MEMORY;
833 if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
835 return JB_ERR_MEMORY;
845 /*********************************************************************
847 * Function : cgi_send_default_favicon
849 * Description : CGI function that sends the standard favicon.
852 * 1 : csp = Current client state (buffers, headers, etc...)
853 * 2 : rsp = http_response data structure for output
854 * 3 : parameters = map of cgi parameters
856 * CGI Parameters : None
858 * Returns : JB_ERR_OK on success
859 * JB_ERR_MEMORY on out-of-memory error.
861 *********************************************************************/
862 jb_err cgi_send_default_favicon(struct client_state *csp,
863 struct http_response *rsp,
864 const struct map *parameters)
866 static const char default_favicon_data[] =
867 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
868 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
869 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
870 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
871 "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
872 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
873 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
874 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
875 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
876 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
877 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
878 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
879 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
881 static const size_t favicon_length = sizeof(default_favicon_data) - 1;
886 rsp->body = bindup(default_favicon_data, favicon_length);
887 rsp->content_length = favicon_length;
889 if (rsp->body == NULL)
891 return JB_ERR_MEMORY;
894 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
896 return JB_ERR_MEMORY;
906 /*********************************************************************
908 * Function : cgi_send_error_favicon
910 * Description : CGI function that sends the favicon for error pages.
913 * 1 : csp = Current client state (buffers, headers, etc...)
914 * 2 : rsp = http_response data structure for output
915 * 3 : parameters = map of cgi parameters
917 * CGI Parameters : None
919 * Returns : JB_ERR_OK on success
920 * JB_ERR_MEMORY on out-of-memory error.
922 *********************************************************************/
923 jb_err cgi_send_error_favicon(struct client_state *csp,
924 struct http_response *rsp,
925 const struct map *parameters)
927 static const char error_favicon_data[] =
928 "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
929 "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
930 "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
931 "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
932 "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
933 "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
934 "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
935 "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
936 "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
937 "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
938 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
939 "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
940 "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
942 static const size_t favicon_length = sizeof(error_favicon_data) - 1;
947 rsp->body = bindup(error_favicon_data, favicon_length);
948 rsp->content_length = favicon_length;
950 if (rsp->body == NULL)
952 return JB_ERR_MEMORY;
955 if (enlist(rsp->headers, "Content-Type: image/x-icon"))
957 return JB_ERR_MEMORY;
967 /*********************************************************************
969 * Function : cgi_send_stylesheet
971 * Description : CGI function that sends a css stylesheet found
972 * in the cgi-style.css template
975 * 1 : csp = Current client state (buffers, headers, etc...)
976 * 2 : rsp = http_response data structure for output
977 * 3 : parameters = map of cgi parameters
979 * CGI Parameters : None
981 * Returns : JB_ERR_OK on success
982 * JB_ERR_MEMORY on out-of-memory error.
984 *********************************************************************/
985 jb_err cgi_send_stylesheet(struct client_state *csp,
986 struct http_response *rsp,
987 const struct map *parameters)
996 err = template_load(csp, &rsp->body, "cgi-style.css", 0);
998 if (err == JB_ERR_FILE)
1001 * No way to tell user; send empty stylesheet
1003 log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
1007 return err; /* JB_ERR_MEMORY */
1010 if (enlist(rsp->headers, "Content-Type: text/css"))
1012 return JB_ERR_MEMORY;
1020 /*********************************************************************
1022 * Function : cgi_send_url_info_osd
1024 * Description : CGI function that sends the OpenSearch Description
1025 * template for the show-url-info page. It allows to
1026 * access the page through "search engine plugins".
1029 * 1 : csp = Current client state (buffers, headers, etc...)
1030 * 2 : rsp = http_response data structure for output
1031 * 3 : parameters = map of cgi parameters
1033 * CGI Parameters : None
1035 * Returns : JB_ERR_OK on success
1036 * JB_ERR_MEMORY on out-of-memory error.
1038 *********************************************************************/
1039 jb_err cgi_send_url_info_osd(struct client_state *csp,
1040 struct http_response *rsp,
1041 const struct map *parameters)
1043 jb_err err = JB_ERR_MEMORY;
1044 struct map *exports = default_exports(csp, NULL);
1049 if (NULL != exports)
1051 err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1052 if (JB_ERR_OK == err)
1054 err = enlist(rsp->headers,
1055 "Content-Type: application/opensearchdescription+xml");
1064 /*********************************************************************
1066 * Function : cgi_send_user_manual
1068 * Description : CGI function that sends a file in the user
1072 * 1 : csp = Current client state (buffers, headers, etc...)
1073 * 2 : rsp = http_response data structure for output
1074 * 3 : parameters = map of cgi parameters
1076 * CGI Parameters : file=name.html, the name of the HTML file
1077 * (relative to user-manual from config)
1079 * Returns : JB_ERR_OK on success
1080 * JB_ERR_MEMORY on out-of-memory error.
1082 *********************************************************************/
1083 jb_err cgi_send_user_manual(struct client_state *csp,
1084 struct http_response *rsp,
1085 const struct map *parameters)
1087 const char * filename;
1089 jb_err err = JB_ERR_OK;
1096 if (!parameters->first)
1098 /* requested http://p.p/user-manual (without trailing slash) */
1099 return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1102 get_string_param(parameters, "file", &filename);
1103 /* Check paramter for hack attempts */
1104 if (filename && strchr(filename, '/'))
1106 return JB_ERR_CGI_PARAMS;
1108 if (filename && strstr(filename, ".."))
1110 return JB_ERR_CGI_PARAMS;
1113 full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1114 if (full_path == NULL)
1116 return JB_ERR_MEMORY;
1119 err = load_file(full_path, &rsp->body, &rsp->content_length);
1120 if (JB_ERR_OK != err)
1122 assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1123 if (JB_ERR_FILE == err)
1125 err = cgi_error_no_template(csp, rsp, full_path);
1132 /* Guess correct Content-Type based on the filename's ending */
1135 length = strlen(filename);
1141 if((length>=4) && !strcmp(&filename[length-4], ".css"))
1143 err = enlist(rsp->headers, "Content-Type: text/css");
1145 else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1147 err = enlist(rsp->headers, "Content-Type: image/jpeg");
1151 err = enlist(rsp->headers, "Content-Type: text/html");
1158 /*********************************************************************
1160 * Function : cgi_show_version
1162 * Description : CGI function that returns a a web page describing the
1163 * file versions of Privoxy.
1166 * 1 : csp = Current client state (buffers, headers, etc...)
1167 * 2 : rsp = http_response data structure for output
1168 * 3 : parameters = map of cgi parameters
1170 * CGI Parameters : none
1172 * Returns : JB_ERR_OK on success
1173 * JB_ERR_MEMORY on out-of-memory error.
1175 *********************************************************************/
1176 jb_err cgi_show_version(struct client_state *csp,
1177 struct http_response *rsp,
1178 const struct map *parameters)
1180 struct map *exports;
1186 if (NULL == (exports = default_exports(csp, "show-version")))
1188 return JB_ERR_MEMORY;
1191 if (map(exports, "sourceversions", 1, show_rcs(), 0))
1194 return JB_ERR_MEMORY;
1197 return template_fill_for_cgi(csp, "show-version", exports, rsp);
1201 /*********************************************************************
1203 * Function : cgi_show_status
1205 * Description : CGI function that returns a web page describing the
1206 * current status of Privoxy.
1209 * 1 : csp = Current client state (buffers, headers, etc...)
1210 * 2 : rsp = http_response data structure for output
1211 * 3 : parameters = map of cgi parameters
1214 * file : Which file to show. Only first letter is checked,
1219 * Default is to show menu and other information.
1221 * Returns : JB_ERR_OK on success
1222 * JB_ERR_MEMORY on out-of-memory error.
1224 *********************************************************************/
1225 jb_err cgi_show_status(struct client_state *csp,
1226 struct http_response *rsp,
1227 const struct map *parameters)
1233 char buf[BUFFER_SIZE];
1234 #ifdef FEATURE_STATISTICS
1235 float perc_rej; /* Percentage of http requests rejected */
1236 int local_urls_read;
1237 int local_urls_rejected;
1238 #endif /* ndef FEATURE_STATISTICS */
1239 jb_err err = JB_ERR_OK;
1241 struct map *exports;
1247 if ('\0' != *(lookup(parameters, "file")))
1249 return cgi_show_file(csp, rsp, parameters);
1252 if (NULL == (exports = default_exports(csp, "show-status")))
1254 return JB_ERR_MEMORY;
1258 for (j = 0; (s != NULL) && (j < Argc); j++)
1260 if (!err) err = string_join (&s, html_encode(Argv[j]));
1261 if (!err) err = string_append(&s, " ");
1263 if (!err) err = map(exports, "invocation", 1, s, 0);
1265 if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1266 if (!err) err = show_defines(exports);
1271 return JB_ERR_MEMORY;
1274 #ifdef FEATURE_STATISTICS
1275 local_urls_read = urls_read;
1276 local_urls_rejected = urls_rejected;
1279 * Need to alter the stats not to include the fetch of this
1282 * Can't do following thread safely! doh!
1285 * urls_rejected--; * This will be incremented subsequently *
1288 if (local_urls_read == 0)
1290 if (!err) err = map_block_killer(exports, "have-stats");
1294 if (!err) err = map_block_killer(exports, "have-no-stats");
1296 perc_rej = (float)local_urls_rejected * 100.0F /
1297 (float)local_urls_read;
1299 snprintf(buf, sizeof(buf), "%d", local_urls_read);
1300 if (!err) err = map(exports, "requests-received", 1, buf, 1);
1302 snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1303 if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1305 snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1306 if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1309 #else /* ndef FEATURE_STATISTICS */
1310 err = err || map_block_killer(exports, "statistics");
1311 #endif /* ndef FEATURE_STATISTICS */
1314 * List all action files in use, together with view and edit links,
1315 * except for standard.action, which should only be viewable. (Not
1316 * enforced in the editor itself)
1317 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1320 for (i = 0; i < MAX_AF_FILES; i++)
1322 if (csp->actions_list[i] != NULL)
1324 if (!err) err = string_append(&s, "<tr><td>");
1325 if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1326 snprintf(buf, sizeof(buf),
1327 "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&index=%u\">View</a>", i);
1328 if (!err) err = string_append(&s, buf);
1330 #ifdef FEATURE_CGI_EDIT_ACTIONS
1331 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1332 && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1333 && (NULL != csp->config->actions_file_short[i]))
1336 if (access(csp->config->actions_file[i], W_OK) == 0)
1338 #endif /* def HAVE_ACCESS */
1339 snprintf(buf, sizeof(buf), " <a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1340 if (!err) err = string_append(&s, buf);
1345 if (!err) err = string_append(&s, " <strong>No write access.</strong>");
1347 #endif /* def HAVE_ACCESS */
1351 if (!err) err = string_append(&s, "</td></tr>\n");
1356 if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1360 if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1364 * List all re_filterfiles in use, together with view options.
1365 * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1368 for (i = 0; i < MAX_AF_FILES; i++)
1370 if (csp->rlist[i] != NULL)
1372 if (!err) err = string_append(&s, "<tr><td>");
1373 if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1374 snprintf(buf, sizeof(buf),
1375 "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&index=%u\">View</a>", i);
1376 if (!err) err = string_append(&s, buf);
1377 if (!err) err = string_append(&s, "</td></tr>\n");
1382 if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1386 if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1387 if (!err) err = map_block_killer(exports, "have-filterfile");
1390 #ifdef FEATURE_TRUST
1393 if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1397 if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1398 if (!err) err = map_block_killer(exports, "have-trustfile");
1401 if (!err) err = map_block_killer(exports, "trust-support");
1402 #endif /* ndef FEATURE_TRUST */
1404 #ifdef FEATURE_CGI_EDIT_ACTIONS
1405 if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1407 err = map_block_killer(exports, "cgi-editor-is-disabled");
1409 #endif /* ndef CGI_EDIT_ACTIONS */
1414 return JB_ERR_MEMORY;
1417 return template_fill_for_cgi(csp, "show-status", exports, rsp);
1421 /*********************************************************************
1423 * Function : cgi_show_url_info
1425 * Description : CGI function that determines and shows which actions
1426 * Privoxy will perform for a given url, and which
1427 * matches starting from the defaults have lead to that.
1430 * 1 : csp = Current client state (buffers, headers, etc...)
1431 * 2 : rsp = http_response data structure for output
1432 * 3 : parameters = map of cgi parameters
1435 * url : The url whose actions are to be determined.
1436 * If url is unset, the url-given conditional will be
1437 * set, so that all but the form can be suppressed in
1440 * Returns : JB_ERR_OK on success
1441 * JB_ERR_MEMORY on out-of-memory error.
1443 *********************************************************************/
1444 jb_err cgi_show_url_info(struct client_state *csp,
1445 struct http_response *rsp,
1446 const struct map *parameters)
1449 struct map *exports;
1456 if (NULL == (exports = default_exports(csp, "show-url-info")))
1458 return JB_ERR_MEMORY;
1462 * Get the url= parameter (if present) and remove any leading/trailing spaces.
1464 url_param = strdup(lookup(parameters, "url"));
1465 if (url_param == NULL)
1468 return JB_ERR_MEMORY;
1473 * Handle prefixes. 4 possibilities:
1474 * 1) "http://" or "https://" prefix present and followed by URL - OK
1475 * 2) Only the "http://" or "https://" part is present, no URL - change
1476 * to empty string so it will be detected later as "no URL".
1477 * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1479 * 4) Parameter not specified or is empty string - let this fall through
1480 * for now, next block of code will handle it.
1482 if (0 == strncmp(url_param, "http://", 7))
1484 if (url_param[7] == '\0')
1487 * Empty URL (just prefix).
1488 * Make it totally empty so it's caught by the next if()
1490 url_param[0] = '\0';
1493 else if (0 == strncmp(url_param, "https://", 8))
1495 if (url_param[8] == '\0')
1498 * Empty URL (just prefix).
1499 * Make it totally empty so it's caught by the next if()
1501 url_param[0] = '\0';
1504 else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1506 /* No prefix - assume http:// */
1507 char *url_param_prefixed = strdup("http://");
1509 if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1512 return JB_ERR_MEMORY;
1514 url_param = url_param_prefixed;
1518 * Hide "toggle off" warning if Privoxy is toggled on.
1521 #ifdef FEATURE_TOGGLE
1522 (global_toggle_state == 1) &&
1523 #endif /* def FEATURE_TOGGLE */
1524 map_block_killer(exports, "privoxy-is-toggled-off")
1528 return JB_ERR_MEMORY;
1531 if (url_param[0] == '\0')
1533 /* URL paramater not specified, display query form only. */
1535 if (map_block_killer(exports, "url-given")
1536 || map(exports, "url", 1, "", 1))
1539 return JB_ERR_MEMORY;
1544 /* Given a URL, so query it. */
1549 struct file_list *fl;
1550 struct url_actions *b;
1551 struct http_request url_to_query[1];
1552 struct current_action_spec action[1];
1555 if (map(exports, "url", 1, html_encode(url_param), 0))
1559 return JB_ERR_MEMORY;
1562 init_current_action(action);
1564 if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1566 free_current_action(action);
1569 return JB_ERR_MEMORY;
1572 memset(url_to_query, '\0', sizeof(url_to_query));
1573 err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1574 assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1578 if (err == JB_ERR_MEMORY)
1580 free_http_request(url_to_query);
1581 free_current_action(action);
1583 return JB_ERR_MEMORY;
1589 err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1590 if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1591 if (!err) err = map_block_killer(exports, "valid-url");
1593 free_current_action(action);
1594 free_http_request(url_to_query);
1599 return JB_ERR_MEMORY;
1602 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1606 * We have a warning about SSL paths. Hide it for unencrypted sites.
1608 if (!url_to_query->ssl)
1610 if (map_block_killer(exports, "https"))
1612 free_current_action(action);
1614 free_http_request(url_to_query);
1615 return JB_ERR_MEMORY;
1619 matches = strdup("<table summary=\"\" class=\"transparent\">");
1621 for (i = 0; i < MAX_AF_FILES; i++)
1623 if (NULL == csp->config->actions_file_short[i]
1624 || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1628 if ((fl = csp->actions_list[i]) != NULL)
1630 if ((b = fl->f) != NULL)
1632 /* FIXME: Hardcoded HTML! */
1633 string_append(&matches, "<tr><th>In file: ");
1634 string_join (&matches, html_encode(csp->config->actions_file_short[i]));
1635 snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&index=%d\">", i);
1636 string_append(&matches, buf);
1637 string_append(&matches, "View</a>");
1638 #ifdef FEATURE_CGI_EDIT_ACTIONS
1639 if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1642 if (access(csp->config->actions_file[i], W_OK) == 0)
1644 #endif /* def HAVE_ACCESS */
1645 snprintf(buf, sizeof(buf),
1646 " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1647 string_append(&matches, buf);
1648 string_append(&matches, "Edit</a>");
1653 string_append(&matches, " <strong>No write access.</strong>");
1655 #endif /* def HAVE_ACCESS */
1657 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1659 string_append(&matches, "</th></tr>\n");
1666 for (; (b != NULL) && (matches != NULL); b = b->next)
1668 if (url_match(b->url, url_to_query))
1670 string_append(&matches, "<tr><td>{");
1671 string_join (&matches, actions_to_html(csp, b->action));
1672 string_append(&matches, " }<br>\n<code>");
1673 string_join (&matches, html_encode(b->url->spec));
1674 string_append(&matches, "</code></td></tr>\n");
1676 if (merge_current_action(action, b->action))
1679 free_http_request(url_to_query);
1680 free_current_action(action);
1682 return JB_ERR_MEMORY;
1690 string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1693 string_append(&matches, "</table>\n");
1696 * XXX: Kludge to make sure the "Forward settings" section
1697 * shows what forward-override{} would do with the requested URL.
1698 * No one really cares how the CGI request would be forwarded
1699 * if it wasn't intercepted as CGI request in the first place.
1701 * From here on the action bitmask will no longer reflect
1702 * the real url (http://config.privoxy.org/show-url-info?url=.*),
1703 * but luckily it's no longer required later on anyway.
1705 free_current_action(csp->action);
1706 get_url_actions(csp, url_to_query);
1709 * Fill in forwarding settings.
1711 * The possibilities are:
1713 * - http forwarding only
1714 * - socks4(a) forwarding only
1715 * - socks4(a) and http forwarding.
1717 * XXX: Parts of this code could be reused for the
1718 * "forwarding-failed" template which currently doesn't
1719 * display the proxy port and an eventual second forwarder.
1722 const struct forward_spec *fwd = forward_url(csp, url_to_query);
1724 if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1726 if (!err) err = map_block_killer(exports, "socks-forwarder");
1727 if (!err) err = map_block_killer(exports, "http-forwarder");
1731 char port[10]; /* We save proxy ports as int but need a string here */
1733 if (!err) err = map_block_killer(exports, "no-forwarder");
1735 if (fwd->gateway_host != NULL)
1737 char *socks_type = NULL;
1742 socks_type = "socks4";
1745 socks_type = "socks4a";
1748 socks_type = "socks5";
1751 log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1754 if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1755 if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1756 snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1757 if (!err) err = map(exports, "gateway-port", 1, port, 1);
1761 if (!err) err = map_block_killer(exports, "socks-forwarder");
1764 if (fwd->forward_host != NULL)
1766 if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1767 snprintf(port, sizeof(port), "%d", fwd->forward_port);
1768 if (!err) err = map(exports, "forward-port", 1, port, 1);
1772 if (!err) err = map_block_killer(exports, "http-forwarder");
1777 free_http_request(url_to_query);
1779 if (err || matches == NULL)
1781 free_current_action(action);
1783 return JB_ERR_MEMORY;
1786 #ifdef FEATURE_CGI_EDIT_ACTIONS
1787 if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1789 err = map_block_killer(exports, "cgi-editor-is-disabled");
1791 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1794 * If zlib support is available, if no content filters
1795 * are enabled or if the prevent-compression action is enabled,
1796 * suppress the "compression could prevent filtering" warning.
1798 #ifndef FEATURE_ZLIB
1799 if (!content_filters_enabled(action) ||
1800 (action->flags & ACTION_NO_COMPRESSION))
1803 if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1806 if (err || map(exports, "matches", 1, matches , 0))
1808 free_current_action(action);
1810 return JB_ERR_MEMORY;
1813 s = current_action_to_html(csp, action);
1815 free_current_action(action);
1817 if (map(exports, "final", 1, s, 0))
1820 return JB_ERR_MEMORY;
1824 return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1828 /*********************************************************************
1830 * Function : cgi_robots_txt
1832 * Description : CGI function to return "/robots.txt".
1835 * 1 : csp = Current client state (buffers, headers, etc...)
1836 * 2 : rsp = http_response data structure for output
1837 * 3 : parameters = map of cgi parameters
1839 * CGI Parameters : None
1841 * Returns : JB_ERR_OK on success
1842 * JB_ERR_MEMORY on out-of-memory error.
1844 *********************************************************************/
1845 jb_err cgi_robots_txt(struct client_state *csp,
1846 struct http_response *rsp,
1847 const struct map *parameters)
1856 "# This is the Privoxy control interface.\n"
1857 "# It isn't very useful to index it, and you're likely to break stuff.\n"
1863 if (rsp->body == NULL)
1865 return JB_ERR_MEMORY;
1868 err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1872 get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1873 if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1875 return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1879 /*********************************************************************
1881 * Function : show_defines
1883 * Description : Add to a map the state od all conditional #defines
1884 * used when building
1887 * 1 : exports = map to extend
1889 * Returns : JB_ERR_OK on success
1890 * JB_ERR_MEMORY on out-of-memory error.
1892 *********************************************************************/
1893 static jb_err show_defines(struct map *exports)
1895 jb_err err = JB_ERR_OK;
1898 if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1899 #else /* ifndef FEATURE_ACL */
1900 if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1901 #endif /* ndef FEATURE_ACL */
1903 #ifdef FEATURE_CGI_EDIT_ACTIONS
1904 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1905 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1906 if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1907 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1909 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1910 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1911 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1912 if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1913 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1915 #ifdef FEATURE_FAST_REDIRECTS
1916 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1917 #else /* ifndef FEATURE_FAST_REDIRECTS */
1918 if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1919 #endif /* ndef FEATURE_FAST_REDIRECTS */
1921 #ifdef FEATURE_FORCE_LOAD
1922 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1923 if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1924 #else /* ifndef FEATURE_FORCE_LOAD */
1925 if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1926 if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1927 #endif /* ndef FEATURE_FORCE_LOAD */
1929 #ifdef FEATURE_GRACEFUL_TERMINATION
1930 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1931 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1932 if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1933 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1935 #ifdef FEATURE_IMAGE_BLOCKING
1936 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1937 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1938 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1939 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1941 #ifdef FEATURE_IMAGE_DETECT_MSIE
1942 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1943 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1944 if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1945 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1947 #ifdef FEATURE_NO_GIFS
1948 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1949 #else /* ifndef FEATURE_NO_GIFS */
1950 if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1951 #endif /* ndef FEATURE_NO_GIFS */
1953 #ifdef FEATURE_PTHREAD
1954 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1955 #else /* ifndef FEATURE_PTHREAD */
1956 if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1957 #endif /* ndef FEATURE_PTHREAD */
1959 #ifdef FEATURE_STATISTICS
1960 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1961 #else /* ifndef FEATURE_STATISTICS */
1962 if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1963 #endif /* ndef FEATURE_STATISTICS */
1965 #ifdef FEATURE_TOGGLE
1966 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1967 #else /* ifndef FEATURE_TOGGLE */
1968 if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1969 #endif /* ndef FEATURE_TOGGLE */
1971 #ifdef FEATURE_TRUST
1972 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1973 #else /* ifndef FEATURE_TRUST */
1974 if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1975 #endif /* ndef FEATURE_TRUST */
1978 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1979 #else /* ifndef FEATURE_ZLIB */
1980 if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1981 #endif /* ndef FEATURE_ZLIB */
1984 if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1985 #else /* ifndef STATIC_PCRE */
1986 if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1987 #endif /* ndef STATIC_PCRE */
1990 if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1991 #else /* ifndef STATIC_PCRS */
1992 if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1993 #endif /* ndef STATIC_PCRS */
1999 /*********************************************************************
2001 * Function : show_rcs
2003 * Description : Create a string with the rcs info for all sourcefiles
2007 * Returns : A string, or NULL on out-of-memory.
2009 *********************************************************************/
2010 static char *show_rcs(void)
2012 char *result = strdup("");
2013 char buf[BUFFER_SIZE];
2015 /* Instead of including *all* dot h's in the project (thus creating a
2016 * tremendous amount of dependencies), I will concede to declaring them
2017 * as extern's. This forces the developer to add to this list, but oh well.
2020 #define SHOW_RCS(__x) \
2022 extern const char __x[]; \
2023 snprintf(buf, sizeof(buf), " %s\n", __x); \
2024 string_append(&result, buf); \
2027 /* In alphabetical order */
2028 SHOW_RCS(actions_h_rcs)
2029 SHOW_RCS(actions_rcs)
2031 SHOW_RCS(amiga_h_rcs)
2033 #endif /* def AMIGA */
2036 #ifdef FEATURE_CGI_EDIT_ACTIONS
2037 SHOW_RCS(cgiedit_h_rcs)
2038 SHOW_RCS(cgiedit_rcs)
2039 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
2040 SHOW_RCS(cgisimple_h_rcs)
2041 SHOW_RCS(cgisimple_rcs)
2043 SHOW_RCS(cygwin_h_rcs)
2045 SHOW_RCS(deanimate_h_rcs)
2046 SHOW_RCS(deanimate_rcs)
2047 SHOW_RCS(encode_h_rcs)
2048 SHOW_RCS(encode_rcs)
2049 SHOW_RCS(errlog_h_rcs)
2050 SHOW_RCS(errlog_rcs)
2051 SHOW_RCS(filters_h_rcs)
2052 SHOW_RCS(filters_rcs)
2053 SHOW_RCS(gateway_h_rcs)
2054 SHOW_RCS(gateway_rcs)
2055 SHOW_RCS(jbsockets_h_rcs)
2056 SHOW_RCS(jbsockets_rcs)
2059 SHOW_RCS(list_h_rcs)
2061 SHOW_RCS(loadcfg_h_rcs)
2062 SHOW_RCS(loadcfg_rcs)
2063 SHOW_RCS(loaders_h_rcs)
2064 SHOW_RCS(loaders_rcs)
2065 SHOW_RCS(miscutil_h_rcs)
2066 SHOW_RCS(miscutil_rcs)
2067 SHOW_RCS(parsers_h_rcs)
2068 SHOW_RCS(parsers_rcs)
2070 SHOW_RCS(pcrs_h_rcs)
2071 SHOW_RCS(project_h_rcs)
2072 SHOW_RCS(ssplit_h_rcs)
2073 SHOW_RCS(ssplit_rcs)
2074 SHOW_RCS(urlmatch_h_rcs)
2075 SHOW_RCS(urlmatch_rcs)
2077 #ifndef _WIN_CONSOLE
2078 SHOW_RCS(w32log_h_rcs)
2079 SHOW_RCS(w32log_rcs)
2080 SHOW_RCS(w32res_h_rcs)
2081 SHOW_RCS(w32taskbar_h_rcs)
2082 SHOW_RCS(w32taskbar_rcs)
2083 #endif /* ndef _WIN_CONSOLE */
2084 SHOW_RCS(win32_h_rcs)
2086 #endif /* def _WIN32 */
2095 /*********************************************************************
2097 * Function : cgi_show_file
2099 * Description : CGI function that shows the content of a
2100 * configuration file.
2103 * 1 : csp = Current client state (buffers, headers, etc...)
2104 * 2 : rsp = http_response data structure for output
2105 * 3 : parameters = map of cgi parameters
2108 * file : Which file to show. Only first letter is checked,
2113 * Default is to show menu and other information.
2115 * Returns : JB_ERR_OK on success
2116 * JB_ERR_MEMORY on out-of-memory error.
2118 *********************************************************************/
2119 static jb_err cgi_show_file(struct client_state *csp,
2120 struct http_response *rsp,
2121 const struct map *parameters)
2124 const char * filename = NULL;
2125 char * file_description = NULL;
2131 switch (*(lookup(parameters, "file")))
2134 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2136 filename = csp->actions_list[i]->filename;
2137 file_description = "Actions File";
2142 if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2144 filename = csp->rlist[i]->filename;
2145 file_description = "Filter File";
2149 #ifdef FEATURE_TRUST
2153 filename = csp->tlist->filename;
2154 file_description = "Trust File";
2157 #endif /* def FEATURE_TRUST */
2160 if (NULL != filename)
2162 struct map *exports;
2167 exports = default_exports(csp, "show-status");
2168 if (NULL == exports)
2170 return JB_ERR_MEMORY;
2173 if ( map(exports, "file-description", 1, file_description, 1)
2174 || map(exports, "filepath", 1, html_encode(filename), 0) )
2177 return JB_ERR_MEMORY;
2180 err = load_file(filename, &s, &length);
2181 if (JB_ERR_OK != err)
2183 if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2186 return JB_ERR_MEMORY;
2191 s = html_encode_and_free_original(s);
2194 return JB_ERR_MEMORY;
2197 if (map(exports, "contents", 1, s, 0))
2200 return JB_ERR_MEMORY;
2204 return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2207 return JB_ERR_CGI_PARAMS;
2211 /*********************************************************************
2213 * Function : load_file
2215 * Description : Loads a file into a buffer.
2218 * 1 : filename = Name of the file to be loaded.
2219 * 2 : buffer = Used to return the file's content.
2220 * 3 : length = Used to return the size of the file.
2222 * Returns : JB_ERR_OK in case of success,
2223 * JB_ERR_FILE in case of ordinary file loading errors
2224 * (fseek() and ftell() errors are fatal)
2225 * JB_ERR_MEMORY in case of out-of-memory.
2227 *********************************************************************/
2228 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2232 jb_err err = JB_ERR_OK;
2234 fp = fopen(filename, "rb");
2240 /* Get file length */
2241 if (fseek(fp, 0, SEEK_END))
2243 log_error(LOG_LEVEL_FATAL,
2244 "Unexpected error while fseek()ing to the end of %s: %E",
2250 log_error(LOG_LEVEL_FATAL,
2251 "Unexpected ftell() error while loading %s: %E",
2254 *length = (size_t)ret;
2256 /* Go back to the beginning. */
2257 if (fseek(fp, 0, SEEK_SET))
2259 log_error(LOG_LEVEL_FATAL,
2260 "Unexpected error while fseek()ing to the beginning of %s: %E",
2264 *buffer = (char *)zalloc(*length + 1);
2265 if (NULL == *buffer)
2267 err = JB_ERR_MEMORY;
2269 else if (!fread(*buffer, *length, 1, fp))
2272 * May happen if the file size changes between fseek() and
2273 * fread(). If it does, we just log it and serve what we got.
2275 log_error(LOG_LEVEL_ERROR,
2276 "Couldn't completely read file %s.", filename);