| 1 |
Writing rules and headers |
| 2 |
========================= |
| 3 |
|
| 4 |
LemonLDAP::NG manages applications by their hostname (Apache Virtual Hosts or |
| 5 |
Nginx Block Servers). Rules are employed for protecting applications, |
| 6 |
and HTTP headers are appended to each request for sending data to protected |
| 7 |
applications (for logs, profiles,...). |
| 8 |
|
| 9 |
|
| 10 |
.. attention:: |
| 11 |
|
| 12 |
Note that variables designed by $xx correspond to the |
| 13 |
name of the :doc:`exported variables<exportedvars>` or |
| 14 |
:ref:`macro names<macros_and_groups>` except for ``$ENV{<cgi-header>}`` which |
| 15 |
correspond to CGI headers (``$ENV{REMOTE_ADDR}`` for example). |
| 16 |
|
| 17 |
|
| 18 |
Available %ENV or $env variables |
| 19 |
-------------------------------- |
| 20 |
|
| 21 |
The %ENV hash or $env hash reference provide: |
| 22 |
|
| 23 |
- all headers in CGI format (``User-Agent`` becomes ``HTTP_USER_AGENT``) |
| 24 |
- some CGI variables depending on the context: |
| 25 |
|
| 26 |
- For portal: all CGI standard variables (you can add custom |
| 27 |
headers using ``fastcgi_param`` with Nginx), |
| 28 |
- For Apache handler: REMOTE_ADDR, QUERY_STRING, REQUEST_URI, SERVER_PORT, REQUEST_METHOD, |
| 29 |
- For Nginx handler: all variables provided by ``fastcgi_param`` commands. |
| 30 |
|
| 31 |
- For Portal: |
| 32 |
|
| 33 |
- ``$env->{urldc}``: origin URL before Handler redirection, in cleartext |
| 34 |
- ``$env->{_url}``: origin URL before Handler redirection, base64 encoded |
| 35 |
|
| 36 |
See also :doc:`extended functions<extendedfunctions>`. |
| 37 |
|
| 38 |
.. tip:: |
| 39 |
|
| 40 |
You can use %ENV hash or $env hash reference. Both syntaxes are available for retro compatibity. |
| 41 |
|
| 42 |
:: |
| 43 |
|
| 44 |
$ENV{REMOTE_ADDR} and $env->{REMOTE_ADDR} are similar. |
| 45 |
|
| 46 |
$env and template variables can be dumped by setting ``logParams = 1`` in ``lemonldap-ng.ini`` file. |
| 47 |
It requires ``logLevel = debug``. |
| 48 |
|
| 49 |
.. _rules: |
| 50 |
|
| 51 |
Rules |
| 52 |
----- |
| 53 |
|
| 54 |
A rule associates a `regular |
| 55 |
expression <http://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions>`__ |
| 56 |
to a Perl boolean expression or a keyword. |
| 57 |
|
| 58 |
|image0| |
| 59 |
|
| 60 |
Examples: |
| 61 |
|
| 62 |
=============================================================================================================================================== ================== ====================================== |
| 63 |
Goal Regular expression Rule |
| 64 |
=============================================================================================================================================== ================== ====================================== |
| 65 |
Restrict /admin/ directory to user bart.simpson ^/admin/ |
| 66 |
Restrict /js/ and /css/ directory to authenticated users ^/(css|js)/ accept |
| 67 |
Deny access to /config/ directory ^/config/ deny |
| 68 |
Do not restrict /public/ ^/public/ skip |
| 69 |
Do not restrict /skip/ and restrict other to authenticated users ^/skip/ $env->{REQUEST_URI} =~ /skip/ ? skip : 1 |
| 70 |
Makes authentication optional, but authenticated users are seen as such (that is, user data are sent to the app through HTTP headers) ^/forum/ unprotect |
| 71 |
Restrict access to the whole site to users that have the LDAP description field set to "LDAP administrator" (must be set in exported variables) default |
| 72 |
=============================================================================================================================================== ================== ====================================== |
| 73 |
|
| 74 |
The "**default**" access rule is used if none rule matches the current URL. |
| 75 |
|
| 76 |
|
| 77 |
.. tip:: |
| 78 |
|
| 79 |
See :doc:`the rule examples page<rules_examples>` for few |
| 80 |
common use cases |
| 81 |
|
| 82 |
|
| 83 |
.. tip:: |
| 84 |
|
| 85 |
|
| 86 |
- Comments can be employed for ordering your rules: rules are applied depending on |
| 87 |
comment alphabetical order (or regexp if no comment is defined). See |
| 88 |
:ref:`security chapter<security-write-good-rules>` to learn more |
| 89 |
about rules best practice. |
| 90 |
- See :ref:`performances<performances-handler-performance>` to learn how |
| 91 |
using macros and groups in rules. |
| 92 |
|
| 93 |
|
| 94 |
Rules can also be used for intercepting logout URL: |
| 95 |
|
| 96 |
================================================================================================================= ======================= ===================================== |
| 97 |
Goal Regular expression Rule |
| 98 |
================================================================================================================= ======================= ===================================== |
| 99 |
Logout user from Lemonldap::NG and redirect it to http://intranet/ ``^/index.php\?logout`` logout_sso http://intranet/ |
| 100 |
Logout user from current application and redirect it to the menu **(Apache only)** ``^/index.php\?logout`` logout_app https://auth.example.com/ |
| 101 |
Logout user from current application and from Lemonldap::NG and redirect it to http://intranet/ **(Apache only)** ``^/index.php\?logout`` logout_app_sso http://intranet/ |
| 102 |
================================================================================================================= ======================= ===================================== |
| 103 |
|
| 104 |
|
| 105 |
.. danger:: |
| 106 |
|
| 107 |
\ For ``logout_app`` and ``logout_app_sso`` rules to work with Nginx, you need |
| 108 |
lua module, and you must enable ``nginx-lua-headers.conf``. |
| 109 |
Also, make sure the ``header_filter_by_lua`` and corresponding ``error_page`` directives |
| 110 |
are present in the appropriate "location" section. See ``test-nginx.conf`` for more information. |
| 111 |
|
| 112 |
By default, users will be redirected to the Portal if no URL is defined, |
| 113 |
or to the specified URL if exists. |
| 114 |
|
| 115 |
|
| 116 |
.. attention:: |
| 117 |
|
| 118 |
Only current application is concerned by logout_app\* |
| 119 |
targets. Be careful with some applications which does not check |
| 120 |
headers sent by LL::NG after having created their own application cookies. |
| 121 |
If so, you can redirect users to a HTML page that explain that it is better |
| 122 |
to close browser after logout. |
| 123 |
|
| 124 |
Rules based on authentication level |
| 125 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 126 |
|
| 127 |
LL::NG set an "authentication level" during authentication process. This |
| 128 |
level depends on authentication backend used by this user. Default |
| 129 |
values are: |
| 130 |
|
| 131 |
- 0 for :doc:`Null<authnull>` |
| 132 |
- 1 for :doc:`CAS<authcas>`, :doc:`OpenID Connect<authopenidconnect>`, |
| 133 |
:doc:`Facebook<authfacebook>`,… |
| 134 |
- 2 for web-form based authentication (:doc:`LDAP<authldap>`, |
| 135 |
:doc:`DBI<authdbi>`,…) |
| 136 |
- 3 for :doc:`Kerberos<authkerberos>` |
| 137 |
- 5 for :doc:`SSL<authssl>` |
| 138 |
|
| 139 |
There are three ways to impose users a higher authentication level: |
| 140 |
|
| 141 |
- writing a rule based on authentication level: |
| 142 |
``$authenticationLevel > 3`` |
| 143 |
- since 2.0, set a minimum level in virtual host options (default value |
| 144 |
for ALL access rules) |
| 145 |
- since 2.0.7, a minimum authentication level can be set for each URI |
| 146 |
access rule. Useful if URI are protected by different types of |
| 147 |
handler (AuthBasic -> level 2, Main -> level set by authentication |
| 148 |
backend). |
| 149 |
|
| 150 |
|
| 151 |
.. tip:: |
| 152 |
|
| 153 |
Instead of returning a 403 code, "minimum level" returns users |
| 154 |
to a form that explains that a higher level is required and propose to |
| 155 |
reauthenticate himself. |
| 156 |
|
| 157 |
.. _headers: |
| 158 |
|
| 159 |
Headers |
| 160 |
------- |
| 161 |
|
| 162 |
Headers are associations between an header name and a perl expression |
| 163 |
that returns a value. Headers are used for sending user data to protected |
| 164 |
applications. |
| 165 |
|
| 166 |
Examples: |
| 167 |
|
| 168 |
============================= ============ ======================= |
| 169 |
Goal Header name Header value |
| 170 |
============================= ============ ======================= |
| 171 |
Give the uid (for accounting) Auth-User $uid |
| 172 |
Give a static value Some-Thing "static-value" |
| 173 |
Give display name Display-Name "$givenName $surName" |
| 174 |
Give a non ASCII data Display-Name |
| 175 |
============================= ============ ======================= |
| 176 |
|
| 177 |
As described in |
| 178 |
:ref:`performances chapter<performances-handler-performance>`, you can |
| 179 |
use macros, local macros,... |
| 180 |
|
| 181 |
|
| 182 |
.. attention:: |
| 183 |
|
| 184 |
- Since many HTTP servers refuse non ASCII headers, it is recommended |
| 185 |
to use encode_base64() function to transmit those headers |
| 186 |
- Do not forget to add an empty string as second argument to |
| 187 |
encode_base64 function to avoid a "newline" characters insertion in |
| 188 |
result: ``encode_base64($string, '')`` |
| 189 |
- Header names must contain only letters and "-" character. |
| 190 |
With Nginx, you can bypass this restriction by using |
| 191 |
``underscores_in_headers on;`` directive |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
.. tip:: |
| 197 |
|
| 198 |
By default, SSO cookie is hidden. So protected applications |
| 199 |
cannot retrieve SSO session key. But you can forward this key if |
| 200 |
absolutely needed (NOT recommanded because can be a security issue): |
| 201 |
|
| 202 |
:: |
| 203 |
|
| 204 |
Session-ID => $_session_id |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
Available functions |
| 209 |
------------------- |
| 210 |
|
| 211 |
In addition to macros and name, you can use some functions in rules and |
| 212 |
headers: |
| 213 |
|
| 214 |
- :doc:`LL::NG extended functions<extendedfunctions>` |
| 215 |
- :doc:`Your custom functions<customfunctions>` |
| 216 |
|
| 217 |
Wildcards in hostnames |
| 218 |
---------------------- |
| 219 |
|
| 220 |
|image1| Since 2.0, a wildcard can be used in virtualhost name (not in |
| 221 |
aliases !): ``*.example.com`` matches all hostnames that belong to |
| 222 |
``example.com`` domain. Version 2.0.9 improves this and allows better |
| 223 |
wildcards such as ``test-*.example.com`` or ``test-%.example.com``. The |
| 224 |
``%`` wilcard doesn't match subdomains. |
| 225 |
|
| 226 |
Even if a wildcard exists, if a VirtualHost is explicitly declared, this |
| 227 |
rule will be applied. Example with precedence order for test.sub.example.com: |
| 228 |
|
| 229 |
#. test.sub.example.com |
| 230 |
#. test%.sub.example.com |
| 231 |
#. test*.sub.example.com |
| 232 |
#. %.sub.example.com |
| 233 |
#. \*.sub.example.com |
| 234 |
#. \*.example.com (``%.example.com`` does not match |
| 235 |
test.sub.example.com) |
| 236 |
|
| 237 |
.. |image0| image:: /documentation/manager-rule.png |
| 238 |
:class: align-center |
| 239 |
.. |image1| image:: /documentation/new.png |
| 240 |
:width: 35px |