-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathstartup.sh
More file actions
executable file
Β·221 lines (177 loc) Β· 7.83 KB
/
startup.sh
File metadata and controls
executable file
Β·221 lines (177 loc) Β· 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash -e
# ensure_uid $servicename $intended_uid $intended_gid $filename(s)
# Taken from OpenLDAP image; should be moved to the shared image at some point, then used by this script and OpenLDAP both
function ensure_uid() {
local servicename=$1
local intended_uid=${2:-33}
local intended_gid=${3:-33}
# Because there are 3 positional params
shift 3
log-helper info "$servicename user and group adjustments"
log-helper info "get current $servicename uid/gid info inside container"
CUR_USER_GID=`id -g $servicename || true`
CUR_USER_UID=`id -u $servicename || true`
SERVICE_UIDGID_CHANGED=false
if [ "$intended_uid" != "$CUR_USER_UID" ]; then
log-helper info "CUR_USER_UID (${CUR_USER_UID}) does't match intended_uid (${intended_uid}), adjusting..."
usermod -o -u "$intended_uid" $servicename
SERVICE_UIDGID_CHANGED=true
fi
if [ "$intended_gid" != "$CUR_USER_GID" ]; then
log-helper info "CUR_USER_GID (${CUR_USER_GID}) does't match intended_gid (${intended_gid}), adjusting..."
groupmod -o -g "$intended_gid" $servicename
SERVICE_UIDGID_CHANGED=true
fi
log-helper info '-------------------------------------'
log-helper info '$servicename GID/UID'
log-helper info '-------------------------------------'
log-helper info "User uid: $(id -u $servicename)"
log-helper info "User gid: $(id -g $servicename)"
log-helper info "uid/gid changed: ${SERVICE_UIDGID_CHANGED}"
log-helper info "-------------------------------------"
# fix file permissions
if [ "${DISABLE_CHOWN,,}" == "false" ]; then
log-helper info "updating file uid/gid ownership"
if [ ! -z "$*" ]; then
for file in $*; do
chown -R $servicename:$servicename $file
done
fi
fi
}
# set -x (bash debug) if log level is trace
# https://github.com/osixia/docker-light-baseimage/blob/stable/image/tool/log-helper
log-helper level eq trace && set -x
FIRST_START_DONE="${CONTAINER_STATE_DIR}/docker-phpldapadmin-first-start-done"
#
# HTTPS config
#
if [ "${PHPLDAPADMIN_HTTPS,,}" == "true" ]; then
log-helper info "Set apache2 https config..."
# generate a certificate and key if files don't exists
# https://github.com/osixia/docker-light-baseimage/blob/stable/image/service-available/:ssl-tools/assets/tool/ssl-helper
ssl-helper ${PHPLDAPADMIN_SSL_HELPER_PREFIX} "${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/apache2/certs/$PHPLDAPADMIN_HTTPS_CRT_FILENAME" "${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/apache2/certs/$PHPLDAPADMIN_HTTPS_KEY_FILENAME" "${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/apache2/certs/$PHPLDAPADMIN_HTTPS_CA_CRT_FILENAME"
# add CA certificat config if CA cert exists
if [ -e "${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/apache2/certs/$PHPLDAPADMIN_HTTPS_CA_CRT_FILENAME" ]; then
sed -i "s/#SSLCACertificateFile/SSLCACertificateFile/g" ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/apache2/https.conf
fi
ln -sf ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/apache2/https.conf /etc/apache2/sites-available/phpldapadmin.conf
#
# HTTP config
#
else
log-helper info "Set apache2 http config..."
ln -sf ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/apache2/http.conf /etc/apache2/sites-available/phpldapadmin.conf
fi
#
# Reverse proxy config
#
if [ "${PHPLDAPADMIN_TRUST_PROXY_SSL,,}" == "true" ]; then
echo 'SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on' > /etc/apache2/mods-enabled/remoteip_ssl.conf
fi
a2ensite phpldapadmin | log-helper debug
#
# phpLDAPadmin directory is empty, we use the bootstrap
#
if [ ! "$(ls -A -I lost+found /var/www/phpldapadmin)" ]; then
log-helper info "Bootstap phpLDAPadmin..."
cp -R /var/www/phpldapadmin_bootstrap/* /var/www/phpldapadmin
rm -rf /var/www/phpldapadmin_bootstrap
rm -f /var/www/phpldapadmin/config/config.php
fi
# if there is no config
if [ ! -e "/var/www/phpldapadmin/config/config.php" ]; then
# on container first start customise the container config file
if [ ! -e "$FIRST_START_DONE" ]; then
get_salt() {
salt=$(</dev/urandom tr -dc '1324567890#<>,()*.^@$% =-_~;:/{}[]+!`azertyuiopqsdfghjklmwxcvbnAZERTYUIOPQSDFGHJKLMWXCVBN' | head -c64 | tr -d '\\')
}
# phpLDAPadmin cookie secret
get_salt
sed -i "s|{{ PHPLDAPADMIN_CONFIG_BLOWFISH }}|${salt}|g" ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/config/config.php
append_to_file() {
TO_APPEND=$1
sed -i "s|{{ PHPLDAPADMIN_SERVERS }}|${TO_APPEND}\n{{ PHPLDAPADMIN_SERVERS }}|g" ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/config/config.php
}
append_value_to_file() {
local TO_PRINT=$1
local VALUE=$2
local php_value=$(print_by_php_type "$VALUE")
append_to_file "\$servers->setValue($TO_PRINT$php_value);"
}
print_by_php_type() {
if [ "$1" == "True" ]; then
echo "true"
elif [ "$1" == "False" ]; then
echo "false"
elif [[ "$1" == array\(\'* ]]; then
echo "$1"
else
echo "'$1'"
fi
}
# phpLDAPadmin host config
host_info(){
local to_print=$1
for info in $(complex-bash-env iterate "$2")
do
if [ $(complex-bash-env isRow "${!info}") = true ]; then
local key=$(complex-bash-env getRowKey "${!info}")
local valueVarName=$(complex-bash-env getRowValueVarName "${!info}")
if [ $(complex-bash-env isTable "${!valueVarName}") = true ] || [ $(complex-bash-env isRow "${!valueVarName}") = true ]; then
host_info "$to_print'$key'," "${valueVarName}"
else
append_value_to_file "$to_print'$key'," "${!valueVarName}"
fi
fi
done
}
# phpLDAPadmin config
for host in $(complex-bash-env iterate PHPLDAPADMIN_LDAP_HOSTS)
do
append_to_file "\$servers->newServer('ldap_pla');"
if [ $(complex-bash-env isRow "${!host}") = true ]; then
hostname=$(complex-bash-env getRowKey "${!host}")
info=$(complex-bash-env getRowValueVarName "${!host}")
if [ "${PHPLDAPADMIN_LDAP_HOSTS_FRIENDLY,,}" != "true" ]; then
append_to_file "\$servers->setValue('server','host','$hostname');"
fi
append_to_file "\$servers->setValue('server','name','$hostname');"
host_info "" "$info"
else
append_to_file "\$servers->setValue('server','name','${!host}');"
append_to_file "\$servers->setValue('server','host','${!host}');"
fi
done
sed -i "/{{ PHPLDAPADMIN_SERVERS }}/d" ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/config/config.php
touch $FIRST_START_DONE
fi
log-helper debug "link ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/config/config.php to /var/www/phpldapadmin/config/config.php"
cp -f ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/config/config.php /var/www/phpldapadmin/config/config.php
fi
PHPLDAPADMIN_WWW_DATA_UID=${PHPLDAPADMIN_WWW_DATA_UID:-33}
PHPLDAPADMIN_WWW_DATA_GID=${PHPLDAPADMIN_WWW_DATA_GID:-33}
ensure_uid www-data $PHPLDAPADMIN_WWW_DATA_UID $PHPLDAPADMIN_WWW_DATA_GID /var/www
##### For each template...
templatedir=${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/templates
echo "Finding templates"
find ${CONTAINER_SERVICE_DIR}/phpldapadmin/assets/
shopt -s nullglob
for action in creation modification; do
for template in ${templatedir}/${action}/*.xml; do
basename=`basename $template`
log-helper info "Linking $action template for $template"
target=/var/www/phpldapadmin/templates/$action/$basename
ln -sf $template $target
chown $PHPLDAPADMIN_WWW_DATA_UID:$PHPLDAPADMIN_WWW_DATA_GID $target
done
done
shopt -u nullglob
# fix file permission
find /var/www/ -type d -exec chmod 755 {} \;
find /var/www/ -type f -exec chmod 644 {} \;
# symlinks special (chown -R don't follow symlinks)
# Should be redone because of ensure_uid above
chown www-data:www-data /var/www/phpldapadmin/config/config.php
chmod 400 /var/www/phpldapadmin/config/config.php
exit 0