blob: 2a3ba64efbd7f7844866e970fe822075a9258c1a [file] [log] [blame]
Brad Topolf127e2f2013-01-22 10:17:50 -06001# lib/ldap
2# Functions to control the installation and configuration of **ldap**
3
Dean Troyercc6b4432013-04-08 15:38:03 -05004# ``lib/keystone`` calls the entry points in this order:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01005#
6# - install_ldap()
Brad Topolf127e2f2013-01-22 10:17:50 -06007
8# Save trace setting
9XTRACE=$(set +o | grep xtrace)
10set +o xtrace
11
Dean Troyerb9e25132013-10-01 14:45:04 -050012
13LDAP_DOMAIN=${LDAP_DOMAIN:-openstack.org}
14# Make an array of domain components
15DC=(${LDAP_DOMAIN/./ })
16
17# Leftmost domain component used in top-level entry
18LDAP_BASE_DC=${DC[0]}
19
20# Build the base DN
21dn=""
22for dc in ${DC[*]}; do
23 dn="$dn,dc=$dc"
24done
25LDAP_BASE_DN=${dn#,}
26
27LDAP_MANAGER_DN="${LDAP_MANAGER_DN:-cn=Manager,${LDAP_BASE_DN}}"
28LDAP_URL=${LDAP_URL:-ldap://localhost}
29
Ralf Haferkamp704106a2013-09-12 14:24:47 +020030LDAP_SERVICE_NAME=slapd
Dean Troyercc6b4432013-04-08 15:38:03 -050031
Dean Troyerb9e25132013-10-01 14:45:04 -050032if is_ubuntu; then
33 LDAP_OLCDB_NUMBER=1
34 LDAP_ROOTPW_COMMAND=replace
35elif is_fedora; then
36 LDAP_OLCDB_NUMBER=2
37 LDAP_ROOTPW_COMMAND=add
38elif is_suse; then
39 # SUSE has slappasswd in /usr/sbin/
40 PATH=$PATH:/usr/sbin/
41 LDAP_OLCDB_NUMBER=1
42 LDAP_ROOTPW_COMMAND=add
43 LDAP_SERVICE_NAME=ldap
44fi
45
46
Dean Troyercc6b4432013-04-08 15:38:03 -050047# Functions
48# ---------
49
Dean Troyerb9e25132013-10-01 14:45:04 -050050# Perform common variable substitutions on the data files
51# _ldap_varsubst file
Ian Wienandaee18c72014-02-21 15:35:08 +110052function _ldap_varsubst {
Dean Troyerb9e25132013-10-01 14:45:04 -050053 local infile=$1
Julie Pichona3d60c82014-11-21 14:57:16 +000054 local slappass=$2
Dean Troyerb9e25132013-10-01 14:45:04 -050055 sed -e "
56 s|\${LDAP_OLCDB_NUMBER}|$LDAP_OLCDB_NUMBER|
Julie Pichona3d60c82014-11-21 14:57:16 +000057 s|\${SLAPPASS}|$slappass|
Dean Troyerb9e25132013-10-01 14:45:04 -050058 s|\${LDAP_ROOTPW_COMMAND}|$LDAP_ROOTPW_COMMAND|
59 s|\${BASE_DC}|$LDAP_BASE_DC|
60 s|\${BASE_DN}|$LDAP_BASE_DN|
61 s|\${MANAGER_DN}|$LDAP_MANAGER_DN|
62 " $infile
63}
64
65# clean_ldap() - Remove ldap server
Ian Wienandaee18c72014-02-21 15:35:08 +110066function cleanup_ldap {
Dean Troyerb9e25132013-10-01 14:45:04 -050067 uninstall_package $(get_packages ldap)
68 if is_ubuntu; then
69 uninstall_package slapd ldap-utils libslp1
70 sudo rm -rf /etc/ldap/ldap.conf /var/lib/ldap
71 elif is_fedora; then
72 sudo rm -rf /etc/openldap /var/lib/ldap
73 elif is_suse; then
74 sudo rm -rf /var/lib/ldap
75 fi
76}
77
78# init_ldap
79# init_ldap() - Initialize databases, etc.
Ian Wienandaee18c72014-02-21 15:35:08 +110080function init_ldap {
Dean Troyerb9e25132013-10-01 14:45:04 -050081 local keystone_ldif
82
Dean Troyeref66a772014-07-25 14:45:34 -050083 local tmp_ldap_dir=$(mktemp -d -t ldap.$$.XXXXXXXXXX)
Dean Troyerb9e25132013-10-01 14:45:04 -050084
85 # Remove data but not schemas
86 clear_ldap_state
87
88 # Add our top level ldap nodes
89 if ldapsearch -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -b "$LDAP_BASE_DN" | grep -q "Success"; then
90 printf "LDAP already configured for $LDAP_BASE_DC\n"
91 else
92 printf "Configuring LDAP for $LDAP_BASE_DC\n"
93 # If BASE_DN is changed, the user may override the default file
94 if [[ -r $FILES/ldap/${LDAP_BASE_DC}.ldif.in ]]; then
Dean Troyeref66a772014-07-25 14:45:34 -050095 local keystone_ldif=${LDAP_BASE_DC}.ldif
Dean Troyerb9e25132013-10-01 14:45:04 -050096 else
Dean Troyeref66a772014-07-25 14:45:34 -050097 local keystone_ldif=keystone.ldif
Dean Troyerb9e25132013-10-01 14:45:04 -050098 fi
Dean Troyeref66a772014-07-25 14:45:34 -050099 _ldap_varsubst $FILES/ldap/${keystone_ldif}.in >$tmp_ldap_dir/${keystone_ldif}
100 if [[ -r $tmp_ldap_dir/${keystone_ldif} ]]; then
101 ldapadd -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -c -f $tmp_ldap_dir/${keystone_ldif}
Dean Troyerb9e25132013-10-01 14:45:04 -0500102 fi
103 fi
104
Dean Troyeref66a772014-07-25 14:45:34 -0500105 rm -rf $tmp_ldap_dir
Dean Troyerb9e25132013-10-01 14:45:04 -0500106}
107
Brad Topolf127e2f2013-01-22 10:17:50 -0600108# install_ldap
109# install_ldap() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100110function install_ldap {
Brad Topolf127e2f2013-01-22 10:17:50 -0600111 echo "Installing LDAP inside function"
Brad Topolf127e2f2013-01-22 10:17:50 -0600112 echo "os_VENDOR is $os_VENDOR"
Dean Troyerb9e25132013-10-01 14:45:04 -0500113
Dean Troyeref66a772014-07-25 14:45:34 -0500114 local tmp_ldap_dir=$(mktemp -d -t ldap.$$.XXXXXXXXXX)
Dean Troyerb9e25132013-10-01 14:45:04 -0500115
116 printf "installing OpenLDAP"
Brad Topolf127e2f2013-01-22 10:17:50 -0600117 if is_ubuntu; then
Dean Troyerb9e25132013-10-01 14:45:04 -0500118 # Ubuntu automatically starts LDAP so no need to call start_ldap()
119 :
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200120 elif is_fedora; then
Brad Topolf127e2f2013-01-22 10:17:50 -0600121 start_ldap
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200122 elif is_suse; then
Dean Troyeref66a772014-07-25 14:45:34 -0500123 _ldap_varsubst $FILES/ldap/suse-base-config.ldif.in >$tmp_ldap_dir/suse-base-config.ldif
124 sudo slapadd -F /etc/openldap/slapd.d/ -bcn=config -l $tmp_ldap_dir/suse-base-config.ldif
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200125 sudo sed -i '/^OPENLDAP_START_LDAPI=/s/"no"/"yes"/g' /etc/sysconfig/openldap
126 start_ldap
Brad Topolf127e2f2013-01-22 10:17:50 -0600127 fi
128
Dean Troyerb9e25132013-10-01 14:45:04 -0500129 echo "LDAP_PASSWORD is $LDAP_PASSWORD"
Dean Troyeref66a772014-07-25 14:45:34 -0500130 local slappass=$(slappasswd -s $LDAP_PASSWORD)
131 printf "LDAP secret is $slappass\n"
Brad Topolf127e2f2013-01-22 10:17:50 -0600132
Dean Troyerb9e25132013-10-01 14:45:04 -0500133 # Create manager.ldif and add to olcdb
Julie Pichona3d60c82014-11-21 14:57:16 +0000134 _ldap_varsubst $FILES/ldap/manager.ldif.in $slappass >$tmp_ldap_dir/manager.ldif
Dean Troyeref66a772014-07-25 14:45:34 -0500135 sudo ldapmodify -Y EXTERNAL -H ldapi:/// -f $tmp_ldap_dir/manager.ldif
Brad Topolf127e2f2013-01-22 10:17:50 -0600136
Brad Topol0c2c3fc2013-03-19 03:01:30 -0500137 # On fedora we need to manually add cosine and inetorgperson schemas
Dean Troyerb9e25132013-10-01 14:45:04 -0500138 if is_fedora; then
Brad Topol0c2c3fc2013-03-19 03:01:30 -0500139 sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
140 sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
141 fi
142
Julie Pichonac937bc2014-09-29 04:55:21 +0100143 pip_install ldappool
144
Dean Troyeref66a772014-07-25 14:45:34 -0500145 rm -rf $tmp_ldap_dir
Brad Topolf127e2f2013-01-22 10:17:50 -0600146}
147
148# start_ldap() - Start LDAP
Ian Wienandaee18c72014-02-21 15:35:08 +1100149function start_ldap {
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200150 sudo service $LDAP_SERVICE_NAME restart
Brad Topolf127e2f2013-01-22 10:17:50 -0600151}
152
Brad Topolf127e2f2013-01-22 10:17:50 -0600153# stop_ldap() - Stop LDAP
Ian Wienandaee18c72014-02-21 15:35:08 +1100154function stop_ldap {
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200155 sudo service $LDAP_SERVICE_NAME stop
Brad Topolf127e2f2013-01-22 10:17:50 -0600156}
157
158# clear_ldap_state() - Clear LDAP State
Ian Wienandaee18c72014-02-21 15:35:08 +1100159function clear_ldap_state {
Dean Troyerb44a8ef2014-03-06 11:25:04 -0600160 ldapdelete -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -r "$LDAP_BASE_DN" || :
Brad Topolf127e2f2013-01-22 10:17:50 -0600161}
162
163# Restore xtrace
164$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400165
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100166# Tell emacs to use shell-script-mode
167## Local variables:
168## mode: shell-script
169## End: