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