Skip to content

Commit 1e51085

Browse files
committed
Change keyutils-raw types and cleanup api.rs
1 parent a37f67a commit 1e51085

File tree

6 files changed

+289
-348
lines changed

6 files changed

+289
-348
lines changed

keyutils-raw/src/constants.rs

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,59 +27,50 @@
2727
// Ignore rustfmt changes in here. The horizontal alignment is too useful to give up.
2828
#![cfg_attr(rustfmt, rustfmt_skip)]
2929

30-
use crate::types::{key_perm_t, key_serial_t};
30+
use crate::{KeyPermissions, KeyringSerial};
3131

3232
// TODO: change these to &CStr when const fns get unblocked.
3333
pub const KEY_TYPE_KEYRING: &str = "keyring";
3434
pub const KEY_TYPE_USER: &str = "user";
3535
pub const KEY_TYPE_LOGON: &str = "logon";
3636
pub const KEY_TYPE_BIG_KEY: &str = "big_key";
3737

38-
pub const KEY_SPEC_THREAD_KEYRING: key_serial_t = -1;
39-
pub const KEY_SPEC_PROCESS_KEYRING: key_serial_t = -2;
40-
pub const KEY_SPEC_SESSION_KEYRING: key_serial_t = -3;
41-
pub const KEY_SPEC_USER_KEYRING: key_serial_t = -4;
42-
pub const KEY_SPEC_USER_SESSION_KEYRING: key_serial_t = -5;
43-
pub const KEY_SPEC_GROUP_KEYRING: key_serial_t = -6;
44-
pub const KEY_SPEC_REQKEY_AUTH_KEY: key_serial_t = -7;
38+
pub const KEY_SPEC_THREAD_KEYRING: KeyringSerial = unsafe { KeyringSerial::new_unchecked(-1) };
39+
pub const KEY_SPEC_PROCESS_KEYRING: KeyringSerial = unsafe { KeyringSerial::new_unchecked(-2) };
40+
pub const KEY_SPEC_SESSION_KEYRING: KeyringSerial = unsafe { KeyringSerial::new_unchecked(-3) };
41+
pub const KEY_SPEC_USER_KEYRING: KeyringSerial = unsafe { KeyringSerial::new_unchecked(-4) };
42+
pub const KEY_SPEC_USER_SESSION_KEYRING: KeyringSerial = unsafe { KeyringSerial::new_unchecked(-5) };
43+
pub const KEY_SPEC_GROUP_KEYRING: KeyringSerial = unsafe { KeyringSerial::new_unchecked(-6) };
44+
pub const KEY_SPEC_REQKEY_AUTH_KEY: KeyringSerial = unsafe { KeyringSerial::new_unchecked(-7) };
4545

46-
pub const KEY_REQKEY_DEFL_NO_CHANGE: key_serial_t = -1;
47-
pub const KEY_REQKEY_DEFL_DEFAULT: key_serial_t = 0;
48-
pub const KEY_REQKEY_DEFL_THREAD_KEYRING: key_serial_t = 1;
49-
pub const KEY_REQKEY_DEFL_PROCESS_KEYRING: key_serial_t = 2;
50-
pub const KEY_REQKEY_DEFL_SESSION_KEYRING: key_serial_t = 3;
51-
pub const KEY_REQKEY_DEFL_USER_KEYRING: key_serial_t = 4;
52-
pub const KEY_REQKEY_DEFL_USER_SESSION_KEYRING: key_serial_t = 5;
53-
pub const KEY_REQKEY_DEFL_GROUP_KEYRING: key_serial_t = 6;
46+
pub const KEY_POS_VIEW: KeyPermissions = 0x0100_0000; /* possessor can view a key's attributes */
47+
pub const KEY_POS_READ: KeyPermissions = 0x0200_0000; /* possessor can read key payload / view keyring */
48+
pub const KEY_POS_WRITE: KeyPermissions = 0x0400_0000; /* possessor can update key payload / add link to keyring */
49+
pub const KEY_POS_SEARCH: KeyPermissions = 0x0800_0000; /* possessor can find a key in search / search a keyring */
50+
pub const KEY_POS_LINK: KeyPermissions = 0x1000_0000; /* possessor can create a link to a key/keyring */
51+
pub const KEY_POS_SETATTR: KeyPermissions = 0x2000_0000; /* possessor can set key attributes */
52+
pub const KEY_POS_ALL: KeyPermissions = 0x3f00_0000;
5453

