xref: /freebsd/crypto/openssh/sshpty.c (revision d7d962ead0b6e5e8a39202d0590022082bf5bfb6)
1 /* $OpenBSD: sshpty.c,v 1.34 2019/07/04 16:20:10 deraadt Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  * Allocating a pseudo-terminal, and making it the controlling tty.
7  *
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".
13  */
14 
15 #include "includes.h"
16 
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/stat.h>
20 #include <signal.h>
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <grp.h>
25 #ifdef HAVE_PATHS_H
26 # include <paths.h>
27 #endif
28 #include <pwd.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <termios.h>
33 #ifdef HAVE_UTIL_H
34 # include <util.h>
35 #endif
36 #include <unistd.h>
37 
38 #include "sshpty.h"
39 #include "log.h"
40 #include "misc.h"
41 
42 #ifdef HAVE_PTY_H
43 # include <pty.h>
44 #endif
45 
46 #ifndef O_NOCTTY
47 #define O_NOCTTY 0
48 #endif
49 
50 #ifdef __APPLE__
51 # include <AvailabilityMacros.h>
52 # if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
53 #  define __APPLE_PRIVPTY__
54 # endif
55 #endif
56 
57 /*
58  * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
59  * nonzero if a pty was successfully allocated.  On success, open file
60  * descriptors for the pty and tty sides and the name of the tty side are
61  * returned (the buffer must be able to hold at least 64 characters).
62  */
63 
64 int
65 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
66 {
67 	/* openpty(3) exists in OSF/1 and some other os'es */
68 	char *name;
69 	int i;
70 
71 	i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
72 	if (i == -1) {
73 		error("openpty: %.100s", strerror(errno));
74 		return 0;
75 	}
76 	name = ttyname(*ttyfd);
77 	if (!name)
78 		fatal("openpty returns device for which ttyname fails.");
79 
80 	strlcpy(namebuf, name, namebuflen);	/* possible truncation */
81 	return 1;
82 }
83 
84 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
85 
86 void
87 pty_release(const char *tty)
88 {
89 #if !defined(__APPLE_PRIVPTY__) && !defined(HAVE_OPENPTY)
90 	if (chown(tty, (uid_t) 0, (gid_t) 0) == -1)
91 		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
92 	if (chmod(tty, (mode_t) 0666) == -1)
93 		error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
94 #endif /* !__APPLE_PRIVPTY__ && !HAVE_OPENPTY */
95 }
96 
97 /* Makes the tty the process's controlling tty and sets it to sane modes. */
98 
99 void
100 pty_make_controlling_tty(int *ttyfd, const char *tty)
101 {
102 	int fd;
103 
104 	/* First disconnect from the old controlling tty. */
105 #ifdef TIOCNOTTY
106 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
107 	if (fd >= 0) {
108 		(void) ioctl(fd, TIOCNOTTY, NULL);
109 		close(fd);
110 	}
111 #endif /* TIOCNOTTY */
112 	if (setsid() == -1)
113 		error("setsid: %.100s", strerror(errno));
114 
115 	/*
116 	 * Verify that we are successfully disconnected from the controlling
117 	 * tty.
118 	 */
119 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
120 	if (fd >= 0) {
121 		error("Failed to disconnect from controlling tty.");
122 		close(fd);
123 	}
124 	/* Make it our controlling tty. */
125 #ifdef TIOCSCTTY
126 	debug("Setting controlling tty using TIOCSCTTY.");
127 	if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
128 		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
129 #endif /* TIOCSCTTY */
130 #ifdef NEED_SETPGRP
131 	if (setpgrp(0,0) < 0)
132 		error("SETPGRP %s",strerror(errno));
133 #endif /* NEED_SETPGRP */
134 	fd = open(tty, O_RDWR);
135 	if (fd == -1)
136 		error("%.100s: %.100s", tty, strerror(errno));
137 	else
138 		close(fd);
139 
140 	/* Verify that we now have a controlling tty. */
141 	fd = open(_PATH_TTY, O_WRONLY);
142 	if (fd == -1)
143 		error("open /dev/tty failed - could not set controlling tty: %.100s",
144 		    strerror(errno));
145 	else
146 		close(fd);
147 }
148 
149 /* Changes the window size associated with the pty. */
150 
151 void
152 pty_change_window_size(int ptyfd, u_int row, u_int col,
153 	u_int xpixel, u_int ypixel)
154 {
155 	struct winsize w;
156 
157 	/* may truncate u_int -> u_short */
158 	w.ws_row = row;
159 	w.ws_col = col;
160 	w.ws_xpixel = xpixel;
161 	w.ws_ypixel = ypixel;
162 	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
163 }
164 
165 void
166 pty_setowner(struct passwd *pw, const char *tty)
167 {
168 	struct group *grp;
169 	gid_t gid;
170 	mode_t mode;
171 	struct stat st;
172 
173 	/* Determine the group to make the owner of the tty. */
174 	grp = getgrnam("tty");
175 	if (grp == NULL)
176 		debug("%s: no tty group", __func__);
177 	gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
178 	mode = (grp != NULL) ? 0620 : 0600;
179 
180 	/*
181 	 * Change owner and mode of the tty as required.
182 	 * Warn but continue if filesystem is read-only and the uids match/
183 	 * tty is owned by root.
184 	 */
185 	if (stat(tty, &st) == -1)
186 		fatal("stat(%.100s) failed: %.100s", tty,
187 		    strerror(errno));
188 
189 #ifdef WITH_SELINUX
190 	ssh_selinux_setup_pty(pw->pw_name, tty);
191 #endif
192 
193 	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
194 		if (chown(tty, pw->pw_uid, gid) == -1) {
195 			if (errno == EROFS &&
196 			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
197 				debug("chown(%.100s, %u, %u) failed: %.100s",
198 				    tty, (u_int)pw->pw_uid, (u_int)gid,
199 				    strerror(errno));
200 			else
201 				fatal("chown(%.100s, %u, %u) failed: %.100s",
202 				    tty, (u_int)pw->pw_uid, (u_int)gid,
203 				    strerror(errno));
204 		}
205 	}
206 
207 	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
208 		if (chmod(tty, mode) == -1) {
209 			if (errno == EROFS &&
210 			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
211 				debug("chmod(%.100s, 0%o) failed: %.100s",
212 				    tty, (u_int)mode, strerror(errno));
213 			else
214 				fatal("chmod(%.100s, 0%o) failed: %.100s",
215 				    tty, (u_int)mode, strerror(errno));
216 		}
217 	}
218 }
219 
220 /* Disconnect from the controlling tty. */
221 void
222 disconnect_controlling_tty(void)
223 {
224 #ifdef TIOCNOTTY
225 	int fd;
226 
227 	if ((fd = open(_PATH_TTY, O_RDWR | O_NOCTTY)) >= 0) {
228 		(void) ioctl(fd, TIOCNOTTY, NULL);
229 		close(fd);
230 	}
231 #endif /* TIOCNOTTY */
232 }
233