libbb: make bb_common_bufsiz1 1 kbyte, add capability to use bss tail for it
[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:     "\n        -f      Run in foreground"
15 //usage:     "\n        -i      Inetd mode"
16 //usage:     "\n        -w      Inetd 'wait' mode"
17 //usage:     "\n        -b ADDR Bind to specified address"
18 //usage:     "\n        STRING  Ident answer string (default: nobody)"
19
20 #include "libbb.h"
21 #include "common_bufsiz.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         char buf[64 - sizeof(int)];
30 } identd_buf_t;
31
32 #define        bogouser bb_common_bufsiz1
33 #define sizeof_bogouser COMMON_BUFSIZE
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         ndelay_on(fd);
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 sz;
56
57         cur = buf->buf + buf->pos;
58
59         sz = safe_read(fd, cur, sizeof(buf->buf) - 1 - buf->pos);
60
61         if (sz < 0) {
62                 if (errno != EAGAIN)
63                         goto term;
64                 return 0; /* "session is ok" */
65         }
66
67         buf->pos += sz;
68         buf->buf[buf->pos] = '\0';
69         p = strpbrk(cur, "\r\n");
70         if (p)
71                 *p = '\0';
72         if (!p && sz)
73                 return 0;  /* "session is ok" */
74
75         /* Terminate session. If we are in server mode, then
76          * fd is still in nonblocking mode - we never block here */
77         if (fd == 0)
78                 fd++; /* inetd mode? then write to fd 1 */
79         fdprintf(fd, "%s : USERID : UNIX : %s\r\n", buf->buf, bogouser);
80         /*
81          * Why bother if we are going to close fd now anyway?
82          * if (server)
83          *      ndelay_off(fd);
84          */
85  term:
86         free(buf);
87         return 1; /* "terminate" */
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         do
100                 alarm(TIMEOUT);
101                 /* Note: we do NOT want nonblocking I/O here! */
102         while (do_rd(0, (void*)&buf) == 0);
103 }
104
105 int fakeidentd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
106 int fakeidentd_main(int argc UNUSED_PARAM, char **argv)
107 {
108         enum {
109                 OPT_foreground = 0x1,
110                 OPT_inetd      = 0x2,
111                 OPT_inetdwait  = 0x4,
112                 OPT_fiw        = 0x7,
113                 OPT_bindaddr   = 0x8,
114         };
115
116         const char *bind_address = NULL;
117         unsigned opt;
118         int fd;
119
120         opt = getopt32(argv, "fiwb:", &bind_address);
121         strcpy(bogouser, "nobody");
122         if (argv[optind])
123                 strncpy(bogouser, argv[optind], sizeof_bogouser - 1);
124
125         /* Daemonize if no -f and no -i and no -w */
126         if (!(opt & OPT_fiw))
127                 bb_daemonize_or_rexec(0, argv);
128
129         /* Where to log in inetd modes? "Classic" inetd
130          * probably has its stderr /dev/null'ed (we need log to syslog?),
131          * but daemontools-like utilities usually expect that children
132          * log to stderr. I like daemontools more. Go their way.
133          * (Or maybe we need yet another option "log to syslog") */
134         if (!(opt & OPT_fiw) /* || (opt & OPT_syslog) */) {
135                 openlog(applet_name, LOG_PID, LOG_DAEMON);
136                 logmode = LOGMODE_SYSLOG;
137         }
138
139         if (opt & OPT_inetd) {
140                 inetd_mode();
141                 return 0;
142         }
143
144         /* Ignore closed connections when writing */
145         signal(SIGPIPE, SIG_IGN);
146
147         fd = 0;
148         if (!(opt & OPT_inetdwait)) {
149                 fd = create_and_bind_stream_or_die(bind_address,
150                                 bb_lookup_port("identd", "tcp", 113));
151                 xlisten(fd, 5);
152         }
153
154         isrv_run(fd, new_peer, do_rd, /*do_wr:*/ NULL, do_timeout,
155                         TIMEOUT, (opt & OPT_inetdwait) ? TIMEOUT : 0);
156         return 0;
157 }