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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright (C) 2015-2016 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/>.

#![allow(deprecated)]

use bincode::rustc_serialize::{DecodingResult, EncodingResult};
use getopts::Options;
use jail::{Jail, JailFn, WORKDIR_PARENT};
use jail::util::nest_path;
use mnt::get_mount_writable;
use self::fsm_kage::KageFsm;
use self::fsm_monitor::MonitorFsmInit;
use std::collections::BTreeSet;
use std::fmt;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::Sender;
use stemflow::{Access, Action, SetAccess, FileAccess};
use super::util;
use unix_socket::UnixStream;

mod fsm_kage;
mod fsm_monitor;

#[derive(Debug, RustcDecodable, RustcEncodable)]
pub enum ShimAction {
    Access(AccessRequest),
    List(ListRequest),
}

impl ShimAction {
    pub fn call(self, cmd_tx: Sender<Box<JailFn>>, client: UnixStream) -> Result<(), String> {
        let ret = match self {
            ShimAction::List(req) => {
                match req.check() {
                    Ok(_) => {
                        let bundle = MonitorBundle {
                            request: req,
                            machine: Some(MonitorFsmInit::new(client)),
                        };
                        cmd_tx.send(Box::new(bundle))
                    }
                    Err(e) => return Err(format!("Request error: {}", e)),
                }
            }
            ShimAction::Access(req) => {
                match req.check() {
                    Ok(_) => {
                        let bundle = MonitorBundle {
                            request: req,
                            machine: Some(MonitorFsmInit::new(client)),
                        };
                        cmd_tx.send(Box::new(bundle))
                    }
                    Err(e) => return Err(format!("Request error: {}", e)),
                }
            }
        };
        match ret {
            Ok(_) => Ok(()),
            Err(e) => Err(format!("Failed to spawn shim action: {}", e)),
        }
    }
}

#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
pub struct ListRequest {
    pub path: PathBuf,
}

#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
pub struct ListResponse {
    pub result: Vec<PathBuf>,
}
impl_encdec!(ListResponse);

pub struct MonitorBundle<T> {
    pub request: T,
    // TODO: Remove the Option (need to revamp the JailFn::call() use)
    pub machine: Option<MonitorFsmInit>,
}

impl<T> fmt::Debug for MonitorBundle<T> where T: fmt::Debug {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        write!(out, "MonitorBundle {{ request: {:?} }}", self.request)
    }
}

impl ListRequest {
    pub fn check(&self) -> Result<(), String> {
        util::check_parent_path(&self.path)
    }
}

impl MonitorBundle<ListRequest> {
    fn list<T>(&self, dir: T) -> io::Result<Vec<PathBuf>> where T: AsRef<Path> {
        let mut ret = vec!();
        for file in try!(fs::read_dir(&dir)) {
            match try!(file).path().relative_from(&dir) {
                Some(d) => ret.push(d.to_path_buf()),
                None => warn!("Failed to get relative path"),
            }
        }
        Ok(ret)
    }
}

impl JailFn for MonitorBundle<ListRequest> {
    // TODO: Spawn a dedicated thread
    fn call(&mut self, jail: &mut Jail) {
        if jail.is_confined() {
            warn!("Unauthorized command");
            return;
        }
        let res = self.list(nest_path(WORKDIR_PARENT, &self.request.path));
        let res = ListResponse {
            result: match res {
                Ok(r) => r,
                Err(e) => {
                    warn!("Failed to read directory: {}", e);
                    vec!()
                }
            }
        };
        match self.machine.take() {
            Some(m) => {
                match m.send_list_response(res) {
                    Ok(()) => {}
                    Err(e) => error!("Connection result: {:?}", e),
                }
            }
            None => error!("No connection possible"),
        }
    }
}

/// An `AccessData` always imply at least a read access
#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
pub struct AccessData {
    pub path: PathBuf,
    pub write: bool,
}

impl Into<Vec<Arc<FileAccess>>> for AccessData {
    fn into(self) -> Vec<Arc<FileAccess>> {
        let path = Arc::new(self.path);
        let mut ret = vec!(Arc::new(FileAccess {
            path: path.clone(),
            action: Action::Read,
        }));
        if self.write {
            ret.push(Arc::new(FileAccess {
                path: path.clone(),
                action: Action::Write,
            }));
        }
        ret
    }
}

/// There is two caches:
/// * `granted` is used to prune the `access_data` request hierarchy
/// * `denied` is used to find an exact match for the `access_data` request
/// e.g. Deny /var but allow /var/cache
pub struct AccessCache {
    granted: BTreeSet<Arc<FileAccess>>,
    denied: BTreeSet<Arc<FileAccess>>,
}

impl AccessCache {
    pub fn new() -> AccessCache {
        let mut def_granted = BTreeSet::new();
        def_granted.insert_dedup_all(["/dev", "/proc", "/tmp"].into_iter().flat_map(|p|
            FileAccess::new_rw(p.into()).unwrap()).map(|x| Arc::new(x)));
        AccessCache {
            granted: def_granted,
            denied: BTreeSet::new(),
        }
    }
}

#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
pub struct AccessRequest {
    pub data: AccessData,
    pub get_all_access: bool,
}

impl AccessRequest {
    pub fn check(&self) -> Result<(), String> {
        util::check_parent_path(&self.data.path)
    }

    pub fn new<T>(path: T, write: bool) -> AccessRequest where T: AsRef<Path> {
        AccessRequest {
            data: AccessData {
                path: path.as_ref().to_path_buf(),
                write: write,
            },
            get_all_access: false,
        }
    }
}

#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
pub struct AccessResponse {
    pub new_access: Vec<AccessData>,
}
impl_encdec!(AccessResponse);

