xref: /dragonfly/lib/libc/net/getnetbynis.c (revision ec7f7fc8)
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/getnetbynis.c,v 1.21 2006/04/15 16:20:27 ume Exp $
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
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[YPMAXRECORD + 2];
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 == 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 	bcopy((char *)result, (char *)&ypbuf, resultlen);
78 	ypbuf[resultlen] = '\0';
79 	free(result);
80 	result = (char *)&ypbuf;
81 
82 	if ((cp = index(result, '\n')))
83 		*cp = '\0';
84 
85 	cp = strpbrk(result, " \t");
86 	*cp++ = '\0';
87 	bp = ned->netbuf;
88 	ep = ned->netbuf + sizeof ned->netbuf;
89 	len = strlen(result) + 1;
90 	if (ep - bp < len) {
91 		RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
92 		return (-1);
93 	}
94 	strlcpy(bp, result, ep - bp);
95 	ne->n_name = bp;
96 	bp += len;
97 
98 	while (*cp == ' ' || *cp == '\t')
99 		cp++;
100 
101 	ne->n_net = inet_network(cp);
102 	ne->n_addrtype = AF_INET;
103 
104 	q = ne->n_aliases = ned->net_aliases;
105 	cp = strpbrk(cp, " \t");
106 	if (cp != NULL)
107 		*cp++ = '\0';
108 	while (cp && *cp) {
109 		if (*cp == ' ' || *cp == '\t') {
110 			cp++;
111 			continue;
112 		}
113 		if (q > &ned->net_aliases[_MAXALIASES - 1])
114 			break;
115 		p = strpbrk(cp, " \t");
116 		if (p != NULL)
117 			*p++ = '\0';
118 		len = strlen(cp) + 1;
119 		if (ep - bp < len)
120 			break;
121 		strlcpy(bp, cp, ep - bp);
122 		*q++ = bp;
123 		bp += len;
124 		cp = p;
125 	}
126 	*q = NULL;
127 	return (0);
128 }
129 #endif /* YP */
130 
131 int
132 _nis_getnetbyname(void *rval, void *cb_data __unused, va_list ap)
133 {
134 #ifdef YP
135 	const char *name;
136 	char *buffer;
137 	size_t buflen;
138 	int *errnop, *h_errnop;
139 	struct netent *nptr, ne;
140 	struct netent_data *ned;
141 	res_state statp;
142 
143 	name = va_arg(ap, const char *);
144 	nptr = va_arg(ap, struct netent *);
145 	buffer = va_arg(ap, char *);
146 	buflen = va_arg(ap, size_t);
147 	errnop = va_arg(ap, int *);
148 	h_errnop = va_arg(ap, int *);
149 
150 	statp = __res_state();
151 	if ((ned = __netent_data_init()) == NULL) {
152 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
153 		*h_errnop = statp->res_h_errno;
154 		return (NS_UNAVAIL);
155 	}
156 
157 	if (_getnetbynis(name, "networks.byname", AF_INET, &ne, ned) != 0) {
158 		*h_errnop = statp->res_h_errno;
159 		return (NS_NOTFOUND);
160 	}
161 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
162 		*h_errnop = statp->res_h_errno;
163 		return (NS_NOTFOUND);
164 	}
165 	*((struct netent **)rval) = nptr;
166 	return (NS_SUCCESS);
167 #else
168 	return (NS_UNAVAIL);
169 #endif
170 
171 }
172 
173 int
174 _nis_getnetbyaddr(void *rval, void *cb_data __unused, va_list ap)
175 {
176 #ifdef YP
177 	uint32_t addr;
178 	int af;
179 	char *buffer;
180 	size_t buflen;
181 	int *errnop, *h_errnop;
182 	struct netent *nptr, ne;
183 	struct netent_data *ned;
184 	char *str, *cp;
185 	uint32_t net2;
186 	int nn;
187 	unsigned int netbr[4];
188 	char buf[MAXDNAME];
189 	res_state statp;
190 
191 	addr = va_arg(ap, uint32_t);
192 	af = va_arg(ap, int);
193 	nptr = va_arg(ap, struct netent *);
194 	buffer = va_arg(ap, char *);
195 	buflen = va_arg(ap, size_t);
196 	errnop = va_arg(ap, int *);
197 	h_errnop = va_arg(ap, int *);
198 
199 	statp = __res_state();
200 	if ((ned = __netent_data_init()) == NULL) {
201 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
202 		*h_errnop = statp->res_h_errno;
203 		return (NS_UNAVAIL);
204 	}
205 
206 	if (af != AF_INET) {
207 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
208 		*h_errnop = statp->res_h_errno;
209 		errno = EAFNOSUPPORT;
210 		return (NS_UNAVAIL);
211 	}
212 
213         for (nn = 4, net2 = addr; net2; net2 >>= 8) {
214                 netbr[--nn] = net2 & 0xff;
215 	}
216 
217 	switch (nn) {
218 	case 3:		/* Class A */
219 		sprintf(buf, "%u", netbr[3]);
220 		break;
221         case 2:		/* Class B */
222 		sprintf(buf, "%u.%u", netbr[2], netbr[3]);
223 		break;
224         case 1:		/* Class C */
225 		sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
226                 break;
227         case 0:		/* Class D - E */
228 		sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
229 			netbr[2], netbr[3]);
230 		break;
231 	}
232 
233 	str = (char *)&buf;
234 	cp = str + (strlen(str) - 2);
235 
236 	while(!strcmp(cp, ".0")) {
237 		*cp = '\0';
238 		cp = str + (strlen(str) - 2);
239 	}
240 
241 	if (_getnetbynis(str, "networks.byaddr", af, &ne, ned) != 0) {
242 		*h_errnop = statp->res_h_errno;
243 		return (NS_NOTFOUND);
244 	}
245 	if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
246 		*h_errnop = statp->res_h_errno;
247 		return (NS_NOTFOUND);
248 	}
249 	*((struct netent **)rval) = nptr;
250 	return (NS_SUCCESS);
251 #else
252 	return (NS_UNAVAIL);
253 #endif /* YP */
254 }
255