| 1 17 18 package org.apache.catalina.realm; 19 20 import java.io.IOException ; 21 import java.security.Principal ; 22 import java.text.MessageFormat ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Hashtable ; 26 import java.util.List ; 27 28 import javax.naming.Context ; 29 import javax.naming.CommunicationException ; 30 import javax.naming.CompositeName ; 31 import javax.naming.InvalidNameException ; 32 import javax.naming.NameNotFoundException ; 33 import javax.naming.NamingEnumeration ; 34 import javax.naming.NamingException ; 35 import javax.naming.NameParser ; 36 import javax.naming.Name ; 37 import javax.naming.AuthenticationException ; 38 import javax.naming.directory.Attribute ; 39 import javax.naming.directory.Attributes ; 40 import javax.naming.directory.DirContext ; 41 import javax.naming.directory.InitialDirContext ; 42 import javax.naming.directory.SearchControls ; 43 import javax.naming.directory.SearchResult ; 44 import org.apache.catalina.LifecycleException; 45 import org.apache.catalina.util.Base64; 46 import org.apache.tomcat.util.buf.ByteChunk; 47 import org.apache.tomcat.util.buf.CharChunk; 48 49 155 156 public class JNDIRealm extends RealmBase { 157 158 159 161 164 protected String authentication = null; 165 166 169 protected String connectionName = null; 170 171 172 175 protected String connectionPassword = null; 176 177 178 181 protected String connectionURL = null; 182 183 184 187 protected DirContext context = null; 188 189 190 195 protected String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory"; 196 197 198 201 protected String derefAliases = null; 202 203 207 public final static String DEREF_ALIASES = "java.naming.ldap.derefAliases"; 208 209 212 protected static final String info = 213 "org.apache.catalina.realm.JNDIRealm/1.0"; 214 215 216 219 protected static final String name = "JNDIRealm"; 220 221 222 226 protected String protocol = null; 227 228 229 234 protected String referrals = null; 235 236 237 240 protected String userBase = ""; 241 242 243 247 protected String userSearch = null; 248 249 250 254 protected MessageFormat userSearchFormat = null; 255 256 257 260 protected boolean userSubtree = false; 261 262 263 266 protected String userPassword = null; 267 268 269 277 protected String [] userPatternArray = null; 278 279 280 285 protected String userPattern = null; 286 287 288 292 protected MessageFormat [] userPatternFormatArray = null; 293 294 295 298 protected String roleBase = ""; 299 300 301 305 protected MessageFormat roleFormat = null; 306 307 308 312 protected String userRoleName = null; 313 314 315 318 protected String roleName = null; 319 320 321 325 protected String roleSearch = null; 326 327 328 331 protected boolean roleSubtree = false; 332 333 336 protected String alternateURL; 337 338 342 protected int connectionAttempt = 0; 343 344 347 protected int curUserPattern = 0; 348 349 351 354 public String getAuthentication() { 355 356 return authentication; 357 358 } 359 360 365 public void setAuthentication(String authentication) { 366 367 this.authentication = authentication; 368 369 } 370 371 374 public String getConnectionName() { 375 376 return (this.connectionName); 377 378 } 379 380 381 386 public void setConnectionName(String connectionName) { 387 388 this.connectionName = connectionName; 389 390 } 391 392 393 396 public String getConnectionPassword() { 397 398 return (this.connectionPassword); 399 400 } 401 402 403 408 public void setConnectionPassword(String connectionPassword) { 409 410 this.connectionPassword = connectionPassword; 411 412 } 413 414 415 418 public String getConnectionURL() { 419 420 return (this.connectionURL); 421 422 } 423 424 425 430 public void setConnectionURL(String connectionURL) { 431 432 this.connectionURL = connectionURL; 433 434 } 435 436 437 440 public String getContextFactory() { 441 442 return (this.contextFactory); 443 444 } 445 446 447 452 public void setContextFactory(String contextFactory) { 453 454 this.contextFactory = contextFactory; 455 456 } 457 458 461 public java.lang.String getDerefAliases() { 462 return derefAliases; 463 } 464 465 470 public void setDerefAliases(java.lang.String derefAliases) { 471 this.derefAliases = derefAliases; 472 } 473 474 477 public String getProtocol() { 478 479 return protocol; 480 481 } 482 483 488 public void setProtocol(String protocol) { 489 490 this.protocol = protocol; 491 492 } 493 494 495 498 public String getReferrals () { 499 return referrals; 500 } 501 502 503 507 public void setReferrals (String referrals) { 508 this.referrals = referrals; 509 } 510 511 512 515 public String getUserBase() { 516 517 return (this.userBase); 518 519 } 520 521 522 527 public void setUserBase(String userBase) { 528 529 this.userBase = userBase; 530 531 } 532 533 534 537 public String getUserSearch() { 538 539 return (this.userSearch); 540 541 } 542 543 544 549 public void setUserSearch(String userSearch) { 550 551 this.userSearch = userSearch; 552 if (userSearch == null) 553 userSearchFormat = null; 554 else 555 userSearchFormat = new MessageFormat (userSearch); 556 557 } 558 559 560 563 public boolean getUserSubtree() { 564 565 return (this.userSubtree); 566 567 } 568 569 570 575 public void setUserSubtree(boolean userSubtree) { 576 577 this.userSubtree = userSubtree; 578 579 } 580 581 582 585 public String getUserRoleName() { 586 587 return userRoleName; 588 } 589 590 591 596 public void setUserRoleName(String userRoleName) { 597 598 this.userRoleName = userRoleName; 599 600 } 601 602 603 606 public String getRoleBase() { 607 608 return (this.roleBase); 609 610 } 611 612 613 618 public void setRoleBase(String roleBase) { 619 620 this.roleBase = roleBase; 621 622 } 623 624 625 628 public String getRoleName() { 629 630 return (this.roleName); 631 632 } 633 634 635 640 public void setRoleName(String roleName) { 641 642 this.roleName = roleName; 643 644 } 645 646 647 650 public String getRoleSearch() { 651 652 return (this.roleSearch); 653 654 } 655 656 657 662 public void setRoleSearch(String roleSearch) { 663 664 this.roleSearch = roleSearch; 665 if (roleSearch == null) 666 roleFormat = null; 667 else 668 roleFormat = new MessageFormat (roleSearch); 669 670 } 671 672 673 676 public boolean getRoleSubtree() { 677 678 return (this.roleSubtree); 679 680 } 681 682 683 688 public void setRoleSubtree(boolean roleSubtree) { 689 690 this.roleSubtree = roleSubtree; 691 692 } 693 694 695 698 public String getUserPassword() { 699 700 return (this.userPassword); 701 702 } 703 704 705 710 public void setUserPassword(String userPassword) { 711 712 this.userPassword = userPassword; 713 714 } 715 716 717 720 public String getUserPattern() { 721 722 return (this.userPattern); 723 724 } 725 726 727 737 public void setUserPattern(String userPattern) { 738 739 this.userPattern = userPattern; 740 if (userPattern == null) 741 userPatternArray = null; 742 else { 743 userPatternArray = parseUserPatternString(userPattern); 744 int len = this.userPatternArray.length; 745 userPatternFormatArray = new MessageFormat [len]; 746 for (int i=0; i < len; i++) { 747 userPatternFormatArray[i] = 748 new MessageFormat (userPatternArray[i]); 749 } 750 } 751 } 752 753 754 759 public String getAlternateURL() { 760 761 return this.alternateURL; 762 763 } 764 765 766 771 public void setAlternateURL(String alternateURL) { 772 773 this.alternateURL = alternateURL; 774 775 } 776 777 778 780 781 794 public Principal authenticate(String username, String credentials) { 795 796 DirContext context = null; 797 Principal principal = null; 798 799 try { 800 801 context = open(); 803 804 try { 807 808 principal = authenticate(context, username, credentials); 810 811 } catch (CommunicationException e) { 812 813 containerLog.warn(sm.getString("jndiRealm.exception"), e); 815 816 if (context != null) 818 close(context); 819 820 context = open(); 822 823 principal = authenticate(context, username, credentials); 825 826 } 827 828 829 release(context); 831 832 return (principal); 834 835 } catch (NamingException e) { 836 837 containerLog.error(sm.getString("jndiRealm.exception"), e); 839 840 if (context != null) 842 close(context); 843 844 return (null); 846 847 } 848 849 } 850 851 852 854 855 857 858 869 public synchronized Principal authenticate(DirContext context, 870 String username, 871 String credentials) 872 throws NamingException { 873 874 if (username == null || username.equals("") 875 || credentials == null || credentials.equals("")) 876 return (null); 877 878 if (userPatternArray != null) { 879 for (curUserPattern = 0; 880 curUserPattern < userPatternFormatArray.length; 881 curUserPattern++) { 882 User user = getUser(context, username); 884 if (user != null) { 885 try { 886 if (checkCredentials(context, user, credentials)) { 888 List roles = getRoles(context, user); 890 return (new GenericPrincipal(this, 891 username, 892 credentials, 893 roles)); 894 } 895 } catch (InvalidNameException ine) { 896 containerLog.warn(sm.getString("jndiRealm.exception"), ine); 898 } 903 } 904 } 905 return null; 906 } else { 907 User user = getUser(context, username); 909 if (user == null) 910 return (null); 911 912 if (!checkCredentials(context, user, credentials)) 914 return (null); 915 916 List roles = getRoles(context, user); 918 919 return (new GenericPrincipal(this, username, credentials, roles)); 921 } 922 } 923 924 925 941 protected User getUser(DirContext context, String username) 942 throws NamingException { 943 944 User user = null; 945 946 ArrayList list = new ArrayList (); 948 if (userPassword != null) 949 list.add(userPassword); 950 if (userRoleName != null) 951 list.add(userRoleName); 952 String [] attrIds = new String [list.size()]; 953 list.toArray(attrIds); 954 955 if (userPatternFormatArray != null) { 957 user = getUserByPattern(context, username, attrIds); 958 } else { 959 user = getUserBySearch(context, username, attrIds); 960 } 961 962 return user; 963 } 964 965 966 979 protected User getUserByPattern(DirContext context, 980 String username, 981 String [] attrIds) 982 throws NamingException { 983 984 if (username == null || userPatternFormatArray[curUserPattern] == null) 985 return (null); 986 987 String dn = userPatternFormatArray[curUserPattern].format(new String [] { username }); 989 990 Attributes attrs = null; 992 try { 993 attrs = context.getAttributes(dn, attrIds); 994 } catch (NameNotFoundException e) { 995 return (null); 996 } 997 if (attrs == null) 998 return (null); 999 1000 String password = null; 1002 if (userPassword != null) 1003 password = getAttributeValue(userPassword, attrs); 1004 1005 ArrayList roles = null; 1007 if (userRoleName != null) 1008 roles = addAttributeValues(userRoleName, attrs, roles); 1009 1010 return new User(username, dn, password, roles); 1011 } 1012 1013
|