blob: 2a6457f2c1aec051634542f67b1ea45910e2f01a [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#include <arpa/inet.h> /* for inet_ntoa()... */
31#define TRACE(x, y) do { if (x) printf y; } while (0)
32#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000033#define TRACE(x, y)
Eric Andersen28c70b32000-06-14 20:42:57 +000034#endif
35
Glenn L McGrath78b0e372001-06-26 02:06:08 +000036#define DATABUFSIZE 128
37#define IACBUFSIZE 128
Eric Andersen28c70b32000-06-14 20:42:57 +000038
Mark Whitley59ab0252001-01-23 22:30:04 +000039enum {
Rob Landleybc68cd12006-03-10 19:22:06 +000040 CHM_TRY = 0,
41 CHM_ON = 1,
42 CHM_OFF = 2,
43
44 UF_ECHO = 0x01,
45 UF_SGA = 0x02,
46
Mark Whitley59ab0252001-01-23 22:30:04 +000047 TS_0 = 1,
48 TS_IAC = 2,
49 TS_OPT = 3,
50 TS_SUB1 = 4,
51 TS_SUB2 = 5,
52};
Eric Andersen28c70b32000-06-14 20:42:57 +000053
54#define WriteCS(fd, str) write(fd, str, sizeof str -1)
55
56typedef unsigned char byte;
57
58/* use globals to reduce size ??? */ /* test this hypothesis later */
Eric Andersen92d23242001-03-19 23:49:41 +000059static struct Globalvars {
Eric Andersen28c70b32000-06-14 20:42:57 +000060 int netfd; /* console fd:s are 0 and 1 (and 2) */
61 /* same buffer used both for network and console read/write */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000062 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
Eric Andersen28c70b32000-06-14 20:42:57 +000063 byte telstate; /* telnet negotiation state from network input */
64 byte telwish; /* DO, DONT, WILL, WONT */
65 byte charmode;
66 byte telflags;
67 byte gotsig;
Paul Foxf2ddc052005-07-20 19:55:19 +000068 byte do_termios;
Eric Andersen28c70b32000-06-14 20:42:57 +000069 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000070 char iacbuf[IACBUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +000071 short iaclen; /* could even use byte */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000072 struct termios termios_def;
73 struct termios termios_raw;
Eric Andersen28c70b32000-06-14 20:42:57 +000074} G;
75
76#define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
77
78#ifdef USE_GLOBALVAR_PTR
79struct Globalvars * Gptr;
80#define G (*Gptr)
Eric Andersen28c70b32000-06-14 20:42:57 +000081#endif
82
Rob Landley88621d72006-08-29 19:41:06 +000083static void iacflush(void)
Eric Andersen28c70b32000-06-14 20:42:57 +000084{
85 write(G.netfd, G.iacbuf, G.iaclen);
86 G.iaclen = 0;
87}
88
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
97/* Some globals */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000098#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +000099static char *ttype;
100#endif
101
Eric Andersen539ffc92004-02-22 12:25:47 +0000102#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000103static const char *autologin;
Eric Andersen539ffc92004-02-22 12:25:47 +0000104#endif
105
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000106#ifdef CONFIG_FEATURE_AUTOWIDTH
107static int win_width, win_height;
108#endif
109
Eric Andersen28c70b32000-06-14 20:42:57 +0000110static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000111{
Eric Andersen28c70b32000-06-14 20:42:57 +0000112 cookmode();
113 exit(ev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000114}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000115
Eric Andersencd8c4362001-11-10 11:22:46 +0000116static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000117{
Eric Andersen28c70b32000-06-14 20:42:57 +0000118 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000119
Eric Andersen28c70b32000-06-14 20:42:57 +0000120 if (G.gotsig) /* came from line mode... go raw */
121 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000122
Eric Andersen28c70b32000-06-14 20:42:57 +0000123 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
124 " l go to line mode\r\n"
125 " c go to character mode\r\n"
126 " z suspend telnet\r\n"
127 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000128
Eric Andersen28c70b32000-06-14 20:42:57 +0000129 if (read(0, &b, 1) <= 0)
130 doexit(1);
131
132 switch (b)
133 {
134 case 'l':
135 if (!G.gotsig)
136 {
137 do_linemode();
138 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000139 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000140 break;
141 case 'c':
142 if (G.gotsig)
143 {
144 will_charmode();
145 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000146 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000147 break;
148 case 'z':
149 cookmode();
150 kill(0, SIGTSTP);
151 rawmode();
152 break;
153 case 'e':
154 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000155 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000156
157 WriteCS(1, "continuing...\r\n");
158
159 if (G.gotsig)
160 cookmode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000161
Eric Andersen28c70b32000-06-14 20:42:57 +0000162 rrturn:
163 G.gotsig = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000164
Eric Andersen28c70b32000-06-14 20:42:57 +0000165}
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000166static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000167{
168 /* here we could do smart tricks how to handle 0xFF:s in output
169 * stream like writing twice every sequence of FF:s (thus doing
170 * many write()s. But I think interactive telnet application does
171 * not need to be 100% 8-bit clean, so changing every 0xff:s to
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000172 * 0x7f:s
173 *
174 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
175 * I don't agree.
176 * first - I cannot use programs like sz/rz
177 * second - the 0x0D is sent as one character and if the next
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000178 * char is 0x0A then it's eaten by a server side.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000179 * third - whay doy you have to make 'many write()s'?
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000180 * I don't understand.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000181 * So I implemented it. It's realy useful for me. I hope that
182 * others people will find it interesting to.
183 */
Eric Andersen28c70b32000-06-14 20:42:57 +0000184
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000185 int i, j;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000186 byte * p = (byte*)G.buf;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000187 byte outbuf[4*DATABUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000188
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000189 for (i = len, j = 0; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000190 {
191 if (*p == 0x1d)
192 {
193 conescape();
194 return;
195 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000196 outbuf[j++] = *p;
Eric Andersen28c70b32000-06-14 20:42:57 +0000197 if (*p == 0xff)
Denis Vlasenko92758142006-10-03 19:56:34 +0000198 outbuf[j++] = 0xff;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000199 else if (*p == 0x0d)
Denis Vlasenko92758142006-10-03 19:56:34 +0000200 outbuf[j++] = 0x00;
Eric Andersen28c70b32000-06-14 20:42:57 +0000201 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000202 if (j > 0 )
Denis Vlasenko92758142006-10-03 19:56:34 +0000203 write(G.netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000204}
205
Eric Andersen28c70b32000-06-14 20:42:57 +0000206
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000207static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000208{
Eric Andersen28c70b32000-06-14 20:42:57 +0000209 int i;
210 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000211
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000212 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000213 {
214 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000215
Eric Andersen28c70b32000-06-14 20:42:57 +0000216 if (G.telstate == 0) /* most of the time state == 0 */
217 {
218 if (c == IAC)
219 {
220 cstart = i;
221 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000222 }
223 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000224 else
225 switch (G.telstate)
226 {
227 case TS_0:
228 if (c == IAC)
229 G.telstate = TS_IAC;
230 else
231 G.buf[cstart++] = c;
232 break;
233
234 case TS_IAC:
235 if (c == IAC) /* IAC IAC -> 0xFF */
236 {
237 G.buf[cstart++] = c;
238 G.telstate = TS_0;
239 break;
240 }
241 /* else */
242 switch (c)
243 {
244 case SB:
245 G.telstate = TS_SUB1;
246 break;
247 case DO:
248 case DONT:
249 case WILL:
250 case WONT:
251 G.telwish = c;
252 G.telstate = TS_OPT;
253 break;
254 default:
255 G.telstate = TS_0; /* DATA MARK must be added later */
256 }
257 break;
258 case TS_OPT: /* WILL, WONT, DO, DONT */
259 telopt(c);
260 G.telstate = TS_0;
261 break;
262 case TS_SUB1: /* Subnegotiation */
263 case TS_SUB2: /* Subnegotiation */
Matt Kraai1f0c4362001-12-20 23:13:26 +0000264 if (subneg(c))
Eric Andersen28c70b32000-06-14 20:42:57 +0000265 G.telstate = TS_0;
266 break;
267 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000268 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000269 if (G.telstate)
270 {
271 if (G.iaclen) iacflush();
272 if (G.telstate == TS_0) G.telstate = 0;
273
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000274 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000275 }
276
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000277 if (len)
278 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000279}
280
Eric Andersen28c70b32000-06-14 20:42:57 +0000281
282/* ******************************* */
283
Rob Landley88621d72006-08-29 19:41:06 +0000284static void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000285{
Eric Andersen28c70b32000-06-14 20:42:57 +0000286 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000287}
288
Eric Andersen28c70b32000-06-14 20:42:57 +0000289
290static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000291{
Eric Andersen28c70b32000-06-14 20:42:57 +0000292 if (G.iaclen + 3 > IACBUFSIZE)
293 iacflush();
294
295 putiac(IAC);
296 putiac(wwdd);
297 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000298}
299
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000300#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000301static void putiac_subopt(byte c, char *str)
302{
303 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
304
305 if (G.iaclen + len > IACBUFSIZE)
306 iacflush();
307
308 putiac(IAC);
309 putiac(SB);
310 putiac(c);
311 putiac(0);
312
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000313 while (*str)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000314 putiac(*str++);
315
316 putiac(IAC);
317 putiac(SE);
318}
319#endif
320
Eric Andersen539ffc92004-02-22 12:25:47 +0000321#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
322static void putiac_subopt_autologin(void)
323{
324 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000325 const char *user = "USER";
Eric Andersen539ffc92004-02-22 12:25:47 +0000326
327 if (G.iaclen + len > IACBUFSIZE)
328 iacflush();
329
330 putiac(IAC);
331 putiac(SB);
332 putiac(TELOPT_NEW_ENVIRON);
333 putiac(TELQUAL_IS);
334 putiac(NEW_ENV_VAR);
335
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000336 while (*user)
Eric Andersen539ffc92004-02-22 12:25:47 +0000337 putiac(*user++);
338
339 putiac(NEW_ENV_VALUE);
340
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000341 while (*autologin)
Eric Andersen539ffc92004-02-22 12:25:47 +0000342 putiac(*autologin++);
343
344 putiac(IAC);
345 putiac(SE);
346}
347#endif
348
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000349#ifdef CONFIG_FEATURE_AUTOWIDTH
350static void putiac_naws(byte c, int x, int y)
351{
352 if (G.iaclen + 9 > IACBUFSIZE)
353 iacflush();
354
355 putiac(IAC);
356 putiac(SB);
357 putiac(c);
358
359 putiac((x >> 8) & 0xff);
360 putiac(x & 0xff);
361 putiac((y >> 8) & 0xff);
362 putiac(y & 0xff);
363
364 putiac(IAC);
365 putiac(SE);
366}
367#endif
368
Eric Andersen28c70b32000-06-14 20:42:57 +0000369/* void putiacstring (subneg strings) */
370
371/* ******************************* */
372
Eric Andersen3e6ff902001-03-09 21:24:12 +0000373static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000374
Eric Andersencd8c4362001-11-10 11:22:46 +0000375static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000376{
377 if (G.telflags & UF_ECHO)
378 {
379 if (G.charmode == CHM_TRY) {
380 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000381 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000382 rawmode();
383 }
384 }
385 else
386 {
387 if (G.charmode != CHM_OFF) {
388 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000389 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000390 cookmode();
391 }
392 }
393}
394
395/* ******************************* */
396
Eric Andersencd8c4362001-11-10 11:22:46 +0000397static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000398{
399 G.charmode = CHM_TRY;
400 G.telflags |= (UF_ECHO | UF_SGA);
401 setConMode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000402
Eric Andersen28c70b32000-06-14 20:42:57 +0000403 putiac2(DO, TELOPT_ECHO);
404 putiac2(DO, TELOPT_SGA);
405 iacflush();
406}
407
Eric Andersencd8c4362001-11-10 11:22:46 +0000408static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000409{
410 G.charmode = CHM_TRY;
411 G.telflags &= ~(UF_ECHO | UF_SGA);
412 setConMode();
413
414 putiac2(DONT, TELOPT_ECHO);
415 putiac2(DONT, TELOPT_SGA);
416 iacflush();
417}
418
419/* ******************************* */
420
Rob Landley88621d72006-08-29 19:41:06 +0000421static void to_notsup(char c)
Eric Andersen28c70b32000-06-14 20:42:57 +0000422{
423 if (G.telwish == WILL) putiac2(DONT, c);
424 else if (G.telwish == DO) putiac2(WONT, c);
425}
426
Rob Landley88621d72006-08-29 19:41:06 +0000427static void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000428{
429 /* if server requests ECHO, don't agree */
430 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
431 else if (G.telwish == DONT) return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000432
Eric Andersen28c70b32000-06-14 20:42:57 +0000433 if (G.telflags & UF_ECHO)
434 {
435 if (G.telwish == WILL)
436 return;
437 }
438 else
439 if (G.telwish == WONT)
440 return;
441
442 if (G.charmode != CHM_OFF)
443 G.telflags ^= UF_ECHO;
444
445 if (G.telflags & UF_ECHO)
446 putiac2(DO, TELOPT_ECHO);
447 else
448 putiac2(DONT, TELOPT_ECHO);
449
450 setConMode();
451 WriteCS(1, "\r\n"); /* sudden modec */
452}
453
Rob Landley88621d72006-08-29 19:41:06 +0000454static void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000455{
456 /* daemon always sends will/wont, client do/dont */
457
458 if (G.telflags & UF_SGA)
459 {
460 if (G.telwish == WILL)
461 return;
462 }
463 else
464 if (G.telwish == WONT)
465 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000466
Eric Andersen28c70b32000-06-14 20:42:57 +0000467 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
468 putiac2(DO, TELOPT_SGA);
469 else
470 putiac2(DONT, TELOPT_SGA);
471
472 return;
473}
474
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000475#ifdef CONFIG_FEATURE_TELNET_TTYPE
Rob Landley88621d72006-08-29 19:41:06 +0000476static void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000477{
478 /* Tell server we will (or won't) do TTYPE */
479
480 if(ttype)
481 putiac2(WILL, TELOPT_TTYPE);
482 else
483 putiac2(WONT, TELOPT_TTYPE);
484
485 return;
486}
487#endif
488
Eric Andersen539ffc92004-02-22 12:25:47 +0000489#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Rob Landley88621d72006-08-29 19:41:06 +0000490static void to_new_environ(void)
Eric Andersen539ffc92004-02-22 12:25:47 +0000491{
492 /* Tell server we will (or will not) do AUTOLOGIN */
493
494 if (autologin)
495 putiac2(WILL, TELOPT_NEW_ENVIRON);
496 else
497 putiac2(WONT, TELOPT_NEW_ENVIRON);
498
499 return;
500}
501#endif
502
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000503#ifdef CONFIG_FEATURE_AUTOWIDTH
Rob Landley88621d72006-08-29 19:41:06 +0000504static void to_naws(void)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000505{
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000506 /* Tell server we will do NAWS */
507 putiac2(WILL, TELOPT_NAWS);
508 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000509}
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000510#endif
511
Eric Andersen28c70b32000-06-14 20:42:57 +0000512static void telopt(byte c)
513{
514 switch (c)
515 {
Eric Andersen41634062002-04-26 08:44:17 +0000516 case TELOPT_ECHO: to_echo(); break;
517 case TELOPT_SGA: to_sga(); break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000518#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen41634062002-04-26 08:44:17 +0000519 case TELOPT_TTYPE: to_ttype();break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000520#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000521#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
522 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
523#endif
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000524#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen41634062002-04-26 08:44:17 +0000525 case TELOPT_NAWS: to_naws();
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000526 putiac_naws(c, win_width, win_height);
527 break;
528#endif
Eric Andersen8db361b2002-04-26 08:00:33 +0000529 default: to_notsup(c);
530 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000531 }
532}
533
534
535/* ******************************* */
536
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000537/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000538
539static int subneg(byte c)
540{
541 switch (G.telstate)
542 {
543 case TS_SUB1:
544 if (c == IAC)
545 G.telstate = TS_SUB2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000546#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000547 else
548 if (c == TELOPT_TTYPE)
549 putiac_subopt(TELOPT_TTYPE,ttype);
550#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000551#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
552 else
553 if (c == TELOPT_NEW_ENVIRON)
554 putiac_subopt_autologin();
555#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000556 break;
557 case TS_SUB2:
558 if (c == SE)
559 return TRUE;
560 G.telstate = TS_SUB1;
561 /* break; */
562 }
563 return FALSE;
564}
565
566/* ******************************* */
567
568static void fgotsig(int sig)
569{
570 G.gotsig = sig;
571}
572
573
Eric Andersencd8c4362001-11-10 11:22:46 +0000574static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000575{
Paul Foxf2ddc052005-07-20 19:55:19 +0000576 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000577}
Eric Andersen28c70b32000-06-14 20:42:57 +0000578
Eric Andersencd8c4362001-11-10 11:22:46 +0000579static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000580{
Paul Foxf2ddc052005-07-20 19:55:19 +0000581 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
Eric Andersen28c70b32000-06-14 20:42:57 +0000582}
583
Denis Vlasenko06af2162007-02-03 17:28:39 +0000584int telnet_main(int argc, char** argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000585int telnet_main(int argc, char** argv)
Eric Andersen28c70b32000-06-14 20:42:57 +0000586{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000587 char *host;
588 int port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000589 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000590#ifdef USE_POLL
591 struct pollfd ufds[2];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000592#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000593 fd_set readfds;
594 int maxfd;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000595#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000596
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000597#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000598 get_terminal_width_height(0, &win_width, &win_height);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000599#endif
600
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000601#ifdef CONFIG_FEATURE_TELNET_TTYPE
Denis Vlasenko621204b2006-10-27 09:03:24 +0000602 ttype = getenv("TERM");
Eric Andersen7e1273e2001-05-07 17:57:45 +0000603#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000604
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000605 /* memset(&G, 0, sizeof G); - already is */
Eric Andersen28c70b32000-06-14 20:42:57 +0000606
Paul Foxf2ddc052005-07-20 19:55:19 +0000607 if (tcgetattr(0, &G.termios_def) >= 0) {
608 G.do_termios = 1;
Paul Foxf2ddc052005-07-20 19:55:19 +0000609 G.termios_raw = G.termios_def;
610 cfmakeraw(&G.termios_raw);
611 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000612
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000613 if (argc < 2)
614 bb_show_usage();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000615
Eric Andersen539ffc92004-02-22 12:25:47 +0000616#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000617 if (1 & getopt32(argc, argv, "al:", &autologin))
Rob Landley1b751c82005-10-28 09:24:33 +0000618 autologin = getenv("USER");
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000619 argv += optind;
Eric Andersen539ffc92004-02-22 12:25:47 +0000620#else
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000621 argv++;
Eric Andersen539ffc92004-02-22 12:25:47 +0000622#endif
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000623 if (!*argv)
624 bb_show_usage();
625 host = *argv++;
626 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
627 if (*argv) /* extra params?? */
628 bb_show_usage();
629
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000630 G.netfd = create_and_connect_stream_or_die(host, port);
Eric Andersen28c70b32000-06-14 20:42:57 +0000631
Denis Vlasenko703e2022007-01-22 14:12:08 +0000632 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
Eric Andersen28c70b32000-06-14 20:42:57 +0000633
634 signal(SIGINT, fgotsig);
635
636#ifdef USE_POLL
637 ufds[0].fd = 0; ufds[1].fd = G.netfd;
638 ufds[0].events = ufds[1].events = POLLIN;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000639#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000640 FD_ZERO(&readfds);
641 FD_SET(0, &readfds);
642 FD_SET(G.netfd, &readfds);
643 maxfd = G.netfd + 1;
644#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000645
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000646 while (1) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000647#ifndef USE_POLL
648 fd_set rfds = readfds;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000649
Eric Andersen28c70b32000-06-14 20:42:57 +0000650 switch (select(maxfd, &rfds, NULL, NULL, NULL))
651#else
652 switch (poll(ufds, 2, -1))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000653#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000654 {
655 case 0:
656 /* timeout */
657 case -1:
658 /* error, ignore and/or log something, bay go to loop */
659 if (G.gotsig)
660 conescape();
661 else
662 sleep(1);
663 break;
664 default:
665
666#ifdef USE_POLL
667 if (ufds[0].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000668#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000669 if (FD_ISSET(0, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000670#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000671 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000672 len = read(0, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000673
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000674 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000675 doexit(0);
676
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000677 TRACE(0, ("Read con: %d\n", len));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000678
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000679 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000680 }
681
682#ifdef USE_POLL
683 if (ufds[1].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000684#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000685 if (FD_ISSET(G.netfd, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000686#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000687 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000688 len = read(G.netfd, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000689
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000690 if (len <= 0) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000691 WriteCS(1, "Connection closed by foreign host.\r\n");
692 doexit(1);
693 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000694 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000695
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000696 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000697 }
698 }
699 }
700}