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