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  */
22132Srobinson 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25132Srobinson  * Use is subject to license terms.
26*1219Sraf  */
27*1219Sraf 
28*1219Sraf /*
290Sstevel@tonic-gate  * Rentrant (MT-safe) getrpcYY interfaces.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
34*1219Sraf #include "mt.h"
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <nss_dbdefs.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <rpc/rpcent.h>
400Sstevel@tonic-gate 
41132Srobinson extern int str2rpcent(const char *, int, void *, char *, int);
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static int rpc_stayopen;	/* Unsynchronized, but it affects only	*/
440Sstevel@tonic-gate 				/*   efficiency, not correctness	*/
450Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
460Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate void
49132Srobinson _nss_initf_rpc(nss_db_params_t *p)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	p->name	= NSS_DBNAM_RPC;
520Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_RPC;
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate struct rpcent *
56132Srobinson getrpcbyname_r(const char *name, struct rpcent *result, char *buffer,
57132Srobinson 								int buflen)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	nss_XbyY_args_t arg;
600Sstevel@tonic-gate 	nss_status_t	res;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
630Sstevel@tonic-gate 	arg.key.name	= name;
640Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
650Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
660Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNAME, &arg);
670Sstevel@tonic-gate 	arg.status = res;
68132Srobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate struct rpcent *
72132Srobinson getrpcbynumber_r(const int number, struct rpcent *result, char *buffer,
73132Srobinson 								int buflen)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	nss_XbyY_args_t arg;
760Sstevel@tonic-gate 	nss_status_t	res;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
790Sstevel@tonic-gate 	arg.key.number	= number;
800Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
810Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
820Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNUMBER, &arg);
830Sstevel@tonic-gate 	arg.status = res;
84132Srobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate void
88132Srobinson setrpcent(const int stay)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	rpc_stayopen |= stay;
910Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_rpc, &context);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate void
95132Srobinson endrpcent(void)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	rpc_stayopen = 0;
980Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_rpc, &context);
990Sstevel@tonic-gate 	nss_delete(&db_root);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate struct rpcent *
103132Srobinson getrpcent_r(struct rpcent *result, char *buffer, int buflen)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate 	nss_XbyY_args_t arg;
1060Sstevel@tonic-gate 	nss_status_t	res;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
1090Sstevel@tonic-gate 	/* No key, no stayopen */
1100Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
1110Sstevel@tonic-gate 	arg.status = res;
112132Srobinson 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate int
116132Srobinson str2rpcent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	struct rpcent	*rpc	= (struct rpcent *)ent;
1190Sstevel@tonic-gate 	const char	*p, *numstart, *limit, *namestart;
1200Sstevel@tonic-gate 	ssize_t		numlen, namelen = 0;
1210Sstevel@tonic-gate 	char		numbuf[12];
1220Sstevel@tonic-gate 	char		*numend;
1230Sstevel@tonic-gate 
124132Srobinson 	if ((instr >= buffer && (buffer + buflen) > instr) ||
125132Srobinson 			(buffer >= instr && (instr + lenstr) > buffer))
126132Srobinson 		return (NSS_STR_PARSE_PARSE);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	p = instr;
1290Sstevel@tonic-gate 	limit = p + lenstr;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1320Sstevel@tonic-gate 		p++;
1330Sstevel@tonic-gate 	}
1340Sstevel@tonic-gate 	namestart = p;
1350Sstevel@tonic-gate 	while (p < limit && !isspace(*p)) {
1360Sstevel@tonic-gate 		p++;		/* Skip over the canonical name */
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	namelen = p - namestart;
1390Sstevel@tonic-gate 
140132Srobinson 	if (buflen <= namelen)		/* not enough buffer */
141132Srobinson 		return (NSS_STR_PARSE_ERANGE);
1420Sstevel@tonic-gate 	(void) memcpy(buffer, namestart, namelen);
1430Sstevel@tonic-gate 	buffer[namelen] = '\0';
1440Sstevel@tonic-gate 	rpc->r_name = buffer;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1470Sstevel@tonic-gate 		p++;
1480Sstevel@tonic-gate 	}
149132Srobinson 	if (p >= limit)			/* Syntax error -- no RPC number */
150132Srobinson 		return (NSS_STR_PARSE_PARSE);
1510Sstevel@tonic-gate 	numstart = p;
1520Sstevel@tonic-gate 	do {
1530Sstevel@tonic-gate 		p++;		/* Find the end of the RPC number */
1540Sstevel@tonic-gate 	} while (p < limit && !isspace(*p));
1550Sstevel@tonic-gate 	numlen = p - numstart;
1560Sstevel@tonic-gate 	if (numlen >= sizeof (numbuf)) {
1570Sstevel@tonic-gate 		/* Syntax error -- supposed number is too long */
158132Srobinson 		return (NSS_STR_PARSE_PARSE);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	(void) memcpy(numbuf, numstart, numlen);
1610Sstevel@tonic-gate 	numbuf[numlen] = '\0';
1620Sstevel@tonic-gate 	rpc->r_number = (int)strtol(numbuf, &numend, 10);
163132Srobinson 	if (*numend != '\0')
164132Srobinson 		return (NSS_STR_PARSE_PARSE);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
1670Sstevel@tonic-gate 		p++;
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * Although nss_files_XY_all calls us with # stripped,
1710Sstevel@tonic-gate 	 * we should be able to deal with it here in order to
1720Sstevel@tonic-gate 	 * be more useful.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	if (p >= limit || *p == '#') { /* no aliases, no problem */
1750Sstevel@tonic-gate 		char **ptr;
1760Sstevel@tonic-gate 
177132Srobinson 		ptr = (char **)ROUND_UP(buffer + namelen + 1,
1780Sstevel@tonic-gate 							sizeof (char *));
1790Sstevel@tonic-gate 		if ((char *)ptr >= buffer + buflen) {
1800Sstevel@tonic-gate 			rpc->r_aliases = 0; /* hope they don't try to peek in */
181132Srobinson 			return (NSS_STR_PARSE_ERANGE);
1820Sstevel@tonic-gate 		}
183132Srobinson 		*ptr = 0;
184132Srobinson 		rpc->r_aliases = ptr;
185132Srobinson 		return (NSS_STR_PARSE_SUCCESS);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 	rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
1880Sstevel@tonic-gate 			buffer + namelen + 1, (int)(buflen - namelen - 1));
189132Srobinson 	return (NSS_STR_PARSE_SUCCESS);
1900Sstevel@tonic-gate }
191