[svn] GnuPG - r4512 - in trunk: . agent am common doc g10 jnlib kbx keyserver scd sm tools

svn author wk cvs at cvs.gnupg.org
Thu Jun 14 19:05:47 CEST 2007


Author: wk
Date: 2007-06-14 19:05:07 +0200 (Thu, 14 Jun 2007)
New Revision: 4512

Added:
   trunk/common/init.c
Modified:
   trunk/ChangeLog
   trunk/NEWS
   trunk/agent/ChangeLog
   trunk/agent/Makefile.am
   trunk/agent/agent.h
   trunk/agent/call-pinentry.c
   trunk/agent/call-scd.c
   trunk/agent/gpg-agent.c
   trunk/agent/minip12.c
   trunk/agent/preset-passphrase.c
   trunk/agent/protect-tool.c
   trunk/agent/trustlist.c
   trunk/am/cmacros.am
   trunk/common/ChangeLog
   trunk/common/Makefile.am
   trunk/common/estream.c
   trunk/common/homedir.c
   trunk/common/http.c
   trunk/common/simple-pwquery.c
   trunk/common/simple-pwquery.h
   trunk/common/util.h
   trunk/configure.ac
   trunk/doc/gpgsm.texi
   trunk/g10/ChangeLog
   trunk/g10/Makefile.am
   trunk/g10/call-agent.c
   trunk/g10/gpg.c
   trunk/g10/gpgv.c
   trunk/g10/keyserver.c
   trunk/g10/misc.c
   trunk/g10/openfile.c
   trunk/jnlib/ChangeLog
   trunk/jnlib/utf8conv.c
   trunk/jnlib/utf8conv.h
   trunk/kbx/ChangeLog
   trunk/kbx/kbxutil.c
   trunk/keyserver/ChangeLog
   trunk/keyserver/gpgkeys_hkp.c
   trunk/scd/ChangeLog
   trunk/scd/Makefile.am
   trunk/scd/apdu.c
   trunk/scd/command.c
   trunk/scd/scdaemon.c
   trunk/scd/scdaemon.h
   trunk/sm/ChangeLog
   trunk/sm/Makefile.am
   trunk/sm/call-agent.c
   trunk/sm/call-dirmngr.c
   trunk/sm/export.c
   trunk/sm/gpgsm.c
   trunk/sm/import.c
   trunk/sm/qualified.c
   trunk/tools/ChangeLog
   trunk/tools/Makefile.am
   trunk/tools/gpg-connect-agent.c
   trunk/tools/gpgconf-comp.c
   trunk/tools/gpgconf.c
   trunk/tools/symcryptrun.c
Log:
A whole bunch of changes to allow building for Windows.
See the ChangeLogs for details.


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,16 @@
+2007-06-14  Werner Koch  <wk at g10code.com>
+
+	* configure.ac [AH_BOTTOM]: Remove the hardwired names of modules.
+
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* configure.ac [AH_BOTTOM]: Define HTTP_NO_WSASTARTUP.
+
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* am/cmacros.am (libcommonstd, libcommonpth, libcommonstd_ldadd)
+	(libcommonpth_ldadd): Add macros.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* configure.ac: Add a few notices message so make browsing of the

Modified: trunk/NEWS
===================================================================
--- trunk/NEWS	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/NEWS	2007-06-14 17:05:07 UTC (rev 4512)
@@ -3,7 +3,9 @@
 
  * Fixed bug when using the --p12-charset without --armor.
 
+ * Changes required for a port to Windows.
 
+
 Noteworthy changes in version 2.0.4 (2007-05-09)
 ------------------------------------------------
 

Modified: trunk/agent/ChangeLog
===================================================================
--- trunk/agent/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,48 @@
+2007-06-14  Werner Koch  <wk at g10code.com>
+
+	* protect-tool.c (main): Setup default socket name for
+	simple-pwquery.
+	(MAP_SPWQ_ERROR_IMPL): New.  Use map_spwq_error for spqw related
+	error codes.
+	* preset-passphrase.c (main): Setup default socket name for
+	simple-pwquery.
+	(map_spwq_error): Remove.
+	(MAP_SPWQ_ERROR_IMPL): New.  
+
+	* call-pinentry.c (start_pinentry): Use gnupg_module_name.
+	* call-scd.c (start_scd): Ditto.
+
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* taskbar.c: New.
+
+	* trustlist.c (read_one_trustfile): Replace GNUPG_SYSCONFDIR by a
+	function call.
+	(read_trustfiles): Ditto.
+
+	* gpg-agent.c (main): Replace some calls by init_common_subsystems.
+	* preset-passphrase.c (main): Ditto.
+	* protect-tool.c (main): Ditto.
+
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* Makefile.am (common_libs): Use libcommonstd macro.
+	(commonpth_libs): Use libcommonpth macro.
+
+	* protect-tool.c (main) [W32]: Call pth_init.
+
+	* preset-passphrase.c (main) [W32]: Repalce the explicit Winsocket
+	init by a call to pth_init.
+
+	* trustlist.c (initialize_module_trustlist): New.
+	* gpg-agent.c (main): Call it.
+
+	* call-pinentry.c (initialize_module_query): Rename to
+	initialize_module_call_pinentry.
+
+	* minip12.c: Remove iconv.h. Add utf8conf.h.  Changed all iconv
+	calss to use these jnlib wrappers.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* minip12.c (enum): Rename CONTEXT to ASNCONTEXT as winnt.h

Modified: trunk/agent/Makefile.am
===================================================================
--- trunk/agent/Makefile.am	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/Makefile.am	2007-06-14 17:05:07 UTC (rev 4512)
@@ -23,6 +23,8 @@
 libexec_PROGRAMS = gpg-protect-tool gpg-preset-passphrase
 noinst_PROGRAMS = $(TESTS)
 
+EXTRA_DIST = gpg-agent.ico gpg-agent-resource.rc
+
 AM_CPPFLAGS = -I$(top_srcdir)/gl -I$(top_srcdir)/common -I$(top_srcdir)/intl
 
 include $(top_srcdir)/am/cmacros.am
@@ -45,15 +47,33 @@
 	call-scd.c \
 	learncard.c
 
+if HAVE_W32_SYSTEM
+gpg_agent_SOURCES += w32main.c w32main.h
+endif
 
-common_libs = ../jnlib/libjnlib.a ../common/libcommon.a ../gl/libgnu.a
-commonpth_libs = ../jnlib/libjnlib.a ../common/libcommonpth.a ../gl/libgnu.a
+common_libs = ../jnlib/libjnlib.a $(libcommon) ../gl/libgnu.a
+commonpth_libs = ../jnlib/libjnlib.a $(libcommonpth) ../gl/libgnu.a
 pwquery_libs = ../common/libsimple-pwquery.a
 
+if HAVE_W32_SYSTEM
+.rc.o:
+	$(WINDRES) `echo $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) | \
+	 sed -e 's/-I/--include-dir /g;s/-D/--define /g'` -i $< -o $@
+
+gpg_agent_res_ldflags = -Wl,gpg-agent-resource.o
+gpg_agent_res_deps = gpg-agent-resource.o
+else
+gpg_agent_res_ldflags =
+gpg_agent_res_deps =
+endif
+
+
 gpg_agent_CFLAGS = $(AM_CFLAGS) $(LIBASSUAN_PTH_CFLAGS) $(PTH_CFLAGS)
 gpg_agent_LDADD = $(commonpth_libs) \
                 $(LIBGCRYPT_LIBS) $(LIBASSUAN_PTH_LIBS) $(PTH_LIBS) \
 	        $(GPG_ERROR_LIBS) $(LIBINTL) $(NETLIBS) $(LIBICONV)
+gpg_agent_LDFLAGS = $(gpg_agent_res_ldflags)
+gpg_agent_DEPENDENCIES = $(gpg_agent_res_deps)
 
 gpg_protect_tool_SOURCES = \
 	protect-tool.c \
@@ -61,14 +81,15 @@
 	minip12.c minip12.h 
 
 # Needs $(NETLIBS) for libsimple-pwquery.la.
-gpg_protect_tool_LDADD =  $(pwquery_libs) $(common_libs) \
+gpg_protect_tool_LDADD = $(pwquery_libs) $(common_libs) \
          $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) $(NETLIBS) $(LIBICONV)
 
 gpg_preset_passphrase_SOURCES = \
 	preset-passphrase.c
 
 # Needs $(NETLIBS) for libsimple-pwquery.la.
-gpg_preset_passphrase_LDADD =  $(pwquery_libs) $(common_libs) \
+gpg_preset_passphrase_LDADD = \
+         $(pwquery_libs) $(common_libs) \
 	 $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) $(NETLIBS) $(LIBICONV)
 
 
@@ -77,6 +98,7 @@
 $(PROGRAMS): $(common_libs) $(commonpth_libs) $(pwquery_libs)
 
 
+
 #
 # Module tests
 #

Modified: trunk/agent/agent.h
===================================================================
--- trunk/agent/agent.h	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/agent.h	2007-06-14 17:05:07 UTC (rev 4512)
@@ -215,8 +215,8 @@
                                         gcry_sexp_t *result);
 int agent_key_available (const unsigned char *grip);
 
-/*-- query.c --*/
-void initialize_module_query (void);
+/*-- call-pinentry.c --*/
+void initialize_module_call_pinentry (void);
 void agent_query_dump_state (void);
 void agent_reset_query (ctrl_t ctrl);
 int pinentry_active_p (ctrl_t ctrl, int waitseconds);
@@ -276,6 +276,7 @@
 
 
 /*-- trustlist.c --*/
