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