]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
4 | * All rights reserved | |
5260325f | 5 | * Allocating a pseudo-terminal, and making it the controlling tty. |
6ae2364d | 6 | * |
bcbf86ec | 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". | |
5260325f | 12 | */ |
8efc0c15 | 13 | |
14 | #include "includes.h" | |
bcbf86ec | 15 | RCSID("$OpenBSD: pty.c,v 1.16 2000/09/07 21:13:37 markus Exp $"); |
8efc0c15 | 16 | |
884bcb37 | 17 | #ifdef HAVE_UTIL_H |
18 | # include <util.h> | |
19 | #endif /* HAVE_UTIL_H */ | |
20 | ||
5260325f | 21 | #include "pty.h" |
22 | #include "ssh.h" | |
23 | ||
8efc0c15 | 24 | /* 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 | ||
6d8c4ea4 | 29 | #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 | ||
8efc0c15 | 36 | #ifndef O_NOCTTY |
37 | #define O_NOCTTY 0 | |
38 | #endif | |
39 | ||
aa3378df | 40 | /* |
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 | */ | |
8efc0c15 | 46 | |
6ae2364d | 47 | int |
a408af76 | 48 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
8efc0c15 | 49 | { |
a408af76 | 50 | #if defined(HAVE_OPENPTY) || defined(BSD4_4) |
5260325f | 51 | /* openpty(3) exists in OSF/1 and some other os'es */ |
a408af76 | 52 | char buf[64]; |
5260325f | 53 | int i; |
8efc0c15 | 54 | |
a408af76 | 55 | i = openpty(ptyfd, ttyfd, buf, NULL, NULL); |
5260325f | 56 | if (i < 0) { |
57 | error("openpty: %.100s", strerror(errno)); | |
58 | return 0; | |
59 | } | |
a408af76 | 60 | strlcpy(namebuf, buf, namebuflen); /* possible truncation */ |
5260325f | 61 | return 1; |
8efc0c15 | 62 | #else /* HAVE_OPENPTY */ |
63 | #ifdef HAVE__GETPTY | |
aa3378df | 64 | /* |
65 | * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more | |
66 | * pty's automagically when needed | |
67 | */ | |
5260325f | 68 | char *slave; |
69 | ||
70 | slave = _getpty(ptyfd, O_RDWR, 0622, 0); | |
71 | if (slave == NULL) { | |
72 | error("_getpty: %.100s", strerror(errno)); | |
73 | return 0; | |
74 | } | |
a408af76 | 75 | strlcpy(namebuf, slave, namebuflen); |
5260325f | 76 | /* 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; | |
8efc0c15 | 84 | #else /* HAVE__GETPTY */ |
a7effaac | 85 | #if defined(HAVE_DEV_PTMX) |
aa3378df | 86 | /* |
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 | */ | |
5260325f | 90 | int ptm; |
91 | char *pts; | |
92 | ||
93 | 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."); | |
a408af76 | 109 | strlcpy(namebuf, pts, namebuflen); |
5260325f | 110 | *ptyfd = ptm; |
111 | ||
112 | /* 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 | } | |
3c62e7eb | 119 | #ifndef HAVE_CYGWIN |
aa3378df | 120 | /* Push the appropriate streams modules, as described in Solaris pts(7). */ |
5260325f | 121 | if (ioctl(*ttyfd, I_PUSH, "ptem") < 0) |
122 | error("ioctl I_PUSH ptem: %.100s", strerror(errno)); | |
123 | if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0) | |
124 | error("ioctl I_PUSH ldterm: %.100s", strerror(errno)); | |
be48d23c | 125 | #ifndef _HPUX_SOURCE |
5260325f | 126 | if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) |
127 | error("ioctl I_PUSH ttcompat: %.100s", strerror(errno)); | |
3c62e7eb | 128 | #endif |
be48d23c | 129 | #endif |
5260325f | 130 | return 1; |
8efc0c15 | 131 | #else /* HAVE_DEV_PTMX */ |
132 | #ifdef HAVE_DEV_PTS_AND_PTC | |
5260325f | 133 | /* AIX-style pty code. */ |
134 | const char *name; | |
8efc0c15 | 135 | |
5260325f | 136 | *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; | |
140 | } | |
141 | name = ttyname(*ptyfd); | |
142 | if (!name) | |
143 | fatal("Open of /dev/ptc returns device for which ttyname fails."); | |
a408af76 | 144 | strlcpy(namebuf, name, namebuflen); |
5260325f | 145 | *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; | |
8efc0c15 | 153 | #else /* HAVE_DEV_PTS_AND_PTC */ |
5260325f | 154 | /* BSD-style pty code. */ |
155 | char buf[64]; | |
156 | int i; | |
aa3378df | 157 | const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
5260325f | 158 | 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]); | |
95f1eccc | 165 | snprintf(namebuf, namebuflen, "/dev/tty%c%c", |
a408af76 | 166 | ptymajors[i / num_minors], ptyminors[i % num_minors]); |
5260325f | 167 | |
b5f90139 | 168 | *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 | ||
5260325f | 178 | /* 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; | |
8efc0c15 | 186 | } |
5260325f | 187 | return 0; |
8efc0c15 | 188 | #endif /* HAVE_DEV_PTS_AND_PTC */ |
189 | #endif /* HAVE_DEV_PTMX */ | |
190 | #endif /* HAVE__GETPTY */ | |
191 | #endif /* HAVE_OPENPTY */ | |
192 | } | |
193 | ||
5260325f | 194 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
8efc0c15 | 195 | |
6ae2364d | 196 | void |
5260325f | 197 | pty_release(const char *ttyname) |
8efc0c15 | 198 | { |
3276571c | 199 | if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) |
817175bc | 200 | error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); |
3276571c | 201 | if (chmod(ttyname, (mode_t) 0666) < 0) |
817175bc | 202 | error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); |
8efc0c15 | 203 | } |
204 | ||
205 | /* Makes the tty the processes controlling tty and sets it to sane modes. */ | |
206 | ||
6ae2364d | 207 | void |
5260325f | 208 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) |
8efc0c15 | 209 | { |
5260325f | 210 | int fd; |
3c62e7eb | 211 | #ifdef USE_VHANGUP |
4e577b89 | 212 | void *old; |
3c62e7eb | 213 | #endif /* USE_VHANGUP */ |
8efc0c15 | 214 | |
5260325f | 215 | /* First disconnect from the old controlling tty. */ |
8efc0c15 | 216 | #ifdef TIOCNOTTY |
5260325f | 217 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
218 | if (fd >= 0) { | |
219 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
220 | close(fd); | |
221 | } | |
8efc0c15 | 222 | #endif /* TIOCNOTTY */ |
5260325f | 223 | if (setsid() < 0) |
224 | error("setsid: %.100s", strerror(errno)); | |
225 | ||
aa3378df | 226 | /* |
227 | * Verify that we are successfully disconnected from the controlling | |
228 | * tty. | |
229 | */ | |
5260325f | 230 | 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. */ | |
8efc0c15 | 236 | #ifdef TIOCSCTTY |
5260325f | 237 | debug("Setting controlling tty using TIOCSCTTY."); |
aa3378df | 238 | /* |
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 | */ | |
5260325f | 243 | ioctl(*ttyfd, TIOCSCTTY, NULL); |
8efc0c15 | 244 | #endif /* TIOCSCTTY */ |
3c62e7eb | 245 | #ifdef USE_VHANGUP |
4e577b89 | 246 | old = signal(SIGHUP, SIG_IGN); |
247 | vhangup(); | |
248 | signal(SIGHUP, old); | |
3c62e7eb | 249 | #endif /* USE_VHANGUP */ |
5260325f | 250 | fd = open(ttyname, O_RDWR); |
4e577b89 | 251 | if (fd < 0) { |
5260325f | 252 | error("%.100s: %.100s", ttyname, strerror(errno)); |
4e577b89 | 253 | } else { |
3c62e7eb | 254 | #ifdef USE_VHANGUP |
4e577b89 | 255 | close(*ttyfd); |
256 | *ttyfd = fd; | |
3c62e7eb | 257 | #else /* USE_VHANGUP */ |
5260325f | 258 | close(fd); |
3c62e7eb | 259 | #endif /* USE_VHANGUP */ |
4e577b89 | 260 | } |
5260325f | 261 | /* 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 | } | |
8efc0c15 | 269 | } |
270 | ||
271 | /* Changes the window size associated with the pty. */ | |
272 | ||
6ae2364d | 273 | void |
5260325f | 274 | pty_change_window_size(int ptyfd, int row, int col, |
275 | int xpixel, int ypixel) | |
8efc0c15 | 276 | { |
5260325f | 277 | 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); | |
8efc0c15 | 283 | } |
817175bc | 284 | |
285 | void | |
286 | pty_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 | } |