blob: 0f5e333a76122030259a05a884a14d4bc41d04f5 [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 */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010034# include <sys/utsname.h>
Denis Vlasenkof3fca912007-12-04 18:46:01 +000035#else /* if !sysV style, wtmp/utmp code is off */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010036# 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
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010042
43/* The following is used for understandable diagnostics. */
44#ifdef DEBUGGING
45static FILE *dbf;
46# define DEBUGTERM "/dev/ttyp0"
47# define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0)
48#else
49# define debug(...) ((void)0)
50#endif
51
52
Denis Vlasenko6476cc12006-11-07 01:52:10 +000053/*
54 * Things you may want to modify.
55 *
56 * You may disagree with the default line-editing etc. characters defined
57 * below. Note, however, that DEL cannot be used for interrupt generation
58 * and for line editing at the same time.
59 */
Robert Griebl1fca5582002-06-04 20:45:46 +000060
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000061/* I doubt there are systems which still need this */
62#undef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000063#undef ANCIENT_BS_KILL_CHARS
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000064
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010065#undef _PATH_LOGIN
Denis Vlasenko6476cc12006-11-07 01:52:10 +000066#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000067
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010068/* Displayed before the login prompt.
69 * If ISSUE is not defined, getty will never display the contents of the
Denis Vlasenko6476cc12006-11-07 01:52:10 +000070 * /etc/issue file. You will not want to spit out large "issue" files at the
71 * wrong baud rate.
72 */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010073#define ISSUE "/etc/issue"
Eric Andersen77804ce2005-07-27 06:05:38 +000074
Robert Griebl1fca5582002-06-04 20:45:46 +000075/* Some shorthands for control characters. */
Denis Vlasenko397de612008-03-17 08:55:44 +000076#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000077#define CR CTL('M') /* carriage return */
78#define NL CTL('J') /* line feed */
79#define BS CTL('H') /* back space */
80#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000081
82/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000083#define DEF_ERASE DEL /* default erase character */
84#define DEF_INTR CTL('C') /* default interrupt character */
85#define DEF_QUIT CTL('\\') /* default quit char */
86#define DEF_KILL CTL('U') /* default kill char */
87#define DEF_EOF CTL('D') /* default EOF char */
88#define DEF_EOL '\n'
89#define DEF_SWITCH 0 /* default switch char */
Robert Griebl1fca5582002-06-04 20:45:46 +000090
Denis Vlasenko6476cc12006-11-07 01:52:10 +000091/*
92 * When multiple baud rates are specified on the command line, the first one
93 * we will try is the first one specified.
94 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000095#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000096
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000097/* Storage for command-line options. */
Robert Griebl1fca5582002-06-04 20:45:46 +000098struct options {
Denis Vlasenko13858992006-10-08 12:49:22 +000099 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000100 const char *login; /* login program */
101 const char *tty; /* name of tty */
102 const char *initstring; /* modem init string */
103 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000104 int numspeed; /* number of baud rates to try */
105 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +0000106};
107
Robert Griebl1fca5582002-06-04 20:45:46 +0000108/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +0000109struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000110 unsigned char erase; /* erase character */
111 unsigned char kill; /* kill character */
112 unsigned char eol; /* end-of-line character */
113 unsigned char parity; /* what parity did we see */
Denis Vlasenko397de612008-03-17 08:55:44 +0000114 /* (parity & 1): saw odd parity char with 7th bit set */
115 /* (parity & 2): saw even parity char with 7th bit set */
116 /* parity == 0: probably 7-bit, space parity? */
117 /* parity == 1: probably 7-bit, odd parity? */
118 /* parity == 2: probably 7-bit, even parity? */
119 /* parity == 3: definitely 8 bit, no parity! */
120 /* Hmm... with any value of "parity" 8 bit, no parity is possible */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000121#ifdef HANDLE_ALLCAPS
122 unsigned char capslock; /* upper case without lower case */
123#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000124};
125
126/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000127static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000128 DEF_ERASE, /* default erase character */
129 DEF_KILL, /* default kill character */
130 13, /* default eol char */
131 0, /* space parity */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000132#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000133 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000134#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000135};
136
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000137#define line_buf bb_common_bufsiz1
Robert Griebl1fca5582002-06-04 20:45:46 +0000138
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100139//usage:#define getty_trivial_usage
140//usage: "[OPTIONS] BAUD_RATE TTY [TERMTYPE]"
141//usage:#define getty_full_usage "\n\n"
142//usage: "Open a tty, prompt for a login name, then invoke /bin/login\n"
143//usage: "\nOptions:"
144//usage: "\n -h Enable hardware (RTS/CTS) flow control"
145//usage: "\n -i Don't display /etc/issue"
146//usage: "\n -L Local line, don't do carrier detect"
147//usage: "\n -m Get baud rate from modem's CONNECT status message"
148//usage: "\n -w Wait for a CR or LF before sending /etc/issue"
149//usage: "\n -n Don't prompt the user for a login name"
150//usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue"
151//usage: "\n -l LOGIN Invoke LOGIN instead of /bin/login"
152//usage: "\n -t SEC Terminate after SEC if no username is read"
153//usage: "\n -I INITSTR Send INITSTR before anything else"
154//usage: "\n -H HOST Log HOST into the utmp file as the hostname"
155
156static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
157#define F_INITSTRING (1 << 0) /* -I */
158#define F_LOCAL (1 << 1) /* -L */
159#define F_FAKEHOST (1 << 2) /* -H */
160#define F_CUSTISSUE (1 << 3) /* -f */
161#define F_RTSCTS (1 << 4) /* -h */
162#define F_NOISSUE (1 << 5) /* -i */
163#define F_LOGIN (1 << 6) /* -l */
164#define F_PARSE (1 << 7) /* -m */
165#define F_TIMEOUT (1 << 8) /* -t */
166#define F_WAITCRLF (1 << 9) /* -w */
167#define F_NOPROMPT (1 << 10) /* -n */
Robert Griebl1fca5582002-06-04 20:45:46 +0000168
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000169
Denis Vlasenko397de612008-03-17 08:55:44 +0000170/* bcode - convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000171static int bcode(const char *s)
172{
Denis Vlasenko397de612008-03-17 08:55:44 +0000173 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
174 if (value < 0) /* bad terminating char, overflow, etc */
175 return value;
176 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000177}
178
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000179/* parse_speeds - parse alternate baud rates */
180static void parse_speeds(struct options *op, char *arg)
181{
182 char *cp;
183
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000184 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000185 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000186 while ((cp = strsep(&arg, ",")) != NULL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000187 op->speeds[op->numspeed] = bcode(cp);
Mike Frysingerfe013a72009-04-09 07:08:04 +0000188 if (op->speeds[op->numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000189 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000190 /* note: arg "0" turns into speed B0 */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000191 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000192 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000193 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000194 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000195 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000196}
197
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000198/* parse_args - parse command-line arguments */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000199static void parse_args(char **argv, struct options *op, char **fakehost_p)
Robert Griebl1fca5582002-06-04 20:45:46 +0000200{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000201 char *ts;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100202 int flags;
Robert Griebl1fca5582002-06-04 20:45:46 +0000203
Denis Vlasenko397de612008-03-17 08:55:44 +0000204 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100205 flags = getopt32(argv, opt_string,
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000206 &(op->initstring), fakehost_p, &(op->issue),
Denis Vlasenko397de612008-03-17 08:55:44 +0000207 &(op->login), &op->timeout);
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100208 if (flags & F_INITSTRING) {
Denys Vlasenko53600592010-10-23 21:06:06 +0200209 op->initstring = xstrdup(op->initstring);
210 /* decode \ddd octal codes into chars */
211 strcpy_and_process_escape_sequences((char*)op->initstring, op->initstring);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000212 }
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100213 argv += optind;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000214 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000215
216 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000217 op->tty = argv[0]; /* tty name */
218 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000219 if (isdigit(argv[0][0])) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000220 /* a number first, assume it's a speed (BSD style) */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000221 op->tty = ts; /* tty name is in argv[1] */
222 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000223 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000224 parse_speeds(op, ts);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200225 applet_name = xasprintf("getty: %s", op->tty);
Denis Vlasenko397de612008-03-17 08:55:44 +0000226
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000227 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000228 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000229
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000230 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000231}
232
Robert Griebl1fca5582002-06-04 20:45:46 +0000233/* open_tty - set up tty as standard { input, output, error } */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000234static void open_tty(const char *tty)
Robert Griebl1fca5582002-06-04 20:45:46 +0000235{
Robert Griebl1fca5582002-06-04 20:45:46 +0000236 /* Set up new standard input, unless we are given an already opened port. */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000237 if (NOT_LONE_DASH(tty)) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000238 if (tty[0] != '/')
239 tty = xasprintf("/dev/%s", tty); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000240
241 /* Open the tty as standard input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000242 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000243 close(0);
244 /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000245
Denis Vlasenko397de612008-03-17 08:55:44 +0000246 /* Set proper protections and ownership. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000247 fchown(0, 0, 0); /* 0:0 */
Denis Vlasenko397de612008-03-17 08:55:44 +0000248 fchmod(0, 0620); /* crw--w---- */
Robert Griebl1fca5582002-06-04 20:45:46 +0000249 } else {
Robert Griebl1fca5582002-06-04 20:45:46 +0000250 /*
251 * Standard input should already be connected to an open port. Make
252 * sure it is open for read/write.
253 */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000254 if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000255 bb_error_msg_and_die("stdin is not open for read/write");
Robert Griebl1fca5582002-06-04 20:45:46 +0000256 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000257}
258
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000259/* termios_init - initialize termios settings */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100260static void termios_init(struct termios *tp, int speed)
Robert Griebl1fca5582002-06-04 20:45:46 +0000261{
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000262 speed_t ispeed, ospeed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000263 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000264 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000265 * Special characters are set after we have read the login name; all
266 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000267 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000268 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000269 /* flush input and output queues, important for modems! */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200270 tcflush(0, TCIOFLUSH);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000271 ispeed = ospeed = speed;
272 if (speed == B0) {
273 /* Speed was specified as "0" on command line.
274 * Just leave it unchanged */
275 ispeed = cfgetispeed(tp);
276 ospeed = cfgetospeed(tp);
277 }
278 tp->c_cflag = CS8 | HUPCL | CREAD;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100279 if (option_mask32 & F_LOCAL)
Robert Griebl1fca5582002-06-04 20:45:46 +0000280 tp->c_cflag |= CLOCAL;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000281 cfsetispeed(tp, ispeed);
282 cfsetospeed(tp, ospeed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000283
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200284 tp->c_iflag = tp->c_lflag = 0;
Rob Landleyf78ab532006-08-24 20:00:44 +0000285 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000286 tp->c_cc[VMIN] = 1;
287 tp->c_cc[VTIME] = 0;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200288#ifdef __linux__
289 tp->c_line = 0;
290#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000291
292 /* Optionally enable hardware flow control */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000293#ifdef CRTSCTS
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100294 if (option_mask32 & F_RTSCTS)
Robert Griebl1fca5582002-06-04 20:45:46 +0000295 tp->c_cflag |= CRTSCTS;
296#endif
297
Denis Vlasenko202ac502008-11-05 13:20:58 +0000298 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000299
Robert Griebl1fca5582002-06-04 20:45:46 +0000300 debug("term_io 2\n");
301}
302
303/* auto_baud - extract baud rate from modem status message */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000304static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000305{
306 int speed;
307 int vmin;
308 unsigned iflag;
Robert Griebl1fca5582002-06-04 20:45:46 +0000309 char *bp;
310 int nread;
311
312 /*
313 * This works only if the modem produces its status code AFTER raising
314 * the DCD line, and if the computer is fast enough to set the proper
315 * baud rate before the message has gone by. We expect a message of the
316 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000317 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000318 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000319 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000320 * The number is interpreted as the baud rate of the incoming call. If the
321 * modem does not tell us the baud rate within one second, we will keep
322 * using the current baud rate. It is advisable to enable BREAK
323 * processing (comma-separated list of baud rates) if the processing of
324 * modem status messages is enabled.
325 */
326
327 /*
328 * Use 7-bit characters, don't block if input queue is empty. Errors will
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000329 * be dealt with later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000330 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000331 iflag = tp->c_iflag;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000332 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
Robert Griebl1fca5582002-06-04 20:45:46 +0000333 vmin = tp->c_cc[VMIN];
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000334 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000335 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000336
337 /*
338 * Wait for a while, then read everything the modem has said so far and
339 * try to extract the speed of the dial-in call.
340 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000341 sleep(1);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000342 nread = safe_read(STDIN_FILENO, buf, size_buf - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000343 if (nread > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000344 buf[nread] = '\0';
345 for (bp = buf; bp < buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000346 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000347 speed = bcode(bp);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200348 if (speed > 0)
349 cfsetspeed(tp, speed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000350 break;
351 }
352 }
353 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000354
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000355 /* Restore terminal settings. Errors will be dealt with later on. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000356 tp->c_iflag = iflag;
357 tp->c_cc[VMIN] = vmin;
Denis Vlasenko202ac502008-11-05 13:20:58 +0000358 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000359}
360
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000361/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000362static void do_prompt(struct options *op)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000363{
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000364#ifdef ISSUE
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100365 if (!(option_mask32 & F_NOISSUE))
366 print_login_issue(op->issue, op->tty);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000367#endif
368 print_login_prompt();
369}
370
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000371#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000372/* all_is_upcase - string contains upper case without lower case */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000373/* returns 1 if true, 0 if false */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000374static int all_is_upcase(const char *s)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000375{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000376 while (*s)
377 if (islower(*s++))
378 return 0;
379 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000380}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000381#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000382
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000383/* get_logname - get user name, establish parity, speed, erase, kill, eol;
384 * return NULL on BREAK, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000385static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000386 struct options *op, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000387{
Robert Griebl1fca5582002-06-04 20:45:46 +0000388 char *bp;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200389 char c; /* input character, full eight bits */
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000390 char ascval; /* low 7 bits of input character */
391 int bits; /* # of "1" bits per character */
392 int mask; /* mask with 1 bit up */
Denis Vlasenko397de612008-03-17 08:55:44 +0000393 static const char erase[][3] = {/* backspace-space-backspace */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000394 "\010\040\010", /* space parity */
395 "\010\040\010", /* odd parity */
396 "\210\240\210", /* even parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000397 "\010\040\010", /* 8 bit no parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000398 };
399
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000400 /* NB: *cp is pre-initialized with init_chardata */
Robert Griebl1fca5582002-06-04 20:45:46 +0000401
402 /* Flush pending input (esp. after parsing or switching the baud rate). */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000403 sleep(1);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200404 tcflush(0, TCIOFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000405
406 /* Prompt for and read a login name. */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000407 logname[0] = '\0';
408 while (!logname[0]) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000409 /* Write issue file and prompt, with "parity" bit == 0. */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000410 do_prompt(op);
Robert Griebl1fca5582002-06-04 20:45:46 +0000411
412 /* Read name, watch for break, parity, erase, kill, end-of-line. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000413 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000414 cp->eol = '\0';
415 while (cp->eol == '\0') {
Robert Griebl1fca5582002-06-04 20:45:46 +0000416
417 /* Do not report trivial EINTR/EIO errors. */
Denys Vlasenko765b0ee2010-05-22 21:17:46 +0200418 errno = EINTR; /* make read of 0 bytes be silent too */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000419 if (read(STDIN_FILENO, &c, 1) < 1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000420 if (errno == EINTR || errno == EIO)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000421 exit(EXIT_SUCCESS);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200422 bb_perror_msg_and_die(bb_msg_read_error);
Robert Griebl1fca5582002-06-04 20:45:46 +0000423 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000424
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000425 /* BREAK. If we have speeds to try,
426 * return NULL (will switch speeds and return here) */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000427 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000428 return NULL;
429
430 /* Do parity bit handling. */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100431 if (!(option_mask32 & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000432 bits = 1;
433 mask = 1;
Denis Vlasenko397de612008-03-17 08:55:44 +0000434 while (mask & 0x7f) {
435 if (mask & c)
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000436 bits++; /* count "1" bits */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000437 mask <<= 1;
438 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000439 /* ... |= 2 - even, 1 - odd */
440 cp->parity |= 2 - (bits & 1);
Robert Griebl1fca5582002-06-04 20:45:46 +0000441 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000442
Denis Vlasenko703aa132006-10-23 22:43:02 +0000443 /* Do erase, kill and end-of-line processing. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000444 ascval = c & 0x7f;
Robert Griebl1fca5582002-06-04 20:45:46 +0000445 switch (ascval) {
446 case CR:
447 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000448 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000449 cp->eol = ascval; /* set end-of-line char */
Robert Griebl1fca5582002-06-04 20:45:46 +0000450 break;
451 case BS:
452 case DEL:
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000453#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000454 case '#':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000455#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000456 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000457 if (bp > logname) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000458 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000459 bp--;
460 }
461 break;
462 case CTL('U'):
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000463#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000464 case '@':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000465#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000466 cp->kill = ascval; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000467 while (bp > logname) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000468 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000469 bp--;
470 }
471 break;
472 case CTL('D'):
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000473 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000474 default:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100475 if (ascval < ' ') {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000476 /* ignore garbage characters */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000477 } else if ((int)(bp - logname) >= size_logname - 1) {
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200478 bb_error_msg_and_die("input overrun");
Robert Griebl1fca5582002-06-04 20:45:46 +0000479 } else {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000480 full_write(STDOUT_FILENO, &c, 1); /* echo the character */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000481 *bp++ = ascval; /* and store it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000482 }
483 break;
484 }
485 }
486 }
487 /* Handle names with upper case and no lower case. */
488
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000489#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000490 cp->capslock = all_is_upcase(logname);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000491 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000492 for (bp = logname; *bp; bp++)
493 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000494 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000495 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000496#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000497 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000498}
499
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000500/* termios_final - set the final tty mode bits */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100501static void termios_final(struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000502{
503 /* General terminal-independent stuff. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000504 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000505 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
506 /* no longer| ECHOCTL | ECHOPRT */
507 tp->c_oflag |= OPOST;
508 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000509 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
510 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
511 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000512 tp->c_cc[VEOL] = DEF_EOL;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200513#ifdef VSWTC
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000514 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200515#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000516
517 /* Account for special characters seen in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000518 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000519 tp->c_iflag |= ICRNL; /* map CR in input to NL */
520 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000521 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000522 tp->c_cc[VERASE] = cp->erase; /* set erase character */
523 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000524
525 /* Account for the presence or absence of parity bits in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000526 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000527 case 0: /* space (always 0) parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000528// I bet most people go here - they use only 7-bit chars in usernames....
Robert Griebl1fca5582002-06-04 20:45:46 +0000529 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000530 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000531 tp->c_cflag |= PARODD;
532 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000533 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000534 tp->c_cflag |= PARENB;
535 tp->c_iflag |= INPCK | ISTRIP;
536 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000537 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000538 tp->c_cflag &= ~CSIZE;
539 tp->c_cflag |= CS7;
Denis Vlasenko397de612008-03-17 08:55:44 +0000540// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes -
541// it's probably some umlauts etc, but definitely NOT 7-bit!!!
542// Entire parity detection madness here just begs for deletion...
Robert Griebl1fca5582002-06-04 20:45:46 +0000543 break;
544 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000545
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000546 /* Account for upper case without lower case. */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000547#ifdef HANDLE_ALLCAPS
Robert Griebl1fca5582002-06-04 20:45:46 +0000548 if (cp->capslock) {
549 tp->c_iflag |= IUCLC;
550 tp->c_lflag |= XCASE;
551 tp->c_oflag |= OLCUC;
552 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000553#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000554 /* Optionally enable hardware flow control */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000555#ifdef CRTSCTS
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100556 if (option_mask32 & F_RTSCTS)
Robert Griebl1fca5582002-06-04 20:45:46 +0000557 tp->c_cflag |= CRTSCTS;
558#endif
559
560 /* Finally, make the new settings effective */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200561 if (tcsetattr_stdin_TCSANOW(tp) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200562 bb_perror_msg_and_die("tcsetattr");
Robert Griebl1fca5582002-06-04 20:45:46 +0000563}
564
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000565int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000566int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000567{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000568 int n;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200569 pid_t pid;
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000570 char *fakehost = NULL; /* Fake hostname for ut_host */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000571 char *logname; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000572 /* Merging these into "struct local" may _seem_ to reduce
573 * parameter passing, but today's gcc will inline
574 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000575 struct chardata chardata; /* set by get_logname() */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000576 struct termios termios; /* terminal mode bits */
577 struct options options;
578
Denis Vlasenko397de612008-03-17 08:55:44 +0000579 chardata = init_chardata;
580
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000581 memset(&options, 0, sizeof(options));
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200582 options.login = _PATH_LOGIN; /* default login program */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000583 options.tty = "tty1"; /* default tty line */
584 options.initstring = ""; /* modem init string */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000585#ifdef ISSUE
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000586 options.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000587#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000588
Denis Vlasenko397de612008-03-17 08:55:44 +0000589 /* Parse command-line arguments. */
590 parse_args(argv, &options, &fakehost);
591
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000592 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000593
594 /* Create new session, lose controlling tty, if any */
595 /* docs/ctty.htm says:
596 * "This is allowed only when the current process
597 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000598 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000599 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000600 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000601 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
602 xdup2(n, 1);
603 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000604 while (n > 2)
605 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000606
607 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000608 die_sleep = 10;
609 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000610 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000611 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000612 logmode = LOGMODE_BOTH;
613
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000614#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000615 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000616 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000617 debug(argv[n]);
618 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000619 }
620#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000621
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000622 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000623 /* If it's not "-" and not taken yet, it will become our ctty */
624 debug("calling open_tty\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000625 open_tty(options.tty);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000626 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000627 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000628 xdup2(0, 1);
629 xdup2(0, 2);
630
631 /*
632 * The following ioctl will fail if stdin is not a tty, but also when
633 * there is noise on the modem control lines. In the latter case, the
634 * common course of action is (1) fix your cables (2) give the modem more
635 * time to properly reset after hanging up. SunOS users can achieve (2)
636 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
637 * 5 seconds seems to be a good value.
638 */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200639 if (tcgetattr(0, &termios) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200640 bb_perror_msg_and_die("tcgetattr");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000641
Denys Vlasenko3a416112010-04-05 22:10:38 +0200642 pid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000643#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000644// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000645 // /* Forcibly make fd 0 our controlling tty, even if another session
646 // * has it as a ctty. (Another session loses ctty). */
647 // ioctl(0, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000648 /* Make ourself a foreground process group within our session */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200649 tcsetpgrp(0, pid);
Denis Vlasenko397de612008-03-17 08:55:44 +0000650#endif
651
Denis Vlasenko397de612008-03-17 08:55:44 +0000652 /* Update the utmp file. This tty is ours now! */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200653 update_utmp(pid, LOGIN_PROCESS, options.tty, "LOGIN", fakehost);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000654
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000655 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
656 debug("calling termios_init\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100657 termios_init(&termios, options.speeds[0]);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000658
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000659 /* Write the modem init string and DON'T flush the buffers */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100660 if (option_mask32 & F_INITSTRING) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000661 debug("writing init string\n");
Denys Vlasenko729ecb82010-06-07 14:14:26 +0200662 full_write1_str(options.initstring);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000663 }
664
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000665 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000666 debug("before autobaud\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100667 if (option_mask32 & F_PARSE)
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000668 auto_baud(line_buf, sizeof(line_buf), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000669
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000670 /* Set the optional timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000671 alarm(options.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000672
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000673 /* Optionally wait for CR or LF before writing /etc/issue */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100674 if (option_mask32 & F_WAITCRLF) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000675 char ch;
676
677 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000678 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000679 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000680 ch &= 0x7f; /* strip "parity bit" */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000681 if (ch == '\n' || ch == '\r')
682 break;
683 }
684 }
685
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000686 logname = NULL;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100687 if (!(option_mask32 & F_NOPROMPT)) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000688 /* NB:termios_init already set line speed
689 * to options.speeds[0] */
690 int baud_index = 0;
691
692 while (1) {
693 /* Read the login name. */
694 debug("reading login name\n");
695 logname = get_logname(line_buf, sizeof(line_buf),
Denis Vlasenko68404f12008-03-17 09:00:54 +0000696 &options, &chardata);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000697 if (logname)
698 break;
699 /* we are here only if options.numspeed > 1 */
700 baud_index = (baud_index + 1) % options.numspeed;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000701 cfsetispeed(&termios, options.speeds[baud_index]);
702 cfsetospeed(&termios, options.speeds[baud_index]);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000703 tcsetattr_stdin_TCSANOW(&termios);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000704 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000705 }
706
707 /* Disable timer. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000708 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000709
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000710 /* Finalize the termios settings. */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100711 termios_final(&termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000712
713 /* Now the newline character should be properly written. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000714 full_write(STDOUT_FILENO, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000715
716 /* Let the login program take care of password validation. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000717 /* We use PATH because we trust that root doesn't set "bad" PATH,
718 * and getty is not suid-root applet. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000719 /* With -n, logname == NULL, and login will ask for username instead */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000720 BB_EXECLP(options.login, options.login, "--", logname, NULL);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200721 bb_error_msg_and_die("can't execute '%s'", options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000722}