xref: /dflybsd-src/lib/libc/net/gethostbynis.c (revision ed5d57202ebab0e923eb8e9d967a9f97792a6e8f)
1 /*-
2  * Copyright (c) 1994, Garrett Wollman
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/lib/libc/net/gethostbynis.c,v 1.16 2003/01/05 14:05:24 fenner Exp $
26  * $DragonFly: src/lib/libc/net/gethostbynis.c,v 1.4 2005/11/13 02:04:47 swildner Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <nsswitch.h>
42 #ifdef YP
43 #include <rpc/rpc.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46 #endif
47 
48 #define	MAXALIASES	35
49 #define	MAXADDRS	35
50 
51 extern int h_errno;
52 
53 #ifdef YP
54 static char *host_aliases[MAXALIASES];
55 static char hostaddr[MAXADDRS];
56 static char *host_addrs[2];
57 
58 static struct hostent *
59 _gethostbynis(const char *name, char *map, int af)
60 {
61 	char *cp, **q;
62 	char *result;
63 	int resultlen,size;
64 	static struct hostent h;
65 	static char *domain = (char *)NULL;
66 	static char ypbuf[YPMAXRECORD + 2];
67 	in_addr_t addr;
68 
69 	switch(af) {
70 	case AF_INET:
71 		size = NS_INADDRSZ;
72 		break;
73 	default:
74 	case AF_INET6:
75 		size = NS_IN6ADDRSZ;
76 		errno = EAFNOSUPPORT;
77 		h_errno = NETDB_INTERNAL;
78 		return NULL;
79 	}
80 
81 	if (domain == (char *)NULL)
82 		if (yp_get_default_domain (&domain)) {
83 			h_errno = NETDB_INTERNAL;
84 			return ((struct hostent *)NULL);
85 		}
86 
87 	if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) {
88 		h_errno = HOST_NOT_FOUND;
89 		return ((struct hostent *)NULL);
90 	}
91 
92 	/* avoid potential memory leak */
93 	bcopy((char *)result, (char *)&ypbuf, resultlen);
94 	ypbuf[resultlen] = '\0';
95 	free(result);
96 	result = (char *)&ypbuf;
97 
98 	if ((cp = index(result, '\n')))
99 		*cp = '\0';
100 
101 	cp = strpbrk(result, " \t");
102 	*cp++ = '\0';
103 	h.h_addr_list = host_addrs;
104 	h.h_addr = hostaddr;
105 	addr = inet_addr(result);
106 	bcopy((char *)&addr, h.h_addr, size);
107 	h.h_length = size;
108 	h.h_addrtype = AF_INET;
109 	while (*cp == ' ' || *cp == '\t')
110 		cp++;
111 	h.h_name = cp;
112 	q = h.h_aliases = host_aliases;
113 	cp = strpbrk(cp, " \t");
114 	if (cp != NULL)
115 		*cp++ = '\0';
116 	while (cp && *cp) {
117 		if (*cp == ' ' || *cp == '\t') {
118 			cp++;
119 			continue;
120 		}
121 		if (q < &host_aliases[MAXALIASES - 1])
122 			*q++ = cp;
123 		cp = strpbrk(cp, " \t");
124 		if (cp != NULL)
125 			*cp++ = '\0';
126 	}
127 	*q = NULL;
128 	return (&h);
129 }
130 #endif /* YP */
131 
132 /* XXX _gethostbynisname/_gethostbynisaddr only used by getaddrinfo */
133 struct hostent *
134 _gethostbynisname(const char *name, int af)
135 {
136 #ifdef YP
137 	return _gethostbynis(name, "hosts.byname", af);
138 #else
139 	return NULL;
140 #endif
141 }
142 
143 struct hostent *
144 _gethostbynisaddr(const char *addr, int len, int af)
145 {
146 #ifdef YP
147 	return _gethostbynis(inet_ntoa(*(struct in_addr *)addr),
148 			     "hosts.byaddr", af);
149 #else
150 	return NULL;
151 #endif
152 }
153 
154 
155 int
156 _nis_gethostbyname(void *rval, void *cb_data, va_list ap)
157 {
158 #ifdef YP
159 	const char *name;
160 	int af;
161 
162 	name = va_arg(ap, const char *);
163 	af = va_arg(ap, int);
164 
165 	*(struct hostent **)rval = _gethostbynis(name, "hosts.byname", af);
166 	return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
167 #else
168 	return NS_UNAVAIL;
169 #endif
170 }
171 
172 int
173 _nis_gethostbyaddr(void *rval, void *cb_data, va_list ap)
174 {
175 #ifdef YP
176 	const char *addr;
177 	int len;
178 	int af;
179 
180 	addr = va_arg(ap, const char *);
181 	len = va_arg(ap, int);
182 	af = va_arg(ap, int);
183 
184 	*(struct hostent **)rval =_gethostbynis(inet_ntoa(*(struct in_addr *)addr),"hosts.byaddr", af);
185 	return (*(struct hostent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
186 #else
187 	return NS_UNAVAIL;
188 #endif
189 }
190