1 /* libguestfs - guestfish and guestmount shared option parsing
2 * Copyright (C) 2010-2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 /* Read a passphrase ('Key') from /dev/tty with echo off.
32 * The caller (cmds.c) will call free on the string afterwards.
33 * Based on the code in cryptsetup file lib/utils.c.
36 read_key (const char *param)
39 struct termios orig, temp;
42 /* Read and write to /dev/tty if available. */
43 if (keys_from_stdin ||
44 (infp = outfp = fopen ("/dev/tty", "w+")) == NULL) {
49 /* Print the prompt and set no echo. */
50 int tty = isatty (fileno (infp));
53 fprintf (outfp, _("Enter key or passphrase (\"%s\"): "), param);
56 if (tcgetattr (fileno (infp), &orig) == -1) {
60 memcpy (&temp, &orig, sizeof temp);
61 temp.c_lflag &= ~ECHO;
63 tcsetattr (fileno (infp), TCSAFLUSH, &temp);
70 len = getline (&ret, &n, infp);
77 /* Remove the terminating \n if there is one. */
78 if (len > 0 && ret[len-1] == '\n')
82 /* Restore echo, close file descriptor. */
85 tcsetattr (fileno (infp), TCSAFLUSH, &orig);
89 fclose (infp); /* outfp == infp, so this is closed also */