1 #include "context_internal.h"
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6
7 #define COMP_USER 0
8 #define COMP_ROLE 1
9 #define COMP_TYPE 2
10 #define COMP_RANGE 3
11
12 typedef struct {
13 char *current_str; /* This is made up-to-date only when needed */
14 char *(component[4]);
15 } context_private_t;
16
17 /*
18 * Allocate a new context, initialized from str. There must be 3 or
19 * 4 colon-separated components and no whitespace in any component other
20 * than the MLS component.
21 */
context_new(const char * str)22 context_t context_new(const char *str)
23 {
24 int i, count;
25 errno = 0;
26 context_private_t *n =
27 (context_private_t *) malloc(sizeof(context_private_t));
28 context_t result = (context_t) malloc(sizeof(context_s_t));
29 const char *p, *tok;
30
31 if (result)
32 result->ptr = n;
33 else
34 free(n);
35 if (n == 0 || result == 0) {
36 goto err;
37 }
38 n->current_str = n->component[0] = n->component[1] = n->component[2] =
39 n->component[3] = 0;
40 for (count = 0, p = str; *p; p++) {
41 switch (*p) {
42 case ':':
43 count++;
44 break;
45 case '\n':
46 case '\t':
47 case '\r':
48 goto err; /* sanity check */
49 case ' ':
50 if (count < 3)
51 goto err; /* sanity check */
52 }
53 }
54 /*
55 * Could be anywhere from 2 - 5
56 * e.g user:role:type to user:role:type:sens1:cata-sens2:catb
57 */
58 if (count < 2 || count > 5) { /* might not have a range */
59 goto err;
60 }
61
62 n->component[3] = 0;
63 for (i = 0, tok = str; *tok; i++) {
64 if (i < 3)
65 for (p = tok; *p && *p != ':'; p++) { /* empty */
66 } else {
67 /* MLS range is one component */
68 for (p = tok; *p; p++) { /* empty */
69 }
70 }
71 n->component[i] = (char *)malloc(p - tok + 1);
72 if (n->component[i] == 0)
73 goto err;
74 strncpy(n->component[i], tok, p - tok);
75 n->component[i][p - tok] = '\0';
76 tok = *p ? p + 1 : p;
77 }
78 return result;
79 err:
80 if (errno == 0) errno = EINVAL;
81 context_free(result);
82 return 0;
83 }
84
85
conditional_free(char ** v)86 static void conditional_free(char **v)
87 {
88 if (*v) {
89 free(*v);
90 }
91 *v = 0;
92 }
93
94 /*
95 * free all storage used by a context. Safe to call with
96 * null pointer.
97 */
context_free(context_t context)98 void context_free(context_t context)
99 {
100 context_private_t *n;
101 int i;
102 if (context) {
103 n = context->ptr;
104 if (n) {
105 conditional_free(&n->current_str);
106 for (i = 0; i < 4; i++) {
107 conditional_free(&n->component[i]);
108 }
109 free(n);
110 }
111 free(context);
112 }
113 }
114
115
116 /*
117 * Return a pointer to the string value of the context.
118 */
context_str(context_t context)119 char *context_str(context_t context)
120 {
121 context_private_t *n = context->ptr;
122 int i;
123 size_t total = 0;
124 conditional_free(&n->current_str);
125 for (i = 0; i < 4; i++) {
126 if (n->component[i]) {
127 total += strlen(n->component[i]) + 1;
128 }
129 }
130 n->current_str = malloc(total);
131 if (n->current_str != 0) {
132 char *cp = n->current_str;
133
134 cp = stpcpy(cp, n->component[0]);
135 for (i = 1; i < 4; i++) {
136 if (n->component[i]) {
137 *cp++ = ':';
138 cp = stpcpy(cp, n->component[i]);
139 }
140 }
141 }
142 return n->current_str;
143 }
144
145
146 /* Returns nonzero iff failed */
set_comp(context_private_t * n,int idx,const char * str)147 static int set_comp(context_private_t * n, int idx, const char *str)
148 {
149 char *t = NULL;
150 const char *p;
151 if (str) {
152 t = (char *)malloc(strlen(str) + 1);
153 if (!t) {
154 return -1;
155 }
156 for (p = str; *p; p++) {
157 if (*p == '\t' || *p == '\n' || *p == '\r' ||
158 ((*p == ':' || *p == ' ') && idx != COMP_RANGE)) {
159 free(t);
160 errno = EINVAL;
161 return -1;
162 }
163 }
164 strcpy(t, str);
165 }
166 conditional_free(&n->component[idx]);
167 n->component[idx] = t;
168 return 0;
169 }
170
171 #define def_get(name,tag) \
172 const char * context_ ## name ## _get(context_t context) \
173 { \
174 context_private_t *n = context->ptr; \
175 return n->component[tag]; \
176 }
177
178 def_get(type, COMP_TYPE)
179 def_get(user, COMP_USER)
180 def_get(range, COMP_RANGE)
181 def_get(role, COMP_ROLE)
182 #define def_set(name,tag) \
183 int context_ ## name ## _set(context_t context, const char* str) \
184 { \
185 return set_comp(context->ptr,tag,str);\
186 }
187 def_set(type, COMP_TYPE)
188 def_set(role, COMP_ROLE)
189 def_set(user, COMP_USER)
190 def_set(range, COMP_RANGE)
191