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