xref: /onnv-gate/usr/src/lib/libsocket/inet/getnetent_r.c (revision 2830:5228d1267a01)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <ctype.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/socket.h>
310Sstevel@tonic-gate #include <netinet/in.h>
320Sstevel@tonic-gate #include <arpa/inet.h>
330Sstevel@tonic-gate #include <netdb.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <nss_dbdefs.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 
39*2830Sdjl int str2netent(const char *, int, void *, char *, int);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static int net_stayopen;
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Unsynchronized, but it affects only
440Sstevel@tonic-gate  * efficiency, not correctness
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
480Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
490Sstevel@tonic-gate 
50*2830Sdjl void
_nss_initf_net(nss_db_params_t * p)510Sstevel@tonic-gate _nss_initf_net(nss_db_params_t *p)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	p->name	= NSS_DBNAM_NETWORKS;
540Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_NETWORKS;
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
570Sstevel@tonic-gate struct netent *
getnetbyname_r(const char * name,struct netent * result,char * buffer,int buflen)580Sstevel@tonic-gate getnetbyname_r(const char *name, struct netent *result,
590Sstevel@tonic-gate 	char *buffer, int buflen)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	nss_XbyY_args_t arg;
620Sstevel@tonic-gate 	nss_status_t	res;
630Sstevel@tonic-gate 
64*2830Sdjl 	if (name == (const char *)NULL) {
65*2830Sdjl 		errno = ERANGE;
66*2830Sdjl 		return (NULL);
67*2830Sdjl 	}
680Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2netent);
690Sstevel@tonic-gate 	arg.key.name	= name;
700Sstevel@tonic-gate 	arg.stayopen	= net_stayopen;
710Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_net,
720Sstevel@tonic-gate 		NSS_DBOP_NETWORKS_BYNAME, &arg);
730Sstevel@tonic-gate 	arg.status = res;
740Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
750Sstevel@tonic-gate 	return ((struct netent *)arg.returnval);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate 
780Sstevel@tonic-gate struct netent *
getnetbyaddr_r(long net,int type,struct netent * result,char * buffer,int buflen)790Sstevel@tonic-gate getnetbyaddr_r(long net, int type, struct netent *result,
800Sstevel@tonic-gate 	char *buffer, int buflen)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	nss_XbyY_args_t arg;
830Sstevel@tonic-gate 	nss_status_t	res;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2netent);
860Sstevel@tonic-gate 	arg.key.netaddr.net = (uint32_t)net;
870Sstevel@tonic-gate 	arg.key.netaddr.type = type;
880Sstevel@tonic-gate 	arg.stayopen	= net_stayopen;
890Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_net,
900Sstevel@tonic-gate 		NSS_DBOP_NETWORKS_BYADDR, &arg);
910Sstevel@tonic-gate 	arg.status = res;
920Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
930Sstevel@tonic-gate 	return ((struct netent *)arg.returnval);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate int
setnetent(int stay)970Sstevel@tonic-gate setnetent(int stay)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	net_stayopen |= stay;	/* === Or maybe just "=" ? */
1000Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_net, &context);
1010Sstevel@tonic-gate 	return (0);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate int
endnetent()1050Sstevel@tonic-gate endnetent()
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	net_stayopen = 0;
1080Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_net, &context);
1090Sstevel@tonic-gate 	nss_delete(&db_root);
1100Sstevel@tonic-gate 	return (0);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate struct netent *
getnetent_r(struct netent * result,char * buffer,int buflen)1140Sstevel@tonic-gate getnetent_r(struct netent *result, char *buffer, int buflen)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1170Sstevel@tonic-gate 	nss_status_t	res;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2netent);
1200Sstevel@tonic-gate 	/* No stayopen flag;  of course you stay open for iteration */
1210Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_net, &context, &arg);
1220Sstevel@tonic-gate 	arg.status = res;
1230Sstevel@tonic-gate 	(void) NSS_XbyY_FINI(&arg);
1240Sstevel@tonic-gate 	return ((struct netent *)arg.returnval);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * Return values: 0 = success, 1 = parse error, 2 = erange ...
1290Sstevel@tonic-gate  * The structure pointer passed in is a structure in the caller's space
1300Sstevel@tonic-gate  * wherein the field pointers would be set to areas in the buffer if
1310Sstevel@tonic-gate  * need be. instring and buffer should be separate areas.
1320Sstevel@tonic-gate  */
133*2830Sdjl int
str2netent(const char * instr,int lenstr,void * ent,char * buffer,int buflen)1340Sstevel@tonic-gate str2netent(const char *instr, int lenstr,
1350Sstevel@tonic-gate 	void *ent /* really (struct netnet *) */, char *buffer, int buflen)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	struct netent	*net	= (struct netent *)ent;
1380Sstevel@tonic-gate 	const char	*p, *numstart, *limit, *namestart;
1390Sstevel@tonic-gate 	int		namelen = 0;
1400Sstevel@tonic-gate 	ptrdiff_t	numlen;
1410Sstevel@tonic-gate 	char		numbuf[16];
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if ((instr >= buffer && (buffer + buflen) > instr) ||
1440Sstevel@tonic-gate 	    (buffer >= instr && (instr + lenstr) > buffer)) {
1450Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	p = instr;
1490Sstevel@tonic-gate 	limit = p + lenstr;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1520Sstevel@tonic-gate 		p++;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 	namestart = p;
1550Sstevel@tonic-gate 	while (p < limit && !isspace(*p)) {
1560Sstevel@tonic-gate 		p++;		/* Skip over the canonical name */
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	namelen = (int)(p - namestart);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (buflen <= namelen) { /* not enough buffer */
1610Sstevel@tonic-gate 		return (NSS_STR_PARSE_ERANGE);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	(void) memcpy(buffer, namestart, namelen);
1640Sstevel@tonic-gate 	buffer[namelen] = '\0';
1650Sstevel@tonic-gate 	net->n_name = buffer;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1680Sstevel@tonic-gate 		p++;
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	if (p >= limit) {
1710Sstevel@tonic-gate 		/* Syntax error -- no net number */
1720Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 	numstart = p;
1750Sstevel@tonic-gate 	do {
1760Sstevel@tonic-gate 		p++;		/* Find the end of the net number */
1770Sstevel@tonic-gate 	} while (p < limit && !isspace(*p));
1780Sstevel@tonic-gate 	numlen = p - numstart;
1790Sstevel@tonic-gate 	if (numlen >= (ptrdiff_t)sizeof (numbuf)) {
1800Sstevel@tonic-gate 		/* Syntax error -- supposed number is too long */
1810Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	(void) memcpy(numbuf, numstart, numlen);
1840Sstevel@tonic-gate 	numbuf[numlen] = '\0';
1850Sstevel@tonic-gate 	net->n_net = inet_network(numbuf);
1860Sstevel@tonic-gate 	if (net->n_net == (in_addr_t)-1) {
1870Sstevel@tonic-gate 		/* inet_network failed to parse the string */
1880Sstevel@tonic-gate 		return (NSS_STR_PARSE_PARSE);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	net->n_addrtype = AF_INET;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1940Sstevel@tonic-gate 		p++;
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * Although nss_files_XY_all calls us with # stripped,
1980Sstevel@tonic-gate 	 * we should be able to deal with it here in order to
1990Sstevel@tonic-gate 	 * be more useful.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	if (p >= limit || *p == '#') { /* no aliases, no problem */
2020Sstevel@tonic-gate 		char **ptr;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		ptr = (char **)ROUND_UP(buffer + namelen + 1,
2050Sstevel@tonic-gate 							sizeof (char *));
2060Sstevel@tonic-gate 		if ((char *)ptr >= buffer + buflen) {
2070Sstevel@tonic-gate 			net->n_aliases = 0; /* hope they don't try to peek in */
2080Sstevel@tonic-gate 			return (NSS_STR_PARSE_ERANGE);
2090Sstevel@tonic-gate 		} else {
2100Sstevel@tonic-gate 			*ptr = 0;
2110Sstevel@tonic-gate 			net->n_aliases = ptr;
2120Sstevel@tonic-gate 			return (NSS_STR_PARSE_SUCCESS);
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 	net->n_aliases = _nss_netdb_aliases(p, lenstr - (int)(p - instr),
2160Sstevel@tonic-gate 				buffer + namelen + 1, buflen - namelen - 1);
2170Sstevel@tonic-gate 	return (NSS_STR_PARSE_SUCCESS);
2180Sstevel@tonic-gate }
219