]>
Commit | Line | Data |
---|---|---|
b1842393 | 1 | /* $OpenBSD: sshpty.c,v 1.21 2006/07/06 16:03:53 stevesk Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
5 | * All rights reserved | |
5260325f | 6 | * Allocating a pseudo-terminal, and making it the controlling tty. |
6ae2364d | 7 | * |
bcbf86ec | 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". | |
5260325f | 13 | */ |
8efc0c15 | 14 | |
15 | #include "includes.h" | |
bd7c69ea | 16 | |
17 | #include <sys/ioctl.h> | |
4095f623 | 18 | #include <sys/types.h> |
19 | #include <sys/stat.h> | |
3e9b2b1b | 20 | #include <signal.h> |
8efc0c15 | 21 | |
16fbe330 | 22 | #include <grp.h> |
a75f5360 | 23 | #ifdef HAVE_PATHS_H |
6f61e0ec | 24 | # include <paths.h> |
a75f5360 | 25 | #endif |
b1842393 | 26 | #include <pwd.h> |
a75f5360 | 27 | #include <termios.h> |
884bcb37 | 28 | #ifdef HAVE_UTIL_H |
29 | # include <util.h> | |
30 | #endif /* HAVE_UTIL_H */ | |
31 | ||
1729c161 | 32 | #include "sshpty.h" |
42f11eb2 | 33 | #include "log.h" |
5ad9f968 | 34 | #include "misc.h" |
5260325f | 35 | |
6d8c4ea4 | 36 | #ifdef HAVE_PTY_H |
37 | # include <pty.h> | |
38 | #endif | |
6d8c4ea4 | 39 | |
8efc0c15 | 40 | #ifndef O_NOCTTY |
41 | #define O_NOCTTY 0 | |
42 | #endif | |
43 | ||
aa3378df | 44 | /* |
45 | * Allocates and opens a pty. Returns 0 if no pty could be allocated, or | |
46 | * nonzero if a pty was successfully allocated. On success, open file | |
47 | * descriptors for the pty and tty sides and the name of the tty side are | |
48 | * returned (the buffer must be able to hold at least 64 characters). | |
49 | */ | |
8efc0c15 | 50 | |
6ae2364d | 51 | int |
148de80c | 52 | pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) |
8efc0c15 | 53 | { |
5260325f | 54 | /* openpty(3) exists in OSF/1 and some other os'es */ |
b82f1310 | 55 | char *name; |
5260325f | 56 | int i; |
8efc0c15 | 57 | |
b82f1310 | 58 | i = openpty(ptyfd, ttyfd, NULL, NULL, NULL); |
5260325f | 59 | if (i < 0) { |
60 | error("openpty: %.100s", strerror(errno)); | |
61 | return 0; | |
62 | } | |
b82f1310 | 63 | name = ttyname(*ttyfd); |
64 | if (!name) | |
65 | fatal("openpty returns device for which ttyname fails."); | |
66 | ||
67 | strlcpy(namebuf, name, namebuflen); /* possible truncation */ | |
5260325f | 68 | return 1; |
8efc0c15 | 69 | } |
70 | ||
5260325f | 71 | /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */ |
8efc0c15 | 72 | |
6ae2364d | 73 | void |
ca75d7de | 74 | pty_release(const char *tty) |
8efc0c15 | 75 | { |
ca75d7de | 76 | if (chown(tty, (uid_t) 0, (gid_t) 0) < 0) |
77 | error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno)); | |
78 | if (chmod(tty, (mode_t) 0666) < 0) | |
79 | error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno)); | |
8efc0c15 | 80 | } |
81 | ||
b8e04133 | 82 | /* Makes the tty the process's controlling tty and sets it to sane modes. */ |
8efc0c15 | 83 | |
6ae2364d | 84 | void |
ca75d7de | 85 | pty_make_controlling_tty(int *ttyfd, const char *tty) |
8efc0c15 | 86 | { |
1a23ac2c | 87 | int fd; |
8140f2aa | 88 | #ifdef USE_VHANGUP |
89 | void *old; | |
90 | #endif /* USE_VHANGUP */ | |
1a23ac2c | 91 | |
ef51930f | 92 | #ifdef _UNICOS |
68187931 | 93 | if (setsid() < 0) |
94 | error("setsid: %.100s", strerror(errno)); | |
1a23ac2c | 95 | |
ca75d7de | 96 | fd = open(tty, O_RDWR|O_NOCTTY); |
68187931 | 97 | if (fd != -1) { |
f00d1f78 | 98 | signal(SIGHUP, SIG_IGN); |
68187931 | 99 | ioctl(fd, TCVHUP, (char *)NULL); |
f00d1f78 | 100 | signal(SIGHUP, SIG_DFL); |
68187931 | 101 | setpgid(0, 0); |
102 | close(fd); | |
1a23ac2c | 103 | } else { |
68187931 | 104 | error("Failed to disconnect from controlling tty."); |
1a23ac2c | 105 | } |
1a23ac2c | 106 | |
68187931 | 107 | debug("Setting controlling tty using TCSETCTTY."); |
108 | ioctl(*ttyfd, TCSETCTTY, NULL); | |
109 | fd = open("/dev/tty", O_RDWR); | |
110 | if (fd < 0) | |
ca75d7de | 111 | error("%.100s: %.100s", tty, strerror(errno)); |
1a23ac2c | 112 | close(*ttyfd); |
875ec275 | 113 | *ttyfd = fd; |
ef51930f | 114 | #else /* _UNICOS */ |
8efc0c15 | 115 | |
5260325f | 116 | /* First disconnect from the old controlling tty. */ |
8efc0c15 | 117 | #ifdef TIOCNOTTY |
5ca51e19 | 118 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
5260325f | 119 | if (fd >= 0) { |
120 | (void) ioctl(fd, TIOCNOTTY, NULL); | |
121 | close(fd); | |
122 | } | |
8efc0c15 | 123 | #endif /* TIOCNOTTY */ |
5260325f | 124 | if (setsid() < 0) |
125 | error("setsid: %.100s", strerror(errno)); | |
126 | ||
aa3378df | 127 | /* |
128 | * Verify that we are successfully disconnected from the controlling | |
129 | * tty. | |
130 | */ | |
5ca51e19 | 131 | fd = open(_PATH_TTY, O_RDWR | O_NOCTTY); |
5260325f | 132 | if (fd >= 0) { |
133 | error("Failed to disconnect from controlling tty."); | |
134 | close(fd); | |
135 | } | |
136 | /* Make it our controlling tty. */ | |
8efc0c15 | 137 | #ifdef TIOCSCTTY |
5260325f | 138 | debug("Setting controlling tty using TIOCSCTTY."); |
a3bf38d0 | 139 | if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0) |
140 | error("ioctl(TIOCSCTTY): %.100s", strerror(errno)); | |
8efc0c15 | 141 | #endif /* TIOCSCTTY */ |
35fc74ed | 142 | #ifdef NEED_SETPGRP |
66d6c27e | 143 | if (setpgrp(0,0) < 0) |
144 | error("SETPGRP %s",strerror(errno)); | |
35fc74ed | 145 | #endif /* NEED_SETPGRP */ |
3c62e7eb | 146 | #ifdef USE_VHANGUP |
f00d1f78 | 147 | old = signal(SIGHUP, SIG_IGN); |
4e577b89 | 148 | vhangup(); |
f00d1f78 | 149 | signal(SIGHUP, old); |
3c62e7eb | 150 | #endif /* USE_VHANGUP */ |
ca75d7de | 151 | fd = open(tty, O_RDWR); |
4e577b89 | 152 | if (fd < 0) { |
ca75d7de | 153 | error("%.100s: %.100s", tty, strerror(errno)); |
4e577b89 | 154 | } else { |
3c62e7eb | 155 | #ifdef USE_VHANGUP |
4e577b89 | 156 | close(*ttyfd); |
157 | *ttyfd = fd; | |
3c62e7eb | 158 | #else /* USE_VHANGUP */ |
5260325f | 159 | close(fd); |
3c62e7eb | 160 | #endif /* USE_VHANGUP */ |
4e577b89 | 161 | } |
5260325f | 162 | /* Verify that we now have a controlling tty. */ |
5ca51e19 | 163 | fd = open(_PATH_TTY, O_WRONLY); |
5260325f | 164 | if (fd < 0) |
165 | error("open /dev/tty failed - could not set controlling tty: %.100s", | |
184eed6a | 166 | strerror(errno)); |
aff51935 | 167 | else |
5260325f | 168 | close(fd); |
ef51930f | 169 | #endif /* _UNICOS */ |
8efc0c15 | 170 | } |
171 | ||
172 | /* Changes the window size associated with the pty. */ | |
173 | ||
6ae2364d | 174 | void |
148de80c | 175 | pty_change_window_size(int ptyfd, u_int row, u_int col, |
176 | u_int xpixel, u_int ypixel) | |
8efc0c15 | 177 | { |
5260325f | 178 | struct winsize w; |
f09ce20a | 179 | |
148de80c | 180 | /* may truncate u_int -> u_short */ |
5260325f | 181 | w.ws_row = row; |
182 | w.ws_col = col; | |
183 | w.ws_xpixel = xpixel; | |
184 | w.ws_ypixel = ypixel; | |
185 | (void) ioctl(ptyfd, TIOCSWINSZ, &w); | |
8efc0c15 | 186 | } |
817175bc | 187 | |
188 | void | |
ca75d7de | 189 | pty_setowner(struct passwd *pw, const char *tty) |
817175bc | 190 | { |
191 | struct group *grp; | |
192 | gid_t gid; | |
193 | mode_t mode; | |
227e8e86 | 194 | struct stat st; |
817175bc | 195 | |
196 | /* Determine the group to make the owner of the tty. */ | |
197 | grp = getgrnam("tty"); | |
198 | if (grp) { | |
199 | gid = grp->gr_gid; | |
200 | mode = S_IRUSR | S_IWUSR | S_IWGRP; | |
201 | } else { | |
202 | gid = pw->pw_gid; | |
203 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; | |
204 | } | |
205 | ||
227e8e86 | 206 | /* |
207 | * Change owner and mode of the tty as required. | |
4f7893dc | 208 | * Warn but continue if filesystem is read-only and the uids match/ |
209 | * tty is owned by root. | |
227e8e86 | 210 | */ |
ca75d7de | 211 | if (stat(tty, &st)) |
212 | fatal("stat(%.100s) failed: %.100s", tty, | |
227e8e86 | 213 | strerror(errno)); |
214 | ||
ef4d1846 | 215 | #ifdef WITH_SELINUX |
216 | ssh_selinux_setup_pty(pw->pw_name, tty); | |
217 | #endif | |
218 | ||
227e8e86 | 219 | if (st.st_uid != pw->pw_uid || st.st_gid != gid) { |
ca75d7de | 220 | if (chown(tty, pw->pw_uid, gid) < 0) { |
184eed6a | 221 | if (errno == EROFS && |
f09ce20a | 222 | (st.st_uid == pw->pw_uid || st.st_uid == 0)) |
d6dc3576 | 223 | debug("chown(%.100s, %u, %u) failed: %.100s", |
ca75d7de | 224 | tty, (u_int)pw->pw_uid, (u_int)gid, |
184eed6a | 225 | strerror(errno)); |
227e8e86 | 226 | else |
d6133f43 | 227 | fatal("chown(%.100s, %u, %u) failed: %.100s", |
ca75d7de | 228 | tty, (u_int)pw->pw_uid, (u_int)gid, |
184eed6a | 229 | strerror(errno)); |
227e8e86 | 230 | } |
231 | } | |
232 | ||
233 | if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) { | |
ca75d7de | 234 | if (chmod(tty, mode) < 0) { |
227e8e86 | 235 | if (errno == EROFS && |
236 | (st.st_mode & (S_IRGRP | S_IROTH)) == 0) | |
d6dc3576 | 237 | debug("chmod(%.100s, 0%o) failed: %.100s", |
ca75d7de | 238 | tty, (u_int)mode, strerror(errno)); |
227e8e86 | 239 | else |
240 | fatal("chmod(%.100s, 0%o) failed: %.100s", | |
ca75d7de | 241 | tty, (u_int)mode, strerror(errno)); |
227e8e86 | 242 | } |
243 | } | |
817175bc | 244 | } |