55-
pub const KEY_POS_VIEW: key_perm_t = 0x0100_0000; /* possessor can view a key's attributes */
56-
pub const KEY_POS_READ: key_perm_t = 0x0200_0000; /* possessor can read key payload / view keyring */
57-
pub const KEY_POS_WRITE: key_perm_t = 0x0400_0000; /* possessor can update key payload / add link to keyring */
58-
pub const KEY_POS_SEARCH: key_perm_t = 0x0800_0000; /* possessor can find a key in search / search a keyring */
59-
pub const KEY_POS_LINK: key_perm_t = 0x1000_0000; /* possessor can create a link to a key/keyring */
60-
pub const KEY_POS_SETATTR: key_perm_t = 0x2000_0000; /* possessor can set key attributes */
61-
pub const KEY_POS_ALL: key_perm_t = 0x3f00_0000;
54+
pub const KEY_USR_VIEW: KeyPermissions = 0x0001_0000; /* user permissions... */
55+
pub const KEY_USR_READ: KeyPermissions = 0x0002_0000;
56+
pub const KEY_USR_WRITE: KeyPermissions = 0x0004_0000;
57+
pub const KEY_USR_SEARCH: KeyPermissions = 0x0008_0000;
58+
pub const KEY_USR_LINK: KeyPermissions = 0x0010_0000;
59+
pub const KEY_USR_SETATTR: KeyPermissions = 0x0020_0000;
60+
pub const KEY_USR_ALL: KeyPermissions = 0x003f_0000;
6261

63-
pub const KEY_USR_VIEW: key_perm_t = 0x0001_0000; /* user permissions... */
64-
pub const KEY_USR_READ: key_perm_t = 0x0002_0000;
65-
pub const KEY_USR_WRITE: key_perm_t = 0x0004_0000;
66-
pub const KEY_USR_SEARCH: key_perm_t = 0x0008_0000;
67-
pub const KEY_USR_LINK: key_perm_t = 0x0010_0000;
68-
pub const KEY_USR_SETATTR: key_perm_t = 0x0020_0000;
69-
pub const KEY_USR_ALL: key_perm_t = 0x003f_0000;
62+
pub const KEY_GRP_VIEW: KeyPermissions = 0x0000_0100; /* group permissions... */
63+
pub const KEY_GRP_READ: KeyPermissions = 0x0000_0200;
64+
pub const KEY_GRP_WRITE: KeyPermissions = 0x0000_0400;
65+
pub const KEY_GRP_SEARCH: KeyPermissions = 0x0000_0800;
66+
pub const KEY_GRP_LINK: KeyPermissions = 0x0000_1000;
67+
pub const KEY_GRP_SETATTR: KeyPermissions = 0x0000_2000;
68+
pub const KEY_GRP_ALL: KeyPermissions = 0x0000_3f00;
7069

71-
pub const KEY_GRP_VIEW: key_perm_t = 0x0000_0100; /* group permissions... */
72-
pub const KEY_GRP_READ: key_perm_t = 0x0000_0200;
73-
pub const KEY_GRP_WRITE: key_perm_t = 0x0000_0400;
74-
pub const KEY_GRP_SEARCH: key_perm_t = 0x0000_0800;
75-
pub const KEY_GRP_LINK: key_perm_t = 0x0000_1000;
76-
pub const KEY_GRP_SETATTR: key_perm_t = 0x0000_2000;
77-
pub const KEY_GRP_ALL: key_perm_t = 0x0000_3f00;
78-
79-
pub const KEY_OTH_VIEW: key_perm_t = 0x0000_0001; /* third party permissions... */
80-
pub const KEY_OTH_READ: key_perm_t = 0x0000_0002;
81-
pub const KEY_OTH_WRITE: key_perm_t = 0x0000_0004;
82-
pub const KEY_OTH_SEARCH: key_perm_t = 0x0000_0008;
83-
pub const KEY_OTH_LINK: key_perm_t = 0x0000_0010;
84-
pub const KEY_OTH_SETATTR: key_perm_t = 0x0000_0020;
85-
pub const KEY_OTH_ALL: key_perm_t = 0x0000_003f;
70+
pub const KEY_OTH_VIEW: KeyPermissions = 0x0000_0001; /* third party permissions... */
71+
pub const KEY_OTH_READ: KeyPermissions = 0x0000_0002;
72+
pub const KEY_OTH_WRITE: KeyPermissions = 0x0000_0004;
73+
pub const KEY_OTH_SEARCH: KeyPermissions = 0x0000_0008;
74+
pub const KEY_OTH_LINK: KeyPermissions = 0x0000_0010;
75+
pub const KEY_OTH_SETATTR: KeyPermissions = 0x0000_0020;
76+
pub const KEY_OTH_ALL: KeyPermissions = 0x0000_003f;

