2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "ask-password-api.h"
31 static const char *arg_icon
= NULL
;
32 static const char *arg_id
= NULL
;
33 static const char *arg_keyname
= NULL
;
34 static char *arg_message
= NULL
;
35 static usec_t arg_timeout
= DEFAULT_TIMEOUT_USEC
;
36 static bool arg_multiple
= false;
37 static bool arg_no_output
= false;
38 static AskPasswordFlags arg_flags
= ASK_PASSWORD_PUSH_CACHE
;
40 static void help(void) {
41 printf("%s [OPTIONS...] MESSAGE\n\n"
42 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
43 " -h --help Show this help\n"
44 " --icon=NAME Icon name\n"
45 " --id=ID Query identifier (e.g. \"cryptsetup:/dev/sda5\")\n"
46 " --keyname=NAME Kernel key name for caching passwords (e.g. \"cryptsetup\")\n"
47 " --timeout=SEC Timeout in seconds\n"
48 " --echo Do not mask input (useful for usernames)\n"
49 " --no-tty Ask question via agent even on TTY\n"
50 " --accept-cached Accept cached passwords\n"
51 " --multiple List multiple passwords if available\n"
52 " --no-output Do not print password to standard output\n"
53 , program_invocation_short_name
);
56 static int parse_argv(int argc
, char *argv
[]) {
70 static const struct option options
[] = {
71 { "help", no_argument
, NULL
, 'h' },
72 { "icon", required_argument
, NULL
, ARG_ICON
},
73 { "timeout", required_argument
, NULL
, ARG_TIMEOUT
},
74 { "echo", no_argument
, NULL
, ARG_ECHO
},
75 { "no-tty", no_argument
, NULL
, ARG_NO_TTY
},
76 { "accept-cached", no_argument
, NULL
, ARG_ACCEPT_CACHED
},
77 { "multiple", no_argument
, NULL
, ARG_MULTIPLE
},
78 { "id", required_argument
, NULL
, ARG_ID
},
79 { "keyname", required_argument
, NULL
, ARG_KEYNAME
},
80 { "no-output", no_argument
, NULL
, ARG_NO_OUTPUT
},
89 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
102 if (parse_sec(optarg
, &arg_timeout
) < 0) {
103 log_error("Failed to parse --timeout parameter %s", optarg
);
109 arg_flags
|= ASK_PASSWORD_ECHO
;
113 arg_flags
|= ASK_PASSWORD_NO_TTY
;
116 case ARG_ACCEPT_CACHED
:
117 arg_flags
|= ASK_PASSWORD_ACCEPT_CACHED
;
129 arg_keyname
= optarg
;
133 arg_no_output
= true;
140 assert_not_reached("Unhandled option");
144 arg_message
= strv_join(argv
+ optind
, " ");
152 int main(int argc
, char *argv
[]) {
153 _cleanup_strv_free_erase_
char **l
= NULL
;
158 log_parse_environment();
161 r
= parse_argv(argc
, argv
);
166 timeout
= now(CLOCK_MONOTONIC
) + arg_timeout
;
170 r
= ask_password_auto(arg_message
, arg_icon
, arg_id
, arg_keyname
, timeout
, arg_flags
, &l
);
172 log_error_errno(r
, "Failed to query password: %m");
187 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;