tty-ask-password-agent.c revision 7f4e08056de0184b205a20632e62db73d299937e
/*-*- 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 <stdbool.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/signalfd.h>
#include "util.h"
#include "conf-parser.h"
#include "utmp-wtmp.h"
#include "socket-util.h"
#include "ask-password-api.h"
static enum {
} arg_action = ACTION_QUERY;
static bool arg_plymouth = false;
static int ask_password_plymouth(const char *message, usec_t until, const char *flag_file, char **_passphrase) {
union sockaddr_union sa;
ssize_t k;
int r, n;
size_t p = 0;
enum {
};
if (flag_file) {
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
}
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
r = -ENOMEM;
goto finish;
}
r = k < 0 ? (int) k : -EIO;
goto finish;
}
for (;;) {
int sleep_for = -1, j;
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 (j == 0) {
r = -ETIMEDOUT;
goto finish;
}
continue;
goto finish;
}
p += k;
if (p < 1)
continue;
if (buffer[0] == 5) {
/* No password, because UI not shown */
r = -ENOENT;
goto finish;
} else if (buffer[0] == 2) {
char *s;
/* One answer */
if (p < 5)
continue;
r = -EIO;
goto finish;
}
if (p-5 < size)
continue;
r = -ENOMEM;
goto finish;
}
*_passphrase = s;
break;
} else {
/* Unknown packet */
r = -EIO;
goto finish;
}
}
r = 0;
if (notify >= 0)
if (fd >= 0)
return r;
}
unsigned pid = 0;
int socket_fd = -1;
const ConfigItem items[] = {
};
FILE *f;
int r;
usec_t n;
return 0;
return -errno;
}
goto finish;
}
if (!socket_name || not_after <= 0) {
r = -EBADMSG;
goto finish;
}
n = now(CLOCK_MONOTONIC);
if (n > not_after) {
r = 0;
goto finish;
}
if (arg_action == ACTION_LIST)
else if (arg_action == ACTION_WALL) {
char *_wall;
"%s%sPassword entry required for \'%s\' (PID %u).\r\n"
"Please enter password with the systemd-tty-password-agent tool!",
pid) < 0) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
} else {
union {
struct sockaddr_un un;
} sa;
char *password;
arg_action == ACTION_WATCH);
if (arg_action == ACTION_QUERY)
r = 0;
goto finish;
}
if (arg_plymouth)
else
if (r < 0) {
goto finish;
}
if (!packet) {
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
log_error("socket(): %m");
r = -errno;
goto finish;
}
if (sendto(socket_fd, packet, strlen(packet), MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
log_error("Failed to send: %m");
r = -errno;
goto finish;
}
}
fclose(f);
if (socket_fd >= 0)
return r;
}
static int show_passwords(void) {
DIR *d;
int r = 0;
if (!(d = opendir("/dev/.systemd/ask-password"))) {
return 0;
log_error("opendir(): %m");
return -errno;
}
char *p;
int q;
char *wall;
continue;
continue;
continue;
log_error("Out of memory");
r = -ENOMEM;
goto finish;
}
if ((q = parse_password(p, &wall)) < 0)
r = q;
free(p);
if (wall) {
}
}
if (d)
closedir(d);
return r;
}
static int watch_passwords(void) {
enum {
};
int r;
r = -errno;
goto finish;
}
r = -errno;
goto finish;
}
log_error("signalfd(): %m");
r = -errno;
goto finish;
}
for (;;) {
if ((r = show_passwords()) < 0)
break;
continue;
r = -errno;
goto finish;
}
break;
}
r = 0;
if (notify >= 0)
if (signal_fd >= 0)
return r;
}
static int help(void) {
printf("%s [OPTIONS...]\n\n"
"Process system password requests.\n\n"
" -h --help Show this help\n"
" --list Show pending password requests\n"
" --query Process pending password requests\n"
" --watch Continously process password requests\n"
" --wall Continously forward password requests to wall\n"
" --plymouth Ask question with Plymouth instead of on TTY\n",
return 0;
}
enum {
ARG_LIST = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_LIST:
break;
case ARG_QUERY:
break;
case ARG_WATCH:
break;
case ARG_WALL:
break;
case ARG_PLYMOUTH:
arg_plymouth = true;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
help();
return -EINVAL;
}
return 1;
}
int r;
log_open();
goto finish;
if (arg_action == ACTION_WATCH ||
r = watch_passwords();
else
r = show_passwords();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}