1/* 2 * Unix SMB/CIFS implementation. 3 * Virtual Windows Registry Layer 4 * Copyright (C) Gerald Carter 2002-2005 5 * 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. 10 * 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. 15 * 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. 19 */ 20 21/* Implementation of registry frontend view functions. */ 22 23#include "includes.h" 24 25#undef DBGC_CLASS 26#define DBGC_CLASS DBGC_RPC_SRV 27 28/********************************************************************** 29 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. 33 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 36 object. 37 38 **********************************************************************/ 39 40/*********************************************************************** 41 Add a new key to the array 42 **********************************************************************/ 43 44int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname ) 45{ 46 if ( !keyname ) 47 return ctr->num_subkeys; 48 49 /* make sure the keyname is not already there */ 50 51 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) 52 return ctr->num_subkeys; 53 54 /* allocate a space for the char* in the array */ 55 56 if (ctr->subkeys == NULL) { 57 ctr->subkeys = TALLOC_P(ctr, char *); 58 } else { 59 ctr->subkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *, ctr->num_subkeys+1); 60 } 61 62 if (!ctr->subkeys) { 63 ctr->num_subkeys = 0; 64 return 0; 65 } 66 67 /* allocate the string and save it in the array */ 68 69 ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr, keyname ); 70 ctr->num_subkeys++; 71 72 return ctr->num_subkeys; 73} 74 75 /*********************************************************************** 76 Add a new key to the array 77 **********************************************************************/ 78 79int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname ) 80{ 81 int i; 82 83 if ( !keyname ) 84 return ctr->num_subkeys; 85 86 /* make sure the keyname is actually already there */ 87 88 for ( i=0; i<ctr->num_subkeys; i++ ) { 89 if ( strequal( ctr->subkeys[i], keyname ) ) 90 break; 91 } 92 93 if ( i == ctr->num_subkeys ) 94 return ctr->num_subkeys; 95 96 /* update if we have any keys left */ 97 ctr->num_subkeys--; 98 if ( i < ctr->num_subkeys ) 99 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) ); 100 101 return ctr->num_subkeys; 102} 103 104/*********************************************************************** 105 Check for the existance of a key 106 **********************************************************************/ 107 108BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname ) 109{ 110 int i; 111 112 if (!ctr->subkeys) { 113 return False; 114 } 115 116 for ( i=0; i<ctr->num_subkeys; i++ ) { 117 if ( strequal( ctr->subkeys[i],keyname ) ) 118 return True; 119 } 120 121 return False; 122} 123 124/*********************************************************************** 125 How many keys does the container hold ? 126 **********************************************************************/ 127 128int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr ) 129{ 130 return ctr->num_subkeys; 131} 132 133/*********************************************************************** 134 Retreive a specific key string 135 **********************************************************************/ 136 137char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index ) 138{ 139 if ( ! (key_index < ctr->num_subkeys) ) 140 return NULL; 141 142 return ctr->subkeys[key_index]; 143} 144 145/* 146 * Utility functions for REGVAL_CTR 147 */ 148 149/*********************************************************************** 150 How many keys does the container hold ? 151 **********************************************************************/ 152 153int regval_ctr_numvals( REGVAL_CTR *ctr ) 154{ 155 return ctr->num_values; 156} 157 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 **********************************************************************/ 162 163REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val ) 164{ 165 REGISTRY_VALUE *copy = NULL; 166 167 if ( !val ) 168 return NULL; 169 170 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) { 171 DEBUG(0,("dup_registry_value: malloc() failed!\n")); 172 return NULL; 173 } 174 175 /* copy all the non-pointer initial data */ 176 177 memcpy( copy, val, sizeof(REGISTRY_VALUE) ); 178 179 copy->size = 0; 180 copy->data_p = NULL; 181 182 if ( val->data_p && val->size ) 183 { 184 if ( !(copy->data_p = (uint8 *)memdup( val->data_p, 185 val->size )) ) { 186 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n", 187 val->size)); 188 SAFE_FREE( copy ); 189 return NULL; 190 } 191 copy->size = val->size; 192 } 193 194 return copy; 195} 196 197/********************************************************************** 198 free the memory allocated to a REGISTRY_VALUE 199 *********************************************************************/ 200 201void free_registry_value( REGISTRY_VALUE *val ) 202{ 203 if ( !val ) 204 return; 205 206 SAFE_FREE( val->data_p ); 207 SAFE_FREE( val ); 208 209 return; 210} 211 212/********************************************************************** 213 *********************************************************************/ 214 215uint8* regval_data_p( REGISTRY_VALUE *val ) 216{ 217 return val->data_p; 218} 219 220/********************************************************************** 221 *********************************************************************/ 222 223uint32 regval_size( REGISTRY_VALUE *val ) 224{ 225 return val->size; 226} 227 228/********************************************************************** 229 *********************************************************************/ 230 231char* regval_name( REGISTRY_VALUE *val ) 232{ 233 return val->valuename; 234} 235 236/********************************************************************** 237 *********************************************************************/ 238 239uint32 regval_type( REGISTRY_VALUE *val ) 240{ 241 return val->type; 242} 243 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 **********************************************************************/ 248 249REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx ) 250{ 251 if ( !(idx < ctr->num_values) ) 252 return NULL; 253 254 return ctr->values[idx]; 255} 256 257/*********************************************************************** 258 Check for the existance of a value 259 **********************************************************************/ 260 261BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value ) 262{ 263 int i; 264 265 for ( i=0; i<ctr->num_values; i++ ) { 266 if ( strequal( ctr->values[i]->valuename, value) ) 267 return True; 268 } 269 270 return False; 271} 272/*********************************************************************** 273 Add a new registry value to the array 274 **********************************************************************/ 275 276int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type, 277 const char *data_p, size_t size ) 278{ 279 if ( !name ) 280 return ctr->num_values; 281 282 /* Delete the current value (if it exists) and add the new one */ 283 284 regval_ctr_delvalue( ctr, name ); 285 286 /* allocate a slot in the array of pointers */ 287 288 if ( ctr->num_values == 0 ) { 289 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *); 290 } else { 291 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); 292 } 293 294 if (!ctr->values) { 295 ctr->num_values = 0; 296 return 0; 297 } 298 299 /* allocate a new value and store the pointer in the arrya */ 300 301 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE); 302 if (!ctr->values[ctr->num_values]) { 303 ctr->num_values = 0; 304 return 0; 305 } 306 307 /* init the value */ 308 309 fstrcpy( ctr->values[ctr->num_values]->valuename, name ); 310 ctr->values[ctr->num_values]->type = type; 311 if (size) { 312 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP( 313 ctr, data_p, size ); 314 if (!ctr->values[ctr->num_values]->data_p) { 315 ctr->num_values = 0; 316 return 0; 317 } 318 } else { 319 ctr->values[ctr->num_values]->data_p = NULL; 320 } 321 ctr->values[ctr->num_values]->size = size; 322 ctr->num_values++; 323 324 return ctr->num_values; 325} 326 327/*********************************************************************** 328 Add a new registry value to the array 329 **********************************************************************/ 330 331int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val ) 332{ 333 if ( val ) { 334 /* allocate a slot in the array of pointers */ 335 336 if ( ctr->num_values == 0 ) { 337 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *); 338 } else { 339 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); 340 } 341 342 if (!ctr->values) { 343 ctr->num_values = 0; 344 return 0; 345 } 346 347 /* allocate a new value and store the pointer in the arrya */ 348 349 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE); 350 if (!ctr->values[ctr->num_values]) { 351 ctr->num_values = 0; 352 return 0; 353 } 354 355 /* init the value */ 356 357 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename ); 358 ctr->values[ctr->num_values]->type = val->type; 359 if (val->size) { 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) { 363 ctr->num_values = 0; 364 return 0; 365 } 366 } else { 367 ctr->values[ctr->num_values]->data_p = NULL; 368 } 369 ctr->values[ctr->num_values]->size = val->size; 370 ctr->num_values++; 371 } 372 373 return ctr->num_values; 374} 375 376/*********************************************************************** 377 Delete a single value from the registry container. 378 No need to free memory since it is talloc'd. 379 **********************************************************************/ 380 381int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name ) 382{ 383 int i; 384 385 for ( i=0; i<ctr->num_values; i++ ) { 386 if ( strequal( ctr->values[i]->valuename, name ) ) 387 break; 388 } 389 390 /* just return if we don't find it */ 391 392 if ( i == ctr->num_values ) 393 return ctr->num_values; 394 395 /* If 'i' was not the last element, just shift everything down one */ 396 ctr->num_values--; 397 if ( i < ctr->num_values ) 398 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) ); 399 400 return ctr->num_values; 401} 402 403/*********************************************************************** 404 Retrieve single value from the registry container. 405 No need to free memory since it is talloc'd. 406 **********************************************************************/ 407 408REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name ) 409{ 410 int i; 411 412 /* search for the value */ 413 414 for ( i=0; i<ctr->num_values; i++ ) { 415 if ( strequal( ctr->values[i]->valuename, name ) ) 416 return ctr->values[i]; 417 } 418 419 return NULL; 420} 421 422/*********************************************************************** 423 return the data_p as a uint32 424 **********************************************************************/ 425 426uint32 regval_dword( REGISTRY_VALUE *val ) 427{ 428 uint32 data; 429 430 data = IVAL( regval_data_p(val), 0 ); 431 432 return data; 433} 434 435/*********************************************************************** 436 return the data_p as a character string 437 **********************************************************************/ 438 439char* regval_sz( REGISTRY_VALUE *val ) 440{ 441 static pstring data; 442 443 rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 ); 444 445 return data; 446} 447