#include <freeradius-devel/ident.h>
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include "rlm_policy.h"
static const CONF_PARSER module_config[] = {
{ "filename", PW_TYPE_FILENAME,
offsetof(rlm_policy_t,filename), NULL, "${confdir}/policy.txt"},
{ NULL, -1, 0, NULL, NULL }
};
static int policyname_cmp(const void *a, const void *b)
{
return strcmp(((const policy_named_t *)a)->name,
((const policy_named_t *)b)->name);
}
static int policy_detach(void *instance)
{
rlm_policy_t *inst = instance;
if (inst->policies) rbtree_free(inst->policies);
free(instance);
return 0;
}
static int policy_instantiate(CONF_SECTION *conf, void **instance)
{
rlm_policy_t *inst;
inst = rad_malloc(sizeof(*inst));
if (!inst) {
return -1;
}
memset(inst, 0, sizeof(*inst));
if (cf_section_parse(conf, inst, module_config) < 0) {
policy_detach(inst);
return -1;
}
inst->policies = rbtree_create(policyname_cmp, rlm_policy_free_item, 0);
if (!inst->policies) {
policy_detach(inst);
return -1;
}
if (!rlm_policy_parse(inst->policies, inst->filename)) {
policy_detach(inst);
return -1;
}
*instance = inst;
return 0;
}
int rlm_policy_insert(rbtree_t *head, policy_named_t *policy)
{
if (!rbtree_insert(head, policy)) {
return 0;
}
return 1;
}
policy_named_t *rlm_policy_find(rbtree_t *head, const char *name)
{
policy_named_t mypolicy;
mypolicy.name = name;
return rbtree_finddata(head, &mypolicy);
}
static int policy_authorize(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"authorize");
}
static int policy_preacct(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"preacct");
}
static int policy_accounting(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"accounting");
}
static int policy_post_auth(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"post-auth");
}
static int policy_pre_proxy(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"pre-proxy");
}
static int policy_post_proxy(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"post-proxy");
}
#ifdef WITH_COA
static int policy_recv_coa(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"recv-coa");
}
static int policy_send_coa(void *instance, REQUEST *request)
{
return rlm_policy_evaluate((rlm_policy_t *) instance, request,
"send-coa");
}
#endif
void rlm_policy_free_item(policy_item_t *item)
{
while (item) {
policy_item_t *next = item->next;
switch (item->type) {
default:
case POLICY_TYPE_BAD:
break;
case POLICY_TYPE_ASSIGNMENT:
{
policy_assignment_t *this;
this = (policy_assignment_t *) item;
if (this->lhs) free(this->lhs);
if (this->rhs) free(this->rhs);
}
break;
case POLICY_TYPE_CONDITIONAL:
{
policy_condition_t *this;
this = (policy_condition_t *) item;
if (this->lhs) free(this->lhs);
if (this->rhs) free(this->rhs);
if (this->child) {
rlm_policy_free_item(this->child);
this->child = NULL;
}
}
break;
case POLICY_TYPE_IF:
{
policy_if_t *this;
this = (policy_if_t *) item;
if (this->condition) {
rlm_policy_free_item(this->condition);
this->condition = NULL;
}
if (this->if_true) {
rlm_policy_free_item(this->if_true);
this->if_true = NULL;
}
if (this->if_false) {
rlm_policy_free_item(this->if_false);
this->if_false = NULL;
}
}
break;
case POLICY_TYPE_ATTRIBUTE_LIST:
{
policy_attributes_t *this;
this = (policy_attributes_t *) item;
rlm_policy_free_item(this->attributes);
}
break;
case POLICY_TYPE_NAMED_POLICY:
{
policy_named_t *this;
this = (policy_named_t *) item;
rad_assert(this->name != NULL);
free(this->name);
rlm_policy_free_item(this->policy);
}
break;
case POLICY_TYPE_CALL:
{
policy_call_t *this;
this = (policy_call_t *) item;
if (this->name) free(this->name);
}
break;
case POLICY_TYPE_RETURN:
break;
case POLICY_TYPE_MODULE:
{
policy_module_t *this;
this = (policy_module_t *) item;
if (this->cs) cf_section_free(&this->cs);
if (this->mc) modcallable_free(&this->mc);
}
break;
}
item->next = NULL;
item->type = POLICY_TYPE_BAD;
free(item);
item = next;
}
}
module_t rlm_policy = {
RLM_MODULE_INIT,
"policy",
RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,
policy_instantiate,
policy_detach,
{
NULL,
policy_authorize,
policy_preacct,
policy_accounting,
NULL,
policy_pre_proxy,
policy_post_proxy,
policy_post_auth
#ifdef WITH_COA
, policy_recv_coa,
policy_send_coa
#endif
},
};