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 #include <stdio.h>
2945916cd2Sjpk #include <string.h>
3045916cd2Sjpk #include <nss_dbdefs.h>
3145916cd2Sjpk #include <libtsnet.h>
3245916cd2Sjpk #include <secdb.h>
3345916cd2Sjpk #include <nss.h>
3445916cd2Sjpk #include <libintl.h>
3545916cd2Sjpk 
3645916cd2Sjpk extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);	/* from lib.c */
3745916cd2Sjpk 
3845916cd2Sjpk static int tsol_tp_stayopen;	/* Unsynchronized, but it affects only	*/
3945916cd2Sjpk 				/*   efficiency, not correctness	*/
4045916cd2Sjpk static DEFINE_NSS_DB_ROOT(db_root);
4145916cd2Sjpk static DEFINE_NSS_GETENT(context);
4245916cd2Sjpk 
4345916cd2Sjpk 
4445916cd2Sjpk static void
_nss_initf_tsol_tp(nss_db_params_t * p)4545916cd2Sjpk _nss_initf_tsol_tp(nss_db_params_t *p)
4645916cd2Sjpk {
4745916cd2Sjpk 	p->name	= NSS_DBNAM_TSOL_TP;
4845916cd2Sjpk 	p->default_config = NSS_DEFCONF_TSOL_TP;
4945916cd2Sjpk }
5045916cd2Sjpk 
5145916cd2Sjpk tsol_tpent_t *
tsol_gettpbyname(const char * name)5245916cd2Sjpk tsol_gettpbyname(const char *name)
5345916cd2Sjpk {
5445916cd2Sjpk 	int		err = 0;
5545916cd2Sjpk 	char		*errstr = NULL;
5645916cd2Sjpk 	char		buf[NSS_BUFLEN_TSOL_TP];
5745916cd2Sjpk 	tsol_tpstr_t	result;
5845916cd2Sjpk 	tsol_tpstr_t	*tpstrp = NULL;
5945916cd2Sjpk 	nss_XbyY_args_t arg;
6045916cd2Sjpk 
6145916cd2Sjpk 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
6245916cd2Sjpk 
6345916cd2Sjpk 	arg.key.name	= name;
6445916cd2Sjpk 	arg.stayopen	= tsol_tp_stayopen;
6545916cd2Sjpk 	arg.h_errno	= TSOL_NOT_FOUND;
6645916cd2Sjpk 	arg.status = nss_search(&db_root, _nss_initf_tsol_tp,
6745916cd2Sjpk 	    NSS_DBOP_TSOL_TP_BYNAME, &arg);
6845916cd2Sjpk 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
6945916cd2Sjpk 
7045916cd2Sjpk #ifdef	DEBUG
7145916cd2Sjpk 	(void) fprintf(stdout, "tsol_gettpbyname %s: %s\n",
7245916cd2Sjpk 	    name, tpstrp ? tpstrp->template : "NULL");
7345916cd2Sjpk #endif	/* DEBUG */
7445916cd2Sjpk 
7545916cd2Sjpk 	if (tpstrp == NULL)
7645916cd2Sjpk 		return (NULL);
7745916cd2Sjpk 
7845916cd2Sjpk 	return (tpstr_to_ent(tpstrp, &err, &errstr));
7945916cd2Sjpk }
8045916cd2Sjpk 
8145916cd2Sjpk void
tsol_settpent(int stay)8245916cd2Sjpk tsol_settpent(int stay)
8345916cd2Sjpk {
8445916cd2Sjpk 	tsol_tp_stayopen |= stay;
8545916cd2Sjpk 	nss_setent(&db_root, _nss_initf_tsol_tp, &context);
8645916cd2Sjpk }
8745916cd2Sjpk 
8845916cd2Sjpk void
tsol_endtpent(void)8945916cd2Sjpk tsol_endtpent(void)
9045916cd2Sjpk {
9145916cd2Sjpk 	tsol_tp_stayopen = 0;
9245916cd2Sjpk 	nss_endent(&db_root, _nss_initf_tsol_tp, &context);
9345916cd2Sjpk 	nss_delete(&db_root);
9445916cd2Sjpk }
9545916cd2Sjpk 
9645916cd2Sjpk tsol_tpent_t *
tsol_gettpent(void)9745916cd2Sjpk tsol_gettpent(void)
9845916cd2Sjpk {
9945916cd2Sjpk 	int		err = 0;
10045916cd2Sjpk 	char		*errstr = NULL;
10145916cd2Sjpk 	char		buf[NSS_BUFLEN_TSOL_TP];
10245916cd2Sjpk 	tsol_tpstr_t	result;
10345916cd2Sjpk 	tsol_tpstr_t	*tpstrp = NULL;
10445916cd2Sjpk 	nss_XbyY_args_t arg;
10545916cd2Sjpk 
10645916cd2Sjpk 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
10745916cd2Sjpk 	/* No key, no stayopen */
10845916cd2Sjpk 	arg.status = nss_getent(&db_root, _nss_initf_tsol_tp, &context, &arg);
10945916cd2Sjpk 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
11045916cd2Sjpk 
11145916cd2Sjpk #ifdef	DEBUG
11245916cd2Sjpk 	(void) fprintf(stdout, "tsol_gettpent: %s\n",
11345916cd2Sjpk 	    tpstrp ? tpstrp->template : "NULL");
11445916cd2Sjpk #endif	/* DEBUG */
11545916cd2Sjpk 
11645916cd2Sjpk 	if (tpstrp == NULL)
11745916cd2Sjpk 		return (NULL);
11845916cd2Sjpk 
11945916cd2Sjpk 	return (tpstr_to_ent(tpstrp, &err, &errstr));
12045916cd2Sjpk }
12145916cd2Sjpk 
12245916cd2Sjpk tsol_tpent_t *
tsol_fgettpent(FILE * f,boolean_t * error)123*909c1a33Ston tsol_fgettpent(FILE *f, boolean_t *error)
12445916cd2Sjpk {
12545916cd2Sjpk 	int		err = 0;
12645916cd2Sjpk 	char		*errstr = NULL;
12745916cd2Sjpk 	char		buf[NSS_BUFLEN_TSOL_TP];
12845916cd2Sjpk 	tsol_tpstr_t	result;
12945916cd2Sjpk 	tsol_tpstr_t	*tpstrp = NULL;
13045916cd2Sjpk 	tsol_tpent_t	*tpentp = NULL;
13145916cd2Sjpk 	nss_XbyY_args_t	arg;
13245916cd2Sjpk 
13345916cd2Sjpk 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
13445916cd2Sjpk 	_nss_XbyY_fgets(f, &arg);
13545916cd2Sjpk 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
13645916cd2Sjpk 	if (tpstrp == NULL)
13745916cd2Sjpk 		return (NULL);
13845916cd2Sjpk 	tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
13945916cd2Sjpk 	while (tpentp == NULL) {
14045916cd2Sjpk 		/*
14145916cd2Sjpk 		 * Loop until we find a non-blank, non-comment line, or
14245916cd2Sjpk 		 * until EOF. No need to log blank lines, comments.
14345916cd2Sjpk 		 */
144*909c1a33Ston 		if (err != LTSNET_EMPTY) {
14545916cd2Sjpk 			(void) fprintf(stderr, "%s: %.32s%s: %s\n",
14645916cd2Sjpk 			    gettext("Error parsing tnrhtp file"), errstr,
14745916cd2Sjpk 			    (strlen(errstr) > 32)? "...": "",
14845916cd2Sjpk 			    (char *)tsol_strerror(err, errno));
149*909c1a33Ston 			*error = B_TRUE;
150*909c1a33Ston 		}
15145916cd2Sjpk 		_nss_XbyY_fgets(f, &arg);
15245916cd2Sjpk 		tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
15345916cd2Sjpk 		if (tpstrp == NULL)	/* EOF */
15445916cd2Sjpk 			return (NULL);
15545916cd2Sjpk 		tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
15645916cd2Sjpk 	}
15745916cd2Sjpk 	return (tpentp);
15845916cd2Sjpk }
15945916cd2Sjpk 
16045916cd2Sjpk /*
16145916cd2Sjpk  * This is the callback routine for nss.  It just wraps the tsol_sgettpent
16245916cd2Sjpk  * parser.
16345916cd2Sjpk  */
16445916cd2Sjpk int
str_to_tpstr(const char * instr,int lenstr,void * entp,char * buffer,int buflen)16545916cd2Sjpk str_to_tpstr(const char *instr, int lenstr, void *entp, char *buffer,
16645916cd2Sjpk     int buflen)
16745916cd2Sjpk {
16845916cd2Sjpk 	int		len;
16945916cd2Sjpk 	char		*last = NULL;
17045916cd2Sjpk 	char		*sep = KV_TOKEN_DELIMIT;
17145916cd2Sjpk 	tsol_tpstr_t	*tpstrp = (tsol_tpstr_t *)entp;
17245916cd2Sjpk 
17345916cd2Sjpk 	if ((instr >= buffer && (buffer + buflen) > instr) ||
17445916cd2Sjpk 	    (buffer >= instr && (instr + lenstr) > buffer))
17545916cd2Sjpk 		return (NSS_STR_PARSE_PARSE);
17645916cd2Sjpk 	if (lenstr >= buflen)
17745916cd2Sjpk 		return (NSS_STR_PARSE_ERANGE);
17845916cd2Sjpk 	(void) strncpy(buffer, instr, buflen);
17945916cd2Sjpk 	tpstrp->template = _strtok_escape(buffer, sep, &last);
18045916cd2Sjpk 	tpstrp->attrs = _strtok_escape(NULL, sep, &last);
18145916cd2Sjpk 	if (tpstrp->attrs != NULL) {
18245916cd2Sjpk 		len = strlen(tpstrp->attrs);
18345916cd2Sjpk 		if (tpstrp->attrs[len - 1] == '\n')
18445916cd2Sjpk 			tpstrp->attrs[len - 1] = '\0';
18545916cd2Sjpk 	}
18645916cd2Sjpk 
18745916cd2Sjpk #ifdef	DEBUG
18845916cd2Sjpk 	(void) fprintf(stdout,
18945916cd2Sjpk 	    "str_to_tpstr:\nstr - %s\n\ttemplate - %s\n\tattrs - %s\n",
19045916cd2Sjpk 	    instr, tpstrp->template ? tpstrp->template : "NULL",
19145916cd2Sjpk 	    tpstrp->attrs ? tpstrp->attrs : "NULL");
19245916cd2Sjpk #endif	/* DEBUG */
19345916cd2Sjpk 
19445916cd2Sjpk 	return (NSS_STR_PARSE_SUCCESS);
19545916cd2Sjpk }
196