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