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