use of com.jcraft.jsch.ConfigRepository in project ant-ivy by apache.
the class AbstractSshBasedRepository method getSession.
/**
* get a new session using the default attributes if the given String is a full uri, use the
* data from the uri instead
*
* @param pathOrUri
* might be just a path or a full ssh or sftp uri
* @return matching Session
* @throws IOException if something goes wrong
*/
protected Session getSession(String pathOrUri) throws IOException {
URI uri = parseURI(pathOrUri);
String host = getHost();
int port = getPort();
String user = getUser();
String userPassword = getUserPassword();
String sshConfig = getSshConfig();
File keyFile = getKeyFile();
if (uri != null && uri.getScheme() != null) {
if (uri.getHost() != null) {
host = uri.getHost();
}
if (uri.getPort() != -1) {
port = uri.getPort();
}
if (uri.getUserInfo() != null) {
String userInfo = uri.getUserInfo();
if (!userInfo.contains(":")) {
user = userInfo;
} else {
user = userInfo.substring(0, userInfo.indexOf(":"));
userPassword = userInfo.substring(userInfo.indexOf(":") + 1);
}
}
}
if (sshConfig != null) {
ConfigRepository configRepository = OpenSSHConfig.parseFile(sshConfig);
Config config = configRepository.getConfig(host);
host = config.getHostname();
if (user == null) {
user = config.getUser();
}
String keyFilePath = config.getValue("IdentityFile");
if (keyFilePath != null && keyFile == null) {
keyFile = new File(keyFilePath);
}
}
if (host == null) {
throw new IllegalArgumentException("missing host information. host should be provided either " + "directly on the repository or in the connection URI " + ", or in the openssh config file specified by sshConfig");
}
if (user == null) {
Credentials c = requestCredentials(host);
if (c != null) {
user = c.getUserName();
userPassword = c.getPasswd();
} else {
Message.error("username is not set");
}
}
return SshCache.getInstance().getSession(host, port, user, userPassword, keyFile, getKeyFilePassword(), getPassFile(), isAllowedAgentUse());
}
use of com.jcraft.jsch.ConfigRepository in project tigervnc by TigerVNC.
the class Tunnel method createTunnelJSch.
private static void createTunnelJSch(String gatewayHost, String remoteHost, int remotePort, int localPort) throws Exception {
JSch.setLogger(new MyJSchLogger());
JSch jsch = new JSch();
try {
// NOTE: jsch does not support all ciphers. User may be
// prompted to accept host key authenticy even if
// the key is in the known_hosts file.
File knownHosts = new File(FileUtils.getHomeDir() + ".ssh" + FileUtils.getFileSeparator() + "known_hosts");
if (knownHosts.exists() && knownHosts.canRead())
jsch.setKnownHosts(knownHosts.getAbsolutePath());
ArrayList<File> privateKeys = new ArrayList<File>();
if (!getSshKey().isEmpty()) {
byte[] keyPass = null, key;
if (!sshKeyPass.getValue().isEmpty())
keyPass = sshKeyPass.getValue().getBytes();
jsch.addIdentity("TigerVNC", getSshKey().getBytes(), null, keyPass);
} else if (!getSshKeyFile().isEmpty()) {
File f = new File(getSshKeyFile());
if (!f.exists() || !f.canRead())
throw new Exception("Cannot access SSH key file " + getSshKeyFile());
privateKeys.add(f);
}
for (Iterator<File> i = privateKeys.iterator(); i.hasNext(); ) {
File privateKey = (File) i.next();
if (privateKey.exists() && privateKey.canRead())
if (!sshKeyPass.getValue().isEmpty())
jsch.addIdentity(privateKey.getAbsolutePath(), sshKeyPass.getValue());
else
jsch.addIdentity(privateKey.getAbsolutePath());
}
String user = getSshUser();
String label = new String("SSH Authentication");
PasswdDialog dlg = new PasswdDialog(label, (user == null ? false : true), false);
dlg.userEntry.setText(user != null ? user : "");
File ssh_config = new File(sshConfig.getValue());
if (ssh_config.exists() && ssh_config.canRead()) {
ConfigRepository repo = OpenSSHConfig.parse(ssh_config.getAbsolutePath());
jsch.setConfigRepository(repo);
}
Session session = jsch.getSession(user, gatewayHost, getSshPort());
session.setUserInfo(dlg);
// OpenSSHConfig doesn't recognize StrictHostKeyChecking
if (session.getConfig("StrictHostKeyChecking") == null)
session.setConfig("StrictHostKeyChecking", "ask");
session.connect();
if (gatewayHost.equals(remoteHost))
session.setPortForwardingL(localPort, new String("localhost"), remotePort);
else
session.setPortForwardingL(localPort, remoteHost, remotePort);
} catch (java.lang.Exception e) {
throw new Exception(e.getMessage());
}
}
Aggregations