From fe585662829457aa95d88396916d6a4d642a3677 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 29 Mar 2023 17:52:35 +0200 Subject: [PATCH 1/3] user-util: Rename ETC_PASSWD_LOCK_NAME to ETC_PASSWD_LOCK_FILENAME --- src/basic/user-util.h | 4 ++-- src/firstboot/firstboot.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/basic/user-util.h b/src/basic/user-util.h index c042b67e0a..f7ff35d0e0 100644 --- a/src/basic/user-util.h +++ b/src/basic/user-util.h @@ -80,8 +80,8 @@ int take_etc_passwd_lock(const char *root); #define UID_MAPPED_ROOT ((uid_t) (INT32_MAX-1)) #define GID_MAPPED_ROOT ((gid_t) (INT32_MAX-1)) -#define ETC_PASSWD_LOCK_NAME ".pwd.lock" -#define ETC_PASSWD_LOCK_PATH "/etc/" ETC_PASSWD_LOCK_NAME +#define ETC_PASSWD_LOCK_FILENAME ".pwd.lock" +#define ETC_PASSWD_LOCK_PATH "/etc/" ETC_PASSWD_LOCK_FILENAME /* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer * NULL is special */ diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index c1b739dd4c..9aaa0af848 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -969,7 +969,7 @@ static int process_root_account(int rfd) { return 0; } - r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_NAME, LOCK_EX, &lock); + r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock); if (r < 0) return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m"); From fe75d5bcfac81505cd89e96baa6e8ee959ec9fb6 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 29 Mar 2023 17:59:50 +0200 Subject: [PATCH 2/3] firstboot: Check for errors returned by dir_fd_is_root() --- src/firstboot/firstboot.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 9aaa0af848..432cab4786 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -344,7 +344,11 @@ static int process_locale(int rfd) { if (r <= 0) return r; - if (arg_copy_locale && !dir_fd_is_root(rfd)) { + r = dir_fd_is_root(rfd); + if (r < 0) + return log_error_errno(r, "Failed to check if directory file descriptor is root: %m"); + + if (arg_copy_locale && r == 0) { r = copy_file_atomic_at(AT_FDCWD, "/etc/locale.conf", pfd, f, 0644, COPY_REFLINK); if (r != -ENOENT) { if (r < 0) @@ -429,7 +433,11 @@ static int process_keymap(int rfd) { if (r <= 0) return r; - if (arg_copy_keymap && !dir_fd_is_root(rfd)) { + r = dir_fd_is_root(rfd); + if (r < 0) + return log_error_errno(r, "Failed to check if directory file descriptor is root: %m"); + + if (arg_copy_keymap && r == 0) { r = copy_file_atomic_at(AT_FDCWD, "/etc/vconsole.conf", pfd, f, 0644, COPY_REFLINK); if (r != -ENOENT) { if (r < 0) @@ -517,7 +525,11 @@ static int process_timezone(int rfd) { if (r <= 0) return r; - if (arg_copy_timezone && !dir_fd_is_root(rfd)) { + r = dir_fd_is_root(rfd); + if (r < 0) + return log_error_errno(r, "Failed to check if directory file descriptor is root: %m"); + + if (arg_copy_timezone && r == 0) { _cleanup_free_ char *s = NULL; r = readlink_malloc("/etc/localtime", &s); @@ -973,7 +985,11 @@ static int process_root_account(int rfd) { if (r < 0) return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m"); - if (arg_copy_root_shell && !dir_fd_is_root(rfd)) { + k = dir_fd_is_root(rfd); + if (k < 0) + return log_error_errno(k, "Failed to check if directory file descriptor is root: %m"); + + if (arg_copy_root_shell && k == 0) { struct passwd *p; errno = 0; @@ -990,7 +1006,7 @@ static int process_root_account(int rfd) { if (r < 0) return r; - if (arg_copy_root_password && !dir_fd_is_root(rfd)) { + if (arg_copy_root_password && k == 0) { struct spwd *p; errno = 0; From df00c5162f667ba6884d3ccd41f57a4a7353e255 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 29 Mar 2023 18:03:32 +0200 Subject: [PATCH 3/3] firstboot: Refactor should_configure() --- src/firstboot/firstboot.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 432cab4786..ba7e08913f 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -230,16 +230,17 @@ static int prompt_loop(const char *text, char **l, unsigned percentage, bool (*i } static int should_configure(int dir_fd, const char *filename) { - int r; - assert(dir_fd >= 0); assert(filename); - r = faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW); - if (r < 0 && errno != ENOENT) - return log_error_errno(errno, "Failed to access %s: %m", filename); + if (faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW) < 0) { + if (errno != ENOENT) + return log_error_errno(errno, "Failed to access %s: %m", filename); - return r < 0 || arg_force; + return true; /* missing */ + } + + return arg_force; /* exists, but if --force was given we should still configure the file. */ } static bool locale_is_ok(const char *name) {