[git] GnuPG - branch, master, updated. gnupg-2.1.14-72-g72fa314

by Justus Winter cvs at cvs.gnupg.org
Thu Aug 11 13:28:25 CEST 2016


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "The GNU Privacy Guard".

The branch, master has been updated
       via  72fa314b71e4ce8780f59b16d32cabf5d4bd5451 (commit)
       via  14479e2515439c73e385f37e8c2b3fc517b038b9 (commit)
       via  9e6503b7ce019aa417099ded1dda87b68c33f912 (commit)
      from  ed5c1b0b8a4790c4fb36a3129387f7c2ef5db302 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 72fa314b71e4ce8780f59b16d32cabf5d4bd5451
Author: Justus Winter <justus at g10code.com>
Date:   Thu Aug 11 13:03:16 2016 +0200

    common: Remove compatibility code.
    
    * common/Makefile.am: Drop deleted files.
    * common/w32-afunix.c: Delete file.
    * common/w32-afunix.h: Likewise.
    
    GnuPG-bug-id: 2408
    Signed-off-by: Justus Winter <justus at g10code.com>

diff --git a/common/Makefile.am b/common/Makefile.am
index 2a24c57..422fcf6 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -95,7 +95,7 @@ common_sources = \
 	recsel.c recsel.h
 
 if HAVE_W32_SYSTEM
-common_sources += w32-reg.c w32-afunix.c w32-afunix.h
+common_sources += w32-reg.c
 endif
 
 # To make the code easier to read we have split home some code into
