@@ -0,0 +1,180 @@
1+From 8fde13d07c0059040ed531ca3ac365f5780ff240 Mon Sep 17 00:00:00 2001
2+From: Shea Levy <shea@shealevy.com>
3+Date: Tue, 18 Nov 2014 14:34:53 -0500
4+Subject: [PATCH] gpg-agent: Enable socket activation
5+
6+This allows gpg-agent to be managed by tools such as systemd or launchd
7+---
8+ agent/gpg-agent.c | 62 ++++++++++++++++++++++++++++++++++++------------------
9+ doc/gpg-agent.texi | 21 +++++++++++++++++-
10+ 2 files changed, 61 insertions(+), 22 deletions(-)
11+
12+diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c
13+index 5960fe3..2f06982 100644
14+--- a/agent/gpg-agent.c
15++++ b/agent/gpg-agent.c
16+@@ -124,7 +124,9 @@ enum cmd_and_opt_values
17+ oPuttySupport,
18+ oDisableScdaemon,
19+ oDisableCheckOwnSocket,
20+- oWriteEnvFile
21++ oWriteEnvFile,
22++ oAgentFD,
23++ oSSHAgentFD
24+ };
25+
26+
27+@@ -138,6 +140,8 @@ static ARGPARSE_OPTS opts[] = {
28+ { 301, NULL, 0, N_("@Options:\n ") },
29+
30+ { oDaemon, "daemon", 0, N_("run in daemon mode (background)") },
31++ { oAgentFD, "agent-fd", 1, "@" },
32++ { oSSHAgentFD, "ssh-agent-fd", 1, "@" },
33+ { oServer, "server", 0, N_("run in server mode (foreground)") },
34+ { oVerbose, "verbose", 0, N_("verbose") },
35+ { oQuiet, "quiet", 0, N_("be somewhat more quiet") },
36+@@ -596,6 +600,31 @@ parse_rereadable_options (ARGPARSE_ARGS *pargs, int reread)
37+ return 1; /* handled */
38+ }
39+
40++/* Handle agent socket(s) */
41++static void
42++handle_agent_socks(int fd, int fd_ssh)
43++{
44++#ifndef HAVE_W32_SYSTEM
45++ if (chdir("/"))
46++ {
47++ log_error ("chdir to / failed: %s\n", strerror (errno));
48++ exit (1);
49++ }
50++
51++ {
52++ struct sigaction sa;
53++
54++ sa.sa_handler = SIG_IGN;
55++ sigemptyset (&sa.sa_mask);
56++ sa.sa_flags = 0;
57++ sigaction (SIGPIPE, &sa, NULL);
58++ }
59++#endif /*!HAVE_W32_SYSTEM*/
60++
61++ log_info ("%s %s started\n", strusage(11), strusage(13) );
62++ handle_connections (fd, fd_ssh);
63++ assuan_sock_close (fd);
64++}
65+
66+ /* The main entry point. */
67+ int
68+@@ -612,6 +641,8 @@ main (int argc, char **argv )
69+ int default_config =1;
70+ int pipe_server = 0;
71+ int is_daemon = 0;
72++ int fd_agent = GNUPG_INVALID_FD;
73++ int fd_ssh_agent = GNUPG_INVALID_FD;
74+ int nodetach = 0;
75+ int csh_style = 0;
76+ char *logfile = NULL;
77+@@ -819,6 +850,8 @@ main (int argc, char **argv )
78+ case oSh: csh_style = 0; break;
79+ case oServer: pipe_server = 1; break;
80+ case oDaemon: is_daemon = 1; break;
81++ case oAgentFD: fd_agent = pargs.r.ret_int; break;
82++ case oSSHAgentFD: fd_ssh_agent = pargs.r.ret_int; break;
83+
84+ case oDisplay: default_display = xstrdup (pargs.r.ret_str); break;
85+ case oTTYname: default_ttyname = xstrdup (pargs.r.ret_str); break;
86+@@ -904,7 +937,8 @@ main (int argc, char **argv )
87+ bind_textdomain_codeset (PACKAGE_GT, "UTF-8");
88+ #endif
89+
90+- if (!pipe_server && !is_daemon && !gpgconf_list)
91++ if (!pipe_server && !is_daemon && !gpgconf_list &&
92++ fd_agent == GNUPG_INVALID_FD)
93+ {
94+ /* We have been called without any options and thus we merely
95+ check whether an agent is already running. We do this right
96+@@ -1054,6 +1088,10 @@ main (int argc, char **argv )
97+ agent_deinit_default_ctrl (ctrl);
98+ xfree (ctrl);
99+ }
100++ else if (fd_agent != GNUPG_INVALID_FD)
101++ {
102++ handle_agent_socks(fd_agent, fd_ssh_agent);
103++ }
104+ else if (!is_daemon)
105+ ; /* NOTREACHED */
106+ else
107+@@ -1238,26 +1276,8 @@ main (int argc, char **argv )
108+ log_set_prefix (NULL, oldflags | JNLIB_LOG_RUN_DETACHED);
109+ opt.running_detached = 1;
110+ }
111+-
112+- if (chdir("/"))
113+- {
114+- log_error ("chdir to / failed: %s\n", strerror (errno));
115+- exit (1);
116+- }
117+-
118+- {
119+- struct sigaction sa;
120+-
121+- sa.sa_handler = SIG_IGN;
122+- sigemptyset (&sa.sa_mask);
123+- sa.sa_flags = 0;
124+- sigaction (SIGPIPE, &sa, NULL);
125+- }
126+ #endif /*!HAVE_W32_SYSTEM*/
127+-
128+- log_info ("%s %s started\n", strusage(11), strusage(13) );
129+- handle_connections (fd, opt.ssh_support ? fd_ssh : GNUPG_INVALID_FD);
130+- assuan_sock_close (fd);
131++ handle_agent_socks(fd, opt.ssh_support ? fd_ssh : GNUPG_INVALID_FD);
132+ }
133+
134+ return 0;
135+diff --git a/doc/gpg-agent.texi b/doc/gpg-agent.texi
136+index a4079d7..1556e54 100644
137+--- a/doc/gpg-agent.texi
138++++ b/doc/gpg-agent.texi
139+@@ -43,7 +43,15 @@
140+ .IR file ]
141+ .RI [ options ]
142+ .B \-\-daemon
143+-.RI [ command_line ]
144++.br
145++.B gpg-agent
146++.RB [ \-\-homedir
147++.IR dir ]
148++.RB [ \-\-options
149++.IR file ]
150++.RI [ options ]
151++.B \-\-agent-fd
152++.IR fd
153+ @end ifset
154+
155+ @mansect description
156+@@ -186,6 +194,11 @@ Yet another way is creating
157+ a new process as a child of gpg-agent: @code{gpg-agent --daemon
158+ /bin/sh}. This way you get a new shell with the environment setup
159+ properly; if you exit from this shell, gpg-agent terminates as well.
160++
161++@item --agent-fd @var{fd}
162++@opindex agent-fd
163++Start the gpg-agent using @var{fd} as the listening socket. This is useful for
164++socket activation a la systemd and launchd.
165+ @end table
166+
167+ @mansect options
168+@@ -532,6 +545,12 @@ Ignore requests to change the current @code{tty} or X window system's
169+ @code{DISPLAY} variable respectively. This is useful to lock the
170+ pinentry to pop up at the @code{tty} or display you started the agent.
171+
172++@item --ssh-agent-fd @var{fd}
173++@opindex ssh-agent-fd
174++
175++When starting the agent with @option{--agent-fd}, use this to pass in a socket
176++to be used for the OpenSSH agent protocol.
177++
178+ @anchor{option --enable-ssh-support}
179+ @item --enable-ssh-support
180+ @opindex enable-ssh-support