r24665: Close file handles properly.
[ira/wip.git] / source / 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, CH_UTF16, CH_UNIX, data.data, data.length, 
65                                                         (void **)&ret);
66           return ret;
67
68   case REG_BINARY:
69           ret = data_blob_hex_string(mem_ctx, &data);
70           return ret;
71
72   case REG_DWORD:
73           if (*(int *)data.data == 0)
74                   return talloc_strdup(mem_ctx, "0");
75
76           return talloc_asprintf(mem_ctx, "0x%x", *(int *)data.data);
77
78   case REG_MULTI_SZ:
79         /* FIXME */
80     break;
81
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, const char *name, 
91                                                                    uint32_t data_type,
92                                                                    const DATA_BLOB data)
93 {
94         return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>", 
95                                                    str_regtype(data_type), 
96                                         reg_val_data_string(mem_ctx, data_type, data));
97 }
98
99 _PUBLIC_ BOOL reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str, const char *data_str, uint32_t *type, 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, CH_UNIX, CH_UTF16, data_str, strlen(data_str), (void **)&data->data);
122                         break;
123
124                 case REG_DWORD: {
125                         uint32_t tmp = strtol(data_str, NULL, 0);
126                         *data = data_blob_talloc(mem_ctx, &tmp, 4);
127                         }
128                         break;
129
130                 case REG_NONE:
131                         ZERO_STRUCTP(data);
132                         break;
133         
134                 case REG_BINARY: 
135                         *data = strhex_to_data_blob(data_str);
136                         talloc_steal(mem_ctx, data->data);
137                         break;
138                         
139                 default:
140                         /* FIXME */
141                         return False;
142         }
143         return True;
144 }
145
146 /** Open a key by name (including the predefined key name!) */
147 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle, const char *name, struct registry_key **result)
148 {
149         struct registry_key *predef;
150         WERROR error;
151         int predeflength;
152         char *predefname;
153
154         if (strchr(name, '\\') != NULL) 
155                 predeflength = strchr(name, '\\')-name;
156         else 
157                 predeflength = strlen(name);
158
159         predefname = talloc_strndup(mem_ctx, name, predeflength);
160         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
161         talloc_free(predefname);
162
163         if (!W_ERROR_IS_OK(error)) {
164                 return error;
165         }
166
167         if (strchr(name, '\\')) {
168                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1, result);
169         } else {
170                 *result = predef;
171                 return WERR_OK;
172         }
173 }
174
175 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx, 
176                                                          const char *path, struct registry_key **parent, 
177                                                          const char **name)
178 {
179         char *parent_name;
180         WERROR error;
181         
182         if (strchr(path, '\\') == NULL) {
183                 return WERR_FOOBAR;
184         }
185         
186         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
187
188         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
189         if (!W_ERROR_IS_OK(error)) {
190                 return error;
191         }
192         
193         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
194
195         return WERR_OK;
196 }
197
198 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
199 {
200         struct registry_key *parent;
201         const char *n;
202         TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
203         WERROR error;
204         
205         if (!strchr(path, '\\')) {
206                 return WERR_FOOBAR;
207         }
208         
209         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
210         if (W_ERROR_IS_OK(error)) {
211                 error = reg_key_del(parent, n);
212         }
213
214         talloc_free(mem_ctx);
215
216         return error;
217 }
218
219 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx, 
220                                            const char *path, uint32_t access_mask, 
221                                            struct security_descriptor *sec_desc, 
222                                            struct registry_key **result)
223 {
224         struct registry_key *parent;
225         const char *n;
226         WERROR error;
227         
228         if (!strchr(path, '\\')) {
229                 return WERR_ALREADY_EXISTS;
230         }
231         
232         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
233         if (!W_ERROR_IS_OK(error)) {
234                 DEBUG(2, ("Opening parent of %s failed with %s\n", path, 
235                                   win_errstr(error)));
236                 return error;
237         }
238
239         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
240
241         return error;
242 }