diff --git a/common/w32-afunix.c b/common/w32-afunix.c
deleted file mode 100644
index 4432219..0000000
--- a/common/w32-afunix.c
+++ /dev/null
@@ -1,148 +0,0 @@
-/* w32-afunix.c - AF_UNIX emulation for Windows (Client only).
- * Copyright (C) 2004, 2006 g10 Code GmbH
- *
- * This file is part of GnuPG.
- *
- * GnuPG is free software; you can redistribute it and/or modify it
- * under the terms of either
- *
- *   - the GNU Lesser General Public License as published by the Free
- *     Software Foundation; either version 3 of the License, or (at
- *     your option) any later version.
- *
- * or
- *
- *   - the GNU General Public License as published by the Free
- *     Software Foundation; either version 2 of the License, or (at
- *     your option) any later version.
- *
- * or both in parallel, as here.
- *
- * GnuPG is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copies of the GNU General Public License
- * and the GNU Lesser General Public License along with this program;
- * if not, see <http://www.gnu.org/licenses/>.
- */
-
-/* Use of this code is deprecated - you better use the socket wrappers
-   from libassuan. */
-
-#ifdef _WIN32
-#include <stdio.h>
-#include <stdlib.h>
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <io.h>
-#include <errno.h>
-
-#include "w32-afunix.h"
-
-
-
-/* The buffer for NONCE needs to be at least 16 bytes.  Returns 0 on
-   success. */
-static int
-read_port_and_nonce (const char *fname, unsigned short *port, char *nonce)
-{
-  FILE *fp;
-  char buffer[50], *p;
-  size_t nread;
-  int aval;
-
-  fp = fopen (fname, "rb");
-  if (!fp)
-    return -1;
-  nread = fread (buffer, 1, sizeof buffer - 1, fp);
-  fclose (fp);
-  if (!nread)
-    {
-      gpg_err_set_errno (EIO);
-      return -1;
-    }
-  buffer[nread] = 0;
-  aval = atoi (buffer);
-  if (aval < 1 || aval > 65535)
-    {
-      gpg_err_set_errno (EINVAL);
-      return -1;
-    }
-  *port = (unsigned int)aval;
-  for (p=buffer; nread && *p != '\n'; p++, nread--)
-    ;
-  if (*p != '\n' || nread != 17)
-    {
-      gpg_err_set_errno (EINVAL);
-      return -1;
-    }
-  p++; nread--;
-  memcpy (nonce, p, 16);
-  return 0;
-}
-
-
-
-int
-_w32_close (int fd)
-{
-  int rc = closesocket (fd);
-  if (rc && WSAGetLastError () == WSAENOTSOCK)
-      rc = close (fd);
-  return rc;
-}
-
-
-int
-_w32_sock_new (int domain, int type, int proto)
-{
-  if (domain == AF_UNIX || domain == AF_LOCAL)
-    domain = AF_INET;
-  return socket (domain, type, proto);
-}
-
-
-int
-_w32_sock_connect (int sockfd, struct sockaddr *addr, int addrlen)
-{
-  struct sockaddr_in myaddr;
-  struct sockaddr_un *unaddr;
-  unsigned short port;
-  char nonce[16];
-  int ret;
-
-  (void)addrlen;
-
-  unaddr = (struct sockaddr_un *)addr;
-  if (read_port_and_nonce (unaddr->sun_path, &port, nonce))
-    return -1;
-
-  myaddr.sin_family = AF_INET;
-  myaddr.sin_port = htons (port);
-  myaddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
-
-  /* Set return values.  */
-  unaddr->sun_family = myaddr.sin_family;
-  unaddr->sun_port = myaddr.sin_port;
-  unaddr->sun_addr.s_addr = myaddr.sin_addr.s_addr;
-
-  ret = connect (sockfd, (struct sockaddr *)&myaddr, sizeof myaddr);
-  if (!ret)
-    {
-      /* Send the nonce. */
-      ret = send (sockfd, nonce, 16, 0);
-      if (ret >= 0 && ret != 16)
-        {
-          gpg_err_set_errno (EIO);
-          ret = -1;
-        }
-    }
-  return ret;
-}
-
-
-#endif /*_WIN32*/
diff --git a/common/w32-afunix.h b/common/w32-afunix.h
deleted file mode 100644
index 7025a49..0000000
--- a/common/w32-afunix.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/* w32-afunix.h - AF_UNIX emulation for Windows
- *	Copyright (C) 2004, 2006 g10 Code GmbH
- *
- * This file is part of GnuPG.
- *
- * GnuPG is free software; you can redistribute it and/or modify it
- * under the terms of either
- *
- *   - the GNU Lesser General Public License as published by the Free
- *     Software Foundation; either version 3 of the License, or (at
- *     your option) any later version.
- *
- * or
- *
- *   - the GNU General Public License as published by the Free
- *     Software Foundation; either version 2 of the License, or (at
- *     your option) any later version.
- *
- * or both in parallel, as here.
- *
- * GnuPG is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copies of the GNU General Public License
- * and the GNU Lesser General Public License along with this program;
- * if not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifdef _WIN32
-#ifndef W32AFUNIX_DEFS_H
-#define W32AFUNIX_DEFS_H
-
-#include <sys/types.h>
-#include <windows.h>
-#include <ws2tcpip.h>
-#include <unistd.h>
-
-/* We can easiliy replace this code by the socket wrappers from libassuan.  */
-#warning Remove this code; it is only used on w32 by symcryptrun.
-
-#define DIRSEP_C '\\'
-
-#define AF_LOCAL AF_UNIX
-/* We need to prefix the structure with a sockaddr_in header so we can
-   use it later for sendto and recvfrom. */
-struct sockaddr_un
-{
-  short          sun_family;
-  unsigned short sun_port;
-  struct         in_addr sun_addr;
-  char           sun_path[108-2-4]; /* Path name.  */
-};
-
-
-int _w32_close (int fd);
-int _w32_sock_new (int domain, int type, int proto);
-int _w32_sock_connect (int sockfd, struct sockaddr *addr, int addrlen);
-
-
-#endif /*W32AFUNIX_DEFS_H*/
-#endif /*_WIN32*/

commit 14479e2515439c73e385f37e8c2b3fc517b038b9
Author: Justus Winter <justus at g10code.com>
Date:   Thu Aug 11 12:26:09 2016 +0200

    common: Rework the simple password query module.
    
    * common/simple-pwquery.c (writen, readline): Drop.
    (agent_send_option, agent_send_all_options, agent_open): Just use
    libassuan.
    (simple_pw_set_socket): Simplify.
    (default_inq_cb): New function.
    (simple_pwquery, simple_query): Just use libassuan.
    * agent/Makefile.am (gpg_preset_passphrase_LDADD): Add libassuan.
    * tools/Makefile.am (symcryptrun_LDADD): Likewise.
    
    Signed-off-by: Justus Winter <justus at g10code.com>

diff --git a/agent/Makefile.am b/agent/Makefile.am
index 4be9090..1970088 100644
--- a/agent/Makefile.am
+++ b/agent/Makefile.am
@@ -85,7 +85,7 @@ gpg_preset_passphrase_SOURCES = \
 
 # Needs $(NETLIBS) for libsimple-pwquery.la.
 gpg_preset_passphrase_LDADD = \
-         $(pwquery_libs) $(common_libs) \
+         $(pwquery_libs) $(common_libs) $(LIBASSUAN_LIBS) \
 	 $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) $(NETLIBS) $(LIBICONV)
 
 
