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