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