isrv_identd.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "libbb.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. #define bogouser bb_common_bufsiz1
  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) | 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) MAIN_EXTERNALLY_VISIBLE;
  81. int fakeidentd_main(int argc, char **argv)
  82. {
  83. enum {
  84. OPT_foreground = 0x1,
  85. OPT_inetd = 0x2,
  86. OPT_inetdwait = 0x4,
  87. OPT_fiw = 0x7,
  88. OPT_bindaddr = 0x8,
  89. };
  90. const char *bind_address = NULL;
  91. unsigned opt;
  92. int fd;
  93. opt = getopt32(argv, "fiwb:", &bind_address);
  94. strcpy(bogouser, "nobody");
  95. if (argv[optind])
  96. strncpy(bogouser, argv[optind], sizeof(bogouser));
  97. /* Daemonize if no -f and no -i and no -w */
  98. if (!(opt & OPT_fiw));
  99. bb_daemonize_or_rexec(0, argv);
  100. /* Where to log in inetd modes? "Classic" inetd
  101. * probably has its stderr /dev/null'ed (we need log to syslog?),
  102. * but daemontools-like utilities usually expect that children
  103. * log to stderr. I like daemontools more. Go their way.
  104. * (Or maybe we need yet another option "log to syslog") */
  105. if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
  106. openlog(applet_name, 0, LOG_DAEMON);
  107. logmode = LOGMODE_SYSLOG;
  108. }
  109. if (opt & OPT_inetd) {
  110. inetd_mode();
  111. return 0;
  112. }
  113. /* Ignore closed connections when writing */
  114. signal(SIGPIPE, SIG_IGN);
  115. fd = 0;
  116. if (!(opt & OPT_inetdwait)) {
  117. fd = create_and_bind_stream_or_die(bind_address,
  118. bb_lookup_port("identd", "tcp", 113));
  119. xlisten(fd, 5);
  120. }
  121. isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
  122. TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
  123. return 0;
  124. }