libsmack: fallback to 'cipso' when 'cipso2' is not available
[framework/security/smack.git] / libsmack / libsmack.c
1 /*
2  * This file is part of libsmack
3  *
4  * Copyright (C) 2010, 2011 Nokia Corporation
5  * Copyright (C) 2011, 2012, 2013 Intel Corporation
6  * Copyright (C) 2012, 2013 Samsung Electronics Co.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include <search.h>
24 #include "sys/smack.h"
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <sys/xattr.h>
36
37 #define SELF_LABEL_FILE "/proc/self/attr/current"
38
39 #define SHORT_LABEL_LEN 23
40 #define ACC_LEN 6
41 #define LOAD_LEN (2 * (SMACK_LABEL_LEN + 1) + 2 * ACC_LEN + 1)
42 #define KERNEL_LONG_FORMAT "%s %s %s"
43 #define KERNEL_SHORT_FORMAT "%-23s %-23s %5.5s"
44 #define KERNEL_MODIFY_FORMAT "%s %s %s %s"
45
46 #define LEVEL_MAX 255
47 #define NUM_LEN 4
48 #define BUF_SIZE 512
49 #define CAT_MAX_COUNT 240
50 #define CAT_MAX_VALUE 63
51 #define CIPSO_POS(i)   (SMACK_LABEL_LEN + 1 + NUM_LEN + NUM_LEN + i * NUM_LEN)
52 #define CIPSO_MAX_SIZE CIPSO_POS(CAT_MAX_COUNT)
53 #define CIPSO_NUM_LEN_STR "%-4d"
54
55 #define ACCESS_TYPE_R 0x01
56 #define ACCESS_TYPE_W 0x02
57 #define ACCESS_TYPE_X 0x04
58 #define ACCESS_TYPE_A 0x08
59 #define ACCESS_TYPE_T 0x10
60 #define ACCESS_TYPE_L 0x20
61
62 #define DICT_HASH_SIZE 4096
63
64 extern char *smackfs_mnt;
65 extern int smackfs_mnt_dirfd;
66
67 struct label_dict {
68         char **labels;
69         int nof_labels;
70         struct hsearch_data *htab;
71 };
72
73 struct smack_rule {
74         int8_t allow_code;
75         int8_t deny_code;
76         int subject_id;
77         int object_id;
78         struct smack_rule *next;
79 };
80
81 struct smack_accesses {
82         int has_long;
83         struct smack_rule *first;
84         struct smack_rule *last;
85         struct label_dict *dict;
86 };
87
88 struct cipso_mapping {
89         char label[SMACK_LABEL_LEN + 1];
90         int cats[CAT_MAX_VALUE];
91         int ncats;
92         int level;
93         struct cipso_mapping *next;
94 };
95
96 struct smack_cipso {
97         int has_long;
98         struct cipso_mapping *first;
99         struct cipso_mapping *last;
100 };
101
102 static int open_smackfs_file(const char *long_name, const char *short_name,
103                              int *use_long);
104 static int accesses_apply(struct smack_accesses *handle, int clear);
105 static int accesses_print(struct smack_accesses *handle, int clear,
106                           int load_fd, int change_fd, int use_long, int add_lf);
107 static inline ssize_t get_label(char *dest, const char *src);
108 static inline int str_to_access_code(const char *str);
109 static inline void access_code_to_str(unsigned code, char *str);
110 static int dict_create(struct label_dict **dict);
111 static int dict_free(struct label_dict *dict);
112 static const char *dict_get_label(const struct label_dict *dict, int id);
113 static ssize_t dict_add_label(struct label_dict *dict, int *id, const char *src);
114
115 int smack_accesses_new(struct smack_accesses **accesses)
116 {
117         struct smack_accesses *result;
118
119         result = calloc(sizeof(struct smack_accesses), 1);
120         if (result == NULL)
121                 return -1;
122
123         if (dict_create(&(result->dict)))
124                 return -1;
125         *accesses = result;
126         return 0;
127 }
128
129 void smack_accesses_free(struct smack_accesses *handle)
130 {
131         if (handle == NULL)
132                 return;
133
134         struct smack_rule *rule = handle->first;
135         struct smack_rule *next_rule = NULL;
136
137         while (rule != NULL) {
138                 next_rule = rule->next;
139                 free(rule);
140                 rule = next_rule;
141         }
142
143         dict_free(handle->dict);
144         free(handle);
145 }
146
147 int smack_accesses_save(struct smack_accesses *handle, int fd)
148 {
149         return accesses_print(handle, 0, fd, fd, 1, 1);
150 }
151
152 int smack_accesses_apply(struct smack_accesses *handle)
153 {
154         return accesses_apply(handle, 0);
155 }
156
157 int smack_accesses_clear(struct smack_accesses *handle)
158 {
159         return accesses_apply(handle, 1);
160 }
161
162 int accesses_add(struct smack_accesses *handle, const char *subject,
163                  const char *object, const char *allow_access_type,
164                  const char *deny_access_type)
165 {
166         struct smack_rule *rule = NULL;
167         int ret;
168
169         rule = calloc(sizeof(struct smack_rule), 1);
170         if (rule == NULL)
171                 return -1;
172
173         ret = dict_add_label(handle->dict, &(rule->subject_id), subject);
174         if (ret < 0)
175                 goto err_out;
176         if (ret > SHORT_LABEL_LEN)
177                 handle->has_long = 1;
178
179         ret = dict_add_label(handle->dict, &(rule->object_id), object);
180         if (ret < 0)
181                 goto err_out;
182         if (ret > SHORT_LABEL_LEN)
183                 handle->has_long = 1;
184
185         rule->allow_code = str_to_access_code(allow_access_type);
186         if (rule->allow_code == -1)
187                 goto err_out;
188
189         if (deny_access_type != NULL) {
190                 rule->deny_code = str_to_access_code(deny_access_type);
191                 if (rule->deny_code == -1)
192                         goto err_out;
193         } else
194                 rule->deny_code = -1; /* no modify */
195
196         if (handle->first == NULL) {
197                 handle->first = handle->last = rule;
198         } else {
199                 handle->last->next = rule;
200                 handle->last = rule;
201         }
202
203         return 0;
204 err_out:
205         free(rule);
206         return -1;
207 }
208
209 int smack_accesses_add(struct smack_accesses *handle, const char *subject,
210                        const char *object, const char *access_type)
211 {
212         return accesses_add(handle, subject, object, access_type, NULL);
213 }
214
215 int smack_accesses_add_modify(struct smack_accesses *handle,
216                               const char *subject,
217                               const char *object,
218                               const char *allow_access_type,
219                               const char *deny_access_type)
220 {
221         return accesses_add(handle, subject, object,
222                 allow_access_type, deny_access_type);
223 }
224
225 int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd)
226 {
227         FILE *file = NULL;
228         char buf[LOAD_LEN + 1];
229         char *ptr;
230         const char *subject, *object, *access, *access2;
231         int newfd;
232         int ret;
233
234         newfd = dup(fd);
235         if (newfd == -1)
236                 return -1;
237
238         file = fdopen(newfd, "r");
239         if (file == NULL) {
240                 close(newfd);
241                 return -1;
242         }
243
244         while (fgets(buf, LOAD_LEN + 1, file) != NULL) {
245                 if (strcmp(buf, "\n") == 0)
246                         continue;
247                 subject = strtok_r(buf, " \t\n", &ptr);
248                 object = strtok_r(NULL, " \t\n", &ptr);
249                 access = strtok_r(NULL, " \t\n", &ptr);
250                 access2 = strtok_r(NULL, " \t\n", &ptr);
251
252                 if (subject == NULL || object == NULL || access == NULL ||
253                     strtok_r(NULL, " \t\n", &ptr) != NULL) {
254                         fclose(file);
255                         return -1;
256                 }
257
258                 if (access2 == NULL)
259                         ret = smack_accesses_add(accesses, subject, object, access);
260                 else
261                         ret = smack_accesses_add_modify(accesses, subject, object, access, access2);
262
263                 if (ret) {
264                         fclose(file);
265                         return -1;
266                 }
267         }
268
269         if (ferror(file)) {
270                 fclose(file);
271                 return -1;
272         }
273
274         fclose(file);
275         return 0;
276 }
277
278 int smack_have_access(const char *subject, const char *object,
279                       const char *access_type)
280 {
281         char buf[LOAD_LEN + 1];
282         char str[ACC_LEN + 1];
283         int code;
284         int ret;
285         int fd;
286         int use_long = 1;
287
288         if (smackfs_mnt_dirfd < 0)
289                 return -1;
290
291         fd = open_smackfs_file("access2", "access", &use_long);
292         if (fd < 0)
293                 return -1;
294
295         if ((code = str_to_access_code(access_type)) < 0)
296                 return -1;
297         access_code_to_str(code, str);
298
299         if (use_long)
300                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
301                                subject, object, str);
302         else
303                 ret = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
304                                subject, object, str);
305
306         if (ret < 0) {
307                 close(fd);
308                 return -1;
309         }
310
311         ret = write(fd, buf, strlen(buf));
312         if (ret < 0) {
313                 close(fd);
314                 return -1;
315         }
316
317         ret = read(fd, buf, 1);
318         close(fd);
319         if (ret < 0)
320                 return -1;
321
322         return buf[0] == '1';
323 }
324
325 int smack_cipso_new(struct smack_cipso **cipso)
326 {
327         struct smack_cipso *result;
328
329         result = calloc(sizeof(struct smack_cipso), 1);
330         if (result == NULL)
331                 return -1;
332
333         *cipso = result;
334         return 0;
335 }
336
337 void smack_cipso_free(struct smack_cipso *cipso)
338 {
339         if (cipso == NULL)
340                 return;
341
342         struct cipso_mapping *mapping = cipso->first;
343         struct cipso_mapping *next_mapping = NULL;
344
345         while (mapping != NULL) {
346                 next_mapping = mapping->next;
347                 free(mapping);
348                 mapping = next_mapping;
349         }
350
351         free(cipso);
352 }
353
354 int smack_cipso_apply(struct smack_cipso *cipso)
355 {
356         struct cipso_mapping *m = NULL;
357         char buf[CIPSO_MAX_SIZE];
358         int fd;
359         int i;
360         int offset;
361         int use_long;
362
363         if (smackfs_mnt_dirfd < 0)
364                 return -1;
365
366         fd = open_smackfs_file("cipso2", "cipso", &use_long);
367         if (fd < 0)
368                 return -1;
369
370         if (!use_long && cipso->has_long)
371                 return -1;
372
373         memset(buf,0,CIPSO_MAX_SIZE);
374         for (m = cipso->first; m != NULL; m = m->next) {
375                 snprintf(buf, SMACK_LABEL_LEN + 1, use_long ? "%s" : "%-23s",
376                          m->label);
377                 offset = strlen(buf) + 1;
378
379                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->level);
380                 offset += NUM_LEN;
381
382                 sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->ncats);
383                 offset += NUM_LEN;
384
385                 for (i = 0; i < m->ncats; i++){
386                         sprintf(&buf[offset], CIPSO_NUM_LEN_STR, m->cats[i]);
387                         offset += NUM_LEN;
388                 }
389
390                 if (write(fd, buf, offset) < 0) {
391                         close(fd);
392                         return -1;
393                 }
394         }
395
396         close(fd);
397         return 0;
398 }
399
400 int smack_cipso_add_from_file(struct smack_cipso *cipso, int fd)
401 {
402         struct cipso_mapping *mapping = NULL;
403         FILE *file = NULL;
404         char buf[BUF_SIZE];
405         char *label, *level, *cat, *ptr;
406         long int val;
407         int i;
408         int newfd;
409
410         newfd = dup(fd);
411         if (newfd == -1)
412                 return -1;
413
414         file = fdopen(newfd, "r");
415         if (file == NULL) {
416                 close(newfd);
417                 return -1;
418         }
419
420         while (fgets(buf, BUF_SIZE, file) != NULL) {
421                 mapping = calloc(sizeof(struct cipso_mapping), 1);
422                 if (mapping == NULL)
423                         goto err_out;
424
425                 label = strtok_r(buf, " \t\n", &ptr);
426                 level = strtok_r(NULL, " \t\n", &ptr);
427                 cat = strtok_r(NULL, " \t\n", &ptr);
428
429                 if (level == NULL)
430                         goto err_out;
431
432                 val  = get_label(mapping->label, label);
433                 if (val < 0)
434                         goto err_out;
435                 if (val > SHORT_LABEL_LEN)
436                         cipso->has_long = 1;
437
438                 errno = 0;
439                 val = strtol(level, NULL, 10);
440                 if (errno)
441                         goto err_out;
442
443                 if (val < 0 || val > LEVEL_MAX)
444                         goto err_out;
445
446                 mapping->level = val;
447
448                 for (i = 0; i < CAT_MAX_COUNT && cat != NULL; i++) {
449                         errno = 0;
450                         val = strtol(cat, NULL, 10);
451                         if (errno)
452                                 goto err_out;
453
454                         if (val < 0 || val > CAT_MAX_VALUE)
455                                 goto err_out;
456
457                         mapping->cats[i] = val;
458
459                         cat = strtok_r(NULL, " \t\n", &ptr);
460                 }
461
462                 mapping->ncats = i;
463
464                 if (cipso->first == NULL) {
465                         cipso->first = cipso->last = mapping;
466                 } else {
467                         cipso->last->next = mapping;
468                         cipso->last = mapping;
469                 }
470         }
471
472         if (ferror(file))
473                 goto err_out;
474
475         fclose(file);
476         return 0;
477 err_out:
478         fclose(file);
479         free(mapping);
480         return -1;
481 }
482
483 const char *smack_smackfs_path(void)
484 {
485         return smackfs_mnt;
486 }
487
488 ssize_t smack_new_label_from_self(char **label)
489 {
490         char *result;
491         int fd;
492         int ret;
493
494         result = calloc(SMACK_LABEL_LEN + 1, 1);
495         if (result == NULL)
496                 return -1;
497
498         fd = open(SELF_LABEL_FILE, O_RDONLY);
499         if (fd < 0) {
500                 free(result);
501                 return -1;
502         }
503
504         ret = read(fd, result, SMACK_LABEL_LEN);
505         close(fd);
506         if (ret < 0) {
507                 free(result);
508                 return -1;
509         }
510
511         *label = result;
512         return ret;
513 }
514
515 ssize_t smack_new_label_from_socket(int fd, char **label)
516 {
517         char dummy;
518         int ret;
519         socklen_t length = 1;
520         char *result;
521
522         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, &dummy, &length);
523         if (ret < 0 && errno != ERANGE)
524                 return -1;
525
526         result = calloc(length + 1, 1);
527         if (result == NULL)
528                 return -1;
529
530         ret = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, result, &length);
531         if (ret < 0) {
532                 free(result);
533                 return -1;
534         }
535
536         *label = result;
537         return length;
538 }
539
540 ssize_t smack_new_label_from_path(const char *path, const char *xattr, 
541                                   int follow, char **label)
542 {
543         char buf[SMACK_LABEL_LEN + 1];
544         char *result;
545         ssize_t ret = 0;
546
547         ret = follow ?
548                 getxattr(path, xattr, buf, SMACK_LABEL_LEN + 1) :
549                 lgetxattr(path, xattr, buf, SMACK_LABEL_LEN + 1);
550         if (ret < 0)
551                 return -1;
552
553         result = calloc(ret + 1, 1);
554         if (result == NULL)
555                 return -1;
556
557         ret = get_label(result, buf);
558         if (ret < 0) {
559                 free(result);
560                 return -1;
561         }
562
563         *label = result;
564         return ret;
565 }
566
567 int smack_set_label_for_self(const char *label)
568 {
569         int len;
570         int fd;
571         int ret;
572
573         len = get_label(NULL, label);
574         if (len < 0)
575                 return -1;
576
577         fd = open(SELF_LABEL_FILE, O_WRONLY);
578         if (fd < 0)
579                 return -1;
580
581         ret = write(fd, label, len);
582         close(fd);
583
584         return (ret < 0) ? -1 : 0;
585 }
586
587 int smack_revoke_subject(const char *subject)
588 {
589         int ret;
590         int fd;
591         int len;
592
593         if (smackfs_mnt_dirfd < 0)
594                 return -1;
595
596         len = get_label(NULL, subject);
597         if (len < 0)
598                 return -1;
599
600         fd = openat(smackfs_mnt_dirfd, "revoke-subject", O_WRONLY);
601         if (fd < 0)
602                 return -1;
603
604         ret = write(fd, subject, len);
605         close(fd);
606
607         return (ret < 0) ? -1 : 0;
608 }
609
610 static int open_smackfs_file(const char *long_name, const char *short_name,
611                              int *use_long)
612 {
613         int fd;
614
615         fd = openat(smackfs_mnt_dirfd, long_name, O_WRONLY);
616         if (fd < 0) {
617                 if (errno != ENOENT)
618                         return -1;
619
620                 fd = openat(smackfs_mnt_dirfd, short_name, O_WRONLY);
621                 if (fd < 0)
622                         return -1;
623
624                 *use_long = 0;
625                 return fd;
626         }
627
628         *use_long = 1;
629         return fd;
630 }
631
632 static int accesses_apply(struct smack_accesses *handle, int clear)
633 {
634         int ret;
635         int load_fd;
636         int change_fd;
637         int use_long = 1;
638
639         if (smackfs_mnt_dirfd < 0)
640                 return -1;
641
642         load_fd = open_smackfs_file("load2", "load", &use_long);
643         if (load_fd < 0)
644                 return -1;
645
646         change_fd = openat(smackfs_mnt_dirfd, "change-rule", O_WRONLY);
647         /* Try to continue if the file doesn't exist, we might not need it. */
648         if (change_fd < 0 && errno != ENOENT) {
649                 ret = -1;
650                 goto err_out;
651         }
652
653         ret = accesses_print(handle, clear, load_fd, change_fd, use_long, 0);
654
655 err_out:
656         if (load_fd >= 0)
657                 close(load_fd);
658         if (change_fd >= 0)
659                 close(change_fd);
660         return ret;
661 }
662
663 static int accesses_print(struct smack_accesses *handle, int clear,
664                           int load_fd, int change_fd, int use_long, int add_lf)
665 {
666         char buf[LOAD_LEN + 1];
667         char allow_str[ACC_LEN + 1];
668         char deny_str[ACC_LEN + 1];
669         struct smack_rule *rule;
670         int ret;
671         int fd;
672         int i;
673         int cnt;
674
675         if (!use_long && handle->has_long)
676                 return -1;
677
678         for (rule = handle->first; rule != NULL; rule = rule->next) {
679                 /* Fail immediately without doing any further processing
680                    if modify rules are not supported. */
681                 if (rule->deny_code >= 0 && change_fd < 0)
682                         return -1;
683
684                 access_code_to_str(clear ? 0 : rule->allow_code, allow_str);
685
686                 if (rule->deny_code != -1 && !clear) {
687                         access_code_to_str(rule->deny_code, deny_str);
688
689                         fd = change_fd;
690                         cnt = snprintf(buf, LOAD_LEN + 1, KERNEL_MODIFY_FORMAT,
691                                        dict_get_label(handle->dict, rule->subject_id),
692                                        dict_get_label(handle->dict, rule->object_id),
693                                        allow_str,
694                                        deny_str);
695                 } else {
696                         fd = load_fd;
697                         if (use_long)
698                                 cnt = snprintf(buf, LOAD_LEN + 1, KERNEL_LONG_FORMAT,
699                                                dict_get_label(handle->dict, rule->subject_id),
700                                                dict_get_label(handle->dict, rule->object_id),
701                                                allow_str);
702                         else {
703                                 cnt = snprintf(buf, LOAD_LEN + 1, KERNEL_SHORT_FORMAT,
704                                                dict_get_label(handle->dict, rule->subject_id),
705                                                dict_get_label(handle->dict, rule->object_id),
706                                                allow_str);
707                         }
708                 }
709
710                 if (cnt < 0)
711                         return -1;
712                 if (add_lf)
713                         buf[cnt++] = '\n';
714
715                 for (i = 0; i < cnt; ) {
716                         ret = write(fd, buf + i, cnt - i);
717                         if (ret == -1) {
718                                 if (errno == EINTR)
719                                         continue;
720                                 return -1;
721                         }
722                         i += ret;
723                 }
724         }
725
726         return 0;
727 }
728
729 static inline ssize_t get_label(char *dest, const char *src)
730 {
731         int i;
732
733         if (!src || src[0] == '\0' || src[0] == '-')
734                 return -1;
735
736         for (i = 0; i < (SMACK_LABEL_LEN + 1) && src[i]; i++) {
737                 if (src[i] <= ' ' || src[i] > '~')
738                         return -1;
739                 switch (src[i]) {
740                 case '/':
741                 case '"':
742                 case '\\':
743                 case '\'':
744                         return -1;
745                 default:
746                         break;
747                 }
748
749                 if (dest)
750                         dest[i] = src[i];
751         }
752
753         if (dest && i < (SMACK_LABEL_LEN + 1))
754                 dest[i] = '\0';
755
756         return i < (SMACK_LABEL_LEN + 1) ? i : -1;
757 }
758
759
760 static inline int str_to_access_code(const char *str)
761 {
762         int i;
763         unsigned int code = 0;
764
765         for (i = 0; str[i] != '\0'; i++) {
766                 switch (str[i]) {
767                 case 'r':
768                 case 'R':
769                         code |= ACCESS_TYPE_R;
770                         break;
771                 case 'w':
772                 case 'W':
773                         code |= ACCESS_TYPE_W;
774                         break;
775                 case 'x':
776                 case 'X':
777                         code |= ACCESS_TYPE_X;
778                         break;
779                 case 'a':
780                 case 'A':
781                         code |= ACCESS_TYPE_A;
782                         break;
783                 case 't':
784                 case 'T':
785                         code |= ACCESS_TYPE_T;
786                         break;
787                 case 'l':
788                 case 'L':
789                         code |= ACCESS_TYPE_L;
790                         break;
791                 case '-':
792                         break;
793                 default:
794                         return -1;
795                 }
796         }
797
798         return code;
799 }
800
801 static inline void access_code_to_str(unsigned int code, char *str)
802 {
803         str[0] = ((code & ACCESS_TYPE_R) != 0) ? 'r' : '-';
804         str[1] = ((code & ACCESS_TYPE_W) != 0) ? 'w' : '-';
805         str[2] = ((code & ACCESS_TYPE_X) != 0) ? 'x' : '-';
806         str[3] = ((code & ACCESS_TYPE_A) != 0) ? 'a' : '-';
807         str[4] = ((code & ACCESS_TYPE_T) != 0) ? 't' : '-';
808         str[5] = ((code & ACCESS_TYPE_L) != 0) ? 'l' : '-';
809         str[6] = '\0';
810 }
811
812 static int dict_create(struct label_dict **dict)
813 {
814         *dict = calloc(1, sizeof(struct label_dict));
815         if (!*dict)
816                 goto err;
817         (*dict)->htab = calloc(1, sizeof(struct hsearch_data));
818         if (!(*dict)->htab)
819                 goto free_dict;
820         (*dict)->labels = calloc(DICT_HASH_SIZE, sizeof(char *));
821         if (!(*dict)->labels)
822                 goto free_htab;
823         if (hcreate_r(DICT_HASH_SIZE, (*dict)->htab) == 0)
824                 goto free_labels;
825         return 0;
826
827 free_labels:
828         free((*dict)->labels);
829 free_htab:
830         free((*dict)->htab);
831 free_dict:
832         free(*dict);
833 err:
834         return -1;
835 }
836
837 static int dict_free(struct label_dict *dict)
838 {
839         int i;
840         for (i = 0; i < (dict->nof_labels); i++)
841                 free((dict->labels)[i]);
842         free(dict->labels);
843         hdestroy_r(dict->htab);
844         free(dict->htab);
845         free(dict);
846         return 0;
847 }
848
849 static ssize_t dict_add_label(struct label_dict *dict, int *id, const char *label)
850 {
851         ENTRY e, *ep;
852         int ret, search;
853
854         ret = get_label(NULL, label);
855
856         if (dict->nof_labels == DICT_HASH_SIZE)
857                 return -2;
858         if (ret == -1)
859                 return -1;
860
861         e.key =  (char *)label;
862         e.data = (void *)(&(dict->labels[dict->nof_labels]));
863
864         search = hsearch_r(e, ENTER, &ep, dict->htab);
865         if (search == 0)
866                 return -2;
867         if (e.data != ep->data) {/*found an existing entry*/
868                 *id = (int)((char **)(ep->data) - dict->labels);
869         } else {/*new entry added*/
870                 ep->key = malloc(ret + 1);
871                 if (!ep->key)
872                         return -3;
873                 ep->key[ret] = '\0';
874                 memcpy(ep->key, label, ret);
875                 dict->labels[dict->nof_labels] = ep->key;
876                 *id = dict->nof_labels++;
877         }
878         return ret;
879 }
880
881 static const char *dict_get_label(const struct label_dict *dict, int id)
882 {
883         if (id < dict->nof_labels)
884                 return dict->labels[id];
885         else
886                 return NULL;
887 }