2 * evaluate.c Evaluate a policy language
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2004 Alan DeKok <aland@ox.org>
21 * Copyright 2006 The FreeRADIUS server project
24 #include <freeradius-devel/ident.h>
27 #include "rlm_policy.h"
33 #define debug_evaluate if (0) printf
36 * Print stuff we've parsed
38 static void policy_print(const policy_item_t *item, int indent)
41 if (indent) printf("%*s", indent, " ");
49 if (indent) printf("%*s", indent, " ");
50 printf("[BAD STATEMENT]");
53 case POLICY_TYPE_PRINT:
54 if (indent) printf("%*s", indent, " ");
56 const policy_print_t *this;
58 this = (const policy_print_t *) item;
60 if (this->rhs_type == POLICY_LEX_BARE_WORD) {
61 printf("print %s\n", this->rhs);
63 printf("print \"%s\"\n", this->rhs);
68 case POLICY_TYPE_ASSIGNMENT:
70 const policy_assignment_t *assign;
72 assign = (const policy_assignment_t *) item;
73 if (indent) printf("%*s", indent, " ");
75 printf("\t%s %s ", assign->lhs,
76 fr_int2str(rlm_policy_tokens,
77 assign->assign, "?"));
78 if (assign->rhs_type == POLICY_LEX_BARE_WORD) {
79 printf("%s\n", assign->rhs);
84 printf("\"%s\"\n", assign->rhs);
89 case POLICY_TYPE_CONDITIONAL: /* no indentation here */
91 const policy_condition_t *condition;
93 condition = (const policy_condition_t *) item;
97 if (condition->sense) {
104 if (condition->compare == POLICY_LEX_L_BRACKET) {
105 policy_print(condition->child, indent);
110 if (condition->compare == POLICY_LEX_L_NOT) {
112 policy_print(condition->child, indent);
117 if (condition->compare == POLICY_LEX_CMP_TRUE) {
118 printf("%s)", condition->lhs);
122 if (condition->lhs_type == POLICY_LEX_FUNCTION) {
123 printf("%s()", condition->lhs);
127 * and move all of this logic
130 printf("\"%s\"", condition->lhs);
134 * We always print this condition.
136 printf(" %s ", fr_int2str(rlm_policy_tokens,
139 if (condition->rhs_type == POLICY_LEX_BARE_WORD) {
140 printf("%s", condition->rhs);
144 * and move all of this logic
147 printf("\"%s\"", condition->rhs);
151 if ((condition->child_condition != POLICY_LEX_BAD) &&
152 (condition->child_condition != POLICY_LEX_BARE_WORD)) {
153 printf(" %s ", fr_int2str(rlm_policy_tokens, condition->child_condition, "?"));
154 policy_print(condition->child, indent);
161 const policy_if_t *statement;
163 statement = (const policy_if_t *) item;
165 if (indent) printf("%*s", indent, " ");
167 policy_print(statement->condition, indent);
169 policy_print(statement->if_true, indent + 1);
170 if (indent) printf("%*s", indent, " ");
171 if (statement->if_false) {
173 if (statement->if_false->type == POLICY_TYPE_ASSIGNMENT) {
175 policy_print(statement->if_false, indent + 1);
176 if (indent) printf("%*s", indent, " ");
179 policy_print(statement->if_false, indent + 1);
187 case POLICY_TYPE_ATTRIBUTE_LIST:
189 const policy_attributes_t *this;
191 this = (const policy_attributes_t *) item;
193 if (indent) printf("%*s", indent, " ");
195 fr_int2str(policy_reserved_words,
197 fr_int2str(rlm_policy_tokens,
199 policy_print(this->attributes, indent + 1);
200 if (indent) printf("%*s", indent, " ");
205 case POLICY_TYPE_NAMED_POLICY:
207 const policy_named_t *this;
209 this = (const policy_named_t *) item;
210 if (indent) printf("%*s", indent, " ");
211 printf("policy %s {\n", this->name);
212 policy_print(this->policy, indent + 1);
213 if (indent) printf("%*s", indent, " ");
218 case POLICY_TYPE_CALL:
220 const policy_call_t *this;
222 this = (const policy_call_t *) item;
223 if (indent) printf("%*s", indent, " ");
224 printf("call %s\n", this->name);
228 case POLICY_TYPE_RETURN:
230 const policy_return_t *this;
232 this = (const policy_return_t *) item;
233 if (indent) printf("%*s", indent, " ");
234 printf("return %s\n",
235 fr_int2str(policy_return_codes,
236 this->rcode, "???"));
240 case POLICY_TYPE_MODULE:
242 const policy_module_t *this;
244 this = (const policy_module_t *) item;
245 if (indent) printf("%*s", indent, " ");
246 printf("module %s <stuff>\n",
247 fr_int2str(policy_component_names,
248 this->component, "???"));
253 if (indent) printf("%*s", indent, " ");
264 void rlm_policy_print(const policy_item_t *item)
266 printf("# rlm_policy \n");
267 policy_print(item, 0);
271 * Internal stack of things to do. This lets us have function
274 * Yes, we should learn lex, yacc, etc.
276 #define POLICY_MAX_STACK 16
277 typedef struct policy_state_t {
279 REQUEST *request; /* so it's not passed on the C stack */
280 int rcode; /* for functions, etc. */
281 int component; /* for calling other modules */
283 const policy_item_t *stack[POLICY_MAX_STACK];
287 static int policy_evaluate_name(policy_state_t *state, const char *name);
290 * Push an item onto the state.
292 static int policy_stack_push(policy_state_t *state, const policy_item_t *item)
294 rad_assert(state->depth >= 0);
297 * Asked to push nothing. Don't push it.
302 * State is full. Die.
304 if (state->depth >= POLICY_MAX_STACK) {
309 * Walk back up the stack, looking for previous ocurrances
310 * of this name. If found, we have infinite recursion,
311 * which we stop dead in the water!
313 * This isn't strictly necessary right now, as we look up
314 * policies by name when they're first referenced. This
315 * means that ALL references are backwards (to the start
316 * of the file), which means that there are no circular
319 if (item->type == POLICY_TYPE_NAMED_POLICY) {
322 for (i = 0; i < state->depth; i++) {
324 * Check for circular references, by seeing
325 * if the function is already on the stack.
327 * Hmmm... do we want to do this for any type?
329 if (state->stack[i] == item) {
330 debug_evaluate("Circular call to policy %s\n",
331 ((const policy_named_t *) item)->name);
337 debug_evaluate("push %d %p\n", state->depth, item);
339 state->stack[state->depth] = item;
340 state->depth++; /* points to unused entry */
347 * Pop an item from the state.
349 static int policy_stack_pop(policy_state_t *state, const policy_item_t **pitem)
351 rad_assert(pitem != NULL);
352 rad_assert(state->depth >= 0);
355 if (state->depth == 0) {
360 *pitem = state->stack[state->depth - 1];
363 * Named policies are on the stack for catching recursion.
365 if ((*pitem)->type == POLICY_TYPE_NAMED_POLICY) {
371 * Process the whole item list.
373 if ((*pitem)->next) {
374 state->stack[state->depth - 1] = (*pitem)->next;
375 debug_evaluate("pop/push %d %p\n", state->depth - 1, *pitem);
377 state->depth--; /* points to unused entry */
378 debug_evaluate("pop %d %p\n", state->depth, *pitem);
386 * Evaluate a print statement
388 static int evaluate_print(policy_state_t *state, const policy_item_t *item)
390 const policy_print_t *this;
392 this = (const policy_print_t *) item;
394 if (this->rhs_type == POLICY_LEX_BARE_WORD) {
395 printf("%s\n", this->rhs);
399 radius_xlat(buffer, sizeof(buffer), this->rhs,
400 state->request, NULL);
401 printf("%s", buffer);
402 if (!strchr(buffer, '\n')) printf("\n");
406 * Doesn't change state->rcode
413 * Return a VALUE_PAIR, given an attribute name.
415 * FIXME: Have it return the N'th one, too, like
418 * The amount of duplicated code is getting annoying...
420 static VALUE_PAIR *find_vp(REQUEST *request, const char *name)
423 const DICT_ATTR *dattr;
427 vps = request->packet->vps;;
430 * FIXME: use names from reserved word list?
432 if (strncasecmp(name, "request:", 8) == 0) {
434 } else if (strncasecmp(name, "reply:", 6) == 0) {
436 vps = request->reply->vps;
437 } else if (strncasecmp(name, "proxy-request:", 14) == 0) {
439 if (request->proxy) {
440 vps = request->proxy->vps;
442 } else if (strncasecmp(name, "proxy-reply:", 12) == 0) {
444 if (request->proxy_reply) {
445 vps = request->proxy_reply->vps;
447 } else if (strncasecmp(name, "control:", 8) == 0) {
449 vps = request->config_items;
450 } /* else it must be a bare attribute name */
456 dattr = dict_attrbyname(p);
458 fprintf(stderr, "No such attribute %s\n", p);
459 return NULL; /* no such attribute */
462 return pairfind(vps, dattr->attr);
467 * Evaluate an assignment
469 * Not really used much...
471 static int evaluate_assignment(UNUSED policy_state_t *state, const policy_item_t *item)
473 const policy_assignment_t *this;
475 const DICT_ATTR *dattr;
478 this = (const policy_assignment_t *) item;
480 rad_assert(this->lhs != NULL);
481 rad_assert(this->rhs != NULL);
484 dattr = dict_attrbyname(this->lhs);
486 fprintf(stderr, "HUH?\n");
496 * Evaluate a condition
498 static int evaluate_condition(policy_state_t *state, const policy_item_t *item)
501 const policy_condition_t *this;
502 VALUE_PAIR *vp = NULL;
503 const char *data = NULL;
509 char lhs_buffer[2048];
511 this = (const policy_condition_t *) item;
515 * FIXME: Don't always do this...
517 if (this->compare != POLICY_LEX_L_BRACKET) {
518 if (this->lhs_type == POLICY_LEX_FUNCTION) {
520 * We can't call evaluate_call here,
521 * because that just pushes stuff onto
522 * the stack, and we want to actually
523 * evaluate all of it...
525 rcode = policy_evaluate_name(state, this->lhs);
526 data = fr_int2str(policy_return_codes, rcode, "???");
527 strlcpy(lhs_buffer, data, sizeof(lhs_buffer)); /* FIXME: yuck */
528 } else if (this->lhs_type == POLICY_LEX_DOUBLE_QUOTED_STRING) {
529 if (radius_xlat(lhs_buffer, sizeof(lhs_buffer), this->lhs,
530 state->request, NULL) > 0) {
536 switch (this->compare) {
537 case POLICY_LEX_L_BRACKET: /* nested brackets are a special case */
538 rcode = evaluate_condition(state, this->child);
541 case POLICY_LEX_L_NOT:
542 rcode = evaluate_condition(state, this->child);
543 rcode = (rcode == FALSE); /* reverse sense of test */
546 case POLICY_LEX_CMP_FALSE: /* non-existence */
547 if (this->lhs_type == POLICY_LEX_BARE_WORD) {
548 vp = find_vp(state->request, this->lhs);
549 rcode = (vp == NULL);
551 rcode = (data == NULL);
555 case POLICY_LEX_CMP_TRUE: /* existence */
556 if (this->lhs_type == POLICY_LEX_BARE_WORD) {
557 vp = find_vp(state->request, this->lhs);
558 rcode = (vp != NULL);
560 rcode = (data != NULL);
564 default: /* process other comparisons */
565 if ((this->compare != POLICY_LEX_CMP_EQUALS) &&
567 (this->compare != POLICY_LEX_RX_EQUALS) &&
568 (this->compare != POLICY_LEX_RX_NOT_EQUALS) &&
570 (this->compare != POLICY_LEX_LT) &&
571 (this->compare != POLICY_LEX_GT) &&
572 (this->compare != POLICY_LEX_LE) &&
573 (this->compare != POLICY_LEX_GE) &&
574 (this->compare != POLICY_LEX_CMP_NOT_EQUALS)) {
575 fprintf(stderr, "%d: bad comparison\n",
580 if (this->lhs_type == POLICY_LEX_BARE_WORD) {
583 vp = find_vp(state->request, this->lhs);
586 * A op B is FALSE if A doesn't
595 * FIXME: Move sanity checks to
596 * post-parse code, so we don't do
597 * it on every packet.
599 vp_prints_value(buffer, sizeof(buffer), vp, 0);
600 myvp = pairmake(vp->name, this->rhs, T_OP_EQ);
601 if (!myvp) return FALSE; /* memory failure */
605 * FIXME: What to do about comparisons
606 * where vp doesn't exist? Right now,
607 * "simplepaircmp" returns -1, which is
608 * probably a bad idea. it should
609 * instead take an operator, a pointer to
610 * the comparison result, and return
611 * "true/false" for "comparions
612 * succeeded/failed", which are different
613 * error codes than "comparison is less
614 * than, equal to, or greater than zero".
616 compare = radius_callback_compare(state->request,
617 vp, myvp, NULL, NULL);
622 * FIXME: Do something for RHS type?
624 printf("CMP %s %s\n", lhs_buffer, this->rhs);
625 compare = strcmp(lhs_buffer, this->rhs);
628 debug_evaluate("CONDITION COMPARE %d\n", compare);
630 switch (this->compare) {
631 case POLICY_LEX_CMP_EQUALS:
632 rcode = (compare == 0);
635 case POLICY_LEX_CMP_NOT_EQUALS:
636 rcode = (compare != 0);
640 rcode = (compare < 0);
644 rcode = (compare > 0);
648 rcode =(compare <= 0);
652 rcode = (compare >= 0);
656 case POLICY_LEX_RX_EQUALS:
657 { /* FIXME: copied from src/main/valuepair.c */
659 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
662 * Include substring matches.
664 if (regcomp(®, this->rhs,
665 REG_EXTENDED) != 0) {
666 /* FIXME: print error */
669 rad_assert(data != NULL);
670 rcode = regexec(®, data,
671 REQUEST_MAX_REGEX + 1,
673 rcode = (rcode == 0);
677 * Add %{0}, %{1}, etc.
679 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
684 * Didn't match: delete old
685 * match, if it existed.
688 (rxmatch[i].rm_so == -1)) {
689 p = request_data_get(state->request, state->request,
690 REQUEST_DATA_REGEX | i);
704 * Copy substring into buffer.
707 data + rxmatch[i].rm_so,
708 rxmatch[i].rm_eo - rxmatch[i].rm_so);
709 rxbuffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
712 * Copy substring, and add it to
715 * Note that we don't check
716 * for out of memory, which is
717 * the only error we can get...
719 p = strdup(rxbuffer);
720 request_data_add(state->request,
722 REQUEST_DATA_REGEX | i,
729 case POLICY_LEX_RX_NOT_EQUALS:
730 regcomp(®, this->rhs, REG_EXTENDED|REG_NOSUB);
731 rad_assert(data != NULL);
732 rcode = regexec(®, data,
734 rcode = (rcode != 0);
737 #endif /* HAVE_REGEX_H */
741 } /* switch over comparison operators */
742 break; /* default from first switch over compare */
745 if (this->sense) rcode = (rcode == FALSE); /* reverse sense of test */
750 switch (this->child_condition) {
754 case POLICY_LEX_L_AND:
755 if (!rcode) return rcode; /* FALSE && x == FALSE */
758 case POLICY_LEX_L_OR:
759 if (rcode) return rcode; /* TRUE && x == TRUE */
766 this = (const policy_condition_t *) this->child;
769 return 1; /* should never reach here */
774 * Evaluate an 'if' statement
776 static int evaluate_if(policy_state_t *state, const policy_item_t *item)
779 const policy_if_t *this;
781 this = (const policy_if_t *) item;
784 * evaluate_condition calls itself recursively.
785 * We should probably allocate a new state, instead.
787 rcode = evaluate_condition(state, this->condition);
788 debug_evaluate("IF condition returned %s\n",
789 rcode ? "true" : "false");
791 rcode = policy_stack_push(state, this->if_true);
792 if (!rcode) return rcode;
793 } else if (this->if_false) {
794 rcode = policy_stack_push(state, this->if_false);
795 if (!rcode) return rcode;
799 * 'if' can fail, if the block it's processing fails.
806 * Make a VALUE_PAIR from a policy_assignment_t*
808 * The assignment operator has to be '='.
810 static VALUE_PAIR *assign2vp(REQUEST *request,
811 const policy_assignment_t *assign)
814 FR_TOKEN operator = T_OP_EQ;
815 const char *value = assign->rhs;
818 if ((assign->rhs_type == POLICY_LEX_DOUBLE_QUOTED_STRING) &&
819 (strchr(assign->rhs, '%') != NULL)) {
820 radius_xlat(buffer, sizeof(buffer), assign->rhs,
826 * This is crappy.. fix it.
828 switch (assign->assign) {
829 case POLICY_LEX_ASSIGN:
833 case POLICY_LEX_SET_EQUALS:
837 case POLICY_LEX_PLUS_EQUALS:
842 fprintf(stderr, "Expected '=' for operator, not '%s' at line %d\n",
843 fr_int2str(rlm_policy_tokens,
844 assign->assign, "?"),
845 assign->item.lineno);
849 vp = pairmake(assign->lhs, value, operator);
851 fprintf(stderr, "SHIT: %s %s\n", value, librad_errstr);
859 * Evaluate a 'packet .= {attrs}' statement
861 static int evaluate_attr_list(policy_state_t *state, const policy_item_t *item)
863 const policy_attributes_t *this;
864 VALUE_PAIR **vps = NULL;
865 VALUE_PAIR *vp, *head, **tail;
866 const policy_item_t *attr;
867 policy_lex_t this_how;
869 this = (const policy_attributes_t *) item;
871 switch (this->where) {
872 case POLICY_RESERVED_CONTROL:
873 vps = &(state->request->config_items);
876 case POLICY_RESERVED_REQUEST:
877 vps = &(state->request->packet->vps);
880 case POLICY_RESERVED_REPLY:
881 vps = &(state->request->reply->vps);
884 case POLICY_RESERVED_PROXY_REQUEST:
885 if (!state->request->proxy) return 0; /* FIXME: print error */
886 vps = &(state->request->proxy->vps);
889 case POLICY_RESERVED_PROXY_REPLY:
890 if (!state->request->proxy_reply) return 0; /* FIXME: print error */
891 vps = &(state->request->proxy_reply->vps);
901 for (attr = this->attributes; attr != NULL; attr = attr->next) {
902 if (attr->type != POLICY_TYPE_ASSIGNMENT) {
903 fprintf(stderr, "bad assignment in attribute list at line %d\n", attr->lineno);
908 vp = assign2vp(state->request, (const policy_assignment_t *) attr);
910 fprintf(stderr, "Failed to allocate VP\n");
918 this_how = this->how;
921 case POLICY_LEX_SET_EQUALS: /* dangerous: removes all previous things! */
926 case POLICY_LEX_AFTER_TAIL_ASSIGN:
927 pairmove(vps, &head);
931 case POLICY_LEX_ASSIGN: /* 'union' */
932 pairmove(vps, &head);
936 case POLICY_LEX_BEFORE_HEAD_ASSIGN:
937 pairmove(&head, vps);
942 case POLICY_LEX_AFTER_TAIL_EQUALS:
943 case POLICY_LEX_CONCAT_EQUALS:
947 case POLICY_LEX_BEFORE_HEAD_EQUALS:
948 pairadd(&head, *vps);
952 case POLICY_LEX_BEFORE_WHERE_EQUALS:
953 case POLICY_LEX_AFTER_WHERE_EQUALS:
954 case POLICY_LEX_BEFORE_WHERE_ASSIGN:
955 case POLICY_LEX_AFTER_WHERE_ASSIGN:
958 VALUE_PAIR *vpprev = NULL, *vpnext = NULL, *lvp;
960 for(lvp = *vps; lvp; vpprev = lvp, lvp = lvp->next) {
963 if (evaluate_condition(state, this->where_loc))
970 case POLICY_LEX_BEFORE_WHERE_EQUALS:
971 case POLICY_LEX_BEFORE_WHERE_ASSIGN:
978 default: /* always reached */
983 case POLICY_LEX_BEFORE_WHERE_EQUALS:
989 case POLICY_LEX_AFTER_WHERE_EQUALS:
992 case POLICY_LEX_BEFORE_WHERE_ASSIGN:
994 pairmove(&lvp, &head);
1000 case POLICY_LEX_AFTER_WHERE_ASSIGN:
1001 pairmove(&lvp, &head);
1004 default:/*never reached*/
1007 for( ; lvp && lvp->next; lvp = lvp->next);
1014 case POLICY_LEX_BEFORE_WHERE_EQUALS:
1015 this_how = POLICY_LEX_BEFORE_HEAD_EQUALS;
1017 case POLICY_LEX_AFTER_WHERE_EQUALS:
1018 this_how = POLICY_LEX_AFTER_TAIL_EQUALS;
1020 case POLICY_LEX_BEFORE_WHERE_ASSIGN:
1021 this_how = POLICY_LEX_BEFORE_HEAD_ASSIGN;
1023 case POLICY_LEX_AFTER_WHERE_ASSIGN:
1024 this_how = POLICY_LEX_AFTER_TAIL_ASSIGN;
1026 default: /*never reached*/
1034 fprintf(stderr, "HUH?\n");
1039 state->rcode = RLM_MODULE_UPDATED; /* we did stuff */
1046 * Evaluate a reference call to a module.
1048 static int evaluate_call(policy_state_t *state, const policy_item_t *item)
1051 const policy_call_t *this;
1052 const policy_named_t *policy;
1054 this = (const policy_call_t *) item;
1056 policy = rlm_policy_find(state->inst->policies, this->name);
1057 if (!policy) return 0; /* not found... */
1059 DEBUG2("rlm_policy: Evaluating policy %s", this->name);
1061 rad_assert(policy->policy->type != POLICY_TYPE_BAD);
1062 rad_assert(policy->policy->type < POLICY_TYPE_NUM_TYPES);
1065 * Push the name of the function onto the stack,
1066 * so that we can catch recursive calls.
1068 * The "pop" function will skip over it when it sees it.
1070 rcode = policy_stack_push(state, (const policy_item_t *) policy);
1076 * Push it onto the stack. Other code will take care of
1079 rcode = policy_stack_push(state, policy->policy);
1089 * Evaluate a return statement
1091 static int evaluate_return(policy_state_t *state, const policy_item_t *item)
1093 const policy_return_t *this;
1095 this = (const policy_return_t *) item;
1096 state->rcode = this->rcode;
1098 return 1; /* we succeeded */
1103 * Evaluate a module statement
1105 static int evaluate_module(policy_state_t *state, const policy_item_t *item)
1107 const policy_module_t *this;
1109 this = (const policy_module_t *) item;
1112 * Just to be paranoid. Maybe we want to loosen this
1113 * restriction in the future?
1115 if (this->component != state->component) {
1116 DEBUG2("rlm_policy: Cannot mix & match components");
1120 DEBUG2("rlm_policy: begin nested call");
1121 state->rcode = modcall(this->component, this->mc, state->request);
1122 DEBUG2("rlm_policy: end nested call");
1124 return 1; /* we succeeded */
1129 * State machine stuff.
1131 typedef int (*policy_evaluate_type_t)(policy_state_t *, const policy_item_t *);
1135 * MUST be kept in sync with policy_type_t
1137 static policy_evaluate_type_t evaluate_functions[POLICY_TYPE_NUM_TYPES] = {
1138 NULL, /* POLICY_TYPE_BAD */
1141 evaluate_assignment,
1144 NULL, /* define a named policy.. */
1152 * Evaluate a policy, keyed by name.
1154 static int policy_evaluate_name(policy_state_t *state, const char *name)
1157 const policy_item_t *this;
1158 policy_named_t mypolicy, *policy;
1160 mypolicy.name = name;
1161 policy = rbtree_finddata(state->inst->policies, &mypolicy);
1162 if (!policy) return RLM_MODULE_FAIL;
1164 DEBUG2("rlm_policy: Evaluating policy %s", name);
1166 rad_assert(policy->item.type != POLICY_TYPE_BAD);
1167 rad_assert(policy->item.type < POLICY_TYPE_NUM_TYPES);
1169 rcode = policy_stack_push(state, policy->policy);
1171 return RLM_MODULE_FAIL;
1175 * FIXME: Look for magic keywords like "return",
1176 * where the packet gets accepted/rejected/whatever
1178 while (policy_stack_pop(state, &this)) {
1179 rad_assert(this != NULL);
1180 rad_assert(this->type != POLICY_TYPE_BAD);
1181 rad_assert(this->type < POLICY_TYPE_NUM_TYPES);
1183 debug_evaluate("Evaluating at line %d\n",
1185 rcode = (*evaluate_functions[this->type])(state,
1188 return RLM_MODULE_FAIL;
1190 } /* loop until the stack is empty */
1192 return state->rcode;
1197 * Evaluate, which is pretty close to print, but we look at what
1200 int rlm_policy_evaluate(rlm_policy_t *inst, REQUEST *request, const char *name)
1203 policy_state_t *state;
1205 state = rad_malloc(sizeof(*state));
1206 memset(state, 0, sizeof(*state));
1207 state->request = request;
1209 state->rcode = RLM_MODULE_OK;
1210 state->component = fr_str2int(policy_component_names, name,
1211 RLM_COMPONENT_COUNT);
1213 rcode = policy_evaluate_name(state, name);
1217 return rcode; /* evaluated OK. */