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.1 2001/03/04 01:46:30 djm 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 */
165 /* BSD-style pty code. */
168 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
169 const char *ptyminors = "0123456789abcdef";
170 int num_minors = strlen(ptyminors);
171 int num_ptys = strlen(ptymajors) * num_minors;
173 for (i = 0; i < num_ptys; i++) {
174 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
175 ptyminors[i % num_minors]);
176 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
177 ptymajors[i / num_minors], ptyminors[i % num_minors]);
179 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
181 /* Try SCO style naming */
182 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
183 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
184 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
189 /* Open the slave side. */
190 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
192 error("%.100s: %.100s", namebuf, strerror(errno));
199 #endif /* HAVE_DEV_PTS_AND_PTC */
200 #endif /* HAVE_DEV_PTMX */
201 #endif /* HAVE__GETPTY */
202 #endif /* HAVE_OPENPTY */
205 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
208 pty_release(const char *ttyname)
210 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
211 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
212 if (chmod(ttyname, (mode_t) 0666) < 0)
213 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
216 /* Makes the tty the processes controlling tty and sets it to sane modes. */
219 pty_make_controlling_tty(int *ttyfd, const char *ttyname)
224 #endif /* USE_VHANGUP */
226 /* First disconnect from the old controlling tty. */
228 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
230 (void) ioctl(fd, TIOCNOTTY, NULL);
233 #endif /* TIOCNOTTY */
235 error("setsid: %.100s", strerror(errno));
238 * Verify that we are successfully disconnected from the controlling
241 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
243 error("Failed to disconnect from controlling tty.");
246 /* Make it our controlling tty. */
248 debug("Setting controlling tty using TIOCSCTTY.");
249 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
250 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
251 #endif /* TIOCSCTTY */
253 if (setpgrp(0,0) < 0)
254 error("SETPGRP %s",strerror(errno));
255 #endif /* HAVE_NEWS4 */
257 old = signal(SIGHUP, SIG_IGN);
260 #endif /* USE_VHANGUP */
261 fd = open(ttyname, O_RDWR);
263 error("%.100s: %.100s", ttyname, strerror(errno));
268 #else /* USE_VHANGUP */
270 #endif /* USE_VHANGUP */
272 /* Verify that we now have a controlling tty. */
273 fd = open(_PATH_TTY, O_WRONLY);
275 error("open /dev/tty failed - could not set controlling tty: %.100s",
282 /* Changes the window size associated with the pty. */
285 pty_change_window_size(int ptyfd, int row, int col,
286 int xpixel, int ypixel)
291 w.ws_xpixel = xpixel;
292 w.ws_ypixel = ypixel;
293 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
297 pty_setowner(struct passwd *pw, const char *ttyname)
304 /* Determine the group to make the owner of the tty. */
305 grp = getgrnam("tty");
308 mode = S_IRUSR | S_IWUSR | S_IWGRP;
311 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
315 * Change owner and mode of the tty as required.
316 * Warn but continue if filesystem is read-only and the uids match.
318 if (stat(ttyname, &st))
319 fatal("stat(%.100s) failed: %.100s", ttyname,
322 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
323 if (chown(ttyname, pw->pw_uid, gid) < 0) {
324 if (errno == EROFS && st.st_uid == pw->pw_uid)
325 error("chown(%.100s, %d, %d) failed: %.100s",
326 ttyname, pw->pw_uid, gid,
329 fatal("chown(%.100s, %d, %d) failed: %.100s",
330 ttyname, pw->pw_uid, gid,
335 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
336 if (chmod(ttyname, mode) < 0) {
337 if (errno == EROFS &&
338 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
339 error("chmod(%.100s, 0%o) failed: %.100s",
340 ttyname, mode, strerror(errno));
342 fatal("chmod(%.100s, 0%o) failed: %.100s",
343 ttyname, mode, strerror(errno));