2003-09-19 09:52:48 +04:00
|
|
|
/* $NetBSD: sys_term.c,v 1.40 2003/09/19 05:54:46 itojun Exp $ */
|
1996-02-28 23:38:08 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-02-25 06:20:39 +03:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:46:37 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-08 12:45:07 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1996-02-28 23:38:08 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)sys_term.c 8.4+1 (Berkeley) 5/30/95";
|
|
|
|
#else
|
2003-09-19 09:52:48 +04:00
|
|
|
__RCSID("$NetBSD: sys_term.c,v 1.40 2003/09/19 05:54:46 itojun Exp $");
|
1996-02-28 23:38:08 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "telnetd.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
1997-10-08 12:45:07 +04:00
|
|
|
#include <util.h>
|
|
|
|
|
1996-02-24 04:22:12 +03:00
|
|
|
#include <sys/cdefs.h>
|
1994-02-25 06:20:39 +03:00
|
|
|
|
2001-09-02 22:32:35 +04:00
|
|
|
#include <utmp.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
struct utmp wtmp;
|
|
|
|
|
|
|
|
#define SCPYN(a, b) (void) strncpy(a, b, sizeof(a))
|
|
|
|
#define SCMPN(a, b) strncmp(a, b, sizeof(a))
|
|
|
|
|
|
|
|
struct termios termbuf, termbuf2; /* pty control structure */
|
|
|
|
|
1997-10-08 12:45:07 +04:00
|
|
|
void getptyslave __P((void));
|
|
|
|
int cleanopen __P((char *));
|
|
|
|
char **addarg __P((char **, char *));
|
|
|
|
void scrub_env __P((void));
|
|
|
|
int getent __P((char *, char *));
|
2001-07-19 08:57:49 +04:00
|
|
|
char *getstr __P((const char *, char **));
|
1999-08-13 03:05:22 +04:00
|
|
|
#ifdef KRB5
|
|
|
|
extern void kerberos5_cleanup __P((void));
|
|
|
|
#endif
|
1997-10-08 12:45:07 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* init_termbuf()
|
|
|
|
* copy_termbuf(cp)
|
|
|
|
* set_termbuf()
|
|
|
|
*
|
|
|
|
* These three routines are used to get and set the "termbuf" structure
|
|
|
|
* to and from the kernel. init_termbuf() gets the current settings.
|
|
|
|
* copy_termbuf() hands in a new "termbuf" to write to the kernel, and
|
|
|
|
* set_termbuf() writes the structure into the kernel.
|
|
|
|
*/
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
init_termbuf()
|
|
|
|
{
|
|
|
|
(void) tcgetattr(pty, &termbuf);
|
|
|
|
termbuf2 = termbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(LINEMODE) && defined(TIOCPKT_IOCTL)
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
copy_termbuf(cp, len)
|
|
|
|
char *cp;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
if (len > sizeof(termbuf))
|
|
|
|
len = sizeof(termbuf);
|
1996-02-24 04:22:12 +03:00
|
|
|
memmove((char *)&termbuf, cp, len);
|
1993-03-21 12:45:37 +03:00
|
|
|
termbuf2 = termbuf;
|
|
|
|
}
|
|
|
|
#endif /* defined(LINEMODE) && defined(TIOCPKT_IOCTL) */
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
set_termbuf()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Only make the necessary changes.
|
|
|
|
*/
|
1996-02-24 04:22:12 +03:00
|
|
|
if (memcmp((char *)&termbuf, (char *)&termbuf2, sizeof(termbuf)))
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) tcsetattr(pty, TCSANOW, &termbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* spcset(func, valp, valpp)
|
|
|
|
*
|
|
|
|
* This function takes various special characters (func), and
|
|
|
|
* sets *valp to the current value of that character, and
|
|
|
|
* *valpp to point to where in the "termbuf" structure that
|
|
|
|
* value is kept.
|
|
|
|
*
|
|
|
|
* It returns the SLC_ level of support for this function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
spcset(func, valp, valpp)
|
|
|
|
int func;
|
|
|
|
cc_t *valp;
|
|
|
|
cc_t **valpp;
|
|
|
|
{
|
|
|
|
|
|
|
|
#define setval(a, b) *valp = termbuf.c_cc[a]; \
|
|
|
|
*valpp = &termbuf.c_cc[a]; \
|
|
|
|
return(b);
|
|
|
|
#define defval(a) *valp = ((cc_t)a); *valpp = (cc_t *)0; return(SLC_DEFAULT);
|
|
|
|
|
|
|
|
switch(func) {
|
|
|
|
case SLC_EOF:
|
|
|
|
setval(VEOF, SLC_VARIABLE);
|
|
|
|
case SLC_EC:
|
|
|
|
setval(VERASE, SLC_VARIABLE);
|
|
|
|
case SLC_EL:
|
|
|
|
setval(VKILL, SLC_VARIABLE);
|
|
|
|
case SLC_IP:
|
|
|
|
setval(VINTR, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
|
|
|
|
case SLC_ABORT:
|
|
|
|
setval(VQUIT, SLC_VARIABLE|SLC_FLUSHIN|SLC_FLUSHOUT);
|
|
|
|
case SLC_XON:
|
|
|
|
setval(VSTART, SLC_VARIABLE);
|
|
|
|
case SLC_XOFF:
|
|
|
|
setval(VSTOP, SLC_VARIABLE);
|
|
|
|
case SLC_EW:
|
|
|
|
setval(VWERASE, SLC_VARIABLE);
|
|
|
|
case SLC_RP:
|
|
|
|
setval(VREPRINT, SLC_VARIABLE);
|
|
|
|
case SLC_LNEXT:
|
|
|
|
setval(VLNEXT, SLC_VARIABLE);
|
|
|
|
case SLC_AO:
|
|
|
|
setval(VDISCARD, SLC_VARIABLE|SLC_FLUSHOUT);
|
|
|
|
case SLC_SUSP:
|
|
|
|
setval(VSUSP, SLC_VARIABLE|SLC_FLUSHIN);
|
|
|
|
case SLC_FORW1:
|
|
|
|
setval(VEOL, SLC_VARIABLE);
|
|
|
|
case SLC_FORW2:
|
|
|
|
setval(VEOL2, SLC_VARIABLE);
|
|
|
|
case SLC_AYT:
|
|
|
|
setval(VSTATUS, SLC_VARIABLE);
|
|
|
|
|
|
|
|
case SLC_BRK:
|
|
|
|
case SLC_SYNCH:
|
|
|
|
case SLC_EOR:
|
|
|
|
defval(0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
*valp = 0;
|
|
|
|
*valpp = 0;
|
|
|
|
return(SLC_NOSUPPORT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getpty()
|
|
|
|
*
|
|
|
|
* Allocate a pty. As a side effect, the external character
|
|
|
|
* array "line" contains the name of the slave side.
|
|
|
|
*
|
|
|
|
* Returns the file descriptor of the opened pty.
|
|
|
|
*/
|
|
|
|
#ifndef __GNUC__
|
2000-11-19 23:17:39 +03:00
|
|
|
char *line = NULL16STR;
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
2000-11-19 23:17:39 +03:00
|
|
|
static char Xline[] = NULL16STR;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *line = Xline;
|
|
|
|
#endif
|
|
|
|
|
1998-08-05 04:15:25 +04:00
|
|
|
|
|
|
|
static int ptyslavefd; /* for cleanopen() */
|
|
|
|
|
|
|
|
int
|
|
|
|
getpty(ptynum)
|
|
|
|
int *ptynum;
|
|
|
|
{
|
|
|
|
int ptyfd;
|
|
|
|
|
|
|
|
ptyfd = openpty(ptynum, &ptyslavefd, line, NULL, NULL);
|
|
|
|
if (ptyfd == 0)
|
|
|
|
return *ptynum;
|
|
|
|
ptyslavefd = -1;
|
|
|
|
return (-1);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#ifdef LINEMODE
|
|
|
|
/*
|
|
|
|
* tty_flowmode() Find out if flow control is enabled or disabled.
|
|
|
|
* tty_linemode() Find out if linemode (external processing) is enabled.
|
|
|
|
* tty_setlinemod(on) Turn on/off linemode.
|
|
|
|
* tty_isecho() Find out if echoing is turned on.
|
|
|
|
* tty_setecho(on) Enable/disable character echoing.
|
|
|
|
* tty_israw() Find out if terminal is in RAW mode.
|
|
|
|
* tty_binaryin(on) Turn on/off BINARY on input.
|
|
|
|
* tty_binaryout(on) Turn on/off BINARY on output.
|
|
|
|
* tty_isediting() Find out if line editing is enabled.
|
|
|
|
* tty_istrapsig() Find out if signal trapping is enabled.
|
|
|
|
* tty_setedit(on) Turn on/off line editing.
|
|
|
|
* tty_setsig(on) Turn on/off signal trapping.
|
|
|
|
* tty_issofttab() Find out if tab expansion is enabled.
|
|
|
|
* tty_setsofttab(on) Turn on/off soft tab expansion.
|
|
|
|
* tty_islitecho() Find out if typed control chars are echoed literally
|
|
|
|
* tty_setlitecho() Turn on/off literal echo of control chars
|
|
|
|
* tty_tspeed(val) Set transmit speed to val.
|
|
|
|
* tty_rspeed(val) Set receive speed to val.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_linemode()
|
|
|
|
{
|
|
|
|
return(termbuf.c_lflag & EXTPROC);
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_setlinemode(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
set_termbuf();
|
|
|
|
(void) ioctl(pty, TIOCEXT, (char *)&on);
|
|
|
|
init_termbuf();
|
|
|
|
}
|
1994-02-25 06:20:39 +03:00
|
|
|
#endif /* LINEMODE */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_isecho()
|
|
|
|
{
|
|
|
|
return (termbuf.c_lflag & ECHO);
|
|
|
|
}
|
1994-02-25 06:20:39 +03:00
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1994-02-25 06:20:39 +03:00
|
|
|
tty_flowmode()
|
|
|
|
{
|
|
|
|
return((termbuf.c_iflag & IXON) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1994-02-25 06:20:39 +03:00
|
|
|
tty_restartany()
|
|
|
|
{
|
|
|
|
return((termbuf.c_iflag & IXANY) ? 1 : 0);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_setecho(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
termbuf.c_lflag |= ECHO;
|
|
|
|
else
|
|
|
|
termbuf.c_lflag &= ~ECHO;
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_israw()
|
|
|
|
{
|
|
|
|
return(!(termbuf.c_lflag & ICANON));
|
1994-02-25 06:20:39 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_binaryin(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
if (on) {
|
|
|
|
termbuf.c_iflag &= ~ISTRIP;
|
|
|
|
} else {
|
|
|
|
termbuf.c_iflag |= ISTRIP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_binaryout(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
if (on) {
|
|
|
|
termbuf.c_cflag &= ~(CSIZE|PARENB);
|
|
|
|
termbuf.c_cflag |= CS8;
|
|
|
|
termbuf.c_oflag &= ~OPOST;
|
|
|
|
} else {
|
|
|
|
termbuf.c_cflag &= ~CSIZE;
|
|
|
|
termbuf.c_cflag |= CS7|PARENB;
|
|
|
|
termbuf.c_oflag |= OPOST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_isbinaryin()
|
|
|
|
{
|
|
|
|
return(!(termbuf.c_iflag & ISTRIP));
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_isbinaryout()
|
|
|
|
{
|
|
|
|
return(!(termbuf.c_oflag&OPOST));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LINEMODE
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_isediting()
|
|
|
|
{
|
|
|
|
return(termbuf.c_lflag & ICANON);
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_istrapsig()
|
|
|
|
{
|
|
|
|
return(termbuf.c_lflag & ISIG);
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_setedit(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
termbuf.c_lflag |= ICANON;
|
|
|
|
else
|
|
|
|
termbuf.c_lflag &= ~ICANON;
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_setsig(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
termbuf.c_lflag |= ISIG;
|
|
|
|
else
|
|
|
|
termbuf.c_lflag &= ~ISIG;
|
|
|
|
}
|
|
|
|
#endif /* LINEMODE */
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_issofttab()
|
|
|
|
{
|
|
|
|
# ifdef OXTABS
|
|
|
|
return (termbuf.c_oflag & OXTABS);
|
|
|
|
# endif
|
|
|
|
# ifdef TABDLY
|
|
|
|
return ((termbuf.c_oflag & TABDLY) == TAB3);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_setsofttab(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
if (on) {
|
|
|
|
# ifdef OXTABS
|
|
|
|
termbuf.c_oflag |= OXTABS;
|
|
|
|
# endif
|
|
|
|
# ifdef TABDLY
|
|
|
|
termbuf.c_oflag &= ~TABDLY;
|
|
|
|
termbuf.c_oflag |= TAB3;
|
|
|
|
# endif
|
|
|
|
} else {
|
|
|
|
# ifdef OXTABS
|
|
|
|
termbuf.c_oflag &= ~OXTABS;
|
|
|
|
# endif
|
|
|
|
# ifdef TABDLY
|
|
|
|
termbuf.c_oflag &= ~TABDLY;
|
|
|
|
termbuf.c_oflag |= TAB0;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_islitecho()
|
|
|
|
{
|
|
|
|
# ifdef ECHOCTL
|
|
|
|
return (!(termbuf.c_lflag & ECHOCTL));
|
|
|
|
# endif
|
|
|
|
# ifdef TCTLECH
|
|
|
|
return (!(termbuf.c_lflag & TCTLECH));
|
|
|
|
# endif
|
|
|
|
# if !defined(ECHOCTL) && !defined(TCTLECH)
|
|
|
|
return (0); /* assumes ctl chars are echoed '^x' */
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_setlitecho(on)
|
|
|
|
int on;
|
|
|
|
{
|
|
|
|
# ifdef ECHOCTL
|
|
|
|
if (on)
|
|
|
|
termbuf.c_lflag &= ~ECHOCTL;
|
|
|
|
else
|
|
|
|
termbuf.c_lflag |= ECHOCTL;
|
|
|
|
# endif
|
|
|
|
# ifdef TCTLECH
|
|
|
|
if (on)
|
|
|
|
termbuf.c_lflag &= ~TCTLECH;
|
|
|
|
else
|
|
|
|
termbuf.c_lflag |= TCTLECH;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_iscrnl()
|
|
|
|
{
|
|
|
|
return (termbuf.c_iflag & ICRNL);
|
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_tspeed(val)
|
|
|
|
int val;
|
|
|
|
{
|
1996-02-24 04:22:12 +03:00
|
|
|
cfsetospeed(&termbuf, val);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
tty_rspeed(val)
|
|
|
|
int val;
|
|
|
|
{
|
1996-02-24 04:22:12 +03:00
|
|
|
cfsetispeed(&termbuf, val);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getptyslave()
|
|
|
|
*
|
|
|
|
* Open the slave side of the pty, and do any initialization
|
|
|
|
* that is necessary. The return value is a file descriptor
|
|
|
|
* for the slave side.
|
|
|
|
*/
|
2001-02-05 01:32:15 +03:00
|
|
|
extern int def_tspeed, def_rspeed;
|
|
|
|
extern int def_row, def_col;
|
|
|
|
|
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
getptyslave()
|
|
|
|
{
|
|
|
|
register int t = -1;
|
|
|
|
|
2001-09-02 22:32:35 +04:00
|
|
|
#ifdef LINEMODE
|
1993-03-21 12:45:37 +03:00
|
|
|
int waslm;
|
2001-09-02 22:32:35 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
struct winsize ws;
|
|
|
|
/*
|
|
|
|
* Opening the slave side may cause initilization of the
|
|
|
|
* kernel tty structure. We need remember the state of
|
|
|
|
* if linemode was turned on
|
|
|
|
* terminal window size
|
|
|
|
* terminal speed
|
|
|
|
* so that we can re-set them if we need to.
|
|
|
|
*/
|
2001-09-02 22:32:35 +04:00
|
|
|
#ifdef LINEMODE
|
1993-03-21 12:45:37 +03:00
|
|
|
waslm = tty_linemode();
|
2001-09-02 22:32:35 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure that we don't have a controlling tty, and
|
|
|
|
* that we are the session (process group) leader.
|
|
|
|
*/
|
|
|
|
t = open(_PATH_TTY, O_RDWR);
|
|
|
|
if (t >= 0) {
|
|
|
|
(void) ioctl(t, TIOCNOTTY, (char *)0);
|
|
|
|
(void) close(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t = cleanopen(line);
|
|
|
|
if (t < 0)
|
|
|
|
fatalperror(net, line);
|
|
|
|
|
1994-02-25 06:20:39 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* set up the tty modes as we like them to be.
|
|
|
|
*/
|
|
|
|
init_termbuf();
|
|
|
|
if (def_row || def_col) {
|
1996-02-24 04:22:12 +03:00
|
|
|
memset((char *)&ws, 0, sizeof(ws));
|
1993-03-21 12:45:37 +03:00
|
|
|
ws.ws_col = def_col;
|
|
|
|
ws.ws_row = def_row;
|
|
|
|
(void)ioctl(t, TIOCSWINSZ, (char *)&ws);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Settings for sgtty based systems
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Settings for all other termios/termio based
|
|
|
|
* systems, other than 4.4BSD. In 4.4BSD the
|
|
|
|
* kernel does the initial terminal setup.
|
|
|
|
*/
|
|
|
|
tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);
|
|
|
|
tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);
|
2001-09-02 22:32:35 +04:00
|
|
|
#ifdef LINEMODE
|
1993-03-21 12:45:37 +03:00
|
|
|
if (waslm)
|
|
|
|
tty_setlinemode(1);
|
2001-09-02 22:32:35 +04:00
|
|
|
#endif /* LINEMODE */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the tty modes, and make this our controlling tty.
|
|
|
|
*/
|
|
|
|
set_termbuf();
|
|
|
|
if (login_tty(t) == -1)
|
|
|
|
fatalperror(net, "login_tty");
|
|
|
|
if (net > 2)
|
|
|
|
(void) close(net);
|
1994-02-25 06:20:39 +03:00
|
|
|
if (pty > 2) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) close(pty);
|
1994-02-25 06:20:39 +03:00
|
|
|
pty = -1;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the specified slave side of the pty,
|
|
|
|
* making sure that we have a clean tty.
|
|
|
|
*/
|
2003-07-14 19:55:53 +04:00
|
|
|
int
|
2001-07-19 08:57:49 +04:00
|
|
|
cleanopen(ttyline)
|
|
|
|
char *ttyline;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1998-08-05 04:15:25 +04:00
|
|
|
return ptyslavefd;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* startslave(host)
|
|
|
|
*
|
|
|
|
* Given a hostname, do whatever
|
|
|
|
* is necessary to startup the login process on the slave side of the pty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
startslave(host, autologin, autoname)
|
|
|
|
char *host;
|
|
|
|
int autologin;
|
|
|
|
char *autoname;
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
#ifdef AUTHENTICATION
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!autoname || !autoname[0])
|
|
|
|
autologin = 0;
|
|
|
|
|
|
|
|
if (autologin < auth_level) {
|
|
|
|
fatal(net, "Authorization failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
if ((i = fork()) < 0)
|
|
|
|
fatalperror(net, "fork");
|
|
|
|
if (i) {
|
|
|
|
} else {
|
1997-10-08 12:45:07 +04:00
|
|
|
getptyslave();
|
1993-03-21 12:45:37 +03:00
|
|
|
start_login(host, autologin, autoname);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *envinit[3];
|
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
init_env()
|
|
|
|
{
|
|
|
|
char **envp;
|
|
|
|
|
|
|
|
envp = envinit;
|
1997-10-08 12:45:07 +04:00
|
|
|
if ((*envp = getenv("TZ")))
|
1993-03-21 12:45:37 +03:00
|
|
|
*envp++ -= 3;
|
|
|
|
*envp = 0;
|
|
|
|
environ = envinit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start_login(host)
|
|
|
|
*
|
|
|
|
* Assuming that we are now running as a child processes, this
|
|
|
|
* function will turn us into the login process.
|
|
|
|
*/
|
2001-02-05 01:32:15 +03:00
|
|
|
extern char *gettyname;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-07-14 19:55:53 +04:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
start_login(host, autologin, name)
|
|
|
|
char *host;
|
|
|
|
int autologin;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
register char **argv;
|
1996-03-20 07:25:53 +03:00
|
|
|
#define TABBUFSIZ 512
|
|
|
|
char defent[TABBUFSIZ];
|
|
|
|
char defstrs[TABBUFSIZ];
|
|
|
|
#undef TABBUFSIZ
|
2001-07-19 08:57:49 +04:00
|
|
|
const char *loginprog = NULL;
|
1994-02-25 06:20:39 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1996-02-24 04:22:12 +03:00
|
|
|
scrub_env();
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* -h : pass on name of host.
|
|
|
|
* WARNING: -h is accepted by login if and only if
|
|
|
|
* getuid() == 0.
|
|
|
|
* -p : don't clobber the environment (so terminal type stays set).
|
|
|
|
*
|
|
|
|
* -f : force this login, he has already been authenticated
|
|
|
|
*/
|
|
|
|
argv = addarg(0, "login");
|
1994-02-25 06:20:39 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
argv = addarg(argv, "-h");
|
|
|
|
argv = addarg(argv, host);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
argv = addarg(argv, "-p");
|
1996-02-24 04:22:12 +03:00
|
|
|
#ifdef LINEMODE
|
|
|
|
/*
|
|
|
|
* Set the environment variable "LINEMODE" to either
|
|
|
|
* "real" or "kludge" if we are operating in either
|
|
|
|
* real or kludge linemode.
|
|
|
|
*/
|
|
|
|
if (lmodetype == REAL_LINEMODE)
|
|
|
|
setenv("LINEMODE", "real", 1);
|
|
|
|
# ifdef KLUDGELINEMODE
|
|
|
|
else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
|
|
|
|
setenv("LINEMODE", "kludge", 1);
|
|
|
|
# endif
|
|
|
|
#endif
|
2003-07-14 19:55:53 +04:00
|
|
|
#ifdef SECURELOGIN
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* don't worry about the -f that might get sent.
|
|
|
|
* A -s is supposed to override it anyhow.
|
|
|
|
*/
|
1999-02-12 08:30:11 +03:00
|
|
|
if (require_secure_login)
|
1993-03-21 12:45:37 +03:00
|
|
|
argv = addarg(argv, "-s");
|
|
|
|
#endif
|
2003-07-14 19:55:53 +04:00
|
|
|
#ifdef AUTHENTICATION
|
1993-03-21 12:45:37 +03:00
|
|
|
if (auth_level >= 0 && autologin == AUTH_VALID) {
|
|
|
|
argv = addarg(argv, "-f");
|
1996-02-08 09:05:31 +03:00
|
|
|
argv = addarg(argv, "--");
|
1994-02-25 06:20:39 +03:00
|
|
|
argv = addarg(argv, name);
|
1993-03-21 12:45:37 +03:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (getenv("USER")) {
|
1996-02-08 09:05:31 +03:00
|
|
|
argv = addarg(argv, "--");
|
1993-03-21 12:45:37 +03:00
|
|
|
argv = addarg(argv, getenv("USER"));
|
1994-02-25 06:20:39 +03:00
|
|
|
/*
|
|
|
|
* Assume that login will set the USER variable
|
|
|
|
* correctly. For SysV systems, this means that
|
|
|
|
* USER will no longer be set, just LOGNAME by
|
|
|
|
* login. (The problem is that if the auto-login
|
|
|
|
* fails, and the user then specifies a different
|
|
|
|
* account name, he can get logged in with both
|
|
|
|
* LOGNAME and USER in his environment, but the
|
|
|
|
* USER value will be wrong.
|
|
|
|
*/
|
|
|
|
unsetenv("USER");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-03-20 07:25:53 +03:00
|
|
|
if (getent(defent, gettyname) == 1) {
|
|
|
|
char *cp = defstrs;
|
|
|
|
|
|
|
|
loginprog = getstr("lo", &cp);
|
|
|
|
}
|
|
|
|
if (loginprog == NULL)
|
|
|
|
loginprog = _PATH_LOGIN;
|
1993-03-21 12:45:37 +03:00
|
|
|
closelog();
|
1996-02-24 04:22:12 +03:00
|
|
|
/*
|
|
|
|
* This sleep(1) is in here so that telnetd can
|
|
|
|
* finish up with the tty. There's a race condition
|
|
|
|
* the login banner message gets lost...
|
|
|
|
*/
|
|
|
|
sleep(1);
|
1996-03-20 07:25:53 +03:00
|
|
|
execv(loginprog, argv);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2001-08-20 15:13:17 +04:00
|
|
|
syslog(LOG_ERR, "%s: %m", loginprog);
|
1996-03-20 07:25:53 +03:00
|
|
|
fatalperror(net, loginprog);
|
1993-03-21 12:45:37 +03:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
|
|
|
addarg(argv, val)
|
|
|
|
register char **argv;
|
|
|
|
register char *val;
|
|
|
|
{
|
|
|
|
register char **cpp;
|
2003-09-19 09:52:48 +04:00
|
|
|
char **nargv;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (argv == NULL) {
|
|
|
|
/*
|
|
|
|
* 10 entries, a leading length, and a null
|
|
|
|
*/
|
|
|
|
argv = (char **)malloc(sizeof(*argv) * 12);
|
|
|
|
if (argv == NULL)
|
|
|
|
return(NULL);
|
|
|
|
*argv++ = (char *)10;
|
|
|
|
*argv = (char *)0;
|
|
|
|
}
|
|
|
|
for (cpp = argv; *cpp; cpp++)
|
|
|
|
;
|
1996-02-24 22:13:37 +03:00
|
|
|
if (cpp == &argv[(long)argv[-1]]) {
|
1993-03-21 12:45:37 +03:00
|
|
|
--argv;
|
2003-09-19 09:52:48 +04:00
|
|
|
nargv = (char **)realloc(argv,
|
|
|
|
sizeof(*argv) * ((long)(*argv) + 10 + 2));
|
1999-12-31 15:42:35 +03:00
|
|
|
if (argv == NULL) {
|
|
|
|
fatal(net, "not enough memory");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2003-09-19 09:52:48 +04:00
|
|
|
argv = nargv;
|
|
|
|
*argv = (char *)((long)(*argv) + 10);
|
1993-03-21 12:45:37 +03:00
|
|
|
argv++;
|
1996-02-24 22:13:37 +03:00
|
|
|
cpp = &argv[(long)argv[-1] - 10];
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
*cpp++ = val;
|
|
|
|
*cpp = 0;
|
|
|
|
return(argv);
|
|
|
|
}
|
|
|
|
|
1996-02-24 04:22:12 +03:00
|
|
|
/*
|
|
|
|
* scrub_env()
|
|
|
|
*
|
2000-12-09 03:50:21 +03:00
|
|
|
* We only accept the environment variables listed below.
|
1996-02-24 04:22:12 +03:00
|
|
|
*/
|
2000-12-09 03:50:21 +03:00
|
|
|
|
1997-10-08 12:45:07 +04:00
|
|
|
void
|
1996-02-24 04:22:12 +03:00
|
|
|
scrub_env()
|
|
|
|
{
|
2000-12-09 03:50:21 +03:00
|
|
|
static const char *reject[] = {
|
|
|
|
"TERMCAP=/",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2001-08-20 15:34:01 +04:00
|
|
|
static const char *acceptstr[] = {
|
2000-12-09 03:50:21 +03:00
|
|
|
"XAUTH=", "XAUTHORITY=", "DISPLAY=",
|
|
|
|
"TERM=",
|
|
|
|
"EDITOR=",
|
|
|
|
"PAGER=",
|
|
|
|
"LOGNAME=",
|
|
|
|
"POSIXLY_CORRECT=",
|
|
|
|
"TERMCAP=",
|
|
|
|
"PRINTER=",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
char **cpp, **cpp2;
|
|
|
|
const char **p;
|
1996-02-24 04:22:12 +03:00
|
|
|
|
|
|
|
for (cpp2 = cpp = environ; *cpp; cpp++) {
|
2000-12-09 03:50:21 +03:00
|
|
|
int reject_it = 0;
|
|
|
|
|
|
|
|
for(p = reject; *p; p++)
|
|
|
|
if(strncmp(*cpp, *p, strlen(*p)) == 0) {
|
|
|
|
reject_it = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (reject_it)
|
|
|
|
continue;
|
|
|
|
|
2001-08-20 15:34:01 +04:00
|
|
|
for(p = acceptstr; *p; p++)
|
2000-12-09 03:50:21 +03:00
|
|
|
if(strncmp(*cpp, *p, strlen(*p)) == 0)
|
|
|
|
break;
|
|
|
|
if(*p != NULL)
|
1996-02-24 04:22:12 +03:00
|
|
|
*cpp2++ = *cpp;
|
|
|
|
}
|
2000-12-09 03:50:21 +03:00
|
|
|
*cpp2 = NULL;
|
1996-02-24 04:22:12 +03:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* cleanup()
|
|
|
|
*
|
|
|
|
* This is the routine to call when we are all through, to
|
|
|
|
* clean up anything that needs to be cleaned up.
|
|
|
|
*/
|
2003-07-14 19:55:53 +04:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
cleanup(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
1998-08-29 21:31:55 +04:00
|
|
|
char *p, c;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
p = line + sizeof("/dev/") - 1;
|
2002-08-20 17:12:00 +04:00
|
|
|
#ifdef SUPPORT_UTMP
|
1993-03-21 12:45:37 +03:00
|
|
|
if (logout(p))
|
|
|
|
logwtmp(p, "", "");
|
2002-08-20 17:12:00 +04:00
|
|
|
#endif
|
|
|
|
#ifdef SUPPORT_UTMPX
|
|
|
|
if (logoutx(p, 0, DEAD_PROCESS))
|
|
|
|
logwtmpx(p, "", "", 0, DEAD_PROCESS);
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)chmod(line, 0666);
|
|
|
|
(void)chown(line, 0, 0);
|
1998-08-29 21:31:55 +04:00
|
|
|
c = *p; *p = 'p';
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)chmod(line, 0666);
|
|
|
|
(void)chown(line, 0, 0);
|
1998-08-29 21:31:55 +04:00
|
|
|
*p = c;
|
|
|
|
if (ttyaction(line, "telnetd", "root"))
|
|
|
|
syslog(LOG_ERR, "%s: ttyaction failed", line);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) shutdown(net, 2);
|
|
|
|
exit(1);
|
1996-02-24 04:22:12 +03:00
|
|
|
}
|