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