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 2000,2006 The FreeRADIUS server project
25 #include <freeradius-devel/radiusd.h>
26 #include <freeradius-devel/modpriv.h>
27 #include <freeradius-devel/modcall.h>
28 #include <freeradius-devel/parser.h>
29 #include <freeradius-devel/rad_assert.h>
32 /* mutually-recursive static functions need a prototype up front */
33 static modcallable *do_compile_modgroup(modcallable *,
34 rlm_components_t, CONF_SECTION *,
37 /* Actions may be a positive integer (the highest one returned in the group
38 * will be returned), or the keyword "return", represented here by
39 * MOD_ACTION_RETURN, to cause an immediate return.
40 * There's also the keyword "reject", represented here by MOD_ACTION_REJECT
41 * to cause an immediate reject. */
42 #define MOD_ACTION_RETURN (-1)
43 #define MOD_ACTION_REJECT (-2)
45 /* Here are our basic types: modcallable, modgroup, and modsingle. For an
46 * explanation of what they are all about, see doc/configurable_failover.rst */
49 struct modcallable *next;
51 char const *debug_name;
52 enum { MOD_SINGLE = 1, MOD_GROUP, MOD_LOAD_BALANCE, MOD_REDUNDANT_LOAD_BALANCE,
54 MOD_IF, MOD_ELSE, MOD_ELSIF, MOD_UPDATE, MOD_SWITCH, MOD_CASE,
55 MOD_FOREACH, MOD_BREAK, MOD_RETURN,
57 MOD_POLICY, MOD_REFERENCE, MOD_XLAT } type;
58 rlm_components_t method;
59 int actions[RLM_MODULE_NUMCODES];
62 #define MOD_LOG_OPEN_BRACE RDEBUG2("%s {", c->debug_name)
64 #define MOD_LOG_CLOSE_BRACE RDEBUG2("} # %s = %s", c->debug_name, fr_int2str(mod_rcode_table, result, "<invalid>"))
67 modcallable mc; /* self */
73 } grouptype; /* after mc */
74 modcallable *children;
75 modcallable *tail; /* of the children list */
77 value_pair_map_t *map; /* update */
78 value_pair_tmpl_t *vpt; /* switch */
79 fr_cond_t *cond; /* if/elsif */
85 module_instance_t *modinst;
100 /* Simple conversions: modsingle and modgroup are subclasses of modcallable,
101 * so we often want to go back and forth between them. */
102 static modsingle *mod_callabletosingle(modcallable *p)
104 rad_assert(p->type==MOD_SINGLE);
105 return (modsingle *)p;
107 static modgroup *mod_callabletogroup(modcallable *p)
109 rad_assert((p->type > MOD_SINGLE) && (p->type <= MOD_POLICY));
111 return (modgroup *)p;
113 static modcallable *mod_singletocallable(modsingle *p)
115 return (modcallable *)p;
117 static modcallable *mod_grouptocallable(modgroup *p)
119 return (modcallable *)p;
122 static modref *mod_callabletoref(modcallable *p)
124 rad_assert(p->type==MOD_REFERENCE);
127 static modcallable *mod_reftocallable(modref *p)
129 return (modcallable *)p;
132 static modxlat *mod_callabletoxlat(modcallable *p)
134 rad_assert(p->type==MOD_XLAT);
137 static modcallable *mod_xlattocallable(modxlat *p)
139 return (modcallable *)p;
142 /* modgroups are grown by adding a modcallable to the end */
143 static void add_child(modgroup *g, modcallable *c)
147 (void) talloc_steal(g, c);
150 g->children = g->tail = c;
152 rad_assert(g->tail->next == NULL);
157 c->parent = mod_grouptocallable(g);
160 /* Here's where we recognize all of our keywords: first the rcodes, then the
162 const FR_NAME_NUMBER mod_rcode_table[] = {
163 { "reject", RLM_MODULE_REJECT },
164 { "fail", RLM_MODULE_FAIL },
165 { "ok", RLM_MODULE_OK },
166 { "handled", RLM_MODULE_HANDLED },
167 { "invalid", RLM_MODULE_INVALID },
168 { "userlock", RLM_MODULE_USERLOCK },
169 { "notfound", RLM_MODULE_NOTFOUND },
170 { "noop", RLM_MODULE_NOOP },
171 { "updated", RLM_MODULE_UPDATED },
176 static char const *group_name[];
179 * Compile action && rcode for later use.
181 static int compile_action(modcallable *c, CONF_PAIR *cp)
184 char const *attr, *value;
186 attr = cf_pair_attr(cp);
187 value = cf_pair_value(cp);
188 if (!value) return 0;
190 if (c->type != MOD_SINGLE) {
191 ERROR("%s[%d] Invalid return code assigment inside of a %s section",
192 cf_pair_filename(cp), cf_pair_lineno(cp), group_name[c->type]);
196 if (!strcasecmp(value, "return"))
197 action = MOD_ACTION_RETURN;
199 else if (!strcasecmp(value, "break"))
200 action = MOD_ACTION_RETURN;
202 else if (!strcasecmp(value, "reject"))
203 action = MOD_ACTION_REJECT;
205 else if (strspn(value, "0123456789")==strlen(value)) {
206 action = atoi(value);
209 * Don't allow priority zero, for future use.
211 if (action == 0) return 0;
213 cf_log_err_cp(cp, "Unknown action '%s'.\n",
218 if (strcasecmp(attr, "default") != 0) {
221 rcode = fr_str2int(mod_rcode_table, attr, -1);
224 "Unknown module rcode '%s'.\n",
228 c->actions[rcode] = action;
230 } else { /* set all unset values to the default */
233 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
234 if (!c->actions[i]) c->actions[i] = action;
241 /* Some short names for debugging output */
242 static char const * const comp2str[] = {
258 #ifdef HAVE_PTHREAD_H
260 * Lock the mutex for the module
262 static void safe_lock(module_instance_t *instance)
265 pthread_mutex_lock(instance->mutex);
269 * Unlock the mutex for the module
271 static void safe_unlock(module_instance_t *instance)
274 pthread_mutex_unlock(instance->mutex);
278 * No threads: these functions become NULL's.
280 #define safe_lock(foo)
281 #define safe_unlock(foo)
284 static rlm_rcode_t CC_HINT(nonnull) call_modsingle(rlm_components_t component, modsingle *sp, REQUEST *request)
287 int indent = request->log.indent;
290 * If the request should stop, refuse to do anything.
292 blocked = (request->master_state == REQUEST_STOP_PROCESSING);
293 if (blocked) return RLM_MODULE_NOOP;
295 RDEBUG3("modsingle[%s]: calling %s (%s) for request %d",
296 comp2str[component], sp->modinst->name,
297 sp->modinst->entry->name, request->number);
298 request->log.indent = 0;
300 if (sp->modinst->force) {
301 request->rcode = sp->modinst->code;
306 * For logging unresponsive children.
308 request->module = sp->modinst->name;
310 safe_lock(sp->modinst);
311 request->rcode = sp->modinst->entry->module->methods[component](sp->modinst->insthandle, request);
312 safe_unlock(sp->modinst);
314 request->module = "";
317 * Wasn't blocked, and now is. Complain!
319 blocked = (request->master_state == REQUEST_STOP_PROCESSING);
321 RWARN("Module %s became unblocked for request %u", sp->modinst->entry->name, request->number);
325 request->log.indent = indent;
326 RDEBUG3("modsingle[%s]: returned from %s (%s) for request %d",
327 comp2str[component], sp->modinst->name,
328 sp->modinst->entry->name, request->number);
330 return request->rcode;
333 static int default_component_results[RLM_COMPONENT_COUNT] = {
334 RLM_MODULE_REJECT, /* AUTH */
335 RLM_MODULE_NOTFOUND, /* AUTZ */
336 RLM_MODULE_NOOP, /* PREACCT */
337 RLM_MODULE_NOOP, /* ACCT */
338 RLM_MODULE_FAIL, /* SESS */
339 RLM_MODULE_NOOP, /* PRE_PROXY */
340 RLM_MODULE_NOOP, /* POST_PROXY */
341 RLM_MODULE_NOOP /* POST_AUTH */
344 RLM_MODULE_NOOP, /* RECV_COA_TYPE */
345 RLM_MODULE_NOOP /* SEND_COA_TYPE */
350 static char const *group_name[] = {
354 "load-balance group",
355 "redundant-load-balance group",
372 static char const modcall_spaces[] = " ";
374 #define MODCALL_STACK_MAX (32)
377 * Don't call the modules recursively. Instead, do them
378 * iteratively, and manage the call stack ourselves.
380 typedef struct modcall_stack_entry_t {
383 int unwind; /* unwind to this one if it exists */
385 } modcall_stack_entry_t;
388 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
389 modcall_stack_entry_t *entry);
392 * Call a child of a block.
394 static void modcall_child(REQUEST *request, rlm_components_t component, int depth,
395 modcall_stack_entry_t *entry, modcallable *c,
398 modcall_stack_entry_t *next;
400 if (depth >= MODCALL_STACK_MAX) {
401 ERROR("Internal sanity check failed: module stack is too deep");
406 * Initialize the childs stack frame.
410 next->result = entry->result;
414 if (!modcall_recurse(request, component,
416 *result = RLM_MODULE_FAIL;
421 * Unwind back up the stack
423 if (next->unwind != 0) {
424 entry->unwind = next->unwind;
427 *result = next->result;
434 * Interpret the various types of blocks.
436 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
437 modcall_stack_entry_t *entry)
439 bool if_taken, was_if;
444 was_if = if_taken = false;
445 result = RLM_MODULE_UNKNOWN;
453 * Nothing more to do. Return the code and priority
454 * which was set by the caller.
459 * We've been asked to stop. Do so.
461 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
463 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
464 entry->result = RLM_MODULE_FAIL;
465 entry->priority = 9999;
471 * Handle "if" conditions.
473 if (c->type == MOD_IF) {
478 g = mod_callabletogroup(c);
479 rad_assert(g->cond != NULL);
481 RDEBUG2("%s %s{", group_name[c->type], c->name);
483 condition = radius_evaluate_cond(request, result, 0, g->cond);
486 REDEBUG("Failed retrieving values required to evaluate condition");
488 RDEBUG2("%s %s -> %s",
490 c->name, condition ? "TRUE" : "FALSE");
494 * Didn't pass. Remember that.
503 * We took the "if". Go recurse into its' children.
511 * "else" if the previous "if" was taken.
512 * "if" if the previous if wasn't taken.
514 if (c->type == MOD_ELSIF) {
515 if (!was_if) goto elsif_error;
518 * Like MOD_ELSE, but allow for a later "else"
521 RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
522 group_name[c->type], request->number);
529 * Check the "if" condition.
535 * "else" for a preceding "if".
537 if (c->type == MOD_ELSE) {
538 if (!was_if) { /* error */
540 RDEBUG2("... skipping %s for request %d: No preceding \"if\"",
541 group_name[c->type], request->number);
546 RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
547 group_name[c->type], request->number);
554 * We need to process it. Go do that.
562 * We're no longer processing if/else/elsif. Reset the
563 * trackers for those conditions.
567 #endif /* WITH_UNLANG */
569 if (c->type == MOD_SINGLE) {
573 * Process a stand-alone child, and fall through
574 * to dealing with it's parent.
576 sp = mod_callabletosingle(c);
578 result = call_modsingle(c->method, sp, request);
579 RDEBUG2("[%s] = %s", c->name ? c->name : "",
580 fr_int2str(mod_rcode_table, result, "<invalid>"));
581 goto calculate_result;
586 * Update attribute(s)
588 if (c->type == MOD_UPDATE) {
590 modgroup *g = mod_callabletogroup(c);
591 value_pair_map_t *map;
595 for (map = g->map; map != NULL; map = map->next) {
596 rcode = map_to_request(request, map, map_to_vp, NULL);
598 result = (rcode == -2) ? RLM_MODULE_INVALID : RLM_MODULE_FAIL;
601 goto calculate_result;
605 result = RLM_MODULE_NOOP;
607 goto calculate_result;
611 * Loop over a set of attributes.
613 if (c->type == MOD_FOREACH) {
614 int i, foreach_depth = -1;
615 VALUE_PAIR *vps, *vp;
616 modcall_stack_entry_t *next = NULL;
618 modgroup *g = mod_callabletogroup(c);
620 if (depth >= MODCALL_STACK_MAX) {
621 ERROR("Internal sanity check failed: module stack is too deep");
626 * Figure out how deep we are in nesting by looking at request_data
629 for (i = 0; i < 8; i++) {
630 if (!request_data_reference(request,
637 if (foreach_depth < 0) {
638 REDEBUG("foreach Nesting too deep!");
639 result = RLM_MODULE_FAIL;
640 goto calculate_result;
644 * Copy the VPs from the original request, this ensures deterministic
645 * behaviour if someone decides to add or remove VPs in the set were
648 if (tmpl_copy_vps(request, &vps, request, g->vpt) < 0) { /* nothing to loop over */
650 result = RLM_MODULE_NOOP;
652 goto calculate_result;
655 rad_assert(vps != NULL);
656 fr_cursor_init(©, &vps);
658 RDEBUG2("foreach %s ", c->name);
661 * This is the actual body of the foreach loop
663 for (vp = fr_cursor_first(©);
665 vp = fr_cursor_next(©)) {
667 if (fr_debug_flag >= 2) {
670 vp_prints_value(buffer, sizeof(buffer), vp, '"');
671 RDEBUG2("# Foreach-Variable-%d = %s", foreach_depth, buffer);
676 * Add the vp to the request, so that
677 * xlat.c, xlat_foreach() can find it.
679 request_data_add(request, radius_get_vp, foreach_depth, &vp, false);
682 * Initialize the childs stack frame.
685 next->c = g->children;
686 next->result = entry->result;
690 if (!modcall_recurse(request, component, depth + 1, next)) {
695 * We've unwound to the enclosing
696 * "foreach". Stop the unwinding.
698 if (next->unwind == MOD_FOREACH) {
703 * Unwind all the way.
705 if (next->unwind == MOD_RETURN) {
706 entry->unwind = MOD_RETURN;
709 } /* loop over VPs */
712 * Free the copied vps and the request data
713 * If we don't remove the request data, something could call
714 * the xlat outside of a foreach loop and trigger a segv.
717 request_data_get(request, radius_get_vp, foreach_depth);
719 rad_assert(next != NULL);
720 result = next->result;
721 priority = next->priority;
723 goto calculate_result;
727 * Break out of a "foreach" loop, or return from a nested
730 if ((c->type == MOD_BREAK) || (c->type == MOD_RETURN)) {
734 RDEBUG2("%s", group_name[c->type]);
736 for (i = 8; i >= 0; i--) {
737 copy_p = request_data_get(request, radius_get_vp, i);
739 if (c->type == MOD_BREAK) {
740 RDEBUG2("# break Foreach-Variable-%d", i);
747 * Leave result / priority on the stack, and stop processing the section.
749 entry->unwind = c->type;
753 #endif /* WITH_UNLANG */
756 * Child is a group that has children of it's own.
758 if ((c->type == MOD_GROUP) || (c->type == MOD_POLICY)
760 || (c->type == MOD_CASE)
768 g = mod_callabletogroup(c);
771 * This should really have been caught in the
772 * compiler, and the node never generated. But
773 * doing that requires changing it's API so that
774 * it returns a flag instead of the compiled
778 RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
783 modcall_child(request, component,
784 depth + 1, entry, g->children,
787 goto calculate_result;
791 if (c->type == MOD_SWITCH) {
792 modcallable *this, *found, *null_case;
796 value_pair_map_t map;
797 value_pair_tmpl_t vpt;
801 g = mod_callabletogroup(c);
803 memset(&cond, 0, sizeof(cond));
804 memset(&map, 0, sizeof(map));
806 cond.type = COND_TYPE_MAP;
807 cond.data.map = ↦
809 map.op = T_OP_CMP_EQ;
810 map.ci = cf_sectiontoitem(g->cs);
812 rad_assert(g->vpt != NULL);
814 null_case = found = NULL;
818 * The attribute doesn't exist. We can skip
819 * directly to the default 'case' statement.
821 if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
823 for (this = g->children; this; this = this->next) {
824 rad_assert(this->type == MOD_CASE);
826 h = mod_callabletogroup(this);
827 if (h->vpt) continue;
837 * Expand the template if necessary, so that it
838 * is evaluated once instead of for each 'case'
841 if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
842 (g->vpt->type == TMPL_TYPE_XLAT) ||
843 (g->vpt->type == TMPL_TYPE_EXEC)) {
844 if (radius_expand_tmpl(&data, request, g->vpt) < 0) goto find_null_case;
845 tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, talloc_array_length(data.strvalue) - 1);
849 * Find either the exact matching name, or the
850 * "case {...}" statement.
852 for (this = g->children; this; this = this->next) {
853 rad_assert(this->type == MOD_CASE);
855 h = mod_callabletogroup(this);
858 * Remember the default case
861 if (!null_case) null_case = this;
866 * If we're switching over an attribute
867 * AND we haven't pre-parsed the data for
868 * the case statement, then cast the data
869 * to the type of the attribute.
871 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
872 (h->vpt->type != TMPL_TYPE_DATA)) {
875 cond.cast = g->vpt->tmpl_da;
878 * Remove unnecessary casting.
880 if ((h->vpt->type == TMPL_TYPE_ATTR) &&
881 (g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
886 * Use the pre-expanded string.
888 } else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
889 (g->vpt->type == TMPL_TYPE_XLAT) ||
890 (g->vpt->type == TMPL_TYPE_EXEC)) {
896 * Else evaluate the 'switch' statement.
904 if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
911 if (!found) found = null_case;
914 talloc_free(data.ptr);
915 modcall_child(request, component, depth + 1, entry, found, &result);
917 goto calculate_result;
921 if ((c->type == MOD_LOAD_BALANCE) ||
922 (c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
924 modcallable *this, *found;
929 g = mod_callabletogroup(c);
931 rad_assert(g->children != NULL);
934 * Choose a child at random.
936 for (this = g->children; this; this = this->next) {
939 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
946 if (c->type == MOD_LOAD_BALANCE) {
947 modcall_child(request, component,
948 depth + 1, entry, found,
955 modcall_child(request, component,
956 depth + 1, entry, this,
958 if (this->actions[result] == MOD_ACTION_RETURN) {
964 if (!this) this = g->children;
965 } while (this != found);
968 goto calculate_result;
969 } /* MOD_LOAD_BALANCE */
972 * Reference another virtual server.
974 * This should really be deleted, and replaced with a
975 * more abstracted / functional version.
977 if (c->type == MOD_REFERENCE) {
978 modref *mr = mod_callabletoref(c);
979 char const *server = request->server;
981 if (server == mr->ref_name) {
982 RWDEBUG("Suppressing recursive call to server %s", server);
986 request->server = mr->ref_name;
987 RDEBUG("server %s { # nested call", mr->ref_name);
988 result = indexed_modcall(component, 0, request);
989 RDEBUG("} # server %s with nested call", mr->ref_name);
990 request->server = server;
991 goto calculate_result;
992 } /* MOD_REFERENCE */
995 * xlat a string without doing anything else
997 * This should really be deleted, and replaced with a
998 * more abstracted / functional version.
1000 if (c->type == MOD_XLAT) {
1001 modxlat *mx = mod_callabletoxlat(c);
1005 radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
1007 RDEBUG("`%s`", mx->xlat_name);
1008 radius_exec_program(NULL, 0, NULL, request, mx->xlat_name, request->packet->vps,
1009 false, true, EXEC_TIMEOUT);
1016 * Add new module types here.
1021 RDEBUG("(%s, %d) ? (%s, %d)",
1022 fr_int2str(mod_rcode_table, result, "<invalid>"),
1024 fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
1029 rad_assert(result != RLM_MODULE_UNKNOWN);
1032 * The child's action says return. Do so.
1034 if ((c->actions[result] == MOD_ACTION_RETURN) &&
1036 entry->result = result;
1041 * If "reject", break out of the loop and return
1044 if (c->actions[result] == MOD_ACTION_REJECT) {
1045 entry->result = RLM_MODULE_REJECT;
1050 * The array holds a default priority for this return
1051 * code. Grab it in preference to any unset priority.
1054 priority = c->actions[result];
1058 * We're higher than any previous priority, remember this
1059 * return code and priority.
1061 if (priority > entry->priority) {
1062 entry->result = result;
1063 entry->priority = priority;
1068 * If we're processing a "case" statement, we return once
1069 * it's done, rather than going to the next "case" statement.
1071 if (c->type == MOD_CASE) goto finish;
1075 * If we've been told to stop processing
1078 if (entry->unwind == MOD_BREAK) {
1079 RDEBUG2("# unwind to enclosing foreach");
1084 if (entry->unwind == MOD_RETURN) {
1089 entry->c = entry->c->next;
1091 if (entry->c) goto redo;
1102 /** Call a module, iteratively, with a local stack, rather than recursively
1104 * What did Paul Graham say about Lisp...?
1106 int modcall(rlm_components_t component, modcallable *c, REQUEST *request)
1108 modcall_stack_entry_t stack[MODCALL_STACK_MAX];
1111 memset(stack, 0, sizeof(stack));
1114 * Set up the initial stack frame.
1117 stack[0].result = default_component_results[component];
1118 stack[0].priority = 0;
1119 stack[0].unwind = 0;
1122 * Call the main handler.
1124 if (!modcall_recurse(request, component, 0, &stack[0])) {
1125 return RLM_MODULE_FAIL;
1129 * Return the result.
1131 return stack[0].result;
1136 static char const *action2str(int action)
1138 static char buf[32];
1139 if(action==MOD_ACTION_RETURN)
1141 if(action==MOD_ACTION_REJECT)
1143 snprintf(buf, sizeof buf, "%d", action);
1147 /* If you suspect a bug in the parser, you'll want to use these dump
1148 * functions. dump_tree should reproduce a whole tree exactly as it was found
1149 * in radiusd.conf, but in long form (all actions explicitly defined) */
1150 static void dump_mc(modcallable *c, int indent)
1154 if(c->type==MOD_SINGLE) {
1155 modsingle *single = mod_callabletosingle(c);
1156 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1157 single->modinst->name);
1158 } else if ((c->type > MOD_SINGLE) && (c->type <= MOD_POLICY)) {
1159 modgroup *g = mod_callabletogroup(c);
1161 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1162 group_name[c->type]);
1163 for(p = g->children;p;p = p->next)
1164 dump_mc(p, indent+1);
1165 } /* else ignore it for now */
1167 for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
1168 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
1169 fr_int2str(mod_rcode_table, i, "<invalid>"),
1170 action2str(c->actions[i]));
1173 DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
1176 static void dump_tree(rlm_components_t comp, modcallable *c)
1178 DEBUG("[%s]", comp2str[comp]);
1182 #define dump_tree(a, b)
1185 /* These are the default actions. For each component, the group{} block
1186 * behaves like the code from the old module_*() function. redundant{} and
1187 * append{} are based on my guesses of what they will be used for. --Pac. */
1189 defaultactions[RLM_COMPONENT_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1195 MOD_ACTION_RETURN, /* reject */
1197 MOD_ACTION_RETURN, /* ok */
1198 MOD_ACTION_RETURN, /* handled */
1200 MOD_ACTION_RETURN, /* userlock */
1201 MOD_ACTION_RETURN, /* notfound */
1207 MOD_ACTION_RETURN, /* reject */
1209 MOD_ACTION_RETURN, /* ok */
1210 MOD_ACTION_RETURN, /* handled */
1211 MOD_ACTION_RETURN, /* invalid */
1212 MOD_ACTION_RETURN, /* userlock */
1213 MOD_ACTION_RETURN, /* notfound */
1214 MOD_ACTION_RETURN, /* noop */
1215 MOD_ACTION_RETURN /* updated */
1219 MOD_ACTION_RETURN, /* reject */
1221 MOD_ACTION_RETURN, /* ok */
1222 MOD_ACTION_RETURN, /* handled */
1223 MOD_ACTION_RETURN, /* invalid */
1224 MOD_ACTION_RETURN, /* userlock */
1226 MOD_ACTION_RETURN, /* noop */
1227 MOD_ACTION_RETURN /* updated */
1234 MOD_ACTION_RETURN, /* reject */
1235 MOD_ACTION_RETURN, /* fail */
1237 MOD_ACTION_RETURN, /* handled */
1238 MOD_ACTION_RETURN, /* invalid */
1239 MOD_ACTION_RETURN, /* userlock */
1246 MOD_ACTION_RETURN, /* reject */
1248 MOD_ACTION_RETURN, /* ok */
1249 MOD_ACTION_RETURN, /* handled */
1250 MOD_ACTION_RETURN, /* invalid */
1251 MOD_ACTION_RETURN, /* userlock */
1252 MOD_ACTION_RETURN, /* notfound */
1253 MOD_ACTION_RETURN, /* noop */
1254 MOD_ACTION_RETURN /* updated */
1258 MOD_ACTION_RETURN, /* reject */
1260 MOD_ACTION_RETURN, /* ok */
1261 MOD_ACTION_RETURN, /* handled */
1262 MOD_ACTION_RETURN, /* invalid */
1263 MOD_ACTION_RETURN, /* userlock */
1265 MOD_ACTION_RETURN, /* noop */
1266 MOD_ACTION_RETURN /* updated */
1273 MOD_ACTION_RETURN, /* reject */
1274 MOD_ACTION_RETURN, /* fail */
1276 MOD_ACTION_RETURN, /* handled */
1277 MOD_ACTION_RETURN, /* invalid */
1278 MOD_ACTION_RETURN, /* userlock */
1279 MOD_ACTION_RETURN, /* notfound */
1285 MOD_ACTION_RETURN, /* reject */
1287 MOD_ACTION_RETURN, /* ok */
1288 MOD_ACTION_RETURN, /* handled */
1289 MOD_ACTION_RETURN, /* invalid */
1290 MOD_ACTION_RETURN, /* userlock */
1291 MOD_ACTION_RETURN, /* notfound */
1292 MOD_ACTION_RETURN, /* noop */
1293 MOD_ACTION_RETURN /* updated */
1297 MOD_ACTION_RETURN, /* reject */
1299 MOD_ACTION_RETURN, /* ok */
1300 MOD_ACTION_RETURN, /* handled */
1301 MOD_ACTION_RETURN, /* invalid */
1302 MOD_ACTION_RETURN, /* userlock */
1304 MOD_ACTION_RETURN, /* noop */
1305 MOD_ACTION_RETURN /* updated */
1312 MOD_ACTION_RETURN, /* reject */
1313 MOD_ACTION_RETURN, /* fail */
1315 MOD_ACTION_RETURN, /* handled */
1316 MOD_ACTION_RETURN, /* invalid */
1317 MOD_ACTION_RETURN, /* userlock */
1318 MOD_ACTION_RETURN, /* notfound */
1326 MOD_ACTION_RETURN, /* ok */
1327 MOD_ACTION_RETURN, /* handled */
1336 MOD_ACTION_RETURN, /* reject */
1338 MOD_ACTION_RETURN, /* ok */
1339 MOD_ACTION_RETURN, /* handled */
1340 MOD_ACTION_RETURN, /* invalid */
1341 MOD_ACTION_RETURN, /* userlock */
1343 MOD_ACTION_RETURN, /* noop */
1344 MOD_ACTION_RETURN /* updated */
1351 MOD_ACTION_RETURN, /* reject */
1353 MOD_ACTION_RETURN, /* ok */
1354 MOD_ACTION_RETURN, /* handled */
1355 MOD_ACTION_RETURN, /* invalid */
1356 MOD_ACTION_RETURN, /* userlock */
1357 MOD_ACTION_RETURN, /* notfound */
1358 MOD_ACTION_RETURN, /* noop */
1359 MOD_ACTION_RETURN /* updated */
1363 MOD_ACTION_RETURN, /* reject */
1365 MOD_ACTION_RETURN, /* ok */
1366 MOD_ACTION_RETURN, /* handled */
1367 MOD_ACTION_RETURN, /* invalid */
1368 MOD_ACTION_RETURN, /* userlock */
1369 MOD_ACTION_RETURN, /* notfound */
1370 MOD_ACTION_RETURN, /* noop */
1371 MOD_ACTION_RETURN /* updated */
1375 MOD_ACTION_RETURN, /* reject */
1377 MOD_ACTION_RETURN, /* ok */
1378 MOD_ACTION_RETURN, /* handled */
1379 MOD_ACTION_RETURN, /* invalid */
1380 MOD_ACTION_RETURN, /* userlock */
1381 MOD_ACTION_RETURN, /* notfound */
1382 MOD_ACTION_RETURN, /* noop */
1383 MOD_ACTION_RETURN /* updated */
1390 MOD_ACTION_RETURN, /* reject */
1391 MOD_ACTION_RETURN, /* fail */
1393 MOD_ACTION_RETURN, /* handled */
1394 MOD_ACTION_RETURN, /* invalid */
1395 MOD_ACTION_RETURN, /* userlock */
1402 MOD_ACTION_RETURN, /* reject */
1404 MOD_ACTION_RETURN, /* ok */
1405 MOD_ACTION_RETURN, /* handled */
1406 MOD_ACTION_RETURN, /* invalid */
1407 MOD_ACTION_RETURN, /* userlock */
1408 MOD_ACTION_RETURN, /* notfound */
1409 MOD_ACTION_RETURN, /* noop */
1410 MOD_ACTION_RETURN /* updated */
1414 MOD_ACTION_RETURN, /* reject */
1416 MOD_ACTION_RETURN, /* ok */
1417 MOD_ACTION_RETURN, /* handled */
1418 MOD_ACTION_RETURN, /* invalid */
1419 MOD_ACTION_RETURN, /* userlock */
1421 MOD_ACTION_RETURN, /* noop */
1422 MOD_ACTION_RETURN /* updated */
1429 MOD_ACTION_RETURN, /* reject */
1430 MOD_ACTION_RETURN, /* fail */
1432 MOD_ACTION_RETURN, /* handled */
1433 MOD_ACTION_RETURN, /* invalid */
1434 MOD_ACTION_RETURN, /* userlock */
1441 MOD_ACTION_RETURN, /* reject */
1443 MOD_ACTION_RETURN, /* ok */
1444 MOD_ACTION_RETURN, /* handled */
1445 MOD_ACTION_RETURN, /* invalid */
1446 MOD_ACTION_RETURN, /* userlock */
1447 MOD_ACTION_RETURN, /* notfound */
1448 MOD_ACTION_RETURN, /* noop */
1449 MOD_ACTION_RETURN /* updated */
1453 MOD_ACTION_RETURN, /* reject */
1455 MOD_ACTION_RETURN, /* ok */
1456 MOD_ACTION_RETURN, /* handled */
1457 MOD_ACTION_RETURN, /* invalid */
1458 MOD_ACTION_RETURN, /* userlock */
1460 MOD_ACTION_RETURN, /* noop */
1461 MOD_ACTION_RETURN /* updated */
1468 MOD_ACTION_RETURN, /* reject */
1469 MOD_ACTION_RETURN, /* fail */
1471 MOD_ACTION_RETURN, /* handled */
1472 MOD_ACTION_RETURN, /* invalid */
1473 MOD_ACTION_RETURN, /* userlock */
1480 MOD_ACTION_RETURN, /* reject */
1482 MOD_ACTION_RETURN, /* ok */
1483 MOD_ACTION_RETURN, /* handled */
1484 MOD_ACTION_RETURN, /* invalid */
1485 MOD_ACTION_RETURN, /* userlock */
1486 MOD_ACTION_RETURN, /* notfound */
1487 MOD_ACTION_RETURN, /* noop */
1488 MOD_ACTION_RETURN /* updated */
1492 MOD_ACTION_RETURN, /* reject */
1494 MOD_ACTION_RETURN, /* ok */
1495 MOD_ACTION_RETURN, /* handled */
1496 MOD_ACTION_RETURN, /* invalid */
1497 MOD_ACTION_RETURN, /* userlock */
1499 MOD_ACTION_RETURN, /* noop */
1500 MOD_ACTION_RETURN /* updated */
1509 MOD_ACTION_RETURN, /* reject */
1510 MOD_ACTION_RETURN, /* fail */
1512 MOD_ACTION_RETURN, /* handled */
1513 MOD_ACTION_RETURN, /* invalid */
1514 MOD_ACTION_RETURN, /* userlock */
1521 MOD_ACTION_RETURN, /* reject */
1523 MOD_ACTION_RETURN, /* ok */
1524 MOD_ACTION_RETURN, /* handled */
1525 MOD_ACTION_RETURN, /* invalid */
1526 MOD_ACTION_RETURN, /* userlock */
1527 MOD_ACTION_RETURN, /* notfound */
1528 MOD_ACTION_RETURN, /* noop */
1529 MOD_ACTION_RETURN /* updated */
1533 MOD_ACTION_RETURN, /* reject */
1535 MOD_ACTION_RETURN, /* ok */
1536 MOD_ACTION_RETURN, /* handled */
1537 MOD_ACTION_RETURN, /* invalid */
1538 MOD_ACTION_RETURN, /* userlock */
1540 MOD_ACTION_RETURN, /* noop */
1541 MOD_ACTION_RETURN /* updated */
1548 MOD_ACTION_RETURN, /* reject */
1549 MOD_ACTION_RETURN, /* fail */
1551 MOD_ACTION_RETURN, /* handled */
1552 MOD_ACTION_RETURN, /* invalid */
1553 MOD_ACTION_RETURN, /* userlock */
1560 MOD_ACTION_RETURN, /* reject */
1562 MOD_ACTION_RETURN, /* ok */
1563 MOD_ACTION_RETURN, /* handled */
1564 MOD_ACTION_RETURN, /* invalid */
1565 MOD_ACTION_RETURN, /* userlock */
1566 MOD_ACTION_RETURN, /* notfound */
1567 MOD_ACTION_RETURN, /* noop */
1568 MOD_ACTION_RETURN /* updated */
1572 MOD_ACTION_RETURN, /* reject */
1574 MOD_ACTION_RETURN, /* ok */
1575 MOD_ACTION_RETURN, /* handled */
1576 MOD_ACTION_RETURN, /* invalid */
1577 MOD_ACTION_RETURN, /* userlock */
1579 MOD_ACTION_RETURN, /* noop */
1580 MOD_ACTION_RETURN /* updated */
1586 /** Validate and fixup a map that's part of an update section.
1588 * @param map to validate.
1589 * @param ctx data to pass to fixup function (currently unused).
1590 * @return 0 if valid else -1.
1592 int modcall_fixup_update(value_pair_map_t *map, UNUSED void *ctx)
1594 CONF_PAIR *cp = cf_itemtopair(map->ci);
1597 * Anal-retentive checks.
1599 if (DEBUG_ENABLED2) {
1600 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->lhs->name[0] != '&')) {
1601 WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
1602 cf_pair_filename(cp), cf_pair_lineno(cp),
1603 map->lhs->name, fr_int2str(fr_tokens, map->op, "<INVALID>"));
1606 if ((map->rhs->type == TMPL_TYPE_ATTR) && (map->rhs->name[0] != '&')) {
1607 WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
1608 cf_pair_filename(cp), cf_pair_lineno(cp),
1609 fr_int2str(fr_tokens, map->op, "<INVALID>"), map->rhs->name);
1614 * Values used by unary operators should be literal ANY
1616 * We then free the template and alloc a NULL one instead.
1618 if (map->op == T_OP_CMP_FALSE) {
1619 if ((map->rhs->type != TMPL_TYPE_LITERAL) || (strcmp(map->rhs->name, "ANY") != 0)) {
1620 WARN("%s[%d] Wildcard deletion MUST use '!* ANY'",
1621 cf_pair_filename(cp), cf_pair_lineno(cp));
1624 tmpl_free(&map->rhs);
1626 map->rhs = tmpl_alloc(map, TMPL_TYPE_NULL, NULL, 0);
1630 * Lots of sanity checks for insane people...
1634 * What exactly where you expecting to happen here?
1636 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
1637 (map->rhs->type == TMPL_TYPE_LIST)) {
1638 cf_log_err(map->ci, "Can't copy list into an attribute");
1643 * Depending on the attribute type, some operators are disallowed.
1645 if (map->lhs->type == TMPL_TYPE_ATTR) {
1648 cf_log_err(map->ci, "Invalid operator for attribute");
1657 case T_OP_CMP_FALSE:
1663 if (map->lhs->type == TMPL_TYPE_LIST) {
1665 * Can't copy an xlat expansion or literal into a list,
1666 * we don't know what type of attribute we'd need
1669 * The only exception is where were using a unary
1672 if (map->op != T_OP_CMP_FALSE) switch (map->rhs->type) {
1673 case TMPL_TYPE_XLAT:
1674 case TMPL_TYPE_LITERAL:
1675 cf_log_err(map->ci, "Can't copy value into list (we don't know which attribute to create)");
1683 * Only += and :=, and !* operators are supported
1687 case T_OP_CMP_FALSE:
1691 if ((map->rhs->type != TMPL_TYPE_LIST) &&
1692 (map->rhs->type != TMPL_TYPE_EXEC)) {
1693 cf_log_err(map->ci, "Invalid source for list assignment '%s += ...'", map->lhs->name);
1699 if (map->rhs->type == TMPL_TYPE_EXEC) {
1700 WARN("%s[%d] Please change ':=' to '=' for list assignment",
1701 cf_pair_filename(cp), cf_pair_lineno(cp));
1704 if (map->rhs->type != TMPL_TYPE_LIST) {
1705 cf_log_err(map->ci, "Invalid source for list assignment '%s := ...'", map->lhs->name);
1711 if (map->rhs->type != TMPL_TYPE_EXEC) {
1712 cf_log_err(map->ci, "Invalid source for list assignment '%s = ...'", map->lhs->name);
1718 cf_log_err(map->ci, "Operator \"%s\" not allowed for list assignment",
1719 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1725 * If the map has a unary operator there's no further
1726 * processing we need to, as RHS is unused.
1728 if (map->op == T_OP_CMP_FALSE) return 0;
1731 * If LHS is an attribute, and RHS is a literal, we can
1732 * preparse the information into a TMPL_TYPE_DATA.
1734 * Unless it's a unary operator in which case we
1737 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->rhs->type == TMPL_TYPE_LITERAL)) {
1739 * It's a literal string, just copy it.
1740 * Don't escape anything.
1742 if (!cf_new_escape &&
1743 (map->lhs->tmpl_da->type == PW_TYPE_STRING) &&
1744 (cf_pair_value_type(cp) == T_SINGLE_QUOTED_STRING)) {
1745 tmpl_cast_in_place_str(map->rhs);
1747 if (!tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da)) {
1748 cf_log_err(map->ci, "%s", fr_strerror());
1753 * Fixup LHS da if it doesn't match the type
1756 if (map->lhs->tmpl_da->type != map->rhs->tmpl_data_type) {
1757 DICT_ATTR const *da;
1759 da = dict_attrbytype(map->lhs->tmpl_da->attr, map->lhs->tmpl_da->vendor,
1760 map->rhs->tmpl_data_type);
1762 fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1763 fr_int2str(dict_attr_types, map->rhs->tmpl_data_type,
1764 "<INVALID>"), map->lhs->tmpl_da->name);
1767 map->lhs->tmpl_da = da;
1770 } /* else we can't precompile the data */
1777 static modcallable *do_compile_modupdate(modcallable *parent, UNUSED rlm_components_t component,
1778 CONF_SECTION *cs, char const *name2)
1782 modcallable *csingle;
1784 value_pair_map_t *head;
1787 * This looks at cs->name2 to determine which list to update
1789 rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
1790 if (rcode < 0) return NULL; /* message already printed */
1792 cf_log_err_cs(cs, "'update' sections cannot be empty");
1796 g = talloc_zero(parent, modgroup);
1797 csingle = mod_grouptocallable(g);
1799 csingle->parent = parent;
1800 csingle->next = NULL;
1803 csingle->name = name2;
1807 csingle->type = MOD_UPDATE;
1808 csingle->method = component;
1810 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
1811 sizeof(csingle->actions));
1813 g->grouptype = GROUPTYPE_SIMPLE;
1816 g->map = talloc_steal(g, head);
1822 static modcallable *do_compile_modswitch (modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1827 bool had_seen_default = false;
1828 modcallable *csingle;
1831 value_pair_tmpl_t *vpt;
1833 name2 = cf_section_name2(cs);
1835 cf_log_err_cs(cs, "You must specify a variable to switch over for 'switch'");
1839 if (!cf_item_find_next(cs, NULL)) {
1840 cf_log_err_cs(cs, "'switch' statements cannot be empty");
1845 * Create the template. If we fail, AND it's a bare word
1846 * with &Foo-Bar, it MAY be an attribute defined by a
1847 * module. Allow it for now. The pass2 checks below
1850 type = cf_section_name2_type(cs);
1851 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
1852 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1853 char *spaces, *text;
1855 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1857 cf_log_err_cs(cs, "Syntax error");
1858 cf_log_err_cs(cs, "%s", name2);
1859 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1861 talloc_free(spaces);
1868 * Otherwise a NULL vpt may refer to an attribute defined
1869 * by a module. That is checked in pass 2.
1873 * Walk through the children of the switch section,
1874 * ensuring that they're all 'case' statements
1876 for (ci = cf_item_find_next(cs, NULL);
1878 ci = cf_item_find_next(cs, ci)) {
1879 CONF_SECTION *subcs;
1882 if (!cf_item_is_section(ci)) {
1883 if (!cf_item_is_pair(ci)) continue;
1885 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1890 subcs = cf_itemtosection(ci); /* can't return NULL */
1891 name1 = cf_section_name1(subcs);
1893 if (strcmp(name1, "case") != 0) {
1894 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1899 name2 = cf_section_name2(subcs);
1901 if (!had_seen_default) {
1902 had_seen_default = true;
1906 cf_log_err(ci, "Cannot have two 'default' case statements");
1912 csingle = do_compile_modgroup(parent, component, cs,
1921 g = mod_callabletogroup(csingle);
1922 g->vpt = talloc_steal(g, vpt);
1927 static modcallable *do_compile_modcase(modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1931 modcallable *csingle;
1933 value_pair_tmpl_t *vpt;
1935 if (!parent || (parent->type != MOD_SWITCH)) {
1936 cf_log_err_cs(cs, "\"case\" statements may only appear within a \"switch\" section");
1941 * case THING means "match THING"
1942 * case means "match anything"
1944 name2 = cf_section_name2(cs);
1949 type = cf_section_name2_type(cs);
1951 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
1952 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1953 char *spaces, *text;
1955 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1957 cf_log_err_cs(cs, "Syntax error");
1958 cf_log_err_cs(cs, "%s", name2);
1959 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1961 talloc_free(spaces);
1968 * Otherwise a NULL vpt may refer to an attribute defined
1969 * by a module. That is checked in pass 2.
1976 csingle = do_compile_modgroup(parent, component, cs,
1986 * The interpretor expects this to be NULL for the
1987 * default case. do_compile_modgroup sets it to name2,
1988 * unless name2 is NULL, in which case it sets it to name1.
1990 csingle->name = name2;
1992 g = mod_callabletogroup(csingle);
1993 g->vpt = talloc_steal(g, vpt);
1996 * Set all of it's codes to return, so that
1997 * when we pick a 'case' statement, we don't
1998 * fall through to processing the next one.
2000 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2001 csingle->actions[i] = MOD_ACTION_RETURN;
2007 static modcallable *do_compile_modforeach(modcallable *parent,
2008 UNUSED rlm_components_t component, CONF_SECTION *cs)
2012 modcallable *csingle;
2015 value_pair_tmpl_t *vpt;
2017 name2 = cf_section_name2(cs);
2020 "You must specify an attribute to loop over in 'foreach'");
2024 if (!cf_item_find_next(cs, NULL)) {
2025 cf_log_err_cs(cs, "'foreach' blocks cannot be empty");
2030 * Create the template. If we fail, AND it's a bare word
2031 * with &Foo-Bar, it MAY be an attribute defined by a
2032 * module. Allow it for now. The pass2 checks below
2035 type = cf_section_name2_type(cs);
2036 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
2037 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
2038 char *spaces, *text;
2040 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
2042 cf_log_err_cs(cs, "Syntax error");
2043 cf_log_err_cs(cs, "%s", name2);
2044 cf_log_err_cs(cs, "%s^ %s", spaces, text);
2046 talloc_free(spaces);
2052 if (vpt && (vpt->type != TMPL_TYPE_ATTR)) {
2053 cf_log_err_cs(cs, "MUST use attribute reference in 'foreach'");
2058 * Fix up the template to iterate over all instances of
2059 * the attribute. In a perfect consistent world, users would do
2060 * foreach &attr[*], but that's taking the consistency thing a bit far.
2062 if (vpt && (vpt->type == TMPL_TYPE_ATTR)) {
2063 vpt->tmpl_num = NUM_ALL;
2066 csingle = do_compile_modgroup(parent, component, cs,
2067 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2075 g = mod_callabletogroup(csingle);
2081 static modcallable *do_compile_modbreak(modcallable *parent,
2082 rlm_components_t component, CONF_ITEM const *ci)
2084 CONF_SECTION const *cs = NULL;
2086 for (cs = cf_item_parent(ci);
2088 cs = cf_item_parent(cf_sectiontoitem(cs))) {
2089 if (strcmp(cf_section_name1(cs), "foreach") == 0) {
2095 cf_log_err(ci, "'break' can only be used in a 'foreach' section");
2099 return do_compile_modgroup(parent, component, NULL,
2100 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2105 static modcallable *do_compile_modserver(modcallable *parent,
2106 rlm_components_t component, CONF_ITEM *ci,
2111 modcallable *csingle;
2112 CONF_SECTION *subcs;
2115 subcs = cf_section_sub_find_name2(cs, comp2str[component], NULL);
2117 cf_log_err(ci, "Server %s has no %s section",
2118 server, comp2str[component]);
2122 mr = talloc_zero(parent, modref);
2124 csingle = mod_reftocallable(mr);
2125 csingle->parent = parent;
2126 csingle->next = NULL;
2127 csingle->name = name;
2128 csingle->type = MOD_REFERENCE;
2129 csingle->method = component;
2131 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2132 sizeof(csingle->actions));
2134 mr->ref_name = strdup(server);
2140 static modcallable *do_compile_modxlat(modcallable *parent,
2141 rlm_components_t component, char const *fmt)
2143 modcallable *csingle;
2146 mx = talloc_zero(parent, modxlat);
2148 csingle = mod_xlattocallable(mx);
2149 csingle->parent = parent;
2150 csingle->next = NULL;
2151 csingle->name = "expand";
2152 csingle->type = MOD_XLAT;
2153 csingle->method = component;
2155 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2156 sizeof(csingle->actions));
2158 mx->xlat_name = strdup(fmt);
2159 if (fmt[0] != '%') {
2163 strcpy(mx->xlat_name, fmt + 1);
2164 p = strrchr(mx->xlat_name, '`');
2172 * redundant, etc. can refer to modules or groups, but not much else.
2174 static int all_children_are_modules(CONF_SECTION *cs, char const *name)
2178 for (ci=cf_item_find_next(cs, NULL);
2180 ci=cf_item_find_next(cs, ci)) {
2182 * If we're a redundant, etc. group, then the
2183 * intention is to call modules, rather than
2184 * processing logic. These checks aren't
2185 * *strictly* necessary, but they keep the users
2186 * from doing crazy things.
2188 if (cf_item_is_section(ci)) {
2189 CONF_SECTION *subcs = cf_itemtosection(ci);
2190 char const *name1 = cf_section_name1(subcs);
2192 if ((strcmp(name1, "if") == 0) ||
2193 (strcmp(name1, "else") == 0) ||
2194 (strcmp(name1, "elsif") == 0) ||
2195 (strcmp(name1, "update") == 0) ||
2196 (strcmp(name1, "switch") == 0) ||
2197 (strcmp(name1, "case") == 0)) {
2198 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
2205 if (cf_item_is_pair(ci)) {
2206 CONF_PAIR *cp = cf_itemtopair(ci);
2207 if (cf_pair_value(cp) != NULL) {
2209 "Entry with no value is invalid");
2220 * Compile one entry of a module call.
2222 static modcallable *do_compile_modsingle(modcallable *parent,
2223 rlm_components_t component, CONF_ITEM *ci,
2225 char const **modname)
2227 char const *modrefname;
2229 modcallable *csingle;
2230 module_instance_t *this;
2231 CONF_SECTION *cs, *subcs, *modules;
2232 char const *realname;
2234 if (cf_item_is_section(ci)) {
2237 cs = cf_itemtosection(ci);
2238 modrefname = cf_section_name1(cs);
2239 name2 = cf_section_name2(cs);
2240 if (!name2) name2 = "";
2243 * group{}, redundant{}, or append{} may appear
2244 * where a single module instance was expected.
2245 * In that case, we hand it off to
2248 if (strcmp(modrefname, "group") == 0) {
2250 return do_compile_modgroup(parent, component, cs,
2252 grouptype, MOD_GROUP);
2254 } else if (strcmp(modrefname, "redundant") == 0) {
2257 if (!all_children_are_modules(cs, modrefname)) {
2261 return do_compile_modgroup(parent, component, cs,
2262 GROUPTYPE_REDUNDANT,
2263 grouptype, MOD_GROUP);
2265 } else if (strcmp(modrefname, "append") == 0) {
2267 return do_compile_modgroup(parent, component, cs,
2269 grouptype, MOD_GROUP);
2271 } else if (strcmp(modrefname, "load-balance") == 0) {
2274 if (!all_children_are_modules(cs, modrefname)) {
2278 return do_compile_modgroup(parent, component, cs,
2280 grouptype, MOD_LOAD_BALANCE);
2282 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2285 if (!all_children_are_modules(cs, modrefname)) {
2289 return do_compile_modgroup(parent, component, cs,
2290 GROUPTYPE_REDUNDANT,
2291 grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2294 } else if (strcmp(modrefname, "if") == 0) {
2295 if (!cf_section_name2(cs)) {
2296 cf_log_err(ci, "'if' without condition");
2301 csingle= do_compile_modgroup(parent, component, cs,
2304 if (!csingle) return NULL;
2309 } else if (strcmp(modrefname, "elsif") == 0) {
2311 ((parent->type == MOD_LOAD_BALANCE) ||
2312 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2313 cf_log_err(ci, "'elsif' cannot be used in this section");
2317 if (!cf_section_name2(cs)) {
2318 cf_log_err(ci, "'elsif' without condition");
2323 return do_compile_modgroup(parent, component, cs,
2325 grouptype, MOD_ELSIF);
2327 } else if (strcmp(modrefname, "else") == 0) {
2329 ((parent->type == MOD_LOAD_BALANCE) ||
2330 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2331 cf_log_err(ci, "'else' cannot be used in this section section");
2335 if (cf_section_name2(cs)) {
2336 cf_log_err(ci, "Cannot have conditions on 'else'");
2341 return do_compile_modgroup(parent, component, cs,
2343 grouptype, MOD_ELSE);
2345 } else if (strcmp(modrefname, "update") == 0) {
2348 return do_compile_modupdate(parent, component, cs,
2351 } else if (strcmp(modrefname, "switch") == 0) {
2354 return do_compile_modswitch (parent, component, cs);
2356 } else if (strcmp(modrefname, "case") == 0) {
2359 return do_compile_modcase(parent, component, cs);
2361 } else if (strcmp(modrefname, "foreach") == 0) {
2364 return do_compile_modforeach(parent, component, cs);
2367 } /* else it's something like sql { fail = 1 ...} */
2369 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2373 * Else it's a module reference, with updated return
2378 CONF_PAIR *cp = cf_itemtopair(ci);
2379 modrefname = cf_pair_attr(cp);
2382 * Actions (ok = 1), etc. are orthogonal to just
2383 * about everything else.
2385 if (cf_pair_value(cp) != NULL) {
2386 cf_log_err(ci, "Entry is not a reference to a module");
2390 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2391 (modrefname[0] == '`')) {
2392 return do_compile_modxlat(parent, component,
2397 * See if the module is a virtual one. If so,
2398 * return that, rather than doing anything here.
2401 cs = cf_section_find("instantiate");
2402 if (cs) subcs = cf_section_sub_find_name2(cs, NULL,
2405 (cs = cf_section_find("policy")) != NULL) {
2408 snprintf(buffer, sizeof(buffer), "%s.%s",
2409 modrefname, comp2str[component]);
2412 * Prefer name.section, then name.
2414 subcs = cf_section_sub_find_name2(cs, NULL,
2417 subcs = cf_section_sub_find_name2(cs, NULL,
2423 * Allow policies to over-ride module names.
2424 * i.e. the "sql" policy can do some extra things,
2425 * and then call the "sql" module.
2427 for (loop = cf_item_parent(ci);
2429 loop = cf_item_parent(cf_sectiontoitem(loop))) {
2430 if (loop == subcs) {
2437 * redundant foo {} is a single.
2439 if (cf_section_name2(subcs)) {
2440 return do_compile_modsingle(parent,
2442 cf_sectiontoitem(subcs),
2447 * foo {} is a group.
2449 return do_compile_modgroup(parent,
2453 grouptype, MOD_GROUP);
2459 if (strcmp(modrefname, "break") == 0) {
2460 return do_compile_modbreak(parent, component, ci);
2463 if (strcmp(modrefname, "return") == 0) {
2464 return do_compile_modgroup(parent, component, NULL,
2465 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2471 * Not a virtual module. It must be a real module.
2473 modules = cf_section_find("modules");
2475 realname = modrefname;
2479 * Try to load the optional module.
2481 if (realname[0] == '-') realname++;
2484 * As of v3, only known modules are in the
2485 * "modules" section.
2487 if (cf_section_sub_find_name2(modules, NULL, realname)) {
2488 this = find_module_instance(modules, realname, true);
2489 if (!this && (realname != modrefname)) {
2495 * We were asked to MAYBE load it and it
2496 * doesn't exist. Return a soft error.
2498 if (realname != modrefname) {
2499 *modname = modrefname;
2510 * Maybe it's module.method
2512 p = strrchr(modrefname, '.');
2513 if (p) for (i = RLM_COMPONENT_AUTH;
2514 i < RLM_COMPONENT_COUNT;
2516 if (strcmp(p + 1, comp2str[i]) == 0) {
2519 strlcpy(buffer, modrefname, sizeof(buffer));
2520 buffer[p - modrefname] = '\0';
2523 this = find_module_instance(modules, buffer, true);
2524 if (this && !this->entry->module->methods[i]) {
2526 cf_log_err(ci, "Module %s has no such method %s", buffer, comp2str[i]);
2535 * Call a server. This should really be deleted...
2537 if (strncmp(modrefname, "server[", 7) == 0) {
2540 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2541 p = strrchr(buffer, ']');
2542 if (!p || p[1] != '\0' || (p == buffer)) {
2543 cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2548 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2550 cf_log_err(ci, "No such server \"%s\".", buffer);
2554 return do_compile_modserver(parent, component, ci,
2555 modrefname, cs, buffer);
2559 cf_log_err(ci, "Failed to find \"%s\" in the \"modules\" section.", modrefname);
2560 cf_log_err(ci, "Please verify that the configuration exists in the file %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2565 * We know it's all OK, allocate the structures, and fill
2568 single = talloc_zero(parent, modsingle);
2569 csingle = mod_singletocallable(single);
2570 csingle->parent = parent;
2571 csingle->next = NULL;
2572 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2573 memcpy(csingle->actions, defaultactions[component][grouptype],
2574 sizeof csingle->actions);
2575 } else { /* inside Auth-Type has different rules */
2576 memcpy(csingle->actions, defaultactions[RLM_COMPONENT_AUTZ][grouptype],
2577 sizeof csingle->actions);
2579 rad_assert(modrefname != NULL);
2580 csingle->name = realname;
2581 csingle->type = MOD_SINGLE;
2582 csingle->method = component;
2585 * Singles can override the actions, virtual modules cannot.
2587 * FIXME: We may want to re-visit how to do this...
2588 * maybe a csingle as a ref?
2590 if (cf_item_is_section(ci)) {
2593 cs = cf_itemtosection(ci);
2594 for (csi=cf_item_find_next(cs, NULL);
2596 csi=cf_item_find_next(cs, csi)) {
2598 if (cf_item_is_section(csi)) {
2599 cf_log_err(csi, "Subsection of module instance call not allowed");
2600 talloc_free(csingle);
2604 if (!cf_item_is_pair(csi)) continue;
2606 if (!compile_action(csingle, cf_itemtopair(csi))) {
2607 talloc_free(csingle);
2614 * Bail out if the module in question does not supply the
2617 if (!this->entry->module->methods[component]) {
2618 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2619 comp2str[component]);
2620 talloc_free(csingle);
2624 single->modinst = this;
2625 *modname = this->entry->module->name;
2629 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2630 modcallable **parent,
2631 rlm_components_t component, CONF_ITEM *ci,
2632 char const **modname)
2639 CONF_SECTION *parentcs;
2641 g = talloc_zero(ctx, modgroup);
2642 memset(g, 0, sizeof(*g));
2643 g->grouptype = GROUPTYPE_SIMPLE;
2644 c = mod_grouptocallable(g);
2647 defaultactions[component][GROUPTYPE_SIMPLE],
2648 sizeof(c->actions));
2650 parentcs = cf_item_parent(ci);
2651 c->name = cf_section_name2(parentcs);
2653 c->name = cf_section_name1(parentcs);
2656 c->type = MOD_GROUP;
2657 c->method = component;
2660 *parent = mod_grouptocallable(g);
2663 ret = do_compile_modsingle(*parent, component, ci,
2666 dump_tree(component, ret);
2672 * Internal compile group code.
2674 static modcallable *do_compile_modgroup(modcallable *parent,
2675 rlm_components_t component, CONF_SECTION *cs,
2676 int grouptype, int parentgrouptype, int mod_type)
2683 g = talloc_zero(parent, modgroup);
2684 g->grouptype = grouptype;
2688 c = mod_grouptocallable(g);
2692 memset(c->actions, 0, sizeof(c->actions));
2694 if (!cs) { /* only for "break" and "return" */
2700 * Remember the name for printing, etc.
2702 * FIXME: We may also want to put the names into a
2703 * rbtree, so that groups can reference each other...
2705 c->name = cf_section_name2(cs);
2707 c->name = cf_section_name1(cs);
2708 if ((strcmp(c->name, "group") == 0) ||
2709 (strcmp(c->name, "redundant") == 0)) {
2711 } else if (c->type == MOD_GROUP) {
2712 c->type = MOD_POLICY;
2718 * Do load-time optimizations
2720 if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2723 rad_assert(parent != NULL);
2725 if (c->type == MOD_IF) {
2726 g->cond = cf_data_find(g->cs, "if");
2727 rad_assert(g->cond != NULL);
2730 if (g->cond->type == COND_TYPE_FALSE) {
2731 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2732 group_name[g->mc.type],
2733 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2737 } else if (c->type == MOD_ELSIF) {
2739 g->cond = cf_data_find(g->cs, "if");
2740 rad_assert(g->cond != NULL);
2742 rad_assert(parent != NULL);
2743 p = mod_callabletogroup(parent);
2745 rad_assert(p->tail != NULL);
2747 f = mod_callabletogroup(p->tail);
2748 rad_assert((f->mc.type == MOD_IF) ||
2749 (f->mc.type == MOD_ELSIF));
2752 * If we took the previous condition, we
2753 * don't need to take this one.
2755 * We reset our condition to 'true', so
2756 * that subsequent sections can check
2757 * that they don't need to be executed.
2759 if (f->cond->type == COND_TYPE_TRUE) {
2761 INFO(" # Skipping contents of '%s' as previous '%s' is always 'true' -- %s:%d",
2762 group_name[g->mc.type],
2763 group_name[f->mc.type],
2764 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2771 rad_assert(c->type == MOD_ELSE);
2773 rad_assert(parent != NULL);
2774 p = mod_callabletogroup(parent);
2776 rad_assert(p->tail != NULL);
2778 f = mod_callabletogroup(p->tail);
2779 rad_assert((f->mc.type == MOD_IF) ||
2780 (f->mc.type == MOD_ELSIF));
2783 * If we took the previous condition, we
2784 * don't need to take this one.
2786 if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2790 * Else we need to compile this section
2796 * Loop over the children of this group.
2798 for (ci=cf_item_find_next(cs, NULL);
2800 ci=cf_item_find_next(cs, ci)) {
2803 * Sections are references to other groups, or
2804 * to modules with updated return codes.
2806 if (cf_item_is_section(ci)) {
2807 char const *junk = NULL;
2808 modcallable *single;
2809 CONF_SECTION *subcs = cf_itemtosection(ci);
2811 single = do_compile_modsingle(c, component, ci,
2814 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2815 cf_section_name1(subcs));
2819 add_child(g, single);
2821 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2825 char const *attr, *value;
2826 CONF_PAIR *cp = cf_itemtopair(ci);
2828 attr = cf_pair_attr(cp);
2829 value = cf_pair_value(cp);
2832 * A CONF_PAIR is either a module
2833 * instance with no actions
2837 modcallable *single;
2838 char const *junk = NULL;
2840 single = do_compile_modsingle(c,
2846 if (cf_item_is_pair(ci) &&
2847 cf_pair_attr(cf_itemtopair(ci))[0] == '-') {
2852 "Failed to parse \"%s\" entry.",
2857 add_child(g, single);
2860 * Or a module instance with action.
2862 } else if (!compile_action(c, cp)) {
2865 } /* else it worked */
2871 * Set the default actions, if they haven't already been
2874 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2875 if (!c->actions[i]) {
2876 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2877 c->actions[i] = defaultactions[component][parentgrouptype][i];
2878 } else { /* inside Auth-Type has different rules */
2879 c->actions[i] = defaultactions[RLM_COMPONENT_AUTZ][parentgrouptype][i];
2889 if (grouptype != GROUPTYPE_REDUNDANT) break;
2892 case MOD_LOAD_BALANCE:
2893 case MOD_REDUNDANT_LOAD_BALANCE:
2895 cf_log_err_cs(g->cs, "%s sections cannot be empty",
2896 cf_section_name1(g->cs));
2903 * FIXME: If there are no children, return NULL?
2905 return mod_grouptocallable(g);
2908 modcallable *compile_modgroup(modcallable *parent,
2909 rlm_components_t component, CONF_SECTION *cs)
2911 modcallable *ret = do_compile_modgroup(parent, component, cs,
2913 GROUPTYPE_SIMPLE, MOD_GROUP);
2915 if (debug_flag > 3) {
2916 modcall_debug(ret, 2);
2922 void add_to_modcallable(modcallable *parent, modcallable *this)
2926 rad_assert(this != NULL);
2927 rad_assert(parent != NULL);
2929 g = mod_callabletogroup(parent);
2936 static bool pass2_xlat_compile(CONF_ITEM const *ci, value_pair_tmpl_t **pvpt, bool convert,
2937 DICT_ATTR const *da)
2943 value_pair_tmpl_t *vpt;
2947 rad_assert(vpt->type == TMPL_TYPE_XLAT);
2949 fmt = talloc_typed_strdup(vpt, vpt->name);
2950 slen = xlat_tokenize(vpt, fmt, &head, &error);
2953 char *spaces, *text;
2955 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
2957 cf_log_err(ci, "Failed parsing expanded string:");
2958 cf_log_err(ci, "%s", text);
2959 cf_log_err(ci, "%s^ %s", spaces, error);
2961 talloc_free(spaces);
2967 * Convert %{Attribute-Name} to &Attribute-Name
2970 value_pair_tmpl_t *attr;
2972 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
2975 * If it's a virtual attribute, leave it
2978 if (attr->tmpl_da->flags.virtual) {
2984 * If the attribute is of incompatible
2985 * type, leave it alone.
2987 if (da && (da->type != attr->tmpl_da->type)) {
2992 if (cf_item_is_pair(ci)) {
2993 CONF_PAIR *cp = cf_itemtopair(ci);
2995 WARN("%s[%d] Please change %%{%s} to &%s",
2996 cf_pair_filename(cp), cf_pair_lineno(cp),
2997 attr->name, attr->name);
2999 CONF_SECTION *cs = cf_itemtosection(ci);
3001 WARN("%s[%d] Please change %%{%s} to &%s",
3002 cf_section_filename(cs), cf_section_lineno(cs),
3003 attr->name, attr->name);
3012 * Re-write it to be a pre-parsed XLAT structure.
3014 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3015 vpt->tmpl_xlat = head;
3022 static bool pass2_regex_compile(CONF_ITEM const *ci, value_pair_tmpl_t *vpt)
3027 rad_assert(vpt->type == TMPL_TYPE_REGEX);
3030 * It's a dynamic expansion. We can't expand the string,
3031 * but we can pre-parse it as an xlat struct. In that
3032 * case, we convert it to a pre-compiled XLAT.
3034 * This is a little more complicated than it needs to be
3035 * because radius_evaluate_map() keys off of the src
3036 * template type, instead of the operators. And, the
3037 * pass2_xlat_compile() function expects to get passed an
3038 * XLAT instead of a REGEX.
3040 if (strchr(vpt->name, '%')) {
3041 vpt->type = TMPL_TYPE_XLAT;
3042 return pass2_xlat_compile(ci, &vpt, false, NULL);
3045 slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3046 vpt->tmpl_iflag, vpt->tmpl_mflag, false);
3048 char *spaces, *text;
3050 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3052 cf_log_err(ci, "Invalid regular expression:");
3053 cf_log_err(ci, "%s", text);
3054 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3056 talloc_free(spaces);
3062 vpt->type = TMPL_TYPE_REGEX_STRUCT;
3063 vpt->tmpl_preg = preg;
3069 static bool pass2_fixup_undefined(CONF_ITEM const *ci, value_pair_tmpl_t *vpt)
3071 DICT_ATTR const *da;
3073 rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3075 da = dict_attrbyname(vpt->tmpl_unknown_name);
3077 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3082 vpt->type = TMPL_TYPE_ATTR;
3086 static bool pass2_callback(UNUSED void *ctx, fr_cond_t *c)
3088 value_pair_map_t *map;
3090 if (c->type == COND_TYPE_EXISTS) {
3091 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3092 return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3095 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3098 * The existence check might have been &Foo-Bar,
3099 * where Foo-Bar is defined by a module.
3101 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3102 if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3103 c->pass2_fixup = PASS2_FIXUP_NONE;
3109 * Maps have a paircompare fixup applied to them.
3110 * Others get ignored.
3112 if (c->pass2_fixup == PASS2_FIXUP_NONE) {
3113 if (c->type == COND_TYPE_MAP) {
3121 map = c->data.map; /* shorter */
3126 * Where "foo" is dynamically defined.
3128 if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3129 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3130 map->lhs->tmpl_da->vendor,
3132 cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3133 map->lhs->tmpl_da->name,
3139 * These guys can't have a paircompare fixup applied.
3141 c->pass2_fixup = PASS2_FIXUP_NONE;
3145 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3146 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3147 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3150 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3151 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3154 c->pass2_fixup = PASS2_FIXUP_NONE;
3159 * Just in case someone adds a new fixup later.
3161 rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3162 (c->pass2_fixup == PASS2_PAIRCOMPARE));
3167 if (map->lhs->type == TMPL_TYPE_XLAT) {
3169 * Don't compile the LHS to an attribute
3170 * reference for now. When we do that, we've got
3171 * to check the RHS for type-specific data, and
3172 * parse it to a TMPL_TYPE_DATA.
3174 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3179 if (map->rhs->type == TMPL_TYPE_XLAT) {
3181 * Convert the RHS to an attribute reference only
3182 * if the LHS is an attribute reference, AND is
3183 * of the same type as the RHS.
3185 * We can fix this when the code in evaluate.c
3186 * can handle strings on the LHS, and attributes
3187 * on the RHS. For now, the code in parser.c
3190 if (map->lhs->type == TMPL_TYPE_ATTR) {
3191 DICT_ATTR const *da = c->cast;
3193 if (!c->cast) da = map->lhs->tmpl_da;
3195 if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3200 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3207 * Convert bare refs to %{Foreach-Variable-N}
3209 if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3210 (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3213 value_pair_tmpl_t *vpt;
3215 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3216 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3217 T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST);
3219 char *spaces, *text;
3221 fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3223 cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3224 cf_log_err(map->ci, "%s", fmt);
3225 cf_log_err(map->ci, "%s^ %s", spaces, text);
3227 talloc_free(spaces);
3233 talloc_free(map->lhs);
3238 if (map->rhs->type == TMPL_TYPE_REGEX) {
3239 if (!pass2_regex_compile(map->ci, map->rhs)) {
3243 rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3247 * Only attributes can have a paircompare registered, and
3248 * they can only be with the current REQUEST, and only
3249 * with the request pairs.
3251 if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3252 (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3253 (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3257 if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3259 if (map->rhs->type == TMPL_TYPE_ATTR) {
3260 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3265 if (map->rhs->type == TMPL_TYPE_REGEX) {
3266 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3272 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3277 if (map->op != T_OP_CMP_EQ) {
3278 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3284 * Mark it as requiring a paircompare() call, instead of
3287 c->pass2_fixup = PASS2_PAIRCOMPARE;
3294 * Compile the RHS of update sections to xlat_exp_t
3296 static bool modcall_pass2_update(modgroup *g)
3298 value_pair_map_t *map;
3300 for (map = g->map; map != NULL; map = map->next) {
3301 if (map->rhs->type == TMPL_TYPE_XLAT) {
3302 rad_assert(map->rhs->tmpl_xlat == NULL);
3305 * FIXME: compile to attribute && handle
3306 * the conversion in map_to_vp().
3308 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3313 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3315 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3316 cf_log_err(map->ci, "Attribute '%s' is unknown", map->lhs->name);
3320 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3321 cf_log_err(map->ci, "Attribute '%s' is unknown", map->rhs->name);
3331 * Do a second-stage pass on compiling the modules.
3333 bool modcall_pass2(modcallable *mc)
3340 for (c = mc; c != NULL; c = c->next) {
3348 g = mod_callabletogroup(c);
3349 if (g->done_pass2) return true;
3351 name2 = cf_section_name2(g->cs);
3353 c->debug_name = group_name[c->type];
3355 c->debug_name = talloc_asprintf(c, "update %s", name2);
3358 if (!modcall_pass2_update(g)) {
3361 g->done_pass2 = true;
3364 case MOD_XLAT: /* @todo: pre-parse xlat's */
3371 c->debug_name = c->name;
3372 break; /* do nothing */
3377 g = mod_callabletogroup(c);
3378 if (g->done_pass2) return true;
3380 name2 = cf_section_name2(g->cs);
3381 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3384 * Don't walk over these.
3386 if ((g->cond->type == COND_TYPE_TRUE) ||
3387 (g->cond->type == COND_TYPE_FALSE)) {
3392 * The compilation code takes care of
3393 * simplifying 'true' and 'false'
3394 * conditions. For others, we have to do
3395 * a second pass to parse && compile xlats.
3397 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3401 if (!modcall_pass2(g->children)) return false;
3402 g->done_pass2 = true;
3408 g = mod_callabletogroup(c);
3409 if (g->done_pass2) return true;
3411 name2 = cf_section_name2(g->cs);
3412 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3415 * We had &Foo-Bar, where Foo-Bar is
3416 * defined by a module.
3419 rad_assert(c->name != NULL);
3420 rad_assert(c->name[0] == '&');
3421 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3423 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3424 cf_section_name2_type(g->cs),
3425 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3427 char *spaces, *text;
3430 fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3432 cf_log_err_cs(g->cs, "Syntax error");
3433 cf_log_err_cs(g->cs, "%s", c->name);
3434 cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3436 talloc_free(spaces);
3446 * Statically compile xlats
3448 if (g->vpt->type == TMPL_TYPE_XLAT) {
3449 if (!pass2_xlat_compile(cf_sectiontoitem(g->cs),
3450 &g->vpt, true, NULL)) {
3458 * We may have: switch Foo-Bar {
3460 * where Foo-Bar is an attribute defined
3461 * by a module. Since there's no leading
3462 * &, it's parsed as a literal. But if
3463 * we can parse it as an attribute,
3464 * switch to using that.
3466 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3467 value_pair_tmpl_t *vpt;
3469 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3470 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3471 if (slen < 0) goto parse_error;
3472 if (vpt->type == TMPL_TYPE_ATTR) {
3473 talloc_free(g->vpt);
3481 * Warn about old-style configuration.
3483 * DEPRECATED: switch User-Name { ...
3484 * ALLOWED : switch &User-Name { ...
3486 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3487 (c->name[0] != '&')) {
3488 WARN("%s[%d]: Please change %s to &%s",
3489 cf_section_filename(g->cs),
3490 cf_section_lineno(g->cs),
3495 if (!modcall_pass2(g->children)) return false;
3496 g->done_pass2 = true;
3500 g = mod_callabletogroup(c);
3501 if (g->done_pass2) return true;
3503 name2 = cf_section_name2(g->cs);
3505 c->debug_name = group_name[c->type];
3507 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3510 rad_assert(c->parent != NULL);
3511 rad_assert(c->parent->type == MOD_SWITCH);
3514 * The statement may refer to an
3515 * attribute which doesn't exist until
3516 * all of the modules have been loaded.
3517 * Check for that now.
3519 if (!g->vpt && c->name &&
3520 (c->name[0] == '&') &&
3521 (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3522 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3523 cf_section_name2_type(g->cs),
3524 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3525 if (slen < 0) goto parse_error;
3529 * We have "case {...}". There's no
3530 * argument, so we don't need to check
3533 if (!g->vpt) goto do_children;
3536 * Do type-specific checks on the case statement
3538 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3541 f = mod_callabletogroup(mc->parent);
3542 rad_assert(f->vpt != NULL);
3545 * We're switching over an
3546 * attribute. Check that the
3549 if (f->vpt->type == TMPL_TYPE_ATTR) {
3550 rad_assert(f->vpt->tmpl_da != NULL);
3552 if (!tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da)) {
3553 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3563 * Compile and sanity check xlat
3566 if (g->vpt->type == TMPL_TYPE_XLAT) {
3569 f = mod_callabletogroup(mc->parent);
3570 rad_assert(f->vpt != NULL);
3573 * Don't expand xlat's into an
3574 * attribute of a different type.
3576 if (f->vpt->type == TMPL_TYPE_ATTR) {
3577 if (!pass2_xlat_compile(cf_sectiontoitem(g->cs),
3578 &g->vpt, true, f->vpt->tmpl_da)) {
3582 if (!pass2_xlat_compile(cf_sectiontoitem(g->cs),
3583 &g->vpt, true, NULL)) {
3589 if (!modcall_pass2(g->children)) return false;
3590 g->done_pass2 = true;
3594 g = mod_callabletogroup(c);
3595 if (g->done_pass2) return true;
3597 name2 = cf_section_name2(g->cs);
3598 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3601 * Already parsed, handle the children.
3603 if (g->vpt) goto check_children;
3606 * We had &Foo-Bar, where Foo-Bar is
3607 * defined by a module.
3609 rad_assert(c->name != NULL);
3610 rad_assert(c->name[0] == '&');
3611 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3614 * The statement may refer to an
3615 * attribute which doesn't exist until
3616 * all of the modules have been loaded.
3617 * Check for that now.
3619 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3620 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3621 if (slen < 0) goto parse_error;
3624 rad_assert(g->vpt->type == TMPL_TYPE_ATTR);
3625 if (g->vpt->tmpl_num != NUM_ALL) {
3626 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3629 if (!modcall_pass2(g->children)) return false;
3630 g->done_pass2 = true;
3634 c->debug_name = group_name[c->type];
3638 g = mod_callabletogroup(c);
3639 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], cf_section_name1(g->cs));
3644 case MOD_LOAD_BALANCE:
3645 case MOD_REDUNDANT_LOAD_BALANCE:
3646 c->debug_name = group_name[c->type];
3651 g = mod_callabletogroup(c);
3653 c->debug_name = mc->name; /* for authorize, etc. */
3655 } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3656 char const *name1 = cf_section_name1(g->cs);
3658 if (strcmp(name1, group_name[c->type]) != 0) {
3659 c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3663 if (g->done_pass2) return true;
3664 if (!modcall_pass2(g->children)) return false;
3665 g->done_pass2 = true;
3673 void modcall_debug(modcallable *mc, int depth)
3677 value_pair_map_t *map;
3680 for (this = mc; this != NULL; this = this->next) {
3681 switch (this->type) {
3686 modsingle *single = mod_callabletosingle(this);
3688 DEBUG("%.*s%s", depth, modcall_spaces,
3689 single->modinst->name);
3695 g = mod_callabletogroup(this);
3696 DEBUG("%.*s%s {", depth, modcall_spaces,
3697 group_name[this->type]);
3699 for (map = g->map; map != NULL; map = map->next) {
3700 map_prints(buffer, sizeof(buffer), map);
3701 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3704 DEBUG("%.*s}", depth, modcall_spaces);
3708 g = mod_callabletogroup(this);
3709 DEBUG("%.*s%s {", depth, modcall_spaces,
3710 group_name[this->type]);
3711 modcall_debug(g->children, depth + 1);
3712 DEBUG("%.*s}", depth, modcall_spaces);
3717 g = mod_callabletogroup(this);
3718 fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3719 DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3720 group_name[this->type], buffer);
3721 modcall_debug(g->children, depth + 1);
3722 DEBUG("%.*s}", depth, modcall_spaces);
3727 g = mod_callabletogroup(this);
3728 tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3729 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3730 group_name[this->type], buffer);
3731 modcall_debug(g->children, depth + 1);
3732 DEBUG("%.*s}", depth, modcall_spaces);
3737 g = mod_callabletogroup(this);
3738 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3739 group_name[this->type], this->name);
3740 modcall_debug(g->children, depth + 1);
3741 DEBUG("%.*s}", depth, modcall_spaces);
3745 DEBUG("%.*sbreak", depth, modcall_spaces);
3750 g = mod_callabletogroup(this);
3751 DEBUG("%.*s%s {", depth, modcall_spaces,
3752 group_name[this->type]);
3753 modcall_debug(g->children, depth + 1);
3754 DEBUG("%.*s}", depth, modcall_spaces);
3758 case MOD_LOAD_BALANCE:
3759 case MOD_REDUNDANT_LOAD_BALANCE:
3760 g = mod_callabletogroup(this);
3761 DEBUG("%.*s%s {", depth, modcall_spaces,
3762 group_name[this->type]);
3763 modcall_debug(g->children, depth + 1);
3764 DEBUG("%.*s}", depth, modcall_spaces);