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