1 /* $OpenBSD: sshpty.c,v 1.23 2006/07/11 20:07:25 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/ioctl.h>
18 #include <sys/types.h>
32 #endif /* HAVE_UTIL_H */
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).
54 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
56 /* openpty(3) exists in OSF/1 and some other os'es */
60 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
62 error("openpty: %.100s", strerror(errno));
65 name = ttyname(*ttyfd);
67 fatal("openpty returns device for which ttyname fails.");
69 strlcpy(namebuf, name, namebuflen); /* possible truncation */
73 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
76 pty_release(const char *tty)
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));
84 /* Makes the tty the process's controlling tty and sets it to sane modes. */
87 pty_make_controlling_tty(int *ttyfd, const char *tty)
92 #endif /* USE_VHANGUP */
96 error("setsid: %.100s", strerror(errno));
98 fd = open(tty, O_RDWR|O_NOCTTY);
100 signal(SIGHUP, SIG_IGN);
101 ioctl(fd, TCVHUP, (char *)NULL);
102 signal(SIGHUP, SIG_DFL);
106 error("Failed to disconnect from controlling tty.");
109 debug("Setting controlling tty using TCSETCTTY.");
110 ioctl(*ttyfd, TCSETCTTY, NULL);
111 fd = open("/dev/tty", O_RDWR);
113 error("%.100s: %.100s", tty, strerror(errno));
118 /* First disconnect from the old controlling tty. */
120 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
122 (void) ioctl(fd, TIOCNOTTY, NULL);
125 #endif /* TIOCNOTTY */
127 error("setsid: %.100s", strerror(errno));
130 * Verify that we are successfully disconnected from the controlling
133 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
135 error("Failed to disconnect from controlling tty.");
138 /* Make it our controlling tty. */
140 debug("Setting controlling tty using TIOCSCTTY.");
141 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
142 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
143 #endif /* TIOCSCTTY */
145 if (setpgrp(0,0) < 0)
146 error("SETPGRP %s",strerror(errno));
147 #endif /* NEED_SETPGRP */
149 old = signal(SIGHUP, SIG_IGN);
152 #endif /* USE_VHANGUP */
153 fd = open(tty, O_RDWR);
155 error("%.100s: %.100s", tty, strerror(errno));
160 #else /* USE_VHANGUP */
162 #endif /* USE_VHANGUP */
164 /* Verify that we now have a controlling tty. */
165 fd = open(_PATH_TTY, O_WRONLY);
167 error("open /dev/tty failed - could not set controlling tty: %.100s",
174 /* Changes the window size associated with the pty. */
177 pty_change_window_size(int ptyfd, u_int row, u_int col,
178 u_int xpixel, u_int ypixel)
182 /* may truncate u_int -> u_short */
185 w.ws_xpixel = xpixel;
186 w.ws_ypixel = ypixel;
187 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
191 pty_setowner(struct passwd *pw, const char *tty)
198 /* Determine the group to make the owner of the tty. */
199 grp = getgrnam("tty");
202 mode = S_IRUSR | S_IWUSR | S_IWGRP;
205 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
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.
214 fatal("stat(%.100s) failed: %.100s", tty,
218 ssh_selinux_setup_pty(pw->pw_name, tty);
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,
229 fatal("chown(%.100s, %u, %u) failed: %.100s",
230 tty, (u_int)pw->pw_uid, (u_int)gid,
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));
242 fatal("chmod(%.100s, 0%o) failed: %.100s",
243 tty, (u_int)mode, strerror(errno));