2 * For license terms, see the file COPYING in this directory.
5 /***********************************************************************
8 programmer: Carl Harris, ceharris@mal.com
9 description: getpass() replacement which allows for long passwords.
10 This version hacked by Wilfred Teiken, allowing the
11 password to be piped to fetchmail.
13 ***********************************************************************/
20 #if defined(HAVE_UNISTD_H)
23 #include "fetchmail.h"
25 #define INPUT_BUF_SIZE PASSWORDLEN
27 #if defined(HAVE_TERMIOS_H) && defined(HAVE_TCSETATTR)
30 #if defined(HAVE_TERMIO_H)
31 # include <sys/ioctl.h>
34 #if defined(HAVE_SGTTY_H)
42 #if defined(HAVE_TCSETATTR)
43 static struct termios termb;
44 static tcflag_t flags;
46 #if defined(HAVE_TERMIO_H)
47 static struct termio termb;
48 static unsigned short flags;
50 #if defined(HAVE_STTY)
51 static struct sgttyb ttyb;
57 void save_tty_state();
58 void disable_tty_echo();
59 void restore_tty_state();
61 char *getpassword(prompt)
64 #if !(defined(HAVE_TCSETATTR) || defined(HAVE_TERMIO_H) || defined(HAVE_STTY))
66 #if defined(HAVE_GETPASS)
68 return getpass(prompt);
70 fputs("ERROR: no support for getpassword() routine\n",stderr);
74 #endif /* !(defined(HAVE_TCSETATTR) || ... */
79 static char pbuf[INPUT_BUF_SIZE];
81 RETSIGTYPE sigint_handler();
82 int istty = (tcgetpgrp(0) != -1);
84 /* get the file descriptor for the input device */
88 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
91 setbuf(fi, (char *)NULL);
96 /* store descriptor for the tty */
101 /* preserve tty state before turning off echo */
104 /* now that we have the current tty state, we can catch SIGINT and
106 sig = signal(SIGINT, sigint_handler);
108 /* turn off echo on the tty */
111 /* display the prompt and get the input string */
112 fprintf(stderr, "%s", prompt);
115 for (p = pbuf; (c = getc(fi))!='\n' && c!=EOF;)
117 if (p < &pbuf[INPUT_BUF_SIZE - 1])
122 /* write a newline so cursor won't appear to hang */
124 fprintf(stderr, "\n");
128 /* restore previous state of the tty */
131 /* restore previous state of SIGINT */
144 #if defined(HAVE_TCSETATTR)
145 tcgetattr(ttyfd, &termb);
146 flags = termb.c_lflag;
148 #if defined(HAVE_TERMIO_H)
149 ioctl(ttyfd, TCGETA, (char *) &termb);
150 flags = termb.c_lflag;
151 #else /* we HAVE_STTY */
153 flags = ttyb.sg_flags;
162 /* turn off echo on the tty */
163 #if defined(HAVE_TCSETATTR)
164 termb.c_lflag &= ~ECHO;
165 tcsetattr(ttyfd, TCSAFLUSH, &termb);
167 #if defined(HAVE_TERMIO_H)
168 termb.c_lflag &= ~ECHO;
169 ioctl(ttyfd, TCSETA, (char *) &termb);
170 #else /* we HAVE_STTY */
171 ttyb.sg_flags &= ~ECHO;
182 /* restore previous tty echo state */
183 #if defined(HAVE_TCSETATTR)
184 termb.c_lflag = flags;
185 tcsetattr(ttyfd, TCSAFLUSH, &termb);
187 #if defined(HAVE_TERMIO_H)
188 termb.c_lflag = flags;
189 ioctl(ttyfd, TCSETA, (char *) &termb);
190 #else /* we HAVE_STTY */
191 ttyb.sg_flags = flags;
198 RETSIGTYPE sigint_handler()
201 error(1, 0, "\nCaught signal... bailing out.");