1 |
/* w32-util.c - Utility functions for the W32 API |
2 |
* Copyright (C) 1999 Free Software Foundation, Inc |
3 |
* Copyright (C) 2001 Werner Koch (dd9jn) |
4 |
* Copyright (C) 2002, 2005 Timo Schulz |
5 |
* |
6 |
* This file is part of MyGPGME. |
7 |
* |
8 |
* MyGPGME is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* MyGPGME is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
21 |
*/ |
22 |
|
23 |
#include <stdio.h> |
24 |
#include <stdlib.h> |
25 |
#include <string.h> |
26 |
#include <assert.h> |
27 |
#include <errno.h> |
28 |
#include <sys/types.h> |
29 |
#include <windows.h> |
30 |
#include <process.h> |
31 |
#include <io.h> |
32 |
|
33 |
#include "util.h" |
34 |
|
35 |
/**************** |
36 |
* Return a string from the Win32 Registry or NULL in case of |
37 |
* error. Caller must release the return value. A NULL for root |
38 |
* is an alias fro HKEY_CURRENT_USER |
39 |
*/ |
40 |
static char * |
41 |
read_w32_registry_string ( const char *root, |
42 |
const char *dir, const char *name ) |
43 |
{ |
44 |
HKEY root_key, key_handle; |
45 |
DWORD n1, nbytes; |
46 |
char *result = NULL; |
47 |
|
48 |
if( !root ) |
49 |
root_key = HKEY_CURRENT_USER; |
50 |
else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) ) |
51 |
root_key = HKEY_CLASSES_ROOT; |
52 |
else if( !strcmp( root, "HKEY_CURRENT_USER" ) ) |
53 |
root_key = HKEY_CURRENT_USER; |
54 |
else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) ) |
55 |
root_key = HKEY_LOCAL_MACHINE; |
56 |
else if( !strcmp( root, "HKEY_USERS" ) ) |
57 |
root_key = HKEY_USERS; |
58 |
else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) ) |
59 |
root_key = HKEY_PERFORMANCE_DATA; |
60 |
else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) ) |
61 |
root_key = HKEY_CURRENT_CONFIG; |
62 |
else |
63 |
return NULL; |
64 |
|
65 |
if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) ) |
66 |
return NULL; /* no need for a RegClose, so return direct */ |
67 |
|
68 |
nbytes = 1; |
69 |
if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) ) |
70 |
goto leave; |
71 |
result = malloc( (n1=nbytes+1) ); |
72 |
if( !result ) |
73 |
goto leave; |
74 |
if( RegQueryValueEx( key_handle, name, 0, NULL, result, &n1 ) ) { |
75 |
safe_free(result); result = NULL; |
76 |
goto leave; |
77 |
} |
78 |
result[nbytes] = 0; /* make sure it is really a string */ |
79 |
|
80 |
leave: |
81 |
RegCloseKey( key_handle ); |
82 |
return result; |
83 |
} |
84 |
|
85 |
|
86 |
const char * |
87 |
_gpgme_get_gpg_path (int cleanup) |
88 |
{ |
89 |
static char *gpg_program = NULL; |
90 |
|
91 |
if (cleanup) { |
92 |
if (gpg_program) { |
93 |
free (gpg_program); |
94 |
gpg_program = NULL; |
95 |
} |
96 |
return NULL; |
97 |
} |
98 |
|
99 |
if (!gpg_program) { |
100 |
gpg_program = read_w32_registry_string (NULL, |
101 |
"Software\\GNU\\GnuPG", "gpgProgram"); |
102 |
if (gpg_program) { |
103 |
int i; |
104 |
|
105 |
DEBUG1 ("found gpgProgram in registry: `%s'\n", gpg_program ); |
106 |
for (i=0; gpg_program[i]; i++) { |
107 |
if (gpg_program[i] == '/') |
108 |
gpg_program[i] = '\\'; |
109 |
} |
110 |
} |
111 |
else |
112 |
gpg_program = strdup ("C:\\GNUPG\\GPG.EXE"); |
113 |
} |
114 |
|
115 |
return gpg_program; |
116 |
} |
117 |
|
118 |
|
119 |
const char * |
120 |
_gpgme_get_gpg_optfile (int cleanup) |
121 |
{ |
122 |
static char *gpg_optfile = NULL; |
123 |
|
124 |
if (cleanup) { |
125 |
if (gpg_optfile) { |
126 |
free (gpg_optfile); |
127 |
gpg_optfile = NULL; |
128 |
} |
129 |
return NULL; |
130 |
} |
131 |
|
132 |
if (!gpg_optfile) { |
133 |
FILE *fp; |
134 |
gpg_optfile = read_w32_registry_string( |
135 |
NULL, "Software\\GNU\\GnuPG", "OptFile" ); |
136 |
if (gpg_optfile) { |
137 |
int i; |
138 |
for (i = 0; gpg_optfile[i]; i++) { |
139 |
if (gpg_optfile[i] == '/') |
140 |
gpg_optfile[i] = '\\'; |
141 |
} |
142 |
if (!strcmp(gpg_optfile, "")) |
143 |
return NULL; /* Then ignore the file */ |
144 |
fp = fopen (gpg_optfile, "r"); |
145 |
if (fp == NULL) |
146 |
return NULL; |
147 |
fclose (fp); |
148 |
} |
149 |
} |
150 |
|
151 |
return gpg_optfile; |
152 |
} /* _gpgme_get_gpg_optfile */ |
153 |
|
154 |
|
155 |
void |
156 |
util_cleanup (void) |
157 |
{ |
158 |
_gpgme_get_gpg_optfile (1); |
159 |
_gpgme_get_gpg_path (1); |
160 |
} |