-
Notifications
You must be signed in to change notification settings - Fork 0
/
conagent.sh
739 lines (728 loc) · 24.2 KB
/
conagent.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
conagent.substitute()
{
local reslist devlist user mandir githubdir port cmd i conagentremoteport1 \
conagentlocalport1 includedir libdir bindir perl_version vendor_perl \
signal cmdlist='sed shred perl lsof sudo ssh tty sshd
basename cat id cut bash mktemp egrep date env mv w
cp chmod ln chown rm touch ssh-agent ps head mkdir chattr lsattr
pwgen sha1sum gpg ssh-add find file tail tr ss awk sleep ping
stat scp groups ssh-keyscan ssh-keygen umount'
declare -A Devlist=(
[sshfs]='sshfs'
[fusermount3]='fusermount3'
[agent]='agent'
)
cmdlist="${Devlist[@]} $cmdlist"
for cmd in $cmdlist;do
i=($(\builtin type -fp $cmd))
[[ -z $i ]] && {
[[ -z ${Devlist[$cmd]} ]] && reslist+=" $cmd" || devlist+=" $cmd"
}
\builtin eval "${cmd//-/_}=${i:-:}"
done
[[ -n $reslist ]] && {
\builtin printf "%s\n" "$FUNCNAME Required: $reslist"
return
}
[[ -n $devlist ]] && \builtin printf "%s\n" "$FUNCNAME Optional: $devlist"
perl_version="$($perl -e 'print $^V')"
vendor_perl=/usr/share/perl5/vendor_perl/
libdir=${LIBDIR}
includedir=${INCLUDEDIR}
bindir=${BINDIR}
githubdir=${GITHUBDIR}
mandir=${MANDIR}
port=${CONAGENTREMOTEPORT:-22}
user=\${USER}
conagentremoteport1=${CONAGENTREMOTEPORT1}
conagentlocalport1=${CONAGENTLOCALPORT1}
sshconfig=${HOME}/.ssh/ssh_config
signal='RETURN HUP INT TERM EXIT'
seed=\${RANDOM}\${RANDOM}\${RANDOM}
\builtin \source <($cat<<-SUB
conagent.listkeys()
{
$ssh-add -E md5 -l
$ssh-add -E sha256 -l
}
conagent.sendkey()
{
local cmd="$env $ssh -T -F \${HOME}/.ssh/ssh_config "
local host="\${1:?[host][keyfile] opt:[port][user]}"
local keycontent="\${2}"
local port=" -o port=\${3:-${port}}"
local user="\${4:-${user}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
\builtin test -r "\$keycontent" || {
\builtin echo "inaccessible key: \$keycontent"
return 1
}
$file -b \$keycontent|$egrep -q "public key" || {
\builtin printf "%s\n" "invalid pubkey."
return 1
}
keycontent="\$(< \$keycontent)"
# set -x
\builtin declare -F conagent.auth >/dev/null || {
\builtin echo "missing function: conagent.auth"
}
local funbody=\$(\builtin declare -f conagent.auth|$sed -E "s;[\$];\\\\\$;g")
\${cmd}${user}@\${host}\${port}<<-AGENTSEND
\${funbody}
conagent.auth "\${keycontent}"
AGENTSEND
# set +x
}
conagent.send()
{
local cmd="$env $ssh -T -F \${HOME}/.ssh/ssh_config "
local host="\${1:?[host][remote cmd/fun] opt:[cmd/fun args][opt port][opt user]}"
local funname="\${2} "
local args="\${3}"
local port=" -o port=\${4:-${port}}"
local user="\${5:-${user}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
# set -x
local funbody=\$(\builtin declare -F \$funname >/dev/null &&\
\builtin declare -f \$funname|$sed -E "s;[\$];\\\\\$;g")
\${cmd}${user}@\${host}\${port}<<-AGENTSEND
\${funbody}
\$funname \${args}
AGENTSEND
# set +x
}
conagent.auth()
{
local keycontent=\${1:?[keycontent]}
local key i oifs=$IFS authorizedkey="\$HOME/.ssh/authorized_keys"
[[ -r \$authorizedkey ]] || > \$authorizedkey
chmod u=rw,go= \$authorizedkey
declare -A Hash
local fcontent="\$(<\$authorizedkey)"
for i in \$fcontent;do
Hash["\${i}"]=1
done
[[ -r "\$keycontent" ]] && keycontent="\$(< \$keycontent)"
i="\${keycontent#* }"
[[ -n "\${Hash["\${i% *}"]}" ]] && return
\builtin printf "%s\n%s" "\${fcontent}" "\$keycontent" > \$authorizedkey
chmod u=r,go= \$authorizedkey
}
agent()
{
$agent -j \${@}
}
conagent.socks()
{
$agent -socks \${@}
}
conagent.agent.start()
{
$agent -s
\builtin test -r ~/.ssh/\$HOSTNAME-ssh && \builtin source ~/.ssh/\$HOSTNAME-ssh
}
conagent.kill.socks()
{
$agent -ks \${@}
}
conagent.addkeys()
{
$agent -a \${@}
\builtin test -r ~/.ssh/\$HOSTNAME-ssh && \builtin source ~/.ssh/\$HOSTNAME-ssh
}
agent.py.install()
{
[[ \${PWD##*/} =~ conagent ]] || return
local debugging=\${1:-0}
[[ \$debugging =~ [[:digit:]] ]] || debugging=1
$sed -e "s;CONAGENTLOCALPORT1;${conagentlocalport1};" \
-e "s;CONAGENTREMOTEPORT1;${conagentremoteport1};" \
-e "s;CONAGENTREMOTEPORT;${port};" \
-e "s;DEBUGGING;\${debugging};" \
src/conagent.py > $bindir/agent
$chmod u=rwx,go=rx $bindir/agent
}
conagent.changepass()
{
\builtin \shopt -s extdebug
\builtin \trap "conagent.genkey.delocate" $signal
declare -A Change=(
[keyfile]="\${1:?[key file][optional debug flag: 0|1]}"
[debug]="\${2}"
[seed]=\${RANDOM}
[display]=${DISPLAY}
[newpassfile]="\$($mktemp --dry-run)"
[oldpassfile]="\$($mktemp --dry-run)"
[askpass]="\$($mktemp --dry-run)"
[option]="--capitalize --numerals --num-passwords=1 \
--secure --sha1=\${Change[newpassfile]}#'' 22"
[date]="\$($date +'%Y%m%d%H%M%S')"
[passasc]="\${Change[keyfile]}_pass.asc"
[uid]=\${UID}
[tty]="\$($tty)"
[ttyowner]="\$($stat --format=%u \${Change[tty]})"
)
[[ "\${Change[debug]}" -eq 1 ]] && Change[debug]="set -o xtrace" || Change[debug]=''
\${Change[debug]}
conagent.genkey.delocate()
{
$shred -fu \${Change[newpassfile]} \${Change[askpass]} \
\${Change[oldpassfile]}
$chmod u=r \${Change[passasc]}
$chmod u=r \${Change[keyfile]}
builtin unset SSH_ASKPASS
builtin declare -x DISPLAY=\${Change[display]}
builtin unset -f conagent.genkey.delocate
builtin trap - $signal
builtin shopt -u extdebug
set +o xtrace
}
if [[ \${Change[ttyowner]} != \${Change[uid]} ]];then
\builtin printf "User ${user} needs to become the owner of \${Change[tty]}.\n"
return
fi
builtin declare -x GPG_TTY="\$($tty)"
$gpg --no-tty --decrypt --no-verbose --quiet \
--output \${Change[oldpassfile]} \${Change[passasc]} || return
$sha1sum <<<\${Change[seed]}|$cut -d' ' -f1 \
>\${Change[newpassfile]}
$pwgen \${Change[option]} > \${Change[newpassfile]}
$shred -fu \${Change[passasc]}
$gpg --no-tty --symmetric --no-verbose --quiet \
--output \${Change[passasc]} --armor \${Change[newpassfile]} || return
$touch \${Change[askpass]}
$chmod u=rwx,go= \${Change[askpass]}
$cat<<-SSHOLDKEY>\${Change[askpass]}
#!$env $bash
$cat \${Change[oldpassfile]}
$cat<<-SSHNEWKEY>\${Change[askpass]}
#!$env $bash
$cat \${Change[newpassfile]}
SSHNEWKEY
SSHOLDKEY
builtin declare -x DISPLAY=':0'
builtin declare -x SSH_ASKPASS="\${Change[askpass]}"
$chmod u=rw \${Change[keyfile]}
$ssh_keygen -p -f \${Change[keyfile]} </dev/null
}
conagent.loadkeys()
{
_conagent.agent.start
declare -A Addkey=(
[homedir]=\${1:-"\${HOME}/.ssh"}
[debug]=\${2}
[display]=\$DISPLAY
)
declare -a Key=(\$($find \${Addkey[homedir]} -name "*_*_*_*" \
! -name "*.pub" ! -name "*.asc"))
declare -A Hash=()
conagent.mutation()
{
declare -A Mut=(
[passasc]=\${1:?[key_pass.asc][askpass][gpgpass]}
[askpass]=\${2:?[askpass][gpgpass]}
[gpgpass]=\${3:?[gpgpass]}
)
[[ -r \${Mut[passasc]} ]] || return
[[ -x \${Mut[askpass]} ]] || return
$cat<<-SSHLOADKEYS>\${Mut[askpass]}
#!$env $bash
$gpg --no-tty --batch --decrypt --no-verbose --quiet \
--passphrase-file \${Mut[gpgpass]} \${Mut[passasc]}
SSHLOADKEYS
}
[[ "\${Addkey[debug]}" -eq 1 ]] && Addkey[debug]="set -o xtrace" || Addkey[debug]=''
\${Addkey[debug]}
builtin trap "conagent.addkey.delocate" SIGHUP SIGTERM SIGINT
conagent.addkey.delocate()
{
[[ -r \${Addkey[askpass]} ]] && $shred -fu \${Addkey[askpass]}
[[ -r \${Addkey[gpgpass]} ]] && $shred -fu \${Addkey[gpgpass]}
builtin unset SSH_ASKPASS
builtin declare -x DISPLAY=\${Addkey[display]}
builtin unset -f conagent.genkey.delocate conagent.mutation \
conagent.addkey.delocate
builtin trap - SIGHUP SIGTERM SIGINT
builtin set +o xtrace
}
local i
for i in \$($ssh_add -L|$cut -d' ' -f2);do
Hash[\$i]="1"
done
builtin declare -x DISPLAY=':0'
builtin declare -x GPG_TTY="\$($tty)"
for i in \${Key[@]};do
[[ -n \${Hash["\$($cut -d' ' -f2 \${i}.pub)"]} ]] && continue
Addkey[passasc]="\${i}_pass.asc"
[[ -r \${Addkey[passasc]} ]] || continue
Addkey[askpass]="\$($mktemp --tmpdir=/var/tmp)"
Addkey[gpgpass]="\$($mktemp --tmpdir=/var/tmp)"
Addkey[gpgpassasc]="\${i}_gpg.asc"
[[ -r \${Addkey[gpgpassasc]} ]] || continue
gpg.decrypt \${Addkey[gpgpassasc]} >\${Addkey[gpgpass]}
builtin declare -x SSH_ASKPASS="\${Addkey[askpass]}"
$chmod u=rwx,go= \${Addkey[askpass]}
conagent.mutation \${Addkey[passasc]} \${Addkey[askpass]} \${Addkey[gpgpass]}
$ssh_add \${i} </dev/null || \builtin printf "Could be invalid password.\n"
$shred -fu \${Addkey[askpass]}
$shred -fu \${Addkey[gpgpass]}
done
conagent.addkey.delocate
}
_conagent.addkeys()
{
\builtin \trap "conagent.addkey.delocate" SIGHUP SIGTERM SIGINT
declare -A Addkey=(
[homedir]=\${1:-"\${HOME}/.ssh"}
[debug]=\${2}
[display]=\$DISPLAY
[uid]=\${UID}
[tty]="\$($tty)"
[ttyowner]="\$($stat --format=%u \${Addkey[tty]})"
)
conagent.addkey.delocate()
{
[[ -a \${Addkey[askpass]} ]] && $shred -fu \${Addkey[askpass]}
\builtin unset SSH_ASKPASS
\builtin declare -x DISPLAY=\${Addkey[display]}
\builtin unset -f conagent.addkey.delocate conagent.mutation
\builtin trap - SIGHUP SIGTERM SIGINT
\builtin \set +o xtrace
}
[[ "\${Addkey[debug]}" -eq 1 ]] && Addkey[debug]="set -o xtrace" || Addkey[debug]=''
\${Addkey[debug]}
if [[ \${Addkey[ttyowner]} != \${Addkey[uid]} ]];then
\builtin printf "User ${user} needs to become the owner of \${Addkey[tty]}.\n"
conagent.addkey.delocate
return
fi
_conagent.agent.start
declare -a Key=(\$($find \${Addkey[homedir]} -name "*_*_*_*" \
! -name "*.pub" ! -name "*.asc"))
declare -A Hash=()
conagent.mutation()
{
declare -A Mut=(
[passasc]=\${1:?[key_pass.asc][askpass]}
[askpass]=\${2:?[askpass]}
)
$cat<<-SSHADDKEYS>\${Mut[askpass]}
#!$env $bash
\builtin declare -x GPG_TTY="\\\$($tty)"
[[ -r \${Mut[passasc]} ]] && \
$gpg --decrypt --no-verbose --quiet \${Mut[passasc]}
SSHADDKEYS
}
local i
for i in \$($ssh_add -L|$cut -d' ' -f2);do
Hash[\$i]='1'
done
\builtin declare -x DISPLAY=':0'
\builtin declare -x GPG_TTY="\${Addkey[tty]}"
for i in \${Key[@]};do
[[ -n \${Hash["\$($cut -d' ' -f2 \${i}.pub)"]} ]] && continue
Addkey[passasc]="\${i}_pass.asc"
[[ -r \${Addkey[passasc]} ]] || continue
Addkey[askpass]="\$($mktemp --dry-run --tmpdir=/var/tmp)"
\builtin declare -x SSH_ASKPASS="\${Addkey[askpass]}"
$gpg --no-tty --decrypt --no-verbose \
--output \${Addkey[askpass]} --quiet \${Addkey[passasc]}
$chmod u=rwx,go= \${Addkey[askpass]}
conagent.mutation \${Addkey[passasc]} \${Addkey[askpass]}
$ssh_add \${i} </dev/null
$shred -fu \${Addkey[askpass]}
done
conagent.addkey.delocate
}
conagent.hostkey()
{
local keytype=\${1:?[type: rsa/ed25519]}
local keyfile="/tmp/ssh_host_\${keytype}_key"
$ssh_keygen -t \${keytype} -f \${keyfile} </dev/null
}
conagent.genkey()
{
$agent -g "\$@"
}
_conagent.genkey()
{
\builtin \shopt -s extdebug
declare -A Genkey=(
[dir]=\${1:?[backup dir] optional: [user][host][keytype][debug flag: 0|1]}
[user]=\${2:-$user}
[hostname]=\${3:-\${HOSTNAME}}
[keytype]=\${4:-rsa}
[debug]=\${5}
[seed]=\$RANDOM
[display]=${DISPLAY}
[tmpfile]=\$($mktemp)
[askpass]=\$($mktemp --tmpdir=/var/tmp/)
[option]="--capitalize --numerals --num-passwords=1 \
--secure --sha1=\${Genkey[tmpfile]}#'' 22"
[date]="\$($date +'%Y%m%d%H%M%S')"
[keyfile]="\${HOME}/.ssh/\${Genkey[user]}_\${Genkey[hostname]}_\${Genkey[keytype]}_\${Genkey[date]}"
[passasc]="\${Genkey[keyfile]}_pass.asc"
[uid]=\${UID}
[tty]="\$($tty)"
[ttyowner]="\$($stat --format=%u \${Genkey[tty]})"
)
\builtin \trap "conagent.genkey.delocate" $signal
[[ "\${Genkey[debug]}" -eq 1 ]] && Genkey[debug]="set -o xtrace" || Genkey[debug]=''
\${Genkey[debug]}
conagent.genkey.delocate()
{
$shred -fu \${Genkey[tmpfile]} \${Genkey[askpass]}
\builtin unset SSH_ASKPASS
\builtin declare -x DISPLAY=\${Genkey[display]}
\builtin unset -f conagent.genkey.delocate
\builtin \shopt -u extdebug
\builtin trap - $signal
\builtin set +o xtrace
}
if [[ \${Genkey[ttyowner]} != \${Genkey[uid]} ]];then
\builtin printf "User ${user} needs to become the owner of \${Genkey[tty]}.\n"
return
fi
$mkdir -p \${HOME}/.ssh
$sha1sum <<<\${Genkey[seed]}|$cut -d' ' -f1 \
>\${Genkey[tmpfile]}
$pwgen \${Genkey[option]} > \${Genkey[tmpfile]}
builtin declare -x GPG_TTY="\$($tty)"
$gpg --symmetric --no-verbose --quiet \
--output \${Genkey[passasc]} --armor \${Genkey[tmpfile]} || return
$chmod u=r,go= \${Genkey[passasc]}
$touch \${Genkey[askpass]}
$chmod u=rwx,go= \${Genkey[askpass]}
$cat<<-SSHGENKEY> \${Genkey[askpass]}
#!$env $bash
builtin declare -x GPG_TTY="\\\$($tty)"
$gpg --decrypt --no-verbose --quiet \${Genkey[passasc]}
SSHGENKEY
builtin declare -x DISPLAY=':0'
builtin declare -x SSH_ASKPASS="\${Genkey[askpass]}"
$ssh_keygen -C "\${Genkey[user]}@\${Genkey[hostname]}" \
-t \${Genkey[keytype]} -f \${Genkey[keyfile]} </dev/null \
|| \${Genkey[passasc]}
[[ -r \${Genkey[keyfile]} ]] &&\
$chmod u=r,go= \${Genkey[keyfile]} \${Genkey[keyfile]}.pub
[[ -r \${Genkey[dir]} ]] && $cp \${Genkey[keyfile]}* \${Genkey[dir]}
}
_conagent.agent.start()
{
declare -a Arg=(\$($ps -C ssh-agent -o pid= -o user=|$egrep $user))
local sshenv=\$HOME/.ssh/\${HOSTNAME}-ssh
\builtin \source <(\builtin \printf "export SSH_TTY="\$($tty)"")
# When no existing agent
if [[ -z \${Arg[0]} ]];then
$ssh_agent | $head -n 2 > \$sshenv
\builtin \source \$sshenv
return
fi
if [[ -r \$sshenv ]];then
\builtin \source \$sshenv
return
fi
# When first login session
\builtin \kill \${Arg[0]}
\builtin \unset SSH_AGENT_PID SSH_AUTH_SOCK
$ssh_agent | $head -n 2 > \$sshenv
\builtin \source \$sshenv
}
conagent.authorizedkey()
{
local keycontent=\${1:?[keycontent: \"\\\$(<key.pub)\"][optional \$HOME/.ssh/authorized_keys][optional debug flag: 0|1]}
local i authorizedkey=\${2:-\$HOME/.ssh/authorized_keys}
local debug=\${3}
local tmpfile=\$($mktemp --tmpdir=/var/tmp)
\builtin \shopt -s extdebug
\builtin \trap "conagent.genkey.delocate" $signal
conagent.genkey.delocate()
{
[[ -r \${tmpfile} ]] && $shred -fu \$tmpfile
\builtin \trap - $signal
\builtin \unset -f conagent.genkey.delocate
\builtin \shopt -u extdebug
\builtin \set +o xtrace
}
[[ "\${debug}" -eq 1 ]] && debug="set -o xtrace" || debug=''
\${debug}
$touch \$authorizedkey
declare -A Hash
for i in \$($cut -d' ' -f2 \$authorizedkey);do
Hash[\$i]=1;
done
[[ -r "\$keycontent" ]] && keycontent="\$($cat \$keycontent)"
i="\$($cut -d' ' -f2 <<<"\$keycontent")"
[[ X\${Hash["\$i"]} == X1 ]] && return
$cat \$authorizedkey - <<<\$keycontent > \$tmpfile
$mv -f \$tmpfile \$authorizedkey
}
conagent.scankey()
{
local host=\${1:-\$HOSTNAME}
local port=\${2:-${port}}
$ssh_keyscan -p $port \$host
}
conagent.kill.openfile()
{
declare -a Pid=(\$($lsof -i|$tail -n 1))
[[ -n \${Pid[1]} ]] || return
builtin kill -s KILL \${Pid[1]}
}
conagentd.checkconfig()
{
$sshd -t -f \${1:?[config]}
}
conagent.interfaces()
{
local conf=\${1:?[interface file]}
[[ -r /etc/network/interfaces ]] || return
$sudo $cp \$conf /etc/network/interfaces
}
conagentd.systmedconfig()
{
local conf=\${1:?[sshd.service conffile]}
$sudo $chattr -i /lib/systemd/system/sshd.service
$sudo $cp \${conf} /lib/systemd/system/sshd.service
$sudo $chattr +i /lib/systemd/system/sshd.service
$lsattr /lib/systemd/system/sshd.service
}
conagent.reconfig()
{
local conf=\${1:?[ssh_config]}
$mkdir -p \$HOME/.ssh
$cp -f \$conf \$HOME/.ssh/ssh_config
$chmod u=r,go= \$HOME/.ssh/ssh_config
}
conagentd.reconfig()
{
local help="[conffile][ ipv6 :: | ipv4 x.x.x.x][Opt: allow users,def: ${user}]"
local conffile=\${1:?\${help}}
local listenip1=\${2:?\${help}}
\builtin shift 2
local allowusers=\${@:-${user}}
$mkdir -p \$HOME/.ssh
$cp -f \$conffile \$HOME/.ssh/sshd_config
\builtin printf "%s\n" "AllowUsers \${allowusers}" \
>> \$HOME/.ssh/sshd_config
\builtin printf "%s\n" "ListenAddress \${listenip1}" \
>> \$HOME/.ssh/sshd_config
$chmod ug=r,o= \$HOME/.ssh/sshd_config
$sudo $mv -f /etc/ssh/sshd_config /etc/ssh/sshd_config.origin
$sudo $ln -s \$HOME/.ssh/sshd_config /etc/ssh/sshd_config
\builtin printf "%s\n" $seed > \$HOME/.ssh/banner
$sudo $ln -fs \$HOME/.ssh/banner /etc/ssh/banner
}
scp.put.remote()
{
local help="Use Pathname expansion \"/tmp/\\*.pdf\" if path exists."
local cmd="$env TERM="xterm-256color" \
$scp -F \${HOME}/.ssh/ssh_config "
local host="\${1:?[host][source escape \*][target dir][port|.][user|.][verbose|.]}"
local source="\${2:?[source file][target dir][port|.][user|.][verbose|.]}"
local target=":\${3:?[target dir][port|.][user|.][verbose|.]}"
local port="\${4:-${port}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts|$egrep -v "#"))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
port=" -o port=\${port/./${port}}"
local user="\${5:-${user}}"
user="\${user/./${user}}"
local verbose=\${6:+" -vvv "}
if [[ ! "$port" =~ [[:digit:]]+ ]];then
\builtin printf "\$FUNCNAME: $port invalid port.\n\${help}\n"
return
fi
\${cmd}\${verbose}\${port} \${source} ${user}@\${host}\${target}
}
scp.get.remote()
{
local help="Use Pathname expansion \"/tmp/\\*.pdf\" if path exists."
local cmd="$env TERM="xterm" \
$scp -F \${HOME}/.ssh/ssh_config "
local host="\${1:?[host][source escape \*][target dir][port|.][user|.][verbose|.]}"
local source=":\${2:?[source file][target dir][port|.][user|.][verbose|.]}"
local target=\${3:?[target dir][port|.][user|.][verbose|.]}
local port="\${4:-${port}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts|$egrep -v "#"))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
port=" -o port=\${port/./${port}}"
local user="\${5:-${user}}"
user="\${user/./${user}}"
local verbose=\${6:+" -vvv "}
if [[ ! "$port" =~ [[:digit:]]+ ]];then
\builtin printf "\$FUNCNAME: $port invalid port.\n\${help}\n"
return
fi
\${cmd}\${verbose}\${port} ${user}@\${host}\${source} \${target}
}
_conagent()
{
local cmd="$env TERM=xterm-256color $ssh -F \${HOME}/.ssh/ssh_config "
local host="\${1:?[host][port|.][user|.][verbose|.]}"
local port="\${2:-${port}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts|$egrep -v "#"))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
port=" -o port=\${port/./${port}}"
local user="\${3:-${user}}"
user="\${user/./${user}}"
local verbose=\${4:+" -vvv "}
\${cmd}\${verbose}\${keyfile}\${user}@\${host}\${port}
}
_conagent.kill.socks()
{
# set -o xtrace
local help="[host][user][port]"
local cmd="$ssh -F \${HOME}/.ssh/ssh_config -fTN -O stop "
local host="@\${1:?\${help}}"
local user="\${2:-"${user}"}"
local port="\${3:-"${port}"}"
\${cmd}\${user}\${host} -o port=\${port}
# set +o xtrace
}
conagent.tunnel()
{
# remote port 22 require root user.
l|$egrep -o ocal cmd="$ssh -F \${HOME}/.ssh/ssh_config "
local usage="\${FUNCNAME}:[tunnel host][remote host][remote port:9418][tunnel port|.][user|.][localport|.]"
local host="\${1:?\${usage}}"
local remotehost="\${2:?\${usage}}"
local remoteport="\${3:?\${usage}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
local port="\${4:-${conagentremoteport1}}"
port=" -o port=\${port/./${conagentremoteport1}}"
local user="\${5:-$user}"
user="\${user/./$user}"
local localport=\${6:-46666}
set -o xtrace
\${cmd}${user}@\${host}\${port} \
-Llocalhost:\${localport}:\${remotehost}:\${remoteport} -N &
set +o xtrace
}
_conagent.socks()
{
local cmd="$ssh -F \${HOME}/.ssh/ssh_config -fTN "
local usage="\${FUNCNAME}:[remote host][remote port|.][user|.][local socksport|.][keyfile]"
local host="\${1:?\${usage}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
local port="\${2:-${conagentremoteport1}}"
port=" -o port=\${port/./${conagentremoteport1}}"
local user="\${3:-$user}"
user="\${user/./$user}"
local socksport1=\${4:-${conagentlocalport1}}
socksport=" -D \${socksport1/./${conagentlocalport1}} "
local keyfile="\${5:+" -i \$5"}"
local verbose=\${6:+" -vvv "}
# $groups |$egrep adm && return
# set -o xtrace
$lsof -i |$egrep "\${socksport1}" && return
\${cmd}\${keyfile}\${socksport}${user}@\${host}\${port}
# set +o xtrace
}
conagent.lsof()
{
$lsof -i
}
conagent.agent.unsetenv()
{
\builtin \unset SSH_AGENT_PID SSH_AUTH_SOCK;
}
conagent.agent.kill()
{
ps.kill ssh-agent
\builtin \unset SSH_AGENT_PID SSH_AUTH_SOCK;
}
sshfs.reconfig()
{
local conf=\${1:?[fuse.conf]}
$sudo $cp \$conf /etc/fuse.conf
}
sshfs.unmount.github()
{
\builtin \cd \$HOME && $fusermount3 -u $githubdir ||\
$sudo $umount -l $githubdir
}
sshfs.github()
{
$egrep $githubdir /proc/mounts && return
$sshfs $user@githost:$githubdir \
-o port=${port},allow_root\${address},ServerAliveInterval=15 \
$githubdir
}
sshfs.mount()
{
local host="\${1:?[hostname][remote folder][mountpoint|.][port|.][user|.][allow_root,|allow_other,|ro,|rw]}"
local folder="\${2:?[remote folder]}"
local mountpoint="\${3:-\${folder}}"
mountpoint="\${mountpoint/./\$folder}"
local port="port=\${4:-${port}}"
port="\${port/./${port}}"
local user="\${5:-${user}}"
user="\${user/./${user}}"
local option="\${6}"
option="\${6:+,\${option},uid=\$($id -u),gid=\$($id -u),allow_other}"
$sshfs $user@\$host:\$folder -o \${port}\${option} \$mountpoint
}
sshfs.unmount()
{
local mountpoint=\${1:?[mountpint]};
\builtin \cd \$HOME && $fusermount3 -u \$mountpoint
$sudo $umount -l $mountpoint
}
conagent.myip()
{
# set -o xtrace
local host=\${1:?[remote host]}
local port=\${2:-"${port}"}
local user=\${3:-"$user"}
local remotecmd="/bin/ss -4Hn state established"
local res="\$($env $ssh -T -F \
\${HOME}/.ssh/ssh_config ${user}@\${host} \
-o connectTimeout=10 -o port=\${port} "\${remotecmd}")"
local myport=\$($ss -4Hn state established|\
$egrep "\${remotehost}:\${port}"|\
$tail -n 1|$cut -d':' -f2|\
$tr -s [:blank:]|$cut -d' ' -f1)
\builtin printf "%s" "\${res}"|$egrep ":\${myport}"|\
$awk '{print \$NF}'|$cut -d':' -f1
# set +o xtrace
}
_conagent.send()
{
# set -o xtrace
local cmd="$env $ssh -T -F \${HOME}/.ssh/ssh_config "
local host="\${1:?[host][remote cmd|fun][opt port][opt user]}"
local remotecmd="\${2:-hostname} "
local port=" -o port=\${3:-${port}}"
local user="\${4:-${user}}"
declare -a Host=(\$($egrep -w "\$host" /etc/hosts))
[[ "\${Host[0]}" =~ : ]] && cmd="\${cmd}-6 "
\${cmd}${user}@\${host}\${port}<<-SSHSEND
\${remotecmd}
SSHSEND
# set +o xtrace
}
conagent.uninstall()
{
$sudo $rm -f $mandir/conagent.1
}
conagent.install()
{
[[ \$($basename \${PWD}) == conagent ]] || return
conagent.uninstall
$sudo $mkdir -p $mandir
$sudo $chmod 0755 $mandir
$sudo $cp doc/conagent.1 \
$mandir/conagent.1
$sudo $chmod 0644 $mandir/conagent.1
$sudo $chown $user:users \
$mandir/conagent.1
}
SUB
)
}
conagent.substitute
builtin unset -f conagent.substitute