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