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.3 2001/07/22 21:32:27 markus Exp $");
19 #endif /* HAVE_UTIL_H */
25 /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
26 #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
33 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
34 # include <sys/stropts.h>
42 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
43 * nonzero if a pty was successfully allocated. On success, open file
44 * descriptors for the pty and tty sides and the name of the tty side are
45 * returned (the buffer must be able to hold at least 64 characters).
49 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
51 #if defined(HAVE_OPENPTY) || defined(BSD4_4)
52 /* openpty(3) exists in OSF/1 and some other os'es */
56 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
58 error("openpty: %.100s", strerror(errno));
61 name = ttyname(*ttyfd);
63 fatal("openpty returns device for which ttyname fails.");
65 strlcpy(namebuf, name, namebuflen); /* possible truncation */
67 #else /* HAVE_OPENPTY */
70 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
71 * pty's automagically when needed
75 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
77 error("_getpty: %.100s", strerror(errno));
80 strlcpy(namebuf, slave, namebuflen);
81 /* Open the slave side. */
82 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
84 error("%.200s: %.100s", namebuf, strerror(errno));
89 #else /* HAVE__GETPTY */
90 #if defined(HAVE_DEV_PTMX)
92 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
93 * also has bsd-style ptys, but they simply do not work.)
99 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
101 error("/dev/ptmx: %.100s", strerror(errno));
104 old_signal = mysignal(SIGCHLD, SIG_DFL);
105 if (grantpt(ptm) < 0) {
106 error("grantpt: %.100s", strerror(errno));
109 mysignal(SIGCHLD, old_signal);
110 if (unlockpt(ptm) < 0) {
111 error("unlockpt: %.100s", strerror(errno));
116 error("Slave pty side name could not be obtained.");
117 strlcpy(namebuf, pts, namebuflen);
120 /* Open the slave side. */
121 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
123 error("%.100s: %.100s", namebuf, strerror(errno));
129 * Push the appropriate streams modules, as described in Solaris pts(7).
130 * HP-UX pts(7) doesn't have ttcompat module.
132 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
133 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
134 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
135 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
137 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
138 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
142 #else /* HAVE_DEV_PTMX */
143 #ifdef HAVE_DEV_PTS_AND_PTC
144 /* AIX-style pty code. */
147 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
149 error("Could not open /dev/ptc: %.100s", strerror(errno));
152 name = ttyname(*ptyfd);
154 fatal("Open of /dev/ptc returns device for which ttyname fails.");
155 strlcpy(namebuf, name, namebuflen);
156 *ttyfd = open(name, O_RDWR | O_NOCTTY);
158 error("Could not open pty slave side %.100s: %.100s",
159 name, strerror(errno));
164 #else /* HAVE_DEV_PTS_AND_PTC */
171 highpty = sysconf(_SC_CRAY_NPTY);
178 for (i = 0; i < highpty; i++) {
179 snprintf(buf, sizeof(buf), "/dev/pty/%03d", i);
180 *ptyfd = open(buf, O_RDWR|O_NOCTTY);
183 snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i);
184 /* Open the slave side. */
185 *ttyfd = open(namebuf, O_RDWR|O_NOCTTY);
187 error("%.100s: %.100s", namebuf, strerror(errno));
195 /* BSD-style pty code. */
198 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
199 const char *ptyminors = "0123456789abcdef";
200 int num_minors = strlen(ptyminors);
201 int num_ptys = strlen(ptymajors) * num_minors;
203 for (i = 0; i < num_ptys; i++) {
204 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
205 ptyminors[i % num_minors]);
206 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
207 ptymajors[i / num_minors], ptyminors[i % num_minors]);
209 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
211 /* Try SCO style naming */
212 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
213 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
214 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
219 /* Open the slave side. */
220 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
222 error("%.100s: %.100s", namebuf, strerror(errno));
230 #endif /* HAVE_DEV_PTS_AND_PTC */
231 #endif /* HAVE_DEV_PTMX */
232 #endif /* HAVE__GETPTY */
233 #endif /* HAVE_OPENPTY */
236 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
239 pty_release(const char *ttyname)
241 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
242 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
243 if (chmod(ttyname, (mode_t) 0666) < 0)
244 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
247 /* Makes the tty the processes controlling tty and sets it to sane modes. */
250 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
255 #endif /* USE_VHANGUP */
259 error("setsid: %.100s", strerror(errno));
261 fd = open(ttyname, O_RDWR|O_NOCTTY);
263 mysignal(SIGHUP, SIG_IGN);
264 ioctl(fd, TCVHUP, (char *)NULL);
265 mysignal(SIGHUP, SIG_DFL);
269 error("Failed to disconnect from controlling tty.");
272 debug("Setting controlling tty using TCSETCTTY.");
273 ioctl(*ttyfd, TCSETCTTY, NULL);
274 fd = open("/dev/tty", O_RDWR);
276 error("%.100s: %.100s", ttyname, strerror(errno));
281 /* First disconnect from the old controlling tty. */
283 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
285 (void) ioctl(fd, TIOCNOTTY, NULL);
288 #endif /* TIOCNOTTY */
290 error("setsid: %.100s", strerror(errno));
293 * Verify that we are successfully disconnected from the controlling
296 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
298 error("Failed to disconnect from controlling tty.");
301 /* Make it our controlling tty. */
303 debug("Setting controlling tty using TIOCSCTTY.");
304 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
305 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
306 #endif /* TIOCSCTTY */
308 if (setpgrp(0,0) < 0)
309 error("SETPGRP %s",strerror(errno));
310 #endif /* HAVE_NEWS4 */
312 old = mysignal(SIGHUP, SIG_IGN);
314 mysignal(SIGHUP, old);
315 #endif /* USE_VHANGUP */
316 fd = open(ttyname, O_RDWR);
318 error("%.100s: %.100s", ttyname, strerror(errno));
323 #else /* USE_VHANGUP */
325 #endif /* USE_VHANGUP */
327 /* Verify that we now have a controlling tty. */
328 fd = open(_PATH_TTY, O_WRONLY);
330 error("open /dev/tty failed - could not set controlling tty: %.100s",
338 /* Changes the window size associated with the pty. */
341 pty_change_window_size(int ptyfd, int row, int col,
342 int xpixel, int ypixel)
347 w.ws_xpixel = xpixel;
348 w.ws_ypixel = ypixel;
349 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
353 pty_setowner(struct passwd *pw, const char *ttyname)
360 /* Determine the group to make the owner of the tty. */
361 grp = getgrnam("tty");
364 mode = S_IRUSR | S_IWUSR | S_IWGRP;
367 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
371 * Change owner and mode of the tty as required.
372 * Warn but continue if filesystem is read-only and the uids match/
373 * tty is owned by root.
375 if (stat(ttyname, &st))
376 fatal("stat(%.100s) failed: %.100s", ttyname,
379 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
380 if (chown(ttyname, pw->pw_uid, gid) < 0) {
381 if (errno == EROFS &&
382 (st.st_uid == pw->pw_uid || st.st_uid == 0))
383 error("chown(%.100s, %d, %d) failed: %.100s",
384 ttyname, pw->pw_uid, gid,
387 fatal("chown(%.100s, %d, %d) failed: %.100s",
388 ttyname, pw->pw_uid, gid,
393 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
394 if (chmod(ttyname, mode) < 0) {
395 if (errno == EROFS &&
396 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
397 error("chmod(%.100s, 0%o) failed: %.100s",
398 ttyname, mode, strerror(errno));
400 fatal("chmod(%.100s, 0%o) failed: %.100s",
401 ttyname, mode, strerror(errno));