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