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