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;
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)
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;
r = send_passwords(socket_name, passwords);
if (r < 0)
return log_error_errno(r, "Failed to send: %m");
+ break;
+ }
}
return 0;