| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 1 | # lib/ldap | 
 | 2 | # Functions to control the installation and configuration of **ldap** | 
 | 3 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 4 | # ``lib/keystone`` calls the entry points in this order: | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 5 | # | 
 | 6 | # - install_ldap() | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 7 |  | 
 | 8 | # Save trace setting | 
 | 9 | XTRACE=$(set +o | grep xtrace) | 
 | 10 | set +o xtrace | 
 | 11 |  | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 12 |  | 
 | 13 | LDAP_DOMAIN=${LDAP_DOMAIN:-openstack.org} | 
 | 14 | # Make an array of domain components | 
 | 15 | DC=(${LDAP_DOMAIN/./ }) | 
 | 16 |  | 
 | 17 | # Leftmost domain component used in top-level entry | 
 | 18 | LDAP_BASE_DC=${DC[0]} | 
 | 19 |  | 
 | 20 | # Build the base DN | 
 | 21 | dn="" | 
 | 22 | for dc in ${DC[*]}; do | 
 | 23 |     dn="$dn,dc=$dc" | 
 | 24 | done | 
 | 25 | LDAP_BASE_DN=${dn#,} | 
 | 26 |  | 
 | 27 | LDAP_MANAGER_DN="${LDAP_MANAGER_DN:-cn=Manager,${LDAP_BASE_DN}}" | 
 | 28 | LDAP_URL=${LDAP_URL:-ldap://localhost} | 
 | 29 |  | 
| Ralf Haferkamp | 704106a | 2013-09-12 14:24:47 +0200 | [diff] [blame] | 30 | LDAP_SERVICE_NAME=slapd | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 31 |  | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 32 | if is_ubuntu; then | 
 | 33 |     LDAP_OLCDB_NUMBER=1 | 
 | 34 |     LDAP_ROOTPW_COMMAND=replace | 
 | 35 | elif is_fedora; then | 
 | 36 |     LDAP_OLCDB_NUMBER=2 | 
 | 37 |     LDAP_ROOTPW_COMMAND=add | 
 | 38 | elif 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 | 
 | 44 | fi | 
 | 45 |  | 
 | 46 |  | 
| Dean Troyer | cc6b443 | 2013-04-08 15:38:03 -0500 | [diff] [blame] | 47 | # Functions | 
 | 48 | # --------- | 
 | 49 |  | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 50 | # Perform common variable substitutions on the data files | 
 | 51 | # _ldap_varsubst file | 
 | 52 | function _ldap_varsubst() { | 
 | 53 |     local infile=$1 | 
 | 54 |     sed -e " | 
 | 55 |         s|\${LDAP_OLCDB_NUMBER}|$LDAP_OLCDB_NUMBER| | 
 | 56 |         s|\${SLAPPASS}|$SLAPPASS| | 
 | 57 |         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 | 
 | 65 | function cleanup_ldap() { | 
 | 66 |     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 | 
 | 72 |     elif is_suse; then | 
 | 73 |         sudo rm -rf /var/lib/ldap | 
 | 74 |     fi | 
 | 75 | } | 
 | 76 |  | 
 | 77 | # init_ldap | 
 | 78 | # init_ldap() - Initialize databases, etc. | 
 | 79 | function init_ldap() { | 
 | 80 |     local keystone_ldif | 
 | 81 |  | 
 | 82 |     TMP_LDAP_DIR=$(mktemp -d -t ldap.$$.XXXXXXXXXX) | 
 | 83 |  | 
 | 84 |     # Remove data but not schemas | 
 | 85 |     clear_ldap_state | 
 | 86 |  | 
 | 87 |     # Add our top level ldap nodes | 
 | 88 |     if ldapsearch -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -b "$LDAP_BASE_DN" | grep -q "Success"; then | 
 | 89 |         printf "LDAP already configured for $LDAP_BASE_DC\n" | 
 | 90 |     else | 
 | 91 |         printf "Configuring LDAP for $LDAP_BASE_DC\n" | 
 | 92 |         # If BASE_DN is changed, the user may override the default file | 
 | 93 |         if [[ -r $FILES/ldap/${LDAP_BASE_DC}.ldif.in ]]; then | 
 | 94 |             keystone_ldif=${LDAP_BASE_DC}.ldif | 
 | 95 |         else | 
 | 96 |             keystone_ldif=keystone.ldif | 
 | 97 |         fi | 
 | 98 |         _ldap_varsubst $FILES/ldap/${keystone_ldif}.in >$TMP_LDAP_DIR/${keystone_ldif} | 
 | 99 |         if [[ -r $TMP_LDAP_DIR/${keystone_ldif} ]]; then | 
 | 100 |             ldapadd -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -c -f $TMP_LDAP_DIR/${keystone_ldif} | 
 | 101 |         fi | 
 | 102 |     fi | 
 | 103 |  | 
 | 104 |     rm -rf TMP_LDAP_DIR | 
 | 105 | } | 
 | 106 |  | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 107 | # install_ldap | 
 | 108 | # install_ldap() - Collect source and prepare | 
 | 109 | function install_ldap() { | 
 | 110 |     echo "Installing LDAP inside function" | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 111 |     echo "os_VENDOR is $os_VENDOR" | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 112 |  | 
 | 113 |     TMP_LDAP_DIR=$(mktemp -d -t ldap.$$.XXXXXXXXXX) | 
 | 114 |  | 
 | 115 |     printf "installing OpenLDAP" | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 116 |     if is_ubuntu; then | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 117 |         # Ubuntu automatically starts LDAP so no need to call start_ldap() | 
 | 118 |         : | 
| Ralf Haferkamp | 704106a | 2013-09-12 14:24:47 +0200 | [diff] [blame] | 119 |     elif is_fedora; then | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 120 |         start_ldap | 
| Ralf Haferkamp | 704106a | 2013-09-12 14:24:47 +0200 | [diff] [blame] | 121 |     elif is_suse; then | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 122 |         _ldap_varsubst $FILES/ldap/suse-base-config.ldif.in >$TMP_LDAP_DIR/suse-base-config.ldif | 
 | 123 |         sudo slapadd -F /etc/openldap/slapd.d/ -bcn=config -l $TMP_LDAP_DIR/suse-base-config.ldif | 
| Ralf Haferkamp | 704106a | 2013-09-12 14:24:47 +0200 | [diff] [blame] | 124 |         sudo sed -i '/^OPENLDAP_START_LDAPI=/s/"no"/"yes"/g' /etc/sysconfig/openldap | 
 | 125 |         start_ldap | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 126 |     fi | 
 | 127 |  | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 128 |     echo "LDAP_PASSWORD is $LDAP_PASSWORD" | 
 | 129 |     SLAPPASS=$(slappasswd -s $LDAP_PASSWORD) | 
 | 130 |     printf "LDAP secret is $SLAPPASS\n" | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 131 |  | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 132 |     # Create manager.ldif and add to olcdb | 
 | 133 |     _ldap_varsubst $FILES/ldap/manager.ldif.in >$TMP_LDAP_DIR/manager.ldif | 
 | 134 |     sudo ldapmodify -Y EXTERNAL -H ldapi:/// -f $TMP_LDAP_DIR/manager.ldif | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 135 |  | 
| Brad Topol | 0c2c3fc | 2013-03-19 03:01:30 -0500 | [diff] [blame] | 136 |     # On fedora we need to manually add cosine and inetorgperson schemas | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 137 |     if is_fedora; then | 
| Brad Topol | 0c2c3fc | 2013-03-19 03:01:30 -0500 | [diff] [blame] | 138 |         sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif | 
 | 139 |         sudo ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif | 
 | 140 |     fi | 
 | 141 |  | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 142 |     rm -rf TMP_LDAP_DIR | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 143 | } | 
 | 144 |  | 
 | 145 | # start_ldap() - Start LDAP | 
 | 146 | function start_ldap() { | 
| Ralf Haferkamp | 704106a | 2013-09-12 14:24:47 +0200 | [diff] [blame] | 147 |     sudo service $LDAP_SERVICE_NAME restart | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 148 | } | 
 | 149 |  | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 150 | # stop_ldap() - Stop LDAP | 
 | 151 | function stop_ldap() { | 
| Ralf Haferkamp | 704106a | 2013-09-12 14:24:47 +0200 | [diff] [blame] | 152 |     sudo service $LDAP_SERVICE_NAME stop | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 153 | } | 
 | 154 |  | 
 | 155 | # clear_ldap_state() - Clear LDAP State | 
 | 156 | function clear_ldap_state() { | 
| Dean Troyer | b9e2513 | 2013-10-01 14:45:04 -0500 | [diff] [blame] | 157 |     ldapdelete -x -w $LDAP_PASSWORD -D "$LDAP_MANAGER_DN" -H $LDAP_URL -r "$LDAP_BASE_DN" | 
| Brad Topol | f127e2f | 2013-01-22 10:17:50 -0600 | [diff] [blame] | 158 | } | 
 | 159 |  | 
 | 160 | # Restore xtrace | 
 | 161 | $XTRACE | 
| Sean Dague | 584d90e | 2013-03-29 14:34:53 -0400 | [diff] [blame] | 162 |  | 
| Adam Spiers | 6a5aa7c | 2013-10-24 11:27:02 +0100 | [diff] [blame] | 163 | # Tell emacs to use shell-script-mode | 
 | 164 | ## Local variables: | 
 | 165 | ## mode: shell-script | 
 | 166 | ## End: |