2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * Rentrant (MT-safe) getrpcYY interfaces.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "mt.h"
2N/A#include <ctype.h>
2N/A#include <nss_dbdefs.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <rpc/rpcent.h>
2N/A
2N/Aextern int str2rpcent(const char *, int, void *, char *, int);
2N/A
2N/Astatic int rpc_stayopen; /* Unsynchronized, but it affects only */
2N/A /* efficiency, not correctness */
2N/Astatic DEFINE_NSS_DB_ROOT(db_root);
2N/Astatic DEFINE_NSS_GETENT(context);
2N/A
2N/Avoid
2N/A_nss_initf_rpc(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_RPC;
2N/A p->default_config = NSS_DEFCONF_RPC;
2N/A}
2N/A
2N/Astruct rpcent *
2N/Agetrpcbyname_r(const char *name, struct rpcent *result, char *buffer,
2N/A int buflen)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A if (name == (const char *)NULL) {
2N/A errno = ERANGE;
2N/A return (NULL);
2N/A }
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
2N/A arg.key.name = name;
2N/A arg.stayopen = rpc_stayopen;
2N/A res = nss_search(&db_root, _nss_initf_rpc,
2N/A NSS_DBOP_RPC_BYNAME, &arg);
2N/A arg.status = res;
2N/A return ((struct rpcent *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/Astruct rpcent *
2N/Agetrpcbynumber_r(const int number, struct rpcent *result, char *buffer,
2N/A int buflen)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
2N/A arg.key.number = number;
2N/A arg.stayopen = rpc_stayopen;
2N/A res = nss_search(&db_root, _nss_initf_rpc,
2N/A NSS_DBOP_RPC_BYNUMBER, &arg);
2N/A arg.status = res;
2N/A return ((struct rpcent *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/Avoid
2N/Asetrpcent(const int stay)
2N/A{
2N/A rpc_stayopen |= stay;
2N/A nss_setent(&db_root, _nss_initf_rpc, &context);
2N/A}
2N/A
2N/Avoid
2N/Aendrpcent(void)
2N/A{
2N/A rpc_stayopen = 0;
2N/A nss_endent(&db_root, _nss_initf_rpc, &context);
2N/A nss_delete(&db_root);
2N/A}
2N/A
2N/Astruct rpcent *
2N/Agetrpcent_r(struct rpcent *result, char *buffer, int buflen)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A NSS_XbyY_INIT(&arg, result, buffer, buflen, str2rpcent);
2N/A /* No key, no stayopen */
2N/A res = nss_getent(&db_root, _nss_initf_rpc, &context, &arg);
2N/A arg.status = res;
2N/A return ((struct rpcent *)NSS_XbyY_FINI(&arg));
2N/A}
2N/A
2N/Aint
2N/Astr2rpcent(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2N/A{
2N/A struct rpcent *rpc = (struct rpcent *)ent;
2N/A const char *p, *numstart, *limit, *namestart;
2N/A ssize_t numlen, namelen = 0;
2N/A char numbuf[12];
2N/A char *numend;
2N/A
2N/A if ((instr >= buffer && (buffer + buflen) > instr) ||
2N/A (buffer >= instr && (instr + lenstr) > buffer))
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A p = instr;
2N/A limit = p + lenstr;
2N/A
2N/A while (p < limit && isspace(*p)) {
2N/A p++;
2N/A }
2N/A namestart = p;
2N/A while (p < limit && !isspace(*p)) {
2N/A p++; /* Skip over the canonical name */
2N/A }
2N/A namelen = p - namestart;
2N/A
2N/A if (buflen <= namelen) /* not enough buffer */
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A (void) memcpy(buffer, namestart, namelen);
2N/A buffer[namelen] = '\0';
2N/A rpc->r_name = buffer;
2N/A
2N/A while (p < limit && isspace(*p)) {
2N/A p++;
2N/A }
2N/A if (p >= limit) /* Syntax error -- no RPC number */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A numstart = p;
2N/A do {
2N/A p++; /* Find the end of the RPC number */
2N/A } while (p < limit && !isspace(*p));
2N/A numlen = p - numstart;
2N/A if (numlen >= sizeof (numbuf)) {
2N/A /* Syntax error -- supposed number is too long */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A (void) memcpy(numbuf, numstart, numlen);
2N/A numbuf[numlen] = '\0';
2N/A rpc->r_number = (int)strtol(numbuf, &numend, 10);
2N/A if (*numend != '\0')
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A while (p < limit && isspace(*p)) {
2N/A p++;
2N/A }
2N/A /*
2N/A * Although nss_files_XY_all calls us with # stripped,
2N/A * we should be able to deal with it here in order to
2N/A * be more useful.
2N/A */
2N/A if (p >= limit || *p == '#') { /* no aliases, no problem */
2N/A char **ptr;
2N/A
2N/A ptr = (char **)ROUND_UP(buffer + namelen + 1,
2N/A sizeof (char *));
2N/A if ((char *)ptr >= buffer + buflen) {
2N/A rpc->r_aliases = 0; /* hope they don't try to peek in */
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A }
2N/A *ptr = 0;
2N/A rpc->r_aliases = ptr;
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A }
2N/A rpc->r_aliases = _nss_netdb_aliases(p, (int)(lenstr - (p - instr)),
2N/A buffer + namelen + 1, (int)(buflen - namelen - 1));
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}