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