sshpty.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* 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.
*
* 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".
*/
#include "includes.h"
#pragma ident "%Z%%M% %I% %E% SMI"
#ifdef HAVE_UTIL_H
# include <util.h>
#endif /* HAVE_UTIL_H */
#include "sshpty.h"
#include "log.h"
#include "misc.h"
/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it. */
#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
#endif
#ifdef HAVE_PTY_H
# include <pty.h>
#endif
#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
#endif
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif
/*
* 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).
*/
int
{
#if defined(HAVE_OPENPTY) || defined(BSD4_4)
/* openpty(3) exists in OSF/1 and some other os'es */
char *name;
int i;
if (i < 0) {
return 0;
}
if (!name)
fatal("openpty returns device for which ttyname fails.");
return 1;
#else /* HAVE_OPENPTY */
#ifdef HAVE__GETPTY
/*
* _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
* pty's automagically when needed
*/
char *slave;
return 0;
}
/* Open the slave side. */
if (*ttyfd < 0) {
return 0;
}
return 1;
#else /* HAVE__GETPTY */
#if defined(HAVE_DEV_PTMX)
/*
* 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.)
*/
int ptm;
char *pts;
if (ptm < 0) {
return 0;
}
return 0;
}
return 0;
}
error("Slave pty side name could not be obtained.");
/* Open the slave side. */
if (*ttyfd < 0) {
return 0;
}
#ifndef HAVE_CYGWIN
/*
* Push the appropriate streams modules, as described in Solaris pts(7).
* HP-UX pts(7) doesn't have ttcompat module.
*/
#ifndef __hpux
#endif
#endif
return 1;
#else /* HAVE_DEV_PTMX */
#ifdef HAVE_DEV_PTS_AND_PTC
/* AIX-style pty code. */
const char *name;
if (*ptyfd < 0) {
return 0;
}
if (!name)
if (*ttyfd < 0) {
error("Could not open pty slave side %.100s: %.100s",
return 0;
}
return 1;
#else /* HAVE_DEV_PTS_AND_PTC */
#ifdef _UNICOS
char buf[64];
int i;
int highpty;
#ifdef _SC_CRAY_NPTY
if (highpty == -1)
highpty = 128;
#else
highpty = 128;
#endif
for (i = 0; i < highpty; i++) {
if (*ptyfd < 0)
continue;
/* Open the slave side. */
if (*ttyfd < 0) {
return 0;
}
return 1;
}
return 0;
#else
/* BSD-style pty code. */
char buf[64];
int i;
const char *ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char *ptyminors = "0123456789abcdef";
for (i = 0; i < num_ptys; i++) {
ptyminors[i % num_minors]);
if (*ptyfd < 0) {
/* Try SCO style naming */
if (*ptyfd < 0)
continue;
}
/* Open the slave side. */
if (*ttyfd < 0) {
return 0;
}
/* set tty modes to a sane state for broken clients */
else {
/* Set the new modes for the terminal. */
}
return 1;
}
return 0;
#endif /* CRAY */
#endif /* HAVE_DEV_PTS_AND_PTC */
#endif /* HAVE_DEV_PTMX */
#endif /* HAVE__GETPTY */
#endif /* HAVE_OPENPTY */
}
/* Releases the tty. Its ownership is returned to root, and permissions to 0666. */
void
pty_release(const char *ttyname)
{
}
/* Makes the tty the processes controlling tty and sets it to sane modes. */
void
{
int fd;
#ifdef USE_VHANGUP
void *old;
#endif /* USE_VHANGUP */
#ifdef _UNICOS
if (setsid() < 0)
if (fd != -1) {
setpgid(0, 0);
} else {
error("Failed to disconnect from controlling tty.");
}
debug("Setting controlling tty using TCSETCTTY.");
if (fd < 0)
#else /* _UNICOS */
/* First disconnect from the old controlling tty. */
#ifdef TIOCNOTTY
if (fd >= 0) {
}
#endif /* TIOCNOTTY */
if (setsid() < 0)
/*
* Verify that we are successfully disconnected from the controlling
* tty.
*/
if (fd >= 0) {
error("Failed to disconnect from controlling tty.");
}
/* Make it our controlling tty. */
#ifdef TIOCSCTTY
debug("Setting controlling tty using TIOCSCTTY.");
#endif /* TIOCSCTTY */
#ifdef HAVE_NEWS4
if (setpgrp(0,0) < 0)
#endif /* HAVE_NEWS4 */
#ifdef USE_VHANGUP
vhangup();
#endif /* USE_VHANGUP */
if (fd < 0) {
} else {
#ifdef USE_VHANGUP
#else /* USE_VHANGUP */
#endif /* USE_VHANGUP */
}
/* Verify that we now have a controlling tty. */
if (fd < 0)
else
#endif /* _UNICOS */
}
/* Changes the window size associated with the pty. */
void
{
struct winsize w;
}
void
{
/* Determine the group to make the owner of the tty. */
if (grp) {
} else {
}
/*
* Change owner and mode of the tty as required.
* Warn but continue if filesystem is read-only and the uids match/
* tty is owned by root.
*/
error("chown(%.100s, %u, %u) failed: %.100s",
else
fatal("chown(%.100s, %u, %u) failed: %.100s",
}
}
error("chmod(%.100s, 0%o) failed: %.100s",
else
fatal("chmod(%.100s, 0%o) failed: %.100s",
}
}
}