]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SimpleUserInfo.java
Improve SSH support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / SimpleUserInfo.java
1 package org.argeo.slc.jsch;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PushbackInputStream;
6 import java.util.Arrays;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.slc.SlcException;
11
12 import com.jcraft.jsch.UserInfo;
13
14 public class SimpleUserInfo implements UserInfo {
15 private Boolean permissive = true;
16 private Boolean verbose = false;
17
18 private final static Log log = LogFactory.getLog(SimpleUserInfo.class);
19
20 private String password;
21 private char[] passwordSafe;
22 private String passphrase;
23 private char[] passphraseSafe;
24
25 public void setPassword(String password) {
26 this.password = password;
27 }
28
29 public void setPassphrase(String passphrase) {
30 this.passphrase = passphrase;
31 }
32
33 public String getPassphrase() {
34 if (passphraseSafe != null)
35 return new String(passphraseSafe);
36 return passphrase;
37 }
38
39 public String getPassword() {
40 if (passwordSafe != null)
41 return new String(passwordSafe);
42 return password;
43 }
44
45 public boolean promptPassphrase(String message) {
46 if (permissive)
47 return true;
48 else {
49 log.info(message);
50 passwordSafe = readPassword(System.in);
51 return passwordSafe != null;
52 }
53 }
54
55 public boolean promptPassword(String message) {
56 if (permissive)
57 return true;
58 else {
59 log.info(message);
60 passwordSafe = readPassword(System.in);
61 return passwordSafe != null;
62 }
63 }
64
65 public boolean promptYesNo(String message) {
66 String msg = message + " (y/n): ";
67 if (permissive) {
68 if (verbose)
69 log.info(msg + "y");
70 return true;
71 } else {
72 log.info(msg);
73 char c;
74 try {
75 c = (char) System.in.read();
76 } catch (IOException e) {
77 throw new SlcException("Cannot read stdin", e);
78 }
79 if (c == 'y')
80 return true;
81 else
82 return false;
83 }
84 }
85
86 public void showMessage(String message) {
87 log.info(message);
88 }
89
90 public void setPermissive(Boolean permissive) {
91 this.permissive = permissive;
92 }
93
94 public void setVerbose(Boolean verbose) {
95 this.verbose = verbose;
96 }
97
98 protected char[] readPassword(InputStream in) {
99
100 try {
101 char[] lineBuffer;
102 char[] buf;
103 // int i;
104
105 buf = lineBuffer = new char[128];
106
107 int room = buf.length;
108 int offset = 0;
109 int c;
110
111 loop: while (true) {
112 switch (c = in.read()) {
113 case -1:
114 case '\n':
115 break loop;
116
117 case '\r':
118 int c2 = in.read();
119 if ((c2 != '\n') && (c2 != -1)) {
120 if (!(in instanceof PushbackInputStream)) {
121 in = new PushbackInputStream(in);
122 }
123 ((PushbackInputStream) in).unread(c2);
124 } else
125 break loop;
126
127 default:
128 if (--room < 0) {
129 buf = new char[offset + 128];
130 room = buf.length - offset - 1;
131 System.arraycopy(lineBuffer, 0, buf, 0, offset);
132 Arrays.fill(lineBuffer, ' ');
133 lineBuffer = buf;
134 }
135 buf[offset++] = (char) c;
136 break;
137 }
138 }
139
140 if (offset == 0) {
141 return null;
142 }
143
144 char[] ret = new char[offset];
145 System.arraycopy(buf, 0, ret, 0, offset);
146 Arrays.fill(buf, ' ');
147
148 return ret;
149 } catch (IOException e) {
150 throw new SlcException("Cannot read password.", e);
151 }
152 }
153
154 }