1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
5*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
6*0Sstevel@tonic-gate  * are met:
7*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
8*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
9*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
10*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
11*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*0Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*0Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*0Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*0Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*0Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*0Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*0Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*0Sstevel@tonic-gate  */
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #include "includes.h"
26*0Sstevel@tonic-gate RCSID("$OpenBSD: readpass.c,v 1.27 2002/03/26 15:58:46 markus Exp $");
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include "xmalloc.h"
31*0Sstevel@tonic-gate #include "readpass.h"
32*0Sstevel@tonic-gate #include "pathnames.h"
33*0Sstevel@tonic-gate #include "log.h"
34*0Sstevel@tonic-gate #include "ssh.h"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate static char *
37*0Sstevel@tonic-gate ssh_askpass(char *askpass, const char *msg)
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate 	pid_t pid;
40*0Sstevel@tonic-gate 	size_t len;
41*0Sstevel@tonic-gate 	char *pass;
42*0Sstevel@tonic-gate 	int p[2], status, ret;
43*0Sstevel@tonic-gate 	char buf[1024];
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate 	if (fflush(stdout) != 0)
46*0Sstevel@tonic-gate 		error("ssh_askpass: fflush: %s", strerror(errno));
47*0Sstevel@tonic-gate 	if (askpass == NULL)
48*0Sstevel@tonic-gate 		fatal("internal error: askpass undefined");
49*0Sstevel@tonic-gate 	if (pipe(p) < 0) {
50*0Sstevel@tonic-gate 		error("ssh_askpass: pipe: %s", strerror(errno));
51*0Sstevel@tonic-gate 		return xstrdup("");
52*0Sstevel@tonic-gate 	}
53*0Sstevel@tonic-gate 	if ((pid = fork()) < 0) {
54*0Sstevel@tonic-gate 		error("ssh_askpass: fork: %s", strerror(errno));
55*0Sstevel@tonic-gate 		return xstrdup("");
56*0Sstevel@tonic-gate 	}
57*0Sstevel@tonic-gate 	if (pid == 0) {
58*0Sstevel@tonic-gate 		seteuid(getuid());
59*0Sstevel@tonic-gate 		setuid(getuid());
60*0Sstevel@tonic-gate 		close(p[0]);
61*0Sstevel@tonic-gate 		if (dup2(p[1], STDOUT_FILENO) < 0)
62*0Sstevel@tonic-gate 			fatal("ssh_askpass: dup2: %s", strerror(errno));
63*0Sstevel@tonic-gate 		execlp(askpass, askpass, msg, (char *) 0);
64*0Sstevel@tonic-gate 		fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
65*0Sstevel@tonic-gate 	}
66*0Sstevel@tonic-gate 	close(p[1]);
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	len = ret = 0;
69*0Sstevel@tonic-gate 	do {
70*0Sstevel@tonic-gate 		ret = read(p[0], buf + len, sizeof(buf) - 1 - len);
71*0Sstevel@tonic-gate 		if (ret == -1 && errno == EINTR)
72*0Sstevel@tonic-gate 			continue;
73*0Sstevel@tonic-gate 		if (ret <= 0)
74*0Sstevel@tonic-gate 			break;
75*0Sstevel@tonic-gate 		len += ret;
76*0Sstevel@tonic-gate 	} while (sizeof(buf) - 1 - len > 0);
77*0Sstevel@tonic-gate 	buf[len] = '\0';
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	close(p[0]);
80*0Sstevel@tonic-gate 	while (waitpid(pid, &status, 0) < 0)
81*0Sstevel@tonic-gate 		if (errno != EINTR)
82*0Sstevel@tonic-gate 			break;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	buf[strcspn(buf, "\r\n")] = '\0';
85*0Sstevel@tonic-gate 	pass = xstrdup(buf);
86*0Sstevel@tonic-gate 	memset(buf, 0, sizeof(buf));
87*0Sstevel@tonic-gate 	return pass;
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate /*
91*0Sstevel@tonic-gate  * Reads a passphrase from /dev/tty with echo turned off/on.  Returns the
92*0Sstevel@tonic-gate  * passphrase (allocated with xmalloc).  Exits if EOF is encountered. If
93*0Sstevel@tonic-gate  * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
94*0Sstevel@tonic-gate  * tty is available
95*0Sstevel@tonic-gate  */
96*0Sstevel@tonic-gate char *
97*0Sstevel@tonic-gate read_passphrase(const char *prompt, int flags)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	char *askpass = NULL, *ret, buf[1024];
100*0Sstevel@tonic-gate 	int rppflags, use_askpass = 0, ttyfd;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
103*0Sstevel@tonic-gate 	if (flags & RP_ALLOW_STDIN) {
104*0Sstevel@tonic-gate 		if (!isatty(STDIN_FILENO))
105*0Sstevel@tonic-gate 			use_askpass = 1;
106*0Sstevel@tonic-gate 	} else {
107*0Sstevel@tonic-gate 		rppflags |= RPP_REQUIRE_TTY;
108*0Sstevel@tonic-gate 		ttyfd = open(_PATH_TTY, O_RDWR);
109*0Sstevel@tonic-gate 		if (ttyfd >= 0)
110*0Sstevel@tonic-gate 			close(ttyfd);
111*0Sstevel@tonic-gate 		else
112*0Sstevel@tonic-gate 			use_askpass = 1;
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	if (use_askpass && getenv("DISPLAY")) {
116*0Sstevel@tonic-gate 		if (getenv(SSH_ASKPASS_ENV))
117*0Sstevel@tonic-gate 			askpass = getenv(SSH_ASKPASS_ENV);
118*0Sstevel@tonic-gate 		else
119*0Sstevel@tonic-gate 			askpass = _PATH_SSH_ASKPASS_DEFAULT;
120*0Sstevel@tonic-gate 		return ssh_askpass(askpass, prompt);
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
124*0Sstevel@tonic-gate 		if (flags & RP_ALLOW_EOF)
125*0Sstevel@tonic-gate 			return NULL;
126*0Sstevel@tonic-gate 		return xstrdup("");
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	ret = xstrdup(buf);
130*0Sstevel@tonic-gate 	memset(buf, 'x', sizeof buf);
131*0Sstevel@tonic-gate 	return ret;
132*0Sstevel@tonic-gate }
133