From: Paul Gofman pgofman@codeweavers.com
--- dlls/ntdll/unix/registry.c | 290 ++++++++++++++++++++++++++++++++++++- server/protocol.def | 14 +- server/registry.c | 124 +++++++++++++--- 3 files changed, 399 insertions(+), 29 deletions(-)
diff --git a/dlls/ntdll/unix/registry.c b/dlls/ntdll/unix/registry.c index a24d6e7c267..b63c7a66925 100644 --- a/dlls/ntdll/unix/registry.c +++ b/dlls/ntdll/unix/registry.c @@ -27,6 +27,8 @@
#include <stdarg.h> #include <string.h> +#include <unistd.h> +#include <errno.h>
#include "ntstatus.h" #define WIN32_NO_STATUS @@ -68,6 +70,248 @@ NTSTATUS open_hkcu_key( const char *path, HANDLE *key ) return NtCreateKey( key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ); }
+/* dump a Unicode string with proper escaping */ +int dump_strW( const WCHAR *str, data_size_t len, FILE *f, const char escape[2] ) +{ + static const char escapes[32] = ".......abtnvfr.............e...."; + char buffer[256]; + char *pos = buffer; + int count = 0; + + for (len /= sizeof(WCHAR); len; str++, len--) + { + if (pos > buffer + sizeof(buffer) - 8) + { + fwrite( buffer, pos - buffer, 1, f ); + count += pos - buffer; + pos = buffer; + } + if (*str > 127) /* hex escape */ + { + if (len > 1 && str[1] < 128 && isxdigit( (char)str[1] )) + pos += sprintf( pos, "\x%04x", *str ); + else + pos += sprintf( pos, "\x%x", *str ); + continue; + } + if (*str < 32) /* octal or C escape */ + { + if (!*str && len == 1) continue; /* do not output terminating NULL */ + if (escapes[*str] != '.') + pos += sprintf( pos, "\%c", escapes[*str] ); + else if (len > 1 && str[1] >= '0' && str[1] <= '7') + pos += sprintf( pos, "\%03o", *str ); + else + pos += sprintf( pos, "\%o", *str ); + continue; + } + if (*str == '\' || *str == escape[0] || *str == escape[1]) *pos++ = '\'; + *pos++ = *str; + } + fwrite( buffer, pos - buffer, 1, f ); + count += pos - buffer; + return count; +} + +struct saved_key +{ + data_size_t namelen; + WCHAR *name; + data_size_t classlen; + WCHAR *class; + int value_count; + int subkey_count; + unsigned int is_symlink; + timeout_t modif; + struct saved_key *parent; +}; + +/* read serialized key data */ +static char *fill_saved_key( struct saved_key *key, struct saved_key *parent, char *data ) +{ + key->parent = parent; + key->namelen = *(data_size_t *)data; + data += sizeof(data_size_t); + key->name = (WCHAR *)data; + data += key->namelen; + key->classlen = *(data_size_t *)data; + data += sizeof(data_size_t); + key->class = (WCHAR *)data; + data += key->classlen; + key->value_count = *(int *)data; + data += sizeof(int); + key->subkey_count = *(int *)data; + data += sizeof(int); + key->is_symlink = *(unsigned int *)data; + data += sizeof(unsigned int); + key->modif = *(timeout_t *)data; + data += sizeof(timeout_t); + + return data; +} + +/* dump serialized key full path */ +static char *dump_parents( char *data, FILE *f, int count ) +{ + data_size_t len; + WCHAR *name; + + len = *(data_size_t *)data; + data += sizeof(data_size_t); + name = (WCHAR *)data; + data += len; + + if (count > 1) + { + data = dump_parents( data, f, count - 1); + fprintf( f, "\\" ); + } + dump_strW( name, len, f, "[]" ); + return data; +} + +/* dump the full path of a key */ +static void dump_path( const struct saved_key *key, const struct saved_key *base, FILE *f ) +{ + if (key->parent && key->parent != base) + { + dump_path( key->parent, base, f ); + fprintf( f, "\\" ); + } + dump_strW( key->name, key->namelen, f, "[]" ); +} + +/* dump a value to a text file */ +static char *dump_value( char *data, FILE *f ) +{ + unsigned int i, dw; + int count; + data_size_t namelen, valuelen; + char *valuedata; + WCHAR *name; + unsigned int type; + + namelen = *(data_size_t *)data; + data += sizeof(data_size_t); + name = (WCHAR *)data; + data += namelen; + type = *(unsigned int *)data; + data += sizeof(unsigned int); + valuelen = *(data_size_t *)data; + data += sizeof(data_size_t); + valuedata = data; + data += valuelen; + + if (namelen) + { + fputc( '"', f ); + count = 1 + dump_strW( name, namelen, f, """" ); + count += fprintf( f, ""=" ); + } + else count = fprintf( f, "@=" ); + + switch(type) + { + case REG_SZ: + case REG_EXPAND_SZ: + case REG_MULTI_SZ: + /* only output properly terminated strings in string format */ + if (valuelen < sizeof(WCHAR)) break; + if (valuelen % sizeof(WCHAR)) break; + if (((WCHAR *)valuedata)[valuelen / sizeof(WCHAR) - 1]) break; + if (type != REG_SZ) fprintf( f, "str(%x):", type ); + fputc( '"', f ); + dump_strW( (WCHAR *)valuedata, valuelen, f, """" ); + fprintf( f, ""\n" ); + return data; + + case REG_DWORD: + if (valuelen != sizeof(dw)) break; + memcpy( &dw, valuedata, sizeof(dw) ); + fprintf( f, "dword:%08x\n", dw ); + return data; + } + + if (type == REG_BINARY) count += fprintf( f, "hex:" ); + else count += fprintf( f, "hex(%x):", type ); + for (i = 0; i < valuelen; i++) + { + count += fprintf( f, "%02x", *((unsigned char *)valuedata + i) ); + if (i < valuelen-1) + { + fputc( ',', f ); + if (++count > 76) + { + fprintf( f, "\\n " ); + count = 2; + } + } + } + fputc( '\n', f ); + return data; +} + +/* save a registry key and all its subkeys to a text file */ +static char *save_subkeys( char *data, struct saved_key *parent, struct saved_key *base, FILE *f ) +{ + struct saved_key key; + int i; + + if (!base) base = &key; + data = fill_saved_key( &key, parent, data ); + + /* save key if it has either some values or no subkeys, or needs special options */ + /* keys with no values but subkeys are saved implicitly by saving the subkeys */ + if ((key.value_count > 0) || !key.subkey_count || key.classlen || key.is_symlink) + { + fprintf( f, "\n[" ); + if (parent) dump_path( &key, base, f ); + fprintf( f, "] %u\n", (unsigned int)((key.modif - SECS_1601_TO_1970 * TICKSPERSEC) / TICKSPERSEC) ); + fprintf( f, "#time=%x%08x\n", (unsigned int)(key.modif >> 32), (unsigned int)key.modif ); + if (key.classlen) + { + fprintf( f, "#class="" ); + dump_strW( key.class, key.classlen, f, """" ); + fprintf( f, ""\n" ); + } + if (key.is_symlink) fputs( "#link\n", f ); + for (i = 0; i < key.value_count; i++) data = dump_value( data, f ); + } + for (i = 0; i < key.subkey_count; i++) data = save_subkeys( data, &key, base, f ); + return data; +} + +/* save a registry branch to a file */ +static void save_all_subkeys( char *data, FILE *f ) +{ + enum prefix_type prefix_type; + int parent_count; + + prefix_type = *(int *)data; + data += sizeof(int); + + parent_count = *(int *)data; + data += sizeof(int); + + fprintf( f, "WINE REGISTRY Version 2\n" ); + fprintf( f, ";; All keys relative to " ); + data = dump_parents( data, f, parent_count ); + fprintf( f, "\n" ); + + switch (prefix_type) + { + case PREFIX_32BIT: + fprintf( f, "\n#arch=win32\n" ); + break; + case PREFIX_64BIT: + fprintf( f, "\n#arch=win64\n" ); + break; + default: + break; + } + save_subkeys( data, NULL, NULL, f ); +} +
/****************************************************************************** * NtCreateKey (NTDLL.@) @@ -776,17 +1020,53 @@ NTSTATUS WINAPI NtUnloadKey( OBJECT_ATTRIBUTES *attr ) */ NTSTATUS WINAPI NtSaveKey( HANDLE key, HANDLE file ) { + data_size_t size = 0; unsigned int ret; + char *data = NULL; + int fd, fd2, needs_close = 0; + FILE *f;
TRACE( "(%p,%p)\n", key, file );
- SERVER_START_REQ( save_registry ) + while (1) { - req->hkey = wine_server_obj_handle( key ); - req->file = wine_server_obj_handle( file ); - ret = wine_server_call( req ); + SERVER_START_REQ( save_registry ) + { + req->hkey = wine_server_obj_handle( key ); + if (size) wine_server_set_reply( req, data, size ); + ret = wine_server_call( req ); + size = reply->total; + } + SERVER_END_REQ; + + if (!ret) break; + free( data ); + if (ret != STATUS_BUFFER_TOO_SMALL) return ret; + if (!(data = malloc( size ))) + { + ERR( "No memory.\n" ); + return STATUS_NO_MEMORY; + } } - SERVER_END_REQ; + + if ((ret = server_get_unix_fd( file, FILE_WRITE_DATA, &fd, &needs_close, NULL, NULL ))) goto done; + if ((fd2 = dup( fd )) == -1) + { + ret = errno_to_status( errno ); + goto done; + } + if (!(f = fdopen( fd2, "w" ))) + { + close( fd2 ); + ret = errno_to_status( errno ); + goto done; + } + save_all_subkeys( data, f ); + if (fclose(f)) ret = errno_to_status( errno ); + +done: + if (needs_close) close( fd ); + free( data ); return ret; }
diff --git a/server/protocol.def b/server/protocol.def index 54059b6b580..2e020984a4d 100644 --- a/server/protocol.def +++ b/server/protocol.def @@ -1897,11 +1897,19 @@ struct process_info @END
-/* Save a registry branch to a file */ +/* Return full registry branch non-volatile data for saving */ @REQ(save_registry) - obj_handle_t hkey; /* key to save */ - obj_handle_t file; /* file to save to */ + obj_handle_t hkey; /* key to save */ +@REPLY + data_size_t total; /* total length needed for data */ + VARARG(data,bytes); /* registry data */ @END +enum prefix_type +{ + PREFIX_UNKNOWN, + PREFIX_32BIT, + PREFIX_64BIT, +};
/* Add a registry key change notification */ diff --git a/server/registry.c b/server/registry.c index 3fea7dd79a4..ba87e7c79c4 100644 --- a/server/registry.c +++ b/server/registry.c @@ -124,7 +124,7 @@ static struct key *root_key; static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC; static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */ static struct timeout_user *save_timeout_user; /* saving timer */ -static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type; +static enum prefix_type prefix_type;
static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'}; static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'}; @@ -2021,29 +2021,104 @@ static void save_all_subkeys( struct key *key, FILE *f ) save_subkeys( key, key, f ); }
-/* save a registry branch to a file handle */ -static void save_registry( struct key *key, obj_handle_t handle ) +static data_size_t serialize_value( const struct key_value *value, char *buf ) { - struct file *file; - int fd; + data_size_t size;
- if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return; - fd = dup( get_file_unix_fd( file ) ); - release_object( file ); - if (fd != -1) + size = sizeof(data_size_t) + value->namelen + sizeof(unsigned int) + sizeof(data_size_t) + value->len; + if (!buf) return size; + + *(data_size_t *)buf = value->namelen; + buf += sizeof(data_size_t); + memcpy( buf, value->name, value->namelen ); + buf += value->namelen; + + *(unsigned int *)buf = value->type; + buf += sizeof(unsigned int); + + *(data_size_t *)buf = value->len; + buf += sizeof(data_size_t); + memcpy( buf, value->data, value->len ); + + return size; +} + +/* save a registry key with subkeys to a buffer */ +static data_size_t serialize_key( const struct key *key, char *buf ) +{ + data_size_t size; + int subkey_count, i; + + if (key->flags & KEY_VOLATILE) return 0; + + size = sizeof(data_size_t) + key->obj.name->len + sizeof(data_size_t) + key->classlen + sizeof(int) + sizeof(int) + + sizeof(unsigned int) + sizeof(timeout_t); + for (i = 0; i <= key->last_value; i++) + size += serialize_value( &key->values[i], buf ? buf + size : NULL ); + subkey_count = 0; + for (i = 0; i <= key->last_subkey; i++) { - FILE *f = fdopen( fd, "w" ); - if (f) - { - save_all_subkeys( key, f ); - if (fclose( f )) file_set_error(); - } - else - { - file_set_error(); - close( fd ); - } + if (key->subkeys[i]->flags & KEY_VOLATILE) continue; + size += serialize_key( key->subkeys[i], buf ? buf + size : NULL ); + ++subkey_count; + } + if (!buf) return size; + + *(data_size_t *)buf = key->obj.name->len; + buf += sizeof(data_size_t); + memcpy( buf, key->obj.name->name, key->obj.name->len ); + buf += key->obj.name->len; + + *(data_size_t *)buf = key->classlen; + buf += sizeof(data_size_t); + memcpy( buf, key->class, key->classlen ); + buf += key->classlen; + + *(int *)buf = key->last_value + 1; + buf += sizeof(int); + + *(int *)buf = subkey_count; + buf += sizeof(int); + + *(unsigned int *)buf = key->flags & KEY_SYMLINK; + buf += sizeof(unsigned int); + + *(timeout_t *)buf = key->modif; + + return size; +} + +/* save registry branch to buffer */ +static data_size_t save_registry( const struct key *key, char *buf ) +{ + int *parent_count = NULL; + const struct key *parent; + data_size_t size; + + size = sizeof(int) + sizeof(int); + if (buf) + { + *(int *)buf = prefix_type; + buf += sizeof(int); + parent_count = (int *)buf; + buf += sizeof(int); + *parent_count = 0; } + + parent = key; + do + { + size += sizeof(data_size_t) + parent->obj.name->len; + if (!buf) continue; + ++*parent_count; + *(data_size_t *)buf = parent->obj.name->len; + buf += sizeof(data_size_t); + memcpy( buf, parent->obj.name->name, parent->obj.name->len ); + buf += parent->obj.name->len; + } while ((parent = get_parent( parent ))); + + size += serialize_key( key, buf ); + return size; }
/* save a registry branch to a file */ @@ -2370,6 +2445,7 @@ DECL_HANDLER(unload_registry) DECL_HANDLER(save_registry) { struct key *key; + char *data;
if (!thread_single_check_privilege( current, SeBackupPrivilege )) { @@ -2379,7 +2455,13 @@ DECL_HANDLER(save_registry)
if ((key = get_hkey_obj( req->hkey, 0 ))) { - save_registry( key, req->file ); + reply->total = save_registry( key, NULL ); + if (reply->total <= get_reply_max_size()) + { + if ((data = set_reply_data_size( reply->total ))) + save_registry( key, data ); + } + else set_error( STATUS_BUFFER_TOO_SMALL ); release_object( key ); } }