-
Notifications
You must be signed in to change notification settings - Fork 108
/
keychain.sh
executable file
·1500 lines (1348 loc) · 39.1 KB
/
keychain.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/sh
# Copyright 1999-2005 Gentoo Foundation
# Copyright 2007 Aron Griffis <[email protected]>
# Copyright 2009-2017 Funtoo Solutions, Inc.
# lockfile() Copyright 2009 Parallels, Inc.
# Distributed under the terms of the GNU General Public License v2
# Originally authored by Daniel Robbins <[email protected]>
# Maintained August 2002 - April 2003 by Seth Chandler <[email protected]>
# Maintained and rewritten April 2004 - July 2007 by Aron Griffis <[email protected]>
# Maintained July 2009 - Sept 2017 by Daniel Robbins <[email protected]>
# Maintained September 2017 - present by Ryan Harris <[email protected]>
version=##VERSION##
PATH="${PATH:-/usr/bin:/bin:/sbin:/usr/sbin:/usr/ucb}"
maintainer="[email protected]"
unset mesglog
unset myaction
unset agentsopt
havelock=false
unset hostopt
ignoreopt=false
noaskopt=false
noguiopt=false
nolockopt=false
lockwait=5
openssh=unknown
sunssh=unknown
confhost=unknown
sshconfig=false
quickopt=false
quietopt=false
clearopt=false
color=true
inheritwhich=local-once
unset stopwhich
unset timeout
unset ssh_timeout
attempts=1
unset sshavail
unset sshkeys
unset gpgkeys
unset mykeys
keydir="${HOME}/.keychain"
unset envf
evalopt=false
queryopt=false
confirmopt=false
absoluteopt=false
systemdopt=false
unset ssh_confirm
unset GREP_OPTIONS
gpg_prog_name="gpg"
BLUE="[34;01m"
CYAN="[36;01m"
CYANN="[36m"
GREEN="[32;01m"
RED="[31;01m"
PURP="[35;01m"
OFF="[0m"
# GNU awk and sed have regex issues in a multibyte environment. If any locale
# variables are set, then override by setting LC_ALL
unset pinentry_locale
if [ -n "$LANG$LC_ALL" ] || [ -n "$(locale 2>/dev/null | egrep -v '="?(|POSIX|C)"?$' 2>/dev/null)" ]; then
# save LC_ALL so that pinentry-curses works right. This has always worked
# correctly for me but peper and kloeri had problems with it.
pinentry_lc_all="$LC_ALL"
LC_ALL=C
export LC_ALL
fi
# synopsis: qprint "message"
qprint() {
$quietopt || echo "$*" >&2
}
# synopsis: mesg "message"
# Prettily print something to stderr, honors quietopt
mesg() {
qprint " ${GREEN}*${OFF} $*"
}
# synopsis: warn "message"
# Prettily print a warning to stderr
warn() {
echo " ${RED}* Warning${OFF}: $*" >&2
}
# synopsis: error "message"
# Prettily print an error
error() {
echo " ${RED}* Error${OFF}: $*" >&2
}
# synopsis: die "message"
# Prettily print an error, then abort
die() {
[ -n "$1" ] && error "$*"
qprint
$evalopt && { echo; echo "false;"; }
exit 1
}
# synopsis: versinfo
# Display the version information
versinfo() {
qprint
qprint " Copyright ${CYANN}2002-2006${OFF} Gentoo Foundation;"
qprint " Copyright ${CYANN}2007${OFF} Aron Griffis;"
qprint " Copyright ${CYANN}2009-2017${OFF} Funtoo Solutions, Inc;"
qprint " lockfile() Copyright ${CYANN}2009${OFF} Parallels, Inc."
qprint
qprint " Keychain is free software: you can redistribute it and/or modify"
qprint " it under the terms of the ${CYANN}GNU General Public License version 2${OFF} as"
qprint " published by the Free Software Foundation."
qprint
}
# synopsis: helpinfo
# Display the help information. There's no really good way to use qprint for
# this...
helpinfo() {
cat >&1 <<EOHELP
INSERT_POD_OUTPUT_HERE
EOHELP
}
# synopsis: testssh
# Figure out which ssh is in use, set the global boolean $openssh and $sunssh
testssh() {
# Query local host for SSH application, presently supporting
# OpenSSH, Sun SSH, and ssh.com
openssh=false
sunssh=false
case "$(ssh -V 2>&1)" in
*OpenSSH*) openssh=true ;;
*Sun?SSH*) sunssh=true ;;
esac
}
# synopsis: getuser
# Set the global string $me
getuser() {
# id -un gives euid, which might be different from USER or LOGNAME
me=$(id -un) || die "Who are you? id -un doesn't know..."
}
# synopsis: getos
# Set the global string $OSTYPE
getos() {
OSTYPE=$(uname) || die 'uname failed'
}
# synopsis: verifykeydir
# Make sure the key dir is set up correctly. Exits on error.
verifykeydir() {
# Create keydir if it doesn't exist already
if [ -f "${keydir}" ]; then
die "${keydir} is a file (it should be a directory)"
# Solaris 9 doesn't have -e; using -d....
elif [ ! -d "${keydir}" ]; then
( umask 0077 && mkdir "${keydir}"; ) || die "can't create ${keydir}"
fi
}
lockfile() {
# This function originates from Parallels Inc.'s OpenVZ vpsreboot script
# Description: This function attempts to acquire the lock. If it succeeds,
# it returns 0. If it fails, it returns 1. This function retuns immediately
# and only tries to acquire the lock once.
tmpfile="$lockf.$$"
echo $$ >"$tmpfile" 2>/dev/null || exit
if ln "$tmpfile" "$lockf" 2>/dev/null; then
rm -f "$tmpfile"
havelock=true && return 0
fi
if kill -0 $(cat $lockf 2>/dev/null) 2>/dev/null; then
rm -f "$tmpfile"
return 1
fi
if ln "$tmpfile" "$lockf" 2>/dev/null; then
rm -f "$tmpfile"
havelock=true && return 0
fi
rm -f "$tmpfile" "$lockf" && return 1
}
takelock() {
# Description: This function calls lockfile() multiple times if necessary
# to try to acquire the lock. It returns 0 on success and 1 on failure.
# Change in behavior: if timeout expires, we will forcefully acquire lock.
[ "$havelock" = "true" ] && return 0
[ "$nolockopt" = "true" ] && return 0
# First attempt:
lockfile && return 0
counter=0
mesg "Waiting $lockwait seconds for lock..."
while [ "$counter" -lt "$(( $lockwait * 2 ))" ]
do
lockfile && return 0
sleep 0.5; counter=$(( $counter + 1 ))
done
rm -f "$lockf" && lockfile && return 0
return 1
}
# synopsis: droplock
# Drops the lock if we're holding it.
droplock() {
$havelock && [ -n "$lockf" ] && rm -f "$lockf"
}
# synopsis: findpids [prog]
# Returns a space-separated list of agent pids.
# prog can be ssh or gpg, defaults to ssh. Note that if another prog is ever
# added, need to pay attention to the length for Solaris compatibility.
findpids() {
fp_prog=${1-ssh}
unset fp_psout
# Different systems require different invocations of ps. Try to generalize
# the best we can. The only requirement is that the agent command name
# appears in the line, and the PID is the first item on the line.
[ -n "$OSTYPE" ] || getos
# Try systems where we know what to do first
case "$OSTYPE" in
AIX|*bsd*|*BSD*|CYGWIN|darwin*|Linux|linux-gnu|OSF1)
fp_psout=$(ps x 2>/dev/null) ;; # BSD syntax
HP-UX)
fp_psout=$(ps -u $me 2>/dev/null) ;; # SysV syntax
SunOS)
case $(uname -r) in
[56]*)
fp_psout=$(ps -u $me 2>/dev/null) ;; # SysV syntax
*)
fp_psout=$(ps x 2>/dev/null) ;; # BSD syntax
esac ;;
GNU|gnu)
fp_psout=$(ps -g 2>/dev/null) ;; # GNU Hurd syntax
esac
# If we didn't get a match above, try a list of possibilities...
# The first one will probably fail on systems supporting only BSD syntax.
if [ -z "$fp_psout" ]; then
fp_psout=$(UNIX95=1 ps -u $me -o pid,comm 2>/dev/null | grep '^ *[0-9]')
[ -z "$fp_psout" ] && fp_psout=$(ps x 2>/dev/null)
[ -z "$fp_psout" ] && fp_psout=$(ps w 2>/dev/null) # Busybox syntax
fi
# Return the list of pids; ignore case for Cygwin.
# Check only 8 characters since Solaris truncates at that length.
# Ignore defunct ssh-agents (bug 28599)
if [ -n "$fp_psout" ]; then
echo "$fp_psout" | \
awk "BEGIN{IGNORECASE=1} /defunct/{next}
/$fp_prog-[a]gen/{print \$1}" | xargs
return 0
fi
# If none worked, we're stuck
error "Unable to use \"ps\" to scan for $fp_prog-agent processes"
error "Please report to $maintainer via http://bugs.gentoo.org"
return 1
}
# synopsis: stopagent [prog]
# --stop tells keychain to kill the existing agent(s)
# prog can be ssh or gpg, defaults to ssh.
stopagent() {
stop_prog=${1-ssh}
eval stop_except=\$\{${stop_prog}_agent_pid\}
stop_mypids=$(findpids "$stop_prog")
[ $? = 0 ] || die
if [ -z "$stop_mypids" ]; then
mesg "No $stop_prog-agent(s) found running"
return 0
fi
case "$stopwhich" in
all)
kill $stop_mypids >/dev/null 2>&1
mesg "All ${CYANN}$me${OFF}'s $stop_prog-agents stopped: ${CYANN}$stop_mypids${OFF}"
;;
others)
# Try to handle the case where we *will* inherit a pid
kill -0 $stop_except >/dev/null 2>&1
if [ -z "$stop_except" -o $? != 0 -o \
"$inheritwhich" = local -o "$inheritwhich" = any ]; then
if [ "$inheritwhich" != none ]; then
eval stop_except=\$\{inherit_${stop_prog}_agent_pid\}
kill -0 $stop_except >/dev/null 2>&1
if [ -z "$stop_except" -o $? != 0 ]; then
# Handle ssh2
eval stop_except=\$\{inherit_${stop_prog}2_agent_pid\}
fi
fi
fi
# Filter out the running agent pid
unset stop_mynewpids
for stop_x in $stop_mypids; do
[ $stop_x -eq $stop_except ] 2>/dev/null && continue
stop_mynewpids="${stop_mynewpids+$stop_mynewpids }$stop_x"
done
if [ -n "$stop_mynewpids" ]; then
kill $stop_mynewpids >/dev/null 2>&1
mesg "Other ${CYANN}$me${OFF}'s $stop_prog-agents stopped: ${CYANN}$stop_mynewpids${OFF}"
else
mesg "No other $stop_prog-agent(s) than keychain's $stop_except found running"
fi
;;
mine)
if [ $stop_except -gt 0 ] 2>/dev/null; then
kill $stop_except >/dev/null 2>&1
mesg "Keychain $stop_prog-agents stopped: ${CYANN}$stop_except${OFF}"
else
mesg "No keychain $stop_prog-agent found running"
fi
;;
esac
# remove pid files if keychain-controlled
if [ "$stopwhich" != others ]; then
if [ "$stop_prog" != ssh ]; then
rm -f "${pidf}-$stop_prog" "${cshpidf}-$stop_prog" "${fishpidf}-$stop_prog" 2>/dev/null
else
rm -f "${pidf}" "${cshpidf}" "${fishpidf}" 2>/dev/null
fi
eval unset ${stop_prog}_agent_pid
fi
}
# synopsis: inheritagents
# Save agent variables from the environment before they get wiped out
inheritagents() {
# Verify these global vars are null
unset inherit_ssh_auth_sock inherit_ssh_agent_pid
unset inherit_ssh2_auth_sock inherit_ssh2_agent_sock
unset inherit_gpg_agent_info inherit_gpg_agent_pid
# Save variables so we can inherit a running agent
if [ "$inheritwhich" != none ]; then
if wantagent ssh; then
if [ -n "$SSH_AUTH_SOCK" ]; then
inherit_ssh_auth_sock="$SSH_AUTH_SOCK"
inherit_ssh_agent_pid="$SSH_AGENT_PID"
fi
if [ -n "$SSH2_AUTH_SOCK" ]; then
inherit_ssh2_auth_sock="$SSH2_AUTH_SOCK"
inherit_ssh2_agent_pid="$SSH2_AGENT_PID"
fi
fi
if wantagent gpg; then
if [ -n "$GPG_AGENT_INFO" ]; then
inherit_gpg_agent_info="$GPG_AGENT_INFO"
inherit_gpg_agent_pid=$(echo "$GPG_AGENT_INFO" | cut -f2 -d:)
# GnuPG v.2.1+ removes $GPG_AGENT_INFO
else
gpg_socket_dir="${GNUPGHOME:=$HOME/.gnupg}"
if [ ! -S "${GNUPGHOME:=$HOME/.gnupg}/S.gpg-agent" ]; then
gpg_socket_dir="${XDG_RUNTIME_DIR}/gnupg"
fi
if [ -S "${gpg_socket_dir}/S.gpg-agent" ]; then
inherit_gpg_agent_pid=$(findpids "${gpg_prog_name}")
inherit_gpg_agent_info="${gpg_socket_dir}/S.gpg-agent:${inherit_gpg_agent_pid}:1"
fi
fi
fi
fi
}
# synopsis: validinherit
# Test inherit_* variables for validity
validinherit() {
vi_agent="$1"
vi_status=0
if [ "$vi_agent" = ssh ]; then
if [ -n "$inherit_ssh_auth_sock" ]; then
ls "$inherit_ssh_auth_sock" >/dev/null 2>&1
if [ $? != 0 ]; then
warn "SSH_AUTH_SOCK in environment is invalid; ignoring it"
unset inherit_ssh_auth_sock inherit_ssh_agent_pid
vi_status=1
fi
fi
if [ -n "$inherit_ssh2_auth_sock" ]; then
ls "$inherit_ssh2_auth_sock" >/dev/null 2>&1
if [ $? != 0 ]; then
warn "SSH2_AUTH_SOCK in environment is invalid; ignoring it"
unset inherit_ssh2_auth_sock inherit_ssh2_agent_pid
vi_status=1
fi
fi
elif [ "$vi_agent" = gpg ]; then
if [ -n "$inherit_gpg_agent_pid" ]; then
kill -0 "$inherit_gpg_agent_pid" >/dev/null 2>&1
if [ $? != 0 ]; then
unset inherit_gpg_agent_pid inherit_gpg_agent_info
warn "GPG_AGENT_INFO in environment is invalid; ignoring it"
vi_status=1
fi
fi
fi
return $vi_status
}
# synopsis: catpidf_shell shell agents...
# cat the pid files for the given agents. This is used by loadagents and also
# for keychain output when --eval is given.
catpidf_shell() {
case "$1" in
*/fish|fish) cp_pidf="$fishpidf" ;;
*csh) cp_pidf="$cshpidf" ;;
*) cp_pidf="$pidf" ;;
esac
shift
for cp_a in "$@"; do
case "${cp_a}" in
ssh) [ -f "$cp_pidf" ] && cat "$cp_pidf" ;;
*) [ -f "${cp_pidf}-$cp_a" ] && cat "${cp_pidf}-$cp_a" ;;
esac
echo
done
return 0
}
# synopsis: catpidf agents...
# cat the pid files for the given agents, appropriate for the current value of
# $SHELL. This is used for keychain output when --eval is given.
catpidf() {
catpidf_shell "$SHELL" "$@"
}
# synopsis: loadagents agents...
# Load agent variables from $pidf and copy implementation-specific environment
# variables into generic global strings
loadagents() {
for la_a in "$@"; do
case "$la_a" in
ssh)
unset SSH_AUTH_SOCK SSH_AGENT_PID SSH2_AUTH_SOCK SSH2_AGENT_PID
eval "$(catpidf_shell sh $la_a)"
if [ -n "$SSH_AUTH_SOCK" ]; then
ssh_auth_sock=$SSH_AUTH_SOCK
ssh_agent_pid=$SSH_AGENT_PID
elif [ -n "$SSH2_AUTH_SOCK" ]; then
ssh_auth_sock=$SSH2_AUTH_SOCK
ssh_agent_pid=$SSH2_AGENT_PID
else
unset ssh_auth_sock ssh_agent_pid
fi
;;
gpg)
unset GPG_AGENT_INFO
eval "$(catpidf_shell sh $la_a)"
if [ -n "$GPG_AGENT_INFO" ]; then
la_IFS="$IFS" # save current IFS
IFS=':' # set IFS to colon to separate PATH
set -- $GPG_AGENT_INFO
IFS="$la_IFS" # restore IFS
gpg_agent_pid=$2
fi
;;
*)
eval "$(catpidf_shell sh $la_a)"
;;
esac
done
return 0
}
# synopsis: startagent [prog]
# Starts an agent if it isn't already running.
# Requires $ssh_agent_pid
startagent() {
start_prog=${1-ssh}
start_proto=${2-${start_prog}}
unset start_pid
start_inherit_pid=none
start_mypids=$(findpids "$start_prog")
[ $? = 0 ] || die
# Unfortunately there isn't much way to genericize this without introducing
# a lot more supporting code/structures.
if [ "$start_prog" = ssh ]; then
start_pidf="$pidf"
start_cshpidf="$cshpidf"
start_fishpidf="$fishpidf"
start_pid="$ssh_agent_pid"
if [ -n "$inherit_ssh_auth_sock" -o -n "$inherit_ssh2_auth_sock" ]; then
if [ -n "$inherit_ssh_agent_pid" ]; then
start_inherit_pid="$inherit_ssh_agent_pid"
elif [ -n "$inherit_ssh2_agent_pid" ]; then
start_inherit_pid="$inherit_ssh2_agent_pid"
else
start_inherit_pid="forwarded"
fi
fi
else
start_pidf="${pidf}-$start_prog"
start_cshpidf="${cshpidf}-$start_prog"
start_fishpidf="${fishpidf}-$start_prog"
if [ "$start_prog" = gpg ]; then
start_pid="$gpg_agent_pid"
if [ -n "$inherit_gpg_agent_pid" ]; then
start_inherit_pid="$inherit_gpg_agent_pid"
fi
else
error "I don't know how to start $start_prog-agent (1)"
return 1
fi
fi
[ "$start_pid" -gt 0 ] 2>/dev/null || start_pid=none
# This hack makes the case statement easier
if [ "$inheritwhich" = any -o "$inheritwhich" = any-once ]; then
start_fwdflg=forwarded
else
unset start_fwdflg
fi
# Check for an existing agent
start_tester="$inheritwhich: $start_mypids $start_fwdflg "
case "$start_tester" in
none:*" $start_pid "*|*-once:*" $start_pid "*)
mesg "Found existing ${start_prog}-agent: ${CYANN}$start_pid${OFF}"
return 0
;;
*:*" $start_inherit_pid "*)
# This test was postponed until now to prevent generating warnings
validinherit "$start_prog"
if [ $? != 0 ]; then
# inherit_* vars have been removed from the environment. Try
# again now
startagent "$start_prog"
return $?
fi
mesg "Inheriting ${start_prog}-agent ($start_inherit_pid)"
;;
*)
# start_inherit_pid might be "forwarded" which we don't allow with,
# for example, local-once (the default setting)
start_inherit_pid=none
;;
esac
# Init the bourne-formatted pidfile
( umask 0177 && :> "$start_pidf"; )
if [ $? != 0 ]; then
rm -f "$start_pidf" "$start_cshpidf" "$start_fishpidf" 2>/dev/null
error "can't create $start_pidf"
return 1
fi
# Init the csh-formatted pidfile
( umask 0177 && :> "$start_cshpidf"; )
if [ $? != 0 ]; then
rm -f "$start_pidf" "$start_cshpidf" "$start_fishpidf" 2>/dev/null
error "can't create $start_cshpidf"
return 1
fi
# Init the fish-formatted pidfile
( umask 0177 && :> "$start_fishpidf"; )
if [ $? != 0 ]; then
rm -f "$start_pidf" "$start_cshpidf" "$start_fishpidf" 2>/dev/null
error "can't create $start_fishpidf"
return 1
fi
# Determine content for files
unset start_out
if [ "$start_inherit_pid" = none ]; then
# Start the agent.
# Branch again since the agents start differently
mesg "Starting ${start_prog}-agent..."
if [ "$start_prog" = ssh ]; then
start_out=$(ssh-agent ${ssh_timeout})
elif [ "$start_prog" = gpg ]; then
if [ -n "${timeout}" ]; then
gpg_cache_ttl="$(expr $timeout \* 60)"
start_gpg_timeout="--default-cache-ttl $gpg_cache_ttl --max-cache-ttl $gpg_cache_ttl"
else
unset start_gpg_timeout
fi
# the 1.9.x series of gpg spews debug on stderr
start_out=$(gpg-agent --daemon --write-env-file $start_gpg_timeout 2>/dev/null)
else
error "I don't know how to start $start_prog-agent (2)"
return 1
fi
if [ $? != 0 -a $? != 2 ]; then
rm -f "$start_pidf" "$start_cshpidf" "$start_fishpidf" 2>/dev/null
error "Failed to start ${start_prog}-agent"
return 1
fi
elif [ "$start_prog" = ssh -a -n "$inherit_ssh_auth_sock" ]; then
start_out="SSH_AUTH_SOCK=$inherit_ssh_auth_sock; export SSH_AUTH_SOCK;"
if [ "$inherit_ssh_agent_pid" -gt 0 ] 2>/dev/null; then
start_out="$start_out
SSH_AGENT_PID=$inherit_ssh_agent_pid; export SSH_AGENT_PID;"
fi
elif [ "$start_prog" = ssh -a -n "$inherit_ssh2_auth_sock" ]; then
start_out="SSH2_AUTH_SOCK=$inherit_ssh2_auth_sock; export SSH2_AUTH_SOCK;
SSH2_AGENT_PID=$inherit_ssh2_agent_pid; export SSH2_AGENT_PID;"
if [ "$inherit_ssh2_agent_pid" -gt 0 ] 2>/dev/null; then
start_out="$start_out
SSH2_AGENT_PID=$inherit_ssh2_agent_pid; export SSH2_AGENT_PID;"
fi
elif [ "$start_prog" = "${gpg_prog_name}" -a -n "$inherit_gpg_agent_info" ]; then
start_out="GPG_AGENT_INFO=$inherit_gpg_agent_info; export GPG_AGENT_INFO;"
else
die "something bad happened" # should never be here
fi
# Add content to pidfiles.
# Some versions of ssh-agent don't understand -s, which means to
# generate Bourne shell syntax. It appears they also ignore SHELL,
# according to http://bugs.gentoo.org/show_bug.cgi?id=52874
# So make no assumptions.
start_out=$(echo "$start_out" | grep -v 'Agent pid')
case "$start_out" in
setenv*)
echo "$start_out" >"$start_cshpidf"
echo "$start_out" | awk '{print $2"="$3" export "$2";"}' >"$start_pidf"
;;
*)
echo "$start_out" >"$start_pidf"
echo "$start_out" | sed 's/;.*/;/' | sed 's/=/ /' | sed 's/^/setenv /' >"$start_cshpidf"
echo "$start_out" | sed 's/;.*/;/' | sed 's/^\(.*\)=\(.*\);/set -e \1; set -x -U \1 \2;/' >"$start_fishpidf"
;;
esac
# Hey the agent should be started now... load it up!
loadagents "$start_prog"
}
# synopsis: extract_fingerprints
# Extract the fingerprints from standard input, returns space-separated list.
# Utility routine for ssh_l and ssh_f
extract_fingerprints() {
while read ef_line; do
case "$ef_line" in
*\ *\ [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:*)
# Sun SSH spits out different things depending on the type of
# key. For example:
# md5 1024 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 /home/barney/.ssh/id_dsa(DSA)
# 2048 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 /home/barney/.ssh/id_rsa.pub
echo "$ef_line" | cut -f3 -d' '
;;
*\ [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:*)
# The more consistent OpenSSH format, we hope
# 1024 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 /home/barney/.ssh/id_dsa (DSA)
echo "$ef_line" | cut -f2 -d' '
;;
*\ [A-Z0-9][A-Z0-9]*:[A-Za-z0-9+/][A-Za-z0-9+/]*)
# The new OpenSSH 6.8+ format,
# 1024 SHA256:mVPwvezndPv/ARoIadVY98vAC0g+P/5633yTC4d/wXE /home/barney/.ssh/id_dsa (DSA)
echo "$ef_line" | cut -f2 -d' '
;;
*)
# Fall back to filename. Note that commercial ssh is handled
# explicitly in ssh_l and ssh_f, so hopefully this rule will
# never fire.
warn "Can't determine fingerprint from the following line, falling back to filename"
mesg "$ef_line"
basename "$ef_line" | sed 's/[ (].*//'
;;
esac
done | xargs
}
# synopsis: ssh_l
# Return space-separated list of known fingerprints
ssh_l() {
sl_mylist=$(ssh-add -l 2>/dev/null)
sl_retval=$?
if $openssh; then
# Error codes:
# 0 success
# 1 OpenSSH_3.8.1p1 on Linux: no identities (not an error)
# OpenSSH_3.0.2p1 on HP-UX: can't connect to auth agent
# 2 can't connect to auth agent
case $sl_retval in
0)
echo "$sl_mylist" | extract_fingerprints
;;
1)
case "$sl_mylist" in
*"open a connection"*) sl_retval=2 ;;
esac
;;
esac
return $sl_retval
elif $sunssh; then
# Error codes (from http://docs.sun.com/db/doc/817-3936/6mjgdbvio?a=view)
# 0 success (even when there are no keys)
# 1 error
case $sl_retval in
0)
echo "$sl_mylist" | extract_fingerprints
;;
1)
case "$sl_mylist" in
*"open a connection"*) sl_retval=2 ;;
esac
;;
esac
return $sl_retval
else
# Error codes:
# 0 success - however might say "The authorization agent has no keys."
# 1 can't connect to auth agent
# 2 bad passphrase
# 3 bad identity file
# 4 the agent does not have the requested identity
# 5 unspecified error
if [ $sl_retval = 0 ]; then
# Output of ssh-add -l:
# The authorization agent has one key:
# id_dsa_2048_a: 2048-bit dsa, [email protected], Fri Jul 25 2003 10:53:49 -0400
# Since we don't have a fingerprint, just get the filenames *shrug*
echo "$sl_mylist" | sed '2,$s/:.*//' | xargs
fi
return $sl_retval
fi
}
# synopsis: ssh_f filename
# Return fingerprint for a keyfile
# Requires $openssh or $sunssh
ssh_f() {
sf_filename="$1"
if $openssh || $sunssh; then
realpath_bin="$(command -v realpath)"
# if private key is symlink and symlink to *.pub is missing:
if [ -L "$sf_filename" ] && [ ! -z "$realpath_bin" ]; then
sf_filename="$($realpath_bin $sf_filename)"
fi
lsf_filename="$sf_filename.pub"
if [ ! -f "$lsf_filename" ]; then
# try to remove extension from private key, *then* add .pub, and see if we now find it:
if [ -L "$sf_filename" ] && [ ! -z "$realpath_bin" ]; then
sf_filename="$($realpath_bin $sf_filename)"
fi
lsf_filename=$(echo "$sf_filename" | sed 's/\.[^\.]*$//').pub
if [ ! -f "$lsf_filename" ]; then
warn "Cannot find separate public key for $1."
lsf_filename="$sf_filename"
fi
fi
sf_fing=$(ssh-keygen -l -f "$lsf_filename") || return 1
echo "$sf_fing" | extract_fingerprints
else
# can't get fingerprint for ssh2 so use filename *shrug*
basename "$sf_filename"
fi
return 0
}
# synopsis: gpg_listmissing
# Uses $gpgkeys
# Returns a newline-separated list of keys found to be missing.
gpg_listmissing() {
unset glm_missing
GPG_TTY=$(tty)
# Parse $gpgkeys into positional params to preserve spaces in filenames
set -f # disable globbing
glm_IFS="$IFS" # save current IFS
IFS="
" # set IFS to newline
set -- $gpgkeys
IFS="$glm_IFS" # restore IFS
set +f # re-enable globbing
for glm_k in "$@"; do
# Check if this key is known to the agent. Don't know another way...
if echo | env -i GPG_TTY="$GPG_TTY" PATH="$PATH" GPG_AGENT_INFO="$GPG_AGENT_INFO" \
"${gpg_prog_name}" --no-options --use-agent --no-tty --sign --local-user "$glm_k" -o- >/dev/null 2>&1; then
# already know about this key
mesg "Known gpg key: ${CYANN}${glm_k}${OFF}"
continue
else
# need to add this key
if [ -z "$glm_missing" ]; then
glm_missing="$glm_k"
else
glm_missing="$glm_missing
$glm_k"
fi
fi
done
echo "$glm_missing"
}
# synopsis: ssh_listmissing
# Uses $sshkeys and $sshavail
# Returns a newline-separated list of keys found to be missing.
ssh_listmissing() {
unset slm_missing
# Parse $sshkeys into positional params to preserve spaces in filenames
set -f # disable globbing
slm_IFS="$IFS" # save current IFS
IFS="
" # set IFS to newline
set -- $sshkeys
IFS="$slm_IFS" # restore IFS
set +f # re-enable globbing
for slm_k in "$@"; do
# Fingerprint current user-specified key
slm_finger=$(ssh_f "$slm_k") || continue
# Check if it needs to be added
case " $sshavail " in
*" $slm_finger "*)
# already know about this key
mesg "Known ssh key: ${CYANN}${slm_k}${OFF}"
;;
*)
# need to add this key
if [ -z "$slm_missing" ]; then
slm_missing="$slm_k"
else
slm_missing="$slm_missing
$slm_k"
fi
;;
esac
done
echo "$slm_missing"
}
# synopsis: add_gpgkey
# Adds a key to $gpgkeys
add_gpgkey() {
gpgkeys=${gpgkeys+"$gpgkeys
"}"$1"
}
# synopsis: add_sshkey
# Adds a key to $sshkeys
add_sshkey() {
sshkeys=${sshkeys+"$sshkeys
"}"$1"
}
# synopsis: parse_mykeys
# Sets $sshkeys and $gpgkeys based on $mykeys
parse_mykeys() {
# Possible path to the private key: if --confhost variable used.
pkeypath="$1"
# Parse $mykeys into positional params to preserve spaces in filenames
set -f # disable globbing
pm_IFS="$IFS" # save current IFS
IFS="
" # set IFS to newline
set -- $mykeys
IFS="$pm_IFS" # restore IFS
set +f # re-enable globbing
for pm_k in "$@"; do
# Check for ssh
if wantagent ssh; then
if [ -f "$pm_k" ]; then
add_sshkey "$pm_k" ; continue
elif [ -f "$HOME/.ssh/$pm_k" ]; then
add_sshkey "$HOME/.ssh/$pm_k" ; continue
elif [ -f "$HOME/.ssh2/$pm_k" ]; then
add_sshkey "$HOME/.ssh2/$pm_k" ; continue
elif [ -f "$pkeypath" ]; then
add_sshkey "$pkeypath"; continue
fi
fi
# Check for gpg
if wantagent gpg; then
"${gpg_prog_name}" --list-secret-keys "$pm_k" >/dev/null 2>&1
if [ $? -eq 0 ]; then
add_gpgkey "$pm_k" ; continue
fi
fi
$ignoreopt || warn "can't find $pm_k; skipping"
continue
done
return 0
}
# synopsis: setaction
# Sets $myaction or dies if $myaction is already set
setaction() {
if [ -n "$myaction" ]; then
die "you can't specify --$myaction and $1 at the same time"
else
myaction="$1"
fi
}
# synopsis: setagents
# Check validity of agentsopt
setagents() {
if [ -n "$agentsopt" ]; then
agentsopt=$(echo "$agentsopt" | sed 's/,/ /g')
unset new_agentsopt
for a in $agentsopt; do
if command -v ${a}-agent >/dev/null; then
new_agentsopt="${new_agentsopt+$new_agentsopt }${a}"
else
warn "can't find ${a}-agent, removing from list"
fi
done
agentsopt="${new_agentsopt}"
else
for a in ssh; do
command -v ${a}-agent >/dev/null || continue
agentsopt="${agentsopt+$agentsopt }${a}"
done
fi
if [ -z "$agentsopt" ]; then
die "no agents available to start"
fi
}
# synopsis: confpath
# Return private key path if found in ~/.ssh/config SSH configuration file.
# Input: the name of the host we would like to connect to.
confpath() {
h=""
while IFS= read -r line; do
# get the Host directives
case $line in
*"Host "*) h=$(echo $line | awk '{print $2}') ;;
esac
case $line in
*IdentityFile*)
if [ $h = "$1" ]; then
echo $line | awk '{print $2}'
break
fi
esac
done < ~/.ssh/config
}
# synopsis: wantagent prog
# Return 0 (true) or 1 (false) depending on whether prog is one of the agents in
# agentsopt
wantagent() {
case "$agentsopt" in
"$1"|"$1 "*|*" $1 "*|*" $1")
return 0 ;;
*)