ask-password-api.c revision 03e334a1c7dc8c20c38902aa039440763acc9b17
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdbool.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stddef.h>
#include <sys/signalfd.h>
#include "util.h"
#include "mkdir.h"
#include "strv.h"
#include "ask-password-api.h"
if (ttyfd < 0)
return;
while (p > 0) {
p--;
}
}
int ask_password_tty(
const char *message,
const char *flag_file,
char **_passphrase) {
char passphrase[LINE_MAX];
size_t p = 0;
bool reset_tty = false;
bool silent_mode = false;
bool dirty = false;
enum {
};
if (flag_file) {
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
}
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
reset_tty = true;
}
for (;;) {
char c;
int sleep_for = -1, k;
ssize_t n;
if (until > 0) {
usec_t y;
y = now(CLOCK_MONOTONIC);
if (y > until) {
r = -ETIME;
goto finish;
}
}
if (flag_file)
r = -errno;
goto finish;
}
continue;
r = -errno;
goto finish;
} else if (k == 0) {
r = -ETIME;
goto finish;
}
continue;
continue;
r = -errno;
goto finish;
} else if (n == 0)
break;
if (c == '\n')
break;
else if (c == 21) { /* C-u */
if (!silent_mode)
backspace_chars(ttyfd, p);
p = 0;
} else if (c == '\b' || c == 127) {
if (p > 0) {
if (!silent_mode)
p--;
} else if (!dirty && !silent_mode) {
silent_mode = true;
/* There are two ways to enter silent
* mode. Either by pressing backspace
* as first key (and only as first key),
* or ... */
if (ttyfd >= 0)
} else if (ttyfd >= 0)
} else if (c == '\t' && !silent_mode) {
backspace_chars(ttyfd, p);
silent_mode = true;
/* ... or by pressing TAB at any time. */
if (ttyfd >= 0)
} else {
passphrase[p++] = c;
if (!silent_mode && ttyfd >= 0)
dirty = true;
}
}
passphrase[p] = 0;
r = -ENOMEM;
goto finish;
}
r = 0;
if (ttyfd >= 0) {
if (reset_tty) {
}
}
return r;
}
static int create_socket(char **name) {
int fd;
union {
struct sockaddr_un un;
} sa = {
};
int one = 1;
int r = 0;
char *c;
if (fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%" PRIx64, random_u64());
RUN_WITH_UMASK(0177) {
}
if (r < 0) {
r = -errno;
log_error("bind() failed: %m");
goto fail;
}
r = -errno;
log_error("SO_PASSCRED failed: %m");
goto fail;
}
if (!c) {
r = log_oom();
goto fail;
}
*name = c;
return fd;
fail:
safe_close(fd);
return r;
}
int ask_password_agent(
const char *message,
const char *icon,
bool accept_cached,
char ***_passphrases) {
enum {
};
char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
int fd = -1, r;
char *socket_name = NULL;
if (fd < 0) {
log_error("Failed to create password file: %m");
r = -errno;
goto finish;
}
log_error("Failed to allocate FILE: %m");
r = -errno;
goto finish;
}
fd = -1;
log_error("signalfd(): %m");
r = -errno;
goto finish;
}
r = socket_fd;
goto finish;
}
fprintf(f,
"[Ask]\n"
"PID=%lu\n"
"Socket=%s\n"
"AcceptCached=%i\n"
"NotAfter=%llu\n",
(unsigned long) getpid(),
accept_cached ? 1 : 0,
(unsigned long long) until);
if (message)
if (icon)
fflush(f);
if (ferror(f)) {
log_error("Failed to write query file: %m");
r = -errno;
goto finish;
}
log_error("Failed to rename query file: %m");
r = -errno;
goto finish;
}
for (;;) {
union {
} control;
ssize_t n;
int k;
usec_t t;
t = now(CLOCK_MONOTONIC);
log_notice("Timed out");
r = -ETIME;
goto finish;
}
continue;
log_error("poll() failed: %m");
r = -errno;
goto finish;
}
if (k <= 0) {
log_notice("Timed out");
r = -ETIME;
goto finish;
}
r = -EINTR;
goto finish;
}
log_error("Unexpected poll() event.");
r = -EIO;
goto finish;
}
continue;
log_error("recvmsg() failed: %m");
r = -errno;
goto finish;
}
if (n <= 0) {
log_error("Message too short");
continue;
}
log_warning("Received message without credentials. Ignoring.");
continue;
}
log_warning("Got request from unprivileged user. Ignoring.");
continue;
}
if (passphrase[0] == '+') {
char **l;
if (n == 1)
else
/* An empty message refers to the empty password */
if (!l) {
r = -ENOMEM;
goto finish;
}
if (strv_length(l) <= 0) {
strv_free(l);
log_error("Invalid packet");
continue;
}
*_passphrases = l;
} else if (passphrase[0] == '-') {
r = -ECANCELED;
goto finish;
} else {
log_error("Invalid packet");
continue;
}
break;
}
r = 0;
safe_close(fd);
if (socket_name) {
}
if (f)
fclose(f);
if (final[0])
return r;
}
int ask_password_auto(const char *message, const char *icon, usec_t until, bool accept_cached, char ***_passphrases) {
if (isatty(STDIN_FILENO)) {
int r;
return r;
free(s);
if (!l)
return -ENOMEM;
*_passphrases = l;
return r;
} else
}