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