Skip to content

Commit cbd4ba8

Browse files
authored
Merge pull request #29 from josephlr/type
Cleanup keyutils types and api.rs
2 parents e73f490 + ddcb564 commit cbd4ba8

File tree

17 files changed

+381
-424
lines changed

17 files changed

+381
-424
lines changed

.cirrus.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ rustfmt_task:
1919
linux_task:
2020
matrix:
2121
- container:
22-
image: rust:1.32.0
22+
image: rust:1.34.0
2323
- container:
2424
image: rust:latest
2525
- allow_failures: true

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ readme = "README.md"
1111
edition = "2018"
1212

1313
[workspace]
14-
members = ["libkeyutils-sys"]
14+
members = ["keyutils-raw"]
1515

1616
[dependencies]
1717
bitflags = "1.0.4"
1818
errno = "0.2"
19-
libkeyutils-sys = { path = "libkeyutils-sys" }
19+
keyutils-raw = { path = "keyutils-raw" }
2020
log = "0.4.4"
2121

2222
libc = "0.2"

libkeyutils-sys/Cargo.toml renamed to keyutils-raw/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
2-
name = "libkeyutils-sys"
3-
version = "0.3.1"
2+
name = "keyutils-raw"
3+
version = "0.4.0"
44
authors = ["Ben Boeckel <[email protected]>"]
55
license = "BSD-3-Clause"
6-
description = "FFI bindings to libkeyutils."
6+
description = "Raw bindings to Linux keyring syscalls"
77
repository = "https://github.com/mathstuf/rust-keyutils.git"
88
homepage = "https://github.com/mathstuf/rust-keyutils"
99
keywords = ["keyutils"]
File renamed without changes.

keyutils-raw/src/constants.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2018, Ben Boeckel
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without modification,
5+
// are permitted provided that the following conditions are met:
6+
//
7+
// * Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
// * Neither the name of this project nor the names of its contributors
13+
// may be used to endorse or promote products derived from this software
14+
// without specific prior written permission.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
// Ignore rustfmt changes in here. The horizontal alignment is too useful to give up.
28+
#![cfg_attr(rustfmt, rustfmt_skip)]
29+
30+
use crate::{KeyPermissions, KeyringSerial};
31+
32+
// TODO: change these to &CStr when const fns get unblocked.
33+
pub const KEY_TYPE_KEYRING: &str = "keyring";
34+
pub const KEY_TYPE_USER: &str = "user";
35+
pub const KEY_TYPE_LOGON: &str = "logon";
36+
pub const KEY_TYPE_BIG_KEY: &str = "big_key";
37+
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) };
45+
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;
53+
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;
61+
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;
69+
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;

libkeyutils-sys/src/functions.rs renamed to keyutils-raw/src/functions.rs

+42-39
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
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};
28+
29+
// Remove when rust-lang/rust#60300 is in stable
30+
#[allow(improper_ctypes)]
2831

2932
#[rustfmt::skip]
3033
extern "C" {
@@ -33,111 +36,111 @@ extern "C" {
3336
description: *const libc::c_char,
3437
payload: *const libc::c_void,
3538
plen: libc::size_t,
36-
keyring: key_serial_t)
37-
-> key_serial_t;
39+
keyring: KeyringSerial)
40+
-> KeyringSerial;
3841
pub fn request_key(
3942
type_: *const libc::c_char,
4043
description: *const libc::c_char,
4144
callout_info: *const libc::c_char,
42-
keyring: key_serial_t)
43-
-> key_serial_t;
45+
keyring: Option<KeyringSerial>)
46+
-> KeyringSerial;
4447

