getnetbynis.c revision 65532
13070Spst/*-
23070Spst * Copyright (c) 1994, Garrett Wollman
33070Spst *
43070Spst * Redistribution and use in source and binary forms, with or without
53070Spst * modification, are permitted provided that the following conditions
63070Spst * are met:
73070Spst * 1. Redistributions of source code must retain the above copyright
83070Spst *    notice, this list of conditions and the following disclaimer.
93070Spst * 2. Redistributions in binary form must reproduce the above copyright
103070Spst *    notice, this list of conditions and the following disclaimer in the
113070Spst *    documentation and/or other materials provided with the distribution.
123070Spst *
133070Spst * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
143070Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
153070Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
163070Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
173070Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
183070Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
193070Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
203070Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
213070Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
223070Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
233070Spst * SUCH DAMAGE.
243070Spst */
253070Spst
263070Spst#if defined(LIBC_SCCS) && !defined(lint)
2750476Speterstatic char sccsid[] = "@(#)$FreeBSD: head/lib/libc/net/getnetbynis.c 65532 2000-09-06 18:16:48Z nectar $";
2850476Speterstatic char rcsid[] = "$FreeBSD: head/lib/libc/net/getnetbynis.c 65532 2000-09-06 18:16:48Z nectar $";
293070Spst#endif /* LIBC_SCCS and not lint */
303070Spst
313070Spst#include <sys/param.h>
323070Spst#include <sys/socket.h>
333070Spst#include <netinet/in.h>
343070Spst#include <arpa/inet.h>
353070Spst#include <netdb.h>
363070Spst#include <stdio.h>
3711661Sphk#include <stdlib.h>
383070Spst#include <ctype.h>
393070Spst#include <errno.h>
403070Spst#include <string.h>
4165532Snectar#include <stdarg.h>
4265532Snectar#include <nsswitch.h>
4314775Swpaul#include <arpa/nameser.h>
4414775Swpaul#ifdef YP
4514775Swpaul#include <rpc/rpc.h>
4614775Swpaul#include <rpcsvc/yp_prot.h>
4714775Swpaul#include <rpcsvc/ypclnt.h>
4814775Swpaul#endif
493070Spst
503070Spst#define	MAXALIASES	35
513070Spst#define	MAXADDRS	35
523070Spst
533070Spst#ifdef YP
543070Spststatic char *host_aliases[MAXALIASES];
553070Spst
563070Spststatic struct netent *
5765532Snectar_getnetbynis(const char *name, char *map, int af)
583070Spst{
593070Spst	register char *cp, **q;
603108Swollman	static char *result;
613070Spst	int resultlen;
623070Spst	static struct netent h;
633070Spst	static char *domain = (char *)NULL;
6420953Swpaul	static char ypbuf[YPMAXRECORD + 2];
653070Spst
6617903Speter	switch(af) {
6717903Speter	case AF_INET:
6817903Speter		break;
6917903Speter	default:
7017903Speter	case AF_INET6:
7117903Speter		errno = EAFNOSUPPORT;
7217903Speter		return NULL;
7317903Speter	}
7417903Speter
753070Spst	if (domain == (char *)NULL)
763070Spst		if (yp_get_default_domain (&domain))
773070Spst			return (NULL);
783070Spst
793070Spst	if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
803070Spst		return (NULL);
813070Spst
8214775Swpaul	bcopy((char *)result, (char *)&ypbuf, resultlen);
8320953Swpaul	ypbuf[resultlen] = '\0';
8414775Swpaul	free(result);
8514775Swpaul	result = (char *)&ypbuf;
8614775Swpaul
873070Spst	if ((cp = index(result, '\n')))
883070Spst		*cp = '\0';
893070Spst
903070Spst	cp = strpbrk(result, " \t");
913070Spst	*cp++ = '\0';
923108Swollman	h.n_name = result;
933108Swollman
943070Spst	while (*cp == ' ' || *cp == '\t')
953070Spst		cp++;
963108Swollman
973108Swollman	h.n_net = inet_network(cp);
983108Swollman	h.n_addrtype = AF_INET;
993108Swollman
1003070Spst	q = h.n_aliases = host_aliases;
1013070Spst	cp = strpbrk(cp, " \t");
1023070Spst	if (cp != NULL)
1033070Spst		*cp++ = '\0';
1043070Spst	while (cp && *cp) {
1053070Spst		if (*cp == ' ' || *cp == '\t') {
1063070Spst			cp++;
1073070Spst			continue;
1083070Spst		}
1093070Spst		if (q < &host_aliases[MAXALIASES - 1])
1103070Spst			*q++ = cp;
1113070Spst		cp = strpbrk(cp, " \t");
1123070Spst		if (cp != NULL)
1133070Spst			*cp++ = '\0';
1143070Spst	}
1153070Spst	*q = NULL;
1163070Spst	return (&h);
11765532Snectar}
11865532Snectar#endif /* YP */
11965532Snectar
12065532Snectarint
12165532Snectar_nis_getnetbyname(void *rval, void *cb_data, va_list ap)
12265532Snectar{
12365532Snectar#ifdef YP
12465532Snectar	const char *name;
12565532Snectar
12665532Snectar	name = va_arg(ap, const char *);
12765532Snectar
12865532Snectar	*(struct netent **)rval = _getnetbynis(name, "networks.byname", AF_INET);
12965532Snectar	return (*(struct netent **)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
1303070Spst#else
13165532Snectar	return NS_UNAVAIL;
1323070Spst#endif
13365532Snectar
1343070Spst}
1353070Spst
13665532Snectarint
13765532Snectar_nis_getnetbyaddr(void *rval, void *cb_data, va_list ap)
1383070Spst{
13965532Snectar#ifdef YP
14017903Speter	unsigned long addr;
14117903Speter	int af;
1423108Swollman	char *str, *cp;
14314775Swpaul	unsigned long net2;
14414775Swpaul	int nn;
14514775Swpaul	unsigned int netbr[4];
14614775Swpaul	char buf[MAXDNAME];
1473070Spst
14865532Snectar	addr = va_arg(ap, unsigned long);
14965532Snectar	af = va_arg(ap, int);
15065532Snectar
15165532Snectar	*(struct netent **)rval = NULL;
15265532Snectar
15317903Speter	if (af != AF_INET) {
15417903Speter		errno = EAFNOSUPPORT;
15565532Snectar		return NS_UNAVAIL;
15617903Speter	}
1573070Spst
15814775Swpaul        for (nn = 4, net2 = addr; net2; net2 >>= 8) {
15914775Swpaul                netbr[--nn] = net2 & 0xff;
16014775Swpaul	}
16114775Swpaul
16214775Swpaul	switch (nn) {
16314775Swpaul	case 3:		/* Class A */
16414775Swpaul		sprintf(buf, "%u", netbr[3]);
16514775Swpaul		break;
16614775Swpaul        case 2:		/* Class B */
16714775Swpaul		sprintf(buf, "%u.%u", netbr[2], netbr[3]);
16814775Swpaul		break;
16914775Swpaul        case 1:		/* Class C */
17014775Swpaul		sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
17114775Swpaul                break;
17214775Swpaul        case 0:		/* Class D - E */
17314775Swpaul		sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
17414775Swpaul			netbr[2], netbr[3]);
17514775Swpaul		break;
17614775Swpaul	}
17714775Swpaul
17814775Swpaul	str = (char *)&buf;
17914775Swpaul	cp = str + (strlen(str) - 2);
18014775Swpaul
1813108Swollman	while(!strcmp(cp, ".0")) {
1823108Swollman		*cp = '\0';
18314775Swpaul		cp = str + (strlen(str) - 2);
1843108Swollman	}
1858870Srgrimes
18665532Snectar	*(struct netent **)rval = _getnetbynis(str, "networks.byaddr", af);
18765532Snectar	return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND;
18865532Snectar#else
18965532Snectar	return NS_UNAVAIL;
19065532Snectar#endif /* YP */
1913070Spst}
192