2 * Workarounds for known system software bugs. This module provides wrappers
3 * around library functions and system calls that are known to have problems
4 * on some systems. Most of these workarounds won't do any harm on regular
7 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
9 * $FreeBSD: src/contrib/tcp_wrappers/workarounds.c,v 1.2 2000/02/03 10:27:01 shin Exp $
10 * $DragonFly: src/contrib/tcp_wrappers/workarounds.c,v 1.2 2003/06/17 04:24:06 dillon Exp $
14 char sccsid[] = "@(#) workarounds.c 1.6 96/03/19 16:22:25";
17 #include <sys/types.h>
18 #include <sys/param.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
33 * Some AIX versions advertise a too small MAXHOSTNAMELEN value (32).
34 * Result: long hostnames would be truncated, and connections would be
35 * dropped because of host name verification failures. Adrian van Bloois
36 * (A.vanBloois@info.nic.surfnet.nl) figured out what was the problem.
39 #if (MAXHOSTNAMELEN < 64)
43 /* In case not defined in <sys/param.h>. */
45 #ifndef MAXHOSTNAMELEN
46 #define MAXHOSTNAMELEN 256 /* storage for host name */
50 * Some DG/UX inet_addr() versions return a struct/union instead of a long.
51 * You have this problem when the compiler complains about illegal lvalues
52 * or something like that. The following code fixes this mutant behaviour.
53 * It should not be enabled on "normal" systems.
55 * Bug reported by ben@piglet.cr.usgs.gov (Rev. Ben A. Mesander).
62 long fix_inet_addr(string)
65 return (inet_addr(string).s_addr);
68 #endif /* INET_ADDR_BUG */
71 * With some System-V versions, the fgets() library function does not
72 * account for partial reads from e.g. sockets. The result is that fgets()
73 * gives up too soon, causing username lookups to fail. Problem first
74 * reported for IRIX 4.0.5, by Steve Kotsopoulos <steve@ecf.toronto.edu>.
75 * The following code works around the problem. It does no harm on "normal"
83 char *fix_fgets(buf, len, fp)
92 * Copy until the buffer fills up, until EOF, or until a newline is
95 while (len > 1 && (c = getc(fp)) != EOF) {
103 * Return 0 if nothing was read. This is correct even when a silly buffer
104 * length was specified.
114 #endif /* BROKEN_FGETS */
117 * With early SunOS 5 versions, recvfrom() does not completely fill in the
118 * source address structure when doing a non-destructive read. The following
119 * code works around the problem. It does no harm on "normal" systems.
126 int fix_recvfrom(sock, buf, buflen, flags, from, fromlen)
131 struct sockaddr *from;
136 /* Assume that both ends of a socket belong to the same address family. */
138 if ((ret = recvfrom(sock, buf, buflen, flags, from, fromlen)) >= 0) {
139 if (from->sa_family == 0) {
140 struct sockaddr my_addr;
141 int my_addr_len = sizeof(my_addr);
143 if (getsockname(0, &my_addr, &my_addr_len)) {
144 tcpd_warn("getsockname: %m");
146 from->sa_family = my_addr.sa_family;
153 #endif /* RECVFROM_BUG */
156 * The Apollo SR10.3 and some SYSV4 getpeername(2) versions do not return an
157 * error in case of a datagram-oriented socket. Instead, they claim that all
158 * UDP requests come from address 0.0.0.0. The following code works around
159 * the problem. It does no harm on "normal" systems.
162 #ifdef GETPEERNAME_BUG
166 int fix_getpeername(sock, sa, len)
173 struct sockaddr *sin = sa;
175 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
178 if ((ret = getpeername(sock, sa, len)) >= 0
180 && ((sin->su_si.si_family == AF_INET6
181 && IN6_IS_ADDR_UNSPECIFIED(&sin->su_sin6.sin6_addr))
182 || (sin->su_si.si_family == AF_INET
183 && sin->su_sin.sin_addr.s_addr == 0))) {
185 && sa->sa_family == AF_INET
186 && sin->sin_addr.s_addr == 0) {
195 #endif /* GETPEERNAME_BUG */
198 * According to Karl Vogel (vogelke@c-17igp.wpafb.af.mil) some Pyramid
199 * versions have no yp_default_domain() function. We use getdomainname()
205 int yp_get_default_domain(ptr)
208 static char mydomain[MAXHOSTNAMELEN];
211 return (getdomainname(mydomain, MAXHOSTNAMELEN));
214 #endif /* USE_GETDOMAIN */
217 #define INADDR_NONE 0xffffffff
221 * Solaris 2.4 gethostbyname() has problems with multihomed hosts. When
222 * doing DNS through NIS, only one host address ends up in the address list.
223 * All other addresses end up in the hostname alias list, interspersed with
224 * copies of the official host name. This would wreak havoc with tcpd's
225 * hostname double checks. Below is a workaround that should do no harm when
226 * accidentally left in. A side effect of the workaround is that address
227 * list members are no longer properly aligned for structure access.
230 #ifdef SOLARIS_24_GETHOSTBYNAME_BUG
234 struct hostent *fix_gethostbyname(name)
242 int broken_gethostbyname = 0;
244 if ((hp = gethostbyname(name)) && !hp->h_addr_list[1] && hp->h_aliases[1]) {
245 for (o_aliases = n_addr_list = hp->h_aliases; *o_aliases; o_aliases++) {
246 if ((addr.s_addr = inet_addr(*o_aliases)) != INADDR_NONE) {
247 memcpy(*n_addr_list++, (char *) &addr, hp->h_length);
248 broken_gethostbyname = 1;
251 if (broken_gethostbyname) {
252 o_addr_list = hp->h_addr_list;
253 memcpy(*n_addr_list++, *o_addr_list, hp->h_length);
255 hp->h_addr_list = hp->h_aliases;
256 hp->h_aliases = o_addr_list + 1;
262 #endif /* SOLARIS_24_GETHOSTBYNAME_BUG */
265 * Horror! Some FreeBSD 2.0 libc routines call strtok(). Since tcpd depends
266 * heavily on strtok(), strange things may happen. Workaround: use our
267 * private strtok(). This has been fixed in the meantime.
272 char *fix_strtok(buf, sep)
281 while ((result = strsep(&state, sep)) && result[0] == 0)
286 #endif /* USE_STRSEP */
289 * IRIX 5.3 (and possibly earlier versions, too) library routines call the
290 * non-reentrant strtok() library routine, causing hosts to slip through
291 * allow/deny filters. Workaround: don't rely on the vendor and use our own
292 * strtok() function. FreeBSD 2.0 has a similar problem (fixed in 2.0.5).
295 #ifdef LIBC_CALLS_STRTOK
297 char *my_strtok(buf, sep)
308 * Skip over separator characters and detect end of string.
310 if (*(state += strspn(state, sep)) == 0)
314 * Skip over non-separator characters and terminate result.
317 if (*(state += strcspn(state, sep)) != 0)
322 #endif /* LIBC_CALLS_STRTOK */