xref: /freebsd/lib/libc/net/getnetbynis.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994, Garrett Wollman
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <netdb.h>
33 #include <resolv.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <nsswitch.h>
41 #include <arpa/nameser.h>
42 #ifdef YP
43 #include <rpc/rpc.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46 #endif
47 #include "netdb_private.h"
48 
49 #ifdef YP
50 static int
_getnetbynis(const char * name,char * map,int af,struct netent * ne,struct netent_data * ned)51 _getnetbynis(const char *name, char *map, int af, struct netent *ne,
52     struct netent_data *ned)
53 {
54 	char *p, *bp, *ep;
55 	char *cp, **q;
56 	char *result;
57 	int resultlen, len;
58 	char *ypbuf;
59 
60 	switch(af) {
61 	case AF_INET:
62 		break;
63 	default:
64 	case AF_INET6:
65 		errno = EAFNOSUPPORT;
66 		return (-1);
67 	}
68 
69 	if (ned->yp_domain == (char *)NULL)
70 		if (yp_get_default_domain (&ned->yp_domain))
71 			return (-1);
72 
73 	if (yp_match(ned->yp_domain, map, name, strlen(name), &result,
74 	    &resultlen))
75 		return (-1);
76 
77 	ypbuf = alloca(resultlen + 2);
78 	bcopy(result, ypbuf, resultlen);
79 	ypbuf[resultlen] = '\0';
80 	free(result);
81 	result = ypbuf;
82 
83 	if ((cp = strchr(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
_nis_getnetbyname(void * rval,void * cb_data,va_list ap)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 		*errnop = errno;
164 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
165 		*h_errnop = statp->res_h_errno;
166 		return (NS_RETURN);
167 	}
168 	*((struct netent **)rval) = nptr;
169 	return (NS_SUCCESS);
170 #else
171 	return (NS_UNAVAIL);
172 #endif
173 
174 }
175 
176 int
_nis_getnetbyaddr(void * rval,void * cb_data,va_list ap)177 _nis_getnetbyaddr(void *rval, void *cb_data, va_list ap)
178 {
179 #ifdef YP
180 	uint32_t addr;
181 	int af;
182 	char *buffer;
183 	size_t buflen;
184 	int *errnop, *h_errnop;
185 	struct netent *nptr, ne;
186 	struct netent_data *ned;
187 	char *str, *cp;
188 	uint32_t net2;
189 	int nn;
190 	unsigned int netbr[4];
191 	char buf[MAXDNAME];
192 	res_state statp;
193 
194 	addr = va_arg(ap, uint32_t);
195 	af = va_arg(ap, int);
196 	nptr = va_arg(ap, struct netent *);
197 	buffer = va_arg(ap, char *);
198 	buflen = va_arg(ap, size_t);
199 	errnop = va_arg(ap, int *);
200 	h_errnop = va_arg(ap, int *);
201 
202 	statp = __res_state();
203 	if ((ned = __netent_data_init()) == NULL) {
204 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
205 		*h_errnop = statp->res_h_errno;
206 		return (NS_UNAVAIL);
207 	}
208 
209 	if (af != AF_INET) {
210 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
211 		*h_errnop = statp->res_h_errno;
212 		errno = EAFNOSUPPORT;
213 		return (NS_UNAVAIL);
214 	}
215 
216         for (nn = 4, net2 = addr; net2; net2 >>= 8) {
217                 netbr[--nn] = net2 & 0xff;
218 	}
219 
220 	switch (nn) {
221 	case 3:		/* Class A */
222 		sprintf(buf, "%u", netbr[3]);
223 		break;
224         case 2:		/* Class B */
225 		sprintf(buf, "%u.%u", netbr[2], netbr[3]);
226 		break;
227         case 1:		/* Class C */
228 		sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
229                 break;
230         case 0:		/* Class D - E */
231 		sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
232 			netbr[2], netbr[3]);
233 		break;
234 	}
235 
236 	str = (char *)&buf;
237 	cp = str + (strlen(str) - 2);
238 
239 	while(!strcmp(cp, ".0")) {
240 		*cp = '\0';
241 		cp = str + (strlen(str) - 2);
242 	}
243 
244 	if (_getnetbynis(str, "networks.byaddr", af, &ne, ned) != 0) {
245 		*h_errnop = statp->res_h_errno;
246 		return (NS_NOTFOUND);
247 	}
248 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
249 		*errnop = errno;
250 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
251 		*h_errnop = statp->res_h_errno;
252 		return (NS_RETURN);
253 	}
254 	*((struct netent **)rval) = nptr;
255 	return (NS_SUCCESS);
256 #else
257 	return (NS_UNAVAIL);
258 #endif /* YP */
259 }
260