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");
2149 /** Load a named module from "instantiate" or "policy".
2151 * If it's "foo.method", look for "foo", and return "method" as the method
2152 * we wish to use, instead of the input component.
2154 * @param[out] pcomponent Where to write the method we found, if any. If no method is specified
2155 * will be set to MOD_COUNT.
2156 * @param[in] real_name Complete name string e.g. foo.authorize.
2157 * @param[in] virtual_name Virtual module name e.g. foo.
2158 * @param[in] method_name Method override (may be NULL) or the method name e.g. authorize.
2159 * @return the CONF_SECTION specifying the virtual module.
2161 static CONF_SECTION *virtual_module_find_cs(rlm_components_t *pcomponent,
2162 char const *real_name, char const *virtual_name, char const *method_name)
2164 CONF_SECTION *cs, *subcs;
2165 rlm_components_t method = *pcomponent;
2169 * Turn the method name into a method enum.
2174 for (i = MOD_AUTHENTICATE; i < MOD_COUNT; i++) {
2175 if (strcmp(comp2str[i], method_name) == 0) break;
2178 if (i != MOD_COUNT) {
2182 virtual_name = real_name;
2187 * Look for "foo" in the "instantiate" section. If we
2188 * find it, AND there's no method name, we've found the
2191 * Return it to the caller, with the updated method.
2193 cs = cf_section_find("instantiate");
2196 * Found "foo". Load it as "foo", or "foo.method".
2198 subcs = cf_section_sub_find_name2(cs, NULL, virtual_name);
2200 *pcomponent = method;
2206 * Look for it in "policy".
2208 * If there's no policy section, we can't do anything else.
2210 cs = cf_section_find("policy");
2211 if (!cs) return NULL;
2214 * "foo.authorize" means "load policy "foo" as method "authorize".
2216 * And bail out if there's no policy "foo".
2219 subcs = cf_section_sub_find_name2(cs, NULL, virtual_name);
2220 if (subcs) *pcomponent = method;
2226 * "foo" means "look for foo.component" first, to allow
2227 * method overrides. If that's not found, just look for
2231 snprintf(buffer, sizeof(buffer), "%s.%s",
2232 virtual_name, comp2str[method]);
2233 subcs = cf_section_sub_find_name2(cs, NULL, buffer);
2234 if (subcs) return subcs;
2236 return cf_section_sub_find_name2(cs, NULL, virtual_name);
2241 * Compile one entry of a module call.
2243 static modcallable *do_compile_modsingle(modcallable *parent,
2244 rlm_components_t component, CONF_ITEM *ci,
2246 char const **modname)
2248 char const *modrefname, *p;
2250 modcallable *csingle;
2251 module_instance_t *this;
2252 CONF_SECTION *cs, *subcs, *modules;
2254 char const *realname;
2255 rlm_components_t method = component;
2257 if (cf_item_is_section(ci)) {
2260 cs = cf_item_to_section(ci);
2261 modrefname = cf_section_name1(cs);
2262 name2 = cf_section_name2(cs);
2263 if (!name2) name2 = "";
2266 * group{}, redundant{}, or append{} may appear
2267 * where a single module instance was expected.
2268 * In that case, we hand it off to
2271 if (strcmp(modrefname, "group") == 0) {
2273 return do_compile_modgroup(parent, component, cs,
2275 grouptype, MOD_GROUP);
2277 } else if (strcmp(modrefname, "redundant") == 0) {
2280 if (!all_children_are_modules(cs, modrefname)) {
2284 return do_compile_modgroup(parent, component, cs,
2285 GROUPTYPE_REDUNDANT,
2286 grouptype, MOD_GROUP);
2288 } else if (strcmp(modrefname, "load-balance") == 0) {
2291 if (!all_children_are_modules(cs, modrefname)) {
2295 return do_compile_modgroup(parent, component, cs,
2297 grouptype, MOD_LOAD_BALANCE);
2299 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2302 if (!all_children_are_modules(cs, modrefname)) {
2306 return do_compile_modgroup(parent, component, cs,
2307 GROUPTYPE_REDUNDANT,
2308 grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2311 } else if (strcmp(modrefname, "if") == 0) {
2312 if (!cf_section_name2(cs)) {
2313 cf_log_err(ci, "'if' without condition");
2318 csingle= do_compile_modgroup(parent, component, cs,
2321 if (!csingle) return NULL;
2326 } else if (strcmp(modrefname, "elsif") == 0) {
2328 ((parent->type == MOD_LOAD_BALANCE) ||
2329 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2330 cf_log_err(ci, "'elsif' cannot be used in this section");
2334 if (!cf_section_name2(cs)) {
2335 cf_log_err(ci, "'elsif' without condition");
2340 return do_compile_modgroup(parent, component, cs,
2342 grouptype, MOD_ELSIF);
2344 } else if (strcmp(modrefname, "else") == 0) {
2346 ((parent->type == MOD_LOAD_BALANCE) ||
2347 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2348 cf_log_err(ci, "'else' cannot be used in this section section");
2352 if (cf_section_name2(cs)) {
2353 cf_log_err(ci, "Cannot have conditions on 'else'");
2358 return do_compile_modgroup(parent, component, cs,
2360 grouptype, MOD_ELSE);
2362 } else if (strcmp(modrefname, "update") == 0) {
2365 return do_compile_modupdate(parent, component, cs,
2368 } else if (strcmp(modrefname, "switch") == 0) {
2371 return do_compile_modswitch (parent, component, cs);
2373 } else if (strcmp(modrefname, "case") == 0) {
2376 return do_compile_modcase(parent, component, cs);
2378 } else if (strcmp(modrefname, "foreach") == 0) {
2381 return do_compile_modforeach(parent, component, cs);
2384 } /* else it's something like sql { fail = 1 ...} */
2386 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2390 * Else it's a module reference, with updated return
2394 CONF_PAIR *cp = cf_item_to_pair(ci);
2395 modrefname = cf_pair_attr(cp);
2398 * Actions (ok = 1), etc. are orthogonal to just
2399 * about everything else.
2401 if (cf_pair_value(cp) != NULL) {
2402 cf_log_err(ci, "Entry is not a reference to a module");
2407 * In-place xlat's via %{...}.
2409 * This should really be removed from the server.
2411 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2412 (modrefname[0] == '`')) {
2413 return do_compile_modxlat(parent, component,
2420 * These can't be over-ridden.
2422 if (strcmp(modrefname, "break") == 0) {
2423 if (!cf_item_is_pair(ci)) {
2424 cf_log_err(ci, "Invalid use of 'break' as section name.");
2428 return do_compile_modbreak(parent, component, ci);
2431 if (strcmp(modrefname, "return") == 0) {
2432 if (!cf_item_is_pair(ci)) {
2433 cf_log_err(ci, "Invalid use of 'return' as section name.");
2437 return do_compile_modgroup(parent, component, NULL,
2438 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2444 * Run a virtual server. This is really terrible and
2445 * should be deleted.
2447 if (strncmp(modrefname, "server[", 7) == 0) {
2450 if (!cf_item_is_pair(ci)) {
2451 cf_log_err(ci, "Invalid syntax");
2455 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2456 p = strrchr(buffer, ']');
2457 if (!p || p[1] != '\0' || (p == buffer)) {
2458 cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2462 buffer[p - buffer] = '\0';
2464 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2466 cf_log_err(ci, "No such server \"%s\".", buffer);
2471 * Ignore stupid attempts to over-ride the return
2474 return do_compile_modserver(parent, component, ci,
2475 modrefname, cs, buffer);
2479 * We now have a name. It can be one of two forms. A
2480 * bare module name, or a section named for the module,
2481 * with over-rides for the return codes.
2483 * The name can refer to a real module, in the "modules"
2484 * section. In that case, the name will be either the
2485 * first or second name of the sub-section of "modules".
2487 * Or, the name can refer to a policy, in the "policy"
2488 * section. In that case, the name will be first name of
2489 * the sub-section of "policy". Unless it's a "redudant"
2492 * Or, the name can refer to a "module.method", in which
2493 * case we're calling a different method than normal for
2496 * Or, the name can refer to a virtual module, in the
2497 * "instantiate" section. In that case, the name will be
2498 * the first of the sub-section of "instantiate". Unless
2499 * it's a "redudant" block...
2501 * We try these in sequence, from the bottom up. This is
2502 * so that things in "instantiate" and "policy" can
2503 * over-ride calls to real modules.
2510 * instantiate { ... name { ...} ... }
2511 * instantiate { ... name.method { ...} ... }
2512 * policy { ... name { .. } .. }
2513 * policy { ... name.method { .. } .. }
2515 * The only difference between things in "instantiate"
2516 * and "policy" is that "instantiate" will cause modules
2517 * to be instantiated in a particular order.
2520 p = strrchr(modrefname, '.');
2522 subcs = virtual_module_find_cs(&method, modrefname, modrefname, NULL);
2526 strlcpy(buffer, modrefname, sizeof(buffer));
2527 buffer[p - modrefname] = '\0';
2529 subcs = virtual_module_find_cs(&method, modrefname, buffer, buffer + (p - modrefname) + 1);
2533 * Check that we're not creating a loop. We may
2534 * be compiling an "sql" module reference inside
2535 * of an "sql" policy. If so, we allow the
2536 * second "sql" to refer to the module.
2538 for (loop = cf_item_parent(ci);
2540 loop = cf_item_parent(cf_section_to_item(loop))) {
2541 if (loop == subcs) {
2547 * We've found the relevant entry. It MUST be a
2550 * However, it can be a "redundant" block, or just a
2555 * modules.c takes care of ensuring that this is:
2558 * load-balance foo { ...
2559 * redundant foo { ...
2560 * redundant-load-balance foo { ...
2562 * We can just recurs to compile the section as
2563 * if it was found here.
2565 if (cf_section_name2(subcs)) {
2566 csingle = do_compile_modsingle(parent,
2568 cf_section_to_item(subcs),
2577 * So we compile it like it was:
2581 csingle = do_compile_modgroup(parent,
2585 grouptype, MOD_GROUP);
2589 * Return the compiled thing if we can.
2591 if (!csingle) return NULL;
2592 if (cf_item_is_pair(ci)) return csingle;
2595 * Else we have a reference to a policy, and that reference
2596 * over-rides the return codes for the policy!
2598 goto action_override;
2602 * Not a virtual module. It must be a real module.
2604 modules = cf_section_find("modules");
2606 realname = modrefname;
2610 * Try to load the optional module.
2612 if (realname[0] == '-') realname++;
2615 * As of v3, the "modules" section contains
2616 * modules we use. Configuration for other
2617 * modules belongs in raddb/mods-available/,
2618 * which isn't loaded into the "modules" section.
2620 this = module_instantiate_method(modules, realname, &method);
2621 if (this) goto allocate_csingle;
2624 * We were asked to MAYBE load it and it
2625 * doesn't exist. Return a soft error.
2627 if (realname != modrefname) {
2628 *modname = modrefname;
2634 * Can't de-reference it to anything. Ugh.
2637 cf_log_err(ci, "Failed to find \"%s\" as a module or policy.", modrefname);
2638 cf_log_err(ci, "Please verify that the configuration exists in %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2642 * We know it's all OK, allocate the structures, and fill
2647 * Check if the module in question has the necessary
2650 if (!this->entry->module->methods[method]) {
2651 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2656 single = talloc_zero(parent, modsingle);
2657 single->modinst = this;
2658 *modname = this->entry->module->name;
2660 csingle = mod_singletocallable(single);
2661 csingle->parent = parent;
2662 csingle->next = NULL;
2663 if (!parent || (component != MOD_AUTHENTICATE)) {
2664 memcpy(csingle->actions, defaultactions[component][grouptype],
2665 sizeof csingle->actions);
2666 } else { /* inside Auth-Type has different rules */
2667 memcpy(csingle->actions, authtype_actions[grouptype],
2668 sizeof csingle->actions);
2670 rad_assert(modrefname != NULL);
2671 csingle->name = realname;
2672 csingle->type = MOD_SINGLE;
2673 csingle->method = method;
2677 * Over-ride the default return codes of the module.
2679 if (cf_item_is_section(ci)) {
2682 cs = cf_item_to_section(ci);
2683 for (csi=cf_item_find_next(cs, NULL);
2685 csi=cf_item_find_next(cs, csi)) {
2687 if (cf_item_is_section(csi)) {
2688 cf_log_err(csi, "Subsection of module instance call not allowed");
2689 talloc_free(csingle);
2693 if (!cf_item_is_pair(csi)) continue;
2695 if (!compile_action(csingle, cf_item_to_pair(csi))) {
2696 talloc_free(csingle);
2705 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2706 modcallable **parent,
2707 rlm_components_t component, CONF_ITEM *ci,
2708 char const **modname)
2715 CONF_SECTION *parentcs;
2717 g = talloc_zero(ctx, modgroup);
2718 memset(g, 0, sizeof(*g));
2719 g->grouptype = GROUPTYPE_SIMPLE;
2720 c = mod_grouptocallable(g);
2723 defaultactions[component][GROUPTYPE_SIMPLE],
2724 sizeof(c->actions));
2726 parentcs = cf_item_parent(ci);
2727 c->name = cf_section_name2(parentcs);
2729 c->name = cf_section_name1(parentcs);
2732 c->type = MOD_GROUP;
2733 c->method = component;
2736 *parent = mod_grouptocallable(g);
2739 ret = do_compile_modsingle(*parent, component, ci,
2742 dump_tree(component, ret);
2748 * Internal compile group code.
2750 static modcallable *do_compile_modgroup(modcallable *parent,
2751 rlm_components_t component, CONF_SECTION *cs,
2752 int grouptype, int parentgrouptype, int mod_type)
2759 g = talloc_zero(parent, modgroup);
2760 g->grouptype = grouptype;
2764 c = mod_grouptocallable(g);
2768 memset(c->actions, 0, sizeof(c->actions));
2770 if (!cs) { /* only for "break" and "return" */
2776 * Remember the name for printing, etc.
2778 * FIXME: We may also want to put the names into a
2779 * rbtree, so that groups can reference each other...
2781 c->name = cf_section_name2(cs);
2783 c->name = cf_section_name1(cs);
2784 if ((strcmp(c->name, "group") == 0) ||
2785 (strcmp(c->name, "redundant") == 0)) {
2787 } else if (c->type == MOD_GROUP) {
2788 c->type = MOD_POLICY;
2794 * Do load-time optimizations
2796 if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2799 rad_assert(parent != NULL);
2801 if (c->type == MOD_IF) {
2802 g->cond = cf_data_find(g->cs, "if");
2803 rad_assert(g->cond != NULL);
2806 if (g->cond->type == COND_TYPE_FALSE) {
2807 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2808 unlang_keyword[g->mc.type],
2809 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2813 } else if (c->type == MOD_ELSIF) {
2815 g->cond = cf_data_find(g->cs, "if");
2816 rad_assert(g->cond != NULL);
2818 rad_assert(parent != NULL);
2819 p = mod_callabletogroup(parent);
2821 rad_assert(p->tail != NULL);
2824 * We're in the process of compiling the
2825 * section, so the parent's tail is the
2826 * previous "if" statement.
2828 f = mod_callabletogroup(p->tail);
2829 if ((f->mc.type != MOD_IF) &&
2830 (f->mc.type != MOD_ELSIF)) {
2831 cf_log_err_cs(g->cs, "Invalid location for 'elsif'. There is no preceding 'if' statement");
2837 * If we took the previous condition, we
2838 * don't need to take this one.
2840 * We reset our condition to 'true', so
2841 * that subsequent sections can check
2842 * that they don't need to be executed.
2844 if (f->cond->type == COND_TYPE_TRUE) {
2846 INFO(" # Skipping contents of '%s' as previous '%s' is always 'true' -- %s:%d",
2847 unlang_keyword[g->mc.type],
2848 unlang_keyword[f->mc.type],
2849 cf_section_filename(g->cs), cf_section_lineno(g->cs));
2856 rad_assert(c->type == MOD_ELSE);
2858 rad_assert(parent != NULL);
2859 p = mod_callabletogroup(parent);
2861 rad_assert(p->tail != NULL);
2863 f = mod_callabletogroup(p->tail);
2864 if ((f->mc.type != MOD_IF) &&
2865 (f->mc.type != MOD_ELSIF)) {
2866 cf_log_err_cs(g->cs, "Invalid location for 'else'. There is no preceding 'if' statement");
2872 * If we took the previous condition, we
2873 * don't need to take this one.
2875 if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2879 * Else we need to compile this section
2885 * Loop over the children of this group.
2887 for (ci=cf_item_find_next(cs, NULL);
2889 ci=cf_item_find_next(cs, ci)) {
2892 * Sections are references to other groups, or
2893 * to modules with updated return codes.
2895 if (cf_item_is_section(ci)) {
2896 char const *junk = NULL;
2897 modcallable *single;
2898 CONF_SECTION *subcs = cf_item_to_section(ci);
2900 single = do_compile_modsingle(c, component, ci,
2903 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2904 cf_section_name1(subcs));
2908 add_child(g, single);
2910 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2914 char const *attr, *value;
2915 CONF_PAIR *cp = cf_item_to_pair(ci);
2917 attr = cf_pair_attr(cp);
2918 value = cf_pair_value(cp);
2921 * A CONF_PAIR is either a module
2922 * instance with no actions
2926 modcallable *single;
2927 char const *junk = NULL;
2929 single = do_compile_modsingle(c,
2935 if (cf_item_is_pair(ci) &&
2936 cf_pair_attr(cf_item_to_pair(ci))[0] == '-') {
2941 "Failed to parse \"%s\" entry.",
2946 add_child(g, single);
2949 * Or a module instance with action.
2951 } else if (!compile_action(c, cp)) {
2954 } /* else it worked */
2960 * Set the default actions, if they haven't already been
2963 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2964 if (!c->actions[i]) {
2965 if (!parent || (component != MOD_AUTHENTICATE)) {
2966 c->actions[i] = defaultactions[component][parentgrouptype][i];
2967 } else { /* inside Auth-Type has different rules */
2968 c->actions[i] = authtype_actions[parentgrouptype][i];
2978 if (grouptype != GROUPTYPE_REDUNDANT) break;
2981 case MOD_LOAD_BALANCE:
2982 case MOD_REDUNDANT_LOAD_BALANCE:
2984 cf_log_err_cs(g->cs, "%s sections cannot be empty",
2985 cf_section_name1(g->cs));
2992 * FIXME: If there are no children, return NULL?
2994 return mod_grouptocallable(g);
2997 modcallable *compile_modgroup(modcallable *parent,
2998 rlm_components_t component, CONF_SECTION *cs)
3000 modcallable *ret = do_compile_modgroup(parent, component, cs,
3002 GROUPTYPE_SIMPLE, MOD_GROUP);
3004 if (rad_debug_lvl > 3) {
3005 modcall_debug(ret, 2);
3011 void add_to_modcallable(modcallable *parent, modcallable *this)
3015 rad_assert(this != NULL);
3016 rad_assert(parent != NULL);
3018 g = mod_callabletogroup(parent);
3025 static bool pass2_xlat_compile(CONF_ITEM const *ci, vp_tmpl_t **pvpt, bool convert,
3026 DICT_ATTR const *da)
3036 rad_assert(vpt->type == TMPL_TYPE_XLAT);
3038 fmt = talloc_typed_strdup(vpt, vpt->name);
3039 slen = xlat_tokenize(vpt, fmt, &head, &error);
3042 char *spaces, *text;
3044 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3046 cf_log_err(ci, "Failed parsing expanded string:");
3047 cf_log_err(ci, "%s", text);
3048 cf_log_err(ci, "%s^ %s", spaces, error);
3050 talloc_free(spaces);
3056 * Convert %{Attribute-Name} to &Attribute-Name
3061 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
3064 * If it's a virtual attribute, leave it
3067 if (attr->tmpl_da->flags.virtual) {
3073 * If the attribute is of incompatible
3074 * type, leave it alone.
3076 if (da && (da->type != attr->tmpl_da->type)) {
3081 if (cf_item_is_pair(ci)) {
3082 CONF_PAIR *cp = cf_item_to_pair(ci);
3084 WARN("%s[%d]: Please change \"%%{%s}\" to &%s",
3085 cf_pair_filename(cp), cf_pair_lineno(cp),
3086 attr->name, attr->name);
3088 CONF_SECTION *cs = cf_item_to_section(ci);
3090 WARN("%s[%d]: Please change \"%%{%s}\" to &%s",
3091 cf_section_filename(cs), cf_section_lineno(cs),
3092 attr->name, attr->name);
3101 * Re-write it to be a pre-parsed XLAT structure.
3103 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3104 vpt->tmpl_xlat = head;
3111 static bool pass2_regex_compile(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3116 rad_assert(vpt->type == TMPL_TYPE_REGEX);
3119 * It's a dynamic expansion. We can't expand the string,
3120 * but we can pre-parse it as an xlat struct. In that
3121 * case, we convert it to a pre-compiled XLAT.
3123 * This is a little more complicated than it needs to be
3124 * because radius_evaluate_map() keys off of the src
3125 * template type, instead of the operators. And, the
3126 * pass2_xlat_compile() function expects to get passed an
3127 * XLAT instead of a REGEX.
3129 if (strchr(vpt->name, '%')) {
3130 vpt->type = TMPL_TYPE_XLAT;
3131 return pass2_xlat_compile(ci, &vpt, false, NULL);
3134 slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3135 vpt->tmpl_iflag, vpt->tmpl_mflag, true, false);
3137 char *spaces, *text;
3139 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3141 cf_log_err(ci, "Invalid regular expression:");
3142 cf_log_err(ci, "%s", text);
3143 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3145 talloc_free(spaces);
3151 vpt->type = TMPL_TYPE_REGEX_STRUCT;
3152 vpt->tmpl_preg = preg;
3158 static bool pass2_fixup_undefined(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3160 DICT_ATTR const *da;
3162 rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3164 da = dict_attrbyname(vpt->tmpl_unknown_name);
3166 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3171 vpt->type = TMPL_TYPE_ATTR;
3175 static bool pass2_callback(void *ctx, fr_cond_t *c)
3181 * These don't get optimized.
3183 if ((c->type == COND_TYPE_TRUE) ||
3184 (c->type == COND_TYPE_FALSE)) {
3191 if (c->type == COND_TYPE_CHILD) return pass2_callback(ctx, c->data.child);
3194 * A few simple checks here.
3196 if (c->type == COND_TYPE_EXISTS) {
3197 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3198 return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3201 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3204 * The existence check might have been &Foo-Bar,
3205 * where Foo-Bar is defined by a module.
3207 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3208 if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3209 c->pass2_fixup = PASS2_FIXUP_NONE;
3213 * Convert virtual &Attr-Foo to "%{Attr-Foo}"
3216 if ((vpt->type == TMPL_TYPE_ATTR) && vpt->tmpl_da->flags.virtual) {
3217 vpt->tmpl_xlat = xlat_from_tmpl_attr(vpt, vpt);
3218 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3225 * And tons of complicated checks.
3227 rad_assert(c->type == COND_TYPE_MAP);
3229 map = c->data.map; /* shorter */
3234 * Where "foo" is dynamically defined.
3236 if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3237 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3238 map->lhs->tmpl_da->vendor,
3240 cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3241 map->lhs->tmpl_da->name,
3247 * These guys can't have a paircompare fixup applied.
3249 c->pass2_fixup = PASS2_FIXUP_NONE;
3253 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3254 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3255 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3258 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3259 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3262 c->pass2_fixup = PASS2_FIXUP_NONE;
3266 * Just in case someone adds a new fixup later.
3268 rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3269 (c->pass2_fixup == PASS2_PAIRCOMPARE));
3274 if (map->lhs->type == TMPL_TYPE_XLAT) {
3276 * Compile the LHS to an attribute reference only
3277 * if the RHS is a literal.
3279 * @todo v3.1: allow anything anywhere.
3281 if (map->rhs->type != TMPL_TYPE_LITERAL) {
3282 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3286 if (!pass2_xlat_compile(map->ci, &map->lhs, true, NULL)) {
3291 * Attribute compared to a literal gets
3292 * the literal cast to the data type of
3295 * The code in parser.c did this for
3299 * But now we've just converted "%{Attr}"
3300 * to &Attr, so we've got to do it again.
3302 if ((map->lhs->type == TMPL_TYPE_ATTR) &&
3303 (map->rhs->type == TMPL_TYPE_LITERAL)) {
3305 * RHS is hex, try to parse it as
3306 * type-specific data.
3308 if (map->lhs->auto_converted &&
3309 (map->rhs->name[0] == '0') && (map->rhs->name[1] == 'x') &&
3310 (map->rhs->len > 2) && ((map->rhs->len & 0x01) == 0)) {
3314 if (!map_cast_from_hex(map, T_BARE_WORD, vpt->name)) {
3316 cf_log_err(map->ci, "%s", fr_strerror());
3321 } else if ((map->rhs->len > 0) ||
3322 (map->op != T_OP_CMP_EQ) ||
3323 (map->lhs->tmpl_da->type == PW_TYPE_STRING) ||
3324 (map->lhs->tmpl_da->type == PW_TYPE_OCTETS)) {
3326 if (tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da) < 0) {
3327 cf_log_err(map->ci, "Failed to parse data type %s from string: %s",
3328 fr_int2str(dict_attr_types, map->lhs->tmpl_da->type, "<UNKNOWN>"),
3331 } /* else the cast was successful */
3333 } else { /* RHS is empty, it's just a check for empty / non-empty string */
3334 vpt = talloc_steal(c, map->lhs);
3336 talloc_free(c->data.map);
3339 * "%{Foo}" == '' ---> !Foo
3340 * "%{Foo}" != '' ---> Foo
3342 c->type = COND_TYPE_EXISTS;
3344 c->negate = !c->negate;
3346 WARN("%s[%d]: Please change (\"%%{%s}\" %s '') to %c&%s",
3347 cf_section_filename(cf_item_to_section(c->ci)),
3348 cf_section_lineno(cf_item_to_section(c->ci)),
3349 vpt->name, c->negate ? "==" : "!=",
3350 c->negate ? '!' : ' ', vpt->name);
3353 * No more RHS, so we can't do more optimizations
3361 if (map->rhs->type == TMPL_TYPE_XLAT) {
3363 * Convert the RHS to an attribute reference only
3364 * if the LHS is an attribute reference, AND is
3365 * of the same type as the RHS.
3367 * We can fix this when the code in evaluate.c
3368 * can handle strings on the LHS, and attributes
3369 * on the RHS. For now, the code in parser.c
3372 if (map->lhs->type == TMPL_TYPE_ATTR) {
3373 DICT_ATTR const *da = c->cast;
3375 if (!c->cast) da = map->lhs->tmpl_da;
3377 if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3382 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3389 * Convert bare refs to %{Foreach-Variable-N}
3391 if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3392 (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3396 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3397 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3398 T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3400 char *spaces, *text;
3402 fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3404 cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3405 cf_log_err(map->ci, "%s", fmt);
3406 cf_log_err(map->ci, "%s^ %s", spaces, text);
3408 talloc_free(spaces);
3414 talloc_free(map->lhs);
3419 if (map->rhs->type == TMPL_TYPE_REGEX) {
3420 if (!pass2_regex_compile(map->ci, map->rhs)) {
3424 rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3428 * Convert &Packet-Type to "%{Packet-Type}", because
3429 * these attributes don't really exist. The code to
3430 * find an attribute reference doesn't work, but the
3433 vpt = c->data.map->lhs;
3434 if ((vpt->type == TMPL_TYPE_ATTR) && vpt->tmpl_da->flags.virtual) {
3435 if (!c->cast) c->cast = vpt->tmpl_da;
3436 vpt->tmpl_xlat = xlat_from_tmpl_attr(vpt, vpt);
3437 vpt->type = TMPL_TYPE_XLAT_STRUCT;
3441 * @todo v3.1: do the same thing for the RHS...
3445 * Only attributes can have a paircompare registered, and
3446 * they can only be with the current REQUEST, and only
3447 * with the request pairs.
3449 if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3450 (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3451 (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3455 if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3457 if (map->rhs->type == TMPL_TYPE_ATTR) {
3458 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3463 if (map->rhs->type == TMPL_TYPE_REGEX) {
3464 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3470 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3475 if (map->op != T_OP_CMP_EQ) {
3476 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3482 * Mark it as requiring a paircompare() call, instead of
3485 c->pass2_fixup = PASS2_PAIRCOMPARE;
3492 * Compile the RHS of update sections to xlat_exp_t
3494 static bool modcall_pass2_update(modgroup *g)
3498 for (map = g->map; map != NULL; map = map->next) {
3499 if (map->rhs->type == TMPL_TYPE_XLAT) {
3500 rad_assert(map->rhs->tmpl_xlat == NULL);
3503 * FIXME: compile to attribute && handle
3504 * the conversion in map_to_vp().
3506 if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3511 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3514 * Deal with undefined attributes now.
3516 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3517 if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3520 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3521 if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3530 * Do a second-stage pass on compiling the modules.
3532 bool modcall_pass2(modcallable *mc)
3539 for (c = mc; c != NULL; c = c->next) {
3547 g = mod_callabletogroup(c);
3548 if (g->done_pass2) goto do_next;
3550 name2 = cf_section_name2(g->cs);
3552 c->debug_name = unlang_keyword[c->type];
3554 c->debug_name = talloc_asprintf(c, "update %s", name2);
3557 if (!modcall_pass2_update(g)) {
3560 g->done_pass2 = true;
3563 case MOD_XLAT: /* @todo: pre-parse xlat's */
3570 c->debug_name = c->name;
3571 break; /* do nothing */
3576 g = mod_callabletogroup(c);
3577 if (g->done_pass2) goto do_next;
3579 name2 = cf_section_name2(g->cs);
3580 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3583 * The compilation code takes care of
3584 * simplifying 'true' and 'false'
3585 * conditions. For others, we have to do
3586 * a second pass to parse && compile
3589 if (!((g->cond->type == COND_TYPE_TRUE) ||
3590 (g->cond->type == COND_TYPE_FALSE))) {
3591 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3596 if (!modcall_pass2(g->children)) return false;
3597 g->done_pass2 = true;
3603 g = mod_callabletogroup(c);
3604 if (g->done_pass2) goto do_next;
3606 name2 = cf_section_name2(g->cs);
3607 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3610 * We had &Foo-Bar, where Foo-Bar is
3611 * defined by a module.
3614 rad_assert(c->name != NULL);
3615 rad_assert(c->name[0] == '&');
3616 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3618 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3619 cf_section_name2_type(g->cs),
3620 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3622 char *spaces, *text;
3625 fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3627 cf_log_err_cs(g->cs, "Syntax error");
3628 cf_log_err_cs(g->cs, "%s", c->name);
3629 cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3631 talloc_free(spaces);
3641 * Statically compile xlats
3643 if (g->vpt->type == TMPL_TYPE_XLAT) {
3644 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3645 &g->vpt, true, NULL)) {
3653 * Convert virtual &Attr-Foo to "%{Attr-Foo}"
3655 if ((g->vpt->type == TMPL_TYPE_ATTR) && g->vpt->tmpl_da->flags.virtual) {
3656 g->vpt->tmpl_xlat = xlat_from_tmpl_attr(g->vpt, g->vpt);
3657 g->vpt->type = TMPL_TYPE_XLAT_STRUCT;
3661 * We may have: switch Foo-Bar {
3663 * where Foo-Bar is an attribute defined
3664 * by a module. Since there's no leading
3665 * &, it's parsed as a literal. But if
3666 * we can parse it as an attribute,
3667 * switch to using that.
3669 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3672 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3673 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3674 if (slen < 0) goto parse_error;
3675 if (vpt->type == TMPL_TYPE_ATTR) {
3676 talloc_free(g->vpt);
3684 * Warn about old-style configuration.
3686 * DEPRECATED: switch User-Name { ...
3687 * ALLOWED : switch &User-Name { ...
3689 if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3690 (c->name[0] != '&')) {
3691 WARN("%s[%d]: Please change %s to &%s",
3692 cf_section_filename(g->cs),
3693 cf_section_lineno(g->cs),
3698 if (!modcall_pass2(g->children)) return false;
3699 g->done_pass2 = true;
3703 g = mod_callabletogroup(c);
3704 if (g->done_pass2) goto do_next;
3706 name2 = cf_section_name2(g->cs);
3708 c->debug_name = unlang_keyword[c->type];
3710 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3713 rad_assert(c->parent != NULL);
3714 rad_assert(c->parent->type == MOD_SWITCH);
3717 * The statement may refer to an
3718 * attribute which doesn't exist until
3719 * all of the modules have been loaded.
3720 * Check for that now.
3722 if (!g->vpt && c->name &&
3723 (c->name[0] == '&') &&
3724 (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3725 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3726 cf_section_name2_type(g->cs),
3727 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3728 if (slen < 0) goto parse_error;
3732 * We have "case {...}". There's no
3733 * argument, so we don't need to check
3736 if (!g->vpt) goto do_children;
3739 * Do type-specific checks on the case statement
3741 if (g->vpt->type == TMPL_TYPE_LITERAL) {
3744 f = mod_callabletogroup(mc->parent);
3745 rad_assert(f->vpt != NULL);
3748 * We're switching over an
3749 * attribute. Check that the
3752 if (f->vpt->type == TMPL_TYPE_ATTR) {
3753 rad_assert(f->vpt->tmpl_da != NULL);
3755 if (tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da) < 0) {
3756 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3766 * Compile and sanity check xlat
3769 if (g->vpt->type == TMPL_TYPE_XLAT) {
3772 f = mod_callabletogroup(mc->parent);
3773 rad_assert(f->vpt != NULL);
3776 * Don't expand xlat's into an
3777 * attribute of a different type.
3779 if (f->vpt->type == TMPL_TYPE_ATTR) {
3780 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3781 &g->vpt, true, f->vpt->tmpl_da)) {
3785 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3786 &g->vpt, true, NULL)) {
3792 if (!modcall_pass2(g->children)) return false;
3793 g->done_pass2 = true;
3797 g = mod_callabletogroup(c);
3798 if (g->done_pass2) goto do_next;
3800 name2 = cf_section_name2(g->cs);
3801 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3804 * Already parsed, handle the children.
3806 if (g->vpt) goto check_children;
3809 * We had &Foo-Bar, where Foo-Bar is
3810 * defined by a module.
3812 rad_assert(c->name != NULL);
3813 rad_assert(c->name[0] == '&');
3814 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3817 * The statement may refer to an
3818 * attribute which doesn't exist until
3819 * all of the modules have been loaded.
3820 * Check for that now.
3822 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3823 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3824 if (slen < 0) goto parse_error;
3827 rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST));
3828 if (g->vpt->tmpl_num != NUM_ALL) {
3829 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3832 if (!modcall_pass2(g->children)) return false;
3833 g->done_pass2 = true;
3837 c->debug_name = unlang_keyword[c->type];
3841 g = mod_callabletogroup(c);
3842 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], cf_section_name1(g->cs));
3847 case MOD_LOAD_BALANCE:
3848 case MOD_REDUNDANT_LOAD_BALANCE:
3849 c->debug_name = unlang_keyword[c->type];
3854 g = mod_callabletogroup(c);
3856 c->debug_name = mc->name; /* for authorize, etc. */
3858 } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3859 char const *name1 = cf_section_name1(g->cs);
3861 if (strcmp(name1, unlang_keyword[c->type]) != 0) {
3862 c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3866 if (g->done_pass2) goto do_next;
3867 if (!modcall_pass2(g->children)) return false;
3868 g->done_pass2 = true;
3873 rad_assert(c->debug_name != NULL);
3879 void modcall_debug(modcallable *mc, int depth)
3886 for (this = mc; this != NULL; this = this->next) {
3887 switch (this->type) {
3892 modsingle *single = mod_callabletosingle(this);
3894 DEBUG("%.*s%s", depth, modcall_spaces,
3895 single->modinst->name);
3901 g = mod_callabletogroup(this);
3902 DEBUG("%.*s%s {", depth, modcall_spaces,
3903 unlang_keyword[this->type]);
3905 for (map = g->map; map != NULL; map = map->next) {
3906 map_prints(buffer, sizeof(buffer), map);
3907 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3910 DEBUG("%.*s}", depth, modcall_spaces);
3914 g = mod_callabletogroup(this);
3915 DEBUG("%.*s%s {", depth, modcall_spaces,
3916 unlang_keyword[this->type]);
3917 modcall_debug(g->children, depth + 1);
3918 DEBUG("%.*s}", depth, modcall_spaces);
3923 g = mod_callabletogroup(this);
3924 fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3925 DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3926 unlang_keyword[this->type], buffer);
3927 modcall_debug(g->children, depth + 1);
3928 DEBUG("%.*s}", depth, modcall_spaces);
3933 g = mod_callabletogroup(this);
3934 tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3935 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3936 unlang_keyword[this->type], buffer);
3937 modcall_debug(g->children, depth + 1);
3938 DEBUG("%.*s}", depth, modcall_spaces);
3943 g = mod_callabletogroup(this);
3944 DEBUG("%.*s%s %s {", depth, modcall_spaces,
3945 unlang_keyword[this->type], this->name);
3946 modcall_debug(g->children, depth + 1);
3947 DEBUG("%.*s}", depth, modcall_spaces);
3951 DEBUG("%.*sbreak", depth, modcall_spaces);
3956 g = mod_callabletogroup(this);
3957 DEBUG("%.*s%s {", depth, modcall_spaces,
3958 unlang_keyword[this->type]);
3959 modcall_debug(g->children, depth + 1);
3960 DEBUG("%.*s}", depth, modcall_spaces);
3964 case MOD_LOAD_BALANCE:
3965 case MOD_REDUNDANT_LOAD_BALANCE:
3966 g = mod_callabletogroup(this);
3967 DEBUG("%.*s%s {", depth, modcall_spaces,
3968 unlang_keyword[this->type]);
3969 modcall_debug(g->children, depth + 1);
3970 DEBUG("%.*s}", depth, modcall_spaces);