sshpty.c 6.19 KB
Newer Older
1
/* $OpenBSD: sshpty.c,v 1.28 2007/09/11 23:49:09 stevesk Exp $ */
Damien Miller's avatar
Damien Miller committed
2
/*
3 4 5 6
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
 *                    All rights reserved
 * Allocating a pseudo-terminal, and making it the controlling tty.
7
 *
8 9 10 11 12
 * As far as I am concerned, the code I have written for this software
 * can be used freely for any purpose.  Any derived versions of this
 * software must be clearly marked as such, and if the derived work is
 * incompatible with the protocol description in the RFC file, it must be
 * called by a name other than "ssh" or "Secure Shell".
13
 */
Damien Miller's avatar
Damien Miller committed
14 15

#include "includes.h"
16

17
#include <sys/types.h>
18
#include <sys/ioctl.h>
19
#include <sys/stat.h>
20
#include <signal.h>
21

22
#include <errno.h>
23
#include <fcntl.h>
24
#include <grp.h>
25
#ifdef HAVE_PATHS_H
Damien Miller's avatar
Damien Miller committed
26
# include <paths.h>
27
#endif
28
#include <pwd.h>
29
#include <stdarg.h>
30
#include <string.h>
31
#include <termios.h>
32 33
#ifdef HAVE_UTIL_H
# include <util.h>
34
#endif
35
#include <unistd.h>
36

37
#include "sshpty.h"
38
#include "log.h"
39
#include "misc.h"
Damien Miller's avatar
Damien Miller committed
40

41 42 43 44
#ifdef HAVE_PTY_H
# include <pty.h>
#endif

Damien Miller's avatar
Damien Miller committed
45 46 47 48
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif

49 50 51 52 53 54 55
#ifdef __APPLE__
# include <AvailabilityMacros.h>
# if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
#  define __APPLE_PRIVPTY__
# endif
#endif

56 57 58 59 60 61
/*
 * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
 * nonzero if a pty was successfully allocated.  On success, open file
 * descriptors for the pty and tty sides and the name of the tty side are
 * returned (the buffer must be able to hold at least 64 characters).
 */
Damien Miller's avatar
Damien Miller committed
62

63
int
64
pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
Damien Miller's avatar
Damien Miller committed
65
{
66
	/* openpty(3) exists in OSF/1 and some other os'es */
67
	char *name;
68
	int i;
Damien Miller's avatar
Damien Miller committed
69

70
	i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
71 72 73 74
	if (i < 0) {
		error("openpty: %.100s", strerror(errno));
		return 0;
	}
75 76 77 78 79
	name = ttyname(*ttyfd);
	if (!name)
		fatal("openpty returns device for which ttyname fails.");

	strlcpy(namebuf, name, namebuflen);	/* possible truncation */
80
	return 1;
Damien Miller's avatar
Damien Miller committed
81 82
}

83
/* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
Damien Miller's avatar
Damien Miller committed
84

85
void
86
pty_release(const char *tty)
Damien Miller's avatar
Damien Miller committed
87
{
88
#ifndef __APPLE_PRIVPTY__
89 90 91 92
	if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
	if (chmod(tty, (mode_t) 0666) < 0)
		error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
93
#endif /* __APPLE_PRIVPTY__ */
Damien Miller's avatar
Damien Miller committed
94 95
}

96
/* Makes the tty the process's controlling tty and sets it to sane modes. */
Damien Miller's avatar
Damien Miller committed
97

