Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : * Virtual Windows Registry Layer
4 : * Copyright (C) Gerald Carter 2002.
5 : * Copyright (C) Michael Adam 2008
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : /* Implementation of registry hook cache tree */
22 :
23 : #include "includes.h"
24 : #include "adt_tree.h"
25 : #include "registry.h"
26 : #include "reg_cachehook.h"
27 :
28 : #undef DBGC_CLASS
29 : #define DBGC_CLASS DBGC_REGISTRY
30 :
31 : static struct sorted_tree *cache_tree = NULL;
32 : extern struct registry_ops regdb_ops; /* these are the default */
33 :
34 191554 : static WERROR keyname_to_path(TALLOC_CTX *mem_ctx, const char *keyname,
35 : char **path)
36 : {
37 191554 : char *tmp_path = NULL;
38 :
39 191554 : if ((keyname == NULL) || (path == NULL)) {
40 0 : return WERR_INVALID_PARAMETER;
41 : }
42 :
43 191554 : tmp_path = talloc_asprintf(mem_ctx, "\\%s", keyname);
44 191554 : if (tmp_path == NULL) {
45 0 : DEBUG(0, ("talloc_asprintf failed!\n"));
46 0 : return WERR_NOT_ENOUGH_MEMORY;
47 : }
48 :
49 191554 : *path = tmp_path;
50 :
51 191554 : return WERR_OK;
52 : }
53 :
54 : /**********************************************************************
55 : Initialize the cache tree if it has not been initialized yet.
56 : *********************************************************************/
57 :
58 166 : WERROR reghook_cache_init(void)
59 : {
60 166 : if (cache_tree != NULL) {
61 37 : return WERR_OK;
62 : }
63 :
64 129 : cache_tree = pathtree_init(®db_ops);
65 129 : if (cache_tree == NULL) {
66 0 : return WERR_NOT_ENOUGH_MEMORY;
67 : }
68 129 : DEBUG(10, ("reghook_cache_init: new tree with default "
69 : "ops %p for key [%s]\n", (void *)®db_ops,
70 : KEY_TREE_ROOT));
71 129 : return WERR_OK;
72 : }
73 :
74 : /**********************************************************************
75 : Add a new registry hook to the cache. Note that the keyname
76 : is not in the exact format that a struct sorted_tree expects.
77 : *********************************************************************/
78 :
79 892 : WERROR reghook_cache_add(const char *keyname, struct registry_ops *ops)
80 : {
81 : WERROR werr;
82 892 : char *key = NULL;
83 :
84 892 : if ((keyname == NULL) || (ops == NULL)) {
85 0 : return WERR_INVALID_PARAMETER;
86 : }
87 :
88 892 : werr = keyname_to_path(talloc_tos(), keyname, &key);
89 892 : if (!W_ERROR_IS_OK(werr)) {
90 0 : goto done;
91 : }
92 :
93 892 : DEBUG(10, ("reghook_cache_add: Adding ops %p for key [%s]\n",
94 : (void *)ops, key));
95 :
96 892 : if (!pathtree_add(cache_tree, key, ops))
97 0 : werr = WERR_NOT_ENOUGH_MEMORY;
98 : else
99 892 : werr = WERR_OK;
100 :
101 892 : done:
102 892 : TALLOC_FREE(key);
103 892 : return werr;
104 : }
105 :
106 : /**********************************************************************
107 : Find a key in the cache.
108 : *********************************************************************/
109 :
110 190662 : struct registry_ops *reghook_cache_find(const char *keyname)
111 : {
112 : WERROR werr;
113 190662 : char *key = NULL;
114 190662 : struct registry_ops *ops = NULL;
115 :
116 190662 : if (keyname == NULL) {
117 0 : return NULL;
118 : }
119 :
120 190662 : werr = keyname_to_path(talloc_tos(), keyname, &key);
121 190662 : if (!W_ERROR_IS_OK(werr)) {
122 0 : goto done;
123 : }
124 :
125 190662 : DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
126 :
127 190662 : ops = (struct registry_ops *)pathtree_find(cache_tree, key);
128 :
129 190662 : DEBUG(10, ("reghook_cache_find: found ops %p for key [%s]\n",
130 : ops ? (void *)ops : 0, key));
131 :
132 190662 : done:
133 190662 : TALLOC_FREE(key);
134 :
135 190662 : return ops;
136 : }
137 :
138 : /**********************************************************************
139 : Print out the cache tree structure for debugging.
140 : *********************************************************************/
141 :
142 0 : void reghook_dump_cache( int debuglevel )
143 : {
144 0 : DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
145 :
146 0 : pathtree_print_keys( cache_tree, debuglevel );
147 0 : }
|