Project

General

Profile

« Previous | Next » 

Revision 8489

Added by Jing Tao almost 11 years ago

Consolidate the user/group utitlities code.

View differences:

src/edu/ucsb/nceas/metacat/authentication/AuthFile.java
618 618
     * Handle the groupAdd action in the main method
619 619
     */
620 620
    private static void handleGroupAdd(AuthFile authFile, String[]argus) throws AuthenticationException {
621
        HashMap<String, String> map = new <String, String>HashMap();
621
        HashMap<String, String> map = null;
622 622
        String G = "-g";
623 623
        String D = "-d";
624
        Vector<String> possibleOptions = new <String>Vector();
625
        possibleOptions.add(G);
626
        possibleOptions.add(D);
627
        for(int i=2; i<argus.length; i++) {
628
            String arg = argus[i];
629
            
630
            if(map.containsKey(arg)) {
631
                System.out.println("Error: the command line for groupadd can't have the duplicated options "+arg+".");
632
                System.exit(1);
633
            }
634
            
635
            
636
            if(possibleOptions.contains(arg) && i<argus.length-1) {
637
                if(possibleOptions.contains(argus[i+1])) {
638
                    //scenario that -d follows -g
639
                    System.out.println("Error: the \""+arg+"\" must be followed by a value rather than an option \""+argus[i+1]+"\"");
640
                    System.exit(1);
641
                } else {
642
                    map.put(arg, argus[i+1]);
643
                }
644
            } else if(!possibleOptions.contains(arg)) {
645
                //when the argu is not a switch check if the previous argument is a switch
646
                if(!possibleOptions.contains(argus[i-1])) {
647
                    System.out.println("Error: an illegal argument "+arg+" in the groupadd command ");
648
                    System.exit(1);
649
                }
650
            }
651
        } 
624
        Vector<String> pairedOptions = new Vector<String>();
625
        pairedOptions.add(G);
626
        pairedOptions.add(D);
627
        int startIndex = 2;
628
        try {
629
            map = parseArgus(startIndex, argus, pairedOptions, null);
630
        } catch (Exception e ) {
631
            System.out.println("Error in the groupadd command: "+e.getMessage());
632
            System.exit(1);
633
        }
652 634
        String groupName = null;
653 635
        String description = null;
654 636
        if(map.keySet().size() == 0) {
655
            System.out.println("Error: the "+G+" group-name is required in the groupadd command line.");
637
            System.out.println("Error in the groupadd command: the \""+G+" group-name\" is required.");
656 638
            System.exit(1);
657
        }
658
        else if(map.keySet().size() ==1 || map.keySet().size() ==2) {
639
        } else if(map.keySet().size() ==1 || map.keySet().size() ==2) {
659 640
            groupName = map.get(G);
660
            if(groupName == null) {
661
                System.out.println("Error: the "+G+" group-name is required in the groupadd command line.");
641
            if(groupName == null || groupName.trim().equals("")) {
642
                System.out.println("Error in the groupadd command : the \""+G+" group-name\" is required.");
662 643
                System.exit(1);
663 644
            }
664 645
            description = map.get(D);
665 646
            authFile.addGroup(groupName, description);
666
            System.out.println("Successfully added a group "+groupName+" to the file authentication system");
647
            System.out.println("Successfully added a group \""+groupName+"\" to the file authentication system");
667 648
        } else {
668 649
            printError(argus);
669 650
            System.exit(1);
......
674 655
     * Handle the userAdd action in the main method
675 656
     */
676 657
    private static void  handleUserAdd(AuthFile authFile,String[]argus) throws UnsupportedEncodingException, AuthenticationException{
677
        boolean inputPassword = false;
678
        boolean passingHashedPassword = false;
679
        boolean hasDN = false;
658
      
680 659
        String I = "-i";
681 660
        String H = "-h";
682 661
        String DN = "-dn";
......
685 664
        String S = "-s";
686 665
        String F = "-f";
687 666
        String O= "-o";
688
        Vector<String> possibleOptions = new <String>Vector();
689
        possibleOptions.add(I);
690
        possibleOptions.add(H);
691
        possibleOptions.add(DN);
692
        possibleOptions.add(G);
693
        possibleOptions.add(E);
694
        possibleOptions.add(S);
695
        possibleOptions.add(F);
696
        possibleOptions.add(O);
667
        Vector<String> pairedOptions = new Vector<String>();
668
        pairedOptions.add(H);
669
        pairedOptions.add(DN);
670
        pairedOptions.add(G);
671
        pairedOptions.add(E);
672
        pairedOptions.add(S);
673
        pairedOptions.add(F);
674
        pairedOptions.add(O);
675
        Vector<String> singleOptions = new Vector<String>();
676
        singleOptions.add(I);
697 677
        
698
        HashMap<String, String> map = new <String, String>HashMap();
699
        for(int i=2; i<argus.length; i++) {
700
            String arg = argus[i];
701
            
702
            if(map.containsKey(arg)) {
703
                System.out.println("Error: the command line for useradd can't have the duplicated options "+arg+".");
704
                System.exit(1);
705
            }
706
            
707
            //this is the scenario that "-i" is at the end of the arguments.
708
            if(arg.equals(I) && i==argus.length-1) {
709
                map.put(I, I);//we need to input password.
710
                inputPassword = true;
711
            } 
712
            
713
            if(possibleOptions.contains(arg) && i<argus.length-1) {
714
                //System.out.println("find the option "+arg);
715
                if(arg.equals(I)) {
716
                    //this is the scenario that "-i" is NOT at the end of the arguments.
717
                    if(!possibleOptions.contains(argus[i+1])) {
718
                        System.out.println("Error: The option \"-i\" means the user will input a password in the useradd command. So it can't be followed by a value. It only can be followed by another option.");
719
                        System.exit(1);
720
                    }
721
                    map.put(I, I);//we need to input password.
722
                    inputPassword = true;
723
                } else {
724
                    if(arg.equals(H)) {
725
                        passingHashedPassword = true;
726
                    } else if (arg.equals(DN)) {
727
                        hasDN = true;
728
                    }
729
                    map.put(arg, argus[i+1]);
730
                }
731
                
732
            } else if(!possibleOptions.contains(arg)) {
733
                //check if the previous argument is an option
734
                if(!possibleOptions.contains(argus[i-1])) {
735
                    System.out.println("Error: an illegal argument "+arg+" in the useradd command ");
736
                    System.exit(1);
737
                }
738
            }
678
        HashMap<String, String> map = new HashMap<String, String>();
679
        int startIndex = 2;
680
        try {
681
            map = parseArgus(startIndex, argus, pairedOptions, singleOptions);
682
        } catch (Exception e) {
683
            System.out.println("Error in the useradd command: "+e.getMessage());
684
            System.exit(1);
685
        }
686
       
687
        String dn = map.get(DN);
688
        if(dn == null || dn.trim().equals("")) {
689
            System.out.println("The \"-dn user-distinguish-name\" is requried in the useradd command ."); 
690
            System.exit(1);
739 691
        } 
740
        
741
        String dn = null;
742 692
        String plainPassword = null;
743 693
        String hashedPassword = null;
744
        if(!hasDN) {
745
            System.out.println("The \"-dn user-distinguish-name\" is requried in the useradd command ."); 
746
            System.exit(1);
747
        } else {
748
            dn = map.get(DN);
749
        }
694
       
750 695
        
751
       
752
        if(inputPassword && passingHashedPassword) {
753
            System.out.println("Error: you can choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the useradd command.");
696
        String input = map.get(I);
697
        String passHash = map.get(H);
698
        if(input != null && passHash != null) {
699
            System.out.println("Error in the useradd command: you only can choose either \"-i\"(input a password) or \"-h hashed-password\"(pass through a hashed passwword).");
754 700
            System.exit(1);
755
        } else if (!inputPassword && !passingHashedPassword) {
756
            System.out.println("Error: you must choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the useradd command.");
701
        } else if (input == null && passHash == null) {
702
            System.out.println("Error in the useradd command: you must choose either \"-i\"(input a password) or \"-h hashed-password\"(pass through a hashed password).");
757 703
            System.exit(1);
758
        } else if(inputPassword) {
704
        } else if(input != null) {
759 705
            plainPassword = inputPassword();
760 706
            //System.out.println("============the plain password is "+plainPassword);
761
        } else if(passingHashedPassword) {
762
            hashedPassword = map.get(H);
707
        } else if(passHash != null) {
708
            hashedPassword = passHash;
763 709
        }
764 710
        
765 711
        String group = map.get(G);
712
        //System.out.println("the groups name is "+group);
766 713
        String[] groups = null;
767 714
        if(group != null && !group.trim().equals("")) {
768 715
            groups = new String[1];
769 716
            groups[0]=group;
717
            //System.out.println("set the first element of the groups to "+groups[0]);
770 718
        }
771 719
        String email = map.get(E);
772 720
        String surname = map.get(S);
773 721
        String givenname = map.get(F);
774 722
        String organization = map.get(O);
775 723
        authFile.addUser(dn, groups, plainPassword, hashedPassword, email, surname, givenname, organization);
776
        System.out.println("Successfully added a user "+dn+" to the file authentication system ");
724
        System.out.println("Successfully added a user \""+dn+"\" to the file authentication system ");
777 725
    }
778 726
    
779 727
    /*
......
804 752
        String DN = "-dn";
805 753
        String I= "-i";
806 754
        String H = "-h";
807
        Vector<String> possibleOptions = new <String>Vector();
808
        possibleOptions.add(I);
809
        possibleOptions.add(H);
810
        possibleOptions.add(DN);
811
        boolean inputPassword = false;
812
        boolean passingHashedPassword = false;
813
        boolean hasDN = false;
814
        HashMap<String, String> map = new <String, String>HashMap();
815
        for(int i=3; i<argus.length; i++) {
816
            String arg = argus[i];
817
            if(map.containsKey(arg)) {
818
                System.out.println("Error: the command line for usermod -password can't have the duplicated options "+arg+".");
819
                System.exit(1);
820
            }
821
            
822
            //this is the scenario that "-i" is at the end of the arguments.
823
            if(arg.equals(I) && i==argus.length-1) {
824
                map.put(I, I);//we need to input password.
825
                inputPassword = true;
826
            } 
827
            
828
            if(possibleOptions.contains(arg) && i<argus.length-1) {
829
                //System.out.println("find the option "+arg);
830
                if(arg.equals(I)) {
831
                    //this is the scenario that "-i" is NOT at the end of the arguments.
832
                    if(!possibleOptions.contains(argus[i+1])) {
833
                        System.out.println("Error: The option \"-i\" means the user will input a password in the usermod -password command. So it can't be followed by a value. It only can be followed by another option.");
834
                        System.exit(1);
835
                    }
836
                    map.put(I, I);//we need to input password.
837
                    inputPassword = true;
838
                } else {
839
                    if(arg.equals(H)) {
840
                        passingHashedPassword = true;
841
                    } else if (arg.equals(DN)) {
842
                        hasDN = true;
843
                    }
844
                    map.put(arg, argus[i+1]);
845
                }
846
                
847
            } else if(!possibleOptions.contains(arg)) {
848
                //check if the previous argument is an option
849
                if(!possibleOptions.contains(argus[i-1])) {
850
                    System.out.println("Error: an illegal argument "+arg+" in the usermod -password command ");
851
                    System.exit(1);
852
                }
853
            }
854
        } 
855
        
856
        String dn = null;
755
        Vector<String> pairedOptions = new Vector<String>();
756
        pairedOptions.add(H);
757
        pairedOptions.add(DN);
758
        Vector<String> singleOptions = new Vector<String>();
759
        singleOptions.add(I);
760
        HashMap<String, String> map = new HashMap<String, String>();
761
        int startIndex = 3;
762
        try {
763
            map = parseArgus(startIndex, argus, pairedOptions, singleOptions);
764
        } catch (Exception e) {
765
            System.out.println("Error in the usermod -password command: "+e.getMessage());
766
            System.exit(1);
767
        }
768
      
769
        String dn = map.get(DN);
770
        if(dn == null || dn.trim().equals("")) {
771
            System.out.println("Error in the usermod -password command: The \"-dn user-distinguish-name\" is requried."); 
772
            System.exit(1);
773
        }
857 774
        String plainPassword = null;
858 775
        String hashedPassword = null;
859
        if(!hasDN) {
860
            System.out.println("The \"-dn user-distinguish-name\" is requried in the usermod -password command ."); 
776
        
777
        String input = map.get(I);
778
        String passHash = map.get(H);
779
        if(input != null && passHash != null) {
780
            System.out.println("Error in the usermod -password command: you only can choose either \"-i\"(input a password) or \"-h hashed-password\"(pass through a hashed password).");
861 781
            System.exit(1);
862
        } else {
863
            dn = map.get(DN);
864
        }
865

  
866
        if(inputPassword && passingHashedPassword) {
867
            System.out.println("Error: you can choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the usermod -password command.");
782
        } else if (input == null && passHash == null) {
783
            System.out.println("Error in the usermod -password command: you must choose either \"-i\"(input a password) or \"-h hashed-password\"(pass through a hashed password).");
868 784
            System.exit(1);
869
        } else if (!inputPassword && !passingHashedPassword) {
870
            System.out.println("Error: you must choose either \"-i\"(input a password) or \"-d dashed-passpwrd\"(pass through a hashed passwprd) in the usermod -password command.");
871
            System.exit(1);
872
        } else if(inputPassword) {
785
        } else if(input != null) {
873 786
            plainPassword = inputPassword();
787
            //System.out.println("============the plain password is "+plainPassword);
874 788
            authFile.modifyPassWithPlain(dn, plainPassword);
875
            System.out.println("Successfully modified the password for the user "+dn);
876
            //System.out.println("============the plain password is "+plainPassword);
877
        } else if(passingHashedPassword) {
878
            hashedPassword = map.get(H);
789
            System.out.println("Successfully modified the password for the user \""+dn+"\".");
790
        } else if(passHash != null) {
791
            hashedPassword = passHash;
879 792
            authFile.modifyPassWithHash(dn, hashedPassword);
880
            System.out.println("Successfully modified the password for the user "+dn);
793
            System.out.println("Successfully modified the password for the user "+dn+"\".");
881 794
        }
882 795
    }
883 796
    
......
889 802
        String A= "-a";
890 803
        String R = "-r";
891 804
        String G = "-g";
892
        Vector<String> possibleOptions = new <String>Vector();
893
        possibleOptions.add(DN);
894
        possibleOptions.add(A);
895
        possibleOptions.add(R);
896
        possibleOptions.add(G);
897
        HashMap<String, String> map = new <String, String>HashMap();
898
        for(int i=3; i<argus.length; i++) {
899
            String arg = argus[i];
900
            if(map.containsKey(arg)) {
901
                System.out.println("Error: the command line for the usermod -group can't have the duplicated options "+arg+".");
902
                System.exit(1);
903
            }
904
            
905
            //this is the scenario that "-a" or "-r" is at the end of the arguments.
906
            if((arg.equals(A) || arg.equals(R)) && i==argus.length-1) {
907
                map.put(arg, arg);//we need to input password.
908
            } 
909
            
910
            if(possibleOptions.contains(arg) && i<argus.length-1) {
911
                //System.out.println("find the option "+arg);
912
                if(arg.equals(A) || arg.equals(R)) {
913
                    //this is the scenario that "-a" or "-r" is NOT at the end of the arguments.
914
                    if(!possibleOptions.contains(argus[i+1])) {
915
                        System.out.println("Error: the option \"-a\" or \"-d\" shouldn't follow any value.");
916
                        System.exit(1);
917
                    }
918
                    map.put(arg, arg);
919
                    
920
                } else {
921
                    map.put(arg, argus[i+1]);
922
                }
923
                
924
            } else if(!possibleOptions.contains(arg)) {
925
                //check if the previous argument is an option
926
                if(!possibleOptions.contains(argus[i-1])) {
927
                    System.out.println("Error: an illegal argument "+arg+" in the usermod -group command ");
928
                    System.exit(1);
929
                }
930
            }
805
        Vector<String> pairedOptions = new Vector<String>();
806
        pairedOptions.add(G);
807
        pairedOptions.add(DN);
808
        Vector<String> singleOptions = new Vector<String>();
809
        singleOptions.add(A);
810
        singleOptions.add(R);
811
        HashMap<String, String> map = new HashMap<String, String>();
812
        int startIndex = 3;
813
        try {
814
            map = parseArgus(startIndex, argus, pairedOptions, singleOptions);
815
        } catch (Exception e) {
816
            System.out.println("Error in the usermod -group command: "+e.getMessage());
817
            System.exit(1);
931 818
        }
932
        
819
               
933 820
        String add = map.get(A);
934 821
        String remove = map.get(R);
935 822
        String group = map.get(G);
936 823
        String dn = map.get(DN);
937 824
        if(dn == null || dn.trim().equals("")) {
938
            System.out.println("Erorr: the \"-dn user-distinguish-name\" is required in the usermod -group command");
825
            System.out.println("Erorr in the usermod -group command: the \"-dn user-distinguish-name\" is required.");
939 826
            System.exit(1);
940 827
        }
941 828
        
942 829
        if(group == null || group.trim().equals("")) {
943
            System.out.println("Erorr: the \"-g group-name\" is required in the usermod -group command");
830
            System.out.println("Erorr in the usermod -group command: the \"-g group-name\" is required.");
944 831
            System.exit(1);
945 832
        }
946 833
        
947 834
        if(add != null && remove!= null) {
948
            System.out.println("Erorr: You can only choose either \"-a\"(add the user to the group or \"-d\"(remove the user from the group in the usermod -group command");
835
            System.out.println("Erorr in the usermod -group command: You can only choose either \"-a\"(add the user to the group) or \"-r\"(remove the user from the group).");
949 836
            System.exit(1);
950 837
        } else if (add == null && remove == null) {
951
            System.out.println("Erorr: You must only choose either \"-a\"(add the user to the group or \"-d\"(remove the user from the group in the usermod -group command");
838
            System.out.println("Erorr in the usermod -group command: You must choose either \"-a\"(add the user to the group) or \"-r\"(remove the user from the group).");
952 839
            System.exit(1);
953 840
        } else if (remove != null) {
954 841
            authFile.removeUserFromGroup(dn, group);
955
            System.out.println("Successfully removed the user "+dn+" from the group "+group);
842
            System.out.println("Successfully removed the user "+dn+" from the group \""+group+"\".");
956 843
        } else {
957 844
            authFile.addUserToGroup(dn, group);
958
            System.out.println("Successfully added the user "+dn+" to the group "+group);
845
            System.out.println("Successfully added the user "+dn+" to the group \""+group+"\".");
959 846
        }
960 847
    }
961 848
    
849
    
850
    /**
851
     * Parse the arguments to get the pairs of option/value. If it is a single option (it doesn't need a value), the pair will be switch/switch. 
852
     * @param startIndex the index of arguments where we will start.
853
     * @param argus the arguments array will be parsed.
854
     * @param pairedOptions the known options which should be a pair
855
     * @param singleOptions the know options which just has a single value
856
     * @return the empty map if there is no pairs were found
857
     * @throws Exception if there is an illegal argument.
858
     */
859
    private static HashMap<String, String> parseArgus(int startIndex, String[]argus, Vector<String>pairedOptions, Vector<String>singleOptions) throws Exception {
860
        HashMap<String, String> map = new <String, String>HashMap();
861
        if(argus != null) {
862
            for(int i=startIndex; i<argus.length; i++) {
863
                String arg = argus[i];
864
                if(map.containsKey(arg)) {
865
                    throw new Exception("The command line can't have the duplicated options \""+arg+"\".");
866
                }
867
                
868
                if(singleOptions != null && singleOptions.contains(arg)) {
869
                    //we find a single option
870
                    if(i==argus.length-1) {
871
                        //it is the last argument, this is fine.
872
                        map.put(arg, arg);
873
                    } else if (i<argus.length -1) {
874
                        //it is not the last argument. 
875
                        if ((pairedOptions != null && pairedOptions.contains(argus[i+1])) || singleOptions.contains(argus[i+1])) {
876
                            //it follows an option, this is fine
877
                            map.put(arg, arg);
878
                        } else {
879
                            //it follows a vlaue, this is illegal
880
                            throw new Exception("The option \""+arg+"\" shouldn't be followed any value, e.g. "+ argus[i+1]+".");
881
                        }
882
                    }
883
                } else if (pairedOptions != null && pairedOptions.contains(arg)) {
884
                    //we found an option which should follow a vlaue
885
                    if(i==argus.length-1) {
886
                        //it is the last argument (no value follows it)
887
                        throw new Exception("The option \""+arg+"\" must be followed by a value");
888
                    } else {
889
                        //it is not the last argument and we need to check its following value
890
                        if (!pairedOptions.contains(argus[i+1]) && (singleOptions == null || !singleOptions.contains(argus[i+1]))) {
891
                            //it is NOT followed by an option, this is fine
892
                            map.put(arg, argus[i+1]);
893
                        } else {
894
                            //it is followed by an option, this is illegal
895
                            throw new Exception("The option \""+arg+"\" shouldn't be followed the option \""+ argus[i+1]+"\". It should be followed by a value.");
896
                        }
897
                    }
898
                } else {
899
                    //we found an argument is not an option
900
                    if(pairedOptions == null || !pairedOptions.contains(argus[i-1])) {
901
                        //the previous argument is not an option switch
902
                        throw new Exception("The \""+arg+"\" is an illegal argument");
903
                    }
904
                }
905
            } 
906
        }
907
        return map;
908
    }
909
    
962 910
    /*
963 911
     * Input the password
964 912
     */
......
967 915
        String quit = "q";
968 916
        Console console = System.console();
969 917
        if (console == null) {
970
            System.out.println("Sorry, we can't fetch the console from the system. You can't use the option \"-i\" to input a password. You have to use the option \"-d dashed-passpwrd\" to pass through a hashed passwprd in the useradd command. ");
918
            System.out.println("Sorry, we can't fetch the console from the system. You can't use the option \"-i\" to input a password. You have to use the option \"-h hashed-passpwrd\" to pass through a hashed passwprd in the useradd command. ");
971 919
            System.exit(1);
972 920
        }
973 921
  
......
1007 955
     */
1008 956
    private static void printUsage() {
1009 957
        System.out.println("Usage:\n"+
1010
                        "./authFileManager.sh useradd -i -dn user-distinguish-name -g groupname -e email-address -s surname -f given-name -o organizationName\n" +
1011
                        "./authFileManager.sh useradd -h hashed-password -dn user-distinguish-name -g groupname -e email-address -s surname -f given-name -o organizationName\n"+
958
                        "./authFileManager.sh useradd -i -dn user-distinguish-name -g group-name -e email-address -s surname -f given-name -o organizationName\n" +
959
                        "./authFileManager.sh useradd -h hashed-password -dn user-distinguish-name -g group-name -e email-address -s surname -f given-name -o organizationName\n"+
1012 960
                        "./authFileManager.sh groupadd -g group-name -d description\n" +
1013 961
                        "./authFileManager.sh usermod -password -dn user-distinguish-name -i\n"+
1014 962
                        "./authFileManager.sh usermod -password -dn user-distinguish-name -h new-hashed-password\n"+
1015
                        "./authFileManager.sh usermod -group -a -dn user-disinguish-name -g added-group-name\n" +
963
                        "./authFileManager.sh usermod -group -a -dn user-distinguish-name -g added-group-name\n" +
1016 964
                        "./authFileManager.sh usermod -group -r -dn user-distinguish-name -g removed-group-name\n"+
1017 965
                        "Note:\n1. if a value of an option has spaces, the value should be enclosed by the double quotes.\n"+
1018 966
                        "  For example: ./authFileManager.sh groupadd -g nceas-dev -d \"Developers at NCEAS\"\n"+
......
1352 1300
                    }
1353 1301
                }
1354 1302
            }
1303
            if(groups != null) {
1304
                for(int i=0; i<groups.length; i++) {
1305
                    String group = groups[i];
1306
                    if(group != null && !group.trim().equals("")) {
1307
                        if(!groupExists(group)) {
1308
                            throw new AuthenticationException("AuthFile.User.serialize - can't put the user into a non-existing group "+group);
1309
                        }
1310
                    }
1311
                }
1312
            }
1355 1313

  
1356 1314
            if(!userExists(dn)) {
1357 1315
                if(userpassword != null) {

Also available in: Unified diff