10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*2830Sdjl * Common Development and Distribution License (the "License").
6*2830Sdjl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21132Srobinson
220Sstevel@tonic-gate /*
231219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24132Srobinson * Use is subject to license terms.
251219Sraf */
261219Sraf
271219Sraf /*
280Sstevel@tonic-gate * Rentrant (MT-safe) getrpcYY interfaces.
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
331219Sraf #include "mt.h"
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <nss_dbdefs.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <rpc/rpcent.h>
390Sstevel@tonic-gate
40132Srobinson extern int str2rpcent(const char *, int, void *, char *, int);
410Sstevel@tonic-gate
420Sstevel@tonic-gate static int rpc_stayopen; /* Unsynchronized, but it affects only */
430Sstevel@tonic-gate /* efficiency, not correctness */
440Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
450Sstevel@tonic-gate static DEFINE_NSS_GETENT(context);
460Sstevel@tonic-gate
470Sstevel@tonic-gate void
_nss_initf_rpc(nss_db_params_t * p)48132Srobinson _nss_initf_rpc(nss_db_params_t *p)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate p->name = NSS_DBNAM_RPC;
510Sstevel@tonic-gate p->default_config = NSS_DEFCONF_RPC;
520Sstevel@tonic-gate }
530Sstevel@tonic-gate
540Sstevel@tonic-gate struct rpcent *
getrpcbyname_r(const char * name,struct rpcent * result,char * buffer,int buflen)55132Srobinson getrpcbyname_r(const char *name, struct rpcent *result, char *buffer,
56132Srobinson int buflen)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate nss_XbyY_args_t arg;
590Sstevel@tonic-gate nss_status_t res;
600Sstevel@tonic-gate
61*2830Sdjl if (name == (const char *)NULL) {
62*2830Sdjl errno = ERANGE;
63*2830Sdjl return (NULL);
64*2830Sdjl }
650Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
660Sstevel@tonic-gate arg.key.name = name;
670Sstevel@tonic-gate arg.stayopen = rpc_stayopen;
680Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_rpc,
690Sstevel@tonic-gate NSS_DBOP_RPC_BYNAME, &arg);
700Sstevel@tonic-gate arg.status = res;
71132Srobinson return ((struct rpcent *)NSS_XbyY_FINI(&arg));
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate struct rpcent *
getrpcbynumber_r(const int number,struct rpcent * result,char * buffer,int buflen)75132Srobinson getrpcbynumber_r(const int number, struct rpcent *result, char *buffer,
76132Srobinson int buflen)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate nss_XbyY_args_t arg;
790Sstevel@tonic-gate nss_status_t res;
800Sstevel@tonic-gate
810Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
820Sstevel@tonic-gate arg.key.number = number;
830Sstevel@tonic-gate arg.stayopen = rpc_stayopen;
840Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_rpc,
850Sstevel@tonic-gate NSS_DBOP_RPC_BYNUMBER, &arg);
860Sstevel@tonic-gate arg.status = res;
87132Srobinson return ((struct rpcent *)NSS_XbyY_FINI(&arg));
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate void
setrpcent(const int stay)91132Srobinson setrpcent(const int stay)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate rpc_stayopen |= stay;
940Sstevel@tonic-gate nss_setent(&db_root, _nss_initf_rpc, &context);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate void
endrpcent(void)98132Srobinson endrpcent(void)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate rpc_stayopen = 0;
1010Sstevel@tonic-gate nss_endent(&db_root, _nss_initf_rpc, &context);
1020Sstevel@tonic-gate nss_delete(&db_root);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate struct rpcent *
getrpcent_r(struct rpcent * result,char * buffer,int buflen)106132Srobinson getrpcent_r(struct rpcent *result, char *buffer, int buflen)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate nss_XbyY_args_t arg;
1090Sstevel@tonic-gate nss_status_t res;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
1120Sstevel@tonic-gate /* No key, no stayopen */
1130Sstevel@tonic-gate res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
1140Sstevel@tonic-gate arg.status = res;
115132Srobinson return ((struct rpcent *)NSS_XbyY_FINI(&arg));
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate int
str2rpcent(const char * instr,int lenstr,void * ent,char * buffer,int buflen)119132Srobinson str2rpcent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate struct rpcent *rpc = (struct rpcent *)ent;
1220Sstevel@tonic-gate const char *p, *numstart, *limit, *namestart;
1230Sstevel@tonic-gate ssize_t numlen, namelen = 0;
1240Sstevel@tonic-gate char numbuf[12];
1250Sstevel@tonic-gate char *numend;
1260Sstevel@tonic-gate
127132Srobinson if ((instr >= buffer && (buffer + buflen) > instr) ||
128132Srobinson (buffer >= instr && (instr + lenstr) > buffer))
129132Srobinson return (NSS_STR_PARSE_PARSE);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate p = instr;
1320Sstevel@tonic-gate limit = p + lenstr;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate while (p < limit && isspace(*p)) {
1350Sstevel@tonic-gate p++;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate namestart = p;
1380Sstevel@tonic-gate while (p < limit && !isspace(*p)) {
1390Sstevel@tonic-gate p++; /* Skip over the canonical name */
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate namelen = p - namestart;
1420Sstevel@tonic-gate
143132Srobinson if (buflen <= namelen) /* not enough buffer */
144132Srobinson return (NSS_STR_PARSE_ERANGE);
1450Sstevel@tonic-gate (void) memcpy(buffer, namestart, namelen);
1460Sstevel@tonic-gate buffer[namelen] = '\0';
1470Sstevel@tonic-gate rpc->r_name = buffer;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate while (p < limit && isspace(*p)) {
1500Sstevel@tonic-gate p++;
1510Sstevel@tonic-gate }
152132Srobinson if (p >= limit) /* Syntax error -- no RPC number */
153132Srobinson return (NSS_STR_PARSE_PARSE);
1540Sstevel@tonic-gate numstart = p;
1550Sstevel@tonic-gate do {
1560Sstevel@tonic-gate p++; /* Find the end of the RPC number */
1570Sstevel@tonic-gate } while (p < limit && !isspace(*p));
1580Sstevel@tonic-gate numlen = p - numstart;
1590Sstevel@tonic-gate if (numlen >= sizeof (numbuf)) {
1600Sstevel@tonic-gate /* Syntax error -- supposed number is too long */
161132Srobinson return (NSS_STR_PARSE_PARSE);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate (void) memcpy(numbuf, numstart, numlen);
1640Sstevel@tonic-gate numbuf[numlen] = '\0';
1650Sstevel@tonic-gate rpc->r_number = (int)strtol(numbuf, &numend, 10);
166132Srobinson if (*numend != '\0')
167132Srobinson return (NSS_STR_PARSE_PARSE);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate while (p < limit && isspace(*p)) {
1700Sstevel@tonic-gate p++;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * Although nss_files_XY_all calls us with # stripped,
1740Sstevel@tonic-gate * we should be able to deal with it here in order to
1750Sstevel@tonic-gate * be more useful.
1760Sstevel@tonic-gate */
1770Sstevel@tonic-gate if (p >= limit || *p == '#') { /* no aliases, no problem */
1780Sstevel@tonic-gate char **ptr;
1790Sstevel@tonic-gate
180132Srobinson ptr = (char **)ROUND_UP(buffer + namelen + 1,
1810Sstevel@tonic-gate sizeof (char *));
1820Sstevel@tonic-gate if ((char *)ptr >= buffer + buflen) {
1830Sstevel@tonic-gate rpc->r_aliases = 0; /* hope they don't try to peek in */
184132Srobinson return (NSS_STR_PARSE_ERANGE);
1850Sstevel@tonic-gate }
186132Srobinson *ptr = 0;
187132Srobinson rpc->r_aliases = ptr;
188132Srobinson return (NSS_STR_PARSE_SUCCESS);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
1910Sstevel@tonic-gate buffer + namelen + 1, (int)(buflen - namelen - 1));
192132Srobinson return (NSS_STR_PARSE_SUCCESS);
1930Sstevel@tonic-gate }
194