blob: c8c54e3b94a250a1021cfc79f30fd585bc242071 [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 *
9 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
10 * - added Native Language Support
Robert Griebl1fca5582002-06-04 20:45:46 +000011
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000012 * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
13 * - enable hardware flow control before displaying /etc/issue
14 *
15 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 *
17 */
Robert Griebl1fca5582002-06-04 20:45:46 +000018
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Denis Vlasenkof7026522006-09-19 13:50:55 +000020#include <syslog.h>
Robert Griebl1fca5582002-06-04 20:45:46 +000021
Denis Vlasenko6476cc12006-11-07 01:52:10 +000022#if ENABLE_FEATURE_UTMP
Mike Frysingerb3810092005-07-01 01:29:44 +000023#include <utmp.h>
24#endif
25
Denis Vlasenko6476cc12006-11-07 01:52:10 +000026/*
27 * Some heuristics to find out what environment we are in: if it is not
28 * System V, assume it is SunOS 4.
29 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000030#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
Denis Vlasenko6476cc12006-11-07 01:52:10 +000031#include <sys/utsname.h>
32#include <time.h>
33#if ENABLE_FEATURE_WTMP
Eric Andersenfdfe2982002-10-10 03:55:09 +000034extern void updwtmp(const char *filename, const struct utmp *ut);
Robert Griebl1fca5582002-06-04 20:45:46 +000035#endif
Denis Vlasenkof3fca912007-12-04 18:46:01 +000036#else /* if !sysV style, wtmp/utmp code is off */
37#undef ENABLE_FEATURE_UTMP
38#undef ENABLE_FEATURE_WTMP
39#define ENABLE_FEATURE_UTMP 0
40#define ENABLE_FEATURE_WTMP 0
Eric Andersenfdfe2982002-10-10 03:55:09 +000041#endif /* LOGIN_PROCESS */
Robert Griebl1fca5582002-06-04 20:45:46 +000042
Denis Vlasenko6476cc12006-11-07 01:52:10 +000043/*
44 * Things you may want to modify.
45 *
46 * You may disagree with the default line-editing etc. characters defined
47 * below. Note, however, that DEL cannot be used for interrupt generation
48 * and for line editing at the same time.
49 */
Robert Griebl1fca5582002-06-04 20:45:46 +000050
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000051/* I doubt there are systems which still need this */
52#undef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000053#undef ANCIENT_BS_KILL_CHARS
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000054
Denis Vlasenko6476cc12006-11-07 01:52:10 +000055#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000056
Denis Vlasenko6476cc12006-11-07 01:52:10 +000057/* If ISSUE is not defined, getty will never display the contents of the
58 * /etc/issue file. You will not want to spit out large "issue" files at the
59 * wrong baud rate.
60 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000061#define ISSUE "/etc/issue" /* displayed before the login prompt */
Eric Andersen77804ce2005-07-27 06:05:38 +000062
Robert Griebl1fca5582002-06-04 20:45:46 +000063/* Some shorthands for control characters. */
Denis Vlasenko397de612008-03-17 08:55:44 +000064#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000065#define CR CTL('M') /* carriage return */
66#define NL CTL('J') /* line feed */
67#define BS CTL('H') /* back space */
68#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000069
70/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000071#define DEF_ERASE DEL /* default erase character */
72#define DEF_INTR CTL('C') /* default interrupt character */
73#define DEF_QUIT CTL('\\') /* default quit char */
74#define DEF_KILL CTL('U') /* default kill char */
75#define DEF_EOF CTL('D') /* default EOF char */
76#define DEF_EOL '\n'
77#define DEF_SWITCH 0 /* default switch char */
Robert Griebl1fca5582002-06-04 20:45:46 +000078
Denis Vlasenko6476cc12006-11-07 01:52:10 +000079/*
80 * When multiple baud rates are specified on the command line, the first one
81 * we will try is the first one specified.
82 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000083#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000084
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000085/* Storage for command-line options. */
Robert Griebl1fca5582002-06-04 20:45:46 +000086struct options {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000087 int flags; /* toggle switches, see below */
Denis Vlasenko13858992006-10-08 12:49:22 +000088 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000089 const char *login; /* login program */
90 const char *tty; /* name of tty */
91 const char *initstring; /* modem init string */
92 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000093 int numspeed; /* number of baud rates to try */
94 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +000095};
96
Robert Griebl1fca5582002-06-04 20:45:46 +000097/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +000098struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000099 unsigned char erase; /* erase character */
100 unsigned char kill; /* kill character */
101 unsigned char eol; /* end-of-line character */
102 unsigned char parity; /* what parity did we see */
Denis Vlasenko397de612008-03-17 08:55:44 +0000103 /* (parity & 1): saw odd parity char with 7th bit set */
104 /* (parity & 2): saw even parity char with 7th bit set */
105 /* parity == 0: probably 7-bit, space parity? */
106 /* parity == 1: probably 7-bit, odd parity? */
107 /* parity == 2: probably 7-bit, even parity? */
108 /* parity == 3: definitely 8 bit, no parity! */
109 /* Hmm... with any value of "parity" 8 bit, no parity is possible */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000110#ifdef HANDLE_ALLCAPS
111 unsigned char capslock; /* upper case without lower case */
112#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000113};
114
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000115
Robert Griebl1fca5582002-06-04 20:45:46 +0000116/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000117static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000118 DEF_ERASE, /* default erase character */
119 DEF_KILL, /* default kill character */
120 13, /* default eol char */
121 0, /* space parity */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000122#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000123 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000124#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000125};
126
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000127static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
128#define F_INITSTRING (1 << 0) /* -I initstring is set */
129#define F_LOCAL (1 << 1) /* -L force local */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000130#define F_FAKEHOST (1 << 2) /* -H fake hostname */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000131#define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */
132#define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */
133#define F_ISSUE (1 << 5) /* -i display /etc/issue */
134#define F_LOGIN (1 << 6) /* -l non-default login program */
135#define F_PARSE (1 << 7) /* -m process modem status messages */
136#define F_TIMEOUT (1 << 8) /* -t time out */
137#define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000138#define F_NOPROMPT (1 << 10) /* -n don't ask for login name */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000139
Robert Griebl1fca5582002-06-04 20:45:46 +0000140
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000141#define line_buf bb_common_bufsiz1
Robert Griebl1fca5582002-06-04 20:45:46 +0000142
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000143/* The following is used for understandable diagnostics. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000144#ifdef DEBUGGING
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000145static FILE *dbf;
Denis Vlasenko397de612008-03-17 08:55:44 +0000146#define DEBUGTERM "/dev/ttyp0"
147#define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000148#else
Denis Vlasenko397de612008-03-17 08:55:44 +0000149#define debug(...) ((void)0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000150#endif
151
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000152
Denis Vlasenko397de612008-03-17 08:55:44 +0000153/* bcode - convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000154static int bcode(const char *s)
155{
Denis Vlasenko397de612008-03-17 08:55:44 +0000156 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
157 if (value < 0) /* bad terminating char, overflow, etc */
158 return value;
159 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000160}
161
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000162/* parse_speeds - parse alternate baud rates */
163static void parse_speeds(struct options *op, char *arg)
164{
165 char *cp;
166
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000167 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000168 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000169 while ((cp = strsep(&arg, ",")) != NULL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000170 op->speeds[op->numspeed] = bcode(cp);
171 if (op->speeds[op->numspeed] <= 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000172 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000173 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000174 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000175 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000176 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000177 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000178}
179
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000180/* parse_args - parse command-line arguments */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000181static void parse_args(char **argv, struct options *op, char **fakehost_p)
Robert Griebl1fca5582002-06-04 20:45:46 +0000182{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000183 char *ts;
Robert Griebl1fca5582002-06-04 20:45:46 +0000184
Denis Vlasenko397de612008-03-17 08:55:44 +0000185 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000186 op->flags = getopt32(argv, opt_string,
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000187 &(op->initstring), fakehost_p, &(op->issue),
Denis Vlasenko397de612008-03-17 08:55:44 +0000188 &(op->login), &op->timeout);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000189 argv += optind;
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000190 if (op->flags & F_INITSTRING) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000191 const char *p = op->initstring;
192 char *q;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000193
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000194 op->initstring = q = xstrdup(p);
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000195 /* copy optarg into op->initstring decoding \ddd
196 octal codes into chars */
197 while (*p) {
198 if (*p == '\\') {
199 p++;
200 *q++ = bb_process_escape_sequence(&p);
201 } else {
202 *q++ = *p++;
Robert Griebl1fca5582002-06-04 20:45:46 +0000203 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000204 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000205 *q = '\0';
206 }
Denis Vlasenko397de612008-03-17 08:55:44 +0000207 op->flags ^= F_ISSUE; /* invert flag "show /etc/issue" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000208 debug("after getopt\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000209
210 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000211 op->tty = argv[0]; /* tty name */
212 ts = argv[1]; /* baud rate(s) */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000213 if (isdigit(argv[0][0])) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000214 /* a number first, assume it's a speed (BSD style) */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000215 op->tty = ts; /* tty name is in argv[1] */
216 ts = argv[0]; /* baud rate(s) */
Robert Griebl1fca5582002-06-04 20:45:46 +0000217 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000218 parse_speeds(op, ts);
Robert Griebl1fca5582002-06-04 20:45:46 +0000219
Denis Vlasenko397de612008-03-17 08:55:44 +0000220// TODO: if applet_name is set to "getty: TTY", bb_error_msg's get simpler!
221// grep for "%s:"
222
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000223 if (argv[2])
Denis Vlasenko397de612008-03-17 08:55:44 +0000224 xsetenv("TERM", argv[2]);
Robert Griebl1fca5582002-06-04 20:45:46 +0000225
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000226 debug("exiting parse_args\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000227}
228
Robert Griebl1fca5582002-06-04 20:45:46 +0000229/* open_tty - set up tty as standard { input, output, error } */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000230static void open_tty(const char *tty)
Robert Griebl1fca5582002-06-04 20:45:46 +0000231{
Robert Griebl1fca5582002-06-04 20:45:46 +0000232 /* Set up new standard input, unless we are given an already opened port. */
Denis Vlasenko9f739442006-12-16 23:49:13 +0000233 if (NOT_LONE_DASH(tty)) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000234// struct stat st;
235// int cur_dir_fd;
236// int fd;
Robert Griebl1fca5582002-06-04 20:45:46 +0000237
238 /* Sanity checks... */
Denis Vlasenko397de612008-03-17 08:55:44 +0000239// cur_dir_fd = xopen(".", O_DIRECTORY | O_NONBLOCK);
240// xchdir("/dev");
241// xstat(tty, &st);
242// if ((st.st_mode & S_IFMT) != S_IFCHR)
243// bb_error_msg_and_die("%s: not a character device", tty);
244
245 if (tty[0] != '/')
246 tty = xasprintf("/dev/%s", tty); /* will leak it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000247
248 /* Open the tty as standard input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000249 debug("open(2)\n");
Denis Vlasenko397de612008-03-17 08:55:44 +0000250 close(0);
251 /*fd =*/ xopen(tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000252
Denis Vlasenko397de612008-03-17 08:55:44 +0000253// /* Restore current directory */
254// fchdir(cur_dir_fd);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000255
256 /* Open the tty as standard input, continued */
Denis Vlasenko397de612008-03-17 08:55:44 +0000257// xmove_fd(fd, 0);
258// /* fd is >= cur_dir_fd, and cur_dir_fd gets closed too here: */
259// while (fd > 2)
260// close(fd--);
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 */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000270 if ((fcntl(0, F_GETFL) & O_RDWR) != 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
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000275/* termios_init - initialize termios settings */
276static void termios_init(struct termios *tp, int speed, struct options *op)
Robert Griebl1fca5582002-06-04 20:45:46 +0000277{
Robert Griebl1fca5582002-06-04 20:45:46 +0000278 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000279 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000280 * Special characters are set after we have read the login name; all
281 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000282 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000283 */
284#ifdef __linux__
285 /* flush input and output queues, important for modems! */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000286 ioctl(0, TCFLSH, TCIOFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000287#endif
288
289 tp->c_cflag = CS8 | HUPCL | CREAD | speed;
Denis Vlasenko397de612008-03-17 08:55:44 +0000290 if (op->flags & F_LOCAL)
Robert Griebl1fca5582002-06-04 20:45:46 +0000291 tp->c_cflag |= CLOCAL;
Robert Griebl1fca5582002-06-04 20:45:46 +0000292
Rob Landleyf78ab532006-08-24 20:00:44 +0000293 tp->c_iflag = tp->c_lflag = tp->c_line = 0;
294 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000295 tp->c_cc[VMIN] = 1;
296 tp->c_cc[VTIME] = 0;
297
298 /* Optionally enable hardware flow control */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000299#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000300 if (op->flags & F_RTSCTS)
301 tp->c_cflag |= CRTSCTS;
302#endif
303
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000304 ioctl(0, TCSETS, tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000305
Robert Griebl1fca5582002-06-04 20:45:46 +0000306 debug("term_io 2\n");
307}
308
309/* auto_baud - extract baud rate from modem status message */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000310static void auto_baud(char *buf, unsigned size_buf, struct termios *tp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000311{
312 int speed;
313 int vmin;
314 unsigned iflag;
Robert Griebl1fca5582002-06-04 20:45:46 +0000315 char *bp;
316 int nread;
317
318 /*
319 * This works only if the modem produces its status code AFTER raising
320 * the DCD line, and if the computer is fast enough to set the proper
321 * baud rate before the message has gone by. We expect a message of the
322 * following format:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000323 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000324 * <junk><number><junk>
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000325 *
Robert Griebl1fca5582002-06-04 20:45:46 +0000326 * The number is interpreted as the baud rate of the incoming call. If the
327 * modem does not tell us the baud rate within one second, we will keep
328 * using the current baud rate. It is advisable to enable BREAK
329 * processing (comma-separated list of baud rates) if the processing of
330 * modem status messages is enabled.
331 */
332
333 /*
334 * Use 7-bit characters, don't block if input queue is empty. Errors will
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000335 * be dealt with later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000336 */
Robert Griebl1fca5582002-06-04 20:45:46 +0000337 iflag = tp->c_iflag;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000338 tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */
Robert Griebl1fca5582002-06-04 20:45:46 +0000339 vmin = tp->c_cc[VMIN];
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000340 tp->c_cc[VMIN] = 0; /* don't block if queue empty */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000341 ioctl(0, TCSETS, tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000342
343 /*
344 * Wait for a while, then read everything the modem has said so far and
345 * try to extract the speed of the dial-in call.
346 */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000347 sleep(1);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000348 nread = safe_read(0, buf, size_buf - 1);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000349 if (nread > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000350 buf[nread] = '\0';
351 for (bp = buf; bp < buf + nread; bp++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000352 if (isdigit(*bp)) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000353 speed = bcode(bp);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000354 if (speed > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000355 tp->c_cflag &= ~CBAUD;
356 tp->c_cflag |= speed;
357 }
358 break;
359 }
360 }
361 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000362
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000363 /* Restore terminal settings. Errors will be dealt with later on. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000364 tp->c_iflag = iflag;
365 tp->c_cc[VMIN] = vmin;
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000366 ioctl(0, TCSETS, tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000367}
368
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000369/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000370static void do_prompt(struct options *op)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000371{
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000372#ifdef ISSUE
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000373 print_login_issue(op->issue, op->tty);
374#endif
375 print_login_prompt();
376}
377
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000378#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000379/* all_is_upcase - string contains upper case without lower case */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000380/* returns 1 if true, 0 if false */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000381static int all_is_upcase(const char *s)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000382{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000383 while (*s)
384 if (islower(*s++))
385 return 0;
386 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000387}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000388#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000389
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000390/* get_logname - get user name, establish parity, speed, erase, kill, eol;
391 * return NULL on BREAK, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000392static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000393 struct options *op, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000394{
Robert Griebl1fca5582002-06-04 20:45:46 +0000395 char *bp;
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000396 char c; /* input character, full eight bits */
397 char ascval; /* low 7 bits of input character */
398 int bits; /* # of "1" bits per character */
399 int mask; /* mask with 1 bit up */
Denis Vlasenko397de612008-03-17 08:55:44 +0000400 static const char erase[][3] = {/* backspace-space-backspace */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000401 "\010\040\010", /* space parity */
402 "\010\040\010", /* odd parity */
403 "\210\240\210", /* even parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000404 "\010\040\010", /* 8 bit no parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000405 };
406
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000407 /* NB: *cp is pre-initialized with init_chardata */
Robert Griebl1fca5582002-06-04 20:45:46 +0000408
409 /* Flush pending input (esp. after parsing or switching the baud rate). */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000410 sleep(1);
411 ioctl(0, TCFLSH, TCIFLUSH);
Robert Griebl1fca5582002-06-04 20:45:46 +0000412
413 /* Prompt for and read a login name. */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000414 logname[0] = '\0';
415 while (!logname[0]) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000416 /* Write issue file and prompt, with "parity" bit == 0. */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000417 do_prompt(op);
Robert Griebl1fca5582002-06-04 20:45:46 +0000418
419 /* Read name, watch for break, parity, erase, kill, end-of-line. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000420 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000421 cp->eol = '\0';
422 while (cp->eol == '\0') {
Robert Griebl1fca5582002-06-04 20:45:46 +0000423
424 /* Do not report trivial EINTR/EIO errors. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000425 if (read(0, &c, 1) < 1) {
426 if (errno == EINTR || errno == EIO)
427 exit(0);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000428 bb_perror_msg_and_die("%s: read", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000429 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000430
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000431 /* BREAK. If we have speeds to try,
432 * return NULL (will switch speeds and return here) */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000433 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000434 return NULL;
435
436 /* Do parity bit handling. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000437 if (!(op->flags & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000438 bits = 1;
439 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 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000448
Denis Vlasenko703aa132006-10-23 22:43:02 +0000449 /* Do erase, kill and end-of-line processing. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000450 ascval = c & 0x7f;
Robert Griebl1fca5582002-06-04 20:45:46 +0000451 switch (ascval) {
452 case CR:
453 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000454 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000455 cp->eol = ascval; /* set end-of-line char */
Robert Griebl1fca5582002-06-04 20:45:46 +0000456 break;
457 case BS:
458 case DEL:
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000459#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000460 case '#':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000461#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000462 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000463 if (bp > logname) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000464 full_write(1, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000465 bp--;
466 }
467 break;
468 case CTL('U'):
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000469#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000470 case '@':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000471#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000472 cp->kill = ascval; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000473 while (bp > logname) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000474 full_write(1, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000475 bp--;
476 }
477 break;
478 case CTL('D'):
479 exit(0);
480 default:
481 if (!isascii(ascval) || !isprint(ascval)) {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000482 /* ignore garbage characters */
483 } else if (bp - logname >= size_logname - 1) {
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000484 bb_error_msg_and_die("%s: input overrun", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000485 } else {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000486 full_write(1, &c, 1); /* echo the character */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000487 *bp++ = ascval; /* and store it */
Robert Griebl1fca5582002-06-04 20:45:46 +0000488 }
489 break;
490 }
491 }
492 }
493 /* Handle names with upper case and no lower case. */
494
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000495#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000496 cp->capslock = all_is_upcase(logname);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000497 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000498 for (bp = logname; *bp; bp++)
499 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000500 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000501 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000502#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000503 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000504}
505
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000506/* termios_final - set the final tty mode bits */
507static void termios_final(struct options *op, struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000508{
509 /* General terminal-independent stuff. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000510 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000511 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
512 /* no longer| ECHOCTL | ECHOPRT */
513 tp->c_oflag |= OPOST;
514 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000515 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
516 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
517 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000518 tp->c_cc[VEOL] = DEF_EOL;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000519 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000520
521 /* Account for special characters seen in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000522 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000523 tp->c_iflag |= ICRNL; /* map CR in input to NL */
524 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000525 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000526 tp->c_cc[VERASE] = cp->erase; /* set erase character */
527 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000528
529 /* Account for the presence or absence of parity bits in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000530 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000531 case 0: /* space (always 0) parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000532// I bet most people go here - they use only 7-bit chars in usernames....
Robert Griebl1fca5582002-06-04 20:45:46 +0000533 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000534 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000535 tp->c_cflag |= PARODD;
536 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000537 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000538 tp->c_cflag |= PARENB;
539 tp->c_iflag |= INPCK | ISTRIP;
540 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000541 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000542 tp->c_cflag &= ~CSIZE;
543 tp->c_cflag |= CS7;
Denis Vlasenko397de612008-03-17 08:55:44 +0000544// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes -
545// it's probably some umlauts etc, but definitely NOT 7-bit!!!
546// Entire parity detection madness here just begs for deletion...
Robert Griebl1fca5582002-06-04 20:45:46 +0000547 break;
548 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000549
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000550 /* Account for upper case without lower case. */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000551#ifdef HANDLE_ALLCAPS
Robert Griebl1fca5582002-06-04 20:45:46 +0000552 if (cp->capslock) {
553 tp->c_iflag |= IUCLC;
554 tp->c_lflag |= XCASE;
555 tp->c_oflag |= OLCUC;
556 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000557#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000558 /* Optionally enable hardware flow control */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000559#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000560 if (op->flags & F_RTSCTS)
561 tp->c_cflag |= CRTSCTS;
562#endif
563
564 /* Finally, make the new settings effective */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000565 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000566}
567
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000568#if ENABLE_FEATURE_UTMP
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000569static void touch(const char *filename)
570{
571 if (access(filename, R_OK | W_OK) == -1)
572 close(open(filename, O_WRONLY | O_CREAT, 0664));
573}
574
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000575/* update_utmp - update our utmp entry */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000576static void update_utmp(const char *line, char *fakehost)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000577{
578 struct utmp ut;
579 struct utmp *utp;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000580 int mypid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000581
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000582 /* In case we won't find an entry below... */
583 memset(&ut, 0, sizeof(ut));
584 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
585
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000586 /*
587 * The utmp file holds miscellaneous information about things started by
588 * /sbin/init and other system-related events. Our purpose is to update
589 * the utmp entry for the current process, in particular the process type
590 * and the tty line we are listening to. Return successfully only if the
591 * utmp file can be opened for update, and if we are able to find our
592 * entry in the utmp file.
593 */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000594 touch(_PATH_UTMP);
595
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000596 utmpname(_PATH_UTMP);
597 setutent();
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000598 while ((utp = getutent()) != NULL) {
599 if (utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) {
600 memcpy(&ut, utp, sizeof(ut));
601 break;
602 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000603 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000604
Rob Landleycf7577d2006-06-25 22:59:31 +0000605 strcpy(ut.ut_user, "LOGIN");
606 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000607 if (fakehost)
Rob Landleycf7577d2006-06-25 22:59:31 +0000608 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000609 ut.ut_time = time(NULL);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000610 ut.ut_type = LOGIN_PROCESS;
611 ut.ut_pid = mypid;
612
613 pututline(&ut);
614 endutent();
615
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000616#if ENABLE_FEATURE_WTMP
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000617 touch(bb_path_wtmp_file);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000618 updwtmp(bb_path_wtmp_file, &ut);
619#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000620}
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000621#endif /* CONFIG_FEATURE_UTMP */
Robert Griebl1fca5582002-06-04 20:45:46 +0000622
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000623int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +0000624int getty_main(int argc ATTRIBUTE_UNUSED, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000625{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000626 int n;
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000627 char *fakehost = NULL; /* Fake hostname for ut_host */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000628 char *logname; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000629 /* Merging these into "struct local" may _seem_ to reduce
630 * parameter passing, but today's gcc will inline
631 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000632 struct chardata chardata; /* set by get_logname() */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000633 struct termios termios; /* terminal mode bits */
634 struct options options;
635
Denis Vlasenko397de612008-03-17 08:55:44 +0000636 chardata = init_chardata;
637
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000638 memset(&options, 0, sizeof(options));
639 options.login = _PATH_LOGIN; /* default login program */
640 options.tty = "tty1"; /* default tty line */
641 options.initstring = ""; /* modem init string */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000642#ifdef ISSUE
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000643 options.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000644#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000645
Denis Vlasenko397de612008-03-17 08:55:44 +0000646 /* Parse command-line arguments. */
647 parse_args(argv, &options, &fakehost);
648
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000649 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000650
651 /* Create new session, lose controlling tty, if any */
652 /* docs/ctty.htm says:
653 * "This is allowed only when the current process
654 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000655 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000656 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000657 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000658 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
659 xdup2(n, 1);
660 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000661 while (n > 2)
662 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000663
664 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000665 die_sleep = 10;
666 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000667 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000668 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000669 logmode = LOGMODE_BOTH;
670
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000671#ifdef DEBUGGING
Rob Landleyd921b2e2006-08-03 15:41:12 +0000672 dbf = xfopen(DEBUGTERM, "w");
Denis Vlasenko68404f12008-03-17 09:00:54 +0000673 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000674 debug(argv[n]);
675 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000676 }
677#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000678
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000679 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000680 /* If it's not "-" and not taken yet, it will become our ctty */
681 debug("calling open_tty\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000682 open_tty(options.tty);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000683 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000684 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000685 xdup2(0, 1);
686 xdup2(0, 2);
687
688 /*
689 * The following ioctl will fail if stdin is not a tty, but also when
690 * there is noise on the modem control lines. In the latter case, the
691 * common course of action is (1) fix your cables (2) give the modem more
692 * time to properly reset after hanging up. SunOS users can achieve (2)
693 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
694 * 5 seconds seems to be a good value.
695 */
696 ioctl_or_perror_and_die(0, TCGETS, &termios, "%s: TCGETS", options.tty);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000697
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000698#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000699// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000700 // /* Forcibly make fd 0 our controlling tty, even if another session
701 // * has it as a ctty. (Another session loses ctty). */
702 // ioctl(0, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000703 /* Make ourself a foreground process group within our session */
704 tcsetpgrp(0, getpid());
705#endif
706
707#if ENABLE_FEATURE_UTMP
708 /* Update the utmp file. This tty is ours now! */
709 update_utmp(options.tty, fakehost);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000710#endif
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000711
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000712 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
713 debug("calling termios_init\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000714 termios_init(&termios, options.speeds[0], &options);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000715
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000716 /* Write the modem init string and DON'T flush the buffers */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000717 if (options.flags & F_INITSTRING) {
718 debug("writing init string\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000719 full_write(1, options.initstring, strlen(options.initstring));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000720 }
721
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000722 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000723 debug("before autobaud\n");
724 if (options.flags & F_PARSE)
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000725 auto_baud(line_buf, sizeof(line_buf), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000726
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000727 /* Set the optional timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000728 alarm(options.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000729
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000730 /* Optionally wait for CR or LF before writing /etc/issue */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000731 if (options.flags & F_WAITCRLF) {
732 char ch;
733
734 debug("waiting for cr-lf\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000735 while (safe_read(0, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000736 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000737 ch &= 0x7f; /* strip "parity bit" */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000738 if (ch == '\n' || ch == '\r')
739 break;
740 }
741 }
742
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000743 logname = NULL;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000744 if (!(options.flags & F_NOPROMPT)) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000745 /* NB:termios_init already set line speed
746 * to options.speeds[0] */
747 int baud_index = 0;
748
749 while (1) {
750 /* Read the login name. */
751 debug("reading login name\n");
752 logname = get_logname(line_buf, sizeof(line_buf),
Denis Vlasenko68404f12008-03-17 09:00:54 +0000753 &options, &chardata);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000754 if (logname)
755 break;
756 /* we are here only if options.numspeed > 1 */
757 baud_index = (baud_index + 1) % options.numspeed;
758 termios.c_cflag &= ~CBAUD;
759 termios.c_cflag |= options.speeds[baud_index];
760 ioctl(0, TCSETS, &termios);
761 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000762 }
763
764 /* Disable timer. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000765 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000766
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000767 /* Finalize the termios settings. */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000768 termios_final(&options, &termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000769
770 /* Now the newline character should be properly written. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000771 full_write(1, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000772
773 /* Let the login program take care of password validation. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000774 /* We use PATH because we trust that root doesn't set "bad" PATH,
775 * and getty is not suid-root applet. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000776 /* With -n, logname == NULL, and login will ask for username instead */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000777 BB_EXECLP(options.login, options.login, "--", logname, NULL);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000778 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000779}