add_member 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/bash
  2. username=$1
  3. group=$2
  4. current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  5. ini="$current_dir/scripts.ini"
  6. ldap_password=$(awk '/ldap_password/ {print $2}' $ini)
  7. dc=$(awk '/dc/ {print $2}' $ini)
  8. cn="$(awk '/cn/ {print $2}' $ini),$dc"
  9. group_ou="$(awk '/group_ou/ {print $2}' $ini),$dc"
  10. users_ou="$(awk '/users_ou/ {print $2}' $ini),$dc"
  11. ldiftmp="/tmp/ldapmemberadd.ldif"
  12. ldiftemplate="$current_dir/add_member.ldif"
  13. if [ ! -f $ldiftemplate ]
  14. then
  15. echo "Template file not found in $ldiftemplate"
  16. exit 0
  17. fi
  18. if [ -z "$username" ]
  19. then
  20. echo "Empty username. usage: add_member 'username' 'group'"
  21. exit 0
  22. fi
  23. if [ -z "$group" ]
  24. then
  25. echo "Empty group. usage: add_member 'username' 'group'"
  26. exit 0
  27. fi
  28. TEXT=$(cat $ldiftemplate)
  29. TEXT=${TEXT//\{LOGIN\}/$username}
  30. TEXT=${TEXT//\{GROUP\}/$group}
  31. TEXT=${TEXT//\{GROUP_OU\}/$group_ou}
  32. TEXT=${TEXT//\{PEOPLE_OU\}/$users_ou}
  33. echo "$TEXT" > $ldiftmp
  34. ldapmodify -D $cn -w $ldap_password -f $ldiftmp > /dev/null 2>&1
  35. if [ $? -gt 0 ]
  36. then
  37. echo 'Error'
  38. else
  39. echo 'Success!'
  40. fi
  41. #rm $ldiftmp