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