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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2015-2017 Intecture Developers. See the COPYRIGHT file at the
// top-level directory of this distribution and at
// https://intecture.io/COPYRIGHT.
//
// Licensed under the Mozilla Public License 2.0 <LICENSE or
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
// modified, or distributed except according to those terms.

#[cfg(feature = "remote-run")]
use czmq;
use libc::c_char;
use mustache;
use regex;
use serde_json;
use std::{convert, error, ffi, fmt, io, num, ptr, result, str, string};
use std::any::Any;
use std::ffi::CString;
#[cfg(feature = "remote-run")]
use zfilexfer;

pub type Result<T> = result::Result<T, Error>;

/// Error message constant for communicating errors from the FFI to C consumers
pub static mut ERRMSG: *const c_char = 0 as *const c_char;

pub fn seterr<E: Into<Error>>(err: E) {
    unsafe { ERRMSG = CString::new(err.into().to_string()).expect("Could not convert error string to CString").into_raw(); }
}

#[no_mangle]
pub extern "C" fn geterr() -> *const c_char {
    unsafe {
        let e = ERRMSG;
        ERRMSG = ptr::null();
        e
    }
}

#[derive(Debug)]
/// Global error type.
pub enum Error {
    /// An error string returned from the host's Intecture Agent
    Agent(String),
    #[cfg(feature = "remote-run")]
    /// An error string returned from the host's Intecture Auth
    Auth(String),
    #[cfg(feature = "remote-run")]
    /// Payload build failure
    BuildFailed(String),
    #[cfg(feature = "remote-run")]
    /// Payload run failure
    RunFailed(String),
    #[cfg(feature = "remote-run")]
    /// CZMQ error
    Czmq(czmq::Error),
    #[cfg(feature = "remote-run")]
    /// Message frames missing in the response from host's Intecture Agent
    Frame(MissingFrame),
    /// Generic error string
    Generic(String),
    #[cfg(feature = "remote-run")]
    /// Cannot run command on disconnected host
    HostDisconnected,
    #[cfg(feature = "remote-run")]
    /// Invalid response from host
    HostResponse,
    /// Invalid file descriptor
    InvalidFileDescriptor,
    /// IO error
    Io(io::Error),
    /// Mustache template error
    Mustache(mustache::Error),
    /// FFI null error
    NulError(ffi::NulError),
    /// FFI received null pointer
    NullPtr(&'static str),
    /// Cast str as float
    ParseFloat(num::ParseFloatError),
    /// Cast str as int
    ParseInt(num::ParseIntError),
    /// Data query parser
    QueryParser(String),
    /// Regex error
    Regex(regex::Error),
    /// Serde JSON error
    SerdeJson(serde_json::Error),
    /// Cast str
    StrFromUtf8(str::Utf8Error),
    /// Cast String
    StringFromUtf8(string::FromUtf8Error),
    #[cfg(feature = "remote-run")]
    /// ZFileXfer error
    ZFileXfer(zfilexfer::Error),
}

unsafe impl Send for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Agent(ref e) => write!(f, "Agent error: {}", e),
            #[cfg(feature = "remote-run")]
            Error::Auth(ref e) => write!(f, "Auth error: {}", e),
            #[cfg(feature = "remote-run")]
            Error::BuildFailed(ref e) => write!(f, "Failed to build payload: {}", e),
            #[cfg(feature = "remote-run")]
            Error::RunFailed(ref e) => write!(f, "Failed to run payload: {}", e),
            #[cfg(feature = "remote-run")]
            Error::Czmq(ref e) => write!(f, "CZMQ error: {}", e),
            #[cfg(feature = "remote-run")]
            Error::Frame(ref e) => write!(f, "Missing frame {} in message: {}", e.order, e.name),
            Error::Generic(ref e) => write!(f, "Error: {}", e),
            #[cfg(feature = "remote-run")]
            Error::HostDisconnected => write!(f, "Cannot run command while host is disconnected"),
            #[cfg(feature = "remote-run")]
            Error::HostResponse => write!(f, "Invalid response from host"),
            Error::InvalidFileDescriptor => write!(f, "Invalid file descriptor"),
            Error::Io(ref e) => write!(f, "IO error: {}", e),
            Error::Mustache(ref e) => write!(f, "Mustache error: {:?}", e),
            Error::NulError(ref e) => write!(f, "Nul error: {}", e),
            Error::NullPtr(ref e) => write!(f, "Received null when we expected a {} pointer", e),
            Error::ParseFloat(ref e) => write!(f, "Parse error: {}", e),
            Error::ParseInt(ref e) => write!(f, "Parse error: {}", e),
            Error::QueryParser(ref e) => write!(f, "Query parser error: {}", e),
            Error::Regex(ref e) => write!(f, "Regex error: {}", e),
            Error::SerdeJson(ref e) => write!(f, "Serde JSON error: {}", e),
            Error::StrFromUtf8(ref e) => write!(f, "Convert from UTF8 slice to str error: {}", e),
            Error::StringFromUtf8(ref e) => write!(f, "Convert from UTF8 slice to String error: {}", e),
            #[cfg(feature = "remote-run")]
            Error::ZFileXfer(ref e) => write!(f, "ZFileXfer error: {}", e),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Agent(ref e) => e,
            #[cfg(feature = "remote-run")]
            Error::Auth(ref e) => e,
            #[cfg(feature = "remote-run")]
            Error::BuildFailed(ref e) => e,
            #[cfg(feature = "remote-run")]
            Error::RunFailed(ref e) => e,
            #[cfg(feature = "remote-run")]
            Error::Czmq(ref e) => e.description(),
            #[cfg(feature = "remote-run")]
            Error::Frame(_) => "The Agent's reply was missing a part ('frame') of the expected message",
            Error::Generic(ref e) => e,
            #[cfg(feature = "remote-run")]
            Error::HostDisconnected => "Cannot run command on disconnected host",
            #[cfg(feature = "remote-run")]
            Error::HostResponse => "Invalid response from host",
            Error::InvalidFileDescriptor => "Invalid file descriptor",
            Error::Io(ref e) => e.description(),
            Error::Mustache(ref e) => e.description(),
            Error::NulError(ref e) => e.description(),
            Error::NullPtr(ref e) => e,
            Error::ParseFloat(ref e) => e.description(),
            Error::ParseInt(ref e) => e.description(),
            Error::QueryParser(ref e) => e,
            Error::Regex(ref e) => e.description(),
            Error::SerdeJson(ref e) => e.description(),
            Error::StrFromUtf8(ref e) => e.description(),
            Error::StringFromUtf8(ref e) => e.description(),
            #[cfg(feature = "remote-run")]
            Error::ZFileXfer(ref e) => e.description(),
        }
    }
}

