]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SimpleUserInfo.java
Update headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / SimpleUserInfo.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.jsch;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.PushbackInputStream;
21 import java.util.Arrays;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.slc.SlcException;
26
27 import com.jcraft.jsch.UserInfo;
28
29 public class SimpleUserInfo implements UserInfo {
30 private Boolean permissive = true;
31 private Boolean verbose = false;
32
33 private final static Log log = LogFactory.getLog(SimpleUserInfo.class);
34
35 protected String password;
36 protected char[] passwordSafe;
37 protected String passphrase;
38 protected char[] passphraseSafe;
39
40 public void reset() {
41 if (passwordSafe != null)
42 Arrays.fill(passwordSafe, (char) 0);
43 passwordSafe = null;
44 if (passphraseSafe != null)
45 Arrays.fill(passphraseSafe, (char) 0);
46 passphraseSafe = null;
47 }
48
49 public void setPassword(String password) {
50 this.password = password;
51 }
52
53 public void setPassphrase(String passphrase) {
54 this.passphrase = passphrase;
55 }
56
57 public String getPassphrase() {
58 if (passphraseSafe != null)
59 return new String(passphraseSafe);
60 return passphrase;
61 }
62
63 public String getPassword() {
64 if (passwordSafe != null)
65 return new String(passwordSafe);
66 return password;
67 }
68
69 public boolean promptPassphrase(String message) {
70 if (permissive)
71 return true;
72 else {
73 log.info(message);
74 passwordSafe = readPassword(System.in);
75 return passwordSafe != null;
76 }
77 }
78
79 public boolean promptPassword(String message) {
80 if (permissive)
81 return true;
82 else {
83 log.info(message);
84 passwordSafe = readPassword(System.in);
85 return passwordSafe != null;
86 }
87 }
88
89 public boolean promptYesNo(String message) {
90 String msg = message + " (y/n): ";
91 if (permissive) {
92 if (verbose)
93 log.info(msg + "y");
94 return true;
95 } else {
96 log.info(msg);
97 char c;
98 try {
99 c = (char) System.in.read();
100 } catch (IOException e) {
101 throw new SlcException("Cannot read stdin", e);
102 }
103 if (c == 'y')
104 return true;
105 else
106 return false;
107 }
108 }
109
110 public void showMessage(String message) {
111 log.info(message);
112 }
113
114 public void setPermissive(Boolean permissive) {
115 this.permissive = permissive;
116 }
117
118 public void setVerbose(Boolean verbose) {
119 this.verbose = verbose;
120 }
121
122 protected char[] readPassword(InputStream in) {
123
124 try {
125 char[] lineBuffer;
126 char[] buf;
127 // int i;
128
129 buf = lineBuffer = new char[128];
130
131 int room = buf.length;
132 int offset = 0;
133 int c;
134
135 loop: while (true) {
136 switch (c = in.read()) {
137 case -1:
138 case '\n':
139 break loop;
140
141 case '\r':
142 int c2 = in.read();
143 if ((c2 != '\n') && (c2 != -1)) {
144 if (!(in instanceof PushbackInputStream)) {
145 in = new PushbackInputStream(in);
146 }
147 ((PushbackInputStream) in).unread(c2);
148 } else
149 break loop;
150
151 default:
152 if (--room < 0) {
153 buf = new char[offset + 128];
154 room = buf.length - offset - 1;
155 System.arraycopy(lineBuffer, 0, buf, 0, offset);
156 Arrays.fill(lineBuffer, ' ');
157 lineBuffer = buf;
158 }
159 buf[offset++] = (char) c;
160 break;
161 }
162 }
163
164 if (offset == 0) {
165 return null;
166 }
167
168 char[] ret = new char[offset];
169 System.arraycopy(buf, 0, ret, 0, offset);
170 Arrays.fill(buf, ' ');
171
172 return ret;
173 } catch (IOException e) {
174 throw new SlcException("Cannot read password.", e);
175 }
176 }
177
178 }