summaryrefslogtreecommitdiff
path: root/ldap/mkldapdb
blob: 77cb6d5b1ec0dbe5950f7de376b808e6bdbfe2dc (plain)
  1. #!/bin/sh
  2. set -e
  3. umask 066
  4. # Resolve some defaults from other system config
  5. basedn="`grep '^BASE\b' /etc/ldap/ldap.conf | sed -e 's/^BASE[[:space:]]\+//' -e 's/,[[:space:]]\+/,/g'`"
  6. dnsdomain="`dnsdomainname`"
  7. orgname=""
  8. if [ -r /etc/local-ORG/orgname ]; then
  9. orgname="$(head -n 1 /etc/local-ORG/orgname)"
  10. fi
  11. # config defaults as of slapd 2.4.10-3
  12. backend="hdb"
  13. # Ensure all required values are properly resolved
  14. for var in basedn dnsdomain orgname backend; do
  15. if [ -z "`eval echo '$'$var`" ]; then
  16. echo 1>&2 "ERROR: Required variable '$var' missing. Exiting...!"
  17. exit 1
  18. fi
  19. done
  20. # concatenate files with an additional newline in between
  21. spacecat() {
  22. perl -e 'foreach (@ARGV) {print "\n" if $n; $n++; open (FH, $_); print while(<FH>); close FH;}' "$@"
  23. }
  24. #TODO: Somehow lookup id directly instead, as getent might be slow with
  25. # thousands of entries, and some NSS mechanisms drop at some limit
  26. # i.e. openldap by default return only first 500 entries
  27. nextfreeid() {
  28. type="$1"
  29. id="$2"
  30. max="$3"
  31. case $type in
  32. uid) column="3";;
  33. gid) column="4";;
  34. esac
  35. while getent passwd | awk -F: "{ print \$$column }" | grep -Fqx "$id"; do
  36. id=$(($id + 1))
  37. [ -z "$max" ] || [ "$id" -lt "$max" ] || return 1
  38. done
  39. echo "$id"
  40. }
  41. masterdir=/etc/local-COMMON/ldap
  42. tempdir=`mktemp -dt slapd.XXXXXX`
  43. snippets="$(run-parts --list --regex '^[0-9]+_[a-z0-9-]+\.conf\.in$' "$masterdir/slapd.conf.d")"
  44. spacecat $snippets | sed >>"$tempdir/slapd.conf" \
  45. -e "s/@BACKEND@/$backend/g" \
  46. -e "s/@SUFFIX@/$basedn/g" \
  47. -e "s/@ADMIN@/cn=admin,$basedn/g"
  48. # TODO: Better separate core from normal lif files than "below 100"...
  49. file=99
  50. for section in core base cipux horde; do
  51. sed <"$masterdir/db/$section.ldif.in" >"$tempdir/${file}_$section.ldif" \
  52. -e "s/@SUFFIX@/$basedn/g" \
  53. -e "s/@DOMAIN@/$dnsdomain/g" \
  54. -e "s/@ORG@/$orgname/g"
  55. file=$(($file + 1))
  56. done
  57. # FIXME: create cipuxadm in addition to below roles!
  58. # FIXME: fix apply passwords for roles in a sane way!
  59. uid=10100
  60. gid=10100
  61. file=200
  62. for role in admin professor assistant pupil student tutor teacher lecturer; do
  63. uid="$(nextfreeid uid "$uid")"
  64. gid="$(nextfreeid gid "$gid")"
  65. snippets="$masterdir/db/cipux_rolegroup.ldif.in $masterdir/db/cipux_roleuser.ldif.in"
  66. spacecat $snippets | sed >"$tempdir/${file}_$role.ldif" \
  67. -e "s/@SUFFIX@/$basedn/g" \
  68. -e "s/@ROLE@/$role/g" \
  69. -e "s/@UID@/$uid/g" \
  70. -e "s/@GID@/$gid/g" \
  71. -e "s/@DOMAIN@/$dnsdomain/g" \
  72. -e "s/@ORG@/$orgname/g"
  73. uid=$(($uid + 1))
  74. gid=$(($gid + 1))
  75. file=$(($file + 1))
  76. done
  77. file=300
  78. for db in passwd group; do
  79. getent $db >"$tempdir/$db.dump"
  80. ( cd /usr/share/migrationtools && ./migrate_$db.pl "$tempdir/$db.dump" >"$tempdir/${file}_$db.ldif" )
  81. file=$(($file + 1))
  82. done
  83. # FIXME: Set core password using slappasswd or similar (no cleartext password!)
  84. #invoke-rc.d slapd stop
  85. #slapadd -l "$tempdir/99_core.ldif"
  86. #invoke-rc.d slapd start
  87. #ldappasswd -x -h localhost -D "cn=admin,$basedn" -S -w supersecretpassword "cn=admin,$basedn"
  88. for file in $(run-parts --list --regex '^1[0-9]{2}_[a-z0-9-]+\.ldif' "$tempdir"); do
  89. ldapadd -x -h localhost -D "cn=admin,$basedn" -f "$file" -W
  90. done
  91. for role in cipux horde; do
  92. echo "Securing $role..."
  93. ldappasswd -x -h localhost -D "cn=admin,$basedn" -S -W "cn=$role,ou=Entities,ou=Access Control,$basedn"
  94. done
  95. # FIXME: Write addmember(), that create group as needed
  96. #ldapmodify -x -h localhost -D "cn=admin,$basedn" -W <<EOF
  97. #dn: cn=DSA,ou=Administrators,ou=Groups,ou=Access Control,$basedn
  98. #changetype: modify
  99. #add: uniqueMember
  100. #uniqueMember: cn=cipux,ou=Entities,ou=Access Control,$basedn
  101. #EOF
  102. ldapadd -x -h localhost -D "cn=admin,$basedn" -W <<EOF
  103. dn: cn=DSA,ou=Administrators,ou=Groups,ou=Access Control,$basedn
  104. objectClass: groupOfUniqueNames
  105. cn: DSA
  106. description: Directory System Agent administrators
  107. uniqueMember: cn=cipux,ou=Entities,ou=Access Control,$basedn
  108. EOF
  109. ldapadd -x -h localhost -D "cn=admin,$basedn" -W <<EOF
  110. dn: cn=SAM,ou=Administrators,ou=Groups,ou=Access Control,$basedn
  111. objectClass: groupOfUniqueNames
  112. cn: SAM
  113. description: Samba and NSS services administrators
  114. uniqueMember: cn=horde,ou=Entities,ou=Access Control,$basedn
  115. EOF
  116. # TODO: Add "uid=cifsdc,ou=Entities,ou=Access Control,@SUFFIX@" to group
  117. # "cn=SAM,ou=Administrators,ou=Access Control,@SUFFIX@" for samba
  118. for file in $(run-parts --list --regex '^2[0-9]{2}_[a-z0-9-]+\.ldif' "$tempdir"); do
  119. ldapadd -x -h localhost -D "cn=admin,$basedn" -f "$file" -W
  120. done
  121. # FIXME: Check (and maybe correct) basedn from migrationtools-generated ldifs
  122. #for file in $(run-parts --list --regex '^3[0-9]{2}_[a-z0-9-]+\.ldif' "$tempdir"); do
  123. # ldapadd -x -h localhost -D "cn=admin,$basedn" -f "$file" -W
  124. #done