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 	const 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 = name;
     34 
     35 	*key_ptr = tmp_key;
     36 	return STATUS_SUCCESS;
     37 }
     38 
     39 hidden_def(sepol_bool_key_create)
     40 
     41 void sepol_bool_key_unpack(const sepol_bool_key_t * key, const char **name)
     42 {
     43 
     44 	*name = key->name;
     45 }
     46 
     47 hidden_def(sepol_bool_key_unpack)
     48 
     49 int sepol_bool_key_extract(sepol_handle_t * handle,
     50 			   const sepol_bool_t * boolean,
     51 			   sepol_bool_key_t ** key_ptr)
     52 {
     53 
     54 	if (sepol_bool_key_create(handle, boolean->name, key_ptr) < 0) {
     55 		ERR(handle, "could not extract key from boolean %s",
     56 		    boolean->name);
     57 		return STATUS_ERR;
     58 	}
     59 
     60 	return STATUS_SUCCESS;
     61 }
     62 
     63 void sepol_bool_key_free(sepol_bool_key_t * key)
     64 {
     65 	free(key);
     66 }
     67 
     68 int sepol_bool_compare(const sepol_bool_t * boolean,
     69 		       const sepol_bool_key_t * key)
     70 {
     71 
     72 	return strcmp(boolean->name, key->name);
     73 }
     74 
     75 int sepol_bool_compare2(const sepol_bool_t * boolean,
     76 			const sepol_bool_t * boolean2)
     77 {
     78 
     79 	return strcmp(boolean->name, boolean2->name);
     80 }
     81 
     82 /* Name */
     83 const char *sepol_bool_get_name(const sepol_bool_t * boolean)
     84 {
     85 
     86 	return boolean->name;
     87 }
     88 
     89 hidden_def(sepol_bool_get_name)
     90 
     91 int sepol_bool_set_name(sepol_handle_t * handle,
     92 			sepol_bool_t * boolean, const char *name)
     93 {
     94 
     95 	char *tmp_name = strdup(name);
     96 	if (!tmp_name) {
     97 		ERR(handle, "out of memory, could not set boolean name");
     98 		return STATUS_ERR;
     99 	}
    100 	free(boolean->name);
    101 	boolean->name = tmp_name;
    102 	return STATUS_SUCCESS;
    103 }
    104 
    105 hidden_def(sepol_bool_set_name)
    106 
    107 /* Value */
    108 int sepol_bool_get_value(const sepol_bool_t * boolean)
    109 {
    110 
    111 	return boolean->value;
    112 }
    113 
    114 hidden_def(sepol_bool_get_value)
    115 
    116 void sepol_bool_set_value(sepol_bool_t * boolean, int value)
    117 {
    118 
    119 	boolean->value = value;
    120 }
    121 
    122 hidden_def(sepol_bool_set_value)
    123 
    124 /* Create */
    125 int sepol_bool_create(sepol_handle_t * handle, sepol_bool_t ** bool_ptr)
    126 {
    127 
    128 	sepol_bool_t *boolean = (sepol_bool_t *) malloc(sizeof(sepol_bool_t));
    129 
    130 	if (!boolean) {
    131 		ERR(handle, "out of memory, "
    132 		    "could not create boolean record");
    133 		return STATUS_ERR;
    134 	}
    135 
    136 	boolean->name = NULL;
    137 	boolean->value = 0;
    138 
    139 	*bool_ptr = boolean;
    140 	return STATUS_SUCCESS;
    141 }
    142 
    143 hidden_def(sepol_bool_create)
    144 
    145 /* Deep copy clone */
    146 int sepol_bool_clone(sepol_handle_t * handle,
    147 		     const sepol_bool_t * boolean, sepol_bool_t ** bool_ptr)
    148 {
    149 
    150 	sepol_bool_t *new_bool = NULL;
    151 
    152 	if (sepol_bool_create(handle, &new_bool) < 0)
    153 		goto err;
    154 
    155 	if (sepol_bool_set_name(handle, new_bool, boolean->name) < 0)
    156 		goto err;
    157 
    158 	new_bool->value = boolean->value;
    159 
    160 	*bool_ptr = new_bool;
    161 	return STATUS_SUCCESS;
    162 
    163       err:
    164 	ERR(handle, "could not clone boolean record");
    165 	sepol_bool_free(new_bool);
    166 	return STATUS_ERR;
    167 }
    168 
    169 /* Destroy */
    170 void sepol_bool_free(sepol_bool_t * boolean)
    171 {
    172 
    173 	if (!boolean)
    174 		return;
    175 
    176 	free(boolean->name);
    177 	free(boolean);
    178 }
    179 
    180 hidden_def(sepol_bool_free)
    181