summaryrefslogtreecommitdiffstats
path: root/source3/registry/reg_cachehook.c
blob: 4f84de52860370b70530a31bb3a824b0389cab7e (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
/* 
 *  Unix SMB/CIFS implementation.
 *  Virtual Windows Registry Layer
 *  Copyright (C) Gerald Carter                     2002.
 *
 *  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/>.
 */

/* Implementation of registry hook cache tree */

#include "includes.h"
#include "adt_tree.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_REGISTRY

static SORTED_TREE *cache_tree = NULL;
extern struct registry_ops regdb_ops;		/* these are the default */

static WERROR keyname_to_path(TALLOC_CTX *mem_ctx, const char *keyname,
			      char **path)
{
	char *tmp_path = NULL;

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

	tmp_path = talloc_asprintf(mem_ctx, "\\%s", keyname);
	if (tmp_path == NULL) {
		DEBUG(0, ("talloc_asprintf failed!\n"));
		return WERR_NOMEM;
	}

	tmp_path = talloc_string_sub(mem_ctx, tmp_path, "\\", "/");
	if (tmp_path == NULL) {
		DEBUG(0, ("talloc_string_sub_failed!\n"));
		return WERR_NOMEM;
	}

	*path = tmp_path;

	return WERR_OK;
}

/**********************************************************************
 Initialize the cache tree if it has not been initialized yet.
 *********************************************************************/

WERROR reghook_cache_init(void)
{
	if (cache_tree != NULL) {
		return WERR_OK;
	}

	cache_tree = pathtree_init(&regdb_ops, NULL);
	if (cache_tree == NULL) {
		return WERR_NOMEM;
	}
	DEBUG(10, ("reghook_cache_init: new tree with default "
		   "ops %p for key [%s]\n", (void *)&regdb_ops,
		   KEY_TREE_ROOT));
	return WERR_OK;
}

/**********************************************************************
 Add a new registry hook to the cache.  Note that the keyname
 is not in the exact format that a SORTED_TREE expects.
 *********************************************************************/

WERROR reghook_cache_add(const char *keyname, struct registry_ops *ops)
{
	WERROR werr;
	char *key = NULL;

	if ((keyname == NULL) || (ops == NULL)) {
		return WERR_INVALID_PARAM;
	}

	werr = keyname_to_path(talloc_tos(), keyname, &key);
	if (!W_ERROR_IS_OK(werr)) {
		goto done;
	}

	DEBUG(10, ("reghook_cache_add: Adding ops %p for key [%s]\n",
		   (void *)ops, key));

	werr = pathtree_add(cache_tree, key, ops);

done:
	TALLOC_FREE(key);
	return werr;
}

/**********************************************************************
 Find a key in the cache.
 *********************************************************************/

struct registry_ops *reghook_cache_find(const char *keyname)
{
	WERROR werr;
	char *key = NULL;
	struct registry_ops *ops = NULL;

	if (keyname == NULL) {
		return NULL;
	}

	werr = keyname_to_path(talloc_tos(), keyname, &key);
	if (!W_ERROR_IS_OK(werr)) {
		goto done;
	}

	DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));

	ops = (struct registry_ops *)pathtree_find(cache_tree, key);

	DEBUG(10, ("reghook_cache_find: found ops %p for key [%s]\n",
		   ops ? (void *)ops : 0, key));

done:
	TALLOC_FREE(key);

	return ops;
}

/**********************************************************************
 Print out the cache tree structure for debugging.
 *********************************************************************/

void reghook_dump_cache( int debuglevel )
{
	DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));

	pathtree_print_keys( cache_tree, debuglevel );
}