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