r7997: Pointers don't kill people, people with pointers kill people...
[ira/wip.git] / source3 / registry / reg_objects.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Gerald Carter                     2002-2005
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 2 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, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /* Implementation of registry frontend view functions. */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
27
28
29 /***********************************************************************
30  Init the talloc context held by a REGSUBKEY_CTR structure
31  This now zero's the structure
32  **********************************************************************/
33
34 void regsubkey_ctr_init( REGSUBKEY_CTR *ctr )
35 {
36         ZERO_STRUCTP( ctr );
37         ctr->ctx = talloc_init("regsubkey_ctr_init for ctr %p", ctr);
38 }
39
40 /***********************************************************************
41  Add a new key to the array
42  **********************************************************************/
43
44 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
45 {
46         char **pp;
47
48         if ( !keyname )
49                 return ctr->num_subkeys;
50
51         /* make sure the keyname is not already there */
52
53         if ( regsubkey_ctr_key_exists( ctr, keyname ) )
54                 return ctr->num_subkeys;
55                 
56         /* allocate a space for the char* in the array */
57                 
58         if (  ctr->subkeys == 0 )
59                 ctr->subkeys = TALLOC_P( ctr->ctx, char *);
60         else {
61                 pp = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->subkeys, char *, ctr->num_subkeys+1);
62                 if ( pp )
63                         ctr->subkeys = pp;
64         }
65
66         /* allocate the string and save it in the array */
67         
68         ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr->ctx, keyname );
69         ctr->num_subkeys++;
70         
71         return ctr->num_subkeys;
72 }
73  
74  /***********************************************************************
75  Add a new key to the array
76  **********************************************************************/
77
78 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
79 {
80         int i;
81
82         if ( !keyname )
83                 return ctr->num_subkeys;
84
85         /* make sure the keyname is actually already there */
86
87         for ( i=0; i<ctr->num_subkeys; i++ ) {
88                 if ( strequal( ctr->subkeys[i], keyname ) )
89                         break;
90         }
91         
92         if ( i == ctr->num_subkeys )
93                 return ctr->num_subkeys;
94
95         /* update if we have any keys left */
96         ctr->num_subkeys--;
97         if ( i < ctr->num_subkeys )
98                 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
99         
100         return ctr->num_subkeys;
101 }
102
103 /***********************************************************************
104  Check for the existance of a key
105  **********************************************************************/
106
107 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
108 {
109         int     i;
110         
111         for ( i=0; i<ctr->num_subkeys; i++ ) {
112                 if ( strequal( ctr->subkeys[i],keyname ) )
113                         return True;
114         }
115         
116         return False;
117 }
118
119 /***********************************************************************
120  How many keys does the container hold ?
121  **********************************************************************/
122
123 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
124 {
125         return ctr->num_subkeys;
126 }
127
128 /***********************************************************************
129  Retreive a specific key string
130  **********************************************************************/
131
132 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
133 {
134         if ( ! (key_index < ctr->num_subkeys) )
135                 return NULL;
136                 
137         return ctr->subkeys[key_index];
138 }
139
140 /***********************************************************************
141  free memory held by a REGSUBKEY_CTR structure
142  **********************************************************************/
143
144 void regsubkey_ctr_destroy( REGSUBKEY_CTR *ctr )
145 {
146         if ( ctr ) {
147                 talloc_destroy( ctr->ctx );     
148                 ZERO_STRUCTP( ctr );
149         }
150 }
151
152
153 /*
154  * Utility functions for REGVAL_CTR
155  */
156
157 /***********************************************************************
158  Init the talloc context held by a REGSUBKEY_CTR structure
159  This now zero's the structure
160  **********************************************************************/
161
162 void regval_ctr_init( REGVAL_CTR *ctr )
163 {
164         ZERO_STRUCTP( ctr );
165         ctr->ctx = talloc_init("regval_ctr_init for ctr %p", ctr);
166 }
167
168 /***********************************************************************
169  How many keys does the container hold ?
170  **********************************************************************/
171
172 int regval_ctr_numvals( REGVAL_CTR *ctr )
173 {
174         return ctr->num_values;
175 }
176
177 /***********************************************************************
178  allocate memory for and duplicate a REGISTRY_VALUE.
179  This is malloc'd memory so the caller should free it when done
180  **********************************************************************/
181
182 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
183 {
184         REGISTRY_VALUE  *copy = NULL;
185         
186         if ( !val )
187                 return NULL;
188         
189         if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
190                 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
191                 return NULL;
192         }
193         
194         /* copy all the non-pointer initial data */
195         
196         memcpy( copy, val, sizeof(REGISTRY_VALUE) );
197         if ( val->data_p ) 
198         {
199                 if ( !(copy->data_p = memdup( val->data_p, val->size )) ) {
200                         DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
201                                 val->size));
202                         SAFE_FREE( copy );
203                 }
204         }
205         
206         return copy;    
207 }
208
209 /**********************************************************************
210  free the memory allocated to a REGISTRY_VALUE 
211  *********************************************************************/
212  
213 void free_registry_value( REGISTRY_VALUE *val )
214 {
215         if ( !val )
216                 return;
217                 
218         SAFE_FREE( val->data_p );
219         SAFE_FREE( val );
220         
221         return;
222 }
223
224 /**********************************************************************
225  *********************************************************************/
226
227 uint8* regval_data_p( REGISTRY_VALUE *val )
228 {
229         return val->data_p;
230 }
231
232 /**********************************************************************
233  *********************************************************************/
234
235 uint32 regval_size( REGISTRY_VALUE *val )
236 {
237         return val->size;
238 }
239
240 /**********************************************************************
241  *********************************************************************/
242
243 char* regval_name( REGISTRY_VALUE *val )
244 {
245         return val->valuename;
246 }
247
248 /**********************************************************************
249  *********************************************************************/
250
251 uint32 regval_type( REGISTRY_VALUE *val )
252 {
253         return val->type;
254 }
255
256 /***********************************************************************
257  Retreive a pointer to a specific value.  Caller shoud dup the structure
258  since this memory may go away with a regval_ctr_destroy()
259  **********************************************************************/
260
261 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
262 {
263         if ( !(idx < ctr->num_values) )
264                 return NULL;
265                 
266         return ctr->values[idx];
267 }
268
269 /***********************************************************************
270  Retrive the TALLOC_CTX associated with a REGISTRY_VALUE 
271  **********************************************************************/
272
273 TALLOC_CTX* regval_ctr_getctx( REGVAL_CTR *val )
274 {
275         if ( !val )
276                 return NULL;
277
278         return val->ctx; }
279
280 /***********************************************************************
281  Check for the existance of a value
282  **********************************************************************/
283
284 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
285 {
286         int     i;
287         
288         for ( i=0; i<ctr->num_values; i++ ) {
289                 if ( strequal( ctr->values[i]->valuename, value) )
290                         return True;
291         }
292         
293         return False;
294 }
295 /***********************************************************************
296  Add a new registry value to the array
297  **********************************************************************/
298
299 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type, 
300                          const char *data_p, size_t size )
301 {
302         REGISTRY_VALUE **ppreg;
303         
304         if ( !name )
305                 return ctr->num_values;
306
307         /* Delete the current value (if it exists) and add the new one */
308
309         regval_ctr_delvalue( ctr, name );
310
311         /* allocate a slot in the array of pointers */
312                 
313         if (  ctr->num_values == 0 )
314                 ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *);
315         else {
316                 ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
317                 if ( ppreg )
318                         ctr->values = ppreg;
319         }
320
321         /* allocate a new value and store the pointer in the arrya */
322                 
323         ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE);
324
325         /* init the value */
326         
327         fstrcpy( ctr->values[ctr->num_values]->valuename, name );
328         ctr->values[ctr->num_values]->type = type;
329         ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, data_p, size );
330         ctr->values[ctr->num_values]->size = size;
331         ctr->num_values++;
332
333         return ctr->num_values;
334 }
335
336 /***********************************************************************
337  Add a new registry value to the array
338  **********************************************************************/
339
340 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
341 {
342         REGISTRY_VALUE **ppreg;
343         
344         if ( val )
345         {
346                 /* allocate a slot in the array of pointers */
347                 
348                 if (  ctr->num_values == 0 )
349                         ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *);
350                 else {
351                         ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
352                         if ( ppreg )
353                                 ctr->values = ppreg;
354                 }
355
356                 /* allocate a new value and store the pointer in the arrya */
357                 
358                 ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE);
359
360                 /* init the value */
361         
362                 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
363                 ctr->values[ctr->num_values]->type = val->type;
364                 ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, val->data_p, val->size );
365                 ctr->values[ctr->num_values]->size = val->size;
366                 ctr->num_values++;
367         }
368
369         return ctr->num_values;
370 }
371
372 /***********************************************************************
373  Delete a single value from the registry container.
374  No need to free memory since it is talloc'd.
375  **********************************************************************/
376
377 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
378 {
379         int     i;
380         
381         for ( i=0; i<ctr->num_values; i++ ) {
382                 if ( strequal( ctr->values[i]->valuename, name ) )
383                         break;
384         }
385         
386         /* just return if we don't find it */
387         
388         if ( i == ctr->num_values )
389                 return ctr->num_values;
390         
391         /* If 'i' was not the last element, just shift everything down one */
392         ctr->num_values--;
393         if ( i < ctr->num_values )
394                 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
395         
396         return ctr->num_values;
397 }
398
399 /***********************************************************************
400  Retrieve single value from the registry container.
401  No need to free memory since it is talloc'd.
402  **********************************************************************/
403
404 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
405 {
406         int     i;
407         
408         /* search for the value */
409         
410         for ( i=0; i<ctr->num_values; i++ ) {
411                 if ( strequal( ctr->values[i]->valuename, name ) )
412                         return ctr->values[i];
413         }
414         
415         return NULL;
416 }
417
418 /***********************************************************************
419  free memory held by a REGVAL_CTR structure
420  **********************************************************************/
421
422 void regval_ctr_destroy( REGVAL_CTR *ctr )
423 {
424         if ( ctr ) {
425                 talloc_destroy( ctr->ctx );
426                 ZERO_STRUCTP( ctr );
427         }
428 }
429
430