summaryrefslogtreecommitdiff
path: root/source3/utils/regedit_valuelist.c
blob: 76417ce5892abd61d3073e295e6f41285221c666 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * Samba Unix/Linux SMB client library
 * Registry Editor
 * Copyright (C) Christopher Davis 2012
 *
 * 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 "regedit.h"
#include "regedit_valuelist.h"
#include "regedit_list.h"
#include "lib/registry/registry.h"

#define HEADING_X 3

static int value_list_free(struct value_list *vl)
{
	if (vl->panel) {
		del_panel(vl->panel);
	}
	if (vl->sub) {
		delwin(vl->sub);
	}
	if (vl->window) {
		delwin(vl->window);
	}

	return 0;
}

static const char *vl_get_column_header(const void *data, unsigned col)
{
	switch (col) {
	case 0:
		return "Name";
	case 1:
		return "Type";
	case 2:
		return "Data";
	}

	return "???";
}

static const void *vl_get_first_row(const void *data)
{
	const struct value_list *vl;

	if (data) {
		vl = talloc_get_type_abort(data, struct value_list);
		if (vl->nvalues) {
			return &vl->values[0];
		}
	}

	return NULL;
}

static const void *vl_get_next_row(const void *data, const void *row)
{
	const struct value_list *vl;
	const struct value_item *value = row;

	SMB_ASSERT(data != NULL);
	SMB_ASSERT(value != NULL);
	vl = talloc_get_type_abort(data, struct value_list);
	if (value == &vl->values[vl->nvalues - 1]) {
		return NULL;
	}

	return value + 1;
}

static const void *vl_get_prev_row(const void *data, const void *row)
{
	const struct value_list *vl;
	const struct value_item *value = row;

	SMB_ASSERT(data != NULL);
	SMB_ASSERT(value != NULL);
	vl = talloc_get_type_abort(data, struct value_list);
	if (value == &vl->values[0]) {
		return NULL;
	}

	return value - 1;
}

static const char *vl_get_item_label(const void *row, unsigned col)
{
	const struct value_item *value = row;

	SMB_ASSERT(value != NULL);
	SMB_ASSERT(value->value_name != NULL);
	switch (col) {
	case 0:
		return value->value_name;
	case 1:
		return str_regtype(value->type);
	case 2:
		if (value->value) {
			return value->value;
		}
		return "";
	}

	return "???";
}

static struct multilist_accessors vl_accessors = {
	.get_column_header = vl_get_column_header,
	.get_first_row = vl_get_first_row,
	.get_next_row = vl_get_next_row,
	.get_prev_row = vl_get_prev_row,
	.get_item_label = vl_get_item_label
};

struct value_list *value_list_new(TALLOC_CTX *ctx, int nlines, int ncols,
				  int begin_y, int begin_x)
{
	struct value_list *vl;

	vl = talloc_zero(ctx, struct value_list);
	if (vl == NULL) {
		return NULL;
	}

	talloc_set_destructor(vl, value_list_free);

	vl->window = newwin(nlines, ncols, begin_y, begin_x);
	if (vl->window == NULL) {
		goto fail;
	}
	vl->sub = subwin(vl->window, nlines - 2, ncols - 2,
			 begin_y + 1, begin_x + 1);
	if (vl->sub == NULL) {
		goto fail;
	}
	box(vl->window, 0, 0);
	mvwprintw(vl->window, 0, HEADING_X, "Value");

	vl->panel = new_panel(vl->window);
	if (vl->panel == NULL) {
		goto fail;
	}

	vl->list = multilist_new(vl, vl->sub, &vl_accessors, 3);
	if (vl->list == NULL) {
		goto fail;
	}

	return vl;

fail:
	talloc_free(vl);

	return NULL;
}

void value_list_set_selected(struct value_list *vl, bool reverse)
{
	attr_t attr = A_NORMAL;

	if (reverse) {
		attr = A_REVERSE;
	}
	mvwchgat(vl->window, 0, HEADING_X, 5, attr, 0, NULL);
}

