Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Ask for a password |
| 4 | * I use a static buffer in this function. Plan accordingly. |
| 5 | * |
| 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <signal.h> |
| 28 | #include <termios.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #define PWD_BUFFER_SIZE 256 |
| 31 | |
| 32 | |
| 33 | /* do nothing signal handler */ |
| 34 | static void askpass_timeout(int ignore) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | char *bb_askpass(int timeout, const char * prompt) |
| 39 | { |
| 40 | char *ret; |
| 41 | int i, size; |
| 42 | struct sigaction sa; |
| 43 | struct termios old, new; |
| 44 | static char passwd[PWD_BUFFER_SIZE]; |
| 45 | |
| 46 | tcgetattr(STDIN_FILENO, &old); |
Rob Landley | e422af6 | 2005-12-12 07:02:15 +0000 | [diff] [blame] | 47 | tcflush(STDIN_FILENO, TCIFLUSH); |
Eric Andersen | 6f9a778 | 2004-05-01 01:27:30 +0000 | [diff] [blame] | 48 | |
| 49 | size = sizeof(passwd); |
| 50 | ret = passwd; |
| 51 | memset(passwd, 0, size); |
| 52 | |
| 53 | fputs(prompt, stdout); |
| 54 | fflush(stdout); |
| 55 | |
| 56 | tcgetattr(STDIN_FILENO, &new); |
| 57 | new.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY); |
| 58 | new.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP); |
| 59 | tcsetattr(STDIN_FILENO, TCSANOW, &new); |
| 60 | |
| 61 | if (timeout) { |
| 62 | sa.sa_flags = 0; |
| 63 | sa.sa_handler = askpass_timeout; |
| 64 | sigaction(SIGALRM, &sa, NULL); |
| 65 | alarm(timeout); |
| 66 | } |
| 67 | |
| 68 | if (read(STDIN_FILENO, passwd, size-1) <= 0) { |
| 69 | ret = NULL; |
| 70 | } else { |
| 71 | for(i = 0; i < size && passwd[i]; i++) { |
| 72 | if (passwd[i]== '\r' || passwd[i] == '\n') { |
| 73 | passwd[i]= 0; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (timeout) { |
| 80 | alarm(0); |
| 81 | } |
| 82 | |
| 83 | tcsetattr(STDIN_FILENO, TCSANOW, &old); |
| 84 | fputs("\n", stdout); |
| 85 | fflush(stdout); |
| 86 | return ret; |
| 87 | } |
| 88 | |