5bc0a639506d2d2470cc23e9b35a88728c2a3c54
[ira/wip.git] / source4 / lib / registry / util.c
1 /*
2    Unix SMB/CIFS implementation.
3    Transparent registry backend handling
4    Copyright (C) Jelmer Vernooij                        2003-2007.
5
6    This program 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 3 of the License, or
9    (at your option) any later version.
10
11    This program 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/registry/registry.h"
22 #include "librpc/gen_ndr/winreg.h"
23
24 /**
25  * @file
26  * @brief Registry utility functions
27  */
28
29 static const struct {
30         uint32_t id;
31         const char *name;
32 } reg_value_types[] = {
33         { REG_NONE, "REG_NONE" },
34         { REG_SZ, "REG_SZ" },
35         { REG_EXPAND_SZ, "REG_EXPAND_SZ" },
36         { REG_BINARY, "REG_BINARY" },
37         { REG_DWORD, "REG_DWORD" },
38         { REG_DWORD_BIG_ENDIAN, "REG_DWORD_BIG_ENDIAN" },
39         { REG_LINK, "REG_LINK" },
40         { REG_MULTI_SZ, "REG_MULTI_SZ" },
41         { REG_RESOURCE_LIST, "REG_RESOURCE_LIST" },
42         { REG_FULL_RESOURCE_DESCRIPTOR, "REG_FULL_RESOURCE_DESCRIPTOR" },
43         { REG_RESOURCE_REQUIREMENTS_LIST, "REG_RESOURCE_REQUIREMENTS_LIST" },
44         { REG_QWORD, "REG_QWORD" },
45
46         { 0, NULL }
47 };
48
49 /** Return string description of registry value type */
50 _PUBLIC_ const char *str_regtype(int type)
51 {
52         unsigned int i;
53         for (i = 0; reg_value_types[i].name; i++) {
54                 if (reg_value_types[i].id == type)
55                         return reg_value_types[i].name;
56         }
57
58         return "Unknown";
59 }
60
61 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, 
62                                    struct smb_iconv_convenience *iconv_convenience,
63                                    uint32_t type,
64                                    const DATA_BLOB data)
65 {
66         char *ret = NULL;
67
68         if (data.length == 0)
69                 return talloc_strdup(mem_ctx, "");
70
71         switch (type) {
72                 case REG_NONE:
73                         /* "NULL" is the right return value */
74                         break;
75                 case REG_EXPAND_SZ:
76                 case REG_SZ:
77                         if (data.length % 2 == 0) {
78                                 convert_string_talloc_convenience(mem_ctx,
79                                                                   iconv_convenience,
80                                                                   CH_UTF16, CH_UNIX,
81                                                                   data.data,
82                                                                   data.length,
83                                                                   (void **)&ret,
84                                                                   NULL, false);
85                         }
86                         break;
87                 case REG_DWORD:
88                 case REG_DWORD_BIG_ENDIAN:
89                         if (data.length == sizeof(uint32_t)) {
90                                 ret = talloc_asprintf(mem_ctx, "0x%8.8x",
91                                                       IVAL(data.data, 0));
92                         }
93                         break;
94                 case REG_QWORD:
95                         if (data.length == sizeof(uint64_t)) {
96                                 ret = talloc_asprintf(mem_ctx, "0x%16.16llx",
97                                                       BVAL(data.data, 0));
98                         }
99                         break;
100                 case REG_BINARY:
101                 default:
102                         ret = data_blob_hex_string_upper(mem_ctx, &data);
103                         break;
104         }
105
106         return ret;
107 }
108
109 /** Generate a string that describes a registry value */
110 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, 
111                                    struct smb_iconv_convenience *iconv_convenience, 
112                                    const char *name,
113                                    uint32_t data_type,
114                                    const DATA_BLOB data)
115 {
116         return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
117                                str_regtype(data_type),
118                                reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
119 }
120
121 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, 
122                                 struct smb_iconv_convenience *iconv_convenience,
123                                 const char *type_str,
124                                 const char *data_str, uint32_t *type,
125                                 DATA_BLOB *data)
126 {
127         unsigned int i;
128         *type = -1;
129
130         /* Find the correct type */
131         for (i = 0; reg_value_types[i].name; i++) {
132                 if (!strcmp(reg_value_types[i].name, type_str)) {
133                         *type = reg_value_types[i].id;
134                         break;
135                 }
136         }
137
138         if (*type == -1)
139                 return false;
140
141         /* Convert data appropriately */
142
143         switch (*type) {
144                 case REG_NONE:
145                         ZERO_STRUCTP(data);
146                         break;
147                 case REG_SZ:
148                 case REG_EXPAND_SZ:
149                         return convert_string_talloc_convenience(mem_ctx,
150                                                                  iconv_convenience,
151                                                                  CH_UNIX, CH_UTF16,
152                                                                  data_str,
153                                                                  strlen(data_str)+1,
154                                                                  (void **)&data->data,
155                                                                  &data->length, false);
156                         break;
157                 case REG_DWORD:
158                 case REG_DWORD_BIG_ENDIAN: {
159                         uint32_t tmp = strtol(data_str, NULL, 0);
160                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint32_t));
161                         if (data->data == NULL) return false;
162                         SIVAL(data->data, 0, tmp);
163                         }
164                         break;
165                 case REG_QWORD: {
166                         uint64_t tmp = strtoll(data_str, NULL, 0);
167                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint64_t));
168                         if (data->data == NULL) return false;
169                         SBVAL(data->data, 0, tmp);
170                         }
171                         break;
172                 case REG_BINARY:
173                 default:
174                         *data = strhex_to_data_blob(mem_ctx, data_str);
175                         break;
176         }
177         return true;
178 }
179
180 /** Open a key by name (including the predefined key name!) */
181 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
182                         const char *name, struct registry_key **result)
183 {
184         struct registry_key *predef;
185         WERROR error;
186         size_t predeflength;
187         char *predefname;
188
189         if (strchr(name, '\\') != NULL)
190                 predeflength = strchr(name, '\\')-name;
191         else
192                 predeflength = strlen(name);
193
194         predefname = talloc_strndup(mem_ctx, name, predeflength);
195         W_ERROR_HAVE_NO_MEMORY(predefname);
196         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
197         talloc_free(predefname);
198
199         if (!W_ERROR_IS_OK(error)) {
200                 return error;
201         }
202
203         if (strchr(name, '\\')) {
204                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
205                                     result);
206         } else {
207                 *result = predef;
208                 return WERR_OK;
209         }
210 }
211
212 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
213                              const char *path, struct registry_key **parent,
214                              const char **name)
215 {
216         char *parent_name;
217         WERROR error;
218
219         if (strchr(path, '\\') == NULL) {
220                 return WERR_FOOBAR;
221         }
222
223         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
224         W_ERROR_HAVE_NO_MEMORY(parent_name);
225         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
226         talloc_free(parent_name);
227         if (!W_ERROR_IS_OK(error)) {
228                 return error;
229         }
230
231         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
232         W_ERROR_HAVE_NO_MEMORY(*name);
233
234         return WERR_OK;
235 }
236
237 WERROR reg_key_del_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
238                        const char *path)
239 {
240         struct registry_key *parent;
241         char *n;
242         WERROR error;
243
244         if (!strchr(path, '\\')) {
245                 return WERR_FOOBAR;
246         }
247
248         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
249         if (W_ERROR_IS_OK(error)) {
250                 error = reg_key_del(mem_ctx, parent, n);
251
252                 talloc_free(parent);
253                 talloc_free(n);
254         }
255
256         return error;
257 }
258
259 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
260                        const char *path, uint32_t access_mask,
261                        struct security_descriptor *sec_desc,
262                        struct registry_key **result)
263 {
264         struct registry_key *parent;
265         char *n;
266         WERROR error;
267
268         *result = NULL;
269
270         if (!strchr(path, '\\')) {
271                 return WERR_ALREADY_EXISTS;
272         }
273
274         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
275         if (!W_ERROR_IS_OK(error)) {
276                 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
277                                   win_errstr(error)));
278                 return error;
279         }
280
281         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
282
283         talloc_free(parent);
284         talloc_free(n);
285
286         return error;
287 }