3ad443717d0b080c211dd41063d2a0c626f71ff8
[privoxy.git] / killpopup.c
1 const char killpopup_rcs[] = "$Id: killpopup.c,v 1.5 2001/07/18 15:02:52 haroon Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/killpopup.c,v $
5  *
6  * Purpose     :  Handles the filtering of popups.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
9  *                IJBSWA team.  http://ijbswa.sourceforge.net
10  *
11  *                Based on the Internet Junkbuster originally written
12  *                by and Copyright (C) 1997 Anonymous Coders and 
13  *                Junkbusters Corporation.  http://www.junkbusters.com
14  *
15  *                This program is free software; you can redistribute it 
16  *                and/or modify it under the terms of the GNU General
17  *                Public License as published by the Free Software
18  *                Foundation; either version 2 of the License, or (at
19  *                your option) any later version.
20  *
21  *                This program is distributed in the hope that it will
22  *                be useful, but WITHOUT ANY WARRANTY; without even the
23  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
24  *                PARTICULAR PURPOSE.  See the GNU General Public
25  *                License for more details.
26  *
27  *                The GNU General Public License should be included with
28  *                this file.  If not, you can view it at
29  *                http://www.gnu.org/copyleft/gpl.html
30  *                or write to the Free Software Foundation, Inc., 59
31  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  *
33  * Revisions   :
34  *    $Log: killpopup.c,v $
35  *    Revision 1.5  2001/07/18 15:02:52  haroon
36  *    improved nuking of window.open
37  *
38  *    Revision 1.4  2001/06/29 13:29:55  oes
39  *    Added FIXMEs (and didn't repair, hehe)
40  *
41  *    Revision 1.3  2001/05/22 18:56:28  oes
42  *    CRLF -> LF
43  *
44  *    Revision 1.2  2001/05/20 01:21:20  jongfoster
45  *    Version 2.9.4 checkin.
46  *    - Merged popupfile and cookiefile, and added control over PCRS
47  *      filtering, in new "permissionsfile".
48  *    - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
49  *      file error you now get a message box (in the Win32 GUI) rather
50  *      than the program exiting with no explanation.
51  *    - Made killpopup use the PCRS MIME-type checking and HTTP-header
52  *      skipping.
53  *    - Removed tabs from "config"
54  *    - Moved duplicated url parsing code in "loaders.c" to a new funcition.
55  *    - Bumped up version number.
56  *
57  *    Revision 1.1.1.1  2001/05/15 13:58:58  oes
58  *    Initial import of version 2.9.3 source tree
59  *
60  *
61  *********************************************************************/
62 \f
63
64 #include "config.h"
65
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <sys/types.h>
69 #include <string.h>
70 #include <malloc.h>
71 #include <errno.h>
72 #include <sys/stat.h>
73 #include <ctype.h>
74
75 #ifndef _WIN32
76 #include <unistd.h>
77 #endif
78
79 #include "project.h"
80 #include "killpopup.h"
81 #include "jcc.h"
82 #include "errlog.h"
83
84 const char killpopup_h_rcs[] = KILLPOPUP_H_VERSION;
85
86 #ifdef KILLPOPUPS
87
88 /* Change these for debug output.  *lots*. */
89 /*#define POPUP_VERBOSE 1*/
90 #undef POPUP_VERBOSE
91
92
93 /*********************************************************************
94  *
95  * Function    :  filter_popups
96  *
97  * Description :  Filter the block of data that's been read from the server.
98  *                Caller is responsible for checking permissons list
99  *                to determine if this function should be called.
100  *                Remember not to change the content length (substitute char by char)
101  *
102  * Parameters  :
103  *          1  :  buff = Buffer to scan and modify.  Null terminated.
104  *          2  :  size = Buffer size, excluding null terminator.
105  *
106  * Returns     :  void
107  *
108  *********************************************************************/
109 void filter_popups(char *buff, int size)
110 {
111    char *popup = NULL;
112    char *close = NULL;
113    char *p     = NULL;
114
115    while ((popup = strstr( buff, "window.open(" )) != NULL)
116    {
117       if ( popup )
118       {
119          /*
120           * replace the window.open( with a harmless JavaScript replacement (notice the two single quotes)
121           * Guy's idea (thanks)
122           */
123          strncpy(popup, "1;''.concat(", 12);
124 #ifdef POPUP_VERBOSE
125          log_error(LOG_LEVEL_POPUPS, "Blocked popup window open");
126 #endif
127       }
128    }
129    
130    while ((popup = strstr( buff, ".resizeTo(" )) != NULL)
131    {
132       if ( popup )
133       {
134          /*
135           * replace the .resizeTo( with a harmless JavaScript replacement
136           * Guy's idea (thanks)
137           */
138          strncpy(popup, ".scrollTo(", 10);
139 #ifdef POPUP_VERBOSE
140          log_error(LOG_LEVEL_POPUPS, "Blocked popup window resize");
141 #endif
142       }
143    }
144
145    /* Filter all other crap like onUnload onExit etc.  (by BREITENB) NEW!*/
146    popup=strstr( buff, "<body");
147    if (!popup) popup=strstr( buff, "<BODY");
148    if (!popup) popup=strstr( buff, "<Body");
149    if (!popup) popup=strstr( buff, "<BOdy");
150    if (popup)
151    {
152       close=strchr(popup,'>');
153       if (close)
154       {
155          /* we are now between <body and the ending > FIXME: No, we're anywhere! --oes*/
156          p=strstr(popup, "onUnload");
157          if (p)
158          {
159             strncpy(p,"_nU_",4);
160          }
161          p=strstr(popup, "onExit");
162          if (p)
163          {
164             strncpy(p,"_nE_",4);
165          }
166       }
167    }
168
169 }
170
171 #endif /* def KILLPOPUPS */
172
173 /*
174   Local Variables:
175   tab-width: 3
176   end:
177 */