summaryrefslogtreecommitdiffstats
path: root/source3/utils/net_registry_util.c
blob: 22cb3a55b1ca33f6fc6a1bf7c4c13f8f154e0b88 (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
/*
 * Samba Unix/Linux SMB client library
 * Distributed SMB/CIFS Server Management Utility
 * registry utility functions
 *
 * Copyright (C) Michael Adam 2008
 *
 * 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 "registry.h"
#include "utils/net_registry_util.h"
#include "utils/net.h"
#include "../libcli/registry/util_reg.h"

void print_registry_key(const char *keyname, NTTIME *modtime)
{
	const char *ts = _("None");
	char *freeme = NULL;

	if (modtime != 0) {
		freeme = http_timestring(talloc_tos(),
					 nt_time_to_unix(*modtime));
		ts = freeme;
	}

	d_printf(_("Keyname   = %s\n"), keyname);
	d_printf(_("Modtime   = %s\n"), ts);
	d_printf("\n");

	TALLOC_FREE(freeme);
}

void print_registry_value(const struct registry_value *valvalue, bool raw)
{
	if (!raw) {
		d_printf(_("Type       = %s\n"),
			 str_regtype(valvalue->type));
	}
	switch(valvalue->type) {
	case REG_DWORD: {
		uint32_t v = 0;
		if (valvalue->data.length >= 4) {
			v = IVAL(valvalue->data.data, 0);
		}
		if (!raw) {
			d_printf(_("Value      = "));
		}
		d_printf("%d\n", v);
		break;
	}
	case REG_SZ:
	case REG_EXPAND_SZ: {
		const char *s;

		if (!pull_reg_sz(talloc_tos(), &valvalue->data, &s)) {
			break;
		}
		if (!raw) {
			d_printf(_("Value      = \""));
		}
		d_printf("%s", s);
		if (!raw) {
			d_printf("\"");
		}
		d_printf("\n");
		break;
	}
	case REG_MULTI_SZ: {
		uint32 j;
		const char **a;

		if (!pull_reg_multi_sz(talloc_tos(), &valvalue->data, &a)) {
			break;
		}
		for (j = 0; a[j] != NULL; j++) {
			if (!raw) {
				d_printf(_("Value[%3.3d] = \""), j);
			}
			d_printf("%s", a[j]);
			if (!raw) {
				d_printf("\"");
			}
			d_printf("\n");
		}
		break;
	}
	case REG_BINARY:
		if (!raw) {
			d_printf(_("Value      = "));
		}
		d_printf(_("%d bytes\n"), (int)valvalue->data.length);
		break;
	default:
		if (!raw) {
			d_printf(_("Value      = "));
		}
		d_printf(_("<unprintable>\n"));
		break;
	}
}

void print_registry_value_with_name(const char *valname,
				    const struct registry_value *valvalue)
{
	d_printf(_("Valuename  = %s\n"), valname);
	print_registry_value(valvalue, false);
	d_printf("\n");
}

/**
 * Split path into hive name and subkeyname
 * normalizations performed:
 *  - if the path contains no '\\' characters,
 *    assume that the legacy format of using '/'
 *    as a separator is used and  convert '/' to '\\'
 *  - strip trailing '\\' chars
 */
WERROR split_hive_key(TALLOC_CTX *ctx, const char *path, char **hivename,
		      char **subkeyname)
{
	char *p;
	const char *tmp_subkeyname;

	if ((path == NULL) || (hivename == NULL) || (subkeyname == NULL)) {
		return WERR_INVALID_PARAM;
	}

	if (strlen(path) == 0) {
		return WERR_INVALID_PARAM;
	}

	if (strchr(path, '\\') == NULL) {
		*hivename = talloc_string_sub(ctx, path, "/", "\\");
	} else {
		*hivename = talloc_strdup(ctx, path);
	}

	if (*hivename == NULL) {
		return WERR_NOMEM;
	}

	/* strip trailing '\\' chars */
	p = strrchr(*hivename, '\\');
	while ((p != NULL) && (p[1] == '\0')) {
		*p = '\0';
		p = strrchr(*hivename, '\\');
	}

	p = strchr(*hivename, '\\');

	if ((p == NULL) || (*p == '\0')) {
		/* just the hive - no subkey given */
		tmp_subkeyname = "";
	} else {
		*p = '\0';
		tmp_subkeyname = p+1;
	}
	*subkeyname = talloc_strdup(ctx, tmp_subkeyname);
	if (*subkeyname == NULL) {
		return WERR_NOMEM;
	}

	return WERR_OK;
}