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