1 /* vi: set sw=4 ts=4: */
5 * Adapted to busybox by Thomas Lundquist <thomasez@zelow.no>
6 * Original Author: Tomi Ollila <too@iki.fi>
7 * http://www.guru-group.fi/~too/sw/
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 #include <sys/syslog.h>
17 #define IDENT_PORT 113
19 #define MAXIDLETIME 45
21 static const char ident_substr[] = " : USERID : UNIX : ";
22 enum { ident_substr_len = sizeof(ident_substr) - 1 };
23 #define PIDFILE "/var/run/identd.pid"
26 * We have to track the 'first connection socket' so that we
27 * don't go around closing file descriptors for non-clients.
29 * descriptor setup normally
31 * 1 = syslog fd (hopefully -- otherwise this won't work)
32 * 2 = connection socket after detached from tty. standard error before that
33 * 3 - 2 + MAXCONNS = rest connection sockets
35 * To try to make sure that syslog fd is what is "requested", the that fd
36 * is closed before openlog() call. It can only severely fail if fd 0
37 * is initially closed.
42 * FD of the connection is always the index of the connection structure
43 * in `conns' array + FCS
51 /* When using global variables, bind those at least to a structure. */
53 const char *identuser;
61 static void reply(int s, char *buf);
62 static void replyError(int s, char *buf);
64 static const char *nobodystr = "nobody"; /* this needs to be declared like this */
65 static char *bind_ip_address = "0.0.0.0";
67 static void movefd(int from, int to)
75 static void inetbind(void)
78 struct sockaddr_in addr;
79 int len = sizeof(addr);
82 se = getservbyname("identd", "tcp");
87 s = xsocket(AF_INET, SOCK_STREAM, 0);
89 setsockopt_reuseaddr(s);
91 memset(&addr, 0, sizeof(addr));
92 addr.sin_addr.s_addr = inet_addr(bind_ip_address);
93 addr.sin_family = AF_INET;
94 addr.sin_port = htons(port);
96 xbind(s, (struct sockaddr *)&addr, len);
102 static void handlexitsigs(int signum)
104 if (unlink(PIDFILE) < 0)
105 close(open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644));
109 /* May succeed. If not, won't care. */
110 static void writepid(uid_t nobody, uid_t nogrp)
112 char buf[sizeof(int)*3 + 2];
113 int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664);
118 sprintf(buf, "%d\n", getpid());
119 write(fd, buf, strlen(buf));
120 fchown(fd, nobody, nogrp);
123 /* should this handle ILL, ... (see signal(7)) */
124 signal(SIGTERM, handlexitsigs);
125 signal(SIGINT, handlexitsigs);
126 signal(SIGQUIT, handlexitsigs);
129 /* return 0 as parent, 1 as child */
130 static int godaemon(void)
137 bb_perror_msg_and_die("fork");
140 pw = getpwnam(nobodystr);
142 bb_error_msg_and_die("cannot find uid/gid of user '%s'", nobodystr);
145 writepid(nobody, nogrp);
154 signal(SIGHUP, SIG_IGN);
155 signal(SIGPIPE, SIG_IGN); /* connection closed when writing (raises ???) */
165 static void deleteConn(int s)
174 * Most of the time there is 0 connections. Most often that there
175 * is connections, there is just one connection. When this one connection
176 * closes, i == G.conncnt = 0 -> no copying.
177 * When there is more than one connection, the oldest connections closes
178 * earlier on average. When this happens, the code below starts copying
179 * the connection structure w/ highest index to the place which which is
180 * just deleted. This means that the connection structures are no longer
181 * in chronological order. I'd quess this means that when there is more
182 * than 1 connection, on average every other connection structure needs
183 * to be copied over the time all these connections are deleted.
185 if (i != G.conncnt) {
186 memcpy(&conns[i], &conns[G.conncnt], sizeof(conns[0]));
187 movefd(G.conncnt + FCS, s);
190 FD_CLR(G.conncnt + FCS, &G.readfds);
193 static int closeOldest(void)
195 time_t min = conns[0].lasttime;
199 for (i = 1; i < MAXCONNS; i++)
200 if (conns[i].lasttime < min)
203 replyError(idx + FCS, "X-SERVER-TOO-BUSY");
209 static int checkInput(char *buf, int len, int l)
212 for (i = len; i < len + l; ++i)
218 int fakeidentd_main(int argc, char **argv)
220 /* This applet is an inetd-style daemon */
221 openlog(applet_name, 0, LOG_DAEMON);
222 logmode = LOGMODE_SYSLOG;
224 memset(conns, 0, sizeof(conns));
225 memset(&G, 0, sizeof(G));
227 FD_SET(0, &G.readfds);
229 /* handle -b <ip> parameter */
230 getopt32(argc, argv, "b:", &bind_ip_address);
231 /* handle optional REPLY STRING */
233 G.identuser = argv[optind];
235 G.identuser = nobodystr;
237 /* daemonize and have the parent return */
241 /* main loop where we process all events and never exit */
243 fd_set rfds = G.readfds;
244 struct timeval tv = { 15, 0 };
246 int tim = time(NULL);
248 select(G.conncnt + FCS, &rfds, NULL, NULL, G.conncnt? &tv: NULL);
250 for (i = G.conncnt - 1; i >= 0; i--) {
253 if (FD_ISSET(s, &rfds)) {
254 char *buf = conns[i].buf;
255 unsigned int len = conns[i].len;
258 if ((l = read(s, buf + len, sizeof(conns[0].buf) - len)) > 0) {
259 if (checkInput(buf, len, l)) {
262 } else if (len + l >= sizeof(conns[0].buf)) {
263 replyError(s, "X-INVALID-REQUEST");
272 conns[i].lasttime = tim;
278 /* implement as time_after() in linux kernel sources ... */
279 if (conns[i].lasttime + MAXIDLETIME <= tim) {
280 replyError(s, "X-TIMEOUT");
286 if (FD_ISSET(0, &rfds)) {
287 int s = accept(0, NULL, 0);
290 if (errno != EINTR) /* EINTR */
291 bb_perror_msg("accept");
293 if (G.conncnt == MAXCONNS)
298 movefd(s, i + FCS); /* move if not already there */
299 FD_SET(i + FCS, &G.readfds);
302 conns[i].lasttime = time(NULL);
305 } /* end of while(1) */
310 static int parseAddrs(char *ptr, char **myaddr, char **heraddr);
311 static void reply(int s, char *buf)
313 char *myaddr, *heraddr;
315 myaddr = heraddr = NULL;
317 if (parseAddrs(buf, &myaddr, &heraddr))
318 replyError(s, "X-INVALID-REQUEST");
321 iv[0].iov_base = myaddr; iv[0].iov_len = strlen(myaddr);
322 iv[1].iov_base = ", "; iv[1].iov_len = 2;
323 iv[2].iov_base = heraddr; iv[2].iov_len = strlen(heraddr);
324 iv[3].iov_base = (void *)ident_substr; iv[3].iov_len = ident_substr_len;
325 iv[4].iov_base = (void *)G.identuser; iv[4].iov_len = strlen(G.identuser);
326 iv[5].iov_base = "\r\n"; iv[5].iov_len = 2;
331 static void replyError(int s, char *buf)
334 iv[0].iov_base = "0, 0 : ERROR : "; iv[0].iov_len = 15;
335 iv[1].iov_base = buf; iv[1].iov_len = strlen(buf);
336 iv[2].iov_base = "\r\n"; iv[2].iov_len = 2;
340 static int chmatch(char c, char *chars)
342 for (; *chars; chars++)
348 static int skipchars(char **p, char *chars)
350 while (chmatch(**p, chars))
352 if (**p == '\r' || **p == '\n')
357 static int parseAddrs(char *ptr, char **myaddr, char **heraddr)
359 /* parse <port-on-server> , <port-on-client> */
361 if (!skipchars(&ptr, " \t"))
366 if (!skipchars(&ptr, "1234567890"))
369 if (!chmatch(*ptr, " \t,"))
374 if (!skipchars(&ptr, " \t,") )
379 skipchars(&ptr, "1234567890");
381 if (!chmatch(*ptr, " \n\r"))