]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homework-password-cache.c
Merge pull request #21443 from poettering/homed-grow-shrink-on-login-logout
[thirdparty/systemd.git] / src / home / homework-password-cache.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "homework-password-cache.h"
4 #include "keyring-util.h"
5 #include "missing_syscall.h"
6 #include "user-record.h"
7
8 void password_cache_free(PasswordCache *cache) {
9 if (!cache)
10 return;
11
12 cache->pkcs11_passwords = strv_free_erase(cache->pkcs11_passwords);
13 cache->fido2_passwords = strv_free_erase(cache->fido2_passwords);
14 }
15
16 void password_cache_load_keyring(UserRecord *h, PasswordCache *cache) {
17 _cleanup_(erase_and_freep) void *p = NULL;
18 _cleanup_free_ char *name = NULL;
19 char **strv = NULL;
20 key_serial_t serial;
21 size_t sz;
22 int r;
23
24 assert(h);
25 assert(cache);
26
27 /* Loads the password we need to for automatic resizing from the kernel keyring */
28
29 name = strjoin("homework-user-", h->user_name);
30 if (!name)
31 return (void) log_oom();
32
33 serial = request_key("user", name, NULL, 0);
34 if (serial == -1)
35 return (void) log_debug_errno(errno, "Failed to request key '%s', ignoring: %m", name);
36
37 r = keyring_read(serial, &p, &sz);
38 if (r < 0)
39 return (void) log_debug_errno(r, "Failed to read keyring key '%s', ignoring: %m", name);
40
41 if (memchr(p, 0, sz))
42 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Cached password contains embedded NUL byte, ignoring.");
43
44 strv = new(char*, 2);
45 if (!strv)
46 return (void) log_oom();
47
48 strv[0] = TAKE_PTR(p); /* Note that keyring_read() will NUL terminate implicitly, hence we don't have
49 * to NUL terminate manually here: it's a valid string. */
50 strv[1] = NULL;
51
52 strv_free_erase(cache->keyring_passswords);
53 cache->keyring_passswords = strv;
54
55 log_debug("Successfully acquired home key from kernel keyring.");
56 }