1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "libbb.h"
32#include "common_bufsiz.h"
33#include <syslog.h>
34#include "isrv.h"
35
36enum { TIMEOUT = 20 };
37
38typedef struct identd_buf_t {
39 int pos;
40 char buf[64 - sizeof(int)];
41} identd_buf_t;
42
43#define bogouser bb_common_bufsiz1
44
45static int new_peer(isrv_state_t *state, int fd)
46{
47 int peer;
48 identd_buf_t *buf = xzalloc(sizeof(*buf));
49
50 peer = isrv_register_peer(state, buf);
51 if (peer < 0)
52 return 0;
53 if (isrv_register_fd(state, peer, fd) < 0)
54 return peer;
55
56 ndelay_on(fd);
57 isrv_want_rd(state, fd);
58 return 0;
59}
60
61static int do_rd(int fd, void **paramp)
62{
63 identd_buf_t *buf = *paramp;
64 char *cur, *p;
65 int sz;
66
67 cur = buf->buf + buf->pos;
68
69 sz = safe_read(fd, cur, sizeof(buf->buf) - 1 - buf->pos);
70
71 if (sz < 0) {
72 if (errno != EAGAIN)
73 goto term;
74 return 0;
75 }
76
77 buf->pos += sz;
78 buf->buf[buf->pos] = '\0';
79 p = strpbrk(cur, "\r\n");
80 if (p)
81 *p = '\0';
82 if (!p && sz)
83 return 0;
84
85
86
87 if (fd == 0)
88 fd++;
89 fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
90
91
92
93
94
95 term:
96 free(buf);
97 return 1;
98}
99
100static int do_timeout(void **paramp UNUSED_PARAM)
101{
102 return 1;
103}
104
105static void inetd_mode(void)
106{
107 identd_buf_t *buf = xzalloc(sizeof(*buf));
108
109 do
110 alarm(TIMEOUT);
111
112 while (do_rd(0, (void*)&buf) == 0);
113}
114
115int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
116int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
117{
118 enum {
119 OPT_foreground = 0x1,
120 OPT_inetd = 0x2,
121 OPT_inetdwait = 0x4,
122 OPT_fiw = 0x7,
123 OPT_bindaddr = 0x8,
124 };
125
126 const char *bind_address = NULL;
127 unsigned opt;
128 int fd;
129
130 setup_common_bufsiz();
131
132 opt = getopt32(argv, "fiwb:", &bind_address);
133 strcpy(bogouser, "nobody");
134 if (argv[optind])
135 strncpy(bogouser, argv[optind], COMMON_BUFSIZE - 1);
136
137
138 if (!(opt & OPT_fiw))
139 bb_daemonize_or_rexec(0, argv);
140
141
142
143
144
145
146 if (!(opt & OPT_fiw) ) {
147 openlog(applet_name, LOG_PID, LOG_DAEMON);
148 logmode = LOGMODE_SYSLOG;
149 }
150
151 if (opt & OPT_inetd) {
152 inetd_mode();
153 return 0;
154 }
155
156
157 signal(SIGPIPE, SIG_IGN);
158
159 fd = 0;
160 if (!(opt & OPT_inetdwait)) {
161 fd = create_and_bind_stream_or_die(bind_address,
162 bb_lookup_port("identd", "tcp", 113));
163 xlisten(fd, 5);
164 }
165
166 isrv_run(fd, new_peer, do_rd, NULL, do_timeout,
167 TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
168 return 0;
169}
170