1 7 8 package javax.security.auth; 9 10 import java.util.*; 11 import java.text.MessageFormat ; 12 import java.security.Permission ; 13 import java.security.PermissionCollection ; 14 import java.security.Principal ; 15 import sun.security.util.ResourcesMgr; 16 17 87 public final class PrivateCredentialPermission extends Permission { 88 89 private static final long serialVersionUID = 5284372143517237068L; 90 91 private static final CredOwner[] EMPTY_PRINCIPALS = new CredOwner[0]; 92 93 96 private String credentialClass; 97 98 103 private Set principals; private transient CredOwner[] credOwners; 105 106 109 private boolean testing = false; 110 111 115 PrivateCredentialPermission(String credentialClass, Set principals) { 116 super(credentialClass); 117 this.credentialClass = credentialClass; 118 119 synchronized(principals) { 120 if (principals.size() == 0) { 121 this.credOwners = EMPTY_PRINCIPALS; 122 } else { 123 this.credOwners = new CredOwner[principals.size()]; 124 int index = 0; 125 Iterator i = principals.iterator(); 126 while (i.hasNext()) { 127 Principal p = (Principal )i.next(); 128 this.credOwners[index++] = new CredOwner 129 (p.getClass().getName(), 130 p.getName()); 131 } 132 } 133 } 134 } 135 136 151 public PrivateCredentialPermission(String name, String actions) { 152 super(name); 153 154 if (!"read".equalsIgnoreCase(actions)) 155 throw new IllegalArgumentException 156 (ResourcesMgr.getString("actions can only be 'read'")); 157 init(name); 158 } 159 160 169 public String getCredentialClass() { 170 return credentialClass; 171 } 172 173 191 public String [][] getPrincipals() { 192 193 if (credOwners == null || credOwners.length == 0) { 194 return new String [0][0]; 195 } 196 197 String [][] pArray = new String [credOwners.length][2]; 198 for (int i = 0; i < credOwners.length; i++) { 199 pArray[i][0] = credOwners[i].principalClass; 200 pArray[i][1] = credOwners[i].principalName; 201 } 202 return pArray; 203 } 204 205 230 public boolean implies(Permission p) { 231 232 if (p == null || !(p instanceof PrivateCredentialPermission )) 233 return false; 234 235 PrivateCredentialPermission that = (PrivateCredentialPermission )p; 236 237 if (!impliesCredentialClass(credentialClass, that.credentialClass)) 238 return false; 239 240 return impliesPrincipalSet(credOwners, that.credOwners); 241 } 242 243 260 public boolean equals(Object obj) { 261 if (obj == this) 262 return true; 263 264 if (! (obj instanceof PrivateCredentialPermission )) 265 return false; 266 267 PrivateCredentialPermission that = (PrivateCredentialPermission )obj; 268 269 return (this.implies(that) && that.implies(this)); 270 } 271 272 277 public int hashCode() { 278 return this.credentialClass.hashCode(); 279 } 280 281 289 public String getActions() { 290 return "read"; 291 } 292 293 303 public PermissionCollection newPermissionCollection() { 304 return null; 305 } 306 307 private void init(String name) { 308 309 if (name == null || name.trim().length() == 0) { 310 throw new IllegalArgumentException ("invalid empty name"); 311 } 312 313 ArrayList pList = new ArrayList(); 314 StringTokenizer tokenizer = new StringTokenizer(name, " ", true); 315 String principalClass = null; 316 String principalName = null; 317 318 if (testing) 319 System.out.println("whole name = " + name); 320 321 credentialClass = tokenizer.nextToken(); 323 if (testing) 324 System.out.println("Credential Class = " + credentialClass); 325 326 if (tokenizer.hasMoreTokens() == false) { 327 MessageFormat form = new MessageFormat (ResourcesMgr.getString 328 ("permission name [name] syntax invalid: ")); 329 Object [] source = {name}; 330 throw new IllegalArgumentException 331 (form.format(source) + ResourcesMgr.getString 332 ("Credential Class not followed by a " + 333 "Principal Class and Name")); 334 } 335 336 while (tokenizer.hasMoreTokens()) { 337 338 tokenizer.nextToken(); 340 341 principalClass = tokenizer.nextToken(); 343 if (testing) 344 System.out.println(" Principal Class = " + principalClass); 345 346 if (tokenizer.hasMoreTokens() == false) { 347 MessageFormat form = new MessageFormat (ResourcesMgr.getString 348 ("permission name [name] syntax invalid: ")); 349 Object [] source = {name}; 350 throw new IllegalArgumentException 351 (form.format(source) + ResourcesMgr.getString 352 ("Principal Class not followed by a Principal Name")); 353 } 354 355 tokenizer.nextToken(); 357 358 principalName = tokenizer.nextToken(); 360 361 if (!principalName.startsWith("\"")) { 362 MessageFormat form = new MessageFormat (ResourcesMgr.getString 363 ("permission name [name] syntax invalid: ")); 364 Object [] source = {name}; 365 throw new IllegalArgumentException 366 (form.format(source) + ResourcesMgr.getString 367 ("Principal Name must be surrounded by quotes")); 368 } 369 370 if (!principalName.endsWith("\"")) { 371 372 376 while (tokenizer.hasMoreTokens()) { 377 principalName = principalName + tokenizer.nextToken(); 378 if (principalName.endsWith("\"")) 379 break; 380 } 381 382 if (!principalName.endsWith("\"")) { 383 MessageFormat form = new MessageFormat 384 (ResourcesMgr.getString 385 ("permission name [name] syntax invalid: ")); 386 Object [] source = {name}; 387 throw new IllegalArgumentException 388 (form.format(source) + ResourcesMgr.getString 389 ("Principal Name missing end quote")); 390 } 391 } 392 393 if (testing) 394 System.out.println("\tprincipalName = '" + principalName + "'"); 395 396 principalName = principalName.substring 397 (1, principalName.length() - 1); 398 399 if (principalClass.equals("*") && 400 !principalName.equals("*")) { 401 throw new IllegalArgumentException (ResourcesMgr.getString 402 ("PrivateCredentialPermission Principal Class " + 403 "can not be a wildcard (*) value if Principal Name " + 404 "is not a wildcard (*) value")); 405 } 406 407 if (testing) 408 System.out.println("\tprincipalName = '" + principalName + "'"); 409 410 pList.add(new CredOwner(principalClass, principalName)); 411 } 412 413 this.credOwners = new CredOwner[pList.size()]; 414 pList.toArray((CredOwner[])this.credOwners); 415 } 416 417 private boolean impliesCredentialClass(String thisC, String thatC) { 418 419 if (thisC == null || thatC == null) 421 return false; 422 423 if (testing) 424 System.out.println("credential class comparison: " + 425 thisC + "/" + thatC); 426 427 if (thisC.equals("*")) 428 return true; 429 430 434 440 441 return thisC.equals(thatC); 442 } 443 444 private boolean impliesPrincipalSet(CredOwner[] thisP, CredOwner[] thatP) { 445 446 if (thisP == null || thatP == null) 448 return false; 449 450 if (thatP.length == 0) 451 return true; 452 453 if (thisP.length == 0) 454 return false; 455 456 for (int i = 0; i < thisP.length; i++) { 457 boolean foundMatch = false; 458 for (int j = 0; j < thatP.length; j++) { 459 if (thisP[i].implies(thatP[j])) { 460 foundMatch = true; 461 break; 462 } 463 } 464 if (!foundMatch) { 465 return false; 466 } 467 } 468 return true; 469 } 470 471 474 private void readObject(java.io.ObjectInputStream s) throws 475 java.io.IOException , 476 ClassNotFoundException { 477 478 s.defaultReadObject(); 479 480 482 if (getName().indexOf(" ") == -1 && getName().indexOf("\"") == -1) { 483 484 credentialClass = getName(); 486 credOwners = EMPTY_PRINCIPALS; 487 488 } else { 489 490 init(getName()); 492 } 493 } 494 495 498 static class CredOwner implements java.io.Serializable { 499 500 private static final long serialVersionUID = -5607449830436408266L; 501 502 505 String principalClass; 506 509 String principalName; 510 511 CredOwner(String principalClass, String principalName) { 512 this.principalClass = principalClass; 513 this.principalName = principalName; 514 } 515 516 public boolean implies(Object obj) { 517 if (obj == null || !(obj instanceof CredOwner)) 518 return false; 519 520 CredOwner that = (CredOwner)obj; 521 522 if (principalClass.equals("*") || 523 principalClass.equals(that.principalClass)) { 524 525 if (principalName.equals("*") || 526 principalName.equals(that.principalName)) { 527 return true; 528 } 529 } 530 531 534 535 return false; 536 } 537 538 public String toString() { 539 MessageFormat form = new MessageFormat (ResourcesMgr.getString 540 ("CredOwner:\n\tPrincipal Class = class\n\t" + 541 "Principal Name = name")); 542 Object [] source = {principalClass, principalName}; 543 return (form.format(source)); 544 } 545 } 546 } 547 | Popular Tags |