]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/ask-password-api.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include <sys/inotify.h>
28 #include <sys/socket.h>
32 #include <sys/signalfd.h>
36 #include "ask-password-api.h"
41 const char *flag_file
,
44 struct termios old_termios
, new_termios
;
45 char passphrase
[LINE_MAX
];
47 int r
, ttyfd
= -1, notify
= -1;
48 struct pollfd pollfd
[2];
49 bool reset_tty
= false;
59 if ((notify
= inotify_init1(IN_CLOEXEC
|IN_NONBLOCK
)) < 0) {
64 if (inotify_add_watch(notify
, flag_file
, IN_ATTRIB
/* for the link count */) < 0) {
70 if ((ttyfd
= open("/dev/tty", O_RDWR
|O_NOCTTY
|O_CLOEXEC
)) >= 0) {
72 if (tcgetattr(ttyfd
, &old_termios
) < 0) {
77 loop_write(ttyfd
, "\x1B[1m", 4, false);
78 loop_write(ttyfd
, message
, strlen(message
), false);
79 loop_write(ttyfd
, ": ", 2, false);
80 loop_write(ttyfd
, "\x1B[0m", 4, false);
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;
87 if (tcsetattr(ttyfd
, TCSADRAIN
, &new_termios
) < 0) {
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
;
104 int sleep_for
= -1, k
;
110 y
= now(CLOCK_MONOTONIC
);
117 sleep_for
= (int) ((until
- y
) / USEC_PER_MSEC
);
121 if (access(flag_file
, F_OK
) < 0) {
126 if ((k
= poll(pollfd
, notify
> 0 ? 2 : 1, sleep_for
)) < 0) {
138 if (notify
> 0 && pollfd
[POLL_INOTIFY
].revents
!= 0)
141 if (pollfd
[POLL_TTY
].revents
== 0)
144 if ((n
= read(ttyfd
>= 0 ? ttyfd
: STDIN_FILENO
, &c
, 1)) < 0) {
146 if (errno
== EINTR
|| errno
== EAGAIN
)
163 loop_write(ttyfd
, "\b \b", 3, false);
166 } else if (c
== '\b' || c
== 127) {
171 loop_write(ttyfd
, "\b \b", 3, false);
177 loop_write(ttyfd
, "*", 1, false);
182 loop_write(ttyfd
, "\n", 1, false);
186 if (!(*_passphrase
= strdup(passphrase
))) {
195 close_nointr_nofail(notify
);
199 tcsetattr(ttyfd
, TCSADRAIN
, &old_termios
);
201 close_nointr_nofail(ttyfd
);
207 static int create_socket(char **name
) {
211 struct sockaddr_un un
;
218 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0)) < 0) {
219 log_error("socket() failed: %m");
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());
227 if (bind(fd
, &sa
.sa
, offsetof(struct sockaddr_un
, sun_path
) + strlen(sa
.un
.sun_path
)) < 0) {
229 log_error("bind() failed: %m");
233 if (setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0) {
235 log_error("SO_PASSCRED failed: %m");
239 if (!(c
= strdup(sa
.un
.sun_path
))) {
241 log_error("Out of memory");
249 close_nointr_nofail(fd
);
255 int ask_password_agent(
259 char **_passphrase
) {
267 char temp
[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
268 char final
[sizeof(temp
)] = "";
271 char *socket_name
= NULL
;
272 int socket_fd
= -1, signal_fd
= -1;
274 struct pollfd pollfd
[_FD_MAX
];
276 mkdir_p("/dev/.systemd/ask-password", 0755);
278 if ((fd
= mkostemp(temp
, O_CLOEXEC
|O_CREAT
|O_WRONLY
)) < 0) {
279 log_error("Failed to create password file: %m");
286 if (!(f
= fdopen(fd
, "w"))) {
287 log_error("Failed to allocate FILE: %m");
294 assert_se(sigemptyset(&mask
) == 0);
295 sigset_add_many(&mask
, SIGINT
, SIGTERM
, -1);
296 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
298 if ((signal_fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
)) < 0) {
299 log_error("signalfd(): %m");
304 if ((socket_fd
= create_socket(&socket_name
)) < 0) {
314 (unsigned long) getpid(),
316 (unsigned long long) until
);
319 fprintf(f
, "Message=%s\n", message
);
322 fprintf(f
, "Icon=%s\n", icon
);
327 log_error("Failed to write query file: %m");
332 memcpy(final
, temp
, sizeof(temp
));
334 final
[sizeof(final
)-11] = 'a';
335 final
[sizeof(final
)-10] = 's';
336 final
[sizeof(final
)-9] = 'k';
338 if (rename(temp
, final
) < 0) {
339 log_error("Failed to rename query file: %m");
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
;
351 char passphrase
[LINE_MAX
+1];
352 struct msghdr msghdr
;
356 struct cmsghdr cmsghdr
;
357 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
))];
363 t
= now(CLOCK_MONOTONIC
);
366 log_notice("Timed out");
371 if ((k
= poll(pollfd
, _FD_MAX
, until
-t
/USEC_PER_MSEC
)) < 0) {
376 log_error("poll() failed: %m");
382 log_notice("Timed out");
387 if (pollfd
[FD_SIGNAL
].revents
& POLLIN
)
390 if (pollfd
[FD_SOCKET
].revents
!= POLLIN
) {
391 log_error("Unexpected poll() event.");
397 iovec
.iov_base
= passphrase
;
398 iovec
.iov_len
= sizeof(passphrase
)-1;
402 msghdr
.msg_iov
= &iovec
;
403 msghdr
.msg_iovlen
= 1;
404 msghdr
.msg_control
= &control
;
405 msghdr
.msg_controllen
= sizeof(control
);
407 if ((n
= recvmsg(socket_fd
, &msghdr
, 0)) < 0) {
409 if (errno
== EAGAIN
||
413 log_error("recvmsg() failed: %m");
419 log_error("Message too short");
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.");
431 ucred
= (struct ucred
*) CMSG_DATA(&control
.cmsghdr
);
432 if (ucred
->uid
!= 0) {
433 log_warning("Got request from unprivileged user. Ignoring.");
437 if (passphrase
[0] == '+') {
440 if (!(*_passphrase
= strdup(passphrase
+1))) {
445 } else if (passphrase
[0] == '-') {
449 log_error("Invalid packet");
460 close_nointr_nofail(fd
);
468 close_nointr_nofail(socket_fd
);
471 close_nointr_nofail(signal_fd
);