[svn] dirmngr - r270 - trunk/src

svn author marcus cvs at cvs.gnupg.org
Fri Oct 5 19:30:43 CEST 2007


Author: marcus
Date: 2007-10-05 19:30:33 +0200 (Fri, 05 Oct 2007)
New Revision: 270

Modified:
   trunk/src/ChangeLog
   trunk/src/dirmngr.c
   trunk/src/dirmngr.h
   trunk/src/server.c
Log:
2007-10-05  Marcus Brinkmann  <marcus at g10code.de>

	* dirmngr.h: Include assuan.h.
	(start_command_handler): Change type of FD to assuan_fd_t.
	* dirmngr.c: Do not include w32-afunix.h.
        (socket_nonce): New global variable.
        (create_server_socket): Use assuan socket wrappers.  Remove W32
	specific stuff.  Save the server nonce.
        (check_nonce): New function.
        (start_connection_thread): Call it.
        (handle_connections): Change args to assuan_fd_t.
	* server.c (start_command_handler): Change type of FD to assuan_fd_t.


Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2007-09-14 11:41:34 UTC (rev 269)
+++ trunk/src/ChangeLog	2007-10-05 17:30:33 UTC (rev 270)
@@ -1,3 +1,16 @@
+2007-10-05  Marcus Brinkmann  <marcus at g10code.de>
+
+	* dirmngr.h: Include assuan.h.
+	(start_command_handler): Change type of FD to assuan_fd_t.
+	* dirmngr.c: Do not include w32-afunix.h.
+        (socket_nonce): New global variable.
+        (create_server_socket): Use assuan socket wrappers.  Remove W32
+	specific stuff.  Save the server nonce.
+        (check_nonce): New function.
+        (start_connection_thread): Call it.
+        (handle_connections): Change args to assuan_fd_t.
+	* server.c (start_command_handler): Change type of FD to assuan_fd_t.
+
 2007-09-12  Marcus Brinkmann  <marcus at g10code.de>
 
 	* dirmngr.c (main): Percent escape pathnames in --gpgconf-list output.

Modified: trunk/src/dirmngr.c
===================================================================
--- trunk/src/dirmngr.c	2007-09-14 11:41:34 UTC (rev 269)
+++ trunk/src/dirmngr.c	2007-10-05 17:30:33 UTC (rev 270)
@@ -42,7 +42,7 @@
 
 #include <gcrypt.h>
 #include <ksba.h>
-#include <assuan.h> /* Needed for the malloc hooks */
+#include <assuan.h> /* Needed for the malloc and socket hooks */
 
 #define JNLIB_NEED_LOG_LOGV
 #include "dirmngr.h"
@@ -53,8 +53,10 @@
 #include "i18n.h"
 
 #ifdef HAVE_W32_SYSTEM
-#include "../jnlib/w32-afunix.h"
 #define sleep _sleep
+#define FD2INT(h) ((unsigned int) (h))
+#else
+#define FD2INT(h) (h)
 #endif
 
 enum cmd_and_opt_values 
@@ -205,15 +207,24 @@
 
 /* For the cleanup handler we need to keep track of the socket's name. */
 static const char *socket_name;
+
+/* We need to keep track of the server's nonces (these are dummies for
+   POSIX systems). */
+static assuan_sock_nonce_t socket_nonce;
+
 /* Only if this flag has been set we will remove the socket file.  */
 static int cleanup_socket;
+
 /* Keep track of the current log file so that we can avoid updating
    the log file after a SIGHUP if it didn't changed. Malloced. */
 static char *current_logfile;
+
 /* Helper to implement --debug-level. */
 static const char *debug_level;
+
 /* Flag indicating that a shutdown has been requested.  */
 static volatile int shutdown_pending;
+
 /* Counter for the active connections.  */
 static int active_connections;
 
@@ -232,7 +243,7 @@
 static ldap_server_t parse_ldapserver_file (const char* filename);
 static void free_ldapservers_list (ldap_server_t servers);
 static fingerprint_list_t parse_ocsp_signer (const char *string);
