1 23 24 package com.sun.enterprise.security; 25 26 import java.util.*; 27 import java.lang.reflect.*; 28 29 import java.util.logging.Logger ; 30 import java.util.logging.Level ; 31 import java.security.Principal ; 32 33 import com.sun.enterprise.config.serverbeans.Server; 34 import com.sun.enterprise.config.serverbeans.SecurityService; 35 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 36 import com.sun.enterprise.config.ConfigContext; 37 import com.sun.enterprise.server.ApplicationServer; 38 39 import com.sun.logging.LogDomains; 40 import org.apache.catalina.HttpRequest; 41 42 import com.sun.enterprise.deployment.Application; 43 import com.sun.enterprise.deployment.EjbBundleDescriptor; 44 import com.sun.enterprise.deployment.EjbDescriptor; 45 import com.sun.enterprise.deployment.MethodPermission; 46 import com.sun.enterprise.deployment.MethodDescriptor; 47 import com.sun.enterprise.deployment.RunAsIdentityDescriptor; 48 import com.sun.enterprise.deployment.EjbIORConfigurationDescriptor; 49 import com.sun.enterprise.deployment.WebBundleDescriptor; 50 import com.sun.enterprise.deployment.SecurityConstraintImpl; 51 import com.sun.enterprise.deployment.AuthorizationConstraintImpl; 52 import com.sun.enterprise.deployment.WebResourceCollectionImpl; 53 import com.sun.enterprise.deployment.web.LoginConfiguration; 54 import com.sun.enterprise.deployment.web.UserDataConstraint; 55 import com.sun.enterprise.deployment.web.SecurityRole; 56 import com.sun.enterprise.deployment.WebComponentDescriptor; 57 import com.sun.ejb.containers.EJBLocalRemoteObject; 58 import com.sun.enterprise.security.SecurityContext; 59 60 import com.sun.enterprise.deployment.Role; 61 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapper; 62 import javax.servlet.http.HttpServletRequest ; 63 64 import com.sun.appserv.security.AuditModule; 65 66 79 public class Audit extends AuditModule 80 { 81 private static final String AUDIT_ON = "auditOn"; 82 private static boolean auditFlag = false; 83 private static Logger logger = 84 LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 85 92 93 99 public static boolean isActive() 100 { 101 return auditFlag; 102 } 103 104 public void init(Properties props) { 105 super.init(props); 106 String audit = props.getProperty(AUDIT_ON); 107 auditFlag = (audit == null)?false: Boolean.valueOf(audit).booleanValue(); 108 } 109 110 116 public void authentication(String user, String realm, boolean success) { 117 if (auditFlag) { 118 StringBuffer sbuf = new StringBuffer ("Audit: Authentication for user = ("); 119 sbuf.append(user); 120 sbuf.append(") under realm = ("); 121 sbuf.append(realm).append(") returned = ").append(success); 122 logger.log(Level.INFO, sbuf.toString()); 123 } 124 } 125 126 134 public void webInvocation(String user, HttpServletRequest req, 135 String type, boolean success) 136 { 137 if (auditFlag){ 138 StringBuilder sbuf = new StringBuilder ("Audit: [Web] Authorization for user = ("); 139 sbuf.append(user).append(") and permission type = (").append(type).append(") for request "); 140 sbuf.append(req.getMethod()).append(" ").append(req.getRequestURI()).append(" returned =").append(success); 141 logger.log(Level.INFO, sbuf.toString()); 142 } 143 } 144 151 public void ejbInvocation(String user, String ejb, String method, boolean success) { 152 if(auditFlag){ 153 StringBuilder sbuf = new StringBuilder ("Audit: [EJB] Authorization for user ="); 155 sbuf.append(user).append(" for ejb = ("); 156 sbuf.append(ejb).append(") method = (").append(method).append(") returned =").append(success); 157 logger.log(Level.INFO, sbuf.toString()); 158 } 159 } 160 161 168 169 175 public void webServiceInvocation(String uri, String endpoint, boolean success) { 176 177 if(auditFlag){ 178 StringBuilder sbuf = new StringBuilder ("Audit: [WebService] "); 179 sbuf.append("uri: ").append(uri); 180 sbuf.append("endpoint: ").append(endpoint); 181 sbuf.append(", valid request =").append(success); 182 logger.log(Level.INFO, sbuf.toString()); 183 } 184 } 185 186 187 192 public void ejbAsWebServiceInvocation(String endpoint, boolean success) { 193 194 if(auditFlag){ 195 StringBuilder sbuf = new StringBuilder ("Audit: [EjbAsWebService] "); 196 sbuf.append("endpoint : ").append(endpoint).append(", valid request =").append(success); 197 logger.log(Level.INFO, sbuf.toString()); 198 } 199 } 200 201 202 205 public void serverStarted() { 206 if(auditFlag){ 207 logger.log(Level.INFO, "Audit: Application server startup complete"); 208 } 209 } 210 211 214 public void serverShutdown() { 215 if(auditFlag){ 216 logger.log(Level.INFO, "Audit: Application server shutdown complete"); 217 } 218 } 219 220 225 261 270 321 328 374 386 public static void showACL(Application app) 387 { 388 if (!isActive() || !logger.isLoggable(Level.FINEST)) { 389 return; 390 } 391 392 try { 393 dumpDiagnostics(app); 394 395 } catch (Throwable e) { 396 logger.fine("Error while showing ACL diagnostics: " + 397 e.toString()); 398 } 399 } 400 401 402 406 private static void dumpDiagnostics(Application app) 407 { 408 logger.finest("====[ Role and ACL Summary ]=========="); 409 if (!app.isVirtual()) { 410 logger.finest("Summary for application: "+ 411 app.getRegistrationName()); 412 } else { 413 logger.finest("Standalone module."); 414 } 415 logger.finest("EJB components: "+ 416 app.getEjbComponentCount()); 417 logger.finest("Web components: " + 418 app.getWebComponentCount()); 419 420 Iterator i; 421 StringBuffer sb; 422 423 Set allRoles = app.getRoles(); 425 if (allRoles == null) { 426 logger.finest("- No roles present."); 427 return; 428 } 429 SecurityRoleMapper rmap = app.getRoleMapper(); 430 if (rmap == null) { 431 logger.finest("- No role mappings present."); 432 return; 433 } 434 435 i = allRoles.iterator(); 436 logger.finest("--[ Configured roles and mappings ]--"); 437 HashMap allRoleMap = new HashMap(); 438 439 while (i.hasNext()) { 440 Role r = (Role)i.next(); 441 logger.finest(" [" + r.getName() + "]"); 442 allRoleMap.put(r.getName(), new HashSet()); 443 444 sb = new StringBuffer (); 445 sb.append(" is mapped to groups: "); 446 Enumeration grps = rmap.getGroupsAssignedTo(r); 447 while (grps.hasMoreElements()) { 448 sb.append(grps.nextElement()); 449 sb.append(" "); 450 } 451 logger.finest(sb.toString()); 452 453 sb = new StringBuffer (); 454 sb.append(" is mapped to principals: "); 455 Enumeration users = rmap.getUsersAssignedTo(r); 456 while (users.hasMoreElements()) { 457 sb.append(users.nextElement()); 458 sb.append(" "); 459 } 460 logger.finest(sb.toString()); 461 } 462 463 465 Set ejbDescriptorSet = app.getEjbBundleDescriptors() ; 466 467 i = ejbDescriptorSet.iterator(); 468 while (i.hasNext()) { 469 470 EjbBundleDescriptor bundle = (EjbBundleDescriptor)i.next(); 471 472 logger.finest("--[ EJB module: " + bundle.getName() + " ]--"); 473 Set ejbs = bundle.getEjbs(); 474 Iterator it = ejbs.iterator(); 475 while (it.hasNext()) { 476 477 EjbDescriptor ejb = (EjbDescriptor)it.next(); 478 logger.finest("EJB: "+ejb.getEjbClassName()); 479 480 if (!ejb.getUsesCallerIdentity()) { 482 RunAsIdentityDescriptor runas = ejb.getRunAsIdentity(); 483 if (runas == null) { 484 logger.finest(" (ejb does not use caller "+ 485 "identity)"); 486 } else { 487 String role = runas.getRoleName(); 488 String user = runas.getPrincipal(); 489 logger.finest(" Will run-as: Role: " + role + 490 " Principal: " + user); 491 if (role==null || "".equals(role) || 492 user==null || "".equals(user)) { 493 if(logger.isLoggable(Level.FINEST)){ 494 logger.finest("*** Configuration error!"); 495 } 496 } 497 } 498 } 499 500 logger.finest(" Method to Role restriction list:"); 502 Set methods = ejb.getMethodDescriptors(); 503 Iterator si = methods.iterator(); 504 505 while (si.hasNext()) { 506 507 MethodDescriptor md = (MethodDescriptor)si.next(); 508 logger.finest(" "+md.getFormattedString()); 509 510 Set perms = ejb.getMethodPermissionsFor(md); 511 StringBuffer rbuf = new StringBuffer (); 512 rbuf.append(" can only be invoked by: "); 513 Iterator sip = perms.iterator(); 514 boolean unchecked=false,excluded=false,roleBased=false; 515 516 while (sip.hasNext()) { 517 MethodPermission p = (MethodPermission)sip.next(); 518 if (p.isExcluded()) { 519 excluded=true; 520 logger.finest(" excluded - can not "+ 521 "be invoked"); 522 } else if (p.isUnchecked()) { 523 unchecked=true; 524 logger.finest(" unchecked - can be "+ 525 "invoked by all"); 526 } else if (p.isRoleBased()) { 527 roleBased = true; 528 Role r = p.getRole(); 529 rbuf.append(r.getName()); 530 rbuf.append(" "); 531 HashSet ram = (HashSet)allRoleMap.get(r.getName()); 533 ram.add(bundle.getName() + ":" + 534 ejb.getEjbClassName() + "." + 535 md.getFormattedString()); 536 } 537 } 538 539 if (roleBased) { 540 logger.finest(rbuf.toString()); 541 if (excluded || unchecked) { 542 logger.finest("*** Configuration error!"); 543 } 544 } else if (unchecked) { 545 if (excluded) { 546 logger.finest("*** Configuration error!"); 547 } 548 Set rks = allRoleMap.keySet(); 549 Iterator rksi = rks.iterator(); 550 while (rksi.hasNext()) { 551 HashSet ram = (HashSet)allRoleMap.get(rksi.next()); 552 ram.add(bundle.getName() + ":" + 553 ejb.getEjbClassName() + "." + 554 md.getFormattedString()); 555 } 556 } else if (!excluded) { 557 logger.finest("*** Configuration error!"); 558 } 559 } 560 561 logger.finest(" IOR configuration:"); 563 Set iors = ejb.getIORConfigurationDescriptors(); 564 if (iors != null) { 565 Iterator iorsi = iors.iterator(); 566 while (iorsi.hasNext()) { 567 EjbIORConfigurationDescriptor ior = 568 (EjbIORConfigurationDescriptor)iorsi.next(); 569 StringBuffer iorsb = new StringBuffer (); 570 iorsb.append("realm="); 571 iorsb.append(ior.getRealmName()); 572 iorsb.append(", integrity="); 573 iorsb.append(ior.getIntegrity()); 574 iorsb.append(", trust-in-target="); 575 iorsb.append(ior.getEstablishTrustInTarget()); 576 iorsb.append(", trust-in-client="); 577 iorsb.append(ior.getEstablishTrustInClient()); 578 iorsb.append(", propagation="); 579 iorsb.append(ior.getCallerPropagation()); 580 iorsb.append(", auth-method="); 581 iorsb.append(ior.getAuthenticationMethod()); 582 logger.finest(iorsb.toString()); 583 } 584 } 585 } 586 } 587 588 logger.finest("--[ EJB methods accessible by role ]--"); 590 591 Set rks = allRoleMap.keySet(); 592 Iterator rksi = rks.iterator(); 593 while (rksi.hasNext()) { 594 String roleName = (String )rksi.next(); 595 logger.finest(" [" + roleName + "]"); 596 HashSet ram = (HashSet)allRoleMap.get(roleName); 597 Iterator rami = ram.iterator(); 598 while (rami.hasNext()) { 599 String meth = (String )rami.next(); 600 logger.finest(" "+meth); 601 } 602 } 603 604 605 606 608 Set webDescriptorSet = app.getWebBundleDescriptors() ; 609 610 i = webDescriptorSet.iterator(); 611 while (i.hasNext()) { 612 WebBundleDescriptor wbd = (WebBundleDescriptor)i.next(); 613 logger.finest("--[ Web module: " + wbd.getContextRoot() + " ]--"); 614 615 LoginConfiguration lconf = wbd.getLoginConfiguration(); 617 if (lconf != null) { 618 logger.finest(" Login config: realm="+ 619 lconf.getRealmName() + ", method="+ 620 lconf.getAuthenticationMethod() + ", form="+ 621 lconf.getFormLoginPage() + ", error="+ 622 lconf.getFormErrorPage()); 623 } 624 625 logger.finest(" Contains components:"); 627 Set webComps = wbd.getWebComponentDescriptorsSet(); 628 Iterator webCompsIt = webComps.iterator(); 629 while (webCompsIt.hasNext()) { 630 WebComponentDescriptor wcd = 631 (WebComponentDescriptor)webCompsIt.next(); 632 StringBuffer name = new StringBuffer (); 633 name.append(" - "+wcd.getCanonicalName()); 634 name.append(" [ "); 635 Enumeration urlPs = wcd.getUrlPatterns(); 636 while (urlPs.hasMoreElements()) { 637 name.append(urlPs.nextElement().toString()); 638 name.append(" "); 639 } 640 name.append("]"); 641 logger.finest(name.toString()); 642 643 RunAsIdentityDescriptor runas = 644 (RunAsIdentityDescriptor)wcd.getRunAsIdentity(); 645 if (runas!=null) { 646 String role = runas.getRoleName(); 647 String user = runas.getPrincipal(); 648 logger.finest(" Will run-as: Role: " + role + 649 " Principal: " + user); 650 if (role==null || "".equals(role) || 651 user==null || "".equals(user)) { 652 logger.finest("*** Configuration error!"); 653 } 654 } 655 656 } 657 658 logger.finest(" Security constraints:"); 660 Enumeration scEnum = wbd.getSecurityConstraints(); 661 while (scEnum.hasMoreElements()) { 662 663 SecurityConstraintImpl sc = 664 (SecurityConstraintImpl)scEnum.nextElement(); 665 666 Set wrcSet = sc.getWebResourceCollectionSet(); 667 Iterator wrcIt = wrcSet.iterator(); 668 while (wrcIt.hasNext()) { 669 WebResourceCollectionImpl wrc = 670 (WebResourceCollectionImpl)wrcIt.next(); 671 672 Enumeration methEnum = wrc.getHttpMethods(); 674 StringBuffer sbm = new StringBuffer (); 675 while (methEnum.hasMoreElements()) { 676 sbm.append(methEnum.nextElement()); 677 sbm.append(" "); 678 } 679 logger.finest(" Using method: "+sbm.toString()); 680 681 Enumeration urlEnum = wrc.getUrlPatterns(); 683 while (urlEnum.hasMoreElements()) { 684 logger.finest(" "+ 685 urlEnum.nextElement().toString()); 686 } 687 } 689 AuthorizationConstraintImpl authCons = 691 (AuthorizationConstraintImpl)sc.getAuthorizationConstraint(); 692 Enumeration rolesEnum = authCons.getSecurityRoles(); 693 StringBuffer rsb = new StringBuffer (); 694 rsb.append(" Accessible by roles: "); 695 while (rolesEnum.hasMoreElements()) { 696 SecurityRole sr = (SecurityRole)rolesEnum.nextElement(); 697 rsb.append(sr.getName()); 698 rsb.append(" "); 699 } 700 logger.finest(rsb.toString()); 701 702 UserDataConstraint udc =sc.getUserDataConstraint(); 704 if (udc != null) { 705 logger.finest(" Transport guarantee: "+ 706 udc.getTransportGuarantee()); 707 } 708 709 } 711 } 713 714 logger.finest("======================================"); 715 } 716 717 718 } 719 | Popular Tags |