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