[461541a] | 1 | |
---|
| 2 | /* abac_attribute.c */ |
---|
| 3 | |
---|
| 4 | #define _GNU_SOURCE |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <assert.h> |
---|
| 7 | #include <ctype.h> |
---|
| 8 | #include <string.h> |
---|
| 9 | #include <time.h> |
---|
| 10 | |
---|
| 11 | #include "libabac_common.h" |
---|
[4721618] | 12 | #include "abac_list.h" |
---|
[461541a] | 13 | #include "abac_util.h" |
---|
| 14 | #include "abac_xml.h" |
---|
| 15 | |
---|
| 16 | #define ROLE_SEPARATOR " <- " |
---|
| 17 | #define INTERSECTION_SEP " & " |
---|
| 18 | #define SHA1_LENGTH 40 |
---|
| 19 | |
---|
[bc12f3d] | 20 | #define DEFAULT_OUTPUT_FORMAT "GENIv1.1" |
---|
| 21 | |
---|
[461541a] | 22 | // a GENI XML attribute chunk might contain multiple |
---|
| 23 | // attribute rules. It will be translate into multiple |
---|
| 24 | // abac_attribute structures but with cert ptr pointing |
---|
| 25 | // to the same xml chunk |
---|
| 26 | // issuer can be missing but then it won't be bakable |
---|
| 27 | // unless it is baked just for creddy's roles call |
---|
| 28 | struct _abac_attribute_t { |
---|
| 29 | abac_id_t *issuer_id; |
---|
| 30 | char *role; |
---|
| 31 | long validity; |
---|
[ab63ece] | 32 | int ntails; |
---|
[461541a] | 33 | |
---|
| 34 | char *head_string; |
---|
[ab63ece] | 35 | char **tail_strings; |
---|
[bc12f3d] | 36 | char *output_format; |
---|
[d2b198c] | 37 | abac_keyid_map_t *keymap; |
---|
[461541a] | 38 | |
---|
| 39 | abac_chunk_t cert; // the XML chunk |
---|
| 40 | }; |
---|
| 41 | |
---|
| 42 | char *abac_attribute_role_string(abac_attribute_t *attr); |
---|
[13b087a] | 43 | extern abac_id_t *abac_verifier_lookup_id(abac_list_t*, char *keyid); |
---|
[461541a] | 44 | static char *_validate_principal(char *keyid); |
---|
| 45 | |
---|
| 46 | /************************************************************/ |
---|
| 47 | abac_chunk_t abac_attribute_cert(abac_attribute_t *ptr) |
---|
| 48 | { |
---|
| 49 | assert(ptr); |
---|
| 50 | return ptr->cert; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | abac_id_t *abac_attribute_issuer_id(abac_attribute_t *ptr) |
---|
| 54 | { |
---|
| 55 | assert(ptr); |
---|
| 56 | return ptr->issuer_id; |
---|
| 57 | } |
---|
| 58 | |
---|
[bc12f3d] | 59 | /* Get the format for this attribute to be output in. This is NULL if the |
---|
| 60 | * attribute has been read from a file. */ |
---|
| 61 | char *abac_attribute_get_output_format(abac_attribute_t *a) { |
---|
| 62 | return a->output_format; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /* Set the format for this attribute to be output in. Valid formats are: |
---|
| 66 | * GENIv1.0 |
---|
| 67 | * GENIv1.1 |
---|
| 68 | */ |
---|
| 69 | void abac_attribute_set_output_format(abac_attribute_t *a, char *fmt) { |
---|
| 70 | if (a->output_format) |
---|
| 71 | free(a->output_format); |
---|
| 72 | a->output_format = abac_xstrdup(fmt); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
[461541a] | 76 | // validity is measured in seconds (as of 0.2.0) |
---|
| 77 | // Acme.customer |
---|
| 78 | int abac_attribute_create(abac_attribute_t **ret, abac_id_t *issuer_id, char *role, long validity) { |
---|
[ef68ac9] | 79 | libabac_init(); |
---|
[461541a] | 80 | if (!abac_id_has_privkey(issuer_id)) |
---|
| 81 | return ABAC_ATTRIBUTE_ISSUER_NOKEY; |
---|
| 82 | if (!abac_clean_name(role)) |
---|
| 83 | return ABAC_ATTRIBUTE_INVALID_ROLE; |
---|
| 84 | if (validity < 0) |
---|
| 85 | return ABAC_ATTRIBUTE_INVALID_VALIDITY; |
---|
| 86 | if (!abac_id_still_valid(issuer_id)) |
---|
| 87 | return ABAC_ATTRIBUTE_INVALID_ISSUER; |
---|
| 88 | |
---|
[7e3f5e2] | 89 | if(validity == 0) validity = (long)(60*60*24*(365)); |
---|
| 90 | |
---|
[461541a] | 91 | abac_attribute_t *attr = abac_xmalloc(sizeof(abac_attribute_t)); |
---|
| 92 | if(issuer_id) attr->issuer_id = abac_id_dup(issuer_id); |
---|
| 93 | else attr->issuer_id = NULL; |
---|
| 94 | attr->role = abac_xstrdup(role); |
---|
| 95 | attr->validity = validity; |
---|
| 96 | |
---|
| 97 | attr->head_string = NULL; |
---|
[3c30b59] | 98 | int rc=asprintf(&attr->head_string,"%s.%s",abac_id_keyid(issuer_id),role); |
---|
[ab63ece] | 99 | attr->ntails = 0; |
---|
| 100 | attr->tail_strings = NULL; |
---|
[d2b198c] | 101 | attr->keymap = NULL; |
---|
[461541a] | 102 | |
---|
[bc12f3d] | 103 | attr->output_format = abac_xstrdup(DEFAULT_OUTPUT_FORMAT); |
---|
| 104 | |
---|
[461541a] | 105 | // NULL until baked |
---|
[8164e70] | 106 | attr->cert=abac_chunk_new(NULL,0); |
---|
[461541a] | 107 | |
---|
| 108 | *ret = attr; |
---|
| 109 | return ABAC_SUCCESS; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Get the validity period.(xml module returns the diff from expire time - now() |
---|
| 114 | */ |
---|
| 115 | int abac_attribute_validity(abac_attribute_t *attr,struct tm *not_before,struct tm *not_after) { |
---|
| 116 | assert(attr); |
---|
| 117 | memset(not_before, 0, sizeof(struct tm)); |
---|
| 118 | memset(not_after, 0, sizeof(struct tm)); |
---|
| 119 | |
---|
| 120 | time_t now; |
---|
| 121 | time(&now); |
---|
| 122 | gmtime_r(&now, not_before); |
---|
[9ac7fb4] | 123 | char *xml=(char *)attr->cert.ptr; |
---|
[461541a] | 124 | long validity=get_validity_from_xml(xml); |
---|
| 125 | |
---|
| 126 | time_t etime = now + validity; |
---|
| 127 | gmtime_r(&etime, not_after); |
---|
| 128 | |
---|
| 129 | if(validity == 0) |
---|
| 130 | return ABAC_FAILURE; |
---|
| 131 | return ABAC_SUCCESS; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | int abac_attribute_still_valid(abac_attribute_t *attr) |
---|
| 135 | { |
---|
| 136 | assert(attr); |
---|
| 137 | assert(attr->cert.ptr); |
---|
[9ac7fb4] | 138 | long v=get_validity_from_xml((char *)attr->cert.ptr); |
---|
[461541a] | 139 | if (v > 0.0) |
---|
| 140 | return 1; |
---|
| 141 | else return 0; |
---|
| 142 | } |
---|
| 143 | |
---|
[f2622ee] | 144 | /* string is a malloc copy */ |
---|
[ab63ece] | 145 | int abac_attribute_add_tail(abac_attribute_t *attr, char *string) { |
---|
| 146 | assert(attr); |
---|
| 147 | |
---|
| 148 | char **old_tail = attr->tail_strings; |
---|
| 149 | int newsize = (attr->ntails+1)*sizeof(char *); |
---|
| 150 | |
---|
| 151 | if ( !(attr->tail_strings = realloc(attr->tail_strings, newsize))) { |
---|
| 152 | attr->tail_strings = old_tail; |
---|
| 153 | return 0; |
---|
| 154 | } |
---|
[f2622ee] | 155 | attr->tail_strings[attr->ntails++] = string; |
---|
[ab63ece] | 156 | return 1; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | |
---|
[461541a] | 160 | void abac_attribute_set_head(abac_attribute_t *attr, char *string) |
---|
| 161 | { |
---|
| 162 | assert(attr); |
---|
| 163 | attr->head_string=string; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | char *abac_attribute_get_head(abac_attribute_t *attr) |
---|
| 167 | { |
---|
| 168 | assert(attr); |
---|
| 169 | return attr->head_string; |
---|
| 170 | } |
---|
[e50f807] | 171 | |
---|
[ab63ece] | 172 | /* |
---|
[e50f807] | 173 | * Return the number of tail strings |
---|
| 174 | */ |
---|
| 175 | int abac_attribute_get_ntails(abac_attribute_t *attr) { |
---|
[461541a] | 176 | assert(attr); |
---|
[e50f807] | 177 | return attr->ntails; |
---|
[461541a] | 178 | } |
---|
| 179 | |
---|
[e50f807] | 180 | /* |
---|
| 181 | * Return the nth tail string or NULL if it is undefined |
---|
| 182 | */ |
---|
| 183 | |
---|
| 184 | char *abac_attribute_get_tail_n(abac_attribute_t *attr, int n) { |
---|
[461541a] | 185 | assert(attr); |
---|
[e50f807] | 186 | if ( n < 0 || n > attr->ntails) return NULL; |
---|
| 187 | return attr->tail_strings[n]; |
---|
[461541a] | 188 | } |
---|
[e50f807] | 189 | |
---|
[461541a] | 190 | |
---|
| 191 | /* A.b->C, return copy of a A */ |
---|
| 192 | char *abac_attribute_get_principal(abac_attribute_t *attr) |
---|
| 193 | { |
---|
[f2622ee] | 194 | /* already a copy */ |
---|
| 195 | char *tmp=abac_attribute_role_string(attr); |
---|
[461541a] | 196 | char *head_tail[2]; |
---|
[ab63ece] | 197 | int ret = 2; |
---|
[461541a] | 198 | abac_split(tmp, "<-", head_tail, &ret); |
---|
| 199 | if (ret != 2) goto err; |
---|
| 200 | abac_split(head_tail[0], ".", head_tail, &ret); |
---|
| 201 | if (ret != 2) goto err; |
---|
| 202 | char *prin=strdup(head_tail[0]); |
---|
| 203 | free(tmp); |
---|
| 204 | return prin; |
---|
| 205 | |
---|
| 206 | err: |
---|
| 207 | free(tmp); |
---|
| 208 | return NULL; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | int abac_attribute_principal(abac_attribute_t *attr, char *keyid) { |
---|
| 212 | char *copy = _validate_principal(keyid); |
---|
| 213 | if (copy == NULL) |
---|
| 214 | return 0; |
---|
| 215 | |
---|
[ab63ece] | 216 | return abac_attribute_add_tail(attr,copy); |
---|
[461541a] | 217 | } |
---|
| 218 | |
---|
| 219 | int abac_attribute_role(abac_attribute_t *attr, char *keyid, char *role) { |
---|
| 220 | if (!abac_clean_name(role)) |
---|
| 221 | return 0; |
---|
| 222 | |
---|
| 223 | char *copy = _validate_principal(keyid); |
---|
[ab63ece] | 224 | char *newcopy=NULL; |
---|
[461541a] | 225 | if (copy == NULL) |
---|
| 226 | return 0; |
---|
| 227 | |
---|
[3c30b59] | 228 | int rc=asprintf(&newcopy,"%s.%s", copy,role); |
---|
[4e10a0f] | 229 | free(copy); |
---|
[ab63ece] | 230 | return abac_attribute_add_tail(attr, newcopy); |
---|
[461541a] | 231 | } |
---|
| 232 | |
---|
| 233 | int abac_attribute_linking_role(abac_attribute_t *attr, char *keyid, char *role, char *linked) { |
---|
| 234 | if (!abac_clean_name(role) || !abac_clean_name(linked)) |
---|
| 235 | return 0; |
---|
| 236 | |
---|
| 237 | char *copy = _validate_principal(keyid); |
---|
| 238 | if (copy == NULL) |
---|
| 239 | return 0; |
---|
| 240 | |
---|
[4e10a0f] | 241 | char *newcopy=NULL; |
---|
[3c30b59] | 242 | int rc=asprintf(&newcopy,"%s.%s.%s", copy,role,linked); |
---|
[4e10a0f] | 243 | free(copy); |
---|
[ab63ece] | 244 | return abac_attribute_add_tail(attr, newcopy); |
---|
[461541a] | 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | // 0 for fail to bake, 1 is baked okay |
---|
[afcafea] | 250 | int abac_attribute_bake_context(abac_attribute_t *attr, abac_context_t *ctxt) { |
---|
[461541a] | 251 | assert(attr); |
---|
| 252 | assert(attr->head_string); |
---|
[ab63ece] | 253 | assert(attr->tail_strings); |
---|
[afcafea] | 254 | abac_keyid_map_t *km = NULL; |
---|
[461541a] | 255 | |
---|
| 256 | abac_chunk_t id_chunk = { NULL, 0 }; |
---|
| 257 | int ret=abac_id_PEM(attr->issuer_id, &id_chunk); |
---|
| 258 | if(ret != ABAC_CERT_SUCCESS) |
---|
| 259 | return 0; |
---|
| 260 | |
---|
[afcafea] | 261 | if ( ctxt && (km = abac_context_get_keyid_map(ctxt))) { |
---|
| 262 | if (attr->keymap) abac_keyid_map_free(attr->keymap); |
---|
| 263 | attr->keymap = abac_keyid_map_dup(km); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /* Make an new GENI abac credential with the rt0 rule that expires secs |
---|
| 267 | * from now. cert is the PEM encoded X.509 of the issuer's certificate as |
---|
| 268 | * a string. certlen is the length of cert. Returns the XML. Caller is |
---|
| 269 | * responsible for freeing it. */ |
---|
| 270 | char *attr_cert=make_credential(attr, attr->validity, |
---|
| 271 | (char *)id_chunk.ptr, id_chunk.len); |
---|
[461541a] | 272 | |
---|
[f2622ee] | 273 | /*MEI, free id_chunk */ |
---|
| 274 | abac_chunk_free(id_chunk); |
---|
| 275 | |
---|
[461541a] | 276 | if (attr_cert == NULL) |
---|
| 277 | return 0; |
---|
| 278 | |
---|
[8164e70] | 279 | attr->cert=abac_chunk_new(attr_cert,strlen(attr_cert)); |
---|
[461541a] | 280 | |
---|
| 281 | return 1; |
---|
| 282 | } |
---|
[afcafea] | 283 | // 0 for fail to bake, 1 is baked okay |
---|
| 284 | int abac_attribute_bake(abac_attribute_t *attr) { |
---|
| 285 | return abac_attribute_bake_context(attr, NULL); |
---|
| 286 | } |
---|
[461541a] | 287 | |
---|
[f2622ee] | 288 | /* |
---|
| 289 | * caller is responsible to free up the chunk after use |
---|
| 290 | */ |
---|
[4721618] | 291 | abac_chunk_t abac_attribute_cert_chunk(abac_attribute_t *attr) { |
---|
| 292 | abac_chunk_t chunk= {NULL,0}; |
---|
[461541a] | 293 | |
---|
[8164e70] | 294 | if (abac_chunk_null(attr->cert)) |
---|
[4721618] | 295 | return chunk; |
---|
[461541a] | 296 | |
---|
| 297 | /* return the xml chunk */ |
---|
[8164e70] | 298 | chunk= abac_chunk_dup(attr->cert.ptr, attr->cert.len); |
---|
[4721618] | 299 | return chunk; |
---|
[461541a] | 300 | } |
---|
| 301 | |
---|
| 302 | int abac_attribute_baked(abac_attribute_t *attr) { |
---|
| 303 | return (attr->cert.ptr != NULL); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | |
---|
[d2b198c] | 307 | static abac_attribute_t *_load_attr(abac_list_t *id_certs,char *rstring, char *xml, abac_keyid_map_t *km) |
---|
[461541a] | 308 | { |
---|
[02036f4] | 309 | /* make a copy of rle_string */ |
---|
| 310 | char *role_string=abac_xstrdup(rstring); |
---|
[461541a] | 311 | |
---|
| 312 | char *head_tail[2]; |
---|
[ab63ece] | 313 | char *role_rest[2]; |
---|
| 314 | int ret = 2; |
---|
[461541a] | 315 | abac_split(role_string, "<-", head_tail, &ret); |
---|
| 316 | if (ret != 2) return NULL; |
---|
| 317 | |
---|
| 318 | char *keyid=get_keyid_from_xml(xml); |
---|
[bec30b5] | 319 | abac_id_t *issuer_id=abac_verifier_lookup_id(id_certs,keyid); |
---|
[461541a] | 320 | |
---|
| 321 | long validity=get_validity_from_xml(xml); |
---|
| 322 | |
---|
| 323 | abac_attribute_t *attr = abac_xmalloc(sizeof(abac_attribute_t)); |
---|
| 324 | if(issuer_id) |
---|
| 325 | attr->issuer_id = abac_id_dup(issuer_id); |
---|
[b7e77df] | 326 | else attr->issuer_id=NULL; |
---|
[461541a] | 327 | attr->validity = validity; |
---|
[b7e77df] | 328 | attr->ntails = 0; |
---|
| 329 | attr->tail_strings = NULL; |
---|
[461541a] | 330 | |
---|
[d2b198c] | 331 | /* If there is a keymap, make a reference to it. */ |
---|
| 332 | if ( km ) attr->keymap = abac_keyid_map_dup(km); |
---|
| 333 | else attr->keymap = NULL; |
---|
| 334 | |
---|
| 335 | |
---|
[461541a] | 336 | attr->head_string = abac_xstrdup(head_tail[0]); |
---|
[ab63ece] | 337 | do { |
---|
| 338 | ret = 2; |
---|
| 339 | abac_split(head_tail[1], " & ", role_rest, &ret); |
---|
| 340 | abac_attribute_add_tail(attr, abac_xstrdup(role_rest[0])); |
---|
| 341 | head_tail[1] =role_rest[1]; |
---|
| 342 | } while (ret == 2); |
---|
| 343 | |
---|
[461541a] | 344 | char *tmp=strstr(attr->head_string,"."); |
---|
| 345 | attr->role =abac_xstrdup(tmp+1); |
---|
| 346 | |
---|
[8164e70] | 347 | attr->cert=abac_chunk_dup(xml,strlen(xml)); |
---|
| 348 | |
---|
[bc12f3d] | 349 | attr->output_format = NULL; |
---|
[461541a] | 350 | |
---|
[f2622ee] | 351 | free(keyid); |
---|
| 352 | free(role_string); |
---|
[461541a] | 353 | return attr; |
---|
| 354 | } |
---|
| 355 | |
---|
[bec30b5] | 356 | abac_list_t *abac_attribute_certs_from_file(abac_list_t *id_certs,char *filename) |
---|
[461541a] | 357 | { |
---|
[ef68ac9] | 358 | libabac_init(); |
---|
[461541a] | 359 | abac_list_t *alist=abac_list_new(); |
---|
| 360 | char *xml=NULL; |
---|
| 361 | char *rt0=NULL; |
---|
[d2b198c] | 362 | abac_keyid_map_t *km = abac_keyid_map_new(); |
---|
[461541a] | 363 | |
---|
[d2b198c] | 364 | char **rt0s=read_credential((void *)id_certs,filename, &xml, km); |
---|
[4571abc] | 365 | if(rt0s == NULL) { |
---|
| 366 | abac_keyid_map_free(km); |
---|
| 367 | return alist; |
---|
| 368 | } |
---|
| 369 | if(xml == NULL || strlen(xml)==0) { |
---|
| 370 | abac_keyid_map_free(km); |
---|
| 371 | return alist; |
---|
| 372 | } |
---|
[461541a] | 373 | |
---|
| 374 | abac_attribute_t *attr; |
---|
| 375 | |
---|
| 376 | int i=0; |
---|
| 377 | do { |
---|
| 378 | rt0 = rt0s[i]; |
---|
| 379 | if(rt0 == NULL) break; |
---|
[d2b198c] | 380 | attr=_load_attr(id_certs,rt0, xml, km); |
---|
[461541a] | 381 | if(attr) |
---|
| 382 | abac_list_add(alist, attr); |
---|
| 383 | free(rt0); |
---|
| 384 | i++; |
---|
| 385 | } while (rt0s[i] !=NULL); |
---|
[d2b198c] | 386 | abac_keyid_map_free(km); |
---|
[461541a] | 387 | |
---|
| 388 | free(rt0s); |
---|
| 389 | free(xml); |
---|
| 390 | |
---|
| 391 | return alist; |
---|
| 392 | } |
---|
| 393 | |
---|
[bec30b5] | 394 | abac_list_t *abac_attribute_certs_from_chunk(abac_list_t *id_certs,abac_chunk_t chunk) { |
---|
[ef68ac9] | 395 | libabac_init(); |
---|
[461541a] | 396 | |
---|
| 397 | abac_list_t *alist=abac_list_new(); |
---|
| 398 | char *xml=(char *)chunk.ptr; |
---|
[d2b198c] | 399 | abac_keyid_map_t *km = abac_keyid_map_new(); |
---|
| 400 | |
---|
[4721618] | 401 | if(chunk.len==0) return alist; |
---|
| 402 | |
---|
[3613ab8] | 403 | char **rt0s=get_rt0_from_xml((void *) id_certs, xml, km); |
---|
[461541a] | 404 | char *rt0=NULL; |
---|
[4571abc] | 405 | if(rt0s==NULL) { |
---|
| 406 | abac_keyid_map_free(km); |
---|
| 407 | return alist; |
---|
| 408 | } |
---|
[461541a] | 409 | |
---|
| 410 | abac_attribute_t *attr; |
---|
| 411 | int i=0; |
---|
| 412 | do { |
---|
| 413 | rt0 = rt0s[i]; |
---|
| 414 | if(rt0 == NULL) break; |
---|
[d2b198c] | 415 | attr=_load_attr(id_certs,rt0, xml, km); |
---|
[461541a] | 416 | if(attr) |
---|
| 417 | abac_list_add(alist, attr); |
---|
| 418 | free(rt0); |
---|
| 419 | i++; |
---|
| 420 | } while (rt0s[i] !=NULL); |
---|
[d2b198c] | 421 | abac_keyid_map_free(km); |
---|
[461541a] | 422 | |
---|
| 423 | free(rt0s); |
---|
| 424 | return alist; |
---|
| 425 | } |
---|
| 426 | |
---|
[4721618] | 427 | // returns ABAC_FAILURE if the cert hasn't been baked |
---|
[461541a] | 428 | int abac_attribute_write(abac_attribute_t *attr, FILE *out) { |
---|
| 429 | assert(attr != NULL); |
---|
| 430 | |
---|
[8164e70] | 431 | if (abac_chunk_null(attr->cert)) |
---|
[4721618] | 432 | return ABAC_FAILURE; |
---|
[461541a] | 433 | |
---|
| 434 | // write to file |
---|
| 435 | fwrite(attr->cert.ptr, attr->cert.len, 1, out); |
---|
| 436 | |
---|
[4721618] | 437 | return ABAC_SUCCESS; |
---|
[461541a] | 438 | } |
---|
| 439 | |
---|
[4721618] | 440 | // returns ABAC_FAILURE if the cert hasn't been baked |
---|
| 441 | int abac_attribute_write_file(abac_attribute_t *attr, const char *fname) { |
---|
[8164e70] | 442 | if (abac_chunk_null(attr->cert)) |
---|
[4721618] | 443 | return ABAC_FAILURE; |
---|
| 444 | |
---|
| 445 | FILE *fp=fopen(fname,"w+"); |
---|
| 446 | if(fp) { |
---|
| 447 | // write to file |
---|
| 448 | fwrite(attr->cert.ptr, attr->cert.len, 1, fp); |
---|
| 449 | } else return ABAC_FAILURE; |
---|
| 450 | fclose(fp); |
---|
| 451 | |
---|
| 452 | return ABAC_SUCCESS; |
---|
| 453 | } |
---|
| 454 | |
---|
[d2b198c] | 455 | /* return a copy of the local name mappings, if any. The returned value is not |
---|
| 456 | * reference counted, so callers will need to call abac_keyid_map_dup on it if |
---|
| 457 | * they need to keep a copy of the pointer. |
---|
| 458 | */ |
---|
| 459 | abac_keyid_map_t *abac_attribute_get_keyid_map(abac_attribute_t *attr) { |
---|
| 460 | return attr->keymap; |
---|
| 461 | } |
---|
| 462 | |
---|
[4721618] | 463 | |
---|
[461541a] | 464 | void abac_attribute_free(abac_attribute_t *attr) { |
---|
| 465 | |
---|
[ab63ece] | 466 | int i = 0; |
---|
[461541a] | 467 | |
---|
| 468 | if (attr == NULL) |
---|
| 469 | return; |
---|
| 470 | |
---|
| 471 | if(attr->issuer_id) abac_id_free(attr->issuer_id); |
---|
| 472 | |
---|
| 473 | free(attr->role); |
---|
| 474 | free(attr->head_string); |
---|
[ab63ece] | 475 | for (i=0; i < attr->ntails; i++) |
---|
| 476 | free(attr->tail_strings[i]); |
---|
| 477 | free(attr->tail_strings); |
---|
[bc12f3d] | 478 | if ( attr->output_format ) |
---|
| 479 | free(attr->output_format); |
---|
[461541a] | 480 | |
---|
[d2b198c] | 481 | if ( attr->keymap ) abac_keyid_map_free(attr->keymap); |
---|
| 482 | |
---|
[8164e70] | 483 | abac_chunk_free(attr->cert); |
---|
[461541a] | 484 | |
---|
| 485 | free(attr); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | // |
---|
| 489 | // Helper functions below |
---|
| 490 | // |
---|
| 491 | |
---|
| 492 | // validate a princpal's name |
---|
| 493 | // makes sure it's a valid SHA1 identifier |
---|
| 494 | // return values: |
---|
| 495 | // success: malloc'd copy with all hex digits lowercase |
---|
| 496 | // fail: NULL |
---|
| 497 | static char *_validate_principal(char *keyid) { |
---|
| 498 | int i; |
---|
| 499 | char *copy = NULL; |
---|
| 500 | |
---|
| 501 | if (strlen(keyid) != SHA1_LENGTH) |
---|
| 502 | return NULL; |
---|
| 503 | |
---|
| 504 | copy = abac_xstrdup(keyid); |
---|
| 505 | for (i = 0; i < SHA1_LENGTH; ++i) { |
---|
| 506 | copy[i] = tolower(copy[i]); |
---|
| 507 | if (!isxdigit(copy[i])) |
---|
| 508 | goto error; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | return copy; |
---|
| 512 | |
---|
| 513 | error: |
---|
| 514 | free(copy); |
---|
| 515 | return NULL; |
---|
| 516 | } |
---|
| 517 | |
---|
[ab63ece] | 518 | static int abac_attribute_role_string_size(abac_attribute_t *attr) { |
---|
| 519 | int sz = 3; /* Start with the end of string character and <-*/ |
---|
| 520 | int i; /* Scratch */ |
---|
| 521 | |
---|
| 522 | if ( !attr) return sz; |
---|
| 523 | if ( attr->head_string) |
---|
| 524 | sz += strlen(attr->head_string); |
---|
| 525 | for (i = 0; i < attr->ntails; i++) |
---|
| 526 | if ( attr->tail_strings[i]) |
---|
| 527 | sz += strlen(attr->tail_strings[i]); |
---|
| 528 | /* " & " between each pair of tails */ |
---|
| 529 | sz += 3 * (attr->ntails-1); |
---|
| 530 | return sz; |
---|
| 531 | } |
---|
| 532 | |
---|
[461541a] | 533 | // combine up the attribute's rule string, explicit copy |
---|
| 534 | char *abac_attribute_role_string(abac_attribute_t *attr) { |
---|
| 535 | assert(attr); |
---|
| 536 | |
---|
[ab63ece] | 537 | int sz = abac_attribute_role_string_size(attr); |
---|
| 538 | char *role_string=abac_xmalloc(sz); |
---|
| 539 | int i; |
---|
| 540 | |
---|
| 541 | if ( !role_string) return NULL; |
---|
[461541a] | 542 | |
---|
[ab63ece] | 543 | sz -= snprintf(role_string, sz, "%s<-", attr->head_string); |
---|
| 544 | for ( i = 0 ; i < attr->ntails; i++ ) { |
---|
| 545 | if ( i > 0 ) { |
---|
| 546 | strncat(role_string, " & ", 3); |
---|
| 547 | sz -= 3; |
---|
| 548 | } |
---|
| 549 | strncat(role_string,attr->tail_strings[i], sz); |
---|
[461541a] | 550 | |
---|
[ab63ece] | 551 | sz -= strlen(attr->tail_strings[i]); |
---|
| 552 | if (sz < 0 ) return NULL; |
---|
| 553 | } |
---|
| 554 | return role_string; |
---|
| 555 | } |
---|