3
0

fakeidentd.c 9.7 KB

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