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