Clean up loopback / inaddr_any checks
[freeradius.git] / man / man5 / unlang.5
1 .\"     # DS - begin display
2 .de DS
3 .RS
4 .nf
5 .sp
6 ..
7 .\"     # DE - end display
8 .de DE
9 .fi
10 .RE
11 .sp
12 ..
13 .TH unlang 5 "19 May 2010" "" "FreeRADIUS Processing un-language"
14 .SH NAME
15 unlang \- FreeRADIUS Processing un\-language
16 .SH DESCRIPTION
17 FreeRADIUS supports a simple processing language in its configuration
18 files.  We call it an "un-language" because the intention is NOT to
19 create yet another programming language.  If you need something more
20 complicated than what is described here, we suggest using the Perl or
21 Python modules rlm_perl, or rlm_python.
22
23 The goal of the language is to allow simple policies to be written
24 with minimal effort.  Those policies are then applied when a request
25 is being processed.  Requests are processed through virtual servers
26 (including the default one), in the sections titled "authorize",
27 "authenticate", "post-auth", "preacct", "accounting", "pre-proxy",
28 "post-proxy", and "session".
29
30 These policies cannot be used in any other part of the configuration
31 files, such as module or client configuration.
32 .SH KEYWORDS
33 The keywords for the language are a combination of pre-defined
34 keywords, and references to loadable module names.  We document only
35 the pre-defined keywords here.
36
37 Subject to a few limitations described below, any keyword can appear
38 in any context.  The language consists of a series of entries, each
39 one one line.  Each entry begins with a keyword.  Entries are
40 organized into lists.  Processing of the language is line by line,
41 from the start of the list to the end.  Actions are executed
42 per-keyword.
43 .IP module-name
44 A reference to the named module.  When processing reaches this point,
45 the pre-compiled module is called.  The module may succeed or fail,
46 and will return a status to "unlang" if so.  This status can be tested
47 in a condition.  See the "Simple Conditions" text in the CONDITIONS
48 section, and MODULE RETURN CODES, below.
49
50 .DS
51         chap  # call the CHAP module
52 .br
53         sql   # call the SQL module
54 .br
55         ...
56 .DE
57 .IP if
58 .br
59 Checks for a particular condition.  If true, the block after the
60 condition is processed.  Otherwise, the block is ignored.  See
61 CONDITIONS, below, for documentation on the format of the conditions.
62
63 .DS
64         if (condition) {
65 .br
66                 ...
67 .br
68         }
69 .DE
70 .IP else
71 .br
72 Define a block to be executed only if the previous "if" condition
73 returned false.
74
75 .DS
76         else {
77 .br
78                 ...
79 .br
80         }
81 .DE
82 .IP elsif
83 .br
84 Define a block to be executed only if the previous "if" condition
85 returned false, and if the specified condition evaluates to true.
86
87 .DS
88         elsif (condition) {
89 .br
90                 ...
91 .br
92         }
93 .DE
94 .IP switch
95 .br
96 Evaluate the given string, and choose the first matching "case"
97 statement inside of the current block.  If the string is surrounded by
98 double quotes, it is expanded as described in the DATA TYPES section,
99 below.
100
101 No statement other than "case" can appear in a "switch" block.
102
103 .DS
104         switch "string" {
105 .br
106                 ...
107 .br
108         }
109 .DE
110 .IP case
111 .br
112 Define a static string to match a parent "switch" statement.  The
113 strings given here are not expanded as is done with the parent
114 "switch" statement.
115
116 A "case" statement cannot appear outside of a "switch" block.
117
118 .DS
119         case string {
120 .br
121                 ...
122 .br
123         }
124 .DE
125
126 A default entry can be defined by omitting the static string.  This
127 entry will be used if no other "case" entry matches.  Only one default
128 entry can exist in a "switch" section.
129
130 .DS
131         case {
132 .br
133                 ...
134 .br
135         }
136 .DE
137 .IP update
138 .br
139 Update a particular attribute list, based on the attributes given in
140 the current block.
141
142 .DS
143         update <list> {
144 .br
145                 attribute = value
146 .br
147                 ...
148 .br
149         }
150 .DE
151
152 The <list> can be one of "request", "reply", "proxy-request",
153 "proxy-reply", "coa", "disconnect", or "control".  The "control" list
154 is the list of attributes maintainted internally by the server that
155 controls how the server processes the request.  Any attribute that
156 does not go in a packet on the network will generally be placed in the
157 "control" list.
158
159 For backwards compatibility with older versions, "check" is accepted
160 as a synonym for "control".  The use of "check" is deprecated, and
161 will be removed in a future release.
162
163 For EAP methods with tunneled authentication sessions (i.e. PEAP and
164 EAP-TTLS), the inner tunnel session can also reference
165 "outer.request", "outer.reply", and "outer.control".  Those references
166 allow you to address the relevant list in the outer tunnel session.
167
168 The "coa" and "disconnect" sections can only be used when the server
169 receives an Access-Request or Accounting-Request.  Use "request" and
170 "reply" instead of "coa" when the server receives a CoA-Request or
171 Disconnect-Request packet.
172
173 Adding one or more attributes to either of the "coa" or "disconnect"
174 list causes server to originate a CoA-Request or Disconnect-Request
175 packet.  That packet is sent when the current Access-Request or
176 Accounting-Request has been finished, and a reply sent to the NAS.
177 See raddb/sites-available/originate-coa for additional information.
178
179 The only contents permitted in an "update" section are attributes and
180 values.  The contents of the "update" section are described in the
181 ATTRIBUTES section below.
182 .IP redundant
183 This section contains a simple list of modules.  The first module is
184 called when the section is being processed.  If the first module
185 succeeds in its operation, then the server stops processing the
186 section, and returns to the parent section.
187
188 If, however, the module fails, then the next module in the list is
189 tried, as described above.  The processing continues until one module
190 succeeds, or until the list has been exhausted.
191
192 Redundant sections can contain only a list of modules, and cannot
193 contain keywords that perform conditional operations (if, else, etc)
194 or update an attribute list.
195
196 .DS
197         redundant {
198 .br
199                 sql1    # try this
200 .br
201                 sql2    # try this only if sql1 fails.
202 .br
203                 ...
204 .br
205         }
206 .DE
207 .IP load-balance
208 This section contains a simple list of modules.  When the section is
209 entered, one module is chosen at random to process the request.  All
210 of the modules in the list should be the same type (e.g. ldap or sql).
211 All of the modules in the list should behave identically, otherwise
212 the load-balance section will return different results for the same
213 request.
214
215 Load-balance sections can contain only a list of modules, and cannot
216 contain keywords that perform conditional operations (if, else, etc)
217 or update an attribute list.
218
219 .DS
220         load-balance {
221 .br
222                 ldap1   # 50% of requests go here
223 .br
224                 ldap2   # 50% of requests go here
225 .br
226         }
227 .DE
228
229 In general, we recommend using "redundant-load-balance" instead of
230 "load-balance".
231 .IP redundant-load-balance
232 This section contains a simple list of modules.  When the section is
233 entered, one module is chosen at random to process the request.  If
234 that module succeeds, then the server stops processing the section.
235 If, however, the module fails, then one of the remaining modules is
236 chosen at random to process the request.  This process repeats until
237 one module succeeds, or until the list has been exhausted.
238
239 All of the modules in the list should be the same type (e.g. ldap or
240 sql).  All of the modules in the list should behave identically,
241 otherwise the load-balance section will return different results for
242 the same request.
243
244 Load-balance sections can contain only a list of modules, and cannot
245 contain keywords that perform conditional operations (if, else, etc)
246 or update an attribute list.
247
248 .DS
249         redundant-load-balance {
250 .br
251                 ldap1   # 50%, unless ldap2 is down, then 100%
252 .br
253                 ldap2   # 50%, unless ldap1 is down, then 100%
254 .br
255         }
256 .DE
257 .SH CONDITIONS
258 The conditions are similar to C conditions in syntax, though
259 quoted strings are supported, as with the Unix shell.
260 .IP Simple
261 conditions
262 .br
263 .DS
264         (foo)
265 .DE
266
267 Evalutes to true if 'foo' is a non-empty string (single quotes, double
268 quotes, or back-quoted).  Also evaluates to true if 'foo' is a
269 non-zero number.  Note that the language is poorly typed, so the
270 string "0000" can be interpreted as a numerical zero.  This issue can
271 be avoided by comparings strings to an empty string, rather than by
272 evaluating the string by itself.
273
274 If the word 'foo' is not a quoted string, then it can be taken as a
275 reference to a named attribute.  See "Referencing attribute lists",
276 below, for examples of attribute references.  The condition evaluates
277 to true if the named attribute exists.
278
279 Otherwise, if the word 'foo' is not a quoted string, and is not an
280 attribute reference, then it is interpreted as a reference to a module
281 return code.  The condition evaluates to true if the most recent
282 module return code matches the name given here.  Valid module return
283 codes are given in MODULE RETURN CODES, below.
284 .IP Negation
285 .DS
286         (!foo)
287 .DE
288
289 Evalutes to true if 'foo' evaluates to false, and vice-versa.
290 .PP
291 Short-circuit operators
292 .RS
293 .br
294 .DS
295         (foo || bar)
296 .br
297         (foo && bar)
298 .DE
299
300 "&&" and "||" are short-circuit operators.  "&&" evaluates the first
301 condition, and evaluates the second condition if and only if the
302 result of the first condition is true.  "||" is similar, but executes
303 the second command if and only if the result of the first condition is
304 false.
305 .RE
306 .IP Comparisons
307 .DS
308         (foo == bar)
309 .DE
310
311 Compares 'foo' to 'bar', and evaluates to true if the comparison holds
312 true.  Valid comparison operators are "==", "!=", "<", "<=", ">",
313 ">=", "=~", and "!~", all with their usual meanings.  Invalid
314 comparison operators are ":=" and "=".
315 .PP
316 Conditions may be nested to any depth, subject only to line length
317 limitations (8192 bytes).
318 .SH DATA TYPES
319 There are only a few data types supported in the language.  Reference
320 to attributes, numbers, and strings.  Any data type can appear in
321 stand-alone condition, in which case they are evaluated as described
322 in "Simple conditions", above.  They can also appear (with some
323 exceptions noted below) on the left-hand or on the right-hand side of
324 a comparison.
325 .IP numbers
326 Numbers are composed of decimal digits.  Floating point, hex, and
327 octal numbers are not supported.  The maximum value for a number is
328 machine-dependent, but is usually 32-bits, including one bit for a
329 sign value.
330 .PP
331 word
332 .RS
333 Text that is not enclosed in quotes is interpreted differently
334 depending on where it occurs in a condition.  On the left hand side of
335 a condition, it is interpreted as a reference to an attribute.  On the
336 right hand side, it is interpreted as a simple string, in the same
337 manner as a single-quoted string.
338
339 Using attribute references permits limited type-specific comparisons,
340 as seen in the examples below.
341
342 .DS
343         if (User-Name == "bob") {
344 .br
345                 ...
346 .br
347         if (Framed-IP-Address > 127.0.0.1) {
348 .br
349                 ...
350 .br
351         if (Service-Type == Login-User) { 
352 .DE
353 .RE
354 .IP """strings"""
355 .RS
356 Double-quoted strings are expanded by inserting the value of any
357 variables (see VARIABLES, below) before being evaluated.  If
358 the result is a number it is evaluated in a numerical context.
359
360 String length is limited by line-length, usually about 8000
361 characters.  A double quote character can be used in a string via
362 the normal back-slash escaping method.  ("like \\"this\\" !")
363 .RE
364 .IP 'strings'
365 Single-quoted strings are evaluated as-is.  Their values are not
366 expanded as with double-quoted strings above, and they are not
367 interpreted as attribute references.
368 .IP `strings`
369 Back-quoted strings are evaluated by expanding the contents of the
370 string, as described above for double-quoted strings.  The resulting
371 command given inside of the string in a sub-shell, and taking the
372 output as a string.  This behavior is much the same as that of Unix
373 shells.
374
375 Note that for security reasons, the input string is split into command
376 and arguments before variable expansion is done.
377
378 For performance reasons, we suggest that the use of back-quoted
379 strings be kept to a minimum.  Executing external programs is
380 relatively expensive, and executing a large number of programs for
381 every request can quickly use all of the CPU time in a server.  If you
382 believe that you need to execute many programs, we suggest finding
383 alternative ways to achieve the same result.  In some cases, using a
384 real language may be sufficient.
385 .IP /regex/i
386 These strings are valid only on the right-hand side of a comparison,
387 and then only when the comparison operator is "=~" or "!~".  They are
388 regular expressions, as implemented by the local regular expression
389 library on the system.  This is usually Posix regular expressions.
390
391 The trailing 'i' is optional, and indicates that the regular
392 expression match should be done in a case-insensitive fashion.
393
394 If the comparison operator is "=~", then parantheses in the regular
395 expression will define variables containing the matching text, as
396 described below in the VARIABLES section.
397 .SH VARIABLES
398 Run-time variables are referenced using the following syntax
399
400 .DS
401         %{Variable-Name}
402 .DE
403
404 Note that unlike C, there is no way to declare variables, or to refer
405 to them outside of a string context.  All references to variables MUST
406 be contained inside of a double-quoted or back-quoted string.
407
408 Many potential variables are defined in the dictionaries that
409 accompany the server.  These definitions define only the name and
410 type, and do not define the value of the variable.  When the server
411 receives a packet, it uses the packet contents to look up entries in
412 the dictionary, and instantiates variables with a name taken from the
413 dictionaries, and a value taken from the packet contents.  This
414 process means that if a variable does not exist, it is usually because
415 it was not mentioned in a packet that the server received.
416
417 Once the variable is instantiated, it is added to an appropriate
418 attribute list, as described below.  In many cases, attributes and
419 variables are inter-changeble, and are often talked about that way.
420 However, variables can also refer to run-time calls to modules, which
421 may perform operations like SQL SELECTs, and which may return the
422 result as the value of the variable.
423 .PP
424 Referencing attribute lists
425 .RS
426 Attribute lists may be referenced via the following syntax
427
428 .DS
429         %{<list>:Attribute-Name}
430 .DE
431
432 Where <list> is one of "request", "reply", "control", "proxy-request",
433 "proxy-reply", or "outer.request", "outer.reply", "outer.control",
434 "outer.proxy-request", or "outer.proxy-reply". just as with the
435 "update" section, above.  The "<list>:" prefix is optional, and if
436 omitted, is assumed to refer to the "request" list.
437
438 When a variable is encountered, the given list is examined for an
439 attribute of the given name.  If found, the variable reference in the
440 string is replaced with the value of that attribute.  Some examples are:
441
442 .DS
443         %{User-Name}
444 .br
445         %{request:User-Name} # same as above
446 .br
447         %{reply:User-Name}
448 .br
449         %{outer.reqest:User-Name} # from inside of a TTLS/PEAP tunnel
450 .DE
451 .RE
452 .PP
453 Results of regular expression matches
454 .RS
455 If a regular expression match has previously been performed, then the
456 special variable %{0} will contain a copy of the input string.  The
457 variables %{1} through %{8} will contain the substring matches,
458 starting from the left-most parantheses, and onwards.  If there are
459 more than 8 parantheses, the additional results will not be placed
460 into any variables.
461 .RE
462 .PP
463 Obtaining results from databases
464 .RS
465 It is useful to query a database for some information, and to use the
466 result in a condition.  The following syntax will call a module, pass
467 it the given string, and replace the variable reference with the
468 resulting string returned from the module.
469
470 .DS
471         %{module: string ...}
472 .DE
473
474 The syntax of the string is module-specific.  Please read the module
475 documentation for additional details.
476 .RE
477 .PP
478 Conditional Syntax
479 .RS
480 Conditional syntax similar to that used in Unix shells may also be
481 used.
482 .IP %{%{Foo}:-bar}
483 If %{Foo} has a value, returns that value.
484 .br
485 Otherwise, returns literal string "bar".
486 .IP %{%{Foo}:-%{Bar}}
487 If %{Foo} has a value, returns that value.
488 .br
489 Otherwise, returns the expansion of %{Bar}.
490
491 These conditional expansions can be nested to almost any depth, such
492 as with %{%{One}:-%{%{Two}:-%{Three}}}
493 .RE
494 .PP
495 String lengths and arrays
496 .RS
497 Similar to a Unix shell, there are ways to reference string lenths,
498 and the second or more instance of an attribute in a list.  If you
499 need this functionality, we recommend using a real language.
500 .IP %{#string}
501 The number of characters in %{string}.  If %{string} is not
502 set, then the length is not set.
503
504 e.g. %{#Junk-junk:-foo} will yeild the string "foo".
505 .IP %{Attribute-Name#}
506 Will print the integer value of the attribute, rather than a decoded
507 VALUE or date.  This feature applies only to attributes of type
508 "date", "integer", "byte", and "short".  It has no effect on any other
509 attributes.  It is used when the numerical value is needed (e.g. Unix
510 seconds), rather than a humanly-readable string.
511
512 e.g. If a request contains "Service-Type = Login-User", the expansion
513 of %{Service-Type#} will yeild "1".
514 .IP %{Attribute-Name[index]}
515 Reference the N'th occurance of the given attribute.  The syntax
516 %{<list>:Attribute-Name[index]} may also be used.  The indexes start
517 at zero.  This feature is NOT available for non-attribute dynamic
518 translations, like %{sql:...}.
519
520 For example, %{User-Name[0]} is the same as %{User-Name}
521
522 The variable %{Cisco-AVPair[2]} will reference the value of the
523 THIRD Cisco-AVPair attribute (if it exists) in the request packet,
524 .IP %{Attribute-Name[#]}
525 Returns the total number of attributes of that name in the relevant
526 attribute list.  The number will usually be between 0 and 200.
527
528 For most requests, %{request:User-Name[#]} == 1
529 .IP %{Attribute-Name[*]}
530 Expands to a single string, with the value of each array
531 member separated by a newline.
532 .IP %{#Attribute-Name[index]}
533 Expands to the length of the string %{Attribute-Name[index]}.
534 .SH ATTRIBUTES
535 The attribute lists described above may be edited by listing one or
536 more attributes in an "update" section.  Once the attributes have been
537 defined, they may be referenced as described above in the VARIABLES
538 section.
539
540 The following syntax defines attributes in an "update" section.  Each
541 attribute and value has to be all on one line in the configuration
542 file.  There is no need for commas or semi-colons after the value.
543
544 .DS
545         Attribute-Name = value
546 .DE
547 .PP
548 Attribute names
549 .RS
550 The Attribute-Name must be a name previously defined in a dictionary.
551 If an undefined name is used, the server will return an error, and
552 will not start.
553 .RE
554 .IP Operators
555 The operator used to assign the value of the attribute may be one of
556 the following, with the given meaning.
557 .RS
558 .IP =
559 Add the attribute to the list, if and only if an attribute of the same
560 name is not already present in that list.
561 .IP := 
562 Add the attribute to the list.  If any attribute of the same name is
563 already present in that list, its value is replaced with the value of
564 the current attribute.
565 .IP +=
566 Add the attribute to the tail of the list, even if attributes of the
567 same name are already present in the list.
568 .RE
569 .PP
570 Enforcement and Filtering Operators
571 .RS
572 The following operators may also be used in addition to the ones
573 listed above.  Their function is to perform enforcement or filtering
574 on attributes in a list.
575 .IP -=
576 Remove all matching attributes from the list.  Both the attribute name
577 and value have to match in order for the attribute to be removed from
578 the list.
579 .IP ==
580 Keep all matching attributes.  Both the attribute name and value have
581 to match in order for the attribute to remain in the list.
582
583 Note that this operator is very different than the '=' operator listed
584 above!
585 .IP <=
586 Keep all attributes having values less than, or equal to, the value
587 given here.  Any larger value is replaced by the value given here.  If
588 no attribute exists, it is added with the value given here, as with
589 "+=".
590
591 This operator is valid only for attributes of integer type.
592 .IP >=
593 Keep all attributes having values greater than, or equal to, the value
594 given here.  Any larger value is replaced by the value given here.  If
595 no attribute exists, it is added with the value given here, as with
596 "+=".
597
598 This operator is valid only for attributes of integer type.
599 .IP !*
600 Delete all occurances of the named attribute, no matter what the
601 value.
602 .RE
603 .IP Values
604 .br
605 The format of the value is attribute-specific, and is usually a
606 string, integer, IP address, etc.  Prior to the attribute being
607 instantiated, the value may be expanded as described above in the DATA
608 TYPES section, above.  This flexibility means that, for example, you
609 can assign an IP address value to an attribute by specifying the IP
610 address directly, or by having the address returned from a database
611 query, or by having the address returned as the output of a program
612 that is executed.
613
614 When string values are finally assigned to a variable, they can have a
615 maximum length of 253 characters.  This limit is due in part to both
616 protocol and internal server requirements.  That is, the strings in
617 the language can be nearly 8k in length, say for a long SQL query.
618 However, the output of that SQL query should be no more than 253
619 characters in length.
620 .SH OTHER KEYWORDS
621 Other keywords in the language are taken from the names of modules
622 loaded by the server.  These keywords are dependent on both the
623 modules, and the local configuration.
624
625 Some use keywords that are defined in the default configuration file
626 are:
627 .IP fail
628 Cause the request to be treated as if a database failure had occurred.
629 .IP noop
630 Do nothing.  This also serves as an instruction to the configurable
631 failover tracking that nothing was done in the current section.
632 .IP ok
633 Instructs the server that the request was processed properly.  This
634 keyword can be used to over-ride earlier failures, if the local
635 administrator determines that the faiures are not catastrophic.
636 .IP reject
637 Causes the request to be immediately rejected
638 .SH MODULE RETURN CODES
639 When a module is called, it returns one of the following codes to
640 "unlang", with the following meaning.
641
642 .DS
643         notfound        information was not found
644 .br
645         noop            the module did nothing
646 .br
647         ok              the module succeeded
648 .br
649         updated         the module updated the request
650 .br
651         fail            the module failed
652 .br
653         reject          the module rejected the request
654 .br
655         userlock        the user was locked out
656 .br
657         invalid         the configuration was invalid
658 .br
659         handled         the module has handled the request itself
660 .DE
661
662 These return codes can be tested for in a condition, as described
663 above in the CONDITIONS section.
664
665 See also the file doc/configurable_failover for additional methods of
666 trapping and modifying module return codes.
667 .SH FILES
668 /etc/raddb/radiusd.conf
669 .SH "SEE ALSO"
670 .BR radiusd.conf (5),
671 .BR dictionary (5)
672 .SH AUTHOR
673 Alan DeKok <aland@deployingradius.com>