1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ 2 /** 3 * This program will demonstrate the keyboard-interactive authentication. 4 * $ CLASSPATH=.:../build javac UserAuthKI.java 5 * $ CLASSPATH=.:../build java UserAuthKI 6 * If the remote sshd supports keyboard-interactive authentication, 7 * you will be prompted. 8 * 9 */ 10 import com.jcraft.jsch.*; 11 import java.awt.*; 12 import javax.swing.*; 13 14 public class UserAuthKI{ main(String[] arg)15 public static void main(String[] arg){ 16 17 try{ 18 JSch jsch=new JSch(); 19 20 String host=null; 21 if(arg.length>0){ 22 host=arg[0]; 23 } 24 else{ 25 host=JOptionPane.showInputDialog("Enter username@hostname", 26 System.getProperty("user.name")+ 27 "@localhost"); 28 } 29 String user=host.substring(0, host.indexOf('@')); 30 host=host.substring(host.indexOf('@')+1); 31 32 Session session=jsch.getSession(user, host, 22); 33 34 // username and passphrase will be given via UserInfo interface. 35 UserInfo ui=new MyUserInfo(); 36 session.setUserInfo(ui); 37 session.connect(); 38 39 Channel channel=session.openChannel("shell"); 40 41 channel.setInputStream(System.in); 42 channel.setOutputStream(System.out); 43 44 channel.connect(); 45 } 46 catch(Exception e){ 47 System.out.println(e); 48 } 49 } 50 51 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ getPassword()52 public String getPassword(){ return passwd; } promptYesNo(String str)53 public boolean promptYesNo(String str){ 54 Object[] options={ "yes", "no" }; 55 int foo=JOptionPane.showOptionDialog(null, 56 str, 57 "Warning", 58 JOptionPane.DEFAULT_OPTION, 59 JOptionPane.WARNING_MESSAGE, 60 null, options, options[0]); 61 return foo==0; 62 } 63 64 String passwd; 65 JTextField passwordField=(JTextField)new JPasswordField(20); 66 getPassphrase()67 public String getPassphrase(){ return null; } promptPassphrase(String message)68 public boolean promptPassphrase(String message){ return false; } promptPassword(String message)69 public boolean promptPassword(String message){ 70 Object[] ob={passwordField}; 71 int result= 72 JOptionPane.showConfirmDialog(null, ob, message, 73 JOptionPane.OK_CANCEL_OPTION); 74 if(result==JOptionPane.OK_OPTION){ 75 passwd=passwordField.getText(); 76 return true; 77 } 78 else{ 79 return false; 80 } 81 } showMessage(String message)82 public void showMessage(String message){ 83 JOptionPane.showMessageDialog(null, message); 84 } 85 86 final GridBagConstraints gbc = 87 new GridBagConstraints(0,0,1,1,1,1, 88 GridBagConstraints.NORTHWEST, 89 GridBagConstraints.NONE, 90 new Insets(0,0,0,0),0,0); 91 private Container panel; promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo)92 public String[] promptKeyboardInteractive(String destination, 93 String name, 94 String instruction, 95 String[] prompt, 96 boolean[] echo){ 97 /* 98 //System.out.println("promptKeyboardInteractive"); 99 System.out.println("destination: "+destination); 100 System.out.println("name: "+name); 101 System.out.println("instruction: "+instruction); 102 System.out.println("prompt.length: "+prompt.length); 103 System.out.println("prompt: "+prompt[0]); 104 */ 105 panel = new JPanel(); 106 panel.setLayout(new GridBagLayout()); 107 108 gbc.weightx = 1.0; 109 gbc.gridwidth = GridBagConstraints.REMAINDER; 110 gbc.gridx = 0; 111 panel.add(new JLabel(instruction), gbc); 112 gbc.gridy++; 113 114 gbc.gridwidth = GridBagConstraints.RELATIVE; 115 116 JTextField[] texts=new JTextField[prompt.length]; 117 for(int i=0; i<prompt.length; i++){ 118 gbc.fill = GridBagConstraints.NONE; 119 gbc.gridx = 0; 120 gbc.weightx = 1; 121 panel.add(new JLabel(prompt[i]),gbc); 122 123 gbc.gridx = 1; 124 gbc.fill = GridBagConstraints.HORIZONTAL; 125 gbc.weighty = 1; 126 if(echo[i]){ 127 texts[i]=new JTextField(20); 128 } 129 else{ 130 texts[i]=new JPasswordField(20); 131 } 132 panel.add(texts[i], gbc); 133 gbc.gridy++; 134 } 135 136 if(JOptionPane.showConfirmDialog(null, panel, 137 destination+": "+name, 138 JOptionPane.OK_CANCEL_OPTION, 139 JOptionPane.QUESTION_MESSAGE) 140 ==JOptionPane.OK_OPTION){ 141 String[] response=new String[prompt.length]; 142 for(int i=0; i<prompt.length; i++){ 143 response[i]=texts[i].getText(); 144 } 145 return response; 146 } 147 else{ 148 return null; // cancel 149 } 150 } 151 } 152 } 153