1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1986-1992 by Sun Microsystems Inc.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Rentrant (MT-safe) getrpcYY interfaces.
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include <ctype.h>
31*0Sstevel@tonic-gate #include <nss_dbdefs.h>
32*0Sstevel@tonic-gate #include <stdlib.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <rpc/rpcent.h>
35*0Sstevel@tonic-gate #include <rpc/trace.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate int str2rpcent(const char *, int, void *,
38*0Sstevel@tonic-gate 		char *, int);
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate static int rpc_stayopen;	/* Unsynchronized, but it affects only	*/
41*0Sstevel@tonic-gate 				/*   efficiency, not correctness	*/
42*0Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
43*0Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate void
46*0Sstevel@tonic-gate _nss_initf_rpc(p)
47*0Sstevel@tonic-gate 	nss_db_params_t	*p;
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	trace1(TR__nss_initf_rpc, 0);
50*0Sstevel@tonic-gate 	p->name	= NSS_DBNAM_RPC;
51*0Sstevel@tonic-gate 	p->default_config = NSS_DEFCONF_RPC;
52*0Sstevel@tonic-gate 	trace1(TR__nss_initf_rpc, 1);
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate struct rpcent *
56*0Sstevel@tonic-gate getrpcbyname_r(name, result, buffer, buflen)
57*0Sstevel@tonic-gate 	const char	*name;
58*0Sstevel@tonic-gate 	struct rpcent	*result;
59*0Sstevel@tonic-gate 	char		*buffer;
60*0Sstevel@tonic-gate 	int		buflen;
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	nss_XbyY_args_t arg;
63*0Sstevel@tonic-gate 	nss_status_t	res;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	trace2(TR_getrpcbyname_r, 0, buflen);
66*0Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
67*0Sstevel@tonic-gate 	arg.key.name	= name;
68*0Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
69*0Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
70*0Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNAME, &arg);
71*0Sstevel@tonic-gate 	arg.status = res;
72*0Sstevel@tonic-gate 	NSS_XbyY_FINI(&arg);
73*0Sstevel@tonic-gate 	trace2(TR_getrpcbyname_r, 1, buflen);
74*0Sstevel@tonic-gate 	return (struct rpcent *) arg.returnval;
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate struct rpcent *
78*0Sstevel@tonic-gate getrpcbynumber_r(number, result, buffer, buflen)
79*0Sstevel@tonic-gate 	const int	number;
80*0Sstevel@tonic-gate 	struct rpcent	*result;
81*0Sstevel@tonic-gate 	char		*buffer;
82*0Sstevel@tonic-gate 	int		buflen;
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	nss_XbyY_args_t arg;
85*0Sstevel@tonic-gate 	nss_status_t	res;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	trace3(TR_getrpcbyname_r, 0, number, buflen);
88*0Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
89*0Sstevel@tonic-gate 	arg.key.number	= number;
90*0Sstevel@tonic-gate 	arg.stayopen	= rpc_stayopen;
91*0Sstevel@tonic-gate 	res = nss_search(&db_root, _nss_initf_rpc,
92*0Sstevel@tonic-gate 		NSS_DBOP_RPC_BYNUMBER, &arg);
93*0Sstevel@tonic-gate 	arg.status = res;
94*0Sstevel@tonic-gate 	NSS_XbyY_FINI(&arg);
95*0Sstevel@tonic-gate 	trace3(TR_getrpcbyname_r, 1, number, buflen);
96*0Sstevel@tonic-gate 	return (struct rpcent *) arg.returnval;
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate void
100*0Sstevel@tonic-gate setrpcent(stay)
101*0Sstevel@tonic-gate 	const int		stay;
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	trace1(TR_setrpcent, 0);
104*0Sstevel@tonic-gate 	rpc_stayopen |= stay;
105*0Sstevel@tonic-gate 	nss_setent(&db_root, _nss_initf_rpc, &context);
106*0Sstevel@tonic-gate 	trace1(TR_setrpcent, 1);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate void
110*0Sstevel@tonic-gate endrpcent()
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	trace1(TR_endrpcent, 0);
113*0Sstevel@tonic-gate 	rpc_stayopen = 0;
114*0Sstevel@tonic-gate 	nss_endent(&db_root, _nss_initf_rpc, &context);
115*0Sstevel@tonic-gate 	nss_delete(&db_root);
116*0Sstevel@tonic-gate 	trace1(TR_endrpcent, 1);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate struct rpcent *
120*0Sstevel@tonic-gate getrpcent_r(result, buffer, buflen)
121*0Sstevel@tonic-gate 	struct rpcent	*result;
122*0Sstevel@tonic-gate 	char		*buffer;
123*0Sstevel@tonic-gate 	int		buflen;
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	nss_XbyY_args_t arg;
126*0Sstevel@tonic-gate 	nss_status_t	res;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	trace2(TR_getrpcbyent_r, 0, buflen);
129*0Sstevel@tonic-gate 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
130*0Sstevel@tonic-gate 	/* No key, no stayopen */
131*0Sstevel@tonic-gate 	res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
132*0Sstevel@tonic-gate 	arg.status = res;
133*0Sstevel@tonic-gate 	NSS_XbyY_FINI(&arg);
134*0Sstevel@tonic-gate 	trace2(TR_getrpcbyent_r, 1, buflen);
135*0Sstevel@tonic-gate 	return (struct rpcent *) arg.returnval;
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate int
139*0Sstevel@tonic-gate str2rpcent(instr, lenstr, ent, buffer, buflen)
140*0Sstevel@tonic-gate 	const char	*instr;
141*0Sstevel@tonic-gate 	int		lenstr;
142*0Sstevel@tonic-gate 	void	*ent;
143*0Sstevel@tonic-gate 	char	*buffer;
144*0Sstevel@tonic-gate 	int	buflen;
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	struct rpcent	*rpc	= (struct rpcent *)ent;
147*0Sstevel@tonic-gate 	const char	*p, *numstart, *limit, *namestart;
148*0Sstevel@tonic-gate 	ssize_t		numlen, namelen = 0;
149*0Sstevel@tonic-gate 	char		numbuf[12];
150*0Sstevel@tonic-gate 	char		*numend;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	trace3(TR_str2rpcent, 0, lenstr, buflen);
153*0Sstevel@tonic-gate 	if ((instr >= buffer && (buffer + buflen) > instr)
154*0Sstevel@tonic-gate 		|| (buffer >= instr && (instr + lenstr) > buffer)) {
155*0Sstevel@tonic-gate 		trace3(TR_str2rpcent, 1, lenstr, buflen);
156*0Sstevel@tonic-gate 		return NSS_STR_PARSE_PARSE;
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	p = instr;
160*0Sstevel@tonic-gate 	limit = p + lenstr;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
163*0Sstevel@tonic-gate 		p++;
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 	namestart = p;
166*0Sstevel@tonic-gate 	while (p < limit && !isspace(*p)) {
167*0Sstevel@tonic-gate 		p++;		/* Skip over the canonical name */
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 	namelen = p - namestart;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (buflen <= namelen) { /* not enough buffer */
172*0Sstevel@tonic-gate 		trace3(TR_str2rpcent, 1, lenstr, buflen);
173*0Sstevel@tonic-gate 		return NSS_STR_PARSE_ERANGE;
174*0Sstevel@tonic-gate 	}
175*0Sstevel@tonic-gate 	(void) memcpy(buffer, namestart, namelen);
176*0Sstevel@tonic-gate 	buffer[namelen] = '\0';
177*0Sstevel@tonic-gate 	rpc->r_name = buffer;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
180*0Sstevel@tonic-gate 		p++;
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	if (p >= limit) {
183*0Sstevel@tonic-gate 		/* Syntax error -- no RPC number */
184*0Sstevel@tonic-gate 		trace3(TR_str2rpcent, 1, lenstr, buflen);
185*0Sstevel@tonic-gate 		return NSS_STR_PARSE_PARSE;
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 	numstart = p;
188*0Sstevel@tonic-gate 	do {
189*0Sstevel@tonic-gate 		p++;		/* Find the end of the RPC number */
190*0Sstevel@tonic-gate 	} while (p < limit && !isspace(*p));
191*0Sstevel@tonic-gate 	numlen = p - numstart;
192*0Sstevel@tonic-gate 	if (numlen >= sizeof (numbuf)) {
193*0Sstevel@tonic-gate 		/* Syntax error -- supposed number is too long */
194*0Sstevel@tonic-gate 		trace3(TR_str2rpcent, 1, lenstr, buflen);
195*0Sstevel@tonic-gate 		return NSS_STR_PARSE_PARSE;
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 	(void) memcpy(numbuf, numstart, numlen);
198*0Sstevel@tonic-gate 	numbuf[numlen] = '\0';
199*0Sstevel@tonic-gate 	rpc->r_number = (int)strtol(numbuf, &numend, 10);
200*0Sstevel@tonic-gate 	if (*numend != '\0') {
201*0Sstevel@tonic-gate 		trace3(TR_str2rpcent, 1, lenstr, buflen);
202*0Sstevel@tonic-gate 		return NSS_STR_PARSE_PARSE;
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	while (p < limit && isspace(*p)) {
206*0Sstevel@tonic-gate 		p++;
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 	/*
209*0Sstevel@tonic-gate 	 * Although nss_files_XY_all calls us with # stripped,
210*0Sstevel@tonic-gate 	 * we should be able to deal with it here in order to
211*0Sstevel@tonic-gate 	 * be more useful.
212*0Sstevel@tonic-gate 	 */
213*0Sstevel@tonic-gate 	if (p >= limit || *p == '#') { /* no aliases, no problem */
214*0Sstevel@tonic-gate 		char **ptr;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 		ptr = (char **) ROUND_UP(buffer + namelen + 1,
217*0Sstevel@tonic-gate 							sizeof (char *));
218*0Sstevel@tonic-gate 		if ((char *)ptr >= buffer + buflen) {
219*0Sstevel@tonic-gate 			rpc->r_aliases = 0; /* hope they don't try to peek in */
220*0Sstevel@tonic-gate 			trace3(TR_str2rpcent, 1, lenstr, buflen);
221*0Sstevel@tonic-gate 			return NSS_STR_PARSE_ERANGE;
222*0Sstevel@tonic-gate 		} else {
223*0Sstevel@tonic-gate 			*ptr = 0;
224*0Sstevel@tonic-gate 			rpc->r_aliases = ptr;
225*0Sstevel@tonic-gate 			trace3(TR_str2rpcent, 1, lenstr, buflen);
226*0Sstevel@tonic-gate 			return NSS_STR_PARSE_SUCCESS;
227*0Sstevel@tonic-gate 		}
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 	rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
230*0Sstevel@tonic-gate 			buffer + namelen + 1, (int)(buflen - namelen - 1));
231*0Sstevel@tonic-gate 	trace3(TR_str2rpcent, 1, lenstr, buflen);
232*0Sstevel@tonic-gate 	return NSS_STR_PARSE_SUCCESS;
233*0Sstevel@tonic-gate }
234