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