isrv_identd.c 3.6 KB

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