98
void
99
pty_make_controlling_tty(int *ttyfd, const char *tty)
Damien Miller's avatar
Damien Miller committed
100
{
101
	int fd;
102 103 104
#ifdef USE_VHANGUP
	void *old;
#endif /* USE_VHANGUP */
105

106
#ifdef _UNICOS
107 108
	if (setsid() < 0)
		error("setsid: %.100s", strerror(errno));
109

110
	fd = open(tty, O_RDWR|O_NOCTTY);
111
	if (fd != -1) {
112
		signal(SIGHUP, SIG_IGN);
113
		ioctl(fd, TCVHUP, (char *)NULL);
114
		signal(SIGHUP, SIG_DFL);
115 116
		setpgid(0, 0);
		close(fd);
117
	} else {
118
		error("Failed to disconnect from controlling tty.");
119 120
	}

121 122 123 124
	debug("Setting controlling tty using TCSETCTTY.");
	ioctl(*ttyfd, TCSETCTTY, NULL);
	fd = open("/dev/tty", O_RDWR);
	if (fd < 0)
125
		error("%.100s: %.100s", tty, strerror(errno));
126
	close(*ttyfd);
Ben Lindstrom's avatar
Ben Lindstrom committed
127
	*ttyfd = fd;
128
#else /* _UNICOS */
Damien Miller's avatar
Damien Miller committed
129

130
	/* First disconnect from the old controlling tty. */
Damien Miller's avatar
Damien Miller committed
131
#ifdef TIOCNOTTY
132
	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
133 134 135 136
	if (fd >= 0) {
		(void) ioctl(fd, TIOCNOTTY, NULL);
		close(fd);
	}
Damien Miller's avatar
Damien Miller committed
137
#endif /* TIOCNOTTY */
138 139 140
	if (setsid() < 0)
		error("setsid: %.100s", strerror(errno));

141 142 143 144
	/*
	 * Verify that we are successfully disconnected from the controlling
	 * tty.
	 */
145
	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
146 147 148 149 150
	if (fd >= 0) {
		error("Failed to disconnect from controlling tty.");
		close(fd);
	}
	/* Make it our controlling tty. */
Damien Miller's avatar
Damien Miller committed
151
#ifdef TIOCSCTTY
152
	debug("Setting controlling tty using TIOCSCTTY.");
153 154
	if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
Damien Miller's avatar
Damien Miller committed
155
#endif /* TIOCSCTTY */
156
#ifdef NEED_SETPGRP
157 158
	if (setpgrp(0,0) < 0)
		error("SETPGRP %s",strerror(errno));
159
#endif /* NEED_SETPGRP */
160
#ifdef USE_VHANGUP
161
	old = signal(SIGHUP, SIG_IGN);
162
	vhangup();
163
	signal(SIGHUP, old);
164
#endif /* USE_VHANGUP */
165
	fd = open(tty, O_RDWR);
166
	if (fd < 0) {
167
		error("%.100s: %.100s", tty, strerror(errno));
168
	} else {
169
#ifdef USE_VHANGUP
170 171
		close(*ttyfd);
		*ttyfd = fd;
172
#else /* USE_VHANGUP */
173
		close(fd);
174
#endif /* USE_VHANGUP */
175
	}
176
	/* Verify that we now have a controlling tty. */
177
	fd = open(_PATH_TTY, O_WRONLY);
178 179
	if (fd < 0)
		error("open /dev/tty failed - could not set controlling tty: %.100s",
180
		    strerror(errno));
181
	else
182
		close(fd);
183
#endif /* _UNICOS */
Damien Miller's avatar
Damien Miller committed
184 185 186 187
}

/* Changes the window size associated with the pty. */

188
void
189 190
pty_change_window_size(int ptyfd, u_int row, u_int col,
	u_int xpixel, u_int ypixel)
Damien Miller's avatar
Damien Miller committed
191
{
192
	struct winsize w;
193

194
	/* may truncate u_int -> u_short */
195 196 197 198 199
	w.ws_row = row;
	w.ws_col = col;
	w.ws_xpixel = xpixel;
	w.ws_ypixel = ypixel;
	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Miller's avatar
Damien Miller committed
200
}
201 202

void
203
pty_setowner(struct passwd *pw, const char *tty)
204 205 206 207
{
	struct group *grp;
	gid_t gid;
	mode_t mode;
208
	struct stat st;
209 210 211 212 213 214 215 216 217 218 219

	/* Determine the group to make the owner of the tty. */
	grp = getgrnam("tty");
	if (grp) {
		gid = grp->gr_gid;
		mode = S_IRUSR | S_IWUSR | S_IWGRP;
	} else {
		gid = pw->pw_gid;
		mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
	}

220 221
	/*
	 * Change owner and mode of the tty as required.
222 223
	 * Warn but continue if filesystem is read-only and the uids match/
	 * tty is owned by root.
224
	 */
225 226
	if (stat(tty, &st))
		fatal("stat(%.100s) failed: %.100s", tty,
227 228
		    strerror(errno));

229 230 231 232
#ifdef WITH_SELINUX
	ssh_selinux_setup_pty(pw->pw_name, tty);
#endif

233
	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
234
		if (chown(tty, pw->pw_uid, gid) < 0) {
235
			if (errno == EROFS &&
236
			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
237
				debug("chown(%.100s, %u, %u) failed: %.100s",
238
				    tty, (u_int)pw->pw_uid, (u_int)gid,
239
				    strerror(errno));
240
			else
241
				fatal("chown(%.100s, %u, %u) failed: %.100s",
242
				    tty, (u_int)pw->pw_uid, (u_int)gid,
243
				    strerror(errno));
244 245 246 247
		}
	}

	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
248
		if (chmod(tty, mode) < 0) {
249 250
			if (errno == EROFS &&
			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
251
				debug("chmod(%.100s, 0%o) failed: %.100s",
252
				    tty, (u_int)mode, strerror(errno));
253 254
			else
				fatal("chmod(%.100s, 0%o) failed: %.100s",
255
				    tty, (u_int)mode, strerror(errno));
256 257
		}
	}
258
}