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