]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/ask-password/ask-password.c
Merge pull request #18886 from anitazha/shutdownconsole
[thirdparty/systemd.git] / src / ask-password / ask-password.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <stddef.h>
6 #include <unistd.h>
7
8 #include "ask-password-api.h"
9 #include "def.h"
10 #include "log.h"
11 #include "macro.h"
12 #include "main-func.h"
13 #include "pretty-print.h"
14 #include "strv.h"
15
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;
24
25 STATIC_DESTRUCTOR_REGISTER(arg_message, freep);
26
27 static int help(void) {
28 _cleanup_free_ char *link = NULL;
29 int r;
30
31 r = terminal_urlify_man("systemd-ask-password", "1", &link);
32 if (r < 0)
33 return log_oom();
34
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,
49 link);
50
51 return 0;
52 }
53
54 static int parse_argv(int argc, char *argv[]) {
55
56 enum {
57 ARG_ICON = 0x100,
58 ARG_TIMEOUT,
59 ARG_ECHO,
60 ARG_NO_TTY,
61 ARG_ACCEPT_CACHED,
62 ARG_MULTIPLE,
63 ARG_ID,
64 ARG_KEYNAME,
65 ARG_NO_OUTPUT,
66 ARG_VERSION,
67 };
68
69 static const struct option options[] = {
70 { "help", no_argument, NULL, 'h' },
71 { "version", no_argument, NULL, ARG_VERSION },
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 },
81 {}
82 };
83
84 int c;
85
86 assert(argc >= 0);
87 assert(argv);
88
89 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
90
91 switch (c) {
92
93 case 'h':
94 return help();
95
96 case ARG_VERSION:
97 return version();
98
99 case ARG_ICON:
100 arg_icon = optarg;
101 break;
102
103 case ARG_TIMEOUT:
104 if (parse_sec(optarg, &arg_timeout) < 0)
105 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
106 "Failed to parse --timeout parameter %s",
107 optarg);
108 break;
109
110 case ARG_ECHO:
111 arg_flags |= ASK_PASSWORD_ECHO;
112 break;
113
114 case ARG_NO_TTY:
115 arg_flags |= ASK_PASSWORD_NO_TTY;
116 break;
117
118 case ARG_ACCEPT_CACHED:
119 arg_flags |= ASK_PASSWORD_ACCEPT_CACHED;
120 break;
121
122 case ARG_MULTIPLE:
123 arg_multiple = true;
124 break;
125
126 case ARG_ID:
127 arg_id = optarg;
128 break;
129
130 case ARG_KEYNAME:
131 arg_keyname = optarg;
132 break;
133
134 case ARG_NO_OUTPUT:
135 arg_no_output = true;
136 break;
137
138 case '?':
139 return -EINVAL;
140
141 default:
142 assert_not_reached("Unhandled option");
143 }
144
145 if (argc > optind) {
146 arg_message = strv_join(argv + optind, " ");
147 if (!arg_message)
148 return log_oom();
149 }
150
151 return 1;
152 }
153
154 static int run(int argc, char *argv[]) {
155 _cleanup_strv_free_erase_ char **l = NULL;
156 usec_t timeout;
157 char **p;
158 int r;
159
160 log_show_color(true);
161 log_parse_environment();
162 log_open();
163
164 r = parse_argv(argc, argv);
165 if (r <= 0)
166 return r;
167
168 if (arg_timeout > 0)
169 timeout = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
170 else
171 timeout = 0;
172
173 r = ask_password_auto(arg_message, arg_icon, arg_id, arg_keyname, timeout, arg_flags, &l);
174 if (r < 0)
175 return log_error_errno(r, "Failed to query password: %m");
176
177 STRV_FOREACH(p, l) {
178 if (!arg_no_output)
179 puts(*p);
180
181 if (!arg_multiple)
182 break;
183 }
184
185 return 0;
186 }
187
188 DEFINE_MAIN_FUNCTION(run);