xref: /freebsd/lib/libc/net/getnetbynis.c (revision 6607310b740c499e273ae5bbf1f308ed91b61c05)
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 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 #include <resolv.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 #include <arpa/nameser.h>
43 #ifdef YP
44 #include <rpc/rpc.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 #endif
48 #include "netdb_private.h"
49 
50 #ifdef YP
51 static int
52 _getnetbynis(const char *name, char *map, int af, struct netent *ne,
53     struct netent_data *ned)
54 {
55 	char *p, *bp, *ep;
56 	char *cp, **q;
57 	char *result;
58 	int resultlen, len;
59 	char ypbuf[YPMAXRECORD + 2];
60 
61 	switch(af) {
62 	case AF_INET:
63 		break;
64 	default:
65 	case AF_INET6:
66 		errno = EAFNOSUPPORT;
67 		return (-1);
68 	}
69 
70 	if (ned->yp_domain == (char *)NULL)
71 		if (yp_get_default_domain (&ned->yp_domain))
72 			return (-1);
73 
74 	if (yp_match(ned->yp_domain, map, name, strlen(name), &result,
75 	    &resultlen))
76 		return (-1);
77 
78 	bcopy((char *)result, (char *)&ypbuf, resultlen);
79 	ypbuf[resultlen] = '\0';
80 	free(result);
81 	result = (char *)&ypbuf;
82 
83 	if ((cp = index(result, '\n')))
84 		*cp = '\0';
85 
86 	cp = strpbrk(result, " \t");
87 	*cp++ = '\0';
88 	bp = ned->netbuf;
89 	ep = ned->netbuf + sizeof ned->netbuf;
90 	len = strlen(result) + 1;
91 	if (ep - bp < len) {
92 		RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
93 		return (-1);
94 	}
95 	strlcpy(bp, result, ep - bp);
96 	ne->n_name = bp;
97 	bp += len;
98 
99 	while (*cp == ' ' || *cp == '\t')
100 		cp++;
101 
102 	ne->n_net = inet_network(cp);
103 	ne->n_addrtype = AF_INET;
104 
105 	q = ne->n_aliases = ned->net_aliases;
106 	cp = strpbrk(cp, " \t");
107 	if (cp != NULL)
108 		*cp++ = '\0';
109 	while (cp && *cp) {
110 		if (*cp == ' ' || *cp == '\t') {
111 			cp++;
112 			continue;
113 		}
114 		if (q > &ned->net_aliases[_MAXALIASES - 1])
115 			break;
116 		p = strpbrk(cp, " \t");
117 		if (p != NULL)
118 			*p++ = '\0';
119 		len = strlen(cp) + 1;
120 		if (ep - bp < len)
121 			break;
122 		strlcpy(bp, cp, ep - bp);
123 		*q++ = bp;
124 		bp += len;
125 		cp = p;
126 	}
127 	*q = NULL;
128 	return (0);
129 }
130 #endif /* YP */
131 
132 int
133 _nis_getnetbyname(void *rval, void *cb_data, va_list ap)
134 {
135 #ifdef YP
136 	const char *name;
137 	char *buffer;
138 	size_t buflen;
139 	int *errnop, *h_errnop;
140 	struct netent *nptr, ne;
141 	struct netent_data *ned;
142 	res_state statp;
143 
144 	name = va_arg(ap, const char *);
145 	nptr = va_arg(ap, struct netent *);
146 	buffer = va_arg(ap, char *);
147 	buflen = va_arg(ap, size_t);
148 	errnop = va_arg(ap, int *);
149 	h_errnop = va_arg(ap, int *);
150 
151 	statp = __res_state();
152 	if ((ned = __netent_data_init()) == NULL) {
153 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
154 		*h_errnop = statp->res_h_errno;
155 		return (NS_UNAVAIL);
156 	}
157 
158 	if (_getnetbynis(name, "networks.byname", AF_INET, &ne, ned) != 0) {
159 		*h_errnop = statp->res_h_errno;
160 		return (NS_NOTFOUND);
161 	}
162 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
163 		*h_errnop = statp->res_h_errno;
164 		return (NS_NOTFOUND);
165 	}
166 	*((struct netent **)rval) = nptr;
167 	return (NS_SUCCESS);
168 #else
169 	return (NS_UNAVAIL);
170 #endif
171 
172 }
173 
174 int
175 _nis_getnetbyaddr(void *rval, void *cb_data, va_list ap)
176 {
177 #ifdef YP
178 	uint32_t addr;
179 	int af;
180 	char *buffer;
181 	size_t buflen;
182 	int *errnop, *h_errnop;
183 	struct netent *nptr, ne;
184 	struct netent_data *ned;
185 	char *str, *cp;
186 	uint32_t net2;
187 	int nn;
188 	unsigned int netbr[4];
189 	char buf[MAXDNAME];
190 	res_state statp;
191 
192 	addr = va_arg(ap, uint32_t);
193 	af = va_arg(ap, int);
194 	nptr = va_arg(ap, struct netent *);
195 	buffer = va_arg(ap, char *);
196 	buflen = va_arg(ap, size_t);
197 	errnop = va_arg(ap, int *);
198 	h_errnop = va_arg(ap, int *);
199 
200 	statp = __res_state();
201 	if ((ned = __netent_data_init()) == NULL) {
202 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
203 		*h_errnop = statp->res_h_errno;
204 		return (NS_UNAVAIL);
205 	}
206 
207 	if (af != AF_INET) {
208 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
209 		*h_errnop = statp->res_h_errno;
210 		errno = EAFNOSUPPORT;
211 		return (NS_UNAVAIL);
212 	}
213 
214         for (nn = 4, net2 = addr; net2; net2 >>= 8) {
215                 netbr[--nn] = net2 & 0xff;
216 	}
217 
218 	switch (nn) {
219 	case 3:		/* Class A */
220 		sprintf(buf, "%u", netbr[3]);
221 		break;
222         case 2:		/* Class B */
223 		sprintf(buf, "%u.%u", netbr[2], netbr[3]);
224 		break;
225         case 1:		/* Class C */
226 		sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
227                 break;
228         case 0:		/* Class D - E */
229 		sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
230 			netbr[2], netbr[3]);
231 		break;
232 	}
233 
234 	str = (char *)&buf;
235 	cp = str + (strlen(str) - 2);
236 
237 	while(!strcmp(cp, ".0")) {
238 		*cp = '\0';
239 		cp = str + (strlen(str) - 2);
240 	}
241 
242 	if (_getnetbynis(str, "networks.byaddr", af, &ne, ned) != 0) {
243 		*h_errnop = statp->res_h_errno;
244 		return (NS_NOTFOUND);
245 	}
246 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
247 		*h_errnop = statp->res_h_errno;
248 		return (NS_NOTFOUND);
249 	}
250 	*((struct netent **)rval) = nptr;
251 	return (NS_SUCCESS);
252 #else
253 	return (NS_UNAVAIL);
254 #endif /* YP */
255 }
256