mirror of
https://github.com/DictXiong/dotfiles.git
synced 2026-09-21 21:56:52 +08:00
Compare commits
No commits in common. "6fa601c458f1ce443ad87139e6846a0432584983" and "7e33f92e51d31f1649739892007796d39dd6dbd8" have entirely different histories.
6fa601c458
...
7e33f92e51
6
.zshrc2
6
.zshrc2
@ -132,11 +132,7 @@ alias "jcfu"='jc -fu'
|
|||||||
alias "sc"='systemctl'
|
alias "sc"='systemctl'
|
||||||
alias "t"='tmux'
|
alias "t"='tmux'
|
||||||
gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX ; }
|
gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX ; }
|
||||||
sagt() {
|
sagt() { eval "$($DOTFILES/tools/sagent.sh $@)" ; }
|
||||||
local output
|
|
||||||
output="$("$DOTFILES/tools/sagent.sh" "$@")" || return $?
|
|
||||||
eval "$output"
|
|
||||||
}
|
|
||||||
## nixos
|
## nixos
|
||||||
use() { nix --experimental-features nix-command --extra-experimental-features flakes shell "${(*)@/#%(#b)([^#]#)/nixpkgs#$match}" ; }
|
use() { nix --experimental-features nix-command --extra-experimental-features flakes shell "${(*)@/#%(#b)([^#]#)/nixpkgs#$match}" ; }
|
||||||
bnd() {
|
bnd() {
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
op=$(command -v op || command -v op.exe || true)
|
op=$(command -v op || command -v op.exe || true)
|
||||||
if [[ -z "$op" || ! -x "$op" ]]; then
|
if [[ ! -x $op ]]; then
|
||||||
echo "1Password CLI not found" >&2
|
echo "1password cli not found" > /dev/stderr
|
||||||
exit 1
|
exit -1
|
||||||
fi
|
fi
|
||||||
exec "$op" read "op://Personal/id25519-passphrase/$(hostname)"
|
"$op" read "op://Personal/id25519-passphrase/$(hostname)"
|
||||||
|
|||||||
226
tools/sagent.sh
226
tools/sagent.sh
@ -20,7 +20,6 @@ find_so_file()
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
create_agent()
|
create_agent()
|
||||||
@ -31,50 +30,27 @@ create_agent()
|
|||||||
|
|
||||||
kill_agent()
|
kill_agent()
|
||||||
{
|
{
|
||||||
local status
|
if pgrep -x ssh-agent > /dev/null; then
|
||||||
if pgrep -u "$EUID" -x ssh-agent > /dev/null; then
|
fmt_note "killing existing agent"
|
||||||
fmt_note "stopping existing ssh-agent"
|
pkill -9 -x ssh-agent
|
||||||
if pkill -TERM -u "$EUID" -x ssh-agent; then
|
|
||||||
:
|
|
||||||
else
|
|
||||||
status=$?
|
|
||||||
[[ $status -eq 1 ]] || return "$status"
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
if command -v gpgconf > /dev/null 2>&1; then
|
|
||||||
fmt_note "stopping gpg-agent if running"
|
|
||||||
gpgconf --kill gpg-agent
|
|
||||||
fi
|
|
||||||
unset SSH_AUTH_SOCK SSH_AGENT_PID
|
|
||||||
echo unset SSH_AUTH_SOCK SSH_AGENT_PID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
add_piv()
|
add_piv()
|
||||||
{
|
{
|
||||||
local SO_FILE
|
local SO_FILE=$(find_so_file)
|
||||||
if ! SO_FILE=$(find_so_file); then
|
if [[ -n "$SO_FILE" ]]; then
|
||||||
|
echo ssh-add -s \"$SO_FILE\"
|
||||||
|
else
|
||||||
fmt_error "opensc-pkcs11.so not found"
|
fmt_error "opensc-pkcs11.so not found"
|
||||||
return 1
|
|
||||||
fi
|
fi
|
||||||
printf 'ssh-add -s %q\n' "$SO_FILE"
|
|
||||||
list
|
list
|
||||||
}
|
}
|
||||||
|
|
||||||
add_id25519_with_op()
|
add_id25519_with_op()
|
||||||
{
|
{
|
||||||
local status
|
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS="$THIS_DIR/sagent-op.sh" timeout 60s ssh-add ~/.ssh/id_ed25519 || fmt_fatal "timed out when adding the key. probably the passphrase is wrong or 1password-cli is not working"
|
||||||
if SSH_ASKPASS_REQUIRE=force SSH_ASKPASS="$THIS_DIR/sagent-op.sh" timeout 60s ssh-add "$HOME/.ssh/id_ed25519"; then
|
list
|
||||||
list
|
|
||||||
return
|
|
||||||
else
|
|
||||||
status=$?
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ $status -eq 124 ]]; then
|
|
||||||
fmt_fatal "timed out when adding the key"
|
|
||||||
else
|
|
||||||
fmt_fatal "failed to add the key (ssh-add exit $status); check the key, agent, and 1Password CLI"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list()
|
list()
|
||||||
@ -83,188 +59,30 @@ list()
|
|||||||
echo ssh-add -l
|
echo ssh-add -l
|
||||||
}
|
}
|
||||||
|
|
||||||
configured_pinentry()
|
|
||||||
{
|
|
||||||
local agent_conf
|
|
||||||
local agent_confs=()
|
|
||||||
for agent_conf in "$@"; do
|
|
||||||
[[ -f "$agent_conf" ]] && agent_confs+=("$agent_conf")
|
|
||||||
done
|
|
||||||
[[ ${#agent_confs[@]} -gt 0 ]] || return 1
|
|
||||||
|
|
||||||
awk '
|
|
||||||
/^[[:space:]]*#/ { next }
|
|
||||||
{
|
|
||||||
line = $0
|
|
||||||
sub(/^[[:space:]]*/, "", line)
|
|
||||||
if (line ~ /^pinentry-program([[:space:]]|=)/) {
|
|
||||||
sub(/^pinentry-program[[:space:]=]*/, "", line)
|
|
||||||
sub(/[[:space:]]*$/, "", line)
|
|
||||||
pinentry = line
|
|
||||||
}
|
|
||||||
}
|
|
||||||
END {
|
|
||||||
if (pinentry == "") exit 1
|
|
||||||
print pinentry
|
|
||||||
}
|
|
||||||
' "${agent_confs[@]}"
|
|
||||||
}
|
|
||||||
|
|
||||||
check_pinentry()
|
|
||||||
{
|
|
||||||
local gnupg_home
|
|
||||||
local gpg_sysconfdir
|
|
||||||
local agent_conf
|
|
||||||
local system_agent_conf
|
|
||||||
local pinentry
|
|
||||||
local gpg_bindir
|
|
||||||
local candidate
|
|
||||||
|
|
||||||
gnupg_home=$(gpgconf --list-dirs homedir)
|
|
||||||
gpg_sysconfdir=$(gpgconf --list-dirs sysconfdir)
|
|
||||||
agent_conf="$gnupg_home/gpg-agent.conf"
|
|
||||||
system_agent_conf="$gpg_sysconfdir/gpg-agent.conf"
|
|
||||||
if pinentry=$(configured_pinentry "$system_agent_conf" "$agent_conf"); then
|
|
||||||
if [[ "$pinentry" == "~/"* ]]; then
|
|
||||||
pinentry="$HOME/${pinentry#\~/}"
|
|
||||||
fi
|
|
||||||
if [[ -x "$pinentry" ]]; then
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
fmt_warning "configured pinentry is not executable: $pinentry"
|
|
||||||
else
|
|
||||||
gpg_bindir=$(gpgconf --list-dirs bindir)
|
|
||||||
if [[ -x "$gpg_bindir/pinentry" || -x "$gpg_bindir/pinentry-basic" ]]; then
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
for candidate in \
|
|
||||||
"$(command -v pinentry-curses 2>/dev/null || true)" \
|
|
||||||
"$(command -v pinentry 2>/dev/null || true)" \
|
|
||||||
"$(command -v pinentry-tty 2>/dev/null || true)"; do
|
|
||||||
[[ -n "$candidate" && -x "$candidate" ]] && break
|
|
||||||
candidate=""
|
|
||||||
done
|
|
||||||
|
|
||||||
if [[ -n "$candidate" ]]; then
|
|
||||||
fmt_warning "gpg-agent has no usable pinentry; add 'pinentry-program $candidate' to $agent_conf"
|
|
||||||
else
|
|
||||||
fmt_warning "gpg-agent has no usable pinentry; install one and configure pinentry-program in $agent_conf"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
use_gpg_agent()
|
|
||||||
{
|
|
||||||
command -v gpgconf > /dev/null 2>&1 || fmt_fatal "gpgconf not found"
|
|
||||||
command -v gpg-connect-agent > /dev/null 2>&1 || fmt_fatal "gpg-connect-agent not found"
|
|
||||||
|
|
||||||
check_pinentry
|
|
||||||
|
|
||||||
local current_tty
|
|
||||||
current_tty=$(tty) || fmt_fatal "unable to determine the current TTY"
|
|
||||||
export GPG_TTY="$current_tty"
|
|
||||||
|
|
||||||
gpgconf --launch gpg-agent
|
|
||||||
gpg-connect-agent updatestartuptty /bye > /dev/null
|
|
||||||
|
|
||||||
local agent_socket
|
|
||||||
agent_socket=$(gpgconf --list-dirs agent-ssh-socket)
|
|
||||||
if [[ -z "$agent_socket" || ! -S "$agent_socket" ]]; then
|
|
||||||
fmt_fatal "gpg-agent SSH socket not found; add 'enable-ssh-support' to ~/.gnupg/gpg-agent.conf and restart gpg-agent"
|
|
||||||
fi
|
|
||||||
|
|
||||||
fmt_note "using gpg-agent: $agent_socket"
|
|
||||||
echo unset SSH_AGENT_PID
|
|
||||||
printf 'export GPG_TTY=%q\n' "$current_tty"
|
|
||||||
printf 'export SSH_AUTH_SOCK=%q\n' "$agent_socket"
|
|
||||||
}
|
|
||||||
|
|
||||||
read_agent_file()
|
|
||||||
{
|
|
||||||
local agent_file="$1"
|
|
||||||
local line
|
|
||||||
local agent_socket=""
|
|
||||||
local agent_pid=""
|
|
||||||
|
|
||||||
while IFS= read -r line; do
|
|
||||||
case "$line" in
|
|
||||||
SSH_AUTH_SOCK=*)
|
|
||||||
agent_socket=${line#SSH_AUTH_SOCK=}
|
|
||||||
agent_socket=${agent_socket%%;*}
|
|
||||||
;;
|
|
||||||
SSH_AGENT_PID=*)
|
|
||||||
agent_pid=${line#SSH_AGENT_PID=}
|
|
||||||
agent_pid=${agent_pid%%;*}
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done < "$agent_file"
|
|
||||||
|
|
||||||
[[ -n "$agent_socket" && "$agent_pid" =~ ^[1-9][0-9]*$ ]] || return 1
|
|
||||||
export SSH_AUTH_SOCK="$agent_socket"
|
|
||||||
export SSH_AGENT_PID="$agent_pid"
|
|
||||||
}
|
|
||||||
|
|
||||||
agent_is_usable()
|
|
||||||
{
|
|
||||||
[[ -S "$SSH_AUTH_SOCK" ]] || return 1
|
|
||||||
ps -p "$SSH_AGENT_PID" -o uid= -o comm= 2>/dev/null |
|
|
||||||
awk -v uid="$EUID" '$1 == uid && $2 ~ /(^|\/)ssh-agent$/ { found=1 } END { exit !found }' || return 1
|
|
||||||
|
|
||||||
local status
|
|
||||||
if ssh-add -l > /dev/null 2>&1; then
|
|
||||||
status=0
|
|
||||||
else
|
|
||||||
status=$?
|
|
||||||
fi
|
|
||||||
[[ $status -eq 0 || $status -eq 1 ]]
|
|
||||||
}
|
|
||||||
|
|
||||||
print_agent_env()
|
|
||||||
{
|
|
||||||
printf 'export SSH_AUTH_SOCK=%q\n' "$SSH_AUTH_SOCK"
|
|
||||||
printf 'export SSH_AGENT_PID=%q\n' "$SSH_AGENT_PID"
|
|
||||||
}
|
|
||||||
|
|
||||||
reset()
|
reset()
|
||||||
{
|
{
|
||||||
kill_agent
|
kill_agent
|
||||||
all already-killed
|
all
|
||||||
}
|
}
|
||||||
|
|
||||||
all()
|
all()
|
||||||
{
|
{
|
||||||
local mode="${1:-}"
|
test -d ~/.ssh || mkdir ~/.ssh
|
||||||
mkdir -p "$HOME/.ssh"
|
local agent_file=~/.ssh/agent-$(whoami)
|
||||||
local agent_file="$HOME/.ssh/agent-$(whoami)"
|
if [[ -f $agent_file ]]; then
|
||||||
[[ ! -L "$agent_file" ]] || fmt_fatal "refusing to use symlink as agent file: $agent_file"
|
source $agent_file > /dev/null
|
||||||
unset SSH_AUTH_SOCK SSH_AGENT_PID
|
|
||||||
|
|
||||||
if [[ "$mode" != "already-killed" && -f "$agent_file" ]]; then
|
|
||||||
chmod 600 "$agent_file"
|
|
||||||
read_agent_file "$agent_file" || true
|
|
||||||
else
|
else
|
||||||
touch "$agent_file"
|
touch $agent_file
|
||||||
chmod 600 "$agent_file"
|
chmod 600 $agent_file
|
||||||
fi
|
fi
|
||||||
|
if ! ps -p "$SSH_AGENT_PID" 1>/dev/null 2>&1; then
|
||||||
if ! agent_is_usable; then
|
kill_agent
|
||||||
if [[ "$mode" != "already-killed" ]]; then
|
|
||||||
kill_agent
|
|
||||||
fi
|
|
||||||
fmt_note "launching a new agent"
|
fmt_note "launching a new agent"
|
||||||
local agent_output
|
create_agent | tee $agent_file
|
||||||
if ! agent_output=$(create_agent); then
|
|
||||||
fmt_fatal "failed to launch ssh-agent"
|
|
||||||
fi
|
|
||||||
printf '%s\n' "$agent_output" > "$agent_file"
|
|
||||||
chmod 600 "$agent_file"
|
|
||||||
read_agent_file "$agent_file" || fmt_fatal "ssh-agent returned invalid environment data"
|
|
||||||
agent_is_usable || fmt_fatal "new ssh-agent is not usable"
|
|
||||||
else
|
else
|
||||||
fmt_note "using existing agent: $SSH_AGENT_PID"
|
fmt_note "using existing agent: $SSH_AGENT_PID"
|
||||||
|
cat $agent_file
|
||||||
fi
|
fi
|
||||||
print_agent_env
|
|
||||||
}
|
}
|
||||||
|
|
||||||
route()
|
route()
|
||||||
@ -287,9 +105,6 @@ route()
|
|||||||
op)
|
op)
|
||||||
add_id25519_with_op
|
add_id25519_with_op
|
||||||
;;
|
;;
|
||||||
gpg)
|
|
||||||
use_gpg_agent
|
|
||||||
;;
|
|
||||||
reset)
|
reset)
|
||||||
reset
|
reset
|
||||||
;;
|
;;
|
||||||
@ -298,7 +113,6 @@ route()
|
|||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
fmt_error "unknown command: $1"
|
fmt_error "unknown command: $1"
|
||||||
return 1
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user