[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#832445: openssh: Rework upstart ssh-agent job and add systemd user unit



Package: openssh-client
Severity: wishlist
Version: 7.2p2-7
Tags: patch
User: ubuntu-devel@lists.ubuntu.com
Usertags: origin-ubuntu ubuntu-patch yakkety systemd-session

Hello,

in Ubuntu we are currently converting the user session from upstart to
systemd [1]. The upstart sessions were entirely Ubuntu specific, but
the systemd user units are aimed to work on every distro and thus I
attempt to standardize those (see [2]).

These two patches refactor and improve the current upstart job and add
a systemd unit. Please see the commit logs for details.

At the moment Debian uses neither upstart nor systemd for its user
sessions, to this is a no-op for Debian. But Colin wants to keep the
packages in sync, so it would be great if you could apply those. The
dead weight is just three very small files.

Thanks for considering,

Martin

[1] https://blueprints.launchpad.net/ubuntu/+spec/convergence-y-replace-upstart
[2] https://github.com/systemd/systemd/pull/3678

-- 
Martin Pitt                        | http://www.piware.de
Ubuntu Developer (www.ubuntu.com)  | Debian Developer  (www.debian.org)
From 01ab606b2e5251fd40da72ff978d7c9e8928a7a6 Mon Sep 17 00:00:00 2001
From: Martin Pitt <martin.pitt@ubuntu.com>
Date: Mon, 25 Jul 2016 17:03:17 +0200
Subject: [PATCH 1/2] Add debian/agent-launch: Helper script for conditionally
 starting the SSH agent in the user session

Use it in ssh-agent.user-session.upstart. This will also be used in a
corresponding systemd user unit.

This replaces the backgrounded "ssh-agent -s" with a foreground task which
works more nicely with modern init systems for logging/debugging and
starting/stopping.

Also use a fixed socket file name in $XDG_RUNTIME_DIR -- under both upstart and
systemd we can assume this, and it allows restarting the service in a running
session.
---
 debian/agent-launch                   | 29 +++++++++++++++++++++++++++++
 debian/changelog                      |  7 +++++++
 debian/openssh-client.install         |  2 ++
 debian/ssh-agent.user-session.upstart | 20 ++------------------
 4 files changed, 40 insertions(+), 18 deletions(-)
 create mode 100755 debian/agent-launch

diff --git a/debian/agent-launch b/debian/agent-launch
new file mode 100755
index 0000000..40479b8
--- /dev/null
+++ b/debian/agent-launch
@@ -0,0 +1,29 @@
+#!/bin/sh
+# helper script for launching ssh-agent, used by systemd unit and upstart job
+set -e
+
+if [ ! -d "$XDG_RUNTIME_DIR" ]; then
+    echo 'This needs $XDG_RUNTIME_DIR to be set' >&2
+    exit 1
+fi
+
+if [ "$1" = start ]; then
+    if [ -z "$SSH_AUTH_SOCK" ] && grep -s -q '^use-ssh-agent$' /etc/X11/Xsession.options; then
+        S="$XDG_RUNTIME_DIR/openssh_agent"
+        dbus-update-activation-environment --verbose --systemd SSH_AUTH_SOCK=$S SSH_AGENT_LAUNCHER=openssh
+        if type initctl >/dev/null 2>&1; then
+            initctl set-env --global SSH_AUTH_SOCK=$S
+        fi
+        exec ssh-agent -D -a $S
+    fi
+elif [ "$1" = stop ]; then
+    if [ "$SSH_AGENT_LAUNCHER" = openssh ]; then
+        dbus-update-activation-environment --systemd  SSH_AUTH_SOCK=
+        if type initctl >/dev/null 2>&1; then
+            initctl unset-env --global SSH_AUTH_SOCK
+        fi
+    fi
+else
+    echo "Unknown command $1" >&2
+    exit 1
+fi
diff --git a/debian/changelog b/debian/changelog
index e81c667..7185d15 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+openssh (1:7.2p2-8) UNRELEASED; urgency=medium
+
+  * Add debian/agent-launch: Helper script for conditionally starting the SSH
+    agent in the user session. Use it in ssh-agent.user-session.upstart.
+
+ -- Martin Pitt <martin.pitt@ubuntu.com>  Mon, 25 Jul 2016 17:01:56 +0200
+
 openssh (1:7.2p2-7) unstable; urgency=medium
 
   * Don't stop the ssh-session-cleanup service on upgrade (closes: #832155).
diff --git a/debian/openssh-client.install b/debian/openssh-client.install
index fd6819a..fd9c02d 100755
--- a/debian/openssh-client.install
+++ b/debian/openssh-client.install
@@ -26,6 +26,8 @@ usr/share/man/man8/ssh-pkcs11-helper.8
 contrib/ssh-copy-id usr/bin
 debian/ssh-argv0 usr/bin
 
+debian/agent-launch usr/lib/openssh
+
 # dh_apport would be neater, but at the time of writing it isn't in unstable
 # yet.
 debian/openssh-client.apport => usr/share/apport/package-hooks/openssh-client.py
diff --git a/debian/ssh-agent.user-session.upstart b/debian/ssh-agent.user-session.upstart
index 385a9ec..672d2a0 100644
--- a/debian/ssh-agent.user-session.upstart
+++ b/debian/ssh-agent.user-session.upstart
@@ -3,21 +3,5 @@ author "Stéphane Graber <stgraber@ubuntu.com>"
 
 start on starting xsession-init
 
-pre-start script
-    [ -e /etc/X11/Xsession.options ] || { stop; exit 0; }
-    grep -q "^use-ssh-agent$" /etc/X11/Xsession.options || { stop; exit 0; }
-    [ -z "$SSH_AUTH_SOCK" ] || { stop; exit 0; }
-
-    eval "$(ssh-agent -s)" >/dev/null
-    initctl set-env --global SSH_AUTH_SOCK=$SSH_AUTH_SOCK
-    initctl set-env --global SSH_AGENT_PID=$SSH_AGENT_PID
-    initctl set-env --global SSH_AGENT_LAUNCHER=upstart
-end script
-
-post-stop script
-    [ "$SSH_AGENT_LAUNCHER" = upstart ] || exit 0
-    kill $SSH_AGENT_PID 2>/dev/null || true
-    initctl unset-env --global SSH_AUTH_SOCK
-    initctl unset-env --global SSH_AGENT_PID
-    initctl unset-env --global SSH_AGENT_LAUNCHER
-end script
+exec /usr/lib/openssh/agent-launch start
+post-stop exec /usr/lib/openssh/agent-launch stop
-- 
2.8.1

From 0aa9e9e60c696f8cc6ed5df13ca52922bb752fa5 Mon Sep 17 00:00:00 2001
From: Martin Pitt <martin.pitt@ubuntu.com>
Date: Mon, 25 Jul 2016 17:07:25 +0200
Subject: [PATCH 2/2] Add systemd user unit for graphical sessions that use
 systemd

Override the corresponding upstart job in that case.
---
 debian/changelog                  | 2 ++
 debian/openssh-client.install     | 4 ++++
 debian/openssh-client.links       | 2 ++
 debian/systemd/ssh-agent.override | 1 +
 debian/systemd/ssh-agent.service  | 8 ++++++++
 5 files changed, 17 insertions(+)
 create mode 100644 debian/systemd/ssh-agent.override
 create mode 100644 debian/systemd/ssh-agent.service

diff --git a/debian/changelog b/debian/changelog
index 7185d15..dd6f7dc 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -2,6 +2,8 @@ openssh (1:7.2p2-8) UNRELEASED; urgency=medium
 
   * Add debian/agent-launch: Helper script for conditionally starting the SSH
     agent in the user session. Use it in ssh-agent.user-session.upstart.
+  * Add systemd user unit for graphical sessions that use systemd. Override
+    the corresponding upstart job in that case.
 
  -- Martin Pitt <martin.pitt@ubuntu.com>  Mon, 25 Jul 2016 17:01:56 +0200
 
diff --git a/debian/openssh-client.install b/debian/openssh-client.install
index fd9c02d..b8b798d 100755
--- a/debian/openssh-client.install
+++ b/debian/openssh-client.install
@@ -35,3 +35,7 @@ debian/openssh-client.apport => usr/share/apport/package-hooks/openssh-client.py
 # Upstart user job (only used under user sessions).
 debian/ssh-agent.user-session.upstart => usr/share/upstart/sessions/ssh-agent.conf
 
+# systemd user unit (only used under sessions)
+debian/systemd/ssh-agent.service usr/lib/systemd/user
+# disable above upstart job when running the systemd user unit
+debian/systemd/ssh-agent.override usr/share/upstart/systemd-session/upstart
diff --git a/debian/openssh-client.links b/debian/openssh-client.links
index 75d798a..1d94c74 100644
--- a/debian/openssh-client.links
+++ b/debian/openssh-client.links
@@ -1,2 +1,4 @@
 usr/bin/ssh usr/bin/slogin
 usr/share/man/man1/ssh.1 usr/share/man/man1/slogin.1
+# enable systemd user unit for graphical sessions that use systemd
+usr/lib/systemd/user/ssh-agent.service usr/lib/systemd/user/graphical-session-pre.target.wants/ssh-agent.service
diff --git a/debian/systemd/ssh-agent.override b/debian/systemd/ssh-agent.override
new file mode 100644
index 0000000..2905494
--- /dev/null
+++ b/debian/systemd/ssh-agent.override
@@ -0,0 +1 @@
+manual
diff --git a/debian/systemd/ssh-agent.service b/debian/systemd/ssh-agent.service
new file mode 100644
index 0000000..2297f8f
--- /dev/null
+++ b/debian/systemd/ssh-agent.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=OpenSSH Agent
+Before=graphical-session-pre.target
+ConditionPathExists=/etc/X11/Xsession.options
+
+[Service]
+ExecStart=/usr/lib/openssh/agent-launch start
+ExecStopPost=/usr/lib/openssh/agent-launch stop
-- 
2.8.1

Attachment: signature.asc
Description: PGP signature


Reply to: