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.
23 #include "lib/registry/registry.h"
24 #include "system/filesys.h"
25 #include "param/param.h"
31 static WERROR
preg_read_utf16(struct smb_iconv_convenience
*ic
, int fd
, char *c
)
35 if (read(fd
, &v
, 2) < 2) {
36 return WERR_GENERAL_FAILURE
;
38 push_codepoint(ic
, c
, v
);
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
;
83 _PUBLIC_ WERROR
reg_preg_diff_save(TALLOC_CTX
*ctx
, const char *filename
,
84 struct reg_diff_callbacks
**callbacks
,
87 struct preg_data
*data
;
94 data
= talloc_zero(ctx
, struct preg_data
);
95 *callback_data
= data
;
98 data
->fd
= open(filename
, O_CREAT
, 0755);
100 DEBUG(0, ("Unable to open %s\n", filename
));
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
;
124 _PUBLIC_ WERROR
reg_preg_diff_load(int fd
,
125 struct smb_iconv_convenience
*iconv_convenience
,
126 const struct reg_diff_callbacks
*callbacks
,
134 size_t buf_size
= 1024;
136 TALLOC_CTX
*mem_ctx
= talloc_init("reg_preg_diff_load");
137 WERROR ret
= WERR_OK
;
138 DATA_BLOB data
= {NULL
, 0};
140 char *value_name
= NULL
;
142 buf
= talloc_array(mem_ctx
, char, buf_size
);
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",
149 ret
= WERR_GENERAL_FAILURE
;
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
;
159 if (preg_header
.version
> 1) {
160 DEBUG(0, ("Warning: file format version is higher than expected.\n"));
163 /* Read the entries */
165 uint32_t value_type
, length
;
167 if (!W_ERROR_IS_OK(preg_read_utf16(iconv_convenience
, fd
, buf_ptr
))) {
170 if (*buf_ptr
!= '[') {
171 DEBUG(0, ("Error in PReg file.\n"));
172 ret
= WERR_GENERAL_FAILURE
;
178 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience
, fd
, buf_ptr
)) &&
179 *buf_ptr
!= ';' && buf_ptr
-buf
< buf_size
) {
182 key
= talloc_asprintf(mem_ctx
, "\\%s", buf
);
186 while (W_ERROR_IS_OK(preg_read_utf16(iconv_convenience
, fd
, buf_ptr
)) &&
187 *buf_ptr
!= ';' && buf_ptr
-buf
< buf_size
) {
190 value_name
= talloc_strdup(mem_ctx
, buf
);
193 if (read(fd
, &value_type
, 4) < 4) {
194 DEBUG(0, ("Error while reading PReg\n"));
195 ret
= WERR_GENERAL_FAILURE
;
198 value_type
= IVAL(&value_type
, 0);
200 /* Read past delimiter */
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
;
208 /* Get data length */
209 if (read(fd
, &length
, 4) < 4) {
210 DEBUG(0, ("Error while reading PReg\n"));
211 ret
= WERR_GENERAL_FAILURE
;
214 /* Read past delimiter */
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
;
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
;
230 data
= data_blob_talloc(mem_ctx
, buf
, length
);
232 /* Check if delimiter is in place (whine if it isn't) */
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) {
249 p
= (char *) data
.data
;
251 while ((q
= strchr_m(p
, ';'))) {
255 callbacks
->del_value(callback_data
, key
, p
);
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
, ';'))) {
269 full_key
= talloc_asprintf(mem_ctx
, "%s\\%s",
271 callbacks
->del_key(callback_data
, full_key
);
272 talloc_free(full_key
);
276 full_key
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, p
);
277 callbacks
->del_key(callback_data
, full_key
);
278 talloc_free(full_key
);
280 callbacks
->add_key(callback_data
, key
);
281 callbacks
->set_value(callback_data
, key
, value_name
,
287 talloc_free(data
.data
);
289 talloc_free(value_name
);