blob: 123a9be5650d43c2d51d0c7e6b904999245b7084 [file] [log] [blame]
Damien Millerf4da3bb2004-01-21 17:07:16 +11001/*
2 * Please note: this implementation of openpty() is far from complete.
3 * it is just enough for portable OpenSSH's needs.
4 */
5
6/*
Damien Miller98225c22004-02-17 16:49:41 +11007 * Copyright (c) 2004 Damien Miller <djm@mindrot.org>
Damien Millerf4da3bb2004-01-21 17:07:16 +11008 *
Damien Miller98225c22004-02-17 16:49:41 +11009 * 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.
Damien Millerf4da3bb2004-01-21 17:07:16 +110012 *
Damien Miller98225c22004-02-17 16:49:41 +110013 * 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.
Damien Millerf4da3bb2004-01-21 17:07:16 +110020 */
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
Darren Tucker44c828f2006-07-11 18:00:06 +100038#include <sys/types.h>
39
Darren Tuckerf78fb542006-08-06 21:25:24 +100040#include <stdlib.h>
41
Darren Tucker44c828f2006-07-11 18:00:06 +100042#ifdef HAVE_SYS_STAT_H
43# include <sys/stat.h>
44#endif
Darren Tuckerf80f5ec2006-08-24 19:52:30 +100045#ifdef HAVE_SYS_IOCTL_H
46# include <sys/ioctl.h>
47#endif
Darren Tucker44c828f2006-07-11 18:00:06 +100048
49#ifdef HAVE_FCNTL_H
50# include <fcntl.h>
51#endif
52
Damien Millerf4da3bb2004-01-21 17:07:16 +110053#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
Damien Miller6645e7a2006-03-15 14:42:54 +110064#include <signal.h>
Damien Miller62da44f2006-07-24 15:08:35 +100065#include <string.h>
66#include <unistd.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110067
Damien Millerf4da3bb2004-01-21 17:07:16 +110068#ifndef O_NOCTTY
69#define O_NOCTTY 0
70#endif
71
72int
73openpty(int *amaster, int *aslave, char *name, struct termios *termp,
74 struct winsize *winp)
75{
76#if defined(HAVE__GETPTY)
77 /*
78 * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
79 * pty's automagically when needed
80 */
81 char *slave;
82
83 if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL)
84 return (-1);
85
86 /* Open the slave side. */
87 if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) {
88 close(*amaster);
89 return (-1);
90 }
91 return (0);
92
93#elif defined(HAVE_DEV_PTMX)
94 /*
95 * This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
96 * also has bsd-style ptys, but they simply do not work.)
97 */
98 int ptm;
99 char *pts;
100 mysig_t old_signal;
101
102 if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
103 return (-1);
104
105 /* XXX: need to close ptm on error? */
106 old_signal = signal(SIGCHLD, SIG_DFL);
107 if (grantpt(ptm) < 0)
108 return (-1);
109 signal(SIGCHLD, old_signal);
110
111 if (unlockpt(ptm) < 0)
112 return (-1);
113
114 if ((pts = ptsname(ptm)) == NULL)
115 return (-1);
116 *amaster = ptm;
117
118 /* Open the slave side. */
119 if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) {
120 close(*amaster);
121 return (-1);
122 }
123
Darren Tuckerb7b83342019-04-26 18:06:34 +1000124# if defined(I_FIND) && defined(__SVR4)
125 /*
126 * If the streams modules have already been pushed then there
127 * is no more work to do here.
128 */
129 if (ioctl(*aslave, I_FIND, "ptem") != 0)
130 return 0;
131# endif
132
Damien Millerf4da3bb2004-01-21 17:07:16 +1100133 /*
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000134 * Try to push the appropriate streams modules, as described
Damien Millerf4da3bb2004-01-21 17:07:16 +1100135 * in Solaris pts(7).
136 */
137 ioctl(*aslave, I_PUSH, "ptem");
138 ioctl(*aslave, I_PUSH, "ldterm");
139# ifndef __hpux
140 ioctl(*aslave, I_PUSH, "ttcompat");
141# endif /* __hpux */
Damien Millerf4da3bb2004-01-21 17:07:16 +1100142
143 return (0);
144
145#elif defined(HAVE_DEV_PTS_AND_PTC)
146 /* AIX-style pty code. */
147 const char *ttname;
148
149 if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1)
150 return (-1);
151 if ((ttname = ttyname(*amaster)) == NULL)
152 return (-1);
153 if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) {
154 close(*amaster);
155 return (-1);
156 }
157 return (0);
158
Damien Millerf4da3bb2004-01-21 17:07:16 +1100159#else
160 /* BSD-style pty code. */
161 char ptbuf[64], ttbuf[64];
162 int i;
163 const char *ptymajors = "pqrstuvwxyzabcdefghijklmno"
164 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
165 const char *ptyminors = "0123456789abcdef";
166 int num_minors = strlen(ptyminors);
167 int num_ptys = strlen(ptymajors) * num_minors;
168 struct termios tio;
169
170 for (i = 0; i < num_ptys; i++) {
Darren Tuckerc20dccb2016-08-02 09:44:25 +1000171 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c",
Damien Millerf4da3bb2004-01-21 17:07:16 +1100172 ptymajors[i / num_minors], ptyminors[i % num_minors]);
173 snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c",
174 ptymajors[i / num_minors], ptyminors[i % num_minors]);
175
176 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) {
177 /* Try SCO style naming */
178 snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i);
179 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i);
180 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1)
181 continue;
182 }
183
184 /* Open the slave side. */
185 if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) {
186 close(*amaster);
187 return (-1);
188 }
189 /* set tty modes to a sane state for broken clients */
190 if (tcgetattr(*amaster, &tio) != -1) {
191 tio.c_lflag |= (ECHO | ISIG | ICANON);
192 tio.c_oflag |= (OPOST | ONLCR);
193 tio.c_iflag |= ICRNL;
194 tcsetattr(*amaster, TCSANOW, &tio);
195 }
196
197 return (0);
198 }
199 return (-1);
200#endif
201}
202
203#endif /* !defined(HAVE_OPENPTY) */
204