Fix ignore for libcli/nbt/nbtname.h
[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 #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(data_str);
144                         talloc_steal(mem_ctx, data->data);
145                         break;
146
147                 default:
148                         /* FIXME */
149                         return false;
150         }
151         return true;
152 }
153
154 /** Open a key by name (including the predefined key name!) */
155 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
156                         const char *name, struct registry_key **result)
157 {
158         struct registry_key *predef;
159         WERROR error;
160         int predeflength;
161         char *predefname;
162
163         if (strchr(name, '\\') != NULL)
164                 predeflength = strchr(name, '\\')-name;
165         else
166                 predeflength = strlen(name);
167
168         predefname = talloc_strndup(mem_ctx, name, predeflength);
169         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
170         talloc_free(predefname);
171
172         if (!W_ERROR_IS_OK(error)) {
173                 return error;
174         }
175
176         if (strchr(name, '\\')) {
177                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
178                                     result);
179         } else {
180                 *result = predef;
181                 return WERR_OK;
182         }
183 }
184
185 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
186                              const char *path, struct registry_key **parent,
187                              const char **name)
188 {
189         char *parent_name;
190         WERROR error;
191
192         if (strchr(path, '\\') == NULL) {
193                 return WERR_FOOBAR;
194         }
195
196         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
197
198         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
199         if (!W_ERROR_IS_OK(error)) {
200                 return error;
201         }
202
203         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
204
205         return WERR_OK;
206 }
207
208 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
209 {
210         struct registry_key *parent;
211         const char *n;
212         TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
213         WERROR error;
214
215         if (!strchr(path, '\\')) {
216                 return WERR_FOOBAR;
217         }
218
219         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
220         if (W_ERROR_IS_OK(error)) {
221                 error = reg_key_del(parent, n);
222         }
223
224         talloc_free(mem_ctx);
225
226         return error;
227 }
228
229 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
230                        const char *path, uint32_t access_mask,
231                        struct security_descriptor *sec_desc,
232                        struct registry_key **result)
233 {
234         struct registry_key *parent;
235         const char *n;
236         WERROR error;
237
238         if (!strchr(path, '\\')) {
239                 return WERR_ALREADY_EXISTS;
240         }
241
242         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
243         if (!W_ERROR_IS_OK(error)) {
244                 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
245                                   win_errstr(error)));
246                 return error;
247         }
248
249         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
250
251         return error;
252 }