linux-keyctl 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- linux_keyctl/__init__.py +661 -0
- linux_keyctl/_syscall.py +116 -0
- linux_keyctl/py.typed +0 -0
- linux_keyctl-1.1.0.dist-info/METADATA +88 -0
- linux_keyctl-1.1.0.dist-info/RECORD +8 -0
- linux_keyctl-1.1.0.dist-info/WHEEL +5 -0
- linux_keyctl-1.1.0.dist-info/licenses/LICENSE +674 -0
- linux_keyctl-1.1.0.dist-info/top_level.txt +1 -0
linux_keyctl/__init__.py
ADDED
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
2
|
+
# Copyright (C) 2026 Juan Domingo Martín
|
|
3
|
+
"""Thin interface to the Linux kernel key management facility.
|
|
4
|
+
|
|
5
|
+
Each function maps to one ``libkeyutils`` operation and is performed
|
|
6
|
+
with the ``add_key(2)``, ``request_key(2)`` or ``keyctl(2)`` system
|
|
7
|
+
call; ``libkeyutils`` itself is not used. Semantics and permission
|
|
8
|
+
checks are the kernel's (see e.g. ``man keyctl_read``).
|
|
9
|
+
|
|
10
|
+
Conventions shared by every function:
|
|
11
|
+
|
|
12
|
+
* Key and keyring IDs are ``int`` in the C ``int`` range; the
|
|
13
|
+
``KEY_SPEC_*`` constants are accepted wherever an ID is.
|
|
14
|
+
* Descriptions, key types and names are ``bytes`` without NUL bytes;
|
|
15
|
+
payloads are ``bytes`` and may contain NUL bytes.
|
|
16
|
+
* A kernel failure raises ``OSError(errno, os.strerror(errno))``, so
|
|
17
|
+
Python selects the usual subclass (``PermissionError`` for
|
|
18
|
+
``EACCES``, ``FileNotFoundError`` for ``ENOENT``, ...). Key-specific
|
|
19
|
+
errors such as ``ENOKEY`` or ``EKEYREVOKED`` raise plain ``OSError``.
|
|
20
|
+
* Invalid arguments raise ``TypeError``, ``ValueError`` or
|
|
21
|
+
``OverflowError`` before any system call. No message contains a
|
|
22
|
+
payload or description.
|
|
23
|
+
* Where the platform cannot make the system calls, importing still
|
|
24
|
+
works and every call raises ``OSError`` with ``ENOSYS``.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import collections.abc as _abc
|
|
28
|
+
import ctypes as _ctypes
|
|
29
|
+
import errno as _errno
|
|
30
|
+
import os as _os
|
|
31
|
+
import struct as _struct
|
|
32
|
+
import typing as _typing
|
|
33
|
+
|
|
34
|
+
from . import _syscall as _sys
|
|
35
|
+
|
|
36
|
+
__version__ = "1.1.0"
|
|
37
|
+
|
|
38
|
+
# Special keyring IDs.
|
|
39
|
+
KEY_SPEC_THREAD_KEYRING = -1
|
|
40
|
+
KEY_SPEC_PROCESS_KEYRING = -2
|
|
41
|
+
KEY_SPEC_SESSION_KEYRING = -3
|
|
42
|
+
KEY_SPEC_USER_KEYRING = -4
|
|
43
|
+
KEY_SPEC_USER_SESSION_KEYRING = -5
|
|
44
|
+
KEY_SPEC_GROUP_KEYRING = -6
|
|
45
|
+
KEY_SPEC_REQKEY_AUTH_KEY = -7
|
|
46
|
+
KEY_SPEC_REQUESTOR_KEYRING = -8
|
|
47
|
+
|
|
48
|
+
# Request-key default keyrings.
|
|
49
|
+
KEY_REQKEY_DEFL_NO_CHANGE = -1
|
|
50
|
+
KEY_REQKEY_DEFL_DEFAULT = 0
|
|
51
|
+
KEY_REQKEY_DEFL_THREAD_KEYRING = 1
|
|
52
|
+
KEY_REQKEY_DEFL_PROCESS_KEYRING = 2
|
|
53
|
+
KEY_REQKEY_DEFL_SESSION_KEYRING = 3
|
|
54
|
+
KEY_REQKEY_DEFL_USER_KEYRING = 4
|
|
55
|
+
KEY_REQKEY_DEFL_USER_SESSION_KEYRING = 5
|
|
56
|
+
KEY_REQKEY_DEFL_GROUP_KEYRING = 6
|
|
57
|
+
KEY_REQKEY_DEFL_REQUESTOR_KEYRING = 7
|
|
58
|
+
|
|
59
|
+
# Move flags.
|
|
60
|
+
KEYCTL_MOVE_EXCL = 0x00000001
|
|
61
|
+
|
|
62
|
+
# Permission bits: possessor, user, group, other.
|
|
63
|
+
KEY_POS_VIEW = 0x01000000
|
|
64
|
+
KEY_POS_READ = 0x02000000
|
|
65
|
+
KEY_POS_WRITE = 0x04000000
|
|
66
|
+
KEY_POS_SEARCH = 0x08000000
|
|
67
|
+
KEY_POS_LINK = 0x10000000
|
|
68
|
+
KEY_POS_SETATTR = 0x20000000
|
|
69
|
+
KEY_POS_ALL = 0x3F000000
|
|
70
|
+
|
|
71
|
+
KEY_USR_VIEW = 0x00010000
|
|
72
|
+
KEY_USR_READ = 0x00020000
|
|
73
|
+
KEY_USR_WRITE = 0x00040000
|
|
74
|
+
KEY_USR_SEARCH = 0x00080000
|
|
75
|
+
KEY_USR_LINK = 0x00100000
|
|
76
|
+
KEY_USR_SETATTR = 0x00200000
|
|
77
|
+
KEY_USR_ALL = 0x003F0000
|
|
78
|
+
|
|
79
|
+
KEY_GRP_VIEW = 0x00000100
|
|
80
|
+
KEY_GRP_READ = 0x00000200
|
|
81
|
+
KEY_GRP_WRITE = 0x00000400
|
|
82
|
+
KEY_GRP_SEARCH = 0x00000800
|
|
83
|
+
KEY_GRP_LINK = 0x00001000
|
|
84
|
+
KEY_GRP_SETATTR = 0x00002000
|
|
85
|
+
KEY_GRP_ALL = 0x00003F00
|
|
86
|
+
|
|
87
|
+
KEY_OTH_VIEW = 0x00000001
|
|
88
|
+
KEY_OTH_READ = 0x00000002
|
|
89
|
+
KEY_OTH_WRITE = 0x00000004
|
|
90
|
+
KEY_OTH_SEARCH = 0x00000008
|
|
91
|
+
KEY_OTH_LINK = 0x00000010
|
|
92
|
+
KEY_OTH_SETATTR = 0x00000020
|
|
93
|
+
KEY_OTH_ALL = 0x0000003F
|
|
94
|
+
|
|
95
|
+
# Key types.
|
|
96
|
+
KEY_TYPE_USER = b"user"
|
|
97
|
+
KEY_TYPE_KEYRING = b"keyring"
|
|
98
|
+
KEY_TYPE_BIG_KEY = b"big_key"
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class KeyInfo(_typing.NamedTuple):
|
|
102
|
+
"""Parsed result of :func:`describe_key`, as returned by
|
|
103
|
+
:func:`key_info`. Fields unpack in this order."""
|
|
104
|
+
|
|
105
|
+
#: Key type, e.g. ``b"user"`` or ``b"keyring"``.
|
|
106
|
+
key_type: bytes
|
|
107
|
+
#: Owning user ID.
|
|
108
|
+
uid: int
|
|
109
|
+
#: Owning group ID.
|
|
110
|
+
gid: int
|
|
111
|
+
#: Permission mask, a combination of the ``KEY_POS_*``,
|
|
112
|
+
#: ``KEY_USR_*``, ``KEY_GRP_*`` and ``KEY_OTH_*`` bits.
|
|
113
|
+
permissions: int
|
|
114
|
+
#: Key description, without the trailing NUL; may contain ``;``.
|
|
115
|
+
description: bytes
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# Argument checks. Messages never contain the value: it may be secret.
|
|
119
|
+
|
|
120
|
+
_INT_MIN, _INT_MAX = -(2**31), 2**31 - 1
|
|
121
|
+
_UINT_MAX = 2**32 - 1
|
|
122
|
+
_MAX_READ_ATTEMPTS = 8
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _int(name, value, low=_INT_MIN, high=_INT_MAX):
|
|
126
|
+
if not isinstance(value, int):
|
|
127
|
+
raise TypeError(f"{name}: expected int")
|
|
128
|
+
if not low <= value <= high:
|
|
129
|
+
raise OverflowError(f"{name}: out of range")
|
|
130
|
+
return value
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _cstr(name, value, nullable=False):
|
|
134
|
+
if value is None and nullable:
|
|
135
|
+
return None
|
|
136
|
+
if not isinstance(value, bytes):
|
|
137
|
+
raise TypeError(f"{name}: expected bytes")
|
|
138
|
+
if b"\x00" in value:
|
|
139
|
+
raise ValueError(f"{name}: embedded null byte")
|
|
140
|
+
return value
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _payload(name, value):
|
|
144
|
+
if not isinstance(value, bytes):
|
|
145
|
+
raise TypeError(f"{name}: expected bytes")
|
|
146
|
+
return value
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _read(op, key_id):
|
|
150
|
+
"""Read variable-length data, resizing until the kernel's copy fits.
|
|
151
|
+
|
|
152
|
+
The kernel returns the full length and copies nothing when the
|
|
153
|
+
buffer is too small, so only a result with ``n <= size`` is used.
|
|
154
|
+
Data that keeps growing raises ``OSError`` with ``EAGAIN`` after
|
|
155
|
+
``_MAX_READ_ATTEMPTS`` calls. Buffers are zeroed after use.
|
|
156
|
+
"""
|
|
157
|
+
size = 64
|
|
158
|
+
for _ in range(_MAX_READ_ATTEMPTS):
|
|
159
|
+
buf = _ctypes.create_string_buffer(size)
|
|
160
|
+
try:
|
|
161
|
+
n = _sys.keyctl(op, key_id, buf, size)
|
|
162
|
+
if n <= size:
|
|
163
|
+
return buf.raw[:n]
|
|
164
|
+
finally:
|
|
165
|
+
_ctypes.memset(buf, 0, size)
|
|
166
|
+
size = n
|
|
167
|
+
raise OSError(_errno.EAGAIN, _os.strerror(_errno.EAGAIN))
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
# Key operations.
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def add_key(
|
|
174
|
+
key: bytes, value: bytes, keyring: int, key_type: bytes = KEY_TYPE_USER
|
|
175
|
+
) -> int:
|
|
176
|
+
"""Create a key in a keyring, or update a matching one.
|
|
177
|
+
|
|
178
|
+
If ``keyring`` already holds a key of ``key_type`` with description
|
|
179
|
+
``key`` and the type supports updates, its payload is replaced and
|
|
180
|
+
its serial returned.
|
|
181
|
+
|
|
182
|
+
Args:
|
|
183
|
+
key: Key description.
|
|
184
|
+
value: Payload.
|
|
185
|
+
keyring: Destination keyring ID.
|
|
186
|
+
key_type: Key type; ``user`` by default.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
The key's serial number.
|
|
190
|
+
|
|
191
|
+
Raises:
|
|
192
|
+
OSError: The kernel refused, e.g. ``ENOKEY`` for a keyring that
|
|
193
|
+
does not exist, ``EDQUOT`` when the user's key quota is
|
|
194
|
+
full, ``EINVAL`` for an invalid description or payload.
|
|
195
|
+
"""
|
|
196
|
+
return _sys.add_key(
|
|
197
|
+
_cstr("key_type", key_type),
|
|
198
|
+
_cstr("key", key),
|
|
199
|
+
_payload("value", value),
|
|
200
|
+
_int("keyring", keyring),
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def read_key(key_id: int) -> bytes:
|
|
205
|
+
"""Return a key's payload.
|
|
206
|
+
|
|
207
|
+
Args:
|
|
208
|
+
key_id: Key ID.
|
|
209
|
+
|
|
210
|
+
Returns:
|
|
211
|
+
The payload, exactly as stored. For a keyring, the linked
|
|
212
|
+
serials as a packed array of native-endian C ``int``.
|
|
213
|
+
|
|
214
|
+
Raises:
|
|
215
|
+
OSError: ``ENOKEY``, ``EKEYREVOKED`` or ``EKEYEXPIRED`` when the
|
|
216
|
+
key is missing, revoked or expired; ``EACCES`` without READ
|
|
217
|
+
permission or possession.
|
|
218
|
+
"""
|
|
219
|
+
return _read(_sys.KEYCTL_READ, _int("key_id", key_id))
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def update_key(key_id: int, payload: bytes) -> int:
|
|
223
|
+
"""Replace a key's payload.
|
|
224
|
+
|
|
225
|
+
Args:
|
|
226
|
+
key_id: Key ID.
|
|
227
|
+
payload: New payload.
|
|
228
|
+
|
|
229
|
+
Returns:
|
|
230
|
+
``0``.
|
|
231
|
+
|
|
232
|
+
Raises:
|
|
233
|
+
OSError: e.g. ``EKEYREVOKED`` for a revoked key, ``EACCES``
|
|
234
|
+
without WRITE permission.
|
|
235
|
+
"""
|
|
236
|
+
payload = _payload("payload", payload)
|
|
237
|
+
return _sys.keyctl(
|
|
238
|
+
_sys.KEYCTL_UPDATE,
|
|
239
|
+
_int("key_id", key_id),
|
|
240
|
+
payload,
|
|
241
|
+
_ctypes.c_size_t(len(payload)),
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def describe_key(key_id: int) -> bytes:
|
|
246
|
+
"""Return the kernel's description record of a key.
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
key_id: Key ID.
|
|
250
|
+
|
|
251
|
+
Returns:
|
|
252
|
+
``type;uid;gid;perm;description`` followed by a NUL byte, e.g.
|
|
253
|
+
``b"user;1000;1000;3f010000;name\\x00"``. ``perm`` is eight
|
|
254
|
+
lowercase hex digits. The description may contain ``;``; use
|
|
255
|
+
:func:`key_info` to parse the record.
|
|
256
|
+
|
|
257
|
+
Raises:
|
|
258
|
+
OSError: e.g. ``ENOKEY``, or ``EACCES`` without VIEW permission.
|
|
259
|
+
"""
|
|
260
|
+
return _read(_sys.KEYCTL_DESCRIBE, _int("key_id", key_id))
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def key_info(key_id: int) -> KeyInfo:
|
|
264
|
+
"""Return a key's parsed description record.
|
|
265
|
+
|
|
266
|
+
Calls the module-level :func:`describe_key` at call time, so
|
|
267
|
+
replacing it (e.g. in tests) also affects this function.
|
|
268
|
+
|
|
269
|
+
Args:
|
|
270
|
+
key_id: Key ID.
|
|
271
|
+
|
|
272
|
+
Returns:
|
|
273
|
+
The parsed record. A description containing ``;`` is kept
|
|
274
|
+
intact.
|
|
275
|
+
|
|
276
|
+
Raises:
|
|
277
|
+
OSError: As for :func:`describe_key`.
|
|
278
|
+
"""
|
|
279
|
+
d = describe_key(key_id)[:-1]
|
|
280
|
+
key_type, uid, gid, perm, description = d.split(b";", 4)
|
|
281
|
+
return KeyInfo(key_type, int(uid), int(gid), int(perm, 16), description)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def search(
|
|
285
|
+
keyring: int,
|
|
286
|
+
description: bytes,
|
|
287
|
+
destination: int = 0,
|
|
288
|
+
key_type: bytes = KEY_TYPE_USER,
|
|
289
|
+
) -> int:
|
|
290
|
+
"""Search a keyring tree for a key.
|
|
291
|
+
|
|
292
|
+
Args:
|
|
293
|
+
keyring: Keyring to search recursively.
|
|
294
|
+
description: Exact description to find.
|
|
295
|
+
destination: If not ``0``, a keyring to link the found key into.
|
|
296
|
+
key_type: Key type to find; ``user`` by default.
|
|
297
|
+
|
|
298
|
+
Returns:
|
|
299
|
+
The found key's serial number.
|
|
300
|
+
|
|
301
|
+
Raises:
|
|
302
|
+
OSError: ``ENOKEY`` if nothing matches (also for an unknown key
|
|
303
|
+
type); ``EKEYREVOKED`` or ``EKEYEXPIRED`` if the match is
|
|
304
|
+
revoked or expired; ``EACCES`` without SEARCH permission.
|
|
305
|
+
"""
|
|
306
|
+
return _sys.keyctl(
|
|
307
|
+
_sys.KEYCTL_SEARCH,
|
|
308
|
+
_int("keyring", keyring),
|
|
309
|
+
_cstr("key_type", key_type),
|
|
310
|
+
_cstr("description", description),
|
|
311
|
+
_int("destination", destination),
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def request_key(
|
|
316
|
+
descr: bytes,
|
|
317
|
+
keyring: int = 0,
|
|
318
|
+
key_type: bytes = KEY_TYPE_USER,
|
|
319
|
+
callout_info: _typing.Optional[bytes] = None,
|
|
320
|
+
) -> int:
|
|
321
|
+
"""Find a key, or have the kernel construct it through an upcall.
|
|
322
|
+
|
|
323
|
+
With ``callout_info`` set, a missing key makes the kernel run
|
|
324
|
+
``/sbin/request-key``; the call may then block for as long as the
|
|
325
|
+
upcall runs (the GIL is released meanwhile).
|
|
326
|
+
|
|
327
|
+
Args:
|
|
328
|
+
descr: Key description.
|
|
329
|
+
keyring: Keyring to link the key into; ``0`` for the default
|
|
330
|
+
request-key keyring.
|
|
331
|
+
key_type: Key type; ``user`` by default.
|
|
332
|
+
callout_info: Upcall data, or ``None`` for no upcall.
|
|
333
|
+
|
|
334
|
+
Returns:
|
|
335
|
+
The found or created key's serial number.
|
|
336
|
+
|
|
337
|
+
Raises:
|
|
338
|
+
OSError: ``ENOKEY`` if the key does not exist and cannot be
|
|
339
|
+
created.
|
|
340
|
+
"""
|
|
341
|
+
return _sys.request_key(
|
|
342
|
+
_cstr("key_type", key_type),
|
|
343
|
+
_cstr("descr", descr),
|
|
344
|
+
_cstr("callout_info", callout_info, nullable=True),
|
|
345
|
+
_int("keyring", keyring),
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
def revoke(key_id: int) -> int:
|
|
350
|
+
"""Revoke a key; later access fails with ``EKEYREVOKED``.
|
|
351
|
+
|
|
352
|
+
Args:
|
|
353
|
+
key_id: Key ID.
|
|
354
|
+
|
|
355
|
+
Returns:
|
|
356
|
+
``0``.
|
|
357
|
+
|
|
358
|
+
Raises:
|
|
359
|
+
OSError: e.g. ``EKEYREVOKED`` if already revoked, ``EACCES``
|
|
360
|
+
without WRITE or SETATTR permission.
|
|
361
|
+
"""
|
|
362
|
+
return _sys.keyctl(_sys.KEYCTL_REVOKE, _int("key_id", key_id))
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def invalidate(key_id: int) -> int:
|
|
366
|
+
"""Invalidate a key; the kernel removes it immediately.
|
|
367
|
+
|
|
368
|
+
Args:
|
|
369
|
+
key_id: Key ID.
|
|
370
|
+
|
|
371
|
+
Returns:
|
|
372
|
+
``0``.
|
|
373
|
+
|
|
374
|
+
Raises:
|
|
375
|
+
OSError: e.g. ``EOPNOTSUPP`` on kernels without this operation.
|
|
376
|
+
"""
|
|
377
|
+
return _sys.keyctl(_sys.KEYCTL_INVALIDATE, _int("key_id", key_id))
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def set_perm(key_id: int, perm: int) -> int:
|
|
381
|
+
"""Set a key's permission mask.
|
|
382
|
+
|
|
383
|
+
Args:
|
|
384
|
+
key_id: Key ID.
|
|
385
|
+
perm: Mask built from the ``KEY_POS_*``, ``KEY_USR_*``,
|
|
386
|
+
``KEY_GRP_*`` and ``KEY_OTH_*`` constants.
|
|
387
|
+
|
|
388
|
+
Returns:
|
|
389
|
+
``0``.
|
|
390
|
+
|
|
391
|
+
Raises:
|
|
392
|
+
OverflowError: ``perm`` is outside the C ``int`` range.
|
|
393
|
+
OSError: e.g. ``EACCES`` without SETATTR permission.
|
|
394
|
+
"""
|
|
395
|
+
return _sys.keyctl(
|
|
396
|
+
_sys.KEYCTL_SETPERM, _int("key_id", key_id), _int("perm", perm)
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def set_timeout(key_id: int, timeout: int) -> int:
|
|
401
|
+
"""Make a key expire after ``timeout`` seconds.
|
|
402
|
+
|
|
403
|
+
Args:
|
|
404
|
+
key_id: Key ID.
|
|
405
|
+
timeout: Seconds from now; ``0`` cancels expiry.
|
|
406
|
+
|
|
407
|
+
Returns:
|
|
408
|
+
``0``.
|
|
409
|
+
|
|
410
|
+
Raises:
|
|
411
|
+
OverflowError: ``timeout`` is outside ``0 .. 2**32 - 1``.
|
|
412
|
+
OSError: e.g. ``EACCES`` without SETATTR permission.
|
|
413
|
+
"""
|
|
414
|
+
return _sys.keyctl(
|
|
415
|
+
_sys.KEYCTL_SET_TIMEOUT,
|
|
416
|
+
_int("key_id", key_id),
|
|
417
|
+
_int("timeout", timeout, 0, _UINT_MAX),
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
# Keyring operations.
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def new_keyring(descr: bytes, parent: int) -> int:
|
|
425
|
+
"""Create a keyring inside another keyring.
|
|
426
|
+
|
|
427
|
+
Equivalent to ``add_key(descr, b"", parent, KEY_TYPE_KEYRING)``.
|
|
428
|
+
|
|
429
|
+
Args:
|
|
430
|
+
descr: Description of the new keyring.
|
|
431
|
+
parent: Keyring to create it in.
|
|
432
|
+
|
|
433
|
+
Returns:
|
|
434
|
+
The new keyring's serial number.
|
|
435
|
+
|
|
436
|
+
Raises:
|
|
437
|
+
OSError: As for :func:`add_key`.
|
|
438
|
+
"""
|
|
439
|
+
return add_key(descr, b"", parent, key_type=KEY_TYPE_KEYRING)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def list_keyring(keyring: int) -> _abc.Generator[int, None, None]:
|
|
443
|
+
"""Yield the serials of the keys linked in a keyring.
|
|
444
|
+
|
|
445
|
+
This is a generator: nothing happens until the first ``next()``,
|
|
446
|
+
which reads the whole list at once. Later changes to the keyring
|
|
447
|
+
are not reflected. The keyring's type is checked first with
|
|
448
|
+
:func:`key_info`, so VIEW permission is needed as well as READ.
|
|
449
|
+
|
|
450
|
+
Args:
|
|
451
|
+
keyring: Keyring ID.
|
|
452
|
+
|
|
453
|
+
Yields:
|
|
454
|
+
Linked key serials, in kernel order.
|
|
455
|
+
|
|
456
|
+
Raises:
|
|
457
|
+
NotADirectoryError: ``keyring`` is not a keyring (``ENOTDIR``).
|
|
458
|
+
OSError: As for :func:`describe_key` and :func:`read_key`.
|
|
459
|
+
"""
|
|
460
|
+
if key_info(keyring).key_type != KEY_TYPE_KEYRING:
|
|
461
|
+
raise OSError(_errno.ENOTDIR, _os.strerror(_errno.ENOTDIR))
|
|
462
|
+
data = _read(_sys.KEYCTL_READ, keyring)
|
|
463
|
+
width = _ctypes.sizeof(_ctypes.c_int)
|
|
464
|
+
count = len(data) // width
|
|
465
|
+
yield from _struct.unpack(f"{count}i", data[: count * width])
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def clear(keyring: int) -> int:
|
|
469
|
+
"""Unlink every key from a keyring.
|
|
470
|
+
|
|
471
|
+
Args:
|
|
472
|
+
keyring: Keyring ID.
|
|
473
|
+
|
|
474
|
+
Returns:
|
|
475
|
+
``0``.
|
|
476
|
+
|
|
477
|
+
Raises:
|
|
478
|
+
OSError: e.g. ``EACCES`` without WRITE permission.
|
|
479
|
+
"""
|
|
480
|
+
return _sys.keyctl(_sys.KEYCTL_CLEAR, _int("keyring", keyring))
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
def link(key_id: int, keyring: int) -> int:
|
|
484
|
+
"""Link a key into a keyring.
|
|
485
|
+
|
|
486
|
+
Linking a key that is already linked there succeeds.
|
|
487
|
+
|
|
488
|
+
Args:
|
|
489
|
+
key_id: Key to link.
|
|
490
|
+
keyring: Keyring to link it into.
|
|
491
|
+
|
|
492
|
+
Returns:
|
|
493
|
+
``0``.
|
|
494
|
+
|
|
495
|
+
Raises:
|
|
496
|
+
OSError: e.g. ``EDEADLK`` if the link would create a cycle,
|
|
497
|
+
``EACCES`` without LINK or WRITE permission.
|
|
498
|
+
"""
|
|
499
|
+
return _sys.keyctl(
|
|
500
|
+
_sys.KEYCTL_LINK, _int("key_id", key_id), _int("keyring", keyring)
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
def unlink(key_id: int, keyring: int) -> int:
|
|
505
|
+
"""Unlink a key from a keyring.
|
|
506
|
+
|
|
507
|
+
Args:
|
|
508
|
+
key_id: Key to unlink.
|
|
509
|
+
keyring: Keyring to unlink it from.
|
|
510
|
+
|
|
511
|
+
Returns:
|
|
512
|
+
``0``.
|
|
513
|
+
|
|
514
|
+
Raises:
|
|
515
|
+
FileNotFoundError: The key is not linked in ``keyring``
|
|
516
|
+
(``ENOENT``).
|
|
517
|
+
OSError: e.g. ``EACCES`` without WRITE permission.
|
|
518
|
+
"""
|
|
519
|
+
return _sys.keyctl(
|
|
520
|
+
_sys.KEYCTL_UNLINK, _int("key_id", key_id), _int("keyring", keyring)
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
def move_key(
|
|
525
|
+
key_id: int, from_keyring: int, to_keyring: int, flags: int = 0
|
|
526
|
+
) -> int:
|
|
527
|
+
"""Move a key from one keyring to another.
|
|
528
|
+
|
|
529
|
+
Args:
|
|
530
|
+
key_id: Key to move.
|
|
531
|
+
from_keyring: Keyring it is linked in.
|
|
532
|
+
to_keyring: Keyring to move it to.
|
|
533
|
+
flags: ``KEYCTL_MOVE_EXCL`` to fail instead of displacing a
|
|
534
|
+
matching key in ``to_keyring``.
|
|
535
|
+
|
|
536
|
+
Returns:
|
|
537
|
+
``0``.
|
|
538
|
+
|
|
539
|
+
Raises:
|
|
540
|
+
OverflowError: ``flags`` is outside ``0 .. 2**32 - 1``.
|
|
541
|
+
FileExistsError: ``KEYCTL_MOVE_EXCL`` and a matching key exists
|
|
542
|
+
(``EEXIST``).
|
|
543
|
+
OSError: e.g. ``EOPNOTSUPP`` on kernels without this operation.
|
|
544
|
+
"""
|
|
545
|
+
return _sys.keyctl(
|
|
546
|
+
_sys.KEYCTL_MOVE,
|
|
547
|
+
_int("key_id", key_id),
|
|
548
|
+
_int("from_keyring", from_keyring),
|
|
549
|
+
_int("to_keyring", to_keyring),
|
|
550
|
+
_int("flags", flags, 0, _UINT_MAX),
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def join_session_keyring(name: _typing.Optional[bytes] = None) -> int:
|
|
555
|
+
"""Make the calling process join a session keyring.
|
|
556
|
+
|
|
557
|
+
This changes the session keyring of the calling process.
|
|
558
|
+
|
|
559
|
+
Args:
|
|
560
|
+
name: Name of the keyring to join or create, or ``None`` for a
|
|
561
|
+
new anonymous one.
|
|
562
|
+
|
|
563
|
+
Returns:
|
|
564
|
+
The session keyring's serial number.
|
|
565
|
+
|
|
566
|
+
Raises:
|
|
567
|
+
OSError: e.g. ``EACCES`` if a keyring of that name exists but
|
|
568
|
+
cannot be joined.
|
|
569
|
+
"""
|
|
570
|
+
return _sys.keyctl(
|
|
571
|
+
_sys.KEYCTL_JOIN_SESSION_KEYRING, _cstr("name", name, nullable=True)
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
def session_to_parent() -> int:
|
|
576
|
+
"""Replace the parent process's session keyring with the caller's.
|
|
577
|
+
|
|
578
|
+
The change takes effect when the parent next returns to user space.
|
|
579
|
+
It affects the parent process (e.g. the shell), not the caller.
|
|
580
|
+
|
|
581
|
+
Returns:
|
|
582
|
+
``0``.
|
|
583
|
+
|
|
584
|
+
Raises:
|
|
585
|
+
OSError: e.g. ``EPERM`` if the parent has a different owner.
|
|
586
|
+
"""
|
|
587
|
+
return _sys.keyctl(_sys.KEYCTL_SESSION_TO_PARENT)
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
__all__ = [
|
|
591
|
+
"__version__",
|
|
592
|
+
"KEY_SPEC_THREAD_KEYRING",
|
|
593
|
+
"KEY_SPEC_PROCESS_KEYRING",
|
|
594
|
+
"KEY_SPEC_SESSION_KEYRING",
|
|
595
|
+
"KEY_SPEC_USER_KEYRING",
|
|
596
|
+
"KEY_SPEC_USER_SESSION_KEYRING",
|
|
597
|
+
"KEY_SPEC_GROUP_KEYRING",
|
|
598
|
+
"KEY_SPEC_REQKEY_AUTH_KEY",
|
|
599
|
+
"KEY_SPEC_REQUESTOR_KEYRING",
|
|
600
|
+
"KEY_REQKEY_DEFL_NO_CHANGE",
|
|
601
|
+
"KEY_REQKEY_DEFL_DEFAULT",
|
|
602
|
+
"KEY_REQKEY_DEFL_THREAD_KEYRING",
|
|
603
|
+
"KEY_REQKEY_DEFL_PROCESS_KEYRING",
|
|
604
|
+
"KEY_REQKEY_DEFL_SESSION_KEYRING",
|
|
605
|
+
"KEY_REQKEY_DEFL_USER_KEYRING",
|
|
606
|
+
"KEY_REQKEY_DEFL_USER_SESSION_KEYRING",
|
|
607
|
+
"KEY_REQKEY_DEFL_GROUP_KEYRING",
|
|
608
|
+
"KEY_REQKEY_DEFL_REQUESTOR_KEYRING",
|
|
609
|
+
"KEYCTL_MOVE_EXCL",
|
|
610
|
+
"KEY_POS_VIEW",
|
|
611
|
+
"KEY_POS_READ",
|
|
612
|
+
"KEY_POS_WRITE",
|
|
613
|
+
"KEY_POS_SEARCH",
|
|
614
|
+
"KEY_POS_LINK",
|
|
615
|
+
"KEY_POS_SETATTR",
|
|
616
|
+
"KEY_POS_ALL",
|
|
617
|
+
"KEY_USR_VIEW",
|
|
618
|
+
"KEY_USR_READ",
|
|
619
|
+
"KEY_USR_WRITE",
|
|
620
|
+
"KEY_USR_SEARCH",
|
|
621
|
+
"KEY_USR_LINK",
|
|
622
|
+
"KEY_USR_SETATTR",
|
|
623
|
+
"KEY_USR_ALL",
|
|
624
|
+
"KEY_GRP_VIEW",
|
|
625
|
+
"KEY_GRP_READ",
|
|
626
|
+
"KEY_GRP_WRITE",
|
|
627
|
+
"KEY_GRP_SEARCH",
|
|
628
|
+
"KEY_GRP_LINK",
|
|
629
|
+
"KEY_GRP_SETATTR",
|
|
630
|
+
"KEY_GRP_ALL",
|
|
631
|
+
"KEY_OTH_VIEW",
|
|
632
|
+
"KEY_OTH_READ",
|
|
633
|
+
"KEY_OTH_WRITE",
|
|
634
|
+
"KEY_OTH_SEARCH",
|
|
635
|
+
"KEY_OTH_LINK",
|
|
636
|
+
"KEY_OTH_SETATTR",
|
|
637
|
+
"KEY_OTH_ALL",
|
|
638
|
+
"KEY_TYPE_USER",
|
|
639
|
+
"KEY_TYPE_KEYRING",
|
|
640
|
+
"KEY_TYPE_BIG_KEY",
|
|
641
|
+
"KeyInfo",
|
|
642
|
+
"add_key",
|
|
643
|
+
"link",
|
|
644
|
+
"unlink",
|
|
645
|
+
"read_key",
|
|
646
|
+
"update_key",
|
|
647
|
+
"search",
|
|
648
|
+
"clear",
|
|
649
|
+
"describe_key",
|
|
650
|
+
"key_info",
|
|
651
|
+
"set_perm",
|
|
652
|
+
"set_timeout",
|
|
653
|
+
"revoke",
|
|
654
|
+
"invalidate",
|
|
655
|
+
"session_to_parent",
|
|
656
|
+
"join_session_keyring",
|
|
657
|
+
"request_key",
|
|
658
|
+
"move_key",
|
|
659
|
+
"new_keyring",
|
|
660
|
+
"list_keyring",
|
|
661
|
+
]
|