001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.ssh;
020:
021: import com.jcraft.jsch.UserInfo;
022: import com.jcraft.jsch.UIKeyboardInteractive;
023:
024: /**
025: * Class containing information on an SSH user.
026: */
027: public class SSHUserInfo implements UserInfo, UIKeyboardInteractive {
028:
029: private String name;
030: private String password = null;
031: private String keyfile;
032: private String passphrase = null;
033: private boolean trustAllCertificates;
034:
035: /** Constructor for SSHUserInfo. */
036: public SSHUserInfo() {
037: super ();
038: this .trustAllCertificates = false;
039: }
040:
041: /**
042: * Constructor for SSHUserInfo.
043: * @param password the user's password
044: * @param trustAllCertificates if true trust hosts whose identity is unknown
045: */
046: public SSHUserInfo(String password, boolean trustAllCertificates) {
047: super ();
048: this .password = password;
049: this .trustAllCertificates = trustAllCertificates;
050: }
051:
052: /**
053: * Gets the user name.
054: * @return the user name
055: */
056: public String getName() {
057: return name;
058: }
059:
060: /**
061: * Gets the pass phrase of the user.
062: * @param message a message
063: * @return the passphrase
064: */
065: public String getPassphrase(String message) {
066: return passphrase;
067: }
068:
069: /**
070: * Gets the user's password.
071: * @return the user's password
072: */
073: public String getPassword() {
074: return password;
075: }
076:
077: /**
078: * Prompts a string.
079: * @param str the string
080: * @return whether the string was prompted
081: */
082: public boolean prompt(String str) {
083: return false;
084: }
085:
086: /**
087: * Indicates whether a retry was done.
088: * @return whether a retry was done
089: */
090: public boolean retry() {
091: return false;
092: }
093:
094: /**
095: * Sets the name.
096: * @param name The name to set
097: */
098: public void setName(String name) {
099: this .name = name;
100: }
101:
102: /**
103: * Sets the passphrase.
104: * @param passphrase The passphrase to set
105: */
106: public void setPassphrase(String passphrase) {
107: this .passphrase = passphrase;
108: }
109:
110: /**
111: * Sets the password.
112: * @param password The password to set
113: */
114: public void setPassword(String password) {
115: this .password = password;
116: }
117:
118: /**
119: * Sets the trust.
120: * @param trust whether to trust or not.
121: */
122: public void setTrust(boolean trust) {
123: this .trustAllCertificates = trust;
124: }
125:
126: /**
127: * @return whether to trust or not.
128: */
129: public boolean getTrust() {
130: return this .trustAllCertificates;
131: }
132:
133: /**
134: * Returns the passphrase.
135: * @return String
136: */
137: public String getPassphrase() {
138: return passphrase;
139: }
140:
141: /**
142: * Returns the keyfile.
143: * @return String
144: */
145: public String getKeyfile() {
146: return keyfile;
147: }
148:
149: /**
150: * Sets the keyfile.
151: * @param keyfile The keyfile to set
152: */
153: public void setKeyfile(String keyfile) {
154: this .keyfile = keyfile;
155: }
156:
157: /**
158: * Implement the UserInfo interface.
159: * @param message ignored
160: * @return true always
161: */
162: public boolean promptPassphrase(String message) {
163: return true;
164: }
165:
166: /**
167: * Implement the UserInfo interface.
168: * @param passwordPrompt ignored
169: * @return true the first time this is called, false otherwise
170: */
171: public boolean promptPassword(String passwordPrompt) {
172: return true;
173: }
174:
175: /**
176: * Implement the UserInfo interface.
177: * @param message ignored
178: * @return the value of trustAllCertificates
179: */
180: public boolean promptYesNo(String message) {
181: return trustAllCertificates;
182: }
183:
184: //why do we do nothing?
185: /**
186: * Implement the UserInfo interface (noop).
187: * @param message ignored
188: */
189: public void showMessage(String message) {
190: //log(message, Project.MSG_DEBUG);
191: }
192:
193: /**
194: * Implementation of UIKeyboardInteractive#promptKeyboardInteractive.
195: * @param destination not used.
196: * @param name not used.
197: * @param instruction not used.
198: * @param prompt the method checks if this is one in length.
199: * @param echo the method checks if the first element is false.
200: * @return the password in an size one array if there is a password
201: * and if the prompt and echo checks pass.
202: */
203: public String[] promptKeyboardInteractive(String destination,
204: String name, String instruction, String[] prompt,
205: boolean[] echo) {
206: if (prompt.length != 1 || echo[0] || this .password == null) {
207: return null;
208: }
209: String[] response = new String[1];
210: response[0] = this.password;
211: return response;
212: }
213:
214: }
|