Add Roland Rosenfeld to contributors.
[privoxy.git] / killpopup.c
1 const char killpopup_rcs[] = "$Id: killpopup.c,v 1.15 2002/03/24 13:25:43 swa 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  *                Privoxy team. http://www.privoxy.org/
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.15  2002/03/24 13:25:43  swa
36  *    name change related issues
37  *
38  *    Revision 1.14  2002/03/07 03:46:53  oes
39  *    Fixed compiler warnings etc
40  *
41  *    Revision 1.13  2001/11/13 00:16:40  jongfoster
42  *    Replacing references to malloc.h with the standard stdlib.h
43  *    (See ANSI or K&R 2nd Ed)
44  *
45  *    Revision 1.12  2001/10/25 03:40:48  david__schmidt
46  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
47  *    threads to call select() simultaneously.  So, it's time to do a real, live,
48  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
49  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
50  *
51  *    Revision 1.11  2001/10/07 15:42:41  oes
52  *    filter_popups now gets a csp pointer so it can raise the new
53  *      CSP_FLAG_MODIFIED flag.
54  *
55  *    Revision 1.10  2001/09/22 16:34:44  jongfoster
56  *    Removing unneeded #includes
57  *
58  *    Revision 1.9  2001/07/31 14:44:22  oes
59  *    Deleted unused size parameter from filter_popups()
60  *
61  *    Revision 1.8  2001/07/30 22:08:36  jongfoster
62  *    Tidying up #defines:
63  *    - All feature #defines are now of the form FEATURE_xxx
64  *    - Permanently turned off WIN_GUI_EDIT
65  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
66  *
67  *    Revision 1.7  2001/07/20 19:29:25  haroon
68  *    - In v1.5 forgot to add that I implemented LOG_LEVEL_POPUPS in errlog.c,
69  *      errlog.h and killpopup.c. In that case, it is superfluous to have define for
70  *      POPUP_VERBOSE, so I removed the defines and logging is now done
71  *      via log_error(LOG_LEVEL_POPUPS, ....)
72  *
73  *    Revision 1.6  2001/07/19 19:11:35  haroon
74  *    - Implemented Guy's idea of replacing window.open( with 1;''.concat(
75  *    - Implemented Guy's idea of replacing .resizeTo( with .scrollTo(
76  *
77  *    Revision 1.5  2001/07/18 15:02:52  haroon
78  *    improved nuking of window.open
79  *
80  *    Revision 1.4  2001/06/29 13:29:55  oes
81  *    Added FIXMEs (and didn't repair, hehe)
82  *
83  *    Revision 1.3  2001/05/22 18:56:28  oes
84  *    CRLF -> LF
85  *
86  *    Revision 1.2  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.1.1.1  2001/05/15 13:58:58  oes
100  *    Initial import of version 2.9.3 source tree
101  *
102  *
103  *********************************************************************/
104 \f
105
106 #include "config.h"
107
108 #include <stdio.h>
109 #include <stdlib.h>
110 #include <sys/types.h>
111 #include <string.h>
112 #include <errno.h>
113 #include <sys/stat.h>
114 #include <ctype.h>
115
116 #if !defined(_WIN32) && !defined(__OS2__)
117 #include <unistd.h>
118 #endif
119
120 #include "project.h"
121 #include "killpopup.h"
122 #include "errlog.h"
123
124 const char killpopup_h_rcs[] = KILLPOPUP_H_VERSION;
125
126 #ifdef FEATURE_KILL_POPUPS
127
128 /*********************************************************************
129  *
130  * Function    :  filter_popups
131  *
132  * Description :  Filter the block of data that's been read from the server
133  *                for javascript popup code and replace by syntactically
134  *                neutral code of the same size.
135  *                Raise the CSP_FLAG_MODIFIED flag on success.
136  *
137  * Parameters  :
138  *          1  :  buff = Buffer to scan and modify.  Null terminated.
139  *          2  :  csp = Client state pointer
140  *
141  * Returns     :  void
142  *
143  *********************************************************************/
144 void filter_popups(char *buff, struct client_state *csp)
145 {
146    char *start_p = NULL;
147    char *close_p = NULL;
148    char *p     = NULL;
149
150    /*
151     * replace the window.open( with a harmless JavaScript replacement
152     * (notice the two single quotes)
153     */
154    while ((start_p = strstr(buff, "window.open(")) != NULL)
155    {
156       if (start_p)
157       {
158          strncpy(start_p, "1;''.concat(", 12);
159          log_error(LOG_LEVEL_POPUPS, "Blocked popup window open");
160          csp->flags |= CSP_FLAG_MODIFIED;
161       }
162    }
163
164    /*
165     * replace the .resizeTo( with a harmless JavaScript replacement
166     */
167    while ((start_p = strstr(buff, ".resizeTo(")) != NULL)
168    {
169       if (start_p)
170       {
171          strncpy(start_p, ".scrollTo(", 10);
172          log_error(LOG_LEVEL_POPUPS, "Blocked popup window resize");
173          csp->flags |= CSP_FLAG_MODIFIED;
174       }
175    }
176
177    /* 
178     * Filter onUnload and onExit
179     */
180    start_p = strstr(buff, "<body");
181    if (!start_p) start_p = strstr(buff, "<BODY");
182    if (!start_p) start_p = strstr(buff, "<Body");
183    if (!start_p) start_p = strstr(buff, "<BOdy");
184    if (start_p)
185    {
186       close_p = strchr(start_p, '>');
187       if (close_p)
188       {
189          /* we are now between <body and the ending > */
190          p = strstr(start_p, "onUnload");
191          if (p)
192          {
193             strncpy(p, "_nU_", 4);
194             csp->flags |= CSP_FLAG_MODIFIED;
195          }
196          p = strstr(start_p, "onExit");
197          if (p)
198          {
199             strncpy(p, "_nE_", 4);
200             csp->flags |= CSP_FLAG_MODIFIED;
201          }
202       }
203    }
204
205 }
206
207 #endif /* def FEATURE_KILL_POPUPS */
208
209 /*
210   Local Variables:
211   tab-width: 3
212   end:
213 */