ba37b9014b540ca501f790ba1b56cafcaa79a7f4
[samba.git] / source / 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 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 frontend view functions. */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_RPC_SRV
26
27 /**********************************************************************
28
29  Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d
30  since the methods use the object pointer as the talloc context for 
31  internal private data.
32
33  There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy()
34  pair of functions.  Simply TALLOC_ZERO_P() and TALLOC_FREE() the 
35  object.
36
37  **********************************************************************/
38
39 /***********************************************************************
40  Add a new key to the array
41  **********************************************************************/
42
43 WERROR regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
44 {
45         char **newkeys;
46
47         if ( !keyname ) {
48                 return WERR_OK;
49         }
50
51         /* make sure the keyname is not already there */
52
53         if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
54                 return WERR_OK;
55         }
56
57         if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
58                                              ctr->num_subkeys+1))) {
59                 return WERR_NOMEM;
60         }
61
62         ctr->subkeys = newkeys;
63
64         if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
65                                                              keyname ))) {
66                 /*
67                  * Don't shrink the new array again, this wastes a pointer
68                  */
69                 return WERR_NOMEM;
70         }
71         ctr->num_subkeys++;
72
73         return WERR_OK;
74 }
75  
76  /***********************************************************************
77  Delete a key from the array
78  **********************************************************************/
79
80 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
81 {
82         int i;
83
84         if ( !keyname )
85                 return ctr->num_subkeys;
86
87         /* make sure the keyname is actually already there */
88
89         for ( i=0; i<ctr->num_subkeys; i++ ) {
90                 if ( strequal( ctr->subkeys[i], keyname ) )
91                         break;
92         }
93         
94         if ( i == ctr->num_subkeys )
95                 return ctr->num_subkeys;
96
97         /* update if we have any keys left */
98         ctr->num_subkeys--;
99         if ( i < ctr->num_subkeys )
100                 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
101         
102         return ctr->num_subkeys;
103 }
104
105 /***********************************************************************
106  Check for the existance of a key
107  **********************************************************************/
108
109 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
110 {
111         int     i;
112         
113         if (!ctr->subkeys) {
114                 return False;
115         }
116
117         for ( i=0; i<ctr->num_subkeys; i++ ) {
118                 if ( strequal( ctr->subkeys[i],keyname ) )
119                         return True;
120         }
121         
122         return False;
123 }
124
125 /***********************************************************************
126  How many keys does the container hold ?
127  **********************************************************************/
128
129 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
130 {
131         return ctr->num_subkeys;
132 }
133
134 /***********************************************************************
135  Retreive a specific key string
136  **********************************************************************/
137
138 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
139 {
140         if ( ! (key_index < ctr->num_subkeys) )
141                 return NULL;
142                 
143         return ctr->subkeys[key_index];
144 }
145
146 /*
147  * Utility functions for REGVAL_CTR
148  */
149
150 /***********************************************************************
151  How many keys does the container hold ?
152  **********************************************************************/
153
154 int regval_ctr_numvals( REGVAL_CTR *ctr )
155 {
156         return ctr->num_values;
157 }
158
159 /***********************************************************************
160  allocate memory for and duplicate a REGISTRY_VALUE.
161  This is malloc'd memory so the caller should free it when done
162  **********************************************************************/
163
164 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
165 {
166         REGISTRY_VALUE  *copy = NULL;
167         
168         if ( !val )
169                 return NULL;
170         
171         if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
172                 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
173                 return NULL;
174         }
175         
176         /* copy all the non-pointer initial data */
177         
178         memcpy( copy, val, sizeof(REGISTRY_VALUE) );
179         
180         copy->size = 0;
181         copy->data_p = NULL;
182         
183         if ( val->data_p && val->size ) 
184         {
185                 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
186                                                        val->size )) ) {
187                         DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
188                                 val->size));
189                         SAFE_FREE( copy );
190                         return NULL;
191                 }
192                 copy->size = val->size;
193         }
194         
195         return copy;    
196 }
197
198 /**********************************************************************
199  free the memory allocated to a REGISTRY_VALUE 
200  *********************************************************************/
201  
202 void free_registry_value( REGISTRY_VALUE *val )
203 {
204         if ( !val )
205                 return;
206                 
207         SAFE_FREE( val->data_p );
208         SAFE_FREE( val );
209         
210         return;
211 }
212
213 /**********************************************************************
214  *********************************************************************/
215
216 uint8* regval_data_p( REGISTRY_VALUE *val )
217 {
218         return val->data_p;
219 }
220
221 /**********************************************************************
222  *********************************************************************/
223
224 uint32 regval_size( REGISTRY_VALUE *val )
225 {
226         return val->size;
227 }
228
229 /**********************************************************************
230  *********************************************************************/
231
232 char* regval_name( REGISTRY_VALUE *val )
233 {
234         return val->valuename;
235 }
236
237 /**********************************************************************
238  *********************************************************************/
239
240 uint32 regval_type( REGISTRY_VALUE *val )
241 {
242         return val->type;
243 }
244
245 /***********************************************************************
246  Retreive a pointer to a specific value.  Caller shoud dup the structure
247  since this memory will go away when the ctr is free()'d
248  **********************************************************************/
249
250 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
251 {
252         if ( !(idx < ctr->num_values) )
253                 return NULL;
254                 
255         return ctr->values[idx];
256 }
257
258 /***********************************************************************
259  Check for the existance of a value
260  **********************************************************************/
261
262 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
263 {
264         int     i;
265         
266         for ( i=0; i<ctr->num_values; i++ ) {
267                 if ( strequal( ctr->values[i]->valuename, value) )
268                         return True;
269         }
270         
271         return False;
272 }
273
274 /***********************************************************************
275  * compose a REGISTRY_VALUE from input data
276  **********************************************************************/
277
278 REGISTRY_VALUE *regval_compose(TALLOC_CTX *ctx, const char *name, uint16 type,
279                                const char *data_p, size_t size)
280 {
281         REGISTRY_VALUE *regval = TALLOC_P(ctx, REGISTRY_VALUE);
282
283         if (regval == NULL) {
284                 return NULL;
285         }
286
287         fstrcpy(regval->valuename, name);
288         regval->type = type;
289         if (size) {
290                 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
291                 if (!regval->data_p) {
292                         TALLOC_FREE(regval);
293                         return NULL;
294                 }
295         } else {
296                 regval->data_p = NULL;
297         }
298         regval->size = size;
299
300         return regval;
301 }
302
303 /***********************************************************************
304  Add a new registry value to the array
305  **********************************************************************/
306
307 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type, 
308                          const char *data_p, size_t size )
309 {
310         if ( !name )
311                 return ctr->num_values;
312
313         /* Delete the current value (if it exists) and add the new one */
314
315         regval_ctr_delvalue( ctr, name );
316
317         /* allocate a slot in the array of pointers */
318                 
319         if (  ctr->num_values == 0 ) {
320                 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
321         } else {
322                 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
323         }
324
325         if (!ctr->values) {
326                 ctr->num_values = 0;
327                 return 0;
328         }
329
330         /* allocate a new value and store the pointer in the arrya */
331                 
332         ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
333         if (!ctr->values[ctr->num_values]) {
334                 ctr->num_values = 0;
335                 return 0;
336         }
337
338         /* init the value */
339         
340         fstrcpy( ctr->values[ctr->num_values]->valuename, name );
341         ctr->values[ctr->num_values]->type = type;
342         if (size) {
343                 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
344                         ctr, data_p, size );
345                 if (!ctr->values[ctr->num_values]->data_p) {
346                         ctr->num_values = 0;
347                         return 0;
348                 }
349         } else {
350                 ctr->values[ctr->num_values]->data_p = NULL;
351         }
352         ctr->values[ctr->num_values]->size = size;
353         ctr->num_values++;
354
355         return ctr->num_values;
356 }
357
358 /***********************************************************************
359  Add a new registry value to the array
360  **********************************************************************/
361
362 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
363 {
364         if ( val ) {
365                 /* allocate a slot in the array of pointers */
366                 
367                 if (  ctr->num_values == 0 ) {
368                         ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
369                 } else {
370                         ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
371                 }
372
373                 if (!ctr->values) {
374                         ctr->num_values = 0;
375                         return 0;
376                 }
377
378                 /* allocate a new value and store the pointer in the arrya */
379                 
380                 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
381                 if (!ctr->values[ctr->num_values]) {
382                         ctr->num_values = 0;
383                         return 0;
384                 }
385
386                 /* init the value */
387         
388                 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
389                 ctr->values[ctr->num_values]->type = val->type;
390                 if (val->size) {
391                         ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
392                                 ctr, val->data_p, val->size );
393                         if (!ctr->values[ctr->num_values]->data_p) {
394                                 ctr->num_values = 0;
395                                 return 0;
396                         }
397                 } else {
398                         ctr->values[ctr->num_values]->data_p = NULL;
399                 }
400                 ctr->values[ctr->num_values]->size = val->size;
401                 ctr->num_values++;
402         }
403
404         return ctr->num_values;
405 }
406
407 /***********************************************************************
408  Delete a single value from the registry container.
409  No need to free memory since it is talloc'd.
410  **********************************************************************/
411
412 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
413 {
414         int     i;
415         
416         for ( i=0; i<ctr->num_values; i++ ) {
417                 if ( strequal( ctr->values[i]->valuename, name ) )
418                         break;
419         }
420         
421         /* just return if we don't find it */
422         
423         if ( i == ctr->num_values )
424                 return ctr->num_values;
425         
426         /* If 'i' was not the last element, just shift everything down one */
427         ctr->num_values--;
428         if ( i < ctr->num_values )
429                 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
430         
431         return ctr->num_values;
432 }
433
434 /***********************************************************************
435  Retrieve single value from the registry container.
436  No need to free memory since it is talloc'd.
437  **********************************************************************/
438
439 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
440 {
441         int     i;
442         
443         /* search for the value */
444         
445         for ( i=0; i<ctr->num_values; i++ ) {
446                 if ( strequal( ctr->values[i]->valuename, name ) )
447                         return ctr->values[i];
448         }
449         
450         return NULL;
451 }
452
453 /***********************************************************************
454  return the data_p as a uint32
455  **********************************************************************/
456
457 uint32 regval_dword( REGISTRY_VALUE *val )
458 {
459         uint32 data;
460         
461         data = IVAL( regval_data_p(val), 0 );
462         
463         return data;
464 }
465
466 /***********************************************************************
467  return the data_p as a character string
468  **********************************************************************/
469
470 char* regval_sz( REGISTRY_VALUE *val )
471 {
472         pstring data;
473
474         rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
475         
476         return talloc_strdup(talloc_tos(), data);
477 }