From 504ac9ed3062640993a0b75f0043a30f84e9da13 Mon Sep 17 00:00:00 2001 From: Sam Hartman Date: Mon, 31 Jul 1995 22:19:47 +0000 Subject: [PATCH] Check in first version of libpty git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@6370 dc483132-0cff-0310-8789-dd5450dbe970 --- src/util/pty/Makefile.in | 91 ++++++++++++++++++++ src/util/pty/cleanup.c | 55 +++++++++++++ src/util/pty/configure.in | 142 ++++++++++++++++++++++++++++++++ src/util/pty/getpty.c | 96 +++++++++++++++++++++ src/util/pty/initialize_slave.c | 89 ++++++++++++++++++++ src/util/pty/libpty.h | 52 ++++++++++++ src/util/pty/open_ctty.c | 79 ++++++++++++++++++ src/util/pty/open_slave.c | 77 +++++++++++++++++ src/util/pty/pty-int.h | 91 ++++++++++++++++++++ src/util/pty/pty_err.et | 41 +++++++++ src/util/pty/update_utmp.c | 114 +++++++++++++++++++++++++ src/util/pty/update_wtmp.c | 68 +++++++++++++++ src/util/pty/vhangup.c | 47 +++++++++++ src/util/pty/void_assoc.c | 36 ++++++++ 14 files changed, 1078 insertions(+) create mode 100644 src/util/pty/Makefile.in create mode 100644 src/util/pty/cleanup.c create mode 100644 src/util/pty/configure.in create mode 100644 src/util/pty/getpty.c create mode 100644 src/util/pty/initialize_slave.c create mode 100644 src/util/pty/libpty.h create mode 100644 src/util/pty/open_ctty.c create mode 100644 src/util/pty/open_slave.c create mode 100644 src/util/pty/pty-int.h create mode 100644 src/util/pty/pty_err.et create mode 100644 src/util/pty/update_utmp.c create mode 100644 src/util/pty/update_wtmp.c create mode 100644 src/util/pty/vhangup.c create mode 100644 src/util/pty/void_assoc.c diff --git a/src/util/pty/Makefile.in b/src/util/pty/Makefile.in new file mode 100644 index 000000000..1e6545888 --- /dev/null +++ b/src/util/pty/Makefile.in @@ -0,0 +1,91 @@ +CFLAGS = $(CCOPTS) $(DEFS) $(LOCALINCLUDE) +SED = sed + + +.c.o: + $(CC) $(CFLAGS) -c $(srcdir)/$*.c + + +LIBOBJS= cleanup.o getpty.o initialize_slave.o open_ctty.o open_slave.o update_utmp.o update_wtmp.o vhangup.o void_assoc.o pty_err.o logwtmp.o + +LIBDONE=DONE +LIB_SUBDIRS=. +INSTALLFILE = cp + +# for pty-int.h +LOCALINCLUDE=-I. -I$(srcdir) + +FILES= Makefile cleanup.c getpty.c initialize_slave.c open_ctty.c open_slave.c update_utmp.c update_wtmp.c vhangup.c void_assoc.c pty_err.h pty_err.c\ +logwtmp.c + +CFILES=cleanup.c getpty.c initialize_slave.c open_ctty.c open_slave.c\ +update_utmp.c update_wtmp.c vhangup.c void_assoc.c pty_err.c logwtmp.c + + +SRCS=pty_err.h $(CFILES) + +DEPLIBS= + +all:: all-$(WHAT) + +# +all-unix:: includes pty_err.h libpty.a $(TOPLIBD)/libpty.a + + + +clean:: clean-$(WHAT) + +clean-unix:: + $(RM) libpty.a $(BUILDTOP)/include/libpty.h pty_err.c pty_err.h + + +depend:: + +install:: libpty.h + $(INSTALL_DATA) $(srcdir)/libpty.h $(DESTDIR)$(KRB5_INCDIR)/libpty.h + + + +libpty.a: $(LIBOBJS) + $(ARCHIVE) $@ $(LIBOBJS) + $(RANLIB) $@ + +DONE:: $(LIBOBJS) + $(RM) DONE + echo $(LIBOBJS) >DONE + +clean-unix:: + $(RM) $(LIBOBJS) + +clean-mac:: + $(RM) libpty.a + $(RM) $(LIBOBJS) + +install:: + $(INSTALL_DATA) libpty.a $(DESTDIR)$(KRB5_LIBDIR)/libpty.a + $(RANLIB) $(DESTDIR)$(KRB5_LIBDIR)/libpty.a +## + +clean-unix:: clean-files + +clean-mac:: clean-files + +clean-files:: + rm -f *~ \#* *.bak \ + *.otl *.aux *.toc *.PS *.dvi *.x9700 *.ps \ + *.cp *.fn *.ky *.log *.pg *.tp *.vr \ + *.o profiled/?*.o libcom_err.a libcom_err_p.a \ + com_err.o compile_et \ + et.ar TAGS y.tab.c lex.yy.c error_table.c \ + et_lex.lex.c \ + test1.h test1.c test2.h test2.c test_et \ + eddep makedep *.ln + +pty_err.o: pty_err.c + $(CC) $(CFLAGS) -c $*.c + +pty_err.h:: pty_err.et +pty_err.c:: pty_err.et + +depend:: et_lex.lex.c + diff --git a/src/util/pty/cleanup.c b/src/util/pty/cleanup.c new file mode 100644 index 000000000..c2f883789 --- /dev/null +++ b/src/util/pty/cleanup.c @@ -0,0 +1,55 @@ +/* + * pty_cleanup: Kill processes associated with pty. + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +long pty_cleanup (slave, pid, update_utmp) + char *slave; + pid_t pid; + int update_utmp; +{ + struct utmp ut; + +#ifndef NO_UT_PID + ut.ut_pid = 0; + ut.ut_type = DEAD_PROCESS; +#endif + pty_update_utmp(&ut, "", slave, (char *)0); + + (void)chmod(slave, 0666); + (void)chown(slave, 0, 0); +#ifndef HAVE_STREAMS + slave[strlen("/dev/")] = 'p'; + (void)chmod(slave, 0666); + (void)chown(slave, 0, 0); +#endif +#ifdef HAVE_REVOKE + revoke(slave); +#else /* HAVE_REVOKE*/ + #ifdef VHANG_LAST + if ( retval = ( pty_open_ctty( slave, &fd ))) + return retval; + ptyint_vhangup(); +#endif + #endif +} diff --git a/src/util/pty/configure.in b/src/util/pty/configure.in new file mode 100644 index 000000000..572fc13b5 --- /dev/null +++ b/src/util/pty/configure.in @@ -0,0 +1,142 @@ +AC_INIT(getpty.c) +CONFIG_RULES +ET_RULES +CopySrcHeader(libpty.h,[$](BUILDTOP)/include) +LinkFileDir([$](TOPLIBD)/libpty.a,libpty.a,../util/pty) +AC_PROG_INSTALL +AC_PROG_ARCHIVE +AC_PROG_RANLIB +AC_CHECK_FUNCS(fchmod fchown revoke) +dnl dbm libs for use of an_to_ln +LOGINLIBS= +dnl +dnl Make our operating system-specific security checks and definitions for +dnl login. +dnl +case $krb5_cv_host in +*-*-aix3*) # AIX has streams include files but not streams TTY +# Moreover, strops.h trashes sys/ioctl.h +krb5_cv_has_streams=no +;; +alpha-dec-osf*) + AC_CHECK_LIB(security,main, + AC_DEFINE(HAVE_SETLUID) + LOGINLIBS="$LOGINLIBS -lsecurity" + ) + ;; +esac +dnl +AC_SUBST(LOGINLIBS) +dnl +AC_TYPE_MODE_T +AC_FUNC_CHECK(strsave,AC_DEFINE(HAS_STRSAVE)) +AC_FUNC_CHECK(getutent,AC_DEFINE(HAVE_GETUTENT)) +AC_FUNC_CHECK(setreuid,AC_DEFINE(HAVE_SETREUID)) +AC_FUNC_CHECK(gettosbyname,AC_DEFINE(HAVE_GETTOSBYNAME)) +AC_FUNC_CHECK(setsid,AC_DEFINE(HAVE_SETSID)) +AC_FUNC_CHECK(ttyname,AC_DEFINE(HAVE_TTYNAME)) +AC_FUNC_CHECK(line_push,AC_DEFINE(HAVE_LINE_PUSH)) +AC_FUNC_CHECK(ptsname,AC_DEFINE(HAVE_PTSNAME)) +AC_FUNC_CHECK(grantpt,AC_DEFINE(HAVE_GRANTPT)) +AC_FUNC_CHECK(openpty,AC_DEFINE(HAVE_OPENPTY)) +AC_FUNC_CHECK(logwtmp,AC_DEFINE(HAVE_LOGWTMP)) +AC_CHECK_HEADERS(unistd.h stdlib.h string.h sys/filio.h sys/sockio.h sys/label.h sys/tty.h ttyent.h lastlog.h sys/select.h sys/ptyvar.h) +AC_REPLACE_FUNCS(getdtablesize) +DECLARE_SYS_ERRLIST +KRB5_SIGTYPE +CHECK_SIGNALS +CHECK_SETJMP +CHECK_DIRENT +CHECK_WAIT_TYPE +AC_HEADER_CHECK(termios.h,AC_FUNC_CHECK(cfsetispeed,AC_DEFINE(POSIX_TERMIOS))) +CHECK_UTMP +dnl +dnl +AC_MSG_CHECKING([streams interface]) +AC_CACHE_VAL(krb5_cv_has_streams, +[AC_TRY_COMPILE( +[#include +#include ], [], +krb5_cv_has_streams=yes, krb5_cv_has_streams=no)]) +AC_MSG_RESULT($krb5_cv_has_streams) +if test $krb5_cv_has_streams = yes; then +AC_DEFINE(HAVE_STREAMS) +fi +dnl +dnl +AC_MSG_CHECKING([F_SETOWN]) +AC_CACHE_VAL(krb5_cv_f_setown, +[AC_TRY_COMPILE( +[#include +#include ], [1+F_SETOWN;], +krb5_cv_f_setown=yes,krb5_cv_f_setown=no)]) +AC_MSG_RESULT($krb5_cv_f_setown) +if test $krb5_cv_f_setown = yes; then +AC_DEFINE(HAVE_SETOWN) +fi +dnl +dnl +AC_MSG_CHECKING([setenv]) +AC_CACHE_VAL(krb5_cv_setenv, +[AC_TRY_LINK( +[],[setenv("PATH","/bin",0);], +krb5_cv_setenv=yes,krb5_cv_setenv=no)]) +AC_MSG_RESULT($krb5_cv_setenv) +if test $krb5_cv_setenv = no; then +SETENVSRC=setenv.c +SETENVOBJ=setenv.o +AC_SUBST([SETENVSRC]) +AC_SUBST([SETENVOBJ]) +fi +dnl +dnl +AC_MSG_CHECKING([arguments to getpgrp]) +AC_CACHE_VAL(krb5_cv_getpgrp_args, +[AC_TRY_COMPILE( +[#ifndef __STDC__ +#define __STDC__ 1 +#endif +#include +#include ], [pid_t pid = getpgrp(getpid())], +krb5_cv_getpgrp_args=pid, krb5_cv_getpgrp_args=void)]) +AC_MSG_RESULT($krb5_cv_getpgrp_args) +if test $krb5_cv_getpgrp_args = pid; then +AC_DEFINE(GETPGRP_ONEARG) +fi +dnl +dnl +AC_MSG_CHECKING([number of arguments to setpgrp]) +AC_CACHE_VAL(krb5_cv_setpgrp_args, +[AC_TRY_COMPILE( +[#ifndef __STDC__ +#define __STDC__ 1 +#endif +#include ],[setpgrp(0,0)], +krb5_cv_setpgrp_args=two, krb5_cv_setpgrp_args=void)]) +AC_MSG_RESULT($krb5_cv_setpgrp_args) +if test $krb5_cv_setpgrp_args = two; then +AC_DEFINE(SETPGRP_TWOARG) +fi +dnl +dnl +AC_MSG_CHECKING([shadow password support]) +AC_CACHE_VAL(krb5_cv_shadow_pwd, +[AC_TRY_LINK( +[#include +#include +#include ], +[struct spwd *sp = getspnam("root")], +krb5_cv_shadow_pwd=yes, krb5_cv_shadow_pwd=no)]) +AC_MSG_RESULT($krb5_cv_shadow_pwd) +if test $krb5_cv_shadow_pwd = yes; then +AC_DEFINE(HAVE_SHADOW) +fi +dnl +dnl +ADD_DEF(-DKERBEROS) +AC_CONST +USE_KADM_LIBRARY +USE_KRB4_LIBRARY +KRB5_LIBRARIES +V5_USE_SHARED_LIB +V5_AC_OUTPUT_MAKEFILE diff --git a/src/util/pty/getpty.c b/src/util/pty/getpty.c new file mode 100644 index 000000000..4033ad5ad --- /dev/null +++ b/src/util/pty/getpty.c @@ -0,0 +1,96 @@ +/* + * pty_getpty: open a PTY master. + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +long pty_getpty (fd, slave) +int *fd; char *slave; +{ + char c; + char *p; + int i,ptynum; + struct stat stb; + +#ifdef HAVE_OPENPTY + int slavefd; + + if(openpty(fd, &slavefd, slave, (struct termios *) 0, + (struct winsize *) 0)) return 1; + return 0; +#else + + *fd = open("/dev/ptmx", O_RDWR|O_NDELAY); /* Solaris, IRIX */ + if (*fd < 0) *fd = open("/dev/ptc", O_RDWR|O_NDELAY); /* AIX */ + if (*fd < 0) *fd = open("/dev/pty", O_RDWR|O_NDELAY); /* sysvimp */ + + if (*fd >= 0) { + +#ifdef HAVE_GRANTPT + if (grantpt(*fd) || unlockpt(*fd)) return PTY_GETPTY_STREAMS; +#endif + +#ifdef HAVE_PTSNAME + p = ptsname(*fd); +#else +#ifdef HAVE_TTYNAME + p = ttyname(*fd); +#else + /* XXX If we don't have either what do we do */ +#endif +#endif + if (p) { + strcpy(slave, p); + return 0; + } + + if (fstat(*fd, &stb) < 0) { + close(*fd); + return PTY_GETPTY_FSTAT; + } + ptynum = (int)(stb.st_rdev&0xFF); + sprintf(slave, "/dev/ttyp%x", ptynum); + return 0; + + } else { + + for (c = 'p'; c <= 's'; c++) { + sprintf(slave,"/dev/ptyXX"); + slave[strlen("/dev/pty")] = c; + slave[strlen("/dev/ptyp")] = '0'; + if (stat(slave, &stb) < 0) + break; + for (i = 0; i < 16; i++) { + slave[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i]; + *fd = open(slave, O_RDWR); + if (*fd < 0) continue; + + /* got pty */ + slave[strlen("/dev/")] = 't'; + return 0; + } + } + return PTY_GETPTY_NOPTY; + } +#endif /* HAVE_OPENPTY */ +} + diff --git a/src/util/pty/initialize_slave.c b/src/util/pty/initialize_slave.c new file mode 100644 index 000000000..1f7327880 --- /dev/null +++ b/src/util/pty/initialize_slave.c @@ -0,0 +1,89 @@ +/* + * pty_open_slave: open slave side of terminal, clearing for use. + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +long pty_initialize_slave (fd) + int fd; +{ +#if defined(POSIX_TERMIOS) && !defined(ultrix) + struct termios new_termio; +#else + struct sgttyb b; +#endif /* POSIX_TERMIOS */ + int pid; +#ifdef POSIX_SIGNALS + struct sigaction sa; + /* Initialize "sa" structure. */ + (void) sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + +#endif + +#ifdef HAVE_STREAMS + while (ioctl (fd, I_POP, 0) == 0); /*Clear out any old lined's*/ +#ifdef HAVE_LINE_PUSH + if (line_push(fd) < 0) + { + (void) close(fd); fd = -1; + return PTY_OPEN_SLAVE_LINE_PUSHFAIL; + } +#else /*No line_push */ +#ifdef sun + if (ioctl(fd, I_PUSH, "ptem") < 0) + return PTY_OPEN_SLAVE_PUSH_FAIL; + if (ioctl(fd, I_PUSH, "ldterm") < 0) + return PTY_OPEN_SLAVE_PUSH_FAIL; + if (ioctl(fd, I_PUSH, "ttcompat") < 0) + return PTY_OPEN_SLAVE_PUSH_FAIL; + +#endif /*SUN*/ +#endif /*LINE_PUSH*/ +#endif /*HAVE_STREAMS*/ + + /* + * Under Ultrix 3.0, the pgrp of the slave pty terminal + * needs to be set explicitly. Why rlogind works at all + * without this on 4.3BSD is a mystery. + */ +#ifdef GETPGRP_ONEARG + pid = getpgrp(getpid()); +#else + pid = getpgrp(); +#endif + +#ifdef TIOCSPGRP + ioctl(fd, TIOCSPGRP, &pid); +#endif + + +#if defined(POSIX_TERMIOS) && !defined(ultrix) + tcsetpgrp(fd, pid); + tcgetattr(fd,&new_termio); + new_termio.c_cc[VMIN] = 1; + new_termio.c_cc[VTIME] = 0; + tcsetattr(fd,TCSANOW,&new_termio); +#endif /* POSIX_TERMIOS */ + + return 0; +} diff --git a/src/util/pty/libpty.h b/src/util/pty/libpty.h new file mode 100644 index 000000000..f85dda55c --- /dev/null +++ b/src/util/pty/libpty.h @@ -0,0 +1,52 @@ +/* + * Header file for manipulation of ptys + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#ifndef __LIBPTY_H__ +#include +#include + +#ifdef __STDC__ /* use prototypes */ + +long pty_getpty ( int *fd, char *slave); + +long pty_open_slave (const char *slave, int *fd); +long pty_open_ctty (const char *slave, int *fd); + +long pty_initialize_slave ( int fd); +long pty_update_utmp (struct utmp *ut, char *user, char *line, char *host); + +long pty_update_wtmp (struct utmp *ut); +long pty_logwtmp (char *tty, char * user, char *host); + +long pty_cleanup(char *slave, int pid, int update_utmp); +#else /*__STDC__*/ +long pty_getpty(); + +long pty_open_slave(); +long pty_open_ctty(); +long pty_initialize_slave(); + +long pty_update_utmp(); +long pty_utmp_wtmp(); +long pty_logwtmp(); +long pty_cleanup(); +#endif /* __STDC__*/ +#define __LIBPTY_H__ +#endif diff --git a/src/util/pty/open_ctty.c b/src/util/pty/open_ctty.c new file mode 100644 index 000000000..a4c2afd70 --- /dev/null +++ b/src/util/pty/open_ctty.c @@ -0,0 +1,79 @@ +/* + * pty_open_ctty: Open and establish controlling terminal. + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +/* + * This routine will be called twice. It's not particularly important + * that the setsid() or TIOCSTTY ioctls succeed (they may not the + * second time), but rather that we have a controlling terminal at the + * end. It is assumed that vhangup doesn't exist and confuse the + * process's notion of controlling terminal on any system without + * TIOCNOTTY. That is, either vhangup() leaves the controlling + * terminal in tact, breaks the association completely, or the system + * provides TIOCNOTTY to get things back into a reasonable state. In + * practice, vhangup() either breaks the association completely or + * doesn't effect controlling terminals, so this condition is met. + */ +long +pty_open_ctty (slave, fd) + const char * slave; + int *fd; +{ + int testfd, retval; +#ifdef HAVE_SETSID + (void) setsid(); +#endif + +#ifdef ultrix + /* The Ultrix (and other BSD tty drivers) require the process group + * to be zero, in order to acquire the new tty as a controlling tty. */ + (void) setpgrp(0, 0); +#endif +/* First, dissociate from previous terminal */ + if ( (retval = ptyint_void_association()) < 0 ) + return retval; + *fd = open(slave, O_RDWR); + if (*fd < 0 ) + return PTY_OPEN_SLAVE_OPENFAIL; + #ifdef ultrix + setpgrp(0, getpid()); +#endif +#ifdef ultrix + setpgrp(0, getpid()); +#endif + +#ifdef TIOCSCTTY + ioctl(*fd, TIOCSTTY, 0); /* Don't check return.*/ +#endif /* TIOCSTTY */ + + testfd = open("/dev/tty", O_RDWR|O_NDELAY); + if ( testfd < 0 ) + { + close(*fd); + *fd = -1; + return PTY_OPEN_SLAVE_NOCTTY; + } + close(testfd); + return 0; +} diff --git a/src/util/pty/open_slave.c b/src/util/pty/open_slave.c new file mode 100644 index 000000000..d96b0db2b --- /dev/null +++ b/src/util/pty/open_slave.c @@ -0,0 +1,77 @@ +/* + * pty_open_slave: open slave side of terminal, clearing for use. + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +long pty_open_slave ( slave, fd) + const char *slave; + int *fd; +{ + int vfd; +long retval; +#ifdef POSIX_SIGNALS + struct sigaction sa; + /* Initialize "sa" structure. */ + (void) sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + +#endif + + /* First, chmod and chown the slave*/ +if (( retval = pty_open_ctty ( slave, &vfd )) != 0 ) + return retval; + + if (vfd < 0) + return PTY_OPEN_SLAVE_OPENFAIL; + +#ifndef HAVE_FCHMOD + if (chmod(line, 0)) + return PTY_OPEN_SLAVE_CHMODFAIL; +#else + if (fchmod(vfd, 0)) +return PTY_OPEN_SLAVE_CHMODFAIL; +#endif /*HAVE_FCHMOD*/ +#ifdef HAVE_FCHOWN + if ( fchown(vfd, 0, 0 ) == -1 ) + #else + if ( chown(slave, 0, 0 ) == -1 ) +#endif /* HAVE_FCHOWN*/ + return PTY_OPEN_SLAVE_CHOWNFAIL; + + #ifdef VHANG_FIRST + ptyint_vhangup(); +#endif + + (void) close(vfd); + #ifdef HAVE_REVOKE + if (revoke (slave) < 0 ) { + return PTY_OPEN_SLAVE_REVOKEFAIL; + } +#endif /*HAVE_REVOKE*/ + +/* Open the pty for real. */ + if (( retval = pty_open_ctty ( slave, fd)) < 0 ) { + return PTY_OPEN_SLAVE_OPENFAIL; + } + return pty_initialize_slave (*fd); +} diff --git a/src/util/pty/pty-int.h b/src/util/pty/pty-int.h new file mode 100644 index 000000000..56a7ac6e7 --- /dev/null +++ b/src/util/pty/pty-int.h @@ -0,0 +1,91 @@ +/* Includes needed by libpty*/ +#ifndef __PTY_INT_H__ +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef __SCO__ +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_SYS_LABEL_H +/* only SunOS 4? */ +#include +#include +#include +#endif + +#include + +#ifdef hpux +#include +#endif +#ifdef sysvimp +#include +#endif + +#ifdef HAVE_SYS_SELECT_H +#include +#endif + +#ifdef HAVE_STREAMS +#include +#include +#endif + +#if defined(POSIX_TERMIOS) && !defined(ultrix) +#include +#else +#include +#endif + +#include +#include +#include +#include + + +#ifdef HAVE_STREAMS +/* krlogin doesn't test sys/tty... */ +#ifdef HAVE_SYS_TTY_H +#include +#endif + + + +#ifdef HAVE_SYS_PTYVAR_H +/* Solaris actually uses packet mode, so the real macros are needed too */ +#include +#endif +#endif + +/* Internal functions */ +#ifdef __STDC__ +long ptyint_void_association(void); +long ptyint_open_ctty (char *slave, int *fd); +void ptyint_vhangup(void); +#else /*__STDC__*/ + +long ptyint_void_association(); +void ptyint_vhangup(); +#endif /* __STDC__*/ + +#define __PTY_INT_H__ +#endif diff --git a/src/util/pty/pty_err.et b/src/util/pty/pty_err.et new file mode 100644 index 000000000..a70d75571 --- /dev/null +++ b/src/util/pty/pty_err.et @@ -0,0 +1,41 @@ +# +# $Source$ +# $Author$ +# +# Copyright1995 by the Massachusetts Institute of Technology. +# All Rights Reserved. +# +# +# Permission to use, copy, modify, and +# distribute this software and its documentation for any purpose and +# without fee is hereby granted, provided that the above copyright +# notice appear in all copies and that both that copyright notice and +# this permission notice appear in supporting documentation, and that +# the name of M.I.T. not be used in advertising or publicity pertaining +# to distribution of the software without specific, written prior +# permission. M.I.T. makes no representations about the suitability of +# this software for any purpose. It is provided "as is" without express +# or implied warranty. +# + +# libpty--pty handling error table + +error_table pty + + error_code PTY_GETPTY_STREAMS, "Failed to unlock or grant streams pty." + +error_code PTY_GETPTY_FSTAT, "fstat of master pty failed" + +error_code PTY_GETPTY_NOPTY, "All terminal ports in use" + +error_code PTY_OPEN_SLAVE_OPENFAIL, "Failed to open slave side of pty" +error_code PTY_OPEN_SLAVE_CHMODFAIL, "Failed to chmod slave side of pty" + +error_code PTY_OPEN_SLAVE_NOCTTY, "Unable to set controlling terminal" +error_code PTY_OPEN_SLAVE_CHOWNFAIL, "Failed to chown d;slave side of pty" +error_code PTY_OPEN_SLAVE_LINE_PUSHFAIL, "Call to line_push failed to push streams on slave pty" + +error_code PTY_OPEN_SLAVE_PUSH_FAIL, "Failed to push stream on slave side of pty" + + +error_code PTY_OPEN_SLAVE_REVOKEFAIL, "Failed to revoke slave side of pty" diff --git a/src/util/pty/update_utmp.c b/src/util/pty/update_utmp.c new file mode 100644 index 000000000..c80c4f8c7 --- /dev/null +++ b/src/util/pty/update_utmp.c @@ -0,0 +1,114 @@ +/* + * pty_update_utmp: Update or create a utmp entry + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +long pty_update_utmp (ent, username, line, host) + struct utmp *ent; + char *username, *line, *host; +{ +#ifdef HAVE_SETUTENT + struct utmp ut; +#else + struct stat statb; + int tty; +#endif +#ifdef HAVE_SETUTXENT + struct utmpx utx; +#endif +#ifndef NO_UT_PID + char *tmpx; + char utmp_id[5]; +#endif + int fd; + + strncpy(ent->ut_line, line+sizeof("/dev/")-1, sizeof(ent->ut_line)); + ent->ut_time = time(0); + +#ifndef NO_UT_HOST + if (host) + strncpy(ent->ut_host, host, sizeof(ent->ut_host)); + else + ent->ut_host[0] = '\0'; +#endif + +#ifndef NO_UT_PID + tmpx = line + strlen(line)-1; + if (*(tmpx-1) != '/') tmpx--; /* last two characters, unless it's a / */ + sprintf(utmp_id, "kl%s", tmpx); + strncpy(ent->ut_id, utmp_id, sizeof(ent->ut_id)); + strncpy(ent->ut_user, username, sizeof(ent->ut_user)); +#else + strncpy(ent->ut_name, username, sizeof(ent->ut_name)); +#endif + +#ifdef HAVE_SETUTENT + + utmpname(UTMP_FILE); + setutent(); + pututline(ent); + endutent(); + +#if 0 + /* XXX -- NOT NEEDED ANYMORE */ + + if (ent->ut_type == DEAD_PROCESS) { + if ((fd = open(UTMP_FILE, O_RDWR)) >= 0) { + int cnt = 0; + while(read(fd, (char *)&ut, sizeof(ut)) == sizeof(ut)) { + if (!strncmp(ut.ut_id, ent->ut_id, sizeof(ut.ut_id))) { + (void) memset(ut.ut_host, 0, sizeof(ut.ut_host)); + (void) memset(ut.ut_user, 0, sizeof(ut.ut_user)); + (void) time(&ut.ut_time); + ut.ut_exit.e_exit = ut.ut_pid = 0; + ut.ut_type = EMPTY; + (void) lseek(fd, -sizeof(ut), SEEK_CUR); + (void) write(fd, &ut, sizeof(ut)); + } + cnt++; + } + close(fd); + } + } +#endif + +#ifdef HAVE_SETUTXENT + setutxent(); + getutmpx(ent, &utx); + pututxline(&utx); + endutxent(); +#endif /* HAVE_SETUTXENT */ + +#else /* HAVE_SETUTENT */ + + tty = ttyslot(); + if (tty > 0 && (fd = open(UTMP_FILE, O_WRONLY, 0)) >= 0) { + (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); + (void)write(fd, (char *)ent, sizeof(struct utmp)); + (void)close(fd); + } + +#endif /* HAVE_SETUTENT */ + + return pty_update_wtmp(ent); +} diff --git a/src/util/pty/update_wtmp.c b/src/util/pty/update_wtmp.c new file mode 100644 index 000000000..ce5230a34 --- /dev/null +++ b/src/util/pty/update_wtmp.c @@ -0,0 +1,68 @@ +/* + * pty_update_utmp: Update or create a utmp entry + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +long pty_update_wtmp (ent) + struct utmp *ent; + { + struct utmp ut; + struct stat statb; + int fd; +#ifdef HAVE_SETUTXENT + struct utmpx utx; + + getutmpx(ent, &utx); + updwtmpx(WTMPX_FILE, &utx); +#endif + +#ifdef HAVE_UPDWTMP + updwtmp(WTMP_FILE, ent); +#else /* HAVE_UPDWTMP */ + + if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) >= 0) { + if (!fstat(fd, &statb)) { + (void)memset((char *)&ut, 0, sizeof(ut)); + (void)strncpy(ut.ut_line, ent->ut_line, sizeof(ut.ut_line)); + (void)strncpy(ut.ut_name, ent->ut_name, sizeof(ut.ut_name)); +#ifndef NO_UT_HOST + (void)strncpy(ut.ut_host, ent->ut_host, sizeof(ut.ut_host)); +#endif + (void)time(&ut.ut_time); +#if defined(HAVE_GETUTENT) && defined(USER_PROCESS) + if (ent->ut_name) { + if (!ut.ut_pid) + ut.ut_pid = getpid(); + ut.ut_type = USER_PROCESS; + } else { + ut.ut_type = EMPTY; + } +#endif + if (write(fd, (char *)&ut, sizeof(struct utmp)) != + sizeof(struct utmp)) + (void)ftruncate(fd, statb.st_size); + } + (void)close(fd); + } +#endif /* HAVE_UPDWTMP */ +} diff --git a/src/util/pty/vhangup.c b/src/util/pty/vhangup.c new file mode 100644 index 000000000..8e1c2f6d7 --- /dev/null +++ b/src/util/pty/vhangup.c @@ -0,0 +1,47 @@ +/* + * pty_open_slave: open slave side of terminal, clearing for use. + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +void ptyint_vhangup() +{ + #ifdef POSIX_SIGNALS + struct sigaction sa; + /* Initialize "sa" structure. */ + (void) sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + +#endif + + #ifdef POSIX_SIGNALS + sa.sa_handler = SIG_IGN; + (void) sigaction(SIGHUP, &sa, (struct sigaction *)0); + vhangup(); + sa.sa_handler = SIG_DFL; + (void) sigaction(SIGHUP, &sa, (struct sigaction *)0); +#else + signal(SIGHUP, SIG_IGN); + vhangup(); + signal(SIGHUP, SIG_DFL); +#endif +} diff --git a/src/util/pty/void_assoc.c b/src/util/pty/void_assoc.c new file mode 100644 index 000000000..1f7d7683d --- /dev/null +++ b/src/util/pty/void_assoc.c @@ -0,0 +1,36 @@ +/* + * ptyint_void_association(): Void association with controlling terminal + * and utmp entries. + * + * Copyright 1990 by the Massachusetts Institute of Technology. + * + * + *Permission to use, copy, modify, and + * distribute this software and its documentation for any purpose and + * without fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright notice and + * this permission notice appear in supporting documentation, and that + * the name of M.I.T. not be used in advertising or publicity pertaining + * to distribution of the software without specific, written prior + * permission. M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + */ + + +#include "mit-copyright.h" +#include +#include "libpty.h" +#include "pty-int.h" + +long ptyint_void_association() +{ + int con_fd; + /* Void tty association first */ + if ((con_fd = open("/dev/tty", O_RDWR)) >= 0) { + ioctl(con_fd, TIOCNOTTY, 0); + close(con_fd); + } + return 0; +} -- 2.26.2