sshpty.c (114426) | sshpty.c (124211) |
---|---|
1/* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * Allocating a pseudo-terminal, and making it the controlling tty. 6 * 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". 12 */ 13 14#include "includes.h" | 1/* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * Allocating a pseudo-terminal, and making it the controlling tty. 6 * 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". 12 */ 13 14#include "includes.h" |
15RCSID("$OpenBSD: sshpty.c,v 1.8 2003/02/03 08:56:16 markus Exp $"); | 15RCSID("$OpenBSD: sshpty.c,v 1.10 2003/06/12 07:57:38 markus Exp $"); |
16 17#ifdef HAVE_UTIL_H 18# include <util.h> 19#endif /* HAVE_UTIL_H */ 20 21#include "sshpty.h" 22#include "log.h" 23#include "misc.h" --- 72 unchanged lines hidden (view full) --- 96 char *pts; 97 mysig_t old_signal; 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 } | 16 17#ifdef HAVE_UTIL_H 18# include <util.h> 19#endif /* HAVE_UTIL_H */ 20 21#include "sshpty.h" 22#include "log.h" 23#include "misc.h" --- 72 unchanged lines hidden (view full) --- 96 char *pts; 97 mysig_t old_signal; 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 } |
104 old_signal = mysignal(SIGCHLD, SIG_DFL); | 104 old_signal = signal(SIGCHLD, SIG_DFL); |
105 if (grantpt(ptm) < 0) { 106 error("grantpt: %.100s", strerror(errno)); 107 return 0; 108 } | 105 if (grantpt(ptm) < 0) { 106 error("grantpt: %.100s", strerror(errno)); 107 return 0; 108 } |
109 mysignal(SIGCHLD, old_signal); | 109 signal(SIGCHLD, old_signal); |
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."); 117 strlcpy(namebuf, pts, namebuflen); --- 103 unchanged lines hidden (view full) --- 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 } 227 /* set tty modes to a sane state for broken clients */ 228 if (tcgetattr(*ptyfd, &tio) < 0) | 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."); 117 strlcpy(namebuf, pts, namebuflen); --- 103 unchanged lines hidden (view full) --- 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 } 227 /* set tty modes to a sane state for broken clients */ 228 if (tcgetattr(*ptyfd, &tio) < 0) |
229 log("Getting tty modes for pty failed: %.100s", strerror(errno)); | 229 logit("Getting tty modes for pty failed: %.100s", strerror(errno)); |
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) | 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) |
237 log("Setting tty modes for pty failed: %.100s", strerror(errno)); | 237 logit("Setting tty modes for pty failed: %.100s", strerror(errno)); |
238 } 239 240 return 1; 241 } 242 return 0; 243#endif /* CRAY */ 244#endif /* HAVE_DEV_PTS_AND_PTC */ 245#endif /* HAVE_DEV_PTMX */ --- 7 unchanged lines hidden (view full) --- 253pty_release(const char *ttyname) 254{ 255 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) 256 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); 257 if (chmod(ttyname, (mode_t) 0666) < 0) 258 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); 259} 260 | 238 } 239 240 return 1; 241 } 242 return 0; 243#endif /* CRAY */ 244#endif /* HAVE_DEV_PTS_AND_PTC */ 245#endif /* HAVE_DEV_PTMX */ --- 7 unchanged lines hidden (view full) --- 253pty_release(const char *ttyname) 254{ 255 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) 256 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); 257 if (chmod(ttyname, (mode_t) 0666) < 0) 258 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); 259} 260 |
261/* Makes the tty the processes controlling tty and sets it to sane modes. */ | 261/* Makes the tty the process's controlling tty and sets it to sane modes. */ |
262 263void 264pty_make_controlling_tty(int *ttyfd, const char *ttyname) 265{ 266 int fd; 267#ifdef USE_VHANGUP 268 void *old; 269#endif /* USE_VHANGUP */ 270 271#ifdef _UNICOS 272 if (setsid() < 0) 273 error("setsid: %.100s", strerror(errno)); 274 275 fd = open(ttyname, O_RDWR|O_NOCTTY); 276 if (fd != -1) { | 262 263void 264pty_make_controlling_tty(int *ttyfd, const char *ttyname) 265{ 266 int fd; 267#ifdef USE_VHANGUP 268 void *old; 269#endif /* USE_VHANGUP */ 270 271#ifdef _UNICOS 272 if (setsid() < 0) 273 error("setsid: %.100s", strerror(errno)); 274 275 fd = open(ttyname, O_RDWR|O_NOCTTY); 276 if (fd != -1) { |
277 mysignal(SIGHUP, SIG_IGN); | 277 signal(SIGHUP, SIG_IGN); |
278 ioctl(fd, TCVHUP, (char *)NULL); | 278 ioctl(fd, TCVHUP, (char *)NULL); |
279 mysignal(SIGHUP, SIG_DFL); | 279 signal(SIGHUP, SIG_DFL); |
280 setpgid(0, 0); 281 close(fd); 282 } else { 283 error("Failed to disconnect from controlling tty."); 284 } 285 286 debug("Setting controlling tty using TCSETCTTY."); 287 ioctl(*ttyfd, TCSETCTTY, NULL); --- 30 unchanged lines hidden (view full) --- 318 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) 319 error("ioctl(TIOCSCTTY): %.100s", strerror(errno)); 320#endif /* TIOCSCTTY */ 321#ifdef HAVE_NEWS4 322 if (setpgrp(0,0) < 0) 323 error("SETPGRP %s",strerror(errno)); 324#endif /* HAVE_NEWS4 */ 325#ifdef USE_VHANGUP | 280 setpgid(0, 0); 281 close(fd); 282 } else { 283 error("Failed to disconnect from controlling tty."); 284 } 285 286 debug("Setting controlling tty using TCSETCTTY."); 287 ioctl(*ttyfd, TCSETCTTY, NULL); --- 30 unchanged lines hidden (view full) --- 318 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) 319 error("ioctl(TIOCSCTTY): %.100s", strerror(errno)); 320#endif /* TIOCSCTTY */ 321#ifdef HAVE_NEWS4 322 if (setpgrp(0,0) < 0) 323 error("SETPGRP %s",strerror(errno)); 324#endif /* HAVE_NEWS4 */ 325#ifdef USE_VHANGUP |
326 old = mysignal(SIGHUP, SIG_IGN); | 326 old = signal(SIGHUP, SIG_IGN); |
327 vhangup(); | 327 vhangup(); |
328 mysignal(SIGHUP, old); | 328 signal(SIGHUP, old); |
329#endif /* USE_VHANGUP */ 330 fd = open(ttyname, O_RDWR); 331 if (fd < 0) { 332 error("%.100s: %.100s", ttyname, strerror(errno)); 333 } else { 334#ifdef USE_VHANGUP 335 close(*ttyfd); 336 *ttyfd = fd; --- 67 unchanged lines hidden (view full) --- 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) 411 debug("chmod(%.100s, 0%o) failed: %.100s", | 329#endif /* USE_VHANGUP */ 330 fd = open(ttyname, O_RDWR); 331 if (fd < 0) { 332 error("%.100s: %.100s", ttyname, strerror(errno)); 333 } else { 334#ifdef USE_VHANGUP 335 close(*ttyfd); 336 *ttyfd = fd; --- 67 unchanged lines hidden (view full) --- 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) 411 debug("chmod(%.100s, 0%o) failed: %.100s", |
412 ttyname, mode, strerror(errno)); | 412 ttyname, (u_int)mode, strerror(errno)); |
413 else 414 fatal("chmod(%.100s, 0%o) failed: %.100s", | 413 else 414 fatal("chmod(%.100s, 0%o) failed: %.100s", |
415 ttyname, mode, strerror(errno)); | 415 ttyname, (u_int)mode, strerror(errno)); |
416 } 417 } 418} | 416 } 417 } 418} |