Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-integ / recipes-support / openldap / files / sources / libexec-functions
1 # Author: Jan Vcelak <jvcelak@redhat.com>
2
3 SLAPD_USER=
4 SLAPD_CONFIG_FILE=
5 SLAPD_CONFIG_DIR=
6 SLAPD_CONFIG_CUSTOM=
7 SLAPD_GLOBAL_OPTIONS=
8 SLAPD_SYSCONFIG_FILE=
9
10 function default_config()
11 {
12         SLAPD_USER=ldap
13         SLAPD_CONFIG_FILE=/etc/openldap/slapd.conf
14         SLAPD_CONFIG_DIR=/etc/openldap/slapd.d
15         SLAPD_CONFIG_CUSTOM=
16         SLAPD_GLOBAL_OPTIONS=
17         SLAPD_SYSCONFIG_FILE=/etc/sysconfig/slapd
18 }
19
20 function parse_config_options()
21 {
22         user=
23         config_file=
24         config_dir=
25         while getopts :u:f:F: opt; do
26                 case "$opt" in
27                 u)
28                         user="$OPTARG"
29                         ;;
30                 f)
31                         config_file="$OPTARG"
32                         ;;
33                 F)
34                         config_dir="$OPTARG"
35                         ;;
36                 esac
37         done
38
39         unset OPTIND
40
41         if [ -n "$user" ]; then
42                 SLAPD_USER="$user"
43         fi
44
45         if [ -n "$config_dir" ]; then
46                 SLAPD_CONFIG_DIR="$config_dir"
47                 SLAPD_CONFIG_FILE=
48                 SLAPD_CONFIG_CUSTOM=1
49                 SLAPD_GLOBAL_OPTIONS="-F '$config_dir'"
50         elif [ -n "$config_file" ]; then
51                 SLAPD_CONFIG_DIR=
52                 SLAPD_CONFIG_FILE="$config_file"
53                 SLAPD_CONFIG_CUSTOM=1
54                 SLAPD_GLOBAL_OPTIONS="-f '$config_file'"
55         fi
56 }
57
58 function uses_new_config()
59 {
60         [ -n "$SLAPD_CONFIG_DIR" ]
61         return $?
62 }
63
64 function run_as_ldap()
65 {
66         /sbin/runuser --shell /bin/sh --session-command "$1" "$SLAPD_USER"
67         return $?
68 }
69
70 function ldif_unbreak()
71 {
72         sed ':a;N;s/\n //;ta;P;D'
73 }
74
75 function ldif_value()
76 {
77         sed 's/^[^:]*: //'
78 }
79
80 function databases_new()
81 {
82         slapcat $SLAPD_GLOBAL_OPTIONS -c \
83         -H 'ldap:///cn=config???(|(objectClass=olcBdbConfig)(objectClass=olcHdbConfig))' 2>/dev/null | \
84                 ldif_unbreak | \
85                 grep '^olcDbDirectory: ' | \
86                 ldif_value
87 }
88
89 function databases_old()
90 {
91         awk     'begin { database="" }
92                 $1 == "database" { database=$2 }
93                 $1 == "directory" { if (database == "bdb" || database == "hdb") print $2}' \
94                 "$SLAPD_CONFIG_FILE"
95 }
96
97 function certificates_new()
98 {
99         slapcat $SLAPD_GLOBAL_OPTIONS -c -H 'ldap:///cn=config???(cn=config)' 2>/dev/null | \
100                 ldif_unbreak | \
101                 grep '^olcTLS\(CACertificateFile\|CACertificatePath\|CertificateFile\|CertificateKeyFile\): ' | \
102                 ldif_value
103 }
104
105 function certificates_old()
106 {
107         awk '$1 ~ "^TLS(CACertificate(File|Path)|CertificateFile|CertificateKeyFile)$" { print $2 } ' \
108                 "$SLAPD_CONFIG_FILE"
109 }
110
111 function certificates()
112 {
113         uses_new_config && certificates_new || certificates_old
114 }
115
116 function databases()
117 {
118         uses_new_config && databases_new || databases_old
119 }
120
121
122 function error()
123 {
124         format="$1\n"; shift
125         printf "$format" $@ >&2
126 }
127
128 function load_sysconfig()
129 {
130         [ -r "$SLAPD_SYSCONFIG_FILE" ] || return
131
132         . "$SLAPD_SYSCONFIG_FILE"
133         [ -n "$SLAPD_OPTIONS" ] && parse_config_options $SLAPD_OPTIONS
134 }
135
136 default_config