blob: 94c9147abebc3786592f1d66efd69e74d3b01242 [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
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +010061/* I doubt there are systems which still... */
62/* ...have only uppercase: */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000063#undef HANDLE_ALLCAPS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +010064/* ...use # and @ for backspace and line erase: */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000065#undef ANCIENT_BS_KILL_CHARS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +010066/* It actually makes sense (tries to guess parity by looking at 7th bit)
67 * but it's broken, and interferes with non-ASCII login names
68 * (yes, I did receive complains from real users):
69 */
70#undef BROKEN_PARITY_DETECTION_CODE
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000071
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010072#undef _PATH_LOGIN
Denis Vlasenko6476cc12006-11-07 01:52:10 +000073#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000074
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010075/* Displayed before the login prompt.
76 * If ISSUE is not defined, getty will never display the contents of the
Denis Vlasenko6476cc12006-11-07 01:52:10 +000077 * /etc/issue file. You will not want to spit out large "issue" files at the
78 * wrong baud rate.
79 */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +010080#define ISSUE "/etc/issue"
Eric Andersen77804ce2005-07-27 06:05:38 +000081
Robert Griebl1fca5582002-06-04 20:45:46 +000082/* Some shorthands for control characters. */
Denis Vlasenko397de612008-03-17 08:55:44 +000083#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000084#define CR CTL('M') /* carriage return */
85#define NL CTL('J') /* line feed */
86#define BS CTL('H') /* back space */
87#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000088
89/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000090#define DEF_ERASE DEL /* default erase character */
91#define DEF_INTR CTL('C') /* default interrupt character */
92#define DEF_QUIT CTL('\\') /* default quit char */
93#define DEF_KILL CTL('U') /* default kill char */
94#define DEF_EOF CTL('D') /* default EOF char */
95#define DEF_EOL '\n'
96#define DEF_SWITCH 0 /* default switch char */
Robert Griebl1fca5582002-06-04 20:45:46 +000097
Denis Vlasenko6476cc12006-11-07 01:52:10 +000098/*
99 * When multiple baud rates are specified on the command line, the first one
100 * we will try is the first one specified.
101 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000102#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +0000103
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000104/* Storage for command-line options. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000105struct options {
Denis Vlasenko13858992006-10-08 12:49:22 +0000106 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000107 const char *login; /* login program */
108 const char *tty; /* name of tty */
109 const char *initstring; /* modem init string */
110 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000111 int numspeed; /* number of baud rates to try */
112 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +0000113};
114
Robert Griebl1fca5582002-06-04 20:45:46 +0000115/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +0000116struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000117 unsigned char erase; /* erase character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100118#ifdef ANCIENT_BS_KILL_CHARS
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000119 unsigned char kill; /* kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100120#endif
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000121 unsigned char eol; /* end-of-line character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100122#ifdef BROKEN_PARITY_DETECTION_CODE
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000123 unsigned char parity; /* what parity did we see */
Denis Vlasenko397de612008-03-17 08:55:44 +0000124 /* (parity & 1): saw odd parity char with 7th bit set */
125 /* (parity & 2): saw even parity char with 7th bit set */
126 /* parity == 0: probably 7-bit, space parity? */
127 /* parity == 1: probably 7-bit, odd parity? */
128 /* parity == 2: probably 7-bit, even parity? */
129 /* parity == 3: definitely 8 bit, no parity! */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100130 /* Hmm... with any value of parity "8 bit, no parity" is possible */
131#endif
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000132#ifdef HANDLE_ALLCAPS
133 unsigned char capslock; /* upper case without lower case */
134#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000135};
136
137/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000138static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000139 DEF_ERASE, /* default erase character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100140#ifdef ANCIENT_BS_KILL_CHARS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000141 DEF_KILL, /* default kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100142#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000143 13, /* default eol char */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100144#ifdef BROKEN_PARITY_DETECTION_CODE
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000145 0, /* space parity */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100146#endif
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000147#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000148 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000149#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000150};
151
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000152#define line_buf bb_common_bufsiz1
Robert Griebl1fca5582002-06-04 20:45:46 +0000153
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100154//usage:#define getty_trivial_usage
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100155//usage: "[OPTIONS] BAUD_RATE[,BAUD_RATE]... TTY [TERMTYPE]"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100156//usage:#define getty_full_usage "\n\n"
157//usage: "Open a tty, prompt for a login name, then invoke /bin/login\n"
158//usage: "\nOptions:"
159//usage: "\n -h Enable hardware (RTS/CTS) flow control"
160//usage: "\n -i Don't display /etc/issue"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100161//usage: "\n -L Local line, set CLOCAL on it"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100162//usage: "\n -m Get baud rate from modem's CONNECT status message"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100163//usage: "\n -w Wait for CR or LF before sending /etc/issue"
164//usage: "\n -n Don't prompt for a login name"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100165//usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue"
166//usage: "\n -l LOGIN Invoke LOGIN instead of /bin/login"
167//usage: "\n -t SEC Terminate after SEC if no username is read"
168//usage: "\n -I INITSTR Send INITSTR before anything else"
169//usage: "\n -H HOST Log HOST into the utmp file as the hostname"
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100170//usage: "\n"
171//usage: "\nBAUD_RATE of 0 leaves it unchanged"
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100172
173static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
174#define F_INITSTRING (1 << 0) /* -I */
175#define F_LOCAL (1 << 1) /* -L */
176#define F_FAKEHOST (1 << 2) /* -H */
177#define F_CUSTISSUE (1 << 3) /* -f */
178#define F_RTSCTS (1 << 4) /* -h */
179#define F_NOISSUE (1 << 5) /* -i */
180#define F_LOGIN (1 << 6) /* -l */
181#define F_PARSE (1 << 7) /* -m */
182#define F_TIMEOUT (1 << 8) /* -t */
183#define F_WAITCRLF (1 << 9) /* -w */
184#define F_NOPROMPT (1 << 10) /* -n */
Robert Griebl1fca5582002-06-04 20:45:46 +0000185
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000186
Denis Vlasenko397de612008-03-17 08:55:44 +0000187/* bcode - convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000188static int bcode(const char *s)
189{
Denis Vlasenko397de612008-03-17 08:55:44 +0000190 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
191 if (value < 0) /* bad terminating char, overflow, etc */
192 return value;
193 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000194}
195
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000196/* parse_speeds - parse alternate baud rates */
197static void parse_speeds(struct options *op, char *arg)
198{
199 char *cp;
200
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000201 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000202 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000203 while ((cp = strsep(&arg, ",")) != NULL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000204 op->speeds[op->numspeed] = bcode(cp);
Mike Frysingerfe013a72009-04-09 07:08:04 +0000205 if (op->speeds[op->numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000206 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000207 /* note: arg "0" turns into speed B0 */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000208 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000209 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000210 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000211 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000212 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000213}
214
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000215/* parse_args - parse command-line arguments */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000216static void parse_args(char **argv, struct options *op, char **fakehost_p)
Robert Griebl1fca5582002-06-04 20:45:46 +0000217{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000218 char *ts;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100219 int flags;
Robert Griebl1fca5582002-06-04 20:45:46 +0000220
Denis Vlasenko397de612008-03-17 08:55:44 +0000221 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100222 flags = getopt32(argv, opt_string,
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000223 &(op->initstring), fakehost_p, &(op->issue),
Denis Vlasenko397de612008-03-17 08:55:44 +0000224 &(op->login), &op->timeout);
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100225 if (flags & F_INITSTRING) {
Denys Vlasenko53600592010-10-23 21:06:06 +0200226 op->initstring = xstrdup(op->initstring);
227 /* decode \ddd octal codes into chars */
228 strcpy_and_process_escape_sequences((char*)op->initstring, op->initstring);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000229 }
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100230 argv += optind;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000231 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000232
233 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000234 op->tty = argv[0]; /* tty name */
235 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000236 if (isdigit(argv[0][0])) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000237 /* a number first, assume it's a speed (BSD style) */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000238 op->tty = ts; /* tty name is in argv[1] */
239 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000240 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000241 parse_speeds(op, ts);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200242 applet_name = xasprintf("getty: %s", op->tty);
Denis Vlasenko397de612008-03-17 08:55:44 +0000243
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000244 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000245 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000246
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000247 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000248}
249
Robert Griebl1fca5582002-06-04 20:45:46 +0000250/* open_tty - set up tty as standard { input, output, error } */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000251static void open_tty(const char *tty)
Robert Griebl1fca5582002-06-04 20:45:46 +0000252{
Robert Griebl1fca5582002-06-04 20:45:46 +0000253 /* Set up new standard input, unless we are given an already opened port. */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000254 if (NOT_LONE_DASH(tty)) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000255 if (tty[0] != '/')
256 tty = xasprintf("/dev/%s", tty); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000257
258 /* Open the tty as standard input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000259 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000260 close(0);
261 /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000262
Denis Vlasenko397de612008-03-17 08:55:44 +0000263 /* Set proper protections and ownership. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000264 fchown(0, 0, 0); /* 0:0 */
Denis Vlasenko397de612008-03-17 08:55:44 +0000265 fchmod(0, 0620); /* crw--w---- */
Robert Griebl1fca5582002-06-04 20:45:46 +0000266 } else {
Robert Griebl1fca5582002-06-04 20:45:46 +0000267 /*
268 * Standard input should already be connected to an open port. Make
269 * sure it is open for read/write.
270 */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000271 if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR)
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000272 bb_error_msg_and_die("stdin is not open for read/write");
Robert Griebl1fca5582002-06-04 20:45:46 +0000273 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000274}
275
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000276/* termios_init - initialize termios settings */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100277static void termios_init(struct termios *tp, int speed)
Robert Griebl1fca5582002-06-04 20:45:46 +0000278{
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100279 /* Flush input and output queues, important for modems! */
280 /* TODO: sleep(1)? Users report lost chars, and I hesitate
281 * to use tcdrain here instead of tcflush */
282 tcflush(0, TCIOFLUSH);
283
284 /* Set speed if it wasn't specified as "0" on command line. */
285 if (speed != B0) {
286 cfsetispeed(tp, speed);
287 cfsetospeed(tp, speed);
288 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000289 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000290 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000291 * Special characters are set after we have read the login name; all
292 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000293 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000294 */
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000295 tp->c_cflag = CS8 | HUPCL | CREAD;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100296 if (option_mask32 & F_LOCAL)
Robert Griebl1fca5582002-06-04 20:45:46 +0000297 tp->c_cflag |= CLOCAL;
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100298 tp->c_iflag = 0;
299 tp->c_lflag = 0;
Rob Landleyf78ab532006-08-24 20:00:44 +0000300 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000301 tp->c_cc[VMIN] = 1;
302 tp->c_cc[VTIME] = 0;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200303#ifdef __linux__
304 tp->c_line = 0;
305#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000306
307 /* Optionally enable hardware flow control */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000308#ifdef CRTSCTS
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100309 if (option_mask32 & F_RTSCTS)
Robert Griebl1fca5582002-06-04 20:45:46 +0000310 tp->c_cflag |= CRTSCTS;
311#endif
312
Denis Vlasenko202ac502008-11-05 13:20:58 +0000313 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000314
Robert Griebl1fca5582002-06-04 20:45:46 +0000315 debug("term_io 2\n");
316}
317
318/* auto_baud - extract baud rate from modem status message */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000319static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000320{
321 int speed;
322 int vmin;
323 unsigned iflag;
Robert Griebl1fca5582002-06-04 20:45:46 +0000324 char *bp;
325 int nread;
326
327 /*
328 * This works only if the modem produces its status code AFTER raising
329 * the DCD line, and if the computer is fast enough to set the proper
330 * baud rate before the message has gone by. We expect a message of the
331 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000332 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000333 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000334 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000335 * The number is interpreted as the baud rate of the incoming call. If the
336 * modem does not tell us the baud rate within one second, we will keep
337 * using the current baud rate. It is advisable to enable BREAK
338 * processing (comma-separated list of baud rates) if the processing of
339 * modem status messages is enabled.
340 */
341
342 /*
343 * Use 7-bit characters, don't block if input queue is empty. Errors will
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000344 * be dealt with later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000345 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000346 iflag = tp->c_iflag;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000347 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
Robert Griebl1fca5582002-06-04 20:45:46 +0000348 vmin = tp->c_cc[VMIN];
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000349 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000350 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000351
352 /*
353 * Wait for a while, then read everything the modem has said so far and
354 * try to extract the speed of the dial-in call.
355 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000356 sleep(1);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000357 nread = safe_read(STDIN_FILENO, buf, size_buf - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000358 if (nread > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000359 buf[nread] = '\0';
360 for (bp = buf; bp < buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000361 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000362 speed = bcode(bp);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200363 if (speed > 0)
364 cfsetspeed(tp, speed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000365 break;
366 }
367 }
368 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000369
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000370 /* Restore terminal settings. Errors will be dealt with later on. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000371 tp->c_iflag = iflag;
372 tp->c_cc[VMIN] = vmin;
Denis Vlasenko202ac502008-11-05 13:20:58 +0000373 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000374}
375
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000376/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000377static void do_prompt(struct options *op)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000378{
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000379#ifdef ISSUE
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100380 if (!(option_mask32 & F_NOISSUE))
381 print_login_issue(op->issue, op->tty);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000382#endif
383 print_login_prompt();
384}
385
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000386#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000387/* all_is_upcase - string contains upper case without lower case */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000388/* returns 1 if true, 0 if false */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000389static int all_is_upcase(const char *s)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000390{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000391 while (*s)
392 if (islower(*s++))
393 return 0;
394 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000395}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000396#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000397
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000398/* get_logname - get user name, establish parity, speed, erase, kill, eol;
399 * return NULL on BREAK, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000400static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000401 struct options *op, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000402{
Robert Griebl1fca5582002-06-04 20:45:46 +0000403 char *bp;
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200404 char c; /* input character, full eight bits */
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000405 char ascval; /* low 7 bits of input character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100406#ifdef BROKEN_PARITY_DETECTION_CODE
Denis Vlasenko397de612008-03-17 08:55:44 +0000407 static const char erase[][3] = {/* backspace-space-backspace */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000408 "\010\040\010", /* space parity */
409 "\010\040\010", /* odd parity */
410 "\210\240\210", /* even parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000411 "\010\040\010", /* 8 bit no parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000412 };
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100413#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000414
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000415 /* NB: *cp is pre-initialized with init_chardata */
Robert Griebl1fca5582002-06-04 20:45:46 +0000416
417 /* Flush pending input (esp. after parsing or switching the baud rate). */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000418 sleep(1);
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200419 tcflush(0, TCIOFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000420
421 /* Prompt for and read a login name. */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000422 logname[0] = '\0';
423 while (!logname[0]) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100424 /* Write issue file and prompt. */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000425 do_prompt(op);
Robert Griebl1fca5582002-06-04 20:45:46 +0000426
427 /* Read name, watch for break, parity, erase, kill, end-of-line. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000428 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000429 cp->eol = '\0';
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100430 while (1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000431 /* Do not report trivial EINTR/EIO errors. */
Denys Vlasenko765b0ee2010-05-22 21:17:46 +0200432 errno = EINTR; /* make read of 0 bytes be silent too */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000433 if (read(STDIN_FILENO, &c, 1) < 1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000434 if (errno == EINTR || errno == EIO)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000435 exit(EXIT_SUCCESS);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200436 bb_perror_msg_and_die(bb_msg_read_error);
Robert Griebl1fca5582002-06-04 20:45:46 +0000437 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000438
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000439 /* BREAK. If we have speeds to try,
440 * return NULL (will switch speeds and return here) */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000441 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000442 return NULL;
443
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100444#ifdef BROKEN_PARITY_DETECTION_CODE
Robert Griebl1fca5582002-06-04 20:45:46 +0000445 /* Do parity bit handling. */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100446 if (!(option_mask32 & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */
447 int bits = 1;
448 int mask = 1;
Denis Vlasenko397de612008-03-17 08:55:44 +0000449 while (mask & 0x7f) {
450 if (mask & c)
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000451 bits++; /* count "1" bits */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000452 mask <<= 1;
453 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000454 /* ... |= 2 - even, 1 - odd */
455 cp->parity |= 2 - (bits & 1);
Robert Griebl1fca5582002-06-04 20:45:46 +0000456 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100457 ascval = c & 0x7f;
458#else
459 ascval = c;
460#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000461
Denis Vlasenko703aa132006-10-23 22:43:02 +0000462 /* Do erase, kill and end-of-line processing. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000463 switch (ascval) {
464 case CR:
465 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000466 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000467 cp->eol = ascval; /* set end-of-line char */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100468 goto got_logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000469 case BS:
470 case DEL:
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000471#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000472 case '#':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000473#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000474 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000475 if (bp > logname) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100476#ifdef BROKEN_PARITY_DETECTION_CODE
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000477 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100478#else
479 full_write(STDOUT_FILENO, "\010 \010", 3);
480#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000481 bp--;
482 }
483 break;
484 case CTL('U'):
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000485#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000486 case '@':
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000487 cp->kill = ascval; /* set kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100488#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000489 while (bp > logname) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100490#ifdef BROKEN_PARITY_DETECTION_CODE
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000491 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100492#else
493 full_write(STDOUT_FILENO, "\010 \010", 3);
494#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000495 bp--;
496 }
497 break;
498 case CTL('D'):
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000499 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000500 default:
Denys Vlasenko8684cbb2009-11-18 11:34:43 +0100501 if (ascval < ' ') {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000502 /* ignore garbage characters */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100503 } else if ((int)(bp - logname) < size_logname - 1) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000504 full_write(STDOUT_FILENO, &c, 1); /* echo the character */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000505 *bp++ = ascval; /* and store it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000506 }
507 break;
508 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100509 } /* end of get char loop */
510 got_logname: ;
511 } /* while logname is empty */
Robert Griebl1fca5582002-06-04 20:45:46 +0000512
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000513#ifdef HANDLE_ALLCAPS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100514 /* Handle names with upper case and no lower case. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000515 cp->capslock = all_is_upcase(logname);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000516 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000517 for (bp = logname; *bp; bp++)
518 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000519 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000520 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000521#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000522 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000523}
524
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000525/* termios_final - set the final tty mode bits */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100526static void termios_final(struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000527{
528 /* General terminal-independent stuff. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000529 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000530 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
531 /* no longer| ECHOCTL | ECHOPRT */
532 tp->c_oflag |= OPOST;
533 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000534 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
535 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
536 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000537 tp->c_cc[VEOL] = DEF_EOL;
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200538#ifdef VSWTC
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000539 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200540#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000541
542 /* Account for special characters seen in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000543 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000544 tp->c_iflag |= ICRNL; /* map CR in input to NL */
545 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000546 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000547 tp->c_cc[VERASE] = cp->erase; /* set erase character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100548#ifdef ANCIENT_BS_KILL_CHARS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000549 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100550#else
551 tp->c_cc[VKILL] = DEF_KILL; /* set kill character */
552#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000553
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100554#ifdef BROKEN_PARITY_DETECTION_CODE
Robert Griebl1fca5582002-06-04 20:45:46 +0000555 /* Account for the presence or absence of parity bits in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000556 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000557 case 0: /* space (always 0) parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000558// I bet most people go here - they use only 7-bit chars in usernames....
Robert Griebl1fca5582002-06-04 20:45:46 +0000559 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000560 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000561 tp->c_cflag |= PARODD;
562 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000563 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000564 tp->c_cflag |= PARENB;
565 tp->c_iflag |= INPCK | ISTRIP;
566 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000567 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000568 tp->c_cflag &= ~CSIZE;
569 tp->c_cflag |= CS7;
Denis Vlasenko397de612008-03-17 08:55:44 +0000570// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes -
571// it's probably some umlauts etc, but definitely NOT 7-bit!!!
572// Entire parity detection madness here just begs for deletion...
Robert Griebl1fca5582002-06-04 20:45:46 +0000573 break;
574 }
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100575#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000576
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000577#ifdef HANDLE_ALLCAPS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100578 /* Account for upper case without lower case. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000579 if (cp->capslock) {
580 tp->c_iflag |= IUCLC;
581 tp->c_lflag |= XCASE;
582 tp->c_oflag |= OLCUC;
583 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000584#endif
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100585
Denis Vlasenko202ac502008-11-05 13:20:58 +0000586#ifdef CRTSCTS
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100587 /* Optionally enable hardware flow control */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100588 if (option_mask32 & F_RTSCTS)
Robert Griebl1fca5582002-06-04 20:45:46 +0000589 tp->c_cflag |= CRTSCTS;
590#endif
591
592 /* Finally, make the new settings effective */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200593 if (tcsetattr_stdin_TCSANOW(tp) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200594 bb_perror_msg_and_die("tcsetattr");
Robert Griebl1fca5582002-06-04 20:45:46 +0000595}
596
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000597int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000598int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000599{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000600 int n;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200601 pid_t pid;
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000602 char *fakehost = NULL; /* Fake hostname for ut_host */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000603 char *logname; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000604 /* Merging these into "struct local" may _seem_ to reduce
605 * parameter passing, but today's gcc will inline
606 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000607 struct chardata chardata; /* set by get_logname() */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000608 struct termios termios; /* terminal mode bits */
609 struct options options;
610
Denis Vlasenko397de612008-03-17 08:55:44 +0000611 chardata = init_chardata;
612
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000613 memset(&options, 0, sizeof(options));
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200614 options.login = _PATH_LOGIN; /* default login program */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000615 options.tty = "tty1"; /* default tty line */
616 options.initstring = ""; /* modem init string */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000617#ifdef ISSUE
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000618 options.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000619#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000620
Denis Vlasenko397de612008-03-17 08:55:44 +0000621 /* Parse command-line arguments. */
622 parse_args(argv, &options, &fakehost);
623
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000624 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000625
626 /* Create new session, lose controlling tty, if any */
627 /* docs/ctty.htm says:
628 * "This is allowed only when the current process
629 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000630 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000631 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000632 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000633 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
634 xdup2(n, 1);
635 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000636 while (n > 2)
637 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000638
639 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000640 die_sleep = 10;
641 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000642 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000643 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000644 logmode = LOGMODE_BOTH;
645
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000646#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000647 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000648 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000649 debug(argv[n]);
650 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000651 }
652#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000653
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000654 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000655 /* If it's not "-" and not taken yet, it will become our ctty */
656 debug("calling open_tty\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000657 open_tty(options.tty);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000658 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000659 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000660 xdup2(0, 1);
661 xdup2(0, 2);
662
663 /*
664 * The following ioctl will fail if stdin is not a tty, but also when
665 * there is noise on the modem control lines. In the latter case, the
666 * common course of action is (1) fix your cables (2) give the modem more
667 * time to properly reset after hanging up. SunOS users can achieve (2)
668 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
669 * 5 seconds seems to be a good value.
670 */
Jeremie Koenigf812eac2010-05-27 15:37:32 +0200671 if (tcgetattr(0, &termios) < 0)
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200672 bb_perror_msg_and_die("tcgetattr");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000673
Denys Vlasenko3a416112010-04-05 22:10:38 +0200674 pid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000675#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000676// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000677 // /* Forcibly make fd 0 our controlling tty, even if another session
678 // * has it as a ctty. (Another session loses ctty). */
679 // ioctl(0, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000680 /* Make ourself a foreground process group within our session */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200681 tcsetpgrp(0, pid);
Denis Vlasenko397de612008-03-17 08:55:44 +0000682#endif
683
Denis Vlasenko397de612008-03-17 08:55:44 +0000684 /* Update the utmp file. This tty is ours now! */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200685 update_utmp(pid, LOGIN_PROCESS, options.tty, "LOGIN", fakehost);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000686
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000687 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
688 debug("calling termios_init\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100689 termios_init(&termios, options.speeds[0]);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000690
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000691 /* Write the modem init string and DON'T flush the buffers */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100692 if (option_mask32 & F_INITSTRING) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000693 debug("writing init string\n");
Denys Vlasenko729ecb82010-06-07 14:14:26 +0200694 full_write1_str(options.initstring);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000695 }
696
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000697 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000698 debug("before autobaud\n");
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100699 if (option_mask32 & F_PARSE)
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000700 auto_baud(line_buf, sizeof(line_buf), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000701
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000702 /* Set the optional timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000703 alarm(options.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000704
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000705 /* Optionally wait for CR or LF before writing /etc/issue */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100706 if (option_mask32 & F_WAITCRLF) {
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000707 char ch;
708
709 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000710 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000711 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000712 ch &= 0x7f; /* strip "parity bit" */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000713 if (ch == '\n' || ch == '\r')
714 break;
715 }
716 }
717
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000718 logname = NULL;
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100719 if (!(option_mask32 & F_NOPROMPT)) {
Denys Vlasenko2b57b6c2011-01-22 03:06:40 +0100720 /* NB: termios_init already set line speed
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000721 * to options.speeds[0] */
722 int baud_index = 0;
723
724 while (1) {
725 /* Read the login name. */
726 debug("reading login name\n");
727 logname = get_logname(line_buf, sizeof(line_buf),
Denis Vlasenko68404f12008-03-17 09:00:54 +0000728 &options, &chardata);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000729 if (logname)
730 break;
731 /* we are here only if options.numspeed > 1 */
732 baud_index = (baud_index + 1) % options.numspeed;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000733 cfsetispeed(&termios, options.speeds[baud_index]);
734 cfsetospeed(&termios, options.speeds[baud_index]);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000735 tcsetattr_stdin_TCSANOW(&termios);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000736 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000737 }
738
739 /* Disable timer. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000740 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000741
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000742 /* Finalize the termios settings. */
Denys Vlasenkobcdb9b82011-01-09 21:44:51 +0100743 termios_final(&termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000744
745 /* Now the newline character should be properly written. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000746 full_write(STDOUT_FILENO, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000747
748 /* Let the login program take care of password validation. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000749 /* We use PATH because we trust that root doesn't set "bad" PATH,
750 * and getty is not suid-root applet. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000751 /* With -n, logname == NULL, and login will ask for username instead */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000752 BB_EXECLP(options.login, options.login, "--", logname, NULL);
Denys Vlasenko9dc04122010-09-01 11:31:43 +0200753 bb_error_msg_and_die("can't execute '%s'", options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000754}