|
| 1 | +#!/bin/bash |
| 2 | +set -ex |
| 3 | +user="$(/usr/bin/python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')" |
| 4 | +HOME="/Users/${user}" |
| 5 | +uagents="$HOME/Library/LaunchAgents" |
| 6 | +plist="net.cooperi.pivy-agent.plist" |
| 7 | +prefix="/opt/pivy" |
| 8 | +bindir="$prefix/bin" |
| 9 | + |
| 10 | +while true; do |
| 11 | + /usr/bin/osascript -e 'display dialog "Please insert your YubiKey and press OK"' |
| 12 | + |
| 13 | + # XXX: we just take the first one we see? |
| 14 | + while IFS=: read rdrname guid chuid ykpiv; do |
| 15 | + # check it's been set up with a CHUID |
| 16 | + if [[ "$chuid" == "false" && "$ykpiv" == "true" ]]; then |
| 17 | + # if it hasn't set up a basic one + 9e key so we can pin it. |
| 18 | + # the user can do the rest with pivy-tool later. |
| 19 | + $bindir/pivy-tool -g $guid init |
| 20 | + # "init" changes the guid |
| 21 | + guid=$($bindir/pivy-tool list -p | \ |
| 22 | + /usr/bin/grep "$rdrname" | /usr/bin/awk -F: '{print $2}') |
| 23 | + $bindir/pivy-tool -g $guid -a eccp256 generate 9e |
| 24 | + fi |
| 25 | + cak="$($bindir/pivy-tool -g $guid pubkey 9e)" |
| 26 | + |
| 27 | + /usr/bin/su "${user}" -c "/bin/mkdir -p \"${uagents}\"" |
| 28 | + # substitute placeholders in the plist |
| 29 | + /bin/cat /opt/pivy/share/net.cooperi.pivy-agent.plist | \ |
| 30 | + /usr/bin/sed -e "s|@@GUID@@|${guid}|g" -e "s|@@CAK@@|${cak}|g" \ |
| 31 | + -e "s|@@HOME@@|${HOME}|g" \ |
| 32 | + > "${uagents}/${plist}" |
| 33 | + chown "${user}" "${uagents}/${plist}" |
| 34 | + |
| 35 | + /usr/bin/su "${user}" -c "/bin/launchctl load \"${uagents}/${plist}\"" |
| 36 | + |
| 37 | + if ! /usr/bin/grep pivy-agent /etc/profile >/dev/null 2>/dev/null; then |
| 38 | + echo '# pivy-agent' >> /etc/profile |
| 39 | + echo 'if [[ ! -e "$SSH_AUTH_SOCK" || "$SSH_AUTH_SOCK" == *"launchd"* ]]; then' >> /etc/profile |
| 40 | + echo ' SSH_AUTH_SOCK=$HOME/.ssh/pivy-agent.sock; export SSH_AUTH_SOCK;' >>/etc/profile |
| 41 | + echo 'fi' >>/etc/profile |
| 42 | + fi |
| 43 | + exit 0 |
| 44 | + done < <($bindir/pivy-tool -p list) |
| 45 | +done |
0 commit comments