]>
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" | |
4f7893dc | 15 | RCSID("$OpenBSD: sshpty.c,v 1.3 2001/07/22 21:32:27 markus Exp $"); |
8efc0c15 | 16 | |
884bcb37 | 17 | #ifdef HAVE_UTIL_H |
18 | # include <util.h> | |
19 | #endif /* HAVE_UTIL_H */ | |
20 | ||
1729c161 | 21 | #include "sshpty.h" |
42f11eb2 | 22 | #include "log.h" |
5ad9f968 | 23 | #include "misc.h" |
5260325f | 24 | |
8efc0c15 | 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 | ||
6d8c4ea4 | 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 | ||
8efc0c15 | 37 | #ifndef O_NOCTTY |
38 | #define O_NOCTTY 0 | |
39 | #endif | |
40 | ||
aa3378df | 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 | */ | |
8efc0c15 | 47 | |
6ae2364d | 48 | int |
a408af76 | 49 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
8efc0c15 | 50 | { |
a408af76 | 51 | #if defined(HAVE_OPENPTY) || defined(BSD4_4) |
5260325f | 52 | /* openpty(3) exists in OSF/1 and some other os'es */ |
b82f1310 | 53 | char *name; |
5260325f | 54 | int i; |
8efc0c15 | 55 | |
b82f1310 | 56 | i = openpty(ptyfd, ttyfd, NULL, NULL, NULL); |
5260325f | 57 | if (i < 0) { |
58 | error("openpty: %.100s", strerror(errno)); | |
59 | return 0; | |
60 | } | |
b82f1310 | 61 | name = ttyname(*ttyfd); |
62 | if (!name) | |
63 | fatal("openpty returns device for which ttyname fails."); | |
64 | ||
65 | strlcpy(namebuf, name, namebuflen); /* possible truncation */ | |
5260325f | 66 | return 1; |
8efc0c15 | 67 | #else /* HAVE_OPENPTY */ |
68 | #ifdef HAVE__GETPTY | |
aa3378df | 69 | /* |
70 | * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more | |
71 | * pty's automagically when needed | |
72 | */ | |
5260325f | 73 | char *slave; |
74 | ||
75 | slave = _getpty(ptyfd, O_RDWR, 0622, 0); | |
76 | if (slave == NULL) { | |
77 | error("_getpty: %.100s", strerror(errno)); | |
78 | return 0; | |
79 | } | |
a408af76 | 80 | strlcpy(namebuf, slave, namebuflen); |
5260325f | 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; | |
8efc0c15 | 89 | #else /* HAVE__GETPTY */ |
a7effaac | 90 | #if defined(HAVE_DEV_PTMX) |
aa3378df | 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 | */ | |
5260325f | 95 | int ptm; |
96 | char *pts; | |
5ad9f968 | 97 | mysig_t old_signal; |
5260325f | 98 | |
99 | ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY); | |
100 | if (ptm < 0) { | |
101 | error("/dev/ptmx: %.100s", strerror(errno)); | |
102 | return 0; | |
103 | } | |
5ad9f968 | 104 | old_signal = mysignal(SIGCHLD, SIG_DFL); |
5260325f | 105 | if (grantpt(ptm) < 0) { |
106 | error("grantpt: %.100s", strerror(errno)); | |
107 | return 0; | |
108 | } | |
5ad9f968 | 109 | mysignal(SIGCHLD, old_signal); |
5260325f | 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."); | |
a408af76 | 117 | strlcpy(namebuf, pts, namebuflen); |
5260325f | 118 | *ptyfd = ptm; |
119 | ||
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 | } | |
3c62e7eb | 127 | #ifndef HAVE_CYGWIN |
96054e6f | 128 | /* |
129 | * Push the appropriate streams modules, as described in Solaris pts(7). | |
130 | * HP-UX pts(7) doesn't have ttcompat module. | |
131 | */ | |
5260325f | 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)); | |
96054e6f | 136 | #ifndef __hpux |
5260325f | 137 | if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) |
138 | error("ioctl I_PUSH ttcompat: %.100s", strerror(errno)); | |
3c62e7eb | 139 | #endif |
be48d23c | 140 | #endif |
5260325f | 141 | return 1; |
8efc0c15 | 142 | #else /* HAVE_DEV_PTMX */ |
143 | #ifdef HAVE_DEV_PTS_AND_PTC | |
5260325f | 144 | /* AIX-style pty code. */ |
145 | const char *name; | |
8efc0c15 | 146 | |
5260325f | 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; | |
151 | } | |
152 | name = ttyname(*ptyfd); | |
153 | if (!name) | |
154 | fatal("Open of /dev/ptc returns device for which ttyname fails."); | |
a408af76 | 155 | strlcpy(namebuf, name, namebuflen); |
5260325f | 156 | *ttyfd = open(name, O_RDWR | O_NOCTTY); |
157 | if (*ttyfd < 0) { | |
158 | error("Could not open pty slave side %.100s: %.100s", | |
159 | name, strerror(errno)); | |
160 | close(*ptyfd); | |
161 | return 0; | |
162 | } | |
163 | return 1; | |
8efc0c15 | 164 | #else /* HAVE_DEV_PTS_AND_PTC */ |
1a23ac2c | 165 | #ifdef _CRAY |
166 | char buf[64]; | |
167 | int i; | |
168 | int highpty; | |
169 | ||
170 | #ifdef _SC_CRAY_NPTY | |
171 | highpty = sysconf(_SC_CRAY_NPTY); | |
172 | if (highpty == -1) | |
173 | highpty = 128; | |
174 | #else | |
175 | highpty = 128; | |
176 | #endif | |
177 | ||
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) continue; | |
182 | snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i); | |
183 | /* Open the slave side. */ | |
184 | *ttyfd = open(namebuf, O_RDWR|O_NOCTTY); | |
185 | if (*ttyfd < 0) { | |
186 | error("%.100s: %.100s", namebuf, strerror(errno)); | |
187 | close(*ptyfd); | |
188 | } | |
189 | return 1; | |
190 | } | |
191 | return 0; | |
192 | #else | |
5260325f | 193 | /* BSD-style pty code. */ |
194 | char buf[64]; | |
195 | int i; | |
aa3378df | 196 | const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
5260325f | 197 | const char *ptyminors = "0123456789abcdef"; |
198 | int num_minors = strlen(ptyminors); | |
199 | int num_ptys = strlen(ptymajors) * num_minors; | |
200 | ||
201 | for (i = 0; i < num_ptys; i++) { | |
202 | snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors], | |
203 | ptyminors[i % num_minors]); | |
95f1eccc | 204 | snprintf(namebuf, namebuflen, "/dev/tty%c%c", |
a408af76 | 205 | ptymajors[i / num_minors], ptyminors[i % num_minors]); |
5260325f | 206 | |
b5f90139 | 207 | *ptyfd = open(buf, O_RDWR | O_NOCTTY); |
208 | if (*ptyfd < 0) { | |
209 | /* Try SCO style naming */ | |
210 | snprintf(buf, sizeof buf, "/dev/ptyp%d", i); | |
211 | snprintf(namebuf, namebuflen, "/dev/ttyp%d", i); | |
212 | *ptyfd = open(buf, O_RDWR | O_NOCTTY); | |
213 | if (*ptyfd < 0) | |
214 | continue; | |
2b87da3b | 215 | } |
216 | ||
5260325f | 217 | /* Open the slave side. */ |
218 | *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); | |
219 | if (*ttyfd < 0) { | |
220 | error("%.100s: %.100s", namebuf, strerror(errno)); | |
221 | close(*ptyfd); | |
222 | return 0; | |
223 | } | |
224 | return 1; | |
8efc0c15 | 225 | } |
5260325f | 226 | return 0; |
1a23ac2c | 227 | #endif /* CRAY */ |
8efc0c15 | 228 | #endif /* HAVE_DEV_PTS_AND_PTC */ |
229 | #endif /* HAVE_DEV_PTMX */ | |
230 | #endif /* HAVE__GETPTY */ | |
231 | #endif /* HAVE_OPENPTY */ | |
232 | } | |
233 | ||
5260325f | 234 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
8efc0c15 | 235 | |
6ae2364d | 236 | void |
5260325f | 237 | pty_release(const char *ttyname) |
8efc0c15 | 238 | { |
3276571c | 239 | if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) |
817175bc | 240 | error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); |
3276571c | 241 | if (chmod(ttyname, (mode_t) 0666) < 0) |
817175bc | 242 | error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); |
8efc0c15 | 243 | } |
244 | ||
245 | /* Makes the tty the processes controlling tty and sets it to sane modes. */ | |
246 | ||
6ae2364d | 247 | void |
5260325f | 248 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) |
8efc0c15 | 249 | { |
1a23ac2c | 250 | #ifdef _CRAY |
251 | int fd; | |
252 | ||
253 | if (setsid() < 0) | |
254 | error("setsid: %.100s", strerror(errno)); | |
255 | ||
256 | fd = open(ttyname, O_RDWR|O_NOCTTY); | |
257 | if (fd >= 0) { | |
258 | signal(SIGHUP, SIG_IGN); | |
259 | ioctl(fd, TCVHUP, (char *)0); | |
260 | signal(SIGHUP, SIG_DFL); | |
261 | setpgid(0,0); | |
262 | close(fd); | |
263 | } else { | |
264 | error("Failed to disconnect from controlling tty."); | |
265 | } | |
266 | ||
267 | ||
268 | debug("Setting controlling tty using TCSETCTTY.\n"); | |
269 | ioctl(*ttyfd, TCSETCTTY, NULL); | |
270 | ||
271 | fd = open("/dev/tty", O_RDWR); | |
272 | ||
273 | if (fd < 0) | |
274 | error("%.100s: %.100s", ttyname, strerror(errno)); | |
275 | ||
276 | close(*ttyfd); | |
277 | *ttyfd = fd; | |
278 | #else | |
5260325f | 279 | int fd; |
3c62e7eb | 280 | #ifdef USE_VHANGUP |
4e577b89 | 281 | void *old; |
3c62e7eb | 282 | #endif /* USE_VHANGUP */ |
8efc0c15 | 283 | |
5260325f | 284 | /* First disconnect from the old controlling tty. */ |
8efc0c15 | 285 | #ifdef TIOCNOTTY |
5ca51e19 | 286 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
5260325f | 287 | if (fd >= 0) { |
288 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
289 | close(fd); | |
290 | } | |
8efc0c15 | 291 | #endif /* TIOCNOTTY */ |
5260325f | 292 | if (setsid() < 0) |
293 | error("setsid: %.100s", strerror(errno)); | |
294 | ||
aa3378df | 295 | /* |
296 | * Verify that we are successfully disconnected from the controlling | |
297 | * tty. | |
298 | */ | |
5ca51e19 | 299 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
5260325f | 300 | if (fd >= 0) { |
301 | error("Failed to disconnect from controlling tty."); | |
302 | close(fd); | |
303 | } | |
304 | /* Make it our controlling tty. */ | |
8efc0c15 | 305 | #ifdef TIOCSCTTY |
5260325f | 306 | debug("Setting controlling tty using TIOCSCTTY."); |
a3bf38d0 | 307 | if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) |
308 | error("ioctl(TIOCSCTTY): %.100s", strerror(errno)); | |
8efc0c15 | 309 | #endif /* TIOCSCTTY */ |
66d6c27e | 310 | #ifdef HAVE_NEWS4 |
311 | if (setpgrp(0,0) < 0) | |
312 | error("SETPGRP %s",strerror(errno)); | |
313 | #endif /* HAVE_NEWS4 */ | |
3c62e7eb | 314 | #ifdef USE_VHANGUP |
4e577b89 | 315 | old = signal(SIGHUP, SIG_IGN); |
316 | vhangup(); | |
317 | signal(SIGHUP, old); | |
3c62e7eb | 318 | #endif /* USE_VHANGUP */ |
5260325f | 319 | fd = open(ttyname, O_RDWR); |
4e577b89 | 320 | if (fd < 0) { |
5260325f | 321 | error("%.100s: %.100s", ttyname, strerror(errno)); |
4e577b89 | 322 | } else { |
3c62e7eb | 323 | #ifdef USE_VHANGUP |
4e577b89 | 324 | close(*ttyfd); |
325 | *ttyfd = fd; | |
3c62e7eb | 326 | #else /* USE_VHANGUP */ |
5260325f | 327 | close(fd); |
3c62e7eb | 328 | #endif /* USE_VHANGUP */ |
4e577b89 | 329 | } |
5260325f | 330 | /* Verify that we now have a controlling tty. */ |
5ca51e19 | 331 | fd = open(_PATH_TTY, O_WRONLY); |
5260325f | 332 | if (fd < 0) |
333 | error("open /dev/tty failed - could not set controlling tty: %.100s", | |
334 | strerror(errno)); | |
335 | else { | |
336 | close(fd); | |
337 | } | |
1a23ac2c | 338 | #endif |
8efc0c15 | 339 | } |
340 | ||
341 | /* Changes the window size associated with the pty. */ | |
342 | ||
6ae2364d | 343 | void |
5260325f | 344 | pty_change_window_size(int ptyfd, int row, int col, |
345 | int xpixel, int ypixel) | |
8efc0c15 | 346 | { |
5260325f | 347 | struct winsize w; |
348 | w.ws_row = row; | |
349 | w.ws_col = col; | |
350 | w.ws_xpixel = xpixel; | |
351 | w.ws_ypixel = ypixel; | |
352 | (void) ioctl(ptyfd, TIOCSWINSZ, &w); | |
8efc0c15 | 353 | } |
817175bc | 354 | |
355 | void | |
356 | pty_setowner(struct passwd *pw, const char *ttyname) | |
357 | { | |
358 | struct group *grp; | |
359 | gid_t gid; | |
360 | mode_t mode; | |
227e8e86 | 361 | struct stat st; |
817175bc | 362 | |
363 | /* Determine the group to make the owner of the tty. */ | |
364 | grp = getgrnam("tty"); | |
365 | if (grp) { | |
366 | gid = grp->gr_gid; | |
367 | mode = S_IRUSR | S_IWUSR | S_IWGRP; | |
368 | } else { | |
369 | gid = pw->pw_gid; | |
370 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; | |
371 | } | |
372 | ||
227e8e86 | 373 | /* |
374 | * Change owner and mode of the tty as required. | |
4f7893dc | 375 | * Warn but continue if filesystem is read-only and the uids match/ |
376 | * tty is owned by root. | |
227e8e86 | 377 | */ |
378 | if (stat(ttyname, &st)) | |
379 | fatal("stat(%.100s) failed: %.100s", ttyname, | |
380 | strerror(errno)); | |
381 | ||
382 | if (st.st_uid != pw->pw_uid || st.st_gid != gid) { | |
383 | if (chown(ttyname, pw->pw_uid, gid) < 0) { | |
7458aff1 | 384 | if (errno == EROFS && |
385 | (st.st_uid == pw->pw_uid || st.st_uid == 0)) | |
227e8e86 | 386 | error("chown(%.100s, %d, %d) failed: %.100s", |
2b87da3b | 387 | ttyname, pw->pw_uid, gid, |
227e8e86 | 388 | strerror(errno)); |
389 | else | |
390 | fatal("chown(%.100s, %d, %d) failed: %.100s", | |
2b87da3b | 391 | ttyname, pw->pw_uid, gid, |
227e8e86 | 392 | strerror(errno)); |
393 | } | |
394 | } | |
395 | ||
396 | if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) { | |
397 | if (chmod(ttyname, mode) < 0) { | |
398 | if (errno == EROFS && | |
399 | (st.st_mode & (S_IRGRP | S_IROTH)) == 0) | |
400 | error("chmod(%.100s, 0%o) failed: %.100s", | |
401 | ttyname, mode, strerror(errno)); | |
402 | else | |
403 | fatal("chmod(%.100s, 0%o) failed: %.100s", | |
404 | ttyname, mode, strerror(errno)); | |
405 | } | |
406 | } | |
817175bc | 407 | } |