We may use 127k read/write for encrypted connections.
[ira/wip.git] / source3 / registry / reg_cachehook.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Gerald Carter                     2002.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /* Implementation of registry hook cache tree */
21
22 #include "includes.h"
23 #include "adt_tree.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
27
28 static SORTED_TREE *cache_tree;
29 extern REGISTRY_OPS regdb_ops;          /* these are the default */
30 static REGISTRY_HOOK default_hook = { KEY_TREE_ROOT, &regdb_ops };
31
32 /**********************************************************************
33  Initialize the cache tree
34  *********************************************************************/
35
36 bool reghook_cache_init( void )
37 {
38         cache_tree = pathtree_init( &default_hook, NULL );
39
40         return ( cache_tree == NULL );
41 }
42
43 /**********************************************************************
44  Add a new REGISTRY_HOOK to the cache.  Note that the keyname
45  is not in the exact format that a SORTED_TREE expects.
46  *********************************************************************/
47
48 bool reghook_cache_add( REGISTRY_HOOK *hook )
49 {
50         TALLOC_CTX *ctx = talloc_tos();
51         char *key = NULL;
52
53         if (!hook) {
54                 return false;
55         }
56
57         key = talloc_asprintf(ctx, "//%s", hook->keyname);
58         if (!key) {
59                 return false;
60         }
61         key = talloc_string_sub(ctx, key, "\\", "/");
62         if (!key) {
63                 return false;
64         }
65
66         DEBUG(10,("reghook_cache_add: Adding key [%s]\n", key));
67
68         return pathtree_add( cache_tree, key, hook );
69 }
70
71 /**********************************************************************
72  Initialize the cache tree
73  *********************************************************************/
74
75 REGISTRY_HOOK* reghook_cache_find( const char *keyname )
76 {
77         char *key;
78         int len;
79         REGISTRY_HOOK *hook;
80         
81         if ( !keyname )
82                 return NULL;
83         
84         /* prepend the string with a '\' character */
85         
86         len = strlen( keyname );
87         if ( !(key = (char *)SMB_MALLOC( len + 2 )) ) {
88                 DEBUG(0,("reghook_cache_find: malloc failed for string [%s] !?!?!\n",
89                         keyname));
90                 return NULL;
91         }
92
93         *key = '\\';
94         strncpy( key+1, keyname, len+1);
95         
96         /* swap to a form understood by the SORTED_TREE */
97
98         string_sub( key, "\\", "/", 0 );
99                 
100         DEBUG(10,("reghook_cache_find: Searching for keyname [%s]\n", key));
101         
102         hook = (REGISTRY_HOOK *)pathtree_find( cache_tree, key ) ;
103         
104         SAFE_FREE( key );
105         
106         return hook;
107 }
108
109 /**********************************************************************
110  Initialize the cache tree
111  *********************************************************************/
112
113 void reghook_dump_cache( int debuglevel )
114 {
115         DEBUG(debuglevel,("reghook_dump_cache: Starting cache dump now...\n"));
116         
117         pathtree_print_keys( cache_tree, debuglevel );
118 }