In cgi_send_user_manual(), log when rejecting a file name due to '/' or '..'
[privoxy.git] / cgisimple.c
1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.101 2011/02/14 16:04:55 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-2010 the
13  *                Privoxy team. http://www.privoxy.org/
14  *
15  *                Based on the Internet Junkbuster originally written
16  *                by and Copyright (C) 1997 Anonymous Coders and 
17  *                Junkbusters Corporation.  http://www.junkbusters.com
18  *
19  *                This program is free software; you can redistribute it 
20  *                and/or modify it under the terms of the GNU General
21  *                Public License as published by the Free Software
22  *                Foundation; either version 2 of the License, or (at
23  *                your option) any later version.
24  *
25  *                This program is distributed in the hope that it will
26  *                be useful, but WITHOUT ANY WARRANTY; without even the
27  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
28  *                PARTICULAR PURPOSE.  See the GNU General Public
29  *                License for more details.
30  *
31  *                The GNU General Public License should be included with
32  *                this file.  If not, you can view it at
33  *                http://www.gnu.org/copyleft/gpl.html
34  *                or write to the Free Software Foundation, Inc., 59
35  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36  *
37  **********************************************************************/
38
39
40 #include "config.h"
41
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <stdlib.h>
45 #include <ctype.h>
46 #include <string.h>
47 #include <assert.h>
48
49 #ifdef HAVE_ACCESS
50 #include <unistd.h>
51 #endif /* def HAVE_ACCESS */
52
53 #include "project.h"
54 #include "cgi.h"
55 #include "cgisimple.h"
56 #include "list.h"
57 #include "encode.h"
58 #include "jcc.h"
59 #include "filters.h"
60 #include "actions.h"
61 #include "miscutil.h"
62 #include "loadcfg.h"
63 #include "parsers.h"
64 #include "urlmatch.h"
65 #include "errlog.h"
66
67 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
68
69 static char *show_rcs(void);
70 static jb_err show_defines(struct map *exports);
71 static jb_err cgi_show_file(struct client_state *csp,
72                             struct http_response *rsp,
73                             const struct map *parameters);
74 static jb_err load_file(const char *filename, char **buffer, size_t *length);
75
76 /*********************************************************************
77  *
78  * Function    :  cgi_default
79  *
80  * Description :  CGI function that is called for the CGI_SITE_1_HOST
81  *                and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
82  *                Boring - only exports the default exports.
83  *               
84  * Parameters  :
85  *          1  :  csp = Current client state (buffers, headers, etc...)
86  *          2  :  rsp = http_response data structure for output
87  *          3  :  parameters = map of cgi parameters
88  *
89  * CGI Parameters : none
90  *
91  * Returns     :  JB_ERR_OK on success
92  *                JB_ERR_MEMORY on out-of-memory
93  *
94  *********************************************************************/
95 jb_err cgi_default(struct client_state *csp,
96                    struct http_response *rsp,
97                    const struct map *parameters)
98 {
99    struct map *exports;
100
101    (void)parameters;
102
103    assert(csp);
104    assert(rsp);
105
106    if (NULL == (exports = default_exports(csp, "")))
107    {
108       return JB_ERR_MEMORY;
109    }
110
111    return template_fill_for_cgi(csp, "default", exports, rsp);
112 }
113
114
115 /*********************************************************************
116  *
117  * Function    :  cgi_error_404
118  *
119  * Description :  CGI function that is called if an unknown action was
120  *                given.
121  *               
122  * Parameters  :
123  *          1  :  csp = Current client state (buffers, headers, etc...)
124  *          2  :  rsp = http_response data structure for output
125  *          3  :  parameters = map of cgi parameters
126  *
127  * CGI Parameters : none
128  *
129  * Returns     :  JB_ERR_OK on success
130  *                JB_ERR_MEMORY on out-of-memory error.  
131  *
132  *********************************************************************/
133 jb_err cgi_error_404(struct client_state *csp,
134                      struct http_response *rsp,
135                      const struct map *parameters)
136 {
137    struct map *exports;
138
139    assert(csp);
140    assert(rsp);
141    assert(parameters);
142
143    if (NULL == (exports = default_exports(csp, NULL)))
144    {
145       return JB_ERR_MEMORY;
146    }
147
148    rsp->status = strdup("404 Privoxy configuration page not found");
149    if (rsp->status == NULL)
150    {
151       free_map(exports);
152       return JB_ERR_MEMORY;
153    }
154
155    return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
156 }
157
158
159 #ifdef FEATURE_GRACEFUL_TERMINATION
160 /*********************************************************************
161  *
162  * Function    :  cgi_die
163  *
164  * Description :  CGI function to shut down Privoxy.
165  *                NOTE: Turning this on in a production build
166  *                would be a BAD idea.  An EXTREMELY BAD idea.
167  *                In short, don't do it.
168  *               
169  * Parameters  :
170  *          1  :  csp = Current client state (buffers, headers, etc...)
171  *          2  :  rsp = http_response data structure for output
172  *          3  :  parameters = map of cgi parameters
173  *
174  * CGI Parameters : none
175  *
176  * Returns     :  JB_ERR_OK on success
177  *                JB_ERR_MEMORY on out-of-memory error.  
178  *
179  *********************************************************************/
180 jb_err cgi_die (struct client_state *csp,
181                 struct http_response *rsp,
182                 const struct map *parameters)
183 {
184    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 (0 == strncmpic(csp->config->usermanual, "http://", 7))
694    {
695       log_error(LOG_LEVEL_CGI, "Request for local user-manual "
696          "received while user-manual delivery is disabled.");
697       return cgi_error_404(csp, rsp, parameters);
698    }
699
700    if (!parameters->first)
701    {
702       /* requested http://p.p/user-manual (without trailing slash) */
703       return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
704    }
705
706    get_string_param(parameters, "file", &filename);
707    if (filename == NULL)
708    {
709       /* It's '/' so serve the index.html if there is one.  */
710       filename = "index.html";
711    }
712    else if (NULL != strchr(filename, '/') || NULL != strstr(filename, ".."))
713    {
714       /*
715        * We currently only support a flat file
716        * hierachy for the documentation.
717        */
718       log_error(LOG_LEVEL_ERROR,
719          "Rejecting the request to serve '%s' as it contains '/' or '..'",
720          filename);
721       return JB_ERR_CGI_PARAMS;
722    }
723
724    full_path = make_path(csp->config->usermanual, filename);
725    if (full_path == NULL)
726    {
727       return JB_ERR_MEMORY;
728    }
729
730    err = load_file(full_path, &rsp->body, &rsp->content_length);
731    if (JB_ERR_OK != err)
732    {
733       assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err));
734       if (JB_ERR_FILE == err)
735       {
736          err = cgi_error_no_template(csp, rsp, full_path);
737       }
738       freez(full_path);
739       return err;
740    }
741    freez(full_path);
742
743    /* Guess correct Content-Type based on the filename's ending */
744    if (filename)
745    {
746       length = strlen(filename);
747    }
748    else
749    {
750       length = 0;
751    } 
752    if((length>=4) && !strcmp(&filename[length-4], ".css"))
753    {
754       err = enlist(rsp->headers, "Content-Type: text/css");
755    }
756    else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
757    {
758       err = enlist(rsp->headers, "Content-Type: image/jpeg");
759    }
760    else
761    {
762       err = enlist(rsp->headers, "Content-Type: text/html");
763    }
764
765    return err;
766 }
767
768
769 /*********************************************************************
770  *
771  * Function    :  cgi_show_version
772  *
773  * Description :  CGI function that returns a a web page describing the
774  *                file versions of Privoxy.
775  *
776  * Parameters  :
777  *          1  :  csp = Current client state (buffers, headers, etc...)
778  *          2  :  rsp = http_response data structure for output
779  *          3  :  parameters = map of cgi parameters
780  *
781  * CGI Parameters : none
782  *
783  * Returns     :  JB_ERR_OK on success
784  *                JB_ERR_MEMORY on out-of-memory error.  
785  *
786  *********************************************************************/
787 jb_err cgi_show_version(struct client_state *csp,
788                         struct http_response *rsp,
789                         const struct map *parameters)
790 {
791    struct map *exports;
792
793    assert(csp);
794    assert(rsp);
795    assert(parameters);
796
797    if (NULL == (exports = default_exports(csp, "show-version")))
798    {
799       return JB_ERR_MEMORY;
800    }
801
802    if (map(exports, "sourceversions", 1, show_rcs(), 0))
803    {
804       free_map(exports);
805       return JB_ERR_MEMORY;
806    }
807
808    return template_fill_for_cgi(csp, "show-version", exports, rsp);
809 }
810
811
812 /*********************************************************************
813  *
814  * Function    :  cgi_show_status
815  *
816  * Description :  CGI function that returns a web page describing the
817  *                current status of Privoxy.
818  *
819  * Parameters  :
820  *          1  :  csp = Current client state (buffers, headers, etc...)
821  *          2  :  rsp = http_response data structure for output
822  *          3  :  parameters = map of cgi parameters
823  *
824  * CGI Parameters :
825  *        file :  Which file to show.  Only first letter is checked,
826  *                valid values are:
827  *                - "a"ction file
828  *                - "r"egex
829  *                - "t"rust
830  *                Default is to show menu and other information.
831  *
832  * Returns     :  JB_ERR_OK on success
833  *                JB_ERR_MEMORY on out-of-memory error.  
834  *
835  *********************************************************************/
836 jb_err cgi_show_status(struct client_state *csp,
837                        struct http_response *rsp,
838                        const struct map *parameters)
839 {
840    char *s = NULL;
841    unsigned i;
842    int j;
843
844    char buf[BUFFER_SIZE];
845 #ifdef FEATURE_STATISTICS
846    float perc_rej;   /* Percentage of http requests rejected */
847    int local_urls_read;
848    int local_urls_rejected;
849 #endif /* ndef FEATURE_STATISTICS */
850    jb_err err = JB_ERR_OK;
851
852    struct map *exports;
853
854    assert(csp);
855    assert(rsp);
856    assert(parameters);
857
858    if ('\0' != *(lookup(parameters, "file")))
859    {
860       return cgi_show_file(csp, rsp, parameters);
861    }
862
863    if (NULL == (exports = default_exports(csp, "show-status")))
864    {
865       return JB_ERR_MEMORY;
866    }
867
868    s = strdup("");
869    for (j = 0; (s != NULL) && (j < Argc); j++)
870    {
871       if (!err) err = string_join  (&s, html_encode(Argv[j]));
872       if (!err) err = string_append(&s, " ");
873    }
874    if (!err) err = map(exports, "invocation", 1, s, 0);
875
876    if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
877    if (!err) err = show_defines(exports);
878
879    if (err) 
880    {
881       free_map(exports);
882       return JB_ERR_MEMORY;
883    }
884
885 #ifdef FEATURE_STATISTICS
886    local_urls_read     = urls_read;
887    local_urls_rejected = urls_rejected;
888
889    /*
890     * Need to alter the stats not to include the fetch of this
891     * page.
892     *
893     * Can't do following thread safely! doh!
894     *
895     * urls_read--;
896     * urls_rejected--; * This will be incremented subsequently *
897     */
898
899    if (local_urls_read == 0)
900    {
901       if (!err) err = map_block_killer(exports, "have-stats");
902    }
903    else
904    {
905       if (!err) err = map_block_killer(exports, "have-no-stats");
906
907       perc_rej = (float)local_urls_rejected * 100.0F /
908             (float)local_urls_read;
909
910       snprintf(buf, sizeof(buf), "%d", local_urls_read);
911       if (!err) err = map(exports, "requests-received", 1, buf, 1);
912
913       snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
914       if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
915
916       snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
917       if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
918    }
919
920 #else /* ndef FEATURE_STATISTICS */
921    err = err || map_block_killer(exports, "statistics");
922 #endif /* ndef FEATURE_STATISTICS */
923    
924    /* 
925     * List all action files in use, together with view and edit links,
926     * except for standard.action, which should only be viewable. (Not
927     * enforced in the editor itself)
928     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
929     */
930    s = strdup("");
931    for (i = 0; i < MAX_AF_FILES; i++)
932    {
933       if (csp->actions_list[i] != NULL)
934       {
935          if (!err) err = string_append(&s, "<tr><td>");
936          if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
937          snprintf(buf, sizeof(buf),
938             "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&amp;index=%u\">View</a>", i);
939          if (!err) err = string_append(&s, buf);
940
941 #ifdef FEATURE_CGI_EDIT_ACTIONS
942          if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
943             && (NULL == strstr(csp->actions_list[i]->filename, "standard.action"))
944             && (NULL != csp->config->actions_file_short[i]))
945          {
946 #ifdef HAVE_ACCESS
947             if (access(csp->config->actions_file[i], W_OK) == 0)
948             {
949 #endif /* def HAVE_ACCESS */
950                snprintf(buf, sizeof(buf), "&nbsp;&nbsp;<a href=\"/edit-actions-list?f=%u\">Edit</a>", i);
951                if (!err) err = string_append(&s, buf);
952 #ifdef HAVE_ACCESS
953             }
954             else
955             {
956                if (!err) err = string_append(&s, "&nbsp;&nbsp;<strong>No write access.</strong>");
957             }
958 #endif /* def HAVE_ACCESS */
959          }
960 #endif
961
962          if (!err) err = string_append(&s, "</td></tr>\n");
963       }
964    }
965    if (*s != '\0')   
966    {
967       if (!err) err = map(exports, "actions-filenames", 1, s, 0);
968    }
969    else
970    {
971       if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
972    }
973
974    /* 
975     * List all re_filterfiles in use, together with view options.
976     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
977     */
978    s = strdup("");
979    for (i = 0; i < MAX_AF_FILES; i++)
980    {
981       if (csp->rlist[i] != NULL)
982       {
983          if (!err) err = string_append(&s, "<tr><td>");
984          if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
985          snprintf(buf, sizeof(buf),
986             "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&amp;index=%u\">View</a>", i);
987          if (!err) err = string_append(&s, buf);
988          if (!err) err = string_append(&s, "</td></tr>\n");
989       }
990    }
991    if (*s != '\0')   
992    {
993       if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
994    }
995    else
996    {
997       if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
998       if (!err) err = map_block_killer(exports, "have-filterfile");
999    }
1000
1001 #ifdef FEATURE_TRUST
1002    if (csp->tlist)
1003    {
1004       if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1005    }
1006    else
1007    {
1008       if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1009       if (!err) err = map_block_killer(exports, "have-trustfile");
1010    }
1011 #else
1012    if (!err) err = map_block_killer(exports, "trust-support");
1013 #endif /* ndef FEATURE_TRUST */
1014
1015 #ifdef FEATURE_CGI_EDIT_ACTIONS
1016    if (!err && (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1017    {
1018       err = map_block_killer(exports, "cgi-editor-is-disabled");
1019    }
1020 #endif /* ndef CGI_EDIT_ACTIONS */
1021
1022    if (err)
1023    {
1024       free_map(exports);
1025       return JB_ERR_MEMORY;
1026    }
1027
1028    return template_fill_for_cgi(csp, "show-status", exports, rsp);
1029 }
1030
1031  
1032 /*********************************************************************
1033  *
1034  * Function    :  cgi_show_url_info
1035  *
1036  * Description :  CGI function that determines and shows which actions
1037  *                Privoxy will perform for a given url, and which
1038  *                matches starting from the defaults have lead to that.
1039  *
1040  * Parameters  :
1041  *          1  :  csp = Current client state (buffers, headers, etc...)
1042  *          2  :  rsp = http_response data structure for output
1043  *          3  :  parameters = map of cgi parameters
1044  *
1045  * CGI Parameters :
1046  *            url : The url whose actions are to be determined.
1047  *                  If url is unset, the url-given conditional will be
1048  *                  set, so that all but the form can be suppressed in
1049  *                  the template.
1050  *
1051  * Returns     :  JB_ERR_OK on success
1052  *                JB_ERR_MEMORY on out-of-memory error.  
1053  *
1054  *********************************************************************/
1055 jb_err cgi_show_url_info(struct client_state *csp,
1056                          struct http_response *rsp,
1057                          const struct map *parameters)
1058 {
1059    char *url_param;
1060    struct map *exports;
1061    char buf[150];
1062
1063    assert(csp);
1064    assert(rsp);
1065    assert(parameters);
1066
1067    if (NULL == (exports = default_exports(csp, "show-url-info")))
1068    {
1069       return JB_ERR_MEMORY;
1070    }
1071
1072    /*
1073     * Get the url= parameter (if present) and remove any leading/trailing spaces.
1074     */
1075    url_param = strdup(lookup(parameters, "url"));
1076    if (url_param == NULL)
1077    {
1078       free_map(exports);
1079       return JB_ERR_MEMORY;
1080    }
1081    chomp(url_param);
1082
1083    /*
1084     * Handle prefixes.  4 possibilities:
1085     * 1) "http://" or "https://" prefix present and followed by URL - OK
1086     * 2) Only the "http://" or "https://" part is present, no URL - change
1087     *    to empty string so it will be detected later as "no URL".
1088     * 3) Parameter specified but doesn't start with "http(s?)://" - add a
1089     *    "http://" prefix.
1090     * 4) Parameter not specified or is empty string - let this fall through
1091     *    for now, next block of code will handle it.
1092     */
1093    if (0 == strncmp(url_param, "http://", 7))
1094    {
1095       if (url_param[7] == '\0')
1096       {
1097          /*
1098           * Empty URL (just prefix).
1099           * Make it totally empty so it's caught by the next if()
1100           */
1101          url_param[0] = '\0';
1102       }
1103    }
1104    else if (0 == strncmp(url_param, "https://", 8))
1105    {
1106       if (url_param[8] == '\0')
1107       {
1108          /*
1109           * Empty URL (just prefix).
1110           * Make it totally empty so it's caught by the next if()
1111           */
1112          url_param[0] = '\0';
1113       }
1114    }
1115    else if ((url_param[0] != '\0')
1116       && ((NULL == strstr(url_param, "://")
1117             || (strstr(url_param, "://") > strstr(url_param, "/")))))
1118    {
1119       /*
1120        * No prefix or at least no prefix before
1121        * the first slash - assume http://
1122        */
1123       char *url_param_prefixed = strdup("http://");
1124
1125       if (JB_ERR_OK != string_join(&url_param_prefixed, url_param))
1126       {
1127          free_map(exports);
1128          return JB_ERR_MEMORY;
1129       }
1130       url_param = url_param_prefixed;
1131    }
1132
1133    /*
1134     * Hide "toggle off" warning if Privoxy is toggled on.
1135     */
1136    if (
1137 #ifdef FEATURE_TOGGLE
1138        (global_toggle_state == 1) &&
1139 #endif /* def FEATURE_TOGGLE */
1140        map_block_killer(exports, "privoxy-is-toggled-off")
1141       )
1142    {
1143       free_map(exports);
1144       return JB_ERR_MEMORY;
1145    }
1146
1147    if (url_param[0] == '\0')
1148    {
1149       /* URL paramater not specified, display query form only. */
1150       free(url_param);
1151       if (map_block_killer(exports, "url-given")
1152         || map(exports, "url", 1, "", 1))
1153       {
1154          free_map(exports);
1155          return JB_ERR_MEMORY;
1156       }
1157    }
1158    else
1159    {
1160       /* Given a URL, so query it. */
1161       jb_err err;
1162       char *matches;
1163       char *s;
1164       int hits = 0;
1165       struct file_list *fl;
1166       struct url_actions *b;
1167       struct http_request url_to_query[1];
1168       struct current_action_spec action[1];
1169       int i;
1170       
1171       if (map(exports, "url", 1, html_encode(url_param), 0))
1172       {
1173          free(url_param);
1174          free_map(exports);
1175          return JB_ERR_MEMORY;
1176       }
1177
1178       init_current_action(action);
1179
1180       if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1181       {
1182          free_current_action(action);
1183          free(url_param);
1184          free_map(exports);
1185          return JB_ERR_MEMORY;
1186       }
1187
1188       memset(url_to_query, '\0', sizeof(url_to_query));
1189       err = parse_http_url(url_param, url_to_query, REQUIRE_PROTOCOL);
1190       assert((err != JB_ERR_OK) || (url_to_query->ssl == !strncmpic(url_param, "https://", 8)));
1191
1192       free(url_param);
1193
1194       if (err == JB_ERR_MEMORY)
1195       {
1196          free_http_request(url_to_query);
1197          free_current_action(action);
1198          free_map(exports);
1199          return JB_ERR_MEMORY;
1200       }
1201       else if (err)
1202       {
1203          /* Invalid URL */
1204
1205          err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1206          if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1207          if (!err) err = map_block_killer(exports, "valid-url");
1208
1209          free_current_action(action);
1210          free_http_request(url_to_query);
1211
1212          if (err)
1213          {
1214             free_map(exports);
1215             return JB_ERR_MEMORY;
1216          }
1217
1218          return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1219       }
1220
1221       /*
1222        * We have a warning about SSL paths.  Hide it for unencrypted sites.
1223        */
1224       if (!url_to_query->ssl)
1225       {
1226          if (map_block_killer(exports, "https"))
1227          {
1228             free_current_action(action);
1229             free_map(exports);
1230             free_http_request(url_to_query);
1231             return JB_ERR_MEMORY;
1232          }
1233       }
1234
1235       matches = strdup("<table summary=\"\" class=\"transparent\">");
1236
1237       for (i = 0; i < MAX_AF_FILES; i++)
1238       {
1239          if (NULL == csp->config->actions_file_short[i]
1240              || !strcmp(csp->config->actions_file_short[i], "standard.action")) continue;
1241
1242          b = NULL;
1243          hits = 1;
1244          if ((fl = csp->actions_list[i]) != NULL)
1245          {
1246             if ((b = fl->f) != NULL)
1247             {
1248                /* FIXME: Hardcoded HTML! */
1249                string_append(&matches, "<tr><th>In file: ");
1250                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1251                snprintf(buf, sizeof(buf), " <a class=\"cmd\" href=\"/show-status?file=actions&amp;index=%d\">", i);
1252                string_append(&matches, buf);
1253                string_append(&matches, "View</a>");
1254 #ifdef FEATURE_CGI_EDIT_ACTIONS
1255                if (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS)
1256                {
1257 #ifdef HAVE_ACCESS
1258                   if (access(csp->config->actions_file[i], W_OK) == 0)
1259                   {
1260 #endif /* def HAVE_ACCESS */
1261                      snprintf(buf, sizeof(buf),
1262                         " <a class=\"cmd\" href=\"/edit-actions-list?f=%d\">", i);
1263                      string_append(&matches, buf);
1264                      string_append(&matches, "Edit</a>");
1265 #ifdef HAVE_ACCESS
1266                   }
1267                   else
1268                   {
1269                      string_append(&matches, " <strong>No write access.</strong>");
1270                   }
1271 #endif /* def HAVE_ACCESS */
1272                }
1273 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1274
1275                string_append(&matches, "</th></tr>\n");
1276
1277                hits = 0;
1278                b = b->next;
1279             }
1280          }
1281
1282          for (; (b != NULL) && (matches != NULL); b = b->next)
1283          {
1284             if (url_match(b->url, url_to_query))
1285             {
1286                string_append(&matches, "<tr><td>{");
1287                string_join  (&matches, actions_to_html(csp, b->action));
1288                string_append(&matches, " }<br>\n<code>");
1289                string_join  (&matches, html_encode(b->url->spec));
1290                string_append(&matches, "</code></td></tr>\n");
1291
1292                if (merge_current_action(action, b->action))
1293                {
1294                   freez(matches);
1295                   free_http_request(url_to_query);
1296                   free_current_action(action);
1297                   free_map(exports);
1298                   return JB_ERR_MEMORY;
1299                }
1300                hits++;
1301             }
1302          }
1303
1304          if (!hits)
1305          {
1306             string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1307          }
1308       }
1309       string_append(&matches, "</table>\n");
1310
1311       /*
1312        * XXX: Kludge to make sure the "Forward settings" section
1313        * shows what forward-override{} would do with the requested URL.
1314        * No one really cares how the CGI request would be forwarded
1315        * if it wasn't intercepted as CGI request in the first place.
1316        *
1317        * From here on the action bitmask will no longer reflect
1318        * the real url (http://config.privoxy.org/show-url-info?url=.*),
1319        * but luckily it's no longer required later on anyway.
1320        */
1321       free_current_action(csp->action);
1322       get_url_actions(csp, url_to_query);
1323
1324       /*
1325        * Fill in forwarding settings.
1326        *
1327        * The possibilities are:
1328        *  - no forwarding
1329        *  - http forwarding only
1330        *  - socks4(a) forwarding only
1331        *  - socks4(a) and http forwarding.
1332        *
1333        * XXX: Parts of this code could be reused for the
1334        * "forwarding-failed" template which currently doesn't
1335        * display the proxy port and an eventual second forwarder.
1336        */
1337       {
1338          const struct forward_spec *fwd = forward_url(csp, url_to_query);
1339
1340          if ((fwd->gateway_host == NULL) && (fwd->forward_host == NULL))
1341          {
1342             if (!err) err = map_block_killer(exports, "socks-forwarder");
1343             if (!err) err = map_block_killer(exports, "http-forwarder");
1344          }
1345          else
1346          {
1347             char port[10]; /* We save proxy ports as int but need a string here */
1348
1349             if (!err) err = map_block_killer(exports, "no-forwarder");
1350
1351             if (fwd->gateway_host != NULL)
1352             {
1353                char *socks_type = NULL;
1354
1355                switch (fwd->type)
1356                {
1357                   case SOCKS_4:
1358                      socks_type = "socks4";
1359                      break;
1360                   case SOCKS_4A:
1361                      socks_type = "socks4a";
1362                      break;
1363                   case SOCKS_5:
1364                      socks_type = "socks5";
1365                      break;
1366                   default:
1367                      log_error(LOG_LEVEL_FATAL, "Unknown socks type: %d.", fwd->type);
1368                }
1369
1370                if (!err) err = map(exports, "socks-type", 1, socks_type, 1);
1371                if (!err) err = map(exports, "gateway-host", 1, fwd->gateway_host, 1);
1372                snprintf(port, sizeof(port), "%d", fwd->gateway_port);
1373                if (!err) err = map(exports, "gateway-port", 1, port, 1);
1374             }
1375             else
1376             {
1377                if (!err) err = map_block_killer(exports, "socks-forwarder");
1378             }
1379
1380             if (fwd->forward_host != NULL)
1381             {
1382                if (!err) err = map(exports, "forward-host", 1, fwd->forward_host, 1);
1383                snprintf(port, sizeof(port), "%d", fwd->forward_port);
1384                if (!err) err = map(exports, "forward-port", 1, port, 1);
1385             }
1386             else
1387             {
1388                if (!err) err = map_block_killer(exports, "http-forwarder");
1389             }
1390          }
1391       }
1392
1393       free_http_request(url_to_query);
1394
1395       if (err || matches == NULL)
1396       {
1397          free_current_action(action);
1398          free_map(exports);
1399          return JB_ERR_MEMORY;
1400       }
1401
1402 #ifdef FEATURE_CGI_EDIT_ACTIONS
1403       if ((csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
1404       {
1405          err = map_block_killer(exports, "cgi-editor-is-disabled");
1406       }
1407 #endif /* FEATURE_CGI_EDIT_ACTIONS */
1408
1409       /*
1410        * If zlib support is available, if no content filters
1411        * are enabled or if the prevent-compression action is enabled,
1412        * suppress the "compression could prevent filtering" warning.
1413        */
1414 #ifndef FEATURE_ZLIB
1415       if (!content_filters_enabled(action) ||
1416          (action->flags & ACTION_NO_COMPRESSION))
1417 #endif
1418       {
1419          if (!err) err = map_block_killer(exports, "filters-might-be-ineffective");
1420       }
1421
1422       if (err || map(exports, "matches", 1, matches , 0))
1423       {
1424          free_current_action(action);
1425          free_map(exports);
1426          return JB_ERR_MEMORY;
1427       }
1428
1429       s = current_action_to_html(csp, action);
1430
1431       free_current_action(action);
1432
1433       if (map(exports, "final", 1, s, 0))
1434       {
1435          free_map(exports);
1436          return JB_ERR_MEMORY;
1437       }
1438    }
1439
1440    return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1441 }
1442
1443
1444 /*********************************************************************
1445  *
1446  * Function    :  cgi_robots_txt
1447  *
1448  * Description :  CGI function to return "/robots.txt".
1449  *
1450  * Parameters  :
1451  *          1  :  csp = Current client state (buffers, headers, etc...)
1452  *          2  :  rsp = http_response data structure for output
1453  *          3  :  parameters = map of cgi parameters
1454  *
1455  * CGI Parameters : None
1456  *
1457  * Returns     :  JB_ERR_OK on success
1458  *                JB_ERR_MEMORY on out-of-memory error.  
1459  *
1460  *********************************************************************/
1461 jb_err cgi_robots_txt(struct client_state *csp,
1462                       struct http_response *rsp,
1463                       const struct map *parameters)
1464 {
1465    char buf[100];
1466    jb_err err;
1467
1468    (void)csp;
1469    (void)parameters;
1470
1471    rsp->body = strdup(
1472       "# This is the Privoxy control interface.\n"
1473       "# It isn't very useful to index it, and you're likely to break stuff.\n"
1474       "# So go away!\n"
1475       "\n"
1476       "User-agent: *\n"
1477       "Disallow: /\n"
1478       "\n");
1479    if (rsp->body == NULL)
1480    {
1481       return JB_ERR_MEMORY;
1482    }
1483
1484    err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1485
1486    rsp->is_static = 1;
1487
1488    get_http_time(7 * 24 * 60 * 60, buf, sizeof(buf)); /* 7 days into future */
1489    if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1490
1491    return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1492 }
1493
1494
1495 /*********************************************************************
1496  *
1497  * Function    :  show_defines
1498  *
1499  * Description :  Add to a map the state od all conditional #defines
1500  *                used when building
1501  *
1502  * Parameters  :
1503  *          1  :  exports = map to extend
1504  *
1505  * Returns     :  JB_ERR_OK on success
1506  *                JB_ERR_MEMORY on out-of-memory error.  
1507  *
1508  *********************************************************************/
1509 static jb_err show_defines(struct map *exports)
1510 {
1511    jb_err err = JB_ERR_OK;
1512
1513 #ifdef FEATURE_ACCEPT_FILTER
1514    if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 1);
1515 #else /* ifndef FEATURE_ACCEPT_FILTER */
1516    if (!err) err = map_conditional(exports, "FEATURE_ACCEPT_FILTER", 0);
1517 #endif /* ndef FEATURE_ACCEPT_FILTER */
1518
1519 #ifdef FEATURE_ACL
1520    if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1521 #else /* ifndef FEATURE_ACL */
1522    if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1523 #endif /* ndef FEATURE_ACL */
1524
1525 #ifdef FEATURE_CGI_EDIT_ACTIONS
1526    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1527 #else /* ifndef FEATURE_CGI_EDIT_ACTIONS */
1528    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1529 #endif /* ndef FEATURE_CGI_EDIT_ACTIONS */
1530
1531 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1532    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 1);
1533 #else /* ifndef FEATURE_CONNECTION_KEEP_ALIVE */
1534    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_KEEP_ALIVE", 0);
1535 #endif /* ndef FEATURE_CONNECTION_KEEP_ALIVE */
1536
1537 #ifdef FEATURE_CONNECTION_SHARING
1538    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 1);
1539 #else /* ifndef FEATURE_CONNECTION_SHARING */
1540    if (!err) err = map_conditional(exports, "FEATURE_CONNECTION_SHARING", 0);
1541 #endif /* ndef FEATURE_CONNECTION_SHARING */
1542
1543 #ifdef FEATURE_FAST_REDIRECTS
1544    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1545 #else /* ifndef FEATURE_FAST_REDIRECTS */
1546    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1547 #endif /* ndef FEATURE_FAST_REDIRECTS */
1548
1549 #ifdef FEATURE_FORCE_LOAD
1550    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1551    if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1552 #else /* ifndef FEATURE_FORCE_LOAD */
1553    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1554    if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1555 #endif /* ndef FEATURE_FORCE_LOAD */
1556
1557 #ifdef FEATURE_GRACEFUL_TERMINATION
1558    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1559 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1560    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1561 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1562
1563 #ifdef FEATURE_IMAGE_BLOCKING
1564    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1565 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1566    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1567 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1568
1569 #ifdef FEATURE_IMAGE_DETECT_MSIE
1570    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1571 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1572    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1573 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1574
1575 #ifdef HAVE_RFC2553
1576    if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 1);
1577 #else /* ifndef HAVE_RFC2553 */
1578    if (!err) err = map_conditional(exports, "FEATURE_IPV6_SUPPORT", 0);
1579 #endif /* ndef HAVE_RFC2553 */
1580
1581 #ifdef FEATURE_NO_GIFS
1582    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1583 #else /* ifndef FEATURE_NO_GIFS */
1584    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1585 #endif /* ndef FEATURE_NO_GIFS */
1586
1587 #ifdef FEATURE_PTHREAD
1588    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1589 #else /* ifndef FEATURE_PTHREAD */
1590    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1591 #endif /* ndef FEATURE_PTHREAD */
1592
1593 #ifdef FEATURE_STATISTICS
1594    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1595 #else /* ifndef FEATURE_STATISTICS */
1596    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1597 #endif /* ndef FEATURE_STATISTICS */
1598
1599 #ifdef FEATURE_TOGGLE
1600    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1601 #else /* ifndef FEATURE_TOGGLE */
1602    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1603 #endif /* ndef FEATURE_TOGGLE */
1604
1605 #ifdef FEATURE_TRUST
1606    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1607 #else /* ifndef FEATURE_TRUST */
1608    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1609 #endif /* ndef FEATURE_TRUST */
1610
1611 #ifdef FEATURE_ZLIB
1612    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 1);
1613 #else /* ifndef FEATURE_ZLIB */
1614    if (!err) err = map_conditional(exports, "FEATURE_ZLIB", 0);
1615 #endif /* ndef FEATURE_ZLIB */
1616
1617 #ifdef STATIC_PCRE
1618    if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1619 #else /* ifndef STATIC_PCRE */
1620    if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1621 #endif /* ndef STATIC_PCRE */
1622
1623 #ifdef STATIC_PCRS
1624    if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1625 #else /* ifndef STATIC_PCRS */
1626    if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1627 #endif /* ndef STATIC_PCRS */
1628
1629    return err;
1630 }
1631
1632
1633 /*********************************************************************
1634  *
1635  * Function    :  show_rcs
1636  *
1637  * Description :  Create a string with the rcs info for all sourcefiles
1638  *
1639  * Parameters  :  None
1640  *
1641  * Returns     :  A string, or NULL on out-of-memory.
1642  *
1643  *********************************************************************/
1644 static char *show_rcs(void)
1645 {
1646    char *result = strdup("");
1647    char buf[BUFFER_SIZE];
1648
1649    /* Instead of including *all* dot h's in the project (thus creating a
1650     * tremendous amount of dependencies), I will concede to declaring them
1651     * as extern's.  This forces the developer to add to this list, but oh well.
1652     */
1653
1654 #define SHOW_RCS(__x)              \
1655    {                               \
1656       extern const char __x[];     \
1657       snprintf(buf, sizeof(buf), " %s\n", __x);   \
1658       string_append(&result, buf); \
1659    }
1660
1661    /* In alphabetical order */
1662    SHOW_RCS(actions_h_rcs)
1663    SHOW_RCS(actions_rcs)
1664 #ifdef AMIGA
1665    SHOW_RCS(amiga_h_rcs)
1666    SHOW_RCS(amiga_rcs)
1667 #endif /* def AMIGA */
1668    SHOW_RCS(cgi_h_rcs)
1669    SHOW_RCS(cgi_rcs)
1670 #ifdef FEATURE_CGI_EDIT_ACTIONS
1671    SHOW_RCS(cgiedit_h_rcs)
1672    SHOW_RCS(cgiedit_rcs)
1673 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1674    SHOW_RCS(cgisimple_h_rcs)
1675    SHOW_RCS(cgisimple_rcs)
1676 #ifdef __MINGW32__
1677    SHOW_RCS(cygwin_h_rcs)
1678 #endif
1679    SHOW_RCS(deanimate_h_rcs)
1680    SHOW_RCS(deanimate_rcs)
1681    SHOW_RCS(encode_h_rcs)
1682    SHOW_RCS(encode_rcs)
1683    SHOW_RCS(errlog_h_rcs)
1684    SHOW_RCS(errlog_rcs)
1685    SHOW_RCS(filters_h_rcs)
1686    SHOW_RCS(filters_rcs)
1687    SHOW_RCS(gateway_h_rcs)
1688    SHOW_RCS(gateway_rcs)
1689    SHOW_RCS(jbsockets_h_rcs)
1690    SHOW_RCS(jbsockets_rcs)
1691    SHOW_RCS(jcc_h_rcs)
1692    SHOW_RCS(jcc_rcs)
1693    SHOW_RCS(list_h_rcs)
1694    SHOW_RCS(list_rcs)
1695    SHOW_RCS(loadcfg_h_rcs)
1696    SHOW_RCS(loadcfg_rcs)
1697    SHOW_RCS(loaders_h_rcs)
1698    SHOW_RCS(loaders_rcs)
1699    SHOW_RCS(miscutil_h_rcs)
1700    SHOW_RCS(miscutil_rcs)
1701    SHOW_RCS(parsers_h_rcs)
1702    SHOW_RCS(parsers_rcs)
1703    SHOW_RCS(pcrs_rcs)
1704    SHOW_RCS(pcrs_h_rcs)
1705    SHOW_RCS(project_h_rcs)
1706    SHOW_RCS(ssplit_h_rcs)
1707    SHOW_RCS(ssplit_rcs)
1708    SHOW_RCS(urlmatch_h_rcs)
1709    SHOW_RCS(urlmatch_rcs)
1710 #ifdef _WIN32
1711 #ifndef _WIN_CONSOLE
1712    SHOW_RCS(w32log_h_rcs)
1713    SHOW_RCS(w32log_rcs)
1714    SHOW_RCS(w32res_h_rcs)
1715    SHOW_RCS(w32taskbar_h_rcs)
1716    SHOW_RCS(w32taskbar_rcs)
1717 #endif /* ndef _WIN_CONSOLE */
1718    SHOW_RCS(win32_h_rcs)
1719    SHOW_RCS(win32_rcs)
1720 #endif /* def _WIN32 */
1721
1722 #undef SHOW_RCS
1723
1724    return result;
1725
1726 }
1727
1728
1729 /*********************************************************************
1730  *
1731  * Function    :  cgi_show_file
1732  *
1733  * Description :  CGI function that shows the content of a
1734  *                configuration file.
1735  *
1736  * Parameters  :
1737  *          1  :  csp = Current client state (buffers, headers, etc...)
1738  *          2  :  rsp = http_response data structure for output
1739  *          3  :  parameters = map of cgi parameters
1740  *
1741  * CGI Parameters :
1742  *        file :  Which file to show.  Only first letter is checked,
1743  *                valid values are:
1744  *                - "a"ction file
1745  *                - "r"egex
1746  *                - "t"rust
1747  *                Default is to show menu and other information.
1748  *
1749  * Returns     :  JB_ERR_OK on success
1750  *                JB_ERR_MEMORY on out-of-memory error.  
1751  *
1752  *********************************************************************/
1753 static jb_err cgi_show_file(struct client_state *csp,
1754                             struct http_response *rsp,
1755                             const struct map *parameters)
1756 {
1757    unsigned i;
1758    const char * filename = NULL;
1759    char * file_description = NULL;
1760
1761    assert(csp);
1762    assert(rsp);
1763    assert(parameters);
1764
1765    switch (*(lookup(parameters, "file")))
1766    {
1767    case 'a':
1768       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
1769       {
1770          filename = csp->actions_list[i]->filename;
1771          file_description = "Actions File";
1772       }
1773       break;
1774
1775    case 'f':
1776       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
1777       {
1778          filename = csp->rlist[i]->filename;
1779          file_description = "Filter File";
1780       }
1781       break;
1782
1783 #ifdef FEATURE_TRUST
1784    case 't':
1785       if (csp->tlist)
1786       {
1787          filename = csp->tlist->filename;
1788          file_description = "Trust File";
1789       }
1790       break;
1791 #endif /* def FEATURE_TRUST */
1792    }
1793
1794    if (NULL != filename)
1795    {
1796       struct map *exports;
1797       char *s;
1798       jb_err err;
1799       size_t length;
1800
1801       exports = default_exports(csp, "show-status");
1802       if (NULL == exports)
1803       {
1804          return JB_ERR_MEMORY;
1805       }
1806
1807       if ( map(exports, "file-description", 1, file_description, 1)
1808         || map(exports, "filepath", 1, html_encode(filename), 0) )
1809       {
1810          free_map(exports);
1811          return JB_ERR_MEMORY;
1812       }
1813
1814       err = load_file(filename, &s, &length);
1815       if (JB_ERR_OK != err)
1816       {
1817          if (map(exports, "contents", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
1818          {
1819             free_map(exports);
1820             return JB_ERR_MEMORY;
1821          }
1822       }
1823       else
1824       {
1825          s = html_encode_and_free_original(s);
1826          if (NULL == s)
1827          {
1828             return JB_ERR_MEMORY;
1829          }
1830
1831          if (map(exports, "contents", 1, s, 0))
1832          {
1833             free_map(exports);
1834             return JB_ERR_MEMORY;
1835          }
1836       }
1837
1838       return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
1839    }
1840
1841    return JB_ERR_CGI_PARAMS;
1842 }
1843
1844  
1845 /*********************************************************************
1846  *
1847  * Function    :  load_file
1848  *
1849  * Description :  Loads a file into a buffer.
1850  *
1851  * Parameters  :
1852  *          1  :  filename = Name of the file to be loaded.
1853  *          2  :  buffer   = Used to return the file's content.
1854  *          3  :  length   = Used to return the size of the file.
1855  *
1856  * Returns     :  JB_ERR_OK in case of success,
1857  *                JB_ERR_FILE in case of ordinary file loading errors
1858  *                            (fseek() and ftell() errors are fatal)
1859  *                JB_ERR_MEMORY in case of out-of-memory.
1860  *
1861  *********************************************************************/
1862 static jb_err load_file(const char *filename, char **buffer, size_t *length)
1863 {
1864    FILE *fp;
1865    long ret;
1866    jb_err err = JB_ERR_OK;
1867
1868    fp = fopen(filename, "rb");
1869    if (NULL == fp)
1870    {
1871       log_error(LOG_LEVEL_ERROR, "Failed to open %s: %E", filename);
1872       return JB_ERR_FILE;
1873    }
1874
1875    /* Get file length */
1876    if (fseek(fp, 0, SEEK_END))
1877    {
1878       log_error(LOG_LEVEL_FATAL,
1879          "Unexpected error while fseek()ing to the end of %s: %E",
1880          filename);
1881    }
1882    ret = ftell(fp);
1883    if (-1 == ret)
1884    {
1885       log_error(LOG_LEVEL_FATAL,
1886          "Unexpected ftell() error while loading %s: %E",
1887          filename);
1888    }
1889    *length = (size_t)ret;
1890
1891    /* Go back to the beginning. */
1892    if (fseek(fp, 0, SEEK_SET))
1893    {
1894       log_error(LOG_LEVEL_FATAL,
1895          "Unexpected error while fseek()ing to the beginning of %s: %E",
1896          filename);
1897    }
1898
1899    *buffer = (char *)zalloc(*length + 1);
1900    if (NULL == *buffer)
1901    {
1902       err = JB_ERR_MEMORY;
1903    }
1904    else if (!fread(*buffer, *length, 1, fp))
1905    {
1906       /*
1907        * May happen if the file size changes between fseek() and
1908        * fread(). If it does, we just log it and serve what we got.
1909        */
1910       log_error(LOG_LEVEL_ERROR,
1911          "Couldn't completely read file %s.", filename);
1912       err = JB_ERR_FILE;
1913    }
1914
1915    fclose(fp);
1916
1917    return err;
1918
1919 }
1920
1921
1922 /*
1923   Local Variables:
1924   tab-width: 3
1925   end:
1926 */