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
;
385 tmode
.sg_flags
|= LCASE
;
387 tmode
.sg_flags
&= ~LCASE
;
389 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
390 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
393 signal(SIGINT
, SIG_DFL
);
394 for (i
= 0; environ
[i
] != NULL
; i
++)
398 limit
.rlim_max
= RLIM_INFINITY
;
399 limit
.rlim_cur
= RLIM_INFINITY
;
400 (void)setrlimit(RLIMIT_CPU
, &limit
);
401 execle(LO
, "login", AL
? "-fp" : "-p", name
,
403 syslog(LOG_ERR
, "%s: %m", LO
);
407 signal(SIGALRM
, SIG_DFL
);
408 signal(SIGINT
, SIG_IGN
);
417 opentty(const char *tty
, int flags
)
419 int failopenlogged
= 0, i
, saved_errno
;
421 while ((i
= open(tty
, flags
)) == -1)
424 if (!failopenlogged
) {
425 syslog(LOG_ERR
, "open %s: %m", tty
);
428 if (saved_errno
== ENOENT
)
432 if (login_tty(i
) < 0) {
433 if (daemon(0,0) < 0) {
434 syslog(LOG_ERR
,"daemon: %m");
438 if (login_tty(i
) < 0) {
439 syslog(LOG_ERR
, "login_tty %s: %m", tty
);
452 /* Start with default tty settings. */
453 if (tcgetattr(STDIN_FILENO
, &tmode
) < 0) {
454 syslog(LOG_ERR
, "tcgetattr %s: %m", ttyn
);
457 omode
= tmode
; /* fill c_cc for dogettytab() */
460 * Don't rely on the driver too much, and initialize crucial
461 * things according to <sys/ttydefaults.h>. Avoid clobbering
462 * the c_cc[] settings however, the console drivers might wish
463 * to leave their idea of the preferred VERASE key value
467 tmode
.c_iflag
= def
.c_iflag
;
468 tmode
.c_oflag
= def
.c_oflag
;
469 tmode
.c_lflag
= def
.c_lflag
;
470 tmode
.c_cflag
= def
.c_cflag
;
472 tmode
.c_cflag
|= CLOCAL
;
481 (void)tcflush(STDIN_FILENO
, TCIOFLUSH
); /* clear out the crap */
482 ioctl(STDIN_FILENO
, FIONBIO
, &off
); /* turn off non-blocking mode */
483 ioctl(STDIN_FILENO
, FIOASYNC
, &off
); /* ditto for async mode */
486 cfsetispeed(&tmode
, speed(IS
));
488 cfsetispeed(&tmode
, speed(SP
));
490 cfsetospeed(&tmode
, speed(OS
));
492 cfsetospeed(&tmode
, speed(SP
));
497 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
498 syslog(LOG_ERR
, "tcsetattr %s: %m", ttyn
);
511 int ppp_connection
= 0;
514 * Interrupt may happen if we use CBREAK mode
516 if (setjmp(intrupt
)) {
517 signal(SIGINT
, SIG_IGN
);
520 signal(SIGINT
, interrupt
);
528 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tmode
) < 0) {
529 syslog(LOG_ERR
, "%s: %m", ttyn
);
532 crmod
= digit
= lower
= upper
= 0;
536 if (read(STDIN_FILENO
, &cs
, 1) <= 0)
538 if ((c
= cs
&0177) == 0)
541 /* PPP detection state machine..
543 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
544 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
546 Derived from code from Michael Hancock, <michaelh@cet.co.jp>
547 and Erik 'PPP' Olson, <eriko@wrq.com>
550 if (PP
&& (cs
== PPP_FRAME
)) {
552 } else if (ppp_state
== 1 && cs
== PPP_STATION
) {
554 } else if (ppp_state
== 2 && cs
== PPP_ESCAPE
) {
556 } else if ((ppp_state
== 2 && cs
== PPP_CONTROL
)
557 || (ppp_state
== 3 && cs
== PPP_CONTROL_ESCAPED
)) {
559 } else if (ppp_state
== 4 && cs
== PPP_LCP_HI
) {
561 } else if (ppp_state
== 5 && cs
== PPP_LCP_LOW
) {
568 if (c
== EOT
|| c
== CTRL('d'))
570 if (c
== '\r' || c
== '\n' || np
>= &name
[sizeof name
-1]) {
578 else if (c
== ERASE
|| c
== '\b' || c
== 0177) {
581 if (cfgetospeed(&tmode
) >= 1200)
587 } else if (c
== KILL
|| c
== CTRL('u')) {
589 if (cfgetospeed(&tmode
) < 1200)
591 /* this is the way they do it down under ... */
595 digit
= lower
= upper
= 0;
598 } else if (isdigit(c
))
600 if (IG
&& (c
<= ' ' || c
> 0176))
605 signal(SIGINT
, SIG_IGN
);
609 if ((upper
&& !lower
&& !LC
) || UC
)
610 for (np
= name
; *np
; np
++)
613 return (1 + ppp_connection
);
617 putpad(const char *s
)
620 speed_t ospeed
= cfgetospeed(&tmode
);
623 while (isdigit(*s
)) {
628 if (*s
== '.' && isdigit(s
[1])) {
636 * If no delay needed, or output speed is
637 * not comprehensible, then don't try to delay.
639 if (pad
== 0 || ospeed
<= 0)
643 * Round up by a half a character frame, and then do the delay.
644 * Too bad there are no user program accessible programmed delays.
645 * Transmitting pad characters slows many terminals down and also
648 pad
= (pad
* ospeed
+ 50000) / 100000;
660 static char outbuf
[OBUFSIZ
];
661 static int obufcnt
= 0;
670 c
|= partab
[c
&0177] & 0200;
675 outbuf
[obufcnt
++] = c
;
676 if (obufcnt
>= OBUFSIZ
)
679 write(STDOUT_FILENO
, &c
, 1);
686 write(STDOUT_FILENO
, outbuf
, obufcnt
);
704 static char linebuf
[512];
707 * This is certainly slow, but it avoids having to include
708 * stdio.h unnecessarily. Issue files should be small anyway.
710 while (i
< (sizeof linebuf
- 3) && read(fd
, linebuf
+i
, 1)==1) {
711 if (linebuf
[i
] == '\n') {
712 /* Don't rely on newline mode, assume raw */
721 return i
? linebuf
: 0;
728 char *slash
, db
[100];
730 static struct utsname kerninfo
;
732 if (!*kerninfo
.sysname
)
743 slash
= strrchr(ttyn
, '/');
758 (void)setlocale(LC_TIME
, Lo
);
759 (void)strftime(db
, sizeof(db
), DF
, localtime(&t
));
764 puts(kerninfo
.sysname
);
768 puts(kerninfo
.machine
);
772 puts(kerninfo
.release
);
776 puts(kerninfo
.version
);
789 * Read a gettytab database entry and perform necessary quirks.
794 /* Read the database entry. */
795 gettable(tname
, tabent
);
798 * Avoid inheriting the parity values from the default entry
799 * if any of them is set in the current entry.
800 * Mixing different parity settings is unreasonable.
802 if (OPset
|| EPset
|| APset
|| NPset
)
803 OPset
= EPset
= APset
= NPset
= 1;
805 /* Fill in default values for unset capabilities. */