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, (void *)radius_get_vp, i)) {
636 if (foreach_depth < 0) {
637 REDEBUG("foreach Nesting too deep!");
638 result = RLM_MODULE_FAIL;
639 goto calculate_result;
643 * Copy the VPs from the original request, this ensures deterministic
644 * behaviour if someone decides to add or remove VPs in the set were
647 if (tmpl_copy_vps(request, &vps, request, g->vpt) < 0) { /* nothing to loop over */
649 result = RLM_MODULE_NOOP;
651 goto calculate_result;
654 rad_assert(vps != NULL);
655 fr_cursor_init(©, &vps);
657 RDEBUG2("foreach %s ", c->name);
660 * This is the actual body of the foreach loop
662 for (vp = fr_cursor_first(©);
664 vp = fr_cursor_next(©)) {
666 if (fr_debug_flag >= 2) {
669 vp_prints_value(buffer, sizeof(buffer), vp, '"');
670 RDEBUG2("# Foreach-Variable-%d = %s", foreach_depth, buffer);
675 * Add the vp to the request, so that
676 * xlat.c, xlat_foreach() can find it.
678 request_data_add(request, (void *)radius_get_vp, foreach_depth, &vp, false);
681 * Initialize the childs stack frame.
684 next->c = g->children;
685 next->result = entry->result;
689 if (!modcall_recurse(request, component, depth + 1, next)) {
694 * We've unwound to the enclosing
695 * "foreach". Stop the unwinding.
697 if (next->unwind == MOD_FOREACH) {
702 * Unwind all the way.
704 if (next->unwind == MOD_RETURN) {
705 entry->unwind = MOD_RETURN;
708 } /* loop over VPs */
711 * Free the copied vps and the request data
712 * If we don't remove the request data, something could call
713 * the xlat outside of a foreach loop and trigger a segv.
716 request_data_get(request, (void *)radius_get_vp, foreach_depth);
718 rad_assert(next != NULL);
719 result = next->result;
720 priority = next->priority;
722 goto calculate_result;
726 * Break out of a "foreach" loop, or return from a nested
729 if ((c->type == MOD_BREAK) || (c->type == MOD_RETURN)) {
733 RDEBUG2("%s", group_name[c->type]);
735 for (i = 8; i >= 0; i--) {
736 copy_p = request_data_get(request, (void *)radius_get_vp, i);
738 if (c->type == MOD_BREAK) {
739 RDEBUG2("# break Foreach-Variable-%d", i);
746 * Leave result / priority on the stack, and stop processing the section.
748 entry->unwind = c->type;
752 #endif /* WITH_UNLANG */
755 * Child is a group that has children of it's own.
757 if ((c->type == MOD_GROUP) || (c->type == MOD_POLICY)
759 || (c->type == MOD_CASE)
767 g = mod_callabletogroup(c);
770 * This should really have been caught in the
771 * compiler, and the node never generated. But
772 * doing that requires changing it's API so that
773 * it returns a flag instead of the compiled
777 RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
782 modcall_child(request, component,
783 depth + 1, entry, g->children,
786 goto calculate_result;
790 if (c->type == MOD_SWITCH) {
791 modcallable *this, *found, *null_case;
795 value_pair_map_t map;
796 value_pair_tmpl_t vpt;
800 g = mod_callabletogroup(c);
802 memset(&cond, 0, sizeof(cond));
803 memset(&map, 0, sizeof(map));
805 cond.type = COND_TYPE_MAP;
806 cond.data.map = ↦
808 map.op = T_OP_CMP_EQ;
809 map.ci = cf_section_to_item(g->cs);
811 rad_assert(g->vpt != NULL);
813 null_case = found = NULL;
817 * The attribute doesn't exist. We can skip
818 * directly to the default 'case' statement.
820 if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
822 for (this = g->children; this; this = this->next) {
823 rad_assert(this->type == MOD_CASE);
825 h = mod_callabletogroup(this);
826 if (h->vpt) continue;
836 * Expand the template if necessary, so that it
837 * is evaluated once instead of for each 'case'
840 if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
841 (g->vpt->type == TMPL_TYPE_XLAT) ||
842 (g->vpt->type == TMPL_TYPE_EXEC)) {
845 if (tmpl_expand(request, &p, request, g->vpt) < 0) goto find_null_case;
847 tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, talloc_array_length(data.strvalue) - 1);
851 * Find either the exact matching name, or the
852 * "case {...}" statement.
854 for (this = g->children; this; this = this->next) {
855 rad_assert(this->type == MOD_CASE);
857 h = mod_callabletogroup(this);
860 * Remember the default case
863 if (!null_case) null_case = this;
868 * If we're switching over an attribute
869 * AND we haven't pre-parsed the data for
870 * the case statement, then cast the data
871 * to the type of the attribute.
873 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
874 (h->vpt->type != TMPL_TYPE_DATA)) {
877 cond.cast = g->vpt->tmpl_da;
880 * Remove unnecessary casting.
882 if ((h->vpt->type == TMPL_TYPE_ATTR) &&
883 (g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
888 * Use the pre-expanded string.
890 } else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
891 (g->vpt->type == TMPL_TYPE_XLAT) ||
892 (g->vpt->type == TMPL_TYPE_EXEC)) {
898 * Else evaluate the 'switch' statement.
906 if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
913 if (!found) found = null_case;
916 talloc_free(data.ptr);
917 modcall_child(request, component, depth + 1, entry, found, &result);
919 goto calculate_result;
923 if ((c->type == MOD_LOAD_BALANCE) ||
924 (c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
926 modcallable *this, *found;
931 g = mod_callabletogroup(c);
933 rad_assert(g->children != NULL);
936 * Choose a child at random.
938 for (this = g->children; this; this = this->next) {
941 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
948 if (c->type == MOD_LOAD_BALANCE) {
949 modcall_child(request, component,
950 depth + 1, entry, found,
957 modcall_child(request, component,
958 depth + 1, entry, this,
960 if (this->actions[result] == MOD_ACTION_RETURN) {
966 if (!this) this = g->children;
967 } while (this != found);
970 goto calculate_result;
971 } /* MOD_LOAD_BALANCE */
974 * Reference another virtual server.
976 * This should really be deleted, and replaced with a
977 * more abstracted / functional version.
979 if (c->type == MOD_REFERENCE) {
980 modref *mr = mod_callabletoref(c);
981 char const *server = request->server;
983 if (server == mr->ref_name) {
984 RWDEBUG("Suppressing recursive call to server %s", server);
988 request->server = mr->ref_name;
989 RDEBUG("server %s { # nested call", mr->ref_name);
990 result = indexed_modcall(component, 0, request);
991 RDEBUG("} # server %s with nested call", mr->ref_name);
992 request->server = server;
993 goto calculate_result;
994 } /* MOD_REFERENCE */
997 * xlat a string without doing anything else
999 * This should really be deleted, and replaced with a
1000 * more abstracted / functional version.
1002 if (c->type == MOD_XLAT) {
1003 modxlat *mx = mod_callabletoxlat(c);
1007 radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
1009 RDEBUG("`%s`", mx->xlat_name);
1010 radius_exec_program(NULL, 0, NULL, request, mx->xlat_name, request->packet->vps,
1011 false, true, EXEC_TIMEOUT);
1018 * Add new module types here.
1023 RDEBUG("(%s, %d) ? (%s, %d)",
1024 fr_int2str(mod_rcode_table, result, "<invalid>"),
1026 fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
1031 rad_assert(result != RLM_MODULE_UNKNOWN);
1034 * The child's action says return. Do so.
1036 if ((c->actions[result] == MOD_ACTION_RETURN) &&
1038 entry->result = result;
1043 * If "reject", break out of the loop and return
1046 if (c->actions[result] == MOD_ACTION_REJECT) {
1047 entry->result = RLM_MODULE_REJECT;
1052 * The array holds a default priority for this return
1053 * code. Grab it in preference to any unset priority.
1056 priority = c->actions[result];
1060 * We're higher than any previous priority, remember this
1061 * return code and priority.
1063 if (priority > entry->priority) {
1064 entry->result = result;
1065 entry->priority = priority;
1070 * If we're processing a "case" statement, we return once
1071 * it's done, rather than going to the next "case" statement.
1073 if (c->type == MOD_CASE) goto finish;
1077 * If we've been told to stop processing
1080 if (entry->unwind == MOD_BREAK) {
1081 RDEBUG2("# unwind to enclosing foreach");
1086 if (entry->unwind == MOD_RETURN) {
1091 entry->c = entry->c->next;
1093 if (entry->c) goto redo;
1104 /** Call a module, iteratively, with a local stack, rather than recursively
1106 * What did Paul Graham say about Lisp...?
1108 int modcall(rlm_components_t component, modcallable *c, REQUEST *request)
1110 modcall_stack_entry_t stack[MODCALL_STACK_MAX];
1113 memset(stack, 0, sizeof(stack));
1116 * Set up the initial stack frame.
1119 stack[0].result = default_component_results[component];
1120 stack[0].priority = 0;
1121 stack[0].unwind = 0;
1124 * Call the main handler.
1126 if (!modcall_recurse(request, component, 0, &stack[0])) {
1127 return RLM_MODULE_FAIL;
1131 * Return the result.
1133 return stack[0].result;
1138 static char const *action2str(int action)
1140 static char buf[32];
1141 if(action==MOD_ACTION_RETURN)
1143 if(action==MOD_ACTION_REJECT)
1145 snprintf(buf, sizeof buf, "%d", action);
1149 /* If you suspect a bug in the parser, you'll want to use these dump
1150 * functions. dump_tree should reproduce a whole tree exactly as it was found
1151 * in radiusd.conf, but in long form (all actions explicitly defined) */
1152 static void dump_mc(modcallable *c, int indent)
1156 if(c->type==MOD_SINGLE) {
1157 modsingle *single = mod_callabletosingle(c);
1158 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1159 single->modinst->name);
1160 } else if ((c->type > MOD_SINGLE) && (c->type <= MOD_POLICY)) {
1161 modgroup *g = mod_callabletogroup(c);
1163 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1164 group_name[c->type]);
1165 for(p = g->children;p;p = p->next)
1166 dump_mc(p, indent+1);
1167 } /* else ignore it for now */
1169 for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
1170 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
1171 fr_int2str(mod_rcode_table, i, "<invalid>"),
1172 action2str(c->actions[i]));
1175 DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
1178 static void dump_tree(rlm_components_t comp, modcallable *c)
1180 DEBUG("[%s]", comp2str[comp]);
1184 #define dump_tree(a, b)
1187 /* These are the default actions. For each component, the group{} block
1188 * behaves like the code from the old module_*() function. redundant{} and
1189 * append{} are based on my guesses of what they will be used for. --Pac. */
1191 defaultactions[RLM_COMPONENT_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1197 MOD_ACTION_RETURN, /* reject */
1199 MOD_ACTION_RETURN, /* ok */
1200 MOD_ACTION_RETURN, /* handled */
1202 MOD_ACTION_RETURN, /* userlock */
1203 MOD_ACTION_RETURN, /* notfound */
1209 MOD_ACTION_RETURN, /* reject */
1211 MOD_ACTION_RETURN, /* ok */
1212 MOD_ACTION_RETURN, /* handled */
1213 MOD_ACTION_RETURN, /* invalid */
1214 MOD_ACTION_RETURN, /* userlock */
1215 MOD_ACTION_RETURN, /* notfound */
1216 MOD_ACTION_RETURN, /* noop */
1217 MOD_ACTION_RETURN /* updated */
1221 MOD_ACTION_RETURN, /* reject */
1223 MOD_ACTION_RETURN, /* ok */
1224 MOD_ACTION_RETURN, /* handled */
1225 MOD_ACTION_RETURN, /* invalid */
1226 MOD_ACTION_RETURN, /* userlock */
1228 MOD_ACTION_RETURN, /* noop */
1229 MOD_ACTION_RETURN /* updated */
1236 MOD_ACTION_RETURN, /* reject */
1237 MOD_ACTION_RETURN, /* fail */
1239 MOD_ACTION_RETURN, /* handled */
1240 MOD_ACTION_RETURN, /* invalid */
1241 MOD_ACTION_RETURN, /* userlock */
1248 MOD_ACTION_RETURN, /* reject */
1250 MOD_ACTION_RETURN, /* ok */
1251 MOD_ACTION_RETURN, /* handled */
1252 MOD_ACTION_RETURN, /* invalid */
1253 MOD_ACTION_RETURN, /* userlock */
1254 MOD_ACTION_RETURN, /* notfound */
1255 MOD_ACTION_RETURN, /* noop */
1256 MOD_ACTION_RETURN /* updated */
1260 MOD_ACTION_RETURN, /* reject */
1262 MOD_ACTION_RETURN, /* ok */
1263 MOD_ACTION_RETURN, /* handled */
1264 MOD_ACTION_RETURN, /* invalid */
1265 MOD_ACTION_RETURN, /* userlock */
1267 MOD_ACTION_RETURN, /* noop */
1268 MOD_ACTION_RETURN /* updated */
1275 MOD_ACTION_RETURN, /* reject */
1276 MOD_ACTION_RETURN, /* fail */
1278 MOD_ACTION_RETURN, /* handled */
1279 MOD_ACTION_RETURN, /* invalid */
1280 MOD_ACTION_RETURN, /* userlock */
1281 MOD_ACTION_RETURN, /* notfound */
1287 MOD_ACTION_RETURN, /* reject */
1289 MOD_ACTION_RETURN, /* ok */
1290 MOD_ACTION_RETURN, /* handled */
1291 MOD_ACTION_RETURN, /* invalid */
1292 MOD_ACTION_RETURN, /* userlock */
1293 MOD_ACTION_RETURN, /* notfound */
1294 MOD_ACTION_RETURN, /* noop */
1295 MOD_ACTION_RETURN /* updated */
1299 MOD_ACTION_RETURN, /* reject */
1301 MOD_ACTION_RETURN, /* ok */
1302 MOD_ACTION_RETURN, /* handled */
1303 MOD_ACTION_RETURN, /* invalid */
1304 MOD_ACTION_RETURN, /* userlock */
1306 MOD_ACTION_RETURN, /* noop */
1307 MOD_ACTION_RETURN /* updated */
1314 MOD_ACTION_RETURN, /* reject */
1315 MOD_ACTION_RETURN, /* fail */
1317 MOD_ACTION_RETURN, /* handled */
1318 MOD_ACTION_RETURN, /* invalid */
1319 MOD_ACTION_RETURN, /* userlock */
1320 MOD_ACTION_RETURN, /* notfound */
1328 MOD_ACTION_RETURN, /* ok */
1329 MOD_ACTION_RETURN, /* handled */
1338 MOD_ACTION_RETURN, /* reject */
1340 MOD_ACTION_RETURN, /* ok */
1341 MOD_ACTION_RETURN, /* handled */
1342 MOD_ACTION_RETURN, /* invalid */
1343 MOD_ACTION_RETURN, /* userlock */
1345 MOD_ACTION_RETURN, /* noop */
1346 MOD_ACTION_RETURN /* updated */
1353 MOD_ACTION_RETURN, /* reject */
1355 MOD_ACTION_RETURN, /* ok */
1356 MOD_ACTION_RETURN, /* handled */
1357 MOD_ACTION_RETURN, /* invalid */
1358 MOD_ACTION_RETURN, /* userlock */
1359 MOD_ACTION_RETURN, /* notfound */
1360 MOD_ACTION_RETURN, /* noop */
1361 MOD_ACTION_RETURN /* updated */
1365 MOD_ACTION_RETURN, /* reject */
1367 MOD_ACTION_RETURN, /* ok */
1368 MOD_ACTION_RETURN, /* handled */
1369 MOD_ACTION_RETURN, /* invalid */
1370 MOD_ACTION_RETURN, /* userlock */
1371 MOD_ACTION_RETURN, /* notfound */
1372 MOD_ACTION_RETURN, /* noop */
1373 MOD_ACTION_RETURN /* updated */
1377 MOD_ACTION_RETURN, /* reject */
1379 MOD_ACTION_RETURN, /* ok */
1380 MOD_ACTION_RETURN, /* handled */
1381 MOD_ACTION_RETURN, /* invalid */
1382 MOD_ACTION_RETURN, /* userlock */
1383 MOD_ACTION_RETURN, /* notfound */
1384 MOD_ACTION_RETURN, /* noop */
1385 MOD_ACTION_RETURN /* updated */
1392 MOD_ACTION_RETURN, /* reject */
1393 MOD_ACTION_RETURN, /* fail */
1395 MOD_ACTION_RETURN, /* handled */
1396 MOD_ACTION_RETURN, /* invalid */
1397 MOD_ACTION_RETURN, /* userlock */
1404 MOD_ACTION_RETURN, /* reject */
1406 MOD_ACTION_RETURN, /* ok */
1407 MOD_ACTION_RETURN, /* handled */
1408 MOD_ACTION_RETURN, /* invalid */
1409 MOD_ACTION_RETURN, /* userlock */
1410 MOD_ACTION_RETURN, /* notfound */
1411 MOD_ACTION_RETURN, /* noop */
1412 MOD_ACTION_RETURN /* updated */
1416 MOD_ACTION_RETURN, /* reject */
1418 MOD_ACTION_RETURN, /* ok */
1419 MOD_ACTION_RETURN, /* handled */
1420 MOD_ACTION_RETURN, /* invalid */
1421 MOD_ACTION_RETURN, /* userlock */
1423 MOD_ACTION_RETURN, /* noop */
1424 MOD_ACTION_RETURN /* updated */
1431 MOD_ACTION_RETURN, /* reject */
1432 MOD_ACTION_RETURN, /* fail */
1434 MOD_ACTION_RETURN, /* handled */
1435 MOD_ACTION_RETURN, /* invalid */
1436 MOD_ACTION_RETURN, /* userlock */
1443 MOD_ACTION_RETURN, /* reject */
1445 MOD_ACTION_RETURN, /* ok */
1446 MOD_ACTION_RETURN, /* handled */
1447 MOD_ACTION_RETURN, /* invalid */
1448 MOD_ACTION_RETURN, /* userlock */
1449 MOD_ACTION_RETURN, /* notfound */
1450 MOD_ACTION_RETURN, /* noop */
1451 MOD_ACTION_RETURN /* updated */
1455 MOD_ACTION_RETURN, /* reject */
1457 MOD_ACTION_RETURN, /* ok */
1458 MOD_ACTION_RETURN, /* handled */
1459 MOD_ACTION_RETURN, /* invalid */
1460 MOD_ACTION_RETURN, /* userlock */
1462 MOD_ACTION_RETURN, /* noop */
1463 MOD_ACTION_RETURN /* updated */
1470 MOD_ACTION_RETURN, /* reject */
1471 MOD_ACTION_RETURN, /* fail */
1473 MOD_ACTION_RETURN, /* handled */
1474 MOD_ACTION_RETURN, /* invalid */
1475 MOD_ACTION_RETURN, /* userlock */
1482 MOD_ACTION_RETURN, /* reject */
1484 MOD_ACTION_RETURN, /* ok */
1485 MOD_ACTION_RETURN, /* handled */
1486 MOD_ACTION_RETURN, /* invalid */
1487 MOD_ACTION_RETURN, /* userlock */
1488 MOD_ACTION_RETURN, /* notfound */
1489 MOD_ACTION_RETURN, /* noop */
1490 MOD_ACTION_RETURN /* updated */
1494 MOD_ACTION_RETURN, /* reject */
1496 MOD_ACTION_RETURN, /* ok */
1497 MOD_ACTION_RETURN, /* handled */
1498 MOD_ACTION_RETURN, /* invalid */
1499 MOD_ACTION_RETURN, /* userlock */
1501 MOD_ACTION_RETURN, /* noop */
1502 MOD_ACTION_RETURN /* updated */
1511 MOD_ACTION_RETURN, /* reject */
1512 MOD_ACTION_RETURN, /* fail */
1514 MOD_ACTION_RETURN, /* handled */
1515 MOD_ACTION_RETURN, /* invalid */
1516 MOD_ACTION_RETURN, /* userlock */
1523 MOD_ACTION_RETURN, /* reject */
1525 MOD_ACTION_RETURN, /* ok */
1526 MOD_ACTION_RETURN, /* handled */
1527 MOD_ACTION_RETURN, /* invalid */
1528 MOD_ACTION_RETURN, /* userlock */
1529 MOD_ACTION_RETURN, /* notfound */
1530 MOD_ACTION_RETURN, /* noop */
1531 MOD_ACTION_RETURN /* updated */
1535 MOD_ACTION_RETURN, /* reject */
1537 MOD_ACTION_RETURN, /* ok */
1538 MOD_ACTION_RETURN, /* handled */
1539 MOD_ACTION_RETURN, /* invalid */
1540 MOD_ACTION_RETURN, /* userlock */
1542 MOD_ACTION_RETURN, /* noop */
1543 MOD_ACTION_RETURN /* updated */
1550 MOD_ACTION_RETURN, /* reject */
1551 MOD_ACTION_RETURN, /* fail */
1553 MOD_ACTION_RETURN, /* handled */
1554 MOD_ACTION_RETURN, /* invalid */
1555 MOD_ACTION_RETURN, /* userlock */
1562 MOD_ACTION_RETURN, /* reject */
1564 MOD_ACTION_RETURN, /* ok */
1565 MOD_ACTION_RETURN, /* handled */
1566 MOD_ACTION_RETURN, /* invalid */
1567 MOD_ACTION_RETURN, /* userlock */
1568 MOD_ACTION_RETURN, /* notfound */
1569 MOD_ACTION_RETURN, /* noop */
1570 MOD_ACTION_RETURN /* updated */
1574 MOD_ACTION_RETURN, /* reject */
1576 MOD_ACTION_RETURN, /* ok */
1577 MOD_ACTION_RETURN, /* handled */
1578 MOD_ACTION_RETURN, /* invalid */
1579 MOD_ACTION_RETURN, /* userlock */
1581 MOD_ACTION_RETURN, /* noop */
1582 MOD_ACTION_RETURN /* updated */
1588 /** Validate and fixup a map that's part of an update section.
1590 * @param map to validate.
1591 * @param ctx data to pass to fixup function (currently unused).
1592 * @return 0 if valid else -1.
1594 int modcall_fixup_update(value_pair_map_t *map, UNUSED void *ctx)
1596 CONF_PAIR *cp = cf_item_to_pair(map->ci);
1599 * Anal-retentive checks.
1601 if (DEBUG_ENABLED2) {
1602 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->lhs->name[0] != '&')) {
1603 WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
1604 cf_pair_filename(cp), cf_pair_lineno(cp),
1605 map->lhs->name, fr_int2str(fr_tokens, map->op, "<INVALID>"));
1608 if ((map->rhs->type == TMPL_TYPE_ATTR) && (map->rhs->name[0] != '&')) {
1609 WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
1610 cf_pair_filename(cp), cf_pair_lineno(cp),
1611 fr_int2str(fr_tokens, map->op, "<INVALID>"), map->rhs->name);
1616 * Values used by unary operators should be literal ANY
1618 * We then free the template and alloc a NULL one instead.
1620 if (map->op == T_OP_CMP_FALSE) {
1621 if ((map->rhs->type != TMPL_TYPE_LITERAL) || (strcmp(map->rhs->name, "ANY") != 0)) {
1622 WARN("%s[%d] Wildcard deletion MUST use '!* ANY'",
1623 cf_pair_filename(cp), cf_pair_lineno(cp));
1626 tmpl_free(&map->rhs);
1628 map->rhs = tmpl_alloc(map, TMPL_TYPE_NULL, NULL, 0);
1632 * Lots of sanity checks for insane people...
1636 * What exactly where you expecting to happen here?
1638 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
1639 (map->rhs->type == TMPL_TYPE_LIST)) {
1640 cf_log_err(map->ci, "Can't copy list into an attribute");
1645 * Depending on the attribute type, some operators are disallowed.
1647 if (map->lhs->type == TMPL_TYPE_ATTR) {
1650 cf_log_err(map->ci, "Invalid operator for attribute");
1659 case T_OP_CMP_FALSE:
1665 if (map->lhs->type == TMPL_TYPE_LIST) {
1667 * Can't copy an xlat expansion or literal into a list,
1668 * we don't know what type of attribute we'd need
1671 * The only exception is where were using a unary
1674 if (map->op != T_OP_CMP_FALSE) switch (map->rhs->type) {
1675 case TMPL_TYPE_XLAT:
1676 case TMPL_TYPE_LITERAL:
1677 cf_log_err(map->ci, "Can't copy value into list (we don't know which attribute to create)");
1685 * Only += and :=, and !* operators are supported
1689 case T_OP_CMP_FALSE:
1693 if ((map->rhs->type != TMPL_TYPE_LIST) &&
1694 (map->rhs->type != TMPL_TYPE_EXEC)) {
1695 cf_log_err(map->ci, "Invalid source for list assignment '%s += ...'", map->lhs->name);
1701 if (map->rhs->type == TMPL_TYPE_EXEC) {
1702 WARN("%s[%d] Please change ':=' to '=' for list assignment",
1703 cf_pair_filename(cp), cf_pair_lineno(cp));
1706 if (map->rhs->type != TMPL_TYPE_LIST) {
1707 cf_log_err(map->ci, "Invalid source for list assignment '%s := ...'", map->lhs->name);
1713 if (map->rhs->type != TMPL_TYPE_EXEC) {
1714 cf_log_err(map->ci, "Invalid source for list assignment '%s = ...'", map->lhs->name);
1720 cf_log_err(map->ci, "Operator \"%s\" not allowed for list assignment",
1721 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1727 * If the map has a unary operator there's no further
1728 * processing we need to, as RHS is unused.
1730 if (map->op == T_OP_CMP_FALSE) return 0;
1733 * If LHS is an attribute, and RHS is a literal, we can
1734 * preparse the information into a TMPL_TYPE_DATA.
1736 * Unless it's a unary operator in which case we
1739 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->rhs->type == TMPL_TYPE_LITERAL)) {
1741 * It's a literal string, just copy it.
1742 * Don't escape anything.
1744 if (!cf_new_escape &&
1745 (map->lhs->tmpl_da->type == PW_TYPE_STRING) &&
1746 (cf_pair_value_type(cp) == T_SINGLE_QUOTED_STRING)) {
1747 tmpl_cast_in_place_str(map->rhs);
1749 if (!tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da)) {
1750 cf_log_err(map->ci, "%s", fr_strerror());
1755 * Fixup LHS da if it doesn't match the type
1758 if (map->lhs->tmpl_da->type != map->rhs->tmpl_data_type) {
1759 DICT_ATTR const *da;
1761 da = dict_attrbytype(map->lhs->tmpl_da->attr, map->lhs->tmpl_da->vendor,
1762 map->rhs->tmpl_data_type);
1764 fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1765 fr_int2str(dict_attr_types, map->rhs->tmpl_data_type,
1766 "<INVALID>"), map->lhs->tmpl_da->name);
1769 map->lhs->tmpl_da = da;
1772 } /* else we can't precompile the data */
1779 static modcallable *do_compile_modupdate(modcallable *parent, rlm_components_t component,
1780 CONF_SECTION *cs, char const *name2)
1784 modcallable *csingle;
1786 value_pair_map_t *head;
1789 * This looks at cs->name2 to determine which list to update
1791 rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
1792 if (rcode < 0) return NULL; /* message already printed */
1794 cf_log_err_cs(cs, "'update' sections cannot be empty");
1798 g = talloc_zero(parent, modgroup);
1799 csingle = mod_grouptocallable(g);
1801 csingle->parent = parent;
1802 csingle->next = NULL;
1805 csingle->name = name2;
1809 csingle->type = MOD_UPDATE;
1810 csingle->method = component;
1812 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
1813 sizeof(csingle->actions));
1815 g->grouptype = GROUPTYPE_SIMPLE;
1818 g->map = talloc_steal(g, head);
1824 static modcallable *do_compile_modswitch (modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1829 bool had_seen_default = false;
1830 modcallable *csingle;
1833 value_pair_tmpl_t *vpt;
1835 name2 = cf_section_name2(cs);
1837 cf_log_err_cs(cs, "You must specify a variable to switch over for 'switch'");
1841 if (!cf_item_find_next(cs, NULL)) {
1842 cf_log_err_cs(cs, "'switch' statements cannot be empty");
1847 * Create the template. If we fail, AND it's a bare word
1848 * with &Foo-Bar, it MAY be an attribute defined by a
1849 * module. Allow it for now. The pass2 checks below
1852 type = cf_section_name2_type(cs);
1853 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
1854 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1855 char *spaces, *text;
1857 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1859 cf_log_err_cs(cs, "Syntax error");
1860 cf_log_err_cs(cs, "%s", name2);
1861 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1863 talloc_free(spaces);
1870 * Otherwise a NULL vpt may refer to an attribute defined
1871 * by a module. That is checked in pass 2.
1875 * Walk through the children of the switch section,
1876 * ensuring that they're all 'case' statements
1878 for (ci = cf_item_find_next(cs, NULL);
1880 ci = cf_item_find_next(cs, ci)) {
1881 CONF_SECTION *subcs;
1884 if (!cf_item_is_section(ci)) {
1885 if (!cf_item_is_pair(ci)) continue;
1887 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1892 subcs = cf_item_to_section(ci); /* can't return NULL */
1893 name1 = cf_section_name1(subcs);
1895 if (strcmp(name1, "case") != 0) {
1896 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1901 name2 = cf_section_name2(subcs);
1903 if (!had_seen_default) {
1904 had_seen_default = true;
1908 cf_log_err(ci, "Cannot have two 'default' case statements");
1914 csingle = do_compile_modgroup(parent, component, cs,
1923 g = mod_callabletogroup(csingle);
1924 g->vpt = talloc_steal(g, vpt);
1929 static modcallable *do_compile_modcase(modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1933 modcallable *csingle;
1935 value_pair_tmpl_t *vpt;
1937 if (!parent || (parent->type != MOD_SWITCH)) {
1938 cf_log_err_cs(cs, "\"case\" statements may only appear within a \"switch\" section");
1943 * case THING means "match THING"
1944 * case means "match anything"
1946 name2 = cf_section_name2(cs);
1951 type = cf_section_name2_type(cs);
1953 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
1954 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1955 char *spaces, *text;
1957 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1959 cf_log_err_cs(cs, "Syntax error");
1960 cf_log_err_cs(cs, "%s", name2);
1961 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1963 talloc_free(spaces);
1970 * Otherwise a NULL vpt may refer to an attribute defined
1971 * by a module. That is checked in pass 2.
1978 csingle = do_compile_modgroup(parent, component, cs,
1988 * The interpretor expects this to be NULL for the
1989 * default case. do_compile_modgroup sets it to name2,
1990 * unless name2 is NULL, in which case it sets it to name1.
1992 csingle->name = name2;
1994 g = mod_callabletogroup(csingle);
1995 g->vpt = talloc_steal(g, vpt);
1998 * Set all of it's codes to return, so that
1999 * when we pick a 'case' statement, we don't
2000 * fall through to processing the next one.
2002 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2003 csingle->actions[i] = MOD_ACTION_RETURN;
2009 static modcallable *do_compile_modforeach(modcallable *parent,
2010 rlm_components_t component, CONF_SECTION *cs)
2014 modcallable *csingle;
2017 value_pair_tmpl_t *vpt;
2019 name2 = cf_section_name2(cs);
2022 "You must specify an attribute to loop over in 'foreach'");
2026 if (!cf_item_find_next(cs, NULL)) {
2027 cf_log_err_cs(cs, "'foreach' blocks cannot be empty");
2032 * Create the template. If we fail, AND it's a bare word
2033 * with &Foo-Bar, it MAY be an attribute defined by a
2034 * module. Allow it for now. The pass2 checks below
2037 type = cf_section_name2_type(cs);
2038 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
2039 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
2040 char *spaces, *text;
2042 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
2044 cf_log_err_cs(cs, "Syntax error");
2045 cf_log_err_cs(cs, "%s", name2);
2046 cf_log_err_cs(cs, "%s^ %s", spaces, text);
2048 talloc_free(spaces);
2054 if (vpt && (vpt->type != TMPL_TYPE_ATTR)) {
2055 cf_log_err_cs(cs, "MUST use attribute reference in 'foreach'");
2060 * Fix up the template to iterate over all instances of
2061 * the attribute. In a perfect consistent world, users would do
2062 * foreach &attr[*], but that's taking the consistency thing a bit far.
2064 if (vpt && (vpt->type == TMPL_TYPE_ATTR)) {
2065 vpt->tmpl_num = NUM_ALL;
2068 csingle = do_compile_modgroup(parent, component, cs,
2069 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2077 g = mod_callabletogroup(csingle);
2083 static modcallable *do_compile_modbreak(modcallable *parent,
2084 rlm_components_t component, CONF_ITEM const *ci)
2086 CONF_SECTION const *cs = NULL;
2088 for (cs = cf_item_parent(ci);
2090 cs = cf_item_parent(cf_section_to_item(cs))) {
2091 if (strcmp(cf_section_name1(cs), "foreach") == 0) {
2097 cf_log_err(ci, "'break' can only be used in a 'foreach' section");
2101 return do_compile_modgroup(parent, component, NULL,
2102 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2107 static modcallable *do_compile_modserver(modcallable *parent,
2108 rlm_components_t component, CONF_ITEM *ci,
2113 modcallable *csingle;
2114 CONF_SECTION *subcs;
2117 subcs = cf_section_sub_find_name2(cs, comp2str[component], NULL);
2119 cf_log_err(ci, "Server %s has no %s section",
2120 server, comp2str[component]);
2124 mr = talloc_zero(parent, modref);
2126 csingle = mod_reftocallable(mr);
2127 csingle->parent = parent;
2128 csingle->next = NULL;
2129 csingle->name = name;
2130 csingle->type = MOD_REFERENCE;
2131 csingle->method = component;
2133 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2134 sizeof(csingle->actions));
2136 mr->ref_name = strdup(server);
2142 static modcallable *do_compile_modxlat(modcallable *parent,
2143 rlm_components_t component, char const *fmt)
2145 modcallable *csingle;
2148 mx = talloc_zero(parent, modxlat);
2150 csingle = mod_xlattocallable(mx);
2151 csingle->parent = parent;
2152 csingle->next = NULL;
2153 csingle->name = "expand";
2154 csingle->type = MOD_XLAT;
2155 csingle->method = component;
2157 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2158 sizeof(csingle->actions));
2160 mx->xlat_name = strdup(fmt);
2161 if (fmt[0] != '%') {
2165 strcpy(mx->xlat_name, fmt + 1);
2166 p = strrchr(mx->xlat_name, '`');
2174 * redundant, etc. can refer to modules or groups, but not much else.
2176 static int all_children_are_modules(CONF_SECTION *cs, char const *name)
2180 for (ci=cf_item_find_next(cs, NULL);
2182 ci=cf_item_find_next(cs, ci)) {
2184 * If we're a redundant, etc. group, then the
2185 * intention is to call modules, rather than
2186 * processing logic. These checks aren't
2187 * *strictly* necessary, but they keep the users
2188 * from doing crazy things.
2190 if (cf_item_is_section(ci)) {
2191 CONF_SECTION *subcs = cf_item_to_section(ci);
2192 char const *name1 = cf_section_name1(subcs);
2194 if ((strcmp(name1, "if") == 0) ||
2195 (strcmp(name1, "else") == 0) ||
2196 (strcmp(name1, "elsif") == 0) ||
2197 (strcmp(name1, "update") == 0) ||
2198 (strcmp(name1, "switch") == 0) ||
2199 (strcmp(name1, "case") == 0)) {
2200 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
2207 if (cf_item_is_pair(ci)) {
2208 CONF_PAIR *cp = cf_item_to_pair(ci);
2209 if (cf_pair_value(cp) != NULL) {
2211 "Entry with no value is invalid");
2222 * Compile one entry of a module call.
2224 static modcallable *do_compile_modsingle(modcallable *parent,
2225 rlm_components_t component, CONF_ITEM *ci,
2227 char const **modname)
2229 char const *modrefname;
2231 modcallable *csingle;
2232 module_instance_t *this;
2233 CONF_SECTION *cs, *subcs, *modules;
2234 char const *realname;
2236 if (cf_item_is_section(ci)) {
2239 cs = cf_item_to_section(ci);
2240 modrefname = cf_section_name1(cs);
2241 name2 = cf_section_name2(cs);
2242 if (!name2) name2 = "";
2245 * group{}, redundant{}, or append{} may appear
2246 * where a single module instance was expected.
2247 * In that case, we hand it off to
2250 if (strcmp(modrefname, "group") == 0) {
2252 return do_compile_modgroup(parent, component, cs,
2254 grouptype, MOD_GROUP);
2256 } else if (strcmp(modrefname, "redundant") == 0) {
2259 if (!all_children_are_modules(cs, modrefname)) {
2263 return do_compile_modgroup(parent, component, cs,
2264 GROUPTYPE_REDUNDANT,
2265 grouptype, MOD_GROUP);
2267 } else if (strcmp(modrefname, "append") == 0) {
2269 return do_compile_modgroup(parent, component, cs,
2271 grouptype, MOD_GROUP);
2273 } else if (strcmp(modrefname, "load-balance") == 0) {
2276 if (!all_children_are_modules(cs, modrefname)) {
2280 return do_compile_modgroup(parent, component, cs,
2282 grouptype, MOD_LOAD_BALANCE);
2284 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2287 if (!all_children_are_modules(cs, modrefname)) {
2291 return do_compile_modgroup(parent, component, cs,
2292 GROUPTYPE_REDUNDANT,
2293 grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2296 } else if (strcmp(modrefname, "if") == 0) {
2297 if (!cf_section_name2(cs)) {
2298 cf_log_err(ci, "'if' without condition");
2303 csingle= do_compile_modgroup(parent, component, cs,
2306 if (!csingle) return NULL;
2311 } else if (strcmp(modrefname, "elsif") == 0) {
2313 ((parent->type == MOD_LOAD_BALANCE) ||
2314 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2315 cf_log_err(ci, "'elsif' cannot be used in this section");
2319 if (!cf_section_name2(cs)) {
2320 cf_log_err(ci, "'elsif' without condition");
2325 return do_compile_modgroup(parent, component, cs,
2327 grouptype, MOD_ELSIF);
2329 } else if (strcmp(modrefname, "else") == 0) {
2331 ((parent->type == MOD_LOAD_BALANCE) ||
2332 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2333 cf_log_err(ci, "'else' cannot be used in this section section");
2337 if (cf_section_name2(cs)) {
2338 cf_log_err(ci, "Cannot have conditions on 'else'");
2343 return do_compile_modgroup(parent, component, cs,
2345 grouptype, MOD_ELSE);
2347 } else if (strcmp(modrefname, "update") == 0) {
2350 return do_compile_modupdate(parent, component, cs,
2353 } else if (strcmp(modrefname, "switch") == 0) {
2356 return do_compile_modswitch (parent, component, cs);
2358 } else if (strcmp(modrefname, "case") == 0) {
2361 return do_compile_modcase(parent, component, cs);
2363 } else if (strcmp(modrefname, "foreach") == 0) {
2366 return do_compile_modforeach(parent, component, cs);
2369 } /* else it's something like sql { fail = 1 ...} */
2371 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2375 * Else it's a module reference, with updated return
2380 CONF_PAIR *cp = cf_item_to_pair(ci);
2381 modrefname = cf_pair_attr(cp);
2384 * Actions (ok = 1), etc. are orthogonal to just
2385 * about everything else.
2387 if (cf_pair_value(cp) != NULL) {
2388 cf_log_err(ci, "Entry is not a reference to a module");
2392 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2393 (modrefname[0] == '`')) {
2394 return do_compile_modxlat(parent, component,
2399 * See if the module is a virtual one. If so,
2400 * return that, rather than doing anything here.
2403 cs = cf_section_find("instantiate");
2404 if (cs) subcs = cf_section_sub_find_name2(cs, NULL,
2407 (cs = cf_section_find("policy")) != NULL) {
2410 snprintf(buffer, sizeof(buffer), "%s.%s",
2411 modrefname, comp2str[component]);
2414 * Prefer name.section, then name.
2416 subcs = cf_section_sub_find_name2(cs, NULL,
2419 subcs = cf_section_sub_find_name2(cs, NULL,
2425 * Allow policies to over-ride module names.
2426 * i.e. the "sql" policy can do some extra things,
2427 * and then call the "sql" module.
2429 for (loop = cf_item_parent(ci);
2431 loop = cf_item_parent(cf_section_to_item(loop))) {
2432 if (loop == subcs) {
2439 * redundant foo {} is a single.
2441 if (cf_section_name2(subcs)) {
2442 return do_compile_modsingle(parent,
2444 cf_section_to_item(subcs),
2449 * foo {} is a group.
2451 return do_compile_modgroup(parent,
2455 grouptype, MOD_GROUP);
2461 if (strcmp(modrefname, "break") == 0) {
2462 return do_compile_modbreak(parent, component, ci);
2465 if (strcmp(modrefname, "return") == 0) {
2466 return do_compile_modgroup(parent, component, NULL,
2467 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2473 * Not a virtual module. It must be a real module.
2475 modules = cf_section_find("modules");
2477 realname = modrefname;
2481 * Try to load the optional module.
2483 if (realname[0] == '-') realname++;
2486 * As of v3, only known modules are in the
2487 * "modules" section.
2489 if (cf_section_sub_find_name2(modules, NULL, realname)) {
2490 this = find_module_instance(modules, realname, true);
2491 if (!this && (realname != modrefname)) {
2497 * We were asked to MAYBE load it and it
2498 * doesn't exist. Return a soft error.
2500 if (realname != modrefname) {
2501 *modname = modrefname;
2512 * Maybe it's module.method
2514 p = strrchr(modrefname, '.');
2515 if (p) for (i = RLM_COMPONENT_AUTH;
2516 i < RLM_COMPONENT_COUNT;
2518 if (strcmp(p + 1, comp2str[i]) == 0) {
2521 strlcpy(buffer, modrefname, sizeof(buffer));
2522 buffer[p - modrefname] = '\0';
2525 this = find_module_instance(modules, buffer, true);
2526 if (this && !this->entry->module->methods[i]) {
2528 cf_log_err(ci, "Module %s has no such method %s", buffer, comp2str[i]);
2537 * Call a server. This should really be deleted...
2539 if (strncmp(modrefname, "server[", 7) == 0) {
2542 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2543 p = strrchr(buffer, ']');
2544 if (!p || p[1] != '\0' || (p == buffer)) {
2545 cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2550 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2552 cf_log_err(ci, "No such server \"%s\".", buffer);
2556 return do_compile_modserver(parent, component, ci,
2557 modrefname, cs, buffer);
2561 cf_log_err(ci, "Failed to find \"%s\" in the \"modules\" section.", modrefname);
2562 cf_log_err(ci, "Please verify that the configuration exists in the file %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2567 * We know it's all OK, allocate the structures, and fill
2570 single = talloc_zero(parent, modsingle);
2571 csingle = mod_singletocallable(single);
2572 csingle->parent = parent;
2573 csingle->next = NULL;
2574 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2575 memcpy(csingle->actions, defaultactions[component][grouptype],
2576 sizeof csingle->actions);
2577 } else { /* inside Auth-Type has different rules */
2578 memcpy(csingle->actions, defaultactions[RLM_COMPONENT_AUTZ][grouptype],
2579 sizeof csingle->actions);
2581 rad_assert(modrefname != NULL);
2582 csingle->name = realname;
2583 csingle->type = MOD_SINGLE;
2584 csingle->method = component;
2587 * Singles can override the actions, virtual modules cannot.
2589 * FIXME: We may want to re-visit how to do this...
2590 * maybe a csingle as a ref?
2592 if (cf_item_is_section(ci)) {
2595 cs = cf_item_to_section(ci);
2596 for (csi=cf_item_find_next(cs, NULL);
2598 csi=cf_item_find_next(cs, csi)) {
2600 if (cf_item_is_section(csi)) {
2601 cf_log_err(csi, "Subsection of module instance call not allowed");
2602 talloc_free(csingle);
2606 if (!cf_item_is_pair(csi)) continue;
2608 if (!compile_action(csingle, cf_item_to_pair(csi))) {
2609 talloc_free(csingle);
2616 * Bail out if the module in question does not supply the
2619 if (!this->entry->module->methods[component]) {
2620 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2621 comp2str[component]);
2622 talloc_free(csingle);
2626 single->modinst = this;
2627 *modname = this->entry->module->name;
2631 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2632 modcallable **parent,
2633 rlm_components_t component, CONF_ITEM *ci,
2634 char const **modname)
2641 CONF_SECTION *parentcs;
2643 g = talloc_zero(ctx, modgroup);
2644 memset(g, 0, sizeof(*g));
2645 g->grouptype = GROUPTYPE_SIMPLE;
2646 c = mod_grouptocallable(g);
2649 defaultactions[component][GROUPTYPE_SIMPLE],
2650 sizeof(c->actions));
2652 parentcs = cf_item_parent(ci);
2653 c->name = cf_section_name2(parentcs);
2655 c->name = cf_section_name1(parentcs);
2658 c->type = MOD_GROUP;
2659 c->method = component;
2662 *parent = mod_grouptocallable(g);
2665 ret = do_compile_modsingle(*parent, component, ci,
2668 dump_tree(component, ret);
2674 * Internal compile group code.
2676 static modcallable *do_compile_modgroup(modcallable *parent,
2677 rlm_components_t component, CONF_SECTION *cs,
2678 int grouptype, int parentgrouptype, int mod_type)
2685 g = talloc_zero(parent, modgroup);
2686 g->grouptype = grouptype;
2690 c = mod_grouptocallable(g);
2694 memset(c->actions, 0, sizeof(c->actions));
2696 if (!cs) { /* only for "break" and "return" */
2702 * Remember the name for printing, etc.
2704 * FIXME: We may also want to put the names into a
2705 * rbtree, so that groups can reference each other...
2707 c->name = cf_section_name2(cs);
2709 c->name = cf_section_name1(cs);
2710 if ((strcmp(c->name, "group") == 0) ||
2711 (strcmp(c->name, "redundant") == 0)) {
2713 } else if (c->type == MOD_GROUP) {
2714 c->type = MOD_POLICY;
2720 * Do load-time optimizations
2722 if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2725 rad_assert(parent != NULL);
2727 if (c->type == MOD_IF) {
2728 g->cond = cf_data_find(g->cs, "if");
2729 rad_assert(g->cond != NULL);
2732 if (g->cond->type == COND_TYPE_FALSE) {
2733 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2734 group_name[g->mc.type],
2735 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2739 } else if (c->type == MOD_ELSIF) {
2741 g->cond = cf_data_find(g->cs, "if");
2742 rad_assert(g->cond != NULL);
2744 rad_assert(parent != NULL);
2745 p = mod_callabletogroup(parent);
2747 rad_assert(p->tail != NULL);
2749 f = mod_callabletogroup(p->tail);
2750 rad_assert((f->mc.type == MOD_IF) ||
2751 (f->mc.type == MOD_ELSIF));
2754 * If we took the previous condition, we
2755 * don't need to take this one.
2757 * We reset our condition to 'true', so
2758 * that subsequent sections can check
2759 * that they don't need to be executed.
2761 if (f->cond->type == COND_TYPE_TRUE) {
2763 INFO(" # Skipping contents of '%s' as previous '%s' is always 'true' -- %s:%d",
2764 group_name[g->mc.type],
2765 group_name[f->mc.type],
2766 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2773 rad_assert(c->type == MOD_ELSE);
2775 rad_assert(parent != NULL);
2776 p = mod_callabletogroup(parent);
2778 rad_assert(p->tail != NULL);
2780 f = mod_callabletogroup(p->tail);
2781 rad_assert((f->mc.type == MOD_IF) ||
2782 (f->mc.type == MOD_ELSIF));
2785 * If we took the previous condition, we
2786 * don't need to take this one.
2788 if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2792 * Else we need to compile this section
2798 * Loop over the children of this group.
2800 for (ci=cf_item_find_next(cs, NULL);
2802 ci=cf_item_find_next(cs, ci)) {
2805 * Sections are references to other groups, or
2806 * to modules with updated return codes.
2808 if (cf_item_is_section(ci)) {
2809 char const *junk = NULL;
2810 modcallable *single;
2811 CONF_SECTION *subcs = cf_item_to_section(ci);
2813 single = do_compile_modsingle(c, component, ci,
2816 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2817 cf_section_name1(subcs));
2821 add_child(g, single);
2823 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2827 char const *attr, *value;
2828 CONF_PAIR *cp = cf_item_to_pair(ci);
2830 attr = cf_pair_attr(cp);
2831 value = cf_pair_value(cp);
2834 * A CONF_PAIR is either a module
2835 * instance with no actions
2839 modcallable *single;
2840 char const *junk = NULL;
2842 single = do_compile_modsingle(c,
2848 if (cf_item_is_pair(ci) &&
2849 cf_pair_attr(cf_item_to_pair(ci))[0] == '-') {
2854 "Failed to parse \"%s\" entry.",
2859 add_child(g, single);
2862 * Or a module instance with action.
2864 } else if (!compile_action(c, cp)) {
2867 } /* else it worked */
2873 * Set the default actions, if they haven't already been
2876 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2877 if (!c->actions[i]) {
2878 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2879 c->actions[i] = defaultactions[component][parentgrouptype][i];
2880 } else { /* inside Auth-Type has different rules */
2881 c->actions[i] = defaultactions[RLM_COMPONENT_AUTZ][parentgrouptype][i];
2891 if (grouptype != GROUPTYPE_REDUNDANT) break;
2894 case MOD_LOAD_BALANCE:
2895 case MOD_REDUNDANT_LOAD_BALANCE:
2897 cf_log_err_cs(g->cs, "%s sections cannot be empty",
2898 cf_section_name1(g->cs));
2905 * FIXME: If there are no children, return NULL?
2907 return mod_grouptocallable(g);
2910 modcallable *compile_modgroup(modcallable *parent,
2911 rlm_components_t component, CONF_SECTION *cs)
2913 modcallable *ret = do_compile_modgroup(parent, component, cs,
2915 GROUPTYPE_SIMPLE, MOD_GROUP);
2917 if (debug_flag > 3) {
2918 modcall_debug(ret, 2);
2924 void add_to_modcallable(modcallable *parent, modcallable *this)
2928 rad_assert(this != NULL);
2929 rad_assert(parent != NULL);
2931 g = mod_callabletogroup(parent);
2938 static bool pass2_xlat_compile(CONF_ITEM const *ci, value_pair_tmpl_t **pvpt, bool convert,
2939 DICT_ATTR const *da)
2945 value_pair_tmpl_t *vpt;
2949 rad_assert(vpt->type == TMPL_TYPE_XLAT);
2951 fmt = talloc_typed_strdup(vpt, vpt->name);
2952 slen = xlat_tokenize(vpt, fmt, &head, &error);
2955 char *spaces, *text;
2957 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
2959 cf_log_err(ci, "Failed parsing expanded string:");
2960 cf_log_err(ci, "%s", text);
2961 cf_log_err(ci, "%s^ %s", spaces, error);
2963 talloc_free(spaces);
2969 * Convert %{Attribute-Name} to &Attribute-Name
2972 value_pair_tmpl_t *attr;
2974 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
2977 * If it's a virtual attribute, leave it
2980 if (attr->tmpl_da->flags.virtual) {
2986 * If the attribute is of incompatible
2987 * type, leave it alone.
2989 if (da && (da->type != attr->tmpl_da->type)) {
2994 if (cf_item_is_pair(ci)) {
2995 CONF_PAIR *cp = cf_item_to_pair(ci);
2997 WARN("%s[%d] Please change %%{%s} to &%s",
2998 cf_pair_filename(cp), cf_pair_lineno(cp),
2999 attr->name, attr->name);
3001 CONF_SECTION *cs = cf_item_to_section(ci);
3003 WARN("%s[%d] Please change %%{%s} to &%s",
3004 cf_section_filename(cs), cf_section_lineno(cs),
3005 attr->name, attr->name);
3014 * Re-write it to be a pre-parsed XLAT structure.
3016 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3017 vpt->tmpl_xlat = head;
3024 static bool pass2_regex_compile(CONF_ITEM const *ci, value_pair_tmpl_t *vpt)
3029 rad_assert(vpt->type == TMPL_TYPE_REGEX);
3032 * It's a dynamic expansion. We can't expand the string,
3033 * but we can pre-parse it as an xlat struct. In that
3034 * case, we convert it to a pre-compiled XLAT.
3036 * This is a little more complicated than it needs to be
3037 * because radius_evaluate_map() keys off of the src
3038 * template type, instead of the operators. And, the
3039 * pass2_xlat_compile() function expects to get passed an
3040 * XLAT instead of a REGEX.
3042 if (strchr(vpt->name, '%')) {
3043 vpt->type = TMPL_TYPE_XLAT;
3044 return pass2_xlat_compile(ci, &vpt, false, NULL);
3047 slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3048 vpt->tmpl_iflag, vpt->tmpl_mflag, false);
3050 char *spaces, *text;
3052 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3054 cf_log_err(ci, "Invalid regular expression:");
3055 cf_log_err(ci, "%s", text);
3056 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3058 talloc_free(spaces);
3064 vpt->type = TMPL_TYPE_REGEX_STRUCT;
3065 vpt->tmpl_preg = preg;
3071 static bool pass2_fixup_undefined(CONF_ITEM const *ci, value_pair_tmpl_t *vpt)
3073 DICT_ATTR const *da;
3075 rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3077 da = dict_attrbyname(vpt->tmpl_unknown_name);
3079 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3084 vpt->type = TMPL_TYPE_ATTR;
3088 static bool pass2_callback(UNUSED void *ctx, fr_cond_t *c)
3090 value_pair_map_t *map;
3092 if (c->type == COND_TYPE_EXISTS) {
3093 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3094 return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3097 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3100 * The existence check might have been &Foo-Bar,
3101 * where Foo-Bar is defined by a module.
3103 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3104 if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3105 c->pass2_fixup = PASS2_FIXUP_NONE;
3111 * Maps have a paircompare fixup applied to them.
3112 * Others get ignored.
3114 if (c->pass2_fixup == PASS2_FIXUP_NONE) {
3115 if (c->type == COND_TYPE_MAP) {
3123 map = c->data.map; /* shorter */
3128 * Where "foo" is dynamically defined.
3130 if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3131 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3132 map->lhs->tmpl_da->vendor,
3134 cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3135 map->lhs->tmpl_da->name,
3141 * These guys can't have a paircompare fixup applied.
3143 c->pass2_fixup = PASS2_FIXUP_NONE;
3147 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3148 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3149 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3152 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3153 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3156 c->pass2_fixup = PASS2_FIXUP_NONE;
3161 * Just in case someone adds a new fixup later.
3163 rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3164 (c->pass2_fixup == PASS2_PAIRCOMPARE));
3169 if (map->lhs->type == TMPL_TYPE_XLAT) {
3171 * Don't compile the LHS to an attribute
3172 * reference for now. When we do that, we've got
3173 * to check the RHS for type-specific data, and
3174 * parse it to a TMPL_TYPE_DATA.
3176 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3181 if (map->rhs->type == TMPL_TYPE_XLAT) {
3183 * Convert the RHS to an attribute reference only
3184 * if the LHS is an attribute reference, AND is
3185 * of the same type as the RHS.
3187 * We can fix this when the code in evaluate.c
3188 * can handle strings on the LHS, and attributes
3189 * on the RHS. For now, the code in parser.c
3192 if (map->lhs->type == TMPL_TYPE_ATTR) {
3193 DICT_ATTR const *da = c->cast;
3195 if (!c->cast) da = map->lhs->tmpl_da;
3197 if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3202 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3209 * Convert bare refs to %{Foreach-Variable-N}
3211 if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3212 (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3215 value_pair_tmpl_t *vpt;
3217 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3218 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3219 T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST);
3221 char *spaces, *text;
3223 fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3225 cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3226 cf_log_err(map->ci, "%s", fmt);
3227 cf_log_err(map->ci, "%s^ %s", spaces, text);
3229 talloc_free(spaces);
3235 talloc_free(map->lhs);
3240 if (map->rhs->type == TMPL_TYPE_REGEX) {
3241 if (!pass2_regex_compile(map->ci, map->rhs)) {
3245 rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3249 * Only attributes can have a paircompare registered, and
3250 * they can only be with the current REQUEST, and only
3251 * with the request pairs.
3253 if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3254 (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3255 (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3259 if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3261 if (map->rhs->type == TMPL_TYPE_ATTR) {
3262 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3267 if (map->rhs->type == TMPL_TYPE_REGEX) {
3268 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3274 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3279 if (map->op != T_OP_CMP_EQ) {
3280 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3286 * Mark it as requiring a paircompare() call, instead of
3289 c->pass2_fixup = PASS2_PAIRCOMPARE;
3296 * Compile the RHS of update sections to xlat_exp_t
3298 static bool modcall_pass2_update(modgroup *g)
3300 value_pair_map_t *map;
3302 for (map = g->map; map != NULL; map = map->next) {
3303 if (map->rhs->type == TMPL_TYPE_XLAT) {
3304 rad_assert(map->rhs->tmpl_xlat == NULL);
3307 * FIXME: compile to attribute && handle
3308 * the conversion in map_to_vp().
3310 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3315 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3318 * Deal with undefined attributes now.
3320 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3321 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3324 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3325 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3334 * Do a second-stage pass on compiling the modules.
3336 bool modcall_pass2(modcallable *mc)
3343 for (c = mc; c != NULL; c = c->next) {
3351 g = mod_callabletogroup(c);
3352 if (g->done_pass2) return true;
3354 name2 = cf_section_name2(g->cs);
3356 c->debug_name = group_name[c->type];
3358 c->debug_name = talloc_asprintf(c, "update %s", name2);
3361 if (!modcall_pass2_update(g)) {
3364 g->done_pass2 = true;
3367 case MOD_XLAT: /* @todo: pre-parse xlat's */
3374 c->debug_name = c->name;
3375 break; /* do nothing */
3380 g = mod_callabletogroup(c);
3381 if (g->done_pass2) return true;
3383 name2 = cf_section_name2(g->cs);
3384 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3387 * Don't walk over these.
3389 if ((g->cond->type == COND_TYPE_TRUE) ||
3390 (g->cond->type == COND_TYPE_FALSE)) {
3395 * The compilation code takes care of
3396 * simplifying 'true' and 'false'
3397 * conditions. For others, we have to do
3398 * a second pass to parse && compile xlats.
3400 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3404 if (!modcall_pass2(g->children)) return false;
3405 g->done_pass2 = true;
3411 g = mod_callabletogroup(c);
3412 if (g->done_pass2) return true;
3414 name2 = cf_section_name2(g->cs);
3415 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3418 * We had &Foo-Bar, where Foo-Bar is
3419 * defined by a module.
3422 rad_assert(c->name != NULL);
3423 rad_assert(c->name[0] == '&');
3424 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3426 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3427 cf_section_name2_type(g->cs),
3428 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3430 char *spaces, *text;
3433 fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3435 cf_log_err_cs(g->cs, "Syntax error");
3436 cf_log_err_cs(g->cs, "%s", c->name);
3437 cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3439 talloc_free(spaces);
3449 * Statically compile xlats
3451 if (g->vpt->type == TMPL_TYPE_XLAT) {
3452 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3453 &g->vpt, true, NULL)) {
3461 * We may have: switch Foo-Bar {
3463 * where Foo-Bar is an attribute defined
3464 * by a module. Since there's no leading
3465 * &, it's parsed as a literal. But if
3466 * we can parse it as an attribute,
3467 * switch to using that.
3469 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3470 value_pair_tmpl_t *vpt;
3472 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3473 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3474 if (slen < 0) goto parse_error;
3475 if (vpt->type == TMPL_TYPE_ATTR) {
3476 talloc_free(g->vpt);
3484 * Warn about old-style configuration.
3486 * DEPRECATED: switch User-Name { ...
3487 * ALLOWED : switch &User-Name { ...
3489 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3490 (c->name[0] != '&')) {
3491 WARN("%s[%d]: Please change %s to &%s",
3492 cf_section_filename(g->cs),
3493 cf_section_lineno(g->cs),
3498 if (!modcall_pass2(g->children)) return false;
3499 g->done_pass2 = true;
3503 g = mod_callabletogroup(c);
3504 if (g->done_pass2) return true;
3506 name2 = cf_section_name2(g->cs);
3508 c->debug_name = group_name[c->type];
3510 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3513 rad_assert(c->parent != NULL);
3514 rad_assert(c->parent->type == MOD_SWITCH);
3517 * The statement may refer to an
3518 * attribute which doesn't exist until
3519 * all of the modules have been loaded.
3520 * Check for that now.
3522 if (!g->vpt && c->name &&
3523 (c->name[0] == '&') &&
3524 (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3525 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3526 cf_section_name2_type(g->cs),
3527 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3528 if (slen < 0) goto parse_error;
3532 * We have "case {...}". There's no
3533 * argument, so we don't need to check
3536 if (!g->vpt) goto do_children;
3539 * Do type-specific checks on the case statement
3541 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3544 f = mod_callabletogroup(mc->parent);
3545 rad_assert(f->vpt != NULL);
3548 * We're switching over an
3549 * attribute. Check that the
3552 if (f->vpt->type == TMPL_TYPE_ATTR) {
3553 rad_assert(f->vpt->tmpl_da != NULL);
3555 if (!tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da)) {
3556 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3566 * Compile and sanity check xlat
3569 if (g->vpt->type == TMPL_TYPE_XLAT) {
3572 f = mod_callabletogroup(mc->parent);
3573 rad_assert(f->vpt != NULL);
3576 * Don't expand xlat's into an
3577 * attribute of a different type.
3579 if (f->vpt->type == TMPL_TYPE_ATTR) {
3580 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3581 &g->vpt, true, f->vpt->tmpl_da)) {
3585 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3586 &g->vpt, true, NULL)) {
3592 if (!modcall_pass2(g->children)) return false;
3593 g->done_pass2 = true;
3597 g = mod_callabletogroup(c);
3598 if (g->done_pass2) return true;
3600 name2 = cf_section_name2(g->cs);
3601 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], name2);
3604 * Already parsed, handle the children.
3606 if (g->vpt) goto check_children;
3609 * We had &Foo-Bar, where Foo-Bar is
3610 * defined by a module.
3612 rad_assert(c->name != NULL);
3613 rad_assert(c->name[0] == '&');
3614 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3617 * The statement may refer to an
3618 * attribute which doesn't exist until
3619 * all of the modules have been loaded.
3620 * Check for that now.
3622 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3623 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3624 if (slen < 0) goto parse_error;
3627 rad_assert(g->vpt->type == TMPL_TYPE_ATTR);
3628 if (g->vpt->tmpl_num != NUM_ALL) {
3629 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3632 if (!modcall_pass2(g->children)) return false;
3633 g->done_pass2 = true;
3637 c->debug_name = group_name[c->type];
3641 g = mod_callabletogroup(c);
3642 c->debug_name = talloc_asprintf(c, "%s %s", group_name[c->type], cf_section_name1(g->cs));
3647 case MOD_LOAD_BALANCE:
3648 case MOD_REDUNDANT_LOAD_BALANCE:
3649 c->debug_name = group_name[c->type];
3654 g = mod_callabletogroup(c);
3656 c->debug_name = mc->name; /* for authorize, etc. */
3658 } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3659 char const *name1 = cf_section_name1(g->cs);
3661 if (strcmp(name1, group_name[c->type]) != 0) {
3662 c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3666 if (g->done_pass2) return true;
3667 if (!modcall_pass2(g->children)) return false;
3668 g->done_pass2 = true;
3676 void modcall_debug(modcallable *mc, int depth)
3680 value_pair_map_t *map;
3683 for (this = mc; this != NULL; this = this->next) {
3684 switch (this->type) {
3689 modsingle *single = mod_callabletosingle(this);
3691 DEBUG("%.*s%s", depth, modcall_spaces,
3692 single->modinst->name);
3698 g = mod_callabletogroup(this);
3699 DEBUG("%.*s%s {", depth, modcall_spaces,
3700 group_name[this->type]);
3702 for (map = g->map; map != NULL; map = map->next) {
3703 map_prints(buffer, sizeof(buffer), map);
3704 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3707 DEBUG("%.*s}", depth, modcall_spaces);
3711 g = mod_callabletogroup(this);
3712 DEBUG("%.*s%s {", depth, modcall_spaces,
3713 group_name[this->type]);
3714 modcall_debug(g->children, depth + 1);
3715 DEBUG("%.*s}", depth, modcall_spaces);
3720 g = mod_callabletogroup(this);
3721 fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3722 DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3723 group_name[this->type], buffer);
3724 modcall_debug(g->children, depth + 1);
3725 DEBUG("%.*s}", depth, modcall_spaces);
3730 g = mod_callabletogroup(this);
3731 tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3732 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3733 group_name[this->type], buffer);
3734 modcall_debug(g->children, depth + 1);
3735 DEBUG("%.*s}", depth, modcall_spaces);
3740 g = mod_callabletogroup(this);
3741 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3742 group_name[this->type], this->name);
3743 modcall_debug(g->children, depth + 1);
3744 DEBUG("%.*s}", depth, modcall_spaces);
3748 DEBUG("%.*sbreak", depth, modcall_spaces);
3753 g = mod_callabletogroup(this);
3754 DEBUG("%.*s%s {", depth, modcall_spaces,
3755 group_name[this->type]);
3756 modcall_debug(g->children, depth + 1);
3757 DEBUG("%.*s}", depth, modcall_spaces);
3761 case MOD_LOAD_BALANCE:
3762 case MOD_REDUNDANT_LOAD_BALANCE:
3763 g = mod_callabletogroup(this);
3764 DEBUG("%.*s%s {", depth, modcall_spaces,
3765 group_name[this->type]);
3766 modcall_debug(g->children, depth + 1);
3767 DEBUG("%.*s}", depth, modcall_spaces);