]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
clean up and add new docs
authorAlan T. DeKok <aland@freeradius.org>
Mon, 5 Dec 2022 18:30:31 +0000 (13:30 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 5 Dec 2022 18:36:59 +0000 (13:36 -0500)
12 files changed:
doc/antora/modules/reference/nav.adoc
doc/antora/modules/reference/pages/unlang/condition/cmp.adoc
doc/antora/modules/reference/pages/unlang/condition/expression.adoc [new file with mode: 0644]
doc/antora/modules/reference/pages/unlang/condition/index.adoc
doc/antora/modules/reference/pages/unlang/condition/operands.adoc
doc/antora/modules/reference/pages/unlang/condition/regex.adoc
doc/antora/modules/reference/pages/unlang/elsif.adoc
doc/antora/modules/reference/pages/unlang/expression.adoc
doc/antora/modules/reference/pages/unlang/if.adoc
doc/antora/modules/reference/pages/unlang/index.adoc
doc/antora/modules/reference/pages/unlang/module.adoc
doc/antora/modules/reference/pages/unlang/switch.adoc

index f26f43987c4754238ba2844b9819cd238d174490..cf64a2972b2486d8595f87390f3f881d72d3f894 100644 (file)
@@ -11,6 +11,7 @@
 **** xref:unlang/edit.adoc[editing]
 **** xref:unlang/else.adoc[else]
 **** xref:unlang/elsif.adoc[elsif]
+**** xref:unlang/expression.adoc[expressions]
 **** xref:unlang/foreach.adoc[foreach]
 **** xref:unlang/group.adoc[group]
 **** xref:unlang/if.adoc[if]
@@ -34,7 +35,7 @@
 
 *** xref:unlang/condition/index.adoc[Conditional Expressions]
 **** xref:unlang/condition/cmp.adoc[Comparisons]
-**** xref:unlang/expression.adoc[expressions]
+**** xref:unlang/condition/expression.adoc[mathematical expressions]
 **** xref:unlang/condition/operands.adoc[Operands]
 **** xref:unlang/condition/return_code.adoc[The Return Code Operator]
 **** xref:unlang/condition/eq.adoc[The '==' Operator]
index 9762aeef4060c3715fc86d2d61712ada2ae66eff..6c144bf0a15cda5ab51a874d9e0d29e81914e476 100644 (file)
@@ -27,12 +27,12 @@ The comparison operators are given below.
 | !== | not equals, or they are not of the same type
 | >= | greater than or equals
 | >  | greater than
-| xref:condition/regex.adoc[=~] | regular expression matches
-| xref:condition/regex.adoc[!~] | regular expression does not match
+| xref:unlang/condition/regex.adoc[=~] | regular expression matches
+| xref:unlang/condition/regex.adoc[!~] | regular expression does not match
 |=====
 
 The comparison operators perform _type-specific_ comparisons.  The
-only exceptions are the xref:condition/regex.adoc[regular expression] operators,
+only exceptions are the xref:unlang/condition/regex.adoc[regular expression] operators,
 which interpret the `lhs` as a printable string, and the `rhs` as a
 regular expression.
 
diff --git a/doc/antora/modules/reference/pages/unlang/condition/expression.adoc b/doc/antora/modules/reference/pages/unlang/condition/expression.adoc
new file mode 100644 (file)
index 0000000..60f854c
--- /dev/null
@@ -0,0 +1,20 @@
+= Mathematical expressions.
+
+It is now possible to use xref:unlang/expression.adoc[expressions] inside of xref:unlang/condition/index.adoc[conditions].
+
+.Syntax
+[source,unlang]
+----
+if (&NAS-Port + 1 == 2 + 3) {
+    ...
+}
+----
+
+There is no longer any need to use the `%{expr:...}` expansion inside of a condition.
+
+See the main xref:unlang/expression.adoc[expressions] page for a list
+of supported operators.  In short, mathematical operations will work,
+and will do what you expect.
+
+// Copyright (C) 2022 Network RADIUS SAS.  Licenced under CC-by-NC 4.0.
+// Development of this documentation was sponsored by Network RADIUS SAS.
index 4d31b0d3b3d12904a8172705b4e04c96d9c02ed1..92b1a7e4479185c6798347af859ef509be440f0d 100644 (file)
@@ -1,7 +1,7 @@
 = Conditional Expressions
 
-Conditions are evaluated when parsing xref:if.adoc[if] and
-xref:elsif.adoc[elsif] statements.  These conditions allow the server to
+Conditions are evaluated when parsing xref:unlang/if.adoc[if] and
+xref:unlang/elsif.adoc[elsif] statements.  These conditions allow the server to
 make complex decisions based on one of a number of possible criteria.
 
 .Syntax
@@ -17,14 +17,15 @@ Conditions are expressed using the following syntax:
 [options="header"]
 |=====
 | Syntax | Description
-| xref:attr.adoc[&Attribute-Name] | Check for attribute existence.
-| xref:condition/return_code.adoc[rcode] | Check return code of a previous module.
-| xref:condition/operands.adoc[data] | Check value of data.
-| xref:condition/cmp.adoc[lhs OP rhs] | Compare two kinds of data.
-| xref:condition/para.adoc[( condition )] | Check sub-condition
-| xref:condition/not.adoc[! condition] | Negate a conditional check
-| xref:condition/and.adoc[( condition ) && ...] | Check a condition AND the next one
-| xref:condition/or.adoc[( condition ) \|\| ...] | Check a condition OR the next one
+| xref:unlang/attr.adoc[&Attribute-Name] | Check for attribute existence.
+| xref:unlang/return_codes.adoc[rcode] | Check return code of a previous module.
+| xref:unlang/condition/operands.adoc[data] | Check value of data.
+| xref:unlang/condition/cmp.adoc[lhs OP rhs] | Compare two kinds of data.
+| xref:unlang/condition/expression.adoc[1 + 2 ...] | Mathematical expressions.
+| xref:unlang/condition/para.adoc[( condition )] | Check sub-condition
+| xref:unlang/condition/not.adoc[! condition] | Negate a conditional check
+| xref:unlang/condition/and.adoc[( condition ) && ...] | Check a condition AND the next one
+| xref:unlang/condition/or.adoc[( condition ) \|\| ...] | Check a condition OR the next one
 |=====
 
 
@@ -66,12 +67,12 @@ evaluated and replaced with the result of the conditional comparison.
 .Example
 [source,unlang]
 ----
-if ( 0 == 1 ) {
+if (0) {
     ...
 }
 ----
 
-The condition `0 == 1` is static and will evaluate to `false`.  Since
+The condition `0` is static and will evaluate to `false`.  Since
 it evaluates to `false`, the configuration inside of the `if`
 statement is ignored.  Any modules referenced inside of the `if`
 statement will not be loaded.
@@ -81,5 +82,41 @@ selectively load (or not) certain policies.  If the condition above
 was used in version 2, then the configuration inside of the `if` statement
 would be loaded, even though it would never be used.
 
+== Practical Suggestions
+
+We have seen many configurations where conditions are written using
+dynamic expansions and double-quoted strings for almost everything.
+For example, the following condition is unnecessarily complex:
+
+.Example with over-use of quotation
+[source,unlang]
+----
+if ("%{Framed-IP-Address}" == "192.0.2.1") {
+    ...
+}
+----
+
+This kind of condition is unnecessarily complex, and over-uses quotes.
+It should instead be written as:
+
+.Simplified Example
+[source,unlang]
+----
+if (&Framed-IP-Address == 192.0.2.1) {
+    ...
+}
+----
+
+In general, the following rules apply:
+
+* There is no need to quote attributes.  `&User-Name` is almost always preferable to `%{User-Name}`.
+
+* There is no need to quote values, unless the value is a string.  `192.0.2.1` is always preferable to `"192.0.2.1"`
+
+* There is no longer a need to use `%{expr:..}` to do math in conditions.  `if (1 + 2 == 3)` will work.  See xref:unlang/condition/expression.adoc[expressions].
+
+* If you want to check that an attribute exists _and_ has a particular value, use `if !(&Attr == value)`, instead of `if (&Attr != value)`.
+
+
 // Copyright (C) 2021 Network RADIUS SAS.  Licenced under CC-by-NC 4.0.
 // Development of this documentation was sponsored by Network RADIUS SAS.
index 43c420bf1ce2aeaed45967daf0a1d971b53ab3a4..ca1daa88589d452cf791c27206c151a8f3f1e451 100644 (file)
@@ -11,7 +11,7 @@ integer
 ----
 
 Any text not matching xref:attr.adoc[&Attribute-Name] or
-xref:condition/return_code.adoc[Return Code] is interpreted as a value for a
+xref:unlang/condition/return_code.adoc[Return Code] is interpreted as a value for a
 particular xref:type/index.adoc[data type].
 
 Double-quoted strings and back-quoted strings are dynamically expanded
index 0d0766b8da7973fa36536f938735abf91df23cfe..59d55b55b8002c8a3064b9ab4a531a57c31a0f28 100644 (file)
@@ -15,7 +15,7 @@
 == Matching
 The regular expression operators perform regular expression matching
 on the data. The `<subject>` field can be an attribute reference or data,
-as with the other xref:condition/cmp.adoc[comparison] operators.  The `/<pattern>/`
+as with the other xref:unlang/condition/cmp.adoc[comparison] operators.  The `/<pattern>/`
 field must be a valid regular expression.
 
 The `=~` operator evaluates to `true` when `data` matches the
index 125d0f4fa676fd5a704bb0b4cd364063fa736c5a..107a903342bbb8b18165ea83e67b5ded9d7dd921 100644 (file)
@@ -3,10 +3,10 @@
 .Syntax
 [source,unlang]
 ----
-if (condition-1) {
+if <condition-1> {
     [ statements-1 ]
 }
-elsif (condition-2) {
+elsif <condition-2> {
     [ statements-2 ]
 }
 else {
@@ -15,11 +15,11 @@ else {
 ----
 
 An `elsif` statement is used to evaluate a subsequent
-xref:condition/index.adoc[condition] after a preceding xref:unlang/if.adoc[if] statement
-evaluates to `false`.  In the example above, when _condition-1_
-evaluates to false, then _statements-1_ are skipped and _condition-2_
+xref:unlang/condition/index.adoc[condition] after a preceding xref:unlang/if.adoc[if] statement
+evaluates to `false`.  In the example above, when _<condition-1>_
+evaluates to false, then _statements-1_ are skipped and _<condition-2>_
 is checked.  When _condition-2_ evaluates true, then _statements-2_
-are executed.  When _condition-2_ evaluates false, then
+are executed.  When _<condition-2>_ evaluates false, then
 _statements-2_ are skipped and _statements-3_ are executed.
 
 As with xref:unlang/if.adoc[if], an `elsif` clause does not need to be followed by
index b10145799558057929255b785d08ed515fcff81e..0bf86c0284cffa45691f5837dd4ace0760f7eb2f 100644 (file)
@@ -1,6 +1,6 @@
 = Expressions
 
-Expressions can be used inside of xref:xlat/index.adoc[dynamic expansions], or inside of xref:condition/index.adoc[conditions].
+Expressions can be used inside of xref:xlat/index.adoc[dynamic expansions], or inside of xref:unlang/condition/index.adoc[conditions].
 
 The following operators are supported (in order of precedence).
 
@@ -33,9 +33,11 @@ The following unary operators are also supported:
 Operator precedence follows the normal rules.
 Division by zero means that the entire expression is invalid.
 
+See the xref:unlang/edit.adoc[edit] documentation for a list of attribute editing operators.
+
 == Conditions
 
-xref:condition/index.adoc[Conditions] in expressions are also
+xref:unlang/condition/index.adoc[Conditions] in expressions are also
 supported.  For example:
 
 [source,unlang]
index e6c519f82976e37d370b494548bf7d13f1e681f5..70b13b2264d20aee0e84dea54c75a4b24411873a 100644 (file)
@@ -3,15 +3,15 @@
 .Syntax
 [source,unlang]
 ----
-if (condition) {
+if <condition> {
     [ statements ]
 }
 ----
 
 .Description
-The `if` statement evaluates a xref:condition/index.adoc[condition].  When the
-_condition_ evaluates to `true`, the statements within the subsection
-are executed.  When the _condition_ evaluates to `false`, those
+The `if` statement evaluates a xref:unlang/condition/index.adoc[condition].  When the
+_<condition>_ evaluates to `true`, the statements within the subsection
+are executed.  When the _<condition>_ evaluates to `false`, those
 statements are skipped.
 
 An `if` statement can optionally be followed by an xref:unlang/else.adoc[else] or
@@ -25,6 +25,51 @@ if (&User-Name == "bob") {
 }
 ----
 
+== Practical Suggestions
+
+There are a number of practical suggestions which make it easier to work with conditions.
+
+=== Brackets are usually optional.
+
+There is no need to put brackets around a _<condition>_.  The following two examples work identically:
+
+.Example With Brackets
+[source,unlang]
+----
+if (&User-Name == "bob") {
+    reject
+}
+----
+
+And without:
+
+.Example Without Brackes
+[source,unlang]
+----
+if &User-Name == "bob" {
+    reject
+}
+----
+
+The same behavior applies to conditions with complex statements using
+`&&` and `||`.  So long as the text between the `if` and the `{` is a
+valid condition, it will be parsed correctly.
+
+=== Multi-line conditions
+
+A _<condition>_ can span multiple lines.  There is no need to use `\\` at the end of a line.  The following condition is valid:
+
+.Example With Brackets
+[source,unlang]
+----
+if (&User-Name ==
+     "bob") {
+    reject
+}
+----
+
+=== Actions
+
 The last entry in an `if` section can also be an xref:unlang/actions.adoc[actions] subsection.
 
 // Copyright (C) 2021 Network RADIUS SAS.  Licenced under CC-by-NC 4.0.
index 50f918897f62a2c05dadf00f1dc678c9b83b9588..628087ab7640ad131f233cbf9644c47cd1d87316 100644 (file)
@@ -55,7 +55,7 @@ replies.  A secondary goal is to do that as quickly as possible.
 
 == Conditional Expressions
 
-xref:condition/index.adoc[Conditional expressions], which are used to check
+xref:unlang/condition/index.adoc[Conditional expressions], which are used to check
 if conditions evaluate to _true_ or _false_.  Conditional expressions
 can be used to control the flow of processing.
 
index af9669b1f25cb90a4e0a5631da7d0e2ce60f51a8..ac2af93bbad4c96484b1c859677049c45454090b 100644 (file)
@@ -37,7 +37,7 @@ the current module call.
 include::partial$rcode_table.adoc[]
 
 These return codes can be used in a subsequent
-xref:condition/index.adoc[conditional expression] thus allowing policies to
+xref:unlang/condition/index.adoc[conditional expression] thus allowing policies to
 perform different actions based on the behaviour of the modules.
 
 .Example
index 94c01d83147337118d37ccbc71438b11f8f0a9e1..e14b34f3eb01d4c4638ce3ad5b83b231bb8da85f 100644 (file)
@@ -18,7 +18,7 @@ switch <expansion> {
 
 A `switch` statement causes the server to evaluate _<expansion>_,
 which can be an xref:unlang/attr.adoc[&Attribute-Name] or
-xref:condition/operands.adoc[data].  The result is compared against
+xref:unlang/condition/operands.adoc[data].  The result is compared against
 _<match-1>_ and _<match-2>_, etc. in order to find a match. If no
 match is found, then the server looks for the `default`
 xref:unlang/case.adoc[case] statement.