aboutsummaryrefslogtreecommitdiff
path: root/mcstrans/src/mls_level.c
blob: 2017f117323084fdd2f934ad41a5efdc0b11eb7e (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
#include <stdio.h>
#include "mls_level.h"
#include <sepol/policydb/ebitmap.h>

mls_level_t *mls_level_from_string(char *mls_context)
{
	char delim;
	char *scontextp, *p, *lptr;
	mls_level_t *l;

	if (!mls_context) {
		return NULL;
	}

	l = (mls_level_t *) calloc(1, sizeof(mls_level_t));

	/* Extract low sensitivity. */
	scontextp = p = mls_context;
	while (*p && *p != ':' && *p != '-')
		p++;

	delim = *p;
	if (delim != 0)
		*p++ = 0;

	if (*scontextp != 's')
		goto err;
	l->sens = atoi(scontextp + 1);

	if (delim == ':') {
		/* Extract category set. */
		while (1) {
			scontextp = p;
			while (*p && *p != ',' && *p != '-')
				p++;
			delim = *p;
			if (delim != 0)
				*p++ = 0;

			/* Separate into level if exists */
			if ((lptr = strchr(scontextp, '.')) != NULL) {
				/* Remove '.' */
				*lptr++ = 0;
			}

			if (*scontextp != 'c')
				goto err;
			int bit = atoi(scontextp + 1);
			if (ebitmap_set_bit(&l->cat, bit, 1))
				goto err;

			/* If level, set all categories in level */
			if (lptr) {
				if (*lptr != 'c')
					goto err;
				int ubit = atoi(lptr + 1);
				int i;
				for (i = bit + 1; i <= ubit; i++) {
					if (ebitmap_set_bit
					    (&l->cat, i, 1))
						goto err;
				}
			}

			if (delim != ',')
				break;
		}
	}

	return l;

      err:
	free(l);
	return NULL;
}

/*
 * Return the length in bytes for the MLS fields of the
 * security context string representation of `context'.
 */
unsigned int mls_compute_string_len(mls_level_t *l)
{
	unsigned int len = 0;
	char temp[16];
	unsigned int i, level = 0;
	ebitmap_node_t *cnode;

	if (!l)
		return 0;

	len += snprintf(temp, sizeof(temp), "s%d", l->sens);

	ebitmap_for_each_bit(&l->cat, cnode, i) {
		if (ebitmap_node_get_bit(cnode, i)) {
			if (level) {
				level++;
				continue;
			}

			len++; /* : or ,` */

			len += snprintf(temp, sizeof(temp), "c%d", i);
			level++;
		} else {
			if (level > 1)
				len += snprintf(temp, sizeof(temp), ".c%d", i-1);
			level = 0;
		}
	}

	/* Handle case where last category is the end of level */
	if (level > 1)
		len += snprintf(temp, sizeof(temp), ".c%d", i-1);
	return len;
}

char *mls_level_to_string(mls_level_t *l)
{
	unsigned int wrote_sep, len = mls_compute_string_len(l);
	unsigned int i, level = 0;
	ebitmap_node_t *cnode;
	wrote_sep = 0;

	if (len == 0)
		return NULL;
	char *result = (char *)malloc(len + 1);
	char *p = result;

	p += sprintf(p, "s%d", l->sens);

	/* categories */
	ebitmap_for_each_bit(&l->cat, cnode, i) {
		if (ebitmap_node_get_bit(cnode, i)) {
			if (level) {
				level++;
				continue;
			}

			if (!wrote_sep) {
				*p++ = ':';
				wrote_sep = 1;
			} else
				*p++ = ',';
			p += sprintf(p, "c%d", i);
			level++;
		} else {
			if (level > 1) {
				if (level > 2)
					*p++ = '.';
				else
					*p++ = ',';

				p += sprintf(p, "c%d", i-1);
			}
			level = 0;
		}
	}
	/* Handle case where last category is the end of level */
	if (level > 1) {
		if (level > 2)
			*p++ = '.';
		else
			*p++ = ',';

		p += sprintf(p, "c%d", i-1);
	}

	*(result + len) = 0;
	return result;
}