blob: f6fad684cff529995852ae3ce1c9125f006343f8 [file] [log] [blame]
Eric Andersen28c70b32000-06-14 20:42:57 +00001/* vi: set sw=4 ts=4: */
Erik Andersenf7c49ef2000-02-22 17:17:45 +00002/*
Eric Andersen28c70b32000-06-14 20:42:57 +00003 * telnet implementation for busybox
Erik Andersenf7c49ef2000-02-22 17:17:45 +00004 *
Eric Andersen28c70b32000-06-14 20:42:57 +00005 * Author: Tomi Ollila <too@iki.fi>
6 * Copyright (C) 1994-2000 by Tomi Ollila
7 *
8 * Created: Thu Apr 7 13:29:41 1994 too
9 * Last modified: Fri Jun 9 14:34:24 2000 too
Erik Andersenf7c49ef2000-02-22 17:17:45 +000010 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020011 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersenf7c49ef2000-02-22 17:17:45 +000012 *
Eric Andersen28c70b32000-06-14 20:42:57 +000013 * HISTORY
14 * Revision 3.1 1994/04/17 11:31:54 too
15 * initial revision
Eric Andersencb81e642003-07-14 21:21:08 +000016 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
Eric Andersen7e1273e2001-05-07 17:57:45 +000017 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
18 * <jam@ltsp.org>
Eric Andersen539ffc92004-02-22 12:25:47 +000019 * Modified 2004/02/11 to add ability to pass the USER variable to remote host
20 * by Fernando Silveira <swrh@gmx.net>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000021 *
Erik Andersenf7c49ef2000-02-22 17:17:45 +000022 */
23
Eric Andersen28c70b32000-06-14 20:42:57 +000024#include <arpa/telnet.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000025#include <netinet/in.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000026#include "libbb.h"
Erik Andersenf7c49ef2000-02-22 17:17:45 +000027
Pavel Roskin616d13b2000-07-28 19:38:27 +000028#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000029#define TRACE(x, y) do { if (x) printf y; } while (0)
30#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000031#define TRACE(x, y)
Eric Andersen28c70b32000-06-14 20:42:57 +000032#endif
33
Mark Whitley59ab0252001-01-23 22:30:04 +000034enum {
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000035 DATABUFSIZE = 128,
36 IACBUFSIZE = 128,
37
Rob Landleybc68cd12006-03-10 19:22:06 +000038 CHM_TRY = 0,
39 CHM_ON = 1,
40 CHM_OFF = 2,
41
42 UF_ECHO = 0x01,
43 UF_SGA = 0x02,
44
Denys Vlasenko036dbb92010-10-29 02:12:22 +020045 TS_NORMAL = 0,
46 TS_COPY = 1,
Mark Whitley59ab0252001-01-23 22:30:04 +000047 TS_IAC = 2,
48 TS_OPT = 3,
49 TS_SUB1 = 4,
50 TS_SUB2 = 5,
Denys Vlasenko036dbb92010-10-29 02:12:22 +020051 TS_CR = 6,
Mark Whitley59ab0252001-01-23 22:30:04 +000052};
Eric Andersen28c70b32000-06-14 20:42:57 +000053
Eric Andersen28c70b32000-06-14 20:42:57 +000054typedef unsigned char byte;
55
Denis Vlasenko1101d1c2008-07-21 09:22:28 +000056enum { netfd = 3 };
57
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000058struct globals {
Denis Vlasenko1101d1c2008-07-21 09:22:28 +000059 int iaclen; /* could even use byte, but it's a loss on x86 */
Eric Andersen28c70b32000-06-14 20:42:57 +000060 byte telstate; /* telnet negotiation state from network input */
61 byte telwish; /* DO, DONT, WILL, WONT */
62 byte charmode;
63 byte telflags;
Paul Foxf2ddc052005-07-20 19:55:19 +000064 byte do_termios;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000065#if ENABLE_FEATURE_TELNET_TTYPE
66 char *ttype;
67#endif
68#if ENABLE_FEATURE_TELNET_AUTOLOGIN
69 const char *autologin;
70#endif
71#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko55995022008-05-18 22:28:26 +000072 unsigned win_width, win_height;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000073#endif
74 /* same buffer used both for network and console read/write */
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +000075 char buf[DATABUFSIZE];
76 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000077 char iacbuf[IACBUFSIZE];
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078 struct termios termios_def;
79 struct termios termios_raw;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010080} FIX_ALIASING;
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000081#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko74324c82007-06-04 10:16:52 +000082#define INIT_G() do { \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +020083 struct G_sizecheck { \
84 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
85 }; \
Denis Vlasenko74324c82007-06-04 10:16:52 +000086} while (0)
Eric Andersen28c70b32000-06-14 20:42:57 +000087
Denys Vlasenko036dbb92010-10-29 02:12:22 +020088
Eric Andersencd8c4362001-11-10 11:22:46 +000089static void rawmode(void);
90static void cookmode(void);
91static void do_linemode(void);
92static void will_charmode(void);
Eric Andersen28c70b32000-06-14 20:42:57 +000093static void telopt(byte c);
Denys Vlasenko036dbb92010-10-29 02:12:22 +020094static void subneg(byte c);
Eric Andersen28c70b32000-06-14 20:42:57 +000095
Denis Vlasenko9f2f8082008-11-11 02:56:39 +000096static void iac_flush(void)
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000097{
Denis Vlasenko1101d1c2008-07-21 09:22:28 +000098 write(netfd, G.iacbuf, G.iaclen);
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000099 G.iaclen = 0;
100}
Eric Andersen7e1273e2001-05-07 17:57:45 +0000101
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000102#define write_str(fd, str) write(fd, str, sizeof(str) - 1)
Eric Andersen539ffc92004-02-22 12:25:47 +0000103
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000104static void doexit(int ev) NORETURN;
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000105static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000106{
Eric Andersen28c70b32000-06-14 20:42:57 +0000107 cookmode();
108 exit(ev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000109}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000110
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000111static void con_escape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000112{
Eric Andersen28c70b32000-06-14 20:42:57 +0000113 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000114
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000115 if (bb_got_signal) /* came from line mode... go raw */
Eric Andersen28c70b32000-06-14 20:42:57 +0000116 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000117
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000118 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
Eric Andersen28c70b32000-06-14 20:42:57 +0000119 " l go to line mode\r\n"
120 " c go to character mode\r\n"
121 " z suspend telnet\r\n"
122 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000123
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000124 if (read(STDIN_FILENO, &b, 1) <= 0)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000125 doexit(EXIT_FAILURE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000126
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000127 switch (b) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000128 case 'l':
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000129 if (!bb_got_signal) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000130 do_linemode();
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000131 goto ret;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000132 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000133 break;
134 case 'c':
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000135 if (bb_got_signal) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000136 will_charmode();
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000137 goto ret;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000138 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000139 break;
140 case 'z':
141 cookmode();
142 kill(0, SIGTSTP);
143 rawmode();
144 break;
145 case 'e':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000146 doexit(EXIT_SUCCESS);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000147 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000148
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000149 write_str(1, "continuing...\r\n");
Eric Andersen28c70b32000-06-14 20:42:57 +0000150
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000151 if (bb_got_signal)
Eric Andersen28c70b32000-06-14 20:42:57 +0000152 cookmode();
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000153 ret:
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000154 bb_got_signal = 0;
Eric Andersen28c70b32000-06-14 20:42:57 +0000155}
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000156
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000157static void handle_net_output(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000158{
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000159 /* here we could do smart tricks how to handle 0xFF:s in output
160 * stream like writing twice every sequence of FF:s (thus doing
161 * many write()s. But I think interactive telnet application does
162 * not need to be 100% 8-bit clean, so changing every 0xff:s to
163 * 0x7f:s
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000164 *
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000165 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
166 * I don't agree.
167 * first - I cannot use programs like sz/rz
168 * second - the 0x0D is sent as one character and if the next
169 * char is 0x0A then it's eaten by a server side.
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000170 * third - why do you have to make 'many write()s'?
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000171 * I don't understand.
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000172 * So I implemented it. It's really useful for me. I hope that
173 * other people will find it interesting too.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000174 */
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200175 byte outbuf[2 * DATABUFSIZE];
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000176 byte *p = (byte*)G.buf;
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200177 int j = 0;
Eric Andersen28c70b32000-06-14 20:42:57 +0000178
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200179 for (; len > 0; len--, p++) {
180 byte c = *p;
181 if (c == 0x1d) {
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000182 con_escape();
Eric Andersen28c70b32000-06-14 20:42:57 +0000183 return;
184 }
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200185 outbuf[j++] = c;
186 if (c == IAC)
187 outbuf[j++] = c; /* IAC -> IAC IAC */
188 else if (c == '\r')
189 outbuf[j++] = '\0'; /* CR -> CR NUL */
Eric Andersen28c70b32000-06-14 20:42:57 +0000190 }
Denis Vlasenko219d14d2007-03-24 15:40:16 +0000191 if (j > 0)
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200192 full_write(netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000193}
194
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000195static void handle_net_input(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000196{
Eric Andersen28c70b32000-06-14 20:42:57 +0000197 int i;
198 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000199
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000200 for (i = 0; i < len; i++) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000201 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000202
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200203 if (G.telstate == TS_NORMAL) { /* most typical state */
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000204 if (c == IAC) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000205 cstart = i;
206 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000207 }
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200208 else if (c == '\r') {
209 cstart = i + 1;
210 G.telstate = TS_CR;
211 }
212 /* No IACs were seen so far, no need to copy
213 * bytes within G.buf: */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000214 continue;
215 }
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200216
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000217 switch (G.telstate) {
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200218 case TS_CR:
219 /* Prev char was CR. If cur one is NUL, ignore it.
220 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
221 */
222 G.telstate = TS_COPY;
223 if (c == '\0')
224 break;
225 /* else: fall through - need to handle CR IAC ... properly */
226
227 case TS_COPY: /* Prev char was ordinary */
228 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000229 if (c == IAC)
230 G.telstate = TS_IAC;
231 else
232 G.buf[cstart++] = c;
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200233 if (c == '\r')
234 G.telstate = TS_CR;
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000235 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000236
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200237 case TS_IAC: /* Prev char was IAC */
238 if (c == IAC) { /* IAC IAC -> one IAC */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000239 G.buf[cstart++] = c;
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200240 G.telstate = TS_COPY;
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000241 break;
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000242 }
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000243 /* else */
244 switch (c) {
245 case SB:
246 G.telstate = TS_SUB1;
247 break;
248 case DO:
249 case DONT:
250 case WILL:
251 case WONT:
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200252 G.telwish = c;
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000253 G.telstate = TS_OPT;
254 break;
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200255 /* DATA MARK must be added later */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000256 default:
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200257 G.telstate = TS_COPY;
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000258 }
259 break;
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200260
261 case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000262 telopt(c);
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200263 G.telstate = TS_COPY;
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000264 break;
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200265
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000266 case TS_SUB1: /* Subnegotiation */
267 case TS_SUB2: /* Subnegotiation */
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200268 subneg(c); /* can change G.telstate */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000269 break;
270 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000271 }
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200272
273 if (G.telstate != TS_NORMAL) {
274 /* We had some IACs, or CR */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000275 if (G.iaclen)
276 iac_flush();
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200277 if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
278 G.telstate = TS_NORMAL;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000279 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000280 }
281
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000282 if (len)
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200283 full_write(STDOUT_FILENO, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000284}
285
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000286static void put_iac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000287{
Eric Andersen28c70b32000-06-14 20:42:57 +0000288 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000289}
290
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000291static void put_iac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000292{
Eric Andersen28c70b32000-06-14 20:42:57 +0000293 if (G.iaclen + 3 > IACBUFSIZE)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000294 iac_flush();
Eric Andersen28c70b32000-06-14 20:42:57 +0000295
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000296 put_iac(IAC);
297 put_iac(wwdd);
298 put_iac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000299}
300
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000301#if ENABLE_FEATURE_TELNET_TTYPE
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000302static void put_iac_subopt(byte c, char *str)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000303{
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000304 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
Eric Andersen7e1273e2001-05-07 17:57:45 +0000305
306 if (G.iaclen + len > IACBUFSIZE)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000307 iac_flush();
Eric Andersen7e1273e2001-05-07 17:57:45 +0000308
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000309 put_iac(IAC);
310 put_iac(SB);
311 put_iac(c);
312 put_iac(0);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000313
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000314 while (*str)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000315 put_iac(*str++);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000316
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000317 put_iac(IAC);
318 put_iac(SE);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000319}
320#endif
321
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000322#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000323static void put_iac_subopt_autologin(void)
Eric Andersen539ffc92004-02-22 12:25:47 +0000324{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000325 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
Denys Vlasenko25b10d92010-04-27 08:54:24 +0200326 const char *p = "USER";
Eric Andersen539ffc92004-02-22 12:25:47 +0000327
328 if (G.iaclen + len > IACBUFSIZE)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000329 iac_flush();
Eric Andersen539ffc92004-02-22 12:25:47 +0000330
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000331 put_iac(IAC);
332 put_iac(SB);
333 put_iac(TELOPT_NEW_ENVIRON);
334 put_iac(TELQUAL_IS);
335 put_iac(NEW_ENV_VAR);
Eric Andersen539ffc92004-02-22 12:25:47 +0000336
Denys Vlasenko25b10d92010-04-27 08:54:24 +0200337 while (*p)
338 put_iac(*p++);
Eric Andersen539ffc92004-02-22 12:25:47 +0000339
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000340 put_iac(NEW_ENV_VALUE);
Eric Andersen539ffc92004-02-22 12:25:47 +0000341
Denys Vlasenko25b10d92010-04-27 08:54:24 +0200342 p = G.autologin;
343 while (*p)
344 put_iac(*p++);
Eric Andersen539ffc92004-02-22 12:25:47 +0000345
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000346 put_iac(IAC);
347 put_iac(SE);
Eric Andersen539ffc92004-02-22 12:25:47 +0000348}
349#endif
350
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000351#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000352static void put_iac_naws(byte c, int x, int y)
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000353{
354 if (G.iaclen + 9 > IACBUFSIZE)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000355 iac_flush();
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000356
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000357 put_iac(IAC);
358 put_iac(SB);
359 put_iac(c);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000360
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000361 put_iac((x >> 8) & 0xff);
362 put_iac(x & 0xff);
363 put_iac((y >> 8) & 0xff);
364 put_iac(y & 0xff);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000365
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000366 put_iac(IAC);
367 put_iac(SE);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000368}
369#endif
370
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000371static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000372
Eric Andersencd8c4362001-11-10 11:22:46 +0000373static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000374{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000375 if (G.telflags & UF_ECHO) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000376 if (G.charmode == CHM_TRY) {
377 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000378 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000379 rawmode();
380 }
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000381 } else {
Eric Andersen28c70b32000-06-14 20:42:57 +0000382 if (G.charmode != CHM_OFF) {
383 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000384 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000385 cookmode();
386 }
387 }
388}
389
Eric Andersencd8c4362001-11-10 11:22:46 +0000390static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000391{
392 G.charmode = CHM_TRY;
393 G.telflags |= (UF_ECHO | UF_SGA);
394 setConMode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000395
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000396 put_iac2(DO, TELOPT_ECHO);
397 put_iac2(DO, TELOPT_SGA);
398 iac_flush();
Eric Andersen28c70b32000-06-14 20:42:57 +0000399}
400
Eric Andersencd8c4362001-11-10 11:22:46 +0000401static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000402{
403 G.charmode = CHM_TRY;
404 G.telflags &= ~(UF_ECHO | UF_SGA);
405 setConMode();
406
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000407 put_iac2(DONT, TELOPT_ECHO);
408 put_iac2(DONT, TELOPT_SGA);
409 iac_flush();
Eric Andersen28c70b32000-06-14 20:42:57 +0000410}
411
Rob Landley88621d72006-08-29 19:41:06 +0000412static void to_notsup(char c)
Eric Andersen28c70b32000-06-14 20:42:57 +0000413{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000414 if (G.telwish == WILL)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000415 put_iac2(DONT, c);
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000416 else if (G.telwish == DO)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000417 put_iac2(WONT, c);
Eric Andersen28c70b32000-06-14 20:42:57 +0000418}
419
Rob Landley88621d72006-08-29 19:41:06 +0000420static void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000421{
422 /* if server requests ECHO, don't agree */
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000423 if (G.telwish == DO) {
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000424 put_iac2(WONT, TELOPT_ECHO);
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000425 return;
426 }
427 if (G.telwish == DONT)
428 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000429
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000430 if (G.telflags & UF_ECHO) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000431 if (G.telwish == WILL)
432 return;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000433 } else if (G.telwish == WONT)
434 return;
Eric Andersen28c70b32000-06-14 20:42:57 +0000435
436 if (G.charmode != CHM_OFF)
437 G.telflags ^= UF_ECHO;
438
439 if (G.telflags & UF_ECHO)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000440 put_iac2(DO, TELOPT_ECHO);
Eric Andersen28c70b32000-06-14 20:42:57 +0000441 else
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000442 put_iac2(DONT, TELOPT_ECHO);
Eric Andersen28c70b32000-06-14 20:42:57 +0000443
444 setConMode();
Denys Vlasenko729ecb82010-06-07 14:14:26 +0200445 full_write1_str("\r\n"); /* sudden modec */
Eric Andersen28c70b32000-06-14 20:42:57 +0000446}
447
Rob Landley88621d72006-08-29 19:41:06 +0000448static void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000449{
450 /* daemon always sends will/wont, client do/dont */
451
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000452 if (G.telflags & UF_SGA) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000453 if (G.telwish == WILL)
454 return;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000455 } else if (G.telwish == WONT)
456 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000457
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000458 G.telflags ^= UF_SGA; /* toggle */
459 if (G.telflags & UF_SGA)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000460 put_iac2(DO, TELOPT_SGA);
Eric Andersen28c70b32000-06-14 20:42:57 +0000461 else
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000462 put_iac2(DONT, TELOPT_SGA);
Eric Andersen28c70b32000-06-14 20:42:57 +0000463}
464
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000465#if ENABLE_FEATURE_TELNET_TTYPE
Rob Landley88621d72006-08-29 19:41:06 +0000466static void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000467{
468 /* Tell server we will (or won't) do TTYPE */
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000469 if (G.ttype)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000470 put_iac2(WILL, TELOPT_TTYPE);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000471 else
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000472 put_iac2(WONT, TELOPT_TTYPE);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000473}
474#endif
475
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000476#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Rob Landley88621d72006-08-29 19:41:06 +0000477static void to_new_environ(void)
Eric Andersen539ffc92004-02-22 12:25:47 +0000478{
479 /* Tell server we will (or will not) do AUTOLOGIN */
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000480 if (G.autologin)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000481 put_iac2(WILL, TELOPT_NEW_ENVIRON);
Eric Andersen539ffc92004-02-22 12:25:47 +0000482 else
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000483 put_iac2(WONT, TELOPT_NEW_ENVIRON);
Eric Andersen539ffc92004-02-22 12:25:47 +0000484}
485#endif
486
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000487#if ENABLE_FEATURE_AUTOWIDTH
Rob Landley88621d72006-08-29 19:41:06 +0000488static void to_naws(void)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000489{
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000490 /* Tell server we will do NAWS */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000491 put_iac2(WILL, TELOPT_NAWS);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000492}
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000493#endif
494
Eric Andersen28c70b32000-06-14 20:42:57 +0000495static void telopt(byte c)
496{
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000497 switch (c) {
498 case TELOPT_ECHO:
499 to_echo(); break;
500 case TELOPT_SGA:
501 to_sga(); break;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000502#if ENABLE_FEATURE_TELNET_TTYPE
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000503 case TELOPT_TTYPE:
504 to_ttype(); break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000505#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000506#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000507 case TELOPT_NEW_ENVIRON:
508 to_new_environ(); break;
Eric Andersen539ffc92004-02-22 12:25:47 +0000509#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000510#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000511 case TELOPT_NAWS:
512 to_naws();
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000513 put_iac_naws(c, G.win_width, G.win_height);
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000514 break;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000515#endif
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000516 default:
517 to_notsup(c);
518 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000519 }
520}
521
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000522/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200523static void subneg(byte c)
Eric Andersen28c70b32000-06-14 20:42:57 +0000524{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000525 switch (G.telstate) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000526 case TS_SUB1:
527 if (c == IAC)
528 G.telstate = TS_SUB2;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000529#if ENABLE_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000530 else
Denys Vlasenko25b10d92010-04-27 08:54:24 +0200531 if (c == TELOPT_TTYPE && G.ttype)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000532 put_iac_subopt(TELOPT_TTYPE, G.ttype);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000533#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000534#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Eric Andersen539ffc92004-02-22 12:25:47 +0000535 else
Denys Vlasenko25b10d92010-04-27 08:54:24 +0200536 if (c == TELOPT_NEW_ENVIRON && G.autologin)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000537 put_iac_subopt_autologin();
Eric Andersen539ffc92004-02-22 12:25:47 +0000538#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000539 break;
540 case TS_SUB2:
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200541 if (c == SE) {
542 G.telstate = TS_COPY;
543 return;
544 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000545 G.telstate = TS_SUB1;
Denys Vlasenko036dbb92010-10-29 02:12:22 +0200546 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000547 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000548}
549
Eric Andersencd8c4362001-11-10 11:22:46 +0000550static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000551{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000552 if (G.do_termios)
553 tcsetattr(0, TCSADRAIN, &G.termios_raw);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000554}
Eric Andersen28c70b32000-06-14 20:42:57 +0000555
Eric Andersencd8c4362001-11-10 11:22:46 +0000556static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000557{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000558 if (G.do_termios)
559 tcsetattr(0, TCSADRAIN, &G.termios_def);
Eric Andersen28c70b32000-06-14 20:42:57 +0000560}
561
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000562int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000563int telnet_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen28c70b32000-06-14 20:42:57 +0000564{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000565 char *host;
566 int port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000567 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000568 struct pollfd ufds[2];
Eric Andersen28c70b32000-06-14 20:42:57 +0000569
Denis Vlasenko74324c82007-06-04 10:16:52 +0000570 INIT_G();
Eric Andersen28c70b32000-06-14 20:42:57 +0000571
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000572#if ENABLE_FEATURE_AUTOWIDTH
573 get_terminal_width_height(0, &G.win_width, &G.win_height);
574#endif
575
576#if ENABLE_FEATURE_TELNET_TTYPE
577 G.ttype = getenv("TERM");
578#endif
579
Paul Foxf2ddc052005-07-20 19:55:19 +0000580 if (tcgetattr(0, &G.termios_def) >= 0) {
581 G.do_termios = 1;
Paul Foxf2ddc052005-07-20 19:55:19 +0000582 G.termios_raw = G.termios_def;
583 cfmakeraw(&G.termios_raw);
584 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000585
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000586#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000587 if (1 & getopt32(argv, "al:", &G.autologin))
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000588 G.autologin = getenv("USER");
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000589 argv += optind;
Eric Andersen539ffc92004-02-22 12:25:47 +0000590#else
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000591 argv++;
Eric Andersen539ffc92004-02-22 12:25:47 +0000592#endif
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000593 if (!*argv)
594 bb_show_usage();
595 host = *argv++;
596 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
597 if (*argv) /* extra params?? */
598 bb_show_usage();
599
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000600 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
Eric Andersen28c70b32000-06-14 20:42:57 +0000601
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000602 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
Eric Andersen28c70b32000-06-14 20:42:57 +0000603
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000604 signal(SIGINT, record_signo);
Eric Andersen28c70b32000-06-14 20:42:57 +0000605
Denys Vlasenkoec074202010-10-29 02:33:38 +0200606 ufds[0].fd = STDIN_FILENO;
607 ufds[0].events = POLLIN;
608 ufds[1].fd = netfd;
609 ufds[1].events = POLLIN;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000610
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000611 while (1) {
Denys Vlasenkoec074202010-10-29 02:33:38 +0200612 if (poll(ufds, 2, -1) < 0) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000613 /* error, ignore and/or log something, bay go to loop */
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000614 if (bb_got_signal)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000615 con_escape();
Eric Andersen28c70b32000-06-14 20:42:57 +0000616 else
617 sleep(1);
Denys Vlasenkoec074202010-10-29 02:33:38 +0200618 continue;
619 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000620
Denys Vlasenkoec074202010-10-29 02:33:38 +0200621// FIXME: reads can block. Need full bidirectional buffering.
Eric Andersen28c70b32000-06-14 20:42:57 +0000622
Denys Vlasenkoec074202010-10-29 02:33:38 +0200623 if (ufds[0].revents) {
624 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
625 if (len <= 0)
626 doexit(EXIT_SUCCESS);
627 TRACE(0, ("Read con: %d\n", len));
628 handle_net_output(len);
629 }
630
631 if (ufds[1].revents) {
632 len = safe_read(netfd, G.buf, DATABUFSIZE);
633 if (len <= 0) {
634 full_write1_str("Connection closed by foreign host\r\n");
635 doexit(EXIT_FAILURE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000636 }
Denys Vlasenkoec074202010-10-29 02:33:38 +0200637 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
638 handle_net_input(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000639 }
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000640 } /* while (1) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000641}