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: pty.c,v 1.22 2001/02/08 19:30:52 itojun Exp $");
19 #endif /* HAVE_UTIL_H */
24 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
25 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
32 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
33 # include <sys/stropts.h>
41 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
42 * nonzero if a pty was successfully allocated. On success, open file
43 * descriptors for the pty and tty sides and the name of the tty side are
44 * returned (the buffer must be able to hold at least 64 characters).
48 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
50 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
51 /* openpty(3) exists in OSF/1 and some other os'es */
55 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
57 error("openpty: %.100s", strerror(errno));
60 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
62 #else /* HAVE_OPENPTY */
65 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
66 * pty's automagically when needed
70 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
72 error("_getpty: %.100s", strerror(errno));
75 strlcpy(namebuf, slave, namebuflen);
76 /* Open the slave side. */
77 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
79 error("%.200s: %.100s", namebuf, strerror(errno));
84 #else /* HAVE__GETPTY */
85 #if defined(HAVE_DEV_PTMX)
87 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
88 * also has bsd-style ptys, but they simply do not work.)
93 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
95 error("/dev/ptmx: %.100s", strerror(errno));
98 if (grantpt(ptm) < 0) {
99 error("grantpt: %.100s", strerror(errno));
102 if (unlockpt(ptm) < 0) {
103 error("unlockpt: %.100s", strerror(errno));
108 error("Slave pty side name could not be obtained.");
109 strlcpy(namebuf, pts, namebuflen);
112 /* Open the slave side. */
113 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
115 error("%.100s: %.100s", namebuf, strerror(errno));
121 * Push the appropriate streams modules, as described in Solaris pts(7).
122 * HP-UX pts(7) doesn't have ttcompat module.
124 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
125 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
126 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
127 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
129 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
130 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
134 #else /* HAVE_DEV_PTMX */
135 #ifdef HAVE_DEV_PTS_AND_PTC
136 /* AIX-style pty code. */
139 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
141 error("Could not open /dev/ptc: %.100s", strerror(errno));
144 name = ttyname(*ptyfd);
146 fatal("Open of /dev/ptc returns device for which ttyname fails.");
147 strlcpy(namebuf, name, namebuflen);
148 *ttyfd = open(name, O_RDWR | O_NOCTTY);
150 error("Could not open pty slave side %.100s: %.100s",
151 name, strerror(errno));
156 #else /* HAVE_DEV_PTS_AND_PTC */
157 /* BSD-style pty code. */
160 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
161 const char *ptyminors = "0123456789abcdef";
162 int num_minors = strlen(ptyminors);
163 int num_ptys = strlen(ptymajors) * num_minors;
165 for (i = 0; i < num_ptys; i++) {
166 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
167 ptyminors[i % num_minors]);
168 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
169 ptymajors[i / num_minors], ptyminors[i % num_minors]);
171 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
173 /* Try SCO style naming */
174 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
175 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
176 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
181 /* Open the slave side. */
182 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
184 error("%.100s: %.100s", namebuf, strerror(errno));
191 #endif /* HAVE_DEV_PTS_AND_PTC */
192 #endif /* HAVE_DEV_PTMX */
193 #endif /* HAVE__GETPTY */
194 #endif /* HAVE_OPENPTY */
197 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
200 pty_release(const char *ttyname)
202 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
203 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
204 if (chmod(ttyname, (mode_t) 0666) < 0)
205 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
208 /* Makes the tty the processes controlling tty and sets it to sane modes. */
211 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
216 #endif /* USE_VHANGUP */
218 /* First disconnect from the old controlling tty. */
220 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
222 (void) ioctl(fd, TIOCNOTTY, NULL);
225 #endif /* TIOCNOTTY */
227 error("setsid: %.100s", strerror(errno));
230 * Verify that we are successfully disconnected from the controlling
233 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
235 error("Failed to disconnect from controlling tty.");
238 /* Make it our controlling tty. */
240 debug("Setting controlling tty using TIOCSCTTY.");
241 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
242 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
243 #endif /* TIOCSCTTY */
245 if (setpgrp(0,0) < 0)
246 error("SETPGRP %s",strerror(errno));
247 #endif /* HAVE_NEWS4 */
249 old = signal(SIGHUP, SIG_IGN);
252 #endif /* USE_VHANGUP */
253 fd = open(ttyname, O_RDWR);
255 error("%.100s: %.100s", ttyname, strerror(errno));
260 #else /* USE_VHANGUP */
262 #endif /* USE_VHANGUP */
264 /* Verify that we now have a controlling tty. */
265 fd = open(_PATH_TTY, O_WRONLY);
267 error("open /dev/tty failed - could not set controlling tty: %.100s",
274 /* Changes the window size associated with the pty. */
277 pty_change_window_size(int ptyfd, int row, int col,
278 int xpixel, int ypixel)
283 w.ws_xpixel = xpixel;
284 w.ws_ypixel = ypixel;
285 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
289 pty_setowner(struct passwd *pw, const char *ttyname)
296 /* Determine the group to make the owner of the tty. */
297 grp = getgrnam("tty");
300 mode = S_IRUSR | S_IWUSR | S_IWGRP;
303 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
307 * Change owner and mode of the tty as required.
308 * Warn but continue if filesystem is read-only and the uids match.
310 if (stat(ttyname, &st))
311 fatal("stat(%.100s) failed: %.100s", ttyname,
314 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
315 if (chown(ttyname, pw->pw_uid, gid) < 0) {
316 if (errno == EROFS && st.st_uid == pw->pw_uid)
317 error("chown(%.100s, %d, %d) failed: %.100s",
318 ttyname, pw->pw_uid, gid,
321 fatal("chown(%.100s, %d, %d) failed: %.100s",
322 ttyname, pw->pw_uid, gid,
327 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
328 if (chmod(ttyname, mode) < 0) {
329 if (errno == EROFS &&
330 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
331 error("chmod(%.100s, 0%o) failed: %.100s",
332 ttyname, mode, strerror(errno));
334 fatal("chmod(%.100s, 0%o) failed: %.100s",
335 ttyname, mode, strerror(errno));