1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  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. */
  17. package org.apache.tools.ant.taskdefs.optional.ssh;
  18. import com.jcraft.jsch.JSchException;
  19. import com.jcraft.jsch.Session;
  20. import com.jcraft.jsch.JSch;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.Project;
  24. /**
  25. * Base class for Ant tasks using jsch.
  26. *
  27. * @since Ant 1.6
  28. */
  29. public abstract class SSHBase extends Task implements LogListener {
  30. /** Default listen port for SSH daemon */
  31. private static final int SSH_PORT = 22;
  32. private String host;
  33. private String keyfile;
  34. private String knownHosts;
  35. private int port = SSH_PORT;
  36. private boolean failOnError = true;
  37. private boolean verbose;
  38. private SSHUserInfo userInfo;
  39. /**
  40. * Constructor for SSHBase.
  41. */
  42. public SSHBase() {
  43. super();
  44. userInfo = new SSHUserInfo();
  45. }
  46. /**
  47. * Remote host, either DNS name or IP.
  48. *
  49. * @param host The new host value
  50. */
  51. public void setHost(String host) {
  52. this.host = host;
  53. }
  54. public String getHost() {
  55. return host;
  56. }
  57. public void setFailonerror(boolean failure) {
  58. failOnError = failure;
  59. }
  60. public boolean getFailonerror() {
  61. return failOnError;
  62. }
  63. /**
  64. * @since Ant 1.6.2
  65. */
  66. public void setVerbose(boolean failure) {
  67. verbose = failure;
  68. }
  69. /**
  70. * @since Ant 1.6.2
  71. */
  72. public boolean getVerbose() {
  73. return verbose;
  74. }
  75. /**
  76. * Username known to remote host.
  77. *
  78. * @param username The new username value
  79. */
  80. public void setUsername(String username) {
  81. userInfo.setName(username);
  82. }
  83. /**
  84. * Sets the password for the user.
  85. *
  86. * @param password The new password value
  87. */
  88. public void setPassword(String password) {
  89. userInfo.setPassword(password);
  90. }
  91. /**
  92. * Sets the keyfile for the user.
  93. *
  94. * @param keyfile The new keyfile value
  95. */
  96. public void setKeyfile(String keyfile) {
  97. userInfo.setKeyfile(keyfile);
  98. }
  99. /**
  100. * Sets the passphrase for the users key.
  101. *
  102. * @param passphrase The new passphrase value
  103. */
  104. public void setPassphrase(String passphrase) {
  105. userInfo.setPassphrase(passphrase);
  106. }
  107. /**
  108. * Sets the path to the file that has the identities of
  109. * all known hosts. This is used by SSH protocol to validate
  110. * the identity of the host. The default is
  111. * <i>${user.home}/.ssh/known_hosts</i>.
  112. *
  113. * @param knownHosts a path to the known hosts file.
  114. */
  115. public void setKnownhosts(String knownHosts) {
  116. this.knownHosts = knownHosts;
  117. }
  118. /**
  119. * Setting this to true trusts hosts whose identity is unknown.
  120. *
  121. * @param yesOrNo if true trust the identity of unknown hosts.
  122. */
  123. public void setTrust(boolean yesOrNo) {
  124. userInfo.setTrust(yesOrNo);
  125. }
  126. /**
  127. * Changes the port used to connect to the remote host.
  128. *
  129. * @param port port number of remote host.
  130. */
  131. public void setPort(int port) {
  132. this.port = port;
  133. }
  134. public int getPort() {
  135. return port;
  136. }
  137. public void init() throws BuildException {
  138. super.init();
  139. this.knownHosts = System.getProperty("user.home") + "/.ssh/known_hosts";
  140. this.port = SSH_PORT;
  141. }
  142. protected Session openSession() throws JSchException {
  143. JSch jsch = new JSch();
  144. if (null != userInfo.getKeyfile()) {
  145. jsch.addIdentity(userInfo.getKeyfile());
  146. }
  147. if (!userInfo.getTrust() && knownHosts != null) {
  148. log("Using known hosts: " + knownHosts, Project.MSG_DEBUG);
  149. jsch.setKnownHosts(knownHosts);
  150. }
  151. Session session = jsch.getSession(userInfo.getName(), host, port);
  152. session.setUserInfo(userInfo);
  153. log("Connecting to " + host + ":" + port);
  154. session.connect();
  155. return session;
  156. }
  157. protected SSHUserInfo getUserInfo() {
  158. return userInfo;
  159. }
  160. }