blob: f5bb861242ba5cfe9cf754fbd21d7534c6158ba9 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * pty.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Fri Mar 17 04:37:25 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Allocating a pseudo-terminal, and making it the controlling tty.
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller6536c7d2000-06-22 21:32:31 +100017RCSID("$OpenBSD: pty.c,v 1.14 2000/06/20 01:39:43 markus Exp $");
Damien Miller95def091999-11-25 00:26:21 +110018
Damien Miller36b339a1999-12-14 10:54:47 +110019#ifdef HAVE_UTIL_H
20# include <util.h>
21#endif /* HAVE_UTIL_H */
22
Damien Miller95def091999-11-25 00:26:21 +110023#include "pty.h"
24#include "ssh.h"
Damien Miller356a0b01999-11-08 15:30:59 +110025
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
27#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
28#undef HAVE_DEV_PTMX
29#endif
30
Damien Millerfce16481999-12-08 08:53:52 +110031#ifdef HAVE_PTY_H
32# include <pty.h>
33#endif
34#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
35# include <sys/stropts.h>
36#endif
37
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038#ifndef O_NOCTTY
39#define O_NOCTTY 0
40#endif
41
Damien Miller5428f641999-11-25 11:54:57 +110042/*
43 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
44 * nonzero if a pty was successfully allocated. On success, open file
45 * descriptors for the pty and tty sides and the name of the tty side are
46 * returned (the buffer must be able to hold at least 64 characters).
47 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
Damien Miller4af51302000-04-16 11:18:38 +100049int
Damien Miller037a0dc1999-12-07 15:38:31 +110050pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051{
Damien Miller037a0dc1999-12-07 15:38:31 +110052#if defined(HAVE_OPENPTY) || defined(BSD4_4)
Damien Miller95def091999-11-25 00:26:21 +110053 /* openpty(3) exists in OSF/1 and some other os'es */
Damien Miller037a0dc1999-12-07 15:38:31 +110054 char buf[64];
Damien Miller95def091999-11-25 00:26:21 +110055 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller037a0dc1999-12-07 15:38:31 +110057 i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110058 if (i < 0) {
59 error("openpty: %.100s", strerror(errno));
60 return 0;
61 }
Damien Miller037a0dc1999-12-07 15:38:31 +110062 strlcpy(namebuf, buf, namebuflen); /* possible truncation */
Damien Miller95def091999-11-25 00:26:21 +110063 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#else /* HAVE_OPENPTY */
65#ifdef HAVE__GETPTY
Damien Miller5428f641999-11-25 11:54:57 +110066 /*
67 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
68 * pty's automagically when needed
69 */
Damien Miller95def091999-11-25 00:26:21 +110070 char *slave;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Miller95def091999-11-25 00:26:21 +110072 slave = _getpty(ptyfd, O_RDWR, 0622, 0);
73 if (slave == NULL) {
74 error("_getpty: %.100s", strerror(errno));
75 return 0;
76 }
Damien Miller037a0dc1999-12-07 15:38:31 +110077 strlcpy(namebuf, slave, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +110078 /* Open the slave side. */
79 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
80 if (*ttyfd < 0) {
81 error("%.200s: %.100s", namebuf, strerror(errno));
82 close(*ptyfd);
83 return 0;
84 }
85 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086#else /* HAVE__GETPTY */
Damien Miller76112de1999-12-21 11:18:08 +110087#if defined(HAVE_DEV_PTMX)
Damien Miller5428f641999-11-25 11:54:57 +110088 /*
89 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
90 * also has bsd-style ptys, but they simply do not work.)
91 */
Damien Miller95def091999-11-25 00:26:21 +110092 int ptm;
93 char *pts;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller95def091999-11-25 00:26:21 +110095 ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
96 if (ptm < 0) {
97 error("/dev/ptmx: %.100s", strerror(errno));
98 return 0;
99 }
100 if (grantpt(ptm) < 0) {
101 error("grantpt: %.100s", strerror(errno));
102 return 0;
103 }
104 if (unlockpt(ptm) < 0) {
105 error("unlockpt: %.100s", strerror(errno));
106 return 0;
107 }
108 pts = ptsname(ptm);
109 if (pts == NULL)
110 error("Slave pty side name could not be obtained.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100111 strlcpy(namebuf, pts, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100112 *ptyfd = ptm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller95def091999-11-25 00:26:21 +1100114 /* Open the slave side. */
115 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
116 if (*ttyfd < 0) {
117 error("%.100s: %.100s", namebuf, strerror(errno));
118 close(*ptyfd);
119 return 0;
120 }
Damien Miller5428f641999-11-25 11:54:57 +1100121 /* Push the appropriate streams modules, as described in Solaris pts(7). */
Damien Miller95def091999-11-25 00:26:21 +1100122 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
123 error("ioctl I_PUSH ptem: %.100s", strerror(errno));
124 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
125 error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
Damien Millere37ac5a2000-03-17 23:58:59 +1100126#ifndef _HPUX_SOURCE
Damien Miller95def091999-11-25 00:26:21 +1100127 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
128 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
Damien Millere37ac5a2000-03-17 23:58:59 +1100129#endif
Damien Miller95def091999-11-25 00:26:21 +1100130 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131#else /* HAVE_DEV_PTMX */
132#ifdef HAVE_DEV_PTS_AND_PTC
Damien Miller95def091999-11-25 00:26:21 +1100133 /* AIX-style pty code. */
134 const char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller95def091999-11-25 00:26:21 +1100136 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
137 if (*ptyfd < 0) {
138 error("Could not open /dev/ptc: %.100s", strerror(errno));
139 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140 }
Damien Miller95def091999-11-25 00:26:21 +1100141 name = ttyname(*ptyfd);
142 if (!name)
143 fatal("Open of /dev/ptc returns device for which ttyname fails.");
Damien Miller037a0dc1999-12-07 15:38:31 +1100144 strlcpy(namebuf, name, namebuflen);
Damien Miller95def091999-11-25 00:26:21 +1100145 *ttyfd = open(name, O_RDWR | O_NOCTTY);
146 if (*ttyfd < 0) {
147 error("Could not open pty slave side %.100s: %.100s",
148 name, strerror(errno));
149 close(*ptyfd);
150 return 0;
151 }
152 return 1;
153#else /* HAVE_DEV_PTS_AND_PTC */
154 /* BSD-style pty code. */
155 char buf[64];
156 int i;
Damien Miller5428f641999-11-25 11:54:57 +1100157 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
Damien Miller95def091999-11-25 00:26:21 +1100158 const char *ptyminors = "0123456789abcdef";
159 int num_minors = strlen(ptyminors);
160 int num_ptys = strlen(ptymajors) * num_minors;
161
162 for (i = 0; i < num_ptys; i++) {
163 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
164 ptyminors[i % num_minors]);
Damien Millera34a28b1999-12-14 10:47:15 +1100165 snprintf(namebuf, namebuflen, "/dev/tty%c%c",
Damien Miller037a0dc1999-12-07 15:38:31 +1100166 ptymajors[i / num_minors], ptyminors[i % num_minors]);
Damien Miller95def091999-11-25 00:26:21 +1100167
Damien Miller6d8c11f2000-08-29 11:52:38 +1100168 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
169 if (*ptyfd < 0) {
170 /* Try SCO style naming */
171 snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
172 snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
173 *ptyfd = open(buf, O_RDWR | O_NOCTTY);
174 if (*ptyfd < 0)
175 continue;
176 }
177
Damien Miller95def091999-11-25 00:26:21 +1100178 /* Open the slave side. */
179 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
180 if (*ttyfd < 0) {
181 error("%.100s: %.100s", namebuf, strerror(errno));
182 close(*ptyfd);
183 return 0;
184 }
185 return 1;
186 }
187 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188#endif /* HAVE_DEV_PTS_AND_PTC */
189#endif /* HAVE_DEV_PTMX */
190#endif /* HAVE__GETPTY */
191#endif /* HAVE_OPENPTY */
192}
193
Damien Miller95def091999-11-25 00:26:21 +1100194/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Miller4af51302000-04-16 11:18:38 +1000196void
Damien Miller95def091999-11-25 00:26:21 +1100197pty_release(const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198{
Damien Miller204ad072000-03-02 23:56:12 +1100199 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100200 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
Damien Miller204ad072000-03-02 23:56:12 +1100201 if (chmod(ttyname, (mode_t) 0666) < 0)
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100202 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203}
204
205/* Makes the tty the processes controlling tty and sets it to sane modes. */
206
Damien Miller4af51302000-04-16 11:18:38 +1000207void
Damien Miller95def091999-11-25 00:26:21 +1100208pty_make_controlling_tty(int *ttyfd, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209{
Damien Miller95def091999-11-25 00:26:21 +1100210 int fd;
Damien Millerd0cff3e2000-04-20 23:12:58 +1000211#ifdef HAVE_VHANGUP
212 void *old;
213#endif /* HAVE_VHANGUP */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller95def091999-11-25 00:26:21 +1100215 /* First disconnect from the old controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216#ifdef TIOCNOTTY
Damien Miller95def091999-11-25 00:26:21 +1100217 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
218 if (fd >= 0) {
219 (void) ioctl(fd, TIOCNOTTY, NULL);
220 close(fd);
221 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222#endif /* TIOCNOTTY */
Damien Miller95def091999-11-25 00:26:21 +1100223 if (setsid() < 0)
224 error("setsid: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
Damien Miller5428f641999-11-25 11:54:57 +1100226 /*
227 * Verify that we are successfully disconnected from the controlling
228 * tty.
229 */
Damien Miller95def091999-11-25 00:26:21 +1100230 fd = open("/dev/tty", O_RDWR | O_NOCTTY);
231 if (fd >= 0) {
232 error("Failed to disconnect from controlling tty.");
233 close(fd);
234 }
235 /* Make it our controlling tty. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236#ifdef TIOCSCTTY
Damien Miller95def091999-11-25 00:26:21 +1100237 debug("Setting controlling tty using TIOCSCTTY.");
Damien Miller5428f641999-11-25 11:54:57 +1100238 /*
239 * We ignore errors from this, because HPSUX defines TIOCSCTTY, but
240 * returns EINVAL with these arguments, and there is absolutely no
241 * documentation.
242 */
Damien Miller95def091999-11-25 00:26:21 +1100243 ioctl(*ttyfd, TIOCSCTTY, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244#endif /* TIOCSCTTY */
Damien Millerd0cff3e2000-04-20 23:12:58 +1000245#ifdef HAVE_VHANGUP
246 old = signal(SIGHUP, SIG_IGN);
247 vhangup();
248 signal(SIGHUP, old);
249#endif /* HAVE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100250 fd = open(ttyname, O_RDWR);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000251 if (fd < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100252 error("%.100s: %.100s", ttyname, strerror(errno));
Damien Millerd0cff3e2000-04-20 23:12:58 +1000253 } else {
254#ifdef HAVE_VHANGUP
255 close(*ttyfd);
256 *ttyfd = fd;
257#else /* HAVE_VHANGUP */
Damien Miller95def091999-11-25 00:26:21 +1100258 close(fd);
Damien Millerd0cff3e2000-04-20 23:12:58 +1000259#endif /* HAVE_VHANGUP */
260 }
Damien Miller95def091999-11-25 00:26:21 +1100261 /* Verify that we now have a controlling tty. */
262 fd = open("/dev/tty", O_WRONLY);
263 if (fd < 0)
264 error("open /dev/tty failed - could not set controlling tty: %.100s",
265 strerror(errno));
266 else {
267 close(fd);
268 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269}
270
271/* Changes the window size associated with the pty. */
272
Damien Miller4af51302000-04-16 11:18:38 +1000273void
Damien Miller95def091999-11-25 00:26:21 +1100274pty_change_window_size(int ptyfd, int row, int col,
275 int xpixel, int ypixel)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
Damien Miller95def091999-11-25 00:26:21 +1100277 struct winsize w;
278 w.ws_row = row;
279 w.ws_col = col;
280 w.ws_xpixel = xpixel;
281 w.ws_ypixel = ypixel;
282 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283}
Damien Millerc7d8dbb2000-03-02 23:30:53 +1100284
285void
286pty_setowner(struct passwd *pw, const char *ttyname)
287{
288 struct group *grp;
289 gid_t gid;
290 mode_t mode;
291
292 /* Determine the group to make the owner of the tty. */
293 grp = getgrnam("tty");
294 if (grp) {
295 gid = grp->gr_gid;
296 mode = S_IRUSR | S_IWUSR | S_IWGRP;
297 } else {
298 gid = pw->pw_gid;
299 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
300 }
301
302 /* Change ownership of the tty. */
303 if (chown(ttyname, pw->pw_uid, gid) < 0)
304 fatal("chown(%.100s, %d, %d) failed: %.100s",
305 ttyname, pw->pw_uid, gid, strerror(errno));
306 if (chmod(ttyname, mode) < 0)
307 fatal("chmod(%.100s, 0%o) failed: %.100s",
308 ttyname, mode, strerror(errno));
309}