From 0cb56ad4ac39b77f7d53ec91174583b5568e5155 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 9 Sep 2004 07:27:57 +0000 Subject: [PATCH] (check_for_running_agent): New. (main): The default action is now to check for an already running agent. (parse_rereadable_options): Set logfile only on reread. (main): Do not print the "is development version" note. --- NEWS | 6 ++++ THANKS | 4 +-- agent/ChangeLog | 8 +++++ agent/gpg-agent.c | 77 ++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/NEWS b/NEWS index 2fbf6475e..a93c7ce80 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,12 @@ Noteworthy changes in version 1.9.11 certificates are validated and only imported if they are fully valid. + * [gpg-agent] New option --max-cache-ttl. + + * [gpg-agent] When used without --daemon or --server, gpg-agent now + check whether a agent is already running and usable. + + Noteworthy changes in version 1.9.10 (2004-07-22) ------------------------------------------------- diff --git a/THANKS b/THANKS index 3f56e29d3..d523474c7 100644 --- a/THANKS +++ b/THANKS @@ -1,5 +1,5 @@ - -Richard Lefebvre rick at CERCA.UMontreal.CA +Alexander Belopolsky belopolsky at mac.com +Richard Lefebvre rick at cerca.umontreal.ca Andrew J. Schorr aschorr at telemetry-investments.com Michael Nottebrock michaelnottebrock at gmx.net diff --git a/agent/ChangeLog b/agent/ChangeLog index 220eb21e4..470c70162 100644 --- a/agent/ChangeLog +++ b/agent/ChangeLog @@ -1,3 +1,11 @@ +2004-09-09 Werner Koch + + * gpg-agent.c (check_for_running_agent): New. + (main): The default action is now to check for an already running + agent. + (parse_rereadable_options): Set logfile only on reread. + (main): Do not print the "is development version" note. + 2004-08-20 Werner Koch * gpg-agent.c: New option --max-cache-ttl. Suggested by Alexander diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c index 6aa3e6d67..4e3f0b507 100644 --- a/agent/gpg-agent.c +++ b/agent/gpg-agent.c @@ -178,6 +178,7 @@ static void handle_connections (int listen_fd); GCRY_THREAD_OPTION_PTH_IMPL; #endif /*USE_GNU_PTH*/ +static void check_for_running_agent (void); @@ -361,8 +362,9 @@ parse_rereadable_options (ARGPARSE_ARGS *pargs, int reread) case oDebugLevel: debug_level = pargs->r.ret_str; break; case oLogFile: - if (!current_logfile || !pargs->r.ret_str - || strcmp (current_logfile, pargs->r.ret_str)) + if (reread + && (!current_logfile || !pargs->r.ret_str + || strcmp (current_logfile, pargs->r.ret_str))) { log_set_file (pargs->r.ret_str); xfree (current_logfile); @@ -599,7 +601,9 @@ main (int argc, char **argv ) fprintf (stderr, "%s\n", strusage(15) ); } #ifdef IS_DEVELOPMENT_VERSION - log_info ("NOTE: this is a development version!\n"); + /* We don't want to print it here because gpg-agent is useful of its + own and quite matured. */ + /*log_info ("NOTE: this is a development version!\n");*/ #endif set_debug (); @@ -669,9 +673,15 @@ main (int argc, char **argv ) agent_exit (0); } + /* If this has been called without any options, we merely check + whether an agent is already running. We do this here so that we + don't clobber a logfile but print it directly to stderr. */ if (!pipe_server && !is_daemon) - log_info (_("please use the option `--daemon'" - " to run the program in the background\n")); + { + log_set_prefix (NULL, JNLIB_LOG_WITH_PREFIX); + check_for_running_agent (); + agent_exit (0); + } #ifdef ENABLE_NLS /* gpg-agent usually does not output any messages because it runs in @@ -705,9 +715,9 @@ main (int argc, char **argv ) start_command_handler (-1, -1); } else if (!is_daemon) - ; + ; /* NOTREACHED */ else - { /* regular server mode */ + { /* Regular server mode */ int fd; pid_t pid; int len; @@ -1220,3 +1230,56 @@ handle_connections (int listen_fd) log_info ("%s %s stopped\n", strusage(11), strusage(13)); } #endif /*USE_GNU_PTH*/ + + +/* Figure out whether an agent is available and running. Prints an + error if not. */ +static void +check_for_running_agent () +{ + int rc; + char *infostr, *p; + assuan_context_t ctx; + int prot, pid; + + infostr = getenv ("GPG_AGENT_INFO"); + if (!infostr || !*infostr) + { + log_error (_("no gpg-agent running in this session\n")); + return; + } + + infostr = xstrdup (infostr); + if ( !(p = strchr (infostr, ':')) || p == infostr) + { + log_error (_("malformed GPG_AGENT_INFO environment variable\n")); + xfree (infostr); + return; + } + + *p++ = 0; + pid = atoi (p); + while (*p && *p != ':') + p++; + prot = *p? atoi (p+1) : 0; + if (prot != 1) + { + log_error (_("gpg-agent protocol version %d is not supported\n"), + prot); + xfree (infostr); + return; + } + + rc = assuan_socket_connect (&ctx, infostr, pid); + xfree (infostr); + if (rc) + { + log_error ("can't connect to the agent: %s\n", assuan_strerror (rc)); + return; + } + + if (!opt.quiet) + log_info ("gpg-agent running and available\n"); + + assuan_disconnect (ctx); +}