+void initialize_module_trustlist (void);
 gpg_error_t agent_istrusted (ctrl_t ctrl, const char *fpr);
 gpg_error_t agent_listtrusted (void *assuan_context);
 gpg_error_t agent_marktrusted (ctrl_t ctrl, const char *name,

Modified: trunk/agent/call-pinentry.c
===================================================================
--- trunk/agent/call-pinentry.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/call-pinentry.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -87,7 +87,7 @@
    static initialization because Pth emulation code might not be able
    to do a static init; in particular, it is not possible for W32. */
 void
-initialize_module_query (void)
+initialize_module_call_pinentry (void)
 {
   static int initialized;
 
@@ -217,7 +217,7 @@
     }
 
   if (!opt.pinentry_program || !*opt.pinentry_program)
-    opt.pinentry_program = GNUPG_DEFAULT_PINENTRY;
+    opt.pinentry_program = gnupg_module_name (GNUPG_MODULE_NAME_PINENTRY);
     pgmname = opt.pinentry_program;
   if ( !(pgmname = strrchr (opt.pinentry_program, '/')))
     pgmname = opt.pinentry_program;
@@ -751,6 +751,9 @@
     ; /* No pid available can't send a kill. */
   else if (popup_finished)
     ; /* Already finished and ready for joining. */
+#ifdef HAVE_W32_SYSTEM
+#  warning need to implement a kill mechanism for pinentry  
+#else
   else if (pid && ((rc=waitpid (pid, NULL, WNOHANG))==-1 || (rc == pid)) )
     { /* The daemon already died.  No need to send a kill.  However
          because we already waited for the process, we need to tell
@@ -762,6 +765,7 @@
   else if (pid > 0)
     kill (pid, SIGKILL);  /* Need to use SIGKILL due to bad
                              interaction of SIGINT with Pth. */
+#endif
 
   /* Now wait for the thread to terminate. */
   rc = pth_join (popup_tid, NULL);

Modified: trunk/agent/call-scd.c
===================================================================
--- trunk/agent/call-scd.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/call-scd.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -300,7 +300,7 @@
     }
 
   if (!opt.scdaemon_program || !*opt.scdaemon_program)
-    opt.scdaemon_program = GNUPG_DEFAULT_SCDAEMON;
+    opt.scdaemon_program = gnupg_module_name (GNUPG_MODULE_NAME_SCDAEMON);
   if ( !(pgmname = strrchr (opt.scdaemon_program, '/')))
     pgmname = opt.scdaemon_program;
   else
@@ -424,6 +424,9 @@
   if (primary_scd_ctx)
     {
       pid = assuan_get_pid (primary_scd_ctx);
+#ifdef HAVE_W32_SYSTEM
+#warning Need to implement an alive test for scdaemon
+#else
       if (pid != (pid_t)(-1) && pid
           && ((rc=waitpid (pid, NULL, WNOHANG))==-1 || (rc == pid)) )
         {
@@ -454,6 +457,7 @@
           xfree (socket_name);
           socket_name = NULL;
         }
+#endif
     }
 
   if (!pth_mutex_release (&start_scd_lock))

Modified: trunk/agent/gpg-agent.c
===================================================================
--- trunk/agent/gpg-agent.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/gpg-agent.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -33,9 +33,9 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 #ifndef HAVE_W32_SYSTEM
-#include <sys/socket.h>
-#include <sys/un.h>
-#endif /*HAVE_W32_SYSTEM*/
+# include <sys/socket.h>
+# include <sys/un.h>
+#endif /*!HAVE_W32_SYSTEM*/
 #include <unistd.h>
 #include <signal.h>
 #include <pth.h>
@@ -47,7 +47,8 @@
 #include "i18n.h"
 #include "sysutils.h"
 #ifdef HAVE_W32_SYSTEM
-#include "../jnlib/w32-afunix.h"
+# include "../jnlib/w32-afunix.h"
+# include "w32main.h"
 #endif
 #include "setenv.h"
 
@@ -408,8 +409,16 @@
 }
 
 
+/* The main entry point.  For W32 another name is used as the real
+   entry points needs to be named WinMain and is defined in
+   w32main.c. */
+#ifdef HAVE_W32_SYSTEM
 int
+w32_main (int argc, char **argv )
+#else
+int
 main (int argc, char **argv )
+#endif
 {
   ARGPARSE_ARGS pargs;
   int orig_argc;
@@ -434,6 +443,7 @@
   gpg_error_t err;
   const char *env_file_name = NULL;
 
+
   set_strusage (my_strusage);
   gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
   /* Please note that we may running SUID(ROOT), so be very CAREFUL
@@ -441,8 +451,8 @@
      somewhere after the option parsing */
   log_set_prefix ("gpg-agent", JNLIB_LOG_WITH_PREFIX|JNLIB_LOG_WITH_PID); 
 
-  /* Try to auto set the character set.  */
-  set_native_charset (NULL); 
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
 
   i18n_init ();
 
@@ -663,8 +673,9 @@
       exit (1);
     }
 
-  initialize_module_query ();
+  initialize_module_call_pinentry ();
   initialize_module_call_scd ();
+  initialize_module_trustlist ();
   
   /* Try to create missing directories. */
   create_directories ();
@@ -837,6 +848,7 @@
 #ifdef HAVE_W32_SYSTEM
       pid = getpid ();
       printf ("set GPG_AGENT_INFO=%s;%lu;1\n", socket_name, (ulong)pid);
+      w32_setup_taskbar ();
 #else /*!HAVE_W32_SYSTEM*/
       pid = fork ();
       if (pid == (pid_t)-1) 
@@ -1029,6 +1041,7 @@
   return 0;
 }
 
+
 void
 agent_exit (int rc)
 {
@@ -1048,7 +1061,6 @@
   exit (rc);
 }
 
