1999-11-23 20:39:11 +01:00
|
|
|
/*
|
|
|
|
* Server-side registry management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1999-11-23 20:39:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* To do:
|
|
|
|
* - symbolic links
|
|
|
|
*/
|
|
|
|
|
2002-04-26 21:05:15 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
#include <assert.h>
|
2000-07-25 23:01:59 +02:00
|
|
|
#include <ctype.h>
|
2000-04-16 19:21:13 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1999-11-23 20:39:11 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
2003-09-07 07:08:14 +02:00
|
|
|
#include <stdarg.h>
|
2000-04-16 19:21:13 +02:00
|
|
|
#include <string.h>
|
1999-11-23 20:39:11 +01:00
|
|
|
#include <stdlib.h>
|
2000-04-16 19:21:13 +02:00
|
|
|
#include <sys/stat.h>
|
1999-11-23 20:39:11 +01:00
|
|
|
#include <unistd.h>
|
2003-01-30 01:26:44 +01:00
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
#include "object.h"
|
2003-01-30 01:26:44 +01:00
|
|
|
#include "file.h"
|
1999-11-23 20:39:11 +01:00
|
|
|
#include "handle.h"
|
|
|
|
#include "request.h"
|
|
|
|
#include "unicode.h"
|
2005-05-05 18:57:55 +02:00
|
|
|
#include "security.h"
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-06-21 01:18:56 +02:00
|
|
|
#include "wine/library.h"
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2002-11-25 03:47:32 +01:00
|
|
|
struct notify
|
|
|
|
{
|
2005-02-24 20:43:53 +01:00
|
|
|
struct list entry; /* entry in list of notifications */
|
2002-11-25 03:47:32 +01:00
|
|
|
struct event *event; /* event to set when changing this key */
|
|
|
|
int subtree; /* true if subtree notification */
|
|
|
|
unsigned int filter; /* which events to notify on */
|
|
|
|
obj_handle_t hkey; /* hkey associated with this notification */
|
2005-06-09 22:36:08 +02:00
|
|
|
struct process *process; /* process in which the hkey is valid */
|
2002-11-25 03:47:32 +01:00
|
|
|
};
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
/* a registry key */
|
|
|
|
struct key
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
WCHAR *name; /* key name */
|
|
|
|
WCHAR *class; /* key class */
|
2005-11-22 13:05:36 +01:00
|
|
|
unsigned short namelen; /* length of key name */
|
|
|
|
unsigned short classlen; /* length of class name */
|
1999-11-23 20:39:11 +01:00
|
|
|
struct key *parent; /* parent key */
|
|
|
|
int last_subkey; /* last in use subkey */
|
|
|
|
int nb_subkeys; /* count of allocated subkeys */
|
|
|
|
struct key **subkeys; /* subkeys array */
|
|
|
|
int last_value; /* last in use value */
|
|
|
|
int nb_values; /* count of allocated values in array */
|
|
|
|
struct key_value *values; /* values array */
|
2005-03-14 12:00:43 +01:00
|
|
|
unsigned int flags; /* flags */
|
1999-11-23 20:39:11 +01:00
|
|
|
time_t modif; /* last modification time */
|
2005-02-24 20:43:53 +01:00
|
|
|
struct list notify_list; /* list of notifications */
|
1999-11-23 20:39:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* key flags */
|
|
|
|
#define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
|
|
|
|
#define KEY_DELETED 0x0002 /* key has been deleted */
|
2002-06-21 01:18:56 +02:00
|
|
|
#define KEY_DIRTY 0x0004 /* key has been modified */
|
1999-11-23 20:39:11 +01:00
|
|
|
|
|
|
|
/* a key value */
|
|
|
|
struct key_value
|
|
|
|
{
|
|
|
|
WCHAR *name; /* value name */
|
2005-11-22 13:05:36 +01:00
|
|
|
unsigned short namelen; /* length of value name */
|
|
|
|
unsigned short type; /* value type */
|
1999-11-23 20:39:11 +01:00
|
|
|
size_t len; /* value data length in bytes */
|
|
|
|
void *data; /* pointer to value data */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
|
|
|
|
#define MIN_VALUES 8 /* min. number of allocated values per key */
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
#define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
|
|
|
|
#define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2004-05-07 01:42:04 +02:00
|
|
|
/* the root of the registry tree */
|
2000-08-26 06:40:07 +02:00
|
|
|
static struct key *root_key;
|
|
|
|
|
2005-03-14 12:00:43 +01:00
|
|
|
static const int save_period = 30000; /* delay between periodic saves (in ms) */
|
2000-04-16 19:21:13 +02:00
|
|
|
static struct timeout_user *save_timeout_user; /* saving timer */
|
|
|
|
|
2005-03-14 12:00:43 +01:00
|
|
|
static void set_periodic_save_timer(void);
|
|
|
|
|
2000-04-16 19:21:13 +02:00
|
|
|
/* information about where to save a registry branch */
|
|
|
|
struct save_branch_info
|
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
char *path;
|
|
|
|
};
|
|
|
|
|
2004-05-07 06:13:21 +02:00
|
|
|
#define MAX_SAVE_BRANCH_INFO 3
|
2000-04-16 19:21:13 +02:00
|
|
|
static int save_branch_count;
|
|
|
|
static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
|
1999-11-24 05:19:43 +01:00
|
|
|
/* information about a file being loaded */
|
|
|
|
struct file_load_info
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
const char *filename; /* input file name */
|
|
|
|
FILE *file; /* input file */
|
|
|
|
char *buffer; /* line buffer */
|
|
|
|
int len; /* buffer length */
|
|
|
|
int line; /* current input line */
|
|
|
|
WCHAR *tmp; /* temp buffer to use while parsing input */
|
|
|
|
size_t tmplen; /* length of temp buffer */
|
1999-11-24 05:19:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
static void key_dump( struct object *obj, int verbose );
|
2005-06-09 17:39:52 +02:00
|
|
|
static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
|
1999-11-23 20:39:11 +01:00
|
|
|
static void key_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops key_ops =
|
|
|
|
{
|
2000-01-01 01:56:27 +01:00
|
|
|
sizeof(struct key), /* size */
|
|
|
|
key_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2000-12-19 03:12:45 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2005-06-09 17:39:52 +02:00
|
|
|
key_close_handle, /* close_handle */
|
2000-01-01 01:56:27 +01:00
|
|
|
key_destroy /* destroy */
|
1999-11-23 20:39:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The registry text file format v2 used by this code is similar to the one
|
|
|
|
* used by REGEDIT import/export functionality, with the following differences:
|
|
|
|
* - strings and key names can contain \x escapes for Unicode
|
|
|
|
* - key names use escapes too in order to support Unicode
|
|
|
|
* - the modification time optionally follows the key name
|
|
|
|
* - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline char to_hex( char ch )
|
|
|
|
{
|
|
|
|
if (isdigit(ch)) return ch - '0';
|
|
|
|
return tolower(ch) - 'a' + 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dump the full path of a key */
|
2002-06-21 01:18:56 +02:00
|
|
|
static void dump_path( const struct key *key, const struct key *base, FILE *f )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-08-26 06:40:07 +02:00
|
|
|
if (key->parent && key->parent != base)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-01-09 22:07:01 +01:00
|
|
|
dump_path( key->parent, base, f );
|
1999-11-23 20:39:11 +01:00
|
|
|
fprintf( f, "\\\\" );
|
|
|
|
}
|
2005-11-22 13:05:36 +01:00
|
|
|
dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* dump a value to a text file */
|
2002-06-21 01:18:56 +02:00
|
|
|
static void dump_value( const struct key_value *value, FILE *f )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2004-09-22 04:46:38 +02:00
|
|
|
unsigned int i;
|
|
|
|
int count;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (value->namelen)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
fputc( '\"', f );
|
2005-11-22 13:05:36 +01:00
|
|
|
count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
|
1999-11-23 20:39:11 +01:00
|
|
|
count += fprintf( f, "\"=" );
|
|
|
|
}
|
|
|
|
else count = fprintf( f, "@=" );
|
|
|
|
|
|
|
|
switch(value->type)
|
|
|
|
{
|
|
|
|
case REG_SZ:
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
|
|
|
|
fputc( '\"', f );
|
|
|
|
if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
|
|
|
|
fputc( '\"', f );
|
|
|
|
break;
|
|
|
|
case REG_DWORD:
|
|
|
|
if (value->len == sizeof(DWORD))
|
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
memcpy( &dw, value->data, sizeof(DWORD) );
|
|
|
|
fprintf( f, "dword:%08lx", dw );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* else fall through */
|
|
|
|
default:
|
|
|
|
if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
|
|
|
|
else count += fprintf( f, "hex(%x):", value->type );
|
|
|
|
for (i = 0; i < value->len; i++)
|
|
|
|
{
|
|
|
|
count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
|
|
|
|
if (i < value->len-1)
|
|
|
|
{
|
|
|
|
fputc( ',', f );
|
|
|
|
if (++count > 76)
|
|
|
|
{
|
|
|
|
fprintf( f, "\\\n " );
|
|
|
|
count = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fputc( '\n', f );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save a registry and all its subkeys to a text file */
|
2002-06-21 01:18:56 +02:00
|
|
|
static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (key->flags & KEY_VOLATILE) return;
|
2005-03-14 12:00:43 +01:00
|
|
|
/* save key if it has either some values or no subkeys */
|
1999-11-23 20:39:11 +01:00
|
|
|
/* keys with no values but subkeys are saved implicitly by saving the subkeys */
|
2005-03-14 12:00:43 +01:00
|
|
|
if ((key->last_value >= 0) || (key->last_subkey == -1))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
fprintf( f, "\n[" );
|
2000-08-26 06:40:07 +02:00
|
|
|
if (key != base) dump_path( key, base, f );
|
2003-10-01 05:32:16 +02:00
|
|
|
fprintf( f, "] %ld\n", (long)key->modif );
|
1999-11-23 20:39:11 +01:00
|
|
|
for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
|
|
|
|
}
|
2000-01-09 22:07:01 +01:00
|
|
|
for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
2002-06-21 01:18:56 +02:00
|
|
|
static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
fprintf( stderr, "%s key ", op );
|
2000-01-09 22:07:01 +01:00
|
|
|
if (key) dump_path( key, NULL, stderr );
|
1999-11-23 20:39:11 +01:00
|
|
|
else fprintf( stderr, "ERROR" );
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
fprintf( stderr, " value ");
|
|
|
|
dump_value( value, stderr );
|
|
|
|
}
|
|
|
|
else fprintf( stderr, "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void key_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct key *key = (struct key *)obj;
|
|
|
|
assert( obj->ops == &key_ops );
|
|
|
|
fprintf( stderr, "Key flags=%x ", key->flags );
|
2000-01-09 22:07:01 +01:00
|
|
|
dump_path( key, NULL, stderr );
|
1999-11-23 20:39:11 +01:00
|
|
|
fprintf( stderr, "\n" );
|
|
|
|
}
|
|
|
|
|
2002-11-25 03:47:32 +01:00
|
|
|
/* notify waiter and maybe delete the notification */
|
|
|
|
static void do_notification( struct key *key, struct notify *notify, int del )
|
|
|
|
{
|
2005-06-10 21:54:46 +02:00
|
|
|
if (notify->event)
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
|
|
|
set_event( notify->event );
|
|
|
|
release_object( notify->event );
|
|
|
|
notify->event = NULL;
|
|
|
|
}
|
2005-02-24 20:43:53 +01:00
|
|
|
if (del)
|
|
|
|
{
|
|
|
|
list_remove( ¬ify->entry );
|
|
|
|
free( notify );
|
|
|
|
}
|
2002-11-25 03:47:32 +01:00
|
|
|
}
|
|
|
|
|
2005-06-09 22:36:08 +02:00
|
|
|
static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
2005-02-24 20:43:53 +01:00
|
|
|
struct notify *notify;
|
2002-11-25 03:47:32 +01:00
|
|
|
|
2005-02-24 20:43:53 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
|
|
|
|
{
|
2005-06-09 22:36:08 +02:00
|
|
|
if (notify->process == process && notify->hkey == hkey) return notify;
|
2005-02-24 20:43:53 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
2002-11-25 03:47:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close the notification associated with a handle */
|
2005-06-09 17:39:52 +02:00
|
|
|
static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
|
|
|
struct key * key = (struct key *) obj;
|
2005-06-09 22:36:08 +02:00
|
|
|
struct notify *notify = find_notify( key, process, handle );
|
2005-06-09 17:39:52 +02:00
|
|
|
if (notify) do_notification( key, notify, 1 );
|
|
|
|
return 1; /* ok to close */
|
2002-11-25 03:47:32 +01:00
|
|
|
}
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
static void key_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
int i;
|
2005-02-24 20:43:53 +01:00
|
|
|
struct list *ptr;
|
1999-11-23 20:39:11 +01:00
|
|
|
struct key *key = (struct key *)obj;
|
|
|
|
assert( obj->ops == &key_ops );
|
|
|
|
|
2000-05-15 00:57:57 +02:00
|
|
|
if (key->name) free( key->name );
|
1999-11-23 20:39:11 +01:00
|
|
|
if (key->class) free( key->class );
|
|
|
|
for (i = 0; i <= key->last_value; i++)
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
if (key->values[i].name) free( key->values[i].name );
|
1999-11-23 20:39:11 +01:00
|
|
|
if (key->values[i].data) free( key->values[i].data );
|
|
|
|
}
|
2005-05-23 11:53:06 +02:00
|
|
|
if (key->values) free( key->values );
|
1999-11-24 05:19:43 +01:00
|
|
|
for (i = 0; i <= key->last_subkey; i++)
|
|
|
|
{
|
|
|
|
key->subkeys[i]->parent = NULL;
|
|
|
|
release_object( key->subkeys[i] );
|
|
|
|
}
|
2005-05-23 11:53:06 +02:00
|
|
|
if (key->subkeys) free( key->subkeys );
|
2002-11-25 03:47:32 +01:00
|
|
|
/* unconditionally notify everything waiting on this key */
|
2005-02-24 20:43:53 +01:00
|
|
|
while ((ptr = list_head( &key->notify_list )))
|
|
|
|
{
|
|
|
|
struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
|
|
|
|
do_notification( key, notify, 1 );
|
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
/* get the request vararg as registry path */
|
|
|
|
inline static void get_req_path( struct unicode_str *str, int skip_root )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
str->str = get_req_data();
|
|
|
|
str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (skip_root && str->len >= sizeof(root_name) &&
|
|
|
|
!memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
|
2000-10-01 03:44:50 +02:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
str->str += sizeof(root_name)/sizeof(WCHAR);
|
|
|
|
str->len -= sizeof(root_name);
|
2000-10-01 03:44:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
/* return the next token in a given path */
|
2005-11-22 13:05:36 +01:00
|
|
|
/* token->str must point inside the path, or be NULL for the first call */
|
|
|
|
static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
size_t i = 0, len = path->len / sizeof(WCHAR);
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!token->str) /* first time */
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-10-01 03:44:50 +02:00
|
|
|
/* path cannot start with a backslash */
|
2005-11-22 13:05:36 +01:00
|
|
|
if (len && path->str[0] == '\\')
|
2000-10-01 03:44:50 +02:00
|
|
|
{
|
|
|
|
set_error( STATUS_OBJECT_PATH_INVALID );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2005-11-22 13:05:36 +01:00
|
|
|
else
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
i = token->str - path->str;
|
|
|
|
i += token->len / sizeof(WCHAR);
|
|
|
|
while (i < len && path->str[i] == '\\') i++;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2005-11-22 13:05:36 +01:00
|
|
|
token->str = path->str + i;
|
|
|
|
while (i < len && path->str[i] != '\\') i++;
|
|
|
|
token->len = (path->str + i - token->str) * sizeof(WCHAR);
|
|
|
|
return token;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a key object */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key *alloc_key( const struct unicode_str *name, time_t modif )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key *key;
|
2003-02-19 01:33:32 +01:00
|
|
|
if ((key = alloc_object( &key_ops )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
key->name = NULL;
|
1999-11-23 20:39:11 +01:00
|
|
|
key->class = NULL;
|
2005-11-22 13:05:36 +01:00
|
|
|
key->namelen = name->len;
|
|
|
|
key->classlen = 0;
|
1999-11-23 20:39:11 +01:00
|
|
|
key->flags = 0;
|
|
|
|
key->last_subkey = -1;
|
|
|
|
key->nb_subkeys = 0;
|
|
|
|
key->subkeys = NULL;
|
|
|
|
key->nb_values = 0;
|
|
|
|
key->last_value = -1;
|
|
|
|
key->values = NULL;
|
|
|
|
key->modif = modif;
|
|
|
|
key->parent = NULL;
|
2005-02-24 20:43:53 +01:00
|
|
|
list_init( &key->notify_list );
|
2005-11-22 13:05:36 +01:00
|
|
|
if (name->len && !(key->name = memdup( name->str, name->len )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
release_object( key );
|
|
|
|
key = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2002-06-21 01:18:56 +02:00
|
|
|
/* mark a key and all its parents as dirty (modified) */
|
|
|
|
static void make_dirty( struct key *key )
|
|
|
|
{
|
|
|
|
while (key)
|
|
|
|
{
|
|
|
|
if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
|
|
|
|
key->flags |= KEY_DIRTY;
|
|
|
|
key = key->parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark a key and all its subkeys as clean (not modified) */
|
|
|
|
static void make_clean( struct key *key )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (key->flags & KEY_VOLATILE) return;
|
|
|
|
if (!(key->flags & KEY_DIRTY)) return;
|
|
|
|
key->flags &= ~KEY_DIRTY;
|
|
|
|
for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
|
|
|
|
}
|
|
|
|
|
2002-11-25 03:47:32 +01:00
|
|
|
/* go through all the notifications and send them if necessary */
|
2005-05-25 20:41:09 +02:00
|
|
|
static void check_notify( struct key *key, unsigned int change, int not_subtree )
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
2005-02-24 20:43:53 +01:00
|
|
|
struct list *ptr, *next;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
2005-02-24 20:43:53 +01:00
|
|
|
struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
|
2002-11-25 03:47:32 +01:00
|
|
|
if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
|
|
|
|
do_notification( key, n, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
/* update key modification time */
|
2002-11-25 03:47:32 +01:00
|
|
|
static void touch_key( struct key *key, unsigned int change )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2002-11-25 03:47:32 +01:00
|
|
|
struct key *k;
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
key->modif = time(NULL);
|
2002-06-21 01:18:56 +02:00
|
|
|
make_dirty( key );
|
2002-11-25 03:47:32 +01:00
|
|
|
|
|
|
|
/* do notifications */
|
|
|
|
check_notify( key, change, 1 );
|
|
|
|
for ( k = key->parent; k; k = k->parent )
|
|
|
|
check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to grow the array of subkeys; return 1 if OK, 0 on error */
|
|
|
|
static int grow_subkeys( struct key *key )
|
|
|
|
{
|
|
|
|
struct key **new_subkeys;
|
|
|
|
int nb_subkeys;
|
|
|
|
|
|
|
|
if (key->nb_subkeys)
|
|
|
|
{
|
|
|
|
nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
|
|
|
|
if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_NO_MEMORY );
|
1999-11-23 20:39:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nb_subkeys = MIN_VALUES;
|
|
|
|
if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
|
|
|
|
}
|
|
|
|
key->subkeys = new_subkeys;
|
|
|
|
key->nb_subkeys = nb_subkeys;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a subkey for a given key, and return its index */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
|
|
|
|
int index, time_t modif )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
int i;
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NAME_TOO_LONG );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
if (parent->last_subkey + 1 == parent->nb_subkeys)
|
|
|
|
{
|
|
|
|
/* need to grow the array */
|
|
|
|
if (!grow_subkeys( parent )) return NULL;
|
|
|
|
}
|
|
|
|
if ((key = alloc_key( name, modif )) != NULL)
|
|
|
|
{
|
|
|
|
key->parent = parent;
|
|
|
|
for (i = ++parent->last_subkey; i > index; i--)
|
|
|
|
parent->subkeys[i] = parent->subkeys[i-1];
|
|
|
|
parent->subkeys[index] = key;
|
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free a subkey of a given key */
|
|
|
|
static void free_subkey( struct key *parent, int index )
|
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
int i, nb_subkeys;
|
|
|
|
|
|
|
|
assert( index >= 0 );
|
|
|
|
assert( index <= parent->last_subkey );
|
|
|
|
|
|
|
|
key = parent->subkeys[index];
|
|
|
|
for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
|
|
|
|
parent->last_subkey--;
|
|
|
|
key->flags |= KEY_DELETED;
|
|
|
|
key->parent = NULL;
|
|
|
|
release_object( key );
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
/* try to shrink the array */
|
2002-06-21 01:18:56 +02:00
|
|
|
nb_subkeys = parent->nb_subkeys;
|
|
|
|
if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key **new_subkeys;
|
|
|
|
nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
|
|
|
|
if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
|
2002-06-21 01:18:56 +02:00
|
|
|
if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
|
|
|
|
parent->subkeys = new_subkeys;
|
|
|
|
parent->nb_subkeys = nb_subkeys;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the named child of a given key and return its index */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
int i, min, max, res;
|
2005-11-22 13:05:36 +01:00
|
|
|
size_t len;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
|
|
|
min = 0;
|
|
|
|
max = key->last_subkey;
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
i = (min + max) / 2;
|
2005-11-22 13:05:36 +01:00
|
|
|
len = min( key->subkeys[i]->namelen, name->len );
|
|
|
|
res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
|
|
|
|
if (!res) res = key->subkeys[i]->namelen - name->len;
|
|
|
|
if (!res)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
*index = i;
|
|
|
|
return key->subkeys[i];
|
|
|
|
}
|
|
|
|
if (res > 0) max = i - 1;
|
|
|
|
else min = i + 1;
|
|
|
|
}
|
|
|
|
*index = min; /* this is where we should insert it */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open a subkey */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key *open_key( struct key *key, const struct unicode_str *name )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
int index;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str token;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
token.str = NULL;
|
|
|
|
if (!get_path_token( name, &token )) return NULL;
|
|
|
|
while (token.len)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!(key = find_subkey( key, &token, &index )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
|
1999-11-23 20:39:11 +01:00
|
|
|
break;
|
|
|
|
}
|
2005-11-22 13:05:36 +01:00
|
|
|
get_path_token( name, &token );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_level > 1) dump_operation( key, NULL, "Open" );
|
|
|
|
if (key) grab_object( key );
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a subkey */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key *create_key( struct key *key, const struct unicode_str *name,
|
|
|
|
const struct unicode_str *class, int flags, time_t modif, int *created )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key *base;
|
2005-11-22 13:05:36 +01:00
|
|
|
int index;
|
|
|
|
struct unicode_str token;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
|
|
|
if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_KEY_DELETED );
|
1999-11-23 20:39:11 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-21 01:18:56 +02:00
|
|
|
if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_CHILD_MUST_BE_VOLATILE );
|
1999-11-23 20:39:11 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-26 06:40:07 +02:00
|
|
|
if (!modif) modif = time(NULL);
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
token.str = NULL;
|
|
|
|
if (!get_path_token( name, &token )) return NULL;
|
1999-11-23 20:39:11 +01:00
|
|
|
*created = 0;
|
2005-11-22 13:05:36 +01:00
|
|
|
while (token.len)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key *subkey;
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!(subkey = find_subkey( key, &token, &index ))) break;
|
1999-11-23 20:39:11 +01:00
|
|
|
key = subkey;
|
2005-11-22 13:05:36 +01:00
|
|
|
get_path_token( name, &token );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* create the remaining part */
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!token.len) goto done;
|
1999-11-23 20:39:11 +01:00
|
|
|
*created = 1;
|
2002-06-21 01:18:56 +02:00
|
|
|
if (flags & KEY_DIRTY) make_dirty( key );
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
|
1999-11-23 20:39:11 +01:00
|
|
|
base = key;
|
2005-11-22 13:05:36 +01:00
|
|
|
for (;;)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
key->flags |= flags;
|
2005-11-22 13:05:36 +01:00
|
|
|
get_path_token( name, &token );
|
|
|
|
if (!token.len) break;
|
1999-11-23 20:39:11 +01:00
|
|
|
/* we know the index is always 0 in a new key */
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!(key = alloc_subkey( key, &token, 0, modif )))
|
|
|
|
{
|
|
|
|
free_subkey( base, index );
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (debug_level > 1) dump_operation( key, NULL, "Create" );
|
2005-11-22 13:05:36 +01:00
|
|
|
if (class && class->len)
|
|
|
|
{
|
|
|
|
key->classlen = class->len;
|
|
|
|
if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
|
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
grab_object( key );
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2000-10-02 05:46:58 +02:00
|
|
|
/* query information about a key or a subkey */
|
2002-06-21 01:18:56 +02:00
|
|
|
static void enum_key( const struct key *key, int index, int info_class,
|
|
|
|
struct enum_key_reply *reply )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-10-02 05:46:58 +02:00
|
|
|
int i;
|
|
|
|
size_t len, namelen, classlen;
|
|
|
|
int max_subkey = 0, max_class = 0;
|
|
|
|
int max_value = 0, max_data = 0;
|
2005-11-22 13:05:36 +01:00
|
|
|
char *data;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2000-10-02 05:46:58 +02:00
|
|
|
if (index != -1) /* -1 means use the specified key directly */
|
|
|
|
{
|
|
|
|
if ((index < 0) || (index > key->last_subkey))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NO_MORE_ENTRIES );
|
2001-11-30 19:46:42 +01:00
|
|
|
return;
|
2000-10-02 05:46:58 +02:00
|
|
|
}
|
|
|
|
key = key->subkeys[index];
|
|
|
|
}
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
namelen = key->namelen;
|
|
|
|
classlen = key->classlen;
|
2001-11-30 19:46:42 +01:00
|
|
|
|
|
|
|
switch(info_class)
|
2000-10-02 05:46:58 +02:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
case KeyBasicInformation:
|
|
|
|
classlen = 0; /* only return the name */
|
|
|
|
/* fall through */
|
|
|
|
case KeyNodeInformation:
|
|
|
|
reply->max_subkey = 0;
|
|
|
|
reply->max_class = 0;
|
|
|
|
reply->max_value = 0;
|
|
|
|
reply->max_data = 0;
|
|
|
|
break;
|
|
|
|
case KeyFullInformation:
|
2000-10-02 05:46:58 +02:00
|
|
|
for (i = 0; i <= key->last_subkey; i++)
|
|
|
|
{
|
|
|
|
struct key *subkey = key->subkeys[i];
|
2005-11-22 13:05:36 +01:00
|
|
|
len = subkey->namelen / sizeof(WCHAR);
|
2000-10-02 05:46:58 +02:00
|
|
|
if (len > max_subkey) max_subkey = len;
|
2005-11-22 13:05:36 +01:00
|
|
|
len = subkey->classlen / sizeof(WCHAR);
|
2000-10-02 05:46:58 +02:00
|
|
|
if (len > max_class) max_class = len;
|
|
|
|
}
|
|
|
|
for (i = 0; i <= key->last_value; i++)
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
len = key->values[i].namelen / sizeof(WCHAR);
|
2000-10-02 05:46:58 +02:00
|
|
|
if (len > max_value) max_value = len;
|
|
|
|
len = key->values[i].len;
|
|
|
|
if (len > max_data) max_data = len;
|
|
|
|
}
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->max_subkey = max_subkey;
|
|
|
|
reply->max_class = max_class;
|
|
|
|
reply->max_value = max_value;
|
|
|
|
reply->max_data = max_data;
|
|
|
|
namelen = 0; /* only return the class */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->subkeys = key->last_subkey + 1;
|
|
|
|
reply->values = key->last_value + 1;
|
|
|
|
reply->modif = key->modif;
|
|
|
|
reply->total = namelen + classlen;
|
2000-10-02 05:46:58 +02:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
len = min( reply->total, get_reply_max_size() );
|
|
|
|
if (len && (data = set_reply_data_size( len )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
if (len > namelen)
|
|
|
|
{
|
|
|
|
reply->namelen = namelen;
|
|
|
|
memcpy( data, key->name, namelen );
|
2005-11-22 13:05:36 +01:00
|
|
|
memcpy( data + namelen, key->class, len - namelen );
|
2001-11-30 19:46:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reply->namelen = len;
|
|
|
|
memcpy( data, key->name, len );
|
|
|
|
}
|
2000-10-02 05:46:58 +02:00
|
|
|
}
|
|
|
|
if (debug_level > 1) dump_operation( key, NULL, "Enum" );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* delete a key and its values */
|
2003-08-19 05:08:17 +02:00
|
|
|
static int delete_key( struct key *key, int recurse )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
struct key *parent;
|
|
|
|
|
2000-10-01 03:44:50 +02:00
|
|
|
/* must find parent and index */
|
2004-05-07 01:42:04 +02:00
|
|
|
if (key == root_key)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-10-01 03:44:50 +02:00
|
|
|
set_error( STATUS_ACCESS_DENIED );
|
2003-08-19 05:08:17 +02:00
|
|
|
return -1;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2000-10-01 03:44:50 +02:00
|
|
|
if (!(parent = key->parent) || (key->flags & KEY_DELETED))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-10-01 03:44:50 +02:00
|
|
|
set_error( STATUS_KEY_DELETED );
|
2003-08-19 05:08:17 +02:00
|
|
|
return -1;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2003-08-19 05:08:17 +02:00
|
|
|
|
|
|
|
while (recurse && (key->last_subkey>=0))
|
2005-06-10 21:54:46 +02:00
|
|
|
if (0 > delete_key(key->subkeys[key->last_subkey], 1))
|
2003-08-19 05:08:17 +02:00
|
|
|
return -1;
|
|
|
|
|
2000-10-01 03:44:50 +02:00
|
|
|
for (index = 0; index <= parent->last_subkey; index++)
|
|
|
|
if (parent->subkeys[index] == key) break;
|
|
|
|
assert( index <= parent->last_subkey );
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2004-05-07 01:42:04 +02:00
|
|
|
/* we can only delete a key that has no subkeys */
|
|
|
|
if (key->last_subkey >= 0)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_ACCESS_DENIED );
|
2003-08-19 05:08:17 +02:00
|
|
|
return -1;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2003-08-19 05:08:17 +02:00
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
if (debug_level > 1) dump_operation( key, NULL, "Delete" );
|
|
|
|
free_subkey( parent, index );
|
2002-11-25 03:47:32 +01:00
|
|
|
touch_key( parent, REG_NOTIFY_CHANGE_NAME );
|
2003-08-19 05:08:17 +02:00
|
|
|
return 0;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* try to grow the array of values; return 1 if OK, 0 on error */
|
|
|
|
static int grow_values( struct key *key )
|
|
|
|
{
|
|
|
|
struct key_value *new_val;
|
|
|
|
int nb_values;
|
|
|
|
|
|
|
|
if (key->nb_values)
|
|
|
|
{
|
|
|
|
nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
|
|
|
|
if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_NO_MEMORY );
|
1999-11-23 20:39:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nb_values = MIN_VALUES;
|
|
|
|
if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
|
|
|
|
}
|
|
|
|
key->values = new_val;
|
|
|
|
key->nb_values = nb_values;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the named value of a given key and return its index in the array */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
int i, min, max, res;
|
2005-11-22 13:05:36 +01:00
|
|
|
size_t len;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
|
|
|
min = 0;
|
|
|
|
max = key->last_value;
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
i = (min + max) / 2;
|
2005-11-22 13:05:36 +01:00
|
|
|
len = min( key->values[i].namelen, name->len );
|
|
|
|
res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
|
|
|
|
if (!res) res = key->values[i].namelen - name->len;
|
|
|
|
if (!res)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
*index = i;
|
|
|
|
return &key->values[i];
|
|
|
|
}
|
|
|
|
if (res > 0) max = i - 1;
|
|
|
|
else min = i + 1;
|
|
|
|
}
|
|
|
|
*index = min; /* this is where we should insert it */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-21 01:18:56 +02:00
|
|
|
/* insert a new value; the index must have been returned by find_value */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key_value *value;
|
2005-11-22 13:05:36 +01:00
|
|
|
WCHAR *new_name = NULL;
|
2002-06-21 01:18:56 +02:00
|
|
|
int i;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NAME_TOO_LONG );
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-21 01:18:56 +02:00
|
|
|
if (key->last_value + 1 == key->nb_values)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2002-06-21 01:18:56 +02:00
|
|
|
if (!grow_values( key )) return NULL;
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2005-11-22 13:05:36 +01:00
|
|
|
if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
|
2002-06-21 01:18:56 +02:00
|
|
|
for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
|
|
|
|
value = &key->values[index];
|
2005-11-22 13:05:36 +01:00
|
|
|
value->name = new_name;
|
|
|
|
value->namelen = name->len;
|
|
|
|
value->len = 0;
|
|
|
|
value->data = NULL;
|
1999-11-23 20:39:11 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a key value */
|
2005-11-22 13:05:36 +01:00
|
|
|
static void set_value( struct key *key, const struct unicode_str *name,
|
|
|
|
int type, const void *data, size_t len )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key_value *value;
|
|
|
|
void *ptr = NULL;
|
2002-06-21 01:18:56 +02:00
|
|
|
int index;
|
|
|
|
|
|
|
|
if ((value = find_value( key, name, &index )))
|
|
|
|
{
|
|
|
|
/* check if the new value is identical to the existing one */
|
|
|
|
if (value->type == type && value->len == len &&
|
|
|
|
value->data && !memcmp( value->data, data, len ))
|
|
|
|
{
|
|
|
|
if (debug_level > 1) dump_operation( key, value, "Skip setting" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2000-05-15 00:57:57 +02:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
if (len && !(ptr = memdup( data, len ))) return;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2002-06-21 01:18:56 +02:00
|
|
|
if (!value)
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2002-06-21 01:18:56 +02:00
|
|
|
if (!(value = insert_value( key, name, index )))
|
|
|
|
{
|
|
|
|
if (ptr) free( ptr );
|
|
|
|
return;
|
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
2002-06-21 01:18:56 +02:00
|
|
|
else if (value->data) free( value->data ); /* already existing, free previous data */
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
value->type = type;
|
2001-11-30 19:46:42 +01:00
|
|
|
value->len = len;
|
1999-11-23 20:39:11 +01:00
|
|
|
value->data = ptr;
|
2002-11-25 03:47:32 +01:00
|
|
|
touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
|
1999-11-23 20:39:11 +01:00
|
|
|
if (debug_level > 1) dump_operation( key, value, "Set" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a key value */
|
2005-11-22 13:05:36 +01:00
|
|
|
static void get_value( struct key *key, const struct unicode_str *name, int *type, unsigned int *len )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key_value *value;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if ((value = find_value( key, name, &index )))
|
|
|
|
{
|
|
|
|
*type = value->type;
|
|
|
|
*len = value->len;
|
2001-11-30 19:46:42 +01:00
|
|
|
if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
|
1999-11-23 20:39:11 +01:00
|
|
|
if (debug_level > 1) dump_operation( key, value, "Get" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*type = -1;
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enumerate a key value */
|
2001-11-30 19:46:42 +01:00
|
|
|
static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key_value *value;
|
|
|
|
|
2000-04-04 21:33:27 +02:00
|
|
|
if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
|
1999-11-23 20:39:11 +01:00
|
|
|
else
|
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
void *data;
|
|
|
|
size_t namelen, maxlen;
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
value = &key->values[i];
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->type = value->type;
|
2005-11-22 13:05:36 +01:00
|
|
|
namelen = value->namelen;
|
2000-12-15 21:57:00 +01:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
switch(info_class)
|
2000-05-15 00:57:57 +02:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
case KeyValueBasicInformation:
|
|
|
|
reply->total = namelen;
|
|
|
|
break;
|
|
|
|
case KeyValueFullInformation:
|
|
|
|
reply->total = namelen + value->len;
|
|
|
|
break;
|
|
|
|
case KeyValuePartialInformation:
|
|
|
|
reply->total = value->len;
|
|
|
|
namelen = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxlen = min( reply->total, get_reply_max_size() );
|
|
|
|
if (maxlen && ((data = set_reply_data_size( maxlen ))))
|
|
|
|
{
|
|
|
|
if (maxlen > namelen)
|
2000-12-15 21:57:00 +01:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->namelen = namelen;
|
|
|
|
memcpy( data, value->name, namelen );
|
|
|
|
memcpy( (char *)data + namelen, value->data, maxlen - namelen );
|
2000-12-15 21:57:00 +01:00
|
|
|
}
|
2001-11-30 19:46:42 +01:00
|
|
|
else
|
2000-12-15 21:57:00 +01:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->namelen = maxlen;
|
|
|
|
memcpy( data, value->name, maxlen );
|
2000-12-15 21:57:00 +01:00
|
|
|
}
|
2000-05-15 00:57:57 +02:00
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
if (debug_level > 1) dump_operation( key, value, "Enum" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete a value */
|
2005-11-22 13:05:36 +01:00
|
|
|
static void delete_value( struct key *key, const struct unicode_str *name )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
struct key_value *value;
|
|
|
|
int i, index, nb_values;
|
|
|
|
|
|
|
|
if (!(value = find_value( key, name, &index )))
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
|
1999-11-23 20:39:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (debug_level > 1) dump_operation( key, value, "Delete" );
|
2005-11-22 13:05:36 +01:00
|
|
|
if (value->name) free( value->name );
|
1999-11-23 20:39:11 +01:00
|
|
|
if (value->data) free( value->data );
|
|
|
|
for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
|
|
|
|
key->last_value--;
|
2002-11-25 03:47:32 +01:00
|
|
|
touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
|
1999-11-23 20:39:11 +01:00
|
|
|
|
|
|
|
/* try to shrink the array */
|
|
|
|
nb_values = key->nb_values;
|
|
|
|
if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
|
|
|
|
{
|
|
|
|
struct key_value *new_val;
|
|
|
|
nb_values -= nb_values / 3; /* shrink by 33% */
|
|
|
|
if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
|
|
|
|
if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
|
|
|
|
key->values = new_val;
|
|
|
|
key->nb_values = nb_values;
|
|
|
|
}
|
2000-08-26 06:40:07 +02:00
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
|
|
|
|
/* get the registry key corresponding to an hkey handle */
|
2002-05-30 22:12:58 +02:00
|
|
|
static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-08-26 06:40:07 +02:00
|
|
|
if (!hkey) return (struct key *)grab_object( root_key );
|
2004-05-07 01:42:04 +02:00
|
|
|
return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
1999-11-24 05:19:43 +01:00
|
|
|
/* read a line from the input file */
|
|
|
|
static int read_next_line( struct file_load_info *info )
|
|
|
|
{
|
|
|
|
char *newbuf;
|
|
|
|
int newlen, pos = 0;
|
|
|
|
|
|
|
|
info->line++;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (!fgets( info->buffer + pos, info->len - pos, info->file ))
|
|
|
|
return (pos != 0); /* EOF */
|
|
|
|
pos = strlen(info->buffer);
|
|
|
|
if (info->buffer[pos-1] == '\n')
|
|
|
|
{
|
|
|
|
/* got a full line */
|
|
|
|
info->buffer[--pos] = 0;
|
|
|
|
if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (pos < info->len - 1) return 1; /* EOF but something was read */
|
|
|
|
|
|
|
|
/* need to enlarge the buffer */
|
|
|
|
newlen = info->len + info->len / 2;
|
|
|
|
if (!(newbuf = realloc( info->buffer, newlen )))
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_NO_MEMORY );
|
1999-11-24 05:19:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
info->buffer = newbuf;
|
|
|
|
info->len = newlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure the temp buffer holds enough space */
|
2005-08-09 12:36:45 +02:00
|
|
|
static int get_file_tmp_space( struct file_load_info *info, size_t size )
|
1999-11-24 05:19:43 +01:00
|
|
|
{
|
2005-08-09 12:36:45 +02:00
|
|
|
WCHAR *tmp;
|
1999-11-24 05:19:43 +01:00
|
|
|
if (info->tmplen >= size) return 1;
|
|
|
|
if (!(tmp = realloc( info->tmp, size )))
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_NO_MEMORY );
|
1999-11-24 05:19:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
info->tmp = tmp;
|
|
|
|
info->tmplen = size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* report an error while loading an input file */
|
|
|
|
static void file_read_error( const char *err, struct file_load_info *info )
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
if (info->filename)
|
|
|
|
fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
|
|
|
|
else
|
|
|
|
fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
|
1999-11-24 05:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse an escaped string back into Unicode */
|
|
|
|
/* return the number of chars read from the input, or -1 on output overflow */
|
2005-11-22 13:05:36 +01:00
|
|
|
static int parse_strW( WCHAR *dest, size_t *len, const char *src, char endchar )
|
1999-11-24 05:19:43 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
size_t count = sizeof(WCHAR); /* for terminating null */
|
1999-11-24 05:19:43 +01:00
|
|
|
const char *p = src;
|
|
|
|
while (*p && *p != endchar)
|
|
|
|
{
|
|
|
|
if (*p != '\\') *dest = (WCHAR)*p++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
switch(*p)
|
|
|
|
{
|
|
|
|
case 'a': *dest = '\a'; p++; break;
|
|
|
|
case 'b': *dest = '\b'; p++; break;
|
|
|
|
case 'e': *dest = '\e'; p++; break;
|
|
|
|
case 'f': *dest = '\f'; p++; break;
|
|
|
|
case 'n': *dest = '\n'; p++; break;
|
|
|
|
case 'r': *dest = '\r'; p++; break;
|
|
|
|
case 't': *dest = '\t'; p++; break;
|
|
|
|
case 'v': *dest = '\v'; p++; break;
|
|
|
|
case 'x': /* hex escape */
|
|
|
|
p++;
|
|
|
|
if (!isxdigit(*p)) *dest = 'x';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dest = to_hex(*p++);
|
|
|
|
if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
|
|
|
|
if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
|
|
|
|
if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7': /* octal escape */
|
|
|
|
*dest = *p++ - '0';
|
|
|
|
if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
|
|
|
|
if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*dest = (WCHAR)*p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
*dest = 0;
|
|
|
|
if (!*p) return -1; /* delimiter not found */
|
|
|
|
*len = count;
|
|
|
|
return p + 1 - src;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert a data type tag to a value type */
|
|
|
|
static int get_data_type( const char *buffer, int *type, int *parse_type )
|
|
|
|
{
|
|
|
|
struct data_type { const char *tag; int len; int type; int parse_type; };
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static const struct data_type data_types[] =
|
1999-11-24 05:19:43 +01:00
|
|
|
{ /* actual type */ /* type to assume for parsing */
|
|
|
|
{ "\"", 1, REG_SZ, REG_SZ },
|
|
|
|
{ "str:\"", 5, REG_SZ, REG_SZ },
|
|
|
|
{ "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
|
|
|
|
{ "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
|
|
|
|
{ "hex:", 4, REG_BINARY, REG_BINARY },
|
|
|
|
{ "dword:", 6, REG_DWORD, REG_DWORD },
|
|
|
|
{ "hex(", 4, -1, REG_BINARY },
|
2000-10-23 01:56:32 +02:00
|
|
|
{ NULL, 0, 0, 0 }
|
1999-11-24 05:19:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct data_type *ptr;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
for (ptr = data_types; ptr->tag; ptr++)
|
|
|
|
{
|
|
|
|
if (memcmp( ptr->tag, buffer, ptr->len )) continue;
|
|
|
|
*parse_type = ptr->parse_type;
|
|
|
|
if ((*type = ptr->type) != -1) return ptr->len;
|
|
|
|
/* "hex(xx):" is special */
|
|
|
|
*type = (int)strtoul( buffer + 4, &end, 16 );
|
|
|
|
if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
|
|
|
|
return end + 2 - buffer;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load and create a key from the input file */
|
2002-06-21 01:18:56 +02:00
|
|
|
static struct key *load_key( struct key *base, const char *buffer, int flags,
|
2003-02-25 05:04:18 +01:00
|
|
|
int prefix_len, struct file_load_info *info,
|
|
|
|
int default_modif )
|
1999-11-24 05:19:43 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
WCHAR *p;
|
|
|
|
struct unicode_str name;
|
|
|
|
int res, modif;
|
|
|
|
size_t len = strlen(buffer) * sizeof(WCHAR);
|
1999-11-24 05:19:43 +01:00
|
|
|
|
|
|
|
if (!get_file_tmp_space( info, len )) return NULL;
|
|
|
|
|
2005-08-09 12:36:45 +02:00
|
|
|
if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
|
1999-11-24 05:19:43 +01:00
|
|
|
{
|
|
|
|
file_read_error( "Malformed key", info );
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-25 05:04:18 +01:00
|
|
|
if (sscanf( buffer + res, " %d", &modif ) != 1) modif = default_modif;
|
1999-11-24 05:19:43 +01:00
|
|
|
|
2005-08-09 12:36:45 +02:00
|
|
|
p = info->tmp;
|
2000-05-15 00:57:57 +02:00
|
|
|
while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
|
|
|
|
|
|
|
|
if (!*p)
|
|
|
|
{
|
|
|
|
if (prefix_len > 1)
|
|
|
|
{
|
|
|
|
file_read_error( "Malformed key", info );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* empty key name, return base key */
|
|
|
|
return (struct key *)grab_object( base );
|
|
|
|
}
|
2005-11-22 13:05:36 +01:00
|
|
|
name.str = p;
|
|
|
|
name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
|
|
|
|
return create_key( base, &name, NULL, flags, modif, &res );
|
1999-11-24 05:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a comma-separated list of hex digits */
|
2005-11-22 13:05:36 +01:00
|
|
|
static int parse_hex( unsigned char *dest, size_t *len, const char *buffer )
|
1999-11-24 05:19:43 +01:00
|
|
|
{
|
|
|
|
const char *p = buffer;
|
2005-11-22 13:05:36 +01:00
|
|
|
size_t count = 0;
|
1999-11-24 05:19:43 +01:00
|
|
|
while (isxdigit(*p))
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
char buf[3];
|
|
|
|
memcpy( buf, p, 2 );
|
|
|
|
buf[2] = 0;
|
|
|
|
sscanf( buf, "%x", &val );
|
|
|
|
if (count++ >= *len) return -1; /* dest buffer overflow */
|
|
|
|
*dest++ = (unsigned char )val;
|
|
|
|
p += 2;
|
|
|
|
if (*p == ',') p++;
|
|
|
|
}
|
|
|
|
*len = count;
|
|
|
|
return p - buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a value name and create the corresponding value */
|
2005-11-22 13:05:36 +01:00
|
|
|
static struct key_value *parse_value_name( struct key *key, const char *buffer, size_t *len,
|
1999-11-24 05:19:43 +01:00
|
|
|
struct file_load_info *info )
|
|
|
|
{
|
2002-06-21 01:18:56 +02:00
|
|
|
struct key_value *value;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str name;
|
|
|
|
int index;
|
|
|
|
size_t maxlen = strlen(buffer) * sizeof(WCHAR);
|
2002-06-21 01:18:56 +02:00
|
|
|
|
1999-11-24 05:19:43 +01:00
|
|
|
if (!get_file_tmp_space( info, maxlen )) return NULL;
|
2005-11-22 13:05:36 +01:00
|
|
|
name.str = info->tmp;
|
1999-11-24 05:19:43 +01:00
|
|
|
if (buffer[0] == '@')
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
name.len = 0;
|
1999-11-24 05:19:43 +01:00
|
|
|
*len = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-08-09 12:36:45 +02:00
|
|
|
if ((*len = parse_strW( info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
|
2005-11-22 13:05:36 +01:00
|
|
|
name.len = maxlen - sizeof(WCHAR);
|
1999-11-24 05:19:43 +01:00
|
|
|
(*len)++; /* for initial quote */
|
|
|
|
}
|
2000-08-26 06:40:07 +02:00
|
|
|
while (isspace(buffer[*len])) (*len)++;
|
1999-11-24 05:19:43 +01:00
|
|
|
if (buffer[*len] != '=') goto error;
|
|
|
|
(*len)++;
|
2000-08-26 06:40:07 +02:00
|
|
|
while (isspace(buffer[*len])) (*len)++;
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
|
2002-06-21 01:18:56 +02:00
|
|
|
return value;
|
1999-11-24 05:19:43 +01:00
|
|
|
|
|
|
|
error:
|
|
|
|
file_read_error( "Malformed value name", info );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load a value from the input file */
|
|
|
|
static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
|
|
|
|
{
|
|
|
|
DWORD dw;
|
|
|
|
void *ptr, *newptr;
|
2005-11-22 13:05:36 +01:00
|
|
|
int res, type, parse_type;
|
|
|
|
size_t maxlen, len;
|
1999-11-24 05:19:43 +01:00
|
|
|
struct key_value *value;
|
|
|
|
|
|
|
|
if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
|
|
|
|
if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
|
|
|
|
buffer += len + res;
|
|
|
|
|
|
|
|
switch(parse_type)
|
|
|
|
{
|
|
|
|
case REG_SZ:
|
|
|
|
len = strlen(buffer) * sizeof(WCHAR);
|
|
|
|
if (!get_file_tmp_space( info, len )) return 0;
|
2005-08-09 12:36:45 +02:00
|
|
|
if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
|
1999-11-24 05:19:43 +01:00
|
|
|
ptr = info->tmp;
|
|
|
|
break;
|
|
|
|
case REG_DWORD:
|
|
|
|
dw = strtoul( buffer, NULL, 16 );
|
|
|
|
ptr = &dw;
|
|
|
|
len = sizeof(dw);
|
|
|
|
break;
|
|
|
|
case REG_BINARY: /* hex digits */
|
|
|
|
len = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
|
|
|
|
if (!get_file_tmp_space( info, len + maxlen )) return 0;
|
2005-08-09 12:36:45 +02:00
|
|
|
if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
|
1999-11-24 05:19:43 +01:00
|
|
|
len += maxlen;
|
|
|
|
buffer += res;
|
|
|
|
while (isspace(*buffer)) buffer++;
|
|
|
|
if (!*buffer) break;
|
|
|
|
if (*buffer != '\\') goto error;
|
|
|
|
if (read_next_line( info) != 1) goto error;
|
|
|
|
buffer = info->buffer;
|
|
|
|
while (isspace(*buffer)) buffer++;
|
|
|
|
}
|
|
|
|
ptr = info->tmp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
ptr = NULL; /* keep compiler quiet */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!len) newptr = NULL;
|
|
|
|
else if (!(newptr = memdup( ptr, len ))) return 0;
|
|
|
|
|
|
|
|
if (value->data) free( value->data );
|
|
|
|
value->data = newptr;
|
|
|
|
value->len = len;
|
|
|
|
value->type = type;
|
2002-06-21 01:18:56 +02:00
|
|
|
make_dirty( key );
|
1999-11-24 05:19:43 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
error:
|
|
|
|
file_read_error( "Malformed value", info );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-05-15 00:57:57 +02:00
|
|
|
/* return the length (in path elements) of name that is part of the key name */
|
|
|
|
/* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
|
|
|
|
static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
|
|
|
|
{
|
|
|
|
WCHAR *p;
|
|
|
|
int res;
|
2005-11-22 13:05:36 +01:00
|
|
|
size_t len = strlen(name) * sizeof(WCHAR);
|
|
|
|
|
2000-05-24 01:38:32 +02:00
|
|
|
if (!get_file_tmp_space( info, len )) return 0;
|
2000-05-15 00:57:57 +02:00
|
|
|
|
2005-08-09 12:36:45 +02:00
|
|
|
if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
|
2000-05-15 00:57:57 +02:00
|
|
|
{
|
|
|
|
file_read_error( "Malformed key", info );
|
2000-05-24 01:38:32 +02:00
|
|
|
return 0;
|
2000-05-15 00:57:57 +02:00
|
|
|
}
|
2005-08-09 12:36:45 +02:00
|
|
|
for (p = info->tmp; *p; p++) if (*p == '\\') break;
|
2005-11-22 13:05:36 +01:00
|
|
|
len = (p - info->tmp) * sizeof(WCHAR);
|
2000-08-26 06:40:07 +02:00
|
|
|
for (res = 1; key != root_key; res++)
|
2000-05-15 00:57:57 +02:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
|
2000-05-15 00:57:57 +02:00
|
|
|
key = key->parent;
|
|
|
|
}
|
2000-08-26 06:40:07 +02:00
|
|
|
if (key == root_key) res = 0; /* no matching name */
|
2000-05-15 00:57:57 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-11-24 05:19:43 +01:00
|
|
|
/* load all the keys from the input file */
|
2004-04-30 20:35:13 +02:00
|
|
|
/* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
|
2005-11-22 13:05:36 +01:00
|
|
|
static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
|
1999-11-24 05:19:43 +01:00
|
|
|
{
|
|
|
|
struct key *subkey = NULL;
|
|
|
|
struct file_load_info info;
|
|
|
|
char *p;
|
2003-02-25 05:04:18 +01:00
|
|
|
int default_modif = time(NULL);
|
2002-06-21 01:18:56 +02:00
|
|
|
int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
|
2000-05-15 00:57:57 +02:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
info.filename = filename;
|
1999-11-24 05:19:43 +01:00
|
|
|
info.file = f;
|
|
|
|
info.len = 4;
|
|
|
|
info.tmplen = 4;
|
|
|
|
info.line = 0;
|
|
|
|
if (!(info.buffer = mem_alloc( info.len ))) return;
|
|
|
|
if (!(info.tmp = mem_alloc( info.tmplen )))
|
|
|
|
{
|
|
|
|
free( info.buffer );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((read_next_line( &info ) != 1) ||
|
|
|
|
strcmp( info.buffer, "WINE REGISTRY Version 2" ))
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_NOT_REGISTRY_FILE );
|
1999-11-24 05:19:43 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (read_next_line( &info ) == 1)
|
|
|
|
{
|
2001-03-05 20:34:17 +01:00
|
|
|
p = info.buffer;
|
|
|
|
while (*p && isspace(*p)) p++;
|
1999-11-24 05:19:43 +01:00
|
|
|
switch(*p)
|
|
|
|
{
|
|
|
|
case '[': /* new key */
|
|
|
|
if (subkey) release_object( subkey );
|
2000-05-15 00:57:57 +02:00
|
|
|
if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
|
2003-02-25 05:04:18 +01:00
|
|
|
if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info, default_modif )))
|
2000-05-15 00:57:57 +02:00
|
|
|
file_read_error( "Error creating key", &info );
|
1999-11-24 05:19:43 +01:00
|
|
|
break;
|
|
|
|
case '@': /* default value */
|
|
|
|
case '\"': /* value */
|
|
|
|
if (subkey) load_value( subkey, p, &info );
|
|
|
|
else file_read_error( "Value without key", &info );
|
|
|
|
break;
|
|
|
|
case '#': /* comment */
|
|
|
|
case ';': /* comment */
|
|
|
|
case 0: /* empty line */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
file_read_error( "Unrecognized input", &info );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (subkey) release_object( subkey );
|
|
|
|
free( info.buffer );
|
|
|
|
free( info.tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load a part of the registry from a file */
|
2002-05-30 22:12:58 +02:00
|
|
|
static void load_registry( struct key *key, obj_handle_t handle )
|
1999-11-24 05:19:43 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct file *file;
|
1999-11-24 05:19:43 +01:00
|
|
|
int fd;
|
|
|
|
|
2003-02-14 21:27:09 +01:00
|
|
|
if (!(file = get_file_obj( current->process, handle, GENERIC_READ ))) return;
|
|
|
|
fd = dup( get_file_unix_fd( file ) );
|
|
|
|
release_object( file );
|
1999-11-24 05:19:43 +01:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
FILE *f = fdopen( fd, "r" );
|
|
|
|
if (f)
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
load_keys( key, NULL, f, -1 );
|
1999-11-24 05:19:43 +01:00
|
|
|
fclose( f );
|
|
|
|
}
|
|
|
|
else file_set_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-07 06:13:21 +02:00
|
|
|
/* load one of the initial registry files */
|
|
|
|
static void load_init_registry_from_file( const char *filename, struct key *key )
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
2004-05-14 02:45:11 +02:00
|
|
|
if ((f = fopen( filename, "r" )))
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
load_keys( key, filename, f, 0 );
|
2004-05-14 02:45:11 +02:00
|
|
|
fclose( f );
|
|
|
|
if (get_error() == STATUS_NOT_REGISTRY_FILE)
|
2005-11-22 13:05:36 +01:00
|
|
|
{
|
|
|
|
fprintf( stderr, "%s is not a valid registry file\n", filename );
|
|
|
|
return;
|
|
|
|
}
|
2004-05-14 02:45:11 +02:00
|
|
|
}
|
2004-05-07 06:13:21 +02:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
|
2004-05-07 06:13:21 +02:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if ((save_branch_info[save_branch_count].path = strdup( filename )))
|
|
|
|
save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
|
2004-05-07 06:13:21 +02:00
|
|
|
}
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
|
2005-07-18 15:22:55 +02:00
|
|
|
{
|
|
|
|
static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
|
|
|
|
static const WCHAR formatW[] = {'-','%','u',0};
|
|
|
|
WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
|
|
|
|
WCHAR *p = buffer;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
strcpyW( p, prefixW );
|
|
|
|
p += strlenW( prefixW );
|
|
|
|
p += sprintfW( p, formatW, sid->Revision );
|
|
|
|
p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
|
|
|
|
sid->IdentifierAuthority.Value[4] ),
|
|
|
|
MAKEWORD( sid->IdentifierAuthority.Value[3],
|
|
|
|
sid->IdentifierAuthority.Value[2] )));
|
|
|
|
for (i = 0; i < sid->SubAuthorityCount; i++)
|
|
|
|
p += sprintfW( p, formatW, sid->SubAuthority[i] );
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
path->len = (p - buffer) * sizeof(WCHAR);
|
|
|
|
path->str = p = memdup( buffer, path->len );
|
|
|
|
return p;
|
2005-07-18 15:22:55 +02:00
|
|
|
}
|
|
|
|
|
2005-04-20 14:57:53 +02:00
|
|
|
/* registry initialisation */
|
|
|
|
void init_registry(void)
|
|
|
|
{
|
2004-05-07 06:13:21 +02:00
|
|
|
static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
|
|
|
|
static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
|
2005-11-22 13:05:36 +01:00
|
|
|
static const struct unicode_str root_name = { NULL, 0 };
|
|
|
|
static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
|
|
|
|
static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
|
|
|
|
|
2005-07-18 15:22:55 +02:00
|
|
|
WCHAR *current_user_path;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str current_user_str;
|
2004-05-07 06:13:21 +02:00
|
|
|
|
|
|
|
const char *config = wine_get_config_dir();
|
|
|
|
char *p, *filename;
|
|
|
|
struct key *key;
|
|
|
|
int dummy;
|
|
|
|
|
2005-04-20 14:57:53 +02:00
|
|
|
/* create the root key */
|
2005-11-22 13:05:36 +01:00
|
|
|
root_key = alloc_key( &root_name, time(NULL) );
|
2005-04-20 14:57:53 +02:00
|
|
|
assert( root_key );
|
|
|
|
|
|
|
|
if (!(filename = malloc( strlen(config) + 16 ))) fatal_error( "out of memory\n" );
|
2004-05-07 06:13:21 +02:00
|
|
|
strcpy( filename, config );
|
|
|
|
p = filename + strlen(filename);
|
|
|
|
|
|
|
|
/* load system.reg into Registry\Machine */
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!(key = create_key( root_key, &HKLM_name, NULL, 0, time(NULL), &dummy )))
|
2004-05-07 06:13:21 +02:00
|
|
|
fatal_error( "could not create Machine registry key\n" );
|
|
|
|
|
|
|
|
strcpy( p, "/system.reg" );
|
|
|
|
load_init_registry_from_file( filename, key );
|
|
|
|
release_object( key );
|
|
|
|
|
|
|
|
/* load userdef.reg into Registry\User\.Default */
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if (!(key = create_key( root_key, &HKU_name, NULL, 0, time(NULL), &dummy )))
|
2004-05-07 06:13:21 +02:00
|
|
|
fatal_error( "could not create User\\.Default registry key\n" );
|
|
|
|
|
|
|
|
strcpy( p, "/userdef.reg" );
|
|
|
|
load_init_registry_from_file( filename, key );
|
|
|
|
release_object( key );
|
|
|
|
|
2005-07-11 22:44:59 +02:00
|
|
|
/* load user.reg into HKEY_CURRENT_USER */
|
|
|
|
|
2005-07-18 15:22:55 +02:00
|
|
|
/* FIXME: match default user in token.c. should get from process token instead */
|
2005-11-22 13:05:36 +01:00
|
|
|
current_user_path = format_user_registry_path( security_interactive_sid, ¤t_user_str );
|
2005-07-18 15:22:55 +02:00
|
|
|
if (!current_user_path ||
|
2005-11-22 13:05:36 +01:00
|
|
|
!(key = create_key( root_key, ¤t_user_str, NULL, 0, time(NULL), &dummy )))
|
2005-07-11 22:44:59 +02:00
|
|
|
fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
|
2005-07-18 15:22:55 +02:00
|
|
|
free( current_user_path );
|
2005-07-11 22:44:59 +02:00
|
|
|
strcpy( p, "/user.reg" );
|
|
|
|
load_init_registry_from_file( filename, key );
|
|
|
|
release_object( key );
|
|
|
|
|
2000-08-26 06:40:07 +02:00
|
|
|
free( filename );
|
2005-07-11 22:44:59 +02:00
|
|
|
|
|
|
|
/* start the periodic save timer */
|
|
|
|
set_periodic_save_timer();
|
2000-08-26 06:40:07 +02:00
|
|
|
}
|
|
|
|
|
2000-05-15 00:57:57 +02:00
|
|
|
/* save a registry branch to a file */
|
|
|
|
static void save_all_subkeys( struct key *key, FILE *f )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2000-05-15 00:57:57 +02:00
|
|
|
fprintf( f, "WINE REGISTRY Version 2\n" );
|
|
|
|
fprintf( f, ";; All keys relative to " );
|
|
|
|
dump_path( key, NULL, f );
|
|
|
|
fprintf( f, "\n" );
|
|
|
|
save_subkeys( key, key, f );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* save a registry branch to a file handle */
|
2002-05-30 22:12:58 +02:00
|
|
|
static void save_registry( struct key *key, obj_handle_t handle )
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2003-02-14 21:27:09 +01:00
|
|
|
struct file *file;
|
1999-11-23 20:39:11 +01:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (key->flags & KEY_DELETED)
|
|
|
|
{
|
2000-01-24 22:58:06 +01:00
|
|
|
set_error( STATUS_KEY_DELETED );
|
1999-11-23 20:39:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2003-02-14 21:27:09 +01:00
|
|
|
if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) return;
|
|
|
|
fd = dup( get_file_unix_fd( file ) );
|
|
|
|
release_object( file );
|
1999-11-23 20:39:11 +01:00
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
FILE *f = fdopen( fd, "w" );
|
|
|
|
if (f)
|
|
|
|
{
|
2000-05-15 00:57:57 +02:00
|
|
|
save_all_subkeys( key, f );
|
1999-11-23 20:39:11 +01:00
|
|
|
if (fclose( f )) file_set_error();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file_set_error();
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-16 19:21:13 +02:00
|
|
|
/* save a registry branch to a file */
|
|
|
|
static int save_branch( struct key *key, const char *path )
|
|
|
|
{
|
2002-12-18 06:03:51 +01:00
|
|
|
struct stat st;
|
2000-04-16 19:21:13 +02:00
|
|
|
char *p, *real, *tmp = NULL;
|
2002-12-18 06:03:51 +01:00
|
|
|
int fd, count = 0, ret = 0, by_symlink;
|
2000-04-16 19:21:13 +02:00
|
|
|
FILE *f;
|
|
|
|
|
2002-06-21 01:18:56 +02:00
|
|
|
if (!(key->flags & KEY_DIRTY))
|
|
|
|
{
|
|
|
|
if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-04-16 19:21:13 +02:00
|
|
|
/* get the real path */
|
|
|
|
|
2002-12-18 06:03:51 +01:00
|
|
|
by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
|
2000-04-16 19:21:13 +02:00
|
|
|
if (!(real = malloc( PATH_MAX ))) return 0;
|
|
|
|
if (!realpath( path, real ))
|
|
|
|
{
|
|
|
|
free( real );
|
|
|
|
real = NULL;
|
|
|
|
}
|
|
|
|
else path = real;
|
|
|
|
|
|
|
|
/* test the file type */
|
|
|
|
|
|
|
|
if ((fd = open( path, O_WRONLY )) != -1)
|
|
|
|
{
|
2002-12-18 06:03:51 +01:00
|
|
|
/* if file is not a regular file or has multiple links or is accessed
|
|
|
|
* via symbolic links, write directly into it; otherwise use a temp file */
|
|
|
|
if (by_symlink ||
|
|
|
|
(!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
|
2000-04-16 19:21:13 +02:00
|
|
|
{
|
|
|
|
ftruncate( fd, 0 );
|
|
|
|
goto save;
|
|
|
|
}
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a temp file in the same directory */
|
|
|
|
|
|
|
|
if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
|
|
|
|
strcpy( tmp, path );
|
|
|
|
if ((p = strrchr( tmp, '/' ))) p++;
|
|
|
|
else p = tmp;
|
|
|
|
for (;;)
|
|
|
|
{
|
2000-04-24 20:04:24 +02:00
|
|
|
sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
|
2000-04-16 19:21:13 +02:00
|
|
|
if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
|
|
|
|
if (errno != EEXIST) goto done;
|
|
|
|
close( fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now save to it */
|
|
|
|
|
|
|
|
save:
|
|
|
|
if (!(f = fdopen( fd, "w" )))
|
|
|
|
{
|
|
|
|
if (tmp) unlink( tmp );
|
|
|
|
close( fd );
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug_level > 1)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s: ", path );
|
|
|
|
dump_operation( key, NULL, "saving" );
|
|
|
|
}
|
|
|
|
|
2000-05-15 00:57:57 +02:00
|
|
|
save_all_subkeys( key, f );
|
2000-04-16 19:21:13 +02:00
|
|
|
ret = !fclose(f);
|
|
|
|
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
/* if successfully written, rename to final name */
|
|
|
|
if (ret) ret = !rename( tmp, path );
|
|
|
|
if (!ret) unlink( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2005-03-10 12:18:31 +01:00
|
|
|
free( tmp );
|
|
|
|
free( real );
|
2002-06-21 01:18:56 +02:00
|
|
|
if (ret) make_clean( key );
|
2000-04-16 19:21:13 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* periodic saving of the registry */
|
|
|
|
static void periodic_save( void *arg )
|
|
|
|
{
|
|
|
|
int i;
|
2005-03-14 12:00:43 +01:00
|
|
|
|
|
|
|
save_timeout_user = NULL;
|
2000-04-16 19:21:13 +02:00
|
|
|
for (i = 0; i < save_branch_count; i++)
|
|
|
|
save_branch( save_branch_info[i].key, save_branch_info[i].path );
|
2005-03-14 12:00:43 +01:00
|
|
|
set_periodic_save_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start the periodic save timer */
|
|
|
|
static void set_periodic_save_timer(void)
|
|
|
|
{
|
|
|
|
struct timeval next;
|
|
|
|
|
2005-05-25 20:41:09 +02:00
|
|
|
gettimeofday( &next, NULL );
|
2005-03-14 12:00:43 +01:00
|
|
|
add_timeout( &next, save_period );
|
|
|
|
if (save_timeout_user) remove_timeout_user( save_timeout_user );
|
2005-05-25 20:41:09 +02:00
|
|
|
save_timeout_user = add_timeout_user( &next, periodic_save, NULL );
|
2000-04-16 19:21:13 +02:00
|
|
|
}
|
|
|
|
|
2002-06-21 01:18:56 +02:00
|
|
|
/* save the modified registry branches to disk */
|
|
|
|
void flush_registry(void)
|
2000-04-16 19:21:13 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < save_branch_count; i++)
|
|
|
|
{
|
|
|
|
if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
|
|
|
|
{
|
|
|
|
fprintf( stderr, "wineserver: could not save registry branch to %s",
|
|
|
|
save_branch_info[i].path );
|
|
|
|
perror( " " );
|
|
|
|
}
|
|
|
|
}
|
2002-06-21 01:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close the top-level keys; used on server exit */
|
|
|
|
void close_registry(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2005-03-14 12:00:43 +01:00
|
|
|
if (save_timeout_user) remove_timeout_user( save_timeout_user );
|
|
|
|
save_timeout_user = NULL;
|
2005-05-23 11:53:06 +02:00
|
|
|
for (i = 0; i < save_branch_count; i++)
|
|
|
|
{
|
|
|
|
release_object( save_branch_info[i].key );
|
|
|
|
free( save_branch_info[i].path );
|
|
|
|
}
|
2000-08-26 06:40:07 +02:00
|
|
|
release_object( root_key );
|
2000-04-16 19:21:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
/* create a registry key */
|
|
|
|
DECL_HANDLER(create_key)
|
|
|
|
{
|
2000-10-01 03:44:50 +02:00
|
|
|
struct key *key = NULL, *parent;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str name, class;
|
1999-11-23 20:39:11 +01:00
|
|
|
unsigned int access = req->access;
|
|
|
|
|
|
|
|
if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->hkey = 0;
|
2005-11-22 13:05:36 +01:00
|
|
|
|
|
|
|
if (req->namelen > get_req_data_size())
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
|
|
|
|
class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
|
|
|
|
get_req_path( &name, !req->parent );
|
|
|
|
if (name.str > class.str)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
name.len = (class.str - name.str) * sizeof(WCHAR);
|
|
|
|
|
2005-06-16 22:34:34 +02:00
|
|
|
/* NOTE: no access rights are required from the parent handle to create a key */
|
|
|
|
if ((parent = get_hkey_obj( req->parent, 0 )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2002-06-21 01:18:56 +02:00
|
|
|
int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
|
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
if ((key = create_key( parent, &name, &class, flags, req->modif, &reply->created )))
|
2000-10-02 05:46:58 +02:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->hkey = alloc_handle( current->process, key, access, 0 );
|
2000-10-02 05:46:58 +02:00
|
|
|
release_object( key );
|
|
|
|
}
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( parent );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open a registry key */
|
|
|
|
DECL_HANDLER(open_key)
|
|
|
|
{
|
|
|
|
struct key *key, *parent;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str name;
|
1999-11-23 20:39:11 +01:00
|
|
|
unsigned int access = req->access;
|
|
|
|
|
|
|
|
if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->hkey = 0;
|
2005-05-05 18:57:55 +02:00
|
|
|
/* NOTE: no access rights are required to open the parent key, only the child key */
|
|
|
|
if ((parent = get_hkey_obj( req->parent, 0 )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
get_req_path( &name, !req->parent );
|
|
|
|
if ((key = open_key( parent, &name )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->hkey = alloc_handle( current->process, key, access, 0 );
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
release_object( parent );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete a registry key */
|
|
|
|
DECL_HANDLER(delete_key)
|
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
|
2005-05-05 18:57:55 +02:00
|
|
|
if ((key = get_hkey_obj( req->hkey, DELETE )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2003-08-19 05:08:17 +02:00
|
|
|
delete_key( key, 0);
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-03 01:38:30 +01:00
|
|
|
/* flush a registry key */
|
|
|
|
DECL_HANDLER(flush_key)
|
|
|
|
{
|
|
|
|
struct key *key = get_hkey_obj( req->hkey, 0 );
|
|
|
|
if (key)
|
|
|
|
{
|
|
|
|
/* we don't need to do anything here with the current implementation */
|
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
/* enumerate registry subkeys */
|
|
|
|
DECL_HANDLER(enum_key)
|
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
|
2000-10-02 05:46:58 +02:00
|
|
|
if ((key = get_hkey_obj( req->hkey,
|
|
|
|
req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
enum_key( key, req->index, req->info_class, reply );
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a value of a registry key */
|
|
|
|
DECL_HANDLER(set_key_value)
|
|
|
|
{
|
|
|
|
struct key *key;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str name;
|
|
|
|
|
|
|
|
if (req->namelen > get_req_data_size())
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_PARAMETER );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
name.str = get_req_data();
|
|
|
|
name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
|
2000-05-15 00:57:57 +02:00
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
|
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
size_t datalen = get_req_data_size() - req->namelen;
|
2004-11-30 22:38:57 +01:00
|
|
|
const char *data = (const char *)get_req_data() + req->namelen;
|
2000-10-01 03:44:50 +02:00
|
|
|
|
2005-11-22 13:05:36 +01:00
|
|
|
set_value( key, &name, req->type, data, datalen );
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* retrieve the value of a registry key */
|
|
|
|
DECL_HANDLER(get_key_value)
|
|
|
|
{
|
|
|
|
struct key *key;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str name;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2001-11-30 19:46:42 +01:00
|
|
|
reply->total = 0;
|
1999-11-23 20:39:11 +01:00
|
|
|
if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
get_req_unicode_str( &name );
|
|
|
|
get_value( key, &name, &reply->type, &reply->total );
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enumerate the value of a registry key */
|
|
|
|
DECL_HANDLER(enum_key_value)
|
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
|
|
|
|
if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
|
|
|
|
{
|
2001-11-30 19:46:42 +01:00
|
|
|
enum_value( key, req->index, req->info_class, reply );
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete a value of a registry key */
|
|
|
|
DECL_HANDLER(delete_key_value)
|
|
|
|
{
|
|
|
|
struct key *key;
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str name;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
|
|
|
if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
|
|
|
|
{
|
2005-11-22 13:05:36 +01:00
|
|
|
get_req_unicode_str( &name );
|
|
|
|
delete_value( key, &name );
|
1999-11-23 20:39:11 +01:00
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load a registry branch from a file */
|
|
|
|
DECL_HANDLER(load_registry)
|
|
|
|
{
|
2005-03-29 13:38:58 +02:00
|
|
|
struct key *key, *parent;
|
2005-05-05 18:57:55 +02:00
|
|
|
struct token *token = thread_get_impersonation_token( current );
|
2005-11-22 13:05:36 +01:00
|
|
|
struct unicode_str name;
|
1999-11-23 20:39:11 +01:00
|
|
|
|
2005-05-05 18:57:55 +02:00
|
|
|
const LUID_AND_ATTRIBUTES privs[] =
|
|
|
|
{
|
|
|
|
{ SeBackupPrivilege, 0 },
|
|
|
|
{ SeRestorePrivilege, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!token || !token_check_privileges( token, TRUE, privs,
|
|
|
|
sizeof(privs)/sizeof(privs[0]), NULL ))
|
|
|
|
{
|
|
|
|
set_error( STATUS_PRIVILEGE_NOT_HELD );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((parent = get_hkey_obj( req->hkey, 0 )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
2005-03-29 13:38:58 +02:00
|
|
|
int dummy;
|
2005-11-22 13:05:36 +01:00
|
|
|
get_req_path( &name, !req->hkey );
|
|
|
|
if ((key = create_key( parent, &name, NULL, KEY_DIRTY, time(NULL), &dummy )))
|
2005-03-29 13:38:58 +02:00
|
|
|
{
|
|
|
|
load_registry( key, req->file );
|
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
release_object( parent );
|
1999-11-23 20:39:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-19 05:08:17 +02:00
|
|
|
DECL_HANDLER(unload_registry)
|
|
|
|
{
|
|
|
|
struct key *key;
|
2005-05-05 18:57:55 +02:00
|
|
|
struct token *token = thread_get_impersonation_token( current );
|
|
|
|
|
|
|
|
const LUID_AND_ATTRIBUTES privs[] =
|
|
|
|
{
|
|
|
|
{ SeBackupPrivilege, 0 },
|
|
|
|
{ SeRestorePrivilege, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!token || !token_check_privileges( token, TRUE, privs,
|
|
|
|
sizeof(privs)/sizeof(privs[0]), NULL ))
|
|
|
|
{
|
|
|
|
set_error( STATUS_PRIVILEGE_NOT_HELD );
|
|
|
|
return;
|
|
|
|
}
|
2003-08-19 05:08:17 +02:00
|
|
|
|
|
|
|
if ((key = get_hkey_obj( req->hkey, 0 )))
|
|
|
|
{
|
|
|
|
delete_key( key, 1 ); /* FIXME */
|
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-23 20:39:11 +01:00
|
|
|
/* save a registry branch to a file */
|
|
|
|
DECL_HANDLER(save_registry)
|
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
|
2005-05-05 18:57:55 +02:00
|
|
|
if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
|
|
|
|
{
|
|
|
|
set_error( STATUS_PRIVILEGE_NOT_HELD );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((key = get_hkey_obj( req->hkey, 0 )))
|
1999-11-23 20:39:11 +01:00
|
|
|
{
|
|
|
|
save_registry( key, req->file );
|
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-25 03:47:32 +01:00
|
|
|
/* add a registry key change notification */
|
|
|
|
DECL_HANDLER(set_registry_notification)
|
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
struct event *event;
|
|
|
|
struct notify *notify;
|
|
|
|
|
|
|
|
key = get_hkey_obj( req->hkey, KEY_NOTIFY );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (key)
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
|
|
|
event = get_event_obj( current->process, req->event, SYNCHRONIZE );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (event)
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
2005-06-09 22:36:08 +02:00
|
|
|
notify = find_notify( key, current->process, req->hkey );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (notify)
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
|
|
|
release_object( notify->event );
|
|
|
|
grab_object( event );
|
|
|
|
notify->event = event;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-02-24 20:43:53 +01:00
|
|
|
notify = mem_alloc( sizeof(*notify) );
|
2005-06-10 21:54:46 +02:00
|
|
|
if (notify)
|
2002-11-25 03:47:32 +01:00
|
|
|
{
|
|
|
|
grab_object( event );
|
|
|
|
notify->event = event;
|
|
|
|
notify->subtree = req->subtree;
|
|
|
|
notify->filter = req->filter;
|
|
|
|
notify->hkey = req->hkey;
|
2005-06-09 22:36:08 +02:00
|
|
|
notify->process = current->process;
|
2005-02-24 20:43:53 +01:00
|
|
|
list_add_head( &key->notify_list, ¬ify->entry );
|
2002-11-25 03:47:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
release_object( event );
|
|
|
|
}
|
|
|
|
release_object( key );
|
|
|
|
}
|
|
|
|
}
|