1 7 8 package java.lang; 9 10 import java.security.*; 11 import java.io.FileDescriptor ; 12 import java.io.File ; 13 import java.io.FilePermission ; 14 import java.awt.AWTPermission ; 15 import java.util.PropertyPermission ; 16 import java.lang.RuntimePermission ; 17 import java.net.SocketPermission ; 18 import java.net.NetPermission ; 19 import java.util.Hashtable ; 20 import java.net.InetAddress ; 21 import java.lang.reflect.Member ; 22 import java.lang.reflect.*; 23 import java.net.URL ; 24 25 import sun.security.util.SecurityConstants; 26 27 209 public 210 class SecurityManager { 211 212 220 @Deprecated 221 protected boolean inCheck; 222 223 226 private boolean initialized = false; 227 228 229 232 private boolean hasAllPermission() 233 { 234 try { 235 checkPermission(SecurityConstants.ALL_PERMISSION); 236 return true; 237 } catch (SecurityException se) { 238 return false; 239 } 240 } 241 242 254 @Deprecated 255 public boolean getInCheck() { 256 return inCheck; 257 } 258 259 276 public SecurityManager() { 277 synchronized(SecurityManager .class) { 278 SecurityManager sm = System.getSecurityManager(); 279 if (sm != null) { 280 sm.checkPermission(new RuntimePermission 283 ("createSecurityManager")); 284 } 285 initialized = true; 286 } 287 } 288 289 299 protected native Class [] getClassContext(); 300 301 337 @Deprecated 338 protected ClassLoader currentClassLoader() 339 { 340 ClassLoader cl = currentClassLoader0(); 341 if ((cl != null) && hasAllPermission()) 342 cl = null; 343 return cl; 344 } 345 346 private native ClassLoader currentClassLoader0(); 347 348 384 @Deprecated 385 protected Class <?> currentLoadedClass() { 386 Class c = currentLoadedClass0(); 387 if ((c != null) && hasAllPermission()) 388 c = null; 389 return c; 390 } 391 392 404 @Deprecated 405 protected native int classDepth(String name); 406 407 442 @Deprecated 443 protected int classLoaderDepth() 444 { 445 int depth = classLoaderDepth0(); 446 if (depth != -1) { 447 if (hasAllPermission()) 448 depth = -1; 449 else 450 depth--; } 452 return depth; 453 } 454 455 private native int classLoaderDepth0(); 456 457 468 @Deprecated 469 protected boolean inClass(String name) { 470 return classDepth(name) >= 0; 471 } 472 473 485 @Deprecated 486 protected boolean inClassLoader() { 487 return currentClassLoader() != null; 488 } 489 490 512 public Object getSecurityContext() { 513 return AccessController.getContext(); 514 } 515 516 531 public void checkPermission(Permission perm) { 532 java.security.AccessController.checkPermission(perm); 533 } 534 535 566 public void checkPermission(Permission perm, Object context) { 567 if (context instanceof AccessControlContext) { 568 ((AccessControlContext)context).checkPermission(perm); 569 } else { 570 throw new SecurityException (); 571 } 572 } 573 574 593 public void checkCreateClassLoader() { 594 checkPermission(SecurityConstants.CREATE_CLASSLOADER_PERMISSION); 595 } 596 597 601 602 private static ThreadGroup rootGroup = getRootGroup(); 603 604 private static ThreadGroup getRootGroup() { 605 ThreadGroup root = Thread.currentThread().getThreadGroup(); 606 while (root.getParent() != null) { 607 root = root.getParent(); 608 } 609 return root; 610 } 611 612 654 public void checkAccess(Thread t) { 655 if (t == null) { 656 throw new NullPointerException ("thread can't be null"); 657 } 658 if (t.getThreadGroup() == rootGroup) { 659 checkPermission(SecurityConstants.MODIFY_THREAD_PERMISSION); 660 } else { 661 } 663 } 664 707 public void checkAccess(ThreadGroup g) { 708 if (g == null) { 709 throw new NullPointerException ("thread group can't be null"); 710 } 711 if (g == rootGroup) { 712 checkPermission(SecurityConstants.MODIFY_THREADGROUP_PERMISSION); 713 } else { 714 } 716 } 717 718 743 public void checkExit(int status) { 744 checkPermission(new RuntimePermission ("exitVM")); 745 } 746 747 776 public void checkExec(String cmd) { 777 File f = new File (cmd); 778 if (f.isAbsolute()) { 779 checkPermission(new FilePermission (cmd, 780 SecurityConstants.FILE_EXECUTE_ACTION)); 781 } else { 782 checkPermission(new FilePermission ("<<ALL FILES>>", 783 SecurityConstants.FILE_EXECUTE_ACTION)); 784 } 785 } 786 787 814 public void checkLink(String lib) { 815 if (lib == null) { 816 throw new NullPointerException ("library can't be null"); 817 } 818 checkPermission(new RuntimePermission ("loadLibrary."+lib)); 819 } 820 821 843 public void checkRead(FileDescriptor fd) { 844 if (fd == null) { 845 throw new NullPointerException ("file descriptor can't be null"); 846 } 847 checkPermission(new RuntimePermission ("readFileDescriptor")); 848 } 849 850 870 public void checkRead(String file) { 871 checkPermission(new FilePermission (file, 872 SecurityConstants.FILE_READ_ACTION)); 873 } 874 875 905 public void checkRead(String file, Object context) { 906 checkPermission( 907 new FilePermission (file, SecurityConstants.FILE_READ_ACTION), 908 context); 909 } 910 911 933 public void checkWrite(FileDescriptor fd) { 934 if (fd == null) { 935 throw new NullPointerException ("file descriptor can't be null"); 936 } 937 checkPermission(new RuntimePermission ("writeFileDescriptor")); 938 939 } 940 941 961 public void checkWrite(String file) { 962 checkPermission(new FilePermission (file, 963 SecurityConstants.FILE_WRITE_ACTION)); 964 } 965 966 989 public void checkDelete(String file) { 990 checkPermission(new FilePermission (file, 991 SecurityConstants.FILE_DELETE_ACTION)); 992 } 993 994 1023 public void checkConnect(String host, int port) { 1024 if (host == null) { 1025 throw new NullPointerException ("host can't be null"); 1026 } 1027 if (!host.startsWith("[") && host.indexOf(':') != -1) { 1028 host = "[" + host + "]"; 1029 } 1030 if (port == -1) { 1031 checkPermission(new SocketPermission (host, 1032 SecurityConstants.SOCKET_RESOLVE_ACTION)); 1033 } else { 1034 checkPermission(new SocketPermission (host+":"+port, 1035 SecurityConstants.SOCKET_CONNECT_ACTION)); 1036 } 1037 } 1038 1039 1078 public void checkConnect(String host, int port, Object context) { 1079 if (host == null) { 1080 throw new NullPointerException ("host can't be null"); 1081 } 1082 if (!host.startsWith("[") && host.indexOf(':') != -1) { 1083 host = "[" + host + "]"; 1084 } 1085 if (port == -1) 1086 checkPermission(new SocketPermission (host, 1087 SecurityConstants.SOCKET_RESOLVE_ACTION), 1088 context); 1089 else 1090 checkPermission(new SocketPermission (host+":"+port, 1091 SecurityConstants.SOCKET_CONNECT_ACTION), 1092 context); 1093 } 1094 1095 1116 public void checkListen(int port) { 1117 if (port == 0) { 1118 checkPermission(SecurityConstants.LOCAL_LISTEN_PERMISSION); 1119 } else { 1120 checkPermission(new SocketPermission ("localhost:"+port, 1121 SecurityConstants.SOCKET_LISTEN_ACTION)); 1122 } 1123 } 1124 1125 1150 public void checkAccept(String host, int port) { 1151 if (host == null) { 1152 throw new NullPointerException ("host can't be null"); 1153 } 1154 if (!host.startsWith("[") && host.indexOf(':') != -1) { 1155 host = "[" + host + "]"; 1156 } 1157 checkPermission(new SocketPermission (host+":"+port, 1158 SecurityConstants.SOCKET_ACCEPT_ACTION)); 1159 } 1160 1161 1183 public void checkMulticast(InetAddress maddr) { 1184 String host = maddr.getHostAddress(); 1185 if (!host.startsWith("[") && host.indexOf(':') != -1) { 1186 host = "[" + host + "]"; 1187 } 1188 checkPermission(new SocketPermission (host, 1189 SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION)); 1190 } 1191 1192 1218 @Deprecated 1219 public void checkMulticast(InetAddress maddr, byte ttl) { 1220 String host = maddr.getHostAddress(); 1221 if (!host.startsWith("[") && host.indexOf(':') != -1) { 1222 host = "[" + host + "]"; 1223 } 1224 checkPermission(new SocketPermission (host, 1225 SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION)); 1226 } 1227 1228 1251 public void checkPropertiesAccess() { 1252 checkPermission(new PropertyPermission ("*", 1253 SecurityConstants.PROPERTY_RW_ACTION)); 1254 } 1255 1256 1284 public void checkPropertyAccess(String key) { 1285 checkPermission(new PropertyPermission (key, 1286 SecurityConstants.PROPERTY_READ_ACTION)); 1287 } 1288 1289 1322 public boolean checkTopLevelWindow(Object window) { 1323 if (window == null) { 1324 throw new NullPointerException ("window can't be null"); 1325 } 1326 try { 1327 checkPermission(SecurityConstants.TOPLEVEL_WINDOW_PERMISSION); 1328 return true; 1329 } catch (SecurityException se) { 1330 } 1332 return false; 1333 } 1334 1335 1354 public void checkPrintJobAccess() { 1355 checkPermission(new RuntimePermission ("queuePrintJob")); 1356 } 1357 1358 1376 public void checkSystemClipboardAccess() { 1377 checkPermission(SecurityConstants.ACCESS_CLIPBOARD_PERMISSION); 1378 } 1379 1380 1397 public void checkAwtEventQueueAccess() { 1398 checkPermission(SecurityConstants.CHECK_AWT_EVENTQUEUE_PERMISSION); 1399 } 1400 1401 1417 private static boolean packageAccessValid = false; 1418 private static String [] packageAccess; 1419 private static final Object packageAccessLock = new Object (); 1420 1421 private static boolean packageDefinitionValid = false; 1422 private static String [] packageDefinition; 1423 private static final Object packageDefinitionLock = new Object (); 1424 1425 private static String [] getPackages(String p) { 1426 String packages[] = null; 1427 if (p != null && !p.equals("")) { 1428 java.util.StringTokenizer tok = 1429 new java.util.StringTokenizer (p, ","); 1430 int n = tok.countTokens(); 1431 if (n > 0) { 1432 packages = new String [n]; 1433 int i = 0; 1434 while (tok.hasMoreElements()) { 1435 String s = tok.nextToken().trim(); 1436 packages[i++] = s; 1437 } 1438 } 1439 } 1440 1441 if (packages == null) 1442 packages = new String [0]; 1443 return packages; 1444 } 1445 1446 1478 public void checkPackageAccess(String pkg) { 1479 if (pkg == null) { 1480 throw new NullPointerException ("package name can't be null"); 1481 } 1482 1483 String [] pkgs; 1484 synchronized (packageAccessLock) { 1485 1488 if (!packageAccessValid) { 1489 String tmpPropertyStr = 1490 (String ) AccessController.doPrivileged( 1491 new PrivilegedAction() { 1492 public Object run() { 1493 return java.security.Security.getProperty( 1494 "package.access"); 1495 } 1496 } 1497 ); 1498 packageAccess = getPackages(tmpPropertyStr); 1499 packageAccessValid = true; 1500 } 1501 1502 pkgs = packageAccess; 1505 } 1506 1507 1510 for (int i = 0; i < pkgs.length; i++) { 1511 if (pkg.startsWith(pkgs[i]) || pkgs[i].equals(pkg + ".")) { 1512 checkPermission( 1513 new RuntimePermission ("accessClassInPackage."+pkg)); 1514 break; } 1516 } 1517 } 1518 1519 1547 public void checkPackageDefinition(String pkg) { 1548 if (pkg == null) { 1549 throw new NullPointerException ("package name can't be null"); 1550 } 1551 1552 String [] pkgs; 1553 synchronized (packageDefinitionLock) { 1554 1557 if (!packageDefinitionValid) { 1558 String tmpPropertyStr = 1559 (String ) AccessController.doPrivileged( 1560 new PrivilegedAction() { 1561 public Object run() { 1562 return java.security.Security.getProperty( 1563 "package.definition"); 1564 } 1565 } 1566 ); 1567 packageDefinition = getPackages(tmpPropertyStr); 1568 packageDefinitionValid = true; 1569 } 1570 pkgs = packageDefinition; 1573 } 1574 1575 1578 for (int i = 0; i < pkgs.length; i++) { 1579 if (pkg.startsWith(pkgs[i]) || pkgs[i].equals(pkg + ".")) { 1580 checkPermission( 1581 new RuntimePermission ("defineClassInPackage."+pkg)); 1582 break; } 1584 } 1585 } 1586 1587 1611 public void checkSetFactory() { 1612 checkPermission(new RuntimePermission ("setFactory")); 1613 } 1614 1615 1643 public void checkMemberAccess(Class <?> clazz, int which) { 1644 if (clazz == null) { 1645 throw new NullPointerException ("class can't be null"); 1646 } 1647 if (which != Member.PUBLIC) { 1648 Class stack[] = getClassContext(); 1649 1660 if ((stack.length<4) || 1661 (stack[3].getClassLoader() != clazz.getClassLoader())) { 1662 checkPermission(SecurityConstants.CHECK_MEMBER_ACCESS_PERMISSION); 1663 } 1664 } 1665 } 1666 1667 1697 public void checkSecurityAccess(String target) { 1698 checkPermission(new SecurityPermission(target)); 1699 } 1700 1701 private native Class currentLoadedClass0(); 1702 1703 1714 public ThreadGroup getThreadGroup() { 1715 return Thread.currentThread().getThreadGroup(); 1716 } 1717 1718} 1719 | Popular Tags |