-
 static void
 agent_init_default_ctrl (ctrl_t ctrl)
 {
@@ -1153,13 +1165,13 @@
 
 
 /* Create a name for the socket.  With USE_STANDARD_SOCKET given as
-   true using STANDARD_NAME in the home directory or if given has
+   true using STANDARD_NAME in the home directory or if given as
    false from the mkdir type name TEMPLATE.  In the latter case a
    unique name in a unique new directory will be created.  In both
    cases check for valid characters as well as against a maximum
    allowed length for a unix domain socket is done.  The function
    terminates the process in case of an error.  Returns: Pointer to an
-   allcoated string with the absolute name of the socket used.  */
+   allocated string with the absolute name of the socket used.  */
 static char *
 create_socket_name (int use_standard_socket,
 		    char *standard_name, char *template)
@@ -1303,6 +1315,9 @@
 create_directories (void)
 {
   struct stat statbuf;
+#ifdef HAVE_W32_SYSTEM
+#warning change it so that it works like in gpg.
+#endif
   const char *defhome = GNUPG_DEFAULT_HOMEDIR;
   char *home;
 
@@ -1478,7 +1493,7 @@
 }
 
 
-/* Connection handler loop.  Wait for coecntion requests and spawn a
+/* Connection handler loop.  Wait for connection requests and spawn a
    thread after accepting a connection.  */
 static void
 handle_connections (int listen_fd, int listen_fd_ssh)
@@ -1510,6 +1525,7 @@
   ev = pth_event (PTH_EVENT_SIGS, &sigs, &signo);
 #else
   ev = NULL;
+  signo = 0;
 #endif
   time_ev = NULL;
 
@@ -1522,6 +1538,10 @@
     {
       sigset_t oldsigs;
 
+#ifdef HAVE_W32_SYSTEM
+      w32_poll_events ();
+#endif
+
       if (shutdown_pending)
         {
           if (pth_ctrl (PTH_CTRL_GETTHREADS) == 1)

Modified: trunk/agent/minip12.c
===================================================================
--- trunk/agent/minip12.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/minip12.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -27,7 +27,6 @@
 #include <string.h>
 #include <assert.h>
 #include <gcrypt.h>
-#include <iconv.h>
 #include <errno.h>
 
 #ifdef TEST
@@ -36,18 +35,14 @@
 #endif
 
 #include "../jnlib/logging.h"
+#include "../jnlib/utf8conv.h"
 #include "minip12.h"
 
 #ifndef DIM
 #define DIM(v)		     (sizeof(v)/sizeof((v)[0]))
 #endif
 
-#ifndef ICONV_CONST
-#define ICONV_CONST
-#endif
 
-
-
 enum
 {
   UNIVERSAL = 0,
@@ -532,7 +527,7 @@
     {
       if (*charsets[charsetidx])
         {
-          iconv_t cd;
+          jnlib_iconv_t cd;
           const char *inptr;
           char *outptr;
           size_t inbytes, outbytes;
@@ -553,22 +548,22 @@
                 }
             }
 
-          cd = iconv_open (charsets[charsetidx], "utf-8");
-          if (cd == (iconv_t)(-1))
+          cd = jnlib_iconv_open (charsets[charsetidx], "utf-8");
+          if (cd == (jnlib_iconv_t)(-1))
             continue;
 
           inptr = pw;
           inbytes = strlen (pw);
           outptr = convertedpw;
           outbytes = convertedpwsize - 1;
-          if ( iconv (cd, (ICONV_CONST char **)&inptr, &inbytes,
+          if ( jnlib_iconv (cd, (const char **)&inptr, &inbytes,
                       &outptr, &outbytes) == (size_t)-1) 
             {
-              iconv_close (cd);
+              jnlib_iconv_close (cd);
               continue;
             }
           *outptr = 0;
-          iconv_close (cd);
+          jnlib_iconv_close (cd);
           log_info ("decryption failed; trying charset `%s'\n",
                     charsets[charsetidx]);
         }
@@ -2167,7 +2162,7 @@
 
   if (charset && pw && *pw)
     {
-      iconv_t cd;
+      jnlib_iconv_t cd;
       const char *inptr;
       char *outptr;
       size_t inbytes, outbytes;
@@ -2182,8 +2177,8 @@
           goto failure;
         }
 
-      cd = iconv_open (charset, "utf-8");
-      if (cd == (iconv_t)(-1))
+      cd = jnlib_iconv_open (charset, "utf-8");
+      if (cd == (jnlib_iconv_t)(-1))
         {
           log_error ("can't convert passphrase to"
                      " requested charset `%s': %s\n",
@@ -2196,18 +2191,18 @@
       inbytes = strlen (pw);
       outptr = pwbuf;
       outbytes = pwbufsize - 1;
-      if ( iconv (cd, (ICONV_CONST char **)&inptr, &inbytes,
+      if ( jnlib_iconv (cd, (const char **)&inptr, &inbytes,
                       &outptr, &outbytes) == (size_t)-1) 
         {
           log_error ("error converting passphrase to"
                      " requested charset `%s': %s\n",
                      charset, strerror (errno));
           gcry_free (pwbuf);
-          iconv_close (cd);
+          jnlib_iconv_close (cd);
           goto failure;
         }
       *outptr = 0;
-      iconv_close (cd);
+      jnlib_iconv_close (cd);
       pw = pwbuf;
     }
 

Modified: trunk/agent/preset-passphrase.c
===================================================================
--- trunk/agent/preset-passphrase.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/preset-passphrase.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -111,31 +111,9 @@
 
 
 
-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_GENERAL_ERROR:
-    default:
-      return gpg_error (GPG_ERR_GENERAL);
-    }
-}
 
+/* Include the implementation of map_spwq_error.  */
+MAP_SPWQ_ERROR_IMPL
       
 /* Convert the string SRC into HEX encoding.  Caller needs to xfree
    the returned string.  */
@@ -260,24 +238,9 @@
   set_strusage (my_strusage);
   log_set_prefix ("gpg-preset-passphrase", 1); 
 
-  /* Try to auto set the character set.  */
-  set_native_charset (NULL); 
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
 
-#ifdef HAVE_W32_SYSTEM
-  /* Fixme: Need to initialize the Windows sockets: This should be
-     moved to another place and we should make sure that it won't get
-     done twice, like when Pth is used too. */
-  {
-    WSADATA wsadat;
-    if (WSAStartup (0x202, &wsadat) )
-      {
-        log_error ("error initializing socket library: ec=%d\n", 
-                   (int)WSAGetLastError () );
-        return 2;
-      }
-  }
-#endif
-
   i18n_init ();
 
   opt_homedir = default_homedir ();
@@ -307,6 +270,13 @@
   else
     usage (1);
 
+  /* Tell simple-pwquery about the the standard socket name.  */
+  {
+    char *tmp = make_filename (opt_homedir, "S.gpg-agent", NULL);
+    simple_pw_set_socket (tmp);
+    xfree (tmp);
+  }
+
   if (cmd == oPreset)
     preset_passphrase (keygrip);
   else if (cmd == oForget)

Modified: trunk/agent/protect-tool.c
===================================================================
--- trunk/agent/protect-tool.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/protect-tool.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -160,6 +160,8 @@
 }
 
 
+/* Include the implementation of map_spwq_error.  */
+MAP_SPWQ_ERROR_IMPL
 
 /*  static void */
 /*  print_mpi (const char *text, gcry_mpi_t a) */
@@ -1033,8 +1035,8 @@
   gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
   log_set_prefix ("gpg-protect-tool", 1); 
 
-  /* Try to auto set the character set.  */
-  set_native_charset (NULL); 
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
 
   i18n_init ();
 
@@ -1092,6 +1094,13 @@
   else if (argc > 1)
     usage (1);
 
+  /* Tell simple-pwquery about the the standard socket name.  */
+  {
+    char *tmp = make_filename (opt_homedir, "S.gpg-agent", NULL);
+    simple_pw_set_socket (tmp);
+    xfree (tmp);
+  }
+
   if (opt_prompt)
     opt_prompt = percent_plus_unescape_string (xstrdup (opt_prompt));
 
@@ -1194,6 +1203,7 @@
   pw = simple_pwquery (NULL,
                        error_msgno == 1? _("does not match - try again"):NULL,
                        _("Passphrase:"), desc, opt_check, &err);
+  err = map_spwq_error (err);
 
 #ifdef ENABLE_NLS
   if (orig_codeset)

Modified: trunk/agent/trustlist.c
===================================================================
--- trunk/agent/trustlist.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/agent/trustlist.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -53,7 +53,7 @@
 static trustitem_t *trusttable; 
 static size_t trusttablesize; 
 /* A mutex used to protect the table. */
-static pth_mutex_t trusttable_lock = PTH_MUTEX_INIT;
+static pth_mutex_t trusttable_lock;
 
 
 
@@ -71,7 +71,25 @@
 "\n";
 
 
+/* This function must be called once to initialize this module.  This
+   has to be done before a second thread is spawned.  We can't do the
+   static initialization because Pth emulation code might not be able
+   to do a static init; in particular, it is not possible for W32. */
+void
+initialize_module_trustlist (void)
+{
+  static int initialized;
 
+  if (!initialized)
+    {
+      if (!pth_mutex_init (&trusttable_lock))
+        log_fatal ("error initializing mutex: %s\n", strerror (errno));
+      initialized = 1;
+    }
+}
+
+
+
 
 static void
 lock_trusttable (void)
@@ -153,7 +171,7 @@
             }
           /* fixme: Should check for trailing garbage.  */
 
-          etcname = make_filename (GNUPG_SYSCONFDIR, "trustlist.txt", NULL);
+          etcname = make_filename (gnupg_sysconfdir (), "trustlist.txt", NULL);
           if ( !strcmp (etcname, fname) ) /* Same file. */
             log_info (_("statement \"%s\" ignored in `%s', line %d\n"),
                       "include-default", fname, lnr);
@@ -303,7 +321,7 @@
           log_error (_("error opening `%s': %s\n"), fname, gpg_strerror (err));
         }
       xfree (fname);
-      fname = make_filename (GNUPG_SYSCONFDIR, "trustlist.txt", NULL);
+      fname = make_filename (gnupg_sysconfdir (), "trustlist.txt", NULL);
       allow_include = 0;
     }
   err = read_one_trustfile (fname, allow_include,

Modified: trunk/am/cmacros.am
===================================================================
--- trunk/am/cmacros.am	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/am/cmacros.am	2007-06-14 17:05:07 UTC (rev 4512)
@@ -30,6 +30,10 @@
                -DGNUPG_SYSCONFDIR="\"$(sysconfdir)/@PACKAGE@\""
 endif
 
+
+# If a specific protect tool program has been defined, pass its name
+# to cc.  Note that these macros should not be used directly but via
+# the gnupg_module_name function.
 if GNUPG_AGENT_PGM
 AM_CPPFLAGS += -DGNUPG_DEFAULT_AGENT="\"@GNUPG_AGENT_PGM@\""
 endif
@@ -45,3 +49,9 @@
 if GNUPG_PROTECT_TOOL_PGM
 AM_CPPFLAGS += -DGNUPG_DEFAULT_PROTECT_TOOL="\"@GNUPG_PROTECT_TOOL_PGM@\""
 endif
+
+
+# Convenience macros
+libcommon = ../common/libcommon.a
+libcommonpth = ../common/libcommonpth.a
+

Modified: trunk/common/ChangeLog
===================================================================
--- trunk/common/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,36 @@
+2007-06-14  Werner Koch  <wk at g10code.com>
+
+	* simple-pwquery.h (MAP_SPWQ_ERROR_IMPL): New.
+	(SPWQ_NO_PIN_ENTRY): New.
+	* simple-pwquery.c (simple_pw_set_socket): New.
+	(agent_open): Use it if GPG_AGENT_INFO is not set.
+	(simple_pwquery): Extended to allow returning of otehyr error codes.
+
+	* util.h (GNUPG_MODULE_NAME_AGENT, GNUPG_MODULE_NAME_PINENTRY) 
+	(GNUPG_MODULE_NAME_SCDAEMON, GNUPG_MODULE_NAME_DIRMNGR) 
+	(GNUPG_MODULE_NAME_PROTECT_TOOL): New.
+	* homedir.c (gnupg_module_name): New.
+	(gnupg_bindir): New.
+
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* homedir.c (gnupg_sysconfdir): New.
+	(gnupg_libexecdir): New.  Taken from g10/misc.c:get_libexecdir.
+	(gnupg_datadir): New.
+	(gnupg_libdir): New.
+
+	* http.c (connect_server) [W32]: Do not call init_sockets if
+	HTTP_NO_WSASTARTUP is defined.
+
+	* init.c: New.
+
+	* estream.c (es_init_do): Init stream lock here because we can't
+	use a static initialization with W32pth.
+
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* Makefile.am (t_common_ldadd): Use libcommonstd macro.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* Makefile.am: Include am/cmacros.am.

Modified: trunk/common/Makefile.am
===================================================================
--- trunk/common/Makefile.am	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/Makefile.am	2007-06-14 17:05:07 UTC (rev 4512)
@@ -37,6 +37,7 @@
 	openpgpdefs.h \
 	keyserver.h \
 	sexp-parse.h \
+	init.c \
 	sexputil.c \
 	sysutils.c sysutils.h \
 	homedir.c \
@@ -59,6 +60,7 @@
 	pka.c pka.h \
 	http.c http.h 
 
+
 libcommon_a_SOURCES = $(common_sources)
 if USE_DNS_SRV
 libcommon_a_SOURCES += srv.c
@@ -83,7 +85,7 @@
 #
 module_tests = t-convert
 
-t_common_ldadd = ../jnlib/libjnlib.a ../common/libcommon.a ../gl/libgnu.a \
+t_common_ldadd = ../jnlib/libjnlib.a $(libcommon) ../gl/libgnu.a \
                  $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) $(LIBINTL) $(LIBICONV)
 
 t_convert_DEPENDENCIES = convert.c libcommon.a

Modified: trunk/common/estream.c
===================================================================
--- trunk/common/estream.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/estream.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -109,8 +109,6 @@
   ((pth_mutex_acquire (&(mutex), 1, NULL) == TRUE) ? 0 : -1)
 # define ESTREAM_MUTEX_INITIALIZE(mutex)  \
   pth_mutex_init    (&(mutex))
-# define ESTREAM_THREADING_INIT() ((pth_init () == TRUE) ? 0 : -1)
-
 #else
 
 typedef void *estream_mutex_t;
@@ -119,8 +117,6 @@
 # define ESTREAM_MUTEX_UNLOCK(mutex) (void) 0
 # define ESTREAM_MUTEX_TRYLOCK(mutex) 0
 # define ESTREAM_MUTEX_INITIALIZE(mutex) (void) 0
-# define ESTREAM_THREADING_INIT() 0
-
 #endif
 
 /* Memory allocator functions.  */
@@ -194,13 +190,9 @@
 
 static estream_list_t estream_list;
 #ifdef HAVE_PTH
-/* Note that we can't use a static initialization with W32Pth; however
-   W32Pth does an implicit initialization anyway.  */
-static estream_mutex_t estream_list_lock
-# ifndef _W32_PTH_H
-         = ESTREAM_MUTEX_INITIALIZER
-# endif
-         ;
+/* Note that we can't use a static initialization with W32Pth, thus we
+   do it in es_init. */
+static estream_mutex_t estream_list_lock;
 #endif
 
 #define ESTREAM_LIST_LOCK   ESTREAM_MUTEX_LOCK   (estream_list_lock)
@@ -308,11 +300,18 @@
 static int
 es_init_do (void)
 {
-  int err;
+#ifdef HAVE_PTH
+  static int initialized;
 
-  err = ESTREAM_THREADING_INIT ();
-
-  return err;
+  if (!initialized)
+    {
+      if (!pth_init ())
+        return -1;
+      if (pth_mutex_init (&estream_list_lock))
+        initialized = 1;
+    }
+#endif
+  return 0;
 }
 
 

Modified: trunk/common/homedir.c
===================================================================
--- trunk/common/homedir.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/homedir.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -124,3 +124,152 @@
 
   return dir;
 }
+
+
+/* Return the name of the sysconfdir.  This is a static string.  This
+   function is required because under Windows we can't simply compile
+   it in.  */
+const char *
+gnupg_sysconfdir (void)
+{
+#ifdef HAVE_W32_SYSTEM
+#warning get the sysconfdir from somewhere else
+  return GNUPG_SYSCONFDIR;
+#else /*!HAVE_W32_SYSTEM*/
+  return GNUPG_SYSCONFDIR;
+#endif /*!HAVE_W32_SYSTEM*/
+}
+
+
+const char *
+gnupg_bindir (void)
+{
+#ifdef HAVE_W32_SYSTEM
+  return gnupg_libexecdir ();
+#else /*!HAVE_W32_SYSTEM*/
+  return GNUPG_BINDIR;
+#endif /*!HAVE_W32_SYSTEM*/
+}
+
+
+/* Return the name of the libexec directory.  The name is allocated in
+   a static area on the first use.  This function won't fail. */
+const char *
+gnupg_libexecdir (void)
+{
+#ifdef HAVE_W32_SYSTEM
+  static int got_dir;
+  static char dir[MAX_PATH+5];
+
+  if (!got_dir)
+    {
+      char *p;
+
+      if ( !GetModuleFileName ( NULL, dir, MAX_PATH) )
+        {
+          log_debug ("GetModuleFileName failed: %s\n", w32_strerror (0));
+          *dir = 0;
+        }
+      got_dir = 1;
+      p = strrchr (dir, DIRSEP_C);
+      if (p)
+        *p = 0;
+      else
+        {
+          log_debug ("bad filename `%s' returned for this process\n", dir);
+          *dir = 0; 
+        }
+    }
+
+  if (*dir)
+    return dir;
+  /* Fallback to the hardwired value. */
+#endif /*HAVE_W32_SYSTEM*/
+
+  return GNUPG_LIBEXECDIR;
+}
+
+const char *
+gnupg_libdir (void)
+{
+#ifdef HAVE_W32_SYSTEM
+#warning get the libdir from somewhere else
+  return GNUPG_LIBDIR;
+#else /*!HAVE_W32_SYSTEM*/
+  return GNUPG_LIBDIR;
+#endif /*!HAVE_W32_SYSTEM*/
+}
+
+const char *
+gnupg_datadir (void)
+{
+#ifdef HAVE_W32_SYSTEM
+#warning get the datadir from somewhere else
+  return GNUPG_DATADIR;
+#else /*!HAVE_W32_SYSTEM*/
+  return GNUPG_DATADIR;
+#endif /*!HAVE_W32_SYSTEM*/
+}
+
+
+/* Return the file name of a helper tool.  WHICH is one of the
+   GNUPG_MODULE_NAME_foo constants.  */
+const char *
+gnupg_module_name (int which)
+{
+  const char *s, *s2;
+
+#define X(a,b) do {                                          \
+        static char *name;                                   \
+        if (!name)                                           \
+          {                                                  \
+            s = gnupg_ ## a ();                              \
+            s2 = DIRSEP_S b EXEEXT_S;                        \
+            name = xmalloc (strlen (s) + strlen (s2) + 1);   \
+            strcpy (stpcpy (name, s), s2);                   \
+          }                                                  \
+        return name;                                         \
+      } while (0)                                                     
+
+  switch (which)
+    {
+    case GNUPG_MODULE_NAME_AGENT:
+#ifdef GNUPG_DEFAULT_AGENT
+      return GNUPG_DEFAULT_AGENT;
+#else 
+      X(bindir, "gpg-agent");
+#endif
+      
+    case GNUPG_MODULE_NAME_PINENTRY:
+#ifdef GNUPG_DEFAULT_PINENTRY
+      return GNUPG_DEFAULT_PINENTRY;
+#else 
+      X(bindir, "pinentry");
+#endif
+
+    case GNUPG_MODULE_NAME_SCDAEMON:
+#ifdef GNUPG_DEFAULT_SCDAEMON
+      return GNUPG_DEFAULT_SCDAEMON;
+#else 
+      X(bindir, "scdaemon");
+#endif
+
+    case GNUPG_MODULE_NAME_DIRMNGR:
+#ifdef GNUPG_DEFAULT_DIRMNGR
+      return GNUPG_DEFAULT_DIRMNGR;
+#else 
+      X(bindir, "dirmngr");
+#endif
+
+    case GNUPG_MODULE_NAME_PROTECT_TOOL:
+#ifdef GNUPG_DEFAULT_PROTECT_TOOL
+      return GNUPG_DEFAULT_PROTECT_TOOL;
+#else 
+      X(libexecdir, "gpg-protect-tool");
+#endif
+
+    default: 
+      BUG ();
+    }
+#undef X
+}

