Convert some more files to GPLv3.
[jra/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
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 "lib/registry/patchfile.h"
25 #include "system/filesys.h"
26 #include "param/param.h"
27
28 struct preg_data {
29         int fd;
30 };
31
32 static WERROR preg_read_utf16(struct smb_iconv_convenience *ic, int fd, char *c)
33 {
34         uint16_t v;
35
36         if (read(fd, &v, 2) < 2) {
37                 return WERR_GENERAL_FAILURE;
38         }
39         push_codepoint(ic, c, v);
40         return WERR_OK;
41 }
42
43 /* FIXME These functions need to be implemented */
44 static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
45 {
46         return WERR_OK;
47 }
48
49 static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
50 {
51         return WERR_OK;
52 }
53
54 static WERROR reg_preg_diff_set_value(void *_data, const char *key_name,
55                                       const char *value_name,
56                                       uint32_t value_type, DATA_BLOB value_data)
57 {
58         return WERR_OK;
59 }
60
61 static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
62                                       const char *value_name)
63 {
64         return WERR_OK;
65 }
66
67 static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
68 {
69         return WERR_OK;
70 }
71
72 static WERROR reg_preg_diff_done(void *_data)
73 {
74         struct preg_data *data = (struct preg_data *)_data;
75
76         close(data->fd);
77         talloc_free(data);
78         return WERR_OK;
79 }
80
81 /**
82  * Save registry diff
83  */
84 _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
85                                    struct reg_diff_callbacks **callbacks,
86                                    void **callback_data)
87 {
88         struct preg_data *data;
89         struct {
90                 char hdr[4];
91                 uint32_t version;
92         } preg_header;
93
94
95         data = talloc_zero(ctx, struct preg_data);
96         *callback_data = data;
97
98         if (filename) {
99                 data->fd = open(filename, O_CREAT, 0755);
100                 if (data->fd == -1) {
101                         DEBUG(0, ("Unable to open %s\n", filename));
102                         return WERR_BADFILE;
103                 }
104         } else {
105                 data->fd = STDOUT_FILENO;
106         }
107         snprintf(preg_header.hdr, 4, "PReg");
108         SIVAL(&preg_header, 4, 1);
109         write(data->fd, (uint8_t *)&preg_header,8);
110
111         *callbacks = talloc(ctx, struct reg_diff_callbacks);
112
113         (*callbacks)->add_key = reg_preg_diff_add_key;
114         (*callbacks)->del_key = reg_preg_diff_del_key;
115         (*callbacks)->set_value = reg_preg_diff_set_value;
116         (*callbacks)->del_value = reg_preg_diff_del_value;
117         (*callbacks)->del_all_values = reg_preg_diff_del_all_values;
118         (*callbacks)->done = reg_preg_diff_done;
119
120         return WERR_OK;
121 }
122 /**
123  * Load diff file
124  */
125 _PUBLIC_ WERROR reg_preg_diff_load(int fd,
126                                    struct smb_iconv_convenience *iconv_convenience, 
127                                    const struct reg_diff_callbacks *callbacks,
128                                    void *callback_data)
129 {
130         struct {
131                 char hdr[4];
132                 uint32_t version;
133         } preg_header;
134         char *buf;
135         size_t buf_size = 1024;
136         char *buf_ptr;
137         TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
138         WERROR ret = WERR_OK;
139         DATA_BLOB data = {NULL, 0};
140         char *key = NULL;
141         char *value_name = NULL;
142
143         buf = talloc_array(mem_ctx, char, buf_size);
144         buf_ptr = buf;
145
146         /* Read first 8 bytes (the header) */
147         if (read(fd, &preg_header, 8) != 8) {
148                 DEBUG(0, ("Could not read PReg file: %s\n",
149                                 strerror(errno)));
150                 ret = WERR_GENERAL_FAILURE;
151                 goto cleanup;
152         }
153         if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
154                 DEBUG(0, ("This file is not a valid preg registry file\n"));
155                 ret = WERR_GENERAL_FAILURE;
156                 goto cleanup;
157         }
158         if (preg_header.version > 1) {
159                 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
160         }
161
162         /* Read the entries */
163         while(1) {
164                 uint32_t value_type, length;
165
166                 if (!W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr))) {
167                         break;
168                 }
169                 if (*buf_ptr != '[') {
170                         DEBUG(0, ("Error in PReg file.\n"));
171                         ret = WERR_GENERAL_FAILURE;
172                         goto cleanup;
173                 }
174
175                 /* Get the path */
176                 buf_ptr = buf;
177                 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
178                        *buf_ptr != ';' && buf_ptr-buf < buf_size) {
179                         buf_ptr++;
180                 }
181                 key = talloc_asprintf(mem_ctx, "\\%s", buf);
182
183                 /* Get the name */
184                 buf_ptr = buf;
185                 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
186                        *buf_ptr != ';' && buf_ptr-buf < buf_size) {
187                         buf_ptr++;
188                 }
189                 value_name = talloc_strdup(mem_ctx, buf);
190
191                 /* Get the type */
192                 if (read(fd, &value_type, 4) < 4) {
193                         DEBUG(0, ("Error while reading PReg\n"));
194                         ret = WERR_GENERAL_FAILURE;
195                         goto cleanup;
196                 }
197                 /* Read past delimiter */
198                 buf_ptr = buf;
199                 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
200                     *buf_ptr == ';') && buf_ptr-buf < buf_size) {
201                         DEBUG(0, ("Error in PReg file.\n"));
202                         ret = WERR_GENERAL_FAILURE;
203                         goto cleanup;
204                 }
205                 /* Get data length */
206                 if (read(fd, &length, 4) < 4) {
207                         DEBUG(0, ("Error while reading PReg\n"));
208                         ret = WERR_GENERAL_FAILURE;
209                         goto cleanup;
210                 }
211                 /* Read past delimiter */
212                 buf_ptr = buf;
213                 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
214                     *buf_ptr == ';') && buf_ptr-buf < buf_size) {
215                         DEBUG(0, ("Error in PReg file.\n"));
216                         ret = WERR_GENERAL_FAILURE;
217                         goto cleanup;
218                 }
219                 /* Get the data */
220                 buf_ptr = buf;
221                 if (length < buf_size &&
222                     read(fd, buf_ptr, length) != length) {
223                         DEBUG(0, ("Error while reading PReg\n"));
224                         ret = WERR_GENERAL_FAILURE;
225                         goto cleanup;
226                 }
227                 data = data_blob_talloc(mem_ctx, buf, length);
228
229                 /* Check if delimiter is in place (whine if it isn't) */
230                 buf_ptr = buf;
231                 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
232                     *buf_ptr == ']') && buf_ptr-buf < buf_size) {
233                         DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",
234                                 *buf_ptr, *buf_ptr));
235                 }
236
237                 if (strcasecmp(value_name, "**DelVals") == 0) {
238                         callbacks->del_all_values(callback_data, key);
239                 } else if (strncasecmp(value_name, "**Del.",6) == 0) {
240                         char *p = value_name+6;
241
242                         callbacks->del_value(callback_data, key, p);
243                 } else  if (strcasecmp(value_name, "**DeleteValues") == 0) {
244                         char *p, *q;
245
246                         p = (char *) data.data;
247
248                         while ((q = strchr_m(p, ';'))) {
249                                 *q = '\0';
250                                 q++;
251
252                                 callbacks->del_value(callback_data, key, p);
253
254                                 p = q;
255                         }
256                         callbacks->del_value(callback_data, key, p);
257                 } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
258                         char *p, *q, *full_key;
259
260                         p = (char *) data.data;
261
262                         while ((q = strchr_m(p, ';'))) {
263                                 *q = '\0';
264                                 q++;
265
266                                 full_key = talloc_asprintf(mem_ctx, "%s\\%s",
267                                                            key, p);
268                                 callbacks->del_key(callback_data, full_key);
269                                 talloc_free(full_key);
270
271                                 p = q;
272                         }
273                         full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
274                         callbacks->del_key(callback_data, full_key);
275                         talloc_free(full_key);
276                 } else {
277                         callbacks->add_key(callback_data, key);
278                         callbacks->set_value(callback_data, key, value_name,
279                                              value_type, data);
280                 }
281         }
282 cleanup:
283         close(fd);
284         talloc_free(data.data);
285         talloc_free(key);
286         talloc_free(value_name);
287         talloc_free(buf);
288         return ret;
289 }