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.
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.
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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * From "tsol_tndb_parser.c 7.24 01/09/05 SMI; TSOL 2.x"
27 * These functions parse entries in the "tnrhtp" (remote host template) file.
28 * Each entry in this file has two fields, separated by a colon. The first
29 * field is the template name. The second is a list of "key=value" attributes,
30 * separated by semicolons.
32 * In order to help preserve sanity, we do not allow more than one unescaped
33 * colon in a line, nor any unescaped '=' or ';' characters in the template
34 * name. Such things are indicative of typing errors, not intentional
45 #include <tsol/label.h>
46 #include <sys/tsol/label_macro.h>
47 #include <sys/types.h>
53 get_tn_doi(tsol_tpent_t
*tpentp
, kva_t
*kv
)
58 val
= kva_match(kv
, TP_DOI
);
60 return (LTSNET_NO_DOI
);
63 tpentp
->tp_doi
= strtol(val
, &cp
, 0);
65 return (LTSNET_SYSERR
);
67 return (LTSNET_ILL_DOI
);
73 get_tn_sl_range(brange_t
*range
, char *min
, char *max
)
77 if (min
== NULL
&& max
== NULL
)
78 return (LTSNET_NO_RANGE
);
80 return (LTSNET_NO_LOWERBOUND
);
82 return (LTSNET_NO_UPPERBOUND
);
84 slp
= &range
->lower_bound
;
85 if (str_to_label(min
, &slp
, MAC_LABEL
, L_NO_CORRECTION
, NULL
) != 0)
86 return (LTSNET_ILL_LOWERBOUND
);
87 slp
= &range
->upper_bound
;
88 if (str_to_label(max
, &slp
, MAC_LABEL
, L_NO_CORRECTION
, NULL
) != 0)
89 return (LTSNET_ILL_UPPERBOUND
);
90 if (!bldominates(&range
->upper_bound
, &range
->lower_bound
))
91 return (LTSNET_ILL_RANGE
);
97 get_tn_sl_set(blset_t
*labelset
, char *setstr
)
100 char *tokp
, *finally
;
101 m_label_t
*labels
, *slp
;
103 (void) memset(labelset
, 0, sizeof (blset_t
));
104 labels
= (m_label_t
*)labelset
;
105 tokp
= strtok_r(setstr
, TNDB_COMMA
, &finally
);
106 for (sc
= 0; tokp
!= NULL
&& sc
< NSLS_MAX
; sc
++) {
108 if (str_to_label(tokp
, &slp
, MAC_LABEL
, L_NO_CORRECTION
,
110 return (LTSNET_ILL_LABEL
);
111 tokp
= strtok_r(NULL
, TNDB_COMMA
, &finally
);
113 if (tokp
!= NULL
&& sc
>= NSLS_MAX
)
114 return (LTSNET_SET_TOO_BIG
);
120 parse_remainder(tsol_tpent_t
*tpentp
, kva_t
*kv
)
126 val
= kva_match(kv
, TP_HOSTTYPE
);
129 return (LTSNET_NO_HOSTTYPE
);
130 if (strcasecmp(val
, TP_UNLABELED
) == 0)
131 tpentp
->host_type
= UNLABELED
;
132 else if (strcasecmp(val
, TP_CIPSO
) == 0)
133 tpentp
->host_type
= SUN_CIPSO
;
135 return (LTSNET_ILL_HOSTTYPE
);
138 * parse fields by host type -
139 * add on to the following if statement for each new host type.
141 if (tpentp
->host_type
== UNLABELED
) {
144 tpentp
->tp_mask_unl
= 0;
148 if ((err
= get_tn_doi(tpentp
, kv
)) != 0)
150 tpentp
->tp_mask_unl
|= TSOL_MSK_CIPSO_DOI
;
154 val
= kva_match(kv
, TP_DEFLABEL
);
156 return (LTSNET_NO_LABEL
);
157 slp
= &tpentp
->tp_def_label
;
158 if (str_to_label(val
, &slp
, MAC_LABEL
, L_NO_CORRECTION
,
160 return (LTSNET_ILL_LABEL
);
161 tpentp
->tp_mask_unl
|= TSOL_MSK_DEF_LABEL
;
165 val
= kva_match(kv
, TP_MINLABEL
);
166 val2
= kva_match(kv
, TP_MAXLABEL
);
167 if (val
== NULL
&& val2
== NULL
) {
168 m_label_t
*llow
= NULL
;
170 * This is the old format. Use ADMIN_LOW to SL of the
171 * default label as the gw_sl_range.
173 if (str_to_label(ADMIN_LOW
, &llow
, MAC_LABEL
,
174 L_NO_CORRECTION
, NULL
) == -1)
175 return (LTSNET_ILL_LABEL
);
176 tpentp
->tp_gw_sl_range
.lower_bound
= *llow
;
178 tpentp
->tp_gw_sl_range
.upper_bound
=
179 tpentp
->tp_def_label
;
181 err
= get_tn_sl_range(&tpentp
->tp_gw_sl_range
, val
,
186 tpentp
->tp_mask_unl
|= TSOL_MSK_SL_RANGE_TSOL
;
189 * also label set, if present. (optional)
191 val
= kva_match(kv
, TP_SET
);
193 err
= get_tn_sl_set(&tpentp
->tp_gw_sl_set
, val
);
196 tpentp
->tp_mask_cipso
|= TSOL_MSK_SL_RANGE_TSOL
;
199 tpentp
->tp_mask_cipso
= 0;
203 if ((err
= get_tn_doi(tpentp
, kv
)) != 0)
205 tpentp
->tp_mask_cipso
|= TSOL_MSK_CIPSO_DOI
;
209 val
= kva_match(kv
, TP_MINLABEL
);
210 val2
= kva_match(kv
, TP_MAXLABEL
);
211 err
= get_tn_sl_range(&tpentp
->tp_sl_range_cipso
, val
, val2
);
214 tpentp
->tp_mask_cipso
|= TSOL_MSK_SL_RANGE_TSOL
;
216 * also label set, if present. (optional)
218 val
= kva_match(kv
, TP_SET
);
220 err
= get_tn_sl_set(&tpentp
->tp_sl_set_cipso
, val
);
223 tpentp
->tp_mask_cipso
|= TSOL_MSK_SL_RANGE_TSOL
;
226 /* CIPSO entries don't support default labels */
227 val
= kva_match(kv
, TP_DEFLABEL
);
229 return (LTSNET_BAD_TYPE
);
236 tpstr_to_ent(tsol_tpstr_t
*tpstrp
, int *errp
, char **errstrp
)
240 char *template = tpstrp
->template;
241 char *attrs
= tpstrp
->attrs
;
243 tsol_tpent_t
*tpentp
= NULL
;
246 * The user can specify NULL pointers for these. Make sure that we
247 * don't have to deal with checking for NULL everywhere by just
248 * pointing to our own variables if the user gives NULL.
254 /* The default, unless we find a more specific error locus. */
257 if (template == NULL
|| *template == '#' || *template == '\n') {
258 *errp
= LTSNET_EMPTY
;
259 if (attrs
&& *attrs
!= '\0' && *attrs
!= '#' && *attrs
!= '\n')
261 else if (template == NULL
)
265 if (*template == '\0') {
266 *errp
= LTSNET_NO_NAME
;
267 if (attrs
&& *attrs
!= '\0' && *attrs
!= '#' && *attrs
!= '\n')
271 if (attrs
== NULL
|| *attrs
== '\0' || *attrs
== '#' ||
273 *errp
= LTSNET_NO_ATTRS
;
276 if ((tpentp
= calloc(1, sizeof (*tpentp
))) == NULL
) {
277 *errp
= LTSNET_SYSERR
;
280 if ((strlcpy(tpentp
->name
, template, sizeof (tpentp
->name
)) >=
281 sizeof (tpentp
->name
)) ||
282 strpbrk(tpentp
->name
, TN_RESERVED
) != NULL
) {
283 *errp
= LTSNET_ILL_NAME
;
286 kv
= _str2kva(attrs
, KV_ASSIGN
, KV_DELIMITER
);
287 *errp
= parse_remainder(tpentp
, kv
);
291 (void) fprintf(stdout
, "tpstr_to_ent: %s:%s\n", tpentp
->name
,
300 tsol_freetpent(tpentp
);
303 (void) fprintf(stderr
, "\ntpstr_to_ent: %s:%s\n",
304 *errstrp
, (char *)tsol_strerror(*errp
, errno
));
311 tsol_freetpent(tsol_tpent_t
*tp
)