-static void handle_connections (int listen_fd);
+static void handle_connections (assuan_fd_t listen_fd);
 
 /* Pth wrapper function definitions. */
 GCRY_THREAD_OPTION_PTH_IMPL;
@@ -883,12 +894,12 @@
       launch_reaper_thread ();
       cert_cache_init ();
       crl_cache_init ();
-      start_command_handler (-1);
+      start_command_handler (ASSUAN_INVALID_FD);
       shutdown_reaper ();
     }
   else if (cmd == aDaemon)
     {
-      int fd;
+      assuan_fd_t fd;
       pid_t pid;
       int len;
       struct sockaddr_un serv_addr;
@@ -919,14 +930,10 @@
           dirmngr_exit (1);
         }
     
-#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)
+      fd = assuan_sock_new (AF_UNIX, SOCK_STREAM, 0);
+      if (fd == ASSUAN_INVALID_FD)
         {
-          log_error (_("can't create socket: %s\n"), strerror(errno) );
+          log_error (_("can't create socket: %s\n"), strerror (errno));
           cleanup ();
           dirmngr_exit (1);
         }
@@ -935,36 +942,30 @@
       serv_addr.sun_family = AF_UNIX;
       strcpy (serv_addr.sun_path, socket_name);
       len = (offsetof (struct sockaddr_un, sun_path)
-             + strlen(serv_addr.sun_path) + 1);
+             + strlen (serv_addr.sun_path) + 1);
 
-#ifdef HAVE_W32_SYSTEM
-      rc = _w32_sock_bind (fd, (struct sockaddr*) &serv_addr, len);
-      if (rc == -1 && errno == WSAEADDRINUSE)
+      rc = assuan_sock_bind (fd, (struct sockaddr*) &serv_addr, len);
+      if (rc == -1 && errno == EADDRINUSE)
 	{
 	  remove (socket_name);
-	  rc = _w32_sock_bind (fd, (struct sockaddr*) &serv_addr, len);
+	  rc = assuan_sock_bind (fd, (struct sockaddr*) &serv_addr, len);
 	}
-#else
-      rc = bind (fd, (struct sockaddr*)&serv_addr, len);
-      if (rc == -1 && errno == EADDRINUSE)
-        {
-          remove (socket_name);
-          rc = bind (fd, (struct sockaddr*)&serv_addr, len);
-        }
-#endif
+      if (rc != -1 
+	  && (rc = assuan_sock_get_nonce ((struct sockaddr*) &serv_addr, len, &socket_nonce)))
+	log_error (_("error getting nonce for the socket\n"));
       if (rc == -1)
         {
           log_error (_("error binding socket to `%s': %s\n"),
-                     serv_addr.sun_path, strerror (errno) );
-          close (fd);
+                     serv_addr.sun_path, gpg_strerror (gpg_error_from_errno (errno)));
+          assuan_sock_close (fd);
           dirmngr_exit (1);
         }
       cleanup_socket = 1;
   
-      if (listen (fd, 5 ) == -1)
+      if (listen (FD2INT (fd), 5) == -1)
         {
           log_error (_("listen() failed: %s\n"), strerror (errno));
-          close (fd);
+          assuan_sock_close (fd);
           dirmngr_exit (1);
         }
 
@@ -1064,7 +1065,7 @@
 	}
 #endif
       handle_connections (fd);
-      close (fd);
+      assuan_sock_close (fd);
       shutdown_reaper ();
 #ifdef HAVE_W32_SYSTEM
       if (opt.system_service)
@@ -1694,20 +1695,40 @@
 }
 
 
