2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)from: main.c 8.1 (Berkeley) 6/20/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
49 #include <sys/resource.h>
51 #include <sys/ttydefaults.h>
52 #include <sys/utsname.h>
70 #include "pathnames.h"
73 * Set the amount of running time that getty should accumulate
74 * before deciding that something is wrong and exit.
76 #define GETTY_TIMEOUT 60 /* seconds */
79 #define CTRL(x) (x&037)
81 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
83 #define PPP_FRAME 0x7e /* PPP Framing character */
84 #define PPP_STATION 0xff /* "All Station" character */
85 #define PPP_ESCAPE 0x7d /* Escape Character */
86 #define PPP_CONTROL 0x03 /* PPP Control Field */
87 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
88 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
89 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
91 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
96 static int crmod, digit, lower, upper;
98 char hostname[MAXHOSTNAMELEN];
99 static char name[MAXLOGNAME*3];
100 static char dev[] = _PATH_DEV;
101 static char ttyn[32];
104 #define TABBUFSIZ 512
106 static char defent[TABBUFSIZ];
107 static char tabent[TABBUFSIZ];
108 static const char *tname;
110 static char *env[128];
112 static char partab[] = {
113 0001,0201,0201,0001,0201,0001,0001,0201,
114 0202,0004,0003,0205,0005,0206,0201,0001,
115 0201,0001,0001,0201,0001,0201,0201,0001,
116 0001,0201,0201,0001,0201,0001,0001,0201,
117 0200,0000,0000,0200,0000,0200,0200,0000,
118 0000,0200,0200,0000,0200,0000,0000,0200,
119 0000,0200,0200,0000,0200,0000,0000,0200,
120 0200,0000,0000,0200,0000,0200,0200,0000,
121 0200,0000,0000,0200,0000,0200,0200,0000,
122 0000,0200,0200,0000,0200,0000,0000,0200,
123 0000,0200,0200,0000,0200,0000,0000,0200,
124 0200,0000,0000,0200,0000,0200,0200,0000,
125 0000,0200,0200,0000,0200,0000,0000,0200,
126 0200,0000,0000,0200,0000,0200,0200,0000,
127 0200,0000,0000,0200,0000,0200,0200,0000,
128 0000,0200,0200,0000,0200,0000,0000,0201
131 #define ERASE tmode.c_cc[VERASE]
132 #define KILL tmode.c_cc[VKILL]
133 #define EOT tmode.c_cc[VEOF]
137 static void defttymode(void);
138 static void dingdong(int);
139 static void dogettytab(void);
140 static int getname(void);
141 static void interrupt(int);
142 static void oflush(void);
143 static void prompt(void);
144 static void putchr(int);
145 static void putf(const char *);
146 static void putpad(const char *);
147 static void puts(const char *);
148 static void timeoverrun(int);
149 static char *get_line(int);
150 static void setttymode(int);
151 static int opentty(const char *, int);
153 static jmp_buf timeout;
156 dingdong(int signo __unused)
162 static jmp_buf intrupt;
165 interrupt(int signo __unused)
171 * Action to take when getty is running too long.
174 timeoverrun(int signo __unused)
177 syslog(LOG_ERR, "getty exiting due to excessive running time");
182 main(int argc, char *argv[])
184 int first_sleep = 1, first_time = 1;
188 signal(SIGINT, SIG_IGN);
189 signal(SIGQUIT, SIG_IGN);
191 openlog("getty", LOG_CONS|LOG_PID, LOG_AUTH);
192 gethostname(hostname, sizeof(hostname) - 1);
193 hostname[sizeof(hostname) - 1] = '\0';
194 if (hostname[0] == '\0')
195 strcpy(hostname, "Amnesiac");
198 * Limit running time to deal with broken or dead lines.
200 (void)signal(SIGXCPU, timeoverrun);
201 limit.rlim_max = RLIM_INFINITY;
202 limit.rlim_cur = GETTY_TIMEOUT;
203 (void)setrlimit(RLIMIT_CPU, &limit);
205 gettable("default", defent);
212 * The following is a work around for vhangup interactions
213 * which cause great problems getting window systems started.
214 * If the tty line is "-", we do the old style getty presuming
215 * that the file descriptors are already set up for us.
216 * J. Gettys - MIT Project Athena.
218 if (argc <= 2 || strcmp(argv[2], "-") == 0)
219 strcpy(ttyn, ttyname(STDIN_FILENO));
222 strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
223 if (strcmp(argv[0], "+") != 0) {
229 * Do the first scan through gettytab.
230 * Terminal mode parameters will be wrong until
231 * defttymode() called, but they're irrelevant for
232 * the initial setup of the terminal device.
237 * Init or answer modem sequence has been specified.
240 if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
247 if (getty_chat(IC, CT, DC) > 0) {
248 syslog(LOG_ERR, "modem init problem on %s", ttyn);
249 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
263 i = select(32, &rfds, NULL, NULL, RT ? &to : NULL);
265 syslog(LOG_ERR, "select %s: %m", ttyn);
267 syslog(LOG_NOTICE, "recycle tty %s", ttyn);
268 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
269 exit(0); /* recycle for init */
271 i = getty_chat(AC, CT, DC);
273 syslog(LOG_ERR, "modem answer problem on %s", ttyn);
274 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
277 } else { /* maybe blocking open */
278 if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
288 * if a delay was specified then sleep for that
289 * number of seconds before writing the initial prompt
291 if (first_sleep && DE) {
293 /* remove any noise */
294 (void)tcflush(STDIN_FILENO, TCIOFLUSH);
305 tname = portselector();
313 /* if this is the first time through this, and an
314 issue file has been given, then send it */
315 if (first_time && IF) {
318 if ((fd = open(IF, O_RDONLY)) != -1) {
321 while ((cp = get_line(fd)) != NULL) {
329 if (IMP && *IMP && !(PL && PP))
331 if (IM && *IM && !(PL && PP))
333 if (setjmp(timeout)) {
334 cfsetispeed(&tmode, B0);
335 cfsetospeed(&tmode, B0);
336 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
340 signal(SIGALRM, dingdong);
349 while (*p && q < &name[sizeof name - 1]) {
352 else if (islower(*p))
354 else if (isdigit(*p))
358 } else if (!(PL && PP))
360 if (rval == 2 || (PL && PP)) {
363 limit.rlim_max = RLIM_INFINITY;
364 limit.rlim_cur = RLIM_INFINITY;
365 (void)setrlimit(RLIMIT_CPU, &limit);
366 execle(PP, "ppplogin", ttyn, (char *) 0, env);
367 syslog(LOG_ERR, "%s: %m", PP);
369 } else if (rval || AL) {
374 signal(SIGALRM, SIG_DFL);
377 if (name[0] == '-') {
378 puts("user names may not start with '-'.");
381 if (!(upper || lower || digit)) {
384 "invalid auto-login name: %s", AL);
391 tmode.c_iflag |= ICRNL;
392 tmode.c_oflag |= ONLCR;
396 tmode.sg_flags |= LCASE;
398 tmode.sg_flags &= ~LCASE;
400 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
401 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
404 signal(SIGINT, SIG_DFL);
405 for (i = 0; environ[i] != (char *)0; i++)
409 limit.rlim_max = RLIM_INFINITY;
410 limit.rlim_cur = RLIM_INFINITY;
411 (void)setrlimit(RLIMIT_CPU, &limit);
412 execle(LO, "login", AL ? "-fp" : "-p", name,
414 syslog(LOG_ERR, "%s: %m", LO);
418 signal(SIGALRM, SIG_DFL);
419 signal(SIGINT, SIG_IGN);
428 opentty(const char *tty, int flags)
431 int failopenlogged = 0;
433 while ((i = open(tty, flags)) == -1)
435 if (!failopenlogged) {
436 syslog(LOG_ERR, "open %s: %m", tty);
441 if (login_tty(i) < 0) {
442 if (daemon(0,0) < 0) {
443 syslog(LOG_ERR,"daemon: %m");
447 if (login_tty(i) < 0) {
448 syslog(LOG_ERR, "login_tty %s: %m", tty);
461 /* Start with default tty settings. */
462 if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
463 syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
466 omode = tmode; /* fill c_cc for dogettytab() */
469 * Don't rely on the driver too much, and initialize crucial
470 * things according to <sys/ttydefaults.h>. Avoid clobbering
471 * the c_cc[] settings however, the console drivers might wish
472 * to leave their idea of the preferred VERASE key value
476 tmode.c_iflag = def.c_iflag;
477 tmode.c_oflag = def.c_oflag;
478 tmode.c_lflag = def.c_lflag;
479 tmode.c_cflag = def.c_cflag;
481 tmode.c_cflag |= CLOCAL;
490 (void)tcflush(STDIN_FILENO, TCIOFLUSH); /* clear out the crap */
491 ioctl(STDIN_FILENO, FIONBIO, &off); /* turn off non-blocking mode */
492 ioctl(STDIN_FILENO, FIOASYNC, &off); /* ditto for async mode */
495 cfsetispeed(&tmode, speed(IS));
497 cfsetispeed(&tmode, speed(SP));
499 cfsetospeed(&tmode, speed(OS));
501 cfsetospeed(&tmode, speed(SP));
506 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
507 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
520 int ppp_connection = 0;
523 * Interrupt may happen if we use CBREAK mode
525 if (setjmp(intrupt)) {
526 signal(SIGINT, SIG_IGN);
529 signal(SIGINT, interrupt);
537 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
538 syslog(LOG_ERR, "%s: %m", ttyn);
541 crmod = digit = lower = upper = 0;
545 if (read(STDIN_FILENO, &cs, 1) <= 0)
547 if ((c = cs&0177) == 0)
550 /* PPP detection state machine..
552 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
553 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
555 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
556 and Erik 'PPP' Olson, <eriko@wrq.com>
559 if (PP && (cs == PPP_FRAME)) {
561 } else if (ppp_state == 1 && cs == PPP_STATION) {
563 } else if (ppp_state == 2 && cs == PPP_ESCAPE) {
565 } else if ((ppp_state == 2 && cs == PPP_CONTROL)
566 || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
568 } else if (ppp_state == 4 && cs == PPP_LCP_HI) {
570 } else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
577 if (c == EOT || c == CTRL('d'))
579 if (c == '\r' || c == '\n' || np >= &name[sizeof name-1]) {
587 else if (c == ERASE || c == '\b' || c == 0177) {
590 if (cfgetospeed(&tmode) >= 1200)
596 } else if (c == KILL || c == CTRL('u')) {
598 if (cfgetospeed(&tmode) < 1200)
600 /* this is the way they do it down under ... */
604 digit = lower = upper = 0;
607 } else if (isdigit(c))
609 if (IG && (c <= ' ' || c > 0176))
614 signal(SIGINT, SIG_IGN);
618 if ((upper && !lower && !LC) || UC)
619 for (np = name; *np; np++)
622 return (1 + ppp_connection);
626 putpad(const char *s)
629 speed_t ospeed = cfgetospeed(&tmode);
632 while (isdigit(*s)) {
637 if (*s == '.' && isdigit(s[1])) {
645 * If no delay needed, or output speed is
646 * not comprehensible, then don't try to delay.
648 if (pad == 0 || ospeed <= 0)
652 * Round up by a half a character frame, and then do the delay.
653 * Too bad there are no user program accessible programmed delays.
654 * Transmitting pad characters slows many terminals down and also
657 pad = (pad * ospeed + 50000) / 100000;
669 static char outbuf[OBUFSIZ];
670 static int obufcnt = 0;
679 c |= partab[c&0177] & 0200;
684 outbuf[obufcnt++] = c;
685 if (obufcnt >= OBUFSIZ)
688 write(STDOUT_FILENO, &c, 1);
695 write(STDOUT_FILENO, outbuf, obufcnt);
713 static char linebuf[512];
716 * This is certainly slow, but it avoids having to include
717 * stdio.h unnecessarily. Issue files should be small anyway.
719 while (i < (sizeof linebuf - 3) && read(fd, linebuf+i, 1)==1) {
720 if (linebuf[i] == '\n') {
721 /* Don't rely on newline mode, assume raw */
730 return i ? linebuf : 0;
737 char *slash, db[100];
739 static struct utsname kerninfo;
741 if (!*kerninfo.sysname)
752 slash = strrchr(ttyn, '/');
753 if (slash == (char *) 0)
767 (void)setlocale(LC_TIME, Lo);
768 (void)strftime(db, sizeof(db), DF, localtime(&t));
773 puts(kerninfo.sysname);
777 puts(kerninfo.machine);
781 puts(kerninfo.release);
785 puts(kerninfo.version);
798 * Read a gettytab database entry and perform necessary quirks.
804 /* Read the database entry. */
805 gettable(tname, tabent);
808 * Avoid inheriting the parity values from the default entry
809 * if any of them is set in the current entry.
810 * Mixing different parity settings is unreasonable.
812 if (OPset || EPset || APset || NPset)
813 OPset = EPset = APset = NPset = 1;
815 /* Fill in default values for unset capabilities. */