[svn] dirmngr - r257 - trunk/src
svn author marcus
cvs at cvs.gnupg.org
Fri Aug 3 10:08:35 CEST 2007
Author: marcus
Date: 2007-08-03 10:08:07 +0200 (Fri, 03 Aug 2007)
New Revision: 257
Added:
trunk/src/get-path.c
Modified:
trunk/src/ChangeLog
Log:
2007-08-03 Marcus Brinkmann <marcus at g10code.de>
* get-path.c: Really add the file this time.
Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog 2007-07-30 22:19:44 UTC (rev 256)
+++ trunk/src/ChangeLog 2007-08-03 08:08:07 UTC (rev 257)
@@ -1,3 +1,7 @@
+2007-08-03 Marcus Brinkmann <marcus at g10code.de>
+
+ * get-path.c: Really add the file this time.
+
2007-07-31 Marcus Brinkmann <marcus at g10code.de>
* crlfetch.c: Include "estream.h".
Added: trunk/src/get-path.c
===================================================================
--- trunk/src/get-path.c 2007-07-30 22:19:44 UTC (rev 256)
+++ trunk/src/get-path.c 2007-08-03 08:08:07 UTC (rev 257)
@@ -0,0 +1,366 @@
+/* get-path.c - Utility functions for the W32 API
+ Copyright (C) 1999 Free Software Foundation, Inc
+ Copyright (C) 2001 Werner Koch (dd9jn)
+ Copyright (C) 2001, 2002, 2003, 2004, 2007 g10 Code GmbH
+
+ This file is part of DirMngr.
+
+ DirMngr 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.
+
+ DirMngr 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., 59 Temple Place - Suite 330, Boston, MA
+ 02111-1307, USA */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <signal.h>
+#include <fcntl.h>
+#if HAVE_W32_SYSTEM
+#include <windows.h>
+#include <shlobj.h>
+#include <io.h>
+#endif
+
+#include "util.h"
+
+#ifdef HAVE_W32_SYSTEM
+#define GNUPG_DEFAULT_HOMEDIR "c:/gnupg"
+#elif defined(__VMS)
+#define GNUPG_DEFAULT_HOMEDIR "/SYS\$LOGIN/gnupg"
+#else
+#define GNUPG_DEFAULT_HOMEDIR "~/.gnupg"
+#endif
+
+
+#if HAVE_W32_SYSTEM
+#define RTLD_LAZY 0
+
+static __inline__ void *
+dlopen (const char * name, int flag)
+{
+ void * hd = LoadLibrary (name);
+ return hd;
+}
+
+static __inline__ void *
+dlsym (void * hd, const char * sym)
+{
+ if (hd && sym)
+ {
+ void * fnc = GetProcAddress (hd, sym);
+ if (!fnc)
+ return NULL;
+ return fnc;
+ }
+ return NULL;
+}
+
+static __inline__ int
+dlclose (void * hd)
+{
+ if (hd)
+ {
+ FreeLibrary (hd);
+ return 0;
+ }
+ return -1;
+}
+
+
+/* Return a string from the W32 Registry or NULL in case of error.
+ Caller must release the return value. A NULL for root is an alias
+ for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */
+static char *
+read_w32_registry_string (const char *root, const char *dir, const char *name)
+{
+ HKEY root_key, key_handle;
+ DWORD n1, nbytes, type;
+ char *result = NULL;
+
+ if ( !root )
+ root_key = HKEY_CURRENT_USER;
+ else if ( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
+ root_key = HKEY_CLASSES_ROOT;
+ else if ( !strcmp( root, "HKEY_CURRENT_USER" ) )
+ root_key = HKEY_CURRENT_USER;
+ else if ( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
+ root_key = HKEY_LOCAL_MACHINE;
+ else if ( !strcmp( root, "HKEY_USERS" ) )
+ root_key = HKEY_USERS;
+ else if ( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
+ root_key = HKEY_PERFORMANCE_DATA;
+ else if ( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
+ root_key = HKEY_CURRENT_CONFIG;
+ else
+ return NULL;
+
+ if ( RegOpenKeyEx ( root_key, dir, 0, KEY_READ, &key_handle ) )
+ {
+ if (root)
+ return NULL; /* no need for a RegClose, so return direct */
+ /* It seems to be common practise to fall back to HKLM. */
+ if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
+ return NULL; /* still no need for a RegClose, so return direct */
+ }
+
+ nbytes = 1;
+ if ( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
+ {
+ if (root)
+ goto leave;
+ /* Try to fallback to HKLM also vor a missing value. */
+ RegCloseKey (key_handle);
+ if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
+ return NULL; /* Nope. */
+ if (RegQueryValueEx ( key_handle, name, 0, NULL, NULL, &nbytes))
+ goto leave;
+ }
+ result = malloc ( (n1=nbytes+1) );
+ if ( !result )
+ goto leave;
+ if ( RegQueryValueEx ( key_handle, name, 0, &type, result, &n1 ) )
+ {
+ free(result); result = NULL;
+ goto leave;
+ }
+ result[nbytes] = 0; /* Make sure it is really a string. */
+ if (type == REG_EXPAND_SZ && strchr (result, '%'))
+ {
+ char *tmp;
+
+ n1 += 1000;
+ tmp = malloc (n1+1);
+ if (!tmp)
+ goto leave;
+ nbytes = ExpandEnvironmentStrings (result, tmp, n1);
+ if (nbytes && nbytes > n1)
+ {
+ free (tmp);
+ n1 = nbytes;
+ tmp = malloc (n1 + 1);
+ if (!tmp)
+ goto leave;
+ nbytes = ExpandEnvironmentStrings (result, tmp, n1);
+ if (nbytes && nbytes > n1) {
+ free (tmp); /* Oops - truncated, better don't expand at all. */
+ goto leave;
+ }
+ tmp[nbytes] = 0;
+ free (result);
+ result = tmp;
+ }
+ else if (nbytes) /* Okay, reduce the length. */
+ {
+ tmp[nbytes] = 0;
+ free (result);
+ result = malloc (strlen (tmp)+1);
+ if (!result)
+ result = tmp;
+ else
+ {
+ strcpy (result, tmp);
+ free (tmp);
+ }
+ }
+ else /* Error - don't expand. */
+ {
+ free (tmp);
+ }
+ }
+
+ leave:
+ RegCloseKey( key_handle );
+ return result;
+}
+
+
+/* This is a helper function to load and run a Windows function from
+ either of one DLLs. */
+static HRESULT
+w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
+{
+ static int initialized;
+ static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
+
+ if (!initialized)
+ {
+ static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
+ void *handle;
+ int i;
+
+ initialized = 1;
+
+ for (i=0, handle = NULL; !handle && dllnames[i]; i++)
+ {
+ handle = dlopen (dllnames[i], RTLD_LAZY);
+ if (handle)
+ {
+ func = dlsym (handle, "SHGetFolderPathA");
+ if (!func)
+ {
+ dlclose (handle);
+ handle = NULL;
+ }
+ }
+ }
+ }
+
+ if (func)
+ return func (a,b,c,d,e);
+ else
+ return -1;
+}
+
+static char *
+find_program_at_standard_place (const char *name)
+{
+ char path[MAX_PATH];
+ char *result = NULL;
+
+ if (w32_shgetfolderpath (NULL, CSIDL_PROGRAM_FILES, NULL, 0, path) >= 0)
+ {
+ result = malloc (strlen (path) + 1 + strlen (name) + 1);
+ if (result)
+ {
+ strcpy (stpcpy (stpcpy (result, path), "\\"), name);
+ if (access (result, F_OK))
+ {
+ free (result);
+ result = NULL;
+ }
+ }
+ }
+ return result;
+}
+#endif
+
+
+const char *
+get_dirmngr_ldap_path (void)
+{
+ static char *pgmname;
+
+#ifdef HAVE_W32_SYSTEM
+ if (!pgmname)
+ pgmname = find_program_at_standard_place ("GNU\\GnuPG\\dirmngr_ldap.exe");
+#endif
+ if (!pgmname)
+ pgmname = DIRMNGR_LIBEXECDIR "/dirmngr_ldap";
+ return pgmname;
+}
+
+
+
+/* Home directory. */
+
+#ifdef HAVE_W32_SYSTEM
+#ifndef CSIDL_APPDATA
+#define CSIDL_APPDATA 0x001a
+#endif
+#ifndef CSIDL_LOCAL_APPDATA
+#define CSIDL_LOCAL_APPDATA 0x001c
+#endif
+#ifndef CSIDL_FLAG_CREATE
+#define CSIDL_FLAG_CREATE 0x8000
+#endif
+#endif /*HAVE_W32_SYSTEM*/
+
+/* Get the standard home directory. In general this function should
+ not be used as it does not consider a registry value (under W32) or
+ the GNUPGHOME encironment variable. It is better to use
+ default_homedir(). */
+const char *
+standard_homedir (void)
+{
+#ifdef HAVE_W32_SYSTEM
+ static const char *dir;
+
+ if (!dir)
+ {
+ char path[MAX_PATH];
+
+ /* It might be better to use LOCAL_APPDATA because this is
+ defined as "non roaming" and thus more likely to be kept
+ locally. For private keys this is desired. However, given
+ that many users copy private keys anyway forth and back,
+ using a system roaming services might be better than to let
+ them do it manually. A security conscious user will anyway
+ use the registry entry to have better control. */
+ if (w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
+ NULL, 0, path) >= 0)
+ {
+ char *tmp = xmalloc (strlen (path) + 6 +1);
+ strcpy (stpcpy (tmp, path), "\\gnupg");
+ dir = tmp;
+
+ /* Try to create the directory if it does not yet exists. */
+ if (access (dir, F_OK))
+ CreateDirectory (dir, NULL);
+ }
+ else
+ dir = GNUPG_DEFAULT_HOMEDIR;
+ }
+ return dir;
+#else/*!HAVE_W32_SYSTEM*/
+ return GNUPG_DEFAULT_HOMEDIR;
+#endif /*!HAVE_W32_SYSTEM*/
+}
+
+/* Set up the default home directory. The usual --homedir option
+ should be parsed later. */
+const char *
+default_homedir (void)
+{
+ const char *dir;
+
+ dir = getenv ("GNUPGHOME");
+#ifdef HAVE_W32_SYSTEM
+ if (!dir || !*dir)
+ {
+ static const char *saved_dir;
+
+ if (!saved_dir)
+ {
+ if (!dir || !*dir)
+ {
+ char *tmp;
+
+ tmp = read_w32_registry_string (NULL, "Software\\GNU\\GnuPG",
+ "HomeDir");
+ if (tmp && *tmp)
+ {
+ xfree (tmp);
+ tmp = NULL;
+ }
+ if (tmp)
+ saved_dir = tmp;
+ }
+
+ if (!saved_dir)
+ saved_dir = standard_homedir ();
+ }
+ dir = saved_dir;
+ }
+#endif /*HAVE_W32_SYSTEM*/
+ if (!dir || !*dir)
+ dir = GNUPG_DEFAULT_HOMEDIR;
+
+ return dir;
+}
More information about the Gnupg-commits
mailing list