• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $OpenBSD: sshpty.c,v 1.31 2016/11/29 03:54:50 dtucker 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 <string.h>
31 #include <termios.h>
32 #ifdef HAVE_UTIL_H
33 # include <util.h>
34 #endif
35 #include <unistd.h>
36 
37 #include "sshpty.h"
38 #include "log.h"
39 #include "misc.h"
40 
41 #ifdef HAVE_PTY_H
42 # include <pty.h>
43 #endif
44 
45 #ifndef O_NOCTTY
46 #define O_NOCTTY 0
47 #endif
48 
49 #ifdef __APPLE__
50 # include <AvailabilityMacros.h>
51 # if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
52 #  define __APPLE_PRIVPTY__
53 # endif
54 #endif
55 
56 /*
57  * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
58  * nonzero if a pty was successfully allocated.  On success, open file
59  * descriptors for the pty and tty sides and the name of the tty side are
60  * returned (the buffer must be able to hold at least 64 characters).
61  */
62 
63 int
pty_allocate(int * ptyfd,int * ttyfd,char * namebuf,size_t namebuflen)64 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
65 {
66 	/* openpty(3) exists in OSF/1 and some other os'es */
67 	char *name;
68 	int i;
69 
70 	i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
71 	if (i < 0) {
72 		error("openpty: %.100s", strerror(errno));
73 		return 0;
74 	}
75 #ifdef ANDROID
76 	if (ptsname_r(*ptyfd, namebuf, namebuflen)) {
77 		fatal("openpty ptsname failed.");
78 		close(*ptyfd);
79 		*ptyfd = -1;
80 		return -1;
81 	}
82 	return 1;
83 #else
84 	name = ttyname(*ttyfd);
85 	if (!name)
86 		fatal("openpty returns device for which ttyname fails.");
87 
88 	strlcpy(namebuf, name, namebuflen);	/* possible truncation */
89 	return 1;
90 #endif
91 }
92 
93 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
94 
95 void
pty_release(const char * tty)96 pty_release(const char *tty)
97 {
98 #if !defined(__APPLE_PRIVPTY__) && !defined(HAVE_OPENPTY)
99 	if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
100 		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
101 	if (chmod(tty, (mode_t) 0666) < 0)
102 		error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
103 #endif /* !__APPLE_PRIVPTY__ && !HAVE_OPENPTY */
104 }
105 
106 /* Makes the tty the process's controlling tty and sets it to sane modes. */
107 
108 void
pty_make_controlling_tty(int * ttyfd,const char * tty)109 pty_make_controlling_tty(int *ttyfd, const char *tty)
110 {
111 	int fd;
112 
113 #ifdef _UNICOS
114 	if (setsid() < 0)
115 		error("setsid: %.100s", strerror(errno));
116 
117 	fd = open(tty, O_RDWR|O_NOCTTY);
118 	if (fd != -1) {
119 		signal(SIGHUP, SIG_IGN);
120 		ioctl(fd, TCVHUP, (char *)NULL);
121 		signal(SIGHUP, SIG_DFL);
122 		setpgid(0, 0);
123 		close(fd);
124 	} else {
125 		error("Failed to disconnect from controlling tty.");
126 	}
127 
128 	debug("Setting controlling tty using TCSETCTTY.");
129 	ioctl(*ttyfd, TCSETCTTY, NULL);
130 	fd = open("/dev/tty", O_RDWR);
131 	if (fd < 0)
132 		error("%.100s: %.100s", tty, strerror(errno));
133 	close(*ttyfd);
134 	*ttyfd = fd;
135 #else /* _UNICOS */
136 
137 	/* First disconnect from the old controlling tty. */
138 #ifdef TIOCNOTTY
139 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
140 	if (fd >= 0) {
141 		(void) ioctl(fd, TIOCNOTTY, NULL);
142 		close(fd);
143 	}
144 #endif /* TIOCNOTTY */
145 	if (setsid() < 0)
146 		error("setsid: %.100s", strerror(errno));
147 
148 	/*
149 	 * Verify that we are successfully disconnected from the controlling
150 	 * tty.
151 	 */
152 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
153 	if (fd >= 0) {
154 		error("Failed to disconnect from controlling tty.");
155 		close(fd);
156 	}
157 	/* Make it our controlling tty. */
158 #ifdef TIOCSCTTY
159 	debug("Setting controlling tty using TIOCSCTTY.");
160 	if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
161 		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
162 #endif /* TIOCSCTTY */
163 #ifdef NEED_SETPGRP
164 	if (setpgrp(0,0) < 0)
165 		error("SETPGRP %s",strerror(errno));
166 #endif /* NEED_SETPGRP */
167 	fd = open(tty, O_RDWR);
168 	if (fd < 0)
169 		error("%.100s: %.100s", tty, strerror(errno));
170 	else
171 		close(fd);
172 
173 	/* Verify that we now have a controlling tty. */
174 	fd = open(_PATH_TTY, O_WRONLY);
175 	if (fd < 0)
176 		error("open /dev/tty failed - could not set controlling tty: %.100s",
177 		    strerror(errno));
178 	else
179 		close(fd);
180 #endif /* _UNICOS */
181 }
182 
183 /* Changes the window size associated with the pty. */
184 
185 void
pty_change_window_size(int ptyfd,u_int row,u_int col,u_int xpixel,u_int ypixel)186 pty_change_window_size(int ptyfd, u_int row, u_int col,
187 	u_int xpixel, u_int ypixel)
188 {
189 	struct winsize w;
190 
191 	/* may truncate u_int -> u_short */
192 	w.ws_row = row;
193 	w.ws_col = col;
194 	w.ws_xpixel = xpixel;
195 	w.ws_ypixel = ypixel;
196 	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
197 }
198 
199 void
pty_setowner(struct passwd * pw,const char * tty)200 pty_setowner(struct passwd *pw, const char *tty)
201 {
202 	struct group *grp;
203 	gid_t gid;
204 	mode_t mode;
205 	struct stat st;
206 
207 	/* Determine the group to make the owner of the tty. */
208 	grp = getgrnam("tty");
209 	gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
210 	mode = (grp != NULL) ? 0620 : 0600;
211 
212 	/*
213 	 * Change owner and mode of the tty as required.
214 	 * Warn but continue if filesystem is read-only and the uids match/
215 	 * tty is owned by root.
216 	 */
217 	if (stat(tty, &st))
218 		fatal("stat(%.100s) failed: %.100s", tty,
219 		    strerror(errno));
220 
221 #ifdef WITH_SELINUX
222 	ssh_selinux_setup_pty(pw->pw_name, tty);
223 #endif
224 
225 	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
226 		if (chown(tty, pw->pw_uid, gid) < 0) {
227 			if (errno == EROFS &&
228 			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
229 				debug("chown(%.100s, %u, %u) failed: %.100s",
230 				    tty, (u_int)pw->pw_uid, (u_int)gid,
231 				    strerror(errno));
232 			else
233 				fatal("chown(%.100s, %u, %u) failed: %.100s",
234 				    tty, (u_int)pw->pw_uid, (u_int)gid,
235 				    strerror(errno));
236 		}
237 	}
238 
239 	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
240 		if (chmod(tty, mode) < 0) {
241 			if (errno == EROFS &&
242 			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
243 				debug("chmod(%.100s, 0%o) failed: %.100s",
244 				    tty, (u_int)mode, strerror(errno));
245 			else
246 				fatal("chmod(%.100s, 0%o) failed: %.100s",
247 				    tty, (u_int)mode, strerror(errno));
248 		}
249 	}
250 }
251 
252 /* Disconnect from the controlling tty. */
253 void
disconnect_controlling_tty(void)254 disconnect_controlling_tty(void)
255 {
256 #ifdef TIOCNOTTY
257 	int fd;
258 
259 	if ((fd = open(_PATH_TTY, O_RDWR | O_NOCTTY)) >= 0) {
260 		(void) ioctl(fd, TIOCNOTTY, NULL);
261 		close(fd);
262 	}
263 #endif /* TIOCNOTTY */
264 }
265