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 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 (void) |
88 |
{ |
89 |
static char *gpg_program = NULL; |
90 |
|
91 |
if (!gpg_program) { |
92 |
gpg_program = read_w32_registry_string ( NULL, |
93 |
"Software\\GNU\\GnuPG", "gpgProgram" ); |
94 |
if (gpg_program) { |
95 |
int i; |
96 |
|
97 |
DEBUG1 ("found gpgProgram in registry: `%s'\n", gpg_program ); |
98 |
for (i=0; gpg_program[i]; i++) { |
99 |
if (gpg_program[i] == '/') |
100 |
gpg_program[i] = '\\'; |
101 |
} |
102 |
} |
103 |
else { |
104 |
gpg_program = "C:\\GNUPG\\GPG.EXE"; |
105 |
} |
106 |
} |
107 |
|
108 |
return gpg_program; |
109 |
} |
110 |
|
111 |
char * |
112 |
_gpgme_get_gpg_optfile( void ) |
113 |
{ |
114 |
FILE *fp; |
115 |
char *gpg_optfile = NULL; |
116 |
|
117 |
gpg_optfile = read_w32_registry_string( |
118 |
NULL, "Software\\GNU\\GnuPG", "OptFile" ); |
119 |
if( gpg_optfile ) { |
120 |
int i; |
121 |
for( i = 0; gpg_optfile[i]; i++ ) { |
122 |
if( gpg_optfile[i] == '/' ) |
123 |
gpg_optfile[i] = '\\'; |
124 |
} |
125 |
if( !strcmp(gpg_optfile, "") ) |
126 |
return NULL; /* Then ignore the file */ |
127 |
fp = fopen( gpg_optfile, "r" ); |
128 |
if( fp == NULL ) |
129 |
return NULL; |
130 |
fclose( fp ); |
131 |
} |
132 |
|
133 |
return gpg_optfile; |
134 |
} /* _gpgme_get_gpg_optfile */ |
135 |
|
136 |
const char * |
137 |
_gpgme_get_gpg_keyring( const char *keyring ) |
138 |
{ |
139 |
static char gpg_keyring[2048] = {0}; |
140 |
char *homedir; |
141 |
|
142 |
homedir = read_w32_registry_string(NULL, "Software\\GNU\\GnuPG", |
143 |
"HomeDir"); |
144 |
if( homedir ) { |
145 |
_snprintf( gpg_keyring, sizeof gpg_keyring -1, "%s\\%s", |
146 |
homedir, keyring ); |
147 |
free( homedir ); |
148 |
} |
149 |
|
150 |
return gpg_keyring; |
151 |
} /* _gpgme_get_gpg_keyring */ |