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