blob: c230c7ec30c20eab0ec6b699a7f9e7f527b0a007 [file] [log] [blame]
Denis Vlasenko7a431b32007-01-14 01:29:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Fake identd server.
4 *
5 * Copyright (C) 2007 Denis Vlasenko
6 *
7 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
8 */
9
10#include <syslog.h>
11#include "busybox.h"
12#include "isrv.h"
13
14enum { TIMEOUT = 20 };
15
Denis Vlasenko7a431b32007-01-14 01:29:06 +000016typedef struct identd_buf_t {
17 int pos;
18 int fd_flag;
19 char buf[64 - 2*sizeof(int)];
20} identd_buf_t;
21
22static const char *bogouser = "nobody";
23
24static int new_peer(isrv_state_t *state, int fd)
25{
26 int peer;
27 identd_buf_t *buf = xzalloc(sizeof(*buf));
28
Denis Vlasenko7a431b32007-01-14 01:29:06 +000029 peer = isrv_register_peer(state, buf);
30 if (peer < 0)
31 return 0; /* failure */
32 if (isrv_register_fd(state, peer, fd) < 0)
33 return peer; /* failure, unregister peer */
34
35 buf->fd_flag = fcntl(fd, F_GETFL, 0) | O_NONBLOCK;
36 isrv_want_rd(state, fd);
37 return 0;
38}
39
40static int do_rd(int fd, void **paramp)
41{
42 identd_buf_t *buf = *paramp;
43 char *cur, *p;
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000044 int retval = 0; /* session is ok (so far) */
Denis Vlasenko7a431b32007-01-14 01:29:06 +000045 int sz;
46
Denis Vlasenko7a431b32007-01-14 01:29:06 +000047 cur = buf->buf + buf->pos;
48
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000049 if (buf->fd_flag & O_NONBLOCK)
50 fcntl(fd, F_SETFL, buf->fd_flag);
Denis Vlasenko7a431b32007-01-14 01:29:06 +000051 sz = safe_read(fd, cur, sizeof(buf->buf) - buf->pos);
52
53 if (sz < 0) {
54 if (errno != EAGAIN)
55 goto term; /* terminate this session if !EAGAIN */
56 goto ok;
57 }
58
59 buf->pos += sz;
60 buf->buf[buf->pos] = '\0';
61 p = strpbrk(cur, "\r\n");
62 if (p)
63 *p = '\0';
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000064 if (!p && sz && buf->pos <= sizeof(buf->buf))
65 goto ok;
66 /* Terminate session. If we are in server mode, then
67 * fd is still in nonblocking mode - we never block here */
Denis Vlasenkod97c9242007-01-14 13:12:06 +000068 if (fd == 0) fd++; /* inetd mode? then write to fd 1 */
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000069 fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
Denis Vlasenko7a431b32007-01-14 01:29:06 +000070 term:
Denis Vlasenko7a431b32007-01-14 01:29:06 +000071 free(buf);
Denis Vlasenkoc14c95e2007-01-14 12:31:26 +000072 retval = 1; /* terminate */
73 ok:
74 if (buf->fd_flag & O_NONBLOCK)
75 fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK);
76 return retval;
Denis Vlasenko7a431b32007-01-14 01:29:06 +000077}
78
79static int do_timeout(void **paramp)
80{
81 return 1; /* terminate session */
82}
83
84static void inetd_mode(void)
85{
86 identd_buf_t *buf = xzalloc(sizeof(*buf));
Denis Vlasenkob8b81a62007-01-14 12:47:27 +000087 /* buf->pos = 0; - xzalloc did it */
Denis Vlasenko7a431b32007-01-14 01:29:06 +000088 /* We do NOT want nonblocking I/O here! */
Denis Vlasenkob8b81a62007-01-14 12:47:27 +000089 /* buf->fd_flag = 0; - xzalloc did it */
Denis Vlasenko19250812007-01-14 12:07:25 +000090 do
91 alarm(TIMEOUT);
Denis Vlasenkob8b81a62007-01-14 12:47:27 +000092 while (do_rd(0, (void*)&buf) == 0);
Denis Vlasenko7a431b32007-01-14 01:29:06 +000093}
94
Denis Vlasenko06af2162007-02-03 17:28:39 +000095int fakeidentd_main(int argc, char **argv);
Denis Vlasenko7a431b32007-01-14 01:29:06 +000096int fakeidentd_main(int argc, char **argv)
97{
98 enum {
99 OPT_foreground = 0x1,
100 OPT_inetd = 0x2,
101 OPT_inetdwait = 0x4,
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000102 OPT_fiw = 0x7,
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000103 OPT_bindaddr = 0x8,
104 };
105
106 const char *bind_address = NULL;
107 unsigned opt;
108 int fd;
109
110 opt = getopt32(argc, argv, "fiwb:", &bind_address);
111 if (optind < argc)
112 bogouser = argv[optind];
113
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000114 /* Daemonize if no -f and no -i and no -w */
Denis Vlasenko5a142022007-03-26 13:20:54 +0000115 if (!(opt & OPT_fiw));
116 bb_daemonize_or_rexec(0, argv);
117
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000118 /* Where to log in inetd modes? "Classic" inetd
119 * probably has its stderr /dev/null'ed (we need log to syslog?),
120 * but daemontools-like utilities usually expect that children
121 * log to stderr. I like daemontools more. Go their way.
122 * (Or maybe we need yet another option "log to syslog") */
123 if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000124 openlog(applet_name, 0, LOG_DAEMON);
125 logmode = LOGMODE_SYSLOG;
126 }
127
128 if (opt & OPT_inetd) {
129 inetd_mode();
130 return 0;
131 }
132
133 /* Ignore closed connections when writing */
134 signal(SIGPIPE, SIG_IGN);
135
Denis Vlasenkod97c9242007-01-14 13:12:06 +0000136 fd = 0;
137 if (!(opt & OPT_inetdwait)) {
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000138 fd = create_and_bind_stream_or_die(bind_address,
139 bb_lookup_port("identd", "tcp", 113));
140 xlisten(fd, 5);
141 }
142
Denis Vlasenko19250812007-01-14 12:07:25 +0000143 isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
144 TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
Denis Vlasenko7a431b32007-01-14 01:29:06 +0000145 return 0;
146}