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
use std::os::raw::c_int;

#[derive(PartialEq,Eq,Debug)]
pub enum Error {
    UnsupportedPlatform,
    Other(c_int),
}

pub enum Promise {
    Audio,
    Chown,
    CPath,
    DiskLabel,
    Dns,
    DPath,
    Drm,
    Exec,
    Fattr,
    Flock,
    Getpw,
    Id,
    Inet,
    Ioctl,
    MCast,
    Pf,
    Proc,
    ProtExec,
    Ps,
    Recvfd,
    Route,
    RPath,
    Sendfd,
    Settime,
    Stdio,
    TMPPath,
    Tty,
    Unix,
    Vminfo,
    Vmm,
    WPath,
}

impl Promise {
    pub fn to_promise_string(&self) -> &'static str {
        return match *self {
            Promise::Audio => "audio",
            Promise::Chown => "chown",
            Promise::CPath => "cpath",
            Promise::DiskLabel => "disklabel",
            Promise::Dns => "dns",
            Promise::DPath => "dpath",
            Promise::Drm => "drm",
            Promise::Exec => "exec",
            Promise::Fattr => "fattr",
            Promise::Flock => "flock",
            Promise::Getpw => "getpw",
            Promise::Id => "id",
            Promise::Inet => "inet",
            Promise::Ioctl => "ioctl",
            Promise::MCast => "mcast",
            Promise::Pf => "pf",
            Promise::Proc => "proc",
            Promise::ProtExec => "prot_exec",
            Promise::Ps => "ps",
            Promise::Recvfd => "recvfd",
            Promise::Route => "route",
            Promise::RPath => "rpath",
            Promise::Sendfd => "sendfd",
            Promise::Settime => "settime",
            Promise::Stdio => "stdio",
            Promise::TMPPath => "tmppath",
            Promise::Tty => "tty",
            Promise::Unix => "unix",
            Promise::Vminfo => "vminfo",
            Promise::Vmm => "vmm",
            Promise::WPath => "wpath",
        };
    }
}

pub trait ToPromiseString {
    fn to_promise_string(&self) -> String;
}

impl ToPromiseString for [Promise] {
    fn to_promise_string(&self) -> String {
        return self.iter()
            .map(|p| p.to_promise_string())
            .collect::<Vec<&'static str>>()
            .join(" ");
    }
}

#[cfg(any(target_os = "bitrig",
          target_os = "openbsd"))]
mod openbsd;

#[cfg(any(target_os = "bitrig",
          target_os = "openbsd"))]
pub use openbsd::pledge_with_paths;

#[cfg(not(any(target_os = "bitrig",
              target_os = "openbsd")))]
pub fn pledge_with_paths(_: &str, _: &[&std::path::Path]) -> Result<(), Error> {
    return Err(Error::UnsupportedPlatform);
}

pub fn pledge(promises: &str) -> Result<(), Error> {
    return pledge_with_paths(promises, &[]);
}

#[macro_export]
macro_rules! pledge {
    ( $( $x:ident ),* ) => {
        {
            let mut promises = Vec::new();
            $(
                promises.push(Promise::$x);
            )*
            let promises_str = promises.to_promise_string();
            pledge(&promises_str)
        }
    };
}

#[cfg(test)]
mod tests {
    use super::{Promise, ToPromiseString, pledge};

    #[test]
    fn test_promise_str() {
        use super::ToPromiseString;

        assert_eq!(vec![].to_promise_string(), "");
        assert_eq!(vec![Promise::Dns].to_promise_string(), "dns");
        assert_eq!(vec![Promise::Stdio, Promise::ProtExec].to_promise_string(),
                   "stdio prot_exec");
    }

    #[test]
    #[cfg(not(any(target_os = "bitrig",
                  target_os = "openbsd")))]
    fn test_pledge_unsupported() {
        use super::Error;
        assert_eq!(pledge![Stdio].unwrap_err(), Error::UnsupportedPlatform);
    }

    #[test]
    #[cfg(any(target_os = "bitrig",
              target_os = "openbsd"))]
    fn test_pledge_supported() {
        pledge![Stdio].unwrap();
        assert!(pledge![Stdio, Audio].is_err());
    }

    #[test]
    #[cfg(target_os = "openbsd")]
    fn test_as_string() {
        if pledge("stdio").is_err() {
            panic!("pledge");
        }
        println!("hello world");
    }
}