1 7 8 package javax.security.auth.kerberos; 9 10 import java.util.*; 11 import java.security.Permission ; 12 import java.security.BasicPermission ; 13 import java.security.PermissionCollection ; 14 import java.io.ObjectStreamField ; 15 import java.io.ObjectOutputStream ; 16 import java.io.ObjectInputStream ; 17 import java.io.IOException ; 18 19 47 48 public final class DelegationPermission extends BasicPermission 49 implements java.io.Serializable { 50 51 private static final long serialVersionUID = 883133252142523922L; 52 53 private transient String subordinate, service; 54 55 63 public DelegationPermission(String principals) { 64 super(principals); 65 init(principals); 66 } 67 68 77 public DelegationPermission(String principals, String actions) { 78 super(principals, actions); 79 init(principals); 80 } 81 82 83 86 private void init(String target) { 87 88 StringTokenizer t = null; 89 if (!target.startsWith("\"")) { 90 throw new IllegalArgumentException 91 ("service principal [" + target + 92 "] syntax invalid: " + 93 "improperly quoted"); 94 } else { 95 t = new StringTokenizer(target, "\"", false); 96 subordinate = t.nextToken(); 97 if (t.countTokens() == 2) { 98 t.nextToken(); service = t.nextToken(); 100 } else if (t.countTokens() > 0) { 101 throw new IllegalArgumentException 102 ("service principal [" + t.nextToken() + 103 "] syntax invalid: " + 104 "improperly quoted"); 105 } 106 } 107 } 108 109 119 public boolean implies(Permission p) { 120 if (!(p instanceof DelegationPermission )) 121 return false; 122 123 DelegationPermission that = (DelegationPermission ) p; 124 if (this.subordinate.equals(that.subordinate) && 125 this.service.equals(that.service)) 126 return true; 127 128 return false; 129 } 130 131 132 141 public boolean equals(Object obj) { 142 if (obj == this) 143 return true; 144 145 if (! (obj instanceof DelegationPermission )) 146 return false; 147 148 DelegationPermission that = (DelegationPermission ) obj; 149 return implies(that); 150 } 151 152 157 158 public int hashCode() { 159 return getName().hashCode(); 160 } 161 162 163 175 176 public PermissionCollection newPermissionCollection() { 177 return new KrbDelegationPermissionCollection(); 178 } 179 180 185 private synchronized void writeObject(java.io.ObjectOutputStream s) 186 throws IOException 187 { 188 s.defaultWriteObject(); 189 } 190 191 195 private synchronized void readObject(java.io.ObjectInputStream s) 196 throws IOException , ClassNotFoundException 197 { 198 s.defaultReadObject(); 200 init(getName()); 201 } 202 203 239 } 240 241 242 final class KrbDelegationPermissionCollection extends PermissionCollection 243 implements java.io.Serializable { 244 245 private transient List perms; 247 248 public KrbDelegationPermissionCollection() { 249 perms = new ArrayList(); 250 } 251 252 253 262 263 public boolean implies(Permission permission) { 264 if (! (permission instanceof DelegationPermission )) 265 return false; 266 267 DelegationPermission np = (DelegationPermission ) permission; 268 synchronized (this) { 269 int len = perms.size(); 270 for (int i = 0; i < len; i++) { 271 DelegationPermission x = (DelegationPermission ) perms.get(i); 272 if (x.implies(np)) 273 return true; 274 } 275 } 276 return false; 277 278 } 279 280 292 293 public void add(Permission permission) { 294 if (! (permission instanceof DelegationPermission )) 295 throw new IllegalArgumentException ("invalid permission: "+ 296 permission); 297 if (isReadOnly()) 298 throw new SecurityException ("attempt to add a Permission to a readonly PermissionCollection"); 299 300 synchronized (this) { 301 perms.add(0, permission); 302 } 303 } 304 305 311 312 public Enumeration elements() { 313 synchronized (this) { 315 return Collections.enumeration(perms); 316 } 317 } 318 319 private static final long serialVersionUID = -3383936936589966948L; 320 321 328 private static final ObjectStreamField [] serialPersistentFields = { 329 new ObjectStreamField ("permissions", Vector.class), 330 }; 331 332 335 339 private void writeObject(ObjectOutputStream out) throws IOException { 340 342 Vector permissions = new Vector(perms.size()); 344 345 synchronized (this) { 346 permissions.addAll(perms); 347 } 348 349 ObjectOutputStream.PutField pfields = out.putFields(); 350 pfields.put("permissions", permissions); 351 out.writeFields(); 352 } 353 354 357 private void readObject(ObjectInputStream in) throws IOException , 358 ClassNotFoundException { 359 361 ObjectInputStream.GetField gfields = in.readFields(); 363 364 Vector permissions = (Vector)gfields.get("permissions", null); 366 perms = new ArrayList(permissions.size()); 367 perms.addAll(permissions); 368 } 369 } 370 | Popular Tags |