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 if (c->type == MOD_CASE) {
776 result = RLM_MODULE_NOOP;
777 goto calculate_result;
780 RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
785 modcall_child(request, component,
786 depth + 1, entry, g->children,
789 goto calculate_result;
793 if (c->type == MOD_SWITCH) {
794 modcallable *this, *found, *null_case;
803 g = mod_callabletogroup(c);
805 memset(&cond, 0, sizeof(cond));
806 memset(&map, 0, sizeof(map));
808 cond.type = COND_TYPE_MAP;
809 cond.data.map = ↦
811 map.op = T_OP_CMP_EQ;
812 map.ci = cf_section_to_item(g->cs);
814 rad_assert(g->vpt != NULL);
816 null_case = found = NULL;
820 * The attribute doesn't exist. We can skip
821 * directly to the default 'case' statement.
823 if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
825 for (this = g->children; this; this = this->next) {
826 rad_assert(this->type == MOD_CASE);
828 h = mod_callabletogroup(this);
829 if (h->vpt) continue;
839 * Expand the template if necessary, so that it
840 * is evaluated once instead of for each 'case'
843 if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
844 (g->vpt->type == TMPL_TYPE_XLAT) ||
845 (g->vpt->type == TMPL_TYPE_EXEC)) {
849 len = tmpl_aexpand(request, &p, request, g->vpt, NULL, NULL);
850 if (len < 0) goto find_null_case;
852 tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, len);
856 * Find either the exact matching name, or the
857 * "case {...}" statement.
859 for (this = g->children; this; this = this->next) {
860 rad_assert(this->type == MOD_CASE);
862 h = mod_callabletogroup(this);
865 * Remember the default case
868 if (!null_case) null_case = this;
873 * If we're switching over an attribute
874 * AND we haven't pre-parsed the data for
875 * the case statement, then cast the data
876 * to the type of the attribute.
878 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
879 (h->vpt->type != TMPL_TYPE_DATA)) {
882 cond.cast = g->vpt->tmpl_da;
885 * Remove unnecessary casting.
887 if ((h->vpt->type == TMPL_TYPE_ATTR) &&
888 (g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
893 * Use the pre-expanded string.
895 } else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
896 (g->vpt->type == TMPL_TYPE_XLAT) ||
897 (g->vpt->type == TMPL_TYPE_EXEC)) {
903 * Else evaluate the 'switch' statement.
911 if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
918 if (!found) found = null_case;
921 talloc_free(data.ptr);
922 modcall_child(request, component, depth + 1, entry, found, &result, true);
924 goto calculate_result;
928 if ((c->type == MOD_LOAD_BALANCE) ||
929 (c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
931 modcallable *this, *found;
936 g = mod_callabletogroup(c);
938 rad_assert(g->children != NULL);
941 * Choose a child at random.
943 for (this = g->children; this; this = this->next) {
946 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
951 if (c->type == MOD_LOAD_BALANCE) {
952 modcall_child(request, component,
953 depth + 1, entry, found,
960 modcall_child(request, component,
961 depth + 1, entry, this,
963 if (this->actions[result] == MOD_ACTION_RETURN) {
969 if (!this) this = g->children;
970 } while (this != found);
973 goto calculate_result;
974 } /* MOD_LOAD_BALANCE */
977 * Reference another virtual server.
979 * This should really be deleted, and replaced with a
980 * more abstracted / functional version.
982 if (c->type == MOD_REFERENCE) {
983 modref *mr = mod_callabletoref(c);
984 char const *server = request->server;
986 if (server == mr->ref_name) {
987 RWDEBUG("Suppressing recursive call to server %s", server);
991 request->server = mr->ref_name;
992 RDEBUG("server %s { # nested call", mr->ref_name);
993 result = indexed_modcall(component, 0, request);
994 RDEBUG("} # server %s with nested call", mr->ref_name);
995 request->server = server;
996 goto calculate_result;
997 } /* MOD_REFERENCE */
1000 * xlat a string without doing anything else
1002 * This should really be deleted, and replaced with a
1003 * more abstracted / functional version.
1005 if (c->type == MOD_XLAT) {
1006 modxlat *mx = mod_callabletoxlat(c);
1010 radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
1012 RDEBUG("`%s`", mx->xlat_name);
1013 radius_exec_program(request, NULL, 0, NULL, request, mx->xlat_name, request->packet->vps,
1014 false, true, EXEC_TIMEOUT);
1021 * Add new module types here.
1026 RDEBUG("(%s, %d) ? (%s, %d)",
1027 fr_int2str(mod_rcode_table, result, "<invalid>"),
1029 fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
1034 rad_assert(result != RLM_MODULE_UNKNOWN);
1037 * The child's action says return. Do so.
1039 if ((c->actions[result] == MOD_ACTION_RETURN) &&
1041 entry->result = result;
1046 * If "reject", break out of the loop and return
1049 if (c->actions[result] == MOD_ACTION_REJECT) {
1050 entry->result = RLM_MODULE_REJECT;
1055 * The array holds a default priority for this return
1056 * code. Grab it in preference to any unset priority.
1059 priority = c->actions[result];
1063 * We're higher than any previous priority, remember this
1064 * return code and priority.
1066 if (priority > entry->priority) {
1067 entry->result = result;
1068 entry->priority = priority;
1073 * If we're processing a "case" statement, we return once
1074 * it's done, rather than going to the next "case" statement.
1076 if (c->type == MOD_CASE) goto finish;
1080 * If we've been told to stop processing
1083 if (entry->unwind == MOD_BREAK) {
1084 RDEBUG2("# unwind to enclosing foreach");
1088 if (entry->unwind == MOD_RETURN) {
1093 if (do_next_sibling) {
1094 entry->c = entry->c->next;
1096 if (entry->c) goto redo;
1108 /** Call a module, iteratively, with a local stack, rather than recursively
1110 * What did Paul Graham say about Lisp...?
1112 int modcall(rlm_components_t component, modcallable *c, REQUEST *request)
1114 modcall_stack_entry_t stack[MODCALL_STACK_MAX];
1117 memset(stack, 0, sizeof(stack));
1120 * Set up the initial stack frame.
1123 stack[0].result = default_component_results[component];
1124 stack[0].priority = 0;
1125 stack[0].unwind = 0;
1128 * Call the main handler.
1130 if (!modcall_recurse(request, component, 0, &stack[0], true)) {
1131 return RLM_MODULE_FAIL;
1135 * Return the result.
1137 return stack[0].result;
1142 static char const *action2str(int action)
1144 static char buf[32];
1145 if(action==MOD_ACTION_RETURN)
1147 if(action==MOD_ACTION_REJECT)
1149 snprintf(buf, sizeof buf, "%d", action);
1153 /* If you suspect a bug in the parser, you'll want to use these dump
1154 * functions. dump_tree should reproduce a whole tree exactly as it was found
1155 * in radiusd.conf, but in long form (all actions explicitly defined) */
1156 static void dump_mc(modcallable *c, int indent)
1160 if(c->type==MOD_SINGLE) {
1161 modsingle *single = mod_callabletosingle(c);
1162 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1163 single->modinst->name);
1164 } else if ((c->type > MOD_SINGLE) && (c->type <= MOD_POLICY)) {
1165 modgroup *g = mod_callabletogroup(c);
1167 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1168 unlang_keyword[c->type]);
1169 for(p = g->children;p;p = p->next)
1170 dump_mc(p, indent+1);
1171 } /* else ignore it for now */
1173 for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
1174 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
1175 fr_int2str(mod_rcode_table, i, "<invalid>"),
1176 action2str(c->actions[i]));
1179 DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
1182 static void dump_tree(rlm_components_t comp, modcallable *c)
1184 DEBUG("[%s]", comp2str[comp]);
1188 #define dump_tree(a, b)
1191 /* These are the default actions. For each component, the group{} block
1192 * behaves like the code from the old module_*() function. redundant{}
1193 * are based on my guesses of what they will be used for. --Pac. */
1195 defaultactions[MOD_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1201 MOD_ACTION_RETURN, /* reject */
1203 MOD_ACTION_RETURN, /* ok */
1204 MOD_ACTION_RETURN, /* handled */
1206 MOD_ACTION_RETURN, /* userlock */
1207 MOD_ACTION_RETURN, /* notfound */
1213 MOD_ACTION_RETURN, /* reject */
1215 MOD_ACTION_RETURN, /* ok */
1216 MOD_ACTION_RETURN, /* handled */
1217 MOD_ACTION_RETURN, /* invalid */
1218 MOD_ACTION_RETURN, /* userlock */
1219 MOD_ACTION_RETURN, /* notfound */
1220 MOD_ACTION_RETURN, /* noop */
1221 MOD_ACTION_RETURN /* updated */
1228 MOD_ACTION_RETURN, /* reject */
1229 MOD_ACTION_RETURN, /* fail */
1231 MOD_ACTION_RETURN, /* handled */
1232 MOD_ACTION_RETURN, /* invalid */
1233 MOD_ACTION_RETURN, /* userlock */
1240 MOD_ACTION_RETURN, /* reject */
1242 MOD_ACTION_RETURN, /* ok */
1243 MOD_ACTION_RETURN, /* handled */
1244 MOD_ACTION_RETURN, /* invalid */
1245 MOD_ACTION_RETURN, /* userlock */
1246 MOD_ACTION_RETURN, /* notfound */
1247 MOD_ACTION_RETURN, /* noop */
1248 MOD_ACTION_RETURN /* updated */
1255 MOD_ACTION_RETURN, /* reject */
1256 MOD_ACTION_RETURN, /* fail */
1258 MOD_ACTION_RETURN, /* handled */
1259 MOD_ACTION_RETURN, /* invalid */
1260 MOD_ACTION_RETURN, /* userlock */
1261 MOD_ACTION_RETURN, /* notfound */
1267 MOD_ACTION_RETURN, /* reject */
1269 MOD_ACTION_RETURN, /* ok */
1270 MOD_ACTION_RETURN, /* handled */
1271 MOD_ACTION_RETURN, /* invalid */
1272 MOD_ACTION_RETURN, /* userlock */
1273 MOD_ACTION_RETURN, /* notfound */
1274 MOD_ACTION_RETURN, /* noop */
1275 MOD_ACTION_RETURN /* updated */
1282 MOD_ACTION_RETURN, /* reject */
1283 MOD_ACTION_RETURN, /* fail */
1285 MOD_ACTION_RETURN, /* handled */
1286 MOD_ACTION_RETURN, /* invalid */
1287 MOD_ACTION_RETURN, /* userlock */
1288 MOD_ACTION_RETURN, /* notfound */
1296 MOD_ACTION_RETURN, /* ok */
1297 MOD_ACTION_RETURN, /* handled */
1309 MOD_ACTION_RETURN, /* reject */
1311 MOD_ACTION_RETURN, /* ok */
1312 MOD_ACTION_RETURN, /* handled */
1313 MOD_ACTION_RETURN, /* invalid */
1314 MOD_ACTION_RETURN, /* userlock */
1315 MOD_ACTION_RETURN, /* notfound */
1316 MOD_ACTION_RETURN, /* noop */
1317 MOD_ACTION_RETURN /* updated */
1321 MOD_ACTION_RETURN, /* reject */
1323 MOD_ACTION_RETURN, /* ok */
1324 MOD_ACTION_RETURN, /* handled */
1325 MOD_ACTION_RETURN, /* invalid */
1326 MOD_ACTION_RETURN, /* userlock */
1327 MOD_ACTION_RETURN, /* notfound */
1328 MOD_ACTION_RETURN, /* noop */
1329 MOD_ACTION_RETURN /* updated */
1336 MOD_ACTION_RETURN, /* reject */
1337 MOD_ACTION_RETURN, /* fail */
1339 MOD_ACTION_RETURN, /* handled */
1340 MOD_ACTION_RETURN, /* invalid */
1341 MOD_ACTION_RETURN, /* userlock */
1348 MOD_ACTION_RETURN, /* reject */
1350 MOD_ACTION_RETURN, /* ok */
1351 MOD_ACTION_RETURN, /* handled */
1352 MOD_ACTION_RETURN, /* invalid */
1353 MOD_ACTION_RETURN, /* userlock */
1354 MOD_ACTION_RETURN, /* notfound */
1355 MOD_ACTION_RETURN, /* noop */
1356 MOD_ACTION_RETURN /* updated */
1363 MOD_ACTION_RETURN, /* reject */
1364 MOD_ACTION_RETURN, /* fail */
1366 MOD_ACTION_RETURN, /* handled */
1367 MOD_ACTION_RETURN, /* invalid */
1368 MOD_ACTION_RETURN, /* userlock */
1375 MOD_ACTION_RETURN, /* reject */
1377 MOD_ACTION_RETURN, /* ok */
1378 MOD_ACTION_RETURN, /* handled */
1379 MOD_ACTION_RETURN, /* invalid */
1380 MOD_ACTION_RETURN, /* userlock */
1381 MOD_ACTION_RETURN, /* notfound */
1382 MOD_ACTION_RETURN, /* noop */
1383 MOD_ACTION_RETURN /* updated */
1390 MOD_ACTION_RETURN, /* reject */
1391 MOD_ACTION_RETURN, /* fail */
1393 MOD_ACTION_RETURN, /* handled */
1394 MOD_ACTION_RETURN, /* invalid */
1395 MOD_ACTION_RETURN, /* userlock */
1402 MOD_ACTION_RETURN, /* reject */
1404 MOD_ACTION_RETURN, /* ok */
1405 MOD_ACTION_RETURN, /* handled */
1406 MOD_ACTION_RETURN, /* invalid */
1407 MOD_ACTION_RETURN, /* userlock */
1408 MOD_ACTION_RETURN, /* notfound */
1409 MOD_ACTION_RETURN, /* noop */
1410 MOD_ACTION_RETURN /* updated */
1419 MOD_ACTION_RETURN, /* reject */
1420 MOD_ACTION_RETURN, /* fail */
1422 MOD_ACTION_RETURN, /* handled */
1423 MOD_ACTION_RETURN, /* invalid */
1424 MOD_ACTION_RETURN, /* userlock */
1431 MOD_ACTION_RETURN, /* reject */
1433 MOD_ACTION_RETURN, /* ok */
1434 MOD_ACTION_RETURN, /* handled */
1435 MOD_ACTION_RETURN, /* invalid */
1436 MOD_ACTION_RETURN, /* userlock */
1437 MOD_ACTION_RETURN, /* notfound */
1438 MOD_ACTION_RETURN, /* noop */
1439 MOD_ACTION_RETURN /* updated */
1446 MOD_ACTION_RETURN, /* reject */
1447 MOD_ACTION_RETURN, /* fail */
1449 MOD_ACTION_RETURN, /* handled */
1450 MOD_ACTION_RETURN, /* invalid */
1451 MOD_ACTION_RETURN, /* userlock */
1458 MOD_ACTION_RETURN, /* reject */
1460 MOD_ACTION_RETURN, /* ok */
1461 MOD_ACTION_RETURN, /* handled */
1462 MOD_ACTION_RETURN, /* invalid */
1463 MOD_ACTION_RETURN, /* userlock */
1464 MOD_ACTION_RETURN, /* notfound */
1465 MOD_ACTION_RETURN, /* noop */
1466 MOD_ACTION_RETURN /* updated */
1472 static const int authtype_actions[GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1476 MOD_ACTION_RETURN, /* reject */
1477 MOD_ACTION_RETURN, /* fail */
1479 MOD_ACTION_RETURN, /* handled */
1480 MOD_ACTION_RETURN, /* invalid */
1481 MOD_ACTION_RETURN, /* userlock */
1488 MOD_ACTION_RETURN, /* reject */
1490 MOD_ACTION_RETURN, /* ok */
1491 MOD_ACTION_RETURN, /* handled */
1492 MOD_ACTION_RETURN, /* invalid */
1493 MOD_ACTION_RETURN, /* userlock */
1494 MOD_ACTION_RETURN, /* notfound */
1495 MOD_ACTION_RETURN, /* noop */
1496 MOD_ACTION_RETURN /* updated */
1500 /** Validate and fixup a map that's part of an update section.
1502 * @param map to validate.
1503 * @param ctx data to pass to fixup function (currently unused).
1504 * @return 0 if valid else -1.
1506 int modcall_fixup_update(vp_map_t *map, UNUSED void *ctx)
1508 CONF_PAIR *cp = cf_item_to_pair(map->ci);
1511 * Anal-retentive checks.
1513 if (DEBUG_ENABLED3) {
1514 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->lhs->name[0] != '&')) {
1515 WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
1516 cf_pair_filename(cp), cf_pair_lineno(cp),
1517 map->lhs->name, fr_int2str(fr_tokens, map->op, "<INVALID>"));
1520 if ((map->rhs->type == TMPL_TYPE_ATTR) && (map->rhs->name[0] != '&')) {
1521 WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
1522 cf_pair_filename(cp), cf_pair_lineno(cp),
1523 fr_int2str(fr_tokens, map->op, "<INVALID>"), map->rhs->name);
1528 * Values used by unary operators should be literal ANY
1530 * We then free the template and alloc a NULL one instead.
1532 if (map->op == T_OP_CMP_FALSE) {
1533 if ((map->rhs->type != TMPL_TYPE_LITERAL) || (strcmp(map->rhs->name, "ANY") != 0)) {
1534 WARN("%s[%d] Wildcard deletion MUST use '!* ANY'",
1535 cf_pair_filename(cp), cf_pair_lineno(cp));
1538 TALLOC_FREE(map->rhs);
1540 map->rhs = tmpl_alloc(map, TMPL_TYPE_NULL, NULL, 0);
1544 * Lots of sanity checks for insane people...
1548 * What exactly where you expecting to happen here?
1550 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
1551 (map->rhs->type == TMPL_TYPE_LIST)) {
1552 cf_log_err(map->ci, "Can't copy list into an attribute");
1557 * Depending on the attribute type, some operators are disallowed.
1559 if ((map->lhs->type == TMPL_TYPE_ATTR) && (!fr_assignment_op[map->op] && !fr_equality_op[map->op])) {
1560 cf_log_err(map->ci, "Invalid operator \"%s\" in update section. "
1561 "Only assignment or filter operators are allowed",
1562 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1566 if (map->lhs->type == TMPL_TYPE_LIST) {
1568 * Can't copy an xlat expansion or literal into a list,
1569 * we don't know what type of attribute we'd need
1572 * The only exception is where were using a unary
1575 if (map->op != T_OP_CMP_FALSE) switch (map->rhs->type) {
1576 case TMPL_TYPE_XLAT:
1577 case TMPL_TYPE_LITERAL:
1578 cf_log_err(map->ci, "Can't copy value into list (we don't know which attribute to create)");
1586 * Only += and :=, and !* operators are supported
1590 case T_OP_CMP_FALSE:
1594 if ((map->rhs->type != TMPL_TYPE_LIST) &&
1595 (map->rhs->type != TMPL_TYPE_EXEC)) {
1596 cf_log_err(map->ci, "Invalid source for list assignment '%s += ...'", map->lhs->name);
1602 if (map->rhs->type == TMPL_TYPE_EXEC) {
1603 WARN("%s[%d]: Please change ':=' to '=' for list assignment",
1604 cf_pair_filename(cp), cf_pair_lineno(cp));
1607 if (map->rhs->type != TMPL_TYPE_LIST) {
1608 cf_log_err(map->ci, "Invalid source for list assignment '%s := ...'", map->lhs->name);
1614 if (map->rhs->type != TMPL_TYPE_EXEC) {
1615 cf_log_err(map->ci, "Invalid source for list assignment '%s = ...'", map->lhs->name);
1621 cf_log_err(map->ci, "Operator \"%s\" not allowed for list assignment",
1622 fr_int2str(fr_tokens, map->op, "<INVALID>"));
1628 * If the map has a unary operator there's no further
1629 * processing we need to, as RHS is unused.
1631 if (map->op == T_OP_CMP_FALSE) return 0;
1634 * If LHS is an attribute, and RHS is a literal, we can
1635 * preparse the information into a TMPL_TYPE_DATA.
1637 * Unless it's a unary operator in which case we
1640 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->rhs->type == TMPL_TYPE_LITERAL)) {
1642 * It's a literal string, just copy it.
1643 * Don't escape anything.
1645 if (!cf_new_escape &&
1646 (map->lhs->tmpl_da->type == PW_TYPE_STRING) &&
1647 (cf_pair_value_type(cp) == T_SINGLE_QUOTED_STRING)) {
1648 tmpl_cast_in_place_str(map->rhs);
1652 * RHS is hex, try to parse it as
1653 * type-specific data.
1655 if (map->lhs->auto_converted &&
1656 (map->rhs->name[0] == '0') && (map->rhs->name[1] == 'x') &&
1657 (map->rhs->len > 2) && ((map->rhs->len & 0x01) == 0)) {
1658 vp_tmpl_t *vpt = map->rhs;
1661 if (!map_cast_from_hex(map, T_BARE_WORD, vpt->name)) {
1663 cf_log_err(map->ci, "%s", fr_strerror());
1668 } else if (tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da) < 0) {
1669 cf_log_err(map->ci, "%s", fr_strerror());
1674 * Fixup LHS da if it doesn't match the type
1677 if (map->lhs->tmpl_da->type != map->rhs->tmpl_data_type) {
1678 DICT_ATTR const *da;
1680 da = dict_attrbytype(map->lhs->tmpl_da->attr, map->lhs->tmpl_da->vendor,
1681 map->rhs->tmpl_data_type);
1683 fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1684 fr_int2str(dict_attr_types, map->rhs->tmpl_data_type,
1685 "<INVALID>"), map->lhs->tmpl_da->name);
1688 map->lhs->tmpl_da = da;
1691 } /* else we can't precompile the data */
1698 static modcallable *do_compile_modupdate(modcallable *parent, rlm_components_t component,
1699 CONF_SECTION *cs, char const *name2)
1703 modcallable *csingle;
1708 * This looks at cs->name2 to determine which list to update
1710 rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
1711 if (rcode < 0) return NULL; /* message already printed */
1713 cf_log_err_cs(cs, "'update' sections cannot be empty");
1717 g = talloc_zero(parent, modgroup);
1718 csingle = mod_grouptocallable(g);
1720 csingle->parent = parent;
1721 csingle->next = NULL;
1724 csingle->name = name2;
1728 csingle->type = MOD_UPDATE;
1729 csingle->method = component;
1731 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
1732 sizeof(csingle->actions));
1734 g->grouptype = GROUPTYPE_SIMPLE;
1737 g->map = talloc_steal(g, head);
1743 static modcallable *do_compile_modswitch (modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1748 bool had_seen_default = false;
1749 modcallable *csingle;
1754 name2 = cf_section_name2(cs);
1756 cf_log_err_cs(cs, "You must specify a variable to switch over for 'switch'");
1760 if (!cf_item_find_next(cs, NULL)) {
1761 cf_log_err_cs(cs, "'switch' statements cannot be empty");
1766 * Create the template. If we fail, AND it's a bare word
1767 * with &Foo-Bar, it MAY be an attribute defined by a
1768 * module. Allow it for now. The pass2 checks below
1771 type = cf_section_name2_type(cs);
1772 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1773 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1774 char *spaces, *text;
1776 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1778 cf_log_err_cs(cs, "Syntax error");
1779 cf_log_err_cs(cs, "%s", name2);
1780 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1782 talloc_free(spaces);
1789 * Otherwise a NULL vpt may refer to an attribute defined
1790 * by a module. That is checked in pass 2.
1793 if (vpt->type == TMPL_TYPE_LIST) {
1794 cf_log_err_cs(cs, "Syntax error: Cannot switch over list '%s'", name2);
1800 * Walk through the children of the switch section,
1801 * ensuring that they're all 'case' statements
1803 for (ci = cf_item_find_next(cs, NULL);
1805 ci = cf_item_find_next(cs, ci)) {
1806 CONF_SECTION *subcs;
1809 if (!cf_item_is_section(ci)) {
1810 if (!cf_item_is_pair(ci)) continue;
1812 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1817 subcs = cf_item_to_section(ci); /* can't return NULL */
1818 name1 = cf_section_name1(subcs);
1820 if (strcmp(name1, "case") != 0) {
1821 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1826 name2 = cf_section_name2(subcs);
1828 if (!had_seen_default) {
1829 had_seen_default = true;
1833 cf_log_err(ci, "Cannot have two 'default' case statements");
1839 csingle = do_compile_modgroup(parent, component, cs,
1848 g = mod_callabletogroup(csingle);
1849 g->vpt = talloc_steal(g, vpt);
1854 static modcallable *do_compile_modcase(modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1858 modcallable *csingle;
1862 if (!parent || (parent->type != MOD_SWITCH)) {
1863 cf_log_err_cs(cs, "\"case\" statements may only appear within a \"switch\" section");
1868 * case THING means "match THING"
1869 * case means "match anything"
1871 name2 = cf_section_name2(cs);
1876 type = cf_section_name2_type(cs);
1878 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1879 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1880 char *spaces, *text;
1882 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1884 cf_log_err_cs(cs, "Syntax error");
1885 cf_log_err_cs(cs, "%s", name2);
1886 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1888 talloc_free(spaces);
1894 if (vpt->type == TMPL_TYPE_LIST) {
1895 cf_log_err_cs(cs, "Syntax error: Cannot match list '%s'", name2);
1900 * Otherwise a NULL vpt may refer to an attribute defined
1901 * by a module. That is checked in pass 2.
1908 csingle = do_compile_modgroup(parent, component, cs,
1918 * The interpretor expects this to be NULL for the
1919 * default case. do_compile_modgroup sets it to name2,
1920 * unless name2 is NULL, in which case it sets it to name1.
1922 csingle->name = name2;
1924 g = mod_callabletogroup(csingle);
1925 g->vpt = talloc_steal(g, vpt);
1928 * Set all of it's codes to return, so that
1929 * when we pick a 'case' statement, we don't
1930 * fall through to processing the next one.
1932 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1933 csingle->actions[i] = MOD_ACTION_RETURN;
1939 static modcallable *do_compile_modforeach(modcallable *parent,
1940 rlm_components_t component, CONF_SECTION *cs)
1944 modcallable *csingle;
1949 name2 = cf_section_name2(cs);
1952 "You must specify an attribute to loop over in 'foreach'");
1956 if (!cf_item_find_next(cs, NULL)) {
1957 cf_log_err_cs(cs, "'foreach' blocks cannot be empty");
1962 * Create the template. If we fail, AND it's a bare word
1963 * with &Foo-Bar, it MAY be an attribute defined by a
1964 * module. Allow it for now. The pass2 checks below
1967 type = cf_section_name2_type(cs);
1968 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1969 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1970 char *spaces, *text;
1972 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1974 cf_log_err_cs(cs, "Syntax error");
1975 cf_log_err_cs(cs, "%s", name2);
1976 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1978 talloc_free(spaces);
1985 * If we don't have a negative return code, we must have a vpt
1986 * (mostly to quiet coverity).
1990 if ((vpt->type != TMPL_TYPE_ATTR) && (vpt->type != TMPL_TYPE_LIST)) {
1991 cf_log_err_cs(cs, "MUST use attribute or list reference in 'foreach'");
1996 * Fix up the template to iterate over all instances of
1997 * the attribute. In a perfect consistent world, users would do
1998 * foreach &attr[*], but that's taking the consistency thing a bit far.
2000 vpt->tmpl_num = NUM_ALL;
2002 csingle = do_compile_modgroup(parent, component, cs,
2003 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2011 g = mod_callabletogroup(csingle);
2017 static modcallable *do_compile_modbreak(modcallable *parent,
2018 rlm_components_t component, CONF_ITEM const *ci)
2020 CONF_SECTION const *cs = NULL;
2022 for (cs = cf_item_parent(ci);
2024 cs = cf_item_parent(cf_section_to_item(cs))) {
2025 if (strcmp(cf_section_name1(cs), "foreach") == 0) {
2031 cf_log_err(ci, "'break' can only be used in a 'foreach' section");
2035 return do_compile_modgroup(parent, component, NULL,
2036 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2041 static modcallable *do_compile_modserver(modcallable *parent,
2042 rlm_components_t component, CONF_ITEM *ci,
2047 modcallable *csingle;
2048 CONF_SECTION *subcs;
2051 subcs = cf_section_sub_find_name2(cs, comp2str[component], NULL);
2053 cf_log_err(ci, "Server %s has no %s section",
2054 server, comp2str[component]);
2058 mr = talloc_zero(parent, modref);
2060 csingle = mod_reftocallable(mr);
2061 csingle->parent = parent;
2062 csingle->next = NULL;
2063 csingle->name = name;
2064 csingle->type = MOD_REFERENCE;
2065 csingle->method = component;
2067 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2068 sizeof(csingle->actions));
2070 mr->ref_name = strdup(server);
2076 static modcallable *do_compile_modxlat(modcallable *parent,
2077 rlm_components_t component, char const *fmt)
2079 modcallable *csingle;
2082 mx = talloc_zero(parent, modxlat);
2084 csingle = mod_xlattocallable(mx);
2085 csingle->parent = parent;
2086 csingle->next = NULL;
2087 csingle->name = "expand";
2088 csingle->type = MOD_XLAT;
2089 csingle->method = component;
2091 memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2092 sizeof(csingle->actions));
2094 mx->xlat_name = strdup(fmt);
2095 if (fmt[0] != '%') {
2099 strcpy(mx->xlat_name, fmt + 1);
2100 p = strrchr(mx->xlat_name, '`');
2108 * redundant, etc. can refer to modules or groups, but not much else.
2110 static int all_children_are_modules(CONF_SECTION *cs, char const *name)
2114 for (ci=cf_item_find_next(cs, NULL);
2116 ci=cf_item_find_next(cs, ci)) {
2118 * If we're a redundant, etc. group, then the
2119 * intention is to call modules, rather than
2120 * processing logic. These checks aren't
2121 * *strictly* necessary, but they keep the users
2122 * from doing crazy things.
2124 if (cf_item_is_section(ci)) {
2125 CONF_SECTION *subcs = cf_item_to_section(ci);
2126 char const *name1 = cf_section_name1(subcs);
2128 if ((strcmp(name1, "if") == 0) ||
2129 (strcmp(name1, "else") == 0) ||
2130 (strcmp(name1, "elsif") == 0) ||
2131 (strcmp(name1, "update") == 0) ||
2132 (strcmp(name1, "switch") == 0) ||
2133 (strcmp(name1, "case") == 0)) {
2134 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
2141 if (cf_item_is_pair(ci)) {
2142 CONF_PAIR *cp = cf_item_to_pair(ci);
2143 if (cf_pair_value(cp) != NULL) {
2145 "Entry with no value is invalid");
2154 /** Load a named module from "instantiate" or "policy".
2156 * If it's "foo.method", look for "foo", and return "method" as the method
2157 * we wish to use, instead of the input component.
2159 * @param[out] pcomponent Where to write the method we found, if any. If no method is specified
2160 * will be set to MOD_COUNT.
2161 * @param[in] real_name Complete name string e.g. foo.authorize.
2162 * @param[in] virtual_name Virtual module name e.g. foo.
2163 * @param[in] method_name Method override (may be NULL) or the method name e.g. authorize.
2164 * @return the CONF_SECTION specifying the virtual module.
2166 static CONF_SECTION *virtual_module_find_cs(rlm_components_t *pcomponent,
2167 char const *real_name, char const *virtual_name, char const *method_name)
2169 CONF_SECTION *cs, *subcs;
2170 rlm_components_t method = *pcomponent;
2174 * Turn the method name into a method enum.
2179 for (i = MOD_AUTHENTICATE; i < MOD_COUNT; i++) {
2180 if (strcmp(comp2str[i], method_name) == 0) break;
2183 if (i != MOD_COUNT) {
2187 virtual_name = real_name;
2192 * Look for "foo" in the "instantiate" section. If we
2193 * find it, AND there's no method name, we've found the
2196 * Return it to the caller, with the updated method.
2198 cs = cf_section_find("instantiate");
2201 * Found "foo". Load it as "foo", or "foo.method".
2203 subcs = cf_section_sub_find_name2(cs, NULL, virtual_name);
2205 *pcomponent = method;
2211 * Look for it in "policy".
2213 * If there's no policy section, we can't do anything else.
2215 cs = cf_section_find("policy");
2216 if (!cs) return NULL;
2219 * "foo.authorize" means "load policy "foo" as method "authorize".
2221 * And bail out if there's no policy "foo".
2224 subcs = cf_section_sub_find_name2(cs, NULL, virtual_name);
2225 if (subcs) *pcomponent = method;
2231 * "foo" means "look for foo.component" first, to allow
2232 * method overrides. If that's not found, just look for
2236 snprintf(buffer, sizeof(buffer), "%s.%s",
2237 virtual_name, comp2str[method]);
2238 subcs = cf_section_sub_find_name2(cs, NULL, buffer);
2239 if (subcs) return subcs;
2241 return cf_section_sub_find_name2(cs, NULL, virtual_name);
2246 * Compile one entry of a module call.
2248 static modcallable *do_compile_modsingle(modcallable *parent,
2249 rlm_components_t component, CONF_ITEM *ci,
2251 char const **modname)
2253 char const *modrefname, *p;
2255 modcallable *csingle;
2256 module_instance_t *this;
2257 CONF_SECTION *cs, *subcs, *modules;
2259 char const *realname;
2260 rlm_components_t method = component;
2262 if (cf_item_is_section(ci)) {
2265 cs = cf_item_to_section(ci);
2266 modrefname = cf_section_name1(cs);
2267 name2 = cf_section_name2(cs);
2268 if (!name2) name2 = "";
2271 * group{}, redundant{}, or append{} may appear
2272 * where a single module instance was expected.
2273 * In that case, we hand it off to
2276 if (strcmp(modrefname, "group") == 0) {
2278 return do_compile_modgroup(parent, component, cs,
2280 grouptype, MOD_GROUP);
2282 } else if (strcmp(modrefname, "redundant") == 0) {
2285 if (!all_children_are_modules(cs, modrefname)) {
2289 return do_compile_modgroup(parent, component, cs,
2290 GROUPTYPE_REDUNDANT,
2291 grouptype, MOD_GROUP);
2293 } else if (strcmp(modrefname, "load-balance") == 0) {
2296 if (!all_children_are_modules(cs, modrefname)) {
2300 return do_compile_modgroup(parent, component, cs,
2302 grouptype, MOD_LOAD_BALANCE);
2304 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2307 if (!all_children_are_modules(cs, modrefname)) {
2311 return do_compile_modgroup(parent, component, cs,
2312 GROUPTYPE_REDUNDANT,
2313 grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2316 } else if (strcmp(modrefname, "if") == 0) {
2317 if (!cf_section_name2(cs)) {
2318 cf_log_err(ci, "'if' without condition");
2323 csingle= do_compile_modgroup(parent, component, cs,
2326 if (!csingle) return NULL;
2331 } else if (strcmp(modrefname, "elsif") == 0) {
2333 ((parent->type == MOD_LOAD_BALANCE) ||
2334 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2335 cf_log_err(ci, "'elsif' cannot be used in this section");
2339 if (!cf_section_name2(cs)) {
2340 cf_log_err(ci, "'elsif' without condition");
2345 return do_compile_modgroup(parent, component, cs,
2347 grouptype, MOD_ELSIF);
2349 } else if (strcmp(modrefname, "else") == 0) {
2351 ((parent->type == MOD_LOAD_BALANCE) ||
2352 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2353 cf_log_err(ci, "'else' cannot be used in this section section");
2357 if (cf_section_name2(cs)) {
2358 cf_log_err(ci, "Cannot have conditions on 'else'");
2363 return do_compile_modgroup(parent, component, cs,
2365 grouptype, MOD_ELSE);
2367 } else if (strcmp(modrefname, "update") == 0) {
2370 return do_compile_modupdate(parent, component, cs,
2373 } else if (strcmp(modrefname, "switch") == 0) {
2376 return do_compile_modswitch (parent, component, cs);
2378 } else if (strcmp(modrefname, "case") == 0) {
2381 return do_compile_modcase(parent, component, cs);
2383 } else if (strcmp(modrefname, "foreach") == 0) {
2386 return do_compile_modforeach(parent, component, cs);
2389 } /* else it's something like sql { fail = 1 ...} */
2391 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2395 * Else it's a module reference, with updated return
2399 CONF_PAIR *cp = cf_item_to_pair(ci);
2400 modrefname = cf_pair_attr(cp);
2403 * Actions (ok = 1), etc. are orthogonal to just
2404 * about everything else.
2406 if (cf_pair_value(cp) != NULL) {
2407 cf_log_err(ci, "Entry is not a reference to a module");
2412 * In-place xlat's via %{...}.
2414 * This should really be removed from the server.
2416 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2417 (modrefname[0] == '`')) {
2418 return do_compile_modxlat(parent, component,
2425 * These can't be over-ridden.
2427 if (strcmp(modrefname, "break") == 0) {
2428 if (!cf_item_is_pair(ci)) {
2429 cf_log_err(ci, "Invalid use of 'break' as section name.");
2433 return do_compile_modbreak(parent, component, ci);
2436 if (strcmp(modrefname, "return") == 0) {
2437 if (!cf_item_is_pair(ci)) {
2438 cf_log_err(ci, "Invalid use of 'return' as section name.");
2442 return do_compile_modgroup(parent, component, NULL,
2443 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2449 * Run a virtual server. This is really terrible and
2450 * should be deleted.
2452 if (strncmp(modrefname, "server[", 7) == 0) {
2455 if (!cf_item_is_pair(ci)) {
2456 cf_log_err(ci, "Invalid syntax");
2460 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2461 p = strrchr(buffer, ']');
2462 if (!p || p[1] != '\0' || (p == buffer)) {
2463 cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2467 buffer[p - buffer] = '\0';
2469 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2471 cf_log_err(ci, "No such server \"%s\".", buffer);
2476 * Ignore stupid attempts to over-ride the return
2479 return do_compile_modserver(parent, component, ci,
2480 modrefname, cs, buffer);
2484 * We now have a name. It can be one of two forms. A
2485 * bare module name, or a section named for the module,
2486 * with over-rides for the return codes.
2488 * The name can refer to a real module, in the "modules"
2489 * section. In that case, the name will be either the
2490 * first or second name of the sub-section of "modules".
2492 * Or, the name can refer to a policy, in the "policy"
2493 * section. In that case, the name will be first name of
2494 * the sub-section of "policy". Unless it's a "redudant"
2497 * Or, the name can refer to a "module.method", in which
2498 * case we're calling a different method than normal for
2501 * Or, the name can refer to a virtual module, in the
2502 * "instantiate" section. In that case, the name will be
2503 * the first of the sub-section of "instantiate". Unless
2504 * it's a "redudant" block...
2506 * We try these in sequence, from the bottom up. This is
2507 * so that things in "instantiate" and "policy" can
2508 * over-ride calls to real modules.
2515 * instantiate { ... name { ...} ... }
2516 * instantiate { ... name.method { ...} ... }
2517 * policy { ... name { .. } .. }
2518 * policy { ... name.method { .. } .. }
2520 * The only difference between things in "instantiate"
2521 * and "policy" is that "instantiate" will cause modules
2522 * to be instantiated in a particular order.
2525 p = strrchr(modrefname, '.');
2527 subcs = virtual_module_find_cs(&method, modrefname, modrefname, NULL);
2531 strlcpy(buffer, modrefname, sizeof(buffer));
2532 buffer[p - modrefname] = '\0';
2534 subcs = virtual_module_find_cs(&method, modrefname, buffer, buffer + (p - modrefname) + 1);
2538 * Check that we're not creating a loop. We may
2539 * be compiling an "sql" module reference inside
2540 * of an "sql" policy. If so, we allow the
2541 * second "sql" to refer to the module.
2543 for (loop = cf_item_parent(ci);
2545 loop = cf_item_parent(cf_section_to_item(loop))) {
2546 if (loop == subcs) {
2552 * We've found the relevant entry. It MUST be a
2555 * However, it can be a "redundant" block, or just a
2560 * modules.c takes care of ensuring that this is:
2563 * load-balance foo { ...
2564 * redundant foo { ...
2565 * redundant-load-balance foo { ...
2567 * We can just recurs to compile the section as
2568 * if it was found here.
2570 if (cf_section_name2(subcs)) {
2571 csingle = do_compile_modsingle(parent,
2573 cf_section_to_item(subcs),
2582 * So we compile it like it was:
2586 csingle = do_compile_modgroup(parent,
2590 grouptype, MOD_GROUP);
2594 * Return the compiled thing if we can.
2596 if (!csingle) return NULL;
2597 if (cf_item_is_pair(ci)) return csingle;
2600 * Else we have a reference to a policy, and that reference
2601 * over-rides the return codes for the policy!
2603 goto action_override;
2607 * Not a virtual module. It must be a real module.
2609 modules = cf_section_find("modules");
2611 realname = modrefname;
2615 * Try to load the optional module.
2617 if (realname[0] == '-') realname++;
2620 * As of v3, the "modules" section contains
2621 * modules we use. Configuration for other
2622 * modules belongs in raddb/mods-available/,
2623 * which isn't loaded into the "modules" section.
2625 this = module_instantiate_method(modules, realname, &method);
2626 if (this) goto allocate_csingle;
2629 * We were asked to MAYBE load it and it
2630 * doesn't exist. Return a soft error.
2632 if (realname != modrefname) {
2633 *modname = modrefname;
2639 * Can't de-reference it to anything. Ugh.
2642 cf_log_err(ci, "Failed to find \"%s\" as a module or policy.", modrefname);
2643 cf_log_err(ci, "Please verify that the configuration exists in %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2647 * We know it's all OK, allocate the structures, and fill
2652 * Check if the module in question has the necessary
2655 if (!this->entry->module->methods[method]) {
2656 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2661 single = talloc_zero(parent, modsingle);
2662 single->modinst = this;
2663 *modname = this->entry->module->name;
2665 csingle = mod_singletocallable(single);
2666 csingle->parent = parent;
2667 csingle->next = NULL;
2668 if (!parent || (component != MOD_AUTHENTICATE)) {
2669 memcpy(csingle->actions, defaultactions[component][grouptype],
2670 sizeof csingle->actions);
2671 } else { /* inside Auth-Type has different rules */
2672 memcpy(csingle->actions, authtype_actions[grouptype],
2673 sizeof csingle->actions);
2675 rad_assert(modrefname != NULL);
2676 csingle->name = realname;
2677 csingle->type = MOD_SINGLE;
2678 csingle->method = method;
2682 * Over-ride the default return codes of the module.
2684 if (cf_item_is_section(ci)) {
2687 cs = cf_item_to_section(ci);
2688 for (csi=cf_item_find_next(cs, NULL);
2690 csi=cf_item_find_next(cs, csi)) {
2692 if (cf_item_is_section(csi)) {
2693 cf_log_err(csi, "Subsection of module instance call not allowed");
2694 talloc_free(csingle);
2698 if (!cf_item_is_pair(csi)) continue;
2700 if (!compile_action(csingle, cf_item_to_pair(csi))) {
2701 talloc_free(csingle);
2710 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2711 modcallable **parent,
2712 rlm_components_t component, CONF_ITEM *ci,
2713 char const **modname)
2720 CONF_SECTION *parentcs;
2722 g = talloc_zero(ctx, modgroup);
2723 memset(g, 0, sizeof(*g));
2724 g->grouptype = GROUPTYPE_SIMPLE;
2725 c = mod_grouptocallable(g);
2728 defaultactions[component][GROUPTYPE_SIMPLE],
2729 sizeof(c->actions));
2731 parentcs = cf_item_parent(ci);
2732 c->name = cf_section_name2(parentcs);
2734 c->name = cf_section_name1(parentcs);
2737 c->type = MOD_GROUP;
2738 c->method = component;
2741 *parent = mod_grouptocallable(g);
2744 ret = do_compile_modsingle(*parent, component, ci,
2747 dump_tree(component, ret);
2753 * Internal compile group code.
2755 static modcallable *do_compile_modgroup(modcallable *parent,
2756 rlm_components_t component, CONF_SECTION *cs,
2757 int grouptype, int parentgrouptype, int mod_type)
2764 g = talloc_zero(parent, modgroup);
2765 g->grouptype = grouptype;
2769 c = mod_grouptocallable(g);
2773 memset(c->actions, 0, sizeof(c->actions));
2775 if (!cs) { /* only for "break" and "return" */
2781 * Remember the name for printing, etc.
2783 * FIXME: We may also want to put the names into a
2784 * rbtree, so that groups can reference each other...
2786 c->name = cf_section_name2(cs);
2788 c->name = cf_section_name1(cs);
2789 if ((strcmp(c->name, "group") == 0) ||
2790 (strcmp(c->name, "redundant") == 0)) {
2792 } else if (c->type == MOD_GROUP) {
2793 c->type = MOD_POLICY;
2799 * Do load-time optimizations
2801 if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2804 rad_assert(parent != NULL);
2806 if (c->type == MOD_IF) {
2807 g->cond = cf_data_find(g->cs, "if");
2808 rad_assert(g->cond != NULL);
2811 if (g->cond->type == COND_TYPE_FALSE) {
2812 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2813 unlang_keyword[g->mc.type],
2814 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2818 } else if (c->type == MOD_ELSIF) {
2820 g->cond = cf_data_find(g->cs, "if");
2821 rad_assert(g->cond != NULL);
2823 rad_assert(parent != NULL);
2824 p = mod_callabletogroup(parent);
2826 rad_assert(p->tail != NULL);
2829 * We're in the process of compiling the
2830 * section, so the parent's tail is the
2831 * previous "if" statement.
2833 f = mod_callabletogroup(p->tail);
2834 if ((f->mc.type != MOD_IF) &&
2835 (f->mc.type != MOD_ELSIF)) {
2836 cf_log_err_cs(g->cs, "Invalid location for 'elsif'. There is no preceding 'if' statement");
2842 * If we took the previous condition, we
2843 * don't need to take this one.
2845 * We reset our condition to 'true', so
2846 * that subsequent sections can check
2847 * that they don't need to be executed.
2849 if (f->cond->type == COND_TYPE_TRUE) {
2851 INFO(" # Skipping contents of '%s' as previous '%s' is always 'true' -- %s:%d",
2852 unlang_keyword[g->mc.type],
2853 unlang_keyword[f->mc.type],
2854 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2861 rad_assert(c->type == MOD_ELSE);
2863 rad_assert(parent != NULL);
2864 p = mod_callabletogroup(parent);
2866 rad_assert(p->tail != NULL);
2868 f = mod_callabletogroup(p->tail);
2869 if ((f->mc.type != MOD_IF) &&
2870 (f->mc.type != MOD_ELSIF)) {
2871 cf_log_err_cs(g->cs, "Invalid location for 'else'. There is no preceding 'if' statement");
2877 * If we took the previous condition, we
2878 * don't need to take this one.
2880 if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2884 * Else we need to compile this section
2890 * Loop over the children of this group.
2892 for (ci=cf_item_find_next(cs, NULL);
2894 ci=cf_item_find_next(cs, ci)) {
2897 * Sections are references to other groups, or
2898 * to modules with updated return codes.
2900 if (cf_item_is_section(ci)) {
2901 char const *junk = NULL;
2902 modcallable *single;
2903 CONF_SECTION *subcs = cf_item_to_section(ci);
2905 single = do_compile_modsingle(c, component, ci,
2908 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2909 cf_section_name1(subcs));
2913 add_child(g, single);
2915 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2919 char const *attr, *value;
2920 CONF_PAIR *cp = cf_item_to_pair(ci);
2922 attr = cf_pair_attr(cp);
2923 value = cf_pair_value(cp);
2926 * A CONF_PAIR is either a module
2927 * instance with no actions
2931 modcallable *single;
2932 char const *junk = NULL;
2934 single = do_compile_modsingle(c,
2940 if (cf_item_is_pair(ci) &&
2941 cf_pair_attr(cf_item_to_pair(ci))[0] == '-') {
2946 "Failed to parse \"%s\" entry.",
2951 add_child(g, single);
2954 * Or a module instance with action.
2956 } else if (!compile_action(c, cp)) {
2959 } /* else it worked */
2965 * Set the default actions, if they haven't already been
2968 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2969 if (!c->actions[i]) {
2970 if (!parent || (component != MOD_AUTHENTICATE)) {
2971 c->actions[i] = defaultactions[component][parentgrouptype][i];
2972 } else { /* inside Auth-Type has different rules */
2973 c->actions[i] = authtype_actions[parentgrouptype][i];
2983 if (grouptype != GROUPTYPE_REDUNDANT) break;
2986 case MOD_LOAD_BALANCE:
2987 case MOD_REDUNDANT_LOAD_BALANCE:
2989 cf_log_err_cs(g->cs, "%s sections cannot be empty",
2990 cf_section_name1(g->cs));
2997 * FIXME: If there are no children, return NULL?
2999 return mod_grouptocallable(g);
3002 modcallable *compile_modgroup(modcallable *parent,
3003 rlm_components_t component, CONF_SECTION *cs)
3005 modcallable *ret = do_compile_modgroup(parent, component, cs,
3007 GROUPTYPE_SIMPLE, MOD_GROUP);
3009 if (rad_debug_lvl > 3) {
3010 modcall_debug(ret, 2);
3016 void add_to_modcallable(modcallable *parent, modcallable *this)
3020 rad_assert(this != NULL);
3021 rad_assert(parent != NULL);
3023 g = mod_callabletogroup(parent);
3030 static bool pass2_xlat_compile(CONF_ITEM const *ci, vp_tmpl_t **pvpt, bool convert,
3031 DICT_ATTR const *da)
3041 rad_assert(vpt->type == TMPL_TYPE_XLAT);
3043 fmt = talloc_typed_strdup(vpt, vpt->name);
3044 slen = xlat_tokenize(vpt, fmt, &head, &error);
3047 char *spaces, *text;
3049 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3051 cf_log_err(ci, "Failed parsing expanded string:");
3052 cf_log_err(ci, "%s", text);
3053 cf_log_err(ci, "%s^ %s", spaces, error);
3055 talloc_free(spaces);
3061 * Convert %{Attribute-Name} to &Attribute-Name
3066 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
3069 * If it's a virtual attribute, leave it
3072 if (attr->tmpl_da->flags.virtual) {
3078 * If the attribute is of incompatible
3079 * type, leave it alone.
3081 if (da && (da->type != attr->tmpl_da->type)) {
3086 if (cf_item_is_pair(ci)) {
3087 CONF_PAIR *cp = cf_item_to_pair(ci);
3089 WARN("%s[%d]: Please change \"%%{%s}\" to &%s",
3090 cf_pair_filename(cp), cf_pair_lineno(cp),
3091 attr->name, attr->name);
3093 CONF_SECTION *cs = cf_item_to_section(ci);
3095 WARN("%s[%d]: Please change \"%%{%s}\" to &%s",
3096 cf_section_filename(cs), cf_section_lineno(cs),
3097 attr->name, attr->name);
3106 * Re-write it to be a pre-parsed XLAT structure.
3108 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3109 vpt->tmpl_xlat = head;
3116 static bool pass2_regex_compile(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3121 rad_assert(vpt->type == TMPL_TYPE_REGEX);
3124 * It's a dynamic expansion. We can't expand the string,
3125 * but we can pre-parse it as an xlat struct. In that
3126 * case, we convert it to a pre-compiled XLAT.
3128 * This is a little more complicated than it needs to be
3129 * because radius_evaluate_map() keys off of the src
3130 * template type, instead of the operators. And, the
3131 * pass2_xlat_compile() function expects to get passed an
3132 * XLAT instead of a REGEX.
3134 if (strchr(vpt->name, '%')) {
3135 vpt->type = TMPL_TYPE_XLAT;
3136 return pass2_xlat_compile(ci, &vpt, false, NULL);
3139 slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3140 vpt->tmpl_iflag, vpt->tmpl_mflag, true, false);
3142 char *spaces, *text;
3144 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3146 cf_log_err(ci, "Invalid regular expression:");
3147 cf_log_err(ci, "%s", text);
3148 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3150 talloc_free(spaces);
3156 vpt->type = TMPL_TYPE_REGEX_STRUCT;
3157 vpt->tmpl_preg = preg;
3163 static bool pass2_fixup_undefined(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3165 DICT_ATTR const *da;
3167 rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3169 da = dict_attrbyname(vpt->tmpl_unknown_name);
3171 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3176 vpt->type = TMPL_TYPE_ATTR;
3180 static bool pass2_callback(void *ctx, fr_cond_t *c)
3186 * These don't get optimized.
3188 if ((c->type == COND_TYPE_TRUE) ||
3189 (c->type == COND_TYPE_FALSE)) {
3196 if (c->type == COND_TYPE_CHILD) return pass2_callback(ctx, c->data.child);
3199 * A few simple checks here.
3201 if (c->type == COND_TYPE_EXISTS) {
3202 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3203 return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3206 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3209 * The existence check might have been &Foo-Bar,
3210 * where Foo-Bar is defined by a module.
3212 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3213 if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3214 c->pass2_fixup = PASS2_FIXUP_NONE;
3218 * Convert virtual &Attr-Foo to "%{Attr-Foo}"
3221 if ((vpt->type == TMPL_TYPE_ATTR) && vpt->tmpl_da->flags.virtual) {
3222 vpt->tmpl_xlat = xlat_from_tmpl_attr(vpt, vpt);
3223 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3230 * And tons of complicated checks.
3232 rad_assert(c->type == COND_TYPE_MAP);
3234 map = c->data.map; /* shorter */
3239 * Where "foo" is dynamically defined.
3241 if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3242 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3243 map->lhs->tmpl_da->vendor,
3245 cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3246 map->lhs->tmpl_da->name,
3252 * These guys can't have a paircompare fixup applied.
3254 c->pass2_fixup = PASS2_FIXUP_NONE;
3258 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3259 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3260 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3263 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3264 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3267 c->pass2_fixup = PASS2_FIXUP_NONE;
3271 * Just in case someone adds a new fixup later.
3273 rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3274 (c->pass2_fixup == PASS2_PAIRCOMPARE));
3279 if (map->lhs->type == TMPL_TYPE_XLAT) {
3281 * Compile the LHS to an attribute reference only
3282 * if the RHS is a literal.
3284 * @todo v3.1: allow anything anywhere.
3286 if (map->rhs->type != TMPL_TYPE_LITERAL) {
3287 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3291 if (!pass2_xlat_compile(map->ci, &map->lhs, true, NULL)) {
3296 * Attribute compared to a literal gets
3297 * the literal cast to the data type of
3300 * The code in parser.c did this for
3304 * But now we've just converted "%{Attr}"
3305 * to &Attr, so we've got to do it again.
3307 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
3308 (map->rhs->type == TMPL_TYPE_LITERAL)) {
3310 * RHS is hex, try to parse it as
3311 * type-specific data.
3313 if (map->lhs->auto_converted &&
3314 (map->rhs->name[0] == '0') && (map->rhs->name[1] == 'x') &&
3315 (map->rhs->len > 2) && ((map->rhs->len & 0x01) == 0)) {
3319 if (!map_cast_from_hex(map, T_BARE_WORD, vpt->name)) {
3321 cf_log_err(map->ci, "%s", fr_strerror());
3326 } else if ((map->rhs->len > 0) ||
3327 (map->op != T_OP_CMP_EQ) ||
3328 (map->lhs->tmpl_da->type == PW_TYPE_STRING) ||
3329 (map->lhs->tmpl_da->type == PW_TYPE_OCTETS)) {
3331 if (tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da) < 0) {
3332 cf_log_err(map->ci, "Failed to parse data type %s from string: %s",
3333 fr_int2str(dict_attr_types, map->lhs->tmpl_da->type, "<UNKNOWN>"),
3336 } /* else the cast was successful */
3338 } else { /* RHS is empty, it's just a check for empty / non-empty string */
3339 vpt = talloc_steal(c, map->lhs);
3341 talloc_free(c->data.map);
3344 * "%{Foo}" == '' ---> !Foo
3345 * "%{Foo}" != '' ---> Foo
3347 c->type = COND_TYPE_EXISTS;
3349 c->negate = !c->negate;
3351 WARN("%s[%d]: Please change (\"%%{%s}\" %s '') to %c&%s",
3352 cf_section_filename(cf_item_to_section(c->ci)),
3353 cf_section_lineno(cf_item_to_section(c->ci)),
3354 vpt->name, c->negate ? "==" : "!=",
3355 c->negate ? '!' : ' ', vpt->name);
3358 * No more RHS, so we can't do more optimizations
3366 if (map->rhs->type == TMPL_TYPE_XLAT) {
3368 * Convert the RHS to an attribute reference only
3369 * if the LHS is an attribute reference, AND is
3370 * of the same type as the RHS.
3372 * We can fix this when the code in evaluate.c
3373 * can handle strings on the LHS, and attributes
3374 * on the RHS. For now, the code in parser.c
3377 if (map->lhs->type == TMPL_TYPE_ATTR) {
3378 DICT_ATTR const *da = c->cast;
3380 if (!c->cast) da = map->lhs->tmpl_da;
3382 if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3387 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3394 * Convert bare refs to %{Foreach-Variable-N}
3396 if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3397 (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3401 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3402 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3403 T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3405 char *spaces, *text;
3407 fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3409 cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3410 cf_log_err(map->ci, "%s", fmt);
3411 cf_log_err(map->ci, "%s^ %s", spaces, text);
3413 talloc_free(spaces);
3419 talloc_free(map->lhs);
3424 if (map->rhs->type == TMPL_TYPE_REGEX) {
3425 if (!pass2_regex_compile(map->ci, map->rhs)) {
3429 rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3433 * Convert &Packet-Type to "%{Packet-Type}", because
3434 * these attributes don't really exist. The code to
3435 * find an attribute reference doesn't work, but the
3438 vpt = c->data.map->lhs;
3439 if ((vpt->type == TMPL_TYPE_ATTR) && vpt->tmpl_da->flags.virtual) {
3440 if (!c->cast) c->cast = vpt->tmpl_da;
3441 vpt->tmpl_xlat = xlat_from_tmpl_attr(vpt, vpt);
3442 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3446 * Convert RHS to expansions, too.
3448 vpt = c->data.map->rhs;
3449 if ((vpt->type == TMPL_TYPE_ATTR) && vpt->tmpl_da->flags.virtual) {
3450 vpt->tmpl_xlat = xlat_from_tmpl_attr(vpt, vpt);
3451 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3455 * @todo v3.1: do the same thing for the RHS...
3459 * Only attributes can have a paircompare registered, and
3460 * they can only be with the current REQUEST, and only
3461 * with the request pairs.
3463 if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3464 (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3465 (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3469 if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3471 if (map->rhs->type == TMPL_TYPE_ATTR) {
3472 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3477 if (map->rhs->type == TMPL_TYPE_REGEX) {
3478 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3484 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3489 if (map->op != T_OP_CMP_EQ) {
3490 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3496 * Mark it as requiring a paircompare() call, instead of
3499 c->pass2_fixup = PASS2_PAIRCOMPARE;
3506 * Compile the RHS of update sections to xlat_exp_t
3508 static bool modcall_pass2_update(modgroup *g)
3512 for (map = g->map; map != NULL; map = map->next) {
3513 if (map->rhs->type == TMPL_TYPE_XLAT) {
3514 rad_assert(map->rhs->tmpl_xlat == NULL);
3517 * FIXME: compile to attribute && handle
3518 * the conversion in map_to_vp().
3520 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3525 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3528 * Deal with undefined attributes now.
3530 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3531 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3534 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3535 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3544 * Do a second-stage pass on compiling the modules.
3546 bool modcall_pass2(modcallable *mc)
3553 for (c = mc; c != NULL; c = c->next) {
3561 g = mod_callabletogroup(c);
3562 if (g->done_pass2) goto do_next;
3564 name2 = cf_section_name2(g->cs);
3566 c->debug_name = unlang_keyword[c->type];
3568 c->debug_name = talloc_asprintf(c, "update %s", name2);
3571 if (!modcall_pass2_update(g)) {
3574 g->done_pass2 = true;
3577 case MOD_XLAT: /* @todo: pre-parse xlat's */
3584 c->debug_name = c->name;
3585 break; /* do nothing */
3590 g = mod_callabletogroup(c);
3591 if (g->done_pass2) goto do_next;
3593 name2 = cf_section_name2(g->cs);
3594 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3597 * The compilation code takes care of
3598 * simplifying 'true' and 'false'
3599 * conditions. For others, we have to do
3600 * a second pass to parse && compile
3603 if (!((g->cond->type == COND_TYPE_TRUE) ||
3604 (g->cond->type == COND_TYPE_FALSE))) {
3605 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3610 if (!modcall_pass2(g->children)) return false;
3611 g->done_pass2 = true;
3617 g = mod_callabletogroup(c);
3618 if (g->done_pass2) goto do_next;
3620 name2 = cf_section_name2(g->cs);
3621 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3624 * We had &Foo-Bar, where Foo-Bar is
3625 * defined by a module.
3628 rad_assert(c->name != NULL);
3629 rad_assert(c->name[0] == '&');
3630 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3632 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3633 cf_section_name2_type(g->cs),
3634 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3636 char *spaces, *text;
3639 fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3641 cf_log_err_cs(g->cs, "Syntax error");
3642 cf_log_err_cs(g->cs, "%s", c->name);
3643 cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3645 talloc_free(spaces);
3655 * Statically compile xlats
3657 if (g->vpt->type == TMPL_TYPE_XLAT) {
3658 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3659 &g->vpt, true, NULL)) {
3667 * Convert virtual &Attr-Foo to "%{Attr-Foo}"
3669 if ((g->vpt->type == TMPL_TYPE_ATTR) && g->vpt->tmpl_da->flags.virtual) {
3670 g->vpt->tmpl_xlat = xlat_from_tmpl_attr(g->vpt, g->vpt);
3671 g->vpt->type = TMPL_TYPE_XLAT_STRUCT;
3675 * We may have: switch Foo-Bar {
3677 * where Foo-Bar is an attribute defined
3678 * by a module. Since there's no leading
3679 * &, it's parsed as a literal. But if
3680 * we can parse it as an attribute,
3681 * switch to using that.
3683 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3686 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3687 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3688 if (slen < 0) goto parse_error;
3689 if (vpt->type == TMPL_TYPE_ATTR) {
3690 talloc_free(g->vpt);
3698 * Warn about old-style configuration.
3700 * DEPRECATED: switch User-Name { ...
3701 * ALLOWED : switch &User-Name { ...
3703 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3704 (c->name[0] != '&')) {
3705 WARN("%s[%d]: Please change %s to &%s",
3706 cf_section_filename(g->cs),
3707 cf_section_lineno(g->cs),
3712 if (!modcall_pass2(g->children)) return false;
3713 g->done_pass2 = true;
3717 g = mod_callabletogroup(c);
3718 if (g->done_pass2) goto do_next;
3720 name2 = cf_section_name2(g->cs);
3722 c->debug_name = unlang_keyword[c->type];
3724 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3727 rad_assert(c->parent != NULL);
3728 rad_assert(c->parent->type == MOD_SWITCH);
3731 * The statement may refer to an
3732 * attribute which doesn't exist until
3733 * all of the modules have been loaded.
3734 * Check for that now.
3736 if (!g->vpt && c->name &&
3737 (c->name[0] == '&') &&
3738 (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3739 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3740 cf_section_name2_type(g->cs),
3741 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3742 if (slen < 0) goto parse_error;
3746 * We have "case {...}". There's no
3747 * argument, so we don't need to check
3750 if (!g->vpt) goto do_children;
3753 * Do type-specific checks on the case statement
3755 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3758 f = mod_callabletogroup(mc->parent);
3759 rad_assert(f->vpt != NULL);
3762 * We're switching over an
3763 * attribute. Check that the
3766 if (f->vpt->type == TMPL_TYPE_ATTR) {
3767 rad_assert(f->vpt->tmpl_da != NULL);
3769 if (tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da) < 0) {
3770 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3780 * Compile and sanity check xlat
3783 if (g->vpt->type == TMPL_TYPE_XLAT) {
3786 f = mod_callabletogroup(mc->parent);
3787 rad_assert(f->vpt != NULL);
3790 * Don't expand xlat's into an
3791 * attribute of a different type.
3793 if (f->vpt->type == TMPL_TYPE_ATTR) {
3794 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3795 &g->vpt, true, f->vpt->tmpl_da)) {
3799 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3800 &g->vpt, true, NULL)) {
3807 * Virtual attribute fixes for "case" statements, too.
3809 if ((g->vpt->type == TMPL_TYPE_ATTR) && g->vpt->tmpl_da->flags.virtual) {
3810 g->vpt->tmpl_xlat = xlat_from_tmpl_attr(g->vpt, g->vpt);
3811 g->vpt->type = TMPL_TYPE_XLAT_STRUCT;
3814 if (!modcall_pass2(g->children)) return false;
3815 g->done_pass2 = true;
3819 g = mod_callabletogroup(c);
3820 if (g->done_pass2) goto do_next;
3822 name2 = cf_section_name2(g->cs);
3823 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3826 * Already parsed, handle the children.
3828 if (g->vpt) goto check_children;
3831 * We had &Foo-Bar, where Foo-Bar is
3832 * defined by a module.
3834 rad_assert(c->name != NULL);
3835 rad_assert(c->name[0] == '&');
3836 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3839 * The statement may refer to an
3840 * attribute which doesn't exist until
3841 * all of the modules have been loaded.
3842 * Check for that now.
3844 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3845 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3846 if (slen < 0) goto parse_error;
3849 rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST));
3850 if (g->vpt->tmpl_num != NUM_ALL) {
3851 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3854 if (!modcall_pass2(g->children)) return false;
3855 g->done_pass2 = true;
3859 c->debug_name = unlang_keyword[c->type];
3863 g = mod_callabletogroup(c);
3864 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], cf_section_name1(g->cs));
3869 case MOD_LOAD_BALANCE:
3870 case MOD_REDUNDANT_LOAD_BALANCE:
3871 c->debug_name = unlang_keyword[c->type];
3876 g = mod_callabletogroup(c);
3878 c->debug_name = mc->name; /* for authorize, etc. */
3880 } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3881 char const *name1 = cf_section_name1(g->cs);
3883 if (strcmp(name1, unlang_keyword[c->type]) != 0) {
3884 c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3888 if (g->done_pass2) goto do_next;
3889 if (!modcall_pass2(g->children)) return false;
3890 g->done_pass2 = true;
3895 rad_assert(c->debug_name != NULL);
3901 void modcall_debug(modcallable *mc, int depth)
3908 for (this = mc; this != NULL; this = this->next) {
3909 switch (this->type) {
3914 modsingle *single = mod_callabletosingle(this);
3916 DEBUG("%.*s%s", depth, modcall_spaces,
3917 single->modinst->name);
3923 g = mod_callabletogroup(this);
3924 DEBUG("%.*s%s {", depth, modcall_spaces,
3925 unlang_keyword[this->type]);
3927 for (map = g->map; map != NULL; map = map->next) {
3928 map_prints(buffer, sizeof(buffer), map);
3929 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3932 DEBUG("%.*s}", depth, modcall_spaces);
3936 g = mod_callabletogroup(this);
3937 DEBUG("%.*s%s {", depth, modcall_spaces,
3938 unlang_keyword[this->type]);
3939 modcall_debug(g->children, depth + 1);
3940 DEBUG("%.*s}", depth, modcall_spaces);
3945 g = mod_callabletogroup(this);
3946 fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3947 DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3948 unlang_keyword[this->type], buffer);
3949 modcall_debug(g->children, depth + 1);
3950 DEBUG("%.*s}", depth, modcall_spaces);
3955 g = mod_callabletogroup(this);
3956 tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3957 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3958 unlang_keyword[this->type], buffer);
3959 modcall_debug(g->children, depth + 1);
3960 DEBUG("%.*s}", depth, modcall_spaces);
3965 g = mod_callabletogroup(this);
3966 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3967 unlang_keyword[this->type], this->name);
3968 modcall_debug(g->children, depth + 1);
3969 DEBUG("%.*s}", depth, modcall_spaces);
3973 DEBUG("%.*sbreak", depth, modcall_spaces);
3978 g = mod_callabletogroup(this);
3979 DEBUG("%.*s%s {", depth, modcall_spaces,
3980 unlang_keyword[this->type]);
3981 modcall_debug(g->children, depth + 1);
3982 DEBUG("%.*s}", depth, modcall_spaces);
3986 case MOD_LOAD_BALANCE:
3987 case MOD_REDUNDANT_LOAD_BALANCE:
3988 g = mod_callabletogroup(this);
3989 DEBUG("%.*s%s {", depth, modcall_spaces,
3990 unlang_keyword[this->type]);
3991 modcall_debug(g->children, depth + 1);
3992 DEBUG("%.*s}", depth, modcall_spaces);