[Pkg-gnupg-commit] [libassuan] 215/437: Add new API assuan_set_sock_nonce. Fixed a blocking problem on Windows.

Eric Dorland eric at moszumanska.debian.org
Fri May 22 05:33:45 UTC 2015


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository libassuan.

commit 28f7b6e0413097e91d9f28da028064d1ba9071eb
Author: Werner Koch <wk at gnupg.org>
Date:   Tue Oct 2 08:44:21 2007 +0000

    Add new API assuan_set_sock_nonce.
    Fixed a blocking problem on Windows.
---
 doc/assuan.texi            | 18 +++++++++++++++++-
 src/ChangeLog              | 11 +++++++++++
 src/assuan-defs.h          |  5 +++--
 src/assuan-io.c            | 37 ++++++++++++++++++++++++-------------
 src/assuan-socket-server.c | 19 ++++++++++++++++++-
 src/assuan-socket.c        |  5 ++++-
 src/assuan.h               |  5 +++++
 7 files changed, 82 insertions(+), 18 deletions(-)

diff --git a/doc/assuan.texi b/doc/assuan.texi
index 943fa10..8b0b36b 100644
--- a/doc/assuan.texi
+++ b/doc/assuan.texi
@@ -920,6 +920,21 @@ is indicated by a returning an error code.  In case of error,
 @end deftypefun
 
 @noindent
+On the Windows platform the following function needs to be called after
+assuan_init_socket_server_ext:
+
+ at deftypefun void assuan_set_sock_nonce ( @
+        @w{assuan_context_t @var{ctx}}, @
+        @w{assuan_sock_nonce_t *@var{nonce}})
+
+Save a copy of @var{nonce} in context @var{ctx}.  This should be used to
+register the server's nonce with a context established by
+assuan_init_socket_server.  It is actually only needed for Windows but
+it does not harm to use it on other systems as well.
+ at end deftypefun
+
+
+ at noindent
 After error checking, the implemented assuan commands are registered with
 the server.  
 
@@ -938,6 +953,7 @@ the server.
     @} 
 @end example
 
+
 @deftypefun assuan_error_t assuan_register_command (@w{assuan_context_t @var{ctx}}, @w{const char *@var{cmd_string}}, @w{int (*@var{handler}) (assuan_context_t, char *)})
 
 This registers the command named @var{cmd_string} with the Assuan
@@ -1642,7 +1658,7 @@ On Windows this is used by the server after an accept to read the nonce
 from the client and compare it with the saved @var{nonce}.  If this
 function fails the server should immediatly drop the connection.  To
 keep the code readable this may also be used on POSIX system; it is a
-dummy function then.
+dummy function then.  See also @code{assuan_set_sock_nonce}.
 @end deftypefun
 
 
diff --git a/src/ChangeLog b/src/ChangeLog
index 3156c7f..fa10021 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,16 @@
+2007-10-02  Werner Koch  <wk at g10code.com>
+
+	* assuan-io.c (_assuan_io_read) [W32]: Map WSAEWOULDBLOCK to EAGAIN.
+	* assuan-socket.c (_assuan_sock_check_nonce): N needs to be signed.
+
+	* assuan-defs.h (struct assuan_context_s): Add LISTEN_NONCE.
+	* assuan-socket-server.c (assuan_set_sock_nonce): New.
+	(accept_connection): Check the nonce.
+
 2007-10-01  Werner Koch  <wk at g10code.com>
 
+	* assuan.h (ASSUAN_INT2FD, ASSUAN_FD2INT): New.
+
 	* assuan-socket.c: Rewritten.
 	(assuan_sock_new, assuan_sock_connect, assuan_sock_bind) 
 	(assuan_sock_get_nonce, assuan_sock_check_nonce): New APIs.
diff --git a/src/assuan-defs.h b/src/assuan-defs.h
index 6c992ef..3c06436 100644
--- a/src/assuan-defs.h
+++ b/src/assuan-defs.h
@@ -136,6 +136,7 @@ struct assuan_context_s
   pid_t pid;	  /* The pid of the peer. */
   assuan_fd_t listen_fd;  /* The fd we are listening on (used by
                              socket servers) */
