1999-10-27 05:42:43 +02:00
|
|
|
/*
|
1999-11-24 14:26:21 +01:00
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
|
|
|
* Allocating a pseudo-terminal, and making it the controlling tty.
|
2000-04-16 03:18:38 +02:00
|
|
|
*
|
2000-09-16 04:29:08 +02:00
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
1999-11-24 14:26:21 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
#include "includes.h"
|
2001-12-21 04:45:46 +01:00
|
|
|
RCSID("$OpenBSD: sshpty.c,v 1.4 2001/12/19 07:18:56 deraadt Exp $");
|
1999-12-14 00:54:47 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_UTIL_H
|
|
|
|
# include <util.h>
|
|
|
|
#endif /* HAVE_UTIL_H */
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2001-02-18 20:13:33 +01:00
|
|
|
#include "sshpty.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "log.h"
|
2001-06-15 02:04:23 +02:00
|
|
|
#include "misc.h"
|
1999-11-08 05:30:59 +01:00
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
|
|
|
|
#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
|
|
|
|
#undef HAVE_DEV_PTMX
|
|
|
|
#endif
|
|
|
|
|
1999-12-07 22:53:52 +01:00
|
|
|
#ifdef HAVE_PTY_H
|
|
|
|
# include <pty.h>
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
|
|
|
|
# include <sys/stropts.h>
|
|
|
|
#endif
|
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
#ifndef O_NOCTTY
|
|
|
|
#define O_NOCTTY 0
|
|
|
|
#endif
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Allocates and opens a pty. Returns 0 if no pty could be allocated, or
|
|
|
|
* nonzero if a pty was successfully allocated. On success, open file
|
|
|
|
* descriptors for the pty and tty sides and the name of the tty side are
|
|
|
|
* returned (the buffer must be able to hold at least 64 characters).
|
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2000-04-16 03:18:38 +02:00
|
|
|
int
|
1999-12-07 05:38:31 +01:00
|
|
|
pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
1999-12-07 05:38:31 +01:00
|
|
|
#if defined(HAVE_OPENPTY) || defined(BSD4_4)
|
1999-11-24 14:26:21 +01:00
|
|
|
/* openpty(3) exists in OSF/1 and some other os'es */
|
2001-02-18 02:49:35 +01:00
|
|
|
char *name;
|
1999-11-24 14:26:21 +01:00
|
|
|
int i;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2001-02-18 02:49:35 +01:00
|
|
|
i = openpty(ptyfd, ttyfd, NULL, NULL, NULL);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (i < 0) {
|
|
|
|
error("openpty: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-18 02:49:35 +01:00
|
|
|
name = ttyname(*ttyfd);
|
|
|
|
if (!name)
|
|
|
|
fatal("openpty returns device for which ttyname fails.");
|
|
|
|
|
|
|
|
strlcpy(namebuf, name, namebuflen); /* possible truncation */
|
1999-11-24 14:26:21 +01:00
|
|
|
return 1;
|
1999-10-27 05:42:43 +02:00
|
|
|
#else /* HAVE_OPENPTY */
|
|
|
|
#ifdef HAVE__GETPTY
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
|
|
|
|
* pty's automagically when needed
|
|
|
|
*/
|
1999-11-24 14:26:21 +01:00
|
|
|
char *slave;
|
|
|
|
|
|
|
|
slave = _getpty(ptyfd, O_RDWR, 0622, 0);
|
|
|
|
if (slave == NULL) {
|
|
|
|
error("_getpty: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
1999-12-07 05:38:31 +01:00
|
|
|
strlcpy(namebuf, slave, namebuflen);
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Open the slave side. */
|
|
|
|
*ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
|
|
|
|
if (*ttyfd < 0) {
|
|
|
|
error("%.200s: %.100s", namebuf, strerror(errno));
|
|
|
|
close(*ptyfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
1999-10-27 05:42:43 +02:00
|
|
|
#else /* HAVE__GETPTY */
|
1999-12-21 01:18:08 +01:00
|
|
|
#if defined(HAVE_DEV_PTMX)
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
|
|
|
|
* also has bsd-style ptys, but they simply do not work.)
|
|
|
|
*/
|
1999-11-24 14:26:21 +01:00
|
|
|
int ptm;
|
|
|
|
char *pts;
|
2001-06-15 02:04:23 +02:00
|
|
|
mysig_t old_signal;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
|
|
|
ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
|
|
|
if (ptm < 0) {
|
|
|
|
error("/dev/ptmx: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-15 02:04:23 +02:00
|
|
|
old_signal = mysignal(SIGCHLD, SIG_DFL);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (grantpt(ptm) < 0) {
|
|
|
|
error("grantpt: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-15 02:04:23 +02:00
|
|
|
mysignal(SIGCHLD, old_signal);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (unlockpt(ptm) < 0) {
|
|
|
|
error("unlockpt: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pts = ptsname(ptm);
|
|
|
|
if (pts == NULL)
|
|
|
|
error("Slave pty side name could not be obtained.");
|
1999-12-07 05:38:31 +01:00
|
|
|
strlcpy(namebuf, pts, namebuflen);
|
1999-11-24 14:26:21 +01:00
|
|
|
*ptyfd = ptm;
|
|
|
|
|
|
|
|
/* Open the slave side. */
|
|
|
|
*ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
|
|
|
|
if (*ttyfd < 0) {
|
|
|
|
error("%.100s: %.100s", namebuf, strerror(errno));
|
|
|
|
close(*ptyfd);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-09-05 07:13:06 +02:00
|
|
|
#ifndef HAVE_CYGWIN
|
2000-11-05 16:31:36 +01:00
|
|
|
/*
|
|
|
|
* Push the appropriate streams modules, as described in Solaris pts(7).
|
|
|
|
* HP-UX pts(7) doesn't have ttcompat module.
|
|
|
|
*/
|
1999-11-24 14:26:21 +01:00
|
|
|
if (ioctl(*ttyfd, I_PUSH, "ptem") < 0)
|
|
|
|
error("ioctl I_PUSH ptem: %.100s", strerror(errno));
|
|
|
|
if (ioctl(*ttyfd, I_PUSH, "ldterm") < 0)
|
|
|
|
error("ioctl I_PUSH ldterm: %.100s", strerror(errno));
|
2000-11-05 16:31:36 +01:00
|
|
|
#ifndef __hpux
|
1999-11-24 14:26:21 +01:00
|
|
|
if (ioctl(*ttyfd, I_PUSH, "ttcompat") < 0)
|
|
|
|
error("ioctl I_PUSH ttcompat: %.100s", strerror(errno));
|
2000-09-05 07:13:06 +02:00
|
|
|
#endif
|
2000-03-17 13:58:59 +01:00
|
|
|
#endif
|
1999-11-24 14:26:21 +01:00
|
|
|
return 1;
|
1999-10-27 05:42:43 +02:00
|
|
|
#else /* HAVE_DEV_PTMX */
|
|
|
|
#ifdef HAVE_DEV_PTS_AND_PTC
|
1999-11-24 14:26:21 +01:00
|
|
|
/* AIX-style pty code. */
|
|
|
|
const char *name;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
*ptyfd = open("/dev/ptc", O_RDWR | O_NOCTTY);
|
|
|
|
if (*ptyfd < 0) {
|
|
|
|
error("Could not open /dev/ptc: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
name = ttyname(*ptyfd);
|
|
|
|
if (!name)
|
|
|
|
fatal("Open of /dev/ptc returns device for which ttyname fails.");
|
1999-12-07 05:38:31 +01:00
|
|
|
strlcpy(namebuf, name, namebuflen);
|
1999-11-24 14:26:21 +01:00
|
|
|
*ttyfd = open(name, O_RDWR | O_NOCTTY);
|
|
|
|
if (*ttyfd < 0) {
|
|
|
|
error("Could not open pty slave side %.100s: %.100s",
|
2001-12-21 04:45:46 +01:00
|
|
|
name, strerror(errno));
|
1999-11-24 14:26:21 +01:00
|
|
|
close(*ptyfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
1999-10-27 05:42:43 +02:00
|
|
|
#else /* HAVE_DEV_PTS_AND_PTC */
|
2001-08-07 01:29:16 +02:00
|
|
|
#ifdef _CRAY
|
|
|
|
char buf[64];
|
|
|
|
int i;
|
|
|
|
int highpty;
|
|
|
|
|
|
|
|
#ifdef _SC_CRAY_NPTY
|
2001-08-14 22:31:49 +02:00
|
|
|
highpty = sysconf(_SC_CRAY_NPTY);
|
|
|
|
if (highpty == -1)
|
|
|
|
highpty = 128;
|
2001-08-07 01:29:16 +02:00
|
|
|
#else
|
2001-08-14 22:31:49 +02:00
|
|
|
highpty = 128;
|
2001-08-07 01:29:16 +02:00
|
|
|
#endif
|
|
|
|
|
2001-08-14 22:31:49 +02:00
|
|
|
for (i = 0; i < highpty; i++) {
|
|
|
|
snprintf(buf, sizeof(buf), "/dev/pty/%03d", i);
|
|
|
|
*ptyfd = open(buf, O_RDWR|O_NOCTTY);
|
|
|
|
if (*ptyfd < 0)
|
|
|
|
continue;
|
|
|
|
snprintf(namebuf, namebuflen, "/dev/ttyp%03d", i);
|
|
|
|
/* Open the slave side. */
|
|
|
|
*ttyfd = open(namebuf, O_RDWR|O_NOCTTY);
|
|
|
|
if (*ttyfd < 0) {
|
2001-08-07 01:29:16 +02:00
|
|
|
error("%.100s: %.100s", namebuf, strerror(errno));
|
2001-08-14 22:31:49 +02:00
|
|
|
close(*ptyfd);
|
2001-08-14 22:41:34 +02:00
|
|
|
return 0;
|
2001-08-14 22:31:49 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2001-08-07 01:29:16 +02:00
|
|
|
#else
|
1999-11-24 14:26:21 +01:00
|
|
|
/* BSD-style pty code. */
|
|
|
|
char buf[64];
|
|
|
|
int i;
|
1999-11-25 01:54:57 +01:00
|
|
|
const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
1999-11-24 14:26:21 +01:00
|
|
|
const char *ptyminors = "0123456789abcdef";
|
|
|
|
int num_minors = strlen(ptyminors);
|
|
|
|
int num_ptys = strlen(ptymajors) * num_minors;
|
2002-05-14 02:07:18 +02:00
|
|
|
struct termios tio;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
|
|
|
for (i = 0; i < num_ptys; i++) {
|
|
|
|
snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors],
|
|
|
|
ptyminors[i % num_minors]);
|
1999-12-14 00:47:15 +01:00
|
|
|
snprintf(namebuf, namebuflen, "/dev/tty%c%c",
|
1999-12-07 05:38:31 +01:00
|
|
|
ptymajors[i / num_minors], ptyminors[i % num_minors]);
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2000-08-29 02:52:38 +02:00
|
|
|
*ptyfd = open(buf, O_RDWR | O_NOCTTY);
|
|
|
|
if (*ptyfd < 0) {
|
|
|
|
/* Try SCO style naming */
|
|
|
|
snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
|
|
|
|
snprintf(namebuf, namebuflen, "/dev/ttyp%d", i);
|
|
|
|
*ptyfd = open(buf, O_RDWR | O_NOCTTY);
|
|
|
|
if (*ptyfd < 0)
|
|
|
|
continue;
|
2001-02-05 13:42:17 +01:00
|
|
|
}
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Open the slave side. */
|
|
|
|
*ttyfd = open(namebuf, O_RDWR | O_NOCTTY);
|
|
|
|
if (*ttyfd < 0) {
|
|
|
|
error("%.100s: %.100s", namebuf, strerror(errno));
|
|
|
|
close(*ptyfd);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-05-14 02:07:18 +02:00
|
|
|
/* set tty modes to a sane state for broken clients */
|
|
|
|
if (tcgetattr(*ptyfd, &tio) < 0)
|
|
|
|
log("Getting tty modes for pty failed: %.100s", strerror(errno));
|
|
|
|
else {
|
|
|
|
tio.c_lflag |= (ECHO | ISIG | ICANON);
|
|
|
|
tio.c_oflag |= (OPOST | ONLCR);
|
|
|
|
tio.c_iflag |= ICRNL;
|
|
|
|
|
|
|
|
/* Set the new modes for the terminal. */
|
|
|
|
if (tcsetattr(*ptyfd, TCSANOW, &tio) < 0)
|
|
|
|
log("Setting tty modes for pty failed: %.100s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
return 1;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
return 0;
|
2001-08-07 01:29:16 +02:00
|
|
|
#endif /* CRAY */
|
1999-10-27 05:42:43 +02:00
|
|
|
#endif /* HAVE_DEV_PTS_AND_PTC */
|
|
|
|
#endif /* HAVE_DEV_PTMX */
|
|
|
|
#endif /* HAVE__GETPTY */
|
|
|
|
#endif /* HAVE_OPENPTY */
|
|
|
|
}
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2000-04-16 03:18:38 +02:00
|
|
|
void
|
1999-11-24 14:26:21 +01:00
|
|
|
pty_release(const char *ttyname)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2000-03-02 13:56:12 +01:00
|
|
|
if (chown(ttyname, (uid_t) 0, (gid_t) 0) < 0)
|
2000-03-02 13:30:53 +01:00
|
|
|
error("chown %.100s 0 0 failed: %.100s", ttyname, strerror(errno));
|
2000-03-02 13:56:12 +01:00
|
|
|
if (chmod(ttyname, (mode_t) 0666) < 0)
|
2000-03-02 13:30:53 +01:00
|
|
|
error("chmod %.100s 0666 failed: %.100s", ttyname, strerror(errno));
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Makes the tty the processes controlling tty and sets it to sane modes. */
|
|
|
|
|
2000-04-16 03:18:38 +02:00
|
|
|
void
|
1999-11-24 14:26:21 +01:00
|
|
|
pty_make_controlling_tty(int *ttyfd, const char *ttyname)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2001-08-07 01:29:16 +02:00
|
|
|
int fd;
|
2001-10-12 11:15:48 +02:00
|
|
|
#ifdef USE_VHANGUP
|
|
|
|
void *old;
|
|
|
|
#endif /* USE_VHANGUP */
|
2001-08-07 01:29:16 +02:00
|
|
|
|
2001-10-12 11:15:48 +02:00
|
|
|
#ifdef _CRAY
|
2001-08-14 22:31:49 +02:00
|
|
|
if (setsid() < 0)
|
|
|
|
error("setsid: %.100s", strerror(errno));
|
2001-08-07 01:29:16 +02:00
|
|
|
|
2001-08-14 22:31:49 +02:00
|
|
|
fd = open(ttyname, O_RDWR|O_NOCTTY);
|
|
|
|
if (fd != -1) {
|
2001-10-12 11:15:48 +02:00
|
|
|
mysignal(SIGHUP, SIG_IGN);
|
2001-08-14 22:31:49 +02:00
|
|
|
ioctl(fd, TCVHUP, (char *)NULL);
|
2001-10-12 11:15:48 +02:00
|
|
|
mysignal(SIGHUP, SIG_DFL);
|
2001-08-14 22:31:49 +02:00
|
|
|
setpgid(0, 0);
|
|
|
|
close(fd);
|
2001-08-07 01:29:16 +02:00
|
|
|
} else {
|
2001-08-14 22:31:49 +02:00
|
|
|
error("Failed to disconnect from controlling tty.");
|
2001-08-07 01:29:16 +02:00
|
|
|
}
|
|
|
|
|
2001-08-14 22:31:49 +02:00
|
|
|
debug("Setting controlling tty using TCSETCTTY.");
|
|
|
|
ioctl(*ttyfd, TCSETCTTY, NULL);
|
|
|
|
fd = open("/dev/tty", O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
error("%.100s: %.100s", ttyname, strerror(errno));
|
2001-08-07 01:29:16 +02:00
|
|
|
close(*ttyfd);
|
|
|
|
*ttyfd = fd;
|
2001-10-12 11:15:48 +02:00
|
|
|
#else /* _CRAY */
|
1999-10-27 05:42:43 +02:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
/* First disconnect from the old controlling tty. */
|
1999-10-27 05:42:43 +02:00
|
|
|
#ifdef TIOCNOTTY
|
2001-02-09 03:11:24 +01:00
|
|
|
fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (fd >= 0) {
|
|
|
|
(void) ioctl(fd, TIOCNOTTY, NULL);
|
|
|
|
close(fd);
|
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
#endif /* TIOCNOTTY */
|
1999-11-24 14:26:21 +01:00
|
|
|
if (setsid() < 0)
|
|
|
|
error("setsid: %.100s", strerror(errno));
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Verify that we are successfully disconnected from the controlling
|
|
|
|
* tty.
|
|
|
|
*/
|
2001-02-09 03:11:24 +01:00
|
|
|
fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (fd >= 0) {
|
|
|
|
error("Failed to disconnect from controlling tty.");
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
/* Make it our controlling tty. */
|
1999-10-27 05:42:43 +02:00
|
|
|
#ifdef TIOCSCTTY
|
1999-11-24 14:26:21 +01:00
|
|
|
debug("Setting controlling tty using TIOCSCTTY.");
|
2000-11-12 10:22:29 +01:00
|
|
|
if (ioctl(*ttyfd, TIOCSCTTY, NULL) < 0)
|
|
|
|
error("ioctl(TIOCSCTTY): %.100s", strerror(errno));
|
1999-10-27 05:42:43 +02:00
|
|
|
#endif /* TIOCSCTTY */
|
2000-10-18 02:02:25 +02:00
|
|
|
#ifdef HAVE_NEWS4
|
|
|
|
if (setpgrp(0,0) < 0)
|
|
|
|
error("SETPGRP %s",strerror(errno));
|
|
|
|
#endif /* HAVE_NEWS4 */
|
2000-09-05 07:13:06 +02:00
|
|
|
#ifdef USE_VHANGUP
|
2001-10-12 11:15:48 +02:00
|
|
|
old = mysignal(SIGHUP, SIG_IGN);
|
2000-04-20 15:12:58 +02:00
|
|
|
vhangup();
|
2001-10-12 11:15:48 +02:00
|
|
|
mysignal(SIGHUP, old);
|
2000-09-05 07:13:06 +02:00
|
|
|
#endif /* USE_VHANGUP */
|
1999-11-24 14:26:21 +01:00
|
|
|
fd = open(ttyname, O_RDWR);
|
2000-04-20 15:12:58 +02:00
|
|
|
if (fd < 0) {
|
1999-11-24 14:26:21 +01:00
|
|
|
error("%.100s: %.100s", ttyname, strerror(errno));
|
2000-04-20 15:12:58 +02:00
|
|
|
} else {
|
2000-09-05 07:13:06 +02:00
|
|
|
#ifdef USE_VHANGUP
|
2000-04-20 15:12:58 +02:00
|
|
|
close(*ttyfd);
|
|
|
|
*ttyfd = fd;
|
2000-09-05 07:13:06 +02:00
|
|
|
#else /* USE_VHANGUP */
|
1999-11-24 14:26:21 +01:00
|
|
|
close(fd);
|
2000-09-05 07:13:06 +02:00
|
|
|
#endif /* USE_VHANGUP */
|
2000-04-20 15:12:58 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Verify that we now have a controlling tty. */
|
2001-02-09 03:11:24 +01:00
|
|
|
fd = open(_PATH_TTY, O_WRONLY);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (fd < 0)
|
|
|
|
error("open /dev/tty failed - could not set controlling tty: %.100s",
|
2001-12-21 04:45:46 +01:00
|
|
|
strerror(errno));
|
1999-11-24 14:26:21 +01:00
|
|
|
else {
|
|
|
|
close(fd);
|
|
|
|
}
|
2001-10-12 11:15:48 +02:00
|
|
|
#endif /* _CRAY */
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Changes the window size associated with the pty. */
|
|
|
|
|
2000-04-16 03:18:38 +02:00
|
|
|
void
|
1999-11-24 14:26:21 +01:00
|
|
|
pty_change_window_size(int ptyfd, int row, int col,
|
2001-12-21 04:45:46 +01:00
|
|
|
int xpixel, int ypixel)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
1999-11-24 14:26:21 +01:00
|
|
|
struct winsize w;
|
|
|
|
w.ws_row = row;
|
|
|
|
w.ws_col = col;
|
|
|
|
w.ws_xpixel = xpixel;
|
|
|
|
w.ws_ypixel = ypixel;
|
|
|
|
(void) ioctl(ptyfd, TIOCSWINSZ, &w);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
2000-03-02 13:30:53 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
pty_setowner(struct passwd *pw, const char *ttyname)
|
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
gid_t gid;
|
|
|
|
mode_t mode;
|
2000-12-15 19:39:12 +01:00
|
|
|
struct stat st;
|
2000-03-02 13:30:53 +01:00
|
|
|
|
|
|
|
/* Determine the group to make the owner of the tty. */
|
|
|
|
grp = getgrnam("tty");
|
|
|
|
if (grp) {
|
|
|
|
gid = grp->gr_gid;
|
|
|
|
mode = S_IRUSR | S_IWUSR | S_IWGRP;
|
|
|
|
} else {
|
|
|
|
gid = pw->pw_gid;
|
|
|
|
mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
|
|
|
|
}
|
|
|
|
|
2000-12-15 19:39:12 +01:00
|
|
|
/*
|
|
|
|
* Change owner and mode of the tty as required.
|
2001-08-06 22:47:23 +02:00
|
|
|
* Warn but continue if filesystem is read-only and the uids match/
|
|
|
|
* tty is owned by root.
|
2000-12-15 19:39:12 +01:00
|
|
|
*/
|
|
|
|
if (stat(ttyname, &st))
|
|
|
|
fatal("stat(%.100s) failed: %.100s", ttyname,
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
if (st.st_uid != pw->pw_uid || st.st_gid != gid) {
|
|
|
|
if (chown(ttyname, pw->pw_uid, gid) < 0) {
|
2001-12-21 04:45:46 +01:00
|
|
|
if (errno == EROFS &&
|
2001-07-22 22:36:57 +02:00
|
|
|
(st.st_uid == pw->pw_uid || st.st_uid == 0))
|
2000-12-15 19:39:12 +01:00
|
|
|
error("chown(%.100s, %d, %d) failed: %.100s",
|
2001-12-21 04:45:46 +01:00
|
|
|
ttyname, pw->pw_uid, gid,
|
|
|
|
strerror(errno));
|
2000-12-15 19:39:12 +01:00
|
|
|
else
|
|
|
|
fatal("chown(%.100s, %d, %d) failed: %.100s",
|
2001-12-21 04:45:46 +01:00
|
|
|
ttyname, pw->pw_uid, gid,
|
|
|
|
strerror(errno));
|
2000-12-15 19:39:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != mode) {
|
|
|
|
if (chmod(ttyname, mode) < 0) {
|
|
|
|
if (errno == EROFS &&
|
|
|
|
(st.st_mode & (S_IRGRP | S_IROTH)) == 0)
|
|
|
|
error("chmod(%.100s, 0%o) failed: %.100s",
|
2001-12-21 04:45:46 +01:00
|
|
|
ttyname, mode, strerror(errno));
|
2000-12-15 19:39:12 +01:00
|
|
|
else
|
|
|
|
fatal("chmod(%.100s, 0%o) failed: %.100s",
|
2001-12-21 04:45:46 +01:00
|
|
|
ttyname, mode, strerror(errno));
|
2000-12-15 19:39:12 +01:00
|
|
|
}
|
|
|
|
}
|
2000-03-02 13:30:53 +01:00
|
|
|
}
|