This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
docker-entrypoint.sh
executable file
·222 lines (195 loc) · 6.14 KB
/
docker-entrypoint.sh
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
222
#!/usr/bin/env bash
set -e
PATH="${PATH}:${PREFIX}/bin"
build() {
local var="$1"
local stmt="$2"
export "$var"+="$(printf "\n%s" "${stmt}")"
}
run() {
echo "${!1}" | "${PREFIX}"/bin/isql
}
createNewPassword() {
# openssl generates random data.
if openssl </dev/null >/dev/null 2>/dev/null
then
# We generate 40 random chars, strip any '/''s and get the first 20
NewPasswd=$(openssl rand -base64 40 | tr -d '/' | cut -c1-20)
fi
# If openssl is missing...
if [ -z "$NewPasswd" ]
then
NewPasswd=$(dd if=/dev/urandom bs=10 count=1 2>/dev/null | od -x | head -n 1 | tr -d ' ' | cut -c8-27)
fi
# On some systems even this routines may be missing. So if
# the specific one isn't available then keep the original password.
if [ -z "$NewPasswd" ]
then
NewPasswd="masterkey"
fi
echo "$NewPasswd"
}
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
confSet() {
confFile="${VOLUME}/etc/firebird.conf"
# Uncomment specified value
sed -i "s/^#${1}/${1}/g" "${confFile}"
# Set Value to new value
sed -i "s~^\(${1}\s*=\s*\).*$~\1${2}~" "${confFile}"
}
restoreBackups() {
if [ ! -f /firebird/etc/SYSDBA.password ]; then
echo "Will not attempt to restore backups because no '/firebird/etc/SYSDBA.password' found"
return
fi
(
shopt -s nullglob
set +e
. /firebird/etc/SYSDBA.password
for fbk in /firebird/restore/*.fbk; do
(
basename="$(basename -- $fbk)"
fname="${basename%.*}"
if [ ! -f "/firebird/data/${fname}.fdb" ]; then
if [ -f "/firebird/restore/${fname}.env" ]; then
. "/firebird/restore/${fname}.env"
fi
echo -n "Restoring '$fbk' "
"${PREFIX}/bin/gbak" -c -user "${RESTORE_USER:-$ISC_USER}" -password "${RESTORE_PASSWORD:-$ISC_PASSWORD}" "$fbk" "/firebird/data/${fname}.fdb"
echo "to '/firebird/data/${fname}.fdb'"
fi
)
done
set -e
)
}
firebirdSetup() {
# Create any missing folders
mkdir -p "${VOLUME}/system"
mkdir -p "${VOLUME}/log"
mkdir -p "${VOLUME}/data"
if [[ ! -e "${VOLUME}/etc/firebird.conf" ]]; then
cp -R "${PREFIX}/skel/etc" "${VOLUME}/"
file_env 'EnableLegacyClientAuth'
file_env 'EnableWireCrypt'
file_env 'DataTypeCompatibility'
if [[ ${EnableLegacyClientAuth} == 'true' ]]; then
confSet AuthServer "Legacy_Auth, Srp, Win_Sspi"
confSet AuthClient "Legacy_Auth, Srp, Win_Sspi"
confSet UserManager "Legacy_UserManager, Srp"
confSet WireCrypt "enabled"
fi
if [[ ${EnableWireCrypt} == 'true' ]]; then
confSet WireCrypt "enabled"
fi
if [[ ${DataTypeCompatibility} != '' ]]; then
confSet DataTypeCompatibility "${DataTypeCompatibility}"
fi
fi
if [ ! -f "${VOLUME}/system/security4.fdb" ]; then
cp "${PREFIX}/skel/security4.fdb" "${VOLUME}/system/security4.fdb"
file_env 'ISC_PASSWORD'
if [ -z "${ISC_PASSWORD}" ]; then
ISC_PASSWORD=$(createNewPassword)
echo "setting 'SYSDBA' password to '${ISC_PASSWORD}'"
fi
# initialize SYSDBA user for Srp authentication
"${PREFIX}"/bin/isql -user sysdba security.db <<EOL
create or alter user SYSDBA password '${ISC_PASSWORD}' using plugin Srp;
commit;
quit;
EOL
if [[ ${EnableLegacyClientAuth} == 'true' ]]; then
# also initialize/reset SYSDBA user for legacy authentication
"${PREFIX}"/bin/isql -user sysdba security.db <<EOL
create or alter user SYSDBA password '${ISC_PASSWORD}' using plugin Legacy_UserManager;
commit;
quit;
EOL
fi
# create or alter user SYSDBA password '${ISC_PASSWORD}';
cat > "${VOLUME}/etc/SYSDBA.password" <<EOL
# Firebird generated password for user SYSDBA is:
#
ISC_USER=sysdba
ISC_PASSWORD=${ISC_PASSWORD}
#
# Also set legacy variable though it can't be exported directly
#
ISC_PASSWD=${ISC_PASSWORD}
#
# generated at time $(date)
#
# Your password can be changed to a more suitable one using
# SQL operator ALTER USER.
#
EOL
fi
if [ -f "${VOLUME}/etc/SYSDBA.password" ]; then
source "${VOLUME}/etc/SYSDBA.password"
fi;
file_env 'FIREBIRD_USER'
file_env 'FIREBIRD_PASSWORD'
file_env 'FIREBIRD_DATABASE'
build isql "set sql dialect 3;"
if [ -n "${FIREBIRD_DATABASE}" ] && [ ! -f "${DBPATH}/${FIREBIRD_DATABASE}" ]; then
if [ "${FIREBIRD_USER}" ]; then
build isql "CONNECT employee USER '${ISC_USER}' PASSWORD '${ISC_PASSWORD}';"
if [ -z "${FIREBIRD_PASSWORD}" ]; then
FIREBIRD_PASSWORD=$(createNewPassword)
echo "setting '${FIREBIRD_USER}' password to '${FIREBIRD_PASSWORD}'"
fi
build isql "CREATE USER ${FIREBIRD_USER} PASSWORD '${FIREBIRD_PASSWORD}';"
build isql "COMMIT;"
fi
stmt="CREATE DATABASE '${DBPATH}/${FIREBIRD_DATABASE}'"
if [ "${FIREBIRD_USER}" ]; then
stmt+=" USER '${FIREBIRD_USER}' PASSWORD '${FIREBIRD_PASSWORD}'"
else
stmt+=" USER '${ISC_USER}' PASSWORD '${ISC_PASSWORD}'"
fi
stmt+=" DEFAULT CHARACTER SET UTF8;";
build isql "${stmt}";
build isql "COMMIT;"
if [ "${isql}" ]; then
build isql "QUIT;"
run isql
fi
fi
while IFS=';' read -ra FBALIAS; do
for i in "${FBALIAS[@]}"; do
if [ -z "${i##*=*}" ]; then
"${PREFIX}"/bin/registerDatabase.sh "${i%=*}" "${i#*=}"
fi
done
done <<< "$FIREBIRD_ALIASES"
}
if [[ "$1" == "firebird" ]]; then
firebirdSetup
restoreBackups
trap 'kill -TERM "$FBPID"' SIGTERM
/usr/local/firebird/bin/fbguard &
FBPID=$!
wait "$FBPID"
fi
exec "$@"