Modified: trunk/common/http.c
===================================================================
--- trunk/common/http.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/http.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -30,6 +30,10 @@
   - With HTTP_USE_ESTREAM defined, all I/O is done through estream.
   - With HTTP_USE_GNUTLS support for https is provided (this also
     requires estream).
+  - With HTTP_NO_WSASTARTUP the socket initialization is not done
+    under Windows.  This is useful if the socket layer has already
+    been initialized elsewhere.  This also avoids the installation of
+    an exit handler to cleanup the socket layer.
 */
 
 #ifdef HAVE_CONFIG_H
@@ -200,7 +204,7 @@
 
 
 
-#ifdef HAVE_W32_SYSTEM
+#if defined(HAVE_W32_SYSTEM) && !defined(HTTP_NO_WSASTARTUP)
 
 #if GNUPG_MAJOR_VERSION == 1
 #define REQ_WINSOCK_MAJOR  1
@@ -244,7 +248,7 @@
   atexit ( deinit_sockets );
   initialized = 1;
 }
-#endif /*HAVE_W32_SYSTEM*/
+#endif /*HAVE_W32_SYSTEM && !HTTP_NO_WSASTARTUP*/
 
 
 
@@ -1504,7 +1508,9 @@
 #ifdef HAVE_W32_SYSTEM
   unsigned long inaddr;
 
-  init_sockets();
+#ifndef HTTP_NO_WSASTARTUP
+  init_sockets ();
+#endif
   /* Win32 gethostbyname doesn't handle IP addresses internally, so we
      try inet_addr first on that platform only. */
   inaddr = inet_addr(server);

Added: trunk/common/init.c
===================================================================
--- trunk/common/init.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/init.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -0,0 +1,73 @@
+/* init.c - Various initializations
+ *	Copyright (C) 2007 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG is free software; you can redistribute it and/or modify
+ * it under the terms of 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.
+ *
+ * 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 copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#include <config.h>
+
+#ifdef WITHOUT_GNU_PTH /* Give the Makefile a chance to build without Pth.  */
+#undef HAVE_PTH
+#undef USE_GNU_PTH
+#endif
+
+#ifdef HAVE_W32_SYSTEM
+#include <windows.h>
+#endif
+#ifdef HAVE_PTH      
+#include <pth.h>
+#endif
+
+#include "estream.h"
+#include "util.h"
+
+
+/* This function is to be used early at program startup to make sure
+   that some subsystems are initialized.  This is in particualr
+   important for W32 to initialize the sockets so that our socket
+   emulation code used directly as well as in libassuan may be used.
+   It should best be called before any I/O is done so that setup
+   required for logging is ready.  CAUTION: This might be called while
+   running suid(root). */
+void
+init_common_subsystems (void)
+{
+  /* Try to auto set the character set.  */
+  set_native_charset (NULL); 
+
+#ifdef HAVE_W32_SYSTEM
+  /* For W32 we need to initialize the socket layer.  This is becuase
+     we use recv and send in libassuan as well as at some other
+     places.  If we are building with PTH we let pth_init do it.  We
+     can't do much on error so we ignore them.  An error would anyway
+     later pop up if one of the socket functions is used. */
+# ifdef HAVE_PTH
+  pth_init ();
+# else
+ {
+   WSADATA wsadat;
+
+   WSAStartup (0x202, &wsadat);
+ }
+# endif /*!HAVE_PTH*/
+#endif
+
+  /* Initialize the Estream library. */
+  es_init ();
+}
+

