Script to add users to LDAP

I have a cluster of servers that manage user authentication and group membership in OpenLDAP. The OpenLDAP clients support adding users by importing LDIF files to create the user and set appropriate group membership. I find this process a little annoying and typically find myself sloppily managing template LDIF files. I wrote the following script to simplify the process.

  1. #!/bin/bash
  2. #
  3. # add_user.sh: Add user to LDAP
  4. # Author: Nick Sabine
  5. #
  6.  
  7. # Defaults
  8. LDAP_BASE="dc=ORG,dc=local"
  9. LDAP_ACCOUNTS_DN="ou=people,${LDAP_BASE}"
  10. LDAP_USER_GROUP="cn=user_group,ou=groups,${LDAP_BASE}"
  11. LDAP_ADMIN_GROUP="cn=admin_group,ou=groups,${LDAP_BASE}"
  12. LDAP_BIND_DN="cn=Manager,${LDAP_BASE}"
  13. USER_NAME=
  14. USER_CN=
  15. USER_SN=
  16. USER_ID=
  17. GROUP_ID=1000
  18. IS_ADMIN=
  19. LDAP_OPTIONS=
  20.  
  21. usage()
  22. {
  23. cat << EOF
  24. usage $0 options
  25.  
  26. This script creates a user in LDAP
  27.  
  28. OPTIONS
  29. -h Show this message
  30. -n Username
  31. -c User CN
  32. -s User SN
  33. -i User ID Number (default: next available number)
  34. -g Primary group ID Number (default: $GROUP_ID)
  35. -a Add user to administrator group
  36. -D LDAP Bind DN (default: $LDAP_BIND_DN)
  37. -b LDAP Base (default: $LDAP_BASE)
  38. -P LDAP Accounts DN (default: $LDAP_ACCOUNTS_DN)
  39. -U LDAP User Group DN (default: $LDAP_USER_GROUP)
  40. -A LDAP Admin Group DN (default: $LDAP_ADMIN_GROUP)
  41. -t Test. Show what would be done, but dont actually modify LDAP.
  42. EOF
  43. }
  44. error_ldap() {
  45. echo "Error: Error connecting to LDAP or uninitialized user tree"
  46. }
  47. while getopts "hn:c:s:i:g:aD:b:P:U:A:t" OPTION
  48. do
  49. case $OPTION in
  50. h)
  51. usage
  52. exit 1
  53. ;;
  54. n)
  55. USER_NAME=$OPTARG
  56. ;;
  57. c)
  58. USER_CN=$OPTARG
  59. ;;
  60. s)
  61. USER_SN=$OPTARG
  62. ;;
  63. i)
  64. USER_ID=$OPTARG
  65. ;;
  66. g)
  67. GROUP_ID=$OPTARG
  68. ;;
  69. a)
  70. IS_ADMIN=1
  71. ;;
  72. D)
  73. LDAP_BIND_DN=$OPTARG
  74. ;;
  75. b)
  76. LDAP_BASE=$OPTARG
  77. ;;
  78. P)
  79. LDAP_ACCOUNTS_DN=$OPTARG
  80. ;;
  81. U)
  82. LDAP_USER_GROUP=$OPTARG
  83. ;;
  84. A)
  85. LDAP_ADMIN_GROUP=$OPTARG
  86. ;;
  87. t)
  88. LDAP_OPTIONS+=" -n "
  89. ;;
  90. ?)
  91. usage
  92. exit
  93. ;;
  94. esac
  95. done
  96. if [ -z $USER_NAME ] ||
  97. [ -z $USER_CN ] ||
  98. [ -z $USER_SN ] ||
  99. [ -z $GROUP_ID ] ||
  100. [ -z $LDAP_BIND_DN ] ||
  101. [ -z $LDAP_ACCOUNTS_DN ] ||
  102. [ -z $LDAP_USER_GROUP ] ||
  103. [ -z $LDAP_ADMIN_GROUP ]
  104. then
  105. usage
  106. exit 1
  107. fi
  108. read -p "LDAP Manager Password: " -s LDAPPASS
  109. echo
  110. # If USER_ID not supplied, find next using ldap query
  111. if [ -z $USER_ID ]
  112. then
  113. HIGHEST_UID=$(ldapsearch -x -w "$LDAPPASS" -b "${LDAP_ACCOUNTS_DN}" -D "${LDAP_BIND_DN}" "(objectclass=posixaccount)" uidnumber | grep -e '^uid' | cut -d':' -f2 | sort | tail -1)
  114. if [ -z $HIGHEST_UID ]
  115. then
  116. error_ldap
  117. exit 1
  118. fi
  119. let USER_ID=HIGHEST_UID+1
  120. fi
  121. read -p "${USER_NAME} Initial Password: " -s USER_CLEARTEXT_PASS
  122. echo
  123. USER_PASS=$(slappasswd -h {SSHA} -s $USER_CLEARTEXT_PASS)
  124. unset USER_CLEARTEXT_PASS
  125. CHANGE_DATE=$(echo "$(date +%s) / ( 60 * 60 * 24 )" | bc)
  126. LDIF=$(cat << EOF
  127. dn: uid=${USER_NAME},${LDAP_ACCOUNTS_DN}
  128. changetype: add
  129. uid: ${USER_NAME}
  130. cn: ${USER_CN}
  131. sn: ${USER_SN}
  132. objectClass: inetOrgPerson
  133. objectClass: posixAccount
  134. objectClass: top
  135. objectClass: shadowAccount
  136. userPassword: ${USER_PASS}
  137. shadowLastChange: ${CHANGE_DATE}
  138. shadowMax: 99999
  139. shadowWarning: 7
  140. loginShell: /bin/bash
  141. uidNumber: ${USER_ID}
  142. gidNumber: ${GROUP_ID}
  143. homeDirectory: /home/${USER_NAME}
  144. dn: ${LDAP_USER_GROUP}
  145. changetype: modify
  146. add: memberuid
  147. memberuid: ${USER_NAME}
  148. EOF
  149. )
  150. if [ $IS_ADMIN ]
  151. then
  152. LDIF+=$(cat << EOF
  153. dn: ${LDAP_ADMIN_GROUP}
  154. changetype: modify
  155. add: memberuid
  156. memberuid: ${USER_NAME}
  157. EOF
  158. )
  159. fi
  160. echo "--------------------"
  161. echo "Adding ${LDIF}"
  162. echo "--------------------"
  163. echo "$LDIF" | ldapmodify -x -w "$LDAPPASS" -D "${LDAP_BIND_DN}" $LDAP_OPTIONS
  164. unset LDAPPASS
https://gist.github.com/nsabine/6599630

comments powered by Disqus