summaryrefslogtreecommitdiff
path: root/usr/src/lib/libtsol/common/stob.c
blob: 43246c359d56107dd6e94ee883e7b93122a8b5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 *	String to binary label translations.
 */

#include <ctype.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#include <tsol/label.h>

#include "labeld.h"
#include <sys/tsol/label_macro.h>

#undef	CALL_SIZE
#define	CALL_SIZE(type, buf)	(size_t)(sizeof (type) - BUFSIZE + sizeof (int)\
	+ (buf))

#if	!defined(TEXT_DOMAIN)		/* should be defined by Makefiles */
#define	TEXT_DOMAIN "SYS_TEST"
#endif	/* TEXT_DOMAIN */

/* short hands */

#define	IS_ADMIN_LOW(sl) \
	((strncasecmp(sl, ADMIN_LOW, (sizeof (ADMIN_LOW) - 1)) == 0))

#define	IS_ADMIN_HIGH(sh) \
	((strncasecmp(sh, ADMIN_HIGH, (sizeof (ADMIN_HIGH) - 1)) == 0))

#define	ISHEX(f, s) \
	(((((f) & NEW_LABEL) == ((f) | NEW_LABEL)) || \
	(((f) & NO_CORRECTION) == ((f) | NO_CORRECTION))) && \
	(((s)[0] == '0') && (((s)[1] == 'x') || ((s)[1] == 'X'))))

#define	slcall callp->param.acall.cargs.stobsl_arg
#define	slret callp->param.aret.rvals.stobsl_ret
/*
 *	stobsl - Translate Sensitivity Label string to a Binary Sensitivity
 *		Label.
 *
 *	Entry	string = Sensitivity Label string to be translated.
 *		label = Address of Binary Sensitivity Label to be initialized or
 *			updated.
 *		flags = Flags to control translation:
 *			NO_CORRECTION implies NEW_LABEL.
 *			NEW_LABEL, Initialize the label to a valid empty
 *				Sensitivity Label structure.
 *			NO_CORRECTION, Initialize the label to a valid
 *				empty Sensitivity Label structure.
 *				Prohibit correction to the Sensitivity Label.
 *			Other, pass existing Sensitivity Label through for
 *				modification.
 *
 *	Exit	label = Translated (updated) Binary Sensitivity Label.
 *		error = If error reported, the error indicator,
 *				-1, Unable to access label encodings file;
 *				 0, Invalid binary label passed;
 *				>0, Position after the first character in
 *				    string of error, 1 indicates entire string.
 *			Otherwise, unchanged.
 *
 *	Returns	0, If error.
 *		1, If successful.
 *
 *	Calls	__call_labeld(STOBSL), ISHEX, htobsl, strlen,
 *			isspace,
 *			strncasecmp.
 *
 *	Uses	ADMIN_HIGH, ADMIN_LOW.
 */

int
stobsl(const char *string, bslabel_t *label, int flags, int *error)
{
	labeld_data_t	call;
	labeld_data_t	*callp = &call;
	size_t	bufsize = sizeof (labeld_data_t);
	size_t	datasize = CALL_SIZE(stobsl_call_t, strlen(string) + 1);
	int	rval;
	char	*s = (char *)string;

	while (isspace(*s))
		s++;
	/* accept a leading '[' */
	if (*s == '[') {
		s++;
		while (isspace(*s))
			s++;
	}
	if (ISHEX(flags, s)) {
		if (htobsl(s, label)) {
			return (1);
		} else {
			if (error != NULL)
				*error = 1;
			return (0);
		}
	}

	if (datasize > bufsize) {
		if ((callp = malloc(datasize)) == NULL) {
			if (error != NULL)
				*error = -1;
			return (0);
		}
		bufsize = datasize;
	}
	callp->callop = STOBSL;
	slcall.flags  = (flags&NEW_LABEL) ? LABELS_NEW_LABEL : 0;
	slcall.flags |= (flags&NO_CORRECTION) ? LABELS_FULL_PARSE : 0;
	slcall.label = *label;
	(void) strcpy(slcall.string, string);

	if ((rval = __call_labeld(&callp, &bufsize, &datasize)) == SUCCESS) {
		int err = callp->reterr;

		if (callp != &call) {
			/* free allocated buffer */
			free(callp);
		}
		/*
		 * reterr == 0, OK,
		 * reterr < 0, invalid binary label,
		 * reterr > 0 error position, 1 == whole string
		 */
		if (err == 0) {
			*label = slret.label;
			return (1);
		} else if (err < 0) {
			err = 0;
		}
		if (error != NULL)
			*error = err;
		return (0);
	} else if (rval == NOSERVER) {
		if (callp != &call) {
			/* free allocated buffer */
			free(callp);
		}
		/* server not present */
		/* special case Admin High and Admin Low */
		if (IS_ADMIN_LOW(s)) {
			BSLLOW(label);
		} else if (IS_ADMIN_HIGH(s)) {
			BSLHIGH(label);
		} else {
			goto err1;
		}
		return (1);
	}
	if (callp != &call) {
		/* free allocated buffer */
		free(callp);
	}
err1:
	if (error != NULL)
		*error = -1;
	return (0);
}  /* stobsl */
#undef	slcall
#undef	slret

