Debian Codegraph @

doc/sources/admin/rules_examples.rst

raw
1 Rules examples
2 ==============
3
4 This page contains a few useful Perl expressions you can use in your
5 :ref:`Handler rules<rules>`, SAML/OIDC/CAS security
6 rules, 2FA Activation rules, etc.
7
8 Using session attributes
9 ------------------------
10
11 Session attributes are visible in the Manager's Session browser, any
12 attribute you see there can be used in a rule!
13
14 - Restricting access to a single user:
15
16 ::
17
18 $uid eq "dwho"
19 $uidNumber == 1000
20 $cn eq "Doctor Who"
21 $email eq "dwho\@badwolf.org"
22 etc.
23
24
25 .. tip::
26
27 In Perl, ``eq`` means *Equal* and must be used on strings.
28 ``==`` should be used only on numbers
29
30 .. danger::
31
32 In Perl, ``@`` character means an array and ``%`` a hash!
33 If you want to write a macro with these characters, you have to escape them like this:
34
35 ::
36
37 $my_email = "$uid\@my-domain.com"
38 $percent = "$rate\%more"
39
40 - Restricting access to specific groups
41
42 ::
43
44 # 2.0.8 and higher only
45 inGroup('administrators')
46
47 # older versions
48 $groups =~ /\b(?:admins|su)\b/ # admins OR su
49 $groups =~ /\badmin_[1-3a]\b/ # admin_1 OR admin_2 OR admin_3 OR admin_a
50
51 defined $hGroups->{'administrators'}
52
53
54 - Combining multiple expressions
55
56 ::
57
58 inGroup('timelords') and not $uid eq 'missy'
59
60 - Using Perl's regular expressions
61
62 ::
63
64 $cn =~ /^Doctor.*/i
65 $email !~ /\@spam.com$/
66
67 - Filtering on Authentication Level
68
69 ::
70
71 $authenticationLevel >= 3
72
73 - Filtering on IP subnet
74
75 ::
76
77 # since 2.17
78 inSubnet('192.168.0.0/16')
79
80 - Filtering on Authentication method
81
82 ::
83
84 $_auth ne 'Demo'
85
86 - Checking if the user has a an **available** second factor.
87
88 ::
89
90 # Since 2.0.10
91 has2f()
92 has2f('TOTP')
93 has2f('TOTP') or has2f('UBK')
94
95 # Before 2.0.10
96 $_2fDevices =~ /"type":\s*"TOTP"/s
97
98 .. tip::
99
100 In Perl, ``ne`` means *Not Equal* and must be used on
101 strings. ``\b`` means *word Boundary*. (?:) means *non capturing*
102 parenthesis.
103
104
105 Using environment variables
106 ---------------------------
107
108 - Comparing the IP address
109
110 ::
111
112 # Before 2.17
113 $env->{REMOTE_ADDR} =~ /^10\./
114
115 # Since 2.17
116 inSubnet('10.0.0.0/8')
117
118 - Comparing requested URI
119
120 ::
121
122 $env->{REQUEST_URI} =~ /test/
123