Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: slog #61

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ bitflags = "^1"
byteorder = "^1.2.3"
failure = "~0.1.1"
libc = "~0.2.41"
log="0.4"
slog = "2.4"
slog-stdlog = "3.0.2"
sysctl = "~0.3.0"
nix= "^0.14.0"
rctl = "0.1.0"
Expand All @@ -38,5 +39,5 @@ serde = { version="1.0", features = ["derive"], optional=true}
serde_json = { version="1.0", optional=true }

[dev-dependencies]
pretty_env_logger = "0.3"
prettytable-rs = "0.8.0"
slog-term = "2.4.0"
41 changes: 29 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! 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)

#![type_length_limit="17825821"]
#![type_length_limit = "17825821"]

extern crate byteorder;

Expand All @@ -13,7 +13,8 @@ extern crate failure;
extern crate libc;

#[macro_use]
extern crate log;
extern crate slog;
extern crate slog_stdlog;

extern crate sysctl;

Expand Down Expand Up @@ -43,6 +44,8 @@ use std::convert;
use std::net;
use std::path;

use slog::Drain;

mod error;
pub use error::JailError;

Expand All @@ -59,6 +62,13 @@ pub mod process;
#[cfg(test)]
mod tests;

#[doc(hidden)]
fn default_logger() -> slog::Logger {
let drain = slog_stdlog::StdLog.fuse();
//let drain = slog_envlogger::new(drain).fuse();
slog::Logger::root(drain, o!())
}

