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 lrad_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;
100 if (condition->compare == POLICY_LEX_L_BRACKET) {
101 policy_print(condition->child, indent);
106 if (condition->compare == POLICY_LEX_L_NOT) {
108 policy_print(condition->child, indent);
113 if (condition->compare == POLICY_LEX_CMP_TRUE) {
114 printf("%s)", condition->lhs);
118 if (condition->lhs_type == POLICY_LEX_FUNCTION) {
119 printf("%s()", condition->lhs);
123 * and move all of this logic
126 printf("\"%s\"", condition->lhs);
130 * We always print this condition.
132 printf(" %s ", lrad_int2str(rlm_policy_tokens,
135 if (condition->rhs_type == POLICY_LEX_BARE_WORD) {
136 printf("%s", condition->rhs);
140 * and move all of this logic
143 printf("\"%s\"", condition->rhs);
147 if ((condition->child_condition != POLICY_LEX_BAD) &&
148 (condition->child_condition != POLICY_LEX_BARE_WORD)) {
149 printf(" %s ", lrad_int2str(rlm_policy_tokens, condition->child_condition, "?"));
150 policy_print(condition->child, indent);
157 const policy_if_t *statement;
159 statement = (const policy_if_t *) item;
161 if (indent) printf("%*s", indent, " ");
163 policy_print(statement->condition, indent);
165 policy_print(statement->if_true, indent + 1);
166 if (indent) printf("%*s", indent, " ");
167 if (statement->if_false) {
169 if (statement->if_false->type == POLICY_TYPE_ASSIGNMENT) {
171 policy_print(statement->if_false, indent + 1);
172 if (indent) printf("%*s", indent, " ");
175 policy_print(statement->if_false, indent + 1);
183 case POLICY_TYPE_ATTRIBUTE_LIST:
185 const policy_attributes_t *this;
187 this = (const policy_attributes_t *) item;
189 if (indent) printf("%*s", indent, " ");
191 lrad_int2str(policy_reserved_words,
193 lrad_int2str(rlm_policy_tokens,
195 policy_print(this->attributes, indent + 1);
196 if (indent) printf("%*s", indent, " ");
201 case POLICY_TYPE_NAMED_POLICY:
203 const policy_named_t *this;
205 this = (const policy_named_t *) item;
206 if (indent) printf("%*s", indent, " ");
207 printf("policy %s {\n", this->name);
208 policy_print(this->policy, indent + 1);
209 if (indent) printf("%*s", indent, " ");
214 case POLICY_TYPE_CALL:
216 const policy_call_t *this;
218 this = (const policy_call_t *) item;
219 if (indent) printf("%*s", indent, " ");
220 printf("call %s\n", this->name);
224 case POLICY_TYPE_RETURN:
226 const policy_return_t *this;
228 this = (const policy_return_t *) item;
229 if (indent) printf("%*s", indent, " ");
230 printf("return %s\n",
231 lrad_int2str(policy_return_codes,
232 this->rcode, "???"));
236 case POLICY_TYPE_MODULE:
238 const policy_module_t *this;
240 this = (const policy_module_t *) item;
241 if (indent) printf("%*s", indent, " ");
242 printf("module %s <stuff>\n",
243 lrad_int2str(policy_component_names,
244 this->component, "???"));
249 if (indent) printf("%*s", indent, " ");
260 void rlm_policy_print(const policy_item_t *item)
262 printf("----------------------------------------------------------\n");
263 policy_print(item, 0);
264 printf("----------------------------------------------------------\n");
268 * Internal stack of things to do. This lets us have function
271 * Yes, we should learn lex, yacc, etc.
273 #define POLICY_MAX_STACK 16
274 typedef struct policy_state_t {
276 REQUEST *request; /* so it's not passed on the C stack */
277 int rcode; /* for functions, etc. */
278 int component; /* for calling other modules */
280 const policy_item_t *stack[POLICY_MAX_STACK];
284 static int policy_evaluate_name(policy_state_t *state, const char *name);
287 * Push an item onto the state.
289 static int policy_stack_push(policy_state_t *state, const policy_item_t *item)
291 rad_assert(state->depth >= 0);
294 * Asked to push nothing. Don't push it.
299 * State is full. Die.
301 if (state->depth >= POLICY_MAX_STACK) {
306 * Walk back up the stack, looking for previous ocurrances
307 * of this name. If found, we have infinite recursion,
308 * which we stop dead in the water!
310 * This isn't strictly necessary right now, as we look up
311 * policies by name when they're first referenced. This
312 * means that ALL references are backwards (to the start
313 * of the file), which means that there are no circular
316 if (item->type == POLICY_TYPE_NAMED_POLICY) {
319 for (i = 0; i < state->depth; i++) {
321 * Check for circular references, by seeing
322 * if the function is already on the stack.
324 * Hmmm... do we want to do this for any type?
326 if (state->stack[i] == item) {
327 debug_evaluate("Circular call to policy %s\n",
328 ((const policy_named_t *) item)->name);
334 debug_evaluate("push %d %p\n", state->depth, item);
336 state->stack[state->depth] = item;
337 state->depth++; /* points to unused entry */
344 * Pop an item from the state.
346 static int policy_stack_pop(policy_state_t *state, const policy_item_t **pitem)
348 rad_assert(pitem != NULL);
349 rad_assert(state->depth >= 0);
352 if (state->depth == 0) {
357 *pitem = state->stack[state->depth - 1];
360 * Named policies are on the stack for catching recursion.
362 if ((*pitem)->type == POLICY_TYPE_NAMED_POLICY) {
368 * Process the whole item list.
370 if ((*pitem)->next) {
371 state->stack[state->depth - 1] = (*pitem)->next;
372 debug_evaluate("pop/push %d %p\n", state->depth - 1, *pitem);
374 state->depth--; /* points to unused entry */
375 debug_evaluate("pop %d %p\n", state->depth, *pitem);
383 * Evaluate a print statement
385 static int evaluate_print(policy_state_t *state, const policy_item_t *item)
387 const policy_print_t *this;
389 this = (const policy_print_t *) item;
391 if (this->rhs_type == POLICY_LEX_BARE_WORD) {
392 printf("%s\n", this->rhs);
396 radius_xlat(buffer, sizeof(buffer), this->rhs,
397 state->request, NULL);
398 printf("%s", buffer);
402 * Doesn't change state->rcode
409 * Return a VALUE_PAIR, given an attribute name.
411 * FIXME: Have it return the N'th one, too, like
414 * The amount of duplicated code is getting annoying...
416 static VALUE_PAIR *find_vp(REQUEST *request, const char *name)
419 const DICT_ATTR *dattr;
423 vps = request->packet->vps;;
426 * FIXME: use names from reserved word list?
428 if (strncasecmp(name, "request:", 8) == 0) {
430 } else if (strncasecmp(name, "reply:", 6) == 0) {
432 vps = request->reply->vps;
433 } else if (strncasecmp(name, "proxy-request:", 14) == 0) {
435 if (request->proxy) {
436 vps = request->proxy->vps;
438 } else if (strncasecmp(name, "proxy-reply:", 12) == 0) {
440 if (request->proxy_reply) {
441 vps = request->proxy_reply->vps;
443 } else if (strncasecmp(name, "control:", 8) == 0) {
445 vps = request->config_items;
446 } /* else it must be a bare attribute name */
452 dattr = dict_attrbyname(p);
454 fprintf(stderr, "No such attribute %s\n", p);
455 return NULL; /* no such attribute */
458 return pairfind(vps, dattr->attr);
463 * Evaluate an assignment
465 * Not really used much...
467 static int evaluate_assignment(policy_state_t *state, const policy_item_t *item)
469 const policy_assignment_t *this;
471 const DICT_ATTR *dattr;
474 this = (const policy_assignment_t *) item;
476 rad_assert(this->lhs != NULL);
477 rad_assert(this->rhs != NULL);
480 dattr = dict_attrbyname(this->lhs);
482 fprintf(stderr, "HUH?\n");
492 * Evaluate a condition
494 static int evaluate_condition(policy_state_t *state, const policy_item_t *item)
497 const policy_condition_t *this;
498 VALUE_PAIR *vp = NULL;
499 const char *data = NULL;
505 char lhs_buffer[2048];
507 this = (const policy_condition_t *) item;
511 * FIXME: Don't always do this...
513 if (this->compare != POLICY_LEX_L_BRACKET) {
514 if (this->lhs_type == POLICY_LEX_FUNCTION) {
516 * We can't call evaluate_call here,
517 * because that just pushes stuff onto
518 * the stack, and we want to actually
519 * evaluate all of it...
521 rcode = policy_evaluate_name(state, this->lhs);
522 data = lrad_int2str(policy_return_codes, rcode, "???");
523 strlcpy(lhs_buffer, data, sizeof(lhs_buffer)); /* FIXME: yuck */
524 } else if (this->lhs_type == POLICY_LEX_DOUBLE_QUOTED_STRING) {
525 if (radius_xlat(lhs_buffer, sizeof(lhs_buffer), this->lhs,
526 state->request, NULL) > 0) {
532 switch (this->compare) {
533 case POLICY_LEX_L_BRACKET: /* nested brackets are a special case */
534 rcode = evaluate_condition(state, this->child);
537 case POLICY_LEX_L_NOT:
538 rcode = evaluate_condition(state, this->child);
539 rcode = (rcode == FALSE); /* reverse sense of test */
542 case POLICY_LEX_CMP_FALSE: /* non-existence */
543 if (this->lhs_type == POLICY_LEX_BARE_WORD) {
544 vp = find_vp(state->request, this->lhs);
545 rcode = (vp == NULL);
547 rcode = (data == NULL);
551 case POLICY_LEX_CMP_TRUE: /* existence */
552 if (this->lhs_type == POLICY_LEX_BARE_WORD) {
553 vp = find_vp(state->request, this->lhs);
554 rcode = (vp != NULL);
556 rcode = (data != NULL);
560 default: /* process other comparisons */
561 if ((this->compare != POLICY_LEX_CMP_EQUALS) &&
563 (this->compare != POLICY_LEX_RX_EQUALS) &&
564 (this->compare != POLICY_LEX_RX_NOT_EQUALS) &&
566 (this->compare != POLICY_LEX_LT) &&
567 (this->compare != POLICY_LEX_GT) &&
568 (this->compare != POLICY_LEX_LE) &&
569 (this->compare != POLICY_LEX_GE) &&
570 (this->compare != POLICY_LEX_CMP_NOT_EQUALS)) {
571 fprintf(stderr, "%d: bad comparison\n",
576 if (this->lhs_type == POLICY_LEX_BARE_WORD) {
579 vp = find_vp(state->request, this->lhs);
582 * A op B always returns FALSE if A doesn't
585 if (!vp) return FALSE; /* not in the request */
588 * FIXME: Move sanity checks to
589 * post-parse code, so we don't do
590 * it on every packet.
592 vp_prints_value(buffer, sizeof(buffer), vp, 0);
593 myvp = pairmake(vp->name, this->rhs, T_OP_EQ);
597 * FIXME: What to do about comparisons
598 * where vp doesn't exist? Right now,
599 * "simplepaircmp" returns -1, which is
600 * probably a bad idea. it should
601 * instead take an operator, a pointer to
602 * the comparison result, and return
603 * "true/false" for "comparions
604 * succeeded/failed", which are different
605 * error codes than "comparison is less
606 * than, equal to, or greater than zero".
608 compare = simplepaircmp(state->request,
614 * FIXME: Do something for RHS type?
616 printf("CMP %s %s\n", lhs_buffer, this->rhs);
617 compare = strcmp(lhs_buffer, this->rhs);
620 debug_evaluate("CONDITION COMPARE %d\n", compare);
622 switch (this->compare) {
623 case POLICY_LEX_CMP_EQUALS:
624 rcode = (compare == 0);
627 case POLICY_LEX_CMP_NOT_EQUALS:
628 rcode = (compare != 0);
632 rcode = (compare < 0);
636 rcode = (compare > 0);
640 rcode =(compare <= 0);
644 rcode = (compare >= 0);
648 case POLICY_LEX_RX_EQUALS:
649 { /* FIXME: copied from src/main/valuepair.c */
651 regmatch_t rxmatch[REQUEST_MAX_REGEX + 1];
654 * Include substring matches.
656 if (regcomp(®, this->rhs,
657 REG_EXTENDED) != 0) {
660 rad_assert(data != NULL);
661 rcode = regexec(®, data,
662 REQUEST_MAX_REGEX + 1,
664 rcode = (rcode == 0);
668 * Add %{0}, %{1}, etc.
670 for (i = 0; i <= REQUEST_MAX_REGEX; i++) {
675 * Didn't match: delete old
676 * match, if it existed.
679 (rxmatch[i].rm_so == -1)) {
680 p = request_data_get(state->request, state->request,
681 REQUEST_DATA_REGEX | i);
695 * Copy substring into buffer.
698 data + rxmatch[i].rm_so,
699 rxmatch[i].rm_eo - rxmatch[i].rm_so);
700 rxbuffer[rxmatch[i].rm_eo - rxmatch[i].rm_so] = '\0';
703 * Copy substring, and add it to
706 * Note that we don't check
707 * for out of memory, which is
708 * the only error we can get...
710 p = strdup(rxbuffer);
711 request_data_add(state->request,
713 REQUEST_DATA_REGEX | i,
720 case POLICY_LEX_RX_NOT_EQUALS:
721 regcomp(®, this->rhs, REG_EXTENDED|REG_NOSUB);
722 rad_assert(data != NULL);
723 rcode = regexec(®, data,
725 rcode = (rcode != 0);
728 #endif /* HAVE_REGEX_H */
732 } /* switch over comparison operators */
733 break; /* default from first switch over compare */
739 switch (this->child_condition) {
743 case POLICY_LEX_L_AND:
744 if (!rcode) return rcode; /* FALSE && x == FALSE */
747 case POLICY_LEX_L_OR:
748 if (rcode) return rcode; /* TRUE && x == TRUE */
755 this = (const policy_condition_t *) this->child;
758 return 1; /* should never reach here */
763 * Evaluate an 'if' statement
765 static int evaluate_if(policy_state_t *state, const policy_item_t *item)
768 const policy_if_t *this;
770 this = (const policy_if_t *) item;
773 * evaluate_condition calls itself recursively.
774 * We should probably allocate a new state, instead.
776 rcode = evaluate_condition(state, this->condition);
777 debug_evaluate("IF condition returned %s\n",
778 rcode ? "true" : "false");
780 rcode = policy_stack_push(state, this->if_true);
781 if (!rcode) return rcode;
782 } else if (this->if_false) {
783 rcode = policy_stack_push(state, this->if_false);
784 if (!rcode) return rcode;
788 * 'if' can fail, if the block it's processing fails.
795 * Make a VALUE_PAIR from a policy_assignment_t*
797 * The assignment operator has to be '='.
799 static VALUE_PAIR *assign2vp(REQUEST *request,
800 const policy_assignment_t *assign)
803 LRAD_TOKEN operator = T_OP_EQ;
804 const char *value = assign->rhs;
807 if ((assign->rhs_type == POLICY_LEX_DOUBLE_QUOTED_STRING) &&
808 (strchr(assign->rhs, '%') != NULL)) {
809 radius_xlat(buffer, sizeof(buffer), assign->rhs,
815 * This is crappy.. fix it.
817 switch (assign->assign) {
818 case POLICY_LEX_ASSIGN:
822 case POLICY_LEX_SET_EQUALS:
826 case POLICY_LEX_PLUS_EQUALS:
831 fprintf(stderr, "Expected '=' for operator, not '%s' at line %d\n",
832 lrad_int2str(rlm_policy_tokens,
833 assign->assign, "?"),
834 assign->item.lineno);
838 vp = pairmake(assign->lhs, value, operator);
840 fprintf(stderr, "SHIT: %s %s\n", value, librad_errstr);
848 * Evaluate a 'packet .= {attrs}' statement
850 static int evaluate_attr_list(policy_state_t *state, const policy_item_t *item)
852 const policy_attributes_t *this;
853 VALUE_PAIR **vps = NULL;
854 VALUE_PAIR *vp, *head, **tail;
855 const policy_item_t *attr;
857 this = (const policy_attributes_t *) item;
859 switch (this->where) {
860 case POLICY_RESERVED_CONTROL:
861 vps = &(state->request->config_items);
864 case POLICY_RESERVED_REQUEST:
865 vps = &(state->request->packet->vps);
868 case POLICY_RESERVED_REPLY:
869 vps = &(state->request->reply->vps);
872 case POLICY_RESERVED_PROXY_REQUEST:
873 if (!state->request->proxy) return 0; /* FIXME: print error */
874 vps = &(state->request->proxy->vps);
877 case POLICY_RESERVED_PROXY_REPLY:
878 if (!state->request->proxy_reply) return 0; /* FIXME: print error */
879 vps = &(state->request->proxy_reply->vps);
889 for (attr = this->attributes; attr != NULL; attr = attr->next) {
890 if (attr->type != POLICY_TYPE_ASSIGNMENT) {
891 fprintf(stderr, "bad assignment in attribute list at line %d\n", attr->lineno);
896 vp = assign2vp(state->request, (const policy_assignment_t *) attr);
898 fprintf(stderr, "Failed to allocate VP\n");
907 case POLICY_LEX_SET_EQUALS: /* dangerous: removes all previous things! */
912 case POLICY_LEX_ASSIGN: /* 'union' */
913 pairmove(vps, &head);
917 case POLICY_LEX_CONCAT_EQUALS:
922 fprintf(stderr, "HUH?\n");
927 state->rcode = RLM_MODULE_UPDATED; /* we did stuff */
934 * Evaluate a reference call to a module.
936 static int evaluate_call(policy_state_t *state, const policy_item_t *item)
939 const policy_call_t *this;
940 const policy_named_t *policy;
942 this = (const policy_call_t *) item;
944 policy = rlm_policy_find(state->inst->policies, this->name);
945 if (!policy) return 0; /* not found... */
947 DEBUG2("rlm_policy: Evaluating policy %s", this->name);
949 rad_assert(policy->policy->type != POLICY_TYPE_BAD);
950 rad_assert(policy->policy->type < POLICY_TYPE_NUM_TYPES);
953 * Push the name of the function onto the stack,
954 * so that we can catch recursive calls.
956 * The "pop" function will skip over it when it sees it.
958 rcode = policy_stack_push(state, (const policy_item_t *) policy);
964 * Push it onto the stack. Other code will take care of
967 rcode = policy_stack_push(state, policy->policy);
977 * Evaluate a return statement
979 static int evaluate_return(policy_state_t *state, const policy_item_t *item)
981 const policy_return_t *this;
983 this = (const policy_return_t *) item;
984 state->rcode = this->rcode;
986 return 1; /* we succeeded */
991 * Evaluate a module statement
993 static int evaluate_module(policy_state_t *state, const policy_item_t *item)
995 const policy_module_t *this;
997 this = (const policy_module_t *) item;
1000 * Just to be paranoid. Maybe we want to loosen this
1001 * restriction in the future?
1003 if (this->component != state->component) {
1004 DEBUG2("rlm_policy: Cannot mix & match components");
1008 DEBUG2("rlm_policy: begin nested call");
1009 state->rcode = modcall(this->component, this->mc, state->request);
1010 DEBUG2("rlm_policy: end nested call");
1012 return 1; /* we succeeded */
1017 * State machine stuff.
1019 typedef int (*policy_evaluate_type_t)(policy_state_t *, const policy_item_t *);
1023 * MUST be kept in sync with policy_type_t
1025 static policy_evaluate_type_t evaluate_functions[POLICY_TYPE_NUM_TYPES] = {
1026 NULL, /* POLICY_TYPE_BAD */
1029 evaluate_assignment,
1032 NULL, /* define a named policy.. */
1040 * Evaluate a policy, keyed by name.
1042 static int policy_evaluate_name(policy_state_t *state, const char *name)
1045 const policy_item_t *this;
1046 policy_named_t mypolicy, *policy;
1048 mypolicy.name = name;
1049 policy = rbtree_finddata(state->inst->policies, &mypolicy);
1050 if (!policy) return RLM_MODULE_FAIL;
1052 DEBUG2("rlm_policy: Evaluating policy %s", name);
1054 rad_assert(policy->item.type != POLICY_TYPE_BAD);
1055 rad_assert(policy->item.type < POLICY_TYPE_NUM_TYPES);
1057 rcode = policy_stack_push(state, policy->policy);
1059 return RLM_MODULE_FAIL;
1063 * FIXME: Look for magic keywords like "return",
1064 * where the packet gets accepted/rejected/whatever
1066 while (policy_stack_pop(state, &this)) {
1067 rad_assert(this != NULL);
1068 rad_assert(this->type != POLICY_TYPE_BAD);
1069 rad_assert(this->type < POLICY_TYPE_NUM_TYPES);
1071 debug_evaluate("Evaluating at line %d\n",
1073 rcode = (*evaluate_functions[this->type])(state,
1076 return RLM_MODULE_FAIL;
1078 } /* loop until the stack is empty */
1080 return state->rcode;
1085 * Evaluate, which is pretty close to print, but we look at what
1088 int rlm_policy_evaluate(rlm_policy_t *inst, REQUEST *request, const char *name)
1091 policy_state_t *state;
1093 state = rad_malloc(sizeof(*state));
1094 memset(state, 0, sizeof(*state));
1095 state->request = request;
1097 state->rcode = RLM_MODULE_OK;
1098 state->component = lrad_str2int(policy_component_names, name,
1099 RLM_COMPONENT_COUNT);
1101 rcode = policy_evaluate_name(state, name);
1105 return rcode; /* evaluated OK. */