]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ask-password-api: move 'flag_file' from function parameter into AskPasswordRequest...
authorLennart Poettering <lennart@poettering.net>
Thu, 7 Nov 2024 09:00:53 +0000 (10:00 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 2 Jan 2025 15:38:00 +0000 (16:38 +0100)
src/firstboot/firstboot.c
src/shared/ask-password-api.c
src/shared/ask-password-api.h
src/test/test-ask-password-api.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index 9be62b8df3d7aa66ce9325759c1020844c96dad1..bf9b840a23c66d90e383a13fae348d3a833ae916 100644 (file)
@@ -738,7 +738,7 @@ static int prompt_root_password(int rfd) {
                         .message = msg1,
                 };
 
-                r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ 0, /* flag_file= */ NULL, &a);
+                r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ 0, &a);
                 if (r < 0)
                         return log_error_errno(r, "Failed to query root password: %m");
                 if (strv_length(a) != 1)
@@ -760,7 +760,7 @@ static int prompt_root_password(int rfd) {
 
                 req.message = msg2;
 
-                r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ 0, /* flag_file= */ NULL, &b);
+                r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ 0, &b);
                 if (r < 0)
                         return log_error_errno(r, "Failed to query root password: %m");
                 if (strv_length(b) != 1)
index 2e49096f820fccb049ac4f3e0c25205185adde9b..e8ba9a2c36161ee873636f5b4f910cfa551649c2 100644 (file)
@@ -311,7 +311,6 @@ int ask_password_plymouth(
                 const AskPasswordRequest *req,
                 usec_t until,
                 AskPasswordFlags flags,
-                const char *flag_file,
                 char ***ret) {
 
         _cleanup_close_ int fd = -EBADF, inotify_fd = -EBADF;
@@ -328,12 +327,12 @@ int ask_password_plymouth(
 
         const char *message = req && req->message ? req->message : "Password:";
 
-        if (flag_file) {
+        if (req->flag_file) {
                 inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
                 if (inotify_fd < 0)
                         return -errno;
 
-                if (inotify_add_watch(inotify_fd, flag_file, IN_ATTRIB) < 0) /* for the link count */
+                if (inotify_add_watch(inotify_fd, req->flag_file, IN_ATTRIB) < 0) /* for the link count */
                         return -errno;
         }
 
@@ -375,7 +374,7 @@ int ask_password_plymouth(
                 else
                         timeout = USEC_INFINITY;
 
-                if (flag_file && access(flag_file, F_OK) < 0)
+                if (req->flag_file && access(req->flag_file, F_OK) < 0)
                         return -errno;
 
                 r = ppoll_usec(pollfd, n_pollfd, timeout);
@@ -468,7 +467,6 @@ int ask_password_tty(
                 const AskPasswordRequest *req,
                 usec_t until,
                 AskPasswordFlags flags,
-                const char *flag_file,
                 char ***ret) {
 
         bool reset_tty = false, dirty = false, use_color = false, press_tab_visible = false;
@@ -493,15 +491,14 @@ int ask_password_tty(
         if (!FLAGS_SET(flags, ASK_PASSWORD_HIDE_EMOJI) && emoji_enabled())
                 message = strjoina(special_glyph(SPECIAL_GLYPH_LOCK_AND_KEY), " ", message);
 
-        if (flag_file || (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyring)) {
+        if (req->flag_file || (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyring)) {
                 inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
                 if (inotify_fd < 0)
                         return -errno;
         }
-        if (flag_file) {
-                if (inotify_add_watch(inotify_fd, flag_file, IN_ATTRIB /* for the link count */) < 0)
+        if (req->flag_file)
+                if (inotify_add_watch(inotify_fd, req->flag_file, IN_ATTRIB /* for the link count */) < 0)
                         return -errno;
-        }
         if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && req && keyring) {
                 r = ask_password_keyring(req, flags, ret);
                 if (r >= 0)
@@ -590,8 +587,8 @@ int ask_password_tty(
                 else
                         timeout = USEC_INFINITY;
 
-                if (flag_file) {
-                        r = RET_NERRNO(access(flag_file, F_OK));
+                if (req->flag_file) {
+                        r = RET_NERRNO(access(req->flag_file, F_OK));
                         if (r < 0)
                                 goto finish;
                 }
@@ -820,6 +817,10 @@ int ask_password_agent(
         if (FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT))
                 return -EUNATCH;
 
+        /* We don't support the flag file concept for now when querying via the agent logic */
+        if (req->flag_file)
+                return -EOPNOTSUPP;
+
         assert_se(sigemptyset(&mask) >= 0);
         assert_se(sigset_add_many(&mask, SIGINT, SIGTERM) >= 0);
         assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) >= 0);
@@ -1127,7 +1128,7 @@ int ask_password_auto(
         }
 
         if (!FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) && isatty_safe(STDIN_FILENO))
-                return ask_password_tty(-EBADF, req, until, flags, NULL, ret);
+                return ask_password_tty(-EBADF, req, until, flags, ret);
 
         if (!FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT))
                 return ask_password_agent(req, until, flags, ret);
index b3cb407e51c7de3000668b155306bb5aaeb4c3a0..c8763700c8c794d63f4b7051b8ab3f87e6046201 100644 (file)
@@ -26,10 +26,11 @@ typedef struct AskPasswordRequest {
         const char *icon;            /* freedesktop icon spec name */
         const char *id;              /* some identifier used for this prompt for the "ask-password" protocol */
         const char *credential;      /* $CREDENTIALS_DIRECTORY credential name */
+        const char *flag_file;       /* Once this flag file disappears abort the query */
 } AskPasswordRequest;
 
-int ask_password_tty(int tty_fd, const AskPasswordRequest *req, usec_t until, AskPasswordFlags flags, const char *flag_file, char ***ret);
-int ask_password_plymouth(const AskPasswordRequest *req, usec_t until, AskPasswordFlags flags, const char *flag_file, char ***ret);
+int ask_password_tty(int tty_fd, const AskPasswordRequest *req, usec_t until, AskPasswordFlags flags, char ***ret);
+int ask_password_plymouth(const AskPasswordRequest *req, usec_t until, AskPasswordFlags flags, char ***ret);
 int ask_password_agent(const AskPasswordRequest *req, usec_t until, AskPasswordFlags flag, char ***ret);
 int ask_password_auto(const AskPasswordRequest *req, usec_t until, AskPasswordFlags flag, char ***ret);
 
index e58e86870b39cf6a156180b96e0650917d1202e1..efd19696c89dd82642fc68c9c057a53d9e5d23c2 100644 (file)
@@ -13,7 +13,7 @@ TEST(ask_password) {
                 .keyring = "da key",
         };
 
-        r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ ASK_PASSWORD_CONSOLE_COLOR, /* flag_file= */ NULL, &ret);
+        r = ask_password_tty(-EBADF, &req, /* until= */ 0, /* flags= */ ASK_PASSWORD_CONSOLE_COLOR, &ret);
         if (r == -ECANCELED)
                 ASSERT_NULL(ret);
         else {
index 214687673f839e3aabf6edd904fcd4009055dd0f..3c38fbbd41ece50c761164fcd6b8140421e5a1e0 100644 (file)
@@ -149,9 +149,10 @@ static int agent_ask_password_tty(
 
         AskPasswordRequest req = {
                 .message = message,
+                .flag_file = flag_file,
         };
 
-        r = ask_password_tty(tty_fd, &req, until, flags, flag_file, ret);
+        r = ask_password_tty(tty_fd, &req, until, flags, ret);
 
         if (arg_console) {
                 assert(tty_fd >= 0);
@@ -254,9 +255,10 @@ static int process_one_password_file(const char *filename, FILE *f) {
                         if (arg_plymouth) {
                                 AskPasswordRequest req = {
                                         .message = message,
+                                        .flag_file = filename,
                                 };
 
-                                r = ask_password_plymouth(&req, not_after, flags, filename, &passwords);
+                                r = ask_password_plymouth(&req, not_after, flags, &passwords);
                         } else
                                 r = agent_ask_password_tty(message, not_after, flags, filename, &passwords);
                         if (r < 0) {