Revert "s4:registry/util - Don't include the trailing '\0' in the internal data forma...
[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_SZ, "REG_SZ" },
34         { REG_DWORD, "REG_DWORD" },
35         { REG_BINARY, "REG_BINARY" },
36         { REG_EXPAND_SZ, "REG_EXPAND_SZ" },
37         { REG_NONE, "REG_NONE" },
38         { 0, NULL }
39 };
40
41 /** Return string description of registry value type */
42 _PUBLIC_ const char *str_regtype(int type)
43 {
44         int i;
45         for (i = 0; reg_value_types[i].name; i++) {
46                 if (reg_value_types[i].id == type)
47                         return reg_value_types[i].name;
48         }
49
50         return "Unknown";
51 }
52
53 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, 
54                                    struct smb_iconv_convenience *iconv_convenience,
55                                    uint32_t type,
56                                    const DATA_BLOB data)
57 {
58         char *ret = NULL;
59
60         if (data.length == 0)
61                 return talloc_strdup(mem_ctx, "");
62
63         switch (type) {
64                 case REG_EXPAND_SZ:
65                 case REG_SZ:
66                         convert_string_talloc_convenience(mem_ctx,
67                                                           iconv_convenience,
68                                                           CH_UTF16, CH_UNIX,
69                                                           data.data,
70                                                           data.length,
71                                                           (void **)&ret,
72                                                           NULL, false);
73                         break;
74                 case REG_BINARY:
75                         ret = data_blob_hex_string_upper(mem_ctx, &data);
76                         break;
77                 case REG_DWORD:
78                         if (*(int *)data.data == 0) {
79                                 ret = talloc_strdup(mem_ctx, "0");
80                         } else {
81                                 ret = talloc_asprintf(mem_ctx, "0x%x",
82                                                       *(int *)data.data);
83                         }
84                         break;
85                 case REG_MULTI_SZ:
86                         /* FIXME */
87                         break;
88                 default:
89                         break;
90         }
91
92         return ret;
93 }
94
95 /** Generate a string that describes a registry value */
96 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, 
97                                    struct smb_iconv_convenience *iconv_convenience, 
98                                    const char *name,
99                                    uint32_t data_type,
100                                    const DATA_BLOB data)
101 {
102         return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
103                                str_regtype(data_type),
104                                reg_val_data_string(mem_ctx, iconv_convenience, data_type, data));
105 }
106
107 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, 
108                                 struct smb_iconv_convenience *iconv_convenience,
109                                 const char *type_str,
110                                 const char *data_str, uint32_t *type,
111                                 DATA_BLOB *data)
112 {
113         int i;
114         *type = -1;
115
116         /* Find the correct type */
117         for (i = 0; reg_value_types[i].name; i++) {
118                 if (!strcmp(reg_value_types[i].name, type_str)) {
119                         *type = reg_value_types[i].id;
120                         break;
121                 }
122         }
123
124         if (*type == -1)
125                 return false;
126
127         /* Convert data appropriately */
128
129         switch (*type) {
130                 case REG_SZ:
131                 case REG_EXPAND_SZ:
132                         convert_string_talloc_convenience(mem_ctx,
133                                                           iconv_convenience,
134                                                           CH_UNIX, CH_UTF16,
135                                                           data_str,
136                                                           strlen(data_str)+1,
137                                                           (void **)&data->data,
138                                                           &data->length, false);
139                         break;
140
141                 case REG_DWORD: {
142                         uint32_t tmp = strtol(data_str, NULL, 0);
143                         *data = data_blob_talloc(mem_ctx, &tmp, 4);
144                         }
145                         break;
146
147                 case REG_NONE:
148                         ZERO_STRUCTP(data);
149                         break;
150
151                 case REG_BINARY:
152                         *data = strhex_to_data_blob(mem_ctx, data_str);
153                         break;
154
155                 default:
156                         /* FIXME */
157                         return false;
158         }
159         return true;
160 }
161
162 /** Open a key by name (including the predefined key name!) */
163 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
164                         const char *name, struct registry_key **result)
165 {
166         struct registry_key *predef;
167         WERROR error;
168         int predeflength;
169         char *predefname;
170
171         if (strchr(name, '\\') != NULL)
172                 predeflength = strchr(name, '\\')-name;
173         else
174                 predeflength = strlen(name);
175
176         predefname = talloc_strndup(mem_ctx, name, predeflength);
177         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
178         talloc_free(predefname);
179
180         if (!W_ERROR_IS_OK(error)) {
181                 return error;
182         }
183
184         if (strchr(name, '\\')) {
185                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
186                                     result);
187         } else {
188                 *result = predef;
189                 return WERR_OK;
190         }
191 }
192
193 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
194                              const char *path, struct registry_key **parent,
195                              const char **name)
196 {
197         char *parent_name;
198         WERROR error;
199
200         if (strchr(path, '\\') == NULL) {
201                 return WERR_FOOBAR;
202         }
203
204         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
205
206         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
207         if (!W_ERROR_IS_OK(error)) {
208                 return error;
209         }
210
211         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
212
213         return WERR_OK;
214 }
215
216 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
217 {
218         struct registry_key *parent;
219         const char *n;
220         TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
221         WERROR error;
222
223         if (!strchr(path, '\\')) {
224                 return WERR_FOOBAR;
225         }
226
227         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
228         if (W_ERROR_IS_OK(error)) {
229                 error = reg_key_del(parent, n);
230         }
231
232         talloc_free(mem_ctx);
233
234         return error;
235 }
236
237 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
238                        const char *path, uint32_t access_mask,
239                        struct security_descriptor *sec_desc,
240                        struct registry_key **result)
241 {
242         struct registry_key *parent;
243         const char *n;
244         WERROR error;
245
246         if (!strchr(path, '\\')) {
247                 return WERR_ALREADY_EXISTS;
248         }
249
250         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
251         if (!W_ERROR_IS_OK(error)) {
252                 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
253                                   win_errstr(error)));
254                 return error;
255         }
256
257         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
258
259         return error;
260 }