-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_shell.sh
More file actions
1691 lines (1328 loc) · 66.6 KB
/
lib_shell.sh
File metadata and controls
1691 lines (1328 loc) · 66.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
# shellcheck source=/dev/null disable=SC2119,SC2120,SC2294,SC2001,SC2045,SC2184,SC2059,SC2034
GETOPT_SHORT_SHELL=h,v,d,b,s,k
ERROR_ARGV=10
GREP="/usr/bin/grep --text" # no _shellcheck
EGREP="/usr/bin/grep --text" # no _shellcheck
####################################################################################################
########################################### PROCESS OPTS ###########################################
####################################################################################################
_process_opts () {
_func_start "$@"
local __short
local __long
local __action
local __return=0
local __help=false
local __bats=false
local __shellcheck=false
local __list_libs=false
local __kcov=false
__short=$(_getopt_short)
__long=$(_getopt_long)
OPTS=$(getopt --options "$__short" --long "$__long" --name "$0" -- "$@" 2>/dev/null) || (_error "Bad or missing argument.\n\nTry '$CUR_NAME --help' for more informations\n" ; return 1)
if ! _startswith "$1" '-'; then
_error "Bad or missing argument.\n\nTry '$CUR_NAME --help' for more informations\n" ; return 1
else
eval set -- "$OPTS"
while true ; do
case "$1" in
-v | --verbose ) VERBOSE=true ; shift ;;
-d | --debug ) DEBUG=true ; shift ;;
--dry-run ) DRY_RUN=true ; shift ;;
--default ) DEFAULT=true ; shift ;;
--force ) FORCE=true ; shift ;;
--yubikey ) YUBIKEY=true ; shift ;;
--lib ) LIB="$2" ; shift ; shift ;;
-h | --help ) __help=true ; export ACTION=true ; shift ;;
-b | --bats ) __bats=true ; export ACTION=true ; shift ;;
-s | --shellcheck ) __shellcheck=true ; export ACTION=true ; shift ;;
-k | --kcov ) __kcov=true ; export ACTION=true ; shift ;;
--list-libs ) __list_libs=true ; export ACTION=true ; shift ;;
-- ) shift ; break ;;
*) shift ;;
esac
done
fi
if $__help ; then
_usage ; __return=$? # no _shellcheck
else
if $__list_libs ; then if ! _get_installed_libs ; then _error "something went wrong when listing installed libs" ; _func_end "1" ; return 1 ;fi ; fi
if $__bats ; then if ! _bats ; then _error "something went wrong in bats" ; _func_end "1" ; return 1 ;fi ; fi
if $__shellcheck ; then if ! _shellcheck "$@" ; then _error "something went wrong in shellcheck" ; _func_end "1" ; return 1 ;fi ; fi
if $__kcov ; then if ! _kcov ; then _error "something went wrong in kcov" ; _func_end "1" ; return 1 ;fi ; fi
fi
_func_end "$__return" ; return $__return
}
_getopt_short () { # no _shellcheck
_func_start "$@"
local __lib
local __tmp
local __libs
__libs=$(_get_installed_libs | _upper)
for __lib in $__libs ; do
__tmp=GETOPT_SHORT_$__lib
if _exist "${!__tmp}"; then echo -n "${!__tmp}," ; fi
done | _remove_last_car
_func_end "0" ; return 0 # no _shellcheck
}
_getopt_long () { # no _shellcheck
_func_start "$@"
local __line
local __word
local __opt
local __result
__result=$(for __lib in $(_get_installed_libs); do
$GREP "^# usage" "$MY_GIT_DIR"/"$__lib"/lib_"$__lib".sh | cut -d: -f2-99 | cut -d_ -f2-99 \
| sed -e "s/(\$1)//" | sed -e "s/(\$2)//" | sed -e "s/(\$3)//" \
| sed -e "s/(\$4)//" | sed -e "s/(\$5)//" | sed -e "s/(\$6)//" |\
while read -r __line; do
for __word in $__line; do
echo "$__word:,"
done
done | sort -u |$GREP "^--" | sed -e 's/--//g' | while read -r __line; do
echo -n "$__line"
done
done)
for __lib in $(_get_installed_libs); do
echo -n "$__lib:,"
done
echo -n "debug,verbose,help,list-libs,bats,shellcheck,kcov,dry-run,default,force,yubikey,$__result""lib:" | sed -e 's/ /:,/g'
_func_end "0" ; return 0 # no _shellcheck
}
####################################################################################################
############################################## USAGES ##############################################
####################################################################################################
_usage () {
_func_start "$@"
# Check argv
if _exist "$LIB" && ! _fileexist "$MY_GIT_DIR/$LIB/lib_$LIB.sh" ; then _error "No such LIB:$LIB\n\nTry '$CUR_NAME -h' for more informations\n"; _func_end "1" ; return 1 ; fi
local __line
if _exist "$LIB"; then
if _func_exist "_usage_$LIB"; then
_usage_"$LIB"
fi
$GREP "^# usage" "$MY_GIT_DIR/$LIB/lib_$LIB.sh" | cut -d_ -f2-99 \
| sed -e "s/(\$1)//" | sed -e "s/(\$2)//" | sed -e "s/(\$3)//" | sed -e "s/(\$4)//" \
| sed -e "s/(\$5)//" | sed -e "s/(\$6)//" | sed -e "s/(\$7)//" | sed -e "s/(\$8)//" \
| sed -e "s/(\$9)//" | sed -e "s/(\$10)//" | while read -r __line
do
echo "$CUR_NAME --lib $LIB $__line"
done | sort -u
else
echo "Usage :"
echo " * This help => $CUR_NAME -h | --help"
echo " * Verbose => $CUR_NAME -v | --verbose"
echo " * Debug => $CUR_NAME -d | --debug"
echo " * Dry run => $CUR_NAME --dry-run"
echo " * Select default values when asked => $CUR_NAME --default"
echo " * Force action => $CUR_NAME --force"
echo " * Use a Yubikey => $CUR_NAME --yubikey"
echo " * List avaliable libs => $CUR_NAME --list-libs"
echo " * Use any lib => $CUR_NAME --lib lib_name"
echo " * Bash Automated Testing System => $CUR_NAME -b | --bats --lib lib_name"
echo " * Shell Syntax Checking => $CUR_NAME -s | --shellcheck --lib lib_name"
echo " * Code coverage => $CUR_NAME -k | --kcov --lib lib_name"
fi
_func_end "0" ; return 0 # no _shellcheck
}
####################################################################################################
######################################### LOAD LIBS & CONF #########################################
####################################################################################################
_load_libs () {
# _func_start "$@"
local __lib
source "$MY_GIT_DIR/shell/lib_shell-base.sh"
for __lib in $(_get_installed_libs); do
_verbose "Loading:$MY_GIT_DIR/$__lib/lib_$__lib.sh"
source "$MY_GIT_DIR"/"$__lib"/lib_"$__lib".sh
done
# _func_end "0" ; return 0 # no _shellcheck
}
_load_lib () {
_func_start "$@"
# Check argv
if ! _exist "$1" ;then _error "LIB EMPTY" ; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$MY_GIT_DIR/$1/lib_$1.sh" ;then _error "$MY_GIT_DIR/$1/lib_$1.sh not exist, not sourcing" ; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
_verbose "Loading $MY_GIT_DIR/$1/lib_$1.sh"
source "$MY_GIT_DIR"/"$1"/lib_"$1".sh
_func_end "0" ; return 0 # no _shellcheck
}
_load_conf () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "CONF EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$1"; then _error "$1 not exist, not sourcing (did you git pull ?)" ; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __basename
local __my_basename
local __my_conf_file
__basename=$(basename "$1" | sed -e "s/\./\\\./")
__my_basename="my_"$__basename
__my_conf_file=$(echo "$1" | sed -e "s/$__basename/$__my_basename/")
if _fileexist "$__my_conf_file"; then
_verbose "Sourcing MY CONF:$__my_conf_file"
source "$__my_conf_file"
else
_verbose "Sourcing:$1"
source "$1"
fi
_func_end "0" ; return 0 # no _shellcheck
}
_get_installed_libs () {
_func_start "$@"
local __lib_dir
for __lib_dir in $(ls "$MY_GIT_DIR"); do
if _fileexist "$MY_GIT_DIR"/"$__lib_dir"/lib_"$__lib_dir".sh ; then
echo -n "$__lib_dir "
fi
done | _remove_last_car
_func_end "0" ; return 0 # no _shellcheck
}
####################################################################################################
######################################### DEBUG MANAGEMENT #########################################
####################################################################################################
_verbose_file () {
local __date
__date=$(_date)
_verbose_func_space
if $VERBOSE; then _echoerr "[$$] -- DEBUG -- $__date -- $VERBOSE_SPACE ---- dump file start ---- " "[$*]"; fi
if $VERBOSE; then cat "$1" >&2; fi
if $VERBOSE; then _echoerr "[$$] -- DEBUG -- $__date -- $VERBOSE_SPACE ---- dump file end ---- " "[$*]"; fi
}
####################################################################################################
############################################ SIMPLE TEST ###########################################
####################################################################################################
_func_exist() {
[ "$(type -t "$1")" == 'function' ]
}
_notstartswith() {
if _startswith "$1" "$2"; then return 1; else return 0; fi
}
_contains () {
if [[ $1 =~ $2 ]]; then return 0; else return 1; fi
}
_notexist () {
if [[ -z "$1" ]] ; then return 0; else return 1; fi
}
_notinstalled () {
if type "$1" 2> /dev/null 1>/dev/null ; then return 1; else return 0; fi
}
_filenotexist () {
if [ -e "$1" ]; then return 1; else return 0; fi
}
# same as _fileexist for nfs files
_remotefileexist () {
_func_start "$@"
timeout 1 stat -t "$1" > /dev/null 2>/dev/null
case "$?" in
0)
_verbose "$1 exist"
_func_end "0" ; return 0 # no _shellcheck
;;
124)
_verbose "$1 TIMEOUT"
_func_end "1" ; return 1 # no _shellcheck
;;
*)
_verbose "$1 not exist"
_func_end "1" ; return 1 # no _shellcheck
;;
esac
}
_workingdir_isnot () {
if [ "a$PWD" = "a$1" ]; then return 1; else return 0; fi
}
_raspberry () {
if [ "$(_os_arch)" = "armv7l" ]; then return 0; else return 1; fi
}
_x86_64 () {
if [ "$(_os_arch)" = "x86_64" ]; then return 0; else return 1; fi
}
#
# usage: _host_up_show --network ($1)(192.168.1.0/24)
#
_host_up_show () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "NETWORK EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "nmap"; then _error "nmap not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "dig"; then _error "dig not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
_debug "Looking for $1 alive ips"
local __line
local __name
nmap -v -sn -n "$1" -oG - | $GREP Up | awk '{print $2}' | while read -r __line
do
__name=$(dig -x "$__line" | $GREP -v ^\; | $GREP PTR | awk '{print $5}' | _remove_last_car)
echo "$__line $__name"
done | sort -u
_func_end "0" ; return 0 # no _shellcheck
}
#
# usage: _iptables_show
#
_iptables_show () {
_func_start "$@"
# Check argv
if ! _installed "iptables"; then _error "iptables not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __return
local __id
# __id=$(_id) ; if [ "$__id" -ne "0" ]; then _error "must be root"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
echo "===> Filter"
iptables -vL -t filter
echo ""
echo "===> Nat"
iptables -vL -t nat
echo ""
echo "===> Mangle"
iptables -vL -t mangle
echo ""
echo "===> Raw"
iptables -vL -t raw
echo ""
echo "===> Secutiry"
iptables -vL -t security
__return=$? ; if [ $__return -ne 0 ] ; then _error "something went wrong with iptables"; _func_end "$__return" ; return $__return ; fi
_func_end "$__return" ; return $__return
}
#
# usage: _iptables_save
#
_iptables_save () {
_func_start "$@"
# Check argv
if ! _installed "iptables"; then _error "iptables not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __return
iptables-save -c > "${HOME}/iptables-save"
__return=$? ; if [ $__return -ne 0 ] ; then _error "something went wrong with iptables"; _func_end "$__return" ; return $__return ; fi
_func_end "$__return" ; return $__return
}
#
# usage: _iptables_restore
#
_iptables_restore () {
_func_start "$@"
# Check argv
if ! _installed "iptables"; then _error "iptables not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __return
iptables-restore -c < "${HOME}/iptables-save"
__return=$? ; if [ $__return -ne 0 ] ; then _error "something went wrong with iptables"; _func_end "$__return" ; return $__return ; fi
_func_end "$__return" ; return $__return
}
#
# usage: _iptables_flush
#
_iptables_flush () {
_func_start "$@"
# Check argv
if _installed "docker"; then _error "Running on host with docker installed is not supported"; _func_end "$ERROR_ARGV" ; return 0 ; fi # no _shellcheck
if ! _installed "iptables"; then _error "iptables not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __return
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -Z
__return=$? ; if [ $__return -ne 0 ] ; then _error "something went wrong with iptables"; _func_end "$__return" ; return $__return ; fi
_func_end "$__return" ; return $__return
}
_showU8Variation () {
#_showU8Variation 1 26 show in right table how char looks like in term
local __i __a __f __e __t
printf -v __t '%31s' ''
__t=${__t// /-}
printf -v __t '%s %s %s\n' "${__t::6}" "$__t"{,}
printf -v __f '%%%ds%%%%b\\\\r' {40..10..-2}
printf -v __f "$__f"
__f=${__f// /$'\UA0'}
printf -v __e '%%%%%%ds%%%%%%%%b\\\\U%X\\\\\\\\r' \
$(( $1 > 16 ? $1 + 917743 : $1 + 65023 ))
printf -v __e "$__e" {73..43..-2}
printf -v __e "$__e"
printf 'Show UTF8 table using: VARIATION SELECTOR-%d (U+%X)\n' "$1" \
$(( $1 > 16 ? $1 + 917743 : $1 + 65023 ))
shift
for __a; do
printf "$__e${__f}U%03Xyx\n%s" {,}{{F..A..-1},{9..0..-1}} 0x"${__a}" "$__t"
for __i in {0..9} {A..F}; do
(( 16#$__a == 0 )) && (( ( 16#$__i & 7 ) < 2 )) &&
printf 'U%04Xx%68s\n' 0x"$__a$__i" '' && continue
printf "$__e${__f}U%04Xx\n" \
"\\U$__a$__i"{,}{{F..A..-1},{9..0..-1}} 0x"$__a$__i"
done
done
}
_show_color_code () {
local __mode
local __bg
local __color
local __black=30
local __red=31
local __green=32
local __yellow=33
local __blue=34
local __magenta=35
local __cyan=36
local __light_gray=37
local __gray=90
local __light_red=91
local __light_green=92
local __light_yellow=93
local __light_blue=94
local __light_magenta=95
local __light_cyan=96
local __whithe=97
local __bg_black=40
local __bg_red=41
local __bg_green=42
local __bg_yellow=43
local __bg_blue=44
local __bg_magenta=45
local __bg_cyan=46
local __bg_gray=47
local __bg_light_gray=100
local __bg_light_red=101
local __bg_light_green=102
local __bg_light_yellow=103
local __bg_light_blue=104
local __bg_light_magenta=105
local __bg_light_cyan=106
local __bg_whithe=107
local __normal=0
local __bold=1
local __dim=2
local __italic=3
local __underline=4
local __blink=5
local __reverse=7
local __invisible=8
local __strikethrough=9
local __dounle_underline=21
local __moverline=53
for __bg in $__normal $__bg_black $__bg_red $__bg_light_red $__bg_green $__bg_light_green $__bg_yellow $__bg_light_yellow $__bg_blue $__bg_light_blue $__bg_magenta $__bg_light_magenta $__bg_cyan $__bg_light_cyan $__bg_gray $__bg_light_gray $__bg_whithe ; do
echo
echo "bg color code : $__bg"
printf 'normal\t\tbold\t\tdim\t\titalic\t\tunderline\t2 underline\tinvisible\tstrikethrough\tmoverline\tblink\t\treverse\n'
for __color in $__black $__red $__light_red $__green $__light_green $__yellow $__light_yellow $__blue $__light_blue $__magenta $__light_magenta $__cyan $__light_cyan $__gray $__whithe; do
for __mode in $__normal $__bold $__dim $__italic $__underline $__dounle_underline $__invisible $__strikethrough $__moverline $__blink $__reverse; do
if [ "a$1" = "a" ] ; then
printf '\e[%d;%d;%dm%-12s\e[0m' "$__bg" "$__mode" "$__color" "$(printf ' \\e[%d;%d;%dm]' "$__bg" "$__mode" "$__color")" && printf '\t'
else
printf '\e[%d;%d;%dm%-12s\e[0m' "$__bg" "$__mode" "$__color" "$(printf "$1")" && printf '\t'
fi
done
printf '\n'
done
done
}
####################################################################################################
############################################## CRYPT ###############################################
####################################################################################################
_pass_2_pin () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "pass EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __pass
local __i
__pass=$(echo "$1" | _lower)
for (( __i = 0; __i < ${#__pass}; ++__i)); do echo -n $(($(printf "%d\n" \'"${__pass:$__i:1}") - 96)) ; done ; echo
_func_end "0" ; return 0 # no _shellcheck
}
_keepassxc_create_database () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if _fileexist "$2"; then _error "DATABASE $2 already exist"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="db-create" ; else __yubikey_opt="db-create" ; fi
# shellcheck disable=2086
__result=$(echo -e "$1\n$1" | keepassxc-cli $__yubikey_opt -p "$2" 2>/dev/null)
_verbose "$__result"
_success "create keepass database \"$2\""
_func_end "0" ; return 0 # no _shellcheck
}
_keepassxc_read () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="show -y 2:$(ykman list -s)" ; else __yubikey_opt="show" ; fi
# shellcheck disable=2086
__result=$(echo "$1" | keepassxc-cli $__yubikey_opt -s "$2" "$3" 2>/dev/null)
if ! _exist "$__result" ; then _error "something went wong in _keepassxc_read"; _func_end "1" ; return 1 ; fi
echo "$__result"
_func_end "0" ; return 0 # no _shellcheck
}
_keepassxc_read_password () {
_func_start "$@"
# Check argv
local __result
local __return
__result=$(_keepassxc_read "$1" "$2" "$3")
__return=$? ; if [ $__return -ne 0 ] ; then _error "something went wong in _keepassxc_read_password"; _func_end "$__return" ; return $__return ; fi
__result=$(echo "$__result" | $GREP -w "Password:" | cut -d\ -f2-99)
echo "$__result"
_func_end "0" ; return 0 # no _shellcheck
}
_keepassxc_read_username () {
_func_start "$@"
# Check argv
local __result
local __return
__result=$(_keepassxc_read "$1" "$2" "$3")
__return=$? ; if [ $__return -ne 0 ] ; then _error "something went wong in _keepassxc_read_username"; _func_end "$__return" ; return $__return ; fi
echo "$__result" | $GREP -w "UserName:" | cut -d\ -f2-99
_func_end "0" ; return 0 # no _shellcheck
}
_keepassxc_list_attachments () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __line
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="show -y 2:$(ykman list -s)" ; else __yubikey_opt="show" ; fi
# shellcheck disable=2086
__result=$(echo "$1" | keepassxc-cli $__yubikey_opt --show-attachments -a Tags "$2" "$3" 2>/dev/null)
if ! _exist "$__result" ; then _error "something went wong in _keepassxc_read"; _func_end "1" ; return 1 ; fi
echo "$__result" | $GREP -v -w "Attachments:" | awk '{print $1}' | while read -r __line; do
if [ "a$__line" != "a" ] ; then echo "$__line"; fi
done
_func_end "0" ; return 0 # no _shellcheck
}
_keepassxc_restore_attachment () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$4"; then _error "ATTACHMENT EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$5"; then _error "DEST EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __return
local __line
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="attachment-export -y 2:$(ykman list -s)" ; else __yubikey_opt="attachment-export" ; fi
# shellcheck disable=2086
__result=$(echo "$1" | keepassxc-cli $__yubikey_opt "$2" "$3" "$4" "$5" 2>&1)
__return=$? ; if [ $__return -ne 0 ] ; then _error "unable to restore $4"; _func_end "$__return" ; return $__return ; fi
__result=$(echo "$__result" | $GREP -v "Enter password")
_verbose "$__result"
_func_end "$__return" ; return $__return # no _shellcheck
}
_keepassxc_add_attachment () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$4"; then _error "ATTACHMENT EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$5"; then _error "SRC EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __return
local __line
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="attachment-import -y 2:$(ykman list -s)" ; else __yubikey_opt="attachment-import" ; fi
# shellcheck disable=2086
__result=$(echo "$1" | keepassxc-cli $__yubikey_opt "$2" "$3" "$4" "$5" 2>&1)
__return=$? ; if [ $__return -ne 0 ] ; then _error "unable to add $4"; _func_end "$__return" ; return $__return ; fi
__result=$(echo "$__result" | $GREP -v "Enter password")
_verbose "$__result"
_func_end "$__return" ; return $__return # no _shellcheck
}
_keepassxc_add_group () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __return
local __line
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="mkdir -y 2:$(ykman list -s)" ; else __yubikey_opt="mkdir" ; fi
# shellcheck disable=2086
__result=$(echo "$1" | keepassxc-cli $__yubikey_opt "$2" "$3" 2>&1)
__return=$? ; if [ $__return -ne 0 ] ; then _error "unable to add $3 as group"; _func_end "$__return" ; return $__return ; fi
__result=$(echo "$__result" | $GREP -v "Enter password")
_verbose "$__result"
_success "add group \"$3\""
_func_end "$__return" ; return $__return # no _shellcheck
}
_keepassxc_add_entry () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __return
local __line
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="add -y 2:$(ykman list -s)" ; else __yubikey_opt="add" ; fi
# shellcheck disable=2086
__result=$(echo "$1" | keepassxc-cli $__yubikey_opt "$2" "$3" 2>&1)
__return=$? ; if [ $__return -ne 0 ] ; then _error "unable to add $3 as entry"; _func_end "$__return" ; return $__return ; fi
__result=$(echo "$__result" | $GREP -v "Enter password")
_verbose "$__result"
_success "add entry \"$3\""
_func_end "$__return" ; return $__return # no _shellcheck
}
_keepassxc_change_username () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$4"; then _error "ENTRY_USER EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __return
local __line
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="edit -y 2:$(ykman list -s)" ; else __yubikey_opt="edit" ; fi
# shellcheck disable=2086
__result=$(echo -e "$1" | keepassxc-cli $__yubikey_opt "$2" "$3" -u "$4" 2>&1)
__return=$? ; if [ $__return -ne 0 ] ; then _error "unable to change username for $3"; _func_end "$__return" ; return $__return ; fi
__result=$(echo "$__result" | $GREP -v "Enter password")
_verbose "$__result"
_success "change username of \"$3\" to \"$4\""
_func_end "$__return" ; return $__return # no _shellcheck
}
_keepassxc_change_password () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "DATABASE EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$3"; then _error "ENTRY EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$4"; then _error "ENTRY_PASS EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _fileexist "$2"; then _error "DATABASE $2 not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "keepassxc-cli" ; then _error "keepassxc-cli not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __result
local __return
local __line
local __yubikey_opt
if $YUBIKEY; then __yubikey_opt="edit -y 2:$(ykman list -s)" ; else __yubikey_opt="edit" ; fi
# shellcheck disable=2086
__result=$(echo -e "$1\n$4" | keepassxc-cli $__yubikey_opt "$2" "$3" -p 2>&1)
__return=$? ; if [ $__return -ne 0 ] ; then _error "unable to change password for $3"; _func_end "$__return" ; return $__return ; fi
__result=$(echo "$__result" | $GREP -v "Enter password")
_verbose "$__result"
_success "change password of \"$3\" to \"***\""
_func_end "$__return" ; return $__return # no _shellcheck
}
_gpg_yubikey_reset () {
_func_start "$@"
# Check argv
if ! _installed "ykman" ; then _error "ykman not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
echo "y" | ykman openpgp reset 2>/dev/null 1>/dev/null
_func_end "0" ; return 0 # no _shellcheck
}
_gpg_yubikey_change_admin_pin () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "NEW PIN EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "ykman" ; then _error "ykman not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __old_pin
__old_pin="${2:-12345678}"
echo -e "admin\npasswd\n3\n$__old_pin\n$1\n$1\nq\nquit\n" | gpg --command-fd=0 --pinentry-mode=loopback --edit-card 2>/dev/null 1>/dev/null
_func_end "0" ; return 0 # no _shellcheck
}
_gpg_yubikey_change_user_pin () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "NEW PIN EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "ykman" ; then _error "ykman not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __old_pin
__old_pin="${2:-123456}"
echo -e "admin\npasswd\n1\n$__old_pin\n$1\n$1\nq\nquit\n" | gpg --command-fd=0 --pinentry-mode=loopback --edit-card 2>/dev/null 1>/dev/null
_func_end "0" ; return 0 # no _shellcheck
}
_gpg_yubikey_set_retries () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "ADMIN PIN EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "ykman" ; then _error "ykman not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __retries
__retries="${2:-5}"
echo "$1" | ykman openpgp access set-retries -f "$__retries" "$__retries" "$__retries"
_func_end "0" ; return 0 # no _shellcheck
}
_gpg_restore_keys_from_keepass () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "keepassxc password EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "keepassxc database EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "gpg" ; then _error "ykman not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
# Declare local var
local __line
local __attachments
local __fp
local __entry
local __dest_dir
local __return
# Set local var
__return="1"
__dest_dir="${HOME}/.gnupg"
__entry="keys"
__attachments=$(_keepassxc_list_attachments "$1" "$2" "$__entry") ; __return=$? ; if [ $__return -ne 0 ] ; then _error "unable to list attachments"; _func_end "$__return" ; return $__return ; fi
__fp=$(echo "$__attachments" | cut -d- -f1 | sort -u)
echo "$__attachments" | while read -r __line; do
if ! _keepassxc_restore_attachment "$1" "$2" "$__entry" "$__line" "$__dest_dir/$__line" ; then _error "unable to restore attachment"; _func_end "$__return" ; return $__return ; fi
gpg --import "$__dest_dir/$__line" 2>/dev/null 1>/dev/null ; __return=$? ; if [ $__return -ne 0 ] ; then _error "gpg import fails"; _func_end "$__return" ; return $__return ; fi
done
echo -e "5\ny\n" | gpg --command-fd 0 --no-tty --batch --expert --edit-key "$__fp" trust 2> /dev/null 1> /dev/null
_func_end "0" ; return 0 # no _shellcheck
}
_gpg_transfert_keys_to_yubikey () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "keepassxc password EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "keepassxc database EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _installed "gpg" ; then _error "gpg not found"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
local __return
local __admin_pin
# local __passphrase
# local __passphrase_entry
local __identity
local __entry
local __key_id
__passphrase_entry="gpg passphrase"
__entry="key"
# because we'r unable to put key to yubikey if we'v changed amin pin before, we have to do everything with default pin, then change it
__admin_pin="12345678"
# __passphrase=$(_keepassxc_read_password "$1" "$2" "$__passphrase_entry")
# __return=$? ; if [ $__return -ne 0 ] ; then _error "unable to read passphrase from $2"; _func_end "$__return" ; return $__return ; fi
__identity=$(_keepassxc_read_username "$1" "$2" "$__entry")
__key_id=$(gpg -k --with-colons "$__identity" | awk -F: '/^pub:/ { print $5; exit }')
echo -e "key 1\nkeytocard\n1\n$__admin_pin\n$__admin_pin\nsave" | gpg --batch --command-fd=0 --pinentry-mode=loopback --edit-key "$__key_id" 2>/dev/null 1>/dev/null
echo -e "key 2\nkeytocard\n2\n$__admin_pin\nsave" | gpg --batch --command-fd=0 --pinentry-mode=loopback --edit-key "$__key_id" 2>/dev/null 1>/dev/null
echo -e "key 3\nkeytocard\n3\n$__admin_pin\nsave" | gpg --batch --command-fd=0 --pinentry-mode=loopback --edit-key "$__key_id" 2>/dev/null 1>/dev/null
echo -e "admin\nlogin\n$__identity" | gpg --batch --command-fd=0 --pinentry-mode=loopback --edit-card 2>/dev/null 1>/dev/null
gpg -K
_func_end "0" ; return 0 # no _shellcheck
}
secret () {
output="${1}".$(date +%s).enc
__key_id=$(gpg -k --with-colons | awk -F: '/^pub:/ { print $5; exit }')
echo "$__key_id"
gpg --encrypt --armor --output "${output}" -r "$__key_id" "${1}" && echo "${1} -> ${output}"
}
_gpg_yubikey_init_from_keepass () {
_func_start "$@"
# Check argv
if ! _exist "$1"; then _error "keepassxc password EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if ! _exist "$2"; then _error "keepassxc database EMPTY"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
if _fileexist "${HOME}/.gnupg" ; then _error "can't restore on existing ${HOME}/.gnupg, please back it up and remove it"; _func_end "$ERROR_ARGV" ; return $ERROR_ARGV ; fi
# Declare local var
local __return
local __result
local __admin_pin_entry
local __user_pin_entry
local __admin_pin
local __user_pin
local __retries
# Set local var
__admin_pin_entry="admin pin"
__user_pin_entry="user pin"
__retries="5"
__admin_pin=$(_keepassxc_read_password "$1" "$2" "$__admin_pin_entry") ; __return=$? ; if [ $__return -ne 0 ] ; then _error "unable to read gpg admin pin from $2"; _func_end "$__return" ; return $__return ; fi
__user_pin=$(_keepassxc_read_password "$1" "$2" "$__user_pin_entry") ; __return=$? ; if [ $__return -ne 0 ] ; then _error "unable to read gpg user pin from $2"; _func_end "$__return" ; return $__return ; fi
# Do what need to be done
ln -s "${HOME}/git/rc/scdaemon.conf" "${HOME}/.gnupg/scdaemon.conf"
ln -s "${HOME}/git/rc/gpg.conf" "${HOME}/.gnupg/gpg.conf"
__result=$(gpg -k 2>/dev/null 1>/dev/null) ; __return=$? ; if [ $__return -ne 0 ] ; then _error "unable to init keyring $__result"; _func_end "$__return" ; return $__return ; fi
# Reload scdaemon
gpg-connect-agent "SCD KILLSCD" "SCD BYE" /bye 2>/dev/null 1>/dev/null
gpg-connect-agent learn /bye 2>/dev/null 1>/dev/null
__return="1"
if ! _gpg_yubikey_reset ; then _error "unable to reset yubikey"; _func_end "$__return" ; return $__return ; fi
if ! _gpg_restore_keys_from_keepass "$1" "$2" ; then _error "unable to restore keys from keepass"; _func_end "$__return" ; return $__return ; fi
if ! _gpg_transfert_keys_to_yubikey "$1" "$2" ; then _error "unable to transfert keys to yubikey"; _func_end "$__return" ; return $__return ; fi
if ! _gpg_yubikey_change_admin_pin "$__admin_pin" ; then _error "unable to change admin pin"; _func_end "$__return" ; return $__return ; fi
if ! _gpg_yubikey_change_user_pin "$__user_pin" ; then _error "unable to change user pin"; _func_end "$__return" ; return $__return ; fi
if ! _gpg_yubikey_set_retries "$__admin_pin" "$__retries" ; then _error "unable to set retries"; _func_end "$__return" ; return $__return ; fi
__return="0"
# Show result and exit
_func_end "$__return" ; return $__return
}
_gpg_init_keepass () {
_func_start "$@"