]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/ask-password/ask-password.c
strv: Add _cleanup_strv_free_erase_ and _cleanup_string_free_erase_
[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;
e287086b
LP
35static const char *arg_keyname = NULL;
36static char *arg_message = NULL;
7f434cf4 37static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC;
21bc923a 38static bool arg_multiple = 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"
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,
490aed58
LP
67 };
68
69 static const struct option options[] = {
21bc923a
LP
70 { "help", no_argument, NULL, 'h' },
71 { "icon", required_argument, NULL, ARG_ICON },
72 { "timeout", required_argument, NULL, ARG_TIMEOUT },
64845bdc 73 { "echo", no_argument, NULL, ARG_ECHO },
21bc923a
LP
74 { "no-tty", no_argument, NULL, ARG_NO_TTY },
75 { "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED },
76 { "multiple", no_argument, NULL, ARG_MULTIPLE },
9fa1de96 77 { "id", required_argument, NULL, ARG_ID },
e287086b 78 { "keyname", required_argument, NULL, ARG_KEYNAME },
eb9da376 79 {}
490aed58
LP
80 };
81
82 int c;
83
84 assert(argc >= 0);
85 assert(argv);
86
601185b4 87 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
490aed58
LP
88
89 switch (c) {
90
91 case 'h':
601185b4
ZJS
92 help();
93 return 0;
490aed58
LP
94
95 case ARG_ICON:
96 arg_icon = optarg;
97 break;
98
99 case ARG_TIMEOUT:
7f602784 100 if (parse_sec(optarg, &arg_timeout) < 0) {
490aed58
LP
101 log_error("Failed to parse --timeout parameter %s", optarg);
102 return -EINVAL;
103 }
104 break;
105
64845bdc 106 case ARG_ECHO:
e287086b 107 arg_flags |= ASK_PASSWORD_ECHO;
64845bdc
DS
108 break;
109
1b39d4b9 110 case ARG_NO_TTY:
e287086b 111 arg_flags |= ASK_PASSWORD_NO_TTY;
1b39d4b9
LP
112 break;
113
21bc923a 114 case ARG_ACCEPT_CACHED:
e287086b 115 arg_flags |= ASK_PASSWORD_ACCEPT_CACHED;
21bc923a
LP
116 break;
117
118 case ARG_MULTIPLE:
119 arg_multiple = true;
120 break;
121
9fa1de96
DH
122 case ARG_ID:
123 arg_id = optarg;
124 break;
125
e287086b
LP
126 case ARG_KEYNAME:
127 arg_keyname = optarg;
128 break;
129
490aed58
LP
130 case '?':
131 return -EINVAL;
132
133 default:
eb9da376 134 assert_not_reached("Unhandled option");
490aed58 135 }
490aed58 136
e287086b
LP
137 if (argc > optind) {
138 arg_message = strv_join(argv + optind, " ");
139 if (!arg_message)
140 return log_oom();
490aed58
LP
141 }
142
1b39d4b9 143 return 1;
490aed58
LP
144}
145
1b39d4b9 146int main(int argc, char *argv[]) {
ab84f5b9 147 _cleanup_strv_free_erase_ char **l = NULL;
7dcda352 148 usec_t timeout;
e287086b
LP
149 char **p;
150 int r;
1b39d4b9
LP
151
152 log_parse_environment();
153 log_open();
154
601185b4
ZJS
155 r = parse_argv(argc, argv);
156 if (r <= 0)
1b39d4b9
LP
157 goto finish;
158
7dcda352
LP
159 if (arg_timeout > 0)
160 timeout = now(CLOCK_MONOTONIC) + arg_timeout;
161 else
162 timeout = 0;
163
e287086b
LP
164 r = ask_password_auto(arg_message, arg_icon, arg_id, arg_keyname, timeout, arg_flags, &l);
165 if (r < 0) {
166 log_error_errno(r, "Failed to query password: %m");
167 goto finish;
168 }
21bc923a 169
e287086b
LP
170 STRV_FOREACH(p, l) {
171 puts(*p);
ec863ba6 172
e287086b
LP
173 if (!arg_multiple)
174 break;
7f4e0805 175 }
1b39d4b9
LP
176
177finish:
e287086b
LP
178 free(arg_message);
179
1b39d4b9
LP
180 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
181}