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