summaryrefslogtreecommitdiff
path: root/src/knownhosts.rs
diff options
context:
space:
mode:
authorWez Furlong <wez@wezfurlong.org>2019-07-23 16:39:28 -0700
committerWez Furlong <wez@wezfurlong.org>2019-07-29 08:55:06 -0700
commit18dc0f59e0b29c50da525dbe85d36fb5cb0ecf62 (patch)
tree5f424e77c3bc053a39aea417fea6fe75b963f1d9 /src/knownhosts.rs
parent78d2e3d9dbf1530cdf4bc152b91f0ddd7ce4895b (diff)
downloadssh2-rs-18dc0f59e0b29c50da525dbe85d36fb5cb0ecf62.zip
cargo fmt
No functional changes, just formatting
Diffstat (limited to 'src/knownhosts.rs')
-rw-r--r--src/knownhosts.rs159
1 files changed, 83 insertions, 76 deletions
diff --git a/src/knownhosts.rs b/src/knownhosts.rs
index d5d93c5..737ff52 100644
--- a/src/knownhosts.rs
+++ b/src/knownhosts.rs
@@ -1,11 +1,11 @@
+use libc::{c_int, size_t};
use std::ffi::CString;
use std::marker;
use std::path::Path;
use std::str;
-use libc::{c_int, size_t};
-use {raw, Session, Error, KnownHostFileKind, CheckResult};
use util::{self, Binding, SessionBinding};
+use {raw, CheckResult, Error, KnownHostFileKind, Session};
/// A set of known hosts which can be used to verify the identity of a remote
/// server.
@@ -63,62 +63,57 @@ pub struct Host<'kh> {
impl<'sess> KnownHosts<'sess> {
/// Reads a collection of known hosts from a specified file and adds them to
/// the collection of known hosts.
- pub fn read_file(&mut self, file: &Path, kind: KnownHostFileKind)
- -> Result<u32, Error> {
+ pub fn read_file(&mut self, file: &Path, kind: KnownHostFileKind) -> Result<u32, Error> {
let file = try!(CString::new(try!(util::path2bytes(file))));
- let n = unsafe {
- raw::libssh2_knownhost_readfile(self.raw, file.as_ptr(),
- kind as c_int)
- };
- if n < 0 { try!(self.sess.rc(n)) }
+ let n = unsafe { raw::libssh2_knownhost_readfile(self.raw, file.as_ptr(), kind as c_int) };
+ if n < 0 {
+ try!(self.sess.rc(n))
+ }
Ok(n as u32)
}
/// Read a line as if it were from a known hosts file.
- pub fn read_str(&mut self, s: &str, kind: KnownHostFileKind)
- -> Result<(), Error> {
+ pub fn read_str(&mut self, s: &str, kind: KnownHostFileKind) -> Result<(), Error> {
self.sess.rc(unsafe {
- raw::libssh2_knownhost_readline(self.raw,
- s.as_ptr() as *const _,
- s.len() as size_t,
- kind as c_int)
+ raw::libssh2_knownhost_readline(
+ self.raw,
+ s.as_ptr() as *const _,
+ s.len() as size_t,
+ kind as c_int,
+ )
})
}
/// Writes all the known hosts to the specified file using the specified
/// file format.
- pub fn write_file(&self, file: &Path, kind: KnownHostFileKind)
- -> Result<(), Error> {
+ pub fn write_file(&self, file: &Path, kind: KnownHostFileKind) -> Result<(), Error> {
let file = try!(CString::new(try!(util::path2bytes(file))));
- let n = unsafe {
- raw::libssh2_knownhost_writefile(self.raw, file.as_ptr(),
- kind as c_int)
- };
+ let n = unsafe { raw::libssh2_knownhost_writefile(self.raw, file.as_ptr(), kind as c_int) };
self.sess.rc(n)
}
/// Converts a single known host to a single line of output for storage,
/// using the 'type' output format.
- pub fn write_string(&self, host: &Host, kind: KnownHostFileKind)
- -> Result<String, Error> {
+ pub fn write_string(&self, host: &Host, kind: KnownHostFileKind) -> Result<String, Error> {
let mut v = Vec::with_capacity(128);
loop {
let mut outlen = 0;
unsafe {
- let rc = raw::libssh2_knownhost_writeline(self.raw,
- host.raw,
- v.as_mut_ptr()
- as *mut _,
- v.capacity() as size_t,
- &mut outlen,
- kind as c_int);
+ let rc = raw::libssh2_knownhost_writeline(
+ self.raw,
+ host.raw,
+ v.as_mut_ptr() as *mut _,
+ v.capacity() as size_t,
+ &mut outlen,
+ kind as c_int,
+ );
if rc == raw::LIBSSH2_ERROR_BUFFER_TOO_SMALL {
// + 1 for the trailing zero
v.reserve(outlen as usize + 1);
} else {
try!(self.sess.rc(rc));
v.set_len(outlen as usize);
- break
+ break;
}
}
}
@@ -127,14 +122,16 @@ impl<'sess> KnownHosts<'sess> {
/// Create an iterator over all of the known hosts in this structure.
pub fn iter(&self) -> Hosts {
- Hosts { prev: 0 as *mut _, hosts: self }
+ Hosts {
+ prev: 0 as *mut _,
+ hosts: self,
+ }
}
/// Delete a known host entry from the collection of known hosts.
pub fn remove(&self, host: Host) -> Result<(), Error> {
- self.sess.rc(unsafe {
- raw::libssh2_knownhost_del(self.raw, host.raw)
- })
+ self.sess
+ .rc(unsafe { raw::libssh2_knownhost_del(self.raw, host.raw) })
}
/// Checks a host and its associated key against the collection of known
@@ -153,16 +150,17 @@ impl<'sess> KnownHosts<'sess> {
fn check_port_(&self, host: &str, port: i32, key: &[u8]) -> CheckResult {
let host = CString::new(host).unwrap();
- let flags = raw::LIBSSH2_KNOWNHOST_TYPE_PLAIN |
- raw::LIBSSH2_KNOWNHOST_KEYENC_RAW;
+ let flags = raw::LIBSSH2_KNOWNHOST_TYPE_PLAIN | raw::LIBSSH2_KNOWNHOST_KEYENC_RAW;
unsafe {
- let rc = raw::libssh2_knownhost_checkp(self.raw,
- host.as_ptr(),
- port as c_int,
- key.as_ptr() as *const _,
- key.len() as size_t,
- flags,
- 0 as *mut _);
+ let rc = raw::libssh2_knownhost_checkp(
+ self.raw,
+ host.as_ptr(),
+ port as c_int,
+ key.as_ptr() as *const _,
+ key.len() as size_t,
+ flags,
+ 0 as *mut _,
+ );
match rc {
raw::LIBSSH2_KNOWNHOST_CHECK_MATCH => CheckResult::Match,
raw::LIBSSH2_KNOWNHOST_CHECK_MISMATCH => CheckResult::Mismatch,
@@ -182,23 +180,28 @@ impl<'sess> KnownHosts<'sess> {
/// For example: "[host.example.com]:222".
///
/// The key provided must be the raw key for the host.
- pub fn add(&mut self, host: &str, key: &[u8], comment: &str,
- fmt: ::KnownHostKeyFormat)
- -> Result<(), Error> {
+ pub fn add(
+ &mut self,
+ host: &str,
+ key: &[u8],
+ comment: &str,
+ fmt: ::KnownHostKeyFormat,
+ ) -> Result<(), Error> {
let host = try!(CString::new(host));
- let flags = raw::LIBSSH2_KNOWNHOST_TYPE_PLAIN |
- raw::LIBSSH2_KNOWNHOST_KEYENC_RAW |
- (fmt as c_int);
+ let flags =
+ raw::LIBSSH2_KNOWNHOST_TYPE_PLAIN | raw::LIBSSH2_KNOWNHOST_KEYENC_RAW | (fmt as c_int);
unsafe {
- let rc = raw::libssh2_knownhost_addc(self.raw,
- host.as_ptr() as *mut _,
- 0 as *mut _,
- key.as_ptr() as *mut _,
- key.len() as size_t,
- comment.as_ptr() as *const _,
- comment.len() as size_t,
- flags,
- 0 as *mut _);
+ let rc = raw::libssh2_knownhost_addc(
+ self.raw,
+ host.as_ptr() as *mut _,
+ 0 as *mut _,
+ key.as_ptr() as *mut _,
+ key.len() as size_t,
+ comment.as_ptr() as *const _,
+ comment.len() as size_t,
+ flags,
+ 0 as *mut _,
+ );
self.sess.rc(rc)
}
}
@@ -207,17 +210,20 @@ impl<'sess> KnownHosts<'sess> {
impl<'sess> SessionBinding<'sess> for KnownHosts<'sess> {
type Raw = raw::LIBSSH2_KNOWNHOSTS;
- unsafe fn from_raw(sess: &'sess Session, raw: *mut raw::LIBSSH2_KNOWNHOSTS)
- -> KnownHosts<'sess> {
+ unsafe fn from_raw(
+ sess: &'sess Session,
+ raw: *mut raw::LIBSSH2_KNOWNHOSTS,
+ ) -> KnownHosts<'sess> {
KnownHosts {
raw: raw,
sess: sess,
}
}
- fn raw(&self) -> *mut raw::LIBSSH2_KNOWNHOSTS { self.raw }
+ fn raw(&self) -> *mut raw::LIBSSH2_KNOWNHOSTS {
+ self.raw
+ }
}
-
impl<'sess> Drop for KnownHosts<'sess> {
fn drop(&mut self) {
unsafe { raw::libssh2_knownhost_free(self.raw) }
@@ -229,10 +235,11 @@ impl<'kh> Iterator for Hosts<'kh> {
fn next(&mut self) -> Option<Result<Host<'kh>, Error>> {
unsafe {
let mut next = 0 as *mut _;
- match raw::libssh2_knownhost_get(self.hosts.raw,
- &mut next,
- self.prev) {
- 0 => { self.prev = next; Some(Ok(Binding::from_raw(next))) }
+ match raw::libssh2_knownhost_get(self.hosts.raw, &mut next, self.prev) {
+ 0 => {
+ self.prev = next;
+ Some(Ok(Binding::from_raw(next)))
+ }
1 => None,
rc => Some(Err(self.hosts.sess.rc(rc).err().unwrap())),
}
@@ -243,17 +250,12 @@ impl<'kh> Iterator for Hosts<'kh> {
impl<'kh> Host<'kh> {
/// This is `None` if no plain text host name exists.
pub fn name(&self) -> Option<&str> {
- unsafe {
- ::opt_bytes(self, (*self.raw).name)
- .and_then(|s| str::from_utf8(s).ok())
- }
+ unsafe { ::opt_bytes(self, (*self.raw).name).and_then(|s| str::from_utf8(s).ok()) }
}
/// Returns the key in base64/printable format
pub fn key(&self) -> &str {
- let bytes = unsafe {
- ::opt_bytes(self, (*self.raw).key).unwrap()
- };
+ let bytes = unsafe { ::opt_bytes(self, (*self.raw).key).unwrap() };
str::from_utf8(bytes).unwrap()
}
}
@@ -262,7 +264,12 @@ impl<'kh> Binding for Host<'kh> {
type Raw = *mut raw::libssh2_knownhost;
unsafe fn from_raw(raw: *mut raw::libssh2_knownhost) -> Host<'kh> {
- Host { raw: raw, _marker: marker::PhantomData }
+ Host {
+ raw: raw,
+ _marker: marker::PhantomData,
+ }
+ }
+ fn raw(&self) -> *mut raw::libssh2_knownhost {
+ self.raw
}
- fn raw(&self) -> *mut raw::libssh2_knownhost { self.raw }
}