1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2007 Denys Vlasenko
7 * Licensed under GPLv2, see file LICENSE in this source tree.
9 //config:config FAKEIDENTD
10 //config: bool "fakeidentd (8.9 kb)"
12 //config: select FEATURE_SYSLOG
14 //config: fakeidentd listens on the ident port and returns a predefined
15 //config: fake value on any query.
17 //applet:IF_FAKEIDENTD(APPLET(fakeidentd, BB_DIR_USR_SBIN, BB_SUID_DROP))
19 //kbuild:lib-$(CONFIG_FAKEIDENTD) += isrv_identd.o isrv.o
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"
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)"
32 #include "common_bufsiz.h"
36 enum { TIMEOUT = 20 };
38 typedef struct identd_buf_t {
40 char buf[64 - sizeof(int)];
43 #define bogouser bb_common_bufsiz1
45 static int new_peer(isrv_state_t *state, int fd)
48 identd_buf_t *buf = xzalloc(sizeof(*buf));
50 peer = isrv_register_peer(state, buf);
52 return 0; /* failure */
53 if (isrv_register_fd(state, peer, fd) < 0)
54 return peer; /* failure, unregister peer */
57 isrv_want_rd(state, fd);
61 static int do_rd(int fd, void **paramp)
63 identd_buf_t *buf = *paramp;
67 cur = buf->buf + buf->pos;
69 sz = safe_read(fd, cur, sizeof(buf->buf) - 1 - buf->pos);
74 return 0; /* "session is ok" */
78 buf->buf[buf->pos] = '\0';
79 p = strpbrk(cur, "\r\n");
83 return 0; /* "session is ok" */
85 /* Terminate session. If we are in server mode, then
86 * fd is still in nonblocking mode - we never block here */
88 fd++; /* inetd mode? then write to fd 1 */
89 fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
91 * Why bother if we are going to close fd now anyway?
97 return 1; /* "terminate" */
100 static int do_timeout(void **paramp UNUSED_PARAM)
102 return 1; /* terminate session */
105 static void inetd_mode(void)
107 identd_buf_t *buf = xzalloc(sizeof(*buf));
108 /* buf->pos = 0; - xzalloc did it */
111 /* Note: we do NOT want nonblocking I/O here! */
112 while (do_rd(0, (void*)&buf) == 0);
115 int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
116 int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
119 OPT_foreground = 0x1,
126 const char *bind_address = NULL;
130 setup_common_bufsiz();
132 opt = getopt32(argv, "fiwb:", &bind_address);
133 strcpy(bogouser, "nobody");
135 strncpy(bogouser, argv[optind], COMMON_BUFSIZE - 1);
137 /* Daemonize if no -f and no -i and no -w */
138 if (!(opt & OPT_fiw))
139 bb_daemonize_or_rexec(0, argv);
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) */) {
147 openlog(applet_name, LOG_PID, LOG_DAEMON);
148 logmode = LOGMODE_SYSLOG;
151 if (opt & OPT_inetd) {
156 /* Ignore closed connections when writing */
157 signal(SIGPIPE, SIG_IGN);
160 if (!(opt & OPT_inetdwait)) {
161 fd = create_and_bind_stream_or_die(bind_address,
162 bb_lookup_port("identd", "tcp", 113));
166 isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
167 TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);