xref: /freebsd/crypto/openssh/openbsd-compat/bsd-openpty.c (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
1 /*
2  * Please note: this implementation of openpty() is far from complete.
3  * it is just enough for portable OpenSSH's needs.
4  */
5 
6 /*
7  * Copyright (c) 2004 Damien Miller <djm@mindrot.org>
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 /*
23  * Author: Tatu Ylonen <ylo@cs.hut.fi>
24  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
25  *                    All rights reserved
26  * Allocating a pseudo-terminal, and making it the controlling tty.
27  *
28  * As far as I am concerned, the code I have written for this software
29  * can be used freely for any purpose.  Any derived versions of this
30  * software must be clearly marked as such, and if the derived work is
31  * incompatible with the protocol description in the RFC file, it must be
32  * called by a name other than "ssh" or "Secure Shell".
33  */
34 
35 #include "includes.h"
36 #if !defined(HAVE_OPENPTY)
37 
38 #include <sys/types.h>
39 
40 #include <stdlib.h>
41 
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 # include <sys/ioctl.h>
47 #endif
48 
49 #ifdef HAVE_FCNTL_H
50 # include <fcntl.h>
51 #endif
52 
53 #ifdef HAVE_UTIL_H
54 # include <util.h>
55 #endif /* HAVE_UTIL_H */
56 
57 #ifdef HAVE_PTY_H
58 # include <pty.h>
59 #endif
60 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
61 # include <sys/stropts.h>
62 #endif
63 
64 #include <signal.h>
65 #include <string.h>
66 #include <unistd.h>
67 
68 #include "misc.h"
69 #include "log.h"
70 
71 #ifndef O_NOCTTY
72 #define O_NOCTTY 0
73 #endif
74 
75 #if defined(HAVE_DEV_PTMX) && !defined(HAVE__GETPTY)
76 static int
77 openpty_streams(int *amaster, int *aslave)
78 {
79 	/*
80 	 * This code is used e.g. on Solaris 2.x.  (Note that Solaris 2.3
81 	 * also has bsd-style ptys, but they simply do not work.)
82 	 */
83 	int ptm;
84 	char *pts;
85 	sshsig_t old_signal;
86 
87 	if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
88 		return (-1);
89 
90 	/* XXX: need to close ptm on error? */
91 	old_signal = ssh_signal(SIGCHLD, SIG_DFL);
92 	if (grantpt(ptm) < 0)
93 		return (-1);
94 	ssh_signal(SIGCHLD, old_signal);
95 
96 	if (unlockpt(ptm) < 0)
97 		return (-1);
98 
99 	if ((pts = ptsname(ptm)) == NULL)
100 		return (-1);
101 	*amaster = ptm;
102 
103 	/* Open the slave side. */
104 	if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) {
105 		close(*amaster);
106 		return (-1);
107 	}
108 
109 # if defined(I_FIND) && defined(__SVR4)
110 	/*
111 	 * If the streams modules have already been pushed then there
112 	 * is no more work to do here.
113 	 */
114 	if (ioctl(*aslave, I_FIND, "ptem") != 0)
115 		return 0;
116 # endif
117 
118 	/*
119 	 * Try to push the appropriate streams modules, as described
120 	 * in Solaris pts(7).
121 	 */
122 	ioctl(*aslave, I_PUSH, "ptem");
123 	ioctl(*aslave, I_PUSH, "ldterm");
124 # ifndef __hpux
125 	ioctl(*aslave, I_PUSH, "ttcompat");
126 # endif /* __hpux */
127 	return (0);
128 }
129 #endif
130 
131 int
132 openpty(int *amaster, int *aslave, char *name, struct termios *termp,
133    struct winsize *winp)
134 {
135 #if defined(HAVE__GETPTY)
136 	/*
137 	 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
138 	 * pty's automagically when needed
139 	 */
140 	char *slave;
141 
142 	if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL)
143 		return (-1);
144 
145 	/* Open the slave side. */
146 	if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) {
147 		close(*amaster);
148 		return (-1);
149 	}
150 	return (0);
151 
152 #elif defined(HAVE_DEV_PTMX)
153 
154 #ifdef SSHD_ACQUIRES_CTTY
155 	/*
156 	 * On some (most? all?) SysV based systems with STREAMS based terminals,
157 	 * sshd will acquire a controlling terminal when it pushes the "ptem"
158 	 * module.  On such platforms, first allocate a sacrificial pty so
159 	 * that sshd already has a controlling terminal before allocating the
160 	 * one that will be passed back to the user process.  This ensures
161 	 * the second pty is not already the controlling terminal for a
162 	 * different session and is available to become controlling terminal
163 	 * for the client's subprocess.  See bugzilla #245 for details.
164 	 */
165 	int r, fd;
166 	static int junk_ptyfd = -1, junk_ttyfd;
167 
168 	r = openpty_streams(amaster, aslave);
169 	if (junk_ptyfd == -1 && (fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) >= 0) {
170 		close(fd);
171 		junk_ptyfd = *amaster;
172 		junk_ttyfd = *aslave;
173 		debug("STREAMS bug workaround pty %d tty %d name %s",
174 		    junk_ptyfd, junk_ttyfd, ttyname(junk_ttyfd));
175         } else
176 		return r;
177 #endif
178 
179 	return openpty_streams(amaster, aslave);
180 
181 #elif defined(HAVE_DEV_PTS_AND_PTC)
182 	/* AIX-style pty code. */
183 	const char *ttname;
184 
185 	if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1)
186 		return (-1);
187 	if ((ttname = ttyname(*amaster)) == NULL)
188 		return (-1);
189 	if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) {
190 		close(*amaster);
191 		return (-1);
192 	}
193 	return (0);
194 
195 #else
196 	/* BSD-style pty code. */
197 	char ptbuf[64], ttbuf[64];
198 	int i;
199 	const char *ptymajors = "pqrstuvwxyzabcdefghijklmno"
200 	    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
201 	const char *ptyminors = "0123456789abcdef";
202 	int num_minors = strlen(ptyminors);
203 	int num_ptys = strlen(ptymajors) * num_minors;
204 	struct termios tio;
205 
206 	for (i = 0; i < num_ptys; i++) {
207 		snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c",
208 		    ptymajors[i / num_minors], ptyminors[i % num_minors]);
209 		snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c",
210 		    ptymajors[i / num_minors], ptyminors[i % num_minors]);
211 
212 		if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) {
213 			/* Try SCO style naming */
214 			snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i);
215 			snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i);
216 			if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1)
217 				continue;
218 		}
219 
220 		/* Open the slave side. */
221 		if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) {
222 			close(*amaster);
223 			return (-1);
224 		}
225 		/* set tty modes to a sane state for broken clients */
226 		if (tcgetattr(*amaster, &tio) != -1) {
227 			tio.c_lflag |= (ECHO | ISIG | ICANON);
228 			tio.c_oflag |= (OPOST | ONLCR);
229 			tio.c_iflag |= ICRNL;
230 			tcsetattr(*amaster, TCSANOW, &tio);
231 		}
232 
233 		return (0);
234 	}
235 	return (-1);
236 #endif
237 }
238 
239 #endif /* !defined(HAVE_OPENPTY) */
240 
241