s3:registry: replace typedef "REGSUBKEY_CTR" by "struct regsubkey_ctr"
[amitay/samba.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 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_REGISTRY
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( struct 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( struct 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],
101                         sizeof(char*) * (ctr->num_subkeys-i));
102
103         return ctr->num_subkeys;
104 }
105
106 /***********************************************************************
107  Check for the existance of a key
108  **********************************************************************/
109
110 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
111 {
112         int     i;
113
114         if (!ctr->subkeys) {
115                 return False;
116         }
117
118         for ( i=0; i<ctr->num_subkeys; i++ ) {
119                 if ( strequal( ctr->subkeys[i],keyname ) )
120                         return True;
121         }
122
123         return False;
124 }
125
126 /***********************************************************************
127  How many keys does the container hold ?
128  **********************************************************************/
129
130 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
131 {
132         return ctr->num_subkeys;
133 }
134
135 /***********************************************************************
136  Retreive a specific key string
137  **********************************************************************/
138
139 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32 key_index )
140 {
141         if ( ! (key_index < ctr->num_subkeys) )
142                 return NULL;
143
144         return ctr->subkeys[key_index];
145 }
146
147 /*
148  * Utility functions for REGVAL_CTR
149  */
150
151 /***********************************************************************
152  How many keys does the container hold ?
153  **********************************************************************/
154
155 int regval_ctr_numvals( REGVAL_CTR *ctr )
156 {
157         return ctr->num_values;
158 }
159
160 /***********************************************************************
161  allocate memory for and duplicate a REGISTRY_VALUE.
162  This is malloc'd memory so the caller should free it when done
163  **********************************************************************/
164
165 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
166 {
167         REGISTRY_VALUE  *copy = NULL;
168
169         if ( !val )
170                 return NULL;
171
172         if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
173                 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
174                 return NULL;
175         }
176
177         /* copy all the non-pointer initial data */
178
179         memcpy( copy, val, sizeof(REGISTRY_VALUE) );
180
181         copy->size = 0;
182         copy->data_p = NULL;
183
184         if ( val->data_p && val->size )
185         {
186                 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
187                                                        val->size )) ) {
188                         DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
189                                  "bytes!\n", val->size));
190                         SAFE_FREE( copy );
191                         return NULL;
192                 }
193                 copy->size = val->size;
194         }
195
196         return copy;
197 }
198
199 /**********************************************************************
200  free the memory allocated to a REGISTRY_VALUE
201  *********************************************************************/
202
203 void free_registry_value( REGISTRY_VALUE *val )
204 {
205         if ( !val )
206                 return;
207
208         SAFE_FREE( val->data_p );
209         SAFE_FREE( val );
210
211         return;
212 }
213
214 /**********************************************************************
215  *********************************************************************/
216
217 uint8* regval_data_p( REGISTRY_VALUE *val )
218 {
219         return val->data_p;
220 }
221
222 /**********************************************************************
223  *********************************************************************/
224
225 uint32 regval_size( REGISTRY_VALUE *val )
226 {
227         return val->size;
228 }
229
230 /**********************************************************************
231  *********************************************************************/
232
233 char* regval_name( REGISTRY_VALUE *val )
234 {
235         return val->valuename;
236 }
237
238 /**********************************************************************
239  *********************************************************************/
240
241 uint32 regval_type( REGISTRY_VALUE *val )
242 {
243         return val->type;
244 }
245
246 /***********************************************************************
247  Retreive a pointer to a specific value.  Caller shoud dup the structure
248  since this memory will go away when the ctr is free()'d
249  **********************************************************************/
250
251 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
252 {
253         if ( !(idx < ctr->num_values) )
254                 return NULL;
255
256         return ctr->values[idx];
257 }
258
259 /***********************************************************************
260  Check for the existance of a value
261  **********************************************************************/
262
263 bool regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
264 {
265         int     i;
266
267         for ( i=0; i<ctr->num_values; i++ ) {
268                 if ( strequal( ctr->values[i]->valuename, value) )
269                         return True;
270         }
271
272         return False;
273 }
274
275 /***********************************************************************
276  * compose a REGISTRY_VALUE from input data
277  **********************************************************************/
278
279 REGISTRY_VALUE *regval_compose(TALLOC_CTX *ctx, const char *name, uint16 type,
280                                const char *data_p, size_t size)
281 {
282         REGISTRY_VALUE *regval = TALLOC_P(ctx, REGISTRY_VALUE);
283
284         if (regval == NULL) {
285                 return NULL;
286         }
287
288         fstrcpy(regval->valuename, name);
289         regval->type = type;
290         if (size) {
291                 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
292                 if (!regval->data_p) {
293                         TALLOC_FREE(regval);
294                         return NULL;
295                 }
296         } else {
297                 regval->data_p = NULL;
298         }
299         regval->size = size;
300
301         return regval;
302 }
303
304 /***********************************************************************
305  Add a new registry value to the array
306  **********************************************************************/
307
308 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
309                          const char *data_p, size_t size )
310 {
311         if ( !name )
312                 return ctr->num_values;
313
314         /* Delete the current value (if it exists) and add the new one */
315
316         regval_ctr_delvalue( ctr, name );
317
318         /* allocate a slot in the array of pointers */
319
320         if (  ctr->num_values == 0 ) {
321                 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
322         } else {
323                 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
324                                                    REGISTRY_VALUE *,
325                                                    ctr->num_values+1);
326         }
327
328         if (!ctr->values) {
329                 ctr->num_values = 0;
330                 return 0;
331         }
332
333         /* allocate a new value and store the pointer in the arrya */
334
335         ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
336                                                       size);
337         if (ctr->values[ctr->num_values] == NULL) {
338                 ctr->num_values = 0;
339                 return 0;
340         }
341         ctr->num_values++;
342
343         return ctr->num_values;
344 }
345
346 /***********************************************************************
347  Add a new registry value to the array
348  **********************************************************************/
349
350 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
351 {
352         if ( val ) {
353                 regval_ctr_addvalue(ctr, val->valuename, val->type,
354                                     (char *)val->data_p, val->size);
355         }
356
357         return ctr->num_values;
358 }
359
360 /***********************************************************************
361  Delete a single value from the registry container.
362  No need to free memory since it is talloc'd.
363  **********************************************************************/
364
365 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
366 {
367         int     i;
368
369         for ( i=0; i<ctr->num_values; i++ ) {
370                 if ( strequal( ctr->values[i]->valuename, name ) )
371                         break;
372         }
373
374         /* just return if we don't find it */
375
376         if ( i == ctr->num_values )
377                 return ctr->num_values;
378
379         /* If 'i' was not the last element, just shift everything down one */
380         ctr->num_values--;
381         if ( i < ctr->num_values )
382                 memmove(&ctr->values[i], &ctr->values[i+1],
383                         sizeof(REGISTRY_VALUE*)*(ctr->num_values-i));
384
385         return ctr->num_values;
386 }
387
388 /***********************************************************************
389  Retrieve single value from the registry container.
390  No need to free memory since it is talloc'd.
391  **********************************************************************/
392
393 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
394 {
395         int     i;
396
397         /* search for the value */
398
399         for ( i=0; i<ctr->num_values; i++ ) {
400                 if ( strequal( ctr->values[i]->valuename, name ) )
401                         return ctr->values[i];
402         }
403
404         return NULL;
405 }
406
407 /***********************************************************************
408  return the data_p as a uint32
409  **********************************************************************/
410
411 uint32 regval_dword( REGISTRY_VALUE *val )
412 {
413         uint32 data;
414
415         data = IVAL( regval_data_p(val), 0 );
416
417         return data;
418 }
419
420 /***********************************************************************
421  return the data_p as a character string
422  **********************************************************************/
423
424 char *regval_sz(REGISTRY_VALUE *val)
425 {
426         char *data = NULL;
427
428         rpcstr_pull_talloc(talloc_tos(), &data,
429                         regval_data_p(val), regval_size(val),0);
430         return data;
431 }