diff --git a/common/simple-pwquery.c b/common/simple-pwquery.c
index bd40fdf..240451b 100644
--- a/common/simple-pwquery.c
+++ b/common/simple-pwquery.c
@@ -17,10 +17,10 @@
  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
-/* This module is intended as a standalone client implementation to
-   gpg-agent's GET_PASSPHRASE command.  In particular it does not use
-   the Assuan library and can only cope with an already running
-   gpg-agent.  Some stuff is configurable in the header file. */
+/* This module is intended as a simple client implementation to
+   gpg-agent's GET_PASSPHRASE command.  It can only cope with an
+   already running gpg-agent.  Some stuff is configurable in the
+   header file. */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -30,6 +30,7 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#include <assuan.h>
 #ifdef HAVE_W32_SYSTEM
 #include <winsock2.h>
 #else
@@ -42,9 +43,8 @@
 
 #define GNUPG_COMMON_NEED_AFLOCAL
 #include "../common/mischelp.h"
-#ifdef HAVE_W32_SYSTEM
-#include "../common/w32-afunix.h"
-#endif
+#include "sysutils.h"
+#include "membuf.h"
 
 
 #define SIMPLE_PWQUERY_IMPLEMENTATION 1
@@ -96,88 +96,11 @@ my_stpcpy(char *a,const char *b)
 #endif
 
 
-
-/* Write NBYTES of BUF to file descriptor FD. */
-static int
-writen (int fd, const void *buf, size_t nbytes)
-{
-  size_t nleft = nbytes;
-  int nwritten;
-
-  while (nleft > 0)
-    {
-#ifdef HAVE_W32_SYSTEM
-      nwritten = send (fd, buf, nleft, 0);
-#else
-      nwritten = write (fd, buf, nleft);
-#endif
-      if (nwritten < 0)
-        {
-          if (errno == EINTR)
-            nwritten = 0;
-          else {
-#ifdef SPWQ_USE_LOGGING
-            log_error ("write failed: %s\n", strerror (errno));
-#endif
-            return SPWQ_IO_ERROR;
-          }
-        }
-      nleft -= nwritten;
-      buf = (const char*)buf + nwritten;
-    }
-
-  return 0;
-}
-
-
-/* Read an entire line and return number of bytes read. */
-static int
-readline (int fd, char *buf, size_t buflen)
-{
-  size_t nleft = buflen;
-  char *p;
-  int nread = 0;
-
-  while (nleft > 0)
-    {
-#ifdef HAVE_W32_SYSTEM
-      int n = recv (fd, buf, nleft, 0);
-#else
-      int n = read (fd, buf, nleft);
-#endif
-      if (n < 0)
-        {
-          if (errno == EINTR)
-            continue;
-          return -(SPWQ_IO_ERROR);
-        }
-      else if (!n)
-        {
-          return -(SPWQ_PROTOCOL_ERROR); /* incomplete line */
-        }
-      p = buf;
-      nleft -= n;
-      buf += n;
-      nread += n;
-
-      for (; n && *p != '\n'; n--, p++)
-        ;
-      if (n)
-        {
-          break; /* At least one full line available - that's enough.
-                    This function is just a simple implementation, so
-                    it is okay to forget about pending bytes.  */
-        }
-    }
-
-  return nread;
-}
-
-
 /* Send an option to the agent */
 static int
