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