diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-12 22:14:34 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-03-12 22:14:34 +0000 |
commit | 4e6d5117b839cef41cd3919966f95bf25d24d8d9 (patch) | |
tree | 277c292905fefcdf40530c8c29e87d6b8c5f7864 | |
parent | 4e6c8120a53709c4a352db42e997303844c74584 (diff) | |
download | busybox-w32-4e6d5117b839cef41cd3919966f95bf25d24d8d9.tar.gz busybox-w32-4e6d5117b839cef41cd3919966f95bf25d24d8d9.tar.bz2 busybox-w32-4e6d5117b839cef41cd3919966f95bf25d24d8d9.zip |
inetd: make it NOMMU-capable and IPv6-friendly. Lots of renames
of variable/function names
Total: -2474 bytes
text data bss dec hex filename
802215 661 7452 810328 c5d58 busybox_old
800120 661 7428 808209 c5511 busybox_unstripped
-rw-r--r-- | TODO_config_nommu | 16 | ||||
-rw-r--r-- | include/libbb.h | 17 | ||||
-rw-r--r-- | include/usage.h | 18 | ||||
-rw-r--r-- | libbb/create_icmp6_socket.c | 13 | ||||
-rw-r--r-- | libbb/create_icmp_socket.c | 13 | ||||
-rw-r--r-- | libbb/xconnect.c | 21 | ||||
-rw-r--r-- | networking/inetd.c | 1774 | ||||
-rw-r--r-- | networking/nc_bloaty.c | 2 | ||||
-rw-r--r-- | networking/tftp.c | 2 | ||||
-rw-r--r-- | runit/chpst.c | 4 |
10 files changed, 829 insertions, 1051 deletions
diff --git a/TODO_config_nommu b/TODO_config_nommu index 7b7c1690f..b0221bbc3 100644 --- a/TODO_config_nommu +++ b/TODO_config_nommu | |||
@@ -1,7 +1,7 @@ | |||
1 | # | 1 | # |
2 | # Automatically generated make config: don't edit | 2 | # Automatically generated make config: don't edit |
3 | # Busybox version: 1.10.0.svn | 3 | # Busybox version: 1.10.0.svn |
4 | # Tue Mar 4 22:57:04 2008 | 4 | # Thu Mar 6 20:18:05 2008 |
5 | # | 5 | # |
6 | CONFIG_HAVE_DOT_CONFIG=y | 6 | CONFIG_HAVE_DOT_CONFIG=y |
7 | 7 | ||
@@ -653,13 +653,13 @@ CONFIG_IFUPDOWN_IFSTATE_PATH="" | |||
653 | # CONFIG_FEATURE_IFUPDOWN_IPV6 is not set | 653 | # CONFIG_FEATURE_IFUPDOWN_IPV6 is not set |
654 | # CONFIG_FEATURE_IFUPDOWN_MAPPING is not set | 654 | # CONFIG_FEATURE_IFUPDOWN_MAPPING is not set |
655 | # CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP is not set | 655 | # CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP is not set |
656 | # CONFIG_INETD is not set | 656 | CONFIG_INETD=y |
657 | # CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO is not set | 657 | CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO=y |
658 | # CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD is not set | 658 | CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD=y |
659 | # CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME is not set | 659 | CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_TIME=y |
660 | # CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME is not set | 660 | CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME=y |
661 | # CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN is not set | 661 | CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN=y |
662 | # CONFIG_FEATURE_INETD_RPC is not set | 662 | CONFIG_FEATURE_INETD_RPC=y |
663 | CONFIG_IP=y | 663 | CONFIG_IP=y |
664 | CONFIG_FEATURE_IP_ADDRESS=y | 664 | CONFIG_FEATURE_IP_ADDRESS=y |
665 | CONFIG_FEATURE_IP_LINK=y | 665 | CONFIG_FEATURE_IP_LINK=y |
diff --git a/include/libbb.h b/include/libbb.h index 6e10cdc41..3c53e5e68 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -390,6 +390,7 @@ typedef struct len_and_sockaddr { | |||
390 | } u; | 390 | } u; |
391 | } len_and_sockaddr; | 391 | } len_and_sockaddr; |
392 | enum { | 392 | enum { |
393 | LSA_LEN_SIZE = offsetof(len_and_sockaddr, u), | ||
393 | LSA_SIZEOF_SA = sizeof( | 394 | LSA_SIZEOF_SA = sizeof( |
394 | union { | 395 | union { |
395 | struct sockaddr sa; | 396 | struct sockaddr sa; |
@@ -405,7 +406,12 @@ enum { | |||
405 | * af == AF_UNSPEC will result in trying to create IPv6 socket, | 406 | * af == AF_UNSPEC will result in trying to create IPv6 socket, |
406 | * and if kernel doesn't support it, IPv4. | 407 | * and if kernel doesn't support it, IPv4. |
407 | */ | 408 | */ |
408 | int xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int af,) int sock_type); | 409 | #if ENABLE_FEATURE_IPV6 |
410 | int xsocket_type(len_and_sockaddr **lsap, int af, int sock_type); | ||
411 | #else | ||
412 | int xsocket_type(len_and_sockaddr **lsap, int sock_type); | ||
413 | #define xsocket_type(lsap, af, sock_type) xsocket_type((lsap), (sock_type)) | ||
414 | #endif | ||
409 | int xsocket_stream(len_and_sockaddr **lsap); | 415 | int xsocket_stream(len_and_sockaddr **lsap); |
410 | /* Create server socket bound to bindaddr:port. bindaddr can be NULL, | 416 | /* Create server socket bound to bindaddr:port. bindaddr can be NULL, |
411 | * numeric IP ("N.N.N.N") or numeric IPv6 address, | 417 | * numeric IP ("N.N.N.N") or numeric IPv6 address, |
@@ -430,14 +436,13 @@ len_and_sockaddr* host2sockaddr(const char *host, int port); | |||
430 | /* Version which dies on error */ | 436 | /* Version which dies on error */ |
431 | len_and_sockaddr* xhost2sockaddr(const char *host, int port); | 437 | len_and_sockaddr* xhost2sockaddr(const char *host, int port); |
432 | len_and_sockaddr* xdotted2sockaddr(const char *host, int port); | 438 | len_and_sockaddr* xdotted2sockaddr(const char *host, int port); |
433 | #if ENABLE_FEATURE_IPV6 | ||
434 | /* Same, useful if you want to force family (e.g. IPv6) */ | 439 | /* Same, useful if you want to force family (e.g. IPv6) */ |
440 | #if !ENABLE_FEATURE_IPV6 | ||
441 | #define host_and_af2sockaddr(host, port, af) host2sockaddr((host), (port)) | ||
442 | #define xhost_and_af2sockaddr(host, port, af) xhost2sockaddr((host), (port)) | ||
443 | #else | ||
435 | len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af); | 444 | len_and_sockaddr* host_and_af2sockaddr(const char *host, int port, sa_family_t af); |
436 | len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af); | 445 | len_and_sockaddr* xhost_and_af2sockaddr(const char *host, int port, sa_family_t af); |
437 | #else | ||
438 | /* [we evaluate af: think about "host_and_af2sockaddr(..., af++)"] */ | ||
439 | #define host_and_af2sockaddr(host, port, af) ((void)(af), host2sockaddr((host), (port))) | ||
440 | #define xhost_and_af2sockaddr(host, port, af) ((void)(af), xhost2sockaddr((host), (port))) | ||
441 | #endif | 446 | #endif |
442 | /* Assign sin[6]_port member if the socket is an AF_INET[6] one, | 447 | /* Assign sin[6]_port member if the socket is an AF_INET[6] one, |
443 | * otherwise no-op. Useful for ftp. | 448 | * otherwise no-op. Useful for ftp. |
diff --git a/include/usage.h b/include/usage.h index a6d970de6..76beb176f 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -1597,7 +1597,7 @@ USE_FEATURE_BRCTL_FANCY("\n" \ | |||
1597 | " -a De/configure all interfaces automatically\n" \ | 1597 | " -a De/configure all interfaces automatically\n" \ |
1598 | " -i FILE Use FILE for interface definitions\n" \ | 1598 | " -i FILE Use FILE for interface definitions\n" \ |
1599 | " -n Print out what would happen, but don't do it\n" \ | 1599 | " -n Print out what would happen, but don't do it\n" \ |
1600 | " (note that this option doesn't disable mappings)\n" \ | 1600 | " (note: doesn't disable mappings)\n" \ |
1601 | " -v Print out what would happen before doing it\n" \ | 1601 | " -v Print out what would happen before doing it\n" \ |
1602 | " -m Don't run any mappings\n" \ | 1602 | " -m Don't run any mappings\n" \ |
1603 | " -f Force de/configuration" | 1603 | " -f Force de/configuration" |
@@ -1609,19 +1609,21 @@ USE_FEATURE_BRCTL_FANCY("\n" \ | |||
1609 | " -a De/configure all interfaces automatically\n" \ | 1609 | " -a De/configure all interfaces automatically\n" \ |
1610 | " -i FILE Use FILE for interface definitions\n" \ | 1610 | " -i FILE Use FILE for interface definitions\n" \ |
1611 | " -n Print out what would happen, but don't do it\n" \ | 1611 | " -n Print out what would happen, but don't do it\n" \ |
1612 | " (note that this option doesn't disable mappings)\n" \ | 1612 | " (note: doesn't disable mappings)\n" \ |
1613 | " -v Print out what would happen before doing it\n" \ | 1613 | " -v Print out what would happen before doing it\n" \ |
1614 | " -m Don't run any mappings\n" \ | 1614 | " -m Don't run any mappings\n" \ |
1615 | " -f Force de/configuration" | 1615 | " -f Force de/configuration" |
1616 | 1616 | ||
1617 | #define inetd_trivial_usage \ | 1617 | #define inetd_trivial_usage \ |
1618 | "[-f] [-q len] [conf]" | 1618 | "[-fe] [-q N] [-R N] [CONFFILE]" |
1619 | #define inetd_full_usage \ | 1619 | #define inetd_full_usage \ |
1620 | "Listen for network connections and launch programs" \ | 1620 | "Listen for network connections and launch programs\n" \ |
1621 | "\n\nOptions:\n" \ | 1621 | "\nOptions:" \ |
1622 | " -f Run in foreground\n" \ | 1622 | "\n -f Run in foreground" \ |
1623 | " -q N Set the size of the socket listen queue to N\n" \ | 1623 | "\n -e Log to stderr" \ |
1624 | " (default: 128)" | 1624 | "\n -q N Socket listen queue (default: 128)" \ |
1625 | "\n -R N Pause services after N connects/min" \ | ||
1626 | "\n (default: 0 - disabled)" | ||
1625 | 1627 | ||
1626 | #define init_trivial_usage \ | 1628 | #define init_trivial_usage \ |
1627 | "" | 1629 | "" |
diff --git a/libbb/create_icmp6_socket.c b/libbb/create_icmp6_socket.c index 51899cdc5..a22ac5d02 100644 --- a/libbb/create_icmp6_socket.c +++ b/libbb/create_icmp6_socket.c | |||
@@ -2,27 +2,26 @@ | |||
2 | /* | 2 | /* |
3 | * Utility routines. | 3 | * Utility routines. |
4 | * | 4 | * |
5 | * create raw socket for icmp (IPv6 version) protocol test permission | 5 | * create raw socket for icmp (IPv6 version) protocol |
6 | * and drop root privileges if running setuid | 6 | * and drop root privileges if running setuid |
7 | * | ||
8 | */ | 7 | */ |
9 | 8 | ||
10 | //#include <sys/types.h> | ||
11 | //#include <netdb.h> | ||
12 | //#include <sys/socket.h> | ||
13 | #include "libbb.h" | 9 | #include "libbb.h" |
14 | 10 | ||
15 | #if ENABLE_FEATURE_IPV6 | 11 | #if ENABLE_FEATURE_IPV6 |
16 | int create_icmp6_socket(void) | 12 | int create_icmp6_socket(void) |
17 | { | 13 | { |
18 | struct protoent *proto; | ||
19 | int sock; | 14 | int sock; |
20 | 15 | #if 0 | |
16 | struct protoent *proto; | ||
21 | proto = getprotobyname("ipv6-icmp"); | 17 | proto = getprotobyname("ipv6-icmp"); |
22 | /* if getprotobyname failed, just silently force | 18 | /* if getprotobyname failed, just silently force |
23 | * proto->p_proto to have the correct value for "ipv6-icmp" */ | 19 | * proto->p_proto to have the correct value for "ipv6-icmp" */ |
24 | sock = socket(AF_INET6, SOCK_RAW, | 20 | sock = socket(AF_INET6, SOCK_RAW, |
25 | (proto ? proto->p_proto : IPPROTO_ICMPV6)); | 21 | (proto ? proto->p_proto : IPPROTO_ICMPV6)); |
22 | #else | ||
23 | sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); | ||
24 | #endif | ||
26 | if (sock < 0) { | 25 | if (sock < 0) { |
27 | if (errno == EPERM) | 26 | if (errno == EPERM) |
28 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); | 27 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
diff --git a/libbb/create_icmp_socket.c b/libbb/create_icmp_socket.c index 3a4655885..64beba82b 100644 --- a/libbb/create_icmp_socket.c +++ b/libbb/create_icmp_socket.c | |||
@@ -2,26 +2,25 @@ | |||
2 | /* | 2 | /* |
3 | * Utility routines. | 3 | * Utility routines. |
4 | * | 4 | * |
5 | * create raw socket for icmp protocol test permission | 5 | * create raw socket for icmp protocol |
6 | * and drop root privileges if running setuid | 6 | * and drop root privileges if running setuid |
7 | * | ||
8 | */ | 7 | */ |
9 | 8 | ||
10 | //#include <sys/types.h> | ||
11 | //#include <netdb.h> | ||
12 | //#include <sys/socket.h> | ||
13 | #include "libbb.h" | 9 | #include "libbb.h" |
14 | 10 | ||
15 | int create_icmp_socket(void) | 11 | int create_icmp_socket(void) |
16 | { | 12 | { |
17 | struct protoent *proto; | ||
18 | int sock; | 13 | int sock; |
19 | 14 | #if 0 | |
15 | struct protoent *proto; | ||
20 | proto = getprotobyname("icmp"); | 16 | proto = getprotobyname("icmp"); |
21 | /* if getprotobyname failed, just silently force | 17 | /* if getprotobyname failed, just silently force |
22 | * proto->p_proto to have the correct value for "icmp" */ | 18 | * proto->p_proto to have the correct value for "icmp" */ |
23 | sock = socket(AF_INET, SOCK_RAW, | 19 | sock = socket(AF_INET, SOCK_RAW, |
24 | (proto ? proto->p_proto : 1)); /* 1 == ICMP */ | 20 | (proto ? proto->p_proto : 1)); /* 1 == ICMP */ |
21 | #else | ||
22 | sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */ | ||
23 | #endif | ||
25 | if (sock < 0) { | 24 | if (sock < 0) { |
26 | if (errno == EPERM) | 25 | if (errno == EPERM) |
27 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); | 26 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 03ae77329..950aee826 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c | |||
@@ -132,11 +132,15 @@ USE_FEATURE_IPV6(sa_family_t af,) | |||
132 | 132 | ||
133 | /* Ugly parsing of host:addr */ | 133 | /* Ugly parsing of host:addr */ |
134 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { | 134 | if (ENABLE_FEATURE_IPV6 && host[0] == '[') { |
135 | /* Even uglier parsing of [xx]:nn */ | ||
135 | host++; | 136 | host++; |
136 | cp = strchr(host, ']'); | 137 | cp = strchr(host, ']'); |
137 | if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */ | 138 | if (!cp || cp[1] != ':') { /* Malformed: must have [xx]:nn */ |
138 | bb_error_msg_and_die("bad address '%s'", org_host); | 139 | bb_error_msg("bad address '%s'", org_host); |
139 | //return r; /* return NULL */ | 140 | if (ai_flags & DIE_ON_ERROR) |
141 | xfunc_die(); | ||
142 | return NULL; | ||
143 | } | ||
140 | } else { | 144 | } else { |
141 | cp = strrchr(host, ':'); | 145 | cp = strrchr(host, ':'); |
142 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { | 146 | if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) { |
@@ -144,13 +148,19 @@ USE_FEATURE_IPV6(sa_family_t af,) | |||
144 | cp = NULL; /* it's not a port spec */ | 148 | cp = NULL; /* it's not a port spec */ |
145 | } | 149 | } |
146 | } | 150 | } |
147 | if (cp) { | 151 | if (cp) { /* points to ":" or "]:" */ |
148 | int sz = cp - host + 1; | 152 | int sz = cp - host + 1; |
149 | host = safe_strncpy(alloca(sz), host, sz); | 153 | host = safe_strncpy(alloca(sz), host, sz); |
150 | if (ENABLE_FEATURE_IPV6 && *cp != ':') | 154 | if (ENABLE_FEATURE_IPV6 && *cp != ':') |
151 | cp++; /* skip ']' */ | 155 | cp++; /* skip ']' */ |
152 | cp++; /* skip ':' */ | 156 | cp++; /* skip ':' */ |
153 | port = xatou16(cp); | 157 | port = bb_strtou(cp, NULL, 10); |
158 | if (errno || (unsigned)port > 0xffff) { | ||
159 | bb_error_msg("bad port spec '%s'", org_host); | ||
160 | if (ai_flags & DIE_ON_ERROR) | ||
161 | xfunc_die(); | ||
162 | return NULL; | ||
163 | } | ||
154 | } | 164 | } |
155 | 165 | ||
156 | memset(&hint, 0 , sizeof(hint)); | 166 | memset(&hint, 0 , sizeof(hint)); |
@@ -221,6 +231,7 @@ len_and_sockaddr* xdotted2sockaddr(const char *host, int port) | |||
221 | return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR); | 231 | return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR); |
222 | } | 232 | } |
223 | 233 | ||
234 | #undef xsocket_type | ||
224 | int xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type) | 235 | int xsocket_type(len_and_sockaddr **lsap, USE_FEATURE_IPV6(int family,) int sock_type) |
225 | { | 236 | { |
226 | SKIP_FEATURE_IPV6(enum { family = AF_INET };) | 237 | SKIP_FEATURE_IPV6(enum { family = AF_INET };) |
diff --git a/networking/inetd.c b/networking/inetd.c index fd865efd1..8a4c9fbf6 100644 --- a/networking/inetd.c +++ b/networking/inetd.c | |||
@@ -63,6 +63,7 @@ | |||
63 | * service name must be in /etc/services | 63 | * service name must be in /etc/services |
64 | * socket type stream/dgram/raw/rdm/seqpacket | 64 | * socket type stream/dgram/raw/rdm/seqpacket |
65 | * protocol must be in /etc/protocols | 65 | * protocol must be in /etc/protocols |
66 | * (usually "tcp" or "udp") | ||
66 | * wait/nowait[.max] single-threaded/multi-threaded, max # | 67 | * wait/nowait[.max] single-threaded/multi-threaded, max # |
67 | * user[.group] or user[:group] user/group to run daemon as | 68 | * user[.group] or user[:group] user/group to run daemon as |
68 | * server program full path name | 69 | * server program full path name |
@@ -71,7 +72,7 @@ | |||
71 | * For RPC services | 72 | * For RPC services |
72 | * service name/version must be in /etc/rpc | 73 | * service name/version must be in /etc/rpc |
73 | * socket type stream/dgram/raw/rdm/seqpacket | 74 | * socket type stream/dgram/raw/rdm/seqpacket |
74 | * protocol must be in /etc/protocols | 75 | * rpc/protocol "rpc/tcp" etc |
75 | * wait/nowait[.max] single-threaded/multi-threaded | 76 | * wait/nowait[.max] single-threaded/multi-threaded |
76 | * user[.group] or user[:group] user to run daemon as | 77 | * user[.group] or user[:group] user to run daemon as |
77 | * server program full path name | 78 | * server program full path name |
@@ -120,7 +121,7 @@ | |||
120 | * It should fork, and the parent should then exit to allow inetd to check | 121 | * It should fork, and the parent should then exit to allow inetd to check |
121 | * for new service requests to spawn new servers. Datagram servers which | 122 | * for new service requests to spawn new servers. Datagram servers which |
122 | * process all incoming datagrams on a socket and eventually time out are | 123 | * process all incoming datagrams on a socket and eventually time out are |
123 | * said to be "single-threaded". The comsat(8), (biff(1)) and talkd(8) | 124 | * said to be "single-threaded". The comsat(8), biff(1) and talkd(8) |
124 | * utilities are both examples of the latter type of datagram server. The | 125 | * utilities are both examples of the latter type of datagram server. The |
125 | * tftpd(8) utility is an example of a multi-threaded datagram server. | 126 | * tftpd(8) utility is an example of a multi-threaded datagram server. |
126 | * | 127 | * |
@@ -134,38 +135,33 @@ | |||
134 | * connection requests until a timeout. | 135 | * connection requests until a timeout. |
135 | */ | 136 | */ |
136 | 137 | ||
137 | /* Here's the scoop concerning the user[.:]group feature: | 138 | /* Here's the scoop concerning the user[:group] feature: |
138 | * | 139 | * 1) group is not specified: |
139 | * 1) set-group-option off. | ||
140 | * | ||
141 | * a) user = root: NO setuid() or setgid() is done | 140 | * a) user = root: NO setuid() or setgid() is done |
142 | * | ||
143 | * b) other: setgid(primary group as found in passwd) | 141 | * b) other: setgid(primary group as found in passwd) |
144 | * initgroups(name, primary group) | 142 | * initgroups(name, primary group) |
145 | * setuid() | 143 | * setuid() |
146 | * | 144 | * 2) group is specified: |
147 | * 2) set-group-option on. | ||
148 | * | ||
149 | * a) user = root: setgid(specified group) | 145 | * a) user = root: setgid(specified group) |
150 | * NO initgroups() | 146 | * NO initgroups() |
151 | * NO setuid() | 147 | * NO setuid() |
152 | * | ||
153 | * b) other: setgid(specified group) | 148 | * b) other: setgid(specified group) |
154 | * initgroups(name, specified group) | 149 | * initgroups(name, specified group) |
155 | * setuid() | 150 | * setuid() |
156 | */ | 151 | */ |
157 | 152 | ||
158 | #include "libbb.h" | ||
159 | #include <syslog.h> | 153 | #include <syslog.h> |
160 | #include <sys/un.h> | 154 | #include <sys/un.h> |
155 | #include "libbb.h" | ||
161 | 156 | ||
162 | //#define ENABLE_FEATURE_INETD_RPC 1 | 157 | #if !BB_MMU |
163 | //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO 1 | 158 | /* stream versions of these builtins are forking, |
164 | //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD 1 | 159 | * can't do that (easily) on NOMMU */ |
165 | //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME 1 | 160 | #undef ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD |
166 | //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME 1 | 161 | #define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD 0 |
167 | //#define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN 1 | 162 | #undef ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
168 | //#define ENABLE_FEATURE_IPV6 1 | 163 | #define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN 0 |
164 | #endif | ||
169 | 165 | ||
170 | #if ENABLE_FEATURE_INETD_RPC | 166 | #if ENABLE_FEATURE_INETD_RPC |
171 | #include <rpc/rpc.h> | 167 | #include <rpc/rpc.h> |
@@ -174,7 +170,7 @@ | |||
174 | 170 | ||
175 | #define _PATH_INETDPID "/var/run/inetd.pid" | 171 | #define _PATH_INETDPID "/var/run/inetd.pid" |
176 | 172 | ||
177 | #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */ | 173 | #define CNT_INTERVAL 60 /* servers in CNT_INTERVAL sec. */ |
178 | #define RETRYTIME (60*10) /* retry after bind or server fail */ | 174 | #define RETRYTIME (60*10) /* retry after bind or server fail */ |
179 | 175 | ||
180 | #ifndef RLIMIT_NOFILE | 176 | #ifndef RLIMIT_NOFILE |
@@ -188,77 +184,53 @@ | |||
188 | /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */ | 184 | /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */ |
189 | #define FD_MARGIN 8 | 185 | #define FD_MARGIN 8 |
190 | 186 | ||
191 | /* Check unsupporting builtin */ | 187 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD \ |
192 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \ | 188 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO \ |
193 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \ | 189 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN \ |
194 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME || \ | 190 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME \ |
195 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME || \ | 191 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME |
196 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN | 192 | # define INETD_BUILTINS_ENABLED |
197 | # define INETD_FEATURE_ENABLED | ||
198 | #endif | ||
199 | |||
200 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \ | ||
201 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD || \ | ||
202 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN | ||
203 | # define INETD_SETPROCTITLE | ||
204 | #endif | 193 | #endif |
205 | 194 | ||
206 | typedef struct servtab_t { | 195 | typedef struct servtab_t { |
207 | /* The most frequently referenced one: */ | 196 | /* The most frequently referenced one: */ |
208 | int se_fd; /* open descriptor */ | 197 | int se_fd; /* open descriptor */ |
209 | /* NB: 'biggest fields last' saves on code size (~250 bytes) */ | 198 | /* NB: 'biggest fields last' saves on code size (~250 bytes) */ |
210 | char *se_hostaddr; /* host address to listen on */ | 199 | /* [addr:]service socktype proto wait user[:group] prog [args] */ |
211 | char *se_service; /* name of service */ | 200 | char *se_local_hostname; /* addr to listen on */ |
212 | char *se_proto; /* protocol used */ | 201 | char *se_service; /* "80" or "www" or "mount/2[-3]" */ |
202 | /* socktype is in se_socktype */ /* "stream" "dgram" "raw" "rdm" "seqpacket" */ | ||
203 | char *se_proto; /* "unix" or "[rpc/]tcp[6]" */ | ||
213 | #if ENABLE_FEATURE_INETD_RPC | 204 | #if ENABLE_FEATURE_INETD_RPC |
214 | int se_rpcprog; /* rpc program number */ | 205 | int se_rpcprog; /* rpc program number */ |
215 | int se_rpcversl; /* rpc program lowest version */ | 206 | int se_rpcver_lo; /* rpc program lowest version */ |
216 | int se_rpcversh; /* rpc program highest version */ | 207 | int se_rpcver_hi; /* rpc program highest version */ |
217 | #define isrpcservice(sep) ((sep)->se_rpcversl != 0) | 208 | #define is_rpc_service(sep) ((sep)->se_rpcver_lo != 0) |
218 | #else | 209 | #else |
219 | #define isrpcservice(sep) 0 | 210 | #define is_rpc_service(sep) 0 |
220 | #endif | 211 | #endif |
221 | pid_t se_wait; /* single threaded server */ | 212 | pid_t se_wait; /* 0:"nowait", 1:"wait" */ |
222 | socktype_t se_socktype; /* type of socket to use */ | 213 | socktype_t se_socktype; /* SOCK_STREAM/DGRAM/RDM/... */ |
223 | family_t se_family; /* address family */ | 214 | family_t se_family; /* AF_UNIX/INET[6] */ |
215 | smallint se_proto_no; /* almost "getprotobyname(se_proto)" */ | ||
224 | smallint se_checked; /* looked at during merge */ | 216 | smallint se_checked; /* looked at during merge */ |
225 | char *se_user; /* user name to run as */ | 217 | char *se_user; /* user name to run as */ |
226 | char *se_group; /* group name to run as */ | 218 | char *se_group; /* group name to run as, can be NULL */ |
227 | #ifdef INETD_FEATURE_ENABLED | 219 | #ifdef INETD_BUILTINS_ENABLED |
228 | const struct builtin *se_bi; /* if built-in, description */ | 220 | const struct builtin *se_builtin; /* if built-in, description */ |
229 | #endif | 221 | #endif |
230 | int se_ctrladdr_size; | 222 | // TODO: wrong algorithm!!! |
231 | int se_max; /* max # of instances of this service */ | 223 | unsigned se_max; /* max # of instances of this service */ |
232 | int se_count; /* number started since se_time */ | 224 | unsigned se_count; /* number started since se_time */ |
225 | unsigned se_time; /* start of se_count */ | ||
233 | struct servtab_t *se_next; | 226 | struct servtab_t *se_next; |
234 | struct timeval se_time; /* start of se_count */ | 227 | len_and_sockaddr *se_lsa; |
235 | char *se_server; /* server program */ | 228 | char *se_program; /* server program */ |
236 | #define MAXARGV 20 | 229 | #define MAXARGV 20 |
237 | char *se_argv[MAXARGV + 1]; /* program arguments */ | 230 | char *se_argv[MAXARGV + 1]; /* program arguments */ |
238 | union { | ||
239 | struct sockaddr se_un_ctrladdr; | ||
240 | struct sockaddr_in se_un_ctrladdr_in; | ||
241 | #if ENABLE_FEATURE_IPV6 | ||
242 | struct sockaddr_in6 se_un_ctrladdr_in6; | ||
243 | #endif | ||
244 | struct sockaddr_un se_un_ctrladdr_un; | ||
245 | } se_un; /* bound address */ | ||
246 | #define se_ctrladdr se_un.se_un_ctrladdr | ||
247 | #define se_ctrladdr_in se_un.se_un_ctrladdr_in | ||
248 | #define se_ctrladdr_in6 se_un.se_un_ctrladdr_in6 | ||
249 | #define se_ctrladdr_un se_un.se_un_ctrladdr_un | ||
250 | } servtab_t; | 231 | } servtab_t; |
251 | 232 | ||
252 | #ifdef INETD_FEATURE_ENABLED | 233 | #ifdef INETD_BUILTINS_ENABLED |
253 | struct builtin { | ||
254 | const char *bi_service; /* internally provided service name */ | ||
255 | socktype_t bi_socktype; /* type of socket supported */ | ||
256 | uint8_t bi_fork; /* 1 if should fork before call */ | ||
257 | // Commented since it is always 0 | ||
258 | // uint8_t bi_wait; /* 1 if should wait for child */ | ||
259 | void (*bi_fn) (int, servtab_t *); | ||
260 | }; | ||
261 | |||
262 | /* Echo received data */ | 234 | /* Echo received data */ |
263 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO | 235 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO |
264 | static void echo_stream(int, servtab_t *); | 236 | static void echo_stream(int, servtab_t *); |
@@ -285,62 +257,61 @@ static void chargen_stream(int, servtab_t *); | |||
285 | static void chargen_dg(int, servtab_t *); | 257 | static void chargen_dg(int, servtab_t *); |
286 | #endif | 258 | #endif |
287 | 259 | ||
260 | struct builtin { | ||
261 | const char *bi_service; /* internally provided service name */ | ||
262 | uint8_t bi_fork; /* 1 if stream fn should run in child */ | ||
263 | /* All builtins are "nowait" */ | ||
264 | /* uint8_t bi_wait; */ /* 1 if should wait for child */ | ||
265 | void (*bi_stream_fn)(int, servtab_t *); | ||
266 | void (*bi_dgram_fn)(int, servtab_t *); | ||
267 | }; | ||
268 | |||
288 | static const struct builtin builtins[] = { | 269 | static const struct builtin builtins[] = { |
289 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO | 270 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO |
290 | /* Echo received data */ | 271 | /* Echo received data */ |
291 | {"echo", SOCK_STREAM, 1, echo_stream,}, | 272 | { "echo", 1, echo_stream, echo_dg }, |
292 | {"echo", SOCK_DGRAM, 0, echo_dg,}, | ||
293 | #endif | 273 | #endif |
294 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD | 274 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD |
295 | /* Internet /dev/null */ | 275 | /* Internet /dev/null */ |
296 | {"discard", SOCK_STREAM, 1, discard_stream,}, | 276 | { "discard", 1, discard_stream, discard_dg }, |
297 | {"discard", SOCK_DGRAM, 0, discard_dg,}, | 277 | #endif |
278 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN | ||
279 | /* Familiar character generator */ | ||
280 | { "chargen", 1, chargen_stream, chargen_dg }, | ||
298 | #endif | 281 | #endif |
299 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME | 282 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME |
300 | /* Return 32 bit time since 1900 */ | 283 | /* Return 32 bit time since 1900 */ |
301 | {"time", SOCK_STREAM, 0, machtime_stream,}, | 284 | { "time", 0, machtime_stream, machtime_dg }, |
302 | {"time", SOCK_DGRAM, 0, machtime_dg,}, | ||
303 | #endif | 285 | #endif |
304 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME | 286 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME |
305 | /* Return human-readable time */ | 287 | /* Return human-readable time */ |
306 | {"daytime", SOCK_STREAM, 0, daytime_stream,}, | 288 | { "daytime", 0, daytime_stream, daytime_dg }, |
307 | {"daytime", SOCK_DGRAM, 0, daytime_dg,}, | ||
308 | #endif | ||
309 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN | ||
310 | /* Familiar character generator */ | ||
311 | {"chargen", SOCK_STREAM, 1, chargen_stream,}, | ||
312 | {"chargen", SOCK_DGRAM, 0, chargen_dg,}, | ||
313 | #endif | 289 | #endif |
314 | { /* zero filled */ } | ||
315 | }; | 290 | }; |
316 | #endif /* INETD_FEATURE_ENABLED */ | 291 | #endif /* INETD_BUILTINS_ENABLED */ |
317 | 292 | ||
318 | struct globals { | 293 | struct globals { |
319 | rlim_t rlim_ofile_cur; | 294 | rlim_t rlim_ofile_cur; |
320 | struct rlimit rlim_ofile; | 295 | struct rlimit rlim_ofile; |
321 | servtab_t *servtab; | 296 | servtab_t *serv_list; |
322 | int global_queuelen; | 297 | int global_queuelen; |
323 | int nsock; | 298 | int prev_maxsock; |
324 | int maxsock; | 299 | int maxsock; |
325 | int toomany; | 300 | unsigned max_concurrency; |
326 | int timingout; | 301 | smallint alarm_armed; |
327 | struct servent *sp; | 302 | uid_t real_uid; /* user ID who ran us */ |
328 | uid_t uid; | 303 | unsigned config_lineno; |
329 | const char *config_filename; | 304 | const char *config_filename; |
330 | FILE *fconfig; | 305 | FILE *fconfig; |
331 | char *defhost; | 306 | char *default_local_hostname; |
332 | #ifdef INETD_SETPROCTITLE | ||
333 | char **Argv; | ||
334 | char *LastArg; | ||
335 | #endif | ||
336 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN | 307 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
337 | char *endring; | 308 | char *endring; |
338 | char *ringpos; | 309 | char *ringpos; |
339 | char ring[128]; | 310 | char ring[128]; |
340 | #endif | 311 | #endif |
341 | fd_set allsock; | 312 | fd_set allsock; |
342 | /* Used only in nextline() */ | 313 | /* Used in next_line(), and as scratch read buffer */ |
343 | char line[80]; /* at least 80, see LINE_SIZE */ | 314 | char line[256]; /* _at least_ 256, see LINE_SIZE */ |
344 | }; | 315 | }; |
345 | #define G (*(struct globals*)&bb_common_bufsiz1) | 316 | #define G (*(struct globals*)&bb_common_bufsiz1) |
346 | enum { LINE_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line) }; | 317 | enum { LINE_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line) }; |
@@ -349,19 +320,19 @@ struct BUG_G_too_big { | |||
349 | }; | 320 | }; |
350 | #define rlim_ofile_cur (G.rlim_ofile_cur ) | 321 | #define rlim_ofile_cur (G.rlim_ofile_cur ) |
351 | #define rlim_ofile (G.rlim_ofile ) | 322 | #define rlim_ofile (G.rlim_ofile ) |
352 | #define servtab (G.servtab ) | 323 | #define serv_list (G.serv_list ) |
353 | #define global_queuelen (G.global_queuelen) | 324 | #define global_queuelen (G.global_queuelen) |
354 | #define nsock (G.nsock ) | 325 | #define prev_maxsock (G.prev_maxsock ) |
355 | #define maxsock (G.maxsock ) | 326 | #define maxsock (G.maxsock ) |
356 | #define toomany (G.toomany ) | 327 | #define max_concurrency (G.max_concurrency) |
357 | #define timingout (G.timingout ) | 328 | #define alarm_armed (G.alarm_armed ) |
358 | #define sp (G.sp ) | 329 | #define real_uid (G.real_uid ) |
359 | #define uid (G.uid ) | 330 | #define config_lineno (G.config_lineno ) |
360 | #define config_filename (G.config_filename) | 331 | #define config_filename (G.config_filename) |
361 | #define fconfig (G.fconfig ) | 332 | #define fconfig (G.fconfig ) |
362 | #define defhost (G.defhost ) | 333 | #define default_local_hostname (G.default_local_hostname) |
363 | #define Argv (G.Argv ) | 334 | #define first_ps_byte (G.first_ps_byte ) |
364 | #define LastArg (G.LastArg ) | 335 | #define last_ps_byte (G.last_ps_byte ) |
365 | #define endring (G.endring ) | 336 | #define endring (G.endring ) |
366 | #define ringpos (G.ringpos ) | 337 | #define ringpos (G.ringpos ) |
367 | #define ring (G.ring ) | 338 | #define ring (G.ring ) |
@@ -373,35 +344,50 @@ struct BUG_G_too_big { | |||
373 | config_filename = "/etc/inetd.conf"; \ | 344 | config_filename = "/etc/inetd.conf"; \ |
374 | } while (0) | 345 | } while (0) |
375 | 346 | ||
376 | /* xstrdup(NULL) returns NULL, but this one | 347 | static void maybe_close(int fd) |
377 | * will return newly-allocated "" if called with NULL arg | ||
378 | * TODO: audit whether this makes any real difference | ||
379 | */ | ||
380 | static char *xxstrdup(char *cp) | ||
381 | { | 348 | { |
382 | return xstrdup(cp ? cp : ""); | 349 | if (fd >= 0) |
350 | close(fd); | ||
383 | } | 351 | } |
384 | 352 | ||
385 | static int setconfig(void) | 353 | // TODO: move to libbb? |
354 | static len_and_sockaddr *xzalloc_lsa(int family) | ||
386 | { | 355 | { |
387 | free(defhost); | 356 | len_and_sockaddr *lsa; |
388 | defhost = xstrdup("*"); | 357 | int sz; |
389 | if (fconfig != NULL) { | 358 | |
390 | fseek(fconfig, 0L, SEEK_SET); | 359 | sz = sizeof(struct sockaddr_in); |
391 | return 1; | 360 | #if ENABLE_FEATURE_IPV6 |
392 | } | 361 | if (family == AF_INET6) |
393 | fconfig = fopen(config_filename, "r"); | 362 | sz = sizeof(struct sockaddr_in6); |
394 | return (fconfig != NULL); | 363 | #endif |
364 | lsa = xzalloc(LSA_LEN_SIZE + sz); | ||
365 | lsa->len = sz; | ||
366 | lsa->u.sa.sa_family = family; | ||
367 | return lsa; | ||
395 | } | 368 | } |
396 | 369 | ||
397 | static void endconfig(void) | 370 | |
371 | static void rearm_alarm(void) | ||
398 | { | 372 | { |
399 | if (fconfig) { | 373 | if (!alarm_armed) { |
400 | (void) fclose(fconfig); | 374 | alarm_armed = 1; |
401 | fconfig = NULL; | 375 | alarm(RETRYTIME); |
402 | } | 376 | } |
403 | free(defhost); | 377 | } |
404 | defhost = 0; | 378 | |
379 | static void block_CHLD_HUP_ALRM(sigset_t *m) | ||
380 | { | ||
381 | sigemptyset(m); | ||
382 | sigaddset(m, SIGCHLD); | ||
383 | sigaddset(m, SIGHUP); | ||
384 | sigaddset(m, SIGALRM); | ||
385 | sigprocmask(SIG_BLOCK, m, NULL); | ||
386 | } | ||
387 | |||
388 | static void unblock_sigs(sigset_t *m) | ||
389 | { | ||
390 | sigprocmask(SIG_UNBLOCK, m, NULL); | ||
405 | } | 391 | } |
406 | 392 | ||
407 | #if ENABLE_FEATURE_INETD_RPC | 393 | #if ENABLE_FEATURE_INETD_RPC |
@@ -409,27 +395,20 @@ static void register_rpc(servtab_t *sep) | |||
409 | { | 395 | { |
410 | int n; | 396 | int n; |
411 | struct sockaddr_in ir_sin; | 397 | struct sockaddr_in ir_sin; |
412 | struct protoent *pp; | ||
413 | socklen_t size; | 398 | socklen_t size; |
414 | 399 | ||
415 | pp = getprotobyname(sep->se_proto + 4); | 400 | size = sizeof(ir_sin); |
416 | if (pp == NULL) { | ||
417 | bb_perror_msg("%s: getproto", sep->se_proto); | ||
418 | return; | ||
419 | } | ||
420 | size = sizeof ir_sin; | ||
421 | if (getsockname(sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) { | 401 | if (getsockname(sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) { |
422 | bb_perror_msg("%s/%s: getsockname", | 402 | bb_perror_msg("getsockname"); |
423 | sep->se_service, sep->se_proto); | ||
424 | return; | 403 | return; |
425 | } | 404 | } |
426 | 405 | ||
427 | for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { | 406 | for (n = sep->se_rpcver_lo; n <= sep->se_rpcver_hi; n++) { |
428 | (void) pmap_unset(sep->se_rpcprog, n); | 407 | pmap_unset(sep->se_rpcprog, n); |
429 | if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(ir_sin.sin_port))) | 408 | if (!pmap_set(sep->se_rpcprog, n, sep->se_proto_no, ntohs(ir_sin.sin_port))) |
430 | bb_perror_msg("%s %s: pmap_set: %u %u %u %u", | 409 | bb_perror_msg("%s %s: pmap_set(%u,%u,%u,%u)", |
431 | sep->se_service, sep->se_proto, | 410 | sep->se_service, sep->se_proto, |
432 | sep->se_rpcprog, n, pp->p_proto, ntohs(ir_sin.sin_port)); | 411 | sep->se_rpcprog, n, sep->se_proto_no, ntohs(ir_sin.sin_port)); |
433 | } | 412 | } |
434 | } | 413 | } |
435 | 414 | ||
@@ -437,138 +416,163 @@ static void unregister_rpc(servtab_t *sep) | |||
437 | { | 416 | { |
438 | int n; | 417 | int n; |
439 | 418 | ||
440 | for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { | 419 | for (n = sep->se_rpcver_lo; n <= sep->se_rpcver_hi; n++) { |
441 | if (!pmap_unset(sep->se_rpcprog, n)) | 420 | if (!pmap_unset(sep->se_rpcprog, n)) |
442 | bb_error_msg("pmap_unset(%u, %u)", sep->se_rpcprog, n); | 421 | bb_perror_msg("pmap_unset(%u,%u)", sep->se_rpcprog, n); |
443 | } | 422 | } |
444 | } | 423 | } |
445 | #endif /* FEATURE_INETD_RPC */ | 424 | #endif /* FEATURE_INETD_RPC */ |
446 | 425 | ||
447 | static void freeconfig(servtab_t *cp) | 426 | static void bump_nofile(void) |
448 | { | ||
449 | int i; | ||
450 | |||
451 | free(cp->se_hostaddr); | ||
452 | free(cp->se_service); | ||
453 | free(cp->se_proto); | ||
454 | free(cp->se_user); | ||
455 | free(cp->se_group); | ||
456 | free(cp->se_server); | ||
457 | for (i = 0; i < MAXARGV; i++) | ||
458 | free(cp->se_argv[i]); | ||
459 | } | ||
460 | |||
461 | static int bump_nofile(void) | ||
462 | { | 427 | { |
463 | #define FD_CHUNK 32 | 428 | enum { FD_CHUNK = 32 }; |
464 | |||
465 | struct rlimit rl; | 429 | struct rlimit rl; |
466 | 430 | ||
467 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { | 431 | /* Never fails under Linux (except if you pass it bad arguments) */ |
468 | bb_perror_msg("getrlimit"); | 432 | getrlimit(RLIMIT_NOFILE, &rl); |
469 | return -1; | ||
470 | } | ||
471 | rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK); | 433 | rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK); |
472 | rl.rlim_cur = MIN(FD_SETSIZE, rl.rlim_cur + FD_CHUNK); | 434 | rl.rlim_cur = MIN(FD_SETSIZE, rl.rlim_cur + FD_CHUNK); |
473 | if (rl.rlim_cur <= rlim_ofile_cur) { | 435 | if (rl.rlim_cur <= rlim_ofile_cur) { |
474 | bb_error_msg("bump_nofile: cannot extend file limit, max = %d", | 436 | bb_error_msg("can't extend file limit, max = %d", |
475 | (int) rl.rlim_cur); | 437 | (int) rl.rlim_cur); |
476 | return -1; | 438 | return; |
477 | } | 439 | } |
478 | 440 | ||
479 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { | 441 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { |
480 | bb_perror_msg("setrlimit"); | 442 | bb_perror_msg("setrlimit"); |
481 | return -1; | 443 | return; |
482 | } | 444 | } |
483 | 445 | ||
484 | rlim_ofile_cur = rl.rlim_cur; | 446 | rlim_ofile_cur = rl.rlim_cur; |
485 | return 0; | ||
486 | } | 447 | } |
487 | 448 | ||
488 | static void setup(servtab_t *sep) | 449 | static void remove_fd_from_set(int fd) |
450 | { | ||
451 | if (fd >= 0) { | ||
452 | FD_CLR(fd, &allsock); | ||
453 | maxsock = -1; | ||
454 | } | ||
455 | } | ||
456 | |||
457 | static void add_fd_to_set(int fd) | ||
458 | { | ||
459 | if (fd >= 0) { | ||
460 | FD_SET(fd, &allsock); | ||
461 | if (maxsock >= 0 && fd > maxsock) { | ||
462 | prev_maxsock = maxsock = fd; | ||
463 | if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN) | ||
464 | bump_nofile(); | ||
465 | } | ||
466 | } | ||
467 | } | ||
468 | |||
469 | static void recalculate_maxsock(void) | ||
470 | { | ||
471 | int fd = 0; | ||
472 | while (fd <= prev_maxsock) { | ||
473 | if (FD_ISSET(fd, &allsock)) | ||
474 | maxsock = fd; | ||
475 | fd++; | ||
476 | } | ||
477 | prev_maxsock = maxsock; | ||
478 | if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN) | ||
479 | bump_nofile(); | ||
480 | } | ||
481 | |||
482 | static void prepare_socket_fd(servtab_t *sep) | ||
489 | { | 483 | { |
490 | int r; | 484 | int r, fd; |
491 | 485 | ||
492 | sep->se_fd = socket(sep->se_family, sep->se_socktype, 0); | 486 | fd = socket(sep->se_family, sep->se_socktype, 0); |
493 | if (sep->se_fd < 0) { | 487 | if (fd < 0) { |
494 | bb_perror_msg("%s/%s: socket", sep->se_service, sep->se_proto); | 488 | bb_perror_msg("socket"); |
495 | return; | 489 | return; |
496 | } | 490 | } |
497 | setsockopt_reuseaddr(sep->se_fd); | 491 | setsockopt_reuseaddr(fd); |
498 | 492 | ||
499 | #if ENABLE_FEATURE_INETD_RPC | 493 | #if ENABLE_FEATURE_INETD_RPC |
500 | if (isrpcservice(sep)) { | 494 | if (is_rpc_service(sep)) { |
501 | struct passwd *pwd; | 495 | struct passwd *pwd; |
502 | 496 | ||
503 | /* | 497 | /* zero out the port for all RPC services; let bind() |
504 | * for RPC services, attempt to use a reserved port | 498 | * find one. */ |
505 | * if they are going to be running as root. | 499 | set_nport(sep->se_lsa, 0); |
506 | * | 500 | |
507 | * Also, zero out the port for all RPC services; let bind() | 501 | /* for RPC services, attempt to use a reserved port |
508 | * find one. | 502 | * if they are going to be running as root. */ |
509 | */ | 503 | if (real_uid == 0 && sep->se_family == AF_INET |
510 | sep->se_ctrladdr_in.sin_port = 0; | 504 | && (pwd = getpwnam(sep->se_user)) != NULL |
511 | if (sep->se_user && (pwd = getpwnam(sep->se_user)) && | 505 | && pwd->pw_uid == 0 |
512 | pwd->pw_uid == 0 && uid == 0) | 506 | ) { |
513 | r = bindresvport(sep->se_fd, &sep->se_ctrladdr_in); | 507 | r = bindresvport(fd, &sep->se_lsa->u.sin); |
514 | else { | 508 | } else { |
515 | r = bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size); | 509 | r = bind(fd, &sep->se_lsa->u.sa, sep->se_lsa->len); |
516 | if (r == 0) { | 510 | } |
517 | socklen_t len = sep->se_ctrladdr_size; | 511 | if (r == 0) { |
518 | int saveerrno = errno; | 512 | int saveerrno = errno; |
519 | 513 | /* update lsa with port# */ | |
520 | /* update se_ctrladdr_in.sin_port */ | 514 | getsockname(fd, &sep->se_lsa->u.sa, &sep->se_lsa->len); |
521 | r = getsockname(sep->se_fd, &sep->se_ctrladdr, &len); | 515 | errno = saveerrno; |
522 | if (r <= 0) | ||
523 | errno = saveerrno; | ||
524 | } | ||
525 | } | 516 | } |
526 | } else | 517 | } else |
527 | #endif | 518 | #endif |
528 | r = bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size); | 519 | { |
529 | if (r < 0) { | 520 | if (sep->se_family == AF_UNIX) { |
530 | bb_perror_msg("%s/%s (%d): bind", | 521 | struct sockaddr_un *sun; |
531 | sep->se_service, sep->se_proto, sep->se_ctrladdr.sa_family); | 522 | sun = (struct sockaddr_un*)&(sep->se_lsa->u.sa); |
532 | close(sep->se_fd); | 523 | unlink(sun->sun_path); |
533 | sep->se_fd = -1; | ||
534 | if (!timingout) { | ||
535 | timingout = 1; | ||
536 | alarm(RETRYTIME); | ||
537 | } | 524 | } |
525 | r = bind(fd, &sep->se_lsa->u.sa, sep->se_lsa->len); | ||
526 | } | ||
527 | if (r < 0) { | ||
528 | bb_perror_msg("%s/%s: bind", | ||
529 | sep->se_service, sep->se_proto); | ||
530 | close(fd); | ||
531 | rearm_alarm(); | ||
538 | return; | 532 | return; |
539 | } | 533 | } |
540 | if (sep->se_socktype == SOCK_STREAM) | 534 | if (sep->se_socktype == SOCK_STREAM) |
541 | listen(sep->se_fd, global_queuelen); | 535 | listen(fd, global_queuelen); |
542 | 536 | ||
543 | FD_SET(sep->se_fd, &allsock); | 537 | add_fd_to_set(fd); |
544 | nsock++; | 538 | sep->se_fd = fd; |
545 | if (sep->se_fd > maxsock) { | 539 | } |
546 | maxsock = sep->se_fd; | 540 | |
547 | if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN) | 541 | static int reopen_config_file(void) |
548 | bump_nofile(); | 542 | { |
543 | free(default_local_hostname); | ||
544 | default_local_hostname = xstrdup("*"); | ||
545 | if (fconfig != NULL) | ||
546 | fclose(fconfig); | ||
547 | config_lineno = 0; | ||
548 | fconfig = fopen_or_warn(config_filename, "r"); | ||
549 | return (fconfig != NULL); | ||
550 | } | ||
551 | |||
552 | static void close_config_file(void) | ||
553 | { | ||
554 | if (fconfig) { | ||
555 | fclose(fconfig); | ||
556 | fconfig = NULL; | ||
549 | } | 557 | } |
550 | } | 558 | } |
551 | 559 | ||
552 | static char *nextline(void) | 560 | static char *next_line(void) |
553 | { | 561 | { |
554 | if (fgets(line, LINE_SIZE, fconfig) == NULL) | 562 | if (fgets(line, LINE_SIZE, fconfig) == NULL) |
555 | return NULL; | 563 | return NULL; |
564 | config_lineno++; | ||
556 | *strchrnul(line, '\n') = '\0'; | 565 | *strchrnul(line, '\n') = '\0'; |
557 | return line; | 566 | return line; |
558 | } | 567 | } |
559 | 568 | ||
560 | static char *skip(char **cpp) /* int report; */ | 569 | static char *next_word(char **cpp) |
561 | { | 570 | { |
562 | char *cp = *cpp; | ||
563 | char *start; | 571 | char *start; |
572 | char *cp = *cpp; | ||
564 | 573 | ||
565 | /* erp: */ | 574 | if (cp == NULL) |
566 | if (cp == NULL) { | ||
567 | /* if (report) */ | ||
568 | /* bb_error_msg("syntax error in inetd config file"); */ | ||
569 | return NULL; | 575 | return NULL; |
570 | } | ||
571 | |||
572 | again: | 576 | again: |
573 | while (*cp == ' ' || *cp == '\t') | 577 | while (*cp == ' ' || *cp == '\t') |
574 | cp++; | 578 | cp++; |
@@ -576,7 +580,7 @@ static char *skip(char **cpp) /* int report; */ | |||
576 | int c = getc(fconfig); | 580 | int c = getc(fconfig); |
577 | ungetc(c, fconfig); | 581 | ungetc(c, fconfig); |
578 | if (c == ' ' || c == '\t') { | 582 | if (c == ' ' || c == '\t') { |
579 | cp = nextline(); | 583 | cp = next_line(); |
580 | if (cp) | 584 | if (cp) |
581 | goto again; | 585 | goto again; |
582 | } | 586 | } |
@@ -593,90 +597,109 @@ static char *skip(char **cpp) /* int report; */ | |||
593 | return start; | 597 | return start; |
594 | } | 598 | } |
595 | 599 | ||
600 | static void free_servtab_strings(servtab_t *cp) | ||
601 | { | ||
602 | int i; | ||
603 | |||
604 | free(cp->se_local_hostname); | ||
605 | free(cp->se_service); | ||
606 | free(cp->se_proto); | ||
607 | free(cp->se_user); | ||
608 | free(cp->se_group); | ||
609 | free(cp->se_lsa); /* not a string in fact */ | ||
610 | free(cp->se_program); | ||
611 | for (i = 0; i < MAXARGV; i++) | ||
612 | free(cp->se_argv[i]); | ||
613 | } | ||
614 | |||
596 | static servtab_t *new_servtab(void) | 615 | static servtab_t *new_servtab(void) |
597 | { | 616 | { |
598 | return xzalloc(sizeof(servtab_t)); | 617 | servtab_t *newtab = xzalloc(sizeof(servtab_t)); |
618 | newtab->se_fd = -1; /* paranoia */ | ||
619 | return newtab; | ||
599 | } | 620 | } |
600 | 621 | ||
601 | static servtab_t *dupconfig(servtab_t *sep) | 622 | static servtab_t *dup_servtab(servtab_t *sep) |
602 | { | 623 | { |
603 | servtab_t *newtab; | 624 | servtab_t *newtab; |
604 | int argc; | 625 | int argc; |
605 | 626 | ||
606 | newtab = new_servtab(); | 627 | newtab = new_servtab(); |
607 | newtab->se_service = xstrdup(sep->se_service); | 628 | *newtab = *sep; /* struct copy */ |
608 | newtab->se_socktype = sep->se_socktype; | 629 | /* deep-copying strings */ |
609 | newtab->se_family = sep->se_family; | 630 | newtab->se_service = xstrdup(newtab->se_service); |
610 | newtab->se_proto = xstrdup(sep->se_proto); | 631 | newtab->se_proto = xstrdup(newtab->se_proto); |
611 | #if ENABLE_FEATURE_INETD_RPC | 632 | newtab->se_user = xstrdup(newtab->se_user); |
612 | newtab->se_rpcprog = sep->se_rpcprog; | 633 | newtab->se_group = xstrdup(newtab->se_group); |
613 | newtab->se_rpcversl = sep->se_rpcversl; | 634 | newtab->se_program = xstrdup(newtab->se_program); |
614 | newtab->se_rpcversh = sep->se_rpcversh; | ||
615 | #endif | ||
616 | newtab->se_wait = sep->se_wait; | ||
617 | newtab->se_user = xstrdup(sep->se_user); | ||
618 | newtab->se_group = xstrdup(sep->se_group); | ||
619 | #ifdef INETD_FEATURE_ENABLED | ||
620 | newtab->se_bi = sep->se_bi; | ||
621 | #endif | ||
622 | newtab->se_server = xstrdup(sep->se_server); | ||
623 | |||
624 | for (argc = 0; argc <= MAXARGV; argc++) | 635 | for (argc = 0; argc <= MAXARGV; argc++) |
625 | newtab->se_argv[argc] = xstrdup(sep->se_argv[argc]); | 636 | newtab->se_argv[argc] = xstrdup(newtab->se_argv[argc]); |
626 | newtab->se_max = sep->se_max; | 637 | /* NB: se_fd, se_hostaddr and se_next are always |
638 | * overwrittend by callers, so we don't bother resetting them | ||
639 | * to NULL/0/-1 etc */ | ||
627 | 640 | ||
628 | return newtab; | 641 | return newtab; |
629 | } | 642 | } |
630 | 643 | ||
631 | static servtab_t *getconfigent(void) | 644 | /* gcc generates much more code if this is inlined */ |
645 | static NOINLINE servtab_t *parse_one_line(void) | ||
632 | { | 646 | { |
633 | servtab_t *sep; | ||
634 | int argc; | 647 | int argc; |
635 | char *cp, *arg; | 648 | char *p, *cp, *arg; |
636 | char *hostdelim; | 649 | char *hostdelim; |
650 | servtab_t *sep; | ||
637 | servtab_t *nsep; | 651 | servtab_t *nsep; |
638 | servtab_t *psep; | 652 | new: |
639 | |||
640 | sep = new_servtab(); | 653 | sep = new_servtab(); |
641 | |||
642 | more: | 654 | more: |
643 | while ((cp = nextline()) && *cp == '#') | 655 | while ((cp = next_line()) && *cp == '#') |
644 | continue; /* skip comment lines */ | 656 | continue; /* skip comment lines */ |
645 | if (cp == NULL) { | 657 | if (cp == NULL) { |
646 | free(sep); | 658 | free(sep); |
647 | return NULL; | 659 | return NULL; |
648 | } | 660 | } |
649 | 661 | ||
650 | arg = skip(&cp); | 662 | arg = next_word(&cp); |
651 | if (arg == NULL) { | 663 | if (arg == NULL) { |
652 | /* A blank line. */ | 664 | /* A blank line. */ |
653 | goto more; | 665 | goto more; |
654 | } | 666 | } |
655 | 667 | ||
656 | /* Check for a host name. */ | 668 | /* [host:]service socktype proto wait user[:group] prog [args] */ |
669 | /* Check for "host:...." line */ | ||
657 | hostdelim = strrchr(arg, ':'); | 670 | hostdelim = strrchr(arg, ':'); |
658 | if (hostdelim) { | 671 | if (hostdelim) { |
659 | *hostdelim = '\0'; | 672 | *hostdelim = '\0'; |
660 | sep->se_hostaddr = xstrdup(arg); | 673 | sep->se_local_hostname = xstrdup(arg); |
661 | arg = hostdelim + 1; | 674 | arg = hostdelim + 1; |
662 | /* | ||
663 | * If the line is of the form `host:', then just change the | ||
664 | * default host for the following lines. | ||
665 | */ | ||
666 | if (*arg == '\0') { | 675 | if (*arg == '\0') { |
667 | arg = skip(&cp); | 676 | arg = next_word(&cp); |
668 | if (cp == NULL) { | 677 | if (arg == NULL) { |
669 | free(defhost); | 678 | /* Line has just "host:", change the |
670 | defhost = sep->se_hostaddr; | 679 | * default host for the following lines. */ |
680 | free(default_local_hostname); | ||
681 | default_local_hostname = sep->se_local_hostname; | ||
671 | goto more; | 682 | goto more; |
672 | } | 683 | } |
673 | } | 684 | } |
674 | } else | 685 | } else |
675 | sep->se_hostaddr = xxstrdup(defhost); | 686 | sep->se_local_hostname = xstrdup(default_local_hostname); |
676 | |||
677 | sep->se_service = xxstrdup(arg); | ||
678 | arg = skip(&cp); | ||
679 | 687 | ||
688 | /* service socktype proto wait user[:group] prog [args] */ | ||
689 | sep->se_service = xstrdup(arg); | ||
690 | /* socktype proto wait user[:group] prog [args] */ | ||
691 | arg = next_word(&cp); | ||
692 | if (arg == NULL) { | ||
693 | parse_err: | ||
694 | bb_error_msg("parse error on line %u, line is ignored", | ||
695 | config_lineno); | ||
696 | free_servtab_strings(sep); | ||
697 | /* Just "goto more" can make sep to carry over e.g. | ||
698 | * "rpc"-ness (by having se_rpcver_lo != 0). | ||
699 | * We will be more paranoid: */ | ||
700 | free(sep); | ||
701 | goto new; | ||
702 | } | ||
680 | { | 703 | { |
681 | static int8_t SOCK_xxx[] ALIGN1 = { | 704 | static int8_t SOCK_xxx[] ALIGN1 = { |
682 | -1, | 705 | -1, |
@@ -689,63 +712,85 @@ static servtab_t *getconfigent(void) | |||
689 | , arg)]; | 712 | , arg)]; |
690 | } | 713 | } |
691 | 714 | ||
692 | sep->se_proto = xxstrdup(skip(&cp)); | 715 | /* {unix,[rpc/]{tcp,udp}[6]} wait user[:group] prog [args] */ |
693 | 716 | sep->se_proto = arg = xstrdup(next_word(&cp)); | |
694 | if (strcmp(sep->se_proto, "unix") == 0) { | 717 | if (arg == NULL) |
718 | goto parse_err; | ||
719 | if (strcmp(arg, "unix") == 0) { | ||
695 | sep->se_family = AF_UNIX; | 720 | sep->se_family = AF_UNIX; |
696 | } else { | 721 | } else { |
722 | char *six; | ||
697 | sep->se_family = AF_INET; | 723 | sep->se_family = AF_INET; |
698 | if (sep->se_proto[strlen(sep->se_proto) - 1] == '6') | 724 | six = last_char_is(arg, '6'); |
725 | if (six) { | ||
699 | #if ENABLE_FEATURE_IPV6 | 726 | #if ENABLE_FEATURE_IPV6 |
727 | *six = '\0'; | ||
700 | sep->se_family = AF_INET6; | 728 | sep->se_family = AF_INET6; |
701 | #else | 729 | #else |
702 | bb_error_msg("%s: IPV6 not supported", sep->se_proto); | 730 | bb_error_msg("%s: no support for IPv6", sep->se_proto); |
731 | goto parse_err; | ||
703 | #endif | 732 | #endif |
704 | if (strncmp(sep->se_proto, "rpc/", 4) == 0) { | 733 | } |
734 | if (strncmp(arg, "rpc/", 4) == 0) { | ||
705 | #if ENABLE_FEATURE_INETD_RPC | 735 | #if ENABLE_FEATURE_INETD_RPC |
706 | char *p, *ccp; | 736 | unsigned n; |
707 | long l; | 737 | arg += 4; |
708 | |||
709 | p = strchr(sep->se_service, '/'); | 738 | p = strchr(sep->se_service, '/'); |
710 | if (p == 0) { | 739 | if (p == NULL) { |
711 | bb_error_msg("%s: no rpc version", sep->se_service); | 740 | bb_error_msg("no rpc version: '%s'", sep->se_service); |
712 | goto more; | 741 | goto parse_err; |
713 | } | 742 | } |
714 | *p++ = '\0'; | 743 | *p++ = '\0'; |
715 | l = strtol(p, &ccp, 0); | 744 | n = bb_strtou(p, &p, 10); |
716 | if (ccp == p || l < 0 || l > INT_MAX) { | 745 | if (n > INT_MAX) { |
717 | badafterall: | 746 | bad_ver_spec: |
718 | bb_error_msg("%s/%s: bad rpc version", sep->se_service, p); | 747 | bb_error_msg("bad rpc version"); |
719 | goto more; | 748 | goto parse_err; |
720 | } | 749 | } |
721 | sep->se_rpcversl = sep->se_rpcversh = l; | 750 | sep->se_rpcver_lo = sep->se_rpcver_hi = n; |
722 | if (*ccp == '-') { | 751 | if (*p == '-') { |
723 | p = ccp + 1; | 752 | p++; |
724 | l = strtol(p, &ccp, 0); | 753 | n = bb_strtou(p, &p, 10); |
725 | if (ccp == p || l < 0 || l > INT_MAX || l < sep->se_rpcversl || *ccp) | 754 | if (n > INT_MAX || n < sep->se_rpcver_lo) |
726 | goto badafterall; | 755 | goto bad_ver_spec; |
727 | sep->se_rpcversh = l; | 756 | sep->se_rpcver_hi = n; |
728 | } else if (*ccp != '\0') | 757 | } |
729 | goto badafterall; | 758 | if (*p != '\0') |
759 | goto bad_ver_spec; | ||
730 | #else | 760 | #else |
731 | bb_error_msg("%s: rpc services not supported", sep->se_service); | 761 | bb_error_msg("no support for rpc services"); |
762 | goto parse_err; | ||
732 | #endif | 763 | #endif |
733 | } | 764 | } |
765 | /* we don't really need getprotobyname()! */ | ||
766 | if (strcmp(arg, "tcp") == 0) | ||
767 | sep->se_proto_no = 6; | ||
768 | if (strcmp(arg, "udp") == 0) | ||
769 | sep->se_proto_no = 17; | ||
770 | if (six) | ||
771 | *six = '6'; | ||
772 | if (!sep->se_proto_no) /* not tcp/udp?? */ | ||
773 | goto parse_err; | ||
734 | } | 774 | } |
735 | arg = skip(&cp); | ||
736 | if (arg == NULL) | ||
737 | goto more; | ||
738 | 775 | ||
739 | { | 776 | /* [no]wait[.max] user[:group] prog [args] */ |
740 | char *s = strchr(arg, '.'); | 777 | arg = next_word(&cp); |
741 | if (s) { | 778 | if (arg == NULL) |
742 | *s++ = '\0'; | 779 | goto parse_err; |
743 | sep->se_max = xatoi(s); | 780 | sep->se_max = max_concurrency; |
744 | } else | 781 | p = strchr(arg, '.'); |
745 | sep->se_max = toomany; | 782 | if (p) { |
783 | *p++ = '\0'; | ||
784 | sep->se_max = bb_strtou(p, NULL, 10); | ||
785 | if (errno) | ||
786 | goto parse_err; | ||
746 | } | 787 | } |
747 | sep->se_wait = (strcmp(arg, "wait") == 0); | 788 | sep->se_wait = (strcmp(arg, "wait") == 0); |
748 | sep->se_user = xxstrdup(skip(&cp)); | 789 | |
790 | /* user[:group] prog [args] */ | ||
791 | sep->se_user = xstrdup(next_word(&cp)); | ||
792 | if (sep->se_user == NULL) | ||
793 | goto parse_err; | ||
749 | arg = strchr(sep->se_user, '.'); | 794 | arg = strchr(sep->se_user, '.'); |
750 | if (arg == NULL) | 795 | if (arg == NULL) |
751 | arg = strchr(sep->se_user, ':'); | 796 | arg = strchr(sep->se_user, ':'); |
@@ -754,27 +799,30 @@ static servtab_t *getconfigent(void) | |||
754 | sep->se_group = xstrdup(arg); | 799 | sep->se_group = xstrdup(arg); |
755 | } | 800 | } |
756 | 801 | ||
757 | sep->se_server = xxstrdup(skip(&cp)); | 802 | /* prog [args] */ |
758 | #ifdef INETD_FEATURE_ENABLED | 803 | sep->se_program = xstrdup(next_word(&cp)); |
759 | /* sep->se_bi = NULL; - done by new_servtab() */ | 804 | if (sep->se_program == NULL) |
760 | if (strcmp(sep->se_server, "internal") == 0) { | 805 | goto parse_err; |
761 | const struct builtin *bi; | 806 | #ifdef INETD_BUILTINS_ENABLED |
762 | 807 | /* sep->se_builtin = NULL; - done by new_servtab() */ | |
763 | for (bi = builtins; bi->bi_service; bi++) | 808 | if (strcmp(sep->se_program, "internal") == 0 |
764 | if (bi->bi_socktype == sep->se_socktype | 809 | && (sep->se_socktype == SOCK_STREAM |
765 | && strcmp(bi->bi_service, sep->se_service) == 0) | 810 | || sep->se_socktype == SOCK_DGRAM) |
766 | break; | 811 | ) { |
767 | if (bi->bi_service == 0) { | 812 | int i; |
768 | bb_error_msg("internal service %s unknown", sep->se_service); | 813 | for (i = 0; i < ARRAY_SIZE(builtins); i++) |
769 | goto more; | 814 | if (strcmp(builtins[i].bi_service, sep->se_service) == 0) |
770 | } | 815 | goto found_bi; |
771 | sep->se_bi = bi; | 816 | bb_error_msg("unknown internal service %s", sep->se_service); |
772 | sep->se_wait = 0; /* = bi->bi_wait; - always 0 */ | 817 | goto parse_err; |
818 | found_bi: | ||
819 | sep->se_builtin = &builtins[i]; | ||
820 | sep->se_wait = 0; /* = builtins[i].bi_wait; - always 0 */ | ||
773 | } | 821 | } |
774 | #endif | 822 | #endif |
775 | argc = 0; | 823 | argc = 0; |
776 | while ((arg = skip(&cp)) != NULL && argc < MAXARGV) { | 824 | while ((arg = next_word(&cp)) != NULL && argc < MAXARGV) { |
777 | sep->se_argv[argc++] = xxstrdup(arg); | 825 | sep->se_argv[argc++] = xstrdup(arg); |
778 | } | 826 | } |
779 | /* while (argc <= MAXARGV) */ | 827 | /* while (argc <= MAXARGV) */ |
780 | /* sep->se_argv[argc++] = NULL; - done by new_servtab() */ | 828 | /* sep->se_argv[argc++] = NULL; - done by new_servtab() */ |
@@ -784,744 +832,531 @@ static servtab_t *getconfigent(void) | |||
784 | * specifier was a comma separated list of hostnames. If so | 832 | * specifier was a comma separated list of hostnames. If so |
785 | * we'll make new entries for each address. | 833 | * we'll make new entries for each address. |
786 | */ | 834 | */ |
787 | while ((hostdelim = strrchr(sep->se_hostaddr, ',')) != NULL) { | 835 | while ((hostdelim = strrchr(sep->se_local_hostname, ',')) != NULL) { |
788 | nsep = dupconfig(sep); | 836 | nsep = dup_servtab(sep); |
789 | |||
790 | /* | 837 | /* |
791 | * NULL terminate the hostname field of the existing entry, | 838 | * NUL terminate the hostname field of the existing entry, |
792 | * and make a dup for the new entry. | 839 | * and make a dup for the new entry. |
793 | */ | 840 | */ |
794 | *hostdelim++ = '\0'; | 841 | *hostdelim++ = '\0'; |
795 | nsep->se_hostaddr = xstrdup(hostdelim); | 842 | nsep->se_local_hostname = xstrdup(hostdelim); |
796 | |||
797 | nsep->se_next = sep->se_next; | 843 | nsep->se_next = sep->se_next; |
798 | sep->se_next = nsep; | 844 | sep->se_next = nsep; |
799 | } | 845 | } |
800 | 846 | ||
801 | nsep = sep; | 847 | /* Was doing it here: */ |
802 | while (nsep != NULL) { | 848 | /* DNS resolution, create copies for each IP address */ |
803 | nsep->se_checked = 1; | ||
804 | if (nsep->se_family == AF_INET) { | ||
805 | if (LONE_CHAR(nsep->se_hostaddr, '*')) | ||
806 | nsep->se_ctrladdr_in.sin_addr.s_addr = INADDR_ANY; | ||
807 | else if (!inet_aton(nsep->se_hostaddr, &nsep->se_ctrladdr_in.sin_addr)) { | ||
808 | int i; | ||
809 | struct hostent *hp; | ||
810 | |||
811 | hp = gethostbyname(nsep->se_hostaddr); | ||
812 | if (hp == NULL) { | ||
813 | bb_error_msg("%s: unknown host", nsep->se_hostaddr); | ||
814 | nsep->se_checked = 0; | ||
815 | goto skip; | ||
816 | } | ||
817 | if (hp->h_addrtype != AF_INET) { | ||
818 | bb_error_msg("%s: address isn't an Internet " | ||
819 | "address", nsep->se_hostaddr); | ||
820 | nsep->se_checked = 0; | ||
821 | goto skip; | ||
822 | } | ||
823 | i = 1; | ||
824 | memmove(&nsep->se_ctrladdr_in.sin_addr, | ||
825 | hp->h_addr_list[0], sizeof(struct in_addr)); | ||
826 | while (hp->h_addr_list[i] != NULL) { | ||
827 | psep = dupconfig(nsep); | ||
828 | psep->se_hostaddr = xxstrdup(nsep->se_hostaddr); | ||
829 | psep->se_checked = 1; | ||
830 | memmove(&psep->se_ctrladdr_in.sin_addr, | ||
831 | hp->h_addr_list[i], sizeof(struct in_addr)); | ||
832 | psep->se_ctrladdr_size = sizeof(psep->se_ctrladdr_in); | ||
833 | i++; | ||
834 | /* Prepend to list, don't want to look up */ | ||
835 | /* its hostname again. */ | ||
836 | psep->se_next = sep; | ||
837 | sep = psep; | ||
838 | } | ||
839 | } | ||
840 | } | ||
841 | /* XXX BUG?: is this skip: label supposed to remain? */ | ||
842 | skip: | ||
843 | nsep = nsep->se_next; | ||
844 | } | ||
845 | |||
846 | /* | ||
847 | * Finally, free any entries which failed the gethostbyname | ||
848 | * check. | ||
849 | */ | ||
850 | psep = NULL; | ||
851 | nsep = sep; | ||
852 | while (nsep != NULL) { | ||
853 | servtab_t *tsep; | ||
854 | |||
855 | if (nsep->se_checked == 0) { | ||
856 | tsep = nsep; | ||
857 | if (psep == NULL) { | ||
858 | sep = nsep->se_next; | ||
859 | nsep = sep; | ||
860 | } else { | ||
861 | nsep = nsep->se_next; | ||
862 | psep->se_next = nsep; | ||
863 | } | ||
864 | freeconfig(tsep); | ||
865 | } else { | ||
866 | nsep->se_checked = 0; | ||
867 | psep = nsep; | ||
868 | nsep = nsep->se_next; | ||
869 | } | ||
870 | } | ||
871 | 849 | ||
872 | return sep; | 850 | return sep; |
873 | } | 851 | } |
874 | 852 | ||
875 | #define Block_Using_Signals(m) do { \ | 853 | static servtab_t *insert_in_servlist(servtab_t *cp) |
876 | sigemptyset(&m); \ | ||
877 | sigaddset(&m, SIGCHLD); \ | ||
878 | sigaddset(&m, SIGHUP); \ | ||
879 | sigaddset(&m, SIGALRM); \ | ||
880 | sigprocmask(SIG_BLOCK, &m, NULL); \ | ||
881 | } while (0) | ||
882 | |||
883 | static servtab_t *enter(servtab_t *cp) | ||
884 | { | 854 | { |
885 | servtab_t *sep; | 855 | servtab_t *sep; |
886 | sigset_t omask; | 856 | sigset_t omask; |
887 | 857 | ||
888 | sep = new_servtab(); | 858 | sep = new_servtab(); |
889 | *sep = *cp; | 859 | *sep = *cp; /* struct copy */ |
890 | sep->se_fd = -1; | 860 | sep->se_fd = -1; |
891 | #if ENABLE_FEATURE_INETD_RPC | 861 | #if ENABLE_FEATURE_INETD_RPC |
892 | sep->se_rpcprog = -1; | 862 | sep->se_rpcprog = -1; |
893 | #endif | 863 | #endif |
894 | Block_Using_Signals(omask); | 864 | block_CHLD_HUP_ALRM(&omask); |
895 | sep->se_next = servtab; | 865 | sep->se_next = serv_list; |
896 | servtab = sep; | 866 | serv_list = sep; |
897 | sigprocmask(SIG_UNBLOCK, &omask, NULL); | 867 | unblock_sigs(&omask); |
898 | return sep; | 868 | return sep; |
899 | } | 869 | } |
900 | 870 | ||
901 | static int matchconf(servtab_t *old, servtab_t *new) | 871 | static int same_serv_addr_proto(servtab_t *old, servtab_t *new) |
902 | { | 872 | { |
903 | if (strcmp(old->se_service, new->se_service) != 0) | 873 | if (strcmp(old->se_local_hostname, new->se_local_hostname) != 0) |
904 | return 0; | 874 | return 0; |
905 | 875 | if (strcmp(old->se_service, new->se_service) != 0) | |
906 | if (strcmp(old->se_hostaddr, new->se_hostaddr) != 0) | ||
907 | return 0; | 876 | return 0; |
908 | |||
909 | if (strcmp(old->se_proto, new->se_proto) != 0) | 877 | if (strcmp(old->se_proto, new->se_proto) != 0) |
910 | return 0; | 878 | return 0; |
911 | |||
912 | /* | ||
913 | * If the new servtab is bound to a specific address, check that the | ||
914 | * old servtab is bound to the same entry. If the new service is not | ||
915 | * bound to a specific address then the check of se_hostaddr above | ||
916 | * is sufficient. | ||
917 | */ | ||
918 | |||
919 | if (old->se_family == AF_INET && new->se_family == AF_INET && | ||
920 | memcmp(&old->se_ctrladdr_in.sin_addr, | ||
921 | &new->se_ctrladdr_in.sin_addr, | ||
922 | sizeof(new->se_ctrladdr_in.sin_addr)) != 0) | ||
923 | return 0; | ||
924 | |||
925 | #if ENABLE_FEATURE_IPV6 | ||
926 | if (old->se_family == AF_INET6 && new->se_family == AF_INET6 && | ||
927 | memcmp(&old->se_ctrladdr_in6.sin6_addr, | ||
928 | &new->se_ctrladdr_in6.sin6_addr, | ||
929 | sizeof(new->se_ctrladdr_in6.sin6_addr)) != 0) | ||
930 | return 0; | ||
931 | #endif | ||
932 | return 1; | 879 | return 1; |
933 | } | 880 | } |
934 | 881 | ||
935 | static void config(int sig ATTRIBUTE_UNUSED) | 882 | static void reread_config_file(int sig ATTRIBUTE_UNUSED) |
936 | { | 883 | { |
937 | servtab_t *sep, *cp, **sepp; | 884 | servtab_t *sep, *cp, **sepp; |
885 | len_and_sockaddr *lsa; | ||
938 | sigset_t omask; | 886 | sigset_t omask; |
939 | size_t n; | 887 | unsigned n; |
940 | char protoname[10]; | 888 | uint16_t port; |
941 | 889 | ||
942 | if (!setconfig()) { | 890 | if (!reopen_config_file()) |
943 | bb_simple_perror_msg(config_filename); | ||
944 | return; | 891 | return; |
945 | } | 892 | for (sep = serv_list; sep; sep = sep->se_next) |
946 | for (sep = servtab; sep; sep = sep->se_next) | ||
947 | sep->se_checked = 0; | 893 | sep->se_checked = 0; |
948 | cp = getconfigent(); | ||
949 | while (cp != NULL) { | ||
950 | for (sep = servtab; sep; sep = sep->se_next) | ||
951 | if (matchconf(sep, cp)) | ||
952 | break; | ||
953 | 894 | ||
954 | if (sep != 0) { | 895 | goto first_line; |
896 | while (1) { | ||
897 | if (cp == NULL) { | ||
898 | first_line: | ||
899 | cp = parse_one_line(); | ||
900 | if (cp == NULL) | ||
901 | break; | ||
902 | } | ||
903 | for (sep = serv_list; sep; sep = sep->se_next) | ||
904 | if (same_serv_addr_proto(sep, cp)) | ||
905 | goto equal_servtab; | ||
906 | /* not an "equal" servtab */ | ||
907 | sep = insert_in_servlist(cp); | ||
908 | goto after_check; | ||
909 | equal_servtab: | ||
910 | { | ||
955 | int i; | 911 | int i; |
956 | 912 | ||
957 | #define SWAP(type, a, b) do {type c=(type)a; a=(type)b; b=(type)c;} while (0) | 913 | block_CHLD_HUP_ALRM(&omask); |
958 | |||
959 | Block_Using_Signals(omask); | ||
960 | /* | ||
961 | * sep->se_wait may be holding the pid of a daemon | ||
962 | * that we're waiting for. If so, don't overwrite | ||
963 | * it unless the config file explicitly says don't | ||
964 | * wait. | ||
965 | */ | ||
966 | if ( | ||
967 | #ifdef INETD_FEATURE_ENABLED | ||
968 | cp->se_bi == 0 && | ||
969 | #endif | ||
970 | (sep->se_wait == 1 || cp->se_wait == 0)) | ||
971 | sep->se_wait = cp->se_wait; | ||
972 | SWAP(int, cp->se_max, sep->se_max); | ||
973 | SWAP(char *, sep->se_user, cp->se_user); | ||
974 | SWAP(char *, sep->se_group, cp->se_group); | ||
975 | SWAP(char *, sep->se_server, cp->se_server); | ||
976 | for (i = 0; i < MAXARGV; i++) | ||
977 | SWAP(char *, sep->se_argv[i], cp->se_argv[i]); | ||
978 | #undef SWAP | ||
979 | |||
980 | #if ENABLE_FEATURE_INETD_RPC | 914 | #if ENABLE_FEATURE_INETD_RPC |
981 | if (isrpcservice(sep)) | 915 | if (is_rpc_service(sep)) |
982 | unregister_rpc(sep); | 916 | unregister_rpc(sep); |
983 | sep->se_rpcversl = cp->se_rpcversl; | 917 | sep->se_rpcver_lo = cp->se_rpcver_lo; |
984 | sep->se_rpcversh = cp->se_rpcversh; | 918 | sep->se_rpcver_hi = cp->se_rpcver_hi; |
985 | #endif | 919 | #endif |
986 | sigprocmask(SIG_UNBLOCK, &omask, NULL); | 920 | if (cp->se_wait == 0) { |
987 | freeconfig(cp); | 921 | /* New config says "nowait". If old one |
988 | } else { | 922 | * was "wait", we currently may be waiting |
989 | sep = enter(cp); | 923 | * for a child (and not accepting connects). |
924 | * Stop waiting, start listening again. | ||
925 | * (if it's not true, this op is harmless) */ | ||
926 | add_fd_to_set(sep->se_fd); | ||
927 | } | ||
928 | sep->se_wait = cp->se_wait; | ||
929 | sep->se_max = cp->se_max; | ||
930 | /* string fields need more love - we don't want to leak them */ | ||
931 | #define SWAP(type, a, b) do { type c = (type)a; a = (type)b; b = (type)c; } while (0) | ||
932 | SWAP(char*, sep->se_user, cp->se_user); | ||
933 | SWAP(char*, sep->se_group, cp->se_group); | ||
934 | SWAP(char*, sep->se_program, cp->se_program); | ||
935 | for (i = 0; i < MAXARGV; i++) | ||
936 | SWAP(char*, sep->se_argv[i], cp->se_argv[i]); | ||
937 | #undef SWAP | ||
938 | unblock_sigs(&omask); | ||
939 | free_servtab_strings(cp); | ||
990 | } | 940 | } |
941 | after_check: | ||
942 | /* cp->string_fields are consumed by insert_in_servlist() | ||
943 | * or freed at this point, cp itself is not yet freed. */ | ||
991 | sep->se_checked = 1; | 944 | sep->se_checked = 1; |
992 | 945 | ||
946 | /* create new len_and_sockaddr */ | ||
993 | switch (sep->se_family) { | 947 | switch (sep->se_family) { |
948 | struct sockaddr_un *sun; | ||
994 | case AF_UNIX: | 949 | case AF_UNIX: |
995 | if (sep->se_fd != -1) | 950 | /* we have poor infrastructure for AF_UNIX... */ |
996 | break; | ||
997 | (void) unlink(sep->se_service); | ||
998 | n = strlen(sep->se_service); | 951 | n = strlen(sep->se_service); |
999 | if (n > sizeof sep->se_ctrladdr_un.sun_path - 1) | 952 | if (n > sizeof(sun->sun_path) - 1) |
1000 | n = sizeof sep->se_ctrladdr_un.sun_path - 1; | 953 | n = sizeof(sun->sun_path) - 1; |
1001 | safe_strncpy(sep->se_ctrladdr_un.sun_path, sep->se_service, n + 1); | 954 | lsa = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un)); |
1002 | sep->se_ctrladdr_un.sun_family = AF_UNIX; | 955 | lsa->len = sizeof(struct sockaddr_un); |
1003 | sep->se_ctrladdr_size = n + sizeof sep->se_ctrladdr_un.sun_family; | 956 | sun = (struct sockaddr_un*)&lsa->u.sa; |
1004 | setup(sep); | 957 | sun->sun_family = AF_UNIX; |
958 | strncpy(sun->sun_path, sep->se_service, n); | ||
1005 | break; | 959 | break; |
1006 | case AF_INET: | ||
1007 | sep->se_ctrladdr_in.sin_family = AF_INET; | ||
1008 | /* se_ctrladdr_in was set in getconfigent */ | ||
1009 | sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in; | ||
1010 | 960 | ||
961 | default: /* case AF_INET, case AF_INET6 */ | ||
962 | n = bb_strtou(sep->se_service, NULL, 10); | ||
1011 | #if ENABLE_FEATURE_INETD_RPC | 963 | #if ENABLE_FEATURE_INETD_RPC |
1012 | if (isrpcservice(sep)) { | 964 | if (is_rpc_service(sep)) { |
1013 | struct rpcent *rp; | 965 | sep->se_rpcprog = n; |
1014 | // FIXME: atoi_or_else(str, 0) would be handy here | 966 | if (errno) { /* se_service is not numeric */ |
1015 | sep->se_rpcprog = atoi(sep->se_service); | 967 | struct rpcent *rp = getrpcbyname(sep->se_service); |
1016 | if (sep->se_rpcprog == 0) { | 968 | if (rp == NULL) { |
1017 | rp = getrpcbyname(sep->se_service); | ||
1018 | if (rp == 0) { | ||
1019 | bb_error_msg("%s: unknown rpc service", sep->se_service); | 969 | bb_error_msg("%s: unknown rpc service", sep->se_service); |
1020 | goto serv_unknown; | 970 | goto next_cp; |
1021 | } | 971 | } |
1022 | sep->se_rpcprog = rp->r_number; | 972 | sep->se_rpcprog = rp->r_number; |
1023 | } | 973 | } |
1024 | if (sep->se_fd == -1) | 974 | if (sep->se_fd == -1) |
1025 | setup(sep); | 975 | prepare_socket_fd(sep); |
1026 | if (sep->se_fd != -1) | 976 | if (sep->se_fd != -1) |
1027 | register_rpc(sep); | 977 | register_rpc(sep); |
1028 | } else | 978 | goto next_cp; |
1029 | #endif | ||
1030 | { | ||
1031 | uint16_t port = htons(atoi(sep->se_service)); | ||
1032 | // FIXME: atoi_or_else(str, 0) would be handy here | ||
1033 | if (!port) { | ||
1034 | /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname)); | ||
1035 | if (isdigit(protoname[strlen(protoname) - 1])) | ||
1036 | protoname[strlen(protoname) - 1] = '\0'; | ||
1037 | sp = getservbyname(sep->se_service, protoname); | ||
1038 | if (sp == 0) { | ||
1039 | bb_error_msg("%s/%s: unknown service", | ||
1040 | sep->se_service, sep->se_proto); | ||
1041 | goto serv_unknown; | ||
1042 | } | ||
1043 | port = sp->s_port; | ||
1044 | } | ||
1045 | if (port != sep->se_ctrladdr_in.sin_port) { | ||
1046 | sep->se_ctrladdr_in.sin_port = port; | ||
1047 | if (sep->se_fd != -1) { | ||
1048 | FD_CLR(sep->se_fd, &allsock); | ||
1049 | nsock--; | ||
1050 | (void) close(sep->se_fd); | ||
1051 | } | ||
1052 | sep->se_fd = -1; | ||
1053 | } | ||
1054 | if (sep->se_fd == -1) | ||
1055 | setup(sep); | ||
1056 | } | 979 | } |
1057 | break; | ||
1058 | #if ENABLE_FEATURE_IPV6 | ||
1059 | case AF_INET6: | ||
1060 | sep->se_ctrladdr_in6.sin6_family = AF_INET6; | ||
1061 | /* se_ctrladdr_in was set in getconfigent */ | ||
1062 | sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in6; | ||
1063 | |||
1064 | #if ENABLE_FEATURE_INETD_RPC | ||
1065 | if (isrpcservice(sep)) { | ||
1066 | struct rpcent *rp; | ||
1067 | |||
1068 | sep->se_rpcprog = atoi(sep->se_service); | ||
1069 | if (sep->se_rpcprog == 0) { | ||
1070 | rp = getrpcbyname(sep->se_service); | ||
1071 | if (rp == 0) { | ||
1072 | bb_error_msg("%s: unknown rpc service", sep->se_service); | ||
1073 | goto serv_unknown; | ||
1074 | } | ||
1075 | sep->se_rpcprog = rp->r_number; | ||
1076 | } | ||
1077 | if (sep->se_fd == -1) | ||
1078 | setup(sep); | ||
1079 | if (sep->se_fd != -1) | ||
1080 | register_rpc(sep); | ||
1081 | } else | ||
1082 | #endif | 980 | #endif |
1083 | { | 981 | /* what port to listen on? */ |
1084 | uint16_t port = htons(atoi(sep->se_service)); | 982 | port = htons(n); |
1085 | 983 | if (errno || n > 0xffff) { /* se_service is not numeric */ | |
1086 | if (!port) { | 984 | char protoname[4]; |
1087 | /*XXX*/ strncpy(protoname, sep->se_proto, sizeof(protoname)); | 985 | struct servent *sp; |
1088 | if (isdigit(protoname[strlen(protoname) - 1])) | 986 | /* can result only in "tcp" or "udp": */ |
1089 | protoname[strlen(protoname) - 1] = '\0'; | 987 | safe_strncpy(protoname, sep->se_proto, 4); |
1090 | sp = getservbyname(sep->se_service, protoname); | 988 | sp = getservbyname(sep->se_service, protoname); |
1091 | if (sp == 0) { | 989 | if (sp == NULL) { |
1092 | bb_error_msg("%s/%s: unknown service", | 990 | bb_error_msg("%s/%s: unknown service", |
1093 | sep->se_service, sep->se_proto); | 991 | sep->se_service, sep->se_proto); |
1094 | goto serv_unknown; | 992 | goto next_cp; |
1095 | } | ||
1096 | port = sp->s_port; | ||
1097 | } | 993 | } |
1098 | if (port != sep->se_ctrladdr_in6.sin6_port) { | 994 | port = sp->s_port; |
1099 | sep->se_ctrladdr_in6.sin6_port = port; | 995 | } |
1100 | if (sep->se_fd != -1) { | 996 | if (LONE_CHAR(sep->se_local_hostname, '*')) { |
1101 | FD_CLR(sep->se_fd, &allsock); | 997 | lsa = xzalloc_lsa(sep->se_family); |
1102 | nsock--; | 998 | set_nport(lsa, port); |
1103 | (void) close(sep->se_fd); | 999 | } else { |
1104 | } | 1000 | lsa = host_and_af2sockaddr(sep->se_local_hostname, |
1105 | sep->se_fd = -1; | 1001 | ntohs(port), sep->se_family); |
1002 | if (!lsa) { | ||
1003 | bb_error_msg("%s/%s: unknown host '%s'", | ||
1004 | sep->se_service, sep->se_proto, | ||
1005 | sep->se_local_hostname); | ||
1006 | goto next_cp; | ||
1106 | } | 1007 | } |
1107 | if (sep->se_fd == -1) | ||
1108 | setup(sep); | ||
1109 | } | 1008 | } |
1110 | break; | 1009 | break; |
1111 | #endif /* FEATURE_IPV6 */ | 1010 | } /* end of "switch (sep->se_family)" */ |
1112 | } | 1011 | |
1113 | serv_unknown: | 1012 | /* did lsa change? Then close/open */ |
1114 | if (cp->se_next != NULL) { | 1013 | if (sep->se_lsa == NULL |
1115 | servtab_t *tmp = cp; | 1014 | || lsa->len != sep->se_lsa->len |
1116 | 1015 | || memcmp(&lsa->u.sa, &sep->se_lsa->u.sa, lsa->len) != 0 | |
1117 | cp = cp->se_next; | 1016 | ) { |
1118 | free(tmp); | 1017 | remove_fd_from_set(sep->se_fd); |
1018 | maybe_close(sep->se_fd); | ||
1019 | free(sep->se_lsa); | ||
1020 | sep->se_lsa = lsa; | ||
1021 | sep->se_fd = -1; | ||
1119 | } else { | 1022 | } else { |
1120 | free(cp); | 1023 | free(lsa); |
1121 | cp = getconfigent(); | ||
1122 | } | 1024 | } |
1123 | } | 1025 | if (sep->se_fd == -1) |
1124 | endconfig(); | 1026 | prepare_socket_fd(sep); |
1125 | /* | 1027 | next_cp: |
1126 | * Purge anything not looked at above. | 1028 | sep = cp->se_next; |
1127 | */ | 1029 | free(cp); |
1128 | Block_Using_Signals(omask); | 1030 | cp = sep; |
1129 | sepp = &servtab; | 1031 | } /* end of "while (1) parse lines" */ |
1032 | close_config_file(); | ||
1033 | |||
1034 | /* Purge anything not looked at above - these are stale entries, | ||
1035 | * new config file doesnt have them. */ | ||
1036 | block_CHLD_HUP_ALRM(&omask); | ||
1037 | sepp = &serv_list; | ||
1130 | while ((sep = *sepp)) { | 1038 | while ((sep = *sepp)) { |
1131 | if (sep->se_checked) { | 1039 | if (sep->se_checked) { |
1132 | sepp = &sep->se_next; | 1040 | sepp = &sep->se_next; |
1133 | continue; | 1041 | continue; |
1134 | } | 1042 | } |
1135 | *sepp = sep->se_next; | 1043 | *sepp = sep->se_next; |
1136 | if (sep->se_fd != -1) { | 1044 | remove_fd_from_set(sep->se_fd); |
1137 | FD_CLR(sep->se_fd, &allsock); | 1045 | maybe_close(sep->se_fd); |
1138 | nsock--; | ||
1139 | (void) close(sep->se_fd); | ||
1140 | } | ||
1141 | #if ENABLE_FEATURE_INETD_RPC | 1046 | #if ENABLE_FEATURE_INETD_RPC |
1142 | if (isrpcservice(sep)) | 1047 | if (is_rpc_service(sep)) |
1143 | unregister_rpc(sep); | 1048 | unregister_rpc(sep); |
1144 | #endif | 1049 | #endif |
1145 | if (sep->se_family == AF_UNIX) | 1050 | if (sep->se_family == AF_UNIX) |
1146 | (void) unlink(sep->se_service); | 1051 | unlink(sep->se_service); |
1147 | freeconfig(sep); | 1052 | free_servtab_strings(sep); |
1148 | free(sep); | 1053 | free(sep); |
1149 | } | 1054 | } |
1150 | sigprocmask(SIG_UNBLOCK, &omask, NULL); | 1055 | unblock_sigs(&omask); |
1151 | } | 1056 | } |
1152 | 1057 | ||
1153 | 1058 | static void reap_child(int sig ATTRIBUTE_UNUSED) | |
1154 | static void reapchild(int sig ATTRIBUTE_UNUSED) | ||
1155 | { | 1059 | { |
1156 | pid_t pid; | 1060 | pid_t pid; |
1157 | int save_errno = errno, status; | 1061 | int status; |
1158 | servtab_t *sep; | 1062 | servtab_t *sep; |
1063 | int save_errno = errno; | ||
1159 | 1064 | ||
1160 | for (;;) { | 1065 | for (;;) { |
1161 | pid = wait_any_nohang(&status); | 1066 | pid = wait_any_nohang(&status); |
1162 | if (pid <= 0) | 1067 | if (pid <= 0) |
1163 | break; | 1068 | break; |
1164 | for (sep = servtab; sep; sep = sep->se_next) | 1069 | for (sep = serv_list; sep; sep = sep->se_next) |
1165 | if (sep->se_wait == pid) { | 1070 | if (sep->se_wait == pid) { |
1071 | /* One of our "wait" services */ | ||
1166 | if (WIFEXITED(status) && WEXITSTATUS(status)) | 1072 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
1167 | bb_error_msg("%s: exit status 0x%x", | 1073 | bb_error_msg("%s: exit status 0x%x", |
1168 | sep->se_server, WEXITSTATUS(status)); | 1074 | sep->se_program, WEXITSTATUS(status)); |
1169 | else if (WIFSIGNALED(status)) | 1075 | else if (WIFSIGNALED(status)) |
1170 | bb_error_msg("%s: exit signal 0x%x", | 1076 | bb_error_msg("%s: exit signal 0x%x", |
1171 | sep->se_server, WTERMSIG(status)); | 1077 | sep->se_program, WTERMSIG(status)); |
1172 | sep->se_wait = 1; | 1078 | sep->se_wait = 1; |
1173 | FD_SET(sep->se_fd, &allsock); | 1079 | add_fd_to_set(sep->se_fd); |
1174 | nsock++; | ||
1175 | } | 1080 | } |
1176 | } | 1081 | } |
1177 | errno = save_errno; | 1082 | errno = save_errno; |
1178 | } | 1083 | } |
1179 | 1084 | ||
1180 | static void retry(int sig ATTRIBUTE_UNUSED) | 1085 | static void retry_network_setup(int sig ATTRIBUTE_UNUSED) |
1181 | { | 1086 | { |
1182 | servtab_t *sep; | 1087 | servtab_t *sep; |
1183 | 1088 | ||
1184 | timingout = 0; | 1089 | alarm_armed = 0; |
1185 | for (sep = servtab; sep; sep = sep->se_next) { | 1090 | for (sep = serv_list; sep; sep = sep->se_next) { |
1186 | if (sep->se_fd == -1) { | 1091 | if (sep->se_fd == -1) { |
1187 | switch (sep->se_family) { | 1092 | prepare_socket_fd(sep); |
1188 | case AF_UNIX: | ||
1189 | case AF_INET: | ||
1190 | #if ENABLE_FEATURE_IPV6 | ||
1191 | case AF_INET6: | ||
1192 | #endif | ||
1193 | setup(sep); | ||
1194 | #if ENABLE_FEATURE_INETD_RPC | 1093 | #if ENABLE_FEATURE_INETD_RPC |
1195 | if (sep->se_fd != -1 && isrpcservice(sep)) | 1094 | if (sep->se_fd != -1 && is_rpc_service(sep)) |
1196 | register_rpc(sep); | 1095 | register_rpc(sep); |
1197 | #endif | 1096 | #endif |
1198 | break; | ||
1199 | } | ||
1200 | } | 1097 | } |
1201 | } | 1098 | } |
1202 | } | 1099 | } |
1203 | 1100 | ||
1204 | static void goaway(int sig ATTRIBUTE_UNUSED) | 1101 | static void clean_up_and_exit(int sig ATTRIBUTE_UNUSED) |
1205 | { | 1102 | { |
1206 | servtab_t *sep; | 1103 | servtab_t *sep; |
1207 | 1104 | ||
1208 | /* XXX signal race walking sep list */ | 1105 | /* XXX signal race walking sep list */ |
1209 | for (sep = servtab; sep; sep = sep->se_next) { | 1106 | for (sep = serv_list; sep; sep = sep->se_next) { |
1210 | if (sep->se_fd == -1) | 1107 | if (sep->se_fd == -1) |
1211 | continue; | 1108 | continue; |
1212 | 1109 | ||
1213 | switch (sep->se_family) { | 1110 | switch (sep->se_family) { |
1214 | case AF_UNIX: | 1111 | case AF_UNIX: |
1215 | (void) unlink(sep->se_service); | 1112 | unlink(sep->se_service); |
1216 | break; | 1113 | break; |
1217 | case AF_INET: | 1114 | default: /* case AF_INET, AF_INET6 */ |
1218 | #if ENABLE_FEATURE_IPV6 | ||
1219 | case AF_INET6: | ||
1220 | #endif | ||
1221 | #if ENABLE_FEATURE_INETD_RPC | 1115 | #if ENABLE_FEATURE_INETD_RPC |
1222 | if (sep->se_wait == 1 && isrpcservice(sep)) | 1116 | if (sep->se_wait == 1 && is_rpc_service(sep)) |
1223 | unregister_rpc(sep); /* XXX signal race */ | 1117 | unregister_rpc(sep); /* XXX signal race */ |
1224 | #endif | 1118 | #endif |
1225 | break; | 1119 | break; |
1226 | } | 1120 | } |
1227 | (void) close(sep->se_fd); | 1121 | if (ENABLE_FEATURE_CLEAN_UP) |
1122 | close(sep->se_fd); | ||
1228 | } | 1123 | } |
1229 | remove_pidfile(_PATH_INETDPID); | 1124 | remove_pidfile(_PATH_INETDPID); |
1230 | exit(0); | 1125 | exit(0); |
1231 | } | 1126 | } |
1232 | 1127 | ||
1233 | |||
1234 | #ifdef INETD_SETPROCTITLE | ||
1235 | |||
1236 | static void | ||
1237 | inetd_setproctitle(char *a, int s) | ||
1238 | { | ||
1239 | socklen_t size; | ||
1240 | char *cp; | ||
1241 | struct sockaddr_in prt_sin; | ||
1242 | char buf[80]; | ||
1243 | |||
1244 | cp = Argv[0]; | ||
1245 | size = sizeof(prt_sin); | ||
1246 | (void) snprintf(buf, sizeof buf, "-%s", a); | ||
1247 | if (getpeername(s, (struct sockaddr *) &prt_sin, &size) == 0) { | ||
1248 | char *sa = inet_ntoa(prt_sin.sin_addr); | ||
1249 | |||
1250 | buf[sizeof(buf) - 1 - strlen(sa) - 3] = '\0'; | ||
1251 | strcat(buf, " ["); | ||
1252 | strcat(buf, sa); | ||
1253 | strcat(buf, "]"); | ||
1254 | } | ||
1255 | strncpy(cp, buf, LastArg - cp); | ||
1256 | cp += strlen(cp); | ||
1257 | while (cp < LastArg) | ||
1258 | *cp++ = ' '; | ||
1259 | } | ||
1260 | #endif | ||
1261 | |||
1262 | |||
1263 | int inetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 1128 | int inetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
1264 | int inetd_main(int argc, char **argv) | 1129 | int inetd_main(int argc, char **argv) |
1265 | { | 1130 | { |
1266 | servtab_t *sep; | 1131 | struct sigaction sa, saved_pipe_handler; |
1132 | servtab_t *sep, *sep2; | ||
1267 | struct passwd *pwd; | 1133 | struct passwd *pwd; |
1268 | struct group *grp = NULL; | 1134 | struct group *grp = grp; /* for compiler */ |
1269 | int tmpint; | ||
1270 | struct sigaction sa, sapipe; | ||
1271 | int opt; | 1135 | int opt; |
1272 | pid_t pid; | 1136 | pid_t pid; |
1273 | char buf[50]; | 1137 | sigset_t omask; |
1274 | char *stoomany; | ||
1275 | sigset_t omask, wait_mask; | ||
1276 | #ifdef INETD_SETPROCTITLE | ||
1277 | char **envp; | ||
1278 | #endif | ||
1279 | 1138 | ||
1280 | INIT_G(); | 1139 | INIT_G(); |
1281 | 1140 | ||
1282 | #ifdef INETD_SETPROCTITLE | 1141 | real_uid = getuid(); |
1283 | envp = environ; | 1142 | if (real_uid != 0) /* run by non-root user */ |
1284 | Argv = argv; | ||
1285 | if (envp == 0 || *envp == 0) | ||
1286 | envp = argv; | ||
1287 | while (*envp) | ||
1288 | envp++; | ||
1289 | LastArg = envp[-1] + strlen(envp[-1]); | ||
1290 | #endif | ||
1291 | |||
1292 | uid = getuid(); | ||
1293 | if (uid != 0) | ||
1294 | config_filename = NULL; | 1143 | config_filename = NULL; |
1295 | 1144 | ||
1296 | opt = getopt32(argv, "R:f", &stoomany); | 1145 | opt_complementary = "R+:q+"; /* -q N, -R N */ |
1297 | if (opt & 1) | 1146 | opt = getopt32(argv, "R:feq:", &max_concurrency, &global_queuelen); |
1298 | toomany = xatoi_u(stoomany); | ||
1299 | argv += optind; | 1147 | argv += optind; |
1300 | argc -= optind; | 1148 | //argc -= optind; |
1301 | if (argc) | 1149 | if (argv[0]) |
1302 | config_filename = argv[0]; | 1150 | config_filename = argv[0]; |
1303 | if (config_filename == NULL) | 1151 | if (config_filename == NULL) |
1304 | bb_error_msg_and_die("non-root must specify a config file"); | 1152 | bb_error_msg_and_die("non-root must specify config file"); |
1305 | |||
1306 | if (!(opt & 2)) | 1153 | if (!(opt & 2)) |
1307 | bb_daemonize_or_rexec(0, argv - optind); | 1154 | bb_daemonize_or_rexec(0, argv - optind); |
1308 | else | 1155 | else |
1309 | bb_sanitize_stdio(); | 1156 | bb_sanitize_stdio(); |
1310 | openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON); | 1157 | if (!(opt & 4)) { |
1311 | logmode = LOGMODE_SYSLOG; | 1158 | openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON); |
1159 | logmode = LOGMODE_SYSLOG; | ||
1160 | } | ||
1312 | 1161 | ||
1313 | if (uid == 0) { | 1162 | if (real_uid == 0) { |
1314 | /* If run by hand, ensure groups vector gets trashed */ | 1163 | /* run by root, ensure groups vector gets trashed */ |
1315 | gid_t gid = getgid(); | 1164 | gid_t gid = getgid(); |
1316 | setgroups(1, &gid); | 1165 | setgroups(1, &gid); |
1317 | } | 1166 | } |
1318 | 1167 | ||
1319 | write_pidfile(_PATH_INETDPID); | 1168 | write_pidfile(_PATH_INETDPID); |
1320 | 1169 | ||
1321 | if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) { | 1170 | /* never fails under Linux (except if you pass it bad arguments) */ |
1322 | bb_perror_msg("getrlimit"); | 1171 | getrlimit(RLIMIT_NOFILE, &rlim_ofile); |
1323 | } else { | 1172 | rlim_ofile_cur = rlim_ofile.rlim_cur; |
1324 | rlim_ofile_cur = rlim_ofile.rlim_cur; | 1173 | if (rlim_ofile_cur == RLIM_INFINITY) /* ! */ |
1325 | if (rlim_ofile_cur == RLIM_INFINITY) /* ! */ | 1174 | rlim_ofile_cur = OPEN_MAX; |
1326 | rlim_ofile_cur = OPEN_MAX; | ||
1327 | } | ||
1328 | 1175 | ||
1329 | memset((char *) &sa, 0, sizeof(sa)); | 1176 | memset(&sa, 0, sizeof(sa)); |
1330 | /*sigemptyset(&sa.sa_mask); - memset did it */ | 1177 | /*sigemptyset(&sa.sa_mask); - memset did it */ |
1331 | sigaddset(&sa.sa_mask, SIGALRM); | 1178 | sigaddset(&sa.sa_mask, SIGALRM); |
1332 | sigaddset(&sa.sa_mask, SIGCHLD); | 1179 | sigaddset(&sa.sa_mask, SIGCHLD); |
1333 | sigaddset(&sa.sa_mask, SIGHUP); | 1180 | sigaddset(&sa.sa_mask, SIGHUP); |
1334 | sa.sa_handler = retry; | 1181 | sa.sa_handler = retry_network_setup; |
1335 | sigaction(SIGALRM, &sa, NULL); | 1182 | sigaction(SIGALRM, &sa, NULL); |
1336 | config(SIGHUP); | 1183 | sa.sa_handler = reread_config_file; |
1337 | sa.sa_handler = config; | ||
1338 | sigaction(SIGHUP, &sa, NULL); | 1184 | sigaction(SIGHUP, &sa, NULL); |
1339 | sa.sa_handler = reapchild; | 1185 | sa.sa_handler = reap_child; |
1340 | sigaction(SIGCHLD, &sa, NULL); | 1186 | sigaction(SIGCHLD, &sa, NULL); |
1341 | sa.sa_handler = goaway; | 1187 | sa.sa_handler = clean_up_and_exit; |
1342 | sigaction(SIGTERM, &sa, NULL); | 1188 | sigaction(SIGTERM, &sa, NULL); |
1343 | sa.sa_handler = goaway; | 1189 | sa.sa_handler = clean_up_and_exit; |
1344 | sigaction(SIGINT, &sa, NULL); | 1190 | sigaction(SIGINT, &sa, NULL); |
1345 | sa.sa_handler = SIG_IGN; | 1191 | sa.sa_handler = SIG_IGN; |
1346 | sigaction(SIGPIPE, &sa, &sapipe); | 1192 | sigaction(SIGPIPE, &sa, &saved_pipe_handler); |
1347 | memset(&wait_mask, 0, sizeof(wait_mask)); | ||
1348 | { | ||
1349 | /* space for daemons to overwrite environment for ps */ | ||
1350 | #define DUMMYSIZE 100 | ||
1351 | char dummy[DUMMYSIZE]; | ||
1352 | 1193 | ||
1353 | (void) memset(dummy, 'x', DUMMYSIZE - 1); | 1194 | reread_config_file(SIGHUP); /* load config from file */ |
1354 | dummy[DUMMYSIZE - 1] = '\0'; | ||
1355 | |||
1356 | (void) setenv("inetd_dummy", dummy, 1); | ||
1357 | } | ||
1358 | 1195 | ||
1359 | for (;;) { | 1196 | for (;;) { |
1360 | int n, ctrl = -1; | 1197 | int ready_fd_cnt; |
1198 | int ctrl, accepted_fd; | ||
1361 | fd_set readable; | 1199 | fd_set readable; |
1362 | 1200 | ||
1363 | if (nsock == 0) { | 1201 | if (maxsock < 0) |
1364 | Block_Using_Signals(omask); | 1202 | recalculate_maxsock(); |
1365 | while (nsock == 0) | ||
1366 | sigsuspend(&wait_mask); | ||
1367 | sigprocmask(SIG_UNBLOCK, &omask, NULL); | ||
1368 | } | ||
1369 | 1203 | ||
1370 | readable = allsock; | 1204 | readable = allsock; /* struct copy */ |
1371 | n = select(maxsock + 1, &readable, NULL, NULL, NULL); | 1205 | /* if there are no fds to wait on, we will block |
1372 | if (n <= 0) { | 1206 | * until signal wakes us up */ |
1373 | if (n < 0 && errno != EINTR) { | 1207 | ready_fd_cnt = select(maxsock + 1, &readable, NULL, NULL, NULL); |
1208 | if (ready_fd_cnt < 0) { | ||
1209 | if (errno != EINTR) { | ||
1374 | bb_perror_msg("select"); | 1210 | bb_perror_msg("select"); |
1375 | sleep(1); | 1211 | sleep(1); |
1376 | } | 1212 | } |
1377 | continue; | 1213 | continue; |
1378 | } | 1214 | } |
1379 | 1215 | ||
1380 | for (sep = servtab; n && sep; sep = sep->se_next) { | 1216 | for (sep = serv_list; ready_fd_cnt && sep; sep = sep->se_next) { |
1381 | if (sep->se_fd == -1 || !FD_ISSET(sep->se_fd, &readable)) | 1217 | if (sep->se_fd == -1 || !FD_ISSET(sep->se_fd, &readable)) |
1382 | continue; | 1218 | continue; |
1383 | 1219 | ||
1384 | n--; | 1220 | ready_fd_cnt--; |
1221 | ctrl = sep->se_fd; | ||
1222 | accepted_fd = -1; | ||
1385 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { | 1223 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { |
1386 | ctrl = accept(sep->se_fd, NULL, NULL); | 1224 | ctrl = accepted_fd = accept(sep->se_fd, NULL, NULL); |
1387 | if (ctrl < 0) { | 1225 | if (ctrl < 0) { |
1388 | if (errno == EINTR) | 1226 | if (errno != EINTR) |
1389 | continue; | 1227 | bb_perror_msg("accept (for %s)", sep->se_service); |
1390 | bb_perror_msg("accept (for %s)", sep->se_service); | ||
1391 | continue; | 1228 | continue; |
1392 | } | 1229 | } |
1393 | if (sep->se_family == AF_INET && sep->se_socktype == SOCK_STREAM) { | 1230 | } |
1394 | struct sockaddr_in peer; | ||
1395 | socklen_t plen = sizeof(peer); | ||
1396 | |||
1397 | if (getpeername(ctrl, (struct sockaddr *) &peer, &plen) < 0) { | ||
1398 | bb_error_msg("cannot getpeername"); | ||
1399 | close(ctrl); | ||
1400 | continue; | ||
1401 | } | ||
1402 | if (ntohs(peer.sin_port) == 20) { | ||
1403 | /* XXX ftp bounce */ | ||
1404 | close(ctrl); | ||
1405 | continue; | ||
1406 | } | ||
1407 | } | ||
1408 | } else | ||
1409 | ctrl = sep->se_fd; | ||
1410 | 1231 | ||
1411 | Block_Using_Signals(omask); | 1232 | block_CHLD_HUP_ALRM(&omask); |
1412 | pid = 0; | 1233 | pid = 0; |
1413 | #ifdef INETD_FEATURE_ENABLED | 1234 | #ifdef INETD_BUILTINS_ENABLED |
1414 | if (sep->se_bi == 0 || sep->se_bi->bi_fork) | 1235 | /* do we need to fork? */ |
1236 | if (sep->se_builtin == NULL | ||
1237 | || (sep->se_socktype == SOCK_STREAM | ||
1238 | && sep->se_builtin->bi_fork)) | ||
1415 | #endif | 1239 | #endif |
1416 | { | 1240 | { |
1417 | if (sep->se_count++ == 0) | 1241 | if (sep->se_max != 0) { |
1418 | (void) gettimeofday(&sep->se_time, NULL); | 1242 | if (++sep->se_count == 1) |
1419 | else if (toomany > 0 && sep->se_count >= sep->se_max) { | 1243 | sep->se_time = monotonic_sec(); |
1420 | struct timeval now; | 1244 | else if (sep->se_count >= sep->se_max) { |
1421 | 1245 | unsigned now = monotonic_sec(); | |
1422 | (void) gettimeofday(&now, NULL); | 1246 | /* did we accumulate se_max connects too quickly? */ |
1423 | if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) { | 1247 | if (now - sep->se_time <= CNT_INTERVAL) { |
1424 | sep->se_time = now; | 1248 | bb_error_msg("%s/%s: too many connections, pausing", |
1425 | sep->se_count = 1; | 1249 | sep->se_service, sep->se_proto); |
1426 | } else { | 1250 | remove_fd_from_set(sep->se_fd); |
1427 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) | 1251 | close(sep->se_fd); |
1428 | close(ctrl); | 1252 | sep->se_fd = -1; |
1429 | if (sep->se_family == AF_INET && | 1253 | sep->se_count = 0; |
1430 | ntohs(sep->se_ctrladdr_in.sin_port) >= IPPORT_RESERVED) { | 1254 | rearm_alarm(); /* will revive it in RETRYTIME sec */ |
1431 | /* | 1255 | unblock_sigs(&omask); |
1432 | * Cannot close it -- there are | 1256 | maybe_close(accepted_fd); |
1433 | * thieves on the system. | 1257 | continue; /* -> check next fd in fd set */ |
1434 | * Simply ignore the connection. | ||
1435 | */ | ||
1436 | --sep->se_count; | ||
1437 | continue; | ||
1438 | } | 1258 | } |
1439 | bb_error_msg("%s/%s server failing (looping), service terminated", | ||
1440 | sep->se_service, sep->se_proto); | ||
1441 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) | ||
1442 | close(ctrl); | ||
1443 | FD_CLR(sep->se_fd, &allsock); | ||
1444 | (void) close(sep->se_fd); | ||
1445 | sep->se_fd = -1; | ||
1446 | sep->se_count = 0; | 1259 | sep->se_count = 0; |
1447 | nsock--; | ||
1448 | sigprocmask(SIG_UNBLOCK, &omask, NULL); | ||
1449 | if (!timingout) { | ||
1450 | timingout = 1; | ||
1451 | alarm(RETRYTIME); | ||
1452 | } | ||
1453 | continue; | ||
1454 | } | 1260 | } |
1455 | } | 1261 | } |
1456 | pid = fork(); | 1262 | /* on NOMMU, streamed echo, chargen and discard |
1263 | * builtins wouldn't work, but they are | ||
1264 | * not allowed on NOMMU (ifdefed out) */ | ||
1265 | #ifdef INETD_BUILTINS_ENABLED | ||
1266 | if (BB_MMU && sep->se_builtin) | ||
1267 | pid = fork(); | ||
1268 | else | ||
1269 | #endif | ||
1270 | pid = vfork(); | ||
1271 | |||
1272 | if (pid < 0) { /* fork error */ | ||
1273 | bb_perror_msg("fork"); | ||
1274 | sleep(1); | ||
1275 | unblock_sigs(&omask); | ||
1276 | maybe_close(accepted_fd); | ||
1277 | continue; /* -> check next fd in fd set */ | ||
1278 | } | ||
1279 | if (pid == 0) | ||
1280 | pid--; /* -1: "we did fork and we are child" */ | ||
1457 | } | 1281 | } |
1458 | if (pid < 0) { | 1282 | /* if pid == 0 here, we never forked */ |
1459 | bb_perror_msg("fork"); | 1283 | |
1460 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) | 1284 | if (pid > 0) { /* parent */ |
1461 | close(ctrl); | 1285 | if (sep->se_wait) { |
1462 | sigprocmask(SIG_UNBLOCK, &omask, NULL); | 1286 | sep->se_wait = pid; |
1463 | sleep(1); | 1287 | remove_fd_from_set(sep->se_fd); |
1464 | continue; | 1288 | /* we passed listening socket to child, |
1289 | * will wait for child to terminate */ | ||
1290 | } | ||
1291 | unblock_sigs(&omask); | ||
1292 | maybe_close(accepted_fd); | ||
1293 | continue; /* -> check next fd in fd set */ | ||
1465 | } | 1294 | } |
1466 | if (pid && sep->se_wait) { | 1295 | |
1467 | sep->se_wait = pid; | 1296 | /* we are either child or didn't fork at all */ |
1468 | FD_CLR(sep->se_fd, &allsock); | 1297 | #ifdef INETD_BUILTINS_ENABLED |
1469 | nsock--; | 1298 | if (sep->se_builtin) { |
1299 | if (pid) { /* "pid" is -1: we did fork */ | ||
1300 | close(sep->se_fd); /* listening socket */ | ||
1301 | logmode = 0; /* make xwrite etc silent */ | ||
1302 | } | ||
1303 | unblock_sigs(&omask); | ||
1304 | if (sep->se_socktype == SOCK_STREAM) | ||
1305 | sep->se_builtin->bi_stream_fn(ctrl, sep); | ||
1306 | else | ||
1307 | sep->se_builtin->bi_dgram_fn(ctrl, sep); | ||
1308 | if (pid) /* we did fork */ | ||
1309 | _exit(0); | ||
1310 | maybe_close(accepted_fd); | ||
1311 | continue; /* -> check next fd in fd set */ | ||
1470 | } | 1312 | } |
1471 | sigprocmask(SIG_UNBLOCK, &omask, NULL); | ||
1472 | if (pid == 0) { | ||
1473 | #ifdef INETD_FEATURE_ENABLED | ||
1474 | if (sep->se_bi) { | ||
1475 | (*sep->se_bi->bi_fn)(ctrl, sep); | ||
1476 | } else | ||
1477 | #endif | 1313 | #endif |
1478 | { | 1314 | /* child. prepare env and exec program */ |
1479 | pwd = getpwnam(sep->se_user); | 1315 | setsid(); |
1480 | if (pwd == NULL) { | 1316 | pwd = getpwnam(sep->se_user); |
1481 | bb_error_msg("getpwnam: %s: no such user", sep->se_user); | 1317 | if (pwd == NULL) { |
1482 | goto do_exit1; | 1318 | bb_error_msg("%s: no such user", sep->se_user); |
1483 | } | 1319 | goto do_exit1; |
1484 | if (setsid() < 0) | 1320 | } |
1485 | bb_perror_msg("%s: setsid", sep->se_service); | 1321 | if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) { |
1486 | if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) { | 1322 | bb_error_msg("%s: no such group", sep->se_group); |
1487 | bb_error_msg("getgrnam: %s: no such group", sep->se_group); | 1323 | goto do_exit1; |
1488 | goto do_exit1; | 1324 | } |
1489 | } | 1325 | if (real_uid != 0 && real_uid != pwd->pw_uid) { |
1490 | if (uid != 0) { | 1326 | /* a user running private inetd */ |
1491 | /* a user running private inetd */ | 1327 | bb_error_msg("non-root must run services as himself"); |
1492 | if (uid != pwd->pw_uid) | 1328 | goto do_exit1; |
1493 | _exit(1); | 1329 | } |
1494 | } else if (pwd->pw_uid) { | 1330 | if (pwd->pw_uid) { |
1495 | if (sep->se_group) | 1331 | if (sep->se_group) |
1496 | pwd->pw_gid = grp->gr_gid; | 1332 | pwd->pw_gid = grp->gr_gid; |
1497 | xsetgid((gid_t) pwd->pw_gid); | 1333 | xsetgid(pwd->pw_gid); |
1498 | initgroups(pwd->pw_name, pwd->pw_gid); | 1334 | initgroups(pwd->pw_name, pwd->pw_gid); |
1499 | xsetuid((uid_t) pwd->pw_uid); | 1335 | xsetuid(pwd->pw_uid); |
1500 | } else if (sep->se_group) { | 1336 | } else if (sep->se_group) { |
1501 | xsetgid(grp->gr_gid); | 1337 | xsetgid(grp->gr_gid); |
1502 | setgroups(1, &grp->gr_gid); | 1338 | setgroups(1, &grp->gr_gid); |
1503 | } | ||
1504 | dup2(ctrl, 0); | ||
1505 | if (ctrl) close(ctrl); | ||
1506 | dup2(0, 1); | ||
1507 | dup2(0, 2); | ||
1508 | if (rlim_ofile.rlim_cur != rlim_ofile_cur) | ||
1509 | if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) | ||
1510 | bb_perror_msg("setrlimit"); | ||
1511 | closelog(); | ||
1512 | for (tmpint = rlim_ofile_cur - 1; --tmpint > 2;) | ||
1513 | (void) close(tmpint); | ||
1514 | sigaction(SIGPIPE, &sapipe, NULL); | ||
1515 | execv(sep->se_server, sep->se_argv); | ||
1516 | bb_perror_msg("execv %s", sep->se_server); | ||
1517 | do_exit1: | ||
1518 | if (sep->se_socktype != SOCK_STREAM) | ||
1519 | recv(0, buf, sizeof(buf), 0); | ||
1520 | _exit(1); | ||
1521 | } | ||
1522 | } | 1339 | } |
1523 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) | 1340 | if (rlim_ofile.rlim_cur != rlim_ofile_cur) |
1524 | close(ctrl); | 1341 | if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) |
1342 | bb_perror_msg("setrlimit"); | ||
1343 | closelog(); | ||
1344 | xmove_fd(ctrl, 0); | ||
1345 | dup2(0, 1); | ||
1346 | dup2(0, 2); | ||
1347 | /* NB: among others, this loop closes listening socket | ||
1348 | * for nowait stream children */ | ||
1349 | for (sep2 = serv_list; sep2; sep2 = sep2->se_next) | ||
1350 | maybe_close(sep2->se_fd); | ||
1351 | sigaction(SIGPIPE, &saved_pipe_handler, NULL); | ||
1352 | unblock_sigs(&omask); | ||
1353 | BB_EXECVP(sep->se_program, sep->se_argv); | ||
1354 | bb_perror_msg("exec %s", sep->se_program); | ||
1355 | do_exit1: | ||
1356 | /* eat packet in udp case */ | ||
1357 | if (sep->se_socktype != SOCK_STREAM) | ||
1358 | recv(0, line, LINE_SIZE, MSG_DONTWAIT); | ||
1359 | _exit(1); | ||
1525 | } /* for (sep = servtab...) */ | 1360 | } /* for (sep = servtab...) */ |
1526 | } /* for (;;) */ | 1361 | } /* for (;;) */ |
1527 | } | 1362 | } |
@@ -1529,94 +1364,61 @@ int inetd_main(int argc, char **argv) | |||
1529 | /* | 1364 | /* |
1530 | * Internet services provided internally by inetd: | 1365 | * Internet services provided internally by inetd: |
1531 | */ | 1366 | */ |
1532 | #define BUFSIZE 4096 | ||
1533 | |||
1534 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO || \ | ||
1535 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN || \ | ||
1536 | ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME | ||
1537 | static int dg_badinput(struct sockaddr_in *dg_sin) | ||
1538 | { | ||
1539 | if (ntohs(dg_sin->sin_port) < IPPORT_RESERVED) | ||
1540 | return 1; | ||
1541 | if (dg_sin->sin_addr.s_addr == htonl(INADDR_BROADCAST)) | ||
1542 | return 1; | ||
1543 | /* XXX compare against broadcast addresses in SIOCGIFCONF list? */ | ||
1544 | return 0; | ||
1545 | } | ||
1546 | #endif | ||
1547 | |||
1548 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO | 1367 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO |
1549 | /* Echo service -- echo data back */ | 1368 | /* Echo service -- echo data back. */ |
1550 | /* ARGSUSED */ | 1369 | /* ARGSUSED */ |
1551 | static void | 1370 | static void echo_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
1552 | echo_stream(int s, servtab_t *sep) | ||
1553 | { | 1371 | { |
1554 | char buffer[BUFSIZE]; | 1372 | #if BB_MMU |
1555 | int i; | ||
1556 | |||
1557 | inetd_setproctitle(sep->se_service, s); | ||
1558 | while (1) { | 1373 | while (1) { |
1559 | i = read(s, buffer, sizeof(buffer)); | 1374 | ssize_t sz = safe_read(s, line, LINE_SIZE); |
1560 | if (i <= 0) break; | 1375 | if (sz <= 0) |
1561 | /* FIXME: this isnt correct - safe_write()? */ | 1376 | break; |
1562 | if (write(s, buffer, i) <= 0) break; | 1377 | xwrite(s, line, sz); |
1563 | } | 1378 | } |
1564 | exit(0); | 1379 | #else |
1380 | static const char *const args[] = { "cat", NULL }; | ||
1381 | BB_EXECVP("cat", (char**)args); | ||
1382 | _exit(1); | ||
1383 | #endif | ||
1565 | } | 1384 | } |
1566 | 1385 | static void echo_dg(int s, servtab_t *sep) | |
1567 | /* Echo service -- echo data back */ | ||
1568 | /* ARGSUSED */ | ||
1569 | static void | ||
1570 | echo_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | ||
1571 | { | 1386 | { |
1572 | char buffer[BUFSIZE]; | 1387 | enum { BUFSIZE = 12*1024 }; /* for jumbo sized packets! :) */ |
1573 | int i; | 1388 | char *buf = xmalloc(BUFSIZE); /* too big for stack */ |
1574 | socklen_t size; | 1389 | int sz; |
1575 | /* struct sockaddr_storage ss; */ | 1390 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
1576 | struct sockaddr sa; | 1391 | |
1577 | 1392 | lsa->len = sep->se_lsa->len; | |
1578 | size = sizeof(sa); | 1393 | /* dgram builtins are non-forking - DONT BLOCK! */ |
1579 | i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size); | 1394 | sz = recvfrom(s, buf, BUFSIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len); |
1580 | if (i < 0) | 1395 | if (sz > 0) |
1581 | return; | 1396 | sendto(s, buf, sz, 0, &lsa->u.sa, lsa->len); |
1582 | if (dg_badinput((struct sockaddr_in *) &sa)) | 1397 | free(buf); |
1583 | return; | ||
1584 | (void) sendto(s, buffer, i, 0, &sa, sizeof(sa)); | ||
1585 | } | 1398 | } |
1586 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_ECHO */ | 1399 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_ECHO */ |
1587 | 1400 | ||
1401 | |||
1588 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD | 1402 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD |
1589 | /* Discard service -- ignore data */ | 1403 | /* Discard service -- ignore data. MMU arches only. */ |
1590 | /* ARGSUSED */ | 1404 | /* ARGSUSED */ |
1591 | static void | 1405 | static void discard_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
1592 | discard_stream(int s, servtab_t *sep) | ||
1593 | { | 1406 | { |
1594 | char buffer[BUFSIZE]; | 1407 | while (safe_read(s, line, LINE_SIZE) > 0) |
1595 | 1408 | continue; | |
1596 | inetd_setproctitle(sep->se_service, s); | ||
1597 | while (1) { | ||
1598 | if (safe_read(s, buffer, sizeof(buffer)) <= 0) | ||
1599 | exit(0); | ||
1600 | } | ||
1601 | } | 1409 | } |
1602 | |||
1603 | /* Discard service -- ignore data */ | ||
1604 | /* ARGSUSED */ | 1410 | /* ARGSUSED */ |
1605 | static void | 1411 | static void discard_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
1606 | discard_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | ||
1607 | { | 1412 | { |
1608 | char buffer[BUFSIZE]; | 1413 | /* dgram builtins are non-forking - DONT BLOCK! */ |
1609 | 1414 | recv(s, line, LINE_SIZE, MSG_DONTWAIT); | |
1610 | (void) read(s, buffer, sizeof(buffer)); | ||
1611 | } | 1415 | } |
1612 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */ | 1416 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */ |
1613 | 1417 | ||
1614 | 1418 | ||
1615 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN | 1419 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
1616 | #define LINESIZ 72 | 1420 | #define LINESIZ 72 |
1617 | 1421 | static void initring(void) | |
1618 | static void | ||
1619 | initring(void) | ||
1620 | { | 1422 | { |
1621 | int i; | 1423 | int i; |
1622 | 1424 | ||
@@ -1625,18 +1427,14 @@ initring(void) | |||
1625 | if (isprint(i)) | 1427 | if (isprint(i)) |
1626 | *endring++ = i; | 1428 | *endring++ = i; |
1627 | } | 1429 | } |
1628 | 1430 | /* Character generator. MMU arches only. */ | |
1629 | /* Character generator */ | ||
1630 | /* ARGSUSED */ | 1431 | /* ARGSUSED */ |
1631 | static void | 1432 | static void chargen_stream(int s, servtab_t *sep) |
1632 | chargen_stream(int s, servtab_t *sep) | ||
1633 | { | 1433 | { |
1634 | char *rs; | 1434 | char *rs; |
1635 | int len; | 1435 | int len; |
1636 | char text[LINESIZ + 2]; | 1436 | char text[LINESIZ + 2]; |
1637 | 1437 | ||
1638 | inetd_setproctitle(sep->se_service, s); | ||
1639 | |||
1640 | if (!endring) { | 1438 | if (!endring) { |
1641 | initring(); | 1439 | initring(); |
1642 | rs = ring; | 1440 | rs = ring; |
@@ -1655,34 +1453,27 @@ chargen_stream(int s, servtab_t *sep) | |||
1655 | } | 1453 | } |
1656 | if (++rs == endring) | 1454 | if (++rs == endring) |
1657 | rs = ring; | 1455 | rs = ring; |
1658 | if (write(s, text, sizeof(text)) != sizeof(text)) | 1456 | xwrite(s, text, sizeof(text)); |
1659 | break; | ||
1660 | } | 1457 | } |
1661 | exit(0); | ||
1662 | } | 1458 | } |
1663 | |||
1664 | /* Character generator */ | ||
1665 | /* ARGSUSED */ | 1459 | /* ARGSUSED */ |
1666 | static void | 1460 | static void chargen_dg(int s, servtab_t *sep) |
1667 | chargen_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | ||
1668 | { | 1461 | { |
1669 | /* struct sockaddr_storage ss; */ | ||
1670 | struct sockaddr sa; | ||
1671 | int len; | 1462 | int len; |
1672 | char text[LINESIZ + 2]; | 1463 | char text[LINESIZ + 2]; |
1673 | socklen_t size; | 1464 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
1465 | |||
1466 | /* Eat UDP packet which started it all */ | ||
1467 | /* dgram builtins are non-forking - DONT BLOCK! */ | ||
1468 | lsa->len = sep->se_lsa->len; | ||
1469 | if (recvfrom(s, text, sizeof(text), MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0) | ||
1470 | return; | ||
1674 | 1471 | ||
1675 | if (!endring) { | 1472 | if (!endring) { |
1676 | initring(); | 1473 | initring(); |
1677 | ringpos = ring; | 1474 | ringpos = ring; |
1678 | } | 1475 | } |
1679 | 1476 | ||
1680 | size = sizeof(sa); | ||
1681 | if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0) | ||
1682 | return; | ||
1683 | if (dg_badinput((struct sockaddr_in *) &sa)) | ||
1684 | return; | ||
1685 | |||
1686 | len = endring - ringpos; | 1477 | len = endring - ringpos; |
1687 | if (len >= LINESIZ) | 1478 | if (len >= LINESIZ) |
1688 | memmove(text, ringpos, LINESIZ); | 1479 | memmove(text, ringpos, LINESIZ); |
@@ -1694,7 +1485,7 @@ chargen_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | |||
1694 | ringpos = ring; | 1485 | ringpos = ring; |
1695 | text[LINESIZ] = '\r'; | 1486 | text[LINESIZ] = '\r'; |
1696 | text[LINESIZ + 1] = '\n'; | 1487 | text[LINESIZ + 1] = '\n'; |
1697 | (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa)); | 1488 | sendto(s, text, sizeof(text), 0, &lsa->u.sa, lsa->len); |
1698 | } | 1489 | } |
1699 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */ | 1490 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */ |
1700 | 1491 | ||
@@ -1707,48 +1498,32 @@ chargen_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | |||
1707 | * we must add 2208988800 seconds to this figure to make up for | 1498 | * we must add 2208988800 seconds to this figure to make up for |
1708 | * some seventy years Bell Labs was asleep. | 1499 | * some seventy years Bell Labs was asleep. |
1709 | */ | 1500 | */ |
1710 | |||
1711 | static unsigned machtime(void) | 1501 | static unsigned machtime(void) |
1712 | { | 1502 | { |
1713 | struct timeval tv; | 1503 | struct timeval tv; |
1714 | 1504 | ||
1715 | if (gettimeofday(&tv, NULL) < 0) { | 1505 | gettimeofday(&tv, NULL); |
1716 | fprintf(stderr, "Unable to get time of day\n"); | 1506 | return htonl((uint32_t)(tv.tv_sec + 2208988800)); |
1717 | return 0L; | ||
1718 | } | ||
1719 | return htonl((unsigned) tv.tv_sec + 2208988800UL); | ||
1720 | } | 1507 | } |
1721 | |||
1722 | /* ARGSUSED */ | 1508 | /* ARGSUSED */ |
1723 | static void | 1509 | static void machtime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
1724 | machtime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) | ||
1725 | { | 1510 | { |
1726 | unsigned result; | 1511 | uint32_t result; |
1727 | 1512 | ||
1728 | result = machtime(); | 1513 | result = machtime(); |
1729 | (void) write(s, (char *) &result, sizeof(result)); | 1514 | full_write(s, &result, sizeof(result)); |
1730 | } | 1515 | } |
1731 | 1516 | static void machtime_dg(int s, servtab_t *sep) | |
1732 | /* ARGSUSED */ | ||
1733 | static void | ||
1734 | machtime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | ||
1735 | { | 1517 | { |
1736 | unsigned result; | 1518 | uint32_t result; |
1737 | /* struct sockaddr_storage ss; */ | 1519 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
1738 | struct sockaddr sa; | ||
1739 | struct sockaddr_in *dg_sin; | ||
1740 | socklen_t size; | ||
1741 | 1520 | ||
1742 | size = sizeof(sa); | 1521 | lsa->len = sep->se_lsa->len; |
1743 | if (recvfrom(s, (char *) &result, sizeof(result), 0, &sa, &size) < 0) | 1522 | if (recvfrom(s, line, LINE_SIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0) |
1744 | return; | ||
1745 | /* if (dg_badinput((struct sockaddr *)&ss)) */ | ||
1746 | dg_sin = (struct sockaddr_in *) &sa; | ||
1747 | if (dg_sin->sin_addr.s_addr == htonl(INADDR_BROADCAST) || | ||
1748 | ntohs(dg_sin->sin_port) < IPPORT_RESERVED / 2) | ||
1749 | return; | 1523 | return; |
1524 | |||
1750 | result = machtime(); | 1525 | result = machtime(); |
1751 | (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa)); | 1526 | sendto(s, &result, sizeof(result), 0, &lsa->u.sa, lsa->len); |
1752 | } | 1527 | } |
1753 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_TIME */ | 1528 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_TIME */ |
1754 | 1529 | ||
@@ -1758,35 +1533,22 @@ machtime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | |||
1758 | /* ARGSUSED */ | 1533 | /* ARGSUSED */ |
1759 | static void daytime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) | 1534 | static void daytime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
1760 | { | 1535 | { |
1761 | char buffer[32]; | ||
1762 | time_t t; | 1536 | time_t t; |
1763 | 1537 | ||
1764 | t = time(NULL); | 1538 | t = time(NULL); |
1765 | 1539 | fdprintf(s, "%.24s\r\n", ctime(&t)); | |
1766 | // fdprintf instead? | ||
1767 | (void) sprintf(buffer, "%.24s\r\n", ctime(&t)); | ||
1768 | (void) write(s, buffer, strlen(buffer)); | ||
1769 | } | 1540 | } |
1770 | 1541 | static void daytime_dg(int s, servtab_t *sep) | |
1771 | /* Return human-readable time of day */ | ||
1772 | /* ARGSUSED */ | ||
1773 | void | ||
1774 | daytime_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) | ||
1775 | { | 1542 | { |
1776 | char buffer[256]; | ||
1777 | time_t t; | 1543 | time_t t; |
1778 | /* struct sockaddr_storage ss; */ | 1544 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
1779 | struct sockaddr sa; | ||
1780 | socklen_t size; | ||
1781 | 1545 | ||
1782 | t = time(NULL); | 1546 | lsa->len = sep->se_lsa->len; |
1783 | 1547 | if (recvfrom(s, line, LINE_SIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0) | |
1784 | size = sizeof(sa); | ||
1785 | if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0) | ||
1786 | return; | ||
1787 | if (dg_badinput((struct sockaddr_in *) &sa)) | ||
1788 | return; | 1548 | return; |
1789 | (void) sprintf(buffer, "%.24s\r\n", ctime(&t)); | 1549 | |
1790 | (void) sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa)); | 1550 | t = time(NULL); |
1551 | sprintf(line, "%.24s\r\n", ctime(&t)); | ||
1552 | sendto(s, line, strlen(line), 0, &lsa->u.sa, lsa->len); | ||
1791 | } | 1553 | } |
1792 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */ | 1554 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */ |
diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c index cd014781b..2ee833e3f 100644 --- a/networking/nc_bloaty.c +++ b/networking/nc_bloaty.c | |||
@@ -756,7 +756,7 @@ int nc_main(int argc, char **argv) | |||
756 | /* We try IPv6, then IPv4, unless addr family is | 756 | /* We try IPv6, then IPv4, unless addr family is |
757 | * implicitly set by way of remote addr/port spec */ | 757 | * implicitly set by way of remote addr/port spec */ |
758 | x = xsocket_type(&ouraddr, | 758 | x = xsocket_type(&ouraddr, |
759 | USE_FEATURE_IPV6((themaddr ? themaddr->u.sa.sa_family : AF_UNSPEC),) | 759 | (themaddr ? themaddr->u.sa.sa_family : AF_UNSPEC), |
760 | x); | 760 | x); |
761 | if (o_lport) | 761 | if (o_lport) |
762 | set_nport(ouraddr, htons(o_lport)); | 762 | set_nport(ouraddr, htons(o_lport)); |
diff --git a/networking/tftp.c b/networking/tftp.c index 737ae7893..6cad295e3 100644 --- a/networking/tftp.c +++ b/networking/tftp.c | |||
@@ -127,7 +127,7 @@ static int tftp( USE_GETPUT(const int cmd,) | |||
127 | char *cp; | 127 | char *cp; |
128 | 128 | ||
129 | unsigned org_port; | 129 | unsigned org_port; |
130 | len_and_sockaddr *const from = alloca(offsetof(len_and_sockaddr, u.sa) + peer_lsa->len); | 130 | len_and_sockaddr *const from = alloca(LSA_LEN_SIZE + peer_lsa->len); |
131 | 131 | ||
132 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation | 132 | /* Can't use RESERVE_CONFIG_BUFFER here since the allocation |
133 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ | 133 | * size varies meaning BUFFERS_GO_ON_STACK would fail */ |
diff --git a/runit/chpst.c b/runit/chpst.c index 89d8ce443..b306974bb 100644 --- a/runit/chpst.c +++ b/runit/chpst.c | |||
@@ -171,8 +171,8 @@ static void limit(int what, long l) | |||
171 | { | 171 | { |
172 | struct rlimit r; | 172 | struct rlimit r; |
173 | 173 | ||
174 | if (getrlimit(what, &r) == -1) | 174 | /* Never fails under Linux (except if you pass it bad arguments) */ |
175 | bb_perror_msg_and_die("getrlimit"); | 175 | getrlimit(what, &r); |
176 | if ((l < 0) || (l > r.rlim_max)) | 176 | if ((l < 0) || (l > r.rlim_max)) |
177 | r.rlim_cur = r.rlim_max; | 177 | r.rlim_cur = r.rlim_max; |
178 | else | 178 | else |