summaryrefslogtreecommitdiff
path: root/source4/lib/registry/patchfile_preg.c
blob: 47bd160c5400bd340debd1b40b3ec1f860ee813b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
   Unix SMB/CIFS implementation.
   Reading Registry.pol PReg registry files

   Copyright (C) Wilco Baan Hofman 2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/registry/registry.h"
#include "lib/registry/patchfile.h"
#include "system/filesys.h"

struct preg_data {
	int fd;
};

static WERROR preg_read_utf16(int fd, char *c)
{
	uint16_t v;

	if (read(fd, &v, 2) < 2) {
		return WERR_GENERAL_FAILURE;
	}
	push_codepoint(global_smb_iconv_convenience, c, v);
	return WERR_OK;
}

/* FIXME These functions need to be implemented */
static WERROR reg_preg_diff_add_key(void *_data, const char *key_name)
{
	return WERR_OK;
}

static WERROR reg_preg_diff_del_key(void *_data, const char *key_name)
{
	return WERR_OK;
}

static WERROR reg_preg_diff_set_value(void *_data, const char *key_name,
				      const char *value_name,
				      uint32_t value_type, DATA_BLOB value_data)
{
	return WERR_OK;
}

static WERROR reg_preg_diff_del_value(void *_data, const char *key_name,
				      const char *value_name)
{
	return WERR_OK;
}

static WERROR reg_preg_diff_del_all_values(void *_data, const char *key_name)
{
	return WERR_OK;
}

static WERROR reg_preg_diff_done(void *_data)
{
	struct preg_data *data = (struct preg_data *)_data;

	close(data->fd);
	talloc_free(data);
	return WERR_OK;
}

/**
 * Save registry diff
 */
_PUBLIC_ WERROR reg_preg_diff_save(TALLOC_CTX *ctx, const char *filename,
				   struct reg_diff_callbacks **callbacks,
				   void **callback_data)
{
	struct preg_data *data;
	struct {
		char hdr[4];
		uint32_t version;
	} preg_header;


	data = talloc_zero(ctx, struct preg_data);
	*callback_data = data;

	if (filename) {
		data->fd = open(filename, O_CREAT, 0755);
		if (data->fd == -1) {
			DEBUG(0, ("Unable to open %s\n", filename));
			return WERR_BADFILE;
		}
	} else {
		data->fd = STDOUT_FILENO;
	}
	snprintf(preg_header.hdr, 4, "PReg");
	SIVAL(&preg_header, 4, 1);
	write(data->fd, (uint8_t *)&preg_header,8);

	*callbacks = talloc(ctx, struct reg_diff_callbacks);

	(*callbacks)->add_key = reg_preg_diff_add_key;
	(*callbacks)->del_key = reg_preg_diff_del_key;
	(*callbacks)->set_value = reg_preg_diff_set_value;
	(*callbacks)->del_value = reg_preg_diff_del_value;
	(*callbacks)->del_all_values = reg_preg_diff_del_all_values;
	(*callbacks)->done = reg_preg_diff_done;

	return WERR_OK;
}
/**
 * Load diff file
 */
