Merge commit '7d815089a43a963b49eaddf97e514194ec29805b'
[unleashed.git] / usr / src / lib / libwrap / workarounds.c
blob63b83490e7d2b3cbf6ed143bd63055af673875df
1 /*
2 * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5 #pragma ident "%Z%%M% %I% %E% SMI"
7 /*
8 * Workarounds for known system software bugs. This module provides wrappers
9 * around library functions and system calls that are known to have problems
10 * on some systems. Most of these workarounds won't do any harm on regular
11 * systems.
13 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
16 char sccsid[] = "@(#) workarounds.c 1.6 96/03/19 16:22:25";
18 #include <sys/types.h>
19 #include <sys/param.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
29 extern int errno;
31 #include "tcpd.h"
34 * Some AIX versions advertise a too small MAXHOSTNAMELEN value (32).
35 * Result: long hostnames would be truncated, and connections would be
36 * dropped because of host name verification failures. Adrian van Bloois
37 * (A.vanBloois@info.nic.surfnet.nl) figured out what was the problem.
40 #if (MAXHOSTNAMELEN < 64)
41 #undef MAXHOSTNAMELEN
42 #endif
44 /* In case not defined in <sys/param.h>. */
46 #ifndef MAXHOSTNAMELEN
47 #define MAXHOSTNAMELEN 256 /* storage for host name */
48 #endif
51 * Some DG/UX inet_addr() versions return a struct/union instead of a long.
52 * You have this problem when the compiler complains about illegal lvalues
53 * or something like that. The following code fixes this mutant behaviour.
54 * It should not be enabled on "normal" systems.
56 * Bug reported by ben@piglet.cr.usgs.gov (Rev. Ben A. Mesander).
59 #ifdef INET_ADDR_BUG
61 #undef inet_addr
63 long fix_inet_addr(string)
64 char *string;
66 return (inet_addr(string).s_addr);
69 #endif /* INET_ADDR_BUG */
72 * With some System-V versions, the fgets() library function does not
73 * account for partial reads from e.g. sockets. The result is that fgets()
74 * gives up too soon, causing username lookups to fail. Problem first
75 * reported for IRIX 4.0.5, by Steve Kotsopoulos <steve@ecf.toronto.edu>.
76 * The following code works around the problem. It does no harm on "normal"
77 * systems.
80 #ifdef BROKEN_FGETS
82 #undef fgets
84 char *fix_fgets(buf, len, fp)
85 char *buf;
86 int len;
87 FILE *fp;
89 char *cp = buf;
90 int c;
93 * Copy until the buffer fills up, until EOF, or until a newline is
94 * found.
96 while (len > 1 && (c = getc(fp)) != EOF) {
97 len--;
98 *cp++ = c;
99 if (c == '\n')
100 break;
104 * Return 0 if nothing was read. This is correct even when a silly buffer
105 * length was specified.
107 if (cp > buf) {
108 *cp = 0;
109 return (buf);
110 } else {
111 return (0);
115 #endif /* BROKEN_FGETS */
118 * With early SunOS 5 versions, recvfrom() does not completely fill in the
119 * source address structure when doing a non-destructive read. The following
120 * code works around the problem. It does no harm on "normal" systems.
123 #ifdef RECVFROM_BUG
125 #undef recvfrom
127 int fix_recvfrom(sock, buf, buflen, flags, from, fromlen)
128 int sock;
129 char *buf;
130 int buflen;
131 int flags;
132 struct sockaddr *from;
133 int *fromlen;
135 int ret;
137 /* Assume that both ends of a socket belong to the same address family. */
139 if ((ret = recvfrom(sock, buf, buflen, flags, from, fromlen)) >= 0) {
140 if (from->sa_family == 0) {
141 struct sockaddr my_addr;
142 int my_addr_len = sizeof(my_addr);
144 if (getsockname(0, &my_addr, &my_addr_len)) {
145 tcpd_warn("getsockname: %m");
146 } else {
147 from->sa_family = my_addr.sa_family;
151 return (ret);
154 #endif /* RECVFROM_BUG */
157 * The Apollo SR10.3 and some SYSV4 getpeername(2) versions do not return an
158 * error in case of a datagram-oriented socket. Instead, they claim that all
159 * UDP requests come from address 0.0.0.0. The following code works around
160 * the problem. It does no harm on "normal" systems.
163 #ifdef GETPEERNAME_BUG
165 #undef getpeername
167 int fix_getpeername(sock, sa, len)
168 int sock;
169 struct sockaddr *sa;
170 socklen_t *len;
172 int ret;
173 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
175 if ((ret = getpeername(sock, sa, len)) >= 0
176 && sa->sa_family == AF_INET
177 && sin->sin_addr.s_addr == 0) {
178 errno = ENOTCONN;
179 return (-1);
180 } else {
181 return (ret);
185 #endif /* GETPEERNAME_BUG */
188 * According to Karl Vogel (vogelke@c-17igp.wpafb.af.mil) some Pyramid
189 * versions have no yp_default_domain() function. We use getdomainname()
190 * instead.
193 #ifdef USE_GETDOMAIN
195 int yp_get_default_domain(ptr)
196 char **ptr;
198 static char mydomain[MAXHOSTNAMELEN];
200 *ptr = mydomain;
201 return (getdomainname(mydomain, MAXHOSTNAMELEN));
204 #endif /* USE_GETDOMAIN */
206 #ifndef INADDR_NONE
207 #define INADDR_NONE 0xffffffff
208 #endif
211 * Solaris 2.4 gethostbyname() has problems with multihomed hosts. When
212 * doing DNS through NIS, only one host address ends up in the address list.
213 * All other addresses end up in the hostname alias list, interspersed with
214 * copies of the official host name. This would wreak havoc with tcpd's
215 * hostname double checks. Below is a workaround that should do no harm when
216 * accidentally left in. A side effect of the workaround is that address
217 * list members are no longer properly aligned for structure access.
220 #ifdef SOLARIS_24_GETHOSTBYNAME_BUG
222 #undef gethostbyname
224 struct hostent *fix_gethostbyname(name)
225 char *name;
227 struct hostent *hp;
228 struct in_addr addr;
229 char **o_addr_list;
230 char **o_aliases;
231 char **n_addr_list;
232 int broken_gethostbyname = 0;
234 if ((hp = gethostbyname(name)) && !hp->h_addr_list[1] && hp->h_aliases[1]) {
235 for (o_aliases = n_addr_list = hp->h_aliases; *o_aliases; o_aliases++) {
236 if ((addr.s_addr = inet_addr(*o_aliases)) != INADDR_NONE) {
237 memcpy(*n_addr_list++, (char *) &addr, hp->h_length);
238 broken_gethostbyname = 1;
241 if (broken_gethostbyname) {
242 o_addr_list = hp->h_addr_list;
243 memcpy(*n_addr_list++, *o_addr_list, hp->h_length);
244 *n_addr_list = 0;
245 hp->h_addr_list = hp->h_aliases;
246 hp->h_aliases = o_addr_list + 1;
249 return (hp);
252 #endif /* SOLARIS_24_GETHOSTBYNAME_BUG */
255 * Horror! Some FreeBSD 2.0 libc routines call strtok(). Since tcpd depends
256 * heavily on strtok(), strange things may happen. Workaround: use our
257 * private strtok(). This has been fixed in the meantime.
260 #ifdef USE_STRSEP
262 char *fix_strtok(buf, sep)
263 char *buf;
264 char *sep;
266 static char *state;
267 char *result;
269 if (buf)
270 state = buf;
271 while ((result = strsep(&state, sep)) && result[0] == 0)
272 /* void */ ;
273 return (result);
276 #endif /* USE_STRSEP */
279 * IRIX 5.3 (and possibly earlier versions, too) library routines call the
280 * non-reentrant strtok() library routine, causing hosts to slip through
281 * allow/deny filters. Workaround: don't rely on the vendor and use our own
282 * strtok() function. FreeBSD 2.0 has a similar problem (fixed in 2.0.5).
285 #ifdef LIBC_CALLS_STRTOK
287 char *my_strtok(buf, sep)
288 char *buf;
289 char *sep;
291 static char *state;
292 char *result;
294 if (buf)
295 state = buf;
298 * Skip over separator characters and detect end of string.
300 if (*(state += strspn(state, sep)) == 0)
301 return (0);
304 * Skip over non-separator characters and terminate result.
306 result = state;
307 if (*(state += strcspn(state, sep)) != 0)
308 *state++ = 0;
309 return (result);
312 #endif /* LIBC_CALLS_STRTOK */