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, NULL, NULL, NULL);
57 error("openpty: %.100s", strerror(errno));
60 name = ttyname(*ttyfd);
62 fatal("openpty returns device for which ttyname fails.");
64 strlcpy(namebuf, name, namebuflen); /* possible truncation */
66 #else /* HAVE_OPENPTY */
69 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
70 * pty's automagically when needed
74 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
76 error("_getpty: %.100s", strerror(errno));
79 strlcpy(namebuf, slave, namebuflen);
80 /* Open the slave side. */
81 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
83 error("%.200s: %.100s", namebuf, strerror(errno));
88 #else /* HAVE__GETPTY */
89 #if defined(HAVE_DEV_PTMX)
91 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
92 * also has bsd-style ptys, but they simply do not work.)
97 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
99 error("/dev/ptmx: %.100s", strerror(errno));
102 if (grantpt(ptm) < 0) {
103 error("grantpt: %.100s", strerror(errno));
106 if (unlockpt(ptm) < 0) {
107 error("unlockpt: %.100s", strerror(errno));
112 error("Slave pty side name could not be obtained.");
113 strlcpy(namebuf, pts, namebuflen);
116 /* Open the slave side. */
117 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
119 error("%.100s: %.100s", namebuf, strerror(errno));
125 * Push the appropriate streams modules, as described in Solaris pts(7).
126 * HP-UX pts(7) doesn't have ttcompat module.
128 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
129 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
130 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
131 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
133 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
134 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
138 #else /* HAVE_DEV_PTMX */
139 #ifdef HAVE_DEV_PTS_AND_PTC
140 /* AIX-style pty code. */
143 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
145 error("Could not open /dev/ptc: %.100s", strerror(errno));
148 name = ttyname(*ptyfd);
150 fatal("Open of /dev/ptc returns device for which ttyname fails.");
151 strlcpy(namebuf, name, namebuflen);
152 *ttyfd = open(name, O_RDWR | O_NOCTTY);
154 error("Could not open pty slave side %.100s: %.100s",
155 name, strerror(errno));
160 #else /* HAVE_DEV_PTS_AND_PTC */
161 /* BSD-style pty code. */
164 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
165 const char *ptyminors = "0123456789abcdef";
166 int num_minors = strlen(ptyminors);
167 int num_ptys = strlen(ptymajors) * num_minors;
169 for (i = 0; i < num_ptys; i++) {
170 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
171 ptyminors[i % num_minors]);
172 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
173 ptymajors[i / num_minors], ptyminors[i % num_minors]);
175 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
177 /* Try SCO style naming */
178 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
179 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
180 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
185 /* Open the slave side. */
186 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
188 error("%.100s: %.100s", namebuf, strerror(errno));
195 #endif /* HAVE_DEV_PTS_AND_PTC */
196 #endif /* HAVE_DEV_PTMX */
197 #endif /* HAVE__GETPTY */
198 #endif /* HAVE_OPENPTY */
201 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
204 pty_release(const char *ttyname)
206 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
207 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
208 if (chmod(ttyname, (mode_t) 0666) < 0)
209 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
212 /* Makes the tty the processes controlling tty and sets it to sane modes. */
215 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
220 #endif /* USE_VHANGUP */
222 /* First disconnect from the old controlling tty. */
224 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
226 (void) ioctl(fd, TIOCNOTTY, NULL);
229 #endif /* TIOCNOTTY */
231 error("setsid: %.100s", strerror(errno));
234 * Verify that we are successfully disconnected from the controlling
237 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
239 error("Failed to disconnect from controlling tty.");
242 /* Make it our controlling tty. */
244 debug("Setting controlling tty using TIOCSCTTY.");
245 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
246 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
247 #endif /* TIOCSCTTY */
249 if (setpgrp(0,0) < 0)
250 error("SETPGRP %s",strerror(errno));
251 #endif /* HAVE_NEWS4 */
253 old = signal(SIGHUP, SIG_IGN);
256 #endif /* USE_VHANGUP */
257 fd = open(ttyname, O_RDWR);
259 error("%.100s: %.100s", ttyname, strerror(errno));
264 #else /* USE_VHANGUP */
266 #endif /* USE_VHANGUP */
268 /* Verify that we now have a controlling tty. */
269 fd = open(_PATH_TTY, O_WRONLY);
271 error("open /dev/tty failed - could not set controlling tty: %.100s",
278 /* Changes the window size associated with the pty. */
281 pty_change_window_size(int ptyfd, int row, int col,
282 int xpixel, int ypixel)
287 w.ws_xpixel = xpixel;
288 w.ws_ypixel = ypixel;
289 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
293 pty_setowner(struct passwd *pw, const char *ttyname)
300 /* Determine the group to make the owner of the tty. */
301 grp = getgrnam("tty");
304 mode = S_IRUSR | S_IWUSR | S_IWGRP;
307 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
311 * Change owner and mode of the tty as required.
312 * Warn but continue if filesystem is read-only and the uids match.
314 if (stat(ttyname, &st))
315 fatal("stat(%.100s) failed: %.100s", ttyname,
318 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
319 if (chown(ttyname, pw->pw_uid, gid) < 0) {
320 if (errno == EROFS && st.st_uid == pw->pw_uid)
321 error("chown(%.100s, %d, %d) failed: %.100s",
322 ttyname, pw->pw_uid, gid,
325 fatal("chown(%.100s, %d, %d) failed: %.100s",
326 ttyname, pw->pw_uid, gid,
331 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
332 if (chmod(ttyname, mode) < 0) {
333 if (errno == EROFS &&
334 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
335 error("chmod(%.100s, 0%o) failed: %.100s",
336 ttyname, mode, strerror(errno));
338 fatal("chmod(%.100s, 0%o) failed: %.100s",
339 ttyname, mode, strerror(errno));