blob: c6b0f6528487c92f9fdc7f6fa63f3a81c2b48eee [file] [log] [blame]
Denis Vlasenko7a431b32007-01-14 01:29:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Fake identd server.
4 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko
Denis Vlasenko7a431b32007-01-14 01:29:06 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko7a431b32007-01-14 01:29:06 +00008 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//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 Orga5bc8c002011-04-11 03:29:49 +020014//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 Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000021#include <syslog.h>
Denis Vlasenko7a431b32007-01-14 01:29:06 +000022#include "isrv.h"
23
24enum { TIMEOUT = 20 };
25
Denis Vlasenko7a431b32007-01-14 01:29:06 +000026typedef struct identd_buf_t {
27 int pos;
28 int fd_flag;
29 char buf[64 - 2*sizeof(int)];
30} identd_buf_t;
31
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000032#define bogouser bb_common_bufsiz1
Denis Vlasenko7a431b32007-01-14 01:29:06 +000033
34static int new_peer(isrv_state_t *state, int fd)
35{
36 int peer;
37 identd_buf_t *buf = xzalloc(sizeof(*buf));
38
Denis Vlasenko7a431b32007-01-14 01:29:06 +000039 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 Vlasenkod37f2222007-08-19 13:42:08 +000045 buf->fd_flag = fcntl(fd, F_GETFL) | O_NONBLOCK;
Denis Vlasenko7a431b32007-01-14 01:29:06 +000046 isrv_want_rd(state, fd);
47 return 0;
48}
49
50static int do_rd(int fd, void **paramp)
51{
52 identd_buf_t *buf = *paramp;
53 char *cur, *p;
54 int sz;
55
Denis Vlasenko7a431b32007-01-14 01:29:06 +000056 cur = buf->buf + buf->pos;
57
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000058 if (buf->fd_flag & O_NONBLOCK)
59 fcntl(fd, F_SETFL, buf->fd_flag);
Ryan Mallon89deb222014-01-09 19:14:07 +010060 sz = safe_read(fd, cur, sizeof(buf->buf) - 1 - buf->pos);
Denis Vlasenko7a431b32007-01-14 01:29:06 +000061
62 if (sz < 0) {
63 if (errno != EAGAIN)
Ryan Mallon89deb222014-01-09 19:14:07 +010064 goto term;
65 return 0; /* "session is ok" */
Denis Vlasenko7a431b32007-01-14 01:29:06 +000066 }
67
68 buf->pos += sz;
69 buf->buf[buf->pos] = '\0';
70 p = strpbrk(cur, "\r\n");
71 if (p)
72 *p = '\0';
Ryan Mallon89deb222014-01-09 19:14:07 +010073 if (!p && sz && buf->pos < (int)sizeof(buf->buf))
74 return 0; /* "session is ok" */
75
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000076 /* Terminate session. If we are in server mode, then
77 * fd is still in nonblocking mode - we never block here */
Ryan Mallon89deb222014-01-09 19:14:07 +010078 if (fd == 0)
79 fd++; /* inetd mode? then write to fd 1 */
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000080 fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000081 if (buf->fd_flag & O_NONBLOCK)
82 fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK);
Ryan Mallon89deb222014-01-09 19:14:07 +010083 term:
84 free(buf);
85 return 1; /* "terminate" */
Denis Vlasenko7a431b32007-01-14 01:29:06 +000086}
87
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000088static int do_timeout(void **paramp UNUSED_PARAM)
Denis Vlasenko7a431b32007-01-14 01:29:06 +000089{
90 return 1; /* terminate session */
91}
92
93static void inetd_mode(void)
94{
95 identd_buf_t *buf = xzalloc(sizeof(*buf));
Denis Vlasenkob8b81a62007-01-14 12:47:27 +000096 /* buf->pos = 0; - xzalloc did it */
Denis Vlasenko7a431b32007-01-14 01:29:06 +000097 /* We do NOT want nonblocking I/O here! */
Denis Vlasenkob8b81a62007-01-14 12:47:27 +000098 /* buf->fd_flag = 0; - xzalloc did it */
Denis Vlasenko19250812007-01-14 12:07:25 +000099 do
100 alarm(TIMEOUT);
Denis Vlasenkob8b81a62007-01-14 12:47:27 +0000101 while (do_rd(0, (void*)&buf) == 0);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000102}
103
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000104int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000105int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000106{
107 enum {
108 OPT_foreground = 0x1,
109 OPT_inetd = 0x2,
110 OPT_inetdwait = 0x4,
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000111 OPT_fiw = 0x7,
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000112 OPT_bindaddr = 0x8,
113 };
114
115 const char *bind_address = NULL;
116 unsigned opt;
117 int fd;
118
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000119 opt = getopt32(argv, "fiwb:", &bind_address);
Denis Vlasenko04c99eb2007-04-07 00:44:31 +0000120 strcpy(bogouser, "nobody");
121 if (argv[optind])
Ryan Mallon89deb222014-01-09 19:14:07 +0100122 strncpy(bogouser, argv[optind], sizeof(bogouser) - 1);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000123
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000124 /* Daemonize if no -f and no -i and no -w */
Denis Vlasenko43d5d422008-05-15 19:44:46 +0000125 if (!(opt & OPT_fiw))
Denis Vlasenko5a142022007-03-26 13:20:54 +0000126 bb_daemonize_or_rexec(0, argv);
127
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000128 /* Where to log in inetd modes? "Classic" inetd
129 * probably has its stderr /dev/null'ed (we need log to syslog?),
130 * but daemontools-like utilities usually expect that children
131 * log to stderr. I like daemontools more. Go their way.
132 * (Or maybe we need yet another option "log to syslog") */
133 if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000134 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000135 logmode = LOGMODE_SYSLOG;
136 }
137
138 if (opt & OPT_inetd) {
139 inetd_mode();
140 return 0;
141 }
142
143 /* Ignore closed connections when writing */
144 signal(SIGPIPE, SIG_IGN);
145
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000146 fd = 0;
147 if (!(opt & OPT_inetdwait)) {
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000148 fd = create_and_bind_stream_or_die(bind_address,
149 bb_lookup_port("identd", "tcp", 113));
150 xlisten(fd, 5);
151 }
152
Denis Vlasenko19250812007-01-14 12:07:25 +0000153 isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
154 TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000155 return 0;
156}