File: getpass.c

package info (click to toggle)
fetchmail 6.5.6-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,596 kB
  • sloc: ansic: 19,190; sh: 7,108; python: 2,395; perl: 564; yacc: 447; lex: 286; makefile: 260; awk: 124; lisp: 84; exp: 43; sed: 17
file content (146 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * For license terms, see the file COPYING in this directory.
 */

/***********************************************************************
  module:       getpass.c
  project:      fetchmail
  programmer:   Carl Harris, ceharris@mal.com
  description: 	getpass() replacement which allows for long passwords.
                This version hacked by Wilfred Teiken, allowing the
                password to be piped to fetchmail.

 ***********************************************************************/

#include "config.h"
#include "fetchmail.h"

#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "i18n.h"

#define INPUT_BUF_SIZE	PASSWORDLEN

#include <termios.h>

static int ttyfd;

static struct termios termb;
static tcflag_t flags;

static void save_tty_state(void);
static void disable_tty_echo(void);
static void restore_tty_state(void);
static void sigint_handler(int);

static char pbuf[INPUT_BUF_SIZE];

void fm_clearpassword(void)
{
    fm_safe_clearmem(pbuf, sizeof(pbuf));
}

char *fm_getpassword(const char *prompt)
{
    char *p;
    FILE *fi = NULL;
    SIGHANDLERTYPE sig = 0;	/* initialization pacifies -Wall */

    int istty = isatty(0);
    ttyfd = STDIN_FILENO;

    /* get the file descriptor for the actual input device if it's a tty */
    if (istty)
    {
        fi = fopen("/dev/tty", "r+");
	if (fi) {
	    setbuf(fi, (char *)NULL);
	    /* store descriptor for the controlling tty */
	    ttyfd = fileno(fi);
	} else {
	    report(stderr, "fm_getpassword: fopen(\"/dev/tty\", \"r+\"): %s\n", strerror(errno));
	}

	/* preserve tty state before turning off echo */
	save_tty_state();

	/* now that we have the current tty state, we can catch SIGINT and
	   exit gracefully */
	sig = set_signal_handler(SIGINT, sigint_handler);

	/* turn off echo on the tty */
	disable_tty_echo();

	/* display the prompt and get the input string */
	fputs(prompt, stderr);
    }

    /* regardless of if the input is a TTY or not, read characters
     * until we have a LF or an EOF, truncating if we reach full
     * buffer size */
    FILE *readstream = fi ? fi : stdin;
    clearerr(readstream);
    p = fgets(pbuf, sizeof(pbuf), readstream);
    int save_errno = errno;
    if (!p && !ferror(readstream)) {
	// fgets() returns NULL if EOF happens early,
	// so don't report error in that case
	p = pbuf;
    }
    // Zap LF:
    (void)strtok(pbuf, "\n");

    if (istty)
    {
	/* write a newline so cursor won't appear to hang */
	fprintf(stderr, "\n");

	/* restore previous state of the tty */
	restore_tty_state();

	/* restore previous state of SIGINT */
	set_signal_handler(SIGINT, sig);

	if (fi) {
	    (void)fclose(fi);	/* not checking should be safe, file mode was "r" */
	}
    }

    errno = save_errno;
    return p;
}

static void save_tty_state (void)
{
    tcgetattr(ttyfd, &termb);
    flags = termb.c_lflag;
}

static void disable_tty_echo(void)
{
    /* turn off echo on the tty */
    termb.c_lflag &= ~ECHO;
    tcsetattr(ttyfd, TCSAFLUSH, &termb);
}

static void restore_tty_state(void)
{
    /* restore previous tty echo state */
    termb.c_lflag = flags;
    tcsetattr(ttyfd, TCSAFLUSH, &termb);
}

static void sigint_handler(int signum)
{
    (void)signum;
    fm_clearpassword();
    restore_tty_state();
    report(stderr, GT_("\nCaught SIGINT... bailing out.\n"));
    exit(1);
}

/* getpass.c ends here */