]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/ask-password-api.c
main: print warning if /usr is on a seperate partition
[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***/
21
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"
35
36#include "ask-password-api.h"
37
38int ask_password_tty(
39 const char *message,
40 usec_t until,
41 const char *flag_file,
42 char **_passphrase) {
43
44 struct termios old_termios, new_termios;
45 char passphrase[LINE_MAX];
46 size_t p = 0;
47 int r, ttyfd = -1, notify = -1;
48 struct pollfd pollfd[2];
49 bool reset_tty = false;
50 enum {
51 POLL_TTY,
52 POLL_INOTIFY
53 };
54
55 assert(message);
56 assert(_passphrase);
57
58 if (flag_file) {
59 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
60 r = -errno;
61 goto finish;
62 }
63
64 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
65 r = -errno;
66 goto finish;
67 }
68 }
69
70 if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC)) >= 0) {
71
72 if (tcgetattr(ttyfd, &old_termios) < 0) {
73 r = -errno;
74 goto finish;
75 }
76
77 loop_write(ttyfd, "\x1B[1m", 4, false);
78 loop_write(ttyfd, message, strlen(message), false);
bd1db33f 79 loop_write(ttyfd, " ", 1, false);
7f4e0805
LP
80 loop_write(ttyfd, "\x1B[0m", 4, false);
81
82 new_termios = old_termios;
83 new_termios.c_lflag &= ~(ICANON|ECHO);
84 new_termios.c_cc[VMIN] = 1;
85 new_termios.c_cc[VTIME] = 0;
86
87 if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) {
88 r = -errno;
89 goto finish;
90 }
91
92 reset_tty = true;
93 }
94
95 zero(pollfd);
96
97 pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
98 pollfd[POLL_TTY].events = POLLIN;
99 pollfd[POLL_INOTIFY].fd = notify;
100 pollfd[POLL_INOTIFY].events = POLLIN;
101
102 for (;;) {
103 char c;
104 int sleep_for = -1, k;
105 ssize_t n;
106
107 if (until > 0) {
108 usec_t y;
109
110 y = now(CLOCK_MONOTONIC);
111
112 if (y > until) {
113 r = -ETIMEDOUT;
114 goto finish;
115 }
116
117 sleep_for = (int) ((until - y) / USEC_PER_MSEC);
118 }
119
120 if (flag_file)
121 if (access(flag_file, F_OK) < 0) {
122 r = -errno;
123 goto finish;
124 }
125
126 if ((k = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
127
128 if (errno == EINTR)
129 continue;
130
131 r = -errno;
132 goto finish;
133 } else if (k == 0) {
134 r = -ETIMEDOUT;
135 goto finish;
136 }
137
138 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
139 flush_fd(notify);
140
141 if (pollfd[POLL_TTY].revents == 0)
142 continue;
143
144 if ((n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1)) < 0) {
145
146 if (errno == EINTR || errno == EAGAIN)
147 continue;
148
149 r = -errno;
150 goto finish;
151
152 } else if (n == 0)
153 break;
154
155 if (c == '\n')
156 break;
157 else if (c == 21) {
158
159 while (p > 0) {
160 p--;
161
162 if (ttyfd >= 0)
163 loop_write(ttyfd, "\b \b", 3, false);
164 }
165
166 } else if (c == '\b' || c == 127) {
167 if (p > 0) {
168 p--;
169
170 if (ttyfd >= 0)
171 loop_write(ttyfd, "\b \b", 3, false);
172 }
173 } else {
174 passphrase[p++] = c;
175
176 if (ttyfd >= 0)
177 loop_write(ttyfd, "*", 1, false);
178 }
179 }
180
181 if (ttyfd >= 0)
182 loop_write(ttyfd, "\n", 1, false);
183
184 passphrase[p] = 0;
185
186 if (!(*_passphrase = strdup(passphrase))) {
187 r = -ENOMEM;
188 goto finish;
189 }
190
191 r = 0;
192
193finish:
194 if (notify >= 0)
195 close_nointr_nofail(notify);
196
197 if (ttyfd >= 0) {
198 if (reset_tty)
199 tcsetattr(ttyfd, TCSADRAIN, &old_termios);
200
201 close_nointr_nofail(ttyfd);
202 }
203
204 return r;
205}
206
207static int create_socket(char **name) {
208 int fd;
209 union {
210 struct sockaddr sa;
211 struct sockaddr_un un;
212 } sa;
213 int one = 1, r;
214 char *c;
215
216 assert(name);
217
218 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
219 log_error("socket() failed: %m");
220 return -errno;
221 }
222
223 zero(sa);
224 sa.un.sun_family = AF_UNIX;
225 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/dev/.systemd/ask-password/sck.%llu", random_ull());
226
227 if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
228 r = -errno;
229 log_error("bind() failed: %m");
230 goto fail;
231 }
232
233 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
234 r = -errno;
235 log_error("SO_PASSCRED failed: %m");
236 goto fail;
237 }
238
239 if (!(c = strdup(sa.un.sun_path))) {
240 r = -ENOMEM;
241 log_error("Out of memory");
242 goto fail;
243 }
244
245 *name = c;
246 return fd;
247
248fail:
249 close_nointr_nofail(fd);
250
251 return r;
252}
253
254
255int ask_password_agent(
256 const char *message,
257 const char *icon,
258 usec_t until,
259 char **_passphrase) {
260
261 enum {
262 FD_SOCKET,
263 FD_SIGNAL,
264 _FD_MAX
265 };
266
267 char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
268 char final[sizeof(temp)] = "";
269 int fd = -1, r;
270 FILE *f = NULL;
271 char *socket_name = NULL;
272 int socket_fd = -1, signal_fd = -1;
273 sigset_t mask;
274 struct pollfd pollfd[_FD_MAX];
275
276 mkdir_p("/dev/.systemd/ask-password", 0755);
277
278 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
279 log_error("Failed to create password file: %m");
280 r = -errno;
281 goto finish;
282 }
283
284 fchmod(fd, 0644);
285
286 if (!(f = fdopen(fd, "w"))) {
287 log_error("Failed to allocate FILE: %m");
288 r = -errno;
289 goto finish;
290 }
291
292 fd = -1;
293
294 assert_se(sigemptyset(&mask) == 0);
295 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
296 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
297
298 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
299 log_error("signalfd(): %m");
300 r = -errno;
301 goto finish;
302 }
303
304 if ((socket_fd = create_socket(&socket_name)) < 0) {
305 r = socket_fd;
306 goto finish;
307 }
308
309 fprintf(f,
310 "[Ask]\n"
311 "PID=%lu\n"
312 "Socket=%s\n"
313 "NotAfter=%llu\n",
314 (unsigned long) getpid(),
315 socket_name,
316 (unsigned long long) until);
317
318 if (message)
319 fprintf(f, "Message=%s\n", message);
320
321 if (icon)
322 fprintf(f, "Icon=%s\n", icon);
323
324 fflush(f);
325
326 if (ferror(f)) {
327 log_error("Failed to write query file: %m");
328 r = -errno;
329 goto finish;
330 }
331
332 memcpy(final, temp, sizeof(temp));
333
334 final[sizeof(final)-11] = 'a';
335 final[sizeof(final)-10] = 's';
336 final[sizeof(final)-9] = 'k';
337
338 if (rename(temp, final) < 0) {
339 log_error("Failed to rename query file: %m");
340 r = -errno;
341 goto finish;
342 }
343
344 zero(pollfd);
345 pollfd[FD_SOCKET].fd = socket_fd;
346 pollfd[FD_SOCKET].events = POLLIN;
347 pollfd[FD_SIGNAL].fd = signal_fd;
348 pollfd[FD_SIGNAL].events = POLLIN;
349
350 for (;;) {
351 char passphrase[LINE_MAX+1];
352 struct msghdr msghdr;
353 struct iovec iovec;
354 struct ucred *ucred;
355 union {
356 struct cmsghdr cmsghdr;
357 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
358 } control;
359 ssize_t n;
360 int k;
361 usec_t t;
362
363 t = now(CLOCK_MONOTONIC);
364
365 if (until <= t) {
366 log_notice("Timed out");
367 r = -ETIME;
368 goto finish;
369 }
370
371 if ((k = poll(pollfd, _FD_MAX, until-t/USEC_PER_MSEC)) < 0) {
372
373 if (errno == EINTR)
374 continue;
375
376 log_error("poll() failed: %m");
377 r = -errno;
378 goto finish;
379 }
380
381 if (k <= 0) {
382 log_notice("Timed out");
383 r = -ETIME;
384 goto finish;
385 }
386
387 if (pollfd[FD_SIGNAL].revents & POLLIN)
388 break;
389
390 if (pollfd[FD_SOCKET].revents != POLLIN) {
391 log_error("Unexpected poll() event.");
392 r = -EIO;
393 goto finish;
394 }
395
396 zero(iovec);
397 iovec.iov_base = passphrase;
398 iovec.iov_len = sizeof(passphrase)-1;
399
400 zero(control);
401 zero(msghdr);
402 msghdr.msg_iov = &iovec;
403 msghdr.msg_iovlen = 1;
404 msghdr.msg_control = &control;
405 msghdr.msg_controllen = sizeof(control);
406
407 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
408
409 if (errno == EAGAIN ||
410 errno == EINTR)
411 continue;
412
413 log_error("recvmsg() failed: %m");
414 r = -errno;
415 goto finish;
416 }
417
418 if (n <= 0) {
419 log_error("Message too short");
420 continue;
421 }
422
423 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
424 control.cmsghdr.cmsg_level != SOL_SOCKET ||
425 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
426 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
427 log_warning("Received message without credentials. Ignoring.");
428 continue;
429 }
430
431 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
432 if (ucred->uid != 0) {
433 log_warning("Got request from unprivileged user. Ignoring.");
434 continue;
435 }
436
437 if (passphrase[0] == '+') {
438 passphrase[n] = 0;
439
440 if (!(*_passphrase = strdup(passphrase+1))) {
441 r = -ENOMEM;
442 goto finish;
443 }
444
445 } else if (passphrase[0] == '-') {
446 r = -ECANCELED;
447 goto finish;
448 } else {
449 log_error("Invalid packet");
450 continue;
451 }
452
453 break;
454 }
455
456 r = 0;
457
458finish:
459 if (fd >= 0)
460 close_nointr_nofail(fd);
461
462 if (socket_name) {
463 unlink(socket_name);
464 free(socket_name);
465 }
466
467 if (socket_fd >= 0)
468 close_nointr_nofail(socket_fd);
469
470 if (signal_fd >= 0)
471 close_nointr_nofail(signal_fd);
472
473 if (f)
474 fclose(f);
475
476 unlink(temp);
477
478 if (final[0])
479 unlink(final);
480
481 return r;
482}
260ab287
LP
483
484int ask_password_auto(const char *message, const char *icon, usec_t until, char **_passphrase) {
485 assert(message);
486 assert(_passphrase);
487
488 if (isatty(STDIN_FILENO))
489 return ask_password_tty(message, until, NULL, _passphrase);
490 else
491 return ask_password_agent(message, icon, until, _passphrase);
492}