1
2 /* Copyright (c) 2008-2009 Nall Design Works
3 Copyright 2006 Trusted Computer Solutions, Inc. */
4
5 /*
6 Exported Interface
7
8 int init_translations(void);
9 void finish_context_translations(void);
10 int trans_context(const char *, char **);
11 int untrans_context(const char *, char **);
12
13 */
14
15 #include <math.h>
16 #include <glob.h>
17 #include <values.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdio_ext.h>
24 #include <ctype.h>
25 #include <selinux/selinux.h>
26 #include <selinux/context.h>
27 #include <syslog.h>
28 #include <errno.h>
29 #include <pcre2.h>
30 #include <ctype.h>
31 #include <time.h>
32 #include <sys/time.h>
33
34
35 #include "mls_level.h"
36 #include "mcstrans.h"
37
38 #define N_BUCKETS 1453
39
40 #define log_error(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
41
42 #ifdef DEBUG
43 #define log_debug(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
44 #else
45 #define log_debug(fmt, ...) do {} while (0)
46 #endif
47
48 static unsigned int maxbit=0;
49
50 /* Define data structures */
51 typedef struct context_map {
52 char *raw;
53 char *trans;
54 } context_map_t;
55
56 typedef struct context_map_node {
57 char *key;
58 context_map_t *map;
59 struct context_map_node *next;
60 } context_map_node_t;
61
62 typedef struct affix {
63 char *text;
64 struct affix *next;
65 } affix_t;
66
67 typedef struct word {
68 char *text;
69 ebitmap_t cat;
70 ebitmap_t normal;
71 ebitmap_t inverse;
72 struct word *next;
73 } word_t;
74
75 typedef struct word_group {
76 char *name;
77 char *whitespace;
78 char *join;
79
80 affix_t *prefixes;
81 affix_t *suffixes;
82 word_t *words;
83
84 pcre2_code *prefix_regexp;
85 pcre2_code *word_regexp;
86 pcre2_code *suffix_regexp;
87
88 ebitmap_t def;
89
90 word_t **sword;
91 unsigned int sword_len;
92
93 struct word_group *next;
94 } word_group_t;
95
96 typedef struct base_classification {
97 char *trans;
98 mls_level_t *level;
99 struct base_classification *next;
100 } base_classification_t;
101
102 typedef struct domain {
103 char *name;
104
105 context_map_node_t *raw_to_trans[N_BUCKETS];
106 context_map_node_t *trans_to_raw[N_BUCKETS];
107
108 base_classification_t *base_classifications;
109 word_group_t *groups;
110
111 pcre2_code *base_classification_regexp;
112 struct domain *next;
113 } domain_t;
114
115 static domain_t *domains;
116
117 typedef struct sens_constraint {
118 char op;
119 char *text;
120 unsigned int sens;
121 ebitmap_t cat;
122 struct sens_constraint *next;
123 } sens_constraint_t;
124
125 static sens_constraint_t *sens_constraints;
126
127 typedef struct cat_constraint {
128 char op;
129 char *text;
130 int nbits;
131 ebitmap_t mask;
132 ebitmap_t cat;
133 struct cat_constraint *next;
134 } cat_constraint_t;
135
136 static cat_constraint_t *cat_constraints;
137
138 static unsigned int
hash(const char * str)139 hash(const char *str) {
140 unsigned int hash = 5381;
141 int c;
142
143 while ((c = *(unsigned const char *)str++))
144 hash = ((hash << 5) + hash) + c;
145
146 return hash;
147 }
148
149 static int
add_to_hashtable(context_map_node_t ** table,char * key,context_map_t * map)150 add_to_hashtable(context_map_node_t **table, char *key, context_map_t *map) {
151 unsigned int bucket = hash(key) % N_BUCKETS;
152 context_map_node_t **n;
153 for (n = &table[bucket]; *n; n = &(*n)->next)
154 ;
155 *n = malloc(sizeof(context_map_node_t));
156 if (! *n)
157 goto err;
158 (*n)->key = key;
159 (*n)->map = map;
160 (*n)->next = NULL;
161 return 0;
162 err:
163 syslog(LOG_ERR, "add_to_hashtable: allocation error");
164 return -1;
165 }
166
167 static int
numdigits(unsigned int n)168 numdigits(unsigned int n)
169 {
170 int count = 1;
171 while ((n = n / 10))
172 count++;
173 return count;
174 }
175
176 static int
parse_category(ebitmap_t * e,const char * raw,int allowinverse)177 parse_category(ebitmap_t *e, const char *raw, int allowinverse)
178 {
179 int inverse = 0;
180 unsigned int low, high;
181 while (*raw) {
182 if (allowinverse && *raw == '~') {
183 inverse = !inverse;
184 raw++;
185 continue;
186 }
187 if (sscanf(raw,"c%u", &low) != 1)
188 return -1;
189 raw += numdigits(low) + 1;
190 if (*raw == '.') {
191 raw++;
192 if (sscanf(raw,"c%u", &high) != 1)
193 return -1;
194 raw += numdigits(high) + 1;
195 } else {
196 high = low;
197 }
198 while (low <= high) {
199 if (low >= maxbit)
200 maxbit = low + 1;
201 if (ebitmap_set_bit(e, low, inverse ? 0 : 1) < 0)
202 return -1;
203 low++;
204 }
205 if (*raw == ',') {
206 raw++;
207 inverse = 0;
208 } else if (*raw != '\0') {
209 return -1;
210 }
211 }
212 return 0;
213 }
214
215 static int
parse_ebitmap(ebitmap_t * e,ebitmap_t * def,const char * raw)216 parse_ebitmap(ebitmap_t *e, ebitmap_t *def, const char *raw) {
217 int rc = ebitmap_cpy(e, def);
218 if (rc < 0)
219 return rc;
220 rc = parse_category(e, raw, 1);
221 if (rc < 0)
222 return rc;
223 return 0;
224 }
225
226 static mls_level_t *
parse_raw(const char * raw)227 parse_raw(const char *raw) {
228 mls_level_t *mls = calloc(1, sizeof(mls_level_t));
229 if (!mls)
230 goto err;
231 if (sscanf(raw,"s%u", &mls->sens) != 1)
232 goto err;
233 raw += numdigits(mls->sens) + 1;
234 if (*raw == ':') {
235 raw++;
236 if (parse_category(&mls->cat, raw, 0) < 0)
237 goto err;
238 } else if (*raw != '\0') {
239 goto err;
240 }
241
242 return mls;
243
244 err:
245 ebitmap_destroy(&mls->cat);
246 free(mls);
247 return NULL;
248 }
249
250 static void
destroy_word(word_t ** list,word_t * word)251 destroy_word(word_t **list, word_t *word) {
252 if (!word) {
253 return;
254 }
255 for (; list && *list; list = &(*list)->next) {
256 if (*list == word) {
257 *list = word->next;
258 break;
259 }
260 }
261 free(word->text);
262 ebitmap_destroy(&word->cat);
263 ebitmap_destroy(&word->normal);
264 ebitmap_destroy(&word->inverse);
265 memset(word, 0, sizeof(word_t));
266 free(word);
267 }
268
269 static word_t *
create_word(word_t ** list,const char * text)270 create_word(word_t **list, const char *text) {
271 word_t *w = calloc(1, sizeof(word_t));
272 if (!w) {
273 goto err;
274 }
275 w->text = strdup(text);
276 if (!w->text) {
277 goto err;
278 }
279 if (list) {
280 for (; *list; list = &(*list)->next)
281 ;
282 *list = w;
283 }
284
285 return w;
286
287 err:
288 log_error("create_word: allocation error %s", strerror(errno));
289 destroy_word(NULL, w);
290 return NULL;
291 }
292
293 static void
destroy_group(word_group_t ** list,word_group_t * group)294 destroy_group(word_group_t **list, word_group_t *group) {
295 for (; list && *list; list = &(*list)->next) {
296 if (*list == group) {
297 *list = group->next;
298 break;
299 }
300 }
301 while(group->prefixes) {
302 affix_t *next = group->prefixes->next;
303 free(group->prefixes->text);
304 free(group->prefixes);
305 group->prefixes=next;
306 }
307 while(group->suffixes) {
308 affix_t *next = group->suffixes->next;
309 free(group->suffixes->text);
310 free(group->suffixes);
311 group->suffixes=next;
312 }
313 while(group->words)
314 destroy_word(&group->words, group->words);
315 free(group->whitespace);
316 free(group->name);
317 free(group->sword);
318 free(group->join);
319 pcre2_code_free(group->prefix_regexp);
320 pcre2_code_free(group->word_regexp);
321 pcre2_code_free(group->suffix_regexp);
322 ebitmap_destroy(&group->def);
323 free(group);
324 }
325
326 static word_group_t *
create_group(word_group_t ** list,const char * name)327 create_group(word_group_t **list, const char *name) {
328 word_group_t *group = calloc(1, sizeof(word_group_t));
329 if (!group)
330 return NULL;
331 group->name = strdup(name);
332 if (!group->name) {
333 goto err;
334 }
335 group->join = strdup(" ");
336 if (!group->join) {
337 goto err;
338 }
339 group->whitespace = strdup(" ");
340 if (!group->whitespace) {
341 goto err;
342 }
343 group->sword = NULL;
344
345 if (list) {
346 for (; *list; list = &(*list)->next)
347 ;
348 *list = group;
349 }
350
351 return group;
352
353 err:
354 log_error("allocation error %s", strerror(errno));
355 destroy_group(NULL, group);
356 return NULL;
357 }
358
359 static void
destroy_domain(domain_t * domain)360 destroy_domain(domain_t *domain) {
361 int i;
362 unsigned int rt = 0, tr = 0;
363 for (i=0; i < N_BUCKETS; i++) {
364 context_map_node_t *ptr;
365 for (ptr = domain->trans_to_raw[i]; ptr;) {
366 context_map_node_t *t = ptr->next;
367 free(ptr);
368 ptr = t;
369 tr++;
370 }
371 domain->trans_to_raw[i] = NULL;
372 }
373 for (i=0; i < N_BUCKETS; i++) {
374 context_map_node_t *ptr;
375 for (ptr = domain->raw_to_trans[i]; ptr;) {
376 context_map_node_t *t = ptr->next;
377 free(ptr->map->raw);
378 free(ptr->map->trans);
379 free(ptr->map);
380 free(ptr);
381 ptr = t;
382 rt++;
383 }
384 domain->raw_to_trans[i] = NULL;
385 }
386 while (domain->base_classifications) {
387 base_classification_t *next = domain->base_classifications->next;
388 free(domain->base_classifications->trans);
389 ebitmap_destroy(&domain->base_classifications->level->cat);
390 free(domain->base_classifications->level);
391 free(domain->base_classifications);
392 domain->base_classifications = next;
393 }
394 pcre2_code_free(domain->base_classification_regexp);
395 while (domain->groups)
396 destroy_group(&domain->groups, domain->groups);
397 free(domain->name);
398 free(domain);
399
400 syslog(LOG_INFO, "cache sizes: tr = %u, rt = %u", tr, rt);
401 }
402
403 static domain_t *
create_domain(const char * name)404 create_domain(const char *name) {
405 domain_t *domain = calloc(1, sizeof(domain_t));
406 if (!domain) {
407 goto err;
408 }
409 domain->name = strdup(name);
410 if (!domain->name) {
411 goto err;
412 }
413
414 domain_t **d = &domains;
415 for (; *d; d = &(*d)->next)
416 ;
417 *d = domain;
418
419 return domain;
420
421 err:
422 log_error("allocation error %s", strerror(errno));
423 destroy_domain(domain);
424 return NULL;
425 }
426
427 static int
add_word(word_group_t * group,char * raw,char * trans)428 add_word(word_group_t *group, char *raw, char *trans) {
429 if (strchr(trans,'-')) {
430 log_error("'%s'is invalid because '-' is illegal in modifiers.\n", trans);
431 return -1;
432 }
433 word_t *word = create_word(&group->words, trans);
434 int rc = parse_ebitmap(&word->cat, &group->def, raw);
435 if (rc < 0) {
436 log_error(" syntax error in %s\n", raw);
437 destroy_word(&group->words, word);
438 return -1;
439 }
440 if (ebitmap_andnot(&word->normal, &word->cat, &group->def, maxbit) < 0)
441 return -1;
442
443 ebitmap_t temp;
444 if (ebitmap_xor(&temp, &word->cat, &group->def) < 0)
445 return -1;
446 if (ebitmap_and(&word->inverse, &temp, &group->def) < 0)
447 return -1;
448 ebitmap_destroy(&temp);
449
450 return 0;
451 }
452
453 static int
add_constraint(char op,char * raw,char * tok)454 add_constraint(char op, char *raw, char *tok) {
455 log_debug("%s\n", "add_constraint");
456 ebitmap_t empty;
457 ebitmap_init(&empty);
458 if (!raw || !*raw) {
459 syslog(LOG_ERR, "unable to parse line");
460 return -1;
461 }
462 if (*raw == 's') {
463 sens_constraint_t *constraint = calloc(1, sizeof(sens_constraint_t));
464 if (!constraint) {
465 log_error("allocation error %s", strerror(errno));
466 return -1;
467 }
468 if (sscanf(raw,"s%u", &constraint->sens) != 1) {
469 syslog(LOG_ERR, "unable to parse level");
470 free(constraint);
471 return -1;
472 }
473 if (parse_ebitmap(&constraint->cat, &empty, tok) < 0) {
474 syslog(LOG_ERR, "unable to parse cat");
475 free(constraint);
476 return -1;
477 }
478 if (asprintf(&constraint->text, "%s%c%s", raw, op, tok) < 0) {
479 log_error("asprintf failed %s", strerror(errno));
480 return -1;
481 }
482 constraint->op = op;
483 sens_constraint_t **p;
484 for (p= &sens_constraints; *p; p = &(*p)->next)
485 ;
486 *p = constraint;
487 return 0;
488 } else if (*raw == 'c' ) {
489 cat_constraint_t *constraint = calloc(1, sizeof(cat_constraint_t));
490 if (!constraint) {
491 log_error("allocation error %s", strerror(errno));
492 return -1;
493 }
494 if (parse_ebitmap(&constraint->mask, &empty, raw) < 0) {
495 syslog(LOG_ERR, "unable to parse mask");
496 free(constraint);
497 return -1;
498 }
499 if (parse_ebitmap(&constraint->cat, &empty, tok) < 0) {
500 syslog(LOG_ERR, "unable to parse cat");
501 ebitmap_destroy(&constraint->mask);
502 free(constraint);
503 return -1;
504 }
505 if (asprintf(&constraint->text, "%s%c%s", raw, op, tok) < 0) {
506 log_error("asprintf failed %s", strerror(errno));
507 return -1;
508 }
509 constraint->nbits = ebitmap_cardinality(&constraint->cat);
510 constraint->op = op;
511 cat_constraint_t **p;
512 for (p= &cat_constraints; *p; p = &(*p)->next)
513 ;
514 *p = constraint;
515 return 0;
516 } else {
517 return -1;
518 }
519
520 return 0;
521 }
522
523 static int
violates_constraints(mls_level_t * l)524 violates_constraints(mls_level_t *l) {
525 int nbits;
526 sens_constraint_t *s;
527 ebitmap_t common;
528 for (s=sens_constraints; s; s=s->next) {
529 if (s->sens == l->sens) {
530 if (ebitmap_and(&common, &s->cat, &l->cat) < 0)
531 return 1;
532 nbits = ebitmap_cardinality(&common);
533 ebitmap_destroy(&common);
534 if (nbits) {
535 char *text = mls_level_to_string(l);
536 syslog(LOG_WARNING, "%s violates %s", text, s->text);
537 free(text);
538 return 1;
539 }
540 }
541 }
542 cat_constraint_t *c;
543 for (c=cat_constraints; c; c=c->next) {
544 if (ebitmap_and(&common, &c->mask, &l->cat) < 0)
545 return 1;
546 nbits = ebitmap_cardinality(&common);
547 ebitmap_destroy(&common);
548 if (nbits > 0) {
549 if (ebitmap_and(&common, &c->cat, &l->cat) < 0)
550 return 1;
551 nbits = ebitmap_cardinality(&common);
552 ebitmap_destroy(&common);
553 if ((c->op == '!' && nbits) ||
554 (c->op == '>' && nbits != c->nbits)) {
555 char *text = mls_level_to_string(l);
556 syslog(LOG_WARNING, "%s violates %s (%d,%d)", text, c->text, nbits, c->nbits);
557 free(text);
558 return 1;
559 }
560 }
561 }
562 return 0;
563 }
564
565 static void
destroy_sens_constraint(sens_constraint_t ** list,sens_constraint_t * constraint)566 destroy_sens_constraint(sens_constraint_t **list, sens_constraint_t *constraint) {
567 if (!constraint) {
568 return;
569 }
570 for (; list && *list; list = &(*list)->next) {
571 if (*list == constraint) {
572 *list = constraint->next;
573 break;
574 }
575 }
576 ebitmap_destroy(&constraint->cat);
577 free(constraint->text);
578 memset(constraint, 0, sizeof(sens_constraint_t));
579 free(constraint);
580 }
581
582 static void
destroy_cat_constraint(cat_constraint_t ** list,cat_constraint_t * constraint)583 destroy_cat_constraint(cat_constraint_t **list, cat_constraint_t *constraint) {
584 if (!constraint) {
585 return;
586 }
587 for (; list && *list; list = &(*list)->next) {
588 if (*list == constraint) {
589 *list = constraint->next;
590 break;
591 }
592 }
593 ebitmap_destroy(&constraint->mask);
594 ebitmap_destroy(&constraint->cat);
595 free(constraint->text);
596 memset(constraint, 0, sizeof(cat_constraint_t));
597 free(constraint);
598 }
599
600
601 static int
add_base_classification(domain_t * domain,char * raw,char * trans)602 add_base_classification(domain_t *domain, char *raw, char *trans) {
603 mls_level_t *level = parse_raw(raw);
604 if (level) {
605 base_classification_t **i;
606 base_classification_t *base_classification = calloc(1, sizeof(base_classification_t));
607 if (!base_classification) {
608 log_error("allocation error %s", strerror(errno));
609 return -1;
610 }
611 base_classification->trans=strdup(trans);
612 if (!base_classification->trans) {
613 log_error("allocation error %s", strerror(errno));
614 free(base_classification);
615 return -1;
616 }
617 base_classification->level=level;
618
619 for (i=&domain->base_classifications; *i; i=&(*i)->next)
620 ;
621 *i = base_classification;
622 return 0;
623 }
624 log_error(" add_base_classification error %s %s\n", raw, trans);
625 return -1;
626 }
627
628 static int
add_cache(domain_t * domain,char * raw,char * trans)629 add_cache(domain_t *domain, char *raw, char *trans) {
630 context_map_t *map = calloc(1, sizeof(context_map_t));
631 if (!map) goto err;
632
633 map->raw = strdup(raw);
634 if (!map->raw) {
635 goto err;
636 }
637 map->trans = strdup(trans);
638 if (!map->trans) {
639 goto err;
640 }
641
642 log_debug(" add_cache (%s,%s)\n", raw, trans);
643 if (add_to_hashtable(domain->raw_to_trans, map->raw, map) < 0)
644 goto err;
645
646 if (add_to_hashtable(domain->trans_to_raw, map->trans, map) < 0)
647 goto err;
648
649 return 0;
650 err:
651 log_error("%s: allocation error", "add_cache");
652 return -1;
653 }
654
655 static context_map_t *
find_in_table(context_map_node_t ** table,const char * key)656 find_in_table(context_map_node_t **table, const char *key) {
657 unsigned int bucket = hash(key) % N_BUCKETS;
658 context_map_node_t **n;
659 for (n = &table[bucket]; *n; n = &(*n)->next)
660 if (!strcmp((*n)->key, key))
661 return (*n)->map;
662 return NULL;
663 }
664
665 static char *
trim(char * str,const char * whitespace)666 trim(char *str, const char *whitespace) {
667 char *p = str + strlen(str);
668
669 while (p > str && strchr(whitespace, *(p-1)) != NULL)
670 *--p = 0;
671 return str;
672 }
673
674 static char *
triml(char * str,const char * whitespace)675 triml(char *str, const char *whitespace) {
676 char *p = str;
677
678 while (*p && (strchr(whitespace, *p) != NULL))
679 p++;
680 return p;
681 }
682
683 static int
update(char ** p,char * const val)684 update(char **p, char *const val) {
685 free (*p);
686 *p = strdup(val);
687 if (!*p) {
688 log_error("allocation error %s", strerror(errno));
689 return -1;
690 }
691 return 0;
692 }
693
694 static int
append(affix_t ** affixes,const char * val)695 append(affix_t **affixes, const char *val) {
696 affix_t *affix = calloc(1, sizeof(affix_t));
697 if (!affix) {
698 goto err;
699 }
700 affix->text = strdup(val);
701 if (!affix->text)
702 goto err;
703 for (;*affixes; affixes = &(*affixes)->next)
704 ;
705 *affixes = affix;
706 return 0;
707
708 err:
709 log_error("allocation error %s", strerror(errno));
710 free(affix);
711 return -1;
712 }
713
714 static int read_translations(const char *filename);
715
716 /* Process line from translation file.
717 Remove white space and set raw do data before the "=" and tok to data after it
718 Modifies the data pointed to by the buffer parameter
719 */
720 static int
process_trans(char * buffer)721 process_trans(char *buffer) {
722 static domain_t *domain;
723 static word_group_t *group;
724 static int base_classification;
725 static int lineno = 0;
726 char op='\0';
727
728 lineno++;
729 log_debug("%d: %s", lineno, buffer);
730
731 /* zap leading whitespace */
732 buffer = triml(buffer, "\t ");
733
734 /* Ignore comments */
735 if (*buffer == '#') return 0;
736 char *comment = strpbrk (buffer, "#");
737 if (comment) {
738 *comment = '\0';
739 }
740
741 /* zap trailing whitespace */
742 buffer = trim(buffer, "\t \r\n");
743
744 if (*buffer == 0) return 0;
745
746 char *delim = strpbrk (buffer, "=!>");
747 if (! delim) {
748 syslog(LOG_ERR, "invalid line (no !, = or >) %d", lineno);
749 return -1;
750 }
751
752 op = *delim;
753 *delim = '\0';
754 char *raw = buffer;
755 char *tok = delim+1;
756
757 /* remove trailing/leading whitespace from the split tokens */
758 trim(raw, "\t ");
759 tok = triml(tok, "\t ");
760
761 if (! *raw) {
762 syslog(LOG_ERR, "invalid line %d", lineno);
763 return -1;
764 }
765
766 if (! *tok) {
767 syslog(LOG_ERR, "invalid line %d", lineno);
768 return -1;
769 }
770
771 /* constraints have different syntax */
772 if (op == '!' || op == '>') {
773 return add_constraint(op, raw, tok);
774 }
775
776 if (!strcmp(raw, "Domain")) {
777 domain = create_domain(tok);
778 group = NULL;
779 return 0;
780 }
781
782 if (!domain) {
783 domain = create_domain("Default");
784 if (!domain)
785 return -1;
786 group = NULL;
787 }
788
789 if (!group &&
790 (!strcmp(raw, "Whitespace") || !strcmp(raw, "Join") ||
791 !strcmp(raw, "Prefix") || !strcmp(raw, "Suffix") ||
792 !strcmp(raw, "Default"))) {
793 syslog(LOG_ERR, "expected ModifierGroup declaration on line %d", lineno);
794 return -1;
795 }
796
797 if (!strcmp(raw, "Include")) {
798 unsigned int n;
799 glob_t g;
800 g.gl_offs = 0;
801 if (glob(tok, GLOB_ERR, NULL, &g) < 0) {
802 globfree(&g);
803 return -1;
804 }
805 for (n=0; n < g.gl_pathc; n++) {
806 if (read_translations(g.gl_pathv[n]) < 0) {
807 globfree(&g);
808 return -1;
809 }
810 }
811 globfree(&g);
812 } else if (!strcmp(raw, "Base")) {
813 base_classification = 1;
814 } else if (!strcmp(raw, "ModifierGroup")) {
815 group = create_group(&domain->groups, tok);
816 if (!group)
817 return -1;
818 base_classification = 0;
819 } else if (!strcmp(raw, "Whitespace")) {
820 if (update (&group->whitespace, tok) < 0)
821 return -1;
822 } else if (!strcmp(raw, "Join")) {
823 if (update (&group->join, tok) < 0)
824 return -1;
825 } else if (!strcmp(raw, "Prefix")) {
826 if (append (&group->prefixes, tok) < 0)
827 return -1;
828 } else if (!strcmp(raw, "Suffix")) {
829 if (append (&group->suffixes, tok) < 0)
830 return -1;
831 } else if (!strcmp(raw, "Default")) {
832 ebitmap_t empty;
833 ebitmap_init(&empty);
834 if (parse_ebitmap(&group->def, &empty, tok) < 0) {
835 syslog(LOG_ERR, "unable to parse Default %d", lineno);
836 return -1;
837 }
838 } else if (group) {
839 if (add_word(group, raw, tok) < 0) {
840 syslog(LOG_ERR, "unable to add base_classification on line %d", lineno);
841 return -1;
842 }
843 } else {
844 if (base_classification) {
845 if (add_base_classification(domain, raw, tok) < 0) {
846 syslog(LOG_ERR, "unable to add base_classification on line %d", lineno);
847 return -1;
848 }
849 }
850 if (add_cache(domain, raw, tok) < 0)
851 return -1;
852 }
853 return 0;
854 }
855
856 int
read_translations(const char * filename)857 read_translations(const char *filename) {
858 size_t size = 0;
859 char *buffer = NULL;
860 int rval = 0;
861
862 FILE *cfg = fopen(filename,"r");
863 if (!cfg) {
864 syslog(LOG_ERR, "%s file open failed", filename);
865 return -1;
866 }
867
868 __fsetlocking(cfg, FSETLOCKING_BYCALLER);
869 while (getline(&buffer, &size, cfg) > 0) {
870 if( process_trans(buffer) < 0 ) {
871 syslog(LOG_ERR, "%s file read failed", filename);
872 rval = -1;
873 break;
874 }
875 }
876 free(buffer);
877 fclose(cfg);
878 return rval;
879 }
880
881 int
init_translations(void)882 init_translations(void) {
883 if (is_selinux_mls_enabled() <= 0)
884 return -1;
885
886 return(read_translations(selinux_translations_path()));
887 }
888
889 static char *
extract_range(const char * incon)890 extract_range(const char *incon) {
891 context_t con = context_new(incon);
892 if (!con) {
893 syslog(LOG_ERR, "extract_range context_new(%s) failed: %s", incon, strerror(errno));
894 return NULL;
895 }
896
897 const char *range = context_range_get(con);
898 if (!range) {
899 syslog(LOG_ERR, "extract_range: context_range_get(%s) failed: %m", incon);
900 context_free(con);
901 return NULL;
902 }
903 char *r = strdup(range);
904 if (!r) {
905 log_error("extract_range: allocation error %s", strerror(errno));
906 return NULL;
907 }
908 context_free(con);
909 return r;
910 }
911
912 static char *
new_context_str(const char * incon,const char * range)913 new_context_str(const char *incon, const char *range) {
914 char *rcon = NULL;
915 context_t con = context_new(incon);
916 if (!con) {
917 goto exit;
918 }
919 context_range_set(con, range);
920 rcon = strdup(context_str(con));
921 context_free(con);
922 if (!rcon) {
923 goto exit;
924 }
925
926 return rcon;
927
928 exit:
929 log_error("new_context_str: %s %s", incon, strerror(errno));
930 return NULL;
931 }
932
933 static char *
find_in_hashtable(const char * range,domain_t * domain,context_map_node_t ** table)934 find_in_hashtable(const char *range, domain_t *domain, context_map_node_t **table) {
935 char *trans = NULL;
936 context_map_t *map = find_in_table(table, range);
937 if (map) {
938 trans = strdup((table == domain->raw_to_trans) ? map->trans: map->raw);
939 if (!trans) {
940 log_error("find_in_hashtable: allocation error %s", strerror(errno));
941 return NULL;
942 }
943 log_debug(" found %s in hashtable returning %s\n", range, trans);
944 }
945 return trans;
946 }
947
948 static int
string_size(const void * p1,const void * p2)949 string_size(const void *p1, const void *p2) {
950 return strlen(*(char **)p2) - strlen(*(char **)p1);
951 }
952
953 static int
word_size(const void * p1,const void * p2)954 word_size(const void *p1, const void *p2) {
955 word_t *w1 = *(word_t **)p1;
956 word_t *w2 = *(word_t **)p2;
957 int w1_len=strlen(w1->text);
958 int w2_len=strlen(w2->text);
959 if (w1_len == w2_len)
960 return strcmp(w1->text, w2->text);
961 return (w2_len - w1_len);
962 }
963
964 static void
build_regexp(pcre2_code ** r,char * buffer)965 build_regexp(pcre2_code **r, char *buffer) {
966 int error;
967 PCRE2_SIZE error_offset;
968 if (*r)
969 pcre2_code_free(*r);
970 *r = pcre2_compile((PCRE2_SPTR8) buffer, PCRE2_ZERO_TERMINATED, PCRE2_CASELESS, &error, &error_offset, NULL);
971 if (!*r) {
972 PCRE2_UCHAR errbuf[256];
973 pcre2_get_error_message(error, errbuf, sizeof(errbuf));
974 log_error("pcre compilation of '%s' failed at offset %zu: %s\n", buffer, error_offset, errbuf);
975 }
976 buffer[0] = '\0';
977 }
978
979 static int
build_regexps(domain_t * domain)980 build_regexps(domain_t *domain) {
981 char buffer[1024 * 128];
982 buffer[0] = '\0';
983 base_classification_t *bc;
984 word_group_t *g;
985 affix_t *a;
986 word_t *w;
987 size_t n_el, i;
988
989 for (n_el = 0, bc = domain->base_classifications; bc; bc = bc->next) {
990 n_el++;
991 }
992
993 char **sortable = calloc(n_el, sizeof(char *));
994 if (!sortable) {
995 log_error("allocation error %s", strerror(errno));
996 return -1;
997 }
998
999 for (i=0, bc = domain->base_classifications; bc; bc = bc->next) {
1000 sortable[i++] = bc->trans;
1001 }
1002
1003 qsort(sortable, n_el, sizeof(char *), string_size);
1004
1005 for (i = 0; i < n_el; i++) {
1006 strcat(buffer, sortable[i]);
1007 if (i < n_el) strcat(buffer,"|");
1008 }
1009
1010 free(sortable);
1011
1012 log_debug(">>> %s classification regexp=%s\n", domain->name, buffer);
1013 build_regexp(&domain->base_classification_regexp, buffer);
1014
1015 for (g = domain->groups; g; g = g->next) {
1016 if (g->prefixes) {
1017 strcat(buffer,"(?:");
1018 for (a = g->prefixes; a; a = a->next) {
1019 strcat(buffer, a->text);
1020 if (a->next) strcat(buffer,"|");
1021 }
1022 strcat(buffer,")");
1023 strcat(buffer,"[ ]+");
1024 log_debug(">>> %s %s prefix regexp=%s\n", domain->name, g->name, buffer);
1025 build_regexp(&g->prefix_regexp, buffer);
1026 }
1027
1028 if (g->prefixes)
1029 strcat(buffer, "^");
1030 strcat(buffer, "(?:");
1031
1032 g->sword_len=0;
1033 for (w = g->words; w; w = w->next)
1034 g->sword_len++;
1035
1036 g->sword = calloc(g->sword_len, sizeof(word_t *));
1037 if (!g->sword) {
1038 log_error("allocation error %s", strerror(errno));
1039 return -1;
1040 }
1041
1042 i=0;
1043 for (w = g->words; w; w = w->next)
1044 g->sword[i++]=w;
1045
1046 qsort(g->sword, g->sword_len, sizeof(word_t *), word_size);
1047
1048 for (i=0; i < g->sword_len; i++) {
1049 if (i) strcat(buffer,"|");
1050 strcat(buffer,"\\b");
1051 strcat(buffer, g->sword[i]->text);
1052 strcat(buffer,"\\b");
1053 }
1054
1055 if (g->whitespace) {
1056 strcat(buffer,"|[");
1057 strcat(buffer, g->whitespace);
1058 strcat(buffer, "]+");
1059 }
1060
1061 strcat(buffer, ")+");
1062 if (g->suffixes)
1063 strcat(buffer, "$");
1064
1065 log_debug(">>> %s %s modifier regexp=%s\n", domain->name, g->name, buffer);
1066 build_regexp(&g->word_regexp, buffer);
1067 if (g->suffixes) {
1068 strcat(buffer,"[ ]+");
1069 strcat(buffer,"(?:");
1070 for (a = g->suffixes; a; a = a->next) {
1071 strcat(buffer, a->text);
1072 if (a->next) strcat(buffer,"|");
1073 }
1074 strcat(buffer,")");
1075 log_debug(">>> %s %s suffix regexp=%s\n", domain->name, g->name, buffer);
1076 build_regexp(&g->suffix_regexp, buffer);
1077 }
1078 }
1079
1080 return 0;
1081 }
1082
1083 static char *
compute_raw_from_trans(const char * level,domain_t * domain)1084 compute_raw_from_trans(const char *level, domain_t *domain) {
1085
1086 #ifdef DEBUG
1087 struct timeval startTime;
1088 gettimeofday(&startTime, 0);
1089 #endif
1090
1091 int rc = 0;
1092 pcre2_match_data *match_data = NULL;
1093 word_group_t *g = NULL;
1094 char *work = NULL;
1095 char *r = NULL;
1096 char *match = NULL;
1097 size_t work_len;
1098 mls_level_t *mraw = NULL;
1099 ebitmap_t set, clear, tmp;
1100
1101 ebitmap_init(&set);
1102 ebitmap_init(&clear);
1103 ebitmap_init(&tmp);
1104
1105 work = strdup(level);
1106 if (!work) {
1107 log_error("compute_raw_from_trans: allocation error %s", strerror(errno));
1108 goto err;
1109 }
1110 work_len = strlen(work);
1111
1112 if (!domain->base_classification_regexp)
1113 if (build_regexps(domain) < 0)
1114 goto err;
1115 if (!domain->base_classification_regexp)
1116 goto err;
1117 log_debug(" compute_raw_from_trans work = %s\n", work);
1118 match_data = pcre2_match_data_create_from_pattern(domain->base_classification_regexp, NULL);
1119 if (!match_data) {
1120 log_error("allocation error %s", strerror(errno));
1121 goto err;
1122 }
1123 rc = pcre2_match(domain->base_classification_regexp, (PCRE2_SPTR8)work, work_len, 0, PCRE2_ANCHORED, match_data, NULL);
1124 if (rc > 0) {
1125 const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
1126 match = strndup(work + ovector[0], ovector[1] - ovector[0]);
1127 if (!match) {
1128 log_error("allocation error %s", strerror(errno));
1129 goto err;
1130 }
1131 log_debug(" compute_raw_from_trans match = %s len = %zu\n", match, strlen(match));
1132 base_classification_t *bc;
1133 for (bc = domain->base_classifications; bc; bc = bc->next) {
1134 if (!strcmp(bc->trans, match)) {
1135 log_debug(" compute_raw_from_trans base classification %s matched %s\n", level, bc->trans);
1136 mraw = malloc(sizeof(mls_level_t));
1137 if (!mraw) {
1138 log_error("allocation error %s", strerror(errno));
1139 goto err;
1140 }
1141 if (mls_level_cpy(mraw, bc->level) < 0)
1142 goto err;
1143 break;
1144 }
1145 }
1146
1147 memset(work + ovector[0], '#', ovector[1] - ovector[0]);
1148 char *p=work + ovector[0] + ovector[1];
1149 while (*p && (strchr(" ", *p) != NULL))
1150 *p++ = '#';
1151
1152 free(match);
1153 match = NULL;
1154 } else {
1155 switch (rc) {
1156 case PCRE2_ERROR_NOMATCH:
1157 log_debug(" compute_raw_from_trans no base classification matched %s\n", level);
1158 break;
1159 default:
1160 log_error("compute_raw_from_trans: base matching error for input '%s': %d\n", level, rc);
1161 break;
1162 }
1163 }
1164
1165 pcre2_match_data_free(match_data);
1166 match_data = NULL;
1167
1168 if (mraw == NULL) {
1169 goto err;
1170 }
1171
1172 int complete = 0;
1173 int change = 1;
1174 while(change && !complete) {
1175 change = 0;
1176 for (g = domain->groups; g && !change && !complete; g = g->next) {
1177 int prefix = 0, suffix = 0;
1178 PCRE2_SIZE prefix_offset = 0, prefix_len = 0;
1179 PCRE2_SIZE suffix_offset = 0, suffix_len = 0;
1180 if (g->prefix_regexp) {
1181 match_data = pcre2_match_data_create_from_pattern(g->prefix_regexp, NULL);
1182 if (!match_data) {
1183 log_error("allocation error %s", strerror(errno));
1184 goto err;
1185 }
1186 rc = pcre2_match(g->prefix_regexp, (PCRE2_SPTR8)work, work_len, 0, 0, match_data, NULL);
1187 if (rc > 0) {
1188 const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
1189 prefix = 1;
1190 prefix_offset = ovector[0];
1191 prefix_len = ovector[1] - ovector[0];
1192 } else if (rc != PCRE2_ERROR_NOMATCH) {
1193 log_error("compute_raw_from_trans: prefix matching error for input '%s': %d\n", level, rc);
1194 }
1195 pcre2_match_data_free(match_data);
1196 match_data = NULL;
1197 }
1198 if (g->suffix_regexp) {
1199 match_data = pcre2_match_data_create_from_pattern(g->suffix_regexp, NULL);
1200 if (!match_data) {
1201 log_error("allocation error %s", strerror(errno));
1202 goto err;
1203 }
1204 rc = pcre2_match(g->suffix_regexp, (PCRE2_SPTR8)work, work_len, 0, 0, match_data, NULL);
1205 if (rc > 0) {
1206 const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
1207 suffix = 1;
1208 suffix_offset = ovector[0];
1209 suffix_len = ovector[1] - ovector[0];
1210 } else if (rc != PCRE2_ERROR_NOMATCH) {
1211 log_error("compute_raw_from_trans: suffix matching error for input '%s': %d\n", level, rc);
1212 }
1213 pcre2_match_data_free(match_data);
1214 match_data = NULL;
1215 }
1216
1217 /* anchors prefix ^, suffix $ */
1218 if (((!g->prefixes && !g->suffixes) ||
1219 (g->prefixes && prefix) ||
1220 (g->suffixes && suffix)) &&
1221 g->word_regexp) {
1222 char *s = work + prefix_offset + prefix_len;
1223 PCRE2_SIZE len = (suffix_len ? suffix_offset : work_len) - prefix_len - prefix_offset;
1224 match_data = pcre2_match_data_create_from_pattern(g->word_regexp, NULL);
1225 if (!match_data) {
1226 log_error("allocation error %s", strerror(errno));
1227 goto err;
1228 }
1229 rc = pcre2_match(g->word_regexp, (PCRE2_SPTR8)s, len, 0, 0, match_data, NULL);
1230 if (rc > 0) {
1231 const PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data);
1232 match = strndup(s + ovector[0], ovector[1] - ovector[0]);
1233 if (!match) {
1234 log_error("allocation error %s", strerror(errno));
1235 goto err;
1236 }
1237 trim(match, g->whitespace);
1238 if (*match) {
1239 char *p = triml(match, g->whitespace);
1240 while (p && *p) {
1241 int plen = strlen(p);
1242 unsigned int i;
1243 for (i = 0; i < g->sword_len; i++) {
1244 word_t *w = g->sword[i];
1245 int wlen = strlen(w->text);
1246 if (plen >= wlen && !strncmp(w->text, p, strlen(w->text))){
1247 if (ebitmap_andnot(&set, &w->cat, &g->def, maxbit) < 0) goto err;
1248
1249 if (ebitmap_xor(&tmp, &w->cat, &g->def) < 0) goto err;
1250 if (ebitmap_and(&clear, &tmp, &g->def) < 0) goto err;
1251 if (ebitmap_union(&mraw->cat, &set) < 0) goto err;
1252
1253 ebitmap_destroy(&tmp);
1254 if (ebitmap_cpy(&tmp, &mraw->cat) < 0) goto err;
1255 ebitmap_destroy(&mraw->cat);
1256 if (ebitmap_andnot(&mraw->cat, &tmp, &clear, maxbit) < 0) goto err;
1257
1258 ebitmap_destroy(&tmp);
1259 ebitmap_destroy(&set);
1260 ebitmap_destroy(&clear);
1261 p += strlen(w->text);
1262 change++;
1263 break;
1264 }
1265 }
1266 if (i == g->sword_len) {
1267 syslog(LOG_ERR, "conversion error");
1268 break;
1269 }
1270 p = triml(p, g->whitespace);
1271 }
1272 memset(work + prefix_offset, '#', prefix_len);
1273 memset(work + suffix_offset, '#', suffix_len);
1274 memset(s + ovector[0], '#', ovector[1] - ovector[0]);
1275 }
1276 free(match);
1277 match = NULL;
1278 } else if (rc != PCRE2_ERROR_NOMATCH) {
1279 log_error("compute_raw_from_trans: word matching error for input '%s' for substring '%s': %d\n", level, s, rc);
1280 }
1281 pcre2_match_data_free(match_data);
1282 match_data = NULL;
1283 }
1284 /* YYY */
1285 complete=1;
1286 char *p = work;
1287 while(*p) {
1288 if (isalnum(*p++)) {
1289 complete=0;
1290 break;
1291 }
1292 }
1293 }
1294 }
1295 free(work);
1296 if (violates_constraints(mraw)) {
1297 complete = 0;
1298 }
1299 if (complete)
1300 r = mls_level_to_string(mraw);
1301 mls_level_destroy(mraw);
1302 free(mraw);
1303
1304 #ifdef DEBUG
1305 struct timeval stopTime;
1306 gettimeofday(&stopTime, 0);
1307 long int ms;
1308 if (startTime.tv_usec > stopTime.tv_usec)
1309 ms = (stopTime.tv_sec - startTime.tv_sec - 1) * 1000 + (stopTime.tv_usec/1000 + 1000 - startTime.tv_usec/1000);
1310 else
1311 ms = (stopTime.tv_sec - startTime.tv_sec ) * 1000 + (stopTime.tv_usec/1000 - startTime.tv_usec/1000);
1312 log_debug(" compute_raw_from_trans in %ld ms'\n", ms);
1313 #endif
1314
1315 return r;
1316
1317 err:
1318 mls_level_destroy(mraw);
1319 free(mraw);
1320 free(work);
1321 free(match);
1322 ebitmap_destroy(&tmp);
1323 ebitmap_destroy(&set);
1324 ebitmap_destroy(&clear);
1325 pcre2_match_data_free(match_data);
1326 return NULL;
1327 }
1328
1329 static char *
compute_trans_from_raw(const char * level,domain_t * domain)1330 compute_trans_from_raw(const char *level, domain_t *domain) {
1331
1332 #ifdef DEBUG
1333 struct timeval startTime;
1334 gettimeofday(&startTime, 0);
1335 #endif
1336
1337 word_group_t *g;
1338 mls_level_t *l = NULL;
1339 char *rval = NULL;
1340 word_group_t *groups = NULL;
1341 ebitmap_t bit_diff, temp, handled, nothandled, unhandled, orig_unhandled;
1342
1343 ebitmap_init(&bit_diff);
1344 ebitmap_init(&temp);
1345 ebitmap_init(&handled);
1346 ebitmap_init(¬handled);
1347 ebitmap_init(&unhandled);
1348 ebitmap_init(&orig_unhandled);
1349
1350 if (!level)
1351 goto err;
1352
1353 l = parse_raw(level);
1354 if (!l)
1355 goto err;
1356 log_debug(" compute_trans_from_raw raw = %s\n", level);
1357
1358 /* YYY */
1359 /* check constraints */
1360 if (violates_constraints(l)) {
1361 syslog(LOG_ERR, "%s violates constraints", level);
1362 goto err;
1363 }
1364
1365 int doInverse = l->sens > 0;
1366
1367 base_classification_t *bc, *last = NULL;
1368 int done = 0;
1369 for (bc = domain->base_classifications; bc && !done; bc = bc->next) {
1370 if (l->sens == bc->level->sens) {
1371 /* skip if alias of last bc */
1372 if (last &&
1373 last->level->sens == bc->level->sens &&
1374 ebitmap_cmp(&last->level->cat, &bc->level->cat) == 0)
1375 continue;
1376
1377 /* compute bits not consumed by base classification */
1378 if (ebitmap_xor(&unhandled, &l->cat, &bc->level->cat) < 0)
1379 goto err;
1380 if (ebitmap_cpy(&orig_unhandled, &unhandled) < 0)
1381 goto err;
1382
1383 /* prebuild groups */
1384 for (g = domain->groups; g; g = g->next) {
1385 word_group_t **t;
1386 for (t = &groups; *t; t = &(*t)->next)
1387 if (!strcmp(g->name, (*t)->name))
1388 break;
1389
1390 if (! *t) {
1391 word_group_t *wg = create_group(&groups, g->name);
1392 if (g->prefixes)
1393 if (append(&wg->prefixes, g->prefixes->text) < 0)
1394 goto err;
1395 if (g->suffixes)
1396 if (append(&wg->suffixes, g->suffixes->text) < 0)
1397 goto err;
1398 if (g->join)
1399 if (update(&wg->join, g->join) < 0)
1400 goto err;
1401 }
1402 }
1403
1404 int loops, hamming, change=1;
1405 for (loops = 50; ebitmap_cardinality(&unhandled) && loops > 0 && change; loops--) {
1406 change = 0;
1407 hamming = 10000;
1408 if (ebitmap_xor(&handled, &unhandled, &orig_unhandled) < 0)
1409 goto err;
1410 if (ebitmap_not(¬handled, &handled, maxbit) < 0)
1411 goto err;
1412 word_group_t *currentGroup = NULL;
1413 word_t *currentWord = NULL;
1414 for (g = domain->groups; g && hamming; g = g->next) {
1415 word_t *w;
1416 for (w = g->words; w && hamming; w = w->next) {
1417 int cardinality = ebitmap_cardinality(&w->normal);
1418 /* If the word is all inverse bits and the level does not have inverse bits - skip */
1419 if (cardinality && !doInverse) {
1420 continue;
1421 }
1422
1423 /* if only unhandled bits are different */
1424 if (ebitmap_or(&temp, &w->normal, &w->inverse) < 0)
1425 goto err;
1426 if (ebitmap_and(&bit_diff, &temp, ¬handled) < 0)
1427 goto err;
1428 ebitmap_destroy(&temp);
1429 // xor bit_diff handled?
1430 if (ebitmap_and(&temp, &bit_diff, &unhandled) < 0)
1431 goto err;
1432 if (ebitmap_cmp(&bit_diff, &temp)) {
1433 int h = ebitmap_hamming_distance(&bit_diff, &unhandled);
1434 if (h < hamming) {
1435 hamming = h;
1436 currentGroup = g;
1437 currentWord = w;
1438 }
1439 }
1440 ebitmap_destroy(&bit_diff);
1441 ebitmap_destroy(&temp);
1442 }
1443 }
1444 ebitmap_destroy(&handled);
1445 ebitmap_destroy(¬handled);
1446
1447 if (currentWord) {
1448 if (ebitmap_xor(&bit_diff, ¤tWord->cat, &bc->level->cat) < 0)
1449 goto err;
1450
1451 if (ebitmap_cpy(&temp, &unhandled) < 0)
1452 goto err;
1453 ebitmap_destroy(&unhandled);
1454 if (ebitmap_andnot(&unhandled, &temp, &bit_diff, maxbit) < 0)
1455 goto err;
1456
1457 ebitmap_destroy(&bit_diff);
1458 ebitmap_destroy(&temp);
1459
1460 word_group_t **t;
1461 for (t = &groups; *t; t = &(*t)->next)
1462 if (!strcmp(currentGroup->name, (*t)->name))
1463 break;
1464 create_word(&(*t)->words, currentWord->text);
1465 change++;
1466 }
1467 }
1468
1469 done = (ebitmap_cardinality(&unhandled) == 0);
1470 ebitmap_destroy(&unhandled);
1471 ebitmap_destroy(&orig_unhandled);
1472 if (done) {
1473 char buffer[9999];
1474 buffer[0] = 0;
1475 strcat(buffer, bc->trans);
1476 strcat(buffer, " ");
1477 for (g=groups; g; g = g->next) {
1478 if (g->words && g->prefixes) {
1479 strcat(buffer, g->prefixes->text);
1480 strcat(buffer, " ");
1481 }
1482 word_t *w;
1483 for (w=g->words; w; w = w->next) {
1484 strcat(buffer, w->text);
1485 if (w->next)
1486 strcat(buffer, g->join);
1487 }
1488 if (g->words && g->suffixes) {
1489 strcat(buffer, " ");
1490 strcat(buffer, g->suffixes->text);
1491 }
1492 word_group_t *n = g->next;
1493 while(g->words && n) {
1494 if (n->words) {
1495 strcat(buffer, " ");
1496 break;
1497 }
1498 n = n->next;
1499 }
1500 }
1501 rval = strdup(buffer);
1502 if (!rval) {
1503 log_error("compute_trans_from_raw: allocation error %s", strerror(errno));
1504 goto err;
1505 }
1506 }
1507 /* clean up */
1508 while (groups)
1509 destroy_group(&groups, groups);
1510 }
1511 last = bc;
1512 }
1513 if (l) {
1514 mls_level_destroy(l);
1515 free(l);
1516 }
1517
1518 #ifdef DEBUG
1519 struct timeval stopTime;
1520 gettimeofday(&stopTime, 0);
1521 long int ms;
1522 if (startTime.tv_usec > stopTime.tv_usec)
1523 ms = (stopTime.tv_sec - startTime.tv_sec - 1) * 1000 + (stopTime.tv_usec/1000 + 1000 - startTime.tv_usec/1000);
1524 else
1525 ms = (stopTime.tv_sec - startTime.tv_sec ) * 1000 + (stopTime.tv_usec/1000 - startTime.tv_usec/1000);
1526
1527 log_debug(" compute_trans_from_raw in %ld ms'\n", ms);
1528 #endif
1529
1530 return rval;
1531
1532 err:
1533 while (groups)
1534 destroy_group(&groups, groups);
1535 mls_level_destroy(l);
1536 free(l);
1537 return NULL;
1538 }
1539
1540 int
trans_context(const char * incon,char ** rcon)1541 trans_context(const char *incon, char **rcon) {
1542 char *trans = NULL;
1543 *rcon = NULL;
1544
1545 #ifdef DEBUG
1546 struct timeval startTime;
1547 gettimeofday(&startTime, 0);
1548 #endif
1549
1550 log_debug(" trans_context input = %s\n", incon);
1551 char *range = extract_range(incon);
1552 if (!range) return -1;
1553
1554 domain_t *domain = domains;
1555 for (;domain; domain = domain->next) {
1556 trans = find_in_hashtable(range, domain, domain->raw_to_trans);
1557 if (trans) break;
1558
1559 /* try split and translate */
1560 char *lrange = NULL, *urange = NULL;
1561 char *ltrans = NULL, *utrans = NULL;
1562 char *dashp = strchr(range,'-');
1563 if (dashp) {
1564 *dashp = 0;
1565 lrange = range;
1566 urange = dashp+1;
1567 } else {
1568 trans = compute_trans_from_raw(range, domain);
1569 if (trans)
1570 if (add_cache(domain, range, trans) < 0) {
1571 free(range);
1572 return -1;
1573 }
1574 }
1575
1576 if (lrange && urange) {
1577 ltrans = find_in_hashtable(lrange, domain, domain->raw_to_trans);
1578 if (! ltrans) {
1579 ltrans = compute_trans_from_raw(lrange, domain);
1580 if (ltrans) {
1581 if (add_cache(domain, lrange, ltrans) < 0) {
1582 free(range);
1583 return -1;
1584 }
1585 } else {
1586 ltrans = strdup(lrange);
1587 if (! ltrans) {
1588 log_error("strdup failed %s", strerror(errno));
1589 free(range);
1590 return -1;
1591 }
1592 }
1593 }
1594
1595 utrans = find_in_hashtable(urange, domain, domain->raw_to_trans);
1596 if (! utrans) {
1597 utrans = compute_trans_from_raw(urange, domain);
1598 if (utrans) {
1599 if (add_cache(domain, urange, utrans) < 0) {
1600 free(ltrans);
1601 free(range);
1602 return -1;
1603 }
1604 } else {
1605 utrans = strdup(urange);
1606 if (! utrans) {
1607 log_error("strdup failed %s", strerror(errno));
1608 free(ltrans);
1609 free(range);
1610 return -1;
1611 }
1612 }
1613 }
1614
1615 if (strcmp(ltrans, utrans) == 0) {
1616 if (asprintf(&trans, "%s", ltrans) < 0) {
1617 log_error("asprintf failed %s", strerror(errno));
1618 free(utrans);
1619 free(ltrans);
1620 free(range);
1621 return -1;
1622 }
1623 } else {
1624 if (asprintf(&trans, "%s-%s", ltrans, utrans) < 0) {
1625 log_error("asprintf failed %s", strerror(errno));
1626 free(utrans);
1627 free(ltrans);
1628 free(range);
1629 return -1;
1630 }
1631 }
1632 free(ltrans);
1633 free(utrans);
1634 *dashp = '-';
1635 break;
1636 }
1637 if (dashp)
1638 *dashp = '-';
1639 }
1640
1641 if (trans) {
1642 *rcon = new_context_str(incon, trans);
1643 free(trans);
1644 } else {
1645 *rcon = new_context_str(incon, range);
1646 }
1647 free(range);
1648
1649 #ifdef DEBUG
1650 struct timeval stopTime;
1651 gettimeofday(&stopTime, 0);
1652 long int ms;
1653 if (startTime.tv_usec > stopTime.tv_usec)
1654 ms = (stopTime.tv_sec - startTime.tv_sec - 1) * 1000 + (stopTime.tv_usec/1000 + 1000 - startTime.tv_usec/1000);
1655 else
1656 ms = (stopTime.tv_sec - startTime.tv_sec ) * 1000 + (stopTime.tv_usec/1000 - startTime.tv_usec/1000);
1657
1658 log_debug(" trans_context input='%s' output='%s in %ld ms'\n", incon, *rcon, ms);
1659 #endif
1660 return 0;
1661 }
1662
1663 int
untrans_context(const char * incon,char ** rcon)1664 untrans_context(const char *incon, char **rcon) {
1665 char *raw = NULL;
1666 *rcon = NULL;
1667
1668 #ifdef DEBUG
1669 struct timeval startTime;
1670 gettimeofday(&startTime, 0);
1671 #endif
1672
1673 log_debug(" untrans_context incon = %s\n", incon);
1674 char *range = extract_range(incon);
1675 if (!range) return -1;
1676 log_debug(" untrans_context range = %s\n", range);
1677
1678 domain_t *domain = domains;
1679 for (;domain; domain = domain->next) {
1680 raw = find_in_hashtable(range, domain, domain->trans_to_raw);
1681 if (raw) break;
1682
1683 /* try split and translate */
1684 char *lrange = NULL, *urange = NULL;
1685 char *lraw = NULL, *uraw = NULL;
1686 char *dashp = strchr(range,'-');
1687 if (dashp) {
1688 *dashp = 0;
1689 lrange = range;
1690 urange = dashp+1;
1691 } else {
1692 raw = compute_raw_from_trans(range, domain);
1693 if (raw) {
1694 char *canonical = find_in_hashtable(raw, domain, domain->raw_to_trans);
1695 if (!canonical) {
1696 canonical = compute_trans_from_raw(raw, domain);
1697 if (canonical && strcmp(canonical, range))
1698 if (add_cache(domain, raw, canonical) < 0) {
1699 free(range);
1700 return -1;
1701 }
1702 }
1703 if (canonical)
1704 free(canonical);
1705 if (add_cache(domain, raw, range) < 0) {
1706 free(range);
1707 return -1;
1708 }
1709 } else {
1710 log_debug("untrans_context unable to compute raw context %s\n", range);
1711 }
1712 }
1713
1714 if (lrange && urange) {
1715 lraw = find_in_hashtable(lrange, domain, domain->trans_to_raw);
1716 if (! lraw) {
1717 lraw = compute_raw_from_trans(lrange, domain);
1718 if (lraw) {
1719 char *canonical = find_in_hashtable(lraw, domain, domain->raw_to_trans);
1720 if (!canonical) {
1721 canonical = compute_trans_from_raw(lraw, domain);
1722 if (canonical)
1723 if (add_cache(domain, lraw, canonical) < 0) {
1724 free(lraw);
1725 free(range);
1726 return -1;
1727 }
1728 }
1729 if (canonical)
1730 free(canonical);
1731 if (add_cache(domain, lraw, lrange) < 0) {
1732 free(lraw);
1733 free(range);
1734 return -1;
1735 }
1736 } else {
1737 lraw = strdup(lrange);
1738 if (! lraw) {
1739 log_error("strdup failed %s", strerror(errno));
1740 free(range);
1741 return -1;
1742 }
1743 }
1744 }
1745
1746 uraw = find_in_hashtable(urange, domain, domain->trans_to_raw);
1747 if (! uraw) {
1748 uraw = compute_raw_from_trans(urange, domain);
1749 if (uraw) {
1750 char *canonical = find_in_hashtable(uraw, domain, domain->raw_to_trans);
1751 if (!canonical) {
1752 canonical = compute_trans_from_raw(uraw, domain);
1753 if (canonical)
1754 if (add_cache(domain, uraw, canonical) < 0) {
1755 free(uraw);
1756 free(lraw);
1757 free(range);
1758 return -1;
1759 }
1760 }
1761 if (canonical)
1762 free(canonical);
1763 if (add_cache(domain, uraw, urange) < 0) {
1764 free(uraw);
1765 free(lraw);
1766 free(range);
1767 return -1;
1768 }
1769 } else {
1770 uraw = strdup(urange);
1771 if (! uraw) {
1772 log_error("strdup failed %s", strerror(errno));
1773 free(lraw);
1774 free(range);
1775 return -1;
1776 }
1777 }
1778 }
1779
1780
1781 if (strcmp(lraw, uraw) == 0) {
1782 if (asprintf(&raw, "%s", lraw) < 0) {
1783 log_error("asprintf failed %s", strerror(errno));
1784 free(uraw);
1785 free(lraw);
1786 free(range);
1787 return -1;
1788 }
1789 } else {
1790 if (asprintf(&raw, "%s-%s", lraw, uraw) < 0) {
1791 log_error("asprintf failed %s", strerror(errno));
1792 free(uraw);
1793 free(lraw);
1794 free(range);
1795 return -1;
1796 }
1797 }
1798 free(lraw);
1799 free(uraw);
1800 *dashp = '-';
1801 break;
1802 }
1803 if (dashp)
1804 *dashp = '-';
1805 }
1806
1807 if (raw) {
1808 *rcon = new_context_str(incon, raw);
1809 free(raw);
1810 } else {
1811 *rcon = new_context_str(incon, range);
1812 }
1813 free(range);
1814
1815 #ifdef DEBUG
1816 struct timeval stopTime;
1817 gettimeofday(&stopTime, 0);
1818 long int ms;
1819 if (startTime.tv_usec > stopTime.tv_usec)
1820 ms = (stopTime.tv_sec - startTime.tv_sec - 1) * 1000 + (stopTime.tv_usec/1000 + 1000 - startTime.tv_usec/1000);
1821 else
1822 ms = (stopTime.tv_sec - startTime.tv_sec ) * 1000 + (stopTime.tv_usec/1000 - startTime.tv_usec/1000);
1823
1824 log_debug(" untrans_context input='%s' output='%s' n %ld ms\n", incon, *rcon, ms);
1825 #endif
1826 return 0;
1827 }
1828
1829 void
finish_context_translations(void)1830 finish_context_translations(void) {
1831 while(domains) {
1832 domain_t *next = domains->next;
1833 destroy_domain(domains);
1834 domains = next;
1835 }
1836 while(sens_constraints) {
1837 sens_constraint_t *next = sens_constraints->next;
1838 destroy_sens_constraint(&sens_constraints, sens_constraints);
1839 sens_constraints = next;
1840 }
1841 while(cat_constraints) {
1842 cat_constraint_t *next = cat_constraints->next;
1843 destroy_cat_constraint(&cat_constraints, cat_constraints);
1844 cat_constraints = next;
1845 }
1846 }
1847
1848