summaryrefslogtreecommitdiff
path: root/ldap/mkldapdb
blob: fbbdb0914fe46f29e4a3d4190a3fb5e0ed73ad5d (plain)
  1. #!/bin/sh
  2. #
  3. # /etc/local-COMMON/ldap/mkldapdb
  4. # Copyright 2008 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # Setup LDAP database from skeleton files
  7. set -e
  8. umask 066
  9. PRG=$(basename "$0")
  10. TEMP=$(getopt -s sh -o b:e:d:fh -l basedn:,enable:,disable:,force,help -n "$PRG" -- "$@")
  11. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  12. eval set -- "$TEMP"
  13. getbasedn() {
  14. grep '^BASE\b' /etc/ldap/ldap.conf | sed -e 's/^BASE[[:space:]]\+//' -e 's/,[[:space:]]\+/,/g'
  15. }
  16. getdnsdomain() {
  17. dnsdomainname
  18. }
  19. getorgname() {
  20. if [ -r /etc/local-ORG/orgname ]; then
  21. head -n 1 /etc/local-ORG/orgname
  22. fi
  23. }
  24. # config defaults as of slapd 2.4.10-3
  25. backend="hdb"
  26. # extension default states (enabled/disabled)
  27. cipux=1
  28. horde=
  29. # strings above, and either functions above or strings right below,
  30. # can be overrided locally through this config file
  31. if [ -f /etc/local/mkldapdb.cfg ]; then
  32. . /etc/local/mkldapdb.cfg
  33. fi
  34. basedn="${basedn:-$(getbasedn)}"
  35. dnsdomain="${dnsdomain:-$(getdnsdomain)}"
  36. orgname="${orgname:-$(getorgname)}"
  37. showhelp() {
  38. cat <<EOF
  39. Usage: $PRG [opts...] [PHASE [PHASE...]]
  40. Setup LDAP database from skeleton files
  41. Options:
  42. -b, --basedn LDAP Base DN (Distinguished Name) to use
  43. (default: ${basedn})
  44. -e, --enable Include this optional extension
  45. -d, --disable Exclude this optional extension
  46. -t, --tempdir Skip prep phase and use content of provided dir
  47. -c, --config Include config phase
  48. -i, --init Include init phase
  49. -f, --force Update without asking for confirmation
  50. -h, --help Show this help text
  51. The following extensions are available:
  52. cipux CipUX admin framework ${cipux:+(enabled by default)}
  53. horde HORDE web-app framework ${horde:+(enabled by default)}
  54. The following phases are possible:
  55. prep Assemble slapd.conf and LDIF files with DIT parts
  56. config Add/update LDAP server configuration file
  57. init Purge any existing ldap data and initialize new core DIT
  58. main Add general DIT for use with POSIX accounts
  59. mainpw Apply/Change main admin password
  60. opt Add optional DIT extensions
  61. optpw Apply/Change passwords for accounts of optional extensions
  62. When no phases are supplied, all but config and init are applied
  63. Examples:
  64. $PRG
  65. $PRG --basedn dc=example,dc=org --enable horde prep
  66. EOF
  67. }
  68. exit1() {
  69. echo >&2 "Error: $1"
  70. echo >&2 "Exiting..."
  71. exit 1
  72. }
  73. while true ; do
  74. case "$1" in
  75. -b|--basedn) basedn="$2"; shift 2;;
  76. -e|--enable-extension)
  77. case "$2" in
  78. cipux|horde) eval "$2=1";;
  79. *) exit1 "Unknown extension \"$2\""
  80. esac
  81. shift 2
  82. ;;
  83. -d|--disable-extension)
  84. case "$2" in
  85. cipux|horde) eval "$2=";;
  86. *) exit1 "Unknown extension \"$2\""
  87. esac
  88. shift 2
  89. ;;
  90. -f|--force) force="1"; shift;;
  91. -h|--help) showhelp; exit 0;;
  92. --) shift; break;;
  93. *) exit1 "Internal error!";;
  94. esac
  95. done
  96. # Ensure all required values are properly resolved
  97. for var in basedn dnsdomain orgname backend; do
  98. if [ -z "`eval echo '$'$var`" ]; then
  99. exit1 "Required variable '$var' missing. Exiting...!"
  100. fi
  101. done
  102. # concatenate files with an additional newline in between
  103. spacecat() {
  104. perl -e 'foreach (@ARGV) {print "\n" if $n; $n++; open (FH, $_); print while(<FH>); close FH;}' "$@"
  105. }
  106. #TODO: Somehow lookup id directly instead, as getent might be slow with
  107. # thousands of entries, and some NSS mechanisms drop at some limit
  108. # i.e. openldap by default return only first 500 entries
  109. nextfreeid() {
  110. type="$1"
  111. id="$2"
  112. max="$3"
  113. case $type in
  114. uid) column="3";;
  115. gid) column="4";;
  116. esac
  117. while getent passwd | awk -F: "{ print \$$column }" | grep -Fqx "$id"; do
  118. id=$(($id + 1))
  119. [ -z "$max" ] || [ "$id" -lt "$max" ] || return 1
  120. done
  121. echo "$id"
  122. }
  123. masterdir=/etc/local-COMMON/ldap
  124. tempdir=`mktemp -dt slapd.XXXXXX`
  125. snippets="$(run-parts --list --regex '^[0-9]+_[a-z0-9-]+\.conf\.in$' "$masterdir/slapd.conf.d")"
  126. spacecat $snippets | sed >>"$tempdir/slapd.conf" \
  127. -e "s/@BACKEND@/$backend/g" \
  128. -e "s/@SUFFIX@/$basedn/g" \
  129. -e "s/@ADMIN@/cn=admin,$basedn/g"
  130. # TODO: Better separate core from normal ldif files than "below 100"...
  131. file=99
  132. for section in core base cipux horde; do
  133. sed <"$masterdir/db/$section.ldif.in" >"$tempdir/${file}_$section.ldif" \
  134. -e "s/@SUFFIX@/$basedn/g" \
  135. -e "s/@DOMAIN@/$dnsdomain/g" \
  136. -e "s/@ORG@/$orgname/g"
  137. file=$(($file + 1))
  138. done
  139. # FIXME: create cipuxadm in addition to below roles!
  140. # FIXME: fix apply passwords for roles in a sane way!
  141. uid=10100
  142. gid=10100
  143. file=200
  144. for role in admin professor assistant pupil student tutor teacher lecturer; do
  145. uid="$(nextfreeid uid "$uid")"
  146. gid="$(nextfreeid gid "$gid")"
  147. snippets="$masterdir/db/cipux_rolegroup.ldif.in $masterdir/db/cipux_roleuser.ldif.in"
  148. spacecat $snippets | sed >"$tempdir/${file}_$role.ldif" \
  149. -e "s/@SUFFIX@/$basedn/g" \
  150. -e "s/@ROLE@/$role/g" \
  151. -e "s/@UID@/$uid/g" \
  152. -e "s/@GID@/$gid/g" \
  153. -e "s/@DOMAIN@/$dnsdomain/g" \
  154. -e "s/@ORG@/$orgname/g"
  155. uid=$(($uid + 1))
  156. gid=$(($gid + 1))
  157. file=$(($file + 1))
  158. done
  159. file=300
  160. for db in passwd group; do
  161. getent $db >"$tempdir/$db.dump"
  162. ( cd /usr/share/migrationtools && ./migrate_$db.pl "$tempdir/$db.dump" >"$tempdir/${file}_$db.ldif" )
  163. file=$(($file + 1))
  164. done
  165. # FIXME: Set core password using slappasswd or similar (no cleartext password!)
  166. #invoke-rc.d slapd stop
  167. #slapadd -l "$tempdir/99_core.ldif"
  168. #invoke-rc.d slapd start
  169. #ldappasswd -x -h localhost -D "cn=admin,$basedn" -S -w supersecretpassword "cn=admin,$basedn"
  170. for file in $(run-parts --list --regex '^1[0-9]{2}_[a-z0-9-]+\.ldif' "$tempdir"); do
  171. ldapadd -x -h localhost -D "cn=admin,$basedn" -f "$file" -W
  172. done
  173. for role in cipux horde; do
  174. echo "Securing $role..."
  175. ldappasswd -x -h localhost -D "cn=admin,$basedn" -S -W "cn=$role,ou=Entities,ou=Access Control,$basedn"
  176. done
  177. # FIXME: Write addmember(), that create group as needed
  178. #ldapmodify -x -h localhost -D "cn=admin,$basedn" -W <<EOF
  179. #dn: cn=DSA,ou=Administrators,ou=Groups,ou=Access Control,$basedn
  180. #changetype: modify
  181. #add: uniqueMember
  182. #uniqueMember: cn=cipux,ou=Entities,ou=Access Control,$basedn
  183. #EOF
  184. ldapadd -x -h localhost -D "cn=admin,$basedn" -W <<EOF
  185. dn: cn=DSA,ou=Administrators,ou=Groups,ou=Access Control,$basedn
  186. objectClass: groupOfUniqueNames
  187. cn: DSA
  188. description: Directory System Agent administrators
  189. uniqueMember: cn=cipux,ou=Entities,ou=Access Control,$basedn
  190. EOF
  191. ldapadd -x -h localhost -D "cn=admin,$basedn" -W <<EOF
  192. dn: cn=SAM,ou=Administrators,ou=Groups,ou=Access Control,$basedn
  193. objectClass: groupOfUniqueNames
  194. cn: SAM
  195. description: Samba and NSS services administrators
  196. uniqueMember: cn=horde,ou=Entities,ou=Access Control,$basedn
  197. EOF
  198. # TODO: Add "uid=cifsdc,ou=Entities,ou=Access Control,@SUFFIX@" to group
  199. # "cn=SAM,ou=Administrators,ou=Access Control,@SUFFIX@" for samba
  200. for file in $(run-parts --list --regex '^2[0-9]{2}_[a-z0-9-]+\.ldif' "$tempdir"); do
  201. ldapadd -x -h localhost -D "cn=admin,$basedn" -f "$file" -W
  202. done
  203. # FIXME: Check (and maybe correct) basedn from migrationtools-generated ldifs
  204. #for file in $(run-parts --list --regex '^3[0-9]{2}_[a-z0-9-]+\.ldif' "$tempdir"); do
  205. # ldapadd -x -h localhost -D "cn=admin,$basedn" -f "$file" -W
  206. #done