sshpty.c revision 76262
1286171Smarkj/* 2210753Srpaulo * Author: Tatu Ylonen <ylo@cs.hut.fi> 3210753Srpaulo * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4210753Srpaulo * All rights reserved 5210753Srpaulo * Allocating a pseudo-terminal, and making it the controlling tty. 6210753Srpaulo * 7210753Srpaulo * As far as I am concerned, the code I have written for this software 8210753Srpaulo * can be used freely for any purpose. Any derived versions of this 9210753Srpaulo * software must be clearly marked as such, and if the derived work is 10210753Srpaulo * incompatible with the protocol description in the RFC file, it must be 11210753Srpaulo * called by a name other than "ssh" or "Secure Shell". 12210753Srpaulo */ 13210753Srpaulo 14210753Srpaulo#include "includes.h" 15210753SrpauloRCSID("$OpenBSD: sshpty.c,v 1.1 2001/03/04 01:46:30 djm Exp $"); 16210753SrpauloRCSID("$FreeBSD: head/crypto/openssh/sshpty.c 76262 2001-05-04 04:14:23Z green $"); 17210753Srpaulo 18210753Srpaulo#include <libutil.h> 19210753Srpaulo#include "sshpty.h" 20210753Srpaulo#include "log.h" 21210753Srpaulo 22210753Srpaulo/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */ 23210753Srpaulo#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY) 24210753Srpaulo#undef HAVE_DEV_PTMX 25210753Srpaulo#endif 26210753Srpaulo 27210753Srpaulo#ifndef O_NOCTTY 28210753Srpaulo#define O_NOCTTY 0 29210753Srpaulo#endif 30210753Srpaulo 31210753Srpaulo/* 32210753Srpaulo * Allocates and opens a pty. Returns 0 if no pty could be allocated, or 33210753Srpaulo * nonzero if a pty was successfully allocated. On success, open file 34210753Srpaulo * descriptors for the pty and tty sides and the name of the tty side are 35210753Srpaulo * returned (the buffer must be able to hold at least 64 characters). 36210753Srpaulo */ 37210753Srpaulo 38210753Srpauloint 39210753Srpaulopty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) 40210753Srpaulo{ 41210753Srpaulo#if defined(HAVE_OPENPTY) || defined(BSD4_4) 42210753Srpaulo /* openpty(3) exists in OSF/1 and some other os'es */ 43210753Srpaulo char buf[64]; 44210753Srpaulo int i; 45210753Srpaulo 46210753Srpaulo i = openpty(ptyfd, ttyfd, buf, NULL, NULL); 47210753Srpaulo if (i < 0) { 48210753Srpaulo error("openpty: %.100s", strerror(errno)); 49210753Srpaulo return 0; 50210753Srpaulo } 51250685Smarkj strlcpy(namebuf, buf, namebuflen); /* possible truncation */ 52210753Srpaulo return 1; 53250685Smarkj#else /* HAVE_OPENPTY */ 54210753Srpaulo#ifdef HAVE__GETPTY 55211545Srpaulo /* 56210753Srpaulo * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more 57210753Srpaulo * pty's automagically when needed 58210753Srpaulo */ 59210753Srpaulo char *slave; 60210753Srpaulo 61210753Srpaulo slave = _getpty(ptyfd, O_RDWR, 0622, 0); 62210753Srpaulo if (slave == NULL) { 63210753Srpaulo error("_getpty: %.100s", strerror(errno)); 64250685Smarkj return 0; 65250685Smarkj } 66250685Smarkj strlcpy(namebuf, slave, namebuflen); 67250685Smarkj /* Open the slave side. */ 68210753Srpaulo *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); 69250685Smarkj if (*ttyfd < 0) { 70210753Srpaulo error("%.200s: %.100s", namebuf, strerror(errno)); 71210753Srpaulo close(*ptyfd); 72250685Smarkj return 0; 73210753Srpaulo } 74250685Smarkj return 1; 75210753Srpaulo#else /* HAVE__GETPTY */ 76210753Srpaulo#ifdef HAVE_DEV_PTMX 77210753Srpaulo /* 78210753Srpaulo * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3 79210753Srpaulo * also has bsd-style ptys, but they simply do not work.) 80210753Srpaulo */ 81250685Smarkj int ptm; 82210753Srpaulo char *pts; 83250685Smarkj 84250685Smarkj ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY); 85250685Smarkj if (ptm < 0) { 86250685Smarkj error("/dev/ptmx: %.100s", strerror(errno)); 87250685Smarkj return 0; 88250685Smarkj } 89250685Smarkj if (grantpt(ptm) < 0) { 90250685Smarkj error("grantpt: %.100s", strerror(errno)); 91250685Smarkj return 0; 92210753Srpaulo } 93210753Srpaulo if (unlockpt(ptm) < 0) { 94210753Srpaulo error("unlockpt: %.100s", strerror(errno)); 95210753Srpaulo return 0; 96210753Srpaulo } 97210753Srpaulo pts = ptsname(ptm); 98 if (pts == NULL) 99 error("Slave pty side name could not be obtained."); 100 strlcpy(namebuf, pts, namebuflen); 101 *ptyfd = ptm; 102 103 /* Open the slave side. */ 104 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); 105 if (*ttyfd < 0) { 106 error("%.100s: %.100s", namebuf, strerror(errno)); 107 close(*ptyfd); 108 return 0; 109 } 110 /* Push the appropriate streams modules, as described in Solaris pts(7). */ 111 if (ioctl(*ttyfd, I_PUSH, "ptem") < 0) 112 error("ioctl I_PUSH ptem: %.100s", strerror(errno)); 113 if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0) 114 error("ioctl I_PUSH ldterm: %.100s", strerror(errno)); 115 if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0) 116 error("ioctl I_PUSH ttcompat: %.100s", strerror(errno)); 117 return 1; 118#else /* HAVE_DEV_PTMX */ 119#ifdef HAVE_DEV_PTS_AND_PTC 120 /* AIX-style pty code. */ 121 const char *name; 122 123 *ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY); 124 if (*ptyfd < 0) { 125 error("Could not open /dev/ptc: %.100s", strerror(errno)); 126 return 0; 127 } 128 name = ttyname(*ptyfd); 129 if (!name) 130 fatal("Open of /dev/ptc returns device for which ttyname fails."); 131 strlcpy(namebuf, name, namebuflen); 132 *ttyfd = open(name, O_RDWR | O_NOCTTY); 133 if (*ttyfd < 0) { 134 error("Could not open pty slave side %.100s: %.100s", 135 name, strerror(errno)); 136 close(*ptyfd); 137 return 0; 138 } 139 return 1; 140#else /* HAVE_DEV_PTS_AND_PTC */ 141 /* BSD-style pty code. */ 142 char buf[64]; 143 int i; 144 const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; 145 const char *ptyminors = "0123456789abcdef"; 146 int num_minors = strlen(ptyminors); 147 int num_ptys = strlen(ptymajors) * num_minors; 148 149 for (i = 0; i < num_ptys; i++) { 150 snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors], 151 ptyminors[i % num_minors]); 152 *ptyfd = open(buf, O_RDWR | O_NOCTTY); 153 if (*ptyfd < 0) 154 continue; 155 snprintf(namebuf, namebuflen, "/dev/tty%c%c", 156 ptymajors[i / num_minors], ptyminors[i % num_minors]); 157 158 /* Open the slave side. */ 159 *ttyfd = open(namebuf, O_RDWR | O_NOCTTY); 160 if (*ttyfd < 0) { 161 error("%.100s: %.100s", namebuf, strerror(errno)); 162 close(*ptyfd); 163 return 0; 164 } 165 return 1; 166 } 167 return 0; 168#endif /* HAVE_DEV_PTS_AND_PTC */ 169#endif /* HAVE_DEV_PTMX */ 170#endif /* HAVE__GETPTY */ 171#endif /* HAVE_OPENPTY */ 172} 173 174/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ 175 176void 177pty_release(const char *ttyname) 178{ 179 if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) 180 error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); 181 if (chmod(ttyname, (mode_t) 0666) < 0) 182 error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); 183} 184 185/* Makes the tty the processes controlling tty and sets it to sane modes. */ 186 187void 188pty_make_controlling_tty(int *ttyfd, const char *ttyname) 189{ 190 int fd; 191 192 /* First disconnect from the old controlling tty. */ 193#ifdef TIOCNOTTY 194 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); 195 if (fd >= 0) { 196 (void) ioctl(fd, TIOCNOTTY, NULL); 197 close(fd); 198 } 199#endif /* TIOCNOTTY */ 200 if (setsid() < 0) 201 error("setsid: %.100s", strerror(errno)); 202 203 /* 204 * Verify that we are successfully disconnected from the controlling 205 * tty. 206 */ 207 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); 208 if (fd >= 0) { 209 error("Failed to disconnect from controlling tty."); 210 close(fd); 211 } 212 /* Make it our controlling tty. */ 213#ifdef TIOCSCTTY 214 debug("Setting controlling tty using TIOCSCTTY."); 215 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) 216 error("ioctl(TIOCSCTTY): %.100s", strerror(errno)); 217#endif /* TIOCSCTTY */ 218 fd = open(ttyname, O_RDWR); 219 if (fd < 0) 220 error("%.100s: %.100s", ttyname, strerror(errno)); 221 else 222 close(fd); 223 224 /* Verify that we now have a controlling tty. */ 225 fd = open(_PATH_TTY, O_WRONLY); 226 if (fd < 0) 227 error("open /dev/tty failed - could not set controlling tty: %.100s", 228 strerror(errno)); 229 else { 230 close(fd); 231 } 232} 233 234/* Changes the window size associated with the pty. */ 235 236void 237pty_change_window_size(int ptyfd, int row, int col, 238 int xpixel, int ypixel) 239{ 240 struct winsize w; 241 w.ws_row = row; 242 w.ws_col = col; 243 w.ws_xpixel = xpixel; 244 w.ws_ypixel = ypixel; 245 (void) ioctl(ptyfd, TIOCSWINSZ, &w); 246} 247 248void 249pty_setowner(struct passwd *pw, const char *ttyname) 250{ 251 struct group *grp; 252 gid_t gid; 253 mode_t mode; 254 struct stat st; 255 256 /* Determine the group to make the owner of the tty. */ 257 grp = getgrnam("tty"); 258 if (grp) { 259 gid = grp->gr_gid; 260 mode = S_IRUSR | S_IWUSR | S_IWGRP; 261 } else { 262 gid = pw->pw_gid; 263 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 264 } 265 266 /* 267 * Change owner and mode of the tty as required. 268 * Warn but continue if filesystem is read-only and the uids match. 269 */ 270 if (stat(ttyname, &st)) 271 fatal("stat(%.100s) failed: %.100s", ttyname, 272 strerror(errno)); 273 274 if (st.st_uid != pw->pw_uid || st.st_gid != gid) { 275 if (chown(ttyname, pw->pw_uid, gid) < 0) { 276 if (errno == EROFS && st.st_uid == pw->pw_uid) 277 error("chown(%.100s, %d, %d) failed: %.100s", 278 ttyname, pw->pw_uid, gid, 279 strerror(errno)); 280 else 281 fatal("chown(%.100s, %d, %d) failed: %.100s", 282 ttyname, pw->pw_uid, gid, 283 strerror(errno)); 284 } 285 } 286 287 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) { 288 if (chmod(ttyname, mode) < 0) { 289 if (errno == EROFS && 290 (st.st_mode & (S_IRGRP | S_IROTH)) == 0) 291 error("chmod(%.100s, 0%o) failed: %.100s", 292 ttyname, mode, strerror(errno)); 293 else 294 fatal("chmod(%.100s, 0%o) failed: %.100s", 295 ttyname, mode, strerror(errno)); 296 } 297 } 298} 299