Home | History | Annotate | Line # | Download | only in getty
main.c revision 1.56.10.1
      1  1.56.10.1      matt /*	$NetBSD: main.c,v 1.56.10.1 2010/04/30 16:24:25 matt 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.56.10.1      matt __RCSID("$NetBSD: main.c,v 1.56.10.1 2010/04/30 16:24:25 matt 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.1       cgd #include <sys/stat.h>
     49       1.14       jtc #include <termios.h>
     50        1.7        pk #include <sys/ioctl.h>
     51        1.8        pk #include <sys/resource.h>
     52        1.3   mycroft #include <sys/utsname.h>
     53       1.21     mikel 
     54       1.54  dholland #include <ctype.h>
     55       1.15       cgd #include <errno.h>
     56        1.1       cgd #include <fcntl.h>
     57       1.54  dholland #include <limits.h>
     58       1.23       cjs #include <pwd.h>
     59        1.1       cgd #include <setjmp.h>
     60        1.8        pk #include <signal.h>
     61        1.8        pk #include <stdlib.h>
     62        1.8        pk #include <string.h>
     63        1.1       cgd #include <syslog.h>
     64       1.54  dholland #include <termcap.h>
     65        1.8        pk #include <time.h>
     66       1.54  dholland #include <ttyent.h>
     67        1.1       cgd #include <unistd.h>
     68       1.17       jtc #include <util.h>
     69        1.8        pk 
     70        1.1       cgd #include "gettytab.h"
     71        1.1       cgd #include "pathnames.h"
     72        1.8        pk #include "extern.h"
     73        1.8        pk 
     74       1.37  christos extern char editedhost[];
     75       1.23       cjs 
     76        1.8        pk /*
     77        1.8        pk  * Set the amount of running time that getty should accumulate
     78        1.8        pk  * before deciding that something is wrong and exit.
     79        1.8        pk  */
     80        1.8        pk #define GETTY_TIMEOUT	60 /* seconds */
     81        1.1       cgd 
     82       1.27    tsarna /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
     83       1.27    tsarna 
     84       1.27    tsarna #define PPP_FRAME           0x7e  /* PPP Framing character */
     85       1.27    tsarna #define PPP_STATION         0xff  /* "All Station" character */
     86       1.27    tsarna #define PPP_ESCAPE          0x7d  /* Escape Character */
     87       1.27    tsarna #define PPP_CONTROL         0x03  /* PPP Control Field */
     88       1.27    tsarna #define PPP_CONTROL_ESCAPED 0x23  /* PPP Control Field, escaped */
     89       1.27    tsarna #define PPP_LCP_HI          0xc0  /* LCP protocol - high byte */
     90       1.27    tsarna #define PPP_LCP_LOW         0x21  /* LCP protocol - low byte */
     91       1.27    tsarna 
     92        1.8        pk struct termios tmode, omode;
     93        1.1       cgd 
     94       1.55  dholland int crmod, digit_or_punc, lower, upper;
     95        1.1       cgd 
     96       1.26       mrg char	hostname[MAXHOSTNAMELEN + 1];
     97        1.3   mycroft struct	utsname kerninfo;
     98       1.29  drochner char	name[LOGIN_NAME_MAX];
     99        1.1       cgd char	dev[] = _PATH_DEV;
    100        1.1       cgd char	ttyn[32];
    101       1.23       cjs char	lockfile[512];
    102       1.23       cjs uid_t	ttyowner;
    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.1       cgd jmp_buf timeout;
    139        1.1       cgd 
    140        1.1       cgd 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.1       cgd 	longjmp(timeout, 1);
    148        1.1       cgd }
    149        1.1       cgd 
    150        1.1       cgd jmp_buf	intrupt;
    151        1.1       cgd 
    152        1.1       cgd 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.1       cgd 	longjmp(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.41   thorpej 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.41   thorpej static void	putchr(int);
    177       1.41   thorpej static void	putf(const char *);
    178       1.41   thorpej static void	putpad(const char *);
    179       1.41   thorpej static void	xputs(const char *);
    180        1.8        pk 
    181        1.8        pk int
    182       1.51  christos main(int argc, char *argv[], char *envp[])
    183        1.1       cgd {
    184       1.38       cgd 	const char *progname;
    185        1.1       cgd 	char *tname;
    186       1.30        ad 	int repcnt = 0, failopenlogged = 0, uugetty = 0, first_time = 1;
    187        1.8        pk 	struct rlimit limit;
    188       1.23       cjs 	struct passwd *pw;
    189       1.53     isaki 	int rval;
    190        1.1       cgd 
    191       1.51  christos 	(void)signal(SIGINT, SIG_IGN);
    192       1.36     lukem 	openlog("getty", LOG_PID, LOG_AUTH);
    193       1.51  christos 	(void)gethostname(hostname, sizeof(hostname));
    194       1.26       mrg 	hostname[sizeof(hostname) - 1] = '\0';
    195        1.1       cgd 	if (hostname[0] == '\0')
    196       1.51  christos 		(void)strlcpy(hostname, "Amnesiac", sizeof(hostname));
    197       1.51  christos 	(void)uname(&kerninfo);
    198        1.8        pk 
    199       1.38       cgd 	progname = getprogname();
    200       1.38       cgd 	if (progname[0] == 'u' && progname[1] == 'u')
    201       1.23       cjs 		uugetty = 1;
    202       1.23       cjs 
    203       1.23       cjs 	/*
    204       1.23       cjs 	 * Find id of uucp login (if present) so we can chown tty properly.
    205       1.23       cjs 	 */
    206       1.23       cjs 	if (uugetty && (pw = getpwnam("uucp")))
    207       1.23       cjs 		ttyowner = pw->pw_uid;
    208       1.23       cjs 	else
    209       1.23       cjs 		ttyowner = 0;
    210       1.23       cjs 
    211        1.8        pk 	/*
    212        1.8        pk 	 * Limit running time to deal with broken or dead lines.
    213        1.8        pk 	 */
    214        1.8        pk 	(void)signal(SIGXCPU, timeoverrun);
    215        1.8        pk 	limit.rlim_max = RLIM_INFINITY;
    216        1.8        pk 	limit.rlim_cur = GETTY_TIMEOUT;
    217        1.8        pk 	(void)setrlimit(RLIMIT_CPU, &limit);
    218        1.8        pk 
    219        1.1       cgd 	/*
    220        1.1       cgd 	 * The following is a work around for vhangup interactions
    221        1.1       cgd 	 * which cause great problems getting window systems started.
    222        1.1       cgd 	 * If the tty line is "-", we do the old style getty presuming
    223       1.52   hubertf 	 * that the file descriptors are already set up for us.
    224        1.1       cgd 	 * J. Gettys - MIT Project Athena.
    225        1.1       cgd 	 */
    226       1.20       tls 	if (argc <= 2 || strcmp(argv[2], "-") == 0) {
    227       1.51  christos 		(void)strlcpy(ttyn, ttyname(0), sizeof(ttyn));
    228       1.20       tls 	}
    229        1.1       cgd 	else {
    230       1.53     isaki 		int i;
    231        1.1       cgd 
    232       1.53     isaki 		rawttyn = argv[2];
    233       1.53     isaki 		(void)strlcpy(ttyn, dev, sizeof(ttyn));
    234       1.53     isaki 		(void)strlcat(ttyn, argv[2], sizeof(ttyn));
    235       1.53     isaki 		if (uugetty)  {
    236       1.53     isaki 			(void)chown(ttyn, ttyowner, 0);
    237       1.53     isaki 			(void)strlcpy(lockfile, _PATH_LOCK,
    238       1.51  christos 				sizeof(lockfile));
    239       1.53     isaki 			(void)strlcat(lockfile, argv[2],
    240       1.51  christos 				sizeof(lockfile));
    241       1.53     isaki 			/*
    242       1.53     isaki 			 * wait for lockfiles to go away before we try
    243       1.53     isaki 			 * to open
    244       1.53     isaki 			 */
    245       1.53     isaki 			if (pidlock(lockfile, 0, 0, 0) != 0)  {
    246       1.53     isaki 				syslog(LOG_ERR,
    247       1.51  christos 					"%s: can't create lockfile", ttyn);
    248       1.53     isaki 				exit(1);
    249       1.53     isaki 			}
    250       1.53     isaki 			(void)unlink(lockfile);
    251       1.53     isaki 		}
    252       1.53     isaki 		if (strcmp(argv[0], "+") != 0) {
    253       1.53     isaki 			(void)chown(ttyn, ttyowner, 0);
    254       1.53     isaki 			(void)chmod(ttyn, 0600);
    255       1.53     isaki 			(void)revoke(ttyn);
    256       1.53     isaki 			if (ttyaction(ttyn, "getty", "root"))
    257       1.53     isaki 				syslog(LOG_WARNING, "%s: ttyaction failed",
    258       1.51  christos 					ttyn);
    259       1.53     isaki 			/*
    260       1.53     isaki 			 * Delay the open so DTR stays down long enough
    261       1.53     isaki 			 * to be detected.
    262       1.53     isaki 			 */
    263       1.53     isaki 			(void)sleep(2);
    264       1.53     isaki 			while ((i = open(ttyn, O_RDWR)) == -1) {
    265       1.53     isaki 				if ((repcnt % 10 == 0) &&
    266       1.53     isaki 				    (errno != ENXIO || !failopenlogged)) {
    267       1.53     isaki 					syslog(LOG_WARNING, "%s: %m", ttyn);
    268       1.53     isaki 					closelog();
    269       1.53     isaki 					failopenlogged = 1;
    270       1.53     isaki 				}
    271       1.53     isaki 				repcnt++;
    272       1.53     isaki 				(void)sleep(60);
    273       1.53     isaki 			}
    274       1.53     isaki 			if (uugetty && pidlock(lockfile, 0, 0, 0) != 0)  {
    275       1.53     isaki 				syslog(LOG_ERR, "%s: can't create lockfile",
    276       1.51  christos 					ttyn);
    277       1.53     isaki 				exit(1);
    278       1.53     isaki 			}
    279       1.53     isaki 			if (uugetty)
    280       1.53     isaki 				(void)chown(lockfile, ttyowner, 0);
    281  1.56.10.1      matt 			if (login_tty(i) < 0)
    282  1.56.10.1      matt 				syslog(LOG_ERR, "%s: login_tty failed: %m",
    283  1.56.10.1      matt 					ttyn);
    284       1.23       cjs 		}
    285        1.1       cgd 	}
    286        1.1       cgd 
    287        1.7        pk 	/* Start with default tty settings */
    288        1.7        pk 	if (tcgetattr(0, &tmode) < 0) {
    289        1.7        pk 		syslog(LOG_ERR, "%s: %m", ttyn);
    290        1.7        pk 		exit(1);
    291        1.7        pk 	}
    292        1.8        pk 	omode = tmode;
    293        1.7        pk 
    294        1.5       cgd 	gettable("default", defent);
    295        1.1       cgd 	gendefaults();
    296        1.1       cgd 	tname = "default";
    297        1.1       cgd 	if (argc > 1)
    298        1.1       cgd 		tname = argv[1];
    299        1.1       cgd 	for (;;) {
    300        1.8        pk 		int off;
    301        1.1       cgd 
    302       1.48     lukem 		rval = 0;
    303        1.5       cgd 		gettable(tname, tabent);
    304        1.1       cgd 		if (OPset || EPset || APset)
    305        1.1       cgd 			APset++, OPset++, EPset++;
    306        1.1       cgd 		setdefaults();
    307        1.8        pk 		off = 0;
    308       1.12        pk 		(void)tcflush(0, TCIOFLUSH);	/* clear out the crap */
    309       1.51  christos 		(void)ioctl(0, FIONBIO, &off);	/* turn off non-blocking mode */
    310       1.51  christos 		(void)ioctl(0, FIOASYNC, &off);	/* ditto for async mode */
    311        1.7        pk 
    312        1.1       cgd 		if (IS)
    313       1.51  christos 			(void)cfsetispeed(&tmode, (speed_t)IS);
    314        1.1       cgd 		else if (SP)
    315       1.51  christos 			(void)cfsetispeed(&tmode, (speed_t)SP);
    316        1.1       cgd 		if (OS)
    317       1.51  christos 			(void)cfsetospeed(&tmode, (speed_t)OS);
    318        1.1       cgd 		else if (SP)
    319       1.51  christos 			(void)cfsetospeed(&tmode, (speed_t)SP);
    320        1.7        pk 		setflags(0);
    321        1.1       cgd 		setchars();
    322        1.7        pk 		if (tcsetattr(0, TCSANOW, &tmode) < 0) {
    323        1.7        pk 			syslog(LOG_ERR, "%s: %m", ttyn);
    324        1.7        pk 			exit(1);
    325        1.7        pk 		}
    326        1.1       cgd 		if (AB) {
    327        1.1       cgd 			tname = autobaud();
    328        1.1       cgd 			continue;
    329        1.1       cgd 		}
    330        1.1       cgd 		if (PS) {
    331        1.1       cgd 			tname = portselector();
    332        1.1       cgd 			continue;
    333        1.1       cgd 		}
    334       1.35  sommerfe 		if (CS)
    335       1.35  sommerfe 			clearscreen();
    336        1.1       cgd 		if (CL && *CL)
    337        1.1       cgd 			putpad(CL);
    338        1.1       cgd 		edithost(HE);
    339       1.30        ad 
    340       1.53     isaki 		/*
    341       1.53     isaki 		 * If this is the first time through this, and an
    342       1.53     isaki 		 * issue file has been given, then send it.
    343       1.53     isaki 		 */
    344       1.31        ad 		if (first_time != 0 && IF != NULL) {
    345       1.31        ad 			char buf[_POSIX2_LINE_MAX];
    346       1.51  christos 			FILE *fp;
    347       1.31        ad 
    348       1.51  christos 			if ((fp = fopen(IF, "r")) != NULL) {
    349       1.51  christos 				while (fgets(buf, sizeof(buf) - 1, fp) != NULL)
    350       1.31        ad 					putf(buf);
    351       1.51  christos 				(void)fclose(fp);
    352       1.31        ad 			}
    353       1.31        ad 		}
    354       1.31        ad 		first_time = 0;
    355       1.52   hubertf 
    356        1.1       cgd 		if (IM && *IM)
    357        1.1       cgd 			putf(IM);
    358       1.28       kml 		oflush();
    359        1.1       cgd 		if (setjmp(timeout)) {
    360        1.7        pk 			tmode.c_ispeed = tmode.c_ospeed = 0;
    361        1.7        pk 			(void)tcsetattr(0, TCSANOW, &tmode);
    362        1.1       cgd 			exit(1);
    363        1.1       cgd 		}
    364        1.1       cgd 		if (TO) {
    365       1.51  christos 			(void)signal(SIGALRM, dingdong);
    366       1.51  christos 			(void)alarm((unsigned int)TO);
    367        1.1       cgd 		}
    368       1.44   thorpej 		if (NN) {
    369       1.44   thorpej 			name[0] = '\0';
    370       1.44   thorpej 			lower = 1;
    371       1.55  dholland 			upper = digit_or_punc = 0;
    372       1.44   thorpej 		} else if (AL) {
    373       1.30        ad 			const char *p = AL;
    374       1.30        ad 			char *q = name;
    375       1.30        ad 
    376       1.30        ad 			while (*p && q < &name[sizeof name - 1]) {
    377       1.46       dsl 				if (isupper((unsigned char)*p))
    378       1.30        ad 					upper = 1;
    379       1.46       dsl 				else if (islower((unsigned char)*p))
    380       1.30        ad 					lower = 1;
    381       1.46       dsl 				else if (isdigit((unsigned char)*p))
    382       1.55  dholland 					digit_or_punc = 1;
    383       1.30        ad 				*q++ = *p++;
    384       1.30        ad 			}
    385       1.30        ad 		} else if ((rval = getname()) == 2) {
    386       1.50  christos 			setflags(2);
    387       1.53     isaki 			(void)execle(PP, "ppplogin", ttyn, (char *) 0, env);
    388       1.53     isaki 			syslog(LOG_ERR, "%s: %m", PP);
    389       1.53     isaki 			exit(1);
    390       1.52   hubertf 		}
    391       1.53     isaki 
    392       1.44   thorpej 		if (rval || AL || NN) {
    393       1.25       mrg 			int i;
    394        1.1       cgd 
    395        1.1       cgd 			oflush();
    396       1.51  christos 			(void)alarm(0);
    397       1.51  christos 			(void)signal(SIGALRM, SIG_DFL);
    398        1.1       cgd 			if (name[0] == '-') {
    399       1.24   thorpej 				xputs("user names may not start with '-'.");
    400        1.1       cgd 				continue;
    401        1.1       cgd 			}
    402       1.55  dholland 			if (!(upper || lower || digit_or_punc))
    403        1.1       cgd 				continue;
    404        1.7        pk 			setflags(2);
    405        1.7        pk 			if (crmod) {
    406        1.7        pk 				tmode.c_iflag |= ICRNL;
    407        1.7        pk 				tmode.c_oflag |= ONLCR;
    408        1.7        pk 			}
    409        1.7        pk #if XXX
    410        1.1       cgd 			if (upper || UC)
    411        1.1       cgd 				tmode.sg_flags |= LCASE;
    412        1.1       cgd 			if (lower || LC)
    413        1.1       cgd 				tmode.sg_flags &= ~LCASE;
    414        1.7        pk #endif
    415        1.7        pk 			if (tcsetattr(0, TCSANOW, &tmode) < 0) {
    416        1.7        pk 				syslog(LOG_ERR, "%s: %m", ttyn);
    417        1.7        pk 				exit(1);
    418        1.7        pk 			}
    419       1.51  christos 			(void)signal(SIGINT, SIG_DFL);
    420       1.51  christos 			for (i = 0; envp[i] != NULL; i++)
    421       1.51  christos 				env[i] = envp[i];
    422        1.1       cgd 			makeenv(&env[i]);
    423        1.8        pk 
    424        1.8        pk 			limit.rlim_max = RLIM_INFINITY;
    425        1.8        pk 			limit.rlim_cur = RLIM_INFINITY;
    426        1.8        pk 			(void)setrlimit(RLIMIT_CPU, &limit);
    427       1.44   thorpej 			if (NN)
    428       1.51  christos 				(void)execle(LO, "login", AL ? "-fp" : "-p",
    429       1.44   thorpej 				    NULL, env);
    430       1.44   thorpej 			else
    431       1.51  christos 				(void)execle(LO, "login", AL ? "-fp" : "-p",
    432       1.44   thorpej 				    "--", name, NULL, env);
    433        1.1       cgd 			syslog(LOG_ERR, "%s: %m", LO);
    434        1.1       cgd 			exit(1);
    435        1.1       cgd 		}
    436       1.51  christos 		(void)alarm(0);
    437       1.51  christos 		(void)signal(SIGALRM, SIG_DFL);
    438       1.51  christos 		(void)signal(SIGINT, SIG_IGN);
    439        1.1       cgd 		if (NX && *NX)
    440        1.1       cgd 			tname = NX;
    441       1.49      elad 		if (uugetty)
    442       1.51  christos 			(void)unlink(lockfile);
    443        1.1       cgd 	}
    444        1.1       cgd }
    445        1.1       cgd 
    446        1.8        pk static int
    447       1.41   thorpej getname(void)
    448        1.1       cgd {
    449       1.25       mrg 	int c;
    450       1.25       mrg 	char *np;
    451       1.27    tsarna 	unsigned char cs;
    452       1.27    tsarna 	int ppp_state, ppp_connection;
    453       1.53     isaki 
    454        1.1       cgd 	/*
    455        1.1       cgd 	 * Interrupt may happen if we use CBREAK mode
    456        1.1       cgd 	 */
    457        1.1       cgd 	if (setjmp(intrupt)) {
    458       1.51  christos 		(void)signal(SIGINT, SIG_IGN);
    459        1.1       cgd 		return (0);
    460        1.1       cgd 	}
    461       1.51  christos 	(void)signal(SIGINT, interrupt);
    462        1.7        pk 	setflags(1);
    463        1.1       cgd 	prompt();
    464        1.1       cgd 	if (PF > 0) {
    465        1.1       cgd 		oflush();
    466       1.51  christos 		(void)sleep((unsigned int)PF);
    467        1.1       cgd 		PF = 0;
    468        1.1       cgd 	}
    469        1.7        pk 	if (tcsetattr(0, TCSANOW, &tmode) < 0) {
    470        1.7        pk 		syslog(LOG_ERR, "%s: %m", ttyn);
    471        1.7        pk 		exit(1);
    472        1.7        pk 	}
    473       1.55  dholland 	crmod = digit_or_punc = lower = upper = 0;
    474       1.53     isaki 	ppp_state = ppp_connection = 0;
    475        1.1       cgd 	np = name;
    476        1.1       cgd 	for (;;) {
    477        1.1       cgd 		oflush();
    478        1.1       cgd 		if (read(STDIN_FILENO, &cs, 1) <= 0)
    479        1.1       cgd 			exit(0);
    480        1.1       cgd 		if ((c = cs&0177) == 0)
    481        1.1       cgd 			return (0);
    482       1.27    tsarna 
    483       1.27    tsarna 		/*
    484       1.27    tsarna 		 * PPP detection state machine..
    485       1.27    tsarna 		 * Look for sequences:
    486       1.27    tsarna 		 * PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
    487       1.27    tsarna 		 * PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
    488       1.27    tsarna 		 * See RFC1662.
    489       1.27    tsarna 		 * Derived from code from Michael Hancock <michaelh (at) cet.co.jp>
    490       1.27    tsarna 		 * and Erik 'PPP' Olson <eriko (at) wrq.com>
    491       1.27    tsarna 		 */
    492       1.27    tsarna 		if (PP && cs == PPP_FRAME) {
    493       1.27    tsarna 			ppp_state = 1;
    494       1.27    tsarna 		} else if (ppp_state == 1 && cs == PPP_STATION) {
    495       1.27    tsarna 			ppp_state = 2;
    496       1.27    tsarna 		} else if (ppp_state == 2 && cs == PPP_ESCAPE) {
    497       1.27    tsarna 			ppp_state = 3;
    498       1.27    tsarna 		} else if ((ppp_state == 2 && cs == PPP_CONTROL) ||
    499       1.27    tsarna 		    (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
    500       1.27    tsarna 			ppp_state = 4;
    501       1.27    tsarna 		} else if (ppp_state == 4 && cs == PPP_LCP_HI) {
    502       1.27    tsarna 			ppp_state = 5;
    503       1.27    tsarna 		} else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
    504       1.27    tsarna 			ppp_connection = 1;
    505       1.27    tsarna 			break;
    506       1.27    tsarna 		} else {
    507       1.27    tsarna 			ppp_state = 0;
    508       1.27    tsarna 		}
    509       1.27    tsarna 
    510        1.1       cgd 		if (c == EOT)
    511        1.1       cgd 			exit(1);
    512       1.29  drochner 		if (c == '\r' || c == '\n' ||
    513       1.29  drochner 		    np >= &name[LOGIN_NAME_MAX - 1]) {
    514       1.29  drochner 			*np = '\0';
    515        1.1       cgd 			putf("\r\n");
    516        1.1       cgd 			break;
    517        1.1       cgd 		}
    518        1.1       cgd 		if (islower(c))
    519        1.1       cgd 			lower = 1;
    520        1.1       cgd 		else if (isupper(c))
    521        1.1       cgd 			upper = 1;
    522        1.1       cgd 		else if (c == ERASE || c == '#' || c == '\b') {
    523        1.1       cgd 			if (np > name) {
    524        1.1       cgd 				np--;
    525       1.10   mycroft 				if (cfgetospeed(&tmode) >= 1200)
    526       1.24   thorpej 					xputs("\b \b");
    527        1.1       cgd 				else
    528        1.1       cgd 					putchr(cs);
    529        1.1       cgd 			}
    530        1.1       cgd 			continue;
    531        1.1       cgd 		} else if (c == KILL || c == '@') {
    532        1.1       cgd 			putchr(cs);
    533        1.1       cgd 			putchr('\r');
    534       1.10   mycroft 			if (cfgetospeed(&tmode) < 1200)
    535        1.1       cgd 				putchr('\n');
    536        1.1       cgd 			/* this is the way they do it down under ... */
    537        1.1       cgd 			else if (np > name)
    538       1.24   thorpej 				xputs(
    539       1.24   thorpej 				    "                                     \r");
    540        1.1       cgd 			prompt();
    541        1.1       cgd 			np = name;
    542        1.1       cgd 			continue;
    543       1.55  dholland 		} else if (isdigit(c) || c == '_')
    544       1.55  dholland 			digit_or_punc = 1;
    545        1.1       cgd 		if (IG && (c <= ' ' || c > 0176))
    546        1.1       cgd 			continue;
    547        1.1       cgd 		*np++ = c;
    548        1.1       cgd 		putchr(cs);
    549       1.52   hubertf 
    550       1.42  christos 		/*
    551       1.42  christos 		 * An MS-Windows direct connect PPP "client" won't send its
    552       1.42  christos 		 * first PPP packet until we respond to its "CLIENT" poll
    553       1.42  christos 		 * with a CRLF sequence.  We cater to yet another broken
    554       1.42  christos 		 * implementation of a previously-standard protocol...
    555       1.42  christos 		 */
    556       1.42  christos 		*np = '\0';
    557       1.42  christos 		if (strstr(name, "CLIENT"))
    558       1.53     isaki 			putf("\r\n");
    559        1.1       cgd 	}
    560       1.51  christos 	(void)signal(SIGINT, SIG_IGN);
    561        1.1       cgd 	*np = 0;
    562        1.1       cgd 	if (c == '\r')
    563        1.1       cgd 		crmod = 1;
    564       1.22     mikel 	if ((upper && !lower && !LC) || UC)
    565        1.1       cgd 		for (np = name; *np; np++)
    566       1.46       dsl 			*np = tolower((unsigned char)*np);
    567       1.27    tsarna 	return (1 + ppp_connection);
    568        1.1       cgd }
    569        1.1       cgd 
    570        1.8        pk static void
    571       1.41   thorpej putpad(const char *s)
    572        1.1       cgd {
    573       1.25       mrg 	int pad = 0;
    574       1.51  christos 	speed_t ospd = cfgetospeed(&tmode);
    575        1.1       cgd 
    576       1.46       dsl 	if (isdigit((unsigned char)*s)) {
    577       1.46       dsl 		while (isdigit((unsigned char)*s)) {
    578        1.1       cgd 			pad *= 10;
    579        1.1       cgd 			pad += *s++ - '0';
    580        1.1       cgd 		}
    581        1.1       cgd 		pad *= 10;
    582       1.46       dsl 		if (*s == '.' && isdigit((unsigned char)s[1])) {
    583        1.1       cgd 			pad += s[1] - '0';
    584        1.1       cgd 			s += 2;
    585        1.1       cgd 		}
    586        1.1       cgd 	}
    587        1.1       cgd 
    588       1.24   thorpej 	xputs(s);
    589        1.1       cgd 	/*
    590        1.1       cgd 	 * If no delay needed, or output speed is
    591        1.1       cgd 	 * not comprehensible, then don't try to delay.
    592        1.1       cgd 	 */
    593       1.51  christos 	if (pad == 0)
    594        1.1       cgd 		return;
    595        1.1       cgd 
    596        1.1       cgd 	/*
    597        1.1       cgd 	 * Round up by a half a character frame, and then do the delay.
    598        1.1       cgd 	 * Too bad there are no user program accessible programmed delays.
    599        1.1       cgd 	 * Transmitting pad characters slows many terminals down and also
    600        1.1       cgd 	 * loads the system.
    601        1.1       cgd 	 */
    602       1.51  christos 	pad = (pad * ospd + 50000) / 100000;
    603       1.10   mycroft 	while (pad--)
    604        1.1       cgd 		putchr(*PC);
    605        1.1       cgd }
    606        1.1       cgd 
    607        1.8        pk static void
    608       1.41   thorpej xputs(const char *s)
    609        1.1       cgd {
    610        1.1       cgd 	while (*s)
    611        1.1       cgd 		putchr(*s++);
    612        1.1       cgd }
    613        1.1       cgd 
    614        1.1       cgd char	outbuf[OBUFSIZ];
    615       1.51  christos size_t	obufcnt = 0;
    616        1.1       cgd 
    617        1.8        pk static void
    618       1.41   thorpej putchr(int cc)
    619        1.1       cgd {
    620       1.51  christos 	unsigned char c;
    621        1.1       cgd 
    622        1.1       cgd 	c = cc;
    623        1.1       cgd 	if (!NP) {
    624        1.1       cgd 		c |= partab[c&0177] & 0200;
    625        1.1       cgd 		if (OP)
    626        1.1       cgd 			c ^= 0200;
    627        1.1       cgd 	}
    628        1.1       cgd 	if (!UB) {
    629        1.1       cgd 		outbuf[obufcnt++] = c;
    630        1.1       cgd 		if (obufcnt >= OBUFSIZ)
    631        1.1       cgd 			oflush();
    632        1.1       cgd 	} else
    633       1.51  christos 		(void)write(STDOUT_FILENO, &c, 1);
    634        1.1       cgd }
    635        1.1       cgd 
    636        1.8        pk static void
    637       1.41   thorpej oflush(void)
    638        1.1       cgd {
    639        1.1       cgd 	if (obufcnt)
    640       1.51  christos 		(void)write(STDOUT_FILENO, outbuf, obufcnt);
    641        1.1       cgd 	obufcnt = 0;
    642        1.1       cgd }
    643        1.1       cgd 
    644        1.8        pk static void
    645       1.41   thorpej prompt(void)
    646        1.1       cgd {
    647        1.1       cgd 
    648        1.1       cgd 	putf(LM);
    649        1.1       cgd 	if (CO)
    650        1.1       cgd 		putchr('\n');
    651        1.1       cgd }
    652        1.1       cgd 
    653        1.8        pk static void
    654       1.41   thorpej putf(const char *cp)
    655        1.1       cgd {
    656        1.1       cgd 	time_t t;
    657        1.1       cgd 	char *slash, db[100];
    658        1.1       cgd 
    659        1.1       cgd 	while (*cp) {
    660        1.1       cgd 		if (*cp != '%') {
    661        1.1       cgd 			putchr(*cp++);
    662        1.1       cgd 			continue;
    663        1.1       cgd 		}
    664        1.1       cgd 		switch (*++cp) {
    665        1.1       cgd 
    666        1.1       cgd 		case 't':
    667       1.47  christos 			if ((slash = strstr(ttyn, "/pts/")) == NULL)
    668       1.47  christos 				slash = strrchr(ttyn, '/');
    669       1.32       mjl 			if (slash == NULL)
    670       1.24   thorpej 				xputs(ttyn);
    671        1.1       cgd 			else
    672       1.24   thorpej 				xputs(&slash[1]);
    673        1.1       cgd 			break;
    674        1.1       cgd 
    675        1.1       cgd 		case 'h':
    676       1.24   thorpej 			xputs(editedhost);
    677        1.1       cgd 			break;
    678        1.1       cgd 
    679       1.34     bjh21 		case 'd':
    680        1.1       cgd 			(void)time(&t);
    681       1.34     bjh21 			(void)strftime(db, sizeof(db),
    682       1.34     bjh21 			    /* SCCS eats %M% */
    683       1.34     bjh21 			    "%l:%M" "%p on %A, %d %B %Y", localtime(&t));
    684       1.24   thorpej 			xputs(db);
    685        1.3   mycroft 			break;
    686        1.3   mycroft 
    687        1.3   mycroft 		case 's':
    688       1.24   thorpej 			xputs(kerninfo.sysname);
    689        1.3   mycroft 			break;
    690        1.3   mycroft 
    691        1.3   mycroft 		case 'm':
    692       1.24   thorpej 			xputs(kerninfo.machine);
    693        1.3   mycroft 			break;
    694        1.3   mycroft 
    695        1.3   mycroft 		case 'r':
    696       1.24   thorpej 			xputs(kerninfo.release);
    697        1.3   mycroft 			break;
    698        1.3   mycroft 
    699        1.3   mycroft 		case 'v':
    700       1.24   thorpej 			xputs(kerninfo.version);
    701        1.1       cgd 			break;
    702        1.1       cgd 
    703        1.1       cgd 		case '%':
    704        1.1       cgd 			putchr('%');
    705        1.1       cgd 			break;
    706        1.1       cgd 		}
    707       1.33     bjh21 		if (*cp)
    708       1.33     bjh21 			cp++;
    709        1.1       cgd 	}
    710       1.35  sommerfe }
    711       1.35  sommerfe 
    712       1.35  sommerfe static void
    713       1.41   thorpej clearscreen(void)
    714       1.35  sommerfe {
    715       1.35  sommerfe 	struct ttyent *typ;
    716       1.35  sommerfe 	struct tinfo *tinfo;
    717       1.35  sommerfe 	char *cs;
    718       1.35  sommerfe 
    719       1.35  sommerfe 	if (rawttyn == NULL)
    720       1.35  sommerfe 		return;
    721       1.35  sommerfe 
    722       1.53     isaki 	typ = getttynam(rawttyn);
    723       1.35  sommerfe 
    724       1.35  sommerfe 	if ((typ == NULL) || (typ->ty_type == NULL) ||
    725       1.35  sommerfe 	    (typ->ty_type[0] == 0))
    726       1.35  sommerfe 		return;
    727       1.35  sommerfe 
    728       1.35  sommerfe 	if (t_getent(&tinfo, typ->ty_type) <= 0)
    729       1.35  sommerfe 		return;
    730       1.35  sommerfe 
    731       1.39  christos 	cs = t_agetstr(tinfo, "cl");
    732       1.35  sommerfe 	if (cs == NULL)
    733       1.35  sommerfe 		return;
    734       1.35  sommerfe 
    735       1.35  sommerfe 	putpad(cs);
    736        1.1       cgd }
    737