rlm_expr

Synopsis

The expr module implements math expressions in dynamically expanded strings. It also implements a number of other useful expansions.

Processing Sections

None.

Expansions

The expr module implements a number of expansions.

expr
Syntax

%{expr: …​}

Contents

The contents of the expansion are a series of mathematical expressions. The output of the expansion is the result of evaluating the mathematical expression.

Normal math operators are supported as shown in the table below.

Table 1. Mathematical operators
Operator Meaning

+

Addition

-

Subtraction

/

Division

*

Multiplication

%%

Remainder

<<

Left shift

>>

Right shift

&

Bitwise "and"

|

Bitwise "or"

^

Power

(

Open bracket, start sub-expression

)

Close bracket, end sub-expression

All calculations are done via unsigned 64-bit integers. Floating point is not supported. All integers are assumed to be base 10. Hexadecimal numbers can be used by preceding them with 0x, such as with 0xabcdef.

.Example

%{expr: 1 ` 2 ` (2^3) + 0xad}
escape
Syntax

%{escape: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the input string with the safe_characters escaped as =HH, where HH is the hex value of the character.

rand
Syntax

%{rand: …​}

Contents

The contents of the expansion are an integer that is the limit for the random number generation. The output of the expansion is a random number that is between 0 and that limit.

The limit must be greater than zero and less than 2^30 (about one billion).

randstr
Syntax

%{randstr: …​}

Contents

The contents of the expansion are a pattern describing the random string to generate. The output of the expansion is the random string.

The input pattern is described by the following table. Each modifier can take a numeric prefix, to create more than one character of output. e.g. %{randstr:5n} will create 5 numbers. The repetition is limited to 100. Any more than that is ignored.

Table 2. Patterns
Character Meaning

!

Punctuation. One of !"#$%&'()*+,-./:;<⇒?@[\\]^_`{|}~

.

Alphanumeric plus punctuation

a

Alphanumeric [a-zA-Z0-9]

c

Latin lowercase character [a-z]

C

Latin uppercase character [A-Z]

h

two hex digits [0-f][0-f]

n

Numbers [0-9]

s

Salt [a-zA-Z0-9./]

tolower
Syntax

%{tolower: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the input string, converted to lowercase.

toupper
Syntax

%{toupper: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the input string, converted to uppercase.

urlquote
Syntax

%{urlquote: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the input string with the safe_characters escaped as %HH, where HH is the hex value of the character.

md5
Syntax

%{md5: …​}

Contents

The contents of the expansion are either a string or a reference to an attribute. The output of the expansion is the MD5 hash of the input string, printed as 32 hexadecimal digits.

Example
%{md5:Hi, Mom}
%{md5:&User-Name}

The attribute reference syntax allows the expansion to take the MD5 hash of binary data. If attribute references were not allowed, then the MD5 expansion would be required to hash the printable form of the binary data, which is not the same as hashing the binary data.

sha1
Syntax

%{sha1: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the sha1 hash of the string. This expansion otherwise operates the same as the md5 one.

sha256
Syntax

%{sha256: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the sha256 hash of the string. This expansion otherwise operates the same as the md5 one.

sha512
Syntax

%{sha512: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the sha512 hash of the string. This expansion otherwise operates the same as the md5 one.

base64
Syntax

%{base64: …​}

Contents

The contents of the expansion are a string. The output of the expansion is the string converted to base64. This expansion supports attribute references, just like the md5 expansion.

base64tohex
Syntax

%{base64tohex: …​}

Contents

The contents of the expansion are a base64 string. The output of the expansion is the string converted to hex digits. The digits are not preceded by any 0x characters.

lpad
Syntax

%{lpad:&Attribute-Reference length pad}

Contents

The contents of the expansion are 3 fields, with the third field being optional. The first field is an attribute references, e.g. &User-Name. The second is an integer specifying the size of the fixed-width field to produce, e.g. 7. The third field is a padding character, e.g. 0. If the the third field is omitted, then a space is used as the fill character.

Example
%{lpad:&User-Name 7}
%{lpad:&User-Name 7 x}
%{lpad:&reply:Filter-Id 7}

The output of the expansion is a fixed-width string, composed of "length" characters, with the attribute printed to a string. If "length" is smaller than the length of the attribute string, the output is truncated to "length" characters. If "length" is equal to the length of the attribute string, the output is exactly the attribute string. If "length" is larger than the length of the attribute string, the output is filled to the left with the "pad" character.

Table 3. Example for User-Name = "test"
Input Output

%{lpad:&User-Name 7}

"   test"

%{lpad:&User-Name 2}

"te"

%{lpad:&User-Name 6 0}

"00test"

rpad
Syntax

%{rpad:&Attribute-Reference length pad}

Contents

The contents of the expansion are 3 fields, with the third field being optional. The first field is an attribute references, e.g. &User-Name. The second is an integer specifying the size of the fixed-width field to produce, e.g. 7. The third field is a padding character, e.g. 0. If the the third field is omitted, then a space is used as the fill character.

Example
%{rpad:&User-Name 7}
%{rpad:&User-Name 7 x}
%{rpad:&reply:Filter-Id 7}

The output of the expansion is a fixed-width string, composed of "length" characters, with the attribute printed to a string. If "length" is smaller than the length of the attribute string, the output is truncated to "length" characters. If "length" is equal to the length of the attribute string, the output is exactly the attribute string. If "length" is larger than the length of the attribute string, the output is filled to the right with the "pad" character.

Table 4. Example for User-Name = "test"
Input Output

%{rpad:&User-Name 7}

"test   "

%{rpad:&User-Name 2}

"te"

%{rpad:&User-Name 6 0}

"test00"

Directives

safe-characters
Syntax

safe-characters = string

Default

@abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-: /_

Description

Characters that will not be encoded by the %\{encode:…​} expansion.