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 $");
20 #endif /* HAVE_UTIL_H */
26 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
27 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
34 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
35 # include <sys/stropts.h>
43 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
44 * nonzero if a pty was successfully allocated. On success, open file
45 * descriptors for the pty and tty sides and the name of the tty side are
46 * returned (the buffer must be able to hold at least 64 characters).
50 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
52 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
53 /* openpty(3) exists in OSF/1 and some other os'es */
57 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
59 error("openpty: %.100s", strerror(errno));
62 name = ttyname(*ttyfd);
64 fatal("openpty returns device for which ttyname fails.");
66 strlcpy(namebuf, name, namebuflen); /* possible truncation */
68 #else /* HAVE_OPENPTY */
71 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
72 * pty's automagically when needed
76 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
78 error("_getpty: %.100s", strerror(errno));
81 strlcpy(namebuf, slave, namebuflen);
82 /* Open the slave side. */
83 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
85 error("%.200s: %.100s", namebuf, strerror(errno));
90 #else /* HAVE__GETPTY */
91 #if defined(HAVE_DEV_PTMX)
93 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
94 * also has bsd-style ptys, but they simply do not work.)
100 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
102 error("/dev/ptmx: %.100s", strerror(errno));
105 old_signal = mysignal(SIGCHLD, SIG_DFL);
106 if (grantpt(ptm) < 0) {
107 error("grantpt: %.100s", strerror(errno));
110 mysignal(SIGCHLD, old_signal);
111 if (unlockpt(ptm) < 0) {
112 error("unlockpt: %.100s", strerror(errno));
117 error("Slave pty side name could not be obtained.");
118 strlcpy(namebuf, pts, namebuflen);
121 /* Open the slave side. */
122 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
124 error("%.100s: %.100s", namebuf, strerror(errno));
130 * Push the appropriate streams modules, as described in Solaris pts(7).
131 * HP-UX pts(7) doesn't have ttcompat module.
133 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
134 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
135 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
136 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
138 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
139 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
143 #else /* HAVE_DEV_PTMX */
144 #ifdef HAVE_DEV_PTS_AND_PTC
145 /* AIX-style pty code. */
148 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
150 error("Could not open /dev/ptc: %.100s", strerror(errno));
153 name = ttyname(*ptyfd);
155 fatal("Open of /dev/ptc returns device for which ttyname fails.");
156 strlcpy(namebuf, name, namebuflen);
157 *ttyfd = open(name, O_RDWR | O_NOCTTY);
159 error("Could not open pty slave side %.100s: %.100s",
160 name, strerror(errno));
165 #else /* HAVE_DEV_PTS_AND_PTC */
172 highpty = sysconf(_SC_CRAY_NPTY);
179 for (i = 0; i < highpty; i++) {
180 snprintf(buf, sizeof(buf), "/dev/pty/%03d", i);
181 *ptyfd = open(buf, O_RDWR|O_NOCTTY);
184 snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i);
185 /* Open the slave side. */
186 *ttyfd = open(namebuf, O_RDWR|O_NOCTTY);
188 error("%.100s: %.100s", namebuf, strerror(errno));
196 /* BSD-style pty code. */
199 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
200 const char *ptyminors = "0123456789abcdef";
201 int num_minors = strlen(ptyminors);
202 int num_ptys = strlen(ptymajors) * num_minors;
205 for (i = 0; i < num_ptys; i++) {
206 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
207 ptyminors[i % num_minors]);
208 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
209 ptymajors[i / num_minors], ptyminors[i % num_minors]);
211 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
213 /* Try SCO style naming */
214 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
215 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
216 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
221 /* Open the slave side. */
222 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
224 error("%.100s: %.100s", namebuf, strerror(errno));
228 /* set tty modes to a sane state for broken clients */
229 if (tcgetattr(*ptyfd, &tio) < 0)
230 log("Getting tty modes for pty failed: %.100s", strerror(errno));
232 tio.c_lflag |= (ECHO | ISIG | ICANON);
233 tio.c_oflag |= (OPOST | ONLCR);
234 tio.c_iflag |= ICRNL;
236 /* Set the new modes for the terminal. */
237 if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0)
238 log("Setting tty modes for pty failed: %.100s", strerror(errno));
245 #endif /* HAVE_DEV_PTS_AND_PTC */
246 #endif /* HAVE_DEV_PTMX */
247 #endif /* HAVE__GETPTY */
248 #endif /* HAVE_OPENPTY */
251 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
254 pty_release(const char *ttyname)
256 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
257 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
258 if (chmod(ttyname, (mode_t) 0666) < 0)
259 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
262 /* Makes the tty the processes controlling tty and sets it to sane modes. */
265 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
270 #endif /* USE_VHANGUP */
274 error("setsid: %.100s", strerror(errno));
276 fd = open(ttyname, O_RDWR|O_NOCTTY);
278 mysignal(SIGHUP, SIG_IGN);
279 ioctl(fd, TCVHUP, (char *)NULL);
280 mysignal(SIGHUP, SIG_DFL);
284 error("Failed to disconnect from controlling tty.");
287 debug("Setting controlling tty using TCSETCTTY.");
288 ioctl(*ttyfd, TCSETCTTY, NULL);
289 fd = open("/dev/tty", O_RDWR);
291 error("%.100s: %.100s", ttyname, strerror(errno));
296 /* First disconnect from the old controlling tty. */
298 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
300 (void) ioctl(fd, TIOCNOTTY, NULL);
303 #endif /* TIOCNOTTY */
305 error("setsid: %.100s", strerror(errno));
308 * Verify that we are successfully disconnected from the controlling
311 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
313 error("Failed to disconnect from controlling tty.");
316 /* Make it our controlling tty. */
318 debug("Setting controlling tty using TIOCSCTTY.");
319 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
320 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
321 #endif /* TIOCSCTTY */
323 if (setpgrp(0,0) < 0)
324 error("SETPGRP %s",strerror(errno));
325 #endif /* HAVE_NEWS4 */
327 old = mysignal(SIGHUP, SIG_IGN);
329 mysignal(SIGHUP, old);
330 #endif /* USE_VHANGUP */
331 fd = open(ttyname, O_RDWR);
333 error("%.100s: %.100s", ttyname, strerror(errno));
338 #else /* USE_VHANGUP */
340 #endif /* USE_VHANGUP */
342 /* Verify that we now have a controlling tty. */
343 fd = open(_PATH_TTY, O_WRONLY);
345 error("open /dev/tty failed - could not set controlling tty: %.100s",
352 /* Changes the window size associated with the pty. */
355 pty_change_window_size(int ptyfd, int row, int col,
356 int xpixel, int ypixel)
362 w.ws_xpixel = xpixel;
363 w.ws_ypixel = ypixel;
364 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
368 pty_setowner(struct passwd *pw, const char *ttyname)
375 /* Determine the group to make the owner of the tty. */
376 grp = getgrnam("tty");
379 mode = S_IRUSR | S_IWUSR | S_IWGRP;
382 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
386 * Change owner and mode of the tty as required.
387 * Warn but continue if filesystem is read-only and the uids match/
388 * tty is owned by root.
390 if (stat(ttyname, &st))
391 fatal("stat(%.100s) failed: %.100s", ttyname,
394 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
395 if (chown(ttyname, pw->pw_uid, gid) < 0) {
396 if (errno == EROFS &&
397 (st.st_uid == pw->pw_uid || st.st_uid == 0))
398 error("chown(%.100s, %u, %u) failed: %.100s",
399 ttyname, (u_int)pw->pw_uid, (u_int)gid,
402 fatal("chown(%.100s, %u, %u) failed: %.100s",
403 ttyname, (u_int)pw->pw_uid, (u_int)gid,
408 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
409 if (chmod(ttyname, mode) < 0) {
410 if (errno == EROFS &&
411 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
412 error("chmod(%.100s, 0%o) failed: %.100s",
413 ttyname, mode, strerror(errno));
415 fatal("chmod(%.100s, 0%o) failed: %.100s",
416 ttyname, mode, strerror(errno));