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