void value_list_resize(struct value_list *vl, int nlines, int ncols,
		       int begin_y, int begin_x)
{
	WINDOW *nwin, *nsub;

	nwin = newwin(nlines, ncols, begin_y, begin_x);
	if (nwin == NULL) {
		return;
	}
	nsub = subwin(nwin, nlines - 2, ncols - 2, begin_y + 1, begin_x + 1);
	if (nsub == NULL) {
		delwin(nwin);
		return;
	}
	replace_panel(vl->panel, nwin);
	delwin(vl->sub);
	delwin(vl->window);
	vl->window = nwin;
	vl->sub = nsub;
	box(vl->window, 0, 0);
	mvwprintw(vl->window, 0, HEADING_X, "Value");
	multilist_set_window(vl->list, vl->sub);
	value_list_show(vl);
}

static uint32_t get_num_values(TALLOC_CTX *ctx, const struct registry_key *key)
{
	const char *classname;
	uint32_t num_subkeys;
	uint32_t num_values;
	NTTIME last_change_time;
	uint32_t max_subkeynamelen;
	uint32_t max_valnamelen;
	uint32_t max_valbufsize;
	WERROR rv;

	rv = reg_key_get_info(ctx, key, &classname, &num_subkeys,
			      &num_values, &last_change_time,
			      &max_subkeynamelen, &max_valnamelen,
			      &max_valbufsize);

	if (W_ERROR_IS_OK(rv)) {
		return num_values;
	}

	return 0;
}

void value_list_show(struct value_list *vl)
{
	multilist_refresh(vl->list);
	touchwin(vl->window);
	wnoutrefresh(vl->window);
	wnoutrefresh(vl->sub);
}

static bool string_is_printable(const char *s)
{
	const char *p;

	for (p = s; *p; ++p) {
		if (!isprint(*p)) {
			return false;
		}
	}

	return true;
}

static WERROR append_data_summary(TALLOC_CTX *ctx, struct value_item *vitem)
{
	char *tmp = NULL;

/* This is adapted from print_registry_value() in net_registry_util.c */

	switch(vitem->type) {
	case REG_DWORD: {
		uint32_t v = 0;
		if (vitem->data.length >= 4) {
			v = IVAL(vitem->data.data, 0);
		}
		tmp = talloc_asprintf(ctx, "0x%08x (%u)", v, v);
		break;
	}
	case REG_SZ:
	case REG_EXPAND_SZ: {
		const char *s;

		if (!pull_reg_sz(ctx, &vitem->data, &s)) {
			break;
		}
		vitem->unprintable = !string_is_printable(s);
		if (vitem->unprintable) {
			tmp = talloc_asprintf(ctx, "(unprintable)");
		} else {
			tmp = talloc_asprintf(ctx, "%s", s);
		}
		break;
	}
	case REG_MULTI_SZ: {
		size_t i, len;
		const char **a;
		const char *val;

		if (!pull_reg_multi_sz(ctx, &vitem->data, &a)) {
			break;
		}
		for (len = 0; a[len] != NULL; ++len) {
		}
		tmp = talloc_asprintf(ctx, "(%u) ", (unsigned)len);
		if (tmp == NULL) {
			return WERR_NOMEM;
		}
		for (i = 0; i < len; ++i) {
			if (!string_is_printable(a[i])) {
				val = "(unprintable)";
				vitem->unprintable = true;
			} else {
				val = a[i];
			}
			if (i == len - 1) {
				tmp = talloc_asprintf_append(tmp,
							     "[%u]=\"%s\"",
							     (unsigned)i, val);
			} else {
				tmp = talloc_asprintf_append(tmp,
							     "[%u]=\"%s\", ",
							     (unsigned)i, val);
			}
			if (tmp == NULL) {
				return WERR_NOMEM;
			}
		}
		break;
	}
	case REG_BINARY:
		tmp = talloc_asprintf(ctx, "(%d bytes)",
				      (int)vitem->data.length);
		break;
	default:
		tmp = talloc_asprintf(ctx, "(unknown)");
		break;
	}

	if (tmp == NULL) {
		return WERR_NOMEM;
	}

	vitem->value = tmp;

	return WERR_OK;
}

