1/* $OpenBSD: sshpty.c,v 1.28 2007/09/11 23:49:09 stevesk Exp $ */
2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 *                    All rights reserved
6 * Allocating a pseudo-terminal, and making it the controlling tty.
7 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose.  Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 */
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
26# include <paths.h>
27#endif
28#include <pwd.h>
29#include <stdarg.h>
30#include <string.h>
31#include <termios.h>
32#ifdef HAVE_UTIL_H
33# include <util.h>
34#endif
35#include <unistd.h>
36
37#include "sshpty.h"
38#include "log.h"
39#include "misc.h"
40
41#ifdef HAVE_PTY_H
42# include <pty.h>
43#endif
44
45#ifndef O_NOCTTY
46#define O_NOCTTY 0
47#endif
48
49#ifdef __APPLE__
50# include <AvailabilityMacros.h>
51# if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
52#  define __APPLE_PRIVPTY__
53# endif
54#endif
55
56/*
57 * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
58 * nonzero if a pty was successfully allocated.  On success, open file
59 * descriptors for the pty and tty sides and the name of the tty side are
60 * returned (the buffer must be able to hold at least 64 characters).
61 */
62
63int
64pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
65{
66	/* openpty(3) exists in OSF/1 and some other os'es */
67	char *name;
68	int i;
69
70	i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
71	if (i < 0) {
72		error("openpty: %.100s", strerror(errno));
73		return 0;
74	}
75#ifdef ANDROID
76	/* Android does not have a working ttyname() */
77	name = "/dev/ptmx";
78#else
79	name = ttyname(*ttyfd);
80	if (!name)
81		fatal("openpty returns device for which ttyname fails.");
82#endif
83
84	strlcpy(namebuf, name, namebuflen);	/* possible truncation */
85	return 1;
86}
87
88/* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
89
90void
91pty_release(const char *tty)
92{
93#ifndef __APPLE_PRIVPTY__
94	if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
95		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
96	if (chmod(tty, (mode_t) 0666) < 0)
97		error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
98#endif /* __APPLE_PRIVPTY__ */
99}
100
101/* Makes the tty the process's controlling tty and sets it to sane modes. */
102
103void
104pty_make_controlling_tty(int *ttyfd, const char *tty)
105{
106	int fd;
107#ifdef USE_VHANGUP
108	void *old;
109#endif /* USE_VHANGUP */
110
111#ifdef _UNICOS
112	if (setsid() < 0)
113		error("setsid: %.100s", strerror(errno));
114
115	fd = open(tty, O_RDWR|O_NOCTTY);
116	if (fd != -1) {
117		signal(SIGHUP, SIG_IGN);
118		ioctl(fd, TCVHUP, (char *)NULL);
119		signal(SIGHUP, SIG_DFL);
120		setpgid(0, 0);
121		close(fd);
122	} else {
123		error("Failed to disconnect from controlling tty.");
124	}
125
126	debug("Setting controlling tty using TCSETCTTY.");
127	ioctl(*ttyfd, TCSETCTTY, NULL);
128	fd = open("/dev/tty", O_RDWR);
129	if (fd < 0)
130		error("%.100s: %.100s", tty, strerror(errno));
131	close(*ttyfd);
132	*ttyfd = fd;
133#else /* _UNICOS */
134
135	/* First disconnect from the old controlling tty. */
136#ifdef TIOCNOTTY
137	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
138	if (fd >= 0) {
139		(void) ioctl(fd, TIOCNOTTY, NULL);
140		close(fd);
141	}
142#endif /* TIOCNOTTY */
143	if (setsid() < 0)
144		error("setsid: %.100s", strerror(errno));
145
146	/*
147	 * Verify that we are successfully disconnected from the controlling
148	 * tty.
149	 */
150	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
151	if (fd >= 0) {
152		error("Failed to disconnect from controlling tty.");
153		close(fd);
154	}
155	/* Make it our controlling tty. */
156#ifdef TIOCSCTTY
157	debug("Setting controlling tty using TIOCSCTTY.");
158	if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
159		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
160#endif /* TIOCSCTTY */
161#ifdef NEED_SETPGRP
162	if (setpgrp(0,0) < 0)
163		error("SETPGRP %s",strerror(errno));
164#endif /* NEED_SETPGRP */
165#ifdef USE_VHANGUP
166	old = signal(SIGHUP, SIG_IGN);
167	vhangup();
168	signal(SIGHUP, old);
169#endif /* USE_VHANGUP */
170	fd = open(tty, O_RDWR);
171	if (fd < 0) {
172		error("%.100s: %.100s", tty, strerror(errno));
173	} else {
174#ifdef USE_VHANGUP
175		close(*ttyfd);
176		*ttyfd = fd;
177#else /* USE_VHANGUP */
178		close(fd);
179#endif /* USE_VHANGUP */
180	}
181	/* Verify that we now have a controlling tty. */
182	fd = open(_PATH_TTY, O_WRONLY);
183	if (fd < 0)
184		error("open /dev/tty failed - could not set controlling tty: %.100s",
185		    strerror(errno));
186	else
187		close(fd);
188#endif /* _UNICOS */
189}
190
191/* Changes the window size associated with the pty. */
192
193void
194pty_change_window_size(int ptyfd, u_int row, u_int col,
195	u_int xpixel, u_int ypixel)
196{
197	struct winsize w;
198
199	/* may truncate u_int -> u_short */
200	w.ws_row = row;
201	w.ws_col = col;
202	w.ws_xpixel = xpixel;
203	w.ws_ypixel = ypixel;
204	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
205}
206
207void
208pty_setowner(struct passwd *pw, const char *tty)
209{
210	struct group *grp;
211	gid_t gid;
212	mode_t mode;
213	struct stat st;
214
215	/* Determine the group to make the owner of the tty. */
216	grp = getgrnam("tty");
217	if (grp) {
218		gid = grp->gr_gid;
219		mode = S_IRUSR | S_IWUSR | S_IWGRP;
220	} else {
221		gid = pw->pw_gid;
222		mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
223	}
224
225	/*
226	 * Change owner and mode of the tty as required.
227	 * Warn but continue if filesystem is read-only and the uids match/
228	 * tty is owned by root.
229	 */
230	if (stat(tty, &st))
231		fatal("stat(%.100s) failed: %.100s", tty,
232		    strerror(errno));
233
234#ifdef WITH_SELINUX
235	ssh_selinux_setup_pty(pw->pw_name, tty);
236#endif
237
238	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
239		if (chown(tty, pw->pw_uid, gid) < 0) {
240			if (errno == EROFS &&
241			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
242				debug("chown(%.100s, %u, %u) failed: %.100s",
243				    tty, (u_int)pw->pw_uid, (u_int)gid,
244				    strerror(errno));
245			else
246				fatal("chown(%.100s, %u, %u) failed: %.100s",
247				    tty, (u_int)pw->pw_uid, (u_int)gid,
248				    strerror(errno));
249		}
250	}
251
252	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
253		if (chmod(tty, mode) < 0) {
254			if (errno == EROFS &&
255			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
256				debug("chmod(%.100s, 0%o) failed: %.100s",
257				    tty, (u_int)mode, strerror(errno));
258			else
259				fatal("chmod(%.100s, 0%o) failed: %.100s",
260				    tty, (u_int)mode, strerror(errno));
261		}
262	}
263}
264