-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.sh
executable file
·1853 lines (1584 loc) · 49.6 KB
/
utils.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
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
TWO_FA_LIB_DIR=`cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd`
. "$TWO_FA_LIB_DIR/pkcs11_utils.sh"
NUMBER_REGEXP='^[0123456789abcdefABCDEF]+$'
CUR_DIR=`pwd`
if [[ -z "$LOG_FILE" ]]
then
LOG_FILE="$CUR_DIR/log.txt"
fi
RTADMIN=rtAdmin
OPENSSL="openssl"
echolog()
{
echo -e [`date` $USER $$] "$@" >> "$LOG_FILE"
}
INIT_CALLED=0
function init()
{
echolog "init"
if [[ -f "/etc/os-release" ]]
then
source /etc/os-release
OS_NAME=$NAME
else
OS="`uname -s`"
if [ "${OS}" = "Darwin" ]
then
OS_NAME="OS X"
fi
realpath() {
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}
fi
echolog "OS: $OS_NAME"
OS_ARCH=`uname -m`
echolog "ARCH: $OS_ARCH"
if [ -f "/etc/debian_version" ]; then
echolog "set env for debian impl"
LIBRTPKCS11ECP=/usr/lib/librtpkcs11ecp.so
PAM_PKCS11_DIR=/etc/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/debian"
fi
if [ -f "/etc/redhat-release" ]; then
echolog "set env for redhat impl"
LIBRTPKCS11ECP=/usr/lib64/librtpkcs11ecp.so
PAM_PKCS11_DIR=/etc/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/redhat/"
fi
case $OS_NAME in
"RED OS")
echolog "set env for red os impl"
LIBRTPKCS11ECP=/usr/lib64/librtpkcs11ecp.so
PAM_PKCS11_DIR=/etc/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/redos/"
;;
*"Astra Linux"*)
echolog "set env for astra impl"
LIBRTPKCS11ECP=/usr/lib/librtpkcs11ecp.so
PAM_PKCS11_DIR=/etc/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/astra"
;;
*"ALT"*)
echolog "set env for alt impl"
LIBRTPKCS11ECP=/usr/lib64/librtpkcs11ecp.so
PAM_PKCS11_DIR=/etc/security/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/alt"
;;
*"ROSA"*)
echolog "set env for rosa impl"
LIBRTPKCS11ECP=/usr/lib64/librtpkcs11ecp.so
PAM_PKCS11_DIR=/etc/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/rosa"
;;
*"MagOS"*)
echolog "set env for magos impl"
LIBRTPKCS11ECP=/usr/lib64/librtpkcs11ecp.so
PAM_PKCS11_DIR=/etc/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/rosa"
;;
"OS X")
echolog "set env for os x impl"
LIBRTPKCS11ECP="/Library/Aktiv Co/Rutoken ECP/rtpkcs11ecp.framework/rtpkcs11ecp"
PAM_PKCS11_DIR=/etc/pam_pkcs11
IPA_NSSDB_DIR=/etc/pki/nssdb
IMPL_DIR="$TWO_FA_LIB_DIR/implementation/macos"
OPENSSL=`ls /usr/local/Cellar/openssl@*/*/bin/openssl`
;;
esac
echolog "setup impl"
. "$IMPL_DIR/setup.sh"
update_openssl_engines_path
local GUESS_LIBRTPKCS11ECP=`whereis librtpkcs11ecp | awk '{print $2}'`
if ! [[ -z "$GUESS_LIBRTPKCS11ECP" ]]
then
echolog "auto setup pkcs11 lib: $GUESS_LIBRTPKCS11ECP"
LIBRTPKCS11ECP="$GUESS_LIBRTPKCS11ECP"
fi
if [ -f "/etc/debian_version" ]
then
local pam_p11_ver=`dpkg -s libpam-p11 | grep "Version" | cut -f 2 -d " "`
dpkg --compare-versions "$pam_p11_ver" "ge" "0.2.0"
if [[ $? -eq 0 ]]
then
PAM_P11=pam_p11.so
else
PAM_P11=pam_p11_opensc.so
fi
fi
case $XDG_CURRENT_DESKTOP in
*"GNOME")
echolog "set env for gnome"
SCREENSAVER_NAME="xdg-screensaver"
LOCK_SCREEN_CMD="xdg-screensaver lock"
;;
"MATE")
echolog "set env for mate"
SCREENSAVER_NAME="mate-screensaver"
LOCK_SCREEN_CMD="mate-screensaver-command --lock"
;;
"X-Cinnamon")
echolog "set env for cinnamon"
SCREENSAVER_NAME="cinnamon-screensaver"
LOCK_SCREEN_CMD="cinnamon-screensaver-command --lock"
;;
"fly")
echolog "set env for fly"
SCREENSAVER_NAME=""
LOCK_SCREEN_CMD="fly-wmfunc FLYWM_LOCK"
;;
"KDE")
echolog "set env for kde"
SCREENSAVER_NAME="kde"
LOCK_SCREEN_CMD="qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock"
;;
"XFCE*" | "xfce*")
echolog "set env for xfce"
SCREENSAVER_NAME="xfce4-screensaver"
LOCK_SCREEN_CMD="xfce4-command --lock"
;;
esac
echolog "init gui manager"
init_gui_manager
if [[ "$INIT_CALLED" -eq 0 ]]
then
SCRIPT=`realpath "$0"`
echolog "script path: $SCRIPT"
SCRIPT_DIR=`dirname "$SCRIPT"`
INIT_CALLED=1
cd $(mktemp -d);
TMP_DIR=`pwd`
fi
return 0
}
function init_gui_manager ()
{
echolog "init gui manager"
case "$GUI_MANAGER" in
"dialog")
echolog "setup impl: dialog gui"
. "$TWO_FA_LIB_DIR/dialog.sh"
;;
"python")
echolog "setup impl: pathon gui"
. "$TWO_FA_LIB_DIR/python_gui.sh"
;;
*)
echolog "setup impl: default(python)"
. "$TWO_FA_LIB_DIR/python_gui.sh"
;;
esac
return 0
}
function update_openssl_engines_path ()
{
echolog "Start searching engine dir"
if [ -f "/etc/debian_version" ]; then
ENGINE_DIR=`ls -d /usr/lib/$OS_ARCH-linux-gnu/engines* 2> /dev/null`
fi
if [ -f "/etc/redhat-release" ]; then
ENGINE_DIR=`ls -d /usr/lib64/engines* 2> /dev/null`
fi
case $OS_NAME in
"RED OS")
ENGINE_DIR="/usr/lib64/engines-1.1"
;;
*"Astra Linux"*)
ENGINE_DIR="/usr/lib/$OS_ARCH-linux-gnu/engines-1.1"
;;
*"ALT"*)
ENGINE_DIR=`ls -d /usr/lib64/openssl/engines*`
;;
*"ROSA"*)
ENGINE_DIR="/usr/lib64/openssl-1.0.0/engines"
;;
*"MagOS"*)
ENGINE_DIR="/usr/lib64/openssl-1.0.0/engines"
;;
"OS X")
ENGINE_DIR=`ls -d /usr/local/Cellar/libp11/*/lib/engines-*`
PKCS11_ENGINE=`echo "${ENGINE_DIR}/pkcs11.dylib"`
RTENGINE=`echo "${ENGINE_DIR}/librtengine.dylib"`
;;
esac
if ! [[ -d "$ENGINE_DIR" ]]
then
if [[ -d "`ls -d /usr/lib64//engines*`" ]]
then
ENGINE_DIR=`ls -d /usr/lib64/engines*`
fi
if [[ -d "`ls -d /usr/lib64/openssl*/engines*`" ]]
then
ENGINE_DIR=`ls -d /usr/lib64/openssl*/engines*`
fi
if [[ -d "`ls -d /usr/lib/$OS_ARCH-linux-gnu/engines*`" ]]
then
ENGINE_DIR=`ls -d /usr/lib/$OS_ARCH-linux-gnu/engines*`
fi
fi
ENGINE_DIR_GUESS=`$OPENSSL version -a | grep "ENGINESDIR" | tail -1 | cut -d ":" -f 2 | tr -d '"' | awk '{$1=$1};1'`
if [[ -f "$ENGINE_DIR_GUESS" ]]
then
ENGINE_DIR="$ENGINE_DIR_GUESS"
fi
if [[ "$ENGINE_DIR" ]] && ! [[ "$OS_NAME" == "OS X" ]]
then
PKCS11_ENGINE=`echo "${ENGINE_DIR}/pkcs11.so"`
RTENGINE=`echo "${ENGINE_DIR}/librtengine.so"`
fi
OPENSSL_TEMPLATE_CONF="$TWO_FA_LIB_DIR/common_files/openssl_template.cnf"
OPENSSL_CONF="$TWO_FA_LIB_DIR/common_files/openssl.cnf"
if ! [[ -f "$OPENSSL_CONF" ]] && [[ "$PKCS11_ENGINE" ]] && [[ "$RTENGINE" ]] && [[ "$LIBRTPKCS11ECP" ]]
then
export PKCS11_ENGINE="$PKCS11_ENGINE"
export LIBRTPKCS11ECP="$LIBRTPKCS11ECP"
export RTENGINE="$RTENGINE"
envsubst < "$OPENSSL_TEMPLATE_CONF" > "$OPENSSL_CONF"
fi
echolog "found engine dir: $ENGINE_DIR"
}
function cleanup()
{
echolog "cleanup"
rm -rf $TMP_DIR
cd "$CUR_DIR"
return 0
}
echoerr()
{
echolog "error $@"
echo -e "Ошибка: $@" 1>&2
return 0
}
function install_packages ()
{
check_updates=$1
light=$2
local rv=0
echolog "install common packages for specific OS"
_install_packages "$check_updates" "$light"
local ossl_ver=`openssl version | cut -d" " -f2`
update_openssl_engines_path # Update engines path after installing new packages
if [[ $? -ne 0 ]]
then
echolog "updates of packages avaliable"
rv=1
fi
if [[ "$OS_NAME" == "OS X" ]]
then
rtadmin_path=/usr/local/bin/rtAdmin
else
rtadmin_path=/usr/bin/rtAdmin
fi
if [[ "$check_updates" ]]
then
echolog "check updates for common packages"
else
echolog "install common packages"
fi
if [[ -z "$light" ]]
then
if [[ "$check_updates" ]]
then
echolog "check rtengine by path $RTENGINE"
if ! [[ -f "$RTENGINE" || "$OS_NAME" == "OS X" ]]
then
echolog "rtengine not found"
rv=1
fi
else
echolog "download rtengine"
wget --no-check-certificate "https://download.rutoken.ru/Rutoken/SDK/rutoken-sdk-latest.zip?action=get&path=sdk%2Fopenssl%2Fbin%2F${ossl_ver:0:3}%2Frtengine-${ossl_ver:0:3}%2Flinux_glibc-$OS_ARCH%2Flib%2F" -O rutoken-sdk-latest.zip
if [[ $? -ne 0 ]]
then
echoerr "Не могу загрузить rtengine из SDK"
rv=1
fi
echolog "unzip SDK"
unzip -q rutoken-sdk-latest.zip
echolog "cp rtengine to $RTENGINE"
if [[ "$OS_NAME" == "OS X" ]]
then
cp sdk/openssl/bin/${ossl_ver:0:3}/rtengine-${ossl_ver:0:3}/macos-$OS_ARCH/rtengine.framework/rtengine "$RTENGINE"
else
cp sdk/openssl/bin/${ossl_ver:0:3}/rtengine-${ossl_ver:0:3}/linux_glibc-$OS_ARCH/lib/librtengine.so "$RTENGINE"
fi
fi
fi
if [[ "$check_updates" ]]
then
echolog "check updates for rtadmin"
if ! [[ -f $rtadmin_path ]]
then
echolog "rtadmin not found"
rv=1
fi
else
echolog "download rtadmin"
if [[ "$OS_NAME" == "OS X" ]]
then
wget --no-check-certificate "https://download.rutoken.ru/Rutoken/Utilites/rtAdmin/1.3/macOS//rtAdmin";
else
wget --no-check-certificate "https://download.rutoken.ru/Rutoken/Utilites/rtAdmin/1.3/linux/$OS_ARCH/rtAdmin";
fi
if [[ $? -ne 0 ]]
then
echoerr "Не могу загрузить утилиту rtAdmin"
rv=1
fi
mv rtAdmin $rtadmin_path
chmod +x $rtadmin_path
fi
if [[ "$check_updates" ]]
then
echolog "check updates for pkcs11 lib"
if ! [[ -f "$LIBRTPKCS11ECP" ]]
then
rv=1
fi
else
echolog "download pkcs11 lib"
local arch=$OS_ARCH
case "$OS_ARCH" in
x86_64)
arch="x64"
;;
x86)
arch="x32"
;;
aarch64)
arch="ARM/aarch64"
;;
esac
local res=
if [[ "$OS_NAME" == "OS X" ]]
then
wget -q --no-check-certificate "http://download.rutoken.ru/Rutoken/PKCS11Lib/Current/Mac/x64+arm64/rtpkcs11ecp.framework.zip";
res=$?
unzip rtpkcs11ecp.framework.zip
res=$?
mkdir -p "$(dirname "$(dirname "$LIBRTPKCS11ECP")")"
mv rtpkcs11ecp.framework "$(dirname "$(dirname "$LIBRTPKCS11ECP")")"
else
wget --no-check-certificate "https://download.rutoken.ru/Rutoken/PKCS11Lib/Current/Linux/$arch/librtpkcs11ecp.so";
res=$?
cp librtpkcs11ecp.so "$LIBRTPKCS11ECP";
fi
if [[ $res -ne 0 ]]
then
echoerr "Не могу загрузить пакет librtpkcs11ecp.so"
rv=1
fi
chmod 0444 $LIBRTPKCS11ECP 2> /dev/null ;
fi
return $rv
}
function setup_local_authentication ()
{
local token=$1
local cert_id=$2
echolog "setup_local_auth token:$token cert_id:$cert_id"
if [[ "$UID" -ne "0" ]]
then
echolog "setup local auth run not under root"
sudo_cmd setup_local_authentication "$@"
res=$?
pkcs11_eventmgr &> /dev/null
return "$res"
fi
if [[ "$OS_NAME" == "OS X" ]]
then
user="$ORIG_USER"
else
user=`choose_user`
fi
if [[ $? -ne 0 ]]
then
echolog "not user choosen"
return 0
fi
echolog "choosen user is $user"
_setup_local_authentication "$token" "$cert_id" "$user" &
show_wait $! "Подождите" "Идет настройка"
res=$?
if [[ $res -ne 0 ]]
then
echolog "Error occured while setup local auth"
show_text "Ошибка" "Во время настройки локальной аутентификации произошла ошибка"
return $res
fi
if ! [[ "$OS_NAME" == "OS X" ]]
then
setup_autolock
res=$?
if [[ $res -eq 0 ]]
then
echolog "local auth setuped sucessfully"
show_text "Успех" "Локальная аутентификация настроена"
else
echolog "autolock settuped with error"
show_text "Ошибка" "Во время настройки автоблокировки произошла ошибка"
fi
fi
return $res
}
function setup_autolock ()
{
echolog "setup auto lock"
LIBRTPKCS11ECP="$LIBRTPKCS11ECP" LOCK_SCREEN_CMD="$LOCK_SCREEN_CMD" envsubst < "$TWO_FA_LIB_DIR/common_files/pkcs11_eventmgr.conf" | tee "$PAM_PKCS11_DIR/pkcs11_eventmgr.conf" > /dev/null
_setup_autolock
systemctl daemon-reload
return $?
}
function setup_freeipa_domain_authentication ()
{
token=$1
sssd_conf=/etc/sssd/sssd.conf
echolog "setup_freeipa_domain_authentication token:$token"
if [[ "$UID" -ne "0" ]]
then
echolog "setup freeipa domain auth run not under root"
sudo_cmd setup_freeipa_domain_authentication "$@"
return $?
fi
mkdir "$IPA_NSSDB_DIR" 2> /dev/null;
if ! [ "$(ls -A "$IPA_NSSDB_DIR")" ]
then
echolog "creating database inside $IPA_NSSDB_DIR"
certutil -N -d "$IPA_NSSDB_DIR" --empty-password
fi
CA_path=`open_file_dialog "Корневой сертификат" "Укажите путь до корневого сертификата" "$HOME"`;
if [[ $? -ne 0 ]]
then
echolog "CA path choosen dialog was closed"
return 0
fi
echolog "CA path is $CA_path"
if ! [ -f "$CA_path" ]
then
echoerr "$CA_path doesn't exist"
return 1
fi
echolog "add ca cert to database"
out=`certutil -A -d "$IPA_NSSDB_DIR" -n 'IPA CA' -t CT,C,C -a -i "$CA_path"`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during adding ca cert to db\n$out"
return 1
fi
echolog "add token pkcs11 lib to database"
out=`echo -e "\n" | modutil -dbdir "$IPA_NSSDB_DIR" -add "My PKCS#11 module" -libfile librtpkcs11ecp.so`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during adding pkcs11 lib to db\n$out"
return 1
fi
if ! [ "$(cat "$sssd_conf" | grep 'pam_cert_auth=True')" ]
then
echolog "adding pam cert auth"
sed -i '/^\[pam\]/a pam_cert_auth=True' "$sssd_conf"
if [[ "$SCREENSAVER_NAME" ]]
then
echolog "modify trusted pam.d modules for sssd: $SCREENSAVER_NAME"
sed -i "/^\[pam\]/a pam_p11_allowed_services = +$SCREENSAVER_NAME" "$sssd_conf"
fi
fi
_setup_freeipa_domain_authentication
systemctl restart sssd
return 0
}
function setup_ad_domain_authentication ()
{
token=$1
sssd_conf=/etc/sssd/sssd.conf
krb5_conf=/etc/krb5.conf
echolog "setup_ad_domain_authentication token:$token"
if [[ "$UID" -ne "0" ]]
then
echolog "setup ca domain auth run not under root"
sudo_cmd setup_ad_domain_authentication "$@"
return $?
fi
domain_name=`dnsdomainname`
if [[ -z "$domain_name" ]]
then
domain_name=`realm list | head -n 1`
fi
echolog "deteceted domain name: $domain_name"
server_name=`dig "_kerberos._udp.${domain_name}" SRV | grep ^_kerberos | rev | cut -d " " -f 1 | cut -c2- | rev`
echolog "deteceted server name: $server_name"
mkdir -p "$IPA_NSSDB_DIR" 2> /dev/null;
if ! [ "$(ls -A "$IPA_NSSDB_DIR")" ]
then
echolog "creating database inside $IPA_NSSDB_DIR"
certutil -N -d "$IPA_NSSDB_DIR" --empty-password
fi
CA_path=`open_file_dialog "Корневой сертификат" "Укажите путь до корневого сертификата" "$HOME"`;
if [[ $? -ne 0 ]]
then
echolog "CA path choosen dialog was closed"
return 0
fi
echolog "CA path is $CA_path"
if ! [ -f "$CA_path" ]
then
echoerr "$CA_path doesn't exist"
return 1
fi
mkdir -p /etc/pki/tls/certs/
cp "$CA_path" /etc/pki/tls/certs/
echolog "add ca cert to database"
out=`certutil -A -d "$IPA_NSSDB_DIR" -n 'AD CA' -t CT,C,C -a -i "$CA_path"`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during adding ca cert to db\n$out"
return 1
fi
echolog "add token pkcs11 lib to database"
out=`echo -e "\n" | modutil -dbdir "$IPA_NSSDB_DIR" -add "My PKCS#11 module" -libfile librtpkcs11ecp.so`
if [[ $? -ne 0 ]]
then
echoerr "Error occured during adding pkcs11 lib to db\n$out"
return 1
fi
echolog "init sssd.conf file"
sed -i 's/use_fully_qualified_names.*/use_fully_qualified_names = False/g' "$sssd_conf"
if [[ -z "$(cat "$sssd_conf" | grep '\[pam\]')" ]]
then
echo -e "\n[pam]" >> "$sssd_conf"
fi
if [[ -z "$(cat "$sssd_conf" | grep 'pam_cert_auth')" ]]
then
sed -i '/^\[pam\]/a pam_cert_auth = True' "$sssd_conf"
fi
sed -i "s/.*pam_cert_auth.*/pam_cert_auth = True/g" "$sssd_conf"
if [[ "$SCREENSAVER_NAME" ]]
then
if [[ -z "$(cat "$sssd_conf" | grep 'pam_p11_allowed_services')" ]]
then
sed -i "/^\[pam\]/a pam_p11_allowed_services = +$SCREENSAVER_NAME" "$sssd_conf"
fi
sed -i "s/.*pam_p11_allowed_services.*/pam_p11_allowed_services = +$SCREENSAVER_NAME/g" "$sssd_conf"
fi
echolog "init krb5.conf file"
if [[ -z "`cat "$krb5_conf" | grep pkinit_anchors`" ]]
then
sed -i "/^\[libdefaults\]/a pkinit_anchors = DIR:\/etc\/pki\/tls/certs\/" "$krb5_conf"
fi
sed -i "s/.*pkinit_anchors.*/pkinit_anchors = DIR:\/etc\/pki\/tls\/certs\//g" "$krb5_conf"
if [[ -z "`cat "$krb5_conf" | grep pkinit_kdc_hostname`" ]]
then
sed -i "/^\[libdefaults\]/a pkinit_kdc_hostname = $server_name" "$krb5_conf"
fi
sed -i "s/.*pkinit_kdc_hostname.*/pkinit_kdc_hostname = $server_name/g" "$krb5_conf"
if [[ -z "`cat "$krb5_conf" | grep pkinit_eku_checking`" ]]
then
sed -i "/^\[libdefaults\]/a pkinit_eku_checking = kpServerAuth" "$krb5_conf"
fi
sed -i "s/.*pkinit_eku_checking.*/pkinit_eku_checking = kpServerAuth/g" "$krb5_conf"
if [[ -z "`cat "$krb5_conf" | grep default_ccache_name`" ]]
then
sed -i "/^\[libdefaults\]/a default_ccache_name = KEYRING:persistent:%{uid}" "$krb5_conf"
fi
sed -i "s/.*default_ccache_name.*/default_ccache_name = KEYRING:persistent:%{uid}/g" "$krb5_conf"
if [[ -z "`cat "$krb5_conf" | grep default_realm`" ]]
then
sed -i "/^\[libdefaults\]/a default_realm = ${domain_name^^}" "$krb5_conf"
fi
sed -i "s/.*default_realm.*/default_realm = ${domain_name^^}/g" "$krb5_conf"
if [[ -z "`cat "$krb5_conf" | grep pkinit_identities`" ]]
then
sed -i "/^\[libdefaults\]/a pkinit_identities = PKCS11:librtpkcs11ecp.so" "$krb5_conf"
fi
sed -i "s/.*pkinit_identities.*/pkinit_identities = PKCS11:librtpkcs11ecp.so/g" "$krb5_conf"
if [[ -z "`cat "$krb5_conf" | grep canonicalize`" ]]
then
sed -i "/^\[libdefaults\]/a canonicalize = True" "$krb5_conf"
fi
sed -i "s/.*canonicalize.*/canonicalize = True/g" "$krb5_conf"
_setup_ad_domain_authentication
systemctl restart sssd
if [[ $? -ne 0 ]]
then
echoerr "error occured while restart sssd. Status:\n`systemctl status sssd`"
return 1
fi
return 0
}
function zenity_enable ()
{
echolog "check zenity enability"
zenity --help 2> /dev/null
if [[ $? -ne 0 ]]
then
echolog "zenity not enabeled"
return 1
fi
return 0
}
#currently unused function
function choose_cert ()
{
token=$1
get_token_objects "$token" "cert" > get_token_objects_res &
show_wait $! "Подождите" "Идет получение списка сертификатов"
res=$?
if [[ $res -ne 0 ]]
then
show_text "Ошибка" "Не могу получить список сертификатов"
return $res
fi
cert_ids=`cat get_token_objects_res`
header=`echo -e "$cert_ids" | head -n 1`
cert_ids=`echo -e "$cert_ids" | tail -n +2`
if [[ -z "$cert_ids" ]]
then
echo "None"
return 0
fi
cert=`show_list "Выберите сертификат" "$header" "$cert_ids" "Новый сертификат"`
res=$?
if [[ $res -ne 0 ]]
then
return $res
fi
if ! [[ $cert == "Новый сертификат" ]]
then
cert_id=`echo "$cert" | cut -f2`
fi
echo "$cert_id"
return 0
}
function choose_user ()
{
echolog "choose_user"
UID_MIN=$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)
res=$?
echolog "UID_MIN: $UID_MIN"
if [[ $res -eq 0 ]]
then
users=`awk -F: -v UID_MIN=$UID_MIN '($3>=UID_MIN){print $1}' /etc/passwd | sort | sed "s/^/$USER\n/" | uniq`
echolog "user list:\nusers"
fi
if [[ -z "$users" ]]
then
echolog "can't get user list. Get user from get string func"
user=`get_string "Выбор пользователя" "Введите имя настраиваемого пользователя" "$USER"`;
else
user=`show_list "Выбор пользователя" "Пользователи" "$users"`;
fi
echolog "chosed user is $user"
echo "$user"
return 0
}
#currently unused function
function choose_key ()
{
token=$1
get_token_objects "$token" "privkey" > get_token_objects_res &
show_wait $! "Подождите" "Идет получение списка ключей"
res=$?
if [[ $res -ne 0 ]]
then
show_text "Ошибка" "Не могу получить список ключей"
return $res
fi
key_ids=`cat get_token_objects_res`
header=`echo -e "$key_ids" | head -n 1`
key_ids=`echo -e "$key_ids" | tail -n +2`
if [[ -z "$key_ids" ]]
then
echo "None"
return 0
fi
key=`show_list "Выберите ключ" "$header" "$key_ids" "Новый ключ"`
res=$?
if [[ $res -ne 0 ]]
then
return 0
fi
if [[ "$key" == "Новый ключ" ]]
then
key_id=`create_key "$token"`
res=$?
if [[ $res -ne 0 ]]
then
return $res
fi
else
key_id=`echo "$key" | cut -f2`
fi
echo "$key_id"
return 0
}
random-string()
{
head /dev/urandom | LC_CTYPE=C tr -dc 'a-zA-Z0-9' | head -c $1
}
function gen_cert_id ()
{
token=$1
echolog "gen new cert id for token: $token"
get_cert_list "$token" > get_cert_list_res &
show_wait $! "Подождите" "Идет получение списка существующих идентификаторов"
cert_ids=`cat get_cert_list_res`
local res=1
while [[ -n "$res" ]]
do
rand=`random-string 8 | xxd -p`
res=`echo $cert_ids | grep -w $rand`
done
echo "$rand"
return 0
}
function gen_key_id ()
{
token=$1
echolog "gen new cert id for token: $token"
get_key_list "$token" > get_key_list_res &
show_wait $! "Подождите" "Идет получение списка существующих идентификаторов"
key_ids=`cat get_key_list_res`
local res=1
while [[ -n "$res" ]]
do
rand=`random-string 8 | xxd -p`
res=`echo "$key_ids" | grep -w "$rand"`
done
echo "$rand"
return 0
}
function import_cert ()
{
token=$1
key_id=$2
echolog "import cert for token: $token with key_id: $key_id"
if [[ -z $key_id ]]
then
key_id=`gen_key_id "$token"`
echolog "key_id not specifed. Generated is $key_id"
fi
cert_path=`open_file_dialog "Путь до сертификата" "Укажите путь до сертификата" "$HOME"`;
if [[ $? -ne 0 ]]
then
echolog "cert path choosen dialog was closed"
return 0
fi
if ! [[ -z "`cat "$cert_path" | grep '\-----BEGIN CERTIFICATE-----'`" ]]
then
echolog "convert cert from PEM to DER format"
out=`$OPENSSL x509 -in "$cert_path" -out cert.crt -inform PEM -outform DER`;
if [[ $? -ne 0 ]]
then
echoerr "Error occured while convert cert from PEM to DER format:\n$out"
fi
echolog "cert path changed to cert.crt"
cert_path=cert.crt
fi
label=`get_string "Метка сертификата" "Укажите метку сертификата"`
if [[ $? -ne 0 ]]
then
echolog "choose label dialog was closed"
return 0
fi
echolog "choosen cert label is: $label"
import_obj_on_token "$token" "cert" "$cert_path" "$label" "$key_id" &
show_wait $! "Подождите" "Идет импорт сертификата"
res=$?
if [[ $res -ne 0 ]]
then
echoerr "Не удалось импортировать сертификат на токен"
show_text "Ошибка" "Не удалось импортировать сертификат на токен"
return $res
fi
return 0
}
function get_token_password ()
{
token=$1
echolog "get_token_password for token: $token"
res=1
while [[ $res -ne 0 ]]
do
pin=`get_password "Ввод PIN-кода" "Введите PIN-код Пользователя:"`
res=$?
if [[ $res -ne 0 ]]
then
echolog "get token PIN dialog was closed"
return $res
fi
check_pin "$token" "$pin" &
show_wait $! "Подождите" "Идет проверка PIN-кода"
res=$?
if [[ $res -eq 2 ]]
then
echolog "User PIN was blocked"
yesno "PIN-код заблокирован" "`echo -e \"PIN-код Пользователя заблокирован.\nРазблокировать его с помощью PIN-кода Администратора?\"`"
res=$?
if [[ $res -ne 0 ]]
then
echolog "User decides to not unlock pin"
return $res
fi
unlock_pin "$token"
res=$?
if [[ $res -eq 0 ]]
then
echolog "User Pin was unlocked"
else
echolog "User Pin is still locked"
fi
else
if [[ $res -ne 0 ]]
then
echoerr "Uncorrect pin"
show_text "Ошибка" "Неправильный PIN-код"
fi
fi
done
echo "$pin"
return 0
}
function get_cert_data ()
{
echolog "get_cert_cubj"
local form_atr="Регион