summaryrefslogtreecommitdiffstats
path: root/src/appl/gssftp/ftp/getpass.c
blob: 3f250048151c24559eabe1cd90663c70fef1c9a8 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 1985 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#ifndef lint
static	char sccsid[] = "@(#)getpass.c 1.1 90/04/28 SMI"; /* from UCB 5.4 3/7/86 */
#endif /* not lint */

#ifdef _WIN32
#include <io.h>
#include <windows.h>
#include <stdio.h>

static DWORD old_mode;
static HANDLE cons_handle;

BOOL WINAPI
GetPassConsoleControlHandler(DWORD dwCtrlType)
{
	switch(dwCtrlType){
	case CTRL_BREAK_EVENT:
	case CTRL_C_EVENT:
		printf("Interrupt\n");
		fflush(stdout);
		(void) SetConsoleMode(cons_handle, old_mode);
		ExitProcess(-1);
		break;
	default:
		break;
	}
	return TRUE;
}

char *
mygetpass(char *prompt)
{
	DWORD new_mode;
	char *ptr;
	int scratchchar;
	static char password[50+1];
	int pwsize = sizeof(password);

	cons_handle = GetStdHandle(STD_INPUT_HANDLE);
	if (cons_handle == INVALID_HANDLE_VALUE)
		return NULL;
	if (!GetConsoleMode(cons_handle, &old_mode))
		return NULL;

	new_mode = old_mode;
	new_mode |=  ( ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT );
	new_mode &= ~( ENABLE_ECHO_INPUT );

	if (!SetConsoleMode(cons_handle, new_mode))
		return NULL;

	SetConsoleCtrlHandler(&GetPassConsoleControlHandler, TRUE);

	(void) fputs(prompt, stdout);
	(void) fflush(stdout);
	(void) memset(password, 0, pwsize);

	if (fgets(password, pwsize, stdin) == NULL) {
		if (ferror(stdin))
			goto out;
		(void) putchar('\n');
	}
	else {
		(void) putchar('\n');

		if ((ptr = strchr(password, '\n')))
			*ptr = '\0';
		else /* need to flush */
			do {
				scratchchar = getchar();
			} while (scratchchar != EOF && scratchchar != '\n');
	}

out:
	(void) SetConsoleMode(cons_handle, old_mode);
	SetConsoleCtrlHandler(&GetPassConsoleControlHandler, FALSE);

	return password;
}

#else /* !_WIN32 */

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <stdio.h>
#include <signal.h>

#if defined (POSIX) || defined (POSIX_TERMIOS)
#include <termios.h>
static	struct termios ttyo, ttyb;
#define stty(f, t) tcsetattr(f, TCSANOW, t)
#define gtty(f, t) tcgetattr(f, t)
#else
#include <sgtty.h>
static	struct sgttyb ttyo, ttyb;
#endif

#include "ftp_var.h"

static	FILE *fi;

#define sig_t my_sig_t
#define sigtype krb5_sigtype
typedef sigtype (*sig_t)();

static sigtype
intfix(sig)
	int sig;
{
	if (fi != NULL)
		(void) stty(fileno(fi), &ttyo);
	exit(SIGINT);
}

char *
mygetpass(prompt)
char *prompt;
{
	register char *p;
	register int c;
	static char pbuf[50+1];
	sigtype (*sig)();

	if ((fi = fopen("/dev/tty", "r")) == NULL)
		fi = stdin;
	else
		setbuf(fi, (char *)NULL);
	sig = signal(SIGINT, intfix);
	(void) gtty(fileno(fi), &ttyb);
	ttyo = ttyb;
#if defined (POSIX) || defined (POSIX_TERMIOS)
	ttyb.c_lflag &= ~ECHO;
#else
	ttyb.sg_flags &= ~ECHO;
#endif
	(void) stty(fileno(fi), &ttyb);
	fprintf(stderr, "%s", prompt); (void) fflush(stderr);
	for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
		if (p < &pbuf[sizeof(pbuf)-1])
			*p++ = c;
	}
	*p = '\0';
	fprintf(stderr, "\n"); (void) fflush(stderr);
	(void) stty(fileno(fi), &ttyo);
	(void) signal(SIGINT, sig);
	if (fi != stdin)
		(void) fclose(fi);
	return(pbuf);
}

#endif /* !_WIN32 */