isrv_identd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Fake identd server.
  4. *
  5. * Copyright (C) 2007 Denis Vlasenko
  6. *
  7. * Licensed under GPL version 2, see file LICENSE in this tarball for details.
  8. */
  9. #include <syslog.h>
  10. #include "busybox.h"
  11. #include "isrv.h"
  12. enum { TIMEOUT = 20 };
  13. typedef struct identd_buf_t {
  14. int pos;
  15. int fd_flag;
  16. char buf[64 - 2*sizeof(int)];
  17. } identd_buf_t;
  18. static const char *bogouser = "nobody";
  19. static int new_peer(isrv_state_t *state, int fd)
  20. {
  21. int peer;
  22. identd_buf_t *buf = xzalloc(sizeof(*buf));
  23. peer = isrv_register_peer(state, buf);
  24. if (peer < 0)
  25. return 0; /* failure */
  26. if (isrv_register_fd(state, peer, fd) < 0)
  27. return peer; /* failure, unregister peer */
  28. buf->fd_flag = fcntl(fd, F_GETFL, 0) | O_NONBLOCK;
  29. isrv_want_rd(state, fd);
  30. return 0;
  31. }
  32. static int do_rd(int fd, void **paramp)
  33. {
  34. identd_buf_t *buf = *paramp;
  35. char *cur, *p;
  36. int retval = 0; /* session is ok (so far) */
  37. int sz;
  38. cur = buf->buf + buf->pos;
  39. if (buf->fd_flag & O_NONBLOCK)
  40. fcntl(fd, F_SETFL, buf->fd_flag);
  41. sz = safe_read(fd, cur, sizeof(buf->buf) - buf->pos);
  42. if (sz < 0) {
  43. if (errno != EAGAIN)
  44. goto term; /* terminate this session if !EAGAIN */
  45. goto ok;
  46. }
  47. buf->pos += sz;
  48. buf->buf[buf->pos] = '\0';
  49. p = strpbrk(cur, "\r\n");
  50. if (p)
  51. *p = '\0';
  52. if (!p && sz && buf->pos <= sizeof(buf->buf))
  53. goto ok;
  54. /* Terminate session. If we are in server mode, then
  55. * fd is still in nonblocking mode - we never block here */
  56. if (fd == 0) fd++; /* inetd mode? then write to fd 1 */
  57. fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
  58. term:
  59. free(buf);
  60. retval = 1; /* terminate */
  61. ok:
  62. if (buf->fd_flag & O_NONBLOCK)
  63. fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK);
  64. return retval;
  65. }
  66. static int do_timeout(void **paramp)
  67. {
  68. return 1; /* terminate session */
  69. }
  70. static void inetd_mode(void)
  71. {
  72. identd_buf_t *buf = xzalloc(sizeof(*buf));
  73. /* buf->pos = 0; - xzalloc did it */
  74. /* We do NOT want nonblocking I/O here! */
  75. /* buf->fd_flag = 0; - xzalloc did it */
  76. do
  77. alarm(TIMEOUT);
  78. while (do_rd(0, (void*)&buf) == 0);
  79. }
  80. int fakeidentd_main(int argc, char **argv)
  81. {
  82. enum {
  83. OPT_foreground = 0x1,
  84. OPT_inetd = 0x2,
  85. OPT_inetdwait = 0x4,
  86. OPT_fiw = 0x7,
  87. OPT_bindaddr = 0x8,
  88. };
  89. const char *bind_address = NULL;
  90. unsigned opt;
  91. int fd;
  92. opt = getopt32(argc, argv, "fiwb:", &bind_address);
  93. if (optind < argc)
  94. bogouser = argv[optind];
  95. /* Daemonize if no -f and no -i and no -w */
  96. bb_sanitize_stdio_maybe_daemonize(!(opt & OPT_fiw));
  97. /* Where to log in inetd modes? "Classic" inetd
  98. * probably has its stderr /dev/null'ed (we need log to syslog?),
  99. * but daemontools-like utilities usually expect that children
  100. * log to stderr. I like daemontools more. Go their way.
  101. * (Or maybe we need yet another option "log to syslog") */
  102. if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
  103. openlog(applet_name, 0, LOG_DAEMON);
  104. logmode = LOGMODE_SYSLOG;
  105. }
  106. if (opt & OPT_inetd) {
  107. inetd_mode();
  108. return 0;
  109. }
  110. /* Ignore closed connections when writing */
  111. signal(SIGPIPE, SIG_IGN);
  112. fd = 0;
  113. if (!(opt & OPT_inetdwait)) {
  114. fd = create_and_bind_stream_or_die(bind_address,
  115. bb_lookup_port("identd", "tcp", 113));
  116. xlisten(fd, 5);
  117. }
  118. isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
  119. TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
  120. return 0;
  121. }