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.13 2006/02/07 01:42:00 stevesk Exp $");
21 #endif /* HAVE_UTIL_H */
36 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
37 * nonzero if a pty was successfully allocated. On success, open file
38 * descriptors for the pty and tty sides and the name of the tty side are
39 * returned (the buffer must be able to hold at least 64 characters).
43 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
45 /* openpty(3) exists in OSF/1 and some other os'es */
49 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
51 error("openpty: %.100s", strerror(errno));
54 name = ttyname(*ttyfd);
56 fatal("openpty returns device for which ttyname fails.");
58 strlcpy(namebuf, name, namebuflen); /* possible truncation */
62 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
65 pty_release(const char *tty)
67 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
68 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
69 if (chmod(tty, (mode_t) 0666) < 0)
70 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
73 /* Makes the tty the process's controlling tty and sets it to sane modes. */
76 pty_make_controlling_tty(int *ttyfd, const char *tty)
81 #endif /* USE_VHANGUP */
85 error("setsid: %.100s", strerror(errno));
87 fd = open(tty, O_RDWR|O_NOCTTY);
89 signal(SIGHUP, SIG_IGN);
90 ioctl(fd, TCVHUP, (char *)NULL);
91 signal(SIGHUP, SIG_DFL);
95 error("Failed to disconnect from controlling tty.");
98 debug("Setting controlling tty using TCSETCTTY.");
99 ioctl(*ttyfd, TCSETCTTY, NULL);
100 fd = open("/dev/tty", O_RDWR);
102 error("%.100s: %.100s", tty, strerror(errno));
107 /* First disconnect from the old controlling tty. */
109 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
111 (void) ioctl(fd, TIOCNOTTY, NULL);
114 #endif /* TIOCNOTTY */
116 error("setsid: %.100s", strerror(errno));
119 * Verify that we are successfully disconnected from the controlling
122 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
124 error("Failed to disconnect from controlling tty.");
127 /* Make it our controlling tty. */
129 debug("Setting controlling tty using TIOCSCTTY.");
130 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
131 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
132 #endif /* TIOCSCTTY */
134 if (setpgrp(0,0) < 0)
135 error("SETPGRP %s",strerror(errno));
136 #endif /* NEED_SETPGRP */
138 old = signal(SIGHUP, SIG_IGN);
141 #endif /* USE_VHANGUP */
142 fd = open(tty, O_RDWR);
144 error("%.100s: %.100s", tty, strerror(errno));
149 #else /* USE_VHANGUP */
151 #endif /* USE_VHANGUP */
153 /* Verify that we now have a controlling tty. */
154 fd = open(_PATH_TTY, O_WRONLY);
156 error("open /dev/tty failed - could not set controlling tty: %.100s",
163 /* Changes the window size associated with the pty. */
166 pty_change_window_size(int ptyfd, int row, int col,
167 int xpixel, int ypixel)
173 w.ws_xpixel = xpixel;
174 w.ws_ypixel = ypixel;
175 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
179 pty_setowner(struct passwd *pw, const char *tty)
186 /* Determine the group to make the owner of the tty. */
187 grp = getgrnam("tty");
190 mode = S_IRUSR | S_IWUSR | S_IWGRP;
193 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
197 * Change owner and mode of the tty as required.
198 * Warn but continue if filesystem is read-only and the uids match/
199 * tty is owned by root.
202 fatal("stat(%.100s) failed: %.100s", tty,
205 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
206 if (chown(tty, pw->pw_uid, gid) < 0) {
207 if (errno == EROFS &&
208 (st.st_uid == pw->pw_uid || st.st_uid == 0))
209 debug("chown(%.100s, %u, %u) failed: %.100s",
210 tty, (u_int)pw->pw_uid, (u_int)gid,
213 fatal("chown(%.100s, %u, %u) failed: %.100s",
214 tty, (u_int)pw->pw_uid, (u_int)gid,
219 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
220 if (chmod(tty, mode) < 0) {
221 if (errno == EROFS &&
222 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
223 debug("chmod(%.100s, 0%o) failed: %.100s",
224 tty, (u_int)mode, strerror(errno));
226 fatal("chmod(%.100s, 0%o) failed: %.100s",
227 tty, (u_int)mode, strerror(errno));