1 /* 2 * Please note: this implementation of openpty() is far from complete. 3 * it is just enough for portable OpenSSH's needs. 4 */ 5 6 /* 7 * Copyright (c) 2004 Damien Miller <djm@mindrot.org> 8 * 9 * Permission to use, copy, modify, and distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 */ 21 22 /* 23 * Author: Tatu Ylonen <ylo@cs.hut.fi> 24 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 25 * All rights reserved 26 * Allocating a pseudo-terminal, and making it the controlling tty. 27 * 28 * As far as I am concerned, the code I have written for this software 29 * can be used freely for any purpose. Any derived versions of this 30 * software must be clearly marked as such, and if the derived work is 31 * incompatible with the protocol description in the RFC file, it must be 32 * called by a name other than "ssh" or "Secure Shell". 33 */ 34 35 #include "includes.h" 36 #if !defined(HAVE_OPENPTY) 37 38 #include <sys/types.h> 39 40 #include <stdlib.h> 41 42 #ifdef HAVE_SYS_STAT_H 43 # include <sys/stat.h> 44 #endif 45 #ifdef HAVE_SYS_IOCTL_H 46 # include <sys/ioctl.h> 47 #endif 48 49 #ifdef HAVE_FCNTL_H 50 # include <fcntl.h> 51 #endif 52 53 #ifdef HAVE_UTIL_H 54 # include <util.h> 55 #endif /* HAVE_UTIL_H */ 56 57 #ifdef HAVE_PTY_H 58 # include <pty.h> 59 #endif 60 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H) 61 # include <sys/stropts.h> 62 #endif 63 64 #include <signal.h> 65 #include <string.h> 66 #include <unistd.h> 67 68 #include "misc.h" 69 70 #ifndef O_NOCTTY 71 #define O_NOCTTY 0 72 #endif 73 74 int 75 openpty(int *amaster, int *aslave, char *name, struct termios *termp, 76 struct winsize *winp) 77 { 78 #if defined(HAVE__GETPTY) 79 /* 80 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more 81 * pty's automagically when needed 82 */ 83 char *slave; 84 85 if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL) 86 return (-1); 87 88 /* Open the slave side. */ 89 if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) { 90 close(*amaster); 91 return (-1); 92 } 93 return (0); 94 95 #elif defined(HAVE_DEV_PTMX) 96 /* 97 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3 98 * also has bsd-style ptys, but they simply do not work.) 99 */ 100 int ptm; 101 char *pts; 102 sshsig_t old_signal; 103 104 if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1) 105 return (-1); 106 107 /* XXX: need to close ptm on error? */ 108 old_signal = ssh_signal(SIGCHLD, SIG_DFL); 109 if (grantpt(ptm) < 0) 110 return (-1); 111 ssh_signal(SIGCHLD, old_signal); 112 113 if (unlockpt(ptm) < 0) 114 return (-1); 115 116 if ((pts = ptsname(ptm)) == NULL) 117 return (-1); 118 *amaster = ptm; 119 120 /* Open the slave side. */ 121 if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) { 122 close(*amaster); 123 return (-1); 124 } 125 126 # if defined(I_FIND) && defined(__SVR4) 127 /* 128 * If the streams modules have already been pushed then there 129 * is no more work to do here. 130 */ 131 if (ioctl(*aslave, I_FIND, "ptem") != 0) 132 return 0; 133 # endif 134 135 /* 136 * Try to push the appropriate streams modules, as described 137 * in Solaris pts(7). 138 */ 139 ioctl(*aslave, I_PUSH, "ptem"); 140 ioctl(*aslave, I_PUSH, "ldterm"); 141 # ifndef __hpux 142 ioctl(*aslave, I_PUSH, "ttcompat"); 143 # endif /* __hpux */ 144 145 return (0); 146 147 #elif defined(HAVE_DEV_PTS_AND_PTC) 148 /* AIX-style pty code. */ 149 const char *ttname; 150 151 if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1) 152 return (-1); 153 if ((ttname = ttyname(*amaster)) == NULL) 154 return (-1); 155 if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) { 156 close(*amaster); 157 return (-1); 158 } 159 return (0); 160 161 #else 162 /* BSD-style pty code. */ 163 char ptbuf[64], ttbuf[64]; 164 int i; 165 const char *ptymajors = "pqrstuvwxyzabcdefghijklmno" 166 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 167 const char *ptyminors = "0123456789abcdef"; 168 int num_minors = strlen(ptyminors); 169 int num_ptys = strlen(ptymajors) * num_minors; 170 struct termios tio; 171 172 for (i = 0; i < num_ptys; i++) { 173 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c", 174 ptymajors[i / num_minors], ptyminors[i % num_minors]); 175 snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c", 176 ptymajors[i / num_minors], ptyminors[i % num_minors]); 177 178 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) { 179 /* Try SCO style naming */ 180 snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i); 181 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i); 182 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) 183 continue; 184 } 185 186 /* Open the slave side. */ 187 if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) { 188 close(*amaster); 189 return (-1); 190 } 191 /* set tty modes to a sane state for broken clients */ 192 if (tcgetattr(*amaster, &tio) != -1) { 193 tio.c_lflag |= (ECHO | ISIG | ICANON); 194 tio.c_oflag |= (OPOST | ONLCR); 195 tio.c_iflag |= ICRNL; 196 tcsetattr(*amaster, TCSANOW, &tio); 197 } 198 199 return (0); 200 } 201 return (-1); 202 #endif 203 } 204 205 #endif /* !defined(HAVE_OPENPTY) */ 206 207