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