ask-password-api.c revision 4a62c710b62a5a3c7a8a278b810b9d5b5a0c8f4f
/*-*- 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,
bool echo,
const char *flag_file,
char **_passphrase) {
char passphrase[LINE_MAX], *x;
size_t p = 0;
int r;
bool reset_tty = false;
bool silent_mode = false;
bool dirty = false;
enum {
};
if (flag_file) {
if (notify < 0) {
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
}
if (ttyfd >= 0) {
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;
}
if (k < 0) {
continue;
r = -errno;
goto finish;
} else if (k == 0) {
r = -ETIME;
goto finish;
}
continue;
if (n < 0) {
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 {
if (p >= sizeof(passphrase)-1) {
continue;
}
passphrase[p++] = c;
if (!silent_mode && ttyfd >= 0)
dirty = true;
}
}
x = strndup(passphrase, p);
if (!x) {
r = -ENOMEM;
goto finish;
}
*_passphrase = x;
r = 0;
}
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)
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;
goto fail;
}
r = -errno;
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,
const char *id,
bool echo,
bool accept_cached,
char ***_passphrases) {
enum {
};
char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
int r;
if (fd < 0) {
r = -errno;
goto finish;
}
if (!f) {
r = -errno;
goto finish;
}
fd = -1;
if (signal_fd < 0) {
r = -errno;
goto finish;
}
if (socket_fd < 0) {
r = socket_fd;
goto finish;
}
fprintf(f,
"[Ask]\n"
"Socket=%s\n"
"AcceptCached=%i\n"
"Echo=%i\n"
getpid(),
accept_cached ? 1 : 0,
echo ? 1 : 0,
until);
if (message)
if (icon)
if (id)
fflush(f);
if (ferror(f)) {
r = -errno;
goto finish;
}
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;
}
if (k < 0) {
continue;
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;
}
if (n < 0) {
continue;
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;
if (socket_name)
if (final[0])
return r;
}
if (isatty(STDIN_FILENO)) {
int r;
if (r < 0)
return r;
r = strv_consume(&l, s);
if (r < 0)
return r;
*_passphrases = l;
return r;
} else
}