Fix clang 9 missing-field-initializer warnings
[samba.git] / source4 / lib / registry / patchfile_preg.c
1 /*
2    Unix SMB/CIFS implementation.
3    Reading Registry.pol PReg registry files
4
5    Copyright (C) Wilco Baan Hofman 2006-2008
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/registry/registry.h"
24 #include "system/filesys.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "lib/util/sys_rw.h"
27
28 struct preg_data {
29         int fd;
30         TALLOC_CTX *ctx;
31 };
32
33 static WERROR preg_read_utf16(int fd, char *c)
34 {
35         uint16_t v;
36
37         if (read(fd, &v, sizeof(uint16_t)) < sizeof(uint16_t)) {
38                 return WERR_GEN_FAILURE;
39         }
40         push_codepoint(c, v);
41         return WERR_OK;
42 }
43 static WERROR preg_write_utf16(int fd, const char *string)
44 {
45         uint16_t v;
46         size_t i, size;
47
48         for (i = 0; i < strlen(string); i+=size) {
49                 v = next_codepoint(&string[i], &size);
50                 if (write(fd, &v, sizeof(uint16_t)) < sizeof(uint16_t)) {
51                         return WERR_GEN_FAILURE;
52                 }
53         }
54         return WERR_OK;
55 }
56 /* PReg does not support adding keys. */
57 static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
58 {
59         return WERR_OK;
60 }
61
62 static WERROR reg_preg_diff_set_value(void *_data, const char *key_name,
63                                       const char *value_name,
64                                       uint32_t value_type, DATA_BLOB value_data)
65 {
66         struct preg_data *data = (struct preg_data *)_data;
67         uint32_t buf;
68         
69         preg_write_utf16(data->fd, "[");
70         preg_write_utf16(data->fd, key_name);
71         preg_write_utf16(data->fd, ";");
72         preg_write_utf16(data->fd, value_name);
73         preg_write_utf16(data->fd, ";");
74         SIVAL(&buf, 0, value_type);
75         sys_write_v(data->fd, &buf, sizeof(uint32_t));
76         preg_write_utf16(data->fd, ";");
77         SIVAL(&buf, 0, value_data.length);
78         sys_write_v(data->fd, &buf, sizeof(uint32_t));
79         preg_write_utf16(data->fd, ";");
80         sys_write_v(data->fd, value_data.data, value_data.length);
81         preg_write_utf16(data->fd, "]");
82         
83         return WERR_OK;
84 }
85
86 static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
87 {
88         struct preg_data *data = (struct preg_data *)_data;
89         char *parent_name;
90         DATA_BLOB blob;
91         WERROR werr;
92
93         parent_name = talloc_strndup(data->ctx, key_name,
94                                      strrchr(key_name, '\\')-key_name);
95         W_ERROR_HAVE_NO_MEMORY(parent_name);
96         blob.data = (uint8_t*)talloc_strndup(data->ctx,
97                                              key_name+(strrchr(key_name, '\\')-key_name)+1,
98                                              strlen(key_name)-(strrchr(key_name, '\\')-key_name));
99         W_ERROR_HAVE_NO_MEMORY(blob.data);
100         blob.length = strlen((char *)blob.data)+1;
101         
102
103         /* FIXME: These values should be accumulated to be written at done(). */
104         werr = reg_preg_diff_set_value(data, parent_name, "**DeleteKeys",
105                                        REG_SZ, blob);
106
107         talloc_free(parent_name);
108         talloc_free(blob.data);
109
110         return werr;
111 }
112
113 static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
114                                       const char *value_name)
115 {
116         struct preg_data *data = (struct preg_data *)_data;
117         char *val;
118         DATA_BLOB blob;
119         WERROR werr;
120
121         val = talloc_asprintf(data->ctx, "**Del.%s", value_name);
122         W_ERROR_HAVE_NO_MEMORY(val);
123         blob.data = (uint8_t *)talloc(data->ctx, uint32_t);
124         W_ERROR_HAVE_NO_MEMORY(blob.data);
125         SIVAL(blob.data, 0, 0);
126         blob.length = sizeof(uint32_t);
127
128         werr = reg_preg_diff_set_value(data, key_name, val, REG_DWORD, blob);
129
130         talloc_free(val);
131         talloc_free(blob.data);
132
133         return werr;
134 }
135
136 static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
137 {
138         struct preg_data *data = (struct preg_data *)_data;
139         DATA_BLOB blob;
140         WERROR werr;
141
142         blob.data = (uint8_t *)talloc(data->ctx, uint32_t);
143         W_ERROR_HAVE_NO_MEMORY(blob.data);
144         SIVAL(blob.data, 0, 0);
145         blob.length = sizeof(uint32_t);
146
147         werr = reg_preg_diff_set_value(data, key_name, "**DelVals.", REG_DWORD,
148                                        blob);
149
150         talloc_free(blob.data);
151
152         return werr;
153 }
154
155 static WERROR reg_preg_diff_done(void *_data)
156 {
157         struct preg_data *data = (struct preg_data *)_data;
158         
159         close(data->fd);
160         talloc_free(data);
161         return WERR_OK;
162 }
163
164 /**
165  * Save registry diff
166  */
167 _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
168                                    struct reg_diff_callbacks **callbacks,
169                                    void **callback_data)
170 {
171         struct preg_data *data;
172         struct {
173                 char hdr[4];
174                 uint32_t version;
175         } preg_header;
176
177
178         data = talloc_zero(ctx, struct preg_data);
179         *callback_data = data;
180
181         if (filename) {
182                 data->fd = open(filename, O_CREAT|O_WRONLY, 0755);
183                 if (data->fd < 0) {
184                         DEBUG(0, ("Unable to open %s\n", filename));
185                         return WERR_FILE_NOT_FOUND;
186                 }
187         } else {
188                 data->fd = STDOUT_FILENO;
189         }
190
191         memcpy(preg_header.hdr, "PReg", sizeof(preg_header.hdr));
192         SIVAL(&preg_header.version, 0, 1);
193         sys_write_v(data->fd, (uint8_t *)&preg_header, sizeof(preg_header));
194
195         data->ctx = ctx;
196
197         *callbacks = talloc(ctx, struct reg_diff_callbacks);
198
199         (*callbacks)->add_key = reg_preg_diff_add_key;
200         (*callbacks)->del_key = reg_preg_diff_del_key;
201         (*callbacks)->set_value = reg_preg_diff_set_value;
202         (*callbacks)->del_value = reg_preg_diff_del_value;
203         (*callbacks)->del_all_values = reg_preg_diff_del_all_values;
204         (*callbacks)->done = reg_preg_diff_done;
205
206         return WERR_OK;
207 }
208 /**
209  * Load diff file
210  */
211 _PUBLIC_ WERROR reg_preg_diff_load(int fd,
212                                    const struct reg_diff_callbacks *callbacks,
213                                    void *callback_data)
214 {
215         struct {
216                 char hdr[4];
217                 uint32_t version;
218         } preg_header;
219         char *buf;
220         size_t buf_size = 1024;
221         char *buf_ptr;
222         TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
223         WERROR ret = WERR_OK;
224
225         buf = talloc_array(mem_ctx, char, buf_size);
226         buf_ptr = buf;
227
228         /* Read first 8 bytes (the header) */
229         if (read(fd, &preg_header, sizeof(preg_header)) != sizeof(preg_header)) {
230                 DEBUG(0, ("Could not read PReg file: %s\n",
231                                 strerror(errno)));
232                 ret = WERR_GEN_FAILURE;
233                 goto cleanup;
234         }
235         preg_header.version = IVAL(&preg_header.version, 0);
236
237         if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
238                 DEBUG(0, ("This file is not a valid preg registry file\n"));
239                 ret = WERR_GEN_FAILURE;
240                 goto cleanup;
241         }
242         if (preg_header.version > 1) {
243                 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
244         }
245
246         /* Read the entries */
247         while(1) {
248                 uint32_t value_type, length;
249                 char *key = NULL;
250                 char *value_name = NULL;
251                 DATA_BLOB data = {NULL, 0};
252
253                 if (!W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr))) {
254                         break;
255                 }
256                 if (*buf_ptr != '[') {
257                         DEBUG(0, ("Error in PReg file.\n"));
258                         ret = WERR_GEN_FAILURE;
259                         goto cleanup;
260                 }
261
262                 /* Get the path */
263                 buf_ptr = buf;
264                 while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
265                        *buf_ptr != ';' && buf_ptr-buf < buf_size) {
266                         buf_ptr++;
267                 }
268                 buf[buf_ptr-buf] = '\0';
269                 key = talloc_strdup(mem_ctx, buf);
270
271                 /* Get the name */
272                 buf_ptr = buf;
273                 while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
274                        *buf_ptr != ';' && buf_ptr-buf < buf_size) {
275                         buf_ptr++;
276                 }
277                 buf[buf_ptr-buf] = '\0';
278                 value_name = talloc_strdup(mem_ctx, buf);
279
280                 /* Get the type */
281                 if (read(fd, &value_type, sizeof(uint32_t)) < sizeof(uint32_t)) {
282                         DEBUG(0, ("Error while reading PReg\n"));
283                         ret = WERR_GEN_FAILURE;
284                         goto cleanup;
285                 }
286                 value_type = IVAL(&value_type, 0);
287
288                 /* Read past delimiter */
289                 buf_ptr = buf;
290                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
291                     *buf_ptr == ';') && buf_ptr-buf < buf_size) {
292                         DEBUG(0, ("Error in PReg file.\n"));
293                         ret = WERR_GEN_FAILURE;
294                         goto cleanup;
295                 }
296
297                 /* Get data length */
298                 if (read(fd, &length, sizeof(uint32_t)) < sizeof(uint32_t)) {
299                         DEBUG(0, ("Error while reading PReg\n"));
300                         ret = WERR_GEN_FAILURE;
301                         goto cleanup;
302                 }
303                 length = IVAL(&length, 0);
304
305                 /* Read past delimiter */
306                 buf_ptr = buf;
307                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
308                     *buf_ptr == ';') && buf_ptr-buf < buf_size) {
309                         DEBUG(0, ("Error in PReg file.\n"));
310                         ret = WERR_GEN_FAILURE;
311                         goto cleanup;
312                 }
313
314                 /* Get the data */
315                 buf_ptr = buf;
316                 if (length < buf_size &&
317                     read(fd, buf_ptr, length) != length) {
318                         DEBUG(0, ("Error while reading PReg\n"));
319                         ret = WERR_GEN_FAILURE;
320                         goto cleanup;
321                 }
322                 data = data_blob_talloc(mem_ctx, buf, length);
323
324                 /* Check if delimiter is in place (whine if it isn't) */
325                 buf_ptr = buf;
326                 if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
327                     *buf_ptr == ']') && buf_ptr-buf < buf_size) {
328                         DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",
329                                 *buf_ptr, *buf_ptr));
330                 }
331
332                 if (strcasecmp(value_name, "**DelVals") == 0) {
333                         callbacks->del_all_values(callback_data, key);
334                 } else if (strncasecmp(value_name, "**Del.",6) == 0) {
335                         char *p = value_name+6;
336
337                         callbacks->del_value(callback_data, key, p);
338                 } else  if (strcasecmp(value_name, "**DeleteValues") == 0) {
339                         char *p, *q;
340
341                         p = (char *) data.data;
342
343                         while ((q = strchr_m(p, ';'))) {
344                                 *q = '\0';
345                                 q++;
346
347                                 callbacks->del_value(callback_data, key, p);
348
349                                 p = q;
350                         }
351                         callbacks->del_value(callback_data, key, p);
352                 } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
353                         char *p, *q, *full_key;
354
355                         p = (char *) data.data;
356
357                         while ((q = strchr_m(p, ';'))) {
358                                 *q = '\0';
359                                 q++;
360
361                                 full_key = talloc_asprintf(mem_ctx, "%s\\%s",
362                                                            key, p);
363                                 callbacks->del_key(callback_data, full_key);
364                                 talloc_free(full_key);
365
366                                 p = q;
367                         }
368                         full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
369                         callbacks->del_key(callback_data, full_key);
370                         talloc_free(full_key);
371                 } else {
372                         callbacks->add_key(callback_data, key);
373                         callbacks->set_value(callback_data, key, value_name,
374                                              value_type, data);
375                 }
376                 TALLOC_FREE(key);
377                 TALLOC_FREE(value_name);
378                 data_blob_free(&data);
379         }
380 cleanup:
381         close(fd);
382         TALLOC_FREE(mem_ctx);
383         return ret;
384 }