Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 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 | |
| 10 | #include <syslog.h> |
| 11 | #include "busybox.h" |
| 12 | #include "isrv.h" |
| 13 | |
| 14 | enum { TIMEOUT = 20 }; |
| 15 | |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 16 | typedef struct identd_buf_t { |
| 17 | int pos; |
| 18 | int fd_flag; |
| 19 | char buf[64 - 2*sizeof(int)]; |
| 20 | } identd_buf_t; |
| 21 | |
| 22 | static const char *bogouser = "nobody"; |
| 23 | |
| 24 | static int new_peer(isrv_state_t *state, int fd) |
| 25 | { |
| 26 | int peer; |
| 27 | identd_buf_t *buf = xzalloc(sizeof(*buf)); |
| 28 | |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 29 | peer = isrv_register_peer(state, buf); |
| 30 | if (peer < 0) |
| 31 | return 0; /* failure */ |
| 32 | if (isrv_register_fd(state, peer, fd) < 0) |
| 33 | return peer; /* failure, unregister peer */ |
| 34 | |
| 35 | buf->fd_flag = fcntl(fd, F_GETFL, 0) | O_NONBLOCK; |
| 36 | isrv_want_rd(state, fd); |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int do_rd(int fd, void **paramp) |
| 41 | { |
| 42 | identd_buf_t *buf = *paramp; |
| 43 | char *cur, *p; |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 44 | int retval = 0; /* session is ok (so far) */ |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 45 | int sz; |
| 46 | |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 47 | cur = buf->buf + buf->pos; |
| 48 | |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 49 | if (buf->fd_flag & O_NONBLOCK) |
| 50 | fcntl(fd, F_SETFL, buf->fd_flag); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 51 | sz = safe_read(fd, cur, sizeof(buf->buf) - buf->pos); |
| 52 | |
| 53 | if (sz < 0) { |
| 54 | if (errno != EAGAIN) |
| 55 | goto term; /* terminate this session if !EAGAIN */ |
| 56 | goto ok; |
| 57 | } |
| 58 | |
| 59 | buf->pos += sz; |
| 60 | buf->buf[buf->pos] = '\0'; |
| 61 | p = strpbrk(cur, "\r\n"); |
| 62 | if (p) |
| 63 | *p = '\0'; |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 64 | if (!p && sz && buf->pos <= sizeof(buf->buf)) |
| 65 | goto ok; |
| 66 | /* Terminate session. If we are in server mode, then |
| 67 | * fd is still in nonblocking mode - we never block here */ |
| 68 | fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 69 | term: |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 70 | free(buf); |
Denis Vlasenko | c14c95e | 2007-01-14 12:31:26 +0000 | [diff] [blame] | 71 | retval = 1; /* terminate */ |
| 72 | ok: |
| 73 | if (buf->fd_flag & O_NONBLOCK) |
| 74 | fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK); |
| 75 | return retval; |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static int do_timeout(void **paramp) |
| 79 | { |
| 80 | return 1; /* terminate session */ |
| 81 | } |
| 82 | |
| 83 | static void inetd_mode(void) |
| 84 | { |
| 85 | identd_buf_t *buf = xzalloc(sizeof(*buf)); |
Denis Vlasenko | b8b81a6 | 2007-01-14 12:47:27 +0000 | [diff] [blame^] | 86 | /* buf->pos = 0; - xzalloc did it */ |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 87 | /* We do NOT want nonblocking I/O here! */ |
Denis Vlasenko | b8b81a6 | 2007-01-14 12:47:27 +0000 | [diff] [blame^] | 88 | /* buf->fd_flag = 0; - xzalloc did it */ |
Denis Vlasenko | 1925081 | 2007-01-14 12:07:25 +0000 | [diff] [blame] | 89 | do |
| 90 | alarm(TIMEOUT); |
Denis Vlasenko | b8b81a6 | 2007-01-14 12:47:27 +0000 | [diff] [blame^] | 91 | while (do_rd(0, (void*)&buf) == 0); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int fakeidentd_main(int argc, char **argv) |
| 95 | { |
| 96 | enum { |
| 97 | OPT_foreground = 0x1, |
| 98 | OPT_inetd = 0x2, |
| 99 | OPT_inetdwait = 0x4, |
| 100 | OPT_nodeamon = 0x7, |
| 101 | OPT_bindaddr = 0x8, |
| 102 | }; |
| 103 | |
| 104 | const char *bind_address = NULL; |
| 105 | unsigned opt; |
| 106 | int fd; |
| 107 | |
| 108 | opt = getopt32(argc, argv, "fiwb:", &bind_address); |
| 109 | if (optind < argc) |
| 110 | bogouser = argv[optind]; |
| 111 | |
| 112 | /* Daemonize if no -f or -i or -w */ |
| 113 | bb_sanitize_stdio(!(opt & OPT_nodeamon)); |
| 114 | if (!(opt & OPT_nodeamon)) { |
| 115 | openlog(applet_name, 0, LOG_DAEMON); |
| 116 | logmode = LOGMODE_SYSLOG; |
| 117 | } |
| 118 | |
| 119 | if (opt & OPT_inetd) { |
| 120 | inetd_mode(); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* Ignore closed connections when writing */ |
| 125 | signal(SIGPIPE, SIG_IGN); |
| 126 | |
| 127 | if (opt & OPT_inetdwait) { |
| 128 | fd = 0; |
| 129 | } else { |
| 130 | fd = create_and_bind_stream_or_die(bind_address, |
| 131 | bb_lookup_port("identd", "tcp", 113)); |
| 132 | xlisten(fd, 5); |
| 133 | } |
| 134 | |
Denis Vlasenko | 1925081 | 2007-01-14 12:07:25 +0000 | [diff] [blame] | 135 | isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout, |
| 136 | TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0); |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 137 | return 0; |
| 138 | } |