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