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
//! Create a more rust-like permissions construct, ported from the unix
//! permissions defined in keyutils.h
use bitflags::bitflags;

/// Construct key permissions
///
/// Usage:
///
/// ```
/// use linux_keyutils::{Permission, KeyPermissions};
///
/// let mut perms = KeyPermissions::new();
/// perms.set_user_perms(Permission::ALL);
/// perms.set_group_perms(Permission::VIEW);
/// ```
pub struct KeyPermissions(u32);

/// Construct key permissions with the builder pattern.
///
/// Usage:
///
/// ```
/// use linux_keyutils::{Permission, KeyPermissionsBuilder};
///
/// let perms = KeyPermissionsBuilder::builder()
///             .user(Permission::ALL)
///             .group(Permission::VIEW)
///             .build();
/// ```
pub struct KeyPermissionsBuilder(KeyPermissions);

bitflags! {
    /// Pre-defined bit-flags to construct permissions easily.
    pub struct Permission: u8 {
        /// Allows viewing a key's attributes
        const VIEW = 0x1;
        /// Allows reading a key's payload / viewing a keyring
        const READ = 0x2;
        /// Allows writing/updating a key's payload / adding a link to keyring
        const WRITE = 0x4;
        /// Allows finding a key in search / searching a keyring
        const SEARCH = 0x8;
        /// Allows creating a link to a key/keyring
        const LINK = 0x10;
        /// Allows setting attributes for a key
        const SETATTR = 0x20;
        /// Allows all actions
        const ALL = 0x3f;
    }
}

impl Default for KeyPermissions {
    fn default() -> Self {
        Self::new()
    }
}

impl KeyPermissions {
    /// Create a new KeyPermissions object, defaults to empty permissions
    pub fn new() -> Self {
        Self(0)
    }

    /// Construct the permissions manually
    pub fn from_u32(raw: u32) -> Self {
        Self(raw)
    }

    /// Obtain the u32 bits for this set
    pub fn bits(&self) -> u32 {
        self.0
    }

    /// Set the permissions available to the key's possessor
    pub fn set_posessor_perms(&mut self, perm: Permission) {
        self.0 &= !(0xFF << 24);
        self.0 += (perm.bits() as u32) << 24;
    }

    /// Set the permissions available to the key's owning user (UID)
    pub fn set_user_perms(&mut self, perm: Permission) {
        self.0 &= !(0xFF << 16);
        self.0 += (perm.bits() as u32) << 16;
    }

    /// Set the permissions available to the key's owning group (GID)
    pub fn set_group_perms(&mut self, perm: Permission) {
        self.0 &= !(0xFF << 8);
        self.0 += (perm.bits() as u32) << 8;
    }

    /// Set the permissions available to any 3rd party
    pub fn set_world_perms(&mut self, perm: Permission) {
        self.0 &= !0xFF;
        self.0 += perm.bits() as u32;
    }
}

impl KeyPermissionsBuilder {
    /// Start a KeyPermissionsBuilder
    pub fn builder() -> Self {
        Self(KeyPermissions::default())
    }

    /// Set the permissions available to the key's possessor
    pub fn posessor(mut self, perm: Permission) -> Self {
        self.0.set_posessor_perms(perm);
        self
    }

    /// Set the permissions available to the key's owning user (UID)
    pub fn user(mut self, perm: Permission) -> Self {
        self.0.set_user_perms(perm);
        self
    }

    /// Set the permissions available to the key's owning group (GID)
    pub fn group(mut self, perm: Permission) -> Self {
        self.0.set_group_perms(perm);
        self
    }

    /// Set the permissions available to any 3rd party
    pub fn world(mut self, perm: Permission) -> Self {
        self.0.set_world_perms(perm);
        self
    }

    /// Finish the build and obtain the created KeyPermissions
    pub fn build(self) -> KeyPermissions {
        self.0
    }
}

#[test]
fn test_posessor_perms() {
    let mut perm = KeyPermissions::default();

    // Initial
    perm.set_posessor_perms(Permission::ALL);
    assert_eq!(perm.0, 0x3f000000);

    // Update
    perm.set_posessor_perms(Permission::SEARCH);
    assert_eq!(perm.0, 0x08000000);

    // Combination
    perm.set_posessor_perms(Permission::SEARCH | Permission::VIEW);
    assert_eq!(perm.0, 0x09000000);

    // Combination two
    perm.set_posessor_perms(
        Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
    );
    assert_eq!(perm.0, 0x27000000);
}

#[test]
fn test_user_perms() {
    let mut perm = KeyPermissions::default();

    // Initial
    perm.set_user_perms(Permission::ALL);
    assert_eq!(perm.0, 0x003f0000);

    // Update
    perm.set_user_perms(Permission::SEARCH);
    assert_eq!(perm.0, 0x00080000);

    // Combination
    perm.set_user_perms(Permission::SEARCH | Permission::VIEW);
    assert_eq!(perm.0, 0x00090000);

    // Combination2
    perm.set_user_perms(
        Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
    );
    assert_eq!(perm.0, 0x00270000);
}

#[test]
fn test_group_perms() {
    let mut perm = KeyPermissions::default();

    // Initial
    perm.set_group_perms(Permission::ALL);
    assert_eq!(perm.0, 0x00003f00);

    // Update
    perm.set_group_perms(Permission::SEARCH);
    assert_eq!(perm.0, 0x00000800);

    // Combination
    perm.set_group_perms(Permission::SEARCH | Permission::VIEW);
    assert_eq!(perm.0, 0x00000900);

    // Combination2
    perm.set_group_perms(
        Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
    );
    assert_eq!(perm.0, 0x00002700);
}

#[test]
fn test_world_perms() {
    let mut perm = KeyPermissions::default();

    // Initial
    perm.set_world_perms(Permission::ALL);
    assert_eq!(perm.0, 0x0000003f);

    // Update
    perm.set_world_perms(Permission::SEARCH);
    assert_eq!(perm.0, 0x00000008);

    // Combination
    perm.set_world_perms(Permission::SEARCH | Permission::VIEW);
    assert_eq!(perm.0, 0x00000009);

    // Combination2
    perm.set_world_perms(
        Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
    );
    assert_eq!(perm.0, 0x00000027);
}

#[test]
fn test_combined_perms() {
    let mut perm = KeyPermissions::default();

    // Posessor
    perm.set_posessor_perms(Permission::ALL);
    assert_eq!(perm.0, 0x3f000000);

    // User
    perm.set_user_perms(Permission::VIEW | Permission::READ | Permission::WRITE);
    assert_eq!(perm.0, 0x3f070000);

    // Group
    perm.set_group_perms(Permission::SEARCH | Permission::VIEW);
    assert_eq!(perm.0, 0x3f070900);

    // World
    perm.set_world_perms(
        Permission::SETATTR | Permission::VIEW | Permission::READ | Permission::WRITE,
    );
    assert_eq!(perm.0, 0x3f070927);
}