Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 1 | /* 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-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Rob Landley | 362dc2b | 2006-06-05 17:35:24 +0000 | [diff] [blame] | 12 | #include "busybox.h" |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 13 | #include <sys/syslog.h> |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 14 | #include <sys/uio.h> |
| 15 | |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 16 | |
| 17 | #define IDENT_PORT 113 |
| 18 | #define MAXCONNS 20 |
| 19 | #define MAXIDLETIME 45 |
| 20 | |
| 21 | static const char ident_substr[] = " : USERID : UNIX : "; |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 22 | enum { ident_substr_len = sizeof(ident_substr) - 1 }; |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 23 | #define PIDFILE "/var/run/identd.pid" |
| 24 | |
| 25 | /* |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 26 | * We have to track the 'first connection socket' so that we |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 27 | * 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 Frysinger | 91d8f0e | 2005-04-18 18:52:15 +0000 | [diff] [blame] | 45 | static struct { |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 46 | 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 Frysinger | 91d8f0e | 2005-04-18 18:52:15 +0000 | [diff] [blame] | 52 | static struct { |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 53 | const char *identuser; |
| 54 | fd_set readfds; |
| 55 | int conncnt; |
| 56 | } G; |
| 57 | |
| 58 | /* |
| 59 | * Prototypes |
| 60 | */ |
| 61 | static void reply(int s, char *buf); |
| 62 | static void replyError(int s, char *buf); |
| 63 | |
| 64 | static const char *nobodystr = "nobody"; /* this needs to be declared like this */ |
| 65 | static char *bind_ip_address = "0.0.0.0"; |
| 66 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 67 | static void movefd(int from, int to) |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 68 | { |
| 69 | if (from != to) { |
| 70 | dup2(from, to); |
| 71 | close(from); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static 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 Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 88 | s = xsocket(AF_INET, SOCK_STREAM, 0); |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 89 | |
| 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 Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 97 | xbind(s, (struct sockaddr *)&addr, len); |
| 98 | xlisten(s, 5); |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 99 | |
| 100 | movefd(s, 0); |
| 101 | } |
| 102 | |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 103 | static void handlexitsigs(int signum) |
| 104 | { |
Ned Ludd | 778ee6d | 2005-06-24 03:47:57 +0000 | [diff] [blame] | 105 | if (unlink(PIDFILE) < 0) |
| 106 | close(open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644)); |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 107 | exit(0); |
| 108 | } |
| 109 | |
| 110 | /* May succeed. If not, won't care. */ |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 111 | static void writepid(uid_t nobody, uid_t nogrp) |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 112 | { |
| 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 */ |
| 131 | static int godaemon(void) |
| 132 | { |
| 133 | uid_t nobody, nogrp; |
| 134 | struct passwd *pw; |
| 135 | |
| 136 | switch (fork()) { |
| 137 | case -1: |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 138 | bb_perror_msg_and_die("fork"); |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 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 Landley | afb94ec | 2006-07-16 08:06:34 +0000 | [diff] [blame] | 150 | xsetgid(nogrp); |
| 151 | xsetuid(nobody); |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 152 | 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 | |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void deleteConn(int s) |
| 167 | { |
| 168 | int i = s - FCS; |
| 169 | |
| 170 | close(s); |
| 171 | |
| 172 | G.conncnt--; |
| 173 | |
| 174 | /* |
| 175 | * Most of the time there is 0 connections. Most often that there |
| 176 | * is connections, there is just one connection. When this one connection |
| 177 | * closes, i == G.conncnt = 0 -> no copying. |
| 178 | * When there is more than one connection, the oldest connections closes |
| 179 | * earlier on average. When this happens, the code below starts copying |
| 180 | * the connection structure w/ highest index to the place which which is |
| 181 | * just deleted. This means that the connection structures are no longer |
| 182 | * in chronological order. I'd quess this means that when there is more |
| 183 | * than 1 connection, on average every other connection structure needs |
| 184 | * to be copied over the time all these connections are deleted. |
| 185 | */ |
| 186 | if (i != G.conncnt) { |
| 187 | memcpy(&conns[i], &conns[G.conncnt], sizeof(conns[0])); |
| 188 | movefd(G.conncnt + FCS, s); |
| 189 | } |
| 190 | |
| 191 | FD_CLR(G.conncnt + FCS, &G.readfds); |
| 192 | } |
| 193 | |
| 194 | static int closeOldest(void) |
| 195 | { |
| 196 | time_t min = conns[0].lasttime; |
| 197 | int idx = 0; |
| 198 | int i; |
| 199 | |
| 200 | for (i = 1; i < MAXCONNS; i++) |
| 201 | if (conns[i].lasttime < min) |
| 202 | idx = i; |
| 203 | |
| 204 | replyError(idx + FCS, "X-SERVER-TOO-BUSY"); |
| 205 | close(idx + FCS); |
| 206 | |
| 207 | return idx; |
| 208 | } |
| 209 | |
| 210 | static int checkInput(char *buf, int len, int l) |
| 211 | { |
| 212 | int i; |
| 213 | for (i = len; i < len + l; ++i) |
| 214 | if (buf[i] == '\n') |
| 215 | return 1; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | int fakeidentd_main(int argc, char **argv) |
| 220 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 221 | /* This applet is an inetd-style daemon */ |
| 222 | openlog(bb_applet_name, 0, LOG_DAEMON); |
| 223 | logmode = LOGMODE_SYSLOG; |
| 224 | |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 225 | memset(conns, 0, sizeof(conns)); |
| 226 | memset(&G, 0, sizeof(G)); |
| 227 | FD_ZERO(&G.readfds); |
| 228 | FD_SET(0, &G.readfds); |
| 229 | |
| 230 | /* handle -b <ip> parameter */ |
Mike Frysinger | 91d8f0e | 2005-04-18 18:52:15 +0000 | [diff] [blame] | 231 | bb_getopt_ulflags(argc, argv, "b:", &bind_ip_address); |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 232 | /* handle optional REPLY STRING */ |
| 233 | if (optind < argc) |
| 234 | G.identuser = argv[optind]; |
| 235 | else |
| 236 | G.identuser = nobodystr; |
| 237 | |
| 238 | /* daemonize and have the parent return */ |
| 239 | if (godaemon() == 0) |
| 240 | return 0; |
| 241 | |
Mike Frysinger | 91d8f0e | 2005-04-18 18:52:15 +0000 | [diff] [blame] | 242 | /* main loop where we process all events and never exit */ |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 243 | while (1) { |
| 244 | fd_set rfds = G.readfds; |
| 245 | struct timeval tv = { 15, 0 }; |
| 246 | int i; |
| 247 | int tim = time(NULL); |
| 248 | |
| 249 | select(G.conncnt + FCS, &rfds, NULL, NULL, G.conncnt? &tv: NULL); |
| 250 | |
| 251 | for (i = G.conncnt - 1; i >= 0; i--) { |
| 252 | int s = i + FCS; |
| 253 | |
| 254 | if (FD_ISSET(s, &rfds)) { |
| 255 | char *buf = conns[i].buf; |
| 256 | unsigned int len = conns[i].len; |
| 257 | unsigned int l; |
| 258 | |
| 259 | if ((l = read(s, buf + len, sizeof(conns[0].buf) - len)) > 0) { |
| 260 | if (checkInput(buf, len, l)) { |
| 261 | reply(s, buf); |
| 262 | goto deleteconn; |
| 263 | } else if (len + l >= sizeof(conns[0].buf)) { |
| 264 | replyError(s, "X-INVALID-REQUEST"); |
| 265 | goto deleteconn; |
| 266 | } else { |
| 267 | conns[i].len += l; |
| 268 | } |
| 269 | } else { |
| 270 | goto deleteconn; |
| 271 | } |
| 272 | |
| 273 | conns[i].lasttime = tim; |
| 274 | continue; |
| 275 | |
| 276 | deleteconn: |
| 277 | deleteConn(s); |
| 278 | } else { |
| 279 | /* implement as time_after() in linux kernel sources ... */ |
| 280 | if (conns[i].lasttime + MAXIDLETIME <= tim) { |
| 281 | replyError(s, "X-TIMEOUT"); |
| 282 | deleteConn(s); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if (FD_ISSET(0, &rfds)) { |
| 288 | int s = accept(0, NULL, 0); |
| 289 | |
| 290 | if (s < 0) { |
| 291 | if (errno != EINTR) /* EINTR */ |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 292 | bb_perror_msg("accept"); |
Mike Frysinger | fa69f11 | 2005-04-17 07:24:19 +0000 | [diff] [blame] | 293 | } else { |
| 294 | if (G.conncnt == MAXCONNS) |
| 295 | i = closeOldest(); |
| 296 | else |
| 297 | i = G.conncnt++; |
| 298 | |
| 299 | movefd(s, i + FCS); /* move if not already there */ |
| 300 | FD_SET(i + FCS, &G.readfds); |
| 301 | |
| 302 | conns[i].len = 0; |
| 303 | conns[i].lasttime = time(NULL); |
| 304 | } |
| 305 | } |
| 306 | } /* end of while(1) */ |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int parseAddrs(char *ptr, char **myaddr, char **heraddr); |
| 312 | static void reply(int s, char *buf) |
| 313 | { |
| 314 | char *myaddr, *heraddr; |
| 315 | |
| 316 | myaddr = heraddr = NULL; |
| 317 | |
| 318 | if (parseAddrs(buf, &myaddr, &heraddr)) |
| 319 | replyError(s, "X-INVALID-REQUEST"); |
| 320 | else { |
| 321 | struct iovec iv[6]; |
| 322 | iv[0].iov_base = myaddr; iv[0].iov_len = strlen(myaddr); |
| 323 | iv[1].iov_base = ", "; iv[1].iov_len = 2; |
| 324 | iv[2].iov_base = heraddr; iv[2].iov_len = strlen(heraddr); |
| 325 | iv[3].iov_base = (void *)ident_substr; iv[3].iov_len = ident_substr_len; |
| 326 | iv[4].iov_base = (void *)G.identuser; iv[4].iov_len = strlen(G.identuser); |
| 327 | iv[5].iov_base = "\r\n"; iv[5].iov_len = 2; |
| 328 | writev(s, iv, 6); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static void replyError(int s, char *buf) |
| 333 | { |
| 334 | struct iovec iv[3]; |
| 335 | iv[0].iov_base = "0, 0 : ERROR : "; iv[0].iov_len = 15; |
| 336 | iv[1].iov_base = buf; iv[1].iov_len = strlen(buf); |
| 337 | iv[2].iov_base = "\r\n"; iv[2].iov_len = 2; |
| 338 | writev(s, iv, 3); |
| 339 | } |
| 340 | |
| 341 | static int chmatch(char c, char *chars) |
| 342 | { |
| 343 | for (; *chars; chars++) |
| 344 | if (c == *chars) |
| 345 | return 1; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int skipchars(char **p, char *chars) |
| 350 | { |
| 351 | while (chmatch(**p, chars)) |
| 352 | (*p)++; |
| 353 | if (**p == '\r' || **p == '\n') |
| 354 | return 0; |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | static int parseAddrs(char *ptr, char **myaddr, char **heraddr) |
| 359 | { |
| 360 | /* parse <port-on-server> , <port-on-client> */ |
| 361 | |
| 362 | if (!skipchars(&ptr, " \t")) |
| 363 | return -1; |
| 364 | |
| 365 | *myaddr = ptr; |
| 366 | |
| 367 | if (!skipchars(&ptr, "1234567890")) |
| 368 | return -1; |
| 369 | |
| 370 | if (!chmatch(*ptr, " \t,")) |
| 371 | return -1; |
| 372 | |
| 373 | *ptr++ = '\0'; |
| 374 | |
| 375 | if (!skipchars(&ptr, " \t,") ) |
| 376 | return -1; |
| 377 | |
| 378 | *heraddr = ptr; |
| 379 | |
| 380 | skipchars(&ptr, "1234567890"); |
| 381 | |
| 382 | if (!chmatch(*ptr, " \n\r")) |
| 383 | return -1; |
| 384 | |
| 385 | *ptr = '\0'; |
| 386 | |
| 387 | return 0; |
| 388 | } |