#include "router.h"
typedef struct aci_user_st *aci_user_t;
struct aci_user_st {
char *name;
aci_user_t next;
};
xht aci_load(router_t r) {
xht aci;
int aelem, uelem, attr;
char type[33];
aci_user_t list_head, list_tail, user;
log_debug(ZONE, "loading aci");
aci = xhash_new(51);
if((aelem = nad_find_elem(r->config->nad, 0, -1, "aci", 1)) < 0)
return aci;
aelem = nad_find_elem(r->config->nad, aelem, -1, "acl", 1);
while(aelem >= 0) {
if((attr = nad_find_attr(r->config->nad, aelem, -1, "type", NULL)) < 0) {
aelem = nad_find_elem(r->config->nad, aelem, -1, "acl", 0);
continue;
}
list_head = NULL;
list_tail = NULL;
snprintf(type, 33, "%.*s", NAD_AVAL_L(r->config->nad, attr), NAD_AVAL(r->config->nad, attr));
log_debug(ZONE, "building list for '%s'", type);
uelem = nad_find_elem(r->config->nad, aelem, -1, "user", 1);
while(uelem >= 0) {
if(NAD_CDATA_L(r->config->nad, uelem) > 0) {
user = (aci_user_t) malloc(sizeof(struct aci_user_st));
memset(user, 0, sizeof(struct aci_user_st));
user->name = (char *) malloc(sizeof(char) * (NAD_CDATA_L(r->config->nad, uelem) + 1));
sprintf(user->name, "%.*s", NAD_CDATA_L(r->config->nad, uelem), NAD_CDATA(r->config->nad, uelem));
if(list_tail != NULL) {
list_tail->next = user;
list_tail = user;
}
if(list_head == NULL) {
list_head = user;
list_tail = user;
}
log_debug(ZONE, "added '%s'", user->name);
}
uelem = nad_find_elem(r->config->nad, uelem, -1, "user", 0);
}
if(list_head != NULL)
xhash_put(aci, pstrdup(xhash_pool(aci), type), (void *) list_head);
aelem = nad_find_elem(r->config->nad, aelem, -1, "acl", 0);
}
return aci;
}
int aci_check(xht aci, char *type, char *name) {
aci_user_t list, scan;
log_debug(ZONE, "checking for '%s' in acl 'all'", name);
list = (aci_user_t) xhash_get(aci, "all");
for(scan = list; scan != NULL; scan = scan->next)
if(strcmp(scan->name, name) == 0)
return 1;
if(type != NULL) {
log_debug(ZONE, "checking for '%s' in acl '%s'", name, type);
list = (aci_user_t) xhash_get(aci, type);
for(scan = list; scan != NULL; scan = scan->next)
if(strcmp(scan->name, name) == 0)
return 1;
}
return 0;
}
void aci_unload(xht aci) {
aci_user_t list, user;
char *aclname;
if(xhash_iter_first(aci))
do {
xhash_iter_get(aci, (const char **) &aclname, (void *) &list);
while (list != NULL) {
user = list;
list = list->next;
free(user->name);
free(user);
}
} while(xhash_iter_next(aci));
xhash_free(aci);
return;
}