Make sure stdlib.h is always included before dmalloc.h to avoid problems
[oweals/busybox.git] / networking / telnetd.c
index d17682eb40d188cb36326ce2f48e1dd2acd5a454..3d5e8d100c68dd0c77f944caecfd94658aa2bea4 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: telnetd.c,v 1.3 2003/01/21 20:55:56 bug1 Exp $
+/* $Id: telnetd.c,v 1.9 2003/12/19 11:30:13 andersen Exp $
  *
  * Simple telnet server
  * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
 
 #define BUFSIZE 4000
 
-static const char *loginpath 
+static const char *loginpath 
 #ifdef CONFIG_LOGIN
-"/bin/login";
+ = "/bin/login";
 #else
-"/bin/sh";
+;
 #endif
 static const char *issuefile = "/etc/issue.net";
 
@@ -116,6 +116,8 @@ static struct tsession *sessions;
    FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
    what is the escape character?  We aren't handling that situation here.
 
+   CR-LF ->'s CR mapping is also done here, for convenience
+
   */
 static char *
 remove_iacs(struct tsession *ts, int *pnum_totty) {
@@ -128,7 +130,14 @@ remove_iacs(struct tsession *ts, int *pnum_totty) {
 
        while (ptr < end) {
                if (*ptr != IAC) {
+                       int c = *ptr;
                        *totty++ = *ptr++;
+                       /* We now map \r\n ==> \r for pragmatic reasons.
+                        * Many client implementations send \r\n when
+                        * the user hits the CarriageReturn key.
+                        */
+                       if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
+                               ptr++;
                }
                else {
                        if ((ptr+2) < end) {
@@ -363,6 +372,11 @@ telnetd_main(int argc, char **argv)
 #else /* CONFIG_EATURE_TELNETD_INETD */
                "f:l:p:";
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
+       int maxlen, w, r;
+
+#ifndef CONFIG_LOGIN
+       loginpath = DEFAULT_SHELL;
+#endif
 
        for (;;) {
                c = getopt( argc, argv, options);
@@ -380,20 +394,19 @@ telnetd_main(int argc, char **argv)
                                break;
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
                        default:
-                               show_usage();
+                               bb_show_usage();
                }
        }
 
        if (access(loginpath, X_OK) < 0) {
-               error_msg_and_die ("'%s' unavailable.", loginpath);
+               bb_error_msg_and_die ("'%s' unavailable.", loginpath);
        }
 
        argv_init[0] = loginpath;
 
 #ifdef CONFIG_FEATURE_TELNETD_INETD
-       sessions = make_new_session();
-
        maxfd = 1;
+       sessions = make_new_session();
 #else /* CONFIG_EATURE_TELNETD_INETD */
        sessions = 0;
 
@@ -401,7 +414,7 @@ telnetd_main(int argc, char **argv)
 
        master_fd = socket(AF_INET, SOCK_STREAM, 0);
        if (master_fd < 0) {
-               perror_msg_and_die("socket");
+               bb_perror_msg_and_die("socket");
        }
        (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
 
@@ -412,15 +425,15 @@ telnetd_main(int argc, char **argv)
        sa.sin_port = htons(portnbr);
 
        if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
-               perror_msg_and_die("bind");
+               bb_perror_msg_and_die("bind");
        }
 
        if (listen(master_fd, 1) < 0) {
-               perror_msg_and_die("listen");
+               bb_perror_msg_and_die("listen");
        }
 
        if (daemon(0, 0) < 0)
-               perror_msg_and_die("daemon");
+               bb_perror_msg_and_die("daemon");
 
 
        maxfd = master_fd;
@@ -506,7 +519,6 @@ telnetd_main(int argc, char **argv)
                ts = sessions;
                while (ts) { /* For all sessions...  */
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
-                       int maxlen, w, r;
 #ifndef CONFIG_FEATURE_TELNETD_INETD
                        struct tsession *next = ts->next; /* in case we free ts. */
 #endif /* CONFIG_FEATURE_TELNETD_INETD */