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