Project

General

Profile

« Previous | Next » 

Revision 991

Added by Jing Tao over 22 years ago

fixed auth bug

View differences:

src/edu/ucsb/nceas/metacat/AuthLdap.java
66 66
  private String ldapsUrl;
67 67
  private String ldapBase;
68 68
  private String referral;
69
  private DirContext referralContext;
69
  private Context referralContext;
70 70
  Hashtable env = new Hashtable(11);
71 71
  private Context rContext;
72 72
  private String userName;
......
466 466
  public String[] getGroups(String user, String password) 
467 467
         throws ConnectException
468 468
  {
469
      return getGroups(user, password, user);
469
      return getGroups(user, password, null);
470 470
  }
471 471

  
472 472
  /**
......
500 500
        SearchControls ctls = new SearchControls();
501 501
        ctls.setReturningAttributes(attrIDs);
502 502
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
503
        
503 504
        String filter = null;
504 505
        String gfilter = "(objectClass=groupOfUniqueNames)";
505 506
        if (null == foruser) {
......
507 508
        } else {
508 509
            filter = "(& " + gfilter + "(uniqueMember=" + foruser + "))";
509 510
        }
510
        
511
        NamingEnumeration enum = ctx.search(ldapBase, filter, ctls);
512

  
513
        // Print the groups
511 514
        Vector uvec = new Vector();
512
        
513
        try
514
        {
515
          NamingEnumeration enum = ctx.search(ldapBase, filter, ctls);
516
          // Print the groups
517
          while (enum.hasMore()) {
518
            SearchResult sr = (SearchResult)enum.next();
519
            uvec.addElement(sr.getName()+","+ldapBase);
520
            //System.out.println("result: " + sr.getName() + "," + ldapBase);
521
          }
515
        while (enum.hasMore()) {
516
          SearchResult sr = (SearchResult)enum.next();
517
          uvec.add(sr.getName()+","+ldapBase);
522 518
        }
523
        catch(ReferralException re)
524
        {
525
          boolean moreReferrals = true;
526
          while(moreReferrals)
527
          {
528
            //System.out.println("Referral: " + re.toString());
529
            refExc = re;
530
            Thread t = new Thread(this);
531
            //System.out.println("Starting thread...");
532
            t.start();
533
            //System.out.println("sleeping for 7 seconds.");
534
            try
535
            {
536
              Thread.sleep(5000);
537
            }
538
            catch(java.lang.InterruptedException iee)
539
            {
540
              //System.out.println("Main Program Sleep Interrupted");
541
            }
542
            //this is a manual override of ldap's hideously long time
543
            //out period.
544
            //System.out.println("Awake after 5 seconds.");
545
            if (referralContext == null)
546
            {
547
              t.interrupt();
548
              //System.out.println("!!!!!!!!thread interrupted!!!!!!!!!");
549
              moreReferrals = false;
550
            }
551
            else
552
            {
553
              try
554
              {
555
                //System.out.println("searching...");
556
                
557
                NamingEnumeration enum = referralContext.search(ldapBase, filter, ctls);
558
                //System.out.println("searching complete.");
559
                while (enum.hasMore() && enum != null) {
560
                  SearchResult sr = (SearchResult)enum.next();
561
                  uvec.addElement(sr.getName()+","+ldapBase);
562
                  //System.out.println("result: " + sr.getName() + "," + ldapBase);
563
                }
564
                moreReferrals = false;
565
              }
566
              catch (ReferralException re2)
567
              {
568
                moreReferrals=true;
569
                refExc=re2;
570
              }
571
              catch (AuthenticationException ae)
572
              {
573
                util.debugMessage("Error running referral handler thread: " + 
574
                                  ae.getMessage());
575
                //check if has another referral
576
                moreReferrals=refExc.skipReferral();
577
                //don't get the context
578
                referralContext = null;
579
              }
580
              catch (NamingException ne)
581
              {
582
                util.debugMessage("Error running referral handler thread: " + 
583
                                  ne.getMessage());
584
                //check if has another referral
585
                moreReferrals=refExc.skipReferral();
586
                //don't get context
587
                referralContext = null;		
588
              }
589
            }
590
          }
591
        }
519
      
592 520

  
593 521
        // initialize groups[] and fill it
594 522
        groups = new String[uvec.size()];
......
598 526

  
599 527
        // Close the context when we're done
600 528
        ctx.close();
601
    }  
602
    catch (NamingException e) 
603
    {
529

  
530
    } catch (ReferralException re) {
531
      try
532
      {
533
        refExc = re;
534
        Thread t = new Thread(new GetGroup());
535
        util.debugMessage("Starting thread...");
536
        t.start();
537
        util.debugMessage("sleeping for 5 seconds.");
538
        Thread.sleep(5000); 
539
        //this is a manual override of ldap's hideously long time
540
        //out period.
541
        util.debugMessage("Awake after 5 seconds.");
542
        if (referralContext == null)
543
        {
544
          t.interrupt();
545
          return null;
546
        }
547
        DirContext dc = (DirContext)referralContext;
548
        String[] attrIDs = {"cn"};
549
        // Specify the attributes to match.
550
        // Groups are objects with attribute objectclass=groupofuniquenames.
551
        // and have attribute uniquemember: uid=foruser,ldapbase.
552
        SearchControls ctls = new SearchControls();
553
        ctls.setReturningAttributes(attrIDs);
554
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
555
        
556
        String filter = null;
557
        String gfilter = "(objectClass=groupOfUniqueNames)";
558
        if (null == foruser) {
559
            filter = gfilter;
560
        } else {
561
            filter = "(& " + gfilter + "(uniqueMember=" + foruser + "))";
562
        }
563
        NamingEnumeration enum = dc.search(ldapBase, filter, ctls);
564

  
565
        // Print the groups
566
        Vector uvec = new Vector();
567
        while (enum.hasMore()) {
568
          SearchResult sr = (SearchResult)enum.next();
569
          uvec.add(sr.getName()+","+ldapBase);
570
        }
571

  
572
        // initialize groups[] and fill it
573
        groups = new String[uvec.size()];
574
        for (int i=0; i < uvec.size(); i++) {
575
          groups[i] = (String)uvec.elementAt(i); 
576
        }
577
        referralContext.close();
578
        dc.close();
579
      }
580
      catch (Exception e)
581
      {
582
        return groups;
583
      }
584
    } catch (NamingException e) {
604 585
      e.printStackTrace(System.err);
605 586
      throw new ConnectException(
606 587
      "Problem getting groups for a user in AuthLdap.getGroups:" + e);
......
836 817
    // Provide a user, such as: "Matt Jones", or "jones"
837 818
    String user = args[0];
838 819
    String password = args[1];
839
    String foruser = null;
840
    
841
    if(args.length == 3)
842
      foruser = args[2];
843 820

  
844 821
    AuthLdap authservice = new AuthLdap();
845 822

  
846 823
                
847 824
    boolean isValid = false;
848 825
    try {
849
      System.out.println("authenticating user " + user);
850 826
      isValid = authservice.authenticate(user, password);
851 827
      if (isValid) {
852
        System.out.println("Authentication successful for: " + user );
828
        MetaCatUtil.debugMessage("Authentication successful for: " + user );
853 829
      } else {
854
        System.out.println("Authentication failed for: " + user);
830
        MetaCatUtil.debugMessage("Authentication failed for: " + user);
855 831
      }
856 832

  
857 833
      // Get attributes for the user
858 834
      if (isValid) {
859
        System.out.println("\nGetting attributes for user....");
860
        HashMap userInfo;
861
        if(foruser == null)
862
        {
863
          userInfo = authservice.getAttributes(user, password, user);
864
        }
865
        else
866
        {
867
          userInfo = authservice.getAttributes(user, password, foruser);
868
        }
835
        MetaCatUtil.debugMessage("\nGetting attributes for user....");
836
        HashMap userInfo = authservice.getAttributes(user, password, user);
869 837
        // Print all of the attributes
870 838
        Iterator attList = (Iterator)(((Set)userInfo.keySet()).iterator());
871 839
        while (attList.hasNext()) {
......
874 842
          Iterator attvalues = values.iterator();
875 843
          while (attvalues.hasNext()) {
876 844
            String value = (String)attvalues.next();
877
            System.out.println(att + ": " + value);
845
            MetaCatUtil.debugMessage(att + ": " + value);
878 846
          }
879 847
        }
880 848
      }
881 849

  
882 850
      // get the groups
883 851
      if (isValid) {
884
        System.out.println("\nGetting all groups....");
885
        String[] groups;
886
        if(foruser == null)
887
        {
888
          groups = authservice.getGroups(user, password);
889
        }
890
        else
891
        {
892
          groups = authservice.getGroups(user, password, foruser);
893
        }
894
        System.out.println("Groups found: " + groups.length);
852
        MetaCatUtil.debugMessage("\nGetting all groups....");
853
        String[] groups = authservice.getGroups(user, password);
854
        MetaCatUtil.debugMessage("Groups found: " + groups.length);
895 855
        for (int i=0; i < groups.length; i++) {
896
            System.out.println("Group " + i + ": " + groups[i]);
856
            MetaCatUtil.debugMessage("Group " + i + ": " + groups[i]);
897 857
        }
898 858
      }
899 859

  
900 860
      // get the groups for the user
901 861
      String savedGroup = null;
902 862
      if (isValid) {
903
        System.out.println("\nGetting groups for user....");
904
        String[] groups;
905
        if(foruser == null)
906
        {
907
          groups = authservice.getGroups(user, password, user);
908
        }
909
        else
910
        {
911
          groups = authservice.getGroups(user, password, foruser);
912
        }
913
        System.out.println("Groups found: " + groups.length);
863
        MetaCatUtil.debugMessage("\nGetting groups for user....");
864
        String[] groups = authservice.getGroups(user, password, user);
865
        MetaCatUtil.debugMessage("Groups found: " + groups.length);
914 866
        for (int i=0; i < groups.length; i++) {
915
            System.out.println("Group " + i + ": " + groups[i]);
867
            MetaCatUtil.debugMessage("Group " + i + ": " + groups[i]);
916 868
            savedGroup = groups[i];
917 869
        }
918 870
      }
919 871

  
920 872
      // get the users for a group
921 873
      if (isValid) {
922
        System.out.println("\nGetting users for group....");
923
        System.out.println("Group: " + savedGroup);
874
        MetaCatUtil.debugMessage("\nGetting users for group....");
875
        MetaCatUtil.debugMessage("Group: " + savedGroup);
924 876
        String[] users = authservice.getUsers(user, password, savedGroup);
925
        System.out.println("Users found: " + users.length);
877
        MetaCatUtil.debugMessage("Users found: " + users.length);
926 878
        for (int i=0; i < users.length; i++) {
927
            System.out.println("User " + i + ": " + users[i]);
879
            MetaCatUtil.debugMessage("User " + i + ": " + users[i]);
928 880
        }
929 881
      }
930 882

  
931 883
      // get all users
932 884
      if (isValid) {
933
        System.out.println("\nGetting all users ....");
885
        MetaCatUtil.debugMessage("\nGetting all users ....");
934 886
        String[] users = authservice.getUsers(user, password);
935
        System.out.println("Users found: " + users.length);
887
        MetaCatUtil.debugMessage("Users found: " + users.length);
936 888
        
937 889
      }
938 890

  
939 891
      // get the whole list groups and users in XML format
940 892
      if (isValid) {
941
        System.out.println("\nTrying principals....");
893
        MetaCatUtil.debugMessage("\nTrying principals....");
942 894
        authservice = new AuthLdap();
943 895
        String out = authservice.getPrincipals(user, password);
944 896
        java.io.File f = new java.io.File("principals.xml");
......
948 900
        buff.flush();
949 901
        buff.close();
950 902
        fw.close();
951
        System.out.println("\nFinished getting principals.");
903
        MetaCatUtil.debugMessage("\nFinished getting principals.");
952 904
      }
953 905

  
954 906
    } catch (ConnectException ce) {
955
      System.out.println(ce.getMessage());
907
      MetaCatUtil.debugMessage(ce.getMessage());
956 908
    } catch (java.io.IOException ioe) {
957
      System.out.println("I/O Error writing to file principals.txt");
909
      MetaCatUtil.debugMessage("I/O Error writing to file principals.txt");
958 910
    }
959 911
  }
960 912
  
......
974 926
      try
975 927
      {
976 928
        //revise environment variable
977
        String refInfo = null;
978
        refInfo = (String)refExc.getReferralInfo();
979
        //refInfo = (String)refExc.getReferralContext().getEnvironment().get(Context.PROVIDER_URL);
980
        //System.out.println("refInfo: " + refInfo);
981
        if(refInfo != null)
982
        {
983
          //System.out.println("Referral in thread to: " + 
984
          //                  refInfo.toString());
985
        }
986
        else
987
        {
988
          refInfo = (String)refExc.getReferralContext().getEnvironment().get(Context.PROVIDER_URL);
989
        }
990
        
991
        /*env.put(Context.PROVIDER_URL, refExc.getReferralInfo());
929
        env.put(Context.PROVIDER_URL, refExc.getReferralInfo());
992 930
        env.put(Context.INITIAL_CONTEXT_FACTORY, 
993 931
                "com.sun.jndi.ldap.LdapCtxFactory");
994 932
        env.put(Context.SECURITY_PRINCIPAL, userName);
995 933
        env.put(Context.SECURITY_CREDENTIALS, userPassword);
996
        env.put(Context.REFERRAL, "throw");*/
