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