+/* Check the nonce on a new connection.  This is a NOP unless we we
+   are using our Unix domain socket emulation under Windows.  */
+static int 
+check_nonce (assuan_fd_t fd, assuan_sock_nonce_t *nonce)
+{
+  if (assuan_sock_check_nonce (fd, nonce))
+    {
+      log_info (_("error reading nonce on fd %d: %s\n"), 
+                FD2INT (fd), strerror (errno));
+      assuan_sock_close (fd);
+      return -1;
+    }
+  else
+    return 0;
+}
+
+
 /* Helper to call a connection's main fucntion. */
 static void *
 start_connection_thread (void *arg)
 {
-  int fd = (int)arg;
+  assuan_fd_t fd = (assuan_fd_t) arg;
 
+  if (check_nonce (fd, &socket_nonce))
+    return NULL;
+
   active_connections++;
   if (opt.verbose)
-    log_info (_("handler for fd %d started\n"), fd);
+    log_info (_("handler for fd %d started\n"), FD2INT (fd));
 
   start_command_handler (fd);
 
   if (opt.verbose)
-    log_info (_("handler for fd %d terminated\n"), fd);
+    log_info (_("handler for fd %d terminated\n"), FD2INT (fd));
   active_connections--;
   
   return NULL;
@@ -1716,7 +1737,7 @@
 
 /* Main loop in daemon mode. */
 static void
-handle_connections (int listen_fd)
+handle_connections (assuan_fd_t listen_fd)
 {
   pth_attr_t tattr;
   pth_event_t ev, time_ev;
@@ -1724,7 +1745,7 @@
   int signo;
   struct sockaddr_un paddr;
   socklen_t plen = sizeof( paddr );
-  int fd;
+  assuan_fd_t fd;
 
   tattr = pth_attr_new();
   pth_attr_set (tattr, PTH_ATTR_JOINABLE, 0);
@@ -1767,11 +1788,11 @@
 
       if (time_ev)
         pth_event_concat (ev, time_ev, NULL);
-      fd = pth_accept_ev (listen_fd, (struct sockaddr *)&paddr, &plen, ev);
+      fd = (assuan_fd_t) pth_accept_ev (FD2INT (listen_fd), (struct sockaddr *)&paddr, &plen, ev);
       if (time_ev)
         pth_event_isolate (time_ev);
 
-      if (fd == -1)
+      if (fd == ASSUAN_INVALID_FD)
         {
           if (pth_event_occurred (ev)
               || (time_ev && pth_event_occurred (time_ev)) )
@@ -1814,7 +1835,7 @@
         {
           log_error (_("error spawning connection handler: %s\n"),
                      strerror (errno) );
-          close (fd);
+          assuan_sock_close (fd);
 	}
 
       /* Restore the signal mask. */

Modified: trunk/src/dirmngr.h
===================================================================
--- trunk/src/dirmngr.h	2007-09-14 11:41:34 UTC (rev 269)
+++ trunk/src/dirmngr.h	2007-10-05 17:30:33 UTC (rev 270)
@@ -27,6 +27,7 @@
 #include <gcrypt.h> /* we need this for the memory function protos */
 #include <gpg-error.h>
 #include <ksba.h>
+#include <assuan.h>
 
 /* to pass hash functions to libksba we need to cast it */
 #define HASH_FNC ((void (*)(void *, const void*,size_t))gcry_md_write)
@@ -172,7 +173,7 @@
 ksba_cert_t get_issuing_cert_local (ctrl_t ctrl, const char *issuer);
 ksba_cert_t get_cert_local_ski (ctrl_t ctrl,
                                 const char *name, ksba_sexp_t keyid);
-void start_command_handler (int fd);
+void start_command_handler (assuan_fd_t fd);
 gpg_error_t dirmngr_status (ctrl_t ctrl, const char *keyword, ...);
 gpg_error_t dirmngr_tick (ctrl_t ctrl);
 

Modified: trunk/src/server.c
===================================================================
--- trunk/src/server.c	2007-09-14 11:41:34 UTC (rev 269)
+++ trunk/src/server.c	2007-10-05 17:30:33 UTC (rev 270)
@@ -1039,7 +1039,7 @@
 /* Startup the server and run the main command loop.  With FD = -1
    used stdin/stdout. */
 void
-start_command_handler (int fd)
+start_command_handler (assuan_fd_t fd)
 {
   static const char hello[] = "Dirmngr " VERSION " at your service";
   static char *hello_line;
@@ -1060,7 +1060,7 @@
     
   dirmngr_init_default_ctrl (ctrl);
 
-  if (fd == -1)
+  if (fd == ASSUAN_INVALID_FD)
     {
       int filedes[2];
 




More information about the Gnupg-commits mailing list