static int vitem_cmp(struct value_item *a, struct value_item *b)
{
	return strcmp(a->value_name, b->value_name);
}

/* load only the value names into memory to enable searching */
WERROR value_list_load_quick(struct value_list *vl, struct registry_key *key)
{
	uint32_t nvalues;
	uint32_t idx;
	struct value_item *vitem, *new_items;
	WERROR rv;

	multilist_set_data(vl->list, NULL);
	vl->nvalues = 0;
	TALLOC_FREE(vl->values);

	nvalues = get_num_values(vl, key);
	if (nvalues == 0) {
		return WERR_OK;
	}

	new_items = talloc_zero_array(vl, struct value_item, nvalues);
	if (new_items == NULL) {
		return WERR_NOMEM;
	}

	for (idx = 0; idx < nvalues; ++idx) {
		vitem = &new_items[idx];
		rv = reg_key_get_value_by_index(new_items, key, idx,
						&vitem->value_name,
						&vitem->type,
						&vitem->data);
		if (!W_ERROR_IS_OK(rv)) {
			talloc_free(new_items);
			return rv;
		}
	}

	TYPESAFE_QSORT(new_items, nvalues, vitem_cmp);
	vl->nvalues = nvalues;
	vl->values = new_items;

	return rv;
}

/* sync up the UI with the list */
WERROR value_list_sync(struct value_list *vl)
{
	uint32_t idx;
	WERROR rv;

	for (idx = 0; idx < vl->nvalues; ++idx) {
		rv = append_data_summary(vl->values, &vl->values[idx]);
		if (!W_ERROR_IS_OK(rv)) {
			return rv;
		}
	}

	rv = multilist_set_data(vl->list, vl);
	if (W_ERROR_IS_OK(rv)) {
		multilist_refresh(vl->list);
	}

	return rv;
}

WERROR value_list_load(struct value_list *vl, struct registry_key *key)
{
	WERROR rv;

	rv = value_list_load_quick(vl, key);
	if (!W_ERROR_IS_OK(rv)) {
		return rv;
	}

	rv = value_list_sync(vl);

	return rv;
}

struct value_item *value_list_find_next_item(struct value_list *vl,
					     struct value_item *vitem,
					     const char *s,
					     regedit_search_match_fn_t match)
{
	struct value_item *end;

	if (!vl->values) {
		return NULL;
	}

	if (vitem) {
		++vitem;
	} else {
		vitem = &vl->values[0];
	}

	for (end = &vl->values[vl->nvalues]; vitem < end; ++vitem) {
		if (match(vitem->value_name, s)) {
			return vitem;
		}
	}

	return NULL;
}

struct value_item *value_list_find_prev_item(struct value_list *vl,
					     struct value_item *vitem,
					     const char *s,
					     regedit_search_match_fn_t match)
{
	struct value_item *end;

	if (!vl->values) {
		return NULL;
	}

	if (vitem) {
		--vitem;
	} else {
		vitem = &vl->values[vl->nvalues - 1];
	}

	for (end = &vl->values[-1]; vitem > end; --vitem) {
		if (match(vitem->value_name, s)) {
			return vitem;
		}
	}

	return NULL;
}

struct value_item *value_list_get_current_item(struct value_list *vl)
{
	return discard_const_p(struct value_item,
			       multilist_get_current_row(vl->list));
}

void value_list_set_current_item_by_name(struct value_list *vl,
					 const char *name)
{
	size_t i;

	for (i = 0; i < vl->nvalues; ++i) {
		if (strequal(vl->values[i].value_name, name)) {
			multilist_set_current_row(vl->list, &vl->values[i]);
			return;
		}
	}
}

void value_list_set_current_item(struct value_list *vl,
				 const struct value_item *item)
{
	multilist_set_current_row(vl->list, item);
}

void value_list_driver(struct value_list *vl, int c)
{
	multilist_driver(vl->list, c);
}