summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWez Furlong <wez@wezfurlong.org>2019-07-23 17:52:31 -0700
committerWez Furlong <wez@wezfurlong.org>2019-07-29 08:55:06 -0700
commitd29b2f7a9fcc6908f5f1e48af15e057d77b3e2ee (patch)
treec0e6ccd13d8cda7443c463645880142bd7d0d535
parenteac5df1d22eea071708db2e57dc0637ceb3fc580 (diff)
downloadssh2-rs-d29b2f7a9fcc6908f5f1e48af15e057d77b3e2ee.zip
Agent no longer borrows session either
-rw-r--r--src/agent.rs43
-rw-r--r--src/session.rs2
2 files changed, 23 insertions, 22 deletions
diff --git a/src/agent.rs b/src/agent.rs
index b1f35f5..ad1b863 100644
--- a/src/agent.rs
+++ b/src/agent.rs
@@ -1,23 +1,24 @@
use std::ffi::CString;
use std::marker;
+use std::rc::Rc;
use std::slice;
use std::str;
-use util::{Binding, SessionBinding};
-use {raw, Error, Session};
+use util::Binding;
+use {raw, Error, SessionInner};
/// A structure representing a connection to an SSH agent.
///
/// Agents can be used to authenticate a session.
-pub struct Agent<'sess> {
+pub struct Agent {
raw: *mut raw::LIBSSH2_AGENT,
- sess: &'sess Session,
+ sess: Rc<SessionInner>,
}
/// An iterator over the identities found in an SSH agent.
pub struct Identities<'agent> {
prev: *mut raw::libssh2_agent_publickey,
- agent: &'agent Agent<'agent>,
+ agent: &'agent Agent,
}
/// A public key which is extracted from an SSH agent.
@@ -26,7 +27,21 @@ pub struct PublicKey<'agent> {
_marker: marker::PhantomData<&'agent [u8]>,
}
-impl<'sess> Agent<'sess> {
+impl Agent {
+ pub(crate) fn from_raw_opt(
+ raw: *mut raw::LIBSSH2_AGENT,
+ sess: &Rc<SessionInner>,
+ ) -> Result<Self, Error> {
+ if raw.is_null() {
+ Err(Error::last_error_raw(sess.raw).unwrap_or_else(Error::unknown))
+ } else {
+ Ok(Self {
+ raw,
+ sess: Rc::clone(sess),
+ })
+ }
+ }
+
/// Connect to an ssh-agent running on the system.
pub fn connect(&mut self) -> Result<(), Error> {
unsafe { self.sess.rc(raw::libssh2_agent_connect(self.raw)) }
@@ -66,21 +81,7 @@ impl<'sess> Agent<'sess> {
}
}
-impl<'sess> SessionBinding<'sess> for Agent<'sess> {
- type Raw = raw::LIBSSH2_AGENT;
-
- unsafe fn from_raw(sess: &'sess Session, raw: *mut raw::LIBSSH2_AGENT) -> Agent<'sess> {
- Agent {
- raw: raw,
- sess: sess,
- }
- }
- fn raw(&self) -> *mut raw::LIBSSH2_AGENT {
- self.raw
- }
-}
-
-impl<'a> Drop for Agent<'a> {
+impl Drop for Agent {
fn drop(&mut self) {
unsafe { raw::libssh2_agent_free(self.raw) }
}
diff --git a/src/session.rs b/src/session.rs
index 47329aa..7228e6e 100644
--- a/src/session.rs
+++ b/src/session.rs
@@ -429,7 +429,7 @@ impl Session {
///
/// The returned agent will still need to be connected manually before use.
pub fn agent(&self) -> Result<Agent, Error> {
- unsafe { SessionBinding::from_raw_opt(self, raw::libssh2_agent_init(self.inner.raw)) }
+ unsafe { Agent::from_raw_opt(raw::libssh2_agent_init(self.inner.raw), &self.inner) }
}
/// Init a collection of known hosts for this session.