-agent_send_option (int fd, const char *name, const char *value)
+agent_send_option (assuan_context_t ctx, const char *name, const char *value)
 {
+  int err;
   char buf[200];
   int nread;
   char *line;
@@ -188,28 +111,17 @@ agent_send_option (int fd, const char *name, const char *value)
     return SPWQ_OUT_OF_CORE;
   strcpy (stpcpy (stpcpy (stpcpy (
                      stpcpy (line, "OPTION "), name), "="), value), "\n");
-  i = writen (fd, line, strlen (line));
-  spwq_free (line);
-  if (i)
-    return i;
-
-  /* get response */
-  nread = readline (fd, buf, DIM(buf)-1);
-  if (nread < 0)
-    return -nread;
-  if (nread < 3)
-    return SPWQ_PROTOCOL_ERROR;
 
-  if (buf[0] == 'O' && buf[1] == 'K' && (buf[2] == ' ' || buf[2] == '\n'))
-    return 0; /* okay */
+  err = assuan_transact (ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
 
-  return SPWQ_ERR_RESPONSE;
+  spwq_free (line);
+  return err;
 }
 
 
 /* Send all available options to the agent. */
 static int
-agent_send_all_options (int fd)
+agent_send_all_options (assuan_context_t ctx)
 {
   char *dft_display = NULL;
   char *dft_ttyname = NULL;
@@ -221,7 +133,7 @@ agent_send_all_options (int fd)
   dft_display = getenv ("DISPLAY");
   if (dft_display)
     {
-      if ((rc = agent_send_option (fd, "display", dft_display)))
+      if ((rc = agent_send_option (ctx, "display", dft_display)))
         return rc;
     }
 
@@ -232,14 +144,14 @@ agent_send_all_options (int fd)
 #endif
   if (dft_ttyname && *dft_ttyname)
     {
-      if ((rc=agent_send_option (fd, "ttyname", dft_ttyname)))
+      if ((rc=agent_send_option (ctx, "ttyname", dft_ttyname)))
         return rc;
     }
 
   dft_ttytype = getenv ("TERM");
   if (dft_ttyname && dft_ttytype)
     {
-      if ((rc = agent_send_option (fd, "ttytype", dft_ttytype)))
+      if ((rc = agent_send_option (ctx, "ttytype", dft_ttytype)))
         return rc;
     }
 
@@ -260,7 +172,7 @@ agent_send_all_options (int fd)
       }
     dft_lc = setlocale (LC_CTYPE, "");
     if (dft_ttyname && dft_lc)
-      rc = agent_send_option (fd, "lc-ctype", dft_lc);
+      rc = agent_send_option (ctx, "lc-ctype", dft_lc);
     if (old_lc)
       {
         setlocale (LC_CTYPE, old_lc);
@@ -282,7 +194,7 @@ agent_send_all_options (int fd)
       }
     dft_lc = setlocale (LC_MESSAGES, "");
     if (dft_ttyname && dft_lc)
-      rc = agent_send_option (fd, "lc-messages", dft_lc);
+      rc = agent_send_option (ctx, "lc-messages", dft_lc);
     if (old_lc)
       {
         setlocale (LC_MESSAGES, old_lc);
@@ -300,7 +212,7 @@ agent_send_all_options (int fd)
     {
       /* We ignore errors here because older gpg-agents don't support
          this option.  */
-      agent_send_option (fd, "xauthority", dft_xauthority);
+      agent_send_option (ctx, "xauthority", dft_xauthority);
     }
 
   /* Send the PINENTRY_USER_DATA variable.  */
@@ -309,9 +221,14 @@ agent_send_all_options (int fd)
     {
       /* We ignore errors here because older gpg-agents don't support
          this option.  */
-      agent_send_option (fd, "pinentry-user-data", dft_pinentry_user_data);
+      agent_send_option (ctx, "pinentry-user-data", dft_pinentry_user_data);
     }
 
+  /* Tell the agent that we support Pinentry notifications.  No
+     error checking so that it will work with older agents.  */
+  assuan_transact (ctx, "OPTION allow-pinentry-notify",
+                   NULL, NULL, NULL, NULL, NULL, NULL);
+
   return 0;
 }
 
@@ -321,7 +238,7 @@ agent_send_all_options (int fd)
    the file descriptor for the connection.  Return -1 in case of
    error. */
 static int
-agent_open (int *rfd)
+agent_open (assuan_context_t *ctx)
 {
   int rc;
   int fd;
@@ -331,7 +248,6 @@ agent_open (int *rfd)
   char line[200];
   int nread;
 
-  *rfd = -1;
   infostr = default_gpg_agent_info;
   if ( !infostr || !*infostr )
     {
@@ -340,81 +256,35 @@ agent_open (int *rfd)
 #endif
       return SPWQ_NO_AGENT;
     }
-  p = spwq_malloc (strlen (infostr)+1);
-  if (!p)
-    return SPWQ_OUT_OF_CORE;
-  strcpy (p, infostr);
-  infostr = p;
-
-  if ( !(p = strchr ( infostr, PATHSEP_C)) || p == infostr
-       || (p-infostr)+1 >= sizeof client_addr.sun_path )
-    {
-      spwq_free (infostr);
-      return SPWQ_NO_AGENT;
-    }
-  *p++ = 0;
-
-  while (*p && *p != PATHSEP_C)
-    p++;
 
-#ifdef HAVE_W32_SYSTEM
-  fd = _w32_sock_new (AF_UNIX, SOCK_STREAM, 0);
-#else
-  fd = socket (AF_UNIX, SOCK_STREAM, 0);
-#endif
-  if (fd == -1)
-    {
-#ifdef SPWQ_USE_LOGGING
-      log_error ("can't create socket: %s\n", strerror(errno) );
-#endif
-      spwq_free (infostr);
-      return SPWQ_SYS_ERROR;
-    }
-
-  memset (&client_addr, 0, sizeof client_addr);
-  client_addr.sun_family = AF_UNIX;
-  strcpy (client_addr.sun_path, infostr);
-  spwq_free (infostr);
-  len = SUN_LEN (&client_addr);
+  rc = assuan_new (ctx);
+  if (rc)
+    return rc;
 
-#ifdef HAVE_W32_SYSTEM
-  rc = _w32_sock_connect (fd, (struct sockaddr*)&client_addr, len );
-#else
-  rc = connect (fd, (struct sockaddr*)&client_addr, len );
-#endif
-  if (rc == -1)
+  rc = assuan_socket_connect (*ctx, infostr, 0, 0);
+  if (rc)
     {
 #ifdef SPWQ_USE_LOGGING
       log_error (_("can't connect to '%s': %s\n"),
-                 client_addr.sun_path, strerror (errno));
+                 infostr, gpg_strerror (rc));
 #endif
-      close (fd );
-      return SPWQ_IO_ERROR;
+      goto errout;
     }
 
-  nread = readline (fd, line, DIM(line));
-  if (nread < 3 || !(line[0] == 'O' && line[1] == 'K'
-                     && (line[2] == '\n' || line[2] == ' ')) )
-    {
-#ifdef SPWQ_USE_LOGGING
-      log_error ( _("communication problem with gpg-agent\n"));
-#endif
-      close (fd );
-      return SPWQ_PROTOCOL_ERROR;
-    }
-
-  rc = agent_send_all_options (fd);
+  rc = agent_send_all_options (*ctx);
   if (rc)
     {
 #ifdef SPWQ_USE_LOGGING
       log_error (_("problem setting the gpg-agent options\n"));
 #endif
-      close (fd);
-      return rc;
+      goto errout;
     }
 
-  *rfd = fd;
   return 0;
+
+ errout:
+  assuan_release (*ctx);
+  *ctx = NULL;
 }
 
 
