1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1999-2001, 2004 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
12*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: sm_gethost.c,v 8.27 2004/08/20 21:12:37 ca Exp $")
13*7c478bd9Sstevel@tonic-gate 
14*7c478bd9Sstevel@tonic-gate #include <sendmail.h>
15*7c478bd9Sstevel@tonic-gate #if NETINET || NETINET6
16*7c478bd9Sstevel@tonic-gate # include <arpa/inet.h>
17*7c478bd9Sstevel@tonic-gate #endif /* NETINET || NETINET6 */
18*7c478bd9Sstevel@tonic-gate #include "libmilter.h"
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate /*
21*7c478bd9Sstevel@tonic-gate **  MI_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
22*7c478bd9Sstevel@tonic-gate **
23*7c478bd9Sstevel@tonic-gate **	Some operating systems have wierd problems with the gethostbyXXX
24*7c478bd9Sstevel@tonic-gate **	routines.  For example, Solaris versions at least through 2.3
25*7c478bd9Sstevel@tonic-gate **	don't properly deliver a canonical h_name field.  This tries to
26*7c478bd9Sstevel@tonic-gate **	work around these problems.
27*7c478bd9Sstevel@tonic-gate **
28*7c478bd9Sstevel@tonic-gate **	Support IPv6 as well as IPv4.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #if NETINET6 && NEEDSGETIPNODE
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate static struct hostent *getipnodebyname __P((char *, int, int, int *));
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate # ifndef AI_ADDRCONFIG
36*7c478bd9Sstevel@tonic-gate #  define AI_ADDRCONFIG	0	/* dummy */
37*7c478bd9Sstevel@tonic-gate # endif /* ! AI_ADDRCONFIG */
38*7c478bd9Sstevel@tonic-gate # ifndef AI_ALL
39*7c478bd9Sstevel@tonic-gate #  define AI_ALL	0	/* dummy */
40*7c478bd9Sstevel@tonic-gate # endif /* ! AI_ALL */
41*7c478bd9Sstevel@tonic-gate # ifndef AI_DEFAULT
42*7c478bd9Sstevel@tonic-gate #  define AI_DEFAULT	0	/* dummy */
43*7c478bd9Sstevel@tonic-gate # endif /* ! AI_DEFAULT */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static struct hostent *
getipnodebyname(name,family,flags,err)46*7c478bd9Sstevel@tonic-gate getipnodebyname(name, family, flags, err)
47*7c478bd9Sstevel@tonic-gate 	char *name;
48*7c478bd9Sstevel@tonic-gate 	int family;
49*7c478bd9Sstevel@tonic-gate 	int flags;
50*7c478bd9Sstevel@tonic-gate 	int *err;
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate 	bool resv6 = true;
53*7c478bd9Sstevel@tonic-gate 	struct hostent *h;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if (family == AF_INET6)
56*7c478bd9Sstevel@tonic-gate 	{
57*7c478bd9Sstevel@tonic-gate 		/* From RFC2133, section 6.1 */
58*7c478bd9Sstevel@tonic-gate 		resv6 = bitset(RES_USE_INET6, _res.options);
59*7c478bd9Sstevel@tonic-gate 		_res.options |= RES_USE_INET6;
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate 	SM_SET_H_ERRNO(0);
62*7c478bd9Sstevel@tonic-gate 	h = gethostbyname(name);
63*7c478bd9Sstevel@tonic-gate 	if (family == AF_INET6 && !resv6)
64*7c478bd9Sstevel@tonic-gate 		_res.options &= ~RES_USE_INET6;
65*7c478bd9Sstevel@tonic-gate 	*err = h_errno;
66*7c478bd9Sstevel@tonic-gate 	return h;
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate void
freehostent(h)70*7c478bd9Sstevel@tonic-gate freehostent(h)
71*7c478bd9Sstevel@tonic-gate 	struct hostent *h;
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	/*
74*7c478bd9Sstevel@tonic-gate 	**  Stub routine -- if they don't have getipnodeby*(),
75*7c478bd9Sstevel@tonic-gate 	**  they probably don't have the free routine either.
76*7c478bd9Sstevel@tonic-gate 	*/
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	return;
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate #endif /* NEEDSGETIPNODE && NETINET6 */
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate struct hostent *
mi_gethostbyname(name,family)83*7c478bd9Sstevel@tonic-gate mi_gethostbyname(name, family)
84*7c478bd9Sstevel@tonic-gate 	char *name;
85*7c478bd9Sstevel@tonic-gate 	int family;
86*7c478bd9Sstevel@tonic-gate {
87*7c478bd9Sstevel@tonic-gate 	struct hostent *h = NULL;
88*7c478bd9Sstevel@tonic-gate #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
89*7c478bd9Sstevel@tonic-gate # if SOLARIS == 20300 || SOLARIS == 203
90*7c478bd9Sstevel@tonic-gate 	static struct hostent hp;
91*7c478bd9Sstevel@tonic-gate 	static char buf[1000];
92*7c478bd9Sstevel@tonic-gate 	extern struct hostent *_switch_gethostbyname_r();
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
95*7c478bd9Sstevel@tonic-gate # else /* SOLARIS == 20300 || SOLARIS == 203 */
96*7c478bd9Sstevel@tonic-gate 	extern struct hostent *__switch_gethostbyname();
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	h = __switch_gethostbyname(name);
99*7c478bd9Sstevel@tonic-gate # endif /* SOLARIS == 20300 || SOLARIS == 203 */
100*7c478bd9Sstevel@tonic-gate #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
101*7c478bd9Sstevel@tonic-gate # if NETINET6
102*7c478bd9Sstevel@tonic-gate 	int flags = AI_DEFAULT|AI_ALL;
103*7c478bd9Sstevel@tonic-gate 	int err;
104*7c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate # if NETINET6
107*7c478bd9Sstevel@tonic-gate #  if ADDRCONFIG_IS_BROKEN
108*7c478bd9Sstevel@tonic-gate 	flags &= ~AI_ADDRCONFIG;
109*7c478bd9Sstevel@tonic-gate #  endif /* ADDRCONFIG_IS_BROKEN */
110*7c478bd9Sstevel@tonic-gate 	h = getipnodebyname(name, family, flags, &err);
111*7c478bd9Sstevel@tonic-gate 	SM_SET_H_ERRNO(err);
112*7c478bd9Sstevel@tonic-gate # else /* NETINET6 */
113*7c478bd9Sstevel@tonic-gate 	h = gethostbyname(name);
114*7c478bd9Sstevel@tonic-gate # endif /* NETINET6 */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
117*7c478bd9Sstevel@tonic-gate 	return h;
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate #if NETINET6
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate **  MI_INET_PTON -- convert printed form to network address.
123*7c478bd9Sstevel@tonic-gate **
124*7c478bd9Sstevel@tonic-gate **	Wrapper for inet_pton() which handles IPv6: labels.
125*7c478bd9Sstevel@tonic-gate **
126*7c478bd9Sstevel@tonic-gate **	Parameters:
127*7c478bd9Sstevel@tonic-gate **		family -- address family
128*7c478bd9Sstevel@tonic-gate **		src -- string
129*7c478bd9Sstevel@tonic-gate **		dst -- destination address structure
130*7c478bd9Sstevel@tonic-gate **
131*7c478bd9Sstevel@tonic-gate **	Returns:
132*7c478bd9Sstevel@tonic-gate **		1 if the address was valid
133*7c478bd9Sstevel@tonic-gate **		0 if the address wasn't parseable
134*7c478bd9Sstevel@tonic-gate **		-1 if error
135*7c478bd9Sstevel@tonic-gate */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate int
mi_inet_pton(family,src,dst)138*7c478bd9Sstevel@tonic-gate mi_inet_pton(family, src, dst)
139*7c478bd9Sstevel@tonic-gate 	int family;
140*7c478bd9Sstevel@tonic-gate 	const char *src;
141*7c478bd9Sstevel@tonic-gate 	void *dst;
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	if (family == AF_INET6 &&
144*7c478bd9Sstevel@tonic-gate 	    strncasecmp(src, "IPv6:", 5) == 0)
145*7c478bd9Sstevel@tonic-gate 		src += 5;
146*7c478bd9Sstevel@tonic-gate 	return inet_pton(family, src, dst);
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate #endif /* NETINET6 */
149