ask-password-api.c revision 21bc923aa35d455cdef1607eb7022608c705c9f3
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#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 "strv.h"
#include "ask-password-api.h"
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;
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 = -ETIMEDOUT;
goto finish;
}
}
if (flag_file)
r = -errno;
goto finish;
}
continue;
r = -errno;
goto finish;
} else if (k == 0) {
r = -ETIMEDOUT;
goto finish;
}
continue;
continue;
r = -errno;
goto finish;
} else if (n == 0)
break;
if (c == '\n')
break;
else if (c == 21) {
while (p > 0) {
p--;
if (ttyfd >= 0)
}
} else if (c == '\b' || c == 127) {
if (p > 0) {
p--;
if (ttyfd >= 0)
}
} else {
passphrase[p++] = c;
if (ttyfd >= 0)
}
}
if (ttyfd >= 0)
passphrase[p] = 0;
r = -ENOMEM;
goto finish;
}
r = 0;
if (notify >= 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, r;
char *c;
log_error("socket() failed: %m");
return -errno;
}
snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/dev/.systemd/ask-password/sck.%llu", random_ull());
r = -errno;
log_error("bind() failed: %m");
goto fail;
}
r = -errno;
log_error("SO_PASSCRED failed: %m");
goto fail;
}
r = -ENOMEM;
log_error("Out of memory");
goto fail;
}
*name = c;
return fd;
fail:
return r;
}
int ask_password_agent(
const char *message,
const char *icon,
bool accept_cached,
char ***_passphrases) {
enum {
};
char temp[] = "/dev/.systemd/ask-password/tmp.XXXXXX";
int fd = -1, r;
char *socket_name = NULL;
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);
if (until <= t) {
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;
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 (fd >= 0)
if (socket_name) {
}
if (socket_fd >= 0)
if (signal_fd >= 0)
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
}