@@ -451,17 +321,37 @@ int
 simple_pw_set_socket (const char *name)
 {
   spwq_free (default_gpg_agent_info);
+  default_gpg_agent_info = NULL;
   if (name)
     {
-      default_gpg_agent_info = spwq_malloc (strlen (name) + 4 + 1);
+      default_gpg_agent_info = spwq_malloc (strlen (name) + 1);
       if (!default_gpg_agent_info)
         return SPWQ_OUT_OF_CORE;
-      /* We don't know the PID thus we use 0.  */
-      strcpy (stpcpy (default_gpg_agent_info, name),
-              PATHSEP_S "0" PATHSEP_S "1");
+      strcpy (default_gpg_agent_info, name);
+    }
+
+  return 0;
+}
+
+
+/* This is the default inquiry callback.  It merely handles the
+   Pinentry notification.  */
+static gpg_error_t
+default_inq_cb (void *opaque, const char *line)
+{
+  (void)opaque;
+
+  if (!strncmp (line, "PINENTRY_LAUNCHED", 17) && (line[17]==' '||!line[17]))
+    {
+      gnupg_allow_set_foregound_window ((pid_t)strtoul (line+17, NULL, 10));
+      /* We do not return errors to avoid breaking other code.  */
     }
   else
-    default_gpg_agent_info = NULL;
+    {
+#ifdef SPWQ_USE_LOGGING
+      log_debug ("ignoring gpg-agent inquiry '%s'\n", line);
+#endif
+    }
 
   return 0;
 }
