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.
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.
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]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Rentrant (MT-safe) getrpcYY interfaces.
31 #pragma ident "%Z%%M% %I% %E% SMI"
35 #include <nss_dbdefs.h>
38 #include <rpc/rpcent.h>
40 extern int str2rpcent(const char *, int, void *, char *, int);
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
);
48 _nss_initf_rpc(nss_db_params_t
*p
)
50 p
->name
= NSS_DBNAM_RPC
;
51 p
->default_config
= NSS_DEFCONF_RPC
;
55 getrpcbyname_r(const char *name
, struct rpcent
*result
, char *buffer
,
61 if (name
== (const char *)NULL
) {
65 NSS_XbyY_INIT(&arg
, result
, buffer
, buflen
, str2rpcent
);
67 arg
.stayopen
= rpc_stayopen
;
68 res
= nss_search(&db_root
, _nss_initf_rpc
,
69 NSS_DBOP_RPC_BYNAME
, &arg
);
71 return ((struct rpcent
*)NSS_XbyY_FINI(&arg
));
75 getrpcbynumber_r(const int number
, struct rpcent
*result
, char *buffer
,
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
);
87 return ((struct rpcent
*)NSS_XbyY_FINI(&arg
));
91 setrpcent(const int stay
)
94 nss_setent(&db_root
, _nss_initf_rpc
, &context
);
101 nss_endent(&db_root
, _nss_initf_rpc
, &context
);
102 nss_delete(&db_root
);
106 getrpcent_r(struct rpcent
*result
, char *buffer
, int buflen
)
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
);
115 return ((struct rpcent
*)NSS_XbyY_FINI(&arg
));
119 str2rpcent(const char *instr
, int lenstr
, void *ent
, char *buffer
, int buflen
)
121 struct rpcent
*rpc
= (struct rpcent
*)ent
;
122 const char *p
, *numstart
, *limit
, *namestart
;
123 ssize_t numlen
, namelen
= 0;
127 if ((instr
>= buffer
&& (buffer
+ buflen
) > instr
) ||
128 (buffer
>= instr
&& (instr
+ lenstr
) > buffer
))
129 return (NSS_STR_PARSE_PARSE
);
134 while (p
< limit
&& isspace(*p
)) {
138 while (p
< limit
&& !isspace(*p
)) {
139 p
++; /* Skip over the canonical name */
141 namelen
= p
- namestart
;
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
;
149 while (p
< limit
&& isspace(*p
)) {
152 if (p
>= limit
) /* Syntax error -- no RPC number */
153 return (NSS_STR_PARSE_PARSE
);
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
);
163 (void) memcpy(numbuf
, numstart
, numlen
);
164 numbuf
[numlen
] = '\0';
165 rpc
->r_number
= (int)strtol(numbuf
, &numend
, 10);
167 return (NSS_STR_PARSE_PARSE
);
169 while (p
< limit
&& isspace(*p
)) {
173 * Although nss_files_XY_all calls us with # stripped,
174 * we should be able to deal with it here in order to
177 if (p
>= limit
|| *p
== '#') { /* no aliases, no problem */
180 ptr
= (char **)ROUND_UP(buffer
+ namelen
+ 1,
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
);
187 rpc
->r_aliases
= ptr
;
188 return (NSS_STR_PARSE_SUCCESS
);
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
);