1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
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
11 (at your option) any later version.
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
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
24 #include <sys/types.h>
32 #include <sys/signalfd.h>
42 #include "ask-password-api.h"
45 static const char *arg_icon = NULL;
46 static const char *arg_id = NULL;
47 static const char *arg_message = NULL;
48 static bool arg_use_tty = true;
49 static usec_t arg_timeout = DEFAULT_TIMEOUT_USEC;
50 static bool arg_accept_cached = false;
51 static bool arg_multiple = false;
53 static void help(void) {
54 printf("%s [OPTIONS...] MESSAGE\n\n"
55 "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
56 " -h --help Show this help\n"
57 " --icon=NAME Icon name\n"
58 " --timeout=SEC Timeout in sec\n"
59 " --no-tty Ask question via agent even on TTY\n"
60 " --accept-cached Accept cached passwords\n"
61 " --multiple List multiple passwords if available\n"
62 " --id=ID Query identifier (e.g. cryptsetup:/dev/sda5)\n"
63 , program_invocation_short_name);
66 static int parse_argv(int argc, char *argv[]) {
77 static const struct option options[] = {
78 { "help", no_argument, NULL, 'h' },
79 { "icon", required_argument, NULL, ARG_ICON },
80 { "timeout", required_argument, NULL, ARG_TIMEOUT },
81 { "no-tty", no_argument, NULL, ARG_NO_TTY },
82 { "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED },
83 { "multiple", no_argument, NULL, ARG_MULTIPLE },
84 { "id", required_argument, NULL, ARG_ID },
93 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
106 if (parse_sec(optarg, &arg_timeout) < 0) {
107 log_error("Failed to parse --timeout parameter %s", optarg);
116 case ARG_ACCEPT_CACHED:
117 arg_accept_cached = true;
132 assert_not_reached("Unhandled option");
135 if (optind != argc - 1) {
136 log_error("%s: required argument missing.", program_invocation_short_name);
140 arg_message = argv[optind];
144 int main(int argc, char *argv[]) {
148 log_parse_environment();
151 r = parse_argv(argc, argv);
156 timeout = now(CLOCK_MONOTONIC) + arg_timeout;
160 if (arg_use_tty && isatty(STDIN_FILENO)) {
161 char *password = NULL;
163 if ((r = ask_password_tty(arg_message, timeout, NULL, &password)) >= 0) {
171 if ((r = ask_password_agent(arg_message, arg_icon, arg_id, timeout, arg_accept_cached, &l)) >= 0) {
187 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;