]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/home/homework-password-cache.c
test: also flush and rotate journal before read
[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->volume_key = erase_and_free(cache->volume_key);
13 cache->pkcs11_passwords = strv_free_erase(cache->pkcs11_passwords);
14 cache->fido2_passwords = strv_free_erase(cache->fido2_passwords);
15 }
16
17 void password_cache_load_keyring(UserRecord *h, PasswordCache *cache) {
18 _cleanup_free_ char *name = NULL;
19 _cleanup_(erase_and_freep) void *vk = NULL;
20 size_t vks;
21 key_serial_t serial;
22 int r;
23
24 assert(h);
25 assert(cache);
26
27 name = strjoin("homework-user-", h->user_name);
28 if (!name)
29 return (void) log_oom();
30
31 serial = request_key("user", name, NULL, 0);
32 if (serial == -1) {
33 if (errno == ENOKEY) {
34 log_info("Home volume key is not available in kernel keyring.");
35 return;
36 }
37 return (void) log_warning_errno(errno, "Failed to request key '%s', ignoring: %m", name);
38 }
39
40 r = keyring_read(serial, &vk, &vks);
41 if (r < 0)
42 return (void) log_warning_errno(r, "Failed to read keyring key '%s', ignoring: %m", name);
43
44 log_info("Successfully acquired home volume key from kernel keyring.");
45
46 erase_and_free(cache->volume_key);
47 cache->volume_key = TAKE_PTR(vk);
48 cache->volume_key_size = vks;
49 }