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