-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker-entrypoint.sh
executable file
·303 lines (249 loc) · 8.9 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
set -eu
cd /var/www/html
# 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"
}
if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
file_env 'QUEXF_DB_HOST' 'mysql'
file_env 'QUEXF_FORMS_DIRECTORY' '/forms/'
file_env 'QUEXF_IMAGES_DIRECTORY' '/images/'
file_env 'QUEXF_ADMIN_PASSWORD' 'password'
# if we're linked to MySQL and thus have credentials already, let's use them
file_env 'QUEXF_DB_USER' "${MYSQL_ENV_MYSQL_USER:-root}"
if [ "$QUEXF_DB_USER" = 'root' ]; then
file_env 'QUEXF_DB_PASSWORD' "${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-}"
else
file_env 'QUEXF_DB_PASSWORD' "${MYSQL_ENV_MYSQL_PASSWORD:-}"
fi
file_env 'QUEXF_DB_NAME' "${MYSQL_ENV_MYSQL_DATABASE:-quexf}"
if [ -z "$QUEXF_DB_PASSWORD" ]; then
echo >&2 'error: missing required QUEXF_DB_PASSWORD environment variable'
echo >&2 ' Did you forget to -e QUEXF_DB_PASSWORD=... ?'
echo >&2
echo >&2 ' (Also of interest might be QUEXF_DB_USER and QUEXF_DB_NAME.)'
exit 1
fi
if [ ! -e /var/www/html/.htaccess ]; then
echo >&2 "queXF access control not found in $(pwd) - installing now..."
cat << EOF > /var/www/html/admin/.htaccess
AuthName "queXF"
AuthType Basic
AuthUserFile /opt/quexf/password
AuthGroupFile /opt/quexf/group
require group admin
EOF
cat << EOF > /var/www/html/client/.htaccess
AuthName "queXF"
AuthType Basic
AuthUserFile /opt/quexf/password
AuthGroupFile /opt/quexf/group
require group client
EOF
cat << EOF > /var/www/html/.htaccess
AuthName "queXF"
AuthType Basic
AuthUserFile /opt/quexf/password
AuthGroupFile /opt/quexf/group
require group verifier
EOF
echo >&2 "Complete! queXF access control has been successfully installed to $(pwd)"
else
echo >&2 "queXF access control found in $(pwd) - not installing."
fi
if [ ! -e /opt/quexf/password ]; then
echo >&2 "queXF password not found in /opt/quexf/password - creating now..."
htpasswd -c -s -b /opt/quexf/password admin "$QUEXF_ADMIN_PASSWORD"
cat << EOF > /opt/quexf/group
admin: admin
verifier: admin
client: admin
EOF
echo >&2 "Complete! queXF admin password created"
else
echo >&2 "queXF password found in /opt/quexf - not copying."
fi
# Install BaltimoreCyberTrustRoot.crt.pem
if [ ! -e BaltimoreCyberTrustRoot.crt.pem ]; then
echo "Downloading BaltimoreCyberTrustroot.crt.pem"
curl -o BaltimoreCyberTrustRoot.crt.pem -fsL "https://www.digicert.com/CACerts/BaltimoreCyberTrustRoot.crt.pem"
fi
chown www-data:www-data -R /images
chown www-data:www-data -R images
chown www-data:www-data -R /forms
chown www-data:www-data -R /opt/quexf
# see http://stackoverflow.com/a/2705678/433558
sed_escape_lhs() {
echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
}
sed_escape_rhs() {
echo "$@" | sed -e 's/[\/&]/\\&/g'
}
php_escape() {
php -r 'var_export(('$2') $argv[1]);' -- "$1"
}
set_config() {
key="$1"
value="$(sed_escape_lhs "$2")"
sed -i "/$key/s/[^,]*/'$value');/2" config.inc.php
}
set_config 'DB_HOST' "$QUEXF_DB_HOST"
set_config 'DB_USER' "$QUEXF_DB_USER"
set_config 'DB_PASS' "$QUEXF_DB_PASSWORD"
set_config 'DB_NAME' "$QUEXF_DB_NAME"
set_config 'SCANS_DIRECTORY' "$QUEXF_FORMS_DIRECTORY"
set_config 'IMAGES_DIRECTORY' "$QUEXF_IMAGES_DIRECTORY"
file_env 'QUEXF_DEBUG'
if [ "$QUEXF_DEBUG" ]; then
set_config 'DEBUG' 1
fi
file_env 'QUEXF_OCR_ENABLED'
if [ "$QUEXF_OCR_ENABLED" ]; then
set_config 'OCR_ENABLED' 'true'
fi
file_env 'QUEXF_HORI_WIDTH_BOX'
if [ "$QUEXF_HORI_WIDTH_BOX" ]; then
set_config 'HORI_WIDTH_BOX' "$QUEXF_HORI_WIDTH_BOX"
fi
file_env 'QUEXF_VERT_WIDTH_BOX'
if [ "$QUEXF_VERT_WIDTH_BOX" ]; then
set_config 'VERT_WIDTH_BOX' "$QUEXF_VERT_WIDTH_BOX"
fi
file_env 'QUEXF_BARCODE_TLX_PORTION'
if [ "$QUEXF_BARCODE_TLX_PORTION" ]; then
set_config 'BARCODE_TLX_PORTION' "$QUEXF_BARCODE_TLX_PORTION"
fi
file_env 'QUEXF_BARCODE_BRY_PORTION'
if [ "$QUEXF_BARCODE_BRY_PORTION" ]; then
set_config 'BARCODE_BRY_PORTION' "$QUEXF_BARCODE_BRY_PORTION"
fi
file_env 'QUEXF_BARCODE_TLY_PORTION'
if [ "$QUEXF_BARCODE_TLY_PORTION" ]; then
set_config 'BARCODE_TLY_PORTION' "$QUEXF_BARCODE_TLY_PORTION"
fi
file_env 'QUEXF_BARCODE_BRX_PORTION'
if [ "$QUEXF_BARCODE_BRX_PORTION" ]; then
set_config 'BARCODE_BRX_PORTION' "$QUEXF_BARCODE_BRX_PORTION"
fi
file_env 'QUEXF_BARCODE_TLX_PORTION2'
if [ "$QUEXF_BARCODE_TLX_PORTION2" ]; then
set_config 'BARCODE_TLX_PORTION2' "$QUEXF_BARCODE_TLX_PORTION2"
fi
file_env 'QUEXF_BARCODE_BRY_PORTION2'
if [ "$QUEXF_BARCODE_BRY_PORTION2" ]; then
set_config 'BARCODE_BRY_PORTION2' "$QUEXF_BARCODE_BRY_PORTION2"
fi
file_env 'QUEXF_BARCODE_TLY_PORTION2'
if [ "$QUEXF_BARCODE_TLY_PORTION2" ]; then
set_config 'BARCODE_TLY_PORTION2' "$QUEXF_BARCODE_TLY_PORTION2"
fi
file_env 'QUEXF_BARCODE_BRX_PORTION2'
if [ "$QUEXF_BARCODE_BRX_PORTION2" ]; then
set_config 'BARCODE_BRX_PORTION2' "$QUEXF_BARCODE_BRX_PORTION2"
fi
file_env 'QUEXF_DISPLAY_PAGE_WIDTH'
if [ "$QUEXF_DISPLAY_PAGE_WIDTH" ]; then
set_config 'DISPLAY_PAGE_WIDTH' "$QUEXF_DISPLAY_PAGE_WIDTH"
fi
file_env 'QUEXF_MULTIPLE_CHOICE_MIN_FILLED'
if [ "$QUEXF_MULTIPLE_CHOICE_MIN_FILLED" ]; then
set_config 'MULTIPLE_CHOICE_MIN_FILLED' "$QUEXF_MULTIPLE_CHOICE_MIN_FILLED"
fi
file_env 'QUEXF_MULTIPLE_CHOICE_MAX_FILLED'
if [ "$QUEXF_MULTIPLE_CHOICE_MAX_FILLED" ]; then
set_config 'MULTIPLE_CHOICE_MAX_FILLED' "$QUEXF_MULTIPLE_CHOICE_MAX_FILLED"
fi
file_env 'QUEXF_SINGLE_CHOICE_MIN_FILLED'
if [ "$QUEXF_SINGLE_CHOICE_MIN_FILLED" ]; then
set_config 'SINGLE_CHOICE_MIN_FILLED' "$QUEXF_SINGLE_CHOICE_MIN_FILLED"
fi
file_env 'QUEXF_SINGLE_CHOICE_MAX_FILLED'
if [ "$QUEXF_SINGLE_CHOICE_MAX_FILLED" ]; then
set_config 'SINGLE_CHOICE_MAX_FILLED' "$QUEXF_SINGLE_CHOICE_MAX_FILLED"
fi
file_env 'QUEXF_HTPASSWD_PATH'
if [ "$QUEXF_HTPASSWD_PATH" ]; then
set_config 'HTPASSWD_PATH' "$QUEXF_HTPASSWD_PATH"
fi
file_env 'QUEXF_HTGROUP_PATH'
if [ "$QUEXF_HTGROUP_PATH" ]; then
set_config 'HTGROUP_PATH' "$QUEXF_HTGROUP_PATH"
fi
file_env 'MYSQL_SSL_CA' ''
if [ "$MYSQL_SSL_CA" ]; then
set_config 'DB_SSL' "$MYSQL_SSL_CA"
fi
TERM=dumb php -- "$QUEXF_DB_HOST" "$QUEXF_DB_USER" "$QUEXF_DB_PASSWORD" "$QUEXF_DB_NAME" "$MYSQL_SSL_CA" <<'EOPHP'
<?php
// database might not exist, so let's try creating it (just to be safe)
$stderr = fopen('php://stderr', 'w');
list($host, $socket) = explode(':', $argv[1], 2);
$port = 0;
if (is_numeric($socket)) {
$port = (int) $socket;
$socket = null;
}
$maxTries = 50;
do {
$con = mysqli_init();
if (isset($argv[5]) && !empty($argv[5])) {
mysqli_ssl_set($con,NULL,NULL,$argv[5],NULL,NULL);
}
$mysql = mysqli_real_connect($con,$host, $argv[2], $argv[3], '', $port, $socket, MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT);
if (!$mysql) {
fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
--$maxTries;
if ($maxTries <= 0) {
exit(1);
}
sleep(3);
}
} while (!$mysql);
if (!$con->query('CREATE DATABASE IF NOT EXISTS `' . $con->real_escape_string($argv[4]) . '`')) {
fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $con->error . "\n");
$con->close();
exit(1);
}
// check if database populated
$con->select_db($con->real_escape_string($argv[4]));
if (!$con->query('SELECT COUNT(*) AS C FROM ' . $argv[4] . '.boxgrouptypes')) {
fwrite($stderr, "\n" . 'Cannot find queXF database. Will now populate... ' . $con->error . "\n");
$command = 'mysql'
. ' --host=' . $host
. ' --user=' . $argv[2]
. ' --password=' . $argv[3]
. ' --database=' . $argv[4]
. ' --ssl-ca=' . $argv[5]
. ' --execute="SOURCE ';
fwrite($stderr, "\n" . 'Loading queXF database...' . "\n");
$output1 = shell_exec($command . '/var/www/html/database/quexf.sql"');
fwrite($stderr, "\n" . 'Loaded queXF database: ' . $output1 . "\n");
$con->query("INSERT INTO " . $argv[4] . ".verifiers (description,http_username) VALUES ('Administrator','admin')");
} else {
fwrite($stderr, "\n" . 'queXF Database found. Leaving unchanged.' . "\n");
}
$con->close();
EOPHP
#Run import process
sleep 10
su -s /bin/bash -c "php /var/www/html/admin/startprocess.php /forms" www-data &
fi
exec "$@"