diff --git a/.zshrc2 b/.zshrc2 index 8526501..bfde9ad 100644 --- a/.zshrc2 +++ b/.zshrc2 @@ -113,44 +113,7 @@ if [[ -x $(command -v trash) ]]; then alias "rm"="echo use the full path i.e. '/bin/rm'\; consider using trash" fi 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 } -piv-agent() -{ - local agent_file="/tmp/piv-agent-$(whoami)" - if [[ -f $agent_file ]]; then - source $agent_file - fi - # they are: ubuntu, macos, respectively - local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" ) - local SO_FILE - for SO_FILE in ${SO_PATHS[*]}; do - if [[ -f "$SO_FILE" ]]; then - break - fi - done - if [[ ! -f "$SO_FILE" ]]; then - echo "error: opensc-pkcs11.so not found" - return 1 - fi - if ! ps -p "$SSH_AGENT_PID" > /dev/null; then - echo "launch agent using: $SO_FILE" - ret=$(ssh-agent -P "$SO_FILE") - echo "$ret" > $agent_file - eval "$ret" - if ! ps -p $SSH_AGENT_PID > /dev/null; then - echo "failed to launch agent" - return 1 - fi - else - echo "using existing agent: $SSH_AGENT_PID" - fi - if ! ssh-add -l > /dev/null; then - echo "no keys found in this agent, let's add some" - ssh-add -s "$SO_FILE" - fi - pgrep ssh-agent | grep -v $SSH_AGENT_PID | xargs kill -9 - echo "now available keys:" - ssh-add -l -} +piv-agent() { eval "$($DOTFILES/tools/sagent.sh $@)" } # key bindings bindkey "^b" beginning-of-line # ctrl+b diff --git a/tools/common.sh b/tools/common.sh index 6ceb6a9..926cae7 100755 --- a/tools/common.sh +++ b/tools/common.sh @@ -75,11 +75,11 @@ fmt_warning() { } fmt_info() { - printf '%sinfo: %s\n' "${FMT_RESET}" "$*" >&1 + printf '%sinfo: %s\n' "${FMT_RESET}" "$*" >&2 } fmt_note() { - printf '%s%s%s\n' "${FMT_GREEN}" "$*" "${FMT_RESET}" >&1 + printf '%s%s%s\n' "${FMT_GREEN}" "$*" "${FMT_RESET}" >&2 } setup_color() { diff --git a/tools/sagent.sh b/tools/sagent.sh new file mode 100755 index 0000000..ba129d7 --- /dev/null +++ b/tools/sagent.sh @@ -0,0 +1,105 @@ +#!/bin/bash +set -e +THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) +export DFS_COLOR=1 +source "$THIS_DIR/common.sh" + + +find_so_file() +{ + local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" ) + local SO_FILE + for SO_FILE in ${SO_PATHS[*]}; do + if [[ -f "$SO_FILE" ]]; then + echo "$SO_FILE" + return + fi + done +} + +create_agent() +{ + local SO_FILE=$(find_so_file) + if [[ -n "$SO_FILE" ]]; then + fmt_note "opensc-pkcs11.so found" + SO_FILE="-P $SO_FILE" + fi + ssh-agent $SO_FILE +} + +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" > /dev/null; 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() +{ + 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 "$@" \ No newline at end of file diff --git a/tools/test.zsh b/tools/test.zsh index 4deeac7..cbe6adf 100644 --- a/tools/test.zsh +++ b/tools/test.zsh @@ -18,6 +18,7 @@ grep -q ".zshrc2" ~/.zshrc dfs version dfs log 1 dfs beacon gh.ci $GITHUB_SHA +sagent z ~ test ~ -ef "$(pwd)" dogo