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