@@ -483,14 +373,15 @@ simple_pwquery (const char *cacheid,
                 int opt_check,
                 int *errorcode)
 {
-  int fd = -1;
+  assuan_context_t ctx;
+  membuf_t data;
   int nread;
   char *result = NULL;
   char *pw = NULL;
   char *p;
   int rc, i;
 
-  rc = agent_open (&fd);
+  rc = agent_open (&ctx);
   if (rc)
     goto leave;
 
@@ -530,73 +421,43 @@ simple_pwquery (const char *cacheid,
     *p++ = ' ';
     p = copy_and_escape (p, description);
     *p++ = '\n';
-    rc = writen (fd, line, p - line);
+
+    init_membuf_secure (&data, 64);
+
+    rc = assuan_transact (ctx, line, put_membuf_cb, &data,
+                          default_inq_cb, NULL, NULL, NULL);
     spwq_free (line);
-    if (rc)
-      goto leave;
-  }
 
-  /* get response */
-  pw = spwq_secure_malloc (500);
-  nread = readline (fd, pw, 499);
-  if (nread < 0)
-    {
-      rc = -nread;
-      goto leave;
-    }
-  if (nread < 3)
-    {
-      rc = SPWQ_PROTOCOL_ERROR;
-      goto leave;
-    }
+    /* Older Pinentries return the old assuan error code for canceled
+       which gets translated by libassuan to GPG_ERR_ASS_CANCELED and
+       not to the code for a user cancel.  Fix this here. */
+    if (rc && gpg_err_source (rc)
+        && gpg_err_code (rc) == GPG_ERR_ASS_CANCELED)
+      rc = gpg_err_make (gpg_err_source (rc), GPG_ERR_CANCELED);
 
-  if (pw[0] == 'O' && pw[1] == 'K' && pw[2] == ' ')
-    { /* we got a passphrase - convert it back from hex */
-      size_t pwlen = 0;
+    if (rc)
+      {
+        void *p;
+        size_t n;
 
-      for (i=3; i < nread && hexdigitp (pw+i); i+=2)
-        pw[pwlen++] = xtoi_2 (pw+i);
-      pw[pwlen] = 0; /* make a C String */
-      result = pw;
-      pw = NULL;
-    }
-  else if ((nread > 7 && !memcmp (pw, "ERR 111", 7)
-            && (pw[7] == ' ' || pw[7] == '\n') )
-           || ((nread > 4 && !memcmp (pw, "ERR ", 4)
-                && (strtoul (pw+4, NULL, 0) & 0xffff) == 99)) )
-    {
-      /* 111 is the old Assuan code for canceled which might still
-         be in use by old installations. 99 is GPG_ERR_CANCELED as
-         used by modern gpg-agents; 0xffff is used to mask out the
-         error source.  */
-#ifdef SPWQ_USE_LOGGING
-      log_info (_("canceled by user\n") );
-#endif
-      *errorcode = 0; /* Special error code to indicate Cancel. */
-    }
-  else if (nread > 4 && !memcmp (pw, "ERR ", 4))
-    {
-      switch ( (strtoul (pw+4, NULL, 0) & 0xffff) )
-        {
-        case 85: rc = SPWQ_NO_PIN_ENTRY;  break;
-        default: rc = SPWQ_GENERAL_ERROR; break;
-        }
-    }
-  else
-    {
-#ifdef SPWQ_USE_LOGGING
-      log_error (_("problem with the agent\n"));
-#endif
-      rc = SPWQ_ERR_RESPONSE;
-    }
+        p = get_membuf (&data, &n);
+        if (p)
+          wipememory (p, n);
+        spwq_free (p);
+      }
+    else
+      {
+        put_membuf (&data, "", 1);
+        result = get_membuf (&data, NULL);
+        if (pw == NULL)
+          rc = gpg_error_from_syserror ();
+      }
+  }
 
  leave:
   if (errorcode)
     *errorcode = rc;
-  if (fd != -1)
-    close (fd);
-  if (pw)
-    spwq_secure_free (pw);
+  assuan_release (ctx);
   return result;
 }
 
@@ -628,96 +489,17 @@ simple_pwclear (const char *cacheid)
 int
 simple_query (const char *query)
 {
-  int fd = -1;
-  int nread;
+  assuan_context_t ctx;
   char response[500];
   int have = 0;
   int rc;
 
-  rc = agent_open (&fd);
-  if (rc)
-    goto leave;
-
-  rc = writen (fd, query, strlen (query));
+  rc = agent_open (&ctx);
   if (rc)
-    goto leave;
-
-  while (1)
-    {
-      if (! have || ! strchr (response, '\n'))
-        /* get response */
-        {
-          nread = readline (fd, &response[have],
-                            sizeof (response) - 1 /* NUL */ - have);
-          if (nread < 0)
-            {
-              rc = -nread;
-              goto leave;
-            }
-          have += nread;
-          if (have < 3)
-            {
-              rc = SPWQ_PROTOCOL_ERROR;
-              goto leave;
-            }
-          response[have] = 0;
-        }
-
-      if (response[0] == 'O' && response[1] == 'K')
-        /* OK, do nothing.  */;
-      else if ((nread > 7 && !memcmp (response, "ERR 111", 7)
-                && (response[7] == ' ' || response[7] == '\n') )
-               || ((nread > 4 && !memcmp (response, "ERR ", 4)
-                    && (strtoul (response+4, NULL, 0) & 0xffff) == 99)) )
-        {
-          /* 111 is the old Assuan code for canceled which might still
-             be in use by old installations. 99 is GPG_ERR_CANCELED as
-             used by modern gpg-agents; 0xffff is used to mask out the
-             error source.  */
-#ifdef SPWQ_USE_LOGGING
-          log_info (_("canceled by user\n") );
-#endif
-        }
-      else if (response[0] == 'S' && response[1] == ' ')
-        {
-          char *nextline;
-          int consumed;
-
-          nextline = strchr (response, '\n');
-          if (! nextline)
-            /* Point to the NUL.  */
-            nextline = &response[have];
-          else
-            /* Move past the \n.  */
-            nextline ++;
-
-          consumed = (size_t) nextline - (size_t) response;
+    return rc;
 
-          /* Skip any additional newlines.  */
-          while (consumed < have && response[consumed] == '\n')
-            consumed ++;
+  rc = assuan_transact (ctx, query, NULL, NULL, NULL, NULL, NULL, NULL);
 
-          have -= consumed;
-
-          if (have)
-            memmove (response, &response[consumed], have + 1);
-
-          continue;
-        }
-      else
-        {
-#ifdef SPWQ_USE_LOGGING
-          log_error (_("problem with the agent (unexpected response \"%s\")\n"),
-                     response);
-#endif
-          rc = SPWQ_ERR_RESPONSE;
-        }
-
-      break;
-    }
-
- leave:
-  if (fd != -1)
-    close (fd);
+  assuan_release (ctx);
   return rc;
 }
