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