lpad on regular expression matches does not work
Hello all, I try to improve Calling-Station-Id MAC address canonicalization a little bit, so that is should left pad MAC octets with zeroes. I modified default settings as is: mac-addr-regexp = '([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})' This regular expression matches example MAC address properly: [0] => 18:d0:71:f3:70:8 [1] => 18 [2] => d0 [3] => 71 [4] => f3 [5] => 70 [6] => 8 Then I want to make the whole thing this way: rewrite_calling_station_id { if (&Calling-Station-Id && (&Calling-Station-Id =~ /^${policy.mac-addr-regexp}$/i)) { update request { &Calling-Station-Id := "%{toupper:%{lpad:%{1} 2 0}:%{lpad:%{2} 2 0}:%{lpad:%{3} 2 0}:%{lpad:%{4} 2 0}:%{lpad:%{5} 2 0}:%{lpad:%{6} 2 0}}" } updated } else { noop } } And here's surprise - lpad expression function handles only attribute parameters (ex. {lpad:&User-Name 100 0}), but not regexp matches. What am I doing wrong? -- Bests, Tomasz Chiliński, Chilan opiekun projektu LMS - https://lms.org.pl kierownik projektu LMS Plus / LMS+ - https://lms-plus.org -- Pozdrawiam Tomasz Chiliński, Chilan opiekun projektu LMS - https://lms.org.pl kierownik projektu LMS Plus / LMS+ - https://lms-plus.org
W dniu 04.03.2021 11:13, Tomasz Chiliński via Freeradius-Users napisał(a):
Hello all,
I try to improve Calling-Station-Id MAC address canonicalization a little bit, so that is should left pad MAC octets with zeroes.
I modified default settings as is:
mac-addr-regexp = '([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})'
This regular expression matches example MAC address properly:
[0] => 18:d0:71:f3:70:8 [1] => 18 [2] => d0 [3] => 71 [4] => f3 [5] => 70 [6] => 8
Then I want to make the whole thing this way:
rewrite_calling_station_id { if (&Calling-Station-Id && (&Calling-Station-Id =~ /^${policy.mac-addr-regexp}$/i)) { update request { &Calling-Station-Id := "%{toupper:%{lpad:%{1} 2 0}:%{lpad:%{2} 2 0}:%{lpad:%{3} 2 0}:%{lpad:%{4} 2 0}:%{lpad:%{5} 2 0}:%{lpad:%{6} 2 0}}" } updated } else { noop } }
And here's surprise - lpad expression function handles only attribute parameters (ex. {lpad:&User-Name 100 0}), but not regexp matches. What am I doing wrong?
Sorry - I forgot about very important info - I use: freeradius-3.0.20-3.module_el8.3.0+476+0982bc20.x86_64 ;-) -- Bests, Tomasz Chiliński, Chilan opiekun projektu LMS - https://lms.org.pl kierownik projektu LMS Plus / LMS+ - https://lms-plus.org
On Mar 4, 2021, at 5:13 AM, Tomasz Chiliński via Freeradius-Users <freeradius-users@lists.freeradius.org> wrote: rewrite_calling_station_id { if (&Calling-Station-Id && (&Calling-Station-Id =~ /^${policy.mac-addr-regexp}$/i)) { update request { &Calling-Station-Id := "%{toupper:%{lpad:%{1} 2 0}:%{lpad:%{2} 2 0}:%{lpad:%{3} 2 0}:%{lpad:%{4} 2 0}:%{lpad:%{5} 2 0}:%{lpad:%{6} 2 0}}"
lpad takes attributes as the first argument, not strings.
And here's surprise - lpad expression function handles only attribute parameters (ex. {lpad:&User-Name 100 0}), but not regexp matches. What am I doing wrong?
You're not passing an attribute. It's probably not too difficult to add the capability to do expansions. As always, patches are welcome. Alan DeKok.
Tomasz, As Alan said, the current %{lpad:} (also the %{rpad:}) behaviour expects an attribute as you can see in https://github.com/FreeRADIUS/freeradius-server/blob/v3.0.x/raddb/mods-avail... <https://github.com/FreeRADIUS/freeradius-server/blob/v3.0.x/raddb/mods-available/expr#L133> Therefore, you could try something like: … if (&Calling-Station-Id && (&Calling-Station-Id =~ /^${policy.mac-addr-regexp}$/i)) { update control { &Tmp-String-1 := “%{1}” &Tmp-String-2 := “%{2}” &Tmp-String-3 := “%{3}” &Tmp-String-4 := “%{4}” &Tmp-String-5 := “%{5}” &Tmp-String-6 := “%{6}” } update request { &Calling-Station-Id := "%{toupper:%{lpad:&control:Tmp-String-1 2 0}:%{lpad:&control:Tmp-String-2 2 0}:%{lpad:&control:Tmp-String-3 2 0}:%{lpad:&control:Tmp-String-4 2 0}:%{lpad:&control:Tmp-String-5 2 0}:%{lpad:&control:Tmp-String-6 2 0}}" } updated } else { ... -- Jorge Pereira jpereira@freeradius.org
On 4 Mar 2021, at 07:13, Tomasz Chiliński via Freeradius-Users <freeradius-users@lists.freeradius.org> wrote:
Hello all,
I try to improve Calling-Station-Id MAC address canonicalization a little bit, so that is should left pad MAC octets with zeroes.
I modified default settings as is:
mac-addr-regexp = '([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})[:\-\.]([0-9a-f]{1,2})'
This regular expression matches example MAC address properly:
[0] => 18:d0:71:f3:70:8 [1] => 18 [2] => d0 [3] => 71 [4] => f3 [5] => 70 [6] => 8
Then I want to make the whole thing this way:
rewrite_calling_station_id { if (&Calling-Station-Id && (&Calling-Station-Id =~ /^${policy.mac-addr-regexp}$/i)) { update request { &Calling-Station-Id := "%{toupper:%{lpad:%{1} 2 0}:%{lpad:%{2} 2 0}:%{lpad:%{3} 2 0}:%{lpad:%{4} 2 0}:%{lpad:%{5} 2 0}:%{lpad:%{6} 2 0}}" } updated } else { noop } }
And here's surprise - lpad expression function handles only attribute parameters (ex. {lpad:&User-Name 100 0}), but not regexp matches. What am I doing wrong?
-- Bests, Tomasz Chiliński, Chilan opiekun projektu LMS - https://lms.org.pl kierownik projektu LMS Plus / LMS+ - https://lms-plus.org
-- Pozdrawiam Tomasz Chiliński, Chilan opiekun projektu LMS - https://lms.org.pl kierownik projektu LMS Plus / LMS+ - https://lms-plus.org - List info/subscribe/unsubscribe? See http://www.freeradius.org/list/users.html
W dniu 04.03.2021 16:26, Jorge Pereira napisał(a):
Tomasz,
Guys, thanks for all replies and clarifications! I've also found code fragment which seems to register all xlat function handlers: https://github.com/FreeRADIUS/freeradius-server/blob/90026932c8fe92751b14091... As I see some functions are quite old and they are treated as "legacy" ones - they don't implement full symbol substitution, but only radius attributes?
As Alan said, the current %{lpad:} (also the %{rpad:}) behaviour expects an attribute as you can see in https://github.com/FreeRADIUS/freeradius-server/blob/v3.0.x/raddb/mods-avail...
Therefore, you could try something like:
… if (&Calling-Station-Id && (&Calling-Station-Id =~ /^${policy.mac-addr-regexp}$/i)) { update control { &Tmp-String-1 := “%{1}” &Tmp-String-2 := “%{2}” &Tmp-String-3 := “%{3}” &Tmp-String-4 := “%{4}” &Tmp-String-5 := “%{5}” &Tmp-String-6 := “%{6}” } update request {
&Calling-Station-Id := "%{toupper:%{lpad:&control:Tmp-String-1 2 0}:%{lpad:&control:Tmp-String-2 2 0}:%{lpad:&control:Tmp-String-3 2 0}:%{lpad:&control:Tmp-String-4 2 0}:%{lpad:&control:Tmp-String-5 2 0}:%{lpad:&control:Tmp-String-6 2 0}}" } updated } else { ...
Yeah, you're right. I considers such work-around and it could be effective. Thanks for my thought confirmation! Do "modern" xlat function support nested expressions? -- Bests, Tomasz Chiliński, Chilan opiekun projektu LMS - https://lms.org.pl kierownik projektu LMS Plus / LMS+ - https://lms-plus.org
On Mar 4, 2021, at 10:51 AM, Tomasz Chiliński via Freeradius-Users <freeradius-users@lists.freeradius.org> wrote:
W dniu 04.03.2021 16:26, Jorge Pereira napisał(a):
Tomasz,
Guys,
thanks for all replies and clarifications!
I've also found code fragment which seems to register all xlat function handlers:
https://github.com/FreeRADIUS/freeradius-server/blob/90026932c8fe92751b14091...
That's for v4. Don't look at that, look at the v3.0.x branch. You should be able to just edit src/modules/rlm_expr.c, function lpad_xlat(). it's that simple. Alan DeKok.
participants (3)
-
Alan DeKok -
Jorge Pereira -
Tomasz Chiliński