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.JSchException;
022: import com.jcraft.jsch.Session;
023: import com.jcraft.jsch.JSch;
024:
025: import org.apache.tools.ant.Task;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.Project;
028:
029: /**
030: * Base class for Ant tasks using jsch.
031: *
032: * @since Ant 1.6
033: */
034: public abstract class SSHBase extends Task implements LogListener {
035:
036: /** Default listen port for SSH daemon */
037: private static final int SSH_PORT = 22;
038:
039: private String host;
040: private String knownHosts;
041: private int port = SSH_PORT;
042: private boolean failOnError = true;
043: private boolean verbose;
044: private SSHUserInfo userInfo;
045:
046: /**
047: * Constructor for SSHBase.
048: */
049: public SSHBase() {
050: super ();
051: userInfo = new SSHUserInfo();
052: }
053:
054: /**
055: * Remote host, either DNS name or IP.
056: *
057: * @param host The new host value
058: */
059: public void setHost(String host) {
060: this .host = host;
061: }
062:
063: /**
064: * Get the host.
065: * @return the host
066: */
067: public String getHost() {
068: return host;
069: }
070:
071: /**
072: * Set the failonerror flag.
073: * Default is true
074: * @param failure if true throw a build exception when a failure occuries,
075: * otherwise just log the failure and continue
076: */
077: public void setFailonerror(boolean failure) {
078: failOnError = failure;
079: }
080:
081: /**
082: * Get the failonerror flag.
083: * @return the failonerror flag
084: */
085: public boolean getFailonerror() {
086: return failOnError;
087: }
088:
089: /**
090: * Set the verbose flag.
091: * @param verbose if true output more verbose logging
092: * @since Ant 1.6.2
093: */
094: public void setVerbose(boolean verbose) {
095: this .verbose = verbose;
096: }
097:
098: /**
099: * Get the verbose flag.
100: * @return the verbose flag
101: * @since Ant 1.6.2
102: */
103: public boolean getVerbose() {
104: return verbose;
105: }
106:
107: /**
108: * Username known to remote host.
109: *
110: * @param username The new username value
111: */
112: public void setUsername(String username) {
113: userInfo.setName(username);
114: }
115:
116: /**
117: * Sets the password for the user.
118: *
119: * @param password The new password value
120: */
121: public void setPassword(String password) {
122: userInfo.setPassword(password);
123: }
124:
125: /**
126: * Sets the keyfile for the user.
127: *
128: * @param keyfile The new keyfile value
129: */
130: public void setKeyfile(String keyfile) {
131: userInfo.setKeyfile(keyfile);
132: }
133:
134: /**
135: * Sets the passphrase for the users key.
136: *
137: * @param passphrase The new passphrase value
138: */
139: public void setPassphrase(String passphrase) {
140: userInfo.setPassphrase(passphrase);
141: }
142:
143: /**
144: * Sets the path to the file that has the identities of
145: * all known hosts. This is used by SSH protocol to validate
146: * the identity of the host. The default is
147: * <i>${user.home}/.ssh/known_hosts</i>.
148: *
149: * @param knownHosts a path to the known hosts file.
150: */
151: public void setKnownhosts(String knownHosts) {
152: this .knownHosts = knownHosts;
153: }
154:
155: /**
156: * Setting this to true trusts hosts whose identity is unknown.
157: *
158: * @param yesOrNo if true trust the identity of unknown hosts.
159: */
160: public void setTrust(boolean yesOrNo) {
161: userInfo.setTrust(yesOrNo);
162: }
163:
164: /**
165: * Changes the port used to connect to the remote host.
166: *
167: * @param port port number of remote host.
168: */
169: public void setPort(int port) {
170: this .port = port;
171: }
172:
173: /**
174: * Get the port attribute.
175: * @return the port
176: */
177: public int getPort() {
178: return port;
179: }
180:
181: /**
182: * Initialize the task.
183: * This initializizs the known hosts and sets the default port.
184: * @throws BuildException on error
185: */
186: public void init() throws BuildException {
187: super .init();
188: this .knownHosts = System.getProperty("user.home")
189: + "/.ssh/known_hosts";
190: this .port = SSH_PORT;
191: }
192:
193: /**
194: * Open an ssh seession.
195: * @return the opened session
196: * @throws JSchException on error
197: */
198: protected Session openSession() throws JSchException {
199: JSch jsch = new JSch();
200: if (null != userInfo.getKeyfile()) {
201: jsch.addIdentity(userInfo.getKeyfile());
202: }
203:
204: if (!userInfo.getTrust() && knownHosts != null) {
205: log("Using known hosts: " + knownHosts, Project.MSG_DEBUG);
206: jsch.setKnownHosts(knownHosts);
207: }
208:
209: Session session = jsch.getSession(userInfo.getName(), host,
210: port);
211: session.setUserInfo(userInfo);
212: log("Connecting to " + host + ":" + port);
213: session.connect();
214: return session;
215: }
216:
217: /**
218: * Get the user information.
219: * @return the user information
220: */
221: protected SSHUserInfo getUserInfo() {
222: return userInfo;
223: }
224: }
|