Skip to content
Permalink
Browse files

[FIX] Make sure pinentry can always be launched by killing gpg-agent …

…on logout

- shutdown-gpg-agent which is started when the user logs in and sleep
  during the entire time, till a user logs out. When they log out, it kills
  an existing instance of gpg-agent.
- shutdown-gpg-agent plist is symlinked into each user account, since it
  has to reside in ~/Library/LaunchAgents, otherwise it's not informed
  of a logout.
  • Loading branch information...
lukele committed Jul 4, 2013
1 parent dc7ef0f commit ca576b20dba0f9176eaded1888b9f85d94d937f9
Showing with 99 additions and 3 deletions.
  1. +19 −1 Installer/macgpg2-postinstall.sh
  2. +20 −0 Installer/org.gpgtools.macgpg2.shutdown-gpg-agent.plist
  3. +53 −0 Installer/shutdown-gpg-agent
  4. +2 −1 build.sh
  5. +5 −1 packer
@@ -76,6 +76,7 @@ function userFixes {
GNUPGHOME=$homedir/.gnupg
fixGpgHome
fixGPGAgent
installShutdownAgentHelper "$homedir"
done

}
@@ -201,14 +202,31 @@ function cleanOldGpg {
rm -rf $e/doc/gnupg $e/gnupg
}

function installShutdownAgentHelper {
LAUNCH_AGENTS_DIR="$1/Library/LaunchAgents"
if [ ! -d "$LAUNCH_AGENTS_DIR" ]; then
mkdir -p "$LAUNCH_AGENTS_DIR";
fi
if [ -L "$LAUNCH_AGENTS_DIR/$PLIST_NAME" ]; then
rm "$LAUNCH_AGENTS_DIR/$PLIST_NAME"
fi
ln -s "/usr/local/MacGPG2/share/$PLIST_NAME" "$LAUNCH_AGENTS_DIR/$PLIST_NAME"
}

function registerShutdownAgentHelper {
launchctl unload "$HOME/Library/LaunchAgents/$PLIST_NAME" &> /dev/null
launchctl load "$HOME/Library/LaunchAgents/$PLIST_NAME"
}

################################################################################

SCRIPT_NAME=macgpg2
PLIST_NAME="org.gpgtools.macgpg2.shutdown-gpg-agent.plist"
[[ $EUID -eq 0 ]] || errExit "This script must be run as root"

cleanOldGpg
globalFixes
userFixes

registerShutdownAgentHelper


@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.gpgtools.shutdown-gpg-agent</string>

<key>Program</key>
<string>/usr/local/MacGPG2/libexec/shutdown-gpg-agent</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<true/>
</dict>
<key>ExitTimeOut</key>
<integer>5</integer>
</dict>
</plist>
@@ -0,0 +1,53 @@
#!/bin/bash

PROGRAM_NAME="gpg-agent"
LOGFILE="$HOME/Library/Logs/shutdown-gpg-agent.log"

function log {
current_date=$(date "+%Y-%m-%d %H:%M:%S")
echo "$current_date: $1"
if [[ "$2" != "" ]]; then
echo ""
fi
}

function agentPID {
echo $(ps axc | awk "{if (\$5==\"$PROGRAM_NAME\") print \$1}");
}

function onExit {
log "* Logout in progress..."
pid="$(agentPID)"

if [[ "$pid" == "" ]]; then
log "* gpg-agent not running, no reason to kill it." 1
return
fi

log " - killing gpg-agent..."
kill $pid

if [[ "$?" != "0" ]]; then
log "* Failed to kill gpg-agent. Sorry..." 1
else
log "* Successfully killed gpg-agent. job well done" 1
fi

exit 0
}

# Setting up the logfile.
exec >> $LOGFILE 2>&1

log "* Setting up kill event listeners"

# Setup the signals to trap which will invoke
# the onExit handler.
trap onExit SIGHUP SIGINT SIGTERM

log " - successfully setup kill event listeners"
log " - waiting for logout..."
# Sleep for approx. a year. Every user should shutdown there computer at
# least once a year.
sleep 31536000 &
wait $!
@@ -160,7 +160,8 @@ fi
# Move the MacGPG2_Updater plist file from $DEPLOYDIR/share/ into build,
# so the packager can find it.
cp -f "$SOURCEDIR/MacGPG2_Updater/$UPDATER_PLIST" "$BUILDDIR/" || (error "Failed to copy updater plist." && exit 1)

# Copy the shutdown-gpg-agent plist into share.
cp -f "$SOURCEDIR/Installer/org.gpgtools.macgpg2.shutdown-gpg-agent.plist" "$DEPLOYDIR/share" || (error "Failed to copy shutdown-gpg-agent plist." && exit 1)

success "Build succeeded!"
exit 0
6 packer
@@ -221,6 +221,7 @@ def main():
DIRS = ("bin", "sbin", "lib", "libexec", "share")
BASE_DIR = os.path.realpath(args[0])
DEST_DIR = os.path.realpath(args[1])
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))

BASE_DIR_ORIGINAL = args[0]
DEST_DIR_ORIGINAL = args[1]
@@ -320,7 +321,10 @@ def main():
import traceback
traceback.print_exc()
error("Failed to copy files from %s - %s" % (BASE_DIR_ORIGINAL, e))


# Copy the shutdown-gpg-agent file from Installer/shutdown-gpg-agent.
xcopy(os.path.join(CURRENT_DIR, "Installer", "shutdown-gpg-agent"), os.path.join(DEST_DIR, "libexec"))

# Change back to the original directory
os.chdir(working_dir)

0 comments on commit ca576b2

Please sign in to comment.
You can’t perform that action at this time.