Fixed gcc43 compiler warnings, zero out cgi_send_user_manual's
[privoxy.git] / cgisimple.c
1 const char cgisimple_rcs[] = "$Id: cgisimple.c,v 1.44 2006/12/22 14:19:27 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 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  * Revisions   :
38  *    $Log: cgisimple.c,v $
39  *    Revision 1.44  2006/12/22 14:19:27  fabiankeil
40  *    Removed checks whether or not AF_FILES have
41  *    data structures associated with them in cgi_show_status.
42  *    It doesn't matter as we're only interested in the file names.
43  *
44  *    For the action files the checks were always true,
45  *    but they prevented empty filter files from being
46  *    listed. Fixes parts of BR 1619208.
47  *
48  *    Revision 1.43  2006/12/17 17:57:56  fabiankeil
49  *    - Added FEATURE_GRACEFUL_TERMINATION to the
50  *      "conditional #defines" section
51  *    - Escaped ampersands in generated HTML.
52  *    - Renamed re-filter-filename to re-filter-filenames
53  *
54  *    Revision 1.42  2006/11/21 15:43:12  fabiankeil
55  *    Add special treatment for WIN32 to make sure
56  *    cgi_send_user_manual opens the files in binary mode.
57  *    Fixes BR 1600411 and unbreaks image delivery.
58  *
59  *    Remove outdated comment.
60  *
61  *    Revision 1.41  2006/10/09 19:18:28  roro
62  *    Redirect http://p.p/user-manual (without trailing slash) to
63  *    http://p.p/user-manual/ (with trailing slash), otherwise links will be broken.
64  *
65  *    Revision 1.40  2006/09/09 13:05:33  fabiankeil
66  *    Modified cgi_send_user_manual to serve binary
67  *    content without destroying it first. Should also be
68  *    faster now. Added ".jpg" check for Content-Type guessing.
69  *
70  *    Revision 1.39  2006/09/08 09:49:23  fabiankeil
71  *    Deliver documents in the user-manual directory
72  *    with "Content-Type text/css" if their filename
73  *    ends with ".css".
74  *
75  *    Revision 1.38  2006/09/06 18:45:03  fabiankeil
76  *    Incorporate modified version of Roland Rosenfeld's patch to
77  *    optionally access the user-manual via Privoxy. Closes patch 679075.
78  *
79  *    Formatting changed to Privoxy style, added call to
80  *    cgi_error_no_template if the requested file doesn't
81  *    exist and modified check whether or not Privoxy itself
82  *    should serve the manual. Should work cross-platform now.
83  *
84  *    Revision 1.37  2006/07/18 14:48:45  david__schmidt
85  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
86  *    with what was really the latest development (the v_3_0_branch branch)
87  *
88  *    Revision 1.35.2.7  2006/01/29 23:10:56  david__schmidt
89  *    Multiple filter file support
90  *
91  *    Revision 1.35.2.6  2005/07/04 03:13:43  david__schmidt
92  *    Undo some damaging memory leak patches
93  *
94  *    Revision 1.35.2.5  2005/05/07 21:50:55  david__schmidt
95  *    A few memory leaks plugged (mostly on error paths)
96  *
97  *    Revision 1.35.2.4  2005/04/04 02:21:24  david__schmidt
98  *    Another instance of:
99  *    Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
100  *    Thanks to Magnus Holmgren for the patch
101  *
102  *    Revision 1.35.2.3  2003/12/17 16:34:15  oes
103  *     - Prevent line wrap beween "View/Edit" link buttons on status page
104  *     - Some (mostly irrelevant) fixes for Out-of-mem-case handling
105  *
106  *    Revision 1.35.2.2  2003/04/03 13:48:28  oes
107  *    Don't show "Edit" buttons #ifndef FEATURE_CGI_EDIT_ACTIONS
108  *
109  *    Revision 1.35.2.1  2002/07/04 15:02:38  oes
110  *    Added ability to send redirects to send-banner CGI, so that it can completely mimic the image blocking action if called with type=auto
111  *
112  *    Revision 1.35.2.1  2002/07/01 17:32:04  morcego
113  *    Applying patch from Andreas as provided by Hal on the list.
114  *    Message-ID: <20020701121218.V1606@feenix.burgiss.net>
115  *
116  *    Revision 1.35  2002/05/12 21:44:44  jongfoster
117  *    Adding amiga.[ch] revision information, if on an amiga.
118  *
119  *    Revision 1.34  2002/04/30 12:06:12  oes
120  *    Deleted unused code from default_cgi
121  *
122  *    Revision 1.33  2002/04/30 11:14:52  oes
123  *    Made csp the first parameter in *action_to_html
124  *
125  *    Revision 1.32  2002/04/26 18:29:13  jongfoster
126  *    Fixing this Visual C++ warning:
127  *    cgisimple.c(775) : warning C4018: '<' : signed/unsigned mismatch
128  *
129  *    Revision 1.31  2002/04/26 12:54:36  oes
130  *     - Kill obsolete REDIRECT_URL code
131  *     - Error handling fixes
132  *     - Style sheet related HTML snipplet changes
133  *     - cgi_show_url_info:
134  *       - Matches now in table, actions on single lines,
135  *         linked to help
136  *       - standard.action suppressed
137  *       - Buttons to View and Edit AFs
138  *
139  *    Revision 1.30  2002/04/24 02:18:08  oes
140  *     - show-status is now the starting point for editing
141  *       the actions files, generate list of all AFs with buttons
142  *       for viewing and editing, new look for file list (Jon:
143  *       buttons now aligned ;-P ), view mode now supports multiple
144  *       AFs, name changes, no view links for unspecified files,
145  *       no edit link for standard.action.
146  *
147  *     - Jon's multiple AF patch: cgi_show_url_info now uses all
148  *       AFs and marks the output accordingly
149  *
150  *    Revision 1.29  2002/04/10 13:38:35  oes
151  *    load_template signature changed
152  *
153  *    Revision 1.28  2002/04/07 15:42:12  jongfoster
154  *    Fixing send-banner?type=auto when the image-blocker is
155  *    a redirect to send-banner
156  *
157  *    Revision 1.27  2002/04/05 15:50:48  oes
158  *    added send-stylesheet CGI
159  *
160  *    Revision 1.26  2002/04/04 00:36:36  gliptak
161  *    always use pcre for matching
162  *
163  *    Revision 1.25  2002/04/03 22:28:03  gliptak
164  *    Removed references to gnu_regex
165  *
166  *    Revision 1.24  2002/04/02 16:12:47  oes
167  *    Fix: moving misplaced lines into #ifdef FEATURE_FORCE
168  *
169  *    Revision 1.23  2002/03/26 22:29:54  swa
170  *    we have a new homepage!
171  *
172  *    Revision 1.22  2002/03/24 16:18:15  jongfoster
173  *    Removing old logo
174  *
175  *    Revision 1.21  2002/03/24 15:23:33  jongfoster
176  *    Name changes
177  *
178  *    Revision 1.20  2002/03/24 13:25:43  swa
179  *    name change related issues
180  *
181  *    Revision 1.19  2002/03/16 23:54:06  jongfoster
182  *    Adding graceful termination feature, to help look for memory leaks.
183  *    If you enable this (which, by design, has to be done by hand
184  *    editing config.h) and then go to http://i.j.b/die, then the program
185  *    will exit cleanly after the *next* request.  It should free all the
186  *    memory that was used.
187  *
188  *    Revision 1.18  2002/03/12 01:44:49  oes
189  *    Changed default for "blocked" image from jb logo to checkboard pattern
190  *
191  *    Revision 1.17  2002/03/08 16:43:18  oes
192  *    Added choice beween GIF and PNG built-in images
193  *
194  *    Revision 1.16  2002/03/07 03:48:38  oes
195  *     - Changed built-in images from GIF to PNG
196  *       (with regard to Unisys patent issue)
197  *     - Added a 4x4 pattern PNG which is less intrusive
198  *       than the logo but also clearly marks the deleted banners
199  *
200  *    Revision 1.15  2002/03/06 22:54:35  jongfoster
201  *    Automated function-comment nitpicking.
202  *
203  *    Revision 1.14  2002/03/02 04:14:50  david__schmidt
204  *    Clean up a little CRLF unpleasantness that suddenly appeared
205  *
206  *    Revision 1.13  2002/02/21 00:10:37  jongfoster
207  *    Adding send-banner?type=auto option
208  *
209  *    Revision 1.12  2002/01/23 01:03:32  jongfoster
210  *    Fixing gcc [CygWin] compiler warnings
211  *
212  *    Revision 1.11  2002/01/23 00:01:04  jongfoster
213  *    Adding cgi_transparent_gif() for http://i.j.b/t
214  *    Adding missing html_encode() to many CGI functions.
215  *    Adding urlmatch.[ch] to http://i.j.b/show-version
216  *
217  *    Revision 1.10  2002/01/17 21:10:37  jongfoster
218  *    Changes to cgi_show_url_info to use new matching code from urlmatch.c.
219  *    Also fixing a problem in the same function with improperly quoted URLs
220  *    in output HTML, and adding code to handle https:// URLs correctly.
221  *
222  *    Revision 1.9  2001/11/30 23:09:15  jongfoster
223  *    Now reports on FEATURE_CGI_EDIT_ACTIONS
224  *    Removing FEATURE_DENY_GZIP from template
225  *
226  *    Revision 1.8  2001/11/13 00:14:07  jongfoster
227  *    Fixing stupid bug now I've figured out what || means.
228  *    (It always returns 0 or 1, not one of it's paramaters.)
229  *
230  *    Revision 1.7  2001/10/23 21:48:19  jongfoster
231  *    Cleaning up error handling in CGI functions - they now send back
232  *    a HTML error page and should never cause a FATAL error.  (Fixes one
233  *    potential source of "denial of service" attacks).
234  *
235  *    CGI actions file editor that works and is actually useful.
236  *
237  *    Ability to toggle JunkBuster remotely using a CGI call.
238  *
239  *    You can turn off both the above features in the main configuration
240  *    file, e.g. if you are running a multi-user proxy.
241  *
242  *    Revision 1.6  2001/10/14 22:00:32  jongfoster
243  *    Adding support for a 404 error when an invalid CGI page is requested.
244  *
245  *    Revision 1.5  2001/10/07 15:30:41  oes
246  *    Removed FEATURE_DENY_GZIP
247  *
248  *    Revision 1.4  2001/10/02 15:31:12  oes
249  *    Introduced show-request cgi
250  *
251  *    Revision 1.3  2001/09/22 16:34:44  jongfoster
252  *    Removing unneeded #includes
253  *
254  *    Revision 1.2  2001/09/19 18:01:11  oes
255  *    Fixed comments; cosmetics
256  *
257  *    Revision 1.1  2001/09/16 17:08:54  jongfoster
258  *    Moving simple CGI functions from cgi.c to new file cgisimple.c
259  *
260  *
261  **********************************************************************/
262 \f
263
264 #include "config.h"
265
266 #include <stdio.h>
267 #include <sys/types.h>
268 #include <stdlib.h>
269 #include <ctype.h>
270 #include <string.h>
271 #include <assert.h>
272
273 #ifdef _WIN32
274 #define snprintf _snprintf
275 #endif /* def _WIN32 */
276
277 #include "project.h"
278 #include "cgi.h"
279 #include "cgisimple.h"
280 #include "list.h"
281 #include "encode.h"
282 #include "jcc.h"
283 #include "filters.h"
284 #include "actions.h"
285 #include "miscutil.h"
286 #include "loadcfg.h"
287 #include "parsers.h"
288 #include "urlmatch.h"
289 #include "errlog.h"
290
291 const char cgisimple_h_rcs[] = CGISIMPLE_H_VERSION;
292
293
294 static char *show_rcs(void);
295 static jb_err show_defines(struct map *exports);
296
297
298 /*********************************************************************
299  *
300  * Function    :  cgi_default
301  *
302  * Description :  CGI function that is called for the CGI_SITE_1_HOST
303  *                and CGI_SITE_2_HOST/CGI_SITE_2_PATH base URLs.
304  *                Boring - only exports the default exports.
305  *               
306  * Parameters  :
307  *          1  :  csp = Current client state (buffers, headers, etc...)
308  *          2  :  rsp = http_response data structure for output
309  *          3  :  parameters = map of cgi parameters
310  *
311  * CGI Parameters : none
312  *
313  * Returns     :  JB_ERR_OK on success
314  *                JB_ERR_MEMORY on out-of-memory
315  *
316  *********************************************************************/
317 jb_err cgi_default(struct client_state *csp,
318                    struct http_response *rsp,
319                    const struct map *parameters)
320 {
321    struct map *exports;
322
323    assert(csp);
324    assert(rsp);
325
326    if (NULL == (exports = default_exports(csp, "")))
327    {
328       return JB_ERR_MEMORY;
329    }
330
331    return template_fill_for_cgi(csp, "default", exports, rsp);
332 }
333
334
335 /*********************************************************************
336  *
337  * Function    :  cgi_error_404
338  *
339  * Description :  CGI function that is called if an unknown action was
340  *                given.
341  *               
342  * Parameters  :
343  *          1  :  csp = Current client state (buffers, headers, etc...)
344  *          2  :  rsp = http_response data structure for output
345  *          3  :  parameters = map of cgi parameters
346  *
347  * CGI Parameters : none
348  *
349  * Returns     :  JB_ERR_OK on success
350  *                JB_ERR_MEMORY on out-of-memory error.  
351  *
352  *********************************************************************/
353 jb_err cgi_error_404(struct client_state *csp,
354                      struct http_response *rsp,
355                      const struct map *parameters)
356 {
357    struct map *exports;
358
359    assert(csp);
360    assert(rsp);
361    assert(parameters);
362
363    if (NULL == (exports = default_exports(csp, NULL)))
364    {
365       return JB_ERR_MEMORY;
366    }
367
368    rsp->status = strdup("404 Privoxy configuration page not found");
369    if (rsp->status == NULL)
370    {
371       free_map(exports);
372       return JB_ERR_MEMORY;
373    }
374
375    return template_fill_for_cgi(csp, "cgi-error-404", exports, rsp);
376 }
377
378
379 #ifdef FEATURE_GRACEFUL_TERMINATION
380 /*********************************************************************
381  *
382  * Function    :  cgi_die
383  *
384  * Description :  CGI function to shut down Privoxy.
385  *                NOTE: Turning this on in a production build
386  *                would be a BAD idea.  An EXTREMELY BAD idea.
387  *                In short, don't do it.
388  *               
389  * Parameters  :
390  *          1  :  csp = Current client state (buffers, headers, etc...)
391  *          2  :  rsp = http_response data structure for output
392  *          3  :  parameters = map of cgi parameters
393  *
394  * CGI Parameters : none
395  *
396  * Returns     :  JB_ERR_OK on success
397  *                JB_ERR_MEMORY on out-of-memory error.  
398  *
399  *********************************************************************/
400 jb_err cgi_die (struct client_state *csp,
401                 struct http_response *rsp,
402                 const struct map *parameters)
403 {
404    assert(csp);
405    assert(rsp);
406    assert(parameters);
407
408    /* quit */
409    g_terminate = 1;
410
411    /*
412     * I don't really care what gets sent back to the browser.
413     * Take the easy option - "out of memory" page.
414     */
415
416    return JB_ERR_MEMORY;
417 }
418 #endif /* def FEATURE_GRACEFUL_TERMINATION */
419
420
421 /*********************************************************************
422  *
423  * Function    :  cgi_show_request
424  *
425  * Description :  Show the client's request and what sed() would have
426  *                made of it.
427  *               
428  * Parameters  :
429  *          1  :  csp = Current client state (buffers, headers, etc...)
430  *          2  :  rsp = http_response data structure for output
431  *          3  :  parameters = map of cgi parameters
432  *
433  * CGI Parameters : none
434  *
435  * Returns     :  JB_ERR_OK on success
436  *                JB_ERR_MEMORY on out-of-memory error.  
437  *
438  *********************************************************************/
439 jb_err cgi_show_request(struct client_state *csp,
440                         struct http_response *rsp,
441                         const struct map *parameters)
442 {
443    char *p;
444    struct map *exports;
445
446    assert(csp);
447    assert(rsp);
448    assert(parameters);
449
450    if (NULL == (exports = default_exports(csp, "show-request")))
451    {
452       return JB_ERR_MEMORY;
453    }
454    
455    /*
456     * Repair the damage done to the IOB by get_header()
457     */
458    for (p = csp->iob->buf; p < csp->iob->eod; p++)
459    {
460       if (*p == '\0') *p = '\n';
461    }
462
463    /*
464     * Export the original client's request and the one we would
465     * be sending to the server if this wasn't a CGI call
466     */
467
468    if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0))
469    {
470       free_map(exports);
471       return JB_ERR_MEMORY;
472    }
473
474    if (map(exports, "processed-request", 1, html_encode_and_free_original(
475       sed(client_patterns, add_client_headers, csp)), 0))
476    {
477       free_map(exports);
478       return JB_ERR_MEMORY;
479    }
480
481    return template_fill_for_cgi(csp, "show-request", exports, rsp);
482 }
483
484
485 /*********************************************************************
486  *
487  * Function    :  cgi_send_banner
488  *
489  * Description :  CGI function that returns a banner. 
490  *
491  * Parameters  :
492  *          1  :  csp = Current client state (buffers, headers, etc...)
493  *          2  :  rsp = http_response data structure for output
494  *          3  :  parameters = map of cgi parameters
495  *
496  * CGI Parameters :
497  *           type : Selects the type of banner between "trans", "logo",
498  *                  and "auto". Defaults to "logo" if absent or invalid.
499  *                  "auto" means to select as if we were image-blocking.
500  *                  (Only the first character really counts; b and t are
501  *                  equivalent).
502  *
503  * Returns     :  JB_ERR_OK on success
504  *                JB_ERR_MEMORY on out-of-memory error.  
505  *
506  *********************************************************************/
507 jb_err cgi_send_banner(struct client_state *csp,
508                        struct http_response *rsp,
509                        const struct map *parameters)
510 {
511    char imagetype = lookup(parameters, "type")[0];
512
513    /*
514     * If type is auto, then determine the right thing
515     * to do from the set-image-blocker action
516     */
517    if (imagetype == 'a') 
518    {
519       /*
520        * Default to pattern
521        */
522       imagetype = 'p';
523
524 #ifdef FEATURE_IMAGE_BLOCKING
525       if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
526       {
527          static const char prefix1[] = CGI_PREFIX "send-banner?type=";
528          static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type=";
529          const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
530
531          if (p == NULL)
532          {
533             /* Use default - nothing to do here. */
534          }
535          else if (0 == strcmpic(p, "blank"))
536          {
537             imagetype = 'b';
538          }
539          else if (0 == strcmpic(p, "pattern"))
540          {
541             imagetype = 'p';
542          }
543
544          /*
545           * If the action is to call this CGI, determine
546           * the argument:
547           */
548          else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1))
549          {
550             imagetype = p[sizeof(prefix1) - 1];
551          }
552          else if (0 == strncmpic(p, prefix2, sizeof(prefix2) - 1))
553          {
554             imagetype = p[sizeof(prefix2) - 1];
555          }
556
557          /*
558           * Everything else must (should) be a URL to
559           * redirect to.
560           */
561          else
562          {
563             imagetype = 'r';
564          }
565       }
566 #endif /* def FEATURE_IMAGE_BLOCKING */
567    }
568       
569    /*
570     * Now imagetype is either the non-auto type we were called with,
571     * or it was auto and has since been determined. In any case, we
572     * can proceed to actually answering the request by sending a redirect
573     * or an image as appropriate:
574     */
575    if (imagetype == 'r') 
576    {
577       rsp->status = strdup("302 Local Redirect from Privoxy");
578       if (rsp->status == NULL)
579       {
580          return JB_ERR_MEMORY;
581       }
582       if (enlist_unique_header(rsp->headers, "Location",
583                                csp->action->string[ACTION_STRING_IMAGE_BLOCKER]))
584       {
585          return JB_ERR_MEMORY;
586       }
587    }
588    else
589    {
590       if ((imagetype == 'b') || (imagetype == 't')) 
591       {
592          rsp->body = bindup(image_blank_data, image_blank_length);
593          rsp->content_length = image_blank_length;
594       }
595       else
596       {
597          rsp->body = bindup(image_pattern_data, image_pattern_length);
598          rsp->content_length = image_pattern_length;
599       }
600
601       if (rsp->body == NULL)
602       {
603          return JB_ERR_MEMORY;
604       }
605       if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
606       {
607          return JB_ERR_MEMORY;
608       }
609
610       rsp->is_static = 1;
611    }
612
613    return JB_ERR_OK;
614
615 }
616
617
618 /*********************************************************************
619  *
620  * Function    :  cgi_transparent_image
621  *
622  * Description :  CGI function that sends a 1x1 transparent image.
623  *
624  * Parameters  :
625  *          1  :  csp = Current client state (buffers, headers, etc...)
626  *          2  :  rsp = http_response data structure for output
627  *          3  :  parameters = map of cgi parameters
628  *
629  * CGI Parameters : None
630  *
631  * Returns     :  JB_ERR_OK on success
632  *                JB_ERR_MEMORY on out-of-memory error.  
633  *
634  *********************************************************************/
635 jb_err cgi_transparent_image(struct client_state *csp,
636                              struct http_response *rsp,
637                              const struct map *parameters)
638 {
639    rsp->body = bindup(image_blank_data, image_blank_length);
640    rsp->content_length = image_blank_length;
641
642    if (rsp->body == NULL)
643    {
644       return JB_ERR_MEMORY;
645    }
646
647    if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE))
648    {
649       return JB_ERR_MEMORY;
650    }
651
652    rsp->is_static = 1;
653
654    return JB_ERR_OK;
655
656 }
657
658
659 /*********************************************************************
660  *
661  * Function    :  cgi_send_stylesheet
662  *
663  * Description :  CGI function that sends a css stylesheet found
664  *                in the cgi-style.css template
665  *
666  * Parameters  :
667  *          1  :  csp = Current client state (buffers, headers, etc...)
668  *          2  :  rsp = http_response data structure for output
669  *          3  :  parameters = map of cgi parameters
670  *
671  * CGI Parameters : None
672  *
673  * Returns     :  JB_ERR_OK on success
674  *                JB_ERR_MEMORY on out-of-memory error.  
675  *
676  *********************************************************************/
677 jb_err cgi_send_stylesheet(struct client_state *csp,
678                            struct http_response *rsp,
679                            const struct map *parameters)
680 {
681    jb_err err;
682    
683    assert(csp);
684    assert(rsp);
685
686    err = template_load(csp, &rsp->body, "cgi-style.css", 0);
687
688    if (err == JB_ERR_FILE)
689    {
690       /*
691        * No way to tell user; send empty stylesheet
692        */
693       log_error(LOG_LEVEL_ERROR, "Could not find cgi-style.css template");
694    }
695    else if (err)
696    {
697       return err; /* JB_ERR_MEMORY */
698    }
699
700    if (enlist(rsp->headers, "Content-Type: text/css"))
701    {
702       return JB_ERR_MEMORY;
703    }
704
705    return JB_ERR_OK;
706
707 }
708 /*********************************************************************
709  *
710  * Function    :  cgi_send_user_manual
711  *
712  * Description :  CGI function that sends a file in the user
713  *                manual directory.
714  *
715  * Parameters  :
716  *          1  :  csp = Current client state (buffers, headers, etc...)
717  *          2  :  rsp = http_response data structure for output
718  *          3  :  parameters = map of cgi parameters
719  *
720  * CGI Parameters : file=name.html, the name of the HTML file
721  *                  (relative to user-manual from config)
722  *
723  * Returns     :  JB_ERR_OK on success
724  *                JB_ERR_MEMORY on out-of-memory error.  
725  *
726  *********************************************************************/
727 jb_err cgi_send_user_manual(struct client_state *csp,
728                             struct http_response *rsp,
729                             const struct map *parameters)
730 {
731    const char * filename;
732    char *full_path;
733    FILE *fp;
734    jb_err err = JB_ERR_OK;
735    size_t length;
736
737    assert(csp);
738    assert(rsp);
739    assert(parameters);
740
741    if (!parameters->first)
742    {
743       /* requested http://p.p/user-manual (without trailing slash) */
744       return cgi_redirect(rsp, CGI_PREFIX "user-manual/");
745    }
746
747    get_string_param(parameters, "file", &filename);
748    /* Check paramter for hack attempts */
749    if (filename && strchr(filename, '/'))
750    {
751       return JB_ERR_CGI_PARAMS;
752    }
753    if (filename && strstr(filename, ".."))
754    {
755       return JB_ERR_CGI_PARAMS;
756    }
757
758    full_path = make_path(csp->config->usermanual, filename ? filename : "index.html");
759    if (full_path == NULL)
760    {
761       return JB_ERR_MEMORY;
762    }
763
764    /* Open user-manual file */
765 #ifdef WIN32
766    /*
767     * XXX: Do we support other operating systems that
768     * require special treatment to fopen in binary mode?
769     */
770    if (NULL == (fp = fopen(full_path, "rb")))
771 #else
772    if (NULL == (fp = fopen(full_path, "r")))
773 #endif /* def WIN32 */
774    {
775       log_error(LOG_LEVEL_ERROR, "Cannot open user-manual file %s: %E", full_path);
776       err = cgi_error_no_template(csp, rsp, full_path);
777       free(full_path);
778       return err;
779    }
780
781    /* Get file length */
782    fseek(fp, 0, SEEK_END);
783    length = (size_t)ftell(fp);
784    fseek(fp, 0, SEEK_SET);
785
786    /* Allocate memory and load the file directly into the body */
787    rsp->body = (char *)malloc(length+1);
788    if (!rsp->body)
789    {
790       fclose(fp);
791       free(full_path);
792       return JB_ERR_MEMORY;
793    }
794    memset(rsp->body, '\0', length+1);
795    if (!fread(rsp->body, length, 1, fp))
796    {
797       /*
798        * This happens if we didn't fopen in binary mode.
799        * If it does, we just log it and serve what we got.
800        */
801       log_error(LOG_LEVEL_ERROR, "Couldn't completely read user-manual file %s.", full_path);
802    }
803    fclose(fp);
804    free(full_path);
805
806    rsp->content_length = length;
807
808    /* Guess correct Content-Type based on the filename's ending */
809    if (filename)
810    {
811       length = strlen(filename);
812    }
813    else
814    {
815       length = 0;
816    } 
817    if((length>=4) && !strcmp(&filename[length-4], ".css"))
818    {
819       err = enlist(rsp->headers, "Content-Type: text/css");
820    }
821    else if((length>=4) && !strcmp(&filename[length-4], ".jpg"))
822    {
823       err = enlist(rsp->headers, "Content-Type: image/jpeg");
824    }
825    else
826    {
827       err = enlist(rsp->headers, "Content-Type: text/html");
828    }
829
830    return err;
831 }
832
833
834 /*********************************************************************
835  *
836  * Function    :  cgi_show_version
837  *
838  * Description :  CGI function that returns a a web page describing the
839  *                file versions of Privoxy.
840  *
841  * Parameters  :
842  *          1  :  csp = Current client state (buffers, headers, etc...)
843  *          2  :  rsp = http_response data structure for output
844  *          3  :  parameters = map of cgi parameters
845  *
846  * CGI Parameters : none
847  *
848  * Returns     :  JB_ERR_OK on success
849  *                JB_ERR_MEMORY on out-of-memory error.  
850  *
851  *********************************************************************/
852 jb_err cgi_show_version(struct client_state *csp,
853                         struct http_response *rsp,
854                         const struct map *parameters)
855 {
856    struct map *exports;
857
858    assert(csp);
859    assert(rsp);
860    assert(parameters);
861
862    if (NULL == (exports = default_exports(csp, "show-version")))
863    {
864       return JB_ERR_MEMORY;
865    }
866
867    if (map(exports, "sourceversions", 1, show_rcs(), 0))
868    {
869       free_map(exports);
870       return JB_ERR_MEMORY;
871    }
872
873    return template_fill_for_cgi(csp, "show-version", exports, rsp);
874 }
875
876  
877 /*********************************************************************
878  *
879  * Function    :  cgi_show_status
880  *
881  * Description :  CGI function that returns a web page describing the
882  *                current status of Privoxy.
883  *
884  * Parameters  :
885  *          1  :  csp = Current client state (buffers, headers, etc...)
886  *          2  :  rsp = http_response data structure for output
887  *          3  :  parameters = map of cgi parameters
888  *
889  * CGI Parameters :
890  *        file :  Which file to show.  Only first letter is checked,
891  *                valid values are:
892  *                - "p"ermissions (actions) file
893  *                - "r"egex
894  *                - "t"rust
895  *                Default is to show menu and other information.
896  *
897  * Returns     :  JB_ERR_OK on success
898  *                JB_ERR_MEMORY on out-of-memory error.  
899  *
900  *********************************************************************/
901 jb_err cgi_show_status(struct client_state *csp,
902                        struct http_response *rsp,
903                        const struct map *parameters)
904 {
905    char *s = NULL;
906    unsigned i;
907    int j;
908
909    FILE * fp;
910    char buf[BUFFER_SIZE];
911    const char * filename = NULL;
912    char * file_description = NULL;
913 #ifdef FEATURE_STATISTICS
914    float perc_rej;   /* Percentage of http requests rejected */
915    int local_urls_read;
916    int local_urls_rejected;
917 #endif /* ndef FEATURE_STATISTICS */
918    jb_err err = JB_ERR_OK;
919
920    struct map *exports;
921
922    assert(csp);
923    assert(rsp);
924    assert(parameters);
925
926    if (NULL == (exports = default_exports(csp, "show-status")))
927    {
928       return JB_ERR_MEMORY;
929    }
930
931    switch (*(lookup(parameters, "file")))
932    {
933    case 'a':
934       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->actions_list[i])
935       {
936          filename = csp->actions_list[i]->filename;
937          file_description = "Actions File";
938       }
939       break;
940
941    case 'f':
942       if (!get_number_param(csp, parameters, "index", &i) && i < MAX_AF_FILES && csp->rlist[i])
943       {
944          filename = csp->rlist[i]->filename;
945          file_description = "Filter File";
946       }
947       break;
948
949 #ifdef FEATURE_TRUST
950    case 't':
951       if (csp->tlist)
952       {
953          filename = csp->tlist->filename;
954          file_description = "Trust File";
955       }
956       break;
957 #endif /* def FEATURE_TRUST */
958    }
959
960    if (NULL != filename)
961    {
962       if ( map(exports, "file-description", 1, file_description, 1)
963         || map(exports, "filepath", 1, html_encode(filename), 0) )
964       {
965          free_map(exports);
966          return JB_ERR_MEMORY;
967       }
968
969       if ((fp = fopen(filename, "r")) == NULL)
970       {
971          if (map(exports, "content", 1, "<h1>ERROR OPENING FILE!</h1>", 1))
972          {
973             free_map(exports);
974             return JB_ERR_MEMORY;
975          }
976       }
977       else
978       {
979          s = strdup("");
980          while ((s != NULL) && fgets(buf, sizeof(buf), fp))
981          {
982             string_join  (&s, html_encode(buf));
983          }
984          fclose(fp);
985
986          if (map(exports, "contents", 1, s, 0))
987          {
988             free_map(exports);
989             return JB_ERR_MEMORY;
990          }
991       }
992
993       return template_fill_for_cgi(csp, "show-status-file", exports, rsp);
994    }
995
996    s = strdup("");
997    for (j = 0; (s != NULL) && (j < Argc); j++)
998    {
999       if (!err) err = string_join  (&s, html_encode(Argv[j]));
1000       if (!err) err = string_append(&s, " ");
1001    }
1002    if (!err) err = map(exports, "invocation", 1, s, 0);
1003
1004    if (!err) err = map(exports, "options", 1, csp->config->proxy_args, 1);
1005    if (!err) err = show_defines(exports);
1006
1007    if (err) 
1008    {
1009       free_map(exports);
1010       return JB_ERR_MEMORY;
1011    }
1012
1013 #ifdef FEATURE_STATISTICS
1014    local_urls_read     = urls_read;
1015    local_urls_rejected = urls_rejected;
1016
1017    /*
1018     * Need to alter the stats not to include the fetch of this
1019     * page.
1020     *
1021     * Can't do following thread safely! doh!
1022     *
1023     * urls_read--;
1024     * urls_rejected--; * This will be incremented subsequently *
1025     */
1026
1027    if (local_urls_read == 0)
1028    {
1029       if (!err) err = map_block_killer(exports, "have-stats");
1030    }
1031    else
1032    {
1033       if (!err) err = map_block_killer(exports, "have-no-stats");
1034
1035       perc_rej = (float)local_urls_rejected * 100.0F /
1036             (float)local_urls_read;
1037
1038       snprintf(buf, sizeof(buf), "%d", local_urls_read);
1039       if (!err) err = map(exports, "requests-received", 1, buf, 1);
1040
1041       snprintf(buf, sizeof(buf), "%d", local_urls_rejected);
1042       if (!err) err = map(exports, "requests-blocked", 1, buf, 1);
1043
1044       snprintf(buf, sizeof(buf), "%6.2f", perc_rej);
1045       if (!err) err = map(exports, "percent-blocked", 1, buf, 1);
1046    }
1047
1048 #else /* ndef FEATURE_STATISTICS */
1049    err = err || map_block_killer(exports, "statistics");
1050 #endif /* ndef FEATURE_STATISTICS */
1051    
1052    /* 
1053     * List all action files in use, together with view and edit links,
1054     * except for standard.action, which should only be viewable. (Not
1055     * enforced in the editor itself)
1056     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1057     */
1058    s = strdup("");
1059    for (i = 0; i < MAX_AF_FILES; i++)
1060    {
1061       if (csp->actions_list[i] != NULL)
1062       {
1063          if (!err) err = string_append(&s, "<tr><td>");
1064          if (!err) err = string_join(&s, html_encode(csp->actions_list[i]->filename));
1065          snprintf(buf, 100, "</td><td class=\"buttons\"><a href=\"/show-status?file=actions&amp;index=%d\">View</a>", i);
1066          if (!err) err = string_append(&s, buf);
1067
1068 #ifdef FEATURE_CGI_EDIT_ACTIONS
1069          if (NULL == strstr(csp->actions_list[i]->filename, "standard.action") && NULL != csp->config->actions_file_short[i])
1070          {
1071             snprintf(buf, 100, "&nbsp;&nbsp;<a href=\"/edit-actions-list?f=%s\">Edit</a>", csp->config->actions_file_short[i]);
1072             if (!err) err = string_append(&s, buf);
1073          }
1074 #endif
1075
1076          if (!err) err = string_append(&s, "</td></tr>\n");
1077       }
1078    }
1079    if (*s != '\0')   
1080    {
1081       if (!err) err = map(exports, "actions-filenames", 1, s, 0);
1082    }
1083    else
1084    {
1085       if (!err) err = map(exports, "actions-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1086    }
1087
1088    /* 
1089     * List all re_filterfiles in use, together with view options.
1090     * FIXME: Shouldn't include hardwired HTML here, use line template instead!
1091     */
1092    s = strdup("");
1093    for (i = 0; i < MAX_AF_FILES; i++)
1094    {
1095       if (csp->rlist[i] != NULL)
1096       {
1097          if (!err) err = string_append(&s, "<tr><td>");
1098          if (!err) err = string_join(&s, html_encode(csp->rlist[i]->filename));
1099          snprintf(buf, 100,
1100             "</td><td class=\"buttons\"><a href=\"/show-status?file=filter&amp;index=%d\">View</a>", i);
1101          if (!err) err = string_append(&s, buf);
1102          if (!err) err = string_append(&s, "</td></tr>\n");
1103       }
1104    }
1105    if (*s != '\0')   
1106    {
1107       if (!err) err = map(exports, "re-filter-filenames", 1, s, 0);
1108    }
1109    else
1110    {
1111       if (!err) err = map(exports, "re-filter-filenames", 1, "<tr><td>None specified</td></tr>", 1);
1112       if (!err) err = map_block_killer(exports, "have-filterfile");
1113    }
1114
1115 #ifdef FEATURE_TRUST
1116    if (csp->tlist)
1117    {
1118       if (!err) err = map(exports, "trust-filename", 1, html_encode(csp->tlist->filename), 0);
1119    }
1120    else
1121    {
1122       if (!err) err = map(exports, "trust-filename", 1, "None specified", 1);
1123       if (!err) err = map_block_killer(exports, "have-trustfile");
1124    }
1125 #else
1126    if (!err) err = map_block_killer(exports, "trust-support");
1127 #endif /* ndef FEATURE_TRUST */
1128
1129    if (err)
1130    {
1131       free_map(exports);
1132       return JB_ERR_MEMORY;
1133    }
1134
1135    return template_fill_for_cgi(csp, "show-status", exports, rsp);
1136 }
1137
1138  
1139 /*********************************************************************
1140  *
1141  * Function    :  cgi_show_url_info
1142  *
1143  * Description :  CGI function that determines and shows which actions
1144  *                Privoxy will perform for a given url, and which
1145  *                matches starting from the defaults have lead to that.
1146  *
1147  * Parameters  :
1148  *          1  :  csp = Current client state (buffers, headers, etc...)
1149  *          2  :  rsp = http_response data structure for output
1150  *          3  :  parameters = map of cgi parameters
1151  *
1152  * CGI Parameters :
1153  *            url : The url whose actions are to be determined.
1154  *                  If url is unset, the url-given conditional will be
1155  *                  set, so that all but the form can be suppressed in
1156  *                  the template.
1157  *
1158  * Returns     :  JB_ERR_OK on success
1159  *                JB_ERR_MEMORY on out-of-memory error.  
1160  *
1161  *********************************************************************/
1162 jb_err cgi_show_url_info(struct client_state *csp,
1163                          struct http_response *rsp,
1164                          const struct map *parameters)
1165 {
1166    char *url_param;
1167    struct map *exports;
1168    char buf[150];
1169
1170    assert(csp);
1171    assert(rsp);
1172    assert(parameters);
1173
1174    if (NULL == (exports = default_exports(csp, "show-url-info")))
1175    {
1176       return JB_ERR_MEMORY;
1177    }
1178
1179    /*
1180     * Get the url= parameter (if present) and remove any leading/trailing spaces.
1181     */
1182    url_param = strdup(lookup(parameters, "url"));
1183    if (url_param == NULL)
1184    {
1185       free_map(exports);
1186       return JB_ERR_MEMORY;
1187    }
1188    chomp(url_param);
1189
1190    /*
1191     * Handle prefixes.  4 possibilities:
1192     * 1) "http://" or "https://" prefix present and followed by URL - OK
1193     * 2) Only the "http://" or "https://" part is present, no URL - change
1194     *    to empty string so it will be detected later as "no URL".
1195     * 3) Parameter specified but doesn't contain "http(s?)://" - add a
1196     *    "http://" prefix.
1197     * 4) Parameter not specified or is empty string - let this fall through
1198     *    for now, next block of code will handle it.
1199     */
1200    if (0 == strncmp(url_param, "http://", 7))
1201    {
1202       if (url_param[7] == '\0')
1203       {
1204          /*
1205           * Empty URL (just prefix).
1206           * Make it totally empty so it's caught by the next if()
1207           */
1208          url_param[0] = '\0';
1209       }
1210    }
1211    else if (0 == strncmp(url_param, "https://", 8))
1212    {
1213       if (url_param[8] == '\0')
1214       {
1215          /*
1216           * Empty URL (just prefix).
1217           * Make it totally empty so it's caught by the next if()
1218           */
1219          url_param[0] = '\0';
1220       }
1221    }
1222    else if (url_param[0] != '\0')
1223    {
1224       /*
1225        * Unknown prefix - assume http://
1226        */
1227       char * url_param_prefixed = malloc(7 + 1 + strlen(url_param));
1228       if (NULL == url_param_prefixed)
1229       {
1230          free(url_param);
1231          free_map(exports);
1232          return JB_ERR_MEMORY;
1233       }
1234       strcpy(url_param_prefixed, "http://");
1235       strcpy(url_param_prefixed + 7, url_param);
1236       free(url_param);
1237       url_param = url_param_prefixed;
1238    }
1239
1240
1241    if (url_param[0] == '\0')
1242    {
1243       /* URL paramater not specified, display query form only. */
1244       free(url_param);
1245       if (map_block_killer(exports, "url-given")
1246         || map(exports, "url", 1, "", 1))
1247       {
1248          free_map(exports);
1249          return JB_ERR_MEMORY;
1250       }
1251    }
1252    else
1253    {
1254       /* Given a URL, so query it. */
1255       jb_err err;
1256       char *matches;
1257       char *s;
1258       int hits = 0;
1259       struct file_list *fl;
1260       struct url_actions *b;
1261       struct http_request url_to_query[1];
1262       struct current_action_spec action[1];
1263       int i;
1264       
1265       if (map(exports, "url", 1, html_encode(url_param), 0))
1266       {
1267          free(url_param);
1268          free_map(exports);
1269          return JB_ERR_MEMORY;
1270       }
1271
1272       init_current_action(action);
1273
1274       if (map(exports, "default", 1, current_action_to_html(csp, action), 0))
1275       {
1276          free_current_action(action);
1277          free(url_param);
1278          free_map(exports);
1279          return JB_ERR_MEMORY;
1280       }
1281
1282       err = parse_http_url(url_param, url_to_query, csp);
1283
1284       free(url_param);
1285
1286       if (err == JB_ERR_MEMORY)
1287       {
1288          free_http_request(url_to_query);
1289          free_current_action(action);
1290          free_map(exports);
1291          return JB_ERR_MEMORY;
1292       }
1293       else if (err)
1294       {
1295          /* Invalid URL */
1296
1297          err = map(exports, "matches", 1, "<b>[Invalid URL specified!]</b>" , 1);
1298          if (!err) err = map(exports, "final", 1, lookup(exports, "default"), 1);
1299
1300          free_current_action(action);
1301          free_http_request(url_to_query);
1302
1303          if (err)
1304          {
1305             free_map(exports);
1306             return JB_ERR_MEMORY;
1307          }
1308
1309          return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1310       }
1311
1312       /*
1313        * We have a warning about SSL paths.  Hide it for insecure sites.
1314        */
1315       if (!url_to_query->ssl)
1316       {
1317          if (map_block_killer(exports, "https"))
1318          {
1319             free_current_action(action);
1320             free_map(exports);
1321             free_http_request(url_to_query);
1322             return JB_ERR_MEMORY;
1323          }
1324       }
1325
1326       matches = strdup("<table class=\"transparent\">");
1327
1328       for (i = 0; i < MAX_AF_FILES; i++)
1329       {
1330          if (NULL == csp->config->actions_file_short[i]
1331              || !strcmp(csp->config->actions_file_short[i], "standard")) continue;
1332
1333          b = NULL;
1334          hits = 1;
1335          if ((fl = csp->actions_list[i]) != NULL)
1336          {
1337             if ((b = fl->f) != NULL)
1338             {
1339                /* FIXME: Hardcoded HTML! */
1340                string_append(&matches, "<tr><th>In file: ");
1341                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1342                snprintf(buf, 150, ".action <a class=\"cmd\" href=\"/show-status?file=actions&amp;index=%d\">", i);
1343                string_append(&matches, buf);
1344                string_append(&matches, "View</a>");
1345 #ifdef FEATURE_CGI_EDIT_ACTIONS
1346                string_append(&matches, " <a class=\"cmd\" href=\"/edit-actions-list?f=");
1347                string_join  (&matches, html_encode(csp->config->actions_file_short[i]));
1348                string_append(&matches, "\">Edit</a>");
1349 #endif
1350                string_append(&matches, "</th></tr>\n");
1351
1352                hits = 0;
1353                b = b->next;
1354             }
1355          }
1356
1357          for (; (b != NULL) && (matches != NULL); b = b->next)
1358          {
1359             if (url_match(b->url, url_to_query))
1360             {
1361                string_append(&matches, "<tr><td>{");
1362                string_join  (&matches, actions_to_html(csp, b->action));
1363                string_append(&matches, " }</b><br>\n<code>");
1364                string_join  (&matches, html_encode(b->url->spec));
1365                string_append(&matches, "</code></td></tr>\n");
1366
1367                if (merge_current_action(action, b->action))
1368                {
1369                   freez(matches);
1370                   free_http_request(url_to_query);
1371                   free_current_action(action);
1372                   free_map(exports);
1373                   return JB_ERR_MEMORY;
1374                }
1375                hits++;
1376             }
1377          }
1378
1379          if (!hits)
1380          {
1381             string_append(&matches, "<tr><td>(no matches in this file)</td></tr>\n");
1382          }
1383       }
1384       string_append(&matches, "</table>\n");
1385
1386       free_http_request(url_to_query);
1387
1388       if (matches == NULL)
1389       {
1390          free_current_action(action);
1391          free_map(exports);
1392          return JB_ERR_MEMORY;
1393       }
1394
1395       if (map(exports, "matches", 1, matches , 0))
1396       {
1397          free_current_action(action);
1398          free_map(exports);
1399          return JB_ERR_MEMORY;
1400       }
1401
1402       s = current_action_to_html(csp, action);
1403
1404       free_current_action(action);
1405
1406       if (map(exports, "final", 1, s, 0))
1407       {
1408          free_map(exports);
1409          return JB_ERR_MEMORY;
1410       }
1411    }
1412
1413    return template_fill_for_cgi(csp, "show-url-info", exports, rsp);
1414 }
1415
1416
1417 /*********************************************************************
1418  *
1419  * Function    :  cgi_robots_txt
1420  *
1421  * Description :  CGI function to return "/robots.txt".
1422  *
1423  * Parameters  :
1424  *          1  :  csp = Current client state (buffers, headers, etc...)
1425  *          2  :  rsp = http_response data structure for output
1426  *          3  :  parameters = map of cgi parameters
1427  *
1428  * CGI Parameters : None
1429  *
1430  * Returns     :  JB_ERR_OK on success
1431  *                JB_ERR_MEMORY on out-of-memory error.  
1432  *
1433  *********************************************************************/
1434 jb_err cgi_robots_txt(struct client_state *csp,
1435                       struct http_response *rsp,
1436                       const struct map *parameters)
1437 {
1438    char buf[100];
1439    jb_err err;
1440
1441    rsp->body = strdup(
1442       "# This is the Privoxy control interface.\n"
1443       "# It isn't very useful to index it, and you're likely to break stuff.\n"
1444       "# So go away!\n"
1445       "\n"
1446       "User-agent: *\n"
1447       "Disallow: /\n"
1448       "\n");
1449    if (rsp->body == NULL)
1450    {
1451       return JB_ERR_MEMORY;
1452    }
1453
1454    err = enlist_unique(rsp->headers, "Content-Type: text/plain", 13);
1455
1456    rsp->is_static = 1;
1457
1458    get_http_time(7 * 24 * 60 * 60, buf); /* 7 days into future */
1459    if (!err) err = enlist_unique_header(rsp->headers, "Expires", buf);
1460
1461    return (err ? JB_ERR_MEMORY : JB_ERR_OK);
1462 }
1463
1464
1465 /*********************************************************************
1466  *
1467  * Function    :  show_defines
1468  *
1469  * Description :  Add to a map the state od all conditional #defines
1470  *                used when building
1471  *
1472  * Parameters  :
1473  *          1  :  exports = map to extend
1474  *
1475  * Returns     :  JB_ERR_OK on success
1476  *                JB_ERR_MEMORY on out-of-memory error.  
1477  *
1478  *********************************************************************/
1479 static jb_err show_defines(struct map *exports)
1480 {
1481    jb_err err = JB_ERR_OK;
1482
1483 #ifdef FEATURE_ACL
1484    if (!err) err = map_conditional(exports, "FEATURE_ACL", 1);
1485 #else /* ifndef FEATURE_ACL */
1486    if (!err) err = map_conditional(exports, "FEATURE_ACL", 0);
1487 #endif /* ndef FEATURE_ACL */
1488
1489 #ifdef FEATURE_CGI_EDIT_ACTIONS
1490    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 1);
1491 #else /* ifndef FEATURE_COOKIE_JAR */
1492    if (!err) err = map_conditional(exports, "FEATURE_CGI_EDIT_ACTIONS", 0);
1493 #endif /* ndef FEATURE_COOKIE_JAR */
1494
1495 #ifdef FEATURE_COOKIE_JAR
1496    if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 1);
1497 #else /* ifndef FEATURE_COOKIE_JAR */
1498    if (!err) err = map_conditional(exports, "FEATURE_COOKIE_JAR", 0);
1499 #endif /* ndef FEATURE_COOKIE_JAR */
1500
1501 #ifdef FEATURE_FAST_REDIRECTS
1502    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 1);
1503 #else /* ifndef FEATURE_FAST_REDIRECTS */
1504    if (!err) err = map_conditional(exports, "FEATURE_FAST_REDIRECTS", 0);
1505 #endif /* ndef FEATURE_FAST_REDIRECTS */
1506
1507 #ifdef FEATURE_FORCE_LOAD
1508    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 1);
1509    if (!err) err = map(exports, "FORCE_PREFIX", 1, FORCE_PREFIX, 1);
1510 #else /* ifndef FEATURE_FORCE_LOAD */
1511    if (!err) err = map_conditional(exports, "FEATURE_FORCE_LOAD", 0);
1512    if (!err) err = map(exports, "FORCE_PREFIX", 1, "(none - disabled)", 1);
1513 #endif /* ndef FEATURE_FORCE_LOAD */
1514
1515 #ifdef FEATURE_GRACEFUL_TERMINATION
1516    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 1);
1517 #else /* ifndef FEATURE_GRACEFUL_TERMINATION */
1518    if (!err) err = map_conditional(exports, "FEATURE_GRACEFUL_TERMINATION", 0);
1519 #endif /* ndef FEATURE_GRACEFUL_TERMINATION */
1520
1521 #ifdef FEATURE_IMAGE_BLOCKING
1522    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 1);
1523 #else /* ifndef FEATURE_IMAGE_BLOCKING */
1524    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_BLOCKING", 0);
1525 #endif /* ndef FEATURE_IMAGE_BLOCKING */
1526
1527 #ifdef FEATURE_IMAGE_DETECT_MSIE
1528    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 1);
1529 #else /* ifndef FEATURE_IMAGE_DETECT_MSIE */
1530    if (!err) err = map_conditional(exports, "FEATURE_IMAGE_DETECT_MSIE", 0);
1531 #endif /* ndef FEATURE_IMAGE_DETECT_MSIE */
1532
1533 #ifdef FEATURE_KILL_POPUPS
1534    if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 1);
1535 #else /* ifndef FEATURE_KILL_POPUPS */
1536    if (!err) err = map_conditional(exports, "FEATURE_KILL_POPUPS", 0);
1537 #endif /* ndef FEATURE_KILL_POPUPS */
1538
1539 #ifdef FEATURE_NO_GIFS
1540    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 1);
1541 #else /* ifndef FEATURE_NO_GIFS */
1542    if (!err) err = map_conditional(exports, "FEATURE_NO_GIFS", 0);
1543 #endif /* ndef FEATURE_NO_GIFS */
1544
1545 #ifdef FEATURE_PTHREAD
1546    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 1);
1547 #else /* ifndef FEATURE_PTHREAD */
1548    if (!err) err = map_conditional(exports, "FEATURE_PTHREAD", 0);
1549 #endif /* ndef FEATURE_PTHREAD */
1550
1551 #ifdef FEATURE_STATISTICS
1552    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 1);
1553 #else /* ifndef FEATURE_STATISTICS */
1554    if (!err) err = map_conditional(exports, "FEATURE_STATISTICS", 0);
1555 #endif /* ndef FEATURE_STATISTICS */
1556
1557 #ifdef FEATURE_TOGGLE
1558    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 1);
1559 #else /* ifndef FEATURE_TOGGLE */
1560    if (!err) err = map_conditional(exports, "FEATURE_TOGGLE", 0);
1561 #endif /* ndef FEATURE_TOGGLE */
1562
1563 #ifdef FEATURE_TRUST
1564    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 1);
1565 #else /* ifndef FEATURE_TRUST */
1566    if (!err) err = map_conditional(exports, "FEATURE_TRUST", 0);
1567 #endif /* ndef FEATURE_TRUST */
1568
1569 #ifdef STATIC_PCRE
1570    if (!err) err = map_conditional(exports, "STATIC_PCRE", 1);
1571 #else /* ifndef STATIC_PCRE */
1572    if (!err) err = map_conditional(exports, "STATIC_PCRE", 0);
1573 #endif /* ndef STATIC_PCRE */
1574
1575 #ifdef STATIC_PCRS
1576    if (!err) err = map_conditional(exports, "STATIC_PCRS", 1);
1577 #else /* ifndef STATIC_PCRS */
1578    if (!err) err = map_conditional(exports, "STATIC_PCRS", 0);
1579 #endif /* ndef STATIC_PCRS */
1580
1581    return err;
1582 }
1583
1584
1585 /*********************************************************************
1586  *
1587  * Function    :  show_rcs
1588  *
1589  * Description :  Create a string with the rcs info for all sourcefiles
1590  *
1591  * Parameters  :  None
1592  *
1593  * Returns     :  A string, or NULL on out-of-memory.
1594  *
1595  *********************************************************************/
1596 static char *show_rcs(void)
1597 {
1598    char *result = strdup("");
1599    char buf[BUFFER_SIZE];
1600
1601    /* Instead of including *all* dot h's in the project (thus creating a
1602     * tremendous amount of dependencies), I will concede to declaring them
1603     * as extern's.  This forces the developer to add to this list, but oh well.
1604     */
1605
1606 #define SHOW_RCS(__x)              \
1607    {                               \
1608       extern const char __x[];     \
1609       snprintf(buf, sizeof(buf), " %s\n", __x);   \
1610       string_append(&result, buf); \
1611    }
1612
1613    /* In alphabetical order */
1614    SHOW_RCS(actions_h_rcs)
1615    SHOW_RCS(actions_rcs)
1616 #ifdef AMIGA
1617    SHOW_RCS(amiga_h_rcs)
1618    SHOW_RCS(amiga_rcs)
1619 #endif /* def AMIGA */
1620    SHOW_RCS(cgi_h_rcs)
1621    SHOW_RCS(cgi_rcs)
1622 #ifdef FEATURE_CGI_EDIT_ACTIONS
1623    SHOW_RCS(cgiedit_h_rcs)
1624    SHOW_RCS(cgiedit_rcs)
1625 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1626    SHOW_RCS(cgisimple_h_rcs)
1627    SHOW_RCS(cgisimple_rcs)
1628 #ifdef __MINGW32__
1629    SHOW_RCS(cygwin_h_rcs)
1630 #endif
1631    SHOW_RCS(deanimate_h_rcs)
1632    SHOW_RCS(deanimate_rcs)
1633    SHOW_RCS(encode_h_rcs)
1634    SHOW_RCS(encode_rcs)
1635    SHOW_RCS(errlog_h_rcs)
1636    SHOW_RCS(errlog_rcs)
1637    SHOW_RCS(filters_h_rcs)
1638    SHOW_RCS(filters_rcs)
1639    SHOW_RCS(gateway_h_rcs)
1640    SHOW_RCS(gateway_rcs)
1641    SHOW_RCS(jbsockets_h_rcs)
1642    SHOW_RCS(jbsockets_rcs)
1643    SHOW_RCS(jcc_h_rcs)
1644    SHOW_RCS(jcc_rcs)
1645 #ifdef FEATURE_KILL_POPUPS
1646    SHOW_RCS(killpopup_h_rcs)
1647    SHOW_RCS(killpopup_rcs)
1648 #endif /* def FEATURE_KILL_POPUPS */
1649    SHOW_RCS(list_h_rcs)
1650    SHOW_RCS(list_rcs)
1651    SHOW_RCS(loadcfg_h_rcs)
1652    SHOW_RCS(loadcfg_rcs)
1653    SHOW_RCS(loaders_h_rcs)
1654    SHOW_RCS(loaders_rcs)
1655    SHOW_RCS(miscutil_h_rcs)
1656    SHOW_RCS(miscutil_rcs)
1657    SHOW_RCS(parsers_h_rcs)
1658    SHOW_RCS(parsers_rcs)
1659    SHOW_RCS(pcrs_rcs)
1660    SHOW_RCS(pcrs_h_rcs)
1661    SHOW_RCS(project_h_rcs)
1662    SHOW_RCS(ssplit_h_rcs)
1663    SHOW_RCS(ssplit_rcs)
1664    SHOW_RCS(urlmatch_h_rcs)
1665    SHOW_RCS(urlmatch_rcs)
1666 #ifdef _WIN32
1667 #ifndef _WIN_CONSOLE
1668    SHOW_RCS(w32log_h_rcs)
1669    SHOW_RCS(w32log_rcs)
1670    SHOW_RCS(w32res_h_rcs)
1671    SHOW_RCS(w32taskbar_h_rcs)
1672    SHOW_RCS(w32taskbar_rcs)
1673 #endif /* ndef _WIN_CONSOLE */
1674    SHOW_RCS(win32_h_rcs)
1675    SHOW_RCS(win32_rcs)
1676 #endif /* def _WIN32 */
1677
1678 #undef SHOW_RCS
1679
1680    return result;
1681
1682 }
1683
1684
1685 /*
1686   Local Variables:
1687   tab-width: 3
1688   end:
1689 */