2005-04-17 12:54:19 +05:30
|
|
|
/* 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 23:25:51 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2005-04-17 12:54:19 +05:30
|
|
|
*/
|
|
|
|
|
2006-06-05 23:05:24 +05:30
|
|
|
#include "busybox.h"
|
2005-04-17 12:54:19 +05:30
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define IDENT_PORT 113
|
|
|
|
#define MAXCONNS 20
|
|
|
|
#define MAXIDLETIME 45
|
|
|
|
|
|
|
|
static const char ident_substr[] = " : USERID : UNIX : ";
|
2006-03-11 00:52:06 +05:30
|
|
|
enum { ident_substr_len = sizeof(ident_substr) - 1 };
|
2005-04-17 12:54:19 +05:30
|
|
|
#define PIDFILE "/var/run/identd.pid"
|
|
|
|
|
|
|
|
/*
|
2006-01-25 05:38:53 +05:30
|
|
|
* We have to track the 'first connection socket' so that we
|
2005-04-17 12:54:19 +05:30
|
|
|
* 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-19 00:22:15 +05:30
|
|
|
static struct {
|
2005-04-17 12:54:19 +05:30
|
|
|
char buf[20];
|
|
|
|
int len;
|
|
|
|
time_t lasttime;
|
|
|
|
} conns[MAXCONNS];
|
|
|
|
|
|
|
|
/* When using global variables, bind those at least to a structure. */
|
2005-04-19 00:22:15 +05:30
|
|
|
static struct {
|
2005-04-17 12:54:19 +05:30
|
|
|
const char *identuser;
|
|
|
|
fd_set readfds;
|
|
|
|
int conncnt;
|
|
|
|
} G;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
|
|
|
static void reply(int s, char *buf);
|
|
|
|
static void replyError(int s, char *buf);
|
|
|
|
|
|
|
|
static const char *nobodystr = "nobody"; /* this needs to be declared like this */
|
|
|
|
static char *bind_ip_address = "0.0.0.0";
|
|
|
|
|
2006-08-30 01:11:06 +05:30
|
|
|
static void movefd(int from, int to)
|
2005-04-17 12:54:19 +05:30
|
|
|
{
|
|
|
|
if (from != to) {
|
|
|
|
dup2(from, to);
|
|
|
|
close(from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inetbind(void)
|
|
|
|
{
|
|
|
|
int s, port;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int len = sizeof(addr);
|
|
|
|
struct servent *se;
|
|
|
|
|
2006-11-23 04:52:06 +05:30
|
|
|
se = getservbyname("identd", "tcp");
|
|
|
|
port = IDENT_PORT;
|
|
|
|
if (se)
|
2005-04-17 12:54:19 +05:30
|
|
|
port = se->s_port;
|
|
|
|
|
2006-08-03 21:11:12 +05:30
|
|
|
s = xsocket(AF_INET, SOCK_STREAM, 0);
|
2005-04-17 12:54:19 +05:30
|
|
|
|
2006-11-23 04:52:06 +05:30
|
|
|
setsockopt_reuseaddr(s);
|
2005-04-17 12:54:19 +05:30
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_addr.s_addr = inet_addr(bind_ip_address);
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
|
2006-08-03 21:11:12 +05:30
|
|
|
xbind(s, (struct sockaddr *)&addr, len);
|
|
|
|
xlisten(s, 5);
|
2005-04-17 12:54:19 +05:30
|
|
|
|
|
|
|
movefd(s, 0);
|
|
|
|
}
|
|
|
|
|
2005-06-24 09:17:57 +05:30
|
|
|
static void handlexitsigs(int signum)
|
2005-04-17 12:54:19 +05:30
|
|
|
{
|
|
|
|
if (unlink(PIDFILE) < 0)
|
|
|
|
close(open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644));
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* May succeed. If not, won't care. */
|
2006-08-30 01:11:06 +05:30
|
|
|
static void writepid(uid_t nobody, uid_t nogrp)
|
2005-04-17 12:54:19 +05:30
|
|
|
{
|
2006-11-22 21:40:39 +05:30
|
|
|
char buf[sizeof(int)*3 + 2];
|
2005-04-17 12:54:19 +05:30
|
|
|
int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
2006-11-22 21:40:39 +05:30
|
|
|
sprintf(buf, "%d\n", getpid());
|
2005-04-17 12:54:19 +05:30
|
|
|
write(fd, buf, strlen(buf));
|
|
|
|
fchown(fd, nobody, nogrp);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* should this handle ILL, ... (see signal(7)) */
|
|
|
|
signal(SIGTERM, handlexitsigs);
|
|
|
|
signal(SIGINT, handlexitsigs);
|
|
|
|
signal(SIGQUIT, handlexitsigs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 0 as parent, 1 as child */
|
|
|
|
static int godaemon(void)
|
|
|
|
{
|
|
|
|
uid_t nobody, nogrp;
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
2006-09-03 17:51:59 +05:30
|
|
|
bb_perror_msg_and_die("fork");
|
2005-04-17 12:54:19 +05:30
|
|
|
|
|
|
|
case 0:
|
|
|
|
pw = getpwnam(nobodystr);
|
|
|
|
if (pw == NULL)
|
2006-10-14 07:53:43 +05:30
|
|
|
bb_error_msg_and_die("cannot find uid/gid of user '%s'", nobodystr);
|
2005-04-17 12:54:19 +05:30
|
|
|
nobody = pw->pw_uid;
|
|
|
|
nogrp = pw->pw_gid;
|
|
|
|
writepid(nobody, nogrp);
|
|
|
|
|
|
|
|
close(0);
|
|
|
|
inetbind();
|
2006-07-16 13:36:34 +05:30
|
|
|
xsetgid(nogrp);
|
|
|
|
xsetuid(nobody);
|
2005-04-17 12:54:19 +05:30
|
|
|
close(1);
|
|
|
|
close(2);
|
|
|
|
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
signal(SIGPIPE, SIG_IGN); /* connection closed when writing (raises ???) */
|
|
|
|
|
|
|
|
setsid();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fakeidentd_main(int argc, char **argv)
|
|
|
|
{
|
2006-09-07 00:06:50 +05:30
|
|
|
/* This applet is an inetd-style daemon */
|
2006-10-04 02:30:43 +05:30
|
|
|
openlog(applet_name, 0, LOG_DAEMON);
|
2006-09-07 00:06:50 +05:30
|
|
|
logmode = LOGMODE_SYSLOG;
|
|
|
|
|
2005-04-17 12:54:19 +05:30
|
|
|
memset(conns, 0, sizeof(conns));
|
|
|
|
memset(&G, 0, sizeof(G));
|
|
|
|
FD_ZERO(&G.readfds);
|
|
|
|
FD_SET(0, &G.readfds);
|
|
|
|
|
|
|
|
/* handle -b <ip> parameter */
|
2006-10-04 02:30:06 +05:30
|
|
|
getopt32(argc, argv, "b:", &bind_ip_address);
|
2005-04-17 12:54:19 +05:30
|
|
|
/* handle optional REPLY STRING */
|
|
|
|
if (optind < argc)
|
|
|
|
G.identuser = argv[optind];
|
|
|
|
else
|
|
|
|
G.identuser = nobodystr;
|
|
|
|
|
|
|
|
/* daemonize and have the parent return */
|
|
|
|
if (godaemon() == 0)
|
|
|
|
return 0;
|
|
|
|
|
2005-04-19 00:22:15 +05:30
|
|
|
/* main loop where we process all events and never exit */
|
2005-04-17 12:54:19 +05:30
|
|
|
while (1) {
|
2007-01-11 22:50:00 +05:30
|
|
|
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;
|
|
|
|
unsigned int len = conns[i].len;
|
|
|
|
unsigned int l;
|
|
|
|
|
|
|
|
if ((l = read(s, buf + len, sizeof(conns[0].buf) - len)) > 0) {
|
|
|
|
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 12:54:19 +05:30
|
|
|
} else {
|
2007-01-11 22:50:00 +05:30
|
|
|
goto deleteconn;
|
2005-04-17 12:54:19 +05:30
|
|
|
}
|
|
|
|
|
2007-01-11 22:50:00 +05:30
|
|
|
conns[i].lasttime = tim;
|
|
|
|
continue;
|
2005-04-17 12:54:19 +05:30
|
|
|
|
|
|
|
deleteconn:
|
|
|
|
deleteConn(s);
|
2007-01-11 22:50:00 +05:30
|
|
|
} else {
|
|
|
|
/* implement as time_after() in linux kernel sources ... */
|
|
|
|
if (conns[i].lasttime + MAXIDLETIME <= tim) {
|
|
|
|
replyError(s, "X-TIMEOUT");
|
|
|
|
deleteConn(s);
|
|
|
|
}
|
2005-04-17 12:54:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-11 22:50:00 +05:30
|
|
|
if (FD_ISSET(0, &rfds)) {
|
|
|
|
int s = accept(0, NULL, 0);
|
2005-04-17 12:54:19 +05:30
|
|
|
|
2007-01-11 22:50:00 +05:30
|
|
|
if (s < 0) {
|
|
|
|
if (errno != EINTR) /* EINTR */
|
|
|
|
bb_perror_msg("accept");
|
|
|
|
} else {
|
|
|
|
if (G.conncnt == MAXCONNS)
|
|
|
|
i = closeOldest();
|
|
|
|
else
|
|
|
|
i = G.conncnt++;
|
2005-04-17 12:54:19 +05:30
|
|
|
|
2007-01-11 22:50:00 +05:30
|
|
|
movefd(s, i + FCS); /* move if not already there */
|
|
|
|
FD_SET(i + FCS, &G.readfds);
|
2005-04-17 12:54:19 +05:30
|
|
|
|
2007-01-11 22:50:00 +05:30
|
|
|
conns[i].len = 0;
|
|
|
|
conns[i].lasttime = time(NULL);
|
|
|
|
}
|
2005-04-17 12:54:19 +05:30
|
|
|
}
|
2006-12-26 16:12:51 +05:30
|
|
|
} /* end of while (1) */
|
2005-04-17 12:54:19 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parseAddrs(char *ptr, char **myaddr, char **heraddr);
|
|
|
|
static void reply(int s, char *buf)
|
|
|
|
{
|
|
|
|
char *myaddr, *heraddr;
|
|
|
|
|
|
|
|
myaddr = heraddr = NULL;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void replyError(int s, char *buf)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chmatch(char c, char *chars)
|
|
|
|
{
|
|
|
|
for (; *chars; chars++)
|
|
|
|
if (c == *chars)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int skipchars(char **p, char *chars)
|
|
|
|
{
|
|
|
|
while (chmatch(**p, chars))
|
|
|
|
(*p)++;
|
|
|
|
if (**p == '\r' || **p == '\n')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parseAddrs(char *ptr, char **myaddr, char **heraddr)
|
|
|
|
{
|
|
|
|
/* parse <port-on-server> , <port-on-client> */
|
|
|
|
|
|
|
|
if (!skipchars(&ptr, " \t"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*myaddr = ptr;
|
|
|
|
|
|
|
|
if (!skipchars(&ptr, "1234567890"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!chmatch(*ptr, " \t,"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*ptr++ = '\0';
|
|
|
|
|
|
|
|
if (!skipchars(&ptr, " \t,") )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*heraddr = ptr;
|
|
|
|
|
|
|
|
skipchars(&ptr, "1234567890");
|
|
|
|
|
|
|
|
if (!chmatch(*ptr, " \n\r"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|