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