| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-varlink.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "ask-password-api.h" |
| 9 |
#include "build.h" |
| 10 |
#include "bus-polkit.h" |
| 11 |
#include "constants.h" |
| 12 |
#include "format-table.h" |
| 13 |
#include "hashmap.h" |
| 14 |
#include "json-util.h" |
| 15 |
#include "log.h" |
| 16 |
#include "main-func.h" |
| 17 |
#include "options.h" |
| 18 |
#include "parse-argument.h" |
| 19 |
#include "pretty-print.h" |
| 20 |
#include "string-table.h" |
| 21 |
#include "string-util.h" |
| 22 |
#include "strv.h" |
| 23 |
#include "time-util.h" |
| 24 |
#include "varlink-io.systemd.AskPassword.h" |
| 25 |
#include "varlink-util.h" |
| 26 |
|
| 27 |
static const char *arg_icon = NULL; |
| 28 |
static const char *arg_id = NULL; /* identifier for 'ask-password' protocol */ |
| 29 |
static const char *arg_key_name = NULL; /* name in kernel keyring */ |
| 30 |
static const char *arg_credential_name = NULL; /* name in $CREDENTIALS_DIRECTORY directory */ |
| 31 |
static char *arg_message = NULL; |
| 32 |
static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC; |
| 33 |
static bool arg_multiple = false; |
| 34 |
static bool arg_no_output = false; |
| 35 |
static AskPasswordFlags arg_flags = ASK_PASSWORD_PUSH_CACHE; |
| 36 |
static bool arg_newline = true; |
| 37 |
static bool arg_varlink = false; |
| 38 |
|
| 39 |
STATIC_DESTRUCTOR_REGISTER(arg_message, freep); |
| 40 |
|
| 41 |
static int help(void) { |
| 42 |
_cleanup_free_ char *link = NULL; |
| 43 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 44 |
int r; |
| 45 |
|
| 46 |
r = terminal_urlify_man("systemd-ask-password", "1", &link); |
| 47 |
if (r < 0) |
| 48 |
return log_oom(); |
| 49 |
|
| 50 |
r = option_parser_get_help_table(&options); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
printf("%s [OPTIONS...] MESSAGE\n" |
| 55 |
"\n%sQuery the user for a passphrase, via the TTY or a UI agent.%s\n" |
| 56 |
"\nOptions:\n", |
| 57 |
program_invocation_short_name, |
| 58 |
ansi_highlight(), |
| 59 |
ansi_normal()); |
| 60 |
r = table_print_or_warn(options); |
| 61 |
if (r < 0) |
| 62 |
return r; |
| 63 |
|
| 64 |
printf("\nSee the %s for details.\n", link); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
static int parse_argv(int argc, char *argv[]) { |
| 69 |
|
| 70 |
const char *emoji = NULL; |
| 71 |
int r; |
| 72 |
|
| 73 |
assert(argc >= 0); |
| 74 |
assert(argv); |
| 75 |
|
| 76 |
OptionParser opts = { argc, argv }; |
| 77 |
|
| 78 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 79 |
switch (c) { |
| 80 |
OPTION_COMMON_HELP: |
| 81 |
return help(); |
| 82 |
|
| 83 |
OPTION_COMMON_VERSION: |
| 84 |
return version(); |
| 85 |
|
| 86 |
OPTION_LONG("icon", "NAME", "Icon name"): |
| 87 |
arg_icon = opts.arg; |
| 88 |
break; |
| 89 |
|
| 90 |
OPTION_LONG("timeout", "SEC", "Timeout in seconds"): |
| 91 |
r = parse_sec(opts.arg, &arg_timeout); |
| 92 |
if (r < 0) |
| 93 |
return log_error_errno(r, "Failed to parse --timeout= parameter: %s", opts.arg); |
| 94 |
break; |
| 95 |
|
| 96 |
/* Note the asymmetry: the long option --echo= allows an optional argument, |
| 97 |
* the short option does not. */ |
| 98 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "echo", "yes|no|masked", |
| 99 |
"Control whether to show password while typing"): {} |
| 100 |
OPTION('e', "echo", NULL, "Equivalent to --echo=yes"): |
| 101 |
if (!opts.arg) { |
| 102 |
/* Short option -e is used, or no argument to long option --echo= */ |
| 103 |
arg_flags |= ASK_PASSWORD_ECHO; |
| 104 |
arg_flags &= ~ASK_PASSWORD_SILENT; |
| 105 |
} else if (isempty(opts.arg) || streq(opts.arg, "masked")) |
| 106 |
/* Empty argument or explicit string "masked" for default behaviour. */ |
| 107 |
arg_flags &= ~(ASK_PASSWORD_ECHO|ASK_PASSWORD_SILENT); |
| 108 |
else { |
| 109 |
r = parse_boolean_argument("--echo=", opts.arg, NULL); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
|
| 113 |
SET_FLAG(arg_flags, ASK_PASSWORD_ECHO, r); |
| 114 |
SET_FLAG(arg_flags, ASK_PASSWORD_SILENT, !r); |
| 115 |
} |
| 116 |
break; |
| 117 |
|
| 118 |
OPTION_LONG("emoji", "yes|no|auto", "Show a lock and key emoji"): |
| 119 |
emoji = opts.arg; |
| 120 |
break; |
| 121 |
|
| 122 |
OPTION_LONG("no-tty", NULL, "Ask question via agent even on TTY"): |
| 123 |
arg_flags |= ASK_PASSWORD_NO_TTY; |
| 124 |
break; |
| 125 |
|
| 126 |
OPTION_LONG("accept-cached", NULL, "Accept cached passwords"): |
| 127 |
arg_flags |= ASK_PASSWORD_ACCEPT_CACHED; |
| 128 |
break; |
| 129 |
|
| 130 |
OPTION_LONG("multiple", NULL, "List multiple passwords if available"): |
| 131 |
arg_multiple = true; |
| 132 |
break; |
| 133 |
|
| 134 |
OPTION_LONG("id", "ID", "Query identifier (e.g. \"cryptsetup:/dev/sda5\")"): |
| 135 |
arg_id = opts.arg; |
| 136 |
break; |
| 137 |
|
| 138 |
OPTION_LONG("keyname", "NAME", "Kernel key name for caching passwords"): |
| 139 |
arg_key_name = opts.arg; |
| 140 |
break; |
| 141 |
|
| 142 |
OPTION_LONG("no-output", NULL, "Do not print password to standard output"): |
| 143 |
arg_no_output = true; |
| 144 |
break; |
| 145 |
|
| 146 |
OPTION_LONG("credential", "NAME", |
| 147 |
"Credential name for ImportCredential=, LoadCredential= or SetCredential= credentials"): |
| 148 |
arg_credential_name = opts.arg; |
| 149 |
break; |
| 150 |
|
| 151 |
OPTION_LONG("user", NULL, "Ask only our own user's agents"): |
| 152 |
arg_flags |= ASK_PASSWORD_USER; |
| 153 |
break; |
| 154 |
|
| 155 |
OPTION_LONG("system", NULL, "Ask agents of the system and of all users"): |
| 156 |
arg_flags &= ~ASK_PASSWORD_USER; |
| 157 |
break; |
| 158 |
|
| 159 |
OPTION_SHORT('n', NULL, "Do not suffix password written to standard output with newline"): |
| 160 |
arg_newline = false; |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
if (isempty(emoji) || streq(emoji, "auto")) |
| 165 |
SET_FLAG(arg_flags, ASK_PASSWORD_HIDE_EMOJI, FLAGS_SET(arg_flags, ASK_PASSWORD_ECHO)); |
| 166 |
else { |
| 167 |
r = parse_boolean_argument("--emoji=", emoji, NULL); |
| 168 |
if (r < 0) |
| 169 |
return r; |
| 170 |
|
| 171 |
SET_FLAG(arg_flags, ASK_PASSWORD_HIDE_EMOJI, !r); |
| 172 |
} |
| 173 |
|
| 174 |
char **args = option_parser_get_args(&opts); |
| 175 |
|
| 176 |
if (!strv_isempty(args)) { |
| 177 |
arg_message = strv_join(args, " "); |
| 178 |
if (!arg_message) |
| 179 |
return log_oom(); |
| 180 |
} else if (FLAGS_SET(arg_flags, ASK_PASSWORD_ECHO)) { |
| 181 |
/* By default ask_password_auto() will query with the string "Password: ", which is not right |
| 182 |
* when full echo is on, since then it's unlikely a password. Let's hence default to a less |
| 183 |
* confusing string in that case. */ |
| 184 |
|
| 185 |
arg_message = strdup("Input:"); |
| 186 |
if (!arg_message) |
| 187 |
return log_oom(); |
| 188 |
} |
| 189 |
|
| 190 |
r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT); |
| 191 |
if (r < 0) |
| 192 |
return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m"); |
| 193 |
if (r > 0) |
| 194 |
arg_varlink = true; |
| 195 |
|
| 196 |
return 1; |
| 197 |
} |
| 198 |
|
| 199 |
typedef enum EchoMode { |
| 200 |
ECHO_OFF, |
| 201 |
ECHO_ON, |
| 202 |
ECHO_MASKED, |
| 203 |
_ECHO_MODE_MAX, |
| 204 |
_ECHO_MODE_INVALID = -EINVAL, |
| 205 |
} EchoMode; |
| 206 |
|
| 207 |
static const char* echo_mode_table[_ECHO_MODE_MAX] = { |
| 208 |
[ECHO_OFF] = "off", |
| 209 |
[ECHO_ON] = "on", |
| 210 |
[ECHO_MASKED] = "masked", |
| 211 |
}; |
| 212 |
|
| 213 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(echo_mode, EchoMode, ECHO_ON); |
| 214 |
|
| 215 |
static JSON_DISPATCH_ENUM_DEFINE(dispatch_echo_mode, EchoMode, echo_mode_from_string); |
| 216 |
|
| 217 |
typedef struct MethodAskParameters { |
| 218 |
const char *message; |
| 219 |
const char *keyring; |
| 220 |
const char *icon; |
| 221 |
const char *id; |
| 222 |
uint64_t timeout_usec; |
| 223 |
uint64_t until_usec; |
| 224 |
int accept_cached; |
| 225 |
int push_cache; |
| 226 |
EchoMode echo_mode; |
| 227 |
} MethodAskParameters; |
| 228 |
|
| 229 |
static int vl_method_ask(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 230 |
|
| 231 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 232 |
{ "message", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodAskParameters, message), SD_JSON_STRICT }, |
| 233 |
{ "keyname", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodAskParameters, keyring), SD_JSON_STRICT }, |
| 234 |
{ "icon", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodAskParameters, icon), SD_JSON_STRICT }, |
| 235 |
{ "id", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(MethodAskParameters, id), SD_JSON_STRICT }, |
| 236 |
{ "timeoutUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(MethodAskParameters, timeout_usec), 0 }, |
| 237 |
{ "untilUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(MethodAskParameters, until_usec), 0 }, |
| 238 |
{ "acceptCached", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(MethodAskParameters, accept_cached), 0 }, |
| 239 |
{ "pushCache", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(MethodAskParameters, push_cache) , 0 }, |
| 240 |
{ "echo", SD_JSON_VARIANT_STRING, dispatch_echo_mode, offsetof(MethodAskParameters, echo_mode), 0 }, |
| 241 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 242 |
{} |
| 243 |
}; |
| 244 |
|
| 245 |
Hashmap **polkit_registry = ASSERT_PTR(userdata); |
| 246 |
MethodAskParameters p = { |
| 247 |
.timeout_usec = DEFAULT_TIMEOUT_USEC, |
| 248 |
.until_usec = UINT64_MAX, |
| 249 |
.accept_cached = -1, |
| 250 |
.push_cache = -1, |
| 251 |
.echo_mode = _ECHO_MODE_INVALID, |
| 252 |
}; |
| 253 |
/* Local copy so that parameters passed are per-invocation */ |
| 254 |
AskPasswordFlags ask_flags = arg_flags; |
| 255 |
int r; |
| 256 |
|
| 257 |
assert(link); |
| 258 |
|
| 259 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 260 |
if (r != 0) |
| 261 |
return r; |
| 262 |
|
| 263 |
r = varlink_verify_polkit_async_full( |
| 264 |
link, |
| 265 |
/* bus= */ NULL, |
| 266 |
"io.systemd.ask-password.ask", |
| 267 |
/* details= */ NULL, |
| 268 |
/* good_user= */ FLAGS_SET(arg_flags, ASK_PASSWORD_USER) ? getuid() : UID_INVALID, |
| 269 |
/* flags= */ 0, |
| 270 |
polkit_registry); |
| 271 |
if (r <= 0) |
| 272 |
return r; |
| 273 |
|
| 274 |
AskPasswordRequest req = { |
| 275 |
.tty_fd = -EBADF, |
| 276 |
.message = p.message ?: arg_message, |
| 277 |
.icon = p.icon ?: arg_icon, |
| 278 |
.id = p.id ?: arg_id, |
| 279 |
.keyring = p.keyring ?: arg_key_name, |
| 280 |
.credential = arg_credential_name, |
| 281 |
.hup_fd = sd_varlink_get_input_fd(link), |
| 282 |
}; |
| 283 |
|
| 284 |
/* Specifying the absolute or relative timeout as zero means: do not ask interactively, only check |
| 285 |
* cache, hence leave the field at zero in that case. Otherwise we take the minimum of both times. */ |
| 286 |
if (p.timeout_usec != 0 && p.until_usec != 0) |
| 287 |
req.until = MIN(usec_add(now(CLOCK_MONOTONIC), p.timeout_usec), p.until_usec); |
| 288 |
|
| 289 |
/* If the timeout is set to zero, don't ask agents, just stick to cache */ |
| 290 |
SET_FLAG(ask_flags, ASK_PASSWORD_NO_AGENT, req.until == 0); |
| 291 |
|
| 292 |
if (p.accept_cached >= 0) |
| 293 |
SET_FLAG(ask_flags, ASK_PASSWORD_ACCEPT_CACHED, p.accept_cached); |
| 294 |
|
| 295 |
if (p.push_cache >= 0) |
| 296 |
SET_FLAG(ask_flags, ASK_PASSWORD_PUSH_CACHE, p.push_cache); |
| 297 |
|
| 298 |
if (p.echo_mode >= 0) { |
| 299 |
SET_FLAG(ask_flags, ASK_PASSWORD_ECHO, p.echo_mode == ECHO_ON); |
| 300 |
SET_FLAG(ask_flags, ASK_PASSWORD_SILENT, p.echo_mode == ECHO_OFF); |
| 301 |
} |
| 302 |
|
| 303 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 304 |
r = ask_password_auto(&req, ask_flags, &l); |
| 305 |
if (r == -EUNATCH) |
| 306 |
return sd_varlink_error(link, "io.systemd.AskPassword.NoPasswordAvailable", NULL); |
| 307 |
if (r == -ETIME) |
| 308 |
return sd_varlink_error(link, "io.systemd.AskPassword.TimeoutReached", NULL); |
| 309 |
if (r == -ECONNRESET) { /* POLLHUP on the varlink fd we passed in via .hup_fd */ |
| 310 |
sd_varlink_close(link); |
| 311 |
return 1; |
| 312 |
} |
| 313 |
if (r < 0) |
| 314 |
return r; |
| 315 |
|
| 316 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *vl = NULL; |
| 317 |
r = sd_json_variant_new_array_strv(&vl, l); |
| 318 |
if (r < 0) |
| 319 |
return r; |
| 320 |
|
| 321 |
sd_json_variant_sensitive(vl); |
| 322 |
|
| 323 |
return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_VARIANT("passwords", vl)); |
| 324 |
} |
| 325 |
|
| 326 |
static int vl_server(void) { |
| 327 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL; |
| 328 |
_cleanup_hashmap_free_ Hashmap *polkit_registry = NULL; |
| 329 |
int r; |
| 330 |
|
| 331 |
r = varlink_server_new(&varlink_server, SD_VARLINK_SERVER_INHERIT_USERDATA, /* userdata= */ &polkit_registry); |
| 332 |
if (r < 0) |
| 333 |
return log_error_errno(r, "Failed to allocate Varlink server: %m"); |
| 334 |
|
| 335 |
r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_AskPassword); |
| 336 |
if (r < 0) |
| 337 |
return log_error_errno(r, "Failed to add Varlink interface: %m"); |
| 338 |
|
| 339 |
r = sd_varlink_server_bind_method(varlink_server, "io.systemd.AskPassword.Ask", vl_method_ask); |
| 340 |
if (r < 0) |
| 341 |
return log_error_errno(r, "Failed to bind Varlink method: %m"); |
| 342 |
|
| 343 |
r = sd_varlink_server_loop_auto(varlink_server); |
| 344 |
if (r < 0) |
| 345 |
return log_error_errno(r, "Failed to run Varlink event loop: %m"); |
| 346 |
|
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
static int run(int argc, char *argv[]) { |
| 351 |
_cleanup_strv_free_erase_ char **l = NULL; |
| 352 |
usec_t timeout; |
| 353 |
int r; |
| 354 |
|
| 355 |
log_setup(); |
| 356 |
|
| 357 |
/* Unprivileged? Then imply ASK_PASSWORD_USER by default */ |
| 358 |
SET_FLAG(arg_flags, ASK_PASSWORD_USER, geteuid() != 0); |
| 359 |
|
| 360 |
r = parse_argv(argc, argv); |
| 361 |
if (r <= 0) |
| 362 |
return r; |
| 363 |
|
| 364 |
if (arg_varlink) |
| 365 |
return vl_server(); /* Invocation as Varlink service */ |
| 366 |
|
| 367 |
timeout = arg_timeout > 0 ? usec_add(now(CLOCK_MONOTONIC), arg_timeout) : 0; |
| 368 |
|
| 369 |
AskPasswordRequest req = { |
| 370 |
.tty_fd = -EBADF, |
| 371 |
.message = arg_message, |
| 372 |
.icon = arg_icon, |
| 373 |
.id = arg_id, |
| 374 |
.keyring = arg_key_name, |
| 375 |
.credential = arg_credential_name ?: "password", |
| 376 |
.until = timeout, |
| 377 |
.hup_fd = -EBADF, |
| 378 |
}; |
| 379 |
|
| 380 |
r = ask_password_auto(&req, arg_flags, &l); |
| 381 |
if (r < 0) |
| 382 |
return log_error_errno(r, "Failed to query password: %m"); |
| 383 |
|
| 384 |
STRV_FOREACH(p, l) { |
| 385 |
if (!arg_no_output) { |
| 386 |
if (arg_newline) |
| 387 |
puts(*p); |
| 388 |
else |
| 389 |
fputs(*p, stdout); |
| 390 |
} |
| 391 |
|
| 392 |
fflush(stdout); |
| 393 |
|
| 394 |
if (!arg_multiple) |
| 395 |
break; |
| 396 |
} |
| 397 |
|
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
DEFINE_MAIN_FUNCTION(run); |