b9481f8d3ea6c2d184acc07efb6587e63c4bdf2f
[oweals/busybox.git] / networking / isrv_identd.c
1 /* 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
14 enum { TIMEOUT = 20 };
15
16 /* Why use alarm(TIMEOUT-1)?
17  * isrv's internal select() will run with timeout=TIMEOUT.
18  * If nothing happens during TIMEOUT-1 seconds (no accept/read),
19  * then ALL sessions timed out by now. Instead of closing them one-by-one
20  * (isrv calls do_timeout for each 'stale' session),
21  * SIGALRM triggered by alarm(TIMEOUT-1) will kill us, terminating them all.
22  */
23
24 typedef struct identd_buf_t {
25         int pos;
26         int fd_flag;
27         char buf[64 - 2*sizeof(int)];
28 } identd_buf_t;
29
30 static const char *bogouser = "nobody";
31
32 static int new_peer(isrv_state_t *state, int fd)
33 {
34         int peer;
35         identd_buf_t *buf = xzalloc(sizeof(*buf));
36
37         alarm(TIMEOUT - 1);
38
39         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
45         buf->fd_flag = fcntl(fd, F_GETFL, 0) | O_NONBLOCK;
46         isrv_want_rd(state, fd);
47         return 0;
48 }
49
50 static int do_rd(int fd, void **paramp)
51 {
52         identd_buf_t *buf = *paramp;
53         char *cur, *p;
54         int sz;
55
56         alarm(TIMEOUT - 1);
57
58         cur = buf->buf + buf->pos;
59
60         fcntl(fd, F_SETFL, buf->fd_flag | O_NONBLOCK);
61         sz = safe_read(fd, cur, sizeof(buf->buf) - buf->pos);
62
63         if (sz < 0) {
64                 if (errno != EAGAIN)
65                         goto term; /* terminate this session if !EAGAIN */
66                 goto ok;
67         }
68
69         buf->pos += sz;
70         buf->buf[buf->pos] = '\0';
71         p = strpbrk(cur, "\r\n");
72         if (p)
73                 *p = '\0';
74         if (p || !sz || buf->pos == sizeof(buf->buf)) {
75                 /* fd is still in nonblocking mode - we never block here */
76                 fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
77                 goto term;
78         }
79  ok:
80         fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK);
81         return 0;
82  term:
83         fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK);
84         free(buf);
85         return 1;
86 }
87
88 static int do_timeout(void **paramp)
89 {
90         return 1; /* terminate session */
91 }
92
93 static void inetd_mode(void)
94 {
95         identd_buf_t *buf = xzalloc(sizeof(*buf));
96         /* We do NOT want nonblocking I/O here! */
97         buf->fd_flag = fcntl(0, F_GETFL, 0);
98         while (do_rd(0, (void*)&buf) == 0) /* repeat */;
99 }
100
101 int fakeidentd_main(int argc, char **argv)
102 {
103         enum {
104                 OPT_foreground = 0x1,
105                 OPT_inetd      = 0x2,
106                 OPT_inetdwait  = 0x4,
107                 OPT_nodeamon   = 0x7,
108                 OPT_bindaddr   = 0x8,
109         };
110
111         const char *bind_address = NULL;
112         unsigned opt;
113         int fd;
114
115         opt = getopt32(argc, argv, "fiwb:", &bind_address);
116         if (optind < argc)
117                 bogouser = argv[optind];
118
119         /* Daemonize if no -f or -i or -w */
120         bb_sanitize_stdio(!(opt & OPT_nodeamon));
121         if (!(opt & OPT_nodeamon)) {
122                 openlog(applet_name, 0, LOG_DAEMON);
123                 logmode = LOGMODE_SYSLOG;
124         }
125
126         if (opt & OPT_inetd) {
127                 inetd_mode();
128                 return 0;
129         }
130
131         /* Ignore closed connections when writing */
132         signal(SIGPIPE, SIG_IGN);
133
134         if (opt & OPT_inetdwait) {
135                 fd = 0;
136         } else {
137                 fd = create_and_bind_stream_or_die(bind_address,
138                                 bb_lookup_port("identd", "tcp", 113));
139                 xlisten(fd, 5);
140         }
141
142         isrv_run(fd, new_peer, do_rd, NULL, do_timeout, TIMEOUT, 1);
143         return 0;
144 }