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 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Allocating a pseudo-terminal, and making it the controlling tty. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Damien Miller | 469954d | 2003-06-18 20:25:33 +1000 | [diff] [blame] | 15 | RCSID("$OpenBSD: sshpty.c,v 1.10 2003/06/12 07:57:38 markus Exp $"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 16 | |
Damien Miller | 36b339a | 1999-12-14 10:54:47 +1100 | [diff] [blame] | 17 | #ifdef HAVE_UTIL_H |
| 18 | # include <util.h> |
| 19 | #endif /* HAVE_UTIL_H */ |
| 20 | |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 21 | #include "sshpty.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 22 | #include "log.h" |
Kevin Steves | 974fb9c | 2001-06-15 00:04:23 +0000 | [diff] [blame] | 23 | #include "misc.h" |
Damien Miller | 356a0b0 | 1999-11-08 15:30:59 +1100 | [diff] [blame] | 24 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 25 | /* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */ |
| 26 | #if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY) |
| 27 | #undef HAVE_DEV_PTMX |
| 28 | #endif |
| 29 | |
Damien Miller | fce1648 | 1999-12-08 08:53:52 +1100 | [diff] [blame] | 30 | #ifdef HAVE_PTY_H |
| 31 | # include <pty.h> |
| 32 | #endif |
| 33 | #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H) |
| 34 | # include <sys/stropts.h> |
| 35 | #endif |
| 36 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | #ifndef O_NOCTTY |
| 38 | #define O_NOCTTY 0 |
| 39 | #endif |
| 40 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 41 | /* |
| 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). |
| 46 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 47 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 48 | int |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 49 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | { |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 51 | #if defined(HAVE_OPENPTY) || defined(BSD4_4) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 52 | /* openpty(3) exists in OSF/1 and some other os'es */ |
Damien Miller | 99e9243 | 2001-02-18 12:49:35 +1100 | [diff] [blame] | 53 | char *name; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 54 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 55 | |
Damien Miller | 99e9243 | 2001-02-18 12:49:35 +1100 | [diff] [blame] | 56 | i = openpty(ptyfd, ttyfd, NULL, NULL, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 57 | if (i < 0) { |
| 58 | error("openpty: %.100s", strerror(errno)); |
| 59 | return 0; |
| 60 | } |
Damien Miller | 99e9243 | 2001-02-18 12:49:35 +1100 | [diff] [blame] | 61 | name = ttyname(*ttyfd); |
| 62 | if (!name) |
| 63 | fatal("openpty returns device for which ttyname fails."); |
| 64 | |
| 65 | strlcpy(namebuf, name, namebuflen); /* possible truncation */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 67 | #else /* HAVE_OPENPTY */ |
| 68 | #ifdef HAVE__GETPTY |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 69 | /* |
| 70 | * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more |
| 71 | * pty's automagically when needed |
| 72 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 73 | char *slave; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | slave = _getpty(ptyfd, O_RDWR, 0622, 0); |
| 76 | if (slave == NULL) { |
| 77 | error("_getpty: %.100s", strerror(errno)); |
| 78 | return 0; |
| 79 | } |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 80 | strlcpy(namebuf, slave, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 81 | /* Open the slave side. */ |
| 82 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); |
| 83 | if (*ttyfd < 0) { |
| 84 | error("%.200s: %.100s", namebuf, strerror(errno)); |
| 85 | close(*ptyfd); |
| 86 | return 0; |
| 87 | } |
| 88 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | #else /* HAVE__GETPTY */ |
Damien Miller | 76112de | 1999-12-21 11:18:08 +1100 | [diff] [blame] | 90 | #if defined(HAVE_DEV_PTMX) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 91 | /* |
| 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.) |
| 94 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 95 | int ptm; |
| 96 | char *pts; |
Kevin Steves | 974fb9c | 2001-06-15 00:04:23 +0000 | [diff] [blame] | 97 | mysig_t old_signal; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 98 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 99 | ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY); |
| 100 | if (ptm < 0) { |
| 101 | error("/dev/ptmx: %.100s", strerror(errno)); |
| 102 | return 0; |
| 103 | } |
Kevin Steves | 974fb9c | 2001-06-15 00:04:23 +0000 | [diff] [blame] | 104 | old_signal = mysignal(SIGCHLD, SIG_DFL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 105 | if (grantpt(ptm) < 0) { |
| 106 | error("grantpt: %.100s", strerror(errno)); |
| 107 | return 0; |
| 108 | } |
Kevin Steves | 974fb9c | 2001-06-15 00:04:23 +0000 | [diff] [blame] | 109 | mysignal(SIGCHLD, old_signal); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | if (unlockpt(ptm) < 0) { |
| 111 | error("unlockpt: %.100s", strerror(errno)); |
| 112 | return 0; |
| 113 | } |
| 114 | pts = ptsname(ptm); |
| 115 | if (pts == NULL) |
| 116 | error("Slave pty side name could not be obtained."); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 117 | strlcpy(namebuf, pts, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | *ptyfd = ptm; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 119 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | /* Open the slave side. */ |
| 121 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); |
| 122 | if (*ttyfd < 0) { |
| 123 | error("%.100s: %.100s", namebuf, strerror(errno)); |
| 124 | close(*ptyfd); |
| 125 | return 0; |
| 126 | } |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 127 | #ifndef HAVE_CYGWIN |
Kevin Steves | 85b3db6 | 2000-11-05 15:31:36 +0000 | [diff] [blame] | 128 | /* |
| 129 | * Push the appropriate streams modules, as described in Solaris pts(7). |
| 130 | * HP-UX pts(7) doesn't have ttcompat module. |
| 131 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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)); |
Kevin Steves | 85b3db6 | 2000-11-05 15:31:36 +0000 | [diff] [blame] | 136 | #ifndef __hpux |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) |
| 138 | error("ioctl I_PUSH ttcompat: %.100s", strerror(errno)); |
Damien Miller | e37ac5a | 2000-03-17 23:58:59 +1100 | [diff] [blame] | 139 | #endif |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 140 | #endif |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 142 | #else /* HAVE_DEV_PTMX */ |
| 143 | #ifdef HAVE_DEV_PTS_AND_PTC |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 144 | /* AIX-style pty code. */ |
| 145 | const char *name; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 147 | *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY); |
| 148 | if (*ptyfd < 0) { |
| 149 | error("Could not open /dev/ptc: %.100s", strerror(errno)); |
| 150 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | name = ttyname(*ptyfd); |
| 153 | if (!name) |
| 154 | fatal("Open of /dev/ptc returns device for which ttyname fails."); |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 155 | strlcpy(namebuf, name, namebuflen); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 156 | *ttyfd = open(name, O_RDWR | O_NOCTTY); |
| 157 | if (*ttyfd < 0) { |
| 158 | error("Could not open pty slave side %.100s: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 159 | name, strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | close(*ptyfd); |
| 161 | return 0; |
| 162 | } |
| 163 | return 1; |
| 164 | #else /* HAVE_DEV_PTS_AND_PTC */ |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 165 | #ifdef _UNICOS |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 166 | char buf[64]; |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 167 | int i; |
| 168 | int highpty; |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 169 | |
| 170 | #ifdef _SC_CRAY_NPTY |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 171 | highpty = sysconf(_SC_CRAY_NPTY); |
| 172 | if (highpty == -1) |
| 173 | highpty = 128; |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 174 | #else |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 175 | highpty = 128; |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 176 | #endif |
| 177 | |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 178 | for (i = 0; i < highpty; i++) { |
| 179 | snprintf(buf, sizeof(buf), "/dev/pty/%03d", i); |
| 180 | *ptyfd = open(buf, O_RDWR|O_NOCTTY); |
| 181 | if (*ptyfd < 0) |
| 182 | continue; |
| 183 | snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i); |
| 184 | /* Open the slave side. */ |
| 185 | *ttyfd = open(namebuf, O_RDWR|O_NOCTTY); |
| 186 | if (*ttyfd < 0) { |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 187 | error("%.100s: %.100s", namebuf, strerror(errno)); |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 188 | close(*ptyfd); |
Kevin Steves | 25ee4e4 | 2001-08-14 20:41:34 +0000 | [diff] [blame] | 189 | return 0; |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 190 | } |
| 191 | return 1; |
| 192 | } |
| 193 | return 0; |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 194 | #else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 195 | /* BSD-style pty code. */ |
| 196 | char buf[64]; |
| 197 | int i; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 198 | const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 199 | const char *ptyminors = "0123456789abcdef"; |
| 200 | int num_minors = strlen(ptyminors); |
| 201 | int num_ptys = strlen(ptymajors) * num_minors; |
Tim Rice | 1e28c9e | 2002-05-13 17:07:18 -0700 | [diff] [blame] | 202 | struct termios tio; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | |
| 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]); |
Damien Miller | a34a28b | 1999-12-14 10:47:15 +1100 | [diff] [blame] | 207 | snprintf(namebuf, namebuflen, "/dev/tty%c%c", |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 208 | ptymajors[i / num_minors], ptyminors[i % num_minors]); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | |
Damien Miller | 6d8c11f | 2000-08-29 11:52:38 +1100 | [diff] [blame] | 210 | *ptyfd = open(buf, O_RDWR | O_NOCTTY); |
| 211 | if (*ptyfd < 0) { |
| 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); |
| 216 | if (*ptyfd < 0) |
| 217 | continue; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 220 | /* Open the slave side. */ |
| 221 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); |
| 222 | if (*ttyfd < 0) { |
| 223 | error("%.100s: %.100s", namebuf, strerror(errno)); |
| 224 | close(*ptyfd); |
| 225 | return 0; |
| 226 | } |
Tim Rice | 1e28c9e | 2002-05-13 17:07:18 -0700 | [diff] [blame] | 227 | /* set tty modes to a sane state for broken clients */ |
| 228 | if (tcgetattr(*ptyfd, &tio) < 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 229 | logit("Getting tty modes for pty failed: %.100s", strerror(errno)); |
Tim Rice | 1e28c9e | 2002-05-13 17:07:18 -0700 | [diff] [blame] | 230 | else { |
| 231 | tio.c_lflag |= (ECHO | ISIG | ICANON); |
| 232 | tio.c_oflag |= (OPOST | ONLCR); |
| 233 | tio.c_iflag |= ICRNL; |
| 234 | |
| 235 | /* Set the new modes for the terminal. */ |
| 236 | if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 237 | logit("Setting tty modes for pty failed: %.100s", strerror(errno)); |
Tim Rice | 1e28c9e | 2002-05-13 17:07:18 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 240 | return 1; |
| 241 | } |
| 242 | return 0; |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 243 | #endif /* CRAY */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | #endif /* HAVE_DEV_PTS_AND_PTC */ |
| 245 | #endif /* HAVE_DEV_PTMX */ |
| 246 | #endif /* HAVE__GETPTY */ |
| 247 | #endif /* HAVE_OPENPTY */ |
| 248 | } |
| 249 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 250 | /* 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] | 251 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 252 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 253 | pty_release(const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 254 | { |
Damien Miller | 204ad07 | 2000-03-02 23:56:12 +1100 | [diff] [blame] | 255 | if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 256 | error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); |
Damien Miller | 204ad07 | 2000-03-02 23:56:12 +1100 | [diff] [blame] | 257 | if (chmod(ttyname, (mode_t) 0666) < 0) |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 258 | error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 259 | } |
| 260 | |
Damien Miller | 469954d | 2003-06-18 20:25:33 +1000 | [diff] [blame] | 261 | /* Makes the tty the process's controlling tty and sets it to sane modes. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 262 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 263 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 264 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 265 | { |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 266 | int fd; |
Damien Miller | 4a820ea | 2001-10-12 19:15:48 +1000 | [diff] [blame] | 267 | #ifdef USE_VHANGUP |
| 268 | void *old; |
| 269 | #endif /* USE_VHANGUP */ |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 270 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 271 | #ifdef _UNICOS |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 272 | if (setsid() < 0) |
| 273 | error("setsid: %.100s", strerror(errno)); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 274 | |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 275 | fd = open(ttyname, O_RDWR|O_NOCTTY); |
| 276 | if (fd != -1) { |
Damien Miller | 4a820ea | 2001-10-12 19:15:48 +1000 | [diff] [blame] | 277 | mysignal(SIGHUP, SIG_IGN); |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 278 | ioctl(fd, TCVHUP, (char *)NULL); |
Damien Miller | 4a820ea | 2001-10-12 19:15:48 +1000 | [diff] [blame] | 279 | mysignal(SIGHUP, SIG_DFL); |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 280 | setpgid(0, 0); |
| 281 | close(fd); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 282 | } else { |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 283 | error("Failed to disconnect from controlling tty."); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 284 | } |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 285 | |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 286 | debug("Setting controlling tty using TCSETCTTY."); |
| 287 | ioctl(*ttyfd, TCSETCTTY, NULL); |
| 288 | fd = open("/dev/tty", O_RDWR); |
| 289 | if (fd < 0) |
| 290 | error("%.100s: %.100s", ttyname, strerror(errno)); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 291 | close(*ttyfd); |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 292 | *ttyfd = fd; |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 293 | #else /* _UNICOS */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 294 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 295 | /* First disconnect from the old controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 296 | #ifdef TIOCNOTTY |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 297 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 298 | if (fd >= 0) { |
| 299 | (void) ioctl(fd, TIOCNOTTY, NULL); |
| 300 | close(fd); |
| 301 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 302 | #endif /* TIOCNOTTY */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 303 | if (setsid() < 0) |
| 304 | error("setsid: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 305 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 306 | /* |
| 307 | * Verify that we are successfully disconnected from the controlling |
| 308 | * tty. |
| 309 | */ |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 310 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 311 | if (fd >= 0) { |
| 312 | error("Failed to disconnect from controlling tty."); |
| 313 | close(fd); |
| 314 | } |
| 315 | /* Make it our controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 316 | #ifdef TIOCSCTTY |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 317 | debug("Setting controlling tty using TIOCSCTTY."); |
Kevin Steves | 6c0d627 | 2000-11-12 09:22:29 +0000 | [diff] [blame] | 318 | if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) |
| 319 | error("ioctl(TIOCSCTTY): %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 320 | #endif /* TIOCSCTTY */ |
Ben Lindstrom | b562864 | 2000-10-18 00:02:25 +0000 | [diff] [blame] | 321 | #ifdef HAVE_NEWS4 |
| 322 | if (setpgrp(0,0) < 0) |
| 323 | error("SETPGRP %s",strerror(errno)); |
| 324 | #endif /* HAVE_NEWS4 */ |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 325 | #ifdef USE_VHANGUP |
Damien Miller | 4a820ea | 2001-10-12 19:15:48 +1000 | [diff] [blame] | 326 | old = mysignal(SIGHUP, SIG_IGN); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 327 | vhangup(); |
Damien Miller | 4a820ea | 2001-10-12 19:15:48 +1000 | [diff] [blame] | 328 | mysignal(SIGHUP, old); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 329 | #endif /* USE_VHANGUP */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 330 | fd = open(ttyname, O_RDWR); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 331 | if (fd < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 332 | error("%.100s: %.100s", ttyname, strerror(errno)); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 333 | } else { |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 334 | #ifdef USE_VHANGUP |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 335 | close(*ttyfd); |
| 336 | *ttyfd = fd; |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 337 | #else /* USE_VHANGUP */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 338 | close(fd); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 339 | #endif /* USE_VHANGUP */ |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 340 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 341 | /* Verify that we now have a controlling tty. */ |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 342 | fd = open(_PATH_TTY, O_WRONLY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 343 | if (fd < 0) |
| 344 | error("open /dev/tty failed - could not set controlling tty: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 345 | strerror(errno)); |
Ben Lindstrom | 8ada5d0 | 2002-06-23 21:42:50 +0000 | [diff] [blame] | 346 | else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 347 | close(fd); |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 348 | #endif /* _UNICOS */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* Changes the window size associated with the pty. */ |
| 352 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 353 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 354 | pty_change_window_size(int ptyfd, int row, int col, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 355 | int xpixel, int ypixel) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 356 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 357 | struct winsize w; |
Ben Lindstrom | 8ada5d0 | 2002-06-23 21:42:50 +0000 | [diff] [blame] | 358 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 359 | w.ws_row = row; |
| 360 | w.ws_col = col; |
| 361 | w.ws_xpixel = xpixel; |
| 362 | w.ws_ypixel = ypixel; |
| 363 | (void) ioctl(ptyfd, TIOCSWINSZ, &w); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 364 | } |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 365 | |
| 366 | void |
| 367 | pty_setowner(struct passwd *pw, const char *ttyname) |
| 368 | { |
| 369 | struct group *grp; |
| 370 | gid_t gid; |
| 371 | mode_t mode; |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 372 | struct stat st; |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 373 | |
| 374 | /* Determine the group to make the owner of the tty. */ |
| 375 | grp = getgrnam("tty"); |
| 376 | if (grp) { |
| 377 | gid = grp->gr_gid; |
| 378 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
| 379 | } else { |
| 380 | gid = pw->pw_gid; |
| 381 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
| 382 | } |
| 383 | |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 384 | /* |
| 385 | * Change owner and mode of the tty as required. |
Ben Lindstrom | c88785e | 2001-08-06 20:47:23 +0000 | [diff] [blame] | 386 | * Warn but continue if filesystem is read-only and the uids match/ |
| 387 | * tty is owned by root. |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 388 | */ |
| 389 | if (stat(ttyname, &st)) |
| 390 | fatal("stat(%.100s) failed: %.100s", ttyname, |
| 391 | strerror(errno)); |
| 392 | |
| 393 | if (st.st_uid != pw->pw_uid || st.st_gid != gid) { |
| 394 | if (chown(ttyname, pw->pw_uid, gid) < 0) { |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 395 | if (errno == EROFS && |
Ben Lindstrom | 8ada5d0 | 2002-06-23 21:42:50 +0000 | [diff] [blame] | 396 | (st.st_uid == pw->pw_uid || st.st_uid == 0)) |
Damien Miller | e8cea9e | 2003-02-24 11:54:10 +1100 | [diff] [blame] | 397 | debug("chown(%.100s, %u, %u) failed: %.100s", |
Ben Lindstrom | c2df3ec | 2002-06-25 23:21:41 +0000 | [diff] [blame] | 398 | ttyname, (u_int)pw->pw_uid, (u_int)gid, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 399 | strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 400 | else |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 401 | fatal("chown(%.100s, %u, %u) failed: %.100s", |
Ben Lindstrom | c2df3ec | 2002-06-25 23:21:41 +0000 | [diff] [blame] | 402 | ttyname, (u_int)pw->pw_uid, (u_int)gid, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 403 | strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 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) |
Damien Miller | e8cea9e | 2003-02-24 11:54:10 +1100 | [diff] [blame] | 411 | debug("chmod(%.100s, 0%o) failed: %.100s", |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 412 | ttyname, (u_int)mode, strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 413 | else |
| 414 | fatal("chmod(%.100s, 0%o) failed: %.100s", |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 415 | ttyname, (u_int)mode, strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 418 | } |