blob: 24a182ff4097c6a6780b1ffccc5f412536138c83 [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* vi: set sw=4 ts=4: */
2/* agetty.c - another getty program for Linux. By W. Z. Venema 1989
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * 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.
6 *
7 * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95
8 *
Denis Vlasenkob44c7902008-03-17 09:29:43 +00009 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000010 * - added Native Language Support
Denis Vlasenkob44c7902008-03-17 09:29:43 +000011 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000012 * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
13 * - enable hardware flow control before displaying /etc/issue
14 *
15 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000016 */
Robert Griebl1fca5582002-06-04 20:45:46 +000017
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Denis Vlasenkof7026522006-09-19 13:50:55 +000019#include <syslog.h>
Robert Griebl1fca5582002-06-04 20:45:46 +000020
Denis Vlasenko6476cc12006-11-07 01:52:10 +000021#if ENABLE_FEATURE_UTMP
Denis Vlasenko9725daa2008-09-11 09:54:23 +000022#include <utmp.h> /* updwtmp() */
Mike Frysingerb3810092005-07-01 01:29:44 +000023#endif
24
Denis Vlasenko6476cc12006-11-07 01:52:10 +000025/*
26 * Some heuristics to find out what environment we are in: if it is not
27 * System V, assume it is SunOS 4.
28 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000029#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
Denis Vlasenko6476cc12006-11-07 01:52:10 +000030#include <sys/utsname.h>
Denis Vlasenkof3fca912007-12-04 18:46:01 +000031#else /* if !sysV style, wtmp/utmp code is off */
32#undef ENABLE_FEATURE_UTMP
33#undef ENABLE_FEATURE_WTMP
34#define ENABLE_FEATURE_UTMP 0
35#define ENABLE_FEATURE_WTMP 0
Eric Andersenfdfe2982002-10-10 03:55:09 +000036#endif /* LOGIN_PROCESS */
Robert Griebl1fca5582002-06-04 20:45:46 +000037
Denis Vlasenko6476cc12006-11-07 01:52:10 +000038/*
39 * Things you may want to modify.
40 *
41 * You may disagree with the default line-editing etc. characters defined
42 * below. Note, however, that DEL cannot be used for interrupt generation
43 * and for line editing at the same time.
44 */
Robert Griebl1fca5582002-06-04 20:45:46 +000045
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000046/* I doubt there are systems which still need this */
47#undef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000048#undef ANCIENT_BS_KILL_CHARS
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000049
Denis Vlasenko6476cc12006-11-07 01:52:10 +000050#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000051
Denis Vlasenko6476cc12006-11-07 01:52:10 +000052/* If ISSUE is not defined, getty will never display the contents of the
53 * /etc/issue file. You will not want to spit out large "issue" files at the
54 * wrong baud rate.
55 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000056#define ISSUE "/etc/issue" /* displayed before the login prompt */
Eric Andersen77804ce2005-07-27 06:05:38 +000057
Robert Griebl1fca5582002-06-04 20:45:46 +000058/* Some shorthands for control characters. */
Denis Vlasenko397de612008-03-17 08:55:44 +000059#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000060#define CR CTL('M') /* carriage return */
61#define NL CTL('J') /* line feed */
62#define BS CTL('H') /* back space */
63#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000064
65/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000066#define DEF_ERASE DEL /* default erase character */
67#define DEF_INTR CTL('C') /* default interrupt character */
68#define DEF_QUIT CTL('\\') /* default quit char */
69#define DEF_KILL CTL('U') /* default kill char */
70#define DEF_EOF CTL('D') /* default EOF char */
71#define DEF_EOL '\n'
72#define DEF_SWITCH 0 /* default switch char */
Robert Griebl1fca5582002-06-04 20:45:46 +000073
Denis Vlasenko6476cc12006-11-07 01:52:10 +000074/*
75 * When multiple baud rates are specified on the command line, the first one
76 * we will try is the first one specified.
77 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000078#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000079
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000080/* Storage for command-line options. */
Robert Griebl1fca5582002-06-04 20:45:46 +000081struct options {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000082 int flags; /* toggle switches, see below */
Denis Vlasenko13858992006-10-08 12:49:22 +000083 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000084 const char *login; /* login program */
85 const char *tty; /* name of tty */
86 const char *initstring; /* modem init string */
87 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000088 int numspeed; /* number of baud rates to try */
89 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +000090};
91
Robert Griebl1fca5582002-06-04 20:45:46 +000092/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +000093struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000094 unsigned char erase; /* erase character */
95 unsigned char kill; /* kill character */
96 unsigned char eol; /* end-of-line character */
97 unsigned char parity; /* what parity did we see */
Denis Vlasenko397de612008-03-17 08:55:44 +000098 /* (parity & 1): saw odd parity char with 7th bit set */
99 /* (parity & 2): saw even parity char with 7th bit set */
100 /* parity == 0: probably 7-bit, space parity? */
101 /* parity == 1: probably 7-bit, odd parity? */
102 /* parity == 2: probably 7-bit, even parity? */
103 /* parity == 3: definitely 8 bit, no parity! */
104 /* Hmm... with any value of "parity" 8 bit, no parity is possible */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000105#ifdef HANDLE_ALLCAPS
106 unsigned char capslock; /* upper case without lower case */
107#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000108};
109
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000110
Robert Griebl1fca5582002-06-04 20:45:46 +0000111/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000112static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000113 DEF_ERASE, /* default erase character */
114 DEF_KILL, /* default kill character */
115 13, /* default eol char */
116 0, /* space parity */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000117#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000118 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000119#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000120};
121
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000122static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
123#define F_INITSTRING (1 << 0) /* -I initstring is set */
124#define F_LOCAL (1 << 1) /* -L force local */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000125#define F_FAKEHOST (1 << 2) /* -H fake hostname */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000126#define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */
127#define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */
128#define F_ISSUE (1 << 5) /* -i display /etc/issue */
129#define F_LOGIN (1 << 6) /* -l non-default login program */
130#define F_PARSE (1 << 7) /* -m process modem status messages */
131#define F_TIMEOUT (1 << 8) /* -t time out */
132#define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000133#define F_NOPROMPT (1 << 10) /* -n don't ask for login name */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000134
Robert Griebl1fca5582002-06-04 20:45:46 +0000135
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000136#define line_buf bb_common_bufsiz1
Robert Griebl1fca5582002-06-04 20:45:46 +0000137
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000138/* The following is used for understandable diagnostics. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000139#ifdef DEBUGGING
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000140static FILE *dbf;
Denis Vlasenko397de612008-03-17 08:55:44 +0000141#define DEBUGTERM "/dev/ttyp0"
142#define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000143#else
Denis Vlasenko397de612008-03-17 08:55:44 +0000144#define debug(...) ((void)0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000145#endif
146
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000147
Denis Vlasenko397de612008-03-17 08:55:44 +0000148/* bcode - convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000149static int bcode(const char *s)
150{
Denis Vlasenko397de612008-03-17 08:55:44 +0000151 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
152 if (value < 0) /* bad terminating char, overflow, etc */
153 return value;
154 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000155}
156
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000157/* parse_speeds - parse alternate baud rates */
158static void parse_speeds(struct options *op, char *arg)
159{
160 char *cp;
161
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000162 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000163 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000164 while ((cp = strsep(&arg, ",")) != NULL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000165 op->speeds[op->numspeed] = bcode(cp);
Mike Frysingerfe013a72009-04-09 07:08:04 +0000166 if (op->speeds[op->numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000167 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000168 /* note: arg "0" turns into speed B0 */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000169 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000170 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000171 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000172 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000173 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000174}
175
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000176/* parse_args - parse command-line arguments */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000177static void parse_args(char **argv, struct options *op, char **fakehost_p)
Robert Griebl1fca5582002-06-04 20:45:46 +0000178{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000179 char *ts;
Robert Griebl1fca5582002-06-04 20:45:46 +0000180
Denis Vlasenko397de612008-03-17 08:55:44 +0000181 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000182 op->flags = getopt32(argv, opt_string,
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000183 &(op->initstring), fakehost_p, &(op->issue),
Denis Vlasenko397de612008-03-17 08:55:44 +0000184 &(op->login), &op->timeout);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000185 argv += optind;
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000186 if (op->flags & F_INITSTRING) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000187 const char *p = op->initstring;
188 char *q;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000189
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000190 op->initstring = q = xstrdup(p);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000191 /* copy optarg into op->initstring decoding \ddd
192 octal codes into chars */
193 while (*p) {
194 if (*p == '\\') {
195 p++;
196 *q++ = bb_process_escape_sequence(&p);
197 } else {
198 *q++ = *p++;
Robert Griebl1fca5582002-06-04 20:45:46 +0000199 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000200 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000201 *q = '\0';
202 }
Denis Vlasenko397de612008-03-17 08:55:44 +0000203 op->flags ^= F_ISSUE; /* invert flag "show /etc/issue" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000204 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000205
206 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000207 op->tty = argv[0]; /* tty name */
208 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000209 if (isdigit(argv[0][0])) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000210 /* a number first, assume it's a speed (BSD style) */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000211 op->tty = ts; /* tty name is in argv[1] */
212 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000213 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000214 parse_speeds(op, ts);
Robert Griebl1fca5582002-06-04 20:45:46 +0000215
Denis Vlasenko397de612008-03-17 08:55:44 +0000216// TODO: if applet_name is set to "getty: TTY", bb_error_msg's get simpler!
217// grep for "%s:"
218
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000219 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000220 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000221
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000222 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000223}
224
Robert Griebl1fca5582002-06-04 20:45:46 +0000225/* open_tty - set up tty as standard { input, output, error } */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000226static void open_tty(const char *tty)
Robert Griebl1fca5582002-06-04 20:45:46 +0000227{
Robert Griebl1fca5582002-06-04 20:45:46 +0000228 /* Set up new standard input, unless we are given an already opened port. */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000229 if (NOT_LONE_DASH(tty)) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000230// struct stat st;
231// int cur_dir_fd;
232// int fd;
Robert Griebl1fca5582002-06-04 20:45:46 +0000233
234 /* Sanity checks... */
Denis Vlasenko397de612008-03-17 08:55:44 +0000235// cur_dir_fd = xopen(".", O_DIRECTORY | O_NONBLOCK);
236// xchdir("/dev");
237// xstat(tty, &st);
238// if ((st.st_mode & S_IFMT) != S_IFCHR)
239// bb_error_msg_and_die("%s: not a character device", tty);
240
241 if (tty[0] != '/')
242 tty = xasprintf("/dev/%s", tty); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000243
244 /* Open the tty as standard input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000245 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000246 close(0);
247 /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000248
Denis Vlasenko397de612008-03-17 08:55:44 +0000249// /* Restore current directory */
250// fchdir(cur_dir_fd);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000251
252 /* Open the tty as standard input, continued */
Denis Vlasenko397de612008-03-17 08:55:44 +0000253// xmove_fd(fd, 0);
254// /* fd is >= cur_dir_fd, and cur_dir_fd gets closed too here: */
255// while (fd > 2)
256// close(fd--);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000257
Denis Vlasenko397de612008-03-17 08:55:44 +0000258 /* Set proper protections and ownership. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000259 fchown(0, 0, 0); /* 0:0 */
Denis Vlasenko397de612008-03-17 08:55:44 +0000260 fchmod(0, 0620); /* crw--w---- */
Robert Griebl1fca5582002-06-04 20:45:46 +0000261 } else {
Robert Griebl1fca5582002-06-04 20:45:46 +0000262 /*
263 * Standard input should already be connected to an open port. Make
264 * sure it is open for read/write.
265 */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000266 if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000267 bb_error_msg_and_die("stdin is not open for read/write");
Robert Griebl1fca5582002-06-04 20:45:46 +0000268 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000269}
270
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000271/* termios_init - initialize termios settings */
272static void termios_init(struct termios *tp, int speed, struct options *op)
Robert Griebl1fca5582002-06-04 20:45:46 +0000273{
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000274 speed_t ispeed, ospeed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000275 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000276 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000277 * Special characters are set after we have read the login name; all
278 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000279 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000280 */
281#ifdef __linux__
282 /* flush input and output queues, important for modems! */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000283 ioctl(0, TCFLSH, TCIOFLUSH); /* tcflush(0, TCIOFLUSH)? - same */
Robert Griebl1fca5582002-06-04 20:45:46 +0000284#endif
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000285 ispeed = ospeed = speed;
286 if (speed == B0) {
287 /* Speed was specified as "0" on command line.
288 * Just leave it unchanged */
289 ispeed = cfgetispeed(tp);
290 ospeed = cfgetospeed(tp);
291 }
292 tp->c_cflag = CS8 | HUPCL | CREAD;
Denis Vlasenko397de612008-03-17 08:55:44 +0000293 if (op->flags & F_LOCAL)
Robert Griebl1fca5582002-06-04 20:45:46 +0000294 tp->c_cflag |= CLOCAL;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000295 cfsetispeed(tp, ispeed);
296 cfsetospeed(tp, ospeed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000297
Rob Landleyf78ab532006-08-24 20:00:44 +0000298 tp->c_iflag = tp->c_lflag = tp->c_line = 0;
299 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000300 tp->c_cc[VMIN] = 1;
301 tp->c_cc[VTIME] = 0;
302
303 /* Optionally enable hardware flow control */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000304#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000305 if (op->flags & F_RTSCTS)
306 tp->c_cflag |= CRTSCTS;
307#endif
308
Denis Vlasenko202ac502008-11-05 13:20:58 +0000309 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000310
Robert Griebl1fca5582002-06-04 20:45:46 +0000311 debug("term_io 2\n");
312}
313
314/* auto_baud - extract baud rate from modem status message */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000315static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000316{
317 int speed;
318 int vmin;
319 unsigned iflag;
Robert Griebl1fca5582002-06-04 20:45:46 +0000320 char *bp;
321 int nread;
322
323 /*
324 * This works only if the modem produces its status code AFTER raising
325 * the DCD line, and if the computer is fast enough to set the proper
326 * baud rate before the message has gone by. We expect a message of the
327 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000328 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000329 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000330 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000331 * The number is interpreted as the baud rate of the incoming call. If the
332 * modem does not tell us the baud rate within one second, we will keep
333 * using the current baud rate. It is advisable to enable BREAK
334 * processing (comma-separated list of baud rates) if the processing of
335 * modem status messages is enabled.
336 */
337
338 /*
339 * Use 7-bit characters, don't block if input queue is empty. Errors will
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000340 * be dealt with later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000341 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000342 iflag = tp->c_iflag;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000343 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
Robert Griebl1fca5582002-06-04 20:45:46 +0000344 vmin = tp->c_cc[VMIN];
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000345 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000346 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000347
348 /*
349 * Wait for a while, then read everything the modem has said so far and
350 * try to extract the speed of the dial-in call.
351 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000352 sleep(1);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000353 nread = safe_read(STDIN_FILENO, buf, size_buf - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000354 if (nread > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000355 buf[nread] = '\0';
356 for (bp = buf; bp < buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000357 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000358 speed = bcode(bp);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000359 if (speed > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000360 tp->c_cflag &= ~CBAUD;
361 tp->c_cflag |= speed;
362 }
363 break;
364 }
365 }
366 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000367
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000368 /* Restore terminal settings. Errors will be dealt with later on. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000369 tp->c_iflag = iflag;
370 tp->c_cc[VMIN] = vmin;
Denis Vlasenko202ac502008-11-05 13:20:58 +0000371 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000372}
373
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000374/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000375static void do_prompt(struct options *op)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000376{
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000377#ifdef ISSUE
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000378 print_login_issue(op->issue, op->tty);
379#endif
380 print_login_prompt();
381}
382
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000383#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000384/* all_is_upcase - string contains upper case without lower case */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000385/* returns 1 if true, 0 if false */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000386static int all_is_upcase(const char *s)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000387{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000388 while (*s)
389 if (islower(*s++))
390 return 0;
391 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000392}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000393#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000394
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000395/* get_logname - get user name, establish parity, speed, erase, kill, eol;
396 * return NULL on BREAK, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000397static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000398 struct options *op, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000399{
Robert Griebl1fca5582002-06-04 20:45:46 +0000400 char *bp;
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000401 char c; /* input character, full eight bits */
402 char ascval; /* low 7 bits of input character */
403 int bits; /* # of "1" bits per character */
404 int mask; /* mask with 1 bit up */
Denis Vlasenko397de612008-03-17 08:55:44 +0000405 static const char erase[][3] = {/* backspace-space-backspace */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000406 "\010\040\010", /* space parity */
407 "\010\040\010", /* odd parity */
408 "\210\240\210", /* even parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000409 "\010\040\010", /* 8 bit no parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000410 };
411
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000412 /* NB: *cp is pre-initialized with init_chardata */
Robert Griebl1fca5582002-06-04 20:45:46 +0000413
414 /* Flush pending input (esp. after parsing or switching the baud rate). */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000415 sleep(1);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000416 ioctl(0, TCFLSH, TCIFLUSH); /* tcflush(0, TCIOFLUSH)? - same */
Robert Griebl1fca5582002-06-04 20:45:46 +0000417
418 /* Prompt for and read a login name. */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000419 logname[0] = '\0';
420 while (!logname[0]) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000421 /* Write issue file and prompt, with "parity" bit == 0. */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000422 do_prompt(op);
Robert Griebl1fca5582002-06-04 20:45:46 +0000423
424 /* Read name, watch for break, parity, erase, kill, end-of-line. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000425 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000426 cp->eol = '\0';
427 while (cp->eol == '\0') {
Robert Griebl1fca5582002-06-04 20:45:46 +0000428
429 /* Do not report trivial EINTR/EIO errors. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000430 if (read(STDIN_FILENO, &c, 1) < 1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000431 if (errno == EINTR || errno == EIO)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000432 exit(EXIT_SUCCESS);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000433 bb_perror_msg_and_die("%s: read", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000434 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000435
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000436 /* BREAK. If we have speeds to try,
437 * return NULL (will switch speeds and return here) */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000438 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000439 return NULL;
440
441 /* Do parity bit handling. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000442 if (!(op->flags & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000443 bits = 1;
444 mask = 1;
Denis Vlasenko397de612008-03-17 08:55:44 +0000445 while (mask & 0x7f) {
446 if (mask & c)
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000447 bits++; /* count "1" bits */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000448 mask <<= 1;
449 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000450 /* ... |= 2 - even, 1 - odd */
451 cp->parity |= 2 - (bits & 1);
Robert Griebl1fca5582002-06-04 20:45:46 +0000452 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000453
Denis Vlasenko703aa132006-10-23 22:43:02 +0000454 /* Do erase, kill and end-of-line processing. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000455 ascval = c & 0x7f;
Robert Griebl1fca5582002-06-04 20:45:46 +0000456 switch (ascval) {
457 case CR:
458 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000459 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000460 cp->eol = ascval; /* set end-of-line char */
Robert Griebl1fca5582002-06-04 20:45:46 +0000461 break;
462 case BS:
463 case DEL:
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000464#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000465 case '#':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000466#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000467 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000468 if (bp > logname) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000469 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000470 bp--;
471 }
472 break;
473 case CTL('U'):
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000474#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000475 case '@':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000476#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000477 cp->kill = ascval; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000478 while (bp > logname) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000479 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000480 bp--;
481 }
482 break;
483 case CTL('D'):
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000484 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000485 default:
Bernhard Reutner-Fischere44a4e32009-01-09 14:49:11 +0000486 if (!isprint(ascval)) {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000487 /* ignore garbage characters */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000488 } else if ((int)(bp - logname) >= size_logname - 1) {
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000489 bb_error_msg_and_die("%s: input overrun", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000490 } else {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000491 full_write(STDOUT_FILENO, &c, 1); /* echo the character */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000492 *bp++ = ascval; /* and store it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000493 }
494 break;
495 }
496 }
497 }
498 /* Handle names with upper case and no lower case. */
499
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000500#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000501 cp->capslock = all_is_upcase(logname);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000502 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000503 for (bp = logname; *bp; bp++)
504 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000505 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000506 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000507#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000508 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000509}
510
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000511/* termios_final - set the final tty mode bits */
512static void termios_final(struct options *op, struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000513{
514 /* General terminal-independent stuff. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000515 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000516 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
517 /* no longer| ECHOCTL | ECHOPRT */
518 tp->c_oflag |= OPOST;
519 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000520 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
521 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
522 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000523 tp->c_cc[VEOL] = DEF_EOL;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000524 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000525
526 /* Account for special characters seen in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000527 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000528 tp->c_iflag |= ICRNL; /* map CR in input to NL */
529 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000530 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000531 tp->c_cc[VERASE] = cp->erase; /* set erase character */
532 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000533
534 /* Account for the presence or absence of parity bits in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000535 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000536 case 0: /* space (always 0) parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000537// I bet most people go here - they use only 7-bit chars in usernames....
Robert Griebl1fca5582002-06-04 20:45:46 +0000538 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000539 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000540 tp->c_cflag |= PARODD;
541 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000542 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000543 tp->c_cflag |= PARENB;
544 tp->c_iflag |= INPCK | ISTRIP;
545 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000546 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000547 tp->c_cflag &= ~CSIZE;
548 tp->c_cflag |= CS7;
Denis Vlasenko397de612008-03-17 08:55:44 +0000549// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes -
550// it's probably some umlauts etc, but definitely NOT 7-bit!!!
551// Entire parity detection madness here just begs for deletion...
Robert Griebl1fca5582002-06-04 20:45:46 +0000552 break;
553 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000554
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000555 /* Account for upper case without lower case. */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000556#ifdef HANDLE_ALLCAPS
Robert Griebl1fca5582002-06-04 20:45:46 +0000557 if (cp->capslock) {
558 tp->c_iflag |= IUCLC;
559 tp->c_lflag |= XCASE;
560 tp->c_oflag |= OLCUC;
561 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000562#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000563 /* Optionally enable hardware flow control */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000564#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000565 if (op->flags & F_RTSCTS)
566 tp->c_cflag |= CRTSCTS;
567#endif
568
569 /* Finally, make the new settings effective */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000570 /* It's tcsetattr_stdin_TCSANOW() + error check */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000571 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000572}
573
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000574#if ENABLE_FEATURE_UTMP
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000575static void touch(const char *filename)
576{
577 if (access(filename, R_OK | W_OK) == -1)
578 close(open(filename, O_WRONLY | O_CREAT, 0664));
579}
580
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000581/* update_utmp - update our utmp entry */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000582static void update_utmp(const char *line, char *fakehost)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000583{
584 struct utmp ut;
585 struct utmp *utp;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000586 int mypid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000587
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000588 /* In case we won't find an entry below... */
589 memset(&ut, 0, sizeof(ut));
590 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
591
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000592 /*
593 * The utmp file holds miscellaneous information about things started by
594 * /sbin/init and other system-related events. Our purpose is to update
595 * the utmp entry for the current process, in particular the process type
596 * and the tty line we are listening to. Return successfully only if the
597 * utmp file can be opened for update, and if we are able to find our
598 * entry in the utmp file.
599 */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000600 touch(_PATH_UTMP);
601
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000602 utmpname(_PATH_UTMP);
603 setutent();
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000604 while ((utp = getutent()) != NULL) {
605 if (utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) {
606 memcpy(&ut, utp, sizeof(ut));
607 break;
608 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000609 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000610
Rob Landleycf7577d2006-06-25 22:59:31 +0000611 strcpy(ut.ut_user, "LOGIN");
612 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000613 if (fakehost)
Rob Landleycf7577d2006-06-25 22:59:31 +0000614 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
Bernhard Reutner-Fischer62d85032008-06-01 10:10:22 +0000615 ut.ut_tv.tv_sec = time(NULL);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000616 ut.ut_type = LOGIN_PROCESS;
617 ut.ut_pid = mypid;
618
619 pututline(&ut);
620 endutent();
621
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000622#if ENABLE_FEATURE_WTMP
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000623 touch(bb_path_wtmp_file);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000624 updwtmp(bb_path_wtmp_file, &ut);
625#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000626}
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000627#endif /* CONFIG_FEATURE_UTMP */
Robert Griebl1fca5582002-06-04 20:45:46 +0000628
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000629int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000630int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000631{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000632 int n;
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000633 char *fakehost = NULL; /* Fake hostname for ut_host */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000634 char *logname; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000635 /* Merging these into "struct local" may _seem_ to reduce
636 * parameter passing, but today's gcc will inline
637 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000638 struct chardata chardata; /* set by get_logname() */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000639 struct termios termios; /* terminal mode bits */
640 struct options options;
641
Denis Vlasenko397de612008-03-17 08:55:44 +0000642 chardata = init_chardata;
643
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000644 memset(&options, 0, sizeof(options));
645 options.login = _PATH_LOGIN; /* default login program */
646 options.tty = "tty1"; /* default tty line */
647 options.initstring = ""; /* modem init string */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000648#ifdef ISSUE
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000649 options.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000650#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000651
Denis Vlasenko397de612008-03-17 08:55:44 +0000652 /* Parse command-line arguments. */
653 parse_args(argv, &options, &fakehost);
654
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000655 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000656
657 /* Create new session, lose controlling tty, if any */
658 /* docs/ctty.htm says:
659 * "This is allowed only when the current process
660 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000661 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000662 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000663 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000664 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
665 xdup2(n, 1);
666 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000667 while (n > 2)
668 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000669
670 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000671 die_sleep = 10;
672 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000673 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000674 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000675 logmode = LOGMODE_BOTH;
676
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000677#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000678 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000679 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000680 debug(argv[n]);
681 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000682 }
683#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000684
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000685 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000686 /* If it's not "-" and not taken yet, it will become our ctty */
687 debug("calling open_tty\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000688 open_tty(options.tty);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000689 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000690 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000691 xdup2(0, 1);
692 xdup2(0, 2);
693
694 /*
695 * The following ioctl will fail if stdin is not a tty, but also when
696 * there is noise on the modem control lines. In the latter case, the
697 * common course of action is (1) fix your cables (2) give the modem more
698 * time to properly reset after hanging up. SunOS users can achieve (2)
699 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
700 * 5 seconds seems to be a good value.
701 */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000702 /* tcgetattr() + error check */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000703 ioctl_or_perror_and_die(0, TCGETS, &termios, "%s: TCGETS", options.tty);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000704
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000705#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000706// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000707 // /* Forcibly make fd 0 our controlling tty, even if another session
708 // * has it as a ctty. (Another session loses ctty). */
709 // ioctl(0, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000710 /* Make ourself a foreground process group within our session */
711 tcsetpgrp(0, getpid());
712#endif
713
714#if ENABLE_FEATURE_UTMP
715 /* Update the utmp file. This tty is ours now! */
716 update_utmp(options.tty, fakehost);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000717#endif
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000718
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000719 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
720 debug("calling termios_init\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000721 termios_init(&termios, options.speeds[0], &options);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000722
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000723 /* Write the modem init string and DON'T flush the buffers */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000724 if (options.flags & F_INITSTRING) {
725 debug("writing init string\n");
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000726 /* todo: use xwrite_str? */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000727 full_write(STDOUT_FILENO, options.initstring, strlen(options.initstring));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000728 }
729
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000730 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000731 debug("before autobaud\n");
732 if (options.flags & F_PARSE)
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000733 auto_baud(line_buf, sizeof(line_buf), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000734
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000735 /* Set the optional timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000736 alarm(options.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000737
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000738 /* Optionally wait for CR or LF before writing /etc/issue */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000739 if (options.flags & F_WAITCRLF) {
740 char ch;
741
742 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000743 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000744 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000745 ch &= 0x7f; /* strip "parity bit" */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000746 if (ch == '\n' || ch == '\r')
747 break;
748 }
749 }
750
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000751 logname = NULL;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000752 if (!(options.flags & F_NOPROMPT)) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000753 /* NB:termios_init already set line speed
754 * to options.speeds[0] */
755 int baud_index = 0;
756
757 while (1) {
758 /* Read the login name. */
759 debug("reading login name\n");
760 logname = get_logname(line_buf, sizeof(line_buf),
Denis Vlasenko68404f12008-03-17 09:00:54 +0000761 &options, &chardata);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000762 if (logname)
763 break;
764 /* we are here only if options.numspeed > 1 */
765 baud_index = (baud_index + 1) % options.numspeed;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000766 cfsetispeed(&termios, options.speeds[baud_index]);
767 cfsetospeed(&termios, options.speeds[baud_index]);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000768 tcsetattr_stdin_TCSANOW(&termios);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000769 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000770 }
771
772 /* Disable timer. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000773 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000774
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000775 /* Finalize the termios settings. */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000776 termios_final(&options, &termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000777
778 /* Now the newline character should be properly written. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000779 full_write(STDOUT_FILENO, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000780
781 /* Let the login program take care of password validation. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000782 /* We use PATH because we trust that root doesn't set "bad" PATH,
783 * and getty is not suid-root applet. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000784 /* With -n, logname == NULL, and login will ask for username instead */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000785 BB_EXECLP(options.login, options.login, "--", logname, NULL);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000786 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000787}