]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/ask-password/ask-password.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / ask-password / ask-password.c
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
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stddef.h>
23 #include <unistd.h>
24
25 #include "ask-password-api.h"
26 #include "def.h"
27 #include "log.h"
28 #include "macro.h"
29 #include "strv.h"
30
31 static const char *arg_icon = NULL;
32 static const char *arg_id = NULL;
33 static const char *arg_keyname = NULL;
34 static char *arg_message = NULL;
35 static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC;
36 static bool arg_multiple = false;
37 static AskPasswordFlags arg_flags = ASK_PASSWORD_PUSH_CACHE;
38
39 static void help(void) {
40 printf("%s [OPTIONS...] MESSAGE\n\n"
41 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
42 " -h --help Show this help\n"
43 " --icon=NAME Icon name\n"
44 " --id=ID Query identifier (e.g. \"cryptsetup:/dev/sda5\")\n"
45 " --keyname=NAME Kernel key name for caching passwords (e.g. \"cryptsetup\")\n"
46 " --timeout=SEC Timeout in seconds\n"
47 " --echo Do not mask input (useful for usernames)\n"
48 " --no-tty Ask question via agent even on TTY\n"
49 " --accept-cached Accept cached passwords\n"
50 " --multiple List multiple passwords if available\n"
51 , program_invocation_short_name);
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 };
66
67 static const struct option options[] = {
68 { "help", no_argument, NULL, 'h' },
69 { "icon", required_argument, NULL, ARG_ICON },
70 { "timeout", required_argument, NULL, ARG_TIMEOUT },
71 { "echo", no_argument, NULL, ARG_ECHO },
72 { "no-tty", no_argument, NULL, ARG_NO_TTY },
73 { "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED },
74 { "multiple", no_argument, NULL, ARG_MULTIPLE },
75 { "id", required_argument, NULL, ARG_ID },
76 { "keyname", required_argument, NULL, ARG_KEYNAME },
77 {}
78 };
79
80 int c;
81
82 assert(argc >= 0);
83 assert(argv);
84
85 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
86
87 switch (c) {
88
89 case 'h':
90 help();
91 return 0;
92
93 case ARG_ICON:
94 arg_icon = optarg;
95 break;
96
97 case ARG_TIMEOUT:
98 if (parse_sec(optarg, &arg_timeout) < 0) {
99 log_error("Failed to parse --timeout parameter %s", optarg);
100 return -EINVAL;
101 }
102 break;
103
104 case ARG_ECHO:
105 arg_flags |= ASK_PASSWORD_ECHO;
106 break;
107
108 case ARG_NO_TTY:
109 arg_flags |= ASK_PASSWORD_NO_TTY;
110 break;
111
112 case ARG_ACCEPT_CACHED:
113 arg_flags |= ASK_PASSWORD_ACCEPT_CACHED;
114 break;
115
116 case ARG_MULTIPLE:
117 arg_multiple = true;
118 break;
119
120 case ARG_ID:
121 arg_id = optarg;
122 break;
123
124 case ARG_KEYNAME:
125 arg_keyname = optarg;
126 break;
127
128 case '?':
129 return -EINVAL;
130
131 default:
132 assert_not_reached("Unhandled option");
133 }
134
135 if (argc > optind) {
136 arg_message = strv_join(argv + optind, " ");
137 if (!arg_message)
138 return log_oom();
139 }
140
141 return 1;
142 }
143
144 int main(int argc, char *argv[]) {
145 _cleanup_strv_free_erase_ char **l = NULL;
146 usec_t timeout;
147 char **p;
148 int r;
149
150 log_parse_environment();
151 log_open();
152
153 r = parse_argv(argc, argv);
154 if (r <= 0)
155 goto finish;
156
157 if (arg_timeout > 0)
158 timeout = now(CLOCK_MONOTONIC) + arg_timeout;
159 else
160 timeout = 0;
161
162 r = ask_password_auto(arg_message, arg_icon, arg_id, arg_keyname, timeout, arg_flags, &l);
163 if (r < 0) {
164 log_error_errno(r, "Failed to query password: %m");
165 goto finish;
166 }
167
168 STRV_FOREACH(p, l) {
169 puts(*p);
170
171 if (!arg_multiple)
172 break;
173 }
174
175 finish:
176 free(arg_message);
177
178 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
179 }