blob: 838adf2e76b116fe702ec12326a1c08256ae415d [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 *
15 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
"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
Denis Vlasenko9725daa2008-09-11 09:54:23 +000022#include <utmp.h> /* updwtmp() */
Mike Frysingerb3810092005-07-01 01:29:44 +000023#endif
24
Denys Vlasenko9b1b62a2009-07-05 03:34:12 +020025#ifndef IUCLC
26# define IUCLC 0
27#endif
28
Denis Vlasenko6476cc12006-11-07 01:52:10 +000029/*
30 * Some heuristics to find out what environment we are in: if it is not
31 * System V, assume it is SunOS 4.
32 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000033#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
Denis Vlasenko6476cc12006-11-07 01:52:10 +000034#include <sys/utsname.h>
Denis Vlasenkof3fca912007-12-04 18:46:01 +000035#else /* if !sysV style, wtmp/utmp code is off */
36#undef ENABLE_FEATURE_UTMP
37#undef ENABLE_FEATURE_WTMP
38#define ENABLE_FEATURE_UTMP 0
39#define ENABLE_FEATURE_WTMP 0
Eric Andersenfdfe2982002-10-10 03:55:09 +000040#endif /* LOGIN_PROCESS */
Robert Griebl1fca5582002-06-04 20:45:46 +000041
Denis Vlasenko6476cc12006-11-07 01:52:10 +000042/*
43 * Things you may want to modify.
44 *
45 * You may disagree with the default line-editing etc. characters defined
46 * below. Note, however, that DEL cannot be used for interrupt generation
47 * and for line editing at the same time.
48 */
Robert Griebl1fca5582002-06-04 20:45:46 +000049
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000050/* I doubt there are systems which still need this */
51#undef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000052#undef ANCIENT_BS_KILL_CHARS
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000053
Denis Vlasenko6476cc12006-11-07 01:52:10 +000054#define _PATH_LOGIN "/bin/login"
Robert Griebl1fca5582002-06-04 20:45:46 +000055
Denis Vlasenko6476cc12006-11-07 01:52:10 +000056/* If ISSUE is not defined, getty will never display the contents of the
57 * /etc/issue file. You will not want to spit out large "issue" files at the
58 * wrong baud rate.
59 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000060#define ISSUE "/etc/issue" /* displayed before the login prompt */
Eric Andersen77804ce2005-07-27 06:05:38 +000061
Robert Griebl1fca5582002-06-04 20:45:46 +000062/* Some shorthands for control characters. */
Denis Vlasenko397de612008-03-17 08:55:44 +000063#define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000064#define CR CTL('M') /* carriage return */
65#define NL CTL('J') /* line feed */
66#define BS CTL('H') /* back space */
67#define DEL CTL('?') /* delete */
Robert Griebl1fca5582002-06-04 20:45:46 +000068
69/* Defaults for line-editing etc. characters; you may want to change this. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000070#define DEF_ERASE DEL /* default erase character */
71#define DEF_INTR CTL('C') /* default interrupt character */
72#define DEF_QUIT CTL('\\') /* default quit char */
73#define DEF_KILL CTL('U') /* default kill char */
74#define DEF_EOF CTL('D') /* default EOF char */
75#define DEF_EOL '\n'
76#define DEF_SWITCH 0 /* default switch char */
Robert Griebl1fca5582002-06-04 20:45:46 +000077
Denis Vlasenko6476cc12006-11-07 01:52:10 +000078/*
79 * When multiple baud rates are specified on the command line, the first one
80 * we will try is the first one specified.
81 */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000082#define MAX_SPEED 10 /* max. nr. of baud rates */
Robert Griebl1fca5582002-06-04 20:45:46 +000083
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000084/* Storage for command-line options. */
Robert Griebl1fca5582002-06-04 20:45:46 +000085struct options {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000086 int flags; /* toggle switches, see below */
Denis Vlasenko13858992006-10-08 12:49:22 +000087 unsigned timeout; /* time-out period */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +000088 const char *login; /* login program */
89 const char *tty; /* name of tty */
90 const char *initstring; /* modem init string */
91 const char *issue; /* alternative issue file */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +000092 int numspeed; /* number of baud rates to try */
93 int speeds[MAX_SPEED]; /* baud rates to be tried */
Robert Griebl1fca5582002-06-04 20:45:46 +000094};
95
Robert Griebl1fca5582002-06-04 20:45:46 +000096/* Storage for things detected while the login name was read. */
Mike Frysingerb3810092005-07-01 01:29:44 +000097struct chardata {
Denis Vlasenko4e70bf42006-10-23 10:17:34 +000098 unsigned char erase; /* erase character */
99 unsigned char kill; /* kill character */
100 unsigned char eol; /* end-of-line character */
101 unsigned char parity; /* what parity did we see */
Denis Vlasenko397de612008-03-17 08:55:44 +0000102 /* (parity & 1): saw odd parity char with 7th bit set */
103 /* (parity & 2): saw even parity char with 7th bit set */
104 /* parity == 0: probably 7-bit, space parity? */
105 /* parity == 1: probably 7-bit, odd parity? */
106 /* parity == 2: probably 7-bit, even parity? */
107 /* parity == 3: definitely 8 bit, no parity! */
108 /* Hmm... with any value of "parity" 8 bit, no parity is possible */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000109#ifdef HANDLE_ALLCAPS
110 unsigned char capslock; /* upper case without lower case */
111#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000112};
113
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000114
Robert Griebl1fca5582002-06-04 20:45:46 +0000115/* Initial values for the above. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000116static const struct chardata init_chardata = {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000117 DEF_ERASE, /* default erase character */
118 DEF_KILL, /* default kill character */
119 13, /* default eol char */
120 0, /* space parity */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000121#ifdef HANDLE_ALLCAPS
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000122 0, /* no capslock */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000123#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000124};
125
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000126static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
127#define F_INITSTRING (1 << 0) /* -I initstring is set */
128#define F_LOCAL (1 << 1) /* -L force local */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000129#define F_FAKEHOST (1 << 2) /* -H fake hostname */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000130#define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */
131#define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */
132#define F_ISSUE (1 << 5) /* -i display /etc/issue */
133#define F_LOGIN (1 << 6) /* -l non-default login program */
134#define F_PARSE (1 << 7) /* -m process modem status messages */
135#define F_TIMEOUT (1 << 8) /* -t time out */
136#define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000137#define F_NOPROMPT (1 << 10) /* -n don't ask for login name */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000138
Robert Griebl1fca5582002-06-04 20:45:46 +0000139
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000140#define line_buf bb_common_bufsiz1
Robert Griebl1fca5582002-06-04 20:45:46 +0000141
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000142/* The following is used for understandable diagnostics. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000143#ifdef DEBUGGING
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000144static FILE *dbf;
Denis Vlasenko397de612008-03-17 08:55:44 +0000145#define DEBUGTERM "/dev/ttyp0"
146#define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000147#else
Denis Vlasenko397de612008-03-17 08:55:44 +0000148#define debug(...) ((void)0)
Robert Griebl1fca5582002-06-04 20:45:46 +0000149#endif
150
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000151
Denis Vlasenko397de612008-03-17 08:55:44 +0000152/* bcode - convert speed string to speed code; return <= 0 on failure */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000153static int bcode(const char *s)
154{
Denis Vlasenko397de612008-03-17 08:55:44 +0000155 int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */
156 if (value < 0) /* bad terminating char, overflow, etc */
157 return value;
158 return tty_value_to_baud(value);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000159}
160
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000161/* parse_speeds - parse alternate baud rates */
162static void parse_speeds(struct options *op, char *arg)
163{
164 char *cp;
165
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000166 /* NB: at least one iteration is always done */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000167 debug("entered parse_speeds\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000168 while ((cp = strsep(&arg, ",")) != NULL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000169 op->speeds[op->numspeed] = bcode(cp);
Mike Frysingerfe013a72009-04-09 07:08:04 +0000170 if (op->speeds[op->numspeed] < 0)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000171 bb_error_msg_and_die("bad speed: %s", cp);
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000172 /* note: arg "0" turns into speed B0 */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000173 op->numspeed++;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000174 if (op->numspeed > MAX_SPEED)
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000175 bb_error_msg_and_die("too many alternate speeds");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000176 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000177 debug("exiting parse_speeds\n");
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000178}
179
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000180/* parse_args - parse command-line arguments */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000181static void parse_args(char **argv, struct options *op, char **fakehost_p)
Robert Griebl1fca5582002-06-04 20:45:46 +0000182{
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000183 char *ts;
Robert Griebl1fca5582002-06-04 20:45:46 +0000184
Denis Vlasenko397de612008-03-17 08:55:44 +0000185 opt_complementary = "-2:t+"; /* at least 2 args; -t N */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000186 op->flags = getopt32(argv, opt_string,
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000187 &(op->initstring), fakehost_p, &(op->issue),
Denis Vlasenko397de612008-03-17 08:55:44 +0000188 &(op->login), &op->timeout);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000189 argv += optind;
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000190 if (op->flags & F_INITSTRING) {
"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);
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200242// if (!S_ISCHR(st.st_mode))
Denis Vlasenko397de612008-03-17 08:55:44 +0000243// 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{
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000278 speed_t ispeed, ospeed;
Robert Griebl1fca5582002-06-04 20:45:46 +0000279 /*
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000280 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
Robert Griebl1fca5582002-06-04 20:45:46 +0000281 * Special characters are set after we have read the login name; all
282 * reads will be done in raw mode anyway. Errors will be dealt with
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000283 * later on.
Robert Griebl1fca5582002-06-04 20:45:46 +0000284 */
285#ifdef __linux__
286 /* flush input and output queues, important for modems! */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000287 ioctl(0, TCFLSH, TCIOFLUSH); /* tcflush(0, TCIOFLUSH)? - same */
Robert Griebl1fca5582002-06-04 20:45:46 +0000288#endif
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000289 ispeed = ospeed = speed;
290 if (speed == B0) {
291 /* Speed was specified as "0" on command line.
292 * Just leave it unchanged */
293 ispeed = cfgetispeed(tp);
294 ospeed = cfgetospeed(tp);
295 }
296 tp->c_cflag = CS8 | HUPCL | CREAD;
Denis Vlasenko397de612008-03-17 08:55:44 +0000297 if (op->flags & F_LOCAL)
Robert Griebl1fca5582002-06-04 20:45:46 +0000298 tp->c_cflag |= CLOCAL;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000299 cfsetispeed(tp, ispeed);
300 cfsetospeed(tp, ospeed);
Robert Griebl1fca5582002-06-04 20:45:46 +0000301
Rob Landleyf78ab532006-08-24 20:00:44 +0000302 tp->c_iflag = tp->c_lflag = tp->c_line = 0;
303 tp->c_oflag = OPOST | ONLCR;
Robert Griebl1fca5582002-06-04 20:45:46 +0000304 tp->c_cc[VMIN] = 1;
305 tp->c_cc[VTIME] = 0;
306
307 /* Optionally enable hardware flow control */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000308#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000309 if (op->flags & F_RTSCTS)
310 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);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000363 if (speed > 0) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000364 tp->c_cflag &= ~CBAUD;
365 tp->c_cflag |= speed;
366 }
367 break;
368 }
369 }
370 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000371
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000372 /* Restore terminal settings. Errors will be dealt with later on. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000373 tp->c_iflag = iflag;
374 tp->c_cc[VMIN] = vmin;
Denis Vlasenko202ac502008-11-05 13:20:58 +0000375 tcsetattr_stdin_TCSANOW(tp);
Robert Griebl1fca5582002-06-04 20:45:46 +0000376}
377
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000378/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000379static void do_prompt(struct options *op)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000380{
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000381#ifdef ISSUE
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000382 print_login_issue(op->issue, op->tty);
383#endif
384 print_login_prompt();
385}
386
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000387#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000388/* all_is_upcase - string contains upper case without lower case */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000389/* returns 1 if true, 0 if false */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000390static int all_is_upcase(const char *s)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000391{
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000392 while (*s)
393 if (islower(*s++))
394 return 0;
395 return 1;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000396}
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000397#endif
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000398
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000399/* get_logname - get user name, establish parity, speed, erase, kill, eol;
400 * return NULL on BREAK, logname on success */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000401static char *get_logname(char *logname, unsigned size_logname,
Denis Vlasenko68404f12008-03-17 09:00:54 +0000402 struct options *op, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000403{
Robert Griebl1fca5582002-06-04 20:45:46 +0000404 char *bp;
"Vladimir N. Oleynik"6f347ef2005-10-15 10:23:55 +0000405 char c; /* input character, full eight bits */
406 char ascval; /* low 7 bits of input character */
407 int bits; /* # of "1" bits per character */
408 int mask; /* mask with 1 bit up */
Denis Vlasenko397de612008-03-17 08:55:44 +0000409 static const char erase[][3] = {/* backspace-space-backspace */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000410 "\010\040\010", /* space parity */
411 "\010\040\010", /* odd parity */
412 "\210\240\210", /* even parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000413 "\010\040\010", /* 8 bit no parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000414 };
415
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000416 /* NB: *cp is pre-initialized with init_chardata */
Robert Griebl1fca5582002-06-04 20:45:46 +0000417
418 /* Flush pending input (esp. after parsing or switching the baud rate). */
Denis Vlasenko50e77e12006-10-23 02:11:22 +0000419 sleep(1);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000420 ioctl(0, TCFLSH, TCIFLUSH); /* tcflush(0, TCIOFLUSH)? - same */
Robert Griebl1fca5582002-06-04 20:45:46 +0000421
422 /* Prompt for and read a login name. */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000423 logname[0] = '\0';
424 while (!logname[0]) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000425 /* Write issue file and prompt, with "parity" bit == 0. */
Denis Vlasenko68404f12008-03-17 09:00:54 +0000426 do_prompt(op);
Robert Griebl1fca5582002-06-04 20:45:46 +0000427
428 /* Read name, watch for break, parity, erase, kill, end-of-line. */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000429 bp = logname;
Denis Vlasenko703aa132006-10-23 22:43:02 +0000430 cp->eol = '\0';
431 while (cp->eol == '\0') {
Robert Griebl1fca5582002-06-04 20:45:46 +0000432
433 /* Do not report trivial EINTR/EIO errors. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000434 if (read(STDIN_FILENO, &c, 1) < 1) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000435 if (errno == EINTR || errno == EIO)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000436 exit(EXIT_SUCCESS);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000437 bb_perror_msg_and_die("%s: read", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000438 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000439
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000440 /* BREAK. If we have speeds to try,
441 * return NULL (will switch speeds and return here) */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000442 if (c == '\0' && op->numspeed > 1)
Robert Griebl1fca5582002-06-04 20:45:46 +0000443 return NULL;
444
445 /* Do parity bit handling. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000446 if (!(op->flags & F_LOCAL) && (c & 0x80)) { /* "parity" bit on? */
Denis Vlasenko703aa132006-10-23 22:43:02 +0000447 bits = 1;
448 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 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000457
Denis Vlasenko703aa132006-10-23 22:43:02 +0000458 /* Do erase, kill and end-of-line processing. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000459 ascval = c & 0x7f;
Robert Griebl1fca5582002-06-04 20:45:46 +0000460 switch (ascval) {
461 case CR:
462 case NL:
Denis Vlasenko703aa132006-10-23 22:43:02 +0000463 *bp = '\0'; /* terminate logname */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000464 cp->eol = ascval; /* set end-of-line char */
Robert Griebl1fca5582002-06-04 20:45:46 +0000465 break;
466 case BS:
467 case DEL:
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000468#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000469 case '#':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000470#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000471 cp->erase = ascval; /* set erase character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000472 if (bp > logname) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000473 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000474 bp--;
475 }
476 break;
477 case CTL('U'):
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000478#ifdef ANCIENT_BS_KILL_CHARS
Robert Griebl1fca5582002-06-04 20:45:46 +0000479 case '@':
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000480#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000481 cp->kill = ascval; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000482 while (bp > logname) {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000483 full_write(STDOUT_FILENO, erase[cp->parity], 3);
Robert Griebl1fca5582002-06-04 20:45:46 +0000484 bp--;
485 }
486 break;
487 case CTL('D'):
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000488 exit(EXIT_SUCCESS);
Robert Griebl1fca5582002-06-04 20:45:46 +0000489 default:
Bernhard Reutner-Fischere44a4e32009-01-09 14:49:11 +0000490 if (!isprint(ascval)) {
Denis Vlasenko703aa132006-10-23 22:43:02 +0000491 /* ignore garbage characters */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000492 } else if ((int)(bp - logname) >= size_logname - 1) {
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000493 bb_error_msg_and_die("%s: input overrun", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000494 } else {
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 }
500 }
501 }
502 /* Handle names with upper case and no lower case. */
503
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000504#ifdef HANDLE_ALLCAPS
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000505 cp->capslock = all_is_upcase(logname);
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000506 if (cp->capslock) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000507 for (bp = logname; *bp; bp++)
508 if (isupper(*bp))
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000509 *bp = tolower(*bp); /* map name to lower case */
Robert Griebl1fca5582002-06-04 20:45:46 +0000510 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000511#endif
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000512 return logname;
Robert Griebl1fca5582002-06-04 20:45:46 +0000513}
514
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000515/* termios_final - set the final tty mode bits */
516static void termios_final(struct options *op, struct termios *tp, struct chardata *cp)
Robert Griebl1fca5582002-06-04 20:45:46 +0000517{
518 /* General terminal-independent stuff. */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000519 tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */
Robert Griebl1fca5582002-06-04 20:45:46 +0000520 tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE;
521 /* no longer| ECHOCTL | ECHOPRT */
522 tp->c_oflag |= OPOST;
523 /* tp->c_cflag = 0; */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000524 tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */
525 tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */
526 tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000527 tp->c_cc[VEOL] = DEF_EOL;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000528 tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000529
530 /* Account for special characters seen in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000531 if (cp->eol == CR) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000532 tp->c_iflag |= ICRNL; /* map CR in input to NL */
533 tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */
Robert Griebl1fca5582002-06-04 20:45:46 +0000534 }
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000535 tp->c_cc[VERASE] = cp->erase; /* set erase character */
536 tp->c_cc[VKILL] = cp->kill; /* set kill character */
Robert Griebl1fca5582002-06-04 20:45:46 +0000537
538 /* Account for the presence or absence of parity bits in input. */
Robert Griebl1fca5582002-06-04 20:45:46 +0000539 switch (cp->parity) {
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000540 case 0: /* space (always 0) parity */
Denis Vlasenko397de612008-03-17 08:55:44 +0000541// I bet most people go here - they use only 7-bit chars in usernames....
Robert Griebl1fca5582002-06-04 20:45:46 +0000542 break;
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000543 case 1: /* odd parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000544 tp->c_cflag |= PARODD;
545 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000546 case 2: /* even parity */
Robert Griebl1fca5582002-06-04 20:45:46 +0000547 tp->c_cflag |= PARENB;
548 tp->c_iflag |= INPCK | ISTRIP;
549 /* FALLTHROUGH */
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000550 case (1 | 2): /* no parity bit */
Robert Griebl1fca5582002-06-04 20:45:46 +0000551 tp->c_cflag &= ~CSIZE;
552 tp->c_cflag |= CS7;
Denis Vlasenko397de612008-03-17 08:55:44 +0000553// FIXME: wtf? case 3: we saw both even and odd 8-bit bytes -
554// it's probably some umlauts etc, but definitely NOT 7-bit!!!
555// Entire parity detection madness here just begs for deletion...
Robert Griebl1fca5582002-06-04 20:45:46 +0000556 break;
557 }
Robert Griebl1fca5582002-06-04 20:45:46 +0000558
Denis Vlasenkoe06bed32007-01-27 22:21:12 +0000559 /* Account for upper case without lower case. */
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000560#ifdef HANDLE_ALLCAPS
Robert Griebl1fca5582002-06-04 20:45:46 +0000561 if (cp->capslock) {
562 tp->c_iflag |= IUCLC;
563 tp->c_lflag |= XCASE;
564 tp->c_oflag |= OLCUC;
565 }
Denis Vlasenko4e70bf42006-10-23 10:17:34 +0000566#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000567 /* Optionally enable hardware flow control */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000568#ifdef CRTSCTS
Robert Griebl1fca5582002-06-04 20:45:46 +0000569 if (op->flags & F_RTSCTS)
570 tp->c_cflag |= CRTSCTS;
571#endif
572
573 /* Finally, make the new settings effective */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000574 /* It's tcsetattr_stdin_TCSANOW() + error check */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000575 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty);
Robert Griebl1fca5582002-06-04 20:45:46 +0000576}
577
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000578#if ENABLE_FEATURE_UTMP
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000579static void touch(const char *filename)
580{
581 if (access(filename, R_OK | W_OK) == -1)
582 close(open(filename, O_WRONLY | O_CREAT, 0664));
583}
584
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000585/* update_utmp - update our utmp entry */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000586static void update_utmp(const char *line, char *fakehost)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000587{
588 struct utmp ut;
589 struct utmp *utp;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000590 int mypid = getpid();
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000591
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000592 /* In case we won't find an entry below... */
593 memset(&ut, 0, sizeof(ut));
594 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
595
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000596 /*
597 * The utmp file holds miscellaneous information about things started by
598 * /sbin/init and other system-related events. Our purpose is to update
599 * the utmp entry for the current process, in particular the process type
600 * and the tty line we are listening to. Return successfully only if the
601 * utmp file can be opened for update, and if we are able to find our
602 * entry in the utmp file.
603 */
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000604 touch(_PATH_UTMP);
605
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000606 utmpname(_PATH_UTMP);
607 setutent();
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000608 while ((utp = getutent()) != NULL) {
609 if (utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) {
610 memcpy(&ut, utp, sizeof(ut));
611 break;
612 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000613 }
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000614
Rob Landleycf7577d2006-06-25 22:59:31 +0000615 strcpy(ut.ut_user, "LOGIN");
616 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000617 if (fakehost)
Rob Landleycf7577d2006-06-25 22:59:31 +0000618 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
Bernhard Reutner-Fischer62d85032008-06-01 10:10:22 +0000619 ut.ut_tv.tv_sec = time(NULL);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000620 ut.ut_type = LOGIN_PROCESS;
621 ut.ut_pid = mypid;
622
623 pututline(&ut);
624 endutent();
625
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000626#if ENABLE_FEATURE_WTMP
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000627 touch(bb_path_wtmp_file);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000628 updwtmp(bb_path_wtmp_file, &ut);
629#endif
Robert Griebl1fca5582002-06-04 20:45:46 +0000630}
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000631#endif /* CONFIG_FEATURE_UTMP */
Robert Griebl1fca5582002-06-04 20:45:46 +0000632
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000633int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000634int getty_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000635{
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000636 int n;
Denis Vlasenkof3fca912007-12-04 18:46:01 +0000637 char *fakehost = NULL; /* Fake hostname for ut_host */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000638 char *logname; /* login name, given to /bin/login */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000639 /* Merging these into "struct local" may _seem_ to reduce
640 * parameter passing, but today's gcc will inline
641 * statics which are called once anyway, so don't do that */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000642 struct chardata chardata; /* set by get_logname() */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000643 struct termios termios; /* terminal mode bits */
644 struct options options;
645
Denis Vlasenko397de612008-03-17 08:55:44 +0000646 chardata = init_chardata;
647
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000648 memset(&options, 0, sizeof(options));
649 options.login = _PATH_LOGIN; /* default login program */
650 options.tty = "tty1"; /* default tty line */
651 options.initstring = ""; /* modem init string */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000652#ifdef ISSUE
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000653 options.issue = ISSUE; /* default issue file */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000654#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000655
Denis Vlasenko397de612008-03-17 08:55:44 +0000656 /* Parse command-line arguments. */
657 parse_args(argv, &options, &fakehost);
658
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000659 logmode = LOGMODE_NONE;
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000660
661 /* Create new session, lose controlling tty, if any */
662 /* docs/ctty.htm says:
663 * "This is allowed only when the current process
664 * is not a process group leader" - is this a problem? */
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000665 setsid();
Denis Vlasenko397de612008-03-17 08:55:44 +0000666 /* close stdio, and stray descriptors, just in case */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000667 n = xopen(bb_dev_null, O_RDWR);
Denis Vlasenko397de612008-03-17 08:55:44 +0000668 /* dup2(n, 0); - no, we need to handle "getty - 9600" too */
669 xdup2(n, 1);
670 xdup2(n, 2);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000671 while (n > 2)
672 close(n--);
Denis Vlasenko397de612008-03-17 08:55:44 +0000673
674 /* Logging. We want special flavor of error_msg_and_die */
Denis Vlasenkodce3fde2006-10-23 02:10:45 +0000675 die_sleep = 10;
676 msg_eol = "\r\n";
Denis Vlasenko397de612008-03-17 08:55:44 +0000677 /* most likely will internally use fd #3 in CLOEXEC mode: */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000678 openlog(applet_name, LOG_PID, LOG_AUTH);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000679 logmode = LOGMODE_BOTH;
680
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000681#ifdef DEBUGGING
Denis Vlasenko5415c852008-07-21 23:05:26 +0000682 dbf = xfopen_for_write(DEBUGTERM);
Denis Vlasenko68404f12008-03-17 09:00:54 +0000683 for (n = 1; argv[n]; n++) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000684 debug(argv[n]);
685 debug("\n");
Robert Griebl1fca5582002-06-04 20:45:46 +0000686 }
687#endif
"Vladimir N. Oleynik"3e245c92005-09-29 11:31:26 +0000688
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000689 /* Open the tty as standard input, if it is not "-" */
Denis Vlasenko397de612008-03-17 08:55:44 +0000690 /* If it's not "-" and not taken yet, it will become our ctty */
691 debug("calling open_tty\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000692 open_tty(options.tty);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000693 ndelay_off(0);
Denis Vlasenko397de612008-03-17 08:55:44 +0000694 debug("duping\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000695 xdup2(0, 1);
696 xdup2(0, 2);
697
698 /*
699 * The following ioctl will fail if stdin is not a tty, but also when
700 * there is noise on the modem control lines. In the latter case, the
701 * common course of action is (1) fix your cables (2) give the modem more
702 * time to properly reset after hanging up. SunOS users can achieve (2)
703 * by patching the SunOS kernel variable "zsadtrlow" to a larger value;
704 * 5 seconds seems to be a good value.
705 */
Denis Vlasenko202ac502008-11-05 13:20:58 +0000706 /* tcgetattr() + error check */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000707 ioctl_or_perror_and_die(0, TCGETS, &termios, "%s: TCGETS", options.tty);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000708
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000709#ifdef __linux__
Denis Vlasenko397de612008-03-17 08:55:44 +0000710// FIXME: do we need this? Otherwise "-" case seems to be broken...
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000711 // /* Forcibly make fd 0 our controlling tty, even if another session
712 // * has it as a ctty. (Another session loses ctty). */
713 // ioctl(0, TIOCSCTTY, (void*)1);
Denis Vlasenko397de612008-03-17 08:55:44 +0000714 /* Make ourself a foreground process group within our session */
715 tcsetpgrp(0, getpid());
716#endif
717
718#if ENABLE_FEATURE_UTMP
719 /* Update the utmp file. This tty is ours now! */
720 update_utmp(options.tty, fakehost);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000721#endif
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000722
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000723 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
724 debug("calling termios_init\n");
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000725 termios_init(&termios, options.speeds[0], &options);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000726
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000727 /* Write the modem init string and DON'T flush the buffers */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000728 if (options.flags & F_INITSTRING) {
729 debug("writing init string\n");
Denis Vlasenko73c571a2009-03-09 00:12:37 +0000730 /* todo: use xwrite_str? */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000731 full_write(STDOUT_FILENO, options.initstring, strlen(options.initstring));
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000732 }
733
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000734 /* Optionally detect the baud rate from the modem status message */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000735 debug("before autobaud\n");
736 if (options.flags & F_PARSE)
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000737 auto_baud(line_buf, sizeof(line_buf), &termios);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000738
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000739 /* Set the optional timer */
Denis Vlasenko397de612008-03-17 08:55:44 +0000740 alarm(options.timeout); /* if 0, alarm is not set */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000741
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000742 /* Optionally wait for CR or LF before writing /etc/issue */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000743 if (options.flags & F_WAITCRLF) {
744 char ch;
745
746 debug("waiting for cr-lf\n");
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000747 while (safe_read(STDIN_FILENO, &ch, 1) == 1) {
Denis Vlasenko397de612008-03-17 08:55:44 +0000748 debug("read %x\n", (unsigned char)ch);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000749 ch &= 0x7f; /* strip "parity bit" */
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000750 if (ch == '\n' || ch == '\r')
751 break;
752 }
753 }
754
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000755 logname = NULL;
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000756 if (!(options.flags & F_NOPROMPT)) {
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000757 /* NB:termios_init already set line speed
758 * to options.speeds[0] */
759 int baud_index = 0;
760
761 while (1) {
762 /* Read the login name. */
763 debug("reading login name\n");
764 logname = get_logname(line_buf, sizeof(line_buf),
Denis Vlasenko68404f12008-03-17 09:00:54 +0000765 &options, &chardata);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000766 if (logname)
767 break;
768 /* we are here only if options.numspeed > 1 */
769 baud_index = (baud_index + 1) % options.numspeed;
Denis Vlasenko32a385f2009-04-12 13:05:40 +0000770 cfsetispeed(&termios, options.speeds[baud_index]);
771 cfsetospeed(&termios, options.speeds[baud_index]);
Denis Vlasenko202ac502008-11-05 13:20:58 +0000772 tcsetattr_stdin_TCSANOW(&termios);
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000773 }
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000774 }
775
776 /* Disable timer. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000777 alarm(0);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000778
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000779 /* Finalize the termios settings. */
Denis Vlasenko6476cc12006-11-07 01:52:10 +0000780 termios_final(&options, &termios, &chardata);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000781
782 /* Now the newline character should be properly written. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000783 full_write(STDOUT_FILENO, "\n", 1);
Bernhard Reutner-Fischer6d82f942006-06-16 16:37:07 +0000784
785 /* Let the login program take care of password validation. */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000786 /* We use PATH because we trust that root doesn't set "bad" PATH,
787 * and getty is not suid-root applet. */
Denis Vlasenko397de612008-03-17 08:55:44 +0000788 /* With -n, logname == NULL, and login will ask for username instead */
Denis Vlasenkod0bbbdc2007-12-04 09:48:40 +0000789 BB_EXECLP(options.login, options.login, "--", logname, NULL);
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000790 bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login);
Robert Griebl1fca5582002-06-04 20:45:46 +0000791}