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