Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Allocating a pseudo-terminal, and making it the controlling tty. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Damien Miller | f4da3bb | 2004-01-21 17:07:16 +1100 | [diff] [blame] | 15 | RCSID("$OpenBSD: sshpty.c,v 1.11 2004/01/11 21:55:06 deraadt Exp $"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 16 | |
Damien Miller | 36b339a | 1999-12-14 10:54:47 +1100 | [diff] [blame] | 17 | #ifdef HAVE_UTIL_H |
| 18 | # include <util.h> |
| 19 | #endif /* HAVE_UTIL_H */ |
| 20 | |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 21 | #include "sshpty.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 22 | #include "log.h" |
Kevin Steves | 974fb9c | 2001-06-15 00:04:23 +0000 | [diff] [blame] | 23 | #include "misc.h" |
Damien Miller | 356a0b0 | 1999-11-08 15:30:59 +1100 | [diff] [blame] | 24 | |
Damien Miller | fce1648 | 1999-12-08 08:53:52 +1100 | [diff] [blame] | 25 | #ifdef HAVE_PTY_H |
| 26 | # include <pty.h> |
| 27 | #endif |
Damien Miller | fce1648 | 1999-12-08 08:53:52 +1100 | [diff] [blame] | 28 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 29 | #ifndef O_NOCTTY |
| 30 | #define O_NOCTTY 0 |
| 31 | #endif |
| 32 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 33 | /* |
| 34 | * Allocates and opens a pty. Returns 0 if no pty could be allocated, or |
| 35 | * nonzero if a pty was successfully allocated. On success, open file |
| 36 | * descriptors for the pty and tty sides and the name of the tty side are |
| 37 | * returned (the buffer must be able to hold at least 64 characters). |
| 38 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 40 | int |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 41 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 42 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 43 | /* openpty(3) exists in OSF/1 and some other os'es */ |
Damien Miller | 99e9243 | 2001-02-18 12:49:35 +1100 | [diff] [blame] | 44 | char *name; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | int i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 46 | |
Damien Miller | 99e9243 | 2001-02-18 12:49:35 +1100 | [diff] [blame] | 47 | i = openpty(ptyfd, ttyfd, NULL, NULL, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | if (i < 0) { |
| 49 | error("openpty: %.100s", strerror(errno)); |
| 50 | return 0; |
| 51 | } |
Damien Miller | 99e9243 | 2001-02-18 12:49:35 +1100 | [diff] [blame] | 52 | name = ttyname(*ttyfd); |
| 53 | if (!name) |
| 54 | fatal("openpty returns device for which ttyname fails."); |
| 55 | |
| 56 | strlcpy(namebuf, name, namebuflen); /* possible truncation */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 57 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 58 | } |
| 59 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 60 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 62 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 63 | pty_release(const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | { |
Damien Miller | 204ad07 | 2000-03-02 23:56:12 +1100 | [diff] [blame] | 65 | if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0) |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 66 | error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno)); |
Damien Miller | 204ad07 | 2000-03-02 23:56:12 +1100 | [diff] [blame] | 67 | if (chmod(ttyname, (mode_t) 0666) < 0) |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 68 | error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | } |
| 70 | |
Damien Miller | 469954d | 2003-06-18 20:25:33 +1000 | [diff] [blame] | 71 | /* Makes the tty the process's controlling tty and sets it to sane modes. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 73 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 74 | pty_make_controlling_tty(int *ttyfd, const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | { |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 76 | int fd; |
Damien Miller | 4a820ea | 2001-10-12 19:15:48 +1000 | [diff] [blame] | 77 | #ifdef USE_VHANGUP |
| 78 | void *old; |
| 79 | #endif /* USE_VHANGUP */ |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 80 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 81 | #ifdef _UNICOS |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 82 | if (setsid() < 0) |
| 83 | error("setsid: %.100s", strerror(errno)); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 84 | |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 85 | fd = open(ttyname, O_RDWR|O_NOCTTY); |
| 86 | if (fd != -1) { |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 87 | signal(SIGHUP, SIG_IGN); |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 88 | ioctl(fd, TCVHUP, (char *)NULL); |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 89 | signal(SIGHUP, SIG_DFL); |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 90 | setpgid(0, 0); |
| 91 | close(fd); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 92 | } else { |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 93 | error("Failed to disconnect from controlling tty."); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 94 | } |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 95 | |
Kevin Steves | f744b51 | 2001-08-14 20:31:49 +0000 | [diff] [blame] | 96 | debug("Setting controlling tty using TCSETCTTY."); |
| 97 | ioctl(*ttyfd, TCSETCTTY, NULL); |
| 98 | fd = open("/dev/tty", O_RDWR); |
| 99 | if (fd < 0) |
| 100 | error("%.100s: %.100s", ttyname, strerror(errno)); |
Ben Lindstrom | 6db66ff | 2001-08-06 23:29:16 +0000 | [diff] [blame] | 101 | close(*ttyfd); |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 102 | *ttyfd = fd; |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 103 | #else /* _UNICOS */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 104 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 105 | /* First disconnect from the old controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 106 | #ifdef TIOCNOTTY |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 107 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 108 | if (fd >= 0) { |
| 109 | (void) ioctl(fd, TIOCNOTTY, NULL); |
| 110 | close(fd); |
| 111 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | #endif /* TIOCNOTTY */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | if (setsid() < 0) |
| 114 | error("setsid: %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 116 | /* |
| 117 | * Verify that we are successfully disconnected from the controlling |
| 118 | * tty. |
| 119 | */ |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 120 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | if (fd >= 0) { |
| 122 | error("Failed to disconnect from controlling tty."); |
| 123 | close(fd); |
| 124 | } |
| 125 | /* Make it our controlling tty. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 126 | #ifdef TIOCSCTTY |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | debug("Setting controlling tty using TIOCSCTTY."); |
Kevin Steves | 6c0d627 | 2000-11-12 09:22:29 +0000 | [diff] [blame] | 128 | if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) |
| 129 | error("ioctl(TIOCSCTTY): %.100s", strerror(errno)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 130 | #endif /* TIOCSCTTY */ |
Ben Lindstrom | b562864 | 2000-10-18 00:02:25 +0000 | [diff] [blame] | 131 | #ifdef HAVE_NEWS4 |
| 132 | if (setpgrp(0,0) < 0) |
| 133 | error("SETPGRP %s",strerror(errno)); |
| 134 | #endif /* HAVE_NEWS4 */ |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 135 | #ifdef USE_VHANGUP |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 136 | old = signal(SIGHUP, SIG_IGN); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 137 | vhangup(); |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 138 | signal(SIGHUP, old); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 139 | #endif /* USE_VHANGUP */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 140 | fd = open(ttyname, O_RDWR); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 141 | if (fd < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 142 | error("%.100s: %.100s", ttyname, strerror(errno)); |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 143 | } else { |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 144 | #ifdef USE_VHANGUP |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 145 | close(*ttyfd); |
| 146 | *ttyfd = fd; |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 147 | #else /* USE_VHANGUP */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | close(fd); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 149 | #endif /* USE_VHANGUP */ |
Damien Miller | d0cff3e | 2000-04-20 23:12:58 +1000 | [diff] [blame] | 150 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | /* Verify that we now have a controlling tty. */ |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 152 | fd = open(_PATH_TTY, O_WRONLY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 153 | if (fd < 0) |
| 154 | error("open /dev/tty failed - could not set controlling tty: %.100s", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 155 | strerror(errno)); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 156 | else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | close(fd); |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 158 | #endif /* _UNICOS */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* Changes the window size associated with the pty. */ |
| 162 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 163 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 164 | pty_change_window_size(int ptyfd, int row, int col, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 165 | int xpixel, int ypixel) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | struct winsize w; |
Ben Lindstrom | 8ada5d0 | 2002-06-23 21:42:50 +0000 | [diff] [blame] | 168 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | w.ws_row = row; |
| 170 | w.ws_col = col; |
| 171 | w.ws_xpixel = xpixel; |
| 172 | w.ws_ypixel = ypixel; |
| 173 | (void) ioctl(ptyfd, TIOCSWINSZ, &w); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 174 | } |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 175 | |
| 176 | void |
| 177 | pty_setowner(struct passwd *pw, const char *ttyname) |
| 178 | { |
| 179 | struct group *grp; |
| 180 | gid_t gid; |
| 181 | mode_t mode; |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 182 | struct stat st; |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 183 | |
| 184 | /* Determine the group to make the owner of the tty. */ |
| 185 | grp = getgrnam("tty"); |
| 186 | if (grp) { |
| 187 | gid = grp->gr_gid; |
| 188 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
| 189 | } else { |
| 190 | gid = pw->pw_gid; |
| 191 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
| 192 | } |
| 193 | |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 194 | /* |
| 195 | * Change owner and mode of the tty as required. |
Ben Lindstrom | c88785e | 2001-08-06 20:47:23 +0000 | [diff] [blame] | 196 | * Warn but continue if filesystem is read-only and the uids match/ |
| 197 | * tty is owned by root. |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 198 | */ |
| 199 | if (stat(ttyname, &st)) |
| 200 | fatal("stat(%.100s) failed: %.100s", ttyname, |
| 201 | strerror(errno)); |
| 202 | |
| 203 | if (st.st_uid != pw->pw_uid || st.st_gid != gid) { |
| 204 | if (chown(ttyname, pw->pw_uid, gid) < 0) { |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 205 | if (errno == EROFS && |
Ben Lindstrom | 8ada5d0 | 2002-06-23 21:42:50 +0000 | [diff] [blame] | 206 | (st.st_uid == pw->pw_uid || st.st_uid == 0)) |
Damien Miller | e8cea9e | 2003-02-24 11:54:10 +1100 | [diff] [blame] | 207 | debug("chown(%.100s, %u, %u) failed: %.100s", |
Ben Lindstrom | c2df3ec | 2002-06-25 23:21:41 +0000 | [diff] [blame] | 208 | ttyname, (u_int)pw->pw_uid, (u_int)gid, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 209 | strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 210 | else |
Ben Lindstrom | 5c38552 | 2002-06-23 21:23:20 +0000 | [diff] [blame] | 211 | fatal("chown(%.100s, %u, %u) failed: %.100s", |
Ben Lindstrom | c2df3ec | 2002-06-25 23:21:41 +0000 | [diff] [blame] | 212 | ttyname, (u_int)pw->pw_uid, (u_int)gid, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 213 | strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) { |
| 218 | if (chmod(ttyname, mode) < 0) { |
| 219 | if (errno == EROFS && |
| 220 | (st.st_mode & (S_IRGRP | S_IROTH)) == 0) |
Damien Miller | e8cea9e | 2003-02-24 11:54:10 +1100 | [diff] [blame] | 221 | debug("chmod(%.100s, 0%o) failed: %.100s", |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 222 | ttyname, (u_int)mode, strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 223 | else |
| 224 | fatal("chmod(%.100s, 0%o) failed: %.100s", |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 225 | ttyname, (u_int)mode, strerror(errno)); |
Kevin Steves | fa72dda | 2000-12-15 18:39:12 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
Damien Miller | c7d8dbb | 2000-03-02 23:30:53 +1100 | [diff] [blame] | 228 | } |