keyutils-raw/src/functions.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2525
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

27-
use crate::types::{key_perm_t, key_serial_t};
27+
use crate::{KeyPermissions, KeyringSerial, TimeoutSeconds};
2828

2929
#[rustfmt::skip]
3030
extern "C" {
@@ -33,111 +33,111 @@ extern "C" {
3333
description: *const libc::c_char,
3434
payload: *const libc::c_void,
3535
plen: libc::size_t,
36-
keyring: key_serial_t)
37-
-> key_serial_t;
36+
keyring: KeyringSerial)
37+
-> KeyringSerial;
3838
pub fn request_key(
3939
type_: *const libc::c_char,
4040
description: *const libc::c_char,
4141
callout_info: *const libc::c_char,
42-
keyring: key_serial_t)
43-
-> key_serial_t;
42+
keyring: Option<KeyringSerial>)
43+
-> KeyringSerial;
4444

4545
pub fn keyctl_get_keyring_ID(
46-
id: key_serial_t,
46+
id: KeyringSerial,
4747
create: libc::c_int)
48-
-> key_serial_t;
48+
-> KeyringSerial;
4949
pub fn keyctl_join_session_keyring(
5050
name: *const libc::c_char)
51-
-> key_serial_t;
51+
-> KeyringSerial;
5252
pub fn keyctl_update(
53-
id: key_serial_t,
53+
id: KeyringSerial,
5454
payload: *const libc::c_void,
5555
plen: libc::size_t)
5656
-> libc::c_long;
5757
pub fn keyctl_revoke(
58-
id: key_serial_t)
58+
id: KeyringSerial)
5959
-> libc::c_long;
6060
pub fn keyctl_chown(
61-
id: key_serial_t,
61+
id: KeyringSerial,
6262
uid: libc::uid_t,
6363
gid: libc::gid_t)
6464
-> libc::c_long;
6565
pub fn keyctl_setperm(
66-
id: key_serial_t,
67-
perm: key_perm_t)
66+
id: KeyringSerial,
67+
perm: KeyPermissions)
6868
-> libc::c_long;
6969
pub fn keyctl_describe(
70-
id: key_serial_t,
70+
id: KeyringSerial,
7171
buffer: *mut libc::c_char,
7272
buflen: libc::size_t)
7373
-> libc::c_long;
7474
pub fn keyctl_clear(
75-
ringid: key_serial_t)
75+
ringid: KeyringSerial)
7676
-> libc::c_long;
7777
pub fn keyctl_link(
78-
id: key_serial_t,
79-
ringid: key_serial_t)
78+
id: KeyringSerial,
79+
ringid: KeyringSerial)
8080
-> libc::c_long;
8181
pub fn keyctl_unlink(
82-
id: key_serial_t,
83-
ringid: key_serial_t)
82+
id: KeyringSerial,
83+
ringid: KeyringSerial)
8484
-> libc::c_long;
8585
pub fn keyctl_search(
86-
ringid: key_serial_t,
86+
ringid: KeyringSerial,
8787
type_: *const libc::c_char,
8888
description: *const libc::c_char,
89-
destringid: key_serial_t)
89+
destringid: KeyringSerial)
9090
-> libc::c_long;
9191
pub fn keyctl_read(
92-
id: key_serial_t,
92+
id: KeyringSerial,
9393
buffer: *mut libc::c_char,
9494
buflen: libc::size_t)
9595
-> libc::c_long;
9696
pub fn keyctl_instantiate(
97-
id: key_serial_t,
97+
id: KeyringSerial,
9898
payload: *const libc::c_void,
9999
plen: libc::size_t,
100-
ringid: key_serial_t)
100+
ringid: KeyringSerial)
101101
-> libc::c_long;
102102
pub fn keyctl_negate(
103-
id: key_serial_t,
104-
timeout: libc::c_uint,
105-
ringid: key_serial_t)
103+
id: KeyringSerial,
104+
timeout: TimeoutSeconds,
105+
ringid: KeyringSerial)
106106
-> libc::c_long;
107107
pub fn keyctl_set_reqkey_keyring(
108108
reqkey_defl: libc::c_int)
109109
-> libc::c_long;
110110
pub fn keyctl_set_timeout(
111-
key: key_serial_t,
112-
timeout: libc::c_uint)
111+
key: KeyringSerial,
112+
timeout: TimeoutSeconds)
113113
-> libc::c_long;
114114
pub fn keyctl_assume_authority(
115-
key: key_serial_t)
115+
key: Option<KeyringSerial>)
116116
-> libc::c_long;
117117
pub fn keyctl_get_security(
118-
key: key_serial_t,
118+
key: KeyringSerial,
119119
buffer: *mut libc::c_char,
120120
buflen: libc::size_t)
121121
-> libc::c_long;
122122
//pub fn keyctl_session_to_parent()
123123
// -> libc::c_long;
124124
pub fn keyctl_reject(
125-
id: key_serial_t,
126-
timeout: libc::c_uint,
125+
id: KeyringSerial,
126+
timeout: TimeoutSeconds,
127127
error: libc::c_uint,
128-
ringid: key_serial_t)
128+
ringid: KeyringSerial)
129129
-> libc::c_long;
130130
pub fn keyctl_invalidate(
131-
id: key_serial_t)
131+
id: KeyringSerial)
132132
-> libc::c_long;
133133
pub fn keyctl_get_persistent(
134134
uid: libc::uid_t,
135-
id: key_serial_t)
135+
id: KeyringSerial)
136136
-> libc::c_long;
137137
pub fn keyctl_dh_compute(
138-
private: key_serial_t,
139-
prime: key_serial_t,
140-
base: key_serial_t,
138+
private: KeyringSerial,
139+
prime: KeyringSerial,
140+
base: KeyringSerial,
141141
buffer: *mut libc::c_char,
142142
buflen: libc::size_t)
143143
-> libc::c_long;

