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