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