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