1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "ask-password-api.h"
12 #include "main-func.h"
13 #include "pretty-print.h"
16 static const char *arg_icon
= NULL
;
17 static const char *arg_id
= NULL
;
18 static const char *arg_keyname
= NULL
;
19 static char *arg_message
= NULL
;
20 static usec_t arg_timeout
= DEFAULT_TIMEOUT_USEC
;
21 static bool arg_multiple
= false;
22 static bool arg_no_output
= false;
23 static AskPasswordFlags arg_flags
= ASK_PASSWORD_PUSH_CACHE
;
25 STATIC_DESTRUCTOR_REGISTER(arg_message
, freep
);
27 static int help(void) {
28 _cleanup_free_
char *link
= NULL
;
31 r
= terminal_urlify_man("systemd-ask-password", "1", &link
);
35 printf("%s [OPTIONS...] MESSAGE\n\n"
36 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
37 " -h --help Show this help\n"
38 " --icon=NAME Icon name\n"
39 " --id=ID Query identifier (e.g. \"cryptsetup:/dev/sda5\")\n"
40 " --keyname=NAME Kernel key name for caching passwords (e.g. \"cryptsetup\")\n"
41 " --timeout=SEC Timeout in seconds\n"
42 " --echo Do not mask input (useful for usernames)\n"
43 " --no-tty Ask question via agent even on TTY\n"
44 " --accept-cached Accept cached passwords\n"
45 " --multiple List multiple passwords if available\n"
46 " --no-output Do not print password to standard output\n"
47 "\nSee the %s for details.\n"
48 , program_invocation_short_name
55 static int parse_argv(int argc
, char *argv
[]) {
70 static const struct option options
[] = {
71 { "help", no_argument
, NULL
, 'h' },
72 { "version", no_argument
, NULL
, ARG_VERSION
},
73 { "icon", required_argument
, NULL
, ARG_ICON
},
74 { "timeout", required_argument
, NULL
, ARG_TIMEOUT
},
75 { "echo", no_argument
, NULL
, ARG_ECHO
},
76 { "no-tty", no_argument
, NULL
, ARG_NO_TTY
},
77 { "accept-cached", no_argument
, NULL
, ARG_ACCEPT_CACHED
},
78 { "multiple", no_argument
, NULL
, ARG_MULTIPLE
},
79 { "id", required_argument
, NULL
, ARG_ID
},
80 { "keyname", required_argument
, NULL
, ARG_KEYNAME
},
81 { "no-output", no_argument
, NULL
, ARG_NO_OUTPUT
},
90 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
105 if (parse_sec(optarg
, &arg_timeout
) < 0)
106 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
107 "Failed to parse --timeout parameter %s",
112 arg_flags
|= ASK_PASSWORD_ECHO
;
116 arg_flags
|= ASK_PASSWORD_NO_TTY
;
119 case ARG_ACCEPT_CACHED
:
120 arg_flags
|= ASK_PASSWORD_ACCEPT_CACHED
;
132 arg_keyname
= optarg
;
136 arg_no_output
= true;
143 assert_not_reached("Unhandled option");
147 arg_message
= strv_join(argv
+ optind
, " ");
155 static int run(int argc
, char *argv
[]) {
156 _cleanup_strv_free_erase_
char **l
= NULL
;
161 log_show_color(true);
162 log_parse_environment();
165 r
= parse_argv(argc
, argv
);
170 timeout
= now(CLOCK_MONOTONIC
) + arg_timeout
;
174 r
= ask_password_auto(arg_message
, arg_icon
, arg_id
, arg_keyname
, timeout
, arg_flags
, &l
);
176 return log_error_errno(r
, "Failed to query password: %m");
189 DEFINE_MAIN_FUNCTION(run
);