#include <config.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_STROPTS_H
#include <sys/stropts.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#if defined(HAVE_OPENPTY)
# if defined(HAVE_LIBUTIL_H)
# include <libutil.h>
# elif defined(HAVE_UTIL_H)
# include <util.h>
# elif defined(HAVE_PTY_H)
# include <pty.h>
# else
# include <termios.h>
# endif
#endif
#include <sudo.h>
#if defined(HAVE_OPENPTY)
char *
get_pty(int *leader, int *follower, uid_t ttyuid)
{
struct group *gr;
gid_t ttygid = (gid_t)-1;
char name[PATH_MAX];
char *ret = NULL;
debug_decl(get_pty, SUDO_DEBUG_PTY);
if ((gr = getgrnam("tty")) != NULL)
ttygid = gr->gr_gid;
if (openpty(leader, follower, name, NULL, NULL) == 0) {
if (chown(name, ttyuid, ttygid) == 0)
ret = strdup(name);
}
debug_return_str(ret);
}
#elif defined(HAVE__GETPTY)
char *
get_pty(int *leader, int *follower, uid_t ttyuid)
{
char *line;
char *ret = NULL;
debug_decl(get_pty, SUDO_DEBUG_PTY);
line = _getpty(leader, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);
if (line != NULL) {
*follower = open(line, O_RDWR|O_NOCTTY, 0);
if (*follower != -1) {
(void) chown(line, ttyuid, -1);
ret = strdup(line);
} else {
close(*leader);
*leader = -1;
}
}
debug_return_str(ret);
}
#elif defined(HAVE_GRANTPT)
# ifndef HAVE_POSIX_OPENPT
static int
posix_openpt(int oflag)
{
int fd;
# ifdef _AIX
fd = open(_PATH_DEV "ptc", oflag);
# else
fd = open(_PATH_DEV "ptmx", oflag);
# endif
return fd;
}
# endif
char *
get_pty(int *leader, int *follower, uid_t ttyuid)
{
char *line, *ret = NULL;
debug_decl(get_pty, SUDO_DEBUG_PTY);
*leader = posix_openpt(O_RDWR|O_NOCTTY);
if (*leader != -1) {
(void) grantpt(*leader);
if (unlockpt(*leader) != 0) {
close(*leader);
goto done;
}
line = ptsname(*leader);
if (line == NULL) {
close(*leader);
goto done;
}
*follower = open(line, O_RDWR|O_NOCTTY, 0);
if (*follower == -1) {
close(*leader);
goto done;
}
# if defined(I_PUSH) && !defined(_AIX)
ioctl(*follower, I_PUSH, "ptem");
ioctl(*follower, I_PUSH, "ldterm");
# endif
(void) chown(line, ttyuid, -1);
ret = strdup(line);
}
done:
debug_return_str(ret);
}
#else
static char line[] = _PATH_DEV "ptyXX";
char *
get_pty(int *leader, int *follower, uid_t ttyuid)
{
char *bank, *cp;
struct group *gr;
gid_t ttygid = -1;
char *ret = NULL;
debug_decl(get_pty, SUDO_DEBUG_PTY);
if ((gr = getgrnam("tty")) != NULL)
ttygid = gr->gr_gid;
for (bank = "pqrs"; *bank != '\0'; bank++) {
line[sizeof(_PATH_DEV "ptyX") - 2] = *bank;
for (cp = "0123456789abcdef"; *cp != '\0'; cp++) {
line[sizeof(_PATH_DEV "ptyXX") - 2] = *cp;
*leader = open(line, O_RDWR|O_NOCTTY, 0);
if (*leader == -1) {
if (errno == ENOENT)
goto done;
continue;
}
line[sizeof(_PATH_DEV "p") - 2] = 't';
(void) chown(line, ttyuid, ttygid);
(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
# ifdef HAVE_REVOKE
(void) revoke(line);
# endif
*follower = open(line, O_RDWR|O_NOCTTY, 0);
if (*follower != -1) {
ret = strdup(line);
goto done;
}
(void) close(*leader);
}
}
done:
debug_return_str(ret);
}
#endif