blob: f63ed8ee4012264ce9e8485d4a4c1b4acb016579 [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"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020021#include "common_bufsiz.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000022#include <syslog.h>
Denis Vlasenko7a431b32007-01-14 01:29:06 +000023#include "isrv.h"
24
25enum { TIMEOUT = 20 };
26
Denis Vlasenko7a431b32007-01-14 01:29:06 +000027typedef struct identd_buf_t {
28 int pos;
Denys Vlasenko604b7b62014-01-10 17:12:54 +010029 char buf[64 - sizeof(int)];
Denis Vlasenko7a431b32007-01-14 01:29:06 +000030} identd_buf_t;
31
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020032#define bogouser bb_common_bufsiz1
33#define sizeof_bogouser COMMON_BUFSIZE
Denis Vlasenko7a431b32007-01-14 01:29:06 +000034
35static int new_peer(isrv_state_t *state, int fd)
36{
37 int peer;
38 identd_buf_t *buf = xzalloc(sizeof(*buf));
39
Denis Vlasenko7a431b32007-01-14 01:29:06 +000040 peer = isrv_register_peer(state, buf);
41 if (peer < 0)
42 return 0; /* failure */
43 if (isrv_register_fd(state, peer, fd) < 0)
44 return peer; /* failure, unregister peer */
45
Denys Vlasenko604b7b62014-01-10 17:12:54 +010046 ndelay_on(fd);
Denis Vlasenko7a431b32007-01-14 01:29:06 +000047 isrv_want_rd(state, fd);
48 return 0;
49}
50
51static int do_rd(int fd, void **paramp)
52{
53 identd_buf_t *buf = *paramp;
54 char *cur, *p;
55 int sz;
56
Denis Vlasenko7a431b32007-01-14 01:29:06 +000057 cur = buf->buf + buf->pos;
58
Ryan Mallon89deb222014-01-09 19:14:07 +010059 sz = safe_read(fd, cur, sizeof(buf->buf) - 1 - buf->pos);
Denis Vlasenko7a431b32007-01-14 01:29:06 +000060
61 if (sz < 0) {
62 if (errno != EAGAIN)
Ryan Mallon89deb222014-01-09 19:14:07 +010063 goto term;
64 return 0; /* "session is ok" */
Denis Vlasenko7a431b32007-01-14 01:29:06 +000065 }
66
67 buf->pos += sz;
68 buf->buf[buf->pos] = '\0';
69 p = strpbrk(cur, "\r\n");
70 if (p)
71 *p = '\0';
Denys Vlasenko604b7b62014-01-10 17:12:54 +010072 if (!p && sz)
Ryan Mallon89deb222014-01-09 19:14:07 +010073 return 0; /* "session is ok" */
74
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000075 /* Terminate session. If we are in server mode, then
76 * fd is still in nonblocking mode - we never block here */
Ryan Mallon89deb222014-01-09 19:14:07 +010077 if (fd == 0)
78 fd++; /* inetd mode? then write to fd 1 */
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000079 fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
Denys Vlasenko604b7b62014-01-10 17:12:54 +010080 /*
81 * Why bother if we are going to close fd now anyway?
82 * if (server)
83 * ndelay_off(fd);
84 */
Ryan Mallon89deb222014-01-09 19:14:07 +010085 term:
86 free(buf);
87 return 1; /* "terminate" */
Denis Vlasenko7a431b32007-01-14 01:29:06 +000088}
89
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000090static int do_timeout(void **paramp UNUSED_PARAM)
Denis Vlasenko7a431b32007-01-14 01:29:06 +000091{
92 return 1; /* terminate session */
93}
94
95static void inetd_mode(void)
96{
97 identd_buf_t *buf = xzalloc(sizeof(*buf));
Denis Vlasenkob8b81a62007-01-14 12:47:27 +000098 /* buf->pos = 0; - xzalloc did it */
Denis Vlasenko19250812007-01-14 12:07:25 +000099 do
100 alarm(TIMEOUT);
Denys Vlasenko604b7b62014-01-10 17:12:54 +0100101 /* Note: we do NOT want nonblocking I/O here! */
Denis Vlasenkob8b81a62007-01-14 12:47:27 +0000102 while (do_rd(0, (void*)&buf) == 0);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000103}
104
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000105int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000106int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000107{
108 enum {
109 OPT_foreground = 0x1,
110 OPT_inetd = 0x2,
111 OPT_inetdwait = 0x4,
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000112 OPT_fiw = 0x7,
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000113 OPT_bindaddr = 0x8,
114 };
115
116 const char *bind_address = NULL;
117 unsigned opt;
118 int fd;
119
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000120 opt = getopt32(argv, "fiwb:", &bind_address);
Denis Vlasenko04c99eb2007-04-07 00:44:31 +0000121 strcpy(bogouser, "nobody");
122 if (argv[optind])
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200123 strncpy(bogouser, argv[optind], sizeof_bogouser - 1);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000124
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000125 /* Daemonize if no -f and no -i and no -w */
Denis Vlasenko43d5d422008-05-15 19:44:46 +0000126 if (!(opt & OPT_fiw))
Denis Vlasenko5a142022007-03-26 13:20:54 +0000127 bb_daemonize_or_rexec(0, argv);
128
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000129 /* 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 Vlasenko5e4fda02009-03-08 23:46:48 +0000135 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000136 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 Vlasenkod97c9242007-01-14 13:12:06 +0000147 fd = 0;
148 if (!(opt & OPT_inetdwait)) {
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000149 fd = create_and_bind_stream_or_die(bind_address,
150 bb_lookup_port("identd", "tcp", 113));
151 xlisten(fd, 5);
152 }
153
Denis Vlasenko19250812007-01-14 12:07:25 +0000154 isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
155 TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000156 return 0;
157}