| 1 |
= Conditional Expressions |
| 2 |
|
| 3 |
Conditions are evaluated when parsing xref:if.adoc[if] and |
| 4 |
xref:elsif.adoc[elsif] statements. These conditions allow the server to |
| 5 |
make complex decisions based on one of a number of possible criteria. |
| 6 |
|
| 7 |
.Syntax |
| 8 |
[source,unlang] |
| 9 |
---- |
| 10 |
if ( condition ) { ... |
| 11 |
|
| 12 |
elsif ( condition ) { ... |
| 13 |
---- |
| 14 |
|
| 15 |
Conditions are expressed using the following syntax: |
| 16 |
|
| 17 |
[options="header"] |
| 18 |
|===== |
| 19 |
| Syntax | Description |
| 20 |
| xref:attr.adoc[&Attribute-Name] | Check for attribute existence. |
| 21 |
| xref:condition/return_codes.adoc[rcode] | Check return code of a previous module. |
| 22 |
| xref:condition/operands.adoc[data] | Check value of data. |
| 23 |
| xref:condition/cmp.adoc[lhs OP rhs] | Compare two kinds of data. |
| 24 |
| xref:condition/para.adoc[( condition )] | Check sub-condition |
| 25 |
| xref:condition/not.adoc[! condition] | Negate a conditional check |
| 26 |
| xref:condition/and.adoc[( condition ) && ...] | Check a condition AND the next one |
| 27 |
| xref:condition/or.adoc[( condition ) \|\| ...] | Check a condition OR the next one |
| 28 |
|===== |
| 29 |
|
| 30 |
|
| 31 |
.Examples |
| 32 |
[source,unlang] |
| 33 |
---- |
| 34 |
if ( &User-Name == "bob" ) { |
| 35 |
... |
| 36 |
} |
| 37 |
|
| 38 |
if ( &Framed-IP-Address == 127.0.0.1 ) { |
| 39 |
... |
| 40 |
} |
| 41 |
|
| 42 |
if ( &Calling-Station-Id == "%{sql:SELECT ...}" ) { |
| 43 |
... |
| 44 |
} |
| 45 |
---- |
| 46 |
|
| 47 |
== Load-time Syntax Checks |
| 48 |
|
| 49 |
The server performs a number of checks when it loads the configuration |
| 50 |
files. Unlike version 2, all of the conditions are syntax checked |
| 51 |
when the server loads. This checking greatly aids in creating |
| 52 |
configurations that are correct. Where the configuration is |
| 53 |
incorrect, a descriptive error is produced. |
| 54 |
|
| 55 |
This error contains the filename and line number of the syntax error. |
| 56 |
In addition, it will print out a portion of the line that caused the |
| 57 |
error and will point to the exact character where the error was seen. |
| 58 |
These descriptive messages mean that most errors are easy to find and fix. |
| 59 |
|
| 60 |
== Load-time Optimizations |
| 61 |
|
| 62 |
The server performs a number of optimizations when it loads the |
| 63 |
configuration files. Conditions that have static values are |
| 64 |
evaluated and replaced with the result of the conditional comparison. |
| 65 |
|
| 66 |
.Example |
| 67 |
[source,unlang] |
| 68 |
---- |
| 69 |
if ( 0 == 1 ) { |
| 70 |
... |
| 71 |
} |
| 72 |
---- |
| 73 |
|
| 74 |
The condition `0 == 1` is static and will evaluate to `false`. Since |
| 75 |
it evaluates to `false`, the configuration inside of the `if` |
| 76 |
statement is ignored. Any modules referenced inside of the `if` |
| 77 |
statement will not be loaded. |
| 78 |
|
| 79 |
This optimization is most useful for creating configurations that |
| 80 |
selectively load (or not) certain policies. If the condition above |
| 81 |
was used in version 2, then the configuration inside of the `if` statement |
| 82 |
would be loaded, even though it would never be used. |
| 83 |
|
| 84 |
// Copyright (C) 2020 Network RADIUS SAS. Licenced under CC-by-NC 4.0. |
| 85 |
// Development of this documentation was sponsored by Network RADIUS SAS. |