145916cd2Sjpk /*
245916cd2Sjpk * CDDL HEADER START
345916cd2Sjpk *
445916cd2Sjpk * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
745916cd2Sjpk *
845916cd2Sjpk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945916cd2Sjpk * or http://www.opensolaris.org/os/licensing.
1045916cd2Sjpk * See the License for the specific language governing permissions
1145916cd2Sjpk * and limitations under the License.
1245916cd2Sjpk *
1345916cd2Sjpk * When distributing Covered Code, include this CDDL HEADER in each
1445916cd2Sjpk * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545916cd2Sjpk * If applicable, add the following below this CDDL HEADER, with the
1645916cd2Sjpk * fields enclosed by brackets "[]" replaced with your own identifying
1745916cd2Sjpk * information: Portions Copyright [yyyy] [name of copyright owner]
1845916cd2Sjpk *
1945916cd2Sjpk * CDDL HEADER END
2045916cd2Sjpk */
2145916cd2Sjpk /*
22*909c1a33Ston * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2345916cd2Sjpk * Use is subject to license terms.
2445916cd2Sjpk *
2545916cd2Sjpk * From "tsol_gettpent.c 7.13 00/10/13 SMI; TSOL 2.x"
2645916cd2Sjpk */
2745916cd2Sjpk
2845916cd2Sjpk #pragma ident "%Z%%M% %I% %E% SMI"
2945916cd2Sjpk
3045916cd2Sjpk #include <stdio.h>
3145916cd2Sjpk #include <string.h>
3245916cd2Sjpk #include <nss_dbdefs.h>
3345916cd2Sjpk #include <libtsnet.h>
3445916cd2Sjpk #include <secdb.h>
3545916cd2Sjpk #include <nss.h>
3645916cd2Sjpk #include <libintl.h>
3745916cd2Sjpk
3845916cd2Sjpk extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *); /* from lib.c */
3945916cd2Sjpk
4045916cd2Sjpk static int tsol_tp_stayopen; /* Unsynchronized, but it affects only */
4145916cd2Sjpk /* efficiency, not correctness */
4245916cd2Sjpk static DEFINE_NSS_DB_ROOT(db_root);
4345916cd2Sjpk static DEFINE_NSS_GETENT(context);
4445916cd2Sjpk
4545916cd2Sjpk
4645916cd2Sjpk static void
_nss_initf_tsol_tp(nss_db_params_t * p)4745916cd2Sjpk _nss_initf_tsol_tp(nss_db_params_t *p)
4845916cd2Sjpk {
4945916cd2Sjpk p->name = NSS_DBNAM_TSOL_TP;
5045916cd2Sjpk p->default_config = NSS_DEFCONF_TSOL_TP;
5145916cd2Sjpk }
5245916cd2Sjpk
5345916cd2Sjpk tsol_tpent_t *
tsol_gettpbyname(const char * name)5445916cd2Sjpk tsol_gettpbyname(const char *name)
5545916cd2Sjpk {
5645916cd2Sjpk int err = 0;
5745916cd2Sjpk char *errstr = NULL;
5845916cd2Sjpk char buf[NSS_BUFLEN_TSOL_TP];
5945916cd2Sjpk tsol_tpstr_t result;
6045916cd2Sjpk tsol_tpstr_t *tpstrp = NULL;
6145916cd2Sjpk nss_XbyY_args_t arg;
6245916cd2Sjpk
6345916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
6445916cd2Sjpk
6545916cd2Sjpk arg.key.name = name;
6645916cd2Sjpk arg.stayopen = tsol_tp_stayopen;
6745916cd2Sjpk arg.h_errno = TSOL_NOT_FOUND;
6845916cd2Sjpk arg.status = nss_search(&db_root, _nss_initf_tsol_tp,
6945916cd2Sjpk NSS_DBOP_TSOL_TP_BYNAME, &arg);
7045916cd2Sjpk tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
7145916cd2Sjpk
7245916cd2Sjpk #ifdef DEBUG
7345916cd2Sjpk (void) fprintf(stdout, "tsol_gettpbyname %s: %s\n",
7445916cd2Sjpk name, tpstrp ? tpstrp->template : "NULL");
7545916cd2Sjpk #endif /* DEBUG */
7645916cd2Sjpk
7745916cd2Sjpk if (tpstrp == NULL)
7845916cd2Sjpk return (NULL);
7945916cd2Sjpk
8045916cd2Sjpk return (tpstr_to_ent(tpstrp, &err, &errstr));
8145916cd2Sjpk }
8245916cd2Sjpk
8345916cd2Sjpk void
tsol_settpent(int stay)8445916cd2Sjpk tsol_settpent(int stay)
8545916cd2Sjpk {
8645916cd2Sjpk tsol_tp_stayopen |= stay;
8745916cd2Sjpk nss_setent(&db_root, _nss_initf_tsol_tp, &context);
8845916cd2Sjpk }
8945916cd2Sjpk
9045916cd2Sjpk void
tsol_endtpent(void)9145916cd2Sjpk tsol_endtpent(void)
9245916cd2Sjpk {
9345916cd2Sjpk tsol_tp_stayopen = 0;
9445916cd2Sjpk nss_endent(&db_root, _nss_initf_tsol_tp, &context);
9545916cd2Sjpk nss_delete(&db_root);
9645916cd2Sjpk }
9745916cd2Sjpk
9845916cd2Sjpk tsol_tpent_t *
tsol_gettpent(void)9945916cd2Sjpk tsol_gettpent(void)
10045916cd2Sjpk {
10145916cd2Sjpk int err = 0;
10245916cd2Sjpk char *errstr = NULL;
10345916cd2Sjpk char buf[NSS_BUFLEN_TSOL_TP];
10445916cd2Sjpk tsol_tpstr_t result;
10545916cd2Sjpk tsol_tpstr_t *tpstrp = NULL;
10645916cd2Sjpk nss_XbyY_args_t arg;
10745916cd2Sjpk
10845916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
10945916cd2Sjpk /* No key, no stayopen */
11045916cd2Sjpk arg.status = nss_getent(&db_root, _nss_initf_tsol_tp, &context, &arg);
11145916cd2Sjpk tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
11245916cd2Sjpk
11345916cd2Sjpk #ifdef DEBUG
11445916cd2Sjpk (void) fprintf(stdout, "tsol_gettpent: %s\n",
11545916cd2Sjpk tpstrp ? tpstrp->template : "NULL");
11645916cd2Sjpk #endif /* DEBUG */
11745916cd2Sjpk
11845916cd2Sjpk if (tpstrp == NULL)
11945916cd2Sjpk return (NULL);
12045916cd2Sjpk
12145916cd2Sjpk return (tpstr_to_ent(tpstrp, &err, &errstr));
12245916cd2Sjpk }
12345916cd2Sjpk
12445916cd2Sjpk tsol_tpent_t *
tsol_fgettpent(FILE * f,boolean_t * error)125*909c1a33Ston tsol_fgettpent(FILE *f, boolean_t *error)
12645916cd2Sjpk {
12745916cd2Sjpk int err = 0;
12845916cd2Sjpk char *errstr = NULL;
12945916cd2Sjpk char buf[NSS_BUFLEN_TSOL_TP];
13045916cd2Sjpk tsol_tpstr_t result;
13145916cd2Sjpk tsol_tpstr_t *tpstrp = NULL;
13245916cd2Sjpk tsol_tpent_t *tpentp = NULL;
13345916cd2Sjpk nss_XbyY_args_t arg;
13445916cd2Sjpk
13545916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
13645916cd2Sjpk _nss_XbyY_fgets(f, &arg);
13745916cd2Sjpk tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
13845916cd2Sjpk if (tpstrp == NULL)
13945916cd2Sjpk return (NULL);
14045916cd2Sjpk tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
14145916cd2Sjpk while (tpentp == NULL) {
14245916cd2Sjpk /*
14345916cd2Sjpk * Loop until we find a non-blank, non-comment line, or
14445916cd2Sjpk * until EOF. No need to log blank lines, comments.
14545916cd2Sjpk */
146*909c1a33Ston if (err != LTSNET_EMPTY) {
14745916cd2Sjpk (void) fprintf(stderr, "%s: %.32s%s: %s\n",
14845916cd2Sjpk gettext("Error parsing tnrhtp file"), errstr,
14945916cd2Sjpk (strlen(errstr) > 32)? "...": "",
15045916cd2Sjpk (char *)tsol_strerror(err, errno));
151*909c1a33Ston *error = B_TRUE;
152*909c1a33Ston }
15345916cd2Sjpk _nss_XbyY_fgets(f, &arg);
15445916cd2Sjpk tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
15545916cd2Sjpk if (tpstrp == NULL) /* EOF */
15645916cd2Sjpk return (NULL);
15745916cd2Sjpk tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
15845916cd2Sjpk }
15945916cd2Sjpk return (tpentp);
16045916cd2Sjpk }
16145916cd2Sjpk
16245916cd2Sjpk /*
16345916cd2Sjpk * This is the callback routine for nss. It just wraps the tsol_sgettpent
16445916cd2Sjpk * parser.
16545916cd2Sjpk */
16645916cd2Sjpk int
str_to_tpstr(const char * instr,int lenstr,void * entp,char * buffer,int buflen)16745916cd2Sjpk str_to_tpstr(const char *instr, int lenstr, void *entp, char *buffer,
16845916cd2Sjpk int buflen)
16945916cd2Sjpk {
17045916cd2Sjpk int len;
17145916cd2Sjpk char *last = NULL;
17245916cd2Sjpk char *sep = KV_TOKEN_DELIMIT;
17345916cd2Sjpk tsol_tpstr_t *tpstrp = (tsol_tpstr_t *)entp;
17445916cd2Sjpk
17545916cd2Sjpk if ((instr >= buffer && (buffer + buflen) > instr) ||
17645916cd2Sjpk (buffer >= instr && (instr + lenstr) > buffer))
17745916cd2Sjpk return (NSS_STR_PARSE_PARSE);
17845916cd2Sjpk if (lenstr >= buflen)
17945916cd2Sjpk return (NSS_STR_PARSE_ERANGE);
18045916cd2Sjpk (void) strncpy(buffer, instr, buflen);
18145916cd2Sjpk tpstrp->template = _strtok_escape(buffer, sep, &last);
18245916cd2Sjpk tpstrp->attrs = _strtok_escape(NULL, sep, &last);
18345916cd2Sjpk if (tpstrp->attrs != NULL) {
18445916cd2Sjpk len = strlen(tpstrp->attrs);
18545916cd2Sjpk if (tpstrp->attrs[len - 1] == '\n')
18645916cd2Sjpk tpstrp->attrs[len - 1] = '\0';
18745916cd2Sjpk }
18845916cd2Sjpk
18945916cd2Sjpk #ifdef DEBUG
19045916cd2Sjpk (void) fprintf(stdout,
19145916cd2Sjpk "str_to_tpstr:\nstr - %s\n\ttemplate - %s\n\tattrs - %s\n",
19245916cd2Sjpk instr, tpstrp->template ? tpstrp->template : "NULL",
19345916cd2Sjpk tpstrp->attrs ? tpstrp->attrs : "NULL");
19445916cd2Sjpk #endif /* DEBUG */
19545916cd2Sjpk
19645916cd2Sjpk return (NSS_STR_PARSE_SUCCESS);
19745916cd2Sjpk }
198