1 /**************************************************************************************************
2 $Id: passinput.c,v 1.10 2005/04/20 16:49:11 bboy Exp $
3
4 passinput.c: Read a password from standard input.
5
6 Copyright (C) 2002-2005 Don Moore <bboy@bboy.net>
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
16 GNU 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 <signal.h>
24 //#include <stdio.h>
25 //#include <termios.h>
26 //#include <unistd.h>
27 #include "mydnsutil.h"
28
29 #ifndef PASS_MAX
30 # define PASS_MAX 128
31 #endif
32
33 /**************************************************************************************************
34 PASSINPUT
35 Prompts user to input password.
36 Truncates all characters beyond PASS_MAX
37 **************************************************************************************************/
38 char *
passinput(const char * prompt)39 passinput(const char *prompt) {
40 static char buf[PASS_MAX + 1];
41 char *ptr;
42 sigset_t sig, sigsave;
43 struct termios term, termsave;
44 FILE *fp;
45 int c;
46
47 buf[0] = '\0';
48
49 if (!isatty(STDIN_FILENO)) return (buf);
50
51 if (!(fp = fopen(ctermid(NULL), "r+"))) return (buf);
52 setbuf(fp, NULL);
53
54 sigemptyset(&sig);
55 sigaddset(&sig, SIGINT);
56 sigaddset(&sig, SIGTSTP);
57 sigprocmask(SIG_BLOCK, &sig, &sigsave);
58
59 tcgetattr(fileno(fp), &termsave);
60 term = termsave;
61 term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
62 tcsetattr(fileno(fp), TCSAFLUSH, &term);
63
64 fputs(prompt, stdout);
65 fputs(": ", stdout);
66
67 ptr = buf;
68 while ((c = getc(fp)) != EOF && c != '\n') {
69 if (ptr < &buf[PASS_MAX])
70 *ptr++ = c;
71 }
72 *ptr = 0;
73 putc('\n', fp);
74
75 tcsetattr(fileno(fp), TCSAFLUSH, &termsave);
76 sigprocmask(SIG_SETMASK, &sigsave, NULL);
77 fclose(fp);
78 return (buf);
79 }
80 /*--- passinput() -------------------------------------------------------------------------------*/
81
82 /* vi:set ts=3: */
83