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