Add parse_time_header(), a wrapper around parse_header_time()
[privoxy.git] / cgisimple.c
1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.130 2014/10/18 11:26:31 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 == strstr(csp->actions_list[i]->filename, "standard.action"))
974             && (NULL != csp->config->actions_file_short[i]))
975          {
976 #ifdef HAVE_ACCESS
977             if (access(csp->config->actions_file[i], W_OK) == 0)
978             {
979 #endif /* def HAVE_ACCESS */
980                snprintf(buf, sizeof(buf), "&nbsp;&nbsp;<a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
981                if (!err) err = string_append(&s, buf);
982 #ifdef HAVE_ACCESS
983             }
984             else
985             {
986                if (!err) err = string_append(&s, "&nbsp;&nbsp;<strong>No write access.</strong>");
987             }
988 #endif /* def HAVE_ACCESS */
989          }
990 #endif
991
992          if (!err) err = string_append(&s, "</td></tr>\n");
993       }
994    }
995    if (*s != '\0')
996    {
997       if (!err) err = map(exports, "actions-filenames", 1, s, 0);
998    }
999    else
1000    {
1001       if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1002    }
1003
1004    /*
1005     * List all re_filterfiles in use, together with view options.
1006     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1007     */
1008    s = strdup("");
1009    for (i = 0; i < MAX_AF_FILES; i++)
1010    {
1011       if (csp->rlist[i] != NULL)
1012       {
1013          if (!err) err = string_append(&s, "<tr><td>");
1014          if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1015          snprintf(buf, sizeof(buf),
1016             "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&amp;index=%u\">View</a>", i);
1017          if (!err) err = string_append(&s, buf);
1018          if (!err) err = string_append(&s, "</td></tr>\n");
1019       }
1020    }
1021    if (*s != '\0')
1022    {
1023       if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1024    }
1025    else
1026    {
1027       if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1028       if (!err) err = map_block_killer(exports, "have-filterfile");
1029    }
1030
1031 #ifdef FEATURE_TRUST
1032    if (csp->tlist)
1033    {
1034       if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1035    }
1036    else
1037    {
1038       if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1039       if (!err) err = map_block_killer(exports, "have-trustfile");
1040    }
1041 #else
1042    if (!err) err = map_block_killer(exports, "trust-support");
1043 #endif /* ndef FEATURE_TRUST */
1044
1045 #ifdef FEATURE_CGI_EDIT_ACTIONS
1046    if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1047    {
1048       err = map_block_killer(exports, "cgi-editor-is-disabled");
1049    }
1050 #endif /* ndef CGI_EDIT_ACTIONS */
1051
1052    if (!err) err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
1053
1054    if (err)
1055    {
1056       free_map(exports);
1057       return JB_ERR_MEMORY;
1058    }
1059
1060    return template_fill_for_cgi(csp, "show-status", exports, rsp);
1061 }
1062
1063
1064 /*********************************************************************
1065  *
1066  * Function    :  cgi_show_url_info
1067  *
1068  * Description :  CGI function that determines and shows which actions
1069  *                Privoxy will perform for a given url, and which
1070  *                matches starting from the defaults have lead to that.
1071  *
1072  * Parameters  :
1073  *          1  :  csp = Current client state (buffers, headers, etc...)
1074  *          2  :  rsp = http_response data structure for output
1075  *          3  :  parameters = map of cgi parameters
1076  *
1077  * CGI Parameters :
1078  *            url : The url whose actions are to be determined.
1079  *                  If url is unset, the url-given conditional will be
1080  *                  set, so that all but the form can be suppressed in
1081  *                  the template.
1082  *
1083  * Returns     :  JB_ERR_OK on success
1084  *                JB_ERR_MEMORY on out-of-memory error.
1085  *
1086  *********************************************************************/
1087 jb_err cgi_show_url_info(struct client_state *csp,
1088                          struct http_response *rsp,
1089                          const struct map *parameters)
1090 {
1091    char *url_param;
1092    struct map *exports;
1093    char buf[150];
1094
1095    assert(csp);
1096    assert(rsp);
1097    assert(parameters);
1098
1099    if (NULL == (exports = default_exports(csp, "show-url-info")))
1100    {
1101       return JB_ERR_MEMORY;
1102    }
1103
1104    /*
1105     * Get the url= parameter (if present) and remove any leading/trailing spaces.
1106     */
1107    url_param = strdup_or_die(lookup(parameters, "url"));
1108    chomp(url_param);
1109
1110    /*
1111     * Handle prefixes.  4 possibilities:
1112     * 1) "http://" or "https://" prefix present and followed by URL - OK
1113     * 2) Only the "http://" or "https://" part is present, no URL - change
1114     *    to empty string so it will be detected later as "no URL".
1115     * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1116     *    "http://" prefix.
1117     * 4) Parameter not specified or is empty string - let this fall through
1118     *    for now, next block of code will handle it.
1119     */
1120    if (0 == strncmp(url_param, "http://", 7))
1121    {
1122       if (url_param[7] == '\0')
1123       {
1124          /*
1125           * Empty URL (just prefix).
1126           * Make it totally empty so it's caught by the next if ()
1127           */
1128          url_param[0] = '\0';
1129       }
1130    }
1131    else if (0 == strncmp(url_param, "https://", 8))
1132    {
1133       if (url_param[8] == '\0')
1134       {
1135          /*
1136           * Empty URL (just prefix).
1137           * Make it totally empty so it's caught by the next if ()
1138           */
1139          url_param[0] = '\0';
1140       }
1141    }
1142    else if ((url_param[0] != '\0')
1143       && ((NULL == strstr(url_param, "://")
1144             || (strstr(url_param, "://") > strstr(url_param, "/")))))
1145    {
1146       /*
1147        * No prefix or at least no prefix before
1148        * the first slash - assume http://
1149        */
1150       char *url_param_prefixed = strdup_or_die("http://");
1151
1152       if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1153       {
1154          free_map(exports);
1155          return JB_ERR_MEMORY;
1156       }
1157       url_param = url_param_prefixed;
1158    }
1159
1160    /*
1161     * Hide "toggle off" warning if Privoxy is toggled on.
1162     */
1163    if (
1164 #ifdef FEATURE_TOGGLE
1165        (global_toggle_state == 1) &&
1166 #endif /* def FEATURE_TOGGLE */
1167        map_block_killer(exports, "privoxy-is-toggled-off")
1168       )
1169    {
1170       freez(url_param);
1171       free_map(exports);
1172       return JB_ERR_MEMORY;
1173    }
1174
1175    if (url_param[0] == '\0')
1176    {
1177       /* URL paramater not specified, display query form only. */
1178       free(url_param);
1179       if (map_block_killer(exports, "url-given")
1180         || map(exports, "url", 1, "", 1))
1181       {
1182          free_map(exports);
1183          return JB_ERR_MEMORY;
1184       }
1185    }
1186    else
1187    {
1188       /* Given a URL, so query it. */
1189       jb_err err;
1190       char *matches;
1191       char *s;
1192       int hits = 0;
1193       struct file_list *fl;
1194       struct url_actions *b;
1195       struct http_request url_to_query[1];
1196       struct current_action_spec action[1];
1197       int i;
1198
1199       if (map(exports, "url", 1, html_encode(url_param), 0))
1200       {
1201          free(url_param);
1202          free_map(exports);
1203          return JB_ERR_MEMORY;
1204       }
1205
1206       init_current_action(action);
1207
1208       if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1209       {
1210          free_current_action(action);
1211          free(url_param);
1212          free_map(exports);
1213          return JB_ERR_MEMORY;
1214       }
1215
1216       memset(url_to_query, '\0', sizeof(url_to_query));
1217       err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1218       assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1219
1220       free(url_param);
1221
1222       if (err == JB_ERR_MEMORY)
1223       {
1224          free_http_request(url_to_query);
1225          free_current_action(action);
1226          free_map(exports);
1227          return JB_ERR_MEMORY;
1228       }
1229       else if (err)
1230       {
1231          /* Invalid URL */
1232
1233          err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1234          if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1235          if (!err) err = map_block_killer(exports, "valid-url");
1236
1237          free_current_action(action);
1238          free_http_request(url_to_query);
1239
1240          if (err)
1241          {
1242             free_map(exports);
1243             return JB_ERR_MEMORY;
1244          }
1245
1246          return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1247       }
1248
1249       /*
1250        * We have a warning about SSL paths.  Hide it for unencrypted sites.
1251        */
1252       if (!url_to_query->ssl)
1253       {
1254          if (map_block_killer(exports, "https"))
1255          {
1256             free_current_action(action);
1257             free_map(exports);
1258             free_http_request(url_to_query);
1259             return JB_ERR_MEMORY;
1260          }
1261       }
1262
1263       matches = strdup_or_die("<table summary=\"\" class=\"transparent\">");
1264
1265       for (i = 0; i < MAX_AF_FILES; i++)
1266       {
1267          if (NULL == csp->config->actions_file_short[i]
1268              || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1269
1270          b = NULL;
1271          hits = 1;
1272          if ((fl = csp->actions_list[i]) != NULL)
1273          {
1274             if ((b = fl->f) != NULL)
1275             {
1276                /* FIXME: Hardcoded HTML! */
1277                string_append(&matches, "<tr><th>In file: ");
1278                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1279                snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&amp;index=%d\">", i);
1280                string_append(&matches, buf);
1281                string_append(&matches, "View</a>");
1282 #ifdef FEATURE_CGI_EDIT_ACTIONS
1283                if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1284                {
1285 #ifdef HAVE_ACCESS
1286                   if (access(csp->config->actions_file[i], W_OK) == 0)
1287                   {
1288 #endif /* def HAVE_ACCESS */
1289                      snprintf(buf, sizeof(buf),
1290                         " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1291                      string_append(&matches, buf);
1292                      string_append(&matches, "Edit</a>");
1293 #ifdef HAVE_ACCESS
1294                   }
1295                   else
1296                   {
1297                      string_append(&matches, " <strong>No write access.</strong>");
1298                   }
1299 #endif /* def HAVE_ACCESS */
1300                }
1301 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1302
1303                string_append(&matches, "</th></tr>\n");
1304
1305                hits = 0;
1306                b = b->next;
1307             }
1308          }
1309
1310          for (; (b != NULL) && (matches != NULL); b = b->next)
1311          {
1312             if (url_match(b->url, url_to_query))
1313             {
1314                string_append(&matches, "<tr><td>{");
1315                string_join  (&matches, actions_to_html(csp, b->action));
1316                string_append(&matches, " }<br>\n<code>");
1317                string_join  (&matches, html_encode(b->url->spec));
1318                string_append(&matches, "</code></td></tr>\n");
1319
1320                if (merge_current_action(action, b->action))
1321                {
1322                   freez(matches);
1323                   free_http_request(url_to_query);
1324                   free_current_action(action);
1325                   free_map(exports);
1326                   return JB_ERR_MEMORY;
1327                }
1328                hits++;
1329             }
1330          }
1331
1332          if (!hits)
1333          {
1334             string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1335          }
1336       }
1337       string_append(&matches, "</table>\n");
1338
1339       /*
1340        * XXX: Kludge to make sure the "Forward settings" section
1341        * shows what forward-override{} would do with the requested URL.
1342        * No one really cares how the CGI request would be forwarded
1343        * if it wasn't intercepted as CGI request in the first place.
1344        *
1345        * From here on the action bitmask will no longer reflect
1346        * the real url (http://config.privoxy.org/show-url-info?url=.*),
1347        * but luckily it's no longer required later on anyway.
1348        */
1349       free_current_action(csp->action);
1350       get_url_actions(csp, url_to_query);
1351
1352       /*
1353        * Fill in forwarding settings.
1354        *
1355        * The possibilities are:
1356        *  - no forwarding
1357        *  - http forwarding only
1358        *  - socks4(a) forwarding only
1359        *  - socks4(a) and http forwarding.
1360        *
1361        * XXX: Parts of this code could be reused for the
1362        * "forwarding-failed" template which currently doesn't
1363        * display the proxy port and an eventual second forwarder.
1364        */
1365       {
1366          const struct forward_spec *fwd = forward_url(csp, url_to_query);
1367
1368          if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1369          {
1370             if (!err) err = map_block_killer(exports, "socks-forwarder");
1371             if (!err) err = map_block_killer(exports, "http-forwarder");
1372          }
1373          else
1374          {
1375             char port[10]; /* We save proxy ports as int but need a string here */
1376
1377             if (!err) err = map_block_killer(exports, "no-forwarder");
1378
1379             if (fwd->gateway_host != NULL)
1380             {
1381                char *socks_type = NULL;
1382
1383                switch (fwd->type)
1384                {
1385                   case SOCKS_4:
1386                      socks_type = "socks4";
1387                      break;
1388                   case SOCKS_4A:
1389                      socks_type = "socks4a";
1390                      break;
1391                   case SOCKS_5:
1392                      socks_type = "socks5";
1393                      break;
1394                   case SOCKS_5T:
1395                      socks_type = "socks5t";
1396                      break;
1397                   default:
1398                      log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1399                }
1400
1401                if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1402                if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1403                snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1404                if (!err) err = map(exports, "gateway-port", 1, port, 1);
1405             }
1406             else
1407             {
1408                if (!err) err = map_block_killer(exports, "socks-forwarder");
1409             }
1410
1411             if (fwd->forward_host != NULL)
1412             {
1413                if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1414                snprintf(port, sizeof(port), "%d", fwd->forward_port);
1415                if (!err) err = map(exports, "forward-port", 1, port, 1);
1416             }
1417             else
1418             {
1419                if (!err) err = map_block_killer(exports, "http-forwarder");
1420             }
1421          }
1422       }
1423
1424       free_http_request(url_to_query);
1425
1426       if (err || matches == NULL)
1427       {
1428          free_current_action(action);
1429          free_map(exports);
1430          return JB_ERR_MEMORY;
1431       }
1432
1433 #ifdef FEATURE_CGI_EDIT_ACTIONS
1434       if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1435       {
1436          err = map_block_killer(exports, "cgi-editor-is-disabled");
1437       }
1438 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1439
1440       /*
1441        * If zlib support is available, if no content filters
1442        * are enabled or if the prevent-compression action is enabled,
1443        * suppress the "compression could prevent filtering" warning.
1444        */
1445 #ifndef FEATURE_ZLIB
1446       if (!content_filters_enabled(action) ||
1447          (action->flags & ACTION_NO_COMPRESSION))
1448 #endif
1449       {
1450          if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1451       }
1452
1453       if (err || map(exports, "matches", 1, matches , 0))
1454       {
1455          free_current_action(action);
1456          free_map(exports);
1457          return JB_ERR_MEMORY;
1458       }
1459
1460       s = current_action_to_html(csp, action);
1461
1462       free_current_action(action);
1463
1464       if (map(exports, "final", 1, s, 0))
1465       {
1466          free_map(exports);
1467          return JB_ERR_MEMORY;
1468       }
1469    }
1470
1471    return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1472 }
1473
1474
1475 /*********************************************************************
1476  *
1477  * Function    :  cgi_robots_txt
1478  *
1479  * Description :  CGI function to return "/robots.txt".
1480  *
1481  * Parameters  :
1482  *          1  :  csp = Current client state (buffers, headers, etc...)
1483  *          2  :  rsp = http_response data structure for output
1484  *          3  :  parameters = map of cgi parameters
1485  *
1486  * CGI Parameters : None
1487  *
1488  * Returns     :  JB_ERR_OK on success
1489  *                JB_ERR_MEMORY on out-of-memory error.
1490  *
1491  *********************************************************************/
1492 jb_err cgi_robots_txt(struct client_state *csp,
1493                       struct http_response *rsp,
1494                       const struct map *parameters)
1495 {
1496    char buf[100];
1497    jb_err err;
1498
1499    (void)csp;
1500    (void)parameters;
1501
1502    rsp->body = strdup_or_die(
1503       "# This is the Privoxy control interface.\n"
1504       "# It isn't very useful to index it, and you're likely to break stuff.\n"
1505       "# So go away!\n"
1506       "\n"
1507       "User-agent: *\n"
1508       "Disallow: /\n"
1509       "\n");
1510
1511    err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1512
1513    rsp->is_static = 1;
1514
1515    get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1516    if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1517
1518    return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1519 }
1520
1521
1522 /*********************************************************************
1523  *
1524  * Function    :  show_defines
1525  *
1526  * Description :  Add to a map the state od all conditional #defines
1527  *                used when building
1528  *
1529  * Parameters  :
1530  *          1  :  exports = map to extend
1531  *
1532  * Returns     :  JB_ERR_OK on success
1533  *                JB_ERR_MEMORY on out-of-memory error.
1534  *
1535  *********************************************************************/
1536 static jb_err show_defines(struct map *exports)
1537 {
1538    jb_err err = JB_ERR_OK;
1539    int i;
1540    struct feature {
1541       const char name[31];
1542       const unsigned char is_available;
1543    };
1544
1545    static const struct feature features[] = {
1546       {
1547          "FEATURE_ACCEPT_FILTER",
1548 #ifdef FEATURE_ACCEPT_FILTER
1549          1,
1550 #else
1551          0,
1552 #endif
1553       },
1554       {
1555          "FEATURE_ACL",
1556 #ifdef FEATURE_ACL
1557          1,
1558 #else
1559          0,
1560 #endif
1561       },
1562       {
1563          "FEATURE_CGI_EDIT_ACTIONS",
1564 #ifdef FEATURE_CGI_EDIT_ACTIONS
1565          1,
1566 #else
1567          0,
1568 #endif
1569       },
1570       {
1571          "FEATURE_COMPRESSION",
1572 #ifdef FEATURE_COMPRESSION
1573          1,
1574 #else
1575          0,
1576 #endif
1577       },
1578       {
1579          "FEATURE_CONNECTION_KEEP_ALIVE",
1580 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1581          1,
1582 #else
1583          0,
1584 #endif
1585       },
1586       {
1587          "FEATURE_CONNECTION_SHARING",
1588 #ifdef FEATURE_CONNECTION_SHARING
1589          1,
1590 #else
1591          0,
1592 #endif
1593       },
1594       {
1595          "FEATURE_FAST_REDIRECTS",
1596 #ifdef FEATURE_FAST_REDIRECTS
1597          1,
1598 #else
1599          0,
1600 #endif
1601       },
1602       {
1603          "FEATURE_FORCE_LOAD",
1604 #ifdef FEATURE_FORCE_LOAD
1605          1,
1606 #else
1607          0,
1608 #endif
1609       },
1610       {
1611          "FEATURE_GRACEFUL_TERMINATION",
1612 #ifdef FEATURE_GRACEFUL_TERMINATION
1613          1,
1614 #else
1615          0,
1616 #endif
1617       },
1618       {
1619          "FEATURE_IMAGE_BLOCKING",
1620 #ifdef FEATURE_IMAGE_BLOCKING
1621          1,
1622 #else
1623          0,
1624 #endif
1625       },
1626       {
1627          "FEATURE_IMAGE_DETECT_MSIE",
1628 #ifdef FEATURE_IMAGE_DETECT_MSIE
1629          1,
1630 #else
1631          0,
1632 #endif
1633       },
1634       {
1635          "FEATURE_IPV6_SUPPORT",
1636 #ifdef HAVE_RFC2553
1637          1,
1638 #else
1639          0,
1640 #endif
1641       },
1642       {
1643          "FEATURE_NO_GIFS",
1644 #ifdef FEATURE_NO_GIFS
1645          1,
1646 #else
1647          0,
1648 #endif
1649       },
1650       {
1651          "FEATURE_PTHREAD",
1652 #ifdef FEATURE_PTHREAD
1653          1,
1654 #else
1655          0,
1656 #endif
1657       },
1658       {
1659          "FEATURE_STATISTICS",
1660 #ifdef FEATURE_STATISTICS
1661          1,
1662 #else
1663          0,
1664 #endif
1665       },
1666       {
1667          "FEATURE_STRPTIME_SANITY_CHECKS",
1668 #ifdef FEATURE_STRPTIME_SANITY_CHECKS
1669          1,
1670 #else
1671          0,
1672 #endif
1673       },
1674       {
1675          "FEATURE_TOGGLE",
1676 #ifdef FEATURE_TOGGLE
1677          1,
1678 #else
1679          0,
1680 #endif
1681       },
1682       {
1683          "FEATURE_TRUST",
1684 #ifdef FEATURE_TRUST
1685          1,
1686 #else
1687          0,
1688 #endif
1689       },
1690       {
1691          "FEATURE_ZLIB",
1692 #ifdef FEATURE_ZLIB
1693          1,
1694 #else
1695          0,
1696 #endif
1697       },
1698       {
1699          "FEATURE_DYNAMIC_PCRE",
1700 #ifdef FEATURE_DYNAMIC_PCRE
1701          1,
1702 #else
1703          0,
1704 #endif
1705       }
1706    };
1707
1708    for (i = 0; i < SZ(features); i++)
1709    {
1710       err = map_conditional(exports, features[i].name, features[i].is_available);
1711       if (err)
1712       {
1713          break;
1714       }
1715    }
1716
1717    return err;
1718
1719 }
1720
1721
1722 /*********************************************************************
1723  *
1724  * Function    :  show_rcs
1725  *
1726  * Description :  Create a string with the rcs info for all sourcefiles
1727  *
1728  * Parameters  :  None
1729  *
1730  * Returns     :  A string, or NULL on out-of-memory.
1731  *
1732  *********************************************************************/
1733 static char *show_rcs(void)
1734 {
1735    char *result = strdup_or_die("");
1736    char buf[BUFFER_SIZE];
1737
1738    /* Instead of including *all* dot h's in the project (thus creating a
1739     * tremendous amount of dependencies), I will concede to declaring them
1740     * as extern's.  This forces the developer to add to this list, but oh well.
1741     */
1742
1743 #define SHOW_RCS(__x)              \
1744    {                               \
1745       extern const char __x[];     \
1746       snprintf(buf, sizeof(buf), " %s\n", __x);   \
1747       string_append(&result, buf); \
1748    }
1749
1750    /* In alphabetical order */
1751    SHOW_RCS(actions_h_rcs)
1752    SHOW_RCS(actions_rcs)
1753 #ifdef AMIGA
1754    SHOW_RCS(amiga_h_rcs)
1755    SHOW_RCS(amiga_rcs)
1756 #endif /* def AMIGA */
1757    SHOW_RCS(cgi_h_rcs)
1758    SHOW_RCS(cgi_rcs)
1759 #ifdef FEATURE_CGI_EDIT_ACTIONS
1760    SHOW_RCS(cgiedit_h_rcs)
1761    SHOW_RCS(cgiedit_rcs)
1762 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1763    SHOW_RCS(cgisimple_h_rcs)
1764    SHOW_RCS(cgisimple_rcs)
1765 #ifdef __MINGW32__
1766    SHOW_RCS(cygwin_h_rcs)
1767 #endif
1768    SHOW_RCS(deanimate_h_rcs)
1769    SHOW_RCS(deanimate_rcs)
1770    SHOW_RCS(encode_h_rcs)
1771    SHOW_RCS(encode_rcs)
1772    SHOW_RCS(errlog_h_rcs)
1773    SHOW_RCS(errlog_rcs)
1774    SHOW_RCS(filters_h_rcs)
1775    SHOW_RCS(filters_rcs)
1776    SHOW_RCS(gateway_h_rcs)
1777    SHOW_RCS(gateway_rcs)
1778    SHOW_RCS(jbsockets_h_rcs)
1779    SHOW_RCS(jbsockets_rcs)
1780    SHOW_RCS(jcc_h_rcs)
1781    SHOW_RCS(jcc_rcs)
1782    SHOW_RCS(list_h_rcs)
1783    SHOW_RCS(list_rcs)
1784    SHOW_RCS(loadcfg_h_rcs)
1785    SHOW_RCS(loadcfg_rcs)
1786    SHOW_RCS(loaders_h_rcs)
1787    SHOW_RCS(loaders_rcs)
1788    SHOW_RCS(miscutil_h_rcs)
1789    SHOW_RCS(miscutil_rcs)
1790    SHOW_RCS(parsers_h_rcs)
1791    SHOW_RCS(parsers_rcs)
1792    SHOW_RCS(pcrs_rcs)
1793    SHOW_RCS(pcrs_h_rcs)
1794    SHOW_RCS(project_h_rcs)
1795    SHOW_RCS(ssplit_h_rcs)
1796    SHOW_RCS(ssplit_rcs)
1797    SHOW_RCS(urlmatch_h_rcs)
1798    SHOW_RCS(urlmatch_rcs)
1799 #ifdef _WIN32
1800 #ifndef _WIN_CONSOLE
1801    SHOW_RCS(w32log_h_rcs)
1802    SHOW_RCS(w32log_rcs)
1803    SHOW_RCS(w32res_h_rcs)
1804    SHOW_RCS(w32taskbar_h_rcs)
1805    SHOW_RCS(w32taskbar_rcs)
1806 #endif /* ndef _WIN_CONSOLE */
1807    SHOW_RCS(win32_h_rcs)
1808    SHOW_RCS(win32_rcs)
1809 #endif /* def _WIN32 */
1810
1811 #undef SHOW_RCS
1812
1813    return result;
1814
1815 }
1816
1817
1818 /*********************************************************************
1819  *
1820  * Function    :  cgi_show_file
1821  *
1822  * Description :  CGI function that shows the content of a
1823  *                configuration file.
1824  *
1825  * Parameters  :
1826  *          1  :  csp = Current client state (buffers, headers, etc...)
1827  *          2  :  rsp = http_response data structure for output
1828  *          3  :  parameters = map of cgi parameters
1829  *
1830  * CGI Parameters :
1831  *        file :  Which file to show.  Only first letter is checked,
1832  *                valid values are:
1833  *                - "a"ction file
1834  *                - "r"egex
1835  *                - "t"rust
1836  *                Default is to show menu and other information.
1837  *
1838  * Returns     :  JB_ERR_OK on success
1839  *                JB_ERR_MEMORY on out-of-memory error.
1840  *
1841  *********************************************************************/
1842 static jb_err cgi_show_file(struct client_state *csp,
1843                             struct http_response *rsp,
1844                             const struct map *parameters)
1845 {
1846    unsigned i;
1847    const char * filename = NULL;
1848    char * file_description = NULL;
1849
1850    assert(csp);
1851    assert(rsp);
1852    assert(parameters);
1853
1854    switch (*(lookup(parameters, "file")))
1855    {
1856    case 'a':
1857       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1858       {
1859          filename = csp->actions_list[i]->filename;
1860          file_description = "Actions File";
1861       }
1862       break;
1863
1864    case 'f':
1865       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1866       {
1867          filename = csp->rlist[i]->filename;
1868          file_description = "Filter File";
1869       }
1870       break;
1871
1872 #ifdef FEATURE_TRUST
1873    case 't':
1874       if (csp->tlist)
1875       {
1876          filename = csp->tlist->filename;
1877          file_description = "Trust File";
1878       }
1879       break;
1880 #endif /* def FEATURE_TRUST */
1881    }
1882
1883    if (NULL != filename)
1884    {
1885       struct map *exports;
1886       char *s;
1887       jb_err err;
1888       size_t length;
1889
1890       exports = default_exports(csp, "show-status");
1891       if (NULL == exports)
1892       {
1893          return JB_ERR_MEMORY;
1894       }
1895
1896       if (map(exports, "file-description", 1, file_description, 1)
1897         || map(exports, "filepath", 1, html_encode(filename), 0))
1898       {
1899          free_map(exports);
1900          return JB_ERR_MEMORY;
1901       }
1902
1903       err = load_file(filename, &s, &length);
1904       if (JB_ERR_OK != err)
1905       {
1906          if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1907          {
1908             free_map(exports);
1909             return JB_ERR_MEMORY;
1910          }
1911       }
1912       else
1913       {
1914          s = html_encode_and_free_original(s);
1915          if (NULL == s)
1916          {
1917             free_map(exports);
1918             return JB_ERR_MEMORY;
1919          }
1920
1921          if (map(exports, "contents", 1, s, 0))
1922          {
1923             free_map(exports);
1924             return JB_ERR_MEMORY;
1925          }
1926       }
1927
1928       return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1929    }
1930
1931    return JB_ERR_CGI_PARAMS;
1932 }
1933
1934
1935 /*********************************************************************
1936  *
1937  * Function    :  load_file
1938  *
1939  * Description :  Loads a file into a buffer.
1940  *
1941  * Parameters  :
1942  *          1  :  filename = Name of the file to be loaded.
1943  *          2  :  buffer   = Used to return the file's content.
1944  *          3  :  length   = Used to return the size of the file.
1945  *
1946  * Returns     :  JB_ERR_OK in case of success,
1947  *                JB_ERR_FILE in case of ordinary file loading errors
1948  *                            (fseek() and ftell() errors are fatal)
1949  *                JB_ERR_MEMORY in case of out-of-memory.
1950  *
1951  *********************************************************************/
1952 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1953 {
1954    FILE *fp;
1955    long ret;
1956    jb_err err = JB_ERR_OK;
1957
1958    fp = fopen(filename, "rb");
1959    if (NULL == fp)
1960    {
1961       log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
1962       return JB_ERR_FILE;
1963    }
1964
1965    /* Get file length */
1966    if (fseek(fp, 0, SEEK_END))
1967    {
1968       log_error(LOG_LEVEL_FATAL,
1969          "Unexpected error while fseek()ing to the end of %s: %E",
1970          filename);
1971    }
1972    ret = ftell(fp);
1973    if (-1 == ret)
1974    {
1975       log_error(LOG_LEVEL_FATAL,
1976          "Unexpected ftell() error while loading %s: %E",
1977          filename);
1978    }
1979    *length = (size_t)ret;
1980
1981    /* Go back to the beginning. */
1982    if (fseek(fp, 0, SEEK_SET))
1983    {
1984       log_error(LOG_LEVEL_FATAL,
1985          "Unexpected error while fseek()ing to the beginning of %s: %E",
1986          filename);
1987    }
1988
1989    *buffer = (char *)zalloc(*length + 1);
1990    if (NULL == *buffer)
1991    {
1992       err = JB_ERR_MEMORY;
1993    }
1994    else if (1 != fread(*buffer, *length, 1, fp))
1995    {
1996       /*
1997        * May theoretically happen if the file size changes between
1998        * fseek() and fread() because it's edited in-place. Privoxy
1999        * and common text editors don't do that, thus we just fail.
2000        */
2001       log_error(LOG_LEVEL_ERROR,
2002          "Couldn't completely read file %s.", filename);
2003       freez(*buffer);
2004       err = JB_ERR_FILE;
2005    }
2006
2007    fclose(fp);
2008
2009    return err;
2010
2011 }
2012
2013
2014 /*
2015   Local Variables:
2016   tab-width: 3
2017   end:
2018 */