createperson 1013 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/bash -e
  2. . /etc/ldap-utils.conf
  3. . /usr/local/lib/ldap-utils/funcs
  4. usage() {
  5. echo "Usage : $(basename $0) -u <uid> -n <First name> -s <Last Name> [-p] [-g group1,group2,...]"
  6. }
  7. trap usage ERR
  8. while getopts 'u:n:s:p?g:?' opt; do
  9. case "$opt" in
  10. u)
  11. uid="$OPTARG"
  12. ;;
  13. n)
  14. gn="$OPTARG"
  15. ;;
  16. s)
  17. sn="$OPTARG"
  18. ;;
  19. p)
  20. posix="true"
  21. ;;
  22. g)
  23. grouplist=$( echo $OPTARG | tr ',' ' ')
  24. ;;
  25. esac
  26. done
  27. clearpw=$(pwgen 50 1)
  28. pw=$(/usr/sbin/slappasswd -o module-load=pw-sha2 -h '{SSHA512}' -s "$clearpw")
  29. if user_exists $uid; then
  30. echo "User $uid already exists."
  31. exit 1
  32. fi
  33. user="dn: uid=$uid,$PEOPLEDN
  34. objectClass: top
  35. objectClass: inetOrgPerson
  36. sn: $sn
  37. gn: $gn
  38. cn: $gn $sn
  39. mail: $uid@$DOMAIN
  40. userPassword: $pw"
  41. echo "$user" | ldapadd $LDAPOPTS
  42. [ -n "$posix" ] && echo posix && person2posix $uid
  43. [ -n "$grouplist" ] && echo groups && addtogroups $uid $grouplist
  44. echo "User $uid created, here is their password :"
  45. echo "$clearpw"