xref: /freebsd/lib/libc/net/getnetbynis.c (revision 036ae3dd790971f07f3cf41e19e087973d0de986)
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 <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 == (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 	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 		h_errno = 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 #if __LONG_BIT == 64
103 	ne->__n_pad0 = 0;	/* ABI compatibility */
104 #endif
105 	ne->n_addrtype = AF_INET;
106 
107 	q = ne->n_aliases = ned->net_aliases;
108 	cp = strpbrk(cp, " \t");
109 	if (cp != NULL)
110 		*cp++ = '\0';
111 	while (cp && *cp) {
112 		if (*cp == ' ' || *cp == '\t') {
113 			cp++;
114 			continue;
115 		}
116 		if (q > &ned->net_aliases[_MAXALIASES - 1])
117 			break;
118 		p = strpbrk(cp, " \t");
119 		if (p != NULL)
120 			*p++ = '\0';
121 		len = strlen(cp) + 1;
122 		if (ep - bp < len)
123 			break;
124 		strlcpy(bp, cp, ep - bp);
125 		*q++ = bp;
126 		bp += len;
127 		cp = p;
128 	}
129 	*q = NULL;
130 	return 0;
131 }
132 #endif /* YP */
133 
134 int
135 _nis_getnetbyname(void *rval, void *cb_data, va_list ap)
136 {
137 #ifdef YP
138 	const char *name;
139 	struct netent *ne;
140 	struct netent_data *ned;
141 	int error;
142 
143 	name = va_arg(ap, const char *);
144 	ne = va_arg(ap, struct netent *);
145 	ned = va_arg(ap, struct netent_data *);
146 
147 	error = _getnetbynis(name, "networks.byname", AF_INET, ne, ned);
148 	return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
149 #else
150 	return NS_UNAVAIL;
151 #endif
152 
153 }
154 
155 int
156 _nis_getnetbyaddr(void *rval, void *cb_data, va_list ap)
157 {
158 #ifdef YP
159 	uint32_t addr;
160 	int af;
161 	struct netent *ne;
162 	struct netent_data *ned;
163 	char *str, *cp;
164 	uint32_t net2;
165 	int nn;
166 	unsigned int netbr[4];
167 	char buf[MAXDNAME];
168 	int error;
169 
170 	addr = va_arg(ap, uint32_t);
171 	af = va_arg(ap, int);
172 	ne = va_arg(ap, struct netent *);
173 	ned = va_arg(ap, struct netent_data *);
174 
175 	if (af != AF_INET) {
176 		errno = EAFNOSUPPORT;
177 		return NS_UNAVAIL;
178 	}
179 
180         for (nn = 4, net2 = addr; net2; net2 >>= 8) {
181                 netbr[--nn] = net2 & 0xff;
182 	}
183 
184 	switch (nn) {
185 	case 3:		/* Class A */
186 		sprintf(buf, "%u", netbr[3]);
187 		break;
188         case 2:		/* Class B */
189 		sprintf(buf, "%u.%u", netbr[2], netbr[3]);
190 		break;
191         case 1:		/* Class C */
192 		sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
193                 break;
194         case 0:		/* Class D - E */
195 		sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
196 			netbr[2], netbr[3]);
197 		break;
198 	}
199 
200 	str = (char *)&buf;
201 	cp = str + (strlen(str) - 2);
202 
203 	while(!strcmp(cp, ".0")) {
204 		*cp = '\0';
205 		cp = str + (strlen(str) - 2);
206 	}
207 
208 	error = _getnetbynis(str, "networks.byaddr", af, ne, ned);
209 	return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
210 #else
211 	return NS_UNAVAIL;
212 #endif /* YP */
213 }
214