Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Based on agetty - another getty program for Linux. By W. Z. Venema 1989 |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 4 | * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk> |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 5 | * This program is freely distributable. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 6 | * |
| 7 | * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95 |
| 8 | * |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 9 | * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org> |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 10 | * - Added Native Language Support |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 11 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 12 | * 1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net> |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 13 | * - Enabled hardware flow control before displaying /etc/issue |
| 14 | * |
| 15 | * 2011-01 Venys Vlasenko |
| 16 | * - Removed parity detection code. It can't work reliably: |
| 17 | * if all chars received have bit 7 cleared and odd (or even) parity, |
| 18 | * it is impossible to determine whether other side is 8-bit,no-parity |
| 19 | * or 7-bit,odd(even)-parity. It also interferes with non-ASCII usernames. |
| 20 | * - From now on, we assume that parity is correctly set. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 21 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 22 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 23 | */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Denis Vlasenko | f702652 | 2006-09-19 13:50:55 +0000 | [diff] [blame] | 26 | #include <syslog.h> |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 27 | #if ENABLE_FEATURE_UTMP |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 28 | # include <utmp.h> /* LOGIN_PROCESS */ |
Mike Frysinger | b381009 | 2005-07-01 01:29:44 +0000 | [diff] [blame] | 29 | #endif |
Denys Vlasenko | 9b1b62a | 2009-07-05 03:34:12 +0200 | [diff] [blame] | 30 | #ifndef IUCLC |
| 31 | # define IUCLC 0 |
| 32 | #endif |
| 33 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 34 | #ifndef LOGIN_PROCESS |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 35 | # undef ENABLE_FEATURE_UTMP |
| 36 | # undef ENABLE_FEATURE_WTMP |
| 37 | # define ENABLE_FEATURE_UTMP 0 |
| 38 | # define ENABLE_FEATURE_WTMP 0 |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 39 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 40 | |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 41 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 42 | /* The following is used for understandable diagnostics */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 43 | #ifdef DEBUGGING |
| 44 | static FILE *dbf; |
| 45 | # define DEBUGTERM "/dev/ttyp0" |
| 46 | # define debug(...) do { fprintf(dbf, __VA_ARGS__); fflush(dbf); } while (0) |
| 47 | #else |
| 48 | # define debug(...) ((void)0) |
| 49 | #endif |
| 50 | |
| 51 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 52 | /* |
| 53 | * Things you may want to modify. |
| 54 | * |
| 55 | * You may disagree with the default line-editing etc. characters defined |
| 56 | * below. Note, however, that DEL cannot be used for interrupt generation |
| 57 | * and for line editing at the same time. |
| 58 | */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 59 | #undef _PATH_LOGIN |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 60 | #define _PATH_LOGIN "/bin/login" |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 61 | |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 62 | /* Displayed before the login prompt. |
| 63 | * If ISSUE is not defined, getty will never display the contents of the |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 64 | * /etc/issue file. You will not want to spit out large "issue" files at the |
| 65 | * wrong baud rate. |
| 66 | */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 67 | #define ISSUE "/etc/issue" |
Eric Andersen | 77804ce | 2005-07-27 06:05:38 +0000 | [diff] [blame] | 68 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 69 | /* Some shorthands for control characters */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 70 | #define CTL(x) ((x) ^ 0100) /* Assumes ASCII dialect */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 71 | #define CR CTL('M') /* carriage return */ |
| 72 | #define NL CTL('J') /* line feed */ |
| 73 | #define BS CTL('H') /* back space */ |
| 74 | #define DEL CTL('?') /* delete */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 75 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 76 | /* Defaults for line-editing etc. characters; you may want to change this */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 77 | #define DEF_ERASE DEL /* default erase character */ |
| 78 | #define DEF_INTR CTL('C') /* default interrupt character */ |
| 79 | #define DEF_QUIT CTL('\\') /* default quit char */ |
| 80 | #define DEF_KILL CTL('U') /* default kill char */ |
| 81 | #define DEF_EOF CTL('D') /* default EOF char */ |
| 82 | #define DEF_EOL '\n' |
| 83 | #define DEF_SWITCH 0 /* default switch char */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 84 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 85 | /* |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 86 | * When multiple baud rates are specified on the command line, |
| 87 | * the first one we will try is the first one specified. |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 88 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 89 | #define MAX_SPEED 10 /* max. nr. of baud rates */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 91 | struct globals { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 92 | unsigned timeout; /* time-out period */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 93 | const char *login; /* login program */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 94 | const char *fakehost; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 95 | const char *tty; /* name of tty */ |
| 96 | const char *initstring; /* modem init string */ |
| 97 | const char *issue; /* alternative issue file */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 98 | int numspeed; /* number of baud rates to try */ |
| 99 | int speeds[MAX_SPEED]; /* baud rates to be tried */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 100 | unsigned char eol; /* end-of-line char seen (CR or NL) */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 101 | struct termios termios; /* terminal mode bits */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 102 | char line_buf[128]; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 105 | #define G (*ptr_to_globals) |
| 106 | #define INIT_G() do { \ |
| 107 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 108 | } while (0) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 109 | |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 110 | //usage:#define getty_trivial_usage |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 111 | //usage: "[OPTIONS] BAUD_RATE[,BAUD_RATE]... TTY [TERMTYPE]" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 112 | //usage:#define getty_full_usage "\n\n" |
| 113 | //usage: "Open a tty, prompt for a login name, then invoke /bin/login\n" |
| 114 | //usage: "\nOptions:" |
| 115 | //usage: "\n -h Enable hardware (RTS/CTS) flow control" |
| 116 | //usage: "\n -i Don't display /etc/issue" |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 117 | //usage: "\n -L Local line, set CLOCAL on it" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 118 | //usage: "\n -m Get baud rate from modem's CONNECT status message" |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 119 | //usage: "\n -w Wait for CR or LF before sending /etc/issue" |
| 120 | //usage: "\n -n Don't prompt for a login name" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 121 | //usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue" |
| 122 | //usage: "\n -l LOGIN Invoke LOGIN instead of /bin/login" |
| 123 | //usage: "\n -t SEC Terminate after SEC if no username is read" |
| 124 | //usage: "\n -I INITSTR Send INITSTR before anything else" |
| 125 | //usage: "\n -H HOST Log HOST into the utmp file as the hostname" |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 126 | //usage: "\n" |
| 127 | //usage: "\nBAUD_RATE of 0 leaves it unchanged" |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 128 | |
| 129 | static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn"; |
| 130 | #define F_INITSTRING (1 << 0) /* -I */ |
| 131 | #define F_LOCAL (1 << 1) /* -L */ |
| 132 | #define F_FAKEHOST (1 << 2) /* -H */ |
| 133 | #define F_CUSTISSUE (1 << 3) /* -f */ |
| 134 | #define F_RTSCTS (1 << 4) /* -h */ |
| 135 | #define F_NOISSUE (1 << 5) /* -i */ |
| 136 | #define F_LOGIN (1 << 6) /* -l */ |
| 137 | #define F_PARSE (1 << 7) /* -m */ |
| 138 | #define F_TIMEOUT (1 << 8) /* -t */ |
| 139 | #define F_WAITCRLF (1 << 9) /* -w */ |
| 140 | #define F_NOPROMPT (1 << 10) /* -n */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 141 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 142 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 143 | /* convert speed string to speed code; return <= 0 on failure */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 144 | static int bcode(const char *s) |
| 145 | { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 146 | int value = bb_strtou(s, NULL, 10); /* yes, int is intended! */ |
| 147 | if (value < 0) /* bad terminating char, overflow, etc */ |
| 148 | return value; |
| 149 | return tty_value_to_baud(value); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 152 | /* parse alternate baud rates */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 153 | static void parse_speeds(char *arg) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 154 | { |
| 155 | char *cp; |
| 156 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 157 | /* NB: at least one iteration is always done */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 158 | debug("entered parse_speeds\n"); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 159 | while ((cp = strsep(&arg, ",")) != NULL) { |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 160 | G.speeds[G.numspeed] = bcode(cp); |
| 161 | if (G.speeds[G.numspeed] < 0) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 162 | bb_error_msg_and_die("bad speed: %s", cp); |
Denis Vlasenko | 32a385f | 2009-04-12 13:05:40 +0000 | [diff] [blame] | 163 | /* note: arg "0" turns into speed B0 */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 164 | G.numspeed++; |
| 165 | if (G.numspeed > MAX_SPEED) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 166 | bb_error_msg_and_die("too many alternate speeds"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 167 | } |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 168 | debug("exiting parse_speeds\n"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 171 | /* parse command-line arguments */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 172 | static void parse_args(char **argv) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 173 | { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 174 | char *ts; |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 175 | int flags; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 177 | opt_complementary = "-2:t+"; /* at least 2 args; -t N */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 178 | flags = getopt32(argv, opt_string, |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 179 | &G.initstring, &G.fakehost, &G.issue, |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 180 | &G.login, &G.timeout |
| 181 | ); |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 182 | if (flags & F_INITSTRING) { |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 183 | G.initstring = xstrdup(G.initstring); |
Denys Vlasenko | 5360059 | 2010-10-23 21:06:06 +0200 | [diff] [blame] | 184 | /* decode \ddd octal codes into chars */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 185 | strcpy_and_process_escape_sequences((char*)G.initstring, G.initstring); |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 186 | } |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 187 | argv += optind; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 188 | debug("after getopt\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 189 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 190 | /* We loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ |
| 191 | G.tty = argv[0]; /* tty name */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 192 | ts = argv[1]; /* baud rate(s) */ |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 193 | if (isdigit(argv[0][0])) { |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 194 | /* A number first, assume it's a speed (BSD style) */ |
| 195 | G.tty = ts; /* tty name is in argv[1] */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 196 | ts = argv[0]; /* baud rate(s) */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 197 | } |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 198 | parse_speeds(ts); |
| 199 | applet_name = xasprintf("getty: %s", G.tty); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 200 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 201 | if (argv[2]) |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 202 | xsetenv("TERM", argv[2]); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 203 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 204 | debug("exiting parse_args\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 207 | /* set up tty as standard input, output, error */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 208 | static void open_tty(void) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 209 | { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 210 | /* Set up new standard input, unless we are given an already opened port */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 211 | if (NOT_LONE_DASH(G.tty)) { |
| 212 | if (G.tty[0] != '/') |
| 213 | G.tty = xasprintf("/dev/%s", G.tty); /* will leak it */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 214 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 215 | /* Open the tty as standard input */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 216 | debug("open(2)\n"); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 217 | close(0); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 218 | xopen(G.tty, O_RDWR | O_NONBLOCK); /* uses fd 0 */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 219 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 220 | /* Set proper protections and ownership */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 221 | fchown(0, 0, 0); /* 0:0 */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 222 | fchmod(0, 0620); /* crw--w---- */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 223 | } else { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 224 | /* |
| 225 | * Standard input should already be connected to an open port. Make |
| 226 | * sure it is open for read/write. |
| 227 | */ |
Denys Vlasenko | 006416e | 2011-01-22 17:29:53 +0100 | [diff] [blame] | 228 | if ((fcntl(0, F_GETFL) & (O_RDWR|O_RDONLY|O_WRONLY)) != O_RDWR) |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 229 | bb_error_msg_and_die("stdin is not open for read/write"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 230 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 233 | /* We manipulate termios this way: |
| 234 | * - first, we read existing termios settings |
| 235 | * - termios_init modifies some parts and sets it |
| 236 | * - auto_baud and/or BREAK processing can set different speed and set termios |
| 237 | * - termios_final again modifies some parts and sets termios before |
| 238 | * execing login |
| 239 | */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 240 | static void termios_init(int speed) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 241 | { |
Denys Vlasenko | dc6cd12 | 2011-01-24 00:28:43 +0100 | [diff] [blame] | 242 | /* Flush input and output queues, important for modems! |
| 243 | * Users report losing previously queued output chars, and I hesitate |
| 244 | * to use tcdrain here instead of tcflush - I imagine it can block. |
| 245 | * Using small sleep instead. |
| 246 | */ |
| 247 | usleep(100*1000); /* 0.1 sec */ |
| 248 | tcflush(STDIN_FILENO, TCIOFLUSH); |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 249 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 250 | /* Set speed if it wasn't specified as "0" on command line */ |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 251 | if (speed != B0) |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 252 | cfsetspeed(&G.termios, speed); |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 253 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 254 | /* |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 255 | * Initial termios settings: 8-bit characters, raw-mode, blocking i/o. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 256 | * Special characters are set after we have read the login name; all |
| 257 | * reads will be done in raw mode anyway. Errors will be dealt with |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 258 | * later on. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 259 | */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 260 | G.termios.c_cflag = CS8 | HUPCL | CREAD; |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 261 | if (option_mask32 & F_LOCAL) |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 262 | G.termios.c_cflag |= CLOCAL; |
| 263 | G.termios.c_iflag = 0; |
| 264 | G.termios.c_lflag = 0; |
| 265 | G.termios.c_oflag = OPOST | ONLCR; |
| 266 | G.termios.c_cc[VMIN] = 1; |
| 267 | G.termios.c_cc[VTIME] = 0; |
Jeremie Koenig | f812eac | 2010-05-27 15:37:32 +0200 | [diff] [blame] | 268 | #ifdef __linux__ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 269 | G.termios.c_line = 0; |
Jeremie Koenig | f812eac | 2010-05-27 15:37:32 +0200 | [diff] [blame] | 270 | #endif |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 271 | #ifdef CRTSCTS |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 272 | if (option_mask32 & F_RTSCTS) |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 273 | G.termios.c_cflag |= CRTSCTS; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 274 | #endif |
| 275 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 276 | tcsetattr_stdin_TCSANOW(&G.termios); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 277 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 278 | debug("term_io 2\n"); |
| 279 | } |
| 280 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 281 | static void termios_final(void) |
| 282 | { |
| 283 | /* General terminal-independent stuff */ |
| 284 | G.termios.c_iflag |= IXON | IXOFF; /* 2-way flow control */ |
| 285 | G.termios.c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE; |
| 286 | /* no longer in lflag: | ECHOCTL | ECHOPRT */ |
| 287 | G.termios.c_oflag |= OPOST; |
| 288 | /* G.termios.c_cflag = 0; */ |
| 289 | G.termios.c_cc[VINTR] = DEF_INTR; |
| 290 | G.termios.c_cc[VQUIT] = DEF_QUIT; |
| 291 | G.termios.c_cc[VEOF] = DEF_EOF; |
| 292 | G.termios.c_cc[VEOL] = DEF_EOL; |
| 293 | #ifdef VSWTC |
| 294 | G.termios.c_cc[VSWTC] = DEF_SWITCH; |
| 295 | #endif |
| 296 | |
| 297 | /* Account for special characters seen in input */ |
| 298 | if (G.eol == CR) { |
| 299 | G.termios.c_iflag |= ICRNL; /* map CR in input to NL */ |
| 300 | /* already done by termios_init */ |
| 301 | /* G.termios.c_oflag |= ONLCR; map NL in output to CR-NL */ |
| 302 | } |
| 303 | G.termios.c_cc[VKILL] = DEF_KILL; |
| 304 | |
| 305 | #ifdef CRTSCTS |
| 306 | /* Optionally enable hardware flow control */ |
| 307 | if (option_mask32 & F_RTSCTS) |
| 308 | G.termios.c_cflag |= CRTSCTS; |
| 309 | #endif |
| 310 | |
| 311 | /* Finally, make the new settings effective */ |
| 312 | if (tcsetattr_stdin_TCSANOW(&G.termios) < 0) |
| 313 | bb_perror_msg_and_die("tcsetattr"); |
| 314 | } |
| 315 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 316 | /* extract baud rate from modem status message */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 317 | static void auto_baud(void) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 318 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 319 | int nread; |
| 320 | |
| 321 | /* |
| 322 | * This works only if the modem produces its status code AFTER raising |
| 323 | * the DCD line, and if the computer is fast enough to set the proper |
| 324 | * baud rate before the message has gone by. We expect a message of the |
| 325 | * following format: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 326 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 327 | * <junk><number><junk> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 328 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 329 | * The number is interpreted as the baud rate of the incoming call. If the |
| 330 | * modem does not tell us the baud rate within one second, we will keep |
| 331 | * using the current baud rate. It is advisable to enable BREAK |
| 332 | * processing (comma-separated list of baud rates) if the processing of |
| 333 | * modem status messages is enabled. |
| 334 | */ |
| 335 | |
| 336 | /* |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 337 | * Don't block if input queue is empty. |
| 338 | * Errors will be dealt with later on. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 339 | */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 340 | G.termios.c_cc[VMIN] = 0; /* don't block if queue empty */ |
| 341 | tcsetattr_stdin_TCSANOW(&G.termios); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 342 | |
| 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 Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 347 | sleep(1); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 348 | nread = safe_read(STDIN_FILENO, G.line_buf, sizeof(G.line_buf) - 1); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 349 | if (nread > 0) { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 350 | int speed; |
| 351 | char *bp; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 352 | G.line_buf[nread] = '\0'; |
| 353 | for (bp = G.line_buf; bp < G.line_buf + nread; bp++) { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 354 | if (isdigit(*bp)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 355 | speed = bcode(bp); |
Jeremie Koenig | f812eac | 2010-05-27 15:37:32 +0200 | [diff] [blame] | 356 | if (speed > 0) |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 357 | cfsetspeed(&G.termios, speed); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 362 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 363 | /* Restore terminal settings. Errors will be dealt with later on */ |
| 364 | G.termios.c_cc[VMIN] = 1; /* restore to value set by termios_init */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 365 | tcsetattr_stdin_TCSANOW(&G.termios); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 368 | /* get user name, establish parity, speed, erase, kill, eol; |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 369 | * return NULL on BREAK, logname on success |
| 370 | */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 371 | static char *get_logname(void) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 372 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 373 | char *bp; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 374 | char c; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 375 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 376 | /* Flush pending input (esp. after parsing or switching the baud rate) */ |
Denys Vlasenko | dc6cd12 | 2011-01-24 00:28:43 +0100 | [diff] [blame] | 377 | usleep(100*1000); /* 0.1 sec */ |
| 378 | tcflush(STDIN_FILENO, TCIOFLUSH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 379 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 380 | /* Prompt for and read a login name */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 381 | G.line_buf[0] = '\0'; |
| 382 | while (!G.line_buf[0]) { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 383 | /* Write issue file and prompt */ |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 384 | #ifdef ISSUE |
| 385 | if (!(option_mask32 & F_NOISSUE)) |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 386 | print_login_issue(G.issue, G.tty); |
Denys Vlasenko | 1f08404 | 2011-01-24 00:16:50 +0100 | [diff] [blame] | 387 | #endif |
| 388 | print_login_prompt(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 389 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 390 | /* Read name, watch for break, parity, erase, kill, end-of-line */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 391 | bp = G.line_buf; |
| 392 | G.eol = '\0'; |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 393 | while (1) { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 394 | /* Do not report trivial EINTR/EIO errors */ |
Denys Vlasenko | 765b0ee | 2010-05-22 21:17:46 +0200 | [diff] [blame] | 395 | errno = EINTR; /* make read of 0 bytes be silent too */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 396 | if (read(STDIN_FILENO, &c, 1) < 1) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 397 | if (errno == EINTR || errno == EIO) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 398 | exit(EXIT_SUCCESS); |
Denys Vlasenko | 9dc0412 | 2010-09-01 11:31:43 +0200 | [diff] [blame] | 399 | bb_perror_msg_and_die(bb_msg_read_error); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 400 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 401 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 402 | /* BREAK. If we have speeds to try, |
| 403 | * return NULL (will switch speeds and return here) */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 404 | if (c == '\0' && G.numspeed > 1) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 405 | return NULL; |
| 406 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 407 | /* Do erase, kill and end-of-line processing */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 408 | switch (c) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 409 | case CR: |
| 410 | case NL: |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 411 | *bp = '\0'; |
| 412 | G.eol = c; |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 413 | goto got_logname; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 414 | case BS: |
| 415 | case DEL: |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 416 | G.termios.c_cc[VERASE] = c; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 417 | if (bp > G.line_buf) { |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 418 | full_write(STDOUT_FILENO, "\010 \010", 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 419 | bp--; |
| 420 | } |
| 421 | break; |
| 422 | case CTL('U'): |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 423 | while (bp > G.line_buf) { |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 424 | full_write(STDOUT_FILENO, "\010 \010", 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 425 | bp--; |
| 426 | } |
| 427 | break; |
| 428 | case CTL('D'): |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 429 | exit(EXIT_SUCCESS); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 430 | default: |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 431 | if ((unsigned char)c < ' ') { |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 432 | /* ignore garbage characters */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 433 | } else if ((int)(bp - G.line_buf) < sizeof(G.line_buf) - 1) { |
| 434 | /* echo and store the character */ |
| 435 | full_write(STDOUT_FILENO, &c, 1); |
| 436 | *bp++ = c; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 437 | } |
| 438 | break; |
| 439 | } |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 440 | } /* end of get char loop */ |
| 441 | got_logname: ; |
| 442 | } /* while logname is empty */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 443 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 444 | return G.line_buf; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 447 | int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 448 | int getty_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 449 | { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 450 | int n; |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 451 | pid_t pid; |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 452 | char *logname; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 453 | |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 454 | INIT_G(); |
| 455 | G.login = _PATH_LOGIN; /* default login program */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 456 | #ifdef ISSUE |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 457 | G.issue = ISSUE; /* default issue file */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 458 | #endif |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 459 | G.eol = CR; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 460 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 461 | /* Parse command-line arguments */ |
| 462 | parse_args(argv); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 463 | |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 464 | logmode = LOGMODE_NONE; |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 465 | |
| 466 | /* Create new session, lose controlling tty, if any */ |
| 467 | /* docs/ctty.htm says: |
| 468 | * "This is allowed only when the current process |
| 469 | * is not a process group leader" - is this a problem? */ |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 470 | setsid(); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 471 | /* close stdio, and stray descriptors, just in case */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 472 | n = xopen(bb_dev_null, O_RDWR); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 473 | /* dup2(n, 0); - no, we need to handle "getty - 9600" too */ |
| 474 | xdup2(n, 1); |
| 475 | xdup2(n, 2); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 476 | while (n > 2) |
| 477 | close(n--); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 478 | |
| 479 | /* Logging. We want special flavor of error_msg_and_die */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 480 | die_sleep = 10; |
| 481 | msg_eol = "\r\n"; |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 482 | /* most likely will internally use fd #3 in CLOEXEC mode: */ |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 483 | openlog(applet_name, LOG_PID, LOG_AUTH); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 484 | logmode = LOGMODE_BOTH; |
| 485 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 486 | #ifdef DEBUGGING |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 487 | dbf = xfopen_for_write(DEBUGTERM); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 488 | for (n = 1; argv[n]; n++) { |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 489 | debug(argv[n]); |
| 490 | debug("\n"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 491 | } |
| 492 | #endif |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 493 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 494 | /* Open the tty as standard input, if it is not "-" */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 495 | /* If it's not "-" and not taken yet, it will become our ctty */ |
| 496 | debug("calling open_tty\n"); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 497 | open_tty(); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 498 | ndelay_off(0); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 499 | debug("duping\n"); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 500 | xdup2(0, 1); |
| 501 | xdup2(0, 2); |
| 502 | |
| 503 | /* |
| 504 | * The following ioctl will fail if stdin is not a tty, but also when |
| 505 | * there is noise on the modem control lines. In the latter case, the |
| 506 | * common course of action is (1) fix your cables (2) give the modem more |
| 507 | * time to properly reset after hanging up. SunOS users can achieve (2) |
| 508 | * by patching the SunOS kernel variable "zsadtrlow" to a larger value; |
| 509 | * 5 seconds seems to be a good value. |
| 510 | */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 511 | if (tcgetattr(STDIN_FILENO, &G.termios) < 0) |
Denys Vlasenko | 9dc0412 | 2010-09-01 11:31:43 +0200 | [diff] [blame] | 512 | bb_perror_msg_and_die("tcgetattr"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 513 | |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 514 | pid = getpid(); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 515 | #ifdef __linux__ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 516 | // FIXME: do we need this? Otherwise "-" case seems to be broken... |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 517 | // /* Forcibly make fd 0 our controlling tty, even if another session |
| 518 | // * has it as a ctty. (Another session loses ctty). */ |
Denys Vlasenko | dc6cd12 | 2011-01-24 00:28:43 +0100 | [diff] [blame] | 519 | // ioctl(STDIN_FILENO, TIOCSCTTY, (void*)1); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 520 | /* Make ourself a foreground process group within our session */ |
Denys Vlasenko | dc6cd12 | 2011-01-24 00:28:43 +0100 | [diff] [blame] | 521 | tcsetpgrp(STDIN_FILENO, pid); |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 522 | #endif |
| 523 | |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 524 | /* Update the utmp file. This tty is ours now! */ |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 525 | update_utmp(pid, LOGIN_PROCESS, G.tty, "LOGIN", G.fakehost); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 526 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 527 | /* Initialize the termios settings (raw mode, eight-bit, blocking i/o) */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 528 | debug("calling termios_init\n"); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 529 | termios_init(G.speeds[0]); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 530 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 531 | /* Write the modem init string and DON'T flush the buffers */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 532 | if (option_mask32 & F_INITSTRING) { |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 533 | debug("writing init string\n"); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 534 | full_write1_str(G.initstring); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 537 | /* Optionally detect the baud rate from the modem status message */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 538 | debug("before autobaud\n"); |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 539 | if (option_mask32 & F_PARSE) |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 540 | auto_baud(); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 541 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 542 | /* Set the optional timer */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 543 | alarm(G.timeout); /* if 0, alarm is not set */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 544 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 545 | /* Optionally wait for CR or LF before writing /etc/issue */ |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 546 | if (option_mask32 & F_WAITCRLF) { |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 547 | char ch; |
| 548 | |
| 549 | debug("waiting for cr-lf\n"); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 550 | while (safe_read(STDIN_FILENO, &ch, 1) == 1) { |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 551 | debug("read %x\n", (unsigned char)ch); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 552 | if (ch == '\n' || ch == '\r') |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 557 | logname = NULL; |
Denys Vlasenko | bcdb9b8 | 2011-01-09 21:44:51 +0100 | [diff] [blame] | 558 | if (!(option_mask32 & F_NOPROMPT)) { |
Denys Vlasenko | 2b57b6c | 2011-01-22 03:06:40 +0100 | [diff] [blame] | 559 | /* NB: termios_init already set line speed |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 560 | * to G.speeds[0] */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 561 | int baud_index = 0; |
| 562 | |
| 563 | while (1) { |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 564 | /* Read the login name */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 565 | debug("reading login name\n"); |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 566 | logname = get_logname(); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 567 | if (logname) |
| 568 | break; |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 569 | /* We are here only if G.numspeed > 1 */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 570 | baud_index = (baud_index + 1) % G.numspeed; |
| 571 | cfsetspeed(&G.termios, G.speeds[baud_index]); |
| 572 | tcsetattr_stdin_TCSANOW(&G.termios); |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 573 | } |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 576 | /* Disable timer */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 577 | alarm(0); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 578 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 579 | /* Finalize the termios settings */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 580 | termios_final(); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 581 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 582 | /* Now the newline character should be properly written */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 583 | full_write(STDOUT_FILENO, "\n", 1); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 584 | |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 585 | /* Let the login program take care of password validation */ |
Denis Vlasenko | d0bbbdc | 2007-12-04 09:48:40 +0000 | [diff] [blame] | 586 | /* We use PATH because we trust that root doesn't set "bad" PATH, |
Denys Vlasenko | faaf8cb | 2011-01-24 14:35:09 +0100 | [diff] [blame] | 587 | * and getty is not suid-root applet */ |
Denis Vlasenko | 397de61 | 2008-03-17 08:55:44 +0000 | [diff] [blame] | 588 | /* With -n, logname == NULL, and login will ask for username instead */ |
Denys Vlasenko | 6596380 | 2011-01-24 04:43:04 +0100 | [diff] [blame] | 589 | BB_EXECLP(G.login, G.login, "--", logname, NULL); |
| 590 | bb_error_msg_and_die("can't execute '%s'", G.login); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 591 | } |