]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/ask-password/ask-password.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / ask-password / ask-password.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
490aed58
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
490aed58
LP
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 15 Lesser General Public License for more details.
490aed58 16
5430f7f2 17 You should have received a copy of the GNU Lesser General Public License
490aed58
LP
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
490aed58 21#include <errno.h>
490aed58 22#include <getopt.h>
0e098b15 23#include <stddef.h>
00843602 24#include <unistd.h>
490aed58 25
00843602
LP
26#include "ask-password-api.h"
27#include "def.h"
490aed58
LP
28#include "log.h"
29#include "macro.h"
21bc923a 30#include "strv.h"
490aed58
LP
31
32static const char *arg_icon = NULL;
9fa1de96 33static const char *arg_id = NULL;
e287086b
LP
34static const char *arg_keyname = NULL;
35static char *arg_message = NULL;
7f434cf4 36static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC;
21bc923a 37static bool arg_multiple = false;
a5a4e365 38static bool arg_no_output = false;
e287086b 39static AskPasswordFlags arg_flags = ASK_PASSWORD_PUSH_CACHE;
490aed58 40
601185b4 41static void help(void) {
490aed58 42 printf("%s [OPTIONS...] MESSAGE\n\n"
ad6ab0af 43 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
e287086b
LP
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"
a5a4e365 53 " --no-output Do not print password to standard output\n"
601185b4 54 , program_invocation_short_name);
490aed58
LP
55}
56
57static int parse_argv(int argc, char *argv[]) {
58
59 enum {
60 ARG_ICON = 0x100,
1b39d4b9 61 ARG_TIMEOUT,
64845bdc 62 ARG_ECHO,
21bc923a
LP
63 ARG_NO_TTY,
64 ARG_ACCEPT_CACHED,
9fa1de96 65 ARG_MULTIPLE,
e287086b
LP
66 ARG_ID,
67 ARG_KEYNAME,
a5a4e365 68 ARG_NO_OUTPUT,
490aed58
LP
69 };
70
71 static const struct option options[] = {
21bc923a
LP
72 { "help", no_argument, NULL, 'h' },
73 { "icon", required_argument, NULL, ARG_ICON },
74 { "timeout", required_argument, NULL, ARG_TIMEOUT },
64845bdc 75 { "echo", no_argument, NULL, ARG_ECHO },
21bc923a
LP
76 { "no-tty", no_argument, NULL, ARG_NO_TTY },
77 { "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED },
78 { "multiple", no_argument, NULL, ARG_MULTIPLE },
9fa1de96 79 { "id", required_argument, NULL, ARG_ID },
e287086b 80 { "keyname", required_argument, NULL, ARG_KEYNAME },
a5a4e365 81 { "no-output", no_argument, NULL, ARG_NO_OUTPUT },
eb9da376 82 {}
490aed58
LP
83 };
84
85 int c;
86
87 assert(argc >= 0);
88 assert(argv);
89
601185b4 90 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
490aed58
LP
91
92 switch (c) {
93
94 case 'h':
601185b4
ZJS
95 help();
96 return 0;
490aed58
LP
97
98 case ARG_ICON:
99 arg_icon = optarg;
100 break;
101
102 case ARG_TIMEOUT:
7f602784 103 if (parse_sec(optarg, &arg_timeout) < 0) {
490aed58
LP
104 log_error("Failed to parse --timeout parameter %s", optarg);
105 return -EINVAL;
106 }
107 break;
108
64845bdc 109 case ARG_ECHO:
e287086b 110 arg_flags |= ASK_PASSWORD_ECHO;
64845bdc
DS
111 break;
112
1b39d4b9 113 case ARG_NO_TTY:
e287086b 114 arg_flags |= ASK_PASSWORD_NO_TTY;
1b39d4b9
LP
115 break;
116
21bc923a 117 case ARG_ACCEPT_CACHED:
e287086b 118 arg_flags |= ASK_PASSWORD_ACCEPT_CACHED;
21bc923a
LP
119 break;
120
121 case ARG_MULTIPLE:
122 arg_multiple = true;
123 break;
124
9fa1de96
DH
125 case ARG_ID:
126 arg_id = optarg;
127 break;
128
e287086b
LP
129 case ARG_KEYNAME:
130 arg_keyname = optarg;
131 break;
132
a5a4e365
CH
133 case ARG_NO_OUTPUT:
134 arg_no_output = true;
135 break;
136
490aed58
LP
137 case '?':
138 return -EINVAL;
139
140 default:
eb9da376 141 assert_not_reached("Unhandled option");
490aed58 142 }
490aed58 143
e287086b
LP
144 if (argc > optind) {
145 arg_message = strv_join(argv + optind, " ");
146 if (!arg_message)
147 return log_oom();
490aed58
LP
148 }
149
1b39d4b9 150 return 1;
490aed58
LP
151}
152
1b39d4b9 153int main(int argc, char *argv[]) {
ab84f5b9 154 _cleanup_strv_free_erase_ char **l = NULL;
7dcda352 155 usec_t timeout;
e287086b
LP
156 char **p;
157 int r;
1b39d4b9
LP
158
159 log_parse_environment();
160 log_open();
161
601185b4
ZJS
162 r = parse_argv(argc, argv);
163 if (r <= 0)
1b39d4b9
LP
164 goto finish;
165
7dcda352
LP
166 if (arg_timeout > 0)
167 timeout = now(CLOCK_MONOTONIC) + arg_timeout;
168 else
169 timeout = 0;
170
e287086b
LP
171 r = ask_password_auto(arg_message, arg_icon, arg_id, arg_keyname, timeout, arg_flags, &l);
172 if (r < 0) {
173 log_error_errno(r, "Failed to query password: %m");
174 goto finish;
175 }
21bc923a 176
e287086b 177 STRV_FOREACH(p, l) {
a5a4e365
CH
178 if (!arg_no_output)
179 puts(*p);
ec863ba6 180
e287086b
LP
181 if (!arg_multiple)
182 break;
7f4e0805 183 }
1b39d4b9
LP
184
185finish:
e287086b
LP
186 free(arg_message);
187
1b39d4b9
LP
188 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
189}