summaryrefslogtreecommitdiffstats
path: root/source3/registry/reg_import.c
blob: aef8645ed9eac76280ec95948131663b992958f5 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Samba Unix/Linux SMB client library
 * Adapter to use reg_parse with the registry api
 *
 * Copyright (C) Gregor Beck 2010
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "reg_parse.h"
#include "reg_import.h"
#include "registry.h"
#include "registry/reg_objects.h"
#include <assert.h>

/* Debuglevel for tracing */
static const int TL = 2;

struct reg_import {
	struct reg_parse_callback reg_parse_callback;
	struct reg_import_callback call;
	void *open_key;
};

static int reg_parse_callback_key(struct reg_import *cb_private,
				  const char *key[], size_t n, bool del);

static int reg_parse_callback_val(struct reg_import *cb_private,
				  const char *name, uint32_t type,
				  const uint8_t *data, size_t len);

static int reg_parse_callback_val_registry_value(struct reg_import *cb_private,
						 const char *name,
						 uint32_t type,
						 const uint8_t *data,
						 size_t len);

static int reg_parse_callback_val_regval_blob(struct reg_import *cb_private,
					      const char *name, uint32_t type,
					      const uint8_t *data,
					      size_t len);

static int reg_parse_callback_val_del(struct reg_import *cb_private,
				      const char *name);

static int reg_parse_callback_comment(struct reg_import *cb_private,
				      const char *txt);


/*******************************************************************************/

int reg_parse_callback_key(struct reg_import *p,
			   const char *key[], size_t n, bool del)
{
	WERROR werr = WERR_OK;

	DEBUG(TL, ("%s: %s\n", __FUNCTION__, key[0]));

	if (p->open_key != NULL) {
		werr = p->call.closekey(p->call.data, p->open_key);
		p->open_key = NULL;
		if (!W_ERROR_IS_OK(werr)) {
			DEBUG(0, ("closekey failed: %s\n", win_errstr(werr)));
		}
	}

	if (del) {
		werr = p->call.deletekey(p->call.data, NULL, key[0]);
		if (W_ERROR_EQUAL(werr, WERR_FILE_NOT_FOUND)) {
			/* the key didn't exist, treat as success */
			werr = WERR_OK;
		}
		if (!W_ERROR_IS_OK(werr)) {
			DEBUG(0, ("deletekey %s failed: %s\n",
				  key[0], win_errstr(werr)));
		}
	} else {
		bool existing;
		werr = p->call.createkey(p->call.data, NULL, key[0],
					 &p->open_key, &existing);
		if (W_ERROR_IS_OK(werr)) {
			DEBUG(TL, ("createkey %s %s\n",
				  existing ? "opened" : "created", key[0]));
		} else {
			DEBUG(0, ("createkey %s failed: %s\n",
				  key[0], win_errstr(werr)));
		}
	}

	return W_ERROR_IS_OK(werr) ? 0 : -1;
}

#define DEBUG_ADD_HEX(LEV, PTR, LEN)					\
	do {								\
		int i;							\
		const unsigned char* ptr = (const unsigned char*)PTR;	\
		for (i=0; i<LEN; i++) {					\
			DEBUGADD(LEV, ("'%c'(%02x)%s",			\
				       isprint(ptr[i]) ? ptr[i] : '.',	\
				       (unsigned)ptr[i],		\
				       ((i+1 < LEN) && (i+1)%8)		\
				       ? ",  " : "\n"));		\
		}							\
	} while(0)

/*----------------------------------------------------------------------------*/
int reg_parse_callback_val(struct reg_import *p,
			   const char *name, uint32_t type,
			   const uint8_t *data, size_t len)
{
	WERROR werr = WERR_OK;

	DEBUG(TL, ("%s(%x): >%s< = [%zx]\n",  __FUNCTION__, type, name, len));
	DEBUG_ADD_HEX(TL, data, len);

	werr = p->call.setval.blob(p->call.data, p->open_key, name, type,
				   data, len);
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(0, ("setval %s failed: %s\n",
			  name, win_errstr(werr)));
	}

	return W_ERROR_IS_OK(werr) ? 0 : -1;
}

/*----------------------------------------------------------------------------*/
int reg_parse_callback_val_registry_value(struct reg_import *p,
					  const char *name, uint32_t type,
					  const uint8_t *data, size_t len)
{
	WERROR werr = WERR_OK;
	struct registry_value val = {
		.type = type,
		.data = data_blob_talloc(p, data, len),
	};

	DEBUG(TL, ("%s(%x): >%s< = [%zx]\n", __FUNCTION__, type, name, len));
	DEBUG_ADD_HEX(TL, data, len);

	werr = p->call.setval.registry_value(p->call.data, p->open_key,
					     name, &val);
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(0, ("setval %s failed: %s\n",
			  name, win_errstr(werr)));
	}

	data_blob_free(&val.data);
	return W_ERROR_IS_OK(werr) ? 0 : -1;
}

