| 1 |
= Unlang Policy Language |
| 2 |
|
| 3 |
The server supports a simple processing language called "Unlang", |
| 4 |
which is short for "un-language". The original intention of using an |
| 5 |
"un-language" was to avoid creating yet another programming language. |
| 6 |
Instead, the `unlang` syntax allows for simple _if / then / else_ |
| 7 |
checks, and attribute editing. It does not support recursion, |
| 8 |
subroutines, or more complex flow controls. |
| 9 |
|
| 10 |
Where more complicated functionality is required, we recommend using |
| 11 |
the `lua`, `perl` or `python` modules. Those modules allow the insertion of |
| 12 |
full-featured scripts at any point in the packet processing. |
| 13 |
|
| 14 |
[NOTE] |
| 15 |
==== |
| 16 |
The documentation is this directory is *reference* |
| 17 |
documentation. That is, it describes the syntax of `unlang` keywords, |
| 18 |
using minimal examples. The reference documentation does not, |
| 19 |
however, describe *when* to use the keywords, or *how* to create |
| 20 |
policies. Please see the xref:howto:index.adoc[Howto] section for |
| 21 |
more in-depth "howto" guides. |
| 22 |
==== |
| 23 |
|
| 24 |
The documentation is organized so that each item is on its own page. |
| 25 |
The page beings with a description of the item, followed by some text |
| 26 |
explaining what the item does. The page concludes with one or more |
| 27 |
examples of using the item in `unlang` policies. |
| 28 |
|
| 29 |
The `unlang` processing can be split into some high-level concepts. |
| 30 |
|
| 31 |
== Keywords |
| 32 |
|
| 33 |
xref:keywords.adoc[Keywords], which are the basic statements of the |
| 34 |
language, e.g., xref:load-balance.adoc[load-balance], |
| 35 |
xref:if.adoc[if], xref:else.adoc[else], etc. |
| 36 |
|
| 37 |
.Example |
| 38 |
[source,unlang] |
| 39 |
---- |
| 40 |
load-balance { |
| 41 |
sql1 |
| 42 |
sql2 |
| 43 |
sql3 |
| 44 |
} |
| 45 |
---- |
| 46 |
|
| 47 |
== Conditional Expressions |
| 48 |
|
| 49 |
xref:condition/index.adoc[Conditional expressions], which are used to check |
| 50 |
if conditions evaluate to _true_ or _false_. Conditional expressions |
| 51 |
can be used to control the flow of processing. |
| 52 |
|
| 53 |
.Example |
| 54 |
[source,unlang] |
| 55 |
---- |
| 56 |
if ((&User-Name == "bob") && (&Calling-Station-Id == "00:01:03:04:05")) { |
| 57 |
... |
| 58 |
} |
| 59 |
---- |
| 60 |
|
| 61 |
== Update Statements |
| 62 |
|
| 63 |
xref:update.adoc[Update] statements are used to edit attributes and |
| 64 |
lists of attributes. |
| 65 |
|
| 66 |
Most request packets will result in reply packets that contain |
| 67 |
additional information for the requestor. The `update` section allows |
| 68 |
policies to add attributes to requests, replies, or to other places. |
| 69 |
|
| 70 |
.Example |
| 71 |
[source,unlang] |
| 72 |
---- |
| 73 |
update reply { |
| 74 |
&Session-Timeout := 3600 |
| 75 |
&Framed-IP-Address := 192.0.2.4 |
| 76 |
} |
| 77 |
---- |
| 78 |
|
| 79 |
== String Expansions |
| 80 |
|
| 81 |
xref:xlat/index.adoc[String expansion] Using `%{...}` to perform dynamic |
| 82 |
string expansions. (also known as xref:xlat/index.adoc[xlat]) |
| 83 |
|
| 84 |
String expansions are usually performed in order to get additional |
| 85 |
information which is not immediately available to the policy. This |
| 86 |
information can be taken from almost any source, including other |
| 87 |
attributes, databases, and scripts. |
| 88 |
|
| 89 |
.Example |
| 90 |
[source,unlang] |
| 91 |
---- |
| 92 |
update reply { |
| 93 |
&Framed-IP-Address := "%{sql:SELECT static_ip from table WHERE user = '%{User-Name}'}" |
| 94 |
} |
| 95 |
---- |
| 96 |
|
| 97 |
== Data Types |
| 98 |
|
| 99 |
Each attribute used by the server has an associated |
| 100 |
xref:type/index.adoc[data type]. The `unlang` interpreter enforces |
| 101 |
restrictions on assignments, so that only valid data types can be |
| 102 |
assigned to an attribute. Invalid assignments result in a run-time |
| 103 |
error. |
| 104 |
|
| 105 |
.Example |
| 106 |
[source,unlang] |
| 107 |
---- |
| 108 |
update reply { |
| 109 |
&Framed-IP-Address := 192.0.2.4 |
| 110 |
&Session-Timeout := 5 |
| 111 |
&Reply-Message := "hello" |
| 112 |
} |
| 113 |
---- |
| 114 |
|
| 115 |
== Design Goals of Unlang |
| 116 |
|
| 117 |
The goal of `unlang` is to allow simple policies to be written with |
| 118 |
minimal effort. Conditional checks can be performed by the policies, |
| 119 |
which can then update the request or response attributes based on the |
| 120 |
results of those checks. `unlang` can only be used in a processing |
| 121 |
section, it cannot be used anywhere else, including in configuration |
| 122 |
sections for a client or a module. The reason for this limitation is |
| 123 |
that the language is intended to perform specific actions on requests |
| 124 |
and responses. The client and module sections contain definitions for |
| 125 |
a client or module; they do not define how a request is processed. |
| 126 |
|
| 127 |
`unlang` uses the same the basic syntax as the configuration files. |
| 128 |
The syntax of the configuration file for lines, comments, sections, |
| 129 |
sub-section, etc., all apply to `unlang`. |
| 130 |
|
| 131 |
Where `unlang` differs from the basic configuration file format is in |
| 132 |
complexity and operation. The normal configuration files are |
| 133 |
_declarative_ and they are _static_. That is, they declare variables |
| 134 |
and values for those variables. Those values do not change when the |
| 135 |
server is running. |
| 136 |
|
| 137 |
In contrast, `unlang` performs run-time processing of requests. |
| 138 |
Conditional statements such as xref:if.adoc[if] are evaluated for every |
| 139 |
packet that is received. Attribute editing statements such as |
| 140 |
xref:update.adoc[update] can be used to edit attribute contents or lists |
| 141 |
of attributes. |
| 142 |
|
| 143 |
.Example |
| 144 |
[source,unlang] |
| 145 |
---- |
| 146 |
# First, the keyword 'if' |
| 147 |
|
| 148 |
# followed by condition which checks that the User-Name |
| 149 |
# attribute has value "bob" |
| 150 |
|
| 151 |
if (&User-Name == "bob") { |
| 152 |
# keyword "update" |
| 153 |
|
| 154 |
# followed by instructions to add the Reply-Message |
| 155 |
# attribute to the "reply" list, with contents |
| 156 |
# "Hello, bob" |
| 157 |
|
| 158 |
update reply { |
| 159 |
Reply-Message := "Hello, bob" |
| 160 |
} |
| 161 |
} |
| 162 |
---- |
| 163 |
|
| 164 |
// Copyright (C) 2020 Network RADIUS SAS. Licenced under CC-by-NC 4.0. |
| 165 |
// Development of this documentation was sponsored by Network RADIUS SAS. |