| 1 |
= Regular Expressions |
| 2 |
|
| 3 |
.Syntax |
| 4 |
==== |
| 5 |
[source,unlang] |
| 6 |
---- |
| 7 |
(<subject> =~ /<pattern>/) |
| 8 |
(<subject> =~ /<pattern>/[imsux]) |
| 9 |
|
| 10 |
(<subject> !~ /<pattern>/) |
| 11 |
(<subject> !~ /<pattern>/[imsux]) |
| 12 |
---- |
| 13 |
==== |
| 14 |
|
| 15 |
== Matching |
| 16 |
The regular expression operators perform regular expression matching |
| 17 |
on the data. The `<subject>` field can be an attribute reference or data, |
| 18 |
as with the other xref:condition/cmp.adoc[comparison] operators. The `/<pattern>/` |
| 19 |
field must be a valid regular expression. |
| 20 |
|
| 21 |
The `=~` operator evaluates to `true` when `data` matches the |
| 22 |
`/<pattern>/`. Otherwise, it evaluates to `false`. |
| 23 |
|
| 24 |
The `!~` operator evaluates to `true` when `data` does not match the |
| 25 |
`/<pattern>/`. Otherwise, it evaluates to `true`. |
| 26 |
|
| 27 |
The regular expression comparison is performed on the _string representation_ |
| 28 |
of the left side of the comparison. That is, if the left side is an |
| 29 |
xref:type/numb.adoc[integer], the regular expression will behave is if the |
| 30 |
value `0` was the literal string `"0"`. Similarly, if the left side is an |
| 31 |
xref:attr.adoc[&Attribute-Name], then the regular expression will behave is if |
| 32 |
the attribute was printed to a string, and the match was performed on the |
| 33 |
resulting string. |
| 34 |
|
| 35 |
.Checking if the `User-Name` attribute contains a realm of example.com |
| 36 |
==== |
| 37 |
[source,unlang] |
| 38 |
---- |
| 39 |
if (&User-Name =~ /@example\.com$/) { |
| 40 |
... |
| 41 |
} |
| 42 |
---- |
| 43 |
==== |
| 44 |
|
| 45 |
== Dialects |
| 46 |
|
| 47 |
The syntax of the regular expression is defined by the regular |
| 48 |
expression library available on the local system. |
| 49 |
|
| 50 |
FreeRADIUS currently supports: |
| 51 |
|
| 52 |
* link:https://www.pcre.org/original/doc/html/[libpcre] and |
| 53 |
link:https://www.pcre.org/current/doc/html/[libpcre2] both of which |
| 54 |
provide |
| 55 |
link:https://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions[Perl |
| 56 |
Compatible Regular expressions]. |
| 57 |
* Regex support provided by the local libc implementation, usually |
| 58 |
link:http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended[ |
| 59 |
Posix regular expressions]. |
| 60 |
|
| 61 |
[TIP] |
| 62 |
==== |
| 63 |
Use the output of `radiusd -Xxv` to determine which regular expression library is in use. |
| 64 |
|
| 65 |
.... |
| 66 |
... |
| 67 |
Debug : regex-pcre : no |
| 68 |
Debug : regex-pcre2 : yes |
| 69 |
Debug : regex-posix : no |
| 70 |
Debug : regex-posix-extended : no |
| 71 |
Debug : regex-binsafe : yes |
| 72 |
... |
| 73 |
Debug : pcre2 : 10.33 (2019-04-16) - retrieved at build time |
| 74 |
.... |
| 75 |
==== |
| 76 |
|
| 77 |
[WARNING] |
| 78 |
==== |
| 79 |
Depending on the regular expression library or libc implementation the server |
| 80 |
was built against, the pattern matching function available may not be binary |
| 81 |
safe (see `regex-binsafe` in the output of `radiusd -Xxv`). |
| 82 |
|
| 83 |
If a binary safe regex match function is not available, and a match is |
| 84 |
attempted against a subject that contains one or more `NUL` ('\0') bytes, the |
| 85 |
match will be aborted, any condition that uses the result will evaluate to false, |
| 86 |
and a warning will be emitted. |
| 87 |
==== |
| 88 |
|
| 89 |
== Flags |
| 90 |
|
| 91 |
The regular expression `/<pattern>/` may be followed by one or more flag |
| 92 |
characters. Again, which flags are available depends on the regular expression |
| 93 |
library the server was built with. Multiple flags may be specified per |
| 94 |
`/pattern/`. |
| 95 |
|
| 96 |
.Flags and their uses |
| 97 |
|
| 98 |
[options="header"] |
| 99 |
|===== |
| 100 |
| Flag Character | Available with | Effect |
| 101 |
| `i` | All | Enable case-insensitive matching. |
| 102 |
| `m` | All | '^' and '$' match newlines within the subject. |
| 103 |
| `s` | libpcre[2] | '.' matches anything, including newlines. |
| 104 |
| `u` | libpcre[2] | Treats subjects as UTF8. Invalid UTF8 |
| 105 |
sequences will result in the match failing. |
| 106 |
|`x` | libpcre[2] | Allows comments in expressions by ignoring |
| 107 |
whitespace, and text between '#' and the next |
| 108 |
newline character. |
| 109 |
|===== |
| 110 |
|
| 111 |
== Subcapture groups |
| 112 |
|
| 113 |
When the `=~` or `!~` operators are used, then parentheses in the regular |
| 114 |
expression will sub capture groups, which contain part of the subject string. |
| 115 |
|
| 116 |
The special expansion `%{0}` expands to the portion of the subject that |
| 117 |
matched. The expansions + |
| 118 |
`%{1}`..`%{32}` expand to the contents of any subcapture groups. |
| 119 |
|
| 120 |
When using libpcre[2], named capture groups may also be accessed using the |
| 121 |
built-in expansion + |
| 122 |
`%{regex:<named capture group>}`. |
| 123 |
|
| 124 |
Please see the xref:xlat/builtin.adoc#_0_32[xlat documentation] for |
| 125 |
more information on regular expression matching. |
| 126 |
|
| 127 |
.Extracting the 'user' portion of a realm qualified string |
| 128 |
==== |
| 129 |
[source,unlang] |
| 130 |
---- |
| 131 |
if (&User-Name =~ /^(.*)@example\.com$/) { |
| 132 |
update reply { |
| 133 |
Reply-Message := "Hello %{1}" |
| 134 |
} |
| 135 |
} |
| 136 |
---- |
| 137 |
==== |
| 138 |
|
| 139 |
== Pre-Compiled vs Runtime Compiled |
| 140 |
|
| 141 |
When the server starts any regular expressions comparisons it finds will be |
| 142 |
pre-compiled, and if support is available, JIT'd (converted to machine code) |
| 143 |
to ensure fast execution. |
| 144 |
|
| 145 |
If a pattern contains a xref:xlat/index.adoc[string expansion], the pattern |
| 146 |
cannot be compiled on startup, and will be compiled at runtime each time the |
| 147 |
expression is evaluated. The server will also turn off JITing for runtime |
| 148 |
compiled expressions, as the overhead is greater than the time that would be |
| 149 |
saved during evaluation. |
| 150 |
|
| 151 |
.A runtime compiled regular expression |
| 152 |
==== |
| 153 |
[source,unlang] |
| 154 |
---- |
| 155 |
if (&User-Name =~ /^@%{Tmp-String-0}$/) { |
| 156 |
... |
| 157 |
} |
| 158 |
---- |
| 159 |
==== |
| 160 |
|
| 161 |
To ensure optimal performance you should limit the number of patterns |
| 162 |
containing xref:xlat/index.adoc[string expansions], and if using PCRE, combine |
| 163 |
multiple expressions operating on the same subject into a single expression |
| 164 |
using the PCRE alternation '|' operator. |
| 165 |
|
| 166 |
.Using multiple string expansions and the PCRE alternation operator |
| 167 |
==== |
| 168 |
[source,unlang] |
| 169 |
---- |
| 170 |
if (&User-Name =~ /^@(%{Tmp-String-0}|%{Tmp-String-1})$/) { |
| 171 |
... |
| 172 |
} |
| 173 |
---- |
| 174 |
==== |
| 175 |
|
| 176 |
|
| 177 |
// Licenced under CC-by-NC 4.0. |
| 178 |
// Copyright (C) 2020 Network RADIUS SAS. |
| 179 |
// Copyright (C) 2019 Arran Cudbard-Bell <[email protected]> |
| 180 |
// Development of this documentation was sponsored by Network RADIUS SAS. |