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