1 /* $OpenBSD: sshpty.c,v 1.21 2006/07/06 16:03:53 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>
30 #endif /* HAVE_UTIL_H */
45 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
46 * nonzero if a pty was successfully allocated. On success, open file
47 * descriptors for the pty and tty sides and the name of the tty side are
48 * returned (the buffer must be able to hold at least 64 characters).
52 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
54 /* openpty(3) exists in OSF/1 and some other os'es */
58 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
60 error("openpty: %.100s", strerror(errno));
63 name = ttyname(*ttyfd);
65 fatal("openpty returns device for which ttyname fails.");
67 strlcpy(namebuf, name, namebuflen); /* possible truncation */
71 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
74 pty_release(const char *tty)
76 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
77 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
78 if (chmod(tty, (mode_t) 0666) < 0)
79 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
82 /* Makes the tty the process's controlling tty and sets it to sane modes. */
85 pty_make_controlling_tty(int *ttyfd, const char *tty)
90 #endif /* USE_VHANGUP */
94 error("setsid: %.100s", strerror(errno));
96 fd = open(tty, O_RDWR|O_NOCTTY);
98 signal(SIGHUP, SIG_IGN);
99 ioctl(fd, TCVHUP, (char *)NULL);
100 signal(SIGHUP, SIG_DFL);
104 error("Failed to disconnect from controlling tty.");
107 debug("Setting controlling tty using TCSETCTTY.");
108 ioctl(*ttyfd, TCSETCTTY, NULL);
109 fd = open("/dev/tty", O_RDWR);
111 error("%.100s: %.100s", tty, strerror(errno));
116 /* First disconnect from the old controlling tty. */
118 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
120 (void) ioctl(fd, TIOCNOTTY, NULL);
123 #endif /* TIOCNOTTY */
125 error("setsid: %.100s", strerror(errno));
128 * Verify that we are successfully disconnected from the controlling
131 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
133 error("Failed to disconnect from controlling tty.");
136 /* Make it our controlling tty. */
138 debug("Setting controlling tty using TIOCSCTTY.");
139 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
140 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
141 #endif /* TIOCSCTTY */
143 if (setpgrp(0,0) < 0)
144 error("SETPGRP %s",strerror(errno));
145 #endif /* NEED_SETPGRP */
147 old = signal(SIGHUP, SIG_IGN);
150 #endif /* USE_VHANGUP */
151 fd = open(tty, O_RDWR);
153 error("%.100s: %.100s", tty, strerror(errno));
158 #else /* USE_VHANGUP */
160 #endif /* USE_VHANGUP */
162 /* Verify that we now have a controlling tty. */
163 fd = open(_PATH_TTY, O_WRONLY);
165 error("open /dev/tty failed - could not set controlling tty: %.100s",
172 /* Changes the window size associated with the pty. */
175 pty_change_window_size(int ptyfd, u_int row, u_int col,
176 u_int xpixel, u_int ypixel)
180 /* may truncate u_int -> u_short */
183 w.ws_xpixel = xpixel;
184 w.ws_ypixel = ypixel;
185 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
189 pty_setowner(struct passwd *pw, const char *tty)
196 /* Determine the group to make the owner of the tty. */
197 grp = getgrnam("tty");
200 mode = S_IRUSR | S_IWUSR | S_IWGRP;
203 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
207 * Change owner and mode of the tty as required.
208 * Warn but continue if filesystem is read-only and the uids match/
209 * tty is owned by root.
212 fatal("stat(%.100s) failed: %.100s", tty,
216 ssh_selinux_setup_pty(pw->pw_name, tty);
219 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
220 if (chown(tty, pw->pw_uid, gid) < 0) {
221 if (errno == EROFS &&
222 (st.st_uid == pw->pw_uid || st.st_uid == 0))
223 debug("chown(%.100s, %u, %u) failed: %.100s",
224 tty, (u_int)pw->pw_uid, (u_int)gid,
227 fatal("chown(%.100s, %u, %u) failed: %.100s",
228 tty, (u_int)pw->pw_uid, (u_int)gid,
233 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
234 if (chmod(tty, mode) < 0) {
235 if (errno == EROFS &&
236 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
237 debug("chmod(%.100s, 0%o) failed: %.100s",
238 tty, (u_int)mode, strerror(errno));
240 fatal("chmod(%.100s, 0%o) failed: %.100s",
241 tty, (u_int)mode, strerror(errno));