blob: 1341572732a7928b222211bef5c682050dd4bda4 [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>
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.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
Glenn L McGrath78b0e372001-06-26 02:06:08 +000035#define DATABUFSIZE 128
36#define IACBUFSIZE 128
Eric Andersen28c70b32000-06-14 20:42:57 +000037
Mark Whitley59ab0252001-01-23 22:30:04 +000038enum {
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
53#define WriteCS(fd, str) write(fd, str, sizeof str -1)
54
55typedef unsigned char byte;
56
57/* use globals to reduce size ??? */ /* test this hypothesis later */
Eric Andersen92d23242001-03-19 23:49:41 +000058static struct Globalvars {
Eric Andersen28c70b32000-06-14 20:42:57 +000059 int netfd; /* console fd:s are 0 and 1 (and 2) */
60 /* same buffer used both for network and console read/write */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000061 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
Eric Andersen28c70b32000-06-14 20:42:57 +000062 byte telstate; /* telnet negotiation state from network input */
63 byte telwish; /* DO, DONT, WILL, WONT */
64 byte charmode;
65 byte telflags;
66 byte gotsig;
Paul Foxf2ddc052005-07-20 19:55:19 +000067 byte do_termios;
Eric Andersen28c70b32000-06-14 20:42:57 +000068 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000069 char iacbuf[IACBUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +000070 short iaclen; /* could even use byte */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000071 struct termios termios_def;
72 struct termios termios_raw;
Eric Andersen28c70b32000-06-14 20:42:57 +000073} G;
74
75#define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
76
77#ifdef USE_GLOBALVAR_PTR
78struct Globalvars * Gptr;
79#define G (*Gptr)
Eric Andersen28c70b32000-06-14 20:42:57 +000080#endif
81
Rob Landley88621d72006-08-29 19:41:06 +000082static void iacflush(void)
Eric Andersen28c70b32000-06-14 20:42:57 +000083{
84 write(G.netfd, G.iacbuf, G.iaclen);
85 G.iaclen = 0;
86}
87
88/* Function prototypes */
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);
94static int subneg(byte c);
Eric Andersen28c70b32000-06-14 20:42:57 +000095
96/* Some globals */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000097#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +000098static char *ttype;
99#endif
100
Eric Andersen539ffc92004-02-22 12:25:47 +0000101#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000102static const char *autologin;
Eric Andersen539ffc92004-02-22 12:25:47 +0000103#endif
104
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000105#ifdef CONFIG_FEATURE_AUTOWIDTH
106static int win_width, win_height;
107#endif
108
Eric Andersen28c70b32000-06-14 20:42:57 +0000109static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000110{
Eric Andersen28c70b32000-06-14 20:42:57 +0000111 cookmode();
112 exit(ev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000113}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000114
Eric Andersencd8c4362001-11-10 11:22:46 +0000115static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000116{
Eric Andersen28c70b32000-06-14 20:42:57 +0000117 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000118
Eric Andersen28c70b32000-06-14 20:42:57 +0000119 if (G.gotsig) /* came from line mode... go raw */
120 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000121
Eric Andersen28c70b32000-06-14 20:42:57 +0000122 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
123 " l go to line mode\r\n"
124 " c go to character mode\r\n"
125 " z suspend telnet\r\n"
126 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000127
Eric Andersen28c70b32000-06-14 20:42:57 +0000128 if (read(0, &b, 1) <= 0)
129 doexit(1);
130
131 switch (b)
132 {
133 case 'l':
134 if (!G.gotsig)
135 {
136 do_linemode();
137 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000138 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000139 break;
140 case 'c':
141 if (G.gotsig)
142 {
143 will_charmode();
144 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000145 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000146 break;
147 case 'z':
148 cookmode();
149 kill(0, SIGTSTP);
150 rawmode();
151 break;
152 case 'e':
153 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000154 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000155
156 WriteCS(1, "continuing...\r\n");
157
158 if (G.gotsig)
159 cookmode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000160
Eric Andersen28c70b32000-06-14 20:42:57 +0000161 rrturn:
162 G.gotsig = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000163
Eric Andersen28c70b32000-06-14 20:42:57 +0000164}
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000165static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000166{
167 /* here we could do smart tricks how to handle 0xFF:s in output
168 * stream like writing twice every sequence of FF:s (thus doing
169 * many write()s. But I think interactive telnet application does
170 * not need to be 100% 8-bit clean, so changing every 0xff:s to
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000171 * 0x7f:s
172 *
173 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
174 * I don't agree.
175 * first - I cannot use programs like sz/rz
176 * second - the 0x0D is sent as one character and if the next
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000177 * char is 0x0A then it's eaten by a server side.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000178 * third - whay doy you have to make 'many write()s'?
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000179 * I don't understand.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000180 * So I implemented it. It's realy useful for me. I hope that
181 * others people will find it interesting to.
182 */
Eric Andersen28c70b32000-06-14 20:42:57 +0000183
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000184 int i, j;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000185 byte * p = (byte*)G.buf;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000186 byte outbuf[4*DATABUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000187
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000188 for (i = len, j = 0; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000189 {
190 if (*p == 0x1d)
191 {
192 conescape();
193 return;
194 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000195 outbuf[j++] = *p;
Eric Andersen28c70b32000-06-14 20:42:57 +0000196 if (*p == 0xff)
Denis Vlasenko92758142006-10-03 19:56:34 +0000197 outbuf[j++] = 0xff;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000198 else if (*p == 0x0d)
Denis Vlasenko92758142006-10-03 19:56:34 +0000199 outbuf[j++] = 0x00;
Eric Andersen28c70b32000-06-14 20:42:57 +0000200 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000201 if (j > 0 )
Denis Vlasenko92758142006-10-03 19:56:34 +0000202 write(G.netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000203}
204
Eric Andersen28c70b32000-06-14 20:42:57 +0000205
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000206static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000207{
Eric Andersen28c70b32000-06-14 20:42:57 +0000208 int i;
209 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000210
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000211 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000212 {
213 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000214
Eric Andersen28c70b32000-06-14 20:42:57 +0000215 if (G.telstate == 0) /* most of the time state == 0 */
216 {
217 if (c == IAC)
218 {
219 cstart = i;
220 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000221 }
222 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000223 else
224 switch (G.telstate)
225 {
226 case TS_0:
227 if (c == IAC)
228 G.telstate = TS_IAC;
229 else
230 G.buf[cstart++] = c;
231 break;
232
233 case TS_IAC:
234 if (c == IAC) /* IAC IAC -> 0xFF */
235 {
236 G.buf[cstart++] = c;
237 G.telstate = TS_0;
238 break;
239 }
240 /* else */
241 switch (c)
242 {
243 case SB:
244 G.telstate = TS_SUB1;
245 break;
246 case DO:
247 case DONT:
248 case WILL:
249 case WONT:
250 G.telwish = c;
251 G.telstate = TS_OPT;
252 break;
253 default:
254 G.telstate = TS_0; /* DATA MARK must be added later */
255 }
256 break;
257 case TS_OPT: /* WILL, WONT, DO, DONT */
258 telopt(c);
259 G.telstate = TS_0;
260 break;
261 case TS_SUB1: /* Subnegotiation */
262 case TS_SUB2: /* Subnegotiation */
Matt Kraai1f0c4362001-12-20 23:13:26 +0000263 if (subneg(c))
Eric Andersen28c70b32000-06-14 20:42:57 +0000264 G.telstate = TS_0;
265 break;
266 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000267 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000268 if (G.telstate)
269 {
270 if (G.iaclen) iacflush();
271 if (G.telstate == TS_0) G.telstate = 0;
272
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000273 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000274 }
275
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000276 if (len)
277 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000278}
279
Eric Andersen28c70b32000-06-14 20:42:57 +0000280
281/* ******************************* */
282
Rob Landley88621d72006-08-29 19:41:06 +0000283static void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000284{
Eric Andersen28c70b32000-06-14 20:42:57 +0000285 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000286}
287
Eric Andersen28c70b32000-06-14 20:42:57 +0000288
289static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000290{
Eric Andersen28c70b32000-06-14 20:42:57 +0000291 if (G.iaclen + 3 > IACBUFSIZE)
292 iacflush();
293
294 putiac(IAC);
295 putiac(wwdd);
296 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000297}
298
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000299#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000300static void putiac_subopt(byte c, char *str)
301{
302 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
303
304 if (G.iaclen + len > IACBUFSIZE)
305 iacflush();
306
307 putiac(IAC);
308 putiac(SB);
309 putiac(c);
310 putiac(0);
311
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000312 while (*str)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000313 putiac(*str++);
314
315 putiac(IAC);
316 putiac(SE);
317}
318#endif
319
Eric Andersen539ffc92004-02-22 12:25:47 +0000320#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
321static void putiac_subopt_autologin(void)
322{
323 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000324 const char *user = "USER";
Eric Andersen539ffc92004-02-22 12:25:47 +0000325
326 if (G.iaclen + len > IACBUFSIZE)
327 iacflush();
328
329 putiac(IAC);
330 putiac(SB);
331 putiac(TELOPT_NEW_ENVIRON);
332 putiac(TELQUAL_IS);
333 putiac(NEW_ENV_VAR);
334
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000335 while (*user)
Eric Andersen539ffc92004-02-22 12:25:47 +0000336 putiac(*user++);
337
338 putiac(NEW_ENV_VALUE);
339
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000340 while (*autologin)
Eric Andersen539ffc92004-02-22 12:25:47 +0000341 putiac(*autologin++);
342
343 putiac(IAC);
344 putiac(SE);
345}
346#endif
347
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000348#ifdef CONFIG_FEATURE_AUTOWIDTH
349static void putiac_naws(byte c, int x, int y)
350{
351 if (G.iaclen + 9 > IACBUFSIZE)
352 iacflush();
353
354 putiac(IAC);
355 putiac(SB);
356 putiac(c);
357
358 putiac((x >> 8) & 0xff);
359 putiac(x & 0xff);
360 putiac((y >> 8) & 0xff);
361 putiac(y & 0xff);
362
363 putiac(IAC);
364 putiac(SE);
365}
366#endif
367
Eric Andersen28c70b32000-06-14 20:42:57 +0000368/* void putiacstring (subneg strings) */
369
370/* ******************************* */
371
Eric Andersen3e6ff902001-03-09 21:24:12 +0000372static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000373
Eric Andersencd8c4362001-11-10 11:22:46 +0000374static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000375{
376 if (G.telflags & UF_ECHO)
377 {
378 if (G.charmode == CHM_TRY) {
379 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000380 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000381 rawmode();
382 }
383 }
384 else
385 {
386 if (G.charmode != CHM_OFF) {
387 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000388 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000389 cookmode();
390 }
391 }
392}
393
394/* ******************************* */
395
Eric Andersencd8c4362001-11-10 11:22:46 +0000396static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000397{
398 G.charmode = CHM_TRY;
399 G.telflags |= (UF_ECHO | UF_SGA);
400 setConMode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000401
Eric Andersen28c70b32000-06-14 20:42:57 +0000402 putiac2(DO, TELOPT_ECHO);
403 putiac2(DO, TELOPT_SGA);
404 iacflush();
405}
406
Eric Andersencd8c4362001-11-10 11:22:46 +0000407static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000408{
409 G.charmode = CHM_TRY;
410 G.telflags &= ~(UF_ECHO | UF_SGA);
411 setConMode();
412
413 putiac2(DONT, TELOPT_ECHO);
414 putiac2(DONT, TELOPT_SGA);
415 iacflush();
416}
417
418/* ******************************* */
419
Rob Landley88621d72006-08-29 19:41:06 +0000420static void to_notsup(char c)
Eric Andersen28c70b32000-06-14 20:42:57 +0000421{
422 if (G.telwish == WILL) putiac2(DONT, c);
423 else if (G.telwish == DO) putiac2(WONT, c);
424}
425
Rob Landley88621d72006-08-29 19:41:06 +0000426static void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000427{
428 /* if server requests ECHO, don't agree */
429 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
430 else if (G.telwish == DONT) return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000431
Eric Andersen28c70b32000-06-14 20:42:57 +0000432 if (G.telflags & UF_ECHO)
433 {
434 if (G.telwish == WILL)
435 return;
436 }
437 else
438 if (G.telwish == WONT)
439 return;
440
441 if (G.charmode != CHM_OFF)
442 G.telflags ^= UF_ECHO;
443
444 if (G.telflags & UF_ECHO)
445 putiac2(DO, TELOPT_ECHO);
446 else
447 putiac2(DONT, TELOPT_ECHO);
448
449 setConMode();
450 WriteCS(1, "\r\n"); /* sudden modec */
451}
452
Rob Landley88621d72006-08-29 19:41:06 +0000453static void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000454{
455 /* daemon always sends will/wont, client do/dont */
456
457 if (G.telflags & UF_SGA)
458 {
459 if (G.telwish == WILL)
460 return;
461 }
462 else
463 if (G.telwish == WONT)
464 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000465
Eric Andersen28c70b32000-06-14 20:42:57 +0000466 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
467 putiac2(DO, TELOPT_SGA);
468 else
469 putiac2(DONT, TELOPT_SGA);
Eric Andersen28c70b32000-06-14 20:42:57 +0000470}
471
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000472#ifdef CONFIG_FEATURE_TELNET_TTYPE
Rob Landley88621d72006-08-29 19:41:06 +0000473static void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000474{
475 /* Tell server we will (or won't) do TTYPE */
476
477 if(ttype)
478 putiac2(WILL, TELOPT_TTYPE);
479 else
480 putiac2(WONT, TELOPT_TTYPE);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000481}
482#endif
483
Eric Andersen539ffc92004-02-22 12:25:47 +0000484#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Rob Landley88621d72006-08-29 19:41:06 +0000485static void to_new_environ(void)
Eric Andersen539ffc92004-02-22 12:25:47 +0000486{
487 /* Tell server we will (or will not) do AUTOLOGIN */
488
489 if (autologin)
490 putiac2(WILL, TELOPT_NEW_ENVIRON);
491 else
492 putiac2(WONT, TELOPT_NEW_ENVIRON);
Eric Andersen539ffc92004-02-22 12:25:47 +0000493}
494#endif
495
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000496#ifdef CONFIG_FEATURE_AUTOWIDTH
Rob Landley88621d72006-08-29 19:41:06 +0000497static void to_naws(void)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000498{
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000499 /* Tell server we will do NAWS */
500 putiac2(WILL, TELOPT_NAWS);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000501}
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000502#endif
503
Eric Andersen28c70b32000-06-14 20:42:57 +0000504static void telopt(byte c)
505{
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000506 switch (c) {
507 case TELOPT_ECHO:
508 to_echo(); break;
509 case TELOPT_SGA:
510 to_sga(); break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000511#ifdef CONFIG_FEATURE_TELNET_TTYPE
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000512 case TELOPT_TTYPE:
513 to_ttype(); break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000514#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000515#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000516 case TELOPT_NEW_ENVIRON:
517 to_new_environ(); break;
Eric Andersen539ffc92004-02-22 12:25:47 +0000518#endif
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000519#ifdef CONFIG_FEATURE_AUTOWIDTH
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000520 case TELOPT_NAWS:
521 to_naws();
522 putiac_naws(c, win_width, win_height);
523 break;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000524#endif
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000525 default:
526 to_notsup(c);
527 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000528 }
529}
530
531
532/* ******************************* */
533
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000534/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000535
536static int subneg(byte c)
537{
538 switch (G.telstate)
539 {
540 case TS_SUB1:
541 if (c == IAC)
542 G.telstate = TS_SUB2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000543#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000544 else
545 if (c == TELOPT_TTYPE)
546 putiac_subopt(TELOPT_TTYPE,ttype);
547#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000548#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
549 else
550 if (c == TELOPT_NEW_ENVIRON)
551 putiac_subopt_autologin();
552#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000553 break;
554 case TS_SUB2:
555 if (c == SE)
556 return TRUE;
557 G.telstate = TS_SUB1;
558 /* break; */
559 }
560 return FALSE;
561}
562
563/* ******************************* */
564
565static void fgotsig(int sig)
566{
567 G.gotsig = sig;
568}
569
570
Eric Andersencd8c4362001-11-10 11:22:46 +0000571static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000572{
Paul Foxf2ddc052005-07-20 19:55:19 +0000573 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000574}
Eric Andersen28c70b32000-06-14 20:42:57 +0000575
Eric Andersencd8c4362001-11-10 11:22:46 +0000576static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000577{
Paul Foxf2ddc052005-07-20 19:55:19 +0000578 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
Eric Andersen28c70b32000-06-14 20:42:57 +0000579}
580
Denis Vlasenko06af2162007-02-03 17:28:39 +0000581int telnet_main(int argc, char** argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000582int telnet_main(int argc, char** argv)
Eric Andersen28c70b32000-06-14 20:42:57 +0000583{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000584 char *host;
585 int port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000586 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000587#ifdef USE_POLL
588 struct pollfd ufds[2];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000589#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000590 fd_set readfds;
591 int maxfd;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000592#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000593
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000594#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000595 get_terminal_width_height(0, &win_width, &win_height);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000596#endif
597
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000598#ifdef CONFIG_FEATURE_TELNET_TTYPE
Denis Vlasenko621204b2006-10-27 09:03:24 +0000599 ttype = getenv("TERM");
Eric Andersen7e1273e2001-05-07 17:57:45 +0000600#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000601
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000602 /* memset(&G, 0, sizeof G); - already is */
Eric Andersen28c70b32000-06-14 20:42:57 +0000603
Paul Foxf2ddc052005-07-20 19:55:19 +0000604 if (tcgetattr(0, &G.termios_def) >= 0) {
605 G.do_termios = 1;
Paul Foxf2ddc052005-07-20 19:55:19 +0000606 G.termios_raw = G.termios_def;
607 cfmakeraw(&G.termios_raw);
608 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000609
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000610 if (argc < 2)
611 bb_show_usage();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000612
Eric Andersen539ffc92004-02-22 12:25:47 +0000613#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000614 if (1 & getopt32(argc, argv, "al:", &autologin))
Rob Landley1b751c82005-10-28 09:24:33 +0000615 autologin = getenv("USER");
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000616 argv += optind;
Eric Andersen539ffc92004-02-22 12:25:47 +0000617#else
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000618 argv++;
Eric Andersen539ffc92004-02-22 12:25:47 +0000619#endif
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000620 if (!*argv)
621 bb_show_usage();
622 host = *argv++;
623 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
624 if (*argv) /* extra params?? */
625 bb_show_usage();
626
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000627 G.netfd = create_and_connect_stream_or_die(host, port);
Eric Andersen28c70b32000-06-14 20:42:57 +0000628
Denis Vlasenko703e2022007-01-22 14:12:08 +0000629 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
Eric Andersen28c70b32000-06-14 20:42:57 +0000630
631 signal(SIGINT, fgotsig);
632
633#ifdef USE_POLL
634 ufds[0].fd = 0; ufds[1].fd = G.netfd;
635 ufds[0].events = ufds[1].events = POLLIN;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000636#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000637 FD_ZERO(&readfds);
638 FD_SET(0, &readfds);
639 FD_SET(G.netfd, &readfds);
640 maxfd = G.netfd + 1;
641#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000642
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000643 while (1) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000644#ifndef USE_POLL
645 fd_set rfds = readfds;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000646
Eric Andersen28c70b32000-06-14 20:42:57 +0000647 switch (select(maxfd, &rfds, NULL, NULL, NULL))
648#else
649 switch (poll(ufds, 2, -1))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000650#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000651 {
652 case 0:
653 /* timeout */
654 case -1:
655 /* error, ignore and/or log something, bay go to loop */
656 if (G.gotsig)
657 conescape();
658 else
659 sleep(1);
660 break;
661 default:
662
663#ifdef USE_POLL
664 if (ufds[0].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000665#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000666 if (FD_ISSET(0, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000667#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000668 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000669 len = read(0, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000670
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000671 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000672 doexit(0);
673
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000674 TRACE(0, ("Read con: %d\n", len));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000675
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000676 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000677 }
678
679#ifdef USE_POLL
680 if (ufds[1].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000681#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000682 if (FD_ISSET(G.netfd, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000683#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000684 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000685 len = read(G.netfd, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000686
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000687 if (len <= 0) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000688 WriteCS(1, "Connection closed by foreign host.\r\n");
689 doexit(1);
690 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000691 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000692
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000693 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000694 }
695 }
696 }
697}