dotfiles/tools/sagent.sh
Dict Xiong 483f7fd7f3
[dev] nixos; riot multiple targets; install without ssh keys (#42)
* auth: add .eid/authorized_certificates for pam pkcs11 auth

* .zshrc: alias sl for sudo zsh -l

* to-install: nix

* zshrc: use gnu ls on mac

* zshrc: try to use gnu-ls

* try to fix ci for macos

* riot: add domain box[0-9]

* riot: shortcuts i,x,j

* .zshrc: warn if not in main channel

* sagt: reset agent so paths

* sagt: import ssh-agent -P paths

* common.sh: is_port_free and get_free_port

* riot use get_free_port to fix issue on windows

* riot: ssh support instant command

* riot: proxy delimiter from comma (,) to slash (/)

* riot: support multiple remotes, delimiter=comma (,)

* riot: fix ci; install.sh: --no-ssh

* riot: improve ci

---------

Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
2023-11-06 19:13:22 +08:00

111 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export DFS_COLOR=1
source "$THIS_DIR/common.sh"
SO_PATHS=(
"/usr/lib/x86_64-linux-gnu/opensc-pkcs11.so" # ubuntu 22.04
"/run/current-system/sw/lib/opensc-pkcs11.so" # nixos 23.05
"/Library/OpenSC/lib/opensc-pkcs11.so" # macos 13.4
)
find_so_file()
{
local SO_FILE
for SO_FILE in ${SO_PATHS[*]}; do
if [[ -f "$SO_FILE" ]]; then
echo "$SO_FILE"
return
fi
done
}
create_agent()
{
local IFS=","
ssh-agent -P "${SO_PATHS[*]}"
}
kill_agent()
{
if pgrep -x ssh-agent > /dev/null; then
fmt_note "killing existing agent"
pkill -9 -x ssh-agent
fi
}
add_piv()
{
local SO_FILE=$(find_so_file)
if [[ -n "$SO_FILE" ]]; then
echo ssh-add -s \"$SO_FILE\"
else
fmt_error "opensc-pkcs11.so not found"
fi
list
}
list()
{
echo echo "available keys:"
echo ssh-add -l
}
reset()
{
kill_agent
all
}
all()
{
local agent_file="/tmp/piv-agent-$(whoami)"
if [[ -f $agent_file ]]; then
source $agent_file > /dev/null
else
touch $agent_file
chmod 600 $agent_file
fi
if ! ps -p "$SSH_AGENT_PID" 1>/dev/null 2>&1; then
kill_agent
fmt_note "launching a new agent"
create_agent | tee $agent_file
else
fmt_note "using existing agent: $SSH_AGENT_PID"
cat $agent_file
fi
}
route()
{
os_type="$(get_os_type)"
if [[ "$os_type" == "msys" || "$os_type" == "cygwin" ]]; then
fmt_fatal "unsupported platform: $os_type. you may use WinCryptSSHAgent."
fi
if [[ $# -eq 0 ]]; then
all
return
fi
case $1 in
kill)
kill_agent
;;
piv)
add_piv
;;
reset)
reset
;;
list|ls)
list
;;
*)
fmt_error "unknown command: $1"
;;
esac
}
route "$@"