]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
490aed58 | 2 | /*** |
490aed58 | 3 | Copyright 2010 Lennart Poettering |
490aed58 LP |
4 | ***/ |
5 | ||
490aed58 | 6 | #include <errno.h> |
490aed58 | 7 | #include <getopt.h> |
0e098b15 | 8 | #include <stddef.h> |
00843602 | 9 | #include <unistd.h> |
490aed58 | 10 | |
00843602 LP |
11 | #include "ask-password-api.h" |
12 | #include "def.h" | |
490aed58 LP |
13 | #include "log.h" |
14 | #include "macro.h" | |
21bc923a | 15 | #include "strv.h" |
490aed58 LP |
16 | |
17 | static const char *arg_icon = NULL; | |
9fa1de96 | 18 | static const char *arg_id = NULL; |
e287086b LP |
19 | static const char *arg_keyname = NULL; |
20 | static char *arg_message = NULL; | |
7f434cf4 | 21 | static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC; |
21bc923a | 22 | static bool arg_multiple = false; |
a5a4e365 | 23 | static bool arg_no_output = false; |
e287086b | 24 | static AskPasswordFlags arg_flags = ASK_PASSWORD_PUSH_CACHE; |
490aed58 | 25 | |
601185b4 | 26 | static void help(void) { |
490aed58 | 27 | printf("%s [OPTIONS...] MESSAGE\n\n" |
ad6ab0af | 28 | "Query the user for a system passphrase, via the TTY or an UI agent.\n\n" |
e287086b LP |
29 | " -h --help Show this help\n" |
30 | " --icon=NAME Icon name\n" | |
31 | " --id=ID Query identifier (e.g. \"cryptsetup:/dev/sda5\")\n" | |
32 | " --keyname=NAME Kernel key name for caching passwords (e.g. \"cryptsetup\")\n" | |
33 | " --timeout=SEC Timeout in seconds\n" | |
34 | " --echo Do not mask input (useful for usernames)\n" | |
35 | " --no-tty Ask question via agent even on TTY\n" | |
36 | " --accept-cached Accept cached passwords\n" | |
37 | " --multiple List multiple passwords if available\n" | |
a5a4e365 | 38 | " --no-output Do not print password to standard output\n" |
601185b4 | 39 | , program_invocation_short_name); |
490aed58 LP |
40 | } |
41 | ||
42 | static int parse_argv(int argc, char *argv[]) { | |
43 | ||
44 | enum { | |
45 | ARG_ICON = 0x100, | |
1b39d4b9 | 46 | ARG_TIMEOUT, |
64845bdc | 47 | ARG_ECHO, |
21bc923a LP |
48 | ARG_NO_TTY, |
49 | ARG_ACCEPT_CACHED, | |
9fa1de96 | 50 | ARG_MULTIPLE, |
e287086b LP |
51 | ARG_ID, |
52 | ARG_KEYNAME, | |
a5a4e365 | 53 | ARG_NO_OUTPUT, |
490aed58 LP |
54 | }; |
55 | ||
56 | static const struct option options[] = { | |
21bc923a LP |
57 | { "help", no_argument, NULL, 'h' }, |
58 | { "icon", required_argument, NULL, ARG_ICON }, | |
59 | { "timeout", required_argument, NULL, ARG_TIMEOUT }, | |
64845bdc | 60 | { "echo", no_argument, NULL, ARG_ECHO }, |
21bc923a LP |
61 | { "no-tty", no_argument, NULL, ARG_NO_TTY }, |
62 | { "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED }, | |
63 | { "multiple", no_argument, NULL, ARG_MULTIPLE }, | |
9fa1de96 | 64 | { "id", required_argument, NULL, ARG_ID }, |
e287086b | 65 | { "keyname", required_argument, NULL, ARG_KEYNAME }, |
a5a4e365 | 66 | { "no-output", no_argument, NULL, ARG_NO_OUTPUT }, |
eb9da376 | 67 | {} |
490aed58 LP |
68 | }; |
69 | ||
70 | int c; | |
71 | ||
72 | assert(argc >= 0); | |
73 | assert(argv); | |
74 | ||
601185b4 | 75 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
490aed58 LP |
76 | |
77 | switch (c) { | |
78 | ||
79 | case 'h': | |
601185b4 ZJS |
80 | help(); |
81 | return 0; | |
490aed58 LP |
82 | |
83 | case ARG_ICON: | |
84 | arg_icon = optarg; | |
85 | break; | |
86 | ||
87 | case ARG_TIMEOUT: | |
7f602784 | 88 | if (parse_sec(optarg, &arg_timeout) < 0) { |
490aed58 LP |
89 | log_error("Failed to parse --timeout parameter %s", optarg); |
90 | return -EINVAL; | |
91 | } | |
92 | break; | |
93 | ||
64845bdc | 94 | case ARG_ECHO: |
e287086b | 95 | arg_flags |= ASK_PASSWORD_ECHO; |
64845bdc DS |
96 | break; |
97 | ||
1b39d4b9 | 98 | case ARG_NO_TTY: |
e287086b | 99 | arg_flags |= ASK_PASSWORD_NO_TTY; |
1b39d4b9 LP |
100 | break; |
101 | ||
21bc923a | 102 | case ARG_ACCEPT_CACHED: |
e287086b | 103 | arg_flags |= ASK_PASSWORD_ACCEPT_CACHED; |
21bc923a LP |
104 | break; |
105 | ||
106 | case ARG_MULTIPLE: | |
107 | arg_multiple = true; | |
108 | break; | |
109 | ||
9fa1de96 DH |
110 | case ARG_ID: |
111 | arg_id = optarg; | |
112 | break; | |
113 | ||
e287086b LP |
114 | case ARG_KEYNAME: |
115 | arg_keyname = optarg; | |
116 | break; | |
117 | ||
a5a4e365 CH |
118 | case ARG_NO_OUTPUT: |
119 | arg_no_output = true; | |
120 | break; | |
121 | ||
490aed58 LP |
122 | case '?': |
123 | return -EINVAL; | |
124 | ||
125 | default: | |
eb9da376 | 126 | assert_not_reached("Unhandled option"); |
490aed58 | 127 | } |
490aed58 | 128 | |
e287086b LP |
129 | if (argc > optind) { |
130 | arg_message = strv_join(argv + optind, " "); | |
131 | if (!arg_message) | |
132 | return log_oom(); | |
490aed58 LP |
133 | } |
134 | ||
1b39d4b9 | 135 | return 1; |
490aed58 LP |
136 | } |
137 | ||
1b39d4b9 | 138 | int main(int argc, char *argv[]) { |
ab84f5b9 | 139 | _cleanup_strv_free_erase_ char **l = NULL; |
7dcda352 | 140 | usec_t timeout; |
e287086b LP |
141 | char **p; |
142 | int r; | |
1b39d4b9 LP |
143 | |
144 | log_parse_environment(); | |
145 | log_open(); | |
146 | ||
601185b4 ZJS |
147 | r = parse_argv(argc, argv); |
148 | if (r <= 0) | |
1b39d4b9 LP |
149 | goto finish; |
150 | ||
7dcda352 LP |
151 | if (arg_timeout > 0) |
152 | timeout = now(CLOCK_MONOTONIC) + arg_timeout; | |
153 | else | |
154 | timeout = 0; | |
155 | ||
e287086b LP |
156 | r = ask_password_auto(arg_message, arg_icon, arg_id, arg_keyname, timeout, arg_flags, &l); |
157 | if (r < 0) { | |
158 | log_error_errno(r, "Failed to query password: %m"); | |
159 | goto finish; | |
160 | } | |
21bc923a | 161 | |
e287086b | 162 | STRV_FOREACH(p, l) { |
a5a4e365 CH |
163 | if (!arg_no_output) |
164 | puts(*p); | |
ec863ba6 | 165 | |
e287086b LP |
166 | if (!arg_multiple) |
167 | break; | |
7f4e0805 | 168 | } |
1b39d4b9 LP |
169 | |
170 | finish: | |
e287086b LP |
171 | free(arg_message); |
172 | ||
1b39d4b9 LP |
173 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
174 | } |