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>
39 static const char *arg_icon = NULL;
40 static const char *arg_message = NULL;
41 static usec_t arg_timeout = 60 * USEC_PER_SEC;
43 static int create_socket(char **name) {
47 struct sockaddr_un un;
54 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
55 log_error("socket() failed: %m");
60 sa.un.sun_family = AF_UNIX;
61 snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, "/org/freedesktop/systemd1/ask-password/%llu", random_ull());
63 if (bind(fd, &sa.sa, sizeof(sa_family_t) + 1 + strlen(sa.un.sun_path+1)) < 0) {
65 log_error("bind() failed: %m");
69 if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
71 log_error("SO_PASSCRED failed: %m");
75 if (!(c = strdup(sa.un.sun_path+1))) {
77 log_error("Out of memory");
85 close_nointr_nofail(fd);
90 static int help(void) {
92 printf("%s [OPTIONS...] MESSAGE\n\n"
93 "Query the user for a passphrase.\n\n"
94 " -h --help Show this help\n"
95 " --icon=NAME Icon name\n"
96 " --timeout=USEC Timeout in usec\n",
97 program_invocation_short_name);
102 static int parse_argv(int argc, char *argv[]) {
109 static const struct option options[] = {
110 { "help", no_argument, NULL, 'h' },
111 { "icon", required_argument, NULL, ARG_ICON },
112 { "timeout", required_argument, NULL, ARG_TIMEOUT },
121 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
134 if (parse_usec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
135 log_error("Failed to parse --timeout parameter %s", optarg);
144 log_error("Unknown option code %c", c);
149 if (optind != argc-1) {
154 arg_message = argv[optind];
158 int main(int argc, char *argv[]) {
159 char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
160 char final[sizeof(temp)] = "";
161 int fd = -1, r = EXIT_FAILURE, k;
163 char *socket_name = NULL;
164 int socket_fd, signal_fd;
168 log_parse_environment();
171 if ((k = parse_argv(argc, argv)) < 0) {
172 r = k < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
176 if ((fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY)) < 0) {
177 log_error("Failed to create password file: %m");
183 if (!(f = fdopen(fd, "w"))) {
184 log_error("Failed to allocate FILE: %m");
190 assert_se(sigemptyset(&mask) == 0);
191 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
192 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
194 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
195 log_error("signalfd(): %m");
199 if ((socket_fd = create_socket(&socket_name)) < 0)
202 not_after = now(CLOCK_MONOTONIC) + arg_timeout;
209 (unsigned long long) not_after);
212 fprintf(f, "Message=%s\n", arg_message);
215 fprintf(f, "Icon=%s\n", arg_icon);
220 log_error("Failed to write query file: %m");
224 memcpy(final, temp, sizeof(temp));
226 final[sizeof(final)-11] = 'a';
227 final[sizeof(final)-10] = 's';
228 final[sizeof(final)-9] = 'k';
230 if (rename(temp, final) < 0) {
231 log_error("Failed to rename query file: %m");
242 char passphrase[LINE_MAX+1];
243 struct msghdr msghdr;
247 struct cmsghdr cmsghdr;
248 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
251 struct pollfd pollfd[_FD_MAX];
254 pollfd[FD_SOCKET].fd = socket_fd;
255 pollfd[FD_SOCKET].events = POLLIN;
256 pollfd[FD_SIGNAL].fd = signal_fd;
257 pollfd[FD_SIGNAL].events = POLLIN;
259 if ((k = poll(pollfd, 2, arg_timeout/USEC_PER_MSEC)) < 0) {
264 log_error("poll() failed: %s", strerror(-r));
269 log_notice("Timed out");
273 if (pollfd[FD_SIGNAL].revents & POLLIN)
276 if (pollfd[FD_SOCKET].revents != POLLIN) {
277 log_error("Unexpected poll() event.");
282 iovec.iov_base = passphrase;
283 iovec.iov_len = sizeof(passphrase)-1;
287 msghdr.msg_iov = &iovec;
288 msghdr.msg_iovlen = 1;
289 msghdr.msg_control = &control;
290 msghdr.msg_controllen = sizeof(control);
292 if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
294 if (errno == EAGAIN ||
298 log_error("recvmsg() failed: %m");
303 log_error("Message too short");
307 if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
308 control.cmsghdr.cmsg_level != SOL_SOCKET ||
309 control.cmsghdr.cmsg_type != SCM_CREDENTIALS ||
310 control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
311 log_warning("Received message without credentials. Ignoring.");
315 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
316 if (ucred->uid != 0) {
317 log_warning("Got request from unprivileged user. Ignoring.");
321 if (passphrase[0] == '+') {
323 fputs(passphrase+1, stdout);
324 } else if (passphrase[0] == '-')
327 log_error("Invalid packet");
338 close_nointr_nofail(fd);
341 close_nointr_nofail(socket_fd);