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
)
112 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
113 if ( strequal( ctr
->subkeys
[i
],keyname
) )
120 /***********************************************************************
121 How many keys does the container hold ?
122 **********************************************************************/
124 int regsubkey_ctr_numkeys( REGSUBKEY_CTR
*ctr
)
126 return ctr
->num_subkeys
;
129 /***********************************************************************
130 Retreive a specific key string
131 **********************************************************************/
133 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR
*ctr
, uint32 key_index
)
135 if ( ! (key_index
< ctr
->num_subkeys
) )
138 return ctr
->subkeys
[key_index
];
142 * Utility functions for REGVAL_CTR
145 /***********************************************************************
146 How many keys does the container hold ?
147 **********************************************************************/
149 int regval_ctr_numvals( REGVAL_CTR
*ctr
)
151 return ctr
->num_values
;
154 /***********************************************************************
155 allocate memory for and duplicate a REGISTRY_VALUE.
156 This is malloc'd memory so the caller should free it when done
157 **********************************************************************/
159 REGISTRY_VALUE
* dup_registry_value( REGISTRY_VALUE
*val
)
161 REGISTRY_VALUE
*copy
= NULL
;
166 if ( !(copy
= SMB_MALLOC_P( REGISTRY_VALUE
)) ) {
167 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
171 /* copy all the non-pointer initial data */
173 memcpy( copy
, val
, sizeof(REGISTRY_VALUE
) );
178 if ( val
->data_p
&& val
->size
)
180 if ( !(copy
->data_p
= (uint8
*)memdup( val
->data_p
,
182 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
186 copy
->size
= val
->size
;
192 /**********************************************************************
193 free the memory allocated to a REGISTRY_VALUE
194 *********************************************************************/
196 void free_registry_value( REGISTRY_VALUE
*val
)
201 SAFE_FREE( val
->data_p
);
207 /**********************************************************************
208 *********************************************************************/
210 uint8
* regval_data_p( REGISTRY_VALUE
*val
)
215 /**********************************************************************
216 *********************************************************************/
218 uint32
regval_size( REGISTRY_VALUE
*val
)
223 /**********************************************************************
224 *********************************************************************/
226 char* regval_name( REGISTRY_VALUE
*val
)
228 return val
->valuename
;
231 /**********************************************************************
232 *********************************************************************/
234 uint32
regval_type( REGISTRY_VALUE
*val
)
239 /***********************************************************************
240 Retreive a pointer to a specific value. Caller shoud dup the structure
241 since this memory will go away when the ctr is free()'d
242 **********************************************************************/
244 REGISTRY_VALUE
* regval_ctr_specific_value( REGVAL_CTR
*ctr
, uint32 idx
)
246 if ( !(idx
< ctr
->num_values
) )
249 return ctr
->values
[idx
];
252 /***********************************************************************
253 Check for the existance of a value
254 **********************************************************************/
256 BOOL
regval_ctr_key_exists( REGVAL_CTR
*ctr
, const char *value
)
260 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
261 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
267 /***********************************************************************
268 Add a new registry value to the array
269 **********************************************************************/
271 int regval_ctr_addvalue( REGVAL_CTR
*ctr
, const char *name
, uint16 type
,
272 const char *data_p
, size_t size
)
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 ctr
->values
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
294 /* allocate a new value and store the pointer in the arrya */
296 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
297 if (!ctr
->values
[ctr
->num_values
]) {
304 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, name
);
305 ctr
->values
[ctr
->num_values
]->type
= type
;
306 ctr
->values
[ctr
->num_values
]->data_p
= (uint8
*)TALLOC_MEMDUP(
308 ctr
->values
[ctr
->num_values
]->size
= size
;
311 return ctr
->num_values
;
314 /***********************************************************************
315 Add a new registry value to the array
316 **********************************************************************/
318 int regval_ctr_copyvalue( REGVAL_CTR
*ctr
, REGISTRY_VALUE
*val
)
321 /* allocate a slot in the array of pointers */
323 if ( ctr
->num_values
== 0 ) {
324 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
326 ctr
->values
= TALLOC_REALLOC_ARRAY( ctr
, ctr
->values
, REGISTRY_VALUE
*, ctr
->num_values
+1 );
334 /* allocate a new value and store the pointer in the arrya */
336 ctr
->values
[ctr
->num_values
] = TALLOC_P( ctr
, REGISTRY_VALUE
);
337 if (!ctr
->values
[ctr
->num_values
]) {
344 fstrcpy( ctr
->values
[ctr
->num_values
]->valuename
, val
->valuename
);
345 ctr
->values
[ctr
->num_values
]->type
= val
->type
;
346 ctr
->values
[ctr
->num_values
]->data_p
= (uint8
*)TALLOC_MEMDUP(
347 ctr
, val
->data_p
, val
->size
);
348 ctr
->values
[ctr
->num_values
]->size
= val
->size
;
352 return ctr
->num_values
;
355 /***********************************************************************
356 Delete a single value from the registry container.
357 No need to free memory since it is talloc'd.
358 **********************************************************************/
360 int regval_ctr_delvalue( REGVAL_CTR
*ctr
, const char *name
)
364 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
365 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
369 /* just return if we don't find it */
371 if ( i
== ctr
->num_values
)
372 return ctr
->num_values
;
374 /* If 'i' was not the last element, just shift everything down one */
376 if ( i
< ctr
->num_values
)
377 memmove( &ctr
->values
[i
], &ctr
->values
[i
+1], sizeof(REGISTRY_VALUE
*)*(ctr
->num_values
-i
) );
379 return ctr
->num_values
;
382 /***********************************************************************
383 Retrieve single value from the registry container.
384 No need to free memory since it is talloc'd.
385 **********************************************************************/
387 REGISTRY_VALUE
* regval_ctr_getvalue( REGVAL_CTR
*ctr
, const char *name
)
391 /* search for the value */
393 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
394 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
395 return ctr
->values
[i
];
401 /***********************************************************************
402 return the data_p as a uint32
403 **********************************************************************/
405 uint32
regval_dword( REGISTRY_VALUE
*val
)
409 data
= IVAL( regval_data_p(val
), 0 );
414 /***********************************************************************
415 return the data_p as a character string
416 **********************************************************************/
418 char* regval_sz( REGISTRY_VALUE
*val
)
422 rpcstr_pull( data
, regval_data_p(val
), sizeof(data
), regval_size(val
), 0 );