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