blob: d51560bfb97ed18d6bb3d5f74206c3d5c01c105a [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 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00006 * Licensed under GPL, 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
27#include <sys/time.h>
28#include <sys/socket.h>
29#include <sys/wait.h>
Glenn L McGrath90ed9a02004-02-22 09:45:57 +000030#include <sys/ioctl.h>
Eric Andersen08a72202002-09-30 20:52:10 +000031#include <string.h>
32#include <stdlib.h>
33#include <unistd.h>
34#include <errno.h>
35#include <netinet/in.h>
Rob Landley64a5f962005-11-10 22:37:40 +000036#include <arpa/inet.h>
Eric Andersen08a72202002-09-30 20:52:10 +000037#include <fcntl.h>
38#include <stdio.h>
39#include <signal.h>
40#include <termios.h>
41#ifdef DEBUG
42#define TELCMDS
43#define TELOPTS
44#endif
45#include <arpa/telnet.h>
46#include <ctype.h>
47#include <sys/syslog.h>
48
49#include "busybox.h"
50
51#define BUFSIZE 4000
52
Rob Landley00e76cb2005-05-10 23:53:33 +000053#ifdef CONFIG_FEATURE_IPV6
54#define SOCKET_TYPE AF_INET6
55typedef struct sockaddr_in6 sockaddr_type;
56#else
57#define SOCKET_TYPE AF_INET
58typedef struct sockaddr_in sockaddr_type;
59#endif
60
61
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000062#ifdef CONFIG_LOGIN
Glenn L McGrathd4004ee2004-09-14 17:24:59 +000063static const char *loginpath = "/bin/login";
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000064#else
Glenn L McGrathd4004ee2004-09-14 17:24:59 +000065static const char *loginpath;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000066#endif
67static const char *issuefile = "/etc/issue.net";
Eric Andersen08a72202002-09-30 20:52:10 +000068
69/* shell name and arguments */
70
71static const char *argv_init[] = {NULL, NULL};
72
73/* structure that describes a session */
74
75struct tsession {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000076#ifdef CONFIG_FEATURE_TELNETD_INETD
77 int sockfd_read, sockfd_write, ptyfd;
78#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +000079 struct tsession *next;
80 int sockfd, ptyfd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +000081#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +000082 int shell_pid;
83 /* two circular buffers */
84 char *buf1, *buf2;
85 int rdidx1, wridx1, size1;
86 int rdidx2, wridx2, size2;
87};
88
89/*
90
91 This is how the buffers are used. The arrows indicate the movement
92 of data.
93
94 +-------+ wridx1++ +------+ rdidx1++ +----------+
95 | | <-------------- | buf1 | <-------------- | |
96 | | size1-- +------+ size1++ | |
Mike Frysingerd2c8fd62006-05-10 17:17:09 +000097 | pty | | socket |
Eric Andersen08a72202002-09-30 20:52:10 +000098 | | rdidx2++ +------+ wridx2++ | |
99 | | --------------> | buf2 | --------------> | |
100 +-------+ size2++ +------+ size2-- +----------+
101
102 Each session has got two buffers.
103
104*/
105
106static int maxfd;
107
108static struct tsession *sessions;
109
110
111/*
112
113 Remove all IAC's from the buffer pointed to by bf (recieved IACs are ignored
114 and must be removed so as to not be interpreted by the terminal). Make an
115 uninterrupted string of characters fit for the terminal. Do this by packing
116 all characters meant for the terminal sequentially towards the end of bf.
117
118 Return a pointer to the beginning of the characters meant for the terminal.
119 and make *num_totty the number of characters that should be sent to
120 the terminal.
121
122 Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
123 past (bf + len) then that IAC will be left unprocessed and *processed will be
124 less than len.
125
126 FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
127 what is the escape character? We aren't handling that situation here.
128
Eric Andersen3752d332003-12-19 11:30:13 +0000129 CR-LF ->'s CR mapping is also done here, for convenience
130
Eric Andersen08a72202002-09-30 20:52:10 +0000131 */
132static char *
133remove_iacs(struct tsession *ts, int *pnum_totty) {
Eric Andersen0cb6f352006-01-30 22:30:41 +0000134 unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1;
Eric Andersen08a72202002-09-30 20:52:10 +0000135 unsigned char *ptr = ptr0;
136 unsigned char *totty = ptr;
137 unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
138 int processed;
139 int num_totty;
140
141 while (ptr < end) {
142 if (*ptr != IAC) {
Eric Andersen3752d332003-12-19 11:30:13 +0000143 int c = *ptr;
Eric Andersen08a72202002-09-30 20:52:10 +0000144 *totty++ = *ptr++;
Eric Andersen3752d332003-12-19 11:30:13 +0000145 /* We now map \r\n ==> \r for pragmatic reasons.
146 * Many client implementations send \r\n when
147 * the user hits the CarriageReturn key.
148 */
149 if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
150 ptr++;
Eric Andersen08a72202002-09-30 20:52:10 +0000151 }
152 else {
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000153 /*
154 * TELOPT_NAWS support!
155 */
156 if ((ptr+2) >= end) {
Eric Andersen08a72202002-09-30 20:52:10 +0000157 /* only the beginning of the IAC is in the
158 buffer we were asked to process, we can't
159 process this char. */
160 break;
161 }
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000162
163 /*
164 * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
165 */
166 else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
167 struct winsize ws;
168 if ((ptr+8) >= end)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000169 break; /* incomplete, can't process */
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000170 ws.ws_col = (ptr[3] << 8) | ptr[4];
171 ws.ws_row = (ptr[5] << 8) | ptr[6];
172 (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
173 ptr += 9;
174 }
175 else {
176 /* skip 3-byte IAC non-SB cmd */
177#ifdef DEBUG
178 fprintf(stderr, "Ignoring IAC %s,%s\n",
179 TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
180#endif
181 ptr += 3;
182 }
Eric Andersen08a72202002-09-30 20:52:10 +0000183 }
184 }
185
186 processed = ptr - ptr0;
187 num_totty = totty - ptr0;
188 /* the difference between processed and num_to tty
189 is all the iacs we removed from the stream.
190 Adjust buf1 accordingly. */
191 ts->wridx1 += processed - num_totty;
192 ts->size1 -= processed - num_totty;
193 *pnum_totty = num_totty;
194 /* move the chars meant for the terminal towards the end of the
195 buffer. */
196 return memmove(ptr - num_totty, ptr0, num_totty);
197}
198
199
200static int
201getpty(char *line)
202{
203 int p;
Glenn L McGrathf234e7c2002-11-10 22:26:19 +0000204#ifdef CONFIG_FEATURE_DEVPTS
Eric Andersen08a72202002-09-30 20:52:10 +0000205 p = open("/dev/ptmx", 2);
206 if (p > 0) {
207 grantpt(p);
208 unlockpt(p);
209 strcpy(line, ptsname(p));
210 return(p);
211 }
212#else
213 struct stat stb;
214 int i;
215 int j;
216
217 strcpy(line, "/dev/ptyXX");
218
219 for (i = 0; i < 16; i++) {
220 line[8] = "pqrstuvwxyzabcde"[i];
221 line[9] = '0';
222 if (stat(line, &stb) < 0) {
223 continue;
224 }
225 for (j = 0; j < 16; j++) {
226 line[9] = j < 10 ? j + '0' : j - 10 + 'a';
Mike Frysinger772a3462006-05-10 17:14:32 +0000227#ifdef DEBUG
228 fprintf(stderr, "Trying to open device: %s\n", line);
229#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000230 if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) {
231 line[5] = 't';
232 return p;
233 }
234 }
235 }
Glenn L McGrathf234e7c2002-11-10 22:26:19 +0000236#endif /* CONFIG_FEATURE_DEVPTS */
Eric Andersen08a72202002-09-30 20:52:10 +0000237 return -1;
238}
239
240
241static void
242send_iac(struct tsession *ts, unsigned char command, int option)
243{
244 /* We rely on that there is space in the buffer for now. */
245 char *b = ts->buf2 + ts->rdidx2;
246 *b++ = IAC;
247 *b++ = command;
248 *b++ = option;
249 ts->rdidx2 += 3;
250 ts->size2 += 3;
251}
252
253
254static struct tsession *
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000255#ifdef CONFIG_FEATURE_TELNETD_INETD
256make_new_session(void)
257#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000258make_new_session(int sockfd)
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000259#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000260{
261 struct termios termbuf;
262 int pty, pid;
263 char tty_name[32];
264 struct tsession *ts = malloc(sizeof(struct tsession) + BUFSIZE * 2);
265
266 ts->buf1 = (char *)(&ts[1]);
267 ts->buf2 = ts->buf1 + BUFSIZE;
268
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000269#ifdef CONFIG_FEATURE_TELNETD_INETD
270 ts->sockfd_read = 0;
271 ts->sockfd_write = 1;
272#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000273 ts->sockfd = sockfd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000274#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000275
276 ts->rdidx1 = ts->wridx1 = ts->size1 = 0;
277 ts->rdidx2 = ts->wridx2 = ts->size2 = 0;
278
279 /* Got a new connection, set up a tty and spawn a shell. */
280
281 pty = getpty(tty_name);
282
283 if (pty < 0) {
Mike Frysinger772a3462006-05-10 17:14:32 +0000284 syslog(LOG_ERR, "All terminals in use!");
Eric Andersen08a72202002-09-30 20:52:10 +0000285 return 0;
286 }
287
288 if (pty > maxfd)
289 maxfd = pty;
290
291 ts->ptyfd = pty;
292
293 /* Make the telnet client understand we will echo characters so it
294 * should not do it locally. We don't tell the client to run linemode,
295 * because we want to handle line editing and tab completion and other
296 * stuff that requires char-by-char support.
297 */
298
299 send_iac(ts, DO, TELOPT_ECHO);
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000300 send_iac(ts, DO, TELOPT_NAWS);
Eric Andersen08a72202002-09-30 20:52:10 +0000301 send_iac(ts, DO, TELOPT_LFLOW);
302 send_iac(ts, WILL, TELOPT_ECHO);
303 send_iac(ts, WILL, TELOPT_SGA);
304
Eric Andersen08a72202002-09-30 20:52:10 +0000305 if ((pid = fork()) < 0) {
Mike Frysinger62ec21d2006-05-10 15:59:07 +0000306 syslog(LOG_ERR, "Could not fork");
Eric Andersen08a72202002-09-30 20:52:10 +0000307 }
308 if (pid == 0) {
309 /* In child, open the child's side of the tty. */
310 int i;
311
312 for(i = 0; i <= maxfd; i++)
313 close(i);
314 /* make new process group */
315 setsid();
316
317 if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
Eric Andersen36adca82004-06-22 10:07:17 +0000318 syslog(LOG_ERR, "Could not open tty");
Eric Andersen08a72202002-09-30 20:52:10 +0000319 exit(1);
Mike Frysinger62ec21d2006-05-10 15:59:07 +0000320 }
Eric Andersen08a72202002-09-30 20:52:10 +0000321 dup(0);
322 dup(0);
323
324 tcsetpgrp(0, getpid());
325
326 /* The pseudo-terminal allocated to the client is configured to operate in
327 * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
328 */
329
330 tcgetattr(0, &termbuf);
331 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
332 termbuf.c_oflag |= ONLCR|XTABS;
333 termbuf.c_iflag |= ICRNL;
334 termbuf.c_iflag &= ~IXOFF;
335 /*termbuf.c_lflag &= ~ICANON;*/
336 tcsetattr(0, TCSANOW, &termbuf);
337
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000338 print_login_issue(issuefile, NULL);
339
Eric Andersen08a72202002-09-30 20:52:10 +0000340 /* exec shell, with correct argv and env */
341 execv(loginpath, (char *const *)argv_init);
342
343 /* NOT REACHED */
Eric Andersen36adca82004-06-22 10:07:17 +0000344 syslog(LOG_ERR, "execv error");
Eric Andersen08a72202002-09-30 20:52:10 +0000345 exit(1);
346 }
347
348 ts->shell_pid = pid;
349
350 return ts;
351}
352
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000353#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000354static void
355free_session(struct tsession *ts)
356{
357 struct tsession *t = sessions;
358
359 /* Unlink this telnet session from the session list. */
Mike Frysinger731f81c2006-05-10 15:23:12 +0000360 if (t == ts)
Eric Andersen08a72202002-09-30 20:52:10 +0000361 sessions = ts->next;
362 else {
363 while(t->next != ts)
364 t = t->next;
365 t->next = ts->next;
366 }
367
368 kill(ts->shell_pid, SIGKILL);
369
370 wait4(ts->shell_pid, NULL, 0, NULL);
371
372 close(ts->ptyfd);
373 close(ts->sockfd);
374
Mike Frysinger731f81c2006-05-10 15:23:12 +0000375 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
Eric Andersen08a72202002-09-30 20:52:10 +0000376 maxfd--;
Mike Frysinger731f81c2006-05-10 15:23:12 +0000377 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
Eric Andersen08a72202002-09-30 20:52:10 +0000378 maxfd--;
379
380 free(ts);
381}
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000382#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000383
384int
385telnetd_main(int argc, char **argv)
386{
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000387#ifndef CONFIG_FEATURE_TELNETD_INETD
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000388 sockaddr_type sa;
Eric Andersen08a72202002-09-30 20:52:10 +0000389 int master_fd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000390#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000391 fd_set rdfdset, wrfdset;
392 int selret;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000393#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000394 int on = 1;
395 int portnbr = 23;
Rob Landley64a5f962005-11-10 22:37:40 +0000396 struct in_addr bind_addr = { .s_addr = 0x0 };
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000397#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000398 int c;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000399 static const char options[] =
400#ifdef CONFIG_FEATURE_TELNETD_INETD
401 "f:l:";
402#else /* CONFIG_EATURE_TELNETD_INETD */
Rob Landley64a5f962005-11-10 22:37:40 +0000403 "f:l:p:b:";
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000404#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen86f2cce2003-04-25 12:32:37 +0000405 int maxlen, w, r;
Eric Andersen08a72202002-09-30 20:52:10 +0000406
Glenn L McGrathc2b91862003-09-12 11:27:15 +0000407#ifndef CONFIG_LOGIN
408 loginpath = DEFAULT_SHELL;
409#endif
410
Eric Andersen08a72202002-09-30 20:52:10 +0000411 for (;;) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000412 c = getopt( argc, argv, options);
Eric Andersen08a72202002-09-30 20:52:10 +0000413 if (c == EOF) break;
414 switch (c) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000415 case 'f':
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000416 issuefile = optarg;
Eric Andersen08a72202002-09-30 20:52:10 +0000417 break;
418 case 'l':
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000419 loginpath = optarg;
Eric Andersen08a72202002-09-30 20:52:10 +0000420 break;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000421#ifndef CONFIG_FEATURE_TELNETD_INETD
422 case 'p':
423 portnbr = atoi(optarg);
424 break;
Rob Landley64a5f962005-11-10 22:37:40 +0000425 case 'b':
426 if (inet_aton(optarg, &bind_addr) == 0)
427 bb_show_usage();
428 break;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000429#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000430 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000431 bb_show_usage();
Eric Andersen08a72202002-09-30 20:52:10 +0000432 }
433 }
434
435 if (access(loginpath, X_OK) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000436 bb_error_msg_and_die ("'%s' unavailable.", loginpath);
Eric Andersen08a72202002-09-30 20:52:10 +0000437 }
438
439 argv_init[0] = loginpath;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000440
Eric Andersen36adca82004-06-22 10:07:17 +0000441 openlog(bb_applet_name, 0, LOG_USER);
442
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000443#ifdef CONFIG_FEATURE_TELNETD_INETD
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000444 maxfd = 1;
Glenn L McGrath8eb214e2003-01-22 21:09:48 +0000445 sessions = make_new_session();
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000446#else /* CONFIG_EATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000447 sessions = 0;
448
449 /* Grab a TCP socket. */
450
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000451 master_fd = bb_xsocket(SOCKET_TYPE, SOCK_STREAM, 0);
Eric Andersen08a72202002-09-30 20:52:10 +0000452 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
453
454 /* Set it to listen to specified port. */
455
456 memset((void *)&sa, 0, sizeof(sa));
Rob Landley00e76cb2005-05-10 23:53:33 +0000457#ifdef CONFIG_FEATURE_IPV6
458 sa.sin6_family = AF_INET6;
459 sa.sin6_port = htons(portnbr);
Rob Landley64a5f962005-11-10 22:37:40 +0000460 /* sa.sin6_addr = bind_addr6; */
Rob Landley00e76cb2005-05-10 23:53:33 +0000461#else
Eric Andersen08a72202002-09-30 20:52:10 +0000462 sa.sin_family = AF_INET;
463 sa.sin_port = htons(portnbr);
Rob Landley64a5f962005-11-10 22:37:40 +0000464 sa.sin_addr = bind_addr;
Rob Landley00e76cb2005-05-10 23:53:33 +0000465#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000466
Bernhard Reutner-Fischer67f641e2006-04-12 18:24:37 +0000467 bb_xbind(master_fd, (struct sockaddr *) &sa, sizeof(sa));
468 bb_xlisten(master_fd, 1);
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +0000469 bb_xdaemon(0, 0);
Eric Andersen08a72202002-09-30 20:52:10 +0000470
471 maxfd = master_fd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000472#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000473
474 do {
475 struct tsession *ts;
476
477 FD_ZERO(&rdfdset);
478 FD_ZERO(&wrfdset);
479
480 /* select on the master socket, all telnet sockets and their
481 * ptys if there is room in their respective session buffers.
482 */
483
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000484#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000485 FD_SET(master_fd, &rdfdset);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000486#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000487
488 ts = sessions;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000489#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000490 while (ts) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000491#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000492 /* buf1 is used from socket to pty
493 * buf2 is used from pty to socket
494 */
495 if (ts->size1 > 0) {
496 FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
497 }
498 if (ts->size1 < BUFSIZE) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000499#ifdef CONFIG_FEATURE_TELNETD_INETD
500 FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
501#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000502 FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000503#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000504 }
505 if (ts->size2 > 0) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000506#ifdef CONFIG_FEATURE_TELNETD_INETD
507 FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
508#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000509 FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000510#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000511 }
512 if (ts->size2 < BUFSIZE) {
513 FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
514 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000515#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000516 ts = ts->next;
517 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000518#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000519
520 selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
521
522 if (!selret)
523 break;
524
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000525#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000526 /* First check for and accept new sessions. */
527 if (FD_ISSET(master_fd, &rdfdset)) {
Mike Frysinger06b00e82006-05-10 17:18:11 +0000528 int fd;
529 socklen_t salen;
Eric Andersen08a72202002-09-30 20:52:10 +0000530
531 salen = sizeof(sa);
532 if ((fd = accept(master_fd, (struct sockaddr *)&sa,
533 &salen)) < 0) {
534 continue;
535 } else {
536 /* Create a new session and link it into
537 our active list. */
538 struct tsession *new_ts = make_new_session(fd);
539 if (new_ts) {
540 new_ts->next = sessions;
541 sessions = new_ts;
542 if (fd > maxfd)
543 maxfd = fd;
544 } else {
545 close(fd);
546 }
547 }
548 }
549
550 /* Then check for data tunneling. */
551
552 ts = sessions;
553 while (ts) { /* For all sessions... */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000554#endif /* CONFIG_FEATURE_TELNETD_INETD */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000555#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000556 struct tsession *next = ts->next; /* in case we free ts. */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000557#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000558
Eric Andersen08a72202002-09-30 20:52:10 +0000559 if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
560 int num_totty;
561 char *ptr;
562 /* Write to pty from buffer 1. */
563
564 ptr = remove_iacs(ts, &num_totty);
565
566 w = write(ts->ptyfd, ptr, num_totty);
567 if (w < 0) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000568#ifdef CONFIG_FEATURE_TELNETD_INETD
569 exit(0);
570#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000571 free_session(ts);
572 ts = next;
573 continue;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000574#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000575 }
576 ts->wridx1 += w;
577 ts->size1 -= w;
578 if (ts->wridx1 == BUFSIZE)
579 ts->wridx1 = 0;
580 }
581
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000582#ifdef CONFIG_FEATURE_TELNETD_INETD
583 if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
584#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000585 if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000586#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000587 /* Write to socket from buffer 2. */
588 maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000589#ifdef CONFIG_FEATURE_TELNETD_INETD
590 w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
591 if (w < 0)
592 exit(0);
593#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000594 w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
595 if (w < 0) {
596 free_session(ts);
597 ts = next;
598 continue;
599 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000600#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000601 ts->wridx2 += w;
602 ts->size2 -= w;
603 if (ts->wridx2 == BUFSIZE)
604 ts->wridx2 = 0;
605 }
606
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000607#ifdef CONFIG_FEATURE_TELNETD_INETD
608 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
609#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000610 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000611#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000612 /* Read from socket to buffer 1. */
613 maxlen = MIN(BUFSIZE - ts->rdidx1,
614 BUFSIZE - ts->size1);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000615#ifdef CONFIG_FEATURE_TELNETD_INETD
616 r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
617 if (!r || (r < 0 && errno != EINTR))
618 exit(0);
619#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000620 r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
621 if (!r || (r < 0 && errno != EINTR)) {
622 free_session(ts);
623 ts = next;
624 continue;
625 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000626#endif /* CONFIG_FEATURE_TELNETD_INETD */
Mike Frysinger731f81c2006-05-10 15:23:12 +0000627 if (!*(ts->buf1 + ts->rdidx1 + r - 1)) {
Eric Andersen08a72202002-09-30 20:52:10 +0000628 r--;
Mike Frysinger731f81c2006-05-10 15:23:12 +0000629 if (!r)
Eric Andersen08a72202002-09-30 20:52:10 +0000630 continue;
631 }
632 ts->rdidx1 += r;
633 ts->size1 += r;
634 if (ts->rdidx1 == BUFSIZE)
635 ts->rdidx1 = 0;
636 }
637
638 if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
639 /* Read from pty to buffer 2. */
640 maxlen = MIN(BUFSIZE - ts->rdidx2,
641 BUFSIZE - ts->size2);
642 r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
643 if (!r || (r < 0 && errno != EINTR)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000644#ifdef CONFIG_FEATURE_TELNETD_INETD
645 exit(0);
646#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000647 free_session(ts);
648 ts = next;
649 continue;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000650#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000651 }
652 ts->rdidx2 += r;
653 ts->size2 += r;
654 if (ts->rdidx2 == BUFSIZE)
655 ts->rdidx2 = 0;
656 }
657
658 if (ts->size1 == 0) {
659 ts->rdidx1 = 0;
660 ts->wridx1 = 0;
661 }
662 if (ts->size2 == 0) {
663 ts->rdidx2 = 0;
664 ts->wridx2 = 0;
665 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000666#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000667 ts = next;
668 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000669#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000670
671 } while (1);
672
673 return 0;
674}