3
0

isrv_identd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "common_bufsiz.h"
  20. #include <syslog.h>
  21. #include "isrv.h"
  22. enum { TIMEOUT = 20 };
  23. typedef struct identd_buf_t {
  24. int pos;
  25. char buf[64 - 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. ndelay_on(fd);
  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 sz;
  46. cur = buf->buf + buf->pos;
  47. sz = safe_read(fd, cur, sizeof(buf->buf) - 1 - buf->pos);
  48. if (sz < 0) {
  49. if (errno != EAGAIN)
  50. goto term;
  51. return 0; /* "session is ok" */
  52. }
  53. buf->pos += sz;
  54. buf->buf[buf->pos] = '\0';
  55. p = strpbrk(cur, "\r\n");
  56. if (p)
  57. *p = '\0';
  58. if (!p && sz)
  59. return 0; /* "session is ok" */
  60. /* Terminate session. If we are in server mode, then
  61. * fd is still in nonblocking mode - we never block here */
  62. if (fd == 0)
  63. fd++; /* inetd mode? then write to fd 1 */
  64. fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
  65. /*
  66. * Why bother if we are going to close fd now anyway?
  67. * if (server)
  68. * ndelay_off(fd);
  69. */
  70. term:
  71. free(buf);
  72. return 1; /* "terminate" */
  73. }
  74. static int do_timeout(void **paramp UNUSED_PARAM)
  75. {
  76. return 1; /* terminate session */
  77. }
  78. static void inetd_mode(void)
  79. {
  80. identd_buf_t *buf = xzalloc(sizeof(*buf));
  81. /* buf->pos = 0; - xzalloc did it */
  82. do
  83. alarm(TIMEOUT);
  84. /* Note: we do NOT want nonblocking I/O here! */
  85. while (do_rd(0, (void*)&buf) == 0);
  86. }
  87. int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  88. int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
  89. {
  90. enum {
  91. OPT_foreground = 0x1,
  92. OPT_inetd = 0x2,
  93. OPT_inetdwait = 0x4,
  94. OPT_fiw = 0x7,
  95. OPT_bindaddr = 0x8,
  96. };
  97. const char *bind_address = NULL;
  98. unsigned opt;
  99. int fd;
  100. setup_common_bufsiz();
  101. opt = getopt32(argv, "fiwb:", &bind_address);
  102. strcpy(bogouser, "nobody");
  103. if (argv[optind])
  104. strncpy(bogouser, argv[optind], COMMON_BUFSIZE - 1);
  105. /* Daemonize if no -f and no -i and no -w */
  106. if (!(opt & OPT_fiw))
  107. bb_daemonize_or_rexec(0, argv);
  108. /* Where to log in inetd modes? "Classic" inetd
  109. * probably has its stderr /dev/null'ed (we need log to syslog?),
  110. * but daemontools-like utilities usually expect that children
  111. * log to stderr. I like daemontools more. Go their way.
  112. * (Or maybe we need yet another option "log to syslog") */
  113. if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
  114. openlog(applet_name, LOG_PID, LOG_DAEMON);
  115. logmode = LOGMODE_SYSLOG;
  116. }
  117. if (opt & OPT_inetd) {
  118. inetd_mode();
  119. return 0;
  120. }
  121. /* Ignore closed connections when writing */
  122. signal(SIGPIPE, SIG_IGN);
  123. fd = 0;
  124. if (!(opt & OPT_inetdwait)) {
  125. fd = create_and_bind_stream_or_die(bind_address,
  126. bb_lookup_port("identd", "tcp", 113));
  127. xlisten(fd, 5);
  128. }
  129. isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
  130. TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
  131. return 0;
  132. }