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