find: support -HLP
[oweals/busybox.git] / networking / telnetd.c
index b3e66eb4b081012d8a89a5df88a3c0470c596d53..9e7a84cce200c2bfa2cf62d1493bed60cf1d2732 100644 (file)
@@ -3,7 +3,7 @@
  * Simple telnet server
  * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  *
  * ---------------------------------------------------------------------------
  * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
  * Vladimir Oleynik <dzo@simtreas.ru> 2001
  * Set process group corrections, initial busybox port
  */
+
+//usage:#define telnetd_trivial_usage
+//usage:       "[OPTIONS]"
+//usage:#define telnetd_full_usage "\n\n"
+//usage:       "Handle incoming telnet connections"
+//usage:       IF_NOT_FEATURE_TELNETD_STANDALONE(" via inetd") "\n"
+//usage:     "\n       -l LOGIN        Exec LOGIN on connect"
+//usage:     "\n       -f ISSUE_FILE   Display ISSUE_FILE instead of /etc/issue"
+//usage:     "\n       -K              Close connection as soon as login exits"
+//usage:     "\n                       (normally wait until all programs close slave pty)"
+//usage:       IF_FEATURE_TELNETD_STANDALONE(
+//usage:     "\n       -p PORT         Port to listen on"
+//usage:     "\n       -b ADDR[:PORT]  Address to bind to"
+//usage:     "\n       -F              Run in foreground"
+//usage:     "\n       -i              Inetd mode"
+//usage:       IF_FEATURE_TELNETD_INETD_WAIT(
+//usage:     "\n       -w SEC          Inetd 'wait' mode, linger time SEC"
+//usage:     "\n       -S              Log to syslog (implied by -i or without -F and -w)"
+//usage:       )
+//usage:       )
+
 #define DEBUG 0
 
 #include "libbb.h"
 #endif
 #include <arpa/telnet.h>
 
-#if ENABLE_FEATURE_UTMP
-# include <utmp.h> /* LOGIN_PROCESS */
-#endif
-
 
 struct tsession {
        struct tsession *next;
@@ -108,6 +125,7 @@ remove_iacs(struct tsession *ts, int *pnum_totty)
                        /* We map \r\n ==> \r for pragmatic reasons.
                         * Many client implementations send \r\n when
                         * the user hits the CarriageReturn key.
+                        * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
                         */
                        if (c == '\r' && ptr < end && (*ptr == '\n' || *ptr == '\0'))
                                ptr++;
@@ -141,7 +159,7 @@ remove_iacs(struct tsession *ts, int *pnum_totty)
                if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
                        struct winsize ws;
                        if ((ptr+8) >= end)
-                               break;  /* incomplete, can't process */
+                               break;  /* incomplete, can't process */
                        ws.ws_col = (ptr[3] << 8) | ptr[4];
                        ws.ws_row = (ptr[5] << 8) | ptr[6];
                        ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
@@ -226,6 +244,9 @@ make_new_session(
                IF_FEATURE_TELNETD_STANDALONE(int sock)
                IF_NOT_FEATURE_TELNETD_STANDALONE(void)
 ) {
+#if !ENABLE_FEATURE_TELNETD_STANDALONE
+       enum { sock = 0 };
+#endif
        const char *login_argv[2];
        struct termios termbuf;
        int fd, pid;
@@ -243,9 +264,9 @@ make_new_session(
        ndelay_on(fd);
        close_on_exec_on(fd);
 
-#if ENABLE_FEATURE_TELNETD_STANDALONE
        /* SO_KEEPALIVE by popular demand */
        setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
+#if ENABLE_FEATURE_TELNETD_STANDALONE
        ts->sockfd_read = sock;
        ndelay_on(sock);
        if (sock == 0) { /* We are called with fd 0 - we are in inetd mode */
@@ -256,8 +277,6 @@ make_new_session(
        if (sock > G.maxfd)
                G.maxfd = sock;
 #else
-       /* SO_KEEPALIVE by popular demand */
-       setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
        /* ts->sockfd_read = 0; - done by xzalloc */
        ts->sockfd_write = 1;
        ndelay_on(0);
@@ -272,8 +291,8 @@ make_new_session(
                static const char iacs_to_send[] ALIGN1 = {
                        IAC, DO, TELOPT_ECHO,
                        IAC, DO, TELOPT_NAWS,
-               /* This requires telnetd.ctrlSQ.patch (incomplete) */
-               /*      IAC, DO, TELOPT_LFLOW, */
+                       /* This requires telnetd.ctrlSQ.patch (incomplete) */
+                       /*IAC, DO, TELOPT_LFLOW,*/
                        IAC, WILL, TELOPT_ECHO,
                        IAC, WILL, TELOPT_SGA
                };
@@ -313,6 +332,19 @@ make_new_session(
        /* Restore default signal handling ASAP */
        bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
 
+       pid = getpid();
+
+       if (ENABLE_FEATURE_UTMP) {
+               len_and_sockaddr *lsa = get_peer_lsa(sock);
+               char *hostname = NULL;
+               if (lsa) {
+                       hostname = xmalloc_sockaddr2dotted(&lsa->u.sa);
+                       free(lsa);
+               }
+               write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", hostname);
+               free(hostname);
+       }
+
        /* Make new session and process group */
        setsid();
 
@@ -323,12 +355,8 @@ make_new_session(
        xopen(tty_name, O_RDWR); /* becomes our ctty */
        xdup2(0, 1);
        xdup2(0, 2);
-       pid = getpid();
        tcsetpgrp(0, pid); /* switch this tty's process group to us */
 
-//TODO: fetch remote addr via getpeername (see ftpd.c)
-       write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", /*hostname:*/ NULL);
-
        /* The pseudo-terminal allocated to the client is configured to operate
         * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */
        tcgetattr(0, &termbuf);