blob: 6ee7c328ecb0cbe3a9a1472557e289fed540f8e0 [file] [log] [blame]
Mike Frysingerfa69f112005-04-17 07:24:19 +00001/* vi: set sw=4 ts=4: */
2/*
3 * A fake identd server
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/
8 *
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Mike Frysingerfa69f112005-04-17 07:24:19 +000010 */
11
Rob Landley362dc2b2006-06-05 17:35:24 +000012#include "busybox.h"
Mike Frysingerfa69f112005-04-17 07:24:19 +000013#include <sys/syslog.h>
Mike Frysingerfa69f112005-04-17 07:24:19 +000014#include <sys/uio.h>
15
Mike Frysingerfa69f112005-04-17 07:24:19 +000016
17#define IDENT_PORT 113
18#define MAXCONNS 20
19#define MAXIDLETIME 45
20
21static const char ident_substr[] = " : USERID : UNIX : ";
Rob Landleybc68cd12006-03-10 19:22:06 +000022enum { ident_substr_len = sizeof(ident_substr) - 1 };
Mike Frysingerfa69f112005-04-17 07:24:19 +000023#define PIDFILE "/var/run/identd.pid"
24
25/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026 * We have to track the 'first connection socket' so that we
Mike Frysingerfa69f112005-04-17 07:24:19 +000027 * don't go around closing file descriptors for non-clients.
28 *
29 * descriptor setup normally
30 * 0 = server socket
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
34 *
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.
38 */
39#define FCS 2
40
41/*
42 * FD of the connection is always the index of the connection structure
43 * in `conns' array + FCS
44 */
Mike Frysinger91d8f0e2005-04-18 18:52:15 +000045static struct {
Mike Frysingerfa69f112005-04-17 07:24:19 +000046 char buf[20];
47 int len;
48 time_t lasttime;
49} conns[MAXCONNS];
50
51/* When using global variables, bind those at least to a structure. */
Mike Frysinger91d8f0e2005-04-18 18:52:15 +000052static struct {
Mike Frysingerfa69f112005-04-17 07:24:19 +000053 const char *identuser;
54 fd_set readfds;
55 int conncnt;
56} G;
57
58/*
59 * Prototypes
60 */
61static void reply(int s, char *buf);
62static void replyError(int s, char *buf);
63
64static const char *nobodystr = "nobody"; /* this needs to be declared like this */
65static char *bind_ip_address = "0.0.0.0";
66
67static inline void movefd(int from, int to)
68{
69 if (from != to) {
70 dup2(from, to);
71 close(from);
72 }
73}
74
75static void inetbind(void)
76{
77 int s, port;
78 struct sockaddr_in addr;
79 int len = sizeof(addr);
80 int one = 1;
81 struct servent *se;
82
83 if ((se = getservbyname("identd", "tcp")) == NULL)
84 port = IDENT_PORT;
85 else
86 port = se->s_port;
87
Rob Landleyd921b2e2006-08-03 15:41:12 +000088 s = xsocket(AF_INET, SOCK_STREAM, 0);
Mike Frysingerfa69f112005-04-17 07:24:19 +000089
90 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
91
92 memset(&addr, 0, sizeof(addr));
93 addr.sin_addr.s_addr = inet_addr(bind_ip_address);
94 addr.sin_family = AF_INET;
95 addr.sin_port = htons(port);
96
Rob Landleyd921b2e2006-08-03 15:41:12 +000097 xbind(s, (struct sockaddr *)&addr, len);
98 xlisten(s, 5);
Mike Frysingerfa69f112005-04-17 07:24:19 +000099
100 movefd(s, 0);
101}
102
Mike Frysingerfa69f112005-04-17 07:24:19 +0000103static void handlexitsigs(int signum)
104{
Ned Ludd778ee6d2005-06-24 03:47:57 +0000105 if (unlink(PIDFILE) < 0)
106 close(open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644));
Mike Frysingerfa69f112005-04-17 07:24:19 +0000107 exit(0);
108}
109
110/* May succeed. If not, won't care. */
Mike Frysinger7202e002005-04-23 01:42:29 +0000111static inline void writepid(uid_t nobody, uid_t nogrp)
Mike Frysingerfa69f112005-04-17 07:24:19 +0000112{
113 char buf[24];
114 int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664);
115
116 if (fd < 0)
117 return;
118
119 snprintf(buf, 23, "%d\n", getpid());
120 write(fd, buf, strlen(buf));
121 fchown(fd, nobody, nogrp);
122 close(fd);
123
124 /* should this handle ILL, ... (see signal(7)) */
125 signal(SIGTERM, handlexitsigs);
126 signal(SIGINT, handlexitsigs);
127 signal(SIGQUIT, handlexitsigs);
128}
129
130/* return 0 as parent, 1 as child */
131static int godaemon(void)
132{
133 uid_t nobody, nogrp;
134 struct passwd *pw;
135
136 switch (fork()) {
137 case -1:
138 bb_perror_msg_and_die("Could not fork");
139
140 case 0:
141 pw = getpwnam(nobodystr);
142 if (pw == NULL)
143 bb_error_msg_and_die("Cannot find uid/gid of user '%s'", nobodystr);
144 nobody = pw->pw_uid;
145 nogrp = pw->pw_gid;
146 writepid(nobody, nogrp);
147
148 close(0);
149 inetbind();
Rob Landleyafb94ec2006-07-16 08:06:34 +0000150 xsetgid(nogrp);
151 xsetuid(nobody);
Mike Frysingerfa69f112005-04-17 07:24:19 +0000152 close(1);
153 close(2);
154
155 signal(SIGHUP, SIG_IGN);
156 signal(SIGPIPE, SIG_IGN); /* connection closed when writing (raises ???) */
157
158 setsid();
159
160 openlog(bb_applet_name, 0, LOG_DAEMON);
161 return 1;
162 }
163
164 return 0;
165}
166
167static void deleteConn(int s)
168{
169 int i = s - FCS;
170
171 close(s);
172
173 G.conncnt--;
174
175 /*
176 * Most of the time there is 0 connections. Most often that there
177 * is connections, there is just one connection. When this one connection
178 * closes, i == G.conncnt = 0 -> no copying.
179 * When there is more than one connection, the oldest connections closes
180 * earlier on average. When this happens, the code below starts copying
181 * the connection structure w/ highest index to the place which which is
182 * just deleted. This means that the connection structures are no longer
183 * in chronological order. I'd quess this means that when there is more
184 * than 1 connection, on average every other connection structure needs
185 * to be copied over the time all these connections are deleted.
186 */
187 if (i != G.conncnt) {
188 memcpy(&conns[i], &conns[G.conncnt], sizeof(conns[0]));
189 movefd(G.conncnt + FCS, s);
190 }
191
192 FD_CLR(G.conncnt + FCS, &G.readfds);
193}
194
195static int closeOldest(void)
196{
197 time_t min = conns[0].lasttime;
198 int idx = 0;
199 int i;
200
201 for (i = 1; i < MAXCONNS; i++)
202 if (conns[i].lasttime < min)
203 idx = i;
204
205 replyError(idx + FCS, "X-SERVER-TOO-BUSY");
206 close(idx + FCS);
207
208 return idx;
209}
210
211static int checkInput(char *buf, int len, int l)
212{
213 int i;
214 for (i = len; i < len + l; ++i)
215 if (buf[i] == '\n')
216 return 1;
217 return 0;
218}
219
220int fakeidentd_main(int argc, char **argv)
221{
Mike Frysingerfa69f112005-04-17 07:24:19 +0000222 memset(conns, 0, sizeof(conns));
223 memset(&G, 0, sizeof(G));
224 FD_ZERO(&G.readfds);
225 FD_SET(0, &G.readfds);
226
227 /* handle -b <ip> parameter */
Mike Frysinger91d8f0e2005-04-18 18:52:15 +0000228 bb_getopt_ulflags(argc, argv, "b:", &bind_ip_address);
Mike Frysingerfa69f112005-04-17 07:24:19 +0000229 /* handle optional REPLY STRING */
230 if (optind < argc)
231 G.identuser = argv[optind];
232 else
233 G.identuser = nobodystr;
234
235 /* daemonize and have the parent return */
236 if (godaemon() == 0)
237 return 0;
238
Mike Frysinger91d8f0e2005-04-18 18:52:15 +0000239 /* main loop where we process all events and never exit */
Mike Frysingerfa69f112005-04-17 07:24:19 +0000240 while (1) {
241 fd_set rfds = G.readfds;
242 struct timeval tv = { 15, 0 };
243 int i;
244 int tim = time(NULL);
245
246 select(G.conncnt + FCS, &rfds, NULL, NULL, G.conncnt? &tv: NULL);
247
248 for (i = G.conncnt - 1; i >= 0; i--) {
249 int s = i + FCS;
250
251 if (FD_ISSET(s, &rfds)) {
252 char *buf = conns[i].buf;
253 unsigned int len = conns[i].len;
254 unsigned int l;
255
256 if ((l = read(s, buf + len, sizeof(conns[0].buf) - len)) > 0) {
257 if (checkInput(buf, len, l)) {
258 reply(s, buf);
259 goto deleteconn;
260 } else if (len + l >= sizeof(conns[0].buf)) {
261 replyError(s, "X-INVALID-REQUEST");
262 goto deleteconn;
263 } else {
264 conns[i].len += l;
265 }
266 } else {
267 goto deleteconn;
268 }
269
270 conns[i].lasttime = tim;
271 continue;
272
273deleteconn:
274 deleteConn(s);
275 } else {
276 /* implement as time_after() in linux kernel sources ... */
277 if (conns[i].lasttime + MAXIDLETIME <= tim) {
278 replyError(s, "X-TIMEOUT");
279 deleteConn(s);
280 }
281 }
282 }
283
284 if (FD_ISSET(0, &rfds)) {
285 int s = accept(0, NULL, 0);
286
287 if (s < 0) {
288 if (errno != EINTR) /* EINTR */
289 syslog(LOG_ERR, "accept: %s", strerror(errno));
290 } else {
291 if (G.conncnt == MAXCONNS)
292 i = closeOldest();
293 else
294 i = G.conncnt++;
295
296 movefd(s, i + FCS); /* move if not already there */
297 FD_SET(i + FCS, &G.readfds);
298
299 conns[i].len = 0;
300 conns[i].lasttime = time(NULL);
301 }
302 }
303 } /* end of while(1) */
304
305 return 0;
306}
307
308static int parseAddrs(char *ptr, char **myaddr, char **heraddr);
309static void reply(int s, char *buf)
310{
311 char *myaddr, *heraddr;
312
313 myaddr = heraddr = NULL;
314
315 if (parseAddrs(buf, &myaddr, &heraddr))
316 replyError(s, "X-INVALID-REQUEST");
317 else {
318 struct iovec iv[6];
319 iv[0].iov_base = myaddr; iv[0].iov_len = strlen(myaddr);
320 iv[1].iov_base = ", "; iv[1].iov_len = 2;
321 iv[2].iov_base = heraddr; iv[2].iov_len = strlen(heraddr);
322 iv[3].iov_base = (void *)ident_substr; iv[3].iov_len = ident_substr_len;
323 iv[4].iov_base = (void *)G.identuser; iv[4].iov_len = strlen(G.identuser);
324 iv[5].iov_base = "\r\n"; iv[5].iov_len = 2;
325 writev(s, iv, 6);
326 }
327}
328
329static void replyError(int s, char *buf)
330{
331 struct iovec iv[3];
332 iv[0].iov_base = "0, 0 : ERROR : "; iv[0].iov_len = 15;
333 iv[1].iov_base = buf; iv[1].iov_len = strlen(buf);
334 iv[2].iov_base = "\r\n"; iv[2].iov_len = 2;
335 writev(s, iv, 3);
336}
337
338static int chmatch(char c, char *chars)
339{
340 for (; *chars; chars++)
341 if (c == *chars)
342 return 1;
343 return 0;
344}
345
346static int skipchars(char **p, char *chars)
347{
348 while (chmatch(**p, chars))
349 (*p)++;
350 if (**p == '\r' || **p == '\n')
351 return 0;
352 return 1;
353}
354
355static int parseAddrs(char *ptr, char **myaddr, char **heraddr)
356{
357 /* parse <port-on-server> , <port-on-client> */
358
359 if (!skipchars(&ptr, " \t"))
360 return -1;
361
362 *myaddr = ptr;
363
364 if (!skipchars(&ptr, "1234567890"))
365 return -1;
366
367 if (!chmatch(*ptr, " \t,"))
368 return -1;
369
370 *ptr++ = '\0';
371
372 if (!skipchars(&ptr, " \t,") )
373 return -1;
374
375 *heraddr = ptr;
376
377 skipchars(&ptr, "1234567890");
378
379 if (!chmatch(*ptr, " \n\r"))
380 return -1;
381
382 *ptr = '\0';
383
384 return 0;
385}