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
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 {
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 {
fn into(self) -> Vec<Arc<FileAccess>> {
let path = PathBuf::from(self.path.clone());
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 {
fn from(other: ArcDomain) -> JailDom {
let cwd = env::current_dir().unwrap();
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() {
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() {
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);
}