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.10 2003/06/12 07:57:38 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 = signal(SIGCHLD, SIG_DFL);
105 if (grantpt(ptm) < 0) {
106 error("grantpt: %.100s", strerror(errno));
109 signal(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;
204 for (i = 0; i < num_ptys; i++) {
205 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
206 ptyminors[i % num_minors]);
207 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
208 ptymajors[i / num_minors], ptyminors[i % num_minors]);
210 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
212 /* Try SCO style naming */
213 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
214 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
215 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
220 /* Open the slave side. */
221 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
223 error("%.100s: %.100s", namebuf, strerror(errno));
227 /* set tty modes to a sane state for broken clients */
228 if (tcgetattr(*ptyfd, &tio) < 0)
229 logit("Getting tty modes for pty failed: %.100s", strerror(errno));
231 tio.c_lflag |= (ECHO | ISIG | ICANON);
232 tio.c_oflag |= (OPOST | ONLCR);
233 tio.c_iflag |= ICRNL;
235 /* Set the new modes for the terminal. */
236 if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0)
237 logit("Setting tty modes for pty failed: %.100s", strerror(errno));
244 #endif /* HAVE_DEV_PTS_AND_PTC */
245 #endif /* HAVE_DEV_PTMX */
246 #endif /* HAVE__GETPTY */
247 #endif /* HAVE_OPENPTY */
250 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
253 pty_release(const char *ttyname)
255 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
256 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
257 if (chmod(ttyname, (mode_t) 0666) < 0)
258 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
261 /* Makes the tty the process's controlling tty and sets it to sane modes. */
264 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
269 #endif /* USE_VHANGUP */
273 error("setsid: %.100s", strerror(errno));
275 fd = open(ttyname, O_RDWR|O_NOCTTY);
277 signal(SIGHUP, SIG_IGN);
278 ioctl(fd, TCVHUP, (char *)NULL);
279 signal(SIGHUP, SIG_DFL);
283 error("Failed to disconnect from controlling tty.");
286 debug("Setting controlling tty using TCSETCTTY.");
287 ioctl(*ttyfd, TCSETCTTY, NULL);
288 fd = open("/dev/tty", O_RDWR);
290 error("%.100s: %.100s", ttyname, strerror(errno));
295 /* First disconnect from the old controlling tty. */
297 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
299 (void) ioctl(fd, TIOCNOTTY, NULL);
302 #endif /* TIOCNOTTY */
304 error("setsid: %.100s", strerror(errno));
307 * Verify that we are successfully disconnected from the controlling
310 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
312 error("Failed to disconnect from controlling tty.");
315 /* Make it our controlling tty. */
317 debug("Setting controlling tty using TIOCSCTTY.");
318 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
319 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
320 #endif /* TIOCSCTTY */
322 if (setpgrp(0,0) < 0)
323 error("SETPGRP %s",strerror(errno));
324 #endif /* HAVE_NEWS4 */
326 old = signal(SIGHUP, SIG_IGN);
329 #endif /* USE_VHANGUP */
330 fd = open(ttyname, O_RDWR);
332 error("%.100s: %.100s", ttyname, strerror(errno));
337 #else /* USE_VHANGUP */
339 #endif /* USE_VHANGUP */
341 /* Verify that we now have a controlling tty. */
342 fd = open(_PATH_TTY, O_WRONLY);
344 error("open /dev/tty failed - could not set controlling tty: %.100s",
351 /* Changes the window size associated with the pty. */
354 pty_change_window_size(int ptyfd, int row, int col,
355 int xpixel, int ypixel)
361 w.ws_xpixel = xpixel;
362 w.ws_ypixel = ypixel;
363 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
367 pty_setowner(struct passwd *pw, const char *ttyname)
374 /* Determine the group to make the owner of the tty. */
375 grp = getgrnam("tty");
378 mode = S_IRUSR | S_IWUSR | S_IWGRP;
381 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
385 * Change owner and mode of the tty as required.
386 * Warn but continue if filesystem is read-only and the uids match/
387 * tty is owned by root.
389 if (stat(ttyname, &st))
390 fatal("stat(%.100s) failed: %.100s", ttyname,
393 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
394 if (chown(ttyname, pw->pw_uid, gid) < 0) {
395 if (errno == EROFS &&
396 (st.st_uid == pw->pw_uid || st.st_uid == 0))
397 debug("chown(%.100s, %u, %u) failed: %.100s",
398 ttyname, (u_int)pw->pw_uid, (u_int)gid,
401 fatal("chown(%.100s, %u, %u) failed: %.100s",
402 ttyname, (u_int)pw->pw_uid, (u_int)gid,
407 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
408 if (chmod(ttyname, mode) < 0) {
409 if (errno == EROFS &&
410 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
411 debug("chmod(%.100s, 0%o) failed: %.100s",
412 ttyname, (u_int)mode, strerror(errno));
414 fatal("chmod(%.100s, 0%o) failed: %.100s",
415 ttyname, (u_int)mode, strerror(errno));