AmigaOS bugfix: PCRS needs a lot of stack, stacksize for child threads
[privoxy.git] / showargs.c
1 const char showargs_rcs[] = "$Id: showargs.c,v 1.7 2001/05/26 00:28:36 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/showargs.c,v $
5  *
6  * Purpose     :  Contains various utility routines needed to 
7  *                generate the show-proxy-args page.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
10  *                IJBSWA team.  http://ijbswa.sourceforge.net
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and 
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
16  *                This program is free software; you can redistribute it 
17  *                and/or modify it under the terms of the GNU General
18  *                Public License as published by the Free Software
19  *                Foundation; either version 2 of the License, or (at
20  *                your option) any later version.
21  *
22  *                This program is distributed in the hope that it will
23  *                be useful, but WITHOUT ANY WARRANTY; without even the
24  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
25  *                PARTICULAR PURPOSE.  See the GNU General Public
26  *                License for more details.
27  *
28  *                The GNU General Public License should be included with
29  *                this file.  If not, you can view it at
30  *                http://www.gnu.org/copyleft/gpl.html
31  *                or write to the Free Software Foundation, Inc., 59
32  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
33  *
34  * Revisions   :
35  *    $Log: showargs.c,v $
36  *    Revision 1.7  2001/05/26 00:28:36  jongfoster
37  *    Automatic reloading of config file.
38  *    Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
39  *    Most of the global variables have been moved to a new
40  *    struct configuration_spec, accessed through csp->config->globalname
41  *    Most of the globals remaining are used by the Win32 GUI.
42  *
43  *    Revision 1.6  2001/05/25 22:32:56  jongfoster
44  *    CRLF->LF
45  *
46  *    Revision 1.5  2001/05/22 18:54:49  oes
47  *
48  *    - Enabled filtering banners by size rather than URL
49  *      by adding patterns that replace all standard banner
50  *      sizes with the "Junkbuster" gif to the re_filterfile
51  *
52  *    - Enabled filtering WebBugs by providing a pattern
53  *      which kills all 1x1 images
54  *
55  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
56  *      which is selected by the (nonstandard and therefore
57  *      capital) letter 'U' in the option string.
58  *      It causes the quantifiers to be ungreedy by default.
59  *      Appending a ? turns back to greedy (!).
60  *
61  *    - Added a new interceptor ijb-send-banner, which
62  *      sends back the "Junkbuster" gif. Without imagelist or
63  *      MSIE detection support, or if tinygif = 1, or the
64  *      URL isn't recognized as an imageurl, a lame HTML
65  *      explanation is sent instead.
66  *
67  *    - Added new feature, which permits blocking remote
68  *      script redirects and firing back a local redirect
69  *      to the browser.
70  *      The feature is conditionally compiled, i.e. it
71  *      can be disabled with --disable-fast-redirects,
72  *      plus it must be activated by a "fast-redirects"
73  *      line in the config file, has its own log level
74  *      and of course wants to be displayed by show-proxy-args
75  *      Note: Boy, all the #ifdefs in 1001 locations and
76  *      all the fumbling with configure.in and acconfig.h
77  *      were *way* more work than the feature itself :-(
78  *
79  *    - Because a generic redirect template was needed for
80  *      this, tinygif = 3 now uses the same.
81  *
82  *    - Moved GIFs, and other static HTTP response templates
83  *      to project.h
84  *
85  *    - Some minor fixes
86  *
87  *    - Removed some >400 CRs again (Jon, you really worked
88  *      a lot! ;-)
89  *
90  *    Revision 1.4  2001/05/20 16:44:47  jongfoster
91  *    Removing last hardcoded JunkBusters.com URLs.
92  *
93  *    Revision 1.3  2001/05/20 01:21:20  jongfoster
94  *    Version 2.9.4 checkin.
95  *    - Merged popupfile and cookiefile, and added control over PCRS
96  *      filtering, in new "permissionsfile".
97  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
98  *      file error you now get a message box (in the Win32 GUI) rather
99  *      than the program exiting with no explanation.
100  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
101  *      skipping.
102  *    - Removed tabs from "config"
103  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
104  *    - Bumped up version number.
105  *
106  *    Revision 1.2  2001/05/17 23:01:01  oes
107  *     - Cleaned CRLF's from the sources and related files
108  *
109  *    Revision 1.1.1.1  2001/05/15 13:59:03  oes
110  *    Initial import of version 2.9.3 source tree
111  *
112  *
113  *********************************************************************/
114 \f
115 #include "config.h"
116
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <sys/types.h>
120 #include <string.h>
121 #include <malloc.h>
122 #include <errno.h>
123
124 #include "project.h"
125 #include "showargs.h"
126 #include "jcc.h"
127 #include "encode.h"
128 #include "parsers.h"
129 #include "errlog.h"
130 #include "miscutil.h"
131 #include "gateway.h"
132
133 const char showargs_h_rcs[] = SHOWARGS_H_VERSION;
134
135 /*********************************************************************
136  *
137  * Function    :  strsav
138  *
139  * Description :  Reallocate "old" and append text to it.  This makes
140  *                it easier to append to malloc'd strings.
141  *
142  * Parameters  :
143  *          1  :  old = Old text that is to be extended.  Will be
144  *                free()d by this routine.
145  *          2  :  text_to_append = Text to be appended to old.
146  *
147  * Returns     :  Pointer to newly malloc'ed appended string.
148  *                If there is no text to append, return old.  Caller
149  *                must free().
150  *
151  *********************************************************************/
152 char *strsav(char *old, const char *text_to_append)
153 {
154    int old_len, new_len;
155    char *p;
156
157    if (( text_to_append == NULL) || (*text_to_append == '\0'))
158    {
159       return(old);
160    }
161
162    if (NULL != old)
163    {
164       old_len = strlen(old);
165    }
166    else
167    {
168       old_len = 0;
169    }
170
171    new_len = old_len + strlen(text_to_append) + 1;
172
173    if (old)
174    {
175       if ((p = realloc(old, new_len)) == NULL)
176       {
177          log_error(LOG_LEVEL_FATAL, "realloc(%d) bytes for proxy_args failed!", new_len);
178          /* Never get here - LOG_LEVEL_FATAL causes program exit */
179       }
180    }
181    else
182    {
183       if ((p = (char *)malloc(new_len)) == NULL)
184       {
185          log_error(LOG_LEVEL_FATAL, "malloc(%d) bytes for proxy_args failed!", new_len);
186          /* Never get here - LOG_LEVEL_FATAL causes program exit */
187       }
188    }
189
190    strcpy(p + old_len, text_to_append);
191    return(p);
192
193 }
194
195
196 /*********************************************************************
197  *
198  * Function    :  savearg
199  *
200  * Description :  Called from `load_config'.  It saves each non-empty
201  *                and non-comment line from config into a list.  This
202  *                list is used to create the show-proxy-args page.
203  *
204  * Parameters  :
205  *          1  :  c = config setting that was found
206  *          2  :  o = the setting's argument (if any)
207  *
208  * Returns     :  N/A
209  *
210  *********************************************************************/
211 void savearg(char *c, char *o, struct configuration_spec * config)
212 {
213    char buf[BUFSIZ];
214
215    *buf = '\0';
216
217    if ( ( NULL != c ) && ( '\0' != *c ) )
218    {
219       if ((c = html_encode(c)))
220       {
221          sprintf(buf, "<a href=\"" REDIRECT_URL "option#%s\">%s</a> ", c, c);
222       }
223       freez(c);
224    }
225    if ( ( NULL != o ) && ( '\0' != *o ) )
226    {
227       if ((o = html_encode(o)))
228       {
229          if (strncmpic(o, "http://", 7) == 0)
230          {
231             strcat(buf, "<a href=\"");
232             strcat(buf, o);
233             strcat(buf, "\">");
234             strcat(buf, o);
235             strcat(buf, "</a>");
236          }
237          else
238          {
239             strcat(buf, o);
240          }
241       }
242       freez(o);
243    }
244
245    strcat(buf, "<br>\n");
246
247    config->proxy_args_invocation = strsav(config->proxy_args_invocation, buf);
248
249 }
250
251
252 /*********************************************************************
253  *
254  * Function    :  init_proxy_args
255  *
256  * Description :  Create the "top" of the show-proxy-args page.
257  *
258  * Parameters  :
259  *          1  :  argc = argument count (same as in main)
260  *          2  :  argv[] = program arguments (same as in main)
261  *
262  * Returns     :  N/A
263  *
264  *********************************************************************/
265 void init_proxy_args(int argc, const char *argv[], struct configuration_spec * config)
266 {
267    const struct gateway *g;
268    char * b;
269    int i;
270
271    freez(config->proxy_args_header);
272    freez(config->proxy_args_invocation);
273    freez(config->proxy_args_gateways);
274    freez(config->proxy_args_trailer);
275    
276
277    b = NULL;
278    b = strsav(b,
279       "HTTP/1.0 200 OK\n"
280       "Server: IJ/" VERSION "\n"
281       "Content-type: text/html\n\n"
282
283       "<html>"
284       "<head>"
285       "<title>Internet Junkbuster Proxy Status</title>"
286       "</head>\n"
287       "<body bgcolor=\"#f8f8f0\" link=\"#000078\" alink=\"#ff0022\" vlink=\"#787878\">\n"
288       "<center>\n"
289       "<h1>" BANNER "\n"
290       "<a href=\"" REDIRECT_URL "faq#show\">Proxy Status</a>\n"
291       "</h1></center>\n"
292       "<h2>You are using the " BANNER " <sup><small><small>TM</small></small></sup></h2>\n"
293       "Version: " VERSION "\n"
294       "<br>Home page: <a href=\"" HOME_PAGE_URL "\">" HOME_PAGE_URL "</a>\n"
295       "<p>\n"
296    );
297
298    b = strsav(b,
299       "<h2>The program was invoked as follows</h2>\n");
300
301    for (i=0; i < argc; i++)
302    {
303       b = strsav(b, argv[i]);
304       b = strsav(b, " ");
305    }
306    b = strsav(b, "<br>\n");
307
308    config->proxy_args_header = b;
309
310    config->proxy_args_invocation = strsav(
311       config->proxy_args_invocation,
312       "<br>\n"
313       "and the following options were set in the configuration file"
314       "<br><br>\n"
315    );
316
317    b = NULL;
318
319    b = strsav(b,
320       "<h2>It supports the following gateway protocols:</h2>\n");
321
322    for (g = gateways; g->name; g++)
323    {
324       b = strsav(b, g->name);
325       b = strsav(b, " ");
326    }
327    b = strsav(b, "<br>\n");
328
329    config->proxy_args_gateways = b;
330 }
331
332
333 /*********************************************************************
334  *
335  * Function    :  end_proxy_args
336  *
337  * Description :  Create the "bottom" of the show-proxy-args page.
338  *
339  * Parameters  :  None
340  *
341  * Returns     :  N/A
342  *
343  *********************************************************************/
344 void end_proxy_args(struct configuration_spec * config)
345 {
346    char *b = NULL;
347    char buf[BUFSIZ];
348
349    /* Instead of including *all* dot h's in the project (thus creating a
350     * tremendous amount of dependencies), I will concede to declaring them
351     * as extern's.  This forces the developer to add to this list, but oh well.
352     */
353
354 #ifndef SPLIT_PROXY_ARGS
355    if (suppress_blocklists && suppress_message!=NULL)
356    {
357       b = strsav(b, "<h2>File contents</h2>\n");
358       b = strsav(b, suppress_message);
359       b = strsav(b, "\n");
360    }
361 #endif /* ndef SPLIT_PROXY_ARGS */
362
363    b = strsav(b, "<h2>Source versions:</h2>\n");
364    b = strsav(b, "<pre>");
365
366 #define SHOW_RCS(__x)            \
367    {                             \
368       extern const char __x[];   \
369       sprintf(buf, "%s\n", __x); \
370       b = strsav(b, buf);        \
371    }
372
373    /* In alphabetical order */
374 #ifdef __MINGW32__
375    SHOW_RCS(cygwin_h_rcs)
376 #endif
377    SHOW_RCS(encode_h_rcs)
378    SHOW_RCS(encode_rcs)
379    SHOW_RCS(errlog_h_rcs)
380    SHOW_RCS(errlog_rcs)
381    SHOW_RCS(filters_h_rcs)
382    SHOW_RCS(filters_rcs)
383    SHOW_RCS(gateway_h_rcs)
384    SHOW_RCS(gateway_rcs)
385 #ifdef GNU_REGEX
386    SHOW_RCS(gnu_regex_h_rcs)
387    SHOW_RCS(gnu_regex_rcs)
388 #endif /* def GNU_REGEX */
389    SHOW_RCS(jbsockets_h_rcs)
390    SHOW_RCS(jbsockets_rcs)
391    SHOW_RCS(jcc_h_rcs)
392    SHOW_RCS(jcc_rcs)
393 #ifdef KILLPOPUPS
394    SHOW_RCS(killpopup_h_rcs)
395    SHOW_RCS(killpopup_rcs)
396 #endif /* def KILLPOPUPS */
397    SHOW_RCS(loadcfg_h_rcs)
398    SHOW_RCS(loadcfg_rcs)
399    SHOW_RCS(loaders_h_rcs)
400    SHOW_RCS(loaders_rcs)
401    SHOW_RCS(miscutil_h_rcs)
402    SHOW_RCS(miscutil_rcs)
403    SHOW_RCS(parsers_h_rcs)
404    SHOW_RCS(parsers_rcs)
405 #ifdef PCRS
406    SHOW_RCS(pcrs_rcs)
407    SHOW_RCS(pcrs_h_rcs)
408 #endif /* def PCRS */
409    SHOW_RCS(project_h_rcs)
410    SHOW_RCS(showargs_h_rcs)
411    SHOW_RCS(showargs_rcs)
412    SHOW_RCS(ssplit_h_rcs)
413    SHOW_RCS(ssplit_rcs)
414 #ifdef _WIN32
415 #ifndef _WIN_CONSOLE
416    SHOW_RCS(w32log_h_rcs)
417    SHOW_RCS(w32log_rcs)
418    SHOW_RCS(w32res_h_rcs)
419    SHOW_RCS(w32rulesdlg_h_rcs)
420    SHOW_RCS(w32rulesdlg_rcs)
421    SHOW_RCS(w32taskbar_h_rcs)
422    SHOW_RCS(w32taskbar_rcs)
423 #endif /* ndef _WIN_CONSOLE */
424    SHOW_RCS(win32_h_rcs)
425    SHOW_RCS(win32_rcs)
426 #endif /* def _WIN32 */
427
428 #undef SHOW_RCS
429
430    b = strsav(b, "</pre>\n");
431
432    b = strsav(b, "<h2>Conditional defines:</h2>\n<ul>");
433
434 #ifdef REGEX
435    b = strsav(b, "  <li><code>#define <b>REGEX</b></code> - Support for regular expressions in the path specs.</li>\n");
436 #else /* ifndef REGEX */
437    b = strsav(b, "  <li><code>#undef <b>REGEX</b></code> - No support for regular expressions in the path specs.</li>\n");
438 #endif /* ndef REGEX */
439
440 #ifdef PCRE
441    b = strsav(b, "  <li><code>#define <b>PCRE</b></code> - Use PCRE rather than old GNU regex library.</li>\n");
442 #else /* ifndef PCRE */
443    b = strsav(b, "  <li><code>#undef <b>PCRE</b></code> - Use old GNU regex library rather than PCRE.</li>\n");
444 #endif /* ndef PCRE */
445
446 #ifdef PCRS
447    b = strsav(b, "  <li><code>#define <b>PCRS</b></code> - Enables arbitrary content modification regexps.</li>\n");
448 #else /* ifndef PCRS */
449    b = strsav(b, "  <li><code>#undef <b>PCRS</b></code> - Disables arbitrary content modification regexps.</li>\n");
450 #endif /* ndef PCRS */
451
452 #ifdef TOGGLE
453    b = strsav(b, "  <li><code>#define <b>TOGGLE</b></code> - Allow JunkBuster to be \"disabled\" so it is just a normal non-blocking non-anonymizing proxy.</li>\n");
454 #else /* ifndef TOGGLE */
455    b = strsav(b, "  <li><code>#undef <b>TOGGLE</b></code> - Do not allow JunkBuster to be \"disabled\" so it is just a normal non-blocking non-anonymizing proxy.</li>\n");
456 #endif /* ndef TOGGLE */
457
458 #ifdef FORCE_LOAD
459    b = strsav(b, "  <li><code>#define <b>FORCE_LOAD</b></code> - Enables bypassing filtering for a single page using the prefix \"" FORCE_PREFIX "\".</li>\n");
460 #else /* ifndef FORCE_LOAD */
461    b = strsav(b, "  <li><code>#undef <b>FORCE_LOAD</b></code> - Disables bypassing filtering for a single page.</li>\n");
462 #endif /* ndef FORCE_LOAD */
463
464 #ifdef DENY_GZIP
465    b = strsav(b, "  <li><code>#define <b>DENY_GZIP</b></code> - Prevents requests from being compressed - required for PCRS.</li>\n");
466 #else /* ifndef DENY_GZIP */
467    b = strsav(b, "  <li><code>#undef <b>DENY_GZIP</b></code> - Allows requests to be compressed if the browser and server support it.</li>\n");
468 #endif /* ndef DENY_GZIP */
469
470 #ifdef STATISTICS
471    b = strsav(b, "  <li><code>#define <b>STATISTICS</b></code> - Enables statistics function.</li>\n");
472 #else /* ifndef STATISTICS */
473    b = strsav(b, "  <li><code>#undef <b>STATISTICS</b></code> - Disables statistics function.</li>\n");
474 #endif /* ndef STATISTICS */
475
476 #ifdef SPLIT_PROXY_ARGS
477    b = strsav(b, "  <li><code>#define <b>SPLIT_PROXY_ARGS</b></code> - Split this page up by placing the configuration files on separate pages.</li>\n");
478 #else /* ifndef SPLIT_PROXY_ARGS */
479    b = strsav(b, "  <li><code>#undef <b>SPLIT_PROXY_ARGS</b></code> - This page contains the text of the configuration files, they are not split onto separate pages.</li>\n");
480 #endif /* ndef SPLIT_PROXY_ARGS */
481
482 #ifdef KILLPOPUPS
483    b = strsav(b, "  <li><code>#define <b>KILLPOPUPS</b></code> - Enables killing JavaScript popups.</li>\n");
484 #else /* ifndef KILLPOPUPS */
485    b = strsav(b, "  <li><code>#undef <b>KILLPOPUPS</b></code> - Disables killing JavaScript popups.</li>\n");
486 #endif /* ndef KILLPOPUPS */
487
488 #ifdef WEBDAV
489    b = strsav(b, "  <li><code>#define <b>WEBDAV</b></code> - Enables support for webDAV - e.g. stops Microsoft Outlook from accessing HotMail e-mail.</li>\n");
490 #else /* ifndef WEBDAV */
491    b = strsav(b, "  <li><code>#undef <b>WEBDAV</b></code> - Disables support for webDAV - e.g. so Microsoft Outlook can access HotMail e-mail.</li>\n");
492 #endif /* ndef WEBDAV */
493
494 #ifdef DETECT_MSIE_IMAGES
495    b = strsav(b, "  <li><code>#define <b>DETECT_MSIE_IMAGES</b></code> - Enables detecting image requests automatically for MSIE.</li>\n");
496 #else /* ifndef DETECT_MSIE_IMAGES */
497    b = strsav(b, "  <li><code>#undef <b>DETECT_MSIE_IMAGES</b></code> - Disables detecting image requests automatically for MSIE.</li>\n");
498 #endif /* ndef DETECT_MSIE_IMAGES */
499
500 #ifdef IMAGE_BLOCKING
501    b = strsav(b, "  <li><code>#define <b>IMAGE_BLOCKING</b></code> - Enables sending \"blocked\" images instead of HTML.</li>\n");
502 #else /* ifndef IMAGE_BLOCKING */
503    b = strsav(b, "  <li><code>#undef <b>IMAGE_BLOCKING</b></code> - Disables sending \"blocked\" images instead of HTML.</li>\n");
504 #endif /* ndef IMAGE_BLOCKING */
505
506 #ifdef ACL_FILES
507    b = strsav(b, "  <li><code>#define <b>ACL_FILES</b></code> - Enables the use of ACL files to control access to the proxy by IP address.</li>\n");
508 #else /* ifndef ACL_FILES */
509    b = strsav(b, "  <li><code>#undef <b>ACL_FILES</b></code> - Disables the use of ACL files to control access to the proxy by IP address.</li>\n");
510 #endif /* ndef ACL_FILES */
511
512 #ifdef TRUST_FILES
513    b = strsav(b, "  <li><code>#define <b>TRUST_FILES</b></code> - Enables the use of trust files.</li>\n");
514 #else /* ifndef TRUST_FILES */
515    b = strsav(b, "  <li><code>#undef <b>TRUST_FILES</b></code> - Disables the use of trust files.</li>\n");
516 #endif /* ndef TRUST_FILES */
517
518 #ifdef JAR_FILES
519    b = strsav(b, "  <li><code>#define <b>JAR_FILES</b></code> - Enables the use of jar files to capture cookies.</li>\n");
520 #else /* ifndef JAR_FILES */
521    b = strsav(b, "  <li><code>#undef <b>JAR_FILES</b></code> - Disables the use of jar files to capture cookies.</li>\n");
522 #endif /* ndef JAR_FILES */
523
524 #ifdef FAST_REDIRECTS
525    b = strsav(b, "  <li><code>#define <b>FAST_REDIRECTS</b></code> - Enables intercepting remote script redirects.</li>\n");
526 #else /* ifndef FAST_REDIRECTS */
527    b = strsav(b, "  <li><code>#undef <b>FAST_REDIRECTS</b></code> - Disables intercepting remote script redirects.</li>\n");
528 #endif  ndef /* FAST_REDIRECTS */
529
530    b = strsav(b, "</ul>\n<br>\n");
531
532    b = strsav(b,
533       "<small><small><p>\n"
534       "The " BANNER " Proxy - \n"
535       "<a href=\"" HOME_PAGE_URL "\">" HOME_PAGE_URL "</a><p>\n"
536       "Copyright &#169; 2001 <a href=\"" HOME_PAGE_URL "\">the SourceForge IJBSWA team</a><br>\n"
537       "Copyright &#169; 1997 <a href=\"http://www.junkbusters.com/\">\n" "Junkbusters Corporation</a><br>\n"
538       "Copying and distribution permitted under the "
539       "<a href=\"http://www.gnu.org/copyleft/gpl.html\">GNU General Public License.</a>\n"
540       "</small></small>"
541       "</body></html>\n"
542    );
543
544    config->proxy_args_trailer = b;
545
546 }
547
548
549 /*
550   Local Variables:
551   tab-width: 3
552   end:
553 */