ask-password.c revision b9ba604e8720c30deb7095b049b577eec1cbb74a
/*-*- 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 <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signalfd.h>
#include <getopt.h>
#include <termios.h>
#include <limits.h>
#include <stddef.h>
#include "log.h"
#include "macro.h"
#include "util.h"
static const char *arg_message = NULL;
static bool arg_use_tty = true;
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;
}
static int help(void) {
printf("%s [OPTIONS...] MESSAGE\n\n"
"Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
" -h --help Show this help\n"
" --icon=NAME Icon name\n"
" --timeout=SEC Timeout in sec\n"
" --no-tty Ask question via agent even on TTY\n",
return 0;
}
enum {
ARG_ICON = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_ICON:
break;
case ARG_TIMEOUT:
return -EINVAL;
}
break;
case ARG_NO_TTY:
arg_use_tty = false;
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
help();
return -EINVAL;
}
return 1;
}
static int ask_agent(void) {
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"
"NotAfter=%llu\n",
(unsigned long) getpid(),
(unsigned long long) not_after);
if (arg_message)
if (arg_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;
continue;
log_error("poll() failed: %m");
r = -errno;
goto finish;
}
if (k <= 0) {
log_notice("Timed out");
r = -ETIME;
goto finish;
}
break;
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] == '+') {
passphrase[n] = 0;
} 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 r;
log_open();
goto finish;
if ((r = ask_password_tty(arg_message, now(CLOCK_MONOTONIC) + arg_timeout, NULL, &password)) >= 0) {
}
} else
r = ask_agent();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}