ask-password-api.c revision 981e4cd325410384cdadd837f34c002699d2d750
0797faae937515a5225a36db4a1ec79480d2555cjorton/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
0797faae937515a5225a36db4a1ec79480d2555cjorton This file is part of systemd.
0797faae937515a5225a36db4a1ec79480d2555cjorton Copyright 2010 Lennart Poettering
0797faae937515a5225a36db4a1ec79480d2555cjorton systemd is free software; you can redistribute it and/or modify it
0797faae937515a5225a36db4a1ec79480d2555cjorton under the terms of the GNU Lesser General Public License as published by
0797faae937515a5225a36db4a1ec79480d2555cjorton the Free Software Foundation; either version 2.1 of the License, or
0797faae937515a5225a36db4a1ec79480d2555cjorton (at your option) any later version.
0797faae937515a5225a36db4a1ec79480d2555cjorton systemd is distributed in the hope that it will be useful, but
0797faae937515a5225a36db4a1ec79480d2555cjorton WITHOUT ANY WARRANTY; without even the implied warranty of
0797faae937515a5225a36db4a1ec79480d2555cjorton MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0797faae937515a5225a36db4a1ec79480d2555cjorton Lesser General Public License for more details.
0797faae937515a5225a36db4a1ec79480d2555cjorton You should have received a copy of the GNU Lesser General Public License
0797faae937515a5225a36db4a1ec79480d2555cjorton along with systemd; If not, see <http://www.gnu.org/licenses/>.
0797faae937515a5225a36db4a1ec79480d2555cjorton while (p > 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *message,
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *flag_file,
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton bool reset_tty = false;
0797faae937515a5225a36db4a1ec79480d2555cjorton bool silent_mode = false;
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton bool dirty = false;
0797faae937515a5225a36db4a1ec79480d2555cjorton if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (ttyfd >= 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton loop_write(ttyfd, ANSI_HIGHLIGHT_ON, sizeof(ANSI_HIGHLIGHT_ON)-1, false);
0797faae937515a5225a36db4a1ec79480d2555cjorton loop_write(ttyfd, ANSI_HIGHLIGHT_OFF, sizeof(ANSI_HIGHLIGHT_OFF)-1, false);
8c4aed3f92674325e17d8360ee2797beda3a1472jorton pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
0797faae937515a5225a36db4a1ec79480d2555cjorton if (k < 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton } else if (k == 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (n < 0) {
4b0e00b3346b3e8fd53219d060f4cf6676847a06jim } else if (n == 0)
eab15974b1d8bbcb6d4f9ec75527b39ffded82aajorton if (c == '\n')
0797faae937515a5225a36db4a1ec79480d2555cjorton if (p > 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /* There are two ways to enter silent
0797faae937515a5225a36db4a1ec79480d2555cjorton * mode. Either by pressing backspace
0797faae937515a5225a36db4a1ec79480d2555cjorton * as first key (and only as first key),
0797faae937515a5225a36db4a1ec79480d2555cjorton * or ... */
0797faae937515a5225a36db4a1ec79480d2555cjorton } else if (ttyfd >= 0)
0797faae937515a5225a36db4a1ec79480d2555cjorton /* ... or by pressing TAB at any time. */
072f7e449a76d28b580de6e89a1723713ab9adb1jorton fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
072f7e449a76d28b580de6e89a1723713ab9adb1jorton if (fd < 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%" PRIx64, random_u64());
a10d8ce69c26142323c66adaba109be1b4baa379wrowe r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
0797faae937515a5225a36db4a1ec79480d2555cjorton if (r < 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *message,
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *icon,
0797faae937515a5225a36db4a1ec79480d2555cjorton char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
a10d8ce69c26142323c66adaba109be1b4baa379wrowe _cleanup_close_ int socket_fd = -1, signal_fd = -1, fd = -1;
501e60e433e1914c64f642114fbb4fb9be9e2ca9jorton assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (fd < 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
a10d8ce69c26142323c66adaba109be1b4baa379wrowe "PID=%lu\n"
0797faae937515a5225a36db4a1ec79480d2555cjorton "Socket=%s\n"
0797faae937515a5225a36db4a1ec79480d2555cjorton "AcceptCached=%i\n"
0797faae937515a5225a36db4a1ec79480d2555cjorton "NotAfter=%llu\n",
a10d8ce69c26142323c66adaba109be1b4baa379wrowe (unsigned long) getpid(),
0797faae937515a5225a36db4a1ec79480d2555cjorton (unsigned long long) until);
a10d8ce69c26142323c66adaba109be1b4baa379wrowe k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (k < 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (k <= 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (n < 0) {
a10d8ce69c26142323c66adaba109be1b4baa379wrowe if (n <= 0) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) ||
0797faae937515a5225a36db4a1ec79480d2555cjorton control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) {
0797faae937515a5225a36db4a1ec79480d2555cjorton log_warning("Received message without credentials. Ignoring.");
0797faae937515a5225a36db4a1ec79480d2555cjorton log_warning("Got request from unprivileged user. Ignoring.");
0797faae937515a5225a36db4a1ec79480d2555cjorton if (n == 1)
a10d8ce69c26142323c66adaba109be1b4baa379wrowe /* An empty message refers to the empty password */
250fbbb51225da0dfc973743b795b04dc9740027wrowe if (strv_length(l) <= 0) {
250fbbb51225da0dfc973743b795b04dc9740027wrowe assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
250fbbb51225da0dfc973743b795b04dc9740027wroweint ask_password_auto(const char *message, const char *icon, usec_t until, bool accept_cached, char ***_passphrases) {