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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright (c) 2015-2016, Arista Networks, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//   * Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
//   * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
//
//   * Neither the name of Arista Networks nor the names of its
//   contributors may be used to endorse or promote products derived from
//   this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//! This crate allows execution of CLI commands on [Arista](https://www.arista.com) EOS switches.
//!
//! # Features
//! * `async` adds async (via [tokio](https://tokio.rs) runtime) support.
//!
//! # Example
//! ```
//! let result = eapi_run(None, &["show clock", "show aliases"], ResultFormat::Json);
//! match result {
//!     Response::Result(v) => println!("{v:?}"),
//!     Response::Error {
//!         message,
//!         code,
//!         errors,
//!     } => println!("error code: {code}, message: {message}, outputs: {errors:#?}"),
//! };

use thiserror::Error;

#[cfg(feature = "async")]
pub mod async_api;
mod client;
mod protocol;

static SYSNAME: &str = "ar";

/// The errors returned by the library.
#[derive(Debug, Error)]
pub enum Error {
    /// Protocol error (incorrect encoding received from server, etc.)
    #[error("protocol error: {0}")]
    ProtocolError(#[from] protocol::Error),
    /// I/O and other communication errors.
    #[error("communication error: {0}")]
    ClientError(#[from] client::Error),
}

/// Format of the commands output.
#[derive(Default)]
pub enum ResultFormat {
    /// JSONified output
    #[default]
    Json,
    /// Plain text (same as in the CLI output)
    Text,
}

/// Commands execution response.
#[derive(Debug, PartialEq)]
pub enum Response {
    /// Error response
    Error {
        /// Error message
        message: String,
        /// Error code
        code: i64,
        /// For each command, either the output or the error associated with it.
        /// The execution stops at the first encountered error, so length of `errors` might be
        /// smaller than number of commands.
        errors: Vec<String>,
    },
    Result(Vec<String>),
}

fn eapi_run_internal<T1: AsRef<str>, T2: AsRef<str>>(
    socket_name: T1,
    sysname: &str,
    cmds: &[T2],
    format: ResultFormat,
) -> Result<Response, Error> {
    let socket_name = socket_name.as_ref();
    let mut conn = client::Client::connect(sysname, socket_name)?;
    let request = protocol::make_run_request(cmds, format);
    let response = conn.do_request(request)?;
    protocol::parse_response(&response).map_err(|e| e.into())
}

/// Runs commands via eAPI and returns the results.
///
/// # Arguments:
/// * `sysname` argument should be left as `None` for running commands on production devices.
/// * `commands` is a list of commands to run (they shouldn't end with new line).
/// * `format` specifies if the results of the commands is plain text or JSON.
///
/// The commands are executed in order and the execution stops at the first command that
/// results in an error.
pub fn eapi_run<T: AsRef<str>>(
    sysname: Option<&str>,
    commands: &[T],
    format: ResultFormat,
) -> Result<Response, Error> {
    let sysname = sysname.unwrap_or(SYSNAME);
    eapi_run_internal(
        protocol::make_socket_name(sysname),
        sysname,
        commands,
        format,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use nix::sys;
    use std::io::IoSliceMut;
    use std::os::unix::io::RawFd;

    fn rcv_string(socket: RawFd) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
        let mut len = [0; 4];
        sys::socket::recv(socket, &mut len, sys::socket::MsgFlags::MSG_WAITALL)?;
        let len = i32::from_le_bytes(len) as usize;

        let mut buf = vec![0; len];
        sys::socket::recv(socket, &mut buf, sys::socket::MsgFlags::MSG_WAITALL)?;

        Ok(buf)
    }

    fn rcv_fd<const N: usize>(
        socket: RawFd,
    ) -> Result<Vec<RawFd>, Box<dyn std::error::Error + Send + Sync>> {
        let mut buf = [0];
        let mut iov = [IoSliceMut::new(&mut buf)];
        let mut cmsg_buf: Vec<u8> = nix::cmsg_space!([RawFd; N]);

        let mut result = Vec::with_capacity(N);
        loop {
            let rcv = sys::socket::recvmsg::<()>(
                socket,
                &mut iov,
                Some(&mut cmsg_buf),
                sys::socket::MsgFlags::empty(),
            )?;

            for cmsg in rcv.cmsgs() {
                if let sys::socket::ControlMessageOwned::ScmRights(fds) = cmsg {
                    result.extend(fds)
                } else {
                    return Err("didn't receive SCM_RIGHTS message".into());
                }
            }
            if result.len() == N {
                break Ok(result);
            }
        }
    }

    pub fn run_server<T: AsRef<str>>(
        socket_name: T,
        sysname: &str,
        response: &str,
    ) -> (
        std::sync::Arc<std::sync::Barrier>,
        std::thread::JoinHandle<Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>>,
    ) {
        let socket_name = socket_name.as_ref().to_string();
        let sysname = sysname.to_string();
        let response = response.to_string();

        let barrier = std::sync::Arc::new(std::sync::Barrier::new(2));
        let ready = barrier.clone();
        let handle = std::thread::spawn(move || {
            let socket = sys::socket::socket(
                sys::socket::AddressFamily::Unix,
                sys::socket::SockType::Stream,
                sys::socket::SockFlag::empty(),
                None,
            )?;

            sys::socket::bind(socket, &sys::socket::UnixAddr::new(socket_name.as_str())?)?;
            sys::socket::listen(socket, 1)?;
            ready.wait();

            let stream = sys::socket::accept(socket)?;

            let signal = rcv_fd::<1>(stream)?[0];

            if rcv_string(stream)? != protocol::make_args(sysname) {
                return Err("received invalid args".into());
            }

            if rcv_string(stream)? != protocol::make_env()? {
                return Err("received invalid env".into());
            }

            if rcv_string(stream)? != "0".as_bytes() {
                return Err("received invalid uid".into());
            }

            if rcv_string(stream)? != "0".as_bytes() {
                return Err("received invalid gid".into());
            }

            if rcv_string(stream)? != "".as_bytes() {
                return Err("received invalid terminal name".into());
            }

            let mut buf = [0];
            sys::socket::recv(stream, &mut buf, sys::socket::MsgFlags::empty())?;
            if buf[0] != b'c' {
                return Err("received invalid mode".into());
            }

            let sockets = rcv_fd::<3>(stream)?;
            let resp_socket = sockets[0];
            let req_socket = sockets[1];
            let stats = sockets[2];

            nix::unistd::close(stream)?;

            let request = rcv_string(req_socket)?;
            sys::socket::send(
                resp_socket,
                response.as_bytes(),
                sys::socket::MsgFlags::empty(),
            )?;

            nix::unistd::close(signal)?;
            nix::unistd::close(req_socket)?;
            nix::unistd::close(resp_socket)?;
            nix::unistd::close(stats)?;

            nix::unistd::close(socket)?;

            Ok(request)
        });

        (barrier, handle)
    }

    #[test]
    fn test_eapi_run_ok() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let tmp_dir = tempfile::tempdir()?;
        let socket_name = tmp_dir
            .path()
            .join(SYSNAME)
            .to_str()
            .ok_or("can't convert path to string")?
            .to_string();

        let response = r#"{
            "jsonrpc": "2.0",
            "result": ["test1", "test2", {"a": "b"}],
            "id": "1"
        }"#;
        let (ready, handle) = run_server(&socket_name, SYSNAME, response);
        ready.wait();
        let result = eapi_run_internal(
            &socket_name,
            SYSNAME,
            &["show run", "show int", "show clock"],
            ResultFormat::Json,
        )?;
        let request = match handle.join() {
            Ok(r) => r?,
            Err(e) => std::panic::resume_unwind(e),
        };
        let expected = serde_json::json!({
            "jsonrpc": "2.0",
            "method": "runCmds",
            "params": {
                "version": "latest",
                "cmds": ["show run", "show int", "show clock"],
                "format": "json",
            },
            "id": "1"
        })
        .to_string();
        assert_eq!(request, expected.as_bytes());
        assert_eq!(
            result,
            Response::Result(vec![
                "\"test1\"".to_string(),
                "\"test2\"".to_string(),
                "{\"a\":\"b\"}".to_string()
            ])
        );

        Ok(())
    }

