#!/bin/bash
. /usr/lib/eole/ihm.sh

# Do not use agent for connections in the container
unset SSH_AUTH_SOCK

[ "$1" = "instance" ] || exit 0
resync=0

if [ -f /etc/eole/.instance ];then
    Question_ouinon "Voulez-vous (re)synchroniser les mots de passe des utilisateurs ?" "True" "oui" "warn"
    resync="$?"
fi
if [ "$resync" = "0" ]; then
    > /var/lib/lxc/addc/rootfs/tmp/injectpwd
    SEARCH_FILTER="(&(objectClass=inetOrgPerson)(objectClass=posixAccount))"
    for user in $(ldapsearch -x $SEARCH_FILTER uid |grep "^uid" | cut -d" " -f2)
    do
        echo "pdbedit -u $user --set-nt-hash $(smbldap-usershow $user | grep sambaNTPassword | cut -d' ' -f2)" >> /var/lib/lxc/addc/rootfs/tmp/injectpwd
    done
    nb=$(wc -l < /var/lib/lxc/addc/rootfs/tmp/injectpwd)
    echo "Synchronisation en cours ($nb mots de passe)..."
    lxc-attach -n addc -- bash /tmp/injectpwd >/dev/null
    [ $? -ne 0 ] && exit 1
    rm -f /var/lib/lxc/addc/rootfs/tmp/injectpwd
    echo
fi
