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