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 vp_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[MOD_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);
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 been asked to unwind to the
691 * enclosing "foreach". We're here, so
692 * we can stop unwinding.
694 if (next->unwind == MOD_BREAK) {
700 * Unwind all the way.
702 if (next->unwind == MOD_RETURN) {
703 entry->unwind = MOD_RETURN;
706 } /* loop over VPs */
709 * Free the copied vps and the request data
710 * If we don't remove the request data, something could call
711 * the xlat outside of a foreach loop and trigger a segv.
713 fr_pair_list_free(&vps);
714 request_data_get(request, (void *)radius_get_vp, foreach_depth);
716 rad_assert(next != NULL);
717 result = next->result;
718 priority = next->priority;
720 goto calculate_result;
724 * Break out of a "foreach" loop, or return from a nested
727 if ((c->type == MOD_BREAK) || (c->type == MOD_RETURN)) {
731 RDEBUG2("%s", unlang_keyword[c->type]);
733 for (i = 8; i >= 0; i--) {
734 copy_p = request_data_get(request, (void *)radius_get_vp, i);
736 if (c->type == MOD_BREAK) {
737 RDEBUG2("# break Foreach-Variable-%d", i);
744 * Leave result / priority on the stack, and stop processing the section.
746 entry->unwind = c->type;
750 #endif /* WITH_UNLANG */
753 * Child is a group that has children of it's own.
755 if ((c->type == MOD_GROUP) || (c->type == MOD_POLICY)
757 || (c->type == MOD_CASE)
765 g = mod_callabletogroup(c);
768 * This should really have been caught in the
769 * compiler, and the node never generated. But
770 * doing that requires changing it's API so that
771 * it returns a flag instead of the compiled
775 RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
780 modcall_child(request, component,
781 depth + 1, entry, g->children,
784 goto calculate_result;
788 if (c->type == MOD_SWITCH) {
789 modcallable *this, *found, *null_case;
798 g = mod_callabletogroup(c);
800 memset(&cond, 0, sizeof(cond));
801 memset(&map, 0, sizeof(map));
803 cond.type = COND_TYPE_MAP;
804 cond.data.map = ↦
806 map.op = T_OP_CMP_EQ;
807 map.ci = cf_section_to_item(g->cs);
809 rad_assert(g->vpt != NULL);
811 null_case = found = NULL;
815 * The attribute doesn't exist. We can skip
816 * directly to the default 'case' statement.
818 if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
820 for (this = g->children; this; this = this->next) {
821 rad_assert(this->type == MOD_CASE);
823 h = mod_callabletogroup(this);
824 if (h->vpt) continue;
834 * Expand the template if necessary, so that it
835 * is evaluated once instead of for each 'case'
838 if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
839 (g->vpt->type == TMPL_TYPE_XLAT) ||
840 (g->vpt->type == TMPL_TYPE_EXEC)) {
844 len = tmpl_aexpand(request, &p, request, g->vpt, NULL, NULL);
845 if (len < 0) goto find_null_case;
847 tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, len);
851 * Find either the exact matching name, or the
852 * "case {...}" statement.
854 for (this = g->children; this; this = this->next) {
855 rad_assert(this->type == MOD_CASE);
857 h = mod_callabletogroup(this);
860 * Remember the default case
863 if (!null_case) null_case = this;
868 * If we're switching over an attribute
869 * AND we haven't pre-parsed the data for
870 * the case statement, then cast the data
871 * to the type of the attribute.
873 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
874 (h->vpt->type != TMPL_TYPE_DATA)) {
877 cond.cast = g->vpt->tmpl_da;
880 * Remove unnecessary casting.
882 if ((h->vpt->type == TMPL_TYPE_ATTR) &&
883 (g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
888 * Use the pre-expanded string.
890 } else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
891 (g->vpt->type == TMPL_TYPE_XLAT) ||
892 (g->vpt->type == TMPL_TYPE_EXEC)) {
898 * Else evaluate the 'switch' statement.
906 if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
913 if (!found) found = null_case;
916 talloc_free(data.ptr);
917 modcall_child(request, component, depth + 1, entry, found, &result, true);
919 goto calculate_result;
923 if ((c->type == MOD_LOAD_BALANCE) ||
924 (c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
926 modcallable *this, *found;
931 g = mod_callabletogroup(c);
933 rad_assert(g->children != NULL);
936 * Choose a child at random.
938 for (this = g->children; this; this = this->next) {
941 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
946 if (c->type == MOD_LOAD_BALANCE) {
947 modcall_child(request, component,
948 depth + 1, entry, found,
955 modcall_child(request, component,
956 depth + 1, entry, this,
958 if (this->actions[result] == MOD_ACTION_RETURN) {
964 if (!this) this = g->children;
965 } while (this != found);
968 goto calculate_result;
969 } /* MOD_LOAD_BALANCE */
972 * Reference another virtual server.
974 * This should really be deleted, and replaced with a
975 * more abstracted / functional version.
977 if (c->type == MOD_REFERENCE) {
978 modref *mr = mod_callabletoref(c);
979 char const *server = request->server;
981 if (server == mr->ref_name) {
982 RWDEBUG("Suppressing recursive call to server %s", server);
986 request->server = mr->ref_name;
987 RDEBUG("server %s { # nested call", mr->ref_name);
988 result = indexed_modcall(component, 0, request);
989 RDEBUG("} # server %s with nested call", mr->ref_name);
990 request->server = server;
991 goto calculate_result;
992 } /* MOD_REFERENCE */
995 * xlat a string without doing anything else
997 * This should really be deleted, and replaced with a
998 * more abstracted / functional version.
1000 if (c->type == MOD_XLAT) {
1001 modxlat *mx = mod_callabletoxlat(c);
1005 radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
1007 RDEBUG("`%s`", mx->xlat_name);
1008 radius_exec_program(request, NULL, 0, NULL, request, mx->xlat_name, request->packet->vps,
1009 false, true, EXEC_TIMEOUT);
1016 * Add new module types here.
1021 RDEBUG("(%s, %d) ? (%s, %d)",
1022 fr_int2str(mod_rcode_table, result, "<invalid>"),
1024 fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
1029 rad_assert(result != RLM_MODULE_UNKNOWN);
1032 * The child's action says return. Do so.
1034 if ((c->actions[result] == MOD_ACTION_RETURN) &&
1036 entry->result = result;
1041 * If "reject", break out of the loop and return
1044 if (c->actions[result] == MOD_ACTION_REJECT) {
1045 entry->result = RLM_MODULE_REJECT;
1050 * The array holds a default priority for this return
1051 * code. Grab it in preference to any unset priority.
1054 priority = c->actions[result];
1058 * We're higher than any previous priority, remember this
1059 * return code and priority.
1061 if (priority > entry->priority) {
1062 entry->result = result;
1063 entry->priority = priority;
1068 * If we're processing a "case" statement, we return once
1069 * it's done, rather than going to the next "case" statement.
1071 if (c->type == MOD_CASE) goto finish;
1075 * If we've been told to stop processing
1078 if (entry->unwind == MOD_BREAK) {
1079 RDEBUG2("# unwind to enclosing foreach");
1083 if (entry->unwind == MOD_RETURN) {
1088 if (do_next_sibling) {
1089 entry->c = entry->c->next;
1091 if (entry->c) goto redo;
1103 /** Call a module, iteratively, with a local stack, rather than recursively
1105 * What did Paul Graham say about Lisp...?
1107 int modcall(rlm_components_t component, modcallable *c, REQUEST *request)
1109 modcall_stack_entry_t stack[MODCALL_STACK_MAX];
1112 memset(stack, 0, sizeof(stack));
1115 * Set up the initial stack frame.
1118 stack[0].result = default_component_results[component];
1119 stack[0].priority = 0;
1120 stack[0].unwind = 0;
1123 * Call the main handler.
1125 if (!modcall_recurse(request, component, 0, &stack[0], true)) {
1126 return RLM_MODULE_FAIL;
1130 * Return the result.
1132 return stack[0].result;
1137 static char const *action2str(int action)
1139 static char buf[32];
1140 if(action==MOD_ACTION_RETURN)
1142 if(action==MOD_ACTION_REJECT)
1144 snprintf(buf, sizeof buf, "%d", action);
1148 /* If you suspect a bug in the parser, you'll want to use these dump
1149 * functions. dump_tree should reproduce a whole tree exactly as it was found
1150 * in radiusd.conf, but in long form (all actions explicitly defined) */
1151 static void dump_mc(modcallable *c, int indent)
1155 if(c->type==MOD_SINGLE) {
1156 modsingle *single = mod_callabletosingle(c);
1157 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1158 single->modinst->name);
1159 } else if ((c->type > MOD_SINGLE) && (c->type <= MOD_POLICY)) {
1160 modgroup *g = mod_callabletogroup(c);
1162 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1163 unlang_keyword[c->type]);
1164 for(p = g->children;p;p = p->next)
1165 dump_mc(p, indent+1);
1166 } /* else ignore it for now */
1168 for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
1169 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
1170 fr_int2str(mod_rcode_table, i, "<invalid>"),
1171 action2str(c->actions[i]));
1174 DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
1177 static void dump_tree(rlm_components_t comp, modcallable *c)
1179 DEBUG("[%s]", comp2str[comp]);
1183 #define dump_tree(a, b)
1186 /* These are the default actions. For each component, the group{} block
1187 * behaves like the code from the old module_*() function. redundant{}
1188 * are based on my guesses of what they will be used for. --Pac. */
1190 defaultactions[MOD_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1196 MOD_ACTION_RETURN, /* reject */
1198 MOD_ACTION_RETURN, /* ok */
1199 MOD_ACTION_RETURN, /* handled */
1201 MOD_ACTION_RETURN, /* userlock */
1202 MOD_ACTION_RETURN, /* notfound */
1208 MOD_ACTION_RETURN, /* reject */
1210 MOD_ACTION_RETURN, /* ok */
1211 MOD_ACTION_RETURN, /* handled */
1212 MOD_ACTION_RETURN, /* invalid */
1213 MOD_ACTION_RETURN, /* userlock */
1214 MOD_ACTION_RETURN, /* notfound */
1215 MOD_ACTION_RETURN, /* noop */
1216 MOD_ACTION_RETURN /* updated */
1223 MOD_ACTION_RETURN, /* reject */
1224 MOD_ACTION_RETURN, /* fail */
1226 MOD_ACTION_RETURN, /* handled */
1227 MOD_ACTION_RETURN, /* invalid */
1228 MOD_ACTION_RETURN, /* userlock */
1235 MOD_ACTION_RETURN, /* reject */
1237 MOD_ACTION_RETURN, /* ok */
1238 MOD_ACTION_RETURN, /* handled */
1239 MOD_ACTION_RETURN, /* invalid */
1240 MOD_ACTION_RETURN, /* userlock */
1241 MOD_ACTION_RETURN, /* notfound */
1242 MOD_ACTION_RETURN, /* noop */
1243 MOD_ACTION_RETURN /* updated */
1250 MOD_ACTION_RETURN, /* reject */
1251 MOD_ACTION_RETURN, /* fail */
1253 MOD_ACTION_RETURN, /* handled */
1254 MOD_ACTION_RETURN, /* invalid */
1255 MOD_ACTION_RETURN, /* userlock */
1256 MOD_ACTION_RETURN, /* notfound */
1262 MOD_ACTION_RETURN, /* reject */
1264 MOD_ACTION_RETURN, /* ok */
1265 MOD_ACTION_RETURN, /* handled */
1266 MOD_ACTION_RETURN, /* invalid */
1267 MOD_ACTION_RETURN, /* userlock */
1268 MOD_ACTION_RETURN, /* notfound */
1269 MOD_ACTION_RETURN, /* noop */
1270 MOD_ACTION_RETURN /* updated */
1277 MOD_ACTION_RETURN, /* reject */
1278 MOD_ACTION_RETURN, /* fail */
1280 MOD_ACTION_RETURN, /* handled */
1281 MOD_ACTION_RETURN, /* invalid */
1282 MOD_ACTION_RETURN, /* userlock */
1283 MOD_ACTION_RETURN, /* notfound */
1291 MOD_ACTION_RETURN, /* ok */
1292 MOD_ACTION_RETURN, /* handled */
1304 MOD_ACTION_RETURN, /* reject */
1306 MOD_ACTION_RETURN, /* ok */
1307 MOD_ACTION_RETURN, /* handled */
1308 MOD_ACTION_RETURN, /* invalid */
1309 MOD_ACTION_RETURN, /* userlock */
1310 MOD_ACTION_RETURN, /* notfound */
1311 MOD_ACTION_RETURN, /* noop */
1312 MOD_ACTION_RETURN /* updated */
1316 MOD_ACTION_RETURN, /* reject */
1318 MOD_ACTION_RETURN, /* ok */
1319 MOD_ACTION_RETURN, /* handled */
1320 MOD_ACTION_RETURN, /* invalid */
1321 MOD_ACTION_RETURN, /* userlock */
1322 MOD_ACTION_RETURN, /* notfound */
1323 MOD_ACTION_RETURN, /* noop */
1324 MOD_ACTION_RETURN /* updated */
1331 MOD_ACTION_RETURN, /* reject */
1332 MOD_ACTION_RETURN, /* fail */
1334 MOD_ACTION_RETURN, /* handled */
1335 MOD_ACTION_RETURN, /* invalid */
1336 MOD_ACTION_RETURN, /* userlock */
1343 MOD_ACTION_RETURN, /* reject */
1345 MOD_ACTION_RETURN, /* ok */
1346 MOD_ACTION_RETURN, /* handled */
1347 MOD_ACTION_RETURN, /* invalid */
1348 MOD_ACTION_RETURN, /* userlock */
1349 MOD_ACTION_RETURN, /* notfound */
1350 MOD_ACTION_RETURN, /* noop */
1351 MOD_ACTION_RETURN /* updated */
1358 MOD_ACTION_RETURN, /* reject */
1359 MOD_ACTION_RETURN, /* fail */
1361 MOD_ACTION_RETURN, /* handled */
1362 MOD_ACTION_RETURN, /* invalid */
1363 MOD_ACTION_RETURN, /* userlock */
1370 MOD_ACTION_RETURN, /* reject */
1372 MOD_ACTION_RETURN, /* ok */
1373 MOD_ACTION_RETURN, /* handled */
1374 MOD_ACTION_RETURN, /* invalid */
1375 MOD_ACTION_RETURN, /* userlock */
1376 MOD_ACTION_RETURN, /* notfound */
1377 MOD_ACTION_RETURN, /* noop */
1378 MOD_ACTION_RETURN /* updated */
1385 MOD_ACTION_RETURN, /* reject */
1386 MOD_ACTION_RETURN, /* fail */
1388 MOD_ACTION_RETURN, /* handled */
1389 MOD_ACTION_RETURN, /* invalid */
1390 MOD_ACTION_RETURN, /* userlock */
1397 MOD_ACTION_RETURN, /* reject */
1399 MOD_ACTION_RETURN, /* ok */
1400 MOD_ACTION_RETURN, /* handled */
1401 MOD_ACTION_RETURN, /* invalid */
1402 MOD_ACTION_RETURN, /* userlock */
1403 MOD_ACTION_RETURN, /* notfound */
1404 MOD_ACTION_RETURN, /* noop */
1405 MOD_ACTION_RETURN /* updated */
1414 MOD_ACTION_RETURN, /* reject */
1415 MOD_ACTION_RETURN, /* fail */
1417 MOD_ACTION_RETURN, /* handled */
1418 MOD_ACTION_RETURN, /* invalid */
1419 MOD_ACTION_RETURN, /* userlock */
1426 MOD_ACTION_RETURN, /* reject */
1428 MOD_ACTION_RETURN, /* ok */
1429 MOD_ACTION_RETURN, /* handled */
1430 MOD_ACTION_RETURN, /* invalid */
1431 MOD_ACTION_RETURN, /* userlock */
1432 MOD_ACTION_RETURN, /* notfound */
1433 MOD_ACTION_RETURN, /* noop */
1434 MOD_ACTION_RETURN /* updated */
1441 MOD_ACTION_RETURN, /* reject */
1442 MOD_ACTION_RETURN, /* fail */
1444 MOD_ACTION_RETURN, /* handled */
1445 MOD_ACTION_RETURN, /* invalid */
1446 MOD_ACTION_RETURN, /* userlock */
1453 MOD_ACTION_RETURN, /* reject */
1455 MOD_ACTION_RETURN, /* ok */
1456 MOD_ACTION_RETURN, /* handled */
1457 MOD_ACTION_RETURN, /* invalid */
1458 MOD_ACTION_RETURN, /* userlock */
1459 MOD_ACTION_RETURN, /* notfound */
1460 MOD_ACTION_RETURN, /* noop */
1461 MOD_ACTION_RETURN /* updated */
1467 static const int authtype_actions[GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1471 MOD_ACTION_RETURN, /* reject */
1472 MOD_ACTION_RETURN, /* fail */
1474 MOD_ACTION_RETURN, /* handled */
1475 MOD_ACTION_RETURN, /* invalid */
1476 MOD_ACTION_RETURN, /* userlock */
1483 MOD_ACTION_RETURN, /* reject */
1485 MOD_ACTION_RETURN, /* ok */
1486 MOD_ACTION_RETURN, /* handled */
1487 MOD_ACTION_RETURN, /* invalid */
1488 MOD_ACTION_RETURN, /* userlock */
1489 MOD_ACTION_RETURN, /* notfound */
1490 MOD_ACTION_RETURN, /* noop */
1491 MOD_ACTION_RETURN /* updated */
1495 /** Validate and fixup a map that's part of an update section.
1497 * @param map to validate.
1498 * @param ctx data to pass to fixup function (currently unused).
1499 * @return 0 if valid else -1.
1501 int modcall_fixup_update(vp_map_t *map, UNUSED void *ctx)
1503 CONF_PAIR *cp = cf_item_to_pair(map->ci);
1506 * Anal-retentive checks.
1508 if (DEBUG_ENABLED3) {
1509 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->lhs->name[0] != '&')) {
1510 WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
1511 cf_pair_filename(cp), cf_pair_lineno(cp),
1512 map->lhs->name, fr_int2str(fr_tokens, map->op, "<INVALID>"));
1515 if ((map->rhs->type == TMPL_TYPE_ATTR) && (map->rhs->name[0] != '&')) {
1516 WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
1517 cf_pair_filename(cp), cf_pair_lineno(cp),
1518 fr_int2str(fr_tokens, map->op, "<INVALID>"), map->rhs->name);
1523 * Values used by unary operators should be literal ANY
1525 * We then free the template and alloc a NULL one instead.
1527 if (map->op == T_OP_CMP_FALSE) {
1528 if ((map->rhs->type != TMPL_TYPE_LITERAL) || (strcmp(map->rhs->name, "ANY") != 0)) {
1529 WARN("%s[%d] Wildcard deletion MUST use '!* ANY'",
1530 cf_pair_filename(cp), cf_pair_lineno(cp));
1533 TALLOC_FREE(map->rhs);
1535 map->rhs = tmpl_alloc(map, TMPL_TYPE_NULL, NULL, 0);
1539 * Lots of sanity checks for insane people...
1543 * What exactly where you expecting to happen here?
1545 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
1546 (map->rhs->type == TMPL_TYPE_LIST)) {
1547 cf_log_err(map->ci, "Can't copy list into an attribute");
1552 * Depending on the attribute type, some operators are disallowed.
1554 if ((map->lhs->type == TMPL_TYPE_ATTR) && (!fr_assignment_op[map->op] && !fr_equality_op[map->op])) {
1555 cf_log_err(map->ci, "Invalid operator \"%s\" in update section. "
1556 "Only assignment or filter operators are allowed",
1557 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1561 if (map->lhs->type == TMPL_TYPE_LIST) {
1563 * Can't copy an xlat expansion or literal into a list,
1564 * we don't know what type of attribute we'd need
1567 * The only exception is where were using a unary
1570 if (map->op != T_OP_CMP_FALSE) switch (map->rhs->type) {
1571 case TMPL_TYPE_XLAT:
1572 case TMPL_TYPE_LITERAL:
1573 cf_log_err(map->ci, "Can't copy value into list (we don't know which attribute to create)");
1581 * Only += and :=, and !* operators are supported
1585 case T_OP_CMP_FALSE:
1589 if ((map->rhs->type != TMPL_TYPE_LIST) &&
1590 (map->rhs->type != TMPL_TYPE_EXEC)) {
1591 cf_log_err(map->ci, "Invalid source for list assignment '%s += ...'", map->lhs->name);
1597 if (map->rhs->type == TMPL_TYPE_EXEC) {
1598 WARN("%s[%d]: Please change ':=' to '=' for list assignment",
1599 cf_pair_filename(cp), cf_pair_lineno(cp));
1602 if (map->rhs->type != TMPL_TYPE_LIST) {
1603 cf_log_err(map->ci, "Invalid source for list assignment '%s := ...'", map->lhs->name);
1609 if (map->rhs->type != TMPL_TYPE_EXEC) {
1610 cf_log_err(map->ci, "Invalid source for list assignment '%s = ...'", map->lhs->name);
1616 cf_log_err(map->ci, "Operator \"%s\" not allowed for list assignment",
1617 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1623 * If the map has a unary operator there's no further
1624 * processing we need to, as RHS is unused.
1626 if (map->op == T_OP_CMP_FALSE) return 0;
1629 * If LHS is an attribute, and RHS is a literal, we can
1630 * preparse the information into a TMPL_TYPE_DATA.
1632 * Unless it's a unary operator in which case we
1635 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->rhs->type == TMPL_TYPE_LITERAL)) {
1637 * It's a literal string, just copy it.
1638 * Don't escape anything.
1640 if (!cf_new_escape &&
1641 (map->lhs->tmpl_da->type == PW_TYPE_STRING) &&
1642 (cf_pair_value_type(cp) == T_SINGLE_QUOTED_STRING)) {
1643 tmpl_cast_in_place_str(map->rhs);
1647 * RHS is hex, try to parse it as
1648 * type-specific data.
1650 if (map->lhs->auto_converted &&
1651 (map->rhs->name[0] == '0') && (map->rhs->name[1] == 'x') &&
1652 (map->rhs->len > 2) && ((map->rhs->len & 0x01) == 0)) {
1653 vp_tmpl_t *vpt = map->rhs;
1656 if (!map_cast_from_hex(map, T_BARE_WORD, vpt->name)) {
1658 cf_log_err(map->ci, "%s", fr_strerror());
1663 } else if (tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da) < 0) {
1664 cf_log_err(map->ci, "%s", fr_strerror());
1669 * Fixup LHS da if it doesn't match the type
1672 if (map->lhs->tmpl_da->type != map->rhs->tmpl_data_type) {
1673 DICT_ATTR const *da;
1675 da = dict_attrbytype(map->lhs->tmpl_da->attr, map->lhs->tmpl_da->vendor,
1676 map->rhs->tmpl_data_type);
1678 fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1679 fr_int2str(dict_attr_types, map->rhs->tmpl_data_type,
1680 "<INVALID>"), map->lhs->tmpl_da->name);
1683 map->lhs->tmpl_da = da;
1686 } /* else we can't precompile the data */
1693 static modcallable *do_compile_modupdate(modcallable *parent, rlm_components_t component,
1694 CONF_SECTION *cs, char const *name2)
1698 modcallable *csingle;
1703 * This looks at cs->name2 to determine which list to update
1705 rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
1706 if (rcode < 0) return NULL; /* message already printed */
1708 cf_log_err_cs(cs, "'update' sections cannot be empty");
1712 g = talloc_zero(parent, modgroup);
1713 csingle = mod_grouptocallable(g);
1715 csingle->parent = parent;
1716 csingle->next = NULL;
1719 csingle->name = name2;
1723 csingle->type = MOD_UPDATE;
1724 csingle->method = component;
1726 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
1727 sizeof(csingle->actions));
1729 g->grouptype = GROUPTYPE_SIMPLE;
1732 g->map = talloc_steal(g, head);
1738 static modcallable *do_compile_modswitch (modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1743 bool had_seen_default = false;
1744 modcallable *csingle;
1749 name2 = cf_section_name2(cs);
1751 cf_log_err_cs(cs, "You must specify a variable to switch over for 'switch'");
1755 if (!cf_item_find_next(cs, NULL)) {
1756 cf_log_err_cs(cs, "'switch' statements cannot be empty");
1761 * Create the template. If we fail, AND it's a bare word
1762 * with &Foo-Bar, it MAY be an attribute defined by a
1763 * module. Allow it for now. The pass2 checks below
1766 type = cf_section_name2_type(cs);
1767 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1768 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1769 char *spaces, *text;
1771 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1773 cf_log_err_cs(cs, "Syntax error");
1774 cf_log_err_cs(cs, "%s", name2);
1775 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1777 talloc_free(spaces);
1784 * Otherwise a NULL vpt may refer to an attribute defined
1785 * by a module. That is checked in pass 2.
1788 if (vpt->type == TMPL_TYPE_LIST) {
1789 cf_log_err_cs(cs, "Syntax error: Cannot switch over list '%s'", name2);
1795 * Walk through the children of the switch section,
1796 * ensuring that they're all 'case' statements
1798 for (ci = cf_item_find_next(cs, NULL);
1800 ci = cf_item_find_next(cs, ci)) {
1801 CONF_SECTION *subcs;
1804 if (!cf_item_is_section(ci)) {
1805 if (!cf_item_is_pair(ci)) continue;
1807 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1812 subcs = cf_item_to_section(ci); /* can't return NULL */
1813 name1 = cf_section_name1(subcs);
1815 if (strcmp(name1, "case") != 0) {
1816 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1821 name2 = cf_section_name2(subcs);
1823 if (!had_seen_default) {
1824 had_seen_default = true;
1828 cf_log_err(ci, "Cannot have two 'default' case statements");
1834 csingle = do_compile_modgroup(parent, component, cs,
1843 g = mod_callabletogroup(csingle);
1844 g->vpt = talloc_steal(g, vpt);
1849 static modcallable *do_compile_modcase(modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1853 modcallable *csingle;
1857 if (!parent || (parent->type != MOD_SWITCH)) {
1858 cf_log_err_cs(cs, "\"case\" statements may only appear within a \"switch\" section");
1863 * case THING means "match THING"
1864 * case means "match anything"
1866 name2 = cf_section_name2(cs);
1871 type = cf_section_name2_type(cs);
1873 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1874 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1875 char *spaces, *text;
1877 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1879 cf_log_err_cs(cs, "Syntax error");
1880 cf_log_err_cs(cs, "%s", name2);
1881 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1883 talloc_free(spaces);
1889 if (vpt->type == TMPL_TYPE_LIST) {
1890 cf_log_err_cs(cs, "Syntax error: Cannot match list '%s'", name2);
1895 * Otherwise a NULL vpt may refer to an attribute defined
1896 * by a module. That is checked in pass 2.
1903 csingle = do_compile_modgroup(parent, component, cs,
1913 * The interpretor expects this to be NULL for the
1914 * default case. do_compile_modgroup sets it to name2,
1915 * unless name2 is NULL, in which case it sets it to name1.
1917 csingle->name = name2;
1919 g = mod_callabletogroup(csingle);
1920 g->vpt = talloc_steal(g, vpt);
1923 * Set all of it's codes to return, so that
1924 * when we pick a 'case' statement, we don't
1925 * fall through to processing the next one.
1927 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1928 csingle->actions[i] = MOD_ACTION_RETURN;
1934 static modcallable *do_compile_modforeach(modcallable *parent,
1935 rlm_components_t component, CONF_SECTION *cs)
1939 modcallable *csingle;
1944 name2 = cf_section_name2(cs);
1947 "You must specify an attribute to loop over in 'foreach'");
1951 if (!cf_item_find_next(cs, NULL)) {
1952 cf_log_err_cs(cs, "'foreach' blocks cannot be empty");
1957 * Create the template. If we fail, AND it's a bare word
1958 * with &Foo-Bar, it MAY be an attribute defined by a
1959 * module. Allow it for now. The pass2 checks below
1962 type = cf_section_name2_type(cs);
1963 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1964 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1965 char *spaces, *text;
1967 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1969 cf_log_err_cs(cs, "Syntax error");
1970 cf_log_err_cs(cs, "%s", name2);
1971 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1973 talloc_free(spaces);
1980 * If we don't have a negative return code, we must have a vpt
1981 * (mostly to quiet coverity).
1985 if ((vpt->type != TMPL_TYPE_ATTR) && (vpt->type != TMPL_TYPE_LIST)) {
1986 cf_log_err_cs(cs, "MUST use attribute or list reference in 'foreach'");
1991 * Fix up the template to iterate over all instances of
1992 * the attribute. In a perfect consistent world, users would do
1993 * foreach &attr[*], but that's taking the consistency thing a bit far.
1995 vpt->tmpl_num = NUM_ALL;
1997 csingle = do_compile_modgroup(parent, component, cs,
1998 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2006 g = mod_callabletogroup(csingle);
2012 static modcallable *do_compile_modbreak(modcallable *parent,
2013 rlm_components_t component, CONF_ITEM const *ci)
2015 CONF_SECTION const *cs = NULL;
2017 for (cs = cf_item_parent(ci);
2019 cs = cf_item_parent(cf_section_to_item(cs))) {
2020 if (strcmp(cf_section_name1(cs), "foreach") == 0) {
2026 cf_log_err(ci, "'break' can only be used in a 'foreach' section");
2030 return do_compile_modgroup(parent, component, NULL,
2031 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2036 static modcallable *do_compile_modserver(modcallable *parent,
2037 rlm_components_t component, CONF_ITEM *ci,
2042 modcallable *csingle;
2043 CONF_SECTION *subcs;
2046 subcs = cf_section_sub_find_name2(cs, comp2str[component], NULL);
2048 cf_log_err(ci, "Server %s has no %s section",
2049 server, comp2str[component]);
2053 mr = talloc_zero(parent, modref);
2055 csingle = mod_reftocallable(mr);
2056 csingle->parent = parent;
2057 csingle->next = NULL;
2058 csingle->name = name;
2059 csingle->type = MOD_REFERENCE;
2060 csingle->method = component;
2062 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2063 sizeof(csingle->actions));
2065 mr->ref_name = strdup(server);
2071 static modcallable *do_compile_modxlat(modcallable *parent,
2072 rlm_components_t component, char const *fmt)
2074 modcallable *csingle;
2077 mx = talloc_zero(parent, modxlat);
2079 csingle = mod_xlattocallable(mx);
2080 csingle->parent = parent;
2081 csingle->next = NULL;
2082 csingle->name = "expand";
2083 csingle->type = MOD_XLAT;
2084 csingle->method = component;
2086 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2087 sizeof(csingle->actions));
2089 mx->xlat_name = strdup(fmt);
2090 if (fmt[0] != '%') {
2094 strcpy(mx->xlat_name, fmt + 1);
2095 p = strrchr(mx->xlat_name, '`');
2103 * redundant, etc. can refer to modules or groups, but not much else.
2105 static int all_children_are_modules(CONF_SECTION *cs, char const *name)
2109 for (ci=cf_item_find_next(cs, NULL);
2111 ci=cf_item_find_next(cs, ci)) {
2113 * If we're a redundant, etc. group, then the
2114 * intention is to call modules, rather than
2115 * processing logic. These checks aren't
2116 * *strictly* necessary, but they keep the users
2117 * from doing crazy things.
2119 if (cf_item_is_section(ci)) {
2120 CONF_SECTION *subcs = cf_item_to_section(ci);
2121 char const *name1 = cf_section_name1(subcs);
2123 if ((strcmp(name1, "if") == 0) ||
2124 (strcmp(name1, "else") == 0) ||
2125 (strcmp(name1, "elsif") == 0) ||
2126 (strcmp(name1, "update") == 0) ||
2127 (strcmp(name1, "switch") == 0) ||
2128 (strcmp(name1, "case") == 0)) {
2129 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
2136 if (cf_item_is_pair(ci)) {
2137 CONF_PAIR *cp = cf_item_to_pair(ci);
2138 if (cf_pair_value(cp) != NULL) {
2140 "Entry with no value is invalid");
2151 * Load a named module from "instantiate" or "policy".
2153 * If it's "foo.method", look for "foo", and return "method" as the method
2154 * we wish to use, instead of the input component.
2156 static CONF_SECTION *virtual_module_find_cs(char const *virtual_name, char const *method_name,
2157 rlm_components_t *pcomponent)
2159 CONF_SECTION *cs, *subcs;
2160 rlm_components_t method = *pcomponent;
2164 * Turn the method name into a method enum.
2169 for (i = MOD_AUTHENTICATE; i < MOD_COUNT; i++) {
2170 if (strcmp(comp2str[i], method_name) == 0) break;
2173 if (i == MOD_COUNT) return NULL;
2179 * Look for "foo" in the "instantiate" section. If we
2180 * find it, AND there's no method name, we've found the
2183 * Return it to the caller, with the updated method.
2185 cs = cf_section_find("instantiate");
2188 * Found "foo". Load it as "foo", or "foo.method".
2190 subcs = cf_section_sub_find_name2(cs, NULL, virtual_name);
2192 *pcomponent = method;
2198 * Look for it in "policy".
2200 * If there's no policy section, we can't do anything else.
2202 cs = cf_section_find("policy");
2203 if (!cs) return NULL;
2206 * "foo.authorize" means "load policy "foo" as method "authorize".
2208 * And bail out if there's no policy "foo".
2211 subcs = cf_section_sub_find_name2(cs, NULL, virtual_name);
2212 if (subcs) *pcomponent = method;
2218 * "foo" means "look for foo.component" first, to allow
2219 * method overrides. If that's not found, just look for
2223 snprintf(buffer, sizeof(buffer), "%s.%s",
2224 virtual_name, comp2str[method]);
2225 subcs = cf_section_sub_find_name2(cs, NULL, buffer);
2226 if (subcs) return subcs;
2228 return cf_section_sub_find_name2(cs, NULL, virtual_name);
2233 * Compile one entry of a module call.
2235 static modcallable *do_compile_modsingle(modcallable *parent,
2236 rlm_components_t component, CONF_ITEM *ci,
2238 char const **modname)
2240 char const *modrefname, *p;
2242 modcallable *csingle;
2243 module_instance_t *this;
2244 CONF_SECTION *cs, *subcs, *modules;
2246 char const *realname;
2247 rlm_components_t method = component;
2249 if (cf_item_is_section(ci)) {
2252 cs = cf_item_to_section(ci);
2253 modrefname = cf_section_name1(cs);
2254 name2 = cf_section_name2(cs);
2255 if (!name2) name2 = "";
2258 * group{}, redundant{}, or append{} may appear
2259 * where a single module instance was expected.
2260 * In that case, we hand it off to
2263 if (strcmp(modrefname, "group") == 0) {
2265 return do_compile_modgroup(parent, component, cs,
2267 grouptype, MOD_GROUP);
2269 } else if (strcmp(modrefname, "redundant") == 0) {
2272 if (!all_children_are_modules(cs, modrefname)) {
2276 return do_compile_modgroup(parent, component, cs,
2277 GROUPTYPE_REDUNDANT,
2278 grouptype, MOD_GROUP);
2280 } else if (strcmp(modrefname, "load-balance") == 0) {
2283 if (!all_children_are_modules(cs, modrefname)) {
2287 return do_compile_modgroup(parent, component, cs,
2289 grouptype, MOD_LOAD_BALANCE);
2291 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2294 if (!all_children_are_modules(cs, modrefname)) {
2298 return do_compile_modgroup(parent, component, cs,
2299 GROUPTYPE_REDUNDANT,
2300 grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2303 } else if (strcmp(modrefname, "if") == 0) {
2304 if (!cf_section_name2(cs)) {
2305 cf_log_err(ci, "'if' without condition");
2310 csingle= do_compile_modgroup(parent, component, cs,
2313 if (!csingle) return NULL;
2318 } else if (strcmp(modrefname, "elsif") == 0) {
2320 ((parent->type == MOD_LOAD_BALANCE) ||
2321 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2322 cf_log_err(ci, "'elsif' cannot be used in this section");
2326 if (!cf_section_name2(cs)) {
2327 cf_log_err(ci, "'elsif' without condition");
2332 return do_compile_modgroup(parent, component, cs,
2334 grouptype, MOD_ELSIF);
2336 } else if (strcmp(modrefname, "else") == 0) {
2338 ((parent->type == MOD_LOAD_BALANCE) ||
2339 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2340 cf_log_err(ci, "'else' cannot be used in this section section");
2344 if (cf_section_name2(cs)) {
2345 cf_log_err(ci, "Cannot have conditions on 'else'");
2350 return do_compile_modgroup(parent, component, cs,
2352 grouptype, MOD_ELSE);
2354 } else if (strcmp(modrefname, "update") == 0) {
2357 return do_compile_modupdate(parent, component, cs,
2360 } else if (strcmp(modrefname, "switch") == 0) {
2363 return do_compile_modswitch (parent, component, cs);
2365 } else if (strcmp(modrefname, "case") == 0) {
2368 return do_compile_modcase(parent, component, cs);
2370 } else if (strcmp(modrefname, "foreach") == 0) {
2373 return do_compile_modforeach(parent, component, cs);
2376 } /* else it's something like sql { fail = 1 ...} */
2378 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2382 * Else it's a module reference, with updated return
2386 CONF_PAIR *cp = cf_item_to_pair(ci);
2387 modrefname = cf_pair_attr(cp);
2390 * Actions (ok = 1), etc. are orthogonal to just
2391 * about everything else.
2393 if (cf_pair_value(cp) != NULL) {
2394 cf_log_err(ci, "Entry is not a reference to a module");
2399 * In-place xlat's via %{...}.
2401 * This should really be removed from the server.
2403 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2404 (modrefname[0] == '`')) {
2405 return do_compile_modxlat(parent, component,
2412 * These can't be over-ridden.
2414 if (strcmp(modrefname, "break") == 0) {
2415 if (!cf_item_is_pair(ci)) {
2416 cf_log_err(ci, "Invalid use of 'break' as section name.");
2420 return do_compile_modbreak(parent, component, ci);
2423 if (strcmp(modrefname, "return") == 0) {
2424 if (!cf_item_is_pair(ci)) {
2425 cf_log_err(ci, "Invalid use of 'return' as section name.");
2429 return do_compile_modgroup(parent, component, NULL,
2430 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2436 * Run a virtual server. This is really terrible and
2437 * should be deleted.
2439 if (strncmp(modrefname, "server[", 7) == 0) {
2442 if (!cf_item_is_pair(ci)) {
2443 cf_log_err(ci, "Invalid syntax");
2447 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2448 p = strrchr(buffer, ']');
2449 if (!p || p[1] != '\0' || (p == buffer)) {
2450 cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2454 buffer[p - buffer] = '\0';
2456 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2458 cf_log_err(ci, "No such server \"%s\".", buffer);
2463 * Ignore stupid attempts to over-ride the return
2466 return do_compile_modserver(parent, component, ci,
2467 modrefname, cs, buffer);
2471 * We now have a name. It can be one of two forms. A
2472 * bare module name, or a section named for the module,
2473 * with over-rides for the return codes.
2475 * The name can refer to a real module, in the "modules"
2476 * section. In that case, the name will be either the
2477 * first or second name of the sub-section of "modules".
2479 * Or, the name can refer to a policy, in the "policy"
2480 * section. In that case, the name will be first name of
2481 * the sub-section of "policy". Unless it's a "redudant"
2484 * Or, the name can refer to a "module.method", in which
2485 * case we're calling a different method than normal for
2488 * Or, the name can refer to a virtual module, in the
2489 * "instantiate" section. In that case, the name will be
2490 * the first of the sub-section of "instantiate". Unless
2491 * it's a "redudant" block...
2493 * We try these in sequence, from the bottom up. This is
2494 * so that things in "instantiate" and "policy" can
2495 * over-ride calls to real modules.
2502 * instantiate { ... name { ...} ... }
2503 * instantiate { ... name.method { ...} ... }
2504 * policy { ... name { .. } .. }
2505 * policy { ... name.method { .. } .. }
2507 * The only difference between things in "instantiate"
2508 * and "policy" is that "instantiate" will cause modules
2509 * to be instantiated in a particular order.
2512 p = strrchr(modrefname, '.');
2514 subcs = virtual_module_find_cs(modrefname, NULL, &method);
2518 strlcpy(buffer, modrefname, sizeof(buffer));
2519 buffer[p - modrefname] = '\0';
2521 subcs = virtual_module_find_cs(buffer, buffer + (p - modrefname) + 1, &method);
2525 * Check that we're not creating a loop. We may
2526 * be compiling an "sql" module reference inside
2527 * of an "sql" policy. If so, we allow the
2528 * second "sql" to refer to the module.
2530 for (loop = cf_item_parent(ci);
2532 loop = cf_item_parent(cf_section_to_item(loop))) {
2533 if (loop == subcs) {
2539 * We've found the relevant entry. It MUST be a
2542 * However, it can be a "redundant" block, or just a
2547 * modules.c takes care of ensuring that this is:
2550 * load-balance foo { ...
2551 * redundant foo { ...
2552 * redundant-load-balance foo { ...
2554 * We can just recurs to compile the section as
2555 * if it was found here.
2557 if (cf_section_name2(subcs)) {
2558 csingle = do_compile_modsingle(parent,
2560 cf_section_to_item(subcs),
2569 * So we compile it like it was:
2573 csingle = do_compile_modgroup(parent,
2577 grouptype, MOD_GROUP);
2581 * Return the compiled thing if we can.
2583 if (!csingle) return NULL;
2584 if (cf_item_is_pair(ci)) return csingle;
2587 * Else we have a reference to a policy, and that reference
2588 * over-rides the return codes for the policy!
2590 goto action_override;
2594 * Not a virtual module. It must be a real module.
2596 modules = cf_section_find("modules");
2598 realname = modrefname;
2602 * Try to load the optional module.
2604 if (realname[0] == '-') realname++;
2607 * As of v3, the "modules" section contains
2608 * modules we use. Configuration for other
2609 * modules belongs in raddb/mods-available/,
2610 * which isn't loaded into the "modules" section.
2612 this = module_instantiate_method(modules, realname, &method);
2613 if (this) goto allocate_csingle;
2616 * We were asked to MAYBE load it and it
2617 * doesn't exist. Return a soft error.
2619 if (realname != modrefname) {
2620 *modname = modrefname;
2626 * Can't de-reference it to anything. Ugh.
2629 cf_log_err(ci, "Failed to find \"%s\" as a module or policy.", modrefname);
2630 cf_log_err(ci, "Please verify that the configuration exists in %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2634 * We know it's all OK, allocate the structures, and fill
2639 * Check if the module in question has the necessary
2642 if (!this->entry->module->methods[method]) {
2643 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2648 single = talloc_zero(parent, modsingle);
2649 single->modinst = this;
2650 *modname = this->entry->module->name;
2652 csingle = mod_singletocallable(single);
2653 csingle->parent = parent;
2654 csingle->next = NULL;
2655 if (!parent || (component != MOD_AUTHENTICATE)) {
2656 memcpy(csingle->actions, defaultactions[component][grouptype],
2657 sizeof csingle->actions);
2658 } else { /* inside Auth-Type has different rules */
2659 memcpy(csingle->actions, authtype_actions[grouptype],
2660 sizeof csingle->actions);
2662 rad_assert(modrefname != NULL);
2663 csingle->name = realname;
2664 csingle->type = MOD_SINGLE;
2665 csingle->method = method;
2669 * Over-ride the default return codes of the module.
2671 if (cf_item_is_section(ci)) {
2674 cs = cf_item_to_section(ci);
2675 for (csi=cf_item_find_next(cs, NULL);
2677 csi=cf_item_find_next(cs, csi)) {
2679 if (cf_item_is_section(csi)) {
2680 cf_log_err(csi, "Subsection of module instance call not allowed");
2681 talloc_free(csingle);
2685 if (!cf_item_is_pair(csi)) continue;
2687 if (!compile_action(csingle, cf_item_to_pair(csi))) {
2688 talloc_free(csingle);
2697 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2698 modcallable **parent,
2699 rlm_components_t component, CONF_ITEM *ci,
2700 char const **modname)
2707 CONF_SECTION *parentcs;
2709 g = talloc_zero(ctx, modgroup);
2710 memset(g, 0, sizeof(*g));
2711 g->grouptype = GROUPTYPE_SIMPLE;
2712 c = mod_grouptocallable(g);
2715 defaultactions[component][GROUPTYPE_SIMPLE],
2716 sizeof(c->actions));
2718 parentcs = cf_item_parent(ci);
2719 c->name = cf_section_name2(parentcs);
2721 c->name = cf_section_name1(parentcs);
2724 c->type = MOD_GROUP;
2725 c->method = component;
2728 *parent = mod_grouptocallable(g);
2731 ret = do_compile_modsingle(*parent, component, ci,
2734 dump_tree(component, ret);
2740 * Internal compile group code.
2742 static modcallable *do_compile_modgroup(modcallable *parent,
2743 rlm_components_t component, CONF_SECTION *cs,
2744 int grouptype, int parentgrouptype, int mod_type)
2751 g = talloc_zero(parent, modgroup);
2752 g->grouptype = grouptype;
2756 c = mod_grouptocallable(g);
2760 memset(c->actions, 0, sizeof(c->actions));
2762 if (!cs) { /* only for "break" and "return" */
2768 * Remember the name for printing, etc.
2770 * FIXME: We may also want to put the names into a
2771 * rbtree, so that groups can reference each other...
2773 c->name = cf_section_name2(cs);
2775 c->name = cf_section_name1(cs);
2776 if ((strcmp(c->name, "group") == 0) ||
2777 (strcmp(c->name, "redundant") == 0)) {
2779 } else if (c->type == MOD_GROUP) {
2780 c->type = MOD_POLICY;
2786 * Do load-time optimizations
2788 if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2791 rad_assert(parent != NULL);
2793 if (c->type == MOD_IF) {
2794 g->cond = cf_data_find(g->cs, "if");
2795 rad_assert(g->cond != NULL);
2798 if (g->cond->type == COND_TYPE_FALSE) {
2799 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2800 unlang_keyword[g->mc.type],
2801 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2805 } else if (c->type == MOD_ELSIF) {
2807 g->cond = cf_data_find(g->cs, "if");
2808 rad_assert(g->cond != NULL);
2810 rad_assert(parent != NULL);
2811 p = mod_callabletogroup(parent);
2813 rad_assert(p->tail != NULL);
2816 * We're in the process of compiling the
2817 * section, so the parent's tail is the
2818 * previous "if" statement.
2820 f = mod_callabletogroup(p->tail);
2821 if ((f->mc.type != MOD_IF) &&
2822 (f->mc.type != MOD_ELSIF)) {
2823 cf_log_err_cs(g->cs, "Invalid location for 'elsif'. There is no preceding 'if' statement");
2829 * If we took the previous condition, we
2830 * don't need to take this one.
2832 * We reset our condition to 'true', so
2833 * that subsequent sections can check
2834 * that they don't need to be executed.
2836 if (f->cond->type == COND_TYPE_TRUE) {
2838 INFO(" # Skipping contents of '%s' as previous '%s' is always 'true' -- %s:%d",
2839 unlang_keyword[g->mc.type],
2840 unlang_keyword[f->mc.type],
2841 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2848 rad_assert(c->type == MOD_ELSE);
2850 rad_assert(parent != NULL);
2851 p = mod_callabletogroup(parent);
2853 rad_assert(p->tail != NULL);
2855 f = mod_callabletogroup(p->tail);
2856 if ((f->mc.type != MOD_IF) &&
2857 (f->mc.type != MOD_ELSIF)) {
2858 cf_log_err_cs(g->cs, "Invalid location for 'else'. There is no preceding 'if' statement");
2864 * If we took the previous condition, we
2865 * don't need to take this one.
2867 if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2871 * Else we need to compile this section
2877 * Loop over the children of this group.
2879 for (ci=cf_item_find_next(cs, NULL);
2881 ci=cf_item_find_next(cs, ci)) {
2884 * Sections are references to other groups, or
2885 * to modules with updated return codes.
2887 if (cf_item_is_section(ci)) {
2888 char const *junk = NULL;
2889 modcallable *single;
2890 CONF_SECTION *subcs = cf_item_to_section(ci);
2892 single = do_compile_modsingle(c, component, ci,
2895 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2896 cf_section_name1(subcs));
2900 add_child(g, single);
2902 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2906 char const *attr, *value;
2907 CONF_PAIR *cp = cf_item_to_pair(ci);
2909 attr = cf_pair_attr(cp);
2910 value = cf_pair_value(cp);
2913 * A CONF_PAIR is either a module
2914 * instance with no actions
2918 modcallable *single;
2919 char const *junk = NULL;
2921 single = do_compile_modsingle(c,
2927 if (cf_item_is_pair(ci) &&
2928 cf_pair_attr(cf_item_to_pair(ci))[0] == '-') {
2933 "Failed to parse \"%s\" entry.",
2938 add_child(g, single);
2941 * Or a module instance with action.
2943 } else if (!compile_action(c, cp)) {
2946 } /* else it worked */
2952 * Set the default actions, if they haven't already been
2955 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2956 if (!c->actions[i]) {
2957 if (!parent || (component != MOD_AUTHENTICATE)) {
2958 c->actions[i] = defaultactions[component][parentgrouptype][i];
2959 } else { /* inside Auth-Type has different rules */
2960 c->actions[i] = authtype_actions[parentgrouptype][i];
2970 if (grouptype != GROUPTYPE_REDUNDANT) break;
2973 case MOD_LOAD_BALANCE:
2974 case MOD_REDUNDANT_LOAD_BALANCE:
2976 cf_log_err_cs(g->cs, "%s sections cannot be empty",
2977 cf_section_name1(g->cs));
2984 * FIXME: If there are no children, return NULL?
2986 return mod_grouptocallable(g);
2989 modcallable *compile_modgroup(modcallable *parent,
2990 rlm_components_t component, CONF_SECTION *cs)
2992 modcallable *ret = do_compile_modgroup(parent, component, cs,
2994 GROUPTYPE_SIMPLE, MOD_GROUP);
2996 if (rad_debug_lvl > 3) {
2997 modcall_debug(ret, 2);
3003 void add_to_modcallable(modcallable *parent, modcallable *this)
3007 rad_assert(this != NULL);
3008 rad_assert(parent != NULL);
3010 g = mod_callabletogroup(parent);
3017 static bool pass2_xlat_compile(CONF_ITEM const *ci, vp_tmpl_t **pvpt, bool convert,
3018 DICT_ATTR const *da)
3028 rad_assert(vpt->type == TMPL_TYPE_XLAT);
3030 fmt = talloc_typed_strdup(vpt, vpt->name);
3031 slen = xlat_tokenize(vpt, fmt, &head, &error);
3034 char *spaces, *text;
3036 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3038 cf_log_err(ci, "Failed parsing expanded string:");
3039 cf_log_err(ci, "%s", text);
3040 cf_log_err(ci, "%s^ %s", spaces, error);
3042 talloc_free(spaces);
3048 * Convert %{Attribute-Name} to &Attribute-Name
3053 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
3056 * If it's a virtual attribute, leave it
3059 if (attr->tmpl_da->flags.virtual) {
3065 * If the attribute is of incompatible
3066 * type, leave it alone.
3068 if (da && (da->type != attr->tmpl_da->type)) {
3073 if (cf_item_is_pair(ci)) {
3074 CONF_PAIR *cp = cf_item_to_pair(ci);
3076 WARN("%s[%d]: Please change \"%%{%s}\" to &%s",
3077 cf_pair_filename(cp), cf_pair_lineno(cp),
3078 attr->name, attr->name);
3080 CONF_SECTION *cs = cf_item_to_section(ci);
3082 WARN("%s[%d]: Please change \"%%{%s}\" to &%s",
3083 cf_section_filename(cs), cf_section_lineno(cs),
3084 attr->name, attr->name);
3093 * Re-write it to be a pre-parsed XLAT structure.
3095 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3096 vpt->tmpl_xlat = head;
3103 static bool pass2_regex_compile(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3108 rad_assert(vpt->type == TMPL_TYPE_REGEX);
3111 * It's a dynamic expansion. We can't expand the string,
3112 * but we can pre-parse it as an xlat struct. In that
3113 * case, we convert it to a pre-compiled XLAT.
3115 * This is a little more complicated than it needs to be
3116 * because radius_evaluate_map() keys off of the src
3117 * template type, instead of the operators. And, the
3118 * pass2_xlat_compile() function expects to get passed an
3119 * XLAT instead of a REGEX.
3121 if (strchr(vpt->name, '%')) {
3122 vpt->type = TMPL_TYPE_XLAT;
3123 return pass2_xlat_compile(ci, &vpt, false, NULL);
3126 slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3127 vpt->tmpl_iflag, vpt->tmpl_mflag, true, false);
3129 char *spaces, *text;
3131 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3133 cf_log_err(ci, "Invalid regular expression:");
3134 cf_log_err(ci, "%s", text);
3135 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3137 talloc_free(spaces);
3143 vpt->type = TMPL_TYPE_REGEX_STRUCT;
3144 vpt->tmpl_preg = preg;
3150 static bool pass2_fixup_undefined(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3152 DICT_ATTR const *da;
3154 rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3156 da = dict_attrbyname(vpt->tmpl_unknown_name);
3158 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3163 vpt->type = TMPL_TYPE_ATTR;
3167 static bool pass2_callback(void *ctx, fr_cond_t *c)
3173 * These don't get optimized.
3175 if ((c->type == COND_TYPE_TRUE) ||
3176 (c->type == COND_TYPE_FALSE)) {
3183 if (c->type == COND_TYPE_CHILD) return pass2_callback(ctx, c->data.child);
3186 * A few simple checks here.
3188 if (c->type == COND_TYPE_EXISTS) {
3189 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3190 return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3193 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3196 * The existence check might have been &Foo-Bar,
3197 * where Foo-Bar is defined by a module.
3199 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3200 if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3201 c->pass2_fixup = PASS2_FIXUP_NONE;
3207 * And tons of complicated checks.
3209 rad_assert(c->type == COND_TYPE_MAP);
3211 map = c->data.map; /* shorter */
3216 * Where "foo" is dynamically defined.
3218 if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3219 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3220 map->lhs->tmpl_da->vendor,
3222 cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3223 map->lhs->tmpl_da->name,
3229 * These guys can't have a paircompare fixup applied.
3231 c->pass2_fixup = PASS2_FIXUP_NONE;
3235 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3236 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3237 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3240 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3241 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3244 c->pass2_fixup = PASS2_FIXUP_NONE;
3248 * Just in case someone adds a new fixup later.
3250 rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3251 (c->pass2_fixup == PASS2_PAIRCOMPARE));
3256 if (map->lhs->type == TMPL_TYPE_XLAT) {
3258 * Compile the LHS to an attribute reference only
3259 * if the RHS is a literal.
3261 * @todo v3.1: allow anything anywhere.
3263 if (map->rhs->type != TMPL_TYPE_LITERAL) {
3264 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3268 if (!pass2_xlat_compile(map->ci, &map->lhs, true, NULL)) {
3273 * Attribute compared to a literal gets
3274 * the literal cast to the data type of
3277 * The code in parser.c did this for
3281 * But now we've just converted "%{Attr}"
3282 * to &Attr, so we've got to do it again.
3284 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
3285 (map->rhs->type == TMPL_TYPE_LITERAL)) {
3287 * RHS is hex, try to parse it as
3288 * type-specific data.
3290 if (map->lhs->auto_converted &&
3291 (map->rhs->name[0] == '0') && (map->rhs->name[1] == 'x') &&
3292 (map->rhs->len > 2) && ((map->rhs->len & 0x01) == 0)) {
3296 if (!map_cast_from_hex(map, T_BARE_WORD, vpt->name)) {
3298 cf_log_err(map->ci, "%s", fr_strerror());
3303 } else if ((map->rhs->len > 0) ||
3304 (map->op != T_OP_CMP_EQ) ||
3305 (map->lhs->tmpl_da->type == PW_TYPE_STRING) ||
3306 (map->lhs->tmpl_da->type == PW_TYPE_OCTETS)) {
3308 if (tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da) < 0) {
3309 cf_log_err(map->ci, "Failed to parse data type %s from string: %s",
3310 fr_int2str(dict_attr_types, map->lhs->tmpl_da->type, "<UNKNOWN>"),
3313 } /* else the cast was successful */
3315 } else { /* RHS is empty, it's just a check for empty / non-empty string */
3316 vpt = talloc_steal(c, map->lhs);
3318 talloc_free(c->data.map);
3321 * "%{Foo}" == '' ---> !Foo
3322 * "%{Foo}" != '' ---> Foo
3324 c->type = COND_TYPE_EXISTS;
3326 c->negate = !c->negate;
3328 WARN("%s[%d]: Please change (\"%%{%s}\" %s '') to %c&%s",
3329 cf_section_filename(cf_item_to_section(c->ci)),
3330 cf_section_lineno(cf_item_to_section(c->ci)),
3331 vpt->name, c->negate ? "==" : "!=",
3332 c->negate ? '!' : ' ', vpt->name);
3335 * No more RHS, so we can't do more optimizations
3343 if (map->rhs->type == TMPL_TYPE_XLAT) {
3345 * Convert the RHS to an attribute reference only
3346 * if the LHS is an attribute reference, AND is
3347 * of the same type as the RHS.
3349 * We can fix this when the code in evaluate.c
3350 * can handle strings on the LHS, and attributes
3351 * on the RHS. For now, the code in parser.c
3354 if (map->lhs->type == TMPL_TYPE_ATTR) {
3355 DICT_ATTR const *da = c->cast;
3357 if (!c->cast) da = map->lhs->tmpl_da;
3359 if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3364 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3371 * Convert bare refs to %{Foreach-Variable-N}
3373 if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3374 (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3378 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3379 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3380 T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3382 char *spaces, *text;
3384 fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3386 cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3387 cf_log_err(map->ci, "%s", fmt);
3388 cf_log_err(map->ci, "%s^ %s", spaces, text);
3390 talloc_free(spaces);
3396 talloc_free(map->lhs);
3401 if (map->rhs->type == TMPL_TYPE_REGEX) {
3402 if (!pass2_regex_compile(map->ci, map->rhs)) {
3406 rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3410 * Convert &Packet-Type to "%{Packet-Type}", because
3411 * these attributes don't really exist. The code to
3412 * find an attribute reference doesn't work, but the
3415 vpt = c->data.map->lhs;
3416 if ((vpt->type == TMPL_TYPE_ATTR) && vpt->tmpl_da->flags.virtual) {
3417 if (!c->cast) c->cast = vpt->tmpl_da;
3418 vpt->tmpl_xlat = xlat_from_tmpl_attr(vpt, vpt);
3419 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3423 * @todo v3.1: do the same thing for the RHS...
3427 * Only attributes can have a paircompare registered, and
3428 * they can only be with the current REQUEST, and only
3429 * with the request pairs.
3431 if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3432 (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3433 (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3437 if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3439 if (map->rhs->type == TMPL_TYPE_ATTR) {
3440 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3445 if (map->rhs->type == TMPL_TYPE_REGEX) {
3446 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3452 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3457 if (map->op != T_OP_CMP_EQ) {
3458 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3464 * Mark it as requiring a paircompare() call, instead of
3467 c->pass2_fixup = PASS2_PAIRCOMPARE;
3474 * Compile the RHS of update sections to xlat_exp_t
3476 static bool modcall_pass2_update(modgroup *g)
3480 for (map = g->map; map != NULL; map = map->next) {
3481 if (map->rhs->type == TMPL_TYPE_XLAT) {
3482 rad_assert(map->rhs->tmpl_xlat == NULL);
3485 * FIXME: compile to attribute && handle
3486 * the conversion in map_to_vp().
3488 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3493 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3496 * Deal with undefined attributes now.
3498 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3499 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3502 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3503 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3512 * Do a second-stage pass on compiling the modules.
3514 bool modcall_pass2(modcallable *mc)
3521 for (c = mc; c != NULL; c = c->next) {
3529 g = mod_callabletogroup(c);
3530 if (g->done_pass2) goto do_next;
3532 name2 = cf_section_name2(g->cs);
3534 c->debug_name = unlang_keyword[c->type];
3536 c->debug_name = talloc_asprintf(c, "update %s", name2);
3539 if (!modcall_pass2_update(g)) {
3542 g->done_pass2 = true;
3545 case MOD_XLAT: /* @todo: pre-parse xlat's */
3552 c->debug_name = c->name;
3553 break; /* do nothing */
3558 g = mod_callabletogroup(c);
3559 if (g->done_pass2) goto do_next;
3561 name2 = cf_section_name2(g->cs);
3562 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3565 * The compilation code takes care of
3566 * simplifying 'true' and 'false'
3567 * conditions. For others, we have to do
3568 * a second pass to parse && compile
3571 if (!((g->cond->type == COND_TYPE_TRUE) ||
3572 (g->cond->type == COND_TYPE_FALSE))) {
3573 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3578 if (!modcall_pass2(g->children)) return false;
3579 g->done_pass2 = true;
3585 g = mod_callabletogroup(c);
3586 if (g->done_pass2) goto do_next;
3588 name2 = cf_section_name2(g->cs);
3589 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3592 * We had &Foo-Bar, where Foo-Bar is
3593 * defined by a module.
3596 rad_assert(c->name != NULL);
3597 rad_assert(c->name[0] == '&');
3598 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3600 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3601 cf_section_name2_type(g->cs),
3602 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3604 char *spaces, *text;
3607 fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3609 cf_log_err_cs(g->cs, "Syntax error");
3610 cf_log_err_cs(g->cs, "%s", c->name);
3611 cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3613 talloc_free(spaces);
3623 * Statically compile xlats
3625 if (g->vpt->type == TMPL_TYPE_XLAT) {
3626 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3627 &g->vpt, true, NULL)) {
3635 * We may have: switch Foo-Bar {
3637 * where Foo-Bar is an attribute defined
3638 * by a module. Since there's no leading
3639 * &, it's parsed as a literal. But if
3640 * we can parse it as an attribute,
3641 * switch to using that.
3643 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3646 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3647 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3648 if (slen < 0) goto parse_error;
3649 if (vpt->type == TMPL_TYPE_ATTR) {
3650 talloc_free(g->vpt);
3658 * Warn about old-style configuration.
3660 * DEPRECATED: switch User-Name { ...
3661 * ALLOWED : switch &User-Name { ...
3663 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3664 (c->name[0] != '&')) {
3665 WARN("%s[%d]: Please change %s to &%s",
3666 cf_section_filename(g->cs),
3667 cf_section_lineno(g->cs),
3672 if (!modcall_pass2(g->children)) return false;
3673 g->done_pass2 = true;
3677 g = mod_callabletogroup(c);
3678 if (g->done_pass2) goto do_next;
3680 name2 = cf_section_name2(g->cs);
3682 c->debug_name = unlang_keyword[c->type];
3684 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3687 rad_assert(c->parent != NULL);
3688 rad_assert(c->parent->type == MOD_SWITCH);
3691 * The statement may refer to an
3692 * attribute which doesn't exist until
3693 * all of the modules have been loaded.
3694 * Check for that now.
3696 if (!g->vpt && c->name &&
3697 (c->name[0] == '&') &&
3698 (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3699 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3700 cf_section_name2_type(g->cs),
3701 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3702 if (slen < 0) goto parse_error;
3706 * We have "case {...}". There's no
3707 * argument, so we don't need to check
3710 if (!g->vpt) goto do_children;
3713 * Do type-specific checks on the case statement
3715 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3718 f = mod_callabletogroup(mc->parent);
3719 rad_assert(f->vpt != NULL);
3722 * We're switching over an
3723 * attribute. Check that the
3726 if (f->vpt->type == TMPL_TYPE_ATTR) {
3727 rad_assert(f->vpt->tmpl_da != NULL);
3729 if (tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da) < 0) {
3730 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3740 * Compile and sanity check xlat
3743 if (g->vpt->type == TMPL_TYPE_XLAT) {
3746 f = mod_callabletogroup(mc->parent);
3747 rad_assert(f->vpt != NULL);
3750 * Don't expand xlat's into an
3751 * attribute of a different type.
3753 if (f->vpt->type == TMPL_TYPE_ATTR) {
3754 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3755 &g->vpt, true, f->vpt->tmpl_da)) {
3759 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3760 &g->vpt, true, NULL)) {
3766 if (!modcall_pass2(g->children)) return false;
3767 g->done_pass2 = true;
3771 g = mod_callabletogroup(c);
3772 if (g->done_pass2) goto do_next;
3774 name2 = cf_section_name2(g->cs);
3775 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3778 * Already parsed, handle the children.
3780 if (g->vpt) goto check_children;
3783 * We had &Foo-Bar, where Foo-Bar is
3784 * defined by a module.
3786 rad_assert(c->name != NULL);
3787 rad_assert(c->name[0] == '&');
3788 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3791 * The statement may refer to an
3792 * attribute which doesn't exist until
3793 * all of the modules have been loaded.
3794 * Check for that now.
3796 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3797 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3798 if (slen < 0) goto parse_error;
3801 rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST));
3802 if (g->vpt->tmpl_num != NUM_ALL) {
3803 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3806 if (!modcall_pass2(g->children)) return false;
3807 g->done_pass2 = true;
3811 c->debug_name = unlang_keyword[c->type];
3815 g = mod_callabletogroup(c);
3816 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], cf_section_name1(g->cs));
3821 case MOD_LOAD_BALANCE:
3822 case MOD_REDUNDANT_LOAD_BALANCE:
3823 c->debug_name = unlang_keyword[c->type];
3828 g = mod_callabletogroup(c);
3830 c->debug_name = mc->name; /* for authorize, etc. */
3832 } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3833 char const *name1 = cf_section_name1(g->cs);
3835 if (strcmp(name1, unlang_keyword[c->type]) != 0) {
3836 c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3840 if (g->done_pass2) goto do_next;
3841 if (!modcall_pass2(g->children)) return false;
3842 g->done_pass2 = true;
3847 rad_assert(c->debug_name != NULL);
3853 void modcall_debug(modcallable *mc, int depth)
3860 for (this = mc; this != NULL; this = this->next) {
3861 switch (this->type) {
3866 modsingle *single = mod_callabletosingle(this);
3868 DEBUG("%.*s%s", depth, modcall_spaces,
3869 single->modinst->name);
3875 g = mod_callabletogroup(this);
3876 DEBUG("%.*s%s {", depth, modcall_spaces,
3877 unlang_keyword[this->type]);
3879 for (map = g->map; map != NULL; map = map->next) {
3880 map_prints(buffer, sizeof(buffer), map);
3881 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3884 DEBUG("%.*s}", depth, modcall_spaces);
3888 g = mod_callabletogroup(this);
3889 DEBUG("%.*s%s {", depth, modcall_spaces,
3890 unlang_keyword[this->type]);
3891 modcall_debug(g->children, depth + 1);
3892 DEBUG("%.*s}", depth, modcall_spaces);
3897 g = mod_callabletogroup(this);
3898 fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3899 DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3900 unlang_keyword[this->type], buffer);
3901 modcall_debug(g->children, depth + 1);
3902 DEBUG("%.*s}", depth, modcall_spaces);
3907 g = mod_callabletogroup(this);
3908 tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3909 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3910 unlang_keyword[this->type], buffer);
3911 modcall_debug(g->children, depth + 1);
3912 DEBUG("%.*s}", depth, modcall_spaces);
3917 g = mod_callabletogroup(this);
3918 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3919 unlang_keyword[this->type], this->name);
3920 modcall_debug(g->children, depth + 1);
3921 DEBUG("%.*s}", depth, modcall_spaces);
3925 DEBUG("%.*sbreak", depth, modcall_spaces);
3930 g = mod_callabletogroup(this);
3931 DEBUG("%.*s%s {", depth, modcall_spaces,
3932 unlang_keyword[this->type]);
3933 modcall_debug(g->children, depth + 1);
3934 DEBUG("%.*s}", depth, modcall_spaces);
3938 case MOD_LOAD_BALANCE:
3939 case MOD_REDUNDANT_LOAD_BALANCE:
3940 g = mod_callabletogroup(this);
3941 DEBUG("%.*s%s {", depth, modcall_spaces,
3942 unlang_keyword[this->type]);
3943 modcall_debug(g->children, depth + 1);
3944 DEBUG("%.*s}", depth, modcall_spaces);