blob: b097d9980b196b1feaf5f282a4aee27641f8882d [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"
13
Mike Frysingerfa69f112005-04-17 07:24:19 +000014#include <unistd.h>
Mike Frysingerfa69f112005-04-17 07:24:19 +000015#include <string.h>
16#include <fcntl.h>
17#include <signal.h>
18#include <sys/syslog.h>
19
20#include <pwd.h>
Mike Frysingerfa69f112005-04-17 07:24:19 +000021
22#include <sys/syslog.h>
Mike Frysingerfa69f112005-04-17 07:24:19 +000023#include <time.h>
24#include <sys/socket.h>
Mike Frysingerfa69f112005-04-17 07:24:19 +000025#include <errno.h>
Mike Frysingerfa69f112005-04-17 07:24:19 +000026#include <sys/uio.h>
27
Mike Frysingerfa69f112005-04-17 07:24:19 +000028
29#define IDENT_PORT 113
30#define MAXCONNS 20
31#define MAXIDLETIME 45
32
33static const char ident_substr[] = " : USERID : UNIX : ";
Rob Landleybc68cd12006-03-10 19:22:06 +000034enum { ident_substr_len = sizeof(ident_substr) - 1 };
Mike Frysingerfa69f112005-04-17 07:24:19 +000035#define PIDFILE "/var/run/identd.pid"
36
37/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000038 * We have to track the 'first connection socket' so that we
Mike Frysingerfa69f112005-04-17 07:24:19 +000039 * don't go around closing file descriptors for non-clients.
40 *
41 * descriptor setup normally
42 * 0 = server socket
43 * 1 = syslog fd (hopefully -- otherwise this won't work)
44 * 2 = connection socket after detached from tty. standard error before that
45 * 3 - 2 + MAXCONNS = rest connection sockets
46 *
47 * To try to make sure that syslog fd is what is "requested", the that fd
48 * is closed before openlog() call. It can only severely fail if fd 0
49 * is initially closed.
50 */
51#define FCS 2
52
53/*
54 * FD of the connection is always the index of the connection structure
55 * in `conns' array + FCS
56 */
Mike Frysinger91d8f0e2005-04-18 18:52:15 +000057static struct {
Mike Frysingerfa69f112005-04-17 07:24:19 +000058 char buf[20];
59 int len;
60 time_t lasttime;
61} conns[MAXCONNS];
62
63/* When using global variables, bind those at least to a structure. */
Mike Frysinger91d8f0e2005-04-18 18:52:15 +000064static struct {
Mike Frysingerfa69f112005-04-17 07:24:19 +000065 const char *identuser;
66 fd_set readfds;
67 int conncnt;
68} G;
69
70/*
71 * Prototypes
72 */
73static void reply(int s, char *buf);
74static void replyError(int s, char *buf);
75
76static const char *nobodystr = "nobody"; /* this needs to be declared like this */
77static char *bind_ip_address = "0.0.0.0";
78
79static inline void movefd(int from, int to)
80{
81 if (from != to) {
82 dup2(from, to);
83 close(from);
84 }
85}
86
87static void inetbind(void)
88{
89 int s, port;
90 struct sockaddr_in addr;
91 int len = sizeof(addr);
92 int one = 1;
93 struct servent *se;
94
95 if ((se = getservbyname("identd", "tcp")) == NULL)
96 port = IDENT_PORT;
97 else
98 port = se->s_port;
99
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +0000100 s = bb_xsocket(AF_INET, SOCK_STREAM, 0);
Mike Frysingerfa69f112005-04-17 07:24:19 +0000101
102 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
103
104 memset(&addr, 0, sizeof(addr));
105 addr.sin_addr.s_addr = inet_addr(bind_ip_address);
106 addr.sin_family = AF_INET;
107 addr.sin_port = htons(port);
108
Bernhard Reutner-Fischer67f641e2006-04-12 18:24:37 +0000109 if (bind(s, (struct sockaddr *)&addr, len) < 0) /* bb_xbind? */
Mike Frysingerfa69f112005-04-17 07:24:19 +0000110 bb_perror_msg_and_die("Cannot bind() port %i", IDENT_PORT);
111
Bernhard Reutner-Fischer67f641e2006-04-12 18:24:37 +0000112 if (listen(s, 5) < 0) /* bb_xlisten? */
Mike Frysingerfa69f112005-04-17 07:24:19 +0000113 bb_perror_msg_and_die("Cannot listen() on port %i", IDENT_PORT);
114
115 movefd(s, 0);
116}
117
Mike Frysingerfa69f112005-04-17 07:24:19 +0000118static void handlexitsigs(int signum)
119{
Ned Ludd778ee6d2005-06-24 03:47:57 +0000120 if (unlink(PIDFILE) < 0)
121 close(open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644));
Mike Frysingerfa69f112005-04-17 07:24:19 +0000122 exit(0);
123}
124
125/* May succeed. If not, won't care. */
Mike Frysinger7202e002005-04-23 01:42:29 +0000126static inline void writepid(uid_t nobody, uid_t nogrp)
Mike Frysingerfa69f112005-04-17 07:24:19 +0000127{
128 char buf[24];
129 int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664);
130
131 if (fd < 0)
132 return;
133
134 snprintf(buf, 23, "%d\n", getpid());
135 write(fd, buf, strlen(buf));
136 fchown(fd, nobody, nogrp);
137 close(fd);
138
139 /* should this handle ILL, ... (see signal(7)) */
140 signal(SIGTERM, handlexitsigs);
141 signal(SIGINT, handlexitsigs);
142 signal(SIGQUIT, handlexitsigs);
143}
144
145/* return 0 as parent, 1 as child */
146static int godaemon(void)
147{
148 uid_t nobody, nogrp;
149 struct passwd *pw;
150
151 switch (fork()) {
152 case -1:
153 bb_perror_msg_and_die("Could not fork");
154
155 case 0:
156 pw = getpwnam(nobodystr);
157 if (pw == NULL)
158 bb_error_msg_and_die("Cannot find uid/gid of user '%s'", nobodystr);
159 nobody = pw->pw_uid;
160 nogrp = pw->pw_gid;
161 writepid(nobody, nogrp);
162
163 close(0);
164 inetbind();
165 if (setgid(nogrp)) bb_error_msg_and_die("Could not setgid()");
Mike Frysingerfa69f112005-04-17 07:24:19 +0000166 if (setuid(nobody)) bb_error_msg_and_die("Could not setuid()");
Mike Frysingerfa69f112005-04-17 07:24:19 +0000167 close(1);
168 close(2);
169
170 signal(SIGHUP, SIG_IGN);
171 signal(SIGPIPE, SIG_IGN); /* connection closed when writing (raises ???) */
172
173 setsid();
174
175 openlog(bb_applet_name, 0, LOG_DAEMON);
176 return 1;
177 }
178
179 return 0;
180}
181
182static void deleteConn(int s)
183{
184 int i = s - FCS;
185
186 close(s);
187
188 G.conncnt--;
189
190 /*
191 * Most of the time there is 0 connections. Most often that there
192 * is connections, there is just one connection. When this one connection
193 * closes, i == G.conncnt = 0 -> no copying.
194 * When there is more than one connection, the oldest connections closes
195 * earlier on average. When this happens, the code below starts copying
196 * the connection structure w/ highest index to the place which which is
197 * just deleted. This means that the connection structures are no longer
198 * in chronological order. I'd quess this means that when there is more
199 * than 1 connection, on average every other connection structure needs
200 * to be copied over the time all these connections are deleted.
201 */
202 if (i != G.conncnt) {
203 memcpy(&conns[i], &conns[G.conncnt], sizeof(conns[0]));
204 movefd(G.conncnt + FCS, s);
205 }
206
207 FD_CLR(G.conncnt + FCS, &G.readfds);
208}
209
210static int closeOldest(void)
211{
212 time_t min = conns[0].lasttime;
213 int idx = 0;
214 int i;
215
216 for (i = 1; i < MAXCONNS; i++)
217 if (conns[i].lasttime < min)
218 idx = i;
219
220 replyError(idx + FCS, "X-SERVER-TOO-BUSY");
221 close(idx + FCS);
222
223 return idx;
224}
225
226static int checkInput(char *buf, int len, int l)
227{
228 int i;
229 for (i = len; i < len + l; ++i)
230 if (buf[i] == '\n')
231 return 1;
232 return 0;
233}
234
235int fakeidentd_main(int argc, char **argv)
236{
Mike Frysingerfa69f112005-04-17 07:24:19 +0000237 memset(conns, 0, sizeof(conns));
238 memset(&G, 0, sizeof(G));
239 FD_ZERO(&G.readfds);
240 FD_SET(0, &G.readfds);
241
242 /* handle -b <ip> parameter */
Mike Frysinger91d8f0e2005-04-18 18:52:15 +0000243 bb_getopt_ulflags(argc, argv, "b:", &bind_ip_address);
Mike Frysingerfa69f112005-04-17 07:24:19 +0000244 /* handle optional REPLY STRING */
245 if (optind < argc)
246 G.identuser = argv[optind];
247 else
248 G.identuser = nobodystr;
249
250 /* daemonize and have the parent return */
251 if (godaemon() == 0)
252 return 0;
253
Mike Frysinger91d8f0e2005-04-18 18:52:15 +0000254 /* main loop where we process all events and never exit */
Mike Frysingerfa69f112005-04-17 07:24:19 +0000255 while (1) {
256 fd_set rfds = G.readfds;
257 struct timeval tv = { 15, 0 };
258 int i;
259 int tim = time(NULL);
260
261 select(G.conncnt + FCS, &rfds, NULL, NULL, G.conncnt? &tv: NULL);
262
263 for (i = G.conncnt - 1; i >= 0; i--) {
264 int s = i + FCS;
265
266 if (FD_ISSET(s, &rfds)) {
267 char *buf = conns[i].buf;
268 unsigned int len = conns[i].len;
269 unsigned int l;
270
271 if ((l = read(s, buf + len, sizeof(conns[0].buf) - len)) > 0) {
272 if (checkInput(buf, len, l)) {
273 reply(s, buf);
274 goto deleteconn;
275 } else if (len + l >= sizeof(conns[0].buf)) {
276 replyError(s, "X-INVALID-REQUEST");
277 goto deleteconn;
278 } else {
279 conns[i].len += l;
280 }
281 } else {
282 goto deleteconn;
283 }
284
285 conns[i].lasttime = tim;
286 continue;
287
288deleteconn:
289 deleteConn(s);
290 } else {
291 /* implement as time_after() in linux kernel sources ... */
292 if (conns[i].lasttime + MAXIDLETIME <= tim) {
293 replyError(s, "X-TIMEOUT");
294 deleteConn(s);
295 }
296 }
297 }
298
299 if (FD_ISSET(0, &rfds)) {
300 int s = accept(0, NULL, 0);
301
302 if (s < 0) {
303 if (errno != EINTR) /* EINTR */
304 syslog(LOG_ERR, "accept: %s", strerror(errno));
305 } else {
306 if (G.conncnt == MAXCONNS)
307 i = closeOldest();
308 else
309 i = G.conncnt++;
310
311 movefd(s, i + FCS); /* move if not already there */
312 FD_SET(i + FCS, &G.readfds);
313
314 conns[i].len = 0;
315 conns[i].lasttime = time(NULL);
316 }
317 }
318 } /* end of while(1) */
319
320 return 0;
321}
322
323static int parseAddrs(char *ptr, char **myaddr, char **heraddr);
324static void reply(int s, char *buf)
325{
326 char *myaddr, *heraddr;
327
328 myaddr = heraddr = NULL;
329
330 if (parseAddrs(buf, &myaddr, &heraddr))
331 replyError(s, "X-INVALID-REQUEST");
332 else {
333 struct iovec iv[6];
334 iv[0].iov_base = myaddr; iv[0].iov_len = strlen(myaddr);
335 iv[1].iov_base = ", "; iv[1].iov_len = 2;
336 iv[2].iov_base = heraddr; iv[2].iov_len = strlen(heraddr);
337 iv[3].iov_base = (void *)ident_substr; iv[3].iov_len = ident_substr_len;
338 iv[4].iov_base = (void *)G.identuser; iv[4].iov_len = strlen(G.identuser);
339 iv[5].iov_base = "\r\n"; iv[5].iov_len = 2;
340 writev(s, iv, 6);
341 }
342}
343
344static void replyError(int s, char *buf)
345{
346 struct iovec iv[3];
347 iv[0].iov_base = "0, 0 : ERROR : "; iv[0].iov_len = 15;
348 iv[1].iov_base = buf; iv[1].iov_len = strlen(buf);
349 iv[2].iov_base = "\r\n"; iv[2].iov_len = 2;
350 writev(s, iv, 3);
351}
352
353static int chmatch(char c, char *chars)
354{
355 for (; *chars; chars++)
356 if (c == *chars)
357 return 1;
358 return 0;
359}
360
361static int skipchars(char **p, char *chars)
362{
363 while (chmatch(**p, chars))
364 (*p)++;
365 if (**p == '\r' || **p == '\n')
366 return 0;
367 return 1;
368}
369
370static int parseAddrs(char *ptr, char **myaddr, char **heraddr)
371{
372 /* parse <port-on-server> , <port-on-client> */
373
374 if (!skipchars(&ptr, " \t"))
375 return -1;
376
377 *myaddr = ptr;
378
379 if (!skipchars(&ptr, "1234567890"))
380 return -1;
381
382 if (!chmatch(*ptr, " \t,"))
383 return -1;
384
385 *ptr++ = '\0';
386
387 if (!skipchars(&ptr, " \t,") )
388 return -1;
389
390 *heraddr = ptr;
391
392 skipchars(&ptr, "1234567890");
393
394 if (!chmatch(*ptr, " \n\r"))
395 return -1;
396
397 *ptr = '\0';
398
399 return 0;
400}