| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
set -e |
| 4 |
|
| 5 |
# DEBIAN_FRONTEND=noninteractive apt --yes install adduser slapd ldap-utils sssd cron sudo man-db procps vim whiptail |
| 6 |
# slappasswd -s kkkk |
| 7 |
|
| 8 |
TESTNR="04" |
| 9 |
BASEDIR="$(pwd)/debian/tests" |
| 10 |
COMMONDIR="${BASEDIR}/common" |
| 11 |
DIR="${BASEDIR}/${TESTNR}" |
| 12 |
PATH="/bin:/usr/bin:/sbin:/usr/sbin" |
| 13 |
ACCTA="testuser1" |
| 14 |
ACCTB="testuser2" |
| 15 |
PASSWD="test${TESTNR}23456" |
| 16 |
HOMEDIRA="/home/${ACCTA}" |
| 17 |
HOMEDIRB="/home/${ACCTB}" |
| 18 |
LDIFDIR="${DIR}/ldif" |
| 19 |
SSSDCONF="/etc/sssd/sssd.conf" |
| 20 |
RUNDIR="/run/slapd" |
| 21 |
VARRUNDIR="/var/run/slapd" |
| 22 |
|
| 23 |
trap ' |
| 24 |
kill $(pidof slapd) 2>/dev/null || true |
| 25 |
kill $(pidof sssd) 2>/dev/null || true |
| 26 |
kill $(pidof socat) 2>/dev/null || true |
| 27 |
rm -f /dev/log || true |
| 28 |
' 0 INT QUIT ABRT PIPE TERM |
| 29 |
|
| 30 |
# openssl req -x509 -days 365 -nodes -newkey rsa:4096 -keyout server_key.pem -out server_cert.pem --subj "/C=DE/CN=emptysid86.zugschlus.de" |
| 31 |
|
| 32 |
printf "make and chown dirs ... " |
| 33 |
mkdir -p "${RUNDIR}" "${VARRUNDIR}" |
| 34 |
chown openldap "${VARRUNDIR}" |
| 35 |
< ${LDIFDIR}/debconf debconf-set-selections |
| 36 |
|
| 37 |
printf "clean up ldap database ... " |
| 38 |
rm -rf /var/lib/ldap/*.mdb |
| 39 |
|
| 40 |
printf "move configuration in place ... " |
| 41 |
mkdir -p /etc/ldap /etc/sssd |
| 42 |
cp ${LDIFDIR}/server_*.pem /etc/ldap/ |
| 43 |
cp ${LDIFDIR}/ldap.conf /etc/ldap/ |
| 44 |
chown openldap:openldap /etc/ldap/server_*.pem |
| 45 |
chmod 600 /etc/ldap/server_key.pem |
| 46 |
# slapd.conf is only needed for OpenLDAP 2.4 on bullseye |
| 47 |
# but since it's already there now, use it for OpenLDAP 2.5+ as well |
| 48 |
# this is a testsuite. If you want /etc/ldap/slapd.d to be used (again), |
| 49 |
# please submit a patch that will also work on bullseye. |
| 50 |
cp ${LDIFDIR}/slapd.conf /etc/ldap/ |
| 51 |
cp ${LDIFDIR}/sssd.conf /etc/sssd |
| 52 |
chown root:root /etc/sssd/sssd.conf |
| 53 |
chmod 600 /etc/sssd/sssd.conf |
| 54 |
cp ${LDIFDIR}/slapd-default /etc/default/slapd |
| 55 |
echo "slapd: [::1]" >> /etc/hosts.allow |
| 56 |
|
| 57 |
printf "reconfigure slapd ... " |
| 58 |
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure -pcritical slapd 2>/dev/null |
| 59 |
kill $(pidof slapd) 2>/dev/null || true |
| 60 |
sleep 1 |
| 61 |
|
| 62 |
if ! [ -S /dev/log ]; then |
| 63 |
echo "starting fake syslog socket on /dev/log" |
| 64 |
|
| 65 |
# remove stale file if present |
| 66 |
[ -e /dev/log ] && rm -f /dev/log |
| 67 |
|
| 68 |
socat -u UNIX-RECV:/dev/log,mode=666 STDOUT >/dev/null 2>/dev/null & |
| 69 |
fi |
| 70 |
|
| 71 |
printf "start slapd ... " |
| 72 |
slapd -f /etc/ldap/slapd.conf -h "ldaps://:1636/ ldapi:///" -g openldap -u openldap |
| 73 |
|
| 74 |
printf "check slapd running .... " |
| 75 |
pgrep -a slapd |
| 76 |
# ldapsearch -x -LLL -s base -b "" namingContexts should work here |
| 77 |
|
| 78 |
printf "add users and groups OUs ...\n" |
| 79 |
ldapadd -x -c -D "cn=admin,dc=example,dc=com" -w ldappw -f ${LDIFDIR}/sss-ous.ldif 2>/dev/null || true |
| 80 |
|
| 81 |
printf "sssd.conf ...\n" |
| 82 |
cp ${LDIFDIR}/sssd.conf "${SSSDCONF}" |
| 83 |
|
| 84 |
printf "sudoers file ...\n" |
| 85 |
mkdir -p /etc/sudoers.d/ |
| 86 |
cp ${LDIFDIR}/ldapsudoers /etc/sudoers.d/ |
| 87 |
chown root:root "${SSSDCONF}" /etc/sudoers.d/ /etc/sudoers.d/* |
| 88 |
chmod 755 /etc/sudoers.d/ |
| 89 |
chmod 600 "${SSSDCONF}" /etc/sudoers.d/* |
| 90 |
|
| 91 |
printf "start sssd ..." |
| 92 |
kill $(pidof sssd) 2>/dev/null || true |
| 93 |
sleep 1 |
| 94 |
sssd --logger=files -D |
| 95 |
|
| 96 |
printf "check sssd running .... " |
| 97 |
pgrep -a sssd |
| 98 |
|
| 99 |
printf "add users ..." |
| 100 |
for user in testuser1 testuser2; do |
| 101 |
ldapadd -x -D "cn=admin,dc=example,dc=com" -w ldappw -f ${LDIFDIR}/${user}.ldif 2>/dev/null |
| 102 |
getent passwd ${user} |
| 103 |
mkdir -p /home/${user} |
| 104 |
chown ${user}:nogroup /home/${user} |
| 105 |
done |
| 106 |
ldapadd -x -D "cn=admin,dc=example,dc=com" -w ldappw -f ${LDIFDIR}/ldapsudoers.ldif 2>/dev/null |
| 107 |
# ldapsearch -x -D "cn=admin,dc=example,dc=com" -w ldappw -b "dc=example,dc=com" -s sub "(objectclass=*)" should work here. |
| 108 |
|
| 109 |
printf "========= test %s\.1: account group member, correct password\n" "${TESTNR}" |
| 110 |
RET=0 |
| 111 |
printf "trying %s with correct password\n" "${ACCTA}" |
| 112 |
su - "${ACCTA}" -c "ASUSER_REPEAT=1 ${COMMONDIR}/asuser ${PASSWD}" || RET=$? |
| 113 |
printf "%s with correct password, return value %s\n" "${ACCTA}" "${RET}" |
| 114 |
if [ "$(< ${HOMEDIRA}/stdout tr -d '\n')" != "00" ]; then |
| 115 |
printf >&2 "test result not as expected:\n" |
| 116 |
printf >&2 "stdout:\n" |
| 117 |
cat >&2 ${HOMEDIRA}/stdout |
| 118 |
printf >&2 "stderr:\n" |
| 119 |
cat >&2 ${HOMEDIRA}/stderr |
| 120 |
printf >&2 "exit code %s\n" "${RET}" |
| 121 |
printf >&2 "exit 1\n" "${RET}" |
| 122 |
exit 1 |
| 123 |
fi |
| 124 |
|
| 125 |
printf "========= test %s\.3: account group member, wrong password\n" "${TESTNR}" |
| 126 |
rm -f "${HOMEDIRA}/std*" |
| 127 |
rm -f /run/sudo/ts/10001 |
| 128 |
RET=0 |
| 129 |
printf "trying %s with wrong password\n" "${ACCTA}" |
| 130 |
su - "${ACCTA}" -c "${COMMONDIR}/asuser wrongpasswd" || RET=$? |
| 131 |
printf "%s with wrong password, return value %s\n" "${ACCTA}" "${RET}" |
| 132 |
head -n-0 ${HOMEDIRA}/stdout ${HOMEDIRA}/stderr |
| 133 |
printf -- "\n-------\n" |
| 134 |
for string in "[sudo] password for ${ACCTA}" "Sorry, try again" "sudo: no password was provided" "sudo: 1 incorrect password attempt"; do |
| 135 |
if ! grep -F "${string}" ${HOMEDIRA}/stderr; then |
| 136 |
printf "%s missing in stderr output\n" "${string}" |
| 137 |
printf >&2 "stdout:\n" |
| 138 |
cat >&2 ${HOMEDIRA}/stdout |
| 139 |
printf >&2 "stderr:\n" |
| 140 |
cat >&2 ${HOMEDIRA}/stderr |
| 141 |
printf >&2 "\nexit code %s\n" "${RET}" |
| 142 |
printf >&2 -- "------\n exit 1\n" |
| 143 |
exit 1 |
| 144 |
fi |
| 145 |
done |
| 146 |
|
| 147 |
printf "========= test %s\.4: account not group member, correct password\n" "${TESTNR}" |
| 148 |
printf "trying %s (no sudo membership) with correct password\n" "${ACCTB}" |
| 149 |
su - "${ACCTB}" -c "${COMMONDIR}/asuser ${PASSWD}" || RET=$? |
| 150 |
printf "%s with correct password, return value %s\n" "${ACCTB}" "${RET}" |
| 151 |
head -n-0 ${HOMEDIRB}/stdout ${HOMEDIRB}/stderr |
| 152 |
printf -- "\n-------\n" |
| 153 |
for string in "[sudo] password for ${ACCTB}: ${ACCTB} is not in the sudoers file." ; do |
| 154 |
if ! grep -q -F "${string}" ${HOMEDIRB}/stderr; then |
| 155 |
printf "%s missing in stderr output\n" "${string}" |
| 156 |
printf >&2 "stdout:\n" |
| 157 |
cat >&2 ${HOMEDIRB}/stdout |
| 158 |
printf >&2 "stderr:\n" |
| 159 |
cat >&2 ${HOMEDIRB}/stderr |
| 160 |
printf >&2 "\nexit code %s\n" "${RET}" |
| 161 |
printf >&2 -- "------\n exit 1\n" |
| 162 |
exit 1 |
| 163 |
fi |
| 164 |
done |
| 165 |
|
| 166 |
printf "test series sucessful, exit 0\n" |
| 167 |
exit 0 |
| 168 |
|