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 #if ENABLE_FEATURE_UTMP
27 * Some heuristics to find out what environment we are in: if it is not
28 * System V, assume it is SunOS 4.
30 #ifdef LOGIN_PROCESS /* defined in System V utmp.h */
31 #define SYSV_STYLE /* select System V style getty */
32 #include <sys/utsname.h>
34 #if ENABLE_FEATURE_WTMP
35 extern void updwtmp(const char *filename, const struct utmp *ut);
36 static void update_utmp(char *line);
38 #endif /* LOGIN_PROCESS */
41 * Things you may want to modify.
43 * You may disagree with the default line-editing etc. characters defined
44 * below. Note, however, that DEL cannot be used for interrupt generation
45 * and for line editing at the same time.
48 /* I doubt there are systems which still need this */
51 #define _PATH_LOGIN "/bin/login"
53 /* If ISSUE is not defined, getty will never display the contents of the
54 * /etc/issue file. You will not want to spit out large "issue" files at the
57 #define ISSUE "/etc/issue" /* displayed before the login prompt */
59 /* Some shorthands for control characters. */
60 #define CTL(x) (x ^ 0100) /* Assumes ASCII dialect */
61 #define CR CTL('M') /* carriage return */
62 #define NL CTL('J') /* line feed */
63 #define BS CTL('H') /* back space */
64 #define DEL CTL('?') /* delete */
66 /* Defaults for line-editing etc. characters; you may want to change this. */
67 #define DEF_ERASE DEL /* default erase character */
68 #define DEF_INTR CTL('C') /* default interrupt character */
69 #define DEF_QUIT CTL('\\') /* default quit char */
70 #define DEF_KILL CTL('U') /* default kill char */
71 #define DEF_EOF CTL('D') /* default EOF char */
73 #define DEF_SWITCH 0 /* default switch char */
76 * When multiple baud rates are specified on the command line, the first one
77 * we will try is the first one specified.
81 /* Storage for command-line options. */
83 #define MAX_SPEED 10 /* max. nr. of baud rates */
86 int flags; /* toggle switches, see below */
87 unsigned timeout; /* time-out period */
88 char *login; /* login program */
89 char *tty; /* name of tty */
90 char *initstring; /* modem init string */
91 char *issue; /* alternative issue file */
92 int numspeed; /* number of baud rates to try */
93 int speeds[MAX_SPEED]; /* baud rates to be tried */
96 static const char opt_string[] = "I:LH:f:hil:mt:wn";
97 #define F_INITSTRING (1<<0) /* initstring is set */
98 #define F_LOCAL (1<<1) /* force local */
99 #define F_FAKEHOST (1<<2) /* force fakehost */
100 #define F_CUSTISSUE (1<<3) /* give alternative issue file */
101 #define F_RTSCTS (1<<4) /* enable RTS/CTS flow control */
102 #define F_ISSUE (1<<5) /* display /etc/issue */
103 #define F_LOGIN (1<<6) /* non-default login program */
104 #define F_PARSE (1<<7) /* process modem status messages */
105 #define F_TIMEOUT (1<<8) /* time out */
106 #define F_WAITCRLF (1<<9) /* wait for CR or LF */
107 #define F_NOPROMPT (1<<10) /* don't ask for login name! */
109 /* Storage for things detected while the login name was read. */
111 unsigned char erase; /* erase character */
112 unsigned char kill; /* kill character */
113 unsigned char eol; /* end-of-line character */
114 unsigned char parity; /* what parity did we see */
115 #ifdef HANDLE_ALLCAPS
116 unsigned char capslock; /* upper case without lower case */
120 /* Initial values for the above. */
121 static const struct chardata init_chardata = {
122 DEF_ERASE, /* default erase character */
123 DEF_KILL, /* default kill character */
124 13, /* default eol char */
125 0, /* space parity */
126 #ifdef HANDLE_ALLCAPS
131 /* The following is used for understandable diagnostics. */
133 /* Fake hostname for ut_host specified on command line. */
134 static char *fakehost = NULL;
138 #define debug(s) fprintf(dbf,s); fflush(dbf)
139 #define DEBUGTERM "/dev/ttyp0"
142 #define debug(s) /* nothing */
146 /* bcode - convert speed string to speed code; return 0 on failure */
147 static int bcode(const char *s)
150 unsigned value = bb_strtou(s, NULL, 10);
154 r = tty_value_to_baud(value);
162 /* parse_speeds - parse alternate baud rates */
163 static void parse_speeds(struct options *op, char *arg)
167 debug("entered parse_speeds\n");
168 for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) {
169 if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
170 bb_error_msg_and_die("bad speed: %s", cp);
171 if (op->numspeed > MAX_SPEED)
172 bb_error_msg_and_die("too many alternate speeds");
174 debug("exiting parsespeeds\n");
178 /* parse_args - parse command-line arguments */
179 static void parse_args(int argc, char **argv, struct options *op)
183 op->flags = getopt32(argc, argv, opt_string,
184 &(op->initstring), &fakehost, &(op->issue),
186 if (op->flags & F_INITSTRING) {
187 const char *p = op->initstring;
190 q = op->initstring = xstrdup(op->initstring);
191 /* copy optarg into op->initstring decoding \ddd
192 octal codes into chars */
196 *q++ = bb_process_escape_sequence(&p);
203 op->flags ^= F_ISSUE; /* revert flag show /etc/issue */
204 if (op->flags & F_TIMEOUT) {
205 op->timeout = xatoul_range(ts, 1, INT_MAX);
209 debug("after getopt loop\n");
210 if (argc < 2) /* check parameter count */
213 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
214 if ('0' <= argv[0][0] && argv[0][0] <= '9') {
215 /* a number first, assume it's a speed (BSD style) */
216 parse_speeds(op, argv[0]); /* baud rate(s) */
217 op->tty = argv[1]; /* tty name */
219 op->tty = argv[0]; /* tty name */
220 parse_speeds(op, argv[1]); /* baud rate(s) */
224 setenv("TERM", argv[2], 1);
226 debug("exiting parseargs\n");
229 static void xdup2(int srcfd, int dstfd, const char *tty)
231 if (dup2(srcfd, dstfd) == -1)
232 bb_perror_msg_and_die("%s: dup", tty);
235 /* open_tty - set up tty as standard { input, output, error } */
236 static void open_tty(char *tty, struct termios *tp, int local)
238 int chdir_to_root = 0;
240 /* Set up new standard input, unless we are given an already opened port. */
242 if (NOT_LONE_DASH(tty)) {
246 /* Sanity checks... */
251 if ((st.st_mode & S_IFMT) != S_IFCHR)
252 bb_error_msg_and_die("%s: not a character device", tty);
254 /* Open the tty as standard input. */
257 fd = xopen(tty, O_RDWR | O_NONBLOCK);
264 * Standard input should already be connected to an open port. Make
265 * sure it is open for read/write.
268 if ((fcntl(0, F_GETFL, 0) & O_RDWR) != O_RDWR)
269 bb_error_msg_and_die("%s: not open for read/write", tty);
272 /* Replace current standard output/error fd's with new ones */
278 * The following ioctl will fail if stdin is not a tty, but also when
279 * there is noise on the modem control lines. In the latter case, the
280 * common course of action is (1) fix your cables (2) give the modem more
281 * time to properly reset after hanging up. SunOS users can achieve (2)
282 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
283 * 5 seconds seems to be a good value.
286 if (ioctl(0, TCGETS, tp) < 0)
287 bb_perror_msg_and_die("%s: ioctl(TCGETS)", tty);
290 * It seems to be a terminal. Set proper protections and ownership. Mode
291 * 0622 is suitable for SYSV <4 because /bin/login does not change
292 * protections. SunOS 4 login will change the protections to 0620 (write
293 * access for group tty) after the login has succeeded.
297 #warning Debian /dev/vcs[a]NN hack is deprecated and will be removed
299 /* tty to root.dialout 660 */
303 gr = getgrnam("dialout");
304 id = gr ? gr->gr_gid : 0;
308 /* vcs,vcsa to root.sys 600 */
309 if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) {
313 vcsa = xmalloc(strlen(tty) + 2);
315 strcpy(vcs + 3, tty + 3);
316 strcpy(vcsa, "vcsa");
317 strcpy(vcsa + 4, tty + 3);
319 id = (gr = getgrnam("sys")) ? gr->gr_gid : 0;
330 chown(tty, 0, 0); /* root, sys */
331 chmod(tty, 0622); /* crw--w--w- */
337 /* termios_init - initialize termios settings */
338 static void termios_init(struct termios *tp, int speed, struct options *op)
341 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
342 * Special characters are set after we have read the login name; all
343 * reads will be done in raw mode anyway. Errors will be dealt with
347 /* flush input and output queues, important for modems! */
348 ioctl(0, TCFLSH, TCIOFLUSH);
351 tp->c_cflag = CS8 | HUPCL | CREAD | speed;
352 if (op->flags & F_LOCAL) {
353 tp->c_cflag |= CLOCAL;
356 tp->c_iflag = tp->c_lflag = tp->c_line = 0;
357 tp->c_oflag = OPOST | ONLCR;
361 /* Optionally enable hardware flow control */
364 if (op->flags & F_RTSCTS)
365 tp->c_cflag |= CRTSCTS;
368 ioctl(0, TCSETS, tp);
370 /* go to blocking input even in local mode */
371 fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) & ~O_NONBLOCK);
373 debug("term_io 2\n");
376 /* auto_baud - extract baud rate from modem status message */
377 static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
386 * This works only if the modem produces its status code AFTER raising
387 * the DCD line, and if the computer is fast enough to set the proper
388 * baud rate before the message has gone by. We expect a message of the
391 * <junk><number><junk>
393 * The number is interpreted as the baud rate of the incoming call. If the
394 * modem does not tell us the baud rate within one second, we will keep
395 * using the current baud rate. It is advisable to enable BREAK
396 * processing (comma-separated list of baud rates) if the processing of
397 * modem status messages is enabled.
401 * Use 7-bit characters, don't block if input queue is empty. Errors will
402 * be dealt with later on.
406 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
407 vmin = tp->c_cc[VMIN];
408 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
409 ioctl(0, TCSETS, tp);
412 * Wait for a while, then read everything the modem has said so far and
413 * try to extract the speed of the dial-in call.
417 nread = read(0, buf, size_buf - 1);
420 for (bp = buf; bp < buf + nread; bp++) {
421 if (isascii(*bp) && isdigit(*bp)) {
424 tp->c_cflag &= ~CBAUD;
425 tp->c_cflag |= speed;
431 /* Restore terminal settings. Errors will be dealt with later on. */
434 tp->c_cc[VMIN] = vmin;
435 ioctl(0, TCSETS, tp);
438 /* next_speed - select next baud rate */
439 static void next_speed(struct termios *tp, struct options *op)
441 static int baud_index = FIRST_SPEED; /* current speed index */
443 baud_index = (baud_index + 1) % op->numspeed;
444 tp->c_cflag &= ~CBAUD;
445 tp->c_cflag |= op->speeds[baud_index];
446 ioctl(0, TCSETS, tp);
450 /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
451 static void do_prompt(struct options *op, struct termios *tp)
454 print_login_issue(op->issue, op->tty);
456 print_login_prompt();
459 #ifdef HANDLE_ALLCAPS
460 /* caps_lock - string contains upper case without lower case */
461 /* returns 1 if true, 0 if false */
462 static int caps_lock(const char *s)
471 /* get_logname - get user name, establish parity, speed, erase, kill, eol */
472 /* return NULL on failure, logname on success */
473 static char *get_logname(char *logname, unsigned size_logname,
474 struct options *op, struct chardata *cp, struct termios *tp)
477 char c; /* input character, full eight bits */
478 char ascval; /* low 7 bits of input character */
479 int bits; /* # of "1" bits per character */
480 int mask; /* mask with 1 bit up */
481 static const char erase[][3] = { /* backspace-space-backspace */
482 "\010\040\010", /* space parity */
483 "\010\040\010", /* odd parity */
484 "\210\240\210", /* even parity */
485 "\210\240\210", /* no parity */
488 /* Initialize kill, erase, parity etc. (also after switching speeds). */
492 /* Flush pending input (esp. after parsing or switching the baud rate). */
495 ioctl(0, TCFLSH, TCIFLUSH);
497 /* Prompt for and read a login name. */
500 while (!logname[0]) {
502 /* Write issue file and prompt, with "parity" bit == 0. */
506 /* Read name, watch for break, parity, erase, kill, end-of-line. */
510 while (cp->eol == '\0') {
512 /* Do not report trivial EINTR/EIO errors. */
513 if (read(0, &c, 1) < 1) {
514 if (errno == EINTR || errno == EIO)
516 bb_perror_msg_and_die("%s: read", op->tty);
519 /* Do BREAK handling elsewhere. */
520 if (c == '\0' && op->numspeed > 1)
523 /* Do parity bit handling. */
525 if (c != ascval) { /* "parity" bit on ? */
528 while (mask & 0177) {
530 bits++; /* count "1" bits */
533 /* ... |= 2 - even, 1 - odd */
534 cp->parity |= 2 - (bits & 1);
537 /* Do erase, kill and end-of-line processing. */
541 *bp = '\0'; /* terminate logname */
542 cp->eol = ascval; /* set end-of-line char */
547 cp->erase = ascval; /* set erase character */
549 write(1, erase[cp->parity], 3);
555 cp->kill = ascval; /* set kill character */
556 while (bp > logname) {
557 write(1, erase[cp->parity], 3);
564 if (!isascii(ascval) || !isprint(ascval)) {
565 /* ignore garbage characters */
566 } else if (bp - logname >= size_logname - 1) {
567 bb_error_msg_and_die("%s: input overrun", op->tty);
569 write(1, &c, 1); /* echo the character */
570 *bp++ = ascval; /* and store it */
576 /* Handle names with upper case and no lower case. */
578 #ifdef HANDLE_ALLCAPS
579 cp->capslock = caps_lock(logname);
581 for (bp = logname; *bp; bp++)
583 *bp = tolower(*bp); /* map name to lower case */
589 /* termios_final - set the final tty mode bits */
590 static void termios_final(struct options *op, struct termios *tp, struct chardata *cp)
592 /* General terminal-independent stuff. */
594 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
595 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
596 /* no longer| ECHOCTL | ECHOPRT */
597 tp->c_oflag |= OPOST;
598 /* tp->c_cflag = 0; */
599 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
600 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
601 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
602 tp->c_cc[VEOL] = DEF_EOL;
603 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
605 /* Account for special characters seen in input. */
608 tp->c_iflag |= ICRNL; /* map CR in input to NL */
609 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
611 tp->c_cc[VERASE] = cp->erase; /* set erase character */
612 tp->c_cc[VKILL] = cp->kill; /* set kill character */
614 /* Account for the presence or absence of parity bits in input. */
616 switch (cp->parity) {
617 case 0: /* space (always 0) parity */
619 case 1: /* odd parity */
620 tp->c_cflag |= PARODD;
622 case 2: /* even parity */
623 tp->c_cflag |= PARENB;
624 tp->c_iflag |= INPCK | ISTRIP;
626 case (1 | 2): /* no parity bit */
627 tp->c_cflag &= ~CSIZE;
631 /* Account for upper case without lower case. */
633 #ifdef HANDLE_ALLCAPS
635 tp->c_iflag |= IUCLC;
636 tp->c_lflag |= XCASE;
637 tp->c_oflag |= OLCUC;
640 /* Optionally enable hardware flow control */
643 if (op->flags & F_RTSCTS)
644 tp->c_cflag |= CRTSCTS;
647 /* Finally, make the new settings effective */
649 if (ioctl(0, TCSETS, tp) < 0)
650 bb_perror_msg_and_die("%s: ioctl(TCSETS)", op->tty);
655 #if ENABLE_FEATURE_UTMP
656 /* update_utmp - update our utmp entry */
657 static void update_utmp(char *line)
662 int mypid = getpid();
665 * The utmp file holds miscellaneous information about things started by
666 * /sbin/init and other system-related events. Our purpose is to update
667 * the utmp entry for the current process, in particular the process type
668 * and the tty line we are listening to. Return successfully only if the
669 * utmp file can be opened for update, and if we are able to find our
670 * entry in the utmp file.
672 if (access(_PATH_UTMP, R_OK|W_OK) == -1) {
673 close(creat(_PATH_UTMP, 0664));
675 utmpname(_PATH_UTMP);
677 while ((utp = getutent())
678 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid))
682 memcpy(&ut, utp, sizeof(ut));
684 /* some inits don't initialize utmp... */
685 memset(&ut, 0, sizeof(ut));
686 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
690 strcpy(ut.ut_user, "LOGIN");
691 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
693 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
696 ut.ut_type = LOGIN_PROCESS;
702 #if ENABLE_FEATURE_WTMP
703 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1)
704 close(creat(bb_path_wtmp_file, 0664));
705 updwtmp(bb_path_wtmp_file, &ut);
709 #endif /* CONFIG_FEATURE_UTMP */
710 #endif /* SYSV_STYLE */
713 int getty_main(int argc, char **argv)
716 char *logname = NULL; /* login name, given to /bin/login */
717 /* Merging these into "struct local" may _seem_ to reduce
718 * parameter passing, but today's gcc will inline
719 * statics which are called once anyway, so don't do that */
720 struct chardata chardata; /* set by get_logname() */
721 struct termios termios; /* terminal mode bits */
722 struct options options = {
723 0, /* show /etc/issue (SYSV_STYLE) */
725 _PATH_LOGIN, /* default login program */
726 "tty1", /* default tty line */
727 "", /* modem init string */
729 ISSUE, /* default issue file */
733 0, /* no baud rates known yet */
736 /* Already too late because of theoretical
737 * possibility of getty --help somehow triggered
738 * inadvertently before we reach this. Oh well. */
742 logmode = LOGMODE_NONE;
746 /* Was "/dev/console". Why should we spam *system console*
747 * if there is a problem with getty on /dev/ttyS15?... */
748 nullfd = xopen(bb_dev_null, O_RDWR);
755 /* We want special flavor of error_msg_and_die */
758 openlog(applet_name, LOG_PID, LOG_AUTH);
759 logmode = LOGMODE_BOTH;
762 dbf = xfopen(DEBUGTERM, "w");
767 for (i = 1; i < argc; i++) {
774 /* Parse command-line arguments. */
775 parse_args(argc, argv, &options);
778 #if ENABLE_FEATURE_UTMP
779 /* Update the utmp file. */
780 update_utmp(options.tty);
784 debug("calling open_tty\n");
785 /* Open the tty as standard { input, output, error }. */
786 open_tty(options.tty, &termios, options.flags & F_LOCAL);
793 ioctl(0, TIOCSPGRP, &iv);
796 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
797 debug("calling termios_init\n");
798 termios_init(&termios, options.speeds[FIRST_SPEED], &options);
800 /* write the modem init string and DON'T flush the buffers */
801 if (options.flags & F_INITSTRING) {
802 debug("writing init string\n");
803 write(1, options.initstring, strlen(options.initstring));
806 if (!(options.flags & F_LOCAL)) {
807 /* go to blocking write mode unless -L is specified */
808 fcntl(1, F_SETFL, fcntl(1, F_GETFL, 0) & ~O_NONBLOCK);
811 /* Optionally detect the baud rate from the modem status message. */
812 debug("before autobaud\n");
813 if (options.flags & F_PARSE)
814 auto_baud(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), &termios);
816 /* Set the optional timer. */
818 alarm(options.timeout);
820 /* optionally wait for CR or LF before writing /etc/issue */
821 if (options.flags & F_WAITCRLF) {
824 debug("waiting for cr-lf\n");
825 while (read(0, &ch, 1) == 1) {
826 ch &= 0x7f; /* strip "parity bit" */
828 fprintf(dbf, "read %c\n", ch);
830 if (ch == '\n' || ch == '\r')
835 chardata = init_chardata;
836 if (!(options.flags & F_NOPROMPT)) {
837 /* Read the login name. */
838 debug("reading login name\n");
839 logname = get_logname(bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
840 &options, &chardata, &termios);
841 while (logname == NULL)
842 next_speed(&termios, &options);
850 /* Finalize the termios settings. */
852 termios_final(&options, &termios, &chardata);
854 /* Now the newline character should be properly written. */
858 /* Let the login program take care of password validation. */
860 execl(options.login, options.login, "--", logname, (char *) 0);
861 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);