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