    #[test]
    fn test_eapi_run_error() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let tmp_dir = tempfile::tempdir()?;
        let socket_name = tmp_dir
            .path()
            .join(SYSNAME)
            .to_str()
            .ok_or("can't convert path to string")?
            .to_string();

        let response = r#"{
            "jsonrpc": "2.0",
            "error": {
                "message": "error message",
                "code": 3,
                "data": ["a", "b"]
            },
            "id": "1"
        }"#;
        let (ready, handle) = run_server(&socket_name, SYSNAME, response);
        ready.wait();
        let result = eapi_run_internal(
            &socket_name,
            SYSNAME,
            &["show run", "show int", "show clock"],
            ResultFormat::Json,
        )?;
        let request = match handle.join() {
            Ok(r) => r?,
            Err(e) => std::panic::resume_unwind(e),
        };
        let expected = serde_json::json!({
            "jsonrpc": "2.0",
            "method": "runCmds",
            "params": {
                "version": "latest",
                "cmds": ["show run", "show int", "show clock"],
                "format": "json",
            },
            "id": "1"
        })
        .to_string();
        assert_eq!(request, expected.as_bytes());
        assert_eq!(
            result,
            Response::Error {
                message: "error message".to_string(),
                code: 3,
                errors: vec!["\"a\"".to_string(), "\"b\"".to_string()]
            }
        );

        Ok(())
    }
}