getnetbynis.c revision 92889
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#if defined(LIBC_SCCS) && !defined(lint)
27static const char rcsid[] =
28  "$FreeBSD: head/lib/libc/net/getnetbynis.c 92889 2002-03-21 18:49:23Z obrien $";
29#endif /* LIBC_SCCS and not lint */
30
31#include <sys/param.h>
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35#include <netdb.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <ctype.h>
39#include <errno.h>
40#include <string.h>
41#include <stdarg.h>
42#include <nsswitch.h>
43#include <arpa/nameser.h>
44#ifdef YP
45#include <rpc/rpc.h>
46#include <rpcsvc/yp_prot.h>
47#include <rpcsvc/ypclnt.h>
48#endif
49
50#define	MAXALIASES	35
51#define	MAXADDRS	35
52
53#ifdef YP
54static char *host_aliases[MAXALIASES];
55
56static struct netent *
57_getnetbynis(const char *name, char *map, int af)
58{
59	char *cp, **q;
60	static char *result;
61	int resultlen;
62	static struct netent h;
63	static char *domain = (char *)NULL;
64	static char ypbuf[YPMAXRECORD + 2];
65
66	switch(af) {
67	case AF_INET:
68		break;
69	default:
70	case AF_INET6:
71		errno = EAFNOSUPPORT;
72		return NULL;
73	}
74
75	if (domain == (char *)NULL)
76		if (yp_get_default_domain (&domain))
77			return (NULL);
78
79	if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
80		return (NULL);
81
82	bcopy((char *)result, (char *)&ypbuf, resultlen);
83	ypbuf[resultlen] = '\0';
84	free(result);
85	result = (char *)&ypbuf;
86
87	if ((cp = index(result, '\n')))
88		*cp = '\0';
89
90	cp = strpbrk(result, " \t");
91	*cp++ = '\0';
92	h.n_name = result;
93
94	while (*cp == ' ' || *cp == '\t')
95		cp++;
96
97	h.n_net = inet_network(cp);
98	h.n_addrtype = AF_INET;
99
100	q = h.n_aliases = host_aliases;
101	cp = strpbrk(cp, " \t");
102	if (cp != NULL)
103		*cp++ = '\0';
104	while (cp && *cp) {
105		if (*cp == ' ' || *cp == '\t') {
106			cp++;
107			continue;
108		}
109		if (q < &host_aliases[MAXALIASES - 1])
110			*q++ = cp;
111		cp = strpbrk(cp, " \t");
112		if (cp != NULL)
113			*cp++ = '\0';
114	}
115	*q = NULL;
116	return (&h);
117}
118#endif /* YP */
119
120int
121_nis_getnetbyname(void *rval, void *cb_data, va_list ap)
122{
123#ifdef YP
124	const char *name;
125
126	name = va_arg(ap, const char *);
127
128	*(struct netent **)rval = _getnetbynis(name, "networks.byname", AF_INET);
129	return (*(struct netent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
130#else
131	return NS_UNAVAIL;
132#endif
133
134}
135
136int
137_nis_getnetbyaddr(void *rval, void *cb_data, va_list ap)
138{
139#ifdef YP
140	unsigned long addr;
141	int af;
142	char *str, *cp;
143	unsigned long net2;
144	int nn;
145	unsigned int netbr[4];
146	char buf[MAXDNAME];
147
148	addr = va_arg(ap, unsigned long);
149	af = va_arg(ap, int);
150
151	*(struct netent **)rval = NULL;
152
153	if (af != AF_INET) {
154		errno = EAFNOSUPPORT;
155		return NS_UNAVAIL;
156	}
157
158        for (nn = 4, net2 = addr; net2; net2 >>= 8) {
159                netbr[--nn] = net2 & 0xff;
160	}
161
162	switch (nn) {
163	case 3:		/* Class A */
164		sprintf(buf, "%u", netbr[3]);
165		break;
166        case 2:		/* Class B */
167		sprintf(buf, "%u.%u", netbr[2], netbr[3]);
168		break;
169        case 1:		/* Class C */
170		sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
171                break;
172        case 0:		/* Class D - E */
173		sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
174			netbr[2], netbr[3]);
175		break;
176	}
177
178	str = (char *)&buf;
179	cp = str + (strlen(str) - 2);
180
181	while(!strcmp(cp, ".0")) {
182		*cp = '\0';
183		cp = str + (strlen(str) - 2);
184	}
185
186	*(struct netent **)rval = _getnetbynis(str, "networks.byaddr", af);
187	return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
188#else
189	return NS_UNAVAIL;
190#endif /* YP */
191}
192