Modified: trunk/common/simple-pwquery.c
===================================================================
--- trunk/common/simple-pwquery.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/simple-pwquery.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,5 +1,5 @@
 /* simple-pwquery.c - A simple password query client for gpg-agent
- *	Copyright (C) 2002, 2004 Free Software Foundation, Inc.
+ *	Copyright (C) 2002, 2004, 2007 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
@@ -68,8 +68,13 @@
 #endif
 
 
+/* Name of the socket to be used if GPG_AGENT_INFO has not been
+   set. No default socket is used if this is NULL.  */
+static char *default_gpg_agent_info;
 
 
+
+
 
 
 #ifndef HAVE_STPCPY
@@ -154,9 +159,9 @@
         ;
       if (n)
         {
-          break; /* at least one full line available - that's enough.
+          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 */
+                    it is okay to forget about pending bytes.  */
         }
     }
 
@@ -305,6 +310,8 @@
   *rfd = -1;
   infostr = getenv ( "GPG_AGENT_INFO" );
   if ( !infostr || !*infostr ) 
+    infostr = default_gpg_agent_info;
+  if ( !infostr || !*infostr ) 
     {
 #ifdef SPWQ_USE_LOGGING
       log_error (_("gpg-agent is not available in this session\n"));
@@ -322,6 +329,9 @@
     {
 #ifdef SPWQ_USE_LOGGING
       log_error ( _("malformed GPG_AGENT_INFO environment variable\n"));
+      log_debug ( "a='%s'\n", infostr);
+      log_debug ( "a='%s'\n", strchr ( infostr, PATHSEP_C));
+      log_debug ( "a=%td\n", (p-infostr));
 #endif
       return SPWQ_NO_AGENT;
     }
@@ -425,13 +435,34 @@
 }
 
 
+/* Set the name of the default socket to NAME.  */
+int 
+simple_pw_set_socket (const char *name)
+{
+  spwq_free (default_gpg_agent_info);
+  if (name)
+    {
+      default_gpg_agent_info = spwq_malloc (strlen (name) + 4 + 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");
+    }
+  else
+    default_gpg_agent_info = NULL;
+
+  return 0;
+}
+
+
 /* Ask the gpg-agent for a passphrase and present the user with a
    DESCRIPTION, a PROMPT and optionally with a TRYAGAIN extra text.
    If a CACHEID is not NULL it is used to locate the passphrase in in
    the cache and store it under this ID.  If OPT_CHECK is true
    gpg-agent is asked to apply some checks on the passphrase security.
    If ERRORCODE is not NULL it should point a variable receiving an
-   errorcode; this errocode might be 0 if the user canceled the
+   errorcode; this error code might be 0 if the user canceled the
    operation.  The function returns NULL to indicate an error.  */
 char *
 simple_pwquery (const char *cacheid, 
@@ -530,8 +561,16 @@
 #ifdef SPWQ_USE_LOGGING
       log_info (_("canceled by user\n") );
 #endif
-      *errorcode = 0; /* canceled */
+      *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

Modified: trunk/common/simple-pwquery.h
===================================================================
--- trunk/common/simple-pwquery.h	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/simple-pwquery.h	2007-06-14 17:05:07 UTC (rev 4512)
@@ -41,7 +41,6 @@
 #define spwq_secure_malloc(a)  gcry_malloc_secure (a)
 #define spwq_secure_free(a)    gcry_free (a)
 
-
 #endif /*SIMPLE_PWQUERY_IMPLEMENTATION*/ /* End configuration stuff. */
 
 
@@ -67,6 +66,11 @@
    terminated) and return the error code.  */
 int simple_query (const char *query);
 
+/* Set the name of the standard socket to be used if GPG_AGENT_INFO is
+   not defined.  The use of this function is optional but if it needs
+   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 
@@ -74,5 +78,40 @@
 #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*/

Modified: trunk/common/util.h
===================================================================
--- trunk/common/util.h	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/common/util.h	2007-06-14 17:05:07 UTC (rev 4512)
@@ -120,6 +120,8 @@
   strcpy (d, s);
 }
 
+/*-- init.c --*/
+void init_common_subsystems (void);
 
 /*-- signal.c --*/
 void gnupg_init_signals (int mode, void (*fast_cleanup)(void));
@@ -170,7 +172,21 @@
 
 /*-- homedir.c --*/
 const char *default_homedir (void);
+const char *gnupg_sysconfdir (void);
+const char *gnupg_bindir (void);
+const char *gnupg_libexecdir (void);
+const char *gnupg_libdir (void);
+const char *gnupg_datadir (void);
 
+#define GNUPG_MODULE_NAME_AGENT        1
+#define GNUPG_MODULE_NAME_PINENTRY     2
+#define GNUPG_MODULE_NAME_SCDAEMON     3 
+#define GNUPG_MODULE_NAME_DIRMNGR      4
+#define GNUPG_MODULE_NAME_PROTECT_TOOL 5
+const char *gnupg_module_name (int which);
+
+
+
 /*-- gpgrlhelp.c --*/
 void gnupg_rl_initialize (void);
 

Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/configure.ac	2007-06-14 17:05:07 UTC (rev 4512)
@@ -363,31 +363,15 @@
    the values may be overridden by the make invocations; this is to
    comply with the GNU coding standards. */
 #ifdef HAVE_DRIVE_LETTERS
+ /* FIXME: We need to use a function to determine these values depending 
+    on the actual installation directory. */
 #define GNUPG_BINDIR      "c:\\gnupg"
 #define GNUPG_LIBEXECDIR  "c:\\gnupg"
 #define GNUPG_LIBDIR      "c:\\gnupg"
 #define GNUPG_DATADIR     "c:\\gnupg"
+#define GNUPG_SYSCONFDIR  "c:\\gnupg"
 #endif
 
-/* Setup the hardwired names of modules. */
-#ifndef GNUPG_DEFAULT_AGENT
-#define GNUPG_DEFAULT_AGENT    ( GNUPG_BINDIR DIRSEP_S "gpg-agent" EXEEXT_S )
-#endif
-#ifndef GNUPG_DEFAULT_PINENTRY
-#define GNUPG_DEFAULT_PINENTRY ( GNUPG_BINDIR DIRSEP_S "pinentry" EXEEXT_S )
-#endif
-#ifndef GNUPG_DEFAULT_SCDAEMON
-#define GNUPG_DEFAULT_SCDAEMON ( GNUPG_BINDIR DIRSEP_S "scdaemon" EXEEXT_S )
-#endif
-#ifndef GNUPG_DEFAULT_DIRMNGR
-#define GNUPG_DEFAULT_DIRMNGR  ( GNUPG_BINDIR DIRSEP_S "dirmngr" EXEEXT_S )
-#endif
-#ifndef GNUPG_DEFAULT_PROTECT_TOOL
-#define GNUPG_DEFAULT_PROTECT_TOOL \
-                    ( GNUPG_LIBEXECDIR DIRSEP_S "gpg-protect-tool" EXEEXT_S )
-#endif
-
-
 /* Derive some other constants. */
 #if !(defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID))
 #define EXEC_TEMPFILE_ONLY
@@ -416,6 +400,11 @@
 /* Our HTTP code is used in estream mode.  */
 #define HTTP_USE_ESTREAM 1
 
+/* Under W32 we do an explicit socket initialization, thus we need to
+   avoid the on-demand initialization which would also install an atexit
+   handler.  */
+#define HTTP_NO_WSASTARTUP
+
 /* We always include support for the OpenPGP card.  */
 #define ENABLE_CARD_SUPPORT 1
 
@@ -460,6 +449,7 @@
 AC_PROG_RANLIB
 AC_CHECK_TOOL(AR, ar, :)
 AC_PATH_PROG(PERL,"perl")
+AC_CHECK_TOOL(WINDRES, windres, :)
 AC_ISC_POSIX
 gl_EARLY
 AC_SYS_LARGEFILE

Modified: trunk/doc/gpgsm.texi
===================================================================
--- trunk/doc/gpgsm.texi	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/doc/gpgsm.texi	2007-06-14 17:05:07 UTC (rev 4512)
@@ -746,6 +746,15 @@
 random number generator accross invocations.  The same file is used by
 other programs of this software too.
 
+ at item S.gpg-agent
+ at cindex S.gpg-agent
+If this file exists and the environment variable @env{GPG_AGENT_INFO} is
+not set, @command{gpgsm} will first try to connect to this socket for
+accessing @command{gpg-agent} before starting a new @command{gpg-agent}
+instance.  Under Windows this socket (which in reality be a plain file
+describing a regular TCP litening port) is the standard way of
+connecting the @command{gpg-agent}.
+
 @end table
 
 

