blob: da7911fcc88156807f4ff5a5c0e61d02006b70e2 [file] [log] [blame]
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00002/*
Eric Andersen08a72202002-09-30 20:52:10 +00003 * Simple telnet server
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
5 *
Rob Landley1ec5b292006-05-29 07:42:02 +00006 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen08a72202002-09-30 20:52:10 +00007 *
8 * ---------------------------------------------------------------------------
9 * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
10 ****************************************************************************
11 *
12 * The telnetd manpage says it all:
13 *
14 * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
15 * a client, then creating a login process which has the slave side of the
16 * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
17 * master side of the pseudo-terminal, implementing the telnet protocol and
18 * passing characters between the remote client and the login process.
19 *
20 * Vladimir Oleynik <dzo@simtreas.ru> 2001
21 * Set process group corrections, initial busybox port
22 */
23
24/*#define DEBUG 1 */
Mike Frysinger772a3462006-05-10 17:14:32 +000025#undef DEBUG
Eric Andersen08a72202002-09-30 20:52:10 +000026
Rob Landley099ed502006-08-28 09:41:49 +000027#include "busybox.h"
28
Eric Andersen08a72202002-09-30 20:52:10 +000029#ifdef DEBUG
30#define TELCMDS
31#define TELOPTS
32#endif
33#include <arpa/telnet.h>
Eric Andersen08a72202002-09-30 20:52:10 +000034#include <sys/syslog.h>
35
Eric Andersen08a72202002-09-30 20:52:10 +000036
37#define BUFSIZE 4000
38
Rob Landley00e76cb2005-05-10 23:53:33 +000039#ifdef CONFIG_FEATURE_IPV6
40#define SOCKET_TYPE AF_INET6
41typedef struct sockaddr_in6 sockaddr_type;
42#else
43#define SOCKET_TYPE AF_INET
44typedef struct sockaddr_in sockaddr_type;
45#endif
46
47
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000048#ifdef CONFIG_LOGIN
Glenn L McGrathd4004ee2004-09-14 17:24:59 +000049static const char *loginpath = "/bin/login";
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000050#else
Glenn L McGrathd4004ee2004-09-14 17:24:59 +000051static const char *loginpath;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000052#endif
53static const char *issuefile = "/etc/issue.net";
Eric Andersen08a72202002-09-30 20:52:10 +000054
55/* shell name and arguments */
56
57static const char *argv_init[] = {NULL, NULL};
58
59/* structure that describes a session */
60
61struct tsession {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000062#ifdef CONFIG_FEATURE_TELNETD_INETD
63 int sockfd_read, sockfd_write, ptyfd;
64#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +000065 struct tsession *next;
66 int sockfd, ptyfd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000067#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +000068 int shell_pid;
69 /* two circular buffers */
70 char *buf1, *buf2;
71 int rdidx1, wridx1, size1;
72 int rdidx2, wridx2, size2;
73};
74
75/*
76
77 This is how the buffers are used. The arrows indicate the movement
78 of data.
79
80 +-------+ wridx1++ +------+ rdidx1++ +----------+
81 | | <-------------- | buf1 | <-------------- | |
82 | | size1-- +------+ size1++ | |
Mike Frysingerd2c8fd62006-05-10 17:17:09 +000083 | pty | | socket |
Eric Andersen08a72202002-09-30 20:52:10 +000084 | | rdidx2++ +------+ wridx2++ | |
85 | | --------------> | buf2 | --------------> | |
86 +-------+ size2++ +------+ size2-- +----------+
87
88 Each session has got two buffers.
89
90*/
91
92static int maxfd;
93
94static struct tsession *sessions;
95
96
97/*
98
Bernhard Reutner-Fischere0387a62006-06-07 13:31:59 +000099 Remove all IAC's from the buffer pointed to by bf (received IACs are ignored
Eric Andersen08a72202002-09-30 20:52:10 +0000100 and must be removed so as to not be interpreted by the terminal). Make an
101 uninterrupted string of characters fit for the terminal. Do this by packing
102 all characters meant for the terminal sequentially towards the end of bf.
103
104 Return a pointer to the beginning of the characters meant for the terminal.
105 and make *num_totty the number of characters that should be sent to
106 the terminal.
107
108 Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
109 past (bf + len) then that IAC will be left unprocessed and *processed will be
110 less than len.
111
112 FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
113 what is the escape character? We aren't handling that situation here.
114
Eric Andersen3752d332003-12-19 11:30:13 +0000115 CR-LF ->'s CR mapping is also done here, for convenience
116
Eric Andersen08a72202002-09-30 20:52:10 +0000117 */
118static char *
119remove_iacs(struct tsession *ts, int *pnum_totty) {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000120 unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1;
Eric Andersen08a72202002-09-30 20:52:10 +0000121 unsigned char *ptr = ptr0;
122 unsigned char *totty = ptr;
123 unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
124 int processed;
125 int num_totty;
126
127 while (ptr < end) {
128 if (*ptr != IAC) {
Eric Andersen3752d332003-12-19 11:30:13 +0000129 int c = *ptr;
Eric Andersen08a72202002-09-30 20:52:10 +0000130 *totty++ = *ptr++;
Eric Andersen3752d332003-12-19 11:30:13 +0000131 /* We now map \r\n ==> \r for pragmatic reasons.
132 * Many client implementations send \r\n when
133 * the user hits the CarriageReturn key.
134 */
135 if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
136 ptr++;
Eric Andersen08a72202002-09-30 20:52:10 +0000137 }
138 else {
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000139 /*
140 * TELOPT_NAWS support!
141 */
142 if ((ptr+2) >= end) {
Eric Andersen08a72202002-09-30 20:52:10 +0000143 /* only the beginning of the IAC is in the
144 buffer we were asked to process, we can't
145 process this char. */
146 break;
147 }
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000148
149 /*
150 * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
151 */
152 else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
153 struct winsize ws;
154 if ((ptr+8) >= end)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000155 break; /* incomplete, can't process */
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000156 ws.ws_col = (ptr[3] << 8) | ptr[4];
157 ws.ws_row = (ptr[5] << 8) | ptr[6];
158 (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
159 ptr += 9;
160 }
161 else {
162 /* skip 3-byte IAC non-SB cmd */
163#ifdef DEBUG
164 fprintf(stderr, "Ignoring IAC %s,%s\n",
165 TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
166#endif
167 ptr += 3;
168 }
Eric Andersen08a72202002-09-30 20:52:10 +0000169 }
170 }
171
172 processed = ptr - ptr0;
173 num_totty = totty - ptr0;
174 /* the difference between processed and num_to tty
175 is all the iacs we removed from the stream.
176 Adjust buf1 accordingly. */
177 ts->wridx1 += processed - num_totty;
178 ts->size1 -= processed - num_totty;
179 *pnum_totty = num_totty;
180 /* move the chars meant for the terminal towards the end of the
181 buffer. */
182 return memmove(ptr - num_totty, ptr0, num_totty);
183}
184
185
186static int
187getpty(char *line)
188{
189 int p;
Glenn L McGrathf234e7c2002-11-10 22:26:19 +0000190#ifdef CONFIG_FEATURE_DEVPTS
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000191 p = open("/dev/ptmx", O_RDWR);
Eric Andersen08a72202002-09-30 20:52:10 +0000192 if (p > 0) {
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000193 const char *name;
Eric Andersen08a72202002-09-30 20:52:10 +0000194 grantpt(p);
195 unlockpt(p);
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000196 name = ptsname(p);
197 if (!name) {
198 bb_perror_msg("ptsname error (is /dev/pts mounted?)");
199 return -1;
200 }
201 strcpy(line, name);
202 return p;
Eric Andersen08a72202002-09-30 20:52:10 +0000203 }
204#else
205 struct stat stb;
206 int i;
207 int j;
208
209 strcpy(line, "/dev/ptyXX");
210
211 for (i = 0; i < 16; i++) {
212 line[8] = "pqrstuvwxyzabcde"[i];
213 line[9] = '0';
214 if (stat(line, &stb) < 0) {
215 continue;
216 }
217 for (j = 0; j < 16; j++) {
218 line[9] = j < 10 ? j + '0' : j - 10 + 'a';
Mike Frysinger772a3462006-05-10 17:14:32 +0000219#ifdef DEBUG
220 fprintf(stderr, "Trying to open device: %s\n", line);
221#endif
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000222 p = open(line, O_RDWR | O_NOCTTY);
223 if (p >= 0) {
Eric Andersen08a72202002-09-30 20:52:10 +0000224 line[5] = 't';
225 return p;
226 }
227 }
228 }
Glenn L McGrathf234e7c2002-11-10 22:26:19 +0000229#endif /* CONFIG_FEATURE_DEVPTS */
Eric Andersen08a72202002-09-30 20:52:10 +0000230 return -1;
231}
232
233
234static void
235send_iac(struct tsession *ts, unsigned char command, int option)
236{
237 /* We rely on that there is space in the buffer for now. */
238 char *b = ts->buf2 + ts->rdidx2;
239 *b++ = IAC;
240 *b++ = command;
241 *b++ = option;
242 ts->rdidx2 += 3;
243 ts->size2 += 3;
244}
245
246
247static struct tsession *
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000248#ifdef CONFIG_FEATURE_TELNETD_INETD
249make_new_session(void)
250#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000251make_new_session(int sockfd)
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000252#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000253{
254 struct termios termbuf;
255 int pty, pid;
256 char tty_name[32];
Rob Landley1ec5b292006-05-29 07:42:02 +0000257 struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
Eric Andersen08a72202002-09-30 20:52:10 +0000258
259 ts->buf1 = (char *)(&ts[1]);
260 ts->buf2 = ts->buf1 + BUFSIZE;
261
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000262#ifdef CONFIG_FEATURE_TELNETD_INETD
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000263 ts->sockfd_write = 1;
264#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000265 ts->sockfd = sockfd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000266#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000267
Eric Andersen08a72202002-09-30 20:52:10 +0000268 /* Got a new connection, set up a tty and spawn a shell. */
269
270 pty = getpty(tty_name);
271
272 if (pty < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000273 bb_error_msg("all terminals in use");
Eric Andersen08a72202002-09-30 20:52:10 +0000274 return 0;
275 }
276
277 if (pty > maxfd)
278 maxfd = pty;
279
280 ts->ptyfd = pty;
281
282 /* Make the telnet client understand we will echo characters so it
283 * should not do it locally. We don't tell the client to run linemode,
284 * because we want to handle line editing and tab completion and other
285 * stuff that requires char-by-char support.
286 */
287
288 send_iac(ts, DO, TELOPT_ECHO);
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000289 send_iac(ts, DO, TELOPT_NAWS);
Eric Andersen08a72202002-09-30 20:52:10 +0000290 send_iac(ts, DO, TELOPT_LFLOW);
291 send_iac(ts, WILL, TELOPT_ECHO);
292 send_iac(ts, WILL, TELOPT_SGA);
293
Eric Andersen08a72202002-09-30 20:52:10 +0000294 if ((pid = fork()) < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000295 bb_perror_msg("fork");
Eric Andersen08a72202002-09-30 20:52:10 +0000296 }
297 if (pid == 0) {
298 /* In child, open the child's side of the tty. */
299 int i;
300
301 for(i = 0; i <= maxfd; i++)
302 close(i);
303 /* make new process group */
304 setsid();
305
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000306 xopen(tty_name, O_RDWR /*| O_NOCTTY*/);
Eric Andersen08a72202002-09-30 20:52:10 +0000307 dup(0);
308 dup(0);
309
310 tcsetpgrp(0, getpid());
311
312 /* The pseudo-terminal allocated to the client is configured to operate in
313 * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
314 */
315
316 tcgetattr(0, &termbuf);
317 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
318 termbuf.c_oflag |= ONLCR|XTABS;
319 termbuf.c_iflag |= ICRNL;
320 termbuf.c_iflag &= ~IXOFF;
321 /*termbuf.c_lflag &= ~ICANON;*/
322 tcsetattr(0, TCSANOW, &termbuf);
323
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000324 print_login_issue(issuefile, NULL);
325
Eric Andersen08a72202002-09-30 20:52:10 +0000326 /* exec shell, with correct argv and env */
327 execv(loginpath, (char *const *)argv_init);
328
329 /* NOT REACHED */
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000330 bb_perror_msg_and_die("execv");
Eric Andersen08a72202002-09-30 20:52:10 +0000331 }
332
333 ts->shell_pid = pid;
334
335 return ts;
336}
337
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000338#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000339static void
340free_session(struct tsession *ts)
341{
342 struct tsession *t = sessions;
343
344 /* Unlink this telnet session from the session list. */
Mike Frysinger731f81c2006-05-10 15:23:12 +0000345 if (t == ts)
Eric Andersen08a72202002-09-30 20:52:10 +0000346 sessions = ts->next;
347 else {
348 while(t->next != ts)
349 t = t->next;
350 t->next = ts->next;
351 }
352
353 kill(ts->shell_pid, SIGKILL);
354
355 wait4(ts->shell_pid, NULL, 0, NULL);
356
357 close(ts->ptyfd);
358 close(ts->sockfd);
359
Mike Frysinger731f81c2006-05-10 15:23:12 +0000360 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
Eric Andersen08a72202002-09-30 20:52:10 +0000361 maxfd--;
Mike Frysinger731f81c2006-05-10 15:23:12 +0000362 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
Eric Andersen08a72202002-09-30 20:52:10 +0000363 maxfd--;
364
365 free(ts);
366}
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000367#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000368
369int
370telnetd_main(int argc, char **argv)
371{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000372 unsigned opt;
Eric Andersen08a72202002-09-30 20:52:10 +0000373 fd_set rdfdset, wrfdset;
374 int selret;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000375#ifndef CONFIG_FEATURE_TELNETD_INETD
Denis Vlasenko0e87d342006-09-22 08:50:29 +0000376 sockaddr_type sa;
377 int master_fd;
Eric Andersen08a72202002-09-30 20:52:10 +0000378 int on = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000379 unsigned portnbr = 23;
Rob Landley64a5f962005-11-10 22:37:40 +0000380 struct in_addr bind_addr = { .s_addr = 0x0 };
Denis Vlasenko0e87d342006-09-22 08:50:29 +0000381 char *opt_portnbr, *opt_bindaddr;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000382#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen86f2cce2003-04-25 12:32:37 +0000383 int maxlen, w, r;
Eric Andersen08a72202002-09-30 20:52:10 +0000384
Glenn L McGrathc2b91862003-09-12 11:27:15 +0000385#ifndef CONFIG_LOGIN
386 loginpath = DEFAULT_SHELL;
387#endif
388
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000389 /* We use inetd-style operation unconditionally
390 * (no --foreground option), user most likely will
391 * look into syslog for all errors, even early ones.
392 * Direct all output to syslog at once.
393 */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000394 openlog(applet_name, 0, LOG_USER);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000395 logmode = LOGMODE_SYSLOG;
396
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000397 opt = getopt32(argc, argv, "f:l:" SKIP_FEATURE_TELNETD_INETD("p:b:"),
Denis Vlasenko0e87d342006-09-22 08:50:29 +0000398 &issuefile, &loginpath
399 SKIP_FEATURE_TELNETD_INETD(, &opt_portnbr, &opt_bindaddr));
400 //if (opt & 1) // -f
401 //if (opt & 2) // -l
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000402#ifndef CONFIG_FEATURE_TELNETD_INETD
Denis Vlasenko13858992006-10-08 12:49:22 +0000403 if (opt & 4) portnbr = xatou16(opt_portnbr); // -p
Denis Vlasenko0e87d342006-09-22 08:50:29 +0000404 if (opt & 8) // -b
405 if (inet_aton(opt_bindaddr, &bind_addr) == 0) bb_show_usage();
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000406#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000407
408 if (access(loginpath, X_OK) < 0) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000409 bb_error_msg_and_die("'%s' unavailable", loginpath);
Eric Andersen08a72202002-09-30 20:52:10 +0000410 }
411
412 argv_init[0] = loginpath;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000413
414#ifdef CONFIG_FEATURE_TELNETD_INETD
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000415 maxfd = 1;
Glenn L McGrath8eb214e2003-01-22 21:09:48 +0000416 sessions = make_new_session();
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000417#else /* CONFIG_EATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000418 sessions = 0;
419
420 /* Grab a TCP socket. */
421
Rob Landley099ed502006-08-28 09:41:49 +0000422 master_fd = xsocket(SOCKET_TYPE, SOCK_STREAM, 0);
Eric Andersen08a72202002-09-30 20:52:10 +0000423 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
424
425 /* Set it to listen to specified port. */
426
427 memset((void *)&sa, 0, sizeof(sa));
Rob Landley00e76cb2005-05-10 23:53:33 +0000428#ifdef CONFIG_FEATURE_IPV6
429 sa.sin6_family = AF_INET6;
430 sa.sin6_port = htons(portnbr);
Rob Landley64a5f962005-11-10 22:37:40 +0000431 /* sa.sin6_addr = bind_addr6; */
Rob Landley00e76cb2005-05-10 23:53:33 +0000432#else
Eric Andersen08a72202002-09-30 20:52:10 +0000433 sa.sin_family = AF_INET;
434 sa.sin_port = htons(portnbr);
Rob Landley64a5f962005-11-10 22:37:40 +0000435 sa.sin_addr = bind_addr;
Rob Landley00e76cb2005-05-10 23:53:33 +0000436#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000437
Rob Landley099ed502006-08-28 09:41:49 +0000438 xbind(master_fd, (struct sockaddr *) &sa, sizeof(sa));
439 xlisten(master_fd, 1);
440 xdaemon(0, 0);
Eric Andersen08a72202002-09-30 20:52:10 +0000441
442 maxfd = master_fd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000443#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000444
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000445 while(1) {
Eric Andersen08a72202002-09-30 20:52:10 +0000446 struct tsession *ts;
447
448 FD_ZERO(&rdfdset);
449 FD_ZERO(&wrfdset);
450
451 /* select on the master socket, all telnet sockets and their
452 * ptys if there is room in their respective session buffers.
453 */
454
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000455#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000456 FD_SET(master_fd, &rdfdset);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000457#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000458
459 ts = sessions;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000460#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000461 while (ts) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000462#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000463 /* buf1 is used from socket to pty
464 * buf2 is used from pty to socket
465 */
466 if (ts->size1 > 0) {
467 FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
468 }
469 if (ts->size1 < BUFSIZE) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000470#ifdef CONFIG_FEATURE_TELNETD_INETD
471 FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
472#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000473 FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000474#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000475 }
476 if (ts->size2 > 0) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000477#ifdef CONFIG_FEATURE_TELNETD_INETD
478 FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
479#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000480 FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000481#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000482 }
483 if (ts->size2 < BUFSIZE) {
484 FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
485 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000486#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000487 ts = ts->next;
488 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000489#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000490
491 selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
492
493 if (!selret)
494 break;
495
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000496#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000497 /* First check for and accept new sessions. */
498 if (FD_ISSET(master_fd, &rdfdset)) {
Mike Frysinger06b00e82006-05-10 17:18:11 +0000499 int fd;
500 socklen_t salen;
Eric Andersen08a72202002-09-30 20:52:10 +0000501
502 salen = sizeof(sa);
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000503 fd = accept(master_fd, (struct sockaddr *)&sa, &salen);
504 if (fd < 0) {
Eric Andersen08a72202002-09-30 20:52:10 +0000505 continue;
506 } else {
507 /* Create a new session and link it into
508 our active list. */
509 struct tsession *new_ts = make_new_session(fd);
510 if (new_ts) {
511 new_ts->next = sessions;
512 sessions = new_ts;
513 if (fd > maxfd)
514 maxfd = fd;
515 } else {
516 close(fd);
517 }
518 }
519 }
520
521 /* Then check for data tunneling. */
522
523 ts = sessions;
524 while (ts) { /* For all sessions... */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000525#endif /* CONFIG_FEATURE_TELNETD_INETD */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000526#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000527 struct tsession *next = ts->next; /* in case we free ts. */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000528#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000529
Eric Andersen08a72202002-09-30 20:52:10 +0000530 if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
531 int num_totty;
532 char *ptr;
533 /* Write to pty from buffer 1. */
534
535 ptr = remove_iacs(ts, &num_totty);
536
537 w = write(ts->ptyfd, ptr, num_totty);
538 if (w < 0) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000539#ifdef CONFIG_FEATURE_TELNETD_INETD
540 exit(0);
541#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000542 free_session(ts);
543 ts = next;
544 continue;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000545#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000546 }
547 ts->wridx1 += w;
548 ts->size1 -= w;
549 if (ts->wridx1 == BUFSIZE)
550 ts->wridx1 = 0;
551 }
552
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000553#ifdef CONFIG_FEATURE_TELNETD_INETD
554 if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
555#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000556 if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000557#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000558 /* Write to socket from buffer 2. */
559 maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000560#ifdef CONFIG_FEATURE_TELNETD_INETD
561 w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
562 if (w < 0)
563 exit(0);
564#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000565 w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
566 if (w < 0) {
567 free_session(ts);
568 ts = next;
569 continue;
570 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000571#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000572 ts->wridx2 += w;
573 ts->size2 -= w;
574 if (ts->wridx2 == BUFSIZE)
575 ts->wridx2 = 0;
576 }
577
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000578#ifdef CONFIG_FEATURE_TELNETD_INETD
579 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
580#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000581 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000582#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000583 /* Read from socket to buffer 1. */
584 maxlen = MIN(BUFSIZE - ts->rdidx1,
585 BUFSIZE - ts->size1);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000586#ifdef CONFIG_FEATURE_TELNETD_INETD
587 r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
588 if (!r || (r < 0 && errno != EINTR))
589 exit(0);
590#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000591 r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
592 if (!r || (r < 0 && errno != EINTR)) {
593 free_session(ts);
594 ts = next;
595 continue;
596 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000597#endif /* CONFIG_FEATURE_TELNETD_INETD */
Mike Frysinger731f81c2006-05-10 15:23:12 +0000598 if (!*(ts->buf1 + ts->rdidx1 + r - 1)) {
Eric Andersen08a72202002-09-30 20:52:10 +0000599 r--;
Mike Frysinger731f81c2006-05-10 15:23:12 +0000600 if (!r)
Eric Andersen08a72202002-09-30 20:52:10 +0000601 continue;
602 }
603 ts->rdidx1 += r;
604 ts->size1 += r;
605 if (ts->rdidx1 == BUFSIZE)
606 ts->rdidx1 = 0;
607 }
608
609 if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
610 /* Read from pty to buffer 2. */
611 maxlen = MIN(BUFSIZE - ts->rdidx2,
612 BUFSIZE - ts->size2);
613 r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
614 if (!r || (r < 0 && errno != EINTR)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000615#ifdef CONFIG_FEATURE_TELNETD_INETD
616 exit(0);
617#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000618 free_session(ts);
619 ts = next;
620 continue;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000621#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000622 }
623 ts->rdidx2 += r;
624 ts->size2 += r;
625 if (ts->rdidx2 == BUFSIZE)
626 ts->rdidx2 = 0;
627 }
628
629 if (ts->size1 == 0) {
630 ts->rdidx1 = 0;
631 ts->wridx1 = 0;
632 }
633 if (ts->size2 == 0) {
634 ts->rdidx2 = 0;
635 ts->wridx2 = 0;
636 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000637#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000638 ts = next;
639 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000640#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000641
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000642 } /* while(1) */
Eric Andersen08a72202002-09-30 20:52:10 +0000643
644 return 0;
645}