1 /* $OpenBSD: sshpty.c,v 1.19 2006/03/25 13:17:03 djm 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>
28 #endif /* HAVE_UTIL_H */
43 * Allocates and opens a pty. Returns 0 if no pty could be allocated, or
44 * nonzero if a pty was successfully allocated. On success, open file
45 * descriptors for the pty and tty sides and the name of the tty side are
46 * returned (the buffer must be able to hold at least 64 characters).
50 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
52 /* openpty(3) exists in OSF/1 and some other os'es */
56 i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
58 error("openpty: %.100s", strerror(errno));
61 name = ttyname(*ttyfd);
63 fatal("openpty returns device for which ttyname fails.");
65 strlcpy(namebuf, name, namebuflen); /* possible truncation */
69 /* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
72 pty_release(const char *tty)
74 if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
75 error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
76 if (chmod(tty, (mode_t) 0666) < 0)
77 error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
80 /* Makes the tty the process's controlling tty and sets it to sane modes. */
83 pty_make_controlling_tty(int *ttyfd, const char *tty)
88 #endif /* USE_VHANGUP */
92 error("setsid: %.100s", strerror(errno));
94 fd = open(tty, O_RDWR|O_NOCTTY);
96 signal(SIGHUP, SIG_IGN);
97 ioctl(fd, TCVHUP, (char *)NULL);
98 signal(SIGHUP, SIG_DFL);
102 error("Failed to disconnect from controlling tty.");
105 debug("Setting controlling tty using TCSETCTTY.");
106 ioctl(*ttyfd, TCSETCTTY, NULL);
107 fd = open("/dev/tty", O_RDWR);
109 error("%.100s: %.100s", tty, strerror(errno));
114 /* First disconnect from the old controlling tty. */
116 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
118 (void) ioctl(fd, TIOCNOTTY, NULL);
121 #endif /* TIOCNOTTY */
123 error("setsid: %.100s", strerror(errno));
126 * Verify that we are successfully disconnected from the controlling
129 fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
131 error("Failed to disconnect from controlling tty.");
134 /* Make it our controlling tty. */
136 debug("Setting controlling tty using TIOCSCTTY.");
137 if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
138 error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
139 #endif /* TIOCSCTTY */
141 if (setpgrp(0,0) < 0)
142 error("SETPGRP %s",strerror(errno));
143 #endif /* NEED_SETPGRP */
145 old = signal(SIGHUP, SIG_IGN);
148 #endif /* USE_VHANGUP */
149 fd = open(tty, O_RDWR);
151 error("%.100s: %.100s", tty, strerror(errno));
156 #else /* USE_VHANGUP */
158 #endif /* USE_VHANGUP */
160 /* Verify that we now have a controlling tty. */
161 fd = open(_PATH_TTY, O_WRONLY);
163 error("open /dev/tty failed - could not set controlling tty: %.100s",
170 /* Changes the window size associated with the pty. */
173 pty_change_window_size(int ptyfd, u_int row, u_int col,
174 u_int xpixel, u_int ypixel)
178 /* may truncate u_int -> u_short */
181 w.ws_xpixel = xpixel;
182 w.ws_ypixel = ypixel;
183 (void) ioctl(ptyfd, TIOCSWINSZ, &w);
187 pty_setowner(struct passwd *pw, const char *tty)
194 /* Determine the group to make the owner of the tty. */
195 grp = getgrnam("tty");
198 mode = S_IRUSR | S_IWUSR | S_IWGRP;
201 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
205 * Change owner and mode of the tty as required.
206 * Warn but continue if filesystem is read-only and the uids match/
207 * tty is owned by root.
210 fatal("stat(%.100s) failed: %.100s", tty,
214 ssh_selinux_setup_pty(pw->pw_name, tty);
217 if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
218 if (chown(tty, pw->pw_uid, gid) < 0) {
219 if (errno == EROFS &&
220 (st.st_uid == pw->pw_uid || st.st_uid == 0))
221 debug("chown(%.100s, %u, %u) failed: %.100s",
222 tty, (u_int)pw->pw_uid, (u_int)gid,
225 fatal("chown(%.100s, %u, %u) failed: %.100s",
226 tty, (u_int)pw->pw_uid, (u_int)gid,
231 if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
232 if (chmod(tty, mode) < 0) {
233 if (errno == EROFS &&
234 (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
235 debug("chmod(%.100s, 0%o) failed: %.100s",
236 tty, (u_int)mode, strerror(errno));
238 fatal("chmod(%.100s, 0%o) failed: %.100s",
239 tty, (u_int)mode, strerror(errno));