merge Debian changes from 3.0.10 to 3.0.12.
[privoxy.git] / cgisimple.c
1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.90 2009/03/01 18:43:09 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.90  2009/03/01 18:43:09  fabiankeil
40  *    Fix cparser warnings.
41  *
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.
45  *
46  *    Revision 1.88  2008/08/30 12:03:07  fabiankeil
47  *    Remove FEATURE_COOKIE_JAR.
48  *
49  *    Revision 1.87  2008/08/29 15:59:22  fabiankeil
50  *    Fix two comments.
51  *
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.
55  *
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".
59  *
60  *    Revision 1.84  2008/05/26 16:16:55  fabiankeil
61  *    Spell error correctly.
62  *
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.
66  *
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.
70  *
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.
75  *
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.
79  *
80  *    Revision 1.79  2008/05/04 13:30:56  fabiankeil
81  *    Streamline parse_http_url()'s prototype.
82  *
83  *    Revision 1.78  2008/05/03 16:50:11  fabiankeil
84  *    Leverage content_filters_enabled() in cgi_show_url_info().
85  *
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.
90  *
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.
94  *
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.
98  *
99  *    Revision 1.74  2008/04/26 15:50:56  fabiankeil
100  *    Fix macro name in cgi_show_file() error path.
101  *
102  *    Revision 1.73  2008/04/26 12:21:55  fabiankeil
103  *    Forget about JB_ERR_PARSE. JB_ERR_CGI_PARAMS to the rescue.
104  *
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.
109  *
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.
113  *
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.
117  *
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.
121  *
122  *    Revision 1.68  2008/03/27 18:27:21  fabiankeil
123  *    Remove kill-popups action.
124  *
125  *    Revision 1.67  2008/03/27 17:00:05  fabiankeil
126  *    Turn the favicon blobs into locals.
127  *
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.
131  *
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.
135  *
136  *    Revision 1.64  2008/02/03 13:56:07  fabiankeil
137  *    Add SOCKS5 support for show-url-info CGI page.
138  *
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.
141  *
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.
145  *
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.
148  *
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.
152  *
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.
156  *
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.
161  *
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
166  *    CGI request).
167  *
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.
172  *      Closes FR#588084.
173  *    - Don't rerun sed() in cgi_show_request().
174  *
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.
179  *
180  *    Revision 1.54  2007/04/09 18:11:35  fabiankeil
181  *    Don't mistake VC++'s _snprintf() for a snprintf() replacement.
182  *
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.
187  *
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.
191  *
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.
195  *
196  *    Revision 1.50  2007/01/23 15:51:17  fabiankeil
197  *    Add favicon delivery functions.
198  *
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.
202  *
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.
206  *
207  *    Revision 1.47  2007/01/12 15:07:10  fabiankeil
208  *    Use zalloc in cgi_send_user_manual.
209  *
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.
213  *
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.
217  *
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.
222  *
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.
226  *
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
232  *
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.
237  *
238  *    Remove outdated comment.
239  *
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.
243  *
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.
248  *
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
252  *    ends with ".css".
253  *
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.
257  *
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.
262  *
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)
266  *
267  *    Revision 1.35.2.7  2006/01/29 23:10:56  david__schmidt
268  *    Multiple filter file support
269  *
270  *    Revision 1.35.2.6  2005/07/04 03:13:43  david__schmidt
271  *    Undo some damaging memory leak patches
272  *
273  *    Revision 1.35.2.5  2005/05/07 21:50:55  david__schmidt
274  *    A few memory leaks plugged (mostly on error paths)
275  *
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
280  *
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
284  *
285  *    Revision 1.35.2.2  2003/04/03 13:48:28  oes
286  *    Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
287  *
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
290  *
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>
294  *
295  *    Revision 1.35  2002/05/12 21:44:44  jongfoster
296  *    Adding amiga.[ch] revision information, if on an amiga.
297  *
298  *    Revision 1.34  2002/04/30 12:06:12  oes
299  *    Deleted unused code from default_cgi
300  *
301  *    Revision 1.33  2002/04/30 11:14:52  oes
302  *    Made csp the first parameter in *action_to_html
303  *
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
307  *
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,
314  *         linked to help
315  *       - standard.action suppressed
316  *       - Buttons to View and Edit AFs
317  *
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.
325  *
326  *     - Jon's multiple AF patch: cgi_show_url_info now uses all
327  *       AFs and marks the output accordingly
328  *
329  *    Revision 1.29  2002/04/10 13:38:35  oes
330  *    load_template signature changed
331  *
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
335  *
336  *    Revision 1.27  2002/04/05 15:50:48  oes
337  *    added send-stylesheet CGI
338  *
339  *    Revision 1.26  2002/04/04 00:36:36  gliptak
340  *    always use pcre for matching
341  *
342  *    Revision 1.25  2002/04/03 22:28:03  gliptak
343  *    Removed references to gnu_regex
344  *
345  *    Revision 1.24  2002/04/02 16:12:47  oes
346  *    Fix: moving misplaced lines into #ifdef FEATURE_FORCE
347  *
348  *    Revision 1.23  2002/03/26 22:29:54  swa
349  *    we have a new homepage!
350  *
351  *    Revision 1.22  2002/03/24 16:18:15  jongfoster
352  *    Removing old logo
353  *
354  *    Revision 1.21  2002/03/24 15:23:33  jongfoster
355  *    Name changes
356  *
357  *    Revision 1.20  2002/03/24 13:25:43  swa
358  *    name change related issues
359  *
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.
366  *
367  *    Revision 1.18  2002/03/12 01:44:49  oes
368  *    Changed default for "blocked" image from jb logo to checkboard pattern
369  *
370  *    Revision 1.17  2002/03/08 16:43:18  oes
371  *    Added choice beween GIF and PNG built-in images
372  *
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
378  *
379  *    Revision 1.15  2002/03/06 22:54:35  jongfoster
380  *    Automated function-comment nitpicking.
381  *
382  *    Revision 1.14  2002/03/02 04:14:50  david__schmidt
383  *    Clean up a little CRLF unpleasantness that suddenly appeared
384  *
385  *    Revision 1.13  2002/02/21 00:10:37  jongfoster
386  *    Adding send-banner?type=auto option
387  *
388  *    Revision 1.12  2002/01/23 01:03:32  jongfoster
389  *    Fixing gcc [CygWin] compiler warnings
390  *
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
395  *
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.
400  *
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
404  *
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.)
408  *
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).
413  *
414  *    CGI actions file editor that works and is actually useful.
415  *
416  *    Ability to toggle JunkBuster remotely using a CGI call.
417  *
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.
420  *
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.
423  *
424  *    Revision 1.5  2001/10/07 15:30:41  oes
425  *    Removed FEATURE_DENY_GZIP
426  *
427  *    Revision 1.4  2001/10/02 15:31:12  oes
428  *    Introduced show-request cgi
429  *
430  *    Revision 1.3  2001/09/22 16:34:44  jongfoster
431  *    Removing unneeded #includes
432  *
433  *    Revision 1.2  2001/09/19 18:01:11  oes
434  *    Fixed comments; cosmetics
435  *
436  *    Revision 1.1  2001/09/16 17:08:54  jongfoster
437  *    Moving simple CGI functions from cgi.c to new file cgisimple.c
438  *
439  *
440  **********************************************************************/
441 \f
442
443 #include "config.h"
444
445 #include <stdio.h>
446 #include <sys/types.h>
447 #include <stdlib.h>
448 #include <ctype.h>
449 #include <string.h>
450 #include <assert.h>
451
452 #ifdef HAVE_ACCESS
453 #include <unistd.h>
454 #endif /* def HAVE_ACCESS */
455
456 #include "project.h"
457 #include "cgi.h"
458 #include "cgisimple.h"
459 #include "list.h"
460 #include "encode.h"
461 #include "jcc.h"
462 #include "filters.h"
463 #include "actions.h"
464 #include "miscutil.h"
465 #include "loadcfg.h"
466 #include "parsers.h"
467 #include "urlmatch.h"
468 #include "errlog.h"
469
470 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
471
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);
478
479 /*********************************************************************
480  *
481  * Function    :  cgi_default
482  *
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.
486  *               
487  * Parameters  :
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
491  *
492  * CGI Parameters : none
493  *
494  * Returns     :  JB_ERR_OK on success
495  *                JB_ERR_MEMORY on out-of-memory
496  *
497  *********************************************************************/
498 jb_err cgi_default(struct client_state *csp,
499                    struct http_response *rsp,
500                    const struct map *parameters)
501 {
502    struct map *exports;
503
504    (void)parameters;
505
506    assert(csp);
507    assert(rsp);
508
509    if (NULL == (exports = default_exports(csp, "")))
510    {
511       return JB_ERR_MEMORY;
512    }
513
514    return template_fill_for_cgi(csp, "default", exports, rsp);
515 }
516
517
518 /*********************************************************************
519  *
520  * Function    :  cgi_error_404
521  *
522  * Description :  CGI function that is called if an unknown action was
523  *                given.
524  *               
525  * Parameters  :
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
529  *
530  * CGI Parameters : none
531  *
532  * Returns     :  JB_ERR_OK on success
533  *                JB_ERR_MEMORY on out-of-memory error.  
534  *
535  *********************************************************************/
536 jb_err cgi_error_404(struct client_state *csp,
537                      struct http_response *rsp,
538                      const struct map *parameters)
539 {
540    struct map *exports;
541
542    assert(csp);
543    assert(rsp);
544    assert(parameters);
545
546    if (NULL == (exports = default_exports(csp, NULL)))
547    {
548       return JB_ERR_MEMORY;
549    }
550
551    rsp->status = strdup("404 Privoxy configuration page not found");
552    if (rsp->status == NULL)
553    {
554       free_map(exports);
555       return JB_ERR_MEMORY;
556    }
557
558    return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
559 }
560
561
562 #ifdef FEATURE_GRACEFUL_TERMINATION
563 /*********************************************************************
564  *
565  * Function    :  cgi_die
566  *
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.
571  *               
572  * Parameters  :
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
576  *
577  * CGI Parameters : none
578  *
579  * Returns     :  JB_ERR_OK on success
580  *                JB_ERR_MEMORY on out-of-memory error.  
581  *
582  *********************************************************************/
583 jb_err cgi_die (struct client_state *csp,
584                 struct http_response *rsp,
585                 const struct map *parameters)
586 {
587    assert(csp);
588    assert(rsp);
589    assert(parameters);
590
591    /* quit */
592    g_terminate = 1;
593
594    /*
595     * I don't really care what gets sent back to the browser.
596     * Take the easy option - "out of memory" page.
597     */
598
599    return JB_ERR_MEMORY;
600 }
601 #endif /* def FEATURE_GRACEFUL_TERMINATION */
602
603
604 /*********************************************************************
605  *
606  * Function    :  cgi_show_request
607  *
608  * Description :  Show the client's request and what sed() would have
609  *                made of it.
610  *               
611  * Parameters  :
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
615  *
616  * CGI Parameters : none
617  *
618  * Returns     :  JB_ERR_OK on success
619  *                JB_ERR_MEMORY on out-of-memory error.  
620  *
621  *********************************************************************/
622 jb_err cgi_show_request(struct client_state *csp,
623                         struct http_response *rsp,
624                         const struct map *parameters)
625 {
626    char *p;
627    struct map *exports;
628
629    assert(csp);
630    assert(rsp);
631    assert(parameters);
632
633    if (NULL == (exports = default_exports(csp, "show-request")))
634    {
635       return JB_ERR_MEMORY;
636    }
637    
638    /*
639     * Repair the damage done to the IOB by get_header()
640     */
641    for (p = csp->iob->buf; p < csp->iob->eod; p++)
642    {
643       if (*p == '\0') *p = '\n';
644    }
645
646    /*
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
649     */
650
651    if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
652    {
653       free_map(exports);
654       return JB_ERR_MEMORY;
655    }
656
657    if (map(exports, "processed-request", 1,
658          html_encode_and_free_original(list_to_text(csp->headers)), 0))
659    {
660       free_map(exports);
661       return JB_ERR_MEMORY;
662    }
663
664    return template_fill_for_cgi(csp, "show-request", exports, rsp);
665 }
666
667
668 /*********************************************************************
669  *
670  * Function    :  cgi_send_banner
671  *
672  * Description :  CGI function that returns a banner. 
673  *
674  * Parameters  :
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
678  *
679  * 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
684  *                  equivalent).
685  *
686  * Returns     :  JB_ERR_OK on success
687  *                JB_ERR_MEMORY on out-of-memory error.  
688  *
689  *********************************************************************/
690 jb_err cgi_send_banner(struct client_state *csp,
691                        struct http_response *rsp,
692                        const struct map *parameters)
693 {
694    char imagetype = lookup(parameters, "type")[0];
695
696    /*
697     * If type is auto, then determine the right thing
698     * to do from the set-image-blocker action
699     */
700    if (imagetype == 'a') 
701    {
702       /*
703        * Default to pattern
704        */
705       imagetype = 'p';
706
707 #ifdef FEATURE_IMAGE_BLOCKING
708       if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
709       {
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];
713
714          if (p == NULL)
715          {
716             /* Use default - nothing to do here. */
717          }
718          else if (0 == strcmpic(p, "blank"))
719          {
720             imagetype = 'b';
721          }
722          else if (0 == strcmpic(p, "pattern"))
723          {
724             imagetype = 'p';
725          }
726
727          /*
728           * If the action is to call this CGI, determine
729           * the argument:
730           */
731          else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
732          {
733             imagetype = p[sizeof(prefix1) - 1];
734          }
735          else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
736          {
737             imagetype = p[sizeof(prefix2) - 1];
738          }
739
740          /*
741           * Everything else must (should) be a URL to
742           * redirect to.
743           */
744          else
745          {
746             imagetype = 'r';
747          }
748       }
749 #endif /* def FEATURE_IMAGE_BLOCKING */
750    }
751       
752    /*
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:
757     */
758    if (imagetype == 'r') 
759    {
760       rsp->status = strdup("302 Local Redirect from Privoxy");
761       if (rsp->status == NULL)
762       {
763          return JB_ERR_MEMORY;
764       }
765       if (enlist_unique_header(rsp->headers, "Location",
766                                csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
767       {
768          return JB_ERR_MEMORY;
769       }
770    }
771    else
772    {
773       if ((imagetype == 'b') || (imagetype == 't')) 
774       {
775          rsp->body = bindup(image_blank_data, image_blank_length);
776          rsp->content_length = image_blank_length;
777       }
778       else
779       {
780          rsp->body = bindup(image_pattern_data, image_pattern_length);
781          rsp->content_length = image_pattern_length;
782       }
783
784       if (rsp->body == NULL)
785       {
786          return JB_ERR_MEMORY;
787       }
788       if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
789       {
790          return JB_ERR_MEMORY;
791       }
792
793       rsp->is_static = 1;
794    }
795
796    return JB_ERR_OK;
797
798 }
799
800
801 /*********************************************************************
802  *
803  * Function    :  cgi_transparent_image
804  *
805  * Description :  CGI function that sends a 1x1 transparent image.
806  *
807  * Parameters  :
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
811  *
812  * CGI Parameters : None
813  *
814  * Returns     :  JB_ERR_OK on success
815  *                JB_ERR_MEMORY on out-of-memory error.  
816  *
817  *********************************************************************/
818 jb_err cgi_transparent_image(struct client_state *csp,
819                              struct http_response *rsp,
820                              const struct map *parameters)
821 {
822    (void)csp;
823    (void)parameters;
824
825    rsp->body = bindup(image_blank_data, image_blank_length);
826    rsp->content_length = image_blank_length;
827
828    if (rsp->body == NULL)
829    {
830       return JB_ERR_MEMORY;
831    }
832
833    if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
834    {
835       return JB_ERR_MEMORY;
836    }
837
838    rsp->is_static = 1;
839
840    return JB_ERR_OK;
841
842 }
843
844
845 /*********************************************************************
846  *
847  * Function    :  cgi_send_default_favicon
848  *
849  * Description :  CGI function that sends the standard favicon.
850  *
851  * Parameters  :
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
855  *
856  * CGI Parameters : None
857  *
858  * Returns     :  JB_ERR_OK on success
859  *                JB_ERR_MEMORY on out-of-memory error.  
860  *
861  *********************************************************************/
862 jb_err cgi_send_default_favicon(struct client_state *csp,
863                                 struct http_response *rsp,
864                                 const struct map *parameters)
865 {
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"
880       "\017\000\000";
881    static const size_t favicon_length = sizeof(default_favicon_data) - 1;
882
883    (void)csp;
884    (void)parameters;
885
886    rsp->body = bindup(default_favicon_data, favicon_length);
887    rsp->content_length = favicon_length;
888
889    if (rsp->body == NULL)
890    {
891       return JB_ERR_MEMORY;
892    }
893
894    if (enlist(rsp->headers, "Content-Type: image/x-icon"))
895    {
896       return JB_ERR_MEMORY;
897    }
898
899    rsp->is_static = 1;
900
901    return JB_ERR_OK;
902
903 }
904
905
906 /*********************************************************************
907  *
908  * Function    :  cgi_send_error_favicon
909  *
910  * Description :  CGI function that sends the favicon for error pages.
911  *
912  * Parameters  :
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
916  *
917  * CGI Parameters : None
918  *
919  * Returns     :  JB_ERR_OK on success
920  *                JB_ERR_MEMORY on out-of-memory error.  
921  *
922  *********************************************************************/
923 jb_err cgi_send_error_favicon(struct client_state *csp,
924                               struct http_response *rsp,
925                               const struct map *parameters)
926 {
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"
941       "\017\000\000";
942    static const size_t favicon_length = sizeof(error_favicon_data) - 1;
943
944    (void)csp;
945    (void)parameters;
946
947    rsp->body = bindup(error_favicon_data, favicon_length);
948    rsp->content_length = favicon_length;
949
950    if (rsp->body == NULL)
951    {
952       return JB_ERR_MEMORY;
953    }
954
955    if (enlist(rsp->headers, "Content-Type: image/x-icon"))
956    {
957       return JB_ERR_MEMORY;
958    }
959
960    rsp->is_static = 1;
961
962    return JB_ERR_OK;
963
964 }
965
966
967 /*********************************************************************
968  *
969  * Function    :  cgi_send_stylesheet
970  *
971  * Description :  CGI function that sends a css stylesheet found
972  *                in the cgi-style.css template
973  *
974  * Parameters  :
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
978  *
979  * CGI Parameters : None
980  *
981  * Returns     :  JB_ERR_OK on success
982  *                JB_ERR_MEMORY on out-of-memory error.  
983  *
984  *********************************************************************/
985 jb_err cgi_send_stylesheet(struct client_state *csp,
986                            struct http_response *rsp,
987                            const struct map *parameters)
988 {
989    jb_err err;
990    
991    assert(csp);
992    assert(rsp);
993
994    (void)parameters;
995
996    err = template_load(csp, &rsp->body, "cgi-style.css", 0);
997
998    if (err == JB_ERR_FILE)
999    {
1000       /*
1001        * No way to tell user; send empty stylesheet
1002        */
1003       log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
1004    }
1005    else if (err)
1006    {
1007       return err; /* JB_ERR_MEMORY */
1008    }
1009
1010    if (enlist(rsp->headers, "Content-Type: text/css"))
1011    {
1012       return JB_ERR_MEMORY;
1013    }
1014
1015    return JB_ERR_OK;
1016
1017 }
1018
1019
1020 /*********************************************************************
1021  *
1022  * Function    :  cgi_send_url_info_osd
1023  *
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".
1027  *
1028  * Parameters  :
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
1032  *
1033  * CGI Parameters : None
1034  *
1035  * Returns     :  JB_ERR_OK on success
1036  *                JB_ERR_MEMORY on out-of-memory error.  
1037  *
1038  *********************************************************************/
1039 jb_err cgi_send_url_info_osd(struct client_state *csp,
1040                                struct http_response *rsp,
1041                                const struct map *parameters)
1042 {
1043    jb_err err = JB_ERR_MEMORY;
1044    struct map *exports = default_exports(csp, NULL);
1045
1046    (void)csp;
1047    (void)parameters;
1048
1049    if (NULL != exports)
1050    {
1051       err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
1052       if (JB_ERR_OK == err)
1053       {
1054          err = enlist(rsp->headers,
1055             "Content-Type: application/opensearchdescription+xml");
1056       }
1057    }
1058
1059    return err;
1060
1061 }
1062
1063
1064 /*********************************************************************
1065  *
1066  * Function    :  cgi_send_user_manual
1067  *
1068  * Description :  CGI function that sends a file in the user
1069  *                manual directory.
1070  *
1071  * Parameters  :
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
1075  *
1076  * CGI Parameters : file=name.html, the name of the HTML file
1077  *                  (relative to user-manual from config)
1078  *
1079  * Returns     :  JB_ERR_OK on success
1080  *                JB_ERR_MEMORY on out-of-memory error.  
1081  *
1082  *********************************************************************/
1083 jb_err cgi_send_user_manual(struct client_state *csp,
1084                             struct http_response *rsp,
1085                             const struct map *parameters)
1086 {
1087    const char * filename;
1088    char *full_path;
1089    jb_err err = JB_ERR_OK;
1090    size_t length;
1091
1092    assert(csp);
1093    assert(rsp);
1094    assert(parameters);
1095
1096    if (!parameters->first)
1097    {
1098       /* requested http://p.p/user-manual (without trailing slash) */
1099       return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
1100    }
1101
1102    get_string_param(parameters, "file", &filename);
1103    /* Check paramter for hack attempts */
1104    if (filename && strchr(filename, '/'))
1105    {
1106       return JB_ERR_CGI_PARAMS;
1107    }
1108    if (filename && strstr(filename, ".."))
1109    {
1110       return JB_ERR_CGI_PARAMS;
1111    }
1112
1113    full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
1114    if (full_path == NULL)
1115    {
1116       return JB_ERR_MEMORY;
1117    }
1118
1119    err = load_file(full_path, &rsp->body, &rsp->content_length);
1120    if (JB_ERR_OK != err)
1121    {
1122       assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
1123       if (JB_ERR_FILE == err)
1124       {
1125          err = cgi_error_no_template(csp, rsp, full_path);
1126       }
1127       freez(full_path);
1128       return err;
1129    }
1130    freez(full_path);
1131
1132    /* Guess correct Content-Type based on the filename's ending */
1133    if (filename)
1134    {
1135       length = strlen(filename);
1136    }
1137    else
1138    {
1139       length = 0;
1140    } 
1141    if((length>=4) && !strcmp(&filename[length-4], ".css"))
1142    {
1143       err = enlist(rsp->headers, "Content-Type: text/css");
1144    }
1145    else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
1146    {
1147       err = enlist(rsp->headers, "Content-Type: image/jpeg");
1148    }
1149    else
1150    {
1151       err = enlist(rsp->headers, "Content-Type: text/html");
1152    }
1153
1154    return err;
1155 }
1156
1157
1158 /*********************************************************************
1159  *
1160  * Function    :  cgi_show_version
1161  *
1162  * Description :  CGI function that returns a a web page describing the
1163  *                file versions of Privoxy.
1164  *
1165  * Parameters  :
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
1169  *
1170  * CGI Parameters : none
1171  *
1172  * Returns     :  JB_ERR_OK on success
1173  *                JB_ERR_MEMORY on out-of-memory error.  
1174  *
1175  *********************************************************************/
1176 jb_err cgi_show_version(struct client_state *csp,
1177                         struct http_response *rsp,
1178                         const struct map *parameters)
1179 {
1180    struct map *exports;
1181
1182    assert(csp);
1183    assert(rsp);
1184    assert(parameters);
1185
1186    if (NULL == (exports = default_exports(csp, "show-version")))
1187    {
1188       return JB_ERR_MEMORY;
1189    }
1190
1191    if (map(exports, "sourceversions", 1, show_rcs(), 0))
1192    {
1193       free_map(exports);
1194       return JB_ERR_MEMORY;
1195    }
1196
1197    return template_fill_for_cgi(csp, "show-version", exports, rsp);
1198 }
1199
1200
1201 /*********************************************************************
1202  *
1203  * Function    :  cgi_show_status
1204  *
1205  * Description :  CGI function that returns a web page describing the
1206  *                current status of Privoxy.
1207  *
1208  * Parameters  :
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
1212  *
1213  * CGI Parameters :
1214  *        file :  Which file to show.  Only first letter is checked,
1215  *                valid values are:
1216  *                - "a"ction file
1217  *                - "r"egex
1218  *                - "t"rust
1219  *                Default is to show menu and other information.
1220  *
1221  * Returns     :  JB_ERR_OK on success
1222  *                JB_ERR_MEMORY on out-of-memory error.  
1223  *
1224  *********************************************************************/
1225 jb_err cgi_show_status(struct client_state *csp,
1226                        struct http_response *rsp,
1227                        const struct map *parameters)
1228 {
1229    char *s = NULL;
1230    unsigned i;
1231    int j;
1232
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;
1240
1241    struct map *exports;
1242
1243    assert(csp);
1244    assert(rsp);
1245    assert(parameters);
1246
1247    if ('\0' != *(lookup(parameters, "file")))
1248    {
1249       return cgi_show_file(csp, rsp, parameters);
1250    }
1251
1252    if (NULL == (exports = default_exports(csp, "show-status")))
1253    {
1254       return JB_ERR_MEMORY;
1255    }
1256
1257    s = strdup("");
1258    for (j = 0; (s != NULL) && (j < Argc); j++)
1259    {
1260       if (!err) err = string_join  (&s, html_encode(Argv[j]));
1261       if (!err) err = string_append(&s, " ");
1262    }
1263    if (!err) err = map(exports, "invocation", 1, s, 0);
1264
1265    if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1266    if (!err) err = show_defines(exports);
1267
1268    if (err) 
1269    {
1270       free_map(exports);
1271       return JB_ERR_MEMORY;
1272    }
1273
1274 #ifdef FEATURE_STATISTICS
1275    local_urls_read     = urls_read;
1276    local_urls_rejected = urls_rejected;
1277
1278    /*
1279     * Need to alter the stats not to include the fetch of this
1280     * page.
1281     *
1282     * Can't do following thread safely! doh!
1283     *
1284     * urls_read--;
1285     * urls_rejected--; * This will be incremented subsequently *
1286     */
1287
1288    if (local_urls_read == 0)
1289    {
1290       if (!err) err = map_block_killer(exports, "have-stats");
1291    }
1292    else
1293    {
1294       if (!err) err = map_block_killer(exports, "have-no-stats");
1295
1296       perc_rej = (float)local_urls_rejected * 100.0F /
1297             (float)local_urls_read;
1298
1299       snprintf(buf, sizeof(buf), "%d", local_urls_read);
1300       if (!err) err = map(exports, "requests-received", 1, buf, 1);
1301
1302       snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1303       if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1304
1305       snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1306       if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1307    }
1308
1309 #else /* ndef FEATURE_STATISTICS */
1310    err = err || map_block_killer(exports, "statistics");
1311 #endif /* ndef FEATURE_STATISTICS */
1312    
1313    /* 
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!
1318     */
1319    s = strdup("");
1320    for (i = 0; i < MAX_AF_FILES; i++)
1321    {
1322       if (csp->actions_list[i] != NULL)
1323       {
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&amp;index=%u\">View</a>", i);
1328          if (!err) err = string_append(&s, buf);
1329
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]))
1334          {
1335 #ifdef HAVE_ACCESS
1336             if (access(csp->config->actions_file[i], W_OK) == 0)
1337             {
1338 #endif /* def HAVE_ACCESS */
1339                snprintf(buf, sizeof(buf), "&nbsp;&nbsp;<a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
1340                if (!err) err = string_append(&s, buf);
1341 #ifdef HAVE_ACCESS
1342             }
1343             else
1344             {
1345                if (!err) err = string_append(&s, "&nbsp;&nbsp;<strong>No write access.</strong>");
1346             }
1347 #endif /* def HAVE_ACCESS */
1348          }
1349 #endif
1350
1351          if (!err) err = string_append(&s, "</td></tr>\n");
1352       }
1353    }
1354    if (*s != '\0')   
1355    {
1356       if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1357    }
1358    else
1359    {
1360       if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1361    }
1362
1363    /* 
1364     * List all re_filterfiles in use, together with view options.
1365     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1366     */
1367    s = strdup("");
1368    for (i = 0; i < MAX_AF_FILES; i++)
1369    {
1370       if (csp->rlist[i] != NULL)
1371       {
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&amp;index=%u\">View</a>", i);
1376          if (!err) err = string_append(&s, buf);
1377          if (!err) err = string_append(&s, "</td></tr>\n");
1378       }
1379    }
1380    if (*s != '\0')   
1381    {
1382       if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1383    }
1384    else
1385    {
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");
1388    }
1389
1390 #ifdef FEATURE_TRUST
1391    if (csp->tlist)
1392    {
1393       if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1394    }
1395    else
1396    {
1397       if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1398       if (!err) err = map_block_killer(exports, "have-trustfile");
1399    }
1400 #else
1401    if (!err) err = map_block_killer(exports, "trust-support");
1402 #endif /* ndef FEATURE_TRUST */
1403
1404 #ifdef FEATURE_CGI_EDIT_ACTIONS
1405    if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1406    {
1407       err = map_block_killer(exports, "cgi-editor-is-disabled");
1408    }
1409 #endif /* ndef CGI_EDIT_ACTIONS */
1410
1411    if (err)
1412    {
1413       free_map(exports);
1414       return JB_ERR_MEMORY;
1415    }
1416
1417    return template_fill_for_cgi(csp, "show-status", exports, rsp);
1418 }
1419
1420  
1421 /*********************************************************************
1422  *
1423  * Function    :  cgi_show_url_info
1424  *
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.
1428  *
1429  * Parameters  :
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
1433  *
1434  * 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
1438  *                  the template.
1439  *
1440  * Returns     :  JB_ERR_OK on success
1441  *                JB_ERR_MEMORY on out-of-memory error.  
1442  *
1443  *********************************************************************/
1444 jb_err cgi_show_url_info(struct client_state *csp,
1445                          struct http_response *rsp,
1446                          const struct map *parameters)
1447 {
1448    char *url_param;
1449    struct map *exports;
1450    char buf[150];
1451
1452    assert(csp);
1453    assert(rsp);
1454    assert(parameters);
1455
1456    if (NULL == (exports = default_exports(csp, "show-url-info")))
1457    {
1458       return JB_ERR_MEMORY;
1459    }
1460
1461    /*
1462     * Get the url= parameter (if present) and remove any leading/trailing spaces.
1463     */
1464    url_param = strdup(lookup(parameters, "url"));
1465    if (url_param == NULL)
1466    {
1467       free_map(exports);
1468       return JB_ERR_MEMORY;
1469    }
1470    chomp(url_param);
1471
1472    /*
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
1478     *    "http://" prefix.
1479     * 4) Parameter not specified or is empty string - let this fall through
1480     *    for now, next block of code will handle it.
1481     */
1482    if (0 == strncmp(url_param, "http://", 7))
1483    {
1484       if (url_param[7] == '\0')
1485       {
1486          /*
1487           * Empty URL (just prefix).
1488           * Make it totally empty so it's caught by the next if()
1489           */
1490          url_param[0] = '\0';
1491       }
1492    }
1493    else if (0 == strncmp(url_param, "https://", 8))
1494    {
1495       if (url_param[8] == '\0')
1496       {
1497          /*
1498           * Empty URL (just prefix).
1499           * Make it totally empty so it's caught by the next if()
1500           */
1501          url_param[0] = '\0';
1502       }
1503    }
1504    else if ((url_param[0] != '\0') && (NULL == strstr(url_param, "://")))
1505    {
1506       /* No prefix - assume http:// */
1507       char *url_param_prefixed = strdup("http://");
1508
1509       if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1510       {
1511          free_map(exports);
1512          return JB_ERR_MEMORY;
1513       }
1514       url_param = url_param_prefixed;
1515    }
1516
1517    /*
1518     * Hide "toggle off" warning if Privoxy is toggled on.
1519     */
1520    if (
1521 #ifdef FEATURE_TOGGLE
1522        (global_toggle_state == 1) &&
1523 #endif /* def FEATURE_TOGGLE */
1524        map_block_killer(exports, "privoxy-is-toggled-off")
1525       )
1526    {
1527       free_map(exports);
1528       return JB_ERR_MEMORY;
1529    }
1530
1531    if (url_param[0] == '\0')
1532    {
1533       /* URL paramater not specified, display query form only. */
1534       free(url_param);
1535       if (map_block_killer(exports, "url-given")
1536         || map(exports, "url", 1, "", 1))
1537       {
1538          free_map(exports);
1539          return JB_ERR_MEMORY;
1540       }
1541    }
1542    else
1543    {
1544       /* Given a URL, so query it. */
1545       jb_err err;
1546       char *matches;
1547       char *s;
1548       int hits = 0;
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];
1553       int i;
1554       
1555       if (map(exports, "url", 1, html_encode(url_param), 0))
1556       {
1557          free(url_param);
1558          free_map(exports);
1559          return JB_ERR_MEMORY;
1560       }
1561
1562       init_current_action(action);
1563
1564       if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1565       {
1566          free_current_action(action);
1567          free(url_param);
1568          free_map(exports);
1569          return JB_ERR_MEMORY;
1570       }
1571
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)));
1575
1576       free(url_param);
1577
1578       if (err == JB_ERR_MEMORY)
1579       {
1580          free_http_request(url_to_query);
1581          free_current_action(action);
1582          free_map(exports);
1583          return JB_ERR_MEMORY;
1584       }
1585       else if (err)
1586       {
1587          /* Invalid URL */
1588
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");
1592
1593          free_current_action(action);
1594          free_http_request(url_to_query);
1595
1596          if (err)
1597          {
1598             free_map(exports);
1599             return JB_ERR_MEMORY;
1600          }
1601
1602          return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1603       }
1604
1605       /*
1606        * We have a warning about SSL paths.  Hide it for unencrypted sites.
1607        */
1608       if (!url_to_query->ssl)
1609       {
1610          if (map_block_killer(exports, "https"))
1611          {
1612             free_current_action(action);
1613             free_map(exports);
1614             free_http_request(url_to_query);
1615             return JB_ERR_MEMORY;
1616          }
1617       }
1618
1619       matches = strdup("<table summary=\"\" class=\"transparent\">");
1620
1621       for (i = 0; i < MAX_AF_FILES; i++)
1622       {
1623          if (NULL == csp->config->actions_file_short[i]
1624              || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1625
1626          b = NULL;
1627          hits = 1;
1628          if ((fl = csp->actions_list[i]) != NULL)
1629          {
1630             if ((b = fl->f) != NULL)
1631             {
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&amp;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)
1640                {
1641 #ifdef HAVE_ACCESS
1642                   if (access(csp->config->actions_file[i], W_OK) == 0)
1643                   {
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>");
1649 #ifdef HAVE_ACCESS
1650                   }
1651                   else
1652                   {
1653                      string_append(&matches, " <strong>No write access.</strong>");
1654                   }
1655 #endif /* def HAVE_ACCESS */
1656                }
1657 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1658
1659                string_append(&matches, "</th></tr>\n");
1660
1661                hits = 0;
1662                b = b->next;
1663             }
1664          }
1665
1666          for (; (b != NULL) && (matches != NULL); b = b->next)
1667          {
1668             if (url_match(b->url, url_to_query))
1669             {
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");
1675
1676                if (merge_current_action(action, b->action))
1677                {
1678                   freez(matches);
1679                   free_http_request(url_to_query);
1680                   free_current_action(action);
1681                   free_map(exports);
1682                   return JB_ERR_MEMORY;
1683                }
1684                hits++;
1685             }
1686          }
1687
1688          if (!hits)
1689          {
1690             string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1691          }
1692       }
1693       string_append(&matches, "</table>\n");
1694
1695       /*
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.
1700        *
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.
1704        */
1705       free_current_action(csp->action);
1706       get_url_actions(csp, url_to_query);
1707
1708       /*
1709        * Fill in forwarding settings.
1710        *
1711        * The possibilities are:
1712        *  - no forwarding
1713        *  - http forwarding only
1714        *  - socks4(a) forwarding only
1715        *  - socks4(a) and http forwarding.
1716        *
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.
1720        */
1721       {
1722          const struct forward_spec *fwd = forward_url(csp, url_to_query);
1723
1724          if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1725          {
1726             if (!err) err = map_block_killer(exports, "socks-forwarder");
1727             if (!err) err = map_block_killer(exports, "http-forwarder");
1728          }
1729          else
1730          {
1731             char port[10]; /* We save proxy ports as int but need a string here */
1732
1733             if (!err) err = map_block_killer(exports, "no-forwarder");
1734
1735             if (fwd->gateway_host != NULL)
1736             {
1737                char *socks_type = NULL;
1738
1739                switch (fwd->type)
1740                {
1741                   case SOCKS_4:
1742                      socks_type = "socks4";
1743                      break;
1744                   case SOCKS_4A:
1745                      socks_type = "socks4a";
1746                      break;
1747                   case SOCKS_5:
1748                      socks_type = "socks5";
1749                      break;
1750                   default:
1751                      log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1752                }
1753
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);
1758             }
1759             else
1760             {
1761                if (!err) err = map_block_killer(exports, "socks-forwarder");
1762             }
1763
1764             if (fwd->forward_host != NULL)
1765             {
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);
1769             }
1770             else
1771             {
1772                if (!err) err = map_block_killer(exports, "http-forwarder");
1773             }
1774          }
1775       }
1776
1777       free_http_request(url_to_query);
1778
1779       if (err || matches == NULL)
1780       {
1781          free_current_action(action);
1782          free_map(exports);
1783          return JB_ERR_MEMORY;
1784       }
1785
1786 #ifdef FEATURE_CGI_EDIT_ACTIONS
1787       if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1788       {
1789          err = map_block_killer(exports, "cgi-editor-is-disabled");
1790       }
1791 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1792
1793       /*
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.
1797        */
1798 #ifndef FEATURE_ZLIB
1799       if (!content_filters_enabled(action) ||
1800          (action->flags & ACTION_NO_COMPRESSION))
1801 #endif
1802       {
1803          if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1804       }
1805
1806       if (err || map(exports, "matches", 1, matches , 0))
1807       {
1808          free_current_action(action);
1809          free_map(exports);
1810          return JB_ERR_MEMORY;
1811       }
1812
1813       s = current_action_to_html(csp, action);
1814
1815       free_current_action(action);
1816
1817       if (map(exports, "final", 1, s, 0))
1818       {
1819          free_map(exports);
1820          return JB_ERR_MEMORY;
1821       }
1822    }
1823
1824    return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1825 }
1826
1827
1828 /*********************************************************************
1829  *
1830  * Function    :  cgi_robots_txt
1831  *
1832  * Description :  CGI function to return "/robots.txt".
1833  *
1834  * Parameters  :
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
1838  *
1839  * CGI Parameters : None
1840  *
1841  * Returns     :  JB_ERR_OK on success
1842  *                JB_ERR_MEMORY on out-of-memory error.  
1843  *
1844  *********************************************************************/
1845 jb_err cgi_robots_txt(struct client_state *csp,
1846                       struct http_response *rsp,
1847                       const struct map *parameters)
1848 {
1849    char buf[100];
1850    jb_err err;
1851
1852    (void)csp;
1853    (void)parameters;
1854
1855    rsp->body = strdup(
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"
1858       "# So go away!\n"
1859       "\n"
1860       "User-agent: *\n"
1861       "Disallow: /\n"
1862       "\n");
1863    if (rsp->body == NULL)
1864    {
1865       return JB_ERR_MEMORY;
1866    }
1867
1868    err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1869
1870    rsp->is_static = 1;
1871
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);
1874
1875    return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1876 }
1877
1878
1879 /*********************************************************************
1880  *
1881  * Function    :  show_defines
1882  *
1883  * Description :  Add to a map the state od all conditional #defines
1884  *                used when building
1885  *
1886  * Parameters  :
1887  *          1  :  exports = map to extend
1888  *
1889  * Returns     :  JB_ERR_OK on success
1890  *                JB_ERR_MEMORY on out-of-memory error.  
1891  *
1892  *********************************************************************/
1893 static jb_err show_defines(struct map *exports)
1894 {
1895    jb_err err = JB_ERR_OK;
1896
1897 #ifdef FEATURE_ACL
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 */
1902
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 */
1908
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 */
1914
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 */
1920
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 */
1928
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 */
1934
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 */
1940
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 */
1946
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 */
1952
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 */
1958
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 */
1964
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 */
1970
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 */
1976
1977 #ifdef FEATURE_ZLIB
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 */
1982
1983 #ifdef STATIC_PCRE
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 */
1988
1989 #ifdef STATIC_PCRS
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 */
1994
1995    return err;
1996 }
1997
1998
1999 /*********************************************************************
2000  *
2001  * Function    :  show_rcs
2002  *
2003  * Description :  Create a string with the rcs info for all sourcefiles
2004  *
2005  * Parameters  :  None
2006  *
2007  * Returns     :  A string, or NULL on out-of-memory.
2008  *
2009  *********************************************************************/
2010 static char *show_rcs(void)
2011 {
2012    char *result = strdup("");
2013    char buf[BUFFER_SIZE];
2014
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.
2018     */
2019
2020 #define SHOW_RCS(__x)              \
2021    {                               \
2022       extern const char __x[];     \
2023       snprintf(buf, sizeof(buf), " %s\n", __x);   \
2024       string_append(&result, buf); \
2025    }
2026
2027    /* In alphabetical order */
2028    SHOW_RCS(actions_h_rcs)
2029    SHOW_RCS(actions_rcs)
2030 #ifdef AMIGA
2031    SHOW_RCS(amiga_h_rcs)
2032    SHOW_RCS(amiga_rcs)
2033 #endif /* def AMIGA */
2034    SHOW_RCS(cgi_h_rcs)
2035    SHOW_RCS(cgi_rcs)
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)
2042 #ifdef __MINGW32__
2043    SHOW_RCS(cygwin_h_rcs)
2044 #endif
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)
2057    SHOW_RCS(jcc_h_rcs)
2058    SHOW_RCS(jcc_rcs)
2059    SHOW_RCS(list_h_rcs)
2060    SHOW_RCS(list_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)
2069    SHOW_RCS(pcrs_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)
2076 #ifdef _WIN32
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)
2085    SHOW_RCS(win32_rcs)
2086 #endif /* def _WIN32 */
2087
2088 #undef SHOW_RCS
2089
2090    return result;
2091
2092 }
2093
2094
2095 /*********************************************************************
2096  *
2097  * Function    :  cgi_show_file
2098  *
2099  * Description :  CGI function that shows the content of a
2100  *                configuration file.
2101  *
2102  * Parameters  :
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
2106  *
2107  * CGI Parameters :
2108  *        file :  Which file to show.  Only first letter is checked,
2109  *                valid values are:
2110  *                - "a"ction file
2111  *                - "r"egex
2112  *                - "t"rust
2113  *                Default is to show menu and other information.
2114  *
2115  * Returns     :  JB_ERR_OK on success
2116  *                JB_ERR_MEMORY on out-of-memory error.  
2117  *
2118  *********************************************************************/
2119 static jb_err cgi_show_file(struct client_state *csp,
2120                             struct http_response *rsp,
2121                             const struct map *parameters)
2122 {
2123    unsigned i;
2124    const char * filename = NULL;
2125    char * file_description = NULL;
2126
2127    assert(csp);
2128    assert(rsp);
2129    assert(parameters);
2130
2131    switch (*(lookup(parameters, "file")))
2132    {
2133    case 'a':
2134       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
2135       {
2136          filename = csp->actions_list[i]->filename;
2137          file_description = "Actions File";
2138       }
2139       break;
2140
2141    case 'f':
2142       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
2143       {
2144          filename = csp->rlist[i]->filename;
2145          file_description = "Filter File";
2146       }
2147       break;
2148
2149 #ifdef FEATURE_TRUST
2150    case 't':
2151       if (csp->tlist)
2152       {
2153          filename = csp->tlist->filename;
2154          file_description = "Trust File";
2155       }
2156       break;
2157 #endif /* def FEATURE_TRUST */
2158    }
2159
2160    if (NULL != filename)
2161    {
2162       struct map *exports;
2163       char *s;
2164       jb_err err;
2165       size_t length;
2166
2167       exports = default_exports(csp, "show-status");
2168       if (NULL == exports)
2169       {
2170          return JB_ERR_MEMORY;
2171       }
2172
2173       if ( map(exports, "file-description", 1, file_description, 1)
2174         || map(exports, "filepath", 1, html_encode(filename), 0) )
2175       {
2176          free_map(exports);
2177          return JB_ERR_MEMORY;
2178       }
2179
2180       err = load_file(filename, &s, &length);
2181       if (JB_ERR_OK != err)
2182       {
2183          if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
2184          {
2185             free_map(exports);
2186             return JB_ERR_MEMORY;
2187          }
2188       }
2189       else
2190       {
2191          s = html_encode_and_free_original(s);
2192          if (NULL == s)
2193          {
2194             return JB_ERR_MEMORY;
2195          }
2196
2197          if (map(exports, "contents", 1, s, 0))
2198          {
2199             free_map(exports);
2200             return JB_ERR_MEMORY;
2201          }
2202       }
2203
2204       return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
2205    }
2206
2207    return JB_ERR_CGI_PARAMS;
2208 }
2209
2210  
2211 /*********************************************************************
2212  *
2213  * Function    :  load_file
2214  *
2215  * Description :  Loads a file into a buffer.
2216  *
2217  * Parameters  :
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.
2221  *
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.
2226  *
2227  *********************************************************************/
2228 static jb_err load_file(const char *filename, char **buffer, size_t *length)
2229 {
2230    FILE *fp;
2231    long ret;
2232    jb_err err = JB_ERR_OK;
2233
2234    fp = fopen(filename, "rb");
2235    if (NULL == fp)
2236    {
2237       return JB_ERR_FILE;
2238    }
2239
2240    /* Get file length */
2241    if (fseek(fp, 0, SEEK_END))
2242    {
2243       log_error(LOG_LEVEL_FATAL,
2244          "Unexpected error while fseek()ing to the end of %s: %E",
2245          filename);
2246    }
2247    ret = ftell(fp);
2248    if (-1 == ret)
2249    {
2250       log_error(LOG_LEVEL_FATAL,
2251          "Unexpected ftell() error while loading %s: %E",
2252          filename);
2253    }
2254    *length = (size_t)ret;
2255
2256    /* Go back to the beginning. */
2257    if (fseek(fp, 0, SEEK_SET))
2258    {
2259       log_error(LOG_LEVEL_FATAL,
2260          "Unexpected error while fseek()ing to the beginning of %s: %E",
2261          filename);
2262    }
2263
2264    *buffer = (char *)zalloc(*length + 1);
2265    if (NULL == *buffer)
2266    {
2267       err = JB_ERR_MEMORY;
2268    }
2269    else if (!fread(*buffer, *length, 1, fp))
2270    {
2271       /*
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.
2274        */
2275       log_error(LOG_LEVEL_ERROR,
2276          "Couldn't completely read file %s.", filename);
2277       err = JB_ERR_FILE;
2278    }
2279
2280    fclose(fp);
2281
2282    return err;
2283
2284 }
2285
2286
2287 /*
2288   Local Variables:
2289   tab-width: 3
2290   end:
2291 */