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