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