17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2461961e0fSrobinson  * Use is subject to license terms.
25e8031f0aSraf  */
26e8031f0aSraf 
27e8031f0aSraf /*
287c478bd9Sstevel@tonic-gate  * Rentrant (MT-safe) getrpcYY interfaces.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
33e8031f0aSraf #include "mt.h"
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <rpc/rpcent.h>
397c478bd9Sstevel@tonic-gate 
4061961e0fSrobinson extern int str2rpcent(const char *, int, void *, char *, int);
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static int rpc_stayopen;	/* Unsynchronized, but it affects only	*/
437c478bd9Sstevel@tonic-gate 				/*   efficiency, not correctness	*/
447c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
457c478bd9Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate void
_nss_initf_rpc(nss_db_params_t * p)4861961e0fSrobinson _nss_initf_rpc(nss_db_params_t *p)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	p->name	= NSS_DBNAM_RPC;
517c478bd9Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_RPC;
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate struct rpcent *
getrpcbyname_r(const char * name,struct rpcent * result,char * buffer,int buflen)5561961e0fSrobinson getrpcbyname_r(const char *name, struct rpcent *result, char *buffer,
5661961e0fSrobinson 								int buflen)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
597c478bd9Sstevel@tonic-gate 	nss_status_t	res;
607c478bd9Sstevel@tonic-gate 
61*cb5caa98Sdjl 	if (name == (const char *)NULL) {
62*cb5caa98Sdjl 		errno = ERANGE;
63*cb5caa98Sdjl 		return (NULL);
64*cb5caa98Sdjl 	}
657c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
667c478bd9Sstevel@tonic-gate 	arg.key.name	= name;
677c478bd9Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
687c478bd9Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
697c478bd9Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNAME, &arg);
707c478bd9Sstevel@tonic-gate 	arg.status = res;
7161961e0fSrobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate struct rpcent *
getrpcbynumber_r(const int number,struct rpcent * result,char * buffer,int buflen)7561961e0fSrobinson getrpcbynumber_r(const int number, struct rpcent *result, char *buffer,
7661961e0fSrobinson 								int buflen)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
797c478bd9Sstevel@tonic-gate 	nss_status_t	res;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
827c478bd9Sstevel@tonic-gate 	arg.key.number	= number;
837c478bd9Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
847c478bd9Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
857c478bd9Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNUMBER, &arg);
867c478bd9Sstevel@tonic-gate 	arg.status = res;
8761961e0fSrobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate void
setrpcent(const int stay)9161961e0fSrobinson setrpcent(const int stay)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	rpc_stayopen |= stay;
947c478bd9Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_rpc, &context);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate void
endrpcent(void)9861961e0fSrobinson endrpcent(void)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	rpc_stayopen = 0;
1017c478bd9Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_rpc, &context);
1027c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate struct rpcent *
getrpcent_r(struct rpcent * result,char * buffer,int buflen)10661961e0fSrobinson getrpcent_r(struct rpcent *result, char *buffer, int buflen)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1097c478bd9Sstevel@tonic-gate 	nss_status_t	res;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
1127c478bd9Sstevel@tonic-gate 	/* No key, no stayopen */
1137c478bd9Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
1147c478bd9Sstevel@tonic-gate 	arg.status = res;
11561961e0fSrobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
str2rpcent(const char * instr,int lenstr,void * ent,char * buffer,int buflen)11961961e0fSrobinson str2rpcent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	struct rpcent	*rpc	= (struct rpcent *)ent;
1227c478bd9Sstevel@tonic-gate 	const char	*p, *numstart, *limit, *namestart;
1237c478bd9Sstevel@tonic-gate 	ssize_t		numlen, namelen = 0;
1247c478bd9Sstevel@tonic-gate 	char		numbuf[12];
1257c478bd9Sstevel@tonic-gate 	char		*numend;
1267c478bd9Sstevel@tonic-gate 
12761961e0fSrobinson 	if ((instr >= buffer && (buffer + buflen) > instr) ||
12861961e0fSrobinson 			(buffer >= instr && (instr + lenstr) > buffer))
12961961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	p = instr;
1327c478bd9Sstevel@tonic-gate 	limit = p + lenstr;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1357c478bd9Sstevel@tonic-gate 		p++;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	namestart = p;
1387c478bd9Sstevel@tonic-gate 	while (p < limit && !isspace(*p)) {
1397c478bd9Sstevel@tonic-gate 		p++;		/* Skip over the canonical name */
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	namelen = p - namestart;
1427c478bd9Sstevel@tonic-gate 
14361961e0fSrobinson 	if (buflen <= namelen)		/* not enough buffer */
14461961e0fSrobinson 		return (NSS_STR_PARSE_ERANGE);
1457c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, namestart, namelen);
1467c478bd9Sstevel@tonic-gate 	buffer[namelen] = '\0';
1477c478bd9Sstevel@tonic-gate 	rpc->r_name = buffer;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1507c478bd9Sstevel@tonic-gate 		p++;
1517c478bd9Sstevel@tonic-gate 	}
15261961e0fSrobinson 	if (p >= limit)			/* Syntax error -- no RPC number */
15361961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1547c478bd9Sstevel@tonic-gate 	numstart = p;
1557c478bd9Sstevel@tonic-gate 	do {
1567c478bd9Sstevel@tonic-gate 		p++;		/* Find the end of the RPC number */
1577c478bd9Sstevel@tonic-gate 	} while (p < limit && !isspace(*p));
1587c478bd9Sstevel@tonic-gate 	numlen = p - numstart;
1597c478bd9Sstevel@tonic-gate 	if (numlen >= sizeof (numbuf)) {
1607c478bd9Sstevel@tonic-gate 		/* Syntax error -- supposed number is too long */
16161961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 	(void) memcpy(numbuf, numstart, numlen);
1647c478bd9Sstevel@tonic-gate 	numbuf[numlen] = '\0';
1657c478bd9Sstevel@tonic-gate 	rpc->r_number = (int)strtol(numbuf, &numend, 10);
16661961e0fSrobinson 	if (*numend != '\0')
16761961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1707c478bd9Sstevel@tonic-gate 		p++;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Although nss_files_XY_all calls us with # stripped,
1747c478bd9Sstevel@tonic-gate 	 * we should be able to deal with it here in order to
1757c478bd9Sstevel@tonic-gate 	 * be more useful.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	if (p >= limit || *p == '#') { /* no aliases, no problem */
1787c478bd9Sstevel@tonic-gate 		char **ptr;
1797c478bd9Sstevel@tonic-gate 
18061961e0fSrobinson 		ptr = (char **)ROUND_UP(buffer + namelen + 1,
1817c478bd9Sstevel@tonic-gate 							sizeof (char *));
1827c478bd9Sstevel@tonic-gate 		if ((char *)ptr >= buffer + buflen) {
1837c478bd9Sstevel@tonic-gate 			rpc->r_aliases = 0; /* hope they don't try to peek in */
18461961e0fSrobinson 			return (NSS_STR_PARSE_ERANGE);
1857c478bd9Sstevel@tonic-gate 		}
18661961e0fSrobinson 		*ptr = 0;
18761961e0fSrobinson 		rpc->r_aliases = ptr;
18861961e0fSrobinson 		return (NSS_STR_PARSE_SUCCESS);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
1917c478bd9Sstevel@tonic-gate 			buffer + namelen + 1, (int)(buflen - namelen - 1));
19261961e0fSrobinson 	return (NSS_STR_PARSE_SUCCESS);
1937c478bd9Sstevel@tonic-gate }
194