2 * Copyright (c) 2009-2012 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>
21 #include <sys/ioctl.h>
22 #ifdef HAVE_SYS_STROPTS_H
23 #include <sys/stropts.h>
24 #endif /* HAVE_SYS_STROPTS_H */
33 #endif /* STDC_HEADERS */
36 #endif /* HAVE_STRING_H */
39 #endif /* HAVE_STRINGS_H */
42 #endif /* HAVE_UNISTD_H */
48 #if defined(HAVE_LIBUTIL_H)
50 #elif defined(HAVE_UTIL_H)
59 #if defined(HAVE_OPENPTY)
61 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
66 debug_decl(get_pty, SUDO_DEBUG_PTY)
68 if ((gr = getgrnam("tty")) != NULL)
71 if (openpty(master, slave, name, NULL, NULL) == 0) {
72 if (chown(name, ttyuid, ttygid) == 0)
76 debug_return_bool(rval);
79 #elif defined(HAVE__GETPTY)
81 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
85 debug_decl(get_pty, SUDO_DEBUG_PTY)
87 /* IRIX-style dynamic ptys (may fork) */
88 line = _getpty(master, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);
90 *slave = open(line, O_RDWR|O_NOCTTY, 0);
92 (void) chown(line, ttyuid, -1);
93 strlcpy(name, line, namesz);
100 debug_return_bool(rval);
102 #elif defined(HAVE_GRANTPT)
103 # ifndef HAVE_POSIX_OPENPT
105 posix_openpt(int oflag)
110 fd = open("/dev/ptc", oflag);
112 fd = open("/dev/ptmx", oflag);
116 # endif /* HAVE_POSIX_OPENPT */
119 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
123 debug_decl(get_pty, SUDO_DEBUG_PTY)
125 *master = posix_openpt(O_RDWR|O_NOCTTY);
127 (void) grantpt(*master); /* may fork */
128 if (unlockpt(*master) != 0) {
132 line = ptsname(*master);
137 *slave = open(line, O_RDWR|O_NOCTTY, 0);
142 # if defined(I_PUSH) && !defined(_AIX)
143 ioctl(*slave, I_PUSH, "ptem"); /* pseudo tty emulation module */
144 ioctl(*slave, I_PUSH, "ldterm"); /* line discipline module */
146 (void) chown(line, ttyuid, -1);
147 strlcpy(name, line, namesz);
151 debug_return_bool(rval);
154 #else /* Old-style BSD ptys */
156 static char line[] = "/dev/ptyXX";
159 get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid)
165 debug_decl(get_pty, SUDO_DEBUG_PTY)
167 if ((gr = getgrnam("tty")) != NULL)
170 for (bank = "pqrs"; *bank != '\0'; bank++) {
171 line[sizeof("/dev/ptyX") - 2] = *bank;
172 for (cp = "0123456789abcdef"; *cp != '\0'; cp++) {
173 line[sizeof("/dev/ptyXX") - 2] = *cp;
174 *master = open(line, O_RDWR|O_NOCTTY, 0);
177 goto done; /* out of ptys */
178 continue; /* already in use */
180 line[sizeof("/dev/p") - 2] = 't';
181 (void) chown(line, ttyuid, ttygid);
182 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
186 *slave = open(line, O_RDWR|O_NOCTTY, 0);
188 strlcpy(name, line, namesz);
189 rval = 1; /* success */
192 (void) close(*master);
198 #endif /* HAVE_OPENPTY */