1 /* $OpenBSD: sshpty.c,v 1.28 2007/09/11 23:49:09 stevesk Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Allocating a pseudo-terminal, and making it the controlling tty.
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".
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
50 # include <AvailabilityMacros.h>
51 # if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
52 # define __APPLE_PRIVPTY__
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).
64 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
66 /* openpty(3) exists in OSF/1 and some other os'es */
70 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
72 error("openpty: %.100s", strerror(errno));
76 /* Android does not have a working ttyname() */
79 name = ttyname(*ttyfd);
81 fatal("openpty returns device for which ttyname fails.");
84 strlcpy(namebuf, name, namebuflen); /* possible truncation */
88 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
91 pty_release(const char *tty)
93 #ifndef __APPLE_PRIVPTY__
94 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
95 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
96 if (chmod(tty, (mode_t) 0666) < 0)
97 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
98 #endif /* __APPLE_PRIVPTY__ */
101 /* Makes the tty the process's controlling tty and sets it to sane modes. */
104 pty_make_controlling_tty(int *ttyfd, const char *tty)
109 #endif /* USE_VHANGUP */
113 error("setsid: %.100s", strerror(errno));
115 fd = open(tty, O_RDWR|O_NOCTTY);
117 signal(SIGHUP, SIG_IGN);
118 ioctl(fd, TCVHUP, (char *)NULL);
119 signal(SIGHUP, SIG_DFL);
123 error("Failed to disconnect from controlling tty.");
126 debug("Setting controlling tty using TCSETCTTY.");
127 ioctl(*ttyfd, TCSETCTTY, NULL);
128 fd = open("/dev/tty", O_RDWR);
130 error("%.100s: %.100s", tty, strerror(errno));
135 /* First disconnect from the old controlling tty. */
137 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
139 (void) ioctl(fd, TIOCNOTTY, NULL);
142 #endif /* TIOCNOTTY */
144 error("setsid: %.100s", strerror(errno));
147 * Verify that we are successfully disconnected from the controlling
150 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
152 error("Failed to disconnect from controlling tty.");
155 /* Make it our controlling tty. */
157 debug("Setting controlling tty using TIOCSCTTY.");
158 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
159 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
160 #endif /* TIOCSCTTY */
162 if (setpgrp(0,0) < 0)
163 error("SETPGRP %s",strerror(errno));
164 #endif /* NEED_SETPGRP */
166 old = signal(SIGHUP, SIG_IGN);
169 #endif /* USE_VHANGUP */
170 fd = open(tty, O_RDWR);
172 error("%.100s: %.100s", tty, strerror(errno));
177 #else /* USE_VHANGUP */
179 #endif /* USE_VHANGUP */
181 /* Verify that we now have a controlling tty. */
182 fd = open(_PATH_TTY, O_WRONLY);
184 error("open /dev/tty failed - could not set controlling tty: %.100s",
191 /* Changes the window size associated with the pty. */
194 pty_change_window_size(int ptyfd, u_int row, u_int col,
195 u_int xpixel, u_int ypixel)
199 /* may truncate u_int -> u_short */
202 w.ws_xpixel = xpixel;
203 w.ws_ypixel = ypixel;
204 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
208 pty_setowner(struct passwd *pw, const char *tty)
215 /* Determine the group to make the owner of the tty. */
216 grp = getgrnam("tty");
219 mode = S_IRUSR | S_IWUSR | S_IWGRP;
222 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
226 * Change owner and mode of the tty as required.
227 * Warn but continue if filesystem is read-only and the uids match/
228 * tty is owned by root.
231 fatal("stat(%.100s) failed: %.100s", tty,
235 ssh_selinux_setup_pty(pw->pw_name, tty);
238 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
239 if (chown(tty, pw->pw_uid, gid) < 0) {
240 if (errno == EROFS &&
241 (st.st_uid == pw->pw_uid || st.st_uid == 0))
242 debug("chown(%.100s, %u, %u) failed: %.100s",
243 tty, (u_int)pw->pw_uid, (u_int)gid,
246 fatal("chown(%.100s, %u, %u) failed: %.100s",
247 tty, (u_int)pw->pw_uid, (u_int)gid,
252 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
253 if (chmod(tty, mode) < 0) {
254 if (errno == EROFS &&
255 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
256 debug("chmod(%.100s, 0%o) failed: %.100s",
257 tty, (u_int)mode, strerror(errno));
259 fatal("chmod(%.100s, 0%o) failed: %.100s",
260 tty, (u_int)mode, strerror(errno));