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/>.
22 #include <sys/socket.h>
24 #include <sys/types.h>
32 #include <sys/signalfd.h>
42 static const char *arg_icon = NULL;
43 static const char *arg_message = NULL;
44 static bool arg_use_tty = true;
45 static usec_t arg_timeout = 60 * USEC_PER_SEC;
47 static int create_socket(char **name) {
51 struct sockaddr_un un;
58 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
59 log_error("socket() failed: %m");
64 sa.un.sun_family = AF_UNIX;
65 snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/dev/.systemd/ask-password/sck.%llu", random_ull());
67 if (bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
69 log_error("bind() failed: %m");
73 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
75 log_error("SO_PASSCRED failed: %m");
79 if (!(c = strdup(sa.un.sun_path))) {
81 log_error("Out of memory");
89 close_nointr_nofail(fd);
94 static int help(void) {
96 printf("%s [OPTIONS...] MESSAGE\n\n"
97 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
98 " -h --help Show this help\n"
99 " --icon=NAME Icon name\n"
100 " --timeout=SEC Timeout in sec\n"
101 " --no-tty Ask question via agent even on TTY\n",
102 program_invocation_short_name);
107 static int parse_argv(int argc, char *argv[]) {
115 static const struct option options[] = {
116 { "help", no_argument, NULL, 'h' },
117 { "icon", required_argument, NULL, ARG_ICON },
118 { "timeout", required_argument, NULL, ARG_TIMEOUT },
119 { "no-tty", no_argument, NULL, ARG_NO_TTY },
128 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
141 if (parse_usec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
142 log_error("Failed to parse --timeout parameter %s", optarg);
155 log_error("Unknown option code %c", c);
160 if (optind != argc-1) {
165 arg_message = argv[optind];
169 static int ask_agent(void) {
176 char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
177 char final[sizeof(temp)] = "";
180 char *socket_name = NULL;
181 int socket_fd = -1, signal_fd = -1;
184 struct pollfd pollfd[_FD_MAX];
186 mkdir_p("/dev/.systemd/ask-password", 0755);
188 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
189 log_error("Failed to create password file: %m");
196 if (!(f = fdopen(fd, "w"))) {
197 log_error("Failed to allocate FILE: %m");
204 assert_se(sigemptyset(&mask) == 0);
205 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
206 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
208 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
209 log_error("signalfd(): %m");
214 if ((socket_fd = create_socket(&socket_name)) < 0) {
219 not_after = now(CLOCK_MONOTONIC) + arg_timeout;
226 (unsigned long) getpid(),
228 (unsigned long long) not_after);
231 fprintf(f, "Message=%s\n", arg_message);
234 fprintf(f, "Icon=%s\n", arg_icon);
239 log_error("Failed to write query file: %m");
244 memcpy(final, temp, sizeof(temp));
246 final[sizeof(final)-11] = 'a';
247 final[sizeof(final)-10] = 's';
248 final[sizeof(final)-9] = 'k';
250 if (rename(temp, final) < 0) {
251 log_error("Failed to rename query file: %m");
257 pollfd[FD_SOCKET].fd = socket_fd;
258 pollfd[FD_SOCKET].events = POLLIN;
259 pollfd[FD_SIGNAL].fd = signal_fd;
260 pollfd[FD_SIGNAL].events = POLLIN;
263 char passphrase[LINE_MAX+1];
264 struct msghdr msghdr;
268 struct cmsghdr cmsghdr;
269 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
274 if ((k = poll(pollfd, _FD_MAX, arg_timeout/USEC_PER_MSEC)) < 0) {
279 log_error("poll() failed: %m");
285 log_notice("Timed out");
290 if (pollfd[FD_SIGNAL].revents & POLLIN)
293 if (pollfd[FD_SOCKET].revents != POLLIN) {
294 log_error("Unexpected poll() event.");
300 iovec.iov_base = passphrase;
301 iovec.iov_len = sizeof(passphrase)-1;
305 msghdr.msg_iov = &iovec;
306 msghdr.msg_iovlen = 1;
307 msghdr.msg_control = &control;
308 msghdr.msg_controllen = sizeof(control);
310 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
312 if (errno == EAGAIN ||
316 log_error("recvmsg() failed: %m");
322 log_error("Message too short");
326 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
327 control.cmsghdr.cmsg_level != SOL_SOCKET ||
328 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
329 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
330 log_warning("Received message without credentials. Ignoring.");
334 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
335 if (ucred->uid != 0) {
336 log_warning("Got request from unprivileged user. Ignoring.");
340 if (passphrase[0] == '+') {
342 fputs(passphrase+1, stdout);
344 } else if (passphrase[0] == '-') {
348 log_error("Invalid packet");
359 close_nointr_nofail(fd);
367 close_nointr_nofail(socket_fd);
370 close_nointr_nofail(signal_fd);
383 int main(int argc, char *argv[]) {
386 log_parse_environment();
389 if ((r = parse_argv(argc, argv)) <= 0)
392 if (arg_use_tty && isatty(STDIN_FILENO)) {
393 char *password = NULL;
395 if ((r = ask_password_tty(arg_message, now(CLOCK_MONOTONIC) + arg_timeout, NULL, &password)) >= 0) {
396 fputs(password, stdout);
405 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;