2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Allocating a pseudo-terminal, and making it the controlling tty.
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
15 RCSID("$OpenBSD: sshpty.c,v 1.14 2006/02/08 12:15:27 stevesk Exp $");
23 #endif /* HAVE_UTIL_H */
38 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
39 * nonzero if a pty was successfully allocated. On success, open file
40 * descriptors for the pty and tty sides and the name of the tty side are
41 * returned (the buffer must be able to hold at least 64 characters).
45 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
47 /* openpty(3) exists in OSF/1 and some other os'es */
51 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
53 error("openpty: %.100s", strerror(errno));
56 name = ttyname(*ttyfd);
58 fatal("openpty returns device for which ttyname fails.");
60 strlcpy(namebuf, name, namebuflen); /* possible truncation */
64 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
67 pty_release(const char *tty)
69 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
70 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
71 if (chmod(tty, (mode_t) 0666) < 0)
72 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
75 /* Makes the tty the process's controlling tty and sets it to sane modes. */
78 pty_make_controlling_tty(int *ttyfd, const char *tty)
83 #endif /* USE_VHANGUP */
87 error("setsid: %.100s", strerror(errno));
89 fd = open(tty, O_RDWR|O_NOCTTY);
91 signal(SIGHUP, SIG_IGN);
92 ioctl(fd, TCVHUP, (char *)NULL);
93 signal(SIGHUP, SIG_DFL);
97 error("Failed to disconnect from controlling tty.");
100 debug("Setting controlling tty using TCSETCTTY.");
101 ioctl(*ttyfd, TCSETCTTY, NULL);
102 fd = open("/dev/tty", O_RDWR);
104 error("%.100s: %.100s", tty, strerror(errno));
109 /* First disconnect from the old controlling tty. */
111 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
113 (void) ioctl(fd, TIOCNOTTY, NULL);
116 #endif /* TIOCNOTTY */
118 error("setsid: %.100s", strerror(errno));
121 * Verify that we are successfully disconnected from the controlling
124 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
126 error("Failed to disconnect from controlling tty.");
129 /* Make it our controlling tty. */
131 debug("Setting controlling tty using TIOCSCTTY.");
132 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
133 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
134 #endif /* TIOCSCTTY */
136 if (setpgrp(0,0) < 0)
137 error("SETPGRP %s",strerror(errno));
138 #endif /* NEED_SETPGRP */
140 old = signal(SIGHUP, SIG_IGN);
143 #endif /* USE_VHANGUP */
144 fd = open(tty, O_RDWR);
146 error("%.100s: %.100s", tty, strerror(errno));
151 #else /* USE_VHANGUP */
153 #endif /* USE_VHANGUP */
155 /* Verify that we now have a controlling tty. */
156 fd = open(_PATH_TTY, O_WRONLY);
158 error("open /dev/tty failed - could not set controlling tty: %.100s",
165 /* Changes the window size associated with the pty. */
168 pty_change_window_size(int ptyfd, int row, int col,
169 int xpixel, int ypixel)
175 w.ws_xpixel = xpixel;
176 w.ws_ypixel = ypixel;
177 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
181 pty_setowner(struct passwd *pw, const char *tty)
188 /* Determine the group to make the owner of the tty. */
189 grp = getgrnam("tty");
192 mode = S_IRUSR | S_IWUSR | S_IWGRP;
195 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
199 * Change owner and mode of the tty as required.
200 * Warn but continue if filesystem is read-only and the uids match/
201 * tty is owned by root.
204 fatal("stat(%.100s) failed: %.100s", tty,
207 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
208 if (chown(tty, pw->pw_uid, gid) < 0) {
209 if (errno == EROFS &&
210 (st.st_uid == pw->pw_uid || st.st_uid == 0))
211 debug("chown(%.100s, %u, %u) failed: %.100s",
212 tty, (u_int)pw->pw_uid, (u_int)gid,
215 fatal("chown(%.100s, %u, %u) failed: %.100s",
216 tty, (u_int)pw->pw_uid, (u_int)gid,
221 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
222 if (chmod(tty, mode) < 0) {
223 if (errno == EROFS &&
224 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
225 debug("chmod(%.100s, 0%o) failed: %.100s",
226 tty, (u_int)mode, strerror(errno));
228 fatal("chmod(%.100s, 0%o) failed: %.100s",
229 tty, (u_int)mode, strerror(errno));