xref: /titanic_52/usr/src/lib/libtsnet/common/tsol_gettpent.c (revision 4a5d661a82b942b6538acd26209d959ce98b593a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * From "tsol_gettpent.c	7.13	00/10/13 SMI; TSOL 2.x"
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <nss_dbdefs.h>
33 #include <libtsnet.h>
34 #include <secdb.h>
35 #include <nss.h>
36 #include <libintl.h>
37 
38 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);	/* from lib.c */
39 
40 static int tsol_tp_stayopen;	/* Unsynchronized, but it affects only	*/
41 				/*   efficiency, not correctness	*/
42 static DEFINE_NSS_DB_ROOT(db_root);
43 static DEFINE_NSS_GETENT(context);
44 
45 
46 static void
47 _nss_initf_tsol_tp(nss_db_params_t *p)
48 {
49 	p->name	= NSS_DBNAM_TSOL_TP;
50 	p->default_config = NSS_DEFCONF_TSOL_TP;
51 }
52 
53 tsol_tpent_t *
54 tsol_gettpbyname(const char *name)
55 {
56 	int		err = 0;
57 	char		*errstr = NULL;
58 	char		buf[NSS_BUFLEN_TSOL_TP];
59 	tsol_tpstr_t	result;
60 	tsol_tpstr_t	*tpstrp = NULL;
61 	nss_XbyY_args_t arg;
62 
63 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
64 
65 	arg.key.name	= name;
66 	arg.stayopen	= tsol_tp_stayopen;
67 	arg.h_errno	= TSOL_NOT_FOUND;
68 	arg.status = nss_search(&db_root, _nss_initf_tsol_tp,
69 	    NSS_DBOP_TSOL_TP_BYNAME, &arg);
70 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
71 
72 #ifdef	DEBUG
73 	(void) fprintf(stdout, "tsol_gettpbyname %s: %s\n",
74 	    name, tpstrp ? tpstrp->template : "NULL");
75 #endif	/* DEBUG */
76 
77 	if (tpstrp == NULL)
78 		return (NULL);
79 
80 	return (tpstr_to_ent(tpstrp, &err, &errstr));
81 }
82 
83 void
84 tsol_settpent(int stay)
85 {
86 	tsol_tp_stayopen |= stay;
87 	nss_setent(&db_root, _nss_initf_tsol_tp, &context);
88 }
89 
90 void
91 tsol_endtpent(void)
92 {
93 	tsol_tp_stayopen = 0;
94 	nss_endent(&db_root, _nss_initf_tsol_tp, &context);
95 	nss_delete(&db_root);
96 }
97 
98 tsol_tpent_t *
99 tsol_gettpent(void)
100 {
101 	int		err = 0;
102 	char		*errstr = NULL;
103 	char		buf[NSS_BUFLEN_TSOL_TP];
104 	tsol_tpstr_t	result;
105 	tsol_tpstr_t	*tpstrp = NULL;
106 	nss_XbyY_args_t arg;
107 
108 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
109 	/* No key, no stayopen */
110 	arg.status = nss_getent(&db_root, _nss_initf_tsol_tp, &context, &arg);
111 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
112 
113 #ifdef	DEBUG
114 	(void) fprintf(stdout, "tsol_gettpent: %s\n",
115 	    tpstrp ? tpstrp->template : "NULL");
116 #endif	/* DEBUG */
117 
118 	if (tpstrp == NULL)
119 		return (NULL);
120 
121 	return (tpstr_to_ent(tpstrp, &err, &errstr));
122 }
123 
124 tsol_tpent_t *
125 tsol_fgettpent(FILE *f, boolean_t *error)
126 {
127 	int		err = 0;
128 	char		*errstr = NULL;
129 	char		buf[NSS_BUFLEN_TSOL_TP];
130 	tsol_tpstr_t	result;
131 	tsol_tpstr_t	*tpstrp = NULL;
132 	tsol_tpent_t	*tpentp = NULL;
133 	nss_XbyY_args_t	arg;
134 
135 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
136 	_nss_XbyY_fgets(f, &arg);
137 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
138 	if (tpstrp == NULL)
139 		return (NULL);
140 	tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
141 	while (tpentp == NULL) {
142 		/*
143 		 * Loop until we find a non-blank, non-comment line, or
144 		 * until EOF. No need to log blank lines, comments.
145 		 */
146 		if (err != LTSNET_EMPTY) {
147 			(void) fprintf(stderr, "%s: %.32s%s: %s\n",
148 			    gettext("Error parsing tnrhtp file"), errstr,
149 			    (strlen(errstr) > 32)? "...": "",
150 			    (char *)tsol_strerror(err, errno));
151 			*error = B_TRUE;
152 		}
153 		_nss_XbyY_fgets(f, &arg);
154 		tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
155 		if (tpstrp == NULL)	/* EOF */
156 			return (NULL);
157 		tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
158 	}
159 	return (tpentp);
160 }
161 
162 /*
163  * This is the callback routine for nss.  It just wraps the tsol_sgettpent
164  * parser.
165  */
166 int
167 str_to_tpstr(const char *instr, int lenstr, void *entp, char *buffer,
168     int buflen)
169 {
170 	int		len;
171 	char		*last = NULL;
172 	char		*sep = KV_TOKEN_DELIMIT;
173 	tsol_tpstr_t	*tpstrp = (tsol_tpstr_t *)entp;
174 
175 	if ((instr >= buffer && (buffer + buflen) > instr) ||
176 	    (buffer >= instr && (instr + lenstr) > buffer))
177 		return (NSS_STR_PARSE_PARSE);
178 	if (lenstr >= buflen)
179 		return (NSS_STR_PARSE_ERANGE);
180 	(void) strncpy(buffer, instr, buflen);
181 	tpstrp->template = _strtok_escape(buffer, sep, &last);
182 	tpstrp->attrs = _strtok_escape(NULL, sep, &last);
183 	if (tpstrp->attrs != NULL) {
184 		len = strlen(tpstrp->attrs);
185 		if (tpstrp->attrs[len - 1] == '\n')
186 			tpstrp->attrs[len - 1] = '\0';
187 	}
188 
189 #ifdef	DEBUG
190 	(void) fprintf(stdout,
191 	    "str_to_tpstr:\nstr - %s\n\ttemplate - %s\n\tattrs - %s\n",
192 	    instr, tpstrp->template ? tpstrp->template : "NULL",
193 	    tpstrp->attrs ? tpstrp->attrs : "NULL");
194 #endif	/* DEBUG */
195 
196 	return (NSS_STR_PARSE_SUCCESS);
197 }
198