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