3
0

fakeidentd.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. */
  11. #include "busybox.h"
  12. #include <sys/syslog.h>
  13. #include <sys/uio.h>
  14. #define IDENT_PORT 113
  15. #define MAXCONNS 20
  16. #define MAXIDLETIME 45
  17. static const char ident_substr[] = " : USERID : UNIX : ";
  18. enum { ident_substr_len = sizeof(ident_substr) - 1 };
  19. #define PIDFILE "/var/run/identd.pid"
  20. /*
  21. * We have to track the 'first connection socket' so that we
  22. * don't go around closing file descriptors for non-clients.
  23. *
  24. * descriptor setup normally
  25. * 0 = server socket
  26. * 1 = syslog fd (hopefully -- otherwise this won't work)
  27. * 2 = connection socket after detached from tty. standard error before that
  28. * 3 - 2 + MAXCONNS = rest connection sockets
  29. *
  30. * To try to make sure that syslog fd is what is "requested", the that fd
  31. * is closed before openlog() call. It can only severely fail if fd 0
  32. * is initially closed.
  33. */
  34. #define FCS 2
  35. /*
  36. * FD of the connection is always the index of the connection structure
  37. * in `conns' array + FCS
  38. */
  39. static struct {
  40. char buf[20];
  41. int len;
  42. time_t lasttime;
  43. } conns[MAXCONNS];
  44. /* When using global variables, bind those at least to a structure. */
  45. static struct {
  46. const char *identuser;
  47. fd_set readfds;
  48. int conncnt;
  49. } G;
  50. /*
  51. * Prototypes
  52. */
  53. static void reply(int s, char *buf);
  54. static void replyError(int s, char *buf);
  55. static const char *nobodystr = "nobody"; /* this needs to be declared like this */
  56. static char *bind_ip_address = "0.0.0.0";
  57. static void movefd(int from, int to)
  58. {
  59. if (from != to) {
  60. dup2(from, to);
  61. close(from);
  62. }
  63. }
  64. static void inetbind(void)
  65. {
  66. int s, port;
  67. struct sockaddr_in addr;
  68. int len = sizeof(addr);
  69. struct servent *se;
  70. se = getservbyname("identd", "tcp");
  71. port = IDENT_PORT;
  72. if (se)
  73. port = se->s_port;
  74. s = xsocket(AF_INET, SOCK_STREAM, 0);
  75. setsockopt_reuseaddr(s);
  76. memset(&addr, 0, sizeof(addr));
  77. addr.sin_addr.s_addr = inet_addr(bind_ip_address);
  78. addr.sin_family = AF_INET;
  79. addr.sin_port = htons(port);
  80. xbind(s, (struct sockaddr *)&addr, len);
  81. xlisten(s, 5);
  82. movefd(s, 0);
  83. }
  84. static void handlexitsigs(int signum)
  85. {
  86. if (unlink(PIDFILE) < 0)
  87. close(open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0644));
  88. exit(0);
  89. }
  90. /* May succeed. If not, won't care. */
  91. static void writepid(uid_t nobody, uid_t nogrp)
  92. {
  93. char buf[sizeof(int)*3 + 2];
  94. int fd = open(PIDFILE, O_WRONLY|O_CREAT|O_TRUNC, 0664);
  95. if (fd < 0)
  96. return;
  97. sprintf(buf, "%d\n", getpid());
  98. write(fd, buf, strlen(buf));
  99. fchown(fd, nobody, nogrp);
  100. close(fd);
  101. /* should this handle ILL, ... (see signal(7)) */
  102. signal(SIGTERM, handlexitsigs);
  103. signal(SIGINT, handlexitsigs);
  104. signal(SIGQUIT, handlexitsigs);
  105. }
  106. /* return 0 as parent, 1 as child */
  107. static int godaemon(void)
  108. {
  109. uid_t nobody, nogrp;
  110. struct passwd *pw;
  111. switch (fork()) {
  112. case -1:
  113. bb_perror_msg_and_die("fork");
  114. case 0:
  115. pw = getpwnam(nobodystr);
  116. if (pw == NULL)
  117. bb_error_msg_and_die("cannot find uid/gid of user '%s'", nobodystr);
  118. nobody = pw->pw_uid;
  119. nogrp = pw->pw_gid;
  120. writepid(nobody, nogrp);
  121. close(0);
  122. inetbind();
  123. xsetgid(nogrp);
  124. xsetuid(nobody);
  125. close(1);
  126. close(2);
  127. signal(SIGHUP, SIG_IGN);
  128. signal(SIGPIPE, SIG_IGN); /* connection closed when writing (raises ???) */
  129. setsid();
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. static void deleteConn(int s)
  135. {
  136. int i = s - FCS;
  137. close(s);
  138. G.conncnt--;
  139. /*
  140. * Most of the time there is 0 connections. Most often that there
  141. * is connections, there is just one connection. When this one connection
  142. * closes, i == G.conncnt = 0 -> no copying.
  143. * When there is more than one connection, the oldest connections closes
  144. * earlier on average. When this happens, the code below starts copying
  145. * the connection structure w/ highest index to the place which which is
  146. * just deleted. This means that the connection structures are no longer
  147. * in chronological order. I'd quess this means that when there is more
  148. * than 1 connection, on average every other connection structure needs
  149. * to be copied over the time all these connections are deleted.
  150. */
  151. if (i != G.conncnt) {
  152. memcpy(&conns[i], &conns[G.conncnt], sizeof(conns[0]));
  153. movefd(G.conncnt + FCS, s);
  154. }
  155. FD_CLR(G.conncnt + FCS, &G.readfds);
  156. }
  157. static int closeOldest(void)
  158. {
  159. time_t min = conns[0].lasttime;
  160. int idx = 0;
  161. int i;
  162. for (i = 1; i < MAXCONNS; i++)
  163. if (conns[i].lasttime < min)
  164. idx = i;
  165. replyError(idx + FCS, "X-SERVER-TOO-BUSY");
  166. close(idx + FCS);
  167. return idx;
  168. }
  169. static int checkInput(char *buf, int len, int l)
  170. {
  171. int i;
  172. for (i = len; i < len + l; ++i)
  173. if (buf[i] == '\n')
  174. return 1;
  175. return 0;
  176. }
  177. int fakeidentd_main(int argc, char **argv)
  178. {
  179. /* This applet is an inetd-style daemon */
  180. openlog(applet_name, 0, LOG_DAEMON);
  181. logmode = LOGMODE_SYSLOG;
  182. memset(conns, 0, sizeof(conns));
  183. memset(&G, 0, sizeof(G));
  184. FD_ZERO(&G.readfds);
  185. FD_SET(0, &G.readfds);
  186. /* handle -b <ip> parameter */
  187. getopt32(argc, argv, "b:", &bind_ip_address);
  188. /* handle optional REPLY STRING */
  189. if (optind < argc)
  190. G.identuser = argv[optind];
  191. else
  192. G.identuser = nobodystr;
  193. /* daemonize and have the parent return */
  194. if (godaemon() == 0)
  195. return 0;
  196. /* main loop where we process all events and never exit */
  197. while (1) {
  198. fd_set rfds = G.readfds;
  199. struct timeval tv = { 15, 0 };
  200. int i;
  201. int tim = time(NULL);
  202. select(G.conncnt + FCS, &rfds, NULL, NULL, G.conncnt? &tv: NULL);
  203. for (i = G.conncnt - 1; i >= 0; i--) {
  204. int s = i + FCS;
  205. if (FD_ISSET(s, &rfds)) {
  206. char *buf = conns[i].buf;
  207. unsigned int len = conns[i].len;
  208. unsigned int l;
  209. if ((l = read(s, buf + len, sizeof(conns[0].buf) - len)) > 0) {
  210. if (checkInput(buf, len, l)) {
  211. reply(s, buf);
  212. goto deleteconn;
  213. } else if (len + l >= sizeof(conns[0].buf)) {
  214. replyError(s, "X-INVALID-REQUEST");
  215. goto deleteconn;
  216. } else {
  217. conns[i].len += l;
  218. }
  219. } else {
  220. goto deleteconn;
  221. }
  222. conns[i].lasttime = tim;
  223. continue;
  224. deleteconn:
  225. deleteConn(s);
  226. } else {
  227. /* implement as time_after() in linux kernel sources ... */
  228. if (conns[i].lasttime + MAXIDLETIME <= tim) {
  229. replyError(s, "X-TIMEOUT");
  230. deleteConn(s);
  231. }
  232. }
  233. }
  234. if (FD_ISSET(0, &rfds)) {
  235. int s = accept(0, NULL, 0);
  236. if (s < 0) {
  237. if (errno != EINTR) /* EINTR */
  238. bb_perror_msg("accept");
  239. } else {
  240. if (G.conncnt == MAXCONNS)
  241. i = closeOldest();
  242. else
  243. i = G.conncnt++;
  244. movefd(s, i + FCS); /* move if not already there */
  245. FD_SET(i + FCS, &G.readfds);
  246. conns[i].len = 0;
  247. conns[i].lasttime = time(NULL);
  248. }
  249. }
  250. } /* end of while(1) */
  251. return 0;
  252. }
  253. static int parseAddrs(char *ptr, char **myaddr, char **heraddr);
  254. static void reply(int s, char *buf)
  255. {
  256. char *myaddr, *heraddr;
  257. myaddr = heraddr = NULL;
  258. if (parseAddrs(buf, &myaddr, &heraddr))
  259. replyError(s, "X-INVALID-REQUEST");
  260. else {
  261. struct iovec iv[6];
  262. iv[0].iov_base = myaddr; iv[0].iov_len = strlen(myaddr);
  263. iv[1].iov_base = ", "; iv[1].iov_len = 2;
  264. iv[2].iov_base = heraddr; iv[2].iov_len = strlen(heraddr);
  265. iv[3].iov_base = (void *)ident_substr; iv[3].iov_len = ident_substr_len;
  266. iv[4].iov_base = (void *)G.identuser; iv[4].iov_len = strlen(G.identuser);
  267. iv[5].iov_base = "\r\n"; iv[5].iov_len = 2;
  268. writev(s, iv, 6);
  269. }
  270. }
  271. static void replyError(int s, char *buf)
  272. {
  273. struct iovec iv[3];
  274. iv[0].iov_base = "0, 0 : ERROR : "; iv[0].iov_len = 15;
  275. iv[1].iov_base = buf; iv[1].iov_len = strlen(buf);
  276. iv[2].iov_base = "\r\n"; iv[2].iov_len = 2;
  277. writev(s, iv, 3);
  278. }
  279. static int chmatch(char c, char *chars)
  280. {
  281. for (; *chars; chars++)
  282. if (c == *chars)
  283. return 1;
  284. return 0;
  285. }
  286. static int skipchars(char **p, char *chars)
  287. {
  288. while (chmatch(**p, chars))
  289. (*p)++;
  290. if (**p == '\r' || **p == '\n')
  291. return 0;
  292. return 1;
  293. }
  294. static int parseAddrs(char *ptr, char **myaddr, char **heraddr)
  295. {
  296. /* parse <port-on-server> , <port-on-client> */
  297. if (!skipchars(&ptr, " \t"))
  298. return -1;
  299. *myaddr = ptr;
  300. if (!skipchars(&ptr, "1234567890"))
  301. return -1;
  302. if (!chmatch(*ptr, " \t,"))
  303. return -1;
  304. *ptr++ = '\0';
  305. if (!skipchars(&ptr, " \t,") )
  306. return -1;
  307. *heraddr = ptr;
  308. skipchars(&ptr, "1234567890");
  309. if (!chmatch(*ptr, " \n\r"))
  310. return -1;
  311. *ptr = '\0';
  312. return 0;
  313. }