2 * Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/param.h>
22 #include <sys/ioctl.h>
23 #ifdef HAVE_SYS_STROPTS_H
24 #include <sys/stropts.h>
25 #endif /* HAVE_SYS_STROPTS_H */
34 #endif /* STDC_HEADERS */
37 #endif /* HAVE_STRING_H */
40 #endif /* HAVE_STRINGS_H */
43 #endif /* HAVE_UNISTD_H */
49 #if defined(HAVE_LIBUTIL_H)
51 #elif defined(HAVE_UTIL_H)
60 #if defined(HAVE_OPENPTY)
62 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
67 debug_decl(get_pty, SUDO_DEBUG_PTY)
69 if ((gr = getgrnam("tty")) != NULL)
72 if (openpty(master, slave, name, NULL, NULL) == 0) {
73 if (chown(name, ttyuid, ttygid) == 0)
77 debug_return_bool(rval);
80 #elif defined(HAVE__GETPTY)
82 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
86 debug_decl(get_pty, SUDO_DEBUG_PTY)
88 /* IRIX-style dynamic ptys (may fork) */
89 line = _getpty(master, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);
91 *slave = open(line, O_RDWR|O_NOCTTY, 0);
93 (void) chown(line, ttyuid, -1);
94 strlcpy(name, line, namesz);
101 debug_return_bool(rval);
103 #elif defined(HAVE_GRANTPT)
104 # ifndef HAVE_POSIX_OPENPT
106 posix_openpt(int oflag)
111 fd = open("/dev/ptc", oflag);
113 fd = open("/dev/ptmx", oflag);
117 # endif /* HAVE_POSIX_OPENPT */
120 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
124 debug_decl(get_pty, SUDO_DEBUG_PTY)
126 *master = posix_openpt(O_RDWR|O_NOCTTY);
128 (void) grantpt(*master); /* may fork */
129 if (unlockpt(*master) != 0) {
133 line = ptsname(*master);
138 *slave = open(line, O_RDWR|O_NOCTTY, 0);
143 # if defined(I_PUSH) && !defined(_AIX)
144 ioctl(*slave, I_PUSH, "ptem"); /* pseudo tty emulation module */
145 ioctl(*slave, I_PUSH, "ldterm"); /* line discipline module */
147 (void) chown(line, ttyuid, -1);
148 strlcpy(name, line, namesz);
152 debug_return_bool(rval);
155 #else /* Old-style BSD ptys */
157 static char line[] = "/dev/ptyXX";
160 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
166 debug_decl(get_pty, SUDO_DEBUG_PTY)
168 if ((gr = getgrnam("tty")) != NULL)
171 for (bank = "pqrs"; *bank != '\0'; bank++) {
172 line[sizeof("/dev/ptyX") - 2] = *bank;
173 for (cp = "0123456789abcdef"; *cp != '\0'; cp++) {
174 line[sizeof("/dev/ptyXX") - 2] = *cp;
175 *master = open(line, O_RDWR|O_NOCTTY, 0);
178 goto done; /* out of ptys */
179 continue; /* already in use */
181 line[sizeof("/dev/p") - 2] = 't';
182 (void) chown(line, ttyuid, ttygid);
183 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
187 *slave = open(line, O_RDWR|O_NOCTTY, 0);
189 strlcpy(name, line, namesz);
190 rval = 1; /* success */
193 (void) close(*master);
199 #endif /* HAVE_OPENPTY */