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.3 2005/04/29 03:00:27 joerg 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>
31 * Some AIX versions advertise a too small MAXHOSTNAMELEN value (32).
32 * Result: long hostnames would be truncated, and connections would be
33 * dropped because of host name verification failures. Adrian van Bloois
34 * (A.vanBloois@info.nic.surfnet.nl) figured out what was the problem.
37 #if (MAXHOSTNAMELEN < 64)
41 /* In case not defined in <sys/param.h>. */
43 #ifndef MAXHOSTNAMELEN
44 #define MAXHOSTNAMELEN 256 /* storage for host name */
48 * Some DG/UX inet_addr() versions return a struct/union instead of a long.
49 * You have this problem when the compiler complains about illegal lvalues
50 * or something like that. The following code fixes this mutant behaviour.
51 * It should not be enabled on "normal" systems.
53 * Bug reported by ben@piglet.cr.usgs.gov (Rev. Ben A. Mesander).
60 long fix_inet_addr(string
)
63 return (inet_addr(string
).s_addr
);
66 #endif /* INET_ADDR_BUG */
69 * With some System-V versions, the fgets() library function does not
70 * account for partial reads from e.g. sockets. The result is that fgets()
71 * gives up too soon, causing username lookups to fail. Problem first
72 * reported for IRIX 4.0.5, by Steve Kotsopoulos <steve@ecf.toronto.edu>.
73 * The following code works around the problem. It does no harm on "normal"
81 char *fix_fgets(buf
, len
, fp
)
90 * Copy until the buffer fills up, until EOF, or until a newline is
93 while (len
> 1 && (c
= getc(fp
)) != EOF
) {
101 * Return 0 if nothing was read. This is correct even when a silly buffer
102 * length was specified.
112 #endif /* BROKEN_FGETS */
115 * With early SunOS 5 versions, recvfrom() does not completely fill in the
116 * source address structure when doing a non-destructive read. The following
117 * code works around the problem. It does no harm on "normal" systems.
124 int fix_recvfrom(sock
, buf
, buflen
, flags
, from
, fromlen
)
129 struct sockaddr
*from
;
134 /* Assume that both ends of a socket belong to the same address family. */
136 if ((ret
= recvfrom(sock
, buf
, buflen
, flags
, from
, fromlen
)) >= 0) {
137 if (from
->sa_family
== 0) {
138 struct sockaddr my_addr
;
139 int my_addr_len
= sizeof(my_addr
);
141 if (getsockname(0, &my_addr
, &my_addr_len
)) {
142 tcpd_warn("getsockname: %m");
144 from
->sa_family
= my_addr
.sa_family
;
151 #endif /* RECVFROM_BUG */
154 * The Apollo SR10.3 and some SYSV4 getpeername(2) versions do not return an
155 * error in case of a datagram-oriented socket. Instead, they claim that all
156 * UDP requests come from address 0.0.0.0. The following code works around
157 * the problem. It does no harm on "normal" systems.
160 #ifdef GETPEERNAME_BUG
164 int fix_getpeername(sock
, sa
, len
)
171 struct sockaddr
*sin
= sa
;
173 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
176 if ((ret
= getpeername(sock
, sa
, len
)) >= 0
178 && ((sin
->su_si
.si_family
== AF_INET6
179 && IN6_IS_ADDR_UNSPECIFIED(&sin
->su_sin6
.sin6_addr
))
180 || (sin
->su_si
.si_family
== AF_INET
181 && sin
->su_sin
.sin_addr
.s_addr
== 0))) {
183 && sa
->sa_family
== AF_INET
184 && sin
->sin_addr
.s_addr
== 0) {
193 #endif /* GETPEERNAME_BUG */
196 * According to Karl Vogel (vogelke@c-17igp.wpafb.af.mil) some Pyramid
197 * versions have no yp_default_domain() function. We use getdomainname()
203 int yp_get_default_domain(ptr
)
206 static char mydomain
[MAXHOSTNAMELEN
];
209 return (getdomainname(mydomain
, MAXHOSTNAMELEN
));
212 #endif /* USE_GETDOMAIN */
215 #define INADDR_NONE 0xffffffff
219 * Solaris 2.4 gethostbyname() has problems with multihomed hosts. When
220 * doing DNS through NIS, only one host address ends up in the address list.
221 * All other addresses end up in the hostname alias list, interspersed with
222 * copies of the official host name. This would wreak havoc with tcpd's
223 * hostname double checks. Below is a workaround that should do no harm when
224 * accidentally left in. A side effect of the workaround is that address
225 * list members are no longer properly aligned for structure access.
228 #ifdef SOLARIS_24_GETHOSTBYNAME_BUG
232 struct hostent
*fix_gethostbyname(name
)
240 int broken_gethostbyname
= 0;
242 if ((hp
= gethostbyname(name
)) && !hp
->h_addr_list
[1] && hp
->h_aliases
[1]) {
243 for (o_aliases
= n_addr_list
= hp
->h_aliases
; *o_aliases
; o_aliases
++) {
244 if ((addr
.s_addr
= inet_addr(*o_aliases
)) != INADDR_NONE
) {
245 memcpy(*n_addr_list
++, (char *) &addr
, hp
->h_length
);
246 broken_gethostbyname
= 1;
249 if (broken_gethostbyname
) {
250 o_addr_list
= hp
->h_addr_list
;
251 memcpy(*n_addr_list
++, *o_addr_list
, hp
->h_length
);
253 hp
->h_addr_list
= hp
->h_aliases
;
254 hp
->h_aliases
= o_addr_list
+ 1;
260 #endif /* SOLARIS_24_GETHOSTBYNAME_BUG */
263 * Horror! Some FreeBSD 2.0 libc routines call strtok(). Since tcpd depends
264 * heavily on strtok(), strange things may happen. Workaround: use our
265 * private strtok(). This has been fixed in the meantime.
270 char *fix_strtok(buf
, sep
)
279 while ((result
= strsep(&state
, sep
)) && result
[0] == 0)
284 #endif /* USE_STRSEP */
287 * IRIX 5.3 (and possibly earlier versions, too) library routines call the
288 * non-reentrant strtok() library routine, causing hosts to slip through
289 * allow/deny filters. Workaround: don't rely on the vendor and use our own
290 * strtok() function. FreeBSD 2.0 has a similar problem (fixed in 2.0.5).
293 #ifdef LIBC_CALLS_STRTOK
295 char *my_strtok(buf
, sep
)
306 * Skip over separator characters and detect end of string.
308 if (*(state
+= strspn(state
, sep
)) == 0)
312 * Skip over non-separator characters and terminate result.
315 if (*(state
+= strcspn(state
, sep
)) != 0)
320 #endif /* LIBC_CALLS_STRTOK */