rewriteEngine on

# We have an attribute value from a DN that we want to paste into a filter
#
# N.B. Do not use ${escape2filter(${unescapedn($1)})}, but chain them inside
# the same rewriteMap like this, since only that is safe in the presence of
# arbitrary data
rewriteMap escape dn2filter unescapedn escape2filter

# We have a DN that we want to paste into a filter
rewriteMap escape dn2dnfilter escape2filter

# We have a filter value and want to construct a DN
rewriteMap escape fitler2dn unescapefilter escape2dn

rewriteContext testdn2filter
rewriteRule "^([^=]*)=([^,+]*)((\+[^,]*)?,(.*))?" "(&($1=${dn2filter($2)})(entryDN:dnOneLevelMatch:=$5))" ":"

rewriteContext testdn2dnfilter
rewriteRule ".*" "entryDN=${dn2dnfilter($0)}" ":"

rewriteContext testfilter2dn
rewriteRule "^\(([^=]*)=([^)]*)\)" "$1=${fitler2dn($2)},dc=example,dc=com" ":"
