Advertise IPv6 support on the show-status page.
[privoxy.git] / cgisimple.c
1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.91 2009/03/08 14:19:23 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/cgisimple.c,v $
5  *
6  * Purpose     :  Simple CGIs to get information about Privoxy's
7  *                status.
8  *                
9  *                Functions declared include:
10  * 
11  *
12  * Copyright   :  Written by and Copyright (C) 2001-2008 the SourceForge
13  *                Privoxy team. http://www.privoxy.org/
14  *
15  *                Based on the Internet Junkbuster originally written
16  *                by and Copyright (C) 1997 Anonymous Coders and 
17  *                Junkbusters Corporation.  http://www.junkbusters.com
18  *
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.
24  *
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.
30  *
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.
36  *
37  * Revisions   :
38  *    $Log: cgisimple.c,v $
39  *    Revision 1.91  2009/03/08 14:19:23  fabiankeil
40  *    Fix justified (but harmless) compiler warnings
41  *    on platforms where sizeof(int) < sizeof(long).
42  *
43  *    Revision 1.90  2009/03/01 18:43:09  fabiankeil
44  *    Fix cparser warnings.
45  *
46  *    Revision 1.89  2008/10/11 11:31:14  fabiankeil
47  *    Add FEATURE_CONNECTION_KEEP_ALIVE to the list
48  *    of conditional defines on the show-status page.
49  *
50  *    Revision 1.88  2008/08/30 12:03:07  fabiankeil
51  *    Remove FEATURE_COOKIE_JAR.
52  *
53  *    Revision 1.87  2008/08/29 15:59:22  fabiankeil
54  *    Fix two comments.
55  *
56  *    Revision 1.86  2008/06/28 14:19:05  fabiankeil
57  *    Protocol detection is done case-insensitive, fix assertion
58  *    to do the same. Yay for Privoxy-Regression-Test and zzuf.
59  *
60  *    Revision 1.85  2008/05/26 17:30:55  fabiankeil
61  *    Provide an OpenSearch Description to access the
62  *    show-url-info page through "search engine plugins".
63  *
64  *    Revision 1.84  2008/05/26 16:16:55  fabiankeil
65  *    Spell error correctly.
66  *
67  *    Revision 1.83  2008/05/12 14:51:30  fabiankeil
68  *    Don't complain about an invalid URL if show-url-info is requested
69  *    without parameters. Regression introduced in 1.81 by yours truly.
70  *
71  *    Revision 1.82  2008/05/10 20:01:47  fabiankeil
72  *    Fix an assertion that could erroneously
73  *    trigger in case of memory shortage.
74  *
75  *    Revision 1.81  2008/05/05 09:54:39  fabiankeil
76  *    In cgi_show_url_info(), make sure ftp URLs are
77  *    declared invalid. Also simplify the code that adds
78  *    "http://" if no protocol has been specified.
79  *
80  *    Revision 1.80  2008/05/04 16:18:32  fabiankeil
81  *    Provide parse_http_url() with a third parameter to specify
82  *    whether or not URLs without protocol are acceptable.
83  *
84  *    Revision 1.79  2008/05/04 13:30:56  fabiankeil
85  *    Streamline parse_http_url()'s prototype.
86  *
87  *    Revision 1.78  2008/05/03 16:50:11  fabiankeil
88  *    Leverage content_filters_enabled() in cgi_show_url_info().
89  *
90  *    Revision 1.77  2008/05/02 09:47:48  fabiankeil
91  *    In cgi_show_url_info, pass an initialized http structure
92  *    to parse_http_url() as that will be required soonish and
93  *    assert that https URLs are recognized correctly.
94  *
95  *    Revision 1.76  2008/04/28 09:13:30  fabiankeil
96  *    In load_file(), remember the error reason and fclose()
97  *    and return later on instead of right away.
98  *
99  *    Revision 1.75  2008/04/27 13:52:52  fabiankeil
100  *    Move CGI file loading code into load_file() and
101  *    add checks for unexpected errors.
102  *
103  *    Revision 1.74  2008/04/26 15:50:56  fabiankeil
104  *    Fix macro name in cgi_show_file() error path.
105  *
106  *    Revision 1.73  2008/04/26 12:21:55  fabiankeil
107  *    Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
108  *
109  *    Revision 1.72  2008/04/26 10:34:15  fabiankeil
110  *    If zlib support is unavailable and there are content filters active
111  *    but the prevent-compression action is disabled, include a warning
112  *    on the show-url-info page that compression might prevent filtering.
113  *
114  *    Revision 1.71  2008/04/25 13:33:56  fabiankeil
115  *    - Factor cgi_show_file() out of cgi_show_status().
116  *    - Adjust cgi_show_status()'s parameter description to match reality.
117  *
118  *    Revision 1.70  2008/04/24 16:12:38  fabiankeil
119  *    In cgi_show_status(), load the requested file at once.
120  *    Using string_join() for every line really doesn't scale.
121  *
122  *    Revision 1.69  2008/04/17 14:40:48  fabiankeil
123  *    Provide get_http_time() with the buffer size so it doesn't
124  *    have to blindly assume that the buffer is big enough.
125  *
126  *    Revision 1.68  2008/03/27 18:27:21  fabiankeil
127  *    Remove kill-popups action.
128  *
129  *    Revision 1.67  2008/03/27 17:00:05  fabiankeil
130  *    Turn the favicon blobs into locals.
131  *
132  *    Revision 1.66  2008/02/23 16:57:12  fabiankeil
133  *    Rename url_actions() to get_url_actions() and let it
134  *    use the standard parameter ordering.
135  *
136  *    Revision 1.65  2008/02/23 16:33:43  fabiankeil
137  *    Let forward_url() use the standard parameter ordering
138  *    and mark its second parameter immutable.
139  *
140  *    Revision 1.64  2008/02/03 13:56:07  fabiankeil
141  *    Add SOCKS5 support for show-url-info CGI page.
142  *
143  *    Revision 1.63  2008/02/01 06:04:31  fabiankeil
144  *    If edit buttons on the show-url-info CGI page are hidden, explain why.
145  *
146  *    Revision 1.62  2008/02/01 05:52:40  fabiankeil
147  *    Hide edit buttons on the show-url-info CGI page if enable-edit-action
148  *    is disabled. Patch by Lee with additional white space adjustments.
149  *
150  *    Revision 1.61  2008/01/26 11:13:25  fabiankeil
151  *    If enable-edit-actions is disabled, hide the edit buttons and explain why.
152  *
153  *    Revision 1.60  2007/10/27 13:12:13  fabiankeil
154  *    Finish 1.49 and check write access before
155  *    showing edit buttons on show-url-info page.
156  *
157  *    Revision 1.59  2007/10/19 16:42:36  fabiankeil
158  *    Plug memory leak I introduced five months ago.
159  *    Yay Valgrind and Privoxy-Regression-Test.
160  *
161  *    Revision 1.58  2007/07/21 12:19:50  fabiankeil
162  *    If show-url-info is called with an URL that Privoxy
163  *    would reject as invalid, don't show unresolved forwarding
164  *    variables, "final matches" or claim the site's secure.
165  *
166  *    Revision 1.57  2007/06/01 16:53:05  fabiankeil
167  *    Adjust cgi_show_url_info() to show what forward-override{}
168  *    would do with the requested URL (instead of showing how the
169  *    request for the CGI page would be forwarded if it wasn't a
170  *    CGI request).
171  *
172  *    Revision 1.56  2007/05/21 10:50:35  fabiankeil
173  *    - Use strlcpy() instead of strcpy().
174  *    - Stop treating actions files special. Expect a complete file name
175  *      (with or without path) like it's done for the rest of the files.
176  *      Closes FR#588084.
177  *    - Don't rerun sed() in cgi_show_request().
178  *
179  *    Revision 1.55  2007/04/13 13:36:46  fabiankeil
180  *    Reference action files in CGI URLs by id instead
181  *    of using the first part of the file name.
182  *    Fixes BR 1694250 and BR 1590556.
183  *
184  *    Revision 1.54  2007/04/09 18:11:35  fabiankeil
185  *    Don't mistake VC++'s _snprintf() for a snprintf() replacement.
186  *
187  *    Revision 1.53  2007/04/08 13:21:04  fabiankeil
188  *    Reference action files in CGI URLs by id instead
189  *    of using the first part of the file name.
190  *    Fixes BR 1694250 and BR 1590556.
191  *
192  *    Revision 1.52  2007/02/13 15:10:26  fabiankeil
193  *    Apparently fopen()ing in "binary" mode doesn't require
194  *    #ifdefs, it's already done without them in cgiedit.c.
195  *
196  *    Revision 1.51  2007/02/10 16:55:22  fabiankeil
197  *    - Show forwarding settings on the show-url-info page
198  *    - Fix some HTML syntax errors.
199  *
200  *    Revision 1.50  2007/01/23 15:51:17  fabiankeil
201  *    Add favicon delivery functions.
202  *
203  *    Revision 1.49  2007/01/20 16:29:38  fabiankeil
204  *    Suppress edit buttons for action files if Privoxy has
205  *    no write access. Suggested by Roland in PR 1564026.
206  *
207  *    Revision 1.48  2007/01/20 15:31:31  fabiankeil
208  *    Display warning if show-url-info CGI page
209  *    is used while Privoxy is toggled off.
210  *
211  *    Revision 1.47  2007/01/12 15:07:10  fabiankeil
212  *    Use zalloc in cgi_send_user_manual.
213  *
214  *    Revision 1.46  2007/01/02 12:49:46  fabiankeil
215  *    Add FEATURE_ZLIB to the list of conditional
216  *    defines at the show-status page.
217  *
218  *    Revision 1.45  2006/12/28 18:16:41  fabiankeil
219  *    Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
220  *    body memory before using it, replaced sprintf calls with snprintf.
221  *
222  *    Revision 1.44  2006/12/22 14:19:27  fabiankeil
223  *    Removed checks whether or not AF_FILES have
224  *    data structures associated with them in cgi_show_status.
225  *    It doesn't matter as we're only interested in the file names.
226  *
227  *    For the action files the checks were always true,
228  *    but they prevented empty filter files from being
229  *    listed. Fixes parts of BR 1619208.
230  *
231  *    Revision 1.43  2006/12/17 17:57:56  fabiankeil
232  *    - Added FEATURE_GRACEFUL_TERMINATION to the
233  *      "conditional #defines" section
234  *    - Escaped ampersands in generated HTML.
235  *    - Renamed re-filter-filename to re-filter-filenames
236  *
237  *    Revision 1.42  2006/11/21 15:43:12  fabiankeil
238  *    Add special treatment for WIN32 to make sure
239  *    cgi_send_user_manual opens the files in binary mode.
240  *    Fixes BR 1600411 and unbreaks image delivery.
241  *
242  *    Remove outdated comment.
243  *
244  *    Revision 1.41  2006/10/09 19:18:28  roro
245  *    Redirect http://p.p/user-manual (without trailing slash) to
246  *    http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
247  *
248  *    Revision 1.40  2006/09/09 13:05:33  fabiankeil
249  *    Modified cgi_send_user_manual to serve binary
250  *    content without destroying it first. Should also be
251  *    faster now. Added ".jpg" check for Content-Type guessing.
252  *
253  *    Revision 1.39  2006/09/08 09:49:23  fabiankeil
254  *    Deliver documents in the user-manual directory
255  *    with "Content-Type text/css" if their filename
256  *    ends with ".css".
257  *
258  *    Revision 1.38  2006/09/06 18:45:03  fabiankeil
259  *    Incorporate modified version of Roland Rosenfeld's patch to
260  *    optionally access the user-manual via Privoxy. Closes patch 679075.
261  *
262  *    Formatting changed to Privoxy style, added call to
263  *    cgi_error_no_template if the requested file doesn't
264  *    exist and modified check whether or not Privoxy itself
265  *    should serve the manual. Should work cross-platform now.
266  *
267  *    Revision 1.37  2006/07/18 14:48:45  david__schmidt
268  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
269  *    with what was really the latest development (the v_3_0_branch branch)
270  *
271  *    Revision 1.35.2.7  2006/01/29 23:10:56  david__schmidt
272  *    Multiple filter file support
273  *
274  *    Revision 1.35.2.6  2005/07/04 03:13:43  david__schmidt
275  *    Undo some damaging memory leak patches
276  *
277  *    Revision 1.35.2.5  2005/05/07 21:50:55  david__schmidt
278  *    A few memory leaks plugged (mostly on error paths)
279  *
280  *    Revision 1.35.2.4  2005/04/04 02:21:24  david__schmidt
281  *    Another instance of:
282  *    Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
283  *    Thanks to Magnus Holmgren for the patch
284  *
285  *    Revision 1.35.2.3  2003/12/17 16:34:15  oes
286  *     - Prevent line wrap beween "View/Edit" link buttons on status page
287  *     - Some (mostly irrelevant) fixes for Out-of-mem-case handling
288  *
289  *    Revision 1.35.2.2  2003/04/03 13:48:28  oes
290  *    Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
291  *
292  *    Revision 1.35.2.1  2002/07/04 15:02:38  oes
293  *    Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
294  *
295  *    Revision 1.35.2.1  2002/07/01 17:32:04  morcego
296  *    Applying patch from Andreas as provided by Hal on the list.
297  *    Message-ID: <20020701121218.V1606@feenix.burgiss.net>
298  *
299  *    Revision 1.35  2002/05/12 21:44:44  jongfoster
300  *    Adding amiga.[ch] revision information, if on an amiga.
301  *
302  *    Revision 1.34  2002/04/30 12:06:12  oes
303  *    Deleted unused code from default_cgi
304  *
305  *    Revision 1.33  2002/04/30 11:14:52  oes
306  *    Made csp the first parameter in *action_to_html
307  *
308  *    Revision 1.32  2002/04/26 18:29:13  jongfoster
309  *    Fixing this Visual C++ warning:
310  *    cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
311  *
312  *    Revision 1.31  2002/04/26 12:54:36  oes
313  *     - Kill obsolete REDIRECT_URL code
314  *     - Error handling fixes
315  *     - Style sheet related HTML snipplet changes
316  *     - cgi_show_url_info:
317  *       - Matches now in table, actions on single lines,
318  *         linked to help
319  *       - standard.action suppressed
320  *       - Buttons to View and Edit AFs
321  *
322  *    Revision 1.30  2002/04/24 02:18:08  oes
323  *     - show-status is now the starting point for editing
324  *       the actions files, generate list of all AFs with buttons
325  *       for viewing and editing, new look for file list (Jon:
326  *       buttons now aligned ;-P ), view mode now supports multiple
327  *       AFs, name changes, no view links for unspecified files,
328  *       no edit link for standard.action.
329  *
330  *     - Jon's multiple AF patch: cgi_show_url_info now uses all
331  *       AFs and marks the output accordingly
332  *
333  *    Revision 1.29  2002/04/10 13:38:35  oes
334  *    load_template signature changed
335  *
336  *    Revision 1.28  2002/04/07 15:42:12  jongfoster
337  *    Fixing send-banner?type=auto when the image-blocker is
338  *    a redirect to send-banner
339  *
340  *    Revision 1.27  2002/04/05 15:50:48  oes
341  *    added send-stylesheet CGI
342  *
343  *    Revision 1.26  2002/04/04 00:36:36  gliptak
344  *    always use pcre for matching
345  *
346  *    Revision 1.25  2002/04/03 22:28:03  gliptak
347  *    Removed references to gnu_regex
348  *
349  *    Revision 1.24  2002/04/02 16:12:47  oes
350  *    Fix: moving misplaced lines into #ifdef FEATURE_FORCE
351  *
352  *    Revision 1.23  2002/03/26 22:29:54  swa
353  *    we have a new homepage!
354  *
355  *    Revision 1.22  2002/03/24 16:18:15  jongfoster
356  *    Removing old logo
357  *
358  *    Revision 1.21  2002/03/24 15:23:33  jongfoster
359  *    Name changes
360  *
361  *    Revision 1.20  2002/03/24 13:25:43  swa
362  *    name change related issues
363  *
364  *    Revision 1.19  2002/03/16 23:54:06  jongfoster
365  *    Adding graceful termination feature, to help look for memory leaks.
366  *    If you enable this (which, by design, has to be done by hand
367  *    editing config.h) and then go to http://i.j.b/die, then the program
368  *    will exit cleanly after the *next* request.  It should free all the
369  *    memory that was used.
370  *
371  *    Revision 1.18  2002/03/12 01:44:49  oes
372  *    Changed default for "blocked" image from jb logo to checkboard pattern
373  *
374  *    Revision 1.17  2002/03/08 16:43:18  oes
375  *    Added choice beween GIF and PNG built-in images
376  *
377  *    Revision 1.16  2002/03/07 03:48:38  oes
378  *     - Changed built-in images from GIF to PNG
379  *       (with regard to Unisys patent issue)
380  *     - Added a 4x4 pattern PNG which is less intrusive
381  *       than the logo but also clearly marks the deleted banners
382  *
383  *    Revision 1.15  2002/03/06 22:54:35  jongfoster
384  *    Automated function-comment nitpicking.
385  *
386  *    Revision 1.14  2002/03/02 04:14:50  david__schmidt
387  *    Clean up a little CRLF unpleasantness that suddenly appeared
388  *
389  *    Revision 1.13  2002/02/21 00:10:37  jongfoster
390  *    Adding send-banner?type=auto option
391  *
392  *    Revision 1.12  2002/01/23 01:03:32  jongfoster
393  *    Fixing gcc [CygWin] compiler warnings
394  *
395  *    Revision 1.11  2002/01/23 00:01:04  jongfoster
396  *    Adding cgi_transparent_gif() for http://i.j.b/t
397  *    Adding missing html_encode() to many CGI functions.
398  *    Adding urlmatch.[ch] to http://i.j.b/show-version
399  *
400  *    Revision 1.10  2002/01/17 21:10:37  jongfoster
401  *    Changes to cgi_show_url_info to use new matching code from urlmatch.c.
402  *    Also fixing a problem in the same function with improperly quoted URLs
403  *    in output HTML, and adding code to handle https:// URLs correctly.
404  *
405  *    Revision 1.9  2001/11/30 23:09:15  jongfoster
406  *    Now reports on FEATURE_CGI_EDIT_ACTIONS
407  *    Removing FEATURE_DENY_GZIP from template
408  *
409  *    Revision 1.8  2001/11/13 00:14:07  jongfoster
410  *    Fixing stupid bug now I've figured out what || means.
411  *    (It always returns 0 or 1, not one of it's paramaters.)
412  *
413  *    Revision 1.7  2001/10/23 21:48:19  jongfoster
414  *    Cleaning up error handling in CGI functions - they now send back
415  *    a HTML error page and should never cause a FATAL error.  (Fixes one
416  *    potential source of "denial of service" attacks).
417  *
418  *    CGI actions file editor that works and is actually useful.
419  *
420  *    Ability to toggle JunkBuster remotely using a CGI call.
421  *
422  *    You can turn off both the above features in the main configuration
423  *    file, e.g. if you are running a multi-user proxy.
424  *
425  *    Revision 1.6  2001/10/14 22:00:32  jongfoster
426  *    Adding support for a 404 error when an invalid CGI page is requested.
427  *
428  *    Revision 1.5  2001/10/07 15:30:41  oes
429  *    Removed FEATURE_DENY_GZIP
430  *
431  *    Revision 1.4  2001/10/02 15:31:12  oes
432  *    Introduced show-request cgi
433  *
434  *    Revision 1.3  2001/09/22 16:34:44  jongfoster
435  *    Removing unneeded #includes
436  *
437  *    Revision 1.2  2001/09/19 18:01:11  oes
438  *    Fixed comments; cosmetics
439  *
440  *    Revision 1.1  2001/09/16 17:08:54  jongfoster
441  *    Moving simple CGI functions from cgi.c to new file cgisimple.c
442  *
443  *
444  **********************************************************************/
445 \f
446
447 #include "config.h"
448
449 #include <stdio.h>
450 #include <sys/types.h>
451 #include <stdlib.h>
452 #include <ctype.h>
453 #include <string.h>
454 #include <assert.h>
455
456 #ifdef HAVE_ACCESS
457 #include <unistd.h>
458 #endif /* def HAVE_ACCESS */
459
460 #include "project.h"
461 #include "cgi.h"
462 #include "cgisimple.h"
463 #include "list.h"
464 #include "encode.h"
465 #include "jcc.h"
466 #include "filters.h"
467 #include "actions.h"
468 #include "miscutil.h"
469 #include "loadcfg.h"
470 #include "parsers.h"
471 #include "urlmatch.h"
472 #include "errlog.h"
473
474 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
475
476 static char *show_rcs(void);
477 static jb_err show_defines(struct map *exports);
478 static jb_err cgi_show_file(struct client_state *csp,
479                             struct http_response *rsp,
480                             const struct map *parameters);
481 static jb_err load_file(const char *filename, char **buffer, size_t *length);
482
483 /*********************************************************************
484  *
485  * Function    :  cgi_default
486  *
487  * Description :  CGI function that is called for the CGI_SITE_1_HOST
488  *                and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
489  *                Boring - only exports the default exports.
490  *               
491  * Parameters  :
492  *          1  :  csp = Current client state (buffers, headers, etc...)
493  *          2  :  rsp = http_response data structure for output
494  *          3  :  parameters = map of cgi parameters
495  *
496  * CGI Parameters : none
497  *
498  * Returns     :  JB_ERR_OK on success
499  *                JB_ERR_MEMORY on out-of-memory
500  *
501  *********************************************************************/
502 jb_err cgi_default(struct client_state *csp,
503                    struct http_response *rsp,
504                    const struct map *parameters)
505 {
506    struct map *exports;
507
508    (void)parameters;
509
510    assert(csp);
511    assert(rsp);
512
513    if (NULL == (exports = default_exports(csp, "")))
514    {
515       return JB_ERR_MEMORY;
516    }
517
518    return template_fill_for_cgi(csp, "default", exports, rsp);
519 }
520
521
522 /*********************************************************************
523  *
524  * Function    :  cgi_error_404
525  *
526  * Description :  CGI function that is called if an unknown action was
527  *                given.
528  *               
529  * Parameters  :
530  *          1  :  csp = Current client state (buffers, headers, etc...)
531  *          2  :  rsp = http_response data structure for output
532  *          3  :  parameters = map of cgi parameters
533  *
534  * CGI Parameters : none
535  *
536  * Returns     :  JB_ERR_OK on success
537  *                JB_ERR_MEMORY on out-of-memory error.  
538  *
539  *********************************************************************/
540 jb_err cgi_error_404(struct client_state *csp,
541                      struct http_response *rsp,
542                      const struct map *parameters)
543 {
544    struct map *exports;
545
546    assert(csp);
547    assert(rsp);
548    assert(parameters);
549
550    if (NULL == (exports = default_exports(csp, NULL)))
551    {
552       return JB_ERR_MEMORY;
553    }
554
555    rsp->status = strdup("404 Privoxy configuration page not found");
556    if (rsp->status == NULL)
557    {
558       free_map(exports);
559       return JB_ERR_MEMORY;
560    }
561
562    return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
563 }
564
565
566 #ifdef FEATURE_GRACEFUL_TERMINATION
567 /*********************************************************************
568  *
569  * Function    :  cgi_die
570  *
571  * Description :  CGI function to shut down Privoxy.
572  *                NOTE: Turning this on in a production build
573  *                would be a BAD idea.  An EXTREMELY BAD idea.
574  *                In short, don't do it.
575  *               
576  * Parameters  :
577  *          1  :  csp = Current client state (buffers, headers, etc...)
578  *          2  :  rsp = http_response data structure for output
579  *          3  :  parameters = map of cgi parameters
580  *
581  * CGI Parameters : none
582  *
583  * Returns     :  JB_ERR_OK on success
584  *                JB_ERR_MEMORY on out-of-memory error.  
585  *
586  *********************************************************************/
587 jb_err cgi_die (struct client_state *csp,
588                 struct http_response *rsp,
589                 const struct map *parameters)
590 {
591    assert(csp);
592    assert(rsp);
593    assert(parameters);
594
595    /* quit */
596    g_terminate = 1;
597
598    /*
599     * I don't really care what gets sent back to the browser.
600     * Take the easy option - "out of memory" page.
601     */
602
603    return JB_ERR_MEMORY;
604 }
605 #endif /* def FEATURE_GRACEFUL_TERMINATION */
606
607
608 /*********************************************************************
609  *
610  * Function    :  cgi_show_request
611  *
612  * Description :  Show the client's request and what sed() would have
613  *                made of it.
614  *               
615  * Parameters  :
616  *          1  :  csp = Current client state (buffers, headers, etc...)
617  *          2  :  rsp = http_response data structure for output
618  *          3  :  parameters = map of cgi parameters
619  *
620  * CGI Parameters : none
621  *
622  * Returns     :  JB_ERR_OK on success
623  *                JB_ERR_MEMORY on out-of-memory error.  
624  *
625  *********************************************************************/
626 jb_err cgi_show_request(struct client_state *csp,
627                         struct http_response *rsp,
628                         const struct map *parameters)
629 {
630    char *p;
631    struct map *exports;
632
633    assert(csp);
634    assert(rsp);
635    assert(parameters);
636
637    if (NULL == (exports = default_exports(csp, "show-request")))
638    {
639       return JB_ERR_MEMORY;
640    }
641    
642    /*
643     * Repair the damage done to the IOB by get_header()
644     */
645    for (p = csp->iob->buf; p < csp->iob->eod; p++)
646    {
647       if (*p == '\0') *p = '\n';
648    }
649
650    /*
651     * Export the original client's request and the one we would
652     * be sending to the server if this wasn't a CGI call
653     */
654
655    if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
656    {
657       free_map(exports);
658       return JB_ERR_MEMORY;
659    }
660
661    if (map(exports, "processed-request", 1,
662          html_encode_and_free_original(list_to_text(csp->headers)), 0))
663    {
664       free_map(exports);
665       return JB_ERR_MEMORY;
666    }
667
668    return template_fill_for_cgi(csp, "show-request", exports, rsp);
669 }
670
671
672 /*********************************************************************
673  *
674  * Function    :  cgi_send_banner
675  *
676  * Description :  CGI function that returns a banner. 
677  *
678  * Parameters  :
679  *          1  :  csp = Current client state (buffers, headers, etc...)
680  *          2  :  rsp = http_response data structure for output
681  *          3  :  parameters = map of cgi parameters
682  *
683  * CGI Parameters :
684  *           type : Selects the type of banner between "trans", "logo",
685  *                  and "auto". Defaults to "logo" if absent or invalid.
686  *                  "auto" means to select as if we were image-blocking.
687  *                  (Only the first character really counts; b and t are
688  *                  equivalent).
689  *
690  * Returns     :  JB_ERR_OK on success
691  *                JB_ERR_MEMORY on out-of-memory error.  
692  *
693  *********************************************************************/
694 jb_err cgi_send_banner(struct client_state *csp,
695                        struct http_response *rsp,
696                        const struct map *parameters)
697 {
698    char imagetype = lookup(parameters, "type")[0];
699
700    /*
701     * If type is auto, then determine the right thing
702     * to do from the set-image-blocker action
703     */
704    if (imagetype == 'a') 
705    {
706       /*
707        * Default to pattern
708        */
709       imagetype = 'p';
710
711 #ifdef FEATURE_IMAGE_BLOCKING
712       if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
713       {
714          static const char prefix1[] = CGI_PREFIX "send-banner?type=";
715          static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
716          const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
717
718          if (p == NULL)
719          {
720             /* Use default - nothing to do here. */
721          }
722          else if (0 == strcmpic(p, "blank"))
723          {
724             imagetype = 'b';
725          }
726          else if (0 == strcmpic(p, "pattern"))
727          {
728             imagetype = 'p';
729          }
730
731          /*
732           * If the action is to call this CGI, determine
733           * the argument:
734           */
735          else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
736          {
737             imagetype = p[sizeof(prefix1) - 1];
738          }
739          else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
740          {
741             imagetype = p[sizeof(prefix2) - 1];
742          }
743
744          /*
745           * Everything else must (should) be a URL to
746           * redirect to.
747           */
748          else
749          {
750             imagetype = 'r';
751          }
752       }
753 #endif /* def FEATURE_IMAGE_BLOCKING */
754    }
755       
756    /*
757     * Now imagetype is either the non-auto type we were called with,
758     * or it was auto and has since been determined. In any case, we
759     * can proceed to actually answering the request by sending a redirect
760     * or an image as appropriate:
761     */
762    if (imagetype == 'r') 
763    {
764       rsp->status = strdup("302 Local Redirect from Privoxy");
765       if (rsp->status == NULL)
766       {
767          return JB_ERR_MEMORY;
768       }
769       if (enlist_unique_header(rsp->headers, "Location",
770                                csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
771       {
772          return JB_ERR_MEMORY;
773       }
774    }
775    else
776    {
777       if ((imagetype == 'b') || (imagetype == 't')) 
778       {
779          rsp->body = bindup(image_blank_data, image_blank_length);
780          rsp->content_length = image_blank_length;
781       }
782       else
783       {
784          rsp->body = bindup(image_pattern_data, image_pattern_length);
785          rsp->content_length = image_pattern_length;
786       }
787
788       if (rsp->body == NULL)
789       {
790          return JB_ERR_MEMORY;
791       }
792       if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
793       {
794          return JB_ERR_MEMORY;
795       }
796
797       rsp->is_static = 1;
798    }
799
800    return JB_ERR_OK;
801
802 }
803
804
805 /*********************************************************************
806  *
807  * Function    :  cgi_transparent_image
808  *
809  * Description :  CGI function that sends a 1x1 transparent image.
810  *
811  * Parameters  :
812  *          1  :  csp = Current client state (buffers, headers, etc...)
813  *          2  :  rsp = http_response data structure for output
814  *          3  :  parameters = map of cgi parameters
815  *
816  * CGI Parameters : None
817  *
818  * Returns     :  JB_ERR_OK on success
819  *                JB_ERR_MEMORY on out-of-memory error.  
820  *
821  *********************************************************************/
822 jb_err cgi_transparent_image(struct client_state *csp,
823                              struct http_response *rsp,
824                              const struct map *parameters)
825 {
826    (void)csp;
827    (void)parameters;
828
829    rsp->body = bindup(image_blank_data, image_blank_length);
830    rsp->content_length = image_blank_length;
831
832    if (rsp->body == NULL)
833    {
834       return JB_ERR_MEMORY;
835    }
836
837    if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
838    {
839       return JB_ERR_MEMORY;
840    }
841
842    rsp->is_static = 1;
843
844    return JB_ERR_OK;
845
846 }
847
848
849 /*********************************************************************
850  *
851  * Function    :  cgi_send_default_favicon
852  *
853  * Description :  CGI function that sends the standard favicon.
854  *
855  * Parameters  :
856  *          1  :  csp = Current client state (buffers, headers, etc...)
857  *          2  :  rsp = http_response data structure for output
858  *          3  :  parameters = map of cgi parameters
859  *
860  * CGI Parameters : None
861  *
862  * Returns     :  JB_ERR_OK on success
863  *                JB_ERR_MEMORY on out-of-memory error.  
864  *
865  *********************************************************************/
866 jb_err cgi_send_default_favicon(struct client_state *csp,
867                                 struct http_response *rsp,
868                                 const struct map *parameters)
869 {
870    static const char default_favicon_data[] =
871       "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
872       "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
873       "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
874       "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
875       "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
876       "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
877       "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
878       "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
879       "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
880       "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
881       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
882       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
883       "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
884       "\017\000\000";
885    static const size_t favicon_length = sizeof(default_favicon_data) - 1;
886
887    (void)csp;
888    (void)parameters;
889
890    rsp->body = bindup(default_favicon_data, favicon_length);
891    rsp->content_length = favicon_length;
892
893    if (rsp->body == NULL)
894    {
895       return JB_ERR_MEMORY;
896    }
897
898    if (enlist(rsp->headers, "Content-Type: image/x-icon"))
899    {
900       return JB_ERR_MEMORY;
901    }
902
903    rsp->is_static = 1;
904
905    return JB_ERR_OK;
906
907 }
908
909
910 /*********************************************************************
911  *
912  * Function    :  cgi_send_error_favicon
913  *
914  * Description :  CGI function that sends the favicon for error pages.
915  *
916  * Parameters  :
917  *          1  :  csp = Current client state (buffers, headers, etc...)
918  *          2  :  rsp = http_response data structure for output
919  *          3  :  parameters = map of cgi parameters
920  *
921  * CGI Parameters : None
922  *
923  * Returns     :  JB_ERR_OK on success
924  *                JB_ERR_MEMORY on out-of-memory error.  
925  *
926  *********************************************************************/
927 jb_err cgi_send_error_favicon(struct client_state *csp,
928                               struct http_response *rsp,
929                               const struct map *parameters)
930 {
931    static const char error_favicon_data[] =
932       "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
933       "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
934       "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
935       "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
936       "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
937       "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
938       "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
939       "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
940       "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
941       "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
942       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
943       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
944       "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
945       "\017\000\000";
946    static const size_t favicon_length = sizeof(error_favicon_data) - 1;
947
948    (void)csp;
949    (void)parameters;
950
951    rsp->body = bindup(error_favicon_data, favicon_length);
952    rsp->content_length = favicon_length;
953
954    if (rsp->body == NULL)
955    {
956       return JB_ERR_MEMORY;
957    }
958
959    if (enlist(rsp->headers, "Content-Type: image/x-icon"))
960    {
961       return JB_ERR_MEMORY;
962    }
963
964    rsp->is_static = 1;
965
966    return JB_ERR_OK;
967
968 }
969
970
971 /*********************************************************************
972  *
973  * Function    :  cgi_send_stylesheet
974  *
975  * Description :  CGI function that sends a css stylesheet found
976  *                in the cgi-style.css template
977  *
978  * Parameters  :
979  *          1  :  csp = Current client state (buffers, headers, etc...)
980  *          2  :  rsp = http_response data structure for output
981  *          3  :  parameters = map of cgi parameters
982  *
983  * CGI Parameters : None
984  *
985  * Returns     :  JB_ERR_OK on success
986  *                JB_ERR_MEMORY on out-of-memory error.  
987  *
988  *********************************************************************/
989 jb_err cgi_send_stylesheet(struct client_state *csp,
990                            struct http_response *rsp,
991                            const struct map *parameters)
992 {
993    jb_err err;
994    
995    assert(csp);
996    assert(rsp);
997
998    (void)parameters;
999
1000    err = template_load(csp, &rsp->body, "cgi-style.css", 0);
1001
1002    if (err == JB_ERR_FILE)
1003    {
1004       /*
1005        * No way to tell user; send empty stylesheet
1006        */
1007       log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
1008    }
1009    else if (err)
1010    {
1011       return err; /* JB_ERR_MEMORY */
1012    }
1013
1014    if (enlist(rsp->headers, "Content-Type: text/css"))
1015    {
1016       return JB_ERR_MEMORY;
1017    }
1018
1019    return JB_ERR_OK;
1020
1021 }
1022
1023
1024 /*********************************************************************
1025  *
1026  * Function    :  cgi_send_url_info_osd
1027  *
1028  * Description :  CGI function that sends the OpenSearch Description
1029  *                template for the show-url-info page. It allows to
1030  *                access the page through "search engine plugins".
1031  *
1032  * Parameters  :
1033  *          1  :  csp = Current client state (buffers, headers, etc...)
1034  *          2  :  rsp = http_response data structure for output
1035  *          3  :  parameters = map of cgi parameters
1036  *
1037  * CGI Parameters : None
1038  *
1039  * Returns     :  JB_ERR_OK on success
1040  *                JB_ERR_MEMORY on out-of-memory error.  
1041  *
1042  *********************************************************************/
1043 jb_err cgi_send_url_info_osd(struct client_state *csp,
1044                                struct http_response *rsp,
1045                                const struct map *parameters)
1046 {
1047    jb_err err = JB_ERR_MEMORY;
1048    struct map *exports = default_exports(csp, NULL);
1049
1050    (void)csp;
1051    (void)parameters;
1052
1053    if (NULL != exports)
1054    {
1055       err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1056       if (JB_ERR_OK == err)
1057       {
1058          err = enlist(rsp->headers,
1059             "Content-Type: application/opensearchdescription+xml");
1060       }
1061    }
1062
1063    return err;
1064
1065 }
1066
1067
1068 /*********************************************************************
1069  *
1070  * Function    :  cgi_send_user_manual
1071  *
1072  * Description :  CGI function that sends a file in the user
1073  *                manual directory.
1074  *
1075  * Parameters  :
1076  *          1  :  csp = Current client state (buffers, headers, etc...)
1077  *          2  :  rsp = http_response data structure for output
1078  *          3  :  parameters = map of cgi parameters
1079  *
1080  * CGI Parameters : file=name.html, the name of the HTML file
1081  *                  (relative to user-manual from config)
1082  *
1083  * Returns     :  JB_ERR_OK on success
1084  *                JB_ERR_MEMORY on out-of-memory error.  
1085  *
1086  *********************************************************************/
1087 jb_err cgi_send_user_manual(struct client_state *csp,
1088                             struct http_response *rsp,
1089                             const struct map *parameters)
1090 {
1091    const char * filename;
1092    char *full_path;
1093    jb_err err = JB_ERR_OK;
1094    size_t length;
1095
1096    assert(csp);
1097    assert(rsp);
1098    assert(parameters);
1099
1100    if (!parameters->first)
1101    {
1102       /* requested http://p.p/user-manual (without trailing slash) */
1103       return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1104    }
1105
1106    get_string_param(parameters, "file", &filename);
1107    /* Check paramter for hack attempts */
1108    if (filename && strchr(filename, '/'))
1109    {
1110       return JB_ERR_CGI_PARAMS;
1111    }
1112    if (filename && strstr(filename, ".."))
1113    {
1114       return JB_ERR_CGI_PARAMS;
1115    }
1116
1117    full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1118    if (full_path == NULL)
1119    {
1120       return JB_ERR_MEMORY;
1121    }
1122
1123    err = load_file(full_path, &rsp->body, &rsp->content_length);
1124    if (JB_ERR_OK != err)
1125    {
1126       assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1127       if (JB_ERR_FILE == err)
1128       {
1129          err = cgi_error_no_template(csp, rsp, full_path);
1130       }
1131       freez(full_path);
1132       return err;
1133    }
1134    freez(full_path);
1135
1136    /* Guess correct Content-Type based on the filename's ending */
1137    if (filename)
1138    {
1139       length = strlen(filename);
1140    }
1141    else
1142    {
1143       length = 0;
1144    } 
1145    if((length>=4) && !strcmp(&filename[length-4], ".css"))
1146    {
1147       err = enlist(rsp->headers, "Content-Type: text/css");
1148    }
1149    else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1150    {
1151       err = enlist(rsp->headers, "Content-Type: image/jpeg");
1152    }
1153    else
1154    {
1155       err = enlist(rsp->headers, "Content-Type: text/html");
1156    }
1157
1158    return err;
1159 }
1160
1161
1162 /*********************************************************************
1163  *
1164  * Function    :  cgi_show_version
1165  *
1166  * Description :  CGI function that returns a a web page describing the
1167  *                file versions of Privoxy.
1168  *
1169  * Parameters  :
1170  *          1  :  csp = Current client state (buffers, headers, etc...)
1171  *          2  :  rsp = http_response data structure for output
1172  *          3  :  parameters = map of cgi parameters
1173  *
1174  * CGI Parameters : none
1175  *
1176  * Returns     :  JB_ERR_OK on success
1177  *                JB_ERR_MEMORY on out-of-memory error.  
1178  *
1179  *********************************************************************/
1180 jb_err cgi_show_version(struct client_state *csp,
1181                         struct http_response *rsp,
1182                         const struct map *parameters)
1183 {
1184    struct map *exports;
1185
1186    assert(csp);
1187    assert(rsp);
1188    assert(parameters);
1189
1190    if (NULL == (exports = default_exports(csp, "show-version")))
1191    {
1192       return JB_ERR_MEMORY;
1193    }
1194
1195    if (map(exports, "sourceversions", 1, show_rcs(), 0))
1196    {
1197       free_map(exports);
1198       return JB_ERR_MEMORY;
1199    }
1200
1201    return template_fill_for_cgi(csp, "show-version", exports, rsp);
1202 }
1203
1204
1205 /*********************************************************************
1206  *
1207  * Function    :  cgi_show_status
1208  *
1209  * Description :  CGI function that returns a web page describing the
1210  *                current status of Privoxy.
1211  *
1212  * Parameters  :
1213  *          1  :  csp = Current client state (buffers, headers, etc...)
1214  *          2  :  rsp = http_response data structure for output
1215  *          3  :  parameters = map of cgi parameters
1216  *
1217  * CGI Parameters :
1218  *        file :  Which file to show.  Only first letter is checked,
1219  *                valid values are:
1220  *                - "a"ction file
1221  *                - "r"egex
1222  *                - "t"rust
1223  *                Default is to show menu and other information.
1224  *
1225  * Returns     :  JB_ERR_OK on success
1226  *                JB_ERR_MEMORY on out-of-memory error.  
1227  *
1228  *********************************************************************/
1229 jb_err cgi_show_status(struct client_state *csp,
1230                        struct http_response *rsp,
1231                        const struct map *parameters)
1232 {
1233    char *s = NULL;
1234    unsigned i;
1235    int j;
1236
1237    char buf[BUFFER_SIZE];
1238 #ifdef FEATURE_STATISTICS
1239    float perc_rej;   /* Percentage of http requests rejected */
1240    int local_urls_read;
1241    int local_urls_rejected;
1242 #endif /* ndef FEATURE_STATISTICS */
1243    jb_err err = JB_ERR_OK;
1244
1245    struct map *exports;
1246
1247    assert(csp);
1248    assert(rsp);
1249    assert(parameters);
1250
1251    if ('\0' != *(lookup(parameters, "file")))
1252    {
1253       return cgi_show_file(csp, rsp, parameters);
1254    }
1255
1256    if (NULL == (exports = default_exports(csp, "show-status")))
1257    {
1258       return JB_ERR_MEMORY;
1259    }
1260
1261    s = strdup("");
1262    for (j = 0; (s != NULL) && (j < Argc); j++)
1263    {
1264       if (!err) err = string_join  (&s, html_encode(Argv[j]));
1265       if (!err) err = string_append(&s, " ");
1266    }
1267    if (!err) err = map(exports, "invocation", 1, s, 0);
1268
1269    if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1270    if (!err) err = show_defines(exports);
1271
1272    if (err) 
1273    {
1274       free_map(exports);
1275       return JB_ERR_MEMORY;
1276    }
1277
1278 #ifdef FEATURE_STATISTICS
1279    local_urls_read     = urls_read;
1280    local_urls_rejected = urls_rejected;
1281
1282    /*
1283     * Need to alter the stats not to include the fetch of this
1284     * page.
1285     *
1286     * Can't do following thread safely! doh!
1287     *
1288     * urls_read--;
1289     * urls_rejected--; * This will be incremented subsequently *
1290     */
1291
1292    if (local_urls_read == 0)
1293    {
1294       if (!err) err = map_block_killer(exports, "have-stats");
1295    }
1296    else
1297    {
1298       if (!err) err = map_block_killer(exports, "have-no-stats");
1299
1300       perc_rej = (float)local_urls_rejected * 100.0F /
1301             (float)local_urls_read;
1302
1303       snprintf(buf, sizeof(buf), "%d", local_urls_read);
1304       if (!err) err = map(exports, "requests-received", 1, buf, 1);
1305
1306       snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1307       if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1308
1309       snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1310       if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1311    }
1312
1313 #else /* ndef FEATURE_STATISTICS */
1314    err = err || map_block_killer(exports, "statistics");
1315 #endif /* ndef FEATURE_STATISTICS */
1316    
1317    /* 
1318     * List all action files in use, together with view and edit links,
1319     * except for standard.action, which should only be viewable. (Not
1320     * enforced in the editor itself)
1321     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1322     */
1323    s = strdup("");
1324    for (i = 0; i < MAX_AF_FILES; i++)
1325    {
1326       if (csp->actions_list[i] != NULL)
1327       {
1328          if (!err) err = string_append(&s, "<tr><td>");
1329          if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1330          snprintf(buf, sizeof(buf),
1331             "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&amp;index=%u\">View</a>", i);
1332          if (!err) err = string_append(&s, buf);
1333
1334 #ifdef FEATURE_CGI_EDIT_ACTIONS
1335          if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1336             && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
1337             && (NULL != csp->config->actions_file_short[i]))
1338          {
1339 #ifdef HAVE_ACCESS
1340             if (access(csp->config->actions_file[i], W_OK) == 0)
1341             {
1342 #endif /* def HAVE_ACCESS */
1343                snprintf(buf, sizeof(buf), "&nbsp;&nbsp;<a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1344                if (!err) err = string_append(&s, buf);
1345 #ifdef HAVE_ACCESS
1346             }
1347             else
1348             {
1349                if (!err) err = string_append(&s, "&nbsp;&nbsp;<strong>No write access.</strong>");
1350             }
1351 #endif /* def HAVE_ACCESS */
1352          }
1353 #endif
1354
1355          if (!err) err = string_append(&s, "</td></tr>\n");
1356       }
1357    }
1358    if (*s != '\0')   
1359    {
1360       if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1361    }
1362    else
1363    {
1364       if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1365    }
1366
1367    /* 
1368     * List all re_filterfiles in use, together with view options.
1369     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1370     */
1371    s = strdup("");
1372    for (i = 0; i < MAX_AF_FILES; i++)
1373    {
1374       if (csp->rlist[i] != NULL)
1375       {
1376          if (!err) err = string_append(&s, "<tr><td>");
1377          if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1378          snprintf(buf, sizeof(buf),
1379             "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&amp;index=%u\">View</a>", i);
1380          if (!err) err = string_append(&s, buf);
1381          if (!err) err = string_append(&s, "</td></tr>\n");
1382       }
1383    }
1384    if (*s != '\0')   
1385    {
1386       if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1387    }
1388    else
1389    {
1390       if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1391       if (!err) err = map_block_killer(exports, "have-filterfile");
1392    }
1393
1394 #ifdef FEATURE_TRUST
1395    if (csp->tlist)
1396    {
1397       if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1398    }
1399    else
1400    {
1401       if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1402       if (!err) err = map_block_killer(exports, "have-trustfile");
1403    }
1404 #else
1405    if (!err) err = map_block_killer(exports, "trust-support");
1406 #endif /* ndef FEATURE_TRUST */
1407
1408 #ifdef FEATURE_CGI_EDIT_ACTIONS
1409    if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1410    {
1411       err = map_block_killer(exports, "cgi-editor-is-disabled");
1412    }
1413 #endif /* ndef CGI_EDIT_ACTIONS */
1414
1415    if (err)
1416    {
1417       free_map(exports);
1418       return JB_ERR_MEMORY;
1419    }
1420
1421    return template_fill_for_cgi(csp, "show-status", exports, rsp);
1422 }
1423
1424  
1425 /*********************************************************************
1426  *
1427  * Function    :  cgi_show_url_info
1428  *
1429  * Description :  CGI function that determines and shows which actions
1430  *                Privoxy will perform for a given url, and which
1431  *                matches starting from the defaults have lead to that.
1432  *
1433  * Parameters  :
1434  *          1  :  csp = Current client state (buffers, headers, etc...)
1435  *          2  :  rsp = http_response data structure for output
1436  *          3  :  parameters = map of cgi parameters
1437  *
1438  * CGI Parameters :
1439  *            url : The url whose actions are to be determined.
1440  *                  If url is unset, the url-given conditional will be
1441  *                  set, so that all but the form can be suppressed in
1442  *                  the template.
1443  *
1444  * Returns     :  JB_ERR_OK on success
1445  *                JB_ERR_MEMORY on out-of-memory error.  
1446  *
1447  *********************************************************************/
1448 jb_err cgi_show_url_info(struct client_state *csp,
1449                          struct http_response *rsp,
1450                          const struct map *parameters)
1451 {
1452    char *url_param;
1453    struct map *exports;
1454    char buf[150];
1455
1456    assert(csp);
1457    assert(rsp);
1458    assert(parameters);
1459
1460    if (NULL == (exports = default_exports(csp, "show-url-info")))
1461    {
1462       return JB_ERR_MEMORY;
1463    }
1464
1465    /*
1466     * Get the url= parameter (if present) and remove any leading/trailing spaces.
1467     */
1468    url_param = strdup(lookup(parameters, "url"));
1469    if (url_param == NULL)
1470    {
1471       free_map(exports);
1472       return JB_ERR_MEMORY;
1473    }
1474    chomp(url_param);
1475
1476    /*
1477     * Handle prefixes.  4 possibilities:
1478     * 1) "http://" or "https://" prefix present and followed by URL - OK
1479     * 2) Only the "http://" or "https://" part is present, no URL - change
1480     *    to empty string so it will be detected later as "no URL".
1481     * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1482     *    "http://" prefix.
1483     * 4) Parameter not specified or is empty string - let this fall through
1484     *    for now, next block of code will handle it.
1485     */
1486    if (0 == strncmp(url_param, "http://", 7))
1487    {
1488       if (url_param[7] == '\0')
1489       {
1490          /*
1491           * Empty URL (just prefix).
1492           * Make it totally empty so it's caught by the next if()
1493           */
1494          url_param[0] = '\0';
1495       }
1496    }
1497    else if (0 == strncmp(url_param, "https://", 8))
1498    {
1499       if (url_param[8] == '\0')
1500       {
1501          /*
1502           * Empty URL (just prefix).
1503           * Make it totally empty so it's caught by the next if()
1504           */
1505          url_param[0] = '\0';
1506       }
1507    }
1508    else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1509    {
1510       /* No prefix - assume http:// */
1511       char *url_param_prefixed = strdup("http://");
1512
1513       if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1514       {
1515          free_map(exports);
1516          return JB_ERR_MEMORY;
1517       }
1518       url_param = url_param_prefixed;
1519    }
1520
1521    /*
1522     * Hide "toggle off" warning if Privoxy is toggled on.
1523     */
1524    if (
1525 #ifdef FEATURE_TOGGLE
1526        (global_toggle_state == 1) &&
1527 #endif /* def FEATURE_TOGGLE */
1528        map_block_killer(exports, "privoxy-is-toggled-off")
1529       )
1530    {
1531       free_map(exports);
1532       return JB_ERR_MEMORY;
1533    }
1534
1535    if (url_param[0] == '\0')
1536    {
1537       /* URL paramater not specified, display query form only. */
1538       free(url_param);
1539       if (map_block_killer(exports, "url-given")
1540         || map(exports, "url", 1, "", 1))
1541       {
1542          free_map(exports);
1543          return JB_ERR_MEMORY;
1544       }
1545    }
1546    else
1547    {
1548       /* Given a URL, so query it. */
1549       jb_err err;
1550       char *matches;
1551       char *s;
1552       int hits = 0;
1553       struct file_list *fl;
1554       struct url_actions *b;
1555       struct http_request url_to_query[1];
1556       struct current_action_spec action[1];
1557       int i;
1558       
1559       if (map(exports, "url", 1, html_encode(url_param), 0))
1560       {
1561          free(url_param);
1562          free_map(exports);
1563          return JB_ERR_MEMORY;
1564       }
1565
1566       init_current_action(action);
1567
1568       if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1569       {
1570          free_current_action(action);
1571          free(url_param);
1572          free_map(exports);
1573          return JB_ERR_MEMORY;
1574       }
1575
1576       memset(url_to_query, '\0', sizeof(url_to_query));
1577       err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1578       assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1579
1580       free(url_param);
1581
1582       if (err == JB_ERR_MEMORY)
1583       {
1584          free_http_request(url_to_query);
1585          free_current_action(action);
1586          free_map(exports);
1587          return JB_ERR_MEMORY;
1588       }
1589       else if (err)
1590       {
1591          /* Invalid URL */
1592
1593          err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1594          if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1595          if (!err) err = map_block_killer(exports, "valid-url");
1596
1597          free_current_action(action);
1598          free_http_request(url_to_query);
1599
1600          if (err)
1601          {
1602             free_map(exports);
1603             return JB_ERR_MEMORY;
1604          }
1605
1606          return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1607       }
1608
1609       /*
1610        * We have a warning about SSL paths.  Hide it for unencrypted sites.
1611        */
1612       if (!url_to_query->ssl)
1613       {
1614          if (map_block_killer(exports, "https"))
1615          {
1616             free_current_action(action);
1617             free_map(exports);
1618             free_http_request(url_to_query);
1619             return JB_ERR_MEMORY;
1620          }
1621       }
1622
1623       matches = strdup("<table summary=\"\" class=\"transparent\">");
1624
1625       for (i = 0; i < MAX_AF_FILES; i++)
1626       {
1627          if (NULL == csp->config->actions_file_short[i]
1628              || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1629
1630          b = NULL;
1631          hits = 1;
1632          if ((fl = csp->actions_list[i]) != NULL)
1633          {
1634             if ((b = fl->f) != NULL)
1635             {
1636                /* FIXME: Hardcoded HTML! */
1637                string_append(&matches, "<tr><th>In file: ");
1638                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1639                snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&amp;index=%d\">", i);
1640                string_append(&matches, buf);
1641                string_append(&matches, "View</a>");
1642 #ifdef FEATURE_CGI_EDIT_ACTIONS
1643                if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1644                {
1645 #ifdef HAVE_ACCESS
1646                   if (access(csp->config->actions_file[i], W_OK) == 0)
1647                   {
1648 #endif /* def HAVE_ACCESS */
1649                      snprintf(buf, sizeof(buf),
1650                         " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1651                      string_append(&matches, buf);
1652                      string_append(&matches, "Edit</a>");
1653 #ifdef HAVE_ACCESS
1654                   }
1655                   else
1656                   {
1657                      string_append(&matches, " <strong>No write access.</strong>");
1658                   }
1659 #endif /* def HAVE_ACCESS */
1660                }
1661 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1662
1663                string_append(&matches, "</th></tr>\n");
1664
1665                hits = 0;
1666                b = b->next;
1667             }
1668          }
1669
1670          for (; (b != NULL) && (matches != NULL); b = b->next)
1671          {
1672             if (url_match(b->url, url_to_query))
1673             {
1674                string_append(&matches, "<tr><td>{");
1675                string_join  (&matches, actions_to_html(csp, b->action));
1676                string_append(&matches, " }<br>\n<code>");
1677                string_join  (&matches, html_encode(b->url->spec));
1678                string_append(&matches, "</code></td></tr>\n");
1679
1680                if (merge_current_action(action, b->action))
1681                {
1682                   freez(matches);
1683                   free_http_request(url_to_query);
1684                   free_current_action(action);
1685                   free_map(exports);
1686                   return JB_ERR_MEMORY;
1687                }
1688                hits++;
1689             }
1690          }
1691
1692          if (!hits)
1693          {
1694             string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1695          }
1696       }
1697       string_append(&matches, "</table>\n");
1698
1699       /*
1700        * XXX: Kludge to make sure the "Forward settings" section
1701        * shows what forward-override{} would do with the requested URL.
1702        * No one really cares how the CGI request would be forwarded
1703        * if it wasn't intercepted as CGI request in the first place.
1704        *
1705        * From here on the action bitmask will no longer reflect
1706        * the real url (http://config.privoxy.org/show-url-info?url=.*),
1707        * but luckily it's no longer required later on anyway.
1708        */
1709       free_current_action(csp->action);
1710       get_url_actions(csp, url_to_query);
1711
1712       /*
1713        * Fill in forwarding settings.
1714        *
1715        * The possibilities are:
1716        *  - no forwarding
1717        *  - http forwarding only
1718        *  - socks4(a) forwarding only
1719        *  - socks4(a) and http forwarding.
1720        *
1721        * XXX: Parts of this code could be reused for the
1722        * "forwarding-failed" template which currently doesn't
1723        * display the proxy port and an eventual second forwarder.
1724        */
1725       {
1726          const struct forward_spec *fwd = forward_url(csp, url_to_query);
1727
1728          if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1729          {
1730             if (!err) err = map_block_killer(exports, "socks-forwarder");
1731             if (!err) err = map_block_killer(exports, "http-forwarder");
1732          }
1733          else
1734          {
1735             char port[10]; /* We save proxy ports as int but need a string here */
1736
1737             if (!err) err = map_block_killer(exports, "no-forwarder");
1738
1739             if (fwd->gateway_host != NULL)
1740             {
1741                char *socks_type = NULL;
1742
1743                switch (fwd->type)
1744                {
1745                   case SOCKS_4:
1746                      socks_type = "socks4";
1747                      break;
1748                   case SOCKS_4A:
1749                      socks_type = "socks4a";
1750                      break;
1751                   case SOCKS_5:
1752                      socks_type = "socks5";
1753                      break;
1754                   default:
1755                      log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1756                }
1757
1758                if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1759                if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1760                snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1761                if (!err) err = map(exports, "gateway-port", 1, port, 1);
1762             }
1763             else
1764             {
1765                if (!err) err = map_block_killer(exports, "socks-forwarder");
1766             }
1767
1768             if (fwd->forward_host != NULL)
1769             {
1770                if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1771                snprintf(port, sizeof(port), "%d", fwd->forward_port);
1772                if (!err) err = map(exports, "forward-port", 1, port, 1);
1773             }
1774             else
1775             {
1776                if (!err) err = map_block_killer(exports, "http-forwarder");
1777             }
1778          }
1779       }
1780
1781       free_http_request(url_to_query);
1782
1783       if (err || matches == NULL)
1784       {
1785          free_current_action(action);
1786          free_map(exports);
1787          return JB_ERR_MEMORY;
1788       }
1789
1790 #ifdef FEATURE_CGI_EDIT_ACTIONS
1791       if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1792       {
1793          err = map_block_killer(exports, "cgi-editor-is-disabled");
1794       }
1795 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1796
1797       /*
1798        * If zlib support is available, if no content filters
1799        * are enabled or if the prevent-compression action is enabled,
1800        * suppress the "compression could prevent filtering" warning.
1801        */
1802 #ifndef FEATURE_ZLIB
1803       if (!content_filters_enabled(action) ||
1804          (action->flags & ACTION_NO_COMPRESSION))
1805 #endif
1806       {
1807          if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1808       }
1809
1810       if (err || map(exports, "matches", 1, matches , 0))
1811       {
1812          free_current_action(action);
1813          free_map(exports);
1814          return JB_ERR_MEMORY;
1815       }
1816
1817       s = current_action_to_html(csp, action);
1818
1819       free_current_action(action);
1820
1821       if (map(exports, "final", 1, s, 0))
1822       {
1823          free_map(exports);
1824          return JB_ERR_MEMORY;
1825       }
1826    }
1827
1828    return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1829 }
1830
1831
1832 /*********************************************************************
1833  *
1834  * Function    :  cgi_robots_txt
1835  *
1836  * Description :  CGI function to return "/robots.txt".
1837  *
1838  * Parameters  :
1839  *          1  :  csp = Current client state (buffers, headers, etc...)
1840  *          2  :  rsp = http_response data structure for output
1841  *          3  :  parameters = map of cgi parameters
1842  *
1843  * CGI Parameters : None
1844  *
1845  * Returns     :  JB_ERR_OK on success
1846  *                JB_ERR_MEMORY on out-of-memory error.  
1847  *
1848  *********************************************************************/
1849 jb_err cgi_robots_txt(struct client_state *csp,
1850                       struct http_response *rsp,
1851                       const struct map *parameters)
1852 {
1853    char buf[100];
1854    jb_err err;
1855
1856    (void)csp;
1857    (void)parameters;
1858
1859    rsp->body = strdup(
1860       "# This is the Privoxy control interface.\n"
1861       "# It isn't very useful to index it, and you're likely to break stuff.\n"
1862       "# So go away!\n"
1863       "\n"
1864       "User-agent: *\n"
1865       "Disallow: /\n"
1866       "\n");
1867    if (rsp->body == NULL)
1868    {
1869       return JB_ERR_MEMORY;
1870    }
1871
1872    err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1873
1874    rsp->is_static = 1;
1875
1876    get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1877    if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1878
1879    return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1880 }
1881
1882
1883 /*********************************************************************
1884  *
1885  * Function    :  show_defines
1886  *
1887  * Description :  Add to a map the state od all conditional #defines
1888  *                used when building
1889  *
1890  * Parameters  :
1891  *          1  :  exports = map to extend
1892  *
1893  * Returns     :  JB_ERR_OK on success
1894  *                JB_ERR_MEMORY on out-of-memory error.  
1895  *
1896  *********************************************************************/
1897 static jb_err show_defines(struct map *exports)
1898 {
1899    jb_err err = JB_ERR_OK;
1900
1901 #ifdef FEATURE_ACL
1902    if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1903 #else /* ifndef FEATURE_ACL */
1904    if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1905 #endif /* ndef FEATURE_ACL */
1906
1907 #ifdef FEATURE_CGI_EDIT_ACTIONS
1908    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1909 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1910    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1911 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1912
1913 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1914    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1915 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1916    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1917 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1918
1919 #ifdef FEATURE_FAST_REDIRECTS
1920    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1921 #else /* ifndef FEATURE_FAST_REDIRECTS */
1922    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1923 #endif /* ndef FEATURE_FAST_REDIRECTS */
1924
1925 #ifdef FEATURE_FORCE_LOAD
1926    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1927    if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1928 #else /* ifndef FEATURE_FORCE_LOAD */
1929    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1930    if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1931 #endif /* ndef FEATURE_FORCE_LOAD */
1932
1933 #ifdef FEATURE_GRACEFUL_TERMINATION
1934    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1935 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1936    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1937 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1938
1939 #ifdef FEATURE_IMAGE_BLOCKING
1940    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1941 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1942    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1943 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1944
1945 #ifdef FEATURE_IMAGE_DETECT_MSIE
1946    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1947 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1948    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1949 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1950
1951 #ifdef HAVE_RFC2553
1952    if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1953 #else /* ifndef HAVE_RFC2553 */
1954    if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1955 #endif /* ndef HAVE_RFC2553 */
1956
1957 #ifdef FEATURE_NO_GIFS
1958    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1959 #else /* ifndef FEATURE_NO_GIFS */
1960    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1961 #endif /* ndef FEATURE_NO_GIFS */
1962
1963 #ifdef FEATURE_PTHREAD
1964    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1965 #else /* ifndef FEATURE_PTHREAD */
1966    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1967 #endif /* ndef FEATURE_PTHREAD */
1968
1969 #ifdef FEATURE_STATISTICS
1970    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1971 #else /* ifndef FEATURE_STATISTICS */
1972    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1973 #endif /* ndef FEATURE_STATISTICS */
1974
1975 #ifdef FEATURE_TOGGLE
1976    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1977 #else /* ifndef FEATURE_TOGGLE */
1978    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1979 #endif /* ndef FEATURE_TOGGLE */
1980
1981 #ifdef FEATURE_TRUST
1982    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1983 #else /* ifndef FEATURE_TRUST */
1984    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1985 #endif /* ndef FEATURE_TRUST */
1986
1987 #ifdef FEATURE_ZLIB
1988    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1989 #else /* ifndef FEATURE_ZLIB */
1990    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1991 #endif /* ndef FEATURE_ZLIB */
1992
1993 #ifdef STATIC_PCRE
1994    if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1995 #else /* ifndef STATIC_PCRE */
1996    if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1997 #endif /* ndef STATIC_PCRE */
1998
1999 #ifdef STATIC_PCRS
2000    if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
2001 #else /* ifndef STATIC_PCRS */
2002    if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
2003 #endif /* ndef STATIC_PCRS */
2004
2005    return err;
2006 }
2007
2008
2009 /*********************************************************************
2010  *
2011  * Function    :  show_rcs
2012  *
2013  * Description :  Create a string with the rcs info for all sourcefiles
2014  *
2015  * Parameters  :  None
2016  *
2017  * Returns     :  A string, or NULL on out-of-memory.
2018  *
2019  *********************************************************************/
2020 static char *show_rcs(void)
2021 {
2022    char *result = strdup("");
2023    char buf[BUFFER_SIZE];
2024
2025    /* Instead of including *all* dot h's in the project (thus creating a
2026     * tremendous amount of dependencies), I will concede to declaring them
2027     * as extern's.  This forces the developer to add to this list, but oh well.
2028     */
2029
2030 #define SHOW_RCS(__x)              \
2031    {                               \
2032       extern const char __x[];     \
2033       snprintf(buf, sizeof(buf), " %s\n", __x);   \
2034       string_append(&result, buf); \
2035    }
2036
2037    /* In alphabetical order */
2038    SHOW_RCS(actions_h_rcs)
2039    SHOW_RCS(actions_rcs)
2040 #ifdef AMIGA
2041    SHOW_RCS(amiga_h_rcs)
2042    SHOW_RCS(amiga_rcs)
2043 #endif /* def AMIGA */
2044    SHOW_RCS(cgi_h_rcs)
2045    SHOW_RCS(cgi_rcs)
2046 #ifdef FEATURE_CGI_EDIT_ACTIONS
2047    SHOW_RCS(cgiedit_h_rcs)
2048    SHOW_RCS(cgiedit_rcs)
2049 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
2050    SHOW_RCS(cgisimple_h_rcs)
2051    SHOW_RCS(cgisimple_rcs)
2052 #ifdef __MINGW32__
2053    SHOW_RCS(cygwin_h_rcs)
2054 #endif
2055    SHOW_RCS(deanimate_h_rcs)
2056    SHOW_RCS(deanimate_rcs)
2057    SHOW_RCS(encode_h_rcs)
2058    SHOW_RCS(encode_rcs)
2059    SHOW_RCS(errlog_h_rcs)
2060    SHOW_RCS(errlog_rcs)
2061    SHOW_RCS(filters_h_rcs)
2062    SHOW_RCS(filters_rcs)
2063    SHOW_RCS(gateway_h_rcs)
2064    SHOW_RCS(gateway_rcs)
2065    SHOW_RCS(jbsockets_h_rcs)
2066    SHOW_RCS(jbsockets_rcs)
2067    SHOW_RCS(jcc_h_rcs)
2068    SHOW_RCS(jcc_rcs)
2069    SHOW_RCS(list_h_rcs)
2070    SHOW_RCS(list_rcs)
2071    SHOW_RCS(loadcfg_h_rcs)
2072    SHOW_RCS(loadcfg_rcs)
2073    SHOW_RCS(loaders_h_rcs)
2074    SHOW_RCS(loaders_rcs)
2075    SHOW_RCS(miscutil_h_rcs)
2076    SHOW_RCS(miscutil_rcs)
2077    SHOW_RCS(parsers_h_rcs)
2078    SHOW_RCS(parsers_rcs)
2079    SHOW_RCS(pcrs_rcs)
2080    SHOW_RCS(pcrs_h_rcs)
2081    SHOW_RCS(project_h_rcs)
2082    SHOW_RCS(ssplit_h_rcs)
2083    SHOW_RCS(ssplit_rcs)
2084    SHOW_RCS(urlmatch_h_rcs)
2085    SHOW_RCS(urlmatch_rcs)
2086 #ifdef _WIN32
2087 #ifndef _WIN_CONSOLE
2088    SHOW_RCS(w32log_h_rcs)
2089    SHOW_RCS(w32log_rcs)
2090    SHOW_RCS(w32res_h_rcs)
2091    SHOW_RCS(w32taskbar_h_rcs)
2092    SHOW_RCS(w32taskbar_rcs)
2093 #endif /* ndef _WIN_CONSOLE */
2094    SHOW_RCS(win32_h_rcs)
2095    SHOW_RCS(win32_rcs)
2096 #endif /* def _WIN32 */
2097
2098 #undef SHOW_RCS
2099
2100    return result;
2101
2102 }
2103
2104
2105 /*********************************************************************
2106  *
2107  * Function    :  cgi_show_file
2108  *
2109  * Description :  CGI function that shows the content of a
2110  *                configuration file.
2111  *
2112  * Parameters  :
2113  *          1  :  csp = Current client state (buffers, headers, etc...)
2114  *          2  :  rsp = http_response data structure for output
2115  *          3  :  parameters = map of cgi parameters
2116  *
2117  * CGI Parameters :
2118  *        file :  Which file to show.  Only first letter is checked,
2119  *                valid values are:
2120  *                - "a"ction file
2121  *                - "r"egex
2122  *                - "t"rust
2123  *                Default is to show menu and other information.
2124  *
2125  * Returns     :  JB_ERR_OK on success
2126  *                JB_ERR_MEMORY on out-of-memory error.  
2127  *
2128  *********************************************************************/
2129 static jb_err cgi_show_file(struct client_state *csp,
2130                             struct http_response *rsp,
2131                             const struct map *parameters)
2132 {
2133    unsigned i;
2134    const char * filename = NULL;
2135    char * file_description = NULL;
2136
2137    assert(csp);
2138    assert(rsp);
2139    assert(parameters);
2140
2141    switch (*(lookup(parameters, "file")))
2142    {
2143    case 'a':
2144       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2145       {
2146          filename = csp->actions_list[i]->filename;
2147          file_description = "Actions File";
2148       }
2149       break;
2150
2151    case 'f':
2152       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2153       {
2154          filename = csp->rlist[i]->filename;
2155          file_description = "Filter File";
2156       }
2157       break;
2158
2159 #ifdef FEATURE_TRUST
2160    case 't':
2161       if (csp->tlist)
2162       {
2163          filename = csp->tlist->filename;
2164          file_description = "Trust File";
2165       }
2166       break;
2167 #endif /* def FEATURE_TRUST */
2168    }
2169
2170    if (NULL != filename)
2171    {
2172       struct map *exports;
2173       char *s;
2174       jb_err err;
2175       size_t length;
2176
2177       exports = default_exports(csp, "show-status");
2178       if (NULL == exports)
2179       {
2180          return JB_ERR_MEMORY;
2181       }
2182
2183       if ( map(exports, "file-description", 1, file_description, 1)
2184         || map(exports, "filepath", 1, html_encode(filename), 0) )
2185       {
2186          free_map(exports);
2187          return JB_ERR_MEMORY;
2188       }
2189
2190       err = load_file(filename, &s, &length);
2191       if (JB_ERR_OK != err)
2192       {
2193          if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2194          {
2195             free_map(exports);
2196             return JB_ERR_MEMORY;
2197          }
2198       }
2199       else
2200       {
2201          s = html_encode_and_free_original(s);
2202          if (NULL == s)
2203          {
2204             return JB_ERR_MEMORY;
2205          }
2206
2207          if (map(exports, "contents", 1, s, 0))
2208          {
2209             free_map(exports);
2210             return JB_ERR_MEMORY;
2211          }
2212       }
2213
2214       return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2215    }
2216
2217    return JB_ERR_CGI_PARAMS;
2218 }
2219
2220  
2221 /*********************************************************************
2222  *
2223  * Function    :  load_file
2224  *
2225  * Description :  Loads a file into a buffer.
2226  *
2227  * Parameters  :
2228  *          1  :  filename = Name of the file to be loaded.
2229  *          2  :  buffer   = Used to return the file's content.
2230  *          3  :  length   = Used to return the size of the file.
2231  *
2232  * Returns     :  JB_ERR_OK in case of success,
2233  *                JB_ERR_FILE in case of ordinary file loading errors
2234  *                            (fseek() and ftell() errors are fatal)
2235  *                JB_ERR_MEMORY in case of out-of-memory.
2236  *
2237  *********************************************************************/
2238 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2239 {
2240    FILE *fp;
2241    long ret;
2242    jb_err err = JB_ERR_OK;
2243
2244    fp = fopen(filename, "rb");
2245    if (NULL == fp)
2246    {
2247       return JB_ERR_FILE;
2248    }
2249
2250    /* Get file length */
2251    if (fseek(fp, 0, SEEK_END))
2252    {
2253       log_error(LOG_LEVEL_FATAL,
2254          "Unexpected error while fseek()ing to the end of %s: %E",
2255          filename);
2256    }
2257    ret = ftell(fp);
2258    if (-1 == ret)
2259    {
2260       log_error(LOG_LEVEL_FATAL,
2261          "Unexpected ftell() error while loading %s: %E",
2262          filename);
2263    }
2264    *length = (size_t)ret;
2265
2266    /* Go back to the beginning. */
2267    if (fseek(fp, 0, SEEK_SET))
2268    {
2269       log_error(LOG_LEVEL_FATAL,
2270          "Unexpected error while fseek()ing to the beginning of %s: %E",
2271          filename);
2272    }
2273
2274    *buffer = (char *)zalloc(*length + 1);
2275    if (NULL == *buffer)
2276    {
2277       err = JB_ERR_MEMORY;
2278    }
2279    else if (!fread(*buffer, *length, 1, fp))
2280    {
2281       /*
2282        * May happen if the file size changes between fseek() and
2283        * fread(). If it does, we just log it and serve what we got.
2284        */
2285       log_error(LOG_LEVEL_ERROR,
2286          "Couldn't completely read file %s.", filename);
2287       err = JB_ERR_FILE;
2288    }
2289
2290    fclose(fp);
2291
2292    return err;
2293
2294 }
2295
2296
2297 /*
2298   Local Variables:
2299   tab-width: 3
2300   end:
2301 */