summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 5b4e1686dfc95632ea06c296c567d6166a55f898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Rust bindings to libssh2, an SSH client library.
//!
//! This library intends to provide a safe interface to the libssh2 library. It
//! will build the library if it's not available on the local system, and
//! otherwise link to an installed copy.
//!
//! Note that libssh2 only supports SSH *clients*, not SSH *servers*.
//! Additionally it only supports protocol v2, not protocol v1.
//!
//! # Examples
//!
//! ## Inspecting ssh-agent
//!
//! ```
//! use ssh2::Session;
//!
//! // Almost all APIs require a `Session` to be available
//! let sess = Session::new().unwrap();
//! let mut agent = sess.agent().unwrap();
//!
//! // Connect the agent and request a list of identities
//! agent.connect().unwrap();
//! agent.list_identities().unwrap();
//!
//! for identity in agent.identities() {
//!     let identity = identity.unwrap(); // assume no I/O errors
//!     println!("{}", identity.comment())
//!     let pubkey = identity.blob();
//! }
//! ```
//!
//! ## Authenticating with ssh-agent
//!
//! ```no_run
//! use ssh2::Session;
//!
//! // Almost all APIs require a `Session` to be available
//! let sess = Session::new().unwrap();
//! let mut agent = sess.agent().unwrap();
//!
//! // Try to authenticate with the first identity in the agent.
//! agent.connect().unwrap();
//! agent.list_identities().unwrap();
//! let identity = agent.identities().next().unwrap().unwrap();
//! agent.userauth("foo", &identity).unwrap();
//!
//! // Make sure we succeeded
//! assert!(sess.authenticated());
//! ```

#![feature(phase, unsafe_destructor)]

extern crate "libssh2-sys" as raw;
extern crate libc;

use std::c_str::CString;
use std::mem;
use std::rt;
use std::sync::{Once, ONCE_INIT};

pub use agent::{Agent, Identities, PublicKey};
pub use channel::{Channel, ExitSignal, ReadWindow, WriteWindow};
pub use error::Error;
pub use knownhosts::{KnownHosts, Hosts, Host};
pub use listener::Listener;
pub use session::Session;

mod agent;
mod channel;
mod error;
mod knownhosts;
mod listener;
mod session;

/// Initialize the libssh2 library.
///
/// This is optional, it is lazily invoked.
pub fn init() {
    static mut INIT: Once = ONCE_INIT;
    unsafe {
        INIT.doit(|| {
            assert_eq!(raw::libssh2_init(0), 0);
            rt::at_exit(proc() {
                raw::libssh2_exit();
            });
        })
    }
}

unsafe fn opt_bytes<'a, T>(_: &'a T,
                           c: *const libc::c_char) -> Option<&'a [u8]> {
    if c.is_null() {
        None
    } else {
        let s = CString::new(c, false);
        Some(mem::transmute(s.as_bytes_no_nul()))
    }
}

#[allow(missing_doc)]
pub enum DisconnectCode {
    HostNotAllowedToConnect =
        raw::SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT as int,
    ProtocolError = raw::SSH_DISCONNECT_PROTOCOL_ERROR as int,
    KeyExchangeFailed = raw::SSH_DISCONNECT_KEY_EXCHANGE_FAILED as int,
    Reserved = raw::SSH_DISCONNECT_RESERVED as int,
    MacError = raw::SSH_DISCONNECT_MAC_ERROR as int,
    CompressionError = raw::SSH_DISCONNECT_COMPRESSION_ERROR as int,
    ServiceNotAvailable = raw::SSH_DISCONNECT_SERVICE_NOT_AVAILABLE as int,
    ProtocolVersionNotSupported =
        raw::SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED as int,
    HostKeyNotVerifiable = raw::SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE as int,
    ConnectionLost = raw::SSH_DISCONNECT_CONNECTION_LOST as int,
    ByApplication = raw::SSH_DISCONNECT_BY_APPLICATION as int,
    TooManyConnections = raw::SSH_DISCONNECT_TOO_MANY_CONNECTIONS as int,
    AuthCancelledByUser = raw::SSH_DISCONNECT_AUTH_CANCELLED_BY_USER as int,
    NoMoreAuthMethodsAvailable =
        raw::SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE as int,
    IllegalUserName = raw::SSH_DISCONNECT_ILLEGAL_USER_NAME as int,

}

/// Flags to be enabled/disabled on a Session
pub enum SessionFlag {
    /// If set, libssh2 will not attempt to block SIGPIPEs but will let them
    /// trigger from the underlying socket layer.
    SigPipe = raw::LIBSSH2_FLAG_SIGPIPE as int,

    /// If set - before the connection negotiation is performed - libssh2 will
    /// try to negotiate compression enabling for this connection. By default
    /// libssh2 will not attempt to use compression.
    Compress = raw::LIBSSH2_FLAG_COMPRESS as int,
}

pub enum HostKeyType {
    TypeUnknown = raw::LIBSSH2_HOSTKEY_TYPE_UNKNOWN as int,
    TypeRsa = raw::LIBSSH2_HOSTKEY_TYPE_RSA as int,
    TypeDss = raw::LIBSSH2_HOSTKEY_TYPE_DSS as int,
}

pub enum MethodType {
    MethodKex = raw::LIBSSH2_METHOD_KEX as int,
    MethodHostKey = raw::LIBSSH2_METHOD_HOSTKEY as int,
    MethodCryptCs = raw::LIBSSH2_METHOD_CRYPT_CS as int,
    MethodCryptSc = raw::LIBSSH2_METHOD_CRYPT_SC as int,
    MethodMacCs = raw::LIBSSH2_METHOD_MAC_CS as int,
    MethodMacSc = raw::LIBSSH2_METHOD_MAC_SC as int,
    MethodCompCs = raw::LIBSSH2_METHOD_COMP_CS as int,
    MethodCompSc = raw::LIBSSH2_METHOD_COMP_SC as int,
    MethodLangCs = raw::LIBSSH2_METHOD_LANG_CS as int,
    MethodLangSc = raw::LIBSSH2_METHOD_LANG_SC as int,
}

pub static FlushExtendedData: uint = -1;
pub static FlushAll: uint = -2;
pub static ExtendedDataStderr: uint = 1;

pub enum HashType {
    HashMd5 = raw::LIBSSH2_HOSTKEY_HASH_MD5 as int,
    HashSha1 = raw:: LIBSSH2_HOSTKEY_HASH_SHA1 as int,
}

pub enum KnownHostFileKind {
    OpenSSH = raw::LIBSSH2_KNOWNHOST_FILE_OPENSSH as int,
}

pub enum CheckResult {
    /// Hosts and keys match
    CheckMatch = raw::LIBSSH2_KNOWNHOST_CHECK_MATCH as int,
    /// Host was found, but the keys didn't match!
    CheckMismatch = raw::LIBSSH2_KNOWNHOST_CHECK_MISMATCH as int,
    /// No host match was found
    CheckNotFound = raw::LIBSSH2_KNOWNHOST_CHECK_NOTFOUND as int,
    /// Something prevented the check to be made
    CheckFailure = raw::LIBSSH2_KNOWNHOST_CHECK_FAILURE as int,
}

pub enum KnownHostKeyFormat {
    KeyRsa1 = raw::LIBSSH2_KNOWNHOST_KEY_RSA1 as int,
    KeySshRsa = raw::LIBSSH2_KNOWNHOST_KEY_SSHRSA as int,
    KeySshDss = raw::LIBSSH2_KNOWNHOST_KEY_SSHDSS as int,
}