997
        
934
        env.put(Context.REFERRAL, "throw");
998 935
        //get a context object for referral in the new envriment
999
        //rContext = refExc.getReferralContext();
1000
        
1001
        env.put(Context.INITIAL_CONTEXT_FACTORY, 
1002
            "com.sun.jndi.ldap.LdapCtxFactory");
1003
        env.put(Context.REFERRAL, "throw");
1004
        env.put(Context.PROVIDER_URL, refInfo);
1005
        
1006
        referralContext = new InitialDirContext(env);
936
        rContext = refExc.getReferralContext(env);
1007 937
        //casting the context to dircontext and it will create a
1008 938
        //autherntication or naming exception if DN and password is incorrect
1009
        //referralContext=rContext;
1010
        //refDirContext=(DirContext)rContext;
1011
        //refDirContext.close();
939
        referralContext=rContext;
940
        refDirContext=(DirContext)rContext;
941
        refDirContext.close();
1012 942
        //get context and jump out the while loop
1013 943
        moreReferrals=false;
1014 944
      }//try
945
      //if referral have another referral excption
1015 946
      catch (ReferralException re)
1016 947
      {
1017 948
        //keep running in while loop
......
1019 950
        //assign refExc to new referral exception re
1020 951
        refExc=re;
1021 952
      }
