1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "sd-varlink.h"
8 #include "alloc-util.h"
9 #include "ask-password-api.h"
11 #include "bus-polkit.h"
12 #include "constants.h"
14 #include "json-util.h"
16 #include "main-func.h"
17 #include "parse-argument.h"
18 #include "pretty-print.h"
19 #include "string-table.h"
20 #include "string-util.h"
22 #include "time-util.h"
23 #include "varlink-io.systemd.AskPassword.h"
24 #include "varlink-util.h"
26 static const char *arg_icon
= NULL
;
27 static const char *arg_id
= NULL
; /* identifier for 'ask-password' protocol */
28 static const char *arg_key_name
= NULL
; /* name in kernel keyring */
29 static const char *arg_credential_name
= NULL
; /* name in $CREDENTIALS_DIRECTORY directory */
30 static char *arg_message
= NULL
;
31 static usec_t arg_timeout
= DEFAULT_TIMEOUT_USEC
;
32 static bool arg_multiple
= false;
33 static bool arg_no_output
= false;
34 static AskPasswordFlags arg_flags
= ASK_PASSWORD_PUSH_CACHE
;
35 static bool arg_newline
= true;
36 static bool arg_varlink
= false;
38 STATIC_DESTRUCTOR_REGISTER(arg_message
, freep
);
40 static int help(void) {
41 _cleanup_free_
char *link
= NULL
;
44 r
= terminal_urlify_man("systemd-ask-password", "1", &link
);
48 printf("%1$s [OPTIONS...] MESSAGE\n\n"
49 "%3$sQuery the user for a passphrase, via the TTY or a UI agent.%4$s\n\n"
50 " -h --help Show this help\n"
51 " --icon=NAME Icon name\n"
52 " --id=ID Query identifier (e.g. \"cryptsetup:/dev/sda5\")\n"
53 " --keyname=NAME Kernel key name for caching passwords (e.g. \"cryptsetup\")\n"
54 " --credential=NAME\n"
55 " Credential name for ImportCredential=, LoadCredential= or\n"
56 " SetCredential= credentials\n"
57 " --timeout=SEC Timeout in seconds\n"
58 " --echo=yes|no|masked\n"
59 " Control whether to show password while typing (echo)\n"
60 " -e --echo Equivalent to --echo=yes\n"
61 " --emoji=yes|no|auto\n"
62 " Show a lock and key emoji\n"
63 " --no-tty Ask question via agent even on TTY\n"
64 " --accept-cached Accept cached passwords\n"
65 " --multiple List multiple passwords if available\n"
66 " --no-output Do not print password to standard output\n"
67 " -n Do not suffix password written to standard output with\n"
69 " --user Ask only our own user's agents\n"
70 " --system Ask agents of the system and of all users\n"
71 "\nSee the %2$s for details.\n",
72 program_invocation_short_name
,
80 static int parse_argv(int argc
, char *argv
[]) {
98 static const struct option options
[] = {
99 { "help", no_argument
, NULL
, 'h' },
100 { "version", no_argument
, NULL
, ARG_VERSION
},
101 { "icon", required_argument
, NULL
, ARG_ICON
},
102 { "timeout", required_argument
, NULL
, ARG_TIMEOUT
},
103 { "echo", optional_argument
, NULL
, 'e' },
104 { "emoji", required_argument
, NULL
, ARG_EMOJI
},
105 { "no-tty", no_argument
, NULL
, ARG_NO_TTY
},
106 { "accept-cached", no_argument
, NULL
, ARG_ACCEPT_CACHED
},
107 { "multiple", no_argument
, NULL
, ARG_MULTIPLE
},
108 { "id", required_argument
, NULL
, ARG_ID
},
109 { "keyname", required_argument
, NULL
, ARG_KEYNAME
},
110 { "no-output", no_argument
, NULL
, ARG_NO_OUTPUT
},
111 { "credential", required_argument
, NULL
, ARG_CREDENTIAL
},
112 { "user", no_argument
, NULL
, ARG_USER
},
113 { "system", no_argument
, NULL
, ARG_SYSTEM
},
117 const char *emoji
= NULL
;
123 /* Note the asymmetry: the long option --echo= allows an optional argument, the short option does
126 /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
127 * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
129 while ((c
= getopt_long(argc
, argv
, "+hen", options
, NULL
)) >= 0)
144 r
= parse_sec(optarg
, &arg_timeout
);
146 return log_error_errno(r
, "Failed to parse --timeout= parameter: %s", optarg
);
152 /* Short option -e is used, or no argument to long option --echo= */
153 arg_flags
|= ASK_PASSWORD_ECHO
;
154 arg_flags
&= ~ASK_PASSWORD_SILENT
;
155 } else if (isempty(optarg
) || streq(optarg
, "masked"))
156 /* Empty argument or explicit string "masked" for default behaviour. */
157 arg_flags
&= ~(ASK_PASSWORD_ECHO
|ASK_PASSWORD_SILENT
);
159 r
= parse_boolean_argument("--echo=", optarg
, NULL
);
163 SET_FLAG(arg_flags
, ASK_PASSWORD_ECHO
, r
);
164 SET_FLAG(arg_flags
, ASK_PASSWORD_SILENT
, !r
);
173 arg_flags
|= ASK_PASSWORD_NO_TTY
;
176 case ARG_ACCEPT_CACHED
:
177 arg_flags
|= ASK_PASSWORD_ACCEPT_CACHED
;
189 arg_key_name
= optarg
;
193 arg_no_output
= true;
197 arg_credential_name
= optarg
;
201 arg_flags
|= ASK_PASSWORD_USER
;
205 arg_flags
&= ~ASK_PASSWORD_USER
;
216 assert_not_reached();
219 if (isempty(emoji
) || streq(emoji
, "auto"))
220 SET_FLAG(arg_flags
, ASK_PASSWORD_HIDE_EMOJI
, FLAGS_SET(arg_flags
, ASK_PASSWORD_ECHO
));
222 r
= parse_boolean_argument("--emoji=", emoji
, NULL
);
226 SET_FLAG(arg_flags
, ASK_PASSWORD_HIDE_EMOJI
, !r
);
230 arg_message
= strv_join(argv
+ optind
, " ");
233 } else if (FLAGS_SET(arg_flags
, ASK_PASSWORD_ECHO
)) {
234 /* By default ask_password_auto() will query with the string "Password: ", which is not right
235 * when full echo is on, since then it's unlikely a password. Let's hence default to a less
236 * confusing string in that case. */
238 arg_message
= strdup("Input:");
243 r
= sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT
);
245 return log_error_errno(r
, "Failed to check if invoked in Varlink mode: %m");
252 typedef enum EchoMode
{
257 _ECHO_MODE_INVALID
= -EINVAL
,
260 static const char* echo_mode_table
[_ECHO_MODE_MAX
] = {
263 [ECHO_MASKED
] = "masked",
266 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(echo_mode
, EchoMode
, ECHO_ON
);
268 static JSON_DISPATCH_ENUM_DEFINE(dispatch_echo_mode
, EchoMode
, echo_mode_from_string
);
270 typedef struct MethodAskParameters
{
275 uint64_t timeout_usec
;
280 } MethodAskParameters
;
282 static int vl_method_ask(sd_varlink
*link
, sd_json_variant
*parameters
, sd_varlink_method_flags_t flags
, void *userdata
) {
284 static const sd_json_dispatch_field dispatch_table
[] = {
285 { "message", SD_JSON_VARIANT_STRING
, sd_json_dispatch_const_string
, offsetof(MethodAskParameters
, message
), 0 },
286 { "keyname", SD_JSON_VARIANT_STRING
, sd_json_dispatch_const_string
, offsetof(MethodAskParameters
, keyring
), 0 },
287 { "icon", SD_JSON_VARIANT_STRING
, sd_json_dispatch_const_string
, offsetof(MethodAskParameters
, icon
), 0 },
288 { "id", SD_JSON_VARIANT_STRING
, sd_json_dispatch_const_string
, offsetof(MethodAskParameters
, id
), 0 },
289 { "timeoutUSec", _SD_JSON_VARIANT_TYPE_INVALID
, sd_json_dispatch_uint64
, offsetof(MethodAskParameters
, timeout_usec
), 0 },
290 { "untilUSec", _SD_JSON_VARIANT_TYPE_INVALID
, sd_json_dispatch_uint64
, offsetof(MethodAskParameters
, until_usec
), 0 },
291 { "acceptCached", SD_JSON_VARIANT_BOOLEAN
, sd_json_dispatch_tristate
, offsetof(MethodAskParameters
, accept_cached
), 0 },
292 { "pushCache", SD_JSON_VARIANT_BOOLEAN
, sd_json_dispatch_tristate
, offsetof(MethodAskParameters
, push_cache
) , 0 },
293 { "echo", SD_JSON_VARIANT_STRING
, dispatch_echo_mode
, offsetof(MethodAskParameters
, echo_mode
), 0 },
294 VARLINK_DISPATCH_POLKIT_FIELD
,
298 Hashmap
**polkit_registry
= ASSERT_PTR(userdata
);
299 MethodAskParameters p
= {
300 .timeout_usec
= DEFAULT_TIMEOUT_USEC
,
301 .until_usec
= UINT64_MAX
,
304 .echo_mode
= _ECHO_MODE_INVALID
,
310 r
= sd_varlink_dispatch(link
, parameters
, dispatch_table
, &p
);
314 r
= varlink_verify_polkit_async_full(
317 "io.systemd.ask-password.ask",
319 /* good_user= */ FLAGS_SET(arg_flags
, ASK_PASSWORD_USER
) ? getuid() : UID_INVALID
,
325 AskPasswordRequest req
= {
327 .message
= p
.message
?: arg_message
,
328 .icon
= p
.icon
?: arg_icon
,
329 .id
= p
.id
?: arg_id
,
330 .keyring
= p
.keyring
?: arg_key_name
,
331 .credential
= arg_credential_name
,
332 .hup_fd
= sd_varlink_get_input_fd(link
),
335 /* Specifying the absolute or relative timeout as zero means: do not ask interactively, only check
336 * cache, hence leave the field at zero in that case. Otherwise we take the minimum of both times. */
337 if (p
.timeout_usec
!= 0 && p
.until_usec
!= 0)
338 req
.until
= MIN(usec_add(now(CLOCK_MONOTONIC
), p
.timeout_usec
), p
.until_usec
);
340 /* If the timeout is set to zero, don't ask agents, just stick to cache */
341 SET_FLAG(arg_flags
, ASK_PASSWORD_NO_AGENT
, req
.until
== 0);
343 if (p
.accept_cached
>= 0)
344 SET_FLAG(arg_flags
, ASK_PASSWORD_ACCEPT_CACHED
, p
.accept_cached
);
346 if (p
.push_cache
>= 0)
347 SET_FLAG(arg_flags
, ASK_PASSWORD_PUSH_CACHE
, p
.push_cache
);
349 if (p
.echo_mode
>= 0) {
350 SET_FLAG(arg_flags
, ASK_PASSWORD_ECHO
, p
.echo_mode
== ECHO_ON
);
351 SET_FLAG(arg_flags
, ASK_PASSWORD_SILENT
, p
.echo_mode
== ECHO_OFF
);
354 _cleanup_strv_free_erase_
char **l
= NULL
;
355 r
= ask_password_auto(&req
, arg_flags
, &l
);
357 return sd_varlink_error(link
, "io.systemd.AskPassword.NoPasswordAvailable", NULL
);
359 return sd_varlink_error(link
, "io.systemd.AskPassword.TimeoutReached", NULL
);
360 if (r
== -ECONNRESET
) { /* POLLHUP on the varlink fd we passed in via .hup_fd */
361 sd_varlink_close(link
);
367 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*vl
= NULL
;
368 r
= sd_json_variant_new_array_strv(&vl
, l
);
372 sd_json_variant_sensitive(vl
);
374 return sd_varlink_replybo(link
, SD_JSON_BUILD_PAIR("passwords", SD_JSON_BUILD_VARIANT(vl
)));
377 static int vl_server(void) {
378 _cleanup_(sd_varlink_server_unrefp
) sd_varlink_server
*varlink_server
= NULL
;
379 _cleanup_hashmap_free_ Hashmap
*polkit_registry
= NULL
;
382 r
= varlink_server_new(&varlink_server
, SD_VARLINK_SERVER_INHERIT_USERDATA
, /* userdata= */ &polkit_registry
);
384 return log_error_errno(r
, "Failed to allocate Varlink server: %m");
386 r
= sd_varlink_server_add_interface(varlink_server
, &vl_interface_io_systemd_AskPassword
);
388 return log_error_errno(r
, "Failed to add Varlink interface: %m");
390 r
= sd_varlink_server_bind_method(varlink_server
, "io.systemd.AskPassword.Ask", vl_method_ask
);
392 return log_error_errno(r
, "Failed to bind Varlink method: %m");
394 r
= sd_varlink_server_loop_auto(varlink_server
);
396 return log_error_errno(r
, "Failed to run Varlink event loop: %m");
401 static int run(int argc
, char *argv
[]) {
402 _cleanup_strv_free_erase_
char **l
= NULL
;
408 /* Unprivileged? Then imply ASK_PASSWORD_USER by default */
409 SET_FLAG(arg_flags
, ASK_PASSWORD_USER
, geteuid() != 0);
411 r
= parse_argv(argc
, argv
);
416 return vl_server(); /* Invocation as Varlink service */
418 timeout
= arg_timeout
> 0 ? usec_add(now(CLOCK_MONOTONIC
), arg_timeout
) : 0;
420 AskPasswordRequest req
= {
422 .message
= arg_message
,
425 .keyring
= arg_key_name
,
426 .credential
= arg_credential_name
?: "password",
431 r
= ask_password_auto(&req
, arg_flags
, &l
);
433 return log_error_errno(r
, "Failed to query password: %m");
436 if (!arg_no_output
) {
452 DEFINE_MAIN_FUNCTION(run
);