]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/ask-password/ask-password.c
ask-password: various modernizations
[thirdparty/systemd.git] / src / ask-password / ask-password.c
CommitLineData
490aed58
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
490aed58
LP
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 16 Lesser General Public License for more details.
490aed58 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
490aed58
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
490aed58 22#include <errno.h>
490aed58 23#include <getopt.h>
0e098b15 24#include <stddef.h>
00843602 25#include <unistd.h>
490aed58 26
00843602
LP
27#include "ask-password-api.h"
28#include "def.h"
490aed58
LP
29#include "log.h"
30#include "macro.h"
21bc923a 31#include "strv.h"
490aed58
LP
32
33static const char *arg_icon = NULL;
9fa1de96 34static const char *arg_id = NULL;
490aed58 35static const char *arg_message = NULL;
64845bdc 36static bool arg_echo = false;
1b39d4b9 37static bool arg_use_tty = true;
7f434cf4 38static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC;
21bc923a
LP
39static bool arg_accept_cached = false;
40static bool arg_multiple = false;
490aed58 41
601185b4 42static void help(void) {
490aed58 43 printf("%s [OPTIONS...] MESSAGE\n\n"
ad6ab0af 44 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
21bc923a
LP
45 " -h --help Show this help\n"
46 " --icon=NAME Icon name\n"
47 " --timeout=SEC Timeout in sec\n"
64845bdc 48 " --echo Do not mask input (useful for usernames)\n"
21bc923a
LP
49 " --no-tty Ask question via agent even on TTY\n"
50 " --accept-cached Accept cached passwords\n"
9fa1de96 51 " --multiple List multiple passwords if available\n"
601185b4
ZJS
52 " --id=ID Query identifier (e.g. cryptsetup:/dev/sda5)\n"
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
DH
64 ARG_MULTIPLE,
65 ARG_ID
490aed58
LP
66 };
67
68 static const struct option options[] = {
21bc923a
LP
69 { "help", no_argument, NULL, 'h' },
70 { "icon", required_argument, NULL, ARG_ICON },
71 { "timeout", required_argument, NULL, ARG_TIMEOUT },
64845bdc 72 { "echo", no_argument, NULL, ARG_ECHO },
21bc923a
LP
73 { "no-tty", no_argument, NULL, ARG_NO_TTY },
74 { "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED },
75 { "multiple", no_argument, NULL, ARG_MULTIPLE },
9fa1de96 76 { "id", required_argument, NULL, ARG_ID },
eb9da376 77 {}
490aed58
LP
78 };
79
80 int c;
81
82 assert(argc >= 0);
83 assert(argv);
84
601185b4 85 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
490aed58
LP
86
87 switch (c) {
88
89 case 'h':
601185b4
ZJS
90 help();
91 return 0;
490aed58
LP
92
93 case ARG_ICON:
94 arg_icon = optarg;
95 break;
96
97 case ARG_TIMEOUT:
7f602784 98 if (parse_sec(optarg, &arg_timeout) < 0) {
490aed58
LP
99 log_error("Failed to parse --timeout parameter %s", optarg);
100 return -EINVAL;
101 }
102 break;
103
64845bdc
DS
104 case ARG_ECHO:
105 arg_echo = true;
106 break;
107
1b39d4b9
LP
108 case ARG_NO_TTY:
109 arg_use_tty = false;
110 break;
111
21bc923a
LP
112 case ARG_ACCEPT_CACHED:
113 arg_accept_cached = true;
114 break;
115
116 case ARG_MULTIPLE:
117 arg_multiple = true;
118 break;
119
9fa1de96
DH
120 case ARG_ID:
121 arg_id = optarg;
122 break;
123
490aed58
LP
124 case '?':
125 return -EINVAL;
126
127 default:
eb9da376 128 assert_not_reached("Unhandled option");
490aed58 129 }
490aed58 130
601185b4
ZJS
131 if (optind != argc - 1) {
132 log_error("%s: required argument missing.", program_invocation_short_name);
490aed58
LP
133 return -EINVAL;
134 }
135
136 arg_message = argv[optind];
1b39d4b9 137 return 1;
490aed58
LP
138}
139
1b39d4b9
LP
140int main(int argc, char *argv[]) {
141 int r;
7dcda352 142 usec_t timeout;
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
21bc923a 156 if (arg_use_tty && isatty(STDIN_FILENO)) {
00843602 157 _cleanup_free_ char *password = NULL;
21bc923a 158
00843602
LP
159 r = ask_password_tty(arg_message, timeout, arg_echo, NULL, &password);
160 if (r < 0) {
161 log_error_errno(r, "Failed to ask for password on terminal: %m");
162 goto finish;
21bc923a
LP
163 }
164
00843602 165 puts(password);
21bc923a 166 } else {
00843602
LP
167 _cleanup_free_ char **l = NULL;
168 char **p;
21bc923a 169
00843602
LP
170 r = ask_password_agent(arg_message, arg_icon, arg_id, timeout, arg_echo, arg_accept_cached, &l);
171 if (r < 0) {
172 log_error_errno(r, "Failed to ask for password via agent: %m");
173 goto finish;
174 }
21bc923a 175
00843602
LP
176 STRV_FOREACH(p, l) {
177 puts(*p);
ec863ba6 178
00843602
LP
179 if (!arg_multiple)
180 break;
21bc923a 181 }
7f4e0805 182 }
1b39d4b9
LP
183
184finish:
185 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
186}