keyutils-raw/src/types.rs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,63 @@
2424
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2525
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

27-
#![allow(non_camel_case_types)]
27+
/// Alias for the key_serial_t kernel type, representing a keyring (or key).
28+
pub type KeyringSerial = std::num::NonZeroI32;
2829

29-
pub type key_serial_t = i32;
30-
pub type key_perm_t = u32;
30+
/// Alias for the key_perm_t kernel type, representing a keyring's (or key's)
31+
/// permission bits.
32+
///
33+
/// See `Permission`.
34+
pub type KeyPermissions = u32;
35+
36+
pub type TimeoutSeconds = libc::c_uint;
37+
38+
/// An enumeration for the keyrings which may be set as the default.
39+
///
40+
/// Keys which are implicitly required via syscalls and other operations are
41+
/// placed in the default keyring.
42+
#[derive(Debug, PartialEq, Eq)]
43+
pub enum DefaultKeyring {
44+
/// Do not change the default keyring.
45+
///
46+
/// This may be used to get the current default keyring.
47+
NoChange = -1,
48+
/// Set the thread-specific keyring as the default.
49+
ThreadKeyring = 1,
50+
/// Set the process-specific keyring as the default.
51+
ProcessKeyring = 2,
52+
/// Set the session-specific keyring as the default.
53+
SessionKeyring = 3,
54+
/// Set the user-specific keyring as the default.
55+
UserKeyring = 4,
56+
/// Set the user session-specific keyring as the default.
57+
UserSessionKeyring = 5,
58+
/// Set the user session-specific keyring as the default.
59+
GroupKeyring = 6,
60+
/// Set the default keyring to the default logic.
61+
///
62+
/// Keys will be placed in the first available keyring of:
63+
///
64+
/// - thread-specific
65+
/// - process-specific
66+
/// - session-specific
67+
/// - user-specific
68+
DefaultKeyring = 0,
69+
}
70+
71+
impl From<libc::c_long> for DefaultKeyring {
72+
fn from(id: libc::c_long) -> DefaultKeyring {
73+
use self::DefaultKeyring::*;
74+
match id {
75+
x if x == NoChange as libc::c_long => NoChange,
76+
x if x == ThreadKeyring as libc::c_long => ThreadKeyring,
77+
x if x == ProcessKeyring as libc::c_long => ProcessKeyring,
78+
x if x == SessionKeyring as libc::c_long => SessionKeyring,
79+
x if x == UserKeyring as libc::c_long => UserKeyring,
80+
x if x == UserSessionKeyring as libc::c_long => UserSessionKeyring,
81+
x if x == GroupKeyring as libc::c_long => GroupKeyring,
82+
x if x == DefaultKeyring as libc::c_long => DefaultKeyring,
83+
_ => panic!("Invalid value for a default keyring: {}", id),
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)