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