Fix PReg write support.
[Samba.git] / source4 / lib / registry / patchfile_preg.c
blobea41a9e21bbd49519cf2743ed487ad0c1aa28ead
1 /*
2 Unix SMB/CIFS implementation.
3 Reading Registry.pol PReg registry files
5 Copyright (C) Wilco Baan Hofman 2006-2008
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.
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.
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.
22 #include "includes.h"
23 #include "lib/registry/registry.h"
24 #include "system/filesys.h"
25 #include "param/param.h"
26 #include "librpc/gen_ndr/winreg.h"
28 struct preg_data {
29 int fd;
30 TALLOC_CTX *ctx;
31 struct smb_iconv_convenience *ic;
34 static WERROR preg_read_utf16(struct smb_iconv_convenience *ic, int fd, char *c)
36 uint16_t v;
38 if (read(fd, &v, 2) < 2) {
39 return WERR_GENERAL_FAILURE;
41 push_codepoint(ic, c, v);
42 return WERR_OK;
44 static WERROR preg_write_utf16(struct smb_iconv_convenience *ic, int fd, const char *string)
46 codepoint_t v;
47 uint16_t i;
48 size_t size;
50 for (i = 0; i < strlen(string); i+=size) {
51 v = next_codepoint(ic, &string[i], &size);
52 if (write(fd, &v, 2) < 2) {
53 return WERR_GENERAL_FAILURE;
56 return WERR_OK;
58 /* PReg does not support adding keys. */
59 static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
61 return WERR_OK;
64 static WERROR reg_preg_diff_set_value(void *_data, const char *key_name,
65 const char *value_name,
66 uint32_t value_type, DATA_BLOB value_data)
68 struct preg_data *data = _data;
69 uint32_t buf;
71 preg_write_utf16(data->ic, data->fd, "[");
72 preg_write_utf16(data->ic, data->fd, key_name);
73 preg_write_utf16(data->ic, data->fd, ";");
74 preg_write_utf16(data->ic, data->fd, value_name);
75 preg_write_utf16(data->ic, data->fd, ";");
76 SIVAL(&buf, 0, value_type);
77 write(data->fd, &buf, sizeof(uint32_t));
78 preg_write_utf16(data->ic, data->fd, ";");
79 SIVAL(&buf, 0, value_data.length);
80 write(data->fd, &buf, sizeof(uint32_t));
81 preg_write_utf16(data->ic, data->fd, ";");
82 write(data->fd, value_data.data, value_data.length);
83 preg_write_utf16(data->ic, data->fd, "]");
85 return WERR_OK;
88 static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
90 struct preg_data *data = _data;
91 char *parent_name;
92 DATA_BLOB blob;
94 parent_name = talloc_strndup(data->ctx, key_name, strrchr(key_name, '\\')-key_name);
95 blob.data = (void *)talloc_strndup(data->ctx, key_name,
96 strlen(key_name)-(strrchr(key_name, '\\')-key_name));
97 blob.length = strlen((char *)blob.data)+1;
100 /* FIXME: These values should be accumulated to be written at done(). */
101 reg_preg_diff_set_value(_data, parent_name, "**DeleteKeys", REG_SZ, blob);
103 return WERR_OK;
106 /* FIXME These functions need to be implemented */
107 static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
108 const char *value_name)
110 return WERR_NOT_SUPPORTED;
113 static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
115 return WERR_NOT_SUPPORTED;
118 static WERROR reg_preg_diff_done(void *_data)
120 struct preg_data *data = (struct preg_data *)_data;
122 close(data->fd);
123 talloc_free(data);
124 return WERR_OK;
128 * Save registry diff
130 _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
131 struct smb_iconv_convenience *ic,
132 struct reg_diff_callbacks **callbacks,
133 void **callback_data)
135 struct preg_data *data;
136 struct {
137 char hdr[4];
138 uint32_t version;
139 } preg_header;
142 data = talloc_zero(ctx, struct preg_data);
143 *callback_data = data;
145 if (filename) {
146 data->fd = open(filename, O_CREAT|O_WRONLY, 0755);
147 if (data->fd < 0) {
148 DEBUG(0, ("Unable to open %s\n", filename));
149 return WERR_BADFILE;
151 } else {
152 data->fd = STDOUT_FILENO;
155 memcpy(preg_header.hdr, "PReg", 4);
156 SIVAL(&preg_header, 4, 1);
157 write(data->fd, (uint8_t *)&preg_header,8);
159 data->ctx = ctx;
160 data->ic = ic;
162 *callbacks = talloc(ctx, struct reg_diff_callbacks);
164 (*callbacks)->add_key = reg_preg_diff_add_key;
165 (*callbacks)->del_key = reg_preg_diff_del_key;
166 (*callbacks)->set_value = reg_preg_diff_set_value;
167 (*callbacks)->del_value = reg_preg_diff_del_value;
168 (*callbacks)->del_all_values = reg_preg_diff_del_all_values;
169 (*callbacks)->done = reg_preg_diff_done;
171 return WERR_OK;
174 * Load diff file
176 _PUBLIC_ WERROR reg_preg_diff_load(int fd,
177 struct smb_iconv_convenience *iconv_convenience,
178 const struct reg_diff_callbacks *callbacks,
179 void *callback_data)
181 struct {
182 char hdr[4];
183 uint32_t version;
184 } preg_header;
185 char *buf;
186 size_t buf_size = 1024;
187 char *buf_ptr;
188 TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
189 WERROR ret = WERR_OK;
190 DATA_BLOB data = {NULL, 0};
191 char *key = NULL;
192 char *value_name = NULL;
194 buf = talloc_array(mem_ctx, char, buf_size);
195 buf_ptr = buf;
197 /* Read first 8 bytes (the header) */
198 if (read(fd, &preg_header, 8) != 8) {
199 DEBUG(0, ("Could not read PReg file: %s\n",
200 strerror(errno)));
201 ret = WERR_GENERAL_FAILURE;
202 goto cleanup;
204 preg_header.version = IVAL(&preg_header.version, 0);
206 if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
207 DEBUG(0, ("This file is not a valid preg registry file\n"));
208 ret = WERR_GENERAL_FAILURE;
209 goto cleanup;
211 if (preg_header.version > 1) {
212 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
215 /* Read the entries */
216 while(1) {
217 uint32_t value_type, length;
219 if (!W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr))) {
220 break;
222 if (*buf_ptr != '[') {
223 DEBUG(0, ("Error in PReg file.\n"));
224 ret = WERR_GENERAL_FAILURE;
225 goto cleanup;
228 /* Get the path */
229 buf_ptr = buf;
230 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
231 *buf_ptr != ';' && buf_ptr-buf < buf_size) {
232 buf_ptr++;
234 buf[buf_ptr-buf] = '\0';
235 key = talloc_strdup(mem_ctx, buf);
237 /* Get the name */
238 buf_ptr = buf;
239 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
240 *buf_ptr != ';' && buf_ptr-buf < buf_size) {
241 buf_ptr++;
243 buf[buf_ptr-buf] = '\0';
244 value_name = talloc_strdup(mem_ctx, buf);
246 /* Get the type */
247 if (read(fd, &value_type, 4) < 4) {
248 DEBUG(0, ("Error while reading PReg\n"));
249 ret = WERR_GENERAL_FAILURE;
250 goto cleanup;
252 value_type = IVAL(&value_type, 0);
254 /* Read past delimiter */
255 buf_ptr = buf;
256 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
257 *buf_ptr == ';') && buf_ptr-buf < buf_size) {
258 DEBUG(0, ("Error in PReg file.\n"));
259 ret = WERR_GENERAL_FAILURE;
260 goto cleanup;
262 /* Get data length */
263 if (read(fd, &length, 4) < 4) {
264 DEBUG(0, ("Error while reading PReg\n"));
265 ret = WERR_GENERAL_FAILURE;
266 goto cleanup;
268 /* Read past delimiter */
269 buf_ptr = buf;
270 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
271 *buf_ptr == ';') && buf_ptr-buf < buf_size) {
272 DEBUG(0, ("Error in PReg file.\n"));
273 ret = WERR_GENERAL_FAILURE;
274 goto cleanup;
276 /* Get the data */
277 buf_ptr = buf;
278 if (length < buf_size &&
279 read(fd, buf_ptr, length) != length) {
280 DEBUG(0, ("Error while reading PReg\n"));
281 ret = WERR_GENERAL_FAILURE;
282 goto cleanup;
284 data = data_blob_talloc(mem_ctx, buf, length);
286 /* Check if delimiter is in place (whine if it isn't) */
287 buf_ptr = buf;
288 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
289 *buf_ptr == ']') && buf_ptr-buf < buf_size) {
290 DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",
291 *buf_ptr, *buf_ptr));
294 if (strcasecmp(value_name, "**DelVals") == 0) {
295 callbacks->del_all_values(callback_data, key);
296 } else if (strncasecmp(value_name, "**Del.",6) == 0) {
297 char *p = value_name+6;
299 callbacks->del_value(callback_data, key, p);
300 } else if (strcasecmp(value_name, "**DeleteValues") == 0) {
301 char *p, *q;
303 p = (char *) data.data;
305 while ((q = strchr_m(p, ';'))) {
306 *q = '\0';
307 q++;
309 callbacks->del_value(callback_data, key, p);
311 p = q;
313 callbacks->del_value(callback_data, key, p);
314 } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
315 char *p, *q, *full_key;
317 p = (char *) data.data;
319 while ((q = strchr_m(p, ';'))) {
320 *q = '\0';
321 q++;
323 full_key = talloc_asprintf(mem_ctx, "%s\\%s",
324 key, p);
325 callbacks->del_key(callback_data, full_key);
326 talloc_free(full_key);
328 p = q;
330 full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
331 callbacks->del_key(callback_data, full_key);
332 talloc_free(full_key);
333 } else {
334 callbacks->add_key(callback_data, key);
335 callbacks->set_value(callback_data, key, value_name,
336 value_type, data);
339 cleanup:
340 close(fd);
341 talloc_free(data.data);
342 talloc_free(key);
343 talloc_free(value_name);
344 talloc_free(buf);
345 return ret;