Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-integ / recipes-support / openldap / files / sources / libexec-update-ppolicy-schema.sh
1 #!/bin/bash
2 # This script serves one purpose, to add a possibly missing attribute
3 # to a ppolicy schema in a dynamic configuration of OpenLDAP. This
4 # attribute was introduced in openldap-2.4.43 and slapd will not 
5 # start without it later on.
6 #
7 # The script tries to update in a directory given as first parameter,
8 # or in /etc/openldap/slapd.d implicitly.
9 #
10 # Author: Matus Honek <mhonek@redhat.com>
11 # Bugzilla: #1487857
12
13 function log {
14     echo "Update dynamic configuration: " $@
15     true
16 }
17
18 function iferr {
19     if [ $? -ne 0 ]; then
20         log "ERROR: " $@
21         true
22     else
23         false
24     fi
25 }
26
27 function update {
28     set -u
29     shopt -s extglob
30
31     ORIGINAL="${1:-/etc/openldap/slapd.d}"
32     ORIGINAL="${ORIGINAL%*(/)}"
33
34     ### check if necessary
35     grep -r "pwdMaxRecordedFail" "${ORIGINAL}/cn=config/cn=schema" >/dev/null
36     [ $? -eq 0 ] && log "Schemas look up to date. Ok. Quitting." && return 0
37
38     ### prep
39     log "Prepare environment."
40     
41     TEMPDIR=$(mktemp -d)
42     iferr "Could not create a temporary directory. Quitting." && return 1
43     DBDIR="${TEMPDIR}/db"
44     SUBDBDIR="${DBDIR}/cn=temporary"
45
46     mkdir "${DBDIR}"
47     iferr "Could not create temporary configuration directory. Quitting." && return 1
48     cp -r --no-target-directory "${ORIGINAL}" "${SUBDBDIR}"
49     iferr "Could not copy configuration. Quitting." && return 1
50     
51     pushd "$TEMPDIR" >/dev/null
52
53     cat > temp.conf <<EOF
54 database ldif
55 suffix cn=temporary
56 directory db
57 access to * by * manage
58 EOF
59     
60     SOCKET="$(pwd)/socket"
61     LISTENER="ldapi://${SOCKET//\//%2F}"
62     CONN_PARAMS=("-Y" "EXTERNAL" "-H" "${LISTENER}")
63     
64     slapd -f temp.conf -h "$LISTENER" -d 0 >/dev/null 2>&1 &
65     SLAPDPID="$!"
66     sleep 2
67
68     ldapadd ${CONN_PARAMS[@]} -d 0 >/dev/null 2>&1 <<EOF
69 dn: cn=temporary
70 objectClass: olcGlobal
71 cn: temporary
72 EOF
73     iferr "Could not populate the temporary database. Quitting." && return 1
74     
75     ### update
76     log "Update with new pwdMaxRecordedFailure attribute."
77     FILTER="(&"
78     FILTER+="(olcObjectClasses=*'pwdPolicy'*)"
79     FILTER+="(!(olcObjectClasses=*'pwdPolicy'*'pwdMaxRecordedFailure'*))"
80     FILTER+="(!(olcAttributeTypes=*'pwdMaxRecordedFailure'*))"
81     FILTER+=")"
82     RES=$(ldapsearch ${CONN_PARAMS[@]} \
83                      -b cn=schema,cn=config,cn=temporary \
84                      -LLL \
85                      -o ldif-wrap=no \
86                      "$FILTER" \
87                      dn olcObjectClasses \
88                      2>/dev/null \
89               | sed '/^$/d')
90     DN=$(printf "$RES" | grep '^dn:')
91     OC=$(printf "$RES" | grep "^olcObjectClasses:.*'pwdPolicy'")
92     NEWOC="${OC//$ pwdSafeModify /$ pwdSafeModify $ pwdMaxRecordedFailure }"
93
94     test $(echo "$DN" | wc -l) = 1
95     iferr "Received more than one DN. Cannot continue. Quitting." && return 1
96     test "$NEWOC" != "$OC"
97     iferr "Updating pwdPolicy objectClass definition failed. Quitting." && return 1
98
99     ldapmodify ${CONN_PARAMS[@]} -d 0 >/dev/null 2>&1 <<EOF
100 $DN
101 changetype: modify
102 add: olcAttributeTypes
103 olcAttributeTypes: ( 1.3.6.1.4.1.42.2.27.8.1.30 NAME 'pwdMaxRecordedFailur
104  e' EQUALITY integerMatch ORDERING integerOrderingMatch  SYNTAX 1.3.6.1.4.1.
105  1466.115.121.1.27 SINGLE-VALUE )
106 -
107 delete: olcObjectClasses
108 $OC
109 -
110 add: olcObjectClasses
111 $NEWOC
112 EOF
113     iferr "Updating with new attribute failed. Quitting." && return 1
114
115     popd >/dev/null
116
117     ### apply
118     log "Apply changes."
119     cp -r --no-target-directory "$ORIGINAL" "$ORIGINAL~backup"
120     iferr "Backing up old configuration failed. Quitting." && return 1
121     cp -r --no-target-directory "$SUBDBDIR" "$ORIGINAL"
122     iferr "Applying new configuration failed. Quitting." && return 1
123     
124     ### clean up
125     log "Clean up."
126     kill "$SLAPDPID"
127     SLAPDPID=
128     rm -rf "$TEMPDIR"
129     TEMPDIR=
130 }
131
132 SLAPDPID=
133 TEMPDIR=
134 update "$1"
135 if [ $? -ne 0 ]; then
136     log "Clean up."
137     echo "$SLAPDPID"
138     echo "$TEMPDIR"
139     kill "$SLAPDPID"
140     rm -rf "$TEMPDIR"
141 fi
142 log "Finished."