Modified: trunk/g10/ChangeLog
===================================================================
--- trunk/g10/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,24 @@
+2007-06-14  Werner Koch  <wk at g10code.com>
+
+	* call-agent.c (start_agent): Use gnupg_module_name.
+
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* openfile.c (copy_options_file): Use gnupg_datadir.
+	* misc.c (get_libexecdir): Remove.  Changed all callers to use
+	gnupg_libexecdir.
+	* gpg.c (check_permissions): Use gnupg_libdir.
+
+	* gpg.c (main): Replace some calls by init_common_subsystems.
+	* gpgv.c (main): Ditto.
+
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* Makefile.am (needed_libs): Use libcommonstd macro.
+
+	* gpgv.c (main) [W32]: Call pth_init.
+	* gpg.c (main) [W32]: Call pth_init.
+
 2007-06-08  Werner Koch  <wk at g10code.com>
 
 	* Makefile.am (gpg2_LDADD): Syntax fix.

Modified: trunk/g10/Makefile.am
===================================================================
--- trunk/g10/Makefile.am	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/Makefile.am	2007-06-14 17:05:07 UTC (rev 4512)
@@ -29,7 +29,7 @@
 
 AM_CFLAGS = $(LIBGCRYPT_CFLAGS) $(LIBASSUAN_CFLAGS) $(GPG_ERROR_CFLAGS)
 
-needed_libs = ../common/libcommon.a ../jnlib/libjnlib.a ../gl/libgnu.a 
+needed_libs = $(libcommon) ../jnlib/libjnlib.a ../gl/libgnu.a 
 
 bin_PROGRAMS = gpg2 gpgv2
 

Modified: trunk/g10/call-agent.c
===================================================================
--- trunk/g10/call-agent.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/call-agent.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -106,7 +106,7 @@
         }
 
       if (!opt.agent_program || !*opt.agent_program)
-        opt.agent_program = GNUPG_DEFAULT_AGENT;
+        opt.agent_program = gnupg_module_name (GNUPG_MODULE_NAME_AGENT);
       if ( !(pgmname = strrchr (opt.agent_program, '/')))
         pgmname = opt.agent_program;
       else

Modified: trunk/g10/gpg.c
===================================================================
--- trunk/g10/gpg.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/gpg.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1147,7 +1147,7 @@
       if(strchr(path,DIRSEP_C))
 	tmppath=make_filename(path,NULL);
       else
-	tmppath=make_filename(GNUPG_LIBDIR,path,NULL);
+	tmppath=make_filename(gnupg_libdir (),path,NULL);
     }
   else
     tmppath=xstrdup(path);
@@ -1814,6 +1814,9 @@
     gcry_control (GCRYCTL_DISABLE_INTERNAL_LOCKING);
     log_set_prefix ("gpg", 1);
 
+    /* Make sure that our subsystems are ready.  */
+    init_common_subsystems ();
+
     /* Check that the libraries are suitable.  Do it right here because the
        option parsing may need services of the library.  */
     if (!gcry_check_version (NEED_LIBGCRYPT_VERSION) )
@@ -1939,8 +1942,6 @@
     assuan_set_assuan_err_source (GPG_ERR_SOURCE_DEFAULT);
  
 
-    set_native_charset (NULL); /* Try to auto set the character set */
-
     /* Try for a version specific config file first */
     if( default_config )
       {

Modified: trunk/g10/gpgv.c
===================================================================
--- trunk/g10/gpgv.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/gpgv.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -122,8 +122,13 @@
 
     set_strusage (my_strusage);
     log_set_prefix ("gpgv", 1);
+
+    /* Make sure that our subsystems are ready.  */
+    init_common_subsystems ();
+
     gnupg_init_signals (0, NULL);
     i18n_init();
+
     opt.command_fd = -1; /* no command fd */
     opt.pgp2_workarounds = 1;
     opt.keyserver_options.options|=KEYSERVER_AUTO_KEY_RETRIEVE;
@@ -136,8 +141,6 @@
     tty_batchmode(1);
     disable_dotlock();
 
-    set_native_charset (NULL); /* Try to auto set the character set */
-    
     pargs.argc = &argc;
     pargs.argv = &argv;
     pargs.flags=  1;  /* do not remove the args */

Modified: trunk/g10/keyserver.c
===================================================================
--- trunk/g10/keyserver.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/keyserver.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -970,7 +970,7 @@
   byte *line=NULL;
   struct exec_info *spawn;
   const char *scheme;
-  const char *libexecdir = get_libexecdir ();
+  const char *libexecdir = gnupg_libexecdir ();
 
   assert(keyserver);
 
@@ -996,7 +996,7 @@
      After some more thinking about this we came to the conclusion
      that it is better to load the helpers from the directory where
      the program of this process lives.  Fortunately Windows provides
-     a way to retrieve this and our get_libexecdir function has been
+     a way to retrieve this and our gnupg_libexecdir function has been
      modified to return just this.  Setting the exec-path is not
      anymore required.  
        set_exec_path(libexecdir);

Modified: trunk/g10/misc.c
===================================================================
--- trunk/g10/misc.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/misc.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1224,43 +1224,6 @@
 }
 
 
-/* Return the name of the libexec directory.  The name is allocated in
-   a static area on the first use.  This function won't fail. */
-const char *
-get_libexecdir (void)
-{
-#ifdef HAVE_W32_SYSTEM
-  static int got_dir;
-  static char dir[MAX_PATH+5];
-
-  if (!got_dir)
-    {
-      char *p;
-
-      if ( !GetModuleFileName ( NULL, dir, MAX_PATH) )
-        {
-          log_debug ("GetModuleFileName failed: %s\n", w32_strerror (0));
-          *dir = 0;
-        }
-      got_dir = 1;
-      p = strrchr (dir, DIRSEP_C);
-      if (p)
-        *p = 0;
-      else
-        {
-          log_debug ("bad filename `%s' returned for this process\n", dir);
-          *dir = 0; 
-        }
-    }
-
-  if (*dir)
-    return dir;
-  /* Fallback to the hardwired value. */
-#endif /*HAVE_W32_SYSTEM*/
-
-  return GNUPG_LIBEXECDIR;
-}
-
 /* Similar to access(2), but uses PATH to find the file. */
 int
 path_access(const char *file,int mode)