diff --git a/tools/Makefile.am b/tools/Makefile.am
index bc159d9..12e5815 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -113,7 +113,7 @@ gpgparsemail_LDADD =
 symcryptrun_SOURCES = symcryptrun.c
 symcryptrun_LDADD = $(LIBUTIL_LIBS) $(common_libs) $(pwquery_libs) \
                     $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) \
-                    $(LIBICONV) $(NETLIBS) $(W32SOCKLIBS)
+                    $(LIBICONV) $(NETLIBS) $(W32SOCKLIBS) $(LIBASSUAN_LIBS)
 
 watchgnupg_SOURCES = watchgnupg.c
 watchgnupg_LDADD = $(NETLIBS)

commit 9e6503b7ce019aa417099ded1dda87b68c33f912
Author: Justus Winter <justus at g10code.com>
Date:   Thu Aug 11 09:52:08 2016 +0200

    common: Remove simple password query error codes.
    
    * common/simple-pwquery.h: Remove mapping function.  Move all
    definitions of status codes...
    * common/simple-pwquery.c: ... here, and define them to meaningful gpg
    error values.
    * agent/preset-passphrase.c (preset_passphrase): Use error code as-is.
    (forget_passphrase): Likewise.
    * tools/symcryptrun.c (confucius_get_pass): Likewise.
    
    Signed-off-by: Justus Winter <justus at g10code.com>

