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