+  assuan_sock_nonce_t listen_nonce; /* Used with LISTEN_FD.  */
   assuan_fd_t connected_fd; /* helper */
 
   struct {
@@ -186,8 +187,8 @@ struct assuan_context_s
   /* If set, this is called right before logging an I/O line.  With
      DIRECTION set to 1 it is called for an output oeration; 0 means
      an input operation. If bit 0 is set in the return value, the
-     logging of the will be suppressed.  With bit 1 set, the entire
-     line will be ignored. */
+     logging of the line will be suppressed.  With bit 1 set, the
+     entire line will be ignored. */
   unsigned int (*io_monitor)(assuan_context_t ctx,
                              int direction,
                              const char *line,
diff --git a/src/assuan-io.c b/src/assuan-io.c
index df5fe57..eb50f7a 100644
--- a/src/assuan-io.c
+++ b/src/assuan-io.c
@@ -56,22 +56,33 @@ _assuan_io_read (assuan_fd_t fd, void *buffer, size_t size)
   int n;
 
   n = recv (HANDLE2SOCKET(fd), buffer, size, 0);
-  if (n == -1 && WSAGetLastError () == WSAENOTSOCK)
+  if (n == -1)
     {
-      DWORD nread = 0;
-
-      n = ReadFile (fd, buffer, size, &nread, NULL);
-      if (!n)
+      switch (WSAGetLastError ())
         {
-          switch (GetLastError())
-            {
-            case ERROR_BROKEN_PIPE: errno = EPIPE; break;
-            default: errno = EIO; 
-            }
-          n = -1;
+        case WSAENOTSOCK:
+          {
+            DWORD nread = 0;
+            
+            n = ReadFile (fd, buffer, size, &nread, NULL);
+            if (!n)
+              {
+                switch (GetLastError())
+                  {
+                  case ERROR_BROKEN_PIPE: errno = EPIPE; break;
+                  default: errno = EIO; 
+                  }
+                n = -1;
+              }
+            else
+              n = (int)nread;
+          }
+          break;
+          
+        case WSAEWOULDBLOCK: errno = EAGAIN; break;
+        case ERROR_BROKEN_PIPE: errno = EPIPE; break;
+        default: errno = EIO; break;
         }
-      else
-        n = (int)nread;
     }
   return n;
 #else /*!HAVE_W32_SYSTEM*/
diff --git a/src/assuan-socket-server.c b/src/assuan-socket-server.c
index d346f66..b754b81 100644
--- a/src/assuan-socket-server.c
+++ b/src/assuan-socket-server.c
@@ -1,5 +1,5 @@
 /* assuan-socket-server.c - Assuan socket based server
- *	Copyright (C) 2002 Free Software Foundation, Inc.
+ *	Copyright (C) 2002, 2007 Free Software Foundation, Inc.
  *
  * This file is part of Assuan.
  *
@@ -98,6 +98,12 @@ accept_connection (assuan_context_t ctx)
       ctx->os_errno = errno;
       return _assuan_error (ASSUAN_Accept_Failed);
     }
+  if (_assuan_sock_check_nonce (fd, &ctx->listen_nonce))
+    {
+      _assuan_close (fd);
+      ctx->os_errno = EACCES;
+      return _assuan_error (ASSUAN_Accept_Failed);
+    }
 
   ctx->connected_fd = fd;
   return accept_connection_bottom (ctx);
@@ -190,3 +196,14 @@ assuan_init_socket_server_ext (assuan_context_t *r_ctx, assuan_fd_t fd,
     *r_ctx = ctx;
   return rc;
 }
+
+
+/* Save a copy of NONCE in context CTX.  This should be used to
+   register the server's nonce with an context established by
+   assuan_init_socket_server.  */
+void
+assuan_set_sock_nonce (assuan_context_t ctx, assuan_sock_nonce_t *nonce)
+{
+  if (ctx && nonce)
+    ctx->listen_nonce = *nonce;
+}
diff --git a/src/assuan-socket.c b/src/assuan-socket.c
index 26ccd05..d2a01f7 100644
--- a/src/assuan-socket.c
+++ b/src/assuan-socket.c
@@ -32,6 +32,8 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <assert.h>
+
 #include "assuan-defs.h"
 
 /* Hacks for Slowaris.  */
@@ -306,7 +308,8 @@ _assuan_sock_check_nonce (assuan_fd_t fd, assuan_sock_nonce_t *nonce)
 {
 #ifdef HAVE_W32_SYSTEM
   char buffer[16], *p;
-  size_t nleft, n;
+  size_t nleft;
+  int n;
 
   if (sizeof nonce->nonce != 16)
     {
diff --git a/src/assuan.h b/src/assuan.h
index eec9842..56c3c8d 100644
--- a/src/assuan.h
+++ b/src/assuan.h
@@ -377,9 +377,13 @@ typedef struct assuan_context_s *ASSUAN_CONTEXT _ASSUAN_DEPRECATED;
 #ifdef _WIN32
 typedef void *assuan_fd_t;
 #define ASSUAN_INVALID_FD ((void*)(-1))
+#define ASSUAN_INT2FD(s)  ((void *)(s))
+#define ASSUAN_FD2INT(h)  ((unsigned int)(h))
 #else
 typedef int assuan_fd_t;
 #define ASSUAN_INVALID_FD (-1)
+#define ASSUAN_INT2FD(s)  ((s))
+#define ASSUAN_FD2INT(h)  ((h))
 #endif
 
 
@@ -475,6 +479,7 @@ int assuan_init_connected_socket_server (assuan_context_t *r_ctx,
                                          assuan_fd_t fd) _ASSUAN_DEPRECATED;
 int assuan_init_socket_server_ext (assuan_context_t *r_ctx, assuan_fd_t fd,
                                    unsigned int flags);
+void assuan_set_sock_nonce (assuan_context_t ctx, assuan_sock_nonce_t *nonce);
 
 /*-- assuan-pipe-connect.c --*/
 assuan_error_t assuan_pipe_connect (assuan_context_t *ctx,

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/libassuan.git



More information about the Pkg-gnupg-commit mailing list