Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* agetty.c - another getty program for Linux. By W. Z. Venema 1989 |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 3 | * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk> |
| 4 | * This program is freely distributable. The entire man-page used to |
| 5 | * be here. Now read the real man-page agetty.8 instead. |
| 6 | * |
| 7 | * option added by Eric Rasmussen <ear@usfirst.org> - 12/28/95 |
| 8 | * |
| 9 | * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org> |
| 10 | * - added Native Language Support |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +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> |
| 13 | * - enable hardware flow control before displaying /etc/issue |
| 14 | * |
| 15 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 16 | * |
| 17 | */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 19 | #include "libbb.h" |
Denis Vlasenko | f702652 | 2006-09-19 13:50:55 +0000 | [diff] [blame] | 20 | #include <syslog.h> |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 22 | #if ENABLE_FEATURE_UTMP |
Mike Frysinger | b381009 | 2005-07-01 01:29:44 +0000 | [diff] [blame] | 23 | #include <utmp.h> |
| 24 | #endif |
| 25 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 26 | /* |
| 27 | * Some heuristics to find out what environment we are in: if it is not |
| 28 | * System V, assume it is SunOS 4. |
| 29 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 30 | #ifdef LOGIN_PROCESS /* defined in System V utmp.h */ |
| 31 | #define SYSV_STYLE /* select System V style getty */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 32 | #include <sys/utsname.h> |
| 33 | #include <time.h> |
| 34 | #if ENABLE_FEATURE_WTMP |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 35 | extern void updwtmp(const char *filename, const struct utmp *ut); |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 36 | static void update_utmp(const char *line); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 37 | #endif |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 38 | #endif /* LOGIN_PROCESS */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 40 | /* |
| 41 | * Things you may want to modify. |
| 42 | * |
| 43 | * You may disagree with the default line-editing etc. characters defined |
| 44 | * below. Note, however, that DEL cannot be used for interrupt generation |
| 45 | * and for line editing at the same time. |
| 46 | */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 48 | /* I doubt there are systems which still need this */ |
| 49 | #undef HANDLE_ALLCAPS |
| 50 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 51 | #define _PATH_LOGIN "/bin/login" |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 53 | /* If ISSUE is not defined, getty will never display the contents of the |
| 54 | * /etc/issue file. You will not want to spit out large "issue" files at the |
| 55 | * wrong baud rate. |
| 56 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 57 | #define ISSUE "/etc/issue" /* displayed before the login prompt */ |
Eric Andersen | 77804ce | 2005-07-27 06:05:38 +0000 | [diff] [blame] | 58 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 59 | /* Some shorthands for control characters. */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 60 | #define CTL(x) (x ^ 0100) /* Assumes ASCII dialect */ |
| 61 | #define CR CTL('M') /* carriage return */ |
| 62 | #define NL CTL('J') /* line feed */ |
| 63 | #define BS CTL('H') /* back space */ |
| 64 | #define DEL CTL('?') /* delete */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 65 | |
| 66 | /* 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] | 67 | #define DEF_ERASE DEL /* default erase character */ |
| 68 | #define DEF_INTR CTL('C') /* default interrupt character */ |
| 69 | #define DEF_QUIT CTL('\\') /* default quit char */ |
| 70 | #define DEF_KILL CTL('U') /* default kill char */ |
| 71 | #define DEF_EOF CTL('D') /* default EOF char */ |
| 72 | #define DEF_EOL '\n' |
| 73 | #define DEF_SWITCH 0 /* default switch char */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 75 | /* |
| 76 | * When multiple baud rates are specified on the command line, the first one |
| 77 | * we will try is the first one specified. |
| 78 | */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 79 | #define FIRST_SPEED 0 |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 80 | |
| 81 | /* Storage for command-line options. */ |
| 82 | |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 83 | #define MAX_SPEED 10 /* max. nr. of baud rates */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 84 | |
| 85 | struct options { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 86 | int flags; /* toggle switches, see below */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 87 | unsigned timeout; /* time-out period */ |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 88 | 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" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 92 | int numspeed; /* number of baud rates to try */ |
| 93 | int speeds[MAX_SPEED]; /* baud rates to be tried */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 96 | static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn"; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 97 | #define F_INITSTRING (1<<0) /* initstring is set */ |
| 98 | #define F_LOCAL (1<<1) /* force local */ |
| 99 | #define F_FAKEHOST (1<<2) /* force fakehost */ |
| 100 | #define F_CUSTISSUE (1<<3) /* give alternative issue file */ |
| 101 | #define F_RTSCTS (1<<4) /* enable RTS/CTS flow control */ |
| 102 | #define F_ISSUE (1<<5) /* display /etc/issue */ |
| 103 | #define F_LOGIN (1<<6) /* non-default login program */ |
| 104 | #define F_PARSE (1<<7) /* process modem status messages */ |
| 105 | #define F_TIMEOUT (1<<8) /* time out */ |
| 106 | #define F_WAITCRLF (1<<9) /* wait for CR or LF */ |
| 107 | #define F_NOPROMPT (1<<10) /* don't ask for login name! */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 108 | |
| 109 | /* Storage for things detected while the login name was read. */ |
Mike Frysinger | b381009 | 2005-07-01 01:29:44 +0000 | [diff] [blame] | 110 | struct chardata { |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 111 | unsigned char erase; /* erase character */ |
| 112 | unsigned char kill; /* kill character */ |
| 113 | unsigned char eol; /* end-of-line character */ |
| 114 | unsigned char parity; /* what parity did we see */ |
| 115 | #ifdef HANDLE_ALLCAPS |
| 116 | unsigned char capslock; /* upper case without lower case */ |
| 117 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* Initial values for the above. */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 121 | static const struct chardata init_chardata = { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 122 | DEF_ERASE, /* default erase character */ |
| 123 | DEF_KILL, /* default kill character */ |
| 124 | 13, /* default eol char */ |
| 125 | 0, /* space parity */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 126 | #ifdef HANDLE_ALLCAPS |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 127 | 0, /* no capslock */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 128 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 131 | /* The following is used for understandable diagnostics. */ |
| 132 | |
| 133 | /* Fake hostname for ut_host specified on command line. */ |
| 134 | static char *fakehost = NULL; |
| 135 | |
| 136 | /* ... */ |
| 137 | #ifdef DEBUGGING |
| 138 | #define debug(s) fprintf(dbf,s); fflush(dbf) |
| 139 | #define DEBUGTERM "/dev/ttyp0" |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 140 | static FILE *dbf; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 141 | #else |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 142 | #define debug(s) /* nothing */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 143 | #endif |
| 144 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 145 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 146 | /* bcode - convert speed string to speed code; return 0 on failure */ |
| 147 | static int bcode(const char *s) |
| 148 | { |
| 149 | int r; |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 150 | unsigned value = bb_strtou(s, NULL, 10); |
| 151 | if (errno) { |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 152 | return -1; |
| 153 | } |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 154 | r = tty_value_to_baud(value); |
| 155 | if (r > 0) { |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 156 | return r; |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* parse_speeds - parse alternate baud rates */ |
| 163 | static void parse_speeds(struct options *op, char *arg) |
| 164 | { |
| 165 | char *cp; |
| 166 | |
| 167 | debug("entered parse_speeds\n"); |
| 168 | for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) { |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame^] | 169 | op->speeds[op->numspeed] = bcode(cp); |
| 170 | if (op->speeds[op->numspeed] <= 0) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 171 | bb_error_msg_and_die("bad speed: %s", cp); |
Denis Vlasenko | 6bef3d1 | 2007-11-06 03:05:54 +0000 | [diff] [blame^] | 172 | op->numspeed++; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 173 | if (op->numspeed > MAX_SPEED) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 174 | bb_error_msg_and_die("too many alternate speeds"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 175 | } |
| 176 | debug("exiting parsespeeds\n"); |
| 177 | } |
| 178 | |
| 179 | |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 180 | /* parse_args - parse command-line arguments */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 181 | static void parse_args(int argc, char **argv, struct options *op) |
| 182 | { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 183 | char *ts; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 185 | op->flags = getopt32(argv, opt_string, |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 186 | &(op->initstring), &fakehost, &(op->issue), |
| 187 | &(op->login), &ts); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 188 | if (op->flags & F_INITSTRING) { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 189 | const char *p = op->initstring; |
| 190 | char *q; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 191 | |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 192 | op->initstring = q = xstrdup(op->initstring); |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 193 | /* copy optarg into op->initstring decoding \ddd |
| 194 | octal codes into chars */ |
| 195 | while (*p) { |
| 196 | if (*p == '\\') { |
| 197 | p++; |
| 198 | *q++ = bb_process_escape_sequence(&p); |
| 199 | } else { |
| 200 | *q++ = *p++; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 201 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 202 | } |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 203 | *q = '\0'; |
| 204 | } |
| 205 | op->flags ^= F_ISSUE; /* revert flag show /etc/issue */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 206 | if (op->flags & F_TIMEOUT) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 207 | op->timeout = xatoul_range(ts, 1, INT_MAX); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 208 | } |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 209 | argv += optind; |
| 210 | argc -= optind; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 211 | debug("after getopt loop\n"); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 212 | if (argc < 2) /* check parameter count */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 213 | bb_show_usage(); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 214 | |
| 215 | /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 216 | if (isdigit(argv[0][0])) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 217 | /* a number first, assume it's a speed (BSD style) */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 218 | parse_speeds(op, argv[0]); /* baud rate(s) */ |
| 219 | op->tty = argv[1]; /* tty name */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 220 | } else { |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 221 | op->tty = argv[0]; /* tty name */ |
| 222 | parse_speeds(op, argv[1]); /* baud rate(s) */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 225 | if (argv[2]) |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 226 | setenv("TERM", argv[2], 1); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 227 | |
| 228 | debug("exiting parseargs\n"); |
| 229 | } |
| 230 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 231 | /* open_tty - set up tty as standard { input, output, error } */ |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 232 | static void open_tty(const char *tty, struct termios *tp, int local) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 233 | { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 234 | int chdir_to_root = 0; |
| 235 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 236 | /* Set up new standard input, unless we are given an already opened port. */ |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 237 | if (NOT_LONE_DASH(tty)) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 238 | struct stat st; |
Mike Frysinger | b381009 | 2005-07-01 01:29:44 +0000 | [diff] [blame] | 239 | int fd; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 240 | |
| 241 | /* Sanity checks... */ |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 242 | xchdir("/dev"); |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 243 | chdir_to_root = 1; |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 244 | xstat(tty, &st); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 245 | if ((st.st_mode & S_IFMT) != S_IFCHR) |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 246 | bb_error_msg_and_die("%s: not a character device", tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 247 | |
| 248 | /* Open the tty as standard input. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 249 | debug("open(2)\n"); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 250 | fd = xopen(tty, O_RDWR | O_NONBLOCK); |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 251 | xdup2(fd, 0); |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 252 | while (fd > 2) |
| 253 | close(fd--); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 254 | } else { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 255 | /* |
| 256 | * Standard input should already be connected to an open port. Make |
| 257 | * sure it is open for read/write. |
| 258 | */ |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 259 | if ((fcntl(0, F_GETFL) & O_RDWR) != O_RDWR) |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 260 | bb_error_msg_and_die("stdin is not open for read/write"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Mike Frysinger | b381009 | 2005-07-01 01:29:44 +0000 | [diff] [blame] | 263 | /* Replace current standard output/error fd's with new ones */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 264 | debug("duping\n"); |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 265 | xdup2(0, 1); |
| 266 | xdup2(0, 2); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * The following ioctl will fail if stdin is not a tty, but also when |
| 270 | * there is noise on the modem control lines. In the latter case, the |
| 271 | * common course of action is (1) fix your cables (2) give the modem more |
| 272 | * time to properly reset after hanging up. SunOS users can achieve (2) |
| 273 | * by patching the SunOS kernel variable "zsadtrlow" to a larger value; |
| 274 | * 5 seconds seems to be a good value. |
| 275 | */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 276 | ioctl_or_perror_and_die(0, TCGETS, tp, "%s: TCGETS", tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * It seems to be a terminal. Set proper protections and ownership. Mode |
| 280 | * 0622 is suitable for SYSV <4 because /bin/login does not change |
| 281 | * protections. SunOS 4 login will change the protections to 0620 (write |
| 282 | * access for group tty) after the login has succeeded. |
| 283 | */ |
| 284 | |
| 285 | #ifdef DEBIAN |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 286 | #warning Debian /dev/vcs[a]NN hack is deprecated and will be removed |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 287 | { |
| 288 | /* tty to root.dialout 660 */ |
| 289 | struct group *gr; |
| 290 | int id; |
| 291 | |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 292 | gr = getgrnam("dialout"); |
| 293 | id = gr ? gr->gr_gid : 0; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 294 | chown(tty, 0, id); |
| 295 | chmod(tty, 0660); |
| 296 | |
| 297 | /* vcs,vcsa to root.sys 600 */ |
| 298 | if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) { |
| 299 | char *vcs, *vcsa; |
| 300 | |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 301 | vcs = xstrdup(tty); |
| 302 | vcsa = xmalloc(strlen(tty) + 2); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 303 | strcpy(vcs, "vcs"); |
| 304 | strcpy(vcs + 3, tty + 3); |
| 305 | strcpy(vcsa, "vcsa"); |
| 306 | strcpy(vcsa + 4, tty + 3); |
| 307 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 308 | gr = getgrnam("sys"); |
| 309 | id = gr ? gr->gr_gid : 0; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 310 | chown(vcs, 0, id); |
| 311 | chmod(vcs, 0600); |
| 312 | chown(vcsa, 0, id); |
| 313 | chmod(vcs, 0600); |
| 314 | |
| 315 | free(vcs); |
| 316 | free(vcsa); |
| 317 | } |
| 318 | } |
| 319 | #else |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 320 | if (NOT_LONE_DASH(tty)) { |
| 321 | chown(tty, 0, 0); /* 0:0 */ |
| 322 | chmod(tty, 0622); /* crw--w--w- */ |
| 323 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 324 | #endif |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 325 | if (chdir_to_root) |
| 326 | xchdir("/"); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 329 | /* termios_init - initialize termios settings */ |
| 330 | static void termios_init(struct termios *tp, int speed, struct options *op) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 331 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 332 | /* |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 333 | * Initial termios settings: 8-bit characters, raw-mode, blocking i/o. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 334 | * Special characters are set after we have read the login name; all |
| 335 | * 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] | 336 | * later on. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 337 | */ |
| 338 | #ifdef __linux__ |
| 339 | /* flush input and output queues, important for modems! */ |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 340 | ioctl(0, TCFLSH, TCIOFLUSH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 341 | #endif |
| 342 | |
| 343 | tp->c_cflag = CS8 | HUPCL | CREAD | speed; |
| 344 | if (op->flags & F_LOCAL) { |
| 345 | tp->c_cflag |= CLOCAL; |
| 346 | } |
| 347 | |
Rob Landley | f78ab53 | 2006-08-24 20:00:44 +0000 | [diff] [blame] | 348 | tp->c_iflag = tp->c_lflag = tp->c_line = 0; |
| 349 | tp->c_oflag = OPOST | ONLCR; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 350 | tp->c_cc[VMIN] = 1; |
| 351 | tp->c_cc[VTIME] = 0; |
| 352 | |
| 353 | /* Optionally enable hardware flow control */ |
| 354 | |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 355 | #ifdef CRTSCTS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 356 | if (op->flags & F_RTSCTS) |
| 357 | tp->c_cflag |= CRTSCTS; |
| 358 | #endif |
| 359 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 360 | ioctl(0, TCSETS, tp); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 361 | |
| 362 | /* go to blocking input even in local mode */ |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 363 | ndelay_off(0); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 364 | |
| 365 | debug("term_io 2\n"); |
| 366 | } |
| 367 | |
| 368 | /* auto_baud - extract baud rate from modem status message */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 369 | static void auto_baud(char *buf, unsigned size_buf, struct termios *tp) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 370 | { |
| 371 | int speed; |
| 372 | int vmin; |
| 373 | unsigned iflag; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 374 | char *bp; |
| 375 | int nread; |
| 376 | |
| 377 | /* |
| 378 | * This works only if the modem produces its status code AFTER raising |
| 379 | * the DCD line, and if the computer is fast enough to set the proper |
| 380 | * baud rate before the message has gone by. We expect a message of the |
| 381 | * following format: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 382 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 383 | * <junk><number><junk> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 384 | * |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 385 | * The number is interpreted as the baud rate of the incoming call. If the |
| 386 | * modem does not tell us the baud rate within one second, we will keep |
| 387 | * using the current baud rate. It is advisable to enable BREAK |
| 388 | * processing (comma-separated list of baud rates) if the processing of |
| 389 | * modem status messages is enabled. |
| 390 | */ |
| 391 | |
| 392 | /* |
| 393 | * Use 7-bit characters, don't block if input queue is empty. Errors will |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 394 | * be dealt with later on. |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 395 | */ |
| 396 | |
| 397 | iflag = tp->c_iflag; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 398 | tp->c_iflag |= ISTRIP; /* enable 8th-bit stripping */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 399 | vmin = tp->c_cc[VMIN]; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 400 | tp->c_cc[VMIN] = 0; /* don't block if queue empty */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 401 | ioctl(0, TCSETS, tp); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * Wait for a while, then read everything the modem has said so far and |
| 405 | * try to extract the speed of the dial-in call. |
| 406 | */ |
| 407 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 408 | sleep(1); |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 409 | nread = read(0, buf, size_buf - 1); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 410 | if (nread > 0) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 411 | buf[nread] = '\0'; |
| 412 | for (bp = buf; bp < buf + nread; bp++) { |
| 413 | if (isascii(*bp) && isdigit(*bp)) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 414 | speed = bcode(bp); |
| 415 | if (speed) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 416 | tp->c_cflag &= ~CBAUD; |
| 417 | tp->c_cflag |= speed; |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 423 | /* Restore terminal settings. Errors will be dealt with later on. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 424 | |
| 425 | tp->c_iflag = iflag; |
| 426 | tp->c_cc[VMIN] = vmin; |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 427 | ioctl(0, TCSETS, tp); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 430 | /* next_speed - select next baud rate */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 431 | static void next_speed(struct termios *tp, struct options *op) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 432 | { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 433 | static int baud_index = FIRST_SPEED; /* current speed index */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 434 | |
| 435 | baud_index = (baud_index + 1) % op->numspeed; |
| 436 | tp->c_cflag &= ~CBAUD; |
| 437 | tp->c_cflag |= op->speeds[baud_index]; |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 438 | ioctl(0, TCSETS, tp); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 441 | |
| 442 | /* do_prompt - show login prompt, optionally preceded by /etc/issue contents */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 443 | static void do_prompt(struct options *op, struct termios *tp) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 444 | { |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 445 | #ifdef ISSUE |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 446 | print_login_issue(op->issue, op->tty); |
| 447 | #endif |
| 448 | print_login_prompt(); |
| 449 | } |
| 450 | |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 451 | #ifdef HANDLE_ALLCAPS |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 452 | /* caps_lock - string contains upper case without lower case */ |
| 453 | /* returns 1 if true, 0 if false */ |
| 454 | static int caps_lock(const char *s) |
| 455 | { |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 456 | while (*s) |
| 457 | if (islower(*s++)) |
| 458 | return 0; |
| 459 | return 1; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 460 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 461 | #endif |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 462 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 463 | /* get_logname - get user name, establish parity, speed, erase, kill, eol */ |
| 464 | /* return NULL on failure, logname on success */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 465 | static char *get_logname(char *logname, unsigned size_logname, |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 466 | struct options *op, struct chardata *cp, struct termios *tp) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 467 | { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 468 | char *bp; |
"Vladimir N. Oleynik" | 6f347ef | 2005-10-15 10:23:55 +0000 | [diff] [blame] | 469 | char c; /* input character, full eight bits */ |
| 470 | char ascval; /* low 7 bits of input character */ |
| 471 | int bits; /* # of "1" bits per character */ |
| 472 | int mask; /* mask with 1 bit up */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 473 | static const char erase[][3] = { /* backspace-space-backspace */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 474 | "\010\040\010", /* space parity */ |
| 475 | "\010\040\010", /* odd parity */ |
| 476 | "\210\240\210", /* even parity */ |
| 477 | "\210\240\210", /* no parity */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | /* Initialize kill, erase, parity etc. (also after switching speeds). */ |
| 481 | |
| 482 | *cp = init_chardata; |
| 483 | |
| 484 | /* Flush pending input (esp. after parsing or switching the baud rate). */ |
| 485 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 486 | sleep(1); |
| 487 | ioctl(0, TCFLSH, TCIFLUSH); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 488 | |
| 489 | /* Prompt for and read a login name. */ |
| 490 | |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 491 | logname[0] = '\0'; |
| 492 | while (!logname[0]) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 493 | |
| 494 | /* Write issue file and prompt, with "parity" bit == 0. */ |
| 495 | |
| 496 | do_prompt(op, tp); |
| 497 | |
| 498 | /* Read name, watch for break, parity, erase, kill, end-of-line. */ |
| 499 | |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 500 | bp = logname; |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 501 | cp->eol = '\0'; |
| 502 | while (cp->eol == '\0') { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 503 | |
| 504 | /* Do not report trivial EINTR/EIO errors. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 505 | if (read(0, &c, 1) < 1) { |
| 506 | if (errno == EINTR || errno == EIO) |
| 507 | exit(0); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 508 | bb_perror_msg_and_die("%s: read", op->tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 509 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 510 | |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 511 | /* Do BREAK handling elsewhere. */ |
| 512 | if (c == '\0' && op->numspeed > 1) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 513 | return NULL; |
| 514 | |
| 515 | /* Do parity bit handling. */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 516 | ascval = c & 0177; |
| 517 | if (c != ascval) { /* "parity" bit on ? */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 518 | bits = 1; |
| 519 | mask = 1; |
| 520 | while (mask & 0177) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 521 | if (mask & ascval) |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 522 | bits++; /* count "1" bits */ |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 523 | mask <<= 1; |
| 524 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 525 | /* ... |= 2 - even, 1 - odd */ |
| 526 | cp->parity |= 2 - (bits & 1); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 527 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 528 | |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 529 | /* Do erase, kill and end-of-line processing. */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 530 | switch (ascval) { |
| 531 | case CR: |
| 532 | case NL: |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 533 | *bp = '\0'; /* terminate logname */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 534 | cp->eol = ascval; /* set end-of-line char */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 535 | break; |
| 536 | case BS: |
| 537 | case DEL: |
| 538 | case '#': |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 539 | cp->erase = ascval; /* set erase character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 540 | if (bp > logname) { |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 541 | write(1, erase[cp->parity], 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 542 | bp--; |
| 543 | } |
| 544 | break; |
| 545 | case CTL('U'): |
| 546 | case '@': |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 547 | cp->kill = ascval; /* set kill character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 548 | while (bp > logname) { |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 549 | write(1, erase[cp->parity], 3); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 550 | bp--; |
| 551 | } |
| 552 | break; |
| 553 | case CTL('D'): |
| 554 | exit(0); |
| 555 | default: |
| 556 | if (!isascii(ascval) || !isprint(ascval)) { |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 557 | /* ignore garbage characters */ |
| 558 | } else if (bp - logname >= size_logname - 1) { |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 559 | bb_error_msg_and_die("%s: input overrun", op->tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 560 | } else { |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 561 | write(1, &c, 1); /* echo the character */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 562 | *bp++ = ascval; /* and store it */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 563 | } |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | /* Handle names with upper case and no lower case. */ |
| 569 | |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 570 | #ifdef HANDLE_ALLCAPS |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 571 | cp->capslock = caps_lock(logname); |
| 572 | if (cp->capslock) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 573 | for (bp = logname; *bp; bp++) |
| 574 | if (isupper(*bp)) |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 575 | *bp = tolower(*bp); /* map name to lower case */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 576 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 577 | #endif |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 578 | return logname; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 581 | /* termios_final - set the final tty mode bits */ |
| 582 | static void termios_final(struct options *op, struct termios *tp, struct chardata *cp) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 583 | { |
| 584 | /* General terminal-independent stuff. */ |
| 585 | |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 586 | tp->c_iflag |= IXON | IXOFF; /* 2-way flow control */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 587 | tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE; |
| 588 | /* no longer| ECHOCTL | ECHOPRT */ |
| 589 | tp->c_oflag |= OPOST; |
| 590 | /* tp->c_cflag = 0; */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 591 | tp->c_cc[VINTR] = DEF_INTR; /* default interrupt */ |
| 592 | tp->c_cc[VQUIT] = DEF_QUIT; /* default quit */ |
| 593 | tp->c_cc[VEOF] = DEF_EOF; /* default EOF character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 594 | tp->c_cc[VEOL] = DEF_EOL; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 595 | tp->c_cc[VSWTC] = DEF_SWITCH; /* default switch character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 596 | |
| 597 | /* Account for special characters seen in input. */ |
| 598 | |
| 599 | if (cp->eol == CR) { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 600 | tp->c_iflag |= ICRNL; /* map CR in input to NL */ |
| 601 | tp->c_oflag |= ONLCR; /* map NL in output to CR-NL */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 602 | } |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 603 | tp->c_cc[VERASE] = cp->erase; /* set erase character */ |
| 604 | tp->c_cc[VKILL] = cp->kill; /* set kill character */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 605 | |
| 606 | /* Account for the presence or absence of parity bits in input. */ |
| 607 | |
| 608 | switch (cp->parity) { |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 609 | case 0: /* space (always 0) parity */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 610 | break; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 611 | case 1: /* odd parity */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 612 | tp->c_cflag |= PARODD; |
| 613 | /* FALLTHROUGH */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 614 | case 2: /* even parity */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 615 | tp->c_cflag |= PARENB; |
| 616 | tp->c_iflag |= INPCK | ISTRIP; |
| 617 | /* FALLTHROUGH */ |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 618 | case (1 | 2): /* no parity bit */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 619 | tp->c_cflag &= ~CSIZE; |
| 620 | tp->c_cflag |= CS7; |
| 621 | break; |
| 622 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 623 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 624 | /* Account for upper case without lower case. */ |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 625 | #ifdef HANDLE_ALLCAPS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 626 | if (cp->capslock) { |
| 627 | tp->c_iflag |= IUCLC; |
| 628 | tp->c_lflag |= XCASE; |
| 629 | tp->c_oflag |= OLCUC; |
| 630 | } |
Denis Vlasenko | 4e70bf4 | 2006-10-23 10:17:34 +0000 | [diff] [blame] | 631 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 632 | /* Optionally enable hardware flow control */ |
| 633 | |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 634 | #ifdef CRTSCTS |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 635 | if (op->flags & F_RTSCTS) |
| 636 | tp->c_cflag |= CRTSCTS; |
| 637 | #endif |
| 638 | |
| 639 | /* Finally, make the new settings effective */ |
| 640 | |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 641 | ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 644 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 645 | #ifdef SYSV_STYLE |
| 646 | #if ENABLE_FEATURE_UTMP |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 647 | /* update_utmp - update our utmp entry */ |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 648 | static void update_utmp(const char *line) |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 649 | { |
| 650 | struct utmp ut; |
| 651 | struct utmp *utp; |
| 652 | time_t t; |
| 653 | int mypid = getpid(); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 654 | |
| 655 | /* |
| 656 | * The utmp file holds miscellaneous information about things started by |
| 657 | * /sbin/init and other system-related events. Our purpose is to update |
| 658 | * the utmp entry for the current process, in particular the process type |
| 659 | * and the tty line we are listening to. Return successfully only if the |
| 660 | * utmp file can be opened for update, and if we are able to find our |
| 661 | * entry in the utmp file. |
| 662 | */ |
| 663 | if (access(_PATH_UTMP, R_OK|W_OK) == -1) { |
| 664 | close(creat(_PATH_UTMP, 0664)); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 665 | } |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 666 | utmpname(_PATH_UTMP); |
| 667 | setutent(); |
| 668 | while ((utp = getutent()) |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 669 | && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid)) |
| 670 | /* nothing */; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 671 | |
| 672 | if (utp) { |
| 673 | memcpy(&ut, utp, sizeof(ut)); |
| 674 | } else { |
| 675 | /* some inits don't initialize utmp... */ |
| 676 | memset(&ut, 0, sizeof(ut)); |
Rob Landley | cf7577d | 2006-06-25 22:59:31 +0000 | [diff] [blame] | 677 | safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id)); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 678 | } |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 679 | /* endutent(); */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 680 | |
Rob Landley | cf7577d | 2006-06-25 22:59:31 +0000 | [diff] [blame] | 681 | strcpy(ut.ut_user, "LOGIN"); |
| 682 | safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line)); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 683 | if (fakehost) |
Rob Landley | cf7577d | 2006-06-25 22:59:31 +0000 | [diff] [blame] | 684 | safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host)); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 685 | time(&t); |
| 686 | ut.ut_time = t; |
| 687 | ut.ut_type = LOGIN_PROCESS; |
| 688 | ut.ut_pid = mypid; |
| 689 | |
| 690 | pututline(&ut); |
| 691 | endutent(); |
| 692 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 693 | #if ENABLE_FEATURE_WTMP |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 694 | if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) |
| 695 | close(creat(bb_path_wtmp_file, 0664)); |
| 696 | updwtmp(bb_path_wtmp_file, &ut); |
| 697 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 700 | #endif /* CONFIG_FEATURE_UTMP */ |
| 701 | #endif /* SYSV_STYLE */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 702 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 703 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 704 | int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 705 | int getty_main(int argc, char **argv) |
| 706 | { |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 707 | int nullfd; |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 708 | char *logname = NULL; /* login name, given to /bin/login */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 709 | /* Merging these into "struct local" may _seem_ to reduce |
| 710 | * parameter passing, but today's gcc will inline |
| 711 | * statics which are called once anyway, so don't do that */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 712 | struct chardata chardata; /* set by get_logname() */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 713 | struct termios termios; /* terminal mode bits */ |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 714 | struct options options = { |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 715 | 0, /* show /etc/issue (SYSV_STYLE) */ |
| 716 | 0, /* no timeout */ |
| 717 | _PATH_LOGIN, /* default login program */ |
| 718 | "tty1", /* default tty line */ |
| 719 | "", /* modem init string */ |
| 720 | #ifdef ISSUE |
| 721 | ISSUE, /* default issue file */ |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 722 | #else |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 723 | NULL, |
| 724 | #endif |
| 725 | 0, /* no baud rates known yet */ |
| 726 | }; |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 727 | |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 728 | /* Already too late because of theoretical |
| 729 | * possibility of getty --help somehow triggered |
| 730 | * inadvertently before we reach this. Oh well. */ |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 731 | logmode = LOGMODE_NONE; |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 732 | setsid(); |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 733 | nullfd = xopen(bb_dev_null, O_RDWR); |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 734 | /* dup2(nullfd, 0); - no, because of possible "getty - 9600" */ |
| 735 | /* open_tty() will take care of fd# 0 anyway */ |
| 736 | dup2(nullfd, 1); |
| 737 | dup2(nullfd, 2); |
| 738 | while (nullfd > 2) close(nullfd--); |
Denis Vlasenko | dce3fde | 2006-10-23 02:10:45 +0000 | [diff] [blame] | 739 | /* We want special flavor of error_msg_and_die */ |
| 740 | die_sleep = 10; |
| 741 | msg_eol = "\r\n"; |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 742 | openlog(applet_name, LOG_PID, LOG_AUTH); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 743 | logmode = LOGMODE_BOTH; |
| 744 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 745 | #ifdef DEBUGGING |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 746 | dbf = xfopen(DEBUGTERM, "w"); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 747 | |
| 748 | { |
| 749 | int i; |
| 750 | |
| 751 | for (i = 1; i < argc; i++) { |
| 752 | debug(argv[i]); |
| 753 | debug("\n"); |
| 754 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 755 | } |
| 756 | #endif |
"Vladimir N. Oleynik" | 3e245c9 | 2005-09-29 11:31:26 +0000 | [diff] [blame] | 757 | |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 758 | /* Parse command-line arguments. */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 759 | parse_args(argc, argv, &options); |
| 760 | |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 761 | #ifdef SYSV_STYLE |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 762 | #if ENABLE_FEATURE_UTMP |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 763 | /* Update the utmp file. */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 764 | update_utmp(options.tty); |
| 765 | #endif |
| 766 | #endif |
| 767 | |
| 768 | debug("calling open_tty\n"); |
| 769 | /* Open the tty as standard { input, output, error }. */ |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 770 | open_tty(options.tty, &termios, options.flags & F_LOCAL); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 771 | |
| 772 | #ifdef __linux__ |
| 773 | { |
| 774 | int iv; |
| 775 | |
| 776 | iv = getpid(); |
| 777 | ioctl(0, TIOCSPGRP, &iv); |
| 778 | } |
| 779 | #endif |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 780 | /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */ |
| 781 | debug("calling termios_init\n"); |
| 782 | termios_init(&termios, options.speeds[FIRST_SPEED], &options); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 783 | |
| 784 | /* write the modem init string and DON'T flush the buffers */ |
| 785 | if (options.flags & F_INITSTRING) { |
| 786 | debug("writing init string\n"); |
| 787 | write(1, options.initstring, strlen(options.initstring)); |
| 788 | } |
| 789 | |
| 790 | if (!(options.flags & F_LOCAL)) { |
| 791 | /* go to blocking write mode unless -L is specified */ |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 792 | ndelay_off(1); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* Optionally detect the baud rate from the modem status message. */ |
| 796 | debug("before autobaud\n"); |
| 797 | if (options.flags & F_PARSE) |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 798 | auto_baud(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), &termios); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 799 | |
| 800 | /* Set the optional timer. */ |
| 801 | if (options.timeout) |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 802 | alarm(options.timeout); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 803 | |
| 804 | /* optionally wait for CR or LF before writing /etc/issue */ |
| 805 | if (options.flags & F_WAITCRLF) { |
| 806 | char ch; |
| 807 | |
| 808 | debug("waiting for cr-lf\n"); |
| 809 | while (read(0, &ch, 1) == 1) { |
| 810 | ch &= 0x7f; /* strip "parity bit" */ |
| 811 | #ifdef DEBUGGING |
| 812 | fprintf(dbf, "read %c\n", ch); |
| 813 | #endif |
| 814 | if (ch == '\n' || ch == '\r') |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | chardata = init_chardata; |
| 820 | if (!(options.flags & F_NOPROMPT)) { |
| 821 | /* Read the login name. */ |
| 822 | debug("reading login name\n"); |
Denis Vlasenko | 703aa13 | 2006-10-23 22:43:02 +0000 | [diff] [blame] | 823 | logname = get_logname(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 824 | &options, &chardata, &termios); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 825 | while (logname == NULL) |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 826 | next_speed(&termios, &options); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | /* Disable timer. */ |
| 830 | |
| 831 | if (options.timeout) |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 832 | alarm(0); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 833 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 834 | /* Finalize the termios settings. */ |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 835 | |
Denis Vlasenko | 6476cc1 | 2006-11-07 01:52:10 +0000 | [diff] [blame] | 836 | termios_final(&options, &termios, &chardata); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 837 | |
| 838 | /* Now the newline character should be properly written. */ |
| 839 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 840 | write(1, "\n", 1); |
Bernhard Reutner-Fischer | 6d82f94 | 2006-06-16 16:37:07 +0000 | [diff] [blame] | 841 | |
| 842 | /* Let the login program take care of password validation. */ |
| 843 | |
Denis Vlasenko | 50e77e1 | 2006-10-23 02:11:22 +0000 | [diff] [blame] | 844 | execl(options.login, options.login, "--", logname, (char *) 0); |
Denis Vlasenko | a980165 | 2006-09-07 16:20:03 +0000 | [diff] [blame] | 845 | bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 846 | } |