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