1 7 8 package com.sun.security.auth; 9 10 import java.io.*; 11 import java.lang.RuntimePermission ; 12 import java.lang.reflect.*; 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.*; 16 17 import java.security.AccessController ; 18 import java.security.CodeSource ; 19 import java.security.Identity ; 20 import java.security.IdentityScope ; 21 import java.security.KeyStore ; 22 import java.security.KeyStoreException ; 23 import java.security.Permission ; 24 import java.security.Permissions ; 25 import java.security.PermissionCollection ; 26 import java.security.Principal ; 27 import java.security.UnresolvedPermission ; 28 import java.security.Security ; 29 import java.security.cert.Certificate ; 30 import java.security.cert.X509Certificate ; 31 32 import javax.security.auth.Subject ; 33 import javax.security.auth.PrivateCredentialPermission ; 34 35 import sun.security.util.PropertyExpander; 36 37 226 @Deprecated 227 public class PolicyFile extends javax.security.auth.Policy { 228 229 static final java.util.ResourceBundle rb = 230 (java.util.ResourceBundle )java.security.AccessController.doPrivileged 231 (new java.security.PrivilegedAction () { 232 public Object run() { 233 return (java.util.ResourceBundle.getBundle 234 ("sun.security.util.AuthResources")); 235 } 236 }); 237 239 private static final sun.security.util.Debug debug = 240 sun.security.util.Debug.getInstance("policy", "\t[Auth Policy]"); 241 242 private static final String AUTH_POLICY = "java.security.auth.policy"; 243 private static final String SECURITY_MANAGER = "java.security.manager"; 244 private static final String AUTH_POLICY_URL = "auth.policy.url."; 245 246 private Vector policyEntries; 247 private Hashtable aliasMapping; 248 249 private boolean initialized = false; 250 251 private boolean expandProperties = true; 252 private boolean ignoreIdentityScope = false; 253 254 256 private static final Class [] PARAMS = { String .class, String .class}; 257 258 262 public PolicyFile() { 263 String prop = System.getProperty(AUTH_POLICY); 266 267 if (prop == null) { 268 prop = System.getProperty(SECURITY_MANAGER); 269 } 270 if (prop != null) 271 init(); 272 } 273 274 private synchronized void init() { 275 276 if (initialized) 277 return; 278 279 policyEntries = new Vector(); 280 aliasMapping = new Hashtable(11); 281 282 initPolicyFile(); 283 initialized = true; 284 } 285 286 294 public synchronized void refresh() 295 { 296 297 java.lang.SecurityManager sm = System.getSecurityManager(); 298 if (sm != null) { 299 sm.checkPermission(new javax.security.auth.AuthPermission 300 ("refreshPolicy")); 301 } 302 303 initialized = false; 315 java.security.AccessController.doPrivileged 316 (new java.security.PrivilegedAction () { 317 public Object run() { 318 init(); 319 return null; 320 } 321 }); 322 } 323 324 private KeyStore initKeyStore(URL policyUrl, String keyStoreName, 325 String keyStoreType) { 326 if (keyStoreName != null) { 327 try { 328 332 URL keyStoreUrl = null; 333 try { 334 keyStoreUrl = new URL (keyStoreName); 335 } catch (java.net.MalformedURLException e) { 337 keyStoreUrl = new URL (policyUrl, keyStoreName); 339 } 340 341 if (debug != null) { 342 debug.println("reading keystore"+keyStoreUrl); 343 } 344 345 InputStream inStream = 346 new BufferedInputStream(getInputStream(keyStoreUrl)); 347 348 KeyStore ks; 349 if (keyStoreType != null) 350 ks = KeyStore.getInstance(keyStoreType); 351 else 352 ks = KeyStore.getInstance(KeyStore.getDefaultType()); 353 ks.load(inStream, null); 354 inStream.close(); 355 return ks; 356 } catch (Exception e) { 357 if (debug != null) { 359 e.printStackTrace(); 360 } 361 return null; 362 } 363 } 364 return null; 365 } 366 367 private void initPolicyFile() { 368 369 String prop = Security.getProperty("policy.expandProperties"); 370 371 if (prop != null) expandProperties = prop.equalsIgnoreCase("true"); 372 373 String iscp = Security.getProperty("policy.ignoreIdentityScope"); 374 375 if (iscp != null) ignoreIdentityScope = iscp.equalsIgnoreCase("true"); 376 377 String allowSys = Security.getProperty("policy.allowSystemProperty"); 378 379 if ((allowSys!=null) && allowSys.equalsIgnoreCase("true")) { 380 381 String extra_policy = System.getProperty(AUTH_POLICY); 382 if (extra_policy != null) { 383 boolean overrideAll = false; 384 if (extra_policy.startsWith("=")) { 385 overrideAll = true; 386 extra_policy = extra_policy.substring(1); 387 } 388 try { 389 extra_policy = PropertyExpander.expand(extra_policy); 390 URL policyURL;; 391 File policyFile = new File(extra_policy); 392 if (policyFile.exists()) { 393 policyURL = 394 new URL ("file:" + policyFile.getCanonicalPath()); 395 } else { 396 policyURL = new URL (extra_policy); 397 } 398 if (debug != null) 399 debug.println("reading "+policyURL); 400 init(policyURL); 401 } catch (Exception e) { 402 if (debug != null) { 404 debug.println("caught exception: "+e); 405 } 406 407 } 408 if (overrideAll) { 409 if (debug != null) { 410 debug.println("overriding other policies!"); 411 } 412 return; 413 } 414 } 415 } 416 417 int n = 1; 418 boolean loaded_one = false; 419 String policy_url; 420 421 while ((policy_url = Security.getProperty(AUTH_POLICY_URL+n)) != null) { 422 try { 423 policy_url = PropertyExpander.expand(policy_url).replace 424 (File.separatorChar, '/'); 425 if (debug != null) 426 debug.println("reading "+policy_url); 427 init(new URL (policy_url)); 428 loaded_one = true; 429 } catch (Exception e) { 430 if (debug != null) { 431 debug.println("error reading policy "+e); 432 e.printStackTrace(); 433 } 434 } 436 n++; 437 } 438 439 if (loaded_one == false) { 440 } 442 } 443 444 445 private static IdentityScope scope = null; 446 447 452 private boolean checkForTrustedIdentity(final Certificate cert) { 453 return false; 456 } 457 458 464 private void init(URL policy) { 465 PolicyParser pp = new PolicyParser(expandProperties); 466 try { 467 InputStreamReader isr 468 = new InputStreamReader(getInputStream(policy)); 469 pp.read(isr); 470 isr.close(); 471 KeyStore keyStore = initKeyStore(policy, pp.getKeyStoreUrl(), 472 pp.getKeyStoreType()); 473 Enumeration enum_ = pp.grantElements(); 474 while (enum_.hasMoreElements()) { 475 PolicyParser.GrantEntry ge = 476 (PolicyParser.GrantEntry) enum_.nextElement(); 477 addGrantEntry(ge, keyStore); 478 } 479 } catch (PolicyParser.ParsingException pe) { 480 System.err.println(AUTH_POLICY + 481 rb.getString(": error parsing ") + policy); 482 System.err.println(AUTH_POLICY + 483 rb.getString(": ") + 484 pe.getMessage()); 485 if (debug != null) 486 pe.printStackTrace(); 487 488 } catch (Exception e) { 489 if (debug != null) { 490 debug.println("error parsing "+policy); 491 debug.println(e.toString()); 492 e.printStackTrace(); 493 } 494 } 495 } 496 497 504 private InputStream getInputStream(URL url) throws IOException { 505 if ("file".equals(url.getProtocol())) { 506 String path = url.getFile().replace('/', File.separatorChar); 507 return new FileInputStream(path); 508 } else { 509 return url.openStream(); 510 } 511 } 512 513 518 CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore) 519 throws java.net.MalformedURLException 520 { 521 Certificate [] certs = null; 522 if (ge.signedBy != null) { 523 certs = getCertificates(keyStore, ge.signedBy); 524 if (certs == null) { 525 if (debug != null) { 528 debug.println(" no certs for alias " + 529 ge.signedBy + ", ignoring."); 530 } 531 return null; 532 } 533 } 534 535 URL location; 536 537 if (ge.codeBase != null) 538 location = new URL (ge.codeBase); 539 else 540 location = null; 541 542 if (ge.principals == null || ge.principals.size() == 0) { 543 return (canonicalizeCodebase 544 (new CodeSource (location, certs), 545 false)); 546 } else { 547 return (canonicalizeCodebase 548 (new SubjectCodeSource(null, ge.principals, location, certs), 549 false)); 550 } 551 } 552 553 556 private void addGrantEntry(PolicyParser.GrantEntry ge, 557 KeyStore keyStore) { 558 559 if (debug != null) { 560 debug.println("Adding policy entry: "); 561 debug.println(" signedBy " + ge.signedBy); 562 debug.println(" codeBase " + ge.codeBase); 563 if (ge.principals != null && ge.principals.size() > 0) { 564 ListIterator li = ge.principals.listIterator(); 565 while (li.hasNext()) { 566 PolicyParser.PrincipalEntry pppe = 567 (PolicyParser.PrincipalEntry)li.next(); 568 debug.println(" " + pppe.principalClass + 569 " " + pppe.principalName); 570 } 571 } 572 debug.println(); 573 } 574 575 try { 576 CodeSource codesource = getCodeSource(ge, keyStore); 577 if (codesource == null) return; 579 580 PolicyEntry entry = new PolicyEntry(codesource); 581 Enumeration enum_ = ge.permissionElements(); 582 while (enum_.hasMoreElements()) { 583 PolicyParser.PermissionEntry pe = 584 (PolicyParser.PermissionEntry) enum_.nextElement(); 585 try { 586 Permission perm; 588 if (pe.permission.equals 589 ("javax.security.auth.PrivateCredentialPermission") && 590 pe.name.endsWith(" self")) { 591 perm = getInstance(pe.permission, 592 pe.name + " \"self\"", 593 pe.action); 594 } else { 595 perm = getInstance(pe.permission, 596 pe.name, 597 pe.action); 598 } 599 entry.add(perm); 600 if (debug != null) { 601 debug.println(" "+perm); 602 } 603 } catch (ClassNotFoundException cnfe) { 604 Certificate certs[]; 605 if (pe.signedBy != null) 606 certs = getCertificates(keyStore, pe.signedBy); 607 else 608 certs = null; 609 610 if (certs != null || pe.signedBy == null) { 613 Permission perm = new UnresolvedPermission ( 614 pe.permission, 615 pe.name, 616 pe.action, 617 certs); 618 entry.add(perm); 619 if (debug != null) { 620 debug.println(" "+perm); 621 } 622 } 623 } catch (java.lang.reflect.InvocationTargetException ite) { 624 System.err.println 625 (AUTH_POLICY + 626 rb.getString(": error adding Permission ") + 627 pe.permission + 628 rb.getString(" ") + 629 ite.getTargetException()); 630 } catch (Exception e) { 631 System.err.println 632 (AUTH_POLICY + 633 rb.getString(": error adding Permission ") + 634 pe.permission + 635 rb.getString(" ") + 636 e); 637 } 638 } 639 policyEntries.addElement(entry); 640 } catch (Exception e) { 641 System.err.println 642 (AUTH_POLICY + 643 rb.getString(": error adding Entry ") + 644 ge + 645 rb.getString(" ") + 646 e); 647 } 648 649 if (debug != null) 650 debug.println(); 651 } 652 653 682 683 private static final Permission getInstance(String type, 684 String name, 685 String actions) 686 throws ClassNotFoundException , 687 InstantiationException , 688 IllegalAccessException , 689 NoSuchMethodException , 690 InvocationTargetException 691 { 692 Class pc = Class.forName(type); 694 Constructor c = pc.getConstructor(PARAMS); 695 return (Permission ) c.newInstance(new Object [] { name, actions }); 696 } 697 698 701 Certificate [] getCertificates( 702 KeyStore keyStore, String aliases) { 703 704 Vector vcerts = null; 705 706 StringTokenizer st = new StringTokenizer(aliases, ","); 707 int n = 0; 708 709 while (st.hasMoreTokens()) { 710 String alias = st.nextToken().trim(); 711 n++; 712 Certificate cert = null; 713 cert = (Certificate ) aliasMapping.get(alias); 715 if (cert == null && keyStore != null) { 716 717 try { 718 cert = keyStore.getCertificate(alias); 719 } catch (KeyStoreException kse) { 720 } 723 if (cert != null) { 724 aliasMapping.put(alias, cert); 725 aliasMapping.put(cert, alias); 726 } 727 } 728 729 if (cert != null) { 730 if (vcerts == null) 731 vcerts = new Vector(); 732 vcerts.addElement(cert); 733 } 734 } 735 736 if (vcerts != null && n == vcerts.size()) { 738 Certificate [] certs = new Certificate [vcerts.size()]; 739 vcerts.copyInto(certs); 740 return certs; 741 } else { 742 return null; 743 } 744 } 745 746 752 private final synchronized Enumeration elements(){ 753 return policyEntries.elements(); 754 } 755 756 811 public PermissionCollection getPermissions(final Subject subject, 812 final CodeSource codesource) { 813 814 821 return (PermissionCollection )java.security.AccessController.doPrivileged 832 (new java.security.PrivilegedAction () { 833 public Object run() { 834 SubjectCodeSource scs = new SubjectCodeSource 835 (subject, 836 null, 837 codesource == null ? null : codesource.getLocation(), 838 codesource == null ? null : codesource.getCertificates()); 839 if (initialized) 840 return getPermissions(new Permissions (), scs); 841 else 842 return new PolicyPermissions(PolicyFile.this, scs); 843 } 844 }); 845 } 846 847 858 PermissionCollection getPermissions(CodeSource codesource) { 859 860 if (initialized) 861 return getPermissions(new Permissions (), codesource); 862 else 863 return new PolicyPermissions(this, codesource); 864 } 865 866 878 Permissions getPermissions(final Permissions perms, 879 final CodeSource cs) 880 { 881 if (!initialized) { 882 init(); 883 } 884 885 final CodeSource codesource[] = {null}; 886 887 codesource[0] = canonicalizeCodebase(cs, true); 888 889 if (debug != null) { 890 debug.println("evaluate("+codesource[0]+")\n"); 891 } 892 893 897 for (int i = 0; i < policyEntries.size(); i++) { 898 899 PolicyEntry entry = (PolicyEntry)policyEntries.elementAt(i); 900 901 if (debug != null) { 902 debug.println("PolicyFile CodeSource implies: " + 903 entry.codesource.toString() + "\n\n" + 904 "\t" + codesource[0].toString() + "\n\n"); 905 } 906 907 if (entry.codesource.implies(codesource[0])) { 908 for (int j = 0; j < entry.permissions.size(); j++) { 909 Permission p = 910 (Permission ) entry.permissions.elementAt(j); 911 if (debug != null) { 912 debug.println(" granting " + p); 913 } 914 if (!addSelfPermissions(p, entry.codesource, 915 codesource[0], perms)) { 916 perms.add(p); 921 } 922 } 923 } 924 } 925 926 928 if (!ignoreIdentityScope) { 929 Certificate certs[] = codesource[0].getCertificates(); 930 if (certs != null) { 931 for (int k=0; k < certs.length; k++) { 932 if ((aliasMapping.get(certs[k]) == null) && 933 checkForTrustedIdentity(certs[k])) { 934 perms.add(new java.security.AllPermission ()); 939 } 940 } 941 } 942 } 943 return perms; 944 } 945 946 962 private boolean addSelfPermissions(final Permission p, 963 CodeSource entryCs, 964 CodeSource accCs, 965 Permissions perms) { 966 967 if (!(p instanceof PrivateCredentialPermission )) 968 return false; 969 970 if (!(entryCs instanceof SubjectCodeSource)) 971 return false; 972 973 974 PrivateCredentialPermission pcp = (PrivateCredentialPermission )p; 975 SubjectCodeSource scs = (SubjectCodeSource)entryCs; 976 977 String [][] pPrincipals = pcp.getPrincipals(); 979 if (pPrincipals.length <= 0 || 980 !pPrincipals[0][0].equalsIgnoreCase("self") || 981 !pPrincipals[0][1].equalsIgnoreCase("self")) { 982 983 return false; 985 } else { 986 987 991 if (scs.getPrincipals() == null) { 992 return true; 994 } 995 996 ListIterator pli = scs.getPrincipals().listIterator(); 997 while (pli.hasNext()) { 998 999 PolicyParser.PrincipalEntry principal = 1000 (PolicyParser.PrincipalEntry)pli.next(); 1001 1002 1013 String [][] principalInfo = getPrincipalInfo 1014 (principal, accCs); 1015 1016 for (int i = 0; i < principalInfo.length; i++) { 1017 1018 1020 PrivateCredentialPermission newPcp = 1021 new PrivateCredentialPermission 1022 (pcp.getCredentialClass() + 1023 " " + 1024 principalInfo[i][0] + 1025 " " + 1026 "\"" + principalInfo[i][1] + "\"", 1027 "read"); 1028 1029 if (debug != null) { 1030 debug.println("adding SELF permission: " + 1031 newPcp.toString()); 1032 } 1033 1034 perms.add(newPcp); 1035 } 1036 } 1037 } 1038 return true; 1039 } 1040 1041 1047 private String [][] getPrincipalInfo 1048 (PolicyParser.PrincipalEntry principal, 1049 final CodeSource accCs) { 1050 1051 1056 if (!principal.principalClass.equals 1057 (PolicyParser.PrincipalEntry.WILDCARD_CLASS) && 1058 !principal.principalName.equals 1059 (PolicyParser.PrincipalEntry.WILDCARD_NAME)) { 1060 1061 String [][] info = new String [1][2]; 1064 info[0][0] = principal.principalClass; 1065 info[0][1] = principal.principalName; 1066 return info; 1067 1068 } else if (!principal.principalClass.equals 1069 (PolicyParser.PrincipalEntry.WILDCARD_CLASS) && 1070 principal.principalName.equals 1071 (PolicyParser.PrincipalEntry.WILDCARD_NAME)) { 1072 1073 1076 SubjectCodeSource scs = (SubjectCodeSource)accCs; 1079 1080 Set principalSet = null; 1081 try { 1082 Class pClass = Class.forName(principal.principalClass, false, 1083 ClassLoader.getSystemClassLoader()); 1084 principalSet = scs.getSubject().getPrincipals(pClass); 1085 } catch (Exception e) { 1086 if (debug != null) { 1087 debug.println("problem finding Principal Class " + 1088 "when expanding SELF permission: " + 1089 e.toString()); 1090 } 1091 } 1092 1093 if (principalSet == null) { 1094 return new String [0][0]; 1096 } 1097 1098 String [][] info = new String [principalSet.size()][2]; 1099 java.util.Iterator pIterator = principalSet.iterator(); 1100 1101 int i = 0; 1102 while (pIterator.hasNext()) { 1103 Principal p = (Principal )pIterator.next(); 1104 info[i][0] = p.getClass().getName(); 1105 info[i][1] = p.getName(); 1106 i++; 1107 } 1108 return info; 1109 1110 } else { 1111 1112 1115 SubjectCodeSource scs = (SubjectCodeSource)accCs; 1118 Set principalSet = scs.getSubject().getPrincipals(); 1119 1120 String [][] info = new String [principalSet.size()][2]; 1121 java.util.Iterator pIterator = principalSet.iterator(); 1122 1123 int i = 0; 1124 while (pIterator.hasNext()) { 1125 Principal p = (Principal )pIterator.next(); 1126 info[i][0] = p.getClass().getName(); 1127 info[i][1] = p.getName(); 1128 i++; 1129 } 1130 return info; 1131 } 1132 } 1133 1134 1147 Certificate [] getSignerCertificates(CodeSource cs) { 1148 Certificate [] certs = null; 1149 if ((certs = cs.getCertificates()) == null) 1150 return null; 1151 for (int i=0; i<certs.length; i++) { 1152 if (!(certs[i] instanceof X509Certificate )) 1153 return cs.getCertificates(); 1154 } 1155 1156 int i = 0; 1158 int count = 0; 1159 while (i < certs.length) { 1160 count++; 1161 while (((i+1) < certs.length) 1162 && ((X509Certificate )certs[i]).getIssuerDN().equals( 1163 ((X509Certificate )certs[i+1]).getSubjectDN())) { 1164 i++; 1165 } 1166 i++; 1167 } 1168 if (count == certs.length) 1169 return certs; 1171 1172 ArrayList userCertList = new ArrayList(); 1173 i = 0; 1174 while (i < certs.length) { 1175 userCertList.add(certs[i]); 1176 while (((i+1) < certs.length) 1177 && ((X509Certificate )certs[i]).getIssuerDN().equals( 1178 ((X509Certificate )certs[i+1]).getSubjectDN())) { 1179 i++; 1180 } 1181 i++; 1182 } 1183 Certificate [] userCerts = new Certificate [userCertList.size()]; 1184 userCertList.toArray(userCerts); 1185 return userCerts; 1186 } 1187 1188 private CodeSource canonicalizeCodebase(CodeSource cs, 1189 boolean extractSignerCerts) { 1190 CodeSource canonCs = cs; 1191 if (cs.getLocation() != null && 1192 cs.getLocation().getProtocol().equalsIgnoreCase("file")) { 1193 try { 1194 String path = cs.getLocation().getFile().replace 1195 ('/', 1196 File.separatorChar); 1197 URL csUrl = null; 1198 if (path.endsWith("*")) { 1199 path = path.substring(0, path.length()-1); 1202 boolean appendFileSep = false; 1203 if (path.endsWith(File.separator)) 1204 appendFileSep = true; 1205 if (path.equals("")) { 1206 path = System.getProperty("user.dir"); 1207 } 1208 File f = new File(path); 1209 path = f.getCanonicalPath(); 1210 StringBuffer sb = new StringBuffer (path); 1211 if (!path.endsWith(File.separator) && 1215 (appendFileSep || f.isDirectory())) 1216 sb.append(File.separatorChar); 1217 sb.append('*'); 1218 path = sb.toString(); 1219 } else { 1220 path = new File(path).getCanonicalPath(); 1221 } 1222 csUrl = new File(path).toURL(); 1223 1224 if (cs instanceof SubjectCodeSource) { 1225 SubjectCodeSource scs = (SubjectCodeSource)cs; 1226 if (extractSignerCerts) { 1227 canonCs = new SubjectCodeSource 1228 (scs.getSubject(), 1229 scs.getPrincipals(), 1230 csUrl, 1231 getSignerCertificates(scs)); 1232 } else { 1233 canonCs = new SubjectCodeSource 1234 (scs.getSubject(), 1235 scs.getPrincipals(), 1236 csUrl, 1237 scs.getCertificates()); 1238 } 1239 } else { 1240 if (extractSignerCerts) { 1241 canonCs = new CodeSource (csUrl, 1242 getSignerCertificates(cs)); 1243 } else { 1244 canonCs = new CodeSource (csUrl, 1245 cs.getCertificates()); 1246 } 1247 } 1248 } catch (IOException ioe) { 1249 if (extractSignerCerts) { 1252 if (!(cs instanceof SubjectCodeSource)) { 1253 canonCs = new CodeSource (cs.getLocation(), 1254 getSignerCertificates(cs)); 1255 } else { 1256 SubjectCodeSource scs = (SubjectCodeSource)cs; 1257 canonCs = new SubjectCodeSource(scs.getSubject(), 1258 scs.getPrincipals(), 1259 scs.getLocation(), 1260 getSignerCertificates(scs)); 1261 } 1262 } 1263 } 1264 } else { 1265 if (extractSignerCerts) { 1266 if (!(cs instanceof SubjectCodeSource)) { 1267 canonCs = new CodeSource (cs.getLocation(), 1268 getSignerCertificates(cs)); 1269 } else { 1270 SubjectCodeSource scs = (SubjectCodeSource)cs; 1271 canonCs = new SubjectCodeSource(scs.getSubject(), 1272 scs.getPrincipals(), 1273 scs.getLocation(), 1274 getSignerCertificates(scs)); 1275 } 1276 } 1277 } 1278 return canonCs; 1279 } 1280 1281 1326 1327 private static class PolicyEntry { 1328 1329 CodeSource codesource; 1330 Vector permissions; 1331 1332 1344 PolicyEntry(CodeSource cs) 1345 { 1346 this.codesource = cs; 1347 this.permissions = new Vector(); 1348 } 1349 1350 1353 void add(Permission p) { 1354 permissions.addElement(p); 1355 } 1356 1357 1360 CodeSource getCodeSource() { 1361 return this.codesource; 1362 } 1363 1364 public String toString(){ 1365 StringBuffer sb = new StringBuffer (); 1366 sb.append(rb.getString("(")); 1367 sb.append(getCodeSource()); 1368 sb.append("\n"); 1369 for (int j = 0; j < permissions.size(); j++) { 1370 Permission p = (Permission ) permissions.elementAt(j); 1371 sb.append(rb.getString(" ")); 1372 sb.append(rb.getString(" ")); 1373 sb.append(p); 1374 sb.append(rb.getString("\n")); 1375 } 1376 sb.append(rb.getString(")")); 1377 sb.append(rb.getString("\n")); 1378 return sb.toString(); 1379 } 1380 1381 } 1382} 1383 1384class PolicyPermissions extends PermissionCollection { 1385 1386 private static final long serialVersionUID = -1954188373270545523L; 1387 1388 private CodeSource codesource; 1389 private Permissions perms; 1390 private PolicyFile policy; 1391 private boolean notInit; private Vector additionalPerms; 1393 1394 PolicyPermissions(PolicyFile policy, 1395 CodeSource codesource) 1396 { 1397 this.codesource = codesource; 1398 this.policy = policy; 1399 this.perms = null; 1400 this.notInit = true; 1401 this.additionalPerms = null; 1402 } 1403 1404 public void add(Permission permission) { 1405 if (isReadOnly()) 1406 throw new SecurityException 1407 (PolicyFile.rb.getString 1408 ("attempt to add a Permission to a readonly PermissionCollection")); 1409 1410 if (perms == null) { 1411 if (additionalPerms == null) 1412 additionalPerms = new Vector(); 1413 additionalPerms.add(permission); 1414 } else { 1415 perms.add(permission); 1416 } 1417 } 1418 1419 private synchronized void init() { 1420 if (notInit) { 1421 if (perms == null) 1422 perms = new Permissions (); 1423 1424 if (additionalPerms != null) { 1425 Enumeration e = additionalPerms.elements(); 1426 while (e.hasMoreElements()) { 1427 perms.add((Permission )e.nextElement()); 1428 } 1429 additionalPerms = null; 1430 } 1431 policy.getPermissions(perms,codesource); 1432 notInit=false; 1433 } 1434 } 1435 1436 public boolean implies(Permission permission) { 1437 if (notInit) 1438 init(); 1439 return perms.implies(permission); 1440 } 1441 1442 public Enumeration elements() { 1443 if (notInit) 1444 init(); 1445 return perms.elements(); 1446 } 1447 1448 public String toString() { 1449 if (notInit) 1450 init(); 1451 return perms.toString(); 1452 } 1453} 1454 | Popular Tags |