]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Allow foreach to operate on lists
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 2 Jan 2015 23:36:29 +0000 (18:36 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 2 Jan 2015 23:37:38 +0000 (18:37 -0500)
No extra code here, just removing restrictions. This was made possible a while ago by the tmpl cursor functions.

src/main/modcall.c
src/tests/keywords/foreach-list [new file with mode: 0644]
src/tests/keywords/foreach-list.attrs [new file with mode: 0644]

index 3bf428039f71f097ab8c7f5d244216a835ce14a2..00593c6adf6e38d005550d5b7e52b5c956f76e2d 100644 (file)
@@ -2051,8 +2051,8 @@ static modcallable *do_compile_modforeach(modcallable *parent,
                return NULL;
        }
 
-       if (vpt && (vpt->type != TMPL_TYPE_ATTR)) {
-               cf_log_err_cs(cs, "MUST use attribute reference in 'foreach'");
+       if (vpt && (vpt->type != TMPL_TYPE_ATTR) && (vpt->type != TMPL_TYPE_LIST)) {
+               cf_log_err_cs(cs, "MUST use attribute or list reference in 'foreach'");
                return NULL;
        }
 
@@ -2061,9 +2061,7 @@ static modcallable *do_compile_modforeach(modcallable *parent,
         *      the attribute. In a perfect consistent world, users would do
         *      foreach &attr[*], but that's taking the consistency thing a bit far.
         */
-       if (vpt && (vpt->type == TMPL_TYPE_ATTR)) {
-               vpt->tmpl_num = NUM_ALL;
-       }
+       vpt->tmpl_num = NUM_ALL;
 
        csingle = do_compile_modgroup(parent, component, cs,
                                      GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE,
@@ -3624,7 +3622,7 @@ bool modcall_pass2(modcallable *mc)
                        if (slen < 0) goto parse_error;
 
                check_children:
-                       rad_assert(g->vpt->type == TMPL_TYPE_ATTR);
+                       rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST));
                        if (g->vpt->tmpl_num != NUM_ALL) {
                                cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'");
                                return false;
diff --git a/src/tests/keywords/foreach-list b/src/tests/keywords/foreach-list
new file mode 100644 (file)
index 0000000..4780e4f
--- /dev/null
@@ -0,0 +1,5 @@
+foreach &request: {
+       update reply {
+               Called-Station-Id += "%{Foreach-Variable-0}"
+       }
+}
diff --git a/src/tests/keywords/foreach-list.attrs b/src/tests/keywords/foreach-list.attrs
new file mode 100644 (file)
index 0000000..aedd599
--- /dev/null
@@ -0,0 +1,21 @@
+#
+#  Input packet
+#
+User-Name = "bob"
+User-Password = "hello"
+Filter-Id = "1"
+Filter-Id += "2"
+Filter-Id += "3"
+Filter-Id += "4"
+
+#
+#  Expected answer
+#
+Response-Packet-Type == Access-Accept
+Called-Station-Id == 'bob'
+Called-Station-Id == 'hello'
+Called-Station-Id == '1'
+Called-Station-Id == '2'
+Called-Station-Id == '3'
+Called-Station-Id == '4'
+