#!/bin/bash

eval "$(ucr shell ldap/base kerberos/realm)"

hosts=$(ldapsearch -xLLL -H ldapi:// '(&(cn=*)(associatedDomain=*)(objectClass=univentionDomainController))' cn associatedDomain | ldapsearch-wrapper |  sed -ne 's|cn: ||p' -ne 's|associatedDomain: |.|p' | tr '\n' ' ' | sed 's| \.|\.|g')

for h in $hosts; do
	principal=$(ldapsearch -xLLL -H ldapi:// -b cn=kerberos,${ldap_base} krb5PrincipalName="ldap/${h}@${kerberos_realm}" dn | ldapsearch-wrapper | sed -ne 's|dn: ||')
	if [ -z "$principal" ]; then
		 kadmin -l add --random-key --use-defaults ldap/${h}
	fi
done

