]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
6ae2364d | 2 | * |
5260325f | 3 | * pty.c |
6ae2364d | 4 | * |
5260325f | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 6 | * |
5260325f | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
8 | * All rights reserved | |
6ae2364d | 9 | * |
5260325f | 10 | * Created: Fri Mar 17 04:37:25 1995 ylo |
6ae2364d | 11 | * |
5260325f | 12 | * Allocating a pseudo-terminal, and making it the controlling tty. |
6ae2364d | 13 | * |
5260325f | 14 | */ |
8efc0c15 | 15 | |
16 | #include "includes.h" | |
74fc9186 | 17 | RCSID("$OpenBSD: pty.c,v 1.14 2000/06/20 01:39:43 markus Exp $"); |
8efc0c15 | 18 | |
884bcb37 | 19 | #ifdef HAVE_UTIL_H |
20 | # include <util.h> | |
21 | #endif /* HAVE_UTIL_H */ | |
22 | ||
5260325f | 23 | #include "pty.h" |
24 | #include "ssh.h" | |
25 | ||
8efc0c15 | 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 | ||
6d8c4ea4 | 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 | ||
8efc0c15 | 38 | #ifndef O_NOCTTY |
39 | #define O_NOCTTY 0 | |
40 | #endif | |
41 | ||
aa3378df | 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 | */ | |
8efc0c15 | 48 | |
6ae2364d | 49 | int |
a408af76 | 50 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
8efc0c15 | 51 | { |
a408af76 | 52 | #if defined(HAVE_OPENPTY) || defined(BSD4_4) |
5260325f | 53 | /* openpty(3) exists in OSF/1 and some other os'es */ |
a408af76 | 54 | char buf[64]; |
5260325f | 55 | int i; |
8efc0c15 | 56 | |
a408af76 | 57 | i = openpty(ptyfd, ttyfd, buf, NULL, NULL); |
5260325f | 58 | if (i < 0) { |
59 | error("openpty: %.100s", strerror(errno)); | |
60 | return 0; | |
61 | } | |
a408af76 | 62 | strlcpy(namebuf, buf, namebuflen); /* possible truncation */ |
5260325f | 63 | return 1; |
8efc0c15 | 64 | #else /* HAVE_OPENPTY */ |
65 | #ifdef HAVE__GETPTY | |
aa3378df | 66 | /* |
67 | * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more | |
68 | * pty's automagically when needed | |
69 | */ | |
5260325f | 70 | char *slave; |
71 | ||
72 | slave = _getpty(ptyfd, O_RDWR, 0622, 0); | |
73 | if (slave == NULL) { | |
74 | error("_getpty: %.100s", strerror(errno)); | |
75 | return 0; | |
76 | } | |
a408af76 | 77 | strlcpy(namebuf, slave, namebuflen); |
5260325f | 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; | |
8efc0c15 | 86 | #else /* HAVE__GETPTY */ |
a7effaac | 87 | #if defined(HAVE_DEV_PTMX) |
aa3378df | 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 | */ | |
5260325f | 92 | int ptm; |
93 | char *pts; | |
94 | ||
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."); | |
a408af76 | 111 | strlcpy(namebuf, pts, namebuflen); |
5260325f | 112 | *ptyfd = ptm; |
113 | ||
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 | } | |
aa3378df | 121 | /* Push the appropriate streams modules, as described in Solaris pts(7). */ |
5260325f | 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)); | |
be48d23c | 126 | #ifndef _HPUX_SOURCE |
5260325f | 127 | if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) |
128 | error("ioctl I_PUSH ttcompat: %.100s", strerror(errno)); | |
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]); | |
165 | *ptyfd = open(buf, O_RDWR | O_NOCTTY); | |
166 | if (*ptyfd < 0) | |
167 | continue; | |
95f1eccc | 168 | snprintf(namebuf, namebuflen, "/dev/tty%c%c", |
a408af76 | 169 | ptymajors[i / num_minors], ptyminors[i % num_minors]); |
5260325f | 170 | |
171 | /* Open the slave side. */ | |
172 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); | |
173 | if (*ttyfd < 0) { | |
174 | error("%.100s: %.100s", namebuf, strerror(errno)); | |
175 | close(*ptyfd); | |
176 | return 0; | |
177 | } | |
178 | return 1; | |
8efc0c15 | 179 | } |
5260325f | 180 | return 0; |
8efc0c15 | 181 | #endif /* HAVE_DEV_PTS_AND_PTC */ |
182 | #endif /* HAVE_DEV_PTMX */ | |
183 | #endif /* HAVE__GETPTY */ | |
184 | #endif /* HAVE_OPENPTY */ | |
185 | } | |
186 | ||
5260325f | 187 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
8efc0c15 | 188 | |
6ae2364d | 189 | void |
5260325f | 190 | pty_release(const char *ttyname) |
8efc0c15 | 191 | { |
3276571c | 192 | if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) |
817175bc | 193 | error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); |
3276571c | 194 | if (chmod(ttyname, (mode_t) 0666) < 0) |
817175bc | 195 | error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); |
8efc0c15 | 196 | } |
197 | ||
198 | /* Makes the tty the processes controlling tty and sets it to sane modes. */ | |
199 | ||
6ae2364d | 200 | void |
5260325f | 201 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) |
8efc0c15 | 202 | { |
5260325f | 203 | int fd; |
4e577b89 | 204 | #ifdef HAVE_VHANGUP |
205 | void *old; | |
206 | #endif /* HAVE_VHANGUP */ | |
8efc0c15 | 207 | |
5260325f | 208 | /* First disconnect from the old controlling tty. */ |
8efc0c15 | 209 | #ifdef TIOCNOTTY |
5260325f | 210 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
211 | if (fd >= 0) { | |
212 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
213 | close(fd); | |
214 | } | |
8efc0c15 | 215 | #endif /* TIOCNOTTY */ |
5260325f | 216 | if (setsid() < 0) |
217 | error("setsid: %.100s", strerror(errno)); | |
218 | ||
aa3378df | 219 | /* |
220 | * Verify that we are successfully disconnected from the controlling | |
221 | * tty. | |
222 | */ | |
5260325f | 223 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); |
224 | if (fd >= 0) { | |
225 | error("Failed to disconnect from controlling tty."); | |
226 | close(fd); | |
227 | } | |
228 | /* Make it our controlling tty. */ | |
8efc0c15 | 229 | #ifdef TIOCSCTTY |
5260325f | 230 | debug("Setting controlling tty using TIOCSCTTY."); |
aa3378df | 231 | /* |
232 | * We ignore errors from this, because HPSUX defines TIOCSCTTY, but | |
233 | * returns EINVAL with these arguments, and there is absolutely no | |
234 | * documentation. | |
235 | */ | |
5260325f | 236 | ioctl(*ttyfd, TIOCSCTTY, NULL); |
8efc0c15 | 237 | #endif /* TIOCSCTTY */ |
4e577b89 | 238 | #ifdef HAVE_VHANGUP |
239 | old = signal(SIGHUP, SIG_IGN); | |
240 | vhangup(); | |
241 | signal(SIGHUP, old); | |
242 | #endif /* HAVE_VHANGUP */ | |
5260325f | 243 | fd = open(ttyname, O_RDWR); |
4e577b89 | 244 | if (fd < 0) { |
5260325f | 245 | error("%.100s: %.100s", ttyname, strerror(errno)); |
4e577b89 | 246 | } else { |
247 | #ifdef HAVE_VHANGUP | |
248 | close(*ttyfd); | |
249 | *ttyfd = fd; | |
250 | #else /* HAVE_VHANGUP */ | |
5260325f | 251 | close(fd); |
4e577b89 | 252 | #endif /* HAVE_VHANGUP */ |
253 | } | |
5260325f | 254 | /* Verify that we now have a controlling tty. */ |
255 | fd = open("/dev/tty", O_WRONLY); | |
256 | if (fd < 0) | |
257 | error("open /dev/tty failed - could not set controlling tty: %.100s", | |
258 | strerror(errno)); | |
259 | else { | |
260 | close(fd); | |
261 | } | |
8efc0c15 | 262 | } |
263 | ||
264 | /* Changes the window size associated with the pty. */ | |
265 | ||
6ae2364d | 266 | void |
5260325f | 267 | pty_change_window_size(int ptyfd, int row, int col, |
268 | int xpixel, int ypixel) | |
8efc0c15 | 269 | { |
5260325f | 270 | struct winsize w; |
271 | w.ws_row = row; | |
272 | w.ws_col = col; | |
273 | w.ws_xpixel = xpixel; | |
274 | w.ws_ypixel = ypixel; | |
275 | (void) ioctl(ptyfd, TIOCSWINSZ, &w); | |
8efc0c15 | 276 | } |
817175bc | 277 | |
278 | void | |
279 | pty_setowner(struct passwd *pw, const char *ttyname) | |
280 | { | |
281 | struct group *grp; | |
282 | gid_t gid; | |
283 | mode_t mode; | |
284 | ||
285 | /* Determine the group to make the owner of the tty. */ | |
286 | grp = getgrnam("tty"); | |
287 | if (grp) { | |
288 | gid = grp->gr_gid; | |
289 | mode = S_IRUSR | S_IWUSR | S_IWGRP; | |
290 | } else { | |
291 | gid = pw->pw_gid; | |
292 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; | |
293 | } | |
294 | ||
295 | /* Change ownership of the tty. */ | |
296 | if (chown(ttyname, pw->pw_uid, gid) < 0) | |
297 | fatal("chown(%.100s, %d, %d) failed: %.100s", | |
298 | ttyname, pw->pw_uid, gid, strerror(errno)); | |
299 | if (chmod(ttyname, mode) < 0) | |
300 | fatal("chmod(%.100s, 0%o) failed: %.100s", | |
301 | ttyname, mode, strerror(errno)); | |
302 | } |