953
      //catch a authentication exception
1022 954
      catch (AuthenticationException ae)
1023 955
      {
1024 956
        util.debugMessage("Error running referral handler thread: " + 
......
1028 960
        //don't get the context
1029 961
        referralContext = null;
1030 962
      }
963
      //catch a naming exception
1031 964
      catch (NamingException ne)
1032 965
      {
1033 966
        util.debugMessage("Error running referral handler thread: " + 
......
1039 972
      }
1040 973
    }//while
1041 974
  }//run()
975
  
976
  private class GetGroup implements Runnable
977
  {
978
    public void run()
979
    {
980
      referralContext = null;
981
      DirContext refDirContext=null;
982
      boolean moreReferrals=true;
983
      //set a while loop is because we don't know if a referral excption 
984
      //contains another referral exception
985
      while (moreReferrals)
986
      {
987
        try
988
        {
989
          //revise environment variable
990
          String refInfo = null;
991
          refInfo = (String)refExc.getReferralInfo();
992
          //refInfo = (String)refExc.getReferralContext().getEnvironment()
993
          //.get(Context.PROVIDER_URL);
994
          //System.out.println("refInfo: " + refInfo);
995
          if(refInfo != null)
996
          {
997
            //System.out.println("Referral in thread to: " + 
998
            //                  refInfo.toString());
999
          }
1000
          else
1001
          {
1002
            refInfo = (String)refExc.getReferralContext().getEnvironment().
1003
                                                  get(Context.PROVIDER_URL);
1004
          }
1005
        
1006
          /*env.put(Context.PROVIDER_URL, refExc.getReferralInfo());
1007
          env.put(Context.INITIAL_CONTEXT_FACTORY, 
1008
                "com.sun.jndi.ldap.LdapCtxFactory");
1009
          env.put(Context.SECURITY_PRINCIPAL, userName);
1010
          env.put(Context.SECURITY_CREDENTIALS, userPassword);
1011
          env.put(Context.REFERRAL, "throw");*/
1012
        
1013
          //get a context object for referral in the new envriment
1014
          //rContext = refExc.getReferralContext();
1015
        
1016
          env.put(Context.INITIAL_CONTEXT_FACTORY, 
1017
              "com.sun.jndi.ldap.LdapCtxFactory");
1018
          env.put(Context.REFERRAL, "throw");
1019
          env.put(Context.PROVIDER_URL, refInfo);
1020
        
1021
          referralContext = new InitialDirContext(env);
1022
          //casting the context to dircontext and it will create a
1023
          //autherntication or naming exception if DN and password is incorrect
1024
          //referralContext=rContext;
1025
          //refDirContext=(DirContext)rContext;
1026
          //refDirContext.close();
1027
          //get context and jump out the while loop
1028
          moreReferrals=false;
1029
        }//try
1030
        catch (ReferralException re)
1031
        {
1032
          //keep running in while loop
1033
          moreReferrals=true;
1034
          //assign refExc to new referral exception re
1035
          refExc=re;
1036
        }
1037
        catch (AuthenticationException ae)
1038
        {
1039
          util.debugMessage("Error running referral handler thread: " + 
1040
                          ae.getMessage());
1041
          //check if has another referral
1042
          moreReferrals=refExc.skipReferral();
1043
          //don't get the context
1044
          referralContext = null;
1045
        }
1046
        catch (NamingException ne)
1047
        {
1048
          util.debugMessage("Error running referral handler thread: " + 
1049
                          ne.getMessage());
1050
          //check if has another referral
1051
          moreReferrals=refExc.skipReferral();
1052
          //don't get context
1053
          referralContext = null;		
1054
        }
1055
      }//while
1056
    }//run()
1057
 }
1042 1058
}

Also available in: Unified diff