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