Skip to content

Commit

Permalink
homework: split out password cache logic into its own .c/.h file
Browse files Browse the repository at this point in the history
Preparation for extending it further down the line.
  • Loading branch information
poettering committed Nov 12, 2021
1 parent 0881991 commit 6b945d7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
11 changes: 11 additions & 0 deletions src/home/homework-password-cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "homework-password-cache.h"

void password_cache_free(PasswordCache *cache) {
if (!cache)
return;

cache->pkcs11_passwords = strv_free_erase(cache->pkcs11_passwords);
cache->fido2_passwords = strv_free_erase(cache->fido2_passwords);
}
21 changes: 21 additions & 0 deletions src/home/homework-password-cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once

#include "strv.h"
#include "user-record.h"

typedef struct PasswordCache {
/* Decoding passwords from security tokens is expensive and typically requires user interaction,
* hence cache any we already figured out. */
char **pkcs11_passwords;
char **fido2_passwords;
} PasswordCache;

void password_cache_free(PasswordCache *cache);

static inline bool password_cache_contains(const PasswordCache *cache, const char *p) {
if (!cache)
return false;

return strv_contains(cache->pkcs11_passwords, p) || strv_contains(cache->fido2_passwords, p);
}
8 changes: 0 additions & 8 deletions src/home/homework.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@
/* Make sure a bad password always results in a 3s delay, no matter what */
#define BAD_PASSWORD_DELAY_USEC (3 * USEC_PER_SEC)

void password_cache_free(PasswordCache *cache) {
if (!cache)
return;

cache->pkcs11_passwords = strv_free_erase(cache->pkcs11_passwords);
cache->fido2_passwords = strv_free_erase(cache->fido2_passwords);
}

int user_record_authenticate(
UserRecord *h,
UserRecord *secret,
Expand Down
18 changes: 1 addition & 17 deletions src/home/homework.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "sd-id128.h"

#include "homework-password-cache.h"
#include "loop-util.h"
#include "strv.h"
#include "user-record.h"
#include "user-record-util.h"

Expand Down Expand Up @@ -43,22 +43,6 @@ typedef struct HomeSetup {
char *temporary_image_path;
} HomeSetup;

typedef struct PasswordCache {
/* Decoding passwords from security tokens is expensive and typically requires user interaction,
* hence cache any we already figured out. */
char **pkcs11_passwords;
char **fido2_passwords;
} PasswordCache;

void password_cache_free(PasswordCache *cache);

static inline bool password_cache_contains(const PasswordCache *cache, const char *p) {
if (!cache)
return false;

return strv_contains(cache->pkcs11_passwords, p) || strv_contains(cache->fido2_passwords, p);
}

#define HOME_SETUP_INIT \
{ \
.root_fd = -1, \
Expand Down
2 changes: 2 additions & 0 deletions src/home/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ systemd_homework_sources = files('''
homework-luks.h
homework-mount.c
homework-mount.h
homework-password-cache.c
homework-password-cache.h
homework-pkcs11.h
homework-quota.c
homework-quota.h
Expand Down

0 comments on commit 6b945d7

Please sign in to comment.