1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Reading Registry.pol PReg registry files
|
---|
4 |
|
---|
5 | Copyright (C) Wilco Baan Hofman 2006-2008
|
---|
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 "system/filesys.h"
|
---|
25 | #include "librpc/gen_ndr/winreg.h"
|
---|
26 |
|
---|
27 | struct preg_data {
|
---|
28 | int fd;
|
---|
29 | TALLOC_CTX *ctx;
|
---|
30 | };
|
---|
31 |
|
---|
32 | static WERROR preg_read_utf16(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(c, v);
|
---|
40 | return WERR_OK;
|
---|
41 | }
|
---|
42 | static WERROR preg_write_utf16(int fd, const char *string)
|
---|
43 | {
|
---|
44 | codepoint_t v;
|
---|
45 | uint16_t i;
|
---|
46 | size_t size;
|
---|
47 |
|
---|
48 | for (i = 0; i < strlen(string); i+=size) {
|
---|
49 | v = next_codepoint(&string[i], &size);
|
---|
50 | if (write(fd, &v, 2) < 2) {
|
---|
51 | return WERR_GENERAL_FAILURE;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return WERR_OK;
|
---|
55 | }
|
---|
56 | /* PReg does not support adding keys. */
|
---|
57 | static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
|
---|
58 | {
|
---|
59 | return WERR_OK;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static WERROR reg_preg_diff_set_value(void *_data, const char *key_name,
|
---|
63 | const char *value_name,
|
---|
64 | uint32_t value_type, DATA_BLOB value_data)
|
---|
65 | {
|
---|
66 | struct preg_data *data = (struct preg_data *)_data;
|
---|
67 | uint32_t buf;
|
---|
68 |
|
---|
69 | preg_write_utf16(data->fd, "[");
|
---|
70 | preg_write_utf16(data->fd, key_name);
|
---|
71 | preg_write_utf16(data->fd, ";");
|
---|
72 | preg_write_utf16(data->fd, value_name);
|
---|
73 | preg_write_utf16(data->fd, ";");
|
---|
74 | SIVAL(&buf, 0, value_type);
|
---|
75 | write(data->fd, &buf, sizeof(uint32_t));
|
---|
76 | preg_write_utf16(data->fd, ";");
|
---|
77 | SIVAL(&buf, 0, value_data.length);
|
---|
78 | write(data->fd, &buf, sizeof(uint32_t));
|
---|
79 | preg_write_utf16(data->fd, ";");
|
---|
80 | write(data->fd, value_data.data, value_data.length);
|
---|
81 | preg_write_utf16(data->fd, "]");
|
---|
82 |
|
---|
83 | return WERR_OK;
|
---|
84 | }
|
---|
85 |
|
---|
86 | static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
|
---|
87 | {
|
---|
88 | struct preg_data *data = (struct preg_data *)_data;
|
---|
89 | char *parent_name;
|
---|
90 | DATA_BLOB blob;
|
---|
91 |
|
---|
92 | parent_name = talloc_strndup(data->ctx, key_name, strrchr(key_name, '\\')-key_name);
|
---|
93 | blob.data = (uint8_t *)talloc_strndup(data->ctx, key_name+(strrchr(key_name, '\\')-key_name)+1,
|
---|
94 | strlen(key_name)-(strrchr(key_name, '\\')-key_name));
|
---|
95 | blob.length = strlen((char *)blob.data)+1;
|
---|
96 |
|
---|
97 |
|
---|
98 | /* FIXME: These values should be accumulated to be written at done(). */
|
---|
99 | return reg_preg_diff_set_value(data, parent_name, "**DeleteKeys", REG_SZ, blob);
|
---|
100 | }
|
---|
101 |
|
---|
102 | static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
|
---|
103 | const char *value_name)
|
---|
104 | {
|
---|
105 | struct preg_data *data = (struct preg_data *)_data;
|
---|
106 | char *val;
|
---|
107 | DATA_BLOB blob;
|
---|
108 |
|
---|
109 | val = talloc_asprintf(data->ctx, "**Del.%s", value_name);
|
---|
110 |
|
---|
111 | blob.data = (uint8_t *)talloc(data->ctx, uint32_t);
|
---|
112 | *(uint32_t *)blob.data = 0;
|
---|
113 | blob.length = 4;
|
---|
114 | return reg_preg_diff_set_value(data, key_name, val, REG_DWORD, blob);
|
---|
115 | }
|
---|
116 |
|
---|
117 | static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
|
---|
118 | {
|
---|
119 | struct preg_data *data = (struct preg_data *)_data;
|
---|
120 | DATA_BLOB blob;
|
---|
121 |
|
---|
122 | blob.data = (uint8_t *)talloc(data->ctx, uint32_t);
|
---|
123 | *(uint32_t *)blob.data = 0;
|
---|
124 | blob.length = 4;
|
---|
125 |
|
---|
126 | return reg_preg_diff_set_value(data, key_name, "**DelVals.", REG_DWORD, blob);
|
---|
127 | }
|
---|
128 |
|
---|
129 | static WERROR reg_preg_diff_done(void *_data)
|
---|
130 | {
|
---|
131 | struct preg_data *data = (struct preg_data *)_data;
|
---|
132 |
|
---|
133 | close(data->fd);
|
---|
134 | talloc_free(data);
|
---|
135 | return WERR_OK;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Save registry diff
|
---|
140 | */
|
---|
141 | _PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
|
---|
142 | struct smb_iconv_convenience *ic,
|
---|
143 | struct reg_diff_callbacks **callbacks,
|
---|
144 | void **callback_data)
|
---|
145 | {
|
---|
146 | struct preg_data *data;
|
---|
147 | struct {
|
---|
148 | char hdr[4];
|
---|
149 | uint32_t version;
|
---|
150 | } preg_header;
|
---|
151 |
|
---|
152 |
|
---|
153 | data = talloc_zero(ctx, struct preg_data);
|
---|
154 | *callback_data = data;
|
---|
155 |
|
---|
156 | if (filename) {
|
---|
157 | data->fd = open(filename, O_CREAT|O_WRONLY, 0755);
|
---|
158 | if (data->fd < 0) {
|
---|
159 | DEBUG(0, ("Unable to open %s\n", filename));
|
---|
160 | return WERR_BADFILE;
|
---|
161 | }
|
---|
162 | } else {
|
---|
163 | data->fd = STDOUT_FILENO;
|
---|
164 | }
|
---|
165 |
|
---|
166 | strncpy(preg_header.hdr, "PReg", 4);
|
---|
167 | SIVAL(&preg_header, 4, 1);
|
---|
168 | write(data->fd, (uint8_t *)&preg_header,8);
|
---|
169 |
|
---|
170 | data->ctx = ctx;
|
---|
171 |
|
---|
172 | *callbacks = talloc(ctx, struct reg_diff_callbacks);
|
---|
173 |
|
---|
174 | (*callbacks)->add_key = reg_preg_diff_add_key;
|
---|
175 | (*callbacks)->del_key = reg_preg_diff_del_key;
|
---|
176 | (*callbacks)->set_value = reg_preg_diff_set_value;
|
---|
177 | (*callbacks)->del_value = reg_preg_diff_del_value;
|
---|
178 | (*callbacks)->del_all_values = reg_preg_diff_del_all_values;
|
---|
179 | (*callbacks)->done = reg_preg_diff_done;
|
---|
180 |
|
---|
181 | return WERR_OK;
|
---|
182 | }
|
---|
183 | /**
|
---|
184 | * Load diff file
|
---|
185 | */
|
---|
186 | _PUBLIC_ WERROR reg_preg_diff_load(int fd,
|
---|
187 | struct smb_iconv_convenience *iconv_convenience,
|
---|
188 | const struct reg_diff_callbacks *callbacks,
|
---|
189 | void *callback_data)
|
---|
190 | {
|
---|
191 | struct {
|
---|
192 | char hdr[4];
|
---|
193 | uint32_t version;
|
---|
194 | } preg_header;
|
---|
195 | char *buf;
|
---|
196 | size_t buf_size = 1024;
|
---|
197 | char *buf_ptr;
|
---|
198 | TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
|
---|
199 | WERROR ret = WERR_OK;
|
---|
200 | DATA_BLOB data = {NULL, 0};
|
---|
201 | char *key = NULL;
|
---|
202 | char *value_name = NULL;
|
---|
203 |
|
---|
204 | buf = talloc_array(mem_ctx, char, buf_size);
|
---|
205 | buf_ptr = buf;
|
---|
206 |
|
---|
207 | /* Read first 8 bytes (the header) */
|
---|
208 | if (read(fd, &preg_header, 8) != 8) {
|
---|
209 | DEBUG(0, ("Could not read PReg file: %s\n",
|
---|
210 | strerror(errno)));
|
---|
211 | ret = WERR_GENERAL_FAILURE;
|
---|
212 | goto cleanup;
|
---|
213 | }
|
---|
214 | preg_header.version = IVAL(&preg_header.version, 0);
|
---|
215 |
|
---|
216 | if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
|
---|
217 | DEBUG(0, ("This file is not a valid preg registry file\n"));
|
---|
218 | ret = WERR_GENERAL_FAILURE;
|
---|
219 | goto cleanup;
|
---|
220 | }
|
---|
221 | if (preg_header.version > 1) {
|
---|
222 | DEBUG(0, ("Warning: file format version is higher than expected.\n"));
|
---|
223 | }
|
---|
224 |
|
---|
225 | /* Read the entries */
|
---|
226 | while(1) {
|
---|
227 | uint32_t value_type, length;
|
---|
228 |
|
---|
229 | if (!W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr))) {
|
---|
230 | break;
|
---|
231 | }
|
---|
232 | if (*buf_ptr != '[') {
|
---|
233 | DEBUG(0, ("Error in PReg file.\n"));
|
---|
234 | ret = WERR_GENERAL_FAILURE;
|
---|
235 | goto cleanup;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* Get the path */
|
---|
239 | buf_ptr = buf;
|
---|
240 | while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
|
---|
241 | *buf_ptr != ';' && buf_ptr-buf < buf_size) {
|
---|
242 | buf_ptr++;
|
---|
243 | }
|
---|
244 | buf[buf_ptr-buf] = '\0';
|
---|
245 | key = talloc_strdup(mem_ctx, buf);
|
---|
246 |
|
---|
247 | /* Get the name */
|
---|
248 | buf_ptr = buf;
|
---|
249 | while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
|
---|
250 | *buf_ptr != ';' && buf_ptr-buf < buf_size) {
|
---|
251 | buf_ptr++;
|
---|
252 | }
|
---|
253 | buf[buf_ptr-buf] = '\0';
|
---|
254 | value_name = talloc_strdup(mem_ctx, buf);
|
---|
255 |
|
---|
256 | /* Get the type */
|
---|
257 | if (read(fd, &value_type, 4) < 4) {
|
---|
258 | DEBUG(0, ("Error while reading PReg\n"));
|
---|
259 | ret = WERR_GENERAL_FAILURE;
|
---|
260 | goto cleanup;
|
---|
261 | }
|
---|
262 | value_type = IVAL(&value_type, 0);
|
---|
263 |
|
---|
264 | /* Read past delimiter */
|
---|
265 | buf_ptr = buf;
|
---|
266 | if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
|
---|
267 | *buf_ptr == ';') && buf_ptr-buf < buf_size) {
|
---|
268 | DEBUG(0, ("Error in PReg file.\n"));
|
---|
269 | ret = WERR_GENERAL_FAILURE;
|
---|
270 | goto cleanup;
|
---|
271 | }
|
---|
272 | /* Get data length */
|
---|
273 | if (read(fd, &length, 4) < 4) {
|
---|
274 | DEBUG(0, ("Error while reading PReg\n"));
|
---|
275 | ret = WERR_GENERAL_FAILURE;
|
---|
276 | goto cleanup;
|
---|
277 | }
|
---|
278 | /* Read past delimiter */
|
---|
279 | buf_ptr = buf;
|
---|
280 | if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
|
---|
281 | *buf_ptr == ';') && buf_ptr-buf < buf_size) {
|
---|
282 | DEBUG(0, ("Error in PReg file.\n"));
|
---|
283 | ret = WERR_GENERAL_FAILURE;
|
---|
284 | goto cleanup;
|
---|
285 | }
|
---|
286 | /* Get the data */
|
---|
287 | buf_ptr = buf;
|
---|
288 | if (length < buf_size &&
|
---|
289 | read(fd, buf_ptr, length) != length) {
|
---|
290 | DEBUG(0, ("Error while reading PReg\n"));
|
---|
291 | ret = WERR_GENERAL_FAILURE;
|
---|
292 | goto cleanup;
|
---|
293 | }
|
---|
294 | data = data_blob_talloc(mem_ctx, buf, length);
|
---|
295 |
|
---|
296 | /* Check if delimiter is in place (whine if it isn't) */
|
---|
297 | buf_ptr = buf;
|
---|
298 | if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
|
---|
299 | *buf_ptr == ']') && buf_ptr-buf < buf_size) {
|
---|
300 | DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",
|
---|
301 | *buf_ptr, *buf_ptr));
|
---|
302 | }
|
---|
303 |
|
---|
304 | if (strcasecmp(value_name, "**DelVals") == 0) {
|
---|
305 | callbacks->del_all_values(callback_data, key);
|
---|
306 | } else if (strncasecmp(value_name, "**Del.",6) == 0) {
|
---|
307 | char *p = value_name+6;
|
---|
308 |
|
---|
309 | callbacks->del_value(callback_data, key, p);
|
---|
310 | } else if (strcasecmp(value_name, "**DeleteValues") == 0) {
|
---|
311 | char *p, *q;
|
---|
312 |
|
---|
313 | p = (char *) data.data;
|
---|
314 |
|
---|
315 | while ((q = strchr_m(p, ';'))) {
|
---|
316 | *q = '\0';
|
---|
317 | q++;
|
---|
318 |
|
---|
319 | callbacks->del_value(callback_data, key, p);
|
---|
320 |
|
---|
321 | p = q;
|
---|
322 | }
|
---|
323 | callbacks->del_value(callback_data, key, p);
|
---|
324 | } else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
|
---|
325 | char *p, *q, *full_key;
|
---|
326 |
|
---|
327 | p = (char *) data.data;
|
---|
328 |
|
---|
329 | while ((q = strchr_m(p, ';'))) {
|
---|
330 | *q = '\0';
|
---|
331 | q++;
|
---|
332 |
|
---|
333 | full_key = talloc_asprintf(mem_ctx, "%s\\%s",
|
---|
334 | key, p);
|
---|
335 | callbacks->del_key(callback_data, full_key);
|
---|
336 | talloc_free(full_key);
|
---|
337 |
|
---|
338 | p = q;
|
---|
339 | }
|
---|
340 | full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
|
---|
341 | callbacks->del_key(callback_data, full_key);
|
---|
342 | talloc_free(full_key);
|
---|
343 | } else {
|
---|
344 | callbacks->add_key(callback_data, key);
|
---|
345 | callbacks->set_value(callback_data, key, value_name,
|
---|
346 | value_type, data);
|
---|
347 | }
|
---|
348 | }
|
---|
349 | cleanup:
|
---|
350 | close(fd);
|
---|
351 | talloc_free(data.data);
|
---|
352 | talloc_free(key);
|
---|
353 | talloc_free(value_name);
|
---|
354 | talloc_free(buf);
|
---|
355 | return ret;
|
---|
356 | }
|
---|