ask-password.c revision 21bc923aa35d455cdef1607eb7022608c705c9f3
d657c51f14601d0235434ffb78cf6ac0f27cc83cLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering This file is part of systemd.
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering Copyright 2010 Lennart Poettering
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering systemd is free software; you can redistribute it and/or modify it
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering under the terms of the GNU General Public License as published by
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering the Free Software Foundation; either version 2 of the License, or
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering (at your option) any later version.
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering systemd is distributed in the hope that it will be useful, but
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering General Public License for more details.
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering You should have received a copy of the GNU General Public License
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poetteringstatic bool arg_use_tty = true;
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poetteringstatic usec_t arg_timeout = 60 * USEC_PER_SEC;
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poetteringstatic bool arg_accept_cached = false;
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poetteringstatic bool arg_multiple = false;
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poetteringstatic int help(void) {
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering "Query the user for a system passphrase, via the TTY or an UI agent.\n\n"
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering " -h --help Show this help\n"
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering " --icon=NAME Icon name\n"
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering " --timeout=SEC Timeout in sec\n"
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering " --no-tty Ask question via agent even on TTY\n"
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering " --accept-cached Accept cached passwords\n"
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poettering " --multiple List multiple passwords if available\n",
c4f1b86299c4ce2a62ce845bc48f2794f5459762Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering { "icon", required_argument, NULL, ARG_ICON },
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering { "timeout", required_argument, NULL, ARG_TIMEOUT },
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering { "no-tty", no_argument, NULL, ARG_NO_TTY },
d1f9edafe7b832c507931640f32069d001916b0eLennart Poettering { "accept-cached", no_argument, NULL, ARG_ACCEPT_CACHED },
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering { "multiple", no_argument, NULL, ARG_MULTIPLE },
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering if (parse_usec(optarg, &arg_timeout) < 0 || arg_timeout <= 0) {
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering log_error("Failed to parse --timeout parameter %s", optarg);
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering if (arg_use_tty && isatty(STDIN_FILENO)) {
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering if ((r = ask_password_tty(arg_message, now(CLOCK_MONOTONIC) + arg_timeout, NULL, &password)) >= 0) {
b5b4c94a67d90891a0225af0e08cf45dbc329377Lennart Poettering if ((r = ask_password_agent(arg_message, arg_icon, now(CLOCK_MONOTONIC) + arg_timeout, arg_accept_cached, &l)) >= 0) {