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