Modified: trunk/g10/openfile.c
===================================================================
--- trunk/g10/openfile.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/g10/openfile.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -330,7 +330,7 @@
 static void
 copy_options_file( const char *destdir )
 {
-    const char *datadir = GNUPG_DATADIR;
+    const char *datadir = gnupg_datadir ();
     char *fname;
     FILE *src, *dst;
     int linefeeds=0;
@@ -407,6 +407,9 @@
 try_make_homedir( const char *fname )
 {
     const char *defhome = GNUPG_DEFAULT_HOMEDIR;
+#ifdef HAVE_W32_SYSTEM
+#warning use a function and not a constant
+#endif
 
     /* Create the directory only if the supplied directory name
      * is the same as the default one.  This way we avoid to create

Modified: trunk/jnlib/ChangeLog
===================================================================
--- trunk/jnlib/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/jnlib/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,7 @@
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* utf8conv.c (jnlib_iconv_open, jnlib_iconv, jnlib_iconv_close): New.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* w32help.h: New.

Modified: trunk/jnlib/utf8conv.c
===================================================================
--- trunk/jnlib/utf8conv.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/jnlib/utf8conv.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -150,6 +150,7 @@
 }
 
 
+
 int
 set_native_charset (const char *newset)
 {
@@ -694,3 +695,49 @@
 {
   return do_utf8_to_native (string, length, delim, use_iconv);
 }
+
+
+
+
+/* Wrapper function for iconv_open, required for W32 as we dlopen that
+   library on that system.  */
+jnlib_iconv_t 
+jnlib_iconv_open (const char *tocode, const char *fromcode)
+{
+#ifdef HAVE_W32_SYSTEM
+  if (load_libiconv ())
+    return (jnlib_iconv_t)(-1);
+#endif /*HAVE_W32_SYSTEM*/      
+
+  return (jnlib_iconv_t)iconv_open (tocode, fromcode);
+}
+
+
+/* Wrapper function for iconv, required for W32 as we dlopen that
+   library on that system.  */
+size_t
+jnlib_iconv (jnlib_iconv_t cd,
+             const char **inbuf, size_t *inbytesleft,
+             char **outbuf, size_t *outbytesleft)
+{
+
+#ifdef HAVE_W32_SYSTEM
+  if (load_libiconv ())
+    return 0;
+#endif /*HAVE_W32_SYSTEM*/      
+
+  return iconv ((iconv_t)cd, inbuf, inbytesleft, outbuf, outbytesleft);
+}
+
+/* Wrapper function for iconv_close, required for W32 as we dlopen that
+   library on that system.  */
+int
+jnlib_iconv_close (jnlib_iconv_t cd)
+{
+#ifdef HAVE_W32_SYSTEM
+  if (load_libiconv ())
+    return 0;
+#endif /*HAVE_W32_SYSTEM*/      
+
+  return iconv_close ((iconv_t)cd);
+}

Modified: trunk/jnlib/utf8conv.h
===================================================================
--- trunk/jnlib/utf8conv.h	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/jnlib/utf8conv.h	2007-06-14 17:05:07 UTC (rev 4512)
@@ -30,4 +30,14 @@
 char *utf8_to_native (const char *string, size_t length, int delim);
 
 
+/* Silly wrappers, required for W32 portability.  */
+typedef void *jnlib_iconv_t;
+
+jnlib_iconv_t jnlib_iconv_open (const char *tocode, const char *fromcode);
+size_t jnlib_iconv (jnlib_iconv_t cd, const char **inbuf, size_t *inbytesleft,
+                    char **outbuf, size_t *outbytesleft);
+int jnlib_iconv_close (jnlib_iconv_t cd);
+
+
+
 #endif /*LIBJNLIB_UTF8CONF_H*/

Modified: trunk/kbx/ChangeLog
===================================================================
--- trunk/kbx/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/kbx/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,7 @@
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* kbxutil.c (main): Replace some calls by init_common_subsystems.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* kbxutil.c (i18n_init): Remove.

Modified: trunk/kbx/kbxutil.c
===================================================================
--- trunk/kbx/kbxutil.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/kbx/kbxutil.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -408,7 +408,10 @@
   set_strusage( my_strusage );
   gcry_control (GCRYCTL_DISABLE_SECMEM);
   log_set_prefix ("kbxutil", 1); 
-  set_native_charset (NULL); 
+
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
+
   i18n_init ();
 
   /* Check that the libraries are suitable.  Do it here because

Modified: trunk/keyserver/ChangeLog
===================================================================
--- trunk/keyserver/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/keyserver/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,9 @@
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* gpgkeys_hkp.c (send_key): Rename eof to r_eof as some Windows
+	header defines such a symbol.
+	(main): Likewise.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* gpgkeys_ldap.c (send_key, send_key_keyserver): Rename eof to

Modified: trunk/keyserver/gpgkeys_hkp.c
===================================================================
--- trunk/keyserver/gpgkeys_hkp.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/keyserver/gpgkeys_hkp.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -93,7 +93,7 @@
 }
 
 int
-send_key(int *eof)
+send_key(int *r_eof)
 {
   CURLcode res;
   char request[MAX_URL+15];
@@ -117,7 +117,7 @@
     {
       /* i.e. eof before the KEY BEGIN was found.  This isn't an
 	 error. */
-      *eof=1;
+      *r_eof=1;
       ret=KEYSERVER_OK;
       goto fail;
     }
@@ -157,7 +157,7 @@
   if(!end)
     {
       fprintf(console,"gpgkeys: no KEY %s END found\n",keyid);
-      *eof=1;
+      *r_eof=1;
       ret=KEYSERVER_KEY_INCOMPLETE;
       goto fail;
     }
@@ -768,16 +768,16 @@
     }
   else if(opt->action==KS_SEND)
     {
-      int eof=0;
+      int myeof=0;
 
       do
 	{
 	  set_timeout(opt->timeout);
 
-	  if(send_key(&eof)!=KEYSERVER_OK)
+	  if(send_key(&myeof)!=KEYSERVER_OK)
 	    failed++;
 	}
-      while(!eof);
+      while(!myeof);
     }
   else if(opt->action==KS_SEARCH)
     {

Modified: trunk/scd/ChangeLog
===================================================================
--- trunk/scd/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/scd/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,15 @@
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* scdaemon.c (main): Replace some calls by init_common_subsystems.
+
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* Makefile.am (scdaemon_LDADD): Use libcommonpth macro.
+
+	* command.c (initialize_module_command): New.
+	* scdaemon.c (main) [W32]: Do not use sigpipe code.
+	(main): Call initialize_module_command.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* app-openpgp.c (do_sign): Fix arithmetic on void*.

Modified: trunk/scd/Makefile.am
===================================================================
--- trunk/scd/Makefile.am	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/scd/Makefile.am	2007-06-14 17:05:07 UTC (rev 4512)
@@ -44,7 +44,7 @@
 	app.c app-common.h app-help.c $(card_apps)
 
 
-scdaemon_LDADD = ../jnlib/libjnlib.a ../common/libcommonpth.a ../gl/libgnu.a \
+scdaemon_LDADD = ../jnlib/libjnlib.a $(libcommonpth) ../gl/libgnu.a \
 	$(LIBGCRYPT_LIBS) $(KSBA_LIBS) $(LIBASSUAN_PTH_LIBS) $(PTH_LIBS) \
 	$(LIBUSB_LIBS) $(GPG_ERROR_LIBS) \
         $(LIBINTL) $(DL_LIBS) $(NETLIBS) $(LIBICONV)

Modified: trunk/scd/apdu.c
===================================================================
--- trunk/scd/apdu.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/scd/apdu.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1337,6 +1337,8 @@
   int err;
   unsigned int dummy_status;
   int sw = SW_HOST_CARD_IO_ERROR;
+  /* Note that we use the constant and not the fucntion because this
+     code won't be be used under Windows.  */
   const char *wrapperpgm = GNUPG_LIBEXECDIR "/gnupg-pcsc-wrapper";
 
   if (access (wrapperpgm, X_OK))

Modified: trunk/scd/command.c
===================================================================
--- trunk/scd/command.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/scd/command.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -124,7 +124,7 @@
 
 /* While doing a reset we need to make sure that the ticker does not
    call scd_update_reader_status_file while we are using it. */
-static pth_mutex_t status_file_update_lock = PTH_MUTEX_INIT;
+static pth_mutex_t status_file_update_lock;
 
 
 /*-- Local prototypes --*/
@@ -132,6 +132,24 @@
 
 
 
+
+/* This function must be called once to initialize this module.  This
+   has to be done before a second thread is spawned.  We can't do the
+   static initialization because Pth emulation code might not be able
+   to do a static init; in particular, it is not possible for W32. */
+void
+initialize_module_command (void)
+{
+  static int initialized;
+
+  if (!initialized)
+    {
+      if (pth_mutex_init (&status_file_update_lock))
+        initialized = 1;
+    }
+}
+
+
 /* Update the CARD_REMOVED element of all sessions using the reader
    given by SLOT to VALUE  */
 static void

Modified: trunk/scd/scdaemon.c
===================================================================
--- trunk/scd/scdaemon.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/scd/scdaemon.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -303,9 +303,10 @@
      when adding any stuff between here and the call to INIT_SECMEM()
      somewhere after the option parsing */
   log_set_prefix ("scdaemon", 1|4); 
-  /* Try to auto set the character set.  */
-  set_native_charset (NULL); 
 
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
+
   i18n_init ();
 
   /* Libgcrypt requires us to register the threading model first.
@@ -522,6 +523,8 @@
       log_debug ("... okay\n");
     }
   
+  initialize_module_command ();
+
   if (gpgconf_list == 2)
     scd_exit (0);
   if (gpgconf_list)
@@ -586,6 +589,7 @@
       pth_attr_t tattr;
       int fd = -1;
 
+#ifndef HAVE_W32_SYSTEM
       {
         struct sigaction sa;
         
@@ -594,6 +598,7 @@
         sa.sa_flags = 0;
         sigaction (SIGPIPE, &sa, NULL);
       }
+#endif
 
       /* If --debug-allow-core-dump has been given we also need to
          switch the working directory to a place where we can actually

Modified: trunk/scd/scdaemon.h
===================================================================
--- trunk/scd/scdaemon.h	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/scd/scdaemon.h	2007-06-14 17:05:07 UTC (rev 4512)
@@ -124,6 +124,7 @@
 const char *scd_get_socket_name (void);
 
 /*-- command.c --*/
+void initialize_module_command (void);
 void scd_command_handler (ctrl_t, int);
 void send_status_info (ctrl_t ctrl, const char *keyword, ...)
      GNUPG_GCC_A_SENTINEL(1);

Modified: trunk/sm/ChangeLog
===================================================================
--- trunk/sm/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,23 @@
+2007-06-14  Werner Koch  <wk at g10code.com>
+
+	* call-agent.c (start_agent): Use gnupg_module_name.
+	* call-dirmngr.c (start_dirmngr): Ditto.
+	* export.c (export_p12): Ditto.
+	* import.c (parse_p12): Ditto.
+	* gpgsm.c (run_protect_tool): Ditto.
+
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* gpgsm.c (main): Replace some calls by init_common_subsystems.
+	(main): Use gnupg_datadir.
+	* qualified.c (read_list): Use gnupg-datadir.
+
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* Makefile.am (common_libs): Use libcommaonstd macr.
+
+	* gpgsm.c (main) [W32]: Call pth_init.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* qualified.c (gpgsm_not_qualified_warning) [!ENABLE_NLS]: Do not

Modified: trunk/sm/Makefile.am
===================================================================
--- trunk/sm/Makefile.am	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/Makefile.am	2007-06-14 17:05:07 UTC (rev 4512)
@@ -54,7 +54,7 @@
 
 
 common_libs = ../jnlib/libjnlib.a ../kbx/libkeybox.a \
-              ../common/libcommon.a ../gl/libgnu.a
+              $(libcommon) ../gl/libgnu.a
 
 gpgsm_LDADD = $(common_libs) \
               $(LIBGCRYPT_LIBS) $(KSBA_LIBS) $(LIBASSUAN_LIBS) \

Modified: trunk/sm/call-agent.c
===================================================================
--- trunk/sm/call-agent.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/call-agent.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -94,6 +94,10 @@
       sockname = make_filename (opt.homedir, "S.gpg-agent", NULL);
       rc = assuan_socket_connect (&ctx, sockname, 0);
       xfree (sockname);
+#ifdef HAVE_W32_SYSTEM
+#      warning Print a warning if connecting is not possible
+      /* and offer to fire up the agent.  */
+#endif
 
       if (rc)
         {
@@ -112,7 +116,7 @@
             }
           
           if (!opt.agent_program || !*opt.agent_program)
-            opt.agent_program = GNUPG_DEFAULT_AGENT;
+            opt.agent_program = gnupg_module_name (GNUPG_MODULE_NAME_AGENT);
           if ( !(pgmname = strrchr (opt.agent_program, '/')))
             pgmname = opt.agent_program;
           else

Modified: trunk/sm/call-dirmngr.c
===================================================================
--- trunk/sm/call-dirmngr.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/call-dirmngr.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -172,7 +172,7 @@
       int i;
 
       if (!opt.dirmngr_program || !*opt.dirmngr_program)
-        opt.dirmngr_program = GNUPG_DEFAULT_DIRMNGR;
+        opt.dirmngr_program = gnupg_module_name (GNUPG_MODULE_NAME_DIRMNGR);
       if ( !(pgmname = strrchr (opt.dirmngr_program, '/')))
         pgmname = opt.dirmngr_program;
       else

Modified: trunk/sm/export.c
===================================================================
--- trunk/sm/export.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/export.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -604,7 +604,7 @@
   int bad_pass = 0;
 
   if (!opt.protect_tool_program || !*opt.protect_tool_program)
-    pgmname = GNUPG_DEFAULT_PROTECT_TOOL;
+    pgmname = gnupg_module_name (GNUPG_MODULE_NAME_PROTECT_TOOL);
   else
     pgmname = opt.protect_tool_program;
 

Modified: trunk/sm/gpgsm.c
===================================================================
--- trunk/sm/gpgsm.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/gpgsm.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -747,8 +747,8 @@
      somewhere after the option parsing */
   log_set_prefix ("gpgsm", 1);
 
-  /* Try to auto set the character set.  */
-  set_native_charset (NULL); 
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
 
   /* Check that the libraries are suitable.  Do it here because the
      option parse may need services of the library */
@@ -1310,7 +1310,7 @@
           /* Import the standard certificates for a new default keybox. */
           char *filelist[2];
           
-          filelist[0] = make_filename (GNUPG_DATADIR, "com-certs.pem", NULL);
+          filelist[0] = make_filename (gnupg_datadir (),"com-certs.pem", NULL);
           filelist[1] = NULL;
           if (!access (filelist[0], F_OK))
             {
@@ -1853,7 +1853,7 @@
   int i;
 
   if (!opt.protect_tool_program || !*opt.protect_tool_program)
-    pgm = GNUPG_DEFAULT_PROTECT_TOOL;
+    pgm = gnupg_module_name (GNUPG_MODULE_NAME_PROTECT_TOOL);
   else
     pgm = opt.protect_tool_program;
 

Modified: trunk/sm/import.c
===================================================================
--- trunk/sm/import.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/import.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -509,7 +509,7 @@
   int bad_pass = 0;
 
   if (!opt.protect_tool_program || !*opt.protect_tool_program)
-    pgmname = GNUPG_DEFAULT_PROTECT_TOOL;
+    pgmname = gnupg_module_name (GNUPG_MODULE_NAME_PROTECT_TOOL);
   else
     pgmname = opt.protect_tool_program;
 

Modified: trunk/sm/qualified.c
===================================================================
--- trunk/sm/qualified.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/sm/qualified.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -66,7 +66,7 @@
 
   if (!listname)
     {
-      listname = make_filename (GNUPG_DATADIR, "qualified.txt", NULL);
+      listname = make_filename (gnupg_datadir (), "qualified.txt", NULL);
       listfp = fopen (listname, "r");
       if (!listfp && errno != ENOENT)
         {

Modified: trunk/tools/ChangeLog
===================================================================
--- trunk/tools/ChangeLog	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/tools/ChangeLog	2007-06-14 17:05:07 UTC (rev 4512)
@@ -1,3 +1,25 @@
+2007-06-14  Werner Koch  <wk at g10code.com>
+
+	* symcryptrun.c (main): Setup default socket name for
+	simple-pwquery.
+	(MAP_SPWQ_ERROR_IMPL): New.  Use it for all spwq error returns.
+
+2007-06-12  Werner Koch  <wk at g10code.com>
+
+	* gpgconf-comp.c (gc_process_gpgconf_conf): Replace
+	GNUPG_SYSCONFDIR by a function call.
+
+	* gpg-connect-agent.c (main): Replace some calls by
+	init_common_subsystems.
+	* gpgconf.c (main): Ditto.
+	* symcryptrun.c (main): Ditto.
+
+2007-06-11  Werner Koch  <wk at g10code.com>
+
+	* symcryptrun.c (main) [W32]: Call pth_init.
+	* gpgconf.c (main) [W32]: Call pth_init
+	* gpg-connect-agent.c (main) [W32]: Call pth_init.
+
 2007-06-06  Werner Koch  <wk at g10code.com>
 
 	* Makefile.am (bin_PROGRAMS) [W32]: Do not build gpgparsemail.

Modified: trunk/tools/Makefile.am
===================================================================
--- trunk/tools/Makefile.am	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/tools/Makefile.am	2007-06-14 17:05:07 UTC (rev 4512)
@@ -50,17 +50,19 @@
 
 noinst_PROGRAMS = clean-sat mk-tdata make-dns-cert gpgsplit
 
-common_libs = ../jnlib/libjnlib.a ../common/libcommon.a ../gl/libgnu.a
+common_libs = ../jnlib/libjnlib.a $(libcommon) ../gl/libgnu.a
 pwquery_libs = ../common/libsimple-pwquery.a
 
-gpgsplit_LDADD = $(common_libs) $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) \
+gpgsplit_LDADD = $(common_libs) \
+	         $(LIBGCRYPT_LIBS) $(GPG_ERROR_LIBS) \
 		 $(ZLIBS) $(LIBINTL) $(LIBICONV)
 
 gpgconf_SOURCES = gpgconf.c gpgconf.h gpgconf-comp.c no-libgcrypt.c
 
 # jnlib/common sucks in gpg-error, will they, nil they (some compilers
 # do not eliminate the supposed-to-be-unused-inline-functions).
-gpgconf_LDADD = $(common_libs) $(LIBINTL) $(GPG_ERROR_LIBS) $(LIBICONV)
+gpgconf_LDADD = $(common_libs) \
+                $(LIBINTL) $(GPG_ERROR_LIBS) $(LIBICONV)
 
 gpgparsemail_SOURCES = gpgparsemail.c rfc822parse.c rfc822parse.h
 gpgparsemail_LDADD = 
@@ -73,7 +75,8 @@
 watchgnupg_LDADD = $(NETLIBS)
 
 gpg_connect_agent_SOURCES = gpg-connect-agent.c no-libgcrypt.c
-gpg_connect_agent_LDADD = $(common_libs) $(LIBASSUAN_LIBS) \
+# FIXME: remove PTH_LIBS (why do we need them at all?)
+gpg_connect_agent_LDADD = $(common_libs) $(LIBASSUAN_LIBS) $(PTH_LIBS) \
 	                  $(GPG_ERROR_LIBS) $(LIBINTL) $(NETLIBS) $(LIBICONV)
 
 gpgkey2ssh_SOURCES = gpgkey2ssh.c

Modified: trunk/tools/gpg-connect-agent.c
===================================================================
--- trunk/tools/gpg-connect-agent.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/tools/gpg-connect-agent.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -274,6 +274,10 @@
 
   set_strusage (my_strusage);
   log_set_prefix ("gpg-connect-agent", 1);
+
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
+
   assuan_set_assuan_err_source (0);
 
   i18n_init();

Modified: trunk/tools/gpgconf-comp.c
===================================================================
--- trunk/tools/gpgconf-comp.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/tools/gpgconf-comp.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -2676,9 +2676,13 @@
   int runtime[GC_BACKEND_NR];
   int used_components[GC_COMPONENT_NR];
   int backend_id, component_id;
+  char *fname_buffer = NULL;
 
   if (!fname)
-    fname = GNUPG_SYSCONFDIR "/gpgconf.conf";
+    {
+      fname_buffer = make_filename (gnupg_sysconfdir (), "gpgconf.conf", NULL);
+      fname = fname_buffer;
+    }
 
   for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
     runtime[backend_id] = 0;
@@ -2695,6 +2699,7 @@
           gc_error (0, errno, "can not open global config file `%s'", fname);
           result = -1;
         }
+      xfree (fname_buffer);
       return result;
     }
 
@@ -2931,5 +2936,6 @@
         }
     }
 
