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