1 18 package org.osgi.service.useradmin; 19 20 import java.io.IOException ; 21 import java.security.*; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 25 119 public final class UserAdminPermission extends BasicPermission { 120 static final long serialVersionUID = -1179971692401603789L; 121 124 public static final String ADMIN = "admin"; 125 128 public static final String CHANGE_PROPERTY = "changeProperty"; 129 private static final int ACTION_CHANGE_PROPERTY = 0x1; 130 133 public static final String CHANGE_CREDENTIAL = "changeCredential"; 134 private static final int ACTION_CHANGE_CREDENTIAL = 0x2; 135 138 public static final String GET_CREDENTIAL = "getCredential"; 139 private static final int ACTION_GET_CREDENTIAL = 0x4; 140 143 private static final int ACTION_ALL = ACTION_CHANGE_PROPERTY 144 | ACTION_CHANGE_CREDENTIAL 145 | ACTION_GET_CREDENTIAL; 146 149 static final int ACTION_NONE = 0x0; 150 155 private String actions = null; 156 159 private transient int action_mask = ACTION_NONE; 160 164 private transient String description; 165 166 180 public UserAdminPermission(String name, String actions) { 181 this(name, getMask(actions)); 182 } 183 184 191 UserAdminPermission(String name, int mask) { 192 super(name); 193 init(mask); 194 } 195 196 201 private void init(int mask) { 202 if (getName().equals(ADMIN)) { 203 if (mask != ACTION_NONE) { 204 throw new IllegalArgumentException ("Actions specified for " 205 + "no-action " + "UserAdminPermission"); 206 } 207 } 208 else { 209 if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) { 210 throw new IllegalArgumentException ("Invalid action string"); 211 } 212 } 213 action_mask = mask; 214 } 215 216 222 private static int getMask(String actions) { 223 boolean seencomma = false; 224 int mask = ACTION_NONE; 225 if (actions == null) { 226 return (mask); 227 } 228 char[] a = actions.toCharArray(); 229 int i = a.length - 1; 230 if (i < 0) 231 return (mask); 232 while (i != -1) { 233 char c; 234 while ((i != -1) 236 && ((c = a[i]) == ' ' || c == '\r' || c == '\n' 237 || c == '\f' || c == '\t')) 238 i--; 239 int matchlen; 241 if (i >= 12 && match_get(a, i - 10) && match_credential(a, i)) { 242 matchlen = 13; 243 mask |= ACTION_GET_CREDENTIAL; 244 } 245 else 246 if (i >= 13 && match_change(a, i - 8) && match_property(a, i)) { 247 matchlen = 14; 248 mask |= ACTION_CHANGE_PROPERTY; 249 } 250 else 251 if (i >= 15 && match_change(a, i - 10) 252 && match_credential(a, i)) { 253 matchlen = 16; 254 mask |= ACTION_CHANGE_CREDENTIAL; 255 } 256 else { 257 throw new IllegalArgumentException ( 259 "invalid permission: " + actions); 260 } 261 seencomma = false; 264 while (i >= matchlen && !seencomma) { 265 switch (a[i - matchlen]) { 266 case ',' : 267 seencomma = true; 268 269 case ' ' : 270 case '\r' : 271 case '\n' : 272 case '\f' : 273 case '\t' : 274 break; 275 default : 276 throw new IllegalArgumentException ( 277 "invalid permission: " + actions); 278 } 279 i--; 280 } 281 i -= matchlen; 283 } 284 if (seencomma) { 285 throw new IllegalArgumentException ("invalid permission: " + actions); 286 } 287 return (mask); 288 } 289 290 private static boolean match_change(char[] a, int i) { 291 return ((a[i - 5] == 'c' || a[i - 5] == 'C') 292 && (a[i - 4] == 'h' || a[i - 4] == 'H') 293 && (a[i - 3] == 'a' || a[i - 3] == 'A') 294 && (a[i - 2] == 'n' || a[i - 2] == 'N') 295 && (a[i - 1] == 'g' || a[i - 1] == 'G') && (a[i - 0] == 'e' || a[i - 0] == 'E')); 296 } 297 298 private static boolean match_get(char[] a, int i) { 299 return ((a[i - 2] == 'g' || a[i - 2] == 'G') 300 && (a[i - 1] == 'e' || a[i - 1] == 'E') && (a[i - 0] == 't' || a[i - 0] == 'T')); 301 } 302 303 private static boolean match_property(char[] a, int i) { 304 return ((a[i - 7] == 'p' || a[i - 7] == 'P') 305 && (a[i - 6] == 'r' || a[i - 6] == 'R') 306 && (a[i - 5] == 'o' || a[i - 5] == 'O') 307 && (a[i - 4] == 'p' || a[i - 4] == 'P') 308 && (a[i - 3] == 'e' || a[i - 3] == 'E') 309 && (a[i - 2] == 'r' || a[i - 2] == 'R') 310 && (a[i - 1] == 't' || a[i - 1] == 'T') && (a[i - 0] == 'y' || a[i - 0] == 'Y')); 311 } 312 313 private static boolean match_credential(char[] a, int i) { 314 return ((a[i - 9] == 'c' || a[i - 9] == 'C') 315 && (a[i - 8] == 'r' || a[i - 8] == 'R') 316 && (a[i - 7] == 'e' || a[i - 7] == 'E') 317 && (a[i - 6] == 'd' || a[i - 6] == 'D') 318 && (a[i - 5] == 'e' || a[i - 5] == 'E') 319 && (a[i - 4] == 'n' || a[i - 4] == 'N') 320 && (a[i - 3] == 't' || a[i - 3] == 'T') 321 && (a[i - 2] == 'i' || a[i - 2] == 'I') 322 && (a[i - 1] == 'a' || a[i - 1] == 'A') && (a[i - 0] == 'l' || a[i - 0] == 'L')); 323 } 324 325 344 public boolean implies(Permission p) { 345 if (p instanceof UserAdminPermission) { 346 UserAdminPermission target = (UserAdminPermission) p; 347 return ( ((target.action_mask & action_mask) == target.action_mask) 349 && 350 (target.action_mask != ACTION_NONE || action_mask == ACTION_NONE) && 353 super.implies(p)); 355 } 356 else { 357 return (false); 358 } 359 } 360 361 367 public String getActions() { 368 if (actions == null) { 369 StringBuffer sb = new StringBuffer (); 370 boolean comma = false; 371 if ((action_mask & ACTION_CHANGE_CREDENTIAL) == ACTION_CHANGE_CREDENTIAL) { 372 sb.append(CHANGE_CREDENTIAL); 373 comma = true; 374 } 375 if ((action_mask & ACTION_CHANGE_PROPERTY) == ACTION_CHANGE_PROPERTY) { 376 if (comma) 377 sb.append(','); 378 sb.append(CHANGE_PROPERTY); 379 comma = true; 380 } 381 if ((action_mask & ACTION_GET_CREDENTIAL) == ACTION_GET_CREDENTIAL) { 382 if (comma) 383 sb.append(','); 384 sb.append(GET_CREDENTIAL); 385 } 386 actions = sb.toString(); 387 } 388 return (actions); 389 } 390 391 398 public PermissionCollection newPermissionCollection() { 399 return (new UserAdminPermissionCollection()); 400 } 401 402 413 public boolean equals(Object obj) { 414 if (obj == this) { 415 return (true); 416 } 417 if (obj instanceof UserAdminPermission) { 418 UserAdminPermission uap = (UserAdminPermission) obj; 419 return ((action_mask == uap.action_mask) && getName().equals( 420 uap.getName())); 421 } 422 else { 423 return (false); 424 } 425 } 426 427 430 public int hashCode() { 431 return (getName().hashCode() ^ getActions().hashCode()); 432 } 433 434 440 int getMask() { 441 return (action_mask); 442 } 443 444 448 private synchronized void writeObject(java.io.ObjectOutputStream s) 449 throws IOException { 450 if (actions == null) 453 getActions(); 454 s.defaultWriteObject(); 455 } 456 457 460 private synchronized void readObject(java.io.ObjectInputStream ois) 461 throws IOException , ClassNotFoundException { 462 ois.defaultReadObject(); 463 init(getMask(actions)); 464 } 465 466 474 public String toString() { 475 if (description == null) { 476 StringBuffer sb = new StringBuffer (); 477 sb.append('('); 478 sb.append(getClass().getName()); 479 sb.append(" \""); 480 sb.append(getName()); 481 String actions = getActions(); 482 if (actions.length() > 0) { 483 sb.append("\" \""); 484 sb.append(actions); 485 } 486 sb.append("\")"); 487 description = sb.toString(); 488 } 489 return (description); 490 } 491 } 492 496 497 final class UserAdminPermissionCollection extends PermissionCollection { 498 static final long serialVersionUID = -7222111885230120581L; 499 504 private Hashtable permissions; 505 510 private boolean all_allowed; 511 512 515 public UserAdminPermissionCollection() { 516 permissions = new Hashtable (); 517 all_allowed = false; 518 } 519 520 531 public void add(Permission permission) { 532 if (!(permission instanceof UserAdminPermission)) 533 throw new IllegalArgumentException ("Invalid permission: " 534 + permission); 535 if (isReadOnly()) { 536 throw new SecurityException ("Attempt to add a Permission to a " 537 + "readonly PermissionCollection"); 538 } 539 UserAdminPermission uap = (UserAdminPermission) permission; 540 String name = uap.getName(); 541 UserAdminPermission existing = (UserAdminPermission) permissions 542 .get(name); 543 if (existing != null) { 544 int oldMask = existing.getMask(); 545 int newMask = uap.getMask(); 546 if (oldMask != newMask) { 547 permissions.put(name, new UserAdminPermission(name, oldMask 548 | newMask)); 549 } 550 } 551 else { 552 permissions.put(name, permission); 553 } 554 if (!all_allowed) { 555 if (name.equals("*")) 556 all_allowed = true; 557 } 558 } 559 560 569 public boolean implies(Permission permission) { 570 if (!(permission instanceof UserAdminPermission)) { 571 return (false); 572 } 573 UserAdminPermission uap = (UserAdminPermission) permission; 574 UserAdminPermission x; 575 int desired = uap.getMask(); 576 int effective = 0; 577 if (all_allowed && desired != UserAdminPermission.ACTION_NONE) { 580 x = (UserAdminPermission) permissions.get("*"); 581 if (x != null) { 582 effective |= x.getMask(); 583 if ((effective & desired) == desired) { 584 return (true); 585 } 586 } 587 } 588 String name = uap.getName(); 592 x = (UserAdminPermission) permissions.get(name); 593 if (x != null) { 594 effective |= x.getMask(); 596 if ((effective & desired) == desired) { 597 return (true); 598 } 599 } 600 int last; 602 int offset = name.length() - 1; 603 while ((last = name.lastIndexOf(".", offset)) != -1) { 604 name = name.substring(0, last + 1) + "*"; 605 x = (UserAdminPermission) permissions.get(name); 606 if (x != null) { 607 effective |= x.getMask(); 608 if ((effective & desired) == desired) { 609 return (true); 610 } 611 } 612 offset = last - 1; 613 } 614 return (false); 617 } 618 619 625 public Enumeration elements() { 626 return (permissions.elements()); 627 } 628 } 629 | Popular Tags |