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