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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <ctype.h>
2N/A#include <netdb.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <nss_dbdefs.h>
2N/A
2N/Aint str2protoent(const char *, int, void *,
2N/A char *, int);
2N/A
2N/Astatic int proto_stayopen;
2N/A/*
2N/A * Unsynchronized, but it affects only
2N/A * efficiency, not correctness
2N/A */
2N/A
2N/Astatic DEFINE_NSS_DB_ROOT(db_root);
2N/Astatic DEFINE_NSS_GETENT(context);
2N/A
2N/Avoid
2N/A_nss_initf_proto(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_PROTOCOLS;
2N/A p->default_config = NSS_DEFCONF_PROTOCOLS;
2N/A}
2N/A
2N/Astruct protoent *
2N/Agetprotobyname_r(const char *name, struct protoent *result,
2N/A char *buffer, 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, str2protoent);
2N/A arg.key.name = name;
2N/A arg.stayopen = proto_stayopen;
2N/A res = nss_search(&db_root, _nss_initf_proto,
2N/A NSS_DBOP_PROTOCOLS_BYNAME, &arg);
2N/A arg.status = res;
2N/A (void) NSS_XbyY_FINI(&arg);
2N/A return ((struct protoent *)arg.returnval);
2N/A}
2N/A
2N/Astruct protoent *
2N/Agetprotobynumber_r(int proto, struct protoent *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, str2protoent);
2N/A arg.key.number = proto;
2N/A arg.stayopen = proto_stayopen;
2N/A res = nss_search(&db_root, _nss_initf_proto,
2N/A NSS_DBOP_PROTOCOLS_BYNUMBER, &arg);
2N/A arg.status = res;
2N/A (void) NSS_XbyY_FINI(&arg);
2N/A return ((struct protoent *)arg.returnval);
2N/A}
2N/A
2N/Aint
2N/Asetprotoent(int stay)
2N/A{
2N/A proto_stayopen = stay;
2N/A nss_setent(&db_root, _nss_initf_proto, &context);
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Aendprotoent()
2N/A{
2N/A proto_stayopen = 0;
2N/A nss_endent(&db_root, _nss_initf_proto, &context);
2N/A nss_delete(&db_root);
2N/A return (0);
2N/A}
2N/A
2N/Astruct protoent *
2N/Agetprotoent_r(struct protoent *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, str2protoent);
2N/A /* No stayopen flag; of course you stay open for iteration */
2N/A res = nss_getent(&db_root, _nss_initf_proto, &context, &arg);
2N/A arg.status = res;
2N/A (void) NSS_XbyY_FINI(&arg);
2N/A return ((struct protoent *)arg.returnval);
2N/A}
2N/A
2N/A/*
2N/A * Return values: 0 = success, 1 = parse error, 2 = erange ...
2N/A * The structure pointer passed in is a structure in the caller's space
2N/A * wherein the field pointers would be set to areas in the buffer if
2N/A * need be. instring and buffer should be separate areas. Let's not
2N/A * fight over crumbs.
2N/A */
2N/Aint
2N/Astr2protoent(const char *instr, int lenstr,
2N/A void *ent /* it is really (struct protoent *) */,
2N/A char *buffer, int buflen)
2N/A{
2N/A struct protoent *proto = (struct protoent *)ent;
2N/A const char *p, *numstart, *namestart, *limit;
2N/A int numlen, namelen = 0;
2N/A char numbuf[16];
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
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 = (int)(p - namestart);
2N/A
2N/A if (buflen <= namelen) { /* not enough buffer */
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A }
2N/A (void) memcpy(buffer, namestart, namelen);
2N/A buffer[namelen] = '\0';
2N/A proto->p_name = buffer;
2N/A
2N/A while (p < limit && isspace(*p)) {
2N/A p++;
2N/A }
2N/A if (p >= limit) {
2N/A /* Syntax error -- no proto number */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
2N/A numstart = p;
2N/A do {
2N/A p++; /* Find the end of the proto number */
2N/A } while (p < limit && !isspace(*p));
2N/A numlen = (int)(p - numstart);
2N/A if (numlen >= (int)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, (size_t)numlen);
2N/A numbuf[numlen] = '\0';
2N/A proto->p_proto = (int)strtol(numbuf, &numend, 10);
2N/A if (*numend != '\0') {
2N/A /* Syntax error -- protocol number isn't a number */
2N/A return (NSS_STR_PARSE_PARSE);
2N/A }
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 /* hope they don't try to peek in */
2N/A proto->p_aliases = 0;
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A } else {
2N/A *ptr = 0;
2N/A proto->p_aliases = ptr;
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A }
2N/A }
2N/A proto->p_aliases = _nss_netdb_aliases(p, lenstr - (int)(p - instr),
2N/A buffer + namelen + 1, buflen - namelen - 1);
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}