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 (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <ctype.h>
2N/A#include <nss_dbdefs.h>
2N/A#include <libtsnet.h>
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <string.h>
2N/A#include <secdb.h>
2N/A#include <nss.h>
2N/A#include <libtsnet.h>
2N/A#include <libintl.h>
2N/A
2N/Aextern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *); /* from lib.c */
2N/A
2N/Astatic int tsol_rh_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/Astatic void
2N/A_nss_initf_tsol_rh(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_TSOL_RH;
2N/A p->default_config = NSS_DEFCONF_TSOL_RH;
2N/A}
2N/A
2N/Atsol_rhent_t *
2N/Atsol_getrhbyaddr(const void *addrp, size_t len, int af)
2N/A{
2N/A int err = 0;
2N/A char *errstr = NULL;
2N/A char buf[NSS_BUFLEN_TSOL_RH];
2N/A tsol_rhstr_t result;
2N/A tsol_rhstr_t *rhstrp = NULL;
2N/A nss_XbyY_args_t arg;
2N/A
2N/A NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
2N/A
2N/A arg.key.hostaddr.addr = (const char *)addrp;
2N/A arg.key.hostaddr.len = len;
2N/A arg.key.hostaddr.type = af;
2N/A arg.stayopen = tsol_rh_stayopen;
2N/A arg.h_errno = TSOL_NOT_FOUND;
2N/A arg.status = nss_search(&db_root, _nss_initf_tsol_rh,
2N/A NSS_DBOP_TSOL_RH_BYADDR, &arg);
2N/A rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
2N/A
2N/A#ifdef DEBUG
2N/A (void) fprintf(stdout, "tsol_getrhbyaddr %s: %s\n",
2N/A (char *)addrp, rhstrp ? rhstrp->rhstr_template : "NULL");
2N/A#endif /* DEBUG */
2N/A
2N/A if (rhstrp == NULL)
2N/A return (NULL);
2N/A
2N/A return (rhstr_to_ent(rhstrp, &err, &errstr));
2N/A}
2N/A
2N/Avoid
2N/Atsol_setrhent(int stay)
2N/A{
2N/A tsol_rh_stayopen |= stay;
2N/A nss_setent(&db_root, _nss_initf_tsol_rh, &context);
2N/A}
2N/A
2N/Avoid
2N/Atsol_endrhent(void)
2N/A{
2N/A tsol_rh_stayopen = 0;
2N/A nss_endent(&db_root, _nss_initf_tsol_rh, &context);
2N/A nss_delete(&db_root);
2N/A}
2N/A
2N/Atsol_rhent_t *
2N/Atsol_getrhent(void)
2N/A{
2N/A int err = 0;
2N/A char *errstr = NULL;
2N/A char buf[NSS_BUFLEN_TSOL_RH];
2N/A tsol_rhstr_t result;
2N/A tsol_rhstr_t *rhstrp = NULL;
2N/A nss_XbyY_args_t arg;
2N/A
2N/A NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
2N/A /* No key, no stayopen */
2N/A arg.status = nss_getent(&db_root, _nss_initf_tsol_rh, &context, &arg);
2N/A rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
2N/A
2N/A#ifdef DEBUG
2N/A (void) fprintf(stdout, "tsol_getrhent: %s\n",
2N/A rhstrp ? rhstrp->rhstr_template : "NULL");
2N/A#endif /* DEBUG */
2N/A
2N/A if (rhstrp == NULL)
2N/A return (NULL);
2N/A
2N/A return (rhstr_to_ent(rhstrp, &err, &errstr));
2N/A}
2N/A
2N/Atsol_rhent_t *
2N/Atsol_fgetrhent(FILE *f, boolean_t *error)
2N/A{
2N/A int err = 0;
2N/A char *errstr = NULL;
2N/A char buf[NSS_BUFLEN_TSOL_RH];
2N/A tsol_rhstr_t result;
2N/A tsol_rhstr_t *rhstrp = NULL;
2N/A tsol_rhent_t *rhentp = NULL;
2N/A nss_XbyY_args_t arg;
2N/A
2N/A NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
2N/A _nss_XbyY_fgets(f, &arg);
2N/A rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
2N/A if (rhstrp == NULL)
2N/A return (NULL);
2N/A rhentp = rhstr_to_ent(rhstrp, &err, &errstr);
2N/A while (rhentp == NULL) {
2N/A /*
2N/A * Loop until we find a non-blank, non-comment line, or
2N/A * until EOF. No need to log blank lines, comments.
2N/A */
2N/A if (err != LTSNET_EMPTY) {
2N/A (void) fprintf(stderr, "%s: %.32s%s: %s\n",
2N/A gettext("Error parsing tnrhdb file"), errstr,
2N/A (strlen(errstr) > 32)? "...": "",
2N/A (char *)tsol_strerror(err, errno));
2N/A *error = B_TRUE;
2N/A }
2N/A _nss_XbyY_fgets(f, &arg);
2N/A rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
2N/A if (rhstrp == NULL) /* EOF */
2N/A return (NULL);
2N/A rhentp = rhstr_to_ent(rhstrp, &err, &errstr);
2N/A }
2N/A return (rhentp);
2N/A}
2N/A
2N/A/*
2N/A * This is the callback routine for nss.
2N/A */
2N/Aint
2N/Astr_to_rhstr(const char *instr, int lenstr, void *entp, char *buffer,
2N/A int buflen)
2N/A{
2N/A int len;
2N/A char *str = NULL;
2N/A char *last = NULL;
2N/A char *sep = KV_TOKEN_DELIMIT;
2N/A tsol_rhstr_t *rhstrp = (tsol_rhstr_t *)entp;
2N/A char *ptr;
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 if (lenstr >= buflen)
2N/A return (NSS_STR_PARSE_ERANGE);
2N/A (void) strncpy(buffer, instr, buflen);
2N/A
2N/A /*
2N/A * Remove trailing white spaces before parsing buffer
2N/A */
2N/A if (buffer != NULL) {
2N/A for (ptr = buffer + strlen(buffer) - 1;
2N/A isspace(*ptr) && (ptr >= buffer); ptr--) {
2N/A *ptr = '\0';
2N/A }
2N/A }
2N/A
2N/A str = _strtok_escape(buffer, sep, &last);
2N/A rhstrp->address = _do_unescape(str);
2N/A /*
2N/A * _do_unesape uses isspace() which removes "\n".
2N/A * we keep "\n" as we use it in checking for
2N/A * blank lines.
2N/A */
2N/A if (strcmp(instr, "\n") == 0)
2N/A rhstrp->address = "\n";
2N/A rhstrp->rhstr_template = _strtok_escape(NULL, sep, &last);
2N/A if (rhstrp->rhstr_template != NULL) {
2N/A len = strlen(rhstrp->rhstr_template);
2N/A if (rhstrp->rhstr_template[len - 1] == '\n')
2N/A rhstrp->rhstr_template[len - 1] = '\0';
2N/A }
2N/A if (rhstrp->address == NULL)
2N/A rhstrp->family = 0;
2N/A else if (strchr(rhstrp->address, ':') == NULL)
2N/A rhstrp->family = AF_INET;
2N/A else
2N/A rhstrp->family = AF_INET6;
2N/A
2N/A#ifdef DEBUG
2N/A (void) fprintf(stdout,
2N/A "str_to_rhstr:str - %s\taddress - %s\n\ttemplate - %s\n",
2N/A instr, rhstrp->address ? rhstrp->address : "NULL",
2N/A rhstrp->rhstr_template ? rhstrp->rhstr_template : "NULL");
2N/A#endif /* DEBUG */
2N/A
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}
2N/A
2N/Atsol_host_type_t
2N/Atsol_getrhtype(char *rhost) {
2N/A int herr;
2N/A struct hostent *hp;
2N/A in6_addr_t in6;
2N/A char abuf[INET6_ADDRSTRLEN];
2N/A tsol_rhent_t rhent;
2N/A tsol_tpent_t tp;
2N/A
2N/A if ((hp = getipnodebyname(rhost, AF_INET6,
2N/A AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &herr)) == NULL) {
2N/A return (UNLABELED);
2N/A }
2N/A
2N/A (void) memset(&rhent, 0, sizeof (rhent));
2N/A (void) memcpy(&in6, hp->h_addr, hp->h_length);
2N/A
2N/A if (IN6_IS_ADDR_V4MAPPED(&in6)) {
2N/A rhent.rh_address.ta_family = AF_INET;
2N/A IN6_V4MAPPED_TO_INADDR(&in6, &rhent.rh_address.ta_addr_v4);
2N/A (void) inet_ntop(AF_INET, &rhent.rh_address.ta_addr_v4, abuf,
2N/A sizeof (abuf));
2N/A } else {
2N/A rhent.rh_address.ta_family = AF_INET6;
2N/A rhent.rh_address.ta_addr_v6 = in6;
2N/A (void) inet_ntop(AF_INET6, &in6, abuf, sizeof (abuf));
2N/A }
2N/A
2N/A if (tnrh(TNDB_GET, &rhent) != 0)
2N/A return (UNLABELED);
2N/A
2N/A if (rhent.rh_template[0] == '\0')
2N/A return (UNLABELED);
2N/A
2N/A (void) strlcpy(tp.name, rhent.rh_template, sizeof (tp.name));
2N/A
2N/A if (tnrhtp(TNDB_GET, &tp) != 0)
2N/A return (UNLABELED);
2N/A
2N/A return (tp.host_type);
2N/A}