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