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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use param;
use sys;
use JailError;
use StoppedJail;
use std::collections::HashMap;
use std::net;
use std::path;
/// Represents a running jail.
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
#[cfg(target_os = "freebsd")]
pub struct RunningJail {
/// The `jid` of the jail
pub jid: i32,
}
/// Represent a running jail.
#[cfg(target_os = "freebsd")]
impl RunningJail {
/// Create a [RunningJail](struct.RunningJail.html) instance given a `jid`.
///
/// No checks will be performed.
///
/// # Examples
///
/// ```
/// use jail::RunningJail;
/// # use jail::StoppedJail;
/// # let jail = StoppedJail::new("/rescue")
/// # .name("testjail_from_jid")
/// # .start()
/// # .expect("could not start jail");
/// # let jid = jail.jid;
///
/// let running = RunningJail::from_jid(jid);
/// # running.kill();
/// ```
pub fn from_jid(jid: i32) -> RunningJail {
RunningJail { jid }
}
/// Create a [RunningJail](struct.RunningJail.html) given the jail `name`.
///
/// The `jid` will be internally resolved using
/// [jail_getid](fn.jail_getid.html).
///
/// # Examples
///
/// ```
/// use jail::RunningJail;
/// # use jail::StoppedJail;
/// # let jail = StoppedJail::new("/rescue")
/// # .name("testjail_from_name")
/// # .start()
/// # .expect("could not start jail");
///
/// let running = RunningJail::from_name("testjail_from_name")
/// .expect("Could not get testjail");
/// #
/// # running.kill();
/// ```
pub fn from_name(name: &str) -> Result<RunningJail, JailError> {
sys::jail_getid(name).map(RunningJail::from_jid)
}
/// Return the jail's `name`.
///
/// # Examples
///
/// ```
/// # use jail::StoppedJail;
/// #
/// # let running = StoppedJail::new("/rescue")
/// # .name("testjail_name")
/// # .start()
/// # .expect("Could not start jail");
/// assert_eq!(running.name().unwrap(), "testjail_name");
/// #
/// # running.kill();
/// ```
pub fn name(self: &RunningJail) -> Result<String, JailError> {
self.param("name")?.unpack_string()
}
/// Return the jail's `path`.
///
/// # Examples
///
/// ```
/// # use jail::StoppedJail;
/// # use std::path::PathBuf;
/// #
/// # let running = StoppedJail::new("/rescue")
/// # .name("testjail_path")
/// # .start()
/// # .expect("Could not start jail");
/// let path = running.path()
/// .expect("Could not get path");
/// # let expected : PathBuf = "/rescue".into();
/// # assert_eq!(path, expected);
/// #
/// # running.kill();
/// ```
pub fn path(self: &RunningJail) -> Result<path::PathBuf, JailError> {
Ok(self.param("path")?.unpack_string()?.into())
}
/// Return the jail's `name`.
///
/// The name will be internall resolved using
/// [jail_getname](fn.jail_getname.html).
///
/// # Examples
///
/// ```
/// # use jail::StoppedJail;
/// #
/// # let running = StoppedJail::new("/rescue")
/// # .name("testjail_name")
/// # .hostname("testjail.example.com")
/// # .start()
/// # .expect("Could not start jail");
/// assert_eq!(running.hostname().unwrap(), "testjail.example.com");
/// #
/// # running.kill();
/// ```
pub fn hostname(self: &RunningJail) -> Result<String, JailError> {
self.param("host.hostname")?.unpack_string()
}
/// Get the IP addresses
///
/// # Examples
/// ```
/// # use jail::StoppedJail;
/// # use std::net::IpAddr;
/// # let running = StoppedJail::new("/rescue")
/// # .name("testjail_ip")
/// # .ip("127.0.1.2".parse().unwrap())
/// # .ip("fe80::2".parse().unwrap())
/// # .start()
/// # .expect("Could not start jail");
/// let ips = running.ips()
/// .expect("could not get ip addresses");
/// assert_eq!(ips[0], "127.0.1.2".parse::<IpAddr>().unwrap());
/// assert_eq!(ips[1], "fe80::2".parse::<IpAddr>().unwrap());
/// # running.kill();
/// ```
pub fn ips(self: &RunningJail) -> Result<Vec<net::IpAddr>, JailError> {
let mut ips: Vec<net::IpAddr> = vec![];
ips.extend(
self.param("ip4.addr")?
.unpack_ipv4()?
.iter()
.cloned()
.map(net::IpAddr::V4),
);
ips.extend(
self.param("ip6.addr")?
.unpack_ipv6()?
.iter()
.cloned()
.map(net::IpAddr::V6),
);
Ok(ips)
}
/// Return a jail parameter.
///
/// # Examples
/// ```
/// # use jail::StoppedJail;
/// # let running = StoppedJail::new("/rescue")
/// # .start().unwrap();
/// #
/// let hostuuid = running.param("host.hostuuid")
/// .expect("could not get jail hostuuid");
/// #
/// # println!("jail uuid: {:?}", hostuuid);
/// # running.kill();
/// ```
pub fn param(self: &Self, name: &str) -> Result<param::Value, JailError> {
param::get(self.jid, name)
}
/// Return a HashMap of all jail parameters.
///
/// # Examples
/// ```
/// use jail::param;
/// # use jail::StoppedJail;
/// # let running = StoppedJail::new("/rescue")
/// # .name("testjail_params")
/// # .param("allow.raw_sockets", param::Value::Int(1))
/// # .start()
/// # .expect("could not start jail");
///
/// let params = running.params()
/// .expect("could not get all parameters");
///
/// assert_eq!(
/// params.get("allow.raw_sockets"),
/// Some(¶m::Value::Int(1))
/// );
/// # running.kill().expect("could not stop jail");
/// ```
pub fn params(self: &Self) -> Result<HashMap<String, param::Value>, JailError> {
param::get_all(self.jid)
}
/// Set a jail parameter.
///
/// # Examples
/// ```
/// # use jail::StoppedJail;
/// # let running = StoppedJail::new("/rescue")
/// # .start().unwrap();
/// #
/// use jail::param;
/// running.param_set("allow.raw_sockets", param::Value::Int(1))
/// .expect("could not set parameter");
/// # let readback = running.param("allow.raw_sockets")
/// # .expect("could not read back value");
/// # assert_eq!(readback, param::Value::Int(1));
/// # running.kill();
/// ```
pub fn param_set(self: &Self, name: &str, value: param::Value) -> Result<(), JailError> {
param::set(self.jid, name, value)
}
/// Kill a running jail, consuming it.
///
/// This will kill all processes belonging to the jail, and remove any
/// children of that jail.
///
/// # Examples
///
/// ```
/// # use jail::StoppedJail;
/// # let running = StoppedJail::new("/rescue")
/// # .start().unwrap();
/// running.kill();
/// ```
pub fn kill(self: RunningJail) -> Result<(), JailError> {
sys::jail_remove(self.jid).and_then(|_| Ok(()))
}
/// Create a StoppedJail from a RunningJail, while not consuming the
/// RunningJail.
///
/// This can be used to clone the config from a RunningJail.
///
/// # Examples
///
/// ```
/// # use jail::StoppedJail;
/// # let running = StoppedJail::new("/rescue")
/// # .name("testjail_save")
/// # .hostname("testjail_save.example.com")
/// # .start()
/// # .unwrap();
/// let stopped = running
/// .save()
/// .expect("could not save jail configuration");
///
/// assert_eq!(stopped.name, Some("testjail_save".into()));
/// assert_eq!(stopped.hostname, Some("testjail_save.example.com".into()));
/// # running.kill().unwrap();
/// ```
pub fn save(self: &RunningJail) -> Result<StoppedJail, JailError> {
let mut stopped = StoppedJail::new(self.path()?);
stopped.name = self.name().ok();
stopped.hostname = self.hostname().ok();
stopped.ips = self.ips()?;
stopped.params = self.params()?;
Ok(stopped)
}
/// Stop a jail, keeping its configuration in a StoppedJail.
///
/// This is a wrapper around `save` and `kill`.
///
/// # Examples
///
/// ```
/// # use jail::StoppedJail;
/// # let running = StoppedJail::new("/rescue")
/// # .name("testjail_stop")
/// # .hostname("testjail_stop.example.com")
/// # .start()
/// # .unwrap();
/// let stopped = running
/// .stop()
/// .expect("failed to stop jail");
///
/// //assert_eq!(stopped.name, Some("testjail_save".into()));
/// //assert_eq!(stopped.hostname, Some("testjail_save.example.com".into()));
/// ```
pub fn stop(self: RunningJail) -> Result<StoppedJail, JailError> {
let stopped = self.save()?;
self.kill()?;
Ok(stopped)
}
/// Restart a jail by stopping it and starting it again
///
/// This is a wrapper around `RunningJail::stop` and `StoppedJail::start`
///
/// # Examples
///
/// ```
/// # use jail::StoppedJail;
/// # let running = StoppedJail::new("/rescue")
/// # .start()
/// # .unwrap();
///
/// let old_jid = running.jid;
/// let running = running.restart()
/// .expect("failed to restart jail");
/// assert!(running.jid != old_jid);
///
/// # running.kill();
/// ```
pub fn restart(self: RunningJail) -> Result<RunningJail, JailError> {
let stopped = self.stop()?;
stopped.start()
}
/// Returns an Iterator over all running jails on this host.
///
/// # Examples
///
/// ```
/// use jail::RunningJail;
/// # use jail::StoppedJail;
/// # let mut running_jails: Vec<RunningJail> = (1..5)
/// # .map(|i| {
/// # StoppedJail::new("/rescue")
/// # .name(format!("testjail_iterate_{}", i))
/// # .start()
/// # .expect("failed to start jail")
/// # })
/// # .collect();
///
/// for running in RunningJail::all() {
/// println!("jail: {}", running.name().unwrap());
/// }
/// #
/// # for to_kill in running_jails.drain(..) {
/// # to_kill.kill().expect("failed to kill jail");
/// # }
/// ```
pub fn all() -> RunningJails {
RunningJails::default()
}
}
/// An Iterator over running Jails
///
/// See [RunningJail::all()](struct.RunningJail.html#method.all) for a usage
/// example.
#[cfg(target_os = "freebsd")]
pub struct RunningJails {
lastjid: i32,
}
#[cfg(target_os = "freebsd")]
impl Default for RunningJails {
fn default() -> Self {
RunningJails { lastjid: 0 }
}
}
#[cfg(target_os = "freebsd")]
impl RunningJails {
pub fn new() -> Self {
RunningJails::default()
}
}
#[cfg(target_os = "freebsd")]
impl Iterator for RunningJails {
type Item = RunningJail;
fn next(&mut self) -> Option<RunningJail> {
let jid = match sys::jail_nextjid(self.lastjid) {
Ok(j) => j,
Err(_) => return None,
};
self.lastjid = jid;
Some(RunningJail { jid })
}
}