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