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