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