/*********************************************************** abac_param.c dterm=data term of rt1 param is a kind of dterm ***********************************************************/ #include #include #include #include #include #include "abac_list.h" #include "abac_util.h" #include "abac_pl.h" typedef enum _termtype { e_TERM_INTEGER = 1, e_TERM_URN = 2, e_TERM_FLOAT = 3, e_TERM_BOOLEAN = 4, e_TERM_STRING = 5, e_TERM_TIME = 6, e_TERM_PRINCIPAL = 7, e_TERM_ANONYMOUS = 8 } termtype_t; /* termname[dtermtype] */ static char *const termname[] = { "badterm", "integer", "urn", "float", "boolean", "string", "time" , "principal", "anonymous", 0 }; static int termname_cnt=8; typedef enum _condtype_t { e_COND_ROLE = 1, e_COND_OSET = 2, e_COND_RANGE = 3, } condtype_t; /* string will contain either the range literal (static constraint) or the oset/role prologized string (dynamic constraint) union ptr will only be set if it is a dynamic constraint */ struct _abac_condition_t { int type; char *string; union { abac_role_t *of_role; abac_oset_t *of_oset; }; }; struct _abac_term_t { int type; char *name; abac_condition_t *constraint; }; /* new, free, add one */ struct _abac_param_list_t { abac_list_t *list; }; /******************************************************************/ static abac_condition_t *abac_condition_new(int type,char* condstr,void *cptr) { abac_condition_t *ptr= (abac_condition_t *)abac_xmalloc(sizeof(abac_condition_t)); ptr->type=type; ptr->string=strdup(condstr); ptr->of_role=cptr; return ptr; } static void abac_condition_free(abac_condition_t *ptr) { switch(ptr->type) { case e_COND_OSET: abac_oset_free(ptr->of_oset); break; case e_COND_ROLE: abac_role_free(ptr->of_role); break; case e_COND_RANGE: /* do nothing */ break; } if(ptr->string) free(ptr->string); free(ptr); } /* made a copy */ char *abac_condition_string(abac_condition_t *ptr) { char *string=NULL; switch(ptr->type) { case e_COND_OSET: string=abac_oset_string_with_condition(ptr->of_oset); break; case e_COND_ROLE: string=abac_role_string_with_condition(ptr->of_role); break; case e_COND_RANGE: string=strdup(ptr->string); break; } return string; } /******************************************************************/ char *abac_term_name(abac_term_t *term) { return term->name; } abac_condition_t *abac_term_constraint(abac_term_t *term) { return term->constraint; } char *abac_term_type(int i) { return termname[i]; } int abac_term_is_time_type(int i) { if (i == e_TERM_TIME) return 1; return 0; } int abac_term_is_string_type(int i) { if (i == e_TERM_STRING) return 1; return 0; } int abac_term_is_integer_type(int i) { if (i == e_TERM_INTEGER) return 1; return 0; } int abac_term_is_urn_type(int i) { if (i == e_TERM_URN) return 1; return 0; } int abac_term_verify_term_type(char *type) { int i; if (type == NULL) return 0; for (i = 1; i <= termname_cnt ; i++) if(strcmp(type,termname[i])==0) return i; return 0; } /* always add p to the name except if its a variable and needed */ abac_term_t *abac_term_new(int type, char *name, char *cond, void *cptr) { abac_condition_t *constraint=NULL; if (cond) { if(type==e_TERM_PRINCIPAL) { constraint=abac_condition_new(e_COND_ROLE,cond,cptr); } else { if(cptr == NULL) constraint=abac_condition_new(e_COND_RANGE,cond,NULL); else constraint=abac_condition_new(e_COND_OSET,cond,cptr); } } abac_term_t *ptr=abac_xmalloc(sizeof(abac_term_t)); ptr->type=type; /* add p to time term if needed and not a variable */ if((!constraint) && abac_term_is_time_type(type) && isdigit(name[0])) { char *tmp; asprintf(&tmp,"p%s",name); ptr->name=tmp; free(name); } else { ptr->name=strdup(name); } ptr->constraint=constraint; return ptr; } void abac_term_free(abac_term_t *ptr) { if(ptr->constraint) { abac_condition_free(ptr->constraint); } free(ptr->name); free(ptr); } abac_term_t *abac_term_add_constraint(abac_term_t *ptr, abac_condition_t *cond) { if(ptr->constraint) { abac_condition_free(ptr->constraint); } ptr->constraint = cond; return ptr; } /* name or name:cond */ static char *abac_term_string_with_condition(abac_term_t *ptr) { char *tmp=NULL; char *cond=NULL; char *name=ptr->name; int len=strlen(name)+1; if(ptr->constraint) { cond=abac_condition_string(ptr->constraint); len=len+strlen(cond)+1; } tmp = abac_xmalloc(len); if(cond) { sprintf(tmp,"%s:%s", name, cond); free(cond); } else sprintf(tmp,"%s", name); return tmp; } /* len of name or name:cond */ static int abac_term_len_with_condition(abac_term_t *ptr) { char *cond=NULL; char *name=ptr->name; int len=strlen(name); if(ptr->constraint) { cond=abac_condition_string(ptr->constraint); len=len+strlen(cond)+1; free(cond); } return len; } /* just name */ static char *abac_term_string(abac_term_t *ptr) { if(ptr->type == abac_term_verify_term_type("urn") || ptr->type == abac_term_verify_term_type("string") ) { char *str; asprintf(&str, "%s", ptr->name); return str; } return strdup(ptr->name); } /* just len of name */ static int abac_term_len(abac_term_t *ptr) { /* including the 2 ' */ if(ptr->type == abac_term_verify_term_type("urn") || ptr->type == abac_term_verify_term_type("string") ) { return strlen(ptr->name)+2; } return strlen(ptr->name); } /********************************************************************/ abac_param_list_t *abac_param_list_new(abac_term_t *term) { abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t)); ptr->list=abac_list_new(); abac_list_add(ptr->list, term); return ptr; } abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr) { abac_list_t *list=ptr->list; abac_term_t *cur; abac_list_foreach(list, cur, abac_term_free(cur); ); abac_list_free(list); free(ptr); } abac_param_list_t *abac_param_list_add_term(abac_param_list_t *ptr, abac_term_t *term) { abac_list_t *list=ptr->list; abac_list_add(list, term); return ptr; } /* param1:cond1,param2:cond2 */ char* abac_param_list_string_with_condition(abac_param_list_t *ptr) { if(ptr->list == NULL) return ""; abac_term_t *cur; char *tmp; int len=0; /* collect up all the lens so can allocate once */ abac_list_foreach(ptr->list, cur, int l=abac_term_len_with_condition(cur); len=len+l+1; ); tmp=abac_xmalloc((len+1) * sizeof(char)); /* collect up all the string */ int first=1; abac_list_foreach(ptr->list, cur, char *s=abac_term_string_with_condition(cur); if(first) { strcpy(tmp,s); first=0; } else { strcat(tmp,","); strcat(tmp,s); } free(s); ); return tmp; } /* param1,param2 */ char* abac_param_list_string(abac_param_list_t *ptr) { abac_list_t *list=ptr->list; if(list == NULL) return ""; abac_term_t *cur; char *tmp; int len=0; /* collect up all the lens so can allocate once */ abac_list_foreach(list, cur, // collect up the len int l=abac_term_len(cur); len=len+l+1; ); tmp=abac_xmalloc((len+1) * sizeof(char)); /* collect up all the string */ int first=1; abac_list_foreach(list, cur, char *s=abac_term_string(cur); if(first) { strcpy(tmp,s); first=0; } else { strcat(tmp,","); strcat(tmp,s); } free(s); ); return tmp; }