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