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