Compare commits
7 Commits
92e38817d0
...
c6f007a468
Author | SHA1 | Date |
---|---|---|
Lennart Poettering | c6f007a468 | |
Lennart Poettering | 7fdaedf5af | |
Lennart Poettering | 9bbb7fcfcc | |
Lennart Poettering | fd0e60e2b1 | |
Lennart Poettering | 6226ebeb2b | |
Lennart Poettering | 5c095620e2 | |
Lennart Poettering | 1714b4f269 |
|
@ -7,25 +7,30 @@ SPDX-License-Identifier: LGPL-2.1-or-later
|
|||
|
||||
# Password Agents
|
||||
|
||||
systemd 12 and newer support lightweight password agents which can be used to query the user for system-level passwords or passphrases.
|
||||
These are passphrases that are not related to a specific user, but to some kind of hardware or service.
|
||||
Right now this is used exclusively for encrypted hard-disk passphrases but later on this is likely to be used to query passphrases of SSL certificates at Apache startup time as well.
|
||||
The basic idea is that a system component requesting a password entry can simply drop a simple .ini-style file into `/run/systemd/ask-password` which multiple different agents may watch via `inotify()`, and query the user as necessary.
|
||||
The answer is then sent back to the querier via an `AF_UNIX`/`SOCK_DGRAM` socket.
|
||||
Multiple agents might be running at the same time in which case they all should query the user and the agent which answers first wins.
|
||||
Right now systemd ships with the following passphrase agents:
|
||||
systemd 12 and newer support lightweight password agents which can be used to
|
||||
query the user for system-level passwords or passphrases. These are
|
||||
passphrases that are not related to a specific user, but to some kind of
|
||||
hardware or service. This is used for encrypted hard-disk passphrases or to
|
||||
query passphrases of SSL certificates at web server start-up time. The basic
|
||||
idea is that a system component requesting a password entry can simply drop a
|
||||
simple .ini-style file into `/run/systemd/ask-password/` which multiple
|
||||
different agents may watch via `inotify()`, and query the user as necessary.
|
||||
The answer is then sent back to the querier via an `AF_UNIX`/`SOCK_DGRAM`
|
||||
socket. Multiple agents might be running at the same time in which case they
|
||||
all should query the user and the agent which answers first wins. Right now
|
||||
systemd ships with the following passphrase agents:
|
||||
|
||||
* A Plymouth agent used for querying passwords during boot-up
|
||||
* A console agent used in similar situations if Plymouth is not available
|
||||
* A GNOME agent which can be run as part of the normal user session which pops up a notification message and icon which when clicked receives the passphrase from the user.
|
||||
This is useful and necessary in case an encrypted system hard-disk is plugged in when the machine is already up.
|
||||
* A [`wall(1)`](https://man7.org/linux/man-pages/man1/wall.1.html) agent which sends wall messages as soon as a password shall be entered.
|
||||
* A simple tty agent which is built into "`systemctl start`" (and similar commands) and asks passwords to the user during manual startup of a service
|
||||
* A simple tty agent which can be run manually to respond to all queued passwords
|
||||
|
||||
## Implementing Agents
|
||||
|
||||
It is easy to write additional agents. The basic algorithm to follow looks like this:
|
||||
|
||||
* Create an inotify watch on /run/systemd/ask-password, watch for `IN_CLOSE_WRITE|IN_MOVED_TO`
|
||||
* Create an inotify watch on `/run/systemd/ask-password/`, watch for `IN_CLOSE_WRITE|IN_MOVED_TO`
|
||||
* Ignore all events on files in that directory that do not start with "`ask.`"
|
||||
* As soon as a file named "`ask.xxxx`" shows up, read it. It's a simple `.ini` file that may be parsed with the usual parsers. The `xxxx` suffix is randomized.
|
||||
* Make sure to ignore unknown `.ini` file keys in those files, so that we can easily extend the format later on.
|
||||
|
@ -42,23 +47,57 @@ It is easy to write additional agents. The basic algorithm to follow looks like
|
|||
* Make sure to hide a password query dialog as soon as a) the `ask.xxxx` file is deleted, watch this with inotify. b) the `NotAfter=` time elapses, if it is set `!= 0`.
|
||||
* Access to the socket is restricted to privileged users.
|
||||
To acquire the necessary privileges to send the answer back, consider using PolicyKit.
|
||||
In fact, the GNOME agent we ship does that, and you may simply piggyback on that, by executing "`/usr/bin/pkexec /lib/systemd/systemd-reply-password 1 /path/to/socket`" or "`/usr/bin/pkexec /lib/systemd/systemd-reply-password 0 /path/to/socket`" and writing the password to its standard input.
|
||||
For convenience, an reference implementation is provided by executing "`/usr/bin/pkexec /lib/systemd/systemd-reply-password 1 /path/to/socket`" or "`/usr/bin/pkexec /lib/systemd/systemd-reply-password 0 /path/to/socket`" and writing the password to its standard input.
|
||||
Use '`1`' as argument if a password was entered by the user, or '`0`' if the user canceled the request.
|
||||
* If you do not want to use PK ensure to acquire the necessary privileges in some other way and send a single datagram
|
||||
to the socket consisting of the password string either prefixed with "`+`" or with "`-`" depending on whether the password entry was successful or not.
|
||||
You may but don't have to include a final `NUL` byte in your message.
|
||||
|
||||
Again, it is essential that you stop showing the password box/notification/status icon if the `ask.xxx` file is removed or when `NotAfter=` elapses (if it is set `!= 0`)!
|
||||
Again, it is essential that you stop showing the password
|
||||
box/notification/status icon if the `ask.xxxx` file is removed or when
|
||||
`NotAfter=` elapses (if it is set `!= 0`)!
|
||||
|
||||
It may happen that multiple password entries are pending at the same time.
|
||||
Your agent needs to be able to deal with that. Depending on your environment you may either choose to show all outstanding passwords at the same time or instead only one and as soon as the user has replied to that one go on to the next one.
|
||||
Your agent needs to be able to deal with that. Depending on your environment
|
||||
you may either choose to show all outstanding passwords at the same time or
|
||||
instead only one and as soon as the user has replied to that one go on to the
|
||||
next one.
|
||||
|
||||
You may test this all with manually invoking the "`systemd-ask-password`" tool on the command line.
|
||||
Pass `--no-tty` to ensure the password is asked via the agent system.
|
||||
Note that only privileged users may use this tool (after all this is intended purely for system-level passwords).
|
||||
If you write a system level agent, a smart way to activate it is using systemd
|
||||
`.path` units. This will ensure that systemd will watch the
|
||||
`/run/systemd/ask-password/` directory and spawn the agent as soon as that
|
||||
directory becomes non-empty. In fact, the console, wall and Plymouth agents
|
||||
are started like this. If systemd is used to maintain user sessions as well
|
||||
you can use a similar scheme to automatically spawn your user password agent as
|
||||
well.
|
||||
|
||||
If you write a system level agent a smart way to activate it is using systemd `.path` units.
|
||||
This will ensure that systemd will watch the `/run/systemd/ask-password` directory and spawn the agent as soon as that directory becomes non-empty.
|
||||
In fact, the console, wall and Plymouth agents are started like this.
|
||||
If systemd is used to maintain user sessions as well you can use a similar scheme to automatically spawn your user password agent as well.
|
||||
(As of this moment we have not switched any DE over to use systemd for session management, however.)
|
||||
## Implementing Queriers
|
||||
|
||||
It's also easy to implement applications that want to query passwords this way
|
||||
(i.e. client for the agents above). Simply bind an `AF_UNIX`/`SOCK_DGRAM`
|
||||
socket somewhere (suggestion: you can do this in `/run/systemd/ask-password/`
|
||||
under a randomized socket name, not beginning with `ask.`). Then, create an
|
||||
`/run/systemd/ask-password/ask.xxxx` (replace the `xxxx` by some randomized
|
||||
string) file, with the appropriate `Message=`, `PID=`, `Icon=`, `Echo=`,
|
||||
`NotAfter=` fields in the `[Ask]` section. Most importantly, include `Socket=`
|
||||
pointing to your socket entrypoint. Then, just wait until the password is
|
||||
delivered to you on the socket. Finally, don't forget to remove the file and
|
||||
the socket once done.
|
||||
|
||||
## Testing
|
||||
|
||||
You may test agents by manually invoking the "`systemd-ask-password`" tool from
|
||||
a shell. Pass `--no-tty` to ensure the password is asked via the agent system.
|
||||
|
||||
You may test queriers by manually invoking the
|
||||
"`systemd-tty-ask-password-agent`" from a shell.
|
||||
|
||||
## Unprivileged Per-User Password Agents
|
||||
|
||||
Starting with systemd v257 the scheme is extended to per-user password
|
||||
agents. A second per-user directory `$XDG_RUNTIME_DIR/systemd/ask-password/` is
|
||||
now available, with the same protocol as the system-wide
|
||||
counterpart. Unprivileged, per-directory agents should watch this directory in
|
||||
parallel to the system-wide one. Unprivileged queriers (i.e. clients to these
|
||||
agents) should pick the per-user directory to place their password request
|
||||
files in.
|
||||
|
|
|
@ -30,26 +30,22 @@
|
|||
<refsect1>
|
||||
<title>Description</title>
|
||||
|
||||
<para><command>systemd-ask-password</command> may be used to query
|
||||
a system password or passphrase from the user, using a question
|
||||
message specified on the command line. When run from a TTY it will
|
||||
query a password on the TTY and print it to standard output. When
|
||||
run with no TTY or with <option>--no-tty</option> it will use the
|
||||
system-wide query mechanism, which allows active users to respond via
|
||||
several agents, listed below.</para>
|
||||
<para><command>systemd-ask-password</command> may be used to query a password or passphrase interactively
|
||||
from the user, using a question prompt specified on the command line. When run from a TTY it will query a
|
||||
password on the TTY and print it to standard output. When run with no TTY or with
|
||||
<option>--no-tty</option> it will use a system-wide or per-user agent-based query mechanism, which allows
|
||||
active users to respond via several agents, listed below.</para>
|
||||
|
||||
<para>The purpose of this tool is to query system-wide passwords
|
||||
— that is passwords not attached to a specific user account.
|
||||
Examples include: unlocking encrypted hard disks when they are
|
||||
plugged in or at boot, entering an SSL certificate passphrase for
|
||||
web and VPN servers.</para>
|
||||
<para>The purpose of this tool is to query system-wide or per-user passwords — the former includes
|
||||
passwords possibly not associated to a specific user account. Examples include: unlocking encrypted hard
|
||||
disks when they are plugged in or at boot, entering an SSL certificate passphrase for web and VPN
|
||||
servers.</para>
|
||||
|
||||
<para>Existing agents are:
|
||||
<para>Existing system-level agents are:
|
||||
<itemizedlist>
|
||||
|
||||
<listitem><para>A boot-time password agent asking the user for
|
||||
passwords using
|
||||
<citerefentry project='die-net'><refentrytitle>plymouth</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
|
||||
<listitem><para>A boot-time password agent asking the user for passwords using <citerefentry
|
||||
project='die-net'><refentrytitle>plymouth</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
|
||||
</para></listitem>
|
||||
|
||||
<listitem><para>A boot-time password agent querying the user
|
||||
|
@ -77,17 +73,15 @@
|
|||
all the agents listed above (except for the last one), run as privileged
|
||||
system services. The last one also needs elevated privileges, so
|
||||
should be run through
|
||||
<citerefentry project='die-net'><refentrytitle>sudo</refentrytitle><manvolnum>8</manvolnum></citerefentry>
|
||||
<citerefentry><refentrytitle>run0</refentrytitle><manvolnum>1</manvolnum></citerefentry>
|
||||
or similar.</para>
|
||||
|
||||
<para>Additional password agents may be implemented according to
|
||||
the <ulink url="https://systemd.io/PASSWORD_AGENTS/">systemd Password Agent
|
||||
Specification</ulink>.</para>
|
||||
<para>Additional password agents may be implemented according to the <ulink
|
||||
url="https://systemd.io/PASSWORD_AGENTS/">systemd Password Agent Specification</ulink>.</para>
|
||||
|
||||
<para>If a password is queried on a TTY, the user may press TAB to
|
||||
hide the asterisks normally shown for each character typed.
|
||||
Pressing Backspace as first key achieves the same effect.</para>
|
||||
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
|
@ -241,6 +235,17 @@
|
|||
<xi:include href="version-info.xml" xpointer="v249"/></listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><option>--user</option></term>
|
||||
<term><option>--system</option></term>
|
||||
|
||||
<listitem><para>Controls whether to query the system-wide or the per-user password agents. By default
|
||||
if invoked privileged the system-wide agents are queried, otherwise the per-user ones. These options
|
||||
allow to override this automatic behaviour.</para>
|
||||
|
||||
<xi:include href="version-info.xml" xpointer="v257"/></listitem>
|
||||
</varlistentry>
|
||||
|
||||
<xi:include href="standard-options.xml" xpointer="help" />
|
||||
</variablelist>
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ static int help(void) {
|
|||
return log_oom();
|
||||
|
||||
printf("%1$s [OPTIONS...] MESSAGE\n\n"
|
||||
"%3$sQuery the user for a system passphrase, via the TTY or a UI agent.%4$s\n\n"
|
||||
"%3$sQuery the user for a passphrase, via the TTY or a UI agent.%4$s\n\n"
|
||||
" -h --help Show this help\n"
|
||||
" --icon=NAME Icon name\n"
|
||||
" --id=ID Query identifier (e.g. \"cryptsetup:/dev/sda5\")\n"
|
||||
|
@ -58,6 +58,8 @@ static int help(void) {
|
|||
" --no-output Do not print password to standard output\n"
|
||||
" -n Do not suffix password written to standard output with\n"
|
||||
" newline\n"
|
||||
" --user Ask only our own user's agents\n"
|
||||
" --system Ask agents of the system and of all users\n"
|
||||
"\nSee the %2$s for details.\n",
|
||||
program_invocation_short_name,
|
||||
link,
|
||||
|
@ -81,6 +83,8 @@ static int parse_argv(int argc, char *argv[]) {
|
|||
ARG_NO_OUTPUT,
|
||||
ARG_VERSION,
|
||||
ARG_CREDENTIAL,
|
||||
ARG_USER,
|
||||
ARG_SYSTEM,
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
|
@ -97,6 +101,8 @@ static int parse_argv(int argc, char *argv[]) {
|
|||
{ "keyname", required_argument, NULL, ARG_KEYNAME },
|
||||
{ "no-output", no_argument, NULL, ARG_NO_OUTPUT },
|
||||
{ "credential", required_argument, NULL, ARG_CREDENTIAL },
|
||||
{ "user", no_argument, NULL, ARG_USER },
|
||||
{ "system", no_argument, NULL, ARG_SYSTEM },
|
||||
{}
|
||||
};
|
||||
|
||||
|
@ -183,6 +189,14 @@ static int parse_argv(int argc, char *argv[]) {
|
|||
arg_credential_name = optarg;
|
||||
break;
|
||||
|
||||
case ARG_USER:
|
||||
arg_flags |= ASK_PASSWORD_USER;
|
||||
break;
|
||||
|
||||
case ARG_SYSTEM:
|
||||
arg_flags &= ~ASK_PASSWORD_USER;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
arg_newline = false;
|
||||
break;
|
||||
|
@ -228,6 +242,9 @@ static int run(int argc, char *argv[]) {
|
|||
|
||||
log_setup();
|
||||
|
||||
/* Unprivileged? Then imply ASK_PASSWORD_USER by default */
|
||||
SET_FLAG(arg_flags, ASK_PASSWORD_USER, geteuid() != 0);
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
|
|
@ -282,13 +282,18 @@ static int have_ask_password(void) {
|
|||
if (!dir) {
|
||||
if (errno == ENOENT)
|
||||
return false;
|
||||
else
|
||||
return -errno;
|
||||
|
||||
return -errno;
|
||||
}
|
||||
|
||||
FOREACH_DIRENT_ALL(de, dir, return -errno)
|
||||
FOREACH_DIRENT_ALL(de, dir, return -errno) {
|
||||
if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN))
|
||||
continue;
|
||||
|
||||
if (startswith(de->d_name, "ask."))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -300,9 +305,8 @@ static int manager_dispatch_ask_password_fd(sd_event_source *source,
|
|||
|
||||
m->have_ask_password = have_ask_password();
|
||||
if (m->have_ask_password < 0)
|
||||
/* Log error but continue. Negative have_ask_password
|
||||
* is treated as unknown status. */
|
||||
log_error_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password: %m");
|
||||
/* Log error but continue. Negative have_ask_password is treated as unknown status. */
|
||||
log_warning_errno(m->have_ask_password, "Failed to list /run/systemd/ask-password/, ignoring: %m");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -311,7 +315,6 @@ static void manager_close_ask_password(Manager *m) {
|
|||
assert(m);
|
||||
|
||||
m->ask_password_event_source = sd_event_source_disable_unref(m->ask_password_event_source);
|
||||
m->ask_password_inotify_fd = safe_close(m->ask_password_inotify_fd);
|
||||
m->have_ask_password = -EINVAL;
|
||||
}
|
||||
|
||||
|
@ -320,37 +323,43 @@ static int manager_check_ask_password(Manager *m) {
|
|||
|
||||
assert(m);
|
||||
|
||||
/* We only care about passwords prompts when running in system mode (because that's the only time we
|
||||
* manage a console) */
|
||||
if (!MANAGER_IS_SYSTEM(m))
|
||||
return 0;
|
||||
|
||||
if (!m->ask_password_event_source) {
|
||||
assert(m->ask_password_inotify_fd < 0);
|
||||
|
||||
(void) mkdir_p_label("/run/systemd/ask-password", 0755);
|
||||
|
||||
m->ask_password_inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
|
||||
if (m->ask_password_inotify_fd < 0)
|
||||
_cleanup_close_ int inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
|
||||
if (inotify_fd < 0)
|
||||
return log_error_errno(errno, "Failed to create inotify object: %m");
|
||||
|
||||
r = inotify_add_watch_and_warn(m->ask_password_inotify_fd,
|
||||
"/run/systemd/ask-password",
|
||||
IN_CREATE|IN_DELETE|IN_MOVE);
|
||||
if (r < 0) {
|
||||
manager_close_ask_password(m);
|
||||
(void) mkdir_p_label("/run/systemd/ask-password", 0755);
|
||||
r = inotify_add_watch_and_warn(inotify_fd, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_DELETE|IN_MOVED_TO|IN_ONLYDIR);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
r = sd_event_add_io(m->event, &m->ask_password_event_source,
|
||||
m->ask_password_inotify_fd, EPOLLIN,
|
||||
manager_dispatch_ask_password_fd, m);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to add event source for /run/systemd/ask-password: %m");
|
||||
manager_close_ask_password(m);
|
||||
return r;
|
||||
}
|
||||
_cleanup_(sd_event_source_disable_unrefp) sd_event_source *event_source = NULL;
|
||||
r = sd_event_add_io(
|
||||
m->event,
|
||||
&event_source,
|
||||
inotify_fd,
|
||||
EPOLLIN,
|
||||
manager_dispatch_ask_password_fd,
|
||||
m);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to add event source for /run/systemd/ask-password/: %m");
|
||||
|
||||
(void) sd_event_source_set_description(m->ask_password_event_source, "manager-ask-password");
|
||||
r = sd_event_source_set_io_fd_own(event_source, true);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to pass ownership of /run/systemd/ask-password/ inotify fd to event source: %m");
|
||||
TAKE_FD(inotify_fd);
|
||||
|
||||
(void) sd_event_source_set_description(event_source, "manager-ask-password");
|
||||
|
||||
m->ask_password_event_source = TAKE_PTR(event_source);
|
||||
|
||||
/* Queries might have been added meanwhile... */
|
||||
manager_dispatch_ask_password_fd(m->ask_password_event_source,
|
||||
m->ask_password_inotify_fd, EPOLLIN, m);
|
||||
(void) manager_dispatch_ask_password_fd(m->ask_password_event_source, sd_event_source_get_io_fd(m->ask_password_event_source), EPOLLIN, m);
|
||||
}
|
||||
|
||||
return m->have_ask_password;
|
||||
|
@ -908,7 +917,6 @@ int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags,
|
|||
.dev_autofs_fd = -EBADF,
|
||||
.cgroup_inotify_fd = -EBADF,
|
||||
.pin_cgroupfs_fd = -EBADF,
|
||||
.ask_password_inotify_fd = -EBADF,
|
||||
.idle_pipe = { -EBADF, -EBADF, -EBADF, -EBADF},
|
||||
|
||||
/* start as id #1, so that we can leave #0 around as "null-like" value */
|
||||
|
|
|
@ -433,7 +433,6 @@ struct Manager {
|
|||
|
||||
/* Do we have any outstanding password prompts? */
|
||||
int have_ask_password;
|
||||
int ask_password_inotify_fd;
|
||||
sd_event_source *ask_password_event_source;
|
||||
|
||||
/* Type=idle pipes */
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "glyph-util.h"
|
||||
#include "inotify-util.h"
|
||||
#include "io-util.h"
|
||||
#include "iovec-util.h"
|
||||
#include "keyring-util.h"
|
||||
|
@ -36,6 +37,7 @@
|
|||
#include "missing_syscall.h"
|
||||
#include "mkdir-label.h"
|
||||
#include "nulstr-util.h"
|
||||
#include "path-lookup.h"
|
||||
#include "plymouth-util.h"
|
||||
#include "process-util.h"
|
||||
#include "random-util.h"
|
||||
|
@ -57,7 +59,7 @@ static int lookup_key(const char *keyname, key_serial_t *ret) {
|
|||
assert(keyname);
|
||||
assert(ret);
|
||||
|
||||
serial = request_key("user", keyname, NULL, 0);
|
||||
serial = request_key("user", keyname, /* callout_info= */ NULL, /* dest_keyring= */ 0);
|
||||
if (serial == -1)
|
||||
return negative_errno();
|
||||
|
||||
|
@ -85,6 +87,34 @@ static int retrieve_key(key_serial_t serial, char ***ret) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int get_ask_password_directory_for_flags(AskPasswordFlags flags, char **ret) {
|
||||
int r;
|
||||
|
||||
if (FLAGS_SET(flags, ASK_PASSWORD_USER))
|
||||
return acquire_user_ask_password_directory(ret);
|
||||
|
||||
r = strdup_to(ret, "/run/systemd/ask-password/");
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return 1; /* there's a suitable directory known to us */
|
||||
}
|
||||
|
||||
static int touch_ask_password_directory(AskPasswordFlags flags) {
|
||||
int r;
|
||||
|
||||
_cleanup_free_ char *p = NULL;
|
||||
r = get_ask_password_directory_for_flags(flags, &p);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
r = touch(p);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return 1; /* did something */
|
||||
}
|
||||
|
||||
static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **passwords) {
|
||||
_cleanup_strv_free_erase_ char **l = NULL;
|
||||
_cleanup_(erase_and_freep) char *p = NULL;
|
||||
|
@ -107,7 +137,7 @@ static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **pa
|
|||
} else if (r != -ENOKEY)
|
||||
return r;
|
||||
|
||||
r = strv_extend_strv(&l, passwords, true);
|
||||
r = strv_extend_strv(&l, passwords, /* filter_duplicates= */ true);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
|
@ -129,7 +159,7 @@ static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **pa
|
|||
log_debug_errno(errno, "Failed to adjust kernel keyring key timeout: %m");
|
||||
|
||||
/* Tell everyone to check the keyring */
|
||||
(void) touch("/run/systemd/ask-password");
|
||||
(void) touch_ask_password_directory(flags);
|
||||
|
||||
log_debug("Added key to kernel keyring as %" PRIi32 ".", serial);
|
||||
|
||||
|
@ -220,16 +250,16 @@ int ask_password_plymouth(
|
|||
const char *flag_file,
|
||||
char ***ret) {
|
||||
|
||||
_cleanup_close_ int fd = -EBADF, notify = -EBADF;
|
||||
_cleanup_close_ int fd = -EBADF, inotify_fd = -EBADF;
|
||||
_cleanup_free_ char *packet = NULL;
|
||||
ssize_t k;
|
||||
int r, n;
|
||||
struct pollfd pollfd[2] = {};
|
||||
char buffer[LINE_MAX];
|
||||
size_t p = 0;
|
||||
enum {
|
||||
POLL_SOCKET,
|
||||
POLL_INOTIFY
|
||||
POLL_INOTIFY,
|
||||
_POLL_MAX,
|
||||
};
|
||||
|
||||
assert(ret);
|
||||
|
@ -240,11 +270,11 @@ int ask_password_plymouth(
|
|||
const char *message = req && req->message ? req->message : "Password:";
|
||||
|
||||
if (flag_file) {
|
||||
notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
|
||||
if (notify < 0)
|
||||
inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
|
||||
if (inotify_fd < 0)
|
||||
return -errno;
|
||||
|
||||
if (inotify_add_watch(notify, flag_file, IN_ATTRIB) < 0) /* for the link count */
|
||||
if (inotify_add_watch(inotify_fd, flag_file, IN_ATTRIB) < 0) /* for the link count */
|
||||
return -errno;
|
||||
}
|
||||
|
||||
|
@ -266,10 +296,11 @@ int ask_password_plymouth(
|
|||
|
||||
CLEANUP_ERASE(buffer);
|
||||
|
||||
pollfd[POLL_SOCKET].fd = fd;
|
||||
pollfd[POLL_SOCKET].events = POLLIN;
|
||||
pollfd[POLL_INOTIFY].fd = notify;
|
||||
pollfd[POLL_INOTIFY].events = POLLIN;
|
||||
struct pollfd pollfd[_POLL_MAX] = {
|
||||
[POLL_SOCKET] = { .fd = fd, .events = POLLIN },
|
||||
[POLL_INOTIFY] = { .fd = inotify_fd, .events = POLLIN },
|
||||
};
|
||||
size_t n_pollfd = inotify_fd >= 0 ? _POLL_MAX : _POLL_MAX-1;
|
||||
|
||||
for (;;) {
|
||||
usec_t timeout;
|
||||
|
@ -282,7 +313,7 @@ int ask_password_plymouth(
|
|||
if (flag_file && access(flag_file, F_OK) < 0)
|
||||
return -errno;
|
||||
|
||||
r = ppoll_usec(pollfd, notify >= 0 ? 2 : 1, timeout);
|
||||
r = ppoll_usec(pollfd, n_pollfd, timeout);
|
||||
if (r == -EINTR)
|
||||
continue;
|
||||
if (r < 0)
|
||||
|
@ -290,8 +321,8 @@ int ask_password_plymouth(
|
|||
if (r == 0)
|
||||
return -ETIME;
|
||||
|
||||
if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0)
|
||||
(void) flush_fd(notify);
|
||||
if (inotify_fd >= 0 && pollfd[POLL_INOTIFY].revents != 0)
|
||||
(void) flush_fd(inotify_fd);
|
||||
|
||||
if (pollfd[POLL_SOCKET].revents == 0)
|
||||
continue;
|
||||
|
@ -382,11 +413,10 @@ int ask_password_tty(
|
|||
};
|
||||
|
||||
bool reset_tty = false, dirty = false, use_color = false, press_tab_visible = false;
|
||||
_cleanup_close_ int cttyfd = -EBADF, notify = -EBADF;
|
||||
_cleanup_close_ int cttyfd = -EBADF, inotify_fd = -EBADF;
|
||||
struct termios old_termios, new_termios;
|
||||
char passphrase[LINE_MAX + 1] = {}, *x;
|
||||
_cleanup_strv_free_erase_ char **l = NULL;
|
||||
struct pollfd pollfd[_POLL_MAX];
|
||||
size_t p = 0, codepoint = 0;
|
||||
int r;
|
||||
|
||||
|
@ -405,23 +435,36 @@ int ask_password_tty(
|
|||
message = strjoina(special_glyph(SPECIAL_GLYPH_LOCK_AND_KEY), " ", message);
|
||||
|
||||
if (flag_file || (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyring)) {
|
||||
notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
|
||||
if (notify < 0)
|
||||
inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
|
||||
if (inotify_fd < 0)
|
||||
return -errno;
|
||||
}
|
||||
if (flag_file) {
|
||||
if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0)
|
||||
if (inotify_add_watch(inotify_fd, flag_file, IN_ATTRIB /* for the link count */) < 0)
|
||||
return -errno;
|
||||
}
|
||||
if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && req && keyring) {
|
||||
r = ask_password_keyring(req, flags, ret);
|
||||
if (r >= 0)
|
||||
return 0;
|
||||
else if (r != -ENOKEY)
|
||||
if (r != -ENOKEY)
|
||||
return r;
|
||||
|
||||
if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_ATTRIB /* for mtime */) < 0)
|
||||
return -errno;
|
||||
/* Let's watch the askpw directory for mtime changes, which we issue above whenever the
|
||||
* keyring changes */
|
||||
_cleanup_free_ char *watch_path = NULL;
|
||||
r = get_ask_password_directory_for_flags(flags, &watch_path);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0) {
|
||||
_cleanup_close_ int watch_fd = open_mkdir(watch_path, O_CLOEXEC|O_RDONLY, 0755);
|
||||
if (watch_fd < 0)
|
||||
return watch_fd;
|
||||
|
||||
r = inotify_add_watch_fd(inotify_fd, watch_fd, IN_ONLYDIR|IN_ATTRIB /* for mtime */);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
CLEANUP_ERASE(passphrase);
|
||||
|
@ -466,14 +509,11 @@ int ask_password_tty(
|
|||
reset_tty = true;
|
||||
}
|
||||
|
||||
pollfd[POLL_TTY] = (struct pollfd) {
|
||||
.fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO,
|
||||
.events = POLLIN,
|
||||
};
|
||||
pollfd[POLL_INOTIFY] = (struct pollfd) {
|
||||
.fd = notify,
|
||||
.events = POLLIN,
|
||||
struct pollfd pollfd[_POLL_MAX] = {
|
||||
{ .fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO, .events = POLLIN },
|
||||
{ .fd = inotify_fd, .events = POLLIN },
|
||||
};
|
||||
size_t n_pollfd = inotify_fd >= 0 ? _POLL_MAX : _POLL_MAX-1;
|
||||
|
||||
for (;;) {
|
||||
_cleanup_(erase_char) char c;
|
||||
|
@ -491,7 +531,7 @@ int ask_password_tty(
|
|||
goto finish;
|
||||
}
|
||||
|
||||
r = ppoll_usec(pollfd, notify >= 0 ? 2 : 1, timeout);
|
||||
r = ppoll_usec(pollfd, n_pollfd, timeout);
|
||||
if (r == -EINTR)
|
||||
continue;
|
||||
if (r < 0)
|
||||
|
@ -501,8 +541,8 @@ int ask_password_tty(
|
|||
goto finish;
|
||||
}
|
||||
|
||||
if (notify >= 0 && pollfd[POLL_INOTIFY].revents != 0 && keyring) {
|
||||
(void) flush_fd(notify);
|
||||
if (inotify_fd >= 0 && pollfd[POLL_INOTIFY].revents != 0 && keyring) {
|
||||
(void) flush_fd(inotify_fd);
|
||||
|
||||
r = ask_password_keyring(req, flags, ret);
|
||||
if (r >= 0) {
|
||||
|
@ -659,20 +699,21 @@ finish:
|
|||
return r;
|
||||
}
|
||||
|
||||
static int create_socket(char **ret) {
|
||||
static int create_socket(const char *askpwdir, char **ret) {
|
||||
_cleanup_free_ char *path = NULL;
|
||||
union sockaddr_union sa;
|
||||
socklen_t sa_len;
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
int r;
|
||||
|
||||
assert(askpwdir);
|
||||
assert(ret);
|
||||
|
||||
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
if (asprintf(&path, "/run/systemd/ask-password/sck.%" PRIx64, random_u64()) < 0)
|
||||
if (asprintf(&path, "%s/sck.%" PRIx64, askpwdir, random_u64()) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sockaddr_un_set_path(&sa.un, path);
|
||||
|
@ -699,19 +740,17 @@ int ask_password_agent(
|
|||
char ***ret) {
|
||||
|
||||
enum {
|
||||
FD_SOCKET,
|
||||
FD_SIGNAL,
|
||||
FD_INOTIFY,
|
||||
_FD_MAX
|
||||
POLL_SOCKET,
|
||||
POLL_SIGNAL,
|
||||
POLL_INOTIFY,
|
||||
_POLL_MAX
|
||||
};
|
||||
|
||||
_cleanup_close_ int socket_fd = -EBADF, signal_fd = -EBADF, notify = -EBADF, fd = -EBADF;
|
||||
char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
|
||||
char final[sizeof(temp)] = "";
|
||||
_cleanup_free_ char *socket_name = NULL;
|
||||
_cleanup_close_ int socket_fd = -EBADF, signal_fd = -EBADF, inotify_fd = -EBADF, dfd = -EBADF;
|
||||
_cleanup_(unlink_and_freep) char *socket_name = NULL;
|
||||
_cleanup_free_ char *temp = NULL, *final = NULL;
|
||||
_cleanup_strv_free_erase_ char **l = NULL;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
struct pollfd pollfd[_FD_MAX];
|
||||
sigset_t mask, oldmask;
|
||||
int r;
|
||||
|
||||
|
@ -727,7 +766,20 @@ int ask_password_agent(
|
|||
assert_se(sigset_add_many(&mask, SIGINT, SIGTERM) >= 0);
|
||||
assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) >= 0);
|
||||
|
||||
(void) mkdir_p_label("/run/systemd/ask-password", 0755);
|
||||
_cleanup_free_ char *askpwdir = NULL;
|
||||
r = get_ask_password_directory_for_flags(flags, &askpwdir);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
if (r == 0) {
|
||||
r = -ENXIO;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
dfd = open_mkdir(askpwdir, O_RDONLY|O_CLOEXEC, 0755);
|
||||
if (dfd < 0) {
|
||||
r = log_debug_errno(dfd, "Failed to open directory '%s': %m", askpwdir);
|
||||
goto finish;
|
||||
}
|
||||
|
||||
if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && req && req->keyring) {
|
||||
r = ask_password_keyring(req, flags, ret);
|
||||
|
@ -737,30 +789,25 @@ int ask_password_agent(
|
|||
} else if (r != -ENOKEY)
|
||||
goto finish;
|
||||
|
||||
notify = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
|
||||
if (notify < 0) {
|
||||
inotify_fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
|
||||
if (inotify_fd < 0) {
|
||||
r = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = RET_NERRNO(inotify_add_watch(notify, "/run/systemd/ask-password", IN_ATTRIB /* for mtime */));
|
||||
r = inotify_add_watch_fd(inotify_fd, dfd, IN_ONLYDIR|IN_ATTRIB /* for mtime */);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
}
|
||||
|
||||
fd = mkostemp_safe(temp);
|
||||
if (fd < 0) {
|
||||
r = fd;
|
||||
if (asprintf(&final, "ask.%" PRIu64, random_u64()) < 0) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
(void) fchmod(fd, 0644);
|
||||
|
||||
f = take_fdopen(&fd, "w");
|
||||
if (!f) {
|
||||
r = -errno;
|
||||
r = fopen_temporary_at(dfd, final, &f, &temp);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
}
|
||||
|
||||
signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
|
||||
if (signal_fd < 0) {
|
||||
|
@ -768,7 +815,7 @@ int ask_password_agent(
|
|||
goto finish;
|
||||
}
|
||||
|
||||
socket_fd = create_socket(&socket_name);
|
||||
socket_fd = create_socket(askpwdir, &socket_name);
|
||||
if (socket_fd < 0) {
|
||||
r = socket_fd;
|
||||
goto finish;
|
||||
|
@ -800,27 +847,28 @@ int ask_password_agent(
|
|||
fprintf(f, "Id=%s\n", req->id);
|
||||
}
|
||||
|
||||
if (fchmod(fileno(f), 0644) < 0) {
|
||||
r = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = fflush_and_check(f);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
memcpy(final, temp, sizeof(temp));
|
||||
|
||||
final[sizeof(final)-11] = 'a';
|
||||
final[sizeof(final)-10] = 's';
|
||||
final[sizeof(final)-9] = 'k';
|
||||
|
||||
r = RET_NERRNO(rename(temp, final));
|
||||
if (r < 0)
|
||||
if (renameat(dfd, temp, dfd, final) < 0) {
|
||||
r = -errno;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
zero(pollfd);
|
||||
pollfd[FD_SOCKET].fd = socket_fd;
|
||||
pollfd[FD_SOCKET].events = POLLIN;
|
||||
pollfd[FD_SIGNAL].fd = signal_fd;
|
||||
pollfd[FD_SIGNAL].events = POLLIN;
|
||||
pollfd[FD_INOTIFY].fd = notify;
|
||||
pollfd[FD_INOTIFY].events = POLLIN;
|
||||
temp = mfree(temp);
|
||||
|
||||
struct pollfd pollfd[_POLL_MAX] = {
|
||||
[POLL_SOCKET] = { .fd = socket_fd, .events = POLLIN },
|
||||
[POLL_SIGNAL] = { .fd = signal_fd, .events = POLLIN },
|
||||
[POLL_INOTIFY] = { .fd = inotify_fd, .events = POLLIN },
|
||||
};
|
||||
size_t n_pollfd = inotify_fd >= 0 ? _POLL_MAX : _POLL_MAX - 1;
|
||||
|
||||
for (;;) {
|
||||
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control;
|
||||
|
@ -835,7 +883,7 @@ int ask_password_agent(
|
|||
else
|
||||
timeout = USEC_INFINITY;
|
||||
|
||||
r = ppoll_usec(pollfd, notify >= 0 ? _FD_MAX : _FD_MAX - 1, timeout);
|
||||
r = ppoll_usec(pollfd, n_pollfd, timeout);
|
||||
if (r == -EINTR)
|
||||
continue;
|
||||
if (r < 0)
|
||||
|
@ -845,13 +893,13 @@ int ask_password_agent(
|
|||
goto finish;
|
||||
}
|
||||
|
||||
if (pollfd[FD_SIGNAL].revents & POLLIN) {
|
||||
if (pollfd[POLL_SIGNAL].revents & POLLIN) {
|
||||
r = -EINTR;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
if (notify >= 0 && pollfd[FD_INOTIFY].revents != 0) {
|
||||
(void) flush_fd(notify);
|
||||
if (inotify_fd >= 0 && pollfd[POLL_INOTIFY].revents != 0) {
|
||||
(void) flush_fd(inotify_fd);
|
||||
|
||||
if (req && req->keyring) {
|
||||
r = ask_password_keyring(req, flags, ret);
|
||||
|
@ -863,10 +911,10 @@ int ask_password_agent(
|
|||
}
|
||||
}
|
||||
|
||||
if (pollfd[FD_SOCKET].revents == 0)
|
||||
if (pollfd[POLL_SOCKET].revents == 0)
|
||||
continue;
|
||||
|
||||
if (pollfd[FD_SOCKET].revents != POLLIN) {
|
||||
if (pollfd[POLL_SOCKET].revents != POLLIN) {
|
||||
r = -EIO;
|
||||
goto finish;
|
||||
}
|
||||
|
@ -911,8 +959,8 @@ int ask_password_agent(
|
|||
continue;
|
||||
}
|
||||
|
||||
if (ucred->uid != 0) {
|
||||
log_debug("Got request from unprivileged user. Ignoring.");
|
||||
if (ucred->uid != getuid() && ucred->uid != 0) {
|
||||
log_debug("Got response from bad user. Ignoring.");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -951,13 +999,13 @@ int ask_password_agent(
|
|||
r = 0;
|
||||
|
||||
finish:
|
||||
if (socket_name)
|
||||
(void) unlink(socket_name);
|
||||
|
||||
(void) unlink(temp);
|
||||
|
||||
if (final[0])
|
||||
(void) unlink(final);
|
||||
if (temp) {
|
||||
assert(dfd >= 0);
|
||||
(void) unlinkat(dfd, temp, 0);
|
||||
} else if (final) {
|
||||
assert(dfd >= 0);
|
||||
(void) unlinkat(dfd, final, 0);
|
||||
}
|
||||
|
||||
assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
|
||||
return r;
|
||||
|
@ -1021,3 +1069,18 @@ int ask_password_auto(
|
|||
|
||||
return -EUNATCH;
|
||||
}
|
||||
|
||||
int acquire_user_ask_password_directory(char **ret) {
|
||||
int r;
|
||||
|
||||
r = xdg_user_runtime_dir(ret, "systemd/ask-password");
|
||||
if (r == -ENXIO) {
|
||||
if (ret)
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -6,16 +6,17 @@
|
|||
#include "time-util.h"
|
||||
|
||||
typedef enum AskPasswordFlags {
|
||||
ASK_PASSWORD_ACCEPT_CACHED = 1 << 0, /* read from kernel keyring */
|
||||
ASK_PASSWORD_PUSH_CACHE = 1 << 1, /* write to kernel keyring after getting password from elsewhere */
|
||||
ASK_PASSWORD_ECHO = 1 << 2, /* show the password literally while reading, instead of "*" */
|
||||
ASK_PASSWORD_SILENT = 1 << 3, /* do no show any password at all while reading */
|
||||
ASK_PASSWORD_NO_TTY = 1 << 4, /* never ask for password on tty */
|
||||
ASK_PASSWORD_NO_AGENT = 1 << 5, /* never ask for password via agent */
|
||||
ASK_PASSWORD_CONSOLE_COLOR = 1 << 6, /* Use color if /dev/console points to a console that supports color */
|
||||
ASK_PASSWORD_NO_CREDENTIAL = 1 << 7, /* never use $CREDENTIALS_DIRECTORY data */
|
||||
ASK_PASSWORD_HIDE_EMOJI = 1 << 8, /* hide the lock and key emoji */
|
||||
ASK_PASSWORD_HEADLESS = 1 << 9, /* headless mode: never query interactively */
|
||||
ASK_PASSWORD_ACCEPT_CACHED = 1 << 0, /* read from kernel keyring */
|
||||
ASK_PASSWORD_PUSH_CACHE = 1 << 1, /* write to kernel keyring after getting password from elsewhere */
|
||||
ASK_PASSWORD_ECHO = 1 << 2, /* show the password literally while reading, instead of "*" */
|
||||
ASK_PASSWORD_SILENT = 1 << 3, /* do no show any password at all while reading */
|
||||
ASK_PASSWORD_NO_TTY = 1 << 4, /* never ask for password on tty */
|
||||
ASK_PASSWORD_NO_AGENT = 1 << 5, /* never ask for password via agent */
|
||||
ASK_PASSWORD_CONSOLE_COLOR = 1 << 6, /* Use color if /dev/console points to a console that supports color */
|
||||
ASK_PASSWORD_NO_CREDENTIAL = 1 << 7, /* never use $CREDENTIALS_DIRECTORY data */
|
||||
ASK_PASSWORD_HIDE_EMOJI = 1 << 8, /* hide the lock and key emoji */
|
||||
ASK_PASSWORD_HEADLESS = 1 << 9, /* headless mode: never query interactively */
|
||||
ASK_PASSWORD_USER = 1 << 10, /* query only our own agents, not any system password agents */
|
||||
} AskPasswordFlags;
|
||||
|
||||
/* Encapsulates the mostly static fields of a password query */
|
||||
|
@ -31,3 +32,5 @@ int ask_password_tty(int tty_fd, const AskPasswordRequest *req, usec_t until, As
|
|||
int ask_password_plymouth(const AskPasswordRequest *req, usec_t until, AskPasswordFlags flags, const char *flag_file, char ***ret);
|
||||
int ask_password_agent(const AskPasswordRequest *req, usec_t until, AskPasswordFlags flag, char ***ret);
|
||||
int ask_password_auto(const AskPasswordRequest *req, usec_t until, AskPasswordFlags flag, char ***ret);
|
||||
|
||||
int acquire_user_ask_password_directory(char **ret);
|
||||
|
|
|
@ -55,40 +55,35 @@ static bool arg_console = false;
|
|||
static const char *arg_device = NULL;
|
||||
|
||||
static int send_passwords(const char *socket_name, char **passwords) {
|
||||
_cleanup_(erase_and_freep) char *packet = NULL;
|
||||
_cleanup_close_ int socket_fd = -EBADF;
|
||||
union sockaddr_union sa;
|
||||
socklen_t sa_len;
|
||||
size_t packet_length = 1;
|
||||
char *d;
|
||||
ssize_t n;
|
||||
int r;
|
||||
|
||||
assert(socket_name);
|
||||
|
||||
union sockaddr_union sa;
|
||||
r = sockaddr_un_set_path(&sa.un, socket_name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
sa_len = r;
|
||||
socklen_t sa_len = r;
|
||||
|
||||
size_t packet_length = 1;
|
||||
STRV_FOREACH(p, passwords)
|
||||
packet_length += strlen(*p) + 1;
|
||||
|
||||
packet = new(char, packet_length);
|
||||
_cleanup_(erase_and_freep) char *packet = new(char, packet_length);
|
||||
if (!packet)
|
||||
return -ENOMEM;
|
||||
|
||||
packet[0] = '+';
|
||||
|
||||
d = packet + 1;
|
||||
char *d = packet + 1;
|
||||
STRV_FOREACH(p, passwords)
|
||||
d = stpcpy(d, *p) + 1;
|
||||
|
||||
socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
|
||||
_cleanup_close_ int socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
|
||||
if (socket_fd < 0)
|
||||
return log_debug_errno(errno, "socket(): %m");
|
||||
|
||||
n = sendto(socket_fd, packet, packet_length, MSG_NOSIGNAL, &sa.sa, sa_len);
|
||||
ssize_t n = sendto(socket_fd, packet, packet_length, MSG_NOSIGNAL, &sa.sa, sa_len);
|
||||
if (n < 0)
|
||||
return log_debug_errno(errno, "sendto(): %m");
|
||||
|
||||
|
@ -96,12 +91,9 @@ static int send_passwords(const char *socket_name, char **passwords) {
|
|||
}
|
||||
|
||||
static bool wall_tty_match(const char *path, bool is_local, void *userdata) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
struct stat st;
|
||||
|
||||
assert(path_is_absolute(path));
|
||||
|
||||
struct stat st;
|
||||
if (lstat(path, &st) < 0) {
|
||||
log_debug_errno(errno, "Failed to stat %s: %m", path);
|
||||
return true;
|
||||
|
@ -120,12 +112,13 @@ static bool wall_tty_match(const char *path, bool is_local, void *userdata) {
|
|||
* advantage that the block will automatically go away if the
|
||||
* process dies. */
|
||||
|
||||
_cleanup_free_ char *p = NULL;
|
||||
if (asprintf(&p, "/run/systemd/ask-password-block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0) {
|
||||
log_oom();
|
||||
log_oom_debug();
|
||||
return true;
|
||||
}
|
||||
|
||||
fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
|
||||
_cleanup_close_ int fd = open(p, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
|
||||
if (fd < 0) {
|
||||
log_debug_errno(errno, "Failed to open the wall pipe: %m");
|
||||
return 1;
|
||||
|
@ -162,6 +155,7 @@ static int agent_ask_password_tty(
|
|||
r = ask_password_tty(tty_fd, &req, until, flags, flag_file, ret);
|
||||
|
||||
if (arg_console) {
|
||||
assert(tty_fd >= 0);
|
||||
tty_fd = safe_close(tty_fd);
|
||||
release_terminal();
|
||||
|
||||
|
@ -172,7 +166,7 @@ static int agent_ask_password_tty(
|
|||
return r;
|
||||
}
|
||||
|
||||
static int process_one_password_file(const char *filename) {
|
||||
static int process_one_password_file(const char *filename, FILE *f) {
|
||||
_cleanup_free_ char *socket_name = NULL, *message = NULL;
|
||||
bool accept_cached = false, echo = false, silent = false;
|
||||
uint64_t not_after = 0;
|
||||
|
@ -192,13 +186,17 @@ static int process_one_password_file(const char *filename) {
|
|||
int r;
|
||||
|
||||
assert(filename);
|
||||
assert(f);
|
||||
|
||||
r = config_parse(NULL, filename, NULL,
|
||||
NULL,
|
||||
config_item_table_lookup, items,
|
||||
r = config_parse(/* unit= */ NULL,
|
||||
filename,
|
||||
f,
|
||||
/* sections= */ "Ask\0",
|
||||
config_item_table_lookup,
|
||||
items,
|
||||
CONFIG_PARSE_RELAXED|CONFIG_PARSE_WARN,
|
||||
NULL,
|
||||
NULL);
|
||||
/* userdata= */ NULL,
|
||||
/* ret_stat= */ NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
@ -297,41 +295,44 @@ static int wall_tty_block(void) {
|
|||
return fd;
|
||||
}
|
||||
|
||||
static int process_password_files(void) {
|
||||
static int process_password_files(const char *path) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
int r = 0;
|
||||
int ret = 0, r;
|
||||
|
||||
d = opendir("/run/systemd/ask-password");
|
||||
assert(path);
|
||||
|
||||
d = opendir(path);
|
||||
if (!d) {
|
||||
if (errno == ENOENT)
|
||||
return 0;
|
||||
|
||||
return log_error_errno(errno, "Failed to open /run/systemd/ask-password: %m");
|
||||
return log_error_errno(errno, "Failed to open '%s': %m", path);
|
||||
}
|
||||
|
||||
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read directory: %m")) {
|
||||
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read directory '%s': %m", path)) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
int q;
|
||||
|
||||
/* We only support /run on tmpfs, hence we can rely on
|
||||
* d_type to be reliable */
|
||||
|
||||
if (de->d_type != DT_REG)
|
||||
if (!IN_SET(de->d_type, DT_REG, DT_UNKNOWN))
|
||||
continue;
|
||||
|
||||
if (!startswith(de->d_name, "ask."))
|
||||
continue;
|
||||
|
||||
p = path_join("/run/systemd/ask-password", de->d_name);
|
||||
p = path_join(path, de->d_name);
|
||||
if (!p)
|
||||
return log_oom();
|
||||
|
||||
q = process_one_password_file(p);
|
||||
if (q < 0 && r == 0)
|
||||
r = q;
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
r = xfopenat(dirfd(d), de->d_name, "re", O_NOFOLLOW, &f);
|
||||
if (r < 0) {
|
||||
log_warning_errno(r, "Failed to open '%s', ignoring: %m", p);
|
||||
continue;
|
||||
}
|
||||
|
||||
RET_GATHER(ret, process_one_password_file(p, f));
|
||||
}
|
||||
|
||||
return r;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int process_and_watch_password_files(bool watch) {
|
||||
|
@ -341,6 +342,7 @@ static int process_and_watch_password_files(bool watch) {
|
|||
_FD_MAX
|
||||
};
|
||||
|
||||
_cleanup_free_ char *user_ask_password_directory = NULL;
|
||||
_unused_ _cleanup_close_ int tty_block_fd = -EBADF;
|
||||
_cleanup_close_ int notify = -EBADF, signal_fd = -EBADF;
|
||||
struct pollfd pollfd[_FD_MAX];
|
||||
|
@ -351,6 +353,12 @@ static int process_and_watch_password_files(bool watch) {
|
|||
|
||||
(void) mkdir_p_label("/run/systemd/ask-password", 0755);
|
||||
|
||||
r = acquire_user_ask_password_directory(&user_ask_password_directory);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to determine per-user password directory: %m");
|
||||
if (r > 0)
|
||||
(void) mkdir_p_label(user_ask_password_directory, 0755);
|
||||
|
||||
assert_se(sigemptyset(&mask) >= 0);
|
||||
assert_se(sigset_add_many(&mask, SIGTERM) >= 0);
|
||||
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) >= 0);
|
||||
|
@ -366,29 +374,34 @@ static int process_and_watch_password_files(bool watch) {
|
|||
if (notify < 0)
|
||||
return log_error_errno(errno, "Failed to allocate directory watch: %m");
|
||||
|
||||
r = inotify_add_watch_and_warn(notify, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO);
|
||||
r = inotify_add_watch_and_warn(notify, "/run/systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO|IN_ONLYDIR);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (user_ask_password_directory) {
|
||||
r = inotify_add_watch_and_warn(notify, user_ask_password_directory, IN_CLOSE_WRITE|IN_MOVED_TO|IN_ONLYDIR);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
pollfd[FD_INOTIFY] = (struct pollfd) { .fd = notify, .events = POLLIN };
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
usec_t timeout = USEC_INFINITY;
|
||||
|
||||
r = process_password_files();
|
||||
if (r < 0) {
|
||||
if (r == -ECANCELED)
|
||||
/* Disable poll() timeout since at least one password has
|
||||
* been skipped and therefore one file remains and is
|
||||
* unlikely to trigger any events. */
|
||||
timeout = 0;
|
||||
else
|
||||
/* FIXME: we should do something here since otherwise the service
|
||||
* requesting the password won't notice the error and will wait
|
||||
* indefinitely. */
|
||||
log_error_errno(r, "Failed to process password: %m");
|
||||
}
|
||||
r = process_password_files("/run/systemd/ask-password");
|
||||
if (user_ask_password_directory)
|
||||
RET_GATHER(r, process_password_files(user_ask_password_directory));
|
||||
if (r == -ECANCELED)
|
||||
/* Disable poll() timeout since at least one password has been skipped and therefore
|
||||
* one file remains and is unlikely to trigger any events. */
|
||||
timeout = 0;
|
||||
else if (r < 0)
|
||||
/* FIXME: we should do something here since otherwise the service
|
||||
* requesting the password won't notice the error and will wait
|
||||
* indefinitely. */
|
||||
log_warning_errno(r, "Failed to process password, ignoring: %m");
|
||||
|
||||
if (!watch)
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue