2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Allocating a pseudo-terminal, and making it the controlling tty.
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".
15 RCSID("$OpenBSD: sshpty.c,v 1.7 2002/06/24 17:57:20 deraadt Exp $");
16 RCSID("$FreeBSD: src/crypto/openssh/sshpty.c,v 1.2.2.3 2003/02/03 17:31:08 des Exp $");
17 RCSID("$DragonFly: src/crypto/openssh/Attic/sshpty.c,v 1.2 2003/06/17 04:24:36 dillon Exp $");
21 #endif /* HAVE_UTIL_H */
27 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
28 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
35 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
36 # include <sys/stropts.h>
44 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
45 * nonzero if a pty was successfully allocated. On success, open file
46 * descriptors for the pty and tty sides and the name of the tty side are
47 * returned (the buffer must be able to hold at least 64 characters).
51 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
53 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
54 /* openpty(3) exists in OSF/1 and some other os'es */
58 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
60 error("openpty: %.100s", strerror(errno));
63 name = ttyname(*ttyfd);
65 fatal("openpty returns device for which ttyname fails.");
67 strlcpy(namebuf, name, namebuflen); /* possible truncation */
69 #else /* HAVE_OPENPTY */
72 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
73 * pty's automagically when needed
77 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
79 error("_getpty: %.100s", strerror(errno));
82 strlcpy(namebuf, slave, namebuflen);
83 /* Open the slave side. */
84 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
86 error("%.200s: %.100s", namebuf, strerror(errno));
91 #else /* HAVE__GETPTY */
92 #if defined(HAVE_DEV_PTMX)
94 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
95 * also has bsd-style ptys, but they simply do not work.)
101 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
103 error("/dev/ptmx: %.100s", strerror(errno));
106 old_signal = mysignal(SIGCHLD, SIG_DFL);
107 if (grantpt(ptm) < 0) {
108 error("grantpt: %.100s", strerror(errno));
111 mysignal(SIGCHLD, old_signal);
112 if (unlockpt(ptm) < 0) {
113 error("unlockpt: %.100s", strerror(errno));
118 error("Slave pty side name could not be obtained.");
119 strlcpy(namebuf, pts, namebuflen);
122 /* Open the slave side. */
123 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
125 error("%.100s: %.100s", namebuf, strerror(errno));
131 * Push the appropriate streams modules, as described in Solaris pts(7).
132 * HP-UX pts(7) doesn't have ttcompat module.
134 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
135 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
136 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
137 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
139 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
140 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
144 #else /* HAVE_DEV_PTMX */
145 #ifdef HAVE_DEV_PTS_AND_PTC
146 /* AIX-style pty code. */
149 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
151 error("Could not open /dev/ptc: %.100s", strerror(errno));
154 name = ttyname(*ptyfd);
156 fatal("Open of /dev/ptc returns device for which ttyname fails.");
157 strlcpy(namebuf, name, namebuflen);
158 *ttyfd = open(name, O_RDWR | O_NOCTTY);
160 error("Could not open pty slave side %.100s: %.100s",
161 name, strerror(errno));
166 #else /* HAVE_DEV_PTS_AND_PTC */
173 highpty = sysconf(_SC_CRAY_NPTY);
180 for (i = 0; i < highpty; i++) {
181 snprintf(buf, sizeof(buf), "/dev/pty/%03d", i);
182 *ptyfd = open(buf, O_RDWR|O_NOCTTY);
185 snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i);
186 /* Open the slave side. */
187 *ttyfd = open(namebuf, O_RDWR|O_NOCTTY);
189 error("%.100s: %.100s", namebuf, strerror(errno));
197 /* BSD-style pty code. */
200 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
201 const char *ptyminors = "0123456789abcdef";
202 int num_minors = strlen(ptyminors);
203 int num_ptys = strlen(ptymajors) * num_minors;
206 for (i = 0; i < num_ptys; i++) {
207 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
208 ptyminors[i % num_minors]);
209 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
210 ptymajors[i / num_minors], ptyminors[i % num_minors]);
212 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
214 /* Try SCO style naming */
215 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
216 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
217 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
222 /* Open the slave side. */
223 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
225 error("%.100s: %.100s", namebuf, strerror(errno));
229 /* set tty modes to a sane state for broken clients */
230 if (tcgetattr(*ptyfd, &tio) < 0)
231 log("Getting tty modes for pty failed: %.100s", strerror(errno));
233 tio.c_lflag |= (ECHO | ISIG | ICANON);
234 tio.c_oflag |= (OPOST | ONLCR);
235 tio.c_iflag |= ICRNL;
237 /* Set the new modes for the terminal. */
238 if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0)
239 log("Setting tty modes for pty failed: %.100s", strerror(errno));
246 #endif /* HAVE_DEV_PTS_AND_PTC */
247 #endif /* HAVE_DEV_PTMX */
248 #endif /* HAVE__GETPTY */
249 #endif /* HAVE_OPENPTY */
252 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
255 pty_release(const char *ttyname)
257 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
258 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
259 if (chmod(ttyname, (mode_t) 0666) < 0)
260 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
263 /* Makes the tty the processes controlling tty and sets it to sane modes. */
266 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
271 #endif /* USE_VHANGUP */
275 error("setsid: %.100s", strerror(errno));
277 fd = open(ttyname, O_RDWR|O_NOCTTY);
279 mysignal(SIGHUP, SIG_IGN);
280 ioctl(fd, TCVHUP, (char *)NULL);
281 mysignal(SIGHUP, SIG_DFL);
285 error("Failed to disconnect from controlling tty.");
288 debug("Setting controlling tty using TCSETCTTY.");
289 ioctl(*ttyfd, TCSETCTTY, NULL);
290 fd = open("/dev/tty", O_RDWR);
292 error("%.100s: %.100s", ttyname, strerror(errno));
297 /* First disconnect from the old controlling tty. */
299 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
301 (void) ioctl(fd, TIOCNOTTY, NULL);
304 #endif /* TIOCNOTTY */
306 error("setsid: %.100s", strerror(errno));
309 * Verify that we are successfully disconnected from the controlling
312 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
314 error("Failed to disconnect from controlling tty.");
317 /* Make it our controlling tty. */
319 debug("Setting controlling tty using TIOCSCTTY.");
320 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
321 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
322 #endif /* TIOCSCTTY */
324 if (setpgrp(0,0) < 0)
325 error("SETPGRP %s",strerror(errno));
326 #endif /* HAVE_NEWS4 */
328 old = mysignal(SIGHUP, SIG_IGN);
330 mysignal(SIGHUP, old);
331 #endif /* USE_VHANGUP */
332 fd = open(ttyname, O_RDWR);
334 error("%.100s: %.100s", ttyname, strerror(errno));
339 #else /* USE_VHANGUP */
341 #endif /* USE_VHANGUP */
343 /* Verify that we now have a controlling tty. */
344 fd = open(_PATH_TTY, O_WRONLY);
346 error("open /dev/tty failed - could not set controlling tty: %.100s",
353 /* Changes the window size associated with the pty. */
356 pty_change_window_size(int ptyfd, int row, int col,
357 int xpixel, int ypixel)
363 w.ws_xpixel = xpixel;
364 w.ws_ypixel = ypixel;
365 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
369 pty_setowner(struct passwd *pw, const char *ttyname)
376 /* Determine the group to make the owner of the tty. */
377 grp = getgrnam("tty");
380 mode = S_IRUSR | S_IWUSR | S_IWGRP;
383 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
387 * Change owner and mode of the tty as required.
388 * Warn but continue if filesystem is read-only and the uids match/
389 * tty is owned by root.
391 if (stat(ttyname, &st))
392 fatal("stat(%.100s) failed: %.100s", ttyname,
395 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
396 if (chown(ttyname, pw->pw_uid, gid) < 0) {
397 if (errno == EROFS &&
398 (st.st_uid == pw->pw_uid || st.st_uid == 0))
399 error("chown(%.100s, %u, %u) failed: %.100s",
400 ttyname, (u_int)pw->pw_uid, (u_int)gid,
403 fatal("chown(%.100s, %u, %u) failed: %.100s",
404 ttyname, (u_int)pw->pw_uid, (u_int)gid,
409 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
410 if (chmod(ttyname, mode) < 0) {
411 if (errno == EROFS &&
412 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
413 error("chmod(%.100s, 0%o) failed: %.100s",
414 ttyname, mode, strerror(errno));
416 fatal("chmod(%.100s, 0%o) failed: %.100s",
417 ttyname, mode, strerror(errno));