397 lines
16 KiB
Bash
397 lines
16 KiB
Bash
#!/usr/bin/env bash
|
|
# Check an LDAP server (or a slapcat dump of one) against what this repo
|
|
# expects: that the schema it needs is loaded, and that a set of entries
|
|
# exists with (at least) the listed attribute values. Extra attributes/
|
|
# values/entries are fine -- this is a subset check, not a diff -- so it can
|
|
# run against the live ldap.metznet.ca as well as a freshly seeded/restored
|
|
# test instance.
|
|
#
|
|
# usage: ldap-verify.sh [-H uri] [-D binddn] [-w pw | -y pwfile] [-x]
|
|
# [-b base | -f dump.ldif] [-X] [-i attr]... [-I dn-regex]... [-K dn-regex]... [-p]
|
|
# [-s schema.txt] [-e expected.ldif]...
|
|
# -H/-D/-w/-y/-x passed through to ldapsearch/ldapcompare (simple bind)
|
|
# -b BASE fetch the whole subtree under BASE with ldapsearch and check
|
|
# it like a dump (-f). Use for cn=config (-b cn=config), whose
|
|
# ordered attributes don't compare well with ldapcompare.
|
|
# -f FILE check an LDIF dump (slapcat or ldapsearch) instead of a
|
|
# live server. Values are compared case-insensitively.
|
|
# -s FILE schema expectations, default tests/ldap/schema.txt:
|
|
# objectClass <name>
|
|
# attributeType <name>
|
|
# Checked against the live server's subschema entry only:
|
|
# skipped with -b/-f, since cn=config doesn't list
|
|
# attributes built into slapd (uidNumber) or registered
|
|
# by overlays (memberOf).
|
|
# -e FILE expected entries as LDIF (repeatable), default
|
|
# build/ldap-structure.ldif (generated from %ldap-structure
|
|
# by tests/ldap/generated-config.sh). Against a live server every
|
|
# attribute line is checked with ldapcompare, so values
|
|
# match using the server's own matching rules. A value
|
|
# written without a leading {N} matches any position of an
|
|
# X-ORDERED value (olcAccess, olcModuleLoad, ...); with one,
|
|
# only that position -- or a value the server stores with
|
|
# no position (e.g. a lone olcOverlay: memberof).
|
|
# -X exact (needs -b/-f): additionally fail on any entry or
|
|
# value in the dump that no -e file lists, so the -e files
|
|
# are a complete snapshot rather than a subset. Ignores
|
|
# operational attributes, olcRootPW, and the definitions
|
|
# in cn=schema,cn=config and the schema entries under it
|
|
# (compared by entry only; the live subschema check
|
|
# covers what's used).
|
|
# -i ATTR another attribute to ignore under -X/-p (repeatable)
|
|
# -I REGEX under -X/-p, skip entries whose DN matches this extended
|
|
# regex (case-insensitive; repeatable), e.g. user entries
|
|
# -K REGEX keep entries matching this regex even if an -I matches,
|
|
# e.g. the system accounts the repo binds as (repeatable)
|
|
# -p print the dump (-b/-f) as a snapshot -- everything -X
|
|
# would compare -- instead of checking, e.g.
|
|
# ... -b dc=metznet,dc=ca -p -I ',ou=users,...$' > dump.ldif
|
|
# Any -i/-I are written into it as directive comments
|
|
# that -X reads back from -e files:
|
|
# # ldap-verify: ignore-attr <attr>
|
|
# # ldap-verify: ignore-dn <regex>
|
|
# # ldap-verify: keep-dn <regex>
|
|
#
|
|
# Bind as a DN that can read everything you list -- entries hidden by ACLs
|
|
# show up as failures. TLS is ldap.conf/LDAPTLS_* as usual (e.g.
|
|
# LDAPTLS_REQCERT=never against a test instance with a self-signed cert).
|
|
#
|
|
# exit: 0 all checks passed, 1 some check failed, 2 usage/connection error
|
|
set -uo pipefail
|
|
|
|
here="$(cd "$(dirname "$0")/.." && pwd)"
|
|
schema_file="$here/tests/ldap/schema.txt"
|
|
ldif_files=()
|
|
conn=()
|
|
dump=""
|
|
base=""
|
|
exact=""
|
|
print=""
|
|
ignore_dns=()
|
|
keep_dns=()
|
|
extra_ignores=""
|
|
ignore="entryuuid creatorsname createtimestamp entrycsn modifiersname modifytimestamp structuralobjectclass contextcsn entrydn hassubordinates subschemasubentry olcrootpw"
|
|
schema_defs="olcattributetypes olcobjectclasses olcobjectidentifier olcldapsyntaxes olcditcontentrules"
|
|
|
|
while getopts "H:D:w:y:xb:f:Xi:I:K:ps:e:h" opt; do
|
|
case $opt in
|
|
H|D|w|y) conn+=("-$opt" "$OPTARG") ;;
|
|
x) conn+=(-x) ;;
|
|
b) base="$OPTARG" ;;
|
|
f) dump="$OPTARG" ;;
|
|
X) exact=1 ;;
|
|
i) ignore="$ignore $(tr 'A-Z' 'a-z' <<<"$OPTARG")"; extra_ignores="$extra_ignores $OPTARG" ;;
|
|
I) ignore_dns+=("$OPTARG") ;;
|
|
K) keep_dns+=("$OPTARG") ;;
|
|
p) print=1 ;;
|
|
s) schema_file="$OPTARG" ;;
|
|
e) ldif_files+=("$OPTARG") ;;
|
|
*) sed -n '2,/^set /s/^# \{0,1\}//p' "$0"; exit 2 ;;
|
|
esac
|
|
done
|
|
if [[ ${#ldif_files[@]} -eq 0 ]]; then
|
|
ldif_files=("$here/build/ldap-structure.ldif")
|
|
[[ -r "${ldif_files[0]}" ]] || {
|
|
echo "no ${ldif_files[0]}; build it with:" >&2
|
|
echo " docker compose run --rm -T guix bash -lc tests/ldap/generated-config.sh" >&2
|
|
exit 2
|
|
}
|
|
fi
|
|
if [[ -n "$exact$print" && -z "$dump$base" ]]; then
|
|
echo "-X/-p need a dump: -b BASE or -f FILE" >&2; exit 2
|
|
fi
|
|
# Directives saved into a snapshot by -p.
|
|
if [[ -n "$exact" ]]; then
|
|
while read -r _ _ kind arg; do
|
|
case $kind in
|
|
ignore-attr) ignore="$ignore $(tr 'A-Z' 'a-z' <<<"$arg")" ;;
|
|
ignore-dn) ignore_dns+=("$arg") ;;
|
|
keep-dn) keep_dns+=("$arg") ;;
|
|
*) printf 'unknown directive "%s"\n' "$kind" >&2; exit 2 ;;
|
|
esac
|
|
done < <(grep -h '^# ldap-verify: ' "${ldif_files[@]}")
|
|
fi
|
|
# Anonymous simple bind unless told otherwise, rather than SASL.
|
|
[[ " ${conn[*]-} " == *" -x "* ]] || conn+=(-x)
|
|
|
|
pass=0
|
|
fail=0
|
|
ok() { pass=$((pass + 1)); printf ' ok %s\n' "$*"; }
|
|
bad() { fail=$((fail + 1)); printf ' FAIL %s\n' "$*"; }
|
|
|
|
search() { ldapsearch "${conn[@]}" -LLL -o ldif-wrap=no "$@"; }
|
|
|
|
# Unfold LDIF continuation lines, drop comments, and emit one record per
|
|
# attribute: "<dn-sep>\t<dn>\t<attr>\t<sep>\t<value>", sep being ":" or "::"
|
|
# (base64), with a bare "<dn-sep>\t<dn>" record starting each entry.
|
|
parse_ldif() {
|
|
awk '
|
|
function flush() {
|
|
if (line == "") return
|
|
if (line !~ /^#/) {
|
|
i = index(line, ":")
|
|
attr = substr(line, 1, i - 1)
|
|
sep = ":"; rest = substr(line, i + 1)
|
|
if (substr(rest, 1, 1) == ":") { sep = "::"; rest = substr(rest, 2) }
|
|
else if (substr(rest, 1, 1) == "<") { printf "unsupported URL value: %s\n", line > "/dev/stderr"; exit 2 }
|
|
sub(/^ +/, "", rest)
|
|
if (tolower(attr) == "dn") { dnsep = sep; dn = rest; print dnsep "\t" dn }
|
|
else if (dn != "" && attr != "changetype") print dnsep "\t" dn "\t" attr "\t" sep "\t" rest
|
|
}
|
|
line = ""
|
|
}
|
|
/^ / { line = line substr($0, 2); next }
|
|
{ flush() }
|
|
/^$/ { dn = "" }
|
|
{ line = $0 }
|
|
END { flush() }
|
|
' "$1"
|
|
}
|
|
|
|
# Comparable forms for the dump index: DNs lowercased with no spaces around
|
|
# "," or "=", attribute names lowercased, base64 values decoded.
|
|
norm_dn() { tr 'A-Z' 'a-z' <<<"$1" | sed -E 's/ *([,=]) */\1/g'; }
|
|
decode() { if [[ "$1" == "::" ]]; then base64 --decode <<<"$2" 2>/dev/null || printf '%s' "$2"; else printf '%s' "$2"; fi; }
|
|
# skipped_dn NDN -> 0 if -X/-p skip this whole entry (-I, unless -K)
|
|
skipped_dn() {
|
|
local re
|
|
for re in ${keep_dns[@]+"${keep_dns[@]}"}; do
|
|
[[ "$1" =~ $(tr 'A-Z' 'a-z' <<<"$re") ]] && return 1
|
|
done
|
|
for re in ${ignore_dns[@]+"${ignore_dns[@]}"}; do
|
|
[[ "$1" =~ $(tr 'A-Z' 'a-z' <<<"$re") ]] && return 0
|
|
done
|
|
return 1
|
|
}
|
|
# ignored LATTR NDN -> 0 if -X/-p skip this attribute on this entry
|
|
ignored() {
|
|
[[ " $ignore " == *" $1 "* ]] && return 0
|
|
[[ ( "$2" == "cn=schema,cn=config" || "$2" == *",cn=schema,cn=config" ) && " $schema_defs " == *" $1 "* ]]
|
|
}
|
|
|
|
# --- connectivity / load ----------------------------------------------------
|
|
|
|
tmp="$(mktemp -d)"
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
|
|
if [[ -n "$base" ]]; then
|
|
dump="$tmp/subtree.ldif"
|
|
if ! search -b "$base" '(objectClass=*)' >"$dump" 2>"$tmp/err"; then
|
|
printf 'cannot read %s:\n%s\n' "$base" "$(cat "$tmp/err")" >&2
|
|
exit 2
|
|
fi
|
|
fi
|
|
|
|
if [[ -n "$dump" ]]; then
|
|
[[ -r "$dump" ]] || { echo "cannot read $dump" >&2; exit 2; }
|
|
index="$tmp/index"
|
|
# "|<dn>" per entry and "|<dn>\t<attr>\t=<value>" per value, plus
|
|
# "|<dn>\t<attr>\t~<value>" for the value without its {N} ordering
|
|
# prefix. The leading "|" anchors substring matches to the start of a line.
|
|
parse_ldif "$dump" | while IFS=$'\t' read -r dnsep dn attr sep value; do
|
|
[[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")"
|
|
ndn="$(norm_dn "$dn")"
|
|
if [[ -z "${attr:-}" ]]; then
|
|
printf '|%s\n' "$ndn"
|
|
else
|
|
lattr="$(tr 'A-Z' 'a-z' <<<"$attr")"
|
|
v="$(decode "$sep" "$value")"
|
|
printf '|%s\t%s\t=%s\n' "$ndn" "$lattr" "$v"
|
|
if [[ "$v" =~ ^\{-?[0-9]+\}(.*)$ ]]; then
|
|
printf '|%s\t%s\t~%s\n' "$ndn" "$lattr" "${BASH_REMATCH[1]}"
|
|
fi
|
|
fi
|
|
done >"$index" || exit 2
|
|
|
|
if [[ -n "$print" ]]; then
|
|
first=1
|
|
for a in $extra_ignores; do echo "# ldap-verify: ignore-attr $a"; first=""; done
|
|
for re in ${ignore_dns[@]+"${ignore_dns[@]}"}; do echo "# ldap-verify: ignore-dn $re"; first=""; done
|
|
for re in ${keep_dns[@]+"${keep_dns[@]}"}; do echo "# ldap-verify: keep-dn $re"; first=""; done
|
|
parse_ldif "$dump" | while IFS=$'\t' read -r dnsep dn attr sep value; do
|
|
if [[ -z "${attr:-}" ]]; then
|
|
ndn="$(norm_dn "$([[ "$dnsep" == "::" ]] && base64 --decode <<<"$dn" || printf '%s' "$dn")")"
|
|
skip=""
|
|
if skipped_dn "$ndn"; then skip=1; continue; fi
|
|
[[ -n "$first" ]] || echo
|
|
first=""
|
|
printf 'dn%s %s\n' "$dnsep" "$dn"
|
|
elif [[ -z "$skip" ]] && ! ignored "$(tr 'A-Z' 'a-z' <<<"$attr")" "$ndn"; then
|
|
printf '%s%s %s\n' "$attr" "$sep" "$value"
|
|
fi
|
|
done
|
|
exit 0
|
|
fi
|
|
else
|
|
if ! rootdse="$(search -s base -b "" '(objectClass=*)' subschemaSubentry 2>&1)"; then
|
|
printf 'cannot query root DSE:\n%s\n' "$rootdse" >&2
|
|
exit 2
|
|
fi
|
|
subschema="$(sed -n 's/^subschemaSubentry: //p' <<<"$rootdse")"
|
|
subschema="${subschema:-cn=Subschema}"
|
|
if ! schema="$(search -s base -b "$subschema" '(objectClass=*)' objectClasses attributeTypes 2>&1)"; then
|
|
printf 'cannot read %s:\n%s\n' "$subschema" >&2
|
|
exit 2
|
|
fi
|
|
fi
|
|
|
|
# has_entry DN -> 0 if present
|
|
# compare DN ATTR SEP VALUE -> 6 match, 5 other value, 16 attribute absent
|
|
# (ldapcompare's exit codes), else error
|
|
if [[ -n "$dump" ]]; then
|
|
has_entry() { grep -Fixq -- "|$(norm_dn "$1")" "$index"; }
|
|
compare() {
|
|
local key
|
|
key="|$(norm_dn "$1")"$'\t'"$(tr 'A-Z' 'a-z' <<<"$2")"$'\t'
|
|
local v; v="$(decode "$3" "$4")"
|
|
if [[ "$v" =~ ^\{-?[0-9]+\}(.*)$ ]]; then
|
|
# positioned: that exact position, or a value stored with no position
|
|
grep -Fixq -- "$key=$v" "$index" && return 6
|
|
grep -Fixq -- "$key=${BASH_REMATCH[1]}" "$index" && return 6
|
|
else
|
|
grep -Fixq -- "$key=$v" "$index" && return 6
|
|
grep -Fixq -- "$key~$v" "$index" && return 6
|
|
fi
|
|
if grep -Fiq -- "$key" "$index"; then return 5; fi
|
|
return 16
|
|
}
|
|
else
|
|
has_entry() { search -s base -b "$1" '(objectClass=*)' 1.1 >/dev/null 2>&1; }
|
|
compare() { out="$(ldapcompare "${conn[@]}" -z "$1" "$2$3$4" 2>&1)"; }
|
|
fi
|
|
|
|
# --- schema -----------------------------------------------------------------
|
|
|
|
echo "schema ($schema_file)"
|
|
if [[ -n "$dump" ]]; then
|
|
echo " (skipped: only checked against a live server's subschema entry)"
|
|
else
|
|
while read -r kind name _; do
|
|
[[ -z "${kind:-}" || "$kind" == \#* ]] && continue
|
|
case $kind in
|
|
objectClass) attr=objectClasses ;;
|
|
attributeType) attr=attributeTypes ;;
|
|
*) printf 'unknown schema kind "%s" in %s\n' "$kind" "$schema_file" >&2; exit 2 ;;
|
|
esac
|
|
# "NAME 'x'" or "NAME ( 'x' 'y' )"
|
|
if grep -Eiq "^$attr: \(.* NAME (\\( [^)]*)?'$name'" <<<"$schema"; then
|
|
ok "$kind $name"
|
|
else
|
|
bad "$kind $name not in $subschema"
|
|
fi
|
|
done <"$schema_file"
|
|
fi
|
|
|
|
# --- entries ----------------------------------------------------------------
|
|
|
|
# lint FILE: problems in an expected-entries file that would otherwise be
|
|
# silently merged or never match -- a DN listed twice, or an RDN whose value
|
|
# isn't among that entry's own values of the RDN attribute.
|
|
lint_ldif() {
|
|
parse_ldif "$1" | awk -F'\t' '
|
|
function check() {
|
|
if (dn != "" && has && !match_) printf "%s: RDN %s=%s but its %s values don'"'"'t include it\n", orig, rattr, rval, rattr
|
|
}
|
|
NF == 2 {
|
|
check()
|
|
orig = $2; dn = tolower($2); gsub(/ *, */, ",", dn); gsub(/ *= */, "=", dn)
|
|
if (seen[dn]++) printf "%s: listed more than once\n", orig
|
|
rdn = dn; sub(/,.*/, "", rdn)
|
|
rattr = rdn; sub(/=.*/, "", rattr); rval = rdn; sub(/^[^=]*=/, "", rval)
|
|
sub(/^\{-?[0-9]+\}/, "", rval) # X-ORDERED RDNs, e.g. olcOverlay={0}memberof
|
|
if (rdn ~ /\+/ || $1 == "::") rattr = ""
|
|
has = 0; match_ = 0; next
|
|
}
|
|
NF == 5 && rattr != "" && tolower($3) == rattr && $4 == ":" {
|
|
v = tolower($5); sub(/^\{-?[0-9]+\}/, "", v)
|
|
has = 1; if (v == rval) match_ = 1
|
|
}
|
|
END { check() }
|
|
'
|
|
}
|
|
|
|
for ldif in "${ldif_files[@]}"; do
|
|
echo "entries ($ldif)"
|
|
while IFS= read -r problem; do
|
|
[[ -n "$problem" ]] && bad "$problem"
|
|
done < <(lint_ldif "$ldif")
|
|
records="$(parse_ldif "$ldif")" || exit 2
|
|
skip=""
|
|
while IFS=$'\t' read -r dnsep dn attr sep value; do
|
|
[[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")"
|
|
if [[ -z "${attr:-}" ]]; then
|
|
if has_entry "$dn"; then
|
|
ok "$dn"; skip=""
|
|
else
|
|
bad "$dn missing (or not readable by this bind DN)"; skip="$dn"
|
|
fi
|
|
continue
|
|
fi
|
|
[[ "$skip" == "$dn" ]] && continue
|
|
# what -X ignores in a dump (schema definitions, olcRootPW, operational
|
|
# attributes) isn't compared from the expected side either, so a
|
|
# generated config can be the expected file
|
|
if [[ -n "$dump" ]] && ignored "$(tr 'A-Z' 'a-z' <<<"$attr")" "$(norm_dn "$dn")"; then continue; fi
|
|
out=""
|
|
compare "$dn" "$attr" "$sep" "$value"
|
|
case $? in
|
|
6) ok "$dn $attr: $value" ;;
|
|
5) bad "$dn $attr: $value (different value)" ;;
|
|
16) bad "$dn $attr: $value (attribute absent)" ;;
|
|
*) bad "$dn $attr: $value ($(tr '\n' ' ' <<<"$out"))" ;;
|
|
esac
|
|
done <<<"$records"
|
|
done
|
|
|
|
# --- exact: nothing beyond what the -e files list ---------------------------
|
|
|
|
if [[ -n "$exact" ]]; then
|
|
echo "exact (no entries or values beyond the above)"
|
|
expected="$tmp/expected"
|
|
for ldif in "${ldif_files[@]}"; do
|
|
parse_ldif "$ldif" | while IFS=$'\t' read -r dnsep dn attr sep value; do
|
|
[[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")"
|
|
if [[ -z "${attr:-}" ]]; then
|
|
printf '|%s\n' "$(norm_dn "$dn")"
|
|
else
|
|
ev="$(decode "$sep" "$value")"
|
|
printf '|%s\t%s\t=%s\n' "$(norm_dn "$dn")" "$(tr 'A-Z' 'a-z' <<<"$attr")" "$ev"
|
|
if [[ "$ev" =~ ^\{-?[0-9]+\}(.*)$ ]]; then
|
|
printf '|%s\t%s\t~%s\n' "$(norm_dn "$dn")" "$(tr 'A-Z' 'a-z' <<<"$attr")" "${BASH_REMATCH[1]}"
|
|
fi
|
|
fi
|
|
done
|
|
done >"$expected"
|
|
before=$fail
|
|
skip=""
|
|
while IFS=$'\t' read -r dnsep dn attr sep value; do
|
|
[[ "$dnsep" == "::" ]] && dn="$(base64 --decode <<<"$dn")"
|
|
ndn="$(norm_dn "$dn")"
|
|
if [[ -z "${attr:-}" ]]; then
|
|
skip=""
|
|
if skipped_dn "$ndn"; then skip="$ndn"; continue; fi
|
|
grep -Fixq -- "|$ndn" "$expected" || { bad "unexpected entry $dn"; skip="$ndn"; }
|
|
continue
|
|
fi
|
|
[[ "$skip" == "$ndn" ]] && continue
|
|
lattr="$(tr 'A-Z' 'a-z' <<<"$attr")"
|
|
ignored "$lattr" "$ndn" && continue
|
|
v="$(decode "$sep" "$value")"
|
|
key="|$ndn"$'\t'"$lattr"$'\t'
|
|
grep -Fixq -- "$key=$v" "$expected" && continue
|
|
if [[ "$v" =~ ^\{-?[0-9]+\}(.*)$ ]]; then
|
|
# positioned on the server: listed unpositioned in the expected file
|
|
grep -Fixq -- "$key=${BASH_REMATCH[1]}" "$expected" && continue
|
|
else
|
|
# unpositioned on the server: listed at any position
|
|
grep -Fixq -- "$key~$v" "$expected" && continue
|
|
fi
|
|
bad "$dn unexpected $attr: $value"
|
|
done < <(parse_ldif "$dump")
|
|
[[ $fail -eq $before ]] && ok "nothing unexpected in ${base:-$dump}"
|
|
fi
|
|
|
|
echo
|
|
echo "$pass passed, $fail failed"
|
|
[[ $fail -eq 0 ]]
|