summaryrefslogtreecommitdiff
path: root/java/jdk15/files/patch-plugin_DispatchImpl.java
blob: abb65a60953e3e3c59da4fa88270c297e9176117 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
$FreeBSD$

--- ../../deploy/src/plugin/src/share/classes/sun/plugin/com/DispatchImpl.java	22 Oct 2003 23:04:20 -0000	1.1
+++ ../../deploy/src/plugin/src/share/classes/sun/plugin/com/DispatchImpl.java	3 Dec 2004 03:56:58 -0000	1.2
@@ -1,7 +1,7 @@
 /*
- * @(#)DispatchImpl.java	1.6 03/01/23
+ * @(#)DispatchImpl.java	1.16 04/06/20
  *
- * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
+ * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  */
 
@@ -9,8 +9,32 @@
 
 import sun.plugin.util.Trace;
 import sun.plugin.javascript.ocx.JSObject;
-import java.applet.Applet;
+import sun.plugin.liveconnect.JavaScriptProtectionDomain;
 import sun.plugin.viewer.context.IExplorerAppletContext;
+import sun.plugin.security.PluginClassLoader;
+import java.applet.Applet;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.net.SocketPermission;
+import java.io.FilePermission;
+import java.io.File;
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.CodeSource;
+import java.security.Policy;
+import java.security.ProtectionDomain;
+import java.security.AllPermission;
+import java.security.Permissions;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
+import java.security.AccessControlException;
+import java.security.PrivilegedActionException;
+import sun.net.www.ParseUtil;
+import sun.security.util.SecurityConstants;
 
 /**
  *  DispatchImpl encapsulates a Java Object and provides Dispatch interface
@@ -22,14 +46,19 @@
 {
     JavaClass targetClass = null;
     Object targetObj = null;
+    int handle = 0;
+    int wndHandle = 0;
+    AccessControlContext context = null;
+    boolean isBridge = false;
 
     /*
      * Constructor
      * @param obj the object to be wrapped
      */
-    public DispatchImpl(Object obj)
+    public DispatchImpl(Object obj, int id)
     {
 	targetObj = obj;
+	handle = id;
     }
 
     /**
@@ -40,19 +69,87 @@
      * @param params Arguments.
      * @return Java object.
      */