+  xfree (fname_buffer);
   return result;
 }

Modified: trunk/tools/gpgconf.c
===================================================================
--- trunk/tools/gpgconf.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/tools/gpgconf.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -116,6 +116,9 @@
   set_strusage (my_strusage);
   log_set_prefix ("gpgconf", 1);
 
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
+
   i18n_init();
 
   /* Parse the command line. */

Modified: trunk/tools/symcryptrun.c
===================================================================
--- trunk/tools/symcryptrun.c	2007-06-13 15:28:11 UTC (rev 4511)
+++ trunk/tools/symcryptrun.c	2007-06-14 17:05:07 UTC (rev 4512)
@@ -238,6 +238,8 @@
        __result; }))
 #endif
 
+/* Include the implementation of map_spwq_error.  */
+MAP_SPWQ_ERROR_IMPL
 
 /* Unlink a file, and shred it if SHRED is true.  */
 int
@@ -455,6 +457,7 @@
   pw = simple_pwquery (cacheid,
                        again ? _("does not match - try again"):NULL,
                        _("Passphrase:"), NULL, 0, &err);
+  err = map_spwq_error (err);
 
 #ifdef ENABLE_NLS
   if (orig_codeset)
@@ -911,8 +914,8 @@
   set_strusage (my_strusage);
   log_set_prefix ("symcryptrun", 1);
 
-  /* Try to auto set the character set.  */
-  set_native_charset (NULL); 
+  /* Make sure that our subsystems are ready.  */
+  init_common_subsystems ();
 
   i18n_init();
 
@@ -1028,13 +1031,22 @@
   setup_libgcrypt_logging ();
   gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
 
+  /* Tell simple-pwquery about the the standard socket name.  */
+  {
+    char *tmp = make_filename (opt.homedir, "S.gpg-agent", NULL);
+    simple_pw_set_socket (tmp);
+    xfree (tmp);
+  }
+
   if (!opt.class)
     {
       log_error (_("no class provided\n"));
       res = 1;
     }
   else if (!strcmp (opt.class, "confucius"))
-    res = confucius_main (mode, argc, argv);
+    {
+      res = confucius_main (mode, argc, argv);
+    }
   else
     {
       log_error (_("class %s is not supported\n"), opt.class);




More information about the Gnupg-commits mailing list