blob: 384e921b5dbe3e78eb03439019d09e00acdbaa14 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Allocating a pseudo-terminal, and making it the controlling tty.
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * 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".
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
14#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000015RCSID("$OpenBSD: pty.c,v 1.20 2001/01/21 19:05:53 markus Exp $");
Damien Miller95def091999-11-25 00:26:21 +110016
Damien Miller36b339a1999-12-14 10:54:47 +110017#ifdef HAVE_UTIL_H
18# include <util.h>
19#endif /* HAVE_UTIL_H */
20
Damien Miller95def091999-11-25 00:26:21 +110021#include "pty.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "log.h"
Damien Miller356a0b01999-11-08 15:30:59 +110023
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
25#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
26#undef HAVE_DEV_PTMX
27#endif
28
Damien Millerfce16481999-12-08 08:53:52 +110029#ifdef HAVE_PTY_H
30# include <pty.h>
31#endif
32#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
33# include <sys/stropts.h>
34#endif
35
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036#ifndef O_NOCTTY
37#define O_NOCTTY 0
38#endif
39
Damien Miller5428f641999-11-25 11:54:57 +110040/*
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).
45 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Damien Miller4af51302000-04-16 11:18:38 +100047int
Damien Miller037a0dc1999-12-07 15:38:31 +110048pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049{
Damien Miller037a0dc1999-12-07 15:38:31 +110050#if defined(HAVE_OPENPTY) || defined(BSD4_4)
Damien Miller95def091999-11-25 00:26:21 +110051 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller037a0dc1999-12-07 15:38:31 +110052 char buf[64];
Damien Miller95def091999-11-25 00:26:21 +110053 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller037a0dc1999-12-07 15:38:31 +110055 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110056 if (i < 0) {
57 error("openpty: %.100s", strerror(errno));
58 return 0;
59 }
Damien Miller037a0dc1999-12-07 15:38:31 +110060 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110061 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#else /* HAVE_OPENPTY */
63#ifdef HAVE__GETPTY
Damien Miller5428f641999-11-25 11:54:57 +110064 /*
65 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
66 * pty's automagically when needed
67 */
Damien Miller95def091999-11-25 00:26:21 +110068 char *slave;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Miller95def091999-11-25 00:26:21 +110070 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
71 if (slave == NULL) {
72 error("_getpty: %.100s", strerror(errno));
73 return 0;
74 }
Damien Miller037a0dc1999-12-07 15:38:31 +110075 strlcpy(namebuf, slave, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +110076 /* Open the slave side. */
77 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
78 if (*ttyfd < 0) {
79 error("%.200s: %.100s", namebuf, strerror(errno));
80 close(*ptyfd);
81 return 0;
82 }
83 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084#else /* HAVE__GETPTY */
Damien Miller76112de1999-12-21 11:18:08 +110085#if defined(HAVE_DEV_PTMX)
Damien Miller5428f641999-11-25 11:54:57 +110086 /*
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.)
89 */
Damien Miller95def091999-11-25 00:26:21 +110090 int ptm;
91 char *pts;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller95def091999-11-25 00:26:21 +110093 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
94 if (ptm < 0) {
95 error("/dev/ptmx: %.100s", strerror(errno));
96 return 0;
97 }
98 if (grantpt(ptm) < 0) {
99 error("grantpt: %.100s", strerror(errno));
100 return 0;
101 }
102 if (unlockpt(ptm) < 0) {
103 error("unlockpt: %.100s", strerror(errno));
104 return 0;
105 }
106 pts = ptsname(ptm);
107 if (pts == NULL)
108 error("Slave pty side name could not be obtained.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100109 strlcpy(namebuf, pts, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100110 *ptyfd = ptm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller95def091999-11-25 00:26:21 +1100112 /* Open the slave side. */
113 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
114 if (*ttyfd < 0) {
115 error("%.100s: %.100s", namebuf, strerror(errno));
116 close(*ptyfd);
117 return 0;
118 }
Damien Millerbac2d8a2000-09-05 16:13:06 +1100119#ifndef HAVE_CYGWIN
Kevin Steves85b3db62000-11-05 15:31:36 +0000120 /*
121 * Push the appropriate streams modules, as described in Solaris pts(7).
122 * HP-UX pts(7) doesn't have ttcompat module.
123 */
Damien Miller95def091999-11-25 00:26:21 +1100124 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));
Kevin Steves85b3db62000-11-05 15:31:36 +0000128#ifndef __hpux
Damien Miller95def091999-11-25 00:26:21 +1100129 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
130 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
Damien Millere37ac5a2000-03-17 23:58:59 +1100131#endif
Damien Millerbac2d8a2000-09-05 16:13:06 +1100132#endif
Damien Miller95def091999-11-25 00:26:21 +1100133 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134#else /* HAVE_DEV_PTMX */
135#ifdef HAVE_DEV_PTS_AND_PTC
Damien Miller95def091999-11-25 00:26:21 +1100136 /* AIX-style pty code. */
137 const char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller95def091999-11-25 00:26:21 +1100139 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
140 if (*ptyfd < 0) {
141 error("Could not open /dev/ptc: %.100s", strerror(errno));
142 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143 }
Damien Miller95def091999-11-25 00:26:21 +1100144 name = ttyname(*ptyfd);
145 if (!name)
146 fatal("Open of /dev/ptc returns device for which ttyname fails.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100147 strlcpy(namebuf, name, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100148 *ttyfd = open(name, O_RDWR | O_NOCTTY);
149 if (*ttyfd < 0) {
150 error("Could not open pty slave side %.100s: %.100s",
151 name, strerror(errno));
152 close(*ptyfd);
153 return 0;
154 }
155 return 1;
156#else /* HAVE_DEV_PTS_AND_PTC */
157 /* BSD-style pty code. */
158 char buf[64];
159 int i;
Damien Miller5428f641999-11-25 11:54:57 +1100160 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
Damien Miller95def091999-11-25 00:26:21 +1100161 const char *ptyminors = "0123456789abcdef";
162 int num_minors = strlen(ptyminors);
163 int num_ptys = strlen(ptymajors) * num_minors;
164
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]);
Damien Millera34a28b1999-12-14 10:47:15 +1100168 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
Damien Miller037a0dc1999-12-07 15:38:31 +1100169 ptymajors[i / num_minors], ptyminors[i % num_minors]);
Damien Miller95def091999-11-25 00:26:21 +1100170
Damien Miller6d8c11f2000-08-29 11:52:38 +1100171 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
172 if (*ptyfd < 0) {
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);
177 if (*ptyfd < 0)
178 continue;
179 }
180
Damien Miller95def091999-11-25 00:26:21 +1100181 /* Open the slave side. */
182 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
183 if (*ttyfd < 0) {
184 error("%.100s: %.100s", namebuf, strerror(errno));
185 close(*ptyfd);
186 return 0;
187 }
188 return 1;
189 }
190 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191#endif /* HAVE_DEV_PTS_AND_PTC */
192#endif /* HAVE_DEV_PTMX */
193#endif /* HAVE__GETPTY */
194#endif /* HAVE_OPENPTY */
195}
196
Damien Miller95def091999-11-25 00:26:21 +1100197/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Damien Miller4af51302000-04-16 11:18:38 +1000199void
Damien Miller95def091999-11-25 00:26:21 +1100200pty_release(const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201{
Damien Miller204ad072000-03-02 23:56:12 +1100202 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100203 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
Damien Miller204ad072000-03-02 23:56:12 +1100204 if (chmod(ttyname, (mode_t) 0666) < 0)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100205 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206}
207
208/* Makes the tty the processes controlling tty and sets it to sane modes. */
209
Damien Miller4af51302000-04-16 11:18:38 +1000210void
Damien Miller95def091999-11-25 00:26:21 +1100211pty_make_controlling_tty(int *ttyfd, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212{
Damien Miller95def091999-11-25 00:26:21 +1100213 int fd;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100214#ifdef USE_VHANGUP
Damien Millerd0cff3e2000-04-20 23:12:58 +1000215 void *old;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100216#endif /* USE_VHANGUP */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219#ifdef TIOCNOTTY
Damien Miller95def091999-11-25 00:26:21 +1100220 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
221 if (fd >= 0) {
222 (void) ioctl(fd, TIOCNOTTY, NULL);
223 close(fd);
224 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100226 if (setsid() < 0)
227 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228
Damien Miller5428f641999-11-25 11:54:57 +1100229 /*
230 * Verify that we are successfully disconnected from the controlling
231 * tty.
232 */
Damien Miller95def091999-11-25 00:26:21 +1100233 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
234 if (fd >= 0) {
235 error("Failed to disconnect from controlling tty.");
236 close(fd);
237 }
238 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100240 debug("Setting controlling tty using TIOCSCTTY.");
Kevin Steves6c0d6272000-11-12 09:22:29 +0000241 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
242 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243#endif /* TIOCSCTTY */
Ben Lindstromb5628642000-10-18 00:02:25 +0000244#ifdef HAVE_NEWS4
245 if (setpgrp(0,0) < 0)
246 error("SETPGRP %s",strerror(errno));
247#endif /* HAVE_NEWS4 */
Damien Millerbac2d8a2000-09-05 16:13:06 +1100248#ifdef USE_VHANGUP
Damien Millerd0cff3e2000-04-20 23:12:58 +1000249 old = signal(SIGHUP, SIG_IGN);
250 vhangup();
251 signal(SIGHUP, old);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100252#endif /* USE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100253 fd = open(ttyname, O_RDWR);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000254 if (fd < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100255 error("%.100s: %.100s", ttyname, strerror(errno));
Damien Millerd0cff3e2000-04-20 23:12:58 +1000256 } else {
Damien Millerbac2d8a2000-09-05 16:13:06 +1100257#ifdef USE_VHANGUP
Damien Millerd0cff3e2000-04-20 23:12:58 +1000258 close(*ttyfd);
259 *ttyfd = fd;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100260#else /* USE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100261 close(fd);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100262#endif /* USE_VHANGUP */
Damien Millerd0cff3e2000-04-20 23:12:58 +1000263 }
Damien Miller95def091999-11-25 00:26:21 +1100264 /* Verify that we now have a controlling tty. */
265 fd = open("/dev/tty", O_WRONLY);
266 if (fd < 0)
267 error("open /dev/tty failed - could not set controlling tty: %.100s",
268 strerror(errno));
269 else {
270 close(fd);
271 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272}
273
274/* Changes the window size associated with the pty. */
275
Damien Miller4af51302000-04-16 11:18:38 +1000276void
Damien Miller95def091999-11-25 00:26:21 +1100277pty_change_window_size(int ptyfd, int row, int col,
278 int xpixel, int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279{
Damien Miller95def091999-11-25 00:26:21 +1100280 struct winsize w;
281 w.ws_row = row;
282 w.ws_col = col;
283 w.ws_xpixel = xpixel;
284 w.ws_ypixel = ypixel;
285 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286}
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100287
288void
289pty_setowner(struct passwd *pw, const char *ttyname)
290{
291 struct group *grp;
292 gid_t gid;
293 mode_t mode;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000294 struct stat st;
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100295
296 /* Determine the group to make the owner of the tty. */
297 grp = getgrnam("tty");
298 if (grp) {
299 gid = grp->gr_gid;
300 mode = S_IRUSR | S_IWUSR | S_IWGRP;
301 } else {
302 gid = pw->pw_gid;
303 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
304 }
305
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000306 /*
307 * Change owner and mode of the tty as required.
308 * Warn but continue if filesystem is read-only and the uids match.
309 */
310 if (stat(ttyname, &st))
311 fatal("stat(%.100s) failed: %.100s", ttyname,
312 strerror(errno));
313
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,
319 strerror(errno));
320 else
321 fatal("chown(%.100s, %d, %d) failed: %.100s",
322 ttyname, pw->pw_uid, gid,
323 strerror(errno));
324 }
325 }
326
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));
333 else
334 fatal("chmod(%.100s, 0%o) failed: %.100s",
335 ttyname, mode, strerror(errno));
336 }
337 }
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100338}