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
)
47 return ctr
->num_subkeys
;
49 /* make sure the keyname is not already there */
51 if ( regsubkey_ctr_key_exists( ctr
, keyname
) )
52 return ctr
->num_subkeys
;
54 /* allocate a space for the char* in the array */
56 if (ctr
->subkeys
== NULL
) {
57 ctr
->subkeys
= TALLOC_P(ctr
, char *);
59 ctr
->subkeys
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->subkeys
, char *, ctr
->num_subkeys
+1);
67 /* allocate the string and save it in the array */
69 ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup( ctr
, keyname
);
72 return ctr
->num_subkeys
;
75 /***********************************************************************
76 Add a new key to the array
77 **********************************************************************/
79 int regsubkey_ctr_delkey( REGSUBKEY_CTR
*ctr
, const char *keyname
)
84 return ctr
->num_subkeys
;
86 /* make sure the keyname is actually already there */
88 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
89 if ( strequal( ctr
->subkeys
[i
], keyname
) )
93 if ( i
== ctr
->num_subkeys
)
94 return ctr
->num_subkeys
;
96 /* update if we have any keys left */
98 if ( i
< ctr
->num_subkeys
)
99 memmove( &ctr
->subkeys
[i
], &ctr
->subkeys
[i
+1], sizeof(char*) * (ctr
->num_subkeys
-i
) );
101 return ctr
->num_subkeys
;
104 /***********************************************************************
105 Check for the existance of a key
106 **********************************************************************/
108 BOOL
regsubkey_ctr_key_exists( REGSUBKEY_CTR
*ctr
, const char *keyname
)
116 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
117 if ( strequal( ctr
->subkeys
[i
],keyname
) )
124 /***********************************************************************
125 How many keys does the container hold ?
126 **********************************************************************/
128 int regsubkey_ctr_numkeys( REGSUBKEY_CTR
*ctr
)
130 return ctr
->num_subkeys
;
133 /***********************************************************************
134 Retreive a specific key string
135 **********************************************************************/
137 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR
*ctr
, uint32 key_index
)
139 if ( ! (key_index
< ctr
->num_subkeys
) )
142 return ctr
->subkeys
[key_index
];
146 * Utility functions for REGVAL_CTR
149 /***********************************************************************
150 How many keys does the container hold ?
151 **********************************************************************/
153 int regval_ctr_numvals( REGVAL_CTR
*ctr
)
155 return ctr
->num_values
;
158 /***********************************************************************
159 allocate memory for and duplicate a REGISTRY_VALUE.
160 This is malloc'd memory so the caller should free it when done
161 **********************************************************************/
163 REGISTRY_VALUE
* dup_registry_value( REGISTRY_VALUE
*val
)
165 REGISTRY_VALUE
*copy
= NULL
;
170 if ( !(copy
= SMB_MALLOC_P( REGISTRY_VALUE
)) ) {
171 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
175 /* copy all the non-pointer initial data */
177 memcpy( copy
, val
, sizeof(REGISTRY_VALUE
) );
182 if ( val
->data_p
&& val
->size
)
184 if ( !(copy
->data_p
= memdup( val
->data_p
, val
->size
)) ) {
185 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
190 copy
->size
= val
->size
;
196 /**********************************************************************
197 free the memory allocated to a REGISTRY_VALUE
198 *********************************************************************/
200 void free_registry_value( REGISTRY_VALUE
*val
)
205 SAFE_FREE( val
->data_p
);
211 /**********************************************************************
212 *********************************************************************/
214 uint8
* regval_data_p( REGISTRY_VALUE
*val
)
219 /**********************************************************************
220 *********************************************************************/
222 uint32
regval_size( REGISTRY_VALUE
*val
)
227 /**********************************************************************
228 *********************************************************************/
230 char* regval_name( REGISTRY_VALUE
*val
)
232 return val
->valuename
;
235 /**********************************************************************
236 *********************************************************************/
238 uint32
regval_type( REGISTRY_VALUE
*val
)
243 /***********************************************************************
244 Retreive a pointer to a specific value. Caller shoud dup the structure
245 since this memory will go away when the ctr is free()'d
246 **********************************************************************/
248 REGISTRY_VALUE
* regval_ctr_specific_value( REGVAL_CTR
*ctr
, uint32 idx
)
250 if ( !(idx
< ctr
->num_values
) )
253 return ctr
->values
[idx
];
256 /***********************************************************************
257 Check for the existance of a value
258 **********************************************************************/
260 BOOL
regval_ctr_key_exists( REGVAL_CTR
*ctr
, const char *value
)
264 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
265 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
271 /***********************************************************************
272 Add a new registry value to the array
273 **********************************************************************/
275 int regval_ctr_addvalue( REGVAL_CTR
*ctr
, const char *name
, uint16 type
,
276 const char *data_p
, size_t size
)
279 return ctr
->num_values
;
281 /* Delete the current value (if it exists) and add the new one */
283 regval_ctr_delvalue( ctr
, name
);
285 /* allocate a slot in the array of pointers */
287 if ( ctr
->num_values
== 0 ) {
288 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
290 ctr
->values
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
298 /* allocate a new value and store the pointer in the arrya */
300 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
301 if (!ctr
->values
[ctr
->num_values
]) {
308 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, name
);
309 ctr
->values
[ctr
->num_values
]->type
= type
;
310 ctr
->values
[ctr
->num_values
]->data_p
= TALLOC_MEMDUP( ctr
, data_p
, size
);
311 ctr
->values
[ctr
->num_values
]->size
= size
;
314 return ctr
->num_values
;
317 /***********************************************************************
318 Add a new registry value to the array
319 **********************************************************************/
321 int regval_ctr_copyvalue( REGVAL_CTR
*ctr
, REGISTRY_VALUE
*val
)
324 /* allocate a slot in the array of pointers */
326 if ( ctr
->num_values
== 0 ) {
327 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
329 ctr
->values
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
337 /* allocate a new value and store the pointer in the arrya */
339 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
340 if (!ctr
->values
[ctr
->num_values
]) {
347 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, val
->valuename
);
348 ctr
->values
[ctr
->num_values
]->type
= val
->type
;
349 ctr
->values
[ctr
->num_values
]->data_p
= TALLOC_MEMDUP( ctr
, val
->data_p
, val
->size
);
350 ctr
->values
[ctr
->num_values
]->size
= val
->size
;
354 return ctr
->num_values
;
357 /***********************************************************************
358 Delete a single value from the registry container.
359 No need to free memory since it is talloc'd.
360 **********************************************************************/
362 int regval_ctr_delvalue( REGVAL_CTR
*ctr
, const char *name
)
366 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
367 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
371 /* just return if we don't find it */
373 if ( i
== ctr
->num_values
)
374 return ctr
->num_values
;
376 /* If 'i' was not the last element, just shift everything down one */
378 if ( i
< ctr
->num_values
)
379 memmove( &ctr
->values
[i
], &ctr
->values
[i
+1], sizeof(REGISTRY_VALUE
*)*(ctr
->num_values
-i
) );
381 return ctr
->num_values
;
384 /***********************************************************************
385 Retrieve single value from the registry container.
386 No need to free memory since it is talloc'd.
387 **********************************************************************/
389 REGISTRY_VALUE
* regval_ctr_getvalue( REGVAL_CTR
*ctr
, const char *name
)
393 /* search for the value */
395 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
396 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
397 return ctr
->values
[i
];
403 /***********************************************************************
404 return the data_p as a uint32
405 **********************************************************************/
407 uint32
regval_dword( REGISTRY_VALUE
*val
)
411 data
= IVAL( regval_data_p(val
), 0 );
416 /***********************************************************************
417 return the data_p as a character string
418 **********************************************************************/
420 char* regval_sz( REGISTRY_VALUE
*val
)
424 rpcstr_pull( data
, regval_data_p(val
), sizeof(data
), regval_size(val
), 0 );