diff options
Diffstat (limited to 'source3/registry')
-rw-r--r-- | source3/registry/reg_cachehook.c | 2 | ||||
-rw-r--r-- | source3/registry/reg_db.c | 253 | ||||
-rw-r--r-- | source3/registry/reg_eventlog.c | 18 | ||||
-rw-r--r-- | source3/registry/reg_frontend.c | 42 | ||||
-rw-r--r-- | source3/registry/reg_objects.c | 82 | ||||
-rw-r--r-- | source3/registry/reg_printing.c | 382 | ||||
-rw-r--r-- | source3/registry/reg_shares.c | 164 | ||||
-rw-r--r-- | source3/registry/reg_util.c | 88 | ||||
-rw-r--r-- | source3/registry/regfio.c | 1871 |
9 files changed, 2528 insertions, 374 deletions
diff --git a/source3/registry/reg_cachehook.c b/source3/registry/reg_cachehook.c index dc7136a1d5..4b03a69acf 100644 --- a/source3/registry/reg_cachehook.c +++ b/source3/registry/reg_cachehook.c @@ -1,6 +1,6 @@ /* * Unix SMB/CIFS implementation. - * RPC Pipe client / server routines + * Virtual Windows Registry Layer * Copyright (C) Gerald Carter 2002. * * This program is free software; you can redistribute it and/or modify diff --git a/source3/registry/reg_db.c b/source3/registry/reg_db.c index 46e93cd2b9..19f7e64479 100644 --- a/source3/registry/reg_db.c +++ b/source3/registry/reg_db.c @@ -1,7 +1,7 @@ /* * Unix SMB/CIFS implementation. - * RPC Pipe client / server routines - * Copyright (C) Gerald Carter 2002. + * Virtual Windows Registry Layer + * Copyright (C) Gerald Carter 2002-2005 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,161 +28,96 @@ static TDB_CONTEXT *tdb_reg; +static BOOL regdb_store_reg_keys( char *keyname, REGSUBKEY_CTR *ctr ); +static int regdb_fetch_reg_keys( char* key, REGSUBKEY_CTR *ctr ); + + + +/* List the deepest path into the registry. All part components will be created.*/ + +/* If you want to have a part of the path controlled by the tdb abd part by + a virtual registry db (e.g. printing), then you have to list the deepest path. + For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print" + allows the reg_db backend to handle everything up to + "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook + the reg_printing backend onto the last component of the path (see + KEY_PRINTING_2K in include/rpc_reg.h) --jerry */ + +static const char *builtin_registry_paths[] = { + "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print", + "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Ports", + "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print", + "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions", + "HKLM\\SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Shares", + "HKLM\\SYSTEM\\CurrentControlSet\\Services\\EventLog", + "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters", + "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters", + "HKU", + "HKCR", + NULL }; + /*********************************************************************** Open the registry data in the tdb ***********************************************************************/ static BOOL init_registry_data( void ) { - pstring keyname; + pstring path, base, remaining; + fstring keyname, subkeyname; REGSUBKEY_CTR subkeys; - + int i; + const char *p, *p2; + ZERO_STRUCTP( &subkeys ); - - /* HKEY_LOCAL_MACHINE */ - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - regsubkey_ctr_addkey( &subkeys, "SYSTEM" ); - regsubkey_ctr_addkey( &subkeys, "SOFTWARE" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SOFTWARE" ); - regsubkey_ctr_addkey( &subkeys, "Microsoft" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SOFTWARE/Microsoft" ); - regsubkey_ctr_addkey( &subkeys, "Windows NT" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SOFTWARE/Microsoft/Windows NT" ); - regsubkey_ctr_addkey( &subkeys, "CurrentVersion" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SOFTWARE/Microsoft/Windows NT/CurrentVersion" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - - - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SOFTWARE/Microsoft/Windows NT/CurrentVersion" ); - regsubkey_ctr_addkey( &subkeys, "SystemRoot" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SOFTWARE/Microsoft/Windows NT/CurrentVersion/SystemRoot" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; + /* loop over all of the predefined paths and add each component */ + + for ( i=0; builtin_registry_paths[i] != NULL; i++ ) { + DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i])); - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM" ); - regsubkey_ctr_addkey( &subkeys, "CurrentControlSet" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); + pstrcpy( path, builtin_registry_paths[i] ); + pstrcpy( base, "" ); + p = path; - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet" ); - regsubkey_ctr_addkey( &subkeys, "Control" ); - regsubkey_ctr_addkey( &subkeys, "Services" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control" ); - regsubkey_ctr_addkey( &subkeys, "Print" ); - regsubkey_ctr_addkey( &subkeys, "ProductOptions" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control/ProductOptions" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); /* added */ - - /* ProductType is a VALUE under ProductOptions */ - - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services" ); - regsubkey_ctr_addkey( &subkeys, "Netlogon" ); - regsubkey_ctr_addkey( &subkeys, "Tcpip" ); - regsubkey_ctr_addkey( &subkeys, "Eventlog" ); - - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); + while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) { - regsubkey_ctr_init( &subkeys ); - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Netlogon" ); - regsubkey_ctr_addkey( &subkeys, "Parameters" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Netlogon/Parameters" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; + /* build up the registry path from the components */ + + if ( *base ) + pstrcat( base, "\\" ); + pstrcat( base, keyname ); + + /* get the immediate subkeyname (if we have one ) */ + + *subkeyname = '\0'; + if ( *p ) { + pstrcpy( remaining, p ); + p2 = remaining; + + if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) ) + fstrcpy( subkeyname, p2 ); + } - regsubkey_ctr_init( &subkeys ); /*added */ - pstrcpy( keyname, KEY_HKLM ); /*added */ - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Tcpip" ); - regsubkey_ctr_addkey( &subkeys, "Parameters" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - regsubkey_ctr_destroy( &subkeys ); - - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Tcpip/Parameters" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; + DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n", + base, *subkeyname ? subkeyname : "NULL")); + + /* we don't really care if the lookup succeeds or not since + we are about to update the record. We just want any + subkeys already present */ + + regsubkey_ctr_init( &subkeys ); + + regdb_fetch_reg_keys( base, &subkeys ); + if ( *subkeyname ) + regsubkey_ctr_addkey( &subkeys, subkeyname ); + if ( !regdb_store_reg_keys( base, &subkeys )) + return False; + + regsubkey_ctr_destroy( &subkeys ); + } + } - pstrcpy( keyname, KEY_HKLM ); - pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Eventlog" ); - if ( !regdb_store_reg_keys( keyname, &subkeys )) - return False; - - /* HKEY_USER */ - - pstrcpy( keyname, KEY_HKU ); - if ( !regdb_store_reg_keys( keyname, &subkeys ) ) - return False; - - /* HKEY_CLASSES_ROOT*/ - - pstrcpy( keyname, KEY_HKCR ); - if ( !regdb_store_reg_keys( keyname, &subkeys ) ) - return False; - return True; } @@ -197,12 +132,9 @@ BOOL init_registry_db( void ) if (tdb_reg && local_pid == sys_getpid()) return True; - /* - * try to open first without creating so we can determine - * if we need to init the data in the registry - */ + /* placeholder tdb; reinit upon startup */ - tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600); + tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST, O_RDWR, 0600); if ( !tdb_reg ) { tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); @@ -213,12 +145,13 @@ BOOL init_registry_db( void ) } DEBUG(10,("init_registry: Successfully created registry tdb\n")); + } - /* create the registry here */ - if ( !init_registry_data() ) { - DEBUG(0,("init_registry: Failed to initiailize data in registry!\n")); - return False; - } + /* create the registry here */ + + if ( !init_registry_data() ) { + DEBUG(0,("init_registry: Failed to initiailize data in registry!\n")); + return False; } local_pid = sys_getpid(); @@ -238,7 +171,7 @@ BOOL init_registry_db( void ) case. ***********************************************************************/ -BOOL regdb_store_reg_keys( char *keyname, REGSUBKEY_CTR *ctr ) +static BOOL regdb_store_reg_keys( char *key, REGSUBKEY_CTR *ctr ) { TDB_DATA kbuf, dbuf; char *buffer, *tmpbuf; @@ -246,10 +179,16 @@ BOOL regdb_store_reg_keys( char *keyname, REGSUBKEY_CTR *ctr ) uint32 len, buflen; BOOL ret = True; uint32 num_subkeys = regsubkey_ctr_numkeys( ctr ); + pstring keyname; - if ( !keyname ) + if ( !key ) return False; + + pstrcpy( keyname, key ); + + /* convert to key format */ + pstring_sub( keyname, "\\", "/" ); strupper_m( keyname ); /* allocate some initial memory */ @@ -303,7 +242,7 @@ done: of null terminated character strings ***********************************************************************/ -int regdb_fetch_reg_keys( char* key, REGSUBKEY_CTR *ctr ) +static int regdb_fetch_reg_keys( char* key, REGSUBKEY_CTR *ctr ) { pstring path; uint32 num_items; @@ -352,7 +291,7 @@ int regdb_fetch_reg_keys( char* key, REGSUBKEY_CTR *ctr ) of null terminated character strings ***********************************************************************/ -int regdb_fetch_reg_values( char* key, REGVAL_CTR *val ) +static int regdb_fetch_reg_values( char* key, REGVAL_CTR *val ) { UNISTR2 data; int num_vals; @@ -394,7 +333,7 @@ int regdb_fetch_reg_values( char* key, REGVAL_CTR *val ) values in the registry.tdb ***********************************************************************/ -BOOL regdb_store_reg_values( char *key, REGVAL_CTR *val ) +static BOOL regdb_store_reg_values( char *key, REGVAL_CTR *val ) { return False; } diff --git a/source3/registry/reg_eventlog.c b/source3/registry/reg_eventlog.c index ccac7a190e..50e4995b9e 100644 --- a/source3/registry/reg_eventlog.c +++ b/source3/registry/reg_eventlog.c @@ -1,6 +1,6 @@ /* * Unix SMB/CIFS implementation. - * RPC Pipe client / server routines + * Virtual Windows Registry Layer * Copyright (C) Marcin Krzysztof Porwit 2005. * * This program is free software; you can redistribute it and/or modify @@ -186,7 +186,7 @@ static char* trim_eventlog_reg_path( char *path ) Enumerate registry subkey names given a registry path. Caller is responsible for freeing memory to **subkeys *********************************************************************/ -int eventlog_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) +static int eventlog_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) { char *path; BOOL top_level = False; @@ -202,10 +202,14 @@ int eventlog_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) if ( !path ) top_level = True; - evtlog_list = lp_eventlog_list(); num_subkeys = 0; + if ( !(evtlog_list = lp_eventlog_list()) ) { + SAFE_FREE(path); + return num_subkeys; + } + - if ( top_level ) + if ( top_level ) { /* todo - get the eventlog subkey values from the smb.conf file for ( num_subkeys=0; num_subkeys<MAX_TOP_LEVEL_KEYS; num_subkeys++ ) @@ -247,7 +251,7 @@ int eventlog_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) Caller is responsible for freeing memory *********************************************************************/ -int eventlog_value_info( char *key, REGVAL_CTR *val ) +static int eventlog_value_info( char *key, REGVAL_CTR *val ) { char *path; BOOL top_level = False; @@ -276,7 +280,7 @@ int eventlog_value_info( char *key, REGVAL_CTR *val ) people storing eventlog information directly via registry calls (for now at least) *********************************************************************/ -BOOL eventlog_store_subkey( char *key, REGSUBKEY_CTR *subkeys ) +static BOOL eventlog_store_subkey( char *key, REGSUBKEY_CTR *subkeys ) { return False; } @@ -286,7 +290,7 @@ BOOL eventlog_store_subkey( char *key, REGSUBKEY_CTR *subkeys ) people storing eventlog information directly via registry calls (for now at least) *********************************************************************/ -BOOL eventlog_store_value( char *key, REGVAL_CTR *val ) +static BOOL eventlog_store_value( char *key, REGVAL_CTR *val ) { return False; } diff --git a/source3/registry/reg_frontend.c b/source3/registry/reg_frontend.c index 9f8747ef37..8333bcd31f 100644 --- a/source3/registry/reg_frontend.c +++ b/source3/registry/reg_frontend.c @@ -1,7 +1,7 @@ /* * Unix SMB/CIFS implementation. - * RPC Pipe client / server routines - * Copyright (C) Gerald Carter 2002. + * Virtual Windows Registry Layer + * Copyright (C) Gerald Carter 2002-2005 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,13 +27,17 @@ extern REGISTRY_OPS printing_ops; extern REGISTRY_OPS eventlog_ops; +extern REGISTRY_OPS shares_reg_ops; extern REGISTRY_OPS regdb_ops; /* these are the default */ /* array of REGISTRY_HOOK's which are read into a tree for easy access */ REGISTRY_HOOK reg_hooks[] = { - { KEY_PRINTING, &printing_ops }, - { KEY_EVENTLOG, &eventlog_ops }, + { KEY_PRINTING, &printing_ops }, + { KEY_PRINTING_2K, &printing_ops }, + { KEY_PRINTING_PORTS, &printing_ops }, + { KEY_EVENTLOG, &eventlog_ops }, + { KEY_SHARES, &shares_reg_ops }, { NULL, NULL } }; @@ -230,34 +234,4 @@ BOOL fetch_reg_values_specific( REGISTRY_KEY *key, REGISTRY_VALUE **val, uint32 return True; } -/*********************************************************************** - Utility function for splitting the base path of a registry path off - by setting base and new_path to the apprapriate offsets withing the - path. - - WARNING!! Does modify the original string! - ***********************************************************************/ - -BOOL reg_split_path( char *path, char **base, char **new_path ) -{ - char *p; - - *new_path = *base = NULL; - - if ( !path) - return False; - - *base = path; - - p = strchr( path, '\\' ); - - if ( p ) { - *p = '\0'; - *new_path = p+1; - } - - return True; -} - - diff --git a/source3/registry/reg_objects.c b/source3/registry/reg_objects.c index c8dc633087..add82ae0d4 100644 --- a/source3/registry/reg_objects.c +++ b/source3/registry/reg_objects.c @@ -1,7 +1,7 @@ /* * Unix SMB/CIFS implementation. - * RPC Pipe client / server routines - * Copyright (C) Gerald Carter 2002. + * Virtual Windows Registry Layer + * Copyright (C) Gerald Carter 2002-2005 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -44,27 +44,35 @@ int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname ) { uint32 len; char **pp; + int i; - if ( keyname ) - { - len = strlen( keyname ); + if ( !keyname ) + return ctr->num_subkeys; - /* allocate a space for the char* in the array */ - - if ( ctr->subkeys == 0 ) - ctr->subkeys = TALLOC_P( ctr->ctx, char *); - else { - pp = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->subkeys, char *, ctr->num_subkeys+1); - if ( pp ) - ctr->subkeys = pp; - } + len = strlen( keyname ); + + /* make sure the keyname is not already there */ + + for ( i=0; i<ctr->num_subkeys; i++ ) { + if ( strequal( ctr->subkeys[i], keyname ) ) + return ctr->num_subkeys; + } - /* allocate the string and save it in the array */ + /* allocate a space for the char* in the array */ - ctr->subkeys[ctr->num_subkeys] = TALLOC( ctr->ctx, len+1 ); - strncpy( ctr->subkeys[ctr->num_subkeys], keyname, len+1 ); - ctr->num_subkeys++; + if ( ctr->subkeys == 0 ) + ctr->subkeys = TALLOC_P( ctr->ctx, char *); + else { + pp = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->subkeys, char *, ctr->num_subkeys+1); + if ( pp ) + ctr->subkeys = pp; } + + /* allocate the string and save it in the array */ + + ctr->subkeys[ctr->num_subkeys] = TALLOC( ctr->ctx, len+1 ); + strncpy( ctr->subkeys[ctr->num_subkeys], keyname, len+1 ); + ctr->num_subkeys++; return ctr->num_subkeys; } @@ -239,30 +247,30 @@ int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type, { REGISTRY_VALUE **ppreg; - if ( name ) - { - /* allocate a slot in the array of pointers */ + if ( !name ) + return ctr->num_values; + + /* allocate a slot in the array of pointers */ - if ( ctr->num_values == 0 ) - ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *); - else { - ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); - if ( ppreg ) - ctr->values = ppreg; - } + if ( ctr->num_values == 0 ) + ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *); + else { + ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); + if ( ppreg ) + ctr->values = ppreg; + } - /* allocate a new value and store the pointer in the arrya */ + /* allocate a new value and store the pointer in the arrya */ - ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE); + ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE); - /* init the value */ + /* init the value */ - fstrcpy( ctr->values[ctr->num_values]->valuename, name ); - ctr->values[ctr->num_values]->type = type; - ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, data_p, size ); - ctr->values[ctr->num_values]->size = size; - ctr->num_values++; - } + fstrcpy( ctr->values[ctr->num_values]->valuename, name ); + ctr->values[ctr->num_values]->type = type; + ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, data_p, size ); + ctr->values[ctr->num_values]->size = size; + ctr->num_values++; return ctr->num_values; } diff --git a/source3/registry/reg_printing.c b/source3/registry/reg_printing.c index ee4d1dcb64..a7a89fdc34 100644 --- a/source3/registry/reg_printing.c +++ b/source3/registry/reg_printing.c @@ -1,7 +1,7 @@ /* * Unix SMB/CIFS implementation. - * RPC Pipe client / server routines - * Copyright (C) Gerald Carter 2002. + * Virtual Windows Registry Layer + * Copyright (C) Gerald Carter 2002-2005 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,7 +38,7 @@ static const char *top_level_keys[MAX_TOP_LEVEL_KEYS] = { "Forms", "Printers" }; - + /********************************************************************** It is safe to assume that every registry path passed into on of @@ -52,7 +52,14 @@ static const char *top_level_keys[MAX_TOP_LEVEL_KEYS] = { static char* trim_reg_path( char *path ) { char *p; - uint16 key_len = strlen(KEY_PRINTING); + uint16 key_len = strlen(path); + uint16 base_key_len; + + static int key_printing_len = strlen( KEY_PRINTING ); + static int key_printing2k_len = strlen( KEY_PRINTING_2K ); + static int key_printing_ports_len = strlen( KEY_PRINTING_PORTS ); + + /* * sanity check...this really should never be True. @@ -60,14 +67,30 @@ static char* trim_reg_path( char *path ) * the path buffer in the extreme case. */ - if ( strlen(path) < key_len ) { + if ( (key_len < key_printing_len) + && (key_len < key_printing2k_len) + && (key_len < key_printing_ports_len) ) + { DEBUG(0,("trim_reg_path: Registry path too short! [%s]\n", path)); - DEBUG(0,("trim_reg_path: KEY_PRINTING => [%s]!\n", KEY_PRINTING)); + return NULL; + } + + base_key_len = 0; + if ( StrnCaseCmp( KEY_PRINTING, path, key_printing_len ) == 0 ) { + base_key_len = key_printing_len; + } + else if ( StrnCaseCmp( KEY_PRINTING_2K, path, key_printing2k_len ) == 0 ) { + base_key_len = key_printing2k_len; + } + else if ( StrnCaseCmp( KEY_PRINTING_PORTS, path, key_printing2k_len ) == 0 ) { + base_key_len = key_printing_ports_len; + } + else { + DEBUG(0,("trim_reg_path: invalid path [%s]\n", path )); return NULL; } - - p = path + strlen( KEY_PRINTING ); + p = path + base_key_len; if ( *p == '\\' ) p++; @@ -79,9 +102,37 @@ static char* trim_reg_path( char *path ) } /********************************************************************** + *********************************************************************/ + +static int fill_ports_values( REGVAL_CTR *values ) +{ + int numlines, i; + char **lines; + UNISTR2 data; + WERROR result; + + result = enumports_hook( &numlines, &lines ); + + if ( !W_ERROR_IS_OK(result) ) + return -1; + + init_unistr2( &data, "", UNI_STR_TERMINATE); + for ( i=0; i<numlines; i++ ) + regval_ctr_addvalue( values, lines[i], REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + + return numlines; +} + + +/********************************************************************** handle enumeration of subkeys below KEY_PRINTING\Environments + Environments\$ARCH\Print Processors + Environments\$ARCH\Drivers\{0,2,3} *********************************************************************/ +#define ENVIRONMENT_DRIVERS 1 +#define ENVIRONMENT_PRINTPROC 2 + static int print_subpath_environments( char *key, REGSUBKEY_CTR *subkeys ) { const char *environments[] = { @@ -95,113 +146,127 @@ static int print_subpath_environments( char *key, REGSUBKEY_CTR *subkeys ) NULL }; fstring *drivers = NULL; int i, env_index, num_drivers; - BOOL valid_env = False; - char *base, *new_path; - char *keystr; - char *key2 = NULL; + char *keystr, *base, *subkeypath; + pstring key2; int num_subkeys = -1; + int env_subkey_type = 0; + int version; DEBUG(10,("print_subpath_environments: key=>[%s]\n", key ? key : "NULL" )); - /* listed architectures of installed drivers */ + /* list all possible architectures */ - if ( !key ) - { - /* Windows 9x drivers */ - - if ( get_ntdrivers( &drivers, environments[0], 0 ) ) - regsubkey_ctr_addkey( subkeys, environments[0] ); - SAFE_FREE( drivers ); - - /* Windows NT/2k intel drivers */ - - if ( get_ntdrivers( &drivers, environments[1], 2 ) - || get_ntdrivers( &drivers, environments[1], 3 ) ) - { - regsubkey_ctr_addkey( subkeys, environments[1] ); - } - SAFE_FREE( drivers ); - - /* Windows NT 4.0; non-intel drivers */ - for ( i=2; environments[i]; i++ ) { - if ( get_ntdrivers( &drivers, environments[i], 2 ) ) - regsubkey_ctr_addkey( subkeys, environments[i] ); - - } - SAFE_FREE( drivers ); + if ( !key ) { + for ( num_subkeys=0; environments[num_subkeys]; num_subkeys++ ) + regsubkey_ctr_addkey( subkeys, environments[num_subkeys] ); - num_subkeys = regsubkey_ctr_numkeys( subkeys ); - goto done; + return num_subkeys; } /* we are dealing with a subkey of "Environments */ - key2 = SMB_STRDUP( key ); + pstrcpy( key2, key ); keystr = key2; - reg_split_path( keystr, &base, &new_path ); + reg_split_path( keystr, &base, &subkeypath ); /* sanity check */ for ( env_index=0; environments[env_index]; env_index++ ) { - if ( StrCaseCmp( environments[env_index], base ) == 0 ) { - valid_env = True; + if ( strequal( environments[env_index], base ) ) break; - } } + if ( !environments[env_index] ) + return -1; + + /* ...\Print\Environements\...\ */ + + if ( !subkeypath ) { + regsubkey_ctr_addkey( subkeys, "Drivers" ); + regsubkey_ctr_addkey( subkeys, "Print Processors" ); + + return 2; + } + + /* more of the key path to process */ + + keystr = subkeypath; + reg_split_path( keystr, &base, &subkeypath ); - if ( !valid_env ) + /* ...\Print\Environements\...\Drivers\ */ + + if ( strequal(base, "Drivers") ) + env_subkey_type = ENVIRONMENT_DRIVERS; + else if ( strequal(base, "Print Processors") ) + env_subkey_type = ENVIRONMENT_PRINTPROC; + else + /* invalid path */ return -1; - - /* enumerate driver versions; environment is environments[env_index] */ - if ( !new_path ) { - switch ( env_index ) { - case 0: /* Win9x */ - if ( get_ntdrivers( &drivers, environments[0], 0 ) ) { - regsubkey_ctr_addkey( subkeys, "0" ); - SAFE_FREE( drivers ); - } - break; - case 1: /* Windows NT/2k - intel */ - if ( get_ntdrivers( &drivers, environments[1], 2 ) ) { - regsubkey_ctr_addkey( subkeys, "2" ); - SAFE_FREE( drivers ); - } - if ( get_ntdrivers( &drivers, environments[1], 3 ) ) { - regsubkey_ctr_addkey( subkeys, "3" ); - SAFE_FREE( drivers ); - } - break; - default: /* Windows NT - nonintel */ - if ( get_ntdrivers( &drivers, environments[env_index], 2 ) ) { - regsubkey_ctr_addkey( subkeys, "2" ); - SAFE_FREE( drivers ); - } - - } + if ( !subkeypath ) { + switch ( env_subkey_type ) { + case ENVIRONMENT_DRIVERS: + switch ( env_index ) { + case 0: /* Win9x */ + regsubkey_ctr_addkey( subkeys, "Version-0" ); + break; + default: /* Windows NT based systems */ + regsubkey_ctr_addkey( subkeys, "Version-2" ); + regsubkey_ctr_addkey( subkeys, "Version-3" ); + break; + } - num_subkeys = regsubkey_ctr_numkeys( subkeys ); - goto done; + return regsubkey_ctr_numkeys( subkeys ); + + case ENVIRONMENT_PRINTPROC: + if ( env_index == 1 || env_index == 5 || env_index == 6 ) + regsubkey_ctr_addkey( subkeys, "winprint" ); + + return regsubkey_ctr_numkeys( subkeys ); + } } /* we finally get to enumerate the drivers */ - keystr = new_path; - reg_split_path( keystr, &base, &new_path ); + keystr = subkeypath; + reg_split_path( keystr, &base, &subkeypath ); + + /* get thr print processors key out of the way */ + if ( env_subkey_type == ENVIRONMENT_PRINTPROC ) { + if ( !strequal( base, "winprint" ) ) + return -1; + return !subkeypath ? 0 : -1; + } - if ( !new_path ) { - num_drivers = get_ntdrivers( &drivers, environments[env_index], atoi(base) ); + /* only dealing with drivers from here on out */ + + version = atoi(&base[strlen(base)-1]); + + switch (env_index) { + case 0: + if ( version != 0 ) + return -1; + break; + default: + if ( version != 2 && version != 3 ) + return -1; + break; + } + + + if ( !subkeypath ) { + num_drivers = get_ntdrivers( &drivers, environments[env_index], version ); for ( i=0; i<num_drivers; i++ ) regsubkey_ctr_addkey( subkeys, drivers[i] ); - num_subkeys = regsubkey_ctr_numkeys( subkeys ); - goto done; - } + return regsubkey_ctr_numkeys( subkeys ); + } -done: - SAFE_FREE( key2 ); - - return num_subkeys; + /* if anything else left, just say if has no subkeys */ + + DEBUG(1,("print_subpath_environments: unhandled key [%s] (subkey == %s\n", + key, subkeypath )); + + return 0; } /*********************************************************************** @@ -228,10 +293,9 @@ static char* dos_basename ( char *path ) static int print_subpath_values_environments( char *key, REGVAL_CTR *val ) { - char *keystr; - char *key2 = NULL; - char *base, *new_path; - fstring env; + char *keystr, *base, *subkeypath; + pstring key2; + fstring arch_environment; fstring driver; int version; NT_PRINTER_DRIVER_INFO_LEVEL driver_ctr; @@ -242,46 +306,63 @@ static int print_subpath_values_environments( char *key, REGVAL_CTR *val ) int buffer_size = 0; int i, length; char *filename; - UNISTR2 data;; + UNISTR2 data; + int env_subkey_type = 0; + DEBUG(8,("print_subpath_values_environments: Enter key => [%s]\n", key ? key : "NULL")); if ( !key ) return 0; - /* - * The only key below KEY_PRINTING\Environments that - * posseses values is each specific printer driver - * First get the arch, version, & driver name - */ + /* The only keys below KEY_PRINTING\Environments is the + specific printer driver info */ - /* env */ + /* environment */ - key2 = SMB_STRDUP( key ); + pstrcpy( key2, key ); keystr = key2; - reg_split_path( keystr, &base, &new_path ); - if ( !base || !new_path ) + reg_split_path( keystr, &base, &subkeypath ); + if ( !subkeypath ) return 0; - fstrcpy( env, base ); + fstrcpy( arch_environment, base ); - /* version */ + /* Driver */ - keystr = new_path; - reg_split_path( keystr, &base, &new_path ); - if ( !base || !new_path ) + keystr = subkeypath; + reg_split_path( keystr, &base, &subkeypath ); + + if ( strequal(base, "Drivers") ) + env_subkey_type = ENVIRONMENT_DRIVERS; + else if ( strequal(base, "Print Processors") ) + env_subkey_type = ENVIRONMENT_PRINTPROC; + else + /* invalid path */ + return -1; + + if ( !subkeypath ) return 0; - version = atoi( base ); + + /* for now bail out if we are seeing anything other than the drivers key */ + + if ( env_subkey_type == ENVIRONMENT_PRINTPROC ) + return 0; + + keystr = subkeypath; + reg_split_path( keystr, &base, &subkeypath ); + + version = atoi(&base[strlen(base)-1]); /* printer driver name */ - keystr = new_path; - reg_split_path( keystr, &base, &new_path ); - /* new_path should be NULL here since this must be the last key */ - if ( !base || new_path ) + keystr = subkeypath; + reg_split_path( keystr, &base, &subkeypath ); + /* don't go any deeper for now */ + if ( subkeypath ) return 0; fstrcpy( driver, base ); - w_result = get_a_printer_driver( &driver_ctr, 3, driver, env, version ); + w_result = get_a_printer_driver( &driver_ctr, 3, driver, arch_environment, version ); if ( !W_ERROR_IS_OK(w_result) ) return -1; @@ -350,7 +431,6 @@ static int print_subpath_values_environments( char *key, REGVAL_CTR *val ) free_a_printer_driver( driver_ctr, 3 ); - SAFE_FREE( key2 ); SAFE_FREE( buffer ); DEBUG(8,("print_subpath_values_environments: Exit\n")); @@ -480,6 +560,11 @@ static int print_subpath_printers( char *key, REGSUBKEY_CTR *subkeys ) for (snum=0; snum<n_services; snum++) { if ( !(lp_snum_ok(snum) && lp_print_ok(snum) ) ) continue; + + /* don't report the [printers] share */ + + if ( strequal( lp_servicename(snum), PRINTERS_NAME ) ) + continue; fstrcpy( sname, lp_servicename(snum) ); @@ -552,8 +637,9 @@ static int print_subpath_values_printers( char *key, REGVAL_CTR *val ) fstrcpy( printername, base ); - if ( !new_path ) - { + if ( !new_path ) { + char *p; + /* we are dealing with the printer itself */ if ( !W_ERROR_IS_OK( get_a_printer(NULL, &printer, 2, printername) ) ) @@ -569,33 +655,45 @@ static int print_subpath_values_printers( char *key, REGVAL_CTR *val ) regval_ctr_addvalue( val, "Status", REG_DWORD, (char*)&info2->status, sizeof(info2->status) ); regval_ctr_addvalue( val, "StartTime", REG_DWORD, (char*)&info2->starttime, sizeof(info2->starttime) ); regval_ctr_addvalue( val, "UntilTime", REG_DWORD, (char*)&info2->untiltime, sizeof(info2->untiltime) ); - regval_ctr_addvalue( val, "cjobs", REG_DWORD, (char*)&info2->cjobs, sizeof(info2->cjobs) ); - regval_ctr_addvalue( val, "AveragePPM", REG_DWORD, (char*)&info2->averageppm, sizeof(info2->averageppm) ); - init_unistr2( &data, info2->printername, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Name", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + /* strip the \\server\ from this string */ + if ( !(p = strrchr( info2->printername, '\\' ) ) ) + p = info2->printername; + else + p++; + init_unistr2( &data, p, UNI_STR_TERMINATE); + regval_ctr_addvalue( val, "Name", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + init_unistr2( &data, info2->location, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Location", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + regval_ctr_addvalue( val, "Location", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + init_unistr2( &data, info2->comment, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Comment", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + regval_ctr_addvalue( val, "Description", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + init_unistr2( &data, info2->parameters, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Parameters", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + regval_ctr_addvalue( val, "Parameters", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + init_unistr2( &data, info2->portname, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Port", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); - init_unistr2( &data, info2->servername, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Server", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + regval_ctr_addvalue( val, "Port", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + init_unistr2( &data, info2->sharename, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Share", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + regval_ctr_addvalue( val, "Share Name", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + init_unistr2( &data, info2->drivername, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Driver", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + regval_ctr_addvalue( val, "Printer Driver", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + init_unistr2( &data, info2->sepfile, UNI_STR_TERMINATE); - regval_ctr_addvalue( val, "Separator File", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); - init_unistr2( &data, "winprint", UNI_STR_TERMINATE); + regval_ctr_addvalue( val, "Separator File", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + + init_unistr2( &data, "WinPrint", UNI_STR_TERMINATE); regval_ctr_addvalue( val, "Print Processor", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); - + + init_unistr2( &data, "RAW", UNI_STR_TERMINATE); + regval_ctr_addvalue( val, "Datatype", REG_SZ, (char*)data.buffer, data.uni_str_len*sizeof(uint16) ); + /* use a prs_struct for converting the devmode and security - descriptor to REG_BIARY */ + descriptor to REG_BINARY */ prs_init( &prs, MAX_PDU_FRAG_LEN, regval_ctr_getctx(val), MARSHALL); @@ -735,7 +833,7 @@ static int handle_printing_subpath( char *key, REGSUBKEY_CTR *subkeys, REGVAL_CT Caller is responsible for freeing memory to **subkeys *********************************************************************/ -int printing_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) +static int printing_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) { char *path; BOOL top_level = False; @@ -751,8 +849,15 @@ int printing_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) top_level = True; if ( top_level ) { - for ( num_subkeys=0; num_subkeys<MAX_TOP_LEVEL_KEYS; num_subkeys++ ) - regsubkey_ctr_addkey( subkey_ctr, top_level_keys[num_subkeys] ); + /* check between the two top level keys here */ + + if ( strequal( KEY_PRINTING, key ) ) { + regsubkey_ctr_addkey( subkey_ctr, "Environments" ); + regsubkey_ctr_addkey( subkey_ctr, "Forms" ); + } + else if ( strequal( KEY_PRINTING_2K, key ) ) { + regsubkey_ctr_addkey( subkey_ctr, "Printers" ); + } } else num_subkeys = handle_printing_subpath( path, subkey_ctr, NULL ); @@ -767,7 +872,7 @@ int printing_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) Caller is responsible for freeing memory *********************************************************************/ -int printing_value_info( char *key, REGVAL_CTR *val ) +static int printing_value_info( char *key, REGVAL_CTR *val ) { char *path; BOOL top_level = False; @@ -783,9 +888,10 @@ int printing_value_info( char *key, REGVAL_CTR *val ) top_level = True; /* fill in values from the getprinterdata_printer_server() */ - if ( top_level ) - num_values = 0; - else + if ( top_level ) { + if ( strequal( key, KEY_PRINTING_PORTS ) ) + num_values = fill_ports_values( val ); + } else num_values = handle_printing_subpath( path, NULL, val ); @@ -798,7 +904,7 @@ int printing_value_info( char *key, REGVAL_CTR *val ) (for now at least) *********************************************************************/ -BOOL printing_store_subkey( char *key, REGSUBKEY_CTR *subkeys ) +static BOOL printing_store_subkey( char *key, REGSUBKEY_CTR *subkeys ) { return False; } @@ -809,7 +915,7 @@ BOOL printing_store_subkey( char *key, REGSUBKEY_CTR *subkeys ) (for now at least) *********************************************************************/ -BOOL printing_store_value( char *key, REGVAL_CTR *val ) +static BOOL printing_store_value( char *key, REGVAL_CTR *val ) { return False; } diff --git a/source3/registry/reg_shares.c b/source3/registry/reg_shares.c new file mode 100644 index 0000000000..7538db7623 --- /dev/null +++ b/source3/registry/reg_shares.c @@ -0,0 +1,164 @@ +/* + * Unix SMB/CIFS implementation. + * Virtual Windows Registry Layer + * Copyright (C) Gerald Carter 2005 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* Implementation of registry virtual views for printing information */ + +#include "includes.h" + +#undef DBGC_CLASS +#define DBGC_CLASS DBGC_RPC_SRV + +/********************************************************************** + It is safe to assume that every registry path passed into on of + the exported functions here begins with KEY_PRINTING else + these functions would have never been called. This is a small utility + function to strip the beginning of the path and make a copy that the + caller can modify. Note that the caller is responsible for releasing + the memory allocated here. + **********************************************************************/ + +static char* trim_reg_path( char *path ) +{ + char *p; + uint16 key_len = strlen(KEY_SHARES); + + /* + * sanity check...this really should never be True. + * It is only here to prevent us from accessing outside + * the path buffer in the extreme case. + */ + + if ( strlen(path) < key_len ) { + DEBUG(0,("trim_reg_path: Registry path too short! [%s]\n", path)); + return NULL; + } + + + p = path + strlen( KEY_SHARES ); + + if ( *p == '\\' ) + p++; + + if ( *p ) + return SMB_STRDUP(p); + else + return NULL; +} + +/********************************************************************** + Enumerate registry subkey names given a registry path. + Caller is responsible for freeing memory to **subkeys + *********************************************************************/ + +static int shares_subkey_info( char *key, REGSUBKEY_CTR *subkey_ctr ) +{ + char *path; + BOOL top_level = False; + int num_subkeys = 0; + + DEBUG(10,("printing_subkey_info: key=>[%s]\n", key)); + + path = trim_reg_path( key ); + + /* check to see if we are dealing with the top level key */ + + if ( !path ) + top_level = True; + + if ( top_level ) { + num_subkeys = 1; + regsubkey_ctr_addkey( subkey_ctr, "Security" ); + } +#if 0 + else + num_subkeys = handle_share_subpath( path, subkey_ctr, NULL ); +#endif + + SAFE_FREE( path ); + + return num_subkeys; +} + +/********************************************************************** + Enumerate registry values given a registry path. + Caller is responsible for freeing memory + *********************************************************************/ + +static int shares_value_info( char *key, REGVAL_CTR *val ) +{ + char *path; + BOOL top_level = False; + int num_values = 0; + + DEBUG(10,("printing_value_info: key=>[%s]\n", key)); + + path = trim_reg_path( key ); + + /* check to see if we are dealing with the top level key */ + + if ( !path ) + top_level = True; + + /* fill in values from the getprinterdata_printer_server() */ + if ( top_level ) + num_values = 0; +#if 0 + else + num_values = handle_printing_subpath( path, NULL, val ); +#endif + + + return num_values; +} + +/********************************************************************** + Stub function which always returns failure since we don't want + people storing printing information directly via regostry calls + (for now at least) + *********************************************************************/ + +static BOOL shares_store_subkey( char *key, REGSUBKEY_CTR *subkeys ) +{ + return False; +} + +/********************************************************************** + Stub function which always returns failure since we don't want + people storing printing information directly via regostry calls + (for now at least) + *********************************************************************/ + +static BOOL shares_store_value( char *key, REGVAL_CTR *val ) +{ + return False; +} + +/* + * Table of function pointers for accessing printing data + */ + +REGISTRY_OPS shares_reg_ops = { + shares_subkey_info, + shares_value_info, + shares_store_subkey, + shares_store_value +}; + + diff --git a/source3/registry/reg_util.c b/source3/registry/reg_util.c new file mode 100644 index 0000000000..9120ce0e0a --- /dev/null +++ b/source3/registry/reg_util.c @@ -0,0 +1,88 @@ +/* + * Unix SMB/CIFS implementation. + * Virtual Windows Registry Layer (utility functions) + * Copyright (C) Gerald Carter 2002-2005 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* Implementation of registry frontend view functions. */ + +#include "includes.h" + +#undef DBGC_CLASS +#define DBGC_CLASS DBGC_RPC_SRV + +/*********************************************************************** + Utility function for splitting the base path of a registry path off + by setting base and new_path to the apprapriate offsets withing the + path. + + WARNING!! Does modify the original string! + ***********************************************************************/ + +BOOL reg_split_path( char *path, char **base, char **new_path ) +{ + char *p; + + *new_path = *base = NULL; + + if ( !path) + return False; + + *base = path; + + p = strchr( path, '\\' ); + + if ( p ) { + *p = '\0'; + *new_path = p+1; + } + + return True; +} + + +/*********************************************************************** + Utility function for splitting the base path of a registry path off + by setting base and new_path to the appropriate offsets withing the + path. + + WARNING!! Does modify the original string! + ***********************************************************************/ + +BOOL reg_split_key( char *path, char **base, char **key ) +{ + char *p; + + *key = *base = NULL; + + if ( !path) + return False; + + *base = path; + + p = strrchr( path, '\\' ); + + if ( p ) { + *p = '\0'; + *key = p+1; + } + + return True; +} + + + diff --git a/source3/registry/regfio.c b/source3/registry/regfio.c new file mode 100644 index 0000000000..ab6be77f18 --- /dev/null +++ b/source3/registry/regfio.c @@ -0,0 +1,1871 @@ +/* + * Unix SMB/CIFS implementation. + * Windows NT registry I/O library + * Copyright (c) Gerald (Jerry) Carter 2005 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "includes.h" +#include "regfio.h" + +/******************************************************************* + * + * TODO : Right now this code basically ignores classnames. + * + ******************************************************************/ + + +/******************************************************************* +*******************************************************************/ + +static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset ) +{ + int bytes_written, returned; + char *buffer = prs_data_p( ps ); + uint32 buffer_size = prs_data_size( ps ); + SMB_STRUCT_STAT sbuf; + + if ( file->fd == -1 ) + return -1; + + /* check for end of file */ + + if ( sys_fstat( file->fd, &sbuf ) ) { + DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno))); + return -1; + } + + if ( lseek( file->fd, offset, SEEK_SET ) == -1 ) { + DEBUG(0,("write_block: lseek() failed! (%s)\n", strerror(errno) )); + return -1; + } + + bytes_written = returned = 0; + while ( bytes_written < buffer_size ) { + if ( (returned = write( file->fd, buffer+bytes_written, buffer_size-bytes_written )) == -1 ) { + DEBUG(0,("write_block: write() failed! (%s)\n", strerror(errno) )); + return False; + } + + bytes_written += returned; + } + + return bytes_written; +} + +/******************************************************************* +*******************************************************************/ + +static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint32 block_size ) +{ + int bytes_read, returned; + char *buffer; + SMB_STRUCT_STAT sbuf; + + /* check for end of file */ + + if ( sys_fstat( file->fd, &sbuf ) ) { + DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno))); + return -1; + } + + if ( (size_t)file_offset >= sbuf.st_size ) + return -1; + + /* if block_size == 0, we are parsnig HBIN records and need + to read some of the header to get the block_size from there */ + + if ( block_size == 0 ) { + uint8 hdr[0x20]; + + if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) { + DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) )); + return -1; + } + + returned = read( file->fd, hdr, 0x20 ); + if ( (returned == -1) || (returned < 0x20) ) { + DEBUG(0,("read_block: failed to read in HBIN header. Is the file corrupt?\n")); + return -1; + } + + /* make sure this is an hbin header */ + + if ( strncmp( hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) { + DEBUG(0,("read_block: invalid block header!\n")); + return -1; + } + + block_size = IVAL( hdr, 0x08 ); + } + + DEBUG(10,("read_block: block_size == 0x%x\n", block_size )); + + /* set the offset, initialize the buffer, and read the block from disk */ + + if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) { + DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) )); + return -1; + } + + prs_init( ps, block_size, file->mem_ctx, UNMARSHALL ); + buffer = prs_data_p( ps ); + bytes_read = returned = 0; + + while ( bytes_read < block_size ) { + if ( (returned = read( file->fd, buffer+bytes_read, block_size-bytes_read )) == -1 ) { + DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) )); + return False; + } + if ( (returned == 0) && (bytes_read < block_size) ) { + DEBUG(0,("read_block: not a vald registry file ?\n" )); + return False; + } + + bytes_read += returned; + } + + return bytes_read; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL write_hbin_block( REGF_FILE *file, REGF_HBIN *hbin ) +{ + if ( !hbin->dirty ) + return True; + + /* write free space record if any is available */ + + if ( hbin->free_off != REGF_OFFSET_NONE ) { + uint32 header = 0xffffffff; + + if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) ) + return False; + if ( !prs_uint32( "free_size", &hbin->ps, 0, &hbin->free_size ) ) + return False; + if ( !prs_uint32( "free_header", &hbin->ps, 0, &header ) ) + return False; + } + + hbin->dirty = (write_block( file, &hbin->ps, hbin->file_off ) != -1); + + return hbin->dirty; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL hbin_block_close( REGF_FILE *file, REGF_HBIN *hbin ) +{ + REGF_HBIN *p; + + /* remove the block from the open list and flush it to disk */ + + for ( p=file->block_list; p && p!=hbin; p=p->next ) + ;; + + if ( p == hbin ) { + DLIST_REMOVE( file->block_list, hbin ); + } + else + DEBUG(0,("hbin_block_close: block not in open list!\n")); + + if ( !write_hbin_block( file, hbin ) ) + return False; + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL prs_regf_block( const char *desc, prs_struct *ps, int depth, REGF_FILE *file ) +{ + prs_debug(ps, depth, desc, "prs_regf_block"); + depth++; + + if ( !prs_uint8s( True, "header", ps, depth, file->header, sizeof( file->header )) ) + return False; + + /* yes, these values are always identical so store them only once */ + + if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 )) + return False; + if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 )) + return False; + + /* get the modtime */ + + if ( !prs_set_offset( ps, 0x0c ) ) + return False; + if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) ) + return False; + + /* constants */ + + if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 )) + return False; + if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 )) + return False; + if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 )) + return False; + if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 )) + return False; + + /* get file offsets */ + + if ( !prs_set_offset( ps, 0x24 ) ) + return False; + if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset )) + return False; + if ( !prs_uint32( "last_block", ps, depth, &file->last_block )) + return False; + + /* one more constant */ + + if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 )) + return False; + + /* get the checksum */ + + if ( !prs_set_offset( ps, 0x01fc ) ) + return False; + if ( !prs_uint32( "checksum", ps, depth, &file->checksum )) + return False; + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL prs_hbin_block( const char *desc, prs_struct *ps, int depth, REGF_HBIN *hbin ) +{ + uint32 block_size2; + + prs_debug(ps, depth, desc, "prs_regf_block"); + depth++; + + if ( !prs_uint8s( True, "header", ps, depth, hbin->header, sizeof( hbin->header )) ) + return False; + + if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off )) + return False; + + /* The dosreg.cpp comments say that the block size is at 0x1c. + According to a WINXP NTUSER.dat file, this is wrong. The block_size + is at 0x08 */ + + if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size )) + return False; + + block_size2 = hbin->block_size; + prs_set_offset( ps, 0x1c ); + if ( !prs_uint32( "block_size2", ps, depth, &block_size2 )) + return False; + + if ( MARSHALLING(ps) ) + hbin->dirty = True; + + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL prs_nk_rec( const char *desc, prs_struct *ps, int depth, REGF_NK_REC *nk ) +{ + uint16 class_length, name_length; + uint32 start; + uint32 data_size, start_off, end_off; + uint32 unknown_off = REGF_OFFSET_NONE; + + nk->hbin_off = prs_offset( ps ); + start = nk->hbin_off; + + prs_debug(ps, depth, desc, "prs_nk_rec"); + depth++; + + /* back up and get the data_size */ + + if ( !prs_set_offset( ps, prs_offset(ps)-sizeof(uint32)) ) + return False; + start_off = prs_offset( ps ); + if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size )) + return False; + + if ( !prs_uint8s( True, "header", ps, depth, nk->header, sizeof( nk->header )) ) + return False; + + if ( !prs_uint16( "key_type", ps, depth, &nk->key_type )) + return False; + if ( !smb_io_time( "mtime", &nk->mtime, ps, depth )) + return False; + + if ( !prs_set_offset( ps, start+0x0010 ) ) + return False; + if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off )) + return False; + if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys )) + return False; + + if ( !prs_set_offset( ps, start+0x001c ) ) + return False; + if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off )) + return False; + if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) ) + return False; + + if ( !prs_set_offset( ps, start+0x0024 ) ) + return False; + if ( !prs_uint32( "num_values", ps, depth, &nk->num_values )) + return False; + if ( !prs_uint32( "values_off", ps, depth, &nk->values_off )) + return False; + if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off )) + return False; + if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off )) + return False; + + if ( !prs_uint32( "max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname)) + return False; + if ( !prs_uint32( "max_bytes_subkeyclassname", ps, depth, &nk->max_bytes_subkeyclassname)) + return False; + if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename)) + return False; + if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value)) + return False; + if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index)) + return False; + + name_length = nk->keyname ? strlen(nk->keyname) : 0 ; + class_length = nk->classname ? strlen(nk->classname) : 0 ; + if ( !prs_uint16( "name_length", ps, depth, &name_length )) + return False; + if ( !prs_uint16( "class_length", ps, depth, &class_length )) + return False; + + if ( class_length ) { + ;; + } + + if ( name_length ) { + if ( UNMARSHALLING(ps) ) { + if ( !(nk->keyname = PRS_ALLOC_MEM( ps, char, name_length+1 )) ) + return False; + } + + if ( !prs_uint8s( True, "name", ps, depth, nk->keyname, name_length) ) + return False; + + if ( UNMARSHALLING(ps) ) + nk->keyname[name_length] = '\0'; + } + + end_off = prs_offset( ps ); + + /* data_size must be divisible by 8 and large enough to hold the original record */ + + data_size = ((start_off - end_off) & 0xfffffff8 ); + if ( data_size > nk->rec_size ) + DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size)); + + if ( MARSHALLING(ps) ) + nk->hbin->dirty = True; + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static uint32 regf_block_checksum( prs_struct *ps ) +{ + char *buffer = prs_data_p( ps ); + uint32 checksum, x; + int i; + + /* XOR of all bytes 0x0000 - 0x01FB */ + + checksum = x = 0; + + for ( i=0; i<0x01FB; i+=4 ) { + x = IVAL(buffer, i ); + checksum ^= x; + } + + return checksum; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL read_regf_block( REGF_FILE *file ) +{ + prs_struct ps; + uint32 checksum; + + /* grab the first block from the file */ + + if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 ) + return False; + + /* parse the block and verify the checksum */ + + if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) + return False; + + checksum = regf_block_checksum( &ps ); + + prs_mem_free( &ps ); + + if ( file->checksum != checksum ) { + DEBUG(0,("read_regf_block: invalid checksum\n" )); + return False; + } + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset ) +{ + REGF_HBIN *hbin; + uint32 record_size, curr_off, block_size, header; + + if ( !(hbin = TALLOC_ZERO_P(file->mem_ctx, REGF_HBIN)) ) + return NULL; + hbin->file_off = offset; + hbin->free_off = -1; + + if ( read_block( file, &hbin->ps, offset, 0 ) == -1 ) + return NULL; + + if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) ) + return NULL; + + /* this should be the same thing as hbin->block_size but just in case */ + + block_size = prs_data_size( &hbin->ps ); + + /* Find the available free space offset. Always at the end, + so walk the record list and stop when you get to the end. + The end is defined by a record header of 0xffffffff. The + previous 4 bytes contains the amount of free space remaining + in the hbin block. */ + + /* remember that the record_size is in the 4 bytes preceeding the record itself */ + + if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) ) + return False; + + record_size = 0; + curr_off = prs_offset( &hbin->ps ); + while ( header != 0xffffffff ) { + /* not done yet so reset the current offset to the + next record_size field */ + + curr_off = curr_off+record_size; + + /* for some reason the record_size of the last record in + an hbin block can extend past the end of the block + even though the record fits within the remaining + space....aaarrrgggghhhhhh */ + + if ( curr_off >= block_size ) { + record_size = -1; + curr_off = -1; + break; + } + + if ( !prs_set_offset( &hbin->ps, curr_off) ) + return False; + + if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) ) + return False; + if ( !prs_uint32( "header", &hbin->ps, 0, &header ) ) + return False; + + SMB_ASSERT( record_size != 0 ); + + if ( record_size & 0x80000000 ) { + /* absolute_value(record_size) */ + record_size = (record_size ^ 0xffffffff) + 1; + } + } + + /* save the free space offset */ + + if ( header == 0xffffffff ) { + + /* account for the fact that the curr_off is 4 bytes behind the actual + record header */ + + hbin->free_off = curr_off + sizeof(uint32); + hbin->free_size = record_size; + } + + DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off)); + + if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE ) ) + return False; + + return hbin; +} + +/******************************************************************* + Input a randon offset and receive the correpsonding HBIN + block for it +*******************************************************************/ + +static BOOL hbin_contains_offset( REGF_HBIN *hbin, uint32 offset ) +{ + if ( !hbin ) + return False; + + if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) ) + return True; + + return False; +} + +/******************************************************************* + Input a randon offset and receive the correpsonding HBIN + block for it +*******************************************************************/ + +static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset ) +{ + REGF_HBIN *hbin = NULL; + uint32 block_off; + + /* start with the open list */ + + for ( hbin=file->block_list; hbin; hbin=hbin->next ) { + DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%x]\n", hbin->file_off, (uint32)hbin )); + if ( hbin_contains_offset( hbin, offset ) ) + return hbin; + } + + if ( !hbin ) { + /* start at the beginning */ + + block_off = REGF_BLOCKSIZE; + do { + /* cleanup before the next round */ + if ( hbin ) + prs_mem_free( &hbin->ps ); + + hbin = read_hbin_block( file, block_off ); + + if ( hbin ) + block_off = hbin->file_off + hbin->block_size; + + } while ( hbin && !hbin_contains_offset( hbin, offset ) ); + } + + if ( hbin ) + DLIST_ADD( file->block_list, hbin ); + + return hbin; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash ) +{ + prs_debug(ps, depth, desc, "prs_hash_rec"); + depth++; + + if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off )) + return False; + if ( !prs_uint8s( True, "keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) ) + return False; + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL hbin_prs_lf_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk ) +{ + int i; + REGF_LF_REC *lf = &nk->subkeys; + uint32 data_size, start_off, end_off; + + prs_debug(&hbin->ps, depth, desc, "prs_lf_records"); + depth++; + + /* check if we have anything to do first */ + + if ( nk->num_subkeys == 0 ) + return True; + + /* move to the LF record */ + + if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) ) + return False; + + /* backup and get the data_size */ + + if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) ) + return False; + start_off = prs_offset( &hbin->ps ); + if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size )) + return False; + + if ( !prs_uint8s( True, "header", &hbin->ps, depth, lf->header, sizeof( lf->header )) ) + return False; + + if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys)) + return False; + + if ( UNMARSHALLING(&hbin->ps) ) { + if ( !(lf->hashes = PRS_ALLOC_MEM( &hbin->ps, REGF_HASH_REC, lf->num_keys )) ) + return False; + } + + for ( i=0; i<lf->num_keys; i++ ) { + if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) ) + return False; + } + + end_off = prs_offset( &hbin->ps ); + + /* data_size must be divisible by 8 and large enough to hold the original record */ + + data_size = ((start_off - end_off) & 0xfffffff8 ); + if ( data_size > lf->rec_size ) + DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size)); + + if ( MARSHALLING(&hbin->ps) ) + hbin->dirty = True; + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk ) +{ + prs_struct *ps = &hbin->ps; + uint16 tag = 0xFFFF; + uint32 data_size, start_off, end_off; + + + prs_debug(ps, depth, desc, "hbin_prs_sk_rec"); + depth++; + + if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) ) + return False; + + /* backup and get the data_size */ + + if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) ) + return False; + start_off = prs_offset( &hbin->ps ); + if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size )) + return False; + + if ( !prs_uint8s( True, "header", ps, depth, sk->header, sizeof( sk->header )) ) + return False; + if ( !prs_uint16( "tag", ps, depth, &tag)) + return False; + + if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off)) + return False; + if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off)) + return False; + if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count)) + return False; + if ( !prs_uint32( "size", ps, depth, &sk->size)) + return False; + + if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth )) + return False; + + end_off = prs_offset( &hbin->ps ); + + /* data_size must be divisible by 8 and large enough to hold the original record */ + + data_size = ((start_off - end_off) & 0xfffffff8 ); + if ( data_size > sk->rec_size ) + DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size)); + + if ( MARSHALLING(&hbin->ps) ) + hbin->dirty = True; + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_VK_REC *vk, REGF_FILE *file ) +{ + uint32 offset; + uint16 name_length; + prs_struct *ps = &hbin->ps; + uint32 data_size, start_off, end_off; + + prs_debug(ps, depth, desc, "prs_vk_rec"); + depth++; + + /* backup and get the data_size */ + + if ( !prs_set_offset( &hbin->ps, prs_offset(&hbin->ps)-sizeof(uint32)) ) + return False; + start_off = prs_offset( &hbin->ps ); + if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size )) + return False; + + if ( !prs_uint8s( True, "header", ps, depth, vk->header, sizeof( vk->header )) ) + return False; + + if ( MARSHALLING(&hbin->ps) ) + name_length = strlen(vk->valuename); + + if ( !prs_uint16( "name_length", ps, depth, &name_length )) + return False; + if ( !prs_uint32( "data_size", ps, depth, &vk->data_size )) + return False; + if ( !prs_uint32( "data_off", ps, depth, &vk->data_off )) + return False; + if ( !prs_uint32( "type", ps, depth, &vk->type)) + return False; + if ( !prs_uint16( "flag", ps, depth, &vk->flag)) + return False; + + offset = prs_offset( ps ); + offset += 2; /* skip 2 bytes */ + prs_set_offset( ps, offset ); + + /* get the name */ + + if ( vk->flag&VK_FLAG_NAME_PRESENT ) { + + if ( UNMARSHALLING(&hbin->ps) ) { + if ( !(vk->valuename = PRS_ALLOC_MEM( ps, char, name_length+1 ))) + return False; + } + if ( !prs_uint8s( True, "name", ps, depth, vk->valuename, name_length ) ) + return False; + } + + end_off = prs_offset( &hbin->ps ); + + /* get the data if necessary */ + + if ( vk->data_size != 0 ) { + BOOL charmode = False; + + if ( (vk->type == REG_SZ) || (vk->type == REG_MULTI_SZ) ) + charmode = True; + + /* the data is stored in the offset if the size <= 4 */ + + if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) { + REGF_HBIN *hblock = hbin; + uint32 data_rec_size; + + if ( UNMARSHALLING(&hbin->ps) ) { + if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, vk->data_size) ) ) + return False; + } + + /* this data can be in another hbin */ + if ( !hbin_contains_offset( hbin, vk->data_off ) ) { + if ( !(hblock = lookup_hbin_block( file, vk->data_off )) ) + return False; + } + if ( !(prs_set_offset( &hblock->ps, (vk->data_off+HBIN_HDR_SIZE-hblock->first_hbin_off)-sizeof(uint32) )) ) + return False; + + if ( MARSHALLING(&hblock->ps) ) { + data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8; + data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF; + } + if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size )) + return False; + if ( !prs_uint8s( charmode, "data", &hblock->ps, depth, vk->data, vk->data_size) ) + return False; + + if ( MARSHALLING(&hblock->ps) ) + hblock->dirty = True; + } + else { + if ( !(vk->data = PRS_ALLOC_MEM( ps, uint8, 4 ) ) ) + return False; + SIVAL( vk->data, 0, vk->data_off ); + } + + } + + /* data_size must be divisible by 8 and large enough to hold the original record */ + + data_size = ((start_off - end_off ) & 0xfffffff8 ); + if ( data_size != vk->rec_size ) + DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size)); + + if ( MARSHALLING(&hbin->ps) ) + hbin->dirty = True; + + return True; +} + +/******************************************************************* + read a VK record which is contained in the HBIN block stored + in the prs_struct *ps. +*******************************************************************/ + +static BOOL hbin_prs_vk_records( const char *desc, REGF_HBIN *hbin, int depth, REGF_NK_REC *nk, REGF_FILE *file ) +{ + int i; + uint32 record_size; + + prs_debug(&hbin->ps, depth, desc, "prs_vk_records"); + depth++; + + /* check if we have anything to do first */ + + if ( nk->num_values == 0 ) + return True; + + if ( UNMARSHALLING(&hbin->ps) ) { + if ( !(nk->values = PRS_ALLOC_MEM( &hbin->ps, REGF_VK_REC, nk->num_values ) ) ) + return False; + } + + /* convert the offset to something relative to this HBIN block */ + + if ( !prs_set_offset( &hbin->ps, nk->values_off+HBIN_HDR_SIZE-hbin->first_hbin_off-sizeof(uint32)) ) + return False; + + if ( MARSHALLING( &hbin->ps) ) { + record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8; + record_size = (record_size - 1) ^ 0xFFFFFFFF; + } + + if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) ) + return False; + + for ( i=0; i<nk->num_values; i++ ) { + if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) ) + return False; + } + + for ( i=0; i<nk->num_values; i++ ) { + REGF_HBIN *sub_hbin = hbin; + uint32 new_offset; + + if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) { + sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off ); + if ( !sub_hbin ) { + DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n", + nk->values[i].hbin_off)); + return False; + } + } + + new_offset = nk->values[i].rec_off + HBIN_HDR_SIZE - sub_hbin->first_hbin_off; + if ( !prs_set_offset( &sub_hbin->ps, new_offset ) ) + return False; + if ( !hbin_prs_vk_rec( "vk_rec", sub_hbin, depth, &nk->values[i], file ) ) + return False; + } + + if ( MARSHALLING(&hbin->ps) ) + hbin->dirty = True; + + + return True; +} + + +/******************************************************************* +*******************************************************************/ + +static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset ) +{ + REGF_SK_REC *p_sk; + + for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) { + if ( p_sk->sk_off == offset ) + return p_sk; + } + + return NULL; +} + +/******************************************************************* +*******************************************************************/ + +static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd ) +{ + REGF_SK_REC *p; + + for ( p=file->sec_desc_list; p; p=p->next ) { + if ( sec_desc_equal( p->sec_desc, sd ) ) + return p; + } + + /* failure */ + + return NULL; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk ) +{ + int depth = 0; + REGF_HBIN *sub_hbin; + + prs_debug(&hbin->ps, depth, "", "fetch_key"); + depth++; + + /* get the initial nk record */ + + if ( !prs_nk_rec( "nk_rec", &hbin->ps, depth, nk )) + return False; + + /* fill in values */ + + if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) { + sub_hbin = hbin; + if ( !hbin_contains_offset( hbin, nk->values_off ) ) { + sub_hbin = lookup_hbin_block( file, nk->values_off ); + if ( !sub_hbin ) { + DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n", + nk->values_off)); + return False; + } + } + + if ( !hbin_prs_vk_records( "vk_rec", sub_hbin, depth, nk, file )) + return False; + } + + /* now get subkeys */ + + if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) { + sub_hbin = hbin; + if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) { + sub_hbin = lookup_hbin_block( file, nk->subkeys_off ); + if ( !sub_hbin ) { + DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n", + nk->subkeys_off)); + return False; + } + } + + if ( !hbin_prs_lf_records( "lf_rec", sub_hbin, depth, nk )) + return False; + } + + /* get the to the security descriptor. First look if we have already parsed it */ + + if ( (nk->sk_off!=REGF_OFFSET_NONE) && !( nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )) ) { + + sub_hbin = hbin; + if ( !hbin_contains_offset( hbin, nk->sk_off ) ) { + sub_hbin = lookup_hbin_block( file, nk->sk_off ); + if ( !sub_hbin ) { + DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n", + nk->subkeys_off)); + return False; + } + } + + if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) ) + return False; + nk->sec_desc->sk_off = nk->sk_off; + if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc )) + return False; + + /* add to the list of security descriptors (ref_count has been read from the files) */ + + nk->sec_desc->sk_off = nk->sk_off; + DLIST_ADD( file->sec_desc_list, nk->sec_desc ); + } + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL next_record( REGF_HBIN *hbin, const char *hdr, BOOL *eob ) +{ + char header[REC_HDR_SIZE] = ""; + uint32 record_size; + uint32 curr_off, block_size; + BOOL found = False; + prs_struct *ps = &hbin->ps; + + curr_off = prs_offset( ps ); + if ( curr_off == 0 ) + prs_set_offset( ps, HBIN_HEADER_REC_SIZE ); + + /* assume that the current offset is at the reacord header + and we need to backup to read the record size */ + + curr_off -= sizeof(uint32); + + block_size = prs_data_size( ps ); + record_size = 0; + while ( !found ) { + + curr_off = curr_off+record_size; + if ( curr_off >= block_size ) + break; + + if ( !prs_set_offset( &hbin->ps, curr_off) ) + return False; + + if ( !prs_uint32( "record_size", ps, 0, &record_size ) ) + return False; + if ( !prs_uint8s( True, "header", ps, 0, header, REC_HDR_SIZE ) ) + return False; + + if ( record_size & 0x80000000 ) { + /* absolute_value(record_size) */ + record_size = (record_size ^ 0xffffffff) + 1; + } + + if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) { + found = True; + curr_off += sizeof(uint32); + } + } + + /* mark prs_struct as done ( at end ) if no more SK records */ + /* mark end-of-block as True */ + + if ( !found ) { + prs_set_offset( &hbin->ps, prs_data_size(&hbin->ps) ); + *eob = True; + return False; + } + + if ( !prs_set_offset( ps, curr_off ) ) + return False; + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL next_nk_record( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk, BOOL *eob ) +{ + if ( next_record( hbin, "nk", eob ) && hbin_prs_key( file, hbin, nk ) ) + return True; + + return False; +} + +/******************************************************************* + Intialize the newly created REGF_BLOCK in *file and write the + block header to disk +*******************************************************************/ + +static BOOL init_regf_block( REGF_FILE *file ) +{ + prs_struct ps; + BOOL result = True; + + if ( !prs_init( &ps, REGF_BLOCKSIZE, file->mem_ctx, MARSHALL ) ) + return False; + + memcpy( file->header, "regf", REGF_HDR_SIZE ); + file->data_offset = 0x20; + file->last_block = 0x1000; + + /* set mod time */ + + unix_to_nt_time( &file->mtime, time(NULL) ); + + /* hard coded values...no diea what these are ... maybe in time */ + + file->unknown1 = 0x2; + file->unknown2 = 0x1; + file->unknown3 = 0x3; + file->unknown4 = 0x0; + file->unknown5 = 0x1; + file->unknown6 = 0x1; + + /* write header to the buffer */ + + if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) { + result = False; + goto out; + } + + /* calculate the checksum, re-marshall data (to include the checksum) + and write to disk */ + + file->checksum = regf_block_checksum( &ps ); + prs_set_offset( &ps, 0 ); + if ( !prs_regf_block( "regf_header", &ps, 0, file ) ) { + result = False; + goto out; + } + + if ( write_block( file, &ps, 0 ) == -1 ) { + DEBUG(0,("init_regf_block: Failed to initialize registry header block!\n")); + result = False; + goto out; + } + +out: + prs_mem_free( &ps ); + + return result; +} +/******************************************************************* + Open the registry file and then read in the REGF block to get the + first hbin offset. +*******************************************************************/ + + REGF_FILE* regfio_open( const char *filename, int flags, int mode ) +{ + REGF_FILE *rb; + + if ( !(rb = SMB_MALLOC_P(REGF_FILE)) ) { + DEBUG(0,("ERROR allocating memory\n")); + return NULL; + } + ZERO_STRUCTP( rb ); + rb->fd = -1; + + if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) { + regfio_close( rb ); + return NULL; + } + + rb->open_flags = flags; + + /* open and existing file */ + + if ( (rb->fd = open(filename, flags, mode)) == -1 ) { + DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno))); + regfio_close( rb ); + return NULL; + } + + /* check if we are creating a new file or overwriting an existing one */ + + if ( flags & (O_CREAT|O_TRUNC) ) { + if ( !init_regf_block( rb ) ) { + DEBUG(0,("regfio_open: Failed to read initial REGF block\n")); + regfio_close( rb ); + return NULL; + } + + /* success */ + return rb; + } + + /* read in an existing file */ + + if ( !read_regf_block( rb ) ) { + DEBUG(0,("regfio_open: Failed to read initial REGF block\n")); + regfio_close( rb ); + return NULL; + } + + /* success */ + + return rb; +} + +/******************************************************************* +*******************************************************************/ + +static void regfio_mem_free( REGF_FILE *file ) +{ + /* free any talloc()'d memory */ + + if ( file && file->mem_ctx ) + talloc_destroy( file->mem_ctx ); +} + +/******************************************************************* +*******************************************************************/ + + int regfio_close( REGF_FILE *file ) +{ + int fd; + + /* cleanup for a file opened for write */ + + if ( file->open_flags & (O_WRONLY|O_RDWR) ) { + prs_struct ps; + REGF_SK_REC *sk; + + /* write of sd list */ + + for ( sk=file->sec_desc_list; sk; sk=sk->next ) { + hbin_prs_sk_rec( "sk_rec", sk->hbin, 0, sk ); + } + + /* flush any dirty blocks */ + + while ( file->block_list ) { + hbin_block_close( file, file->block_list ); + } + + ZERO_STRUCT( ps ); + + unix_to_nt_time( &file->mtime, time(NULL) ); + + if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) != -1 ) { + /* now use for writing */ + prs_switch_type( &ps, MARSHALL ); + + /* stream the block once, generate the checksum, + and stream it again */ + prs_set_offset( &ps, 0 ); + prs_regf_block( "regf_blocK", &ps, 0, file ); + file->checksum = regf_block_checksum( &ps ); + prs_set_offset( &ps, 0 ); + prs_regf_block( "regf_blocK", &ps, 0, file ); + + /* now we are ready to write it to disk */ + if ( write_block( file, &ps, 0 ) == -1 ) + DEBUG(0,("regfio_close: failed to update the regf header block!\n")); + } + + prs_mem_free( &ps ); + } + + regfio_mem_free( file ); + + /* nothing tdo do if there is no open file */ + + if ( !file || (file->fd == -1) ) + return 0; + + fd = file->fd; + file->fd = -1; + SAFE_FREE( file ); + + return close( fd ); +} + +/******************************************************************* +*******************************************************************/ + +static void regfio_flush( REGF_FILE *file ) +{ + REGF_HBIN *hbin; + + for ( hbin=file->block_list; hbin; hbin=hbin->next ) { + write_hbin_block( file, hbin ); + } +} + +/******************************************************************* + There should be only *one* root key in the registry file based + on my experience. --jerry +*******************************************************************/ + +REGF_NK_REC* regfio_rootkey( REGF_FILE *file ) +{ + REGF_NK_REC *nk; + REGF_HBIN *hbin; + uint32 offset = REGF_BLOCKSIZE; + BOOL found = False; + BOOL eob; + + if ( !file ) + return NULL; + + if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) { + DEBUG(0,("regfio_rootkey: talloc() failed!\n")); + return NULL; + } + + /* scan through the file on HBIN block at a time looking + for an NK record with a type == 0x002c. + Normally this is the first nk record in the first hbin + block (but I'm not assuming that for now) */ + + while ( (hbin = read_hbin_block( file, offset )) ) { + eob = False; + + while ( !eob) { + if ( next_nk_record( file, hbin, nk, &eob ) ) { + if ( nk->key_type == NK_TYPE_ROOTKEY ) { + found = True; + break; + } + } + prs_mem_free( &hbin->ps ); + } + + if ( found ) + break; + + offset += hbin->block_size; + } + + if ( !found ) { + DEBUG(0,("regfio_rootkey: corrupt registry file ? No root key record located\n")); + return NULL; + } + + DLIST_ADD( file->block_list, hbin ); + + return nk; +} + +/******************************************************************* + This acts as an interator over the subkeys defined for a given + NK record. Remember that offsets are from the *first* HBIN block. +*******************************************************************/ + + REGF_NK_REC* regfio_fetch_subkey( REGF_FILE *file, REGF_NK_REC *nk ) +{ + REGF_NK_REC *subkey; + REGF_HBIN *hbin; + uint32 nk_offset; + + /* see if there is anything left to report */ + + if ( !nk || (nk->subkeys_off==REGF_OFFSET_NONE) || (nk->subkey_index >= nk->num_subkeys) ) + return NULL; + + /* find the HBIN block which should contain the nk record */ + + if ( !(hbin = lookup_hbin_block( file, nk->subkeys.hashes[nk->subkey_index].nk_off )) ) { + DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n", + nk->subkeys.hashes[nk->subkey_index].nk_off)); + return NULL; + } + + nk_offset = nk->subkeys.hashes[nk->subkey_index].nk_off; + if ( !prs_set_offset( &hbin->ps, (HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off) ) ) + return NULL; + + nk->subkey_index++; + if ( !(subkey = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) + return NULL; + + if ( !hbin_prs_key( file, hbin, subkey ) ) + return NULL; + + return subkey; +} + + +/******************************************************************* +*******************************************************************/ + +static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size ) +{ + REGF_HBIN *hbin; + SMB_STRUCT_STAT sbuf; + + if ( !(hbin = TALLOC_ZERO_P( file->mem_ctx, REGF_HBIN )) ) + return NULL; + + memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) ); + + + if ( sys_fstat( file->fd, &sbuf ) ) { + DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno))); + return NULL; + } + + hbin->file_off = sbuf.st_size; + + hbin->free_off = HBIN_HEADER_REC_SIZE; + hbin->free_size = block_size - hbin->free_off + sizeof(uint32);; + + hbin->block_size = block_size; + hbin->first_hbin_off = hbin->file_off - REGF_BLOCKSIZE; + + if ( !prs_init( &hbin->ps, block_size, file->mem_ctx, MARSHALL ) ) + return NULL; + + if ( !prs_hbin_block( "new_hbin", &hbin->ps, 0, hbin ) ) + return NULL; + + if ( !write_hbin_block( file, hbin ) ) + return NULL; + + file->last_block = hbin->file_off; + + return hbin; +} + +/******************************************************************* +*******************************************************************/ + +static void update_free_space( REGF_HBIN *hbin, uint32 size_used ) +{ + hbin->free_off += size_used; + hbin->free_size -= size_used; + + if ( hbin->free_off >= hbin->block_size ) { + hbin->free_off = REGF_OFFSET_NONE; + } + + return; +} + +/******************************************************************* +*******************************************************************/ + +static REGF_HBIN* find_free_space( REGF_FILE *file, uint32 size ) +{ + REGF_HBIN *hbin, *p_hbin; + uint32 block_off; + BOOL cached; + + /* check open block list */ + + for ( hbin=file->block_list; hbin!=NULL; hbin=hbin->next ) { + /* only check blocks that actually have available space */ + + if ( hbin->free_off == REGF_OFFSET_NONE ) + continue; + + /* check for a large enough available chunk */ + + if ( (hbin->block_size - hbin->free_off) >= size ) { + DLIST_PROMOTE( file->block_list, hbin ); + goto done; + } + } + + /* parse the file until we find a block with + enough free space; save the last non-filled hbin */ + + block_off = REGF_BLOCKSIZE; + do { + /* cleanup before the next round */ + cached = False; + if ( hbin ) + prs_mem_free( &hbin->ps ); + + hbin = read_hbin_block( file, block_off ); + + if ( hbin ) { + + /* make sure that we don't already have this block in memory */ + + for ( p_hbin=file->block_list; p_hbin!=NULL; p_hbin=p_hbin->next ) { + if ( p_hbin->file_off == hbin->file_off ) { + cached = True; + break; + } + } + + block_off = hbin->file_off + hbin->block_size; + + if ( cached ) { + prs_mem_free( &hbin->ps ); + hbin = NULL; + continue; + } + } + /* if (cached block or (new block and not enough free space)) then continue looping */ + } while ( cached || (hbin && (hbin->free_size < size)) ); + + /* no free space; allocate a new one */ + + if ( !hbin ) { + uint32 alloc_size; + + /* allocate in multiples of REGF_ALLOC_BLOCK; make sure (size + hbin_header) fits */ + + alloc_size = ((size+HBIN_HEADER_REC_SIZE) / REGF_ALLOC_BLOCK ) + REGF_ALLOC_BLOCK; + + if ( !(hbin = regf_hbin_allocate( file, alloc_size )) ) { + DEBUG(0,("find_free_space: regf_hbin_allocate() failed!\n")); + return NULL; + } + DLIST_ADD( file->block_list, hbin ); + } + +done: + /* set the offset to be ready to write */ + + if ( !prs_set_offset( &hbin->ps, hbin->free_off-sizeof(uint32) ) ) + return NULL; + + /* write the record size as a placeholder for now, it should be + probably updated by the caller once it all of the data necessary + for the record */ + + if ( !prs_uint32("allocated_size", &hbin->ps, 0, &size) ) + return False; + + update_free_space( hbin, size ); + + return hbin; +} + +/******************************************************************* +*******************************************************************/ + +static uint32 sk_record_data_size( SEC_DESC * sd ) +{ + uint32 size, size_mod8; + + size_mod8 = 0; + + /* the record size is sizeof(hdr) + name + static members + data_size_field */ + + size = sizeof(uint32)*5 + sec_desc_size( sd ) + sizeof(uint32); + + /* multiple of 8 */ + size_mod8 = size & 0xfffffff8; + if ( size_mod8 < size ) + size_mod8 += 8; + + return size_mod8; +} + +/******************************************************************* +*******************************************************************/ + +static uint32 vk_record_data_size( REGF_VK_REC *vk ) +{ + uint32 size, size_mod8; + + size_mod8 = 0; + + /* the record size is sizeof(hdr) + name + static members + data_size_field */ + + size = REC_HDR_SIZE + (sizeof(uint16)*3) + (sizeof(uint32)*3) + sizeof(uint32); + + if ( vk->valuename ) + size += strlen(vk->valuename); + + /* multiple of 8 */ + size_mod8 = size & 0xfffffff8; + if ( size_mod8 < size ) + size_mod8 += 8; + + return size_mod8; +} + +/******************************************************************* +*******************************************************************/ + +static uint32 lf_record_data_size( uint32 num_keys ) +{ + uint32 size, size_mod8; + + size_mod8 = 0; + + /* the record size is sizeof(hdr) + num_keys + sizeof of hash_array + data_size_uint32 */ + + size = REC_HDR_SIZE + sizeof(uint16) + (sizeof(REGF_HASH_REC) * num_keys) + sizeof(uint32); + + /* multiple of 8 */ + size_mod8 = size & 0xfffffff8; + if ( size_mod8 < size ) + size_mod8 += 8; + + return size_mod8; +} + +/******************************************************************* +*******************************************************************/ + +static uint32 nk_record_data_size( REGF_NK_REC *nk ) +{ + uint32 size, size_mod8; + + size_mod8 = 0; + + /* the record size is static + length_of_keyname + length_of_classname + data_size_uint32 */ + + size = 0x4c + strlen(nk->keyname) + sizeof(uint32); + + if ( nk->classname ) + size += strlen( nk->classname ); + + /* multiple of 8 */ + size_mod8 = size & 0xfffffff8; + if ( size_mod8 < size ) + size_mod8 += 8; + + return size_mod8; +} + +/******************************************************************* +*******************************************************************/ + +static BOOL create_vk_record( REGF_FILE *file, REGF_VK_REC *vk, REGISTRY_VALUE *value ) +{ + char *name = regval_name(value); + REGF_HBIN *data_hbin; + + ZERO_STRUCTP( vk ); + + memcpy( vk->header, "vk", REC_HDR_SIZE ); + + if ( name ) { + vk->valuename = talloc_strdup( file->mem_ctx, regval_name(value) ); + vk->flag = VK_FLAG_NAME_PRESENT; + } + + vk->data_size = regval_size( value ); + vk->type = regval_type( value ); + + if ( vk->data_size > sizeof(uint32) ) { + uint32 data_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8; + + vk->data = TALLOC_MEMDUP( file->mem_ctx, regval_data_p(value), vk->data_size ); + + /* go ahead and store the offset....we'll pick this hbin block back up when + we stream the data */ + + data_hbin = find_free_space(file, data_size ); + vk->data_off = prs_offset( &data_hbin->ps ) + data_hbin->first_hbin_off - HBIN_HDR_SIZE; + } + else { + vk->data_size |= VK_DATA_IN_OFFSET; + memcpy( &vk->data_off, regval_data_p(value), sizeof(uint32) ); + } + + return True; +} + +/******************************************************************* +*******************************************************************/ + +static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 ) +{ + return StrnCaseCmp( h1->keycheck, h2->keycheck, sizeof(uint32) ); +} + +/******************************************************************* +*******************************************************************/ + + REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name, + REGVAL_CTR *values, REGSUBKEY_CTR *subkeys, + SEC_DESC *sec_desc, REGF_NK_REC *parent ) +{ + REGF_NK_REC *nk; + REGF_HBIN *vlist_hbin; + uint32 size; + + if ( !(nk = TALLOC_ZERO_P( file->mem_ctx, REGF_NK_REC )) ) + return NULL; + + memcpy( nk->header, "nk", REC_HDR_SIZE ); + + if ( !parent ) + nk->key_type = NK_TYPE_ROOTKEY; + else + nk->key_type = NK_TYPE_NORMALKEY; + + /* store the parent offset (or -1 if a the root key */ + + nk->parent_off = parent ? (parent->hbin_off + parent->hbin->file_off - REGF_BLOCKSIZE - HBIN_HDR_SIZE ) : REGF_OFFSET_NONE; + + /* no classname currently */ + + nk->classname_off = REGF_OFFSET_NONE; + nk->classname = NULL; + nk->keyname = talloc_strdup( file->mem_ctx, name ); + + /* current modification time */ + + unix_to_nt_time( &nk->mtime, time(NULL) ); + + /* allocate the record on disk */ + + size = nk_record_data_size( nk ); + nk->rec_size = ( size - 1 ) ^ 0XFFFFFFFF; + nk->hbin = find_free_space( file, size ); + nk->hbin_off = prs_offset( &nk->hbin->ps ); + + /* Update the hash record in the parent */ + + if ( parent ) { + REGF_HASH_REC *hash = &parent->subkeys.hashes[parent->subkey_index]; + + hash->nk_off = prs_offset( &nk->hbin->ps ) + nk->hbin->first_hbin_off - HBIN_HDR_SIZE; + memcpy( hash->keycheck, name, sizeof(uint32) ); + parent->subkey_index++; + + /* sort the list by keyname */ + + qsort( parent->subkeys.hashes, parent->subkey_index, sizeof(REGF_HASH_REC), QSORT_CAST hashrec_cmp ); + + if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) ) + return False; + } + + /* write the security descriptor */ + + nk->sk_off = REGF_OFFSET_NONE; + if ( sec_desc ) { + uint32 sk_size = sk_record_data_size( sec_desc ); + REGF_HBIN *sk_hbin; + REGF_SK_REC *tmp = NULL; + + /* search for it in the existing list of sd's */ + + if ( (nk->sec_desc = find_sk_record_by_sec_desc( file, sec_desc )) == NULL ) { + /* not found so add it to the list */ + + sk_hbin = find_free_space( file, sk_size ); + + if ( !(nk->sec_desc = TALLOC_ZERO_P( file->mem_ctx, REGF_SK_REC )) ) + return NULL; + + /* now we have to store the security descriptor in the list and + update the offsets */ + + memcpy( nk->sec_desc->header, "sk", REC_HDR_SIZE ); + nk->sec_desc->hbin = sk_hbin; + nk->sec_desc->hbin_off = prs_offset( &sk_hbin->ps ); + nk->sec_desc->sk_off = prs_offset( &sk_hbin->ps ) + sk_hbin->first_hbin_off - HBIN_HDR_SIZE; + nk->sec_desc->rec_size = (sk_size-1) ^ 0xFFFFFFFF; + + nk->sec_desc->sec_desc = sec_desc; + nk->sec_desc->ref_count = 0; + + /* size value must be self-inclusive */ + nk->sec_desc->size = sec_desc_size(sec_desc) + sizeof(uint32); + + DLIST_ADD_END( file->sec_desc_list, nk->sec_desc, tmp ); + + /* initialize offsets */ + + nk->sec_desc->prev_sk_off = nk->sec_desc->sk_off; + nk->sec_desc->next_sk_off = nk->sec_desc->sk_off; + + /* now update the offsets for us and the previous sd in the list */ + + if ( nk->sec_desc->prev ) { + REGF_SK_REC *prev = nk->sec_desc->prev; + + nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE; + prev->next_sk_off = nk->sk_off; + } + } + + /* dump the reference count */ + + nk->sk_off = nk->sec_desc->sk_off; + nk->sec_desc->ref_count++; + } + + /* write the subkeys */ + + nk->subkeys_off = REGF_OFFSET_NONE; + if ( (nk->num_subkeys = regsubkey_ctr_numkeys( subkeys )) != 0 ) { + uint32 lf_size = lf_record_data_size( nk->num_subkeys ); + uint32 namelen; + int i; + + nk->subkeys.hbin = find_free_space( file, lf_size ); + nk->subkeys.hbin_off = prs_offset( &nk->subkeys.hbin->ps ); + nk->subkeys.rec_size = (lf_size-1) ^ 0xFFFFFFFF; + nk->subkeys_off = prs_offset( &nk->subkeys.hbin->ps ) + nk->subkeys.hbin->first_hbin_off - HBIN_HDR_SIZE; + + memcpy( nk->subkeys.header, "lf", REC_HDR_SIZE ); + + nk->subkeys.num_keys = nk->num_subkeys; + if ( !(nk->subkeys.hashes = TALLOC_ZERO_ARRAY( file->mem_ctx, REGF_HASH_REC, nk->subkeys.num_keys )) ) + return NULL; + nk->subkey_index = 0; + + /* update the max_bytes_subkey{name,classname} fields */ + for ( i=0; i<nk->num_subkeys; i++ ) { + namelen = strlen( regsubkey_ctr_specific_key(subkeys, i) ); + if ( namelen*2 > nk->max_bytes_subkeyname ) + nk->max_bytes_subkeyname = namelen * 2; + } + } + + /* write the values */ + + nk->values_off = REGF_OFFSET_NONE; + if ( (nk->num_values = regval_ctr_numvals( values )) != 0 ) { + uint32 vlist_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8; + int i; + + vlist_hbin = find_free_space( file, vlist_size ); + nk->values_off = prs_offset( &vlist_hbin->ps ) + vlist_hbin->first_hbin_off - HBIN_HDR_SIZE; + + if ( !(nk->values = TALLOC_ARRAY( file->mem_ctx, REGF_VK_REC, nk->num_values )) ) + return NULL; + + /* create the vk records */ + + for ( i=0; i<nk->num_values; i++ ) { + uint32 vk_size, namelen, datalen; + REGISTRY_VALUE *r; + + r = regval_ctr_specific_value( values, i ); + create_vk_record( file, &nk->values[i], r ); + vk_size = vk_record_data_size( &nk->values[i] ); + nk->values[i].hbin = find_free_space( file, vk_size ); + nk->values[i].hbin_off = prs_offset( &nk->values[i].hbin->ps ); + nk->values[i].rec_size = ( vk_size - 1 ) ^ 0xFFFFFFFF; + nk->values[i].rec_off = prs_offset( &nk->values[i].hbin->ps ) + + nk->values[i].hbin->first_hbin_off + - HBIN_HDR_SIZE; + + /* update the max bytes fields if necessary */ + + namelen = strlen( regval_name(r) ); + if ( namelen*2 > nk->max_bytes_valuename ) + nk->max_bytes_valuename = namelen * 2; + + datalen = regval_size( r ); + if ( datalen*2 > nk->max_bytes_value ) + nk->max_bytes_value = datalen * 2; + } + } + + /* stream the records */ + + prs_set_offset( &nk->hbin->ps, nk->hbin_off ); + if ( !prs_nk_rec( "nk_rec", &nk->hbin->ps, 0, nk ) ) + return False; + + if ( nk->num_values ) { + if ( !hbin_prs_vk_records( "vk_records", vlist_hbin, 0, nk, file ) ) + return False; + } + + + regfio_flush( file ); + + return nk; +} + |