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