| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <poll.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <sys/inotify.h> |
| 7 |
#include <sys/signalfd.h> |
| 8 |
#include <sys/stat.h> |
| 9 |
#include <termios.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "ansi-color.h" |
| 14 |
#include "ask-password-api.h" |
| 15 |
#include "creds-util.h" |
| 16 |
#include "fd-util.h" |
| 17 |
#include "fileio.h" |
| 18 |
#include "format-util.h" |
| 19 |
#include "fs-util.h" |
| 20 |
#include "glyph-util.h" |
| 21 |
#include "inotify-util.h" |
| 22 |
#include "io-util.h" |
| 23 |
#include "iovec-util.h" |
| 24 |
#include "keyring-util.h" |
| 25 |
#include "log.h" |
| 26 |
#include "nulstr-util.h" |
| 27 |
#include "parse-util.h" |
| 28 |
#include "path-lookup.h" |
| 29 |
#include "plymouth-util.h" |
| 30 |
#include "process-util.h" |
| 31 |
#include "random-util.h" |
| 32 |
#include "signal-util.h" |
| 33 |
#include "socket-util.h" |
| 34 |
#include "string-table.h" |
| 35 |
#include "string-util.h" |
| 36 |
#include "strv.h" |
| 37 |
#include "terminal-util.h" |
| 38 |
#include "time-util.h" |
| 39 |
#include "tmpfile-util.h" |
| 40 |
#include "umask-util.h" |
| 41 |
#include "utf8.h" |
| 42 |
|
| 43 |
#define KEYRING_TIMEOUT_USEC ((5 * USEC_PER_MINUTE) / 2) |
| 44 |
|
| 45 |
static const char* keyring_table[] = { |
| 46 |
[-KEY_SPEC_THREAD_KEYRING] = "thread", |
| 47 |
[-KEY_SPEC_PROCESS_KEYRING] = "process", |
| 48 |
[-KEY_SPEC_SESSION_KEYRING] = "session", |
| 49 |
[-KEY_SPEC_USER_KEYRING] = "user", |
| 50 |
[-KEY_SPEC_USER_SESSION_KEYRING] = "user-session", |
| 51 |
[-KEY_SPEC_GROUP_KEYRING] = "group", |
| 52 |
}; |
| 53 |
|
| 54 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(keyring, int); |
| 55 |
|
| 56 |
static int lookup_key(const char *keyname, key_serial_t *ret) { |
| 57 |
key_serial_t serial; |
| 58 |
|
| 59 |
assert(keyname); |
| 60 |
assert(ret); |
| 61 |
|
| 62 |
serial = request_key("user", keyname, /* callout_info= */ NULL, /* destringid= */ 0); |
| 63 |
if (serial == -1) |
| 64 |
return negative_errno(); |
| 65 |
|
| 66 |
*ret = serial; |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
static int retrieve_key(key_serial_t serial, char ***ret) { |
| 71 |
_cleanup_(erase_and_freep) void *p = NULL; |
| 72 |
char **l; |
| 73 |
size_t n; |
| 74 |
int r; |
| 75 |
|
| 76 |
assert(ret); |
| 77 |
|
| 78 |
r = keyring_read(serial, &p, &n); |
| 79 |
if (r < 0) |
| 80 |
return r; |
| 81 |
|
| 82 |
l = strv_parse_nulstr(p, n); |
| 83 |
if (!l) |
| 84 |
return -ENOMEM; |
| 85 |
|
| 86 |
*ret = l; |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
static int get_ask_password_directory_for_flags(AskPasswordFlags flags, char **ret) { |
| 91 |
if (FLAGS_SET(flags, ASK_PASSWORD_USER)) |
| 92 |
return acquire_user_ask_password_directory(ret); |
| 93 |
|
| 94 |
return strdup_to_full(ret, "/run/systemd/ask-password/"); /* Returns 1, indicating there's a suitable directory */ |
| 95 |
} |
| 96 |
|
| 97 |
static int touch_ask_password_directory(AskPasswordFlags flags) { |
| 98 |
int r; |
| 99 |
|
| 100 |
_cleanup_free_ char *p = NULL; |
| 101 |
r = get_ask_password_directory_for_flags(flags, &p); |
| 102 |
if (r <= 0) |
| 103 |
return r; |
| 104 |
|
| 105 |
_cleanup_close_ int fd = open_mkdir(p, O_CLOEXEC, 0755); |
| 106 |
if (fd < 0) |
| 107 |
return fd; |
| 108 |
|
| 109 |
r = touch_fd(fd, USEC_INFINITY); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
|
| 113 |
return 1; /* did something */ |
| 114 |
} |
| 115 |
|
| 116 |
static usec_t keyring_cache_timeout(void) { |
| 117 |
static usec_t saved_timeout = KEYRING_TIMEOUT_USEC; |
| 118 |
static bool saved_timeout_set = false; |
| 119 |
int r; |
| 120 |
|
| 121 |
if (saved_timeout_set) |
| 122 |
return saved_timeout; |
| 123 |
|
| 124 |
const char *e = secure_getenv("SYSTEMD_ASK_PASSWORD_KEYRING_TIMEOUT_SEC"); |
| 125 |
if (e) { |
| 126 |
r = parse_sec(e, &saved_timeout); |
| 127 |
if (r < 0) |
| 128 |
log_debug_errno(r, "Invalid value in $SYSTEMD_ASK_PASSWORD_KEYRING_TIMEOUT_SEC, ignoring: %s", e); |
| 129 |
} |
| 130 |
|
| 131 |
saved_timeout_set = true; |
| 132 |
|
| 133 |
return saved_timeout; |
| 134 |
} |
| 135 |
|
| 136 |
static key_serial_t keyring_cache_type(void) { |
| 137 |
static key_serial_t saved_keyring = KEY_SPEC_USER_KEYRING; |
| 138 |
static bool saved_keyring_set = false; |
| 139 |
int r; |
| 140 |
|
| 141 |
if (saved_keyring_set) |
| 142 |
return saved_keyring; |
| 143 |
|
| 144 |
const char *e = secure_getenv("SYSTEMD_ASK_PASSWORD_KEYRING_TYPE"); |
| 145 |
if (e) { |
| 146 |
key_serial_t keyring; |
| 147 |
|
| 148 |
r = safe_atoi32(e, &keyring); |
| 149 |
if (r >= 0) |
| 150 |
if (keyring < 0) |
| 151 |
log_debug_errno(keyring, "Invalid value in $SYSTEMD_ASK_PASSWORD_KEYRING_TYPE, ignoring: %s", e); |
| 152 |
else |
| 153 |
saved_keyring = keyring; |
| 154 |
else { |
| 155 |
keyring = keyring_from_string(e); |
| 156 |
if (keyring < 0) |
| 157 |
log_debug_errno(keyring, "Invalid value in $SYSTEMD_ASK_PASSWORD_KEYRING_TYPE, ignoring: %s", e); |
| 158 |
else |
| 159 |
saved_keyring = -keyring; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
saved_keyring_set = true; |
| 164 |
|
| 165 |
return saved_keyring; |
| 166 |
} |
| 167 |
|
| 168 |
static int add_to_keyring(const char *keyname, AskPasswordFlags flags, char **passwords) { |
| 169 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 170 |
_cleanup_(erase_and_freep) char *p = NULL; |
| 171 |
key_serial_t serial; |
| 172 |
size_t n; |
| 173 |
int r; |
| 174 |
|
| 175 |
assert(keyname); |
| 176 |
|
| 177 |
if (!FLAGS_SET(flags, ASK_PASSWORD_PUSH_CACHE) || keyring_cache_timeout() == 0) |
| 178 |
return 0; |
| 179 |
if (strv_isempty(passwords)) |
| 180 |
return 0; |
| 181 |
|
| 182 |
r = lookup_key(keyname, &serial); |
| 183 |
if (r >= 0) { |
| 184 |
r = retrieve_key(serial, &l); |
| 185 |
if (r < 0) |
| 186 |
return r; |
| 187 |
} else if (r != -ENOKEY) |
| 188 |
return r; |
| 189 |
|
| 190 |
r = strv_extend_strv(&l, passwords, /* filter_duplicates= */ true); |
| 191 |
if (r <= 0) |
| 192 |
return r; |
| 193 |
|
| 194 |
r = strv_make_nulstr(l, &p, &n); |
| 195 |
if (r < 0) |
| 196 |
return r; |
| 197 |
|
| 198 |
/* chop off the final NUL byte. We do this because we want to use the separator NUL bytes only if we |
| 199 |
* have multiple passwords. */ |
| 200 |
n = LESS_BY(n, (size_t) 1); |
| 201 |
|
| 202 |
serial = add_key("user", keyname, p, n, keyring_cache_type()); |
| 203 |
if (serial == -1) |
| 204 |
return -errno; |
| 205 |
|
| 206 |
if (keyring_cache_timeout() != USEC_INFINITY && |
| 207 |
keyctl(KEYCTL_SET_TIMEOUT, |
| 208 |
(unsigned long) serial, |
| 209 |
(unsigned long) DIV_ROUND_UP(keyring_cache_timeout(), USEC_PER_SEC), 0, 0) < 0) |
| 210 |
log_debug_errno(errno, "Failed to adjust kernel keyring key timeout: %m"); |
| 211 |
|
| 212 |
/* Tell everyone to check the keyring */ |
| 213 |
(void) touch_ask_password_directory(flags); |
| 214 |
|
| 215 |
log_debug("Added key to kernel keyring as %" PRIi32 ".", serial); |
| 216 |
|
| 217 |
return 1; |
| 218 |
} |
| 219 |
|
| 220 |
static int add_to_keyring_and_log(const char *keyname, AskPasswordFlags flags, char **passwords) { |
| 221 |
int r; |
| 222 |
|
| 223 |
assert(keyname); |
| 224 |
|
| 225 |
r = add_to_keyring(keyname, flags, passwords); |
| 226 |
if (r < 0) |
| 227 |
return log_debug_errno(r, "Failed to add password to kernel keyring: %m"); |
| 228 |
|
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
static int ask_password_keyring(const AskPasswordRequest *req, AskPasswordFlags flags, char ***ret) { |
| 233 |
key_serial_t serial; |
| 234 |
int r; |
| 235 |
|
| 236 |
assert(req); |
| 237 |
assert(ret); |
| 238 |
|
| 239 |
if (!FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) |
| 240 |
return -EUNATCH; |
| 241 |
|
| 242 |
r = lookup_key(req->keyring, &serial); |
| 243 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r) || IN_SET(r, -EPERM, -ENOKEY)) |
| 244 |
/* When retrieving, the distinction between "kernel or container manager don't support or |
| 245 |
* allow this" and "no matching key known" doesn't matter. Note that we propagate EACCESS |
| 246 |
* here (even if EPERM not) since that is used if the keyring is available, but we lack |
| 247 |
* access to the key. */ |
| 248 |
return -ENOKEY; |
| 249 |
if (r < 0) |
| 250 |
return log_debug_errno(r, "Failed to look up key %s in keyring: %m", req->keyring); |
| 251 |
|
| 252 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 253 |
r = retrieve_key(serial, &l); |
| 254 |
if (r < 0) |
| 255 |
return r; |
| 256 |
|
| 257 |
if (strv_isempty(l)) |
| 258 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOKEY), "Found an empty password from keyring."); |
| 259 |
|
| 260 |
*ret = TAKE_PTR(l); |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
static int backspace_chars(int ttyfd, size_t p) { |
| 265 |
if (ttyfd < 0) |
| 266 |
return 0; |
| 267 |
|
| 268 |
_cleanup_free_ char *buf = malloc_multiply(3, p); |
| 269 |
if (!buf) |
| 270 |
return log_oom(); |
| 271 |
|
| 272 |
for (size_t i = 0; i < p; i++) |
| 273 |
memcpy(buf + 3 * i, "\b \b", 3); |
| 274 |
|
| 275 |
return loop_write(ttyfd, buf, 3 * p); |
| 276 |
} |
| 277 |
|
| 278 |
static int backspace_string(int ttyfd, const char *str) { |
| 279 |
assert(str); |
| 280 |
|
| 281 |
/* Backspaces through enough characters to entirely undo printing of the specified string. */ |
| 282 |
|
| 283 |
if (ttyfd < 0) |
| 284 |
return 0; |
| 285 |
|
| 286 |
size_t m = utf8_n_codepoints(str); |
| 287 |
if (m == SIZE_MAX) |
| 288 |
m = strlen(str); /* Not a valid UTF-8 string? If so, let's backspace the number of bytes |
| 289 |
* output. Most likely this happened because we are not in a UTF-8 locale, |
| 290 |
* and in that case that is the correct thing to do. And even if it's not, |
| 291 |
* terminals tend to stop backspacing at the leftmost column, hence |
| 292 |
* backspacing too much should be mostly OK. */ |
| 293 |
|
| 294 |
return backspace_chars(ttyfd, m); |
| 295 |
} |
| 296 |
|
| 297 |
int ask_password_plymouth( |
| 298 |
const AskPasswordRequest *req, |
| 299 |
AskPasswordFlags flags, |
| 300 |
char ***ret) { |
| 301 |
|
| 302 |
_cleanup_close_ int fd = -EBADF, inotify_fd = -EBADF; |
| 303 |
_cleanup_free_ char *packet = NULL; |
| 304 |
ssize_t k; |
| 305 |
int r, n; |
| 306 |
char buffer[LINE_MAX]; |
| 307 |
size_t p = 0; |
| 308 |
|
| 309 |
assert(req); |
| 310 |
assert(ret); |
| 311 |
|
| 312 |
if (FLAGS_SET(flags, ASK_PASSWORD_HEADLESS)) |
| 313 |
return -ENOEXEC; |
| 314 |
|
| 315 |
const char *message = req->message ?: "Password:"; |
| 316 |
|
| 317 |
if (req->flag_file) { |
| 318 |
inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); |
| 319 |
if (inotify_fd < 0) |
| 320 |
return -errno; |
| 321 |
|
| 322 |
if (inotify_add_watch(inotify_fd, req->flag_file, IN_ATTRIB) < 0) /* for the link count */ |
| 323 |
return -errno; |
| 324 |
} |
| 325 |
|
| 326 |
fd = plymouth_connect(SOCK_NONBLOCK); |
| 327 |
if (fd < 0) |
| 328 |
return fd; |
| 329 |
|
| 330 |
if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) { |
| 331 |
packet = strdup("c"); |
| 332 |
n = 1; |
| 333 |
} else if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) |
| 334 |
packet = NULL; |
| 335 |
if (!packet) |
| 336 |
return -ENOMEM; |
| 337 |
|
| 338 |
r = loop_write_full(fd, packet, n + 1, USEC_INFINITY); |
| 339 |
if (r < 0) |
| 340 |
return r; |
| 341 |
|
| 342 |
CLEANUP_ERASE(buffer); |
| 343 |
|
| 344 |
enum { |
| 345 |
POLL_SOCKET, |
| 346 |
POLL_TWO, |
| 347 |
POLL_THREE, |
| 348 |
_POLL_MAX, |
| 349 |
}; |
| 350 |
|
| 351 |
struct pollfd pollfd[_POLL_MAX] = { |
| 352 |
[POLL_SOCKET] = { |
| 353 |
.fd = fd, |
| 354 |
.events = POLLIN, |
| 355 |
}, |
| 356 |
}; |
| 357 |
size_t n_pollfd = POLL_SOCKET + 1, inotify_idx = SIZE_MAX, hup_fd_idx = SIZE_MAX; |
| 358 |
if (inotify_fd >= 0) |
| 359 |
pollfd[inotify_idx = n_pollfd++] = (struct pollfd) { |
| 360 |
.fd = inotify_fd, |
| 361 |
.events = POLLIN, |
| 362 |
}; |
| 363 |
if (req->hup_fd >= 0) |
| 364 |
pollfd[hup_fd_idx = n_pollfd++] = (struct pollfd) { |
| 365 |
.fd = req->hup_fd, |
| 366 |
.events = POLLHUP, |
| 367 |
}; |
| 368 |
|
| 369 |
assert(n_pollfd <= _POLL_MAX); |
| 370 |
|
| 371 |
for (;;) { |
| 372 |
usec_t timeout; |
| 373 |
|
| 374 |
if (req->until > 0) |
| 375 |
timeout = usec_sub_unsigned(req->until, now(CLOCK_MONOTONIC)); |
| 376 |
else |
| 377 |
timeout = USEC_INFINITY; |
| 378 |
|
| 379 |
if (req->flag_file && access(req->flag_file, F_OK) < 0) |
| 380 |
return -errno; |
| 381 |
|
| 382 |
r = ppoll_usec(pollfd, n_pollfd, timeout); |
| 383 |
if (r == -EINTR) |
| 384 |
continue; |
| 385 |
if (r < 0) |
| 386 |
return r; |
| 387 |
if (r == 0) |
| 388 |
return -ETIME; |
| 389 |
|
| 390 |
if (req->hup_fd >= 0 && pollfd[hup_fd_idx].revents & POLLHUP) |
| 391 |
return -ECONNRESET; |
| 392 |
|
| 393 |
if (inotify_fd >= 0 && pollfd[inotify_idx].revents != 0) |
| 394 |
(void) flush_fd(inotify_fd); |
| 395 |
|
| 396 |
if (pollfd[POLL_SOCKET].revents == 0) |
| 397 |
continue; |
| 398 |
|
| 399 |
k = read(fd, buffer + p, sizeof(buffer) - p); |
| 400 |
if (k < 0) { |
| 401 |
if (ERRNO_IS_TRANSIENT(errno)) |
| 402 |
continue; |
| 403 |
|
| 404 |
return -errno; |
| 405 |
} |
| 406 |
if (k == 0) |
| 407 |
return -EIO; |
| 408 |
|
| 409 |
p += k; |
| 410 |
|
| 411 |
if (buffer[0] == 5) { |
| 412 |
|
| 413 |
if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) { |
| 414 |
/* Hmm, first try with cached |
| 415 |
* passwords failed, so let's retry |
| 416 |
* with a normal password request */ |
| 417 |
packet = mfree(packet); |
| 418 |
|
| 419 |
if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) |
| 420 |
return -ENOMEM; |
| 421 |
|
| 422 |
r = loop_write_full(fd, packet, n + 1, USEC_INFINITY); |
| 423 |
if (r < 0) |
| 424 |
return r; |
| 425 |
|
| 426 |
flags &= ~ASK_PASSWORD_ACCEPT_CACHED; |
| 427 |
p = 0; |
| 428 |
continue; |
| 429 |
} |
| 430 |
|
| 431 |
/* No password, because UI not shown */ |
| 432 |
return -ENOENT; |
| 433 |
|
| 434 |
} else if (IN_SET(buffer[0], 2, 9)) { |
| 435 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 436 |
uint32_t size; |
| 437 |
|
| 438 |
/* One or more answers */ |
| 439 |
if (p < 5) |
| 440 |
continue; |
| 441 |
|
| 442 |
memcpy(&size, buffer+1, sizeof(size)); |
| 443 |
size = le32toh(size); |
| 444 |
if (size + 5 > sizeof(buffer)) |
| 445 |
return -EIO; |
| 446 |
|
| 447 |
if (p-5 < size) |
| 448 |
continue; |
| 449 |
|
| 450 |
l = strv_parse_nulstr(buffer + 5, size); |
| 451 |
if (!l) |
| 452 |
return -ENOMEM; |
| 453 |
|
| 454 |
if (strv_isempty(l)) |
| 455 |
return log_debug_errno(SYNTHETIC_ERRNO(ECANCELED), "Received an empty password."); |
| 456 |
|
| 457 |
*ret = TAKE_PTR(l); |
| 458 |
return 0; |
| 459 |
|
| 460 |
} else |
| 461 |
/* Unknown packet */ |
| 462 |
return -EIO; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
#define NO_ECHO "(no echo) " |
| 467 |
#define PRESS_TAB "(press TAB for no echo) " |
| 468 |
#define SKIPPED "(skipped)" |
| 469 |
|
| 470 |
int ask_password_tty( |
| 471 |
const AskPasswordRequest *req, |
| 472 |
AskPasswordFlags flags, |
| 473 |
char ***ret) { |
| 474 |
|
| 475 |
bool reset_tty = false, dirty = false, use_color = false, press_tab_visible = false; |
| 476 |
_cleanup_close_ int cttyfd = -EBADF, inotify_fd = -EBADF; |
| 477 |
struct termios old_termios, new_termios; |
| 478 |
char passphrase[LINE_MAX + 1] = {}, *x; |
| 479 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 480 |
size_t p = 0, codepoint = 0; |
| 481 |
int r; |
| 482 |
|
| 483 |
assert(req); |
| 484 |
assert(ret); |
| 485 |
|
| 486 |
if (FLAGS_SET(flags, ASK_PASSWORD_HEADLESS)) |
| 487 |
return -ENOEXEC; |
| 488 |
|
| 489 |
if (FLAGS_SET(flags, ASK_PASSWORD_NO_TTY)) |
| 490 |
return -EUNATCH; |
| 491 |
|
| 492 |
const char *message = req->message ?: "Password:"; |
| 493 |
const char *keyring = req->keyring; |
| 494 |
|
| 495 |
if (!FLAGS_SET(flags, ASK_PASSWORD_HIDE_EMOJI) && emoji_enabled()) |
| 496 |
message = strjoina(glyph(GLYPH_LOCK_AND_KEY), " ", message); |
| 497 |
|
| 498 |
if (req->flag_file || (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyring)) { |
| 499 |
inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK); |
| 500 |
if (inotify_fd < 0) |
| 501 |
return -errno; |
| 502 |
} |
| 503 |
if (req->flag_file) |
| 504 |
if (inotify_add_watch(inotify_fd, req->flag_file, IN_ATTRIB /* for the link count */) < 0) |
| 505 |
return -errno; |
| 506 |
if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && keyring) { |
| 507 |
r = ask_password_keyring(req, flags, ret); |
| 508 |
if (r >= 0) |
| 509 |
return 0; |
| 510 |
if (r != -ENOKEY) |
| 511 |
return r; |
| 512 |
|
| 513 |
/* Let's watch the askpw directory for mtime changes, which we issue above whenever the |
| 514 |
* keyring changes */ |
| 515 |
_cleanup_free_ char *watch_path = NULL; |
| 516 |
r = get_ask_password_directory_for_flags(flags, &watch_path); |
| 517 |
if (r < 0) |
| 518 |
return r; |
| 519 |
if (r > 0) { |
| 520 |
_cleanup_close_ int watch_fd = open_mkdir(watch_path, O_CLOEXEC|O_RDONLY, 0755); |
| 521 |
if (watch_fd < 0) |
| 522 |
return watch_fd; |
| 523 |
|
| 524 |
r = inotify_add_watch_fd(inotify_fd, watch_fd, IN_ONLYDIR|IN_ATTRIB /* for mtime */); |
| 525 |
if (r < 0) |
| 526 |
return r; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
CLEANUP_ERASE(passphrase); |
| 531 |
|
| 532 |
/* If the caller didn't specify a TTY, then use the controlling tty, if we can. */ |
| 533 |
int ttyfd; |
| 534 |
if (req->tty_fd < 0) |
| 535 |
ttyfd = cttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC); |
| 536 |
else |
| 537 |
ttyfd = req->tty_fd; |
| 538 |
|
| 539 |
if (ttyfd >= 0) { |
| 540 |
if (tcgetattr(ttyfd, &old_termios) < 0) |
| 541 |
return -errno; |
| 542 |
|
| 543 |
if (FLAGS_SET(flags, ASK_PASSWORD_CONSOLE_COLOR)) |
| 544 |
use_color = dev_console_colors_enabled(); |
| 545 |
else |
| 546 |
use_color = colors_enabled(); |
| 547 |
|
| 548 |
if (use_color) |
| 549 |
(void) loop_write(ttyfd, ANSI_HIGHLIGHT, SIZE_MAX); |
| 550 |
|
| 551 |
(void) loop_write(ttyfd, message, SIZE_MAX); |
| 552 |
(void) loop_write(ttyfd, " ", 1); |
| 553 |
|
| 554 |
if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT) && !FLAGS_SET(flags, ASK_PASSWORD_ECHO)) { |
| 555 |
if (use_color) |
| 556 |
(void) loop_write(ttyfd, ansi_grey(), SIZE_MAX); |
| 557 |
|
| 558 |
(void) loop_write(ttyfd, PRESS_TAB, SIZE_MAX); |
| 559 |
press_tab_visible = true; |
| 560 |
} |
| 561 |
|
| 562 |
if (use_color) |
| 563 |
(void) loop_write(ttyfd, ANSI_NORMAL, SIZE_MAX); |
| 564 |
|
| 565 |
new_termios = old_termios; |
| 566 |
termios_disable_echo(&new_termios); |
| 567 |
|
| 568 |
r = RET_NERRNO(tcsetattr(ttyfd, TCSANOW, &new_termios)); |
| 569 |
if (r < 0) |
| 570 |
goto finish; |
| 571 |
|
| 572 |
reset_tty = true; |
| 573 |
} |
| 574 |
|
| 575 |
enum { |
| 576 |
POLL_TTY, |
| 577 |
POLL_TWO, |
| 578 |
POLL_THREE, |
| 579 |
_POLL_MAX, |
| 580 |
}; |
| 581 |
|
| 582 |
struct pollfd pollfd[_POLL_MAX] = { |
| 583 |
[POLL_TTY] = { |
| 584 |
.fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO, |
| 585 |
.events = POLLIN, |
| 586 |
}, |
| 587 |
}; |
| 588 |
size_t n_pollfd = POLL_TTY + 1, inotify_idx = SIZE_MAX, hup_fd_idx = SIZE_MAX; |
| 589 |
|
| 590 |
if (inotify_fd >= 0) |
| 591 |
pollfd[inotify_idx = n_pollfd++] = (struct pollfd) { |
| 592 |
.fd = inotify_fd, |
| 593 |
.events = POLLIN, |
| 594 |
}; |
| 595 |
if (req->hup_fd >= 0) |
| 596 |
pollfd[hup_fd_idx = n_pollfd++] = (struct pollfd) { |
| 597 |
.fd = req->hup_fd, |
| 598 |
.events = POLLHUP, |
| 599 |
}; |
| 600 |
|
| 601 |
assert(n_pollfd <= _POLL_MAX); |
| 602 |
|
| 603 |
for (;;) { |
| 604 |
_cleanup_(erase_char) char c; |
| 605 |
usec_t timeout; |
| 606 |
ssize_t n; |
| 607 |
|
| 608 |
if (req->until > 0) |
| 609 |
timeout = usec_sub_unsigned(req->until, now(CLOCK_MONOTONIC)); |
| 610 |
else |
| 611 |
timeout = USEC_INFINITY; |
| 612 |
|
| 613 |
if (req->flag_file) { |
| 614 |
r = RET_NERRNO(access(req->flag_file, F_OK)); |
| 615 |
if (r < 0) |
| 616 |
goto finish; |
| 617 |
} |
| 618 |
|
| 619 |
r = ppoll_usec(pollfd, n_pollfd, timeout); |
| 620 |
if (r == -EINTR) |
| 621 |
continue; |
| 622 |
if (r < 0) |
| 623 |
goto finish; |
| 624 |
if (r == 0) { |
| 625 |
r = -ETIME; |
| 626 |
goto finish; |
| 627 |
} |
| 628 |
|
| 629 |
if (req->hup_fd >= 0 && pollfd[hup_fd_idx].revents & POLLHUP) { |
| 630 |
r = -ECONNRESET; |
| 631 |
goto finish; |
| 632 |
} |
| 633 |
|
| 634 |
if (inotify_fd >= 0 && pollfd[inotify_idx].revents != 0 && keyring) { |
| 635 |
(void) flush_fd(inotify_fd); |
| 636 |
|
| 637 |
r = ask_password_keyring(req, flags, ret); |
| 638 |
if (r >= 0) { |
| 639 |
r = 0; |
| 640 |
goto finish; |
| 641 |
} else if (r != -ENOKEY) |
| 642 |
goto finish; |
| 643 |
} |
| 644 |
|
| 645 |
if (pollfd[POLL_TTY].revents == 0) |
| 646 |
continue; |
| 647 |
|
| 648 |
n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1); |
| 649 |
if (n < 0) { |
| 650 |
if (ERRNO_IS_TRANSIENT(errno)) |
| 651 |
continue; |
| 652 |
|
| 653 |
r = -errno; |
| 654 |
goto finish; |
| 655 |
|
| 656 |
} |
| 657 |
|
| 658 |
if (press_tab_visible) { |
| 659 |
assert(ttyfd >= 0); |
| 660 |
backspace_chars(ttyfd, strlen(PRESS_TAB)); |
| 661 |
press_tab_visible = false; |
| 662 |
} |
| 663 |
|
| 664 |
/* We treat EOF, newline and NUL byte all as valid end markers */ |
| 665 |
if (n == 0 || c == '\n' || c == 0) |
| 666 |
break; |
| 667 |
|
| 668 |
if (c == 4) { /* C-d also known as EOT */ |
| 669 |
if (ttyfd >= 0) |
| 670 |
(void) loop_write(ttyfd, SKIPPED, SIZE_MAX); |
| 671 |
|
| 672 |
goto skipped; |
| 673 |
} |
| 674 |
|
| 675 |
if (c == 21) { /* C-u */ |
| 676 |
|
| 677 |
if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT)) |
| 678 |
(void) backspace_string(ttyfd, passphrase); |
| 679 |
|
| 680 |
explicit_bzero_safe(passphrase, sizeof(passphrase)); |
| 681 |
p = codepoint = 0; |
| 682 |
|
| 683 |
} else if (IN_SET(c, '\b', 127)) { |
| 684 |
|
| 685 |
if (p > 0) { |
| 686 |
size_t q; |
| 687 |
|
| 688 |
if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT)) |
| 689 |
(void) backspace_chars(ttyfd, 1); |
| 690 |
|
| 691 |
/* Remove a full UTF-8 codepoint from the end. For that, figure out where the |
| 692 |
* last one begins */ |
| 693 |
q = 0; |
| 694 |
for (;;) { |
| 695 |
int z; |
| 696 |
|
| 697 |
z = utf8_encoded_valid_unichar(passphrase + q, SIZE_MAX); |
| 698 |
if (z <= 0) { |
| 699 |
q = SIZE_MAX; /* Invalid UTF8! */ |
| 700 |
break; |
| 701 |
} |
| 702 |
|
| 703 |
if (q + z >= p) /* This one brings us over the edge */ |
| 704 |
break; |
| 705 |
|
| 706 |
q += z; |
| 707 |
} |
| 708 |
|
| 709 |
p = codepoint = q == SIZE_MAX ? p - 1 : q; |
| 710 |
explicit_bzero_safe(passphrase + p, sizeof(passphrase) - p); |
| 711 |
|
| 712 |
} else if (!dirty && !FLAGS_SET(flags, ASK_PASSWORD_SILENT)) { |
| 713 |
|
| 714 |
flags |= ASK_PASSWORD_SILENT; |
| 715 |
|
| 716 |
/* There are two ways to enter silent mode. Either by pressing backspace as |
| 717 |
* first key (and only as first key), or ... */ |
| 718 |
|
| 719 |
if (ttyfd >= 0) |
| 720 |
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX); |
| 721 |
|
| 722 |
} else if (ttyfd >= 0) |
| 723 |
(void) loop_write(ttyfd, "\a", 1); |
| 724 |
|
| 725 |
} else if (c == '\t' && !FLAGS_SET(flags, ASK_PASSWORD_SILENT)) { |
| 726 |
|
| 727 |
(void) backspace_string(ttyfd, passphrase); |
| 728 |
flags |= ASK_PASSWORD_SILENT; |
| 729 |
|
| 730 |
/* ... or by pressing TAB at any time. */ |
| 731 |
|
| 732 |
if (ttyfd >= 0) |
| 733 |
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX); |
| 734 |
|
| 735 |
} else if (char_is_cc(c) || p >= sizeof(passphrase)-1) { |
| 736 |
/* Don't accept control chars or overly long passphrases */ |
| 737 |
if (ttyfd >= 0) |
| 738 |
(void) loop_write(ttyfd, "\a", 1); |
| 739 |
|
| 740 |
} else { |
| 741 |
passphrase[p++] = c; |
| 742 |
|
| 743 |
if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT) && ttyfd >= 0) { |
| 744 |
/* Check if we got a complete UTF-8 character now. If so, let's output one '*'. */ |
| 745 |
n = utf8_encoded_valid_unichar(passphrase + codepoint, SIZE_MAX); |
| 746 |
if (n >= 0) { |
| 747 |
if (FLAGS_SET(flags, ASK_PASSWORD_ECHO)) |
| 748 |
(void) loop_write(ttyfd, passphrase + codepoint, n); |
| 749 |
else |
| 750 |
(void) loop_write(ttyfd, |
| 751 |
glyph(GLYPH_BULLET), |
| 752 |
SIZE_MAX); |
| 753 |
codepoint = p; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
dirty = true; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
x = strndup(passphrase, p); |
| 762 |
if (!x) { |
| 763 |
r = -ENOMEM; |
| 764 |
goto finish; |
| 765 |
} |
| 766 |
|
| 767 |
r = strv_consume(&l, x); |
| 768 |
if (r < 0) |
| 769 |
goto finish; |
| 770 |
|
| 771 |
skipped: |
| 772 |
if (strv_isempty(l)) |
| 773 |
r = log_debug_errno(SYNTHETIC_ERRNO(ECANCELED), "Password query was cancelled."); |
| 774 |
else { |
| 775 |
if (keyring) |
| 776 |
(void) add_to_keyring_and_log(keyring, flags, l); |
| 777 |
|
| 778 |
*ret = TAKE_PTR(l); |
| 779 |
r = 0; |
| 780 |
} |
| 781 |
|
| 782 |
finish: |
| 783 |
if (ttyfd >= 0 && reset_tty) { |
| 784 |
(void) loop_write(ttyfd, "\n", 1); |
| 785 |
(void) tcsetattr(ttyfd, TCSANOW, &old_termios); |
| 786 |
} |
| 787 |
|
| 788 |
return r; |
| 789 |
} |
| 790 |
|
| 791 |
static int create_socket(const char *askpwdir, char **ret) { |
| 792 |
_cleanup_free_ char *path = NULL; |
| 793 |
union sockaddr_union sa; |
| 794 |
socklen_t sa_len; |
| 795 |
_cleanup_close_ int fd = -EBADF; |
| 796 |
int r; |
| 797 |
|
| 798 |
assert(askpwdir); |
| 799 |
assert(ret); |
| 800 |
|
| 801 |
fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 802 |
if (fd < 0) |
| 803 |
return -errno; |
| 804 |
|
| 805 |
if (asprintf(&path, "%s/sck.%" PRIx64, askpwdir, random_u64()) < 0) |
| 806 |
return -ENOMEM; |
| 807 |
|
| 808 |
r = sockaddr_un_set_path(&sa.un, path); |
| 809 |
if (r < 0) |
| 810 |
return r; |
| 811 |
sa_len = r; |
| 812 |
|
| 813 |
WITH_UMASK(0177) |
| 814 |
if (bind(fd, &sa.sa, sa_len) < 0) |
| 815 |
return -errno; |
| 816 |
|
| 817 |
r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true); |
| 818 |
if (r < 0) |
| 819 |
return r; |
| 820 |
|
| 821 |
(void) setsockopt_int(fd, SOL_SOCKET, SO_PASSRIGHTS, false); |
| 822 |
|
| 823 |
*ret = TAKE_PTR(path); |
| 824 |
return TAKE_FD(fd); |
| 825 |
} |
| 826 |
|
| 827 |
int ask_password_agent( |
| 828 |
const AskPasswordRequest *req, |
| 829 |
AskPasswordFlags flags, |
| 830 |
char ***ret) { |
| 831 |
|
| 832 |
_cleanup_close_ int socket_fd = -EBADF, signal_fd = -EBADF, inotify_fd = -EBADF, dfd = -EBADF; |
| 833 |
_cleanup_(unlink_and_freep) char *socket_name = NULL; |
| 834 |
int r; |
| 835 |
|
| 836 |
assert(req); |
| 837 |
assert(ret); |
| 838 |
|
| 839 |
if (FLAGS_SET(flags, ASK_PASSWORD_HEADLESS)) |
| 840 |
return -ENOEXEC; |
| 841 |
|
| 842 |
if (FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT)) |
| 843 |
return -EUNATCH; |
| 844 |
|
| 845 |
/* We don't support the flag file concept for now when querying via the agent logic */ |
| 846 |
if (req->flag_file) |
| 847 |
return -EOPNOTSUPP; |
| 848 |
|
| 849 |
_cleanup_free_ char *askpwdir = NULL; |
| 850 |
r = get_ask_password_directory_for_flags(flags, &askpwdir); |
| 851 |
if (r < 0) |
| 852 |
return r; |
| 853 |
if (r == 0) |
| 854 |
return -ENXIO; |
| 855 |
|
| 856 |
dfd = open_mkdir(askpwdir, O_CLOEXEC, 0755); |
| 857 |
if (dfd < 0) |
| 858 |
return log_debug_errno(dfd, "Failed to open directory '%s': %m", askpwdir); |
| 859 |
|
| 860 |
if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && req->keyring) { |
| 861 |
r = ask_password_keyring(req, flags, ret); |
| 862 |
if (r != -ENOKEY) |
| 863 |
return r; |
| 864 |
|
| 865 |
inotify_fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); |
| 866 |
if (inotify_fd < 0) |
| 867 |
return -errno; |
| 868 |
|
| 869 |
r = inotify_add_watch_fd(inotify_fd, dfd, IN_ONLYDIR|IN_ATTRIB /* for mtime */); |
| 870 |
if (r < 0) |
| 871 |
return r; |
| 872 |
} |
| 873 |
|
| 874 |
sigset_t mask, oldmask; |
| 875 |
_unused_ _cleanup_(block_signals_reset) sigset_t *saved_ssp = NULL; |
| 876 |
|
| 877 |
assert_se(sigemptyset(&mask) >= 0); |
| 878 |
assert_se(sigset_add_many(&mask, SIGINT, SIGTERM) >= 0); |
| 879 |
assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) >= 0); |
| 880 |
|
| 881 |
saved_ssp = &oldmask; |
| 882 |
|
| 883 |
_cleanup_free_ char *fname = NULL, *final = NULL; |
| 884 |
_cleanup_fclose_ FILE *f = NULL; |
| 885 |
|
| 886 |
if (asprintf(&final, "ask.%" PRIu64, random_u64()) < 0) |
| 887 |
return -ENOMEM; |
| 888 |
|
| 889 |
r = fopen_temporary_at(dfd, final, &f, &fname); |
| 890 |
if (r < 0) |
| 891 |
return r; |
| 892 |
|
| 893 |
CLEANUP_TMPFILE_AT(dfd, fname); |
| 894 |
|
| 895 |
signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); |
| 896 |
if (signal_fd < 0) |
| 897 |
return -errno; |
| 898 |
|
| 899 |
socket_fd = create_socket(askpwdir, &socket_name); |
| 900 |
if (socket_fd < 0) |
| 901 |
return socket_fd; |
| 902 |
|
| 903 |
fprintf(f, |
| 904 |
"[Ask]\n" |
| 905 |
"PID="PID_FMT"\n" |
| 906 |
"Socket=%s\n" |
| 907 |
"AcceptCached=%i\n" |
| 908 |
"Echo=%i\n" |
| 909 |
"NotAfter="USEC_FMT"\n" |
| 910 |
"Silent=%i\n", |
| 911 |
getpid_cached(), |
| 912 |
socket_name, |
| 913 |
FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED), |
| 914 |
FLAGS_SET(flags, ASK_PASSWORD_ECHO), |
| 915 |
req->until, |
| 916 |
FLAGS_SET(flags, ASK_PASSWORD_SILENT)); |
| 917 |
|
| 918 |
if (req->message) |
| 919 |
fprintf(f, "Message=%s\n", req->message); |
| 920 |
|
| 921 |
if (req->icon) |
| 922 |
fprintf(f, "Icon=%s\n", req->icon); |
| 923 |
|
| 924 |
if (req->id) |
| 925 |
fprintf(f, "Id=%s\n", req->id); |
| 926 |
|
| 927 |
if (fchmod(fileno(f), 0644) < 0) |
| 928 |
return -errno; |
| 929 |
|
| 930 |
r = fflush_and_check(f); |
| 931 |
if (r < 0) |
| 932 |
return r; |
| 933 |
|
| 934 |
/* Paranoia: never clobber an ongoing request */ |
| 935 |
r = rename_noreplace(dfd, fname, dfd, final); |
| 936 |
if (r < 0) |
| 937 |
return r; |
| 938 |
|
| 939 |
free_and_replace(fname, final); |
| 940 |
|
| 941 |
enum { |
| 942 |
POLL_SOCKET, |
| 943 |
POLL_SIGNAL, |
| 944 |
POLL_THREE, |
| 945 |
POLL_FOUR, |
| 946 |
_POLL_MAX |
| 947 |
}; |
| 948 |
|
| 949 |
struct pollfd pollfd[_POLL_MAX] = { |
| 950 |
[POLL_SOCKET] = { .fd = socket_fd, .events = POLLIN }, |
| 951 |
[POLL_SIGNAL] = { .fd = signal_fd, .events = POLLIN }, |
| 952 |
}; |
| 953 |
size_t n_pollfd = POLL_SIGNAL + 1, inotify_idx = SIZE_MAX, hup_fd_idx = SIZE_MAX; |
| 954 |
|
| 955 |
if (inotify_fd >= 0) |
| 956 |
pollfd[inotify_idx = n_pollfd++] = (struct pollfd) { |
| 957 |
.fd = inotify_fd, |
| 958 |
.events = POLLIN, |
| 959 |
}; |
| 960 |
if (req->hup_fd >= 0) |
| 961 |
pollfd[hup_fd_idx = n_pollfd++] = (struct pollfd) { |
| 962 |
.fd = req->hup_fd, |
| 963 |
.events = POLLHUP, |
| 964 |
}; |
| 965 |
|
| 966 |
assert(n_pollfd <= _POLL_MAX); |
| 967 |
|
| 968 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 969 |
|
| 970 |
for (;;) { |
| 971 |
usec_t timeout; |
| 972 |
|
| 973 |
if (req->until > 0) |
| 974 |
timeout = usec_sub_unsigned(req->until, now(CLOCK_MONOTONIC)); |
| 975 |
else |
| 976 |
timeout = USEC_INFINITY; |
| 977 |
|
| 978 |
r = ppoll_usec(pollfd, n_pollfd, timeout); |
| 979 |
if (r == -EINTR) |
| 980 |
continue; |
| 981 |
if (r < 0) |
| 982 |
return r; |
| 983 |
if (r == 0) |
| 984 |
return -ETIME; |
| 985 |
|
| 986 |
if (pollfd[POLL_SIGNAL].revents & POLLIN) |
| 987 |
return -EINTR; |
| 988 |
|
| 989 |
if (req->hup_fd >= 0 && pollfd[hup_fd_idx].revents & POLLHUP) |
| 990 |
return -ECONNRESET; |
| 991 |
|
| 992 |
if (inotify_fd >= 0 && pollfd[inotify_idx].revents != 0) { |
| 993 |
(void) flush_fd(inotify_fd); |
| 994 |
|
| 995 |
if (req->keyring) { |
| 996 |
r = ask_password_keyring(req, flags, ret); |
| 997 |
if (r != -ENOKEY) |
| 998 |
return r; |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
if (pollfd[POLL_SOCKET].revents == 0) |
| 1003 |
continue; |
| 1004 |
if (pollfd[POLL_SOCKET].revents != POLLIN) |
| 1005 |
return -EIO; |
| 1006 |
|
| 1007 |
char passphrase[LINE_MAX+1]; |
| 1008 |
struct iovec iovec = IOVEC_MAKE(passphrase, sizeof(passphrase)); |
| 1009 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control; |
| 1010 |
struct msghdr msghdr = { |
| 1011 |
.msg_iov = &iovec, |
| 1012 |
.msg_iovlen = 1, |
| 1013 |
.msg_control = &control, |
| 1014 |
.msg_controllen = sizeof(control), |
| 1015 |
}; |
| 1016 |
|
| 1017 |
ssize_t n = recvmsg_safe(socket_fd, &msghdr, 0); |
| 1018 |
if (ERRNO_IS_NEG_TRANSIENT(n)) |
| 1019 |
continue; |
| 1020 |
if (n == -ECHRNG) { |
| 1021 |
log_debug_errno(n, "Got message with truncated control data (unexpected fds sent?), ignoring."); |
| 1022 |
continue; |
| 1023 |
} |
| 1024 |
if (n == -EXFULL) { |
| 1025 |
log_debug_errno(n, "Got message with truncated payload data, ignoring."); |
| 1026 |
continue; |
| 1027 |
} |
| 1028 |
if (n < 0) |
| 1029 |
return n; |
| 1030 |
|
| 1031 |
CLEANUP_ERASE(passphrase); |
| 1032 |
|
| 1033 |
cmsg_close_all(&msghdr); |
| 1034 |
|
| 1035 |
if (n == 0) { |
| 1036 |
log_debug("Got empty reply from ask-password agent, ignoring."); |
| 1037 |
continue; |
| 1038 |
} |
| 1039 |
|
| 1040 |
struct ucred *ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred); |
| 1041 |
if (!ucred) { |
| 1042 |
log_debug("Received message without credentials, ignoring."); |
| 1043 |
continue; |
| 1044 |
} |
| 1045 |
if (ucred->uid != getuid() && ucred->uid != 0) { |
| 1046 |
log_debug("Got password response from bad user, ignoring."); |
| 1047 |
continue; |
| 1048 |
} |
| 1049 |
|
| 1050 |
if (passphrase[0] == '-') |
| 1051 |
return -ECANCELED; |
| 1052 |
|
| 1053 |
if (passphrase[0] == '+') { |
| 1054 |
/* An empty message refers to the empty password */ |
| 1055 |
if (n == 1) |
| 1056 |
l = strv_new(""); |
| 1057 |
else |
| 1058 |
l = strv_parse_nulstr(passphrase+1, n-1); |
| 1059 |
if (!l) |
| 1060 |
return -ENOMEM; |
| 1061 |
|
| 1062 |
if (!strv_isempty(l)) |
| 1063 |
break; |
| 1064 |
|
| 1065 |
l = strv_free(l); |
| 1066 |
} |
| 1067 |
|
| 1068 |
log_debug("Got invalid response from ask-password agent, ignoring."); |
| 1069 |
} |
| 1070 |
|
| 1071 |
if (req->keyring) |
| 1072 |
(void) add_to_keyring_and_log(req->keyring, flags, l); |
| 1073 |
|
| 1074 |
*ret = TAKE_PTR(l); |
| 1075 |
return 0; |
| 1076 |
} |
| 1077 |
|
| 1078 |
static int ask_password_credential(const AskPasswordRequest *req, AskPasswordFlags flags, char ***ret) { |
| 1079 |
_cleanup_(erase_and_freep) char *buffer = NULL; |
| 1080 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 1081 |
size_t size; |
| 1082 |
int r; |
| 1083 |
|
| 1084 |
assert(req); |
| 1085 |
assert(req->credential); |
| 1086 |
assert(ret); |
| 1087 |
|
| 1088 |
r = read_credential(req->credential, (void**) &buffer, &size); |
| 1089 |
if (IN_SET(r, -ENXIO, -ENOENT)) /* No credentials passed or this credential not defined? */ |
| 1090 |
return -ENOKEY; |
| 1091 |
if (r < 0) |
| 1092 |
return r; |
| 1093 |
|
| 1094 |
l = strv_parse_nulstr(buffer, size); |
| 1095 |
if (!l) |
| 1096 |
return -ENOMEM; |
| 1097 |
|
| 1098 |
if (strv_isempty(l)) |
| 1099 |
return log_debug_errno(SYNTHETIC_ERRNO(ENOKEY), "Found an empty password in credential."); |
| 1100 |
|
| 1101 |
*ret = TAKE_PTR(l); |
| 1102 |
return 0; |
| 1103 |
} |
| 1104 |
|
| 1105 |
int ask_password_auto( |
| 1106 |
const AskPasswordRequest *req, |
| 1107 |
AskPasswordFlags flags, |
| 1108 |
char ***ret) { |
| 1109 |
|
| 1110 |
int r; |
| 1111 |
|
| 1112 |
assert(req); |
| 1113 |
assert(ret); |
| 1114 |
|
| 1115 |
/* Returns the following well-known errors: |
| 1116 |
* |
| 1117 |
* -ETIME → a timeout was specified and hit |
| 1118 |
* -EUNATCH → couldn't ask interactively and no cached password available either |
| 1119 |
* -ENOENT → the specified flag file disappeared |
| 1120 |
* -ECANCELED → the user explicitly cancelled the request |
| 1121 |
* -EINTR → SIGINT/SIGTERM where received during the query |
| 1122 |
* -ENOEXEC → headless mode was requested but no password could be acquired non-interactively |
| 1123 |
* -ECONNRESET → a POLLHUP has been seen on the specified hup_fd |
| 1124 |
*/ |
| 1125 |
|
| 1126 |
if (!FLAGS_SET(flags, ASK_PASSWORD_NO_CREDENTIAL) && req->credential) { |
| 1127 |
r = ask_password_credential(req, flags, ret); |
| 1128 |
if (r != -ENOKEY) |
| 1129 |
return r; |
| 1130 |
} |
| 1131 |
|
| 1132 |
if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED) && |
| 1133 |
req->keyring && |
| 1134 |
(FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) || !isatty_safe(STDIN_FILENO)) && |
| 1135 |
FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT)) { |
| 1136 |
r = ask_password_keyring(req, flags, ret); |
| 1137 |
if (r != -ENOKEY) |
| 1138 |
return r; |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (!FLAGS_SET(flags, ASK_PASSWORD_NO_TTY) && isatty_safe(STDIN_FILENO)) |
| 1142 |
return ask_password_tty(req, flags, ret); |
| 1143 |
|
| 1144 |
if (!FLAGS_SET(flags, ASK_PASSWORD_NO_AGENT)) |
| 1145 |
return ask_password_agent(req, flags, ret); |
| 1146 |
|
| 1147 |
return -EUNATCH; |
| 1148 |
} |
| 1149 |
|
| 1150 |
int acquire_user_ask_password_directory(char **ret) { |
| 1151 |
int r; |
| 1152 |
|
| 1153 |
r = xdg_user_runtime_dir("systemd/ask-password", ret); |
| 1154 |
if (r == -ENXIO) { |
| 1155 |
if (ret) |
| 1156 |
*ret = NULL; |
| 1157 |
return 0; |
| 1158 |
} |
| 1159 |
if (r < 0) |
| 1160 |
return r; |
| 1161 |
|
| 1162 |
return 1; |
| 1163 |
} |