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
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modpriv.h>
28 #include <freeradius-devel/modcall.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 *,
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/README.failover */
49 struct modcallable *next;
51 enum { MOD_SINGLE = 1, MOD_GROUP, MOD_LOAD_BALANCE, MOD_REDUNDANT_LOAD_BALANCE, MOD_IF, MOD_ELSE, MOD_ELSIF, MOD_UPDATE, MOD_SWITCH, MOD_CASE } type;
53 int actions[RLM_MODULE_NUMCODES];
56 #define GROUPTYPE_SIMPLE 0
57 #define GROUPTYPE_REDUNDANT 1
58 #define GROUPTYPE_APPEND 2
59 #define GROUPTYPE_COUNT 3
62 modcallable mc; /* self */
63 int grouptype; /* after mc */
64 modcallable *children;
71 module_instance_t *modinst;
74 static const FR_NAME_NUMBER grouptype_table[] = {
75 { "", GROUPTYPE_SIMPLE },
76 { "redundant ", GROUPTYPE_REDUNDANT },
77 { "append ", GROUPTYPE_APPEND },
81 /* Simple conversions: modsingle and modgroup are subclasses of modcallable,
82 * so we often want to go back and forth between them. */
83 static modsingle *mod_callabletosingle(modcallable *p)
85 rad_assert(p->type==MOD_SINGLE);
86 return (modsingle *)p;
88 static modgroup *mod_callabletogroup(modcallable *p)
90 rad_assert((p->type > MOD_SINGLE) && (p->type <= MOD_CASE));
94 static modcallable *mod_singletocallable(modsingle *p)
96 return (modcallable *)p;
98 static modcallable *mod_grouptocallable(modgroup *p)
100 return (modcallable *)p;
103 /* modgroups are grown by adding a modcallable to the end */
104 static void add_child(modgroup *g, modcallable *c)
106 modcallable **head = &g->children;
107 modcallable *node = *head;
108 modcallable **last = head;
117 rad_assert(c->next == NULL);
119 c->parent = mod_grouptocallable(g);
122 /* Here's where we recognize all of our keywords: first the rcodes, then the
124 static const FR_NAME_NUMBER rcode_table[] = {
125 { "reject", RLM_MODULE_REJECT },
126 { "fail", RLM_MODULE_FAIL },
127 { "ok", RLM_MODULE_OK },
128 { "handled", RLM_MODULE_HANDLED },
129 { "invalid", RLM_MODULE_INVALID },
130 { "userlock", RLM_MODULE_USERLOCK },
131 { "notfound", RLM_MODULE_NOTFOUND },
132 { "noop", RLM_MODULE_NOOP },
133 { "updated", RLM_MODULE_UPDATED },
139 * Compile action && rcode for later use.
141 static int compile_action(modcallable *c, CONF_PAIR *cp)
144 const char *attr, *value;
146 attr = cf_pair_attr(cp);
147 value = cf_pair_value(cp);
148 if (!value) return 0;
150 if (!strcasecmp(value, "return"))
151 action = MOD_ACTION_RETURN;
153 else if (!strcasecmp(value, "reject"))
154 action = MOD_ACTION_REJECT;
156 else if (strspn(value, "0123456789")==strlen(value)) {
157 action = atoi(value);
160 * Don't allow priority zero, for future use.
162 if (action == 0) return 0;
164 cf_log_err(cf_pairtoitem(cp), "Unknown action '%s'.\n",
169 if (strcasecmp(attr, "default") != 0) {
172 rcode = fr_str2int(rcode_table, attr, -1);
174 cf_log_err(cf_pairtoitem(cp),
175 "Unknown module rcode '%s'.\n",
179 c->actions[rcode] = action;
181 } else { /* set all unset values to the default */
184 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
185 if (!c->actions[i]) c->actions[i] = action;
192 /* Some short names for debugging output */
193 static const char * const comp2str[] = {
204 #ifdef HAVE_PTHREAD_H
206 * Lock the mutex for the module
208 static void safe_lock(module_instance_t *instance)
211 pthread_mutex_lock(instance->mutex);
215 * Unlock the mutex for the module
217 static void safe_unlock(module_instance_t *instance)
220 pthread_mutex_unlock(instance->mutex);
224 * No threads: these functions become NULL's.
226 #define safe_lock(foo)
227 #define safe_unlock(foo)
230 static int call_modsingle(int component, modsingle *sp, REQUEST *request,
233 int myresult = default_result;
235 DEBUG3(" modsingle[%s]: calling %s (%s) for request %d",
236 comp2str[component], sp->modinst->name,
237 sp->modinst->entry->name, request->number);
238 safe_lock(sp->modinst);
241 * For logging unresponsive children.
243 request->module = sp->modinst->name;
245 myresult = sp->modinst->entry->module->methods[component](
246 sp->modinst->insthandle, request);
248 request->module = "<server-core>";
249 safe_unlock(sp->modinst);
250 DEBUG3(" modsingle[%s]: returned from %s (%s) for request %d",
251 comp2str[component], sp->modinst->name,
252 sp->modinst->entry->name, request->number);
258 static int default_component_results[RLM_COMPONENT_COUNT] = {
259 RLM_MODULE_REJECT, /* AUTH */
260 RLM_MODULE_NOTFOUND, /* AUTZ */
261 RLM_MODULE_NOOP, /* PREACCT */
262 RLM_MODULE_NOOP, /* ACCT */
263 RLM_MODULE_FAIL, /* SESS */
264 RLM_MODULE_NOOP, /* PRE_PROXY */
265 RLM_MODULE_NOOP, /* POST_PROXY */
266 RLM_MODULE_NOOP /* POST_AUTH */
270 static const char *group_name[] = {
274 "load-balance group",
275 "redundant-load-balance group",
284 static const char *modcall_spaces = "++++++++++++++++++++++++++++++++";
286 #define MODCALL_STACK_MAX (32)
289 * Don't call the modules recursively. Instead, do them
290 * iteratively, and manage the call stack ourselves.
292 typedef struct modcall_stack {
295 int priority[MODCALL_STACK_MAX];
296 int result[MODCALL_STACK_MAX];
297 modcallable *children[MODCALL_STACK_MAX];
298 modcallable *start[MODCALL_STACK_MAX];
303 * Call a module, iteratively, with a local stack, rather than
304 * recursively. What did Paul Graham say about Lisp...?
306 int modcall(int component, modcallable *c, REQUEST *request)
310 modcallable *parent, *child;
312 int if_taken, was_if;
314 if ((component < 0) || (component >= RLM_COMPONENT_COUNT)) {
315 return RLM_MODULE_FAIL;
319 stack.priority[0] = 0;
320 stack.children[0] = c;
321 myresult = stack.result[0] = default_component_results[component];
322 was_if = if_taken = FALSE;
326 * A module has taken too long to process the request,
327 * and we've been told to stop processing it.
329 if ((request->master_state == REQUEST_STOP_PROCESSING) ||
331 (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
332 myresult = RLM_MODULE_FAIL;
336 child = stack.children[stack.pointer];
338 myresult = stack.result[stack.pointer];
341 parent = child->parent;
343 if ((child->type == MOD_ELSE) || (child->type == MOD_ELSIF)) {
344 myresult = stack.result[stack.pointer];
346 if (!was_if) { /* error */
347 DEBUG2("%.*s ... skipping %s for request %d: No preceding \"if\"",
348 stack.pointer + 1, modcall_spaces,
349 group_name[child->type],
354 DEBUG2("%.*s ... skipping %s for request %d: Preceding \"if\" was taken",
355 stack.pointer + 1, modcall_spaces,
356 group_name[child->type],
363 * "if" or "elsif". Evaluate the condition.
365 if ((child->type == MOD_IF) || (child->type == MOD_ELSIF)) {
366 int condition = TRUE;
367 const char *p = child->name;
369 DEBUG2("%.*s? %s %s",
370 stack.pointer + 1, modcall_spaces,
371 (child->type == MOD_IF) ? "if" : "elsif",
374 if (radius_evaluate_condition(request, myresult,
375 0, &p, TRUE, &condition)) {
376 DEBUG2("%.*s? %s %s -> %s",
377 stack.pointer + 1, modcall_spaces,
378 (child->type == MOD_IF) ? "if" : "elsif",
379 child->name, (condition != FALSE) ? "TRUE" : "FALSE");
382 * This should never happen, the
383 * condition is checked when the
384 * module section is loaded.
390 stack.result[stack.pointer] = myresult;
391 stack.children[stack.pointer] = NULL;
395 } /* else process it as a simple group */
398 if (child->type == MOD_UPDATE) {
400 modgroup *g = mod_callabletogroup(child);
402 rcode = radius_update_attrlist(request, g->cs,
403 g->vps, child->name);
404 if (rcode != RLM_MODULE_UPDATED) {
411 * Child is a group that has children of it's own.
413 if (child->type != MOD_SINGLE) {
415 modcallable *p, *q, *null_case;
416 modgroup *g = mod_callabletogroup(child);
421 * Catastrophic error. This SHOULD have
422 * been caught when we were reading in the
427 if (stack.pointer >= MODCALL_STACK_MAX) {
428 radlog(L_ERR, "Internal sanity check failed: module stack is too deep");
432 stack.priority[stack.pointer] = 0;
433 stack.result[stack.pointer] = default_component_results[component];
434 switch (child->type) {
442 stack.children[stack.pointer] = g->children;
446 * See the "camel book" for why
449 * If (rand(0..n) < 1), pick the
450 * current realm. We add a scale
451 * factor of 65536, to avoid
454 case MOD_LOAD_BALANCE:
455 case MOD_REDUNDANT_LOAD_BALANCE:
457 for(p = g->children; p; p = p->next) {
466 if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
470 stack.children[stack.pointer] = q;
474 radius_xlat(buffer, sizeof(buffer),
475 child->name, request, NULL);
477 null_case = q = NULL;
478 for(p = g->children; p; p = p->next) {
480 if (!null_case) null_case = p;
483 if (strcmp(buffer, p->name) == 0) {
489 if (!q) q = null_case;
491 stack.children[stack.pointer] = q;
495 DEBUG2("Internal sanity check failed in modcall %d", child->type);
496 exit(1); /* internal sanity check failure */
501 stack.start[stack.pointer] = stack.children[stack.pointer];
503 DEBUG2("%.*s- entering %s %s",
504 stack.pointer, modcall_spaces,
505 group_name[child->type],
506 child->name ? child->name : "");
509 * Catch the special case of a NULL group.
511 if (!stack.children[stack.pointer]) {
513 * Print message for NULL group
515 DEBUG2("%.*s- %s %s returns %s",
516 stack.pointer + 1, modcall_spaces,
517 group_name[child->type],
518 child->name ? child->name : "",
519 fr_int2str(rcode_table,
520 stack.result[stack.pointer],
526 * The child may be a group, so we want to
527 * recurse into it's children, rather than
528 * falling through to the code below.
534 * Process a stand-alone child, and fall through
535 * to dealing with it's parent.
537 sp = mod_callabletosingle(child);
539 myresult = call_modsingle(child->method, sp, request,
540 default_component_results[component]);
542 DEBUG2("%.*s[%s] returns %s",
543 stack.pointer + 1, modcall_spaces,
544 child->name ? child->name : "",
545 fr_int2str(rcode_table, myresult, "??"));
549 * FIXME: Allow modules to push a modcallable
550 * onto this stack. This should simplify
551 * configuration a LOT!
553 * Once we do that, we can't do load-time
554 * checking of the maximum stack depth, and we've
555 * got to cache the stack pointer before storing
558 * Also, if the stack changed, we need to set
559 * children[ptr] to NULL, and process the next
560 * entry on the stack, rather than falling
561 * through to finalize the processing of this
564 * Don't put "myresult" on the stack here,
565 * we have to do so with priority.
569 * We roll back up the stack at this point.
573 * The child's action says return. Do so.
575 if (child->actions[myresult] == MOD_ACTION_RETURN) {
576 stack.result[stack.pointer] = myresult;
577 stack.children[stack.pointer] = NULL;
582 * If "reject", break out of the loop and return
585 if (child->actions[myresult] == MOD_ACTION_REJECT) {
586 stack.children[stack.pointer] = NULL;
587 stack.result[stack.pointer] = RLM_MODULE_REJECT;
592 * Otherwise, the action is a number, the
593 * preference level of this return code. If no
594 * higher preference has been seen yet, remember
597 if (child->actions[myresult] >= stack.priority[stack.pointer]) {
598 stack.result[stack.pointer] = myresult;
599 stack.priority[stack.pointer] = child->actions[myresult];
605 * No parent, we must be done.
608 rad_assert(stack.pointer == 0);
609 myresult = stack.result[0];
613 rad_assert(child != NULL);
616 * Go to the "next" child, whatever that is.
618 switch (parent->type) {
624 stack.children[stack.pointer] = child->next;
628 case MOD_LOAD_BALANCE:
629 stack.children[stack.pointer] = NULL;
632 case MOD_REDUNDANT_LOAD_BALANCE:
634 stack.children[stack.pointer] = child->next;
636 modgroup *g = mod_callabletogroup(parent);
638 stack.children[stack.pointer] = g->children;
640 if (stack.children[stack.pointer] == stack.start[stack.pointer]) {
641 stack.children[stack.pointer] = NULL;
645 DEBUG2("Internal sanity check failed in modcall next %d", child->type);
650 * No child, we're done this group, and we return
651 * "myresult" to the caller by pushing it back up
654 if (!stack.children[stack.pointer]) {
656 rad_assert(stack.pointer > 0);
657 myresult = stack.result[stack.pointer];
660 if (stack.pointer == 0) break;
662 DEBUG2("%.*s- %s %s returns %s",
663 stack.pointer + 1, modcall_spaces,
664 group_name[parent->type],
665 parent->name ? parent->name : "",
666 fr_int2str(rcode_table, myresult, "??"));
668 if ((parent->type == MOD_IF) ||
669 (parent->type == MOD_ELSIF)) {
670 if_taken = was_if = TRUE;
672 if_taken = was_if = FALSE;
678 child = stack.children[stack.pointer];
679 parent = child->parent;
683 } /* loop until done */
690 static const char *action2str(int action)
693 if(action==MOD_ACTION_RETURN)
695 if(action==MOD_ACTION_REJECT)
697 snprintf(buf, sizeof buf, "%d", action);
701 /* If you suspect a bug in the parser, you'll want to use these dump
702 * functions. dump_tree should reproduce a whole tree exactly as it was found
703 * in radiusd.conf, but in long form (all actions explicitly defined) */
704 static void dump_mc(modcallable *c, int indent)
708 if(c->type==MOD_SINGLE) {
709 modsingle *single = mod_callabletosingle(c);
710 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
711 single->modinst->name);
713 modgroup *g = mod_callabletogroup(c);
715 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
716 group_name[c->type]);
717 for(p = g->children;p;p = p->next)
718 dump_mc(p, indent+1);
721 for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
722 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
723 fr_int2str(rcode_table, i, "??"),
724 action2str(c->actions[i]));
727 DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
730 static void dump_tree(int comp, modcallable *c)
732 DEBUG("[%s]", comp2str[comp]);
736 #define dump_tree(a, b)
739 /* These are the default actions. For each component, the group{} block
740 * behaves like the code from the old module_*() function. redundant{} and
741 * append{} are based on my guesses of what they will be used for. --Pac. */
743 defaultactions[RLM_COMPONENT_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
749 MOD_ACTION_RETURN, /* reject */
751 MOD_ACTION_RETURN, /* ok */
752 MOD_ACTION_RETURN, /* handled */
754 MOD_ACTION_RETURN, /* userlock */
755 MOD_ACTION_RETURN, /* notfound */
761 MOD_ACTION_RETURN, /* reject */
763 MOD_ACTION_RETURN, /* ok */
764 MOD_ACTION_RETURN, /* handled */
765 MOD_ACTION_RETURN, /* invalid */
766 MOD_ACTION_RETURN, /* userlock */
767 MOD_ACTION_RETURN, /* notfound */
768 MOD_ACTION_RETURN, /* noop */
769 MOD_ACTION_RETURN /* updated */
773 MOD_ACTION_RETURN, /* reject */
775 MOD_ACTION_RETURN, /* ok */
776 MOD_ACTION_RETURN, /* handled */
777 MOD_ACTION_RETURN, /* invalid */
778 MOD_ACTION_RETURN, /* userlock */
780 MOD_ACTION_RETURN, /* noop */
781 MOD_ACTION_RETURN /* updated */
788 MOD_ACTION_RETURN, /* reject */
789 MOD_ACTION_RETURN, /* fail */
791 MOD_ACTION_RETURN, /* handled */
792 MOD_ACTION_RETURN, /* invalid */
793 MOD_ACTION_RETURN, /* userlock */
800 MOD_ACTION_RETURN, /* reject */
802 MOD_ACTION_RETURN, /* ok */
803 MOD_ACTION_RETURN, /* handled */
804 MOD_ACTION_RETURN, /* invalid */
805 MOD_ACTION_RETURN, /* userlock */
806 MOD_ACTION_RETURN, /* notfound */
807 MOD_ACTION_RETURN, /* noop */
808 MOD_ACTION_RETURN /* updated */
812 MOD_ACTION_RETURN, /* reject */
814 MOD_ACTION_RETURN, /* ok */
815 MOD_ACTION_RETURN, /* handled */
816 MOD_ACTION_RETURN, /* invalid */
817 MOD_ACTION_RETURN, /* userlock */
819 MOD_ACTION_RETURN, /* noop */
820 MOD_ACTION_RETURN /* updated */
827 MOD_ACTION_RETURN, /* reject */
828 MOD_ACTION_RETURN, /* fail */
830 MOD_ACTION_RETURN, /* handled */
831 MOD_ACTION_RETURN, /* invalid */
832 MOD_ACTION_RETURN, /* userlock */
833 MOD_ACTION_RETURN, /* notfound */
839 MOD_ACTION_RETURN, /* reject */
841 MOD_ACTION_RETURN, /* ok */
842 MOD_ACTION_RETURN, /* handled */
843 MOD_ACTION_RETURN, /* invalid */
844 MOD_ACTION_RETURN, /* userlock */
845 MOD_ACTION_RETURN, /* notfound */
846 MOD_ACTION_RETURN, /* noop */
847 MOD_ACTION_RETURN /* updated */
851 MOD_ACTION_RETURN, /* reject */
853 MOD_ACTION_RETURN, /* ok */
854 MOD_ACTION_RETURN, /* handled */
855 MOD_ACTION_RETURN, /* invalid */
856 MOD_ACTION_RETURN, /* userlock */
858 MOD_ACTION_RETURN, /* noop */
859 MOD_ACTION_RETURN /* updated */
866 MOD_ACTION_RETURN, /* reject */
867 MOD_ACTION_RETURN, /* fail */
869 MOD_ACTION_RETURN, /* handled */
870 MOD_ACTION_RETURN, /* invalid */
871 MOD_ACTION_RETURN, /* userlock */
872 MOD_ACTION_RETURN, /* notfound */
880 MOD_ACTION_RETURN, /* ok */
881 MOD_ACTION_RETURN, /* handled */
890 MOD_ACTION_RETURN, /* reject */
892 MOD_ACTION_RETURN, /* ok */
893 MOD_ACTION_RETURN, /* handled */
894 MOD_ACTION_RETURN, /* invalid */
895 MOD_ACTION_RETURN, /* userlock */
897 MOD_ACTION_RETURN, /* noop */
898 MOD_ACTION_RETURN /* updated */
905 MOD_ACTION_RETURN, /* reject */
907 MOD_ACTION_RETURN, /* ok */
908 MOD_ACTION_RETURN, /* handled */
909 MOD_ACTION_RETURN, /* invalid */
910 MOD_ACTION_RETURN, /* userlock */
911 MOD_ACTION_RETURN, /* notfound */
912 MOD_ACTION_RETURN, /* noop */
913 MOD_ACTION_RETURN /* updated */
917 MOD_ACTION_RETURN, /* reject */
919 MOD_ACTION_RETURN, /* ok */
920 MOD_ACTION_RETURN, /* handled */
921 MOD_ACTION_RETURN, /* invalid */
922 MOD_ACTION_RETURN, /* userlock */
923 MOD_ACTION_RETURN, /* notfound */
924 MOD_ACTION_RETURN, /* noop */
925 MOD_ACTION_RETURN /* updated */
929 MOD_ACTION_RETURN, /* reject */
931 MOD_ACTION_RETURN, /* ok */
932 MOD_ACTION_RETURN, /* handled */
933 MOD_ACTION_RETURN, /* invalid */
934 MOD_ACTION_RETURN, /* userlock */
935 MOD_ACTION_RETURN, /* notfound */
936 MOD_ACTION_RETURN, /* noop */
937 MOD_ACTION_RETURN /* updated */
944 MOD_ACTION_RETURN, /* reject */
945 MOD_ACTION_RETURN, /* fail */
947 MOD_ACTION_RETURN, /* handled */
948 MOD_ACTION_RETURN, /* invalid */
949 MOD_ACTION_RETURN, /* userlock */
956 MOD_ACTION_RETURN, /* reject */
958 MOD_ACTION_RETURN, /* ok */
959 MOD_ACTION_RETURN, /* handled */
960 MOD_ACTION_RETURN, /* invalid */
961 MOD_ACTION_RETURN, /* userlock */
962 MOD_ACTION_RETURN, /* notfound */
963 MOD_ACTION_RETURN, /* noop */
964 MOD_ACTION_RETURN /* updated */
968 MOD_ACTION_RETURN, /* reject */
970 MOD_ACTION_RETURN, /* ok */
971 MOD_ACTION_RETURN, /* handled */
972 MOD_ACTION_RETURN, /* invalid */
973 MOD_ACTION_RETURN, /* userlock */
975 MOD_ACTION_RETURN, /* noop */
976 MOD_ACTION_RETURN /* updated */
983 MOD_ACTION_RETURN, /* reject */
984 MOD_ACTION_RETURN, /* fail */
986 MOD_ACTION_RETURN, /* handled */
987 MOD_ACTION_RETURN, /* invalid */
988 MOD_ACTION_RETURN, /* userlock */
995 MOD_ACTION_RETURN, /* reject */
997 MOD_ACTION_RETURN, /* ok */
998 MOD_ACTION_RETURN, /* handled */
999 MOD_ACTION_RETURN, /* invalid */
1000 MOD_ACTION_RETURN, /* userlock */
1001 MOD_ACTION_RETURN, /* notfound */
1002 MOD_ACTION_RETURN, /* noop */
1003 MOD_ACTION_RETURN /* updated */
1007 MOD_ACTION_RETURN, /* reject */
1009 MOD_ACTION_RETURN, /* ok */
1010 MOD_ACTION_RETURN, /* handled */
1011 MOD_ACTION_RETURN, /* invalid */
1012 MOD_ACTION_RETURN, /* userlock */
1014 MOD_ACTION_RETURN, /* noop */
1015 MOD_ACTION_RETURN /* updated */
1022 MOD_ACTION_RETURN, /* reject */
1023 MOD_ACTION_RETURN, /* fail */
1025 MOD_ACTION_RETURN, /* handled */
1026 MOD_ACTION_RETURN, /* invalid */
1027 MOD_ACTION_RETURN, /* userlock */
1034 MOD_ACTION_RETURN, /* reject */
1036 MOD_ACTION_RETURN, /* ok */
1037 MOD_ACTION_RETURN, /* handled */
1038 MOD_ACTION_RETURN, /* invalid */
1039 MOD_ACTION_RETURN, /* userlock */
1040 MOD_ACTION_RETURN, /* notfound */
1041 MOD_ACTION_RETURN, /* noop */
1042 MOD_ACTION_RETURN /* updated */
1046 MOD_ACTION_RETURN, /* reject */
1048 MOD_ACTION_RETURN, /* ok */
1049 MOD_ACTION_RETURN, /* handled */
1050 MOD_ACTION_RETURN, /* invalid */
1051 MOD_ACTION_RETURN, /* userlock */
1053 MOD_ACTION_RETURN, /* noop */
1054 MOD_ACTION_RETURN /* updated */
1060 static modcallable *do_compile_modupdate(modcallable *parent,
1061 int component, CONF_SECTION *cs,
1066 modcallable *csingle;
1068 VALUE_PAIR *head, **tail;
1070 static const char *attrlist_names[] = {
1071 "request", "reply", "proxy-request", "proxy-reply",
1072 "config", "control",
1073 "outer.request", "outer.reply",
1074 "outer.config", "outer.control",
1078 component = component; /* -Wunused */
1080 if (!cf_section_name2(cs)) {
1081 cf_log_err(cf_sectiontoitem(cs),
1082 "Require list name for 'update'.\n");
1086 for (i = 0; attrlist_names[i] != NULL; i++) {
1087 if (strcmp(name2, attrlist_names[i]) == 0) {
1094 cf_log_err(cf_sectiontoitem(cs),
1095 "Unknown attribute list \"%s\"",
1104 * Walk through the children of the update section,
1105 * ensuring that they're all known attributes.
1107 for (ci=cf_item_find_next(cs, NULL);
1109 ci=cf_item_find_next(cs, ci)) {
1113 if (cf_item_is_section(ci)) {
1114 cf_log_err(ci, "\"update\" sections cannot have subsections");
1118 if (!cf_item_is_pair(ci)) continue;
1120 cp = cf_itemtopair(ci); /* can't return NULL */
1121 vp = cf_pairtovp(cp);
1124 cf_log_err(ci, "ERROR: %s", librad_errstr);
1128 if ((vp->operator != T_OP_EQ) &&
1129 (vp->operator != T_OP_CMP_EQ) &&
1130 (vp->operator != T_OP_ADD) &&
1131 (vp->operator != T_OP_SUB) &&
1132 (vp->operator != T_OP_LE) &&
1133 (vp->operator != T_OP_GE) &&
1134 (vp->operator != T_OP_SET)) {
1136 cf_log_err(ci, "Invalid operator for attribute");
1141 * A few more sanity checks. The enforcement of
1142 * <= or >= can only happen for integer
1145 if ((vp->operator == T_OP_LE) ||
1146 (vp->operator == T_OP_GE)) {
1147 if ((vp->type != PW_TYPE_BYTE) &&
1148 (vp->type != PW_TYPE_SHORT) &&
1149 (vp->type != PW_TYPE_INTEGER)) {
1151 cf_log_err(ci, "Enforcment of <= or >= is possible only for integer attributes");
1161 cf_log_err(cf_sectiontoitem(cs),
1162 "ERROR: update %s section cannot be empty",
1167 g = rad_malloc(sizeof(*g)); /* never fails */
1168 memset(g, 0, sizeof(*g));
1169 csingle = mod_grouptocallable(g);
1171 csingle->parent = parent;
1172 csingle->next = NULL;
1173 csingle->name = name2;
1174 csingle->type = MOD_UPDATE;
1175 csingle->method = component;
1177 g->grouptype = GROUPTYPE_SIMPLE;
1186 static modcallable *do_compile_modswitch(modcallable *parent,
1187 int component, CONF_SECTION *cs)
1189 modcallable *csingle;
1191 int had_seen_default = FALSE;
1193 component = component; /* -Wunused */
1195 if (!cf_section_name2(cs)) {
1196 cf_log_err(cf_sectiontoitem(cs),
1197 "You must specify a variable to switch over for 'switch'.");
1201 if (!cf_item_find_next(cs, NULL)) {
1202 cf_log_err(cf_sectiontoitem(cs), "'switch' statments cannot be empty.");
1207 * Walk through the children of the switch section,
1208 * ensuring that they're all 'case' statements
1210 for (ci=cf_item_find_next(cs, NULL);
1212 ci=cf_item_find_next(cs, ci)) {
1213 CONF_SECTION *subcs;
1214 const char *name1, *name2;
1216 if (!cf_item_is_section(ci)) {
1217 if (!cf_item_is_pair(ci)) continue;
1219 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1223 subcs = cf_itemtosection(ci); /* can't return NULL */
1224 name1 = cf_section_name1(subcs);
1226 if (strcmp(name1, "case") != 0) {
1227 cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1231 name2 = cf_section_name2(subcs);
1232 if (!name2 && !had_seen_default) {
1233 had_seen_default = TRUE;
1237 if (!name2 || (name2[0] == '\0')) {
1238 cf_log_err(ci, "\"case\" sections must have a name");
1243 csingle= do_compile_modgroup(parent, component, cs,
1244 GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE);
1245 if (!csingle) return NULL;
1246 csingle->type = MOD_SWITCH;
1251 * redundant, etc. can refer to modules or groups, but not much else.
1253 static int all_children_are_modules(CONF_SECTION *cs, const char *name)
1257 for (ci=cf_item_find_next(cs, NULL);
1259 ci=cf_item_find_next(cs, ci)) {
1261 * If we're a redundant, etc. group, then the
1262 * intention is to call modules, rather than
1263 * processing logic. These checks aren't
1264 * *strictly* necessary, but they keep the users
1265 * from doing crazy things.
1267 if (cf_item_is_section(ci)) {
1268 CONF_SECTION *subcs = cf_itemtosection(ci);
1269 const char *name1 = cf_section_name1(subcs);
1271 if ((strcmp(name1, "if") == 0) ||
1272 (strcmp(name1, "else") == 0) ||
1273 (strcmp(name1, "elsif") == 0) ||
1274 (strcmp(name1, "update") == 0) ||
1275 (strcmp(name1, "switch") == 0) ||
1276 (strcmp(name1, "case") == 0)) {
1277 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
1284 if (cf_item_is_pair(ci)) {
1285 CONF_PAIR *cp = cf_itemtopair(ci);
1286 if (cf_pair_value(cp) != NULL) {
1287 cf_log_err(ci, "Invalid entry in %s section");
1298 * Compile one entry of a module call.
1300 static modcallable *do_compile_modsingle(modcallable *parent,
1301 int component, CONF_ITEM *ci,
1303 const char **modname)
1306 const char *modrefname;
1308 modcallable *csingle;
1309 module_instance_t *this;
1310 CONF_SECTION *cs, *subcs, *modules;
1312 if (cf_item_is_section(ci)) {
1313 cs = cf_itemtosection(ci);
1314 const char *name2 = cf_section_name2(cs);
1316 modrefname = cf_section_name1(cs);
1317 if (!name2) name2 = "_UnNamedGroup";
1320 * group{}, redundant{}, or append{} may appear
1321 * where a single module instance was expected.
1322 * In that case, we hand it off to
1325 if (strcmp(modrefname, "group") == 0) {
1327 return do_compile_modgroup(parent, component, cs,
1331 } else if (strcmp(modrefname, "redundant") == 0) {
1334 if (!all_children_are_modules(cs, modrefname)) {
1338 return do_compile_modgroup(parent, component, cs,
1339 GROUPTYPE_REDUNDANT,
1342 } else if (strcmp(modrefname, "append") == 0) {
1344 return do_compile_modgroup(parent, component, cs,
1348 } else if (strcmp(modrefname, "load-balance") == 0) {
1351 if (!all_children_are_modules(cs, modrefname)) {
1355 csingle= do_compile_modgroup(parent, component, cs,
1358 if (!csingle) return NULL;
1359 csingle->type = MOD_LOAD_BALANCE;
1362 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
1365 if (!all_children_are_modules(cs, modrefname)) {
1369 csingle= do_compile_modgroup(parent, component, cs,
1370 GROUPTYPE_REDUNDANT,
1372 if (!csingle) return NULL;
1373 csingle->type = MOD_REDUNDANT_LOAD_BALANCE;
1376 } else if (strcmp(modrefname, "if") == 0) {
1377 if (!cf_section_name2(cs)) {
1378 cf_log_err(ci, "'if' without condition.");
1383 csingle= do_compile_modgroup(parent, component, cs,
1386 if (!csingle) return NULL;
1387 csingle->type = MOD_IF;
1389 if (!radius_evaluate_condition(NULL, 0, 0, modname,
1391 modcallable_free(&csingle);
1398 } else if (strcmp(modrefname, "elsif") == 0) {
1400 ((parent->type == MOD_LOAD_BALANCE) ||
1401 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
1402 cf_log_err(ci, "'elsif' cannot be used in this section section.");
1406 if (!cf_section_name2(cs)) {
1407 cf_log_err(ci, "'elsif' without condition.");
1412 csingle= do_compile_modgroup(parent, component, cs,
1415 if (!csingle) return NULL;
1416 csingle->type = MOD_ELSIF;
1418 if (!radius_evaluate_condition(NULL, 0, 0, modname,
1420 modcallable_free(&csingle);
1427 } else if (strcmp(modrefname, "else") == 0) {
1429 ((parent->type == MOD_LOAD_BALANCE) ||
1430 (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
1431 cf_log_err(ci, "'else' cannot be used in this section section.");
1435 if (cf_section_name2(cs)) {
1436 cf_log_err(ci, "Cannot have conditions on 'else'.");
1441 csingle= do_compile_modgroup(parent, component, cs,
1444 if (!csingle) return NULL;
1445 csingle->type = MOD_ELSE;
1448 } else if (strcmp(modrefname, "update") == 0) {
1451 csingle = do_compile_modupdate(parent, component, cs,
1453 if (!csingle) return NULL;
1457 } else if (strcmp(modrefname, "switch") == 0) {
1460 csingle = do_compile_modswitch(parent, component, cs);
1461 if (!csingle) return NULL;
1465 } else if (strcmp(modrefname, "case") == 0) {
1471 * FIXME: How to tell that the parent can only
1472 * be a "switch" statement?
1475 cf_log_err(ci, "\"case\" statements may only appear within a \"switch\" section");
1479 csingle= do_compile_modgroup(parent, component, cs,
1482 if (!csingle) return NULL;
1483 csingle->type = MOD_CASE;
1484 csingle->name = cf_section_name2(cs); /* may be NULL */
1487 * Set all of it's codes to return, so that
1488 * when we pick a 'case' statement, we don't
1489 * fall through to processing the next one.
1491 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1492 csingle->actions[i] = MOD_ACTION_RETURN;
1498 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
1502 * Else it's a module reference, with updated return
1506 CONF_PAIR *cp = cf_itemtopair(ci);
1507 modrefname = cf_pair_attr(cp);
1510 * Actions (ok = 1), etc. are orthoganal to just
1511 * about everything else.
1513 if (cf_pair_value(cp) != NULL) {
1514 cf_log_err(ci, "Entry is not a reference to a module");
1520 * See if the module is a virtual one. If so, return that,
1521 * rather than doing anything here.
1523 if (((cs = cf_section_find("instantiate")) != NULL) &&
1524 (subcs = cf_section_sub_find_name2(cs, NULL, modrefname)) != NULL) {
1525 DEBUG2(" Module: Loading virtual module %s", modrefname);
1528 * As it's sole configuration, the
1529 * virtual module takes a section which
1532 return do_compile_modsingle(parent,
1534 cf_sectiontoitem(subcs),
1540 * Not a virtual module. It must be a real module.
1542 modules = cf_section_find("modules");
1545 if (modules && cf_section_sub_find_name2(modules, NULL, modrefname)) {
1546 this = find_module_instance(modules, modrefname);
1554 * Maybe it's module.method
1556 p = strchr(modrefname, '.');
1557 if (p) for (i = RLM_COMPONENT_AUTH;
1558 i < RLM_COMPONENT_COUNT;
1560 if (strcmp(p + 1, comp2str[i]) == 0) {
1563 strlcpy(buffer, modrefname, sizeof(buffer));
1564 buffer[p - modrefname] = '\0';
1567 this = find_module_instance(cf_section_find("modules"), buffer);
1569 !this->entry->module->methods[i]) {
1571 cf_log_err(ci, "Module %s has no such method %s", buffer, comp2str[i]);
1580 cf_log_err(ci, "Failed to find module \"%s\".", modrefname);
1586 * We know it's all OK, allocate the structures, and fill
1589 single = rad_malloc(sizeof(*single));
1590 memset(single, 0, sizeof(*single));
1591 csingle = mod_singletocallable(single);
1592 csingle->parent = parent;
1593 csingle->next = NULL;
1594 memcpy(csingle->actions, defaultactions[component][grouptype],
1595 sizeof csingle->actions);
1596 rad_assert(modrefname != NULL);
1597 csingle->name = modrefname;
1598 csingle->type = MOD_SINGLE;
1599 csingle->method = component;
1602 * Singles can override the actions, virtual modules cannot.
1604 * FIXME: We may want to re-visit how to do this...
1605 * maybe a csingle as a ref?
1607 if (cf_item_is_section(ci)) {
1608 cs = cf_itemtosection(ci);
1610 for (ci=cf_item_find_next(cs, NULL);
1612 ci=cf_item_find_next(cs, ci)) {
1614 if (cf_item_is_section(ci)) {
1615 cf_log_err(ci, "Subsection of module instance call not allowed");
1616 modcallable_free(&csingle);
1620 if (!cf_item_is_pair(ci)) continue;
1622 if (!compile_action(csingle, cf_itemtopair(ci))) {
1623 modcallable_free(&csingle);
1630 * Bail out if the module in question does not supply the
1633 if (!this->entry->module->methods[component]) {
1634 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
1635 comp2str[component]);
1636 modcallable_free(&csingle);
1640 single->modinst = this;
1641 *modname = this->entry->module->name;
1645 modcallable *compile_modsingle(modcallable *parent,
1646 int component, CONF_ITEM *ci,
1647 const char **modname)
1649 modcallable *ret = do_compile_modsingle(parent, component, ci,
1652 dump_tree(component, ret);
1658 * Internal compile group code.
1660 static modcallable *do_compile_modgroup(modcallable *parent,
1661 int component, CONF_SECTION *cs,
1662 int grouptype, int parentgrouptype)
1669 g = rad_malloc(sizeof(*g));
1670 memset(g, 0, sizeof(*g));
1671 g->grouptype = grouptype;
1673 c = mod_grouptocallable(g);
1676 memset(c->actions, 0, sizeof(c->actions));
1679 * Remember the name for printing, etc.
1681 * FIXME: We may also want to put the names into a
1682 * rbtree, so that groups can reference each other...
1684 c->name = cf_section_name2(cs);
1685 if (!c->name) c->name = "";
1686 c->type = MOD_GROUP;
1690 * Loop over the children of this group.
1692 for (ci=cf_item_find_next(cs, NULL);
1694 ci=cf_item_find_next(cs, ci)) {
1697 * Sections are references to other groups, or
1698 * to modules with updated return codes.
1700 if (cf_item_is_section(ci)) {
1701 const char *junk = NULL;
1702 modcallable *single;
1703 CONF_SECTION *subcs = cf_itemtosection(ci);
1705 single = do_compile_modsingle(c, component, ci,
1708 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
1709 cf_section_name1(subcs));
1710 modcallable_free(&c);
1713 add_child(g, single);
1715 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
1719 const char *attr, *value;
1720 CONF_PAIR *cp = cf_itemtopair(ci);
1722 attr = cf_pair_attr(cp);
1723 value = cf_pair_value(cp);
1726 * A CONF_PAIR is either a module
1727 * instance with no actions
1731 modcallable *single;
1732 const char *junk = NULL;
1734 single = do_compile_modsingle(c,
1741 "Failed to parse \"%s\" entry.",
1743 modcallable_free(&c);
1746 add_child(g, single);
1749 * Or a module instance with action.
1751 } else if (!compile_action(c, cp)) {
1752 modcallable_free(&c);
1754 } /* else it worked */
1759 * Set the default actions, if they haven't already been
1762 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1763 if (!c->actions[i]) {
1764 c->actions[i] = defaultactions[component][parentgrouptype][i];
1769 * FIXME: If there are no children, return NULL?
1771 return mod_grouptocallable(g);
1774 modcallable *compile_modgroup(modcallable *parent,
1775 int component, CONF_SECTION *cs)
1777 modcallable *ret = do_compile_modgroup(parent, component, cs,
1780 dump_tree(component, ret);
1784 void add_to_modcallable(modcallable **parent, modcallable *this,
1785 int component, const char *name)
1789 rad_assert(this != NULL);
1791 if (*parent == NULL) {
1794 g = rad_malloc(sizeof *g);
1795 memset(g, 0, sizeof(*g));
1796 g->grouptype = GROUPTYPE_SIMPLE;
1797 c = mod_grouptocallable(g);
1800 defaultactions[component][GROUPTYPE_SIMPLE],
1801 sizeof(c->actions));
1802 rad_assert(name != NULL);
1804 c->type = MOD_GROUP;
1805 c->method = component;
1808 *parent = mod_grouptocallable(g);
1810 g = mod_callabletogroup(*parent);
1816 void modcallable_free(modcallable **pc)
1818 modcallable *c, *loop, *next;
1820 if (c->type != MOD_SINGLE) {
1821 modgroup *g = mod_callabletogroup(c);
1823 for(loop = g->children;
1827 modcallable_free(&loop);