impl JailFn for MonitorBundle<AccessRequest> {
    fn call(&mut self, jail: &mut Jail) {
        let acl = if self.request.data.write {
            FileAccess::new_rw(self.request.data.path.clone())
        } else {
            FileAccess::new_ro(self.request.data.path.clone())
        };
        let response = AccessResponse {
            new_access: {
                let ret = match acl {
                    Ok(acl) => {
                        match jail.gain_access(acl) {
                            Ok(new_access) => {
                                debug!("Access granted to {:?}", new_access);
                                new_access
                            }
                            Err(()) => {
                                debug!("Access denied");
                                vec!()
                            }
                        }
                    }
                    Err(()) => {
                        error!("Failed to create an ACL for {:?}", self.request.data);
                        vec!()
                    }
                };
                if self.request.get_all_access {
                    // TODO: Use FileAccess
                    jail.as_ref().binds.iter().map(|x| x.into()).collect()
                } else {
                    ret
                }
            }
        };
        match self.machine.take() {
            Some(m) => {
                match m.send_access_response(response) {
                    Ok(()) => {}
                    Err(e) => error!("Connection result: {:?}", e),
                }
            }
            None => error!("No connection possible"),
        }
    }
}


pub struct ShimKageCmd {
    name: String,
    opts: Options,
}

impl ShimKageCmd {
    pub fn new() -> ShimKageCmd {
        let mut opts = Options::new();
        opts.optflag("h", "help", "Print this message");
        opts.optopt("l", "list", "List a directory from the parent", "DIR");
        opts.optopt("a", "access", "Ask to access a path from the parent", "PATH");
        opts.optflag("w", "write", "Ask for write access");
        ShimKageCmd {
            name: "shim".to_string(),
            opts: opts,
        }
    }

    pub fn list_directory<T>(path: T) -> Result<(), String> where T: AsRef<Path> {
        let req = ListRequest {
            path: path.as_ref().to_path_buf(),
        };
        try!(req.check());

        let machine = try!(KageFsm::new());
        let machine = try!(machine.send_list_request(req));
        let list = try!(machine.recv_list_response()).result;
        // TODO: Add an output Writer like do_dot()
        println!("{}", list.into_iter().map(|x| x.to_string_lossy().into_owned()).collect::<Vec<_>>().connect("\n"));
        Ok(())
    }

    /// @get_all_access: Get back a list of all current access
    pub fn ask_access(request: AccessRequest) -> Result<Vec<AccessData>, String> {
        try!(request.check());
        let machine = try!(KageFsm::new());
        let machine = try!(machine.send_access_request(request));
        Ok(try!(machine.recv_access_response()).new_access)
    }

    pub fn cache_ask_access(access_data: AccessData, cache: &mut AccessCache)
            -> Result<(), String> {
        let acl: Vec<Arc<FileAccess>> = access_data.clone().into();
        // The denied cache must exactly match the request to not ignore a valid (nested) one
        if ! acl.iter().find(|&x| ! ( cache.granted.is_allowed(x) || cache.denied.contains(x) )).is_some() {
            return Ok(());
        }
        // TODO: Merge `found_mount` below
        let found_mount = match get_mount_writable(&access_data.path, access_data.write) {
            Some(m) => if m.file != Path::new("/") {
                let new_access = if access_data.write {
                    FileAccess::new_rw(m.file)
                } else {
                    FileAccess::new_ro(m.file)
                };
                match new_access {
                    Ok(a) => {
                        let _ = cache.granted.insert_dedup_all(a.into_iter().map(|x| Arc::new(x)));
                        true
                    }
                    Err(_) => false,
                }
            } else {
                false
            },
            _ => false
        };
        if found_mount {
            Ok(())
        } else {
            let req = AccessRequest {
                data: access_data,
                get_all_access: cache.granted.is_empty(),
            };

            match ShimKageCmd::ask_access(req) {
                Ok(new_access) => {
                    if new_access.is_empty() {
                        // Access denied
                        for access in acl.into_iter() {
                            let _ = cache.denied.insert(access);
                        }
                    } else {
                        // New access
                        let _ = cache.granted.insert_dedup_all(new_access.into_iter().flat_map(|x| {
                            let i: Vec<Arc<FileAccess>> = x.into();
                            i.into_iter()
                        }));
                    }
                    Ok(())
                }
                Err(e) => {
                    // Cache the request to not replay it
                    for access in acl.into_iter() {
                        let _ = cache.denied.insert(access);
                    }
                    Err(e)
                }
            }
            // TODO: Cleanup included requests if needed (not a big deal because StemJail
            // hints help to get the big picture).
        }
    }
}

impl super::KageCommand for ShimKageCmd {
    fn get_name<'a>(&'a self) -> &'a String {
        &self.name
    }

    fn get_usage(&self) -> String {
        let msg = format!("Usage for the {} command", self.name);
        format!("{}", self.opts.usage(msg.as_ref()))
    }

    fn call(&mut self, args: &Vec<String>) -> Result<(), String> {
        let matches = match self.opts.parse(args.as_slice()) {
            Ok(m) => m,
            Err(e) => return Err(format!("{}", e)),
        };
        if matches.opt_present("help") {
            println!("{}", self.get_usage());
            return Ok(());
        }

        match matches.opt_str("list") {
            Some(path) => {
                check_remaining!(matches);
                return ShimKageCmd::list_directory(PathBuf::from(path));
            }
            None => {}
        }

        match matches.opt_str("access") {
            Some(path) => {
                check_remaining!(matches);
                return match ShimKageCmd::ask_access(
                        AccessRequest::new(path, matches.opt_present("write"))) {
                    Ok(s) => {
                        println!("Gain access: {:?}", s);
                        Ok(())
                    }
                    Err(e) => Err(e),
                }
            }
            None => {}
        }

        Err("No command".into())
    }
}