1 7 8 package javax.security.auth.kerberos; 9 10 import java.util.*; 11 import java.security.Permission ; 12 import java.security.PermissionCollection ; 13 import java.io.ObjectStreamField ; 14 import java.io.ObjectOutputStream ; 15 import java.io.ObjectInputStream ; 16 import java.io.IOException ; 17 18 81 82 public final class ServicePermission extends Permission 83 implements java.io.Serializable { 84 85 private static final long serialVersionUID = -1227585031618624935L; 86 87 90 private final static int INITIATE = 0x1; 91 92 95 private final static int ACCEPT = 0x2; 96 97 100 private final static int ALL = INITIATE|ACCEPT; 101 102 105 private final static int NONE = 0x0; 106 107 private transient int mask; 109 110 115 116 private String actions; 119 129 public ServicePermission(String servicePrincipal, String action) { 130 super(servicePrincipal); 131 init(servicePrincipal, getMask(action)); 132 } 133 134 135 138 private void init(String servicePrincipal, int mask) { 139 140 if (servicePrincipal == null) 141 throw new NullPointerException ("service principal can't be null"); 142 143 if ((mask & ALL) != mask) 144 throw new IllegalArgumentException ("invalid actions mask"); 145 146 this.mask = mask; 147 } 148 149 150 160 public boolean implies(Permission p) { 161 if (!(p instanceof ServicePermission )) 162 return false; 163 164 ServicePermission that = (ServicePermission ) p; 165 166 return ((this.mask & that.mask) == that.mask) && 167 impliesIgnoreMask(that); 168 } 169 170 171 boolean impliesIgnoreMask(ServicePermission p) { 172 return ((this.getName().equals("*")) || 173 this.getName().equals(p.getName())); 174 } 175 176 185 public boolean equals(Object obj) { 186 if (obj == this) 187 return true; 188 189 if (! (obj instanceof ServicePermission )) 190 return false; 191 192 ServicePermission that = (ServicePermission ) obj; 193 return ((this.mask & that.mask) == that.mask) && 194 this.getName().equals(that.getName()); 195 196 197 } 198 199 204 205 public int hashCode() { 206 return (getName().hashCode() ^ mask); 207 } 208 209 210 219 private static String getActions(int mask) 220 { 221 StringBuilder sb = new StringBuilder (); 222 boolean comma = false; 223 224 if ((mask & INITIATE) == INITIATE) { 225 if (comma) sb.append(','); 226 else comma = true; 227 sb.append("initiate"); 228 } 229 230 if ((mask & ACCEPT) == ACCEPT) { 231 if (comma) sb.append(','); 232 else comma = true; 233 sb.append("accept"); 234 } 235 236 return sb.toString(); 237 } 238 239 244 245 public String getActions() { 246 if (actions == null) 247 actions = getActions(this.mask); 248 249 return actions; 250 } 251 252 253 265 266 public PermissionCollection newPermissionCollection() { 267 return new KrbServicePermissionCollection(); 268 } 269 270 275 276 int getMask() { 277 return mask; 278 } 279 280 286 287 private static int getMask(String action) { 288 289 if (action == null) { 290 throw new NullPointerException ("action can't be null"); 291 } 292 293 if (action.equals("")) { 294 throw new IllegalArgumentException ("action can't be empty"); 295 } 296 297 int mask = NONE; 298 299 if (action == null) { 300 return mask; 301 } 302 303 char[] a = action.toCharArray(); 304 305 int i = a.length - 1; 306 if (i < 0) 307 return mask; 308 309 while (i != -1) { 310 char c; 311 312 while ((i!=-1) && ((c = a[i]) == ' ' || 314 c == '\r' || 315 c == '\n' || 316 c == '\f' || 317 c == '\t')) 318 i--; 319 320 int matchlen; 322 323 if (i >= 7 && (a[i-7] == 'i' || a[i-7] == 'I') && 324 (a[i-6] == 'n' || a[i-6] == 'N') && 325 (a[i-5] == 'i' || a[i-5] == 'I') && 326 (a[i-4] == 't' || a[i-4] == 'T') && 327 (a[i-3] == 'i' || a[i-3] == 'I') && 328 (a[i-2] == 'a' || a[i-2] == 'A') && 329 (a[i-1] == 't' || a[i-1] == 'T') && 330 (a[i] == 'e' || a[i] == 'E')) 331 { 332 matchlen = 8; 333 mask |= INITIATE; 334 335 } else if (i >= 5 && (a[i-5] == 'a' || a[i-5] == 'A') && 336 (a[i-4] == 'c' || a[i-4] == 'C') && 337 (a[i-3] == 'c' || a[i-3] == 'C') && 338 (a[i-2] == 'e' || a[i-2] == 'E') && 339 (a[i-1] == 'p' || a[i-1] == 'P') && 340 (a[i] == 't' || a[i] == 'T')) 341 { 342 matchlen = 6; 343 mask |= ACCEPT; 344 345 } else { 346 throw new IllegalArgumentException ( 348 "invalid permission: " + action); 349 } 350 351 boolean seencomma = false; 354 while (i >= matchlen && !seencomma) { 355 switch(a[i-matchlen]) { 356 case ',': 357 seencomma = true; 358 359 case ' ': case '\r': case '\n': 360 case '\f': case '\t': 361 break; 362 default: 363 throw new IllegalArgumentException ( 364 "invalid permission: " + action); 365 } 366 i--; 367 } 368 369 i -= matchlen; 371 } 372 373 return mask; 374 } 375 376 377 382 private synchronized void writeObject(java.io.ObjectOutputStream s) 383 throws IOException 384 { 385 if (actions == null) 388 getActions(); 389 s.defaultWriteObject(); 390 } 391 392 396 private synchronized void readObject(java.io.ObjectInputStream s) 397 throws IOException , ClassNotFoundException 398 { 399 s.defaultReadObject(); 401 init(getName(),getMask(actions)); 402 } 403 404 405 439 440 } 441 442 443 final class KrbServicePermissionCollection extends PermissionCollection 444 implements java.io.Serializable { 445 446 private transient List perms; 448 449 public KrbServicePermissionCollection() { 450 perms = new ArrayList(); 451 } 452 453 462 463 public boolean implies(Permission permission) { 464 if (! (permission instanceof ServicePermission )) 465 return false; 466 467 ServicePermission np = (ServicePermission ) permission; 468 int desired = np.getMask(); 469 int effective = 0; 470 int needed = desired; 471 472 synchronized (this) { 473 int len = perms.size(); 474 475 479 for (int i = 0; i < len; i++) { 480 ServicePermission x = (ServicePermission ) perms.get(i); 481 482 if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) { 484 effective |= x.getMask(); 485 if ((effective & desired) == desired) 486 return true; 487 needed = (desired ^ effective); 488 } 489 } 490 } 491 return false; 492 } 493 494 506 507 public void add(Permission permission) { 508 if (! (permission instanceof ServicePermission )) 509 throw new IllegalArgumentException ("invalid permission: "+ 510 permission); 511 if (isReadOnly()) 512 throw new SecurityException ("attempt to add a Permission to a readonly PermissionCollection"); 513 514 synchronized (this) { 515 perms.add(0, permission); 516 } 517 } 518 519 525 526 public Enumeration elements() { 527 synchronized (this) { 529 return Collections.enumeration(perms); 530 } 531 } 532 533 private static final long serialVersionUID = -4118834211490102011L; 534 535 539 543 private static final ObjectStreamField [] serialPersistentFields = { 544 new ObjectStreamField ("permissions", Vector.class), 545 }; 546 547 550 554 private void writeObject(ObjectOutputStream out) throws IOException { 555 557 Vector permissions = new Vector(perms.size()); 559 560 synchronized (this) { 561 permissions.addAll(perms); 562 } 563 564 ObjectOutputStream.PutField pfields = out.putFields(); 565 pfields.put("permissions", permissions); 566 out.writeFields(); 567 } 568 569 572 private void readObject(ObjectInputStream in) throws IOException , 573 ClassNotFoundException { 574 576 ObjectInputStream.GetField gfields = in.readFields(); 578 579 Vector permissions = (Vector)gfields.get("permissions", null); 581 perms = new ArrayList(permissions.size()); 582 perms.addAll(permissions); 583 } 584 } 585 | Popular Tags |