]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tty-ask-pwd-agent: small cleanup in process_one_password_file()
authorFranck Bui <fbui@suse.com>
Fri, 20 Sep 2019 13:07:31 +0000 (15:07 +0200)
committerFranck Bui <fbui@suse.com>
Sat, 5 Oct 2019 06:08:24 +0000 (08:08 +0200)
Split the part dealing which asks for password on tty in a dedicated function
making process_one_password_file() hopefully easier to read.

No functional changes.

src/tty-ask-password-agent/tty-ask-password-agent.c

index b3b7403db279eaedcd8deda0339363ed14410551..4e67448a379cedc9143c16adf8eca1db98f99194 100644 (file)
@@ -318,6 +318,39 @@ static bool wall_tty_match(const char *path, void *userdata) {
         return 0;
 }
 
+static int agent_ask_password_tty(
+                const char *message,
+                usec_t until,
+                AskPasswordFlags flags,
+                const char *flag_file,
+                char ***ret) {
+
+        int tty_fd = -1;
+        int r;
+
+        if (arg_console) {
+                const char *con = arg_device ?: "/dev/console";
+
+                tty_fd = acquire_terminal(con, ACQUIRE_TERMINAL_WAIT, USEC_INFINITY);
+                if (tty_fd < 0)
+                        return log_error_errno(tty_fd, "Failed to acquire %s: %m", con);
+
+                r = reset_terminal_fd(tty_fd, true);
+                if (r < 0)
+                        log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
+
+        }
+
+        r = ask_password_tty(tty_fd, message, NULL, until, flags, flag_file, ret);
+
+        if (arg_console) {
+                tty_fd = safe_close(tty_fd);
+                release_terminal();
+        }
+
+        return 0;
+}
+
 static int process_one_password_file(const char *filename) {
         _cleanup_free_ char *socket_name = NULL, *message = NULL;
         bool accept_cached = false, echo = false;
@@ -355,25 +388,28 @@ static int process_one_password_file(const char *filename) {
         if (pid > 0 && !pid_is_alive(pid))
                 return 0;
 
-        if (arg_action == ACTION_LIST)
+        switch (arg_action) {
+        case ACTION_LIST:
                 printf("'%s' (PID %u)\n", message, pid);
+                return 0;
 
-        else if (arg_action == ACTION_WALL) {
-                _cleanup_free_ char *wall = NULL;
-
-                if (asprintf(&wall,
-                             "Password entry required for \'%s\' (PID %u).\r\n"
-                             "Please enter password with the systemd-tty-ask-password-agent tool.",
-                             message,
-                             pid) < 0)
-                        return log_oom();
+        case ACTION_WALL: {
+                 _cleanup_free_ char *wall = NULL;
 
-                (void) utmp_wall(wall, NULL, NULL, wall_tty_match, NULL);
+                 if (asprintf(&wall,
+                              "Password entry required for \'%s\' (PID %u).\r\n"
+                              "Please enter password with the systemd-tty-ask-password-agent tool.",
+                              message,
+                              pid) < 0)
+                         return log_oom();
 
-        } else {
+                 (void) utmp_wall(wall, NULL, NULL, wall_tty_match, NULL);
+                 return 0;
+        }
+        case ACTION_QUERY:
+        case ACTION_WATCH: {
                 _cleanup_strv_free_erase_ char **passwords = NULL;
-
-                assert(IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH));
+                AskPasswordFlags flags = 0;
 
                 if (access(socket_name, W_OK) < 0) {
                         if (arg_action == ACTION_QUERY)
@@ -382,40 +418,22 @@ static int process_one_password_file(const char *filename) {
                         return 0;
                 }
 
-                if (arg_plymouth)
-                        r = ask_password_plymouth(message, not_after, accept_cached ? ASK_PASSWORD_ACCEPT_CACHED : 0, filename, &passwords);
-                else {
-                        int tty_fd = -1;
-
-                        if (arg_console) {
-                                const char *con = arg_device ?: "/dev/console";
-
-                                tty_fd = acquire_terminal(con, ACQUIRE_TERMINAL_WAIT, USEC_INFINITY);
-                                if (tty_fd < 0)
-                                        return log_error_errno(tty_fd, "Failed to acquire %s: %m", con);
-
-                                r = reset_terminal_fd(tty_fd, true);
-                                if (r < 0)
-                                        log_warning_errno(r, "Failed to reset terminal, ignoring: %m");
-                        }
-
-                        r = ask_password_tty(tty_fd, message, NULL, not_after,
-                                             (echo ? ASK_PASSWORD_ECHO : 0) |
-                                             (arg_console ? ASK_PASSWORD_CONSOLE_COLOR : 0),
-                                             filename, &passwords);
+                SET_FLAG(flags, ASK_PASSWORD_ACCEPT_CACHED, accept_cached);
+                SET_FLAG(flags, ASK_PASSWORD_CONSOLE_COLOR, arg_console);
+                SET_FLAG(flags, ASK_PASSWORD_ECHO, echo);
 
-                        if (arg_console) {
-                                tty_fd = safe_close(tty_fd);
-                                release_terminal();
-                        }
-                }
+                if (arg_plymouth)
+                        r = ask_password_plymouth(message, not_after, flags, filename, &passwords);
+                else
+                        r = agent_ask_password_tty(message, not_after, flags, filename, &passwords);
 
-                /* If the query went away, that's OK */
-                if (IN_SET(r, -ETIME, -ENOENT))
-                        return 0;
+                if (r < 0) {
+                        /* If the query went away, that's OK */
+                        if (IN_SET(r, -ETIME, -ENOENT))
+                                return 0;
 
-                if (r < 0)
                         return log_error_errno(r, "Failed to query password: %m");
+                }
 
                 if (strv_isempty(passwords))
                         return -ECANCELED;
@@ -423,6 +441,8 @@ static int process_one_password_file(const char *filename) {
                 r = send_passwords(socket_name, passwords);
                 if (r < 0)
                         return log_error_errno(r, "Failed to send: %m");
+                break;
+        }
         }
 
         return 0;