Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sha2 = "0.6.0"
nix = "0.9.0"
dbus = { version = "0.5.4", optional = true }
version-compare = "0.0.6"
regex = "0.2.2"

[lib]
name = "specinfra"
Expand Down
68 changes: 50 additions & 18 deletions src/backend/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ pub extern crate ssh2;

use libc::c_char;
use std::ffi::CStr;
use regex::Regex;

use std::result::Result;
use std::str;
use std::net::TcpStream;
use std::env;
use std::io::prelude::*;
use std::path::Path;

use backend;
use backend::error::Error;
Expand All @@ -24,28 +24,66 @@ pub struct SSH {
_tcp: TcpStream,
}

#[repr(C)]
pub struct SSHInterface {
hostname: *const c_char,
user: *const c_char,
password: *const c_char,
key_file: *const c_char,
}

pub struct SSHBuilder {
hostname: Option<String>,
hostname: String,
user: String,
password: String,
key_file: String,
}

impl SSHBuilder {
pub fn new() -> Self {
SSHBuilder { hostname: None }
SSHBuilder {
hostname: "".to_string(),
user: "".to_string(),
password: "".to_string(),
key_file: "".to_string(),
}
}

pub fn hostname(mut self, h: &str) -> Self {
self.hostname = Some(h.to_string());
pub fn set_target(mut self, s: *const SSHInterface) -> Self {
unsafe {
let t = &*s;
self.hostname = CStr::from_ptr(&*t.hostname).to_string_lossy().into_owned();
self.user = CStr::from_ptr(&*t.user).to_string_lossy().into_owned();
self.password = CStr::from_ptr(&*t.password).to_string_lossy().into_owned();
self.key_file = CStr::from_ptr(&*t.key_file).to_string_lossy().into_owned();
}
self
}

pub fn finalize(self) -> Result<SSH, Error> {
let hostname = self.hostname.unwrap();
let remote_addr = hostname + ":22";
let hostname = self.hostname;
let re = Regex::new(r":\d+$").unwrap();
let remote_addr: String;
if re.is_match(&hostname) {
remote_addr = hostname;
} else {
remote_addr = hostname + ":22";
}

let user = self.user;
let password = self.password;
let key_file = self.key_file;
let tcp = try!(TcpStream::connect(remote_addr));
let mut session = ssh2::Session::new().unwrap();
try!(session.handshake(&tcp));
let user = try!(env::var("USER"));
try!(session.userauth_agent(&user));

if &password != "" {
try!(session.userauth_password(&user, &password));
} else if &key_file != "" {
try!(session.userauth_pubkey_file(&user, None, Path::new(&key_file), None));
} else {
try!(session.userauth_agent(&user));
}

let ssh = SSH {
session: session,
Expand Down Expand Up @@ -103,14 +141,8 @@ impl Backend for SSH {
use backend::BackendWrapper;

#[no_mangle]
pub extern "C" fn backend_ssh_new(host: *const c_char) -> *mut BackendWrapper {
let host = unsafe {
assert!(!host.is_null());
CStr::from_ptr(host)
};
let host_str = host.to_str().unwrap();

let s = SSHBuilder::new().hostname(host_str).finalize().unwrap();
pub extern "C" fn backend_ssh_new(s: *const SSHInterface) -> *mut BackendWrapper {
let s = SSHBuilder::new().set_target(s).finalize().unwrap();
let b = BackendWrapper { backend: Box::new(s) };
Box::into_raw(Box::new(b))
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate md5;
extern crate sha2;
extern crate nix;
extern crate version_compare;
extern crate regex;

use std::ffi::CStr;

Expand Down