]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/ask-password-api.c
unit: skip default cgroup setup if we have no hierarchy to work on
[thirdparty/systemd.git] / src / ask-password-api.c
CommitLineData
7f4e0805
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
d1676230 21#include <stdbool.h>
7f4e0805
LP
22#include <termios.h>
23#include <unistd.h>
24#include <sys/poll.h>
25#include <sys/inotify.h>
26#include <errno.h>
27#include <fcntl.h>
28#include <sys/socket.h>
29#include <string.h>
30#include <sys/un.h>
31#include <stddef.h>
32#include <sys/signalfd.h>
33
34#include "util.h"
21bc923a 35#include "strv.h"
7f4e0805
LP
36
37#include "ask-password-api.h"
38
58fc840b
LP
39static void backspace_chars(int ttyfd, size_t p) {
40
41 if (ttyfd < 0)
42 return;
43
44 while (p > 0) {
45 p--;
46
47 loop_write(ttyfd, "\b \b", 3, false);
48 }
49}
50
7f4e0805
LP
51int ask_password_tty(
52 const char *message,
53 usec_t until,
54 const char *flag_file,
55 char **_passphrase) {
56
57 struct termios old_termios, new_termios;
58 char passphrase[LINE_MAX];
59 size_t p = 0;
60 int r, ttyfd = -1, notify = -1;
61 struct pollfd pollfd[2];
62 bool reset_tty = false;
d1676230 63 bool silent_mode = false;
441dfe09 64 bool dirty = false;
7f4e0805
LP
65 enum {
66 POLL_TTY,
67 POLL_INOTIFY
68 };
69
70 assert(message);
71 assert(_passphrase);
72
73 if (flag_file) {
74 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
75 r = -errno;
76 goto finish;
77 }
78
79 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
80 r = -errno;
81 goto finish;
82 }
83 }
84
85 if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC)) >= 0) {
86
87 if (tcgetattr(ttyfd, &old_termios) < 0) {
88 r = -errno;
89 goto finish;
90 }
91
92 loop_write(ttyfd, "\x1B[1m", 4, false);
93 loop_write(ttyfd, message, strlen(message), false);
bd1db33f 94 loop_write(ttyfd, " ", 1, false);
7f4e0805
LP
95 loop_write(ttyfd, "\x1B[0m", 4, false);
96
97 new_termios = old_termios;
98 new_termios.c_lflag &= ~(ICANON|ECHO);
99 new_termios.c_cc[VMIN] = 1;
100 new_termios.c_cc[VTIME] = 0;
101
102 if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) {
103 r = -errno;
104 goto finish;
105 }
106
107 reset_tty = true;
108 }
109
110 zero(pollfd);
111
112 pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
113 pollfd[POLL_TTY].events = POLLIN;
114 pollfd[POLL_INOTIFY].fd = notify;
115 pollfd[POLL_INOTIFY].events = POLLIN;
116
117 for (;;) {
118 char c;
119 int sleep_for = -1, k;
120 ssize_t n;
121
122 if (until > 0) {
123 usec_t y;
124
125 y = now(CLOCK_MONOTONIC);
126
127 if (y > until) {
7ded2e28 128 r = -ETIME;
7f4e0805
LP
129 goto finish;
130 }
131
132 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
133 }
134
135 if (flag_file)
136 if (access(flag_file, F_OK) < 0) {
137 r = -errno;
138 goto finish;
139 }
140
141 if ((k = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
142
143 if (errno == EINTR)
144 continue;
145
146 r = -errno;
147 goto finish;
148 } else if (k == 0) {
ccc80078 149 r = -ETIME;
7f4e0805
LP
150 goto finish;
151 }
152
153 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
154 flush_fd(notify);
155
156 if (pollfd[POLL_TTY].revents == 0)
157 continue;
158
159 if ((n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1)) < 0) {
160
161 if (errno == EINTR || errno == EAGAIN)
162 continue;
163
164 r = -errno;
165 goto finish;
166
167 } else if (n == 0)
168 break;
169
170 if (c == '\n')
171 break;
58fc840b 172 else if (c == 21) { /* C-u */
7f4e0805 173
58fc840b
LP
174 if (!silent_mode)
175 backspace_chars(ttyfd, p);
176 p = 0;
7f4e0805
LP
177
178 } else if (c == '\b' || c == 127) {
58fc840b
LP
179
180 if (p > 0) {
181
182 if (!silent_mode)
183 backspace_chars(ttyfd, 1);
184
7f4e0805 185 p--;
441dfe09
LP
186 } else if (!dirty && !silent_mode) {
187
188 silent_mode = true;
189
190 /* There are two ways to enter silent
191 * mode. Either by pressing backspace
192 * as first key (and only as first key),
193 * or ... */
194 if (ttyfd >= 0)
195 loop_write(ttyfd, "(no echo) ", 10, false);
196
58fc840b
LP
197 } else if (ttyfd >= 0)
198 loop_write(ttyfd, "\a", 1, false);
7f4e0805 199
58fc840b
LP
200 } else if (c == '\t' && !silent_mode) {
201
202 backspace_chars(ttyfd, p);
203 silent_mode = true;
204
441dfe09
LP
205 /* ... or by pressing TAB at any time. */
206
58fc840b
LP
207 if (ttyfd >= 0)
208 loop_write(ttyfd, "(no echo) ", 10, false);
7f4e0805
LP
209 } else {
210 passphrase[p++] = c;
211
d1676230 212 if (!silent_mode && ttyfd >= 0)
7f4e0805 213 loop_write(ttyfd, "*", 1, false);
441dfe09
LP
214
215 dirty = true;
7f4e0805
LP
216 }
217 }
218
7f4e0805
LP
219 passphrase[p] = 0;
220
221 if (!(*_passphrase = strdup(passphrase))) {
222 r = -ENOMEM;
223 goto finish;
224 }
225
226 r = 0;
227
228finish:
229 if (notify >= 0)
230 close_nointr_nofail(notify);
231
232 if (ttyfd >= 0) {
c0f9c7da
LP
233
234 if (reset_tty) {
235 loop_write(ttyfd, "\n", 1, false);
7f4e0805 236 tcsetattr(ttyfd, TCSADRAIN, &old_termios);
c0f9c7da 237 }
7f4e0805
LP
238
239 close_nointr_nofail(ttyfd);
240 }
241
242 return r;
243}
244
245static int create_socket(char **name) {
246 int fd;
247 union {
248 struct sockaddr sa;
249 struct sockaddr_un un;
250 } sa;
251 int one = 1, r;
252 char *c;
253
254 assert(name);
255
256 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
257 log_error("socket() failed: %m");
258 return -errno;
259 }
260
261 zero(sa);
262 sa.un.sun_family = AF_UNIX;
2b583ce6 263 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%llu", random_ull());
7f4e0805
LP
264
265 if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
266 r = -errno;
267 log_error("bind() failed: %m");
268 goto fail;
269 }
270
271 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
272 r = -errno;
273 log_error("SO_PASSCRED failed: %m");
274 goto fail;
275 }
276
277 if (!(c = strdup(sa.un.sun_path))) {
278 r = -ENOMEM;
279 log_error("Out of memory");
280 goto fail;
281 }
282
283 *name = c;
284 return fd;
285
286fail:
287 close_nointr_nofail(fd);
288
289 return r;
290}
291
7f4e0805
LP
292int ask_password_agent(
293 const char *message,
294 const char *icon,
295 usec_t until,
21bc923a
LP
296 bool accept_cached,
297 char ***_passphrases) {
7f4e0805
LP
298
299 enum {
300 FD_SOCKET,
301 FD_SIGNAL,
302 _FD_MAX
303 };
304
2b583ce6 305 char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
7f4e0805
LP
306 char final[sizeof(temp)] = "";
307 int fd = -1, r;
308 FILE *f = NULL;
309 char *socket_name = NULL;
310 int socket_fd = -1, signal_fd = -1;
f9b72cd8 311 sigset_t mask, oldmask;
7f4e0805
LP
312 struct pollfd pollfd[_FD_MAX];
313
21bc923a
LP
314 assert(_passphrases);
315
f9b72cd8
LP
316 assert_se(sigemptyset(&mask) == 0);
317 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
318 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
319
2b583ce6 320 mkdir_p("/run/systemd/ask-password", 0755);
7f4e0805
LP
321
322 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
323 log_error("Failed to create password file: %m");
324 r = -errno;
325 goto finish;
326 }
327
328 fchmod(fd, 0644);
329
330 if (!(f = fdopen(fd, "w"))) {
331 log_error("Failed to allocate FILE: %m");
332 r = -errno;
333 goto finish;
334 }
335
336 fd = -1;
337
7f4e0805
LP
338 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
339 log_error("signalfd(): %m");
340 r = -errno;
341 goto finish;
342 }
343
344 if ((socket_fd = create_socket(&socket_name)) < 0) {
345 r = socket_fd;
346 goto finish;
347 }
348
349 fprintf(f,
350 "[Ask]\n"
351 "PID=%lu\n"
352 "Socket=%s\n"
21bc923a 353 "AcceptCached=%i\n"
7f4e0805
LP
354 "NotAfter=%llu\n",
355 (unsigned long) getpid(),
356 socket_name,
21bc923a 357 accept_cached ? 1 : 0,
7f4e0805
LP
358 (unsigned long long) until);
359
360 if (message)
361 fprintf(f, "Message=%s\n", message);
362
363 if (icon)
364 fprintf(f, "Icon=%s\n", icon);
365
366 fflush(f);
367
368 if (ferror(f)) {
369 log_error("Failed to write query file: %m");
370 r = -errno;
371 goto finish;
372 }
373
374 memcpy(final, temp, sizeof(temp));
375
376 final[sizeof(final)-11] = 'a';
377 final[sizeof(final)-10] = 's';
378 final[sizeof(final)-9] = 'k';
379
380 if (rename(temp, final) < 0) {
381 log_error("Failed to rename query file: %m");
382 r = -errno;
383 goto finish;
384 }
385
386 zero(pollfd);
387 pollfd[FD_SOCKET].fd = socket_fd;
388 pollfd[FD_SOCKET].events = POLLIN;
389 pollfd[FD_SIGNAL].fd = signal_fd;
390 pollfd[FD_SIGNAL].events = POLLIN;
391
392 for (;;) {
393 char passphrase[LINE_MAX+1];
394 struct msghdr msghdr;
395 struct iovec iovec;
396 struct ucred *ucred;
397 union {
398 struct cmsghdr cmsghdr;
399 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
400 } control;
401 ssize_t n;
402 int k;
403 usec_t t;
404
405 t = now(CLOCK_MONOTONIC);
406
407 if (until <= t) {
408 log_notice("Timed out");
409 r = -ETIME;
410 goto finish;
411 }
412
7ded2e28 413 if ((k = poll(pollfd, _FD_MAX, (until-t)/USEC_PER_MSEC)) < 0) {
7f4e0805
LP
414
415 if (errno == EINTR)
416 continue;
417
418 log_error("poll() failed: %m");
419 r = -errno;
420 goto finish;
421 }
422
423 if (k <= 0) {
424 log_notice("Timed out");
425 r = -ETIME;
426 goto finish;
427 }
428
21bc923a
LP
429 if (pollfd[FD_SIGNAL].revents & POLLIN) {
430 r = -EINTR;
431 goto finish;
432 }
7f4e0805
LP
433
434 if (pollfd[FD_SOCKET].revents != POLLIN) {
435 log_error("Unexpected poll() event.");
436 r = -EIO;
437 goto finish;
438 }
439
440 zero(iovec);
441 iovec.iov_base = passphrase;
21bc923a 442 iovec.iov_len = sizeof(passphrase);
7f4e0805
LP
443
444 zero(control);
445 zero(msghdr);
446 msghdr.msg_iov = &iovec;
447 msghdr.msg_iovlen = 1;
448 msghdr.msg_control = &control;
449 msghdr.msg_controllen = sizeof(control);
450
451 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
452
453 if (errno == EAGAIN ||
454 errno == EINTR)
455 continue;
456
457 log_error("recvmsg() failed: %m");
458 r = -errno;
459 goto finish;
460 }
461
462 if (n <= 0) {
463 log_error("Message too short");
464 continue;
465 }
466
467 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
468 control.cmsghdr.cmsg_level != SOL_SOCKET ||
469 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
470 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
471 log_warning("Received message without credentials. Ignoring.");
472 continue;
473 }
474
475 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
476 if (ucred->uid != 0) {
477 log_warning("Got request from unprivileged user. Ignoring.");
478 continue;
479 }
480
481 if (passphrase[0] == '+') {
21bc923a 482 char **l;
7f4e0805 483
21bc923a 484 if (!(l = strv_parse_nulstr(passphrase+1, n-1))) {
7f4e0805
LP
485 r = -ENOMEM;
486 goto finish;
487 }
488
21bc923a
LP
489 if (strv_length(l) <= 0) {
490 strv_free(l);
491 log_error("Invalid packet");
492 continue;
493 }
494
495 *_passphrases = l;
496
7f4e0805
LP
497 } else if (passphrase[0] == '-') {
498 r = -ECANCELED;
499 goto finish;
500 } else {
501 log_error("Invalid packet");
502 continue;
503 }
504
505 break;
506 }
507
508 r = 0;
509
510finish:
511 if (fd >= 0)
512 close_nointr_nofail(fd);
513
514 if (socket_name) {
515 unlink(socket_name);
516 free(socket_name);
517 }
518
519 if (socket_fd >= 0)
520 close_nointr_nofail(socket_fd);
521
522 if (signal_fd >= 0)
523 close_nointr_nofail(signal_fd);
524
525 if (f)
526 fclose(f);
527
528 unlink(temp);
529
530 if (final[0])
531 unlink(final);
532
f9b72cd8
LP
533 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
534
7f4e0805
LP
535 return r;
536}
260ab287 537
21bc923a 538int ask_password_auto(const char *message, const char *icon, usec_t until, bool accept_cached, char ***_passphrases) {
260ab287 539 assert(message);
21bc923a
LP
540 assert(_passphrases);
541
542 if (isatty(STDIN_FILENO)) {
543 int r;
544 char *s = NULL, **l = NULL;
545
546 if ((r = ask_password_tty(message, until, NULL, &s)) < 0)
547 return r;
548
549 l = strv_new(s, NULL);
550 free(s);
551
552 if (!l)
553 return -ENOMEM;
554
555 *_passphrases = l;
556 return r;
260ab287 557
21bc923a
LP
558 } else
559 return ask_password_agent(message, icon, until, accept_cached, _passphrases);
260ab287 560}