xref: /freebsd/crypto/openssh/sshpty.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
5  * Allocating a pseudo-terminal, and making it the controlling tty.
6  *
7  * As far as I am concerned, the code I have written for this software
8  * can be used freely for any purpose.  Any derived versions of this
9  * software must be clearly marked as such, and if the derived work is
10  * incompatible with the protocol description in the RFC file, it must be
11  * called by a name other than "ssh" or "Secure Shell".
12  */
13 
14 #include "includes.h"
15 RCSID("$OpenBSD: sshpty.c,v 1.1 2001/03/04 01:46:30 djm Exp $");
16 RCSID("$FreeBSD$");
17 
18 #include <libutil.h>
19 #include "sshpty.h"
20 #include "log.h"
21 
22 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
23 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
24 #undef HAVE_DEV_PTMX
25 #endif
26 
27 #ifndef O_NOCTTY
28 #define O_NOCTTY 0
29 #endif
30 
31 /*
32  * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
33  * nonzero if a pty was successfully allocated.  On success, open file
34  * descriptors for the pty and tty sides and the name of the tty side are
35  * returned (the buffer must be able to hold at least 64 characters).
36  */
37 
38 int
39 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
40 {
41 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
42 	/* openpty(3) exists in OSF/1 and some other os'es */
43 	char buf[64];
44 	int i;
45 
46 	i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
47 	if (i < 0) {
48 		error("openpty: %.100s", strerror(errno));
49 		return 0;
50 	}
51 	strlcpy(namebuf, buf, namebuflen);	/* possible truncation */
52 	return 1;
53 #else /* HAVE_OPENPTY */
54 #ifdef HAVE__GETPTY
55 	/*
56 	 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
57 	 * pty's automagically when needed
58 	 */
59 	char *slave;
60 
61 	slave = _getpty(ptyfd, O_RDWR, 0622, 0);
62 	if (slave == NULL) {
63 		error("_getpty: %.100s", strerror(errno));
64 		return 0;
65 	}
66 	strlcpy(namebuf, slave, namebuflen);
67 	/* Open the slave side. */
68 	*ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
69 	if (*ttyfd < 0) {
70 		error("%.200s: %.100s", namebuf, strerror(errno));
71 		close(*ptyfd);
72 		return 0;
73 	}
74 	return 1;
75 #else /* HAVE__GETPTY */
76 #ifdef HAVE_DEV_PTMX
77 	/*
78 	 * This code is used e.g. on Solaris 2.x.  (Note that Solaris 2.3
79 	 * also has bsd-style ptys, but they simply do not work.)
80 	 */
81 	int ptm;
82 	char *pts;
83 
84 	ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
85 	if (ptm < 0) {
86 		error("/dev/ptmx: %.100s", strerror(errno));
87 		return 0;
88 	}
89 	if (grantpt(ptm) < 0) {
90 		error("grantpt: %.100s", strerror(errno));
91 		return 0;
92 	}
93 	if (unlockpt(ptm) < 0) {
94 		error("unlockpt: %.100s", strerror(errno));
95 		return 0;
96 	}
97 	pts = ptsname(ptm);
98 	if (pts == NULL)
99 		error("Slave pty side name could not be obtained.");
100 	strlcpy(namebuf, pts, namebuflen);
101 	*ptyfd = ptm;
102 
103 	/* Open the slave side. */
104 	*ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
105 	if (*ttyfd < 0) {
106 		error("%.100s: %.100s", namebuf, strerror(errno));
107 		close(*ptyfd);
108 		return 0;
109 	}
110 	/* Push the appropriate streams modules, as described in Solaris pts(7). */
111 	if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
112 		error("ioctl I_PUSH ptem: %.100s", strerror(errno));
113 	if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
114 		error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
115 	if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
116 		error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
117 	return 1;
118 #else /* HAVE_DEV_PTMX */
119 #ifdef HAVE_DEV_PTS_AND_PTC
120 	/* AIX-style pty code. */
121 	const char *name;
122 
123 	*ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
124 	if (*ptyfd < 0) {
125 		error("Could not open /dev/ptc: %.100s", strerror(errno));
126 		return 0;
127 	}
128 	name = ttyname(*ptyfd);
129 	if (!name)
130 		fatal("Open of /dev/ptc returns device for which ttyname fails.");
131 	strlcpy(namebuf, name, namebuflen);
132 	*ttyfd = open(name, O_RDWR | O_NOCTTY);
133 	if (*ttyfd < 0) {
134 		error("Could not open pty slave side %.100s: %.100s",
135 		      name, strerror(errno));
136 		close(*ptyfd);
137 		return 0;
138 	}
139 	return 1;
140 #else /* HAVE_DEV_PTS_AND_PTC */
141 	/* BSD-style pty code. */
142 	char buf[64];
143 	int i;
144 	const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
145 	const char *ptyminors = "0123456789abcdef";
146 	int num_minors = strlen(ptyminors);
147 	int num_ptys = strlen(ptymajors) * num_minors;
148 
149 	for (i = 0; i < num_ptys; i++) {
150 		snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
151 			 ptyminors[i % num_minors]);
152 		*ptyfd = open(buf, O_RDWR | O_NOCTTY);
153 		if (*ptyfd < 0)
154 			continue;
155 		snprintf(namebuf, namebuflen, "/dev/tty%c%c",
156 		    ptymajors[i / num_minors], ptyminors[i % num_minors]);
157 
158 		/* Open the slave side. */
159 		*ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
160 		if (*ttyfd < 0) {
161 			error("%.100s: %.100s", namebuf, strerror(errno));
162 			close(*ptyfd);
163 			return 0;
164 		}
165 		return 1;
166 	}
167 	return 0;
168 #endif /* HAVE_DEV_PTS_AND_PTC */
169 #endif /* HAVE_DEV_PTMX */
170 #endif /* HAVE__GETPTY */
171 #endif /* HAVE_OPENPTY */
172 }
173 
174 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
175 
176 void
177 pty_release(const char *ttyname)
178 {
179 	if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
180 		error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
181 	if (chmod(ttyname, (mode_t) 0666) < 0)
182 		error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
183 }
184 
185 /* Makes the tty the processes controlling tty and sets it to sane modes. */
186 
187 void
188 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
189 {
190 	int fd;
191 
192 	/* First disconnect from the old controlling tty. */
193 #ifdef TIOCNOTTY
194 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
195 	if (fd >= 0) {
196 		(void) ioctl(fd, TIOCNOTTY, NULL);
197 		close(fd);
198 	}
199 #endif /* TIOCNOTTY */
200 	if (setsid() < 0)
201 		error("setsid: %.100s", strerror(errno));
202 
203 	/*
204 	 * Verify that we are successfully disconnected from the controlling
205 	 * tty.
206 	 */
207 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
208 	if (fd >= 0) {
209 		error("Failed to disconnect from controlling tty.");
210 		close(fd);
211 	}
212 	/* Make it our controlling tty. */
213 #ifdef TIOCSCTTY
214 	debug("Setting controlling tty using TIOCSCTTY.");
215 	if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
216 		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
217 #endif /* TIOCSCTTY */
218 	fd = open(ttyname, O_RDWR);
219 	if (fd < 0)
220 		error("%.100s: %.100s", ttyname, strerror(errno));
221 	else
222 		close(fd);
223 
224 	/* Verify that we now have a controlling tty. */
225 	fd = open(_PATH_TTY, O_WRONLY);
226 	if (fd < 0)
227 		error("open /dev/tty failed - could not set controlling tty: %.100s",
228 		      strerror(errno));
229 	else {
230 		close(fd);
231 	}
232 }
233 
234 /* Changes the window size associated with the pty. */
235 
236 void
237 pty_change_window_size(int ptyfd, int row, int col,
238 		       int xpixel, int ypixel)
239 {
240 	struct winsize w;
241 	w.ws_row = row;
242 	w.ws_col = col;
243 	w.ws_xpixel = xpixel;
244 	w.ws_ypixel = ypixel;
245 	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
246 }
247 
248 void
249 pty_setowner(struct passwd *pw, const char *ttyname)
250 {
251 	struct group *grp;
252 	gid_t gid;
253 	mode_t mode;
254 	struct stat st;
255 
256 	/* Determine the group to make the owner of the tty. */
257 	grp = getgrnam("tty");
258 	if (grp) {
259 		gid = grp->gr_gid;
260 		mode = S_IRUSR | S_IWUSR | S_IWGRP;
261 	} else {
262 		gid = pw->pw_gid;
263 		mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
264 	}
265 
266 	/*
267 	 * Change owner and mode of the tty as required.
268 	 * Warn but continue if filesystem is read-only and the uids match.
269 	 */
270 	if (stat(ttyname, &st))
271 		fatal("stat(%.100s) failed: %.100s", ttyname,
272 		    strerror(errno));
273 
274 	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
275 		if (chown(ttyname, pw->pw_uid, gid) < 0) {
276 			if (errno == EROFS && st.st_uid == pw->pw_uid)
277 				error("chown(%.100s, %d, %d) failed: %.100s",
278 				      ttyname, pw->pw_uid, gid,
279 				      strerror(errno));
280 			else
281 				fatal("chown(%.100s, %d, %d) failed: %.100s",
282 				      ttyname, pw->pw_uid, gid,
283 				      strerror(errno));
284 		}
285 	}
286 
287 	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
288 		if (chmod(ttyname, mode) < 0) {
289 			if (errno == EROFS &&
290 			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
291 				error("chmod(%.100s, 0%o) failed: %.100s",
292 				      ttyname, mode, strerror(errno));
293 			else
294 				fatal("chmod(%.100s, 0%o) failed: %.100s",
295 				      ttyname, mode, strerror(errno));
296 		}
297 	}
298 }
299