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