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