registry/reg_objects.c

説明を見る。
00001 /* 
00002  *  Unix SMB/CIFS implementation.
00003  *  Virtual Windows Registry Layer
00004  *  Copyright (C) Gerald Carter                     2002-2005
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *  
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *  
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 /* Implementation of registry frontend view functions. */
00022 
00023 #include "includes.h"
00024 
00025 #undef DBGC_CLASS
00026 #define DBGC_CLASS DBGC_RPC_SRV
00027 
00028 /**********************************************************************
00029 
00030  Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d
00031  since the methods use the object pointer as the talloc context for 
00032  internal private data.
00033 
00034  There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy()
00035  pair of functions.  Simply TALLOC_ZERO_P() and TALLOC_FREE() the 
00036  object.
00037 
00038  **********************************************************************/
00039 
00040 /***********************************************************************
00041  Add a new key to the array
00042  **********************************************************************/
00043 
00044 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
00045 {
00046         if ( !keyname )
00047                 return ctr->num_subkeys;
00048 
00049         /* make sure the keyname is not already there */
00050 
00051         if ( regsubkey_ctr_key_exists( ctr, keyname ) )
00052                 return ctr->num_subkeys;
00053                 
00054         /* allocate a space for the char* in the array */
00055                 
00056         if (ctr->subkeys == NULL) {
00057                 ctr->subkeys = TALLOC_P(ctr, char *);
00058         } else {
00059                 ctr->subkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *, ctr->num_subkeys+1);
00060         }
00061 
00062         if (!ctr->subkeys) {
00063                 ctr->num_subkeys = 0;
00064                 return 0;
00065         }
00066 
00067         /* allocate the string and save it in the array */
00068         
00069         ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr, keyname );
00070         ctr->num_subkeys++;
00071         
00072         return ctr->num_subkeys;
00073 }
00074  
00075  /***********************************************************************
00076  Add a new key to the array
00077  **********************************************************************/
00078 
00079 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
00080 {
00081         int i;
00082 
00083         if ( !keyname )
00084                 return ctr->num_subkeys;
00085 
00086         /* make sure the keyname is actually already there */
00087 
00088         for ( i=0; i<ctr->num_subkeys; i++ ) {
00089                 if ( strequal( ctr->subkeys[i], keyname ) )
00090                         break;
00091         }
00092         
00093         if ( i == ctr->num_subkeys )
00094                 return ctr->num_subkeys;
00095 
00096         /* update if we have any keys left */
00097         ctr->num_subkeys--;
00098         if ( i < ctr->num_subkeys )
00099                 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
00100         
00101         return ctr->num_subkeys;
00102 }
00103 
00104 /***********************************************************************
00105  Check for the existance of a key
00106  **********************************************************************/
00107 
00108 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
00109 {
00110         int     i;
00111         
00112         if (!ctr->subkeys) {
00113                 return False;
00114         }
00115 
00116         for ( i=0; i<ctr->num_subkeys; i++ ) {
00117                 if ( strequal( ctr->subkeys[i],keyname ) )
00118                         return True;
00119         }
00120         
00121         return False;
00122 }
00123 
00124 /***********************************************************************
00125  How many keys does the container hold ?
00126  **********************************************************************/
00127 
00128 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
00129 {
00130         return ctr->num_subkeys;
00131 }
00132 
00133 /***********************************************************************
00134  Retreive a specific key string
00135  **********************************************************************/
00136 
00137 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
00138 {
00139         if ( ! (key_index < ctr->num_subkeys) )
00140                 return NULL;
00141                 
00142         return ctr->subkeys[key_index];
00143 }
00144 
00145 /*
00146  * Utility functions for REGVAL_CTR
00147  */
00148 
00149 /***********************************************************************
00150  How many keys does the container hold ?
00151  **********************************************************************/
00152 
00153 int regval_ctr_numvals( REGVAL_CTR *ctr )
00154 {
00155         return ctr->num_values;
00156 }
00157 
00158 /***********************************************************************
00159  allocate memory for and duplicate a REGISTRY_VALUE.
00160  This is malloc'd memory so the caller should free it when done
00161  **********************************************************************/
00162 
00163 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
00164 {
00165         REGISTRY_VALUE  *copy = NULL;
00166         
00167         if ( !val )
00168                 return NULL;
00169         
00170         if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
00171                 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
00172                 return NULL;
00173         }
00174         
00175         /* copy all the non-pointer initial data */
00176         
00177         memcpy( copy, val, sizeof(REGISTRY_VALUE) );
00178         
00179         copy->size = 0;
00180         copy->data_p = NULL;
00181         
00182         if ( val->data_p && val->size ) 
00183         {
00184                 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
00185                                                        val->size )) ) {
00186                         DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
00187                                 val->size));
00188                         SAFE_FREE( copy );
00189                         return NULL;
00190                 }
00191                 copy->size = val->size;
00192         }
00193         
00194         return copy;    
00195 }
00196 
00197 /**********************************************************************
00198  free the memory allocated to a REGISTRY_VALUE 
00199  *********************************************************************/
00200  
00201 void free_registry_value( REGISTRY_VALUE *val )
00202 {
00203         if ( !val )
00204                 return;
00205                 
00206         SAFE_FREE( val->data_p );
00207         SAFE_FREE( val );
00208         
00209         return;
00210 }
00211 
00212 /**********************************************************************
00213  *********************************************************************/
00214 
00215 uint8* regval_data_p( REGISTRY_VALUE *val )
00216 {
00217         return val->data_p;
00218 }
00219 
00220 /**********************************************************************
00221  *********************************************************************/
00222 
00223 uint32 regval_size( REGISTRY_VALUE *val )
00224 {
00225         return val->size;
00226 }
00227 
00228 /**********************************************************************
00229  *********************************************************************/
00230 
00231 char* regval_name( REGISTRY_VALUE *val )
00232 {
00233         return val->valuename;
00234 }
00235 
00236 /**********************************************************************
00237  *********************************************************************/
00238 
00239 uint32 regval_type( REGISTRY_VALUE *val )
00240 {
00241         return val->type;
00242 }
00243 
00244 /***********************************************************************
00245  Retreive a pointer to a specific value.  Caller shoud dup the structure
00246  since this memory will go away when the ctr is free()'d
00247  **********************************************************************/
00248 
00249 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
00250 {
00251         if ( !(idx < ctr->num_values) )
00252                 return NULL;
00253                 
00254         return ctr->values[idx];
00255 }
00256 
00257 /***********************************************************************
00258  Check for the existance of a value
00259  **********************************************************************/
00260 
00261 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
00262 {
00263         int     i;
00264         
00265         for ( i=0; i<ctr->num_values; i++ ) {
00266                 if ( strequal( ctr->values[i]->valuename, value) )
00267                         return True;
00268         }
00269         
00270         return False;
00271 }
00272 /***********************************************************************
00273  Add a new registry value to the array
00274  **********************************************************************/
00275 
00276 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type, 
00277                          const char *data_p, size_t size )
00278 {
00279         if ( !name )
00280                 return ctr->num_values;
00281 
00282         /* Delete the current value (if it exists) and add the new one */
00283 
00284         regval_ctr_delvalue( ctr, name );
00285 
00286         /* allocate a slot in the array of pointers */
00287                 
00288         if (  ctr->num_values == 0 ) {
00289                 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
00290         } else {
00291                 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
00292         }
00293 
00294         if (!ctr->values) {
00295                 ctr->num_values = 0;
00296                 return 0;
00297         }
00298 
00299         /* allocate a new value and store the pointer in the arrya */
00300                 
00301         ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
00302         if (!ctr->values[ctr->num_values]) {
00303                 ctr->num_values = 0;
00304                 return 0;
00305         }
00306 
00307         /* init the value */
00308         
00309         fstrcpy( ctr->values[ctr->num_values]->valuename, name );
00310         ctr->values[ctr->num_values]->type = type;
00311         if (size) {
00312                 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
00313                         ctr, data_p, size );
00314                 if (!ctr->values[ctr->num_values]->data_p) {
00315                         ctr->num_values = 0;
00316                         return 0;
00317                 }
00318         } else {
00319                 ctr->values[ctr->num_values]->data_p = NULL;
00320         }
00321         ctr->values[ctr->num_values]->size = size;
00322         ctr->num_values++;
00323 
00324         return ctr->num_values;
00325 }
00326 
00327 /***********************************************************************
00328  Add a new registry value to the array
00329  **********************************************************************/
00330 
00331 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
00332 {
00333         if ( val ) {
00334                 /* allocate a slot in the array of pointers */
00335                 
00336                 if (  ctr->num_values == 0 ) {
00337                         ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
00338                 } else {
00339                         ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
00340                 }
00341 
00342                 if (!ctr->values) {
00343                         ctr->num_values = 0;
00344                         return 0;
00345                 }
00346 
00347                 /* allocate a new value and store the pointer in the arrya */
00348                 
00349                 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
00350                 if (!ctr->values[ctr->num_values]) {
00351                         ctr->num_values = 0;
00352                         return 0;
00353                 }
00354 
00355                 /* init the value */
00356         
00357                 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
00358                 ctr->values[ctr->num_values]->type = val->type;
00359                 if (val->size) {
00360                         ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
00361                                 ctr, val->data_p, val->size );
00362                         if (!ctr->values[ctr->num_values]->data_p) {
00363                                 ctr->num_values = 0;
00364                                 return 0;
00365                         }
00366                 } else {
00367                         ctr->values[ctr->num_values]->data_p = NULL;
00368                 }
00369                 ctr->values[ctr->num_values]->size = val->size;
00370                 ctr->num_values++;
00371         }
00372 
00373         return ctr->num_values;
00374 }
00375 
00376 /***********************************************************************
00377  Delete a single value from the registry container.
00378  No need to free memory since it is talloc'd.
00379  **********************************************************************/
00380 
00381 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
00382 {
00383         int     i;
00384         
00385         for ( i=0; i<ctr->num_values; i++ ) {
00386                 if ( strequal( ctr->values[i]->valuename, name ) )
00387                         break;
00388         }
00389         
00390         /* just return if we don't find it */
00391         
00392         if ( i == ctr->num_values )
00393                 return ctr->num_values;
00394         
00395         /* If 'i' was not the last element, just shift everything down one */
00396         ctr->num_values--;
00397         if ( i < ctr->num_values )
00398                 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
00399         
00400         return ctr->num_values;
00401 }
00402 
00403 /***********************************************************************
00404  Retrieve single value from the registry container.
00405  No need to free memory since it is talloc'd.
00406  **********************************************************************/
00407 
00408 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
00409 {
00410         int     i;
00411         
00412         /* search for the value */
00413         
00414         for ( i=0; i<ctr->num_values; i++ ) {
00415                 if ( strequal( ctr->values[i]->valuename, name ) )
00416                         return ctr->values[i];
00417         }
00418         
00419         return NULL;
00420 }
00421 
00422 /***********************************************************************
00423  return the data_p as a uint32
00424  **********************************************************************/
00425 
00426 uint32 regval_dword( REGISTRY_VALUE *val )
00427 {
00428         uint32 data;
00429         
00430         data = IVAL( regval_data_p(val), 0 );
00431         
00432         return data;
00433 }
00434 
00435 /***********************************************************************
00436  return the data_p as a character string
00437  **********************************************************************/
00438 
00439 char* regval_sz( REGISTRY_VALUE *val )
00440 {
00441         static pstring data;
00442 
00443         rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
00444         
00445         return data;
00446 }

Sambaに対してSat Aug 29 21:23:15 2009に生成されました。  doxygen 1.4.7