diff --git a/agent/preset-passphrase.c b/agent/preset-passphrase.c
index 549ecc3..485ca7b 100644
--- a/agent/preset-passphrase.c
+++ b/agent/preset-passphrase.c
@@ -111,10 +111,6 @@ my_strusage (int level)
 
 

 
-/* Include the implementation of map_spwq_error.  */
-MAP_SPWQ_ERROR_IMPL
-
-
 static void
 preset_passphrase (const char *keygrip)
 {
@@ -170,7 +166,7 @@ preset_passphrase (const char *keygrip)
   if (!opt_passphrase)
     wipememory (passphrase, sizeof (passphrase));
 
-  rc = map_spwq_error (simple_query (line));
+  rc = simple_query (line);
   if (rc)
     {
       log_error ("caching passphrase failed: %s\n", gpg_strerror (rc));
@@ -192,7 +188,7 @@ forget_passphrase (const char *keygrip)
   if (rc < 0)
     rc = gpg_error_from_syserror ();
   else
-    rc = map_spwq_error (simple_query (line));
+    rc = simple_query (line);
   if (rc)
     {
       log_error ("clearing passphrase failed: %s\n", gpg_strerror (rc));
diff --git a/common/simple-pwquery.c b/common/simple-pwquery.c
index 708b157..bd40fdf 100644
--- a/common/simple-pwquery.c
+++ b/common/simple-pwquery.c
@@ -50,6 +50,15 @@
 #define SIMPLE_PWQUERY_IMPLEMENTATION 1
 #include "simple-pwquery.h"
 
+#define SPWQ_OUT_OF_CORE	gpg_error_from_errno (ENOMEM)
+#define SPWQ_IO_ERROR		gpg_error_from_errno (EIO)
+#define SPWQ_PROTOCOL_ERROR	gpg_error (GPG_ERR_PROTOCOL_VIOLATION)
+#define SPWQ_ERR_RESPONSE	gpg_error (GPG_ERR_INV_RESPONSE)
+#define SPWQ_NO_AGENT		gpg_error (GPG_ERR_NO_AGENT)
+#define SPWQ_SYS_ERROR		gpg_error_from_syserror ()
+#define SPWQ_GENERAL_ERROR	gpg_error (GPG_ERR_GENERAL)
+#define SPWQ_NO_PIN_ENTRY	gpg_error (GPG_ERR_NO_PIN_ENTRY)
+
 #ifndef _
 #define _(a) (a)
 #endif
diff --git a/common/simple-pwquery.h b/common/simple-pwquery.h
index 5ae696a..2b87b11 100644
--- a/common/simple-pwquery.h
+++ b/common/simple-pwquery.h
@@ -67,47 +67,4 @@ int simple_query (const char *query);
    to be called before any other function.  Returns 0 on success.  */
 int simple_pw_set_socket (const char *name);
 
-#define SPWQ_OUT_OF_CORE 1
-#define SPWQ_IO_ERROR 2
-#define SPWQ_PROTOCOL_ERROR 3
-#define SPWQ_ERR_RESPONSE 4
-#define SPWQ_NO_AGENT 5
-#define SPWQ_SYS_ERROR 6
-#define SPWQ_GENERAL_ERROR 7
-#define SPWQ_NO_PIN_ENTRY 8
-
-
-/* We often need to map error codes to gpg-error style error codes.
-   To have a consistent mapping this macro may be used to implemt the
-   mapping function.  */
-#define MAP_SPWQ_ERROR_IMPL                                 \
-       static gpg_error_t                                   \
-       map_spwq_error (int err)                             \
-       {                                                    \
-         switch (err)                                       \
-           {                                                \
-           case 0:                                          \
-             return 0;                                      \
-           case SPWQ_OUT_OF_CORE:                           \
-             return gpg_error_from_errno (ENOMEM);          \
-           case SPWQ_IO_ERROR:                              \
-             return gpg_error_from_errno (EIO);             \
-           case SPWQ_PROTOCOL_ERROR:                        \
-             return gpg_error (GPG_ERR_PROTOCOL_VIOLATION); \
-           case SPWQ_ERR_RESPONSE:                          \
-             return gpg_error (GPG_ERR_INV_RESPONSE);       \
-           case SPWQ_NO_AGENT:                              \
-             return gpg_error (GPG_ERR_NO_AGENT);           \
-           case SPWQ_SYS_ERROR:                             \
-             return gpg_error_from_syserror ();             \
-           case SPWQ_NO_PIN_ENTRY:                          \
-             return gpg_error (GPG_ERR_NO_PIN_ENTRY);       \
-           case SPWQ_GENERAL_ERROR:                         \
-           default:                                         \
-             return gpg_error (GPG_ERR_GENERAL);            \
-           }                                                \
-       }
-/* End of MAP_SPWQ_ERROR_IMPL.  */
-
-
 #endif /*SIMPLE_PWQUERY_H*/
diff --git a/tools/symcryptrun.c b/tools/symcryptrun.c
index 5c1f0da..b6dc843 100644
--- a/tools/symcryptrun.c
+++ b/tools/symcryptrun.c
@@ -238,9 +238,6 @@ my_strusage (int level)
        __result; }))
 #endif
 
-/* Include the implementation of map_spwq_error.  */
-MAP_SPWQ_ERROR_IMPL
-
 /* Unlink a file, and shred it if SHRED is true.  */
 int
 remove_file (char *name, int shred)
@@ -441,7 +438,6 @@ confucius_get_pass (const char *cacheid, int again, int *canceled)
   pw = simple_pwquery (cacheid,
                        again ? _("does not match - try again"):NULL,
                        _("Passphrase:"), NULL, 0, &err);
-  err = map_spwq_error (err);
   i18n_switchback (orig_codeset);
 
   if (!pw)

-----------------------------------------------------------------------

Summary of changes:
 agent/Makefile.am         |   2 +-
 agent/preset-passphrase.c |   8 +-
 common/Makefile.am        |   2 +-
 common/simple-pwquery.c   | 429 ++++++++++++----------------------------------
 common/simple-pwquery.h   |  43 -----
 common/w32-afunix.c       | 148 ----------------
 common/w32-afunix.h       |  63 -------
 tools/Makefile.am         |   2 +-
 tools/symcryptrun.c       |   4 -
 9 files changed, 115 insertions(+), 586 deletions(-)
 delete mode 100644 common/w32-afunix.c
 delete mode 100644 common/w32-afunix.h


hooks/post-receive
-- 
The GNU Privacy Guard
http://git.gnupg.org




More information about the Gnupg-commits mailing list