| 1 |
/* |
| 2 |
* stunnel TLS offloading and load-balancing proxy |
| 3 |
* Copyright (C) 1998-2026 Michal Trojnara <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the |
| 7 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 8 |
* option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, see <http://www.gnu.org/licenses>. |
| 17 |
* |
| 18 |
* Linking stunnel statically or dynamically with other modules is making |
| 19 |
* a combined work based on stunnel. Thus, the terms and conditions of |
| 20 |
* the GNU General Public License cover the whole combination. |
| 21 |
* |
| 22 |
* In addition, as a special exception, the copyright holder of stunnel |
| 23 |
* gives you permission to combine stunnel with free software programs or |
| 24 |
* libraries that are released under the GNU LGPL and with code included |
| 25 |
* in the standard release of OpenSSL under the OpenSSL License (or |
| 26 |
* modified versions of such code, with unchanged license). You may copy |
| 27 |
* and distribute such a system following the terms of the GNU GPL for |
| 28 |
* stunnel and the licenses of the other code concerned. |
| 29 |
* |
| 30 |
* Note that people who make modified versions of stunnel are not obligated |
| 31 |
* to grant this special exception for their modified versions; it is their |
| 32 |
* choice whether to do so. The GNU General Public License gives permission |
| 33 |
* to release a modified version without this exception; this exception |
| 34 |
* also makes it possible to release a modified version which carries |
| 35 |
* forward this exception. |
| 36 |
*/ |
| 37 |
|
| 38 |
#include "prototypes.h" |
| 39 |
|
| 40 |
#ifdef HAVE_UTIL_H |
| 41 |
#include <util.h> |
| 42 |
#endif /* HAVE_UTIL_H */ |
| 43 |
|
| 44 |
#ifdef HAVE_SYS_IOCTL_H |
| 45 |
#include <sys/ioctl.h> |
| 46 |
#endif /* HAVE_SYS_IOCTL_H */ |
| 47 |
|
| 48 |
/* pty allocated with _getpty gets broken if we do I_PUSH:es to it. */ |
| 49 |
#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY) |
| 50 |
#undef HAVE_DEV_PTMX |
| 51 |
#endif /* HAVE__GETPTY || HAVE_OPENPTY */ |
| 52 |
|
| 53 |
#ifdef HAVE_PTY_H |
| 54 |
#include <pty.h> |
| 55 |
#endif /* HAVE_PTY_H */ |
| 56 |
|
| 57 |
#ifdef HAVE_LIBUTIL_H |
| 58 |
#include <libutil.h> |
| 59 |
#endif /* HAVE_LIBUTIL_H */ |
| 60 |
|
| 61 |
#ifndef O_NOCTTY |
| 62 |
#define O_NOCTTY 0 |
| 63 |
#endif /* O_NOCTTY */ |
| 64 |
|
| 65 |
/* |
| 66 |
* allocates and opens a pty |
| 67 |
* returns -1 if no pty could be allocated, or zero if a pty was successfully |
| 68 |
* allocated |
| 69 |
* on success, open file descriptors for the pty and tty sides and the name of |
| 70 |
* the tty side are returned |
| 71 |
* the buffer must be able to hold at least 64 characters |
| 72 |
*/ |
| 73 |
|
| 74 |
int pty_allocate(int *ptyfd, int *ttyfd, char *namebuf) { |
| 75 |
#if defined(HAVE_OPENPTY) || defined(BSD4_4) && !defined(__INNOTEK_LIBC__) |
| 76 |
/* openpty(3) exists in OSF/1 and some other os'es */ |
| 77 |
char buf[64]; |
| 78 |
int i; |
| 79 |
|
| 80 |
i=openpty(ptyfd, ttyfd, buf, NULL, NULL); |
| 81 |
if(i<0) { |
| 82 |
ioerror("openpty"); |
| 83 |
return -1; |
| 84 |
} |
| 85 |
strcpy(namebuf, buf); /* possible truncation */ |
| 86 |
return 0; |
| 87 |
#else /* HAVE_OPENPTY */ |
| 88 |
#ifdef HAVE__GETPTY |
| 89 |
/* |
| 90 |
* _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more |
| 91 |
* pty's automagically when needed |
| 92 |
*/ |
| 93 |
char *slave; |
| 94 |
|
| 95 |
slave=_getpty(ptyfd, O_RDWR, 0622, 0); |
| 96 |
if(slave==NULL) { |
| 97 |
ioerror("_getpty"); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
strcpy(namebuf, slave); |
| 101 |
/* open the slave side */ |
| 102 |
*ttyfd=open(namebuf, O_RDWR|O_NOCTTY); |
| 103 |
if(*ttyfd<0) { |
| 104 |
ioerror(namebuf); |
| 105 |
close(*ptyfd); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
return 0; |
| 109 |
#else /* HAVE__GETPTY */ |
| 110 |
#if defined(HAVE_DEV_PTMX) |
| 111 |
/* |
| 112 |
* this code is used e.g. on Solaris 2.x |
| 113 |
* note that Solaris 2.3 * also has bsd-style ptys, but they simply do not |
| 114 |
* work |
| 115 |
*/ |
| 116 |
int ptm; char *pts; |
| 117 |
|
| 118 |
ptm=open("/dev/ptmx", O_RDWR|O_NOCTTY); |
| 119 |
if(ptm<0) { |
| 120 |
ioerror("/dev/ptmx"); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
if(grantpt(ptm)<0) { |
| 124 |
ioerror("grantpt"); |
| 125 |
/* return -1; */ |
| 126 |
/* can you tell me why it doesn't work? */ |
| 127 |
} |
| 128 |
if(unlockpt(ptm)<0) { |
| 129 |
ioerror("unlockpt"); |
| 130 |
return -1; |
| 131 |
} |
| 132 |
pts=ptsname(ptm); |
| 133 |
if(pts==NULL) |
| 134 |
s_log(LOG_ERR, "Slave pty side name could not be obtained"); |
| 135 |
strcpy(namebuf, pts); |
| 136 |
*ptyfd=ptm; |
| 137 |
|
| 138 |
/* open the slave side */ |
| 139 |
*ttyfd=open(namebuf, O_RDWR|O_NOCTTY); |
| 140 |
if(*ttyfd<0) { |
| 141 |
ioerror(namebuf); |
| 142 |
close(*ptyfd); |
| 143 |
return -1; |
| 144 |
} |
| 145 |
/* push the appropriate streams modules, as described in Solaris pts(7) */ |
| 146 |
if(ioctl(*ttyfd, I_PUSH, "ptem")<0) |
| 147 |
ioerror("ioctl I_PUSH ptem"); |
| 148 |
if(ioctl(*ttyfd, I_PUSH, "ldterm")<0) |
| 149 |
ioerror("ioctl I_PUSH ldterm"); |
| 150 |
if(ioctl(*ttyfd, I_PUSH, "ttcompat")<0) |
| 151 |
ioerror("ioctl I_PUSH ttcompat"); |
| 152 |
return 0; |
| 153 |
#else /* HAVE_DEV_PTMX */ |
| 154 |
#ifdef HAVE_DEV_PTS_AND_PTC |
| 155 |
/* AIX-style pty code. */ |
| 156 |
const char *name; |
| 157 |
|
| 158 |
*ptyfd=open("/dev/ptc", O_RDWR|O_NOCTTY); |
| 159 |
if(*ptyfd<0) { |
| 160 |
ioerror("open(/dev/ptc)"); |
| 161 |
return -1; |
| 162 |
} |
| 163 |
name=ttyname(*ptyfd); |
| 164 |
if(!name) { |
| 165 |
s_log(LOG_ERR, "Open of /dev/ptc returns device for which ttyname fails"); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
strcpy(namebuf, name); |
| 169 |
*ttyfd=open(name, O_RDWR|O_NOCTTY); |
| 170 |
if(*ttyfd<0) { |
| 171 |
ioerror(name); |
| 172 |
close(*ptyfd); |
| 173 |
return -1; |
| 174 |
} |
| 175 |
return 0; |
| 176 |
#else /* HAVE_DEV_PTS_AND_PTC */ |
| 177 |
/* BSD-style pty code. */ |
| 178 |
char buf[64]; |
| 179 |
size_t i; |
| 180 |
const char *ptymajors="pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 181 |
const char *ptyminors="0123456789abcdef"; |
| 182 |
size_t num_minors=strlen(ptyminors); |
| 183 |
size_t num_ptys=strlen(ptymajors)*num_minors; |
| 184 |
|
| 185 |
for(i=0; i<num_ptys; i++) { |
| 186 |
#ifdef HAVE_SNPRINTF |
| 187 |
snprintf(buf, sizeof buf, |
| 188 |
#else |
| 189 |
sprintf(buf, |
| 190 |
#endif |
| 191 |
"/dev/pty%c%c", ptymajors[i/num_minors], |
| 192 |
ptyminors[i%num_minors]); |
| 193 |
*ptyfd=open(buf, O_RDWR|O_NOCTTY); |
| 194 |
if(*ptyfd<0) |
| 195 |
continue; |
| 196 |
#ifdef HAVE_SNPRINTF |
| 197 |
snprintf(namebuf, 64, |
| 198 |
#else |
| 199 |
sprintf(namebuf, |
| 200 |
#endif |
| 201 |
"/dev/tty%c%c", |
| 202 |
ptymajors[i/num_minors], ptyminors[i%num_minors]); |
| 203 |
|
| 204 |
/* open the slave side */ |
| 205 |
*ttyfd=open(namebuf, O_RDWR | O_NOCTTY); |
| 206 |
if(*ttyfd<0) { |
| 207 |
ioerror(namebuf); |
| 208 |
close(*ptyfd); |
| 209 |
return -1; |
| 210 |
} |
| 211 |
return 0; |
| 212 |
} |
| 213 |
return -1; |
| 214 |
#endif /* HAVE_DEV_PTS_AND_PTC */ |
| 215 |
#endif /* HAVE_DEV_PTMX */ |
| 216 |
#endif /* HAVE__GETPTY */ |
| 217 |
#endif /* HAVE_OPENPTY */ |
| 218 |
} |
| 219 |
|
| 220 |
/* end of pty.c */ |