]> andersk Git - openssh.git/blob - openbsd-compat/bsd-openpty.c
0b77a1da9a593c5a2ed11b9e111298d3b4c5dd62
[openssh.git] / openbsd-compat / bsd-openpty.c
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 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 #endif
43
44 #ifdef HAVE_FCNTL_H
45 # include <fcntl.h>
46 #endif
47
48 #ifdef HAVE_UTIL_H
49 # include <util.h>
50 #endif /* HAVE_UTIL_H */
51
52 #ifdef HAVE_PTY_H
53 # include <pty.h>
54 #endif
55 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
56 # include <sys/stropts.h>
57 #endif
58
59 #include <signal.h>
60
61 #ifndef O_NOCTTY
62 #define O_NOCTTY 0
63 #endif
64
65 int
66 openpty(int *amaster, int *aslave, char *name, struct termios *termp,
67    struct winsize *winp)
68 {
69 #if defined(HAVE__GETPTY)
70         /*
71          * _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
72          * pty's automagically when needed
73          */
74         char *slave;
75
76         if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL)
77                 return (-1);
78
79         /* Open the slave side. */
80         if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) {
81                 close(*amaster);
82                 return (-1);
83         }
84         return (0);
85
86 #elif defined(HAVE_DEV_PTMX)
87         /*
88          * This code is used e.g. on Solaris 2.x.  (Note that Solaris 2.3
89          * also has bsd-style ptys, but they simply do not work.)
90          */
91         int ptm;
92         char *pts;
93         mysig_t old_signal;
94
95         if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
96                 return (-1);
97
98         /* XXX: need to close ptm on error? */
99         old_signal = signal(SIGCHLD, SIG_DFL);
100         if (grantpt(ptm) < 0)
101                 return (-1);
102         signal(SIGCHLD, old_signal);
103
104         if (unlockpt(ptm) < 0)
105                 return (-1);
106
107         if ((pts = ptsname(ptm)) == NULL)
108                 return (-1);
109         *amaster = ptm;
110
111         /* Open the slave side. */
112         if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) {
113                 close(*amaster);
114                 return (-1);
115         }
116
117         /*
118          * Try to push the appropriate streams modules, as described 
119          * in Solaris pts(7).
120          */
121         ioctl(*aslave, I_PUSH, "ptem");
122         ioctl(*aslave, I_PUSH, "ldterm");
123 # ifndef __hpux
124         ioctl(*aslave, I_PUSH, "ttcompat");
125 # endif /* __hpux */
126
127         return (0);
128
129 #elif defined(HAVE_DEV_PTS_AND_PTC)
130         /* AIX-style pty code. */
131         const char *ttname;
132
133         if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1)
134                 return (-1);
135         if ((ttname = ttyname(*amaster)) == NULL)
136                 return (-1);
137         if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) {
138                 close(*amaster);
139                 return (-1);
140         }
141         return (0);
142
143 #elif defined(_UNICOS)
144         char ptbuf[64], ttbuf[64];
145         int i;
146         int highpty;
147
148         highpty = 128;
149 #ifdef _SC_CRAY_NPTY
150         if ((highpty = sysconf(_SC_CRAY_NPTY)) == -1)
151                 highpty = 128;
152 #endif /* _SC_CRAY_NPTY */
153
154         for (i = 0; i < highpty; i++) {
155                 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty/%03d", i);
156                 snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%03d", i);
157                 if ((*amaster = open(ptbuf, O_RDWR|O_NOCTTY)) == -1)
158                         continue;
159                 /* Open the slave side. */
160                 if ((*aslave = open(ttbuf, O_RDWR|O_NOCTTY)) == -1) {
161                         close(*amaster);
162                         return (-1);
163                 }
164                 return (0);
165         }
166         return (-1);
167
168 #else
169         /* BSD-style pty code. */
170         char ptbuf[64], ttbuf[64];
171         int i;
172         const char *ptymajors = "pqrstuvwxyzabcdefghijklmno"
173             "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
174         const char *ptyminors = "0123456789abcdef";
175         int num_minors = strlen(ptyminors);
176         int num_ptys = strlen(ptymajors) * num_minors;
177         struct termios tio;
178
179         for (i = 0; i < num_ptys; i++) {
180                 snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c", 
181                     ptymajors[i / num_minors], ptyminors[i % num_minors]);
182                 snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c",
183                     ptymajors[i / num_minors], ptyminors[i % num_minors]);
184
185                 if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) {
186                         /* Try SCO style naming */
187                         snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i);
188                         snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i);
189                         if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1)
190                                 continue;
191                 }
192
193                 /* Open the slave side. */
194                 if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) {
195                         close(*amaster);
196                         return (-1);
197                 }
198                 /* set tty modes to a sane state for broken clients */
199                 if (tcgetattr(*amaster, &tio) != -1) {
200                         tio.c_lflag |= (ECHO | ISIG | ICANON);
201                         tio.c_oflag |= (OPOST | ONLCR);
202                         tio.c_iflag |= ICRNL;
203                         tcsetattr(*amaster, TCSANOW, &tio);
204                 }
205
206                 return (0);
207         }
208         return (-1);
209 #endif
210 }
211
212 #endif /* !defined(HAVE_OPENPTY) */
213
This page took 0.782444 seconds and 3 git commands to generate.