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
// Copyright (C) 2014-2015 Mickaël Salaün
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use jail::BindMount;
use std::env;
use std::path::PathBuf;
use std::sync::Arc;
use stemflow::{Action, FileAccess, RefDom, SetAccess};
use super::ArcDomain;

#[derive(Clone, Debug, RustcDecodable, PartialEq)]
pub struct ProfileConfig {
    pub name: String,
    pub fs: FsConfig,
    pub run: RunConfig,
}

#[derive(Clone, Debug, RustcDecodable, PartialEq)]
pub struct FsConfig {
    pub bind: Option<Vec<BindConfig>>,
}

#[derive(Clone, Debug, RustcDecodable, PartialEq)]
pub struct BindConfig {
    // TODO: Force absolute path
    pub path: String,
    pub write: Option<bool>,
}

#[derive(Clone, Debug, RustcDecodable, PartialEq)]
pub struct RunConfig {
    pub cmd: Vec<String>,
}


impl<'a> Into<Vec<Arc<FileAccess>>> for &'a BindConfig {
    /// Assume there is no relative path, otherwise they are ignored
    fn into(self) -> Vec<Arc<FileAccess>> {
        // TODO: Map between outside/src and inside/dst
        let path = PathBuf::from(self.path.clone());
        // TODO: Put the default policy in unique place
        let file_access = if self.write.unwrap_or(false) {
            FileAccess::new_rw(path)
        } else {
            FileAccess::new_ro(path)
        };
        match file_access {
            Ok(fa) => fa.into_iter().map(|x| Arc::new(x)).collect(),
            Err(()) => vec!(),
        }
    }
}

impl Into<Vec<Arc<FileAccess>>> for ProfileConfig {
    fn into(self) -> Vec<Arc<FileAccess>> {
        match self.fs.bind {
            Some(bind) => bind.into_iter().map(|x| Into::<Vec<Arc<FileAccess>>>::into(&x))
                .flat_map(|x| x.into_iter()).collect(),
            None => vec!(),
        }
    }
}

pub struct ProfileDom {
    pub cmd: Vec<String>,
    pub jdom: JailDom,
}

#[derive(Clone)]
pub struct JailDom {
    pub binds: Vec<BindMount>,
    pub dom: ArcDomain,
}

impl From<ArcDomain> for JailDom {
    /// Loosely conversion: merge read and write into read-write, ignore write-only)
    fn from(other: ArcDomain) -> JailDom {
        // TODO: Remove unwrap
        let cwd = env::current_dir().unwrap();
        // For each read access, if the path match a write access, then RW, else RO
        let binds = other.acl.range_read().map(|access_read| {
            let access_write = FileAccess::new(access_read.path.clone(), Action::Write).unwrap();
            let path = cwd.join(access_read.as_ref());
            BindMount::new(path.clone(), path).writable(other.is_allowed(&Arc::new(access_write)))
        }).collect();
        JailDom {
            binds: binds,
            dom: other,
        }
    }
}


#[test]
fn test_get_config_example1() {
    // TODO: Use absolute configuration path
    let c1: ProfileConfig = match super::get_config("./config/profiles/test/example1.toml") {
        Ok(c) => c,
        Err(e) => panic!("{}", e),
    };
    let c2 = ProfileConfig {
        name: "example1".to_string(),
        fs: FsConfig {
            bind: Some(vec!(
                BindConfig {
                    path: "/home".to_string(),
                    write: None,
                },
            )),
        },
        run: RunConfig {
            cmd: vec!("/bin/sh".to_string(), "-c".to_string(), "id".to_string()),
        },
    };
    assert_eq!(c1, c2);
}

#[test]
fn test_get_config_example2() {
    // TODO: Use absolute configuration path
    let c1: ProfileConfig = match super::get_config("./config/profiles/test/example2.toml") {
        Ok(c) => c,
        Err(e) => panic!("{}", e),
    };
    let c2 = ProfileConfig {
        name: "example2".to_string(),
        fs: FsConfig {
            bind: Some(vec!(
                BindConfig {
                    path: "/run".to_string(),
                    write: Some(true),
                },
                BindConfig {
                    path: "/home".to_string(),
                    write: None,
                },
            )),
        },
        run: RunConfig {
            cmd: vec!("/usr/bin/setsid".to_string(), "-c".to_string(), "/bin/sh".to_string()),
        },
    };
    assert_eq!(c1, c2);
}