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 2 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.
23 #include "lib/registry/registry.h"
24 #include "lib/registry/patchfile.h"
25 #include "system/filesys.h"
26 #include "param/param.h"
32 static WERROR preg_read_utf16(struct smb_iconv_convenience *ic, int fd, char *c)
36 if (read(fd, &v, 2) < 2) {
37 return WERR_GENERAL_FAILURE;
39 push_codepoint(ic, c, v);
43 /* FIXME These functions need to be implemented */
44 static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
49 static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
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)
61 static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
62 const char *value_name)
67 static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
72 static WERROR reg_preg_diff_done(void *_data)
74 struct preg_data *data = (struct preg_data *)_data;
84 _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
85 struct reg_diff_callbacks **callbacks,
88 struct preg_data *data;
95 data = talloc_zero(ctx, struct preg_data);
96 *callback_data = data;
99 data->fd = open(filename, O_CREAT, 0755);
100 if (data->fd == -1) {
101 DEBUG(0, ("Unable to open %s\n", filename));
105 data->fd = STDOUT_FILENO;
107 snprintf(preg_header.hdr, 4, "PReg");
108 SIVAL(&preg_header, 4, 1);
109 write(data->fd, (uint8_t *)&preg_header,8);
111 *callbacks = talloc(ctx, struct reg_diff_callbacks);
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;
125 _PUBLIC_ WERROR reg_preg_diff_load(int fd,
126 struct smb_iconv_convenience *iconv_convenience,
127 const struct reg_diff_callbacks *callbacks,
135 size_t buf_size = 1024;
137 TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
138 WERROR ret = WERR_OK;
139 DATA_BLOB data = {NULL, 0};
141 char *value_name = NULL;
143 buf = talloc_array(mem_ctx, char, buf_size);
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",
150 ret = WERR_GENERAL_FAILURE;
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;
158 if (preg_header.version > 1) {
159 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
162 /* Read the entries */
164 uint32_t value_type, length;
166 if (!W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr))) {
169 if (*buf_ptr != '[') {
170 DEBUG(0, ("Error in PReg file.\n"));
171 ret = WERR_GENERAL_FAILURE;
177 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
178 *buf_ptr != ';' && buf_ptr-buf < buf_size) {
181 key = talloc_asprintf(mem_ctx, "\\%s", buf);
185 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience, fd, buf_ptr)) &&
186 *buf_ptr != ';' && buf_ptr-buf < buf_size) {
189 value_name = talloc_strdup(mem_ctx, buf);
192 if (read(fd, &value_type, 4) < 4) {
193 DEBUG(0, ("Error while reading PReg\n"));
194 ret = WERR_GENERAL_FAILURE;
197 /* Read past delimiter */
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;
205 /* Get data length */
206 if (read(fd, &length, 4) < 4) {
207 DEBUG(0, ("Error while reading PReg\n"));
208 ret = WERR_GENERAL_FAILURE;
211 /* Read past delimiter */
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;
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;
227 data = data_blob_talloc(mem_ctx, buf, length);
229 /* Check if delimiter is in place (whine if it isn't) */
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));
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;
242 callbacks->del_value(callback_data, key, p);
243 } else if (strcasecmp(value_name, "**DeleteValues") == 0) {
246 p = (char *) data.data;
248 while ((q = strchr_m(p, ';'))) {
252 callbacks->del_value(callback_data, key, p);
256 callbacks->del_value(callback_data, key, p);
257 } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
258 char *p, *q, *full_key;
260 p = (char *) data.data;
262 while ((q = strchr_m(p, ';'))) {
266 full_key = talloc_asprintf(mem_ctx, "%s\\%s",
268 callbacks->del_key(callback_data, full_key);
269 talloc_free(full_key);
273 full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
274 callbacks->del_key(callback_data, full_key);
275 talloc_free(full_key);
277 callbacks->add_key(callback_data, key);
278 callbacks->set_value(callback_data, key, value_name,
284 talloc_free(data.data);
286 talloc_free(value_name);