/* utelnetd.c
*
* Simple telnet server
*
* Artur Bajor, Centrum Informatyki ROW
* <abajor@cirow.pl>
*
* Joerg Schmitz-Linneweber, Aston GmbH
* <schmitz-linneweber@aston-technologie.de>
*
* Vladimir Oleynik
* <dzo@simtreas.ru>
*
* Robert Schwebel, Pengutronix
* <r.schwebel@pengutronix.de>
*
* Bjorn Wesen, Axis Communications AB
* <bjornw@axis.com>
*
* Sepherosa Ziehau
* <sepherosa@myrealbox.com>
*
* This file is distributed under the GNU General Public License (GPL),
* please see the file LICENSE for further information.
*
* ---------------------------------------------------------------------------
* (C) 2000, 2001, 2002, 2003 by the authors mentioned above
* ---------------------------------------------------------------------------
*
* The telnetd manpage says it all:
*
* Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
* a client, then creating a login process which has the slave side of the
* pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
* master side of the pseudo-terminal, implementing the telnet protocol and
* passing characters between the remote client and the login process.
*/
/* configuration - we'll separate this out later */
#define USE_SYSLOG 1
#define USE_ISSUE 1
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>
/* we need getpty() */
#define __USE_GNU 1
#define __USE_XOPEN 1
#include <stdlib.h>
#undef __USE_GNU
#undef __USE_XOPEN
#include <errno.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#ifdef USE_SYSLOG
#include <syslog.h>
#endif
#include <termios.h>
#ifdef BSD
#define XTABS OXTABS
#define getpt() posix_openpt(O_RDWR|O_NOCTTY)
#endif
#ifdef DEBUG
#define TELCMDS
#define TELOPTS
#endif
#include <arpa/telnet.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <net/if.h>
#define BUFSIZE 4000
#ifdef USE_ISSUE
#define ISSUE_FILE "/etc/issue.net"
#endif
#define MIN(a,b) ((a) > (b) ? (b) : (a))
static char *loginpath = NULL;
/* shell name and arguments */
static char *argv_init[] = {NULL, NULL};
/* structure that describes a session */
struct tsession {
struct tsession *next;
int sockfd, ptyfd;
int shell_pid;
/* two circular buffers */
char *buf1, *buf2;
int rdidx1, wridx1, size1;
int rdidx2, wridx2, size2;
};
#ifdef DEBUG
#define DEBUG_OUT(...) fprintf(stderr, __VA_ARGS__)
#else
#define DEBUG_OUT(...)
//static inline void DEBUG_OUT(const char *format, ...) {};
#endif
/*
This is how the buffers are used. The arrows indicate the movement
of data.
+-------+ wridx1++ +------+ rdidx1++ +----------+
| | <-------------- | buf1 | <-------------- | |
| | size1-- +------+ size1++ | |
| pty | | socket |
| | rdidx2++ +------+ wridx2++ | |
| | --------------> | buf2 | --------------> | |
+-------+ size2++ +------+ size2-- +----------+
Each session has got two buffers.
*/
static int maxfd;
static struct tsession *sessions;
/*
* This code was ported from a version which was made for busybox.
* So we have to define some helper functions which are originally
* available in busybox...
*/
void show_usage(void)
{
printf("Usage: telnetd [-p port] [-i interface] [-l loginprogram] [-d]\n");
printf("\n");
printf(" -p port specify the tcp port to connect to\n");
printf(" -i interface specify the network interface to listen on\n");
printf(" (default: all interfaces)\n");
printf(" -l loginprogram program started by the server\n");
printf(" -d daemonize\n");
printf("\n");
exit(1);
}
void perror_msg_and_die(char *text)
{
fprintf(stderr,text);
exit(1);
}
void error_msg_and_die(char *text)
{
perror_msg_and_die(text);
}
/*
Remove all IAC's from the buffer pointed to by bf (recieved IACs are ignored
and must be removed so as to not be interpreted by the terminal). Make an
uninterrupted string of characters fit for the terminal. Do this by packing
all characters meant for the terminal sequentially towards the end of bf.
Return a pointer to the beginning of the characters meant for the terminal.
and make *processed equal to the number of characters that were actually
processed and *num_totty the number of characters that should be sent to
the terminal.
Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
past (bf + len) then that IAC will be left unprocessed and *processed will be
less than len.
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.
*/
static char *
remove_iacs(unsigned char *bf, int len, int *processed, int *num_totty) {
unsigned char *ptr = bf;
unsigned char *totty = bf;
unsigned char *end = bf + len;
while (ptr < end) {
if (*ptr != IAC) {
*totty++ = *ptr++;
}
else {
if ((ptr+2) < end) {
/* the entire IAC is contained in the buffer
we were asked to process. */
DEBUG_OUT("Ignoring IAC 0x%02x, %s, %s\n", *ptr, TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
ptr += 3;
} else {
/* only the beginning of the IAC is in the
buffer we were asked to process, we can't
process this char. */
break;
}
}
}
*processed = ptr - bf;
*num_totty = totty - bf;
/* move the chars meant for the terminal towards the end of the
buffer. */
return memmove(ptr - *num_totty, bf, *num_totty);
}
static int getpty(char *line)
{
int p;
p = getpt();
if (p < 0) {
DEBUG_OUT("getpty(): couldn't get pty\n");
close(p);
return -1;
}
if (grantpt(p)<0 || unlockpt(p)<0) {
DEBUG_OUT("getpty(): couldn't grant and unlock pty\n");
close(p);
return -1;
}
DEBUG_OUT("getpty(): got pty %s\n",ptsname(p));
strcpy(line, (const char*)ptsname(p));
return(p);
}
static void
send_iac(struct tsession *ts, unsigned char command, int option)
{
/* We rely on that there is space in the buffer for now. */
char *b = ts->buf2 + ts->rdidx2;
*b++ = IAC;
*b++ = command;
*b++ = option;
ts->rdidx2 += 3;
ts->size2 += 3;
}
static struct tsession *
make_new_session(int sockfd)
{
struct termios termbuf;
int pty, pid;
static char tty_name[32];
struct tsession *ts = (struct tsession *)malloc(sizeof(struct tsession));
int t1, t2;
#ifdef USE_ISSUE
FILE *fp;
int chr;
#endif
ts->buf1 = (char *)malloc(BUFSIZE);
ts->buf2 = (char *)malloc(BUFSIZE);
ts->sockfd = sockfd;
ts->rdidx1 = ts->wridx1 = ts->size1 = 0;
ts->rdidx2 = ts->wridx2 = ts->size2 = 0;
/* Got a new connection, set up a tty and spawn a shell. */
pty = getpty(tty_name);
if (pty < 0) {
fprintf(stderr, "All network ports in use!\n");
return 0;
}
if (pty > maxfd)
maxfd = pty;
ts->ptyfd = pty;
/* Make the telnet client understand we will echo characters so it
* should not do it locally. We don't tell the client to run linemode,
* because we want to handle line editing and tab completion and other
* stuff that requires char-by-char support.
*/
send_iac(ts, DO, TELOPT_ECHO);
send_iac(ts, DO, TELOPT_LFLOW);
send_iac(ts, WILL, TELOPT_ECHO);
send_iac(ts, WILL, TELOPT_SGA);
if ((pid = fork()) < 0) {
perror("fork");
}
if (pid == 0) {
/* In child, open the child's side of the tty. */
int i, t;
for(i = 0; i <= maxfd; i++)
close(i);
/* make new process group */
if (setsid() < 0)
perror_msg_and_die("setsid");
t = open(tty_name, O_RDWR | O_NOCTTY);
//t = open(tty_name, O_RDWR);
if (t < 0)
perror_msg_and_die("Could
评论0