1 /* $Id: telnetd.c,v 1.8 2003/09/12 11:27:15 bug1 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.
121 remove_iacs(struct tsession *ts, int *pnum_totty) {
122 unsigned char *ptr0 = ts->buf1 + ts->wridx1;
123 unsigned char *ptr = ptr0;
124 unsigned char *totty = ptr;
125 unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
135 /* the entire IAC is contained in the buffer
136 we were asked to process. */
138 fprintf(stderr, "Ignoring IAC %s,%s\n",
139 *ptr, TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
143 /* only the beginning of the IAC is in the
144 buffer we were asked to process, we can't
145 process this char. */
151 processed = ptr - ptr0;
152 num_totty = totty - ptr0;
153 /* the difference between processed and num_to tty
154 is all the iacs we removed from the stream.
155 Adjust buf1 accordingly. */
156 ts->wridx1 += processed - num_totty;
157 ts->size1 -= processed - num_totty;
158 *pnum_totty = num_totty;
159 /* move the chars meant for the terminal towards the end of the
161 return memmove(ptr - num_totty, ptr0, num_totty);
169 #ifdef CONFIG_FEATURE_DEVPTS
170 p = open("/dev/ptmx", 2);
174 strcpy(line, ptsname(p));
182 strcpy(line, "/dev/ptyXX");
184 for (i = 0; i < 16; i++) {
185 line[8] = "pqrstuvwxyzabcde"[i];
187 if (stat(line, &stb) < 0) {
190 for (j = 0; j < 16; j++) {
191 line[9] = j < 10 ? j + '0' : j - 10 + 'a';
192 if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) {
198 #endif /* CONFIG_FEATURE_DEVPTS */
204 send_iac(struct tsession *ts, unsigned char command, int option)
206 /* We rely on that there is space in the buffer for now. */
207 char *b = ts->buf2 + ts->rdidx2;
216 static struct tsession *
217 #ifdef CONFIG_FEATURE_TELNETD_INETD
218 make_new_session(void)
219 #else /* CONFIG_FEATURE_TELNETD_INETD */
220 make_new_session(int sockfd)
221 #endif /* CONFIG_FEATURE_TELNETD_INETD */
223 struct termios termbuf;
226 struct tsession *ts = malloc(sizeof(struct tsession) + BUFSIZE * 2);
228 ts->buf1 = (char *)(&ts[1]);
229 ts->buf2 = ts->buf1 + BUFSIZE;
231 #ifdef CONFIG_FEATURE_TELNETD_INETD
233 ts->sockfd_write = 1;
234 #else /* CONFIG_FEATURE_TELNETD_INETD */
236 #endif /* CONFIG_FEATURE_TELNETD_INETD */
238 ts->rdidx1 = ts->wridx1 = ts->size1 = 0;
239 ts->rdidx2 = ts->wridx2 = ts->size2 = 0;
241 /* Got a new connection, set up a tty and spawn a shell. */
243 pty = getpty(tty_name);
246 syslog_msg(LOG_USER, LOG_ERR, "All network ports in use!");
255 /* Make the telnet client understand we will echo characters so it
256 * should not do it locally. We don't tell the client to run linemode,
257 * because we want to handle line editing and tab completion and other
258 * stuff that requires char-by-char support.
261 send_iac(ts, DO, TELOPT_ECHO);
262 send_iac(ts, DO, TELOPT_LFLOW);
263 send_iac(ts, WILL, TELOPT_ECHO);
264 send_iac(ts, WILL, TELOPT_SGA);
267 if ((pid = fork()) < 0) {
268 syslog_msg(LOG_USER, LOG_ERR, "Can`t forking");
271 /* In child, open the child's side of the tty. */
274 for(i = 0; i <= maxfd; i++)
276 /* make new process group */
279 if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
280 syslog_msg(LOG_USER, LOG_ERR, "Could not open tty");
286 tcsetpgrp(0, getpid());
288 /* The pseudo-terminal allocated to the client is configured to operate in
289 * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
292 tcgetattr(0, &termbuf);
293 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
294 termbuf.c_oflag |= ONLCR|XTABS;
295 termbuf.c_iflag |= ICRNL;
296 termbuf.c_iflag &= ~IXOFF;
297 /*termbuf.c_lflag &= ~ICANON;*/
298 tcsetattr(0, TCSANOW, &termbuf);
300 print_login_issue(issuefile, NULL);
302 /* exec shell, with correct argv and env */
303 execv(loginpath, (char *const *)argv_init);
306 syslog_msg(LOG_USER, LOG_ERR, "execv error");
315 #ifndef CONFIG_FEATURE_TELNETD_INETD
317 free_session(struct tsession *ts)
319 struct tsession *t = sessions;
321 /* Unlink this telnet session from the session list. */
330 kill(ts->shell_pid, SIGKILL);
332 wait4(ts->shell_pid, NULL, 0, NULL);
337 if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
339 if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
344 #endif /* CONFIG_FEATURE_TELNETD_INETD */
347 telnetd_main(int argc, char **argv)
349 #ifndef CONFIG_FEATURE_TELNETD_INETD
350 struct sockaddr_in sa;
352 #endif /* CONFIG_FEATURE_TELNETD_INETD */
353 fd_set rdfdset, wrfdset;
355 #ifndef CONFIG_FEATURE_TELNETD_INETD
358 #endif /* CONFIG_FEATURE_TELNETD_INETD */
360 static const char options[] =
361 #ifdef CONFIG_FEATURE_TELNETD_INETD
363 #else /* CONFIG_EATURE_TELNETD_INETD */
365 #endif /* CONFIG_FEATURE_TELNETD_INETD */
369 loginpath = DEFAULT_SHELL;
373 c = getopt( argc, argv, options);
377 issuefile = strdup (optarg);
380 loginpath = strdup (optarg);
382 #ifndef CONFIG_FEATURE_TELNETD_INETD
384 portnbr = atoi(optarg);
386 #endif /* CONFIG_FEATURE_TELNETD_INETD */
392 if (access(loginpath, X_OK) < 0) {
393 bb_error_msg_and_die ("'%s' unavailable.", loginpath);
396 argv_init[0] = loginpath;
398 #ifdef CONFIG_FEATURE_TELNETD_INETD
400 sessions = make_new_session();
401 #else /* CONFIG_EATURE_TELNETD_INETD */
404 /* Grab a TCP socket. */
406 master_fd = socket(AF_INET, SOCK_STREAM, 0);
408 bb_perror_msg_and_die("socket");
410 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
412 /* Set it to listen to specified port. */
414 memset((void *)&sa, 0, sizeof(sa));
415 sa.sin_family = AF_INET;
416 sa.sin_port = htons(portnbr);
418 if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
419 bb_perror_msg_and_die("bind");
422 if (listen(master_fd, 1) < 0) {
423 bb_perror_msg_and_die("listen");
426 if (daemon(0, 0) < 0)
427 bb_perror_msg_and_die("daemon");
431 #endif /* CONFIG_FEATURE_TELNETD_INETD */
439 /* select on the master socket, all telnet sockets and their
440 * ptys if there is room in their respective session buffers.
443 #ifndef CONFIG_FEATURE_TELNETD_INETD
444 FD_SET(master_fd, &rdfdset);
445 #endif /* CONFIG_FEATURE_TELNETD_INETD */
448 #ifndef CONFIG_FEATURE_TELNETD_INETD
450 #endif /* CONFIG_FEATURE_TELNETD_INETD */
451 /* buf1 is used from socket to pty
452 * buf2 is used from pty to socket
455 FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
457 if (ts->size1 < BUFSIZE) {
458 #ifdef CONFIG_FEATURE_TELNETD_INETD
459 FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
460 #else /* CONFIG_FEATURE_TELNETD_INETD */
461 FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
462 #endif /* CONFIG_FEATURE_TELNETD_INETD */
465 #ifdef CONFIG_FEATURE_TELNETD_INETD
466 FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
467 #else /* CONFIG_FEATURE_TELNETD_INETD */
468 FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
469 #endif /* CONFIG_FEATURE_TELNETD_INETD */
471 if (ts->size2 < BUFSIZE) {
472 FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
474 #ifndef CONFIG_FEATURE_TELNETD_INETD
477 #endif /* CONFIG_FEATURE_TELNETD_INETD */
479 selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
484 #ifndef CONFIG_FEATURE_TELNETD_INETD
485 /* First check for and accept new sessions. */
486 if (FD_ISSET(master_fd, &rdfdset)) {
490 if ((fd = accept(master_fd, (struct sockaddr *)&sa,
494 /* Create a new session and link it into
496 struct tsession *new_ts = make_new_session(fd);
498 new_ts->next = sessions;
508 /* Then check for data tunneling. */
511 while (ts) { /* For all sessions... */
512 #endif /* CONFIG_FEATURE_TELNETD_INETD */
513 #ifndef CONFIG_FEATURE_TELNETD_INETD
514 struct tsession *next = ts->next; /* in case we free ts. */
515 #endif /* CONFIG_FEATURE_TELNETD_INETD */
517 if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
520 /* Write to pty from buffer 1. */
522 ptr = remove_iacs(ts, &num_totty);
524 w = write(ts->ptyfd, ptr, num_totty);
526 #ifdef CONFIG_FEATURE_TELNETD_INETD
528 #else /* CONFIG_FEATURE_TELNETD_INETD */
532 #endif /* CONFIG_FEATURE_TELNETD_INETD */
536 if (ts->wridx1 == BUFSIZE)
540 #ifdef CONFIG_FEATURE_TELNETD_INETD
541 if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
542 #else /* CONFIG_FEATURE_TELNETD_INETD */
543 if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
544 #endif /* CONFIG_FEATURE_TELNETD_INETD */
545 /* Write to socket from buffer 2. */
546 maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
547 #ifdef CONFIG_FEATURE_TELNETD_INETD
548 w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
551 #else /* CONFIG_FEATURE_TELNETD_INETD */
552 w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
558 #endif /* CONFIG_FEATURE_TELNETD_INETD */
561 if (ts->wridx2 == BUFSIZE)
565 #ifdef CONFIG_FEATURE_TELNETD_INETD
566 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
567 #else /* CONFIG_FEATURE_TELNETD_INETD */
568 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
569 #endif /* CONFIG_FEATURE_TELNETD_INETD */
570 /* Read from socket to buffer 1. */
571 maxlen = MIN(BUFSIZE - ts->rdidx1,
572 BUFSIZE - ts->size1);
573 #ifdef CONFIG_FEATURE_TELNETD_INETD
574 r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
575 if (!r || (r < 0 && errno != EINTR))
577 #else /* CONFIG_FEATURE_TELNETD_INETD */
578 r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
579 if (!r || (r < 0 && errno != EINTR)) {
584 #endif /* CONFIG_FEATURE_TELNETD_INETD */
585 if(!*(ts->buf1 + ts->rdidx1 + r - 1)) {
592 if (ts->rdidx1 == BUFSIZE)
596 if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
597 /* Read from pty to buffer 2. */
598 maxlen = MIN(BUFSIZE - ts->rdidx2,
599 BUFSIZE - ts->size2);
600 r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
601 if (!r || (r < 0 && errno != EINTR)) {
602 #ifdef CONFIG_FEATURE_TELNETD_INETD
604 #else /* CONFIG_FEATURE_TELNETD_INETD */
608 #endif /* CONFIG_FEATURE_TELNETD_INETD */
612 if (ts->rdidx2 == BUFSIZE)
616 if (ts->size1 == 0) {
620 if (ts->size2 == 0) {
624 #ifndef CONFIG_FEATURE_TELNETD_INETD
627 #endif /* CONFIG_FEATURE_TELNETD_INETD */