199e112256f1ef8ae75c700c6463471477a8dfa9
[oweals/busybox.git] / networking / isrv_identd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Fake identd server.
4  *
5  * Copyright (C) 2007 Denys Vlasenko
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 //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"
14 //usage:     "\nOptions:"
15 //usage:     "\n        -f      Run in foreground"
16 //usage:     "\n        -i      Inetd mode"
17 //usage:     "\n        -w      Inetd 'wait' mode"
18 //usage:     "\n        -b ADDR Bind to specified address"
19 //usage:     "\n        STRING  Ident answer string (default: nobody)"
20
21 #include "libbb.h"
22 #include <syslog.h>
23 #include "isrv.h"
24
25 enum { TIMEOUT = 20 };
26
27 typedef struct identd_buf_t {
28         int pos;
29         int fd_flag;
30         char buf[64 - 2*sizeof(int)];
31 } identd_buf_t;
32
33 #define bogouser bb_common_bufsiz1
34
35 static int new_peer(isrv_state_t *state, int fd)
36 {
37         int peer;
38         identd_buf_t *buf = xzalloc(sizeof(*buf));
39
40         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
46         buf->fd_flag = fcntl(fd, F_GETFL) | O_NONBLOCK;
47         isrv_want_rd(state, fd);
48         return 0;
49 }
50
51 static int do_rd(int fd, void **paramp)
52 {
53         identd_buf_t *buf = *paramp;
54         char *cur, *p;
55         int retval = 0; /* session is ok (so far) */
56         int sz;
57
58         cur = buf->buf + buf->pos;
59
60         if (buf->fd_flag & O_NONBLOCK)
61                 fcntl(fd, F_SETFL, buf->fd_flag);
62         sz = safe_read(fd, cur, sizeof(buf->buf) - buf->pos);
63
64         if (sz < 0) {
65                 if (errno != EAGAIN)
66                         goto term; /* terminate this session if !EAGAIN */
67                 goto ok;
68         }
69
70         buf->pos += sz;
71         buf->buf[buf->pos] = '\0';
72         p = strpbrk(cur, "\r\n");
73         if (p)
74                 *p = '\0';
75         if (!p && sz && buf->pos <= (int)sizeof(buf->buf))
76                 goto ok;
77         /* Terminate session. If we are in server mode, then
78          * fd is still in nonblocking mode - we never block here */
79         if (fd == 0) fd++; /* inetd mode? then write to fd 1 */
80         fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
81  term:
82         free(buf);
83         retval = 1; /* terminate */
84  ok:
85         if (buf->fd_flag & O_NONBLOCK)
86                 fcntl(fd, F_SETFL, buf->fd_flag & ~O_NONBLOCK);
87         return retval;
88 }
89
90 static int do_timeout(void **paramp UNUSED_PARAM)
91 {
92         return 1; /* terminate session */
93 }
94
95 static void inetd_mode(void)
96 {
97         identd_buf_t *buf = xzalloc(sizeof(*buf));
98         /* buf->pos = 0; - xzalloc did it */
99         /* We do NOT want nonblocking I/O here! */
100         /* buf->fd_flag = 0; - xzalloc did it */
101         do
102                 alarm(TIMEOUT);
103         while (do_rd(0, (void*)&buf) == 0);
104 }
105
106 int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
107 int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
108 {
109         enum {
110                 OPT_foreground = 0x1,
111                 OPT_inetd      = 0x2,
112                 OPT_inetdwait  = 0x4,
113                 OPT_fiw        = 0x7,
114                 OPT_bindaddr   = 0x8,
115         };
116
117         const char *bind_address = NULL;
118         unsigned opt;
119         int fd;
120
121         opt = getopt32(argv, "fiwb:", &bind_address);
122         strcpy(bogouser, "nobody");
123         if (argv[optind])
124                 strncpy(bogouser, argv[optind], sizeof(bogouser));
125
126         /* Daemonize if no -f and no -i and no -w */
127         if (!(opt & OPT_fiw))
128                 bb_daemonize_or_rexec(0, argv);
129
130         /* Where to log in inetd modes? "Classic" inetd
131          * probably has its stderr /dev/null'ed (we need log to syslog?),
132          * but daemontools-like utilities usually expect that children
133          * log to stderr. I like daemontools more. Go their way.
134          * (Or maybe we need yet another option "log to syslog") */
135         if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
136                 openlog(applet_name, LOG_PID, LOG_DAEMON);
137                 logmode = LOGMODE_SYSLOG;
138         }
139
140         if (opt & OPT_inetd) {
141                 inetd_mode();
142                 return 0;
143         }
144
145         /* Ignore closed connections when writing */
146         signal(SIGPIPE, SIG_IGN);
147
148         fd = 0;
149         if (!(opt & OPT_inetdwait)) {
150                 fd = create_and_bind_stream_or_die(bind_address,
151                                 bb_lookup_port("identd", "tcp", 113));
152                 xlisten(fd, 5);
153         }
154
155         isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
156                         TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
157         return 0;
158 }