Home | History | Annotate | Download | only in src
      1 #include <errno.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include "boolean_internal.h"
      6 #include "debug.h"
      7 
      8 struct sepol_bool {
      9 	/* This boolean's name */
     10 	char *name;
     11 
     12 	/* Its value */
     13 	int value;
     14 };
     15 
     16 struct sepol_bool_key {
     17 	/* This boolean's name */
     18 	char *name;
     19 };
     20 
     21 int sepol_bool_key_create(sepol_handle_t * handle,
     22 			  const char *name, sepol_bool_key_t ** key_ptr)
     23 {
     24 
     25 	sepol_bool_key_t *tmp_key =
     26 	    (sepol_bool_key_t *) malloc(sizeof(struct sepol_bool_key));
     27 
     28 	if (!tmp_key) {
     29 		ERR(handle, "out of memory, " "could not create boolean key");
     30 		return STATUS_ERR;
     31 	}
     32 
     33 	tmp_key->name = strdup(name);
     34 	if (!tmp_key->name) {
     35 		ERR(handle, "out of memory, " "could not create boolean key");
     36 		free(tmp_key);
     37 		return STATUS_ERR;
     38 	}
     39 
     40 	*key_ptr = tmp_key;
     41 	return STATUS_SUCCESS;
     42 }
     43 
     44 hidden_def(sepol_bool_key_create)
     45 
     46 void sepol_bool_key_unpack(const sepol_bool_key_t * key, const char **name)
     47 {
     48 
     49 	*name = key->name;
     50 }
     51 
     52 hidden_def(sepol_bool_key_unpack)
     53 
     54 int sepol_bool_key_extract(sepol_handle_t * handle,
     55 			   const sepol_bool_t * boolean,
     56 			   sepol_bool_key_t ** key_ptr)
     57 {
     58 
     59 	if (sepol_bool_key_create(handle, boolean->name, key_ptr) < 0) {
     60 		ERR(handle, "could not extract key from boolean %s",
     61 		    boolean->name);
     62 		return STATUS_ERR;
     63 	}
     64 
     65 	return STATUS_SUCCESS;
     66 }
     67 
     68 void sepol_bool_key_free(sepol_bool_key_t * key)
     69 {
     70 	if (!key)
     71 		return;
     72 	free(key->name);
     73 	free(key);
     74 }
     75 
     76 int sepol_bool_compare(const sepol_bool_t * boolean,
     77 		       const sepol_bool_key_t * key)
     78 {
     79 
     80 	return strcmp(boolean->name, key->name);
     81 }
     82 
     83 int sepol_bool_compare2(const sepol_bool_t * boolean,
     84 			const sepol_bool_t * boolean2)
     85 {
     86 
     87 	return strcmp(boolean->name, boolean2->name);
     88 }
     89 
     90 /* Name */
     91 const char *sepol_bool_get_name(const sepol_bool_t * boolean)
     92 {
     93 
     94 	return boolean->name;
     95 }
     96 
     97 hidden_def(sepol_bool_get_name)
     98 
     99 int sepol_bool_set_name(sepol_handle_t * handle,
    100 			sepol_bool_t * boolean, const char *name)
    101 {
    102 
    103 	char *tmp_name = strdup(name);
    104 	if (!tmp_name) {
    105 		ERR(handle, "out of memory, could not set boolean name");
    106 		return STATUS_ERR;
    107 	}
    108 	free(boolean->name);
    109 	boolean->name = tmp_name;
    110 	return STATUS_SUCCESS;
    111 }
    112 
    113 hidden_def(sepol_bool_set_name)
    114 
    115 /* Value */
    116 int sepol_bool_get_value(const sepol_bool_t * boolean)
    117 {
    118 
    119 	return boolean->value;
    120 }
    121 
    122 hidden_def(sepol_bool_get_value)
    123 
    124 void sepol_bool_set_value(sepol_bool_t * boolean, int value)
    125 {
    126 
    127 	boolean->value = value;
    128 }
    129 
    130 hidden_def(sepol_bool_set_value)
    131 
    132 /* Create */
    133 int sepol_bool_create(sepol_handle_t * handle, sepol_bool_t ** bool_ptr)
    134 {
    135 
    136 	sepol_bool_t *boolean = (sepol_bool_t *) malloc(sizeof(sepol_bool_t));
    137 
    138 	if (!boolean) {
    139 		ERR(handle, "out of memory, "
    140 		    "could not create boolean record");
    141 		return STATUS_ERR;
    142 	}
    143 
    144 	boolean->name = NULL;
    145 	boolean->value = 0;
    146 
    147 	*bool_ptr = boolean;
    148 	return STATUS_SUCCESS;
    149 }
    150 
    151 hidden_def(sepol_bool_create)
    152 
    153 /* Deep copy clone */
    154 int sepol_bool_clone(sepol_handle_t * handle,
    155 		     const sepol_bool_t * boolean, sepol_bool_t ** bool_ptr)
    156 {
    157 
    158 	sepol_bool_t *new_bool = NULL;
    159 
    160 	if (sepol_bool_create(handle, &new_bool) < 0)
    161 		goto err;
    162 
    163 	if (sepol_bool_set_name(handle, new_bool, boolean->name) < 0)
    164 		goto err;
    165 
    166 	new_bool->value = boolean->value;
    167 
    168 	*bool_ptr = new_bool;
    169 	return STATUS_SUCCESS;
    170 
    171       err:
    172 	ERR(handle, "could not clone boolean record");
    173 	sepol_bool_free(new_bool);
    174 	return STATUS_ERR;
    175 }
    176 
    177 /* Destroy */
    178 void sepol_bool_free(sepol_bool_t * boolean)
    179 {
    180 
    181 	if (!boolean)
    182 		return;
    183 
    184 	free(boolean->name);
    185 	free(boolean);
    186 }
    187 
    188 hidden_def(sepol_bool_free)
    189