Skip to content
Merged
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
2 changes: 2 additions & 0 deletions libssh2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ extern "C" {
username: *const c_char,
identity: *mut libssh2_agent_publickey,
) -> c_int;
pub fn libssh2_agent_set_identity_path(agent: *mut LIBSSH2_AGENT, path: *const c_char);
pub fn libssh2_agent_get_identity_path(agent: *mut LIBSSH2_AGENT) -> *const c_char;

// channels
pub fn libssh2_channel_free(chan: *mut LIBSSH2_CHANNEL) -> c_int;
Expand Down
23 changes: 23 additions & 0 deletions src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use parking_lot::{Mutex, MutexGuard};
use std::ffi::{CStr, CString};
use std::path::{Path, PathBuf};
use std::ptr::null_mut;
use std::slice;
use std::str;
use std::sync::Arc;

use util;
use {raw, Error, ErrorCode, SessionInner};

/// A structure representing a connection to an SSH agent.
Expand Down Expand Up @@ -125,6 +127,27 @@ impl Agent {
))
}
}

/// Set a custom agent socket path to connect to.
pub fn set_identity_path(&mut self, path: &Path) -> Result<(), Error> {
let path = CString::new(util::path2bytes(path)?)?;
unsafe {
raw::libssh2_agent_set_identity_path(self.raw, path.as_ptr());
}
Ok(())
}

/// Get the custom agent socket path, if set.
pub fn identity_path(&self) -> Option<PathBuf> {
unsafe {
let ptr = raw::libssh2_agent_get_identity_path(self.raw);
if ptr.is_null() {
None
} else {
Some(util::mkpath(CStr::from_ptr(ptr).to_bytes()))
}
}
}
}

impl Drop for Agent {
Expand Down
16 changes: 2 additions & 14 deletions src/sftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl Sftp {
}
Self::rc(&locked, rc).map(move |_| {
unsafe { ret.set_len(rc as usize) }
mkpath(ret)
util::mkpath(&ret)
})
}

Expand Down Expand Up @@ -644,7 +644,7 @@ impl File {
unsafe {
buf.set_len(rc as usize);
}
(mkpath(buf), FileStat::from_raw(&stat))
(util::mkpath(&buf), FileStat::from_raw(&stat))
})
}
}
Expand Down Expand Up @@ -907,15 +907,3 @@ impl FileType {
}
}
}

#[cfg(unix)]
fn mkpath(v: Vec<u8>) -> PathBuf {
use std::ffi::OsStr;
use std::os::unix::prelude::*;
PathBuf::from(OsStr::from_bytes(&v))
}
#[cfg(windows)]
fn mkpath(v: Vec<u8>) -> PathBuf {
use std::str;
PathBuf::from(str::from_utf8(&v).unwrap())
}
14 changes: 13 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use std::path::Path;
use std::path::{Path, PathBuf};

use {raw, Error, ErrorCode};

Expand Down Expand Up @@ -37,6 +37,18 @@ pub fn path2bytes(p: &Path) -> Result<Cow<'_, [u8]>, Error> {
.and_then(check)
}

#[cfg(unix)]
pub fn mkpath(bytes: &[u8]) -> PathBuf {
use std::ffi::OsStr;
use std::os::unix::prelude::*;
PathBuf::from(OsStr::from_bytes(bytes))
}
#[cfg(windows)]
pub fn mkpath(bytes: &[u8]) -> PathBuf {
use std::str;
PathBuf::from(str::from_utf8(bytes).unwrap())
}

fn check(b: Cow<[u8]>) -> Result<Cow<[u8]>, Error> {
if b.iter().any(|b| *b == 0) {
Err(Error::new(
Expand Down