blob: c835c7a4525b2450fdc820428e4cdc551614852c [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
Eric Andersen28c70b32000-06-14 20:42:57 +000037#if 0
Rob Landleybc68cd12006-03-10 19:22:06 +000038enum { DOTRACE = 1 };
Eric Andersen28c70b32000-06-14 20:42:57 +000039#endif
40
Pavel Roskin616d13b2000-07-28 19:38:27 +000041#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000042#include <arpa/inet.h> /* for inet_ntoa()... */
43#define TRACE(x, y) do { if (x) printf y; } while (0)
44#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000045#define TRACE(x, y)
Eric Andersen28c70b32000-06-14 20:42:57 +000046#endif
47
Glenn L McGrath78b0e372001-06-26 02:06:08 +000048#define DATABUFSIZE 128
49#define IACBUFSIZE 128
Eric Andersen28c70b32000-06-14 20:42:57 +000050
Mark Whitley59ab0252001-01-23 22:30:04 +000051enum {
Rob Landleybc68cd12006-03-10 19:22:06 +000052 CHM_TRY = 0,
53 CHM_ON = 1,
54 CHM_OFF = 2,
55
56 UF_ECHO = 0x01,
57 UF_SGA = 0x02,
58
Mark Whitley59ab0252001-01-23 22:30:04 +000059 TS_0 = 1,
60 TS_IAC = 2,
61 TS_OPT = 3,
62 TS_SUB1 = 4,
63 TS_SUB2 = 5,
64};
Eric Andersen28c70b32000-06-14 20:42:57 +000065
66#define WriteCS(fd, str) write(fd, str, sizeof str -1)
67
68typedef unsigned char byte;
69
70/* use globals to reduce size ??? */ /* test this hypothesis later */
Eric Andersen92d23242001-03-19 23:49:41 +000071static struct Globalvars {
Eric Andersen28c70b32000-06-14 20:42:57 +000072 int netfd; /* console fd:s are 0 and 1 (and 2) */
73 /* same buffer used both for network and console read/write */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000074 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
Eric Andersen28c70b32000-06-14 20:42:57 +000075 byte telstate; /* telnet negotiation state from network input */
76 byte telwish; /* DO, DONT, WILL, WONT */
77 byte charmode;
78 byte telflags;
79 byte gotsig;
Paul Foxf2ddc052005-07-20 19:55:19 +000080 byte do_termios;
Eric Andersen28c70b32000-06-14 20:42:57 +000081 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000082 char iacbuf[IACBUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +000083 short iaclen; /* could even use byte */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000084 struct termios termios_def;
85 struct termios termios_raw;
Eric Andersen28c70b32000-06-14 20:42:57 +000086} G;
87
88#define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
89
90#ifdef USE_GLOBALVAR_PTR
91struct Globalvars * Gptr;
92#define G (*Gptr)
Eric Andersen28c70b32000-06-14 20:42:57 +000093#endif
94
Eric Andersencd8c4362001-11-10 11:22:46 +000095static inline void iacflush(void)
Eric Andersen28c70b32000-06-14 20:42:57 +000096{
97 write(G.netfd, G.iacbuf, G.iaclen);
98 G.iaclen = 0;
99}
100
101/* Function prototypes */
Eric Andersencd8c4362001-11-10 11:22:46 +0000102static void rawmode(void);
103static void cookmode(void);
104static void do_linemode(void);
105static void will_charmode(void);
Eric Andersen28c70b32000-06-14 20:42:57 +0000106static void telopt(byte c);
107static int subneg(byte c);
Eric Andersen28c70b32000-06-14 20:42:57 +0000108
109/* Some globals */
110static int one = 1;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000111
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000112#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000113static char *ttype;
114#endif
115
Eric Andersen539ffc92004-02-22 12:25:47 +0000116#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000117static const char *autologin;
Eric Andersen539ffc92004-02-22 12:25:47 +0000118#endif
119
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000120#ifdef CONFIG_FEATURE_AUTOWIDTH
121static int win_width, win_height;
122#endif
123
Eric Andersen28c70b32000-06-14 20:42:57 +0000124static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000125{
Eric Andersen28c70b32000-06-14 20:42:57 +0000126 cookmode();
127 exit(ev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000129
Eric Andersencd8c4362001-11-10 11:22:46 +0000130static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000131{
Eric Andersen28c70b32000-06-14 20:42:57 +0000132 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000133
Eric Andersen28c70b32000-06-14 20:42:57 +0000134 if (G.gotsig) /* came from line mode... go raw */
135 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000136
Eric Andersen28c70b32000-06-14 20:42:57 +0000137 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
138 " l go to line mode\r\n"
139 " c go to character mode\r\n"
140 " z suspend telnet\r\n"
141 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000142
Eric Andersen28c70b32000-06-14 20:42:57 +0000143 if (read(0, &b, 1) <= 0)
144 doexit(1);
145
146 switch (b)
147 {
148 case 'l':
149 if (!G.gotsig)
150 {
151 do_linemode();
152 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000153 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000154 break;
155 case 'c':
156 if (G.gotsig)
157 {
158 will_charmode();
159 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000160 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000161 break;
162 case 'z':
163 cookmode();
164 kill(0, SIGTSTP);
165 rawmode();
166 break;
167 case 'e':
168 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000169 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000170
171 WriteCS(1, "continuing...\r\n");
172
173 if (G.gotsig)
174 cookmode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000175
Eric Andersen28c70b32000-06-14 20:42:57 +0000176 rrturn:
177 G.gotsig = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000178
Eric Andersen28c70b32000-06-14 20:42:57 +0000179}
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000180static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000181{
182 /* here we could do smart tricks how to handle 0xFF:s in output
183 * stream like writing twice every sequence of FF:s (thus doing
184 * many write()s. But I think interactive telnet application does
185 * not need to be 100% 8-bit clean, so changing every 0xff:s to
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000186 * 0x7f:s
187 *
188 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
189 * I don't agree.
190 * first - I cannot use programs like sz/rz
191 * second - the 0x0D is sent as one character and if the next
192 * char is 0x0A then it's eaten by a server side.
193 * third - whay doy you have to make 'many write()s'?
194 * I don't understand.
195 * So I implemented it. It's realy useful for me. I hope that
196 * others people will find it interesting to.
197 */
Eric Andersen28c70b32000-06-14 20:42:57 +0000198
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000199 int i, j;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000200 byte * p = (byte*)G.buf;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000201 byte outbuf[4*DATABUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000202
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000203 for (i = len, j = 0; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000204 {
205 if (*p == 0x1d)
206 {
207 conescape();
208 return;
209 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000210 outbuf[j++] = *p;
Eric Andersen28c70b32000-06-14 20:42:57 +0000211 if (*p == 0xff)
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000212 outbuf[j++] = 0xff;
213 else if (*p == 0x0d)
214 outbuf[j++] = 0x00;
Eric Andersen28c70b32000-06-14 20:42:57 +0000215 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000216 if (j > 0 )
217 write(G.netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000218}
219
Eric Andersen28c70b32000-06-14 20:42:57 +0000220
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000221static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000222{
Eric Andersen28c70b32000-06-14 20:42:57 +0000223 int i;
224 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000225
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000226 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000227 {
228 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000229
Eric Andersen28c70b32000-06-14 20:42:57 +0000230 if (G.telstate == 0) /* most of the time state == 0 */
231 {
232 if (c == IAC)
233 {
234 cstart = i;
235 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000236 }
237 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000238 else
239 switch (G.telstate)
240 {
241 case TS_0:
242 if (c == IAC)
243 G.telstate = TS_IAC;
244 else
245 G.buf[cstart++] = c;
246 break;
247
248 case TS_IAC:
249 if (c == IAC) /* IAC IAC -> 0xFF */
250 {
251 G.buf[cstart++] = c;
252 G.telstate = TS_0;
253 break;
254 }
255 /* else */
256 switch (c)
257 {
258 case SB:
259 G.telstate = TS_SUB1;
260 break;
261 case DO:
262 case DONT:
263 case WILL:
264 case WONT:
265 G.telwish = c;
266 G.telstate = TS_OPT;
267 break;
268 default:
269 G.telstate = TS_0; /* DATA MARK must be added later */
270 }
271 break;
272 case TS_OPT: /* WILL, WONT, DO, DONT */
273 telopt(c);
274 G.telstate = TS_0;
275 break;
276 case TS_SUB1: /* Subnegotiation */
277 case TS_SUB2: /* Subnegotiation */
Matt Kraai1f0c4362001-12-20 23:13:26 +0000278 if (subneg(c))
Eric Andersen28c70b32000-06-14 20:42:57 +0000279 G.telstate = TS_0;
280 break;
281 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000282 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000283 if (G.telstate)
284 {
285 if (G.iaclen) iacflush();
286 if (G.telstate == TS_0) G.telstate = 0;
287
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000288 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000289 }
290
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000291 if (len)
292 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000293}
294
Eric Andersen28c70b32000-06-14 20:42:57 +0000295
296/* ******************************* */
297
298static inline void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000299{
Eric Andersen28c70b32000-06-14 20:42:57 +0000300 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000301}
302
Eric Andersen28c70b32000-06-14 20:42:57 +0000303
304static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000305{
Eric Andersen28c70b32000-06-14 20:42:57 +0000306 if (G.iaclen + 3 > IACBUFSIZE)
307 iacflush();
308
309 putiac(IAC);
310 putiac(wwdd);
311 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000312}
313
Eric Andersen28c70b32000-06-14 20:42:57 +0000314#if 0
315static void putiac1(byte c)
316{
317 if (G.iaclen + 2 > IACBUFSIZE)
318 iacflush();
319
320 putiac(IAC);
321 putiac(c);
322}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000323#endif
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000324
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000325#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000326static void putiac_subopt(byte c, char *str)
327{
328 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
329
330 if (G.iaclen + len > IACBUFSIZE)
331 iacflush();
332
333 putiac(IAC);
334 putiac(SB);
335 putiac(c);
336 putiac(0);
337
338 while(*str)
339 putiac(*str++);
340
341 putiac(IAC);
342 putiac(SE);
343}
344#endif
345
Eric Andersen539ffc92004-02-22 12:25:47 +0000346#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
347static void putiac_subopt_autologin(void)
348{
349 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
350 char *user = "USER";
351
352 if (G.iaclen + len > IACBUFSIZE)
353 iacflush();
354
355 putiac(IAC);
356 putiac(SB);
357 putiac(TELOPT_NEW_ENVIRON);
358 putiac(TELQUAL_IS);
359 putiac(NEW_ENV_VAR);
360
361 while(*user)
362 putiac(*user++);
363
364 putiac(NEW_ENV_VALUE);
365
366 while(*autologin)
367 putiac(*autologin++);
368
369 putiac(IAC);
370 putiac(SE);
371}
372#endif
373
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000374#ifdef CONFIG_FEATURE_AUTOWIDTH
375static void putiac_naws(byte c, int x, int y)
376{
377 if (G.iaclen + 9 > IACBUFSIZE)
378 iacflush();
379
380 putiac(IAC);
381 putiac(SB);
382 putiac(c);
383
384 putiac((x >> 8) & 0xff);
385 putiac(x & 0xff);
386 putiac((y >> 8) & 0xff);
387 putiac(y & 0xff);
388
389 putiac(IAC);
390 putiac(SE);
391}
392#endif
393
Eric Andersen28c70b32000-06-14 20:42:57 +0000394/* void putiacstring (subneg strings) */
395
396/* ******************************* */
397
Eric Andersen3e6ff902001-03-09 21:24:12 +0000398static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000399
Eric Andersencd8c4362001-11-10 11:22:46 +0000400static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000401{
402 if (G.telflags & UF_ECHO)
403 {
404 if (G.charmode == CHM_TRY) {
405 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000406 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000407 rawmode();
408 }
409 }
410 else
411 {
412 if (G.charmode != CHM_OFF) {
413 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000414 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000415 cookmode();
416 }
417 }
418}
419
420/* ******************************* */
421
Eric Andersencd8c4362001-11-10 11:22:46 +0000422static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000423{
424 G.charmode = CHM_TRY;
425 G.telflags |= (UF_ECHO | UF_SGA);
426 setConMode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000427
Eric Andersen28c70b32000-06-14 20:42:57 +0000428 putiac2(DO, TELOPT_ECHO);
429 putiac2(DO, TELOPT_SGA);
430 iacflush();
431}
432
Eric Andersencd8c4362001-11-10 11:22:46 +0000433static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000434{
435 G.charmode = CHM_TRY;
436 G.telflags &= ~(UF_ECHO | UF_SGA);
437 setConMode();
438
439 putiac2(DONT, TELOPT_ECHO);
440 putiac2(DONT, TELOPT_SGA);
441 iacflush();
442}
443
444/* ******************************* */
445
446static inline void to_notsup(char c)
447{
448 if (G.telwish == WILL) putiac2(DONT, c);
449 else if (G.telwish == DO) putiac2(WONT, c);
450}
451
Eric Andersencd8c4362001-11-10 11:22:46 +0000452static inline void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000453{
454 /* if server requests ECHO, don't agree */
455 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
456 else if (G.telwish == DONT) return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000457
Eric Andersen28c70b32000-06-14 20:42:57 +0000458 if (G.telflags & UF_ECHO)
459 {
460 if (G.telwish == WILL)
461 return;
462 }
463 else
464 if (G.telwish == WONT)
465 return;
466
467 if (G.charmode != CHM_OFF)
468 G.telflags ^= UF_ECHO;
469
470 if (G.telflags & UF_ECHO)
471 putiac2(DO, TELOPT_ECHO);
472 else
473 putiac2(DONT, TELOPT_ECHO);
474
475 setConMode();
476 WriteCS(1, "\r\n"); /* sudden modec */
477}
478
Eric Andersencd8c4362001-11-10 11:22:46 +0000479static inline void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000480{
481 /* daemon always sends will/wont, client do/dont */
482
483 if (G.telflags & UF_SGA)
484 {
485 if (G.telwish == WILL)
486 return;
487 }
488 else
489 if (G.telwish == WONT)
490 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000491
Eric Andersen28c70b32000-06-14 20:42:57 +0000492 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
493 putiac2(DO, TELOPT_SGA);
494 else
495 putiac2(DONT, TELOPT_SGA);
496
497 return;
498}
499
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000500#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersencd8c4362001-11-10 11:22:46 +0000501static inline void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000502{
503 /* Tell server we will (or won't) do TTYPE */
504
505 if(ttype)
506 putiac2(WILL, TELOPT_TTYPE);
507 else
508 putiac2(WONT, TELOPT_TTYPE);
509
510 return;
511}
512#endif
513
Eric Andersen539ffc92004-02-22 12:25:47 +0000514#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
515static inline void to_new_environ(void)
516{
517 /* Tell server we will (or will not) do AUTOLOGIN */
518
519 if (autologin)
520 putiac2(WILL, TELOPT_NEW_ENVIRON);
521 else
522 putiac2(WONT, TELOPT_NEW_ENVIRON);
523
524 return;
525}
526#endif
527
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000528#ifdef CONFIG_FEATURE_AUTOWIDTH
Tim Rikered8e0362002-04-26 07:53:39 +0000529static inline void to_naws(void)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000530{
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000531 /* Tell server we will do NAWS */
532 putiac2(WILL, TELOPT_NAWS);
533 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000534}
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000535#endif
536
Eric Andersen28c70b32000-06-14 20:42:57 +0000537static void telopt(byte c)
538{
539 switch (c)
540 {
Eric Andersen41634062002-04-26 08:44:17 +0000541 case TELOPT_ECHO: to_echo(); break;
542 case TELOPT_SGA: to_sga(); break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000543#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen41634062002-04-26 08:44:17 +0000544 case TELOPT_TTYPE: to_ttype();break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000545#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000546#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
547 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
548#endif
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000549#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen41634062002-04-26 08:44:17 +0000550 case TELOPT_NAWS: to_naws();
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000551 putiac_naws(c, win_width, win_height);
552 break;
553#endif
Eric Andersen8db361b2002-04-26 08:00:33 +0000554 default: to_notsup(c);
555 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000556 }
557}
558
559
560/* ******************************* */
561
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000562/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000563
564static int subneg(byte c)
565{
566 switch (G.telstate)
567 {
568 case TS_SUB1:
569 if (c == IAC)
570 G.telstate = TS_SUB2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000571#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000572 else
573 if (c == TELOPT_TTYPE)
574 putiac_subopt(TELOPT_TTYPE,ttype);
575#endif
Eric Andersen539ffc92004-02-22 12:25:47 +0000576#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
577 else
578 if (c == TELOPT_NEW_ENVIRON)
579 putiac_subopt_autologin();
580#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000581 break;
582 case TS_SUB2:
583 if (c == SE)
584 return TRUE;
585 G.telstate = TS_SUB1;
586 /* break; */
587 }
588 return FALSE;
589}
590
591/* ******************************* */
592
593static void fgotsig(int sig)
594{
595 G.gotsig = sig;
596}
597
598
Eric Andersencd8c4362001-11-10 11:22:46 +0000599static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000600{
Paul Foxf2ddc052005-07-20 19:55:19 +0000601 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000602}
Eric Andersen28c70b32000-06-14 20:42:57 +0000603
Eric Andersencd8c4362001-11-10 11:22:46 +0000604static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000605{
Paul Foxf2ddc052005-07-20 19:55:19 +0000606 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
Eric Andersen28c70b32000-06-14 20:42:57 +0000607}
608
Rob Landleydfba7412006-03-06 20:47:33 +0000609int telnet_main(int argc, char** argv)
Eric Andersen28c70b32000-06-14 20:42:57 +0000610{
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000611 int len;
Eric Andersene6dc4392003-10-31 09:31:46 +0000612 struct sockaddr_in s_in;
Eric Andersen28c70b32000-06-14 20:42:57 +0000613#ifdef USE_POLL
614 struct pollfd ufds[2];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000615#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000616 fd_set readfds;
617 int maxfd;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000618#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000619
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000620#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000621 get_terminal_width_height(0, &win_width, &win_height);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000622#endif
623
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000624#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000625 ttype = getenv("TERM");
626#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000627
628 memset(&G, 0, sizeof G);
629
Paul Foxf2ddc052005-07-20 19:55:19 +0000630 if (tcgetattr(0, &G.termios_def) >= 0) {
631 G.do_termios = 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000632
Paul Foxf2ddc052005-07-20 19:55:19 +0000633 G.termios_raw = G.termios_def;
634 cfmakeraw(&G.termios_raw);
635 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000636
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000637 if (argc < 2)
638 bb_show_usage();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000639
Eric Andersen539ffc92004-02-22 12:25:47 +0000640#ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
Paul Fox5a16a892005-11-28 16:29:45 +0000641 if (1 & bb_getopt_ulflags(argc, argv, "al:", &autologin))
Rob Landley1b751c82005-10-28 09:24:33 +0000642 autologin = getenv("USER");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000643
Eric Andersen539ffc92004-02-22 12:25:47 +0000644 if (optind < argc) {
645 bb_lookup_host(&s_in, argv[optind++]);
646 s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
647 "telnet", "tcp", 23);
648 if (optind < argc)
649 bb_show_usage();
650 } else
651 bb_show_usage();
652#else
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000653 bb_lookup_host(&s_in, argv[1]);
Glenn L McGrath036dbaa2004-01-17 05:03:31 +0000654 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
Eric Andersen539ffc92004-02-22 12:25:47 +0000655#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000656
Eric Andersene6dc4392003-10-31 09:31:46 +0000657 G.netfd = xconnect(&s_in);
Eric Andersen28c70b32000-06-14 20:42:57 +0000658
Eric Andersen0b315862002-07-03 11:51:44 +0000659 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
Eric Andersen28c70b32000-06-14 20:42:57 +0000660
661 signal(SIGINT, fgotsig);
662
663#ifdef USE_POLL
664 ufds[0].fd = 0; ufds[1].fd = G.netfd;
665 ufds[0].events = ufds[1].events = POLLIN;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000666#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000667 FD_ZERO(&readfds);
668 FD_SET(0, &readfds);
669 FD_SET(G.netfd, &readfds);
670 maxfd = G.netfd + 1;
671#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000672
Eric Andersen28c70b32000-06-14 20:42:57 +0000673 while (1)
674 {
675#ifndef USE_POLL
676 fd_set rfds = readfds;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000677
Eric Andersen28c70b32000-06-14 20:42:57 +0000678 switch (select(maxfd, &rfds, NULL, NULL, NULL))
679#else
680 switch (poll(ufds, 2, -1))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000681#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000682 {
683 case 0:
684 /* timeout */
685 case -1:
686 /* error, ignore and/or log something, bay go to loop */
687 if (G.gotsig)
688 conescape();
689 else
690 sleep(1);
691 break;
692 default:
693
694#ifdef USE_POLL
695 if (ufds[0].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000696#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000697 if (FD_ISSET(0, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000698#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000699 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000700 len = read(0, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000701
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000702 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000703 doexit(0);
704
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000705 TRACE(0, ("Read con: %d\n", len));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000706
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000707 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000708 }
709
710#ifdef USE_POLL
711 if (ufds[1].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000712#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000713 if (FD_ISSET(G.netfd, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000714#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000715 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000716 len = read(G.netfd, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000717
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000718 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000719 {
720 WriteCS(1, "Connection closed by foreign host.\r\n");
721 doexit(1);
722 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000723 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000724
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000725 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000726 }
727 }
728 }
729}