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