#define	clrcall callp->param.acall.cargs.stobclear_arg
#define	clrret callp->param.aret.rvals.stobclear_ret
/*
 *	stobclear - Translate Clearance string to a Binary Clearance.
 *
 *	Entry	string = Clearance string to be translated.
 *		clearance = Address of Binary Clearance to be initialized or
 *			updated.
 *		flags = Flags to control translation:
 *			NO_CORRECTION implies NEW_LABEL.
 *			NEW_LABEL, Initialize the label to a valid empty
 *				Sensitivity Label structure.
 *			NO_CORRECTION, Initialize the label to a valid
 *				empty Sensitivity Label structure.
 *				Prohibit correction to the Sensitivity Label.
 *			Other, pass existing Sensitivity Label through for
 *				modification.
 *
 *	Exit	clearance = Translated (updated) Binary Clearance.
 *		error = If error reported, the error indicator,
 *				-1, Unable to access label encodings file;
 *				 0, Invalid binary label passed;
 *				>0, Position after the first character in
 *				    string of error, 1 indicates entire string.
 *			Otherwise, unchanged.
 *
 *	Returns	0, If error.
 *		1, If successful.
 *
 *	Calls	__call_labeld(STOBCLEAR), ISHEX, htobsl, strlen,
 *			isspace,
 *			strncasecmp.
 *
 *	Uses	ADMIN_HIGH, ADMIN_LOW.
 */

int
stobclear(const char *string, bclear_t *clearance, int flags, int *error)
{
	labeld_data_t	call;
	labeld_data_t	*callp = &call;
	size_t	bufsize = sizeof (labeld_data_t);
	size_t	datasize = CALL_SIZE(stobclear_call_t, strlen(string) + 1);
	int	rval;

	if (ISHEX(flags, string)) {
		if (htobclear(string, clearance)) {
			return (1);
		} else {
			if (error != NULL)
				*error = 1;
			return (0);
		}
	}

	if (datasize > bufsize) {
		if ((callp = malloc(datasize)) == NULL) {
			if (error != NULL)
				*error = -1;
			return (0);
		}
		bufsize = datasize;
	}
	callp->callop = STOBCLEAR;
	clrcall.flags  = (flags&NEW_LABEL) ? LABELS_NEW_LABEL : 0;
	clrcall.flags |= (flags&NO_CORRECTION) ? LABELS_FULL_PARSE : 0;
	clrcall.clear = *clearance;
	(void) strcpy(clrcall.string, string);

	if ((rval = __call_labeld(&callp, &bufsize, &datasize)) == SUCCESS) {
		int err = callp->reterr;

		if (callp != &call) {
			/* free allocated buffer */
			free(callp);
		}
		/*
		 * reterr == 0, OK,
		 * reterr < 0, invalid binary label,
		 * reterr > 0 error position, 1 == whole string
		 */
		if (err == 0) {
			*clearance = clrret.clear;
			return (1);
		} else if (err < 0) {
			err = 0;
		}
		if (error != NULL)
			*error = err;
		return (0);
	} else if (rval == NOSERVER) {
		char *s = (char *)string;

		if (callp != &call) {
			/* free allocated buffer */
			free(callp);
		}
		/* server not present */
		/* special case Admin High and Admin Low */
		while (isspace(*s))
			s++;
		if (IS_ADMIN_LOW(s)) {
			BCLEARLOW(clearance);
		} else if (IS_ADMIN_HIGH(s)) {
			BCLEARHIGH(clearance);
		} else {
			goto err1;
		}
		return (1);
	}
	if (callp != &call) {
		/* free allocated buffer */
		free(callp);
	}
err1:
	if (error != NULL)
		*error = -1;
	return (0);
}  /* stobclear */
#undef	clrcall
#undef	clrret