/*----------------------------------------------------------------------------*/
int reg_parse_callback_val_regval_blob(struct reg_import *p,
				       const char *name, uint32_t type,
				       const uint8_t *data, size_t len)
{
	WERROR werr = WERR_OK;
	void* mem_ctx = talloc_new(p);
	struct regval_blob *v = NULL;

	DEBUG(TL, ("%s(%x): >%s< = [%zx]\n", __FUNCTION__, type, name, len));
	DEBUG_ADD_HEX(TL, data, len);

	v = regval_compose(mem_ctx, name, type, data, len);
	if (v == NULL) {
		DEBUG(0, ("regval_compose %s failed\n", name));
		werr = WERR_NOT_ENOUGH_MEMORY;
		goto done;
	}

	werr = p->call.setval.regval_blob(p->call.data, p->open_key, v);
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(0, ("setval %s failed: %s\n",
			  name, win_errstr(werr)));
	}

done:
	talloc_free(mem_ctx);

	return W_ERROR_IS_OK(werr) ? 0 : -1;
}


/*----------------------------------------------------------------------------*/

int reg_parse_callback_val_del(struct reg_import *p,
			       const char *name)
{
	WERROR werr = WERR_OK;

	DEBUG(TL, ("%s: %s\n", __FUNCTION__, name));

	werr = p->call.deleteval(p->call.data, p->open_key, name);
	if (!W_ERROR_IS_OK(werr)) {
		DEBUG(0, ("deleteval %s failed: %s\n",
			  name, win_errstr(werr)));
	}

	return W_ERROR_IS_OK(werr) ? 0 : -1;
}


int reg_parse_callback_comment(struct reg_import *cb_private,
			       const char *txt)
{
	DEBUG(TL, ("%s: %s\n", __FUNCTION__, txt));
	return 0;
}

/******************************************************************************/
static WERROR nop_callback_open(void* private_data,
		void* parent,
		const char* name,
		void** key)
{
	return WERR_OK;
}

static WERROR nop_callback_close(void* private_data, void* key)
{
	return WERR_OK;
}

static WERROR nop_callback_create(void* private_data,
		void* parent,
		const char* name,
		void** key,
		bool* existing)
{
	return WERR_OK;
}


static WERROR nop_callback_del(void* private_data,
		void* parent,
		const char* name)
{
	return WERR_OK;
}

struct reg_parse_callback *reg_import_adapter(TALLOC_CTX *talloc_ctx,
					      struct reg_import_callback cb)
{
	struct reg_parse_callback *ret;
	struct reg_import *p = talloc_zero(talloc_ctx, struct reg_import);
	if (p == NULL) {
		goto fail;
	}
	if (cb.openkey == NULL) {
		cb.openkey = (reg_import_callback_openkey_t)&nop_callback_open;
	}
	if (cb.closekey == NULL) {
		cb.closekey =
			(reg_import_callback_closekey_t)&nop_callback_close;
	}
	if (cb.createkey == NULL) {
		cb.createkey =
			(reg_import_callback_createkey_t)&nop_callback_create;
	}
	if (cb.deletekey == NULL) {
		cb.deletekey =
			(reg_import_callback_deletekey_t)&nop_callback_del;

	}
	if (cb.deleteval == NULL) {
		cb.deleteval =
			(reg_import_callback_deleteval_t)&nop_callback_del;
	}

	p->call = cb;

	ret = &p->reg_parse_callback;
	ret->key     = (reg_parse_callback_key_t)     &reg_parse_callback_key;
	ret->val_del = (reg_parse_callback_val_del_t) &reg_parse_callback_val_del;
	ret->comment = (reg_parse_callback_comment_t) &reg_parse_callback_comment;
	ret->data = p;

	switch (cb.setval_type) {
	case BLOB:
		assert(cb.setval.blob != NULL);
		ret->val = (reg_parse_callback_val_t) &reg_parse_callback_val;
		break;
	case REGISTRY_VALUE:
		assert(cb.setval.registry_value != NULL);
		ret->val = (reg_parse_callback_val_t) &reg_parse_callback_val_registry_value;
		break;
	case REGVAL_BLOB:
		assert(cb.setval.regval_blob != NULL);
		ret->val = (reg_parse_callback_val_t) &reg_parse_callback_val_regval_blob;
		break;
	case NONE:
		ret->val = NULL;
		break;
	default:
		assert(false);
	}

	assert((struct reg_parse_callback *)p == ret);
	return ret;
fail:
	talloc_free(p);
	return NULL;
}