/// Represents a running or stopped jail.
#[cfg(target_os = "freebsd")]
#[derive(Debug, PartialEq, Clone)]
Expand All @@ -75,15 +85,22 @@ impl convert::From<RunningJail> for Jail {

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

impl Jail {
fn get_logger(&self) -> &slog::Logger {
match self {
Jail::Running(ref r) => &r.logger,
Jail::Stopped(ref s) => &s.logger,
}
}

/// Check if a jail is running
pub fn is_started(&self) -> bool {
trace!("Jail::is_started({:?})", self);
trace!(self.get_logger(), "Jail::is_started({:?})", self);
match self {
Jail::Running(_) => true,
Jail::Stopped(_) => false,
Expand All @@ -95,7 +112,7 @@ impl 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);
trace!(self.get_logger(), "Jail::start({:?})", self);
match self {
Jail::Running(r) => Ok(Jail::Running(r)),
Jail::Stopped(s) => Ok(Jail::Running(s.start()?)),
Expand All @@ -107,7 +124,7 @@ impl 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);
trace!(self.get_logger(), "Jail::stop({:?})", self);
match self {
Jail::Running(r) => Ok(Jail::Stopped(r.stop()?)),
Jail::Stopped(s) => Ok(Jail::Stopped(s)),
Expand All @@ -116,7 +133,7 @@ impl Jail {

/// Get the name of the Jail
pub fn name(&self) -> Result<String, JailError> {
trace!("Jail::name({:?})", self);
trace!(self.get_logger(), "Jail::name({:?})", self);
match self {
Jail::Running(r) => r.name(),
Jail::Stopped(s) => s
Expand All @@ -128,7 +145,7 @@ impl Jail {

/// Get the name of the Jail
pub fn path(&self) -> Result<path::PathBuf, JailError> {
trace!("Jail::path({:?})", self);
trace!(self.get_logger(), "Jail::path({:?})", self);
match self {
Jail::Running(r) => r.path(),
Jail::Stopped(s) => s
Expand All @@ -140,7 +157,7 @@ impl Jail {

/// Get the hostname of the Jail
pub fn hostname(&self) -> Result<String, JailError> {
trace!("Jail::hostname({:?})", self);
trace!(self.get_logger(), "Jail::hostname({:?})", self);
match self {
Jail::Running(r) => r.hostname(),
Jail::Stopped(s) => s
Expand All @@ -152,7 +169,7 @@ impl Jail {

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

/// Get a jail parameter
pub fn param(&self, name: &str) -> Result<param::Value, JailError> {
trace!("Jail::param({:?})", self);
trace!(self.get_logger(), "Jail::param({:?})", self);
match self {
Jail::Running(r) => r.param(name),
Jail::Stopped(s) => s
Expand All @@ -173,7 +190,7 @@ impl Jail {
}

pub fn params(&self) -> Result<HashMap<String, param::Value>, JailError> {
trace!("Jail::params({:?})", self);
trace!(self.get_logger(), "Jail::params({:?})", self);
match self {
Jail::Running(r) => r.params(),
Jail::Stopped(s) => Ok(s.params.clone()),
Expand Down
37 changes: 13 additions & 24 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl Type {
/// assert_eq!(Type::of_param("ip6.addr").unwrap(), Type::Ipv6Addrs);
/// ```
pub fn of_param(name: &str) -> Result<Type, JailError> {
trace!("Type::of_param(name={:?})", name);
let (ctl_type, _, _) = info(name)?;

ctltype_to_type(name, ctl_type)
Expand All @@ -50,7 +49,6 @@ impl Type {
/// assert_eq!(Type::Int.is_string(), false);
/// ```
pub fn is_string(&self) -> bool {
trace!("Type::is_string({:?})", self);
match self {
Type::String => true,
_ => false,
Expand All @@ -66,7 +64,6 @@ impl Type {
/// assert_eq!(Type::String.is_numeric(), false);
/// ```
pub fn is_numeric(&self) -> bool {
trace!("Type::is_numeric({:?})", self);
match self {
Type::U8 => true,
Type::U16 => true,
Expand Down Expand Up @@ -96,7 +93,6 @@ impl Type {
/// assert_eq!(Type::String.is_signed(), false);
/// ```
pub fn is_signed(&self) -> bool {
trace!("Type::is_signed({:?})", self);
match self {
Type::S8 => true,
Type::S16 => true,
Expand All @@ -119,7 +115,6 @@ impl Type {
/// assert_eq!(Type::String.is_ip(), false);
/// ```
pub fn is_ip(&self) -> bool {
trace!("Type::is_ip({:?})", self);
match self {
Type::Ipv4Addrs => true,
Type::Ipv6Addrs => true,
Expand All @@ -137,7 +132,6 @@ impl Type {
/// assert_eq!(Type::Ipv6Addrs.is_ipv4(), false);
/// ```
pub fn is_ipv4(&self) -> bool {
trace!("Type::is_ipv4({:?})", self);
match self {
Type::Ipv4Addrs => true,
_ => false,
Expand All @@ -154,7 +148,6 @@ impl Type {
/// assert_eq!(Type::Ipv4Addrs.is_ipv6(), false);
/// ```
pub fn is_ipv6(&self) -> bool {
trace!("Type::is_ipv6({:?})", self);
match self {
Type::Ipv6Addrs => true,
_ => false,
Expand Down Expand Up @@ -184,7 +177,6 @@ impl Type {

impl convert::Into<CtlType> for Type {
fn into(self: Type) -> CtlType {
trace!("Type::into::<CtlType>({:?})", self);
match self {
Type::String => CtlType::String,
Type::U8 => CtlType::U8,
Expand Down Expand Up @@ -269,14 +261,12 @@ impl Value {
/// assert!(Value::Int(42).get_type().is_signed());
/// ```
pub fn get_type(&self) -> Type {
trace!("Value::get_type({:?})", self);
self.into()
}

/// Format the value into a vector of bytes as expected by the jail
/// parameter API.
pub fn as_bytes(self) -> Result<Vec<u8>, JailError> {
trace!("Value::as_bytes({:?})", self);
let mut bytes: Vec<u8> = vec![];

// Some conversions are identity on 64 bit, but not on 32 bit and vice versa
Expand Down Expand Up @@ -356,7 +346,6 @@ impl Value {
/// not_ipv4_addrs.unpack_ipv4().unwrap();
/// ```
pub fn unpack_ipv4(self) -> Result<Vec<net::Ipv4Addr>, JailError> {
trace!("Value::unpack_ipv4({:?})", self);
match self {
Value::Ipv4Addrs(v) => Ok(v),
_ => Err(JailError::ParameterUnpackError),
Expand Down Expand Up @@ -391,7 +380,6 @@ impl Value {
/// rfc1918.unpack_ipv6().unwrap();
/// ```
pub fn unpack_ipv6(self) -> Result<Vec<net::Ipv6Addr>, JailError> {
trace!("Value::unpack_ipv6({:?})", self);
match self {
Value::Ipv6Addrs(v) => Ok(v),
_ => Err(JailError::ParameterUnpackError),
Expand All @@ -416,7 +404,6 @@ impl Value {
/// not_a_string.unpack_string().unwrap();
/// ```
pub fn unpack_string(self) -> Result<String, JailError> {
trace!("Value::unpack_string({:?})", self);
match self {
Value::String(v) => Ok(v),
_ => Err(JailError::ParameterUnpackError),
Expand All @@ -443,7 +430,6 @@ impl Value {
/// assert!(Value::S64(64i64).unpack_u64().is_err());
/// ```
pub fn unpack_u64(self) -> Result<u64, JailError> {
trace!("Value::unpack_u64({:?})", self);
#[cfg_attr(feature = "cargo-clippy", allow(identity_conversion))]
match self {
Value::U64(v) => Ok(v),
Expand Down Expand Up @@ -480,7 +466,6 @@ impl Value {
/// assert!(Value::U64(64u64).unpack_i64().is_err());
/// ```
pub fn unpack_i64(self) -> Result<i64, JailError> {
trace!("Value::unpack_i64({:?})", self);
#[cfg_attr(feature = "cargo-clippy", allow(identity_conversion))]
match self {
Value::S64(v) => Ok(v),
Expand All @@ -500,7 +485,6 @@ impl Value {

#[cfg(target_os = "freebsd")]
fn info(name: &str) -> Result<(CtlType, CtlFlags, usize), JailError> {
trace!("info({:?})", name);
// Get parameter type
let ctlname = format!("security.jail.param.{}", name);

Expand Down Expand Up @@ -551,7 +535,6 @@ fn info(name: &str) -> Result<(CtlType, CtlFlags, usize), JailError> {

#[cfg(target_os = "freebsd")]
fn ctltype_to_type(name: &str, ctl_type: CtlType) -> Result<Type, JailError> {
trace!("ctltype_to_type({:?}, ctl_type={:?})", name, ctl_type);
let param_type = match ctl_type {
CtlType::Int => Type::Int,
CtlType::S64 => Type::S64,
Expand Down Expand Up @@ -597,8 +580,8 @@ fn ctltype_to_type(name: &str, ctl_type: CtlType) -> Result<Type, JailError> {
/// # jail.kill().expect("could not stop jail");
/// ```
#[cfg(target_os = "freebsd")]
pub fn get(jid: i32, name: &str) -> Result<Value, JailError> {
trace!("get(jid={}, name={:?})", jid, name);
pub fn get(jid: i32, name: &str, logger: &slog::Logger) -> Result<Value, JailError> {
trace!(logger, "get(jid={}, name={:?})", jid, name);
let (paramtype, _, typesize) = info(name)?;

// ip4.addr and ip6.addr are arrays, which can be up to
Expand Down Expand Up @@ -772,8 +755,14 @@ pub fn get(jid: i32, name: &str) -> Result<Value, JailError> {
/// # }
/// # jail.kill().expect("could not stop jail");
/// ```
pub fn set(jid: i32, name: &str, value: Value) -> Result<(), JailError> {
trace!("set(jid={}, name={:?}, value={:?})", jid, name, value);
pub fn set(jid: i32, name: &str, value: Value, logger: &slog::Logger) -> Result<(), JailError> {
trace!(
logger,
"set(jid={}, name={:?}, value={:?})",
jid,
name,
value
);
let (ctltype, ctl_flags, _) = info(name)?;

// Check if this is a tunable.
Expand Down Expand Up @@ -839,8 +828,8 @@ pub fn set(jid: i32, name: &str, value: Value) -> Result<(), JailError> {
/// assert_eq!(params.get("allow.raw_sockets"), Some(&param::Value::Int(1)));
/// # jail.kill().expect("could not stop jail");
/// ```
pub fn get_all(jid: i32) -> Result<HashMap<String, Value>, JailError> {
trace!("get_all(jid={})", jid);
pub fn get_all(jid: i32, logger: &slog::Logger) -> Result<HashMap<String, Value>, JailError> {
trace!(logger, "get_all(jid={})", jid);
let params: Result<Vec<(String, Value)>, JailError> = Ctl::new("security.jail.param")
.map_err(JailError::SysctlError)?
.into_iter()
Expand All @@ -866,7 +855,7 @@ pub fn get_all(jid: i32) -> Result<HashMap<String, Value>, JailError> {
.filter(|name| name != "ip6.addr")
.filter(|name| name != "ip4.addr")
// get parameters
.map(|name| get(jid, &name).map(|v| (name, v)))
.map(|name| get(jid, &name, logger).map(|v| (name, v)))
.collect();

Ok(HashMap::from_iter(params?))
Expand Down
6 changes: 3 additions & 3 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ pub trait Jailed {
#[cfg(target_os = "freebsd")]
impl Jailed for process::Command {
fn jail(&mut self, jail: &RunningJail) -> &mut process::Command {
trace!("process::Command::jail({:?}, jail={:?})", self, jail);
let jail = *jail;
//trace!("process::Command::jail({:?}, jail={:?})", self, jail);
let jail = jail.clone();
self.before_exec(move || {
trace!("before_exec handler: attaching");
//trace!("before_exec handler: attaching");
jail.attach().map_err(|err| match err {
JailError::JailAttachError(e) => e,
_ => panic!("jail.attach() failed with unexpected error"),
Expand Down
Loading