Revert "s4:registry - util.c - move the "REG_NONE" case in the conversion functions...
[metze/samba/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_EXPAND_SZ:
73                 case REG_SZ:
74                         if (data.length % 2 == 0) {
75                                 convert_string_talloc_convenience(mem_ctx,
76                                                                   iconv_convenience,
77                                                                   CH_UTF16, CH_UNIX,
78                                                                   data.data,
79                                                                   data.length,
80                                                                   (void **)&ret,
81                                                                   NULL, false);
82                         }
83                         break;
84                 case REG_BINARY:
85                         ret = data_blob_hex_string_upper(mem_ctx, &data);
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_NONE:
101                         /* "NULL" is the right return value */
102                         break;
103                 case REG_MULTI_SZ:
104                         /* FIXME: We don't support this yet */
105                         break;
106                 default:
107                         /* FIXME */
108                         /* Other datatypes aren't supported -> return "NULL" */
109                         break;
110         }
111
112         return ret;
113 }
114
115 /** Generate a string that describes a registry value */
116 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, 
117                                    struct smb_iconv_convenience *iconv_convenience, 
118                                    const char *name,
119                                    uint32_t data_type,
120                                    const DATA_BLOB data)
121 {
122         return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
123                                str_regtype(data_type),
124                                reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
125 }
126
127 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, 
128                                 struct smb_iconv_convenience *iconv_convenience,
129                                 const char *type_str,
130                                 const char *data_str, uint32_t *type,
131                                 DATA_BLOB *data)
132 {
133         unsigned int i;
134         *type = -1;
135
136         /* Find the correct type */
137         for (i = 0; reg_value_types[i].name; i++) {
138                 if (!strcmp(reg_value_types[i].name, type_str)) {
139                         *type = reg_value_types[i].id;
140                         break;
141                 }
142         }
143
144         if (*type == -1)
145                 return false;
146
147         /* Convert data appropriately */
148
149         switch (*type) {
150                 case REG_SZ:
151                 case REG_EXPAND_SZ:
152                         return convert_string_talloc_convenience(mem_ctx,
153                                                                  iconv_convenience,
154                                                                  CH_UNIX, CH_UTF16,
155                                                                  data_str,
156                                                                  strlen(data_str)+1,
157                                                                  (void **)&data->data,
158                                                                  &data->length, false);
159                         break;
160                 case REG_BINARY:
161                         *data = strhex_to_data_blob(mem_ctx, data_str);
162                         break;
163                 case REG_DWORD: {
164                 case REG_DWORD_BIG_ENDIAN: {
165                         uint32_t tmp = strtol(data_str, NULL, 0);
166                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint32_t));
167                         if (data->data == NULL) return false;
168                         SIVAL(data->data, 0, tmp);
169                         }
170                         break;
171                 case REG_QWORD: {
172                         uint64_t tmp = strtoll(data_str, NULL, 0);
173                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint64_t));
174                         if (data->data == NULL) return false;
175                         SBVAL(data->data, 0, tmp);
176                         }
177                         break;
178                 case REG_NONE:
179                         ZERO_STRUCTP(data);
180                         break;
181                 case REG_MULTI_SZ:
182                         /* FIXME: We don't support this yet */
183                         return false;
184                 default:
185                         /* FIXME */
186                         /* Other datatypes aren't supported -> return no success */
187                         return false;
188         }
189         return true;
190 }
191
192 /** Open a key by name (including the predefined key name!) */
193 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
194                         const char *name, struct registry_key **result)
195 {
196         struct registry_key *predef;
197         WERROR error;
198         size_t predeflength;
199         char *predefname;
200
201         if (strchr(name, '\\') != NULL)
202                 predeflength = strchr(name, '\\')-name;
203         else
204                 predeflength = strlen(name);
205
206         predefname = talloc_strndup(mem_ctx, name, predeflength);
207         W_ERROR_HAVE_NO_MEMORY(predefname);
208         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
209         talloc_free(predefname);
210
211         if (!W_ERROR_IS_OK(error)) {
212                 return error;
213         }
214
215         if (strchr(name, '\\')) {
216                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
217                                     result);
218         } else {
219                 *result = predef;
220                 return WERR_OK;
221         }
222 }
223
224 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
225                              const char *path, struct registry_key **parent,
226                              const char **name)
227 {
228         char *parent_name;
229         WERROR error;
230
231         if (strchr(path, '\\') == NULL) {
232                 return WERR_FOOBAR;
233         }
234
235         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
236         W_ERROR_HAVE_NO_MEMORY(parent_name);
237         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
238         talloc_free(parent_name);
239         if (!W_ERROR_IS_OK(error)) {
240                 return error;
241         }
242
243         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
244         W_ERROR_HAVE_NO_MEMORY(*name);
245
246         return WERR_OK;
247 }
248
249 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
250 {
251         struct registry_key *parent;
252         char *n;
253         const char *n;
254         TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
255         WERROR error;
256
257         if (!strchr(path, '\\')) {
258                 return WERR_FOOBAR;
259         }
260
261         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
262         if (W_ERROR_IS_OK(error)) {
263                 error = reg_key_del(mem_ctx, parent, n);
264
265                 talloc_free(parent);
266                 talloc_free(n);
267         }
268         talloc_free(mem_ctx);
269
270
271         return error;
272 }
273
274 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
275                        const char *path, uint32_t access_mask,
276                        struct security_descriptor *sec_desc,
277                        struct registry_key **result)
278 {
279         struct registry_key *parent;
280         char *n;
281         WERROR error;
282
283         *result = NULL;
284
285         if (!strchr(path, '\\')) {
286                 return WERR_ALREADY_EXISTS;
287         }
288
289         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
290         if (!W_ERROR_IS_OK(error)) {
291                 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
292                                   win_errstr(error)));
293                 return error;
294         }
295
296         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
297
298         talloc_free(parent);
299         talloc_free(n);
300
301         return error;
302 }