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