1 7 8 package com.sun.jmx.remote.security; 9 10 import java.security.AccessControlContext ; 11 import java.security.AccessController ; 12 import java.security.AllPermission ; 13 import java.security.CodeSource ; 14 import java.security.Permissions ; 15 import java.security.ProtectionDomain ; 16 import javax.security.auth.Subject ; 17 import javax.security.auth.SubjectDomainCombiner ; 18 19 29 public class JMXSubjectDomainCombiner extends SubjectDomainCombiner { 30 31 public JMXSubjectDomainCombiner(Subject s) { 32 super(s); 33 } 34 35 public ProtectionDomain [] combine(ProtectionDomain [] current, 36 ProtectionDomain [] assigned) { 37 ProtectionDomain [] newCurrent; 44 if (current == null || current.length == 0) { 45 newCurrent = new ProtectionDomain [1]; 46 newCurrent[0] = pdNoPerms; 47 } else { 48 newCurrent = new ProtectionDomain [current.length + 1]; 49 for (int i = 0; i < current.length; i++) { 50 newCurrent[i] = current[i]; 51 } 52 newCurrent[current.length] = pdNoPerms; 53 } 54 return super.combine(newCurrent, assigned); 55 } 56 57 60 private static final CodeSource nullCodeSource = 61 new CodeSource (null, (java.security.cert.Certificate []) null); 62 63 66 private static final ProtectionDomain pdNoPerms = 67 new ProtectionDomain (nullCodeSource, new Permissions ()); 68 69 72 private static final Permissions allPermissions = new Permissions (); 73 static { 74 allPermissions.add(new AllPermission ()); 75 } 76 77 81 private static final ProtectionDomain pdAllPerms = 82 new ProtectionDomain (nullCodeSource, allPermissions); 83 84 87 private static final AccessControlContext systemACC = 88 new AccessControlContext (new ProtectionDomain [0]); 89 90 93 private static boolean hasOnlySystemCode(AccessControlContext acc) { 94 return systemACC.equals(acc); 95 } 96 97 103 public static AccessControlContext getContext(Subject subject) { 104 AccessControlContext currentACC = AccessController.getContext(); 105 if (hasOnlySystemCode(currentACC)) { 106 currentACC = 107 new AccessControlContext (new ProtectionDomain [] {pdAllPerms}); 108 } 109 return new AccessControlContext (currentACC, 110 new JMXSubjectDomainCombiner(subject)); 111 } 112 } 113 | Popular Tags |