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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*132Srobinson 
230Sstevel@tonic-gate /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*132Srobinson  * Use is subject to license terms.
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * Rentrant (MT-safe) getrpcYY interfaces.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <nss_dbdefs.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <rpc/rpcent.h>
370Sstevel@tonic-gate 
38*132Srobinson extern int str2rpcent(const char *, int, void *, char *, int);
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static int rpc_stayopen;	/* Unsynchronized, but it affects only	*/
410Sstevel@tonic-gate 				/*   efficiency, not correctness	*/
420Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
430Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
440Sstevel@tonic-gate 
450Sstevel@tonic-gate void
46*132Srobinson _nss_initf_rpc(nss_db_params_t *p)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	p->name	= NSS_DBNAM_RPC;
490Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_RPC;
500Sstevel@tonic-gate }
510Sstevel@tonic-gate 
520Sstevel@tonic-gate struct rpcent *
53*132Srobinson getrpcbyname_r(const char *name, struct rpcent *result, char *buffer,
54*132Srobinson 								int buflen)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	nss_XbyY_args_t arg;
570Sstevel@tonic-gate 	nss_status_t	res;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
600Sstevel@tonic-gate 	arg.key.name	= name;
610Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
620Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
630Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNAME, &arg);
640Sstevel@tonic-gate 	arg.status = res;
65*132Srobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate struct rpcent *
69*132Srobinson getrpcbynumber_r(const int number, struct rpcent *result, char *buffer,
70*132Srobinson 								int buflen)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	nss_XbyY_args_t arg;
730Sstevel@tonic-gate 	nss_status_t	res;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
760Sstevel@tonic-gate 	arg.key.number	= number;
770Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
780Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
790Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNUMBER, &arg);
800Sstevel@tonic-gate 	arg.status = res;
81*132Srobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate void
85*132Srobinson setrpcent(const int stay)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	rpc_stayopen |= stay;
880Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_rpc, &context);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate void
92*132Srobinson endrpcent(void)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	rpc_stayopen = 0;
950Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_rpc, &context);
960Sstevel@tonic-gate 	nss_delete(&db_root);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate 
990Sstevel@tonic-gate struct rpcent *
100*132Srobinson getrpcent_r(struct rpcent *result, char *buffer, int buflen)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1030Sstevel@tonic-gate 	nss_status_t	res;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
1060Sstevel@tonic-gate 	/* No key, no stayopen */
1070Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
1080Sstevel@tonic-gate 	arg.status = res;
109*132Srobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int
113*132Srobinson str2rpcent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	struct rpcent	*rpc	= (struct rpcent *)ent;
1160Sstevel@tonic-gate 	const char	*p, *numstart, *limit, *namestart;
1170Sstevel@tonic-gate 	ssize_t		numlen, namelen = 0;
1180Sstevel@tonic-gate 	char		numbuf[12];
1190Sstevel@tonic-gate 	char		*numend;
1200Sstevel@tonic-gate 
121*132Srobinson 	if ((instr >= buffer && (buffer + buflen) > instr) ||
122*132Srobinson 			(buffer >= instr && (instr + lenstr) > buffer))
123*132Srobinson 		return (NSS_STR_PARSE_PARSE);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	p = instr;
1260Sstevel@tonic-gate 	limit = p + lenstr;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1290Sstevel@tonic-gate 		p++;
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	namestart = p;
1320Sstevel@tonic-gate 	while (p < limit && !isspace(*p)) {
1330Sstevel@tonic-gate 		p++;		/* Skip over the canonical name */
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	namelen = p - namestart;
1360Sstevel@tonic-gate 
137*132Srobinson 	if (buflen <= namelen)		/* not enough buffer */
138*132Srobinson 		return (NSS_STR_PARSE_ERANGE);
1390Sstevel@tonic-gate 	(void) memcpy(buffer, namestart, namelen);
1400Sstevel@tonic-gate 	buffer[namelen] = '\0';
1410Sstevel@tonic-gate 	rpc->r_name = buffer;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1440Sstevel@tonic-gate 		p++;
1450Sstevel@tonic-gate 	}
146*132Srobinson 	if (p >= limit)			/* Syntax error -- no RPC number */
147*132Srobinson 		return (NSS_STR_PARSE_PARSE);
1480Sstevel@tonic-gate 	numstart = p;
1490Sstevel@tonic-gate 	do {
1500Sstevel@tonic-gate 		p++;		/* Find the end of the RPC number */
1510Sstevel@tonic-gate 	} while (p < limit && !isspace(*p));
1520Sstevel@tonic-gate 	numlen = p - numstart;
1530Sstevel@tonic-gate 	if (numlen >= sizeof (numbuf)) {
1540Sstevel@tonic-gate 		/* Syntax error -- supposed number is too long */
155*132Srobinson 		return (NSS_STR_PARSE_PARSE);
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 	(void) memcpy(numbuf, numstart, numlen);
1580Sstevel@tonic-gate 	numbuf[numlen] = '\0';
1590Sstevel@tonic-gate 	rpc->r_number = (int)strtol(numbuf, &numend, 10);
160*132Srobinson 	if (*numend != '\0')
161*132Srobinson 		return (NSS_STR_PARSE_PARSE);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1640Sstevel@tonic-gate 		p++;
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	/*
1670Sstevel@tonic-gate 	 * Although nss_files_XY_all calls us with # stripped,
1680Sstevel@tonic-gate 	 * we should be able to deal with it here in order to
1690Sstevel@tonic-gate 	 * be more useful.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	if (p >= limit || *p == '#') { /* no aliases, no problem */
1720Sstevel@tonic-gate 		char **ptr;
1730Sstevel@tonic-gate 
174*132Srobinson 		ptr = (char **)ROUND_UP(buffer + namelen + 1,
1750Sstevel@tonic-gate 							sizeof (char *));
1760Sstevel@tonic-gate 		if ((char *)ptr >= buffer + buflen) {
1770Sstevel@tonic-gate 			rpc->r_aliases = 0; /* hope they don't try to peek in */
178*132Srobinson 			return (NSS_STR_PARSE_ERANGE);
1790Sstevel@tonic-gate 		}
180*132Srobinson 		*ptr = 0;
181*132Srobinson 		rpc->r_aliases = ptr;
182*132Srobinson 		return (NSS_STR_PARSE_SUCCESS);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
1850Sstevel@tonic-gate 			buffer + namelen + 1, (int)(buflen - namelen - 1));
186*132Srobinson 	return (NSS_STR_PARSE_SUCCESS);
1870Sstevel@tonic-gate }
188