blob: f06bff8b95f4827019ad7801b3a16a8762bc86eb [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
Denis Vlasenko75f8d082006-11-22 15:54:52 +000024#define DEBUG 0
Eric Andersen08a72202002-09-30 20:52:10 +000025
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000026#include "libbb.h"
Rob Landley099ed502006-08-28 09:41:49 +000027
Denis Vlasenko75f8d082006-11-22 15:54:52 +000028#if DEBUG
Eric Andersen08a72202002-09-30 20:52:10 +000029#define TELCMDS
30#define TELOPTS
31#endif
32#include <arpa/telnet.h>
Eric Andersen08a72202002-09-30 20:52:10 +000033#include <sys/syslog.h>
34
Eric Andersen08a72202002-09-30 20:52:10 +000035
Denis Vlasenko59d7c432007-10-15 15:19:36 +000036/* Structure that describes a session */
Eric Andersen08a72202002-09-30 20:52:10 +000037struct tsession {
38 struct tsession *next;
Denis Vlasenko75f8d082006-11-22 15:54:52 +000039 int sockfd_read, sockfd_write, ptyfd;
Denis Vlasenko2450c452007-10-15 22:09:15 +000040 int shell_pid;
Denis Vlasenko59d7c432007-10-15 15:19:36 +000041
Eric Andersen08a72202002-09-30 20:52:10 +000042 /* two circular buffers */
Denis Vlasenko59d7c432007-10-15 15:19:36 +000043 /*char *buf1, *buf2;*/
44/*#define TS_BUF1 ts->buf1*/
45/*#define TS_BUF2 TS_BUF2*/
Denis Vlasenko10916c52007-10-15 17:28:00 +000046#define TS_BUF1 ((unsigned char*)(ts + 1))
47#define TS_BUF2 (((unsigned char*)(ts + 1)) + BUFSIZE)
Eric Andersen08a72202002-09-30 20:52:10 +000048 int rdidx1, wridx1, size1;
49 int rdidx2, wridx2, size2;
50};
51
Denis Vlasenko88308fe2007-06-25 10:35:11 +000052/* Two buffers are directly after tsession in malloced memory.
53 * Make whole thing fit in 4k */
Denis Vlasenko59d7c432007-10-15 15:19:36 +000054enum { BUFSIZE = (4 * 1024 - sizeof(struct tsession)) / 2 };
Denis Vlasenko88308fe2007-06-25 10:35:11 +000055
Eric Andersen08a72202002-09-30 20:52:10 +000056
Denis Vlasenko59d7c432007-10-15 15:19:36 +000057/* Globals */
Eric Andersen08a72202002-09-30 20:52:10 +000058static int maxfd;
Eric Andersen08a72202002-09-30 20:52:10 +000059static struct tsession *sessions;
Denis Vlasenko59d7c432007-10-15 15:19:36 +000060#if ENABLE_LOGIN
61static const char *loginpath = "/bin/login";
62#else
63static const char *loginpath = DEFAULT_SHELL;
64#endif
65static const char *issuefile = "/etc/issue.net";
Eric Andersen08a72202002-09-30 20:52:10 +000066
67
68/*
Denis Vlasenko59d7c432007-10-15 15:19:36 +000069 Remove all IAC's from buf1 (received IACs are ignored and must be removed
70 so as to not be interpreted by the terminal). Make an uninterrupted
71 string of characters fit for the terminal. Do this by packing
72 all characters meant for the terminal sequentially towards the end of buf.
Eric Andersen08a72202002-09-30 20:52:10 +000073
74 Return a pointer to the beginning of the characters meant for the terminal.
75 and make *num_totty the number of characters that should be sent to
76 the terminal.
77
78 Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
Denis Vlasenko59d7c432007-10-15 15:19:36 +000079 past (bf + len) then that IAC will be left unprocessed and *processed
80 will be less than len.
Eric Andersen08a72202002-09-30 20:52:10 +000081
82 FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
83 what is the escape character? We aren't handling that situation here.
84
Denis Vlasenko10916c52007-10-15 17:28:00 +000085 CR-LF ->'s CR mapping is also done here, for convenience.
86
87 NB: may fail to remove iacs which wrap around buffer!
Denis Vlasenko75f8d082006-11-22 15:54:52 +000088 */
Denis Vlasenko10916c52007-10-15 17:28:00 +000089static unsigned char *
Denis Vlasenko75f8d082006-11-22 15:54:52 +000090remove_iacs(struct tsession *ts, int *pnum_totty)
91{
Denis Vlasenko10916c52007-10-15 17:28:00 +000092 unsigned char *ptr0 = TS_BUF1 + ts->wridx1;
Eric Andersen08a72202002-09-30 20:52:10 +000093 unsigned char *ptr = ptr0;
94 unsigned char *totty = ptr;
95 unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
Eric Andersen08a72202002-09-30 20:52:10 +000096 int num_totty;
97
98 while (ptr < end) {
99 if (*ptr != IAC) {
Denis Vlasenko10916c52007-10-15 17:28:00 +0000100 char c = *ptr;
101
102 *totty++ = c;
103 ptr++;
Eric Andersen3752d332003-12-19 11:30:13 +0000104 /* We now map \r\n ==> \r for pragmatic reasons.
105 * Many client implementations send \r\n when
106 * the user hits the CarriageReturn key.
107 */
Denis Vlasenko10916c52007-10-15 17:28:00 +0000108 if (c == '\r' && ptr < end && (*ptr == '\n' || *ptr == '\0'))
Eric Andersen3752d332003-12-19 11:30:13 +0000109 ptr++;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000110 } else {
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000111 /*
112 * TELOPT_NAWS support!
113 */
114 if ((ptr+2) >= end) {
Eric Andersen08a72202002-09-30 20:52:10 +0000115 /* only the beginning of the IAC is in the
116 buffer we were asked to process, we can't
117 process this char. */
118 break;
119 }
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000120
121 /*
122 * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
123 */
124 else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
125 struct winsize ws;
Denis Vlasenko10916c52007-10-15 17:28:00 +0000126
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000127 if ((ptr+8) >= end)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000128 break; /* incomplete, can't process */
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000129 ws.ws_col = (ptr[3] << 8) | ptr[4];
130 ws.ws_row = (ptr[5] << 8) | ptr[6];
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000131 ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000132 ptr += 9;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000133 } else {
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000134 /* skip 3-byte IAC non-SB cmd */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000135#if DEBUG
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000136 fprintf(stderr, "Ignoring IAC %s,%s\n",
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000137 TELCMD(ptr[1]), TELOPT(ptr[2]));
Glenn L McGrath90ed9a02004-02-22 09:45:57 +0000138#endif
139 ptr += 3;
140 }
Eric Andersen08a72202002-09-30 20:52:10 +0000141 }
142 }
143
Denis Vlasenko10916c52007-10-15 17:28:00 +0000144 num_totty = totty - ptr0;
Eric Andersen08a72202002-09-30 20:52:10 +0000145 *pnum_totty = num_totty;
Denis Vlasenko10916c52007-10-15 17:28:00 +0000146 /* the difference between ptr and totty is number of iacs
147 we removed from the stream. Adjust buf1 accordingly. */
148 if ((ptr - totty) == 0) /* 99.999% of cases */
149 return ptr0;
150 ts->wridx1 += ptr - totty;
151 ts->size1 -= ptr - totty;
152 /* move chars meant for the terminal towards the end of the buffer */
Eric Andersen08a72202002-09-30 20:52:10 +0000153 return memmove(ptr - num_totty, ptr0, num_totty);
154}
155
156
157static int
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000158getpty(char *line, int size)
Eric Andersen08a72202002-09-30 20:52:10 +0000159{
160 int p;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000161#if ENABLE_FEATURE_DEVPTS
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000162 p = open("/dev/ptmx", O_RDWR);
Eric Andersen08a72202002-09-30 20:52:10 +0000163 if (p > 0) {
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000164 const char *name;
Eric Andersen08a72202002-09-30 20:52:10 +0000165 grantpt(p);
166 unlockpt(p);
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000167 name = ptsname(p);
168 if (!name) {
169 bb_perror_msg("ptsname error (is /dev/pts mounted?)");
170 return -1;
171 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000172 safe_strncpy(line, name, size);
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000173 return p;
Eric Andersen08a72202002-09-30 20:52:10 +0000174 }
175#else
176 struct stat stb;
177 int i;
178 int j;
179
180 strcpy(line, "/dev/ptyXX");
181
182 for (i = 0; i < 16; i++) {
183 line[8] = "pqrstuvwxyzabcde"[i];
184 line[9] = '0';
185 if (stat(line, &stb) < 0) {
186 continue;
187 }
188 for (j = 0; j < 16; j++) {
189 line[9] = j < 10 ? j + '0' : j - 10 + 'a';
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000190 if (DEBUG)
191 fprintf(stderr, "Trying to open device: %s\n", line);
Denis Vlasenkoc6ec8c92006-10-15 18:22:05 +0000192 p = open(line, O_RDWR | O_NOCTTY);
193 if (p >= 0) {
Eric Andersen08a72202002-09-30 20:52:10 +0000194 line[5] = 't';
195 return p;
196 }
197 }
198 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000199#endif /* FEATURE_DEVPTS */
Eric Andersen08a72202002-09-30 20:52:10 +0000200 return -1;
201}
202
203
Eric Andersen08a72202002-09-30 20:52:10 +0000204static struct tsession *
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000205make_new_session(
Denis Vlasenkof472b232007-10-16 21:35:17 +0000206 USE_FEATURE_TELNETD_STANDALONE(int sock)
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000207 SKIP_FEATURE_TELNETD_STANDALONE(void)
208) {
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000209 const char *login_argv[2];
Eric Andersen08a72202002-09-30 20:52:10 +0000210 struct termios termbuf;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000211 int fd, pid;
Eric Andersen08a72202002-09-30 20:52:10 +0000212 char tty_name[32];
Rob Landley1ec5b292006-05-29 07:42:02 +0000213 struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
Eric Andersen08a72202002-09-30 20:52:10 +0000214
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000215 /*ts->buf1 = (char *)(ts + 1);*/
216 /*ts->buf2 = ts->buf1 + BUFSIZE;*/
Eric Andersen08a72202002-09-30 20:52:10 +0000217
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000218 /* Got a new connection, set up a tty. */
219 fd = getpty(tty_name, 32);
220 if (fd < 0) {
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000221 bb_error_msg("can't create pty");
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000222 return NULL;
Eric Andersen08a72202002-09-30 20:52:10 +0000223 }
Denis Vlasenkof472b232007-10-16 21:35:17 +0000224 if (fd > maxfd)
225 maxfd = fd;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000226 ts->ptyfd = fd;
227 ndelay_on(fd);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000228#if ENABLE_FEATURE_TELNETD_STANDALONE
Denis Vlasenkof472b232007-10-16 21:35:17 +0000229 ts->sockfd_read = sock;
230 ndelay_on(sock);
231 if (!sock) /* We are called with fd 0 - we are in inetd mode */
232 sock++;
233 ts->sockfd_write = sock;
234 ndelay_on(sock);
235 if (sock > maxfd)
236 maxfd = sock;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000237#else
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000238 /* ts->sockfd_read = 0; - done by xzalloc */
Denis Vlasenkof472b232007-10-16 21:35:17 +0000239 ts->sockfd_write = 1;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000240 ndelay_on(0);
241 ndelay_on(1);
242#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000243 /* Make the telnet client understand we will echo characters so it
244 * should not do it locally. We don't tell the client to run linemode,
245 * because we want to handle line editing and tab completion and other
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000246 * stuff that requires char-by-char support. */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000247 {
248 static const char iacs_to_send[] ALIGN1 = {
249 IAC, DO, TELOPT_ECHO,
250 IAC, DO, TELOPT_NAWS,
251 IAC, DO, TELOPT_LFLOW,
252 IAC, WILL, TELOPT_ECHO,
253 IAC, WILL, TELOPT_SGA
254 };
255 memcpy(TS_BUF2, iacs_to_send, sizeof(iacs_to_send));
256 ts->rdidx2 = sizeof(iacs_to_send);
257 ts->size2 = sizeof(iacs_to_send);
258 }
Eric Andersen08a72202002-09-30 20:52:10 +0000259
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000260 fflush(NULL); /* flush all streams */
261 pid = vfork(); /* NOMMU-friendly */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000262 if (pid < 0) {
263 free(ts);
264 close(fd);
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000265 /* sock_r and sock_w will be closed by caller */
266 bb_perror_msg("vfork");
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000267 return NULL;
Eric Andersen08a72202002-09-30 20:52:10 +0000268 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000269 if (pid > 0) {
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000270 /* Parent */
Denis Vlasenko2450c452007-10-15 22:09:15 +0000271 ts->shell_pid = pid;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000272 return ts;
Eric Andersen08a72202002-09-30 20:52:10 +0000273 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000274
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000275 /* Child */
276 /* Careful - we are after vfork! */
Eric Andersen08a72202002-09-30 20:52:10 +0000277
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000278 /* make new session and process group */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000279 setsid();
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000280
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000281 /* open the child's side of the tty. */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000282 /* NB: setsid() disconnects from any previous ctty's. Therefore
283 * we must open child's side of the tty AFTER setsid! */
284 fd = xopen(tty_name, O_RDWR); /* becomes our ctty */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000285 dup2(fd, 0);
286 dup2(fd, 1);
287 dup2(fd, 2);
288 while (fd > 2) close(fd--);
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000289 tcsetpgrp(0, getpid()); /* switch this tty's process group to us */
Eric Andersen08a72202002-09-30 20:52:10 +0000290
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000291 /* The pseudo-terminal allocated to the client is configured to operate in
292 * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */
293 tcgetattr(0, &termbuf);
294 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000295 termbuf.c_oflag |= ONLCR | XTABS;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000296 termbuf.c_iflag |= ICRNL;
297 termbuf.c_iflag &= ~IXOFF;
298 /*termbuf.c_lflag &= ~ICANON;*/
299 tcsetattr(0, TCSANOW, &termbuf);
300
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000301 /* Uses FILE-based I/O to stdout, but does fflush(stdout),
302 * so should be safe with vfork.
303 * I fear, though, that some users will have ridiculously big
304 * issue files, and they may block writing to fd 1. */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000305 print_login_issue(issuefile, NULL);
306
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000307 /* Exec shell / login / whatever */
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000308 login_argv[0] = loginpath;
309 login_argv[1] = NULL;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000310 execvp(loginpath, (char **)login_argv);
Denis Vlasenko2450c452007-10-15 22:09:15 +0000311 /* Safer with vfork, and we shouldn't send message
312 * to remote clients anyway */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000313 _exit(1); /*bb_perror_msg_and_die("execv %s", loginpath);*/
Eric Andersen08a72202002-09-30 20:52:10 +0000314}
315
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000316#if ENABLE_FEATURE_TELNETD_STANDALONE
317
Eric Andersen08a72202002-09-30 20:52:10 +0000318static void
319free_session(struct tsession *ts)
320{
321 struct tsession *t = sessions;
322
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000323 /* Unlink this telnet session from the session list */
Mike Frysinger731f81c2006-05-10 15:23:12 +0000324 if (t == ts)
Eric Andersen08a72202002-09-30 20:52:10 +0000325 sessions = ts->next;
326 else {
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000327 while (t->next != ts)
Eric Andersen08a72202002-09-30 20:52:10 +0000328 t = t->next;
329 t->next = ts->next;
330 }
331
Denis Vlasenkof472b232007-10-16 21:35:17 +0000332#if 0
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000333 /* It was said that "normal" telnetd just closes ptyfd,
334 * doesn't send SIGKILL. When we close ptyfd,
335 * kernel sends SIGHUP to processes having slave side opened. */
Denis Vlasenkof472b232007-10-16 21:35:17 +0000336 kill(ts->shell_pid, SIGKILL);
337 wait4(ts->shell_pid, NULL, 0, NULL);
338#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000339 close(ts->ptyfd);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000340 close(ts->sockfd_read);
Denis Vlasenkof472b232007-10-16 21:35:17 +0000341 /* We do not need to close(ts->sockfd_write), it's the same
342 * as sockfd_read unless we are in inetd mode. But in inetd mode
343 * we do not free_session(), ever */
Eric Andersen08a72202002-09-30 20:52:10 +0000344 free(ts);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000345
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000346 /* Scan all sessions and find new maxfd */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000347 maxfd = 0;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000348 ts = sessions;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000349 while (ts) {
350 if (maxfd < ts->ptyfd)
351 maxfd = ts->ptyfd;
352 if (maxfd < ts->sockfd_read)
353 maxfd = ts->sockfd_read;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000354#if 0
355 /* Again, sockfd_write == sockfd_read here */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000356 if (maxfd < ts->sockfd_write)
357 maxfd = ts->sockfd_write;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000358#endif
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000359 ts = ts->next;
360 }
Eric Andersen08a72202002-09-30 20:52:10 +0000361}
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000362
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000363#else /* !FEATURE_TELNETD_STANDALONE */
364
365/* Never actually called */
366void free_session(struct tsession *ts);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000367
368#endif
369
Denis Vlasenko2450c452007-10-15 22:09:15 +0000370static void handle_sigchld(int sig)
371{
372 pid_t pid;
373 struct tsession *ts;
374
375 pid = waitpid(-1, &sig, WNOHANG);
376 if (pid > 0) {
377 ts = sessions;
378 while (ts) {
379 if (ts->shell_pid == pid) {
380 ts->shell_pid = -1;
381 return;
382 }
383 ts = ts->next;
384 }
385 }
386}
387
Eric Andersen08a72202002-09-30 20:52:10 +0000388
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000389int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko06af2162007-02-03 17:28:39 +0000390int telnetd_main(int argc, char **argv)
Eric Andersen08a72202002-09-30 20:52:10 +0000391{
Eric Andersen08a72202002-09-30 20:52:10 +0000392 fd_set rdfdset, wrfdset;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000393 unsigned opt;
Denis Vlasenko10916c52007-10-15 17:28:00 +0000394 int count;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000395 struct tsession *ts;
396#if ENABLE_FEATURE_TELNETD_STANDALONE
397#define IS_INETD (opt & OPT_INETD)
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000398 int master_fd = master_fd; /* be happy, gcc */
Denis Vlasenko13858992006-10-08 12:49:22 +0000399 unsigned portnbr = 23;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000400 char *opt_bindaddr = NULL;
401 char *opt_portnbr;
402#else
403 enum {
404 IS_INETD = 1,
405 master_fd = -1,
406 portnbr = 23,
407 };
Glenn L McGrathc2b91862003-09-12 11:27:15 +0000408#endif
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000409 enum {
Denis Vlasenko2450c452007-10-15 22:09:15 +0000410 OPT_WATCHCHILD = (1 << 2), /* -K */
411 OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
412 OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */
413 OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000414 };
Glenn L McGrathc2b91862003-09-12 11:27:15 +0000415
Denis Vlasenko2450c452007-10-15 22:09:15 +0000416 /* Even if !STANDALONE, we accept (and ignore) -i, thus people
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000417 * don't need to guess whether it's ok to pass -i to us */
Denis Vlasenko2450c452007-10-15 22:09:15 +0000418 opt = getopt32(argv, "f:l:Ki" USE_FEATURE_TELNETD_STANDALONE("p:b:F"),
Denis Vlasenko0e87d342006-09-22 08:50:29 +0000419 &issuefile, &loginpath
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000420 USE_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr));
Denis Vlasenko2450c452007-10-15 22:09:15 +0000421 if (!IS_INETD /*&& !re_execed*/) {
422 /* inform that we start in standalone mode?
423 * May be useful when people forget to give -i */
424 /*bb_error_msg("listening for connections");*/
425 if (!(opt & OPT_FOREGROUND)) {
426 /* DAEMON_CHDIR_ROOT was giving inconsistent
Denis Vlasenko008eda22007-10-16 10:47:27 +0000427 * behavior with/without -F, -i */
Denis Vlasenko2450c452007-10-15 22:09:15 +0000428 bb_daemonize_or_rexec(0 /*DAEMON_CHDIR_ROOT*/, argv);
429 }
430 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000431 /* Redirect log to syslog early, if needed */
432 if (IS_INETD || !(opt & OPT_FOREGROUND)) {
433 openlog(applet_name, 0, LOG_USER);
434 logmode = LOGMODE_SYSLOG;
435 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000436 USE_FEATURE_TELNETD_STANDALONE(
Denis Vlasenko2450c452007-10-15 22:09:15 +0000437 if (opt & OPT_PORT)
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000438 portnbr = xatou16(opt_portnbr);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000439 );
Eric Andersen08a72202002-09-30 20:52:10 +0000440
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000441 /* Used to check access(loginpath, X_OK) here. Pointless.
442 * exec will do this for us for free later. */
Glenn L McGrath9e5d6c02003-01-21 20:55:56 +0000443
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000444#if ENABLE_FEATURE_TELNETD_STANDALONE
445 if (IS_INETD) {
Denis Vlasenkof472b232007-10-16 21:35:17 +0000446 sessions = make_new_session(0);
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000447 if (!sessions) /* pty opening or vfork problem, exit */
448 return 1; /* make_new_session prints error message */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000449 } else {
Denis Vlasenko9de420c2007-01-10 09:28:01 +0000450 master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
Denis Vlasenkoc8717cd2006-11-22 16:10:39 +0000451 xlisten(master_fd, 1);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000452 }
Rob Landley00e76cb2005-05-10 23:53:33 +0000453#else
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000454 sessions = make_new_session();
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000455 if (!sessions) /* pty opening or vfork problem, exit */
456 return 1; /* make_new_session prints error message */
Rob Landley00e76cb2005-05-10 23:53:33 +0000457#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000458
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000459 /* We don't want to die if just one session is broken */
460 signal(SIGPIPE, SIG_IGN);
Eric Andersen08a72202002-09-30 20:52:10 +0000461
Denis Vlasenko2450c452007-10-15 22:09:15 +0000462 if (opt & OPT_WATCHCHILD)
463 signal(SIGCHLD, handle_sigchld);
464
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000465/*
466 This is how the buffers are used. The arrows indicate the movement
467 of data.
468 +-------+ wridx1++ +------+ rdidx1++ +----------+
469 | | <-------------- | buf1 | <-------------- | |
470 | | size1-- +------+ size1++ | |
471 | pty | | socket |
472 | | rdidx2++ +------+ wridx2++ | |
473 | | --------------> | buf2 | --------------> | |
474 +-------+ size2++ +------+ size2-- +----------+
475
476 size1: "how many bytes are buffered for pty between rdidx1 and wridx1?"
477 size2: "how many bytes are buffered for socket between rdidx2 and wridx2?"
478
479 Each session has got two buffers. Buffers are circular. If sizeN == 0,
480 buffer is empty. If sizeN == BUFSIZE, buffer is full. In both these cases
481 rdidxN == wridxN.
482*/
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000483 again:
484 FD_ZERO(&rdfdset);
485 FD_ZERO(&wrfdset);
Eric Andersen08a72202002-09-30 20:52:10 +0000486
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000487 /* Select on the master socket, all telnet sockets and their
488 * ptys if there is room in their session buffers.
489 * NB: scalability problem: we recalculate entire bitmap
490 * before each select. Can be a problem with 500+ connections. */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000491 ts = sessions;
492 while (ts) {
Denis Vlasenko2450c452007-10-15 22:09:15 +0000493 struct tsession *next = ts->next; /* in case we free ts. */
494 if (ts->shell_pid == -1) {
495 free_session(ts);
496 } else {
497 if (ts->size1 > 0) /* can write to pty */
498 FD_SET(ts->ptyfd, &wrfdset);
499 if (ts->size1 < BUFSIZE) /* can read from socket */
500 FD_SET(ts->sockfd_read, &rdfdset);
501 if (ts->size2 > 0) /* can write to socket */
502 FD_SET(ts->sockfd_write, &wrfdset);
503 if (ts->size2 < BUFSIZE) /* can read from pty */
504 FD_SET(ts->ptyfd, &rdfdset);
505 }
506 ts = next;
507 }
508 if (!IS_INETD) {
509 FD_SET(master_fd, &rdfdset);
510 /* This is needed because free_session() does not
511 * take into account master_fd when it finds new
512 * maxfd among remaining fd's */
513 if (master_fd > maxfd)
514 maxfd = master_fd;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000515 }
516
Denis Vlasenko10916c52007-10-15 17:28:00 +0000517 count = select(maxfd + 1, &rdfdset, &wrfdset, NULL, NULL);
518 if (count < 0)
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000519 goto again; /* EINTR or ENOMEM */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000520
521#if ENABLE_FEATURE_TELNETD_STANDALONE
522 /* First check for and accept new sessions. */
523 if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000524 int fd;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000525 struct tsession *new_ts;
526
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000527 fd = accept(master_fd, NULL, NULL);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000528 if (fd < 0)
529 goto again;
530 /* Create a new session and link it into our active list */
Denis Vlasenkof472b232007-10-16 21:35:17 +0000531 new_ts = make_new_session(fd);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000532 if (new_ts) {
533 new_ts->next = sessions;
534 sessions = new_ts;
535 } else {
536 close(fd);
Eric Andersen08a72202002-09-30 20:52:10 +0000537 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000538 }
539#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000540
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000541 /* Then check for data tunneling. */
542 ts = sessions;
543 while (ts) { /* For all sessions... */
544 struct tsession *next = ts->next; /* in case we free ts. */
Eric Andersen08a72202002-09-30 20:52:10 +0000545
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000546 if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) {
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000547 int num_totty;
Denis Vlasenko10916c52007-10-15 17:28:00 +0000548 unsigned char *ptr;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000549 /* Write to pty from buffer 1. */
550 ptr = remove_iacs(ts, &num_totty);
Denis Vlasenko10916c52007-10-15 17:28:00 +0000551 count = safe_write(ts->ptyfd, ptr, num_totty);
552 if (count < 0) {
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000553 if (errno == EAGAIN)
554 goto skip1;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000555 if (IS_INETD)
556 return 0;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000557 goto kill_session;
Eric Andersen08a72202002-09-30 20:52:10 +0000558 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000559 ts->size1 -= count;
560 ts->wridx1 += count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000561 if (ts->wridx1 >= BUFSIZE) /* actually == BUFSIZE */
Eric Andersen08a72202002-09-30 20:52:10 +0000562 ts->wridx1 = 0;
Eric Andersen08a72202002-09-30 20:52:10 +0000563 }
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000564 skip1:
565 if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) {
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000566 /* Write to socket from buffer 2. */
Denis Vlasenko10916c52007-10-15 17:28:00 +0000567 count = MIN(BUFSIZE - ts->wridx2, ts->size2);
568 count = safe_write(ts->sockfd_write, TS_BUF2 + ts->wridx2, count);
569 if (count < 0) {
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000570 if (errno == EAGAIN)
571 goto skip2;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000572 if (IS_INETD)
573 return 0;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000574 goto kill_session;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000575 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000576 ts->size2 -= count;
577 ts->wridx2 += count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000578 if (ts->wridx2 >= BUFSIZE) /* actually == BUFSIZE */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000579 ts->wridx2 = 0;
580 }
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000581 skip2:
Denis Vlasenko10916c52007-10-15 17:28:00 +0000582 /* Should not be needed, but... remove_iacs is actually buggy
583 * (it cannot process iacs which wrap around buffer's end)!
584 * Since properly fixing it requires writing bigger code,
Denis Vlasenkof472b232007-10-16 21:35:17 +0000585 * we rely instead on this code making it virtually impossible
Denis Vlasenko10916c52007-10-15 17:28:00 +0000586 * to have wrapped iac (people don't type at 2k/second).
587 * It also allows for bigger reads in common case. */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000588 if (ts->size1 == 0) {
589 ts->rdidx1 = 0;
590 ts->wridx1 = 0;
591 }
592 if (ts->size2 == 0) {
593 ts->rdidx2 = 0;
594 ts->wridx2 = 0;
595 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000596
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000597 if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) {
598 /* Read from socket to buffer 1. */
Denis Vlasenko10916c52007-10-15 17:28:00 +0000599 count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
600 count = safe_read(ts->sockfd_read, TS_BUF1 + ts->rdidx1, count);
601 if (count <= 0) {
602 if (count < 0 && errno == EAGAIN)
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000603 goto skip3;
604 if (IS_INETD)
605 return 0;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000606 goto kill_session;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000607 }
608 /* Ignore trailing NUL if it is there */
Denis Vlasenko10916c52007-10-15 17:28:00 +0000609 if (!TS_BUF1[ts->rdidx1 + count - 1]) {
Denis Vlasenkof472b232007-10-16 21:35:17 +0000610 --count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000611 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000612 ts->size1 += count;
613 ts->rdidx1 += count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000614 if (ts->rdidx1 >= BUFSIZE) /* actually == BUFSIZE */
615 ts->rdidx1 = 0;
616 }
617 skip3:
618 if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) {
619 /* Read from pty to buffer 2. */
Denis Vlasenko10916c52007-10-15 17:28:00 +0000620 count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
621 count = safe_read(ts->ptyfd, TS_BUF2 + ts->rdidx2, count);
622 if (count <= 0) {
623 if (count < 0 && errno == EAGAIN)
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000624 goto skip4;
625 if (IS_INETD)
626 return 0;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000627 goto kill_session;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000628 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000629 ts->size2 += count;
630 ts->rdidx2 += count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000631 if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */
632 ts->rdidx2 = 0;
633 }
634 skip4:
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000635 ts = next;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000636 continue;
637 kill_session:
638 free_session(ts);
639 ts = next;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000640 }
Denis Vlasenkof472b232007-10-16 21:35:17 +0000641
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000642 goto again;
Eric Andersen08a72202002-09-30 20:52:10 +0000643}