-
Notifications
You must be signed in to change notification settings - Fork 29
/
pki-realm
executable file
·2458 lines (1992 loc) · 95.3 KB
/
pki-realm
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
#!/usr/bin/env bash
# pki-realm: client-side PKI management
# Copyright (C) 2016 Maciej Delmanowski <drybjed@gmail.com>
# Homepage: https://debops.org/
set -o nounset -o pipefail -o errexit
umask 022
script="${0}"
script_name="$(basename "${script}")"
pid="$$"
state=()
log_message () {
# Display a message if script is used interactively, otherwise send it to syslog
local msg="${1:-}"
if [ -n "${msg}" ] ; then
if tty -s > /dev/null 2>&1 ; then
echo "${script_name}: ${msg}" 1>&2
elif type logger > /dev/null 2>&1 ; then
logger -t "${script_name}[${pid}]" "${msg}"
fi
fi
}
clean_up () {
# Clean up pidfile
local pidfile="${1}"
if [ -n "${pidfile}" ] && [ -r "${pidfile}" ] ; then
rm -f "${pidfile}"
fi
exit 0
}
wait_for_pid () {
# Wait for the specified process to exit
local pidfile="${1}"
if [ -n "${pidfile}" ] && [ -r "${pidfile}" ] ; then
local wait_pid
wait_pid="$(< "${pidfile}")"
while kill -0 "${wait_pid}" > /dev/null 2>&1 ; do
log_message "Waiting for PID ${wait_pid} to finish"
sleep $(( ( RANDOM % 30 ) + 5 ))
done
if [ -f "${pidfile}" ] ; then
rm -f "${pidfile}"
fi
fi
}
create_lock () {
local pidfile="${1}"
# Try and lock the script operation
echo "${pid}" > "${pidfile}"
sleep 0.5
# Exclusive lock failed
if [ "x$(cat "${pidfile}")" != "x${pid}" ] ; then
log_message "PKI management started by PID $(< "${pidfile}")"
exit 0
fi
# Exclusive lock succeeded
# shellcheck disable=SC2064
trap "clean_up ${pidfile}" EXIT
}
# Inlined in the following scripts: pki-authority, pki-realm {{{
version () {
# Normalize version numbers for comparison.
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
}
key_exists () {
# Check if an associative array has a specified key present
eval '[ ${'$1'[$2]+test_of_existence} ]'
}
get_absolute_path () {
# Get an absolute path to a file
python -c 'import sys, os.path; print(os.path.abspath(sys.argv[1]))' "${1}"
}
get_relative_path () {
# Get a relative path to a file
python -c 'import sys, os.path; print(os.path.relpath(sys.argv[1], sys.argv[2]))' "${1}" "${2:-$PWD}"
}
get_dnsdomainname () {
# Wrap dnsdomainname if it's not present (on MacOS X)
if type dnsdomainname > /dev/null 2>&1 ; then
dnsdomainname
else
local fqdn
fqdn="$(hostname -f)"
if [[ ${fqdn} == *.* ]] ; then
echo "${fqdn#*.}"
else
echo ""
fi
fi
}
chmod_idempotent () {
# chmod does a `fchmodat` (after a `stat`) even if it does not need to
# change anything as of 8.23-4.
# This behavior is not ideal for this script as it would update the ctime.
local new_mode_in_octal="${1##0}"
local file_path="${2}"
if [ "$(stat -c %a "${file_path}")" != "${new_mode_in_octal}" ] ; then
chmod "${new_mode_in_octal}" "${file_path}"
fi
}
chgrp_idempotent () {
# chgrp does a `fchownat` (after a `newfstatat`) even if it does not need
# to change anything as of 8.23-4.
# This behavior is not ideal for this script as it would update the ctime.
local new_group_name="${1}"
local file_path="${2}"
if [ "$(stat -c %G "${file_path}")" != "${new_group_name}" ] ; then
chgrp "${new_group_name}" "${file_path}"
fi
}
# }}}
initialize_environment () {
declare -gA config
config["pki_authority_preference"]="external/acme/internal/selfsigned"
config["acme_user"]="${PKI_ACME_USER:-pki-acme}"
config["acme_group"]="${PKI_ACME_GROUP:-pki-acme}"
config["private_group"]="${PKI_PRIVATE_GROUP:-ssl-cert}"
config["acme_challenge_dir"]="/srv/www/sites/acme/public/.well-known/acme-challenge"
config["acme_client"]="acme_tiny"
local -A acme_client_script_map
acme_client_script_map=(
["acme_tiny"]="/usr/local/lib/pki/acme-tiny"
)
config["acme_client_script"]="${acme_client_script_map[${config['acme_client']}]}"
config["acme_ca"]="le-staging"
local -A acme_ca_api_map
acme_ca_api_map=(
["le-live"]="https://acme-v01.api.letsencrypt.org"
["le-staging"]="https://acme-staging.api.letsencrypt.org"
)
config["acme_ca_api"]="${acme_ca_api_map[${config['acme_ca']}]}"
config["acme_expiration_days"]="30"
config["acme_expiration_seconds"]="$(( 60 * 60 * 24 * ${config['acme_expiration_days']} ))"
config["acme_root_ca_path"]="/usr/share/ca-certificates"
config["acme_root_ca_file"]="mozilla/DST_Root_CA_X3.crt"
config["pki_default_ca_bundle"]="/etc/ssl/certs/ca-certificates.crt"
config["pki_default_subject"]="CN=$(hostname -f)"
config["pki_default_fqdn"]="$(hostname -f)"
config["pki_default_domain"]="$(get_dnsdomainname)"
config["pki_default_subdomains"]="_wildcard_"
config["pki_dhparam"]="false"
config["dhparam_file"]=""
config["realm_key_size"]="2048"
config["internal_expiration_days"]="14"
config["internal_expiration_seconds"]="$(( 60 * 60 * 24 * ${config['internal_expiration_days']} ))"
config["selfsigned_sign_days"]="365"
config["selfsigned_sign_seconds"]="$(( 60 * 60 * 24 * ${config['selfsigned_sign_days']} ))"
config["selfsigned_expiration_days"]="14"
config["selfsigned_expiration_seconds"]="$(( 60 * 60 * 24 * ${config['selfsigned_expiration_days']} ))"
config["subject"]=""
config["domains"]=""
config["subdomains"]=""
config["subject_alt_names"]=""
config["acme_default_subject"]="CN=$(get_dnsdomainname)"
config["acme_default_domain"]="$(get_dnsdomainname)"
config["acme_default_subdomains"]="www/ftp/mail/smtp/imap"
config["acme_subject"]=""
config["acme_domains"]=""
config["acme_subdomains"]=""
config["acme_alt_names"]=""
if [ "$EUID" -eq 0 ] ; then
config["lock_dir"]="/run"
config["pki_root"]="${PKI_ROOT:-/etc/pki}"
config["pki_library"]="${PKI_LIBRARY:-gnutls}"
config["pki_acme"]="${PKI_ACME:-true}"
config["pki_acme_library"]="${PKI_ACME_LIBRARY:-openssl}"
config["pki_internal"]="${PKI_INTERNAL:-true}"
config["pki_realms"]="${config['pki_root']}/realms"
config["pki_hooks"]="${config['pki_root']}/hooks"
config["public_dir_group"]="root"
config["public_file_group"]="root"
config["realm_dir_group"]="root"
config["realm_file_group"]="root"
if getent group "${config['private_group']}" > /dev/null ; then
config["private_dir_group"]="${config['private_group']}"
config["private_file_group"]="${config['private_group']}"
else
config["private_dir_group"]="root"
config["private_file_group"]="root"
fi
if getent group "${config['acme_group']}" > /dev/null ; then
config["acme_dir_group"]="${config['acme_group']}"
config["acme_file_group"]="${config['acme_group']}"
else
config["acme_dir_group"]="root"
config["acme_file_group"]="root"
fi
config["public_dir_mode"]="755"
config["acme_dir_mode"]="775"
config["private_dir_mode"]="750"
config["realm_dir_mode"]="700"
config["public_file_mode"]="644"
config["acme_file_mode"]="640"
config["private_file_mode"]="640"
config["realm_file_mode"]="400"
else
config["lock_dir"]="${XDG_RUNTIME_DIR:-/tmp}"
config["pki_root"]="${PKI_ROOT:-${XDG_CONFIG_HOME:-${HOME}/.config}/pki}"
config["pki_library"]="${PKI_LIBRARY:-gnutls}"
config["pki_acme"]="${PKI_ACME:-true}"
config["pki_acme_library"]="${PKI_ACME_LIBRARY:-openssl}"
config["pki_internal"]="${PKI_INTERNAL:-true}"
config["pki_realms"]="${config['pki_root']}/realms"
config["pki_hooks"]="${config['pki_root']}/hooks"
config["public_dir_group"]="$(id -g)"
config["public_file_group"]="$(id -g)"
config["private_dir_group"]="$(id -g)"
config["private_file_group"]="$(id -g)"
config["realm_dir_group"]="$(id -g)"
config["realm_file_group"]="$(id -g)"
config["acme_dir_group"]="$(id -g)"
config["acme_file_group"]="$(id -g)"
config["public_dir_mode"]="755"
config["acme_dir_mode"]="775"
config["private_dir_mode"]="700"
config["realm_dir_mode"]="700"
config["public_file_mode"]="644"
config["acme_file_mode"]="640"
config["private_file_mode"]="600"
config["realm_file_mode"]="400"
fi
config["private_dir_acl_groups_x"]=""
config["private_file_acl_groups_r"]=""
mkdir -p "${config['pki_root']}"
chmod_idempotent "${config['public_dir_mode']}" "${config['pki_root']}"
}
check_openssl_key_req_match () {
local check_key="${1}"
local check_req="${2}"
local key_modulus
local req_modulus
key_modulus="$(openssl rsa -noout -modulus -in "${check_key}" | base64)"
req_modulus="$(openssl req -noout -modulus -in "${check_req}" | base64)"
if [ "${key_modulus}" = "${req_modulus}" ] ; then
return 0
else
return 1
fi
}
check_openssl_key_crt_match () {
local check_key="${1}"
local check_crt="${2}"
if [ -r "${check_key}" ] && [ -r "${check_crt}" ] ; then
local key_modulus
local crt_modulus
key_modulus="$(openssl rsa -noout -modulus -in "${check_key}" | base64)"
crt_modulus="$(openssl x509 -noout -modulus -in "${check_crt}" | base64)"
if [ "${key_modulus}" = "${crt_modulus}" ] ; then
return 0
else
return 1
fi
else
return 1
fi
}
update_file_signature () {
local check_file="${1}"
if [ -n "${check_file}" ] && [ -r "${check_file}" ] ; then
if [ -r "${check_file}.sig" ] ; then
set +e
md5sum --quiet --status -c "${check_file}.sig"
local rc=$?
set -e
if [ ${rc} -ne 0 ] ; then
rm -rf "${check_file}.sig"
md5sum "${check_file}" > "${check_file}.sig"
fi
else
md5sum "${check_file}" > "${check_file}.sig"
fi
fi
}
check_file_signature () {
local check_file="${1}"
if [ -n "${check_file}" ] && [ -r "${check_file}" ] ; then
if [ -r "${check_file}.sig" ] ; then
set +e
md5sum --quiet --status -c "${check_file}.sig"
local rc=$?
set -e
return "${rc}"
fi
fi
return 1
}
enter_realm () {
local realm="${1}"
local config_file="${2:-config/realm.conf}"
mkdir -p "${config['pki_hooks']}"
chmod_idempotent "${config['public_dir_mode']}" "${config['pki_hooks']}"
if [ ! -d "${config['pki_realms']}/${realm}" ] ; then
mkdir -p "${config['pki_realms']}/${realm}"
chmod "${config['public_dir_mode']}" "${config['pki_realms']}/${realm}"
state+=('new-realm')
fi
cd "${config['pki_realms']}/${realm}"
local rc=$?
if [ -r "${config_file}" ] ; then
# FIXME: Add a code that checks if the config file has no dangerous code inside
# shellcheck disable=SC1090
. "${config_file}"
fi
return "${rc}"
}
run_pki_hooks () {
if [ -n "${state:-}" ] && [ -d "${config['pki_hooks']}" ] ; then
export PKI_SCRIPT_REALM="${config['name']}"
export PKI_SCRIPT_FQDN="${config['pki_default_fqdn']}"
export PKI_SCRIPT_SUBJECT="${config['subject']:-${config['pki_default_subject']}}"
export PKI_SCRIPT_DOMAINS="${config['domains']:-${config['pki_default_domain']}}"
export PKI_SCRIPT_SUBDOMAINS="${config['subdomains']:-${config['pki_default_subdomains']}}"
export PKI_SCRIPT_PRIVATE_KEY
PKI_SCRIPT_PRIVATE_KEY="$(get_absolute_path private/key.pem)"
export PKI_SCRIPT_DEFAULT_CRT
export PKI_SCRIPT_DEFAULT_KEY
export PKI_SCRIPT_DEFAULT_PEM
PKI_SCRIPT_DEFAULT_CRT="$(get_absolute_path default.crt)"
PKI_SCRIPT_DEFAULT_KEY="$(get_absolute_path default.key)"
PKI_SCRIPT_DEFAULT_PEM="$(get_absolute_path default.pem)"
local current_state
current_state=$(printf ",%s" "${state[@]:-}")
current_state="${current_state:1}"
export PKI_SCRIPT_STATE="${current_state}"
cd "${config['pki_hooks']}"
( run-parts . )
cd - > /dev/null 2>&1
fi
}
run_external_script () {
if [ -r external/script ] ; then
if [ ! -r private/realm_key.pem ] ; then
generate_${config['pki_library']}_rsa_realm_key private/realm_key.pem
# FIXME: Redundancy! Why?
if [ ! -r private/key.pem ] ; then
install -g "${config['private_file_group']}" -m "${config['private_file_mode']}" /dev/null private/key.pem.tmp
if type setfacl > /dev/null 2>&1 ; then
local private_file_acl_groups_r=( $(echo "${config['private_file_acl_groups_r']}" | tr "/" " ") )
if [ ${#private_file_acl_groups_r[@]} -ne 0 ] ; then
for acl_group in "${private_file_acl_groups_r[@]}" ; do
if getent group "${acl_group}" > /dev/null ; then
setfacl -m "g:${acl_group}:r" private/key.pem.tmp
fi
done
fi
fi
cat private/realm_key.pem >> private/key.pem.tmp
mv private/key.pem.tmp private/key.pem
fi
fi
export PKI_SCRIPT_REALM="${config['name']}"
export PKI_SCRIPT_FQDN="${config['pki_default_fqdn']}"
export PKI_SCRIPT_SUBJECT="${config['subject']:-${config['pki_default_subject']}}"
export PKI_SCRIPT_DOMAINS="${config['domains']:-${config['pki_default_domain']}}"
export PKI_SCRIPT_SUBDOMAINS="${config['subdomains']:-${config['pki_default_subdomains']}}"
# FIXME: Redundancy! Why?
export PKI_SCRIPT_PRIVATE_KEY
PKI_SCRIPT_PRIVATE_KEY="$(get_absolute_path private/key.pem)"
export PKI_SCRIPT_DEFAULT_CRT
export PKI_SCRIPT_DEFAULT_KEY
export PKI_SCRIPT_DEFAULT_PEM
PKI_SCRIPT_DEFAULT_CRT="$(get_absolute_path default.crt)"
PKI_SCRIPT_DEFAULT_KEY="$(get_absolute_path default.key)"
PKI_SCRIPT_DEFAULT_PEM="$(get_absolute_path default.pem)"
local current_state
current_state=$(printf ",%s" "${state[@]:-}")
current_state="${current_state:1}"
export PKI_SCRIPT_STATE="${current_state}"
test -x external/script || chmod ug+x external/script
cd external
( ./script )
cd - > /dev/null 2>&1
fi
}
update_symlink () {
local symlink_target="${1}"
if [ -n "${symlink_target}" ] ; then
shift 1
for target in "${@}" ; do
if [ -r "${target}" ] ; then
if [ -L "${symlink_target}" ] && [ "$(readlink "${symlink_target}")" == "${target}" ] ; then
break
else
ln -sf "${target}" "${symlink_target}"
fi
break
fi
done
if [ ! -e "${symlink_target}" ] ; then
rm -f "${symlink_target}"
fi
fi
}
create_openssl_config () {
local config_file="${1:-config/openssl.conf}"
local config_type="${2:-internal}"
local req_dn="${3}"
local req_domains="${4}"
local req_subdomains="${5:-}"
local req_san="${6:-}"
if [ -n "${config_file}" ] && [ -n "${req_dn}" ] ; then
cat << EOF > "${config_file}"
# Configuration file generated by pki-realm
[ req ]
default_md = sha256
default_bits = ${config['realm_key_size']}
default_keyfile = private/key.pem
prompt = no
encrypt_key = no
distinguished_name = req_dn
req_extensions = ext_req
utf8 = yes
string_mask = utf8only
EOF
if [ -n "${PKI_SESSION_TOKEN:-}" ] && [ "${config_type}" = "internal" ] ; then
cat << EOF >> "${config_file}"
attributes = req_attributes
[ req_attributes ]
challengePassword = \${ENV::PKI_SESSION_TOKEN}
EOF
fi
# shellcheck disable=SC2129
cat << EOF >> "${config_file}"
[ req_dn ]
EOF
echo "${req_dn}" | tr "/" "\n" | grep --invert-match '^\s*$' | sed \
-e 's/^[Cc]=/countryName=/' \
-e 's/^[Ss][Tt]=/stateOrProvinceName=/' \
-e 's/^[Ll]=/localityName=/' \
-e 's/^[Oo]=/organizationName=/' \
-e 's/^[Oo][Uu]=/organizationalUnitName=/' \
-e 's/^[Cc][Nn]=/commonName=/' >> "${config_file}"
cat << EOF >> "${config_file}"
[ ext_req ]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth, clientAuth
EOF
if [ -n "${req_domains}" ] || [ -n "${req_subdomains}" ] || [ -n "${req_san}" ] ; then
cat << EOF >> "${config_file}"
subjectAltName = @ext_req_san
[ ext_req_san ]
EOF
local domains=( $(echo "${req_domains}" | tr "/" " ") )
local subdomains=( $(echo "${req_subdomains}" | tr "/" " ") )
local san_ip=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^ip://pi' | xargs ) )
local san_dns=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^dns://pi' | xargs ) )
local san_uri=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^uri://pi' | xargs ) )
local san_email=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^email://pi' | xargs ) )
local all_dns=( ${domains[@]:-} )
if [ ${#subdomains[@]} -gt 0 ] ; then
for i in "${!domains[@]}" ; do
for j in "${!subdomains[@]}" ; do
if [ "${subdomains[${j}]}" == "_wildcard_" ] ; then
all_dns+=( "*.${domains[${i}]}" )
else
all_dns+=( "${subdomains[${j}]}.${domains[${i}]}" )
fi
done
done
fi
all_dns+=( ${san_dns[@]:-} )
for i in "${!all_dns[@]}" ; do
printf "%-18s = %s\n" "DNS.${i}" "${all_dns[${i}]}" >> "${config_file}"
done
for i in "${!san_ip[@]}" ; do
printf "%-18s = %s\n" "IP.${i}" "${san_ip[${i}]}" >> "${config_file}"
done
for i in "${!san_uri[@]}" ; do
printf "%-18s = %s\n" "URI.${i}" "${san_uri[${i}]}" >> "${config_file}"
done
for i in "${!san_email[@]}" ; do
printf "%-18s = %s\n" "email.${i}" "${san_email[${i}]}" >> "${config_file}"
done
fi
fi
}
create_gnutls_config () {
local config_file="${1:-config/gnutls.conf}"
local config_type="${2:-internal}"
local req_dn="${3}"
local req_domains="${4}"
local req_subdomains="${5:-}"
local req_san="${6:-}"
if [ -n "${config_file}" ] && [ -n "${req_dn}" ] ; then
cat << EOF > "${config_file}"
# Configuration file generated by pki-realm
signing_key
encryption_key
tls_www_client
tls_www_server
EOF
if [ -n "${PKI_SESSION_TOKEN:-}" ] && [ "${config_type}" = "internal" ] ; then
cat << EOF >> "${config_file}"
# This password is meaningless outside of current session
challenge_password = ${PKI_SESSION_TOKEN:-}
EOF
elif [ -n "${PKI_SESSION_TOKEN:-}" ] && [ "${config_type}" = "selfsigned" ] ; then
cat << EOF >> "${config_file}"
expiration_days = ${config['selfsigned_sign_days']}
EOF
fi
echo "${req_dn}" | tr "/" "\n" | grep --invert-match '^\s*$' | sed \
-e 's/^[Cc]=/country = "/' \
-e 's/^[Ss][Tt]=/state = "/' \
-e 's/^[Ll]=/locality = "/' \
-e 's/^[Oo]=/organization = "/' \
-e 's/^[Oo][Uu]=/unit = "/' \
-e 's/^[Cc][Nn]=/cn = "/' \
-e 's/$/"/' >> "${config_file}"
local domains=( $(echo "${req_domains}" | tr "/" " ") )
local subdomains=( $(echo "${req_subdomains}" | tr "/" " ") )
local san_ip=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^ip://pi' | xargs ) )
local san_dns=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^dns://pi' | xargs ) )
local san_uri=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^uri://pi' | xargs ) )
local san_email=( $( echo "${req_san}" | tr "|" "\n" | sed --quiet -e 's/^email://pi' | xargs ) )
local all_dns=( ${domains[@]:-} )
if [ ${#subdomains[@]} -gt 0 ] ; then
for i in "${!domains[@]}" ; do
for j in "${!subdomains[@]}" ; do
if [ "${subdomains[${j}]}" == "_wildcard_" ] ; then
all_dns+=( "*.${domains[${i}]}" )
else
all_dns+=( "${subdomains[${j}]}.${domains[${i}]}" )
fi
done
done
fi
all_dns+=( ${san_dns[@]:-} )
for i in "${!all_dns[@]}" ; do
printf 'dns_name = "%s"\n' "${all_dns[${i}]}" >> "${config_file}"
done
for i in "${!san_ip[@]}" ; do
printf 'ip_address = "%s"\n' "${san_ip[${i}]}" >> "${config_file}"
done
if [ "$(version "$(certtool --version | head -n 1 | awk '{print $NF}')")" -ge "$(version 3.0.20)" ] ; then
for i in "${!san_uri[@]}" ; do
printf 'uri = "%s"\n' "${san_uri[${i}]}" >> "${config_file}"
done
fi
for i in "${!san_email[@]}" ; do
printf 'email = "%s"\n' "${san_email[${i}]}" >> "${config_file}"
done
fi
}
save_realm_config () {
local config_file="${1:-config/realm.conf}"
if [ -r "${config_file}" ] ; then
if grep -q -E "^#\s+Configuration\s+file\s+generated\s+by\s+pki-realm$" "${config_file}" ; then
rm -f "${config_file}"
fi
fi
if [ ! -r "${config_file}" ] ; then
cat << EOF > "${config_file}"
# Configuration file generated by pki-realm
EOF
for key in "${!config[@]}" ; do
echo "config['${key}']='${config[${key}]}'" >> "${config_file}"
done
fi
}
generate_openssl_request () {
local req_config="${1}"
local req_out="${2}"
if [ -n "${req_config}" ] && [ -n "${req_out}" ] ; then
if [ -r "${req_config}" ] && [ ! -r "${req_out}" ] ; then
local req_keyfile
req_keyfile=$(grep -E '^default_keyfile\s+=\s+' "${req_config}" | awk '{print $3}')
if [ -n "${req_keyfile}" ] ; then
openssl req -new -key "${req_keyfile}" -config "${req_config}" -out "${req_out}.tmp"
else
openssl req -new -config "${req_config}" -out "${req_out}.tmp"
fi
test -r "${req_out}.tmp" && mv "${req_out}.tmp" "${req_out}"
fi
fi
}
generate_gnutls_request () {
local req_config="${1}"
local req_out="${2}"
if [ -n "${req_config}" ] && [ -n "${req_out}" ] ; then
if [ -r "${req_config}" ] && [ ! -r "${req_out}" ] ; then
certtool --generate-request --template "${req_config}" \
--load-privkey private/key.pem --outfile "${req_out}.tmp"
# Remove text output from the request
test -r "${req_out}.tmp" && sed -n -i '/-----BEGIN NEW CERTIFICATE REQUEST-----/,$ p' "${req_out}.tmp"
test -r "${req_out}.tmp" && mv "${req_out}.tmp" "${req_out}"
fi
fi
}
selfsign_openssl_request () {
local sign_req="selfsigned/request.pem"
local sign_out="selfsigned/cert.pem"
local root_out="selfsigned/root.pem"
if [ -n "${sign_req}" ] && [ -n "${sign_out}" ] ; then
if [ -r "${sign_req}" ] && [ ! -r "${sign_out}" ] ; then
openssl x509 -in "${sign_req}" -out "${sign_out}.tmp" \
-req -signkey private/key.pem -days "${config['selfsigned_sign_days']}"
test -r "${sign_out}.tmp" && mv "${sign_out}.tmp" "${sign_out}"
if [ -r "${sign_out}" ] && [ ! -r "${root_out}" ] ; then
cd "$(dirname "${root_out}")"
ln -s "${sign_out}" "${root_out}"
cd - > /dev/null
fi
fi
fi
}
selfsign_gnutls_request () {
local sign_req="selfsigned/request.pem"
local sign_out="selfsigned/cert.pem"
local root_out="selfsigned/root.pem"
if [ -n "${sign_req}" ] && [ -n "${sign_out}" ] ; then
if [ -r "${sign_req}" ] && [ ! -r "${sign_out}" ] ; then
certtool --generate-self-signed --load-privkey private/key.pem \
--load-request "${sign_req}" --outfile "${sign_out}.tmp" \
--template selfsigned/gnutls.conf
test -r "${sign_out}.tmp" && mv "${sign_out}.tmp" "${sign_out}"
if [ -r "${sign_out}" ] && [ ! -r "${root_out}" ] ; then
cd "$(dirname "${root_out}")"
ln -s "$(basename ${sign_out})" "$(basename "${root_out}")"
cd - > /dev/null
fi
fi
fi
}
generate_openssl_rsa_realm_key () {
local key_file="${1:-private/realm_key.pem}"
local key_size="${2:-${config['realm_key_size']}}"
local key_group="${3:-${config['realm_file_group']}}"
test -r "${key_file}" || openssl genrsa -out "${key_file}.tmp" "${key_size}"
if [ -r "${key_file}.tmp" ] ; then
chmod "${config['realm_file_mode']}" "${key_file}.tmp"
chgrp "${key_group}" "${key_file}.tmp"
mv "${key_file}.tmp" "${key_file}"
fi
chmod_idempotent "${config['realm_file_mode']}" "${key_file}"
chgrp_idempotent "${key_group}" "${key_file}"
}
generate_gnutls_rsa_realm_key () {
local key_file="${1:-private/realm_key.pem}"
local key_size="${2:-${config['realm_key_size']}}"
local key_group="${3:-${config['realm_file_group']}}"
if ! [ -r "${key_file}" ] ; then
if [ "$(version "$(certtool --version | head -n 1 | awk '{print $NF}')")" -lt "$(version 3.1.0)" ] ; then
certtool --generate-privkey --outfile "${key_file}.tmp" --bits "${key_size}"
else
certtool --generate-privkey --rsa --outfile "${key_file}.tmp" --bits "${key_size}"
fi
fi
if [ -r "${key_file}.tmp" ] ; then
sed -n -i '/-----BEGIN RSA PRIVATE KEY-----/,$ p' "${key_file}.tmp"
chmod "${config['realm_file_mode']}" "${key_file}.tmp"
chgrp "${key_group}" "${key_file}.tmp"
mv "${key_file}.tmp" "${key_file}"
fi
chmod_idempotent "${config['realm_file_mode']}" "${key_file}"
chgrp_idempotent "${key_group}" "${key_file}"
}
generate_openssl_rsa_private_key () {
local key_file="${1:-private/key.pem}"
local key_size="${2:-${config['realm_key_size']}}"
local key_group="${3:-${config['private_file_group']}}"
local key_acl="${4:-true}"
test -r "${key_file}" || openssl genrsa -out "${key_file}.tmp" "${key_size}"
if [ -r "${key_file}.tmp" ] ; then
chmod "${config['private_file_mode']}" "${key_file}.tmp"
chgrp "${key_group}" "${key_file}.tmp"
if [ "${key_acl}" = "true" ] ; then
if type setfacl > /dev/null 2>&1 ; then
local private_file_acl_groups_r=( $(echo "${config['private_file_acl_groups_r']}" | tr "/" " ") )
if [ ${#private_file_acl_groups_r[@]} -ne 0 ] ; then
for acl_group in "${private_file_acl_groups_r[@]}" ; do
if getent group "${acl_group}" > /dev/null ; then
setfacl -m "g:${acl_group}:r" "${key_file}.tmp"
fi
done
fi
fi
fi
mv "${key_file}.tmp" "${key_file}"
fi
chmod_idempotent "${config['private_file_mode']}" "${key_file}"
chgrp_idempotent "${key_group}" "${key_file}"
}
generate_gnutls_rsa_private_key () {
local key_file="${1:-private/key.pem}"
local key_size="${2:-${config['realm_key_size']}}"
local key_group="${3:-${config['private_file_group']}}"
local key_acl="${4:-true}"
if ! [ -r "${key_file}" ] ; then
if [ "$(version "$(certtool --version | head -n 1 | awk '{print $NF}')")" -lt "$(version 3.1.0)" ] ; then
certtool --generate-privkey --outfile "${key_file}.tmp" --bits "${key_size}"
else
certtool --generate-privkey --rsa --outfile "${key_file}.tmp" --bits "${key_size}"
fi
fi
if [ -r "${key_file}.tmp" ] ; then
sed -n -i '/-----BEGIN RSA PRIVATE KEY-----/,$ p' "${key_file}.tmp"
chmod "${config['private_file_mode']}" "${key_file}.tmp"
chgrp "${key_group}" "${key_file}.tmp"
if [ "${key_acl}" = "true" ] ; then
if type setfacl > /dev/null 2>&1 ; then
local private_file_acl_groups_r=( $(echo "${config['private_file_acl_groups_r']}" | tr "/" " ") )
if [ ${#private_file_acl_groups_r[@]} -ne 0 ] ; then
for acl_group in "${private_file_acl_groups_r[@]}" ; do
if getent group "${acl_group}" > /dev/null ; then
setfacl -m "g:${acl_group}:r" "${key_file}.tmp"
fi
done
fi
fi
fi
mv "${key_file}.tmp" "${key_file}"
fi
chmod_idempotent "${config['private_file_mode']}" "${key_file}"
chgrp_idempotent "${key_group}" "${key_file}"
}
create_public_directories () {
local dir_group="${1}"
if [ -n "${dir_group}" ] ; then
shift
else
return 1
fi
local directories=( ${@} )
for directory in "${directories[@]}" ; do
mkdir -p "${directory}"
chmod_idempotent "${config['public_dir_mode']}" "${directory}"
chgrp_idempotent "${dir_group}" "${directory}"
done
}
create_private_directories () {
local dir_group="${1}"
if [ -n "${dir_group}" ] ; then
shift
else
return 1
fi
local directories=( ${@} )
for directory in "${directories[@]}" ; do
mkdir -p "${directory}"
chmod_idempotent "${config['private_dir_mode']}" "${directory}"
chgrp_idempotent "${dir_group}" "${directory}"
if type setfacl > /dev/null 2>&1 ; then
local private_dir_acl_groups_x=( $(echo "${config['private_dir_acl_groups_x']}" | tr "/" " ") )
if [ ${#private_dir_acl_groups_x[@]} -ne 0 ] ; then
for acl_group in "${private_dir_acl_groups_x[@]}" ; do
if getent group "${acl_group}" > /dev/null ; then
setfacl -m "g:${acl_group}:x" "${directory}"
fi
done
fi
fi
done
}
create_private_noacl_directories () {
local dir_group="${1}"
if [ -n "${dir_group}" ] ; then
shift
else
return 1
fi
local directories=( ${@} )
for directory in "${directories[@]}" ; do
mkdir -p "${directory}"
chmod_idempotent "${config['private_dir_mode']}" "${directory}"
chgrp_idempotent "${dir_group}" "${directory}"
done
}
check_expiration_time () {