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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
|
/* java.lang.SecurityManager
Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.lang;
import java.net.*;
import java.util.*;
import java.io.*;
/**
** SecurityManager is a class you can extend to create
** your own Java security policy. By default, there is
** no SecurityManager installed in 1.1, which means that
** all things are permitted to all people.<P>
**
** The default methods in this class deny all
** things to all people.
**
** @author John Keiser
** @version 1.1.0, 31 May 1998
** @since JDK1.0
**/
public class SecurityManager {
/** Tells whether or not the SecurityManager is currently
** performing a security check.
**/
protected boolean inCheck;
/** Tells whether or not the SecurityManager is currently
** performing a security check.
**
** @return whether or not the SecurityManager is
** currently performing a security check.
**/
public boolean getInCheck() {
return inCheck;
}
/** Get a list of all the classes currently executing
** methods on the Java stack. getClassContext()[0] is
** the currently executing method
** <STRONG>Spec Note:</STRONG> does not say whether
** the stack will include the getClassContext() call or
** the one just before it.
**
** @return an array containing all the methods on classes
** on the Java execution stack.
**/
protected Class[] getClassContext() {
return VMSecurityManager.getClassContext();
}
/** Find the ClassLoader for the most recent class on the
** stack that was loaded by an explicit ClassLoader. If
** everything on the stack was loaded by the system
** classloader, null is returned.
**
** @return the most recent ClassLoader on the execution
** stack.
**/
protected ClassLoader currentClassLoader() {
return VMSecurityManager.currentClassLoader();
}
/** Find the most recent class on the stack that was
** loaded by an explicit ClassLoader. If everything on
** the stack was loaded by the system classloader, null
** is returned.
**
** @return the most recent loaded Class on the execution
** stack.
**/
protected Class currentLoadedClass() {
Class[] c = getClassContext();
for(int i=0;i<c.length;i++) {
if(c[i].getClassLoader() != null) {
return c[i];
}
}
return null;
}
/** Get the depth on the execution stack of the most
** recent class that was loaded by an explicit
** ClassLoader. This can be used as an index into
** getClassContext().
**
** @return the index of the most recent loaded Class on
** the execution stack.
**/
protected int classLoaderDepth() {
Class[] c = getClassContext();
for(int i=0;i<c.length;i++) {
if(c[i].getClassLoader() != null) {
return i;
}
}
return -1;
}
/** Tell whether there is a class loaded with an explicit
** ClassLoader on the stack.
**
** @return whether there is a class loaded with an
** explicit ClassLoader on the stack.
**/
protected boolean inClassLoader() {
return classLoaderDepth() != -1;
}
/** Get the depth of a particular class on the execution
** stack.
**
** @param className the fully-qualified name of the class
** to search for on the stack.
** @return the index of the class on the stack, or -1 if
** the class is not on the stack.
**/
protected int classDepth(String className) {
Class[] c = getClassContext();
for(int i=0;i<c.length;i++) {
if(className.equals(c[i].getName())) {
return i;
}
}
return -1;
}
/** Tell whether the specified class is on the execution
** stack.
**
** @param className the fully-qualified name of the class
** to search for on the stack.
** @return whether the specified class is on the
** execution stack.
**/
protected boolean inClass(String className) {
return classDepth(className) != -1;
}
/** Get an implementation-dependent Object that contains
** enough information about the current environment to be
** able to perform standard security checks later. This
** is used by trusted methods that need to verify that
** their callers have sufficient access to perform
** certain operations.<P>
**
** Currently the only methods that use this are checkRead()
** and checkConnect().
**
** @see checkConnect(java.lang.String,int,java.lang.Object)
** @see checkRead(java.lang.String,java.lang.Object)
**/
public Object getSecurityContext() {
return new SecurityContext(getClassContext());
}
/** Check if the current thread is allowed to create a
** ClassLoader.<P>
**
** This method is called from ClassLoader.ClassLoader(),
** in other words, whenever a ClassLoader is created.<P>
**
** SecurityManager's implementation always denies access.
**
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.ClassLoader#ClassLoader()
**/
public void checkCreateClassLoader() {
throw new SecurityException("Cannot create new ClassLoaders.");
}
/** Check if the current thread is allowed to modify this
** other Thread.<P>
**
** Called by Thread.stop(), suspend(), resume(), and
** interrupt(), destroy(), setPriority(), setName() and
** setDaemon().<P>
**
** SecurityManager's implementation always denies access.
**
** @param g the Thread to check against
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.Thread#stop()
** @see java.lang.Thread#suspend()
** @see java.lang.Thread#resume()
** @see java.lang.Thread#interrupt()
** @see java.lang.Thread#destroy()
** @see java.lang.Thread#setPriority(int)
** @see java.lang.Thread#setName(java.lang.String)
** @see java.lang.Thread#setDaemon(boolean)
**/
public void checkAccess(Thread t) {
throw new SecurityException("Cannot modify Threads.");
}
/** Check if the current thread is allowed to modify this
** ThreadGroup.<P>
**
** Called by Thread.Thread() (to add a thread to the
** ThreadGroup), ThreadGroup.ThreadGroup() (to add this
** ThreadGroup to a parent), ThreadGroup.stop(),
** suspend(), resume(), interrupt(), destroy(),
** setDaemon(), and setMaxPriority().<P>
**
** SecurityManager's implementation always denies access.
**
** @param g the ThreadGroup to check against
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.Thread#Thread()
** @see java.lang.ThreadGroup#ThreadGroup()
** @see java.lang.ThreadGroup#stop()
** @see java.lang.ThreadGroup#suspend()
** @see java.lang.ThreadGroup#resume()
** @see java.lang.ThreadGroup#interrupt()
** @see java.lang.ThreadGroup#setDaemon(boolean)
** @see java.lang.ThreadGroup#setMaxPriority(int)
**/
public void checkAccess(ThreadGroup g) {
throw new SecurityException("Cannot modify ThreadGroups.");
}
/** Check if the current thread is allowed to exit the
** JVM with the given status.<P>
**
** This method is called from Runtime.exit().<P>
**
** SecurityManager's implementation always denies access.
**
** @param status the status to exit with
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.Runtime#exit()
** @see java.lang.Runtime#exit(int)
**/
public void checkExit(int status) {
throw new SecurityException("Cannot exit JVM.");
}
/** Check if the current thread is allowed to execute the
** given program.<P>
**
** This method is called from Runtime.exec().<P>
**
** SecurityManager's implementation always denies access.
**
** @param program the name of the program to exec
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.Runtime#exec(java.lang.String[],java.lang.String[])
**/
public void checkExec(String program) {
throw new SecurityException("Cannot execute programs.");
}
/** Check if the current thread is allowed to link in the
** given native library.<P>
**
** This method is called from Runtime.load() (and hence,
** by loadLibrary() as well).<P>
**
** SecurityManager's implementation always denies access.
**
** @param filename the full name of the library to load
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.Runtime#load(java.lang.String)
**/
public void checkLink(String filename) {
throw new SecurityException("Cannot link native libraries.");
}
/** Check if the current thread is allowed to read the
** given file using the FileDescriptor.<P>
**
** This method is called from
** FileInputStream.FileInputStream().<P>
**
** SecurityManager's implementation always denies access.
**
** @param desc the FileDescriptor representing the file
** to access
** @exception SecurityException if the operation is not
** permitted.
** @see java.io.FileInputStream#FileInputStream(java.io.FileDescriptor)
**/
public void checkRead(FileDescriptor desc) {
throw new SecurityException("Cannot read files via file descriptors.");
}
/** Check if the current thread is allowed to read the
** given file.<P>
**
** This method is called from
** FileInputStream.FileInputStream(),
** RandomAccessFile.RandomAccessFile(), File.exists(),
** canRead(), isFile(), isDirectory(), lastModified(),
** length() and list().<P>
**
** SecurityManager's implementation always denies access.
**
** @param filename the full name of the file to access
** @exception SecurityException if the operation is not
** permitted.
** @see java.io.File
** @see java.io.FileInputStream#FileInputStream(java.lang.String)
** @see java.io.RandomAccessFile#RandomAccessFile(java.lang.String)
**/
public void checkRead(String filename) {
throw new SecurityException("Cannot read files via file names.");
}
/** Check if the current thread is allowed to read the
** given file. using the given SecurityContext.<P>
**
** I know of no core class that calls this method.<P>
**
** SecurityManager's implementation always denies access.
**
** @param filename the full name of the file to access
** @param securityContext the Security Context to
** determine access for.
** @exception SecurityException if the operation is not
** permitted.
**/
public void checkRead(String filename, Object securityContext) {
throw new SecurityException("Cannot read files via file names.");
}
/** Check if the current thread is allowed to write to the
** given file using the FileDescriptor.<P>
**
** This method is called from
** FileOutputStream.FileOutputStream().<P>
**
** SecurityManager's implementation always denies access.
**
** @param desc the FileDescriptor representing the file
** to access
** @exception SecurityException if the operation is not
** permitted.
** @see java.io.FileOutputStream#FileOutputStream(java.io.FileDescriptor)
**/
public void checkWrite(FileDescriptor desc) {
throw new SecurityException("Cannot write files via file descriptors.");
}
/** Check if the current thread is allowed to write to the
** given file.<P>
**
** This method is called from
** FileOutputStream.FileOutputStream(),
** RandomAccessFile.RandomAccessFile(),
** File.canWrite(), mkdir(), and renameTo().<P>
**
** SecurityManager's implementation always denies access.
**
** @param filename the full name of the file to access
** @exception SecurityException if the operation is not
** permitted.
** @see java.io.File#canWrite()
** @see java.io.File#mkdir()
** @see java.io.File#renameTo()
** @see java.io.FileOutputStream#FileOutputStream(java.lang.String)
** @see java.io.RandomAccessFile#RandomAccessFile(java.lang.String)
**/
public void checkWrite(String filename) {
throw new SecurityException("Cannot write files via file names.");
}
/** Check if the current thread is allowed to delete the
** given file.<P>
**
** This method is called from File.delete().<P>
**
** SecurityManager's implementation always denies access.
**
** @param filename the full name of the file to delete
** @exception SecurityException if th operation is not
** permitted.
** @see java.io.File#delete()
**/
public void checkDelete(String filename) {
throw new SecurityException("Cannot delete files.");
}
/** Check if the current thread is allowed to connect to a
** given host on a given port.<P>
**
** This method is called from Socket.Socket().
**
** SecurityManager's implementation always denies access.
**
** @param host the host to connect to
** @param port the port to connect on
** @exception SecurityException if the operation is not
** permitted
** @see java.net.Socket#Socket()
**/
public void checkConnect(String host, int port) {
throw new SecurityException("Cannot make network connections.");
}
/** Check if the current thread is allowed to connect to a
** given host on a given port using a specific security
** context to determine access.<P>
**
** This method is not called in the 1.1 core classes.<P>
**
** SecurityManager's implementation always denies access.
**
** @param host the host to connect to
** @param port the port to connect on
** @param securityContext the security context to
** determine access with
** @exception SecurityException if the operation is not
** permitted
**/
public void checkConnect(String host, int port, Object securityContext) {
throw new SecurityException("Cannot make network connections.");
}
/** Check if the current thread is allowed to listen to a
** specific port for data.<P>
**
** This method is called by ServerSocket.ServerSocket().<P>
**
** SecurityManager's implementation always denies access.
**
** @param port the port to listen on
** @exception SecurityException if the operation is not
** permitted
** @see java.net.ServerSocket#ServerSocket(int)
**/
public void checkListen(int port) {
throw new SecurityException("Cannot listen for connections.");
}
/** Check if the current thread is allowed to accept a
** connection from a particular host on a particular
** port.<P>
**
** This method is called by ServerSocket.implAccept().<P>
**
** SecurityManager's implementation always denies access.
**
** @param host the host which wishes to connect
** @param port the port the connection will be on
** @exception SecurityException if the operation is not
** permitted
** @see java.net.ServerSocket#accept()
**/
public void checkAccept(String host, int port) {
throw new SecurityException("Cannot accept connections.");
}
/** Check if the current thread is allowed to read and
** write multicast to a particular address.<P>
**
** SecurityManager's implementation always denies access.
**
** @XXX where is it called?
**
** @param addr the address to multicast to.
** @exception SecurityException if the operation is not
** permitted.
**/
public void checkMulticast(InetAddress addr) {
throw new SecurityException("Cannot read or write multicast.");
}
/** Check if the current thread is allowed to read and
** write multicast to a particular address with a
** particular ttl value.<P>
**
** SecurityManager's implementation always denies access.<P>
**
** @XXX where is it called?
**
** @XXX what the hell is ttl? Expand abbreviation.
**
** @param addr the address to multicast to.
** @param ttl the ttl value to use
** @exception SecurityException if the operation is not
** permitted.
**/
public void checkMulticast(InetAddress addr, byte ttl) {
throw new SecurityException("Cannot read or write multicast.");
}
/**
** Check if the current thread is allowed to perform an
** operation that requires the specified <code>Permission</code>.
**
** @param perm The <code>Permission</code> required.
** @exception SecurityException If the operation is not allowed.
**/
public void checkPermission(java.security.Permission perm) {
throw new SecurityException("Operation not allowed");
}
/**
** Check if the current thread is allowed to perform an
** operation that requires the specified <code>Permission</code>.
**
** @param perm The <code>Permission</code> required.
** @param context A security context
** @exception SecurityException If the operation is not allowed.
** @since 1.2
**/
public void checkPermission(java.security.Permission perm,
Object context) {
throw new SecurityException("Operation not allowed");
}
/** Check if the current thread is allowed to read or
** write all the system properties at once.<P>
**
** This method is called by System.getProperties()
** and setProperties().<P>
**
** SecurityManager's implementation always denies access.
**
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.System#getProperties()
** @see java.lang.System#setProperties(java.util.Properties)
**/
public void checkPropertiesAccess() {
throw new SecurityException("Cannot access all system properties at once.");
}
/** Check if the current thread is allowed to read or
** write a particular system property.<P>
**
** This method is called by System.getProperty() and
** setProperty().<P>
**
** SecurityManager's implementation always denies access.
**
** @exception SecurityException is the operation is not
** permitted.
** @see java.lang.System#getProperty(java.lang.String)
** @see java.lang.System#setProperty(java.lang.String,java.lang.String)
**/
public void checkPropertyAccess(String name) {
throw new SecurityException("Cannot access individual system properties.");
}
/** Check if the current thread is allowed to create a
** top-level window. If it is not, the operation should
** still go through, but some sort of nonremovable
** warning should be placed on the window to show that it
** is untrusted.<P>
**
** This method is called by Window.Window().<P>
**
** SecurityManager's implementation always denies access.
**
** @param window the window to create
** @see java.awt.Window#Window(java.awt.Frame)
**/
public boolean checkTopLevelWindow(Object window) {
return false;
}
/** Check if the current thread is allowed to create a
** print job.<P>
**
** This method is called by Toolkit.getPrintJob(). (I
** assume so, at least, it just don't say nothing about
** it in the spec.)<P>
**
** SecurityManager's implementation always denies access.
**
** @exception SecurityException if the operation is not
** permitted.
** @see java.awt.Toolkit.getPrintJob(java.awt.Frame,java.lang.String,java.util.Properties)
**/
public void checkPrintJobAccess() {
throw new SecurityException("Cannot create print jobs.");
}
/** Check if the current thread is allowed to use the
** system clipboard.<P>
**
** This method is called by Toolkit.getSystemClipboard().
** (I assume.)<P>
**
** SecurityManager's implementation always denies access.
**
** @exception SecurityException if the operation is not
** permitted.
** @see java.awt.Toolkit#getSystemClipboard()
**/
public void checkSystemClipboardAccess() {
throw new SecurityException("Cannot access the system clipboard.");
}
/** Check if the current thread is allowed to use the AWT
** event queue.<P>
**
** This method is called by Toolkit.getSystemEventQueue().<P>
**
** SecurityManager's implementation always denies access.
**
** @exception SecurityException if the operation is not
** permitted.
** @see java.awt.Toolkit#getSystemEventQueue()
**/
public void checkAwtEventQueueAccess() {
throw new SecurityException("Cannot access the AWT event queue.");
}
/** Check if the current thread is allowed to access the
** specified package at all.<P>
**
** This method is called by ClassLoader.loadClass() in
** user-created ClassLoaders.<P>
**
** SecurityManager's implementation always denies access.
**
** @param packageName the package name to check access to
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.ClassLoader#loadClass(java.lang.String,boolean)
**/
public void checkPackageAccess(String packageName) {
throw new SecurityException("Cannot access packages via the ClassLoader.");
}
/** Check if the current thread is allowed to define
** classes the specified package. If the class already
** created, though, ClassLoader.loadClass() can still
** return the Class if checkPackageAccess() checks out.<P>
**
** This method is called by ClassLoader.loadClass() in
** user-created ClassLoaders.<P>
**
** SecurityManager's implementation always denies access.
**
** @param packageName the package name to check access to
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.ClassLoader#loadClass(java.lang.String,boolean)
**/
public void checkPackageDefinition(String packageName) {
throw new SecurityException("Cannot load classes into any packages via the ClassLoader.");
}
/** Check if the current thread is allowed to set the
** current socket factory.<P>
**
** This method is called by Socket.setSocketImplFactory(),
** ServerSocket.setSocketFactory(), and
** URL.setURLStreamHandlerFactory().<P>
**
** SecurityManager's implementation always denies access.
**
** @exception SecurityException if the operation is not
** permitted.
** @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)
** @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
** @see java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory)
**/
public void checkSetFactory() {
throw new SecurityException("Cannot set the socket factory.");
}
/** Check if the current thread is allowed to get certain
** types of Methods, Fields and Constructors from a Class
** object.<P>
**
** This method is called by Class.getMethod[s](),
** Class.getField[s](), Class.getConstructor[s],
** Class.getDeclaredMethod[s](),
** Class.getDeclaredField[s](), and
** Class.getDeclaredConstructor[s]().<P>
**
** SecurityManager's implementation always denies access.
**
** @param c the Class to check
** @param memberType the type of members to check
** against, either Member.DECLARED or
** Member.PUBLIC.
** @exception SecurityException if the operation is not
** permitted.
** @see java.lang.Class
** @see java.lang.reflect.Member#DECLARED
** @see java.lang.reflect.Member#PUBLIC
**/
public void checkMemberAccess(Class c, int memberType) {
throw new SecurityException("Cannot access members of classes.");
}
/** Test whether a particular security action may be
** taken.
** @param action the desired action to take
** @exception SecurityException if the action is denied.
** @XXX I have no idea what actions must be tested
** or where.
**/
public void checkSecurityAccess(String action) {
checkPermission (new java.security.SecurityPermission (action));
}
/** Get the ThreadGroup that a new Thread should belong
** to by default.<P>
**
** Called by Thread.Thread().<P>
**
** SecurityManager's implementation just uses the
** ThreadGroup of the current Thread.<P>
**
** <STRONG>Spec Note:</STRONG> it is not clear whether
** the new Thread is guaranteed to pass the
** checkAccessThreadGroup() test when using this
** ThreadGroup. I presume so.
**
** @return the ThreadGroup to put the new Thread into.
**/
public ThreadGroup getThreadGroup() {
return Thread.currentThread().getThreadGroup();
}
protected SecurityManager () {
if (System.getSecurityManager () != null)
throw new SecurityException ();
}
}
class SecurityContext {
Class[] classes;
SecurityContext(Class[] classes) {
this.classes = classes;
}
}
|