summaryrefslogtreecommitdiffstats
path: root/src/ask-password
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-06-03 10:41:05 +0200
committerLennart Poettering <lennart@poettering.net>2021-06-03 11:16:48 +0200
commit49365d1c6d4889f15205af93c50ebd4a784ee173 (patch)
treec73fcd784ee121c624d5e63e40547f3c495c3a4f /src/ask-password
parentefi: drop glibc header and use pre-defined macros (diff)
downloadsystemd-49365d1c6d4889f15205af93c50ebd4a784ee173.tar.xz
systemd-49365d1c6d4889f15205af93c50ebd4a784ee173.zip
ask-password: make password echo fully configurable
This adds --visible=yes|no|asterisk which allow controlling the echo of the password prompt in detail. The existing --echo switch is then made an alias for --visible=yes (and a shortcut -e added for it too).
Diffstat (limited to 'src/ask-password')
-rw-r--r--src/ask-password/ask-password.c43
1 files changed, 31 insertions, 12 deletions
diff --git a/src/ask-password/ask-password.c b/src/ask-password/ask-password.c
index 26cf012f01..fb4b7be4fc 100644
--- a/src/ask-password/ask-password.c
+++ b/src/ask-password/ask-password.c
@@ -45,7 +45,9 @@ static int help(void) {
" Credential name for LoadCredential=/SetCredential=\n"
" credentials\n"
" --timeout=SEC Timeout in seconds\n"
- " --echo Do not mask input (useful for usernames)\n"
+ " --echo=yes|no|masked\n"
+ " Control whether to show password while typing (echo)\n"
+ " -e --echo Equivalent to --echo=yes\n"
" --emoji=yes|no|auto\n"
" Show a lock and key emoji\n"
" --no-tty Ask question via agent even on TTY\n"
@@ -66,7 +68,6 @@ static int parse_argv(int argc, char *argv[]) {
enum {
ARG_ICON = 0x100,
ARG_TIMEOUT,
- ARG_ECHO,
ARG_EMOJI,
ARG_NO_TTY,
ARG_ACCEPT_CACHED,
@@ -83,7 +84,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "version", no_argument, NULL, ARG_VERSION },
{ "icon", required_argument, NULL, ARG_ICON },
{ "timeout", required_argument, NULL, ARG_TIMEOUT },
- { "echo", no_argument, NULL, ARG_ECHO },
+ { "echo", optional_argument, NULL, 'e' },
{ "emoji", required_argument, NULL, ARG_EMOJI },
{ "no-tty", no_argument, NULL, ARG_NO_TTY },
{ "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED },
@@ -96,12 +97,14 @@ static int parse_argv(int argc, char *argv[]) {
};
const char *emoji = NULL;
- int c;
+ int c, r;
assert(argc >= 0);
assert(argv);
- while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
+ /* Note the asymmetry: the long option --echo= allows an optional argument, the short option does
+ * not. */
+ while ((c = getopt_long(argc, argv, "he", options, NULL)) >= 0)
switch (c) {
@@ -116,14 +119,30 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_TIMEOUT:
- if (parse_sec(optarg, &arg_timeout) < 0)
- return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
- "Failed to parse --timeout parameter %s",
- optarg);
+ r = parse_sec(optarg, &arg_timeout);
+ if (r < 0)
+ return log_error_errno(r, "Failed to parse --timeout= parameter: %s", optarg);
+
break;
- case ARG_ECHO:
- arg_flags |= ASK_PASSWORD_ECHO;
+ case 'e':
+ if (!optarg) {
+ /* Short option -e is used, or no argument to long option --echo= */
+ arg_flags |= ASK_PASSWORD_ECHO;
+ arg_flags &= ~ASK_PASSWORD_SILENT;
+ } else if (isempty(optarg) || streq(optarg, "masked"))
+ /* Empty argument or explicit string "masked" for default behaviour. */
+ arg_flags &= ~(ASK_PASSWORD_ECHO|ASK_PASSWORD_SILENT);
+ else {
+ bool b;
+
+ r = parse_boolean_argument("--echo=", optarg, &b);
+ if (r < 0)
+ return r;
+
+ SET_FLAG(arg_flags, ASK_PASSWORD_ECHO, b);
+ SET_FLAG(arg_flags, ASK_PASSWORD_SILENT, !b);
+ }
break;
case ARG_EMOJI:
@@ -168,12 +187,12 @@ static int parse_argv(int argc, char *argv[]) {
if (isempty(emoji) || streq(emoji, "auto"))
SET_FLAG(arg_flags, ASK_PASSWORD_HIDE_EMOJI, FLAGS_SET(arg_flags, ASK_PASSWORD_ECHO));
else {
- int r;
bool b;
r = parse_boolean_argument("--emoji=", emoji, &b);
if (r < 0)
return r;
+
SET_FLAG(arg_flags, ASK_PASSWORD_HIDE_EMOJI, !b);
}