This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[nivanova/samba-autobuild/.git] / source3 / registry / reg_objects.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
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 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  **********************************************************************/
32
33 void regsubkey_ctr_init( REGSUBKEY_CTR *ctr )
34 {
35         if ( !ctr->ctx )
36                 ctr->ctx = talloc_init();
37 }
38
39 /***********************************************************************
40  Add a new key to the array
41  **********************************************************************/
42
43 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, char *keyname )
44 {
45         uint32 len;
46         char **pp;
47         
48         if ( keyname )
49         {
50                 len = strlen( keyname );
51
52                 /* allocate a space for the char* in the array */
53                 
54                 if (  ctr->subkeys == 0 )
55                         ctr->subkeys = talloc( ctr->ctx, sizeof(char*) );
56                 else {
57                         pp = talloc_realloc( ctr->ctx, ctr->subkeys, sizeof(char*)*(ctr->num_subkeys+1) );
58                         if ( pp )
59                                 ctr->subkeys = pp;
60                 }
61
62                 /* allocate the string and save it in the array */
63                 
64                 ctr->subkeys[ctr->num_subkeys] = talloc( ctr->ctx, len+1 );
65                 strncpy( ctr->subkeys[ctr->num_subkeys], keyname, len+1 );
66                 ctr->num_subkeys++;
67         }
68         
69         return ctr->num_subkeys;
70 }
71  
72 /***********************************************************************
73  How many keys does the container hold ?
74  **********************************************************************/
75
76 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
77 {
78         return ctr->num_subkeys;
79 }
80
81 /***********************************************************************
82  Retreive a specific key string
83  **********************************************************************/
84
85 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
86 {
87         if ( ! (key_index < ctr->num_subkeys) )
88                 return NULL;
89                 
90         return ctr->subkeys[key_index];
91 }
92
93 /***********************************************************************
94  free memory held by a REGSUBKEY_CTR structure
95  **********************************************************************/
96
97 void regsubkey_ctr_destroy( REGSUBKEY_CTR *ctr )
98 {
99         if ( ctr ) {
100                 talloc_destroy( ctr->ctx );     
101                 ZERO_STRUCTP( ctr );
102         }
103 }
104
105
106 /*
107  * Utility functions for REGVAL_CTR
108  */
109
110 /***********************************************************************
111  Init the talloc context held by a REGSUBKEY_CTR structure
112  **********************************************************************/
113
114 void regval_ctr_init( REGVAL_CTR *ctr )
115 {
116         if ( !ctr->ctx )
117                 ctr->ctx = talloc_init();
118 }
119
120 /***********************************************************************
121  How many keys does the container hold ?
122  **********************************************************************/
123
124 int regval_ctr_numvals( REGVAL_CTR *ctr )
125 {
126         return ctr->num_values;
127 }
128
129 /***********************************************************************
130  allocate memory for and duplicate a REGISTRY_VALUE.
131  This is malloc'd memory so the caller should free it when done
132  **********************************************************************/
133
134 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
135 {
136         REGISTRY_VALUE  *copy = NULL;
137         
138         if ( !val )
139                 return NULL;
140         
141         if ( !(copy = malloc( sizeof(REGISTRY_VALUE) )) ) {
142                 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
143                 return NULL;
144         }
145         
146         /* copy all the non-pointer initial data */
147         
148         memcpy( copy, val, sizeof(REGISTRY_VALUE) );
149         if ( val->data_p ) 
150         {
151                 if ( !(copy->data_p = memdup( val->data_p, val->size )) ) {
152                         DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
153                                 val->size));
154                         SAFE_FREE( copy );
155                 }
156         }
157         
158         return copy;    
159 }
160
161 /**********************************************************************
162  free the memory allocated to a REGISTRY_VALUE 
163  *********************************************************************/
164  
165 void free_registry_value( REGISTRY_VALUE *val )
166 {
167         if ( !val )
168                 return;
169                 
170         SAFE_FREE( val->data_p );
171         SAFE_FREE( val );
172         
173         return;
174 }
175
176 /**********************************************************************
177  *********************************************************************/
178
179 uint8* regval_data_p( REGISTRY_VALUE *val )
180 {
181         return val->data_p;
182 }
183
184 /**********************************************************************
185  *********************************************************************/
186
187 int regval_size( REGISTRY_VALUE *val )
188 {
189         return val->size;
190 }
191
192 /**********************************************************************
193  *********************************************************************/
194
195 char* regval_name( REGISTRY_VALUE *val )
196 {
197         return val->valuename;
198 }
199
200 /**********************************************************************
201  *********************************************************************/
202
203 uint32 regval_type( REGISTRY_VALUE *val )
204 {
205         return val->type;
206 }
207
208 /***********************************************************************
209  Retreive a pointer to a specific value.  Caller shoud dup the structure
210  since this memory may go away with a regval_ctr_destroy()
211  **********************************************************************/
212
213 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
214 {
215         if ( !(idx < ctr->num_values) )
216                 return NULL;
217                 
218         return ctr->values[idx];
219 }
220
221 /***********************************************************************
222  Retrive the TALLOC_CTX associated with a REGISTRY_VALUE 
223  **********************************************************************/
224
225 TALLOC_CTX* regval_ctr_getctx( REGVAL_CTR *val )
226 {
227         if ( !val )
228                 return NULL;
229
230         return val->ctx;
231 }
232
233 /***********************************************************************
234  Add a new registry value to the array
235  **********************************************************************/
236
237 int regval_ctr_addvalue( REGVAL_CTR *ctr, char *name, uint16 type, 
238                          char *data_p, size_t size )
239 {
240         REGISTRY_VALUE **ppreg;
241         
242         if ( name )
243         {
244                 /* allocate a slot in the array of pointers */
245                 
246                 if (  ctr->num_values == 0 )
247                         ctr->values = talloc( ctr->ctx, sizeof(REGISTRY_VALUE*) );
248                 else {
249                         ppreg = talloc_realloc( ctr->ctx, ctr->values, sizeof(REGISTRY_VALUE*)*(ctr->num_values+1) );
250                         if ( ppreg )
251                                 ctr->values = ppreg;
252                 }
253
254                 /* allocate a new value and store the pointer in the arrya */
255                 
256                 ctr->values[ctr->num_values] = talloc( ctr->ctx, sizeof(REGISTRY_VALUE) );
257
258                 /* init the value */
259         
260                 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
261                 ctr->values[ctr->num_values]->type = type;
262                 ctr->values[ctr->num_values]->data_p = talloc_memdup( ctr->ctx, data_p, size );
263                 ctr->values[ctr->num_values]->size = size;
264                 ctr->num_values++;
265         }
266
267         return ctr->num_values;
268 }
269
270 /***********************************************************************
271  Add a new registry value to the array
272  **********************************************************************/
273
274 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
275 {
276         REGISTRY_VALUE **ppreg;
277         
278         if ( val )
279         {
280                 /* allocate a slot in the array of pointers */
281                 
282                 if (  ctr->num_values == 0 )
283                         ctr->values = talloc( ctr->ctx, sizeof(REGISTRY_VALUE*) );
284                 else {
285                         ppreg = talloc_realloc( ctr->ctx, ctr->values, sizeof(REGISTRY_VALUE*)*(ctr->num_values+1) );
286                         if ( ppreg )
287                                 ctr->values = ppreg;
288                 }
289
290                 /* allocate a new value and store the pointer in the arrya */
291                 
292                 ctr->values[ctr->num_values] = talloc( ctr->ctx, sizeof(REGISTRY_VALUE) );
293
294                 /* init the value */
295         
296                 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
297                 ctr->values[ctr->num_values]->type = val->type;
298                 ctr->values[ctr->num_values]->data_p = talloc_memdup( ctr->ctx, val->data_p, val->size );
299                 ctr->values[ctr->num_values]->size = val->size;
300                 ctr->num_values++;
301         }
302
303         return ctr->num_values;
304 }
305
306 /***********************************************************************
307  Delete a single value from the registry container.
308  No need to free memory since it is talloc'd.
309  **********************************************************************/
310
311 int regval_ctr_delvalue( REGVAL_CTR *ctr, char *name )
312 {
313         int     i;
314         
315         /* search for the value */
316         
317         for ( i=0; i<ctr->num_values; i++ ) {
318                 if ( strcmp( ctr->values[i]->valuename, name ) == 0)
319                         break;
320         }
321         
322         /* just return if we don't find it */
323         
324         if ( i == ctr->num_values )
325                 return ctr->num_values;
326         
327         /* just shift everything down one */
328         
329         for ( /* use previous i */; i<(ctr->num_values-1); i++ )
330                 memcpy( ctr->values[i], ctr->values[i+1], sizeof(REGISTRY_VALUE) );
331                 
332         /* paranoia */
333         
334         ZERO_STRUCTP( ctr->values[i] );
335         
336         ctr->num_values--;
337         
338         return ctr->num_values;
339 }
340
341 /***********************************************************************
342  Delete a single value from the registry container.
343  No need to free memory since it is talloc'd.
344  **********************************************************************/
345
346 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, char *name )
347 {
348         int     i;
349         
350         /* search for the value */
351         
352         for ( i=0; i<ctr->num_values; i++ ) {
353                 if ( strequal( ctr->values[i]->valuename, name ) )
354                         return ctr->values[i];
355         }
356         
357         return NULL;
358 }
359
360 /***********************************************************************
361  free memory held by a REGVAL_CTR structure
362  **********************************************************************/
363
364 void regval_ctr_destroy( REGVAL_CTR *ctr )
365 {
366         if ( ctr ) {
367                 talloc_destroy( ctr->ctx );
368                 ZERO_STRUCTP( ctr );
369         }
370 }
371
372