Debian Codegraph @

doc/sources/admin/extendedfunctions.rst

raw
1 Extended functions
2 ==================
3
4 Presentation
5 ------------
6
7 When :doc:`writing rules and headers<writingrulesand_headers>`, you can
8 use Perl expressions that will be evaluated in a jail, to prevent bad
9 code execution.
10
11 This is also true for:
12
13 - :ref:`Menu modules activation rules<portalmenu-menu-modules>`
14 - :doc:`Form replay data<formreplay>`
15 - Macros
16 - Issuer databases use rules
17 - etc.
18
19 Inside this jail, you can access to:
20
21 * All session values and CGI environment variables (through `$ENV{<HTTP_NAME>}`)
22 * Core Perl subroutines (split, pop, map, etc.)
23 * :doc:`Custom functions<customfunctions>`
24 * The `encode_base64 <http://perldoc.perl.org/MIME/Base64.html>`__ subroutine
25 * Information about current request
26 * Extended functions except basic, iso2unicode and unicode2iso:
27
28 * basic_
29 * checkDate_
30 * checkLogonHours_
31 * date_
32 * dateToTime_ (|new| in version 2.0.12)
33 * decrypt_ (|new| in version 2.18)
34 * encrypt_
35 * groupMatch_
36 * has2f_ (|new| in version 2.0.10)
37 * inDomain_ (|new| in version 2.19.0)
38 * inGroup_ (|new| in version 2.0.8)
39 * inSubnet_ (|new| in version 2.17)
40 * ipInSubnet_ (|new| in version 2.17)
41 * isInNet6_
42 * iso2unicode_
43 * iso2unicodeSafe_ (|new| in version 2.0.15)
44 * listMatch_ (|new| in version 2.0.7)
45 * subjectid_ (|new| in version 2.17)
46 * token_
47 * unicode2iso_
48 * unicode2isoSafe_ (|new| in version 2.0.15)
49 * varIsInUri_ (|new| in version 2.0.7)
50
51
52 .. |new| image:: /documentation/new.png
53 :width: 35px
54
55 .. tip::
56
57 To know more about the jail, check `Safe module
58 documentation <http://perldoc.perl.org/Safe.html>`__.
59
60 Extended Functions List
61 -----------------------
62
63 basic
64 ~~~~~
65
66 .. attention::
67
68 This function is not compliant with the :doc:`Safe jail<safejail>`,
69 you will have to disable the jail to use it.
70
71 This function builds the ``Authorization`` HTTP header employed in
72 :doc:`HTTP Basic authentication scheme<applications/authbasic>`. It will
73 convert `user` and `password` parameters from UTF-8 to ISO-8859-1.
74
75 Functions parameters:
76
77 - **user**
78 - **password**
79
80 Simple usage example:
81
82 ::
83
84 basic($uid,$_password)
85
86 checkDate
87 ~~~~~~~~~
88
89 This function checks date of current request, and compare it to
90 a start date and an end date. It returns 1 if this matches, 0 else.
91
92
93 The date format corresponds to LDAP date syntax, for example for the 1st of March
94 2009 (GMT)
95
96 ::
97
98 20090301000000Z
99
100 |new| Since version 2.0.12, the date may end with a differential timezone,
101 for example for the 1st of March 2009 (+0100):
102
103 ::
104
105 20090301000000+0100
106
107
108 Functions parameters:
109
110 - **start**: Start date (GMT unless, |new| since version 2.0.12, a
111 differential timezone is included)
112 - **end**: End date (GMT unless, |new| since version 2.0.12, a
113 differential timezone is included)
114 - **default_access** (optional): Which result to return if **start** and
115 **end** dates are empty
116
117 Simple usage example:
118
119 ::
120
121 checkDate($ssoStartDate, $ssoEndDate)
122
123 checkLogonHours
124 ~~~~~~~~~~~~~~~
125
126 This function checks the day and the hour of current request, and
127 compare it to allowed days and hours. It returns 1 if matches, 0
128 else. By default, the allowed days and hours is an hexadecimal
129 value, representing each hour of the week. A day has 24 hours, and a
130 week 7 days, so the value contains 168 bits, converted into 42
131 hexadecimal characters. Sunday is the first day.
132
133 For example, for a full access, excepted week-end:
134
135 ::
136
137 000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000
138
139
140 .. tip::
141
142 You can use the binary value from the logonHours attribute of Active
143 Directory, or create a custom attribute in your LDAP schema.
144
145 Function parameters:
146
147 - **logon_hours**: String representing allowed logon hours (GMT)
148 - **syntax** (optional): ``hexadecimal`` (default) or ``octetstring``
149 - **time_correction** (optional): Hours to add or to subtract
150 - **default_access** (optional): Which result to return if
151 **logon_hours** is empty
152
153 Simple usage example:
154
155 ::
156
157 checkLogonHours($ssoLogonHours)
158
159 If you use the binary value (Active Directory), use this:
160
161 ::
162
163 checkLogonHours($ssoLogonHours, 'octetstring')
164
165 You can also configure jetlag (if all of your users use the same
166 timezone):
167
168 ::
169
170 checkLogonHours($ssoLogonHours, '', '+2')
171
172 If you manage different timezones, you have to take the jetlag into
173 account in ssoLogonHours values, or use the $_timezone parameter. This
174 parameter is set by the portal and use javascript to get the connected
175 user timezone. It should works on every browser:
176
177 ::
178
179 checkLogonHours($ssoLogonHours, '', $_timezone)
180
181 You can modify the default behavior for people without value in
182 ssoLogonHours. Indeed, by default, users without logon hours values are
183 rejected. You can allow these users instead of reject them:
184
185 ::
186
187 checkLogonHours($ssoLogonHours, '', '', '1')
188
189 date
190 ~~~~
191
192 Returns the date, in format YYYYMMDDHHMMSS, local time by default, GMT
193 by calling ``date(1)``
194
195 ::
196
197 For example: date(1) lt '19551018080000'
198
199 dateToTime
200 ~~~~~~~~~~
201
202 .. versionadded:: 2.0.12
203
204 Used for converting a string date into epoch time.
205
206 The date format is the LDAP date syntax, for example for the 1st March
207 2009 (GMT):
208
209 ::
210
211 20090301000000Z
212
213 The date may end with a differential timezone that is interpreted to
214 adjust the epoch time, for example for the 1st March 2009 (+0100):
215
216 ::
217
218 20090301000000+0100
219
220 Simple usage example:
221
222 ::
223
224 dateToTime($ssoStartDate) lt dateToTime(date(1))
225
226 decrypt
227 ~~~~~~~
228
229 This function uses the secret key of LL::NG configuration to decrypt a value.
230
231 ::
232
233 decrypt($_password)
234
235 encrypt
236 ~~~~~~~
237
238 .. tip::
239
240 Since version 2.0, this function is now compliant with
241 :doc:`Safe jail<safejail>`.
242
243 This function uses the secret key of LL::NG configuration to crypt a data.
244 This can be used for anonymizing identifier given to the protected
245 application.
246
247 ::
248
249 encrypt($_whatToTrace)
250
251 groupMatch
252 ~~~~~~~~~~
253
254 This function allows one to parse the ``$hGroups`` variable to check if
255 a value is present inside a group attribute.
256
257 Function parameter:
258
259 - **groups**: ``$hGroups`` variable
260 - **attribute**: Name of group attribute
261 - **value**: Value to check
262
263 Simple usage example:
264
265 ::
266
267 groupMatch($hGroups, 'description', 'Service 1')
268
269
270 .. _has2f:
271
272 has2f
273 ~~~~~
274
275 .. versionadded:: 2.0.10
276
277 This function tests if the current user has registered a second factor. The following types are supported out of the box:
278
279 * :doc:`TOTP<totp2f>`
280 * :doc:`UBK<yubikey2f>`
281 * :doc:`WebAuthn<webauthn2f>`
282
283 Example::
284
285 has2f()
286 has2f('UBK')
287 has2f('UBK') or has2f('TOTP')
288
289
290 .. warning::
291
292 Do **NOT** use this test to check if the user has **used** their second factor for logging in!
293 This test only checks if the user has registered a second factor. Regardless of their **current**
294 authentication level. It can be used to simplify second factor activation rules.
295
296
297 .. note::
298
299 Before version 2.0.10, you need to use the following syntax ::
300
301 $_2fDevices =~ /"type":\s*"TOTP"/s
302
303 inDomain
304 ~~~~~~~~
305
306 .. versionadded:: 2.19.0
307
308 This function tests if the request's hostname (HTTP ``Host`` header) belongs to a given DNS domain
309
310 Usage example:
311
312 ::
313
314 inDomain('example.com')
315
316 The function returns 1 if the targeted Vhost is equal to the given domain or contains it as a suffix.
317
318 inGroup
319 ~~~~~~~
320
321 .. versionadded:: 2.0.8
322
323 This function lets you test if the user is in a given group. It is
324 case-insensitive.
325
326 Usage examples:
327
328 ::
329
330 inGroup('admins')
331
332 inGroup('test users')
333
334 inGroup('test users', 'admins')
335
336 The function returns 1 if the user belongs to the given group, and 0 if they don't.
337
338 inSubnet
339 ~~~~~~~~
340
341 .. versionadded:: 2.17
342
343 This function lets you test if the request's IP address (``REMOTE_ADDR``
344 environment variable) belongs to a certain subnet. You can give multiple subnets.
345
346 Usage examples:
347
348 ::
349
350 inSubnet('127.0.0.0/8')
351
352 inSubnet('10.0.0.0/8', '192.168.0.0/16')
353
354 The function returns 1 if the user's IP is in provided subnets, and 0 if it is not.
355
356 isInNet6
357 ~~~~~~~~
358
359 Function to check if an IPv6 address is in a subnet. Example *check if
360 IP address is local*:
361
362 .. code-block:: perl
363
364 isInNet6($ipAddr, 'fe80::/10')
365
366 ipInSubnet
367 ~~~~~~~~~~
368
369 .. versionadded:: 2.17
370
371 This function lets you test if the provided IP address belongs to a certain
372 subnet. You can give multiple subnets.
373
374 Usage examples:
375
376 ::
377
378 ipInSubnet($env->{REMOTE_ADDR}, '127.0.0.0/8')
379
380 ipInSubnet($env->{REMOTE_ADDR}, '10.0.0.0/8', '192.168.0.0/16')
381
382 The function returns 1 if provided IP is in one of the subnets, and 0 if
383 it is not.
384
385 iso2unicode
386 ~~~~~~~~~~~
387
388 .. attention::
389
390 This function is not compliant with :doc:`Safe jail<safejail>`.
391 You will have to disable the jail to use it.
392
393 This function converts a string from ISO-8859-1 to UTF-8.
394
395 Function parameter:
396
397 - **string**
398
399 Simple usage example:
400
401 ::
402
403 iso2unicode($name)
404
405 iso2unicodeSafe
406 ~~~~~~~~~~~~~~~
407
408 This function converts a string from ISO-8859-1 to UTF-8
409 but it is not as portable as the original one.
410
411 Functions parameters:
412
413 - **string**
414
415 Simple usage example:
416
417 ::
418
419 iso2unicodeSafe($name)
420
421 .. _listMatch:
422
423 listMatch
424 ~~~~~~~~~
425
426 .. versionadded:: 2.0.7
427
428 This function lets you test if a particular value can be found with a
429 multi-valued session attribute.
430
431 Function parameter:
432
433 - **list**: Variable containing several values (plain string with separator, array or hash)
434 - **values**: Values to search in the list
435 - **ignorecase**: Ignore case, by default the search is case-sensitive
436
437 Simple usage example:
438
439 ::
440
441 # Case sensitive match
442 listMatch($roles, 'role-app1')
443 listMatch($roles, 'role-app1', 'Role-App2')
444
445 # Case insensitive match
446 listMatch($roles, 'RoLe-aPp1', 1)
447 listMatch($roles, 'RoLe-aPp1', 'Role-App2', 1)
448
449 The function returns 1 if a value was found, and 0 if it was not found.
450
451 .. _subjectid:
452
453 subjectid
454 ~~~~~~~~~
455
456 .. versionadded:: 2.17
457
458 This function lets you generate an identifier suitable with the requirements of `urn:oasis:names:tc:SAML:profiles:subject-id <https://docs.oasis-open.org/security/saml-subject-id-attr/v1.0/cs01/saml-subject-id-attr-v1.0-cs01.html>`__
459
460 It takes an optional salt that makes it suitable for pairwise IDs as well.
461
462 .. warning::
463
464 The generated ID will only be as stable as the underlying attribute
465
466 Function parameter:
467
468 - **value**: Value to hash
469 - **scope**: DNS suffix to append
470 - **salt**: optional salt to be added at the end of the value before hashing
471
472 Simple usage example:
473
474 ::
475
476 # Generate a subject-id from uid, in a global macro
477 subjectid($uid, "scope.edu", "myglobalsalt")
478
479 # Generate a pairwise-id, in a per-service macro
480 subjectid($uid, "scope.edu", "myservice")
481
482
483 token
484 ~~~~~
485
486 This function generates token used for
487 :doc:`handling server webservice calls<servertoserver>`.
488
489 .. code-block:: perl
490
491 token($_session_id, 'app1.example.com', 'apptest.example.com', '/^webapp[23]\.example\.com/')
492
493 .. attention::
494
495 The colon character is not allowed in regexp.
496
497
498
499 varIsInUri
500 ~~~~~~~~~~
501
502 .. versionadded:: 2.0.7
503
504 Function to check if a variable is in requested URI
505
506 Example *check if $uid is in /check-auth/ URI*:
507
508 .. code-block:: perl
509
510 varIsInUri($ENV{REQUEST_URI}, '/check-auth/', $uid)
511
512 https://test1.example.com/check-auth/dwho -> true
513 https://test1.example.com/check-auth/dwho/api -> true
514 https://test1.example.com/check-auth/dwh -> false
515
516 \* You can set "restricted" flag to match exact URI:
517
518 .. code-block:: perl
519
520 varIsInUri($ENV{REQUEST_URI}, '/check-auth/', "$uid/", 1)
521
522 https://test1.example.com/check-auth/rtyler/ -> true
523 https://test1.example.com/check-auth/rtyler/api -> false
524 https://test1.example.com/check-auth/rtyler -> false
525
526 .. |image0| image:: /documentation/new.png
527 :width: 35px
528 .. |image1| image:: /documentation/new.png
529 :width: 35px
530
531 unicode2iso
532 ~~~~~~~~~~~
533
534 .. attention::
535
536 This function is not compliant with :doc:`Safe jail<safejail>`.
537 You will have to disable the jail to use it.
538
539 This function convert a string from UTF-8 to ISO-8859-1.
540
541 Function parameter:
542
543 - **string**
544
545 Simple usage example:
546
547 ::
548
549 unicode2iso($name)
550
551 unicode2isoSafe
552 ~~~~~~~~~~~~~~~
553
554 This function convert a string from UTF-8 to ISO-8859-1
555 but it is not as portable as the original one.
556
557 Function parameter:
558
559 - **string**
560
561 Simple usage example:
562
563 ::
564
565 unicode2isoSafe($name)