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