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