s4: fixed some printf format errors
[kamenim/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 _PUBLIC_ char *reg_val_data_string(TALLOC_CTX *mem_ctx, uint32_t type,
25                                    const DATA_BLOB data)
26 {
27         char *ret = NULL;
28
29         if (data.length == 0)
30                 return talloc_strdup(mem_ctx, "");
31
32         switch (type) {
33                 case REG_EXPAND_SZ:
34                 case REG_SZ:
35                         convert_string_talloc(mem_ctx,
36                                                           CH_UTF16, CH_UNIX, data.data, data.length,
37                                                           (void **)&ret, NULL, false);
38                         break;
39                 case REG_DWORD:
40                 case REG_DWORD_BIG_ENDIAN:
41                         SMB_ASSERT(data.length == sizeof(uint32_t));
42                         ret = talloc_asprintf(mem_ctx, "0x%8.8x",
43                                               IVAL(data.data, 0));
44                         break;
45                 case REG_QWORD:
46                         SMB_ASSERT(data.length == sizeof(uint64_t));
47                         ret = talloc_asprintf(mem_ctx, "0x%16.16llx",
48                                               (unsigned long long)BVAL(data.data, 0));
49                         break;
50                 case REG_BINARY:
51                         ret = data_blob_hex_string_upper(mem_ctx, &data);
52                         break;
53                 case REG_NONE:
54                         /* "NULL" is the right return value */
55                         break;
56                 case REG_MULTI_SZ:
57                         /* FIXME: We don't support this yet */
58                         break;
59                 default:
60                         /* FIXME */
61                         /* Other datatypes aren't supported -> return "NULL" */
62                         break;
63         }
64
65         return ret;
66 }
67
68 /** Generate a string that describes a registry value */
69 _PUBLIC_ char *reg_val_description(TALLOC_CTX *mem_ctx, 
70                                    const char *name,
71                                    uint32_t data_type,
72                                    const DATA_BLOB data)
73 {
74         return talloc_asprintf(mem_ctx, "%s = %s : %s", name?name:"<No Name>",
75                                str_regtype(data_type),
76                                reg_val_data_string(mem_ctx, data_type, data));
77 }
78
79 _PUBLIC_ bool reg_string_to_val(TALLOC_CTX *mem_ctx, const char *type_str,
80                                 const char *data_str, uint32_t *type, DATA_BLOB *data)
81 {
82         *type = regtype_by_string(type_str);
83
84         if (*type == -1)
85                 return false;
86
87         /* Convert data appropriately */
88
89         switch (*type) {
90                 case REG_SZ:
91                 case REG_EXPAND_SZ:
92                         return convert_string_talloc(mem_ctx,
93                                                                  CH_UNIX, CH_UTF16, data_str,
94                                                                  strlen(data_str)+1,
95                                                                  (void **)&data->data,
96                                                                  &data->length, false);
97                         break;
98                 case REG_BINARY:
99                         *data = strhex_to_data_blob(mem_ctx, data_str);
100                         break;
101                 case REG_DWORD:
102                 case REG_DWORD_BIG_ENDIAN: {
103                         uint32_t tmp = strtol(data_str, NULL, 0);
104                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint32_t));
105                         if (data->data == NULL) return false;
106                         SIVAL(data->data, 0, tmp);
107                         }
108                         break;
109                 case REG_QWORD: {
110                         uint64_t tmp = strtoll(data_str, NULL, 0);
111                         *data = data_blob_talloc(mem_ctx, NULL, sizeof(uint64_t));
112                         if (data->data == NULL) return false;
113                         SBVAL(data->data, 0, tmp);
114                         }
115                         break;
116                 case REG_NONE:
117                         ZERO_STRUCTP(data);
118                         break;
119                 case REG_MULTI_SZ:
120                         /* FIXME: We don't support this yet */
121                         return false;
122                 default:
123                         /* FIXME */
124                         /* Other datatypes aren't supported -> return no success */
125                         return false;
126         }
127         return true;
128 }
129
130 /** Open a key by name (including the predefined key name!) */
131 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle,
132                         const char *name, struct registry_key **result)
133 {
134         struct registry_key *predef;
135         WERROR error;
136         size_t predeflength;
137         char *predefname;
138
139         if (strchr(name, '\\') != NULL)
140                 predeflength = strchr(name, '\\')-name;
141         else
142                 predeflength = strlen(name);
143
144         predefname = talloc_strndup(mem_ctx, name, predeflength);
145         W_ERROR_HAVE_NO_MEMORY(predefname);
146         error = reg_get_predefined_key_by_name(handle, predefname, &predef);
147         talloc_free(predefname);
148
149         if (!W_ERROR_IS_OK(error)) {
150                 return error;
151         }
152
153         if (strchr(name, '\\')) {
154                 return reg_open_key(mem_ctx, predef, strchr(name, '\\')+1,
155                                     result);
156         } else {
157                 *result = predef;
158                 return WERR_OK;
159         }
160 }
161
162 static WERROR get_abs_parent(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
163                              const char *path, struct registry_key **parent,
164                              const char **name)
165 {
166         char *parent_name;
167         WERROR error;
168
169         if (strchr(path, '\\') == NULL) {
170                 return WERR_FOOBAR;
171         }
172
173         parent_name = talloc_strndup(mem_ctx, path, strrchr(path, '\\')-path);
174         W_ERROR_HAVE_NO_MEMORY(parent_name);
175         error = reg_open_key_abs(mem_ctx, ctx, parent_name, parent);
176         talloc_free(parent_name);
177         if (!W_ERROR_IS_OK(error)) {
178                 return error;
179         }
180
181         *name = talloc_strdup(mem_ctx, strrchr(path, '\\')+1);
182         W_ERROR_HAVE_NO_MEMORY(*name);
183
184         return WERR_OK;
185 }
186
187 WERROR reg_key_del_abs(struct registry_context *ctx, const char *path)
188 {
189         struct registry_key *parent;
190         const char *n;
191         TALLOC_CTX *mem_ctx = talloc_init("reg_key_del_abs");
192         WERROR error;
193
194         if (!strchr(path, '\\')) {
195                 return WERR_FOOBAR;
196         }
197
198         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
199         if (W_ERROR_IS_OK(error)) {
200                 error = reg_key_del(mem_ctx, parent, n);
201         }
202
203         talloc_free(mem_ctx);
204
205         return error;
206 }
207
208 WERROR reg_key_add_abs(TALLOC_CTX *mem_ctx, struct registry_context *ctx,
209                        const char *path, uint32_t access_mask,
210                        struct security_descriptor *sec_desc,
211                        struct registry_key **result)
212 {
213         struct registry_key *parent;
214         const char *n;
215         WERROR error;
216
217         *result = NULL;
218
219         if (!strchr(path, '\\')) {
220                 return WERR_ALREADY_EXISTS;
221         }
222
223         error = get_abs_parent(mem_ctx, ctx, path, &parent, &n);
224         if (!W_ERROR_IS_OK(error)) {
225                 DEBUG(2, ("Opening parent of %s failed with %s\n", path,
226                                   win_errstr(error)));
227                 return error;
228         }
229
230         error = reg_key_add_name(mem_ctx, parent, n, NULL, sec_desc, result);
231
232         return error;
233 }