blob: e60af3aa3674d0bcdcfc09a7c456497e39d87852 [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko1f084042011-01-24 00:16:50 +01002/* Based on agetty - 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>
Denys Vlasenko1f084042011-01-24 00:16:50 +01004 * This program is freely distributable.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00005 *
6 * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95
7 *
Denis Vlasenkob44c7902008-03-17 09:29:43 +00008 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00009 * - added Native Language Support
Denis Vlasenkob44c7902008-03-17 09:29:43 +000010 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000011 * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
12 * - enable hardware flow control before displaying /etc/issue
13 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000015 */
Robert Griebl1fca5582002-06-04 20:45:46 +000016
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Denis Vlasenkof7026522006-09-19 13:50:55 +000018#include <syslog.h>
Robert Griebl1fca5582002-06-04 20:45:46 +000019
Denis Vlasenko6476cc12006-11-07 01:52:10 +000020#if ENABLE_FEATURE_UTMP
Denys Vlasenko3a416112010-04-05 22:10:38 +020021# include <utmp.h> /* LOGIN_PROCESS */
Mike Frysingerb3810092005-07-01 01:29:44 +000022#endif
23
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020024#ifndef IUCLC
25# define IUCLC 0
26#endif
27
Denis Vlasenko6476cc12006-11-07 01:52:10 +000028/*
29 * Some heuristics to find out what environment we are in: if it is not
30 * System V, assume it is SunOS 4.
31 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000032#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010033# include <sys/utsname.h>
Denis Vlasenkof3fca912007-12-04 18:46:01 +000034#else /* if !sysV style, wtmp/utmp code is off */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010035# undef ENABLE_FEATURE_UTMP
36# undef ENABLE_FEATURE_WTMP
37# define ENABLE_FEATURE_UTMP 0
38# define ENABLE_FEATURE_WTMP 0
Eric Andersenfdfe2982002-10-10 03:55:09 +000039#endif /* LOGIN_PROCESS */
Robert Griebl1fca5582002-06-04 20:45:46 +000040
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010041
42/* The following is used for understandable diagnostics. */
43#ifdef DEBUGGING
44static FILE *dbf;
45# define DEBUGTERM "/dev/ttyp0"
46# define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0)
47#else
48# define debug(...) ((void)0)
49#endif
50
51
Denis Vlasenko6476cc12006-11-07 01:52:10 +000052/*
53 * Things you may want to modify.
54 *
55 * You may disagree with the default line-editing etc. characters defined
56 * below. Note, however, that DEL cannot be used for interrupt generation
57 * and for line editing at the same time.
58 */
Robert Griebl1fca5582002-06-04 20:45:46 +000059
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +010060/* I doubt there are systems which still... */
61/* ...have only uppercase: */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000062#undef HANDLE_ALLCAPS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +010063/* ...use # and @ for backspace and line erase: */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000064#undef ANCIENT_BS_KILL_CHARS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +010065/* It actually makes sense (tries to guess parity by looking at 7th bit)
66 * but it's broken, and interferes with non-ASCII login names
67 * (yes, I did receive complains from real users):
68 */
69#undef BROKEN_PARITY_DETECTION_CODE
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000070
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010071#undef _PATH_LOGIN
Denis Vlasenko6476cc12006-11-07 01:52:10 +000072#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000073
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010074/* Displayed before the login prompt.
75 * If ISSUE is not defined, getty will never display the contents of the
Denis Vlasenko6476cc12006-11-07 01:52:10 +000076 * /etc/issue file. You will not want to spit out large "issue" files at the
77 * wrong baud rate.
78 */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010079#define ISSUE "/etc/issue"
Eric Andersen77804ce2005-07-27 06:05:38 +000080
Robert Griebl1fca5582002-06-04 20:45:46 +000081/* Some shorthands for control characters. */
Denis Vlasenko397de612008-03-17 08:55:44 +000082#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000083#define CR CTL('M') /* carriage return */
84#define NL CTL('J') /* line feed */
85#define BS CTL('H') /* back space */
86#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000087
88/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000089#define DEF_ERASE DEL /* default erase character */
90#define DEF_INTR CTL('C') /* default interrupt character */
91#define DEF_QUIT CTL('\\') /* default quit char */
92#define DEF_KILL CTL('U') /* default kill char */
93#define DEF_EOF CTL('D') /* default EOF char */
94#define DEF_EOL '\n'
95#define DEF_SWITCH 0 /* default switch char */
Robert Griebl1fca5582002-06-04 20:45:46 +000096
Denis Vlasenko6476cc12006-11-07 01:52:10 +000097/*
98 * When multiple baud rates are specified on the command line, the first one
99 * we will try is the first one specified.
100 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000101#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +0000102
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000103/* Storage for command-line options. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000104struct options {
Denis Vlasenko13858992006-10-08 12:49:22 +0000105 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000106 const char *login; /* login program */
107 const char *tty; /* name of tty */
108 const char *initstring; /* modem init string */
109 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000110 int numspeed; /* number of baud rates to try */
111 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +0000112};
113
Robert Griebl1fca5582002-06-04 20:45:46 +0000114/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +0000115struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000116 unsigned char erase; /* erase character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100117#ifdef ANCIENT_BS_KILL_CHARS
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000118 unsigned char kill; /* kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100119#endif
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000120 unsigned char eol; /* end-of-line character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100121#ifdef BROKEN_PARITY_DETECTION_CODE
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000122 unsigned char parity; /* what parity did we see */
Denis Vlasenko397de612008-03-17 08:55:44 +0000123 /* (parity & 1): saw odd parity char with 7th bit set */
124 /* (parity & 2): saw even parity char with 7th bit set */
125 /* parity == 0: probably 7-bit, space parity? */
126 /* parity == 1: probably 7-bit, odd parity? */
127 /* parity == 2: probably 7-bit, even parity? */
128 /* parity == 3: definitely 8 bit, no parity! */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100129 /* Hmm... with any value of parity "8 bit, no parity" is possible */
130#endif
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000131#ifdef HANDLE_ALLCAPS
132 unsigned char capslock; /* upper case without lower case */
133#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000134};
135
136/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000137static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000138 DEF_ERASE, /* default erase character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100139#ifdef ANCIENT_BS_KILL_CHARS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000140 DEF_KILL, /* default kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100141#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000142 13, /* default eol char */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100143#ifdef BROKEN_PARITY_DETECTION_CODE
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000144 0, /* space parity */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100145#endif
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000146#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000147 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000148#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000149};
150
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000151#define line_buf bb_common_bufsiz1
Robert Griebl1fca5582002-06-04 20:45:46 +0000152
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100153//usage:#define getty_trivial_usage
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100154//usage: "[OPTIONS] BAUD_RATE[,BAUD_RATE]... TTY [TERMTYPE]"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100155//usage:#define getty_full_usage "\n\n"
156//usage: "Open a tty, prompt for a login name, then invoke /bin/login\n"
157//usage: "\nOptions:"
158//usage: "\n -h Enable hardware (RTS/CTS) flow control"
159//usage: "\n -i Don't display /etc/issue"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100160//usage: "\n -L Local line, set CLOCAL on it"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100161//usage: "\n -m Get baud rate from modem's CONNECT status message"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100162//usage: "\n -w Wait for CR or LF before sending /etc/issue"
163//usage: "\n -n Don't prompt for a login name"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100164//usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue"
165//usage: "\n -l LOGIN Invoke LOGIN instead of /bin/login"
166//usage: "\n -t SEC Terminate after SEC if no username is read"
167//usage: "\n -I INITSTR Send INITSTR before anything else"
168//usage: "\n -H HOST Log HOST into the utmp file as the hostname"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100169//usage: "\n"
170//usage: "\nBAUD_RATE of 0 leaves it unchanged"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100171
172static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
173#define F_INITSTRING (1 << 0) /* -I */
174#define F_LOCAL (1 << 1) /* -L */
175#define F_FAKEHOST (1 << 2) /* -H */
176#define F_CUSTISSUE (1 << 3) /* -f */
177#define F_RTSCTS (1 << 4) /* -h */
178#define F_NOISSUE (1 << 5) /* -i */
179#define F_LOGIN (1 << 6) /* -l */
180#define F_PARSE (1 << 7) /* -m */
181#define F_TIMEOUT (1 << 8) /* -t */
182#define F_WAITCRLF (1 << 9) /* -w */
183#define F_NOPROMPT (1 << 10) /* -n */
Robert Griebl1fca5582002-06-04 20:45:46 +0000184
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000185
Denys Vlasenko1f084042011-01-24 00:16:50 +0100186/* convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000187static int bcode(const char *s)
188{
Denis Vlasenko397de612008-03-17 08:55:44 +0000189 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
190 if (value < 0) /* bad terminating char, overflow, etc */
191 return value;
192 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000193}
194
Denys Vlasenko1f084042011-01-24 00:16:50 +0100195/* parse alternate baud rates */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000196static void parse_speeds(struct options *op, char *arg)
197{
198 char *cp;
199
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000200 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000201 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000202 while ((cp = strsep(&arg, ",")) != NULL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000203 op->speeds[op->numspeed] = bcode(cp);
Mike Frysingerfe013a72009-04-09 07:08:04 +0000204 if (op->speeds[op->numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000205 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000206 /* note: arg "0" turns into speed B0 */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000207 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000208 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000209 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000210 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000211 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000212}
213
Denys Vlasenko1f084042011-01-24 00:16:50 +0100214/* parse command-line arguments */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000215static void parse_args(char **argv, struct options *op, char **fakehost_p)
Robert Griebl1fca5582002-06-04 20:45:46 +0000216{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000217 char *ts;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100218 int flags;
Robert Griebl1fca5582002-06-04 20:45:46 +0000219
Denis Vlasenko397de612008-03-17 08:55:44 +0000220 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100221 flags = getopt32(argv, opt_string,
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000222 &(op->initstring), fakehost_p, &(op->issue),
Denis Vlasenko397de612008-03-17 08:55:44 +0000223 &(op->login), &op->timeout);
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100224 if (flags & F_INITSTRING) {
Denys Vlasenko53600592010-10-23 21:06:06 +0200225 op->initstring = xstrdup(op->initstring);
226 /* decode \ddd octal codes into chars */
227 strcpy_and_process_escape_sequences((char*)op->initstring, op->initstring);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000228 }
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100229 argv += optind;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000230 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000231
232 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000233 op->tty = argv[0]; /* tty name */
234 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000235 if (isdigit(argv[0][0])) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000236 /* a number first, assume it's a speed (BSD style) */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000237 op->tty = ts; /* tty name is in argv[1] */
238 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000239 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000240 parse_speeds(op, ts);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200241 applet_name = xasprintf("getty: %s", op->tty);
Denis Vlasenko397de612008-03-17 08:55:44 +0000242
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000243 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000244 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000245
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000246 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000247}
248
Denys Vlasenko1f084042011-01-24 00:16:50 +0100249/* set up tty as standard input, output, error */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000250static void open_tty(const char *tty)
Robert Griebl1fca5582002-06-04 20:45:46 +0000251{
Robert Griebl1fca5582002-06-04 20:45:46 +0000252 /* Set up new standard input, unless we are given an already opened port. */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000253 if (NOT_LONE_DASH(tty)) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000254 if (tty[0] != '/')
255 tty = xasprintf("/dev/%s", tty); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000256
257 /* Open the tty as standard input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000258 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000259 close(0);
260 /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000261
Denis Vlasenko397de612008-03-17 08:55:44 +0000262 /* Set proper protections and ownership. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000263 fchown(0, 0, 0); /* 0:0 */
Denis Vlasenko397de612008-03-17 08:55:44 +0000264 fchmod(0, 0620); /* crw--w---- */
Robert Griebl1fca5582002-06-04 20:45:46 +0000265 } else {
Robert Griebl1fca5582002-06-04 20:45:46 +0000266 /*
267 * Standard input should already be connected to an open port. Make
268 * sure it is open for read/write.
269 */
Denys Vlasenko006416e2011-01-22 17:29:53 +0100270 if ((fcntl(0, F_GETFL) & (O_RDWR|O_RDONLY|O_WRONLY)) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000271 bb_error_msg_and_die("stdin is not open for read/write");
Robert Griebl1fca5582002-06-04 20:45:46 +0000272 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000273}
274
Denys Vlasenko1f084042011-01-24 00:16:50 +0100275/* initialize termios settings */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100276static void termios_init(struct termios *tp, int speed)
Robert Griebl1fca5582002-06-04 20:45:46 +0000277{
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100278 /* Flush input and output queues, important for modems! */
279 /* TODO: sleep(1)? Users report lost chars, and I hesitate
280 * to use tcdrain here instead of tcflush */
281 tcflush(0, TCIOFLUSH);
282
283 /* Set speed if it wasn't specified as "0" on command line. */
Denys Vlasenko1f084042011-01-24 00:16:50 +0100284 if (speed != B0)
285 cfsetspeed(tp, speed);
286
Robert Griebl1fca5582002-06-04 20:45:46 +0000287 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000288 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000289 * Special characters are set after we have read the login name; all
290 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000291 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000292 */
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000293 tp->c_cflag = CS8 | HUPCL | CREAD;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100294 if (option_mask32 & F_LOCAL)
Robert Griebl1fca5582002-06-04 20:45:46 +0000295 tp->c_cflag |= CLOCAL;
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100296 tp->c_iflag = 0;
297 tp->c_lflag = 0;
Rob Landleyf78ab532006-08-24 20:00:44 +0000298 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000299 tp->c_cc[VMIN] = 1;
300 tp->c_cc[VTIME] = 0;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200301#ifdef __linux__
302 tp->c_line = 0;
303#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000304
305 /* Optionally enable hardware flow control */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000306#ifdef CRTSCTS
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100307 if (option_mask32 & F_RTSCTS)
Robert Griebl1fca5582002-06-04 20:45:46 +0000308 tp->c_cflag |= CRTSCTS;
309#endif
310
Denis Vlasenko202ac502008-11-05 13:20:58 +0000311 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000312
Robert Griebl1fca5582002-06-04 20:45:46 +0000313 debug("term_io 2\n");
314}
315
Denys Vlasenko1f084042011-01-24 00:16:50 +0100316/* extract baud rate from modem status message */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000317static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000318{
319 int speed;
320 int vmin;
321 unsigned iflag;
Robert Griebl1fca5582002-06-04 20:45:46 +0000322 char *bp;
323 int nread;
324
325 /*
326 * This works only if the modem produces its status code AFTER raising
327 * the DCD line, and if the computer is fast enough to set the proper
328 * baud rate before the message has gone by. We expect a message of the
329 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000330 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000331 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000332 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000333 * The number is interpreted as the baud rate of the incoming call. If the
334 * modem does not tell us the baud rate within one second, we will keep
335 * using the current baud rate. It is advisable to enable BREAK
336 * processing (comma-separated list of baud rates) if the processing of
337 * modem status messages is enabled.
338 */
339
340 /*
341 * Use 7-bit characters, don't block if input queue is empty. Errors will
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000342 * be dealt with later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000343 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000344 iflag = tp->c_iflag;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000345 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
Robert Griebl1fca5582002-06-04 20:45:46 +0000346 vmin = tp->c_cc[VMIN];
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000347 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000348 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000349
350 /*
351 * Wait for a while, then read everything the modem has said so far and
352 * try to extract the speed of the dial-in call.
353 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000354 sleep(1);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000355 nread = safe_read(STDIN_FILENO, buf, size_buf - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000356 if (nread > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000357 buf[nread] = '\0';
358 for (bp = buf; bp < buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000359 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000360 speed = bcode(bp);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200361 if (speed > 0)
362 cfsetspeed(tp, speed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000363 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
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000374#ifdef HANDLE_ALLCAPS
Denys Vlasenko1f084042011-01-24 00:16:50 +0100375/* does string contain upper case without lower case? */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000376static int all_is_upcase(const char *s)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000377{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000378 while (*s)
379 if (islower(*s++))
380 return 0;
381 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000382}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000383#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000384
Denys Vlasenko1f084042011-01-24 00:16:50 +0100385/* get user name, establish parity, speed, erase, kill, eol;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000386 * return NULL on BREAK, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000387static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000388 struct options *op, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000389{
Robert Griebl1fca5582002-06-04 20:45:46 +0000390 char *bp;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200391 char c; /* input character, full eight bits */
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000392 char ascval; /* low 7 bits of input character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100393#ifdef BROKEN_PARITY_DETECTION_CODE
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 };
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100400#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000401
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000402 /* NB: *cp is pre-initialized with init_chardata */
Robert Griebl1fca5582002-06-04 20:45:46 +0000403
404 /* Flush pending input (esp. after parsing or switching the baud rate). */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000405 sleep(1);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200406 tcflush(0, TCIOFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000407
408 /* Prompt for and read a login name. */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000409 logname[0] = '\0';
410 while (!logname[0]) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100411 /* Write issue file and prompt. */
Denys Vlasenko1f084042011-01-24 00:16:50 +0100412#ifdef ISSUE
413 if (!(option_mask32 & F_NOISSUE))
414 print_login_issue(op->issue, op->tty);
415#endif
416 print_login_prompt();
Robert Griebl1fca5582002-06-04 20:45:46 +0000417
418 /* Read name, watch for break, parity, erase, kill, end-of-line. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000419 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000420 cp->eol = '\0';
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100421 while (1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000422 /* Do not report trivial EINTR/EIO errors. */
Denys Vlasenko765b0ee2010-05-22 21:17:46 +0200423 errno = EINTR; /* make read of 0 bytes be silent too */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000424 if (read(STDIN_FILENO, &c, 1) < 1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000425 if (errno == EINTR || errno == EIO)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000426 exit(EXIT_SUCCESS);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200427 bb_perror_msg_and_die(bb_msg_read_error);
Robert Griebl1fca5582002-06-04 20:45:46 +0000428 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000429
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000430 /* BREAK. If we have speeds to try,
431 * return NULL (will switch speeds and return here) */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000432 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000433 return NULL;
434
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100435#ifdef BROKEN_PARITY_DETECTION_CODE
Robert Griebl1fca5582002-06-04 20:45:46 +0000436 /* Do parity bit handling. */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100437 if (!(option_mask32 & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */
438 int bits = 1;
439 int mask = 1;
Denis Vlasenko397de612008-03-17 08:55:44 +0000440 while (mask & 0x7f) {
441 if (mask & c)
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000442 bits++; /* count "1" bits */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000443 mask <<= 1;
444 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000445 /* ... |= 2 - even, 1 - odd */
446 cp->parity |= 2 - (bits & 1);
Robert Griebl1fca5582002-06-04 20:45:46 +0000447 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100448 ascval = c & 0x7f;
449#else
450 ascval = c;
451#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000452
Denis Vlasenko703aa132006-10-23 22:43:02 +0000453 /* Do erase, kill and end-of-line processing. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000454 switch (ascval) {
455 case CR:
456 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000457 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000458 cp->eol = ascval; /* set end-of-line char */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100459 goto got_logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000460 case BS:
461 case DEL:
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000462#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000463 case '#':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000464#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000465 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000466 if (bp > logname) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100467#ifdef BROKEN_PARITY_DETECTION_CODE
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000468 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100469#else
470 full_write(STDOUT_FILENO, "\010 \010", 3);
471#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000472 bp--;
473 }
474 break;
475 case CTL('U'):
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000476#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000477 case '@':
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000478 cp->kill = ascval; /* set kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100479#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000480 while (bp > logname) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100481#ifdef BROKEN_PARITY_DETECTION_CODE
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000482 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100483#else
484 full_write(STDOUT_FILENO, "\010 \010", 3);
485#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000486 bp--;
487 }
488 break;
489 case CTL('D'):
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000490 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000491 default:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100492 if (ascval < ' ') {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000493 /* ignore garbage characters */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100494 } else if ((int)(bp - logname) < size_logname - 1) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000495 full_write(STDOUT_FILENO, &c, 1); /* echo the character */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000496 *bp++ = ascval; /* and store it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000497 }
498 break;
499 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100500 } /* end of get char loop */
501 got_logname: ;
502 } /* while logname is empty */
Robert Griebl1fca5582002-06-04 20:45:46 +0000503
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000504#ifdef HANDLE_ALLCAPS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100505 /* Handle names with upper case and no lower case. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000506 cp->capslock = all_is_upcase(logname);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000507 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000508 for (bp = logname; *bp; bp++)
509 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000510 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000511 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000512#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000513 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000514}
515
Denys Vlasenko1f084042011-01-24 00:16:50 +0100516/* set the final tty mode bits */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100517static void termios_final(struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000518{
519 /* General terminal-independent stuff. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000520 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000521 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
522 /* no longer| ECHOCTL | ECHOPRT */
523 tp->c_oflag |= OPOST;
524 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000525 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
526 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
527 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000528 tp->c_cc[VEOL] = DEF_EOL;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200529#ifdef VSWTC
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000530 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200531#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000532
533 /* Account for special characters seen in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000534 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000535 tp->c_iflag |= ICRNL; /* map CR in input to NL */
536 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000537 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000538 tp->c_cc[VERASE] = cp->erase; /* set erase character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100539#ifdef ANCIENT_BS_KILL_CHARS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000540 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100541#else
542 tp->c_cc[VKILL] = DEF_KILL; /* set kill character */
543#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000544
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100545#ifdef BROKEN_PARITY_DETECTION_CODE
Robert Griebl1fca5582002-06-04 20:45:46 +0000546 /* Account for the presence or absence of parity bits in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000547 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000548 case 0: /* space (always 0) parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000549// I bet most people go here - they use only 7-bit chars in usernames....
Robert Griebl1fca5582002-06-04 20:45:46 +0000550 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000551 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000552 tp->c_cflag |= PARODD;
553 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000554 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000555 tp->c_cflag |= PARENB;
556 tp->c_iflag |= INPCK | ISTRIP;
557 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000558 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000559 tp->c_cflag &= ~CSIZE;
560 tp->c_cflag |= CS7;
Denis Vlasenko397de612008-03-17 08:55:44 +0000561// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes -
562// it's probably some umlauts etc, but definitely NOT 7-bit!!!
563// Entire parity detection madness here just begs for deletion...
Robert Griebl1fca5582002-06-04 20:45:46 +0000564 break;
565 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100566#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000567
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000568#ifdef HANDLE_ALLCAPS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100569 /* Account for upper case without lower case. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000570 if (cp->capslock) {
571 tp->c_iflag |= IUCLC;
572 tp->c_lflag |= XCASE;
573 tp->c_oflag |= OLCUC;
574 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000575#endif
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100576
Denis Vlasenko202ac502008-11-05 13:20:58 +0000577#ifdef CRTSCTS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100578 /* Optionally enable hardware flow control */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100579 if (option_mask32 & F_RTSCTS)
Robert Griebl1fca5582002-06-04 20:45:46 +0000580 tp->c_cflag |= CRTSCTS;
581#endif
582
583 /* Finally, make the new settings effective */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200584 if (tcsetattr_stdin_TCSANOW(tp) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200585 bb_perror_msg_and_die("tcsetattr");
Robert Griebl1fca5582002-06-04 20:45:46 +0000586}
587
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000588int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000589int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000590{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000591 int n;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200592 pid_t pid;
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000593 char *fakehost = NULL; /* Fake hostname for ut_host */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000594 char *logname; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000595 /* Merging these into "struct local" may _seem_ to reduce
596 * parameter passing, but today's gcc will inline
597 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000598 struct chardata chardata; /* set by get_logname() */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000599 struct termios termios; /* terminal mode bits */
600 struct options options;
601
Denis Vlasenko397de612008-03-17 08:55:44 +0000602 chardata = init_chardata;
603
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000604 memset(&options, 0, sizeof(options));
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200605 options.login = _PATH_LOGIN; /* default login program */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000606 options.tty = "tty1"; /* default tty line */
607 options.initstring = ""; /* modem init string */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000608#ifdef ISSUE
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000609 options.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000610#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000611
Denis Vlasenko397de612008-03-17 08:55:44 +0000612 /* Parse command-line arguments. */
613 parse_args(argv, &options, &fakehost);
614
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000615 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000616
617 /* Create new session, lose controlling tty, if any */
618 /* docs/ctty.htm says:
619 * "This is allowed only when the current process
620 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000621 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000622 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000623 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000624 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
625 xdup2(n, 1);
626 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000627 while (n > 2)
628 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000629
630 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000631 die_sleep = 10;
632 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000633 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000634 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000635 logmode = LOGMODE_BOTH;
636
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000637#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000638 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000639 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000640 debug(argv[n]);
641 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000642 }
643#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000644
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000645 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000646 /* If it's not "-" and not taken yet, it will become our ctty */
647 debug("calling open_tty\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000648 open_tty(options.tty);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000649 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000650 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000651 xdup2(0, 1);
652 xdup2(0, 2);
653
654 /*
655 * The following ioctl will fail if stdin is not a tty, but also when
656 * there is noise on the modem control lines. In the latter case, the
657 * common course of action is (1) fix your cables (2) give the modem more
658 * time to properly reset after hanging up. SunOS users can achieve (2)
659 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
660 * 5 seconds seems to be a good value.
661 */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200662 if (tcgetattr(0, &termios) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200663 bb_perror_msg_and_die("tcgetattr");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000664
Denys Vlasenko3a416112010-04-05 22:10:38 +0200665 pid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000666#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000667// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000668 // /* Forcibly make fd 0 our controlling tty, even if another session
669 // * has it as a ctty. (Another session loses ctty). */
670 // ioctl(0, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000671 /* Make ourself a foreground process group within our session */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200672 tcsetpgrp(0, pid);
Denis Vlasenko397de612008-03-17 08:55:44 +0000673#endif
674
Denis Vlasenko397de612008-03-17 08:55:44 +0000675 /* Update the utmp file. This tty is ours now! */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200676 update_utmp(pid, LOGIN_PROCESS, options.tty, "LOGIN", fakehost);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000677
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000678 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
679 debug("calling termios_init\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100680 termios_init(&termios, options.speeds[0]);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000681
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000682 /* Write the modem init string and DON'T flush the buffers */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100683 if (option_mask32 & F_INITSTRING) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000684 debug("writing init string\n");
Denys Vlasenko729ecb82010-06-07 14:14:26 +0200685 full_write1_str(options.initstring);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000686 }
687
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000688 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000689 debug("before autobaud\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100690 if (option_mask32 & F_PARSE)
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000691 auto_baud(line_buf, sizeof(line_buf), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000692
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000693 /* Set the optional timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000694 alarm(options.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000695
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000696 /* Optionally wait for CR or LF before writing /etc/issue */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100697 if (option_mask32 & F_WAITCRLF) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000698 char ch;
699
700 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000701 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000702 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000703 ch &= 0x7f; /* strip "parity bit" */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000704 if (ch == '\n' || ch == '\r')
705 break;
706 }
707 }
708
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000709 logname = NULL;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100710 if (!(option_mask32 & F_NOPROMPT)) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100711 /* NB: termios_init already set line speed
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000712 * to options.speeds[0] */
713 int baud_index = 0;
714
715 while (1) {
716 /* Read the login name. */
717 debug("reading login name\n");
718 logname = get_logname(line_buf, sizeof(line_buf),
Denis Vlasenko68404f12008-03-17 09:00:54 +0000719 &options, &chardata);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000720 if (logname)
721 break;
722 /* we are here only if options.numspeed > 1 */
723 baud_index = (baud_index + 1) % options.numspeed;
Denys Vlasenko1f084042011-01-24 00:16:50 +0100724 cfsetspeed(&termios, options.speeds[baud_index]);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000725 tcsetattr_stdin_TCSANOW(&termios);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000726 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000727 }
728
729 /* Disable timer. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000730 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000731
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000732 /* Finalize the termios settings. */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100733 termios_final(&termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000734
735 /* Now the newline character should be properly written. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000736 full_write(STDOUT_FILENO, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000737
738 /* Let the login program take care of password validation. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000739 /* We use PATH because we trust that root doesn't set "bad" PATH,
740 * and getty is not suid-root applet. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000741 /* With -n, logname == NULL, and login will ask for username instead */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000742 BB_EXECLP(options.login, options.login, "--", logname, NULL);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200743 bb_error_msg_and_die("can't execute '%s'", options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000744}