0a6c7794247723f1bcaa699aa3a6baf77e8010ed
[privoxy.git] / cgisimple.c
1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.106 2011/02/14 16:07:52 fabiankeil 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-2011 the
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  **********************************************************************/
38
39
40 #include "config.h"
41
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <stdlib.h>
45 #include <ctype.h>
46 #include <string.h>
47 #include <assert.h>
48
49 #ifdef HAVE_ACCESS
50 #include <unistd.h>
51 #endif /* def HAVE_ACCESS */
52
53 #include "project.h"
54 #include "cgi.h"
55 #include "cgisimple.h"
56 #include "list.h"
57 #include "encode.h"
58 #include "jcc.h"
59 #include "filters.h"
60 #include "actions.h"
61 #include "miscutil.h"
62 #include "loadcfg.h"
63 #include "parsers.h"
64 #include "urlmatch.h"
65 #include "errlog.h"
66
67 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
68
69 static char *show_rcs(void);
70 static jb_err show_defines(struct map *exports);
71 static jb_err cgi_show_file(struct client_state *csp,
72                             struct http_response *rsp,
73                             const struct map *parameters);
74 static jb_err load_file(const char *filename, char **buffer, size_t *length);
75
76 /*********************************************************************
77  *
78  * Function    :  cgi_default
79  *
80  * Description :  CGI function that is called for the CGI_SITE_1_HOST
81  *                and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
82  *                Boring - only exports the default exports.
83  *               
84  * Parameters  :
85  *          1  :  csp = Current client state (buffers, headers, etc...)
86  *          2  :  rsp = http_response data structure for output
87  *          3  :  parameters = map of cgi parameters
88  *
89  * CGI Parameters : none
90  *
91  * Returns     :  JB_ERR_OK on success
92  *                JB_ERR_MEMORY on out-of-memory
93  *
94  *********************************************************************/
95 jb_err cgi_default(struct client_state *csp,
96                    struct http_response *rsp,
97                    const struct map *parameters)
98 {
99    struct map *exports;
100
101    (void)parameters;
102
103    assert(csp);
104    assert(rsp);
105
106    if (NULL == (exports = default_exports(csp, "")))
107    {
108       return JB_ERR_MEMORY;
109    }
110
111    return template_fill_for_cgi(csp, "default", exports, rsp);
112 }
113
114
115 /*********************************************************************
116  *
117  * Function    :  cgi_error_404
118  *
119  * Description :  CGI function that is called if an unknown action was
120  *                given.
121  *               
122  * Parameters  :
123  *          1  :  csp = Current client state (buffers, headers, etc...)
124  *          2  :  rsp = http_response data structure for output
125  *          3  :  parameters = map of cgi parameters
126  *
127  * CGI Parameters : none
128  *
129  * Returns     :  JB_ERR_OK on success
130  *                JB_ERR_MEMORY on out-of-memory error.  
131  *
132  *********************************************************************/
133 jb_err cgi_error_404(struct client_state *csp,
134                      struct http_response *rsp,
135                      const struct map *parameters)
136 {
137    struct map *exports;
138
139    assert(csp);
140    assert(rsp);
141    assert(parameters);
142
143    if (NULL == (exports = default_exports(csp, NULL)))
144    {
145       return JB_ERR_MEMORY;
146    }
147
148    rsp->status = strdup("404 Privoxy configuration page not found");
149    if (rsp->status == NULL)
150    {
151       free_map(exports);
152       return JB_ERR_MEMORY;
153    }
154
155    return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
156 }
157
158
159 #ifdef FEATURE_GRACEFUL_TERMINATION
160 /*********************************************************************
161  *
162  * Function    :  cgi_die
163  *
164  * Description :  CGI function to shut down Privoxy.
165  *                NOTE: Turning this on in a production build
166  *                would be a BAD idea.  An EXTREMELY BAD idea.
167  *                In short, don't do it.
168  *               
169  * Parameters  :
170  *          1  :  csp = Current client state (buffers, headers, etc...)
171  *          2  :  rsp = http_response data structure for output
172  *          3  :  parameters = map of cgi parameters
173  *
174  * CGI Parameters : none
175  *
176  * Returns     :  JB_ERR_OK on success
177  *                JB_ERR_MEMORY on out-of-memory error.  
178  *
179  *********************************************************************/
180 jb_err cgi_die (struct client_state *csp,
181                 struct http_response *rsp,
182                 const struct map *parameters)
183 {
184    static const char status[] = "200 OK Privoxy shutdown request received";
185    static const char body[] =
186       "<html>\n"
187       "<head>\n"
188       " <title>Privoxy shutdown request received</title>\n"
189       " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">\n"
190       " <link rel=\"stylesheet\" type=\"text/css\" href=\"http://config.privoxy.org/send-stylesheet\">\n"
191       "</head>\n"
192       "<body>\n"
193       "<h1>Privoxy shutdown request received</h1>\n"
194       "<p>Privoxy is going to shut down after the next request.</p>\n"
195       "</body>\n"
196       "</html>\n";
197
198    assert(csp);
199    assert(rsp);
200    assert(parameters);
201
202    /* quit */
203    g_terminate = 1;
204
205    rsp->content_length = 0;
206    rsp->head_length = 0;
207    rsp->is_static = 0;
208
209    rsp->body = strdup(body);
210    rsp->status = strdup(status);
211
212    if ((rsp->body == NULL) || (rsp->status == NULL))
213    {
214       return JB_ERR_MEMORY;
215    }
216
217    return JB_ERR_OK;
218 }
219 #endif /* def FEATURE_GRACEFUL_TERMINATION */
220
221
222 /*********************************************************************
223  *
224  * Function    :  cgi_show_request
225  *
226  * Description :  Show the client's request and what sed() would have
227  *                made of it.
228  *               
229  * Parameters  :
230  *          1  :  csp = Current client state (buffers, headers, etc...)
231  *          2  :  rsp = http_response data structure for output
232  *          3  :  parameters = map of cgi parameters
233  *
234  * CGI Parameters : none
235  *
236  * Returns     :  JB_ERR_OK on success
237  *                JB_ERR_MEMORY on out-of-memory error.  
238  *
239  *********************************************************************/
240 jb_err cgi_show_request(struct client_state *csp,
241                         struct http_response *rsp,
242                         const struct map *parameters)
243 {
244    char *p;
245    struct map *exports;
246
247    assert(csp);
248    assert(rsp);
249    assert(parameters);
250
251    if (NULL == (exports = default_exports(csp, "show-request")))
252    {
253       return JB_ERR_MEMORY;
254    }
255    
256    /*
257     * Repair the damage done to the IOB by get_header()
258     */
259    for (p = csp->iob->buf; p < csp->iob->eod; p++)
260    {
261       if (*p == '\0') *p = '\n';
262    }
263
264    /*
265     * Export the original client's request and the one we would
266     * be sending to the server if this wasn't a CGI call
267     */
268
269    if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
270    {
271       free_map(exports);
272       return JB_ERR_MEMORY;
273    }
274
275    if (map(exports, "processed-request", 1,
276          html_encode_and_free_original(list_to_text(csp->headers)), 0))
277    {
278       free_map(exports);
279       return JB_ERR_MEMORY;
280    }
281
282    return template_fill_for_cgi(csp, "show-request", exports, rsp);
283 }
284
285
286 /*********************************************************************
287  *
288  * Function    :  cgi_send_banner
289  *
290  * Description :  CGI function that returns a banner. 
291  *
292  * Parameters  :
293  *          1  :  csp = Current client state (buffers, headers, etc...)
294  *          2  :  rsp = http_response data structure for output
295  *          3  :  parameters = map of cgi parameters
296  *
297  * CGI Parameters :
298  *           type : Selects the type of banner between "trans", "logo",
299  *                  and "auto". Defaults to "logo" if absent or invalid.
300  *                  "auto" means to select as if we were image-blocking.
301  *                  (Only the first character really counts; b and t are
302  *                  equivalent).
303  *
304  * Returns     :  JB_ERR_OK on success
305  *                JB_ERR_MEMORY on out-of-memory error.  
306  *
307  *********************************************************************/
308 jb_err cgi_send_banner(struct client_state *csp,
309                        struct http_response *rsp,
310                        const struct map *parameters)
311 {
312    char imagetype = lookup(parameters, "type")[0];
313
314    /*
315     * If type is auto, then determine the right thing
316     * to do from the set-image-blocker action
317     */
318    if (imagetype == 'a') 
319    {
320       /*
321        * Default to pattern
322        */
323       imagetype = 'p';
324
325 #ifdef FEATURE_IMAGE_BLOCKING
326       if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
327       {
328          static const char prefix1[] = CGI_PREFIX "send-banner?type=";
329          static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
330          const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
331
332          if (p == NULL)
333          {
334             /* Use default - nothing to do here. */
335          }
336          else if (0 == strcmpic(p, "blank"))
337          {
338             imagetype = 'b';
339          }
340          else if (0 == strcmpic(p, "pattern"))
341          {
342             imagetype = 'p';
343          }
344
345          /*
346           * If the action is to call this CGI, determine
347           * the argument:
348           */
349          else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
350          {
351             imagetype = p[sizeof(prefix1) - 1];
352          }
353          else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
354          {
355             imagetype = p[sizeof(prefix2) - 1];
356          }
357
358          /*
359           * Everything else must (should) be a URL to
360           * redirect to.
361           */
362          else
363          {
364             imagetype = 'r';
365          }
366       }
367 #endif /* def FEATURE_IMAGE_BLOCKING */
368    }
369       
370    /*
371     * Now imagetype is either the non-auto type we were called with,
372     * or it was auto and has since been determined. In any case, we
373     * can proceed to actually answering the request by sending a redirect
374     * or an image as appropriate:
375     */
376    if (imagetype == 'r') 
377    {
378       rsp->status = strdup("302 Local Redirect from Privoxy");
379       if (rsp->status == NULL)
380       {
381          return JB_ERR_MEMORY;
382       }
383       if (enlist_unique_header(rsp->headers, "Location",
384                                csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
385       {
386          return JB_ERR_MEMORY;
387       }
388    }
389    else
390    {
391       if ((imagetype == 'b') || (imagetype == 't')) 
392       {
393          rsp->body = bindup(image_blank_data, image_blank_length);
394          rsp->content_length = image_blank_length;
395       }
396       else
397       {
398          rsp->body = bindup(image_pattern_data, image_pattern_length);
399          rsp->content_length = image_pattern_length;
400       }
401
402       if (rsp->body == NULL)
403       {
404          return JB_ERR_MEMORY;
405       }
406       if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
407       {
408          return JB_ERR_MEMORY;
409       }
410
411       rsp->is_static = 1;
412    }
413
414    return JB_ERR_OK;
415
416 }
417
418
419 /*********************************************************************
420  *
421  * Function    :  cgi_transparent_image
422  *
423  * Description :  CGI function that sends a 1x1 transparent image.
424  *
425  * Parameters  :
426  *          1  :  csp = Current client state (buffers, headers, etc...)
427  *          2  :  rsp = http_response data structure for output
428  *          3  :  parameters = map of cgi parameters
429  *
430  * CGI Parameters : None
431  *
432  * Returns     :  JB_ERR_OK on success
433  *                JB_ERR_MEMORY on out-of-memory error.  
434  *
435  *********************************************************************/
436 jb_err cgi_transparent_image(struct client_state *csp,
437                              struct http_response *rsp,
438                              const struct map *parameters)
439 {
440    (void)csp;
441    (void)parameters;
442
443    rsp->body = bindup(image_blank_data, image_blank_length);
444    rsp->content_length = image_blank_length;
445
446    if (rsp->body == NULL)
447    {
448       return JB_ERR_MEMORY;
449    }
450
451    if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
452    {
453       return JB_ERR_MEMORY;
454    }
455
456    rsp->is_static = 1;
457
458    return JB_ERR_OK;
459
460 }
461
462
463 /*********************************************************************
464  *
465  * Function    :  cgi_send_default_favicon
466  *
467  * Description :  CGI function that sends the standard favicon.
468  *
469  * Parameters  :
470  *          1  :  csp = Current client state (buffers, headers, etc...)
471  *          2  :  rsp = http_response data structure for output
472  *          3  :  parameters = map of cgi parameters
473  *
474  * CGI Parameters : None
475  *
476  * Returns     :  JB_ERR_OK on success
477  *                JB_ERR_MEMORY on out-of-memory error.  
478  *
479  *********************************************************************/
480 jb_err cgi_send_default_favicon(struct client_state *csp,
481                                 struct http_response *rsp,
482                                 const struct map *parameters)
483 {
484    static const char default_favicon_data[] =
485       "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
486       "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
487       "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
488       "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
489       "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077"
490       "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
491       "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
492       "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
493       "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
494       "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
495       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
496       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
497       "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
498       "\017\000\000";
499    static const size_t favicon_length = sizeof(default_favicon_data) - 1;
500
501    (void)csp;
502    (void)parameters;
503
504    rsp->body = bindup(default_favicon_data, favicon_length);
505    rsp->content_length = favicon_length;
506
507    if (rsp->body == NULL)
508    {
509       return JB_ERR_MEMORY;
510    }
511
512    if (enlist(rsp->headers, "Content-Type: image/x-icon"))
513    {
514       return JB_ERR_MEMORY;
515    }
516
517    rsp->is_static = 1;
518
519    return JB_ERR_OK;
520
521 }
522
523
524 /*********************************************************************
525  *
526  * Function    :  cgi_send_error_favicon
527  *
528  * Description :  CGI function that sends the favicon for error pages.
529  *
530  * Parameters  :
531  *          1  :  csp = Current client state (buffers, headers, etc...)
532  *          2  :  rsp = http_response data structure for output
533  *          3  :  parameters = map of cgi parameters
534  *
535  * CGI Parameters : None
536  *
537  * Returns     :  JB_ERR_OK on success
538  *                JB_ERR_MEMORY on out-of-memory error.  
539  *
540  *********************************************************************/
541 jb_err cgi_send_error_favicon(struct client_state *csp,
542                               struct http_response *rsp,
543                               const struct map *parameters)
544 {
545    static const char error_favicon_data[] =
546       "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260"
547       "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000"
548       "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000"
549       "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000"
550       "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077"
551       "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000"
552       "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000"
553       "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036"
554       "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360"
555       "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000"
556       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
557       "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
558       "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360"
559       "\017\000\000";
560    static const size_t favicon_length = sizeof(error_favicon_data) - 1;
561
562    (void)csp;
563    (void)parameters;
564
565    rsp->body = bindup(error_favicon_data, favicon_length);
566    rsp->content_length = favicon_length;
567
568    if (rsp->body == NULL)
569    {
570       return JB_ERR_MEMORY;
571    }
572
573    if (enlist(rsp->headers, "Content-Type: image/x-icon"))
574    {
575       return JB_ERR_MEMORY;
576    }
577
578    rsp->is_static = 1;
579
580    return JB_ERR_OK;
581
582 }
583
584
585 /*********************************************************************
586  *
587  * Function    :  cgi_send_stylesheet
588  *
589  * Description :  CGI function that sends a css stylesheet found
590  *                in the cgi-style.css template
591  *
592  * Parameters  :
593  *          1  :  csp = Current client state (buffers, headers, etc...)
594  *          2  :  rsp = http_response data structure for output
595  *          3  :  parameters = map of cgi parameters
596  *
597  * CGI Parameters : None
598  *
599  * Returns     :  JB_ERR_OK on success
600  *                JB_ERR_MEMORY on out-of-memory error.  
601  *
602  *********************************************************************/
603 jb_err cgi_send_stylesheet(struct client_state *csp,
604                            struct http_response *rsp,
605                            const struct map *parameters)
606 {
607    jb_err err;
608    
609    assert(csp);
610    assert(rsp);
611
612    (void)parameters;
613
614    err = template_load(csp, &rsp->body, "cgi-style.css", 0);
615
616    if (err == JB_ERR_FILE)
617    {
618       /*
619        * No way to tell user; send empty stylesheet
620        */
621       log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
622    }
623    else if (err)
624    {
625       return err; /* JB_ERR_MEMORY */
626    }
627
628    if (enlist(rsp->headers, "Content-Type: text/css"))
629    {
630       return JB_ERR_MEMORY;
631    }
632
633    return JB_ERR_OK;
634
635 }
636
637
638 /*********************************************************************
639  *
640  * Function    :  cgi_send_url_info_osd
641  *
642  * Description :  CGI function that sends the OpenSearch Description
643  *                template for the show-url-info page. It allows to
644  *                access the page through "search engine plugins".
645  *
646  * Parameters  :
647  *          1  :  csp = Current client state (buffers, headers, etc...)
648  *          2  :  rsp = http_response data structure for output
649  *          3  :  parameters = map of cgi parameters
650  *
651  * CGI Parameters : None
652  *
653  * Returns     :  JB_ERR_OK on success
654  *                JB_ERR_MEMORY on out-of-memory error.  
655  *
656  *********************************************************************/
657 jb_err cgi_send_url_info_osd(struct client_state *csp,
658                                struct http_response *rsp,
659                                const struct map *parameters)
660 {
661    jb_err err = JB_ERR_MEMORY;
662    struct map *exports = default_exports(csp, NULL);
663
664    (void)csp;
665    (void)parameters;
666
667    if (NULL != exports)
668    {
669       err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp);
670       if (JB_ERR_OK == err)
671       {
672          err = enlist(rsp->headers,
673             "Content-Type: application/opensearchdescription+xml");
674       }
675    }
676
677    return err;
678
679 }
680
681
682 /*********************************************************************
683  *
684  * Function    :  get_content_type
685  *
686  * Description :  Use the file extension to guess the content type
687  *                header we should use to serve the file.
688  *
689  * Parameters  :
690  *          1  :  filename = Name of the file whose content type
691  *                           we care about
692  *
693  * Returns     :  The guessed content type.
694  *
695  *********************************************************************/
696 static const char *get_content_type(const char *filename)
697 {
698    int i;
699    struct content_type
700    {
701       const char *extension;
702       const char *content_type;
703    };
704    static const struct content_type content_types[] =
705    {
706       {".css",  "text/css"},
707       {".jpg",  "image/jpeg"},
708       {".jpeg", "image/jpeg"},
709       {".png",  "image/png"},
710    };
711
712    for (i = 0; i < SZ(content_types); i++)
713    {
714       if (strstr(filename, content_types[i].extension))
715       {
716          return content_types[i].content_type;
717       }
718    }
719
720    /* No match by extension, default to html */
721    return "text/html";
722 }
723
724 /*********************************************************************
725  *
726  * Function    :  cgi_send_user_manual
727  *
728  * Description :  CGI function that sends a file in the user
729  *                manual directory.
730  *
731  * Parameters  :
732  *          1  :  csp = Current client state (buffers, headers, etc...)
733  *          2  :  rsp = http_response data structure for output
734  *          3  :  parameters = map of cgi parameters
735  *
736  * CGI Parameters : file=name.html, the name of the HTML file
737  *                  (relative to user-manual from config)
738  *
739  * Returns     :  JB_ERR_OK on success
740  *                JB_ERR_MEMORY on out-of-memory error.  
741  *
742  *********************************************************************/
743 jb_err cgi_send_user_manual(struct client_state *csp,
744                             struct http_response *rsp,
745                             const struct map *parameters)
746 {
747    const char *filename;
748    char *full_path;
749    jb_err err = JB_ERR_OK;
750    const char *content_type;
751
752    assert(csp);
753    assert(rsp);
754    assert(parameters);
755
756    if (0 == strncmpic(csp->config->usermanual, "http://", 7))
757    {
758       log_error(LOG_LEVEL_CGI, "Request for local user-manual "
759          "received while user-manual delivery is disabled.");
760       return cgi_error_404(csp, rsp, parameters);
761    }
762
763    if (!parameters->first)
764    {
765       /* requested http://p.p/user-manual (without trailing slash) */
766       return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
767    }
768
769    get_string_param(parameters, "file", &filename);
770    if (filename == NULL)
771    {
772       /* It's '/' so serve the index.html if there is one.  */
773       filename = "index.html";
774    }
775    else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
776    {
777       /*
778        * We currently only support a flat file
779        * hierachy for the documentation.
780        */
781       log_error(LOG_LEVEL_ERROR,
782          "Rejecting the request to serve '%s' as it contains '/' or '..'",
783          filename);
784       return JB_ERR_CGI_PARAMS;
785    }
786
787    full_path = make_path(csp->config->usermanual, filename);
788    if (full_path == NULL)
789    {
790       return JB_ERR_MEMORY;
791    }
792
793    err = load_file(full_path, &rsp->body, &rsp->content_length);
794    if (JB_ERR_OK != err)
795    {
796       assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
797       if (JB_ERR_FILE == err)
798       {
799          err = cgi_error_no_template(csp, rsp, full_path);
800       }
801       freez(full_path);
802       return err;
803    }
804    freez(full_path);
805
806    content_type = get_content_type(filename);
807    log_error(LOG_LEVEL_CGI,
808       "Content-Type guessed for %s: %s", filename, content_type);
809
810    return enlist_unique_header(rsp->headers, "Content-Type", content_type);
811
812 }
813
814
815 /*********************************************************************
816  *
817  * Function    :  cgi_show_version
818  *
819  * Description :  CGI function that returns a a web page describing the
820  *                file versions of Privoxy.
821  *
822  * Parameters  :
823  *          1  :  csp = Current client state (buffers, headers, etc...)
824  *          2  :  rsp = http_response data structure for output
825  *          3  :  parameters = map of cgi parameters
826  *
827  * CGI Parameters : none
828  *
829  * Returns     :  JB_ERR_OK on success
830  *                JB_ERR_MEMORY on out-of-memory error.  
831  *
832  *********************************************************************/
833 jb_err cgi_show_version(struct client_state *csp,
834                         struct http_response *rsp,
835                         const struct map *parameters)
836 {
837    struct map *exports;
838
839    assert(csp);
840    assert(rsp);
841    assert(parameters);
842
843    if (NULL == (exports = default_exports(csp, "show-version")))
844    {
845       return JB_ERR_MEMORY;
846    }
847
848    if (map(exports, "sourceversions", 1, show_rcs(), 0))
849    {
850       free_map(exports);
851       return JB_ERR_MEMORY;
852    }
853
854    return template_fill_for_cgi(csp, "show-version", exports, rsp);
855 }
856
857
858 /*********************************************************************
859  *
860  * Function    :  cgi_show_status
861  *
862  * Description :  CGI function that returns a web page describing the
863  *                current status of Privoxy.
864  *
865  * Parameters  :
866  *          1  :  csp = Current client state (buffers, headers, etc...)
867  *          2  :  rsp = http_response data structure for output
868  *          3  :  parameters = map of cgi parameters
869  *
870  * CGI Parameters :
871  *        file :  Which file to show.  Only first letter is checked,
872  *                valid values are:
873  *                - "a"ction file
874  *                - "r"egex
875  *                - "t"rust
876  *                Default is to show menu and other information.
877  *
878  * Returns     :  JB_ERR_OK on success
879  *                JB_ERR_MEMORY on out-of-memory error.  
880  *
881  *********************************************************************/
882 jb_err cgi_show_status(struct client_state *csp,
883                        struct http_response *rsp,
884                        const struct map *parameters)
885 {
886    char *s = NULL;
887    unsigned i;
888    int j;
889
890    char buf[BUFFER_SIZE];
891 #ifdef FEATURE_STATISTICS
892    float perc_rej;   /* Percentage of http requests rejected */
893    int local_urls_read;
894    int local_urls_rejected;
895 #endif /* ndef FEATURE_STATISTICS */
896    jb_err err = JB_ERR_OK;
897
898    struct map *exports;
899
900    assert(csp);
901    assert(rsp);
902    assert(parameters);
903
904    if ('\0' != *(lookup(parameters, "file")))
905    {
906       return cgi_show_file(csp, rsp, parameters);
907    }
908
909    if (NULL == (exports = default_exports(csp, "show-status")))
910    {
911       return JB_ERR_MEMORY;
912    }
913
914    s = strdup("");
915    for (j = 0; (s != NULL) && (j < Argc); j++)
916    {
917       if (!err) err = string_join  (&s, html_encode(Argv[j]));
918       if (!err) err = string_append(&s, " ");
919    }
920    if (!err) err = map(exports, "invocation", 1, s, 0);
921
922    if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
923    if (!err) err = show_defines(exports);
924
925    if (err) 
926    {
927       free_map(exports);
928       return JB_ERR_MEMORY;
929    }
930
931 #ifdef FEATURE_STATISTICS
932    local_urls_read     = urls_read;
933    local_urls_rejected = urls_rejected;
934
935    /*
936     * Need to alter the stats not to include the fetch of this
937     * page.
938     *
939     * Can't do following thread safely! doh!
940     *
941     * urls_read--;
942     * urls_rejected--; * This will be incremented subsequently *
943     */
944
945    if (local_urls_read == 0)
946    {
947       if (!err) err = map_block_killer(exports, "have-stats");
948    }
949    else
950    {
951       if (!err) err = map_block_killer(exports, "have-no-stats");
952
953       perc_rej = (float)local_urls_rejected * 100.0F /
954             (float)local_urls_read;
955
956       snprintf(buf, sizeof(buf), "%d", local_urls_read);
957       if (!err) err = map(exports, "requests-received", 1, buf, 1);
958
959       snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
960       if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
961
962       snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
963       if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
964    }
965
966 #else /* ndef FEATURE_STATISTICS */
967    err = err || map_block_killer(exports, "statistics");
968 #endif /* ndef FEATURE_STATISTICS */
969    
970    /* 
971     * List all action files in use, together with view and edit links,
972     * except for standard.action, which should only be viewable. (Not
973     * enforced in the editor itself)
974     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
975     */
976    s = strdup("");
977    for (i = 0; i < MAX_AF_FILES; i++)
978    {
979       if (csp->actions_list[i] != NULL)
980       {
981          if (!err) err = string_append(&s, "<tr><td>");
982          if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
983          snprintf(buf, sizeof(buf),
984             "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&amp;index=%u\">View</a>", i);
985          if (!err) err = string_append(&s, buf);
986
987 #ifdef FEATURE_CGI_EDIT_ACTIONS
988          if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
989             && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
990             && (NULL != csp->config->actions_file_short[i]))
991          {
992 #ifdef HAVE_ACCESS
993             if (access(csp->config->actions_file[i], W_OK) == 0)
994             {
995 #endif /* def HAVE_ACCESS */
996                snprintf(buf, sizeof(buf), "&nbsp;&nbsp;<a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
997                if (!err) err = string_append(&s, buf);
998 #ifdef HAVE_ACCESS
999             }
1000             else
1001             {
1002                if (!err) err = string_append(&s, "&nbsp;&nbsp;<strong>No write access.</strong>");
1003             }
1004 #endif /* def HAVE_ACCESS */
1005          }
1006 #endif
1007
1008          if (!err) err = string_append(&s, "</td></tr>\n");
1009       }
1010    }
1011    if (*s != '\0')   
1012    {
1013       if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1014    }
1015    else
1016    {
1017       if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1018    }
1019
1020    /* 
1021     * List all re_filterfiles in use, together with view options.
1022     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1023     */
1024    s = strdup("");
1025    for (i = 0; i < MAX_AF_FILES; i++)
1026    {
1027       if (csp->rlist[i] != NULL)
1028       {
1029          if (!err) err = string_append(&s, "<tr><td>");
1030          if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1031          snprintf(buf, sizeof(buf),
1032             "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&amp;index=%u\">View</a>", i);
1033          if (!err) err = string_append(&s, buf);
1034          if (!err) err = string_append(&s, "</td></tr>\n");
1035       }
1036    }
1037    if (*s != '\0')   
1038    {
1039       if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1040    }
1041    else
1042    {
1043       if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1044       if (!err) err = map_block_killer(exports, "have-filterfile");
1045    }
1046
1047 #ifdef FEATURE_TRUST
1048    if (csp->tlist)
1049    {
1050       if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1051    }
1052    else
1053    {
1054       if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1055       if (!err) err = map_block_killer(exports, "have-trustfile");
1056    }
1057 #else
1058    if (!err) err = map_block_killer(exports, "trust-support");
1059 #endif /* ndef FEATURE_TRUST */
1060
1061 #ifdef FEATURE_CGI_EDIT_ACTIONS
1062    if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1063    {
1064       err = map_block_killer(exports, "cgi-editor-is-disabled");
1065    }
1066 #endif /* ndef CGI_EDIT_ACTIONS */
1067
1068    if (err)
1069    {
1070       free_map(exports);
1071       return JB_ERR_MEMORY;
1072    }
1073
1074    return template_fill_for_cgi(csp, "show-status", exports, rsp);
1075 }
1076
1077  
1078 /*********************************************************************
1079  *
1080  * Function    :  cgi_show_url_info
1081  *
1082  * Description :  CGI function that determines and shows which actions
1083  *                Privoxy will perform for a given url, and which
1084  *                matches starting from the defaults have lead to that.
1085  *
1086  * Parameters  :
1087  *          1  :  csp = Current client state (buffers, headers, etc...)
1088  *          2  :  rsp = http_response data structure for output
1089  *          3  :  parameters = map of cgi parameters
1090  *
1091  * CGI Parameters :
1092  *            url : The url whose actions are to be determined.
1093  *                  If url is unset, the url-given conditional will be
1094  *                  set, so that all but the form can be suppressed in
1095  *                  the template.
1096  *
1097  * Returns     :  JB_ERR_OK on success
1098  *                JB_ERR_MEMORY on out-of-memory error.  
1099  *
1100  *********************************************************************/
1101 jb_err cgi_show_url_info(struct client_state *csp,
1102                          struct http_response *rsp,
1103                          const struct map *parameters)
1104 {
1105    char *url_param;
1106    struct map *exports;
1107    char buf[150];
1108
1109    assert(csp);
1110    assert(rsp);
1111    assert(parameters);
1112
1113    if (NULL == (exports = default_exports(csp, "show-url-info")))
1114    {
1115       return JB_ERR_MEMORY;
1116    }
1117
1118    /*
1119     * Get the url= parameter (if present) and remove any leading/trailing spaces.
1120     */
1121    url_param = strdup(lookup(parameters, "url"));
1122    if (url_param == NULL)
1123    {
1124       free_map(exports);
1125       return JB_ERR_MEMORY;
1126    }
1127    chomp(url_param);
1128
1129    /*
1130     * Handle prefixes.  4 possibilities:
1131     * 1) "http://" or "https://" prefix present and followed by URL - OK
1132     * 2) Only the "http://" or "https://" part is present, no URL - change
1133     *    to empty string so it will be detected later as "no URL".
1134     * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1135     *    "http://" prefix.
1136     * 4) Parameter not specified or is empty string - let this fall through
1137     *    for now, next block of code will handle it.
1138     */
1139    if (0 == strncmp(url_param, "http://", 7))
1140    {
1141       if (url_param[7] == '\0')
1142       {
1143          /*
1144           * Empty URL (just prefix).
1145           * Make it totally empty so it's caught by the next if()
1146           */
1147          url_param[0] = '\0';
1148       }
1149    }
1150    else if (0 == strncmp(url_param, "https://", 8))
1151    {
1152       if (url_param[8] == '\0')
1153       {
1154          /*
1155           * Empty URL (just prefix).
1156           * Make it totally empty so it's caught by the next if()
1157           */
1158          url_param[0] = '\0';
1159       }
1160    }
1161    else if ((url_param[0] != '\0')
1162       && ((NULL == strstr(url_param, "://")
1163             || (strstr(url_param, "://") > strstr(url_param, "/")))))
1164    {
1165       /*
1166        * No prefix or at least no prefix before
1167        * the first slash - assume http://
1168        */
1169       char *url_param_prefixed = strdup("http://");
1170
1171       if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1172       {
1173          free_map(exports);
1174          return JB_ERR_MEMORY;
1175       }
1176       url_param = url_param_prefixed;
1177    }
1178
1179    /*
1180     * Hide "toggle off" warning if Privoxy is toggled on.
1181     */
1182    if (
1183 #ifdef FEATURE_TOGGLE
1184        (global_toggle_state == 1) &&
1185 #endif /* def FEATURE_TOGGLE */
1186        map_block_killer(exports, "privoxy-is-toggled-off")
1187       )
1188    {
1189       free_map(exports);
1190       return JB_ERR_MEMORY;
1191    }
1192
1193    if (url_param[0] == '\0')
1194    {
1195       /* URL paramater not specified, display query form only. */
1196       free(url_param);
1197       if (map_block_killer(exports, "url-given")
1198         || map(exports, "url", 1, "", 1))
1199       {
1200          free_map(exports);
1201          return JB_ERR_MEMORY;
1202       }
1203    }
1204    else
1205    {
1206       /* Given a URL, so query it. */
1207       jb_err err;
1208       char *matches;
1209       char *s;
1210       int hits = 0;
1211       struct file_list *fl;
1212       struct url_actions *b;
1213       struct http_request url_to_query[1];
1214       struct current_action_spec action[1];
1215       int i;
1216       
1217       if (map(exports, "url", 1, html_encode(url_param), 0))
1218       {
1219          free(url_param);
1220          free_map(exports);
1221          return JB_ERR_MEMORY;
1222       }
1223
1224       init_current_action(action);
1225
1226       if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1227       {
1228          free_current_action(action);
1229          free(url_param);
1230          free_map(exports);
1231          return JB_ERR_MEMORY;
1232       }
1233
1234       memset(url_to_query, '\0', sizeof(url_to_query));
1235       err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1236       assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1237
1238       free(url_param);
1239
1240       if (err == JB_ERR_MEMORY)
1241       {
1242          free_http_request(url_to_query);
1243          free_current_action(action);
1244          free_map(exports);
1245          return JB_ERR_MEMORY;
1246       }
1247       else if (err)
1248       {
1249          /* Invalid URL */
1250
1251          err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1252          if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1253          if (!err) err = map_block_killer(exports, "valid-url");
1254
1255          free_current_action(action);
1256          free_http_request(url_to_query);
1257
1258          if (err)
1259          {
1260             free_map(exports);
1261             return JB_ERR_MEMORY;
1262          }
1263
1264          return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1265       }
1266
1267       /*
1268        * We have a warning about SSL paths.  Hide it for unencrypted sites.
1269        */
1270       if (!url_to_query->ssl)
1271       {
1272          if (map_block_killer(exports, "https"))
1273          {
1274             free_current_action(action);
1275             free_map(exports);
1276             free_http_request(url_to_query);
1277             return JB_ERR_MEMORY;
1278          }
1279       }
1280
1281       matches = strdup("<table summary=\"\" class=\"transparent\">");
1282
1283       for (i = 0; i < MAX_AF_FILES; i++)
1284       {
1285          if (NULL == csp->config->actions_file_short[i]
1286              || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1287
1288          b = NULL;
1289          hits = 1;
1290          if ((fl = csp->actions_list[i]) != NULL)
1291          {
1292             if ((b = fl->f) != NULL)
1293             {
1294                /* FIXME: Hardcoded HTML! */
1295                string_append(&matches, "<tr><th>In file: ");
1296                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1297                snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&amp;index=%d\">", i);
1298                string_append(&matches, buf);
1299                string_append(&matches, "View</a>");
1300 #ifdef FEATURE_CGI_EDIT_ACTIONS
1301                if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1302                {
1303 #ifdef HAVE_ACCESS
1304                   if (access(csp->config->actions_file[i], W_OK) == 0)
1305                   {
1306 #endif /* def HAVE_ACCESS */
1307                      snprintf(buf, sizeof(buf),
1308                         " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1309                      string_append(&matches, buf);
1310                      string_append(&matches, "Edit</a>");
1311 #ifdef HAVE_ACCESS
1312                   }
1313                   else
1314                   {
1315                      string_append(&matches, " <strong>No write access.</strong>");
1316                   }
1317 #endif /* def HAVE_ACCESS */
1318                }
1319 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1320
1321                string_append(&matches, "</th></tr>\n");
1322
1323                hits = 0;
1324                b = b->next;
1325             }
1326          }
1327
1328          for (; (b != NULL) && (matches != NULL); b = b->next)
1329          {
1330             if (url_match(b->url, url_to_query))
1331             {
1332                string_append(&matches, "<tr><td>{");
1333                string_join  (&matches, actions_to_html(csp, b->action));
1334                string_append(&matches, " }<br>\n<code>");
1335                string_join  (&matches, html_encode(b->url->spec));
1336                string_append(&matches, "</code></td></tr>\n");
1337
1338                if (merge_current_action(action, b->action))
1339                {
1340                   freez(matches);
1341                   free_http_request(url_to_query);
1342                   free_current_action(action);
1343                   free_map(exports);
1344                   return JB_ERR_MEMORY;
1345                }
1346                hits++;
1347             }
1348          }
1349
1350          if (!hits)
1351          {
1352             string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1353          }
1354       }
1355       string_append(&matches, "</table>\n");
1356
1357       /*
1358        * XXX: Kludge to make sure the "Forward settings" section
1359        * shows what forward-override{} would do with the requested URL.
1360        * No one really cares how the CGI request would be forwarded
1361        * if it wasn't intercepted as CGI request in the first place.
1362        *
1363        * From here on the action bitmask will no longer reflect
1364        * the real url (http://config.privoxy.org/show-url-info?url=.*),
1365        * but luckily it's no longer required later on anyway.
1366        */
1367       free_current_action(csp->action);
1368       get_url_actions(csp, url_to_query);
1369
1370       /*
1371        * Fill in forwarding settings.
1372        *
1373        * The possibilities are:
1374        *  - no forwarding
1375        *  - http forwarding only
1376        *  - socks4(a) forwarding only
1377        *  - socks4(a) and http forwarding.
1378        *
1379        * XXX: Parts of this code could be reused for the
1380        * "forwarding-failed" template which currently doesn't
1381        * display the proxy port and an eventual second forwarder.
1382        */
1383       {
1384          const struct forward_spec *fwd = forward_url(csp, url_to_query);
1385
1386          if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1387          {
1388             if (!err) err = map_block_killer(exports, "socks-forwarder");
1389             if (!err) err = map_block_killer(exports, "http-forwarder");
1390          }
1391          else
1392          {
1393             char port[10]; /* We save proxy ports as int but need a string here */
1394
1395             if (!err) err = map_block_killer(exports, "no-forwarder");
1396
1397             if (fwd->gateway_host != NULL)
1398             {
1399                char *socks_type = NULL;
1400
1401                switch (fwd->type)
1402                {
1403                   case SOCKS_4:
1404                      socks_type = "socks4";
1405                      break;
1406                   case SOCKS_4A:
1407                      socks_type = "socks4a";
1408                      break;
1409                   case SOCKS_5:
1410                      socks_type = "socks5";
1411                      break;
1412                   default:
1413                      log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1414                }
1415
1416                if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1417                if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1418                snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1419                if (!err) err = map(exports, "gateway-port", 1, port, 1);
1420             }
1421             else
1422             {
1423                if (!err) err = map_block_killer(exports, "socks-forwarder");
1424             }
1425
1426             if (fwd->forward_host != NULL)
1427             {
1428                if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1429                snprintf(port, sizeof(port), "%d", fwd->forward_port);
1430                if (!err) err = map(exports, "forward-port", 1, port, 1);
1431             }
1432             else
1433             {
1434                if (!err) err = map_block_killer(exports, "http-forwarder");
1435             }
1436          }
1437       }
1438
1439       free_http_request(url_to_query);
1440
1441       if (err || matches == NULL)
1442       {
1443          free_current_action(action);
1444          free_map(exports);
1445          return JB_ERR_MEMORY;
1446       }
1447
1448 #ifdef FEATURE_CGI_EDIT_ACTIONS
1449       if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1450       {
1451          err = map_block_killer(exports, "cgi-editor-is-disabled");
1452       }
1453 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1454
1455       /*
1456        * If zlib support is available, if no content filters
1457        * are enabled or if the prevent-compression action is enabled,
1458        * suppress the "compression could prevent filtering" warning.
1459        */
1460 #ifndef FEATURE_ZLIB
1461       if (!content_filters_enabled(action) ||
1462          (action->flags & ACTION_NO_COMPRESSION))
1463 #endif
1464       {
1465          if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1466       }
1467
1468       if (err || map(exports, "matches", 1, matches , 0))
1469       {
1470          free_current_action(action);
1471          free_map(exports);
1472          return JB_ERR_MEMORY;
1473       }
1474
1475       s = current_action_to_html(csp, action);
1476
1477       free_current_action(action);
1478
1479       if (map(exports, "final", 1, s, 0))
1480       {
1481          free_map(exports);
1482          return JB_ERR_MEMORY;
1483       }
1484    }
1485
1486    return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1487 }
1488
1489
1490 /*********************************************************************
1491  *
1492  * Function    :  cgi_robots_txt
1493  *
1494  * Description :  CGI function to return "/robots.txt".
1495  *
1496  * Parameters  :
1497  *          1  :  csp = Current client state (buffers, headers, etc...)
1498  *          2  :  rsp = http_response data structure for output
1499  *          3  :  parameters = map of cgi parameters
1500  *
1501  * CGI Parameters : None
1502  *
1503  * Returns     :  JB_ERR_OK on success
1504  *                JB_ERR_MEMORY on out-of-memory error.  
1505  *
1506  *********************************************************************/
1507 jb_err cgi_robots_txt(struct client_state *csp,
1508                       struct http_response *rsp,
1509                       const struct map *parameters)
1510 {
1511    char buf[100];
1512    jb_err err;
1513
1514    (void)csp;
1515    (void)parameters;
1516
1517    rsp->body = strdup(
1518       "# This is the Privoxy control interface.\n"
1519       "# It isn't very useful to index it, and you're likely to break stuff.\n"
1520       "# So go away!\n"
1521       "\n"
1522       "User-agent: *\n"
1523       "Disallow: /\n"
1524       "\n");
1525    if (rsp->body == NULL)
1526    {
1527       return JB_ERR_MEMORY;
1528    }
1529
1530    err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1531
1532    rsp->is_static = 1;
1533
1534    get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1535    if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1536
1537    return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1538 }
1539
1540
1541 /*********************************************************************
1542  *
1543  * Function    :  show_defines
1544  *
1545  * Description :  Add to a map the state od all conditional #defines
1546  *                used when building
1547  *
1548  * Parameters  :
1549  *          1  :  exports = map to extend
1550  *
1551  * Returns     :  JB_ERR_OK on success
1552  *                JB_ERR_MEMORY on out-of-memory error.  
1553  *
1554  *********************************************************************/
1555 static jb_err show_defines(struct map *exports)
1556 {
1557    jb_err err = JB_ERR_OK;
1558
1559 #ifdef FEATURE_ACCEPT_FILTER
1560    if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 1);
1561 #else /* ifndef FEATURE_ACCEPT_FILTER */
1562    if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 0);
1563 #endif /* ndef FEATURE_ACCEPT_FILTER */
1564
1565 #ifdef FEATURE_ACL
1566    if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1567 #else /* ifndef FEATURE_ACL */
1568    if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1569 #endif /* ndef FEATURE_ACL */
1570
1571 #ifdef FEATURE_CGI_EDIT_ACTIONS
1572    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1573 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1574    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1575 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1576
1577 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1578    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1579 #else /* ifndef FEATURE_CONNECTION_KEEP_ALIVE */
1580    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1581 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1582
1583 #ifdef FEATURE_CONNECTION_SHARING
1584    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 1);
1585 #else /* ifndef FEATURE_CONNECTION_SHARING */
1586    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 0);
1587 #endif /* ndef FEATURE_CONNECTION_SHARING */
1588
1589 #ifdef FEATURE_FAST_REDIRECTS
1590    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1591 #else /* ifndef FEATURE_FAST_REDIRECTS */
1592    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1593 #endif /* ndef FEATURE_FAST_REDIRECTS */
1594
1595 #ifdef FEATURE_FORCE_LOAD
1596    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1597    if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1598 #else /* ifndef FEATURE_FORCE_LOAD */
1599    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1600    if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1601 #endif /* ndef FEATURE_FORCE_LOAD */
1602
1603 #ifdef FEATURE_GRACEFUL_TERMINATION
1604    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1605 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1606    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1607 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1608
1609 #ifdef FEATURE_IMAGE_BLOCKING
1610    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1611 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1612    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1613 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1614
1615 #ifdef FEATURE_IMAGE_DETECT_MSIE
1616    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1617 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1618    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1619 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1620
1621 #ifdef HAVE_RFC2553
1622    if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1623 #else /* ifndef HAVE_RFC2553 */
1624    if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1625 #endif /* ndef HAVE_RFC2553 */
1626
1627 #ifdef FEATURE_NO_GIFS
1628    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1629 #else /* ifndef FEATURE_NO_GIFS */
1630    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1631 #endif /* ndef FEATURE_NO_GIFS */
1632
1633 #ifdef FEATURE_PTHREAD
1634    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1635 #else /* ifndef FEATURE_PTHREAD */
1636    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1637 #endif /* ndef FEATURE_PTHREAD */
1638
1639 #ifdef FEATURE_STATISTICS
1640    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1641 #else /* ifndef FEATURE_STATISTICS */
1642    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1643 #endif /* ndef FEATURE_STATISTICS */
1644
1645 #ifdef FEATURE_TOGGLE
1646    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1647 #else /* ifndef FEATURE_TOGGLE */
1648    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1649 #endif /* ndef FEATURE_TOGGLE */
1650
1651 #ifdef FEATURE_TRUST
1652    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1653 #else /* ifndef FEATURE_TRUST */
1654    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1655 #endif /* ndef FEATURE_TRUST */
1656
1657 #ifdef FEATURE_ZLIB
1658    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1659 #else /* ifndef FEATURE_ZLIB */
1660    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1661 #endif /* ndef FEATURE_ZLIB */
1662
1663 #ifdef STATIC_PCRE
1664    if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1665 #else /* ifndef STATIC_PCRE */
1666    if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1667 #endif /* ndef STATIC_PCRE */
1668
1669 #ifdef STATIC_PCRS
1670    if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1671 #else /* ifndef STATIC_PCRS */
1672    if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1673 #endif /* ndef STATIC_PCRS */
1674
1675    return err;
1676 }
1677
1678
1679 /*********************************************************************
1680  *
1681  * Function    :  show_rcs
1682  *
1683  * Description :  Create a string with the rcs info for all sourcefiles
1684  *
1685  * Parameters  :  None
1686  *
1687  * Returns     :  A string, or NULL on out-of-memory.
1688  *
1689  *********************************************************************/
1690 static char *show_rcs(void)
1691 {
1692    char *result = strdup("");
1693    char buf[BUFFER_SIZE];
1694
1695    /* Instead of including *all* dot h's in the project (thus creating a
1696     * tremendous amount of dependencies), I will concede to declaring them
1697     * as extern's.  This forces the developer to add to this list, but oh well.
1698     */
1699
1700 #define SHOW_RCS(__x)              \
1701    {                               \
1702       extern const char __x[];     \
1703       snprintf(buf, sizeof(buf), " %s\n", __x);   \
1704       string_append(&result, buf); \
1705    }
1706
1707    /* In alphabetical order */
1708    SHOW_RCS(actions_h_rcs)
1709    SHOW_RCS(actions_rcs)
1710 #ifdef AMIGA
1711    SHOW_RCS(amiga_h_rcs)
1712    SHOW_RCS(amiga_rcs)
1713 #endif /* def AMIGA */
1714    SHOW_RCS(cgi_h_rcs)
1715    SHOW_RCS(cgi_rcs)
1716 #ifdef FEATURE_CGI_EDIT_ACTIONS
1717    SHOW_RCS(cgiedit_h_rcs)
1718    SHOW_RCS(cgiedit_rcs)
1719 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1720    SHOW_RCS(cgisimple_h_rcs)
1721    SHOW_RCS(cgisimple_rcs)
1722 #ifdef __MINGW32__
1723    SHOW_RCS(cygwin_h_rcs)
1724 #endif
1725    SHOW_RCS(deanimate_h_rcs)
1726    SHOW_RCS(deanimate_rcs)
1727    SHOW_RCS(encode_h_rcs)
1728    SHOW_RCS(encode_rcs)
1729    SHOW_RCS(errlog_h_rcs)
1730    SHOW_RCS(errlog_rcs)
1731    SHOW_RCS(filters_h_rcs)
1732    SHOW_RCS(filters_rcs)
1733    SHOW_RCS(gateway_h_rcs)
1734    SHOW_RCS(gateway_rcs)
1735    SHOW_RCS(jbsockets_h_rcs)
1736    SHOW_RCS(jbsockets_rcs)
1737    SHOW_RCS(jcc_h_rcs)
1738    SHOW_RCS(jcc_rcs)
1739    SHOW_RCS(list_h_rcs)
1740    SHOW_RCS(list_rcs)
1741    SHOW_RCS(loadcfg_h_rcs)
1742    SHOW_RCS(loadcfg_rcs)
1743    SHOW_RCS(loaders_h_rcs)
1744    SHOW_RCS(loaders_rcs)
1745    SHOW_RCS(miscutil_h_rcs)
1746    SHOW_RCS(miscutil_rcs)
1747    SHOW_RCS(parsers_h_rcs)
1748    SHOW_RCS(parsers_rcs)
1749    SHOW_RCS(pcrs_rcs)
1750    SHOW_RCS(pcrs_h_rcs)
1751    SHOW_RCS(project_h_rcs)
1752    SHOW_RCS(ssplit_h_rcs)
1753    SHOW_RCS(ssplit_rcs)
1754    SHOW_RCS(urlmatch_h_rcs)
1755    SHOW_RCS(urlmatch_rcs)
1756 #ifdef _WIN32
1757 #ifndef _WIN_CONSOLE
1758    SHOW_RCS(w32log_h_rcs)
1759    SHOW_RCS(w32log_rcs)
1760    SHOW_RCS(w32res_h_rcs)
1761    SHOW_RCS(w32taskbar_h_rcs)
1762    SHOW_RCS(w32taskbar_rcs)
1763 #endif /* ndef _WIN_CONSOLE */
1764    SHOW_RCS(win32_h_rcs)
1765    SHOW_RCS(win32_rcs)
1766 #endif /* def _WIN32 */
1767
1768 #undef SHOW_RCS
1769
1770    return result;
1771
1772 }
1773
1774
1775 /*********************************************************************
1776  *
1777  * Function    :  cgi_show_file
1778  *
1779  * Description :  CGI function that shows the content of a
1780  *                configuration file.
1781  *
1782  * Parameters  :
1783  *          1  :  csp = Current client state (buffers, headers, etc...)
1784  *          2  :  rsp = http_response data structure for output
1785  *          3  :  parameters = map of cgi parameters
1786  *
1787  * CGI Parameters :
1788  *        file :  Which file to show.  Only first letter is checked,
1789  *                valid values are:
1790  *                - "a"ction file
1791  *                - "r"egex
1792  *                - "t"rust
1793  *                Default is to show menu and other information.
1794  *
1795  * Returns     :  JB_ERR_OK on success
1796  *                JB_ERR_MEMORY on out-of-memory error.  
1797  *
1798  *********************************************************************/
1799 static jb_err cgi_show_file(struct client_state *csp,
1800                             struct http_response *rsp,
1801                             const struct map *parameters)
1802 {
1803    unsigned i;
1804    const char * filename = NULL;
1805    char * file_description = NULL;
1806
1807    assert(csp);
1808    assert(rsp);
1809    assert(parameters);
1810
1811    switch (*(lookup(parameters, "file")))
1812    {
1813    case 'a':
1814       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1815       {
1816          filename = csp->actions_list[i]->filename;
1817          file_description = "Actions File";
1818       }
1819       break;
1820
1821    case 'f':
1822       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1823       {
1824          filename = csp->rlist[i]->filename;
1825          file_description = "Filter File";
1826       }
1827       break;
1828
1829 #ifdef FEATURE_TRUST
1830    case 't':
1831       if (csp->tlist)
1832       {
1833          filename = csp->tlist->filename;
1834          file_description = "Trust File";
1835       }
1836       break;
1837 #endif /* def FEATURE_TRUST */
1838    }
1839
1840    if (NULL != filename)
1841    {
1842       struct map *exports;
1843       char *s;
1844       jb_err err;
1845       size_t length;
1846
1847       exports = default_exports(csp, "show-status");
1848       if (NULL == exports)
1849       {
1850          return JB_ERR_MEMORY;
1851       }
1852
1853       if ( map(exports, "file-description", 1, file_description, 1)
1854         || map(exports, "filepath", 1, html_encode(filename), 0) )
1855       {
1856          free_map(exports);
1857          return JB_ERR_MEMORY;
1858       }
1859
1860       err = load_file(filename, &s, &length);
1861       if (JB_ERR_OK != err)
1862       {
1863          if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1864          {
1865             free_map(exports);
1866             return JB_ERR_MEMORY;
1867          }
1868       }
1869       else
1870       {
1871          s = html_encode_and_free_original(s);
1872          if (NULL == s)
1873          {
1874             return JB_ERR_MEMORY;
1875          }
1876
1877          if (map(exports, "contents", 1, s, 0))
1878          {
1879             free_map(exports);
1880             return JB_ERR_MEMORY;
1881          }
1882       }
1883
1884       return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1885    }
1886
1887    return JB_ERR_CGI_PARAMS;
1888 }
1889
1890  
1891 /*********************************************************************
1892  *
1893  * Function    :  load_file
1894  *
1895  * Description :  Loads a file into a buffer.
1896  *
1897  * Parameters  :
1898  *          1  :  filename = Name of the file to be loaded.
1899  *          2  :  buffer   = Used to return the file's content.
1900  *          3  :  length   = Used to return the size of the file.
1901  *
1902  * Returns     :  JB_ERR_OK in case of success,
1903  *                JB_ERR_FILE in case of ordinary file loading errors
1904  *                            (fseek() and ftell() errors are fatal)
1905  *                JB_ERR_MEMORY in case of out-of-memory.
1906  *
1907  *********************************************************************/
1908 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1909 {
1910    FILE *fp;
1911    long ret;
1912    jb_err err = JB_ERR_OK;
1913
1914    fp = fopen(filename, "rb");
1915    if (NULL == fp)
1916    {
1917       log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
1918       return JB_ERR_FILE;
1919    }
1920
1921    /* Get file length */
1922    if (fseek(fp, 0, SEEK_END))
1923    {
1924       log_error(LOG_LEVEL_FATAL,
1925          "Unexpected error while fseek()ing to the end of %s: %E",
1926          filename);
1927    }
1928    ret = ftell(fp);
1929    if (-1 == ret)
1930    {
1931       log_error(LOG_LEVEL_FATAL,
1932          "Unexpected ftell() error while loading %s: %E",
1933          filename);
1934    }
1935    *length = (size_t)ret;
1936
1937    /* Go back to the beginning. */
1938    if (fseek(fp, 0, SEEK_SET))
1939    {
1940       log_error(LOG_LEVEL_FATAL,
1941          "Unexpected error while fseek()ing to the beginning of %s: %E",
1942          filename);
1943    }
1944
1945    *buffer = (char *)zalloc(*length + 1);
1946    if (NULL == *buffer)
1947    {
1948       err = JB_ERR_MEMORY;
1949    }
1950    else if (!fread(*buffer, *length, 1, fp))
1951    {
1952       /*
1953        * May happen if the file size changes between fseek() and
1954        * fread(). If it does, we just log it and serve what we got.
1955        */
1956       log_error(LOG_LEVEL_ERROR,
1957          "Couldn't completely read file %s.", filename);
1958       err = JB_ERR_FILE;
1959    }
1960
1961    fclose(fp);
1962
1963    return err;
1964
1965 }
1966
1967
1968 /*
1969   Local Variables:
1970   tab-width: 3
1971   end:
1972 */