// Specifically for catch_unwind()
impl convert::From<Box<Any + Send>> for Error {
    fn from(err: Box<Any + Send>) -> Error {
        match err.downcast::<Error>() {
            Ok(e) => *e,
            Err(err) => match err.downcast::<String>() {
                Ok(s) => Error::Generic(*s),
                Err(_) => Error::Generic("Could not convert panic to Error".into())
            }
        }
    }
}

#[cfg(feature = "remote-run")]
impl convert::From<czmq::Error> for Error {
    fn from(err: czmq::Error) -> Error {
        Error::Czmq(err)
    }
}

impl convert::From<ffi::NulError> for Error {
    fn from(err: ffi::NulError) -> Error {
        Error::NulError(err)
    }
}

impl convert::From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::Io(err)
    }
}

#[cfg(feature = "remote-run")]
impl convert::From<MissingFrame> for Error {
    fn from(err: MissingFrame) -> Error {
        Error::Frame(err)
    }
}

impl convert::From<mustache::Error> for Error {
    fn from(err: mustache::Error) -> Error {
        Error::Mustache(err)
    }
}

impl convert::From<regex::Error> for Error {
    fn from(err: regex::Error) -> Error {
        Error::Regex(err)
    }
}

impl convert::From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Error {
        Error::SerdeJson(err)
    }
}

impl convert::From<str::Utf8Error> for Error {
    fn from(err: str::Utf8Error) -> Error {
        Error::StrFromUtf8(err)
    }
}

impl convert::From<string::FromUtf8Error> for Error {
    fn from(err: string::FromUtf8Error) -> Error {
        Error::StringFromUtf8(err)
    }
}

impl convert::From<num::ParseFloatError> for Error {
    fn from(err: num::ParseFloatError) -> Error {
        Error::ParseFloat(err)
    }
}

impl convert::From<num::ParseIntError> for Error {
    fn from(err: num::ParseIntError) -> Error {
        Error::ParseInt(err)
    }
}

#[cfg(feature = "remote-run")]
impl convert::From<zfilexfer::Error> for Error {
    fn from(err: zfilexfer::Error) -> Error {
        Error::ZFileXfer(err)
    }
}

#[cfg(feature = "remote-run")]
#[derive(Debug)]
pub struct MissingFrame {
    name: String,
    order: u8,
}

#[cfg(feature = "remote-run")]
impl MissingFrame {
    pub fn new(name: &str, order: u8) -> MissingFrame {
        MissingFrame {
            name: name.to_string(),
            order: order,
        }
    }
}