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