1 package es.caib.signatura.impl;
2
3 import java.io.FileDescriptor;
4 import java.io.FilePermission;
5 import java.net.InetAddress;
6 import java.security.AccessController;
7 import java.security.Permission;
8 import java.security.PrivilegedAction;
9 import java.security.SecurityPermission;
10
11 public class CAIBSecurityManager extends java.lang.SecurityManager {
12 private java.lang.SecurityManager proxy;
13 private static boolean isRegistered = false;
14 private ClassLoader trustedClassLoader;
15 private ClassLoader systemClassLoader;
16 private ThreadLocal checkIn = new ThreadLocal() {
17 private boolean ckechInBoolean = false;
18 protected synchronized Object initialValue() {
19 return new Boolean(ckechInBoolean);
20 }
21 public synchronized void set(Object booleanObject) {
22 if(booleanObject != null){
23 if(booleanObject instanceof Boolean){
24 Boolean booleanValue = (Boolean)booleanObject;
25 ckechInBoolean = booleanValue.booleanValue();
26 }
27 }
28 }
29 public synchronized Object get() {
30 return new Boolean(ckechInBoolean);
31 }
32 };
33
34
35 private boolean hasAllPermission ()
36 {
37 if ( getInCheck() )
38 return true;
39 try{
40 checkIn.set(new Boolean(true));
41 Class classes [] = getClassContext();
42 for (int i = classes.length - 1; i >= 0; i--)
43 {
44 try{
45 ClassLoader cl = classes[i].getClassLoader();
46 if (classes[i] != CAIBSecurityManager.class &&
47 cl == CAIBSecurityManager.class.getClassLoader() &&
48 classes[i].getPackage().getName().startsWith("es.caib.signatura."))
49 {
50 checkIn.set(new Boolean(false));
51 return true;
52 }
53 }catch(Exception e){
54 System.out.println("WARNING. Exception at security manager:");
55 e.printStackTrace(System.out);
56
57
58
59
60
61 }
62 }
63 }finally{
64 checkIn.set(new Boolean(false));
65 }
66 return false;
67 }
68
69
70 public static void register ()
71 {
72 AccessController.doPrivileged(
73 new PrivilegedAction() {
74 public Object run() {
75 try {
76 if ( ! isRegistered && System.getSecurityManager() != null)
77 {
78 System.setSecurityManager( new CAIBSecurityManager (System.getSecurityManager()));
79 isRegistered = true;
80 }
81 } catch (Throwable t) {
82 t.printStackTrace() ;
83 }
84 return null;
85 };
86 }
87 );
88 }
89
90 public CAIBSecurityManager(java.lang.SecurityManager proxy) {
91 this.proxy = proxy;
92 try {
93 trustedClassLoader = ClassLoaderFactory.getFactory().getMasterClassLoader();
94 systemClassLoader = getClass().getClassLoader();
95 } catch (Throwable e) {
96 e.printStackTrace();
97 }
98 }
99
100 public void checkAccept(String host, int port) {
101 proxy.checkAccept(host, port);
102 }
103
104 public void checkAccess(Thread t) {
105 proxy.checkAccess(t);
106 }
107
108 public void checkAccess(ThreadGroup g) {
109 proxy.checkAccess(g);
110 }
111
112 public void checkAwtEventQueueAccess() {
113 proxy.checkAwtEventQueueAccess();
114 }
115
116 public void checkConnect(String host, int port, Object context) {
117 if (! hasAllPermission())
118 proxy.checkConnect(host, port, context);
119 }
120
121 public void checkConnect(String host, int port) {
122 if (hasAllPermission())
123 return;
124 proxy.checkConnect(host, port);
125 }
126
127 public void checkCreateClassLoader() {
128 if (!hasAllPermission())
129 proxy.checkCreateClassLoader();
130 }
131
132 public void checkDelete(String file) {
133 if (hasAllPermission() &&
134 file.startsWith(System.getProperty("java.io.tmpdir")) &&
135 file.endsWith("pkcs11.cfg"))
136 return;
137 proxy.checkDelete(file);
138 }
139
140 public void checkExec(String cmd) {
141 if (! hasAllPermission())
142 proxy.checkExec(cmd);
143 }
144
145 public void checkExit(int status) {
146 proxy.checkExit(status);
147 }
148
149 public void checkLink(String lib) {
150 if (! hasAllPermission())
151 proxy.checkLink(lib);
152 }
153
154 public void checkListen(int port) {
155 proxy.checkListen(port);
156 }
157
158 public void checkMemberAccess(Class arg0, int arg1) {
159 if(hasAllPermission()){
160 return;
161 }
162 proxy.checkMemberAccess(arg0, arg1);
163 }
164
165 public void checkMulticast(InetAddress maddr, byte ttl) {
166 proxy.checkMulticast(maddr, ttl);
167 }
168
169 public void checkMulticast(InetAddress maddr) {
170 proxy.checkMulticast(maddr);
171 }
172
173 public void checkPackageAccess(String pkg) {
174 if(hasAllPermission()){
175 return;
176 }
177 if (pkg.startsWith("sun.security.pkcs11")){
178 return ;
179 }
180 proxy.checkPackageAccess(pkg);
181
182 }
183
184 public void checkPackageDefinition(String pkg) {
185 proxy.checkPackageDefinition(pkg);
186 }
187
188 public void checkPermission(Permission perm, Object context) {
189 if( perm instanceof java.util.logging.LoggingPermission && hasAllPermission()){
190 return;
191 }
192 if(perm instanceof java.lang.RuntimePermission && hasAllPermission()){
193 return;
194 }
195 proxy.checkPermission(perm, context);
196 }
197
198 public void checkPermission(Permission perm) {
199 if (perm instanceof FilePermission)
200 {
201 FilePermission fperm = (FilePermission) perm;
202 if ("read".equals(fperm.getActions()) && hasAllPermission())
203 {
204 return;
205 }
206 }
207 if (perm instanceof SecurityPermission && perm.getName().startsWith("authProvider.SunPKCS11-"))
208 return;
209 if (perm instanceof SecurityPermission && perm.getName().startsWith("authProvider.SunMSCAPI"))
210 return;
211 if(perm instanceof java.util.logging.LoggingPermission && hasAllPermission()){
212 return;
213 }
214 if(perm instanceof java.lang.RuntimePermission && hasAllPermission()){
215 return;
216 }
217
218
219 if(perm instanceof java.net.NetPermission && "specifyStreamHandler".equals(perm.getName()) && hasAllPermission()){
220 return;
221 }
222
223 proxy.checkPermission(perm);
224 }
225
226 public void checkPrintJobAccess() {
227 proxy.checkPrintJobAccess();
228 }
229
230 public void checkPropertiesAccess() {
231 if (! hasAllPermission())
232 proxy.checkPropertiesAccess();
233 }
234
235 public void checkPropertyAccess(String key) {
236
237 if (! hasAllPermission())
238 proxy.checkPropertyAccess(key);
239 }
240
241 public void checkRead(FileDescriptor fd) {
242
243 if (! hasAllPermission())
244 proxy.checkRead(fd);
245 }
246
247 public void checkRead(String file, Object context) {
248
249 if (! hasAllPermission())
250 proxy.checkRead(file, context);
251 }
252
253 public void checkRead(String file) {
254
255 if (! hasAllPermission())
256 proxy.checkRead(file);
257 }
258
259 public void checkSecurityAccess(String target) {
260 if ( (target.startsWith("putProviderProperty.") ||
261 target.startsWith ("insertProvider.")) &&
262 hasAllPermission())
263 {
264 return ;
265 }
266 proxy.checkSecurityAccess(target);
267 }
268
269 public void checkSetFactory() {
270
271 proxy.checkSetFactory();
272 }
273
274 public void checkSystemClipboardAccess() {
275
276 proxy.checkSystemClipboardAccess();
277 }
278
279 public boolean checkTopLevelWindow(Object window) {
280
281 return proxy.checkTopLevelWindow(window);
282 }
283
284 public void checkWrite(FileDescriptor fd) {
285
286 if(hasAllPermission()) return;
287 proxy.checkWrite(fd);
288 }
289
290 public void checkWrite(String file) {
291 if (hasAllPermission() && (
292 file.startsWith(System.getProperty("java.io.tmpdir")) &&
293 file.endsWith("pkcs11.cfg") ||
294 file.contains("signatura_api.properties") ||
295 file.contains("serviceLog.txt")
296 )
297 )
298 return;
299 proxy.checkWrite(file);
300 }
301
302 public boolean equals(Object obj) {
303
304 return proxy.equals(obj);
305 }
306
307 public boolean getInCheck() {
308 boolean toReturn = false;
309 if(checkIn.get() != null){
310 if(checkIn.get() instanceof Boolean){
311 if(((Boolean)checkIn.get()).booleanValue()){
312 toReturn = true;
313 }
314 }
315 }
316 toReturn = toReturn || proxy.getInCheck();
317 return toReturn;
318 }
319
320 public Object getSecurityContext() {
321
322 return proxy.getSecurityContext();
323 }
324
325 public ThreadGroup getThreadGroup() {
326 return proxy.getThreadGroup();
327 }
328
329 public int hashCode() {
330 return proxy.hashCode();
331 }
332
333 public String toString() {
334 return proxy.toString();
335 }
336
337 }