dhcpcd: update README.DRAGONFLY
[dragonfly.git] / libexec / getty / main.c
blobdb92e05621c1bfc3187c913b0da5201c74ab2d90
1 /*-
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
9 * are met:
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
29 * SUCH DAMAGE.
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>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/stat.h>
40 #include <sys/ttydefaults.h>
41 #include <sys/utsname.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <locale.h>
47 #include <libutil.h>
48 #include <setjmp.h>
49 #include <signal.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <termios.h>
54 #include <time.h>
55 #include <unistd.h>
57 #include "gettytab.h"
58 #include "extern.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 */
67 #undef CTRL
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> */
81 struct termios omode;
82 /* current mode */
83 struct termios tmode;
85 static int crmod, digit, lower, upper;
87 char hostname[MAXHOSTNAMELEN];
88 static char name[MAXLOGNAME*3];
89 static char dev[] = _PATH_DEV;
90 static char ttyn[32];
92 #define OBUFSIZ 128
93 #define TABBUFSIZ 512
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]
124 #define puts Gputs
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;
144 static void
145 dingdong(int signo __unused)
147 alarm(0);
148 longjmp(timeout, 1);
151 static jmp_buf intrupt;
153 static void
154 interrupt(int signo __unused)
156 longjmp(intrupt, 1);
160 * Action to take when getty is running too long.
162 static void
163 timeoverrun(int signo __unused)
166 syslog(LOG_ERR, "getty exiting due to excessive running time");
167 exit(1);
171 main(int argc, char *argv[])
173 int first_sleep = 1, first_time = 1;
174 struct rlimit limit;
175 int rval;
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);
195 gendefaults();
196 tname = "default";
197 if (argc > 1)
198 tname = argv[1];
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));
209 else {
210 strcpy(ttyn, dev);
211 strncat(ttyn, argv[2], sizeof(ttyn)-sizeof(dev));
212 if (strcmp(argv[0], "+") != 0) {
213 chown(ttyn, 0, 0);
214 chmod(ttyn, 0600);
215 revoke(ttyn);
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.
223 dogettytab();
226 * Init or answer modem sequence has been specified.
228 if (IC || AC) {
229 if (!opentty(ttyn, O_RDWR|O_NONBLOCK))
230 exit(1);
231 defttymode();
232 setttymode(1);
235 if (IC) {
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);
239 exit(1);
243 if (AC) {
244 fd_set rfds;
245 struct timeval to;
246 int i;
248 FD_ZERO(&rfds);
249 FD_SET(0, &rfds);
250 to.tv_sec = RT;
251 to.tv_usec = 0;
252 i = select(32, &rfds, NULL, NULL, RT ? &to : NULL);
253 if (i < 0) {
254 syslog(LOG_ERR, "select %s: %m", ttyn);
255 } else if (i == 0) {
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);
261 if (i > 0) {
262 syslog(LOG_ERR, "modem answer problem on %s", ttyn);
263 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
264 exit(1);
266 } else { /* maybe blocking open */
267 if (!opentty(ttyn, O_RDWR | (NC ? O_NONBLOCK : 0 )))
268 exit(1);
273 defttymode();
274 for (;;) {
277 * if a delay was specified then sleep for that
278 * number of seconds before writing the initial prompt
280 if (first_sleep && DE) {
281 sleep(DE);
282 /* remove any noise */
283 (void)tcflush(STDIN_FILENO, TCIOFLUSH);
285 first_sleep = 0;
287 setttymode(0);
288 if (AB) {
289 tname = autobaud();
290 dogettytab();
291 continue;
293 if (PS) {
294 tname = portselector();
295 dogettytab();
296 continue;
298 if (CL && *CL)
299 putpad(CL);
300 edithost(HE);
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) {
305 int fd;
307 if ((fd = open(IF, O_RDONLY)) != -1) {
308 char * cp;
310 while ((cp = get_line(fd)) != NULL) {
311 putf(cp);
313 close(fd);
316 first_time = 0;
318 if (IMP && *IMP && !(PL && PP))
319 system(IMP);
320 if (IM && *IM && !(PL && PP))
321 putf(IM);
322 if (setjmp(timeout)) {
323 cfsetispeed(&tmode, B0);
324 cfsetospeed(&tmode, B0);
325 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tmode);
326 exit(1);
328 if (TO) {
329 signal(SIGALRM, dingdong);
330 alarm(TO);
333 rval = 0;
334 if (AL) {
335 const char *p = AL;
336 char *q = name;
338 while (*p && q < &name[sizeof name - 1]) {
339 if (isupper(*p))
340 upper = 1;
341 else if (islower(*p))
342 lower = 1;
343 else if (isdigit(*p))
344 digit = 1;
345 *q++ = *p++;
347 } else if (!(PL && PP))
348 rval = getname();
349 if (rval == 2 || (PL && PP)) {
350 oflush();
351 alarm(0);
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);
357 exit(1);
358 } else if (rval || AL) {
359 int i;
361 oflush();
362 alarm(0);
363 signal(SIGALRM, SIG_DFL);
364 if (name[0] == '\0')
365 continue;
366 if (name[0] == '-') {
367 puts("user names may not start with '-'.");
368 continue;
370 if (!(upper || lower || digit)) {
371 if (AL) {
372 syslog(LOG_ERR,
373 "invalid auto-login name: %s", AL);
374 exit(1);
375 } else
376 continue;
378 set_flags(2);
379 if (crmod) {
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);
385 exit(1);
387 signal(SIGINT, SIG_DFL);
388 for (i = 0; environ[i] != NULL; i++)
389 env[i] = environ[i];
390 makeenv(&env[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,
396 NULL, env);
397 syslog(LOG_ERR, "%s: %m", LO);
398 exit(1);
400 alarm(0);
401 signal(SIGALRM, SIG_DFL);
402 signal(SIGINT, SIG_IGN);
403 if (NX && *NX) {
404 tname = NX;
405 dogettytab();
410 static int
411 opentty(const char *tty, int flags)
413 int failopenlogged = 0, i, saved_errno;
415 while ((i = open(tty, flags)) == -1)
417 saved_errno = errno;
418 if (!failopenlogged) {
419 syslog(LOG_ERR, "open %s: %m", tty);
420 failopenlogged = 1;
422 if (saved_errno == ENOENT)
423 return 0;
424 sleep(60);
426 if (login_tty(i) < 0) {
427 if (daemon(0,0) < 0) {
428 syslog(LOG_ERR,"daemon: %m");
429 close(i);
430 return 0;
432 if (login_tty(i) < 0) {
433 syslog(LOG_ERR, "login_tty %s: %m", tty);
434 close(i);
435 return 0;
438 return 1;
441 static void
442 defttymode(void)
444 struct termios def;
446 /* Start with default tty settings. */
447 if (tcgetattr(STDIN_FILENO, &tmode) < 0) {
448 syslog(LOG_ERR, "tcgetattr %s: %m", ttyn);
449 exit(1);
451 omode = tmode; /* fill c_cc for dogettytab() */
452 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
458 * there.
460 cfmakesane(&def);
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;
465 if (NC)
466 tmode.c_cflag |= CLOCAL;
467 omode = tmode;
470 static void
471 setttymode(int raw)
473 int off = 0;
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 */
479 if (IS)
480 cfsetispeed(&tmode, speed(IS));
481 else if (SP)
482 cfsetispeed(&tmode, speed(SP));
483 if (OS)
484 cfsetospeed(&tmode, speed(OS));
485 else if (SP)
486 cfsetospeed(&tmode, speed(SP));
487 set_flags(0);
488 setchars();
489 if (raw)
490 cfmakeraw(&tmode);
491 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
492 syslog(LOG_ERR, "tcsetattr %s: %m", ttyn);
493 exit(1);
498 static int
499 getname(void)
501 int c;
502 char *np;
503 unsigned char cs;
504 int ppp_state = 0;
505 int ppp_connection = 0;
508 * Interrupt may happen if we use CBREAK mode
510 if (setjmp(intrupt)) {
511 signal(SIGINT, SIG_IGN);
512 return (0);
514 signal(SIGINT, interrupt);
515 set_flags(1);
516 prompt();
517 oflush();
518 if (PF > 0) {
519 sleep(PF);
520 PF = 0;
522 if (tcsetattr(STDIN_FILENO, TCSANOW, &tmode) < 0) {
523 syslog(LOG_ERR, "%s: %m", ttyn);
524 exit(1);
526 crmod = digit = lower = upper = 0;
527 np = name;
528 for (;;) {
529 oflush();
530 if (read(STDIN_FILENO, &cs, 1) <= 0)
531 exit(0);
532 if ((c = cs&0177) == 0)
533 return (0);
535 /* PPP detection state machine..
536 Look for sequences:
537 PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
538 PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
539 See RFC1662.
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)) {
545 ppp_state = 1;
546 } else if (ppp_state == 1 && cs == PPP_STATION) {
547 ppp_state = 2;
548 } else if (ppp_state == 2 && cs == PPP_ESCAPE) {
549 ppp_state = 3;
550 } else if ((ppp_state == 2 && cs == PPP_CONTROL)
551 || (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
552 ppp_state = 4;
553 } else if (ppp_state == 4 && cs == PPP_LCP_HI) {
554 ppp_state = 5;
555 } else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
556 ppp_connection = 1;
557 break;
558 } else {
559 ppp_state = 0;
562 if (c == EOT || c == CTRL('d'))
563 exit(0);
564 if (c == '\r' || c == '\n' || np >= &name[sizeof name-1]) {
565 putf("\r\n");
566 break;
568 if (islower(c))
569 lower = 1;
570 else if (isupper(c))
571 upper = 1;
572 else if (c == ERASE || c == '\b' || c == 0177) {
573 if (np > name) {
574 np--;
575 if (cfgetospeed(&tmode) >= 1200)
576 puts("\b \b");
577 else
578 putchr(cs);
580 continue;
581 } else if (c == KILL || c == CTRL('u')) {
582 putchr('\r');
583 if (cfgetospeed(&tmode) < 1200)
584 putchr('\n');
585 /* this is the way they do it down under ... */
586 else if (np > name)
587 puts(" \r");
588 prompt();
589 digit = lower = upper = 0;
590 np = name;
591 continue;
592 } else if (isdigit(c))
593 digit = 1;
594 if (IG && (c <= ' ' || c > 0176))
595 continue;
596 *np++ = c;
597 putchr(cs);
599 signal(SIGINT, SIG_IGN);
600 *np = 0;
601 if (c == '\r')
602 crmod = 1;
603 if ((upper && !lower && !LC) || UC)
604 for (np = name; *np; np++)
605 if (isupper(*np))
606 *np = tolower(*np);
607 return (1 + ppp_connection);
610 static void
611 putpad(const char *s)
613 int pad = 0;
614 speed_t ospeed = cfgetospeed(&tmode);
616 if (isdigit(*s)) {
617 while (isdigit(*s)) {
618 pad *= 10;
619 pad += *s++ - '0';
621 pad *= 10;
622 if (*s == '.' && isdigit(s[1])) {
623 pad += s[1] - '0';
624 s += 2;
628 puts(s);
630 * If no delay needed, or output speed is
631 * not comprehensible, then don't try to delay.
633 if (pad == 0 || ospeed <= 0)
634 return;
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
640 * loads the system.
642 pad = (pad * ospeed + 50000) / 100000;
643 while (pad--)
644 putchr(*PC);
647 static void
648 puts(const char *s)
650 while (*s)
651 putchr(*s++);
654 static char outbuf[OBUFSIZ];
655 static int obufcnt = 0;
657 static void
658 putchr(int cc)
660 char c;
662 c = cc;
663 if (!NP) {
664 c |= partab[c&0177] & 0200;
665 if (OP)
666 c ^= 0200;
668 if (!UB) {
669 outbuf[obufcnt++] = c;
670 if (obufcnt >= OBUFSIZ)
671 oflush();
672 } else
673 write(STDOUT_FILENO, &c, 1);
676 static void
677 oflush(void)
679 if (obufcnt)
680 write(STDOUT_FILENO, outbuf, obufcnt);
681 obufcnt = 0;
684 static void
685 prompt(void)
688 putf(LM);
689 if (CO)
690 putchr('\n');
694 static char *
695 get_line(int fd)
697 size_t i = 0;
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 */
707 linebuf[i++] = '\r';
708 linebuf[i++] = '\n';
709 linebuf[i] = '\0';
710 return linebuf;
712 ++i;
714 linebuf[i] = '\0';
715 return i ? linebuf : 0;
718 static void
719 putf(const char *cp)
721 time_t t;
722 char *slash, db[100];
724 static struct utsname kerninfo;
726 if (!*kerninfo.sysname)
727 uname(&kerninfo);
729 while (*cp) {
730 if (*cp != '%') {
731 putchr(*cp++);
732 continue;
734 switch (*++cp) {
736 case 't':
737 slash = strrchr(ttyn, '/');
738 if (slash == NULL)
739 puts(ttyn);
740 else
741 puts(&slash[1]);
742 break;
744 case 'h':
745 puts(editedhost);
746 break;
748 case 'd': {
749 t = (time_t)0;
750 (void)time(&t);
751 if (Lo)
752 (void)setlocale(LC_TIME, Lo);
753 (void)strftime(db, sizeof(db), DF, localtime(&t));
754 puts(db);
755 break;
757 case 's':
758 puts(kerninfo.sysname);
759 break;
761 case 'm':
762 puts(kerninfo.machine);
763 break;
765 case 'r':
766 puts(kerninfo.release);
767 break;
769 case 'v':
770 puts(kerninfo.version);
771 break;
774 case '%':
775 putchr('%');
776 break;
778 cp++;
783 * Read a gettytab database entry and perform necessary quirks.
785 static void
786 dogettytab(void)
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. */
800 setdefaults();