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 <string.h>
2N/A#include <nss_dbdefs.h>
2N/A#include <libtsnet.h>
2N/A#include <secdb.h>
2N/A#include <nss.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_tp_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/A
2N/Astatic void
2N/A_nss_initf_tsol_tp(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_TSOL_TP;
2N/A p->default_config = NSS_DEFCONF_TSOL_TP;
2N/A}
2N/A
2N/Atsol_tpent_t *
2N/Atsol_gettpbyname(const char *name)
2N/A{
2N/A int err = 0;
2N/A char *errstr = NULL;
2N/A char buf[NSS_BUFLEN_TSOL_TP];
2N/A tsol_tpstr_t result;
2N/A tsol_tpstr_t *tpstrp = NULL;
2N/A nss_XbyY_args_t arg;
2N/A
2N/A NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
2N/A
2N/A arg.key.name = name;
2N/A arg.stayopen = tsol_tp_stayopen;
2N/A arg.h_errno = TSOL_NOT_FOUND;
2N/A arg.status = nss_search(&db_root, _nss_initf_tsol_tp,
2N/A NSS_DBOP_TSOL_TP_BYNAME, &arg);
2N/A tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
2N/A
2N/A#ifdef DEBUG
2N/A (void) fprintf(stdout, "tsol_gettpbyname %s: %s\n",
2N/A name, tpstrp ? tpstrp->tpstr_template : "NULL");
2N/A#endif /* DEBUG */
2N/A
2N/A if (tpstrp == NULL)
2N/A return (NULL);
2N/A
2N/A return (tpstr_to_ent(tpstrp, &err, &errstr));
2N/A}
2N/A
2N/Avoid
2N/Atsol_settpent(int stay)
2N/A{
2N/A tsol_tp_stayopen |= stay;
2N/A nss_setent(&db_root, _nss_initf_tsol_tp, &context);
2N/A}
2N/A
2N/Avoid
2N/Atsol_endtpent(void)
2N/A{
2N/A tsol_tp_stayopen = 0;
2N/A nss_endent(&db_root, _nss_initf_tsol_tp, &context);
2N/A nss_delete(&db_root);
2N/A}
2N/A
2N/Atsol_tpent_t *
2N/Atsol_gettpent(void)
2N/A{
2N/A int err = 0;
2N/A char *errstr = NULL;
2N/A char buf[NSS_BUFLEN_TSOL_TP];
2N/A tsol_tpstr_t result;
2N/A tsol_tpstr_t *tpstrp = NULL;
2N/A nss_XbyY_args_t arg;
2N/A
2N/A NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
2N/A /* No key, no stayopen */
2N/A arg.status = nss_getent(&db_root, _nss_initf_tsol_tp, &context, &arg);
2N/A tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
2N/A
2N/A#ifdef DEBUG
2N/A (void) fprintf(stdout, "tsol_gettpent: %s\n",
2N/A tpstrp ? tpstrp->tpstr_template : "NULL");
2N/A#endif /* DEBUG */
2N/A
2N/A if (tpstrp == NULL)
2N/A return (NULL);
2N/A
2N/A return (tpstr_to_ent(tpstrp, &err, &errstr));
2N/A}
2N/A
2N/Atsol_tpent_t *
2N/Atsol_fgettpent(FILE *f, boolean_t *error)
2N/A{
2N/A int err = 0;
2N/A char *errstr = NULL;
2N/A char buf[NSS_BUFLEN_TSOL_TP];
2N/A tsol_tpstr_t result;
2N/A tsol_tpstr_t *tpstrp = NULL;
2N/A tsol_tpent_t *tpentp = NULL;
2N/A nss_XbyY_args_t arg;
2N/A
2N/A NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
2N/A _nss_XbyY_fgets(f, &arg);
2N/A tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
2N/A if (tpstrp == NULL)
2N/A return (NULL);
2N/A tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
2N/A while (tpentp == 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 tnrhtp 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 tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
2N/A if (tpstrp == NULL) /* EOF */
2N/A return (NULL);
2N/A tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
2N/A }
2N/A return (tpentp);
2N/A}
2N/A
2N/A/*
2N/A * This is the callback routine for nss. It just wraps the tsol_sgettpent
2N/A * parser.
2N/A */
2N/Aint
2N/Astr_to_tpstr(const char *instr, int lenstr, void *entp, char *buffer,
2N/A int buflen)
2N/A{
2N/A int len;
2N/A char *last = NULL;
2N/A char *sep = KV_TOKEN_DELIMIT;
2N/A tsol_tpstr_t *tpstrp = (tsol_tpstr_t *)entp;
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 tpstrp->tpstr_template = _strtok_escape(buffer, sep, &last);
2N/A tpstrp->attrs = _strtok_escape(NULL, sep, &last);
2N/A if (tpstrp->attrs != NULL) {
2N/A len = strlen(tpstrp->attrs);
2N/A if (tpstrp->attrs[len - 1] == '\n')
2N/A tpstrp->attrs[len - 1] = '\0';
2N/A }
2N/A
2N/A#ifdef DEBUG
2N/A (void) fprintf(stdout,
2N/A "str_to_tpstr:\nstr - %s\n\ttemplate - %s\n\tattrs - %s\n",
2N/A instr, tpstrp->tpstr_template ? tpstrp->tpstr_template : "NULL",
2N/A tpstrp->attrs ? tpstrp->attrs : "NULL");
2N/A#endif /* DEBUG */
2N/A
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}