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
31 * @(#)from: main.c 8.1 (Berkeley) 6/20/93
32 * $FreeBSD: head/libexec/getty/main.c 329992 2018-02-25 20:15:06Z trasz $
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
38 #include <sys/resource.h>
40 #include <sys/ttydefaults.h>
41 #include <sys/utsname.h>
59 #include "pathnames.h"
62 * Set the amount of running time that getty should accumulate
63 * before deciding that something is wrong and exit.
65 #define GETTY_TIMEOUT 60 /* seconds */
68 #define CTRL(x) (x&037)
70 /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
72 #define PPP_FRAME 0x7e /* PPP Framing character */
73 #define PPP_STATION 0xff /* "All Station" character */
74 #define PPP_ESCAPE 0x7d /* Escape Character */
75 #define PPP_CONTROL 0x03 /* PPP Control Field */
76 #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
77 #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
78 #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
80 /* original mode; flags've been reset using values from <sys/ttydefaults.h> */
85 static int crmod, digit, lower, upper;
87 char hostname[MAXHOSTNAMELEN];
88 static char name[MAXLOGNAME*3];
89 static char dev[] = _PATH_DEV;
95 static char defent[TABBUFSIZ];
96 static char tabent[TABBUFSIZ];
97 static const char *tname;
99 static char *env[128];
101 static char partab[] = {
102 0001,0201,0201,0001,0201,0001,0001,0201,
103 0202,0004,0003,0205,0005,0206,0201,0001,
104 0201,0001,0001,0201,0001,0201,0201,0001,
105 0001,0201,0201,0001,0201,0001,0001,0201,
106 0200,0000,0000,0200,0000,0200,0200,0000,
107 0000,0200,0200,0000,0200,0000,0000,0200,
108 0000,0200,0200,0000,0200,0000,0000,0200,
109 0200,0000,0000,0200,0000,0200,0200,0000,
110 0200,0000,0000,0200,0000,0200,0200,0000,
111 0000,0200,0200,0000,0200,0000,0000,0200,
112 0000,0200,0200,0000,0200,0000,0000,0200,
113 0200,0000,0000,0200,0000,0200,0200,0000,
114 0000,0200,0200,0000,0200,0000,0000,0200,
115 0200,0000,0000,0200,0000,0200,0200,0000,
116 0200,0000,0000,0200,0000,0200,0200,0000,
117 0000,0200,0200,0000,0200,0000,0000,0201
120 #define ERASE tmode.c_cc[VERASE]
121 #define KILL tmode.c_cc[VKILL]
122 #define EOT tmode.c_cc[VEOF]
126 static void defttymode(void);
127 static void dingdong(int);
128 static void dogettytab(void);
129 static int getname(void);
130 static void interrupt(int);
131 static void oflush(void);
132 static void prompt(void);
133 static void putchr(int);
134 static void putf(const char *);
135 static void putpad(const char *);
136 static void puts(const char *);
137 static void timeoverrun(int);
138 static char *get_line(int);
139 static void setttymode(int);
140 static int opentty(const char *, int);
142 static jmp_buf timeout;
145 dingdong(int signo __unused)
151 static jmp_buf intrupt;
154 interrupt(int signo __unused)
160 * Action to take when getty is running too long.
163 timeoverrun(int signo __unused)
166 syslog(LOG_ERR, "getty exiting due to excessive running time");
171 main(int argc, char *argv[])
173 int first_sleep = 1, first_time = 1;
177 signal(SIGINT, SIG_IGN);
178 signal(SIGQUIT, SIG_IGN);
180 openlog("getty", LOG_CONS|LOG_PID, LOG_AUTH);
181 gethostname(hostname, sizeof(hostname) - 1);
182 hostname[sizeof(hostname) - 1] = '\0';
183 if (hostname[0] == '\0')
184 strcpy(hostname, "Amnesiac");
187 * Limit running time to deal with broken or dead lines.
189 (void)signal(SIGXCPU, timeoverrun);
190 limit.rlim_max = RLIM_INFINITY;
191 limit.rlim_cur = GETTY_TIMEOUT;
192 (void)setrlimit(RLIMIT_CPU, &limit);
194 gettable("default", defent);
201 * The following is a work around for vhangup interactions
202 * which cause great problems getting window systems started.
203 * If the tty line is "-", we do the old style getty presuming
204 * that the file descriptors are already set up for us.
205 * J. Gettys - MIT Project Athena.
207 if (argc <= 2 || strcmp(argv[2], "-") == 0)
208 strcpy(ttyn, ttyname(STDIN_FILENO));
211 strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
212 if (strcmp(argv[0], "+") != 0) {
218 * Do the first scan through gettytab.
219 * Terminal mode parameters will be wrong until
220 * defttymode() called, but they're irrelevant for
221 * the initial setup of the terminal device.
226 * Init or answer modem sequence has been specified.
229 if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
236 if (getty_chat(IC, CT, DC) > 0) {
237 syslog(LOG_ERR, "modem init problem on %s", ttyn);
238 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
252 i = select(32, &rfds, NULL, NULL, RT ? &to : NULL);
254 syslog(LOG_ERR, "select %s: %m", ttyn);
256 syslog(LOG_NOTICE, "recycle tty %s", ttyn);
257 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
258 exit(0); /* recycle for init */
260 i = getty_chat(AC, CT, DC);
262 syslog(LOG_ERR, "modem answer problem on %s", ttyn);
263 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
266 } else { /* maybe blocking open */
267 if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
277 * if a delay was specified then sleep for that
278 * number of seconds before writing the initial prompt
280 if (first_sleep && DE) {
282 /* remove any noise */
283 (void)tcflush(STDIN_FILENO, TCIOFLUSH);
294 tname = portselector();
302 /* if this is the first time through this, and an
303 issue file has been given, then send it */
304 if (first_time && IF) {
307 if ((fd = open(IF, O_RDONLY)) != -1) {
310 while ((cp = get_line(fd)) != NULL) {
318 if (IMP && *IMP && !(PL && PP))
320 if (IM && *IM && !(PL && PP))
322 if (setjmp(timeout)) {
323 cfsetispeed(&tmode, B0);
324 cfsetospeed(&tmode, B0);
325 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
329 signal(SIGALRM, dingdong);
338 while (*p && q < &name[sizeof name - 1]) {
341 else if (islower(*p))
343 else if (isdigit(*p))
347 } else if (!(PL && PP))
349 if (rval == 2 || (PL && PP)) {
352 limit.rlim_max = RLIM_INFINITY;
353 limit.rlim_cur = RLIM_INFINITY;
354 (void)setrlimit(RLIMIT_CPU, &limit);
355 execle(PP, "ppplogin", ttyn, NULL, env);
356 syslog(LOG_ERR, "%s: %m", PP);
358 } else if (rval || AL) {
363 signal(SIGALRM, SIG_DFL);
366 if (name[0] == '-') {
367 puts("user names may not start with '-'.");
370 if (!(upper || lower || digit)) {
373 "invalid auto-login name: %s", AL);
380 tmode.c_iflag |= ICRNL;
381 tmode.c_oflag |= ONLCR;
383 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
384 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
387 signal(SIGINT, SIG_DFL);
388 for (i = 0; environ[i] != NULL; i++)
392 limit.rlim_max = RLIM_INFINITY;
393 limit.rlim_cur = RLIM_INFINITY;
394 (void)setrlimit(RLIMIT_CPU, &limit);
395 execle(LO, "login", AL ? "-fp" : "-p", name,
397 syslog(LOG_ERR, "%s: %m", LO);
401 signal(SIGALRM, SIG_DFL);
402 signal(SIGINT, SIG_IGN);
411 opentty(const char *tty, int flags)
413 int failopenlogged = 0, i, saved_errno;
415 while ((i = open(tty, flags)) == -1)
418 if (!failopenlogged) {
419 syslog(LOG_ERR, "open %s: %m", tty);
422 if (saved_errno == ENOENT)
426 if (login_tty(i) < 0) {
427 if (daemon(0,0) < 0) {
428 syslog(LOG_ERR,"daemon: %m");
432 if (login_tty(i) < 0) {
433 syslog(LOG_ERR, "login_tty %s: %m", tty);
446 /* Start with default tty settings. */
447 if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
448 syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
451 omode = tmode; /* fill c_cc for dogettytab() */
454 * Don't rely on the driver too much, and initialize crucial
455 * things according to <sys/ttydefaults.h>. Avoid clobbering
456 * the c_cc[] settings however, the console drivers might wish
457 * to leave their idea of the preferred VERASE key value
461 tmode.c_iflag = def.c_iflag;
462 tmode.c_oflag = def.c_oflag;
463 tmode.c_lflag = def.c_lflag;
464 tmode.c_cflag = def.c_cflag;
466 tmode.c_cflag |= CLOCAL;
475 (void)tcflush(STDIN_FILENO, TCIOFLUSH); /* clear out the crap */
476 ioctl(STDIN_FILENO, FIONBIO, &off); /* turn off non-blocking mode */
477 ioctl(STDIN_FILENO, FIOASYNC, &off); /* ditto for async mode */
480 cfsetispeed(&tmode, speed(IS));
482 cfsetispeed(&tmode, speed(SP));
484 cfsetospeed(&tmode, speed(OS));
486 cfsetospeed(&tmode, speed(SP));
491 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
492 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
505 int ppp_connection = 0;
508 * Interrupt may happen if we use CBREAK mode
510 if (setjmp(intrupt)) {
511 signal(SIGINT, SIG_IGN);
514 signal(SIGINT, interrupt);
522 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
523 syslog(LOG_ERR, "%s: %m", ttyn);
526 crmod = digit = lower = upper = 0;
530 if (read(STDIN_FILENO, &cs, 1) <= 0)
532 if ((c = cs&0177) == 0)
535 /* PPP detection state machine..
537 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
538 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
540 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
541 and Erik 'PPP' Olson, <eriko@wrq.com>
544 if (PP && (cs == PPP_FRAME)) {
546 } else if (ppp_state == 1 && cs == PPP_STATION) {
548 } else if (ppp_state == 2 && cs == PPP_ESCAPE) {
550 } else if ((ppp_state == 2 && cs == PPP_CONTROL)
551 || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
553 } else if (ppp_state == 4 && cs == PPP_LCP_HI) {
555 } else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
562 if (c == EOT || c == CTRL('d'))
564 if (c == '\r' || c == '\n' || np >= &name[sizeof name-1]) {
572 else if (c == ERASE || c == '\b' || c == 0177) {
575 if (cfgetospeed(&tmode) >= 1200)
581 } else if (c == KILL || c == CTRL('u')) {
583 if (cfgetospeed(&tmode) < 1200)
585 /* this is the way they do it down under ... */
589 digit = lower = upper = 0;
592 } else if (isdigit(c))
594 if (IG && (c <= ' ' || c > 0176))
599 signal(SIGINT, SIG_IGN);
603 if ((upper && !lower && !LC) || UC)
604 for (np = name; *np; np++)
607 return (1 + ppp_connection);
611 putpad(const char *s)
614 speed_t ospeed = cfgetospeed(&tmode);
617 while (isdigit(*s)) {
622 if (*s == '.' && isdigit(s[1])) {
630 * If no delay needed, or output speed is
631 * not comprehensible, then don't try to delay.
633 if (pad == 0 || ospeed <= 0)
637 * Round up by a half a character frame, and then do the delay.
638 * Too bad there are no user program accessible programmed delays.
639 * Transmitting pad characters slows many terminals down and also
642 pad = (pad * ospeed + 50000) / 100000;
654 static char outbuf[OBUFSIZ];
655 static int obufcnt = 0;
664 c |= partab[c&0177] & 0200;
669 outbuf[obufcnt++] = c;
670 if (obufcnt >= OBUFSIZ)
673 write(STDOUT_FILENO, &c, 1);
680 write(STDOUT_FILENO, outbuf, obufcnt);
698 static char linebuf[512];
701 * This is certainly slow, but it avoids having to include
702 * stdio.h unnecessarily. Issue files should be small anyway.
704 while (i < (sizeof linebuf - 3) && read(fd, linebuf+i, 1)==1) {
705 if (linebuf[i] == '\n') {
706 /* Don't rely on newline mode, assume raw */
715 return i ? linebuf : 0;
722 char *slash, db[100];
724 static struct utsname kerninfo;
726 if (!*kerninfo.sysname)
737 slash = strrchr(ttyn, '/');
752 (void)setlocale(LC_TIME, Lo);
753 (void)strftime(db, sizeof(db), DF, localtime(&t));
758 puts(kerninfo.sysname);
762 puts(kerninfo.machine);
766 puts(kerninfo.release);
770 puts(kerninfo.version);
783 * Read a gettytab database entry and perform necessary quirks.
788 /* Read the database entry. */
789 gettable(tname, tabent);
792 * Avoid inheriting the parity values from the default entry
793 * if any of them is set in the current entry.
794 * Mixing different parity settings is unreasonable.
796 if (OPset || EPset || APset || NPset)
797 OPset = EPset = APset = NPset = 1;
799 /* Fill in default values for unset capabilities. */