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