1 /* $Id: telnetd.c,v 1.9 2003/12/19 11:30:13 andersen Exp $
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
6 * This file is distributed under the Gnu Public License (GPL),
7 * please see the file LICENSE for further information.
9 * ---------------------------------------------------------------------------
10 * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
11 ****************************************************************************
13 * The telnetd manpage says it all:
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.
21 * Vladimir Oleynik <dzo@simtreas.ru> 2001
22 * Set process group corrections, initial busybox port
28 #include <sys/socket.h>
34 #include <netinet/in.h>
43 #include <arpa/telnet.h>
45 #include <sys/syslog.h>
51 static const char *loginpath
57 static const char *issuefile = "/etc/issue.net";
59 /* shell name and arguments */
61 static const char *argv_init[] = {NULL, NULL};
63 /* structure that describes a session */
66 #ifdef CONFIG_FEATURE_TELNETD_INETD
67 int sockfd_read, sockfd_write, ptyfd;
68 #else /* CONFIG_FEATURE_TELNETD_INETD */
69 struct tsession *next;
71 #endif /* CONFIG_FEATURE_TELNETD_INETD */
73 /* two circular buffers */
75 int rdidx1, wridx1, size1;
76 int rdidx2, wridx2, size2;
81 This is how the buffers are used. The arrows indicate the movement
84 +-------+ wridx1++ +------+ rdidx1++ +----------+
85 | | <-------------- | buf1 | <-------------- | |
86 | | size1-- +------+ size1++ | |
88 | | rdidx2++ +------+ wridx2++ | |
89 | | --------------> | buf2 | --------------> | |
90 +-------+ size2++ +------+ size2-- +----------+
92 Each session has got two buffers.
98 static struct tsession *sessions;
103 Remove all IAC's from the buffer pointed to by bf (recieved IACs are ignored
104 and must be removed so as to not be interpreted by the terminal). Make an
105 uninterrupted string of characters fit for the terminal. Do this by packing
106 all characters meant for the terminal sequentially towards the end of bf.
108 Return a pointer to the beginning of the characters meant for the terminal.
109 and make *num_totty the number of characters that should be sent to
112 Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
113 past (bf + len) then that IAC will be left unprocessed and *processed will be
116 FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
117 what is the escape character? We aren't handling that situation here.
119 CR-LF ->'s CR mapping is also done here, for convenience
123 remove_iacs(struct tsession *ts, int *pnum_totty) {
124 unsigned char *ptr0 = ts->buf1 + ts->wridx1;
125 unsigned char *ptr = ptr0;
126 unsigned char *totty = ptr;
127 unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
135 /* We now map \r\n ==> \r for pragmatic reasons.
136 * Many client implementations send \r\n when
137 * the user hits the CarriageReturn key.
139 if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
144 /* the entire IAC is contained in the buffer
145 we were asked to process. */
147 fprintf(stderr, "Ignoring IAC %s,%s\n",
148 *ptr, TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
152 /* only the beginning of the IAC is in the
153 buffer we were asked to process, we can't
154 process this char. */
160 processed = ptr - ptr0;
161 num_totty = totty - ptr0;
162 /* the difference between processed and num_to tty
163 is all the iacs we removed from the stream.
164 Adjust buf1 accordingly. */
165 ts->wridx1 += processed - num_totty;
166 ts->size1 -= processed - num_totty;
167 *pnum_totty = num_totty;
168 /* move the chars meant for the terminal towards the end of the
170 return memmove(ptr - num_totty, ptr0, num_totty);
178 #ifdef CONFIG_FEATURE_DEVPTS
179 p = open("/dev/ptmx", 2);
183 strcpy(line, ptsname(p));
191 strcpy(line, "/dev/ptyXX");
193 for (i = 0; i < 16; i++) {
194 line[8] = "pqrstuvwxyzabcde"[i];
196 if (stat(line, &stb) < 0) {
199 for (j = 0; j < 16; j++) {
200 line[9] = j < 10 ? j + '0' : j - 10 + 'a';
201 if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) {
207 #endif /* CONFIG_FEATURE_DEVPTS */
213 send_iac(struct tsession *ts, unsigned char command, int option)
215 /* We rely on that there is space in the buffer for now. */
216 char *b = ts->buf2 + ts->rdidx2;
225 static struct tsession *
226 #ifdef CONFIG_FEATURE_TELNETD_INETD
227 make_new_session(void)
228 #else /* CONFIG_FEATURE_TELNETD_INETD */
229 make_new_session(int sockfd)
230 #endif /* CONFIG_FEATURE_TELNETD_INETD */
232 struct termios termbuf;
235 struct tsession *ts = malloc(sizeof(struct tsession) + BUFSIZE * 2);
237 ts->buf1 = (char *)(&ts[1]);
238 ts->buf2 = ts->buf1 + BUFSIZE;
240 #ifdef CONFIG_FEATURE_TELNETD_INETD
242 ts->sockfd_write = 1;
243 #else /* CONFIG_FEATURE_TELNETD_INETD */
245 #endif /* CONFIG_FEATURE_TELNETD_INETD */
247 ts->rdidx1 = ts->wridx1 = ts->size1 = 0;
248 ts->rdidx2 = ts->wridx2 = ts->size2 = 0;
250 /* Got a new connection, set up a tty and spawn a shell. */
252 pty = getpty(tty_name);
255 syslog_msg(LOG_USER, LOG_ERR, "All network ports in use!");
264 /* Make the telnet client understand we will echo characters so it
265 * should not do it locally. We don't tell the client to run linemode,
266 * because we want to handle line editing and tab completion and other
267 * stuff that requires char-by-char support.
270 send_iac(ts, DO, TELOPT_ECHO);
271 send_iac(ts, DO, TELOPT_LFLOW);
272 send_iac(ts, WILL, TELOPT_ECHO);
273 send_iac(ts, WILL, TELOPT_SGA);
276 if ((pid = fork()) < 0) {
277 syslog_msg(LOG_USER, LOG_ERR, "Can`t forking");
280 /* In child, open the child's side of the tty. */
283 for(i = 0; i <= maxfd; i++)
285 /* make new process group */
288 if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
289 syslog_msg(LOG_USER, LOG_ERR, "Could not open tty");
295 tcsetpgrp(0, getpid());
297 /* The pseudo-terminal allocated to the client is configured to operate in
298 * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
301 tcgetattr(0, &termbuf);
302 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
303 termbuf.c_oflag |= ONLCR|XTABS;
304 termbuf.c_iflag |= ICRNL;
305 termbuf.c_iflag &= ~IXOFF;
306 /*termbuf.c_lflag &= ~ICANON;*/
307 tcsetattr(0, TCSANOW, &termbuf);
309 print_login_issue(issuefile, NULL);
311 /* exec shell, with correct argv and env */
312 execv(loginpath, (char *const *)argv_init);
315 syslog_msg(LOG_USER, LOG_ERR, "execv error");
324 #ifndef CONFIG_FEATURE_TELNETD_INETD
326 free_session(struct tsession *ts)
328 struct tsession *t = sessions;
330 /* Unlink this telnet session from the session list. */
339 kill(ts->shell_pid, SIGKILL);
341 wait4(ts->shell_pid, NULL, 0, NULL);
346 if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
348 if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
353 #endif /* CONFIG_FEATURE_TELNETD_INETD */
356 telnetd_main(int argc, char **argv)
358 #ifndef CONFIG_FEATURE_TELNETD_INETD
359 struct sockaddr_in sa;
361 #endif /* CONFIG_FEATURE_TELNETD_INETD */
362 fd_set rdfdset, wrfdset;
364 #ifndef CONFIG_FEATURE_TELNETD_INETD
367 #endif /* CONFIG_FEATURE_TELNETD_INETD */
369 static const char options[] =
370 #ifdef CONFIG_FEATURE_TELNETD_INETD
372 #else /* CONFIG_EATURE_TELNETD_INETD */
374 #endif /* CONFIG_FEATURE_TELNETD_INETD */
378 loginpath = DEFAULT_SHELL;
382 c = getopt( argc, argv, options);
386 issuefile = strdup (optarg);
389 loginpath = strdup (optarg);
391 #ifndef CONFIG_FEATURE_TELNETD_INETD
393 portnbr = atoi(optarg);
395 #endif /* CONFIG_FEATURE_TELNETD_INETD */
401 if (access(loginpath, X_OK) < 0) {
402 bb_error_msg_and_die ("'%s' unavailable.", loginpath);
405 argv_init[0] = loginpath;
407 #ifdef CONFIG_FEATURE_TELNETD_INETD
409 sessions = make_new_session();
410 #else /* CONFIG_EATURE_TELNETD_INETD */
413 /* Grab a TCP socket. */
415 master_fd = socket(AF_INET, SOCK_STREAM, 0);
417 bb_perror_msg_and_die("socket");
419 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
421 /* Set it to listen to specified port. */
423 memset((void *)&sa, 0, sizeof(sa));
424 sa.sin_family = AF_INET;
425 sa.sin_port = htons(portnbr);
427 if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
428 bb_perror_msg_and_die("bind");
431 if (listen(master_fd, 1) < 0) {
432 bb_perror_msg_and_die("listen");
435 if (daemon(0, 0) < 0)
436 bb_perror_msg_and_die("daemon");
440 #endif /* CONFIG_FEATURE_TELNETD_INETD */
448 /* select on the master socket, all telnet sockets and their
449 * ptys if there is room in their respective session buffers.
452 #ifndef CONFIG_FEATURE_TELNETD_INETD
453 FD_SET(master_fd, &rdfdset);
454 #endif /* CONFIG_FEATURE_TELNETD_INETD */
457 #ifndef CONFIG_FEATURE_TELNETD_INETD
459 #endif /* CONFIG_FEATURE_TELNETD_INETD */
460 /* buf1 is used from socket to pty
461 * buf2 is used from pty to socket
464 FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
466 if (ts->size1 < BUFSIZE) {
467 #ifdef CONFIG_FEATURE_TELNETD_INETD
468 FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
469 #else /* CONFIG_FEATURE_TELNETD_INETD */
470 FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
471 #endif /* CONFIG_FEATURE_TELNETD_INETD */
474 #ifdef CONFIG_FEATURE_TELNETD_INETD
475 FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
476 #else /* CONFIG_FEATURE_TELNETD_INETD */
477 FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
478 #endif /* CONFIG_FEATURE_TELNETD_INETD */
480 if (ts->size2 < BUFSIZE) {
481 FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
483 #ifndef CONFIG_FEATURE_TELNETD_INETD
486 #endif /* CONFIG_FEATURE_TELNETD_INETD */
488 selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
493 #ifndef CONFIG_FEATURE_TELNETD_INETD
494 /* First check for and accept new sessions. */
495 if (FD_ISSET(master_fd, &rdfdset)) {
499 if ((fd = accept(master_fd, (struct sockaddr *)&sa,
503 /* Create a new session and link it into
505 struct tsession *new_ts = make_new_session(fd);
507 new_ts->next = sessions;
517 /* Then check for data tunneling. */
520 while (ts) { /* For all sessions... */
521 #endif /* CONFIG_FEATURE_TELNETD_INETD */
522 #ifndef CONFIG_FEATURE_TELNETD_INETD
523 struct tsession *next = ts->next; /* in case we free ts. */
524 #endif /* CONFIG_FEATURE_TELNETD_INETD */
526 if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
529 /* Write to pty from buffer 1. */
531 ptr = remove_iacs(ts, &num_totty);
533 w = write(ts->ptyfd, ptr, num_totty);
535 #ifdef CONFIG_FEATURE_TELNETD_INETD
537 #else /* CONFIG_FEATURE_TELNETD_INETD */
541 #endif /* CONFIG_FEATURE_TELNETD_INETD */
545 if (ts->wridx1 == BUFSIZE)
549 #ifdef CONFIG_FEATURE_TELNETD_INETD
550 if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
551 #else /* CONFIG_FEATURE_TELNETD_INETD */
552 if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
553 #endif /* CONFIG_FEATURE_TELNETD_INETD */
554 /* Write to socket from buffer 2. */
555 maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
556 #ifdef CONFIG_FEATURE_TELNETD_INETD
557 w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
560 #else /* CONFIG_FEATURE_TELNETD_INETD */
561 w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
567 #endif /* CONFIG_FEATURE_TELNETD_INETD */
570 if (ts->wridx2 == BUFSIZE)
574 #ifdef CONFIG_FEATURE_TELNETD_INETD
575 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
576 #else /* CONFIG_FEATURE_TELNETD_INETD */
577 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
578 #endif /* CONFIG_FEATURE_TELNETD_INETD */
579 /* Read from socket to buffer 1. */
580 maxlen = MIN(BUFSIZE - ts->rdidx1,
581 BUFSIZE - ts->size1);
582 #ifdef CONFIG_FEATURE_TELNETD_INETD
583 r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
584 if (!r || (r < 0 && errno != EINTR))
586 #else /* CONFIG_FEATURE_TELNETD_INETD */
587 r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
588 if (!r || (r < 0 && errno != EINTR)) {
593 #endif /* CONFIG_FEATURE_TELNETD_INETD */
594 if(!*(ts->buf1 + ts->rdidx1 + r - 1)) {
601 if (ts->rdidx1 == BUFSIZE)
605 if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
606 /* Read from pty to buffer 2. */
607 maxlen = MIN(BUFSIZE - ts->rdidx2,
608 BUFSIZE - ts->size2);
609 r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
610 if (!r || (r < 0 && errno != EINTR)) {
611 #ifdef CONFIG_FEATURE_TELNETD_INETD
613 #else /* CONFIG_FEATURE_TELNETD_INETD */
617 #endif /* CONFIG_FEATURE_TELNETD_INETD */
621 if (ts->rdidx2 == BUFSIZE)
625 if (ts->size1 == 0) {
629 if (ts->size2 == 0) {
633 #ifndef CONFIG_FEATURE_TELNETD_INETD
636 #endif /* CONFIG_FEATURE_TELNETD_INETD */