blob: caca89d2d00ce271d7ff33835b4b0b2240d8fdad [file] [log] [blame]
Eric Andersen28c70b32000-06-14 20:42:57 +00001/* vi: set sw=4 ts=4: */
Erik Andersenf7c49ef2000-02-22 17:17:45 +00002/*
Eric Andersen28c70b32000-06-14 20:42:57 +00003 * telnet implementation for busybox
Erik Andersenf7c49ef2000-02-22 17:17:45 +00004 *
Eric Andersen28c70b32000-06-14 20:42:57 +00005 * Author: Tomi Ollila <too@iki.fi>
6 * Copyright (C) 1994-2000 by Tomi Ollila
7 *
8 * Created: Thu Apr 7 13:29:41 1994 too
9 * Last modified: Fri Jun 9 14:34:24 2000 too
Erik Andersenf7c49ef2000-02-22 17:17:45 +000010 *
Rob Landley1b751c82005-10-28 09:24:33 +000011 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Erik Andersenf7c49ef2000-02-22 17:17:45 +000012 *
Eric Andersen28c70b32000-06-14 20:42:57 +000013 * HISTORY
14 * Revision 3.1 1994/04/17 11:31:54 too
15 * initial revision
Eric Andersencb81e642003-07-14 21:21:08 +000016 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
Eric Andersen7e1273e2001-05-07 17:57:45 +000017 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
18 * <jam@ltsp.org>
Eric Andersen539ffc92004-02-22 12:25:47 +000019 * Modified 2004/02/11 to add ability to pass the USER variable to remote host
20 * by Fernando Silveira <swrh@gmx.net>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000021 *
Erik Andersenf7c49ef2000-02-22 17:17:45 +000022 */
23
Erik Andersenf7c49ef2000-02-22 17:17:45 +000024#include <termios.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000025#include <arpa/telnet.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000026#include <netinet/in.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000027#include "libbb.h"
Erik Andersenf7c49ef2000-02-22 17:17:45 +000028
Pavel Roskin616d13b2000-07-28 19:38:27 +000029#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000030#define TRACE(x, y) do { if (x) printf y; } while (0)
31#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000032#define TRACE(x, y)
Eric Andersen28c70b32000-06-14 20:42:57 +000033#endif
34
Mark Whitley59ab0252001-01-23 22:30:04 +000035enum {
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000036 DATABUFSIZE = 128,
37 IACBUFSIZE = 128,
38
Rob Landleybc68cd12006-03-10 19:22:06 +000039 CHM_TRY = 0,
40 CHM_ON = 1,
41 CHM_OFF = 2,
42
43 UF_ECHO = 0x01,
44 UF_SGA = 0x02,
45
Mark Whitley59ab0252001-01-23 22:30:04 +000046 TS_0 = 1,
47 TS_IAC = 2,
48 TS_OPT = 3,
49 TS_SUB1 = 4,
50 TS_SUB2 = 5,
51};
Eric Andersen28c70b32000-06-14 20:42:57 +000052
Eric Andersen28c70b32000-06-14 20:42:57 +000053typedef unsigned char byte;
54
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000055
56struct globals {
57 int netfd; /* console fd:s are 0 and 1 (and 2) */
58 short iaclen; /* could even use byte */
Eric Andersen28c70b32000-06-14 20:42:57 +000059 byte telstate; /* telnet negotiation state from network input */
60 byte telwish; /* DO, DONT, WILL, WONT */
61 byte charmode;
62 byte telflags;
63 byte gotsig;
Paul Foxf2ddc052005-07-20 19:55:19 +000064 byte do_termios;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000065#if ENABLE_FEATURE_TELNET_TTYPE
66 char *ttype;
67#endif
68#if ENABLE_FEATURE_TELNET_AUTOLOGIN
69 const char *autologin;
70#endif
71#if ENABLE_FEATURE_AUTOWIDTH
72 int win_width, win_height;
73#endif
74 /* same buffer used both for network and console read/write */
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +000075 char buf[DATABUFSIZE];
76 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000077 char iacbuf[IACBUFSIZE];
Eric Andersenc7bda1c2004-03-15 08:29:22 +000078 struct termios termios_def;
79 struct termios termios_raw;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000080};
Eric Andersen28c70b32000-06-14 20:42:57 +000081
Denis Vlasenko4e5f82c2007-06-03 22:30:22 +000082#define G (*(struct globals*)&bb_common_bufsiz1)
Eric Andersen28c70b32000-06-14 20:42:57 +000083
Eric Andersen28c70b32000-06-14 20:42:57 +000084
85/* Function prototypes */
Eric Andersencd8c4362001-11-10 11:22:46 +000086static void rawmode(void);
87static void cookmode(void);
88static void do_linemode(void);
89static void will_charmode(void);
Eric Andersen28c70b32000-06-14 20:42:57 +000090static void telopt(byte c);
91static int subneg(byte c);
Eric Andersen28c70b32000-06-14 20:42:57 +000092
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000093static void iacflush(void)
94{
95 write(G.netfd, G.iacbuf, G.iaclen);
96 G.iaclen = 0;
97}
Eric Andersen7e1273e2001-05-07 17:57:45 +000098
Denis Vlasenkof24cdf12007-03-19 14:47:09 +000099#define write_str(fd, str) write(fd, str, sizeof(str) - 1)
Eric Andersen539ffc92004-02-22 12:25:47 +0000100
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000101static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000102{
Eric Andersen28c70b32000-06-14 20:42:57 +0000103 cookmode();
104 exit(ev);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000105}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000106
Eric Andersencd8c4362001-11-10 11:22:46 +0000107static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000108{
Eric Andersen28c70b32000-06-14 20:42:57 +0000109 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000110
Eric Andersen28c70b32000-06-14 20:42:57 +0000111 if (G.gotsig) /* came from line mode... go raw */
112 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000113
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000114 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
Eric Andersen28c70b32000-06-14 20:42:57 +0000115 " l go to line mode\r\n"
116 " c go to character mode\r\n"
117 " z suspend telnet\r\n"
118 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000119
Eric Andersen28c70b32000-06-14 20:42:57 +0000120 if (read(0, &b, 1) <= 0)
121 doexit(1);
122
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000123 switch (b) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000124 case 'l':
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000125 if (!G.gotsig) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000126 do_linemode();
127 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000128 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000129 break;
130 case 'c':
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000131 if (G.gotsig) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000132 will_charmode();
133 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000134 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000135 break;
136 case 'z':
137 cookmode();
138 kill(0, SIGTSTP);
139 rawmode();
140 break;
141 case 'e':
142 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000143 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000144
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000145 write_str(1, "continuing...\r\n");
Eric Andersen28c70b32000-06-14 20:42:57 +0000146
147 if (G.gotsig)
148 cookmode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000149
Eric Andersen28c70b32000-06-14 20:42:57 +0000150 rrturn:
151 G.gotsig = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000152
Eric Andersen28c70b32000-06-14 20:42:57 +0000153}
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000154
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000155static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000156{
157 /* here we could do smart tricks how to handle 0xFF:s in output
158 * stream like writing twice every sequence of FF:s (thus doing
159 * many write()s. But I think interactive telnet application does
160 * not need to be 100% 8-bit clean, so changing every 0xff:s to
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000161 * 0x7f:s
162 *
163 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
164 * I don't agree.
165 * first - I cannot use programs like sz/rz
166 * second - the 0x0D is sent as one character and if the next
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000167 * char is 0x0A then it's eaten by a server side.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000168 * third - whay doy you have to make 'many write()s'?
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000169 * I don't understand.
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000170 * So I implemented it. It's realy useful for me. I hope that
171 * others people will find it interesting to.
172 */
Eric Andersen28c70b32000-06-14 20:42:57 +0000173
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000174 int i, j;
Eric Andersen0cb6f352006-01-30 22:30:41 +0000175 byte * p = (byte*)G.buf;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000176 byte outbuf[4*DATABUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000177
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000178 for (i = len, j = 0; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000179 {
180 if (*p == 0x1d)
181 {
182 conescape();
183 return;
184 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000185 outbuf[j++] = *p;
Eric Andersen28c70b32000-06-14 20:42:57 +0000186 if (*p == 0xff)
Denis Vlasenko92758142006-10-03 19:56:34 +0000187 outbuf[j++] = 0xff;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000188 else if (*p == 0x0d)
Denis Vlasenko92758142006-10-03 19:56:34 +0000189 outbuf[j++] = 0x00;
Eric Andersen28c70b32000-06-14 20:42:57 +0000190 }
Denis Vlasenko219d14d2007-03-24 15:40:16 +0000191 if (j > 0)
Denis Vlasenko92758142006-10-03 19:56:34 +0000192 write(G.netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000193}
194
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000195static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000196{
Eric Andersen28c70b32000-06-14 20:42:57 +0000197 int i;
198 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000199
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000200 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000201 {
202 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000203
Eric Andersen28c70b32000-06-14 20:42:57 +0000204 if (G.telstate == 0) /* most of the time state == 0 */
205 {
206 if (c == IAC)
207 {
208 cstart = i;
209 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000210 }
211 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000212 else
213 switch (G.telstate)
214 {
215 case TS_0:
216 if (c == IAC)
217 G.telstate = TS_IAC;
218 else
219 G.buf[cstart++] = c;
220 break;
221
222 case TS_IAC:
223 if (c == IAC) /* IAC IAC -> 0xFF */
224 {
225 G.buf[cstart++] = c;
226 G.telstate = TS_0;
227 break;
228 }
229 /* else */
230 switch (c)
231 {
232 case SB:
233 G.telstate = TS_SUB1;
234 break;
235 case DO:
236 case DONT:
237 case WILL:
238 case WONT:
239 G.telwish = c;
240 G.telstate = TS_OPT;
241 break;
242 default:
243 G.telstate = TS_0; /* DATA MARK must be added later */
244 }
245 break;
246 case TS_OPT: /* WILL, WONT, DO, DONT */
247 telopt(c);
248 G.telstate = TS_0;
249 break;
250 case TS_SUB1: /* Subnegotiation */
251 case TS_SUB2: /* Subnegotiation */
Matt Kraai1f0c4362001-12-20 23:13:26 +0000252 if (subneg(c))
Eric Andersen28c70b32000-06-14 20:42:57 +0000253 G.telstate = TS_0;
254 break;
255 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000256 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000257 if (G.telstate)
258 {
259 if (G.iaclen) iacflush();
260 if (G.telstate == TS_0) G.telstate = 0;
261
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000262 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000263 }
264
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000265 if (len)
266 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000267}
268
Rob Landley88621d72006-08-29 19:41:06 +0000269static void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000270{
Eric Andersen28c70b32000-06-14 20:42:57 +0000271 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000272}
273
Eric Andersen28c70b32000-06-14 20:42:57 +0000274static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000275{
Eric Andersen28c70b32000-06-14 20:42:57 +0000276 if (G.iaclen + 3 > IACBUFSIZE)
277 iacflush();
278
279 putiac(IAC);
280 putiac(wwdd);
281 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000282}
283
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000284#if ENABLE_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000285static void putiac_subopt(byte c, char *str)
286{
287 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
288
289 if (G.iaclen + len > IACBUFSIZE)
290 iacflush();
291
292 putiac(IAC);
293 putiac(SB);
294 putiac(c);
295 putiac(0);
296
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000297 while (*str)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000298 putiac(*str++);
299
300 putiac(IAC);
301 putiac(SE);
302}
303#endif
304
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000305#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Eric Andersen539ffc92004-02-22 12:25:47 +0000306static void putiac_subopt_autologin(void)
307{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000308 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000309 const char *user = "USER";
Eric Andersen539ffc92004-02-22 12:25:47 +0000310
311 if (G.iaclen + len > IACBUFSIZE)
312 iacflush();
313
314 putiac(IAC);
315 putiac(SB);
316 putiac(TELOPT_NEW_ENVIRON);
317 putiac(TELQUAL_IS);
318 putiac(NEW_ENV_VAR);
319
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000320 while (*user)
Eric Andersen539ffc92004-02-22 12:25:47 +0000321 putiac(*user++);
322
323 putiac(NEW_ENV_VALUE);
324
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000325 while (*G.autologin)
326 putiac(*G.autologin++);
Eric Andersen539ffc92004-02-22 12:25:47 +0000327
328 putiac(IAC);
329 putiac(SE);
330}
331#endif
332
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000333#if ENABLE_FEATURE_AUTOWIDTH
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000334static void putiac_naws(byte c, int x, int y)
335{
336 if (G.iaclen + 9 > IACBUFSIZE)
337 iacflush();
338
339 putiac(IAC);
340 putiac(SB);
341 putiac(c);
342
343 putiac((x >> 8) & 0xff);
344 putiac(x & 0xff);
345 putiac((y >> 8) & 0xff);
346 putiac(y & 0xff);
347
348 putiac(IAC);
349 putiac(SE);
350}
351#endif
352
Eric Andersen3e6ff902001-03-09 21:24:12 +0000353static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000354
Eric Andersencd8c4362001-11-10 11:22:46 +0000355static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000356{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000357 if (G.telflags & UF_ECHO) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000358 if (G.charmode == CHM_TRY) {
359 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000360 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000361 rawmode();
362 }
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000363 } else {
Eric Andersen28c70b32000-06-14 20:42:57 +0000364 if (G.charmode != CHM_OFF) {
365 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000366 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000367 cookmode();
368 }
369 }
370}
371
Eric Andersencd8c4362001-11-10 11:22:46 +0000372static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000373{
374 G.charmode = CHM_TRY;
375 G.telflags |= (UF_ECHO | UF_SGA);
376 setConMode();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000377
Eric Andersen28c70b32000-06-14 20:42:57 +0000378 putiac2(DO, TELOPT_ECHO);
379 putiac2(DO, TELOPT_SGA);
380 iacflush();
381}
382
Eric Andersencd8c4362001-11-10 11:22:46 +0000383static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000384{
385 G.charmode = CHM_TRY;
386 G.telflags &= ~(UF_ECHO | UF_SGA);
387 setConMode();
388
389 putiac2(DONT, TELOPT_ECHO);
390 putiac2(DONT, TELOPT_SGA);
391 iacflush();
392}
393
Rob Landley88621d72006-08-29 19:41:06 +0000394static void to_notsup(char c)
Eric Andersen28c70b32000-06-14 20:42:57 +0000395{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000396 if (G.telwish == WILL)
397 putiac2(DONT, c);
398 else if (G.telwish == DO)
399 putiac2(WONT, c);
Eric Andersen28c70b32000-06-14 20:42:57 +0000400}
401
Rob Landley88621d72006-08-29 19:41:06 +0000402static void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000403{
404 /* if server requests ECHO, don't agree */
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000405 if (G.telwish == DO) {
406 putiac2(WONT, TELOPT_ECHO);
407 return;
408 }
409 if (G.telwish == DONT)
410 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000411
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000412 if (G.telflags & UF_ECHO) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000413 if (G.telwish == WILL)
414 return;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000415 } else if (G.telwish == WONT)
416 return;
Eric Andersen28c70b32000-06-14 20:42:57 +0000417
418 if (G.charmode != CHM_OFF)
419 G.telflags ^= UF_ECHO;
420
421 if (G.telflags & UF_ECHO)
422 putiac2(DO, TELOPT_ECHO);
423 else
424 putiac2(DONT, TELOPT_ECHO);
425
426 setConMode();
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000427 write_str(1, "\r\n"); /* sudden modec */
Eric Andersen28c70b32000-06-14 20:42:57 +0000428}
429
Rob Landley88621d72006-08-29 19:41:06 +0000430static void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000431{
432 /* daemon always sends will/wont, client do/dont */
433
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000434 if (G.telflags & UF_SGA) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000435 if (G.telwish == WILL)
436 return;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000437 } else if (G.telwish == WONT)
438 return;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000439
Eric Andersen28c70b32000-06-14 20:42:57 +0000440 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
441 putiac2(DO, TELOPT_SGA);
442 else
443 putiac2(DONT, TELOPT_SGA);
Eric Andersen28c70b32000-06-14 20:42:57 +0000444}
445
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000446#if ENABLE_FEATURE_TELNET_TTYPE
Rob Landley88621d72006-08-29 19:41:06 +0000447static void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000448{
449 /* Tell server we will (or won't) do TTYPE */
450
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000451 if (G.ttype)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000452 putiac2(WILL, TELOPT_TTYPE);
453 else
454 putiac2(WONT, TELOPT_TTYPE);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000455}
456#endif
457
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000458#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Rob Landley88621d72006-08-29 19:41:06 +0000459static void to_new_environ(void)
Eric Andersen539ffc92004-02-22 12:25:47 +0000460{
461 /* Tell server we will (or will not) do AUTOLOGIN */
462
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000463 if (G.autologin)
Eric Andersen539ffc92004-02-22 12:25:47 +0000464 putiac2(WILL, TELOPT_NEW_ENVIRON);
465 else
466 putiac2(WONT, TELOPT_NEW_ENVIRON);
Eric Andersen539ffc92004-02-22 12:25:47 +0000467}
468#endif
469
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000470#if ENABLE_FEATURE_AUTOWIDTH
Rob Landley88621d72006-08-29 19:41:06 +0000471static void to_naws(void)
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000472{
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000473 /* Tell server we will do NAWS */
474 putiac2(WILL, TELOPT_NAWS);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000475}
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000476#endif
477
Eric Andersen28c70b32000-06-14 20:42:57 +0000478static void telopt(byte c)
479{
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000480 switch (c) {
481 case TELOPT_ECHO:
482 to_echo(); break;
483 case TELOPT_SGA:
484 to_sga(); break;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000485#if ENABLE_FEATURE_TELNET_TTYPE
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000486 case TELOPT_TTYPE:
487 to_ttype(); break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000488#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000489#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000490 case TELOPT_NEW_ENVIRON:
491 to_new_environ(); break;
Eric Andersen539ffc92004-02-22 12:25:47 +0000492#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000493#if ENABLE_FEATURE_AUTOWIDTH
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000494 case TELOPT_NAWS:
495 to_naws();
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000496 putiac_naws(c, G.win_width, G.win_height);
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000497 break;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000498#endif
Denis Vlasenkobaca1752007-03-11 13:43:10 +0000499 default:
500 to_notsup(c);
501 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000502 }
503}
504
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000505/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000506static int subneg(byte c)
507{
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000508 switch (G.telstate) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000509 case TS_SUB1:
510 if (c == IAC)
511 G.telstate = TS_SUB2;
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000512#if ENABLE_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000513 else
514 if (c == TELOPT_TTYPE)
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000515 putiac_subopt(TELOPT_TTYPE, G.ttype);
Eric Andersen7e1273e2001-05-07 17:57:45 +0000516#endif
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000517#if ENABLE_FEATURE_TELNET_AUTOLOGIN
Eric Andersen539ffc92004-02-22 12:25:47 +0000518 else
519 if (c == TELOPT_NEW_ENVIRON)
520 putiac_subopt_autologin();
521#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000522 break;
523 case TS_SUB2:
524 if (c == SE)
525 return TRUE;
526 G.telstate = TS_SUB1;
527 /* break; */
528 }
529 return FALSE;
530}
531
Eric Andersen28c70b32000-06-14 20:42:57 +0000532static void fgotsig(int sig)
533{
534 G.gotsig = sig;
535}
536
537
Eric Andersencd8c4362001-11-10 11:22:46 +0000538static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000539{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000540 if (G.do_termios)
541 tcsetattr(0, TCSADRAIN, &G.termios_raw);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000542}
Eric Andersen28c70b32000-06-14 20:42:57 +0000543
Eric Andersencd8c4362001-11-10 11:22:46 +0000544static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000545{
Denis Vlasenko54e3d1f2007-03-19 14:52:26 +0000546 if (G.do_termios)
547 tcsetattr(0, TCSADRAIN, &G.termios_def);
Eric Andersen28c70b32000-06-14 20:42:57 +0000548}
549
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000550void BUG_telnet_globals_too_big(void);
551
Denis Vlasenko06af2162007-02-03 17:28:39 +0000552int telnet_main(int argc, char** argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000553int telnet_main(int argc, char** argv)
Eric Andersen28c70b32000-06-14 20:42:57 +0000554{
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000555 char *host;
556 int port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000557 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000558#ifdef USE_POLL
559 struct pollfd ufds[2];
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000560#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000561 fd_set readfds;
562 int maxfd;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000563#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000564
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000565 if (sizeof(G) > sizeof(bb_common_bufsiz1))
566 BUG_telnet_globals_too_big();
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000567 /* memset(&G, 0, sizeof G); - already is */
Eric Andersen28c70b32000-06-14 20:42:57 +0000568
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000569#if ENABLE_FEATURE_AUTOWIDTH
570 get_terminal_width_height(0, &G.win_width, &G.win_height);
571#endif
572
573#if ENABLE_FEATURE_TELNET_TTYPE
574 G.ttype = getenv("TERM");
575#endif
576
Paul Foxf2ddc052005-07-20 19:55:19 +0000577 if (tcgetattr(0, &G.termios_def) >= 0) {
578 G.do_termios = 1;
Paul Foxf2ddc052005-07-20 19:55:19 +0000579 G.termios_raw = G.termios_def;
580 cfmakeraw(&G.termios_raw);
581 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000582
Glenn L McGrathffccf6e2003-12-20 01:47:18 +0000583 if (argc < 2)
584 bb_show_usage();
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000585
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000586#if ENABLE_FEATURE_TELNET_AUTOLOGIN
587 if (1 & getopt32(argc, argv, "al:", &G.autologin))
588 G.autologin = getenv("USER");
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000589 argv += optind;
Eric Andersen539ffc92004-02-22 12:25:47 +0000590#else
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000591 argv++;
Eric Andersen539ffc92004-02-22 12:25:47 +0000592#endif
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000593 if (!*argv)
594 bb_show_usage();
595 host = *argv++;
596 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
597 if (*argv) /* extra params?? */
598 bb_show_usage();
599
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000600 G.netfd = create_and_connect_stream_or_die(host, port);
Eric Andersen28c70b32000-06-14 20:42:57 +0000601
Denis Vlasenko703e2022007-01-22 14:12:08 +0000602 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
Eric Andersen28c70b32000-06-14 20:42:57 +0000603
604 signal(SIGINT, fgotsig);
605
606#ifdef USE_POLL
607 ufds[0].fd = 0; ufds[1].fd = G.netfd;
608 ufds[0].events = ufds[1].events = POLLIN;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000609#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000610 FD_ZERO(&readfds);
611 FD_SET(0, &readfds);
612 FD_SET(G.netfd, &readfds);
613 maxfd = G.netfd + 1;
614#endif
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000615
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000616 while (1) {
Eric Andersen28c70b32000-06-14 20:42:57 +0000617#ifndef USE_POLL
618 fd_set rfds = readfds;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000619
Eric Andersen28c70b32000-06-14 20:42:57 +0000620 switch (select(maxfd, &rfds, NULL, NULL, NULL))
621#else
622 switch (poll(ufds, 2, -1))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000623#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000624 {
625 case 0:
626 /* timeout */
627 case -1:
628 /* error, ignore and/or log something, bay go to loop */
629 if (G.gotsig)
630 conescape();
631 else
632 sleep(1);
633 break;
634 default:
635
636#ifdef USE_POLL
637 if (ufds[0].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000638#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000639 if (FD_ISSET(0, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000640#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000641 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000642 len = read(0, G.buf, DATABUFSIZE);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000643 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000644 doexit(0);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000645 TRACE(0, ("Read con: %d\n", len));
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000646 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000647 }
648
649#ifdef USE_POLL
650 if (ufds[1].revents) /* well, should check POLLIN, but ... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000651#else
Eric Andersen28c70b32000-06-14 20:42:57 +0000652 if (FD_ISSET(G.netfd, &rfds))
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000653#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000654 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000655 len = read(G.netfd, G.buf, DATABUFSIZE);
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000656 if (len <= 0) {
Denis Vlasenkof24cdf12007-03-19 14:47:09 +0000657 write_str(1, "Connection closed by foreign host\r\n");
Eric Andersen28c70b32000-06-14 20:42:57 +0000658 doexit(1);
659 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000660 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000661 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000662 }
663 }
664 }
665}