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