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