isrv_identd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. //config:config FAKEIDENTD
  10. //config: bool "fakeidentd (8.9 kb)"
  11. //config: default y
  12. //config: select FEATURE_SYSLOG
  13. //config: help
  14. //config: fakeidentd listens on the ident port and returns a predefined
  15. //config: fake value on any query.
  16. //applet:IF_FAKEIDENTD(APPLET(fakeidentd, BB_DIR_USR_SBIN, BB_SUID_DROP))
  17. //kbuild:lib-$(CONFIG_FAKEIDENTD) += isrv_identd.o isrv.o
  18. //usage:#define fakeidentd_trivial_usage
  19. //usage: "[-fiw] [-b ADDR] [STRING]"
  20. //usage:#define fakeidentd_full_usage "\n\n"
  21. //usage: "Provide fake ident (auth) service\n"
  22. //usage: "\n -f Run in foreground"
  23. //usage: "\n -i Inetd mode"
  24. //usage: "\n -w Inetd 'wait' mode"
  25. //usage: "\n -b ADDR Bind to specified address"
  26. //usage: "\n STRING Ident answer string (default: nobody)"
  27. #include "libbb.h"
  28. #include "common_bufsiz.h"
  29. #include <syslog.h>
  30. #include "isrv.h"
  31. enum { TIMEOUT = 20 };
  32. typedef struct identd_buf_t {
  33. int pos;
  34. char buf[64 - sizeof(int)];
  35. } identd_buf_t;
  36. #define bogouser bb_common_bufsiz1
  37. static int new_peer(isrv_state_t *state, int fd)
  38. {
  39. int peer;
  40. identd_buf_t *buf = xzalloc(sizeof(*buf));
  41. peer = isrv_register_peer(state, buf);
  42. if (peer < 0)
  43. return 0; /* failure */
  44. if (isrv_register_fd(state, peer, fd) < 0)
  45. return peer; /* failure, unregister peer */
  46. ndelay_on(fd);
  47. isrv_want_rd(state, fd);
  48. return 0;
  49. }
  50. static int do_rd(int fd, void **paramp)
  51. {
  52. identd_buf_t *buf = *paramp;
  53. char *cur, *p;
  54. int sz;
  55. cur = buf->buf + buf->pos;
  56. sz = safe_read(fd, cur, sizeof(buf->buf) - 1 - buf->pos);
  57. if (sz < 0) {
  58. if (errno != EAGAIN)
  59. goto term;
  60. return 0; /* "session is ok" */
  61. }
  62. buf->pos += sz;
  63. buf->buf[buf->pos] = '\0';
  64. p = strpbrk(cur, "\r\n");
  65. if (p)
  66. *p = '\0';
  67. if (!p && sz)
  68. return 0; /* "session is ok" */
  69. /* Terminate session. If we are in server mode, then
  70. * fd is still in nonblocking mode - we never block here */
  71. if (fd == 0)
  72. fd++; /* inetd mode? then write to fd 1 */
  73. fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
  74. /*
  75. * Why bother if we are going to close fd now anyway?
  76. * if (server)
  77. * ndelay_off(fd);
  78. */
  79. term:
  80. free(buf);
  81. return 1; /* "terminate" */
  82. }
  83. static int do_timeout(void **paramp UNUSED_PARAM)
  84. {
  85. return 1; /* terminate session */
  86. }
  87. static void inetd_mode(void)
  88. {
  89. identd_buf_t *buf = xzalloc(sizeof(*buf));
  90. /* buf->pos = 0; - xzalloc did it */
  91. do
  92. alarm(TIMEOUT);
  93. /* Note: we do NOT want nonblocking I/O here! */
  94. while (do_rd(0, (void*)&buf) == 0);
  95. }
  96. int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  97. int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
  98. {
  99. enum {
  100. OPT_foreground = 0x1,
  101. OPT_inetd = 0x2,
  102. OPT_inetdwait = 0x4,
  103. OPT_fiw = 0x7,
  104. OPT_bindaddr = 0x8,
  105. };
  106. const char *bind_address = NULL;
  107. unsigned opt;
  108. int fd;
  109. setup_common_bufsiz();
  110. opt = getopt32(argv, "fiwb:", &bind_address);
  111. strcpy(bogouser, "nobody");
  112. if (argv[optind])
  113. strncpy(bogouser, argv[optind], COMMON_BUFSIZE - 1);
  114. /* Daemonize if no -f and no -i and no -w */
  115. if (!(opt & OPT_fiw))
  116. bb_daemonize_or_rexec(0, argv);
  117. /* Where to log in inetd modes? "Classic" inetd
  118. * probably has its stderr /dev/null'ed (we need log to syslog?),
  119. * but daemontools-like utilities usually expect that children
  120. * log to stderr. I like daemontools more. Go their way.
  121. * (Or maybe we need yet another option "log to syslog") */
  122. if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
  123. openlog(applet_name, LOG_PID, LOG_DAEMON);
  124. logmode = LOGMODE_SYSLOG;
  125. }
  126. if (opt & OPT_inetd) {
  127. inetd_mode();
  128. return 0;
  129. }
  130. /* Ignore closed connections when writing */
  131. signal(SIGPIPE, SIG_IGN);
  132. fd = 0;
  133. if (!(opt & OPT_inetdwait)) {
  134. fd = create_and_bind_stream_or_die(bind_address,
  135. bb_lookup_port("identd", "tcp", 113));
  136. xlisten(fd, 5);
  137. }
  138. isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
  139. TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
  140. return 0;
  141. }