Do not systematically ask for Scdaemon's socket

Try to find Scdaemon's socket by ourself, looking at some
known locations, before asking the agent for it.
develop
Damien Goutte-Gattat 6 years ago
parent 4eb505e544
commit eeef3a55b7
  1. 59
      src/gpg-util.c

@ -221,9 +221,8 @@ socket_name_cb(void *arg, const void *line, size_t len)
}
/*
* Find the socket of Scdaemon. Attempt to connect to a running
* GnuPG Agent and inquire about Scdaemon's running status and
* socket name.
* Find the socket of Scdaemon. Look for the socket in some
* standard locations, and if not found ask to the agent.
*
* @return 0 if Scdaemon's socket was found, or a gpg_error_t
* error code otherwise.
@ -231,19 +230,53 @@ socket_name_cb(void *arg, const void *line, size_t len)
static gpg_error_t
get_scd_socket_name(char *buffer, size_t len)
{
assuan_context_t ctx;
gpg_error_t e;
char *env_info = NULL;
gpg_error_t e = GPG_ERR_NO_SCDAEMON;
/*
* This function roughly follows the same logic as the
* get_agent_socket_name above:
* - first look for $GNUPGHOME/S.scdaemon;
* - then and if GNUPGHOME is not set, look in
* GnuPG's default home directory and under [/var]/run;
* - as a fallback, send a request to the GnuPG Agent.
*/
if ( (env_info = getenv("GNUPGHOME")) )
e = check_socket_fmt(buffer, len, "%s/S.scdaemon", env_info);
if ( e && ! env_info ) {
static const char *prefixes[] = { "/run", "/var/run", NULL };
struct passwd *pwd;
int i;
if ( ! (env_info = getenv("HOME")) && (pwd = getpwuid(getuid())) )
env_info = pwd->pw_dir;
if ( env_info )
e = check_socket_fmt(buffer, len, "%s/.gnupg/S.scdaemon", env_info);
for ( i = 0; e && prefixes[i]; i++ )
e = check_socket_fmt(buffer, len, "%s/user/%u/gnupg/S.scdaemon",
prefixes[i], getuid());
}
if ( e ) {
assuan_context_t ctx;
if ( ! (e = connect_to_agent(&ctx, 0)) ) {
if ( ! (e = connect_to_agent(&ctx, 0)) ) {
if ( ! (e = assuan_transact(ctx, "GETINFO scd_running",
NULL, NULL, NULL, NULL, NULL, NULL)) ) {
struct pstring p;
if ( ! (e = assuan_transact(ctx, "GETINFO scd_running",
NULL, NULL, NULL, NULL, NULL, NULL)) ) {
struct pstring p;
p.len = len;
p.buffer = buffer;
e = assuan_transact(ctx, "SCD GETINFO socket_name",
socket_name_cb, &p, NULL, NULL, NULL, NULL);
p.len = len;
p.buffer = buffer;
e = assuan_transact(ctx, "SCD GETINFO socket_name",
socket_name_cb, &p, NULL, NULL, NULL, NULL);
if ( ! e )
e = check_socket(buffer);
}
}
assuan_release(ctx);

Loading…
Cancel
Save