]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ask-password-api: move timeout into AskPasswordRequest structure too
authorLennart Poettering <lennart@poettering.net>
Thu, 7 Nov 2024 09:41:56 +0000 (10:41 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 2 Jan 2025 15:38:25 +0000 (16:38 +0100)
22 files changed:
src/ask-password/ask-password.c
src/bootctl/bootctl-install.c
src/cryptenroll/cryptenroll-password.c
src/cryptenroll/cryptenroll-tpm2.c
src/cryptsetup/cryptsetup.c
src/firstboot/firstboot.c
src/home/homectl.c
src/keyutil/keyutil.c
src/measure/measure.c
src/pcrlock/pcrlock.c
src/repart/repart.c
src/sbsign/sbsign.c
src/shared/ask-password-api.c
src/shared/ask-password-api.h
src/shared/cryptsetup-fido2.c
src/shared/cryptsetup-tpm2.c
src/shared/dissect-image.c
src/shared/libfido2-util.c
src/shared/openssl-util.c
src/shared/pkcs11-util.c
src/test/test-ask-password-api.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index 17e5fa4dc1a893d34b3f051ed80e982b4e2ee0db..d3afc377813b5e2c63de7f3e421781d571517281 100644 (file)
@@ -258,9 +258,10 @@ static int run(int argc, char *argv[]) {
                 .id = arg_id,
                 .keyring = arg_key_name,
                 .credential = arg_credential_name ?: "password",
+                .until = timeout,
         };
 
-        r = ask_password_auto(&req, timeout, arg_flags, &l);
+        r = ask_password_auto(&req, arg_flags, &l);
         if (r < 0)
                 return log_error_errno(r, "Failed to query password: %m");
 
index 0a5b59a503086c95b954791ee34780fe78dbfe3d..76deba56e7bbb9149c5128925cc9c4de4cd97c45 100644 (file)
@@ -988,6 +988,7 @@ int verb_install(int argc, char *argv[], void *userdata) {
                                         .id = "bootctl-private-key-pin",
                                         .keyring = arg_private_key,
                                         .credential = "bootctl.private-key-pin",
+                                        .until = USEC_INFINITY,
                                 },
                                 &private_key,
                                 &ui);
