1 /* $OpenBSD: sshpty.c,v 1.25 2006/07/22 20:48:23 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>
33 #endif /* HAVE_UTIL_H */
49 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
50 * nonzero if a pty was successfully allocated. On success, open file
51 * descriptors for the pty and tty sides and the name of the tty side are
52 * returned (the buffer must be able to hold at least 64 characters).
56 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
58 /* openpty(3) exists in OSF/1 and some other os'es */
62 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
64 error("openpty: %.100s", strerror(errno));
67 name = ttyname(*ttyfd);
69 fatal("openpty returns device for which ttyname fails.");
71 strlcpy(namebuf, name, namebuflen); /* possible truncation */
75 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
78 pty_release(const char *tty)
80 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
81 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
82 if (chmod(tty, (mode_t) 0666) < 0)
83 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
86 /* Makes the tty the process's controlling tty and sets it to sane modes. */
89 pty_make_controlling_tty(int *ttyfd, const char *tty)
94 #endif /* USE_VHANGUP */
98 error("setsid: %.100s", strerror(errno));
100 fd = open(tty, O_RDWR|O_NOCTTY);
102 signal(SIGHUP, SIG_IGN);
103 ioctl(fd, TCVHUP, (char *)NULL);
104 signal(SIGHUP, SIG_DFL);
108 error("Failed to disconnect from controlling tty.");
111 debug("Setting controlling tty using TCSETCTTY.");
112 ioctl(*ttyfd, TCSETCTTY, NULL);
113 fd = open("/dev/tty", O_RDWR);
115 error("%.100s: %.100s", tty, strerror(errno));
120 /* First disconnect from the old controlling tty. */
122 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
124 (void) ioctl(fd, TIOCNOTTY, NULL);
127 #endif /* TIOCNOTTY */
129 error("setsid: %.100s", strerror(errno));
132 * Verify that we are successfully disconnected from the controlling
135 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
137 error("Failed to disconnect from controlling tty.");
140 /* Make it our controlling tty. */
142 debug("Setting controlling tty using TIOCSCTTY.");
143 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
144 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
145 #endif /* TIOCSCTTY */
147 if (setpgrp(0,0) < 0)
148 error("SETPGRP %s",strerror(errno));
149 #endif /* NEED_SETPGRP */
151 old = signal(SIGHUP, SIG_IGN);
154 #endif /* USE_VHANGUP */
155 fd = open(tty, O_RDWR);
157 error("%.100s: %.100s", tty, strerror(errno));
162 #else /* USE_VHANGUP */
164 #endif /* USE_VHANGUP */
166 /* Verify that we now have a controlling tty. */
167 fd = open(_PATH_TTY, O_WRONLY);
169 error("open /dev/tty failed - could not set controlling tty: %.100s",
176 /* Changes the window size associated with the pty. */
179 pty_change_window_size(int ptyfd, u_int row, u_int col,
180 u_int xpixel, u_int ypixel)
184 /* may truncate u_int -> u_short */
187 w.ws_xpixel = xpixel;
188 w.ws_ypixel = ypixel;
189 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
193 pty_setowner(struct passwd *pw, const char *tty)
200 /* Determine the group to make the owner of the tty. */
201 grp = getgrnam("tty");
204 mode = S_IRUSR | S_IWUSR | S_IWGRP;
207 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
211 * Change owner and mode of the tty as required.
212 * Warn but continue if filesystem is read-only and the uids match/
213 * tty is owned by root.
216 fatal("stat(%.100s) failed: %.100s", tty,
220 ssh_selinux_setup_pty(pw->pw_name, tty);
223 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
224 if (chown(tty, pw->pw_uid, gid) < 0) {
225 if (errno == EROFS &&
226 (st.st_uid == pw->pw_uid || st.st_uid == 0))
227 debug("chown(%.100s, %u, %u) failed: %.100s",
228 tty, (u_int)pw->pw_uid, (u_int)gid,
231 fatal("chown(%.100s, %u, %u) failed: %.100s",
232 tty, (u_int)pw->pw_uid, (u_int)gid,
237 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
238 if (chmod(tty, mode) < 0) {
239 if (errno == EROFS &&
240 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
241 debug("chmod(%.100s, 0%o) failed: %.100s",
242 tty, (u_int)mode, strerror(errno));
244 fatal("chmod(%.100s, 0%o) failed: %.100s",
245 tty, (u_int)mode, strerror(errno));