2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Gerald Carter 2002.
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
29 /***********************************************************************
30 Init the talloc context held by a REGSUBKEY_CTR structure
31 **********************************************************************/
33 void regsubkey_ctr_init( REGSUBKEY_CTR
*ctr
)
36 ctr
->ctx
= talloc_init("regsubkey_ctr_init for ctr %p", ctr
);
39 /***********************************************************************
40 Add a new key to the array
41 **********************************************************************/
43 int regsubkey_ctr_addkey( REGSUBKEY_CTR
*ctr
, const char *keyname
)
50 len
= strlen( keyname
);
52 /* allocate a space for the char* in the array */
54 if ( ctr
->subkeys
== 0 )
55 ctr
->subkeys
= talloc( ctr
->ctx
, sizeof(char*) );
57 pp
= talloc_realloc( ctr
->ctx
, ctr
->subkeys
, sizeof(char*)*(ctr
->num_subkeys
+1) );
62 /* allocate the string and save it in the array */
64 ctr
->subkeys
[ctr
->num_subkeys
] = talloc( ctr
->ctx
, len
+1 );
65 strncpy( ctr
->subkeys
[ctr
->num_subkeys
], keyname
, len
+1 );
69 return ctr
->num_subkeys
;
72 /***********************************************************************
73 How many keys does the container hold ?
74 **********************************************************************/
76 int regsubkey_ctr_numkeys( REGSUBKEY_CTR
*ctr
)
78 return ctr
->num_subkeys
;
81 /***********************************************************************
82 Retreive a specific key string
83 **********************************************************************/
85 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR
*ctr
, uint32 key_index
)
87 if ( ! (key_index
< ctr
->num_subkeys
) )
90 return ctr
->subkeys
[key_index
];
93 /***********************************************************************
94 free memory held by a REGSUBKEY_CTR structure
95 **********************************************************************/
97 void regsubkey_ctr_destroy( REGSUBKEY_CTR
*ctr
)
100 talloc_destroy( ctr
->ctx
);
107 * Utility functions for REGVAL_CTR
110 /***********************************************************************
111 Init the talloc context held by a REGSUBKEY_CTR structure
112 **********************************************************************/
114 void regval_ctr_init( REGVAL_CTR
*ctr
)
117 ctr
->ctx
= talloc_init("regval_ctr_init for ctr %p", ctr
);
120 /***********************************************************************
121 How many keys does the container hold ?
122 **********************************************************************/
124 int regval_ctr_numvals( REGVAL_CTR
*ctr
)
126 return ctr
->num_values
;
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 **********************************************************************/
134 REGISTRY_VALUE
* dup_registry_value( REGISTRY_VALUE
*val
)
136 REGISTRY_VALUE
*copy
= NULL
;
141 if ( !(copy
= malloc( sizeof(REGISTRY_VALUE
) )) ) {
142 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
146 /* copy all the non-pointer initial data */
148 memcpy( copy
, val
, sizeof(REGISTRY_VALUE
) );
151 if ( !(copy
->data_p
= memdup( val
->data_p
, val
->size
)) ) {
152 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
161 /**********************************************************************
162 free the memory allocated to a REGISTRY_VALUE
163 *********************************************************************/
165 void free_registry_value( REGISTRY_VALUE
*val
)
170 SAFE_FREE( val
->data_p
);
176 /**********************************************************************
177 *********************************************************************/
179 uint8
* regval_data_p( REGISTRY_VALUE
*val
)
184 /**********************************************************************
185 *********************************************************************/
187 int regval_size( REGISTRY_VALUE
*val
)
192 /**********************************************************************
193 *********************************************************************/
195 char* regval_name( REGISTRY_VALUE
*val
)
197 return val
->valuename
;
200 /**********************************************************************
201 *********************************************************************/
203 uint32
regval_type( REGISTRY_VALUE
*val
)
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 **********************************************************************/
213 REGISTRY_VALUE
* regval_ctr_specific_value( REGVAL_CTR
*ctr
, uint32 idx
)
215 if ( !(idx
< ctr
->num_values
) )
218 return ctr
->values
[idx
];
221 /***********************************************************************
222 Retrive the TALLOC_CTX associated with a REGISTRY_VALUE
223 **********************************************************************/
225 TALLOC_CTX
* regval_ctr_getctx( REGVAL_CTR
*val
)
233 /***********************************************************************
234 Add a new registry value to the array
235 **********************************************************************/
237 int regval_ctr_addvalue( REGVAL_CTR
*ctr
, const char *name
, uint16 type
,
238 const char *data_p
, size_t size
)
240 REGISTRY_VALUE
**ppreg
;
244 /* allocate a slot in the array of pointers */
246 if ( ctr
->num_values
== 0 )
247 ctr
->values
= talloc( ctr
->ctx
, sizeof(REGISTRY_VALUE
*) );
249 ppreg
= talloc_realloc( ctr
->ctx
, ctr
->values
, sizeof(REGISTRY_VALUE
*)*(ctr
->num_values
+1) );
254 /* allocate a new value and store the pointer in the arrya */
256 ctr
->values
[ctr
->num_values
] = talloc( ctr
->ctx
, sizeof(REGISTRY_VALUE
) );
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
;
267 return ctr
->num_values
;
270 /***********************************************************************
271 Add a new registry value to the array
272 **********************************************************************/
274 int regval_ctr_copyvalue( REGVAL_CTR
*ctr
, REGISTRY_VALUE
*val
)
276 REGISTRY_VALUE
**ppreg
;
280 /* allocate a slot in the array of pointers */
282 if ( ctr
->num_values
== 0 )
283 ctr
->values
= talloc( ctr
->ctx
, sizeof(REGISTRY_VALUE
*) );
285 ppreg
= talloc_realloc( ctr
->ctx
, ctr
->values
, sizeof(REGISTRY_VALUE
*)*(ctr
->num_values
+1) );
290 /* allocate a new value and store the pointer in the arrya */
292 ctr
->values
[ctr
->num_values
] = talloc( ctr
->ctx
, sizeof(REGISTRY_VALUE
) );
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
;
303 return ctr
->num_values
;
306 /***********************************************************************
307 Delete a single value from the registry container.
308 No need to free memory since it is talloc'd.
309 **********************************************************************/
311 int regval_ctr_delvalue( REGVAL_CTR
*ctr
, const char *name
)
315 /* search for the value */
316 if (!(ctr
->num_values
))
319 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
320 if ( strcmp( ctr
->values
[i
]->valuename
, name
) == 0)
324 /* just return if we don't find it */
326 if ( i
== ctr
->num_values
)
327 return ctr
->num_values
;
329 /* just shift everything down one */
331 for ( /* use previous i */; i
<(ctr
->num_values
-1); i
++ )
332 memcpy( ctr
->values
[i
], ctr
->values
[i
+1], sizeof(REGISTRY_VALUE
) );
336 ZERO_STRUCTP( ctr
->values
[i
] );
340 return ctr
->num_values
;
343 /***********************************************************************
344 Delete a single value from the registry container.
345 No need to free memory since it is talloc'd.
346 **********************************************************************/
348 REGISTRY_VALUE
* regval_ctr_getvalue( REGVAL_CTR
*ctr
, const char *name
)
352 /* search for the value */
354 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
355 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
356 return ctr
->values
[i
];
362 /***********************************************************************
363 free memory held by a REGVAL_CTR structure
364 **********************************************************************/
366 void regval_ctr_destroy( REGVAL_CTR
*ctr
)
369 talloc_destroy( ctr
->ctx
);