Start the GPG-Agent if needed

When looking for the GPG-Agent's socket, call gpg-connect-agent
as a fallback method. This will have the side-effect of launching
the agent if it was not already running (if the agent is configured
to use a standard socket, which is always the case with GnuPG 2.1).
develop
Damien Goutte-Gattat 8 years ago
parent a3586c6af2
commit 7bf523f361
  1. 87
      src/gpg-util.c

@ -27,52 +27,31 @@
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <gcrypt.h>
#include "gpg-util.h"
/*
* Wrap call to asprintf to make sure that when a memory allocation
* error occurs, errno is set to ENOMEM and the string pointer is
* set to NULL.
*/
static int
do_asprintf(char **strp, const char *fmt, ...)
{
int n;
va_list ap;
va_start(ap, fmt);
if ( (n = vasprintf(strp, fmt, ap)) == -1 ) {
errno = ENOMEM;
*strp = NULL;
}
va_end(ap);
return n;
}
/*
* Find the socket of GnuPG Agent. First look for the GPG_AGENT_INFO
* environment variable, and if unset look for the standard socket
* in GnuPG's home directory.
* environment variable, if unset look for the standard socket in
* GnuPG's home directory, if not found use gpg-connect-agent.
*
* The socket name is stored in a newly allocated buffer that should
* be freed by the caller.
*
* May return NULL upon memory allocation error, or if the user's
* home directory cannot be found.
* Return NULL if the socket could not be found.
*/
static char *
get_agent_socket_name(void)
{
char *env_info, *socket_name;
struct passwd *pwd;
char *env_info, *socket_name = NULL;
/* First look for GPG_AGENT_INFO in the environment. */
if ( (env_info = getenv("GPG_AGENT_INFO")) ) {
if ( (socket_name = strdup(env_info)) ) {
char *colon_index;
@ -80,23 +59,49 @@ get_agent_socket_name(void)
/*
* GPG_AGENT_INFO should be of the form
* "path_to_socket:x:y", where x is the PID
* of the GnuPG Agent and y is the protocol
* version (currently 1).
* of the GnuPG Agent process and y is the
* protocol version (currently 1).
*/
if ( (colon_index = strchr(socket_name, ':')) )
*colon_index = '\0';
}
}
else if ( (env_info = getenv("GNUPGHOME")) )
do_asprintf(&socket_name, "%s/S.gpg-agent", env_info);
else if ( (env_info = getenv("HOME")) )
do_asprintf(&socket_name, "%s/.gnupg/S.gpg-agent", env_info);
else if ( (pwd = getpwuid(getuid())) )
do_asprintf(&socket_name, "%s/.gnupg/S.gpg-agent", pwd->pw_dir);
else
/* No way to get the user's home directory?
* Can that ever happen? */
socket_name = NULL;
else {
/* Then look for the standard socket. */
char buf[255] = "";
struct passwd *pwd;
struct stat st;
if ( (env_info = getenv("GNUPGHOME")) )
snprintf(buf, sizeof(buf), "%s/S.gpg-agent", env_info);
else if ( (env_info = getenv("HOME")) )
snprintf(buf, sizeof(buf), "%s/.gnupg/S.gpg-agent", env_info);
else if ( (pwd = getpwuid(getuid())) )
snprintf(buf, sizeof(buf), "%s/.gnupg/S.gpg-agent", pwd->pw_dir);
/* Check whether the socket does exist. */
if ( *buf && stat(buf, &st) != -1 && S_ISSOCK(st.st_mode) )
socket_name = strdup(buf);
else {
/*
* Finally, try to ask the agent directly. This method could
* replace the two above, but since it requires spawning a
* new process, we use it only as a fallback. We expect that
* most of the time, the agent should already be running when
* our programs are called, and thus the socket would already
* exist and be found by the above code.
*/
FILE *f;
f = popen("gpg-connect-agent 'GETINFO socket_name' /bye", "r");
if ( f ) {
if ( fscanf(f, "D %254s\nOK\n", buf) == 1 )
socket_name = strdup(buf);
pclose(f);
}
}
}
return socket_name;
}

Loading…
Cancel
Save