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