Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * |
| 3 | * pty.c |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Fri Mar 17 04:37:25 1995 ylo |
| 11 | * |
| 12 | * Allocating a pseudo-terminal, and making it the controlling tty. |
| 13 | * |
| 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame^] | 17 | RCSID("$Id: pty.c,v 1.11 1999/12/21 00:18:08 damien Exp $"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 18 | |
Damien Miller | 36b339a | 1999-12-14 10:54:47 +1100 | [diff] [blame] | 19 | #ifdef HAVE_UTIL_H |
| 20 | # include <util.h> |
| 21 | #endif /* HAVE_UTIL_H */ |
| 22 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 23 | #include "pty.h" |
| 24 | #include "ssh.h" |
Damien Miller | 356a0b0 | 1999-11-08 15:30:59 +1100 | [diff] [blame] | 25 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | /* 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 Miller | fce1648 | 1999-12-08 08:53:52 +1100 | [diff] [blame] | 31 | #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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | #ifndef O_NOCTTY |
| 39 | #define O_NOCTTY 0 |
| 40 | #endif |
| 41 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 42 | /* |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 49 | int |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 50 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | { |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 52 | #if defined(HAVE_OPENPTY) || defined(BSD4_4) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 53 | /* openpty(3) exists in OSF/1 and some other os'es */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 54 | char buf[64]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 55 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 57 | i = openpty(ptyfd, ttyfd, buf, NULL, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | if (i < 0) { |
| 59 | error("openpty: %.100s", strerror(errno)); |
| 60 | return 0; |
| 61 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 62 | strlcpy(namebuf, buf, namebuflen); /* possible truncation */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 63 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | #else /* HAVE_OPENPTY */ |
| 65 | #ifdef HAVE__GETPTY |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 66 | /* |
| 67 | * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more |
| 68 | * pty's automagically when needed |
| 69 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | char *slave; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 72 | slave = _getpty(ptyfd, O_RDWR, 0622, 0); |
| 73 | if (slave == NULL) { |
| 74 | error("_getpty: %.100s", strerror(errno)); |
| 75 | return 0; |
| 76 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 77 | strlcpy(namebuf, slave, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | /* 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | #else /* HAVE__GETPTY */ |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame^] | 87 | #if defined(HAVE_DEV_PTMX) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 88 | /* |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | int ptm; |
| 93 | char *pts; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 95 | 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 Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 111 | strlcpy(namebuf, pts, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | *ptyfd = ptm; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 114 | /* 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 Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 121 | /* Push the appropriate streams modules, as described in Solaris pts(7). */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 122 | 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)); |
| 126 | if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) |
| 127 | error("ioctl I_PUSH ttcompat: %.100s", strerror(errno)); |
| 128 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | #else /* HAVE_DEV_PTMX */ |
| 130 | #ifdef HAVE_DEV_PTS_AND_PTC |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | /* AIX-style pty code. */ |
| 132 | const char *name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 134 | *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY); |
| 135 | if (*ptyfd < 0) { |
| 136 | error("Could not open /dev/ptc: %.100s", strerror(errno)); |
| 137 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 138 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | name = ttyname(*ptyfd); |
| 140 | if (!name) |
| 141 | fatal("Open of /dev/ptc returns device for which ttyname fails."); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 142 | strlcpy(namebuf, name, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 143 | *ttyfd = open(name, O_RDWR | O_NOCTTY); |
| 144 | if (*ttyfd < 0) { |
| 145 | error("Could not open pty slave side %.100s: %.100s", |
| 146 | name, strerror(errno)); |
| 147 | close(*ptyfd); |
| 148 | return 0; |
| 149 | } |
| 150 | return 1; |
| 151 | #else /* HAVE_DEV_PTS_AND_PTC */ |
| 152 | /* BSD-style pty code. */ |
| 153 | char buf[64]; |
| 154 | int i; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 155 | const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 156 | const char *ptyminors = "0123456789abcdef"; |
| 157 | int num_minors = strlen(ptyminors); |
| 158 | int num_ptys = strlen(ptymajors) * num_minors; |
| 159 | |
| 160 | for (i = 0; i < num_ptys; i++) { |
| 161 | snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors], |
| 162 | ptyminors[i % num_minors]); |
| 163 | *ptyfd = open(buf, O_RDWR | O_NOCTTY); |
| 164 | if (*ptyfd < 0) |
| 165 | continue; |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 166 | snprintf(namebuf, namebuflen, "/dev/tty%c%c", |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 167 | ptymajors[i / num_minors], ptyminors[i % num_minors]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 168 | |
| 169 | /* Open the slave side. */ |
| 170 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); |
| 171 | if (*ttyfd < 0) { |
| 172 | error("%.100s: %.100s", namebuf, strerror(errno)); |
| 173 | close(*ptyfd); |
| 174 | return 0; |
| 175 | } |
| 176 | return 1; |
| 177 | } |
| 178 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 179 | #endif /* HAVE_DEV_PTS_AND_PTC */ |
| 180 | #endif /* HAVE_DEV_PTMX */ |
| 181 | #endif /* HAVE__GETPTY */ |
| 182 | #endif /* HAVE_OPENPTY */ |
| 183 | } |
| 184 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 185 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 187 | void |
| 188 | pty_release(const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 189 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) |
| 191 | debug("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); |
| 192 | if (chmod(ttyname, (mode_t) 0666) < 0) |
| 193 | debug("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Makes the tty the processes controlling tty and sets it to sane modes. */ |
| 197 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | void |
| 199 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 200 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 201 | int fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 202 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | /* First disconnect from the old controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 204 | #ifdef TIOCNOTTY |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 205 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
| 206 | if (fd >= 0) { |
| 207 | (void) ioctl(fd, TIOCNOTTY, NULL); |
| 208 | close(fd); |
| 209 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 210 | #endif /* TIOCNOTTY */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 211 | if (setsid() < 0) |
| 212 | error("setsid: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 214 | /* |
| 215 | * Verify that we are successfully disconnected from the controlling |
| 216 | * tty. |
| 217 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 218 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
| 219 | if (fd >= 0) { |
| 220 | error("Failed to disconnect from controlling tty."); |
| 221 | close(fd); |
| 222 | } |
| 223 | /* Make it our controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 224 | #ifdef TIOCSCTTY |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 225 | debug("Setting controlling tty using TIOCSCTTY."); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 226 | /* |
| 227 | * We ignore errors from this, because HPSUX defines TIOCSCTTY, but |
| 228 | * returns EINVAL with these arguments, and there is absolutely no |
| 229 | * documentation. |
| 230 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | ioctl(*ttyfd, TIOCSCTTY, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | #endif /* TIOCSCTTY */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 233 | fd = open(ttyname, O_RDWR); |
| 234 | if (fd < 0) |
| 235 | error("%.100s: %.100s", ttyname, strerror(errno)); |
| 236 | else |
| 237 | close(fd); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 238 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 239 | /* Verify that we now have a controlling tty. */ |
| 240 | fd = open("/dev/tty", O_WRONLY); |
| 241 | if (fd < 0) |
| 242 | error("open /dev/tty failed - could not set controlling tty: %.100s", |
| 243 | strerror(errno)); |
| 244 | else { |
| 245 | close(fd); |
| 246 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* Changes the window size associated with the pty. */ |
| 250 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | void |
| 252 | pty_change_window_size(int ptyfd, int row, int col, |
| 253 | int xpixel, int ypixel) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 254 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 255 | struct winsize w; |
| 256 | w.ws_row = row; |
| 257 | w.ws_col = col; |
| 258 | w.ws_xpixel = xpixel; |
| 259 | w.ws_ypixel = ypixel; |
| 260 | (void) ioctl(ptyfd, TIOCSWINSZ, &w); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 261 | } |