2007-01-19 21:19:35 +00:00
|
|
|
/* NB: this file is to be removed soon. See isrv_identd.c */
|
|
|
|
|
2005-04-17 07:24:19 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* A fake identd server
|
|
|
|
*
|
|
|
|
* Adapted to busybox by Thomas Lundquist <thomasez@zelow.no>
|
|
|
|
* Original Author: Tomi Ollila <too@iki.fi>
|
|
|
|
* http://www.guru-group.fi/~too/sw/
|
|
|
|
*
|
2006-04-12 17:55:51 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2005-04-17 07:24:19 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
/* Ident crash course
|
|
|
|
*
|
|
|
|
* Incoming requests are of form "6191, 23\r\n" - peer asks us
|
|
|
|
* "which user connected from your port 6191 to my port 23?"
|
|
|
|
* We should answer:
|
|
|
|
* "6193, 23 : USERID : UNIX : username\r\n"
|
|
|
|
* and close the connection.
|
|
|
|
* We can also reply:
|
|
|
|
* "6195, 23 : USERID : OTHER[,US-ASCII] : username\r\n"
|
|
|
|
* "6195, 23 : ERROR : INVALID-PORT/NO-USER/HIDDEN-USER/UNKNOWN-ERROR\r\n"
|
|
|
|
* but we probably will never want that.
|
|
|
|
*/
|
|
|
|
|
2006-06-05 17:35:24 +00:00
|
|
|
#include "busybox.h"
|
2007-01-11 23:26:13 +00:00
|
|
|
|
|
|
|
#define SANE_INETD_ONLY_VERSION
|
|
|
|
|
|
|
|
#ifdef SANE_INETD_ONLY_VERSION
|
|
|
|
|
|
|
|
int fakeidentd_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
const char *bogouser = "nobody";
|
|
|
|
char *cur = buf;
|
|
|
|
int rem = sizeof(buf)-1;
|
|
|
|
|
|
|
|
if (argv[1])
|
|
|
|
bogouser = argv[1];
|
|
|
|
|
|
|
|
alarm(30);
|
|
|
|
while (1) {
|
|
|
|
char *p;
|
|
|
|
int sz = safe_read(0, cur, rem);
|
|
|
|
if (sz < 0) return 1;
|
|
|
|
cur[sz] = '\0';
|
|
|
|
p = strpbrk(cur, "\r\n");
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur += sz;
|
|
|
|
rem -= sz;
|
|
|
|
if (!rem || !sz)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s : USERID : UNIX : %s\r\n", buf, bogouser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* Welcome to the bloaty horrors */
|
|
|
|
|
2005-04-17 07:24:19 +00:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#define MAXCONNS 20
|
|
|
|
#define MAXIDLETIME 45
|
|
|
|
|
|
|
|
static const char ident_substr[] = " : USERID : UNIX : ";
|
2006-03-10 19:22:06 +00:00
|
|
|
enum { ident_substr_len = sizeof(ident_substr) - 1 };
|
2005-04-17 07:24:19 +00:00
|
|
|
#define PIDFILE "/var/run/identd.pid"
|
|
|
|
|
|
|
|
/*
|
2006-01-25 00:08:53 +00:00
|
|
|
* We have to track the 'first connection socket' so that we
|
2005-04-17 07:24:19 +00:00
|
|
|
* don't go around closing file descriptors for non-clients.
|
|
|
|
*
|
|
|
|
* descriptor setup normally
|
|
|
|
* 0 = server socket
|
|
|
|
* 1 = syslog fd (hopefully -- otherwise this won't work)
|
|
|
|
* 2 = connection socket after detached from tty. standard error before that
|
|
|
|
* 3 - 2 + MAXCONNS = rest connection sockets
|
|
|
|
*
|
|
|
|
* To try to make sure that syslog fd is what is "requested", the that fd
|
|
|
|
* is closed before openlog() call. It can only severely fail if fd 0
|
|
|
|
* is initially closed.
|
|
|
|
*/
|
|
|
|
#define FCS 2
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FD of the connection is always the index of the connection structure
|
|
|
|
* in `conns' array + FCS
|
|
|
|
*/
|
2005-04-18 18:52:15 +00:00
|
|
|
static struct {
|
2005-04-17 07:24:19 +00:00
|
|
|
time_t lasttime;
|
2007-01-11 23:26:13 +00:00
|
|
|
int len;
|
|
|
|
char buf[20];
|
2005-04-17 07:24:19 +00:00
|
|
|
} conns[MAXCONNS];
|
|
|
|
|
|
|
|
/* When using global variables, bind those at least to a structure. */
|
2005-04-18 18:52:15 +00:00
|
|
|
static struct {
|
2005-04-17 07:24:19 +00:00
|
|
|
const char *identuser;
|
|
|
|
fd_set readfds;
|
|
|
|
int conncnt;
|
|
|
|
} G;
|
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
static char *bind_ip_address;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
static int chmatch(char c, char *chars)
|
|
|
|
{
|
|
|
|
for (; *chars; chars++)
|
|
|
|
if (c == *chars)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
static int skipchars(char **p, char *chars)
|
2005-04-17 07:24:19 +00:00
|
|
|
{
|
2007-01-11 23:26:13 +00:00
|
|
|
while (chmatch(**p, chars))
|
|
|
|
(*p)++;
|
|
|
|
if (**p == '\r' || **p == '\n')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2005-04-17 07:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
static int parseAddrs(char *ptr, char **myaddr, char **heraddr)
|
2005-04-17 07:24:19 +00:00
|
|
|
{
|
2007-01-11 23:26:13 +00:00
|
|
|
/* parse <port-on-server> , <port-on-client> */
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
if (!skipchars(&ptr, " \t"))
|
|
|
|
return -1;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
*myaddr = ptr;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
if (!skipchars(&ptr, "1234567890"))
|
|
|
|
return -1;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
if (!chmatch(*ptr, " \t,"))
|
|
|
|
return -1;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
*ptr++ = '\0';
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
if (!skipchars(&ptr, " \t,") )
|
|
|
|
return -1;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
*heraddr = ptr;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
skipchars(&ptr, "1234567890");
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
if (!chmatch(*ptr, " \n\r"))
|
|
|
|
return -1;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
*ptr = '\0';
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
return 0;
|
2005-04-17 07:24:19 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
static void replyError(int s, char *buf)
|
2005-04-17 07:24:19 +00:00
|
|
|
{
|
2007-01-11 23:26:13 +00:00
|
|
|
struct iovec iv[3];
|
|
|
|
iv[0].iov_base = "0, 0 : ERROR : "; iv[0].iov_len = 15;
|
|
|
|
iv[1].iov_base = buf; iv[1].iov_len = strlen(buf);
|
|
|
|
iv[2].iov_base = "\r\n"; iv[2].iov_len = 2;
|
|
|
|
writev(s, iv, 3);
|
|
|
|
}
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
static void reply(int s, char *buf)
|
|
|
|
{
|
|
|
|
char *myaddr, *heraddr;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
myaddr = heraddr = NULL;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
if (parseAddrs(buf, &myaddr, &heraddr))
|
|
|
|
replyError(s, "X-INVALID-REQUEST");
|
|
|
|
else {
|
|
|
|
struct iovec iv[6];
|
|
|
|
iv[0].iov_base = myaddr; iv[0].iov_len = strlen(myaddr);
|
|
|
|
iv[1].iov_base = ", "; iv[1].iov_len = 2;
|
|
|
|
iv[2].iov_base = heraddr; iv[2].iov_len = strlen(heraddr);
|
|
|
|
iv[3].iov_base = (void *)ident_substr; iv[3].iov_len = ident_substr_len;
|
|
|
|
iv[4].iov_base = (void *)G.identuser; iv[4].iov_len = strlen(G.identuser);
|
|
|
|
iv[5].iov_base = "\r\n"; iv[5].iov_len = 2;
|
|
|
|
writev(s, iv, 6);
|
2005-04-17 07:24:19 +00:00
|
|
|
}
|
2007-01-11 23:26:13 +00:00
|
|
|
}
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
static void movefd(int from, int to)
|
|
|
|
{
|
|
|
|
if (from != to) {
|
|
|
|
dup2(from, to);
|
|
|
|
close(from);
|
|
|
|
}
|
2005-04-17 07:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void deleteConn(int s)
|
|
|
|
{
|
|
|
|
int i = s - FCS;
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
|
|
|
|
G.conncnt--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Most of the time there is 0 connections. Most often that there
|
|
|
|
* is connections, there is just one connection. When this one connection
|
|
|
|
* closes, i == G.conncnt = 0 -> no copying.
|
|
|
|
* When there is more than one connection, the oldest connections closes
|
|
|
|
* earlier on average. When this happens, the code below starts copying
|
|
|
|
* the connection structure w/ highest index to the place which which is
|
|
|
|
* just deleted. This means that the connection structures are no longer
|
|
|
|
* in chronological order. I'd quess this means that when there is more
|
|
|
|
* than 1 connection, on average every other connection structure needs
|
|
|
|
* to be copied over the time all these connections are deleted.
|
|
|
|
*/
|
|
|
|
if (i != G.conncnt) {
|
|
|
|
memcpy(&conns[i], &conns[G.conncnt], sizeof(conns[0]));
|
|
|
|
movefd(G.conncnt + FCS, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_CLR(G.conncnt + FCS, &G.readfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int closeOldest(void)
|
|
|
|
{
|
|
|
|
time_t min = conns[0].lasttime;
|
|
|
|
int idx = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < MAXCONNS; i++)
|
|
|
|
if (conns[i].lasttime < min)
|
|
|
|
idx = i;
|
|
|
|
|
|
|
|
replyError(idx + FCS, "X-SERVER-TOO-BUSY");
|
|
|
|
close(idx + FCS);
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int checkInput(char *buf, int len, int l)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = len; i < len + l; ++i)
|
|
|
|
if (buf[i] == '\n')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
/* May succeed. If not, won't care. */
|
|
|
|
static const char *to_unlink;
|
|
|
|
static void writepid(void)
|
|
|
|
{
|
|
|
|
int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664);
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
to_unlink = PIDFILE;
|
|
|
|
fdprintf(fd, "%d\n", getpid());
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handlexitsigs(int signum)
|
|
|
|
{
|
|
|
|
if (to_unlink)
|
|
|
|
if (unlink(to_unlink) < 0)
|
|
|
|
close(open(to_unlink, O_WRONLY|O_CREAT|O_TRUNC, 0644));
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2005-04-17 07:24:19 +00:00
|
|
|
int fakeidentd_main(int argc, char **argv)
|
|
|
|
{
|
2007-01-11 23:26:13 +00:00
|
|
|
int fd;
|
|
|
|
pid_t pid;
|
2006-09-06 18:36:50 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
/* FD_ZERO(&G.readfds); - in bss, already zeroed */
|
2005-04-17 07:24:19 +00:00
|
|
|
FD_SET(0, &G.readfds);
|
|
|
|
|
|
|
|
/* handle -b <ip> parameter */
|
2006-10-03 21:00:06 +00:00
|
|
|
getopt32(argc, argv, "b:", &bind_ip_address);
|
2005-04-17 07:24:19 +00:00
|
|
|
/* handle optional REPLY STRING */
|
|
|
|
if (optind < argc)
|
|
|
|
G.identuser = argv[optind];
|
|
|
|
else
|
2007-01-11 23:26:13 +00:00
|
|
|
G.identuser = "nobody";
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
writepid();
|
|
|
|
signal(SIGTERM, handlexitsigs);
|
|
|
|
signal(SIGINT, handlexitsigs);
|
|
|
|
signal(SIGQUIT, handlexitsigs);
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
signal(SIGPIPE, SIG_IGN); /* ignore closed connections when writing */
|
|
|
|
|
|
|
|
fd = create_and_bind_stream_or_die(bind_ip_address, bb_lookup_port("identd", "tcp", 113));
|
|
|
|
xlisten(fd, 5);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
bb_perror_msg_and_die("fork");
|
|
|
|
if (pid != 0) /* parent */
|
|
|
|
exit(0);
|
|
|
|
/* child */
|
|
|
|
setsid();
|
|
|
|
movefd(fd, 0);
|
|
|
|
while (fd)
|
|
|
|
close(fd--);
|
|
|
|
openlog(applet_name, 0, LOG_DAEMON);
|
|
|
|
logmode = LOGMODE_SYSLOG;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2005-04-18 18:52:15 +00:00
|
|
|
/* main loop where we process all events and never exit */
|
2005-04-17 07:24:19 +00:00
|
|
|
while (1) {
|
2007-01-11 17:20:00 +00:00
|
|
|
fd_set rfds = G.readfds;
|
|
|
|
struct timeval tv = { 15, 0 };
|
|
|
|
int i;
|
|
|
|
int tim = time(NULL);
|
|
|
|
|
|
|
|
select(G.conncnt + FCS, &rfds, NULL, NULL, G.conncnt? &tv: NULL);
|
|
|
|
|
|
|
|
for (i = G.conncnt - 1; i >= 0; i--) {
|
|
|
|
int s = i + FCS;
|
|
|
|
|
|
|
|
if (FD_ISSET(s, &rfds)) {
|
|
|
|
char *buf = conns[i].buf;
|
2007-01-11 23:26:13 +00:00
|
|
|
unsigned len = conns[i].len;
|
|
|
|
unsigned l;
|
2007-01-11 17:20:00 +00:00
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
l = read(s, buf + len, sizeof(conns[0].buf) - len);
|
|
|
|
if (l > 0) {
|
2007-01-11 17:20:00 +00:00
|
|
|
if (checkInput(buf, len, l)) {
|
|
|
|
reply(s, buf);
|
|
|
|
goto deleteconn;
|
|
|
|
} else if (len + l >= sizeof(conns[0].buf)) {
|
|
|
|
replyError(s, "X-INVALID-REQUEST");
|
|
|
|
goto deleteconn;
|
|
|
|
} else {
|
|
|
|
conns[i].len += l;
|
|
|
|
}
|
2005-04-17 07:24:19 +00:00
|
|
|
} else {
|
2007-01-11 17:20:00 +00:00
|
|
|
goto deleteconn;
|
2005-04-17 07:24:19 +00:00
|
|
|
}
|
2007-01-11 17:20:00 +00:00
|
|
|
conns[i].lasttime = tim;
|
|
|
|
continue;
|
2005-04-17 07:24:19 +00:00
|
|
|
deleteconn:
|
|
|
|
deleteConn(s);
|
2007-01-11 17:20:00 +00:00
|
|
|
} else {
|
|
|
|
/* implement as time_after() in linux kernel sources ... */
|
|
|
|
if (conns[i].lasttime + MAXIDLETIME <= tim) {
|
|
|
|
replyError(s, "X-TIMEOUT");
|
|
|
|
deleteConn(s);
|
|
|
|
}
|
2005-04-17 07:24:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-11 17:20:00 +00:00
|
|
|
if (FD_ISSET(0, &rfds)) {
|
|
|
|
int s = accept(0, NULL, 0);
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 17:20:00 +00:00
|
|
|
if (s < 0) {
|
2007-01-11 23:26:13 +00:00
|
|
|
if (errno != EINTR)
|
2007-01-11 17:20:00 +00:00
|
|
|
bb_perror_msg("accept");
|
|
|
|
} else {
|
|
|
|
if (G.conncnt == MAXCONNS)
|
|
|
|
i = closeOldest();
|
|
|
|
else
|
|
|
|
i = G.conncnt++;
|
2005-04-17 07:24:19 +00:00
|
|
|
|
2007-01-11 17:20:00 +00:00
|
|
|
movefd(s, i + FCS); /* move if not already there */
|
|
|
|
FD_SET(i + FCS, &G.readfds);
|
|
|
|
conns[i].len = 0;
|
|
|
|
conns[i].lasttime = time(NULL);
|
|
|
|
}
|
2005-04-17 07:24:19 +00:00
|
|
|
}
|
2006-12-26 10:42:51 +00:00
|
|
|
} /* end of while (1) */
|
2005-04-17 07:24:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-11 23:26:13 +00:00
|
|
|
#endif /* !SANE_INETD_ONLY_VERSION */
|