4548
pub fn keyctl_get_keyring_ID(
46-
id: key_serial_t,
49+
id: KeyringSerial,
4750
create: libc::c_int)
48-
-> key_serial_t;
51+
-> KeyringSerial;
4952
pub fn keyctl_join_session_keyring(
5053
name: *const libc::c_char)
51-
-> key_serial_t;
54+
-> KeyringSerial;
5255
pub fn keyctl_update(
53-
id: key_serial_t,
56+
id: KeyringSerial,
5457
payload: *const libc::c_void,
5558
plen: libc::size_t)
5659
-> libc::c_long;
5760
pub fn keyctl_revoke(
58-
id: key_serial_t)
61+
id: KeyringSerial)
5962
-> libc::c_long;
6063
pub fn keyctl_chown(
61-
id: key_serial_t,
64+
id: KeyringSerial,
6265
uid: libc::uid_t,
6366
gid: libc::gid_t)
6467
-> libc::c_long;
6568
pub fn keyctl_setperm(
66-
id: key_serial_t,
67-
perm: key_perm_t)
69+
id: KeyringSerial,
70+
perm: KeyPermissions)
6871
-> libc::c_long;
6972
pub fn keyctl_describe(
70-
id: key_serial_t,
73+
id: KeyringSerial,
7174
buffer: *mut libc::c_char,
7275
buflen: libc::size_t)
7376
-> libc::c_long;
7477
pub fn keyctl_clear(
75-
ringid: key_serial_t)
78+
ringid: KeyringSerial)
7679
-> libc::c_long;
7780
pub fn keyctl_link(
78-
id: key_serial_t,
79-
ringid: key_serial_t)
81+
id: KeyringSerial,
82+
ringid: KeyringSerial)
8083
-> libc::c_long;
8184
pub fn keyctl_unlink(
82-
id: key_serial_t,
83-
ringid: key_serial_t)
85+
id: KeyringSerial,
86+
ringid: KeyringSerial)
8487
-> libc::c_long;
8588
pub fn keyctl_search(
86-
ringid: key_serial_t,
89+
ringid: KeyringSerial,
8790
type_: *const libc::c_char,
8891
description: *const libc::c_char,
89-
destringid: key_serial_t)
92+
destringid: KeyringSerial)
9093
-> libc::c_long;
9194
pub fn keyctl_read(
92-
id: key_serial_t,
95+
id: KeyringSerial,
9396
buffer: *mut libc::c_char,
9497
buflen: libc::size_t)
9598
-> libc::c_long;
9699
pub fn keyctl_instantiate(
97-
id: key_serial_t,
100+
id: KeyringSerial,
98101
payload: *const libc::c_void,
99102
plen: libc::size_t,
100-
ringid: key_serial_t)
103+
ringid: KeyringSerial)
101104
-> libc::c_long;
102105
pub fn keyctl_negate(
103-
id: key_serial_t,
104-
timeout: libc::c_uint,
105-
ringid: key_serial_t)
106+
id: KeyringSerial,
107+
timeout: TimeoutSeconds,
108+
ringid: KeyringSerial)
106109
-> libc::c_long;
107110
pub fn keyctl_set_reqkey_keyring(
108111
reqkey_defl: libc::c_int)
109112
-> libc::c_long;
110113
pub fn keyctl_set_timeout(
111-
key: key_serial_t,
112-
timeout: libc::c_uint)
114+
key: KeyringSerial,
115+
timeout: TimeoutSeconds)
113116
-> libc::c_long;
114117
pub fn keyctl_assume_authority(
115-
key: key_serial_t)
118+
key: Option<KeyringSerial>)
116119
-> libc::c_long;
117120
pub fn keyctl_get_security(
118-
key: key_serial_t,
121+
key: KeyringSerial,
119122
buffer: *mut libc::c_char,
120123
buflen: libc::size_t)
121124
-> libc::c_long;
122125
//pub fn keyctl_session_to_parent()
123126
// -> libc::c_long;
124127
pub fn keyctl_reject(
125-
id: key_serial_t,
126-
timeout: libc::c_uint,
128+
id: KeyringSerial,
129+
timeout: TimeoutSeconds,
127130
error: libc::c_uint,
128-
ringid: key_serial_t)
131+
ringid: KeyringSerial)
129132
-> libc::c_long;
130133
pub fn keyctl_invalidate(
131-
id: key_serial_t)
134+
id: KeyringSerial)
132135
-> libc::c_long;
133136
pub fn keyctl_get_persistent(
134137
uid: libc::uid_t,
135-
id: key_serial_t)
138+
id: KeyringSerial)
136139
-> libc::c_long;
137140
pub fn keyctl_dh_compute(
138-
private: key_serial_t,
139-
prime: key_serial_t,
140-
base: key_serial_t,
141+
private: KeyringSerial,
142+
prime: KeyringSerial,
143+
base: KeyringSerial,
141144
buffer: *mut libc::c_char,
142145
buflen: libc::size_t)
143146
-> libc::c_long;
File renamed without changes.

