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 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  *
27  * Rentrant (MT-safe) getrpcYY interfaces.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <ctype.h>
33 #include <nss_dbdefs.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <rpc/rpcent.h>
37 
38 extern int str2rpcent(const char *, int, void *, 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(nss_db_params_t *p)
47 {
48 	p->name	= NSS_DBNAM_RPC;
49 	p->default_config = NSS_DEFCONF_RPC;
50 }
51 
52 struct rpcent *
53 getrpcbyname_r(const char *name, struct rpcent *result, char *buffer,
54 								int buflen)
55 {
56 	nss_XbyY_args_t arg;
57 	nss_status_t	res;
58 
59 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
60 	arg.key.name	= name;
61 	arg.stayopen	= rpc_stayopen;
62 	res = nss_search(&db_root, _nss_initf_rpc,
63 		NSS_DBOP_RPC_BYNAME, &arg);
64 	arg.status = res;
65 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
66 }
67 
68 struct rpcent *
69 getrpcbynumber_r(const int number, struct rpcent *result, char *buffer,
70 								int buflen)
71 {
72 	nss_XbyY_args_t arg;
73 	nss_status_t	res;
74 
75 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
76 	arg.key.number	= number;
77 	arg.stayopen	= rpc_stayopen;
78 	res = nss_search(&db_root, _nss_initf_rpc,
79 		NSS_DBOP_RPC_BYNUMBER, &arg);
80 	arg.status = res;
81 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
82 }
83 
84 void
85 setrpcent(const int stay)
86 {
87 	rpc_stayopen |= stay;
88 	nss_setent(&db_root, _nss_initf_rpc, &context);
89 }
90 
91 void
92 endrpcent(void)
93 {
94 	rpc_stayopen = 0;
95 	nss_endent(&db_root, _nss_initf_rpc, &context);
96 	nss_delete(&db_root);
97 }
98 
99 struct rpcent *
100 getrpcent_r(struct rpcent *result, char *buffer, int buflen)
101 {
102 	nss_XbyY_args_t arg;
103 	nss_status_t	res;
104 
105 	NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
106 	/* No key, no stayopen */
107 	res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
108 	arg.status = res;
109 	return ((struct rpcent *)NSS_XbyY_FINI(&arg));
110 }
111 
112 int
113 str2rpcent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
114 {
115 	struct rpcent	*rpc	= (struct rpcent *)ent;
116 	const char	*p, *numstart, *limit, *namestart;
117 	ssize_t		numlen, namelen = 0;
118 	char		numbuf[12];
119 	char		*numend;
120 
121 	if ((instr >= buffer && (buffer + buflen) > instr) ||
122 			(buffer >= instr && (instr + lenstr) > buffer))
123 		return (NSS_STR_PARSE_PARSE);
124 
125 	p = instr;
126 	limit = p + lenstr;
127 
128 	while (p < limit && isspace(*p)) {
129 		p++;
130 	}
131 	namestart = p;
132 	while (p < limit && !isspace(*p)) {
133 		p++;		/* Skip over the canonical name */
134 	}
135 	namelen = p - namestart;
136 
137 	if (buflen <= namelen)		/* not enough buffer */
138 		return (NSS_STR_PARSE_ERANGE);
139 	(void) memcpy(buffer, namestart, namelen);
140 	buffer[namelen] = '\0';
141 	rpc->r_name = buffer;
142 
143 	while (p < limit && isspace(*p)) {
144 		p++;
145 	}
146 	if (p >= limit)			/* Syntax error -- no RPC number */
147 		return (NSS_STR_PARSE_PARSE);
148 	numstart = p;
149 	do {
150 		p++;		/* Find the end of the RPC number */
151 	} while (p < limit && !isspace(*p));
152 	numlen = p - numstart;
153 	if (numlen >= sizeof (numbuf)) {
154 		/* Syntax error -- supposed number is too long */
155 		return (NSS_STR_PARSE_PARSE);
156 	}
157 	(void) memcpy(numbuf, numstart, numlen);
158 	numbuf[numlen] = '\0';
159 	rpc->r_number = (int)strtol(numbuf, &numend, 10);
160 	if (*numend != '\0')
161 		return (NSS_STR_PARSE_PARSE);
162 
163 	while (p < limit && isspace(*p)) {
164 		p++;
165 	}
166 	/*
167 	 * Although nss_files_XY_all calls us with # stripped,
168 	 * we should be able to deal with it here in order to
169 	 * be more useful.
170 	 */
171 	if (p >= limit || *p == '#') { /* no aliases, no problem */
172 		char **ptr;
173 
174 		ptr = (char **)ROUND_UP(buffer + namelen + 1,
175 							sizeof (char *));
176 		if ((char *)ptr >= buffer + buflen) {
177 			rpc->r_aliases = 0; /* hope they don't try to peek in */
178 			return (NSS_STR_PARSE_ERANGE);
179 		}
180 		*ptr = 0;
181 		rpc->r_aliases = ptr;
182 		return (NSS_STR_PARSE_SUCCESS);
183 	}
184 	rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
185 			buffer + namelen + 1, (int)(buflen - namelen - 1));
186 	return (NSS_STR_PARSE_SUCCESS);
187 }
188