Attempt to fix the patchfile_preg backend for big endian machines.
[Samba.git] / source4 / lib / registry / patchfile_preg.c
blob5216a04c8b63fd0896a47127c1cd598d18a9e606
1 /*
2 Unix SMB/CIFS implementation.
3 Reading Registry.pol PReg registry files
5 Copyright (C) Wilco Baan Hofman 2006
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"
27 struct preg_data {
28 int fd;
31 static WERROR preg_read_utf16(struct smb_iconv_convenience *ic, int fd, char *c)
33 uint16_t v;
35 if (read(fd, &v, 2) < 2) {
36 return WERR_GENERAL_FAILURE;
38 push_codepoint(ic, c, v);
39 return WERR_OK;
42 /* FIXME These functions need to be implemented */
43 static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
45 return WERR_NOT_SUPPORTED;
48 static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
50 return WERR_NOT_SUPPORTED;
53 static WERROR reg_preg_diff_set_value(void *_data, const char *key_name,
54 const char *value_name,
55 uint32_t value_type, DATA_BLOB value_data)
57 return WERR_NOT_SUPPORTED;
60 static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
61 const char *value_name)
63 return WERR_NOT_SUPPORTED;
66 static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
68 return WERR_NOT_SUPPORTED;
71 static WERROR reg_preg_diff_done(void *_data)
73 struct preg_data *data = (struct preg_data *)_data;
75 close(data->fd);
76 talloc_free(data);
77 return WERR_OK;
80 /**
81 * Save registry diff
83 _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
84 struct reg_diff_callbacks **callbacks,
85 void **callback_data)
87 struct preg_data *data;
88 struct {
89 char hdr[4];
90 uint32_t version;
91 } preg_header;
94 data = talloc_zero(ctx, struct preg_data);
95 *callback_data = data;
97 if (filename) {
98 data->fd = open(filename, O_CREAT, 0755);
99 if (data->fd == -1) {
100 DEBUG(0, ("Unable to open %s\n", filename));
101 return WERR_BADFILE;
103 } else {
104 data->fd = STDOUT_FILENO;
106 snprintf(preg_header.hdr, 4, "PReg");
107 SIVAL(&preg_header, 4, 1);
108 write(data->fd, (uint8_t *)&preg_header,8);
110 *callbacks = talloc(ctx, struct reg_diff_callbacks);
112 (*callbacks)->add_key = reg_preg_diff_add_key;
113 (*callbacks)->del_key = reg_preg_diff_del_key;
114 (*callbacks)->set_value = reg_preg_diff_set_value;
115 (*callbacks)->del_value = reg_preg_diff_del_value;
116 (*callbacks)->del_all_values = reg_preg_diff_del_all_values;
117 (*callbacks)->done = reg_preg_diff_done;
119 return WERR_OK;
122 * Load diff file
124 _PUBLIC_ WERROR reg_preg_diff_load(int fd,
125 struct smb_iconv_convenience *iconv_convenience,
126 const struct reg_diff_callbacks *callbacks,
127 void *callback_data)
129 struct {
130 char hdr[4];
131 uint32_t version;
132 } preg_header;
133 char *buf;
134 size_t buf_size = 1024;
135 char *buf_ptr;
136 TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
137 WERROR ret = WERR_OK;
138 DATA_BLOB data = {NULL, 0};
139 char *key = NULL;
140 char *value_name = NULL;
142 buf = talloc_array(mem_ctx, char, buf_size);
143 buf_ptr = buf;
145 /* Read first 8 bytes (the header) */
146 if (read(fd, &preg_header, 8) != 8) {
147 DEBUG(0, ("Could not read PReg file: %s\n",
148 strerror(errno)));
149 ret = WERR_GENERAL_FAILURE;
150 goto cleanup;
152 preg_header.version = IVAL(&preg_header.version, 0);
154 if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
155 DEBUG(0, ("This file is not a valid preg registry file\n"));
156 ret = WERR_GENERAL_FAILURE;
157 goto cleanup;
159 if (preg_header.version > 1) {
160 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
163 /* Read the entries */
164 while(1) {
165 uint32_t value_type, length;
167 if (!W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr))) {
168 break;
170 if (*buf_ptr != '[') {
171 DEBUG(0, ("Error in PReg file.\n"));
172 ret = WERR_GENERAL_FAILURE;
173 goto cleanup;
176 /* Get the path */
177 buf_ptr = buf;
178 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
179 *buf_ptr != ';' && buf_ptr-buf < buf_size) {
180 buf_ptr++;
182 key = talloc_asprintf(mem_ctx, "\\%s", buf);
184 /* Get the name */
185 buf_ptr = buf;
186 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
187 *buf_ptr != ';' && buf_ptr-buf < buf_size) {
188 buf_ptr++;
190 value_name = talloc_strdup(mem_ctx, buf);
192 /* Get the type */
193 if (read(fd, &value_type, 4) < 4) {
194 DEBUG(0, ("Error while reading PReg\n"));
195 ret = WERR_GENERAL_FAILURE;
196 goto cleanup;
198 value_type = IVAL(&value_type, 0);
200 /* Read past delimiter */
201 buf_ptr = buf;
202 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
203 *buf_ptr == ';') && buf_ptr-buf < buf_size) {
204 DEBUG(0, ("Error in PReg file.\n"));
205 ret = WERR_GENERAL_FAILURE;
206 goto cleanup;
208 /* Get data length */
209 if (read(fd, &length, 4) < 4) {
210 DEBUG(0, ("Error while reading PReg\n"));
211 ret = WERR_GENERAL_FAILURE;
212 goto cleanup;
214 /* Read past delimiter */
215 buf_ptr = buf;
216 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
217 *buf_ptr == ';') && buf_ptr-buf < buf_size) {
218 DEBUG(0, ("Error in PReg file.\n"));
219 ret = WERR_GENERAL_FAILURE;
220 goto cleanup;
222 /* Get the data */
223 buf_ptr = buf;
224 if (length < buf_size &&
225 read(fd, buf_ptr, length) != length) {
226 DEBUG(0, ("Error while reading PReg\n"));
227 ret = WERR_GENERAL_FAILURE;
228 goto cleanup;
230 data = data_blob_talloc(mem_ctx, buf, length);
232 /* Check if delimiter is in place (whine if it isn't) */
233 buf_ptr = buf;
234 if (!(W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
235 *buf_ptr == ']') && buf_ptr-buf < buf_size) {
236 DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",
237 *buf_ptr, *buf_ptr));
240 if (strcasecmp(value_name, "**DelVals") == 0) {
241 callbacks->del_all_values(callback_data, key);
242 } else if (strncasecmp(value_name, "**Del.",6) == 0) {
243 char *p = value_name+6;
245 callbacks->del_value(callback_data, key, p);
246 } else if (strcasecmp(value_name, "**DeleteValues") == 0) {
247 char *p, *q;
249 p = (char *) data.data;
251 while ((q = strchr_m(p, ';'))) {
252 *q = '\0';
253 q++;
255 callbacks->del_value(callback_data, key, p);
257 p = q;
259 callbacks->del_value(callback_data, key, p);
260 } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
261 char *p, *q, *full_key;
263 p = (char *) data.data;
265 while ((q = strchr_m(p, ';'))) {
266 *q = '\0';
267 q++;
269 full_key = talloc_asprintf(mem_ctx, "%s\\%s",
270 key, p);
271 callbacks->del_key(callback_data, full_key);
272 talloc_free(full_key);
274 p = q;
276 full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
277 callbacks->del_key(callback_data, full_key);
278 talloc_free(full_key);
279 } else {
280 callbacks->add_key(callback_data, key);
281 callbacks->set_value(callback_data, key, value_name,
282 value_type, data);
285 cleanup:
286 close(fd);
287 talloc_free(data.data);
288 talloc_free(key);
289 talloc_free(value_name);
290 talloc_free(buf);
291 return ret;