keyutils-raw/src/types.rs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2018, Ben Boeckel
2+
// All rights reserved.
3+
//
4+
// Redistribution and use in source and binary forms, with or without modification,
5+
// are permitted provided that the following conditions are met:
6+
//
7+
// * Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
// * Neither the name of this project nor the names of its contributors
13+
// may be used to endorse or promote products derived from this software
14+
// without specific prior written permission.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
27+
use std::convert::TryFrom;
28+
use std::num::NonZeroI32;
29+
30+
/// Alias for the key_serial_t kernel type, representing a keyring (or key).
31+
pub type KeyringSerial = NonZeroI32;
32+
33+
/// Alias for the key_perm_t kernel type, representing a keyring's (or key's)
34+
/// permission bits.
35+
///
36+
/// See `Permission`.
37+
pub type KeyPermissions = u32;
38+
39+
pub type TimeoutSeconds = libc::c_uint;
40+
41+
/// An enumeration for the keyrings which may be set as the default.
42+
///
43+
/// Keys which are implicitly required via syscalls and other operations are
44+
/// placed in the default keyring.
45+
#[derive(Debug, PartialEq, Eq)]
46+
pub enum DefaultKeyring {
47+
/// Do not change the default keyring.
48+
///
49+
/// This may be used to get the current default keyring.
50+
NoChange = -1,
51+
/// Set the thread-specific keyring as the default.
52+
ThreadKeyring = 1,
53+
/// Set the process-specific keyring as the default.
54+
ProcessKeyring = 2,
55+
/// Set the session-specific keyring as the default.
56+
SessionKeyring = 3,
57+
/// Set the user-specific keyring as the default.
58+
UserKeyring = 4,
59+
/// Set the user session-specific keyring as the default.
60+
UserSessionKeyring = 5,
61+
/// Set the user session-specific keyring as the default.
62+
GroupKeyring = 6,
63+
/// Set the default keyring to the default logic.
64+
///
65+
/// Keys will be placed in the first available keyring of:
66+
///
67+
/// - thread-specific
68+
/// - process-specific
69+
/// - session-specific
70+
/// - user-specific
71+
DefaultKeyring = 0,
72+
}
73+
74+
#[derive(Debug, PartialEq, Eq)]
75+
pub struct UnknownDefault(libc::c_long);
76+
77+
impl TryFrom<libc::c_long> for DefaultKeyring {
78+
type Error = UnknownDefault;
79+
fn try_from(id: libc::c_long) -> Result<DefaultKeyring, UnknownDefault> {
80+
use self::DefaultKeyring::*;
81+
match id {
82+
x if x == NoChange as libc::c_long => Ok(NoChange),
83+
x if x == ThreadKeyring as libc::c_long => Ok(ThreadKeyring),
84+
x if x == ProcessKeyring as libc::c_long => Ok(ProcessKeyring),
85+
x if x == SessionKeyring as libc::c_long => Ok(SessionKeyring),
86+
x if x == UserKeyring as libc::c_long => Ok(UserKeyring),
87+
x if x == UserSessionKeyring as libc::c_long => Ok(UserSessionKeyring),
88+
x if x == GroupKeyring as libc::c_long => Ok(GroupKeyring),
89+
x if x == DefaultKeyring as libc::c_long => Ok(DefaultKeyring),
90+
x => Err(UnknownDefault(x)),
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)