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