xref: /illumos-gate/usr/src/lib/libtsnet/common/tsol_gettpent.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 #include <stdio.h>
29 #include <string.h>
30 #include <nss_dbdefs.h>
31 #include <libtsnet.h>
32 #include <secdb.h>
33 #include <nss.h>
34 #include <libintl.h>
35 
36 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);	/* from lib.c */
37 
38 static int tsol_tp_stayopen;	/* Unsynchronized, but it affects only	*/
39 				/*   efficiency, not correctness	*/
40 static DEFINE_NSS_DB_ROOT(db_root);
41 static DEFINE_NSS_GETENT(context);
42 
43 
44 static void
_nss_initf_tsol_tp(nss_db_params_t * p)45 _nss_initf_tsol_tp(nss_db_params_t *p)
46 {
47 	p->name	= NSS_DBNAM_TSOL_TP;
48 	p->default_config = NSS_DEFCONF_TSOL_TP;
49 }
50 
51 tsol_tpent_t *
tsol_gettpbyname(const char * name)52 tsol_gettpbyname(const char *name)
53 {
54 	int		err = 0;
55 	char		*errstr = NULL;
56 	char		buf[NSS_BUFLEN_TSOL_TP];
57 	tsol_tpstr_t	result;
58 	tsol_tpstr_t	*tpstrp = NULL;
59 	nss_XbyY_args_t arg;
60 
61 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
62 
63 	arg.key.name	= name;
64 	arg.stayopen	= tsol_tp_stayopen;
65 	arg.h_errno	= TSOL_NOT_FOUND;
66 	arg.status = nss_search(&db_root, _nss_initf_tsol_tp,
67 	    NSS_DBOP_TSOL_TP_BYNAME, &arg);
68 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
69 
70 #ifdef	DEBUG
71 	(void) fprintf(stdout, "tsol_gettpbyname %s: %s\n",
72 	    name, tpstrp ? tpstrp->template : "NULL");
73 #endif	/* DEBUG */
74 
75 	if (tpstrp == NULL)
76 		return (NULL);
77 
78 	return (tpstr_to_ent(tpstrp, &err, &errstr));
79 }
80 
81 void
tsol_settpent(int stay)82 tsol_settpent(int stay)
83 {
84 	tsol_tp_stayopen |= stay;
85 	nss_setent(&db_root, _nss_initf_tsol_tp, &context);
86 }
87 
88 void
tsol_endtpent(void)89 tsol_endtpent(void)
90 {
91 	tsol_tp_stayopen = 0;
92 	nss_endent(&db_root, _nss_initf_tsol_tp, &context);
93 	nss_delete(&db_root);
94 }
95 
96 tsol_tpent_t *
tsol_gettpent(void)97 tsol_gettpent(void)
98 {
99 	int		err = 0;
100 	char		*errstr = NULL;
101 	char		buf[NSS_BUFLEN_TSOL_TP];
102 	tsol_tpstr_t	result;
103 	tsol_tpstr_t	*tpstrp = NULL;
104 	nss_XbyY_args_t arg;
105 
106 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
107 	/* No key, no stayopen */
108 	arg.status = nss_getent(&db_root, _nss_initf_tsol_tp, &context, &arg);
109 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
110 
111 #ifdef	DEBUG
112 	(void) fprintf(stdout, "tsol_gettpent: %s\n",
113 	    tpstrp ? tpstrp->template : "NULL");
114 #endif	/* DEBUG */
115 
116 	if (tpstrp == NULL)
117 		return (NULL);
118 
119 	return (tpstr_to_ent(tpstrp, &err, &errstr));
120 }
121 
122 tsol_tpent_t *
tsol_fgettpent(FILE * f,boolean_t * error)123 tsol_fgettpent(FILE *f, boolean_t *error)
124 {
125 	int		err = 0;
126 	char		*errstr = NULL;
127 	char		buf[NSS_BUFLEN_TSOL_TP];
128 	tsol_tpstr_t	result;
129 	tsol_tpstr_t	*tpstrp = NULL;
130 	tsol_tpent_t	*tpentp = NULL;
131 	nss_XbyY_args_t	arg;
132 
133 	NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
134 	_nss_XbyY_fgets(f, &arg);
135 	tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
136 	if (tpstrp == NULL)
137 		return (NULL);
138 	tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
139 	while (tpentp == NULL) {
140 		/*
141 		 * Loop until we find a non-blank, non-comment line, or
142 		 * until EOF. No need to log blank lines, comments.
143 		 */
144 		if (err != LTSNET_EMPTY) {
145 			(void) fprintf(stderr, "%s: %.32s%s: %s\n",
146 			    gettext("Error parsing tnrhtp file"), errstr,
147 			    (strlen(errstr) > 32)? "...": "",
148 			    (char *)tsol_strerror(err, errno));
149 			*error = B_TRUE;
150 		}
151 		_nss_XbyY_fgets(f, &arg);
152 		tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
153 		if (tpstrp == NULL)	/* EOF */
154 			return (NULL);
155 		tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
156 	}
157 	return (tpentp);
158 }
159 
160 /*
161  * This is the callback routine for nss.  It just wraps the tsol_sgettpent
162  * parser.
163  */
164 int
str_to_tpstr(const char * instr,int lenstr,void * entp,char * buffer,int buflen)165 str_to_tpstr(const char *instr, int lenstr, void *entp, char *buffer,
166     int buflen)
167 {
168 	int		len;
169 	char		*last = NULL;
170 	char		*sep = KV_TOKEN_DELIMIT;
171 	tsol_tpstr_t	*tpstrp = (tsol_tpstr_t *)entp;
172 
173 	if ((instr >= buffer && (buffer + buflen) > instr) ||
174 	    (buffer >= instr && (instr + lenstr) > buffer))
175 		return (NSS_STR_PARSE_PARSE);
176 	if (lenstr >= buflen)
177 		return (NSS_STR_PARSE_ERANGE);
178 	(void) strncpy(buffer, instr, buflen);
179 	tpstrp->template = _strtok_escape(buffer, sep, &last);
180 	tpstrp->attrs = _strtok_escape(NULL, sep, &last);
181 	if (tpstrp->attrs != NULL) {
182 		len = strlen(tpstrp->attrs);
183 		if (tpstrp->attrs[len - 1] == '\n')
184 			tpstrp->attrs[len - 1] = '\0';
185 	}
186 
187 #ifdef	DEBUG
188 	(void) fprintf(stdout,
189 	    "str_to_tpstr:\nstr - %s\n\ttemplate - %s\n\tattrs - %s\n",
190 	    instr, tpstrp->template ? tpstrp->template : "NULL",
191 	    tpstrp->attrs ? tpstrp->attrs : "NULL");
192 #endif	/* DEBUG */
193 
194 	return (NSS_STR_PARSE_SUCCESS);
195 }
196