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 },
177 * Compile action && rcode for later use.
179 static int compile_action(modcallable *c, CONF_PAIR *cp)
182 char const *attr, *value;
184 attr = cf_pair_attr(cp);
185 value = cf_pair_value(cp);
186 if (!value) return 0;
188 if (!strcasecmp(value, "return"))
189 action = MOD_ACTION_RETURN;
191 else if (!strcasecmp(value, "break"))
192 action = MOD_ACTION_RETURN;
194 else if (!strcasecmp(value, "reject"))
195 action = MOD_ACTION_REJECT;
197 else if (strspn(value, "0123456789")==strlen(value)) {
198 action = atoi(value);
201 * Don't allow priority zero, for future use.
203 if (action == 0) return 0;
205 cf_log_err_cp(cp, "Unknown action '%s'.\n",
210 if (strcasecmp(attr, "default") != 0) {
213 rcode = fr_str2int(mod_rcode_table, attr, -1);
216 "Unknown module rcode '%s'.\n",
220 c->actions[rcode] = action;
222 } else { /* set all unset values to the default */
225 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
226 if (!c->actions[i]) c->actions[i] = action;
233 /* Some short names for debugging output */
234 static char const * const comp2str[] = {
250 #ifdef HAVE_PTHREAD_H
252 * Lock the mutex for the module
254 static void safe_lock(module_instance_t *instance)
257 pthread_mutex_lock(instance->mutex);
261 * Unlock the mutex for the module
263 static void safe_unlock(module_instance_t *instance)
266 pthread_mutex_unlock(instance->mutex);
270 * No threads: these functions become NULL's.
272 #define safe_lock(foo)
273 #define safe_unlock(foo)
276 static rlm_rcode_t CC_HINT(nonnull) call_modsingle(rlm_components_t component, modsingle *sp, REQUEST *request)
279 int indent = request->log.indent;
282 * If the request should stop, refuse to do anything.
284 blocked = (request->master_state == REQUEST_STOP_PROCESSING);
285 if (blocked) return RLM_MODULE_NOOP;
287 RDEBUG3("modsingle[%s]: calling %s (%s) for request %d",
288 comp2str[component], sp->modinst->name,
289 sp->modinst->entry->name, request->number);
290 request->log.indent = 0;
292 if (sp->modinst->force) {
293 request->rcode = sp->modinst->code;
298 * For logging unresponsive children.
300 request->module = sp->modinst->name;
302 safe_lock(sp->modinst);
303 request->rcode = sp->modinst->entry->module->methods[component](sp->modinst->insthandle, request);
304 safe_unlock(sp->modinst);
306 request->module = "";
309 * Wasn't blocked, and now is. Complain!
311 blocked = (request->master_state == REQUEST_STOP_PROCESSING);
313 RWARN("Module %s became unblocked for request %u", sp->modinst->entry->name, request->number);
317 request->log.indent = indent;
318 RDEBUG3("modsingle[%s]: returned from %s (%s) for request %d",
319 comp2str[component], sp->modinst->name,
320 sp->modinst->entry->name, request->number);
322 return request->rcode;
325 static int default_component_results[RLM_COMPONENT_COUNT] = {
326 RLM_MODULE_REJECT, /* AUTH */
327 RLM_MODULE_NOTFOUND, /* AUTZ */
328 RLM_MODULE_NOOP, /* PREACCT */
329 RLM_MODULE_NOOP, /* ACCT */
330 RLM_MODULE_FAIL, /* SESS */
331 RLM_MODULE_NOOP, /* PRE_PROXY */
332 RLM_MODULE_NOOP, /* POST_PROXY */
333 RLM_MODULE_NOOP /* POST_AUTH */
336 RLM_MODULE_NOOP, /* RECV_COA_TYPE */
337 RLM_MODULE_NOOP /* SEND_COA_TYPE */
342 extern char const *unlang_keyword[];
344 char const *unlang_keyword[] = {
348 "load-balance group",
349 "redundant-load-balance group",
367 static char const modcall_spaces[] = " ";
369 #define MODCALL_STACK_MAX (32)
372 * Don't call the modules recursively. Instead, do them
373 * iteratively, and manage the call stack ourselves.
375 typedef struct modcall_stack_entry_t {
378 int unwind; /* unwind to this one if it exists */
380 } modcall_stack_entry_t;
383 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
384 modcall_stack_entry_t *entry);
387 * Call a child of a block.
389 static void modcall_child(REQUEST *request, rlm_components_t component, int depth,
390 modcall_stack_entry_t *entry, modcallable *c,
393 modcall_stack_entry_t *next;
395 if (depth >= MODCALL_STACK_MAX) {
396 ERROR("Internal sanity check failed: module stack is too deep");
401 * Initialize the childs stack frame.
405 next->result = entry->result;
409 if (!modcall_recurse(request, component,
411 *result = RLM_MODULE_FAIL;
416 * Unwind back up the stack
418 if (next->unwind != 0) {
419 entry->unwind = next->unwind;
422 *result = next->result;
429 * Interpret the various types of blocks.
431 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
432 modcall_stack_entry_t *entry)
434 bool if_taken, was_if;
439 was_if = if_taken = false;
440 result = RLM_MODULE_UNKNOWN;
448 * Nothing more to do. Return the code and priority
449 * which was set by the caller.
454 * We've been asked to stop. Do so.
456 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
458 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
459 entry->result = RLM_MODULE_FAIL;
460 entry->priority = 9999;
466 * Handle "if" conditions.
468 if (c->type == MOD_IF) {
473 g = mod_callabletogroup(c);
474 rad_assert(g->cond != NULL);
476 RDEBUG2("%s %s{", unlang_keyword[c->type], c->name);
478 condition = radius_evaluate_cond(request, result, 0, g->cond);
481 REDEBUG("Failed retrieving values required to evaluate condition");
483 RDEBUG2("%s %s -> %s",
484 unlang_keyword[c->type],
485 c->name, condition ? "TRUE" : "FALSE");
489 * Didn't pass. Remember that.
498 * We took the "if". Go recurse into its' children.
506 * "else" if the previous "if" was taken.
507 * "if" if the previous if wasn't taken.
509 if (c->type == MOD_ELSIF) {
510 if (!was_if) goto elsif_error;
513 * Like MOD_ELSE, but allow for a later "else"
516 RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
517 unlang_keyword[c->type], request->number);
524 * Check the "if" condition.
530 * "else" for a preceding "if".
532 if (c->type == MOD_ELSE) {
533 if (!was_if) { /* error */
535 RDEBUG2("... skipping %s for request %d: No preceding \"if\"",
536 unlang_keyword[c->type], request->number);
541 RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
542 unlang_keyword[c->type], request->number);
549 * We need to process it. Go do that.
557 * We're no longer processing if/else/elsif. Reset the
558 * trackers for those conditions.
562 #endif /* WITH_UNLANG */
564 if (c->type == MOD_SINGLE) {
568 * Process a stand-alone child, and fall through
569 * to dealing with it's parent.
571 sp = mod_callabletosingle(c);
573 result = call_modsingle(c->method, sp, request);
574 RDEBUG2("[%s] = %s", c->name ? c->name : "",
575 fr_int2str(mod_rcode_table, result, "<invalid>"));
576 goto calculate_result;
581 * Update attribute(s)
583 if (c->type == MOD_UPDATE) {
585 modgroup *g = mod_callabletogroup(c);
586 value_pair_map_t *map;
590 for (map = g->map; map != NULL; map = map->next) {
591 rcode = map_to_request(request, map, map_to_vp, NULL);
593 result = (rcode == -2) ? RLM_MODULE_INVALID : RLM_MODULE_FAIL;
596 goto calculate_result;
600 result = RLM_MODULE_NOOP;
602 goto calculate_result;
606 * Loop over a set of attributes.
608 if (c->type == MOD_FOREACH) {
609 int i, foreach_depth = -1;
610 VALUE_PAIR *vps, *vp;
611 modcall_stack_entry_t *next = NULL;
613 modgroup *g = mod_callabletogroup(c);
615 if (depth >= MODCALL_STACK_MAX) {
616 ERROR("Internal sanity check failed: module stack is too deep");
621 * Figure out how deep we are in nesting by looking at request_data
624 for (i = 0; i < 8; i++) {
625 if (!request_data_reference(request, (void *)radius_get_vp, i)) {
631 if (foreach_depth < 0) {
632 REDEBUG("foreach Nesting too deep!");
633 result = RLM_MODULE_FAIL;
634 goto calculate_result;
638 * Copy the VPs from the original request, this ensures deterministic
639 * behaviour if someone decides to add or remove VPs in the set were
642 if (tmpl_copy_vps(request, &vps, request, g->vpt) < 0) { /* nothing to loop over */
644 result = RLM_MODULE_NOOP;
646 goto calculate_result;
649 rad_assert(vps != NULL);
650 fr_cursor_init(©, &vps);
652 RDEBUG2("foreach %s ", c->name);
655 * This is the actual body of the foreach loop
657 for (vp = fr_cursor_first(©);
659 vp = fr_cursor_next(©)) {
661 if (fr_debug_flag >= 2) {
664 vp_prints_value(buffer, sizeof(buffer), vp, '"');
665 RDEBUG2("# Foreach-Variable-%d = %s", foreach_depth, buffer);
670 * Add the vp to the request, so that
671 * xlat.c, xlat_foreach() can find it.
673 request_data_add(request, (void *)radius_get_vp, foreach_depth, &vp, false);
676 * Initialize the childs stack frame.
679 next->c = g->children;
680 next->result = entry->result;
684 if (!modcall_recurse(request, component, depth + 1, next)) {
689 * We've unwound to the enclosing
690 * "foreach". Stop the unwinding.
692 if (next->unwind == MOD_FOREACH) {
697 * Unwind all the way.
699 if (next->unwind == MOD_RETURN) {
700 entry->unwind = MOD_RETURN;
703 } /* loop over VPs */
706 * Free the copied vps and the request data
707 * If we don't remove the request data, something could call
708 * the xlat outside of a foreach loop and trigger a segv.
711 request_data_get(request, (void *)radius_get_vp, foreach_depth);
713 rad_assert(next != NULL);
714 result = next->result;
715 priority = next->priority;
717 goto calculate_result;
721 * Break out of a "foreach" loop, or return from a nested
724 if ((c->type == MOD_BREAK) || (c->type == MOD_RETURN)) {
728 RDEBUG2("%s", unlang_keyword[c->type]);
730 for (i = 8; i >= 0; i--) {
731 copy_p = request_data_get(request, (void *)radius_get_vp, i);
733 if (c->type == MOD_BREAK) {
734 RDEBUG2("# break Foreach-Variable-%d", i);
741 * Leave result / priority on the stack, and stop processing the section.
743 entry->unwind = c->type;
747 #endif /* WITH_UNLANG */
750 * Child is a group that has children of it's own.
752 if ((c->type == MOD_GROUP) || (c->type == MOD_POLICY)
754 || (c->type == MOD_CASE)
762 g = mod_callabletogroup(c);
765 * This should really have been caught in the
766 * compiler, and the node never generated. But
767 * doing that requires changing it's API so that
768 * it returns a flag instead of the compiled
772 RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
777 modcall_child(request, component,
778 depth + 1, entry, g->children,
781 goto calculate_result;
785 if (c->type == MOD_SWITCH) {
786 modcallable *this, *found, *null_case;
790 value_pair_map_t map;
791 value_pair_tmpl_t vpt;
795 g = mod_callabletogroup(c);
797 memset(&cond, 0, sizeof(cond));
798 memset(&map, 0, sizeof(map));
800 cond.type = COND_TYPE_MAP;
801 cond.data.map = ↦
803 map.op = T_OP_CMP_EQ;
804 map.ci = cf_section_to_item(g->cs);
806 rad_assert(g->vpt != NULL);
808 null_case = found = NULL;
812 * The attribute doesn't exist. We can skip
813 * directly to the default 'case' statement.
815 if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
817 for (this = g->children; this; this = this->next) {
818 rad_assert(this->type == MOD_CASE);
820 h = mod_callabletogroup(this);
821 if (h->vpt) continue;
831 * Expand the template if necessary, so that it
832 * is evaluated once instead of for each 'case'
835 if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
836 (g->vpt->type == TMPL_TYPE_XLAT) ||
837 (g->vpt->type == TMPL_TYPE_EXEC)) {
840 if (tmpl_aexpand(request, &p, request, g->vpt, NULL, NULL) < 0) goto find_null_case;
842 tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, talloc_array_length(data.strvalue) - 1);
846 * Find either the exact matching name, or the
847 * "case {...}" statement.
849 for (this = g->children; this; this = this->next) {
850 rad_assert(this->type == MOD_CASE);
852 h = mod_callabletogroup(this);
855 * Remember the default case
858 if (!null_case) null_case = this;
863 * If we're switching over an attribute
864 * AND we haven't pre-parsed the data for
865 * the case statement, then cast the data
866 * to the type of the attribute.
868 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
869 (h->vpt->type != TMPL_TYPE_DATA)) {
872 cond.cast = g->vpt->tmpl_da;
875 * Remove unnecessary casting.
877 if ((h->vpt->type == TMPL_TYPE_ATTR) &&
878 (g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
883 * Use the pre-expanded string.
885 } else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
886 (g->vpt->type == TMPL_TYPE_XLAT) ||
887 (g->vpt->type == TMPL_TYPE_EXEC)) {
893 * Else evaluate the 'switch' statement.
901 if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
908 if (!found) found = null_case;
911 talloc_free(data.ptr);
912 modcall_child(request, component, depth + 1, entry, found, &result);
914 goto calculate_result;
918 if ((c->type == MOD_LOAD_BALANCE) ||
919 (c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
921 modcallable *this, *found;
926 g = mod_callabletogroup(c);
928 rad_assert(g->children != NULL);
931 * Choose a child at random.
933 for (this = g->children; this; this = this->next) {
936 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
943 if (c->type == MOD_LOAD_BALANCE) {
944 modcall_child(request, component,
945 depth + 1, entry, found,
952 modcall_child(request, component,
953 depth + 1, entry, this,
955 if (this->actions[result] == MOD_ACTION_RETURN) {
961 if (!this) this = g->children;
962 } while (this != found);
965 goto calculate_result;
966 } /* MOD_LOAD_BALANCE */
969 * Reference another virtual server.
971 * This should really be deleted, and replaced with a
972 * more abstracted / functional version.
974 if (c->type == MOD_REFERENCE) {
975 modref *mr = mod_callabletoref(c);
976 char const *server = request->server;
978 if (server == mr->ref_name) {
979 RWDEBUG("Suppressing recursive call to server %s", server);
983 request->server = mr->ref_name;
984 RDEBUG("server %s { # nested call", mr->ref_name);
985 result = indexed_modcall(component, 0, request);
986 RDEBUG("} # server %s with nested call", mr->ref_name);
987 request->server = server;
988 goto calculate_result;
989 } /* MOD_REFERENCE */
992 * xlat a string without doing anything else
994 * This should really be deleted, and replaced with a
995 * more abstracted / functional version.
997 if (c->type == MOD_XLAT) {
998 modxlat *mx = mod_callabletoxlat(c);
1002 radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
1004 RDEBUG("`%s`", mx->xlat_name);
1005 radius_exec_program(NULL, 0, NULL, request, mx->xlat_name, request->packet->vps,
1006 false, true, EXEC_TIMEOUT);
1013 * Add new module types here.
1018 RDEBUG("(%s, %d) ? (%s, %d)",
1019 fr_int2str(mod_rcode_table, result, "<invalid>"),
1021 fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
1026 rad_assert(result != RLM_MODULE_UNKNOWN);
1029 * The child's action says return. Do so.
1031 if ((c->actions[result] == MOD_ACTION_RETURN) &&
1033 entry->result = result;
1038 * If "reject", break out of the loop and return
1041 if (c->actions[result] == MOD_ACTION_REJECT) {
1042 entry->result = RLM_MODULE_REJECT;
1047 * The array holds a default priority for this return
1048 * code. Grab it in preference to any unset priority.
1051 priority = c->actions[result];
1055 * We're higher than any previous priority, remember this
1056 * return code and priority.
1058 if (priority > entry->priority) {
1059 entry->result = result;
1060 entry->priority = priority;
1065 * If we're processing a "case" statement, we return once
1066 * it's done, rather than going to the next "case" statement.
1068 if (c->type == MOD_CASE) goto finish;
1072 * If we've been told to stop processing
1075 if (entry->unwind == MOD_BREAK) {
1076 RDEBUG2("# unwind to enclosing foreach");
1081 if (entry->unwind == MOD_RETURN) {
1086 entry->c = entry->c->next;
1088 if (entry->c) goto redo;
1099 /** Call a module, iteratively, with a local stack, rather than recursively
1101 * What did Paul Graham say about Lisp...?
1103 int modcall(rlm_components_t component, modcallable *c, REQUEST *request)
1105 modcall_stack_entry_t stack[MODCALL_STACK_MAX];
1108 memset(stack, 0, sizeof(stack));
1111 * Set up the initial stack frame.
1114 stack[0].result = default_component_results[component];
1115 stack[0].priority = 0;
1116 stack[0].unwind = 0;
1119 * Call the main handler.
1121 if (!modcall_recurse(request, component, 0, &stack[0])) {
1122 return RLM_MODULE_FAIL;
1126 * Return the result.
1128 return stack[0].result;
1133 static char const *action2str(int action)
1135 static char buf[32];
1136 if(action==MOD_ACTION_RETURN)
1138 if(action==MOD_ACTION_REJECT)
1140 snprintf(buf, sizeof buf, "%d", action);
1144 /* If you suspect a bug in the parser, you'll want to use these dump
1145 * functions. dump_tree should reproduce a whole tree exactly as it was found
1146 * in radiusd.conf, but in long form (all actions explicitly defined) */
1147 static void dump_mc(modcallable *c, int indent)
1151 if(c->type==MOD_SINGLE) {
1152 modsingle *single = mod_callabletosingle(c);
1153 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1154 single->modinst->name);
1155 } else if ((c->type > MOD_SINGLE) && (c->type <= MOD_POLICY)) {
1156 modgroup *g = mod_callabletogroup(c);
1158 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1159 unlang_keyword[c->type]);
1160 for(p = g->children;p;p = p->next)
1161 dump_mc(p, indent+1);
1162 } /* else ignore it for now */
1164 for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
1165 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
1166 fr_int2str(mod_rcode_table, i, "<invalid>"),
1167 action2str(c->actions[i]));
1170 DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
1173 static void dump_tree(rlm_components_t comp, modcallable *c)
1175 DEBUG("[%s]", comp2str[comp]);
1179 #define dump_tree(a, b)
1182 /* These are the default actions. For each component, the group{} block
1183 * behaves like the code from the old module_*() function. redundant{} and
1184 * append{} are based on my guesses of what they will be used for. --Pac. */
1186 defaultactions[RLM_COMPONENT_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1192 MOD_ACTION_RETURN, /* reject */
1194 MOD_ACTION_RETURN, /* ok */
1195 MOD_ACTION_RETURN, /* handled */
1197 MOD_ACTION_RETURN, /* userlock */
1198 MOD_ACTION_RETURN, /* notfound */
1204 MOD_ACTION_RETURN, /* reject */
1206 MOD_ACTION_RETURN, /* ok */
1207 MOD_ACTION_RETURN, /* handled */
1208 MOD_ACTION_RETURN, /* invalid */
1209 MOD_ACTION_RETURN, /* userlock */
1210 MOD_ACTION_RETURN, /* notfound */
1211 MOD_ACTION_RETURN, /* noop */
1212 MOD_ACTION_RETURN /* updated */
1216 MOD_ACTION_RETURN, /* reject */
1218 MOD_ACTION_RETURN, /* ok */
1219 MOD_ACTION_RETURN, /* handled */
1220 MOD_ACTION_RETURN, /* invalid */
1221 MOD_ACTION_RETURN, /* userlock */
1223 MOD_ACTION_RETURN, /* noop */
1224 MOD_ACTION_RETURN /* updated */
1231 MOD_ACTION_RETURN, /* reject */
1232 MOD_ACTION_RETURN, /* fail */
1234 MOD_ACTION_RETURN, /* handled */
1235 MOD_ACTION_RETURN, /* invalid */
1236 MOD_ACTION_RETURN, /* userlock */
1243 MOD_ACTION_RETURN, /* reject */
1245 MOD_ACTION_RETURN, /* ok */
1246 MOD_ACTION_RETURN, /* handled */
1247 MOD_ACTION_RETURN, /* invalid */
1248 MOD_ACTION_RETURN, /* userlock */
1249 MOD_ACTION_RETURN, /* notfound */
1250 MOD_ACTION_RETURN, /* noop */
1251 MOD_ACTION_RETURN /* updated */
1255 MOD_ACTION_RETURN, /* reject */
1257 MOD_ACTION_RETURN, /* ok */
1258 MOD_ACTION_RETURN, /* handled */
1259 MOD_ACTION_RETURN, /* invalid */
1260 MOD_ACTION_RETURN, /* userlock */
1262 MOD_ACTION_RETURN, /* noop */
1263 MOD_ACTION_RETURN /* updated */
1270 MOD_ACTION_RETURN, /* reject */
1271 MOD_ACTION_RETURN, /* fail */
1273 MOD_ACTION_RETURN, /* handled */
1274 MOD_ACTION_RETURN, /* invalid */
1275 MOD_ACTION_RETURN, /* userlock */
1276 MOD_ACTION_RETURN, /* notfound */
1282 MOD_ACTION_RETURN, /* reject */
1284 MOD_ACTION_RETURN, /* ok */
1285 MOD_ACTION_RETURN, /* handled */
1286 MOD_ACTION_RETURN, /* invalid */
1287 MOD_ACTION_RETURN, /* userlock */
1288 MOD_ACTION_RETURN, /* notfound */
1289 MOD_ACTION_RETURN, /* noop */
1290 MOD_ACTION_RETURN /* updated */
1294 MOD_ACTION_RETURN, /* reject */
1296 MOD_ACTION_RETURN, /* ok */
1297 MOD_ACTION_RETURN, /* handled */
1298 MOD_ACTION_RETURN, /* invalid */
1299 MOD_ACTION_RETURN, /* userlock */
1301 MOD_ACTION_RETURN, /* noop */
1302 MOD_ACTION_RETURN /* updated */
1309 MOD_ACTION_RETURN, /* reject */
1310 MOD_ACTION_RETURN, /* fail */
1312 MOD_ACTION_RETURN, /* handled */
1313 MOD_ACTION_RETURN, /* invalid */
1314 MOD_ACTION_RETURN, /* userlock */
1315 MOD_ACTION_RETURN, /* notfound */
1323 MOD_ACTION_RETURN, /* ok */
1324 MOD_ACTION_RETURN, /* handled */
1333 MOD_ACTION_RETURN, /* reject */
1335 MOD_ACTION_RETURN, /* ok */
1336 MOD_ACTION_RETURN, /* handled */
1337 MOD_ACTION_RETURN, /* invalid */
1338 MOD_ACTION_RETURN, /* userlock */
1340 MOD_ACTION_RETURN, /* noop */
1341 MOD_ACTION_RETURN /* updated */
1348 MOD_ACTION_RETURN, /* reject */
1350 MOD_ACTION_RETURN, /* ok */
1351 MOD_ACTION_RETURN, /* handled */
1352 MOD_ACTION_RETURN, /* invalid */
1353 MOD_ACTION_RETURN, /* userlock */
1354 MOD_ACTION_RETURN, /* notfound */
1355 MOD_ACTION_RETURN, /* noop */
1356 MOD_ACTION_RETURN /* updated */
1360 MOD_ACTION_RETURN, /* reject */
1362 MOD_ACTION_RETURN, /* ok */
1363 MOD_ACTION_RETURN, /* handled */
1364 MOD_ACTION_RETURN, /* invalid */
1365 MOD_ACTION_RETURN, /* userlock */
1366 MOD_ACTION_RETURN, /* notfound */
1367 MOD_ACTION_RETURN, /* noop */
1368 MOD_ACTION_RETURN /* updated */
1372 MOD_ACTION_RETURN, /* reject */
1374 MOD_ACTION_RETURN, /* ok */
1375 MOD_ACTION_RETURN, /* handled */
1376 MOD_ACTION_RETURN, /* invalid */
1377 MOD_ACTION_RETURN, /* userlock */
1378 MOD_ACTION_RETURN, /* notfound */
1379 MOD_ACTION_RETURN, /* noop */
1380 MOD_ACTION_RETURN /* updated */
1387 MOD_ACTION_RETURN, /* reject */
1388 MOD_ACTION_RETURN, /* fail */
1390 MOD_ACTION_RETURN, /* handled */
1391 MOD_ACTION_RETURN, /* invalid */
1392 MOD_ACTION_RETURN, /* userlock */
1399 MOD_ACTION_RETURN, /* reject */
1401 MOD_ACTION_RETURN, /* ok */
1402 MOD_ACTION_RETURN, /* handled */
1403 MOD_ACTION_RETURN, /* invalid */
1404 MOD_ACTION_RETURN, /* userlock */
1405 MOD_ACTION_RETURN, /* notfound */
1406 MOD_ACTION_RETURN, /* noop */
1407 MOD_ACTION_RETURN /* updated */
1411 MOD_ACTION_RETURN, /* reject */
1413 MOD_ACTION_RETURN, /* ok */
1414 MOD_ACTION_RETURN, /* handled */
1415 MOD_ACTION_RETURN, /* invalid */
1416 MOD_ACTION_RETURN, /* userlock */
1418 MOD_ACTION_RETURN, /* noop */
1419 MOD_ACTION_RETURN /* updated */
1426 MOD_ACTION_RETURN, /* reject */
1427 MOD_ACTION_RETURN, /* fail */
1429 MOD_ACTION_RETURN, /* handled */
1430 MOD_ACTION_RETURN, /* invalid */
1431 MOD_ACTION_RETURN, /* userlock */
1438 MOD_ACTION_RETURN, /* reject */
1440 MOD_ACTION_RETURN, /* ok */
1441 MOD_ACTION_RETURN, /* handled */
1442 MOD_ACTION_RETURN, /* invalid */
1443 MOD_ACTION_RETURN, /* userlock */
1444 MOD_ACTION_RETURN, /* notfound */
1445 MOD_ACTION_RETURN, /* noop */
1446 MOD_ACTION_RETURN /* updated */
1450 MOD_ACTION_RETURN, /* reject */
1452 MOD_ACTION_RETURN, /* ok */
1453 MOD_ACTION_RETURN, /* handled */
1454 MOD_ACTION_RETURN, /* invalid */
1455 MOD_ACTION_RETURN, /* userlock */
1457 MOD_ACTION_RETURN, /* noop */
1458 MOD_ACTION_RETURN /* updated */
1465 MOD_ACTION_RETURN, /* reject */
1466 MOD_ACTION_RETURN, /* fail */
1468 MOD_ACTION_RETURN, /* handled */
1469 MOD_ACTION_RETURN, /* invalid */
1470 MOD_ACTION_RETURN, /* userlock */
1477 MOD_ACTION_RETURN, /* reject */
1479 MOD_ACTION_RETURN, /* ok */
1480 MOD_ACTION_RETURN, /* handled */
1481 MOD_ACTION_RETURN, /* invalid */
1482 MOD_ACTION_RETURN, /* userlock */
1483 MOD_ACTION_RETURN, /* notfound */
1484 MOD_ACTION_RETURN, /* noop */
1485 MOD_ACTION_RETURN /* updated */
1489 MOD_ACTION_RETURN, /* reject */
1491 MOD_ACTION_RETURN, /* ok */
1492 MOD_ACTION_RETURN, /* handled */
1493 MOD_ACTION_RETURN, /* invalid */
1494 MOD_ACTION_RETURN, /* userlock */
1496 MOD_ACTION_RETURN, /* noop */
1497 MOD_ACTION_RETURN /* updated */
1506 MOD_ACTION_RETURN, /* reject */
1507 MOD_ACTION_RETURN, /* fail */
1509 MOD_ACTION_RETURN, /* handled */
1510 MOD_ACTION_RETURN, /* invalid */
1511 MOD_ACTION_RETURN, /* userlock */
1518 MOD_ACTION_RETURN, /* reject */
1520 MOD_ACTION_RETURN, /* ok */
1521 MOD_ACTION_RETURN, /* handled */
1522 MOD_ACTION_RETURN, /* invalid */
1523 MOD_ACTION_RETURN, /* userlock */
1524 MOD_ACTION_RETURN, /* notfound */
1525 MOD_ACTION_RETURN, /* noop */
1526 MOD_ACTION_RETURN /* updated */
1530 MOD_ACTION_RETURN, /* reject */
1532 MOD_ACTION_RETURN, /* ok */
1533 MOD_ACTION_RETURN, /* handled */
1534 MOD_ACTION_RETURN, /* invalid */
1535 MOD_ACTION_RETURN, /* userlock */
1537 MOD_ACTION_RETURN, /* noop */
1538 MOD_ACTION_RETURN /* updated */
1545 MOD_ACTION_RETURN, /* reject */
1546 MOD_ACTION_RETURN, /* fail */
1548 MOD_ACTION_RETURN, /* handled */
1549 MOD_ACTION_RETURN, /* invalid */
1550 MOD_ACTION_RETURN, /* userlock */
1557 MOD_ACTION_RETURN, /* reject */
1559 MOD_ACTION_RETURN, /* ok */
1560 MOD_ACTION_RETURN, /* handled */
1561 MOD_ACTION_RETURN, /* invalid */
1562 MOD_ACTION_RETURN, /* userlock */
1563 MOD_ACTION_RETURN, /* notfound */
1564 MOD_ACTION_RETURN, /* noop */
1565 MOD_ACTION_RETURN /* updated */
1569 MOD_ACTION_RETURN, /* reject */
1571 MOD_ACTION_RETURN, /* ok */
1572 MOD_ACTION_RETURN, /* handled */
1573 MOD_ACTION_RETURN, /* invalid */
1574 MOD_ACTION_RETURN, /* userlock */
1576 MOD_ACTION_RETURN, /* noop */
1577 MOD_ACTION_RETURN /* updated */
1583 /** Validate and fixup a map that's part of an update section.
1585 * @param map to validate.
1586 * @param ctx data to pass to fixup function (currently unused).
1587 * @return 0 if valid else -1.
1589 int modcall_fixup_update(value_pair_map_t *map, UNUSED void *ctx)
1591 CONF_PAIR *cp = cf_item_to_pair(map->ci);
1594 * Anal-retentive checks.
1596 if (DEBUG_ENABLED3) {
1597 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->lhs->name[0] != '&')) {
1598 WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
1599 cf_pair_filename(cp), cf_pair_lineno(cp),
1600 map->lhs->name, fr_int2str(fr_tokens, map->op, "<INVALID>"));
1603 if ((map->rhs->type == TMPL_TYPE_ATTR) && (map->rhs->name[0] != '&')) {
1604 WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
1605 cf_pair_filename(cp), cf_pair_lineno(cp),
1606 fr_int2str(fr_tokens, map->op, "<INVALID>"), map->rhs->name);
1611 * Values used by unary operators should be literal ANY
1613 * We then free the template and alloc a NULL one instead.
1615 if (map->op == T_OP_CMP_FALSE) {
1616 if ((map->rhs->type != TMPL_TYPE_LITERAL) || (strcmp(map->rhs->name, "ANY") != 0)) {
1617 WARN("%s[%d] Wildcard deletion MUST use '!* ANY'",
1618 cf_pair_filename(cp), cf_pair_lineno(cp));
1621 tmpl_free(&map->rhs);
1623 map->rhs = tmpl_alloc(map, TMPL_TYPE_NULL, NULL, 0);
1627 * Lots of sanity checks for insane people...
1631 * What exactly where you expecting to happen here?
1633 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
1634 (map->rhs->type == TMPL_TYPE_LIST)) {
1635 cf_log_err(map->ci, "Can't copy list into an attribute");
1640 * Depending on the attribute type, some operators are disallowed.
1642 if (map->lhs->type == TMPL_TYPE_ATTR) {
1645 cf_log_err(map->ci, "Invalid operator for attribute");
1654 case T_OP_CMP_FALSE:
1660 if (map->lhs->type == TMPL_TYPE_LIST) {
1662 * Can't copy an xlat expansion or literal into a list,
1663 * we don't know what type of attribute we'd need
1666 * The only exception is where were using a unary
1669 if (map->op != T_OP_CMP_FALSE) switch (map->rhs->type) {
1670 case TMPL_TYPE_XLAT:
1671 case TMPL_TYPE_LITERAL:
1672 cf_log_err(map->ci, "Can't copy value into list (we don't know which attribute to create)");
1680 * Only += and :=, and !* operators are supported
1684 case T_OP_CMP_FALSE:
1688 if ((map->rhs->type != TMPL_TYPE_LIST) &&
1689 (map->rhs->type != TMPL_TYPE_EXEC)) {
1690 cf_log_err(map->ci, "Invalid source for list assignment '%s += ...'", map->lhs->name);
1696 if (map->rhs->type == TMPL_TYPE_EXEC) {
1697 WARN("%s[%d] Please change ':=' to '=' for list assignment",
1698 cf_pair_filename(cp), cf_pair_lineno(cp));
1701 if (map->rhs->type != TMPL_TYPE_LIST) {
1702 cf_log_err(map->ci, "Invalid source for list assignment '%s := ...'", map->lhs->name);
1708 if (map->rhs->type != TMPL_TYPE_EXEC) {
1709 cf_log_err(map->ci, "Invalid source for list assignment '%s = ...'", map->lhs->name);
1715 cf_log_err(map->ci, "Operator \"%s\" not allowed for list assignment",
1716 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1722 * If the map has a unary operator there's no further
1723 * processing we need to, as RHS is unused.
1725 if (map->op == T_OP_CMP_FALSE) return 0;
1728 * If LHS is an attribute, and RHS is a literal, we can
1729 * preparse the information into a TMPL_TYPE_DATA.
1731 * Unless it's a unary operator in which case we
1734 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->rhs->type == TMPL_TYPE_LITERAL)) {
1736 * It's a literal string, just copy it.
1737 * Don't escape anything.
1739 if (!cf_new_escape &&
1740 (map->lhs->tmpl_da->type == PW_TYPE_STRING) &&
1741 (cf_pair_value_type(cp) == T_SINGLE_QUOTED_STRING)) {
1742 tmpl_cast_in_place_str(map->rhs);
1744 if (!tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da)) {
1745 cf_log_err(map->ci, "%s", fr_strerror());
1750 * Fixup LHS da if it doesn't match the type
1753 if (map->lhs->tmpl_da->type != map->rhs->tmpl_data_type) {
1754 DICT_ATTR const *da;
1756 da = dict_attrbytype(map->lhs->tmpl_da->attr, map->lhs->tmpl_da->vendor,
1757 map->rhs->tmpl_data_type);
1759 fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1760 fr_int2str(dict_attr_types, map->rhs->tmpl_data_type,
1761 "<INVALID>"), map->lhs->tmpl_da->name);
1764 map->lhs->tmpl_da = da;
1767 } /* else we can't precompile the data */
1774 static modcallable *do_compile_modupdate(modcallable *parent, rlm_components_t component,
1775 CONF_SECTION *cs, char const *name2)
1779 modcallable *csingle;
1781 value_pair_map_t *head;
1784 * This looks at cs->name2 to determine which list to update
1786 rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
1787 if (rcode < 0) return NULL; /* message already printed */
1789 cf_log_err_cs(cs, "'update' sections cannot be empty");
1793 g = talloc_zero(parent, modgroup);
1794 csingle = mod_grouptocallable(g);
1796 csingle->parent = parent;
1797 csingle->next = NULL;
1800 csingle->name = name2;
1804 csingle->type = MOD_UPDATE;
1805 csingle->method = component;
1807 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
1808 sizeof(csingle->actions));
1810 g->grouptype = GROUPTYPE_SIMPLE;
1813 g->map = talloc_steal(g, head);
1819 static modcallable *do_compile_modswitch (modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1824 bool had_seen_default = false;
1825 modcallable *csingle;
1828 value_pair_tmpl_t *vpt;
1830 name2 = cf_section_name2(cs);
1832 cf_log_err_cs(cs, "You must specify a variable to switch over for 'switch'");
1836 if (!cf_item_find_next(cs, NULL)) {
1837 cf_log_err_cs(cs, "'switch' statements cannot be empty");
1842 * Create the template. If we fail, AND it's a bare word
1843 * with &Foo-Bar, it MAY be an attribute defined by a
1844 * module. Allow it for now. The pass2 checks below
1847 type = cf_section_name2_type(cs);
1848 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
1849 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1850 char *spaces, *text;
1852 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1854 cf_log_err_cs(cs, "Syntax error");
1855 cf_log_err_cs(cs, "%s", name2);
1856 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1858 talloc_free(spaces);
1865 * Otherwise a NULL vpt may refer to an attribute defined
1866 * by a module. That is checked in pass 2.
1870 * Walk through the children of the switch section,
1871 * ensuring that they're all 'case' statements
1873 for (ci = cf_item_find_next(cs, NULL);
1875 ci = cf_item_find_next(cs, ci)) {
1876 CONF_SECTION *subcs;
1879 if (!cf_item_is_section(ci)) {
1880 if (!cf_item_is_pair(ci)) continue;
1882 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1887 subcs = cf_item_to_section(ci); /* can't return NULL */
1888 name1 = cf_section_name1(subcs);
1890 if (strcmp(name1, "case") != 0) {
1891 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1896 name2 = cf_section_name2(subcs);
1898 if (!had_seen_default) {
1899 had_seen_default = true;
1903 cf_log_err(ci, "Cannot have two 'default' case statements");
1909 csingle = do_compile_modgroup(parent, component, cs,
1918 g = mod_callabletogroup(csingle);
1919 g->vpt = talloc_steal(g, vpt);
1924 static modcallable *do_compile_modcase(modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1928 modcallable *csingle;
1930 value_pair_tmpl_t *vpt;
1932 if (!parent || (parent->type != MOD_SWITCH)) {
1933 cf_log_err_cs(cs, "\"case\" statements may only appear within a \"switch\" section");
1938 * case THING means "match THING"
1939 * case means "match anything"
1941 name2 = cf_section_name2(cs);
1946 type = cf_section_name2_type(cs);
1948 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
1949 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1950 char *spaces, *text;
1952 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1954 cf_log_err_cs(cs, "Syntax error");
1955 cf_log_err_cs(cs, "%s", name2);
1956 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1958 talloc_free(spaces);
1965 * Otherwise a NULL vpt may refer to an attribute defined
1966 * by a module. That is checked in pass 2.
1973 csingle = do_compile_modgroup(parent, component, cs,
1983 * The interpretor expects this to be NULL for the
1984 * default case. do_compile_modgroup sets it to name2,
1985 * unless name2 is NULL, in which case it sets it to name1.
1987 csingle->name = name2;
1989 g = mod_callabletogroup(csingle);
1990 g->vpt = talloc_steal(g, vpt);
1993 * Set all of it's codes to return, so that
1994 * when we pick a 'case' statement, we don't
1995 * fall through to processing the next one.
1997 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1998 csingle->actions[i] = MOD_ACTION_RETURN;
2004 static modcallable *do_compile_modforeach(modcallable *parent,
2005 rlm_components_t component, CONF_SECTION *cs)
2009 modcallable *csingle;
2012 value_pair_tmpl_t *vpt;
2014 name2 = cf_section_name2(cs);
2017 "You must specify an attribute to loop over in 'foreach'");
2021 if (!cf_item_find_next(cs, NULL)) {
2022 cf_log_err_cs(cs, "'foreach' blocks cannot be empty");
2027 * Create the template. If we fail, AND it's a bare word
2028 * with &Foo-Bar, it MAY be an attribute defined by a
2029 * module. Allow it for now. The pass2 checks below
2032 type = cf_section_name2_type(cs);
2033 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST);
2034 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
2035 char *spaces, *text;
2037 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
2039 cf_log_err_cs(cs, "Syntax error");
2040 cf_log_err_cs(cs, "%s", name2);
2041 cf_log_err_cs(cs, "%s^ %s", spaces, text);
2043 talloc_free(spaces);
2050 * If we don't have a negative return code, we must have a vpt
2051 * (mostly to quiet coverity).
2055 if ((vpt->type != TMPL_TYPE_ATTR) && (vpt->type != TMPL_TYPE_LIST)) {
2056 cf_log_err_cs(cs, "MUST use attribute or list reference in 'foreach'");
2061 * Fix up the template to iterate over all instances of
2062 * the attribute. In a perfect consistent world, users would do
2063 * foreach &attr[*], but that's taking the consistency thing a bit far.
2065 vpt->tmpl_num = NUM_ALL;
2067 csingle = do_compile_modgroup(parent, component, cs,
2068 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2076 g = mod_callabletogroup(csingle);
2082 static modcallable *do_compile_modbreak(modcallable *parent,
2083 rlm_components_t component, CONF_ITEM const *ci)
2085 CONF_SECTION const *cs = NULL;
2087 for (cs = cf_item_parent(ci);
2089 cs = cf_item_parent(cf_section_to_item(cs))) {
2090 if (strcmp(cf_section_name1(cs), "foreach") == 0) {
2096 cf_log_err(ci, "'break' can only be used in a 'foreach' section");
2100 return do_compile_modgroup(parent, component, NULL,
2101 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2106 static modcallable *do_compile_modserver(modcallable *parent,
2107 rlm_components_t component, CONF_ITEM *ci,
2112 modcallable *csingle;
2113 CONF_SECTION *subcs;
2116 subcs = cf_section_sub_find_name2(cs, comp2str[component], NULL);
2118 cf_log_err(ci, "Server %s has no %s section",
2119 server, comp2str[component]);
2123 mr = talloc_zero(parent, modref);
2125 csingle = mod_reftocallable(mr);
2126 csingle->parent = parent;
2127 csingle->next = NULL;
2128 csingle->name = name;
2129 csingle->type = MOD_REFERENCE;
2130 csingle->method = component;
2132 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2133 sizeof(csingle->actions));
2135 mr->ref_name = strdup(server);
2141 static modcallable *do_compile_modxlat(modcallable *parent,
2142 rlm_components_t component, char const *fmt)
2144 modcallable *csingle;
2147 mx = talloc_zero(parent, modxlat);
2149 csingle = mod_xlattocallable(mx);
2150 csingle->parent = parent;
2151 csingle->next = NULL;
2152 csingle->name = "expand";
2153 csingle->type = MOD_XLAT;
2154 csingle->method = component;
2156 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2157 sizeof(csingle->actions));
2159 mx->xlat_name = strdup(fmt);
2160 if (fmt[0] != '%') {
2164 strcpy(mx->xlat_name, fmt + 1);
2165 p = strrchr(mx->xlat_name, '`');
2173 * redundant, etc. can refer to modules or groups, but not much else.
2175 static int all_children_are_modules(CONF_SECTION *cs, char const *name)
2179 for (ci=cf_item_find_next(cs, NULL);
2181 ci=cf_item_find_next(cs, ci)) {
2183 * If we're a redundant, etc. group, then the
2184 * intention is to call modules, rather than
2185 * processing logic. These checks aren't
2186 * *strictly* necessary, but they keep the users
2187 * from doing crazy things.
2189 if (cf_item_is_section(ci)) {
2190 CONF_SECTION *subcs = cf_item_to_section(ci);
2191 char const *name1 = cf_section_name1(subcs);
2193 if ((strcmp(name1, "if") == 0) ||
2194 (strcmp(name1, "else") == 0) ||
2195 (strcmp(name1, "elsif") == 0) ||
2196 (strcmp(name1, "update") == 0) ||
2197 (strcmp(name1, "switch") == 0) ||
2198 (strcmp(name1, "case") == 0)) {
2199 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
2206 if (cf_item_is_pair(ci)) {
2207 CONF_PAIR *cp = cf_item_to_pair(ci);
2208 if (cf_pair_value(cp) != NULL) {
2210 "Entry with no value is invalid");
2221 * Compile one entry of a module call.
2223 static modcallable *do_compile_modsingle(modcallable *parent,
2224 rlm_components_t component, CONF_ITEM *ci,
2226 char const **modname)
2228 char const *modrefname, *p;
2230 modcallable *csingle;
2231 module_instance_t *this;
2232 CONF_SECTION *cs, *subcs, *modules;
2234 char const *realname;
2235 rlm_components_t method = component;
2237 if (cf_item_is_section(ci)) {
2240 cs = cf_item_to_section(ci);
2241 modrefname = cf_section_name1(cs);
2242 name2 = cf_section_name2(cs);
2243 if (!name2) name2 = "";
2246 * group{}, redundant{}, or append{} may appear
2247 * where a single module instance was expected.
2248 * In that case, we hand it off to
2251 if (strcmp(modrefname, "group") == 0) {
2253 return do_compile_modgroup(parent, component, cs,
2255 grouptype, MOD_GROUP);
2257 } else if (strcmp(modrefname, "redundant") == 0) {
2260 if (!all_children_are_modules(cs, modrefname)) {
2264 return do_compile_modgroup(parent, component, cs,
2265 GROUPTYPE_REDUNDANT,
2266 grouptype, MOD_GROUP);
2268 } else if (strcmp(modrefname, "append") == 0) {
2270 return do_compile_modgroup(parent, component, cs,
2272 grouptype, MOD_GROUP);
2274 } else if (strcmp(modrefname, "load-balance") == 0) {
2277 if (!all_children_are_modules(cs, modrefname)) {
2281 return do_compile_modgroup(parent, component, cs,
2283 grouptype, MOD_LOAD_BALANCE);
2285 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2288 if (!all_children_are_modules(cs, modrefname)) {
2292 return do_compile_modgroup(parent, component, cs,
2293 GROUPTYPE_REDUNDANT,
2294 grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2297 } else if (strcmp(modrefname, "if") == 0) {
2298 if (!cf_section_name2(cs)) {
2299 cf_log_err(ci, "'if' without condition");
2304 csingle= do_compile_modgroup(parent, component, cs,
2307 if (!csingle) return NULL;
2312 } else if (strcmp(modrefname, "elsif") == 0) {
2314 ((parent->type == MOD_LOAD_BALANCE) ||
2315 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2316 cf_log_err(ci, "'elsif' cannot be used in this section");
2320 if (!cf_section_name2(cs)) {
2321 cf_log_err(ci, "'elsif' without condition");
2326 return do_compile_modgroup(parent, component, cs,
2328 grouptype, MOD_ELSIF);
2330 } else if (strcmp(modrefname, "else") == 0) {
2332 ((parent->type == MOD_LOAD_BALANCE) ||
2333 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2334 cf_log_err(ci, "'else' cannot be used in this section section");
2338 if (cf_section_name2(cs)) {
2339 cf_log_err(ci, "Cannot have conditions on 'else'");
2344 return do_compile_modgroup(parent, component, cs,
2346 grouptype, MOD_ELSE);
2348 } else if (strcmp(modrefname, "update") == 0) {
2351 return do_compile_modupdate(parent, component, cs,
2354 } else if (strcmp(modrefname, "switch") == 0) {
2357 return do_compile_modswitch (parent, component, cs);
2359 } else if (strcmp(modrefname, "case") == 0) {
2362 return do_compile_modcase(parent, component, cs);
2364 } else if (strcmp(modrefname, "foreach") == 0) {
2367 return do_compile_modforeach(parent, component, cs);
2370 } /* else it's something like sql { fail = 1 ...} */
2372 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2376 * 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");
2393 * In-place xlat's via %{...}.
2395 * This should really be removed from the server.
2397 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2398 (modrefname[0] == '`')) {
2399 return do_compile_modxlat(parent, component,
2406 * These can't be over-ridden.
2408 if (strcmp(modrefname, "break") == 0) {
2409 if (!cf_item_is_pair(ci)) {
2410 cf_log_err(ci, "Invalid use of 'break' as section name.");
2414 return do_compile_modbreak(parent, component, ci);
2417 if (strcmp(modrefname, "return") == 0) {
2418 if (!cf_item_is_pair(ci)) {
2419 cf_log_err(ci, "Invalid use of 'return' as section name.");
2423 return do_compile_modgroup(parent, component, NULL,
2424 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2430 * Run a virtual server. This is really terrible and
2431 * should be deleted.
2433 if (strncmp(modrefname, "server[", 7) == 0) {
2436 if (!cf_item_is_pair(ci)) {
2437 cf_log_err(ci, "Invalid syntax");
2441 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2442 p = strrchr(buffer, ']');
2443 if (!p || p[1] != '\0' || (p == buffer)) {
2444 cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2448 buffer[p - buffer] = '\0';
2450 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2452 cf_log_err(ci, "No such server \"%s\".", buffer);
2457 * Ignore stupid attempts to over-ride the return
2460 return do_compile_modserver(parent, component, ci,
2461 modrefname, cs, buffer);
2465 * We now have a name. It can be one of two forms. A
2466 * bare module name, or a section named for the module,
2467 * with over-rides for the return codes.
2469 * The name can refer to a real module, in the "modules"
2470 * section. In that case, the name will be either the
2471 * first or second name of the sub-section of "modules".
2473 * Or, the name can refer to a policy, in the "policy"
2474 * section. In that case, the name will be first name of
2475 * the sub-section of "policy". Unless it's a "redudant"
2478 * Or, the name can refer to a "module.method", in which
2479 * case we're calling a different method than normal for
2482 * Or, the name can refer to a virtual module, in the
2483 * "instantiate" section. In that case, the name will be
2484 * the first of the sub-section of "instantiate". Unless
2485 * it's a "redudant" block...
2487 * We try these in sequence, from the bottom up. This is
2488 * so that things in "instantiate" and "policy" can
2489 * over-ride calls to real modules.
2496 * instantiate { ... name { ...} ... }
2497 * instantiate { ... name.method { ...} ... }
2498 * policy { ... name { .. } .. }
2499 * policy { ... name.method { .. } .. }
2501 * The "instantiate" virtual modules are identical to the
2502 * policies at this point. We should probably get rid of
2503 * the "instantiate" ones, as they're duplicate and
2507 cs = cf_section_find("instantiate");
2508 if (cs) subcs = cf_section_sub_find_name2(cs, NULL,
2511 (cs = cf_section_find("policy")) != NULL) {
2514 snprintf(buffer, sizeof(buffer), "%s.%s",
2515 modrefname, comp2str[component]);
2518 * Prefer name.section, then name.
2520 subcs = cf_section_sub_find_name2(cs, NULL,
2523 subcs = cf_section_sub_find_name2(cs, NULL,
2529 * Check that we're not creating a loop. We may
2530 * be compiling an "sql" module reference inside
2531 * of an "sql" policy. If so, we allow the
2532 * second "sql" to refer to the module.
2534 for (loop = cf_item_parent(ci);
2536 loop = cf_item_parent(cf_section_to_item(loop))) {
2537 if (loop == subcs) {
2543 * We've found the relevant entry. It MUST be a
2546 * However, it can be a "redundant" block, or just a
2551 * modules.c takes care of ensuring that this is:
2554 * load-balance foo { ...
2555 * redundant foo { ...
2556 * redundant-load-balance foo { ...
2558 * We can just recurs to compile the section as
2559 * if it was found here.
2561 if (cf_section_name2(subcs)) {
2562 csingle = do_compile_modsingle(parent,
2564 cf_section_to_item(subcs),
2573 * So we compile it like it was:
2577 csingle = do_compile_modgroup(parent,
2581 grouptype, MOD_GROUP);
2585 * Return the compiled thing if we can.
2587 if (!csingle) return NULL;
2588 if (cf_item_is_pair(ci)) return csingle;
2591 * Else we have a reference to a policy, and that reference
2592 * over-rides the return codes for the policy!
2594 goto action_override;
2598 * Not a virtual module. It must be a real module.
2600 modules = cf_section_find("modules");
2602 realname = modrefname;
2606 * Try to load the optional module.
2608 if (realname[0] == '-') realname++;
2611 * As of v3, the "modules" section contains
2612 * modules we use. Configuration for other
2613 * modules belongs in raddb/mods-available/,
2614 * which isn't loaded into the "modules" section.
2616 if (cf_section_sub_find_name2(modules, NULL, realname)) {
2617 this = find_module_instance(modules, realname, true);
2618 if (this) goto allocate_csingle;
2623 if (realname != modrefname) {
2629 * We were asked to MAYBE load it and it
2630 * doesn't exist. Return a soft error.
2632 if (realname != modrefname) {
2633 *modname = modrefname;
2640 * No module found by that name. Maybe we're calling
2643 p = strrchr(modrefname, '.');
2649 * Find the component.
2651 for (i = RLM_COMPONENT_AUTH;
2652 i < RLM_COMPONENT_COUNT;
2654 if (strcmp(p, comp2str[i]) == 0) {
2657 strlcpy(buffer, modrefname, sizeof(buffer));
2658 buffer[p - modrefname - 1] = '\0';
2661 this = find_module_instance(modules, buffer, true);
2664 goto allocate_csingle;
2670 * FIXME: check for "module", and give error "no
2671 * such component" when we don't find the method.
2676 * Can't de-reference it to anything. Ugh.
2679 cf_log_err(ci, "Failed to find \"%s\" as a module or policy.", modrefname);
2680 cf_log_err(ci, "Please verify that the configuration exists in %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2684 * We know it's all OK, allocate the structures, and fill
2689 * Check if the module in question has the necessary
2692 if (!this->entry->module->methods[method]) {
2693 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2698 single = talloc_zero(parent, modsingle);
2699 single->modinst = this;
2700 *modname = this->entry->module->name;
2702 csingle = mod_singletocallable(single);
2703 csingle->parent = parent;
2704 csingle->next = NULL;
2705 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2706 memcpy(csingle->actions, defaultactions[component][grouptype],
2707 sizeof csingle->actions);
2708 } else { /* inside Auth-Type has different rules */
2709 memcpy(csingle->actions, defaultactions[RLM_COMPONENT_AUTZ][grouptype],
2710 sizeof csingle->actions);
2712 rad_assert(modrefname != NULL);
2713 csingle->name = realname;
2714 csingle->type = MOD_SINGLE;
2715 csingle->method = method;
2719 * Over-ride the default return codes of the module.
2721 if (cf_item_is_section(ci)) {
2724 cs = cf_item_to_section(ci);
2725 for (csi=cf_item_find_next(cs, NULL);
2727 csi=cf_item_find_next(cs, csi)) {
2729 if (cf_item_is_section(csi)) {
2730 cf_log_err(csi, "Subsection of module instance call not allowed");
2731 talloc_free(csingle);
2735 if (!cf_item_is_pair(csi)) continue;
2737 if (!compile_action(csingle, cf_item_to_pair(csi))) {
2738 talloc_free(csingle);
2747 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2748 modcallable **parent,
2749 rlm_components_t component, CONF_ITEM *ci,
2750 char const **modname)
2757 CONF_SECTION *parentcs;
2759 g = talloc_zero(ctx, modgroup);
2760 memset(g, 0, sizeof(*g));
2761 g->grouptype = GROUPTYPE_SIMPLE;
2762 c = mod_grouptocallable(g);
2765 defaultactions[component][GROUPTYPE_SIMPLE],
2766 sizeof(c->actions));
2768 parentcs = cf_item_parent(ci);
2769 c->name = cf_section_name2(parentcs);
2771 c->name = cf_section_name1(parentcs);
2774 c->type = MOD_GROUP;
2775 c->method = component;
2778 *parent = mod_grouptocallable(g);
2781 ret = do_compile_modsingle(*parent, component, ci,
2784 dump_tree(component, ret);
2790 * Internal compile group code.
2792 static modcallable *do_compile_modgroup(modcallable *parent,
2793 rlm_components_t component, CONF_SECTION *cs,
2794 int grouptype, int parentgrouptype, int mod_type)
2801 g = talloc_zero(parent, modgroup);
2802 g->grouptype = grouptype;
2806 c = mod_grouptocallable(g);
2810 memset(c->actions, 0, sizeof(c->actions));
2812 if (!cs) { /* only for "break" and "return" */
2818 * Remember the name for printing, etc.
2820 * FIXME: We may also want to put the names into a
2821 * rbtree, so that groups can reference each other...
2823 c->name = cf_section_name2(cs);
2825 c->name = cf_section_name1(cs);
2826 if ((strcmp(c->name, "group") == 0) ||
2827 (strcmp(c->name, "redundant") == 0)) {
2829 } else if (c->type == MOD_GROUP) {
2830 c->type = MOD_POLICY;
2836 * Do load-time optimizations
2838 if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2841 rad_assert(parent != NULL);
2843 if (c->type == MOD_IF) {
2844 g->cond = cf_data_find(g->cs, "if");
2845 rad_assert(g->cond != NULL);
2848 if (g->cond->type == COND_TYPE_FALSE) {
2849 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2850 unlang_keyword[g->mc.type],
2851 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2855 } else if (c->type == MOD_ELSIF) {
2857 g->cond = cf_data_find(g->cs, "if");
2858 rad_assert(g->cond != NULL);
2860 rad_assert(parent != NULL);
2861 p = mod_callabletogroup(parent);
2863 rad_assert(p->tail != NULL);
2865 f = mod_callabletogroup(p->tail);
2866 rad_assert((f->mc.type == MOD_IF) ||
2867 (f->mc.type == MOD_ELSIF));
2870 * If we took the previous condition, we
2871 * don't need to take this one.
2873 * We reset our condition to 'true', so
2874 * that subsequent sections can check
2875 * that they don't need to be executed.
2877 if (f->cond->type == COND_TYPE_TRUE) {
2879 INFO(" # Skipping contents of '%s' as previous '%s' is always 'true' -- %s:%d",
2880 unlang_keyword[g->mc.type],
2881 unlang_keyword[f->mc.type],
2882 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2889 rad_assert(c->type == MOD_ELSE);
2891 rad_assert(parent != NULL);
2892 p = mod_callabletogroup(parent);
2894 rad_assert(p->tail != NULL);
2896 f = mod_callabletogroup(p->tail);
2897 rad_assert((f->mc.type == MOD_IF) ||
2898 (f->mc.type == MOD_ELSIF));
2901 * If we took the previous condition, we
2902 * don't need to take this one.
2904 if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2908 * Else we need to compile this section
2914 * Loop over the children of this group.
2916 for (ci=cf_item_find_next(cs, NULL);
2918 ci=cf_item_find_next(cs, ci)) {
2921 * Sections are references to other groups, or
2922 * to modules with updated return codes.
2924 if (cf_item_is_section(ci)) {
2925 char const *junk = NULL;
2926 modcallable *single;
2927 CONF_SECTION *subcs = cf_item_to_section(ci);
2929 single = do_compile_modsingle(c, component, ci,
2932 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2933 cf_section_name1(subcs));
2937 add_child(g, single);
2939 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2943 char const *attr, *value;
2944 CONF_PAIR *cp = cf_item_to_pair(ci);
2946 attr = cf_pair_attr(cp);
2947 value = cf_pair_value(cp);
2950 * A CONF_PAIR is either a module
2951 * instance with no actions
2955 modcallable *single;
2956 char const *junk = NULL;
2958 single = do_compile_modsingle(c,
2964 if (cf_item_is_pair(ci) &&
2965 cf_pair_attr(cf_item_to_pair(ci))[0] == '-') {
2970 "Failed to parse \"%s\" entry.",
2975 add_child(g, single);
2978 * Or a module instance with action.
2980 } else if (!compile_action(c, cp)) {
2983 } /* else it worked */
2989 * Set the default actions, if they haven't already been
2992 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2993 if (!c->actions[i]) {
2994 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2995 c->actions[i] = defaultactions[component][parentgrouptype][i];
2996 } else { /* inside Auth-Type has different rules */
2997 c->actions[i] = defaultactions[RLM_COMPONENT_AUTZ][parentgrouptype][i];
3007 if (grouptype != GROUPTYPE_REDUNDANT) break;
3010 case MOD_LOAD_BALANCE:
3011 case MOD_REDUNDANT_LOAD_BALANCE:
3013 cf_log_err_cs(g->cs, "%s sections cannot be empty",
3014 cf_section_name1(g->cs));
3021 * FIXME: If there are no children, return NULL?
3023 return mod_grouptocallable(g);
3026 modcallable *compile_modgroup(modcallable *parent,
3027 rlm_components_t component, CONF_SECTION *cs)
3029 modcallable *ret = do_compile_modgroup(parent, component, cs,
3031 GROUPTYPE_SIMPLE, MOD_GROUP);
3033 if (debug_flag > 3) {
3034 modcall_debug(ret, 2);
3040 void add_to_modcallable(modcallable *parent, modcallable *this)
3044 rad_assert(this != NULL);
3045 rad_assert(parent != NULL);
3047 g = mod_callabletogroup(parent);
3054 static bool pass2_xlat_compile(CONF_ITEM const *ci, value_pair_tmpl_t **pvpt, bool convert,
3055 DICT_ATTR const *da)
3061 value_pair_tmpl_t *vpt;
3065 rad_assert(vpt->type == TMPL_TYPE_XLAT);
3067 fmt = talloc_typed_strdup(vpt, vpt->name);
3068 slen = xlat_tokenize(vpt, fmt, &head, &error);
3071 char *spaces, *text;
3073 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3075 cf_log_err(ci, "Failed parsing expanded string:");
3076 cf_log_err(ci, "%s", text);
3077 cf_log_err(ci, "%s^ %s", spaces, error);
3079 talloc_free(spaces);
3085 * Convert %{Attribute-Name} to &Attribute-Name
3088 value_pair_tmpl_t *attr;
3090 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
3093 * If it's a virtual attribute, leave it
3096 if (attr->tmpl_da->flags.virtual) {
3102 * If the attribute is of incompatible
3103 * type, leave it alone.
3105 if (da && (da->type != attr->tmpl_da->type)) {
3110 if (cf_item_is_pair(ci)) {
3111 CONF_PAIR *cp = cf_item_to_pair(ci);
3113 WARN("%s[%d] Please change %%{%s} to &%s",
3114 cf_pair_filename(cp), cf_pair_lineno(cp),
3115 attr->name, attr->name);
3117 CONF_SECTION *cs = cf_item_to_section(ci);
3119 WARN("%s[%d] Please change %%{%s} to &%s",
3120 cf_section_filename(cs), cf_section_lineno(cs),
3121 attr->name, attr->name);
3130 * Re-write it to be a pre-parsed XLAT structure.
3132 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3133 vpt->tmpl_xlat = head;
3140 static bool pass2_regex_compile(CONF_ITEM const *ci, value_pair_tmpl_t *vpt)
3145 rad_assert(vpt->type == TMPL_TYPE_REGEX);
3148 * It's a dynamic expansion. We can't expand the string,
3149 * but we can pre-parse it as an xlat struct. In that
3150 * case, we convert it to a pre-compiled XLAT.
3152 * This is a little more complicated than it needs to be
3153 * because radius_evaluate_map() keys off of the src
3154 * template type, instead of the operators. And, the
3155 * pass2_xlat_compile() function expects to get passed an
3156 * XLAT instead of a REGEX.
3158 if (strchr(vpt->name, '%')) {
3159 vpt->type = TMPL_TYPE_XLAT;
3160 return pass2_xlat_compile(ci, &vpt, false, NULL);
3163 slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3164 vpt->tmpl_iflag, vpt->tmpl_mflag, true, false);
3166 char *spaces, *text;
3168 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3170 cf_log_err(ci, "Invalid regular expression:");
3171 cf_log_err(ci, "%s", text);
3172 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3174 talloc_free(spaces);
3180 vpt->type = TMPL_TYPE_REGEX_STRUCT;
3181 vpt->tmpl_preg = preg;
3187 static bool pass2_fixup_undefined(CONF_ITEM const *ci, value_pair_tmpl_t *vpt)
3189 DICT_ATTR const *da;
3191 rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3193 da = dict_attrbyname(vpt->tmpl_unknown_name);
3195 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3200 vpt->type = TMPL_TYPE_ATTR;
3204 static bool pass2_callback(UNUSED void *ctx, fr_cond_t *c)
3206 value_pair_map_t *map;
3208 if (c->type == COND_TYPE_EXISTS) {
3209 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3210 return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3213 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3216 * The existence check might have been &Foo-Bar,
3217 * where Foo-Bar is defined by a module.
3219 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3220 if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3221 c->pass2_fixup = PASS2_FIXUP_NONE;
3227 * Maps have a paircompare fixup applied to them.
3228 * Others get ignored.
3230 if (c->pass2_fixup == PASS2_FIXUP_NONE) {
3231 if (c->type == COND_TYPE_MAP) {
3239 map = c->data.map; /* shorter */
3244 * Where "foo" is dynamically defined.
3246 if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3247 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3248 map->lhs->tmpl_da->vendor,
3250 cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3251 map->lhs->tmpl_da->name,
3257 * These guys can't have a paircompare fixup applied.
3259 c->pass2_fixup = PASS2_FIXUP_NONE;
3263 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3264 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3265 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3268 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3269 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3272 c->pass2_fixup = PASS2_FIXUP_NONE;
3277 * Just in case someone adds a new fixup later.
3279 rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3280 (c->pass2_fixup == PASS2_PAIRCOMPARE));
3285 if (map->lhs->type == TMPL_TYPE_XLAT) {
3287 * Don't compile the LHS to an attribute
3288 * reference for now. When we do that, we've got
3289 * to check the RHS for type-specific data, and
3290 * parse it to a TMPL_TYPE_DATA.
3292 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3297 if (map->rhs->type == TMPL_TYPE_XLAT) {
3299 * Convert the RHS to an attribute reference only
3300 * if the LHS is an attribute reference, AND is
3301 * of the same type as the RHS.
3303 * We can fix this when the code in evaluate.c
3304 * can handle strings on the LHS, and attributes
3305 * on the RHS. For now, the code in parser.c
3308 if (map->lhs->type == TMPL_TYPE_ATTR) {
3309 DICT_ATTR const *da = c->cast;
3311 if (!c->cast) da = map->lhs->tmpl_da;
3313 if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3318 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3325 * Convert bare refs to %{Foreach-Variable-N}
3327 if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3328 (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3331 value_pair_tmpl_t *vpt;
3333 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3334 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3335 T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST);
3337 char *spaces, *text;
3339 fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3341 cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3342 cf_log_err(map->ci, "%s", fmt);
3343 cf_log_err(map->ci, "%s^ %s", spaces, text);
3345 talloc_free(spaces);
3351 talloc_free(map->lhs);
3356 if (map->rhs->type == TMPL_TYPE_REGEX) {
3357 if (!pass2_regex_compile(map->ci, map->rhs)) {
3361 rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3365 * Only attributes can have a paircompare registered, and
3366 * they can only be with the current REQUEST, and only
3367 * with the request pairs.
3369 if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3370 (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3371 (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3375 if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3377 if (map->rhs->type == TMPL_TYPE_ATTR) {
3378 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3383 if (map->rhs->type == TMPL_TYPE_REGEX) {
3384 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3390 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3395 if (map->op != T_OP_CMP_EQ) {
3396 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3402 * Mark it as requiring a paircompare() call, instead of
3405 c->pass2_fixup = PASS2_PAIRCOMPARE;
3412 * Compile the RHS of update sections to xlat_exp_t
3414 static bool modcall_pass2_update(modgroup *g)
3416 value_pair_map_t *map;
3418 for (map = g->map; map != NULL; map = map->next) {
3419 if (map->rhs->type == TMPL_TYPE_XLAT) {
3420 rad_assert(map->rhs->tmpl_xlat == NULL);
3423 * FIXME: compile to attribute && handle
3424 * the conversion in map_to_vp().
3426 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3431 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3434 * Deal with undefined attributes now.
3436 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3437 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3440 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3441 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3450 * Do a second-stage pass on compiling the modules.
3452 bool modcall_pass2(modcallable *mc)
3459 for (c = mc; c != NULL; c = c->next) {
3467 g = mod_callabletogroup(c);
3468 if (g->done_pass2) return true;
3470 name2 = cf_section_name2(g->cs);
3472 c->debug_name = unlang_keyword[c->type];
3474 c->debug_name = talloc_asprintf(c, "update %s", name2);
3477 if (!modcall_pass2_update(g)) {
3480 g->done_pass2 = true;
3483 case MOD_XLAT: /* @todo: pre-parse xlat's */
3490 c->debug_name = c->name;
3491 break; /* do nothing */
3496 g = mod_callabletogroup(c);
3497 if (g->done_pass2) return true;
3499 name2 = cf_section_name2(g->cs);
3500 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3503 * The compilation code takes care of
3504 * simplifying 'true' and 'false'
3505 * conditions. For others, we have to do
3506 * a second pass to parse && compile
3509 if (!((g->cond->type == COND_TYPE_TRUE) ||
3510 (g->cond->type == COND_TYPE_FALSE))) {
3511 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3516 if (!modcall_pass2(g->children)) return false;
3517 g->done_pass2 = true;
3523 g = mod_callabletogroup(c);
3524 if (g->done_pass2) return true;
3526 name2 = cf_section_name2(g->cs);
3527 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3530 * We had &Foo-Bar, where Foo-Bar is
3531 * defined by a module.
3534 rad_assert(c->name != NULL);
3535 rad_assert(c->name[0] == '&');
3536 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3538 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3539 cf_section_name2_type(g->cs),
3540 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3542 char *spaces, *text;
3545 fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3547 cf_log_err_cs(g->cs, "Syntax error");
3548 cf_log_err_cs(g->cs, "%s", c->name);
3549 cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3551 talloc_free(spaces);
3561 * Statically compile xlats
3563 if (g->vpt->type == TMPL_TYPE_XLAT) {
3564 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3565 &g->vpt, true, NULL)) {
3573 * We may have: switch Foo-Bar {
3575 * where Foo-Bar is an attribute defined
3576 * by a module. Since there's no leading
3577 * &, it's parsed as a literal. But if
3578 * we can parse it as an attribute,
3579 * switch to using that.
3581 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3582 value_pair_tmpl_t *vpt;
3584 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3585 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3586 if (slen < 0) goto parse_error;
3587 if (vpt->type == TMPL_TYPE_ATTR) {
3588 talloc_free(g->vpt);
3596 * Warn about old-style configuration.
3598 * DEPRECATED: switch User-Name { ...
3599 * ALLOWED : switch &User-Name { ...
3601 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3602 (c->name[0] != '&')) {
3603 WARN("%s[%d]: Please change %s to &%s",
3604 cf_section_filename(g->cs),
3605 cf_section_lineno(g->cs),
3610 if (!modcall_pass2(g->children)) return false;
3611 g->done_pass2 = true;
3615 g = mod_callabletogroup(c);
3616 if (g->done_pass2) return true;
3618 name2 = cf_section_name2(g->cs);
3620 c->debug_name = unlang_keyword[c->type];
3622 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3625 rad_assert(c->parent != NULL);
3626 rad_assert(c->parent->type == MOD_SWITCH);
3629 * The statement may refer to an
3630 * attribute which doesn't exist until
3631 * all of the modules have been loaded.
3632 * Check for that now.
3634 if (!g->vpt && c->name &&
3635 (c->name[0] == '&') &&
3636 (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3637 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3638 cf_section_name2_type(g->cs),
3639 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3640 if (slen < 0) goto parse_error;
3644 * We have "case {...}". There's no
3645 * argument, so we don't need to check
3648 if (!g->vpt) goto do_children;
3651 * Do type-specific checks on the case statement
3653 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3656 f = mod_callabletogroup(mc->parent);
3657 rad_assert(f->vpt != NULL);
3660 * We're switching over an
3661 * attribute. Check that the
3664 if (f->vpt->type == TMPL_TYPE_ATTR) {
3665 rad_assert(f->vpt->tmpl_da != NULL);
3667 if (!tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da)) {
3668 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3678 * Compile and sanity check xlat
3681 if (g->vpt->type == TMPL_TYPE_XLAT) {
3684 f = mod_callabletogroup(mc->parent);
3685 rad_assert(f->vpt != NULL);
3688 * Don't expand xlat's into an
3689 * attribute of a different type.
3691 if (f->vpt->type == TMPL_TYPE_ATTR) {
3692 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3693 &g->vpt, true, f->vpt->tmpl_da)) {
3697 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3698 &g->vpt, true, NULL)) {
3704 if (!modcall_pass2(g->children)) return false;
3705 g->done_pass2 = true;
3709 g = mod_callabletogroup(c);
3710 if (g->done_pass2) return true;
3712 name2 = cf_section_name2(g->cs);
3713 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3716 * Already parsed, handle the children.
3718 if (g->vpt) goto check_children;
3721 * We had &Foo-Bar, where Foo-Bar is
3722 * defined by a module.
3724 rad_assert(c->name != NULL);
3725 rad_assert(c->name[0] == '&');
3726 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3729 * The statement may refer to an
3730 * attribute which doesn't exist until
3731 * all of the modules have been loaded.
3732 * Check for that now.
3734 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3735 REQUEST_CURRENT, PAIR_LIST_REQUEST);
3736 if (slen < 0) goto parse_error;
3739 rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST));
3740 if (g->vpt->tmpl_num != NUM_ALL) {
3741 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3744 if (!modcall_pass2(g->children)) return false;
3745 g->done_pass2 = true;
3749 c->debug_name = unlang_keyword[c->type];
3753 g = mod_callabletogroup(c);
3754 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], cf_section_name1(g->cs));
3759 case MOD_LOAD_BALANCE:
3760 case MOD_REDUNDANT_LOAD_BALANCE:
3761 c->debug_name = unlang_keyword[c->type];
3766 g = mod_callabletogroup(c);
3768 c->debug_name = mc->name; /* for authorize, etc. */
3770 } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3771 char const *name1 = cf_section_name1(g->cs);
3773 if (strcmp(name1, unlang_keyword[c->type]) != 0) {
3774 c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3778 if (g->done_pass2) return true;
3779 if (!modcall_pass2(g->children)) return false;
3780 g->done_pass2 = true;
3788 void modcall_debug(modcallable *mc, int depth)
3792 value_pair_map_t *map;
3795 for (this = mc; this != NULL; this = this->next) {
3796 switch (this->type) {
3801 modsingle *single = mod_callabletosingle(this);
3803 DEBUG("%.*s%s", depth, modcall_spaces,
3804 single->modinst->name);
3810 g = mod_callabletogroup(this);
3811 DEBUG("%.*s%s {", depth, modcall_spaces,
3812 unlang_keyword[this->type]);
3814 for (map = g->map; map != NULL; map = map->next) {
3815 map_prints(buffer, sizeof(buffer), map);
3816 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3819 DEBUG("%.*s}", depth, modcall_spaces);
3823 g = mod_callabletogroup(this);
3824 DEBUG("%.*s%s {", depth, modcall_spaces,
3825 unlang_keyword[this->type]);
3826 modcall_debug(g->children, depth + 1);
3827 DEBUG("%.*s}", depth, modcall_spaces);
3832 g = mod_callabletogroup(this);
3833 fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3834 DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3835 unlang_keyword[this->type], buffer);
3836 modcall_debug(g->children, depth + 1);
3837 DEBUG("%.*s}", depth, modcall_spaces);
3842 g = mod_callabletogroup(this);
3843 tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3844 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3845 unlang_keyword[this->type], buffer);
3846 modcall_debug(g->children, depth + 1);
3847 DEBUG("%.*s}", depth, modcall_spaces);
3852 g = mod_callabletogroup(this);
3853 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3854 unlang_keyword[this->type], this->name);
3855 modcall_debug(g->children, depth + 1);
3856 DEBUG("%.*s}", depth, modcall_spaces);
3860 DEBUG("%.*sbreak", depth, modcall_spaces);
3865 g = mod_callabletogroup(this);
3866 DEBUG("%.*s%s {", depth, modcall_spaces,
3867 unlang_keyword[this->type]);
3868 modcall_debug(g->children, depth + 1);
3869 DEBUG("%.*s}", depth, modcall_spaces);
3873 case MOD_LOAD_BALANCE:
3874 case MOD_REDUNDANT_LOAD_BALANCE:
3875 g = mod_callabletogroup(this);
3876 DEBUG("%.*s%s {", depth, modcall_spaces,
3877 unlang_keyword[this->type]);
3878 modcall_debug(g->children, depth + 1);
3879 DEBUG("%.*s}", depth, modcall_spaces);