]> 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 final static Log log = LogFactory.getLog(SimpleUserInfo.class);
17
18 private String password;
19 private char[] passwordSafe;
20
21 public void setPassword(String password) {
22 this.password = password;
23 }
24
25 public String getPassphrase() {
26 return null;
27 }
28
29 public String getPassword() {
30 if (passwordSafe != null)
31 return new String(passwordSafe);
32 return password;
33 }
34
35 public boolean promptPassphrase(String message) {
36 return true;
37 }
38
39 public boolean promptPassword(String message) {
40 log.info(message);
41 if (permissive)
42 return true;
43 else {
44 passwordSafe = readPassword(System.in);
45 return passwordSafe != null;
46 }
47 }
48
49 public boolean promptYesNo(String message) {
50 String msg = message + " (y/n): ";
51 if (permissive) {
52 log.info(msg + "y");
53 return true;
54 } else {
55 log.info(msg);
56 char c;
57 try {
58 c = (char) System.in.read();
59 } catch (IOException e) {
60 throw new SlcException("Cannot read stdin", e);
61 }
62 if (c == 'y')
63 return true;
64 else
65 return false;
66 }
67 }
68
69 public void showMessage(String message) {
70 log.info(message);
71 }
72
73 public void setPermissive(Boolean permissive) {
74 this.permissive = permissive;
75 }
76
77 protected char[] readPassword(InputStream in) {
78
79 try {
80 char[] lineBuffer;
81 char[] buf;
82 //int i;
83
84 buf = lineBuffer = new char[128];
85
86 int room = buf.length;
87 int offset = 0;
88 int c;
89
90 loop: while (true) {
91 switch (c = in.read()) {
92 case -1:
93 case '\n':
94 break loop;
95
96 case '\r':
97 int c2 = in.read();
98 if ((c2 != '\n') && (c2 != -1)) {
99 if (!(in instanceof PushbackInputStream)) {
100 in = new PushbackInputStream(in);
101 }
102 ((PushbackInputStream) in).unread(c2);
103 } else
104 break loop;
105
106 default:
107 if (--room < 0) {
108 buf = new char[offset + 128];
109 room = buf.length - offset - 1;
110 System.arraycopy(lineBuffer, 0, buf, 0, offset);
111 Arrays.fill(lineBuffer, ' ');
112 lineBuffer = buf;
113 }
114 buf[offset++] = (char) c;
115 break;
116 }
117 }
118
119 if (offset == 0) {
120 return null;
121 }
122
123 char[] ret = new char[offset];
124 System.arraycopy(buf, 0, ret, 0, offset);
125 Arrays.fill(buf, ' ');
126
127 return ret;
128 } catch (IOException e) {
129 throw new SlcException("Cannot read password.", e);
130 }
131 }
132
133 }