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