_PUBLIC_ WERROR reg_preg_diff_load(int fd,
				   const struct reg_diff_callbacks *callbacks,
				   void *callback_data)
{
	struct {
		char hdr[4];
		uint32_t version;
	} preg_header;
	char *buf;
	size_t buf_size = 1024;
	char *buf_ptr;
	TALLOC_CTX *mem_ctx = talloc_init("reg_preg_diff_load");
	WERROR ret = WERR_OK;
	DATA_BLOB data = {NULL, 0};
	char *key = NULL;
	char *value_name = NULL;

	buf = talloc_array(mem_ctx, char, buf_size);
	buf_ptr = buf;

	/* Read first 8 bytes (the header) */
	if (read(fd, &preg_header, 8) != 8) {
		DEBUG(0, ("Could not read PReg file: %s\n",
				strerror(errno)));
		ret = WERR_GENERAL_FAILURE;
		goto cleanup;
	}
	if (strncmp(preg_header.hdr, "PReg", 4) != 0) {
		DEBUG(0, ("This file is not a valid preg registry file\n"));
		ret = WERR_GENERAL_FAILURE;
		goto cleanup;
	}
	if (preg_header.version > 1) {
		DEBUG(0, ("Warning: file format version is higher than expected.\n"));
	}

	/* Read the entries */
	while(1) {
		uint32_t value_type, length;

		if (!W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr))) {
			break;
		}
		if (*buf_ptr != '[') {
			DEBUG(0, ("Error in PReg file.\n"));
			ret = WERR_GENERAL_FAILURE;
			goto cleanup;
		}

		/* Get the path */
		buf_ptr = buf;
		while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
		       *buf_ptr != ';' && buf_ptr-buf < buf_size) {
			buf_ptr++;
		}
		key = talloc_asprintf(mem_ctx, "\\%s", buf);

		/* Get the name */
		buf_ptr = buf;
		while (W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
		       *buf_ptr != ';' && buf_ptr-buf < buf_size) {
			buf_ptr++;
		}
		value_name = talloc_strdup(mem_ctx, buf);

		/* Get the type */
		if (read(fd, &value_type, 4) < 4) {
			DEBUG(0, ("Error while reading PReg\n"));
			ret = WERR_GENERAL_FAILURE;
			goto cleanup;
		}
		/* Read past delimiter */
		buf_ptr = buf;
		if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
		    *buf_ptr == ';') && buf_ptr-buf < buf_size) {
			DEBUG(0, ("Error in PReg file.\n"));
			ret = WERR_GENERAL_FAILURE;
			goto cleanup;
		}
		/* Get data length */
		if (read(fd, &length, 4) < 4) {
			DEBUG(0, ("Error while reading PReg\n"));
			ret = WERR_GENERAL_FAILURE;
			goto cleanup;
		}
		/* Read past delimiter */
		buf_ptr = buf;
		if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
		    *buf_ptr == ';') && buf_ptr-buf < buf_size) {
			DEBUG(0, ("Error in PReg file.\n"));
			ret = WERR_GENERAL_FAILURE;
			goto cleanup;
		}
		/* Get the data */
		buf_ptr = buf;
		if (length < buf_size &&
		    read(fd, buf_ptr, length) != length) {
			DEBUG(0, ("Error while reading PReg\n"));
			ret = WERR_GENERAL_FAILURE;
			goto cleanup;
		}
		data = data_blob_talloc(mem_ctx, buf, length);

		/* Check if delimiter is in place (whine if it isn't) */
		buf_ptr = buf;
		if (!(W_ERROR_IS_OK(preg_read_utf16(fd, buf_ptr)) &&
		    *buf_ptr == ']') && buf_ptr-buf < buf_size) {
			DEBUG(0, ("Warning: Missing ']' in PReg file, expected ']', got '%c' 0x%x.\n",
				*buf_ptr, *buf_ptr));
		}

		if (strcasecmp(value_name, "**DelVals") == 0) {
			callbacks->del_all_values(callback_data, key);
		} else if (strncasecmp(value_name, "**Del.",6) == 0) {
			char *p = value_name+6;

			callbacks->del_value(callback_data, key, p);
		} else 	if (strcasecmp(value_name, "**DeleteValues") == 0) {
			char *p, *q;

			p = (char *) data.data;

			while ((q = strchr_m(p, ';'))) {
				*q = '\0';
				q++;

				callbacks->del_value(callback_data, key, p);

				p = q;
			}
			callbacks->del_value(callback_data, key, p);
		} else if (strcasecmp(value_name, "**DeleteKeys") == 0) {
			char *p, *q, *full_key;

			p = (char *) data.data;

			while ((q = strchr_m(p, ';'))) {
				*q = '\0';
				q++;

				full_key = talloc_asprintf(mem_ctx, "%s\\%s",
							   key, p);
				callbacks->del_key(callback_data, full_key);
				talloc_free(full_key);

				p = q;
			}
			full_key = talloc_asprintf(mem_ctx, "%s\\%s", key, p);
			callbacks->del_key(callback_data, full_key);
			talloc_free(full_key);
		} else {
			callbacks->add_key(callback_data, key);
			callbacks->set_value(callback_data, key, value_name,
					     value_type, data);
 		}
	}
cleanup:
	close(fd);
	talloc_free(data.data);
	talloc_free(key);
	talloc_free(value_name);
	talloc_free(buf);
	return ret;
}