blob: 628e2e6e3c48b213c0a63e385fa1a12dcea0cc05 [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 <unistd.h>
26#include <errno.h>
27#include <stdlib.h>
28#include <stdarg.h>
Eric Andersen2276d832002-07-11 11:11:56 +000029#include <string.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000030#include <signal.h>
31#include <arpa/telnet.h>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000032#include <sys/types.h>
33#include <sys/socket.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000034#include <netinet/in.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include "busybox.h"
Erik Andersenf7c49ef2000-02-22 17:17:45 +000036
Pavel Roskin616d13b2000-07-28 19:38:27 +000037#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000038#include <arpa/inet.h> /* for inet_ntoa()... */
39#define TRACE(x, y) do { if (x) printf y; } while (0)
40#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000041#define TRACE(x, y)
Eric Andersen28c70b32000-06-14 20:42:57 +000042#endif
43
Glenn L McGrath78b0e372001-06-26 02:06:08 +000044#define DATABUFSIZE 128
45#define IACBUFSIZE 128
Eric Andersen28c70b32000-06-14 20:42:57 +000046
Mark Whitley59ab0252001-01-23 22:30:04 +000047enum {
Rob Landleybc68cd12006-03-10 19:22:06 +000048 CHM_TRY = 0,
49 CHM_ON = 1,
50 CHM_OFF = 2,
51
52 UF_ECHO = 0x01,
53 UF_SGA = 0x02,
54
Mark Whitley59ab0252001-01-23 22:30:04 +000055 TS_0 = 1,
56 TS_IAC = 2,
57 TS_OPT = 3,
58 TS_SUB1 = 4,
59 TS_SUB2 = 5,
60};
Eric Andersen28c70b32000-06-14 20:42:57 +000061
62#define WriteCS(fd, str) write(fd, str, sizeof str -1)
63
64typedef unsigned char byte;
65
66/* use globals to reduce size ??? */ /* test this hypothesis later */
Eric Andersen92d23242001-03-19 23:49:41 +000067static struct Globalvars {
Eric Andersen28c70b32000-06-14 20:42:57 +000068 int netfd; /* console fd:s are 0 and 1 (and 2) */
69 /* same buffer used both for network and console read/write */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000070 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
Eric Andersen28c70b32000-06-14 20:42:57 +000071 byte telstate; /* telnet negotiation state from network input */
72 byte telwish; /* DO, DONT, WILL, WONT */
73 byte charmode;
74 byte telflags;
75 byte gotsig;
Paul Foxf2ddc052005-07-20 19:55:19 +000076 byte do_termios;
Eric Andersen28c70b32000-06-14 20:42:57 +000077 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000078 char iacbuf[IACBUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +000079 short iaclen; /* could even use byte */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000080 struct termios termios_def;
81 struct termios termios_raw;
Eric Andersen28c70b32000-06-14 20:42:57 +000082} G;
83
84#define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
85
86#ifdef USE_GLOBALVAR_PTR
87struct Globalvars * Gptr;
88#define G (*Gptr)
Eric Andersen28c70b32000-06-14 20:42:57 +000089#endif
90
Rob Landley88621d72006-08-29 19:41:06 +000091static void iacflush(void)
Eric Andersen28c70b32000-06-14 20:42:57 +000092{
93 write(G.netfd, G.iacbuf, G.iaclen);
94 G.iaclen = 0;
95}
96
97/* Function prototypes */
Eric Andersencd8c4362001-11-10 11:22:46 +000098static void rawmode(void);
99static void cookmode(void);
100static void do_linemode(void);
101static void will_charmode(void);
Eric Andersen28c70b32000-06-14 20:42:57 +0000102static void telopt(byte c);
103static int subneg(byte c);
Eric Andersen28c70b32000-06-14 20:42:57 +0000104
105/* Some globals */
106static int one = 1;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000107
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000108#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000109static char *ttype;
110#endif
111
Eric Andersen539ffc92004-02-22 12:25:47 +0000112#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000113static const char *autologin;
Eric Andersen539ffc92004-02-22 12:25:47 +0000114#endif
115
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000116#ifdef CONFIG_FEATURE_AUTOWIDTH
117static int win_width, win_height;
118#endif
119
Eric Andersen28c70b32000-06-14 20:42:57 +0000120static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000121{
Eric Andersen28c70b32000-06-14 20:42:57 +0000122 cookmode();
123 exit(ev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000124}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000125
Eric Andersencd8c4362001-11-10 11:22:46 +0000126static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000127{
Eric Andersen28c70b32000-06-14 20:42:57 +0000128 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000129
Eric Andersen28c70b32000-06-14 20:42:57 +0000130 if (G.gotsig) /* came from line mode... go raw */
131 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000132
Eric Andersen28c70b32000-06-14 20:42:57 +0000133 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
134 " l go to line mode\r\n"
135 " c go to character mode\r\n"
136 " z suspend telnet\r\n"
137 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000138
Eric Andersen28c70b32000-06-14 20:42:57 +0000139 if (read(0, &b, 1) <= 0)
140 doexit(1);
141
142 switch (b)
143 {
144 case 'l':
145 if (!G.gotsig)
146 {
147 do_linemode();
148 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000149 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000150 break;
151 case 'c':
152 if (G.gotsig)
153 {
154 will_charmode();
155 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000156 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000157 break;
158 case 'z':
159 cookmode();
160 kill(0, SIGTSTP);
161 rawmode();
162 break;
163 case 'e':
164 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000165 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000166
167 WriteCS(1, "continuing...\r\n");
168
169 if (G.gotsig)
170 cookmode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000171
Eric Andersen28c70b32000-06-14 20:42:57 +0000172 rrturn:
173 G.gotsig = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000174
Eric Andersen28c70b32000-06-14 20:42:57 +0000175}
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000176static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000177{
178 /* here we could do smart tricks how to handle 0xFF:s in output
179 * stream like writing twice every sequence of FF:s (thus doing
180 * many write()s. But I think interactive telnet application does
181 * not need to be 100% 8-bit clean, so changing every 0xff:s to
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000182 * 0x7f:s
183 *
184 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
185 * I don't agree.
186 * first - I cannot use programs like sz/rz
187 * second - the 0x0D is sent as one character and if the next
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000188 * char is 0x0A then it's eaten by a server side.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000189 * third - whay doy you have to make 'many write()s'?
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000190 * I don't understand.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000191 * So I implemented it. It's realy useful for me. I hope that
192 * others people will find it interesting to.
193 */
Eric Andersen28c70b32000-06-14 20:42:57 +0000194
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000195 int i, j;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000196 byte * p = (byte*)G.buf;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000197 byte outbuf[4*DATABUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000198
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000199 for (i = len, j = 0; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000200 {
201 if (*p == 0x1d)
202 {
203 conescape();
204 return;
205 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000206 outbuf[j++] = *p;
Eric Andersen28c70b32000-06-14 20:42:57 +0000207 if (*p == 0xff)
Denis Vlasenko92758142006-10-03 19:56:34 +0000208 outbuf[j++] = 0xff;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000209 else if (*p == 0x0d)
Denis Vlasenko92758142006-10-03 19:56:34 +0000210 outbuf[j++] = 0x00;
Eric Andersen28c70b32000-06-14 20:42:57 +0000211 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000212 if (j > 0 )
Denis Vlasenko92758142006-10-03 19:56:34 +0000213 write(G.netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000214}
215
Eric Andersen28c70b32000-06-14 20:42:57 +0000216
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000217static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000218{
Eric Andersen28c70b32000-06-14 20:42:57 +0000219 int i;
220 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000221
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000222 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000223 {
224 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000225
Eric Andersen28c70b32000-06-14 20:42:57 +0000226 if (G.telstate == 0) /* most of the time state == 0 */
227 {
228 if (c == IAC)
229 {
230 cstart = i;
231 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000232 }
233 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000234 else
235 switch (G.telstate)
236 {
237 case TS_0:
238 if (c == IAC)
239 G.telstate = TS_IAC;
240 else
241 G.buf[cstart++] = c;
242 break;
243
244 case TS_IAC:
245 if (c == IAC) /* IAC IAC -> 0xFF */
246 {
247 G.buf[cstart++] = c;
248 G.telstate = TS_0;
249 break;
250 }
251 /* else */
252 switch (c)
253 {
254 case SB:
255 G.telstate = TS_SUB1;
256 break;
257 case DO:
258 case DONT:
259 case WILL:
260 case WONT:
261 G.telwish = c;
262 G.telstate = TS_OPT;
263 break;
264 default:
265 G.telstate = TS_0; /* DATA MARK must be added later */
266 }
267 break;
268 case TS_OPT: /* WILL, WONT, DO, DONT */
269 telopt(c);
270 G.telstate = TS_0;
271 break;
272 case TS_SUB1: /* Subnegotiation */
273 case TS_SUB2: /* Subnegotiation */
Matt Kraai1f0c4362001-12-20 23:13:26 +0000274 if (subneg(c))
Eric Andersen28c70b32000-06-14 20:42:57 +0000275 G.telstate = TS_0;
276 break;
277 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000278 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000279 if (G.telstate)
280 {
281 if (G.iaclen) iacflush();
282 if (G.telstate == TS_0) G.telstate = 0;
283
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000284 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000285 }
286
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000287 if (len)
288 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000289}
290
Eric Andersen28c70b32000-06-14 20:42:57 +0000291
292/* ******************************* */
293
Rob Landley88621d72006-08-29 19:41:06 +0000294static void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000295{
Eric Andersen28c70b32000-06-14 20:42:57 +0000296 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000297}
298
Eric Andersen28c70b32000-06-14 20:42:57 +0000299
300static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000301{
Eric Andersen28c70b32000-06-14 20:42:57 +0000302 if (G.iaclen + 3 > IACBUFSIZE)
303 iacflush();
304
305 putiac(IAC);
306 putiac(wwdd);
307 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000308}
309
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000310#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000311static void putiac_subopt(byte c, char *str)
312{
313 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
314
315 if (G.iaclen + len > IACBUFSIZE)
316 iacflush();
317
318 putiac(IAC);
319 putiac(SB);
320 putiac(c);
321 putiac(0);
322
323 while(*str)
324 putiac(*str++);
325
326 putiac(IAC);
327 putiac(SE);
328}
329#endif
330
Eric Andersen539ffc92004-02-22 12:25:47 +0000331#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
332static void putiac_subopt_autologin(void)
333{
334 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
335 char *user = "USER";
336
337 if (G.iaclen + len > IACBUFSIZE)
338 iacflush();
339
340 putiac(IAC);
341 putiac(SB);
342 putiac(TELOPT_NEW_ENVIRON);
343 putiac(TELQUAL_IS);
344 putiac(NEW_ENV_VAR);
345
346 while(*user)
347 putiac(*user++);
348
349 putiac(NEW_ENV_VALUE);
350
351 while(*autologin)
352 putiac(*autologin++);
353
354 putiac(IAC);
355 putiac(SE);
356}
357#endif
358
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000359#ifdef CONFIG_FEATURE_AUTOWIDTH
360static void putiac_naws(byte c, int x, int y)
361{
362 if (G.iaclen + 9 > IACBUFSIZE)
363 iacflush();
364
365 putiac(IAC);
366 putiac(SB);
367 putiac(c);
368
369 putiac((x >> 8) & 0xff);
370 putiac(x & 0xff);
371 putiac((y >> 8) & 0xff);
372 putiac(y & 0xff);
373
374 putiac(IAC);
375 putiac(SE);
376}
377#endif
378
Eric Andersen28c70b32000-06-14 20:42:57 +0000379/* void putiacstring (subneg strings) */
380
381/* ******************************* */
382
Eric Andersen3e6ff902001-03-09 21:24:12 +0000383static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000384
Eric Andersencd8c4362001-11-10 11:22:46 +0000385static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000386{
387 if (G.telflags & UF_ECHO)
388 {
389 if (G.charmode == CHM_TRY) {
390 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000391 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000392 rawmode();
393 }
394 }
395 else
396 {
397 if (G.charmode != CHM_OFF) {
398 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000399 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000400 cookmode();
401 }
402 }
403}
404
405/* ******************************* */
406
Eric Andersencd8c4362001-11-10 11:22:46 +0000407static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000408{
409 G.charmode = CHM_TRY;
410 G.telflags |= (UF_ECHO | UF_SGA);
411 setConMode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000412
Eric Andersen28c70b32000-06-14 20:42:57 +0000413 putiac2(DO, TELOPT_ECHO);
414 putiac2(DO, TELOPT_SGA);
415 iacflush();
416}
417
Eric Andersencd8c4362001-11-10 11:22:46 +0000418static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000419{
420 G.charmode = CHM_TRY;
421 G.telflags &= ~(UF_ECHO | UF_SGA);
422 setConMode();
423
424 putiac2(DONT, TELOPT_ECHO);
425 putiac2(DONT, TELOPT_SGA);
426 iacflush();
427}
428
429/* ******************************* */
430
Rob Landley88621d72006-08-29 19:41:06 +0000431static void to_notsup(char c)
Eric Andersen28c70b32000-06-14 20:42:57 +0000432{
433 if (G.telwish == WILL) putiac2(DONT, c);
434 else if (G.telwish == DO) putiac2(WONT, c);
435}
436
Rob Landley88621d72006-08-29 19:41:06 +0000437static void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000438{
439 /* if server requests ECHO, don't agree */
440 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
441 else if (G.telwish == DONT) return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000442
Eric Andersen28c70b32000-06-14 20:42:57 +0000443 if (G.telflags & UF_ECHO)
444 {
445 if (G.telwish == WILL)
446 return;
447 }
448 else
449 if (G.telwish == WONT)
450 return;
451
452 if (G.charmode != CHM_OFF)
453 G.telflags ^= UF_ECHO;
454
455 if (G.telflags & UF_ECHO)
456 putiac2(DO, TELOPT_ECHO);
457 else
458 putiac2(DONT, TELOPT_ECHO);
459
460 setConMode();
461 WriteCS(1, "\r\n"); /* sudden modec */
462}
463
Rob Landley88621d72006-08-29 19:41:06 +0000464static void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000465{
466 /* daemon always sends will/wont, client do/dont */
467
468 if (G.telflags & UF_SGA)
469 {
470 if (G.telwish == WILL)
471 return;
472 }
473 else
474 if (G.telwish == WONT)
475 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000476
Eric Andersen28c70b32000-06-14 20:42:57 +0000477 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
478 putiac2(DO, TELOPT_SGA);
479 else
480 putiac2(DONT, TELOPT_SGA);
481
482 return;
483}
484
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000485#ifdef CONFIG_FEATURE_TELNET_TTYPE
Rob Landley88621d72006-08-29 19:41:06 +0000486static void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000487{
488 /* Tell server we will (or won't) do TTYPE */
489
490 if(ttype)
491 putiac2(WILL, TELOPT_TTYPE);
492 else
493 putiac2(WONT, TELOPT_TTYPE);
494
495 return;
496}
497#endif
498
Eric Andersen539ffc92004-02-22 12:25:47 +0000499#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Rob Landley88621d72006-08-29 19:41:06 +0000500static void to_new_environ(void)
Eric Andersen539ffc92004-02-22 12:25:47 +0000501{
502 /* Tell server we will (or will not) do AUTOLOGIN */
503
504 if (autologin)
505 putiac2(WILL, TELOPT_NEW_ENVIRON);
506 else
507 putiac2(WONT, TELOPT_NEW_ENVIRON);
508
509 return;
510}
511#endif
512
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000513#ifdef CONFIG_FEATURE_AUTOWIDTH
Rob Landley88621d72006-08-29 19:41:06 +0000514static void to_naws(void)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000515{
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000516 /* Tell server we will do NAWS */
517 putiac2(WILL, TELOPT_NAWS);
518 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000519}
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000520#endif
521
Eric Andersen28c70b32000-06-14 20:42:57 +0000522static void telopt(byte c)
523{
524 switch (c)
525 {
Eric Andersen41634062002-04-26 08:44:17 +0000526 case TELOPT_ECHO: to_echo(); break;
527 case TELOPT_SGA: to_sga(); break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000528#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen41634062002-04-26 08:44:17 +0000529 case TELOPT_TTYPE: to_ttype();break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000530#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000531#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
532 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
533#endif
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000534#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen41634062002-04-26 08:44:17 +0000535 case TELOPT_NAWS: to_naws();
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000536 putiac_naws(c, win_width, win_height);
537 break;
538#endif
Eric Andersen8db361b2002-04-26 08:00:33 +0000539 default: to_notsup(c);
540 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000541 }
542}
543
544
545/* ******************************* */
546
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000547/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000548
549static int subneg(byte c)
550{
551 switch (G.telstate)
552 {
553 case TS_SUB1:
554 if (c == IAC)
555 G.telstate = TS_SUB2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000556#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000557 else
558 if (c == TELOPT_TTYPE)
559 putiac_subopt(TELOPT_TTYPE,ttype);
560#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000561#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
562 else
563 if (c == TELOPT_NEW_ENVIRON)
564 putiac_subopt_autologin();
565#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000566 break;
567 case TS_SUB2:
568 if (c == SE)
569 return TRUE;
570 G.telstate = TS_SUB1;
571 /* break; */
572 }
573 return FALSE;
574}
575
576/* ******************************* */
577
578static void fgotsig(int sig)
579{
580 G.gotsig = sig;
581}
582
583
Eric Andersencd8c4362001-11-10 11:22:46 +0000584static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000585{
Paul Foxf2ddc052005-07-20 19:55:19 +0000586 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000587}
Eric Andersen28c70b32000-06-14 20:42:57 +0000588
Eric Andersencd8c4362001-11-10 11:22:46 +0000589static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000590{
Paul Foxf2ddc052005-07-20 19:55:19 +0000591 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
Eric Andersen28c70b32000-06-14 20:42:57 +0000592}
593
Rob Landleydfba7412006-03-06 20:47:33 +0000594int telnet_main(int argc, char** argv)
Eric Andersen28c70b32000-06-14 20:42:57 +0000595{
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000596 int len;
Eric Andersene6dc4392003-10-31 09:31:46 +0000597 struct sockaddr_in s_in;
Eric Andersen28c70b32000-06-14 20:42:57 +0000598#ifdef USE_POLL
599 struct pollfd ufds[2];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000600#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000601 fd_set readfds;
602 int maxfd;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000603#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000604
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000605#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000606 get_terminal_width_height(0, &win_width, &win_height);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000607#endif
608
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000609#ifdef CONFIG_FEATURE_TELNET_TTYPE
Denis Vlasenko621204b2006-10-27 09:03:24 +0000610 ttype = getenv("TERM");
Eric Andersen7e1273e2001-05-07 17:57:45 +0000611#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000612
613 memset(&G, 0, sizeof G);
614
Paul Foxf2ddc052005-07-20 19:55:19 +0000615 if (tcgetattr(0, &G.termios_def) >= 0) {
616 G.do_termios = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000617
Paul Foxf2ddc052005-07-20 19:55:19 +0000618 G.termios_raw = G.termios_def;
619 cfmakeraw(&G.termios_raw);
620 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000621
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000622 if (argc < 2)
623 bb_show_usage();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000624
Eric Andersen539ffc92004-02-22 12:25:47 +0000625#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000626 if (1 & getopt32(argc, argv, "al:", &autologin))
Rob Landley1b751c82005-10-28 09:24:33 +0000627 autologin = getenv("USER");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000628
Eric Andersen539ffc92004-02-22 12:25:47 +0000629 if (optind < argc) {
630 bb_lookup_host(&s_in, argv[optind++]);
631 s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
632 "telnet", "tcp", 23);
633 if (optind < argc)
634 bb_show_usage();
635 } else
636 bb_show_usage();
637#else
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000638 bb_lookup_host(&s_in, argv[1]);
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000639 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
Eric Andersen539ffc92004-02-22 12:25:47 +0000640#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000641
Denis Vlasenko14579152006-10-26 01:09:46 +0000642 G.netfd = xconnect_tcp_v4(&s_in);
Eric Andersen28c70b32000-06-14 20:42:57 +0000643
Eric Andersen0b315862002-07-03 11:51:44 +0000644 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
Eric Andersen28c70b32000-06-14 20:42:57 +0000645
646 signal(SIGINT, fgotsig);
647
648#ifdef USE_POLL
649 ufds[0].fd = 0; ufds[1].fd = G.netfd;
650 ufds[0].events = ufds[1].events = POLLIN;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000651#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000652 FD_ZERO(&readfds);
653 FD_SET(0, &readfds);
654 FD_SET(G.netfd, &readfds);
655 maxfd = G.netfd + 1;
656#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000657
Eric Andersen28c70b32000-06-14 20:42:57 +0000658 while (1)
659 {
660#ifndef USE_POLL
661 fd_set rfds = readfds;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000662
Eric Andersen28c70b32000-06-14 20:42:57 +0000663 switch (select(maxfd, &rfds, NULL, NULL, NULL))
664#else
665 switch (poll(ufds, 2, -1))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000666#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000667 {
668 case 0:
669 /* timeout */
670 case -1:
671 /* error, ignore and/or log something, bay go to loop */
672 if (G.gotsig)
673 conescape();
674 else
675 sleep(1);
676 break;
677 default:
678
679#ifdef USE_POLL
680 if (ufds[0].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(0, &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(0, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000686
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000687 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000688 doexit(0);
689
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000690 TRACE(0, ("Read con: %d\n", len));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000691
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000692 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000693 }
694
695#ifdef USE_POLL
696 if (ufds[1].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000697#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000698 if (FD_ISSET(G.netfd, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000699#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000700 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000701 len = read(G.netfd, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000702
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000703 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000704 {
705 WriteCS(1, "Connection closed by foreign host.\r\n");
706 doexit(1);
707 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000708 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000709
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000710 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000711 }
712 }
713 }
714}