Remove redundant open brace
[freeradius.git] / src / main / modcall.c
1 /*
2  * @name modcall.c
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  */
22
23 RCSID("$Id$")
24
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>
30
31
32 /* mutually-recursive static functions need a prototype up front */
33 static modcallable *do_compile_modgroup(modcallable *,
34                                         rlm_components_t, CONF_SECTION *,
35                                         int, int, int);
36
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)
44
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 */
47 struct modcallable {
48         modcallable *parent;
49         struct modcallable *next;
50         char const *name;
51         char const *debug_name;
52         enum { MOD_SINGLE = 1, MOD_GROUP, MOD_LOAD_BALANCE, MOD_REDUNDANT_LOAD_BALANCE,
53 #ifdef WITH_UNLANG
54                MOD_IF, MOD_ELSE, MOD_ELSIF, MOD_UPDATE, MOD_SWITCH, MOD_CASE,
55                MOD_FOREACH, MOD_BREAK, MOD_RETURN,
56 #endif
57                MOD_POLICY, MOD_REFERENCE, MOD_XLAT } type;
58         rlm_components_t method;
59         int actions[RLM_MODULE_NUMCODES];
60 };
61
62 #define MOD_LOG_OPEN_BRACE RDEBUG2("%s {", c->debug_name)
63
64 #define MOD_LOG_CLOSE_BRACE RDEBUG2("} # %s = %s", c->debug_name, fr_int2str(mod_rcode_table, result, "<invalid>"))
65
66 typedef struct {
67         modcallable             mc;             /* self */
68         enum {
69                 GROUPTYPE_SIMPLE = 0,
70                 GROUPTYPE_REDUNDANT,
71                 GROUPTYPE_COUNT
72         } grouptype;                            /* after mc */
73         modcallable             *children;
74         modcallable             *tail;          /* of the children list */
75         CONF_SECTION            *cs;
76         value_pair_map_t        *map;           /* update */
77         vp_tmpl_t       *vpt;           /* switch */
78         fr_cond_t               *cond;          /* if/elsif */
79         bool                    done_pass2;
80 } modgroup;
81
82 typedef struct {
83         modcallable mc;
84         module_instance_t *modinst;
85 } modsingle;
86
87 typedef struct {
88         modcallable mc;
89         char const *ref_name;
90         CONF_SECTION *ref_cs;
91 } modref;
92
93 typedef struct {
94         modcallable mc;
95         int exec;
96         char *xlat_name;
97 } modxlat;
98
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)
102 {
103         rad_assert(p->type==MOD_SINGLE);
104         return (modsingle *)p;
105 }
106 static modgroup *mod_callabletogroup(modcallable *p)
107 {
108         rad_assert((p->type > MOD_SINGLE) && (p->type <= MOD_POLICY));
109
110         return (modgroup *)p;
111 }
112 static modcallable *mod_singletocallable(modsingle *p)
113 {
114         return (modcallable *)p;
115 }
116 static modcallable *mod_grouptocallable(modgroup *p)
117 {
118         return (modcallable *)p;
119 }
120
121 static modref *mod_callabletoref(modcallable *p)
122 {
123         rad_assert(p->type==MOD_REFERENCE);
124         return (modref *)p;
125 }
126 static modcallable *mod_reftocallable(modref *p)
127 {
128         return (modcallable *)p;
129 }
130
131 static modxlat *mod_callabletoxlat(modcallable *p)
132 {
133         rad_assert(p->type==MOD_XLAT);
134         return (modxlat *)p;
135 }
136 static modcallable *mod_xlattocallable(modxlat *p)
137 {
138         return (modcallable *)p;
139 }
140
141 /* modgroups are grown by adding a modcallable to the end */
142 static void add_child(modgroup *g, modcallable *c)
143 {
144         if (!c) return;
145
146         (void) talloc_steal(g, c);
147
148         if (!g->children) {
149                 g->children = g->tail = c;
150         } else {
151                 rad_assert(g->tail->next == NULL);
152                 g->tail->next = c;
153                 g->tail = c;
154         }
155
156         c->parent = mod_grouptocallable(g);
157 }
158
159 /* Here's where we recognize all of our keywords: first the rcodes, then the
160  * actions */
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      },
171         { NULL, 0 }
172 };
173
174
175 /*
176  *      Compile action && rcode for later use.
177  */
178 static int compile_action(modcallable *c, CONF_PAIR *cp)
179 {
180         int action;
181         char const *attr, *value;
182
183         attr = cf_pair_attr(cp);
184         value = cf_pair_value(cp);
185         if (!value) return 0;
186
187         if (!strcasecmp(value, "return"))
188                 action = MOD_ACTION_RETURN;
189
190         else if (!strcasecmp(value, "break"))
191                 action = MOD_ACTION_RETURN;
192
193         else if (!strcasecmp(value, "reject"))
194                 action = MOD_ACTION_REJECT;
195
196         else if (strspn(value, "0123456789")==strlen(value)) {
197                 action = atoi(value);
198
199                 /*
200                  *      Don't allow priority zero, for future use.
201                  */
202                 if (action == 0) return 0;
203         } else {
204                 cf_log_err_cp(cp, "Unknown action '%s'.\n",
205                            value);
206                 return 0;
207         }
208
209         if (strcasecmp(attr, "default") != 0) {
210                 int rcode;
211
212                 rcode = fr_str2int(mod_rcode_table, attr, -1);
213                 if (rcode < 0) {
214                         cf_log_err_cp(cp,
215                                    "Unknown module rcode '%s'.\n",
216                                    attr);
217                         return 0;
218                 }
219                 c->actions[rcode] = action;
220
221         } else {                /* set all unset values to the default */
222                 int i;
223
224                 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
225                         if (!c->actions[i]) c->actions[i] = action;
226                 }
227         }
228
229         return 1;
230 }
231
232 /* Some short names for debugging output */
233 static char const * const comp2str[] = {
234         "authenticate",
235         "authorize",
236         "preacct",
237         "accounting",
238         "session",
239         "pre-proxy",
240         "post-proxy",
241         "post-auth"
242 #ifdef WITH_COA
243         ,
244         "recv-coa",
245         "send-coa"
246 #endif
247 };
248
249 #ifdef HAVE_PTHREAD_H
250 /*
251  *      Lock the mutex for the module
252  */
253 static void safe_lock(module_instance_t *instance)
254 {
255         if (instance->mutex)
256                 pthread_mutex_lock(instance->mutex);
257 }
258
259 /*
260  *      Unlock the mutex for the module
261  */
262 static void safe_unlock(module_instance_t *instance)
263 {
264         if (instance->mutex)
265                 pthread_mutex_unlock(instance->mutex);
266 }
267 #else
268 /*
269  *      No threads: these functions become NULL's.
270  */
271 #define safe_lock(foo)
272 #define safe_unlock(foo)
273 #endif
274
275 static rlm_rcode_t CC_HINT(nonnull) call_modsingle(rlm_components_t component, modsingle *sp, REQUEST *request)
276 {
277         int blocked;
278         int indent = request->log.indent;
279
280         /*
281          *      If the request should stop, refuse to do anything.
282          */
283         blocked = (request->master_state == REQUEST_STOP_PROCESSING);
284         if (blocked) return RLM_MODULE_NOOP;
285
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;
290
291         if (sp->modinst->force) {
292                 request->rcode = sp->modinst->code;
293                 goto fail;
294         }
295
296         /*
297          *      For logging unresponsive children.
298          */
299         request->module = sp->modinst->name;
300
301         safe_lock(sp->modinst);
302         request->rcode = sp->modinst->entry->module->methods[component](sp->modinst->insthandle, request);
303         safe_unlock(sp->modinst);
304
305         request->module = "";
306
307         /*
308          *      Wasn't blocked, and now is.  Complain!
309          */
310         blocked = (request->master_state == REQUEST_STOP_PROCESSING);
311         if (blocked) {
312                 RWARN("Module %s became unblocked for request %u", sp->modinst->entry->name, request->number);
313         }
314
315  fail:
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);
320
321         return request->rcode;
322 }
323
324 static int default_component_results[RLM_COMPONENT_COUNT] = {
325         RLM_MODULE_REJECT,      /* AUTH */
326         RLM_MODULE_NOTFOUND,    /* AUTZ */
327         RLM_MODULE_NOOP,        /* PREACCT */
328         RLM_MODULE_NOOP,        /* ACCT */
329         RLM_MODULE_FAIL,        /* SESS */
330         RLM_MODULE_NOOP,        /* PRE_PROXY */
331         RLM_MODULE_NOOP,        /* POST_PROXY */
332         RLM_MODULE_NOOP         /* POST_AUTH */
333 #ifdef WITH_COA
334         ,
335         RLM_MODULE_NOOP,        /* RECV_COA_TYPE */
336         RLM_MODULE_NOOP         /* SEND_COA_TYPE */
337 #endif
338 };
339
340
341 extern char const *unlang_keyword[];
342
343 char const *unlang_keyword[] = {
344         "",
345         "single",
346         "group",
347         "load-balance group",
348         "redundant-load-balance group",
349 #ifdef WITH_UNLANG
350         "if",
351         "else",
352         "elsif",
353         "update",
354         "switch",
355         "case",
356         "foreach",
357         "break",
358         "return",
359 #endif
360         "policy",
361         "reference",
362         "xlat",
363         NULL
364 };
365
366 static char const modcall_spaces[] = "                                                                ";
367
368 #define MODCALL_STACK_MAX (32)
369
370 /*
371  *      Don't call the modules recursively.  Instead, do them
372  *      iteratively, and manage the call stack ourselves.
373  */
374 typedef struct modcall_stack_entry_t {
375         rlm_rcode_t result;
376         int priority;
377         int unwind;             /* unwind to this one if it exists */
378         modcallable *c;
379 } modcall_stack_entry_t;
380
381
382 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
383                             modcall_stack_entry_t *entry);
384
385 /*
386  *      Call a child of a block.
387  */
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)
391 {
392         modcall_stack_entry_t *next;
393
394         if (depth >= MODCALL_STACK_MAX) {
395                 ERROR("Internal sanity check failed: module stack is too deep");
396                 fr_exit(1);
397         }
398
399         /*
400          *      Initialize the childs stack frame.
401          */
402         next = entry + 1;
403         next->c = c;
404         next->result = entry->result;
405         next->priority = 0;
406         next->unwind = 0;
407
408         if (!modcall_recurse(request, component,
409                              depth, next)) {
410                 *result = RLM_MODULE_FAIL;
411                  return;
412         }
413
414         /*
415          *      Unwind back up the stack
416          */
417         if (next->unwind != 0) {
418                 entry->unwind = next->unwind;
419         }
420
421         *result = next->result;
422
423         return;
424 }
425
426
427 /*
428  *      Interpret the various types of blocks.
429  */
430 static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
431                             modcall_stack_entry_t *entry)
432 {
433         bool if_taken, was_if;
434         modcallable *c;
435         int priority;
436         rlm_rcode_t result;
437
438         was_if = if_taken = false;
439         result = RLM_MODULE_UNKNOWN;
440         RINDENT();
441
442 redo:
443         priority = -1;
444         c = entry->c;
445
446         /*
447          *      Nothing more to do.  Return the code and priority
448          *      which was set by the caller.
449          */
450         if (!c) goto finish;
451
452         rad_assert(c->debug_name != NULL); /* if this happens, all bets are off. */
453
454         /*
455          *      We've been asked to stop.  Do so.
456          */
457         if ((request->master_state == REQUEST_STOP_PROCESSING) ||
458             (request->parent &&
459              (request->parent->master_state == REQUEST_STOP_PROCESSING))) {
460                 entry->result = RLM_MODULE_FAIL;
461                 entry->priority = 9999;
462                 goto finish;
463         }
464
465 #ifdef WITH_UNLANG
466         /*
467          *      Handle "if" conditions.
468          */
469         if (c->type == MOD_IF) {
470                 int condition;
471                 modgroup *g;
472
473         mod_if:
474                 g = mod_callabletogroup(c);
475                 rad_assert(g->cond != NULL);
476
477                 RDEBUG2("%s %s{", unlang_keyword[c->type], c->name);
478
479                 condition = radius_evaluate_cond(request, result, 0, g->cond);
480                 if (condition < 0) {
481                         condition = false;
482                         REDEBUG("Failed retrieving values required to evaluate condition");
483                 } else {
484                         RDEBUG2("%s %s -> %s",
485                                 unlang_keyword[c->type],
486                                 c->name, condition ? "TRUE" : "FALSE");
487                 }
488
489                 /*
490                  *      Didn't pass.  Remember that.
491                  */
492                 if (!condition) {
493                         was_if = true;
494                         if_taken = false;
495                         goto next_sibling;
496                 }
497
498                 /*
499                  *      We took the "if".  Go recurse into its' children.
500                  */
501                 was_if = true;
502                 if_taken = true;
503                 goto do_children;
504         } /* MOD_IF */
505
506         /*
507          *      "else" if the previous "if" was taken.
508          *      "if" if the previous if wasn't taken.
509          */
510         if (c->type == MOD_ELSIF) {
511                 if (!was_if) goto elsif_error;
512
513                 /*
514                  *      Like MOD_ELSE, but allow for a later "else"
515                  */
516                 if (if_taken) {
517                         RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
518                                 unlang_keyword[c->type], request->number);
519                         was_if = true;
520                         if_taken = true;
521                         goto next_sibling;
522                 }
523
524                 /*
525                  *      Check the "if" condition.
526                  */
527                 goto mod_if;
528         } /* MOD_ELSIF */
529
530         /*
531          *      "else" for a preceding "if".
532          */
533         if (c->type == MOD_ELSE) {
534                 if (!was_if) { /* error */
535                 elsif_error:
536                         RDEBUG2("... skipping %s for request %d: No preceding \"if\"",
537                                 unlang_keyword[c->type], request->number);
538                         goto next_sibling;
539                 }
540
541                 if (if_taken) {
542                         RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
543                                 unlang_keyword[c->type], request->number);
544                         was_if = false;
545                         if_taken = false;
546                         goto next_sibling;
547                 }
548
549                 /*
550                  *      We need to process it.  Go do that.
551                  */
552                 was_if = false;
553                 if_taken = false;
554                 goto do_children;
555         } /* MOD_ELSE */
556
557         /*
558          *      We're no longer processing if/else/elsif.  Reset the
559          *      trackers for those conditions.
560          */
561         was_if = false;
562         if_taken = false;
563 #endif  /* WITH_UNLANG */
564
565         if (c->type == MOD_SINGLE) {
566                 modsingle *sp;
567
568                 /*
569                  *      Process a stand-alone child, and fall through
570                  *      to dealing with it's parent.
571                  */
572                 sp = mod_callabletosingle(c);
573
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;
578         } /* MOD_SINGLE */
579
580 #ifdef WITH_UNLANG
581         /*
582          *      Update attribute(s)
583          */
584         if (c->type == MOD_UPDATE) {
585                 int rcode;
586                 modgroup *g = mod_callabletogroup(c);
587                 value_pair_map_t *map;
588
589                 MOD_LOG_OPEN_BRACE;
590                 RINDENT();
591                 for (map = g->map; map != NULL; map = map->next) {
592                         rcode = map_to_request(request, map, map_to_vp, NULL);
593                         if (rcode < 0) {
594                                 result = (rcode == -2) ? RLM_MODULE_INVALID : RLM_MODULE_FAIL;
595                                 REXDENT();
596                                 MOD_LOG_CLOSE_BRACE;
597                                 goto calculate_result;
598                         }
599                 }
600                 REXDENT();
601                 result = RLM_MODULE_NOOP;
602                 MOD_LOG_CLOSE_BRACE;
603                 goto calculate_result;
604         } /* MOD_IF */
605
606         /*
607          *      Loop over a set of attributes.
608          */
609         if (c->type == MOD_FOREACH) {
610                 int i, foreach_depth = -1;
611                 VALUE_PAIR *vps, *vp;
612                 modcall_stack_entry_t *next = NULL;
613                 vp_cursor_t copy;
614                 modgroup *g = mod_callabletogroup(c);
615
616                 if (depth >= MODCALL_STACK_MAX) {
617                         ERROR("Internal sanity check failed: module stack is too deep");
618                         fr_exit(1);
619                 }
620
621                 /*
622                  *      Figure out how deep we are in nesting by looking at request_data
623                  *      stored previously.
624                  */
625                 for (i = 0; i < 8; i++) {
626                         if (!request_data_reference(request, (void *)radius_get_vp, i)) {
627                                 foreach_depth = i;
628                                 break;
629                         }
630                 }
631
632                 if (foreach_depth < 0) {
633                         REDEBUG("foreach Nesting too deep!");
634                         result = RLM_MODULE_FAIL;
635                         goto calculate_result;
636                 }
637
638                 /*
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
641                  *      iterating over.
642                  */
643                 if (tmpl_copy_vps(request, &vps, request, g->vpt) < 0) {        /* nothing to loop over */
644                         MOD_LOG_OPEN_BRACE;
645                         result = RLM_MODULE_NOOP;
646                         MOD_LOG_CLOSE_BRACE;
647                         goto calculate_result;
648                 }
649
650                 rad_assert(vps != NULL);
651                 fr_cursor_init(&copy, &vps);
652
653                 RDEBUG2("foreach %s ", c->name);
654
655                 /*
656                  *      This is the actual body of the foreach loop
657                  */
658                 for (vp = fr_cursor_first(&copy);
659                      vp != NULL;
660                      vp = fr_cursor_next(&copy)) {
661 #ifndef NDEBUG
662                         if (fr_debug_flag >= 2) {
663                                 char buffer[1024];
664
665                                 vp_prints_value(buffer, sizeof(buffer), vp, '"');
666                                 RDEBUG2("# Foreach-Variable-%d = %s", foreach_depth, buffer);
667                         }
668 #endif
669
670                         /*
671                          *      Add the vp to the request, so that
672                          *      xlat.c, xlat_foreach() can find it.
673                          */
674                         request_data_add(request, (void *)radius_get_vp, foreach_depth, &vp, false);
675
676                         /*
677                          *      Initialize the childs stack frame.
678                          */
679                         next = entry + 1;
680                         next->c = g->children;
681                         next->result = entry->result;
682                         next->priority = 0;
683                         next->unwind = 0;
684
685                         if (!modcall_recurse(request, component, depth + 1, next)) {
686                                 break;
687                         }
688
689                         /*
690                          *      We've unwound to the enclosing
691                          *      "foreach".  Stop the unwinding.
692                          */
693                         if (next->unwind == MOD_FOREACH) {
694                                 break;
695                         }
696
697                         /*
698                          *      Unwind all the way.
699                          */
700                         if (next->unwind == MOD_RETURN) {
701                                 entry->unwind = MOD_RETURN;
702                                 break;
703                         }
704                 } /* loop over VPs */
705
706                 /*
707                  *      Free the copied vps and the request data
708                  *      If we don't remove the request data, something could call
709                  *      the xlat outside of a foreach loop and trigger a segv.
710                  */
711                 pairfree(&vps);
712                 request_data_get(request, (void *)radius_get_vp, foreach_depth);
713
714                 rad_assert(next != NULL);
715                 result = next->result;
716                 priority = next->priority;
717                 MOD_LOG_CLOSE_BRACE;
718                 goto calculate_result;
719         } /* MOD_FOREACH */
720
721         /*
722          *      Break out of a "foreach" loop, or return from a nested
723          *      group.
724          */
725         if ((c->type == MOD_BREAK) || (c->type == MOD_RETURN)) {
726                 int i;
727                 VALUE_PAIR **copy_p;
728
729                 RDEBUG2("%s", unlang_keyword[c->type]);
730
731                 for (i = 8; i >= 0; i--) {
732                         copy_p = request_data_get(request, (void *)radius_get_vp, i);
733                         if (copy_p) {
734                                 if (c->type == MOD_BREAK) {
735                                         RDEBUG2("# break Foreach-Variable-%d", i);
736                                         break;
737                                 }
738                         }
739                 }
740
741                 /*
742                  *      Leave result / priority on the stack, and stop processing the section.
743                  */
744                 entry->unwind = c->type;
745                 goto finish;
746         } /* MOD_BREAK */
747
748 #endif    /* WITH_UNLANG */
749
750         /*
751          *      Child is a group that has children of it's own.
752          */
753         if ((c->type == MOD_GROUP) || (c->type == MOD_POLICY)
754 #ifdef WITH_UNLANG
755             || (c->type == MOD_CASE)
756 #endif
757                 ) {
758                 modgroup *g;
759
760 #ifdef WITH_UNLANG
761         do_children:
762 #endif
763                 g = mod_callabletogroup(c);
764
765                 /*
766                  *      This should really have been caught in the
767                  *      compiler, and the node never generated.  But
768                  *      doing that requires changing it's API so that
769                  *      it returns a flag instead of the compiled
770                  *      MOD_GROUP.
771                  */
772                 if (!g->children) {
773                         RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
774                         goto next_sibling;
775                 }
776
777                 MOD_LOG_OPEN_BRACE;
778                 modcall_child(request, component,
779                               depth + 1, entry, g->children,
780                               &result);
781                 MOD_LOG_CLOSE_BRACE;
782                 goto calculate_result;
783         } /* MOD_GROUP */
784
785 #ifdef WITH_UNLANG
786         if (c->type == MOD_SWITCH) {
787                 modcallable *this, *found, *null_case;
788                 modgroup *g, *h;
789                 fr_cond_t cond;
790                 value_data_t data;
791                 value_pair_map_t map;
792                 vp_tmpl_t vpt;
793
794                 MOD_LOG_OPEN_BRACE;
795
796                 g = mod_callabletogroup(c);
797
798                 memset(&cond, 0, sizeof(cond));
799                 memset(&map, 0, sizeof(map));
800
801                 cond.type = COND_TYPE_MAP;
802                 cond.data.map = &map;
803
804                 map.op = T_OP_CMP_EQ;
805                 map.ci = cf_section_to_item(g->cs);
806
807                 rad_assert(g->vpt != NULL);
808
809                 null_case = found = NULL;
810                 data.ptr = NULL;
811
812                 /*
813                  *      The attribute doesn't exist.  We can skip
814                  *      directly to the default 'case' statement.
815                  */
816                 if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
817                 find_null_case:
818                         for (this = g->children; this; this = this->next) {
819                                 rad_assert(this->type == MOD_CASE);
820
821                                 h = mod_callabletogroup(this);
822                                 if (h->vpt) continue;
823
824                                 found = this;
825                                 break;
826                         }
827
828                         goto do_null_case;
829                 }
830
831                 /*
832                  *      Expand the template if necessary, so that it
833                  *      is evaluated once instead of for each 'case'
834                  *      statement.
835                  */
836                 if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
837                     (g->vpt->type == TMPL_TYPE_XLAT) ||
838                     (g->vpt->type == TMPL_TYPE_EXEC)) {
839                         char *p;
840                         ssize_t len;
841
842                         len = tmpl_aexpand(request, &p, request, g->vpt, NULL, NULL);
843                         if (len < 0) goto find_null_case;
844                         data.strvalue = p;
845                         tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, len);
846                 }
847
848                 /*
849                  *      Find either the exact matching name, or the
850                  *      "case {...}" statement.
851                  */
852                 for (this = g->children; this; this = this->next) {
853                         rad_assert(this->type == MOD_CASE);
854
855                         h = mod_callabletogroup(this);
856
857                         /*
858                          *      Remember the default case
859                          */
860                         if (!h->vpt) {
861                                 if (!null_case) null_case = this;
862                                 continue;
863                         }
864
865                         /*
866                          *      If we're switching over an attribute
867                          *      AND we haven't pre-parsed the data for
868                          *      the case statement, then cast the data
869                          *      to the type of the attribute.
870                          */
871                         if ((g->vpt->type == TMPL_TYPE_ATTR) &&
872                             (h->vpt->type != TMPL_TYPE_DATA)) {
873                                 map.rhs = g->vpt;
874                                 map.lhs = h->vpt;
875                                 cond.cast = g->vpt->tmpl_da;
876
877                                 /*
878                                  *      Remove unnecessary casting.
879                                  */
880                                 if ((h->vpt->type == TMPL_TYPE_ATTR) &&
881                                     (g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
882                                         cond.cast = NULL;
883                                 }
884
885                                 /*
886                                  *      Use the pre-expanded string.
887                                  */
888                         } else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
889                                    (g->vpt->type == TMPL_TYPE_XLAT) ||
890                                    (g->vpt->type == TMPL_TYPE_EXEC)) {
891                                 map.rhs = h->vpt;
892                                 map.lhs = &vpt;
893                                 cond.cast = NULL;
894
895                                 /*
896                                  *      Else evaluate the 'switch' statement.
897                                  */
898                         } else {
899                                 map.rhs = h->vpt;
900                                 map.lhs = g->vpt;
901                                 cond.cast = NULL;
902                         }
903
904                         if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
905                                                 &cond) == 1) {
906                                 found = this;
907                                 break;
908                         }
909                 }
910
911                 if (!found) found = null_case;
912
913         do_null_case:
914                 talloc_free(data.ptr);
915                 modcall_child(request, component, depth + 1, entry, found, &result);
916                 MOD_LOG_CLOSE_BRACE;
917                 goto calculate_result;
918         } /* MOD_SWITCH */
919 #endif
920
921         if ((c->type == MOD_LOAD_BALANCE) ||
922             (c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
923                 uint32_t count = 0;
924                 modcallable *this, *found;
925                 modgroup *g;
926
927                 MOD_LOG_OPEN_BRACE;
928
929                 g = mod_callabletogroup(c);
930                 found = g->children;
931                 rad_assert(g->children != NULL);
932
933                 /*
934                  *      Choose a child at random.
935                  */
936                 for (this = g->children; this; this = this->next) {
937                         count++;
938
939                         if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
940                                 found = this;
941                         }
942                 }
943
944                 if (c->type == MOD_LOAD_BALANCE) {
945                         modcall_child(request, component,
946                                       depth + 1, entry, found,
947                                       &result);
948
949                 } else {
950                         this = found;
951
952                         do {
953                                 modcall_child(request, component,
954                                               depth + 1, entry, this,
955                                               &result);
956                                 if (this->actions[result] == MOD_ACTION_RETURN) {
957                                         priority = -1;
958                                         break;
959                                 }
960
961                                 this = this->next;
962                                 if (!this) this = g->children;
963                         } while (this != found);
964                 }
965                 MOD_LOG_CLOSE_BRACE;
966                 goto calculate_result;
967         } /* MOD_LOAD_BALANCE */
968
969         /*
970          *      Reference another virtual server.
971          *
972          *      This should really be deleted, and replaced with a
973          *      more abstracted / functional version.
974          */
975         if (c->type == MOD_REFERENCE) {
976                 modref *mr = mod_callabletoref(c);
977                 char const *server = request->server;
978
979                 if (server == mr->ref_name) {
980                         RWDEBUG("Suppressing recursive call to server %s", server);
981                         goto next_sibling;
982                 }
983
984                 request->server = mr->ref_name;
985                 RDEBUG("server %s { # nested call", mr->ref_name);
986                 result = indexed_modcall(component, 0, request);
987                 RDEBUG("} # server %s with nested call", mr->ref_name);
988                 request->server = server;
989                 goto calculate_result;
990         } /* MOD_REFERENCE */
991
992         /*
993          *      xlat a string without doing anything else
994          *
995          *      This should really be deleted, and replaced with a
996          *      more abstracted / functional version.
997          */
998         if (c->type == MOD_XLAT) {
999                 modxlat *mx = mod_callabletoxlat(c);
1000                 char buffer[128];
1001
1002                 if (!mx->exec) {
1003                         radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
1004                 } else {
1005                         RDEBUG("`%s`", mx->xlat_name);
1006                         radius_exec_program(NULL, 0, NULL, request, mx->xlat_name, request->packet->vps,
1007                                             false, true, EXEC_TIMEOUT);
1008                 }
1009
1010                 goto next_sibling;
1011         } /* MOD_XLAT */
1012
1013         /*
1014          *      Add new module types here.
1015          */
1016
1017 calculate_result:
1018 #if 0
1019         RDEBUG("(%s, %d) ? (%s, %d)",
1020                fr_int2str(mod_rcode_table, result, "<invalid>"),
1021                priority,
1022                fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
1023                entry->priority);
1024 #endif
1025
1026
1027         rad_assert(result != RLM_MODULE_UNKNOWN);
1028
1029         /*
1030          *      The child's action says return.  Do so.
1031          */
1032         if ((c->actions[result] == MOD_ACTION_RETURN) &&
1033             (priority <= 0)) {
1034                 entry->result = result;
1035                 goto finish;
1036         }
1037
1038         /*
1039          *      If "reject", break out of the loop and return
1040          *      reject.
1041          */
1042         if (c->actions[result] == MOD_ACTION_REJECT) {
1043                 entry->result = RLM_MODULE_REJECT;
1044                 goto finish;
1045         }
1046
1047         /*
1048          *      The array holds a default priority for this return
1049          *      code.  Grab it in preference to any unset priority.
1050          */
1051         if (priority < 0) {
1052                 priority = c->actions[result];
1053         }
1054
1055         /*
1056          *      We're higher than any previous priority, remember this
1057          *      return code and priority.
1058          */
1059         if (priority > entry->priority) {
1060                 entry->result = result;
1061                 entry->priority = priority;
1062         }
1063
1064 #ifdef WITH_UNLANG
1065         /*
1066          *      If we're processing a "case" statement, we return once
1067          *      it's done, rather than going to the next "case" statement.
1068          */
1069         if (c->type == MOD_CASE) goto finish;
1070 #endif
1071
1072         /*
1073          *      If we've been told to stop processing
1074          *      it, do so.
1075          */
1076         if (entry->unwind == MOD_BREAK) {
1077                 RDEBUG2("# unwind to enclosing foreach");
1078                 entry->unwind = 0;
1079                 goto finish;
1080         }
1081
1082         if (entry->unwind == MOD_RETURN) {
1083                 goto finish;
1084         }
1085
1086 next_sibling:
1087         entry->c = entry->c->next;
1088
1089         if (entry->c) goto redo;
1090
1091 finish:
1092         /*
1093          *      And we're done!
1094          */
1095         REXDENT();
1096         return true;
1097 }
1098
1099
1100 /** Call a module, iteratively, with a local stack, rather than recursively
1101  *
1102  * What did Paul Graham say about Lisp...?
1103  */
1104 int modcall(rlm_components_t component, modcallable *c, REQUEST *request)
1105 {
1106         modcall_stack_entry_t stack[MODCALL_STACK_MAX];
1107
1108 #ifndef NDEBUG
1109         memset(stack, 0, sizeof(stack));
1110 #endif
1111         /*
1112          *      Set up the initial stack frame.
1113          */
1114         stack[0].c = c;
1115         stack[0].result = default_component_results[component];
1116         stack[0].priority = 0;
1117         stack[0].unwind = 0;
1118
1119         /*
1120          *      Call the main handler.
1121          */
1122         if (!modcall_recurse(request, component, 0, &stack[0])) {
1123                 return RLM_MODULE_FAIL;
1124         }
1125
1126         /*
1127          *      Return the result.
1128          */
1129         return stack[0].result;
1130 }
1131
1132
1133 #if 0
1134 static char const *action2str(int action)
1135 {
1136         static char buf[32];
1137         if(action==MOD_ACTION_RETURN)
1138                 return "return";
1139         if(action==MOD_ACTION_REJECT)
1140                 return "reject";
1141         snprintf(buf, sizeof buf, "%d", action);
1142         return buf;
1143 }
1144
1145 /* If you suspect a bug in the parser, you'll want to use these dump
1146  * functions. dump_tree should reproduce a whole tree exactly as it was found
1147  * in radiusd.conf, but in long form (all actions explicitly defined) */
1148 static void dump_mc(modcallable *c, int indent)
1149 {
1150         int i;
1151
1152         if(c->type==MOD_SINGLE) {
1153                 modsingle *single = mod_callabletosingle(c);
1154                 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1155                         single->modinst->name);
1156         } else if ((c->type > MOD_SINGLE) && (c->type <= MOD_POLICY)) {
1157                 modgroup *g = mod_callabletogroup(c);
1158                 modcallable *p;
1159                 DEBUG("%.*s%s {", indent, "\t\t\t\t\t\t\t\t\t\t\t",
1160                       unlang_keyword[c->type]);
1161                 for(p = g->children;p;p = p->next)
1162                         dump_mc(p, indent+1);
1163         } /* else ignore it for now */
1164
1165         for(i = 0; i<RLM_MODULE_NUMCODES; ++i) {
1166                 DEBUG("%.*s%s = %s", indent+1, "\t\t\t\t\t\t\t\t\t\t\t",
1167                       fr_int2str(mod_rcode_table, i, "<invalid>"),
1168                       action2str(c->actions[i]));
1169         }
1170
1171         DEBUG("%.*s}", indent, "\t\t\t\t\t\t\t\t\t\t\t");
1172 }
1173
1174 static void dump_tree(rlm_components_t comp, modcallable *c)
1175 {
1176         DEBUG("[%s]", comp2str[comp]);
1177         dump_mc(c, 0);
1178 }
1179 #else
1180 #define dump_tree(a, b)
1181 #endif
1182
1183 /* These are the default actions. For each component, the group{} block
1184  * behaves like the code from the old module_*() function. redundant{}
1185  * are based on my guesses of what they will be used for. --Pac. */
1186 static const int
1187 defaultactions[RLM_COMPONENT_COUNT][GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1188 {
1189         /* authenticate */
1190         {
1191                 /* group */
1192                 {
1193                         MOD_ACTION_RETURN,      /* reject   */
1194                         1,                      /* fail     */
1195                         MOD_ACTION_RETURN,      /* ok       */
1196                         MOD_ACTION_RETURN,      /* handled  */
1197                         1,                      /* invalid  */
1198                         MOD_ACTION_RETURN,      /* userlock */
1199                         MOD_ACTION_RETURN,      /* notfound */
1200                         1,                      /* noop     */
1201                         1                       /* updated  */
1202                 },
1203                 /* redundant */
1204                 {
1205                         MOD_ACTION_RETURN,      /* reject   */
1206                         1,                      /* fail     */
1207                         MOD_ACTION_RETURN,      /* ok       */
1208                         MOD_ACTION_RETURN,      /* handled  */
1209                         MOD_ACTION_RETURN,      /* invalid  */
1210                         MOD_ACTION_RETURN,      /* userlock */
1211                         MOD_ACTION_RETURN,      /* notfound */
1212                         MOD_ACTION_RETURN,      /* noop     */
1213                         MOD_ACTION_RETURN       /* updated  */
1214                 }
1215         },
1216         /* authorize */
1217         {
1218                 /* group */
1219                 {
1220                         MOD_ACTION_RETURN,      /* reject   */
1221                         MOD_ACTION_RETURN,      /* fail     */
1222                         3,                      /* ok       */
1223                         MOD_ACTION_RETURN,      /* handled  */
1224                         MOD_ACTION_RETURN,      /* invalid  */
1225                         MOD_ACTION_RETURN,      /* userlock */
1226                         1,                      /* notfound */
1227                         2,                      /* noop     */
1228                         4                       /* updated  */
1229                 },
1230                 /* redundant */
1231                 {
1232                         MOD_ACTION_RETURN,      /* reject   */
1233                         1,                      /* fail     */
1234                         MOD_ACTION_RETURN,      /* ok       */
1235                         MOD_ACTION_RETURN,      /* handled  */
1236                         MOD_ACTION_RETURN,      /* invalid  */
1237                         MOD_ACTION_RETURN,      /* userlock */
1238                         MOD_ACTION_RETURN,      /* notfound */
1239                         MOD_ACTION_RETURN,      /* noop     */
1240                         MOD_ACTION_RETURN       /* updated  */
1241                 }
1242         },
1243         /* preacct */
1244         {
1245                 /* group */
1246                 {
1247                         MOD_ACTION_RETURN,      /* reject   */
1248                         MOD_ACTION_RETURN,      /* fail     */
1249                         2,                      /* ok       */
1250                         MOD_ACTION_RETURN,      /* handled  */
1251                         MOD_ACTION_RETURN,      /* invalid  */
1252                         MOD_ACTION_RETURN,      /* userlock */
1253                         MOD_ACTION_RETURN,      /* notfound */
1254                         1,                      /* noop     */
1255                         3                       /* updated  */
1256                 },
1257                 /* redundant */
1258                 {
1259                         MOD_ACTION_RETURN,      /* reject   */
1260                         1,                      /* fail     */
1261                         MOD_ACTION_RETURN,      /* ok       */
1262                         MOD_ACTION_RETURN,      /* handled  */
1263                         MOD_ACTION_RETURN,      /* invalid  */
1264                         MOD_ACTION_RETURN,      /* userlock */
1265                         MOD_ACTION_RETURN,      /* notfound */
1266                         MOD_ACTION_RETURN,      /* noop     */
1267                         MOD_ACTION_RETURN       /* updated  */
1268                 }
1269         },
1270         /* accounting */
1271         {
1272                 /* group */
1273                 {
1274                         MOD_ACTION_RETURN,      /* reject   */
1275                         MOD_ACTION_RETURN,      /* fail     */
1276                         2,                      /* ok       */
1277                         MOD_ACTION_RETURN,      /* handled  */
1278                         MOD_ACTION_RETURN,      /* invalid  */
1279                         MOD_ACTION_RETURN,      /* userlock */
1280                         MOD_ACTION_RETURN,      /* notfound */
1281                         1,                      /* noop     */
1282                         3                       /* updated  */
1283                 },
1284                 /* redundant */
1285                 {
1286                         1,                      /* reject   */
1287                         1,                      /* fail     */
1288                         MOD_ACTION_RETURN,      /* ok       */
1289                         MOD_ACTION_RETURN,      /* handled  */
1290                         1,                      /* invalid  */
1291                         1,                      /* userlock */
1292                         1,                      /* notfound */
1293                         2,                      /* noop     */
1294                         4                       /* updated  */
1295                 }
1296         },
1297         /* checksimul */
1298         {
1299                 /* group */
1300                 {
1301                         MOD_ACTION_RETURN,      /* reject   */
1302                         1,                      /* fail     */
1303                         MOD_ACTION_RETURN,      /* ok       */
1304                         MOD_ACTION_RETURN,      /* handled  */
1305                         MOD_ACTION_RETURN,      /* invalid  */
1306                         MOD_ACTION_RETURN,      /* userlock */
1307                         MOD_ACTION_RETURN,      /* notfound */
1308                         MOD_ACTION_RETURN,      /* noop     */
1309                         MOD_ACTION_RETURN       /* updated  */
1310                 },
1311                 /* redundant */
1312                 {
1313                         MOD_ACTION_RETURN,      /* reject   */
1314                         1,                      /* fail     */
1315                         MOD_ACTION_RETURN,      /* ok       */
1316                         MOD_ACTION_RETURN,      /* handled  */
1317                         MOD_ACTION_RETURN,      /* invalid  */
1318                         MOD_ACTION_RETURN,      /* userlock */
1319                         MOD_ACTION_RETURN,      /* notfound */
1320                         MOD_ACTION_RETURN,      /* noop     */
1321                         MOD_ACTION_RETURN       /* updated  */
1322                 }
1323         },
1324         /* pre-proxy */
1325         {
1326                 /* group */
1327                 {
1328                         MOD_ACTION_RETURN,      /* reject   */
1329                         MOD_ACTION_RETURN,      /* fail     */
1330                         3,                      /* ok       */
1331                         MOD_ACTION_RETURN,      /* handled  */
1332                         MOD_ACTION_RETURN,      /* invalid  */
1333                         MOD_ACTION_RETURN,      /* userlock */
1334                         1,                      /* notfound */
1335                         2,                      /* noop     */
1336                         4                       /* updated  */
1337                 },
1338                 /* redundant */
1339                 {
1340                         MOD_ACTION_RETURN,      /* reject   */
1341                         1,                      /* fail     */
1342                         MOD_ACTION_RETURN,      /* ok       */
1343                         MOD_ACTION_RETURN,      /* handled  */
1344                         MOD_ACTION_RETURN,      /* invalid  */
1345                         MOD_ACTION_RETURN,      /* userlock */
1346                         MOD_ACTION_RETURN,      /* notfound */
1347                         MOD_ACTION_RETURN,      /* noop     */
1348                         MOD_ACTION_RETURN       /* updated  */
1349                 }
1350         },
1351         /* post-proxy */
1352         {
1353                 /* group */
1354                 {
1355                         MOD_ACTION_RETURN,      /* reject   */
1356                         MOD_ACTION_RETURN,      /* fail     */
1357                         3,                      /* ok       */
1358                         MOD_ACTION_RETURN,      /* handled  */
1359                         MOD_ACTION_RETURN,      /* invalid  */
1360                         MOD_ACTION_RETURN,      /* userlock */
1361                         1,                      /* notfound */
1362                         2,                      /* noop     */
1363                         4                       /* updated  */
1364                 },
1365                 /* redundant */
1366                 {
1367                         MOD_ACTION_RETURN,      /* reject   */
1368                         1,                      /* fail     */
1369                         MOD_ACTION_RETURN,      /* ok       */
1370                         MOD_ACTION_RETURN,      /* handled  */
1371                         MOD_ACTION_RETURN,      /* invalid  */
1372                         MOD_ACTION_RETURN,      /* userlock */
1373                         MOD_ACTION_RETURN,      /* notfound */
1374                         MOD_ACTION_RETURN,      /* noop     */
1375                         MOD_ACTION_RETURN       /* updated  */
1376                 }
1377         },
1378         /* post-auth */
1379         {
1380                 /* group */
1381                 {
1382                         MOD_ACTION_RETURN,      /* reject   */
1383                         MOD_ACTION_RETURN,      /* fail     */
1384                         3,                      /* ok       */
1385                         MOD_ACTION_RETURN,      /* handled  */
1386                         MOD_ACTION_RETURN,      /* invalid  */
1387                         MOD_ACTION_RETURN,      /* userlock */
1388                         1,                      /* notfound */
1389                         2,                      /* noop     */
1390                         4                       /* updated  */
1391                 },
1392                 /* redundant */
1393                 {
1394                         MOD_ACTION_RETURN,      /* reject   */
1395                         1,                      /* fail     */
1396                         MOD_ACTION_RETURN,      /* ok       */
1397                         MOD_ACTION_RETURN,      /* handled  */
1398                         MOD_ACTION_RETURN,      /* invalid  */
1399                         MOD_ACTION_RETURN,      /* userlock */
1400                         MOD_ACTION_RETURN,      /* notfound */
1401                         MOD_ACTION_RETURN,      /* noop     */
1402                         MOD_ACTION_RETURN       /* updated  */
1403                 }
1404         }
1405 #ifdef WITH_COA
1406         ,
1407         /* recv-coa */
1408         {
1409                 /* group */
1410                 {
1411                         MOD_ACTION_RETURN,      /* reject   */
1412                         MOD_ACTION_RETURN,      /* fail     */
1413                         3,                      /* ok       */
1414                         MOD_ACTION_RETURN,      /* handled  */
1415                         MOD_ACTION_RETURN,      /* invalid  */
1416                         MOD_ACTION_RETURN,      /* userlock */
1417                         1,                      /* notfound */
1418                         2,                      /* noop     */
1419                         4                       /* updated  */
1420                 },
1421                 /* redundant */
1422                 {
1423                         MOD_ACTION_RETURN,      /* reject   */
1424                         1,                      /* fail     */
1425                         MOD_ACTION_RETURN,      /* ok       */
1426                         MOD_ACTION_RETURN,      /* handled  */
1427                         MOD_ACTION_RETURN,      /* invalid  */
1428                         MOD_ACTION_RETURN,      /* userlock */
1429                         MOD_ACTION_RETURN,      /* notfound */
1430                         MOD_ACTION_RETURN,      /* noop     */
1431                         MOD_ACTION_RETURN       /* updated  */
1432                 }
1433         },
1434         /* send-coa */
1435         {
1436                 /* group */
1437                 {
1438                         MOD_ACTION_RETURN,      /* reject   */
1439                         MOD_ACTION_RETURN,      /* fail     */
1440                         3,                      /* ok       */
1441                         MOD_ACTION_RETURN,      /* handled  */
1442                         MOD_ACTION_RETURN,      /* invalid  */
1443                         MOD_ACTION_RETURN,      /* userlock */
1444                         1,                      /* notfound */
1445                         2,                      /* noop     */
1446                         4                       /* updated  */
1447                 },
1448                 /* redundant */
1449                 {
1450                         MOD_ACTION_RETURN,      /* reject   */
1451                         1,                      /* fail     */
1452                         MOD_ACTION_RETURN,      /* ok       */
1453                         MOD_ACTION_RETURN,      /* handled  */
1454                         MOD_ACTION_RETURN,      /* invalid  */
1455                         MOD_ACTION_RETURN,      /* userlock */
1456                         MOD_ACTION_RETURN,      /* notfound */
1457                         MOD_ACTION_RETURN,      /* noop     */
1458                         MOD_ACTION_RETURN       /* updated  */
1459                 }
1460         }
1461 #endif
1462 };
1463
1464 static const int authtype_actions[GROUPTYPE_COUNT][RLM_MODULE_NUMCODES] =
1465 {
1466         /* group */
1467         {
1468                 MOD_ACTION_RETURN,      /* reject   */
1469                 MOD_ACTION_RETURN,      /* fail     */
1470                 4,                      /* ok       */
1471                 MOD_ACTION_RETURN,      /* handled  */
1472                 MOD_ACTION_RETURN,      /* invalid  */
1473                 MOD_ACTION_RETURN,      /* userlock */
1474                 1,                      /* notfound */
1475                 2,                      /* noop     */
1476                 3                       /* updated  */
1477         },
1478         /* redundant */
1479         {
1480                 MOD_ACTION_RETURN,      /* reject   */
1481                 1,                      /* fail     */
1482                 MOD_ACTION_RETURN,      /* ok       */
1483                 MOD_ACTION_RETURN,      /* handled  */
1484                 MOD_ACTION_RETURN,      /* invalid  */
1485                 MOD_ACTION_RETURN,      /* userlock */
1486                 MOD_ACTION_RETURN,      /* notfound */
1487                 MOD_ACTION_RETURN,      /* noop     */
1488                 MOD_ACTION_RETURN       /* updated  */
1489         }
1490 };
1491
1492 /** Validate and fixup a map that's part of an update section.
1493  *
1494  * @param map to validate.
1495  * @param ctx data to pass to fixup function (currently unused).
1496  * @return 0 if valid else -1.
1497  */
1498 int modcall_fixup_update(value_pair_map_t *map, UNUSED void *ctx)
1499 {
1500         CONF_PAIR *cp = cf_item_to_pair(map->ci);
1501
1502         /*
1503          *      Anal-retentive checks.
1504          */
1505         if (DEBUG_ENABLED3) {
1506                 if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->lhs->name[0] != '&')) {
1507                         WARN("%s[%d]: Please change attribute reference to '&%s %s ...'",
1508                              cf_pair_filename(cp), cf_pair_lineno(cp),
1509                              map->lhs->name, fr_int2str(fr_tokens, map->op, "<INVALID>"));
1510                 }
1511
1512                 if ((map->rhs->type == TMPL_TYPE_ATTR) && (map->rhs->name[0] != '&')) {
1513                         WARN("%s[%d]: Please change attribute reference to '... %s &%s'",
1514                              cf_pair_filename(cp), cf_pair_lineno(cp),
1515                              fr_int2str(fr_tokens, map->op, "<INVALID>"), map->rhs->name);
1516                 }
1517         }
1518
1519         /*
1520          *      Values used by unary operators should be literal ANY
1521          *
1522          *      We then free the template and alloc a NULL one instead.
1523          */
1524         if (map->op == T_OP_CMP_FALSE) {
1525                 if ((map->rhs->type != TMPL_TYPE_LITERAL) || (strcmp(map->rhs->name, "ANY") != 0)) {
1526                         WARN("%s[%d] Wildcard deletion MUST use '!* ANY'",
1527                              cf_pair_filename(cp), cf_pair_lineno(cp));
1528                 }
1529
1530                 TALLOC_FREE(map->rhs);
1531
1532                 map->rhs = tmpl_alloc(map, TMPL_TYPE_NULL, NULL, 0);
1533         }
1534
1535         /*
1536          *      Lots of sanity checks for insane people...
1537          */
1538
1539         /*
1540          *      What exactly where you expecting to happen here?
1541          */
1542         if ((map->lhs->type == TMPL_TYPE_ATTR) &&
1543             (map->rhs->type == TMPL_TYPE_LIST)) {
1544                 cf_log_err(map->ci, "Can't copy list into an attribute");
1545                 return -1;
1546         }
1547
1548         /*
1549          *      Depending on the attribute type, some operators are disallowed.
1550          */
1551         if (map->lhs->type == TMPL_TYPE_ATTR) {
1552                 switch (map->op) {
1553                 default:
1554                         cf_log_err(map->ci, "Invalid operator for attribute");
1555                         return -1;
1556
1557                 case T_OP_EQ:
1558                 case T_OP_CMP_EQ:
1559                 case T_OP_ADD:
1560                 case T_OP_SUB:
1561                 case T_OP_LE:
1562                 case T_OP_GE:
1563                 case T_OP_CMP_FALSE:
1564                 case T_OP_SET:
1565                         break;
1566                 }
1567         }
1568
1569         if (map->lhs->type == TMPL_TYPE_LIST) {
1570                 /*
1571                  *      Can't copy an xlat expansion or literal into a list,
1572                  *      we don't know what type of attribute we'd need
1573                  *      to create.
1574                  *
1575                  *      The only exception is where were using a unary
1576                  *      operator like !*.
1577                  */
1578                 if (map->op != T_OP_CMP_FALSE) switch (map->rhs->type) {
1579                 case TMPL_TYPE_XLAT:
1580                 case TMPL_TYPE_LITERAL:
1581                         cf_log_err(map->ci, "Can't copy value into list (we don't know which attribute to create)");
1582                         return -1;
1583
1584                 default:
1585                         break;
1586                 }
1587
1588                 /*
1589                  *      Only += and :=, and !* operators are supported
1590                  *      for lists.
1591                  */
1592                 switch (map->op) {
1593                 case T_OP_CMP_FALSE:
1594                         break;
1595
1596                 case T_OP_ADD:
1597                         if ((map->rhs->type != TMPL_TYPE_LIST) &&
1598                             (map->rhs->type != TMPL_TYPE_EXEC)) {
1599                                 cf_log_err(map->ci, "Invalid source for list assignment '%s += ...'", map->lhs->name);
1600                                 return -1;
1601                         }
1602                         break;
1603
1604                 case T_OP_SET:
1605                         if (map->rhs->type == TMPL_TYPE_EXEC) {
1606                                 WARN("%s[%d] Please change ':=' to '=' for list assignment",
1607                                      cf_pair_filename(cp), cf_pair_lineno(cp));
1608                         }
1609
1610                         if (map->rhs->type != TMPL_TYPE_LIST) {
1611                                 cf_log_err(map->ci, "Invalid source for list assignment '%s := ...'", map->lhs->name);
1612                                 return -1;
1613                         }
1614                         break;
1615
1616                 case T_OP_EQ:
1617                         if (map->rhs->type != TMPL_TYPE_EXEC) {
1618                                 cf_log_err(map->ci, "Invalid source for list assignment '%s = ...'", map->lhs->name);
1619                                 return -1;
1620                         }
1621                         break;
1622
1623                 default:
1624                         cf_log_err(map->ci, "Operator \"%s\" not allowed for list assignment",
1625                                    fr_int2str(fr_tokens, map->op, "<INVALID>"));
1626                         return -1;
1627                 }
1628         }
1629
1630         /*
1631          *      If the map has a unary operator there's no further
1632          *      processing we need to, as RHS is unused.
1633          */
1634         if (map->op == T_OP_CMP_FALSE) return 0;
1635
1636         /*
1637          *      If LHS is an attribute, and RHS is a literal, we can
1638          *      preparse the information into a TMPL_TYPE_DATA.
1639          *
1640          *      Unless it's a unary operator in which case we
1641          *      ignore map->rhs.
1642          */
1643         if ((map->lhs->type == TMPL_TYPE_ATTR) && (map->rhs->type == TMPL_TYPE_LITERAL)) {
1644                 /*
1645                  *      It's a literal string, just copy it.
1646                  *      Don't escape anything.
1647                  */
1648                 if (!cf_new_escape &&
1649                     (map->lhs->tmpl_da->type == PW_TYPE_STRING) &&
1650                     (cf_pair_value_type(cp) == T_SINGLE_QUOTED_STRING)) {
1651                         tmpl_cast_in_place_str(map->rhs);
1652                 } else {
1653                         if (tmpl_cast_in_place(map->rhs, map->lhs->tmpl_da->type, map->lhs->tmpl_da) < 0) {
1654                                 cf_log_err(map->ci, "%s", fr_strerror());
1655                                 return -1;
1656                         }
1657
1658                         /*
1659                          *      Fixup LHS da if it doesn't match the type
1660                          *      of the RHS.
1661                          */
1662                         if (map->lhs->tmpl_da->type != map->rhs->tmpl_data_type) {
1663                                 DICT_ATTR const *da;
1664
1665                                 da = dict_attrbytype(map->lhs->tmpl_da->attr, map->lhs->tmpl_da->vendor,
1666                                                      map->rhs->tmpl_data_type);
1667                                 if (!da) {
1668                                         fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1669                                                            fr_int2str(dict_attr_types, map->rhs->tmpl_data_type,
1670                                                            "<INVALID>"), map->lhs->tmpl_da->name);
1671                                         return -1;
1672                                 }
1673                                 map->lhs->tmpl_da = da;
1674                         }
1675                 }
1676         } /* else we can't precompile the data */
1677
1678         return 0;
1679 }
1680
1681
1682 #ifdef WITH_UNLANG
1683 static modcallable *do_compile_modupdate(modcallable *parent, rlm_components_t component,
1684                                          CONF_SECTION *cs, char const *name2)
1685 {
1686         int rcode;
1687         modgroup *g;
1688         modcallable *csingle;
1689
1690         value_pair_map_t *head;
1691
1692         /*
1693          *      This looks at cs->name2 to determine which list to update
1694          */
1695         rcode = map_afrom_cs(&head, cs, PAIR_LIST_REQUEST, PAIR_LIST_REQUEST, modcall_fixup_update, NULL, 128);
1696         if (rcode < 0) return NULL; /* message already printed */
1697         if (!head) {
1698                 cf_log_err_cs(cs, "'update' sections cannot be empty");
1699                 return NULL;
1700         }
1701
1702         g = talloc_zero(parent, modgroup);
1703         csingle = mod_grouptocallable(g);
1704
1705         csingle->parent = parent;
1706         csingle->next = NULL;
1707
1708         if (name2) {
1709                 csingle->name = name2;
1710         } else {
1711                 csingle->name = "";
1712         }
1713         csingle->type = MOD_UPDATE;
1714         csingle->method = component;
1715
1716         memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
1717                sizeof(csingle->actions));
1718
1719         g->grouptype = GROUPTYPE_SIMPLE;
1720         g->children = NULL;
1721         g->cs = cs;
1722         g->map = talloc_steal(g, head);
1723
1724         return csingle;
1725 }
1726
1727
1728 static modcallable *do_compile_modswitch (modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1729 {
1730         CONF_ITEM *ci;
1731         FR_TOKEN type;
1732         char const *name2;
1733         bool had_seen_default = false;
1734         modcallable *csingle;
1735         modgroup *g;
1736         ssize_t slen;
1737         vp_tmpl_t *vpt;
1738
1739         name2 = cf_section_name2(cs);
1740         if (!name2) {
1741                 cf_log_err_cs(cs, "You must specify a variable to switch over for 'switch'");
1742                 return NULL;
1743         }
1744
1745         if (!cf_item_find_next(cs, NULL)) {
1746                 cf_log_err_cs(cs, "'switch' statements cannot be empty");
1747                 return NULL;
1748         }
1749
1750         /*
1751          *      Create the template.  If we fail, AND it's a bare word
1752          *      with &Foo-Bar, it MAY be an attribute defined by a
1753          *      module.  Allow it for now.  The pass2 checks below
1754          *      will fix it up.
1755          */
1756         type = cf_section_name2_type(cs);
1757         slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1758         if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1759                 char *spaces, *text;
1760
1761                 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1762
1763                 cf_log_err_cs(cs, "Syntax error");
1764                 cf_log_err_cs(cs, "%s", name2);
1765                 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1766
1767                 talloc_free(spaces);
1768                 talloc_free(text);
1769
1770                 return NULL;
1771         }
1772
1773         /*
1774          *      Otherwise a NULL vpt may refer to an attribute defined
1775          *      by a module.  That is checked in pass 2.
1776          */
1777
1778         if (vpt->type == TMPL_TYPE_LIST) {
1779                 cf_log_err_cs(cs, "Syntax error: Cannot switch over list '%s'", name2);
1780                 return NULL;
1781         }
1782
1783
1784         /*
1785          *      Walk through the children of the switch section,
1786          *      ensuring that they're all 'case' statements
1787          */
1788         for (ci = cf_item_find_next(cs, NULL);
1789              ci != NULL;
1790              ci = cf_item_find_next(cs, ci)) {
1791                 CONF_SECTION *subcs;
1792                 char const *name1;
1793
1794                 if (!cf_item_is_section(ci)) {
1795                         if (!cf_item_is_pair(ci)) continue;
1796
1797                         cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1798                         talloc_free(vpt);
1799                         return NULL;
1800                 }
1801
1802                 subcs = cf_item_to_section(ci); /* can't return NULL */
1803                 name1 = cf_section_name1(subcs);
1804
1805                 if (strcmp(name1, "case") != 0) {
1806                         cf_log_err(ci, "\"switch\" sections can only have \"case\" subsections");
1807                         talloc_free(vpt);
1808                         return NULL;
1809                 }
1810
1811                 name2 = cf_section_name2(subcs);
1812                 if (!name2) {
1813                         if (!had_seen_default) {
1814                                 had_seen_default = true;
1815                                 continue;
1816                         }
1817
1818                         cf_log_err(ci, "Cannot have two 'default' case statements");
1819                         talloc_free(vpt);
1820                         return NULL;
1821                 }
1822         }
1823
1824         csingle = do_compile_modgroup(parent, component, cs,
1825                                       GROUPTYPE_SIMPLE,
1826                                       GROUPTYPE_SIMPLE,
1827                                       MOD_SWITCH);
1828         if (!csingle) {
1829                 talloc_free(vpt);
1830                 return NULL;
1831         }
1832
1833         g = mod_callabletogroup(csingle);
1834         g->vpt = talloc_steal(g, vpt);
1835
1836         return csingle;
1837 }
1838
1839 static modcallable *do_compile_modcase(modcallable *parent, rlm_components_t component, CONF_SECTION *cs)
1840 {
1841         int i;
1842         char const *name2;
1843         modcallable *csingle;
1844         modgroup *g;
1845         vp_tmpl_t *vpt;
1846
1847         if (!parent || (parent->type != MOD_SWITCH)) {
1848                 cf_log_err_cs(cs, "\"case\" statements may only appear within a \"switch\" section");
1849                 return NULL;
1850         }
1851
1852         /*
1853          *      case THING means "match THING"
1854          *      case       means "match anything"
1855          */
1856         name2 = cf_section_name2(cs);
1857         if (name2) {
1858                 ssize_t slen;
1859                 FR_TOKEN type;
1860
1861                 type = cf_section_name2_type(cs);
1862
1863                 slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1864                 if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1865                         char *spaces, *text;
1866
1867                         fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1868
1869                         cf_log_err_cs(cs, "Syntax error");
1870                         cf_log_err_cs(cs, "%s", name2);
1871                         cf_log_err_cs(cs, "%s^ %s", spaces, text);
1872
1873                         talloc_free(spaces);
1874                         talloc_free(text);
1875
1876                         return NULL;
1877                 }
1878
1879                 if (vpt->type == TMPL_TYPE_LIST) {
1880                         cf_log_err_cs(cs, "Syntax error: Cannot match list '%s'", name2);
1881                         return NULL;
1882                 }
1883
1884                 /*
1885                  *      Otherwise a NULL vpt may refer to an attribute defined
1886                  *      by a module.  That is checked in pass 2.
1887                  */
1888
1889         } else {
1890                 vpt = NULL;
1891         }
1892
1893         csingle = do_compile_modgroup(parent, component, cs,
1894                                       GROUPTYPE_SIMPLE,
1895                                       GROUPTYPE_SIMPLE,
1896                                       MOD_CASE);
1897         if (!csingle) {
1898                 talloc_free(vpt);
1899                 return NULL;
1900         }
1901
1902         /*
1903          *      The interpretor expects this to be NULL for the
1904          *      default case.  do_compile_modgroup sets it to name2,
1905          *      unless name2 is NULL, in which case it sets it to name1.
1906          */
1907         csingle->name = name2;
1908
1909         g = mod_callabletogroup(csingle);
1910         g->vpt = talloc_steal(g, vpt);
1911
1912         /*
1913          *      Set all of it's codes to return, so that
1914          *      when we pick a 'case' statement, we don't
1915          *      fall through to processing the next one.
1916          */
1917         for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1918                 csingle->actions[i] = MOD_ACTION_RETURN;
1919         }
1920
1921         return csingle;
1922 }
1923
1924 static modcallable *do_compile_modforeach(modcallable *parent,
1925                                           rlm_components_t component, CONF_SECTION *cs)
1926 {
1927         FR_TOKEN type;
1928         char const *name2;
1929         modcallable *csingle;
1930         modgroup *g;
1931         ssize_t slen;
1932         vp_tmpl_t *vpt;
1933
1934         name2 = cf_section_name2(cs);
1935         if (!name2) {
1936                 cf_log_err_cs(cs,
1937                            "You must specify an attribute to loop over in 'foreach'");
1938                 return NULL;
1939         }
1940
1941         if (!cf_item_find_next(cs, NULL)) {
1942                 cf_log_err_cs(cs, "'foreach' blocks cannot be empty");
1943                 return NULL;
1944         }
1945
1946         /*
1947          *      Create the template.  If we fail, AND it's a bare word
1948          *      with &Foo-Bar, it MAY be an attribute defined by a
1949          *      module.  Allow it for now.  The pass2 checks below
1950          *      will fix it up.
1951          */
1952         type = cf_section_name2_type(cs);
1953         slen = tmpl_afrom_str(cs, &vpt, name2, strlen(name2), type, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1954         if ((slen < 0) && ((type != T_BARE_WORD) || (name2[0] != '&'))) {
1955                 char *spaces, *text;
1956
1957                 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1958
1959                 cf_log_err_cs(cs, "Syntax error");
1960                 cf_log_err_cs(cs, "%s", name2);
1961                 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1962
1963                 talloc_free(spaces);
1964                 talloc_free(text);
1965
1966                 return NULL;
1967         }
1968
1969         /*
1970          *      If we don't have a negative return code, we must have a vpt
1971          *      (mostly to quiet coverity).
1972          */
1973         rad_assert(vpt);
1974
1975         if ((vpt->type != TMPL_TYPE_ATTR) && (vpt->type != TMPL_TYPE_LIST)) {
1976                 cf_log_err_cs(cs, "MUST use attribute or list reference in 'foreach'");
1977                 return NULL;
1978         }
1979
1980         /*
1981          *      Fix up the template to iterate over all instances of
1982          *      the attribute. In a perfect consistent world, users would do
1983          *      foreach &attr[*], but that's taking the consistency thing a bit far.
1984          */
1985         vpt->tmpl_num = NUM_ALL;
1986
1987         csingle = do_compile_modgroup(parent, component, cs,
1988                                       GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
1989                                       MOD_FOREACH);
1990
1991         if (!csingle) {
1992                 talloc_free(vpt);
1993                 return NULL;
1994         }
1995
1996         g = mod_callabletogroup(csingle);
1997         g->vpt = vpt;
1998
1999         return csingle;
2000 }
2001
2002 static modcallable *do_compile_modbreak(modcallable *parent,
2003                                         rlm_components_t component, CONF_ITEM const *ci)
2004 {
2005         CONF_SECTION const *cs = NULL;
2006
2007         for (cs = cf_item_parent(ci);
2008              cs != NULL;
2009              cs = cf_item_parent(cf_section_to_item(cs))) {
2010                 if (strcmp(cf_section_name1(cs), "foreach") == 0) {
2011                         break;
2012                 }
2013         }
2014
2015         if (!cs) {
2016                 cf_log_err(ci, "'break' can only be used in a 'foreach' section");
2017                 return NULL;
2018         }
2019
2020         return do_compile_modgroup(parent, component, NULL,
2021                                    GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2022                                    MOD_BREAK);
2023 }
2024 #endif
2025
2026 static modcallable *do_compile_modserver(modcallable *parent,
2027                                          rlm_components_t component, CONF_ITEM *ci,
2028                                          char const *name,
2029                                          CONF_SECTION *cs,
2030                                          char const *server)
2031 {
2032         modcallable *csingle;
2033         CONF_SECTION *subcs;
2034         modref *mr;
2035
2036         subcs = cf_section_sub_find_name2(cs, comp2str[component], NULL);
2037         if (!subcs) {
2038                 cf_log_err(ci, "Server %s has no %s section",
2039                            server, comp2str[component]);
2040                 return NULL;
2041         }
2042
2043         mr = talloc_zero(parent, modref);
2044
2045         csingle = mod_reftocallable(mr);
2046         csingle->parent = parent;
2047         csingle->next = NULL;
2048         csingle->name = name;
2049         csingle->type = MOD_REFERENCE;
2050         csingle->method = component;
2051
2052         memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2053                sizeof(csingle->actions));
2054
2055         mr->ref_name = strdup(server);
2056         mr->ref_cs = cs;
2057
2058         return csingle;
2059 }
2060
2061 static modcallable *do_compile_modxlat(modcallable *parent,
2062                                        rlm_components_t component, char const *fmt)
2063 {
2064         modcallable *csingle;
2065         modxlat *mx;
2066
2067         mx = talloc_zero(parent, modxlat);
2068
2069         csingle = mod_xlattocallable(mx);
2070         csingle->parent = parent;
2071         csingle->next = NULL;
2072         csingle->name = "expand";
2073         csingle->type = MOD_XLAT;
2074         csingle->method = component;
2075
2076         memcpy(csingle->actions, defaultactions[component][GROUPTYPE_SIMPLE],
2077                sizeof(csingle->actions));
2078
2079         mx->xlat_name = strdup(fmt);
2080         if (fmt[0] != '%') {
2081                 char *p;
2082                 mx->exec = true;
2083
2084                 strcpy(mx->xlat_name, fmt + 1);
2085                 p = strrchr(mx->xlat_name, '`');
2086                 if (p) *p = '\0';
2087         }
2088
2089         return csingle;
2090 }
2091
2092 /*
2093  *      redundant, etc. can refer to modules or groups, but not much else.
2094  */
2095 static int all_children_are_modules(CONF_SECTION *cs, char const *name)
2096 {
2097         CONF_ITEM *ci;
2098
2099         for (ci=cf_item_find_next(cs, NULL);
2100              ci != NULL;
2101              ci=cf_item_find_next(cs, ci)) {
2102                 /*
2103                  *      If we're a redundant, etc. group, then the
2104                  *      intention is to call modules, rather than
2105                  *      processing logic.  These checks aren't
2106                  *      *strictly* necessary, but they keep the users
2107                  *      from doing crazy things.
2108                  */
2109                 if (cf_item_is_section(ci)) {
2110                         CONF_SECTION *subcs = cf_item_to_section(ci);
2111                         char const *name1 = cf_section_name1(subcs);
2112
2113                         if ((strcmp(name1, "if") == 0) ||
2114                             (strcmp(name1, "else") == 0) ||
2115                             (strcmp(name1, "elsif") == 0) ||
2116                             (strcmp(name1, "update") == 0) ||
2117                             (strcmp(name1, "switch") == 0) ||
2118                             (strcmp(name1, "case") == 0)) {
2119                                 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
2120                                        name, name1);
2121                                 return 0;
2122                         }
2123                         continue;
2124                 }
2125
2126                 if (cf_item_is_pair(ci)) {
2127                         CONF_PAIR *cp = cf_item_to_pair(ci);
2128                         if (cf_pair_value(cp) != NULL) {
2129                                 cf_log_err(ci,
2130                                            "Entry with no value is invalid");
2131                                 return 0;
2132                         }
2133                 }
2134         }
2135
2136         return 1;
2137 }
2138
2139
2140 /*
2141  *      Compile one entry of a module call.
2142  */
2143 static modcallable *do_compile_modsingle(modcallable *parent,
2144                                          rlm_components_t component, CONF_ITEM *ci,
2145                                          int grouptype,
2146                                          char const **modname)
2147 {
2148         char const *modrefname, *p;
2149         modsingle *single;
2150         modcallable *csingle;
2151         module_instance_t *this;
2152         CONF_SECTION *cs, *subcs, *modules;
2153         CONF_SECTION *loop;
2154         char const *realname;
2155         rlm_components_t method = component;
2156
2157         if (cf_item_is_section(ci)) {
2158                 char const *name2;
2159
2160                 cs = cf_item_to_section(ci);
2161                 modrefname = cf_section_name1(cs);
2162                 name2 = cf_section_name2(cs);
2163                 if (!name2) name2 = "";
2164
2165                 /*
2166                  *      group{}, redundant{}, or append{} may appear
2167                  *      where a single module instance was expected.
2168                  *      In that case, we hand it off to
2169                  *      compile_modgroup
2170                  */
2171                 if (strcmp(modrefname, "group") == 0) {
2172                         *modname = name2;
2173                         return do_compile_modgroup(parent, component, cs,
2174                                                    GROUPTYPE_SIMPLE,
2175                                                    grouptype, MOD_GROUP);
2176
2177                 } else if (strcmp(modrefname, "redundant") == 0) {
2178                         *modname = name2;
2179
2180                         if (!all_children_are_modules(cs, modrefname)) {
2181                                 return NULL;
2182                         }
2183
2184                         return do_compile_modgroup(parent, component, cs,
2185                                                    GROUPTYPE_REDUNDANT,
2186                                                    grouptype, MOD_GROUP);
2187
2188                 } else if (strcmp(modrefname, "load-balance") == 0) {
2189                         *modname = name2;
2190
2191                         if (!all_children_are_modules(cs, modrefname)) {
2192                                 return NULL;
2193                         }
2194
2195                         return do_compile_modgroup(parent, component, cs,
2196                                                    GROUPTYPE_SIMPLE,
2197                                                    grouptype, MOD_LOAD_BALANCE);
2198
2199                 } else if (strcmp(modrefname, "redundant-load-balance") == 0) {
2200                         *modname = name2;
2201
2202                         if (!all_children_are_modules(cs, modrefname)) {
2203                                 return NULL;
2204                         }
2205
2206                         return do_compile_modgroup(parent, component, cs,
2207                                                    GROUPTYPE_REDUNDANT,
2208                                                    grouptype, MOD_REDUNDANT_LOAD_BALANCE);
2209
2210 #ifdef WITH_UNLANG
2211                 } else  if (strcmp(modrefname, "if") == 0) {
2212                         if (!cf_section_name2(cs)) {
2213                                 cf_log_err(ci, "'if' without condition");
2214                                 return NULL;
2215                         }
2216
2217                         *modname = name2;
2218                         csingle= do_compile_modgroup(parent, component, cs,
2219                                                      GROUPTYPE_SIMPLE,
2220                                                      grouptype, MOD_IF);
2221                         if (!csingle) return NULL;
2222                         *modname = name2;
2223
2224                         return csingle;
2225
2226                 } else  if (strcmp(modrefname, "elsif") == 0) {
2227                         if (parent &&
2228                             ((parent->type == MOD_LOAD_BALANCE) ||
2229                              (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2230                                 cf_log_err(ci, "'elsif' cannot be used in this section");
2231                                 return NULL;
2232                         }
2233
2234                         if (!cf_section_name2(cs)) {
2235                                 cf_log_err(ci, "'elsif' without condition");
2236                                 return NULL;
2237                         }
2238
2239                         *modname = name2;
2240                         return do_compile_modgroup(parent, component, cs,
2241                                                    GROUPTYPE_SIMPLE,
2242                                                    grouptype, MOD_ELSIF);
2243
2244                 } else  if (strcmp(modrefname, "else") == 0) {
2245                         if (parent &&
2246                             ((parent->type == MOD_LOAD_BALANCE) ||
2247                              (parent->type == MOD_REDUNDANT_LOAD_BALANCE))) {
2248                                 cf_log_err(ci, "'else' cannot be used in this section section");
2249                                 return NULL;
2250                         }
2251
2252                         if (cf_section_name2(cs)) {
2253                                 cf_log_err(ci, "Cannot have conditions on 'else'");
2254                                 return NULL;
2255                         }
2256
2257                         *modname = name2;
2258                         return  do_compile_modgroup(parent, component, cs,
2259                                                     GROUPTYPE_SIMPLE,
2260                                                     grouptype, MOD_ELSE);
2261
2262                 } else  if (strcmp(modrefname, "update") == 0) {
2263                         *modname = name2;
2264
2265                         return do_compile_modupdate(parent, component, cs,
2266                                                     name2);
2267
2268                 } else  if (strcmp(modrefname, "switch") == 0) {
2269                         *modname = name2;
2270
2271                         return do_compile_modswitch (parent, component, cs);
2272
2273                 } else  if (strcmp(modrefname, "case") == 0) {
2274                         *modname = name2;
2275
2276                         return do_compile_modcase(parent, component, cs);
2277
2278                 } else  if (strcmp(modrefname, "foreach") == 0) {
2279                         *modname = name2;
2280
2281                         return do_compile_modforeach(parent, component, cs);
2282
2283 #endif
2284                 } /* else it's something like sql { fail = 1 ...} */
2285
2286         } else if (!cf_item_is_pair(ci)) { /* CONF_DATA or some such */
2287                 return NULL;
2288
2289                 /*
2290                  *      Else it's a module reference, with updated return
2291                  *      codes.
2292                  */
2293         } else {
2294                 CONF_PAIR *cp = cf_item_to_pair(ci);
2295                 modrefname = cf_pair_attr(cp);
2296
2297                 /*
2298                  *      Actions (ok = 1), etc. are orthogonal to just
2299                  *      about everything else.
2300                  */
2301                 if (cf_pair_value(cp) != NULL) {
2302                         cf_log_err(ci, "Entry is not a reference to a module");
2303                         return NULL;
2304                 }
2305
2306                 /*
2307                  *      In-place xlat's via %{...}.
2308                  *
2309                  *      This should really be removed from the server.
2310                  */
2311                 if (((modrefname[0] == '%') && (modrefname[1] == '{')) ||
2312                     (modrefname[0] == '`')) {
2313                         return do_compile_modxlat(parent, component,
2314                                                   modrefname);
2315                 }
2316         }
2317
2318 #ifdef WITH_UNLANG
2319         /*
2320          *      These can't be over-ridden.
2321          */
2322         if (strcmp(modrefname, "break") == 0) {
2323                 if (!cf_item_is_pair(ci)) {
2324                         cf_log_err(ci, "Invalid use of 'break' as section name.");
2325                         return NULL;
2326                 }
2327
2328                 return do_compile_modbreak(parent, component, ci);
2329         }
2330
2331         if (strcmp(modrefname, "return") == 0) {
2332                 if (!cf_item_is_pair(ci)) {
2333                         cf_log_err(ci, "Invalid use of 'return' as section name.");
2334                         return NULL;
2335                 }
2336
2337                 return do_compile_modgroup(parent, component, NULL,
2338                                            GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
2339                                            MOD_RETURN);
2340         }
2341 #endif
2342
2343         /*
2344          *      Run a virtual server.  This is really terrible and
2345          *      should be deleted.
2346          */
2347         if (strncmp(modrefname, "server[", 7) == 0) {
2348                 char buffer[256];
2349
2350                 if (!cf_item_is_pair(ci)) {
2351                         cf_log_err(ci, "Invalid syntax");
2352                         return NULL;
2353                 }
2354
2355                 strlcpy(buffer, modrefname + 7, sizeof(buffer));
2356                 p = strrchr(buffer, ']');
2357                 if (!p || p[1] != '\0' || (p == buffer)) {
2358                         cf_log_err(ci, "Invalid server reference in \"%s\".", modrefname);
2359                         return NULL;
2360                 }
2361
2362                 buffer[p - buffer] = '\0';
2363
2364                 cs = cf_section_sub_find_name2(NULL, "server", buffer);
2365                 if (!cs) {
2366                         cf_log_err(ci, "No such server \"%s\".", buffer);
2367                         return NULL;
2368                 }
2369
2370                 /*
2371                  *      Ignore stupid attempts to over-ride the return
2372                  *      code.
2373                  */
2374                 return do_compile_modserver(parent, component, ci,
2375                                             modrefname, cs, buffer);
2376         }
2377
2378         /*
2379          *      We now have a name.  It can be one of two forms.  A
2380          *      bare module name, or a section named for the module,
2381          *      with over-rides for the return codes.
2382          *
2383          *      The name can refer to a real module, in the "modules"
2384          *      section.  In that case, the name will be either the
2385          *      first or second name of the sub-section of "modules".
2386          *
2387          *      Or, the name can refer to a policy, in the "policy"
2388          *      section.  In that case, the name will be first name of
2389          *      the sub-section of "policy".  Unless it's a "redudant"
2390          *      block...
2391          *
2392          *      Or, the name can refer to a "module.method", in which
2393          *      case we're calling a different method than normal for
2394          *      this section.
2395          *
2396          *      Or, the name can refer to a virtual module, in the
2397          *      "instantiate" section.  In that case, the name will be
2398          *      the first of the sub-section of "instantiate".  Unless
2399          *      it's a "redudant" block...
2400          *
2401          *      We try these in sequence, from the bottom up.  This is
2402          *      so that things in "instantiate" and "policy" can
2403          *      over-ride calls to real modules.
2404          */
2405
2406
2407         /*
2408          *      Try:
2409          *
2410          *      instantiate { ... name { ...} ... }
2411          *      instantiate { ... name.method { ...} ... }
2412          *      policy { ... name { .. } .. }
2413          *      policy { ... name.method { .. } .. }
2414          *
2415          *      The "instantiate" virtual modules are identical to the
2416          *      policies at this point.  We should probably get rid of
2417          *      the "instantiate" ones, as they're duplicate and
2418          *      confusing.
2419          */
2420         subcs = NULL;
2421         cs = cf_section_find("instantiate");
2422         if (cs) subcs = cf_section_sub_find_name2(cs, NULL,
2423                                                   modrefname);
2424         if (!subcs &&
2425             (cs = cf_section_find("policy")) != NULL) {
2426                 char buffer[256];
2427
2428                 snprintf(buffer, sizeof(buffer), "%s.%s",
2429                          modrefname, comp2str[component]);
2430
2431                 /*
2432                  *      Prefer name.section, then name.
2433                  */
2434                 subcs = cf_section_sub_find_name2(cs, NULL,
2435                                                           buffer);
2436                 if (!subcs) {
2437                         subcs = cf_section_sub_find_name2(cs, NULL,
2438                                                           modrefname);
2439                 }
2440         }
2441
2442         /*
2443          *      Check that we're not creating a loop.  We may
2444          *      be compiling an "sql" module reference inside
2445          *      of an "sql" policy.  If so, we allow the
2446          *      second "sql" to refer to the module.
2447          */
2448         for (loop = cf_item_parent(ci);
2449              loop && subcs;
2450              loop = cf_item_parent(cf_section_to_item(loop))) {
2451                 if (loop == subcs) {
2452                         subcs = NULL;
2453                 }
2454         }
2455
2456         /*
2457          *      We've found the relevant entry.  It MUST be a
2458          *      sub-section.
2459          *
2460          *      However, it can be a "redundant" block, or just a
2461          *      section name.
2462          */
2463         if (subcs) {
2464                 /*
2465                  *      modules.c takes care of ensuring that this is:
2466                  *
2467                  *      group foo { ...
2468                  *      load-balance foo { ...
2469                  *      redundant foo { ...
2470                  *      redundant-load-balance foo { ...
2471                  *
2472                  *      We can just recurs to compile the section as
2473                  *      if it was found here.
2474                  */
2475                 if (cf_section_name2(subcs)) {
2476                         csingle = do_compile_modsingle(parent,
2477                                                        component,
2478                                                        cf_section_to_item(subcs),
2479                                                        grouptype,
2480                                                        modname);
2481                 } else {
2482                         /*
2483                          *      We have:
2484                          *
2485                          *      foo { ...
2486                          *
2487                          *      So we compile it like it was:
2488                          *
2489                          *      group foo { ...
2490                          */
2491                         csingle = do_compile_modgroup(parent,
2492                                                       component,
2493                                                       subcs,
2494                                                       GROUPTYPE_SIMPLE,
2495                                                       grouptype, MOD_GROUP);
2496                 }
2497
2498                 /*
2499                  *      Return the compiled thing if we can.
2500                  */
2501                 if (!csingle) return NULL;
2502                 if (cf_item_is_pair(ci)) return csingle;
2503
2504                 /*
2505                  *      Else we have a reference to a policy, and that reference
2506                  *      over-rides the return codes for the policy!
2507                  */
2508                 goto action_override;
2509         }
2510
2511         /*
2512          *      Not a virtual module.  It must be a real module.
2513          */
2514         modules = cf_section_find("modules");
2515         this = NULL;
2516         realname = modrefname;
2517
2518         if (modules) {
2519                 /*
2520                  *      Try to load the optional module.
2521                  */
2522                 if (realname[0] == '-') realname++;
2523
2524                 /*
2525                  *      As of v3, the "modules" section contains
2526                  *      modules we use.  Configuration for other
2527                  *      modules belongs in raddb/mods-available/,
2528                  *      which isn't loaded into the "modules" section.
2529                  */
2530                 if (cf_section_sub_find_name2(modules, NULL, realname)) {
2531                         this = find_module_instance(modules, realname, true);
2532                         if (this) goto allocate_csingle;
2533
2534                         /*
2535                          *
2536                          */
2537                         if (realname != modrefname) {
2538                                 return NULL;
2539                         }
2540
2541                 } else {
2542                         /*
2543                          *      We were asked to MAYBE load it and it
2544                          *      doesn't exist.  Return a soft error.
2545                          */
2546                         if (realname != modrefname) {
2547                                 *modname = modrefname;
2548                                 return NULL;
2549                         }
2550                 }
2551         }
2552
2553         /*
2554          *      No module found by that name.  Maybe we're calling
2555          *      module.method
2556          */
2557         p = strrchr(modrefname, '.');
2558         if (p) {
2559                 rlm_components_t i;
2560                 p++;
2561
2562                 /*
2563                  *      Find the component.
2564                  */
2565                 for (i = RLM_COMPONENT_AUTH;
2566                      i < RLM_COMPONENT_COUNT;
2567                      i++) {
2568                         if (strcmp(p, comp2str[i]) == 0) {
2569                                 char buffer[256];
2570
2571                                 strlcpy(buffer, modrefname, sizeof(buffer));
2572                                 buffer[p - modrefname - 1] = '\0';
2573                                 component = i;
2574
2575                                 this = find_module_instance(modules, buffer, true);
2576                                 if (this) {
2577                                         method = i;
2578                                         goto allocate_csingle;
2579                                 }
2580                         }
2581                 }
2582
2583                 /*
2584                  *      FIXME: check for "module", and give error "no
2585                  *      such component" when we don't find the method.
2586                  */
2587         }
2588
2589         /*
2590          *      Can't de-reference it to anything.  Ugh.
2591          */
2592         *modname = NULL;
2593         cf_log_err(ci, "Failed to find \"%s\" as a module or policy.", modrefname);
2594         cf_log_err(ci, "Please verify that the configuration exists in %s/mods-enabled/%s.", get_radius_dir(), modrefname);
2595         return NULL;
2596
2597         /*
2598          *      We know it's all OK, allocate the structures, and fill
2599          *      them in.
2600          */
2601 allocate_csingle:
2602         /*
2603          *      Check if the module in question has the necessary
2604          *      component.
2605          */
2606         if (!this->entry->module->methods[method]) {
2607                 cf_log_err(ci, "\"%s\" modules aren't allowed in '%s' sections -- they have no such method.", this->entry->module->name,
2608                            comp2str[method]);
2609                 return NULL;
2610         }
2611
2612         single = talloc_zero(parent, modsingle);
2613         single->modinst = this;
2614         *modname = this->entry->module->name;
2615
2616         csingle = mod_singletocallable(single);
2617         csingle->parent = parent;
2618         csingle->next = NULL;
2619         if (!parent || (component != RLM_COMPONENT_AUTH)) {
2620                 memcpy(csingle->actions, defaultactions[component][grouptype],
2621                        sizeof csingle->actions);
2622         } else { /* inside Auth-Type has different rules */
2623                 memcpy(csingle->actions, authtype_actions[grouptype],
2624                        sizeof csingle->actions);
2625         }
2626         rad_assert(modrefname != NULL);
2627         csingle->name = realname;
2628         csingle->type = MOD_SINGLE;
2629         csingle->method = method;
2630
2631 action_override:
2632         /*
2633          *      Over-ride the default return codes of the module.
2634          */
2635         if (cf_item_is_section(ci)) {
2636                 CONF_ITEM *csi;
2637
2638                 cs = cf_item_to_section(ci);
2639                 for (csi=cf_item_find_next(cs, NULL);
2640                      csi != NULL;
2641                      csi=cf_item_find_next(cs, csi)) {
2642
2643                         if (cf_item_is_section(csi)) {
2644                                 cf_log_err(csi, "Subsection of module instance call not allowed");
2645                                 talloc_free(csingle);
2646                                 return NULL;
2647                         }
2648
2649                         if (!cf_item_is_pair(csi)) continue;
2650
2651                         if (!compile_action(csingle, cf_item_to_pair(csi))) {
2652                                 talloc_free(csingle);
2653                                 return NULL;
2654                         }
2655                 }
2656         }
2657
2658         return csingle;
2659 }
2660
2661 modcallable *compile_modsingle(TALLOC_CTX *ctx,
2662                                modcallable **parent,
2663                                rlm_components_t component, CONF_ITEM *ci,
2664                                char const **modname)
2665 {
2666         modcallable *ret;
2667
2668         if (!*parent) {
2669                 modcallable *c;
2670                 modgroup *g;
2671                 CONF_SECTION *parentcs;
2672
2673                 g = talloc_zero(ctx, modgroup);
2674                 memset(g, 0, sizeof(*g));
2675                 g->grouptype = GROUPTYPE_SIMPLE;
2676                 c = mod_grouptocallable(g);
2677                 c->next = NULL;
2678                 memcpy(c->actions,
2679                        defaultactions[component][GROUPTYPE_SIMPLE],
2680                        sizeof(c->actions));
2681
2682                 parentcs = cf_item_parent(ci);
2683                 c->name = cf_section_name2(parentcs);
2684                 if (!c->name) {
2685                         c->name = cf_section_name1(parentcs);
2686                 }
2687
2688                 c->type = MOD_GROUP;
2689                 c->method = component;
2690                 g->children = NULL;
2691
2692                 *parent = mod_grouptocallable(g);
2693         }
2694
2695         ret = do_compile_modsingle(*parent, component, ci,
2696                                    GROUPTYPE_SIMPLE,
2697                                    modname);
2698         dump_tree(component, ret);
2699         return ret;
2700 }
2701
2702
2703 /*
2704  *      Internal compile group code.
2705  */
2706 static modcallable *do_compile_modgroup(modcallable *parent,
2707                                         rlm_components_t component, CONF_SECTION *cs,
2708                                         int grouptype, int parentgrouptype, int mod_type)
2709 {
2710         int i;
2711         modgroup *g;
2712         modcallable *c;
2713         CONF_ITEM *ci;
2714
2715         g = talloc_zero(parent, modgroup);
2716         g->grouptype = grouptype;
2717         g->children = NULL;
2718         g->cs = cs;
2719
2720         c = mod_grouptocallable(g);
2721         c->parent = parent;
2722         c->type = mod_type;
2723         c->next = NULL;
2724         memset(c->actions, 0, sizeof(c->actions));
2725
2726         if (!cs) {              /* only for "break" and "return" */
2727                 c->name = "";
2728                 goto set_codes;
2729         }
2730
2731         /*
2732          *      Remember the name for printing, etc.
2733          *
2734          *      FIXME: We may also want to put the names into a
2735          *      rbtree, so that groups can reference each other...
2736          */
2737         c->name = cf_section_name2(cs);
2738         if (!c->name) {
2739                 c->name = cf_section_name1(cs);
2740                 if ((strcmp(c->name, "group") == 0) ||
2741                     (strcmp(c->name, "redundant") == 0)) {
2742                         c->name = "";
2743                 } else if (c->type == MOD_GROUP) {
2744                         c->type = MOD_POLICY;
2745                 }
2746         }
2747
2748 #ifdef WITH_UNLANG
2749         /*
2750          *      Do load-time optimizations
2751          */
2752         if ((c->type == MOD_IF) || (c->type == MOD_ELSIF) || (c->type == MOD_ELSE)) {
2753                 modgroup *f, *p;
2754
2755                 rad_assert(parent != NULL);
2756
2757                 if (c->type == MOD_IF) {
2758                         g->cond = cf_data_find(g->cs, "if");
2759                         rad_assert(g->cond != NULL);
2760
2761                 check_if:
2762                         if (g->cond->type == COND_TYPE_FALSE) {
2763                                 INFO(" # Skipping contents of '%s' as it is always 'false' -- %s:%d",
2764                                      unlang_keyword[g->mc.type],
2765                                      cf_section_filename(g->cs), cf_section_lineno(g->cs));
2766                                 goto set_codes;
2767                         }
2768
2769                 } else if (c->type == MOD_ELSIF) {
2770
2771                         g->cond = cf_data_find(g->cs, "if");
2772                         rad_assert(g->cond != NULL);
2773
2774                         rad_assert(parent != NULL);
2775                         p = mod_callabletogroup(parent);
2776
2777                         rad_assert(p->tail != NULL);
2778
2779                         f = mod_callabletogroup(p->tail);
2780                         rad_assert((f->mc.type == MOD_IF) ||
2781                                    (f->mc.type == MOD_ELSIF));
2782
2783                         /*
2784                          *      If we took the previous condition, we
2785                          *      don't need to take this one.
2786                          *
2787                          *      We reset our condition to 'true', so
2788                          *      that subsequent sections can check
2789                          *      that they don't need to be executed.
2790                          */
2791                         if (f->cond->type == COND_TYPE_TRUE) {
2792                         skip_true:
2793                                 INFO(" # Skipping contents of '%s' as previous '%s' is always  'true' -- %s:%d",
2794                                      unlang_keyword[g->mc.type],
2795                                      unlang_keyword[f->mc.type],
2796                                      cf_section_filename(g->cs), cf_section_lineno(g->cs));
2797                                 g->cond = f->cond;
2798                                 goto set_codes;
2799                         }
2800                         goto check_if;
2801
2802                 } else {
2803                         rad_assert(c->type == MOD_ELSE);
2804
2805                         rad_assert(parent != NULL);
2806                         p = mod_callabletogroup(parent);
2807
2808                         rad_assert(p->tail != NULL);
2809
2810                         f = mod_callabletogroup(p->tail);
2811                         rad_assert((f->mc.type == MOD_IF) ||
2812                                    (f->mc.type == MOD_ELSIF));
2813
2814                         /*
2815                          *      If we took the previous condition, we
2816                          *      don't need to take this one.
2817                          */
2818                         if (f->cond->type == COND_TYPE_TRUE) goto skip_true;
2819                 }
2820
2821                 /*
2822                  *      Else we need to compile this section
2823                  */
2824         }
2825 #endif
2826
2827         /*
2828          *      Loop over the children of this group.
2829          */
2830         for (ci=cf_item_find_next(cs, NULL);
2831              ci != NULL;
2832              ci=cf_item_find_next(cs, ci)) {
2833
2834                 /*
2835                  *      Sections are references to other groups, or
2836                  *      to modules with updated return codes.
2837                  */
2838                 if (cf_item_is_section(ci)) {
2839                         char const *junk = NULL;
2840                         modcallable *single;
2841                         CONF_SECTION *subcs = cf_item_to_section(ci);
2842
2843                         single = do_compile_modsingle(c, component, ci,
2844                                                       grouptype, &junk);
2845                         if (!single) {
2846                                 cf_log_err(ci, "Failed to parse \"%s\" subsection.",
2847                                        cf_section_name1(subcs));
2848                                 talloc_free(c);
2849                                 return NULL;
2850                         }
2851                         add_child(g, single);
2852
2853                 } else if (!cf_item_is_pair(ci)) { /* CONF_DATA */
2854                         continue;
2855
2856                 } else {
2857                         char const *attr, *value;
2858                         CONF_PAIR *cp = cf_item_to_pair(ci);
2859
2860                         attr = cf_pair_attr(cp);
2861                         value = cf_pair_value(cp);
2862
2863                         /*
2864                          *      A CONF_PAIR is either a module
2865                          *      instance with no actions
2866                          *      specified ...
2867                          */
2868                         if (!value) {
2869                                 modcallable *single;
2870                                 char const *junk = NULL;
2871
2872                                 single = do_compile_modsingle(c,
2873                                                               component,
2874                                                               ci,
2875                                                               grouptype,
2876                                                               &junk);
2877                                 if (!single) {
2878                                         if (cf_item_is_pair(ci) &&
2879                                             cf_pair_attr(cf_item_to_pair(ci))[0] == '-') {
2880                                                 continue;
2881                                         }
2882
2883                                         cf_log_err(ci,
2884                                                    "Failed to parse \"%s\" entry.",
2885                                                    attr);
2886                                         talloc_free(c);
2887                                         return NULL;
2888                                 }
2889                                 add_child(g, single);
2890
2891                                 /*
2892                                  *      Or a module instance with action.
2893                                  */
2894                         } else if (!compile_action(c, cp)) {
2895                                 talloc_free(c);
2896                                 return NULL;
2897                         } /* else it worked */
2898                 }
2899         }
2900
2901 set_codes:
2902         /*
2903          *      Set the default actions, if they haven't already been
2904          *      set.
2905          */
2906         for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
2907                 if (!c->actions[i]) {
2908                         if (!parent || (component != RLM_COMPONENT_AUTH)) {
2909                                 c->actions[i] = defaultactions[component][parentgrouptype][i];
2910                         } else { /* inside Auth-Type has different rules */
2911                                 c->actions[i] = authtype_actions[parentgrouptype][i];
2912                         }
2913                 }
2914         }
2915
2916         switch (c->type) {
2917         default:
2918                 break;
2919
2920         case MOD_GROUP:
2921                 if (grouptype != GROUPTYPE_REDUNDANT) break;
2922                 /* FALL-THROUGH */
2923
2924         case MOD_LOAD_BALANCE:
2925         case MOD_REDUNDANT_LOAD_BALANCE:
2926                 if (!g->children) {
2927                         cf_log_err_cs(g->cs, "%s sections cannot be empty",
2928                                       cf_section_name1(g->cs));
2929                         talloc_free(c);
2930                         return NULL;
2931                 }
2932         }
2933
2934         /*
2935          *      FIXME: If there are no children, return NULL?
2936          */
2937         return mod_grouptocallable(g);
2938 }
2939
2940 modcallable *compile_modgroup(modcallable *parent,
2941                               rlm_components_t component, CONF_SECTION *cs)
2942 {
2943         modcallable *ret = do_compile_modgroup(parent, component, cs,
2944                                                GROUPTYPE_SIMPLE,
2945                                                GROUPTYPE_SIMPLE, MOD_GROUP);
2946
2947         if (debug_flag > 3) {
2948                 modcall_debug(ret, 2);
2949         }
2950
2951         return ret;
2952 }
2953
2954 void add_to_modcallable(modcallable *parent, modcallable *this)
2955 {
2956         modgroup *g;
2957
2958         rad_assert(this != NULL);
2959         rad_assert(parent != NULL);
2960
2961         g = mod_callabletogroup(parent);
2962
2963         add_child(g, this);
2964 }
2965
2966
2967 #ifdef WITH_UNLANG
2968 static bool pass2_xlat_compile(CONF_ITEM const *ci, vp_tmpl_t **pvpt, bool convert,
2969                                DICT_ATTR const *da)
2970 {
2971         ssize_t slen;
2972         char *fmt;
2973         char const *error;
2974         xlat_exp_t *head;
2975         vp_tmpl_t *vpt;
2976
2977         vpt = *pvpt;
2978
2979         rad_assert(vpt->type == TMPL_TYPE_XLAT);
2980
2981         fmt = talloc_typed_strdup(vpt, vpt->name);
2982         slen = xlat_tokenize(vpt, fmt, &head, &error);
2983
2984         if (slen < 0) {
2985                 char *spaces, *text;
2986
2987                 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
2988
2989                 cf_log_err(ci, "Failed parsing expanded string:");
2990                 cf_log_err(ci, "%s", text);
2991                 cf_log_err(ci, "%s^ %s", spaces, error);
2992
2993                 talloc_free(spaces);
2994                 talloc_free(text);
2995                 return false;
2996         }
2997
2998         /*
2999          *      Convert %{Attribute-Name} to &Attribute-Name
3000          */
3001         if (convert) {
3002                 vp_tmpl_t *attr;
3003
3004                 attr = xlat_to_tmpl_attr(talloc_parent(vpt), head);
3005                 if (attr) {
3006                         /*
3007                          *      If it's a virtual attribute, leave it
3008                          *      alone.
3009                          */
3010                         if (attr->tmpl_da->flags.virtual) {
3011                                 talloc_free(attr);
3012                                 return true;
3013                         }
3014
3015                         /*
3016                          *      If the attribute is of incompatible
3017                          *      type, leave it alone.
3018                          */
3019                         if (da && (da->type != attr->tmpl_da->type)) {
3020                                 talloc_free(attr);
3021                                 return true;
3022                         }
3023
3024                         if (cf_item_is_pair(ci)) {
3025                                 CONF_PAIR *cp = cf_item_to_pair(ci);
3026
3027                                 WARN("%s[%d] Please change %%{%s} to &%s",
3028                                        cf_pair_filename(cp), cf_pair_lineno(cp),
3029                                        attr->name, attr->name);
3030                         } else {
3031                                 CONF_SECTION *cs = cf_item_to_section(ci);
3032
3033                                 WARN("%s[%d] Please change %%{%s} to &%s",
3034                                        cf_section_filename(cs), cf_section_lineno(cs),
3035                                        attr->name, attr->name);
3036                         }
3037                         TALLOC_FREE(*pvpt);
3038                         *pvpt = attr;
3039                         return true;
3040                 }
3041         }
3042
3043         /*
3044          *      Re-write it to be a pre-parsed XLAT structure.
3045          */
3046         vpt->type = TMPL_TYPE_XLAT_STRUCT;
3047         vpt->tmpl_xlat = head;
3048
3049         return true;
3050 }
3051
3052
3053 #ifdef HAVE_REGEX
3054 static bool pass2_regex_compile(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3055 {
3056         ssize_t slen;
3057         regex_t *preg;
3058
3059         rad_assert(vpt->type == TMPL_TYPE_REGEX);
3060
3061         /*
3062          *      It's a dynamic expansion.  We can't expand the string,
3063          *      but we can pre-parse it as an xlat struct.  In that
3064          *      case, we convert it to a pre-compiled XLAT.
3065          *
3066          *      This is a little more complicated than it needs to be
3067          *      because radius_evaluate_map() keys off of the src
3068          *      template type, instead of the operators.  And, the
3069          *      pass2_xlat_compile() function expects to get passed an
3070          *      XLAT instead of a REGEX.
3071          */
3072         if (strchr(vpt->name, '%')) {
3073                 vpt->type = TMPL_TYPE_XLAT;
3074                 return pass2_xlat_compile(ci, &vpt, false, NULL);
3075         }
3076
3077         slen = regex_compile(vpt, &preg, vpt->name, vpt->len,
3078                              vpt->tmpl_iflag, vpt->tmpl_mflag, true, false);
3079         if (slen <= 0) {
3080                 char *spaces, *text;
3081
3082                 fr_canonicalize_error(vpt, &spaces, &text, slen, vpt->name);
3083
3084                 cf_log_err(ci, "Invalid regular expression:");
3085                 cf_log_err(ci, "%s", text);
3086                 cf_log_err(ci, "%s^ %s", spaces, fr_strerror());
3087
3088                 talloc_free(spaces);
3089                 talloc_free(text);
3090
3091                 return false;
3092         }
3093
3094         vpt->type = TMPL_TYPE_REGEX_STRUCT;
3095         vpt->tmpl_preg = preg;
3096
3097         return true;
3098 }
3099 #endif
3100
3101 static bool pass2_fixup_undefined(CONF_ITEM const *ci, vp_tmpl_t *vpt)
3102 {
3103         DICT_ATTR const *da;
3104
3105         rad_assert(vpt->type == TMPL_TYPE_ATTR_UNDEFINED);
3106
3107         da = dict_attrbyname(vpt->tmpl_unknown_name);
3108         if (!da) {
3109                 cf_log_err(ci, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
3110                 return false;
3111         }
3112
3113         vpt->tmpl_da = da;
3114         vpt->type = TMPL_TYPE_ATTR;
3115         return true;
3116 }
3117
3118 static bool pass2_callback(UNUSED void *ctx, fr_cond_t *c)
3119 {
3120         value_pair_map_t *map;
3121
3122         if (c->type == COND_TYPE_EXISTS) {
3123                 if (c->data.vpt->type == TMPL_TYPE_XLAT) {
3124                         return pass2_xlat_compile(c->ci, &c->data.vpt, true, NULL);
3125                 }
3126
3127                 rad_assert(c->data.vpt->type != TMPL_TYPE_REGEX);
3128
3129                 /*
3130                  *      The existence check might have been &Foo-Bar,
3131                  *      where Foo-Bar is defined by a module.
3132                  */
3133                 if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3134                         if (!pass2_fixup_undefined(c->ci, c->data.vpt)) return false;
3135                         c->pass2_fixup = PASS2_FIXUP_NONE;
3136                 }
3137                 return true;
3138         }
3139
3140         /*
3141          *      Maps have a paircompare fixup applied to them.
3142          *      Others get ignored.
3143          */
3144         if (c->pass2_fixup == PASS2_FIXUP_NONE) {
3145                 if (c->type == COND_TYPE_MAP) {
3146                         map = c->data.map;
3147                         goto check_paircmp;
3148                 }
3149
3150                 return true;
3151         }
3152
3153         map = c->data.map;      /* shorter */
3154
3155         /*
3156          *      Auth-Type := foo
3157          *
3158          *      Where "foo" is dynamically defined.
3159          */
3160         if (c->pass2_fixup == PASS2_FIXUP_TYPE) {
3161                 if (!dict_valbyname(map->lhs->tmpl_da->attr,
3162                                     map->lhs->tmpl_da->vendor,
3163                                     map->rhs->name)) {
3164                         cf_log_err(map->ci, "Invalid reference to non-existent %s %s { ... }",
3165                                    map->lhs->tmpl_da->name,
3166                                    map->rhs->name);
3167                         return false;
3168                 }
3169
3170                 /*
3171                  *      These guys can't have a paircompare fixup applied.
3172                  */
3173                 c->pass2_fixup = PASS2_FIXUP_NONE;
3174                 return true;
3175         }
3176
3177         if (c->pass2_fixup == PASS2_FIXUP_ATTR) {
3178                 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3179                         if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3180                 }
3181
3182                 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3183                         if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3184                 }
3185
3186                 c->pass2_fixup = PASS2_FIXUP_NONE;
3187         }
3188
3189 check_paircmp:
3190         /*
3191          *      Just in case someone adds a new fixup later.
3192          */
3193         rad_assert((c->pass2_fixup == PASS2_FIXUP_NONE) ||
3194                    (c->pass2_fixup == PASS2_PAIRCOMPARE));
3195
3196         /*
3197          *      Precompile xlat's
3198          */
3199         if (map->lhs->type == TMPL_TYPE_XLAT) {
3200                 /*
3201                  *      Don't compile the LHS to an attribute
3202                  *      reference for now.  When we do that, we've got
3203                  *      to check the RHS for type-specific data, and
3204                  *      parse it to a TMPL_TYPE_DATA.
3205                  */
3206                 if (!pass2_xlat_compile(map->ci, &map->lhs, false, NULL)) {
3207                         return false;
3208                 }
3209         }
3210
3211         if (map->rhs->type == TMPL_TYPE_XLAT) {
3212                 /*
3213                  *      Convert the RHS to an attribute reference only
3214                  *      if the LHS is an attribute reference, AND is
3215                  *      of the same type as the RHS.
3216                  *
3217                  *      We can fix this when the code in evaluate.c
3218                  *      can handle strings on the LHS, and attributes
3219                  *      on the RHS.  For now, the code in parser.c
3220                  *      forbids this.
3221                  */
3222                 if (map->lhs->type == TMPL_TYPE_ATTR) {
3223                         DICT_ATTR const *da = c->cast;
3224
3225                         if (!c->cast) da = map->lhs->tmpl_da;
3226
3227                         if (!pass2_xlat_compile(map->ci, &map->rhs, true, da)) {
3228                                 return false;
3229                         }
3230
3231                 } else {
3232                         if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3233                                 return false;
3234                         }
3235                 }
3236         }
3237
3238         /*
3239          *      Convert bare refs to %{Foreach-Variable-N}
3240          */
3241         if ((map->lhs->type == TMPL_TYPE_LITERAL) &&
3242             (strncmp(map->lhs->name, "Foreach-Variable-", 17) == 0)) {
3243                 char *fmt;
3244                 ssize_t slen;
3245                 vp_tmpl_t *vpt;
3246
3247                 fmt = talloc_asprintf(map->lhs, "%%{%s}", map->lhs->name);
3248                 slen = tmpl_afrom_str(map, &vpt, fmt, talloc_array_length(fmt) - 1,
3249                                       T_DOUBLE_QUOTED_STRING, REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3250                 if (slen < 0) {
3251                         char *spaces, *text;
3252
3253                         fr_canonicalize_error(map->ci, &spaces, &text, slen, fr_strerror());
3254
3255                         cf_log_err(map->ci, "Failed converting %s to xlat", map->lhs->name);
3256                         cf_log_err(map->ci, "%s", fmt);
3257                         cf_log_err(map->ci, "%s^ %s", spaces, text);
3258
3259                         talloc_free(spaces);
3260                         talloc_free(text);
3261                         talloc_free(fmt);
3262
3263                         return false;
3264                 }
3265                 talloc_free(map->lhs);
3266                 map->lhs = vpt;
3267         }
3268
3269 #ifdef HAVE_REGEX
3270         if (map->rhs->type == TMPL_TYPE_REGEX) {
3271                 if (!pass2_regex_compile(map->ci, map->rhs)) {
3272                         return false;
3273                 }
3274         }
3275         rad_assert(map->lhs->type != TMPL_TYPE_REGEX);
3276 #endif
3277
3278         /*
3279          *      Only attributes can have a paircompare registered, and
3280          *      they can only be with the current REQUEST, and only
3281          *      with the request pairs.
3282          */
3283         if ((map->lhs->type != TMPL_TYPE_ATTR) ||
3284             (map->lhs->tmpl_request != REQUEST_CURRENT) ||
3285             (map->lhs->tmpl_list != PAIR_LIST_REQUEST)) {
3286                 return true;
3287         }
3288
3289         if (!radius_find_compare(map->lhs->tmpl_da)) return true;
3290
3291         if (map->rhs->type == TMPL_TYPE_ATTR) {
3292                 cf_log_err(map->ci, "Cannot compare virtual attribute %s to another attribute",
3293                            map->lhs->name);
3294                 return false;
3295         }
3296
3297         if (map->rhs->type == TMPL_TYPE_REGEX) {
3298                 cf_log_err(map->ci, "Cannot compare virtual attribute %s via a regex",
3299                            map->lhs->name);
3300                 return false;
3301         }
3302
3303         if (c->cast) {
3304                 cf_log_err(map->ci, "Cannot cast virtual attribute %s",
3305                            map->lhs->name);
3306                 return false;
3307         }
3308
3309         if (map->op != T_OP_CMP_EQ) {
3310                 cf_log_err(map->ci, "Must use '==' for comparisons with virtual attribute %s",
3311                            map->lhs->name);
3312                 return false;
3313         }
3314
3315         /*
3316          *      Mark it as requiring a paircompare() call, instead of
3317          *      paircmp().
3318          */
3319         c->pass2_fixup = PASS2_PAIRCOMPARE;
3320
3321         return true;
3322 }
3323
3324
3325 /*
3326  *      Compile the RHS of update sections to xlat_exp_t
3327  */
3328 static bool modcall_pass2_update(modgroup *g)
3329 {
3330         value_pair_map_t *map;
3331
3332         for (map = g->map; map != NULL; map = map->next) {
3333                 if (map->rhs->type == TMPL_TYPE_XLAT) {
3334                         rad_assert(map->rhs->tmpl_xlat == NULL);
3335
3336                         /*
3337                          *      FIXME: compile to attribute && handle
3338                          *      the conversion in map_to_vp().
3339                          */
3340                         if (!pass2_xlat_compile(map->ci, &map->rhs, false, NULL)) {
3341                                 return false;
3342                         }
3343                 }
3344
3345                 rad_assert(map->rhs->type != TMPL_TYPE_REGEX);
3346
3347                 /*
3348                  *      Deal with undefined attributes now.
3349                  */
3350                 if (map->lhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3351                         if (!pass2_fixup_undefined(map->ci, map->lhs)) return false;
3352                 }
3353
3354                 if (map->rhs->type == TMPL_TYPE_ATTR_UNDEFINED) {
3355                         if (!pass2_fixup_undefined(map->ci, map->rhs)) return false;
3356                 }
3357         }
3358
3359         return true;
3360 }
3361 #endif
3362
3363 /*
3364  *      Do a second-stage pass on compiling the modules.
3365  */
3366 bool modcall_pass2(modcallable *mc)
3367 {
3368         ssize_t slen;
3369         char const *name2;
3370         modcallable *c;
3371         modgroup *g;
3372
3373         for (c = mc; c != NULL; c = c->next) {
3374                 switch (c->type) {
3375                 default:
3376                         rad_assert(0 == 1);
3377                         break;
3378
3379 #ifdef WITH_UNLANG
3380                 case MOD_UPDATE:
3381                         g = mod_callabletogroup(c);
3382                         if (g->done_pass2) goto do_next;
3383
3384                         name2 = cf_section_name2(g->cs);
3385                         if (!name2) {
3386                                 c->debug_name = unlang_keyword[c->type];
3387                         } else {
3388                                 c->debug_name = talloc_asprintf(c, "update %s", name2);
3389                         }
3390
3391                         if (!modcall_pass2_update(g)) {
3392                                 return false;
3393                         }
3394                         g->done_pass2 = true;
3395                         break;
3396
3397                 case MOD_XLAT:   /* @todo: pre-parse xlat's */
3398                 case MOD_REFERENCE:
3399                 case MOD_BREAK:
3400                 case MOD_RETURN:
3401 #endif
3402
3403                 case MOD_SINGLE:
3404                         c->debug_name = c->name;
3405                         break;  /* do nothing */
3406
3407 #ifdef WITH_UNLANG
3408                 case MOD_IF:
3409                 case MOD_ELSIF:
3410                         g = mod_callabletogroup(c);
3411                         if (g->done_pass2) goto do_next;
3412
3413                         name2 = cf_section_name2(g->cs);
3414                         c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3415
3416                         /*
3417                          *      The compilation code takes care of
3418                          *      simplifying 'true' and 'false'
3419                          *      conditions.  For others, we have to do
3420                          *      a second pass to parse && compile
3421                          *      xlats.
3422                          */
3423                         if (!((g->cond->type == COND_TYPE_TRUE) ||
3424                               (g->cond->type == COND_TYPE_FALSE))) {
3425                                 if (!fr_condition_walk(g->cond, pass2_callback, NULL)) {
3426                                         return false;
3427                                 }
3428                         }
3429
3430                         if (!modcall_pass2(g->children)) return false;
3431                         g->done_pass2 = true;
3432                         break;
3433 #endif
3434
3435 #ifdef WITH_UNLANG
3436                 case MOD_SWITCH:
3437                         g = mod_callabletogroup(c);
3438                         if (g->done_pass2) goto do_next;
3439
3440                         name2 = cf_section_name2(g->cs);
3441                         c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3442
3443                         /*
3444                          *      We had &Foo-Bar, where Foo-Bar is
3445                          *      defined by a module.
3446                          */
3447                         if (!g->vpt) {
3448                                 rad_assert(c->name != NULL);
3449                                 rad_assert(c->name[0] == '&');
3450                                 rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3451
3452                                 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3453                                                       cf_section_name2_type(g->cs),
3454                                                       REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3455                                 if (slen < 0) {
3456                                         char *spaces, *text;
3457
3458                                 parse_error:
3459                                         fr_canonicalize_error(g->cs, &spaces, &text, slen, fr_strerror());
3460
3461                                         cf_log_err_cs(g->cs, "Syntax error");
3462                                         cf_log_err_cs(g->cs, "%s", c->name);
3463                                         cf_log_err_cs(g->cs, "%s^ %s", spaces, text);
3464
3465                                         talloc_free(spaces);
3466                                         talloc_free(text);
3467
3468                                         return false;
3469                                 }
3470
3471                                 goto do_children;
3472                         }
3473
3474                         /*
3475                          *      Statically compile xlats
3476                          */
3477                         if (g->vpt->type == TMPL_TYPE_XLAT) {
3478                                 if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3479                                                         &g->vpt, true, NULL)) {
3480                                         return false;
3481                                 }
3482
3483                                 goto do_children;
3484                         }
3485
3486                         /*
3487                          *      We may have: switch Foo-Bar {
3488                          *
3489                          *      where Foo-Bar is an attribute defined
3490                          *      by a module.  Since there's no leading
3491                          *      &, it's parsed as a literal.  But if
3492                          *      we can parse it as an attribute,
3493                          *      switch to using that.
3494                          */
3495                         if (g->vpt->type == TMPL_TYPE_LITERAL) {
3496                                 vp_tmpl_t *vpt;
3497
3498                                 slen = tmpl_afrom_str(g->cs, &vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3499                                                       REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3500                                 if (slen < 0) goto parse_error;
3501                                 if (vpt->type == TMPL_TYPE_ATTR) {
3502                                         talloc_free(g->vpt);
3503                                         g->vpt = vpt;
3504                                 }
3505
3506                                 goto do_children;
3507                         }
3508
3509                         /*
3510                          *      Warn about old-style configuration.
3511                          *
3512                          *      DEPRECATED: switch User-Name { ...
3513                          *      ALLOWED   : switch &User-Name { ...
3514                          */
3515                         if ((g->vpt->type == TMPL_TYPE_ATTR) &&
3516                             (c->name[0] != '&')) {
3517                                 WARN("%s[%d]: Please change %s to &%s",
3518                                        cf_section_filename(g->cs),
3519                                        cf_section_lineno(g->cs),
3520                                        c->name, c->name);
3521                         }
3522
3523                 do_children:
3524                         if (!modcall_pass2(g->children)) return false;
3525                         g->done_pass2 = true;
3526                         break;
3527
3528                 case MOD_CASE:
3529                         g = mod_callabletogroup(c);
3530                         if (g->done_pass2) goto do_next;
3531
3532                         name2 = cf_section_name2(g->cs);
3533                         if (!name2) {
3534                                 c->debug_name = unlang_keyword[c->type];
3535                         } else {
3536                                 c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3537                         }
3538
3539                         rad_assert(c->parent != NULL);
3540                         rad_assert(c->parent->type == MOD_SWITCH);
3541
3542                         /*
3543                          *      The statement may refer to an
3544                          *      attribute which doesn't exist until
3545                          *      all of the modules have been loaded.
3546                          *      Check for that now.
3547                          */
3548                         if (!g->vpt && c->name &&
3549                             (c->name[0] == '&') &&
3550                             (cf_section_name2_type(g->cs) == T_BARE_WORD)) {
3551                                 slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name),
3552                                                       cf_section_name2_type(g->cs),
3553                                                       REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3554                                 if (slen < 0) goto parse_error;
3555                         }
3556
3557                         /*
3558                          *      We have "case {...}".  There's no
3559                          *      argument, so we don't need to check
3560                          *      it.
3561                          */
3562                         if (!g->vpt) goto do_children;
3563
3564                         /*
3565                          *      Do type-specific checks on the case statement
3566                          */
3567                         if (g->vpt->type == TMPL_TYPE_LITERAL) {
3568                                 modgroup *f;
3569
3570                                 f = mod_callabletogroup(mc->parent);
3571                                 rad_assert(f->vpt != NULL);
3572
3573                                 /*
3574                                  *      We're switching over an
3575                                  *      attribute.  Check that the
3576                                  *      values match.
3577                                  */
3578                                 if (f->vpt->type == TMPL_TYPE_ATTR) {
3579                                         rad_assert(f->vpt->tmpl_da != NULL);
3580
3581                                         if (tmpl_cast_in_place(g->vpt, f->vpt->tmpl_da->type, f->vpt->tmpl_da) < 0) {
3582                                                 cf_log_err_cs(g->cs, "Invalid argument for case statement: %s",
3583                                                               fr_strerror());
3584                                                 return false;
3585                                         }
3586                                 }
3587
3588                                 goto do_children;
3589                         }
3590
3591                         /*
3592                          *      Compile and sanity check xlat
3593                          *      expansions.
3594                          */
3595                         if (g->vpt->type == TMPL_TYPE_XLAT) {
3596                                 modgroup *f;
3597
3598                                 f = mod_callabletogroup(mc->parent);
3599                                 rad_assert(f->vpt != NULL);
3600
3601                                 /*
3602                                  *      Don't expand xlat's into an
3603                                  *      attribute of a different type.
3604                                  */
3605                                 if (f->vpt->type == TMPL_TYPE_ATTR) {
3606                                         if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3607                                                                 &g->vpt, true, f->vpt->tmpl_da)) {
3608                                                 return false;
3609                                         }
3610                                 } else {
3611                                         if (!pass2_xlat_compile(cf_section_to_item(g->cs),
3612                                                                 &g->vpt, true, NULL)) {
3613                                                 return false;
3614                                         }
3615                                 }
3616                         }
3617
3618                         if (!modcall_pass2(g->children)) return false;
3619                         g->done_pass2 = true;
3620                         break;
3621
3622                 case MOD_FOREACH:
3623                         g = mod_callabletogroup(c);
3624                         if (g->done_pass2) goto do_next;
3625
3626                         name2 = cf_section_name2(g->cs);
3627                         c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], name2);
3628
3629                         /*
3630                          *      Already parsed, handle the children.
3631                          */
3632                         if (g->vpt) goto check_children;
3633
3634                         /*
3635                          *      We had &Foo-Bar, where Foo-Bar is
3636                          *      defined by a module.
3637                          */
3638                         rad_assert(c->name != NULL);
3639                         rad_assert(c->name[0] == '&');
3640                         rad_assert(cf_section_name2_type(g->cs) == T_BARE_WORD);
3641
3642                         /*
3643                          *      The statement may refer to an
3644                          *      attribute which doesn't exist until
3645                          *      all of the modules have been loaded.
3646                          *      Check for that now.
3647                          */
3648                         slen = tmpl_afrom_str(g->cs, &g->vpt, c->name, strlen(c->name), cf_section_name2_type(g->cs),
3649                                               REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
3650                         if (slen < 0) goto parse_error;
3651
3652                 check_children:
3653                         rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST));
3654                         if (g->vpt->tmpl_num != NUM_ALL) {
3655                                 cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
3656                                 return false;
3657                         }
3658                         if (!modcall_pass2(g->children)) return false;
3659                         g->done_pass2 = true;
3660                         break;
3661
3662                 case MOD_ELSE:
3663                         c->debug_name = unlang_keyword[c->type];
3664                         goto do_recurse;
3665
3666                 case MOD_POLICY:
3667                         g = mod_callabletogroup(c);
3668                         c->debug_name = talloc_asprintf(c, "%s %s", unlang_keyword[c->type], cf_section_name1(g->cs));
3669                         goto do_recurse;
3670 #endif
3671
3672                 case MOD_GROUP:
3673                 case MOD_LOAD_BALANCE:
3674                 case MOD_REDUNDANT_LOAD_BALANCE:
3675                         c->debug_name = unlang_keyword[c->type];
3676
3677 #ifdef WITH_UNLANG
3678                 do_recurse:
3679 #endif
3680                         g = mod_callabletogroup(c);
3681                         if (!g->cs) {
3682                                 c->debug_name = mc->name; /* for authorize, etc. */
3683
3684                         } else if (c->type == MOD_GROUP) { /* for Auth-Type, etc. */
3685                                 char const *name1 = cf_section_name1(g->cs);
3686
3687                                 if (strcmp(name1, unlang_keyword[c->type]) != 0) {
3688                                         c->debug_name = talloc_asprintf(c, "%s %s", name1, cf_section_name2(g->cs));
3689                                 }
3690                         }
3691
3692                         if (g->done_pass2) goto do_next;
3693                         if (!modcall_pass2(g->children)) return false;
3694                         g->done_pass2 = true;
3695                         break;
3696                 }
3697
3698         do_next:
3699                 rad_assert(c->debug_name != NULL);
3700         }
3701
3702         return true;
3703 }
3704
3705 void modcall_debug(modcallable *mc, int depth)
3706 {
3707         modcallable *this;
3708         modgroup *g;
3709         value_pair_map_t *map;
3710         char buffer[1024];
3711
3712         for (this = mc; this != NULL; this = this->next) {
3713                 switch (this->type) {
3714                 default:
3715                         break;
3716
3717                 case MOD_SINGLE: {
3718                         modsingle *single = mod_callabletosingle(this);
3719
3720                         DEBUG("%.*s%s", depth, modcall_spaces,
3721                                 single->modinst->name);
3722                         }
3723                         break;
3724
3725 #ifdef WITH_UNLANG
3726                 case MOD_UPDATE:
3727                         g = mod_callabletogroup(this);
3728                         DEBUG("%.*s%s {", depth, modcall_spaces,
3729                                 unlang_keyword[this->type]);
3730
3731                         for (map = g->map; map != NULL; map = map->next) {
3732                                 map_prints(buffer, sizeof(buffer), map);
3733                                 DEBUG("%.*s%s", depth + 1, modcall_spaces, buffer);
3734                         }
3735
3736                         DEBUG("%.*s}", depth, modcall_spaces);
3737                         break;
3738
3739                 case MOD_ELSE:
3740                         g = mod_callabletogroup(this);
3741                         DEBUG("%.*s%s {", depth, modcall_spaces,
3742                                 unlang_keyword[this->type]);
3743                         modcall_debug(g->children, depth + 1);
3744                         DEBUG("%.*s}", depth, modcall_spaces);
3745                         break;
3746
3747                 case MOD_IF:
3748                 case MOD_ELSIF:
3749                         g = mod_callabletogroup(this);
3750                         fr_cond_sprint(buffer, sizeof(buffer), g->cond);
3751                         DEBUG("%.*s%s (%s) {", depth, modcall_spaces,
3752                                 unlang_keyword[this->type], buffer);
3753                         modcall_debug(g->children, depth + 1);
3754                         DEBUG("%.*s}", depth, modcall_spaces);
3755                         break;
3756
3757                 case MOD_SWITCH:
3758                 case MOD_CASE:
3759                         g = mod_callabletogroup(this);
3760                         tmpl_prints(buffer, sizeof(buffer), g->vpt, NULL);
3761                         DEBUG("%.*s%s %s {", depth, modcall_spaces,
3762                                 unlang_keyword[this->type], buffer);
3763                         modcall_debug(g->children, depth + 1);
3764                         DEBUG("%.*s}", depth, modcall_spaces);
3765                         break;
3766
3767                 case MOD_POLICY:
3768                 case MOD_FOREACH:
3769                         g = mod_callabletogroup(this);
3770                         DEBUG("%.*s%s %s {", depth, modcall_spaces,
3771                                 unlang_keyword[this->type], this->name);
3772                         modcall_debug(g->children, depth + 1);
3773                         DEBUG("%.*s}", depth, modcall_spaces);
3774                         break;
3775
3776                 case MOD_BREAK:
3777                         DEBUG("%.*sbreak", depth, modcall_spaces);
3778                         break;
3779
3780 #endif
3781                 case MOD_GROUP:
3782                         g = mod_callabletogroup(this);
3783                         DEBUG("%.*s%s {", depth, modcall_spaces,
3784                               unlang_keyword[this->type]);
3785                         modcall_debug(g->children, depth + 1);
3786                         DEBUG("%.*s}", depth, modcall_spaces);
3787                         break;
3788
3789
3790                 case MOD_LOAD_BALANCE:
3791                 case MOD_REDUNDANT_LOAD_BALANCE:
3792                         g = mod_callabletogroup(this);
3793                         DEBUG("%.*s%s {", depth, modcall_spaces,
3794                                 unlang_keyword[this->type]);
3795                         modcall_debug(g->children, depth + 1);
3796                         DEBUG("%.*s}", depth, modcall_spaces);
3797                         break;
3798                 }
3799         }
3800 }