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
= (uint8
*)memdup( val
->data_p
,
186 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
191 copy
->size
= val
->size
;
197 /**********************************************************************
198 free the memory allocated to a REGISTRY_VALUE
199 *********************************************************************/
201 void free_registry_value( REGISTRY_VALUE
*val
)
206 SAFE_FREE( val
->data_p
);
212 /**********************************************************************
213 *********************************************************************/
215 uint8
* regval_data_p( REGISTRY_VALUE
*val
)
220 /**********************************************************************
221 *********************************************************************/
223 uint32
regval_size( REGISTRY_VALUE
*val
)
228 /**********************************************************************
229 *********************************************************************/
231 char* regval_name( REGISTRY_VALUE
*val
)
233 return val
->valuename
;
236 /**********************************************************************
237 *********************************************************************/
239 uint32
regval_type( REGISTRY_VALUE
*val
)
244 /***********************************************************************
245 Retreive a pointer to a specific value. Caller shoud dup the structure
246 since this memory will go away when the ctr is free()'d
247 **********************************************************************/
249 REGISTRY_VALUE
* regval_ctr_specific_value( REGVAL_CTR
*ctr
, uint32 idx
)
251 if ( !(idx
< ctr
->num_values
) )
254 return ctr
->values
[idx
];
257 /***********************************************************************
258 Check for the existance of a value
259 **********************************************************************/
261 BOOL
regval_ctr_key_exists( REGVAL_CTR
*ctr
, const char *value
)
265 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
266 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
272 /***********************************************************************
273 Add a new registry value to the array
274 **********************************************************************/
276 int regval_ctr_addvalue( REGVAL_CTR
*ctr
, const char *name
, uint16 type
,
277 const char *data_p
, size_t size
)
280 return ctr
->num_values
;
282 /* Delete the current value (if it exists) and add the new one */
284 regval_ctr_delvalue( ctr
, name
);
286 /* allocate a slot in the array of pointers */
288 if ( ctr
->num_values
== 0 ) {
289 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
291 ctr
->values
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
299 /* allocate a new value and store the pointer in the arrya */
301 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
302 if (!ctr
->values
[ctr
->num_values
]) {
309 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, name
);
310 ctr
->values
[ctr
->num_values
]->type
= type
;
312 ctr
->values
[ctr
->num_values
]->data_p
= (uint8
*)TALLOC_MEMDUP(
314 if (!ctr
->values
[ctr
->num_values
]->data_p
) {
319 ctr
->values
[ctr
->num_values
]->data_p
= NULL
;
321 ctr
->values
[ctr
->num_values
]->size
= size
;
324 return ctr
->num_values
;
327 /***********************************************************************
328 Add a new registry value to the array
329 **********************************************************************/
331 int regval_ctr_copyvalue( REGVAL_CTR
*ctr
, REGISTRY_VALUE
*val
)
334 /* allocate a slot in the array of pointers */
336 if ( ctr
->num_values
== 0 ) {
337 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
339 ctr
->values
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
347 /* allocate a new value and store the pointer in the arrya */
349 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
350 if (!ctr
->values
[ctr
->num_values
]) {
357 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, val
->valuename
);
358 ctr
->values
[ctr
->num_values
]->type
= val
->type
;
360 ctr
->values
[ctr
->num_values
]->data_p
= (uint8
*)TALLOC_MEMDUP(
361 ctr
, val
->data_p
, val
->size
);
362 if (!ctr
->values
[ctr
->num_values
]->data_p
) {
367 ctr
->values
[ctr
->num_values
]->data_p
= NULL
;
369 ctr
->values
[ctr
->num_values
]->size
= val
->size
;
373 return ctr
->num_values
;
376 /***********************************************************************
377 Delete a single value from the registry container.
378 No need to free memory since it is talloc'd.
379 **********************************************************************/
381 int regval_ctr_delvalue( REGVAL_CTR
*ctr
, const char *name
)
385 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
386 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
390 /* just return if we don't find it */
392 if ( i
== ctr
->num_values
)
393 return ctr
->num_values
;
395 /* If 'i' was not the last element, just shift everything down one */
397 if ( i
< ctr
->num_values
)
398 memmove( &ctr
->values
[i
], &ctr
->values
[i
+1], sizeof(REGISTRY_VALUE
*)*(ctr
->num_values
-i
) );
400 return ctr
->num_values
;
403 /***********************************************************************
404 Retrieve single value from the registry container.
405 No need to free memory since it is talloc'd.
406 **********************************************************************/
408 REGISTRY_VALUE
* regval_ctr_getvalue( REGVAL_CTR
*ctr
, const char *name
)
412 /* search for the value */
414 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
415 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
416 return ctr
->values
[i
];
422 /***********************************************************************
423 return the data_p as a uint32
424 **********************************************************************/
426 uint32
regval_dword( REGISTRY_VALUE
*val
)
430 data
= IVAL( regval_data_p(val
), 0 );
435 /***********************************************************************
436 return the data_p as a character string
437 **********************************************************************/
439 char* regval_sz( REGISTRY_VALUE
*val
)
443 rpcstr_pull( data
, regval_data_p(val
), sizeof(data
), regval_size(val
), 0 );