blob: 63ee3808851ca1bd7a93a4ae637d3554721b4c45 [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 *
Rob Landley1b751c82005-10-28 09:24:33 +000011 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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
Erik Andersenf7c49ef2000-02-22 17:17:45 +000024#include <termios.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000025#include <arpa/telnet.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000026#include <netinet/in.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027#include "libbb.h"
Erik Andersenf7c49ef2000-02-22 17:17:45 +000028
Pavel Roskin616d13b2000-07-28 19:38:27 +000029#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000030#define TRACE(x, y) do { if (x) printf y; } while (0)
31#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000032#define TRACE(x, y)
Eric Andersen28c70b32000-06-14 20:42:57 +000033#endif
34
Mark Whitley59ab0252001-01-23 22:30:04 +000035enum {
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000036 DATABUFSIZE = 128,
37 IACBUFSIZE = 128,
38
Rob Landleybc68cd12006-03-10 19:22:06 +000039 CHM_TRY = 0,
40 CHM_ON = 1,
41 CHM_OFF = 2,
42
43 UF_ECHO = 0x01,
44 UF_SGA = 0x02,
45
Mark Whitley59ab0252001-01-23 22:30:04 +000046 TS_0 = 1,
47 TS_IAC = 2,
48 TS_OPT = 3,
49 TS_SUB1 = 4,
50 TS_SUB2 = 5,
51};
Eric Andersen28c70b32000-06-14 20:42:57 +000052
Eric Andersen28c70b32000-06-14 20:42:57 +000053typedef unsigned char byte;
54
Denis Vlasenko1101d1c2008-07-21 09:22:28 +000055enum { netfd = 3 };
56
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000057struct globals {
Denis Vlasenko1101d1c2008-07-21 09:22:28 +000058 int iaclen; /* could even use byte, but it's a loss on x86 */
Eric Andersen28c70b32000-06-14 20:42:57 +000059 byte telstate; /* telnet negotiation state from network input */
60 byte telwish; /* DO, DONT, WILL, WONT */
61 byte charmode;
62 byte telflags;
63 byte gotsig;
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;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000080};
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000081#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko74324c82007-06-04 10:16:52 +000082void BUG_telnet_globals_too_big(void);
83#define INIT_G() do { \
84 if (sizeof(G) > COMMON_BUFSIZE) \
85 BUG_telnet_globals_too_big(); \
86 /* memset(&G, 0, sizeof G); - already is */ \
87} while (0)
Eric Andersen28c70b32000-06-14 20:42:57 +000088
89/* Function prototypes */
Eric Andersencd8c4362001-11-10 11:22:46 +000090static void rawmode(void);
91static void cookmode(void);
92static void do_linemode(void);
93static void will_charmode(void);
Eric Andersen28c70b32000-06-14 20:42:57 +000094static void telopt(byte c);
95static int subneg(byte c);
Eric Andersen28c70b32000-06-14 20:42:57 +000096
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000097static void iacflush(void)
98{
Denis Vlasenko1101d1c2008-07-21 09:22:28 +000099 write(netfd, G.iacbuf, G.iaclen);
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000100 G.iaclen = 0;
101}
Eric Andersen7e1273e2001-05-07 17:57:45 +0000102
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000103#define write_str(fd, str) write(fd, str, sizeof(str) - 1)
Eric Andersen539ffc92004-02-22 12:25:47 +0000104
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000105static void doexit(int ev) NORETURN;
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000106static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000107{
Eric Andersen28c70b32000-06-14 20:42:57 +0000108 cookmode();
109 exit(ev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000110}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000111
Eric Andersencd8c4362001-11-10 11:22:46 +0000112static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000113{
Eric Andersen28c70b32000-06-14 20:42:57 +0000114 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000115
Eric Andersen28c70b32000-06-14 20:42:57 +0000116 if (G.gotsig) /* came from line mode... go raw */
117 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000118
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000119 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
Eric Andersen28c70b32000-06-14 20:42:57 +0000120 " l go to line mode\r\n"
121 " c go to character mode\r\n"
122 " z suspend telnet\r\n"
123 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000124
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000125 if (read(STDIN_FILENO, &b, 1) <= 0)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000126 doexit(EXIT_FAILURE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000127
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000128 switch (b) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000129 case 'l':
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000130 if (!G.gotsig) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000131 do_linemode();
132 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000133 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000134 break;
135 case 'c':
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000136 if (G.gotsig) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000137 will_charmode();
138 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000139 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000140 break;
141 case 'z':
142 cookmode();
143 kill(0, SIGTSTP);
144 rawmode();
145 break;
146 case 'e':
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000147 doexit(EXIT_SUCCESS);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000148 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000149
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000150 write_str(1, "continuing...\r\n");
Eric Andersen28c70b32000-06-14 20:42:57 +0000151
152 if (G.gotsig)
153 cookmode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000154
Eric Andersen28c70b32000-06-14 20:42:57 +0000155 rrturn:
156 G.gotsig = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000157
Eric Andersen28c70b32000-06-14 20:42:57 +0000158}
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000159
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000160static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000161{
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000162 /* here we could do smart tricks how to handle 0xFF:s in output
163 * stream like writing twice every sequence of FF:s (thus doing
164 * many write()s. But I think interactive telnet application does
165 * not need to be 100% 8-bit clean, so changing every 0xff:s to
166 * 0x7f:s
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000167 *
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000168 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
169 * I don't agree.
170 * first - I cannot use programs like sz/rz
171 * second - the 0x0D is sent as one character and if the next
172 * char is 0x0A then it's eaten by a server side.
173 * third - whay doy you have to make 'many write()s'?
174 * I don't understand.
175 * So I implemented it. It's realy useful for me. I hope that
176 * others people will find it interesting too.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000177 */
Eric Andersen28c70b32000-06-14 20:42:57 +0000178
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000179 int i, j;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000180 byte * p = (byte*)G.buf;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000181 byte outbuf[4*DATABUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000182
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000183 for (i = len, j = 0; i > 0; i--, p++) {
184 if (*p == 0x1d) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000185 conescape();
186 return;
187 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000188 outbuf[j++] = *p;
Eric Andersen28c70b32000-06-14 20:42:57 +0000189 if (*p == 0xff)
Denis Vlasenko92758142006-10-03 19:56:34 +0000190 outbuf[j++] = 0xff;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000191 else if (*p == 0x0d)
Denis Vlasenko92758142006-10-03 19:56:34 +0000192 outbuf[j++] = 0x00;
Eric Andersen28c70b32000-06-14 20:42:57 +0000193 }
Denis Vlasenko219d14d2007-03-24 15:40:16 +0000194 if (j > 0)
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000195 write(netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000196}
197
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000198static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000199{
Eric Andersen28c70b32000-06-14 20:42:57 +0000200 int i;
201 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000202
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000203 for (i = 0; i < len; i++) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000204 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000205
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000206 if (G.telstate == 0) { /* most of the time state == 0 */
207 if (c == IAC) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000208 cstart = i;
209 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000210 }
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000211 } else
212 switch (G.telstate) {
213 case TS_0:
214 if (c == IAC)
215 G.telstate = TS_IAC;
216 else
217 G.buf[cstart++] = c;
218 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000219
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000220 case TS_IAC:
221 if (c == IAC) { /* IAC IAC -> 0xFF */
222 G.buf[cstart++] = c;
223 G.telstate = TS_0;
224 break;
225 }
226 /* else */
227 switch (c) {
228 case SB:
229 G.telstate = TS_SUB1;
230 break;
231 case DO:
232 case DONT:
233 case WILL:
234 case WONT:
235 G.telwish = c;
236 G.telstate = TS_OPT;
237 break;
238 default:
239 G.telstate = TS_0; /* DATA MARK must be added later */
240 }
241 break;
242 case TS_OPT: /* WILL, WONT, DO, DONT */
243 telopt(c);
244 G.telstate = TS_0;
245 break;
246 case TS_SUB1: /* Subnegotiation */
247 case TS_SUB2: /* Subnegotiation */
248 if (subneg(c))
249 G.telstate = TS_0;
250 break;
251 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000252 }
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000253 if (G.telstate) {
254 if (G.iaclen) iacflush();
Eric Andersen28c70b32000-06-14 20:42:57 +0000255 if (G.telstate == TS_0) G.telstate = 0;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000256 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000257 }
258
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000259 if (len)
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000260 write(STDOUT_FILENO, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000261}
262
Rob Landley88621d72006-08-29 19:41:06 +0000263static void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000264{
Eric Andersen28c70b32000-06-14 20:42:57 +0000265 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000266}
267
Eric Andersen28c70b32000-06-14 20:42:57 +0000268static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000269{
Eric Andersen28c70b32000-06-14 20:42:57 +0000270 if (G.iaclen + 3 > IACBUFSIZE)
271 iacflush();
272
273 putiac(IAC);
274 putiac(wwdd);
275 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000276}
277
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000278#if ENABLE_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000279static void putiac_subopt(byte c, char *str)
280{
281 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
282
283 if (G.iaclen + len > IACBUFSIZE)
284 iacflush();
285
286 putiac(IAC);
287 putiac(SB);
288 putiac(c);
289 putiac(0);
290
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000291 while (*str)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000292 putiac(*str++);
293
294 putiac(IAC);
295 putiac(SE);
296}
297#endif
298
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000299#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Eric Andersen539ffc92004-02-22 12:25:47 +0000300static void putiac_subopt_autologin(void)
301{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000302 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000303 const char *user = "USER";
Eric Andersen539ffc92004-02-22 12:25:47 +0000304
305 if (G.iaclen + len > IACBUFSIZE)
306 iacflush();
307
308 putiac(IAC);
309 putiac(SB);
310 putiac(TELOPT_NEW_ENVIRON);
311 putiac(TELQUAL_IS);
312 putiac(NEW_ENV_VAR);
313
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000314 while (*user)
Eric Andersen539ffc92004-02-22 12:25:47 +0000315 putiac(*user++);
316
317 putiac(NEW_ENV_VALUE);
318
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000319 while (*G.autologin)
320 putiac(*G.autologin++);
Eric Andersen539ffc92004-02-22 12:25:47 +0000321
322 putiac(IAC);
323 putiac(SE);
324}
325#endif
326
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000327#if ENABLE_FEATURE_AUTOWIDTH
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000328static void putiac_naws(byte c, int x, int y)
329{
330 if (G.iaclen + 9 > IACBUFSIZE)
331 iacflush();
332
333 putiac(IAC);
334 putiac(SB);
335 putiac(c);
336
337 putiac((x >> 8) & 0xff);
338 putiac(x & 0xff);
339 putiac((y >> 8) & 0xff);
340 putiac(y & 0xff);
341
342 putiac(IAC);
343 putiac(SE);
344}
345#endif
346
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000347static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000348
Eric Andersencd8c4362001-11-10 11:22:46 +0000349static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000350{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000351 if (G.telflags & UF_ECHO) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000352 if (G.charmode == CHM_TRY) {
353 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000354 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000355 rawmode();
356 }
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000357 } else {
Eric Andersen28c70b32000-06-14 20:42:57 +0000358 if (G.charmode != CHM_OFF) {
359 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000360 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000361 cookmode();
362 }
363 }
364}
365
Eric Andersencd8c4362001-11-10 11:22:46 +0000366static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000367{
368 G.charmode = CHM_TRY;
369 G.telflags |= (UF_ECHO | UF_SGA);
370 setConMode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000371
Eric Andersen28c70b32000-06-14 20:42:57 +0000372 putiac2(DO, TELOPT_ECHO);
373 putiac2(DO, TELOPT_SGA);
374 iacflush();
375}
376
Eric Andersencd8c4362001-11-10 11:22:46 +0000377static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000378{
379 G.charmode = CHM_TRY;
380 G.telflags &= ~(UF_ECHO | UF_SGA);
381 setConMode();
382
383 putiac2(DONT, TELOPT_ECHO);
384 putiac2(DONT, TELOPT_SGA);
385 iacflush();
386}
387
Rob Landley88621d72006-08-29 19:41:06 +0000388static void to_notsup(char c)
Eric Andersen28c70b32000-06-14 20:42:57 +0000389{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000390 if (G.telwish == WILL)
391 putiac2(DONT, c);
392 else if (G.telwish == DO)
393 putiac2(WONT, c);
Eric Andersen28c70b32000-06-14 20:42:57 +0000394}
395
Rob Landley88621d72006-08-29 19:41:06 +0000396static void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000397{
398 /* if server requests ECHO, don't agree */
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000399 if (G.telwish == DO) {
400 putiac2(WONT, TELOPT_ECHO);
401 return;
402 }
403 if (G.telwish == DONT)
404 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000405
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000406 if (G.telflags & UF_ECHO) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000407 if (G.telwish == WILL)
408 return;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000409 } else if (G.telwish == WONT)
410 return;
Eric Andersen28c70b32000-06-14 20:42:57 +0000411
412 if (G.charmode != CHM_OFF)
413 G.telflags ^= UF_ECHO;
414
415 if (G.telflags & UF_ECHO)
416 putiac2(DO, TELOPT_ECHO);
417 else
418 putiac2(DONT, TELOPT_ECHO);
419
420 setConMode();
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000421 write_str(1, "\r\n"); /* sudden modec */
Eric Andersen28c70b32000-06-14 20:42:57 +0000422}
423
Rob Landley88621d72006-08-29 19:41:06 +0000424static void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000425{
426 /* daemon always sends will/wont, client do/dont */
427
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000428 if (G.telflags & UF_SGA) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000429 if (G.telwish == WILL)
430 return;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000431 } else if (G.telwish == WONT)
432 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000433
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000434 G.telflags ^= UF_SGA; /* toggle */
435 if (G.telflags & UF_SGA)
Eric Andersen28c70b32000-06-14 20:42:57 +0000436 putiac2(DO, TELOPT_SGA);
437 else
438 putiac2(DONT, TELOPT_SGA);
Eric Andersen28c70b32000-06-14 20:42:57 +0000439}
440
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000441#if ENABLE_FEATURE_TELNET_TTYPE
Rob Landley88621d72006-08-29 19:41:06 +0000442static void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000443{
444 /* Tell server we will (or won't) do TTYPE */
445
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000446 if (G.ttype)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000447 putiac2(WILL, TELOPT_TTYPE);
448 else
449 putiac2(WONT, TELOPT_TTYPE);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000450}
451#endif
452
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000453#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Rob Landley88621d72006-08-29 19:41:06 +0000454static void to_new_environ(void)
Eric Andersen539ffc92004-02-22 12:25:47 +0000455{
456 /* Tell server we will (or will not) do AUTOLOGIN */
457
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000458 if (G.autologin)
Eric Andersen539ffc92004-02-22 12:25:47 +0000459 putiac2(WILL, TELOPT_NEW_ENVIRON);
460 else
461 putiac2(WONT, TELOPT_NEW_ENVIRON);
Eric Andersen539ffc92004-02-22 12:25:47 +0000462}
463#endif
464
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000465#if ENABLE_FEATURE_AUTOWIDTH
Rob Landley88621d72006-08-29 19:41:06 +0000466static void to_naws(void)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000467{
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000468 /* Tell server we will do NAWS */
469 putiac2(WILL, TELOPT_NAWS);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000470}
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000471#endif
472
Eric Andersen28c70b32000-06-14 20:42:57 +0000473static void telopt(byte c)
474{
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000475 switch (c) {
476 case TELOPT_ECHO:
477 to_echo(); break;
478 case TELOPT_SGA:
479 to_sga(); break;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000480#if ENABLE_FEATURE_TELNET_TTYPE
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000481 case TELOPT_TTYPE:
482 to_ttype(); break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000483#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000484#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000485 case TELOPT_NEW_ENVIRON:
486 to_new_environ(); break;
Eric Andersen539ffc92004-02-22 12:25:47 +0000487#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000488#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000489 case TELOPT_NAWS:
490 to_naws();
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000491 putiac_naws(c, G.win_width, G.win_height);
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000492 break;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000493#endif
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000494 default:
495 to_notsup(c);
496 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000497 }
498}
499
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000500/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000501static int subneg(byte c)
502{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000503 switch (G.telstate) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000504 case TS_SUB1:
505 if (c == IAC)
506 G.telstate = TS_SUB2;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000507#if ENABLE_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000508 else
509 if (c == TELOPT_TTYPE)
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000510 putiac_subopt(TELOPT_TTYPE, G.ttype);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000511#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000512#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Eric Andersen539ffc92004-02-22 12:25:47 +0000513 else
514 if (c == TELOPT_NEW_ENVIRON)
515 putiac_subopt_autologin();
516#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000517 break;
518 case TS_SUB2:
519 if (c == SE)
520 return TRUE;
521 G.telstate = TS_SUB1;
522 /* break; */
523 }
524 return FALSE;
525}
526
Eric Andersen28c70b32000-06-14 20:42:57 +0000527static void fgotsig(int sig)
528{
529 G.gotsig = sig;
530}
531
532
Eric Andersencd8c4362001-11-10 11:22:46 +0000533static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000534{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000535 if (G.do_termios)
536 tcsetattr(0, TCSADRAIN, &G.termios_raw);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000537}
Eric Andersen28c70b32000-06-14 20:42:57 +0000538
Eric Andersencd8c4362001-11-10 11:22:46 +0000539static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000540{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000541 if (G.do_termios)
542 tcsetattr(0, TCSADRAIN, &G.termios_def);
Eric Andersen28c70b32000-06-14 20:42:57 +0000543}
544
Denis Vlasenko1bec1b92007-11-06 02:23:39 +0000545/* poll gives smaller (-70 bytes) code */
546#define USE_POLL 1
547
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000548int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000549int telnet_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen28c70b32000-06-14 20:42:57 +0000550{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000551 char *host;
552 int port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000553 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000554#ifdef USE_POLL
555 struct pollfd ufds[2];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000556#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000557 fd_set readfds;
558 int maxfd;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000559#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000560
Denis Vlasenko74324c82007-06-04 10:16:52 +0000561 INIT_G();
Eric Andersen28c70b32000-06-14 20:42:57 +0000562
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000563#if ENABLE_FEATURE_AUTOWIDTH
564 get_terminal_width_height(0, &G.win_width, &G.win_height);
565#endif
566
567#if ENABLE_FEATURE_TELNET_TTYPE
568 G.ttype = getenv("TERM");
569#endif
570
Paul Foxf2ddc052005-07-20 19:55:19 +0000571 if (tcgetattr(0, &G.termios_def) >= 0) {
572 G.do_termios = 1;
Paul Foxf2ddc052005-07-20 19:55:19 +0000573 G.termios_raw = G.termios_def;
574 cfmakeraw(&G.termios_raw);
575 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000576
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000577#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000578 if (1 & getopt32(argv, "al:", &G.autologin))
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000579 G.autologin = getenv("USER");
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000580 argv += optind;
Eric Andersen539ffc92004-02-22 12:25:47 +0000581#else
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000582 argv++;
Eric Andersen539ffc92004-02-22 12:25:47 +0000583#endif
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000584 if (!*argv)
585 bb_show_usage();
586 host = *argv++;
587 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
588 if (*argv) /* extra params?? */
589 bb_show_usage();
590
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000591 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
Eric Andersen28c70b32000-06-14 20:42:57 +0000592
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000593 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
Eric Andersen28c70b32000-06-14 20:42:57 +0000594
595 signal(SIGINT, fgotsig);
596
597#ifdef USE_POLL
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000598 ufds[0].fd = 0; ufds[1].fd = netfd;
Eric Andersen28c70b32000-06-14 20:42:57 +0000599 ufds[0].events = ufds[1].events = POLLIN;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000600#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000601 FD_ZERO(&readfds);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000602 FD_SET(STDIN_FILENO, &readfds);
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000603 FD_SET(netfd, &readfds);
604 maxfd = netfd + 1;
Eric Andersen28c70b32000-06-14 20:42:57 +0000605#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000606
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000607 while (1) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000608#ifndef USE_POLL
609 fd_set rfds = readfds;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000610
Eric Andersen28c70b32000-06-14 20:42:57 +0000611 switch (select(maxfd, &rfds, NULL, NULL, NULL))
612#else
613 switch (poll(ufds, 2, -1))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000614#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000615 {
616 case 0:
617 /* timeout */
618 case -1:
619 /* error, ignore and/or log something, bay go to loop */
620 if (G.gotsig)
621 conescape();
622 else
623 sleep(1);
624 break;
625 default:
626
627#ifdef USE_POLL
628 if (ufds[0].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000629#else
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000630 if (FD_ISSET(STDIN_FILENO, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000631#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000632 {
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000633 len = read(STDIN_FILENO, G.buf, DATABUFSIZE);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000634 if (len <= 0)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000635 doexit(EXIT_SUCCESS);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000636 TRACE(0, ("Read con: %d\n", len));
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000637 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000638 }
639
640#ifdef USE_POLL
641 if (ufds[1].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000642#else
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000643 if (FD_ISSET(netfd, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000644#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000645 {
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000646 len = read(netfd, G.buf, DATABUFSIZE);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000647 if (len <= 0) {
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000648 write_str(1, "Connection closed by foreign host\r\n");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000649 doexit(EXIT_FAILURE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000650 }
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000651 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000652 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000653 }
654 }
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000655 } /* while (1) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000656}