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