#include <config.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#ifdef HAVE_SYS_STROPTS_H
#include <sys/stropts.h>
#endif
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#if defined(HAVE_LIBUTIL_H)
# include <libutil.h>
#elif defined(HAVE_UTIL_H)
# include <util.h>
#endif
#ifdef HAVE_PTY_H
# include <pty.h>
#endif
#include "sudo.h"
#if defined(HAVE_OPENPTY)
int
get_pty(master, slave, name, namesz, ttyuid)
int *master;
int *slave;
char *name;
size_t namesz;
uid_t ttyuid;
{
struct group *gr;
gid_t ttygid = -1;
if ((gr = sudo_getgrnam("tty")) != NULL) {
ttygid = gr->gr_gid;
gr_delref(gr);
}
if (openpty(master, slave, name, NULL, NULL) != 0)
return 0;
if (chown(name, ttyuid, ttygid) != 0)
return 0;
return 1;
}
#elif defined(HAVE__GETPTY)
int
get_pty(master, slave, name, namesz, ttyuid)
int *master;
int *slave;
char *name;
size_t namesz;
uid_t ttyuid;
{
char *line;
line = _getpty(master, O_RDWR, S_IRUSR|S_IWUSR|S_IWGRP, 0);
if (line == NULL)
return 0;
*slave = open(line, O_RDWR|O_NOCTTY, 0);
if (*slave == -1) {
close(*master);
return 0;
}
(void) chown(line, ttyuid, -1);
strlcpy(name, line, namesz);
return 1;
}
#elif defined(HAVE_GRANTPT)
# ifndef HAVE_POSIX_OPENPT
static int
posix_openpt(oflag)
int oflag;
{
int fd;
# ifdef _AIX
fd = open("/dev/ptc", oflag);
# else
fd = open("/dev/ptmx", oflag);
# endif
return fd;
}
# endif
int
get_pty(master, slave, name, namesz, ttyuid)
int *master;
int *slave;
char *name;
size_t namesz;
uid_t ttyuid;
{
char *line;
*master = posix_openpt(O_RDWR|O_NOCTTY);
if (*master == -1)
return 0;
(void) grantpt(*master);
if (unlockpt(*master) != 0) {
close(*master);
return 0;
}
line = ptsname(*master);
if (line == NULL) {
close(*master);
return 0;
}
*slave = open(line, O_RDWR|O_NOCTTY, 0);
if (*slave == -1) {
close(*master);
return 0;
}
# if defined(I_PUSH) && !defined(_AIX)
ioctl(*slave, I_PUSH, "ptem");
ioctl(*slave, I_PUSH, "ldterm");
# endif
(void) chown(line, ttyuid, -1);
strlcpy(name, line, namesz);
return 1;
}
#else
static char line[] = "/dev/ptyXX";
int
get_pty(master, slave, name, namesz, ttyuid)
int *master;
int *slave;
char *name;
size_t namesz;
uid_t ttyuid;
{
char *bank, *cp;
struct group *gr;
gid_t ttygid = -1;
if ((gr = sudo_getgrnam("tty")) != NULL)
ttygid = gr->gr_gid;
for (bank = "pqrs"; *bank != '\0'; bank++) {
line[sizeof("/dev/ptyX") - 2] = *bank;
for (cp = "0123456789abcdef"; *cp != '\0'; cp++) {
line[sizeof("/dev/ptyXX") - 2] = *cp;
*master = open(line, O_RDWR|O_NOCTTY, 0);
if (*master == -1) {
if (errno == ENOENT)
return 0;
continue;
}
line[sizeof("/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
*slave = open(line, O_RDWR|O_NOCTTY, 0);
if (*slave != -1) {
strlcpy(name, line, namesz);
return 1;
}
(void) close(*master);
}
}
return 0;
}
#endif