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