index 0560eddb6642c40d17cb7ad85128fce701442de2..56a3bb44566e7770de1e6fb95f54943383a55b0a 100644 (file)
@@ -61,6 +61,7 @@ int load_volume_key_password(
                         .id = id,
                         .keyring = "cryptenroll",
                         .credential = "cryptenroll.passphrase",
+                        .until = USEC_INFINITY,
                 };
 
                 for (;;) {
@@ -70,7 +71,7 @@ int load_volume_key_password(
                                 return log_error_errno(SYNTHETIC_ERRNO(ENOKEY),
                                                        "Too many attempts, giving up.");
 
-                        r = ask_password_auto(&req, USEC_INFINITY, ask_password_flags, &passwords);
+                        r = ask_password_auto(&req, ask_password_flags, &passwords);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to query password: %m");
 
@@ -136,6 +137,7 @@ int enroll_password(
                         .id = id,
                         .keyring = "cryptenroll",
                         .credential = "cryptenroll.new-passphrase",
+                        .until = USEC_INFINITY,
                 };
 
                 for (;;) {
@@ -152,7 +154,7 @@ int enroll_password(
 
                         req.message = question;
 
-                        r = ask_password_auto(&req, USEC_INFINITY, /* flags= */ 0, &passwords);
+                        r = ask_password_auto(&req, /* flags= */ 0, &passwords);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to query password: %m");
 
@@ -165,7 +167,7 @@ int enroll_password(
 
                         req.message = question;
 
-                        r = ask_password_auto(&req, USEC_INFINITY, /* flags= */ 0, &passwords2);
+                        r = ask_password_auto(&req, /* flags= */ 0, &passwords2);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to query password: %m");
 
index a5750427dc78b6fb5037b34eea025a5e336faa86..42d5a9cd036cc47a608933b8f256993a5de74c8e 100644 (file)
@@ -124,12 +124,12 @@ static int get_pin(char **ret_pin_str, TPM2Flags *ret_flags) {
                                 .icon = "drive-harddisk",
                                 .keyring = "tpm2-pin",
                                 .credential = "cryptenroll.new-tpm2-pin",
+                                .until = USEC_INFINITY,
                         };
 
                         pin = strv_free_erase(pin);
                         r = ask_password_auto(
                                         &req,
-                                        /* until= */ USEC_INFINITY,
                                         /* flags= */ 0,
                                         &pin);
                         if (r < 0)
@@ -140,7 +140,6 @@ static int get_pin(char **ret_pin_str, TPM2Flags *ret_flags) {
 
                         r = ask_password_auto(
                                         &req,
-                                        USEC_INFINITY,
                                         /* flags= */ 0,
                                         &pin2);
                         if (r < 0)
index 97acb03baf734904279310bd4e44f4bf13fbd24e..cb3b591186a63969038c71350ec1df79eba78a1f 100644 (file)
@@ -912,12 +912,13 @@ static int get_password(
                 .id = id,
                 .keyring = "cryptsetup",
                 .credential = "cryptsetup.passphrase",
+                .until = until,
         };
 
         if (ignore_cached)
                 flags &= ~ASK_PASSWORD_ACCEPT_CACHED;
 
-        r = ask_password_auto(&req, until, flags, &passwords);
+        r = ask_password_auto(&req, flags, &passwords);
         if (r < 0)
                 return log_error_errno(r, "Failed to query password: %m");
 
@@ -938,7 +939,7 @@ static int get_password(
                 req.message = text;
                 req.id = id;
 
-                r = ask_password_auto(&req, until, flags, &passwords2);
+                r = ask_password_auto(&req, flags, &passwords2);
                 if (r < 0)
                         return log_error_errno(r, "Failed to query verification password: %m");
 
@@ -1428,9 +1429,10 @@ static int crypt_activate_by_token_pin_ask_password(
                         .icon = "drive-harddisk",
                         .keyring = keyring,
                         .credential = credential,
+                        .until = until,
                 };
 
-                r = ask_password_auto(&req, until, flags, &pins);
+                r = ask_password_auto(&req, flags, &pins);
                 if (r < 0)
                         return r;
 
index 08c80f539a9538f650443a80fe676525ec3d5245..5658255358c37d3d0eda28f13254e6453c614cb0 100644 (file)
@@ -737,9 +737,10 @@ static int prompt_root_password(int rfd) {
                 AskPasswordRequest req = {
                         .tty_fd = -EBADF,
                         .message = msg1,
+                        .until = USEC_INFINITY,
                 };
 
-                r = ask_password_tty(&req, /* until= */ 0, /* flags= */ 0, &a);
+                r = ask_password_tty(&req, /* flags= */ 0, &a);
                 if (r < 0)
                         return log_error_errno(r, "Failed to query root password: %m");
                 if (strv_length(a) != 1)
@@ -761,7 +762,7 @@ static int prompt_root_password(int rfd) {
 
                 req.message = msg2;
 
-                r = ask_password_tty(&req, /* until= */ 0, /* flags= */ 0, &b);
+                r = ask_password_tty(&req, /* flags= */ 0, &b);
                 if (r < 0)
                         return log_error_errno(r, "Failed to query root password: %m");
                 if (strv_length(b) != 1)
index fbb38b64cf281b4a46785b3fd2717a8ee5973652..611fb966fe60eaf43cbb9892ad42bb4c5fe7d34f 100644 (file)
@@ -268,9 +268,10 @@ static int acquire_existing_password(
                 .icon = "user-home",
                 .keyring = "home-password",
                 .credential = "home.password",
+                .until = USEC_INFINITY,
         };
 
-        r = ask_password_auto(&req, USEC_INFINITY, flags, &password);
+        r = ask_password_auto(&req, flags, &password);
         if (r == -EUNATCH) { /* EUNATCH is returned if no password was found and asking interactively was
                               * disabled via the flags. Not an error for us. */
                 log_debug_errno(r, "No passwords acquired.");
@@ -327,9 +328,10 @@ static int acquire_recovery_key(
                 .icon = "user-home",
                 .keyring = "home-recovery-key",
                 .credential = "home.recovery-key",
+                .until = USEC_INFINITY,
         };
 
-        r = ask_password_auto(&req, USEC_INFINITY, flags, &recovery_key);
+        r = ask_password_auto(&req, flags, &recovery_key);
         if (r == -EUNATCH) { /* EUNATCH is returned if no recovery key was found and asking interactively was
                               * disabled via the flags. Not an error for us. */
                 log_debug_errno(r, "No recovery keys acquired.");
@@ -382,9 +384,10 @@ static int acquire_token_pin(
                 .icon = "user-home",
                 .keyring = "token-pin",
                 .credential = "home.token-pin",
+                .until = USEC_INFINITY,
         };
 
-        r = ask_password_auto(&req, USEC_INFINITY, flags, &pin);
+        r = ask_password_auto(&req, flags, &pin);
         if (r == -EUNATCH) { /* EUNATCH is returned if no PIN was found and asking interactively was disabled
                               * via the flags. Not an error for us. */
                 log_debug_errno(r, "No security token PINs acquired.");
@@ -1237,11 +1240,11 @@ static int acquire_new_password(
                         .icon = "user-home",
                         .keyring = "home-password",
                         .credential = "home.new-password",
+                        .until = USEC_INFINITY,
                 };
 
                 r = ask_password_auto(
                                 &req,
-                                USEC_INFINITY,
                                 /* flags= */ 0, /* no caching, we want to collect a new password here after all */
                                 &first);
                 if (r < 0)
@@ -1257,7 +1260,6 @@ static int acquire_new_password(
 
                 r = ask_password_auto(
                                 &req,
-                                USEC_INFINITY,
                                 /* flags= */ 0, /* no caching */
                                 &second);
                 if (r < 0)
index b034c1c40fef8fedc6c86b9bbc8bc8c65a9bd444..bae742157286bca5ea1a1cdccabb783a1581c753 100644 (file)
@@ -186,6 +186,7 @@ static int verb_validate(int argc, char *argv[], void *userdata) {
                                 .id = "keyutil-private-key-pin",
                                 .keyring = arg_private_key,
                                 .credential = "keyutil.private-key-pin",
+                                .until = USEC_INFINITY,
                         },
                         &private_key,
                         &ui);
@@ -243,6 +244,7 @@ static int verb_public(int argc, char *argv[], void *userdata) {
                                         .id = "keyutil-private-key-pin",
                                         .keyring = arg_private_key,
                                         .credential = "keyutil.private-key-pin",
+                                        .until = USEC_INFINITY,
                                 },
                                 &private_key,
                                 &ui);
index c4ca9dc8da6508ab2d227391fe0bc46c17557f54..fbd039da7915f26e99e5e59500d6c5775314c399 100644 (file)
@@ -892,6 +892,7 @@ static int verb_sign(int argc, char *argv[], void *userdata) {
                                         .id = "measure-private-key-pin",
                                         .keyring = arg_private_key,
                                         .credential = "measure.private-key-pin",
+                                        .until = USEC_INFINITY,
                                 },
                                 &privkey,
                                 &ui);
index bbf2ae856c921d29822b13db7f3f53c87fca68c8..b22ee57788e18ea4c3906a2265cc609c90a29748 100644 (file)
@@ -4555,11 +4555,11 @@ static int make_policy(bool force, RecoveryPinMode recovery_pin_mode) {
                                 .message = "Recovery PIN",
                                 .id = "pcrlock-recovery-pin",
                                 .credential = "pcrlock.recovery-pin",
+                                .until = USEC_INFINITY,
                         };
 
                         r = ask_password_auto(
                                         &req,
-                                        /* until= */ 0,
                                         /* flags= */ 0,
                                         &l);
                         if (r < 0)
index a89810fbf57ece0b7922bdaf577eb2d9656b2d68..b381d4808ac56cf41e39b5c9d44d4e0593b74663 100644 (file)
@@ -8576,6 +8576,7 @@ static int parse_argv(int argc, char *argv[], X509 **ret_certificate, EVP_PKEY *
                                         .id = "repart-private-key-pin",
                                         .keyring = arg_private_key,
                                         .credential = "repart.private-key-pin",
+                                        .until = USEC_INFINITY,
                                 },
                                 &private_key,
                                 &ui);
index 538c2ebe63e82d2515683e583bf4c380cdd2e249..5c5c884c98d88b5fba325f953651cebc6ae6d456 100644 (file)
@@ -207,6 +207,7 @@ static int verb_sign(int argc, char *argv[], void *userdata) {
                                 .id = "sbsign-private-key-pin",
                                 .keyring = arg_private_key,
                                 .credential = "sbsign.private-key-pin",
+                                .until = USEC_INFINITY,
                         },
                         &private_key,
                         &ui);
index f1e2b06c3dc4edac587edc6efcd3dcdfe6c2d847..39e6277b376e63ad2a8fbade18a47e780781ad1b 100644 (file)
@@ -309,7 +309,6 @@ static int backspace_string(int ttyfd, const char *str) {
 
 int ask_password_plymouth(
                 const AskPasswordRequest *req,
-                usec_t until,
                 AskPasswordFlags flags,
                 char ***ret) {
 
@@ -369,8 +368,8 @@ int ask_password_plymouth(
         for (;;) {
                 usec_t timeout;
 
-                if (until > 0)
-                        timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
+                if (req->until > 0)
+                        timeout = usec_sub_unsigned(req->until, now(CLOCK_MONOTONIC));
                 else
                         timeout = USEC_INFINITY;
 
@@ -464,7 +463,6 @@ int ask_password_plymouth(
 
 int ask_password_tty(
                 const AskPasswordRequest *req,
-                usec_t until,
                 AskPasswordFlags flags,
                 char ***ret) {
 
@@ -584,8 +582,8 @@ int ask_password_tty(
                 usec_t timeout;
                 ssize_t n;
 
-                if (until > 0)
-                        timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
+                if (req->until > 0)
+                        timeout = usec_sub_unsigned(req->until, now(CLOCK_MONOTONIC));
                 else
                         timeout = USEC_INFINITY;
 
@@ -799,7 +797,6 @@ static int create_socket(const char *askpwdir, char **ret) {
 
 int ask_password_agent(
                 const AskPasswordRequest *req,
-                usec_t until,
                 AskPasswordFlags flags,
                 char ***ret) {
 
@@ -894,7 +891,7 @@ int ask_password_agent(
                 socket_name,
                 FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED),
                 FLAGS_SET(flags, ASK_PASSWORD_ECHO),
-                until,
+                req->until,
                 FLAGS_SET(flags, ASK_PASSWORD_SILENT));
 
         if (req) {
@@ -946,8 +943,8 @@ int ask_password_agent(
                 usec_t timeout;
                 ssize_t n;
 
-                if (until > 0)
-                        timeout = usec_sub_unsigned(until, now(CLOCK_MONOTONIC));
+                if (req->until > 0)
+                        timeout = usec_sub_unsigned(req->until, now(CLOCK_MONOTONIC));
                 else
                         timeout = USEC_INFINITY;
 
@@ -1106,7 +1103,6 @@ static int ask_password_credential(const AskPasswordRequest *req, AskPasswordFla
 
 int ask_password_auto(
                 const AskPasswordRequest *req,
-                usec_t until,
                 AskPasswordFlags flags,
                 char ***ret) {
 
@@ -1130,10 +1126,10 @@ int ask_password_auto(
         }
 
         if (!FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) && isatty_safe(STDIN_FILENO))
-                return ask_password_tty(req, until, flags, ret);
+                return ask_password_tty(req, flags, ret);
 
         if (!FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT))
-                return ask_password_agent(req, until, flags, ret);
+                return ask_password_agent(req, flags, ret);
 
         return -EUNATCH;
 }
index 1abaad2134f63aeb830dbd7ee1d7517ee9effdfb..fe453522fcad95109b835a37b2ec2b3459b2ec98 100644 (file)
@@ -28,11 +28,12 @@ typedef struct AskPasswordRequest {
         const char *credential;      /* $CREDENTIALS_DIRECTORY credential name */
         const char *flag_file;       /* Once this flag file disappears abort the query */
         int tty_fd;                  /* If querying on a TTY, the TTY to query on (or -EBADF) */
+        usec_t until;                /* CLOCK_MONOTONIC time until which to show the prompt (if zero: forever) */
 } AskPasswordRequest;
 
-int ask_password_tty(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);
+int ask_password_tty(const AskPasswordRequest *req, AskPasswordFlags flags, char ***ret);
+int ask_password_plymouth(const AskPasswordRequest *req, AskPasswordFlags flags, char ***ret);
+int ask_password_agent(const AskPasswordRequest *req, AskPasswordFlags flag, char ***ret);
+int ask_password_auto(const AskPasswordRequest *req, AskPasswordFlags flag, char ***ret);
 
 int acquire_user_ask_password_directory(char **ret);
index 6e4b120ae6a9b26ee8d4cd7ba130fa2a87a1f619..070908d5257bb5725cf53fd44797f75627403775 100644 (file)
@@ -111,16 +111,17 @@ int acquire_fido2_key(
                 if (FLAGS_SET(askpw_flags, ASK_PASSWORD_HEADLESS))
                         return log_error_errno(SYNTHETIC_ERRNO(ENOPKG), "PIN querying disabled via 'headless' option. Use the '$PIN' environment variable.");
 
-                static const AskPasswordRequest req = {
+                AskPasswordRequest req = {
                         .tty_fd = -EBADF,
                         .message = "Please enter security token PIN:",
                         .icon = "drive-harddisk",
                         .keyring = "fido2-pin",
                         .credential = "cryptsetup.fido2-pin",
+                        .until = until,
                 };
 
                 pins = strv_free_erase(pins);
-                r = ask_password_auto(&req, until, askpw_flags, &pins);
+                r = ask_password_auto(&req, askpw_flags, &pins);
                 if (r < 0)
                         return log_error_errno(r, "Failed to ask for user password: %m");
 
index 05f0fa2bfa703bcf6c8d09721c58585f4c691273..7a1275eb381f037ec108fa8ba9c3d594cdb6198d 100644 (file)
@@ -40,10 +40,11 @@ static int get_pin(
                         .icon = "drive-harddisk",
                         .keyring = "tpm2-pin",
                         .credential = askpw_credential,
+                        .until = until,
                 };
 
                 pin = strv_free_erase(pin);
-                r = ask_password_auto(&req, until, askpw_flags, &pin);
+                r = ask_password_auto(&req, askpw_flags, &pin);
                 if (r < 0)
                         return log_error_errno(r, "Failed to ask for user pin: %m");
                 assert(strv_length(pin) == 1);
index 5a65f55fca9977e0703b93cac945f67237b4c6e6..3feda770af9bce810a258759cbecea6cb0e442a2 100644 (file)
@@ -3082,9 +3082,10 @@ int dissected_image_decrypt_interactively(
                         .id = "dissect",
                         .keyring = "dissect",
                         .credential = "dissect.passphrase",
+                        .until = USEC_INFINITY,
                 };
 
-                r = ask_password_auto(&req, USEC_INFINITY, /* flags= */ 0, &z);
+                r = ask_password_auto(&req, /* flags= */ 0, &z);
                 if (r < 0)
                         return log_error_errno(r, "Failed to query for passphrase: %m");
 
index 8e981f4c846fff007fbed86ba7b5c40320623719..200068dce441bc8e68363f198b8eae8cc45c4743 100644 (file)
@@ -862,9 +862,10 @@ int fido2_generate_hmac_hash(
                                 .icon = askpw_icon,
                                 .keyring = "fido2-pin",
                                 .credential = askpw_credential,
+                                .until = USEC_INFINITY,
                         };
 
-                        r = ask_password_auto(&req, USEC_INFINITY, /* flags= */ 0, &pin);
+                        r = ask_password_auto(&req, /* flags= */ 0, &pin);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to acquire user PIN: %m");
 
index 417f84254680f9642c732f77b47f66a36f90887a..b60fc67b26c93f801b69b7cb590a9b87347bac30 100644 (file)
@@ -1399,7 +1399,7 @@ static int openssl_ask_password_ui_read(UI *ui, UI_STRING *uis) {
                 req->message = UI_get0_output_string(uis);
 
                 _cleanup_(strv_freep) char **l = NULL;
-                r = ask_password_auto(req, /*until=*/ 0, ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE, &l);
+                r = ask_password_auto(req, ASK_PASSWORD_ACCEPT_CACHED|ASK_PASSWORD_PUSH_CACHE, &l);
                 if (r < 0) {
                         log_error_errno(r, "Failed to query for PIN: %m");
                         return 0;
index caec1606ed0255f04f3f76e4f30cfa83146ae601..8d14b2f8f7e321b6894c50ed19e08c92c1f6edb1 100644 (file)
@@ -386,10 +386,11 @@ int pkcs11_token_login(
                                 .id = id,
                                 .keyring = askpw_keyring,
                                 .credential = askpw_credential,
+                                .until = until,
                         };
 
                         /* We never cache PINs, simply because it's fatal if we use wrong PINs, since usually there are only 3 tries */
-                        r = ask_password_auto(&req, until, askpw_flags, &passwords);
+                        r = ask_password_auto(&req, askpw_flags, &passwords);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to query PIN for security token '%s': %m", token_label);
                 }
index ccf3cee9f386f329e74f00d91280414de7ceaec9..38afe0c6efc1ce76bc298fee09a0e941f2653947 100644 (file)
@@ -12,9 +12,10 @@ TEST(ask_password) {
                 .tty_fd = -EBADF,
                 .message = "hello?",
                 .keyring = "da key",
+                .until = USEC_INFINITY,
         };
 
-        r = ask_password_tty(&req, /* until= */ 0, /* flags= */ ASK_PASSWORD_CONSOLE_COLOR, &ret);
+        r = ask_password_tty(&req, /* flags= */ ASK_PASSWORD_CONSOLE_COLOR, &ret);
         if (r == -ECANCELED)
                 ASSERT_NULL(ret);
         else {
index fdc819754bcd5be566c4bceeb8a2a872bd92f3b4..31292d5acf5542ff82d853bac7c6bb9278df71f6 100644 (file)
@@ -151,9 +151,10 @@ static int agent_ask_password_tty(
                 .tty_fd = tty_fd,
                 .message = message,
                 .flag_file = flag_file,
+                .until = until,
         };
 
-        r = ask_password_tty(&req, until, flags, ret);
+        r = ask_password_tty(&req, flags, ret);
 
         if (arg_console) {
                 assert(tty_fd >= 0);
@@ -258,9 +259,10 @@ static int process_one_password_file(const char *filename, FILE *f) {
                                         .tty_fd = -EBADF,
                                         .message = message,
                                         .flag_file = filename,
+                                        .until = not_after,
                                 };
 
-                                r = ask_password_plymouth(&req, not_after, flags, &passwords);
+                                r = ask_password_plymouth(&req, flags, &passwords);
                         } else
                                 r = agent_ask_password_tty(message, not_after, flags, filename, &passwords);
                         if (r < 0) {