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