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