blob: 4cea812d3cbf951f6267a7025865184c1638d6c1 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Brad Topolf127e2f2013-01-22 10:17:50 -06003# lib/ldap
4# Functions to control the installation and configuration of **ldap**
5
Dean Troyercc6b4432013-04-08 15:38:03 -05006# ``lib/keystone`` calls the entry points in this order:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - install_ldap()
Brad Topolf127e2f2013-01-22 10:17:50 -06009
10# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110011_XTRACE_LDAP=$(set +o | grep xtrace)
Brad Topolf127e2f2013-01-22 10:17:50 -060012set +o xtrace
13
Dean Troyerb9e25132013-10-01 14:45:04 -050014
15LDAP_DOMAIN=${LDAP_DOMAIN:-openstack.org}
16# Make an array of domain components
17DC=(${LDAP_DOMAIN/./ })
18
19# Leftmost domain component used in top-level entry
20LDAP_BASE_DC=${DC[0]}
21
22# Build the base DN
23dn=""
24for dc in ${DC[*]}; do
25 dn="$dn,dc=$dc"
26done
27LDAP_BASE_DN=${dn#,}
28
29LDAP_MANAGER_DN="${LDAP_MANAGER_DN:-cn=Manager,${LDAP_BASE_DN}}"
30LDAP_URL=${LDAP_URL:-ldap://localhost}
31
Ralf Haferkamp704106a2013-09-12 14:24:47 +020032LDAP_SERVICE_NAME=slapd
Dean Troyercc6b4432013-04-08 15:38:03 -050033
Dean Troyerb9e25132013-10-01 14:45:04 -050034if is_ubuntu; then
35 LDAP_OLCDB_NUMBER=1
36 LDAP_ROOTPW_COMMAND=replace
37elif is_fedora; then
38 LDAP_OLCDB_NUMBER=2
39 LDAP_ROOTPW_COMMAND=add
40elif is_suse; then
41 # SUSE has slappasswd in /usr/sbin/
42 PATH=$PATH:/usr/sbin/
43 LDAP_OLCDB_NUMBER=1
44 LDAP_ROOTPW_COMMAND=add
45 LDAP_SERVICE_NAME=ldap
46fi
47
48
Dean Troyercc6b4432013-04-08 15:38:03 -050049# Functions
50# ---------
51
Dean Troyerb9e25132013-10-01 14:45:04 -050052# Perform common variable substitutions on the data files
53# _ldap_varsubst file
Ian Wienandaee18c72014-02-21 15:35:08 +110054function _ldap_varsubst {
Dean Troyerb9e25132013-10-01 14:45:04 -050055 local infile=$1
Julie Pichona3d60c82014-11-21 14:57:16 +000056 local slappass=$2
Dean Troyerb9e25132013-10-01 14:45:04 -050057 sed -e "
58 s|\${LDAP_OLCDB_NUMBER}|$LDAP_OLCDB_NUMBER|
Julie Pichona3d60c82014-11-21 14:57:16 +000059 s|\${SLAPPASS}|$slappass|
Dean Troyerb9e25132013-10-01 14:45:04 -050060 s|\${LDAP_ROOTPW_COMMAND}|$LDAP_ROOTPW_COMMAND|
61 s|\${BASE_DC}|$LDAP_BASE_DC|
62 s|\${BASE_DN}|$LDAP_BASE_DN|
63 s|\${MANAGER_DN}|$LDAP_MANAGER_DN|
64 " $infile
65}
66
67# clean_ldap() - Remove ldap server
Ian Wienandaee18c72014-02-21 15:35:08 +110068function cleanup_ldap {
Dean Troyerb9e25132013-10-01 14:45:04 -050069 uninstall_package $(get_packages ldap)
70 if is_ubuntu; then
71 uninstall_package slapd ldap-utils libslp1
72 sudo rm -rf /etc/ldap/ldap.conf /var/lib/ldap
73 elif is_fedora; then
74 sudo rm -rf /etc/openldap /var/lib/ldap
75 elif is_suse; then
76 sudo rm -rf /var/lib/ldap
77 fi
78}
79
80# init_ldap
81# init_ldap() - Initialize databases, etc.
Ian Wienandaee18c72014-02-21 15:35:08 +110082function init_ldap {
Dean Troyerb9e25132013-10-01 14:45:04 -050083 local keystone_ldif
84
Ian Wienandada886d2015-10-07 14:06:26 +110085 local tmp_ldap_dir
86 tmp_ldap_dir=$(mktemp -d -t ldap.$$.XXXXXXXXXX)
Dean Troyerb9e25132013-10-01 14:45:04 -050087
88 # Remove data but not schemas
89 clear_ldap_state
90
91 # Add our top level ldap nodes
92 if ldapsearch -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -b "$LDAP_BASE_DN" | grep -q "Success"; then
93 printf "LDAP already configured for $LDAP_BASE_DC\n"
94 else
95 printf "Configuring LDAP for $LDAP_BASE_DC\n"
96 # If BASE_DN is changed, the user may override the default file
97 if [[ -r $FILES/ldap/${LDAP_BASE_DC}.ldif.in ]]; then
Dean Troyeref66a772014-07-25 14:45:34 -050098 local keystone_ldif=${LDAP_BASE_DC}.ldif
Dean Troyerb9e25132013-10-01 14:45:04 -050099 else
Dean Troyeref66a772014-07-25 14:45:34 -0500100 local keystone_ldif=keystone.ldif
Dean Troyerb9e25132013-10-01 14:45:04 -0500101 fi
Dean Troyeref66a772014-07-25 14:45:34 -0500102 _ldap_varsubst $FILES/ldap/${keystone_ldif}.in >$tmp_ldap_dir/${keystone_ldif}
103 if [[ -r $tmp_ldap_dir/${keystone_ldif} ]]; then
104 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 -0500105 fi
106 fi
107
Dean Troyeref66a772014-07-25 14:45:34 -0500108 rm -rf $tmp_ldap_dir
Dean Troyerb9e25132013-10-01 14:45:04 -0500109}
110
Brad Topolf127e2f2013-01-22 10:17:50 -0600111# install_ldap
112# install_ldap() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100113function install_ldap {
Brad Topolf127e2f2013-01-22 10:17:50 -0600114 echo "Installing LDAP inside function"
Brad Topolf127e2f2013-01-22 10:17:50 -0600115 echo "os_VENDOR is $os_VENDOR"
Dean Troyerb9e25132013-10-01 14:45:04 -0500116
Ian Wienandada886d2015-10-07 14:06:26 +1100117 local tmp_ldap_dir
118 tmp_ldap_dir=$(mktemp -d -t ldap.$$.XXXXXXXXXX)
Dean Troyerb9e25132013-10-01 14:45:04 -0500119
120 printf "installing OpenLDAP"
Brad Topolf127e2f2013-01-22 10:17:50 -0600121 if is_ubuntu; then
Dean Troyerb9e25132013-10-01 14:45:04 -0500122 # Ubuntu automatically starts LDAP so no need to call start_ldap()
123 :
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200124 elif is_fedora; then
Brad Topolf127e2f2013-01-22 10:17:50 -0600125 start_ldap
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200126 elif is_suse; then
Dean Troyeref66a772014-07-25 14:45:34 -0500127 _ldap_varsubst $FILES/ldap/suse-base-config.ldif.in >$tmp_ldap_dir/suse-base-config.ldif
128 sudo slapadd -F /etc/openldap/slapd.d/ -bcn=config -l $tmp_ldap_dir/suse-base-config.ldif
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200129 sudo sed -i '/^OPENLDAP_START_LDAPI=/s/"no"/"yes"/g' /etc/sysconfig/openldap
130 start_ldap
Brad Topolf127e2f2013-01-22 10:17:50 -0600131 fi
132
Dean Troyerb9e25132013-10-01 14:45:04 -0500133 echo "LDAP_PASSWORD is $LDAP_PASSWORD"
Ian Wienandada886d2015-10-07 14:06:26 +1100134 local slappass
135 slappass=$(slappasswd -s $LDAP_PASSWORD)
Dean Troyeref66a772014-07-25 14:45:34 -0500136 printf "LDAP secret is $slappass\n"
Brad Topolf127e2f2013-01-22 10:17:50 -0600137
Dean Troyerb9e25132013-10-01 14:45:04 -0500138 # Create manager.ldif and add to olcdb
Julie Pichona3d60c82014-11-21 14:57:16 +0000139 _ldap_varsubst $FILES/ldap/manager.ldif.in $slappass >$tmp_ldap_dir/manager.ldif
Dean Troyeref66a772014-07-25 14:45:34 -0500140 sudo ldapmodify -Y EXTERNAL -H ldapi:/// -f $tmp_ldap_dir/manager.ldif
Brad Topolf127e2f2013-01-22 10:17:50 -0600141
Brad Topol0c2c3fc2013-03-19 03:01:30 -0500142 # On fedora we need to manually add cosine and inetorgperson schemas
Dean Troyerb9e25132013-10-01 14:45:04 -0500143 if is_fedora; then
Brad Topol0c2c3fc2013-03-19 03:01:30 -0500144 sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
145 sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
146 fi
147
Dean Troyeref66a772014-07-25 14:45:34 -0500148 rm -rf $tmp_ldap_dir
Brad Topolf127e2f2013-01-22 10:17:50 -0600149}
150
151# start_ldap() - Start LDAP
Ian Wienandaee18c72014-02-21 15:35:08 +1100152function start_ldap {
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200153 sudo service $LDAP_SERVICE_NAME restart
Brad Topolf127e2f2013-01-22 10:17:50 -0600154}
155
Brad Topolf127e2f2013-01-22 10:17:50 -0600156# stop_ldap() - Stop LDAP
Ian Wienandaee18c72014-02-21 15:35:08 +1100157function stop_ldap {
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200158 sudo service $LDAP_SERVICE_NAME stop
Brad Topolf127e2f2013-01-22 10:17:50 -0600159}
160
161# clear_ldap_state() - Clear LDAP State
Ian Wienandaee18c72014-02-21 15:35:08 +1100162function clear_ldap_state {
Dean Troyerb44a8ef2014-03-06 11:25:04 -0600163 ldapdelete -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -r "$LDAP_BASE_DN" || :
Brad Topolf127e2f2013-01-22 10:17:50 -0600164}
165
166# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100167$_XTRACE_LDAP
Sean Dague584d90e2013-03-29 14:34:53 -0400168
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100169# Tell emacs to use shell-script-mode
170## Local variables:
171## mode: shell-script
172## End: