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