isrv_identd.c 3.8 KB

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