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