-    public Object invoke(int flag, int index, Object []params)
+    public Object invoke(final int flag, final int index, final Object []params)
+        throws  Exception
+    {
+	try {
+	    //No security constraints in case of ActiveX bridge application
+	    if(isBridge)
+		return invokeImpl(flag, index, params);
+
+	    if(context == null) {
+		context = createContext();
+	    }
+
+	    // Invoke the method within the applet sand box security restricitions
+	    return AccessController.doPrivileged(
+		new PrivilegedExceptionAction() {
+		    public Object run() throws Exception{
+			return invokeImpl(flag, index, params);
+		    }
+		}, context	
+	    );
+	}catch(Exception exc) {
+	    Throwable cause = exc.getCause();
+	    if(cause == null) {
+		cause = exc;
+	    }
+
+       	    Trace.liveConnectPrintException(cause);
+	    throw new Exception(cause.toString());
+	}
+    }
+
+    public AccessControlContext createContext() {
+    	try {
+	    ProtectionDomain[] domains = new ProtectionDomain[1];
+	    //Obtain the java code origin
+	    ProtectionDomain pd = (ProtectionDomain)AccessController.doPrivileged(new PrivilegedAction() {
+		public Object run() {
+		    return targetObj.getClass().getProtectionDomain();
+		}
+	    });
+
+	    CodeSource cs = null;
+	    URL url = null;
+	    if(pd != null)
+		cs = pd.getCodeSource();
+	    if(cs != null)
+		url = cs.getLocation();
+
+	    domains[0] = getJSProtectionDomain(url, targetObj.getClass());
+	    return new AccessControlContext(domains);
+	}catch(Exception exc) {
+       	    Trace.liveConnectPrintException(exc);
+	}
+	
+	return null;    
+    }
+
+    /**
+     * Invoke a method according to the method index.
+     *
+     * @param flag Invoke flag
+     * @param index Method index
+     * @param params Arguments.
+     * @return Java object.
+     */
+    public Object invokeImpl(int flag, int index, Object []params)
         throws  Exception
     {
 	Object retObj = null;
 	Dispatcher disp = null;
 	try {
-	    convertParams(params);
+	    if(params != null)
+		convertParams(params);
 	    disp = targetClass.getDispatcher(flag, index, params);
-	    return disp.invoke(targetObj, params);
-        }
-        catch (Throwable e)
-        {
-	    //e.printStackTrace();
+	    if(disp != null) {
+		retObj = disp.invoke(targetObj, params);
+		if(retObj != null)
+		    retObj = Utils.convertReturn(disp.getReturnType(), retObj, handle);
+	    }
+	    return retObj;		
+        } catch (Throwable e) {
 	    Throwable cause = e.getCause();
 	    if(cause == null) {
 		cause = e;
@@ -82,11 +179,39 @@
 	return targetClass;
     }
 
+    public int getReturnType(int id){
+	return targetClass.getReturnType(id);
+    }
+
+    public int getIdForName(final String name) throws Exception{
+	try {
+	    //No security constraints in case of ActiveX bridge application
+	    if(isBridge)
+		return getIdForNameImpl(name);
+
+	    if(context == null) {
+		context = createContext();
+	    }
+
+	    // Invoke the method within the applet sand box security restricitions
+	    Integer retVal = (Integer)AccessController.doPrivileged(
+		new PrivilegedExceptionAction() {
+		    public Object run() throws Exception{
+			return new Integer(getIdForNameImpl(name));
+		    }
+		}, context	
+	    );
+	    return retVal.intValue();
+	}catch(PrivilegedActionException pe) {
+	}
+
+	return -1;
+    }
 
     /*
      *
      */
-    public int getIdForName(String name) throws Exception{
+    public int getIdForNameImpl(String name) throws Exception{
 	int id = -1;
 
 	if(targetClass == null && targetObj != null) {
@@ -103,27 +228,122 @@
     }
 
     /*
-     *
+     * Unwraps the wrapped java object arguments
      */
     private void convertParams(Object []params) {
 	for(int i=0;i<params.length;i++) {
 	    if(params[i] != null && params[i] instanceof DispatchImpl) {
 		params[i] = ((DispatchImpl)params[i]).getWrappedObject();
 	    } else if(params[i] != null && params[i] instanceof DispatchClient){
-		JSObject jsObj = new JSObject((DispatchClient)params[i]);
-		jsObj.setIExplorerAppletContext((IExplorerAppletContext)
+		JSObject jsObj = null;
+		if (!isBridge) {
+		    jsObj = new JSObject((DispatchClient)params[i]);
+		    jsObj.setIExplorerAppletContext((IExplorerAppletContext)
 				((Applet)targetObj).getAppletContext());
+		} else {
+		    jsObj = new JSObject((DispatchClient)params[i], handle);
+		}		    
 		params[i] = jsObj;
 	    }
 	}
     }
 
+    /**
+     * Returns a protection domain that represents the default permission
+     * for a given URL.
+     *
+     * @param urlString URL
+     * @return protection domain.
+     */
+    public static ProtectionDomain getJSProtectionDomain(URL url, Class clazz)
+	throws MalformedURLException {
+	
+	// Obtain default java applet policy
+	Policy policy = (Policy)AccessController.doPrivileged(new PrivilegedAction() {
+	    public Object run() {
+		return Policy.getPolicy();
+	    }
+	});
+
+	CodeSource cs = new CodeSource(null, (java.security.cert.Certificate[])null);
+	final PermissionCollection pc = policy.getPermissions(cs);
+	if(url != null) {
+	    Permission p;
+	    String path = null;
+	    try {
+		p = url.openConnection().getPermission();
+	    } catch (java.io.IOException ioe) {
+		p = null;
+	    }
+
+	    if (p instanceof FilePermission) {
+		path = p.getName();
+	    } else if ((p == null) && (url.getProtocol().equals("file"))) {
+		path = url.getFile().replace('/', File.separatorChar);
+		path = ParseUtil.decode(path);
+	    } else if (p instanceof SocketPermission) {
+		/* 
+		Socket permission to connect back to the host
+		*/
+		String host = url.getHost();
+		pc.add(new SocketPermission(host,  
+		    SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
+	    }
+
+	    if(path != null &&
+		(clazz.getClassLoader() instanceof PluginClassLoader)) {
+		//We need to add an additional permission to read recursively
+		if (path.endsWith(File.separator)) {
+		    path += "-";
+		} else {
+		    int endIndex = path.lastIndexOf(File.separatorChar);
+		    if (endIndex != -1)
+			path = path.substring(0, endIndex+1) + "-";
+		}
+
+		pc.add(new FilePermission(path, SecurityConstants.FILE_READ_ACTION));
+
+		/* 
+		Socket permission to connect back to the "localhost"
+		*/
+		pc.add(new SocketPermission("localhost", 
+		    SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
+		AccessController.doPrivileged(new PrivilegedAction() {
+		    public Object run() {
+			try {
+			    String host = InetAddress.getLocalHost().getHostName();
+			    pc.add(new SocketPermission(host, 
+				SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
+			} catch (UnknownHostException uhe) {
+			}
+			return null;
+		    }
+		});
+	    }
+	}
+
+	return new JavaScriptProtectionDomain(pc);
+    }
+
     public String toString() {
 	if(targetObj != null) {
 	    return targetObj.toString();
 	}
 	return null;
     }
+
+    public int getWindowHandle() {
+	if(wndHandle == 0) {
+	    wndHandle = getWindowHandle(handle);
+	}
+	return wndHandle;
+    }
+
+    protected void setBridge() {
+	isBridge = true;
+    }
+
+    native int getWindowHandle(int id);
 }