Repository navigation
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
2119 lines (1921 loc) · 99.6 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
2119 lines (1921 loc) · 99.6 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -e
# Default paths (CONFIG_FILE is set after secrets are processed in main())
DB_DIR="/db"
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}
debug_log() {
if [ "${DEBUG:-false}" = "true" ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] DEBUG: $*"
fi
}
# Normalize a boolean-ish value to a literal PHP bool token (true/false). Emitting
# the raw value (e.g. PA_DNSSEC_ENABLED=yes) as a bare token produced an undefined
# constant fatal in the generated config; anything not truthy becomes false.
to_php_bool() {
case "$(echo "${1}" | tr '[:upper:]' '[:lower:]')" in
true | 1 | yes | on) echo "true" ;;
*) echo "false" ;;
esac
}
# Escape a value for safe interpolation inside a single-quoted PHP string. Without
# this a secret containing a quote (e.g. DB_PASS=p'ass) produced an unparseable
# config and every request 500'd. Backslashes are doubled, then single quotes.
php_sq_escape() {
local value="$1"
value="${value//\\/\\\\}"
printf '%s' "${value//\'/\\\'}"
}
# Print $1 as a single-quoted PHP string literal
php_sq() {
printf "'%s'" "$(php_sq_escape "$1")"
}
# Print $2 when it is a number (or null when that is the default); otherwise warn on
# stderr and print the default $3. $1 names the variable for the log line. A stray unit
# such as PA_SESSION_TIMEOUT=30m used to land in settings.php as a parse error.
php_num() {
local name="$1" value="$2" default="$3"
if [[ "${value}" =~ ^-?[0-9]+(\.[0-9]+)?$ ]] || { [ "${default}" = "null" ] && [ "${value}" = "null" ]; }; then
printf '%s' "${value}"
else
log "WARNING: ${name}='${value}' is not a number, using default ${default}" >&2
printf '%s' "${default}"
fi
}
# Print PA_CLIENT_IP_HEADER as one of the values Poweradmin accepts; warn on stderr and print
# auto for anything else, so a typo does not pass unnoticed
client_ip_header() {
local value
value=$(printf '%s' "${PA_CLIENT_IP_HEADER:-auto}" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
case "${value}" in
auto|x-forwarded-for|x-real-ip|none) printf '%s' "${value}" ;;
*)
log "WARNING: PA_CLIENT_IP_HEADER='${PA_CLIENT_IP_HEADER}' is not one of auto, x-forwarded-for, x-real-ip, none; using auto" >&2
printf 'auto'
;;
esac
}
# Print $2 when it names a PHP LOG_* constant; otherwise warn on stderr and print $3
php_log_facility() {
local name="$1" value="$2" default="$3"
if [[ "${value}" =~ ^LOG_[A-Z0-9]+$ ]]; then
printf '%s' "${value}"
else
log "WARNING: ${name}='${value}' is not a LOG_* constant, using default ${default}" >&2
printf '%s' "${default}"
fi
}
# Print a comma-separated list as a PHP array of quoted strings ([] when empty)
php_str_array() {
local item items="" IFS=','
for item in $1; do
item="${item#"${item%%[![:space:]]*}"}"
item="${item%"${item##*[![:space:]]}"}"
[ -n "${item}" ] || continue
items="${items}$(php_sq "${item}"), "
done
printf '[%s]' "${items%, }"
}
# Wait until the database answers; on a fresh compose stack the app usually starts first.
# Returns 1 after DB_WAIT_TIMEOUT seconds so the caller can decide what to skip.
wait_for_db() {
local timeout="${DB_WAIT_TIMEOUT:-30}" waited=0
while ! "$@" >/dev/null 2>&1; do
if [ "${waited}" -ge "${timeout}" ]; then
log "WARNING: ${DB_TYPE} database at ${DB_HOST} did not answer within ${timeout}s (DB_WAIT_TIMEOUT)"
return 1
fi
[ "${waited}" -eq 0 ] && log "Waiting for the ${DB_TYPE} database at ${DB_HOST}..."
sleep 2
waited=$((waited + 2))
done
return 0
}
# Process Docker secrets - converts *__FILE environment variables to regular variables
process_secret_files() {
for VAR_NAME in $(env | grep '^[^=]\+__FILE=.\+' | sed -r 's/^([^=]*)__FILE=.*/\1/g'); do
VAR_NAME_FILE="${VAR_NAME}__FILE"
# Check if both regular and __FILE versions are set (they are exclusive)
if [ "${!VAR_NAME}" ]; then
log "ERROR: Both ${VAR_NAME} and ${VAR_NAME_FILE} are set but are exclusive"
exit 1
fi
VAR_FILENAME="${!VAR_NAME_FILE}"
log "Getting secret ${VAR_NAME} from ${VAR_FILENAME}"
# Validate the secret file exists and is readable
if [ ! -r "${VAR_FILENAME}" ]; then
log "ERROR: ${VAR_FILENAME} does not exist or is not readable"
exit 1
fi
# Read the secret file content and export as environment variable
export "${VAR_NAME}"="$(<"${VAR_FILENAME}")"
unset "${VAR_NAME_FILE}"
done
}
# Install custom CA certificate for trusting self-signed certs (e.g., internal Keycloak/OIDC)
install_trusted_ca() {
if [ -n "${TRUSTED_CA_FILE:-}" ]; then
# TRUSTED_CA_FILE__FILE arrives here as the PEM text itself, not as a path
if [[ "${TRUSTED_CA_FILE}" == *"-----BEGIN "* ]]; then
log "Installing custom CA certificate from TRUSTED_CA_FILE__FILE..."
printf '%s\n' "${TRUSTED_CA_FILE}" > /usr/local/share/ca-certificates/custom-ca.crt
update-ca-certificates
log "Custom CA certificate installed successfully"
return
fi
if [ ! -f "${TRUSTED_CA_FILE}" ]; then
log "ERROR: TRUSTED_CA_FILE points to non-existent file: ${TRUSTED_CA_FILE}"
exit 1
fi
log "Installing custom CA certificate from ${TRUSTED_CA_FILE}..."
cp "${TRUSTED_CA_FILE}" /usr/local/share/ca-certificates/custom-ca.crt
update-ca-certificates
log "Custom CA certificate installed successfully"
fi
}
# Configure Caddy trusted_proxies for correct client IP behind reverse proxies
configure_trusted_proxies() {
if [ -z "${TRUSTED_PROXIES:-}" ]; then
return
fi
local caddyfile="/etc/caddy/Caddyfile"
if [ ! -w "${caddyfile}" ]; then
log "WARNING: TRUSTED_PROXIES is set but ${caddyfile} is not writable - skipping"
return
fi
# Skip if already configured (container restart)
if grep -q 'trusted_proxies' "${caddyfile}"; then
debug_log "trusted_proxies already present in Caddyfile, skipping"
return
fi
# Caddy reads the same header the application trusts, so its log and Poweradmin's agree
local headers_line
case "$(client_ip_header)" in
none)
log "PA_CLIENT_IP_HEADER=none: leaving Caddy on the peer address"
return
;;
x-forwarded-for) headers_line='client_ip_headers X-Forwarded-For' ;;
x-real-ip) headers_line='client_ip_headers X-Real-IP' ;;
*) headers_line='client_ip_headers X-Forwarded-For X-Real-IP' ;;
esac
# Convert comma-separated input to space-separated (Caddy format)
local proxies
proxies=$(echo "${TRUSTED_PROXIES}" | tr ',' ' ' | tr -s ' ')
log "Configuring trusted proxies: ${proxies}"
# & and | are special in the sed replacement; a stray one would corrupt the Caddyfile
local proxies_sed
proxies_sed=$(printf '%s' "${proxies}" | sed 's/[&|\\]/\\&/g')
sed -i "s|order php_server before file_server|order php_server before file_server\n servers {\n trusted_proxies static ${proxies_sed}\n ${headers_line}\n }|" "${caddyfile}"
log "Trusted proxies configured in Caddyfile"
}
# Escape single quotes for SQL by replacing ' with ''
escape_sql() {
printf '%s' "$1" | sed "s/'/''/g"
}
# Initialize SQLite database if it doesn't exist
init_sqlite_db() {
if [ "${DB_TYPE}" = "sqlite" ] && [ ! -f "${DB_FILE:-/db/pdns.db}" ]; then
local db_file="${DB_FILE:-/db/pdns.db}"
local pdns_version="${PDNS_VERSION:-49}"
log "Initializing SQLite database at ${db_file}..."
# Create database directory if it doesn't exist
mkdir -p "$(dirname "${db_file}")"
# Initialize PowerDNS schema
if [ -f "/app/sql/pdns/${pdns_version}/schema.sqlite3.sql" ]; then
log "Using PowerDNS version ${pdns_version} schema"
sqlite3 "${db_file}" < /app/sql/pdns/${pdns_version}/schema.sqlite3.sql
else
log "WARNING: PowerDNS schema file for version ${pdns_version} not found, database may not be properly initialized"
fi
# Initialize Poweradmin schema
if [ -f "/app/sql/poweradmin-sqlite-db-structure.sql" ]; then
sqlite3 "${db_file}" < /app/sql/poweradmin-sqlite-db-structure.sql
else
log "WARNING: Poweradmin schema file not found, database may not be properly initialized"
fi
log "SQLite database initialized successfully"
fi
}
# Build MySQL SSL command-line options from DB_SSL / DB_SSL_VERIFY
build_mysql_ssl_opts() {
local db_ssl
db_ssl=$(to_php_bool "${DB_SSL:-false}")
local db_ssl_verify
db_ssl_verify=$(to_php_bool "${DB_SSL_VERIFY:-false}")
if [ "$db_ssl" != "true" ]; then
echo "--skip-ssl"
elif [ "$db_ssl_verify" != "true" ]; then
echo "--skip-ssl-verify-server-cert"
fi
}
# Write MySQL credentials to a private temporary file so the password is never passed
# on the command line, where it is readable until the client scrubs its own arguments.
# Option-file values interpret backslash escapes, so the value is quoted and escaped.
make_mysql_defaults_file() {
local tmpfile
tmpfile=$(mktemp)
chmod 600 "${tmpfile}"
printf '[client]\npassword="%s"\n' \
"$(printf '%s' "${DB_PASS:-}" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')" > "${tmpfile}"
printf '%s' "${tmpfile}"
}
# One place for the client connection arguments: every MySQL/PostgreSQL call below goes
# through these, so host, port, SSL and credentials cannot drift between call sites.
MYSQL_DEFAULTS_FILE=""
# Must run in the main shell (not a subshell) so the file and its cleanup trap persist
ensure_mysql_defaults_file() {
if [ -z "${MYSQL_DEFAULTS_FILE}" ]; then
MYSQL_DEFAULTS_FILE=$(make_mysql_defaults_file)
# shellcheck disable=SC2064
trap "rm -f '${MYSQL_DEFAULTS_FILE}'" EXIT
fi
}
mysql_connection_args() {
printf '%s\n' "--defaults-file=${MYSQL_DEFAULTS_FILE}"
build_mysql_ssl_opts
printf '%s\n' "-h${DB_HOST}"
[ -n "${DB_PORT:-}" ] && printf '%s\n' "-P${DB_PORT}"
printf '%s\n' "-u${DB_USER}"
}
mysql_client() { # mysql_client [mysql args...] - runs against DB_NAME
ensure_mysql_defaults_file
local -a args
mapfile -t args < <(mysql_connection_args)
mysql "${args[@]}" "${DB_NAME}" "$@"
}
mysql_ping() {
ensure_mysql_defaults_file
local -a args
mapfile -t args < <(mysql_connection_args)
mysqladmin "${args[@]}" ping
}
psql_client() { # psql_client [psql args...]
local -a port_opt=()
[ -n "${DB_PORT:-}" ] && port_opt=(-p "${DB_PORT}")
PGPASSWORD="${DB_PASS}" psql -h "${DB_HOST}" "${port_opt[@]}" -U "${DB_USER}" -d "${DB_NAME}" "$@"
}
pg_ready() {
local -a port_opt=()
[ -n "${DB_PORT:-}" ] && port_opt=(-p "${DB_PORT}")
pg_isready -h "${DB_HOST}" "${port_opt[@]}" -U "${DB_USER}" -d "${DB_NAME}" -q
}
# Load the Poweradmin schema into an empty MySQL database (parity with SQLite init).
# Idempotent: skips when the users table already exists, so existing data is never touched.
init_mysql_db() {
[ "${DB_TYPE}" = "mysql" ] || return 0
# mysqladmin ping succeeds once the server is up, even before credentials are checked
wait_for_db mysql_ping || true
# '|| true' keeps a probe failure (DB unreachable / bad credentials) from tripping 'set -e';
# the empty-result check below turns it into a graceful skip.
local table_exists
table_exists=$(mysql_client -sNe \
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$(escape_sql "${DB_NAME}")' AND table_name='users';" 2>/dev/null) || true
if [ -z "${table_exists}" ]; then
log "WARNING: Could not reach MySQL database '${DB_NAME}' to verify schema - skipping auto-initialization"
return 0
fi
# Optionally load the PowerDNS schema (domains, records, ...) into an empty database, matching
# SQLite init. Off by default: most MySQL/PostgreSQL deployments provision the PowerDNS schema
# separately (dedicated container, DBA) or use the API backend. Set PA_INIT_PDNS_SCHEMA=true to
# opt in. Skipped when PA_PDNS_DB_NAME points the PowerDNS tables at a separate database.
local init_pdns_schema
init_pdns_schema=$(to_php_bool "${PA_INIT_PDNS_SCHEMA:-false}")
if [ "${init_pdns_schema}" = "true" ] && [ -z "${PA_PDNS_DB_NAME:-}" ]; then
local pdns_version="${PDNS_VERSION:-49}"
local pdns_table_exists
pdns_table_exists=$(mysql_client -sNe \
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$(escape_sql "${DB_NAME}")' AND table_name='domains';" 2>/dev/null) || true
if [ "${pdns_table_exists}" = "0" ]; then
local pdns_schema="/app/sql/pdns/${pdns_version}/schema.mysql.sql"
if [ ! -f "${pdns_schema}" ]; then
log "WARNING: PowerDNS MySQL schema file for version ${pdns_version} not found, database may not be properly initialized"
elif mysql_client < "${pdns_schema}"; then
log "PowerDNS schema (version ${pdns_version}) initialized successfully in MySQL database '${DB_NAME}'"
else
log "ERROR: Failed to initialize PowerDNS schema in MySQL database '${DB_NAME}'"
exit 1
fi
fi
fi
if [ "${table_exists}" -gt 0 ]; then
debug_log "Poweradmin schema already present in MySQL database '${DB_NAME}'"
return 0
fi
log "Poweradmin schema not found in database '${DB_NAME}', initializing..."
if [ ! -f "/app/sql/poweradmin-mysql-db-structure.sql" ]; then
log "WARNING: Poweradmin MySQL schema file not found, database may not be properly initialized"
return 0
fi
if mysql_client \
< /app/sql/poweradmin-mysql-db-structure.sql; then
log "Poweradmin schema initialized successfully in MySQL database '${DB_NAME}'"
else
log "ERROR: Failed to initialize Poweradmin schema in MySQL database '${DB_NAME}'"
exit 1
fi
}
# Load the Poweradmin schema into an empty PostgreSQL database (parity with SQLite init).
# Idempotent: skips when the users table already exists, so existing data is never touched.
init_pgsql_db() {
[ "${DB_TYPE}" = "pgsql" ] || return 0
wait_for_db pg_ready || true
# '|| true' keeps a probe failure (DB unreachable / bad credentials) from tripping 'set -e';
# the empty-result check below turns it into a graceful skip.
local table_exists
table_exists=$(psql_client -tAc \
"SELECT to_regclass('public.users') IS NOT NULL;" 2>/dev/null) || true
if [ -z "${table_exists}" ]; then
log "WARNING: Could not reach PostgreSQL database '${DB_NAME}' to verify schema - skipping auto-initialization"
return 0
fi
# Optionally load the PowerDNS schema (domains, records, ...) into an empty database, matching
# SQLite init. Off by default: most MySQL/PostgreSQL deployments provision the PowerDNS schema
# separately (dedicated container, DBA) or use the API backend. Set PA_INIT_PDNS_SCHEMA=true to
# opt in. Skipped when PA_PDNS_DB_NAME points the PowerDNS tables at a separate database.
local init_pdns_schema
init_pdns_schema=$(to_php_bool "${PA_INIT_PDNS_SCHEMA:-false}")
if [ "${init_pdns_schema}" = "true" ] && [ -z "${PA_PDNS_DB_NAME:-}" ]; then
local pdns_version="${PDNS_VERSION:-49}"
local pdns_table_exists
pdns_table_exists=$(psql_client -tAc \
"SELECT to_regclass('public.domains') IS NOT NULL;" 2>/dev/null) || true
if [ "${pdns_table_exists}" = "f" ]; then
local pdns_schema="/app/sql/pdns/${pdns_version}/schema.pgsql.sql"
if [ ! -f "${pdns_schema}" ]; then
log "WARNING: PowerDNS PostgreSQL schema file for version ${pdns_version} not found, database may not be properly initialized"
elif psql_client -v ON_ERROR_STOP=1 -q -f "${pdns_schema}"; then
log "PowerDNS schema (version ${pdns_version}) initialized successfully in PostgreSQL database '${DB_NAME}'"
else
log "ERROR: Failed to initialize PowerDNS schema in PostgreSQL database '${DB_NAME}'"
exit 1
fi
fi
fi
if [ "${table_exists}" = "t" ]; then
debug_log "Poweradmin schema already present in PostgreSQL database '${DB_NAME}'"
return 0
fi
log "Poweradmin schema not found in database '${DB_NAME}', initializing..."
if [ ! -f "/app/sql/poweradmin-pgsql-db-structure.sql" ]; then
log "WARNING: Poweradmin PostgreSQL schema file not found, database may not be properly initialized"
return 0
fi
if psql_client -v ON_ERROR_STOP=1 -q -f /app/sql/poweradmin-pgsql-db-structure.sql; then
log "Poweradmin schema initialized successfully in PostgreSQL database '${DB_NAME}'"
else
log "ERROR: Failed to initialize Poweradmin schema in PostgreSQL database '${DB_NAME}'"
exit 1
fi
}
# Validate required database configuration
validate_database_config() {
if [ -z "${DB_TYPE}" ]; then
log "ERROR: DB_TYPE environment variable is required. Supported types: sqlite, mysql, pgsql"
exit 1
fi
debug_log "Starting database validation with DB_TYPE=${DB_TYPE}"
case "${DB_TYPE}" in
"sqlite")
local db_file="${DB_FILE:-/db/pdns.db}"
debug_log "Checking SQLite database file: ${db_file}"
debug_log "File exists check: [ -f ${db_file} ] = $([ -f "${db_file}" ] && echo true || echo false)"
debug_log "Directory writable check: [ -w $(dirname ${db_file}) ] = $([ -w "$(dirname "${db_file}")" ] && echo true || echo false)"
if [ ! -f "${db_file}" ] && [ ! -w "$(dirname "${db_file}")" ]; then
log "ERROR: SQLite database file ${db_file} doesn't exist and directory is not writable"
exit 1
fi
debug_log "SQLite validation passed"
;;
"mysql"|"pgsql")
if [ -z "${DB_HOST}" ]; then
log "ERROR: DB_HOST is required for ${DB_TYPE} database"
exit 1
fi
if [ -z "${DB_USER}" ]; then
log "ERROR: DB_USER is required for ${DB_TYPE} database"
exit 1
fi
if [ -z "${DB_NAME}" ]; then
log "ERROR: DB_NAME is required for ${DB_TYPE} database"
exit 1
fi
# An empty or 0 port means the driver default, as it always has
if [ -n "${DB_PORT:-}" ] && [ "${DB_PORT}" != "0" ]; then
if ! [[ "${DB_PORT}" =~ ^[0-9]{1,5}$ ]] || [ "$((10#${DB_PORT}))" -lt 1 ] || [ "$((10#${DB_PORT}))" -gt 65535 ]; then
log "ERROR: DB_PORT must be a port number between 1 and 65535, got '${DB_PORT}'"
exit 1
fi
# The MySQL client and PDO both treat "localhost" as the unix socket and ignore the port
if [ "${DB_TYPE}" = "mysql" ] && [ "${DB_HOST,,}" = "localhost" ]; then
log "WARNING: DB_HOST=localhost connects through the MySQL socket and ignores DB_PORT=${DB_PORT}; set DB_HOST=127.0.0.1 to use TCP"
fi
fi
;;
*)
log "ERROR: Unsupported database type: ${DB_TYPE}. Supported types: sqlite, mysql, pgsql"
exit 1
;;
esac
debug_log "Database validation function completed"
}
# Validate DNS configuration
validate_dns_config() {
debug_log "DNS_NS1='${DNS_NS1}'"
debug_log "DNS_NS2='${DNS_NS2}'"
debug_log "DNS_HOSTMASTER='${DNS_HOSTMASTER}'"
if [ -z "${DNS_NS1}" ]; then
log "WARNING: DNS_NS1 not set, using default: ns1.example.com"
DNS_NS1="ns1.example.com"
fi
if [ -z "${DNS_NS2}" ]; then
log "WARNING: DNS_NS2 not set, using default: ns2.example.com"
DNS_NS2="ns2.example.com"
fi
if [ -z "${DNS_HOSTMASTER}" ]; then
log "WARNING: DNS_HOSTMASTER not set, using default: hostmaster@example.com"
DNS_HOSTMASTER="hostmaster@example.com"
fi
debug_log "DNS validation completed"
}
# Validate mail configuration if enabled
validate_mail_config() {
local mail_enabled
mail_enabled=$(to_php_bool "${PA_MAIL_ENABLED:-true}")
if [ "$mail_enabled" = "true" ] && [ "${PA_MAIL_TRANSPORT}" = "smtp" ]; then
if [ -z "${PA_SMTP_HOST}" ]; then
log "ERROR: PA_SMTP_HOST is required when using SMTP transport"
exit 1
fi
if [ -z "${PA_MAIL_FROM}" ]; then
log "ERROR: PA_MAIL_FROM is required when mail is enabled"
exit 1
fi
fi
}
# Validate API configuration if enabled
validate_api_config() {
local api_enabled
api_enabled=$(to_php_bool "${PA_API_ENABLED:-false}")
if [ "$api_enabled" = "true" ] && [ -n "${PA_PDNS_API_URL}" ]; then
if [ -z "${PA_PDNS_API_KEY}" ]; then
log "ERROR: PA_PDNS_API_KEY is required when PowerDNS API URL is specified"
exit 1
fi
fi
if [ -n "${PA_PDNS_BACKEND:-}" ]; then
log "WARNING: PA_PDNS_BACKEND is not a recognized variable - did you mean PA_DNS_BACKEND?"
fi
}
# Validate LDAP configuration if enabled
validate_ldap_config() {
local ldap_enabled
ldap_enabled=$(to_php_bool "${PA_LDAP_ENABLED:-false}")
if [ "$ldap_enabled" = "true" ]; then
local required_ldap_vars=("PA_LDAP_URI" "PA_LDAP_BASE_DN")
for var in "${required_ldap_vars[@]}"; do
if [ -z "${!var}" ]; then
log "ERROR: ${var} is required when LDAP is enabled"
exit 1
fi
done
fi
}
# Warn when header mode is configured without any trusted proxy: the header is ignored then
validate_remote_user_config() {
if [ "$(to_php_bool "${PA_REMOTE_USER_ENABLED:-false}")" = "true" ] \
&& [ -n "${PA_REMOTE_USER_HEADER:-}" ] \
&& [ "$(php_str_array "${PA_REMOTE_USER_TRUSTED_PROXIES:-}")" = "[]" ]; then
log "WARNING: PA_REMOTE_USER_HEADER is set but PA_REMOTE_USER_TRUSTED_PROXIES is empty. The header will be ignored until the proxy address is listed in PA_REMOTE_USER_TRUSTED_PROXIES"
fi
}
# Validate SAML configuration if enabled
validate_saml_config() {
local saml_enabled
saml_enabled=$(to_php_bool "${PA_SAML_ENABLED:-false}")
if [ "$saml_enabled" = "true" ]; then
# The entityID and ACS URL advertised to the IdP are never derived from the
# request, so one of the three supported sources has to supply them: the
# application URL, the legacy base URL, or all three explicit SP URLs.
if [ -z "${PA_APPLICATION_URL}" ] && [ -z "${PA_BASE_URL}" ] &&
{ [ -z "${PA_SAML_SP_ENTITY_ID}" ] || [ -z "${PA_SAML_SP_ACS_URL}" ] || [ -z "${PA_SAML_SP_SLS_URL}" ]; }; then
log "ERROR: SAML needs PA_APPLICATION_URL, PA_BASE_URL, or all of PA_SAML_SP_ENTITY_ID, PA_SAML_SP_ACS_URL and PA_SAML_SP_SLS_URL. They define the entityID and ACS URL advertised to the identity provider"
exit 1
fi
# Check if at least one provider is enabled
local azure_enabled
azure_enabled=$(to_php_bool "${PA_SAML_AZURE_ENABLED:-false}")
local okta_enabled
okta_enabled=$(to_php_bool "${PA_SAML_OKTA_ENABLED:-false}")
local auth0_enabled
auth0_enabled=$(to_php_bool "${PA_SAML_AUTH0_ENABLED:-false}")
local keycloak_enabled
keycloak_enabled=$(to_php_bool "${PA_SAML_KEYCLOAK_ENABLED:-false}")
local generic_enabled
generic_enabled=$(to_php_bool "${PA_SAML_GENERIC_ENABLED:-false}")
if [ "$azure_enabled" != "true" ] && [ "$okta_enabled" != "true" ] && [ "$auth0_enabled" != "true" ] && [ "$keycloak_enabled" != "true" ] && [ "$generic_enabled" != "true" ]; then
log "ERROR: SAML is enabled but no SAML providers are configured. Enable at least one provider (PA_SAML_*_ENABLED=true)"
exit 1
fi
# Validate Azure SAML configuration if enabled
if [ "$azure_enabled" = "true" ]; then
if [ -z "${PA_SAML_AZURE_X509_CERT}" ]; then
log "ERROR: PA_SAML_AZURE_X509_CERT is required when Azure SAML is enabled"
exit 1
fi
fi
# Validate Okta SAML configuration if enabled
if [ "$okta_enabled" = "true" ]; then
local required_okta_vars=("PA_SAML_OKTA_ENTITY_ID" "PA_SAML_OKTA_SSO_URL" "PA_SAML_OKTA_X509_CERT")
for var in "${required_okta_vars[@]}"; do
if [ -z "${!var}" ]; then
log "ERROR: ${var} is required when Okta SAML is enabled"
exit 1
fi
done
fi
# Validate Auth0 SAML configuration if enabled
if [ "$auth0_enabled" = "true" ]; then
local required_auth0_vars=("PA_SAML_AUTH0_ENTITY_ID" "PA_SAML_AUTH0_SSO_URL" "PA_SAML_AUTH0_X509_CERT")
for var in "${required_auth0_vars[@]}"; do
if [ -z "${!var}" ]; then
log "ERROR: ${var} is required when Auth0 SAML is enabled"
exit 1
fi
done
fi
# Validate Keycloak SAML configuration if enabled
if [ "$keycloak_enabled" = "true" ]; then
local required_keycloak_vars=("PA_SAML_KEYCLOAK_ENTITY_ID" "PA_SAML_KEYCLOAK_SSO_URL" "PA_SAML_KEYCLOAK_X509_CERT")
for var in "${required_keycloak_vars[@]}"; do
if [ -z "${!var}" ]; then
log "ERROR: ${var} is required when Keycloak SAML is enabled"
exit 1
fi
done
fi
# Validate Generic SAML configuration if enabled
if [ "$generic_enabled" = "true" ]; then
local required_generic_vars=("PA_SAML_GENERIC_ENTITY_ID" "PA_SAML_GENERIC_SSO_URL" "PA_SAML_GENERIC_X509_CERT")
for var in "${required_generic_vars[@]}"; do
if [ -z "${!var}" ]; then
log "ERROR: ${var} is required when Generic SAML is enabled"
exit 1
fi
done
fi
fi
}
# Validate OIDC configuration if enabled
validate_oidc_config() {
local oidc_enabled
oidc_enabled=$(to_php_bool "${PA_OIDC_ENABLED:-false}")
if [ "$oidc_enabled" = "true" ]; then
# The redirect_uri is never derived from the request, so OIDC cannot
# start without an explicit application URL
if [ -z "${PA_APPLICATION_URL}" ]; then
log "ERROR: PA_APPLICATION_URL is required when OIDC is enabled. It defines the OAuth redirect_uri registered with the provider"
exit 1
fi
# Check if at least one provider is enabled
local azure_enabled
azure_enabled=$(to_php_bool "${PA_OIDC_AZURE_ENABLED:-false}")
local google_enabled
google_enabled=$(to_php_bool "${PA_OIDC_GOOGLE_ENABLED:-false}")
local generic_enabled
generic_enabled=$(to_php_bool "${PA_OIDC_GENERIC_ENABLED:-false}")
if [ "$azure_enabled" != "true" ] && [ "$google_enabled" != "true" ] && [ "$generic_enabled" != "true" ]; then
log "ERROR: OIDC is enabled but no OIDC providers are configured. Enable at least one provider (PA_OIDC_*_ENABLED=true)"
exit 1
fi
# Validate Azure OIDC configuration if enabled
if [ "$azure_enabled" = "true" ]; then
if [ -z "${PA_OIDC_AZURE_CLIENT_ID}" ] || [ -z "${PA_OIDC_AZURE_CLIENT_SECRET}" ]; then
log "ERROR: PA_OIDC_AZURE_CLIENT_ID and PA_OIDC_AZURE_CLIENT_SECRET are required when Azure OIDC is enabled"
exit 1
fi
fi
# Validate Google OIDC configuration if enabled
if [ "$google_enabled" = "true" ]; then
if [ -z "${PA_OIDC_GOOGLE_CLIENT_ID}" ] || [ -z "${PA_OIDC_GOOGLE_CLIENT_SECRET}" ]; then
log "ERROR: PA_OIDC_GOOGLE_CLIENT_ID and PA_OIDC_GOOGLE_CLIENT_SECRET are required when Google OIDC is enabled"
exit 1
fi
fi
# Validate Generic OIDC configuration if enabled
if [ "$generic_enabled" = "true" ]; then
if [ -z "${PA_OIDC_GENERIC_CLIENT_ID}" ] || [ -z "${PA_OIDC_GENERIC_CLIENT_SECRET}" ]; then
log "ERROR: PA_OIDC_GENERIC_CLIENT_ID and PA_OIDC_GENERIC_CLIENT_SECRET are required when Generic OIDC is enabled"
exit 1
fi
# Check for either auto_discovery with metadata_url OR manual endpoint URLs
local auto_discovery
auto_discovery=$(to_php_bool "${PA_OIDC_GENERIC_AUTO_DISCOVERY:-false}")
if [ "$auto_discovery" = "true" ]; then
if [ -z "${PA_OIDC_GENERIC_METADATA_URL}" ]; then
log "ERROR: PA_OIDC_GENERIC_METADATA_URL is required when PA_OIDC_GENERIC_AUTO_DISCOVERY is enabled"
exit 1
fi
else
# Manual configuration - require essential endpoints
if [ -z "${PA_OIDC_GENERIC_AUTHORIZE_URL}" ] || [ -z "${PA_OIDC_GENERIC_TOKEN_URL}" ]; then
log "ERROR: PA_OIDC_GENERIC_AUTHORIZE_URL and PA_OIDC_GENERIC_TOKEN_URL are required when auto_discovery is disabled"
exit 1
fi
fi
fi
fi
}
# Create initial admin user if specified
create_admin_user() {
local create_admin
create_admin=$(to_php_bool "${PA_CREATE_ADMIN:-false}")
if [ "$create_admin" != "true" ]; then
debug_log "Admin user creation disabled"
return 0
fi
local admin_username="${PA_ADMIN_USERNAME:-admin}"
local admin_password="${PA_ADMIN_PASSWORD:-}"
local admin_email="${PA_ADMIN_EMAIL:-admin@example.com}"
local admin_fullname="${PA_ADMIN_FULLNAME:-Administrator}"
local password_generated="false"
# Generate secure random password if not provided
if [ -z "${admin_password}" ]; then
admin_password=$(openssl rand -base64 16 | tr -d "=+/" | cut -c1-16)
password_generated="true"
log "Generated secure random password for admin user"
fi
debug_log "Creating admin user: ${admin_username}"
# Generate password hash using PHP (secure method with proper argument passing)
local password_hash
if ! password_hash=$(php -r "echo password_hash(\$argv[1], PASSWORD_DEFAULT);" -- "${admin_password}" 2>/dev/null) \
|| [ -z "${password_hash}" ]; then
log "ERROR: Failed to generate password hash for admin user"
exit 1
fi
debug_log "Generated password hash for admin user"
# Database-specific user creation
local insert_result=0
case "${DB_TYPE}" in
"sqlite")
local db_file="${DB_FILE:-/db/pdns.db}"
debug_log "Creating admin user in SQLite database: ${db_file}"
# Check if user already exists
local user_exists
user_exists=$(sqlite3 "${db_file}" "SELECT COUNT(*) FROM users WHERE username='$(escape_sql "${admin_username}")';")
if [ "${user_exists}" -gt 0 ]; then
log "Admin user '${admin_username}' already exists, skipping creation"
return 0
fi
# Insert admin user
if ! sqlite3 "${db_file}" "INSERT INTO users (username, password, fullname, email, description, perm_templ, active, use_ldap) VALUES ('$(escape_sql "${admin_username}")', '$(escape_sql "${password_hash}")', '$(escape_sql "${admin_fullname}")', '$(escape_sql "${admin_email}")', 'System Administrator', 1, 1, 0);"; then
insert_result=1
fi
;;
"mysql")
debug_log "Creating admin user in MySQL database"
# Check if user already exists
local user_exists
user_exists=$(mysql_client -sNe "SELECT COUNT(*) FROM users WHERE username='$(escape_sql "${admin_username}")';") \
|| { log "ERROR: Cannot query the users table in MySQL database '${DB_NAME}' to create the admin user"; exit 1; }
if [ "${user_exists}" -gt 0 ]; then
log "Admin user '${admin_username}' already exists, skipping creation"
return 0
fi
# Insert admin user
if ! mysql_client -e "INSERT INTO users (username, password, fullname, email, description, perm_templ, active, use_ldap) VALUES ('$(escape_sql "${admin_username}")', '$(escape_sql "${password_hash}")', '$(escape_sql "${admin_fullname}")', '$(escape_sql "${admin_email}")', 'System Administrator', 1, 1, 0);"; then
insert_result=1
fi
;;
"pgsql")
debug_log "Creating admin user in PostgreSQL database"
# Check if user already exists
local user_exists
user_exists=$(psql_client -tAc "SELECT COUNT(*) FROM users WHERE username='$(escape_sql "${admin_username}")';") \
|| { log "ERROR: Cannot query the users table in PostgreSQL database '${DB_NAME}' to create the admin user"; exit 1; }
if [ "${user_exists}" -gt 0 ]; then
log "Admin user '${admin_username}' already exists, skipping creation"
return 0
fi
# Insert admin user
if ! psql_client -c "INSERT INTO users (username, password, fullname, email, description, perm_templ, active, use_ldap) VALUES ('$(escape_sql "${admin_username}")', '$(escape_sql "${password_hash}")', '$(escape_sql "${admin_fullname}")', '$(escape_sql "${admin_email}")', 'System Administrator', 1, 1, 0);"; then
insert_result=1
fi
;;
*)
log "ERROR: Cannot create the admin user: DB_TYPE '${DB_TYPE:-}' is not one of sqlite, mysql, pgsql"
exit 1
;;
esac
if [ $insert_result -eq 0 ]; then
log "Admin user '${admin_username}' created successfully"
# Display credentials prominently if password was generated
if [ "${password_generated}" = "true" ]; then
log "=========================================="
log "IMPORTANT: Admin credentials"
log "Username: ${admin_username}"
log "Password: ${admin_password}"
log "Change this password after the first login."
log "Set PA_ADMIN_PASSWORD or PA_ADMIN_PASSWORD__FILE to keep it out of the logs."
log "=========================================="
fi
else
log "ERROR: Failed to create admin user '${admin_username}'"
exit 1
fi
# Export for use in print_config_summary
export ADMIN_PASSWORD_GENERATED="${password_generated}"
export ADMIN_USERNAME="${admin_username}"
export ADMIN_PASSWORD="${admin_password}"
}
# Generate configuration file from environment variables
generate_config() {
log "Generating configuration from environment variables..."
# Generate a random session key if not provided
local session_key="${PA_SESSION_KEY:-$(openssl rand -hex 32)}"
# Convert boolean values to lowercase
local recaptcha_enabled
recaptcha_enabled=$(to_php_bool "${PA_RECAPTCHA_ENABLED:-false}")
local mail_enabled
mail_enabled=$(to_php_bool "${PA_MAIL_ENABLED:-true}")
local api_enabled
api_enabled=$(to_php_bool "${PA_API_ENABLED:-false}")
local api_basic_auth_enabled
api_basic_auth_enabled=$(to_php_bool "${PA_API_BASIC_AUTH_ENABLED:-false}")
local api_docs_enabled
api_docs_enabled=$(to_php_bool "${PA_API_DOCS_ENABLED:-false}")
local health_enabled
health_enabled=$(to_php_bool "${PA_HEALTH_ENABLED:-false}")
local health_ping_enabled
health_ping_enabled=$(to_php_bool "${PA_HEALTH_PING_ENABLED:-false}")
local metrics_enabled
metrics_enabled=$(to_php_bool "${PA_METRICS_ENABLED:-false}")
local metrics_require_auth
metrics_require_auth=$(to_php_bool "${PA_METRICS_REQUIRE_AUTH:-true}")
local metrics_count_records
metrics_count_records=$(to_php_bool "${PA_METRICS_COUNT_RECORDS:-false}")
local ldap_enabled
ldap_enabled=$(to_php_bool "${PA_LDAP_ENABLED:-false}")
# Convert interface boolean values to lowercase
local web_enabled
web_enabled=$(to_php_bool "${PA_WEB_ENABLED:-true}")
local show_record_id
show_record_id=$(to_php_bool "${PA_SHOW_RECORD_ID:-true}")
local position_record_form_top
position_record_form_top=$(to_php_bool "${PA_POSITION_RECORD_FORM_TOP:-true}")
local position_save_button_top
position_save_button_top=$(to_php_bool "${PA_POSITION_SAVE_BUTTON_TOP:-false}")
local show_zone_comments
show_zone_comments=$(to_php_bool "${PA_SHOW_ZONE_COMMENTS:-true}")
local show_record_comments
show_record_comments=$(to_php_bool "${PA_SHOW_RECORD_COMMENTS:-false}")
local show_zone_limit_column
show_zone_limit_column=$(to_php_bool "${PA_SHOW_ZONE_LIMIT_COLUMN:-false}")
local display_serial_in_zone_list
display_serial_in_zone_list=$(to_php_bool "${PA_DISPLAY_SERIAL_IN_ZONE_LIST:-false}")
local display_signed_serial_in_zone_list
display_signed_serial_in_zone_list=$(to_php_bool "${PA_DISPLAY_SIGNED_SERIAL_IN_ZONE_LIST:-false}")
local display_owner_in_zone_list
display_owner_in_zone_list=$(to_php_bool "${PA_DISPLAY_OWNER_IN_ZONE_LIST:-true}")
local display_group_in_zone_list
display_group_in_zone_list=$(to_php_bool "${PA_DISPLAY_GROUP_IN_ZONE_LIST:-true}")
local display_template_in_zone_list
display_template_in_zone_list=$(to_php_bool "${PA_DISPLAY_TEMPLATE_IN_ZONE_LIST:-false}")
local display_fullname_in_zone_list
display_fullname_in_zone_list=$(to_php_bool "${PA_DISPLAY_FULLNAME_IN_ZONE_LIST:-false}")
local search_group_records
search_group_records=$(to_php_bool "${PA_SEARCH_GROUP_RECORDS:-false}")
local show_pdns_status
show_pdns_status=$(to_php_bool "${PA_SHOW_PDNS_STATUS:-false}")
local add_reverse_record
add_reverse_record=$(to_php_bool "${PA_ADD_REVERSE_RECORD:-true}")
local add_domain_record
add_domain_record=$(to_php_bool "${PA_ADD_DOMAIN_RECORD:-true}")
local show_dashboard_stats
show_dashboard_stats=$(to_php_bool "${PA_SHOW_DASHBOARD_STATS:-true}")
local avatar_oauth_enabled
avatar_oauth_enabled=$(to_php_bool "${PA_AVATAR_OAUTH_ENABLED:-false}")
local avatar_gravatar_enabled
avatar_gravatar_enabled=$(to_php_bool "${PA_AVATAR_GRAVATAR_ENABLED:-false}")
# Convert permission template visibility values to lowercase
local show_user_access_templates
show_user_access_templates=$(to_php_bool "${PA_SHOW_USER_ACCESS_TEMPLATES:-true}")
local show_group_access_templates
show_group_access_templates=$(to_php_bool "${PA_SHOW_GROUP_ACCESS_TEMPLATES:-true}")
local display_hostname_only
display_hostname_only=$(to_php_bool "${PA_DISPLAY_HOSTNAME_ONLY:-false}")
local enable_consistency_checks
enable_consistency_checks=$(to_php_bool "${PA_ENABLE_CONSISTENCY_CHECKS:-false}")
# Convert DNS boolean values to lowercase
local dns_strict_tld_check
dns_strict_tld_check=$(to_php_bool "${PA_DNS_STRICT_TLD_CHECK:-false}")
local dns_top_level_tld_check
dns_top_level_tld_check=$(to_php_bool "${PA_DNS_TOP_LEVEL_TLD_CHECK:-false}")
local dns_third_level_check
dns_third_level_check=$(to_php_bool "${PA_DNS_THIRD_LEVEL_CHECK:-false}")
local dns_txt_auto_quote
dns_txt_auto_quote=$(to_php_bool "${PA_DNS_TXT_AUTO_QUOTE:-false}")
local dns_prevent_duplicate_ptr
dns_prevent_duplicate_ptr=$(to_php_bool "${PA_DNS_PREVENT_DUPLICATE_PTR:-true}")
local dns_bump_serial_on_unchanged_save
dns_bump_serial_on_unchanged_save=$(to_php_bool "${PA_DNS_BUMP_SERIAL_ON_UNCHANGED_SAVE:-true}")
local dns_sync_zone_owner_to_account
dns_sync_zone_owner_to_account=$(to_php_bool "${PA_DNS_SYNC_ZONE_OWNER_TO_ACCOUNT:-false}")
local dns_adopt_zone_owner_from_account
dns_adopt_zone_owner_from_account=$(to_php_bool "${PA_DNS_ADOPT_ZONE_OWNER_FROM_ACCOUNT:-false}")
local dns_parent_zone_ownership_check
dns_parent_zone_ownership_check=$(to_php_bool "${PA_DNS_PARENT_ZONE_OWNERSHIP_CHECK:-true}")
# null means "no template", which '' would not: the setting reads an empty string as a
# real value. The quotes therefore have to be conditional too.
local dns_default_zone_template="null"
if [ -n "${PA_DNS_DEFAULT_ZONE_TEMPLATE:-}" ]; then
dns_default_zone_template=$(php_sq "${PA_DNS_DEFAULT_ZONE_TEMPLATE}")
fi
# Convert DNSSEC boolean values to lowercase
local dnssec_enabled
dnssec_enabled=$(to_php_bool "${PA_DNSSEC_ENABLED:-false}")
# Convert logging boolean values to lowercase
local logging_database_enabled
logging_database_enabled=$(to_php_bool "${PA_LOGGING_DATABASE_ENABLED:-false}")
local logging_syslog_enabled
logging_syslog_enabled=$(to_php_bool "${PA_LOGGING_SYSLOG_ENABLED:-false}")
local logging_require_change_comment
logging_require_change_comment=$(to_php_bool "${PA_LOGGING_REQUIRE_CHANGE_COMMENT:-false}")
local logging_api_request_logging
logging_api_request_logging=$(to_php_bool "${PA_LOGGING_API_REQUEST_LOGGING:-false}")
# Convert password policy boolean values to lowercase
local password_rules_enabled
password_rules_enabled=$(to_php_bool "${PA_PASSWORD_RULES_ENABLED:-true}")
local password_require_uppercase
password_require_uppercase=$(to_php_bool "${PA_PASSWORD_REQUIRE_UPPERCASE:-true}")
local password_require_lowercase
password_require_lowercase=$(to_php_bool "${PA_PASSWORD_REQUIRE_LOWERCASE:-true}")
local password_require_numbers
password_require_numbers=$(to_php_bool "${PA_PASSWORD_REQUIRE_NUMBERS:-true}")
local password_require_special
password_require_special=$(to_php_bool "${PA_PASSWORD_REQUIRE_SPECIAL:-false}")
# Lockout IP lists - comma-separated IPs, CIDRs or wildcards to a PHP array
local lockout_whitelist_ips
lockout_whitelist_ips=$(php_str_array "${PA_LOCKOUT_WHITELIST_IPS:-}")
local lockout_blacklist_ips
lockout_blacklist_ips=$(php_str_array "${PA_LOCKOUT_BLACKLIST_IPS:-}")
# The default contains } so it cannot sit inside a ${VAR:-default} expansion
local password_special_characters='!@#$%^&*()+-=[]{}|;:,.<>?'
if [ -n "${PA_PASSWORD_SPECIAL_CHARACTERS:-}" ]; then
password_special_characters="${PA_PASSWORD_SPECIAL_CHARACTERS}"
fi
# Convert account lockout boolean values to lowercase
local lockout_enabled
lockout_enabled=$(to_php_bool "${PA_LOCKOUT_ENABLED:-false}")
local lockout_track_ip
lockout_track_ip=$(to_php_bool "${PA_LOCKOUT_TRACK_IP:-true}")
local lockout_clear_on_success
lockout_clear_on_success=$(to_php_bool "${PA_LOCKOUT_CLEAR_ON_SUCCESS:-true}")
# Convert password reset/recovery boolean values to lowercase
local password_reset_enabled
password_reset_enabled=$(to_php_bool "${PA_PASSWORD_RESET_ENABLED:-false}")
local username_recovery_enabled
username_recovery_enabled=$(to_php_bool "${PA_USERNAME_RECOVERY_ENABLED:-false}")
# Convert security boolean values to lowercase
local login_token_validation
login_token_validation=$(to_php_bool "${PA_LOGIN_TOKEN_VALIDATION:-true}")
local global_token_validation
global_token_validation=$(to_php_bool "${PA_GLOBAL_TOKEN_VALIDATION:-true}")
local mfa_enforced