2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
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.
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.
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.
21 /* Implementation of registry frontend view functions. */
26 #define DBGC_CLASS DBGC_RPC_SRV
28 /**********************************************************************
30 Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d
31 since the methods use the object pointer as the talloc context for
32 internal private data.
34 There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy()
35 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
38 **********************************************************************/
40 /***********************************************************************
41 Add a new key to the array
42 **********************************************************************/
44 int regsubkey_ctr_addkey( REGSUBKEY_CTR
*ctr
, const char *keyname
)
49 return ctr
->num_subkeys
;
51 /* make sure the keyname is not already there */
53 if ( regsubkey_ctr_key_exists( ctr
, keyname
) )
54 return ctr
->num_subkeys
;
56 /* allocate a space for the char* in the array */
58 if ( ctr
->subkeys
== 0 )
59 ctr
->subkeys
= TALLOC_P( ctr
, char *);
61 pp
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->subkeys
, char *, ctr
->num_subkeys
+1);
66 /* allocate the string and save it in the array */
68 ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup( ctr
, keyname
);
71 return ctr
->num_subkeys
;
74 /***********************************************************************
75 Add a new key to the array
76 **********************************************************************/
78 int regsubkey_ctr_delkey( REGSUBKEY_CTR
*ctr
, const char *keyname
)
83 return ctr
->num_subkeys
;
85 /* make sure the keyname is actually already there */
87 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
88 if ( strequal( ctr
->subkeys
[i
], keyname
) )
92 if ( i
== ctr
->num_subkeys
)
93 return ctr
->num_subkeys
;
95 /* update if we have any keys left */
97 if ( i
< ctr
->num_subkeys
)
98 memmove( &ctr
->subkeys
[i
], &ctr
->subkeys
[i
+1], sizeof(char*) * (ctr
->num_subkeys
-i
) );
100 return ctr
->num_subkeys
;
103 /***********************************************************************
104 Check for the existance of a key
105 **********************************************************************/
107 BOOL
regsubkey_ctr_key_exists( REGSUBKEY_CTR
*ctr
, const char *keyname
)
111 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
112 if ( strequal( ctr
->subkeys
[i
],keyname
) )
119 /***********************************************************************
120 How many keys does the container hold ?
121 **********************************************************************/
123 int regsubkey_ctr_numkeys( REGSUBKEY_CTR
*ctr
)
125 return ctr
->num_subkeys
;
128 /***********************************************************************
129 Retreive a specific key string
130 **********************************************************************/
132 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR
*ctr
, uint32 key_index
)
134 if ( ! (key_index
< ctr
->num_subkeys
) )
137 return ctr
->subkeys
[key_index
];
141 * Utility functions for REGVAL_CTR
144 /***********************************************************************
145 How many keys does the container hold ?
146 **********************************************************************/
148 int regval_ctr_numvals( REGVAL_CTR
*ctr
)
150 return ctr
->num_values
;
153 /***********************************************************************
154 allocate memory for and duplicate a REGISTRY_VALUE.
155 This is malloc'd memory so the caller should free it when done
156 **********************************************************************/
158 REGISTRY_VALUE
* dup_registry_value( REGISTRY_VALUE
*val
)
160 REGISTRY_VALUE
*copy
= NULL
;
165 if ( !(copy
= SMB_MALLOC_P( REGISTRY_VALUE
)) ) {
166 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
170 /* copy all the non-pointer initial data */
172 memcpy( copy
, val
, sizeof(REGISTRY_VALUE
) );
177 if ( val
->data_p
&& val
->size
)
179 if ( !(copy
->data_p
= memdup( val
->data_p
, val
->size
)) ) {
180 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
184 copy
->size
= val
->size
;
190 /**********************************************************************
191 free the memory allocated to a REGISTRY_VALUE
192 *********************************************************************/
194 void free_registry_value( REGISTRY_VALUE
*val
)
199 SAFE_FREE( val
->data_p
);
205 /**********************************************************************
206 *********************************************************************/
208 uint8
* regval_data_p( REGISTRY_VALUE
*val
)
213 /**********************************************************************
214 *********************************************************************/
216 uint32
regval_size( REGISTRY_VALUE
*val
)
221 /**********************************************************************
222 *********************************************************************/
224 char* regval_name( REGISTRY_VALUE
*val
)
226 return val
->valuename
;
229 /**********************************************************************
230 *********************************************************************/
232 uint32
regval_type( REGISTRY_VALUE
*val
)
237 /***********************************************************************
238 Retreive a pointer to a specific value. Caller shoud dup the structure
239 since this memory will go away when the ctr is free()'d
240 **********************************************************************/
242 REGISTRY_VALUE
* regval_ctr_specific_value( REGVAL_CTR
*ctr
, uint32 idx
)
244 if ( !(idx
< ctr
->num_values
) )
247 return ctr
->values
[idx
];
250 /***********************************************************************
251 Check for the existance of a value
252 **********************************************************************/
254 BOOL
regval_ctr_key_exists( REGVAL_CTR
*ctr
, const char *value
)
258 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
259 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
265 /***********************************************************************
266 Add a new registry value to the array
267 **********************************************************************/
269 int regval_ctr_addvalue( REGVAL_CTR
*ctr
, const char *name
, uint16 type
,
270 const char *data_p
, size_t size
)
272 REGISTRY_VALUE
**ppreg
;
275 return ctr
->num_values
;
277 /* Delete the current value (if it exists) and add the new one */
279 regval_ctr_delvalue( ctr
, name
);
281 /* allocate a slot in the array of pointers */
283 if ( ctr
->num_values
== 0 )
284 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
286 ppreg
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
291 /* allocate a new value and store the pointer in the arrya */
293 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
297 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, name
);
298 ctr
->values
[ctr
->num_values
]->type
= type
;
299 ctr
->values
[ctr
->num_values
]->data_p
= TALLOC_MEMDUP( ctr
, data_p
, size
);
300 ctr
->values
[ctr
->num_values
]->size
= size
;
303 return ctr
->num_values
;
306 /***********************************************************************
307 Add a new registry value to the array
308 **********************************************************************/
310 int regval_ctr_copyvalue( REGVAL_CTR
*ctr
, REGISTRY_VALUE
*val
)
312 REGISTRY_VALUE
**ppreg
;
316 /* allocate a slot in the array of pointers */
318 if ( ctr
->num_values
== 0 )
319 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
321 ppreg
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
326 /* allocate a new value and store the pointer in the arrya */
328 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
332 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, val
->valuename
);
333 ctr
->values
[ctr
->num_values
]->type
= val
->type
;
334 ctr
->values
[ctr
->num_values
]->data_p
= TALLOC_MEMDUP( ctr
, val
->data_p
, val
->size
);
335 ctr
->values
[ctr
->num_values
]->size
= val
->size
;
339 return ctr
->num_values
;
342 /***********************************************************************
343 Delete a single value from the registry container.
344 No need to free memory since it is talloc'd.
345 **********************************************************************/
347 int regval_ctr_delvalue( REGVAL_CTR
*ctr
, const char *name
)
351 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
352 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
356 /* just return if we don't find it */
358 if ( i
== ctr
->num_values
)
359 return ctr
->num_values
;
361 /* If 'i' was not the last element, just shift everything down one */
363 if ( i
< ctr
->num_values
)
364 memmove( &ctr
->values
[i
], &ctr
->values
[i
+1], sizeof(REGISTRY_VALUE
*)*(ctr
->num_values
-i
) );
366 return ctr
->num_values
;
369 /***********************************************************************
370 Retrieve single value from the registry container.
371 No need to free memory since it is talloc'd.
372 **********************************************************************/
374 REGISTRY_VALUE
* regval_ctr_getvalue( REGVAL_CTR
*ctr
, const char *name
)
378 /* search for the value */
380 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
381 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
382 return ctr
->values
[i
];