r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[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_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  Add a new registry value to the array
275  **********************************************************************/
276
277 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type, 
278                          const char *data_p, size_t size )
279 {
280         if ( !name )
281                 return ctr->num_values;
282
283         /* Delete the current value (if it exists) and add the new one */
284
285         regval_ctr_delvalue( ctr, name );
286
287         /* allocate a slot in the array of pointers */
288                 
289         if (  ctr->num_values == 0 ) {
290                 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
291         } else {
292                 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
293         }
294
295         if (!ctr->values) {
296                 ctr->num_values = 0;
297                 return 0;
298         }
299
300         /* allocate a new value and store the pointer in the arrya */
301                 
302         ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
303         if (!ctr->values[ctr->num_values]) {
304                 ctr->num_values = 0;
305                 return 0;
306         }
307
308         /* init the value */
309         
310         fstrcpy( ctr->values[ctr->num_values]->valuename, name );
311         ctr->values[ctr->num_values]->type = type;
312         if (size) {
313                 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
314                         ctr, data_p, size );
315                 if (!ctr->values[ctr->num_values]->data_p) {
316                         ctr->num_values = 0;
317                         return 0;
318                 }
319         } else {
320                 ctr->values[ctr->num_values]->data_p = NULL;
321         }
322         ctr->values[ctr->num_values]->size = size;
323         ctr->num_values++;
324
325         return ctr->num_values;
326 }
327
328 /***********************************************************************
329  Add a new registry value to the array
330  **********************************************************************/
331
332 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
333 {
334         if ( val ) {
335                 /* allocate a slot in the array of pointers */
336                 
337                 if (  ctr->num_values == 0 ) {
338                         ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
339                 } else {
340                         ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
341                 }
342
343                 if (!ctr->values) {
344                         ctr->num_values = 0;
345                         return 0;
346                 }
347
348                 /* allocate a new value and store the pointer in the arrya */
349                 
350                 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
351                 if (!ctr->values[ctr->num_values]) {
352                         ctr->num_values = 0;
353                         return 0;
354                 }
355
356                 /* init the value */
357         
358                 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
359                 ctr->values[ctr->num_values]->type = val->type;
360                 if (val->size) {
361                         ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
362                                 ctr, val->data_p, val->size );
363                         if (!ctr->values[ctr->num_values]->data_p) {
364                                 ctr->num_values = 0;
365                                 return 0;
366                         }
367                 } else {
368                         ctr->values[ctr->num_values]->data_p = NULL;
369                 }
370                 ctr->values[ctr->num_values]->size = val->size;
371                 ctr->num_values++;
372         }
373
374         return ctr->num_values;
375 }
376
377 /***********************************************************************
378  Delete a single value from the registry container.
379  No need to free memory since it is talloc'd.
380  **********************************************************************/
381
382 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
383 {
384         int     i;
385         
386         for ( i=0; i<ctr->num_values; i++ ) {
387                 if ( strequal( ctr->values[i]->valuename, name ) )
388                         break;
389         }
390         
391         /* just return if we don't find it */
392         
393         if ( i == ctr->num_values )
394                 return ctr->num_values;
395         
396         /* If 'i' was not the last element, just shift everything down one */
397         ctr->num_values--;
398         if ( i < ctr->num_values )
399                 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
400         
401         return ctr->num_values;
402 }
403
404 /***********************************************************************
405  Retrieve single value from the registry container.
406  No need to free memory since it is talloc'd.
407  **********************************************************************/
408
409 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
410 {
411         int     i;
412         
413         /* search for the value */
414         
415         for ( i=0; i<ctr->num_values; i++ ) {
416                 if ( strequal( ctr->values[i]->valuename, name ) )
417                         return ctr->values[i];
418         }
419         
420         return NULL;
421 }
422
423 /***********************************************************************
424  return the data_p as a uint32
425  **********************************************************************/
426
427 uint32 regval_dword( REGISTRY_VALUE *val )
428 {
429         uint32 data;
430         
431         data = IVAL( regval_data_p(val), 0 );
432         
433         return data;
434 }
435
436 /***********************************************************************
437  return the data_p as a character string
438  **********************************************************************/
439
440 char* regval_sz( REGISTRY_VALUE *val )
441 {
442         static pstring data;
443
444         rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
445         
446         return data;
447 }