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