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
//! A rust library for FreeBSD jails.
//!
//! it aims to provide the features exposed by the FreeBSD Jail Library
//! [jail(3)](https://www.freebsd.org/cgi/man.cgi?query=jail&sektion=3&manpath=FreeBSD+11.1-stable)

extern crate byteorder;

#[macro_use]
extern crate failure;

extern crate libc;

#[macro_use]
extern crate log;

extern crate sysctl;

#[macro_use]
mod sys;

#[macro_use]
extern crate bitflags;

extern crate nix;

extern crate rctl;

extern crate strum;

#[macro_use]
extern crate strum_macros;

#[cfg(feature = "serialize")]
extern crate serde;

#[cfg(feature = "serialize")]
extern crate serde_json;

use std::collections::HashMap;
use std::convert;
use std::net;
use std::path;

mod error;
pub use error::JailError;

mod running;
pub use running::RunningJail;
pub use running::RunningJails as RunningJailIter;

mod stopped;
pub use stopped::StoppedJail;

pub mod param;
pub mod process;

#[cfg(test)]
mod tests;

/// Represents a running or stopped jail.
#[cfg(target_os = "freebsd")]
#[derive(Debug, PartialEq, Clone)]
pub enum Jail {
    Stopped(StoppedJail),
    Running(RunningJail),
}

impl convert::From<RunningJail> for Jail {
    fn from(running: RunningJail) -> Self {
        Jail::Running(running)
    }
}

impl convert::From<StoppedJail> for Jail {
    fn from(stopped: StoppedJail) -> Self {
        trace!("Jail::from({:?})", stopped);
        Jail::Stopped(stopped)
    }
}

impl Jail {
    /// Check if a jail is running
    pub fn is_started(&self) -> bool {
        trace!("Jail::is_started({:?})", self);
        match self {
            Jail::Running(_) => true,
            Jail::Stopped(_) => false,
        }
    }

    /// Start the Jail
    ///
    /// This calls start() on a stopped Jail, and is a no-op for an already
    /// running Jail.
    pub fn start(self) -> Result<Self, JailError> {
        trace!("Jail::start({:?})", self);
        match self {
            Jail::Running(r) => Ok(Jail::Running(r)),
            Jail::Stopped(s) => Ok(Jail::Running(s.start()?)),
        }
    }

    /// Stop the jail
    ///
    /// This calls stop() on a started Jail, and is a no-op for an already
    /// stopped Jail.
    pub fn stop(self) -> Result<Self, JailError> {
        trace!("Jail::stop({:?})", self);
        match self {
            Jail::Running(r) => Ok(Jail::Stopped(r.stop()?)),
            Jail::Stopped(s) => Ok(Jail::Stopped(s)),
        }
    }

    /// Get the name of the Jail
    pub fn name(&self) -> Result<String, JailError> {
        trace!("Jail::name({:?})", self);
        match self {
            Jail::Running(r) => r.name(),
            Jail::Stopped(s) => s
                .name
                .clone()
                .ok_or_else(|| JailError::NoSuchParameter("name".into())),
        }
    }

    /// Get the name of the Jail
    pub fn path(&self) -> Result<path::PathBuf, JailError> {
        trace!("Jail::path({:?})", self);
        match self {
            Jail::Running(r) => r.path(),
            Jail::Stopped(s) => s
                .path
                .clone()
                .ok_or_else(|| JailError::NoSuchParameter("path".into())),
        }
    }

    /// Get the hostname of the Jail
    pub fn hostname(&self) -> Result<String, JailError> {
        trace!("Jail::hostname({:?})", self);
        match self {
            Jail::Running(r) => r.hostname(),
            Jail::Stopped(s) => s
                .hostname
                .clone()
                .ok_or_else(|| JailError::NoSuchParameter("hostname".into())),
        }
    }

    /// Get the IP Addresses of a jail
    pub fn ips(&self) -> Result<Vec<net::IpAddr>, JailError> {
        trace!("Jail::ips({:?})", self);
        match self {
            Jail::Running(r) => r.ips(),
            Jail::Stopped(s) => Ok(s.ips.clone()),
        }
    }

    /// Get a jail parameter
    pub fn param(&self, name: &str) -> Result<param::Value, JailError> {
        trace!("Jail::param({:?})", self);
        match self {
            Jail::Running(r) => r.param(name),
            Jail::Stopped(s) => s
                .params
                .get(name)
                .ok_or_else(|| JailError::NoSuchParameter(name.into()))
                .map(|x| x.clone()),
        }
    }

    pub fn params(&self) -> Result<HashMap<String, param::Value>, JailError> {
        trace!("Jail::params({:?})", self);
        match self {
            Jail::Running(r) => r.params(),
            Jail::Stopped(s) => Ok(s.params.clone()),
        }
    }
}