Revision 8485
Added by Jing Tao almost 11 years ago
src/edu/ucsb/nceas/metacat/authentication/AuthFile.java | ||
---|---|---|
20 | 20 |
*/ |
21 | 21 |
package edu.ucsb.nceas.metacat.authentication; |
22 | 22 |
|
23 |
import java.io.Console; |
|
23 | 24 |
import java.io.File; |
24 | 25 |
import java.io.FileOutputStream; |
25 | 26 |
import java.io.IOException; |
26 | 27 |
import java.io.OutputStreamWriter; |
27 | 28 |
import java.io.UnsupportedEncodingException; |
28 | 29 |
import java.net.ConnectException; |
29 |
import java.security.GeneralSecurityException; |
|
30 |
import java.util.Enumeration; |
|
31 | 30 |
import java.util.HashMap; |
32 |
import java.util.Hashtable; |
|
33 | 31 |
import java.util.List; |
34 |
import java.util.Random; |
|
35 | 32 |
import java.util.Vector; |
36 | 33 |
|
37 |
import javax.crypto.Cipher; |
|
38 |
import javax.crypto.SecretKey; |
|
39 |
import javax.crypto.SecretKeyFactory; |
|
40 |
import javax.crypto.spec.PBEKeySpec; |
|
41 |
import javax.crypto.spec.PBEParameterSpec; |
|
42 | 34 |
|
43 |
import org.apache.commons.codec.binary.Base64; |
|
35 |
|
|
44 | 36 |
import org.apache.commons.configuration.ConfigurationException; |
45 | 37 |
import org.apache.commons.configuration.XMLConfiguration; |
46 | 38 |
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; |
... | ... | |
664 | 656 |
/* |
665 | 657 |
* Handle the userAdd action in the main method |
666 | 658 |
*/ |
667 |
private static void handleUserAdd(AuthFile authFile,String[]argus) { |
|
659 |
private static void handleUserAdd(AuthFile authFile,String[]argus) throws UnsupportedEncodingException{ |
|
660 |
boolean inputPassword = false; |
|
661 |
boolean passingHashedPassword = false; |
|
662 |
boolean hasDN = false; |
|
668 | 663 |
String I = "-i"; |
669 | 664 |
String H = "-h"; |
670 | 665 |
String DN = "-dn"; |
... | ... | |
673 | 668 |
String S = "-s"; |
674 | 669 |
String F = "-f"; |
675 | 670 |
String O= "-o"; |
671 |
Vector<String> possibleOptions = new <String>Vector(); |
|
672 |
possibleOptions.add(I); |
|
673 |
possibleOptions.add(H); |
|
674 |
possibleOptions.add(DN); |
|
675 |
possibleOptions.add(G); |
|
676 |
possibleOptions.add(E); |
|
677 |
possibleOptions.add(S); |
|
678 |
possibleOptions.add(F); |
|
679 |
possibleOptions.add(O); |
|
680 |
|
|
676 | 681 |
HashMap<String, String> map = new <String, String>HashMap(); |
682 |
for(int i=2; i<argus.length; i++) { |
|
683 |
String arg = argus[i]; |
|
684 |
|
|
685 |
if(map.containsKey(arg)) { |
|
686 |
System.out.println("Error: the command line for useradd can't have the duplicated options "+arg+"."); |
|
687 |
System.exit(1); |
|
688 |
} |
|
689 |
|
|
690 |
//this is the scenario that "-i" is at the end of the arguments. |
|
691 |
if(arg.equals(I) && i==argus.length-1) { |
|
692 |
map.put(I, I);//we need to input password. |
|
693 |
inputPassword = true; |
|
694 |
} |
|
695 |
|
|
696 |
if(possibleOptions.contains(arg) && i<argus.length-1) { |
|
697 |
//System.out.println("find the option "+arg); |
|
698 |
if(arg.equals(I)) { |
|
699 |
//this is the scenario that "-i" is NOT at the end of the arguments. |
|
700 |
if(!possibleOptions.contains(argus[i+1])) { |
|
701 |
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."); |
|
702 |
System.exit(1); |
|
703 |
} |
|
704 |
map.put(I, I);//we need to input password. |
|
705 |
inputPassword = true; |
|
706 |
} else { |
|
707 |
if(arg.equals(H)) { |
|
708 |
passingHashedPassword = true; |
|
709 |
} else if (arg.equals(DN)) { |
|
710 |
hasDN = true; |
|
711 |
} |
|
712 |
map.put(arg, argus[i+1]); |
|
713 |
} |
|
714 |
|
|
715 |
} else if(!possibleOptions.contains(arg)) { |
|
716 |
//check if the previous argument is an option |
|
717 |
if(!possibleOptions.contains(argus[i-1])) { |
|
718 |
System.out.println("Error: an illegal argument "+arg+" in the useradd command "); |
|
719 |
System.exit(1); |
|
720 |
} |
|
721 |
} |
|
722 |
} |
|
677 | 723 |
|
724 |
if(!hasDN) { |
|
725 |
System.out.println("The \"-dn user-distinguish-name\" is requried in the useradd command ."); |
|
726 |
System.exit(1); |
|
727 |
} |
|
728 |
|
|
729 |
String plainPassword = null; |
|
730 |
if(inputPassword && passingHashedPassword) { |
|
731 |
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."); |
|
732 |
System.exit(1); |
|
733 |
} else if (!inputPassword && !passingHashedPassword) { |
|
734 |
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."); |
|
735 |
System.exit(1); |
|
736 |
} else if(inputPassword) { |
|
737 |
plainPassword = inputPassword(); |
|
738 |
} |
|
739 |
//System.out.println("============the plain password is "+plainPassword); |
|
678 | 740 |
} |
679 | 741 |
|
742 |
|
|
680 | 743 |
/* |
744 |
* Input the password |
|
745 |
*/ |
|
746 |
private static String inputPassword() throws UnsupportedEncodingException { |
|
747 |
String password = null; |
|
748 |
String quit = "q"; |
|
749 |
Console console = System.console(); |
|
750 |
if (console == null) { |
|
751 |
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. "); |
|
752 |
System.exit(1); |
|
753 |
} |
|
754 |
|
|
755 |
while(true) { |
|
756 |
System.out.print("Enter your password(input 'q' to quite): "); |
|
757 |
String password1 = new String(console.readPassword()); |
|
758 |
if(password1== null || password1.trim().equals("")) { |
|
759 |
System.out.println("Eorror: the password can't be blank or null. Please try again."); |
|
760 |
continue; |
|
761 |
} else if (password1.equals(quit)) { |
|
762 |
System.exit(0); |
|
763 |
} |
|
764 |
System.out.print("Confirm your password(input 'q' to quite): "); |
|
765 |
String password2 = new String(console.readPassword()); |
|
766 |
if(password2 == null || password2.trim().equals("")) { |
|
767 |
System.out.println("Eorror: the password can't be blank or null. Please try again."); |
|
768 |
continue; |
|
769 |
} else if (password2.equals(quit)) { |
|
770 |
System.exit(0); |
|
771 |
} |
|
772 |
|
|
773 |
if(!password1.equals(password2)) { |
|
774 |
System.out.println("Eorror: The second passwords does't match the first one. Please try again."); |
|
775 |
} else { |
|
776 |
password = password1; |
|
777 |
break; |
|
778 |
} |
|
779 |
|
|
780 |
|
|
781 |
} |
|
782 |
|
|
783 |
return password; |
|
784 |
|
|
785 |
} |
|
786 |
/* |
|
681 | 787 |
* Print out the usage statement |
682 | 788 |
*/ |
683 | 789 |
private static void printUsage() { |
Also available in: Unified diff
Add code to handle useradd command.