~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to util/w32reg.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* w32reg.c -  MS-Windows Registry access
2
 
 *      Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
 
 *
4
 
 * This file is part of GnuPG.
5
 
 *
6
 
 * GnuPG is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * GnuPG is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
 
 */
20
 
 
21
 
#include <config.h>
22
 
#if defined (__MINGW32__) || defined (__CYGWIN32__)
23
 
 /* This module is only used in this environment */
24
 
 
25
 
#include <stdio.h>
26
 
#include <stdlib.h>
27
 
#include <string.h>
28
 
#include <stdarg.h>
29
 
#include <windows.h>
30
 
#include "util.h"
31
 
#include "memory.h"
32
 
 
33
 
static HKEY
34
 
get_root_key(const char *root)
35
 
{
36
 
    HKEY root_key;
37
 
        
38
 
    if( !root )
39
 
        root_key = HKEY_CURRENT_USER;
40
 
    else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
41
 
        root_key = HKEY_CLASSES_ROOT;
42
 
    else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
43
 
        root_key = HKEY_CURRENT_USER;
44
 
    else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) )
45
 
        root_key = HKEY_LOCAL_MACHINE;
46
 
    else if( !strcmp( root, "HKEY_USERS" ) )
47
 
        root_key = HKEY_USERS;
48
 
    else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) )
49
 
        root_key = HKEY_PERFORMANCE_DATA;
50
 
    else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
51
 
        root_key = HKEY_CURRENT_CONFIG;
52
 
    else
53
 
        return NULL;
54
 
        
55
 
    return root_key;
56
 
}
57
 
 
58
 
 
59
 
/****************
60
 
 * Return a string from the Win32 Registry or NULL in case of
61
 
 * error.  Caller must release the return value.   A NULL for root
62
 
 * is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn.
63
 
 * NOTE: The value is allocated with a plain malloc() - use free() and not
64
 
 * the usual m_free()!!!
65
 
 */
66
 
char *
67
 
read_w32_registry_string( const char *root, const char *dir, const char *name )
68
 
{
69
 
    HKEY root_key, key_handle;
70
 
    DWORD n1, nbytes, type;
71
 
    char *result = NULL;
72
 
 
73
 
    if ( !(root_key = get_root_key(root) ) )
74
 
        return NULL;
75
 
 
76
 
    if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
77
 
      {
78
 
        if (root)
79
 
          return NULL; /* no need for a RegClose, so return direct */
80
 
        /* It seems to be common practise to fall back to HLM. */
81
 
        if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) )
82
 
          return NULL; /* still no need for a RegClose, so return direct */
83
 
      }
84
 
 
85
 
    nbytes = 1;
86
 
    if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) )
87
 
        goto leave;
88
 
    result = malloc( (n1=nbytes+1) );
89
 
    if( !result )
90
 
        goto leave;
91
 
    if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) {
92
 
        free(result); result = NULL;
93
 
        goto leave;
94
 
    }
95
 
    result[nbytes] = 0; /* make sure it is really a string  */
96
 
    if (type == REG_EXPAND_SZ && strchr (result, '%')) {
97
 
        char *tmp;
98
 
        
99
 
        n1 += 1000;
100
 
        tmp = malloc (n1+1);
101
 
        if (!tmp)
102
 
            goto leave;
103
 
        nbytes = ExpandEnvironmentStrings (result, tmp, n1);
104
 
        if (nbytes && nbytes > n1) {
105
 
            free (tmp);
106
 
            tmp = malloc (n1 + 1);
107
 
            if (!tmp)
108
 
                goto leave;
109
 
            nbytes = ExpandEnvironmentStrings (result, tmp, n1);
110
 
            if (nbytes && nbytes > n1) {
111
 
                free (tmp); /* oops - truncated, better don't expand at all */
112
 
                goto leave;
113
 
            }
114
 
            tmp[nbytes] = 0;
115
 
            free (result);
116
 
            result = tmp;
117
 
        }
118
 
        else if (nbytes) { /* okay, reduce the length */
119
 
            tmp[nbytes] = 0;
120
 
            free (result);
121
 
            result = malloc (strlen (tmp)+1);
122
 
            if (!result)
123
 
                result = tmp;
124
 
            else {
125
 
                strcpy (result, tmp);
126
 
                free (tmp);
127
 
            }
128
 
        }
129
 
        else {  /* error - don't expand */
130
 
            free (tmp);
131
 
        }
132
 
    }
133
 
 
134
 
  leave:
135
 
    RegCloseKey( key_handle );
136
 
    return result;
137
 
}
138
 
 
139
 
 
140
 
int
141
 
write_w32_registry_string(const char *root, const char *dir, 
142
 
                          const char *name, const char *value)
143
 
{
144
 
    HKEY root_key, reg_key;
145
 
        
146
 
    if ( !(root_key = get_root_key(root) ) )
147
 
        return -1;
148
 
 
149
 
    if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key ) 
150
 
         != ERROR_SUCCESS )
151
 
        return -1;
152
 
        
153
 
    if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value, 
154
 
                        strlen( value ) ) != ERROR_SUCCESS ) {
155
 
        if ( RegCreateKey( root_key, name, &reg_key ) != ERROR_SUCCESS ) {
156
 
            RegCloseKey(reg_key);
157
 
            return -1;
158
 
        }
159
 
        if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
160
 
                            strlen( value ) ) != ERROR_SUCCESS ) {
161
 
            RegCloseKey(reg_key);
162
 
            return -1;
163
 
        }
164
 
    }
165
 
 
166
 
    RegCloseKey( reg_key );
167
 
        
168
 
    return 0;
169
 
}
170
 
 
171
 
#endif /* __MINGW32__ || __CYGWIN32__ */