xref: /netbsd-src/crypto/external/bsd/openssh/dist/sshpty.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: sshpty.c,v 1.7 2017/04/18 18:41:46 christos Exp $	*/
2 /* $OpenBSD: sshpty.c,v 1.31 2016/11/29 03:54:50 dtucker Exp $ */
3 
4 /*
5  * Author: Tatu Ylonen <ylo@cs.hut.fi>
6  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7  *                    All rights reserved
8  * Allocating a pseudo-terminal, and making it the controlling tty.
9  *
10  * As far as I am concerned, the code I have written for this software
11  * can be used freely for any purpose.  Any derived versions of this
12  * software must be clearly marked as such, and if the derived work is
13  * incompatible with the protocol description in the RFC file, it must be
14  * called by a name other than "ssh" or "Secure Shell".
15  */
16 
17 #include "includes.h"
18 __RCSID("$NetBSD: sshpty.c,v 1.7 2017/04/18 18:41:46 christos Exp $");
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <grp.h>
26 #include <paths.h>
27 #include <pwd.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <termios.h>
31 #include <unistd.h>
32 #include <util.h>
33 
34 #include "sshpty.h"
35 #include "log.h"
36 
37 #ifndef O_NOCTTY
38 #define O_NOCTTY 0
39 #endif
40 
41 /*
42  * Allocates and opens a pty.  Returns 0 if no pty could be allocated, or
43  * nonzero if a pty was successfully allocated.  On success, open file
44  * descriptors for the pty and tty sides and the name of the tty side are
45  * returned (the buffer must be able to hold at least 64 characters).
46  */
47 
48 int
49 pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
50 {
51 	char buf[64];
52 	int i;
53 
54 	i = openpty(ptyfd, ttyfd, buf, NULL, NULL);
55 	if (i < 0) {
56 		error("openpty: %.100s", strerror(errno));
57 		return 0;
58 	}
59 	strlcpy(namebuf, buf, namebuflen);	/* possible truncation */
60 	return 1;
61 }
62 
63 /* Releases the tty.  Its ownership is returned to root, and permissions to 0666. */
64 
65 void
66 pty_release(const char *tty)
67 {
68 	if (chown(tty, (uid_t) 0, (gid_t) 0) < 0)
69 		error("chown %.100s 0 0 failed: %.100s", tty, strerror(errno));
70 	if (chmod(tty, (mode_t) 0666) < 0)
71 		error("chmod %.100s 0666 failed: %.100s", tty, strerror(errno));
72 }
73 
74 /* Makes the tty the process's controlling tty and sets it to sane modes. */
75 
76 void
77 pty_make_controlling_tty(int *ttyfd, const char *tty)
78 {
79 	int fd;
80 
81 	/* First disconnect from the old controlling tty. */
82 #ifdef TIOCNOTTY
83 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
84 	if (fd >= 0) {
85 		(void) ioctl(fd, TIOCNOTTY, NULL);
86 		close(fd);
87 	}
88 #endif /* TIOCNOTTY */
89 	if (setsid() < 0)
90 		error("setsid: %.100s", strerror(errno));
91 
92 	/*
93 	 * Verify that we are successfully disconnected from the controlling
94 	 * tty.
95 	 */
96 	fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
97 	if (fd >= 0) {
98 		error("Failed to disconnect from controlling tty.");
99 		close(fd);
100 	}
101 	/* Make it our controlling tty. */
102 #ifdef TIOCSCTTY
103 	debug("Setting controlling tty using TIOCSCTTY.");
104 	if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
105 		error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
106 #endif /* TIOCSCTTY */
107 	fd = open(tty, O_RDWR);
108 	if (fd < 0)
109 		error("%.100s: %.100s", tty, strerror(errno));
110 	else
111 		close(fd);
112 
113 	/* Verify that we now have a controlling tty. */
114 	fd = open(_PATH_TTY, O_WRONLY);
115 	if (fd < 0)
116 		error("open /dev/tty failed - could not set controlling tty: %.100s",
117 		    strerror(errno));
118 	else
119 		close(fd);
120 }
121 
122 /* Changes the window size associated with the pty. */
123 
124 void
125 pty_change_window_size(int ptyfd, u_int row, u_int col,
126 	u_int xpixel, u_int ypixel)
127 {
128 	struct winsize w;
129 
130 	/* may truncate u_int -> u_short */
131 	w.ws_row = row;
132 	w.ws_col = col;
133 	w.ws_xpixel = xpixel;
134 	w.ws_ypixel = ypixel;
135 	(void) ioctl(ptyfd, TIOCSWINSZ, &w);
136 }
137 
138 void
139 pty_setowner(struct passwd *pw, const char *tty)
140 {
141 	struct group *grp;
142 	gid_t gid;
143 	mode_t mode;
144 	struct stat st;
145 
146 	/* Determine the group to make the owner of the tty. */
147 	grp = getgrnam("tty");
148 	gid = (grp != NULL) ? grp->gr_gid : pw->pw_gid;
149 	mode = (grp != NULL) ? 0620 : 0600;
150 
151 	/*
152 	 * Change owner and mode of the tty as required.
153 	 * Warn but continue if filesystem is read-only and the uids match/
154 	 * tty is owned by root.
155 	 */
156 	if (stat(tty, &st))
157 		fatal("stat(%.100s) failed: %.100s", tty,
158 		    strerror(errno));
159 
160 	if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
161 		if (chown(tty, pw->pw_uid, gid) < 0) {
162 			if (errno == EROFS &&
163 			    (st.st_uid == pw->pw_uid || st.st_uid == 0))
164 				debug("chown(%.100s, %u, %u) failed: %.100s",
165 				    tty, (u_int)pw->pw_uid, (u_int)gid,
166 				    strerror(errno));
167 			else
168 				fatal("chown(%.100s, %u, %u) failed: %.100s",
169 				    tty, (u_int)pw->pw_uid, (u_int)gid,
170 				    strerror(errno));
171 		}
172 	}
173 
174 	if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
175 		if (chmod(tty, mode) < 0) {
176 			if (errno == EROFS &&
177 			    (st.st_mode & (S_IRGRP | S_IROTH)) == 0)
178 				debug("chmod(%.100s, 0%o) failed: %.100s",
179 				    tty, (u_int)mode, strerror(errno));
180 			else
181 				fatal("chmod(%.100s, 0%o) failed: %.100s",
182 				    tty, (u_int)mode, strerror(errno));
183 		}
184 	}
185 }
186 
187 /* Disconnect from the controlling tty. */
188 void
189 disconnect_controlling_tty(void)
190 {
191 	int fd;
192 
193 	if ((fd = open(_PATH_TTY, O_RDWR | O_NOCTTY)) >= 0) {
194 		(void) ioctl(fd, TIOCNOTTY, NULL);
195 		close(fd);
196 	}
197 }
198