Home | History | Annotate | Line # | Download | only in getty
main.c revision 1.67
      1  1.67  riastrad /*	$NetBSD: main.c,v 1.67 2020/02/26 15:44:57 riastradh Exp $	*/
      2  1.18   thorpej 
      3   1.1       cgd /*-
      4   1.8        pk  * Copyright (c) 1980, 1993
      5   1.8        pk  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.45       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.22     mikel #include <sys/cdefs.h>
     33  1.22     mikel 
     34   1.1       cgd #ifndef lint
     35  1.56     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
     36  1.56     lukem  The Regents of the University of California.  All rights reserved.");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40  1.18   thorpej #if 0
     41  1.18   thorpej static char sccsid[] = "from: @(#)main.c	8.1 (Berkeley) 6/20/93";
     42  1.18   thorpej #else
     43  1.67  riastrad __RCSID("$NetBSD: main.c,v 1.67 2020/02/26 15:44:57 riastradh Exp $");
     44  1.18   thorpej #endif
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47   1.1       cgd #include <sys/param.h>
     48   1.7        pk #include <sys/ioctl.h>
     49   1.8        pk #include <sys/resource.h>
     50  1.66  christos #include <sys/stat.h>
     51   1.3   mycroft #include <sys/utsname.h>
     52  1.21     mikel 
     53  1.54  dholland #include <ctype.h>
     54  1.15       cgd #include <errno.h>
     55   1.1       cgd #include <fcntl.h>
     56  1.54  dholland #include <limits.h>
     57  1.23       cjs #include <pwd.h>
     58   1.1       cgd #include <setjmp.h>
     59   1.8        pk #include <signal.h>
     60  1.60  christos #include <stdio.h>
     61   1.8        pk #include <stdlib.h>
     62   1.8        pk #include <string.h>
     63   1.1       cgd #include <syslog.h>
     64  1.58       roy #include <term.h>
     65  1.60  christos #include <termios.h>
     66   1.8        pk #include <time.h>
     67  1.54  dholland #include <ttyent.h>
     68   1.1       cgd #include <unistd.h>
     69  1.17       jtc #include <util.h>
     70   1.8        pk 
     71   1.1       cgd #include "gettytab.h"
     72   1.1       cgd #include "pathnames.h"
     73   1.8        pk #include "extern.h"
     74   1.8        pk 
     75  1.37  christos extern char editedhost[];
     76  1.23       cjs 
     77   1.8        pk /*
     78   1.8        pk  * Set the amount of running time that getty should accumulate
     79   1.8        pk  * before deciding that something is wrong and exit.
     80   1.8        pk  */
     81   1.8        pk #define GETTY_TIMEOUT	60 /* seconds */
     82   1.1       cgd 
     83  1.27    tsarna /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
     84  1.27    tsarna 
     85  1.27    tsarna #define PPP_FRAME           0x7e  /* PPP Framing character */
     86  1.27    tsarna #define PPP_STATION         0xff  /* "All Station" character */
     87  1.27    tsarna #define PPP_ESCAPE          0x7d  /* Escape Character */
     88  1.27    tsarna #define PPP_CONTROL         0x03  /* PPP Control Field */
     89  1.27    tsarna #define PPP_CONTROL_ESCAPED 0x23  /* PPP Control Field, escaped */
     90  1.27    tsarna #define PPP_LCP_HI          0xc0  /* LCP protocol - high byte */
     91  1.27    tsarna #define PPP_LCP_LOW         0x21  /* LCP protocol - low byte */
     92  1.27    tsarna 
     93   1.8        pk struct termios tmode, omode;
     94   1.1       cgd 
     95  1.55  dholland int crmod, digit_or_punc, lower, upper;
     96   1.1       cgd 
     97  1.26       mrg char	hostname[MAXHOSTNAMELEN + 1];
     98   1.3   mycroft struct	utsname kerninfo;
     99  1.29  drochner char	name[LOGIN_NAME_MAX];
    100   1.1       cgd char	dev[] = _PATH_DEV;
    101   1.1       cgd char	ttyn[32];
    102  1.67  riastrad uid_t	ttyowner = 0;
    103  1.35  sommerfe char	*rawttyn;
    104   1.1       cgd 
    105   1.1       cgd #define	OBUFSIZ		128
    106   1.1       cgd #define	TABBUFSIZ	512
    107   1.1       cgd 
    108   1.1       cgd char	defent[TABBUFSIZ];
    109   1.1       cgd char	tabent[TABBUFSIZ];
    110   1.1       cgd 
    111   1.1       cgd char	*env[128];
    112   1.1       cgd 
    113  1.51  christos const unsigned char partab[] = {
    114   1.1       cgd 	0001,0201,0201,0001,0201,0001,0001,0201,
    115   1.1       cgd 	0202,0004,0003,0205,0005,0206,0201,0001,
    116   1.1       cgd 	0201,0001,0001,0201,0001,0201,0201,0001,
    117   1.1       cgd 	0001,0201,0201,0001,0201,0001,0001,0201,
    118   1.1       cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    119   1.1       cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    120   1.1       cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    121   1.1       cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    122   1.1       cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    123   1.1       cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    124   1.1       cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    125   1.1       cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    126   1.1       cgd 	0000,0200,0200,0000,0200,0000,0000,0200,
    127   1.1       cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    128   1.1       cgd 	0200,0000,0000,0200,0000,0200,0200,0000,
    129   1.1       cgd 	0000,0200,0200,0000,0200,0000,0000,0201
    130   1.1       cgd };
    131   1.1       cgd 
    132   1.7        pk #define	ERASE	tmode.c_cc[VERASE]
    133   1.7        pk #define	KILL	tmode.c_cc[VKILL]
    134   1.7        pk #define	EOT	tmode.c_cc[VEOF]
    135   1.1       cgd 
    136  1.41   thorpej static void	clearscreen(void);
    137  1.22     mikel 
    138  1.62  dholland sigjmp_buf timeout;
    139   1.1       cgd 
    140  1.64     joerg __dead static void
    141  1.51  christos /*ARGSUSED*/
    142  1.41   thorpej dingdong(int signo)
    143   1.1       cgd {
    144   1.1       cgd 
    145  1.51  christos 	(void)alarm(0);
    146  1.51  christos 	(void)signal(SIGALRM, SIG_DFL);
    147  1.62  dholland 	siglongjmp(timeout, 1);
    148   1.1       cgd }
    149   1.1       cgd 
    150  1.62  dholland sigjmp_buf intrupt;
    151   1.1       cgd 
    152  1.64     joerg __dead static void
    153  1.51  christos /*ARGSUSED*/
    154  1.41   thorpej interrupt(int signo)
    155   1.1       cgd {
    156   1.1       cgd 
    157  1.51  christos 	(void)signal(SIGINT, interrupt);
    158  1.62  dholland 	siglongjmp(intrupt, 1);
    159   1.1       cgd }
    160   1.1       cgd 
    161   1.8        pk /*
    162   1.8        pk  * Action to take when getty is running too long.
    163   1.8        pk  */
    164  1.64     joerg __dead static void
    165  1.51  christos /*ARGSUSED*/
    166  1.41   thorpej timeoverrun(int signo)
    167   1.8        pk {
    168   1.8        pk 
    169  1.27    tsarna 	syslog(LOG_ERR, "getty exiting due to excessive running time");
    170   1.8        pk 	exit(1);
    171   1.8        pk }
    172   1.8        pk 
    173  1.41   thorpej static int	getname(void);
    174  1.41   thorpej static void	oflush(void);
    175  1.41   thorpej static void	prompt(void);
    176  1.59       roy static int	putchr(int);
    177  1.41   thorpej static void	putf(const char *);
    178  1.41   thorpej static void	xputs(const char *);
    179   1.8        pk 
    180  1.59       roy #define putpad(s) tputs(s, 1, putchr)
    181  1.59       roy 
    182   1.8        pk int
    183  1.51  christos main(int argc, char *argv[], char *envp[])
    184   1.1       cgd {
    185  1.65  christos 	int repcnt = 0, failopenlogged = 0;
    186  1.65  christos 	volatile int first_time = 1;
    187   1.8        pk 	struct rlimit limit;
    188  1.53     isaki 	int rval;
    189  1.63    martin 	/* this is used past the siglongjmp, so make sure it is not cached
    190  1.63    martin 	   in registers that might become invalid. */
    191  1.63    martin 	const char * volatile tname = "default";
    192   1.1       cgd 
    193  1.51  christos 	(void)signal(SIGINT, SIG_IGN);
    194  1.36     lukem 	openlog("getty", LOG_PID, LOG_AUTH);
    195  1.51  christos 	(void)gethostname(hostname, sizeof(hostname));
    196  1.26       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    197   1.1       cgd 	if (hostname[0] == '\0')
    198  1.51  christos 		(void)strlcpy(hostname, "Amnesiac", sizeof(hostname));
    199  1.51  christos 	(void)uname(&kerninfo);
    200   1.8        pk 
    201   1.8        pk 	/*
    202   1.8        pk 	 * Limit running time to deal with broken or dead lines.
    203   1.8        pk 	 */
    204   1.8        pk 	(void)signal(SIGXCPU, timeoverrun);
    205   1.8        pk 	limit.rlim_max = RLIM_INFINITY;
    206   1.8        pk 	limit.rlim_cur = GETTY_TIMEOUT;
    207   1.8        pk 	(void)setrlimit(RLIMIT_CPU, &limit);
    208   1.8        pk 
    209   1.1       cgd 	/*
    210   1.1       cgd 	 * The following is a work around for vhangup interactions
    211   1.1       cgd 	 * which cause great problems getting window systems started.
    212   1.1       cgd 	 * If the tty line is "-", we do the old style getty presuming
    213  1.52   hubertf 	 * that the file descriptors are already set up for us.
    214   1.1       cgd 	 * J. Gettys - MIT Project Athena.
    215   1.1       cgd 	 */
    216  1.20       tls 	if (argc <= 2 || strcmp(argv[2], "-") == 0) {
    217  1.51  christos 		(void)strlcpy(ttyn, ttyname(0), sizeof(ttyn));
    218  1.20       tls 	}
    219   1.1       cgd 	else {
    220  1.53     isaki 		int i;
    221   1.1       cgd 
    222  1.53     isaki 		rawttyn = argv[2];
    223  1.53     isaki 		(void)strlcpy(ttyn, dev, sizeof(ttyn));
    224  1.53     isaki 		(void)strlcat(ttyn, argv[2], sizeof(ttyn));
    225  1.53     isaki 		if (strcmp(argv[0], "+") != 0) {
    226  1.53     isaki 			(void)chown(ttyn, ttyowner, 0);
    227  1.53     isaki 			(void)chmod(ttyn, 0600);
    228  1.53     isaki 			(void)revoke(ttyn);
    229  1.53     isaki 			if (ttyaction(ttyn, "getty", "root"))
    230  1.53     isaki 				syslog(LOG_WARNING, "%s: ttyaction failed",
    231  1.51  christos 					ttyn);
    232  1.53     isaki 			/*
    233  1.53     isaki 			 * Delay the open so DTR stays down long enough
    234  1.53     isaki 			 * to be detected.
    235  1.53     isaki 			 */
    236  1.53     isaki 			(void)sleep(2);
    237  1.53     isaki 			while ((i = open(ttyn, O_RDWR)) == -1) {
    238  1.53     isaki 				if ((repcnt % 10 == 0) &&
    239  1.53     isaki 				    (errno != ENXIO || !failopenlogged)) {
    240  1.53     isaki 					syslog(LOG_WARNING, "%s: %m", ttyn);
    241  1.53     isaki 					closelog();
    242  1.53     isaki 					failopenlogged = 1;
    243  1.53     isaki 				}
    244  1.53     isaki 				repcnt++;
    245  1.53     isaki 				(void)sleep(60);
    246  1.53     isaki 			}
    247  1.53     isaki 			(void)login_tty(i);
    248  1.23       cjs 		}
    249   1.1       cgd 	}
    250   1.1       cgd 
    251   1.7        pk 	/* Start with default tty settings */
    252   1.7        pk 	if (tcgetattr(0, &tmode) < 0) {
    253   1.7        pk 		syslog(LOG_ERR, "%s: %m", ttyn);
    254   1.7        pk 		exit(1);
    255   1.7        pk 	}
    256   1.8        pk 	omode = tmode;
    257   1.7        pk 
    258   1.5       cgd 	gettable("default", defent);
    259   1.1       cgd 	gendefaults();
    260   1.1       cgd 	if (argc > 1)
    261   1.1       cgd 		tname = argv[1];
    262   1.1       cgd 	for (;;) {
    263   1.8        pk 		int off;
    264   1.1       cgd 
    265  1.48     lukem 		rval = 0;
    266   1.5       cgd 		gettable(tname, tabent);
    267   1.1       cgd 		if (OPset || EPset || APset)
    268   1.1       cgd 			APset++, OPset++, EPset++;
    269   1.1       cgd 		setdefaults();
    270   1.8        pk 		off = 0;
    271  1.12        pk 		(void)tcflush(0, TCIOFLUSH);	/* clear out the crap */
    272  1.51  christos 		(void)ioctl(0, FIONBIO, &off);	/* turn off non-blocking mode */
    273  1.51  christos 		(void)ioctl(0, FIOASYNC, &off);	/* ditto for async mode */
    274   1.7        pk 
    275   1.1       cgd 		if (IS)
    276  1.51  christos 			(void)cfsetispeed(&tmode, (speed_t)IS);
    277   1.1       cgd 		else if (SP)
    278  1.51  christos 			(void)cfsetispeed(&tmode, (speed_t)SP);
    279   1.1       cgd 		if (OS)
    280  1.51  christos 			(void)cfsetospeed(&tmode, (speed_t)OS);
    281   1.1       cgd 		else if (SP)
    282  1.51  christos 			(void)cfsetospeed(&tmode, (speed_t)SP);
    283   1.7        pk 		setflags(0);
    284   1.1       cgd 		setchars();
    285   1.7        pk 		if (tcsetattr(0, TCSANOW, &tmode) < 0) {
    286   1.7        pk 			syslog(LOG_ERR, "%s: %m", ttyn);
    287   1.7        pk 			exit(1);
    288   1.7        pk 		}
    289   1.1       cgd 		if (AB) {
    290   1.1       cgd 			tname = autobaud();
    291   1.1       cgd 			continue;
    292   1.1       cgd 		}
    293   1.1       cgd 		if (PS) {
    294   1.1       cgd 			tname = portselector();
    295   1.1       cgd 			continue;
    296   1.1       cgd 		}
    297  1.35  sommerfe 		if (CS)
    298  1.35  sommerfe 			clearscreen();
    299   1.1       cgd 		if (CL && *CL)
    300   1.1       cgd 			putpad(CL);
    301   1.1       cgd 		edithost(HE);
    302  1.30        ad 
    303  1.53     isaki 		/*
    304  1.53     isaki 		 * If this is the first time through this, and an
    305  1.53     isaki 		 * issue file has been given, then send it.
    306  1.53     isaki 		 */
    307  1.31        ad 		if (first_time != 0 && IF != NULL) {
    308  1.31        ad 			char buf[_POSIX2_LINE_MAX];
    309  1.51  christos 			FILE *fp;
    310  1.31        ad 
    311  1.51  christos 			if ((fp = fopen(IF, "r")) != NULL) {
    312  1.51  christos 				while (fgets(buf, sizeof(buf) - 1, fp) != NULL)
    313  1.31        ad 					putf(buf);
    314  1.51  christos 				(void)fclose(fp);
    315  1.31        ad 			}
    316  1.31        ad 		}
    317  1.31        ad 		first_time = 0;
    318  1.52   hubertf 
    319   1.1       cgd 		if (IM && *IM)
    320   1.1       cgd 			putf(IM);
    321  1.28       kml 		oflush();
    322  1.62  dholland 		if (sigsetjmp(timeout, 1)) {
    323   1.7        pk 			tmode.c_ispeed = tmode.c_ospeed = 0;
    324   1.7        pk 			(void)tcsetattr(0, TCSANOW, &tmode);
    325   1.1       cgd 			exit(1);
    326   1.1       cgd 		}
    327   1.1       cgd 		if (TO) {
    328  1.51  christos 			(void)signal(SIGALRM, dingdong);
    329  1.51  christos 			(void)alarm((unsigned int)TO);
    330   1.1       cgd 		}
    331  1.44   thorpej 		if (NN) {
    332  1.44   thorpej 			name[0] = '\0';
    333  1.44   thorpej 			lower = 1;
    334  1.55  dholland 			upper = digit_or_punc = 0;
    335  1.44   thorpej 		} else if (AL) {
    336  1.30        ad 			const char *p = AL;
    337  1.30        ad 			char *q = name;
    338  1.30        ad 
    339  1.30        ad 			while (*p && q < &name[sizeof name - 1]) {
    340  1.46       dsl 				if (isupper((unsigned char)*p))
    341  1.30        ad 					upper = 1;
    342  1.46       dsl 				else if (islower((unsigned char)*p))
    343  1.30        ad 					lower = 1;
    344  1.46       dsl 				else if (isdigit((unsigned char)*p))
    345  1.55  dholland 					digit_or_punc = 1;
    346  1.30        ad 				*q++ = *p++;
    347  1.30        ad 			}
    348  1.30        ad 		} else if ((rval = getname()) == 2) {
    349  1.50  christos 			setflags(2);
    350  1.53     isaki 			(void)execle(PP, "ppplogin", ttyn, (char *) 0, env);
    351  1.53     isaki 			syslog(LOG_ERR, "%s: %m", PP);
    352  1.53     isaki 			exit(1);
    353  1.52   hubertf 		}
    354  1.53     isaki 
    355  1.44   thorpej 		if (rval || AL || NN) {
    356  1.25       mrg 			int i;
    357   1.1       cgd 
    358   1.1       cgd 			oflush();
    359  1.51  christos 			(void)alarm(0);
    360  1.51  christos 			(void)signal(SIGALRM, SIG_DFL);
    361   1.1       cgd 			if (name[0] == '-') {
    362  1.24   thorpej 				xputs("user names may not start with '-'.");
    363   1.1       cgd 				continue;
    364   1.1       cgd 			}
    365  1.55  dholland 			if (!(upper || lower || digit_or_punc))
    366   1.1       cgd 				continue;
    367   1.7        pk 			setflags(2);
    368   1.7        pk 			if (crmod) {
    369   1.7        pk 				tmode.c_iflag |= ICRNL;
    370   1.7        pk 				tmode.c_oflag |= ONLCR;
    371   1.7        pk 			}
    372   1.7        pk #if XXX
    373   1.1       cgd 			if (upper || UC)
    374   1.1       cgd 				tmode.sg_flags |= LCASE;
    375   1.1       cgd 			if (lower || LC)
    376   1.1       cgd 				tmode.sg_flags &= ~LCASE;
    377   1.7        pk #endif
    378   1.7        pk 			if (tcsetattr(0, TCSANOW, &tmode) < 0) {
    379   1.7        pk 				syslog(LOG_ERR, "%s: %m", ttyn);
    380   1.7        pk 				exit(1);
    381   1.7        pk 			}
    382  1.51  christos 			(void)signal(SIGINT, SIG_DFL);
    383  1.51  christos 			for (i = 0; envp[i] != NULL; i++)
    384  1.51  christos 				env[i] = envp[i];
    385   1.1       cgd 			makeenv(&env[i]);
    386   1.8        pk 
    387   1.8        pk 			limit.rlim_max = RLIM_INFINITY;
    388   1.8        pk 			limit.rlim_cur = RLIM_INFINITY;
    389   1.8        pk 			(void)setrlimit(RLIMIT_CPU, &limit);
    390  1.44   thorpej 			if (NN)
    391  1.51  christos 				(void)execle(LO, "login", AL ? "-fp" : "-p",
    392  1.44   thorpej 				    NULL, env);
    393  1.44   thorpej 			else
    394  1.51  christos 				(void)execle(LO, "login", AL ? "-fp" : "-p",
    395  1.44   thorpej 				    "--", name, NULL, env);
    396   1.1       cgd 			syslog(LOG_ERR, "%s: %m", LO);
    397   1.1       cgd 			exit(1);
    398   1.1       cgd 		}
    399  1.51  christos 		(void)alarm(0);
    400  1.51  christos 		(void)signal(SIGALRM, SIG_DFL);
    401  1.51  christos 		(void)signal(SIGINT, SIG_IGN);
    402   1.1       cgd 		if (NX && *NX)
    403   1.1       cgd 			tname = NX;
    404   1.1       cgd 	}
    405   1.1       cgd }
    406   1.1       cgd 
    407   1.8        pk static int
    408  1.41   thorpej getname(void)
    409   1.1       cgd {
    410  1.25       mrg 	int c;
    411  1.25       mrg 	char *np;
    412  1.27    tsarna 	unsigned char cs;
    413  1.27    tsarna 	int ppp_state, ppp_connection;
    414  1.53     isaki 
    415   1.1       cgd 	/*
    416   1.1       cgd 	 * Interrupt may happen if we use CBREAK mode
    417   1.1       cgd 	 */
    418  1.62  dholland 	if (sigsetjmp(intrupt, 1)) {
    419  1.51  christos 		(void)signal(SIGINT, SIG_IGN);
    420   1.1       cgd 		return (0);
    421   1.1       cgd 	}
    422  1.51  christos 	(void)signal(SIGINT, interrupt);
    423   1.7        pk 	setflags(1);
    424   1.1       cgd 	prompt();
    425   1.1       cgd 	if (PF > 0) {
    426   1.1       cgd 		oflush();
    427  1.51  christos 		(void)sleep((unsigned int)PF);
    428   1.1       cgd 		PF = 0;
    429   1.1       cgd 	}
    430   1.7        pk 	if (tcsetattr(0, TCSANOW, &tmode) < 0) {
    431   1.7        pk 		syslog(LOG_ERR, "%s: %m", ttyn);
    432   1.7        pk 		exit(1);
    433   1.7        pk 	}
    434  1.55  dholland 	crmod = digit_or_punc = lower = upper = 0;
    435  1.53     isaki 	ppp_state = ppp_connection = 0;
    436   1.1       cgd 	np = name;
    437   1.1       cgd 	for (;;) {
    438   1.1       cgd 		oflush();
    439   1.1       cgd 		if (read(STDIN_FILENO, &cs, 1) <= 0)
    440   1.1       cgd 			exit(0);
    441   1.1       cgd 		if ((c = cs&0177) == 0)
    442   1.1       cgd 			return (0);
    443  1.27    tsarna 
    444  1.27    tsarna 		/*
    445  1.27    tsarna 		 * PPP detection state machine..
    446  1.27    tsarna 		 * Look for sequences:
    447  1.27    tsarna 		 * PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
    448  1.27    tsarna 		 * PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
    449  1.27    tsarna 		 * See RFC1662.
    450  1.27    tsarna 		 * Derived from code from Michael Hancock <michaelh (at) cet.co.jp>
    451  1.27    tsarna 		 * and Erik 'PPP' Olson <eriko (at) wrq.com>
    452  1.27    tsarna 		 */
    453  1.27    tsarna 		if (PP && cs == PPP_FRAME) {
    454  1.27    tsarna 			ppp_state = 1;
    455  1.27    tsarna 		} else if (ppp_state == 1 && cs == PPP_STATION) {
    456  1.27    tsarna 			ppp_state = 2;
    457  1.27    tsarna 		} else if (ppp_state == 2 && cs == PPP_ESCAPE) {
    458  1.27    tsarna 			ppp_state = 3;
    459  1.27    tsarna 		} else if ((ppp_state == 2 && cs == PPP_CONTROL) ||
    460  1.27    tsarna 		    (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
    461  1.27    tsarna 			ppp_state = 4;
    462  1.27    tsarna 		} else if (ppp_state == 4 && cs == PPP_LCP_HI) {
    463  1.27    tsarna 			ppp_state = 5;
    464  1.27    tsarna 		} else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
    465  1.27    tsarna 			ppp_connection = 1;
    466  1.27    tsarna 			break;
    467  1.27    tsarna 		} else {
    468  1.27    tsarna 			ppp_state = 0;
    469  1.27    tsarna 		}
    470  1.27    tsarna 
    471   1.1       cgd 		if (c == EOT)
    472   1.1       cgd 			exit(1);
    473  1.29  drochner 		if (c == '\r' || c == '\n' ||
    474  1.29  drochner 		    np >= &name[LOGIN_NAME_MAX - 1]) {
    475  1.29  drochner 			*np = '\0';
    476   1.1       cgd 			putf("\r\n");
    477   1.1       cgd 			break;
    478   1.1       cgd 		}
    479   1.1       cgd 		if (islower(c))
    480   1.1       cgd 			lower = 1;
    481   1.1       cgd 		else if (isupper(c))
    482   1.1       cgd 			upper = 1;
    483   1.1       cgd 		else if (c == ERASE || c == '#' || c == '\b') {
    484   1.1       cgd 			if (np > name) {
    485   1.1       cgd 				np--;
    486  1.10   mycroft 				if (cfgetospeed(&tmode) >= 1200)
    487  1.24   thorpej 					xputs("\b \b");
    488   1.1       cgd 				else
    489   1.1       cgd 					putchr(cs);
    490   1.1       cgd 			}
    491   1.1       cgd 			continue;
    492   1.1       cgd 		} else if (c == KILL || c == '@') {
    493   1.1       cgd 			putchr(cs);
    494   1.1       cgd 			putchr('\r');
    495  1.10   mycroft 			if (cfgetospeed(&tmode) < 1200)
    496   1.1       cgd 				putchr('\n');
    497   1.1       cgd 			/* this is the way they do it down under ... */
    498   1.1       cgd 			else if (np > name)
    499  1.24   thorpej 				xputs(
    500  1.24   thorpej 				    "                                     \r");
    501   1.1       cgd 			prompt();
    502   1.1       cgd 			np = name;
    503   1.1       cgd 			continue;
    504  1.55  dholland 		} else if (isdigit(c) || c == '_')
    505  1.55  dholland 			digit_or_punc = 1;
    506   1.1       cgd 		if (IG && (c <= ' ' || c > 0176))
    507   1.1       cgd 			continue;
    508   1.1       cgd 		*np++ = c;
    509   1.1       cgd 		putchr(cs);
    510  1.52   hubertf 
    511  1.42  christos 		/*
    512  1.42  christos 		 * An MS-Windows direct connect PPP "client" won't send its
    513  1.42  christos 		 * first PPP packet until we respond to its "CLIENT" poll
    514  1.42  christos 		 * with a CRLF sequence.  We cater to yet another broken
    515  1.42  christos 		 * implementation of a previously-standard protocol...
    516  1.42  christos 		 */
    517  1.42  christos 		*np = '\0';
    518  1.42  christos 		if (strstr(name, "CLIENT"))
    519  1.53     isaki 			putf("\r\n");
    520   1.1       cgd 	}
    521  1.51  christos 	(void)signal(SIGINT, SIG_IGN);
    522   1.1       cgd 	*np = 0;
    523   1.1       cgd 	if (c == '\r')
    524   1.1       cgd 		crmod = 1;
    525  1.22     mikel 	if ((upper && !lower && !LC) || UC)
    526   1.1       cgd 		for (np = name; *np; np++)
    527  1.46       dsl 			*np = tolower((unsigned char)*np);
    528  1.27    tsarna 	return (1 + ppp_connection);
    529   1.1       cgd }
    530   1.1       cgd 
    531   1.8        pk static void
    532  1.41   thorpej xputs(const char *s)
    533   1.1       cgd {
    534   1.1       cgd 	while (*s)
    535   1.1       cgd 		putchr(*s++);
    536   1.1       cgd }
    537   1.1       cgd 
    538   1.1       cgd char	outbuf[OBUFSIZ];
    539  1.51  christos size_t	obufcnt = 0;
    540   1.1       cgd 
    541  1.59       roy static int
    542  1.41   thorpej putchr(int cc)
    543   1.1       cgd {
    544  1.51  christos 	unsigned char c;
    545   1.1       cgd 
    546   1.1       cgd 	c = cc;
    547   1.1       cgd 	if (!NP) {
    548   1.1       cgd 		c |= partab[c&0177] & 0200;
    549   1.1       cgd 		if (OP)
    550   1.1       cgd 			c ^= 0200;
    551   1.1       cgd 	}
    552   1.1       cgd 	if (!UB) {
    553   1.1       cgd 		outbuf[obufcnt++] = c;
    554   1.1       cgd 		if (obufcnt >= OBUFSIZ)
    555   1.1       cgd 			oflush();
    556  1.59       roy 		return 1;
    557  1.59       roy 	}
    558  1.59       roy 	return write(STDOUT_FILENO, &c, 1);
    559   1.1       cgd }
    560   1.1       cgd 
    561   1.8        pk static void
    562  1.41   thorpej oflush(void)
    563   1.1       cgd {
    564   1.1       cgd 	if (obufcnt)
    565  1.51  christos 		(void)write(STDOUT_FILENO, outbuf, obufcnt);
    566   1.1       cgd 	obufcnt = 0;
    567   1.1       cgd }
    568   1.1       cgd 
    569   1.8        pk static void
    570  1.41   thorpej prompt(void)
    571   1.1       cgd {
    572   1.1       cgd 
    573   1.1       cgd 	putf(LM);
    574   1.1       cgd 	if (CO)
    575   1.1       cgd 		putchr('\n');
    576   1.1       cgd }
    577   1.1       cgd 
    578   1.8        pk static void
    579  1.41   thorpej putf(const char *cp)
    580   1.1       cgd {
    581   1.1       cgd 	time_t t;
    582   1.1       cgd 	char *slash, db[100];
    583   1.1       cgd 
    584   1.1       cgd 	while (*cp) {
    585   1.1       cgd 		if (*cp != '%') {
    586   1.1       cgd 			putchr(*cp++);
    587   1.1       cgd 			continue;
    588   1.1       cgd 		}
    589   1.1       cgd 		switch (*++cp) {
    590   1.1       cgd 
    591   1.1       cgd 		case 't':
    592  1.47  christos 			if ((slash = strstr(ttyn, "/pts/")) == NULL)
    593  1.47  christos 				slash = strrchr(ttyn, '/');
    594  1.32       mjl 			if (slash == NULL)
    595  1.24   thorpej 				xputs(ttyn);
    596   1.1       cgd 			else
    597  1.24   thorpej 				xputs(&slash[1]);
    598   1.1       cgd 			break;
    599   1.1       cgd 
    600   1.1       cgd 		case 'h':
    601  1.24   thorpej 			xputs(editedhost);
    602   1.1       cgd 			break;
    603   1.1       cgd 
    604  1.34     bjh21 		case 'd':
    605   1.1       cgd 			(void)time(&t);
    606  1.34     bjh21 			(void)strftime(db, sizeof(db),
    607  1.57   mbalmer 			    "%l:%M%p on %A, %d %B %Y", localtime(&t));
    608  1.24   thorpej 			xputs(db);
    609   1.3   mycroft 			break;
    610   1.3   mycroft 
    611   1.3   mycroft 		case 's':
    612  1.24   thorpej 			xputs(kerninfo.sysname);
    613   1.3   mycroft 			break;
    614   1.3   mycroft 
    615   1.3   mycroft 		case 'm':
    616  1.24   thorpej 			xputs(kerninfo.machine);
    617   1.3   mycroft 			break;
    618   1.3   mycroft 
    619   1.3   mycroft 		case 'r':
    620  1.24   thorpej 			xputs(kerninfo.release);
    621   1.3   mycroft 			break;
    622   1.3   mycroft 
    623   1.3   mycroft 		case 'v':
    624  1.24   thorpej 			xputs(kerninfo.version);
    625   1.1       cgd 			break;
    626   1.1       cgd 
    627   1.1       cgd 		case '%':
    628   1.1       cgd 			putchr('%');
    629   1.1       cgd 			break;
    630   1.1       cgd 		}
    631  1.33     bjh21 		if (*cp)
    632  1.33     bjh21 			cp++;
    633   1.1       cgd 	}
    634  1.35  sommerfe }
    635  1.35  sommerfe 
    636  1.35  sommerfe static void
    637  1.41   thorpej clearscreen(void)
    638  1.35  sommerfe {
    639  1.35  sommerfe 	struct ttyent *typ;
    640  1.58       roy 	int err;
    641  1.35  sommerfe 
    642  1.35  sommerfe 	if (rawttyn == NULL)
    643  1.35  sommerfe 		return;
    644  1.35  sommerfe 
    645  1.53     isaki 	typ = getttynam(rawttyn);
    646  1.35  sommerfe 
    647  1.35  sommerfe 	if ((typ == NULL) || (typ->ty_type == NULL) ||
    648  1.35  sommerfe 	    (typ->ty_type[0] == 0))
    649  1.35  sommerfe 		return;
    650  1.35  sommerfe 
    651  1.58       roy 	if (setupterm(typ->ty_type, 0, &err) == ERR)
    652  1.35  sommerfe 		return;
    653  1.35  sommerfe 
    654  1.58       roy 	if (clear_screen)
    655  1.58       roy 		putpad(clear_screen);
    656  1.35  sommerfe 
    657  1.58       roy 	del_curterm(cur_term);
    658  1.58       roy 	cur_term = NULL;
    659   1.1       cgd }
    660