blob: b0195db2580df130357808c72b4d63eb3a785bcc [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
Grzegorz Grasza26f81492021-08-16 10:36:03 +020036 LDAP_OLCDB_TYPE=mdb
Dean Troyerb9e25132013-10-01 14:45:04 -050037 LDAP_ROOTPW_COMMAND=replace
38elif is_fedora; then
39 LDAP_OLCDB_NUMBER=2
Grzegorz Grasza26f81492021-08-16 10:36:03 +020040 LDAP_OLCDB_TYPE=hdb
Dean Troyerb9e25132013-10-01 14:45:04 -050041 LDAP_ROOTPW_COMMAND=add
Dean Troyerb9e25132013-10-01 14:45:04 -050042fi
43
44
Dean Troyercc6b4432013-04-08 15:38:03 -050045# Functions
46# ---------
47
Dean Troyerb9e25132013-10-01 14:45:04 -050048# Perform common variable substitutions on the data files
49# _ldap_varsubst file
Ian Wienandaee18c72014-02-21 15:35:08 +110050function _ldap_varsubst {
Dean Troyerb9e25132013-10-01 14:45:04 -050051 local infile=$1
Julie Pichona3d60c82014-11-21 14:57:16 +000052 local slappass=$2
Dean Troyerb9e25132013-10-01 14:45:04 -050053 sed -e "
54 s|\${LDAP_OLCDB_NUMBER}|$LDAP_OLCDB_NUMBER|
Grzegorz Grasza26f81492021-08-16 10:36:03 +020055 s|\${LDAP_OLCDB_TYPE}|$LDAP_OLCDB_TYPE|
Julie Pichona3d60c82014-11-21 14:57:16 +000056 s|\${SLAPPASS}|$slappass|
Dean Troyerb9e25132013-10-01 14:45:04 -050057 s|\${LDAP_ROOTPW_COMMAND}|$LDAP_ROOTPW_COMMAND|
58 s|\${BASE_DC}|$LDAP_BASE_DC|
59 s|\${BASE_DN}|$LDAP_BASE_DN|
60 s|\${MANAGER_DN}|$LDAP_MANAGER_DN|
61 " $infile
62}
63
64# clean_ldap() - Remove ldap server
Ian Wienandaee18c72014-02-21 15:35:08 +110065function cleanup_ldap {
Dean Troyerb9e25132013-10-01 14:45:04 -050066 uninstall_package $(get_packages ldap)
67 if is_ubuntu; then
68 uninstall_package slapd ldap-utils libslp1
69 sudo rm -rf /etc/ldap/ldap.conf /var/lib/ldap
70 elif is_fedora; then
71 sudo rm -rf /etc/openldap /var/lib/ldap
Dean Troyerb9e25132013-10-01 14:45:04 -050072 fi
73}
74
75# init_ldap
76# init_ldap() - Initialize databases, etc.
Ian Wienandaee18c72014-02-21 15:35:08 +110077function init_ldap {
Dean Troyerb9e25132013-10-01 14:45:04 -050078 local keystone_ldif
79
Ian Wienandada886d2015-10-07 14:06:26 +110080 local tmp_ldap_dir
81 tmp_ldap_dir=$(mktemp -d -t ldap.$$.XXXXXXXXXX)
Dean Troyerb9e25132013-10-01 14:45:04 -050082
83 # Remove data but not schemas
84 clear_ldap_state
85
86 # Add our top level ldap nodes
87 if ldapsearch -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -b "$LDAP_BASE_DN" | grep -q "Success"; then
88 printf "LDAP already configured for $LDAP_BASE_DC\n"
89 else
90 printf "Configuring LDAP for $LDAP_BASE_DC\n"
91 # If BASE_DN is changed, the user may override the default file
92 if [[ -r $FILES/ldap/${LDAP_BASE_DC}.ldif.in ]]; then
Dean Troyeref66a772014-07-25 14:45:34 -050093 local keystone_ldif=${LDAP_BASE_DC}.ldif
Dean Troyerb9e25132013-10-01 14:45:04 -050094 else
Dean Troyeref66a772014-07-25 14:45:34 -050095 local keystone_ldif=keystone.ldif
Dean Troyerb9e25132013-10-01 14:45:04 -050096 fi
Dean Troyeref66a772014-07-25 14:45:34 -050097 _ldap_varsubst $FILES/ldap/${keystone_ldif}.in >$tmp_ldap_dir/${keystone_ldif}
98 if [[ -r $tmp_ldap_dir/${keystone_ldif} ]]; then
99 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 -0500100 fi
101 fi
102
Dean Troyeref66a772014-07-25 14:45:34 -0500103 rm -rf $tmp_ldap_dir
Dean Troyerb9e25132013-10-01 14:45:04 -0500104}
105
Brad Topolf127e2f2013-01-22 10:17:50 -0600106# install_ldap
107# install_ldap() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100108function install_ldap {
Brad Topolf127e2f2013-01-22 10:17:50 -0600109 echo "Installing LDAP inside function"
Brad Topolf127e2f2013-01-22 10:17:50 -0600110 echo "os_VENDOR is $os_VENDOR"
Dean Troyerb9e25132013-10-01 14:45:04 -0500111
Ian Wienandada886d2015-10-07 14:06:26 +1100112 local tmp_ldap_dir
113 tmp_ldap_dir=$(mktemp -d -t ldap.$$.XXXXXXXXXX)
Dean Troyerb9e25132013-10-01 14:45:04 -0500114
115 printf "installing OpenLDAP"
Brad Topolf127e2f2013-01-22 10:17:50 -0600116 if is_ubuntu; then
Leticia Wanderleycc363972017-06-26 23:52:52 -0300117 configure_ldap
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200118 elif is_fedora; then
Brad Topolf127e2f2013-01-22 10:17:50 -0600119 start_ldap
120 fi
121
Dean Troyerb9e25132013-10-01 14:45:04 -0500122 echo "LDAP_PASSWORD is $LDAP_PASSWORD"
Ian Wienandada886d2015-10-07 14:06:26 +1100123 local slappass
124 slappass=$(slappasswd -s $LDAP_PASSWORD)
Dean Troyeref66a772014-07-25 14:45:34 -0500125 printf "LDAP secret is $slappass\n"
Brad Topolf127e2f2013-01-22 10:17:50 -0600126
Dean Troyerb9e25132013-10-01 14:45:04 -0500127 # Create manager.ldif and add to olcdb
Julie Pichona3d60c82014-11-21 14:57:16 +0000128 _ldap_varsubst $FILES/ldap/manager.ldif.in $slappass >$tmp_ldap_dir/manager.ldif
Dean Troyeref66a772014-07-25 14:45:34 -0500129 sudo ldapmodify -Y EXTERNAL -H ldapi:/// -f $tmp_ldap_dir/manager.ldif
Brad Topolf127e2f2013-01-22 10:17:50 -0600130
Brad Topol0c2c3fc2013-03-19 03:01:30 -0500131 # On fedora we need to manually add cosine and inetorgperson schemas
Dean Troyerb9e25132013-10-01 14:45:04 -0500132 if is_fedora; then
Brad Topol0c2c3fc2013-03-19 03:01:30 -0500133 sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
134 sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
135 fi
136
Dean Troyeref66a772014-07-25 14:45:34 -0500137 rm -rf $tmp_ldap_dir
Brad Topolf127e2f2013-01-22 10:17:50 -0600138}
139
Leticia Wanderleycc363972017-06-26 23:52:52 -0300140# configure_ldap() - Configure LDAP - reconfigure slapd
141function configure_ldap {
142 sudo debconf-set-selections <<EOF
143 slapd slapd/internal/generated_adminpw password $LDAP_PASSWORD
144 slapd slapd/internal/adminpw password $LDAP_PASSWORD
145 slapd slapd/password2 password $LDAP_PASSWORD
146 slapd slapd/password1 password $LDAP_PASSWORD
147 slapd slapd/dump_database_destdir string /var/backups/slapd-VERSION
148 slapd slapd/domain string Users
149 slapd shared/organization string $LDAP_DOMAIN
Grzegorz Grasza26f81492021-08-16 10:36:03 +0200150 slapd slapd/backend string ${LDAP_OLCDB_TYPE^^}
Leticia Wanderleycc363972017-06-26 23:52:52 -0300151 slapd slapd/purge_database boolean true
152 slapd slapd/move_old_database boolean true
153 slapd slapd/allow_ldap_v2 boolean false
154 slapd slapd/no_configuration boolean false
155 slapd slapd/dump_database select when needed
156EOF
157 sudo apt-get install -y slapd ldap-utils
158 sudo dpkg-reconfigure -f noninteractive $LDAP_SERVICE_NAME
159}
160
Brad Topolf127e2f2013-01-22 10:17:50 -0600161# start_ldap() - Start LDAP
Ian Wienandaee18c72014-02-21 15:35:08 +1100162function start_ldap {
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200163 sudo service $LDAP_SERVICE_NAME restart
Brad Topolf127e2f2013-01-22 10:17:50 -0600164}
165
Brad Topolf127e2f2013-01-22 10:17:50 -0600166# stop_ldap() - Stop LDAP
Ian Wienandaee18c72014-02-21 15:35:08 +1100167function stop_ldap {
Ralf Haferkamp704106a2013-09-12 14:24:47 +0200168 sudo service $LDAP_SERVICE_NAME stop
Brad Topolf127e2f2013-01-22 10:17:50 -0600169}
170
171# clear_ldap_state() - Clear LDAP State
Ian Wienandaee18c72014-02-21 15:35:08 +1100172function clear_ldap_state {
Dean Troyerb44a8ef2014-03-06 11:25:04 -0600173 ldapdelete -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -r "$LDAP_BASE_DN" || :
Brad Topolf127e2f2013-01-22 10:17:50 -0600174}
175
176# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100177$_XTRACE_LDAP
Sean Dague584d90e2013-03-29 14:34:53 -0400178
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100179# Tell emacs to use shell-script-mode
180## Local variables:
181## mode: shell-script
182## End: