-/* $Id: telnetd.c,v 1.3 2003/01/21 20:55:56 bug1 Exp $
+/* $Id: telnetd.c,v 1.13 2004/09/14 17:24:58 bug1 Exp $
*
* Simple telnet server
* Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/wait.h>
+#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFSIZE 4000
-static const char *loginpath =
#ifdef CONFIG_LOGIN
-"/bin/login";
+static const char *loginpath = "/bin/login";
#else
-"/bin/sh";
+static const char *loginpath;
#endif
static const char *issuefile = "/etc/issue.net";
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) {
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) {
- /* the entire IAC is contained in the buffer
- we were asked to process. */
-#ifdef DEBUG
- fprintf(stderr, "Ignoring IAC %s,%s\n",
- *ptr, TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
-#endif
- ptr += 3;
- } else {
+ /*
+ * TELOPT_NAWS support!
+ */
+ if ((ptr+2) >= end) {
/* only the beginning of the IAC is in the
buffer we were asked to process, we can't
process this char. */
break;
}
+
+ /*
+ * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
+ */
+ else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
+ struct winsize ws;
+ if ((ptr+8) >= end)
+ break; /* incomplete, can't process */
+ ws.ws_col = (ptr[3] << 8) | ptr[4];
+ ws.ws_row = (ptr[5] << 8) | ptr[6];
+ (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
+ ptr += 9;
+ }
+ else {
+ /* skip 3-byte IAC non-SB cmd */
+#ifdef DEBUG
+ fprintf(stderr, "Ignoring IAC %s,%s\n",
+ TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
+#endif
+ ptr += 3;
+ }
}
}
pty = getpty(tty_name);
if (pty < 0) {
- syslog_msg(LOG_USER, LOG_ERR, "All network ports in use!");
+ syslog(LOG_ERR, "All network ports in use!");
return 0;
}
*/
send_iac(ts, DO, TELOPT_ECHO);
+ send_iac(ts, DO, TELOPT_NAWS);
send_iac(ts, DO, TELOPT_LFLOW);
send_iac(ts, WILL, TELOPT_ECHO);
send_iac(ts, WILL, TELOPT_SGA);
if ((pid = fork()) < 0) {
- syslog_msg(LOG_USER, LOG_ERR, "Can`t forking");
+ syslog(LOG_ERR, "Can`t forking");
}
if (pid == 0) {
/* In child, open the child's side of the tty. */
setsid();
if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
- syslog_msg(LOG_USER, LOG_ERR, "Could not open tty");
+ syslog(LOG_ERR, "Could not open tty");
exit(1);
}
dup(0);
execv(loginpath, (char *const *)argv_init);
/* NOT REACHED */
- syslog_msg(LOG_USER, LOG_ERR, "execv error");
+ syslog(LOG_ERR, "execv error");
exit(1);
}
#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);
if (c == EOF) break;
switch (c) {
case 'f':
- issuefile = strdup (optarg);
+ issuefile = optarg;
break;
case 'l':
- loginpath = strdup (optarg);
+ loginpath = optarg;
break;
#ifndef CONFIG_FEATURE_TELNETD_INETD
case 'p':
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();
+ openlog(bb_applet_name, 0, LOG_USER);
+#ifdef CONFIG_FEATURE_TELNETD_INETD
maxfd = 1;
+ sessions = make_new_session();
#else /* CONFIG_EATURE_TELNETD_INETD */
sessions = 0;
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));
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;
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 */
-
+
if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
int num_totty;
char *ptr;