Debian Codegraph @

doc/antora/modules/unlang/pages/xlat/builtin.adoc

raw
1 = Built-In Expansions
2
3 In addition to storing attribute references, the server has a number
4 of built-in expansions. These expansions act largely as functions
5 which operate on inputs, and produce an output.
6
7
8
9 == Attribute Manipulation
10
11 === %{length: ... }
12
13 The `length` expansion returns the size of the input as an integer.
14 When the input is a string, then the output is identical to the
15 `strlen` expansion.
16
17 When the input is an attribute reference, the output is the size of
18 the attributes data as encoded "on the wire".
19
20 .Return: _size_
21
22 .Determining the length of fixed and variable length attributes
23 ====
24 [source,unlang]
25 ----
26 update control {
27 &Tmp-String-0 := "Caipirinha"
28 &Framed-IP-Address := 192.0.2.1
29 }
30
31 update reply {
32 &Reply-Message := "The length of %{control:Tmp-String-0} is %{length:&control:Tmp-String-0}"
33 &Reply-Message += "The length of %{control:Framed-IP-Address} is %{length:&control:Framed-IP-Address}"
34 }
35 ----
36
37 .Output
38 ....
39 The length of Caipirinha is 10
40 The length of 192.168.0.2 is 4
41 ....
42 ====
43
44 `length` is built in to the server core.
45
46
47
48 === %{integer:<&ref>}
49
50 Print the value of the attribute an integer.
51
52 In normal operation, `integer` attributes are printed using the name
53 given by a `VALUE` statement in a dictionary. Similarly, date
54 attributes are printed as dates, i.e., "January 1 2010.
55
56 The `integer` expansion applies only to attributes which can be
57 converted to an integer. For all other inputs, it returns `0`.
58
59 A common usage is to find the difference between two dates.
60
61 For example, if a request contains `Service-Type = Login-User`, the
62 expansion of `%{integer:&Service-Type}` will yield `1`, which is the
63 value associated with the `Login-User` name. Using
64 `%{integer:&Event-Timestamp}` will return the event timestamp as an
65 unsigned 32-bit number.
66
67 .Return: _string_
68
69 .Determining the integer value of an enumerated attribute
70 ====
71 [source,unlang]
72 ----
73 update {
74 &control:Service-Type := Login-User
75 }
76 update reply {
77 &Reply-Message := "The value of Service-Type is %{integer:&control:Service-Type}"
78 }
79 ----
80
81 .Output
82
83 ```
84 The value of Service-Type is 1
85 ```
86 ====
87
88 `integer` is built in to the server core.
89
90
91
92 === %{rand:<number>}
93
94 Generate random number from `0` to `<number>-1`.
95
96 .Return: _uint64_
97
98 .Generating a random number between 0 and 511
99 ====
100 [source,unlang]
101 ----
102 update reply {
103 &Reply-Message := "The random number is %{rand:512}"
104 }
105 ----
106
107 .Output
108
109 ```
110 The random number is 347
111 ```
112 ====
113
114 `rand` is provided by the `rlm_expr` module.
115
116
117
118 === %{tag:<attribute ref>}
119
120 CAUTION: This expansion is deprecated and will likely be removed.
121
122 Returns a list of tags for any attributes found using ``<attribute ref>``.
123
124 .Return: _int8_
125
126 .Determining the tag value of the second instance of the `radius.Tunnel-Server-Endpoint` attribute
127 ====
128 [source,unlang]
129 ----
130 update request {
131 &Tunnel-Server-Endpoint := '192.0.1.1'
132 &Tunnel-Server-Endpoint:1 := '192.0.5.2'
133 &Tunnel-Server-Endpoint:1 += '192.0.3.8'
134 &Tunnel-Server-Endpoint:2 := '192.0.2.1'
135 &Tunnel-Server-Endpoint:2 += '192.0.3.4'
136 }
137
138 update reply {
139 &Reply-Message := "The tag value of the second instance of Tunnel-Server-Enpoint is %{request:Tunnel-Server-Endpoint[1]}"
140 }
141 ----
142
143 .Output
144
145 ```
146 The tag value of the second instance of Tunnel-Server-Enpoint is 192.0.5.2
147 ```
148 ====
149
150 `tag` is built in to the server core.
151
152
153
154 === %{string:<data>}
155
156 Convert input to a string (if possible). For _octets_ type attributes, this
157 means interpreting the data as a UTF8 string, and inserting octal escape
158 sequences where appropriate.
159
160 For other types, this means printing the value in its _presentation_ format,
161 i.e. dotted quads for IPv4 addresses, link:https://en.wikipedia.org/wiki/ISO_8601[ISO 8601]
162 time for date types, enumeration values for attributes such as `radius.Service-Type` etc.
163
164 .Return: _string_
165
166 .String interpolation using the raw octets value of Tmp-Octets-0, and the stringified version
167 ====
168 [source,unlang]
169 ----
170 update control {
171 &Tmp-Octets-0 := 0x7465737431
172 }
173 update reply {
174 &Reply-Message := "The string value of %{control:Tmp-Octets-0} is %{string:%{control:Tmp-Octets-0}}"
175 }
176 ----
177 ====
178
179 .Output
180
181 ```
182 The string value of 0x7465737431 is test1
183 ```
184
185 `string` is built in to the server core.
186
187
188
189 == Server Manipulation
190
191 === %{config:<key>}
192
193 Refers to a variable in the configuration file. See the documentation
194 on configuration file references.
195
196 .Return: _string_
197
198 .Example
199
200 [source,unlang]
201 ----
202 "Server installed in %{config:prefix}"
203 "Module rlm_exec.shell_escape = %{config:modules.exec.shell_escape}"
204 ----
205
206 .Output
207
208 ```
209 Server installed in /opt/freeradius
210 Module rlm_exec.shell_escape = yes
211 ```
212
213 `config` is built in to the server core.
214
215
216
217 === %{client:<key>}
218
219 Refers to a variable that was defined in the client section for the
220 current client. See the sections `client { ... }` in `clients.conf`.
221
222 .Return: _string_
223
224 .Example
225
226 [source,unlang]
227 ----
228 "The client ipaddr is %{client:ipaddr}"
229 ----
230
231 .Output
232
233 ```
234 The client ipaddr is 192.168.5.9
235 ```
236
237 `client` is built in to the server core.
238
239
240
241 === %{debug:<level>}
242
243 Dynamically change the debug level to something high, recording the old level.
244
245 .Return: _string_
246
247 .Example
248
249 [source,unlang]
250 ----
251 authorize {
252 if (&request:User-Name == "bob") {
253 "%{debug:4}"
254 } else {
255 "%{debug:0}"
256 }
257 ...
258 }
259 ----
260
261 .Output (_extra informations only for that condition_)
262
263 ```
264 ...
265 (0) authorize {
266 (0) if (&request:User-Name == "bob") {
267 (0) EXPAND %{debug:4}
268 (0) --> 2
269 (0) } # if (&request:User-Name == "bob") (...)
270 (0) filter_username {
271 (0) if (&State) {
272 (0) ...
273 (0) }
274 ...
275 ```
276
277 `debug` is built in to the server core.
278
279
280
281 === %{debug_attr:<list:[index]>}
282
283 Print to debug output all instances of current attribute, or all attributes in a list.
284 expands to a zero-length string.
285
286 .Return: _string_
287
288 .Example
289
290 [source,unlang]
291 ----
292 authorize {
293 if (&request:User-Name == "bob") {
294 "%{debug_attr:request[*]}"
295 }
296 ...
297 }
298 ----
299
300 .Output
301
302 ```
303 ...
304 (0) authorize {
305 (0) if (&request:User-Name == "bob") {
306 (0) Attributes matching "request[*]"
307 (0) &request:User-Name = bob
308 (0) &request:User-Password = hello
309 (0) &request:NAS-IP-Address = 127.0.1.1
310 (0) &request:NAS-Port = 1
311 (0) &request:Message-Authenticator = 0x9210ee447a9f4c522f5300eb8fc15e14
312 (0) EXPAND %{debug_attr:request[*]}
313 (0) } # if (&request:User-Name == "bob") (...)
314 ...
315 ```
316
317 `debug_attr` is built in to the server core.
318
319
320
321 == String manipulation
322
323 === %{lpad:<&ref> <val> <char>}
324
325 Left-pad a string.
326
327 .Return: _string_
328
329 .Example
330
331 [source,unlang]
332 ----
333 update control {
334 &Tmp-String-0 := "123"
335 }
336 update reply {
337 &Reply-Message := "Maximum should be %{lpad:&control:Tmp-String-0 11 0}"
338 }
339 ----
340
341 .Output
342
343 ```
344 Maximum should be 00000000123
345 ```
346
347 `lpad` is provided by the `rlm_expr` module.
348
349
350
351 === %{rpad:<&ref> <val> <char>}
352
353 Right-pad a string.
354
355 .Return: _string_
356
357 .Example
358
359 [source,unlang]
360 ----
361 update control {
362 &Tmp-String-0 := "123"
363 }
364 update reply {
365 &Reply-Message := "Maximum should be %{rpad:&control:Tmp-String-0 11 0}"
366 }
367 ----
368
369 .Output
370
371 ```
372 Maximum should be 12300000000
373 ```
374
375 `rpad` is provided by the `rlm_expr` module.
376
377
378
379 === %{pairs:<&list:[*]>}
380
381 Serialize attributes as comma-delimited string.
382
383 .Return: _string_
384
385 .Example
386
387 [source,unlang]
388 ----
389 update {
390 &control:Tmp-String-0 := "This is a string"
391 &control:Tmp-String-0 += "This is another one"
392 }
393
394 update reply {
395 &Reply-Message := "Serialize output: %{pairs:&control[*]}"
396 }
397 ----
398
399 .Output
400
401 ```
402 Serialize output: Tmp-String-0 = \"This is a string\"Tmp-String-0 = \"This is another one\"
403 ```
404
405 `pairs` is provided by the `rlm_expr` module.
406
407
408
409 === %{randstr: ...}
410
411 Get random string built from character classes.
412
413 .Return: _string_
414
415 .Example
416
417 [source,unlang]
418 ----
419 update reply {
420 &Reply-Message := "The random string output is %{randstr:aaaaaaaa}"
421 }
422 ----
423
424 .Output
425
426 ```
427 The random string output is 4Uq0gPyG
428 ```
429
430 `randstr` is provided by the `rlm_expr` module.
431
432
433
434 === %{strlen: ... }
435
436 Length of given string.
437
438 .Return: _integer_
439
440 .Example
441
442 [source,unlang]
443 ----
444 update control {
445 &Tmp-String-0 := "Caipirinha"
446 }
447 update reply {
448 &Reply-Message := "The length of %{control:Tmp-String-0} is %{strlen:&control:Tmp-String-0}"
449 }
450 ----
451
452 .Output
453
454 ```
455 The length of Caipirinha is 21
456 ```
457
458 `strlen` is built in to the server core.
459
460
461
462 === %{tolower: ... }
463
464 Dynamically expands the string and returns the lowercase version of
465 it. This definition is only available in version 2.1.10 and later.
466
467 .Return: _string_
468
469 .Example
470
471 [source,unlang]
472 ----
473 update control {
474 &Tmp-String-0 := "CAIPIRINHA"
475 }
476 update reply {
477 &Reply-Message := "tolower of %{control:Tmp-String-0} is %{tolower:%{control:Tmp-String-0}}"
478 }
479 ----
480
481 .Output
482
483 ```
484 tolower of CAIPIRINHA is caipirinha
485 ```
486
487 `tolower` is provided by the `rlm_expr` module.
488
489
490
491 === %{toupper: ... }
492
493 Dynamically expands the string and returns the uppercase version of
494 it. This definition is only available in version 2.1.10 and later.
495
496 .Return: _string_
497
498 .Example
499
500 [source,unlang]
501 ----
502 update control {
503 &Tmp-String-0 := "caipirinha"
504 }
505 update reply {
506 &Reply-Message := "toupper of %{control:Tmp-String-0} is %{toupper:%{control:Tmp-String-0}}"
507 }
508 ----
509
510 .Output
511
512 ```
513 toupper of caipirinha is CAIPIRINHA
514 ```
515
516 `toupper` is provided by the `rlm_expr` module.
517
518
519
520 == String Conversion
521
522 === %{base64: ... }
523
524 Encode a string using Base64.
525
526 .Return: _string_
527
528 .Example
529
530 [source,unlang]
531 ----
532 update control {
533 &Tmp-String-0 := "Caipirinha"
534 }
535 update reply {
536 &Reply-Message := "The base64 of %{control:Tmp-String-0} is %{base64:%{control:Tmp-String-0}}"
537 }
538 ----
539
540 .Output
541
542 ```
543 The base64 of foo is Q2FpcGlyaW5oYQ==
544 ```
545
546 `base64` is provided by the `rlm_expr` module.
547
548
549
550 === %{base64tohex: ... }
551
552 Decode a base64 string (e.g. previously encoded using `base64`) to
553 hex.
554
555 .Return: _string_
556
557 .Example
558
559 [source,unlang]
560 ----
561 update control {
562 &Tmp-String-0 := "Q2FpcGlyaW5oYQ=="
563 }
564 update reply {
565 &Reply-Message := "The base64tohex of %{control:Tmp-String-0} is %{base64tohex:%{control:Tmp-String-0}}"
566 }
567 ----
568
569 .Output
570
571 ```
572 The base64decode of Q2FpcGlyaW5oYQ== is 436169706972696e6861
573 ```
574
575 `base64tohex` is provided by the `rlm_expr` module.
576
577
578
579 === %{hex: ... }
580
581 Convert to hex.
582
583 .Return: _string_
584
585 .Example
586
587 [source,unlang]
588 ----
589 update control {
590 &Tmp-String-0 := "12345"
591 }
592 update reply {
593 &Reply-Message := "The value of %{control:Tmp-String-0} in hex is %{hex:%{control:Tmp-String-0}}"
594 }
595 ----
596
597 .Output
598
599 ```
600 The value of 12345 in hex is 3132333435
601 ```
602
603 `hex` is built in to the server core.
604
605
606
607 === %{urlquote: ... }
608
609 Quote URL special characters.
610
611 .Return: _string_.
612
613 .Example
614
615 [source,unlang]
616 ----
617 update {
618 &control:Tmp-String-0 := "http://example.org/"
619 }
620 update reply {
621 &Reply-Message += "The urlquote of %{control:Tmp-String-0} is %{urlquote:%{control:Tmp-String-0}}"
622 }
623 ----
624
625 .Output
626
627 ```
628 The urlquote of http://example.org/ is http%3A%2F%2Fexample.org%2F
629 ```
630
631 `urlquote` is provided by the `rlm_expr` module.
632
633
634
635 === %{urlunquote: ... }
636
637 Unquote URL special characters.
638
639 .Return: _string_.
640
641 .Example
642
643 [source,unlang]
644 ----
645 update {
646 &control:Tmp-String-0 := "http%%3A%%2F%%2Fexample.org%%2F" # Attention for the double %.
647 }
648 update reply {
649 &Reply-Message += "The urlunquote of %{control:Tmp-String-0} is %{urlunquote:%{control:Tmp-String-0}}"
650 }
651 ----
652
653 .Output
654
655 ```
656 The urlunquote of http%3A%2F%2Fexample.org%2F is http://example.org/
657 ```
658
659 `urlunquote` is provided by the `rlm_expr` module.
660
661
662
663 == Hashing and Encryption
664
665 === %{hmacmd5:<shared_key> <string>}
666
667 Generate `HMAC-MD5` of string.
668
669 .Return: _octal_
670
671 .Example
672
673 [source,unlang]
674 ----
675 update {
676 &control:Tmp-String-0 := "mykey"
677 &control:Tmp-String-1 := "Caipirinha"
678 }
679 update {
680 &control:Tmp-Octets-0 := "%{hmacmd5:%{control:Tmp-String-0} %{control:Tmp-String-1}}"
681 }
682
683 update reply {
684 &Reply-Message := "The HMAC-MD5 of %{control:Tmp-String-1} in octets is %{control:Tmp-Octets-0}"
685 &Reply-Message += "The HMAC-MD5 of %{control:Tmp-String-1} in hex is %{hex:control:Tmp-Octets-0}"
686 }
687 ----
688
689 .Output
690
691 ```
692 The HMAC-MD5 of Caipirinha in octets is \317}\264@K\216\371\035\304\367\202,c\376\341\203
693 The HMAC-MD5 of Caipirinha in hex is 636f6e74726f6c3a546d702d4f63746574732d30
694 ```
695
696 `hmacmd5` is provided by the `rlm_expr` module.
697
698
699
700 === %{hmacsha1:<shared_key> <string>}
701
702 Generate `HMAC-SHA1` of string.
703
704 .Return: _octal_
705
706 .Example
707
708 [source,unlang]
709 ----
710 update {
711 &control:Tmp-String-0 := "mykey"
712 &control:Tmp-String-1 := "Caipirinha"
713 }
714 update {
715 &control:Tmp-Octets-0 := "%{hmacsha1:%{control:Tmp-String-0} %{control:Tmp-String-1}}"
716 }
717
718 update reply {
719 &Reply-Message := "The HMAC-SHA1 of %{control:Tmp-String-1} in octets is %{control:Tmp-Octets-0}"
720 &Reply-Message += "The HMAC-SHA1 of %{control:Tmp-String-1} in hex is %{hex:control:Tmp-Octets-0}"
721 }
722 ----
723
724 .Output
725
726 ```
727 The HMAC-SHA1 of Caipirinha in octets is \311\007\212\234j\355\207\035\225\256\372ʙ>R\"\341\351O)
728 The HMAC-SHA1 of Caipirinha in hex is 636f6e74726f6c3a546d702d4f63746574732d30
729 ```
730
731 `hmacsha1` is provided by the `rlm_expr` module.
732
733
734
735 === %{md5: ... }
736
737 Dynamically expands the string and performs an MD5 hash on it. The
738 result is binary data.
739
740 .Return: _binary data_
741
742 .Example
743
744 [source,unlang]
745 ----
746 update control {
747 &Tmp-String-0 := "Caipirinha"
748 }
749 update reply {
750 &Reply-Message := "md5 of %{control:Tmp-String-0} is octal=%{md5:%{control:Tmp-String-0}}"
751 &Reply-Message := "md5 of %{control:Tmp-String-0} is hex=%{hex:%{md5:%{control:Tmp-String-0}}}"
752 }
753 ----
754
755 .Output
756
757 ```
758 md5 of Caipirinha is octal=\024\204\013md||\230\243\3472\3703\330n\251
759 md5 of Caipirinha is hex=14840b6d647c7c98a3e732f833d86ea9
760 ```
761
762 `md5` is provided by the `rlm_expr` module.
763
764
765
766 == Miscellaneous Expansions
767
768 === +%{0}+..+%{32}+
769
770 `%{0}` expands to the portion of the subject that matched the last regular
771 expression evaluated. `%{1}`..`%{32}` expand to the contents of any capture
772 groups in the pattern.
773
774 Every time a regular expression is evaluated, whether it matches or not,
775 the numbered capture group values will be cleared.
776
777
778
779 === +%{regex:<named capture group>}+
780
781 Return named subcapture value from the last regular expression evaluated.
782
783 Results of named capture groups are available using the `%{regex:<named capture
784 group>}` expansion. They will also be accessible using the numbered expansions
785 described xref:#_0_32[above].
786
787 Every time a regular expression is evaluated, whether it matches or not,
788 the named capture group values will be cleared.
789
790 [NOTE]
791 ====
792 This expansion is only available if the server is built with libpcre or libpcre2.
793 Use the output of `radiusd -Xxv` to determine which regular expression library in use.
794
795 ....
796 ...
797 Debug : regex-pcre : no
798 Debug : regex-pcre2 : yes
799 Debug : regex-posix : no
800 Debug : regex-posix-extended : no
801 Debug : regex-binsafe : yes
802 ...
803 Debug : pcre2 : 10.33 (2019-04-16) - retrieved at build time
804 ....
805 ====
806
807 `regex` is built in to the server core.
808
809
810
811 === +%{nexttime:<time>}+
812
813 Calculate number of seconds until next n hour(`s`), day(`s`), week(`s`), year(`s`).
814
815 .Return: _string_
816
817 .Example:
818
819 With the current time at 16:18, `%{nexttime:1h}` will expand to `2520`.
820
821 [source,unlang]
822 ----
823 update reply {
824 &Reply-Message := "You should wait for %{nexttime:1h}s"
825 }
826 ----
827
828 .Output
829
830 ```
831 You should wait for 2520s
832 ```
833
834 `nexttime` is provided by the `rlm_expr` module.
835
836
837
838 === +%{Packet-Type}+
839
840 The packet type (`Access-Request`, etc.)
841
842
843
844 === +%{Packet-Src-IP-Address} and %{Packet-Src-IPv6-Address}+
845
846 The source IPv4 or IPv6 address of the packet. See also the expansions
847 `%{client:ipaddr}` and `%{client:ipv6addr}`. The two expansions
848 should be identical, unless `%{client:ipaddr}` contains a DNS hostname.
849
850
851
852 === +%{Packet-Dst-IP-Address} and %{Packet-Dst-IPv6-Address}+
853
854 The destination IPv4 or IPv6 address of the packet. See also the
855 expansions `%{listen:ipaddr}` and `%{listen:ipv6addr}`. If the socket
856 is listening on a "wildcard" address, then these two expansions will be
857 different, as follows: the `%{listen:ipaddr}` will be the wildcard
858 address and `%{Packet-DST-IP-Address}` will be the unicast address to
859 which the packet was sent.
860
861
862
863 === +%{Packet-Src-Port} and %{Packet-Dst-Port}+
864
865 The source/destination ports associated with the packet.
866
867 .Return: _string_.
868
869 .Example
870
871 [source,unlang]
872 ----
873 update control {
874 &Tmp-String-0 := "[email protected]"
875 }
876
877 if (&control:Tmp-String-0 =~ /^(?<login>(.*))@(?<domain>(.*))$/) {
878 update reply {
879 &Reply-Message := "The %{control:Tmp-String-0} { login=%{regex:login}, domain=%{regex:domain} }"
880 }
881 }
882 ----
883
884 .Output
885
886 ```
887 The [email protected] { login=user, domain=example.com }
888 ```
889
890 // Copyright (C) 2020 Network RADIUS SAS. Licenced under CC-by-NC 4.0.
891 // Development of this documentation was sponsored by Network RADIUS SAS.