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 
31e8031f0aSraf #include "mt.h"
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <rpc/rpcent.h>
377c478bd9Sstevel@tonic-gate 
3861961e0fSrobinson extern int str2rpcent(const char *, int, void *, char *, int);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static int rpc_stayopen;	/* Unsynchronized, but it affects only	*/
417c478bd9Sstevel@tonic-gate 				/*   efficiency, not correctness	*/
427c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
437c478bd9Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate void
_nss_initf_rpc(nss_db_params_t * p)4661961e0fSrobinson _nss_initf_rpc(nss_db_params_t *p)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	p->name	= NSS_DBNAM_RPC;
497c478bd9Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_RPC;
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate struct rpcent *
getrpcbyname_r(const char * name,struct rpcent * result,char * buffer,int buflen)5361961e0fSrobinson getrpcbyname_r(const char *name, struct rpcent *result, char *buffer,
5461961e0fSrobinson 								int buflen)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
577c478bd9Sstevel@tonic-gate 	nss_status_t	res;
587c478bd9Sstevel@tonic-gate 
59*cb5caa98Sdjl 	if (name == (const char *)NULL) {
60*cb5caa98Sdjl 		errno = ERANGE;
61*cb5caa98Sdjl 		return (NULL);
62*cb5caa98Sdjl 	}
637c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
647c478bd9Sstevel@tonic-gate 	arg.key.name	= name;
657c478bd9Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
667c478bd9Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
677c478bd9Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNAME, &arg);
687c478bd9Sstevel@tonic-gate 	arg.status = res;
6961961e0fSrobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate struct rpcent *
getrpcbynumber_r(const int number,struct rpcent * result,char * buffer,int buflen)7361961e0fSrobinson getrpcbynumber_r(const int number, struct rpcent *result, char *buffer,
7461961e0fSrobinson 								int buflen)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
777c478bd9Sstevel@tonic-gate 	nss_status_t	res;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
807c478bd9Sstevel@tonic-gate 	arg.key.number	= number;
817c478bd9Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
827c478bd9Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
837c478bd9Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNUMBER, &arg);
847c478bd9Sstevel@tonic-gate 	arg.status = res;
8561961e0fSrobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate void
setrpcent(const int stay)8961961e0fSrobinson setrpcent(const int stay)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	rpc_stayopen |= stay;
927c478bd9Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_rpc, &context);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate void
endrpcent(void)9661961e0fSrobinson endrpcent(void)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	rpc_stayopen = 0;
997c478bd9Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_rpc, &context);
1007c478bd9Sstevel@tonic-gate 	nss_delete(&db_root);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate struct rpcent *
getrpcent_r(struct rpcent * result,char * buffer,int buflen)10461961e0fSrobinson getrpcent_r(struct rpcent *result, char *buffer, int buflen)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1077c478bd9Sstevel@tonic-gate 	nss_status_t	res;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
1107c478bd9Sstevel@tonic-gate 	/* No key, no stayopen */
1117c478bd9Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
1127c478bd9Sstevel@tonic-gate 	arg.status = res;
11361961e0fSrobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate int
str2rpcent(const char * instr,int lenstr,void * ent,char * buffer,int buflen)11761961e0fSrobinson str2rpcent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	struct rpcent	*rpc	= (struct rpcent *)ent;
1207c478bd9Sstevel@tonic-gate 	const char	*p, *numstart, *limit, *namestart;
1217c478bd9Sstevel@tonic-gate 	ssize_t		numlen, namelen = 0;
1227c478bd9Sstevel@tonic-gate 	char		numbuf[12];
1237c478bd9Sstevel@tonic-gate 	char		*numend;
1247c478bd9Sstevel@tonic-gate 
12561961e0fSrobinson 	if ((instr >= buffer && (buffer + buflen) > instr) ||
12661961e0fSrobinson 			(buffer >= instr && (instr + lenstr) > buffer))
12761961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	p = instr;
1307c478bd9Sstevel@tonic-gate 	limit = p + lenstr;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1337c478bd9Sstevel@tonic-gate 		p++;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	namestart = p;
1367c478bd9Sstevel@tonic-gate 	while (p < limit && !isspace(*p)) {
1377c478bd9Sstevel@tonic-gate 		p++;		/* Skip over the canonical name */
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	namelen = p - namestart;
1407c478bd9Sstevel@tonic-gate 
14161961e0fSrobinson 	if (buflen <= namelen)		/* not enough buffer */
14261961e0fSrobinson 		return (NSS_STR_PARSE_ERANGE);
1437c478bd9Sstevel@tonic-gate 	(void) memcpy(buffer, namestart, namelen);
1447c478bd9Sstevel@tonic-gate 	buffer[namelen] = '\0';
1457c478bd9Sstevel@tonic-gate 	rpc->r_name = buffer;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1487c478bd9Sstevel@tonic-gate 		p++;
1497c478bd9Sstevel@tonic-gate 	}
15061961e0fSrobinson 	if (p >= limit)			/* Syntax error -- no RPC number */
15161961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1527c478bd9Sstevel@tonic-gate 	numstart = p;
1537c478bd9Sstevel@tonic-gate 	do {
1547c478bd9Sstevel@tonic-gate 		p++;		/* Find the end of the RPC number */
1557c478bd9Sstevel@tonic-gate 	} while (p < limit && !isspace(*p));
1567c478bd9Sstevel@tonic-gate 	numlen = p - numstart;
1577c478bd9Sstevel@tonic-gate 	if (numlen >= sizeof (numbuf)) {
1587c478bd9Sstevel@tonic-gate 		/* Syntax error -- supposed number is too long */
15961961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	(void) memcpy(numbuf, numstart, numlen);
1627c478bd9Sstevel@tonic-gate 	numbuf[numlen] = '\0';
1637c478bd9Sstevel@tonic-gate 	rpc->r_number = (int)strtol(numbuf, &numend, 10);
16461961e0fSrobinson 	if (*numend != '\0')
16561961e0fSrobinson 		return (NSS_STR_PARSE_PARSE);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1687c478bd9Sstevel@tonic-gate 		p++;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Although nss_files_XY_all calls us with # stripped,
1727c478bd9Sstevel@tonic-gate 	 * we should be able to deal with it here in order to
1737c478bd9Sstevel@tonic-gate 	 * be more useful.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if (p >= limit || *p == '#') { /* no aliases, no problem */
1767c478bd9Sstevel@tonic-gate 		char **ptr;
1777c478bd9Sstevel@tonic-gate 
17861961e0fSrobinson 		ptr = (char **)ROUND_UP(buffer + namelen + 1,
1797c478bd9Sstevel@tonic-gate 							sizeof (char *));
1807c478bd9Sstevel@tonic-gate 		if ((char *)ptr >= buffer + buflen) {
1817c478bd9Sstevel@tonic-gate 			rpc->r_aliases = 0; /* hope they don't try to peek in */
18261961e0fSrobinson 			return (NSS_STR_PARSE_ERANGE);
1837c478bd9Sstevel@tonic-gate 		}
18461961e0fSrobinson 		*ptr = 0;
18561961e0fSrobinson 		rpc->r_aliases = ptr;
18661961e0fSrobinson 		return (NSS_STR_PARSE_SUCCESS);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
1897c478bd9Sstevel@tonic-gate 			buffer + namelen + 1, (int)(buflen - namelen - 1));
19061961e0fSrobinson 	return (NSS_STR_PARSE_SUCCESS);
1917c478bd9Sstevel@tonic-gate }
192