blob: 76b0de4499b176561b85cd171133a298763320c2 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020015 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"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
Denys Vlasenko3a416112010-04-05 22:10:38 +020022# include <utmp.h> /* LOGIN_PROCESS */
Mike Frysingerb3810092005-07-01 01:29:44 +000023#endif
24
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020025#ifndef IUCLC
26# define IUCLC 0
27#endif
28
Denis Vlasenko6476cc12006-11-07 01:52:10 +000029/*
30 * Some heuristics to find out what environment we are in: if it is not
31 * System V, assume it is SunOS 4.
32 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000033#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
Denis Vlasenko6476cc12006-11-07 01:52:10 +000034#include <sys/utsname.h>
Denis Vlasenkof3fca912007-12-04 18:46:01 +000035#else /* if !sysV style, wtmp/utmp code is off */
36#undef ENABLE_FEATURE_UTMP
37#undef ENABLE_FEATURE_WTMP
38#define ENABLE_FEATURE_UTMP 0
39#define ENABLE_FEATURE_WTMP 0
Eric Andersenfdfe2982002-10-10 03:55:09 +000040#endif /* LOGIN_PROCESS */
Robert Griebl1fca5582002-06-04 20:45:46 +000041
Denis Vlasenko6476cc12006-11-07 01:52:10 +000042/*
43 * Things you may want to modify.
44 *
45 * You may disagree with the default line-editing etc. characters defined
46 * below. Note, however, that DEL cannot be used for interrupt generation
47 * and for line editing at the same time.
48 */
Robert Griebl1fca5582002-06-04 20:45:46 +000049
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000050/* I doubt there are systems which still need this */
51#undef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000052#undef ANCIENT_BS_KILL_CHARS
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000053
Denis Vlasenko6476cc12006-11-07 01:52:10 +000054#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000055
Denis Vlasenko6476cc12006-11-07 01:52:10 +000056/* If ISSUE is not defined, getty will never display the contents of the
57 * /etc/issue file. You will not want to spit out large "issue" files at the
58 * wrong baud rate.
59 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000060#define ISSUE "/etc/issue" /* displayed before the login prompt */
Eric Andersen77804ce2005-07-27 06:05:38 +000061
Robert Griebl1fca5582002-06-04 20:45:46 +000062/* Some shorthands for control characters. */
Denis Vlasenko397de612008-03-17 08:55:44 +000063#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000064#define CR CTL('M') /* carriage return */
65#define NL CTL('J') /* line feed */
66#define BS CTL('H') /* back space */
67#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000068
69/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000070#define DEF_ERASE DEL /* default erase character */
71#define DEF_INTR CTL('C') /* default interrupt character */
72#define DEF_QUIT CTL('\\') /* default quit char */
73#define DEF_KILL CTL('U') /* default kill char */
74#define DEF_EOF CTL('D') /* default EOF char */
75#define DEF_EOL '\n'
76#define DEF_SWITCH 0 /* default switch char */
Robert Griebl1fca5582002-06-04 20:45:46 +000077
Denis Vlasenko6476cc12006-11-07 01:52:10 +000078/*
79 * When multiple baud rates are specified on the command line, the first one
80 * we will try is the first one specified.
81 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000082#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000083
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000084/* Storage for command-line options. */
Robert Griebl1fca5582002-06-04 20:45:46 +000085struct options {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000086 int flags; /* toggle switches, see below */
Denis Vlasenko13858992006-10-08 12:49:22 +000087 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000088 const char *login; /* login program */
89 const char *tty; /* name of tty */
90 const char *initstring; /* modem init string */
91 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000092 int numspeed; /* number of baud rates to try */
93 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +000094};
95
Robert Griebl1fca5582002-06-04 20:45:46 +000096/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +000097struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000098 unsigned char erase; /* erase character */
99 unsigned char kill; /* kill character */
100 unsigned char eol; /* end-of-line character */
101 unsigned char parity; /* what parity did we see */
Denis Vlasenko397de612008-03-17 08:55:44 +0000102 /* (parity & 1): saw odd parity char with 7th bit set */
103 /* (parity & 2): saw even parity char with 7th bit set */
104 /* parity == 0: probably 7-bit, space parity? */
105 /* parity == 1: probably 7-bit, odd parity? */
106 /* parity == 2: probably 7-bit, even parity? */
107 /* parity == 3: definitely 8 bit, no parity! */
108 /* Hmm... with any value of "parity" 8 bit, no parity is possible */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000109#ifdef HANDLE_ALLCAPS
110 unsigned char capslock; /* upper case without lower case */
111#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000112};
113
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000114
Robert Griebl1fca5582002-06-04 20:45:46 +0000115/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000116static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000117 DEF_ERASE, /* default erase character */
118 DEF_KILL, /* default kill character */
119 13, /* default eol char */
120 0, /* space parity */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000121#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000122 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000123#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000124};
125
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000126static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
127#define F_INITSTRING (1 << 0) /* -I initstring is set */
128#define F_LOCAL (1 << 1) /* -L force local */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000129#define F_FAKEHOST (1 << 2) /* -H fake hostname */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000130#define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */
131#define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */
132#define F_ISSUE (1 << 5) /* -i display /etc/issue */
133#define F_LOGIN (1 << 6) /* -l non-default login program */
134#define F_PARSE (1 << 7) /* -m process modem status messages */
135#define F_TIMEOUT (1 << 8) /* -t time out */
136#define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000137#define F_NOPROMPT (1 << 10) /* -n don't ask for login name */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000138
Robert Griebl1fca5582002-06-04 20:45:46 +0000139
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000140#define line_buf bb_common_bufsiz1
Robert Griebl1fca5582002-06-04 20:45:46 +0000141
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000142/* The following is used for understandable diagnostics. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000143#ifdef DEBUGGING
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000144static FILE *dbf;
Denis Vlasenko397de612008-03-17 08:55:44 +0000145#define DEBUGTERM "/dev/ttyp0"
146#define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000147#else
Denis Vlasenko397de612008-03-17 08:55:44 +0000148#define debug(...) ((void)0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000149#endif
150
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000151
Denis Vlasenko397de612008-03-17 08:55:44 +0000152/* bcode - convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000153static int bcode(const char *s)
154{
Denis Vlasenko397de612008-03-17 08:55:44 +0000155 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
156 if (value < 0) /* bad terminating char, overflow, etc */
157 return value;
158 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000159}
160
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000161/* parse_speeds - parse alternate baud rates */
162static void parse_speeds(struct options *op, char *arg)
163{
164 char *cp;
165
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000166 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000167 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000168 while ((cp = strsep(&arg, ",")) != NULL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000169 op->speeds[op->numspeed] = bcode(cp);
Mike Frysingerfe013a72009-04-09 07:08:04 +0000170 if (op->speeds[op->numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000171 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000172 /* note: arg "0" turns into speed B0 */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000173 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000174 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000175 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000176 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000177 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000178}
179
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000180/* parse_args - parse command-line arguments */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000181static void parse_args(char **argv, struct options *op, char **fakehost_p)
Robert Griebl1fca5582002-06-04 20:45:46 +0000182{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000183 char *ts;
Robert Griebl1fca5582002-06-04 20:45:46 +0000184
Denis Vlasenko397de612008-03-17 08:55:44 +0000185 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000186 op->flags = getopt32(argv, opt_string,
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000187 &(op->initstring), fakehost_p, &(op->issue),
Denis Vlasenko397de612008-03-17 08:55:44 +0000188 &(op->login), &op->timeout);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000189 argv += optind;
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000190 if (op->flags & F_INITSTRING) {
Denys Vlasenko53600592010-10-23 21:06:06 +0200191 op->initstring = xstrdup(op->initstring);
192 /* decode \ddd octal codes into chars */
193 strcpy_and_process_escape_sequences((char*)op->initstring, op->initstring);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000194 }
Denis Vlasenko397de612008-03-17 08:55:44 +0000195 op->flags ^= F_ISSUE; /* invert flag "show /etc/issue" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000196 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000197
198 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000199 op->tty = argv[0]; /* tty name */
200 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000201 if (isdigit(argv[0][0])) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000202 /* a number first, assume it's a speed (BSD style) */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000203 op->tty = ts; /* tty name is in argv[1] */
204 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000205 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000206 parse_speeds(op, ts);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200207 applet_name = xasprintf("getty: %s", op->tty);
Denis Vlasenko397de612008-03-17 08:55:44 +0000208
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000209 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000210 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000211
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000212 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000213}
214
Robert Griebl1fca5582002-06-04 20:45:46 +0000215/* open_tty - set up tty as standard { input, output, error } */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000216static void open_tty(const char *tty)
Robert Griebl1fca5582002-06-04 20:45:46 +0000217{
Robert Griebl1fca5582002-06-04 20:45:46 +0000218 /* Set up new standard input, unless we are given an already opened port. */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000219 if (NOT_LONE_DASH(tty)) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000220// struct stat st;
221// int cur_dir_fd;
222// int fd;
Robert Griebl1fca5582002-06-04 20:45:46 +0000223
224 /* Sanity checks... */
Denis Vlasenko397de612008-03-17 08:55:44 +0000225// cur_dir_fd = xopen(".", O_DIRECTORY | O_NONBLOCK);
226// xchdir("/dev");
227// xstat(tty, &st);
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200228// if (!S_ISCHR(st.st_mode))
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200229// bb_error_msg_and_die("not a character device");
Denis Vlasenko397de612008-03-17 08:55:44 +0000230
231 if (tty[0] != '/')
232 tty = xasprintf("/dev/%s", tty); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000233
234 /* Open the tty as standard input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000235 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000236 close(0);
237 /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000238
Denis Vlasenko397de612008-03-17 08:55:44 +0000239// /* Restore current directory */
240// fchdir(cur_dir_fd);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000241
242 /* Open the tty as standard input, continued */
Denis Vlasenko397de612008-03-17 08:55:44 +0000243// xmove_fd(fd, 0);
244// /* fd is >= cur_dir_fd, and cur_dir_fd gets closed too here: */
245// while (fd > 2)
246// close(fd--);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000247
Denis Vlasenko397de612008-03-17 08:55:44 +0000248 /* Set proper protections and ownership. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000249 fchown(0, 0, 0); /* 0:0 */
Denis Vlasenko397de612008-03-17 08:55:44 +0000250 fchmod(0, 0620); /* crw--w---- */
Robert Griebl1fca5582002-06-04 20:45:46 +0000251 } else {
Robert Griebl1fca5582002-06-04 20:45:46 +0000252 /*
253 * Standard input should already be connected to an open port. Make
254 * sure it is open for read/write.
255 */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000256 if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000257 bb_error_msg_and_die("stdin is not open for read/write");
Robert Griebl1fca5582002-06-04 20:45:46 +0000258 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000259}
260
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000261/* termios_init - initialize termios settings */
262static void termios_init(struct termios *tp, int speed, struct options *op)
Robert Griebl1fca5582002-06-04 20:45:46 +0000263{
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000264 speed_t ispeed, ospeed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000265 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000266 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000267 * Special characters are set after we have read the login name; all
268 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000269 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000270 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000271 /* flush input and output queues, important for modems! */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200272 tcflush(0, TCIOFLUSH);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000273 ispeed = ospeed = speed;
274 if (speed == B0) {
275 /* Speed was specified as "0" on command line.
276 * Just leave it unchanged */
277 ispeed = cfgetispeed(tp);
278 ospeed = cfgetospeed(tp);
279 }
280 tp->c_cflag = CS8 | HUPCL | CREAD;
Denis Vlasenko397de612008-03-17 08:55:44 +0000281 if (op->flags & F_LOCAL)
Robert Griebl1fca5582002-06-04 20:45:46 +0000282 tp->c_cflag |= CLOCAL;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000283 cfsetispeed(tp, ispeed);
284 cfsetospeed(tp, ospeed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000285
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200286 tp->c_iflag = tp->c_lflag = 0;
Rob Landleyf78ab532006-08-24 20:00:44 +0000287 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000288 tp->c_cc[VMIN] = 1;
289 tp->c_cc[VTIME] = 0;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200290#ifdef __linux__
291 tp->c_line = 0;
292#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000293
294 /* Optionally enable hardware flow control */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000295#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000296 if (op->flags & F_RTSCTS)
297 tp->c_cflag |= CRTSCTS;
298#endif
299
Denis Vlasenko202ac502008-11-05 13:20:58 +0000300 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000301
Robert Griebl1fca5582002-06-04 20:45:46 +0000302 debug("term_io 2\n");
303}
304
305/* auto_baud - extract baud rate from modem status message */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000306static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000307{
308 int speed;
309 int vmin;
310 unsigned iflag;
Robert Griebl1fca5582002-06-04 20:45:46 +0000311 char *bp;
312 int nread;
313
314 /*
315 * This works only if the modem produces its status code AFTER raising
316 * the DCD line, and if the computer is fast enough to set the proper
317 * baud rate before the message has gone by. We expect a message of the
318 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000319 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000320 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000321 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000322 * The number is interpreted as the baud rate of the incoming call. If the
323 * modem does not tell us the baud rate within one second, we will keep
324 * using the current baud rate. It is advisable to enable BREAK
325 * processing (comma-separated list of baud rates) if the processing of
326 * modem status messages is enabled.
327 */
328
329 /*
330 * Use 7-bit characters, don't block if input queue is empty. Errors will
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000331 * be dealt with later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000332 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000333 iflag = tp->c_iflag;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000334 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
Robert Griebl1fca5582002-06-04 20:45:46 +0000335 vmin = tp->c_cc[VMIN];
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000336 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000337 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000338
339 /*
340 * Wait for a while, then read everything the modem has said so far and
341 * try to extract the speed of the dial-in call.
342 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000343 sleep(1);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000344 nread = safe_read(STDIN_FILENO, buf, size_buf - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000345 if (nread > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000346 buf[nread] = '\0';
347 for (bp = buf; bp < buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000348 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000349 speed = bcode(bp);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200350 if (speed > 0)
351 cfsetspeed(tp, speed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000352 break;
353 }
354 }
355 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000356
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000357 /* Restore terminal settings. Errors will be dealt with later on. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000358 tp->c_iflag = iflag;
359 tp->c_cc[VMIN] = vmin;
Denis Vlasenko202ac502008-11-05 13:20:58 +0000360 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000361}
362
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000363/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000364static void do_prompt(struct options *op)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000365{
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000366#ifdef ISSUE
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000367 print_login_issue(op->issue, op->tty);
368#endif
369 print_login_prompt();
370}
371
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000372#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000373/* all_is_upcase - string contains upper case without lower case */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000374/* returns 1 if true, 0 if false */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000375static int all_is_upcase(const char *s)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000376{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000377 while (*s)
378 if (islower(*s++))
379 return 0;
380 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000381}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000382#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000383
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000384/* get_logname - get user name, establish parity, speed, erase, kill, eol;
385 * return NULL on BREAK, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000386static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000387 struct options *op, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000388{
Robert Griebl1fca5582002-06-04 20:45:46 +0000389 char *bp;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200390 char c; /* input character, full eight bits */
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000391 char ascval; /* low 7 bits of input character */
392 int bits; /* # of "1" bits per character */
393 int mask; /* mask with 1 bit up */
Denis Vlasenko397de612008-03-17 08:55:44 +0000394 static const char erase[][3] = {/* backspace-space-backspace */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000395 "\010\040\010", /* space parity */
396 "\010\040\010", /* odd parity */
397 "\210\240\210", /* even parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000398 "\010\040\010", /* 8 bit no parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000399 };
400
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000401 /* NB: *cp is pre-initialized with init_chardata */
Robert Griebl1fca5582002-06-04 20:45:46 +0000402
403 /* Flush pending input (esp. after parsing or switching the baud rate). */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000404 sleep(1);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200405 tcflush(0, TCIOFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000406
407 /* Prompt for and read a login name. */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000408 logname[0] = '\0';
409 while (!logname[0]) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000410 /* Write issue file and prompt, with "parity" bit == 0. */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000411 do_prompt(op);
Robert Griebl1fca5582002-06-04 20:45:46 +0000412
413 /* Read name, watch for break, parity, erase, kill, end-of-line. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000414 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000415 cp->eol = '\0';
416 while (cp->eol == '\0') {
Robert Griebl1fca5582002-06-04 20:45:46 +0000417
418 /* Do not report trivial EINTR/EIO errors. */
Denys Vlasenko765b0ee2010-05-22 21:17:46 +0200419 errno = EINTR; /* make read of 0 bytes be silent too */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000420 if (read(STDIN_FILENO, &c, 1) < 1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000421 if (errno == EINTR || errno == EIO)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000422 exit(EXIT_SUCCESS);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200423 bb_perror_msg_and_die(bb_msg_read_error);
Robert Griebl1fca5582002-06-04 20:45:46 +0000424 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000425
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000426 /* BREAK. If we have speeds to try,
427 * return NULL (will switch speeds and return here) */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000428 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000429 return NULL;
430
431 /* Do parity bit handling. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000432 if (!(op->flags & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000433 bits = 1;
434 mask = 1;
Denis Vlasenko397de612008-03-17 08:55:44 +0000435 while (mask & 0x7f) {
436 if (mask & c)
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000437 bits++; /* count "1" bits */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000438 mask <<= 1;
439 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000440 /* ... |= 2 - even, 1 - odd */
441 cp->parity |= 2 - (bits & 1);
Robert Griebl1fca5582002-06-04 20:45:46 +0000442 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000443
Denis Vlasenko703aa132006-10-23 22:43:02 +0000444 /* Do erase, kill and end-of-line processing. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000445 ascval = c & 0x7f;
Robert Griebl1fca5582002-06-04 20:45:46 +0000446 switch (ascval) {
447 case CR:
448 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000449 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000450 cp->eol = ascval; /* set end-of-line char */
Robert Griebl1fca5582002-06-04 20:45:46 +0000451 break;
452 case BS:
453 case DEL:
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000454#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000455 case '#':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000456#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000457 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000458 if (bp > logname) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000459 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000460 bp--;
461 }
462 break;
463 case CTL('U'):
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->kill = ascval; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000468 while (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('D'):
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000474 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000475 default:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100476 if (ascval < ' ') {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000477 /* ignore garbage characters */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000478 } else if ((int)(bp - logname) >= size_logname - 1) {
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200479 bb_error_msg_and_die("input overrun");
Robert Griebl1fca5582002-06-04 20:45:46 +0000480 } else {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000481 full_write(STDOUT_FILENO, &c, 1); /* echo the character */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000482 *bp++ = ascval; /* and store it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000483 }
484 break;
485 }
486 }
487 }
488 /* Handle names with upper case and no lower case. */
489
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000490#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000491 cp->capslock = all_is_upcase(logname);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000492 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000493 for (bp = logname; *bp; bp++)
494 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000495 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000496 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000497#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000498 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000499}
500
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000501/* termios_final - set the final tty mode bits */
502static void termios_final(struct options *op, struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000503{
504 /* General terminal-independent stuff. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000505 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000506 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
507 /* no longer| ECHOCTL | ECHOPRT */
508 tp->c_oflag |= OPOST;
509 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000510 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
511 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
512 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000513 tp->c_cc[VEOL] = DEF_EOL;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200514#ifdef VSWTC
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000515 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200516#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000517
518 /* Account for special characters seen in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000519 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000520 tp->c_iflag |= ICRNL; /* map CR in input to NL */
521 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000522 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000523 tp->c_cc[VERASE] = cp->erase; /* set erase character */
524 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000525
526 /* Account for the presence or absence of parity bits in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000527 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000528 case 0: /* space (always 0) parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000529// I bet most people go here - they use only 7-bit chars in usernames....
Robert Griebl1fca5582002-06-04 20:45:46 +0000530 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000531 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000532 tp->c_cflag |= PARODD;
533 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000534 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000535 tp->c_cflag |= PARENB;
536 tp->c_iflag |= INPCK | ISTRIP;
537 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000538 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000539 tp->c_cflag &= ~CSIZE;
540 tp->c_cflag |= CS7;
Denis Vlasenko397de612008-03-17 08:55:44 +0000541// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes -
542// it's probably some umlauts etc, but definitely NOT 7-bit!!!
543// Entire parity detection madness here just begs for deletion...
Robert Griebl1fca5582002-06-04 20:45:46 +0000544 break;
545 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000546
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000547 /* Account for upper case without lower case. */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000548#ifdef HANDLE_ALLCAPS
Robert Griebl1fca5582002-06-04 20:45:46 +0000549 if (cp->capslock) {
550 tp->c_iflag |= IUCLC;
551 tp->c_lflag |= XCASE;
552 tp->c_oflag |= OLCUC;
553 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000554#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000555 /* Optionally enable hardware flow control */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000556#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000557 if (op->flags & F_RTSCTS)
558 tp->c_cflag |= CRTSCTS;
559#endif
560
561 /* Finally, make the new settings effective */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200562 if (tcsetattr_stdin_TCSANOW(tp) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200563 bb_perror_msg_and_die("tcsetattr");
Robert Griebl1fca5582002-06-04 20:45:46 +0000564}
565
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000566int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000567int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000568{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000569 int n;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200570 pid_t pid;
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000571 char *fakehost = NULL; /* Fake hostname for ut_host */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000572 char *logname; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000573 /* Merging these into "struct local" may _seem_ to reduce
574 * parameter passing, but today's gcc will inline
575 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000576 struct chardata chardata; /* set by get_logname() */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000577 struct termios termios; /* terminal mode bits */
578 struct options options;
579
Denis Vlasenko397de612008-03-17 08:55:44 +0000580 chardata = init_chardata;
581
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000582 memset(&options, 0, sizeof(options));
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200583 options.login = _PATH_LOGIN; /* default login program */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000584 options.tty = "tty1"; /* default tty line */
585 options.initstring = ""; /* modem init string */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000586#ifdef ISSUE
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000587 options.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000588#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000589
Denis Vlasenko397de612008-03-17 08:55:44 +0000590 /* Parse command-line arguments. */
591 parse_args(argv, &options, &fakehost);
592
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000593 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000594
595 /* Create new session, lose controlling tty, if any */
596 /* docs/ctty.htm says:
597 * "This is allowed only when the current process
598 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000599 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000600 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000601 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000602 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
603 xdup2(n, 1);
604 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000605 while (n > 2)
606 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000607
608 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000609 die_sleep = 10;
610 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000611 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000612 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000613 logmode = LOGMODE_BOTH;
614
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000615#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000616 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000617 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000618 debug(argv[n]);
619 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000620 }
621#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000622
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000623 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000624 /* If it's not "-" and not taken yet, it will become our ctty */
625 debug("calling open_tty\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000626 open_tty(options.tty);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000627 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000628 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000629 xdup2(0, 1);
630 xdup2(0, 2);
631
632 /*
633 * The following ioctl will fail if stdin is not a tty, but also when
634 * there is noise on the modem control lines. In the latter case, the
635 * common course of action is (1) fix your cables (2) give the modem more
636 * time to properly reset after hanging up. SunOS users can achieve (2)
637 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
638 * 5 seconds seems to be a good value.
639 */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200640 if (tcgetattr(0, &termios) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200641 bb_perror_msg_and_die("tcgetattr");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000642
Denys Vlasenko3a416112010-04-05 22:10:38 +0200643 pid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000644#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000645// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000646 // /* Forcibly make fd 0 our controlling tty, even if another session
647 // * has it as a ctty. (Another session loses ctty). */
648 // ioctl(0, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000649 /* Make ourself a foreground process group within our session */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200650 tcsetpgrp(0, pid);
Denis Vlasenko397de612008-03-17 08:55:44 +0000651#endif
652
Denis Vlasenko397de612008-03-17 08:55:44 +0000653 /* Update the utmp file. This tty is ours now! */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200654 update_utmp(pid, LOGIN_PROCESS, options.tty, "LOGIN", fakehost);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000655
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000656 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
657 debug("calling termios_init\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000658 termios_init(&termios, options.speeds[0], &options);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000659
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000660 /* Write the modem init string and DON'T flush the buffers */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000661 if (options.flags & F_INITSTRING) {
662 debug("writing init string\n");
Denys Vlasenko729ecb82010-06-07 14:14:26 +0200663 full_write1_str(options.initstring);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000664 }
665
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000666 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000667 debug("before autobaud\n");
668 if (options.flags & F_PARSE)
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000669 auto_baud(line_buf, sizeof(line_buf), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000670
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000671 /* Set the optional timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000672 alarm(options.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000673
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000674 /* Optionally wait for CR or LF before writing /etc/issue */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000675 if (options.flags & F_WAITCRLF) {
676 char ch;
677
678 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000679 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000680 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000681 ch &= 0x7f; /* strip "parity bit" */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000682 if (ch == '\n' || ch == '\r')
683 break;
684 }
685 }
686
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000687 logname = NULL;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000688 if (!(options.flags & F_NOPROMPT)) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000689 /* NB:termios_init already set line speed
690 * to options.speeds[0] */
691 int baud_index = 0;
692
693 while (1) {
694 /* Read the login name. */
695 debug("reading login name\n");
696 logname = get_logname(line_buf, sizeof(line_buf),
Denis Vlasenko68404f12008-03-17 09:00:54 +0000697 &options, &chardata);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000698 if (logname)
699 break;
700 /* we are here only if options.numspeed > 1 */
701 baud_index = (baud_index + 1) % options.numspeed;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000702 cfsetispeed(&termios, options.speeds[baud_index]);
703 cfsetospeed(&termios, options.speeds[baud_index]);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000704 tcsetattr_stdin_TCSANOW(&termios);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000705 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000706 }
707
708 /* Disable timer. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000709 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000710
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000711 /* Finalize the termios settings. */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000712 termios_final(&options, &termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000713
714 /* Now the newline character should be properly written. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000715 full_write(STDOUT_FILENO, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000716
717 /* Let the login program take care of password validation. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000718 /* We use PATH because we trust that root doesn't set "bad" PATH,
719 * and getty is not suid-root applet. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000720 /* With -n, logname == NULL, and login will ask for username instead */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000721 BB_EXECLP(options.login, options.login, "--", logname, NULL);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200722 bb_error_msg_and_die("can't execute '%s'", options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000723}