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