blob: d5de8903c06c3d4907409aa1a115c9f1cd1beea0 [file] [log] [blame]
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001/* $Id: telnetd.c,v 1.13 2004/09/14 17:24:58 bug1 Exp $
Eric Andersen08a72202002-09-30 20:52:10 +00002 *
3 * Simple telnet server
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
5 *
6 * This file is distributed under the Gnu Public License (GPL),
7 * please see the file LICENSE for further information.
8 *
9 * ---------------------------------------------------------------------------
10 * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
11 ****************************************************************************
12 *
13 * The telnetd manpage says it all:
14 *
15 * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
16 * a client, then creating a login process which has the slave side of the
17 * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
18 * master side of the pseudo-terminal, implementing the telnet protocol and
19 * passing characters between the remote client and the login process.
20 *
21 * Vladimir Oleynik <dzo@simtreas.ru> 2001
22 * Set process group corrections, initial busybox port
23 */
24
25/*#define DEBUG 1 */
26
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++ | |
97 | pty | | socket |
98 | | 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) {
134 unsigned char *ptr0 = ts->buf1 + ts->wridx1;
135 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)
169 break; /* incomplete, can't process */
170 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';
227 if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) {
228 line[5] = 't';
229 return p;
230 }
231 }
232 }
Glenn L McGrathf234e7c2002-11-10 22:26:19 +0000233#endif /* CONFIG_FEATURE_DEVPTS */
Eric Andersen08a72202002-09-30 20:52:10 +0000234 return -1;
235}
236
237
238static void
239send_iac(struct tsession *ts, unsigned char command, int option)
240{
241 /* We rely on that there is space in the buffer for now. */
242 char *b = ts->buf2 + ts->rdidx2;
243 *b++ = IAC;
244 *b++ = command;
245 *b++ = option;
246 ts->rdidx2 += 3;
247 ts->size2 += 3;
248}
249
250
251static struct tsession *
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000252#ifdef CONFIG_FEATURE_TELNETD_INETD
253make_new_session(void)
254#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000255make_new_session(int sockfd)
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000256#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000257{
258 struct termios termbuf;
259 int pty, pid;
260 char tty_name[32];
261 struct tsession *ts = malloc(sizeof(struct tsession) + BUFSIZE * 2);
262
263 ts->buf1 = (char *)(&ts[1]);
264 ts->buf2 = ts->buf1 + BUFSIZE;
265
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000266#ifdef CONFIG_FEATURE_TELNETD_INETD
267 ts->sockfd_read = 0;
268 ts->sockfd_write = 1;
269#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000270 ts->sockfd = sockfd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000271#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000272
273 ts->rdidx1 = ts->wridx1 = ts->size1 = 0;
274 ts->rdidx2 = ts->wridx2 = ts->size2 = 0;
275
276 /* Got a new connection, set up a tty and spawn a shell. */
277
278 pty = getpty(tty_name);
279
280 if (pty < 0) {
Eric Andersen36adca82004-06-22 10:07:17 +0000281 syslog(LOG_ERR, "All network ports in use!");
Eric Andersen08a72202002-09-30 20:52:10 +0000282 return 0;
283 }
284
285 if (pty > maxfd)
286 maxfd = pty;
287
288 ts->ptyfd = pty;
289
290 /* Make the telnet client understand we will echo characters so it
291 * should not do it locally. We don't tell the client to run linemode,
292 * because we want to handle line editing and tab completion and other
293 * stuff that requires char-by-char support.
294 */
295
296 send_iac(ts, DO, TELOPT_ECHO);
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000297 send_iac(ts, DO, TELOPT_NAWS);
Eric Andersen08a72202002-09-30 20:52:10 +0000298 send_iac(ts, DO, TELOPT_LFLOW);
299 send_iac(ts, WILL, TELOPT_ECHO);
300 send_iac(ts, WILL, TELOPT_SGA);
301
302
303 if ((pid = fork()) < 0) {
Eric Andersen36adca82004-06-22 10:07:17 +0000304 syslog(LOG_ERR, "Can`t forking");
Eric Andersen08a72202002-09-30 20:52:10 +0000305 }
306 if (pid == 0) {
307 /* In child, open the child's side of the tty. */
308 int i;
309
310 for(i = 0; i <= maxfd; i++)
311 close(i);
312 /* make new process group */
313 setsid();
314
315 if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
Eric Andersen36adca82004-06-22 10:07:17 +0000316 syslog(LOG_ERR, "Could not open tty");
Eric Andersen08a72202002-09-30 20:52:10 +0000317 exit(1);
318 }
319 dup(0);
320 dup(0);
321
322 tcsetpgrp(0, getpid());
323
324 /* The pseudo-terminal allocated to the client is configured to operate in
325 * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
326 */
327
328 tcgetattr(0, &termbuf);
329 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
330 termbuf.c_oflag |= ONLCR|XTABS;
331 termbuf.c_iflag |= ICRNL;
332 termbuf.c_iflag &= ~IXOFF;
333 /*termbuf.c_lflag &= ~ICANON;*/
334 tcsetattr(0, TCSANOW, &termbuf);
335
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000336 print_login_issue(issuefile, NULL);
337
Eric Andersen08a72202002-09-30 20:52:10 +0000338 /* exec shell, with correct argv and env */
339 execv(loginpath, (char *const *)argv_init);
340
341 /* NOT REACHED */
Eric Andersen36adca82004-06-22 10:07:17 +0000342 syslog(LOG_ERR, "execv error");
Eric Andersen08a72202002-09-30 20:52:10 +0000343 exit(1);
344 }
345
346 ts->shell_pid = pid;
347
348 return ts;
349}
350
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000351#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000352static void
353free_session(struct tsession *ts)
354{
355 struct tsession *t = sessions;
356
357 /* Unlink this telnet session from the session list. */
358 if(t == ts)
359 sessions = ts->next;
360 else {
361 while(t->next != ts)
362 t = t->next;
363 t->next = ts->next;
364 }
365
366 kill(ts->shell_pid, SIGKILL);
367
368 wait4(ts->shell_pid, NULL, 0, NULL);
369
370 close(ts->ptyfd);
371 close(ts->sockfd);
372
373 if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
374 maxfd--;
375 if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
376 maxfd--;
377
378 free(ts);
379}
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000380#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000381
382int
383telnetd_main(int argc, char **argv)
384{
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000385#ifndef CONFIG_FEATURE_TELNETD_INETD
Rob Landley00e76cb2005-05-10 23:53:33 +0000386 sockaddr_type sa;
Eric Andersen08a72202002-09-30 20:52:10 +0000387 int master_fd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000388#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000389 fd_set rdfdset, wrfdset;
390 int selret;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000391#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000392 int on = 1;
393 int portnbr = 23;
Rob Landley64a5f962005-11-10 22:37:40 +0000394 struct in_addr bind_addr = { .s_addr = 0x0 };
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000395#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000396 int c;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000397 static const char options[] =
398#ifdef CONFIG_FEATURE_TELNETD_INETD
399 "f:l:";
400#else /* CONFIG_EATURE_TELNETD_INETD */
Rob Landley64a5f962005-11-10 22:37:40 +0000401 "f:l:p:b:";
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000402#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen86f2cce2003-04-25 12:32:37 +0000403 int maxlen, w, r;
Eric Andersen08a72202002-09-30 20:52:10 +0000404
Glenn L McGrathc2b91862003-09-12 11:27:15 +0000405#ifndef CONFIG_LOGIN
406 loginpath = DEFAULT_SHELL;
407#endif
408
Eric Andersen08a72202002-09-30 20:52:10 +0000409 for (;;) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000410 c = getopt( argc, argv, options);
Eric Andersen08a72202002-09-30 20:52:10 +0000411 if (c == EOF) break;
412 switch (c) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000413 case 'f':
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000414 issuefile = optarg;
Eric Andersen08a72202002-09-30 20:52:10 +0000415 break;
416 case 'l':
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000417 loginpath = optarg;
Eric Andersen08a72202002-09-30 20:52:10 +0000418 break;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000419#ifndef CONFIG_FEATURE_TELNETD_INETD
420 case 'p':
421 portnbr = atoi(optarg);
422 break;
Rob Landley64a5f962005-11-10 22:37:40 +0000423 case 'b':
424 if (inet_aton(optarg, &bind_addr) == 0)
425 bb_show_usage();
426 break;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000427#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000428 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000429 bb_show_usage();
Eric Andersen08a72202002-09-30 20:52:10 +0000430 }
431 }
432
433 if (access(loginpath, X_OK) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000434 bb_error_msg_and_die ("'%s' unavailable.", loginpath);
Eric Andersen08a72202002-09-30 20:52:10 +0000435 }
436
437 argv_init[0] = loginpath;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000438
Eric Andersen36adca82004-06-22 10:07:17 +0000439 openlog(bb_applet_name, 0, LOG_USER);
440
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000441#ifdef CONFIG_FEATURE_TELNETD_INETD
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000442 maxfd = 1;
Glenn L McGrath8eb214e2003-01-22 21:09:48 +0000443 sessions = make_new_session();
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000444#else /* CONFIG_EATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000445 sessions = 0;
446
447 /* Grab a TCP socket. */
448
Rob Landley00e76cb2005-05-10 23:53:33 +0000449 master_fd = socket(SOCKET_TYPE, SOCK_STREAM, 0);
Eric Andersen08a72202002-09-30 20:52:10 +0000450 if (master_fd < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000451 bb_perror_msg_and_die("socket");
Eric Andersen08a72202002-09-30 20:52:10 +0000452 }
453 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
454
455 /* Set it to listen to specified port. */
456
457 memset((void *)&sa, 0, sizeof(sa));
Rob Landley00e76cb2005-05-10 23:53:33 +0000458#ifdef CONFIG_FEATURE_IPV6
459 sa.sin6_family = AF_INET6;
460 sa.sin6_port = htons(portnbr);
Rob Landley64a5f962005-11-10 22:37:40 +0000461 /* sa.sin6_addr = bind_addr6; */
Rob Landley00e76cb2005-05-10 23:53:33 +0000462#else
Eric Andersen08a72202002-09-30 20:52:10 +0000463 sa.sin_family = AF_INET;
464 sa.sin_port = htons(portnbr);
Rob Landley64a5f962005-11-10 22:37:40 +0000465 sa.sin_addr = bind_addr;
Rob Landley00e76cb2005-05-10 23:53:33 +0000466#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000467
468 if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000469 bb_perror_msg_and_die("bind");
Eric Andersen08a72202002-09-30 20:52:10 +0000470 }
471
472 if (listen(master_fd, 1) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000473 bb_perror_msg_and_die("listen");
Eric Andersen08a72202002-09-30 20:52:10 +0000474 }
475
476 if (daemon(0, 0) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000477 bb_perror_msg_and_die("daemon");
Eric Andersen08a72202002-09-30 20:52:10 +0000478
479
480 maxfd = master_fd;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000481#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000482
483 do {
484 struct tsession *ts;
485
486 FD_ZERO(&rdfdset);
487 FD_ZERO(&wrfdset);
488
489 /* select on the master socket, all telnet sockets and their
490 * ptys if there is room in their respective session buffers.
491 */
492
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000493#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000494 FD_SET(master_fd, &rdfdset);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000495#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000496
497 ts = sessions;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000498#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000499 while (ts) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000500#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000501 /* buf1 is used from socket to pty
502 * buf2 is used from pty to socket
503 */
504 if (ts->size1 > 0) {
505 FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
506 }
507 if (ts->size1 < BUFSIZE) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000508#ifdef CONFIG_FEATURE_TELNETD_INETD
509 FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
510#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000511 FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000512#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000513 }
514 if (ts->size2 > 0) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000515#ifdef CONFIG_FEATURE_TELNETD_INETD
516 FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
517#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000518 FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000519#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000520 }
521 if (ts->size2 < BUFSIZE) {
522 FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
523 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000524#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000525 ts = ts->next;
526 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000527#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000528
529 selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
530
531 if (!selret)
532 break;
533
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000534#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000535 /* First check for and accept new sessions. */
536 if (FD_ISSET(master_fd, &rdfdset)) {
537 int fd, salen;
538
539 salen = sizeof(sa);
540 if ((fd = accept(master_fd, (struct sockaddr *)&sa,
541 &salen)) < 0) {
542 continue;
543 } else {
544 /* Create a new session and link it into
545 our active list. */
546 struct tsession *new_ts = make_new_session(fd);
547 if (new_ts) {
548 new_ts->next = sessions;
549 sessions = new_ts;
550 if (fd > maxfd)
551 maxfd = fd;
552 } else {
553 close(fd);
554 }
555 }
556 }
557
558 /* Then check for data tunneling. */
559
560 ts = sessions;
561 while (ts) { /* For all sessions... */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000562#endif /* CONFIG_FEATURE_TELNETD_INETD */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000563#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000564 struct tsession *next = ts->next; /* in case we free ts. */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000565#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000566
Eric Andersen08a72202002-09-30 20:52:10 +0000567 if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
568 int num_totty;
569 char *ptr;
570 /* Write to pty from buffer 1. */
571
572 ptr = remove_iacs(ts, &num_totty);
573
574 w = write(ts->ptyfd, ptr, num_totty);
575 if (w < 0) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000576#ifdef CONFIG_FEATURE_TELNETD_INETD
577 exit(0);
578#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000579 free_session(ts);
580 ts = next;
581 continue;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000582#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000583 }
584 ts->wridx1 += w;
585 ts->size1 -= w;
586 if (ts->wridx1 == BUFSIZE)
587 ts->wridx1 = 0;
588 }
589
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000590#ifdef CONFIG_FEATURE_TELNETD_INETD
591 if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
592#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000593 if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000594#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000595 /* Write to socket from buffer 2. */
596 maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000597#ifdef CONFIG_FEATURE_TELNETD_INETD
598 w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
599 if (w < 0)
600 exit(0);
601#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000602 w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
603 if (w < 0) {
604 free_session(ts);
605 ts = next;
606 continue;
607 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000608#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000609 ts->wridx2 += w;
610 ts->size2 -= w;
611 if (ts->wridx2 == BUFSIZE)
612 ts->wridx2 = 0;
613 }
614
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000615#ifdef CONFIG_FEATURE_TELNETD_INETD
616 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
617#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000618 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000619#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000620 /* Read from socket to buffer 1. */
621 maxlen = MIN(BUFSIZE - ts->rdidx1,
622 BUFSIZE - ts->size1);
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000623#ifdef CONFIG_FEATURE_TELNETD_INETD
624 r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
625 if (!r || (r < 0 && errno != EINTR))
626 exit(0);
627#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000628 r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
629 if (!r || (r < 0 && errno != EINTR)) {
630 free_session(ts);
631 ts = next;
632 continue;
633 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000634#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000635 if(!*(ts->buf1 + ts->rdidx1 + r - 1)) {
636 r--;
637 if(!r)
638 continue;
639 }
640 ts->rdidx1 += r;
641 ts->size1 += r;
642 if (ts->rdidx1 == BUFSIZE)
643 ts->rdidx1 = 0;
644 }
645
646 if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
647 /* Read from pty to buffer 2. */
648 maxlen = MIN(BUFSIZE - ts->rdidx2,
649 BUFSIZE - ts->size2);
650 r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
651 if (!r || (r < 0 && errno != EINTR)) {
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000652#ifdef CONFIG_FEATURE_TELNETD_INETD
653 exit(0);
654#else /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000655 free_session(ts);
656 ts = next;
657 continue;
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000658#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000659 }
660 ts->rdidx2 += r;
661 ts->size2 += r;
662 if (ts->rdidx2 == BUFSIZE)
663 ts->rdidx2 = 0;
664 }
665
666 if (ts->size1 == 0) {
667 ts->rdidx1 = 0;
668 ts->wridx1 = 0;
669 }
670 if (ts->size2 == 0) {
671 ts->rdidx2 = 0;
672 ts->wridx2 = 0;
673 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000674#ifndef CONFIG_FEATURE_TELNETD_INETD
Eric Andersen08a72202002-09-30 20:52:10 +0000675 ts = next;
676 }
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000677#endif /* CONFIG_FEATURE_TELNETD_INETD */
Eric Andersen08a72202002-09-30 20:52:10 +0000678
679 } while (1);
680
681 return 0;
682}