| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2015 Werner Fink |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <fcntl.h> |
| 7 |
#include <poll.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <sys/prctl.h> |
| 10 |
#include <sys/signalfd.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <sys/wait.h> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
#include "alloc-util.h" |
| 16 |
#include "ask-password-api.h" |
| 17 |
#include "build.h" |
| 18 |
#include "conf-parser.h" |
| 19 |
#include "daemon-util.h" |
| 20 |
#include "devnum-util.h" |
| 21 |
#include "dirent-util.h" |
| 22 |
#include "errno-util.h" |
| 23 |
#include "exit-status.h" |
| 24 |
#include "fd-util.h" |
| 25 |
#include "fileio.h" |
| 26 |
#include "format-table.h" |
| 27 |
#include "format-util.h" |
| 28 |
#include "inotify-util.h" |
| 29 |
#include "io-util.h" |
| 30 |
#include "main-func.h" |
| 31 |
#include "mkdir.h" |
| 32 |
#include "options.h" |
| 33 |
#include "path-util.h" |
| 34 |
#include "pidref.h" |
| 35 |
#include "pretty-print.h" |
| 36 |
#include "process-util.h" |
| 37 |
#include "set.h" |
| 38 |
#include "signal-util.h" |
| 39 |
#include "socket-util.h" |
| 40 |
#include "static-destruct.h" |
| 41 |
#include "string-util.h" |
| 42 |
#include "strv.h" |
| 43 |
#include "terminal-util.h" |
| 44 |
#include "time-util.h" |
| 45 |
#include "wall.h" |
| 46 |
|
| 47 |
static enum { |
| 48 |
ACTION_LIST, |
| 49 |
ACTION_QUERY, |
| 50 |
ACTION_WATCH, |
| 51 |
ACTION_WALL, |
| 52 |
} arg_action = ACTION_QUERY; |
| 53 |
|
| 54 |
static bool arg_plymouth = false; |
| 55 |
static bool arg_console = false; |
| 56 |
static char *arg_device = NULL; |
| 57 |
|
| 58 |
STATIC_DESTRUCTOR_REGISTER(arg_device, freep); |
| 59 |
|
| 60 |
static int send_passwords(const char *socket_name, char **passwords) { |
| 61 |
int r; |
| 62 |
|
| 63 |
assert(socket_name); |
| 64 |
|
| 65 |
union sockaddr_union sa; |
| 66 |
r = sockaddr_un_set_path(&sa.un, socket_name); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
socklen_t sa_len = r; |
| 70 |
|
| 71 |
size_t packet_length = 1; |
| 72 |
STRV_FOREACH(p, passwords) |
| 73 |
packet_length += strlen(*p) + 1; |
| 74 |
|
| 75 |
_cleanup_(erase_and_freep) char *packet = new(char, packet_length); |
| 76 |
if (!packet) |
| 77 |
return -ENOMEM; |
| 78 |
|
| 79 |
packet[0] = '+'; |
| 80 |
|
| 81 |
char *d = packet + 1; |
| 82 |
STRV_FOREACH(p, passwords) |
| 83 |
d = stpcpy(d, *p) + 1; |
| 84 |
|
| 85 |
_cleanup_close_ int socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); |
| 86 |
if (socket_fd < 0) |
| 87 |
return log_debug_errno(errno, "socket(): %m"); |
| 88 |
|
| 89 |
ssize_t n = sendto(socket_fd, packet, packet_length, MSG_NOSIGNAL, &sa.sa, sa_len); |
| 90 |
if (n < 0) |
| 91 |
return log_debug_errno(errno, "sendto(): %m"); |
| 92 |
|
| 93 |
return (int) n; |
| 94 |
} |
| 95 |
|
| 96 |
static bool wall_tty_match(const char *path, bool is_local, void *userdata) { |
| 97 |
assert(path_is_absolute(path)); |
| 98 |
|
| 99 |
struct stat st; |
| 100 |
if (lstat(path, &st) < 0) { |
| 101 |
log_debug_errno(errno, "Failed to stat TTY '%s', not restricting wall: %m", path); |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
if (!S_ISCHR(st.st_mode)) { |
| 106 |
log_debug("TTY '%s' is not a character device, not restricting wall.", path); |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
/* We use named pipes to ensure that wall messages suggesting password entry are not printed over |
| 111 |
* password prompts already shown. We use the fact here that opening a pipe in non-blocking mode for |
| 112 |
* write-only will succeed only if there's some writer behind it. Using pipes has the advantage that |
| 113 |
* the block will automatically go away if the process dies. */ |
| 114 |
|
| 115 |
_cleanup_free_ char *p = NULL; |
| 116 |
if (asprintf(&p, "/run/systemd/ask-password-block/" DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(st.st_rdev)) < 0) { |
| 117 |
log_oom_debug(); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
_cleanup_close_ int fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 122 |
if (fd < 0) { |
| 123 |
log_debug_errno(errno, "Failed to open the wall pipe for TTY '%s', not restricting wall: %m", path); |
| 124 |
return 1; |
| 125 |
} |
| 126 |
|
| 127 |
/* What, we managed to open the pipe? Then this tty is filtered. */ |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static int agent_ask_password_tty( |
| 132 |
const char *message, |
| 133 |
usec_t until, |
| 134 |
AskPasswordFlags flags, |
| 135 |
const char *flag_file, |
| 136 |
char ***ret) { |
| 137 |
|
| 138 |
int tty_fd = -EBADF, r; |
| 139 |
const char *con = arg_device ?: "/dev/console"; |
| 140 |
|
| 141 |
if (arg_console) { |
| 142 |
tty_fd = acquire_terminal(con, ACQUIRE_TERMINAL_WAIT|ACQUIRE_TERMINAL_WATCH_SIGTERM, USEC_INFINITY); |
| 143 |
if (tty_fd < 0) |
| 144 |
return log_error_errno(tty_fd, "Failed to acquire %s: %m", con); |
| 145 |
|
| 146 |
(void) terminal_reset_defensive_locked(tty_fd, TERMINAL_RESET_SWITCH_TO_TEXT); |
| 147 |
|
| 148 |
log_info("Starting password query on %s.", con); |
| 149 |
} |
| 150 |
|
| 151 |
AskPasswordRequest req = { |
| 152 |
.tty_fd = tty_fd, |
| 153 |
.message = message, |
| 154 |
.flag_file = flag_file, |
| 155 |
.until = until, |
| 156 |
.hup_fd = -EBADF, |
| 157 |
}; |
| 158 |
|
| 159 |
r = ask_password_tty(&req, flags, ret); |
| 160 |
|
| 161 |
if (arg_console) { |
| 162 |
assert(tty_fd >= 0); |
| 163 |
tty_fd = safe_close(tty_fd); |
| 164 |
release_terminal(); |
| 165 |
|
| 166 |
if (r >= 0) |
| 167 |
log_info("Password query on %s finished successfully.", con); |
| 168 |
} |
| 169 |
|
| 170 |
return r; |
| 171 |
} |
| 172 |
|
| 173 |
static int process_one_password_file(const char *filename, FILE *f) { |
| 174 |
_cleanup_free_ char *socket_name = NULL, *message = NULL; |
| 175 |
bool accept_cached = false, echo = false, silent = false; |
| 176 |
uint64_t not_after = 0; |
| 177 |
pid_t pid = 0; |
| 178 |
|
| 179 |
const ConfigTableItem items[] = { |
| 180 |
{ "Ask", "Socket", config_parse_string, CONFIG_PARSE_STRING_SAFE, &socket_name }, |
| 181 |
{ "Ask", "NotAfter", config_parse_uint64, 0, ¬_after }, |
| 182 |
{ "Ask", "Message", config_parse_string, 0, &message }, |
| 183 |
{ "Ask", "PID", config_parse_pid, 0, &pid }, |
| 184 |
{ "Ask", "AcceptCached", config_parse_bool, 0, &accept_cached }, |
| 185 |
{ "Ask", "Echo", config_parse_bool, 0, &echo }, |
| 186 |
{ "Ask", "Silent", config_parse_bool, 0, &silent }, |
| 187 |
{} |
| 188 |
}; |
| 189 |
|
| 190 |
int r; |
| 191 |
|
| 192 |
assert(filename); |
| 193 |
assert(f); |
| 194 |
|
| 195 |
r = config_parse(/* unit= */ NULL, |
| 196 |
filename, |
| 197 |
f, |
| 198 |
/* sections= */ "Ask\0", |
| 199 |
config_item_table_lookup, |
| 200 |
items, |
| 201 |
CONFIG_PARSE_RELAXED|CONFIG_PARSE_WARN, |
| 202 |
/* userdata= */ NULL, |
| 203 |
/* ret_stat= */ NULL); |
| 204 |
if (r < 0) |
| 205 |
return r; |
| 206 |
|
| 207 |
if (!socket_name) |
| 208 |
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 209 |
"Invalid password file %s", filename); |
| 210 |
|
| 211 |
if (not_after > 0 && now(CLOCK_MONOTONIC) > not_after) |
| 212 |
return 0; |
| 213 |
|
| 214 |
if (pid > 0 && pid_is_alive(pid) <= 0) |
| 215 |
return 0; |
| 216 |
|
| 217 |
switch (arg_action) { |
| 218 |
case ACTION_LIST: |
| 219 |
printf("'%s' (PID " PID_FMT ")\n", strna(message), pid); |
| 220 |
return 0; |
| 221 |
|
| 222 |
case ACTION_WALL: { |
| 223 |
_cleanup_free_ char *msg = NULL; |
| 224 |
|
| 225 |
if (asprintf(&msg, |
| 226 |
"Password entry required for \'%s\' (PID " PID_FMT ").\r\n" |
| 227 |
"Please enter password with the systemd-tty-ask-password-agent tool.", |
| 228 |
strna(message), |
| 229 |
pid) < 0) |
| 230 |
return log_oom(); |
| 231 |
|
| 232 |
(void) wall(msg, NULL, NULL, wall_tty_match, NULL); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
case ACTION_QUERY: |
| 236 |
case ACTION_WATCH: { |
| 237 |
_cleanup_strv_free_erase_ char **passwords = NULL; |
| 238 |
AskPasswordFlags flags = 0; |
| 239 |
|
| 240 |
if (access(socket_name, W_OK) < 0) { |
| 241 |
if (arg_action == ACTION_QUERY) |
| 242 |
log_info("Not querying '%s' (PID " PID_FMT "), lacking privileges.", strna(message), pid); |
| 243 |
|
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
SET_FLAG(flags, ASK_PASSWORD_ACCEPT_CACHED, accept_cached); |
| 248 |
SET_FLAG(flags, ASK_PASSWORD_CONSOLE_COLOR, arg_console); |
| 249 |
SET_FLAG(flags, ASK_PASSWORD_ECHO, echo); |
| 250 |
SET_FLAG(flags, ASK_PASSWORD_SILENT, silent); |
| 251 |
|
| 252 |
/* Allow providing a password via env var, for debugging purposes */ |
| 253 |
const char *e = secure_getenv("SYSTEMD_ASK_PASSWORD_AGENT_PASSWORD"); |
| 254 |
if (e) { |
| 255 |
passwords = strv_new(e); |
| 256 |
if (!passwords) |
| 257 |
return log_oom(); |
| 258 |
} else { |
| 259 |
if (arg_plymouth) { |
| 260 |
AskPasswordRequest req = { |
| 261 |
.tty_fd = -EBADF, |
| 262 |
.message = message, |
| 263 |
.flag_file = filename, |
| 264 |
.until = not_after, |
| 265 |
.hup_fd = -EBADF, |
| 266 |
}; |
| 267 |
|
| 268 |
r = ask_password_plymouth(&req, flags, &passwords); |
| 269 |
} else |
| 270 |
r = agent_ask_password_tty(message, not_after, flags, filename, &passwords); |
| 271 |
if (r < 0) { |
| 272 |
/* If the query went away, that's OK */ |
| 273 |
if (IN_SET(r, -ETIME, -ENOENT)) |
| 274 |
return 0; |
| 275 |
|
| 276 |
return log_error_errno(r, "Failed to query password: %m"); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
assert(!strv_isempty(passwords)); |
| 281 |
r = send_passwords(socket_name, passwords); |
| 282 |
if (r < 0) |
| 283 |
return log_error_errno(r, "Failed to send: %m"); |
| 284 |
break; |
| 285 |
}} |
| 286 |
|
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
static int wall_tty_block(void) { |
| 291 |
_cleanup_free_ char *p = NULL; |
| 292 |
dev_t devnr; |
| 293 |
int fd, r; |
| 294 |
|
| 295 |
r = get_ctty_devnr(0, &devnr); |
| 296 |
if (r == -ENXIO) /* We have no controlling tty */ |
| 297 |
return -ENOTTY; |
| 298 |
if (r < 0) |
| 299 |
return log_error_errno(r, "Failed to get controlling TTY: %m"); |
| 300 |
|
| 301 |
if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(devnr), minor(devnr)) < 0) |
| 302 |
return log_oom(); |
| 303 |
|
| 304 |
(void) mkdir_parents_label(p, 0700); |
| 305 |
(void) mkfifo(p, 0600); |
| 306 |
|
| 307 |
fd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 308 |
if (fd < 0) |
| 309 |
return log_debug_errno(errno, "Failed to open %s: %m", p); |
| 310 |
|
| 311 |
return fd; |
| 312 |
} |
| 313 |
|
| 314 |
static int process_password_files(const char *path) { |
| 315 |
_cleanup_closedir_ DIR *d = NULL; |
| 316 |
int ret = 0, r; |
| 317 |
|
| 318 |
assert(path); |
| 319 |
|
| 320 |
d = opendir(path); |
| 321 |
if (!d) { |
| 322 |
if (errno == ENOENT) |
| 323 |
return 0; |
| 324 |
|
| 325 |
return log_error_errno(errno, "Failed to open '%s': %m", path); |
| 326 |
} |
| 327 |
|
| 328 |
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read directory '%s': %m", path)) { |
| 329 |
_cleanup_free_ char *p = NULL; |
| 330 |
|
| 331 |
if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN)) |
| 332 |
continue; |
| 333 |
|
| 334 |
if (!startswith(de->d_name, "ask.")) |
| 335 |
continue; |
| 336 |
|
| 337 |
p = path_join(path, de->d_name); |
| 338 |
if (!p) |
| 339 |
return log_oom(); |
| 340 |
|
| 341 |
_cleanup_fclose_ FILE *f = NULL; |
| 342 |
r = xfopenat(dirfd(d), de->d_name, "re", O_NOFOLLOW, &f); |
| 343 |
if (r < 0) { |
| 344 |
log_warning_errno(r, "Failed to open '%s', ignoring: %m", p); |
| 345 |
continue; |
| 346 |
} |
| 347 |
|
| 348 |
RET_GATHER(ret, process_one_password_file(p, f)); |
| 349 |
} |
| 350 |
|
| 351 |
return ret; |
| 352 |
} |
| 353 |
|
| 354 |
static int process_and_watch_password_files(bool watch) { |
| 355 |
enum { |
| 356 |
FD_SIGNAL, |
| 357 |
FD_INOTIFY, |
| 358 |
_FD_MAX |
| 359 |
}; |
| 360 |
|
| 361 |
_cleanup_free_ char *user_ask_password_directory = NULL; |
| 362 |
_unused_ _cleanup_close_ int tty_block_fd = -EBADF; |
| 363 |
_cleanup_close_ int notify = -EBADF, signal_fd = -EBADF; |
| 364 |
struct pollfd pollfd[_FD_MAX]; |
| 365 |
sigset_t mask; |
| 366 |
int r; |
| 367 |
|
| 368 |
tty_block_fd = wall_tty_block(); |
| 369 |
|
| 370 |
(void) mkdir_p_label("/run/systemd/ask-password", 0755); |
| 371 |
|
| 372 |
r = acquire_user_ask_password_directory(&user_ask_password_directory); |
| 373 |
if (r < 0) |
| 374 |
return log_error_errno(r, "Failed to determine per-user password directory: %m"); |
| 375 |
if (r > 0) |
| 376 |
(void) mkdir_p_label(user_ask_password_directory, 0755); |
| 377 |
|
| 378 |
assert_se(sigemptyset(&mask) >= 0); |
| 379 |
assert_se(sigset_add_many(&mask, SIGTERM) >= 0); |
| 380 |
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) >= 0); |
| 381 |
|
| 382 |
if (watch) { |
| 383 |
signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
| 384 |
if (signal_fd < 0) |
| 385 |
return log_error_errno(errno, "Failed to allocate signal file descriptor: %m"); |
| 386 |
|
| 387 |
pollfd[FD_SIGNAL] = (struct pollfd) { .fd = signal_fd, .events = POLLIN }; |
| 388 |
|
| 389 |
notify = inotify_init1(IN_CLOEXEC); |
| 390 |
if (notify < 0) |
| 391 |
return log_error_errno(errno, "Failed to allocate directory watch: %m"); |
| 392 |
|
| 393 |
r = inotify_add_watch_and_warn(notify, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO|IN_ONLYDIR); |
| 394 |
if (r < 0) |
| 395 |
return r; |
| 396 |
|
| 397 |
if (user_ask_password_directory) { |
| 398 |
r = inotify_add_watch_and_warn(notify, user_ask_password_directory, IN_CLOSE_WRITE|IN_MOVED_TO|IN_ONLYDIR); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
} |
| 402 |
|
| 403 |
pollfd[FD_INOTIFY] = (struct pollfd) { .fd = notify, .events = POLLIN }; |
| 404 |
} |
| 405 |
|
| 406 |
_unused_ _cleanup_(notify_on_cleanup) const char *notify_stop = |
| 407 |
notify_start(NOTIFY_READY_MESSAGE, NOTIFY_STOPPING_MESSAGE); |
| 408 |
|
| 409 |
for (;;) { |
| 410 |
usec_t timeout = USEC_INFINITY; |
| 411 |
|
| 412 |
r = process_password_files("/run/systemd/ask-password"); |
| 413 |
if (user_ask_password_directory) |
| 414 |
RET_GATHER(r, process_password_files(user_ask_password_directory)); |
| 415 |
if (r == -ECANCELED) |
| 416 |
/* Disable poll() timeout since at least one password has been skipped and therefore |
| 417 |
* one file remains and is unlikely to trigger any events. */ |
| 418 |
timeout = 0; |
| 419 |
else if (r < 0) |
| 420 |
/* FIXME: we should do something here since otherwise the service |
| 421 |
* requesting the password won't notice the error and will wait |
| 422 |
* indefinitely. */ |
| 423 |
log_warning_errno(r, "Failed to process password, ignoring: %m"); |
| 424 |
|
| 425 |
if (!watch) |
| 426 |
break; |
| 427 |
|
| 428 |
r = ppoll_usec(pollfd, _FD_MAX, timeout); |
| 429 |
if (r == -EINTR) |
| 430 |
continue; |
| 431 |
if (r < 0) |
| 432 |
return r; |
| 433 |
|
| 434 |
if (pollfd[FD_INOTIFY].revents != 0) |
| 435 |
(void) flush_fd(notify); |
| 436 |
|
| 437 |
if (pollfd[FD_SIGNAL].revents != 0) |
| 438 |
break; |
| 439 |
} |
| 440 |
|
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
static int help(void) { |
| 445 |
_cleanup_free_ char *link = NULL; |
| 446 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 447 |
int r; |
| 448 |
|
| 449 |
r = terminal_urlify_man("systemd-tty-ask-password-agent", "1", &link); |
| 450 |
if (r < 0) |
| 451 |
return log_oom(); |
| 452 |
|
| 453 |
r = option_parser_get_help_table(&options); |
| 454 |
if (r < 0) |
| 455 |
return r; |
| 456 |
|
| 457 |
printf("%s [OPTIONS...]\n" |
| 458 |
"\n%sProcess system password requests.%s\n\n", |
| 459 |
program_invocation_short_name, |
| 460 |
ansi_highlight(), |
| 461 |
ansi_normal()); |
| 462 |
|
| 463 |
r = table_print_or_warn(options); |
| 464 |
if (r < 0) |
| 465 |
return r; |
| 466 |
|
| 467 |
printf("\nSee the %s for details.\n", link); |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
static int parse_argv(int argc, char *argv[]) { |
| 472 |
assert(argc >= 0); |
| 473 |
assert(argv); |
| 474 |
|
| 475 |
OptionParser opts = { argc, argv }; |
| 476 |
int r; |
| 477 |
|
| 478 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 479 |
switch (c) { |
| 480 |
|
| 481 |
OPTION_COMMON_HELP: |
| 482 |
return help(); |
| 483 |
|
| 484 |
OPTION_COMMON_VERSION: |
| 485 |
return version(); |
| 486 |
|
| 487 |
OPTION_LONG("list", NULL, "Show pending password requests"): |
| 488 |
arg_action = ACTION_LIST; |
| 489 |
break; |
| 490 |
|
| 491 |
OPTION_LONG("query", NULL, "Process pending password requests"): |
| 492 |
arg_action = ACTION_QUERY; |
| 493 |
break; |
| 494 |
|
| 495 |
OPTION_LONG("watch", NULL, "Continuously process password requests"): |
| 496 |
arg_action = ACTION_WATCH; |
| 497 |
break; |
| 498 |
|
| 499 |
OPTION_LONG("wall", NULL, "Continuously forward password requests to wall"): |
| 500 |
arg_action = ACTION_WALL; |
| 501 |
break; |
| 502 |
|
| 503 |
OPTION_LONG("plymouth", NULL, |
| 504 |
"Ask question with Plymouth instead of on TTY"): |
| 505 |
arg_plymouth = true; |
| 506 |
break; |
| 507 |
|
| 508 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "console", "DEVICE", |
| 509 |
"Ask question on /dev/console (or DEVICE if specified) instead of the current TTY"): |
| 510 |
arg_console = true; |
| 511 |
if (opts.arg) { |
| 512 |
if (isempty(opts.arg)) |
| 513 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 514 |
"Empty console device path is not allowed."); |
| 515 |
|
| 516 |
r = free_and_strdup_warn(&arg_device, opts.arg); |
| 517 |
if (r < 0) |
| 518 |
return r; |
| 519 |
} |
| 520 |
break; |
| 521 |
} |
| 522 |
|
| 523 |
if (option_parser_get_n_args(&opts) > 0) |
| 524 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 525 |
"%s takes no arguments.", program_invocation_short_name); |
| 526 |
|
| 527 |
if (arg_plymouth || arg_console) { |
| 528 |
|
| 529 |
if (!IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH)) |
| 530 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 531 |
"Options --query and --watch conflict."); |
| 532 |
|
| 533 |
if (arg_plymouth && arg_console) |
| 534 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 535 |
"Options --plymouth and --console conflict."); |
| 536 |
} |
| 537 |
|
| 538 |
return 1; |
| 539 |
} |
| 540 |
|
| 541 |
/* |
| 542 |
* To be able to ask on all terminal devices of /dev/console the devices are collected. If more than one |
| 543 |
* device is found, then on each of the terminals an inquiring task is forked. Every task has its own session |
| 544 |
* and its own controlling terminal. If one of the tasks does handle a password, the remaining tasks will be |
| 545 |
* terminated. |
| 546 |
*/ |
| 547 |
static int ask_on_this_console(const char *tty, char **arguments, PidRef *ret) { |
| 548 |
int r; |
| 549 |
|
| 550 |
assert(tty); |
| 551 |
assert(arguments); |
| 552 |
assert(ret); |
| 553 |
|
| 554 |
assert_se(sigaction(SIGCHLD, &sigaction_nop_nocldstop, NULL) >= 0); |
| 555 |
assert_se(sigaction(SIGHUP, &sigaction_default, NULL) >= 0); |
| 556 |
assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD) >= 0); |
| 557 |
|
| 558 |
r = pidref_safe_fork("(sd-passwd)", FORK_RESET_SIGNALS|FORK_KEEP_NOTIFY_SOCKET|FORK_LOG, ret); |
| 559 |
if (r < 0) |
| 560 |
return r; |
| 561 |
if (r == 0) { |
| 562 |
assert_se(prctl(PR_SET_PDEATHSIG, SIGHUP) >= 0); |
| 563 |
|
| 564 |
STRV_FOREACH(i, arguments) { |
| 565 |
char *k; |
| 566 |
|
| 567 |
if (!streq(*i, "--console")) |
| 568 |
continue; |
| 569 |
|
| 570 |
k = strjoin("--console=", tty); |
| 571 |
if (!k) { |
| 572 |
log_oom(); |
| 573 |
_exit(EXIT_FAILURE); |
| 574 |
} |
| 575 |
|
| 576 |
free_and_replace(*i, k); |
| 577 |
} |
| 578 |
|
| 579 |
execv(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, arguments); |
| 580 |
_exit(EXIT_FAILURE); |
| 581 |
} |
| 582 |
|
| 583 |
return 0; |
| 584 |
} |
| 585 |
|
| 586 |
static void terminate_agents(Set *pids) { |
| 587 |
sigset_t set; |
| 588 |
void *p; |
| 589 |
int r, signum; |
| 590 |
|
| 591 |
/* |
| 592 |
* Request termination of the remaining processes as those |
| 593 |
* are not required anymore. |
| 594 |
*/ |
| 595 |
SET_FOREACH(p, pids) |
| 596 |
(void) kill(PTR_TO_PID(p), SIGTERM); |
| 597 |
|
| 598 |
/* |
| 599 |
* Collect the processes which have go away. |
| 600 |
*/ |
| 601 |
assert_se(sigemptyset(&set) >= 0); |
| 602 |
assert_se(sigaddset(&set, SIGCHLD) >= 0); |
| 603 |
|
| 604 |
while (!set_isempty(pids)) { |
| 605 |
siginfo_t status = {}; |
| 606 |
|
| 607 |
r = waitid(P_ALL, 0, &status, WEXITED|WNOHANG); |
| 608 |
if (r < 0 && errno == EINTR) |
| 609 |
continue; |
| 610 |
|
| 611 |
if (r == 0 && status.si_pid > 0) { |
| 612 |
set_remove(pids, PID_TO_PTR(status.si_pid)); |
| 613 |
continue; |
| 614 |
} |
| 615 |
|
| 616 |
signum = sigtimedwait(&set, NULL, TIMESPEC_STORE(50 * USEC_PER_MSEC)); |
| 617 |
if (signum < 0) { |
| 618 |
if (errno != EAGAIN) |
| 619 |
log_error_errno(errno, "sigtimedwait() failed: %m"); |
| 620 |
break; |
| 621 |
} |
| 622 |
assert(signum == SIGCHLD); |
| 623 |
} |
| 624 |
|
| 625 |
/* |
| 626 |
* Kill hanging processes. |
| 627 |
*/ |
| 628 |
SET_FOREACH(p, pids) { |
| 629 |
log_warning("Failed to terminate child %d, killing it", PTR_TO_PID(p)); |
| 630 |
(void) kill(PTR_TO_PID(p), SIGKILL); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
static int ask_on_consoles(char *argv[]) { |
| 635 |
_cleanup_strv_free_ char **consoles = NULL, **arguments = NULL; |
| 636 |
_cleanup_set_free_ Set *pids = NULL; |
| 637 |
int r; |
| 638 |
|
| 639 |
assert(!arg_device); |
| 640 |
assert(argv); |
| 641 |
|
| 642 |
r = get_kernel_consoles(&consoles); |
| 643 |
if (r < 0) |
| 644 |
return log_error_errno(r, "Failed to determine devices of /dev/console: %m"); |
| 645 |
if (r <= 1) { |
| 646 |
/* No need to spawn subprocesses, there's only one console or using /dev/console as fallback */ |
| 647 |
arg_device = TAKE_PTR(consoles[0]); |
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
pids = set_new(NULL); |
| 652 |
if (!pids) |
| 653 |
return log_oom(); |
| 654 |
|
| 655 |
arguments = strv_copy(argv); |
| 656 |
if (!arguments) |
| 657 |
return log_oom(); |
| 658 |
|
| 659 |
/* Grant agents we spawn notify access too, so that once an agent establishes inotify watch |
| 660 |
* READY=1 from them is accepted by service manager (see process_and_watch_password_files()). |
| 661 |
* |
| 662 |
* Note that when any agent exits STOPPING=1 would also be sent, but that's utterly what we want, |
| 663 |
* i.e. the password is answered on one console and other agents get killed below. */ |
| 664 |
(void) sd_notify(/* unset_environment= */ false, "NOTIFYACCESS=all"); |
| 665 |
|
| 666 |
/* Start an agent on each console. */ |
| 667 |
STRV_FOREACH(tty, consoles) { |
| 668 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 669 |
|
| 670 |
r = ask_on_this_console(*tty, arguments, &pidref); |
| 671 |
if (r < 0) |
| 672 |
return r; |
| 673 |
|
| 674 |
if (set_put(pids, PID_TO_PTR(pidref.pid)) < 0) |
| 675 |
return log_oom(); |
| 676 |
} |
| 677 |
|
| 678 |
/* Wait for an agent to exit. */ |
| 679 |
for (;;) { |
| 680 |
siginfo_t status = {}; |
| 681 |
|
| 682 |
if (waitid(P_ALL, 0, &status, WEXITED) < 0) { |
| 683 |
if (errno == EINTR) |
| 684 |
continue; |
| 685 |
|
| 686 |
return log_error_errno(errno, "Failed to wait for console ask-password agent: %m"); |
| 687 |
} |
| 688 |
|
| 689 |
if (!is_clean_exit(status.si_code, status.si_status, EXIT_CLEAN_DAEMON, NULL)) |
| 690 |
log_error("Password agent failed with: %d", status.si_status); |
| 691 |
|
| 692 |
set_remove(pids, PID_TO_PTR(status.si_pid)); |
| 693 |
break; |
| 694 |
} |
| 695 |
|
| 696 |
terminate_agents(pids); |
| 697 |
return 1; |
| 698 |
} |
| 699 |
|
| 700 |
static int run(int argc, char *argv[]) { |
| 701 |
int r; |
| 702 |
|
| 703 |
log_setup(); |
| 704 |
|
| 705 |
umask(0022); |
| 706 |
|
| 707 |
r = parse_argv(argc, argv); |
| 708 |
if (r <= 0) |
| 709 |
return r; |
| 710 |
|
| 711 |
/* Spawn a separate process for each console device if there're multiple. */ |
| 712 |
if (arg_console && !arg_device) { |
| 713 |
r = ask_on_consoles(argv); |
| 714 |
if (r != 0) |
| 715 |
return r; |
| 716 |
|
| 717 |
assert(arg_device); |
| 718 |
} |
| 719 |
|
| 720 |
if (arg_device) |
| 721 |
/* Later on, a controlling terminal will be acquired, therefore the current process has to |
| 722 |
* become a session leader and should not have a controlling terminal already. */ |
| 723 |
terminal_detach_session(); |
| 724 |
|
| 725 |
return process_and_watch_password_files(!IN_SET(arg_action, ACTION_QUERY, ACTION_LIST)); |
| 726 |
} |
| 727 |
|
| 728 |
DEFINE_MAIN_FUNCTION(run); |