1 /* vi: set sw=4 ts=4: */
2 /* agetty.c - another getty program for Linux. By W. Z. Venema 1989
3 * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk>
4 * This program is freely distributable. The entire man-page used to
5 * be here. Now read the real man-page agetty.8 instead.
7 * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95
9 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
10 * - added Native Language Support
12 * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
13 * - enable hardware flow control before displaying /etc/issue
15 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
22 #ifdef CONFIG_FEATURE_UTMP
26 #define _PATH_LOGIN "/bin/login"
29 #include <sys/param.h>
34 * Some heuristics to find out what environment we are in: if it is not
35 * System V, assume it is SunOS 4.
38 #ifdef LOGIN_PROCESS /* defined in System V utmp.h */
39 #define SYSV_STYLE /* select System V style getty */
40 #ifdef CONFIG_FEATURE_WTMP
41 extern void updwtmp(const char *filename, const struct utmp *ut);
43 #endif /* LOGIN_PROCESS */
46 * Things you may want to modify.
48 * You may disagree with the default line-editing etc. characters defined
49 * below. Note, however, that DEL cannot be used for interrupt generation
50 * and for line editing at the same time.
54 #include <sys/utsname.h>
58 /* If ISSUE is not defined, agetty will never display the contents of the
59 * /etc/issue file. You will not want to spit out large "issue" files at the
62 #define ISSUE "/etc/issue" /* displayed before the login prompt */
64 /* Some shorthands for control characters. */
66 #define CTL(x) (x ^ 0100) /* Assumes ASCII dialect */
67 #define CR CTL('M') /* carriage return */
68 #define NL CTL('J') /* line feed */
69 #define BS CTL('H') /* back space */
70 #define DEL CTL('?') /* delete */
72 /* Defaults for line-editing etc. characters; you may want to change this. */
74 #define DEF_ERASE DEL /* default erase character */
75 #define DEF_INTR CTL('C') /* default interrupt character */
76 #define DEF_QUIT CTL('\\') /* default quit char */
77 #define DEF_KILL CTL('U') /* default kill char */
78 #define DEF_EOF CTL('D') /* default EOF char */
80 #define DEF_SWITCH 0 /* default switch char */
83 * SunOS 4.1.1 termio is broken. We must use the termios stuff instead,
84 * because the termio -> termios translation does not clear the termios
85 * CIBAUD bits. Therefore, the tty driver would sometimes report that input
86 * baud rate != output baud rate. I did not notice that problem with SunOS
87 * 4.1. We will use termios where available, and termio otherwise.
90 /* linux 0.12 termio is broken too, if we use it c_cc[VERASE] isn't set
91 properly, but all is well if we use termios?! */
97 #define termio termios
100 #define TCSETAW TCSETSW
104 * When multiple baud rates are specified on the command line, the first one
105 * we will try is the first one specified.
108 #define FIRST_SPEED 0
110 /* Storage for command-line options. */
112 #define MAX_SPEED 10 /* max. nr. of baud rates */
115 int flags; /* toggle switches, see below */
116 unsigned timeout; /* time-out period */
117 char *login; /* login program */
118 char *tty; /* name of tty */
119 char *initstring; /* modem init string */
120 char *issue; /* alternative issue file */
121 int numspeed; /* number of baud rates to try */
122 int speeds[MAX_SPEED]; /* baud rates to be tried */
125 static const char opt_string[] = "I:LH:f:hil:mt:wn";
126 #define F_INITSTRING (1<<0) /* initstring is set */
127 #define F_LOCAL (1<<1) /* force local */
128 #define F_FAKEHOST (1<<2) /* force fakehost */
129 #define F_CUSTISSUE (1<<3) /* give alternative issue file */
130 #define F_RTSCTS (1<<4) /* enable RTS/CTS flow control */
131 #define F_ISSUE (1<<5) /* display /etc/issue */
132 #define F_LOGIN (1<<6) /* non-default login program */
133 #define F_PARSE (1<<7) /* process modem status messages */
134 #define F_TIMEOUT (1<<8) /* time out */
135 #define F_WAITCRLF (1<<9) /* wait for CR or LF */
136 #define F_NOPROMPT (1<<10) /* don't ask for login name! */
138 /* Storage for things detected while the login name was read. */
141 int erase; /* erase character */
142 int kill; /* kill character */
143 int eol; /* end-of-line character */
144 int parity; /* what parity did we see */
145 int capslock; /* upper case without lower case */
148 /* Initial values for the above. */
150 static struct chardata init_chardata = {
151 DEF_ERASE, /* default erase character */
152 DEF_KILL, /* default kill character */
153 13, /* default eol char */
154 0, /* space parity */
164 static struct Speedtab speedtab[] = {
205 #ifdef CONFIG_FEATURE_UTMP
206 static void update_utmp(char *line);
210 /* The following is used for understandable diagnostics. */
212 /* Fake hostname for ut_host specified on command line. */
213 static char *fakehost = NULL;
217 #define debug(s) fprintf(dbf,s); fflush(dbf)
218 #define DEBUGTERM "/dev/ttyp0"
221 #define debug(s) /* nothing */
225 /* bcode - convert speed string to speed code; return 0 on failure */
226 static int bcode(const char *s)
230 if (safe_strtou((char *)s, &value)) {
233 r = tty_value_to_baud(value);
241 /* parse_speeds - parse alternate baud rates */
242 static void parse_speeds(struct options *op, char *arg)
246 debug("entered parse_speeds\n");
247 for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) {
248 if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
249 bb_error_msg_and_die("bad speed: %s", cp);
250 if (op->numspeed > MAX_SPEED)
251 bb_error_msg_and_die("too many alternate speeds");
253 debug("exiting parsespeeds\n");
257 /* parse_args - parse command-line arguments */
258 static void parse_args(int argc, char **argv, struct options *op)
262 op->flags = getopt32(argc, argv, opt_string,
263 &(op->initstring), &fakehost, &(op->issue),
265 if(op->flags & F_INITSTRING) {
266 const char *p = op->initstring;
269 q = op->initstring = xstrdup(op->initstring);
270 /* copy optarg into op->initstring decoding \ddd
271 octal codes into chars */
275 *q++ = bb_process_escape_sequence(&p);
282 op->flags ^= F_ISSUE; /* revert flag show /etc/issue */
283 if(op->flags & F_TIMEOUT) {
284 op->timeout = xatoul_range(ts, 1, INT_MAX);
286 debug("after getopt loop\n");
287 if (argc < optind + 2) /* check parameter count */
290 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
291 if ('0' <= argv[optind][0] && argv[optind][0] <= '9') {
292 /* a number first, assume it's a speed (BSD style) */
293 parse_speeds(op, argv[optind++]); /* baud rate(s) */
294 op->tty = argv[optind]; /* tty name */
296 op->tty = argv[optind++]; /* tty name */
297 parse_speeds(op, argv[optind]); /* baud rate(s) */
301 if (argc > optind && argv[optind])
302 setenv("TERM", argv[optind], 1);
304 debug("exiting parseargs\n");
307 static void xdup2(int srcfd, int dstfd, const char *tty)
309 if(dup2(srcfd, dstfd) == -1)
310 bb_perror_msg_and_die("%s: dup", tty);
313 /* open_tty - set up tty as standard { input, output, error } */
314 static void open_tty(char *tty, struct termio *tp, int local)
316 int chdir_to_root = 0;
318 /* Set up new standard input, unless we are given an already opened port. */
320 if (strcmp(tty, "-")) {
324 /* Sanity checks... */
329 if ((st.st_mode & S_IFMT) != S_IFCHR)
330 bb_error_msg_and_die("%s: not a character device", tty);
332 /* Open the tty as standard input. */
335 fd = xopen(tty, O_RDWR | O_NONBLOCK);
342 * Standard input should already be connected to an open port. Make
343 * sure it is open for read/write.
346 if ((fcntl(0, F_GETFL, 0) & O_RDWR) != O_RDWR)
347 bb_error_msg_and_die("%s: not open for read/write", tty);
350 /* Replace current standard output/error fd's with new ones */
356 * The following ioctl will fail if stdin is not a tty, but also when
357 * there is noise on the modem control lines. In the latter case, the
358 * common course of action is (1) fix your cables (2) give the modem more
359 * time to properly reset after hanging up. SunOS users can achieve (2)
360 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
361 * 5 seconds seems to be a good value.
364 if (ioctl(0, TCGETA, tp) < 0)
365 bb_perror_msg_and_die("%s: ioctl(TCGETA)", tty);
368 * It seems to be a terminal. Set proper protections and ownership. Mode
369 * 0622 is suitable for SYSV <4 because /bin/login does not change
370 * protections. SunOS 4 login will change the protections to 0620 (write
371 * access for group tty) after the login has succeeded.
376 /* tty to root.dialout 660 */
380 id = (gr = getgrnam("dialout")) ? gr->gr_gid : 0;
384 /* vcs,vcsa to root.sys 600 */
385 if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) {
389 vcsa = xmalloc(strlen(tty) + 2);
391 strcpy(vcs + 3, tty + 3);
392 strcpy(vcsa, "vcsa");
393 strcpy(vcsa + 4, tty + 3);
395 id = (gr = getgrnam("sys")) ? gr->gr_gid : 0;
406 (void) chown(tty, 0, 0); /* root, sys */
407 (void) chmod(tty, 0622); /* crw--w--w- */
413 /* termio_init - initialize termio settings */
414 static void termio_init(struct termio *tp, int speed, struct options *op)
417 * Initial termio settings: 8-bit characters, raw-mode, blocking i/o.
418 * Special characters are set after we have read the login name; all
419 * reads will be done in raw mode anyway. Errors will be dealt with
423 /* flush input and output queues, important for modems! */
424 (void) ioctl(0, TCFLSH, TCIOFLUSH);
427 tp->c_cflag = CS8 | HUPCL | CREAD | speed;
428 if (op->flags & F_LOCAL) {
429 tp->c_cflag |= CLOCAL;
432 tp->c_iflag = tp->c_lflag = tp->c_line = 0;
433 tp->c_oflag = OPOST | ONLCR;
437 /* Optionally enable hardware flow control */
440 if (op->flags & F_RTSCTS)
441 tp->c_cflag |= CRTSCTS;
444 (void) ioctl(0, TCSETA, tp);
446 /* go to blocking input even in local mode */
447 fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK);
449 debug("term_io 2\n");
452 /* auto_baud - extract baud rate from modem status message */
453 static void auto_baud(struct termio *tp)
463 * This works only if the modem produces its status code AFTER raising
464 * the DCD line, and if the computer is fast enough to set the proper
465 * baud rate before the message has gone by. We expect a message of the
468 * <junk><number><junk>
470 * The number is interpreted as the baud rate of the incoming call. If the
471 * modem does not tell us the baud rate within one second, we will keep
472 * using the current baud rate. It is advisable to enable BREAK
473 * processing (comma-separated list of baud rates) if the processing of
474 * modem status messages is enabled.
478 * Use 7-bit characters, don't block if input queue is empty. Errors will
479 * be dealt with lateron.
483 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
484 vmin = tp->c_cc[VMIN];
485 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
486 (void) ioctl(0, TCSETA, tp);
489 * Wait for a while, then read everything the modem has said so far and
490 * try to extract the speed of the dial-in call.
494 if ((nread = read(0, buf, sizeof(buf) - 1)) > 0) {
496 for (bp = buf; bp < buf + nread; bp++) {
497 if (isascii(*bp) && isdigit(*bp)) {
500 tp->c_cflag &= ~CBAUD;
501 tp->c_cflag |= speed;
507 /* Restore terminal settings. Errors will be dealt with lateron. */
510 tp->c_cc[VMIN] = vmin;
511 (void) ioctl(0, TCSETA, tp);
514 /* next_speed - select next baud rate */
515 static void next_speed(struct termio *tp, struct options *op)
517 static int baud_index = FIRST_SPEED; /* current speed index */
519 baud_index = (baud_index + 1) % op->numspeed;
520 tp->c_cflag &= ~CBAUD;
521 tp->c_cflag |= op->speeds[baud_index];
522 (void) ioctl(0, TCSETA, tp);
526 /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
527 static void do_prompt(struct options *op, struct termio *tp)
529 #ifdef ISSUE /* optional: show /etc/issue */
530 print_login_issue(op->issue, op->tty);
532 print_login_prompt();
535 /* caps_lock - string contains upper case without lower case */
536 /* returns 1 if true, 0 if false */
537 static int caps_lock(const char *s)
541 for (capslock = 0; *s; s++) {
545 capslock = isupper(*s);
550 #define logname bb_common_bufsiz1
551 /* get_logname - get user name, establish parity, speed, erase, kill, eol */
552 /* return NULL on failure, logname on success */
553 static char *get_logname(struct options *op, struct chardata *cp, struct termio *tp)
556 char c; /* input character, full eight bits */
557 char ascval; /* low 7 bits of input character */
558 int bits; /* # of "1" bits per character */
559 int mask; /* mask with 1 bit up */
560 static char *erase[] = { /* backspace-space-backspace */
561 "\010\040\010", /* space parity */
562 "\010\040\010", /* odd parity */
563 "\210\240\210", /* even parity */
564 "\210\240\210", /* no parity */
567 /* Initialize kill, erase, parity etc. (also after switching speeds). */
571 /* Flush pending input (esp. after parsing or switching the baud rate). */
574 (void) ioctl(0, TCFLSH, TCIFLUSH);
576 /* Prompt for and read a login name. */
578 for (*logname = 0; *logname == 0; /* void */ ) {
580 /* Write issue file and prompt, with "parity" bit == 0. */
584 /* Read name, watch for break, parity, erase, kill, end-of-line. */
586 for (bp = logname, cp->eol = 0; cp->eol == 0; /* void */ ) {
588 /* Do not report trivial EINTR/EIO errors. */
590 if (read(0, &c, 1) < 1) {
591 if (errno == EINTR || errno == EIO)
593 bb_perror_msg_and_die("%s: read", op->tty);
595 /* Do BREAK handling elsewhere. */
597 if ((c == 0) && op->numspeed > 1)
601 /* Do parity bit handling. */
603 if (c != (ascval = (c & 0177))) { /* "parity" bit on ? */
604 for (bits = 1, mask = 1; mask & 0177; mask <<= 1)
606 bits++; /* count "1" bits */
607 cp->parity |= ((bits & 1) ? 1 : 2);
609 /* Do erase, kill and end-of-line processing. */
614 *bp = 0; /* terminate logname */
615 cp->eol = ascval; /* set end-of-line char */
620 cp->erase = ascval; /* set erase character */
622 (void) write(1, erase[cp->parity], 3);
628 cp->kill = ascval; /* set kill character */
629 while (bp > logname) {
630 (void) write(1, erase[cp->parity], 3);
637 if (!isascii(ascval) || !isprint(ascval)) {
638 /* ignore garbage characters */ ;
639 } else if (bp - logname >= sizeof(logname) - 1) {
640 bb_error_msg_and_die("%s: input overrun", op->tty);
642 (void) write(1, &c, 1); /* echo the character */
643 *bp++ = ascval; /* and store it */
649 /* Handle names with upper case and no lower case. */
651 if ((cp->capslock = caps_lock(logname))) {
652 for (bp = logname; *bp; bp++)
654 *bp = tolower(*bp); /* map name to lower case */
659 /* termio_final - set the final tty mode bits */
660 static void termio_final(struct options *op, struct termio *tp, struct chardata *cp)
662 /* General terminal-independent stuff. */
664 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
665 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
666 /* no longer| ECHOCTL | ECHOPRT */
667 tp->c_oflag |= OPOST;
668 /* tp->c_cflag = 0; */
669 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
670 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
671 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
672 tp->c_cc[VEOL] = DEF_EOL;
673 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
675 /* Account for special characters seen in input. */
678 tp->c_iflag |= ICRNL; /* map CR in input to NL */
679 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
681 tp->c_cc[VERASE] = cp->erase; /* set erase character */
682 tp->c_cc[VKILL] = cp->kill; /* set kill character */
684 /* Account for the presence or absence of parity bits in input. */
686 switch (cp->parity) {
687 case 0: /* space (always 0) parity */
689 case 1: /* odd parity */
690 tp->c_cflag |= PARODD;
692 case 2: /* even parity */
693 tp->c_cflag |= PARENB;
694 tp->c_iflag |= INPCK | ISTRIP;
696 case (1 | 2): /* no parity bit */
697 tp->c_cflag &= ~CSIZE;
701 /* Account for upper case without lower case. */
704 tp->c_iflag |= IUCLC;
705 tp->c_lflag |= XCASE;
706 tp->c_oflag |= OLCUC;
708 /* Optionally enable hardware flow control */
711 if (op->flags & F_RTSCTS)
712 tp->c_cflag |= CRTSCTS;
715 /* Finally, make the new settings effective */
717 if (ioctl(0, TCSETA, tp) < 0)
718 bb_perror_msg_and_die("%s: ioctl(TCSETA)", op->tty);
723 #ifdef CONFIG_FEATURE_UTMP
724 /* update_utmp - update our utmp entry */
725 static void update_utmp(char *line)
730 int mypid = getpid();
731 #if ! (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1))
736 * The utmp file holds miscellaneous information about things started by
737 * /sbin/init and other system-related events. Our purpose is to update
738 * the utmp entry for the current process, in particular the process type
739 * and the tty line we are listening to. Return successfully only if the
740 * utmp file can be opened for update, and if we are able to find our
741 * entry in the utmp file.
743 if (access(_PATH_UTMP, R_OK|W_OK) == -1) {
744 close(creat(_PATH_UTMP, 0664));
746 utmpname(_PATH_UTMP);
748 while ((utp = getutent())
749 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid)) /* nothing */
753 memcpy(&ut, utp, sizeof(ut));
755 /* some inits don't initialize utmp... */
756 memset(&ut, 0, sizeof(ut));
757 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
761 strcpy(ut.ut_user, "LOGIN");
762 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
764 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
767 ut.ut_type = LOGIN_PROCESS;
773 #ifdef CONFIG_FEATURE_WTMP
774 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1)
775 close(creat(bb_path_wtmp_file, 0664));
776 updwtmp(bb_path_wtmp_file, &ut);
780 #endif /* CONFIG_FEATURE_UTMP */
781 #endif /* SYSV_STYLE */
785 int getty_main(int argc, char **argv)
788 char *logname = NULL; /* login name, given to /bin/login */
789 struct chardata chardata; /* set by get_logname() */
790 struct termio termio; /* terminal mode bits */
791 static struct options options = {
792 0, /* show /etc/issue (SYSV_STYLE) */
794 _PATH_LOGIN, /* default login program */
795 "tty1", /* default tty line */
796 "", /* modem init string */
798 ISSUE, /* default issue file */
802 0, /* no baud rates known yet */
805 /* Already too late because of theoretical
806 * possibility of getty --help somehow triggered
807 * inadvertently before we reach this. Oh well. */
814 /* We want special flavor of error_msg_and_die */
817 /* Was "/dev/console". Why should we spam *system console*
818 * if there is a problem with getty on /dev/ttyS15?... */
819 nullfd = xopen(bb_dev_null, O_RDWR);
825 openlog(applet_name, LOG_PID, LOG_AUTH);
826 logmode = LOGMODE_BOTH;
829 dbf = xfopen(DEBUGTERM, "w");
834 for (i = 1; i < argc; i++) {
841 /* Parse command-line arguments. */
842 parse_args(argc, argv, &options);
845 #ifdef CONFIG_FEATURE_UTMP
846 /* Update the utmp file. */
847 update_utmp(options.tty);
851 debug("calling open_tty\n");
852 /* Open the tty as standard { input, output, error }. */
853 open_tty(options.tty, &termio, options.flags & F_LOCAL);
860 ioctl(0, TIOCSPGRP, &iv);
863 /* Initialize the termio settings (raw mode, eight-bit, blocking i/o). */
864 debug("calling termio_init\n");
865 termio_init(&termio, options.speeds[FIRST_SPEED], &options);
867 /* write the modem init string and DON'T flush the buffers */
868 if (options.flags & F_INITSTRING) {
869 debug("writing init string\n");
870 write(1, options.initstring, strlen(options.initstring));
873 if (!(options.flags & F_LOCAL)) {
874 /* go to blocking write mode unless -L is specified */
875 fcntl(1, F_SETFL, fcntl(1, F_GETFL, 0) & ~O_NONBLOCK);
878 /* Optionally detect the baud rate from the modem status message. */
879 debug("before autobaud\n");
880 if (options.flags & F_PARSE)
883 /* Set the optional timer. */
885 (void) alarm(options.timeout);
887 /* optionally wait for CR or LF before writing /etc/issue */
888 if (options.flags & F_WAITCRLF) {
891 debug("waiting for cr-lf\n");
892 while (read(0, &ch, 1) == 1) {
893 ch &= 0x7f; /* strip "parity bit" */
895 fprintf(dbf, "read %c\n", ch);
897 if (ch == '\n' || ch == '\r')
902 chardata = init_chardata;
903 if (!(options.flags & F_NOPROMPT)) {
904 /* Read the login name. */
905 debug("reading login name\n");
906 /* while ((logname = get_logname(&options, &chardata, &termio)) == 0) */
907 logname = get_logname(&options, &chardata, &termio);
908 while (logname == NULL)
909 next_speed(&termio, &options);
917 /* Finalize the termio settings. */
919 termio_final(&options, &termio, &chardata);
921 /* Now the newline character should be properly written. */
923 (void) write(1, "\n", 1);
925 /* Let the login program take care of password validation. */
927 (void) execl(options.login, options.login, "--", logname, (char *) 0);
928 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);