]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 | 2 | /*** |
b012e921 | 3 | Copyright © 2015 Werner Fink |
663996b3 MS |
4 | ***/ |
5 | ||
663996b3 | 6 | #include <errno.h> |
6300502b MP |
7 | #include <fcntl.h> |
8 | #include <getopt.h> | |
9 | #include <poll.h> | |
5a920b42 | 10 | #include <signal.h> |
6300502b MP |
11 | #include <stdbool.h> |
12 | #include <stddef.h> | |
663996b3 | 13 | #include <string.h> |
6300502b | 14 | #include <sys/inotify.h> |
5a920b42 | 15 | #include <sys/prctl.h> |
6300502b | 16 | #include <sys/signalfd.h> |
663996b3 | 17 | #include <sys/socket.h> |
bb4f798a MB |
18 | #include <sys/stat.h> |
19 | #include <sys/types.h> | |
663996b3 | 20 | #include <sys/un.h> |
bb4f798a | 21 | #include <sys/wait.h> |
663996b3 | 22 | #include <unistd.h> |
663996b3 | 23 | |
db2df898 | 24 | #include "alloc-util.h" |
6300502b MP |
25 | #include "ask-password-api.h" |
26 | #include "conf-parser.h" | |
27 | #include "def.h" | |
db2df898 | 28 | #include "dirent-util.h" |
5a920b42 | 29 | #include "exit-status.h" |
db2df898 | 30 | #include "fd-util.h" |
5a920b42 MP |
31 | #include "fileio.h" |
32 | #include "hashmap.h" | |
db2df898 | 33 | #include "io-util.h" |
5a920b42 | 34 | #include "macro.h" |
6e866b33 | 35 | #include "main-func.h" |
bb4f798a | 36 | #include "memory-util.h" |
663996b3 MS |
37 | #include "mkdir.h" |
38 | #include "path-util.h" | |
bb4f798a | 39 | #include "plymouth-util.h" |
6e866b33 | 40 | #include "pretty-print.h" |
6300502b MP |
41 | #include "process-util.h" |
42 | #include "signal-util.h" | |
663996b3 | 43 | #include "socket-util.h" |
db2df898 | 44 | #include "string-util.h" |
663996b3 | 45 | #include "strv.h" |
e3bff60a | 46 | #include "terminal-util.h" |
6300502b | 47 | #include "utmp-wtmp.h" |
663996b3 MS |
48 | |
49 | static enum { | |
50 | ACTION_LIST, | |
51 | ACTION_QUERY, | |
52 | ACTION_WATCH, | |
53 | ACTION_WALL | |
54 | } arg_action = ACTION_QUERY; | |
55 | ||
56 | static bool arg_plymouth = false; | |
57 | static bool arg_console = false; | |
5a920b42 | 58 | static const char *arg_device = NULL; |
663996b3 MS |
59 | |
60 | static int ask_password_plymouth( | |
61 | const char *message, | |
62 | usec_t until, | |
6300502b | 63 | AskPasswordFlags flags, |
663996b3 | 64 | const char *flag_file, |
6300502b | 65 | char ***ret) { |
663996b3 | 66 | |
aa27b158 | 67 | static const union sockaddr_union sa = PLYMOUTH_SOCKET; |
5eef597e | 68 | _cleanup_close_ int fd = -1, notify = -1; |
5eef597e | 69 | _cleanup_free_ char *packet = NULL; |
663996b3 MS |
70 | ssize_t k; |
71 | int r, n; | |
72 | struct pollfd pollfd[2] = {}; | |
73 | char buffer[LINE_MAX]; | |
74 | size_t p = 0; | |
75 | enum { | |
76 | POLL_SOCKET, | |
77 | POLL_INOTIFY | |
78 | }; | |
79 | ||
6300502b | 80 | assert(ret); |
663996b3 MS |
81 | |
82 | if (flag_file) { | |
5eef597e MP |
83 | notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); |
84 | if (notify < 0) | |
85 | return -errno; | |
663996b3 | 86 | |
5eef597e MP |
87 | r = inotify_add_watch(notify, flag_file, IN_ATTRIB); /* for the link count */ |
88 | if (r < 0) | |
89 | return -errno; | |
663996b3 MS |
90 | } |
91 | ||
5eef597e MP |
92 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
93 | if (fd < 0) | |
94 | return -errno; | |
663996b3 | 95 | |
aa27b158 | 96 | r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); |
f47781d8 | 97 | if (r < 0) |
6300502b | 98 | return -errno; |
663996b3 | 99 | |
6300502b | 100 | if (flags & ASK_PASSWORD_ACCEPT_CACHED) { |
663996b3 MS |
101 | packet = strdup("c"); |
102 | n = 1; | |
6300502b | 103 | } else if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) |
5eef597e | 104 | packet = NULL; |
5eef597e | 105 | if (!packet) |
6300502b | 106 | return -ENOMEM; |
663996b3 | 107 | |
f47781d8 MP |
108 | r = loop_write(fd, packet, n + 1, true); |
109 | if (r < 0) | |
110 | return r; | |
663996b3 MS |
111 | |
112 | pollfd[POLL_SOCKET].fd = fd; | |
113 | pollfd[POLL_SOCKET].events = POLLIN; | |
114 | pollfd[POLL_INOTIFY].fd = notify; | |
115 | pollfd[POLL_INOTIFY].events = POLLIN; | |
116 | ||
117 | for (;;) { | |
118 | int sleep_for = -1, j; | |
119 | ||
120 | if (until > 0) { | |
121 | usec_t y; | |
122 | ||
123 | y = now(CLOCK_MONOTONIC); | |
124 | ||
db2df898 MP |
125 | if (y > until) { |
126 | r = -ETIME; | |
127 | goto finish; | |
128 | } | |
663996b3 MS |
129 | |
130 | sleep_for = (int) ((until - y) / USEC_PER_MSEC); | |
131 | } | |
132 | ||
db2df898 MP |
133 | if (flag_file && access(flag_file, F_OK) < 0) { |
134 | r = -errno; | |
135 | goto finish; | |
136 | } | |
663996b3 | 137 | |
6300502b | 138 | j = poll(pollfd, notify >= 0 ? 2 : 1, sleep_for); |
5eef597e | 139 | if (j < 0) { |
663996b3 MS |
140 | if (errno == EINTR) |
141 | continue; | |
142 | ||
db2df898 MP |
143 | r = -errno; |
144 | goto finish; | |
145 | } else if (j == 0) { | |
146 | r = -ETIME; | |
147 | goto finish; | |
148 | } | |
663996b3 | 149 | |
6300502b | 150 | if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0) |
1d42b86d | 151 | (void) flush_fd(notify); |
663996b3 MS |
152 | |
153 | if (pollfd[POLL_SOCKET].revents == 0) | |
154 | continue; | |
155 | ||
5eef597e | 156 | k = read(fd, buffer + p, sizeof(buffer) - p); |
6300502b | 157 | if (k < 0) { |
f5e65279 | 158 | if (IN_SET(errno, EINTR, EAGAIN)) |
6300502b MP |
159 | continue; |
160 | ||
db2df898 MP |
161 | r = -errno; |
162 | goto finish; | |
163 | } else if (k == 0) { | |
164 | r = -EIO; | |
165 | goto finish; | |
166 | } | |
663996b3 MS |
167 | |
168 | p += k; | |
169 | ||
170 | if (p < 1) | |
171 | continue; | |
172 | ||
173 | if (buffer[0] == 5) { | |
174 | ||
6300502b | 175 | if (flags & ASK_PASSWORD_ACCEPT_CACHED) { |
663996b3 MS |
176 | /* Hmm, first try with cached |
177 | * passwords failed, so let's retry | |
178 | * with a normal password request */ | |
13d276d0 | 179 | packet = mfree(packet); |
663996b3 | 180 | |
db2df898 MP |
181 | if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) { |
182 | r = -ENOMEM; | |
183 | goto finish; | |
184 | } | |
663996b3 | 185 | |
f47781d8 MP |
186 | r = loop_write(fd, packet, n+1, true); |
187 | if (r < 0) | |
db2df898 | 188 | goto finish; |
663996b3 | 189 | |
6300502b | 190 | flags &= ~ASK_PASSWORD_ACCEPT_CACHED; |
663996b3 MS |
191 | p = 0; |
192 | continue; | |
193 | } | |
194 | ||
195 | /* No password, because UI not shown */ | |
db2df898 MP |
196 | r = -ENOENT; |
197 | goto finish; | |
663996b3 | 198 | |
f5e65279 | 199 | } else if (IN_SET(buffer[0], 2, 9)) { |
663996b3 MS |
200 | uint32_t size; |
201 | char **l; | |
202 | ||
5eef597e | 203 | /* One or more answers */ |
663996b3 MS |
204 | if (p < 5) |
205 | continue; | |
206 | ||
207 | memcpy(&size, buffer+1, sizeof(size)); | |
208 | size = le32toh(size); | |
db2df898 MP |
209 | if (size + 5 > sizeof(buffer)) { |
210 | r = -EIO; | |
211 | goto finish; | |
212 | } | |
663996b3 MS |
213 | |
214 | if (p-5 < size) | |
215 | continue; | |
216 | ||
5eef597e | 217 | l = strv_parse_nulstr(buffer + 5, size); |
db2df898 MP |
218 | if (!l) { |
219 | r = -ENOMEM; | |
220 | goto finish; | |
221 | } | |
663996b3 | 222 | |
6300502b | 223 | *ret = l; |
663996b3 MS |
224 | break; |
225 | ||
db2df898 | 226 | } else { |
663996b3 | 227 | /* Unknown packet */ |
db2df898 MP |
228 | r = -EIO; |
229 | goto finish; | |
230 | } | |
663996b3 MS |
231 | } |
232 | ||
db2df898 MP |
233 | r = 0; |
234 | ||
235 | finish: | |
6e866b33 | 236 | explicit_bzero_safe(buffer, sizeof(buffer)); |
db2df898 | 237 | return r; |
663996b3 MS |
238 | } |
239 | ||
4c89c718 MP |
240 | static int send_passwords(const char *socket_name, char **passwords) { |
241 | _cleanup_free_ char *packet = NULL; | |
242 | _cleanup_close_ int socket_fd = -1; | |
6e866b33 | 243 | union sockaddr_union sa = {}; |
4c89c718 MP |
244 | size_t packet_length = 1; |
245 | char **p, *d; | |
98393f85 | 246 | ssize_t n; |
6e866b33 | 247 | int r, salen; |
4c89c718 MP |
248 | |
249 | assert(socket_name); | |
250 | ||
6e866b33 MB |
251 | salen = sockaddr_un_set_path(&sa.un, socket_name); |
252 | if (salen < 0) | |
253 | return salen; | |
254 | ||
4c89c718 MP |
255 | STRV_FOREACH(p, passwords) |
256 | packet_length += strlen(*p) + 1; | |
257 | ||
258 | packet = new(char, packet_length); | |
259 | if (!packet) | |
260 | return -ENOMEM; | |
261 | ||
262 | packet[0] = '+'; | |
263 | ||
264 | d = packet + 1; | |
265 | STRV_FOREACH(p, passwords) | |
266 | d = stpcpy(d, *p) + 1; | |
267 | ||
268 | socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); | |
269 | if (socket_fd < 0) { | |
270 | r = log_debug_errno(errno, "socket(): %m"); | |
271 | goto finish; | |
272 | } | |
273 | ||
6e866b33 | 274 | n = sendto(socket_fd, packet, packet_length, MSG_NOSIGNAL, &sa.sa, salen); |
98393f85 | 275 | if (n < 0) { |
4c89c718 | 276 | r = log_debug_errno(errno, "sendto(): %m"); |
98393f85 MB |
277 | goto finish; |
278 | } | |
279 | ||
280 | r = (int) n; | |
4c89c718 MP |
281 | |
282 | finish: | |
6e866b33 | 283 | explicit_bzero_safe(packet, packet_length); |
4c89c718 MP |
284 | return r; |
285 | } | |
286 | ||
663996b3 | 287 | static int parse_password(const char *filename, char **wall) { |
4c89c718 | 288 | _cleanup_free_ char *socket_name = NULL, *message = NULL; |
db2df898 | 289 | bool accept_cached = false, echo = false; |
663996b3 MS |
290 | uint64_t not_after = 0; |
291 | unsigned pid = 0; | |
663996b3 MS |
292 | |
293 | const ConfigTableItem items[] = { | |
294 | { "Ask", "Socket", config_parse_string, 0, &socket_name }, | |
295 | { "Ask", "NotAfter", config_parse_uint64, 0, ¬_after }, | |
296 | { "Ask", "Message", config_parse_string, 0, &message }, | |
297 | { "Ask", "PID", config_parse_unsigned, 0, &pid }, | |
298 | { "Ask", "AcceptCached", config_parse_bool, 0, &accept_cached }, | |
5eef597e MP |
299 | { "Ask", "Echo", config_parse_bool, 0, &echo }, |
300 | {} | |
663996b3 MS |
301 | }; |
302 | ||
663996b3 MS |
303 | int r; |
304 | ||
305 | assert(filename); | |
306 | ||
5eef597e MP |
307 | r = config_parse(NULL, filename, NULL, |
308 | NULL, | |
309 | config_item_table_lookup, items, | |
52ad194e | 310 | CONFIG_PARSE_RELAXED|CONFIG_PARSE_WARN, NULL); |
5eef597e MP |
311 | if (r < 0) |
312 | return r; | |
663996b3 | 313 | |
6e866b33 MB |
314 | if (!socket_name) |
315 | return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), | |
316 | "Invalid password file %s", filename); | |
663996b3 | 317 | |
5eef597e MP |
318 | if (not_after > 0 && now(CLOCK_MONOTONIC) > not_after) |
319 | return 0; | |
663996b3 | 320 | |
5eef597e MP |
321 | if (pid > 0 && !pid_is_alive(pid)) |
322 | return 0; | |
663996b3 MS |
323 | |
324 | if (arg_action == ACTION_LIST) | |
325 | printf("'%s' (PID %u)\n", message, pid); | |
5eef597e | 326 | |
663996b3 MS |
327 | else if (arg_action == ACTION_WALL) { |
328 | char *_wall; | |
329 | ||
330 | if (asprintf(&_wall, | |
331 | "%s%sPassword entry required for \'%s\' (PID %u).\r\n" | |
6e866b33 | 332 | "Please enter password with the systemd-tty-ask-password-agent tool:", |
6300502b | 333 | strempty(*wall), |
663996b3 MS |
334 | *wall ? "\r\n\r\n" : "", |
335 | message, | |
5eef597e MP |
336 | pid) < 0) |
337 | return log_oom(); | |
663996b3 MS |
338 | |
339 | free(*wall); | |
340 | *wall = _wall; | |
5eef597e | 341 | |
663996b3 | 342 | } else { |
4c89c718 | 343 | _cleanup_strv_free_erase_ char **passwords = NULL; |
663996b3 | 344 | |
f5e65279 | 345 | assert(IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH)); |
663996b3 MS |
346 | |
347 | if (access(socket_name, W_OK) < 0) { | |
663996b3 MS |
348 | if (arg_action == ACTION_QUERY) |
349 | log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid); | |
350 | ||
5eef597e | 351 | return 0; |
663996b3 MS |
352 | } |
353 | ||
4c89c718 | 354 | if (arg_plymouth) |
6300502b | 355 | r = ask_password_plymouth(message, not_after, accept_cached ? ASK_PASSWORD_ACCEPT_CACHED : 0, filename, &passwords); |
4c89c718 | 356 | else { |
6300502b | 357 | int tty_fd = -1; |
663996b3 | 358 | |
5eef597e | 359 | if (arg_console) { |
98393f85 | 360 | const char *con = arg_device ?: "/dev/console"; |
5a920b42 | 361 | |
98393f85 | 362 | tty_fd = acquire_terminal(con, ACQUIRE_TERMINAL_WAIT, USEC_INFINITY); |
5eef597e | 363 | if (tty_fd < 0) |
98393f85 | 364 | return log_error_errno(tty_fd, "Failed to acquire %s: %m", con); |
db2df898 MP |
365 | |
366 | r = reset_terminal_fd(tty_fd, true); | |
367 | if (r < 0) | |
368 | log_warning_errno(r, "Failed to reset terminal, ignoring: %m"); | |
5eef597e | 369 | } |
663996b3 | 370 | |
98393f85 MB |
371 | r = ask_password_tty(tty_fd, message, NULL, not_after, |
372 | (echo ? ASK_PASSWORD_ECHO : 0) | | |
373 | (arg_console ? ASK_PASSWORD_CONSOLE_COLOR : 0), | |
6e866b33 | 374 | filename, &passwords); |
663996b3 MS |
375 | |
376 | if (arg_console) { | |
6300502b | 377 | tty_fd = safe_close(tty_fd); |
663996b3 MS |
378 | release_terminal(); |
379 | } | |
db2df898 | 380 | } |
663996b3 | 381 | |
4c89c718 MP |
382 | /* If the query went away, that's OK */ |
383 | if (IN_SET(r, -ETIME, -ENOENT)) | |
384 | return 0; | |
663996b3 | 385 | |
4c89c718 MP |
386 | if (r < 0) |
387 | return log_error_errno(r, "Failed to query password: %m"); | |
663996b3 | 388 | |
4c89c718 | 389 | r = send_passwords(socket_name, passwords); |
6300502b | 390 | if (r < 0) |
4c89c718 | 391 | return log_error_errno(r, "Failed to send: %m"); |
663996b3 MS |
392 | } |
393 | ||
5eef597e | 394 | return 0; |
663996b3 MS |
395 | } |
396 | ||
397 | static int wall_tty_block(void) { | |
5eef597e | 398 | _cleanup_free_ char *p = NULL; |
663996b3 | 399 | dev_t devnr; |
6300502b | 400 | int fd, r; |
663996b3 MS |
401 | |
402 | r = get_ctty_devnr(0, &devnr); | |
6300502b MP |
403 | if (r == -ENXIO) /* We have no controlling tty */ |
404 | return -ENOTTY; | |
663996b3 | 405 | if (r < 0) |
6300502b | 406 | return log_error_errno(r, "Failed to get controlling TTY: %m"); |
663996b3 MS |
407 | |
408 | if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(devnr), minor(devnr)) < 0) | |
6300502b | 409 | return log_oom(); |
663996b3 | 410 | |
1d42b86d MB |
411 | (void) mkdir_parents_label(p, 0700); |
412 | (void) mkfifo(p, 0600); | |
663996b3 MS |
413 | |
414 | fd = open(p, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
663996b3 | 415 | if (fd < 0) |
db2df898 | 416 | return log_debug_errno(errno, "Failed to open %s: %m", p); |
663996b3 MS |
417 | |
418 | return fd; | |
419 | } | |
420 | ||
e3bff60a | 421 | static bool wall_tty_match(const char *path, void *userdata) { |
5eef597e | 422 | _cleanup_free_ char *p = NULL; |
6300502b MP |
423 | _cleanup_close_ int fd = -1; |
424 | struct stat st; | |
663996b3 | 425 | |
5eef597e | 426 | if (!path_is_absolute(path)) |
e735f4d4 | 427 | path = strjoina("/dev/", path); |
663996b3 | 428 | |
6300502b MP |
429 | if (lstat(path, &st) < 0) { |
430 | log_debug_errno(errno, "Failed to stat %s: %m", path); | |
663996b3 | 431 | return true; |
6300502b | 432 | } |
663996b3 | 433 | |
6300502b MP |
434 | if (!S_ISCHR(st.st_mode)) { |
435 | log_debug("%s is not a character device.", path); | |
663996b3 | 436 | return true; |
6300502b | 437 | } |
663996b3 MS |
438 | |
439 | /* We use named pipes to ensure that wall messages suggesting | |
440 | * password entry are not printed over password prompts | |
441 | * already shown. We use the fact here that opening a pipe in | |
442 | * non-blocking mode for write-only will succeed only if | |
443 | * there's some writer behind it. Using pipes has the | |
444 | * advantage that the block will automatically go away if the | |
445 | * process dies. */ | |
446 | ||
6300502b MP |
447 | if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0) { |
448 | log_oom(); | |
663996b3 | 449 | return true; |
6300502b | 450 | } |
663996b3 MS |
451 | |
452 | fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
6300502b | 453 | if (fd < 0) { |
98393f85 | 454 | log_debug_errno(errno, "Failed to open the wall pipe: %m"); |
6300502b MP |
455 | return 1; |
456 | } | |
663996b3 MS |
457 | |
458 | /* What, we managed to open the pipe? Then this tty is filtered. */ | |
6300502b | 459 | return 0; |
663996b3 MS |
460 | } |
461 | ||
462 | static int show_passwords(void) { | |
5eef597e | 463 | _cleanup_closedir_ DIR *d; |
663996b3 MS |
464 | struct dirent *de; |
465 | int r = 0; | |
466 | ||
5eef597e MP |
467 | d = opendir("/run/systemd/ask-password"); |
468 | if (!d) { | |
663996b3 MS |
469 | if (errno == ENOENT) |
470 | return 0; | |
471 | ||
db2df898 | 472 | return log_error_errno(errno, "Failed to open /run/systemd/ask-password: %m"); |
663996b3 MS |
473 | } |
474 | ||
6300502b | 475 | FOREACH_DIRENT_ALL(de, d, return log_error_errno(errno, "Failed to read directory: %m")) { |
5eef597e | 476 | _cleanup_free_ char *p = NULL, *wall = NULL; |
663996b3 | 477 | int q; |
663996b3 MS |
478 | |
479 | /* We only support /dev on tmpfs, hence we can rely on | |
480 | * d_type to be reliable */ | |
481 | ||
482 | if (de->d_type != DT_REG) | |
483 | continue; | |
484 | ||
aa27b158 | 485 | if (hidden_or_backup_file(de->d_name)) |
663996b3 MS |
486 | continue; |
487 | ||
488 | if (!startswith(de->d_name, "ask.")) | |
489 | continue; | |
490 | ||
5eef597e MP |
491 | p = strappend("/run/systemd/ask-password/", de->d_name); |
492 | if (!p) | |
493 | return log_oom(); | |
663996b3 | 494 | |
5eef597e MP |
495 | q = parse_password(p, &wall); |
496 | if (q < 0 && r == 0) | |
663996b3 MS |
497 | r = q; |
498 | ||
5eef597e | 499 | if (wall) |
6300502b | 500 | (void) utmp_wall(wall, NULL, NULL, wall_tty_match, NULL); |
663996b3 MS |
501 | } |
502 | ||
663996b3 MS |
503 | return r; |
504 | } | |
505 | ||
506 | static int watch_passwords(void) { | |
507 | enum { | |
508 | FD_INOTIFY, | |
509 | FD_SIGNAL, | |
510 | _FD_MAX | |
511 | }; | |
512 | ||
5eef597e | 513 | _cleanup_close_ int notify = -1, signal_fd = -1, tty_block_fd = -1; |
663996b3 MS |
514 | struct pollfd pollfd[_FD_MAX] = {}; |
515 | sigset_t mask; | |
516 | int r; | |
517 | ||
518 | tty_block_fd = wall_tty_block(); | |
519 | ||
6300502b | 520 | (void) mkdir_p_label("/run/systemd/ask-password", 0755); |
663996b3 | 521 | |
5eef597e MP |
522 | notify = inotify_init1(IN_CLOEXEC); |
523 | if (notify < 0) | |
6300502b | 524 | return log_error_errno(errno, "Failed to allocate directory watch: %m"); |
663996b3 | 525 | |
6e866b33 MB |
526 | if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) { |
527 | if (errno == ENOSPC) | |
528 | return log_error_errno(errno, "Failed to add /run/systemd/ask-password to directory watch: inotify watch limit reached"); | |
529 | else | |
530 | return log_error_errno(errno, "Failed to add /run/systemd/ask-password to directory watch: %m"); | |
531 | } | |
663996b3 | 532 | |
86f210e9 MP |
533 | assert_se(sigemptyset(&mask) >= 0); |
534 | assert_se(sigset_add_many(&mask, SIGINT, SIGTERM, -1) >= 0); | |
535 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) >= 0); | |
663996b3 | 536 | |
5eef597e MP |
537 | signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
538 | if (signal_fd < 0) | |
6300502b | 539 | return log_error_errno(errno, "Failed to allocate signal file descriptor: %m"); |
663996b3 MS |
540 | |
541 | pollfd[FD_INOTIFY].fd = notify; | |
542 | pollfd[FD_INOTIFY].events = POLLIN; | |
543 | pollfd[FD_SIGNAL].fd = signal_fd; | |
544 | pollfd[FD_SIGNAL].events = POLLIN; | |
545 | ||
546 | for (;;) { | |
5eef597e MP |
547 | r = show_passwords(); |
548 | if (r < 0) | |
f47781d8 | 549 | log_error_errno(r, "Failed to show password: %m"); |
663996b3 MS |
550 | |
551 | if (poll(pollfd, _FD_MAX, -1) < 0) { | |
663996b3 MS |
552 | if (errno == EINTR) |
553 | continue; | |
554 | ||
5eef597e | 555 | return -errno; |
663996b3 MS |
556 | } |
557 | ||
558 | if (pollfd[FD_INOTIFY].revents != 0) | |
6300502b | 559 | (void) flush_fd(notify); |
663996b3 MS |
560 | |
561 | if (pollfd[FD_SIGNAL].revents != 0) | |
562 | break; | |
563 | } | |
564 | ||
5eef597e | 565 | return 0; |
663996b3 MS |
566 | } |
567 | ||
6e866b33 MB |
568 | static int help(void) { |
569 | _cleanup_free_ char *link = NULL; | |
570 | int r; | |
571 | ||
572 | r = terminal_urlify_man("systemd-tty-ask-password-agent", "1", &link); | |
573 | if (r < 0) | |
574 | return log_oom(); | |
575 | ||
663996b3 MS |
576 | printf("%s [OPTIONS...]\n\n" |
577 | "Process system password requests.\n\n" | |
578 | " -h --help Show this help\n" | |
579 | " --version Show package version\n" | |
580 | " --list Show pending password requests\n" | |
581 | " --query Process pending password requests\n" | |
582 | " --watch Continuously process password requests\n" | |
583 | " --wall Continuously forward password requests to wall\n" | |
584 | " --plymouth Ask question with Plymouth instead of on TTY\n" | |
6e866b33 MB |
585 | " --console Ask question on /dev/console instead of current TTY\n" |
586 | "\nSee the %s for details.\n" | |
587 | , program_invocation_short_name | |
588 | , link | |
589 | ); | |
590 | ||
591 | return 0; | |
663996b3 MS |
592 | } |
593 | ||
594 | static int parse_argv(int argc, char *argv[]) { | |
595 | ||
596 | enum { | |
597 | ARG_LIST = 0x100, | |
598 | ARG_QUERY, | |
599 | ARG_WATCH, | |
600 | ARG_WALL, | |
601 | ARG_PLYMOUTH, | |
602 | ARG_CONSOLE, | |
603 | ARG_VERSION | |
604 | }; | |
605 | ||
606 | static const struct option options[] = { | |
5a920b42 MP |
607 | { "help", no_argument, NULL, 'h' }, |
608 | { "version", no_argument, NULL, ARG_VERSION }, | |
609 | { "list", no_argument, NULL, ARG_LIST }, | |
610 | { "query", no_argument, NULL, ARG_QUERY }, | |
611 | { "watch", no_argument, NULL, ARG_WATCH }, | |
612 | { "wall", no_argument, NULL, ARG_WALL }, | |
613 | { "plymouth", no_argument, NULL, ARG_PLYMOUTH }, | |
614 | { "console", optional_argument, NULL, ARG_CONSOLE }, | |
60f067b4 | 615 | {} |
663996b3 MS |
616 | }; |
617 | ||
618 | int c; | |
619 | ||
620 | assert(argc >= 0); | |
621 | assert(argv); | |
622 | ||
5eef597e | 623 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
663996b3 MS |
624 | |
625 | switch (c) { | |
626 | ||
627 | case 'h': | |
6e866b33 | 628 | return help(); |
663996b3 MS |
629 | |
630 | case ARG_VERSION: | |
6300502b | 631 | return version(); |
663996b3 MS |
632 | |
633 | case ARG_LIST: | |
634 | arg_action = ACTION_LIST; | |
635 | break; | |
636 | ||
637 | case ARG_QUERY: | |
638 | arg_action = ACTION_QUERY; | |
639 | break; | |
640 | ||
641 | case ARG_WATCH: | |
642 | arg_action = ACTION_WATCH; | |
643 | break; | |
644 | ||
645 | case ARG_WALL: | |
646 | arg_action = ACTION_WALL; | |
647 | break; | |
648 | ||
649 | case ARG_PLYMOUTH: | |
650 | arg_plymouth = true; | |
651 | break; | |
652 | ||
653 | case ARG_CONSOLE: | |
654 | arg_console = true; | |
5a920b42 MP |
655 | if (optarg) { |
656 | ||
6e866b33 MB |
657 | if (isempty(optarg)) |
658 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
659 | "Empty console device path is not allowed."); | |
5a920b42 MP |
660 | |
661 | arg_device = optarg; | |
662 | } | |
663996b3 MS |
663 | break; |
664 | ||
665 | case '?': | |
666 | return -EINVAL; | |
667 | ||
668 | default: | |
60f067b4 | 669 | assert_not_reached("Unhandled option"); |
663996b3 | 670 | } |
663996b3 | 671 | |
6e866b33 MB |
672 | if (optind != argc) |
673 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
674 | "%s takes no arguments.", program_invocation_short_name); | |
663996b3 | 675 | |
5a920b42 MP |
676 | if (arg_plymouth || arg_console) { |
677 | ||
6e866b33 MB |
678 | if (!IN_SET(arg_action, ACTION_QUERY, ACTION_WATCH)) |
679 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
680 | "Options --query and --watch conflict."); | |
5a920b42 | 681 | |
6e866b33 MB |
682 | if (arg_plymouth && arg_console) |
683 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
684 | "Options --plymouth and --console conflict."); | |
5a920b42 MP |
685 | } |
686 | ||
663996b3 MS |
687 | return 1; |
688 | } | |
689 | ||
5a920b42 | 690 | /* |
bb4f798a MB |
691 | * To be able to ask on all terminal devices of /dev/console the devices are collected. If more than one |
692 | * device is found, then on each of the terminals a inquiring task is forked. Every task has its own session | |
693 | * and its own controlling terminal. If one of the tasks does handle a password, the remaining tasks will be | |
694 | * terminated. | |
5a920b42 | 695 | */ |
bb4f798a MB |
696 | static int ask_on_this_console(const char *tty, pid_t *ret_pid, char **arguments) { |
697 | static const struct sigaction sigchld = { | |
5a920b42 MP |
698 | .sa_handler = nop_signal_handler, |
699 | .sa_flags = SA_NOCLDSTOP | SA_RESTART, | |
700 | }; | |
bb4f798a MB |
701 | static const struct sigaction sighup = { |
702 | .sa_handler = SIG_DFL, | |
703 | .sa_flags = SA_RESTART, | |
704 | }; | |
1d42b86d | 705 | int r; |
5a920b42 | 706 | |
bb4f798a MB |
707 | assert_se(sigaction(SIGCHLD, &sigchld, NULL) >= 0); |
708 | assert_se(sigaction(SIGHUP, &sighup, NULL) >= 0); | |
5a920b42 MP |
709 | assert_se(sigprocmask_many(SIG_UNBLOCK, NULL, SIGHUP, SIGCHLD, -1) >= 0); |
710 | ||
bb4f798a | 711 | r = safe_fork("(sd-passwd)", FORK_RESET_SIGNALS|FORK_LOG, ret_pid); |
1d42b86d MB |
712 | if (r < 0) |
713 | return r; | |
714 | if (r == 0) { | |
bb4f798a | 715 | char **i; |
5a920b42 MP |
716 | |
717 | assert_se(prctl(PR_SET_PDEATHSIG, SIGHUP) >= 0); | |
718 | ||
bb4f798a MB |
719 | STRV_FOREACH(i, arguments) { |
720 | char *k; | |
721 | ||
722 | if (!streq(*i, "--console")) | |
723 | continue; | |
724 | ||
725 | k = strjoin("--console=", tty); | |
726 | if (!k) { | |
727 | log_oom(); | |
728 | _exit(EXIT_FAILURE); | |
5a920b42 | 729 | } |
5a920b42 | 730 | |
bb4f798a MB |
731 | free_and_replace(*i, k); |
732 | } | |
5a920b42 | 733 | |
bb4f798a | 734 | execv(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, arguments); |
5a920b42 MP |
735 | _exit(EXIT_FAILURE); |
736 | } | |
1d42b86d | 737 | |
5a920b42 MP |
738 | return 0; |
739 | } | |
740 | ||
741 | static void terminate_agents(Set *pids) { | |
742 | struct timespec ts; | |
743 | siginfo_t status = {}; | |
744 | sigset_t set; | |
745 | Iterator i; | |
746 | void *p; | |
747 | int r, signum; | |
748 | ||
749 | /* | |
750 | * Request termination of the remaining processes as those | |
751 | * are not required anymore. | |
752 | */ | |
753 | SET_FOREACH(p, pids, i) | |
754 | (void) kill(PTR_TO_PID(p), SIGTERM); | |
755 | ||
756 | /* | |
757 | * Collect the processes which have go away. | |
758 | */ | |
759 | assert_se(sigemptyset(&set) >= 0); | |
760 | assert_se(sigaddset(&set, SIGCHLD) >= 0); | |
761 | timespec_store(&ts, 50 * USEC_PER_MSEC); | |
762 | ||
763 | while (!set_isempty(pids)) { | |
764 | ||
765 | zero(status); | |
766 | r = waitid(P_ALL, 0, &status, WEXITED|WNOHANG); | |
767 | if (r < 0 && errno == EINTR) | |
768 | continue; | |
769 | ||
770 | if (r == 0 && status.si_pid > 0) { | |
771 | set_remove(pids, PID_TO_PTR(status.si_pid)); | |
772 | continue; | |
773 | } | |
774 | ||
775 | signum = sigtimedwait(&set, NULL, &ts); | |
776 | if (signum < 0) { | |
777 | if (errno != EAGAIN) | |
778 | log_error_errno(errno, "sigtimedwait() failed: %m"); | |
779 | break; | |
780 | } | |
781 | assert(signum == SIGCHLD); | |
782 | } | |
783 | ||
784 | /* | |
785 | * Kill hanging processes. | |
786 | */ | |
787 | SET_FOREACH(p, pids, i) { | |
788 | log_warning("Failed to terminate child %d, killing it", PTR_TO_PID(p)); | |
789 | (void) kill(PTR_TO_PID(p), SIGKILL); | |
790 | } | |
791 | } | |
792 | ||
bb4f798a | 793 | static int ask_on_consoles(char *argv[]) { |
5a920b42 | 794 | _cleanup_set_free_ Set *pids = NULL; |
bb4f798a | 795 | _cleanup_strv_free_ char **consoles = NULL, **arguments = NULL; |
5a920b42 MP |
796 | siginfo_t status = {}; |
797 | char **tty; | |
798 | pid_t pid; | |
799 | int r; | |
800 | ||
801 | r = get_kernel_consoles(&consoles); | |
802 | if (r < 0) | |
803 | return log_error_errno(r, "Failed to determine devices of /dev/console: %m"); | |
804 | ||
805 | pids = set_new(NULL); | |
806 | if (!pids) | |
807 | return log_oom(); | |
808 | ||
bb4f798a MB |
809 | arguments = strv_copy(argv); |
810 | if (!arguments) | |
811 | return log_oom(); | |
812 | ||
5a920b42 MP |
813 | /* Start an agent on each console. */ |
814 | STRV_FOREACH(tty, consoles) { | |
bb4f798a | 815 | r = ask_on_this_console(*tty, &pid, arguments); |
5a920b42 MP |
816 | if (r < 0) |
817 | return r; | |
818 | ||
819 | if (set_put(pids, PID_TO_PTR(pid)) < 0) | |
820 | return log_oom(); | |
821 | } | |
822 | ||
823 | /* Wait for an agent to exit. */ | |
824 | for (;;) { | |
825 | zero(status); | |
826 | ||
827 | if (waitid(P_ALL, 0, &status, WEXITED) < 0) { | |
828 | if (errno == EINTR) | |
829 | continue; | |
830 | ||
831 | return log_error_errno(errno, "waitid() failed: %m"); | |
832 | } | |
833 | ||
834 | set_remove(pids, PID_TO_PTR(status.si_pid)); | |
835 | break; | |
836 | } | |
837 | ||
8a584da2 | 838 | if (!is_clean_exit(status.si_code, status.si_status, EXIT_CLEAN_DAEMON, NULL)) |
5a920b42 MP |
839 | log_error("Password agent failed with: %d", status.si_status); |
840 | ||
841 | terminate_agents(pids); | |
842 | return 0; | |
843 | } | |
844 | ||
6e866b33 | 845 | static int run(int argc, char *argv[]) { |
663996b3 MS |
846 | int r; |
847 | ||
6e866b33 | 848 | log_setup_service(); |
663996b3 MS |
849 | |
850 | umask(0022); | |
851 | ||
5eef597e MP |
852 | r = parse_argv(argc, argv); |
853 | if (r <= 0) | |
6e866b33 | 854 | return r; |
663996b3 | 855 | |
5a920b42 MP |
856 | if (arg_console && !arg_device) |
857 | /* | |
6e866b33 | 858 | * Spawn a separate process for each console device. |
5a920b42 | 859 | */ |
bb4f798a | 860 | return ask_on_consoles(argv); |
663996b3 | 861 | |
6e866b33 MB |
862 | if (arg_device) { |
863 | /* | |
864 | * Later on, a controlling terminal will be acquired, | |
865 | * therefore the current process has to become a session | |
866 | * leader and should not have a controlling terminal already. | |
867 | */ | |
868 | (void) setsid(); | |
869 | (void) release_terminal(); | |
5a920b42 | 870 | } |
663996b3 | 871 | |
6e866b33 MB |
872 | if (IN_SET(arg_action, ACTION_WATCH, ACTION_WALL)) |
873 | return watch_passwords(); | |
874 | else | |
875 | return show_passwords(); | |
663996b3 | 876 | } |
6e866b33 MB |
877 | |
878 | DEFINE_MAIN_FUNCTION(run); |