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 */
72 } grouptype; /* after mc */
73 modcallable *children;
74 modcallable *tail; /* of the children list */
76 value_pair_map_t *map; /* update */
77 vp_tmpl_t *vpt; /* switch */
78 fr_cond_t *cond; /* if/elsif */
84 module_instance_t *modinst;
99 /* Simple conversions: modsingle and modgroup are subclasses of modcallable,
100 * so we often want to go back and forth between them. */
101 static modsingle *mod_callabletosingle(modcallable *p)
103 rad_assert(p->type==MOD_SINGLE);
104 return (modsingle *)p;
106 static modgroup *mod_callabletogroup(modcallable *p)
108 rad_assert((p->type > MOD_SINGLE) && (p->type <= MOD_POLICY));
110 return (modgroup *)p;
112 static modcallable *mod_singletocallable(modsingle *p)
114 return (modcallable *)p;
116 static modcallable *mod_grouptocallable(modgroup *p)
118 return (modcallable *)p;
121 static modref *mod_callabletoref(modcallable *p)
123 rad_assert(p->type==MOD_REFERENCE);
126 static modcallable *mod_reftocallable(modref *p)
128 return (modcallable *)p;
131 static modxlat *mod_callabletoxlat(modcallable *p)
133 rad_assert(p->type==MOD_XLAT);
136 static modcallable *mod_xlattocallable(modxlat *p)
138 return (modcallable *)p;
141 /* modgroups are grown by adding a modcallable to the end */
142 static void add_child(modgroup *g, modcallable *c)
146 (void) talloc_steal(g, c);
149 g->children = g->tail = c;
151 rad_assert(g->tail->next == NULL);
156 c->parent = mod_grouptocallable(g);
159 /* Here's where we recognize all of our keywords: first the rcodes, then the
161 const FR_NAME_NUMBER mod_rcode_table[] = {
162 { "reject", RLM_MODULE_REJECT },
163 { "fail", RLM_MODULE_FAIL },
164 { "ok", RLM_MODULE_OK },
165 { "handled", RLM_MODULE_HANDLED },
166 { "invalid", RLM_MODULE_INVALID },
167 { "userlock", RLM_MODULE_USERLOCK },
168 { "notfound", RLM_MODULE_NOTFOUND },
169 { "noop", RLM_MODULE_NOOP },
170 { "updated", RLM_MODULE_UPDATED },
176 * Compile action && rcode for later use.
178 static int compile_action(modcallable *c, CONF_PAIR *cp)
181 char const *attr, *value;
183 attr = cf_pair_attr(cp);
184 value = cf_pair_value(cp);
185 if (!value) return 0;
187 if (!strcasecmp(value, "return"))
188 action = MOD_ACTION_RETURN;
190 else if (!strcasecmp(value, "break"))
191 action = MOD_ACTION_RETURN;
193 else if (!strcasecmp(value, "reject"))
194 action = MOD_ACTION_REJECT;
196 else if (strspn(value, "0123456789")==strlen(value)) {
197 action = atoi(value);
200 * Don't allow priority zero, for future use.
202 if (action == 0) return 0;
204 cf_log_err_cp(cp, "Unknown action '%s'.\n",
209 if (strcasecmp(attr, "default") != 0) {
212 rcode = fr_str2int(mod_rcode_table, attr, -1);
215 "Unknown module rcode '%s'.\n",
219 c->actions[rcode] = action;
221 } else { /* set all unset values to the default */
224 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
225 if (!c->actions[i]) c->actions[i] = action;
232 /* Some short names for debugging output */
233 static char const * const comp2str[] = {
249 #ifdef HAVE_PTHREAD_H
251 * Lock the mutex for the module
253 static void safe_lock(module_instance_t *instance)
256 pthread_mutex_lock(instance->mutex);
260 * Unlock the mutex for the module
262 static void safe_unlock(module_instance_t *instance)
265 pthread_mutex_unlock(instance->mutex);
269 * No threads: these functions become NULL's.
271 #define safe_lock(foo)
272 #define safe_unlock(foo)
275 static rlm_rcode_t CC_HINT(nonnull) call_modsingle(rlm_components_t component, modsingle *sp, REQUEST *request)
278 int indent = request->log.indent;
281 * If the request should stop, refuse to do anything.
283 blocked = (request->master_state == REQUEST_STOP_PROCESSING);
284 if (blocked) return RLM_MODULE_NOOP;
286 RDEBUG3("modsingle[%s]: calling %s (%s) for request %d",
287 comp2str[component], sp->modinst->name,
288 sp->modinst->entry->name, request->number);
289 request->log.indent = 0;
291 if (sp->modinst->force) {
292 request->rcode = sp->modinst->code;
297 * For logging unresponsive children.
299 request->module = sp->modinst->name;
301 safe_lock(sp->modinst);
302 request->rcode = sp->modinst->entry->module->methods[component](sp->modinst->insthandle, request);
303 safe_unlock(sp->modinst);
305 request->module = "";
308 * Wasn't blocked, and now is. Complain!
310 blocked = (request->master_state == REQUEST_STOP_PROCESSING);
312 RWARN("Module %s became unblocked for request %u", sp->modinst->entry->name, request->number);
316 request->log.indent = indent;
317 RDEBUG3("modsingle[%s]: returned from %s (%s) for request %d",
318 comp2str[component], sp->modinst->name,
319 sp->modinst->entry->name, request->number);
321 return request->rcode;
324 static int default_component_results[RLM_COMPONENT_COUNT] = {
325 RLM_MODULE_REJECT, /* AUTH */
326 RLM_MODULE_NOTFOUND, /* AUTZ */
327 RLM_MODULE_NOOP, /* PREACCT */
328 RLM_MODULE_NOOP, /* ACCT */
329 RLM_MODULE_FAIL, /* SESS */
330 RLM_MODULE_NOOP, /* PRE_PROXY */
331 RLM_MODULE_NOOP, /* POST_PROXY */
332 RLM_MODULE_NOOP /* POST_AUTH */
335 RLM_MODULE_NOOP, /* RECV_COA_TYPE */
336 RLM_MODULE_NOOP /* SEND_COA_TYPE */
341 extern char const *unlang_keyword[];
343 char const *unlang_keyword[] = {
347 "load-balance group",
348 "redundant-load-balance group",
366 static char const modcall_spaces[] = " ";
368 #define MODCALL_STACK_MAX (32)
371 * Don't call the modules recursively. Instead, do them
372 * iteratively, and manage the call stack ourselves.
374 typedef struct modcall_stack_entry_t {
377 int unwind; /* unwind to this one if it exists */
379 } modcall_stack_entry_t;
382 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
383 modcall_stack_entry_t *entry, bool do_next_sibling);
386 * Call a child of a block.
388 static void modcall_child(REQUEST *request, rlm_components_t component, int depth,
389 modcall_stack_entry_t *entry, modcallable *c,
390 rlm_rcode_t *result, bool do_next_sibling)
392 modcall_stack_entry_t *next;
394 if (depth >= MODCALL_STACK_MAX) {
395 ERROR("Internal sanity check failed: module stack is too deep");
400 * Initialize the childs stack frame.
404 next->result = entry->result;
408 if (!modcall_recurse(request, component,
409 depth, next, do_next_sibling)) {
410 *result = RLM_MODULE_FAIL;
415 * Unwind back up the stack
417 if (next->unwind != 0) {
418 entry->unwind = next->unwind;
421 *result = next->result;
428 * Interpret the various types of blocks.
430 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
431 modcall_stack_entry_t *entry, bool do_next_sibling)
433 bool if_taken, was_if;
438 was_if = if_taken = false;
439 result = RLM_MODULE_UNKNOWN;
447 * Nothing more to do. Return the code and priority
448 * which was set by the caller.
452 rad_assert(c->debug_name != NULL); /* if this happens, all bets are off. */
455 * We've been asked to stop. Do so.
457 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
459 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
460 entry->result = RLM_MODULE_FAIL;
461 entry->priority = 9999;
467 * Handle "if" conditions.
469 if (c->type == MOD_IF) {
474 g = mod_callabletogroup(c);
475 rad_assert(g->cond != NULL);
477 RDEBUG2("%s %s{", unlang_keyword[c->type], c->name);
479 condition = radius_evaluate_cond(request, result, 0, g->cond);
482 REDEBUG("Failed retrieving values required to evaluate condition");
484 RDEBUG2("%s %s -> %s",
485 unlang_keyword[c->type],
486 c->name, condition ? "TRUE" : "FALSE");
490 * Didn't pass. Remember that.
499 * We took the "if". Go recurse into its' children.
507 * "else" if the previous "if" was taken.
508 * "if" if the previous if wasn't taken.
510 if (c->type == MOD_ELSIF) {
511 if (!was_if) goto elsif_error;
514 * Like MOD_ELSE, but allow for a later "else"
517 RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
518 unlang_keyword[c->type], request->number);
525 * Check the "if" condition.
531 * "else" for a preceding "if".
533 if (c->type == MOD_ELSE) {
534 if (!was_if) { /* error */
536 RDEBUG2("... skipping %s for request %d: No preceding \"if\"",
537 unlang_keyword[c->type], request->number);
542 RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
543 unlang_keyword[c->type], request->number);
550 * We need to process it. Go do that.
558 * We're no longer processing if/else/elsif. Reset the
559 * trackers for those conditions.
563 #endif /* WITH_UNLANG */
565 if (c->type == MOD_SINGLE) {
569 * Process a stand-alone child, and fall through
570 * to dealing with it's parent.
572 sp = mod_callabletosingle(c);
574 result = call_modsingle(c->method, sp, request);
575 RDEBUG2("[%s] = %s", c->name ? c->name : "",
576 fr_int2str(mod_rcode_table, result, "<invalid>"));
577 goto calculate_result;
582 * Update attribute(s)
584 if (c->type == MOD_UPDATE) {
586 modgroup *g = mod_callabletogroup(c);
587 value_pair_map_t *map;
591 for (map = g->map; map != NULL; map = map->next) {
592 rcode = map_to_request(request, map, map_to_vp, NULL);
594 result = (rcode == -2) ? RLM_MODULE_INVALID : RLM_MODULE_FAIL;
597 goto calculate_result;
601 result = RLM_MODULE_NOOP;
603 goto calculate_result;
607 * Loop over a set of attributes.
609 if (c->type == MOD_FOREACH) {
610 int i, foreach_depth = -1;
611 VALUE_PAIR *vps, *vp;
612 modcall_stack_entry_t *next = NULL;
614 modgroup *g = mod_callabletogroup(c);
616 if (depth >= MODCALL_STACK_MAX) {
617 ERROR("Internal sanity check failed: module stack is too deep");
622 * Figure out how deep we are in nesting by looking at request_data
625 for (i = 0; i < 8; i++) {
626 if (!request_data_reference(request, (void *)radius_get_vp, i)) {
632 if (foreach_depth < 0) {
633 REDEBUG("foreach Nesting too deep!");
634 result = RLM_MODULE_FAIL;
635 goto calculate_result;
639 * Copy the VPs from the original request, this ensures deterministic
640 * behaviour if someone decides to add or remove VPs in the set were
643 if (tmpl_copy_vps(request, &vps, request, g->vpt) < 0) { /* nothing to loop over */
645 result = RLM_MODULE_NOOP;
647 goto calculate_result;
650 rad_assert(vps != NULL);
651 fr_cursor_init(©, &vps);
653 RDEBUG2("foreach %s ", c->name);
656 * This is the actual body of the foreach loop
658 for (vp = fr_cursor_first(©);
660 vp = fr_cursor_next(©)) {
662 if (fr_debug_lvl >= 2) {
665 vp_prints_value(buffer, sizeof(buffer), vp, '"');
666 RDEBUG2("# Foreach-Variable-%d = %s", foreach_depth, buffer);
671 * Add the vp to the request, so that
672 * xlat.c, xlat_foreach() can find it.
674 request_data_add(request, (void *)radius_get_vp, foreach_depth, &vp, false);
677 * Initialize the childs stack frame.
680 next->c = g->children;
681 next->result = entry->result;
685 if (!modcall_recurse(request, component, depth + 1, next, true)) {
690 * We've unwound to the enclosing
691 * "foreach". Stop the unwinding.
693 if (next->unwind == MOD_FOREACH) {
698 * Unwind all the way.
700 if (next->unwind == MOD_RETURN) {
701 entry->unwind = MOD_RETURN;
704 } /* loop over VPs */
707 * Free the copied vps and the request data
708 * If we don't remove the request data, something could call
709 * the xlat outside of a foreach loop and trigger a segv.
712 request_data_get(request, (void *)radius_get_vp, foreach_depth);
714 rad_assert(next != NULL);
715 result = next->result;
716 priority = next->priority;
718 goto calculate_result;
722 * Break out of a "foreach" loop, or return from a nested
725 if ((c->type == MOD_BREAK) || (c->type == MOD_RETURN)) {
729 RDEBUG2("%s", unlang_keyword[c->type]);
731 for (i = 8; i >= 0; i--) {
732 copy_p = request_data_get(request, (void *)radius_get_vp, i);
734 if (c->type == MOD_BREAK) {
735 RDEBUG2("# break Foreach-Variable-%d", i);
742 * Leave result / priority on the stack, and stop processing the section.
744 entry->unwind = c->type;
748 #endif /* WITH_UNLANG */
751 * Child is a group that has children of it's own.
753 if ((c->type == MOD_GROUP) || (c->type == MOD_POLICY)
755 || (c->type == MOD_CASE)
763 g = mod_callabletogroup(c);
766 * This should really have been caught in the
767 * compiler, and the node never generated. But
768 * doing that requires changing it's API so that
769 * it returns a flag instead of the compiled
773 RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
778 modcall_child(request, component,
779 depth + 1, entry, g->children,
782 goto calculate_result;
786 if (c->type == MOD_SWITCH) {
787 modcallable *this, *found, *null_case;
791 value_pair_map_t map;
796 g = mod_callabletogroup(c);
798 memset(&cond, 0, sizeof(cond));
799 memset(&map, 0, sizeof(map));
801 cond.type = COND_TYPE_MAP;
802 cond.data.map = ↦
804 map.op = T_OP_CMP_EQ;
805 map.ci = cf_section_to_item(g->cs);
807 rad_assert(g->vpt != NULL);
809 null_case = found = NULL;
813 * The attribute doesn't exist. We can skip
814 * directly to the default 'case' statement.
816 if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
818 for (this = g->children; this; this = this->next) {
819 rad_assert(this->type == MOD_CASE);
821 h = mod_callabletogroup(this);
822 if (h->vpt) continue;
832 * Expand the template if necessary, so that it
833 * is evaluated once instead of for each 'case'
836 if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
837 (g->vpt->type == TMPL_TYPE_XLAT) ||
838 (g->vpt->type == TMPL_TYPE_EXEC)) {
842 len = tmpl_aexpand(request, &p, request, g->vpt, NULL, NULL);
843 if (len < 0) goto find_null_case;
845 tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, len);
849 * Find either the exact matching name, or the
850 * "case {...}" statement.
852 for (this = g->children; this; this = this->next) {
853 rad_assert(this->type == MOD_CASE);
855 h = mod_callabletogroup(this);
858 * Remember the default case
861 if (!null_case) null_case = this;
866 * If we're switching over an attribute
867 * AND we haven't pre-parsed the data for
868 * the case statement, then cast the data
869 * to the type of the attribute.
871 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
872 (h->vpt->type != TMPL_TYPE_DATA)) {
875 cond.cast = g->vpt->tmpl_da;
878 * Remove unnecessary casting.
880 if ((h->vpt->type == TMPL_TYPE_ATTR) &&
881 (g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
886 * Use the pre-expanded string.
888 } else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
889 (g->vpt->type == TMPL_TYPE_XLAT) ||
890 (g->vpt->type == TMPL_TYPE_EXEC)) {
896 * Else evaluate the 'switch' statement.
904 if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
911 if (!found) found = null_case;
914 talloc_free(data.ptr);
915 modcall_child(request, component, depth + 1, entry, found, &result, true);
917 goto calculate_result;
921 if ((c->type == MOD_LOAD_BALANCE) ||
922 (c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
924 modcallable *this, *found;
929 g = mod_callabletogroup(c);
931 rad_assert(g->children != NULL);
934 * Choose a child at random.
936 for (this = g->children; this; this = this->next) {
939 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
944 if (c->type == MOD_LOAD_BALANCE) {
945 modcall_child(request, component,
946 depth + 1, entry, found,
953 modcall_child(request, component,
954 depth + 1, entry, this,
956 if (this->actions[result] == MOD_ACTION_RETURN) {
962 if (!this) this = g->children;
963 } while (this != found);
966 goto calculate_result;
967 } /* MOD_LOAD_BALANCE */
970 * Reference another virtual server.
972 * This should really be deleted, and replaced with a
973 * more abstracted / functional version.
975 if (c->type == MOD_REFERENCE) {
976 modref *mr = mod_callabletoref(c);
977 char const *server = request->server;
979 if (server == mr->ref_name) {
980 RWDEBUG("Suppressing recursive call to server %s", server);
984 request->server = mr->ref_name;
985 RDEBUG("server %s { # nested call", mr->ref_name);
986 result = indexed_modcall(component, 0, request);
987 RDEBUG("} # server %s with nested call", mr->ref_name);
988 request->server = server;
989 goto calculate_result;
990 } /* MOD_REFERENCE */
993 * xlat a string without doing anything else
995 * This should really be deleted, and replaced with a
996 * more abstracted / functional version.
998 if (c->type == MOD_XLAT) {
999 modxlat *mx = mod_callabletoxlat(c);
1003 radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
1005 RDEBUG("`%s`", mx->xlat_name);
1006 radius_exec_program(NULL, 0, NULL, request, mx->xlat_name, request->packet->vps,
1007 false, true, EXEC_TIMEOUT);
1014 * Add new module types here.
1019 RDEBUG("(%s, %d) ? (%s, %d)",
1020 fr_int2str(mod_rcode_table, result, "<invalid>"),
1022 fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
1027 rad_assert(result != RLM_MODULE_UNKNOWN);
1030 * The child's action says return. Do so.
1032 if ((c->actions[result] == MOD_ACTION_RETURN) &&
1034 entry->result = result;
1039 * If "reject", break out of the loop and return
1042 if (c->actions[result] == MOD_ACTION_REJECT) {
1043 entry->result = RLM_MODULE_REJECT;
1048 * The array holds a default priority for this return
1049 * code. Grab it in preference to any unset priority.
1052 priority = c->actions[result];
1056 * We're higher than any previous priority, remember this
1057 * return code and priority.
1059 if (priority > entry->priority) {
1060 entry->result = result;
1061 entry->priority = priority;
1066 * If we're processing a "case" statement, we return once
1067 * it's done, rather than going to the next "case" statement.
1069 if (c->type == MOD_CASE) goto finish;
1073 * If we've been told to stop processing
1076 if (entry->unwind == MOD_BREAK) {
1077 RDEBUG2("# unwind to enclosing foreach");
1082 if (entry->unwind == MOD_RETURN) {
1087 if (do_next_sibling) {
1088 entry->c = entry->c->next;
1090 if (entry->c) goto redo;
1102 /** Call a module, iteratively, with a local stack, rather than recursively
1104 * What did Paul Graham say about Lisp...?
1106 int modcall(rlm_components_t component, modcallable *c, REQUEST *request)
1108 modcall_stack_entry_t stack[MODCALL_STACK_MAX];
1111 memset(stack, 0, sizeof(stack));
1114 * Set up the initial stack frame.
1117 stack[0].result = default_component_results[component];
1118 stack[0].priority = 0;
1119 stack[0].unwind = 0;
1122 * Call the main handler.
1124 if (!modcall_recurse(request, component, 0, &stack[0], true)) {
1125 return RLM_MODULE_FAIL;
1129 * Return the result.
1131 return stack[0].result;
1136 static char const *action2str(int action)
1138 static char buf[32];
1139 if(action==MOD_ACTION_RETURN)
1141 if(action==MOD_ACTION_REJECT)
1143 snprintf(buf, sizeof buf, "%d", action);
1147 /* If you suspect a bug in the parser, you'll want to use these dump
1148 * functions. dump_tree should reproduce a whole tree exactly as it was found
1149 * in radiusd.conf, but in long form (all actions explicitly defined) */
1150 static void dump_mc(modcallable *c, int indent)
1154 if(c->type==MOD_SINGLE) {
1155 modsingle *single = mod_callabletosingle(c);
1156 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1157 single->modinst->name);
1158 } else if ((c->type > MOD_SINGLE) && (c->type <= MOD_POLICY)) {
1159 modgroup *g = mod_callabletogroup(c);
1161 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1162 unlang_keyword[c->type]);
1163 for(p = g->children;p;p = p->next)
1164 dump_mc(p, indent+1);
1165 } /* else ignore it for now */
1167 for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
1168 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
1169 fr_int2str(mod_rcode_table, i, "<invalid>"),
1170 action2str(c->actions[i]));
1173 DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
1176 static void dump_tree(rlm_components_t comp, modcallable *c)
1178 DEBUG("[%s]", comp2str[comp]);
1182 #define dump_tree(a, b)
1185 /* These are the default actions. For each component, the group{} block
1186 * behaves like the code from the old module_*() function. redundant{}
1187 * are based on my guesses of what they will be used for. --Pac. */
1189 defaultactions[RLM_COMPONENT_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1195 MOD_ACTION_RETURN, /* reject */
1197 MOD_ACTION_RETURN, /* ok */
1198 MOD_ACTION_RETURN, /* handled */
1200 MOD_ACTION_RETURN, /* userlock */
1201 MOD_ACTION_RETURN, /* notfound */
1207 MOD_ACTION_RETURN, /* reject */
1209 MOD_ACTION_RETURN, /* ok */
1210 MOD_ACTION_RETURN, /* handled */
1211 MOD_ACTION_RETURN, /* invalid */
1212 MOD_ACTION_RETURN, /* userlock */
1213 MOD_ACTION_RETURN, /* notfound */
1214 MOD_ACTION_RETURN, /* noop */
1215 MOD_ACTION_RETURN /* updated */
1222 MOD_ACTION_RETURN, /* reject */
1223 MOD_ACTION_RETURN, /* fail */
1225 MOD_ACTION_RETURN, /* handled */
1226 MOD_ACTION_RETURN, /* invalid */
1227 MOD_ACTION_RETURN, /* userlock */
1234 MOD_ACTION_RETURN, /* reject */
1236 MOD_ACTION_RETURN, /* ok */
1237 MOD_ACTION_RETURN, /* handled */
1238 MOD_ACTION_RETURN, /* invalid */
1239 MOD_ACTION_RETURN, /* userlock */
1240 MOD_ACTION_RETURN, /* notfound */
1241 MOD_ACTION_RETURN, /* noop */
1242 MOD_ACTION_RETURN /* updated */
1249 MOD_ACTION_RETURN, /* reject */
1250 MOD_ACTION_RETURN, /* fail */
1252 MOD_ACTION_RETURN, /* handled */
1253 MOD_ACTION_RETURN, /* invalid */
1254 MOD_ACTION_RETURN, /* userlock */
1255 MOD_ACTION_RETURN, /* notfound */
1261 MOD_ACTION_RETURN, /* reject */
1263 MOD_ACTION_RETURN, /* ok */
1264 MOD_ACTION_RETURN, /* handled */
1265 MOD_ACTION_RETURN, /* invalid */
1266 MOD_ACTION_RETURN, /* userlock */
1267 MOD_ACTION_RETURN, /* notfound */
1268 MOD_ACTION_RETURN, /* noop */
1269 MOD_ACTION_RETURN /* updated */
1276 MOD_ACTION_RETURN, /* reject */
1277 MOD_ACTION_RETURN, /* fail */
1279 MOD_ACTION_RETURN, /* handled */
1280 MOD_ACTION_RETURN, /* invalid */
1281 MOD_ACTION_RETURN, /* userlock */
1282 MOD_ACTION_RETURN, /* notfound */
1290 MOD_ACTION_RETURN, /* ok */
1291 MOD_ACTION_RETURN, /* handled */
1303 MOD_ACTION_RETURN, /* reject */
1305 MOD_ACTION_RETURN, /* ok */
1306 MOD_ACTION_RETURN, /* handled */
1307 MOD_ACTION_RETURN, /* invalid */
1308 MOD_ACTION_RETURN, /* userlock */
1309 MOD_ACTION_RETURN, /* notfound */
1310 MOD_ACTION_RETURN, /* noop */
1311 MOD_ACTION_RETURN /* updated */
1315 MOD_ACTION_RETURN, /* reject */
1317 MOD_ACTION_RETURN, /* ok */
1318 MOD_ACTION_RETURN, /* handled */
1319 MOD_ACTION_RETURN, /* invalid */
1320 MOD_ACTION_RETURN, /* userlock */
1321 MOD_ACTION_RETURN, /* notfound */
1322 MOD_ACTION_RETURN, /* noop */
1323 MOD_ACTION_RETURN /* updated */
1330 MOD_ACTION_RETURN, /* reject */
1331 MOD_ACTION_RETURN, /* fail */
1333 MOD_ACTION_RETURN, /* handled */
1334 MOD_ACTION_RETURN, /* invalid */
1335 MOD_ACTION_RETURN, /* userlock */
1342 MOD_ACTION_RETURN, /* reject */
1344 MOD_ACTION_RETURN, /* ok */
1345 MOD_ACTION_RETURN, /* handled */
1346 MOD_ACTION_RETURN, /* invalid */
1347 MOD_ACTION_RETURN, /* userlock */
1348 MOD_ACTION_RETURN, /* notfound */
1349 MOD_ACTION_RETURN, /* noop */
1350 MOD_ACTION_RETURN /* updated */
1357 MOD_ACTION_RETURN, /* reject */
1358 MOD_ACTION_RETURN, /* fail */
1360 MOD_ACTION_RETURN, /* handled */
1361 MOD_ACTION_RETURN, /* invalid */
1362 MOD_ACTION_RETURN, /* userlock */
1369 MOD_ACTION_RETURN, /* reject */
1371 MOD_ACTION_RETURN, /* ok */
1372 MOD_ACTION_RETURN, /* handled */
1373 MOD_ACTION_RETURN, /* invalid */
1374 MOD_ACTION_RETURN, /* userlock */
1375 MOD_ACTION_RETURN, /* notfound */
1376 MOD_ACTION_RETURN, /* noop */
1377 MOD_ACTION_RETURN /* updated */
1384 MOD_ACTION_RETURN, /* reject */
1385 MOD_ACTION_RETURN, /* fail */
1387 MOD_ACTION_RETURN, /* handled */
1388 MOD_ACTION_RETURN, /* invalid */
1389 MOD_ACTION_RETURN, /* userlock */
1396 MOD_ACTION_RETURN, /* reject */
1398 MOD_ACTION_RETURN, /* ok */
1399 MOD_ACTION_RETURN, /* handled */
1400 MOD_ACTION_RETURN, /* invalid */
1401 MOD_ACTION_RETURN, /* userlock */
1402 MOD_ACTION_RETURN, /* notfound */
1403 MOD_ACTION_RETURN, /* noop */
1404 MOD_ACTION_RETURN /* updated */
1413 MOD_ACTION_RETURN, /* reject */
1414 MOD_ACTION_RETURN, /* fail */
1416 MOD_ACTION_RETURN, /* handled */
1417 MOD_ACTION_RETURN, /* invalid */
1418 MOD_ACTION_RETURN, /* userlock */
1425 MOD_ACTION_RETURN, /* reject */
1427 MOD_ACTION_RETURN, /* ok */
1428 MOD_ACTION_RETURN, /* handled */
1429 MOD_ACTION_RETURN, /* invalid */
1430 MOD_ACTION_RETURN, /* userlock */
1431 MOD_ACTION_RETURN, /* notfound */
1432 MOD_ACTION_RETURN, /* noop */
1433 MOD_ACTION_RETURN /* updated */
1440 MOD_ACTION_RETURN, /* reject */
1441 MOD_ACTION_RETURN, /* fail */
1443 MOD_ACTION_RETURN, /* handled */
1444 MOD_ACTION_RETURN, /* invalid */
1445 MOD_ACTION_RETURN, /* userlock */
1452 MOD_ACTION_RETURN, /* reject */
1454 MOD_ACTION_RETURN, /* ok */
1455 MOD_ACTION_RETURN, /* handled */
1456 MOD_ACTION_RETURN, /* invalid */
1457 MOD_ACTION_RETURN, /* userlock */
1458 MOD_ACTION_RETURN, /* notfound */
1459 MOD_ACTION_RETURN, /* noop */
1460 MOD_ACTION_RETURN /* updated */
1466 static const int authtype_actions[GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
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 /** Validate and fixup a map that's part of an update section.
1496 * @param map to validate.
1497 * @param ctx data to pass to fixup function (currently unused).
1498 * @return 0 if valid else -1.
1500 int modcall_fixup_update(value_pair_map_t *map, UNUSED void *ctx)
1502 CONF_PAIR *cp = cf_item_to_pair(map->ci);
1505 * Anal-retentive checks.
1507 if (DEBUG_ENABLED3) {
1508 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->lhs->name[0] != '&')) {
1509 WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
1510 cf_pair_filename(cp), cf_pair_lineno(cp),
1511 map->lhs->name, fr_int2str(fr_tokens, map->op, "<INVALID>"));
1514 if ((map->rhs->type == TMPL_TYPE_ATTR) && (map->rhs->name[0] != '&')) {
1515 WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
1516 cf_pair_filename(cp), cf_pair_lineno(cp),
1517 fr_int2str(fr_tokens, map->op, "<INVALID>"), map->rhs->name);
1522 * Values used by unary operators should be literal ANY
1524 * We then free the template and alloc a NULL one instead.
1526 if (map->op == T_OP_CMP_FALSE) {
1527 if ((map->rhs->type != TMPL_TYPE_LITERAL) || (strcmp(map->rhs->name, "ANY") != 0)) {
1528 WARN("%s[%d] Wildcard deletion MUST use '!* ANY'",
1529 cf_pair_filename(cp), cf_pair_lineno(cp));
1532 TALLOC_FREE(map->rhs);
1534 map->rhs = tmpl_alloc(map, TMPL_TYPE_NULL, NULL, 0);
1538 * Lots of sanity checks for insane people...
1542 * What exactly where you expecting to happen here?
1544 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
1545 (map->rhs->type == TMPL_TYPE_LIST)) {
1546 cf_log_err(map->ci, "Can't copy list into an attribute");
1551 * Depending on the attribute type, some operators are disallowed.
1553 if (map->lhs->type == TMPL_TYPE_ATTR) {
1556 cf_log_err(map->ci, "Invalid operator for attribute");
1565 case T_OP_CMP_FALSE:
1571 if (map->lhs->type == TMPL_TYPE_LIST) {
1573 * Can't copy an xlat expansion or literal into a list,
1574 * we don't know what type of attribute we'd need
1577 * The only exception is where were using a unary
1580 if (map->op != T_OP_CMP_FALSE) switch (map->rhs->type) {
1581 case TMPL_TYPE_XLAT:
1582 case TMPL_TYPE_LITERAL:
1583 cf_log_err(map->ci, "Can't copy value into list (we don't know which attribute to create)");
1591 * Only += and :=, and !* operators are supported
1595 case T_OP_CMP_FALSE:
1599 if ((map->rhs->type != TMPL_TYPE_LIST) &&
1600 (map->rhs->type != TMPL_TYPE_EXEC)) {
1601 cf_log_err(map->ci, "Invalid source for list assignment '%s += ...'", map->lhs->name);
1607 if (map->rhs->type == TMPL_TYPE_EXEC) {
1608 WARN("%s[%d] Please change ':=' to '=' for list assignment",
1609 cf_pair_filename(cp), cf_pair_lineno(cp));
1612 if (map->rhs->type != TMPL_TYPE_LIST) {
1613 cf_log_err(map->ci, "Invalid source for list assignment '%s := ...'", map->lhs->name);
1619 if (map->rhs->type != TMPL_TYPE_EXEC) {
1620 cf_log_err(map->ci, "Invalid source for list assignment '%s = ...'", map->lhs->name);
1626 cf_log_err(map->ci, "Operator \"%s\" not allowed for list assignment",
1627 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1633 * If the map has a unary operator there's no further
1634 * processing we need to, as RHS is unused.
1636 if (map->op == T_OP_CMP_FALSE) return 0;
1639 * If LHS is an attribute, and RHS is a literal, we can
1640 * preparse the information into a TMPL_TYPE_DATA.
1642 * Unless it's a unary operator in which case we
1645 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->rhs->type == TMPL_TYPE_LITERAL)) {
1647 * It's a literal string, just copy it.
1648 * Don't escape anything.
1650 if (!cf_new_escape &&
1651 (map->lhs->tmpl_da->type == PW_TYPE_STRING) &&
1652 (cf_pair_value_type(cp) == T_SINGLE_QUOTED_STRING)) {
1653 tmpl_cast_in_place_str(map->rhs);
1659 if (map->lhs->auto_converted &&
1660 (map->rhs->name[0] == '0') && (map->rhs->name[1] == 'x') &&
1661 (map->rhs->len > 2) && ((map->rhs->len & 0x01) == 0)) {
1662 vp_tmpl_t *vpt = map->rhs;
1665 if (!map_cast_from_hex(map, T_BARE_WORD, vpt->name)) {
1667 cf_log_err(map->ci, "%s", fr_strerror());
1672 } else if (tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da) < 0) {
1673 cf_log_err(map->ci, "%s", fr_strerror());
1678 * Fixup LHS da if it doesn't match the type
1681 if (map->lhs->tmpl_da->type != map->rhs->tmpl_data_type) {
1682 DICT_ATTR const *da;
1684 da = dict_attrbytype(map->lhs->tmpl_da->attr, map->lhs->tmpl_da->vendor,
1685 map->rhs->tmpl_data_type);
1687 fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1688 fr_int2str(dict_attr_types, map->rhs->tmpl_data_type,
1689 "<INVALID>"), map->lhs->tmpl_da->name);
1692 map->lhs->tmpl_da = da;
1695 } /* else we can't precompile the data */
1702 static modcallable *do_compile_modupdate(modcallable *parent, rlm_components_t component,
1703 CONF_SECTION *cs, char const *name2)
1707 modcallable *csingle;
1709 value_pair_map_t *head;
1712 * This looks at cs->name2 to determine which list to update
1714 rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
1715 if (rcode < 0) return NULL; /* message already printed */
1717 cf_log_err_cs(cs, "'update' sections cannot be empty");
1721 g = talloc_zero(parent, modgroup);
1722 csingle = mod_grouptocallable(g);
1724 csingle->parent = parent;
1725 csingle->next = NULL;
1728 csingle->name = name2;
1732 csingle->type = MOD_UPDATE;
1733 csingle->method = component;
1735 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
1736 sizeof(csingle->actions));
1738 g->grouptype = GROUPTYPE_SIMPLE;
1741 g->map = talloc_steal(g, head);
1747 static modcallable *do_compile_modswitch (modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1752 bool had_seen_default = false;
1753 modcallable *csingle;
1758 name2 = cf_section_name2(cs);
1760 cf_log_err_cs(cs, "You must specify a variable to switch over for 'switch'");
1764 if (!cf_item_find_next(cs, NULL)) {
1765 cf_log_err_cs(cs, "'switch' statements cannot be empty");
1770 * Create the template. If we fail, AND it's a bare word
1771 * with &Foo-Bar, it MAY be an attribute defined by a
1772 * module. Allow it for now. The pass2 checks below
1775 type = cf_section_name2_type(cs);
1776 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1777 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1778 char *spaces, *text;
1780 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1782 cf_log_err_cs(cs, "Syntax error");
1783 cf_log_err_cs(cs, "%s", name2);
1784 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1786 talloc_free(spaces);
1793 * Otherwise a NULL vpt may refer to an attribute defined
1794 * by a module. That is checked in pass 2.
1797 if (vpt->type == TMPL_TYPE_LIST) {
1798 cf_log_err_cs(cs, "Syntax error: Cannot switch over list '%s'", name2);
1804 * Walk through the children of the switch section,
1805 * ensuring that they're all 'case' statements
1807 for (ci = cf_item_find_next(cs, NULL);
1809 ci = cf_item_find_next(cs, ci)) {
1810 CONF_SECTION *subcs;
1813 if (!cf_item_is_section(ci)) {
1814 if (!cf_item_is_pair(ci)) continue;
1816 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1821 subcs = cf_item_to_section(ci); /* can't return NULL */
1822 name1 = cf_section_name1(subcs);
1824 if (strcmp(name1, "case") != 0) {
1825 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1830 name2 = cf_section_name2(subcs);
1832 if (!had_seen_default) {
1833 had_seen_default = true;
1837 cf_log_err(ci, "Cannot have two 'default' case statements");
1843 csingle = do_compile_modgroup(parent, component, cs,
1852 g = mod_callabletogroup(csingle);
1853 g->vpt = talloc_steal(g, vpt);
1858 static modcallable *do_compile_modcase(modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1862 modcallable *csingle;
1866 if (!parent || (parent->type != MOD_SWITCH)) {
1867 cf_log_err_cs(cs, "\"case\" statements may only appear within a \"switch\" section");
1872 * case THING means "match THING"
1873 * case means "match anything"
1875 name2 = cf_section_name2(cs);
1880 type = cf_section_name2_type(cs);
1882 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1883 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1884 char *spaces, *text;
1886 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1888 cf_log_err_cs(cs, "Syntax error");
1889 cf_log_err_cs(cs, "%s", name2);
1890 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1892 talloc_free(spaces);
1898 if (vpt->type == TMPL_TYPE_LIST) {
1899 cf_log_err_cs(cs, "Syntax error: Cannot match list '%s'", name2);
1904 * Otherwise a NULL vpt may refer to an attribute defined
1905 * by a module. That is checked in pass 2.
1912 csingle = do_compile_modgroup(parent, component, cs,
1922 * The interpretor expects this to be NULL for the
1923 * default case. do_compile_modgroup sets it to name2,
1924 * unless name2 is NULL, in which case it sets it to name1.
1926 csingle->name = name2;
1928 g = mod_callabletogroup(csingle);
1929 g->vpt = talloc_steal(g, vpt);
1932 * Set all of it's codes to return, so that
1933 * when we pick a 'case' statement, we don't
1934 * fall through to processing the next one.
1936 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1937 csingle->actions[i] = MOD_ACTION_RETURN;
1943 static modcallable *do_compile_modforeach(modcallable *parent,
1944 rlm_components_t component, CONF_SECTION *cs)
1948 modcallable *csingle;
1953 name2 = cf_section_name2(cs);
1956 "You must specify an attribute to loop over in 'foreach'");
1960 if (!cf_item_find_next(cs, NULL)) {
1961 cf_log_err_cs(cs, "'foreach' blocks cannot be empty");
1966 * Create the template. If we fail, AND it's a bare word
1967 * with &Foo-Bar, it MAY be an attribute defined by a
1968 * module. Allow it for now. The pass2 checks below
1971 type = cf_section_name2_type(cs);
1972 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1973 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1974 char *spaces, *text;
1976 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1978 cf_log_err_cs(cs, "Syntax error");
1979 cf_log_err_cs(cs, "%s", name2);
1980 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1982 talloc_free(spaces);
1989 * If we don't have a negative return code, we must have a vpt
1990 * (mostly to quiet coverity).
1994 if ((vpt->type != TMPL_TYPE_ATTR) && (vpt->type != TMPL_TYPE_LIST)) {
1995 cf_log_err_cs(cs, "MUST use attribute or list reference in 'foreach'");
2000 * Fix up the template to iterate over all instances of
2001 * the attribute. In a perfect consistent world, users would do
2002 * foreach &attr[*], but that's taking the consistency thing a bit far.
2004 vpt->tmpl_num = NUM_ALL;
2006 csingle = do_compile_modgroup(parent, component, cs,
2007 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2015 g = mod_callabletogroup(csingle);
2021 static modcallable *do_compile_modbreak(modcallable *parent,
2022 rlm_components_t component, CONF_ITEM const *ci)
2024 CONF_SECTION const *cs = NULL;
2026 for (cs = cf_item_parent(ci);
2028 cs = cf_item_parent(cf_section_to_item(cs))) {
2029 if (strcmp(cf_section_name1(cs), "foreach") == 0) {
2035 cf_log_err(ci, "'break' can only be used in a 'foreach' section");
2039 return do_compile_modgroup(parent, component, NULL,
2040 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2045 static modcallable *do_compile_modserver(modcallable *parent,
2046 rlm_components_t component, CONF_ITEM *ci,
2051 modcallable *csingle;
2052 CONF_SECTION *subcs;
2055 subcs = cf_section_sub_find_name2(cs, comp2str[component], NULL);
2057 cf_log_err(ci, "Server %s has no %s section",
2058 server, comp2str[component]);
2062 mr = talloc_zero(parent, modref);
2064 csingle = mod_reftocallable(mr);
2065 csingle->parent = parent;
2066 csingle->next = NULL;
2067 csingle->name = name;
2068 csingle->type = MOD_REFERENCE;
2069 csingle->method = component;
2071 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2072 sizeof(csingle->actions));
2074 mr->ref_name = strdup(server);
2080 static modcallable *do_compile_modxlat(modcallable *parent,
2081 rlm_components_t component, char const *fmt)
2083 modcallable *csingle;
2086 mx = talloc_zero(parent, modxlat);
2088 csingle = mod_xlattocallable(mx);
2089 csingle->parent = parent;
2090 csingle->next = NULL;
2091 csingle->name = "expand";
2092 csingle->type = MOD_XLAT;
2093 csingle->method = component;
2095 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2096 sizeof(csingle->actions));
2098 mx->xlat_name = strdup(fmt);
2099 if (fmt[0] != '%') {
2103 strcpy(mx->xlat_name, fmt + 1);
2104 p = strrchr(mx->xlat_name, '`');
2112 * redundant, etc. can refer to modules or groups, but not much else.
2114 static int all_children_are_modules(CONF_SECTION *cs, char const *name)
2118 for (ci=cf_item_find_next(cs, NULL);
2120 ci=cf_item_find_next(cs, ci)) {
2122 * If we're a redundant, etc. group, then the
2123 * intention is to call modules, rather than
2124 * processing logic. These checks aren't
2125 * *strictly* necessary, but they keep the users
2126 * from doing crazy things.
2128 if (cf_item_is_section(ci)) {
2129 CONF_SECTION *subcs = cf_item_to_section(ci);
2130 char const *name1 = cf_section_name1(subcs);
2132 if ((strcmp(name1, "if") == 0) ||
2133 (strcmp(name1, "else") == 0) ||
2134 (strcmp(name1, "elsif") == 0) ||
2135 (strcmp(name1, "update") == 0) ||
2136 (strcmp(name1, "switch") == 0) ||
2137 (strcmp(name1, "case") == 0)) {
2138 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
2145 if (cf_item_is_pair(ci)) {
2146 CONF_PAIR *cp = cf_item_to_pair(ci);
2147 if (cf_pair_value(cp) != NULL) {
2149 "Entry with no value is invalid");
2160 * Compile one entry of a module call.
2162 static modcallable *do_compile_modsingle(modcallable *parent,
2163 rlm_components_t component, CONF_ITEM *ci,
2165 char const **modname)
2167 char const *modrefname, *p;
2169 modcallable *csingle;
2170 module_instance_t *this;
2171 CONF_SECTION *cs, *subcs, *modules;
2173 char const *realname;
2174 rlm_components_t method = component;
2176 if (cf_item_is_section(ci)) {
2179 cs = cf_item_to_section(ci);
2180 modrefname = cf_section_name1(cs);
2181 name2 = cf_section_name2(cs);
2182 if (!name2) name2 = "";
2185 * group{}, redundant{}, or append{} may appear
2186 * where a single module instance was expected.
2187 * In that case, we hand it off to
2190 if (strcmp(modrefname, "group") == 0) {
2192 return do_compile_modgroup(parent, component, cs,
2194 grouptype, MOD_GROUP);
2196 } else if (strcmp(modrefname, "redundant") == 0) {
2199 if (!all_children_are_modules(cs, modrefname)) {
2203 return do_compile_modgroup(parent, component, cs,
2204 GROUPTYPE_REDUNDANT,
2205 grouptype, MOD_GROUP);
2207 } else if (strcmp(modrefname, "load-balance") == 0) {
2210 if (!all_children_are_modules(cs, modrefname)) {
2214 return do_compile_modgroup(parent, component, cs,
2216 grouptype, MOD_LOAD_BALANCE);
2218 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2221 if (!all_children_are_modules(cs, modrefname)) {
2225 return do_compile_modgroup(parent, component, cs,
2226 GROUPTYPE_REDUNDANT,
2227 grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2230 } else if (strcmp(modrefname, "if") == 0) {
2231 if (!cf_section_name2(cs)) {
2232 cf_log_err(ci, "'if' without condition");
2237 csingle= do_compile_modgroup(parent, component, cs,
2240 if (!csingle) return NULL;
2245 } else if (strcmp(modrefname, "elsif") == 0) {
2247 ((parent->type == MOD_LOAD_BALANCE) ||
2248 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2249 cf_log_err(ci, "'elsif' cannot be used in this section");
2253 if (!cf_section_name2(cs)) {
2254 cf_log_err(ci, "'elsif' without condition");
2259 return do_compile_modgroup(parent, component, cs,
2261 grouptype, MOD_ELSIF);
2263 } else if (strcmp(modrefname, "else") == 0) {
2265 ((parent->type == MOD_LOAD_BALANCE) ||
2266 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2267 cf_log_err(ci, "'else' cannot be used in this section section");
2271 if (cf_section_name2(cs)) {
2272 cf_log_err(ci, "Cannot have conditions on 'else'");
2277 return do_compile_modgroup(parent, component, cs,
2279 grouptype, MOD_ELSE);
2281 } else if (strcmp(modrefname, "update") == 0) {
2284 return do_compile_modupdate(parent, component, cs,
2287 } else if (strcmp(modrefname, "switch") == 0) {
2290 return do_compile_modswitch (parent, component, cs);
2292 } else if (strcmp(modrefname, "case") == 0) {
2295 return do_compile_modcase(parent, component, cs);
2297 } else if (strcmp(modrefname, "foreach") == 0) {
2300 return do_compile_modforeach(parent, component, cs);
2303 } /* else it's something like sql { fail = 1 ...} */
2305 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2309 * Else it's a module reference, with updated return
2313 CONF_PAIR *cp = cf_item_to_pair(ci);
2314 modrefname = cf_pair_attr(cp);
2317 * Actions (ok = 1), etc. are orthogonal to just
2318 * about everything else.
2320 if (cf_pair_value(cp) != NULL) {
2321 cf_log_err(ci, "Entry is not a reference to a module");
2326 * In-place xlat's via %{...}.
2328 * This should really be removed from the server.
2330 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2331 (modrefname[0] == '`')) {
2332 return do_compile_modxlat(parent, component,
2339 * These can't be over-ridden.
2341 if (strcmp(modrefname, "break") == 0) {
2342 if (!cf_item_is_pair(ci)) {
2343 cf_log_err(ci, "Invalid use of 'break' as section name.");
2347 return do_compile_modbreak(parent, component, ci);
2350 if (strcmp(modrefname, "return") == 0) {
2351 if (!cf_item_is_pair(ci)) {
2352 cf_log_err(ci, "Invalid use of 'return' as section name.");
2356 return do_compile_modgroup(parent, component, NULL,
2357 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2363 * Run a virtual server. This is really terrible and
2364 * should be deleted.
2366 if (strncmp(modrefname, "server[", 7) == 0) {
2369 if (!cf_item_is_pair(ci)) {
2370 cf_log_err(ci, "Invalid syntax");
2374 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2375 p = strrchr(buffer, ']');
2376 if (!p || p[1] != '\0' || (p == buffer)) {
2377 cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2381 buffer[p - buffer] = '\0';
2383 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2385 cf_log_err(ci, "No such server \"%s\".", buffer);
2390 * Ignore stupid attempts to over-ride the return
2393 return do_compile_modserver(parent, component, ci,
2394 modrefname, cs, buffer);
2398 * We now have a name. It can be one of two forms. A
2399 * bare module name, or a section named for the module,
2400 * with over-rides for the return codes.
2402 * The name can refer to a real module, in the "modules"
2403 * section. In that case, the name will be either the
2404 * first or second name of the sub-section of "modules".
2406 * Or, the name can refer to a policy, in the "policy"
2407 * section. In that case, the name will be first name of
2408 * the sub-section of "policy". Unless it's a "redudant"
2411 * Or, the name can refer to a "module.method", in which
2412 * case we're calling a different method than normal for
2415 * Or, the name can refer to a virtual module, in the
2416 * "instantiate" section. In that case, the name will be
2417 * the first of the sub-section of "instantiate". Unless
2418 * it's a "redudant" block...
2420 * We try these in sequence, from the bottom up. This is
2421 * so that things in "instantiate" and "policy" can
2422 * over-ride calls to real modules.
2429 * instantiate { ... name { ...} ... }
2430 * instantiate { ... name.method { ...} ... }
2431 * policy { ... name { .. } .. }
2432 * policy { ... name.method { .. } .. }
2434 * The "instantiate" virtual modules are identical to the
2435 * policies at this point. We should probably get rid of
2436 * the "instantiate" ones, as they're duplicate and
2440 cs = cf_section_find("instantiate");
2441 if (cs) subcs = cf_section_sub_find_name2(cs, NULL,
2444 (cs = cf_section_find("policy")) != NULL) {
2447 snprintf(buffer, sizeof(buffer), "%s.%s",
2448 modrefname, comp2str[component]);
2451 * Prefer name.section, then name.
2453 subcs = cf_section_sub_find_name2(cs, NULL,
2456 subcs = cf_section_sub_find_name2(cs, NULL,
2462 * Check that we're not creating a loop. We may
2463 * be compiling an "sql" module reference inside
2464 * of an "sql" policy. If so, we allow the
2465 * second "sql" to refer to the module.
2467 for (loop = cf_item_parent(ci);
2469 loop = cf_item_parent(cf_section_to_item(loop))) {
2470 if (loop == subcs) {
2476 * We've found the relevant entry. It MUST be a
2479 * However, it can be a "redundant" block, or just a
2484 * modules.c takes care of ensuring that this is:
2487 * load-balance foo { ...
2488 * redundant foo { ...
2489 * redundant-load-balance foo { ...
2491 * We can just recurs to compile the section as
2492 * if it was found here.
2494 if (cf_section_name2(subcs)) {
2495 csingle = do_compile_modsingle(parent,
2497 cf_section_to_item(subcs),
2506 * So we compile it like it was:
2510 csingle = do_compile_modgroup(parent,
2514 grouptype, MOD_GROUP);
2518 * Return the compiled thing if we can.
2520 if (!csingle) return NULL;
2521 if (cf_item_is_pair(ci)) return csingle;
2524 * Else we have a reference to a policy, and that reference
2525 * over-rides the return codes for the policy!
2527 goto action_override;
2531 * Not a virtual module. It must be a real module.
2533 modules = cf_section_find("modules");
2535 realname = modrefname;
2539 * Try to load the optional module.
2541 if (realname[0] == '-') realname++;
2544 * As of v3, the "modules" section contains
2545 * modules we use. Configuration for other
2546 * modules belongs in raddb/mods-available/,
2547 * which isn't loaded into the "modules" section.
2549 if (cf_section_sub_find_name2(modules, NULL, realname)) {
2550 this = find_module_instance(modules, realname, true);
2551 if (this) goto allocate_csingle;
2556 if (realname != modrefname) {
2562 * We were asked to MAYBE load it and it
2563 * doesn't exist. Return a soft error.
2565 if (realname != modrefname) {
2566 *modname = modrefname;
2573 * No module found by that name. Maybe we're calling
2576 p = strrchr(modrefname, '.');
2582 * Find the component.
2584 for (i = RLM_COMPONENT_AUTH;
2585 i < RLM_COMPONENT_COUNT;
2587 if (strcmp(p, comp2str[i]) == 0) {
2590 strlcpy(buffer, modrefname, sizeof(buffer));
2591 buffer[p - modrefname - 1] = '\0';
2594 this = find_module_instance(modules, buffer, true);
2597 goto allocate_csingle;
2603 * FIXME: check for "module", and give error "no
2604 * such component" when we don't find the method.
2609 * Can't de-reference it to anything. Ugh.
2612 cf_log_err(ci, "Failed to find \"%s\" as a module or policy.", modrefname);
2613 cf_log_err(ci, "Please verify that the configuration exists in %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2617 * We know it's all OK, allocate the structures, and fill
2622 * Check if the module in question has the necessary
2625 if (!this->entry->module->methods[method]) {
2626 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2631 single = talloc_zero(parent, modsingle);
2632 single->modinst = this;
2633 *modname = this->entry->module->name;
2635 csingle = mod_singletocallable(single);
2636 csingle->parent = parent;
2637 csingle->next = NULL;
2638 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2639 memcpy(csingle->actions, defaultactions[component][grouptype],
2640 sizeof csingle->actions);
2641 } else { /* inside Auth-Type has different rules */
2642 memcpy(csingle->actions, authtype_actions[grouptype],
2643 sizeof csingle->actions);
2645 rad_assert(modrefname != NULL);
2646 csingle->name = realname;
2647 csingle->type = MOD_SINGLE;
2648 csingle->method = method;
2652 * Over-ride the default return codes of the module.
2654 if (cf_item_is_section(ci)) {
2657 cs = cf_item_to_section(ci);
2658 for (csi=cf_item_find_next(cs, NULL);
2660 csi=cf_item_find_next(cs, csi)) {
2662 if (cf_item_is_section(csi)) {
2663 cf_log_err(csi, "Subsection of module instance call not allowed");
2664 talloc_free(csingle);
2668 if (!cf_item_is_pair(csi)) continue;
2670 if (!compile_action(csingle, cf_item_to_pair(csi))) {
2671 talloc_free(csingle);
2680 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2681 modcallable **parent,
2682 rlm_components_t component, CONF_ITEM *ci,
2683 char const **modname)
2690 CONF_SECTION *parentcs;
2692 g = talloc_zero(ctx, modgroup);
2693 memset(g, 0, sizeof(*g));
2694 g->grouptype = GROUPTYPE_SIMPLE;
2695 c = mod_grouptocallable(g);
2698 defaultactions[component][GROUPTYPE_SIMPLE],
2699 sizeof(c->actions));
2701 parentcs = cf_item_parent(ci);
2702 c->name = cf_section_name2(parentcs);
2704 c->name = cf_section_name1(parentcs);
2707 c->type = MOD_GROUP;
2708 c->method = component;
2711 *parent = mod_grouptocallable(g);
2714 ret = do_compile_modsingle(*parent, component, ci,
2717 dump_tree(component, ret);
2723 * Internal compile group code.
2725 static modcallable *do_compile_modgroup(modcallable *parent,
2726 rlm_components_t component, CONF_SECTION *cs,
2727 int grouptype, int parentgrouptype, int mod_type)
2734 g = talloc_zero(parent, modgroup);
2735 g->grouptype = grouptype;
2739 c = mod_grouptocallable(g);
2743 memset(c->actions, 0, sizeof(c->actions));
2745 if (!cs) { /* only for "break" and "return" */
2751 * Remember the name for printing, etc.
2753 * FIXME: We may also want to put the names into a
2754 * rbtree, so that groups can reference each other...
2756 c->name = cf_section_name2(cs);
2758 c->name = cf_section_name1(cs);
2759 if ((strcmp(c->name, "group") == 0) ||
2760 (strcmp(c->name, "redundant") == 0)) {
2762 } else if (c->type == MOD_GROUP) {
2763 c->type = MOD_POLICY;
2769 * Do load-time optimizations
2771 if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2774 rad_assert(parent != NULL);
2776 if (c->type == MOD_IF) {
2777 g->cond = cf_data_find(g->cs, "if");
2778 rad_assert(g->cond != NULL);
2781 if (g->cond->type == COND_TYPE_FALSE) {
2782 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2783 unlang_keyword[g->mc.type],
2784 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2788 } else if (c->type == MOD_ELSIF) {
2790 g->cond = cf_data_find(g->cs, "if");
2791 rad_assert(g->cond != NULL);
2793 rad_assert(parent != NULL);
2794 p = mod_callabletogroup(parent);
2796 rad_assert(p->tail != NULL);
2798 f = mod_callabletogroup(p->tail);
2799 rad_assert((f->mc.type == MOD_IF) ||
2800 (f->mc.type == MOD_ELSIF));
2803 * If we took the previous condition, we
2804 * don't need to take this one.
2806 * We reset our condition to 'true', so
2807 * that subsequent sections can check
2808 * that they don't need to be executed.
2810 if (f->cond->type == COND_TYPE_TRUE) {
2812 INFO(" # Skipping contents of '%s' as previous '%s' is always 'true' -- %s:%d",
2813 unlang_keyword[g->mc.type],
2814 unlang_keyword[f->mc.type],
2815 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2822 rad_assert(c->type == MOD_ELSE);
2824 rad_assert(parent != NULL);
2825 p = mod_callabletogroup(parent);
2827 rad_assert(p->tail != NULL);
2829 f = mod_callabletogroup(p->tail);
2830 rad_assert((f->mc.type == MOD_IF) ||
2831 (f->mc.type == MOD_ELSIF));
2834 * If we took the previous condition, we
2835 * don't need to take this one.
2837 if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2841 * Else we need to compile this section
2847 * Loop over the children of this group.
2849 for (ci=cf_item_find_next(cs, NULL);
2851 ci=cf_item_find_next(cs, ci)) {
2854 * Sections are references to other groups, or
2855 * to modules with updated return codes.
2857 if (cf_item_is_section(ci)) {
2858 char const *junk = NULL;
2859 modcallable *single;
2860 CONF_SECTION *subcs = cf_item_to_section(ci);
2862 single = do_compile_modsingle(c, component, ci,
2865 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2866 cf_section_name1(subcs));
2870 add_child(g, single);
2872 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2876 char const *attr, *value;
2877 CONF_PAIR *cp = cf_item_to_pair(ci);
2879 attr = cf_pair_attr(cp);
2880 value = cf_pair_value(cp);
2883 * A CONF_PAIR is either a module
2884 * instance with no actions
2888 modcallable *single;
2889 char const *junk = NULL;
2891 single = do_compile_modsingle(c,
2897 if (cf_item_is_pair(ci) &&
2898 cf_pair_attr(cf_item_to_pair(ci))[0] == '-') {
2903 "Failed to parse \"%s\" entry.",
2908 add_child(g, single);
2911 * Or a module instance with action.
2913 } else if (!compile_action(c, cp)) {
2916 } /* else it worked */
2922 * Set the default actions, if they haven't already been
2925 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2926 if (!c->actions[i]) {
2927 if (!parent || (component != RLM_COMPONENT_AUTH)) {
2928 c->actions[i] = defaultactions[component][parentgrouptype][i];
2929 } else { /* inside Auth-Type has different rules */
2930 c->actions[i] = authtype_actions[parentgrouptype][i];
2940 if (grouptype != GROUPTYPE_REDUNDANT) break;
2943 case MOD_LOAD_BALANCE:
2944 case MOD_REDUNDANT_LOAD_BALANCE:
2946 cf_log_err_cs(g->cs, "%s sections cannot be empty",
2947 cf_section_name1(g->cs));
2954 * FIXME: If there are no children, return NULL?
2956 return mod_grouptocallable(g);
2959 modcallable *compile_modgroup(modcallable *parent,
2960 rlm_components_t component, CONF_SECTION *cs)
2962 modcallable *ret = do_compile_modgroup(parent, component, cs,
2964 GROUPTYPE_SIMPLE, MOD_GROUP);
2966 if (rad_debug_lvl > 3) {
2967 modcall_debug(ret, 2);
2973 void add_to_modcallable(modcallable *parent, modcallable *this)
2977 rad_assert(this != NULL);
2978 rad_assert(parent != NULL);
2980 g = mod_callabletogroup(parent);
2987 static bool pass2_xlat_compile(CONF_ITEM const *ci, vp_tmpl_t **pvpt, bool convert,
2988 DICT_ATTR const *da)
2998 rad_assert(vpt->type == TMPL_TYPE_XLAT);
3000 fmt = talloc_typed_strdup(vpt, vpt->name);
3001 slen = xlat_tokenize(vpt, fmt, &head, &error);
3004 char *spaces, *text;
3006 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3008 cf_log_err(ci, "Failed parsing expanded string:");
3009 cf_log_err(ci, "%s", text);
3010 cf_log_err(ci, "%s^ %s", spaces, error);
3012 talloc_free(spaces);
3018 * Convert %{Attribute-Name} to &Attribute-Name
3023 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
3026 * If it's a virtual attribute, leave it
3029 if (attr->tmpl_da->flags.virtual) {
3035 * If the attribute is of incompatible
3036 * type, leave it alone.
3038 if (da && (da->type != attr->tmpl_da->type)) {
3043 if (cf_item_is_pair(ci)) {
3044 CONF_PAIR *cp = cf_item_to_pair(ci);
3046 WARN("%s[%d] Please change %%{%s} to &%s",
3047 cf_pair_filename(cp), cf_pair_lineno(cp),
3048 attr->name, attr->name);
3050 CONF_SECTION *cs = cf_item_to_section(ci);
3052 WARN("%s[%d] Please change %%{%s} to &%s",
3053 cf_section_filename(cs), cf_section_lineno(cs),
3054 attr->name, attr->name);
3063 * Re-write it to be a pre-parsed XLAT structure.
3065 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3066 vpt->tmpl_xlat = head;
3073 static bool pass2_regex_compile(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3078 rad_assert(vpt->type == TMPL_TYPE_REGEX);
3081 * It's a dynamic expansion. We can't expand the string,
3082 * but we can pre-parse it as an xlat struct. In that
3083 * case, we convert it to a pre-compiled XLAT.
3085 * This is a little more complicated than it needs to be
3086 * because radius_evaluate_map() keys off of the src
3087 * template type, instead of the operators. And, the
3088 * pass2_xlat_compile() function expects to get passed an
3089 * XLAT instead of a REGEX.
3091 if (strchr(vpt->name, '%')) {
3092 vpt->type = TMPL_TYPE_XLAT;
3093 return pass2_xlat_compile(ci, &vpt, false, NULL);
3096 slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3097 vpt->tmpl_iflag, vpt->tmpl_mflag, true, false);
3099 char *spaces, *text;
3101 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3103 cf_log_err(ci, "Invalid regular expression:");
3104 cf_log_err(ci, "%s", text);
3105 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3107 talloc_free(spaces);
3113 vpt->type = TMPL_TYPE_REGEX_STRUCT;
3114 vpt->tmpl_preg = preg;
3120 static bool pass2_fixup_undefined(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3122 DICT_ATTR const *da;
3124 rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3126 da = dict_attrbyname(vpt->tmpl_unknown_name);
3128 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3133 vpt->type = TMPL_TYPE_ATTR;
3137 static bool pass2_callback(UNUSED void *ctx, fr_cond_t *c)
3139 value_pair_map_t *map;
3141 if (c->type == COND_TYPE_EXISTS) {
3142 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3143 return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3146 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3149 * The existence check might have been &Foo-Bar,
3150 * where Foo-Bar is defined by a module.
3152 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3153 if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3154 c->pass2_fixup = PASS2_FIXUP_NONE;
3160 * Maps have a paircompare fixup applied to them.
3161 * Others get ignored.
3163 if (c->pass2_fixup == PASS2_FIXUP_NONE) {
3164 if (c->type == COND_TYPE_MAP) {
3172 map = c->data.map; /* shorter */
3177 * Where "foo" is dynamically defined.
3179 if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3180 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3181 map->lhs->tmpl_da->vendor,
3183 cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3184 map->lhs->tmpl_da->name,
3190 * These guys can't have a paircompare fixup applied.
3192 c->pass2_fixup = PASS2_FIXUP_NONE;
3196 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3197 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3198 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3201 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3202 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3205 c->pass2_fixup = PASS2_FIXUP_NONE;
3210 * Just in case someone adds a new fixup later.
3212 rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3213 (c->pass2_fixup == PASS2_PAIRCOMPARE));
3218 if (map->lhs->type == TMPL_TYPE_XLAT) {
3220 * Don't compile the LHS to an attribute
3221 * reference for now. When we do that, we've got
3222 * to check the RHS for type-specific data, and
3223 * parse it to a TMPL_TYPE_DATA.
3225 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3230 if (map->rhs->type == TMPL_TYPE_XLAT) {
3232 * Convert the RHS to an attribute reference only
3233 * if the LHS is an attribute reference, AND is
3234 * of the same type as the RHS.
3236 * We can fix this when the code in evaluate.c
3237 * can handle strings on the LHS, and attributes
3238 * on the RHS. For now, the code in parser.c
3241 if (map->lhs->type == TMPL_TYPE_ATTR) {
3242 DICT_ATTR const *da = c->cast;
3244 if (!c->cast) da = map->lhs->tmpl_da;
3246 if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3251 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3258 * Convert bare refs to %{Foreach-Variable-N}
3260 if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3261 (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3266 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3267 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3268 T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3270 char *spaces, *text;
3272 fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3274 cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3275 cf_log_err(map->ci, "%s", fmt);
3276 cf_log_err(map->ci, "%s^ %s", spaces, text);
3278 talloc_free(spaces);
3284 talloc_free(map->lhs);
3289 if (map->rhs->type == TMPL_TYPE_REGEX) {
3290 if (!pass2_regex_compile(map->ci, map->rhs)) {
3294 rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3298 * Only attributes can have a paircompare registered, and
3299 * they can only be with the current REQUEST, and only
3300 * with the request pairs.
3302 if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3303 (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3304 (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3308 if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3310 if (map->rhs->type == TMPL_TYPE_ATTR) {
3311 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3316 if (map->rhs->type == TMPL_TYPE_REGEX) {
3317 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3323 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3328 if (map->op != T_OP_CMP_EQ) {
3329 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3335 * Mark it as requiring a paircompare() call, instead of
3338 c->pass2_fixup = PASS2_PAIRCOMPARE;
3345 * Compile the RHS of update sections to xlat_exp_t
3347 static bool modcall_pass2_update(modgroup *g)
3349 value_pair_map_t *map;
3351 for (map = g->map; map != NULL; map = map->next) {
3352 if (map->rhs->type == TMPL_TYPE_XLAT) {
3353 rad_assert(map->rhs->tmpl_xlat == NULL);
3356 * FIXME: compile to attribute && handle
3357 * the conversion in map_to_vp().
3359 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3364 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3367 * Deal with undefined attributes now.
3369 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3370 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3373 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3374 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3383 * Do a second-stage pass on compiling the modules.
3385 bool modcall_pass2(modcallable *mc)
3392 for (c = mc; c != NULL; c = c->next) {
3400 g = mod_callabletogroup(c);
3401 if (g->done_pass2) goto do_next;
3403 name2 = cf_section_name2(g->cs);
3405 c->debug_name = unlang_keyword[c->type];
3407 c->debug_name = talloc_asprintf(c, "update %s", name2);
3410 if (!modcall_pass2_update(g)) {
3413 g->done_pass2 = true;
3416 case MOD_XLAT: /* @todo: pre-parse xlat's */
3423 c->debug_name = c->name;
3424 break; /* do nothing */
3429 g = mod_callabletogroup(c);
3430 if (g->done_pass2) goto do_next;
3432 name2 = cf_section_name2(g->cs);
3433 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3436 * The compilation code takes care of
3437 * simplifying 'true' and 'false'
3438 * conditions. For others, we have to do
3439 * a second pass to parse && compile
3442 if (!((g->cond->type == COND_TYPE_TRUE) ||
3443 (g->cond->type == COND_TYPE_FALSE))) {
3444 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3449 if (!modcall_pass2(g->children)) return false;
3450 g->done_pass2 = true;
3456 g = mod_callabletogroup(c);
3457 if (g->done_pass2) goto do_next;
3459 name2 = cf_section_name2(g->cs);
3460 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3463 * We had &Foo-Bar, where Foo-Bar is
3464 * defined by a module.
3467 rad_assert(c->name != NULL);
3468 rad_assert(c->name[0] == '&');
3469 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3471 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3472 cf_section_name2_type(g->cs),
3473 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3475 char *spaces, *text;
3478 fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3480 cf_log_err_cs(g->cs, "Syntax error");
3481 cf_log_err_cs(g->cs, "%s", c->name);
3482 cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3484 talloc_free(spaces);
3494 * Statically compile xlats
3496 if (g->vpt->type == TMPL_TYPE_XLAT) {
3497 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3498 &g->vpt, true, NULL)) {
3506 * We may have: switch Foo-Bar {
3508 * where Foo-Bar is an attribute defined
3509 * by a module. Since there's no leading
3510 * &, it's parsed as a literal. But if
3511 * we can parse it as an attribute,
3512 * switch to using that.
3514 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3517 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3518 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3519 if (slen < 0) goto parse_error;
3520 if (vpt->type == TMPL_TYPE_ATTR) {
3521 talloc_free(g->vpt);
3529 * Warn about old-style configuration.
3531 * DEPRECATED: switch User-Name { ...
3532 * ALLOWED : switch &User-Name { ...
3534 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3535 (c->name[0] != '&')) {
3536 WARN("%s[%d]: Please change %s to &%s",
3537 cf_section_filename(g->cs),
3538 cf_section_lineno(g->cs),
3543 if (!modcall_pass2(g->children)) return false;
3544 g->done_pass2 = true;
3548 g = mod_callabletogroup(c);
3549 if (g->done_pass2) goto do_next;
3551 name2 = cf_section_name2(g->cs);
3553 c->debug_name = unlang_keyword[c->type];
3555 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3558 rad_assert(c->parent != NULL);
3559 rad_assert(c->parent->type == MOD_SWITCH);
3562 * The statement may refer to an
3563 * attribute which doesn't exist until
3564 * all of the modules have been loaded.
3565 * Check for that now.
3567 if (!g->vpt && c->name &&
3568 (c->name[0] == '&') &&
3569 (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3570 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3571 cf_section_name2_type(g->cs),
3572 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3573 if (slen < 0) goto parse_error;
3577 * We have "case {...}". There's no
3578 * argument, so we don't need to check
3581 if (!g->vpt) goto do_children;
3584 * Do type-specific checks on the case statement
3586 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3589 f = mod_callabletogroup(mc->parent);
3590 rad_assert(f->vpt != NULL);
3593 * We're switching over an
3594 * attribute. Check that the
3597 if (f->vpt->type == TMPL_TYPE_ATTR) {
3598 rad_assert(f->vpt->tmpl_da != NULL);
3600 if (tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da) < 0) {
3601 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3611 * Compile and sanity check xlat
3614 if (g->vpt->type == TMPL_TYPE_XLAT) {
3617 f = mod_callabletogroup(mc->parent);
3618 rad_assert(f->vpt != NULL);
3621 * Don't expand xlat's into an
3622 * attribute of a different type.
3624 if (f->vpt->type == TMPL_TYPE_ATTR) {
3625 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3626 &g->vpt, true, f->vpt->tmpl_da)) {
3630 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3631 &g->vpt, true, NULL)) {
3637 if (!modcall_pass2(g->children)) return false;
3638 g->done_pass2 = true;
3642 g = mod_callabletogroup(c);
3643 if (g->done_pass2) goto do_next;
3645 name2 = cf_section_name2(g->cs);
3646 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3649 * Already parsed, handle the children.
3651 if (g->vpt) goto check_children;
3654 * We had &Foo-Bar, where Foo-Bar is
3655 * defined by a module.
3657 rad_assert(c->name != NULL);
3658 rad_assert(c->name[0] == '&');
3659 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3662 * The statement may refer to an
3663 * attribute which doesn't exist until
3664 * all of the modules have been loaded.
3665 * Check for that now.
3667 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3668 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3669 if (slen < 0) goto parse_error;
3672 rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST));
3673 if (g->vpt->tmpl_num != NUM_ALL) {
3674 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3677 if (!modcall_pass2(g->children)) return false;
3678 g->done_pass2 = true;
3682 c->debug_name = unlang_keyword[c->type];
3686 g = mod_callabletogroup(c);
3687 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], cf_section_name1(g->cs));
3692 case MOD_LOAD_BALANCE:
3693 case MOD_REDUNDANT_LOAD_BALANCE:
3694 c->debug_name = unlang_keyword[c->type];
3699 g = mod_callabletogroup(c);
3701 c->debug_name = mc->name; /* for authorize, etc. */
3703 } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3704 char const *name1 = cf_section_name1(g->cs);
3706 if (strcmp(name1, unlang_keyword[c->type]) != 0) {
3707 c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3711 if (g->done_pass2) goto do_next;
3712 if (!modcall_pass2(g->children)) return false;
3713 g->done_pass2 = true;
3718 rad_assert(c->debug_name != NULL);
3724 void modcall_debug(modcallable *mc, int depth)
3728 value_pair_map_t *map;
3731 for (this = mc; this != NULL; this = this->next) {
3732 switch (this->type) {
3737 modsingle *single = mod_callabletosingle(this);
3739 DEBUG("%.*s%s", depth, modcall_spaces,
3740 single->modinst->name);
3746 g = mod_callabletogroup(this);
3747 DEBUG("%.*s%s {", depth, modcall_spaces,
3748 unlang_keyword[this->type]);
3750 for (map = g->map; map != NULL; map = map->next) {
3751 map_prints(buffer, sizeof(buffer), map);
3752 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3755 DEBUG("%.*s}", depth, modcall_spaces);
3759 g = mod_callabletogroup(this);
3760 DEBUG("%.*s%s {", depth, modcall_spaces,
3761 unlang_keyword[this->type]);
3762 modcall_debug(g->children, depth + 1);
3763 DEBUG("%.*s}", depth, modcall_spaces);
3768 g = mod_callabletogroup(this);
3769 fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3770 DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3771 unlang_keyword[this->type], buffer);
3772 modcall_debug(g->children, depth + 1);
3773 DEBUG("%.*s}", depth, modcall_spaces);
3778 g = mod_callabletogroup(this);
3779 tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3780 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3781 unlang_keyword[this->type], buffer);
3782 modcall_debug(g->children, depth + 1);
3783 DEBUG("%.*s}", depth, modcall_spaces);
3788 g = mod_callabletogroup(this);
3789 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3790 unlang_keyword[this->type], this->name);
3791 modcall_debug(g->children, depth + 1);
3792 DEBUG("%.*s}", depth, modcall_spaces);
3796 DEBUG("%.*sbreak", depth, modcall_spaces);
3801 g = mod_callabletogroup(this);
3802 DEBUG("%.*s%s {", depth, modcall_spaces,
3803 unlang_keyword[this->type]);
3804 modcall_debug(g->children, depth + 1);
3805 DEBUG("%.*s}", depth, modcall_spaces);
3809 case MOD_LOAD_BALANCE:
3810 case MOD_REDUNDANT_LOAD_BALANCE:
3811 g = mod_callabletogroup(this);
3812 DEBUG("%.*s%s {", depth, modcall_spaces,
3813 unlang_keyword[this->type]);
3814 modcall_debug(g->children, depth + 1);
3815 DEBUG("%.*s}", depth, modcall_spaces);