get-path.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* agent.c - Talking to gpg-agent.
  2. Copyright (C) 2008 g10 Code GmbH
  3. This file is part of Scute.
  4. Scute is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Scute is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Scute; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. In addition, as a special exception, g10 Code GmbH gives permission
  16. to link this library: with the Mozilla Foundation's code for
  17. Mozilla (or with modified versions of it that use the same license
  18. as the "Mozilla" code), and distribute the linked executables. You
  19. must obey the GNU General Public License in all respects for all of
  20. the code used other than "Mozilla". If you modify this file, you
  21. may extend this exception to your version of the file, but you are
  22. not obligated to do so. If you do not wish to do so, delete this
  23. exception statement from your version. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #include <errno.h>
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #include <signal.h>
  35. #include <fcntl.h>
  36. #include <stdarg.h>
  37. #ifdef HAVE_W32_SYSTEM
  38. #include <windows.h>
  39. #include <shlobj.h>
  40. #include <io.h>
  41. #endif
  42. #include "support.h"
  43. #ifdef HAVE_W32_SYSTEM
  44. #define GNUPG_DEFAULT_HOMEDIR "c:/gnupg"
  45. #elif defined(__VMS)
  46. #define GNUPG_DEFAULT_HOMEDIR "/SYS\$LOGIN/gnupg"
  47. #else
  48. #define GNUPG_DEFAULT_HOMEDIR "~/.gnupg"
  49. #endif
  50. #ifdef HAVE_DOSISH_SYSTEM
  51. #define DIRSEP_C '\\'
  52. #define DIRSEP_S "\\"
  53. #else
  54. #define DIRSEP_C '/'
  55. #define DIRSEP_S "/"
  56. #endif
  57. #ifdef HAVE_W32_SYSTEM
  58. #define RTLD_LAZY 0
  59. static __inline__ void *
  60. dlopen (const char * name, int flag)
  61. {
  62. void * hd = LoadLibrary (name);
  63. return hd;
  64. }
  65. static __inline__ void *
  66. dlsym (void * hd, const char * sym)
  67. {
  68. if (hd && sym)
  69. {
  70. void * fnc = GetProcAddress (hd, sym);
  71. if (!fnc)
  72. return NULL;
  73. return fnc;
  74. }
  75. return NULL;
  76. }
  77. static __inline__ int
  78. dlclose (void * hd)
  79. {
  80. if (hd)
  81. {
  82. FreeLibrary (hd);
  83. return 0;
  84. }
  85. return -1;
  86. }
  87. /* Return a string from the W32 Registry or NULL in case of error.
  88. Caller must release the return value. A NULL for root is an alias
  89. for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */
  90. static char *
  91. read_w32_registry_string (const char *root, const char *dir, const char *name)
  92. {
  93. HKEY root_key, key_handle;
  94. DWORD n1, nbytes, type;
  95. char *result = NULL;
  96. if ( !root )
  97. root_key = HKEY_CURRENT_USER;
  98. else if ( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
  99. root_key = HKEY_CLASSES_ROOT;
  100. else if ( !strcmp( root, "HKEY_CURRENT_USER" ) )
  101. root_key = HKEY_CURRENT_USER;
  102. else if ( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
  103. root_key = HKEY_LOCAL_MACHINE;
  104. else if ( !strcmp( root, "HKEY_USERS" ) )
  105. root_key = HKEY_USERS;
  106. else if ( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
  107. root_key = HKEY_PERFORMANCE_DATA;
  108. else if ( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
  109. root_key = HKEY_CURRENT_CONFIG;
  110. else
  111. return NULL;
  112. if ( RegOpenKeyEx ( root_key, dir, 0, KEY_READ, &key_handle ) )
  113. {
  114. if (root)
  115. return NULL; /* no need for a RegClose, so return direct */
  116. /* It seems to be common practise to fall back to HKLM. */
  117. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
  118. return NULL; /* still no need for a RegClose, so return direct */
  119. }
  120. nbytes = 1;
  121. if ( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
  122. {
  123. if (root)
  124. goto leave;
  125. /* Try to fallback to HKLM also vor a missing value. */
  126. RegCloseKey (key_handle);
  127. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
  128. return NULL; /* Nope. */
  129. if (RegQueryValueEx ( key_handle, name, 0, NULL, NULL, &nbytes))
  130. goto leave;
  131. }
  132. result = malloc ( (n1=nbytes+1) );
  133. if ( !result )
  134. goto leave;
  135. if ( RegQueryValueEx ( key_handle, name, 0, &type, result, &n1 ) )
  136. {
  137. free(result); result = NULL;
  138. goto leave;
  139. }
  140. result[nbytes] = 0; /* Make sure it is really a string. */
  141. if (type == REG_EXPAND_SZ && strchr (result, '%'))
  142. {
  143. char *tmp;
  144. n1 += 1000;
  145. tmp = malloc (n1+1);
  146. if (!tmp)
  147. goto leave;
  148. nbytes = ExpandEnvironmentStrings (result, tmp, n1);
  149. if (nbytes && nbytes > n1)
  150. {
  151. free (tmp);
  152. n1 = nbytes;
  153. tmp = malloc (n1 + 1);
  154. if (!tmp)
  155. goto leave;
  156. nbytes = ExpandEnvironmentStrings (result, tmp, n1);
  157. if (nbytes && nbytes > n1) {
  158. free (tmp); /* Oops - truncated, better don't expand at all. */
  159. goto leave;
  160. }
  161. tmp[nbytes] = 0;
  162. free (result);
  163. result = tmp;
  164. }
  165. else if (nbytes) /* Okay, reduce the length. */
  166. {
  167. tmp[nbytes] = 0;
  168. free (result);
  169. result = malloc (strlen (tmp)+1);
  170. if (!result)
  171. result = tmp;
  172. else
  173. {
  174. strcpy (result, tmp);
  175. free (tmp);
  176. }
  177. }
  178. else /* Error - don't expand. */
  179. {
  180. free (tmp);
  181. }
  182. }
  183. leave:
  184. RegCloseKey( key_handle );
  185. return result;
  186. }
  187. /* This is a helper function to load and run a Windows function from
  188. either of one DLLs. */
  189. static HRESULT
  190. w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e)
  191. {
  192. static int initialized;
  193. static HRESULT (WINAPI * func)(HWND,int,HANDLE,DWORD,LPSTR);
  194. if (!initialized)
  195. {
  196. static char *dllnames[] = { "shell32.dll", "shfolder.dll", NULL };
  197. void *handle;
  198. int i;
  199. initialized = 1;
  200. for (i=0, handle = NULL; !handle && dllnames[i]; i++)
  201. {
  202. handle = dlopen (dllnames[i], RTLD_LAZY);
  203. if (handle)
  204. {
  205. func = dlsym (handle, "SHGetFolderPathA");
  206. if (!func)
  207. {
  208. dlclose (handle);
  209. handle = NULL;
  210. }
  211. }
  212. }
  213. }
  214. if (func)
  215. return func (a,b,c,d,e);
  216. else
  217. return -1;
  218. }
  219. static char *
  220. find_program_in_inst_dir (const char *name)
  221. {
  222. char *result = NULL;
  223. char *tmp;
  224. tmp = read_w32_registry_string ("HKEY_LOCAL_MACHINE",
  225. "Software\\GNU\\GnuPG",
  226. "Install Directory");
  227. if (!tmp)
  228. return NULL;
  229. result = malloc (strlen (tmp) + 1 + strlen (name) + 1);
  230. if (!result)
  231. {
  232. free (tmp);
  233. return NULL;
  234. }
  235. strcpy (stpcpy (stpcpy (result, tmp), "\\"), name);
  236. free (tmp);
  237. if (access (result, F_OK))
  238. {
  239. free (result);
  240. return NULL;
  241. }
  242. return result;
  243. }
  244. static char *
  245. find_program_at_standard_place (const char *name)
  246. {
  247. char path[MAX_PATH];
  248. char *result = NULL;
  249. if (w32_shgetfolderpath (NULL, CSIDL_PROGRAM_FILES, NULL, 0, path) >= 0)
  250. {
  251. result = malloc (strlen (path) + 1 + strlen (name) + 1);
  252. if (result)
  253. {
  254. strcpy (stpcpy (stpcpy (result, path), "\\"), name);
  255. if (access (result, F_OK))
  256. {
  257. free (result);
  258. result = NULL;
  259. }
  260. }
  261. }
  262. return result;
  263. }
  264. #endif
  265. const char *
  266. get_gpgsm_path (void)
  267. {
  268. static const char *pgmname;
  269. #ifdef HAVE_W32_SYSTEM
  270. if (!pgmname)
  271. pgmname = find_program_in_inst_dir ("gpgsm.exe");
  272. if (!pgmname)
  273. pgmname = find_program_at_standard_place ("GNU\\GnuPG\\gpgsm.exe");
  274. #endif
  275. if (!pgmname)
  276. pgmname = GPGSM_PATH;
  277. return pgmname;
  278. }
  279. const char *
  280. get_gpg_agent_path (void)
  281. {
  282. static const char *pgmname;
  283. #ifdef HAVE_W32_SYSTEM
  284. if (!pgmname)
  285. pgmname = find_program_in_inst_dir ("gpg-agent.exe");
  286. if (!pgmname)
  287. pgmname = find_program_at_standard_place ("GNU\\GnuPG\\gpg-agent.exe");
  288. #endif
  289. if (!pgmname)
  290. pgmname = GPG_AGENT_PATH;
  291. return pgmname;
  292. }
  293. /* Home directory. */
  294. #ifdef HAVE_W32_SYSTEM
  295. #ifndef CSIDL_APPDATA
  296. #define CSIDL_APPDATA 0x001a
  297. #endif
  298. #ifndef CSIDL_LOCAL_APPDATA
  299. #define CSIDL_LOCAL_APPDATA 0x001c
  300. #endif
  301. #ifndef CSIDL_COMMON_APPDATA
  302. #define CSIDL_COMMON_APPDATA 0x0023
  303. #endif
  304. #ifndef CSIDL_FLAG_CREATE
  305. #define CSIDL_FLAG_CREATE 0x8000
  306. #endif
  307. #endif /*HAVE_W32_SYSTEM*/
  308. /* Get the standard home directory. In general this function should
  309. not be used as it does not consider a registry value (under W32) or
  310. the GNUPGHOME environment variable. It is better to use
  311. default_homedir(). */
  312. const char *
  313. standard_homedir (void)
  314. {
  315. #ifdef HAVE_W32_SYSTEM
  316. static const char *dir;
  317. if (!dir)
  318. {
  319. char path[MAX_PATH];
  320. /* It might be better to use LOCAL_APPDATA because this is
  321. defined as "non roaming" and thus more likely to be kept
  322. locally. For private keys this is desired. However, given
  323. that many users copy private keys anyway forth and back,
  324. using a system roaming services might be better than to let
  325. them do it manually. A security conscious user will anyway
  326. use the registry entry to have better control. */
  327. if (w32_shgetfolderpath (NULL, CSIDL_APPDATA|CSIDL_FLAG_CREATE,
  328. NULL, 0, path) >= 0)
  329. {
  330. char *tmp = malloc (strlen (path) + 6 +1);
  331. if (tmp)
  332. {
  333. strcpy (stpcpy (tmp, path), "\\gnupg");
  334. dir = tmp;
  335. /* Try to create the directory if it does not yet exists. */
  336. if (access (dir, F_OK))
  337. CreateDirectory (dir, NULL);
  338. }
  339. }
  340. if (!dir)
  341. dir = GNUPG_DEFAULT_HOMEDIR;
  342. }
  343. return dir;
  344. #else/*!HAVE_W32_SYSTEM*/
  345. return GNUPG_DEFAULT_HOMEDIR;
  346. #endif /*!HAVE_W32_SYSTEM*/
  347. }
  348. /* Set up the default home directory. The usual --homedir option
  349. should be parsed later. */
  350. const char *
  351. default_homedir (void)
  352. {
  353. const char *dir;
  354. dir = getenv ("GNUPGHOME");
  355. #ifdef HAVE_W32_SYSTEM
  356. if (!dir || !*dir)
  357. {
  358. static const char *saved_dir;
  359. if (!saved_dir)
  360. {
  361. if (!dir || !*dir)
  362. {
  363. char *tmp;
  364. tmp = read_w32_registry_string (NULL, "Software\\GNU\\GnuPG",
  365. "HomeDir");
  366. if (tmp && *tmp)
  367. {
  368. free (tmp);
  369. tmp = NULL;
  370. }
  371. if (tmp)
  372. saved_dir = tmp;
  373. }
  374. if (!saved_dir)
  375. saved_dir = standard_homedir ();
  376. }
  377. dir = saved_dir;
  378. }
  379. #endif /*HAVE_W32_SYSTEM*/
  380. if (!dir || !*dir)
  381. dir = GNUPG_DEFAULT_HOMEDIR;
  382. return dir;
  383. }
  384. /* Construct a filename from the NULL terminated list of parts. Tilde
  385. expansion is done here. */
  386. char *
  387. make_filename (const char *first_part, ...)
  388. {
  389. va_list arg_ptr;
  390. size_t n;
  391. const char *s;
  392. char *name;
  393. char *home;
  394. char *p;
  395. va_start (arg_ptr, first_part);
  396. n = strlen (first_part) + 1;
  397. while ((s = va_arg (arg_ptr, const char *)))
  398. n += strlen (s) + 1;
  399. va_end (arg_ptr);
  400. home = NULL;
  401. if (*first_part == '~' && first_part[1] == '/'
  402. && (home = getenv("HOME")) && *home)
  403. n += strlen (home);
  404. name = malloc (n);
  405. if (! name)
  406. return NULL;
  407. p = (home
  408. ? stpcpy (stpcpy (name,home), first_part + 1)
  409. : stpcpy (name, first_part));
  410. va_start (arg_ptr, first_part);
  411. while ((s = va_arg(arg_ptr, const char *)))
  412. p = stpcpy (stpcpy (p,"/"), s);
  413. va_end (arg_ptr);
  414. #ifdef HAVE_W32_SYSTEM
  415. /* We better avoid mixing slashes and backslashes and prefer
  416. backslashes. There is usual no problem with mixing them, however
  417. a very few W32 API calls can't grok plain slashes. Printing
  418. filenames with mixed slashes also looks a bit strange. */
  419. if (strchr (name, '\\'))
  420. {
  421. for (p = name; *p; p++)
  422. if (*p == '/')
  423. *p = '\\';
  424. }
  425. #endif
  426. return name;
  427. }