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