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.
13 char sccsid
[] = "@(#) workarounds.c 1.6 96/03/19 16:22:25";
16 #include <sys/types.h>
17 #include <sys/param.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
32 * Some AIX versions advertise a too small MAXHOSTNAMELEN value (32).
33 * Result: long hostnames would be truncated, and connections would be
34 * dropped because of host name verification failures. Adrian van Bloois
35 * (A.vanBloois@info.nic.surfnet.nl) figured out what was the problem.
38 #if (MAXHOSTNAMELEN < 64)
42 /* In case not defined in <sys/param.h>. */
44 #ifndef MAXHOSTNAMELEN
45 #define MAXHOSTNAMELEN 256 /* storage for host name */
49 * Some DG/UX inet_addr() versions return a struct/union instead of a long.
50 * You have this problem when the compiler complains about illegal lvalues
51 * or something like that. The following code fixes this mutant behaviour.
52 * It should not be enabled on "normal" systems.
54 * Bug reported by ben@piglet.cr.usgs.gov (Rev. Ben A. Mesander).
61 long fix_inet_addr(string
)
64 return (inet_addr(string
).s_addr
);
67 #endif /* INET_ADDR_BUG */
70 * With some System-V versions, the fgets() library function does not
71 * account for partial reads from e.g. sockets. The result is that fgets()
72 * gives up too soon, causing username lookups to fail. Problem first
73 * reported for IRIX 4.0.5, by Steve Kotsopoulos <steve@ecf.toronto.edu>.
74 * The following code works around the problem. It does no harm on "normal"
82 char *fix_fgets(buf
, len
, fp
)
91 * Copy until the buffer fills up, until EOF, or until a newline is
94 while (len
> 1 && (c
= getc(fp
)) != EOF
) {
102 * Return 0 if nothing was read. This is correct even when a silly buffer
103 * length was specified.
113 #endif /* BROKEN_FGETS */
116 * With early SunOS 5 versions, recvfrom() does not completely fill in the
117 * source address structure when doing a non-destructive read. The following
118 * code works around the problem. It does no harm on "normal" systems.
125 int fix_recvfrom(sock
, buf
, buflen
, flags
, from
, fromlen
)
130 struct sockaddr
*from
;
135 /* Assume that both ends of a socket belong to the same address family. */
137 if ((ret
= recvfrom(sock
, buf
, buflen
, flags
, from
, fromlen
)) >= 0) {
138 if (from
->sa_family
== 0) {
139 struct sockaddr my_addr
;
140 int my_addr_len
= sizeof(my_addr
);
142 if (getsockname(0, &my_addr
, &my_addr_len
)) {
143 tcpd_warn("getsockname: %m");
145 from
->sa_family
= my_addr
.sa_family
;
152 #endif /* RECVFROM_BUG */
155 * The Apollo SR10.3 and some SYSV4 getpeername(2) versions do not return an
156 * error in case of a datagram-oriented socket. Instead, they claim that all
157 * UDP requests come from address 0.0.0.0. The following code works around
158 * the problem. It does no harm on "normal" systems.
161 #ifdef GETPEERNAME_BUG
165 int fix_getpeername(sock
, sa
, len
)
172 struct sockaddr
*sin
= sa
;
174 struct sockaddr_in
*sin
= (struct sockaddr_in
*) sa
;
177 if ((ret
= getpeername(sock
, sa
, len
)) >= 0
179 && ((sin
->su_si
.si_family
== AF_INET6
180 && IN6_IS_ADDR_UNSPECIFIED(&sin
->su_sin6
.sin6_addr
))
181 || (sin
->su_si
.si_family
== AF_INET
182 && sin
->su_sin
.sin_addr
.s_addr
== 0))) {
184 && sa
->sa_family
== AF_INET
185 && sin
->sin_addr
.s_addr
== 0) {
194 #endif /* GETPEERNAME_BUG */
197 * According to Karl Vogel (vogelke@c-17igp.wpafb.af.mil) some Pyramid
198 * versions have no yp_default_domain() function. We use getdomainname()
204 int yp_get_default_domain(ptr
)
207 static char mydomain
[MAXHOSTNAMELEN
];
210 return (getdomainname(mydomain
, MAXHOSTNAMELEN
));
213 #endif /* USE_GETDOMAIN */
216 #define INADDR_NONE 0xffffffff
220 * Solaris 2.4 gethostbyname() has problems with multihomed hosts. When
221 * doing DNS through NIS, only one host address ends up in the address list.
222 * All other addresses end up in the hostname alias list, interspersed with
223 * copies of the official host name. This would wreak havoc with tcpd's
224 * hostname double checks. Below is a workaround that should do no harm when
225 * accidentally left in. A side effect of the workaround is that address
226 * list members are no longer properly aligned for structure access.
229 #ifdef SOLARIS_24_GETHOSTBYNAME_BUG
233 struct hostent
*fix_gethostbyname(name
)
241 int broken_gethostbyname
= 0;
243 if ((hp
= gethostbyname(name
)) && !hp
->h_addr_list
[1] && hp
->h_aliases
[1]) {
244 for (o_aliases
= n_addr_list
= hp
->h_aliases
; *o_aliases
; o_aliases
++) {
245 if ((addr
.s_addr
= inet_addr(*o_aliases
)) != INADDR_NONE
) {
246 memcpy(*n_addr_list
++, (char *) &addr
, hp
->h_length
);
247 broken_gethostbyname
= 1;
250 if (broken_gethostbyname
) {
251 o_addr_list
= hp
->h_addr_list
;
252 memcpy(*n_addr_list
++, *o_addr_list
, hp
->h_length
);
254 hp
->h_addr_list
= hp
->h_aliases
;
255 hp
->h_aliases
= o_addr_list
+ 1;
261 #endif /* SOLARIS_24_GETHOSTBYNAME_BUG */
264 * Horror! Some FreeBSD 2.0 libc routines call strtok(). Since tcpd depends
265 * heavily on strtok(), strange things may happen. Workaround: use our
266 * private strtok(). This has been fixed in the meantime.
271 char *fix_strtok(buf
, sep
)
280 while ((result
= strsep(&state
, sep
)) && result
[0] == 0)
285 #endif /* USE_STRSEP */
288 * IRIX 5.3 (and possibly earlier versions, too) library routines call the
289 * non-reentrant strtok() library routine, causing hosts to slip through
290 * allow/deny filters. Workaround: don't rely on the vendor and use our own
291 * strtok() function. FreeBSD 2.0 has a similar problem (fixed in 2.0.5).
294 #ifdef LIBC_CALLS_STRTOK
296 char *my_strtok(buf
, sep
)
307 * Skip over separator characters and detect end of string.
309 if (*(state
+= strspn(state
, sep
)) == 0)
313 * Skip over non-separator characters and terminate result.
316 if (*(state
+= strcspn(state
, sep
)) != 0)
321 #endif /* LIBC_CALLS_STRTOK */