2015-02-16 12:36:25 +01:00
|
|
|
---
|
2015-03-09 09:20:20 +01:00
|
|
|
src/tty-ask-password-agent/tty-ask-password-agent.c | 190 +++++++++++++++++++-
|
|
|
|
1 file changed, 185 insertions(+), 5 deletions(-)
|
2015-02-16 12:36:25 +01:00
|
|
|
|
2015-03-09 09:20:20 +01:00
|
|
|
--- systemd-210/src/tty-ask-password-agent/tty-ask-password-agent.c
|
|
|
|
+++ systemd-210/src/tty-ask-password-agent/tty-ask-password-agent.c 2014-07-30 10:48:43.602052750 +0000
|
|
|
|
@@ -28,8 +28,12 @@
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <unistd.h>
|
2015-02-16 12:36:25 +01:00
|
|
|
+#include <sys/prctl.h>
|
2015-03-09 09:20:20 +01:00
|
|
|
#include <getopt.h>
|
2015-02-16 12:36:25 +01:00
|
|
|
+#include <signal.h>
|
|
|
|
+#include <sys/wait.h>
|
2015-03-09 09:20:20 +01:00
|
|
|
#include <sys/signalfd.h>
|
2015-02-16 12:36:25 +01:00
|
|
|
+#include <sys/mman.h>
|
2015-03-09 09:20:20 +01:00
|
|
|
#include <fcntl.h>
|
2015-02-16 12:36:25 +01:00
|
|
|
|
|
|
|
#include "util.h"
|
2015-03-09 09:20:20 +01:00
|
|
|
@@ -41,6 +45,9 @@
|
|
|
|
#include "ask-password-api.h"
|
2015-02-16 12:36:25 +01:00
|
|
|
#include "strv.h"
|
|
|
|
#include "build.h"
|
|
|
|
+#include "fileio.h"
|
|
|
|
+#include "macro.h"
|
|
|
|
+#include "list.h"
|
|
|
|
|
|
|
|
static enum {
|
|
|
|
ACTION_LIST,
|
2015-03-09 09:20:20 +01:00
|
|
|
@@ -49,6 +56,21 @@ static enum {
|
2015-02-16 12:36:25 +01:00
|
|
|
ACTION_WALL
|
|
|
|
} arg_action = ACTION_QUERY;
|
|
|
|
|
|
|
|
+struct console {
|
|
|
|
+ LIST_FIELDS(struct console, handle);
|
2015-03-09 09:20:20 +01:00
|
|
|
+ char *tty;
|
2015-02-16 12:36:25 +01:00
|
|
|
+ pid_t pid;
|
|
|
|
+ int id;
|
|
|
|
+};
|
|
|
|
+
|
2015-03-09 09:20:20 +01:00
|
|
|
+static volatile uint32_t *usemask;
|
2015-02-16 12:36:25 +01:00
|
|
|
+static volatile sig_atomic_t sigchild;
|
|
|
|
+static void chld_handler(int sig)
|
|
|
|
+{
|
|
|
|
+ (void)sig;
|
2015-03-09 09:20:20 +01:00
|
|
|
+ sigchild++;
|
2015-02-16 12:36:25 +01:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
static bool arg_plymouth = false;
|
|
|
|
static bool arg_console = false;
|
|
|
|
|
2015-03-09 09:20:20 +01:00
|
|
|
@@ -246,12 +268,77 @@ finish:
|
|
|
|
return r;
|
2015-02-16 12:36:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
+static const char *current_dev = "/dev/console";
|
|
|
|
+static LIST_HEAD(struct console, consoles);
|
|
|
|
+static int collect_consoles(void) {
|
|
|
|
+ _cleanup_free_ char *active = NULL;
|
2015-03-09 09:20:20 +01:00
|
|
|
+ char *w, *state;
|
|
|
|
+ struct console *c;
|
|
|
|
+ size_t l;
|
|
|
|
+ int id;
|
|
|
|
+ int r;
|
|
|
|
+
|
|
|
|
+ r = read_one_line_file("/sys/class/tty/console/active", &active);
|
|
|
|
+ if (r < 0)
|
|
|
|
+ return r;
|
|
|
|
+
|
|
|
|
+ id = 0;
|
|
|
|
+ FOREACH_WORD(w, l, active, state) {
|
2015-02-16 12:36:25 +01:00
|
|
|
+ _cleanup_free_ char *tty = NULL;
|
|
|
|
+
|
2015-03-09 09:20:20 +01:00
|
|
|
+ if (strneq(w, "tty0", l)) {
|
|
|
|
+ if (read_one_line_file("/sys/class/tty/tty0/active", &tty) >= 0) {
|
|
|
|
+ w = tty;
|
|
|
|
+ l = strlen(tty);
|
|
|
|
+ }
|
2015-02-16 12:36:25 +01:00
|
|
|
+ }
|
2015-03-09 09:20:20 +01:00
|
|
|
+
|
|
|
|
+ c = malloc0(sizeof(struct console)+5+l+1);
|
|
|
|
+ if (!c) {
|
2015-02-16 12:36:25 +01:00
|
|
|
+ log_oom();
|
|
|
|
+ continue;
|
|
|
|
+ }
|
2015-03-09 09:20:20 +01:00
|
|
|
+
|
|
|
|
+ c->tty = ((char*)c)+sizeof(struct console);
|
|
|
|
+ stpncpy(stpcpy(c->tty, "/dev/"),w,l);
|
|
|
|
+ c->id = id++;
|
|
|
|
+
|
|
|
|
+ LIST_PREPEND(handle, consoles, c);
|
2015-02-16 12:36:25 +01:00
|
|
|
+ }
|
2015-03-09 09:20:20 +01:00
|
|
|
+
|
|
|
|
+ if (!consoles) {
|
|
|
|
+
|
|
|
|
+ c = malloc0(sizeof(struct console));
|
|
|
|
+ if (!c) {
|
2015-02-16 12:36:25 +01:00
|
|
|
+ log_oom();
|
|
|
|
+ return -ENOMEM;
|
|
|
|
+ }
|
2015-03-09 09:20:20 +01:00
|
|
|
+
|
|
|
|
+ c->tty = (char *)current_dev;
|
|
|
|
+ c->id = id++;
|
|
|
|
+
|
|
|
|
+ LIST_PREPEND(handle, consoles, c);
|
2015-02-16 12:36:25 +01:00
|
|
|
+ }
|
2015-03-09 09:20:20 +01:00
|
|
|
+
|
2015-02-16 12:36:25 +01:00
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void free_consoles(void) {
|
|
|
|
+ struct console *c;
|
|
|
|
+ LIST_FOREACH(handle, c, consoles) {
|
|
|
|
+ LIST_REMOVE(handle, consoles, c);
|
|
|
|
+ free(c);
|
|
|
|
+ }
|
|
|
|
+ LIST_HEAD_INIT(consoles);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
static int parse_password(const char *filename, char **wall) {
|
2015-03-09 09:20:20 +01:00
|
|
|
char *socket_name = NULL, *message = NULL, *packet = NULL;
|
2015-02-16 12:36:25 +01:00
|
|
|
uint64_t not_after = 0;
|
2015-03-09 09:20:20 +01:00
|
|
|
unsigned pid = 0;
|
|
|
|
int socket_fd = -1;
|
|
|
|
bool accept_cached = false;
|
|
|
|
+ size_t packet_length = 0;
|
2015-02-16 12:36:25 +01:00
|
|
|
|
2015-03-09 09:20:20 +01:00
|
|
|
const ConfigTableItem items[] = {
|
|
|
|
{ "Ask", "Socket", config_parse_string, 0, &socket_name },
|
|
|
|
@@ -323,7 +410,6 @@ static int parse_password(const char *fi
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_un un;
|
|
|
|
} sa = {};
|
|
|
|
- size_t packet_length = 0;
|
|
|
|
|
|
|
|
assert(arg_action == ACTION_QUERY ||
|
|
|
|
arg_action == ACTION_WATCH);
|
|
|
|
@@ -365,7 +451,7 @@ static int parse_password(const char *fi
|
|
|
|
char *password = NULL;
|
|
|
|
|
|
|
|
if (arg_console)
|
|
|
|
- if ((tty_fd = acquire_terminal("/dev/console", false, false, false, (usec_t) -1)) < 0) {
|
|
|
|
+ if ((tty_fd = acquire_terminal(current_dev, false, false, true, (usec_t) -1)) < 0) {
|
|
|
|
r = tty_fd;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
@@ -386,6 +472,7 @@ static int parse_password(const char *fi
|
|
|
|
strcpy(packet+1, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
+ memset(password, 0, strlen(password));
|
|
|
|
free(password);
|
2015-02-16 12:36:25 +01:00
|
|
|
}
|
2015-03-09 09:20:20 +01:00
|
|
|
}
|
|
|
|
@@ -423,6 +510,7 @@ finish:
|
|
|
|
if (socket_fd >= 0)
|
|
|
|
close_nointr_nofail(socket_fd);
|
|
|
|
|
|
|
|
+ memset(packet, 0, packet_length);
|
|
|
|
free(packet);
|
|
|
|
free(socket_name);
|
|
|
|
free(message);
|
|
|
|
@@ -726,8 +814,10 @@ static int parse_argv(int argc, char *ar
|
2015-02-16 12:36:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2015-03-09 09:20:20 +01:00
|
|
|
+ int id = 0;
|
|
|
|
int r;
|
2015-02-16 12:36:25 +01:00
|
|
|
|
|
|
|
+ LIST_HEAD_INIT(consoles);
|
|
|
|
log_set_target(LOG_TARGET_AUTO);
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
2015-03-09 09:20:20 +01:00
|
|
|
@@ -737,11 +827,99 @@ int main(int argc, char *argv[]) {
|
|
|
|
if ((r = parse_argv(argc, argv)) <= 0)
|
2015-02-16 12:36:25 +01:00
|
|
|
goto finish;
|
|
|
|
|
2015-03-09 09:20:20 +01:00
|
|
|
+ usemask = (uint32_t*) mmap(NULL, sizeof(uint32_t), PROT_READ|PROT_WRITE,
|
|
|
|
+ MAP_ANONYMOUS|MAP_SHARED, -1, 0);
|
2015-02-16 12:36:25 +01:00
|
|
|
+
|
|
|
|
if (arg_console) {
|
|
|
|
- setsid();
|
|
|
|
- release_terminal();
|
2015-03-09 09:20:20 +01:00
|
|
|
- }
|
2015-02-16 12:36:25 +01:00
|
|
|
+ if (!arg_plymouth && arg_action != ACTION_WALL &&
|
|
|
|
+ arg_action != ACTION_LIST) {
|
2015-03-09 09:20:20 +01:00
|
|
|
+ struct console *c;
|
|
|
|
+ struct sigaction sig = {
|
|
|
|
+ .sa_handler = chld_handler,
|
|
|
|
+ .sa_flags = SA_NOCLDSTOP|SA_RESTART,
|
|
|
|
+ };
|
|
|
|
+ struct sigaction oldsig;
|
|
|
|
+ sigset_t set, oldset;
|
|
|
|
+ int status = 0;
|
|
|
|
+ pid_t job;
|
|
|
|
+
|
|
|
|
+ collect_consoles();
|
|
|
|
+
|
|
|
|
+ if (!consoles->handle_next) {
|
|
|
|
+ consoles->pid = 0;
|
|
|
|
+ c = consoles;
|
|
|
|
+ goto nofork;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ assert_se(sigemptyset(&set) == 0);
|
|
|
|
+ assert_se(sigaddset(&set, SIGHUP) == 0);
|
|
|
|
+ assert_se(sigaddset(&set, SIGCHLD) == 0);
|
|
|
|
+ assert_se(sigemptyset(&sig.sa_mask) == 0);
|
|
|
|
+
|
|
|
|
+ assert_se(sigprocmask(SIG_UNBLOCK, &set, &oldset) == 0);
|
|
|
|
+ assert_se(sigaction(SIGCHLD, &sig, &oldsig) == 0);
|
|
|
|
+ sig.sa_handler = SIG_DFL;
|
|
|
|
+ assert_se(sigaction(SIGHUP, &sig, NULL) == 0);
|
|
|
|
+ LIST_FOREACH(handle, c, consoles) {
|
|
|
|
+
|
|
|
|
+ switch ((c->pid = fork())) {
|
|
|
|
+ case 0:
|
|
|
|
+ if (prctl(PR_SET_PDEATHSIG, SIGHUP) < 0)
|
|
|
|
+ _exit(EXIT_FAILURE);
|
|
|
|
+ zero(sig);
|
|
|
|
+ assert_se(sigprocmask(SIG_UNBLOCK, &oldset, NULL) == 0);
|
|
|
|
+ assert_se(sigaction(SIGCHLD, &oldsig, NULL) == 0);
|
|
|
|
+ /* fall through */
|
|
|
|
+ nofork:
|
|
|
|
+ setsid();
|
|
|
|
+ release_terminal();
|
|
|
|
+ id = c->id;
|
|
|
|
+ *usemask |= (1<<id);
|
|
|
|
+ current_dev = c->tty;
|
|
|
|
+ goto forked; /* child */
|
|
|
|
+ case -1:
|
|
|
|
+ log_error("Failed to query password: %s", strerror(errno));
|
|
|
|
+ return EXIT_FAILURE;
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ r = 0;
|
|
|
|
+ while ((job = wait(&status))) {
|
|
|
|
+ if (job < 0) {
|
|
|
|
+ if (errno != EINTR)
|
|
|
|
+ break;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ LIST_FOREACH(handle, c, consoles) {
|
|
|
|
+ if (c->pid == job) {
|
|
|
|
+ *usemask &= ~(1<<c->id);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (kill(c->pid, 0) < 0) {
|
|
|
|
+ *usemask &= ~(1<<c->id);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (*usemask & (1<<c->id))
|
|
|
|
+ continue;
|
|
|
|
+ kill(c->pid, SIGHUP);
|
|
|
|
+ usleep(50000);
|
|
|
|
+ kill(c->pid, SIGKILL);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (WIFEXITED(status) && !r)
|
|
|
|
+ r = WEXITSTATUS(status);
|
|
|
|
+ }
|
|
|
|
+ free_consoles();
|
|
|
|
+ return r != 0 ? EXIT_FAILURE : EXIT_SUCCESS; /* parent */
|
|
|
|
+
|
2015-02-16 12:36:25 +01:00
|
|
|
+ } else {
|
|
|
|
+ setsid();
|
|
|
|
+ release_terminal();
|
|
|
|
+ }
|
2015-03-09 09:20:20 +01:00
|
|
|
+ }
|
|
|
|
+forked:
|
|
|
|
if (arg_action == ACTION_WATCH ||
|
|
|
|
arg_action == ACTION_WALL)
|
2015-02-16 12:36:25 +01:00
|
|
|
r = watch_passwords();
|
2015-03-09 09:20:20 +01:00
|
|
|
@@ -751,6 +929,8 @@ int main(int argc, char *argv[]) {
|
2015-02-16 12:36:25 +01:00
|
|
|
if (r < 0)
|
2015-03-09 09:20:20 +01:00
|
|
|
log_error("Error: %s", strerror(-r));
|
2015-02-16 12:36:25 +01:00
|
|
|
|
|
|
|
+ free_consoles();
|
2015-03-09 09:20:20 +01:00
|
|
|
+ *usemask &= ~(1<<id);
|
2015-02-16 12:36:25 +01:00
|
|
|
finish:
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|