Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side registry management |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* To do: |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 22 | * - symbolic links |
| 23 | */ |
| 24 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 25 | #include "config.h" |
| 26 | #include "wine/port.h" |
| 27 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 28 | #include <assert.h> |
Alexandre Julliard | 7e495e1 | 2000-07-25 21:01:59 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 32 | #include <limits.h> |
| 33 | #include <stdio.h> |
Alexandre Julliard | 62986a0 | 2003-09-07 05:08:14 +0000 | [diff] [blame] | 34 | #include <stdarg.h> |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 35 | #include <string.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 36 | #include <stdlib.h> |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 37 | #include <sys/stat.h> |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 39 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 40 | #include "ntstatus.h" |
| 41 | #define WIN32_NO_STATUS |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 42 | #include "object.h" |
Alexandre Julliard | 863637b | 2003-01-30 00:26:44 +0000 | [diff] [blame] | 43 | #include "file.h" |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 44 | #include "handle.h" |
| 45 | #include "request.h" |
| 46 | #include "unicode.h" |
Robert Shearman | b3957e3 | 2005-05-05 16:57:55 +0000 | [diff] [blame] | 47 | #include "security.h" |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 48 | |
Patrik Stridvall | 9c1de6d | 2002-09-12 22:07:02 +0000 | [diff] [blame] | 49 | #include "winternl.h" |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 50 | #include "wine/library.h" |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 51 | |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 52 | struct notify |
| 53 | { |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 54 | struct list entry; /* entry in list of notifications */ |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 55 | struct event *event; /* event to set when changing this key */ |
| 56 | int subtree; /* true if subtree notification */ |
| 57 | unsigned int filter; /* which events to notify on */ |
| 58 | obj_handle_t hkey; /* hkey associated with this notification */ |
Alexandre Julliard | 9b3b7ca | 2005-06-09 20:36:08 +0000 | [diff] [blame] | 59 | struct process *process; /* process in which the hkey is valid */ |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 62 | /* a registry key */ |
| 63 | struct key |
| 64 | { |
| 65 | struct object obj; /* object header */ |
| 66 | WCHAR *name; /* key name */ |
| 67 | WCHAR *class; /* key class */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 68 | unsigned short namelen; /* length of key name */ |
| 69 | unsigned short classlen; /* length of class name */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 70 | struct key *parent; /* parent key */ |
| 71 | int last_subkey; /* last in use subkey */ |
| 72 | int nb_subkeys; /* count of allocated subkeys */ |
| 73 | struct key **subkeys; /* subkeys array */ |
| 74 | int last_value; /* last in use value */ |
| 75 | int nb_values; /* count of allocated values in array */ |
| 76 | struct key_value *values; /* values array */ |
Alexandre Julliard | dcab706 | 2005-03-14 11:00:43 +0000 | [diff] [blame] | 77 | unsigned int flags; /* flags */ |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 78 | timeout_t modif; /* last modification time */ |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 79 | struct list notify_list; /* list of notifications */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /* key flags */ |
| 83 | #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */ |
| 84 | #define KEY_DELETED 0x0002 /* key has been deleted */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 85 | #define KEY_DIRTY 0x0004 /* key has been modified */ |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 86 | #define KEY_SYMLINK 0x0008 /* key is a symbolic link */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 87 | |
| 88 | /* a key value */ |
| 89 | struct key_value |
| 90 | { |
| 91 | WCHAR *name; /* value name */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 92 | unsigned short namelen; /* length of value name */ |
| 93 | unsigned short type; /* value type */ |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 94 | data_size_t len; /* value data length in bytes */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 95 | void *data; /* pointer to value data */ |
| 96 | }; |
| 97 | |
| 98 | #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */ |
| 99 | #define MIN_VALUES 8 /* min. number of allocated values per key */ |
| 100 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 101 | #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */ |
| 102 | #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 103 | |
Alexandre Julliard | 7e9757c | 2004-05-06 23:42:04 +0000 | [diff] [blame] | 104 | /* the root of the registry tree */ |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 105 | static struct key *root_key; |
| 106 | |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 107 | static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC; |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 108 | static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */ |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 109 | static struct timeout_user *save_timeout_user; /* saving timer */ |
| 110 | |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 111 | static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' }; |
| 112 | static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'}; |
| 113 | static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) }; |
| 114 | |
Alexandre Julliard | dcab706 | 2005-03-14 11:00:43 +0000 | [diff] [blame] | 115 | static void set_periodic_save_timer(void); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 116 | static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index ); |
Alexandre Julliard | dcab706 | 2005-03-14 11:00:43 +0000 | [diff] [blame] | 117 | |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 118 | /* information about where to save a registry branch */ |
| 119 | struct save_branch_info |
| 120 | { |
| 121 | struct key *key; |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 122 | const char *path; |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 125 | #define MAX_SAVE_BRANCH_INFO 3 |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 126 | static int save_branch_count; |
| 127 | static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO]; |
| 128 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 129 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 130 | /* information about a file being loaded */ |
| 131 | struct file_load_info |
| 132 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 133 | const char *filename; /* input file name */ |
| 134 | FILE *file; /* input file */ |
| 135 | char *buffer; /* line buffer */ |
| 136 | int len; /* buffer length */ |
| 137 | int line; /* current input line */ |
| 138 | WCHAR *tmp; /* temp buffer to use while parsing input */ |
| 139 | size_t tmplen; /* length of temp buffer */ |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 143 | static void key_dump( struct object *obj, int verbose ); |
Alexandre Julliard | 32a9396 | 2005-12-12 15:02:26 +0100 | [diff] [blame] | 144 | static unsigned int key_map_access( struct object *obj, unsigned int access ); |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 145 | static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 146 | static void key_destroy( struct object *obj ); |
| 147 | |
| 148 | static const struct object_ops key_ops = |
| 149 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 150 | sizeof(struct key), /* size */ |
| 151 | key_dump, /* dump */ |
Alexandre Julliard | 8382eb0 | 2007-12-05 18:16:42 +0100 | [diff] [blame] | 152 | no_get_type, /* get_type */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 153 | no_add_queue, /* add_queue */ |
| 154 | NULL, /* remove_queue */ |
| 155 | NULL, /* signaled */ |
| 156 | NULL, /* satisfied */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 157 | no_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 158 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 32a9396 | 2005-12-12 15:02:26 +0100 | [diff] [blame] | 159 | key_map_access, /* map_access */ |
Rob Shearman | c1707d8 | 2007-10-03 13:10:37 +0100 | [diff] [blame] | 160 | default_get_sd, /* get_sd */ |
| 161 | default_set_sd, /* set_sd */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 162 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | 7e71c1d | 2007-03-22 11:44:29 +0100 | [diff] [blame] | 163 | no_open_file, /* open_file */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 164 | key_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 165 | key_destroy /* destroy */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | |
| 169 | /* |
| 170 | * The registry text file format v2 used by this code is similar to the one |
| 171 | * used by REGEDIT import/export functionality, with the following differences: |
| 172 | * - strings and key names can contain \x escapes for Unicode |
| 173 | * - key names use escapes too in order to support Unicode |
| 174 | * - the modification time optionally follows the key name |
| 175 | * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex |
| 176 | */ |
| 177 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 178 | /* dump the full path of a key */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 179 | static void dump_path( const struct key *key, const struct key *base, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 180 | { |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 181 | if (key->parent && key->parent != base) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 182 | { |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 183 | dump_path( key->parent, base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 184 | fprintf( f, "\\\\" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 185 | } |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 186 | dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* dump a value to a text file */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 190 | static void dump_value( const struct key_value *value, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 191 | { |
Alexandre Julliard | b647ded | 2008-01-07 21:06:49 +0100 | [diff] [blame] | 192 | unsigned int i, dw; |
Hans Leidekker | 719a789 | 2004-09-22 02:46:38 +0000 | [diff] [blame] | 193 | int count; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 194 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 195 | if (value->namelen) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 196 | { |
| 197 | fputc( '\"', f ); |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 198 | count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 199 | count += fprintf( f, "\"=" ); |
| 200 | } |
| 201 | else count = fprintf( f, "@=" ); |
| 202 | |
| 203 | switch(value->type) |
| 204 | { |
| 205 | case REG_SZ: |
| 206 | case REG_EXPAND_SZ: |
| 207 | case REG_MULTI_SZ: |
Alexandre Julliard | b647ded | 2008-01-07 21:06:49 +0100 | [diff] [blame] | 208 | /* only output properly terminated strings in string format */ |
| 209 | if (value->len < sizeof(WCHAR)) break; |
| 210 | if (value->len % sizeof(WCHAR)) break; |
| 211 | if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break; |
| 212 | if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 213 | fputc( '\"', f ); |
Marcus Meissner | 368d935 | 2008-01-09 22:56:06 +0100 | [diff] [blame] | 214 | dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" ); |
Alexandre Julliard | b647ded | 2008-01-07 21:06:49 +0100 | [diff] [blame] | 215 | fprintf( f, "\"\n" ); |
| 216 | return; |
| 217 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 218 | case REG_DWORD: |
Alexandre Julliard | b647ded | 2008-01-07 21:06:49 +0100 | [diff] [blame] | 219 | if (value->len != sizeof(dw)) break; |
| 220 | memcpy( &dw, value->data, sizeof(dw) ); |
| 221 | fprintf( f, "dword:%08x\n", dw ); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | if (value->type == REG_BINARY) count += fprintf( f, "hex:" ); |
| 226 | else count += fprintf( f, "hex(%x):", value->type ); |
| 227 | for (i = 0; i < value->len; i++) |
| 228 | { |
| 229 | count += fprintf( f, "%02x", *((unsigned char *)value->data + i) ); |
| 230 | if (i < value->len-1) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 231 | { |
Alexandre Julliard | b647ded | 2008-01-07 21:06:49 +0100 | [diff] [blame] | 232 | fputc( ',', f ); |
| 233 | if (++count > 76) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 234 | { |
Alexandre Julliard | b647ded | 2008-01-07 21:06:49 +0100 | [diff] [blame] | 235 | fprintf( f, "\\\n " ); |
| 236 | count = 2; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 239 | } |
| 240 | fputc( '\n', f ); |
| 241 | } |
| 242 | |
| 243 | /* save a registry and all its subkeys to a text file */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 244 | static void save_subkeys( const struct key *key, const struct key *base, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 245 | { |
| 246 | int i; |
| 247 | |
| 248 | if (key->flags & KEY_VOLATILE) return; |
Alexandre Julliard | 2a37867 | 2010-02-16 12:26:15 +0100 | [diff] [blame^] | 249 | /* save key if it has either some values or no subkeys, or needs special options */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 250 | /* keys with no values but subkeys are saved implicitly by saving the subkeys */ |
Alexandre Julliard | 2a37867 | 2010-02-16 12:26:15 +0100 | [diff] [blame^] | 251 | if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 252 | { |
| 253 | fprintf( f, "\n[" ); |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 254 | if (key != base) dump_path( key, base, f ); |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 255 | fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) ); |
Alexandre Julliard | 2a37867 | 2010-02-16 12:26:15 +0100 | [diff] [blame^] | 256 | if (key->class) |
| 257 | { |
| 258 | fprintf( f, "#class=\"" ); |
| 259 | dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" ); |
| 260 | fprintf( f, "\"\n" ); |
| 261 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 262 | for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f ); |
| 263 | } |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 264 | for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 267 | static void dump_operation( const struct key *key, const struct key_value *value, const char *op ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 268 | { |
| 269 | fprintf( stderr, "%s key ", op ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 270 | if (key) dump_path( key, NULL, stderr ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 271 | else fprintf( stderr, "ERROR" ); |
| 272 | if (value) |
| 273 | { |
| 274 | fprintf( stderr, " value "); |
| 275 | dump_value( value, stderr ); |
| 276 | } |
| 277 | else fprintf( stderr, "\n" ); |
| 278 | } |
| 279 | |
| 280 | static void key_dump( struct object *obj, int verbose ) |
| 281 | { |
| 282 | struct key *key = (struct key *)obj; |
| 283 | assert( obj->ops == &key_ops ); |
| 284 | fprintf( stderr, "Key flags=%x ", key->flags ); |
Juergen Schmied | 5d0ae2d | 2000-01-09 21:07:01 +0000 | [diff] [blame] | 285 | dump_path( key, NULL, stderr ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 286 | fprintf( stderr, "\n" ); |
| 287 | } |
| 288 | |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 289 | /* notify waiter and maybe delete the notification */ |
| 290 | static void do_notification( struct key *key, struct notify *notify, int del ) |
| 291 | { |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 292 | if (notify->event) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 293 | { |
| 294 | set_event( notify->event ); |
| 295 | release_object( notify->event ); |
| 296 | notify->event = NULL; |
| 297 | } |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 298 | if (del) |
| 299 | { |
| 300 | list_remove( ¬ify->entry ); |
| 301 | free( notify ); |
| 302 | } |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Alexandre Julliard | 9b3b7ca | 2005-06-09 20:36:08 +0000 | [diff] [blame] | 305 | static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey ) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 306 | { |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 307 | struct notify *notify; |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 308 | |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 309 | LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry ) |
| 310 | { |
Alexandre Julliard | 9b3b7ca | 2005-06-09 20:36:08 +0000 | [diff] [blame] | 311 | if (notify->process == process && notify->hkey == hkey) return notify; |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 312 | } |
| 313 | return NULL; |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Alexandre Julliard | 32a9396 | 2005-12-12 15:02:26 +0100 | [diff] [blame] | 316 | static unsigned int key_map_access( struct object *obj, unsigned int access ) |
| 317 | { |
| 318 | if (access & GENERIC_READ) access |= KEY_READ; |
| 319 | if (access & GENERIC_WRITE) access |= KEY_WRITE; |
| 320 | if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE; |
| 321 | if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS; |
| 322 | return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL); |
| 323 | } |
| 324 | |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 325 | /* close the notification associated with a handle */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 326 | static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle ) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 327 | { |
| 328 | struct key * key = (struct key *) obj; |
Alexandre Julliard | 9b3b7ca | 2005-06-09 20:36:08 +0000 | [diff] [blame] | 329 | struct notify *notify = find_notify( key, process, handle ); |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 330 | if (notify) do_notification( key, notify, 1 ); |
| 331 | return 1; /* ok to close */ |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 334 | static void key_destroy( struct object *obj ) |
| 335 | { |
| 336 | int i; |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 337 | struct list *ptr; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 338 | struct key *key = (struct key *)obj; |
| 339 | assert( obj->ops == &key_ops ); |
| 340 | |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 341 | free( key->name ); |
| 342 | free( key->class ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 343 | for (i = 0; i <= key->last_value; i++) |
| 344 | { |
Lionel Debroux | e122f81 | 2007-10-20 09:37:43 +0200 | [diff] [blame] | 345 | free( key->values[i].name ); |
| 346 | free( key->values[i].data ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 347 | } |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 348 | free( key->values ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 349 | for (i = 0; i <= key->last_subkey; i++) |
| 350 | { |
| 351 | key->subkeys[i]->parent = NULL; |
| 352 | release_object( key->subkeys[i] ); |
| 353 | } |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 354 | free( key->subkeys ); |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 355 | /* unconditionally notify everything waiting on this key */ |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 356 | while ((ptr = list_head( &key->notify_list ))) |
| 357 | { |
| 358 | struct notify *notify = LIST_ENTRY( ptr, struct notify, entry ); |
| 359 | do_notification( key, notify, 1 ); |
| 360 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 363 | /* get the request vararg as registry path */ |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 364 | static inline void get_req_path( struct unicode_str *str, int skip_root ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 365 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 366 | str->str = get_req_data(); |
| 367 | str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 368 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 369 | if (skip_root && str->len >= sizeof(root_name) && |
| 370 | !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) )) |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 371 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 372 | str->str += sizeof(root_name)/sizeof(WCHAR); |
| 373 | str->len -= sizeof(root_name); |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 374 | } |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 377 | /* return the next token in a given path */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 378 | /* token->str must point inside the path, or be NULL for the first call */ |
| 379 | static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 380 | { |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 381 | data_size_t i = 0, len = path->len / sizeof(WCHAR); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 382 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 383 | if (!token->str) /* first time */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 384 | { |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 385 | /* path cannot start with a backslash */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 386 | if (len && path->str[0] == '\\') |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 387 | { |
| 388 | set_error( STATUS_OBJECT_PATH_INVALID ); |
| 389 | return NULL; |
| 390 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 391 | } |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 392 | else |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 393 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 394 | i = token->str - path->str; |
| 395 | i += token->len / sizeof(WCHAR); |
| 396 | while (i < len && path->str[i] == '\\') i++; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 397 | } |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 398 | token->str = path->str + i; |
| 399 | while (i < len && path->str[i] != '\\') i++; |
| 400 | token->len = (path->str + i - token->str) * sizeof(WCHAR); |
| 401 | return token; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* allocate a key object */ |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 405 | static struct key *alloc_key( const struct unicode_str *name, timeout_t modif ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 406 | { |
| 407 | struct key *key; |
Alexandre Julliard | e66207e | 2003-02-19 00:33:32 +0000 | [diff] [blame] | 408 | if ((key = alloc_object( &key_ops ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 409 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 410 | key->name = NULL; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 411 | key->class = NULL; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 412 | key->namelen = name->len; |
| 413 | key->classlen = 0; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 414 | key->flags = 0; |
| 415 | key->last_subkey = -1; |
| 416 | key->nb_subkeys = 0; |
| 417 | key->subkeys = NULL; |
| 418 | key->nb_values = 0; |
| 419 | key->last_value = -1; |
| 420 | key->values = NULL; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 421 | key->modif = modif; |
| 422 | key->parent = NULL; |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 423 | list_init( &key->notify_list ); |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 424 | if (name->len && !(key->name = memdup( name->str, name->len ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 425 | { |
| 426 | release_object( key ); |
| 427 | key = NULL; |
| 428 | } |
| 429 | } |
| 430 | return key; |
| 431 | } |
| 432 | |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 433 | /* mark a key and all its parents as dirty (modified) */ |
| 434 | static void make_dirty( struct key *key ) |
| 435 | { |
| 436 | while (key) |
| 437 | { |
| 438 | if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */ |
| 439 | key->flags |= KEY_DIRTY; |
| 440 | key = key->parent; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* mark a key and all its subkeys as clean (not modified) */ |
| 445 | static void make_clean( struct key *key ) |
| 446 | { |
| 447 | int i; |
| 448 | |
| 449 | if (key->flags & KEY_VOLATILE) return; |
| 450 | if (!(key->flags & KEY_DIRTY)) return; |
| 451 | key->flags &= ~KEY_DIRTY; |
| 452 | for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] ); |
| 453 | } |
| 454 | |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 455 | /* go through all the notifications and send them if necessary */ |
Robert Shearman | c516571 | 2005-05-25 18:41:09 +0000 | [diff] [blame] | 456 | static void check_notify( struct key *key, unsigned int change, int not_subtree ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 457 | { |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 458 | struct list *ptr, *next; |
| 459 | |
| 460 | LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list ) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 461 | { |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 462 | struct notify *n = LIST_ENTRY( ptr, struct notify, entry ); |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 463 | if ( ( not_subtree || n->subtree ) && ( change & n->filter ) ) |
| 464 | do_notification( key, n, 0 ); |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | /* update key modification time */ |
| 469 | static void touch_key( struct key *key, unsigned int change ) |
| 470 | { |
| 471 | struct key *k; |
| 472 | |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 473 | key->modif = current_time; |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 474 | make_dirty( key ); |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 475 | |
| 476 | /* do notifications */ |
| 477 | check_notify( key, change, 1 ); |
| 478 | for ( k = key->parent; k; k = k->parent ) |
| 479 | check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* try to grow the array of subkeys; return 1 if OK, 0 on error */ |
| 483 | static int grow_subkeys( struct key *key ) |
| 484 | { |
| 485 | struct key **new_subkeys; |
| 486 | int nb_subkeys; |
| 487 | |
| 488 | if (key->nb_subkeys) |
| 489 | { |
| 490 | nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */ |
| 491 | if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) |
| 492 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 493 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | nb_subkeys = MIN_VALUES; |
| 500 | if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0; |
| 501 | } |
| 502 | key->subkeys = new_subkeys; |
| 503 | key->nb_subkeys = nb_subkeys; |
| 504 | return 1; |
| 505 | } |
| 506 | |
| 507 | /* allocate a subkey for a given key, and return its index */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 508 | static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name, |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 509 | int index, timeout_t modif ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 510 | { |
| 511 | struct key *key; |
| 512 | int i; |
| 513 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 514 | if (name->len > MAX_NAME_LEN * sizeof(WCHAR)) |
| 515 | { |
| 516 | set_error( STATUS_NAME_TOO_LONG ); |
| 517 | return NULL; |
| 518 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 519 | if (parent->last_subkey + 1 == parent->nb_subkeys) |
| 520 | { |
| 521 | /* need to grow the array */ |
| 522 | if (!grow_subkeys( parent )) return NULL; |
| 523 | } |
| 524 | if ((key = alloc_key( name, modif )) != NULL) |
| 525 | { |
| 526 | key->parent = parent; |
| 527 | for (i = ++parent->last_subkey; i > index; i--) |
| 528 | parent->subkeys[i] = parent->subkeys[i-1]; |
| 529 | parent->subkeys[index] = key; |
| 530 | } |
| 531 | return key; |
| 532 | } |
| 533 | |
| 534 | /* free a subkey of a given key */ |
| 535 | static void free_subkey( struct key *parent, int index ) |
| 536 | { |
| 537 | struct key *key; |
| 538 | int i, nb_subkeys; |
| 539 | |
| 540 | assert( index >= 0 ); |
| 541 | assert( index <= parent->last_subkey ); |
| 542 | |
| 543 | key = parent->subkeys[index]; |
| 544 | for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1]; |
| 545 | parent->last_subkey--; |
| 546 | key->flags |= KEY_DELETED; |
| 547 | key->parent = NULL; |
| 548 | release_object( key ); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 549 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 550 | /* try to shrink the array */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 551 | nb_subkeys = parent->nb_subkeys; |
| 552 | if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 553 | { |
| 554 | struct key **new_subkeys; |
| 555 | nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */ |
| 556 | if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS; |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 557 | if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return; |
| 558 | parent->subkeys = new_subkeys; |
| 559 | parent->nb_subkeys = nb_subkeys; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | /* find the named child of a given key and return its index */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 564 | static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 565 | { |
| 566 | int i, min, max, res; |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 567 | data_size_t len; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 568 | |
| 569 | min = 0; |
| 570 | max = key->last_subkey; |
| 571 | while (min <= max) |
| 572 | { |
| 573 | i = (min + max) / 2; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 574 | len = min( key->subkeys[i]->namelen, name->len ); |
| 575 | res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) ); |
| 576 | if (!res) res = key->subkeys[i]->namelen - name->len; |
| 577 | if (!res) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 578 | { |
| 579 | *index = i; |
| 580 | return key->subkeys[i]; |
| 581 | } |
| 582 | if (res > 0) max = i - 1; |
| 583 | else min = i + 1; |
| 584 | } |
| 585 | *index = min; /* this is where we should insert it */ |
| 586 | return NULL; |
| 587 | } |
| 588 | |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 589 | /* follow a symlink and return the resolved key */ |
| 590 | static struct key *follow_symlink( struct key *key, int iteration ) |
| 591 | { |
| 592 | struct unicode_str path, token; |
| 593 | struct key_value *value; |
| 594 | int index; |
| 595 | |
| 596 | if (iteration > 16) return NULL; |
| 597 | if (!(key->flags & KEY_SYMLINK)) return key; |
| 598 | if (!(value = find_value( key, &symlink_str, &index ))) return NULL; |
| 599 | |
| 600 | path.str = value->data; |
| 601 | path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR); |
| 602 | if (path.len <= sizeof(root_name)) return NULL; |
| 603 | if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL; |
| 604 | path.str += sizeof(root_name) / sizeof(WCHAR); |
| 605 | path.len -= sizeof(root_name); |
| 606 | |
| 607 | key = root_key; |
| 608 | token.str = NULL; |
| 609 | if (!get_path_token( &path, &token )) return NULL; |
| 610 | while (token.len) |
| 611 | { |
| 612 | if (!(key = find_subkey( key, &token, &index ))) break; |
| 613 | if (!(key = follow_symlink( key, iteration + 1 ))) break; |
| 614 | get_path_token( &path, &token ); |
| 615 | } |
| 616 | return key; |
| 617 | } |
| 618 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 619 | /* open a subkey */ |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 620 | static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int attributes ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 621 | { |
| 622 | int index; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 623 | struct unicode_str token; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 624 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 625 | token.str = NULL; |
| 626 | if (!get_path_token( name, &token )) return NULL; |
| 627 | while (token.len) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 628 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 629 | if (!(key = find_subkey( key, &token, &index ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 630 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 631 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 632 | return NULL; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 633 | } |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 634 | get_path_token( name, &token ); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 635 | if (!token.len) break; |
| 636 | if (!(key = follow_symlink( key, 0 ))) |
| 637 | { |
| 638 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
| 639 | return NULL; |
| 640 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 643 | if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 ))) |
| 644 | { |
| 645 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
| 646 | return NULL; |
| 647 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 648 | if (debug_level > 1) dump_operation( key, NULL, "Open" ); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 649 | grab_object( key ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 650 | return key; |
| 651 | } |
| 652 | |
| 653 | /* create a subkey */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 654 | static struct key *create_key( struct key *key, const struct unicode_str *name, |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 655 | const struct unicode_str *class, int flags, unsigned int options, |
| 656 | unsigned int attributes, timeout_t modif, int *created ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 657 | { |
| 658 | struct key *base; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 659 | int index; |
| 660 | struct unicode_str token; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 661 | |
| 662 | if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */ |
| 663 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 664 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 665 | return NULL; |
| 666 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 667 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 668 | token.str = NULL; |
| 669 | if (!get_path_token( name, &token )) return NULL; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 670 | *created = 0; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 671 | while (token.len) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 672 | { |
| 673 | struct key *subkey; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 674 | if (!(subkey = find_subkey( key, &token, &index ))) break; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 675 | key = subkey; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 676 | get_path_token( name, &token ); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 677 | if (!token.len) break; |
| 678 | if (!(subkey = follow_symlink( subkey, 0 ))) |
| 679 | { |
| 680 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
| 681 | return NULL; |
| 682 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* create the remaining part */ |
| 686 | |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 687 | if (!token.len) |
| 688 | { |
| 689 | if (options & REG_OPTION_CREATE_LINK) |
| 690 | { |
| 691 | set_error( STATUS_OBJECT_NAME_COLLISION ); |
| 692 | return NULL; |
| 693 | } |
| 694 | if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 ))) |
| 695 | { |
| 696 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
| 697 | return NULL; |
| 698 | } |
| 699 | goto done; |
| 700 | } |
| 701 | if (options & REG_OPTION_VOLATILE) |
| 702 | { |
| 703 | flags = (flags & ~KEY_DIRTY) | KEY_VOLATILE; |
| 704 | } |
| 705 | else if (key->flags & KEY_VOLATILE) |
Alexandre Julliard | 72ba00f | 2009-11-20 11:39:55 +0100 | [diff] [blame] | 706 | { |
| 707 | set_error( STATUS_CHILD_MUST_BE_VOLATILE ); |
| 708 | return NULL; |
| 709 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 710 | *created = 1; |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 711 | if (flags & KEY_DIRTY) make_dirty( key ); |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 712 | if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 713 | base = key; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 714 | for (;;) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 715 | { |
| 716 | key->flags |= flags; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 717 | get_path_token( name, &token ); |
| 718 | if (!token.len) break; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 719 | /* we know the index is always 0 in a new key */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 720 | if (!(key = alloc_subkey( key, &token, 0, modif ))) |
| 721 | { |
| 722 | free_subkey( base, index ); |
| 723 | return NULL; |
| 724 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 725 | } |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 726 | /* FIXME: no saving of symlinks yet */ |
| 727 | if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK | KEY_VOLATILE; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 728 | |
| 729 | done: |
| 730 | if (debug_level > 1) dump_operation( key, NULL, "Create" ); |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 731 | if (class && class->len) |
| 732 | { |
| 733 | key->classlen = class->len; |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 734 | free(key->class); |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 735 | if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0; |
| 736 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 737 | grab_object( key ); |
| 738 | return key; |
| 739 | } |
| 740 | |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 741 | /* query information about a key or a subkey */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 742 | static void enum_key( const struct key *key, int index, int info_class, |
| 743 | struct enum_key_reply *reply ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 744 | { |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 745 | int i; |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 746 | data_size_t len, namelen, classlen; |
Michael Stefaniuc | a624977 | 2006-07-25 10:30:04 +0200 | [diff] [blame] | 747 | data_size_t max_subkey = 0, max_class = 0; |
| 748 | data_size_t max_value = 0, max_data = 0; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 749 | char *data; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 750 | |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 751 | if (index != -1) /* -1 means use the specified key directly */ |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 752 | { |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 753 | if ((index < 0) || (index > key->last_subkey)) |
| 754 | { |
| 755 | set_error( STATUS_NO_MORE_ENTRIES ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 756 | return; |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 757 | } |
| 758 | key = key->subkeys[index]; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 759 | } |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 760 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 761 | namelen = key->namelen; |
| 762 | classlen = key->classlen; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 763 | |
| 764 | switch(info_class) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 765 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 766 | case KeyBasicInformation: |
| 767 | classlen = 0; /* only return the name */ |
| 768 | /* fall through */ |
| 769 | case KeyNodeInformation: |
| 770 | reply->max_subkey = 0; |
| 771 | reply->max_class = 0; |
| 772 | reply->max_value = 0; |
| 773 | reply->max_data = 0; |
| 774 | break; |
| 775 | case KeyFullInformation: |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 776 | for (i = 0; i <= key->last_subkey; i++) |
| 777 | { |
| 778 | struct key *subkey = key->subkeys[i]; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 779 | len = subkey->namelen / sizeof(WCHAR); |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 780 | if (len > max_subkey) max_subkey = len; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 781 | len = subkey->classlen / sizeof(WCHAR); |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 782 | if (len > max_class) max_class = len; |
| 783 | } |
| 784 | for (i = 0; i <= key->last_value; i++) |
| 785 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 786 | len = key->values[i].namelen / sizeof(WCHAR); |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 787 | if (len > max_value) max_value = len; |
| 788 | len = key->values[i].len; |
| 789 | if (len > max_data) max_data = len; |
| 790 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 791 | reply->max_subkey = max_subkey; |
| 792 | reply->max_class = max_class; |
| 793 | reply->max_value = max_value; |
| 794 | reply->max_data = max_data; |
| 795 | namelen = 0; /* only return the class */ |
| 796 | break; |
| 797 | default: |
| 798 | set_error( STATUS_INVALID_PARAMETER ); |
| 799 | return; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 800 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 801 | reply->subkeys = key->last_subkey + 1; |
| 802 | reply->values = key->last_value + 1; |
| 803 | reply->modif = key->modif; |
| 804 | reply->total = namelen + classlen; |
| 805 | |
| 806 | len = min( reply->total, get_reply_max_size() ); |
| 807 | if (len && (data = set_reply_data_size( len ))) |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 808 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 809 | if (len > namelen) |
| 810 | { |
| 811 | reply->namelen = namelen; |
| 812 | memcpy( data, key->name, namelen ); |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 813 | memcpy( data + namelen, key->class, len - namelen ); |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 814 | } |
| 815 | else |
| 816 | { |
| 817 | reply->namelen = len; |
| 818 | memcpy( data, key->name, len ); |
| 819 | } |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 820 | } |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 821 | if (debug_level > 1) dump_operation( key, NULL, "Enum" ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* delete a key and its values */ |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 825 | static int delete_key( struct key *key, int recurse ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 826 | { |
| 827 | int index; |
| 828 | struct key *parent; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 829 | |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 830 | /* must find parent and index */ |
Alexandre Julliard | 7e9757c | 2004-05-06 23:42:04 +0000 | [diff] [blame] | 831 | if (key == root_key) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 832 | { |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 833 | set_error( STATUS_ACCESS_DENIED ); |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 834 | return -1; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 835 | } |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 836 | if (!(parent = key->parent) || (key->flags & KEY_DELETED)) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 837 | { |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 838 | set_error( STATUS_KEY_DELETED ); |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 839 | return -1; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 840 | } |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 841 | |
| 842 | while (recurse && (key->last_subkey>=0)) |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 843 | if (0 > delete_key(key->subkeys[key->last_subkey], 1)) |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 844 | return -1; |
| 845 | |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 846 | for (index = 0; index <= parent->last_subkey; index++) |
| 847 | if (parent->subkeys[index] == key) break; |
| 848 | assert( index <= parent->last_subkey ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 849 | |
Alexandre Julliard | 7e9757c | 2004-05-06 23:42:04 +0000 | [diff] [blame] | 850 | /* we can only delete a key that has no subkeys */ |
| 851 | if (key->last_subkey >= 0) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 852 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 853 | set_error( STATUS_ACCESS_DENIED ); |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 854 | return -1; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 855 | } |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 856 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 857 | if (debug_level > 1) dump_operation( key, NULL, "Delete" ); |
| 858 | free_subkey( parent, index ); |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 859 | touch_key( parent, REG_NOTIFY_CHANGE_NAME ); |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 860 | return 0; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* try to grow the array of values; return 1 if OK, 0 on error */ |
| 864 | static int grow_values( struct key *key ) |
| 865 | { |
| 866 | struct key_value *new_val; |
| 867 | int nb_values; |
| 868 | |
| 869 | if (key->nb_values) |
| 870 | { |
| 871 | nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */ |
| 872 | if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) |
| 873 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 874 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | nb_values = MIN_VALUES; |
| 881 | if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0; |
| 882 | } |
| 883 | key->values = new_val; |
| 884 | key->nb_values = nb_values; |
| 885 | return 1; |
| 886 | } |
| 887 | |
| 888 | /* find the named value of a given key and return its index in the array */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 889 | static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 890 | { |
| 891 | int i, min, max, res; |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 892 | data_size_t len; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 893 | |
| 894 | min = 0; |
| 895 | max = key->last_value; |
| 896 | while (min <= max) |
| 897 | { |
| 898 | i = (min + max) / 2; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 899 | len = min( key->values[i].namelen, name->len ); |
| 900 | res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) ); |
| 901 | if (!res) res = key->values[i].namelen - name->len; |
| 902 | if (!res) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 903 | { |
| 904 | *index = i; |
| 905 | return &key->values[i]; |
| 906 | } |
| 907 | if (res > 0) max = i - 1; |
| 908 | else min = i + 1; |
| 909 | } |
| 910 | *index = min; /* this is where we should insert it */ |
| 911 | return NULL; |
| 912 | } |
| 913 | |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 914 | /* insert a new value; the index must have been returned by find_value */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 915 | static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 916 | { |
| 917 | struct key_value *value; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 918 | WCHAR *new_name = NULL; |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 919 | int i; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 920 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 921 | if (name->len > MAX_VALUE_LEN * sizeof(WCHAR)) |
| 922 | { |
| 923 | set_error( STATUS_NAME_TOO_LONG ); |
| 924 | return NULL; |
| 925 | } |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 926 | if (key->last_value + 1 == key->nb_values) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 927 | { |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 928 | if (!grow_values( key )) return NULL; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 929 | } |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 930 | if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL; |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 931 | for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1]; |
| 932 | value = &key->values[index]; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 933 | value->name = new_name; |
| 934 | value->namelen = name->len; |
| 935 | value->len = 0; |
| 936 | value->data = NULL; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 937 | return value; |
| 938 | } |
| 939 | |
| 940 | /* set a key value */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 941 | static void set_value( struct key *key, const struct unicode_str *name, |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 942 | int type, const void *data, data_size_t len ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 943 | { |
| 944 | struct key_value *value; |
| 945 | void *ptr = NULL; |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 946 | int index; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 947 | |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 948 | if ((value = find_value( key, name, &index ))) |
| 949 | { |
| 950 | /* check if the new value is identical to the existing one */ |
| 951 | if (value->type == type && value->len == len && |
| 952 | value->data && !memcmp( value->data, data, len )) |
| 953 | { |
| 954 | if (debug_level > 1) dump_operation( key, value, "Skip setting" ); |
| 955 | return; |
| 956 | } |
| 957 | } |
| 958 | |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 959 | if (key->flags & KEY_SYMLINK) |
| 960 | { |
| 961 | if (type != REG_LINK || name->len != symlink_str.len || |
| 962 | memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) )) |
| 963 | { |
| 964 | set_error( STATUS_ACCESS_DENIED ); |
| 965 | return; |
| 966 | } |
| 967 | } |
| 968 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 969 | if (len && !(ptr = memdup( data, len ))) return; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 970 | |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 971 | if (!value) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 972 | { |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 973 | if (!(value = insert_value( key, name, index ))) |
| 974 | { |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 975 | free( ptr ); |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 976 | return; |
| 977 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 978 | } |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 979 | else free( value->data ); /* already existing, free previous data */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 980 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 981 | value->type = type; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 982 | value->len = len; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 983 | value->data = ptr; |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 984 | touch_key( key, REG_NOTIFY_CHANGE_LAST_SET ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 985 | if (debug_level > 1) dump_operation( key, value, "Set" ); |
| 986 | } |
| 987 | |
| 988 | /* get a key value */ |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 989 | static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 990 | { |
| 991 | struct key_value *value; |
| 992 | int index; |
| 993 | |
| 994 | if ((value = find_value( key, name, &index ))) |
| 995 | { |
| 996 | *type = value->type; |
| 997 | *len = value->len; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 998 | if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() )); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 999 | if (debug_level > 1) dump_operation( key, value, "Get" ); |
| 1000 | } |
| 1001 | else |
| 1002 | { |
| 1003 | *type = -1; |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1004 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* enumerate a key value */ |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1009 | static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1010 | { |
| 1011 | struct key_value *value; |
| 1012 | |
Alexandre Julliard | ef88637 | 2000-04-04 19:33:27 +0000 | [diff] [blame] | 1013 | if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1014 | else |
| 1015 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1016 | void *data; |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 1017 | data_size_t namelen, maxlen; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1018 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1019 | value = &key->values[i]; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1020 | reply->type = value->type; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1021 | namelen = value->namelen; |
Alexandre Julliard | 0b6a79c | 2000-12-15 20:57:00 +0000 | [diff] [blame] | 1022 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1023 | switch(info_class) |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1024 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1025 | case KeyValueBasicInformation: |
| 1026 | reply->total = namelen; |
| 1027 | break; |
| 1028 | case KeyValueFullInformation: |
| 1029 | reply->total = namelen + value->len; |
| 1030 | break; |
| 1031 | case KeyValuePartialInformation: |
| 1032 | reply->total = value->len; |
| 1033 | namelen = 0; |
| 1034 | break; |
| 1035 | default: |
| 1036 | set_error( STATUS_INVALID_PARAMETER ); |
| 1037 | return; |
| 1038 | } |
Alexandre Julliard | 0b6a79c | 2000-12-15 20:57:00 +0000 | [diff] [blame] | 1039 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1040 | maxlen = min( reply->total, get_reply_max_size() ); |
| 1041 | if (maxlen && ((data = set_reply_data_size( maxlen )))) |
| 1042 | { |
| 1043 | if (maxlen > namelen) |
Alexandre Julliard | 0b6a79c | 2000-12-15 20:57:00 +0000 | [diff] [blame] | 1044 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1045 | reply->namelen = namelen; |
| 1046 | memcpy( data, value->name, namelen ); |
| 1047 | memcpy( (char *)data + namelen, value->data, maxlen - namelen ); |
Alexandre Julliard | 0b6a79c | 2000-12-15 20:57:00 +0000 | [diff] [blame] | 1048 | } |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1049 | else |
Alexandre Julliard | 0b6a79c | 2000-12-15 20:57:00 +0000 | [diff] [blame] | 1050 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1051 | reply->namelen = maxlen; |
| 1052 | memcpy( data, value->name, maxlen ); |
Alexandre Julliard | 0b6a79c | 2000-12-15 20:57:00 +0000 | [diff] [blame] | 1053 | } |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1054 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1055 | if (debug_level > 1) dump_operation( key, value, "Enum" ); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | /* delete a value */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1060 | static void delete_value( struct key *key, const struct unicode_str *name ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1061 | { |
| 1062 | struct key_value *value; |
| 1063 | int i, index, nb_values; |
| 1064 | |
| 1065 | if (!(value = find_value( key, name, &index ))) |
| 1066 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1067 | set_error( STATUS_OBJECT_NAME_NOT_FOUND ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1068 | return; |
| 1069 | } |
| 1070 | if (debug_level > 1) dump_operation( key, value, "Delete" ); |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 1071 | free( value->name ); |
| 1072 | free( value->data ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1073 | for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1]; |
| 1074 | key->last_value--; |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 1075 | touch_key( key, REG_NOTIFY_CHANGE_LAST_SET ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1076 | |
| 1077 | /* try to shrink the array */ |
| 1078 | nb_values = key->nb_values; |
| 1079 | if (nb_values > MIN_VALUES && key->last_value < nb_values / 2) |
| 1080 | { |
| 1081 | struct key_value *new_val; |
| 1082 | nb_values -= nb_values / 3; /* shrink by 33% */ |
| 1083 | if (nb_values < MIN_VALUES) nb_values = MIN_VALUES; |
| 1084 | if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return; |
| 1085 | key->values = new_val; |
| 1086 | key->nb_values = nb_values; |
| 1087 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 1088 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1089 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1090 | /* get the registry key corresponding to an hkey handle */ |
Alexandre Julliard | dd77d1a | 2006-07-10 11:53:23 +0200 | [diff] [blame] | 1091 | static inline struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access ) |
| 1092 | { |
| 1093 | return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops ); |
| 1094 | } |
| 1095 | |
| 1096 | /* get the registry key corresponding to a parent key handle */ |
| 1097 | static inline struct key *get_parent_hkey_obj( obj_handle_t hkey ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1098 | { |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 1099 | if (!hkey) return (struct key *)grab_object( root_key ); |
Alexandre Julliard | dd77d1a | 2006-07-10 11:53:23 +0200 | [diff] [blame] | 1100 | return (struct key *)get_handle_obj( current->process, hkey, 0, &key_ops ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1103 | /* read a line from the input file */ |
| 1104 | static int read_next_line( struct file_load_info *info ) |
| 1105 | { |
| 1106 | char *newbuf; |
| 1107 | int newlen, pos = 0; |
| 1108 | |
| 1109 | info->line++; |
| 1110 | for (;;) |
| 1111 | { |
| 1112 | if (!fgets( info->buffer + pos, info->len - pos, info->file )) |
| 1113 | return (pos != 0); /* EOF */ |
| 1114 | pos = strlen(info->buffer); |
| 1115 | if (info->buffer[pos-1] == '\n') |
| 1116 | { |
| 1117 | /* got a full line */ |
| 1118 | info->buffer[--pos] = 0; |
| 1119 | if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0; |
| 1120 | return 1; |
| 1121 | } |
| 1122 | if (pos < info->len - 1) return 1; /* EOF but something was read */ |
| 1123 | |
| 1124 | /* need to enlarge the buffer */ |
| 1125 | newlen = info->len + info->len / 2; |
| 1126 | if (!(newbuf = realloc( info->buffer, newlen ))) |
| 1127 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1128 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1129 | return -1; |
| 1130 | } |
| 1131 | info->buffer = newbuf; |
| 1132 | info->len = newlen; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | /* make sure the temp buffer holds enough space */ |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1137 | static int get_file_tmp_space( struct file_load_info *info, size_t size ) |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1138 | { |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1139 | WCHAR *tmp; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1140 | if (info->tmplen >= size) return 1; |
| 1141 | if (!(tmp = realloc( info->tmp, size ))) |
| 1142 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1143 | set_error( STATUS_NO_MEMORY ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1144 | return 0; |
| 1145 | } |
| 1146 | info->tmp = tmp; |
| 1147 | info->tmplen = size; |
| 1148 | return 1; |
| 1149 | } |
| 1150 | |
| 1151 | /* report an error while loading an input file */ |
| 1152 | static void file_read_error( const char *err, struct file_load_info *info ) |
| 1153 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1154 | if (info->filename) |
| 1155 | fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer ); |
| 1156 | else |
| 1157 | fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1160 | /* convert a data type tag to a value type */ |
| 1161 | static int get_data_type( const char *buffer, int *type, int *parse_type ) |
| 1162 | { |
| 1163 | struct data_type { const char *tag; int len; int type; int parse_type; }; |
| 1164 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1165 | static const struct data_type data_types[] = |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1166 | { /* actual type */ /* type to assume for parsing */ |
| 1167 | { "\"", 1, REG_SZ, REG_SZ }, |
| 1168 | { "str:\"", 5, REG_SZ, REG_SZ }, |
| 1169 | { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ }, |
| 1170 | { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ }, |
| 1171 | { "hex:", 4, REG_BINARY, REG_BINARY }, |
| 1172 | { "dword:", 6, REG_DWORD, REG_DWORD }, |
| 1173 | { "hex(", 4, -1, REG_BINARY }, |
Joerg Mayer | 959d73e | 2000-10-22 23:56:32 +0000 | [diff] [blame] | 1174 | { NULL, 0, 0, 0 } |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1175 | }; |
| 1176 | |
| 1177 | const struct data_type *ptr; |
| 1178 | char *end; |
| 1179 | |
| 1180 | for (ptr = data_types; ptr->tag; ptr++) |
| 1181 | { |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1182 | if (strncmp( ptr->tag, buffer, ptr->len )) continue; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1183 | *parse_type = ptr->parse_type; |
| 1184 | if ((*type = ptr->type) != -1) return ptr->len; |
| 1185 | /* "hex(xx):" is special */ |
| 1186 | *type = (int)strtoul( buffer + 4, &end, 16 ); |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1187 | if ((end <= buffer) || strncmp( end, "):", 2 )) return 0; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1188 | return end + 2 - buffer; |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | /* load and create a key from the input file */ |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1194 | static struct key *load_key( struct key *base, const char *buffer, int flags, |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 1195 | int prefix_len, struct file_load_info *info ) |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1196 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1197 | WCHAR *p; |
| 1198 | struct unicode_str name; |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 1199 | int res; |
| 1200 | unsigned int mod; |
| 1201 | timeout_t modif = current_time; |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1202 | data_size_t len; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1203 | |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1204 | if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1205 | |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1206 | len = info->tmplen; |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1207 | if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1) |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1208 | { |
| 1209 | file_read_error( "Malformed key", info ); |
| 1210 | return NULL; |
| 1211 | } |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 1212 | if (sscanf( buffer + res, " %u", &mod ) == 1) |
| 1213 | modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1214 | |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1215 | p = info->tmp; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1216 | while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; } |
| 1217 | |
| 1218 | if (!*p) |
| 1219 | { |
| 1220 | if (prefix_len > 1) |
| 1221 | { |
| 1222 | file_read_error( "Malformed key", info ); |
| 1223 | return NULL; |
| 1224 | } |
| 1225 | /* empty key name, return base key */ |
| 1226 | return (struct key *)grab_object( base ); |
| 1227 | } |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1228 | name.str = p; |
| 1229 | name.len = len - (p - info->tmp + 1) * sizeof(WCHAR); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 1230 | return create_key( base, &name, NULL, flags, 0, 0, modif, &res ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Alexandre Julliard | 2a37867 | 2010-02-16 12:26:15 +0100 | [diff] [blame^] | 1233 | /* load a key option from the input file */ |
| 1234 | static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info ) |
| 1235 | { |
| 1236 | const char *p; |
| 1237 | data_size_t len; |
| 1238 | |
| 1239 | if (!strncmp( buffer, "#class=", 7 )) |
| 1240 | { |
| 1241 | p = buffer + 7; |
| 1242 | if (*p++ != '"') return 0; |
| 1243 | if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0; |
| 1244 | len = info->tmplen; |
| 1245 | if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0; |
| 1246 | free( key->class ); |
| 1247 | if (!(key->class = memdup( info->tmp, len ))) len = 0; |
| 1248 | key->classlen = len; |
| 1249 | } |
| 1250 | /* ignore unknown options */ |
| 1251 | return 1; |
| 1252 | } |
| 1253 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1254 | /* parse a comma-separated list of hex digits */ |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 1255 | static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer ) |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1256 | { |
| 1257 | const char *p = buffer; |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 1258 | data_size_t count = 0; |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1259 | char *end; |
| 1260 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1261 | while (isxdigit(*p)) |
| 1262 | { |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1263 | unsigned int val = strtoul( p, &end, 16 ); |
| 1264 | if (end == p || val > 0xff) return -1; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1265 | if (count++ >= *len) return -1; /* dest buffer overflow */ |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1266 | *dest++ = val; |
| 1267 | p = end; |
| 1268 | while (isspace(*p)) p++; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1269 | if (*p == ',') p++; |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1270 | while (isspace(*p)) p++; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1271 | } |
| 1272 | *len = count; |
| 1273 | return p - buffer; |
| 1274 | } |
| 1275 | |
| 1276 | /* parse a value name and create the corresponding value */ |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 1277 | static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len, |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1278 | struct file_load_info *info ) |
| 1279 | { |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1280 | struct key_value *value; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1281 | struct unicode_str name; |
| 1282 | int index; |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1283 | |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1284 | if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1285 | name.str = info->tmp; |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1286 | name.len = info->tmplen; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1287 | if (buffer[0] == '@') |
| 1288 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1289 | name.len = 0; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1290 | *len = 1; |
| 1291 | } |
| 1292 | else |
| 1293 | { |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1294 | int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' ); |
Mike McCormack | 14278b4 | 2006-04-07 15:06:15 +0900 | [diff] [blame] | 1295 | if (r == -1) goto error; |
| 1296 | *len = r + 1; /* for initial quote */ |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1297 | name.len -= sizeof(WCHAR); /* terminating null */ |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1298 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 1299 | while (isspace(buffer[*len])) (*len)++; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1300 | if (buffer[*len] != '=') goto error; |
| 1301 | (*len)++; |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 1302 | while (isspace(buffer[*len])) (*len)++; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1303 | if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index ); |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1304 | return value; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1305 | |
| 1306 | error: |
| 1307 | file_read_error( "Malformed value name", info ); |
| 1308 | return NULL; |
| 1309 | } |
| 1310 | |
| 1311 | /* load a value from the input file */ |
| 1312 | static int load_value( struct key *key, const char *buffer, struct file_load_info *info ) |
| 1313 | { |
| 1314 | DWORD dw; |
| 1315 | void *ptr, *newptr; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1316 | int res, type, parse_type; |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 1317 | data_size_t maxlen, len; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1318 | struct key_value *value; |
| 1319 | |
| 1320 | if (!(value = parse_value_name( key, buffer, &len, info ))) return 0; |
| 1321 | if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error; |
| 1322 | buffer += len + res; |
| 1323 | |
| 1324 | switch(parse_type) |
| 1325 | { |
| 1326 | case REG_SZ: |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1327 | if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0; |
| 1328 | len = info->tmplen; |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1329 | if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1330 | ptr = info->tmp; |
| 1331 | break; |
| 1332 | case REG_DWORD: |
| 1333 | dw = strtoul( buffer, NULL, 16 ); |
| 1334 | ptr = &dw; |
| 1335 | len = sizeof(dw); |
| 1336 | break; |
| 1337 | case REG_BINARY: /* hex digits */ |
| 1338 | len = 0; |
| 1339 | for (;;) |
| 1340 | { |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1341 | maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */ |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1342 | if (!get_file_tmp_space( info, len + maxlen )) return 0; |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1343 | if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1344 | len += maxlen; |
| 1345 | buffer += res; |
| 1346 | while (isspace(*buffer)) buffer++; |
| 1347 | if (!*buffer) break; |
| 1348 | if (*buffer != '\\') goto error; |
| 1349 | if (read_next_line( info) != 1) goto error; |
| 1350 | buffer = info->buffer; |
| 1351 | while (isspace(*buffer)) buffer++; |
| 1352 | } |
| 1353 | ptr = info->tmp; |
| 1354 | break; |
| 1355 | default: |
| 1356 | assert(0); |
| 1357 | ptr = NULL; /* keep compiler quiet */ |
| 1358 | break; |
| 1359 | } |
| 1360 | |
| 1361 | if (!len) newptr = NULL; |
| 1362 | else if (!(newptr = memdup( ptr, len ))) return 0; |
| 1363 | |
Michael Stefaniuc | 5ceccec | 2006-10-09 23:34:36 +0200 | [diff] [blame] | 1364 | free( value->data ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1365 | value->data = newptr; |
| 1366 | value->len = len; |
| 1367 | value->type = type; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1368 | return 1; |
| 1369 | |
| 1370 | error: |
| 1371 | file_read_error( "Malformed value", info ); |
Alexandre Julliard | fb9955d | 2008-01-07 21:06:01 +0100 | [diff] [blame] | 1372 | free( value->data ); |
| 1373 | value->data = NULL; |
| 1374 | value->len = 0; |
| 1375 | value->type = REG_NONE; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1376 | return 0; |
| 1377 | } |
| 1378 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1379 | /* return the length (in path elements) of name that is part of the key name */ |
| 1380 | /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */ |
| 1381 | static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info ) |
| 1382 | { |
| 1383 | WCHAR *p; |
| 1384 | int res; |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1385 | data_size_t len; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1386 | |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1387 | if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1388 | |
Alexandre Julliard | 1271739 | 2008-01-07 20:17:24 +0100 | [diff] [blame] | 1389 | len = info->tmplen; |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1390 | if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1) |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1391 | { |
| 1392 | file_read_error( "Malformed key", info ); |
Patrik Stridvall | 17d1e9e | 2000-05-23 23:38:32 +0000 | [diff] [blame] | 1393 | return 0; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1394 | } |
Alexandre Julliard | b0e091a | 2005-08-09 10:36:45 +0000 | [diff] [blame] | 1395 | for (p = info->tmp; *p; p++) if (*p == '\\') break; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1396 | len = (p - info->tmp) * sizeof(WCHAR); |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 1397 | for (res = 1; key != root_key; res++) |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1398 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1399 | if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1400 | key = key->parent; |
| 1401 | } |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 1402 | if (key == root_key) res = 0; /* no matching name */ |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1403 | return res; |
| 1404 | } |
| 1405 | |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1406 | /* load all the keys from the input file */ |
Alexandre Julliard | 58447bc | 2004-04-30 18:35:13 +0000 | [diff] [blame] | 1407 | /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1408 | static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len ) |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1409 | { |
| 1410 | struct key *subkey = NULL; |
| 1411 | struct file_load_info info; |
| 1412 | char *p; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1413 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1414 | info.filename = filename; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1415 | info.file = f; |
| 1416 | info.len = 4; |
| 1417 | info.tmplen = 4; |
| 1418 | info.line = 0; |
| 1419 | if (!(info.buffer = mem_alloc( info.len ))) return; |
| 1420 | if (!(info.tmp = mem_alloc( info.tmplen ))) |
| 1421 | { |
| 1422 | free( info.buffer ); |
| 1423 | return; |
| 1424 | } |
| 1425 | |
| 1426 | if ((read_next_line( &info ) != 1) || |
| 1427 | strcmp( info.buffer, "WINE REGISTRY Version 2" )) |
| 1428 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1429 | set_error( STATUS_NOT_REGISTRY_FILE ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1430 | goto done; |
| 1431 | } |
| 1432 | |
| 1433 | while (read_next_line( &info ) == 1) |
| 1434 | { |
Alexandre Julliard | 566a52a | 2001-03-05 19:34:17 +0000 | [diff] [blame] | 1435 | p = info.buffer; |
| 1436 | while (*p && isspace(*p)) p++; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1437 | switch(*p) |
| 1438 | { |
| 1439 | case '[': /* new key */ |
| 1440 | if (subkey) release_object( subkey ); |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1441 | if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info ); |
Paul Chitescu | 51000c1 | 2009-11-24 15:12:56 +0200 | [diff] [blame] | 1442 | if (!(subkey = load_key( key, p + 1, key->flags, prefix_len, &info ))) |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1443 | file_read_error( "Error creating key", &info ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | case '@': /* default value */ |
| 1446 | case '\"': /* value */ |
| 1447 | if (subkey) load_value( subkey, p, &info ); |
| 1448 | else file_read_error( "Value without key", &info ); |
| 1449 | break; |
Alexandre Julliard | 2a37867 | 2010-02-16 12:26:15 +0100 | [diff] [blame^] | 1450 | case '#': /* option */ |
| 1451 | if (subkey) load_key_option( subkey, p, &info ); |
| 1452 | break; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1453 | case ';': /* comment */ |
| 1454 | case 0: /* empty line */ |
| 1455 | break; |
| 1456 | default: |
| 1457 | file_read_error( "Unrecognized input", &info ); |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | done: |
| 1463 | if (subkey) release_object( subkey ); |
| 1464 | free( info.buffer ); |
| 1465 | free( info.tmp ); |
| 1466 | } |
| 1467 | |
| 1468 | /* load a part of the registry from a file */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 1469 | static void load_registry( struct key *key, obj_handle_t handle ) |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1470 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1471 | struct file *file; |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1472 | int fd; |
| 1473 | |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 1474 | if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1475 | fd = dup( get_file_unix_fd( file ) ); |
| 1476 | release_object( file ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1477 | if (fd != -1) |
| 1478 | { |
| 1479 | FILE *f = fdopen( fd, "r" ); |
| 1480 | if (f) |
| 1481 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1482 | load_keys( key, NULL, f, -1 ); |
Alexandre Julliard | 53f3a83 | 1999-11-24 04:19:43 +0000 | [diff] [blame] | 1483 | fclose( f ); |
| 1484 | } |
| 1485 | else file_set_error(); |
| 1486 | } |
| 1487 | } |
| 1488 | |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1489 | /* load one of the initial registry files */ |
| 1490 | static void load_init_registry_from_file( const char *filename, struct key *key ) |
| 1491 | { |
| 1492 | FILE *f; |
| 1493 | |
Alexandre Julliard | 5b23efc | 2004-05-14 00:45:11 +0000 | [diff] [blame] | 1494 | if ((f = fopen( filename, "r" ))) |
| 1495 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1496 | load_keys( key, filename, f, 0 ); |
Alexandre Julliard | 5b23efc | 2004-05-14 00:45:11 +0000 | [diff] [blame] | 1497 | fclose( f ); |
| 1498 | if (get_error() == STATUS_NOT_REGISTRY_FILE) |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1499 | { |
| 1500 | fprintf( stderr, "%s is not a valid registry file\n", filename ); |
| 1501 | return; |
| 1502 | } |
Alexandre Julliard | 5b23efc | 2004-05-14 00:45:11 +0000 | [diff] [blame] | 1503 | } |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1504 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1505 | assert( save_branch_count < MAX_SAVE_BRANCH_INFO ); |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1506 | |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1507 | save_branch_info[save_branch_count].path = filename; |
| 1508 | save_branch_info[save_branch_count++].key = (struct key *)grab_object( key ); |
| 1509 | make_object_static( &key->obj ); |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1512 | static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path ) |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1513 | { |
| 1514 | static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0}; |
| 1515 | static const WCHAR formatW[] = {'-','%','u',0}; |
| 1516 | WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES]; |
| 1517 | WCHAR *p = buffer; |
| 1518 | unsigned int i; |
| 1519 | |
| 1520 | strcpyW( p, prefixW ); |
| 1521 | p += strlenW( prefixW ); |
| 1522 | p += sprintfW( p, formatW, sid->Revision ); |
| 1523 | p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5], |
| 1524 | sid->IdentifierAuthority.Value[4] ), |
| 1525 | MAKEWORD( sid->IdentifierAuthority.Value[3], |
| 1526 | sid->IdentifierAuthority.Value[2] ))); |
| 1527 | for (i = 0; i < sid->SubAuthorityCount; i++) |
| 1528 | p += sprintfW( p, formatW, sid->SubAuthority[i] ); |
| 1529 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1530 | path->len = (p - buffer) * sizeof(WCHAR); |
| 1531 | path->str = p = memdup( buffer, path->len ); |
| 1532 | return p; |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Alexandre Julliard | 27f0f80 | 2005-04-20 12:57:53 +0000 | [diff] [blame] | 1535 | /* registry initialisation */ |
| 1536 | void init_registry(void) |
| 1537 | { |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1538 | static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' }; |
| 1539 | static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' }; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1540 | static const struct unicode_str root_name = { NULL, 0 }; |
| 1541 | static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) }; |
| 1542 | static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) }; |
| 1543 | |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1544 | WCHAR *current_user_path; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1545 | struct unicode_str current_user_str; |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1546 | |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1547 | struct key *key; |
| 1548 | int dummy; |
| 1549 | |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1550 | /* switch to the config dir */ |
| 1551 | |
| 1552 | if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" ); |
| 1553 | |
Alexandre Julliard | 27f0f80 | 2005-04-20 12:57:53 +0000 | [diff] [blame] | 1554 | /* create the root key */ |
Alexandre Julliard | 3343c40 | 2008-12-06 17:29:01 +0100 | [diff] [blame] | 1555 | root_key = alloc_key( &root_name, current_time ); |
Alexandre Julliard | 27f0f80 | 2005-04-20 12:57:53 +0000 | [diff] [blame] | 1556 | assert( root_key ); |
Alexandre Julliard | b00fb17 | 2006-03-22 20:32:04 +0100 | [diff] [blame] | 1557 | make_object_static( &root_key->obj ); |
Alexandre Julliard | 27f0f80 | 2005-04-20 12:57:53 +0000 | [diff] [blame] | 1558 | |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1559 | /* load system.reg into Registry\Machine */ |
| 1560 | |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 1561 | if (!(key = create_key( root_key, &HKLM_name, NULL, 0, 0, 0, current_time, &dummy ))) |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1562 | fatal_error( "could not create Machine registry key\n" ); |
| 1563 | |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1564 | load_init_registry_from_file( "system.reg", key ); |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1565 | release_object( key ); |
| 1566 | |
| 1567 | /* load userdef.reg into Registry\User\.Default */ |
| 1568 | |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 1569 | if (!(key = create_key( root_key, &HKU_name, NULL, 0, 0, 0, current_time, &dummy ))) |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1570 | fatal_error( "could not create User\\.Default registry key\n" ); |
| 1571 | |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1572 | load_init_registry_from_file( "userdef.reg", key ); |
Alexandre Julliard | c07ce05 | 2004-05-07 04:13:21 +0000 | [diff] [blame] | 1573 | release_object( key ); |
| 1574 | |
Alexandre Julliard | 6aa0cc5 | 2005-07-11 20:44:59 +0000 | [diff] [blame] | 1575 | /* load user.reg into HKEY_CURRENT_USER */ |
| 1576 | |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1577 | /* FIXME: match default user in token.c. should get from process token instead */ |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1578 | current_user_path = format_user_registry_path( security_interactive_sid, ¤t_user_str ); |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1579 | if (!current_user_path || |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 1580 | !(key = create_key( root_key, ¤t_user_str, NULL, 0, 0, 0, current_time, &dummy ))) |
Alexandre Julliard | 6aa0cc5 | 2005-07-11 20:44:59 +0000 | [diff] [blame] | 1581 | fatal_error( "could not create HKEY_CURRENT_USER registry key\n" ); |
Robert Shearman | 91eaea5 | 2005-07-18 13:22:55 +0000 | [diff] [blame] | 1582 | free( current_user_path ); |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1583 | load_init_registry_from_file( "user.reg", key ); |
Alexandre Julliard | 6aa0cc5 | 2005-07-11 20:44:59 +0000 | [diff] [blame] | 1584 | release_object( key ); |
| 1585 | |
Alexandre Julliard | 6aa0cc5 | 2005-07-11 20:44:59 +0000 | [diff] [blame] | 1586 | /* start the periodic save timer */ |
| 1587 | set_periodic_save_timer(); |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1588 | |
| 1589 | /* go back to the server dir */ |
| 1590 | if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" ); |
Alexandre Julliard | 6c8d917 | 2000-08-26 04:40:07 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1593 | /* save a registry branch to a file */ |
| 1594 | static void save_all_subkeys( struct key *key, FILE *f ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1595 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1596 | fprintf( f, "WINE REGISTRY Version 2\n" ); |
| 1597 | fprintf( f, ";; All keys relative to " ); |
| 1598 | dump_path( key, NULL, f ); |
| 1599 | fprintf( f, "\n" ); |
| 1600 | save_subkeys( key, key, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /* save a registry branch to a file handle */ |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 1604 | static void save_registry( struct key *key, obj_handle_t handle ) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1605 | { |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1606 | struct file *file; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1607 | int fd; |
| 1608 | |
| 1609 | if (key->flags & KEY_DELETED) |
| 1610 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 1611 | set_error( STATUS_KEY_DELETED ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1612 | return; |
| 1613 | } |
Alexandre Julliard | a510a7e | 2005-12-12 16:46:17 +0100 | [diff] [blame] | 1614 | if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return; |
Alexandre Julliard | cf27a7f | 2003-02-14 20:27:09 +0000 | [diff] [blame] | 1615 | fd = dup( get_file_unix_fd( file ) ); |
| 1616 | release_object( file ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1617 | if (fd != -1) |
| 1618 | { |
| 1619 | FILE *f = fdopen( fd, "w" ); |
| 1620 | if (f) |
| 1621 | { |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1622 | save_all_subkeys( key, f ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1623 | if (fclose( f )) file_set_error(); |
| 1624 | } |
| 1625 | else |
| 1626 | { |
| 1627 | file_set_error(); |
| 1628 | close( fd ); |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1633 | /* save a registry branch to a file */ |
| 1634 | static int save_branch( struct key *key, const char *path ) |
| 1635 | { |
Bill Medland | 309566d | 2002-12-18 05:03:51 +0000 | [diff] [blame] | 1636 | struct stat st; |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1637 | char *p, *tmp = NULL; |
| 1638 | int fd, count = 0, ret = 0; |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1639 | FILE *f; |
| 1640 | |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1641 | if (!(key->flags & KEY_DIRTY)) |
| 1642 | { |
| 1643 | if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" ); |
| 1644 | return 1; |
| 1645 | } |
| 1646 | |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1647 | /* test the file type */ |
| 1648 | |
| 1649 | if ((fd = open( path, O_WRONLY )) != -1) |
| 1650 | { |
Bill Medland | 309566d | 2002-12-18 05:03:51 +0000 | [diff] [blame] | 1651 | /* if file is not a regular file or has multiple links or is accessed |
| 1652 | * via symbolic links, write directly into it; otherwise use a temp file */ |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1653 | if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)) |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1654 | { |
| 1655 | ftruncate( fd, 0 ); |
| 1656 | goto save; |
| 1657 | } |
| 1658 | close( fd ); |
| 1659 | } |
| 1660 | |
| 1661 | /* create a temp file in the same directory */ |
| 1662 | |
| 1663 | if (!(tmp = malloc( strlen(path) + 20 ))) goto done; |
| 1664 | strcpy( tmp, path ); |
| 1665 | if ((p = strrchr( tmp, '/' ))) p++; |
| 1666 | else p = tmp; |
| 1667 | for (;;) |
| 1668 | { |
Patrik Stridvall | e29dbc5 | 2000-04-24 18:04:24 +0000 | [diff] [blame] | 1669 | sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1670 | if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break; |
| 1671 | if (errno != EEXIST) goto done; |
| 1672 | close( fd ); |
| 1673 | } |
| 1674 | |
| 1675 | /* now save to it */ |
| 1676 | |
| 1677 | save: |
| 1678 | if (!(f = fdopen( fd, "w" ))) |
| 1679 | { |
| 1680 | if (tmp) unlink( tmp ); |
| 1681 | close( fd ); |
| 1682 | goto done; |
| 1683 | } |
| 1684 | |
| 1685 | if (debug_level > 1) |
| 1686 | { |
| 1687 | fprintf( stderr, "%s: ", path ); |
| 1688 | dump_operation( key, NULL, "saving" ); |
| 1689 | } |
| 1690 | |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1691 | save_all_subkeys( key, f ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1692 | ret = !fclose(f); |
| 1693 | |
| 1694 | if (tmp) |
| 1695 | { |
| 1696 | /* if successfully written, rename to final name */ |
| 1697 | if (ret) ret = !rename( tmp, path ); |
| 1698 | if (!ret) unlink( tmp ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | done: |
Peter Berg Larsen | 514f432 | 2005-03-10 11:18:31 +0000 | [diff] [blame] | 1702 | free( tmp ); |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1703 | if (ret) make_clean( key ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1704 | return ret; |
| 1705 | } |
| 1706 | |
| 1707 | /* periodic saving of the registry */ |
| 1708 | static void periodic_save( void *arg ) |
| 1709 | { |
| 1710 | int i; |
Alexandre Julliard | dcab706 | 2005-03-14 11:00:43 +0000 | [diff] [blame] | 1711 | |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1712 | if (fchdir( config_dir_fd ) == -1) return; |
Alexandre Julliard | dcab706 | 2005-03-14 11:00:43 +0000 | [diff] [blame] | 1713 | save_timeout_user = NULL; |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1714 | for (i = 0; i < save_branch_count; i++) |
| 1715 | save_branch( save_branch_info[i].key, save_branch_info[i].path ); |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1716 | if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" ); |
Alexandre Julliard | dcab706 | 2005-03-14 11:00:43 +0000 | [diff] [blame] | 1717 | set_periodic_save_timer(); |
| 1718 | } |
| 1719 | |
| 1720 | /* start the periodic save timer */ |
| 1721 | static void set_periodic_save_timer(void) |
| 1722 | { |
Alexandre Julliard | dcab706 | 2005-03-14 11:00:43 +0000 | [diff] [blame] | 1723 | if (save_timeout_user) remove_timeout_user( save_timeout_user ); |
Alexandre Julliard | aaf477f | 2007-04-17 20:08:59 +0200 | [diff] [blame] | 1724 | save_timeout_user = add_timeout_user( save_period, periodic_save, NULL ); |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1727 | /* save the modified registry branches to disk */ |
| 1728 | void flush_registry(void) |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1729 | { |
| 1730 | int i; |
| 1731 | |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1732 | if (fchdir( config_dir_fd ) == -1) return; |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1733 | for (i = 0; i < save_branch_count; i++) |
| 1734 | { |
| 1735 | if (!save_branch( save_branch_info[i].key, save_branch_info[i].path )) |
| 1736 | { |
| 1737 | fprintf( stderr, "wineserver: could not save registry branch to %s", |
| 1738 | save_branch_info[i].path ); |
| 1739 | perror( " " ); |
| 1740 | } |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1741 | } |
Alexandre Julliard | 161160f | 2008-04-17 12:41:34 +0200 | [diff] [blame] | 1742 | if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" ); |
Alexandre Julliard | 88e4261 | 2002-06-20 23:18:56 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Alexandre Julliard | c970904 | 2000-04-16 17:21:13 +0000 | [diff] [blame] | 1745 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1746 | /* create a registry key */ |
| 1747 | DECL_HANDLER(create_key) |
| 1748 | { |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 1749 | struct key *key = NULL, *parent; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1750 | struct unicode_str name, class; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1751 | unsigned int access = req->access; |
| 1752 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1753 | reply->hkey = 0; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1754 | |
| 1755 | if (req->namelen > get_req_data_size()) |
| 1756 | { |
| 1757 | set_error( STATUS_INVALID_PARAMETER ); |
| 1758 | return; |
| 1759 | } |
| 1760 | class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR); |
| 1761 | class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR); |
| 1762 | get_req_path( &name, !req->parent ); |
| 1763 | if (name.str > class.str) |
| 1764 | { |
| 1765 | set_error( STATUS_INVALID_PARAMETER ); |
| 1766 | return; |
| 1767 | } |
| 1768 | name.len = (class.str - name.str) * sizeof(WCHAR); |
| 1769 | |
Robert Shearman | 8cb3f92 | 2005-06-16 20:34:34 +0000 | [diff] [blame] | 1770 | /* NOTE: no access rights are required from the parent handle to create a key */ |
Alexandre Julliard | dd77d1a | 2006-07-10 11:53:23 +0200 | [diff] [blame] | 1771 | if ((parent = get_parent_hkey_obj( req->parent ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1772 | { |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 1773 | if ((key = create_key( parent, &name, &class, KEY_DIRTY, req->options, |
| 1774 | req->attributes, current_time, &reply->created ))) |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 1775 | { |
Alexandre Julliard | 03b040c | 2005-12-09 14:52:04 +0100 | [diff] [blame] | 1776 | reply->hkey = alloc_handle( current->process, key, access, req->attributes ); |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 1777 | release_object( key ); |
| 1778 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1779 | release_object( parent ); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /* open a registry key */ |
| 1784 | DECL_HANDLER(open_key) |
| 1785 | { |
| 1786 | struct key *key, *parent; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1787 | struct unicode_str name; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1788 | unsigned int access = req->access; |
| 1789 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1790 | reply->hkey = 0; |
Robert Shearman | b3957e3 | 2005-05-05 16:57:55 +0000 | [diff] [blame] | 1791 | /* NOTE: no access rights are required to open the parent key, only the child key */ |
Alexandre Julliard | dd77d1a | 2006-07-10 11:53:23 +0200 | [diff] [blame] | 1792 | if ((parent = get_parent_hkey_obj( req->parent ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1793 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1794 | get_req_path( &name, !req->parent ); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 1795 | if ((key = open_key( parent, &name, req->attributes ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1796 | { |
Alexandre Julliard | 03b040c | 2005-12-09 14:52:04 +0100 | [diff] [blame] | 1797 | reply->hkey = alloc_handle( current->process, key, access, req->attributes ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1798 | release_object( key ); |
| 1799 | } |
| 1800 | release_object( parent ); |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | /* delete a registry key */ |
| 1805 | DECL_HANDLER(delete_key) |
| 1806 | { |
| 1807 | struct key *key; |
| 1808 | |
Robert Shearman | b3957e3 | 2005-05-05 16:57:55 +0000 | [diff] [blame] | 1809 | if ((key = get_hkey_obj( req->hkey, DELETE ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1810 | { |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 1811 | delete_key( key, 0); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1812 | release_object( key ); |
| 1813 | } |
| 1814 | } |
| 1815 | |
Mike Hearn | 43cb03b | 2004-01-03 00:38:30 +0000 | [diff] [blame] | 1816 | /* flush a registry key */ |
| 1817 | DECL_HANDLER(flush_key) |
| 1818 | { |
| 1819 | struct key *key = get_hkey_obj( req->hkey, 0 ); |
| 1820 | if (key) |
| 1821 | { |
| 1822 | /* we don't need to do anything here with the current implementation */ |
| 1823 | release_object( key ); |
| 1824 | } |
| 1825 | } |
| 1826 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1827 | /* enumerate registry subkeys */ |
| 1828 | DECL_HANDLER(enum_key) |
| 1829 | { |
| 1830 | struct key *key; |
| 1831 | |
Alexandre Julliard | 454355e | 2000-10-02 03:46:58 +0000 | [diff] [blame] | 1832 | if ((key = get_hkey_obj( req->hkey, |
| 1833 | req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1834 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1835 | enum_key( key, req->index, req->info_class, reply ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1836 | release_object( key ); |
| 1837 | } |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /* set a value of a registry key */ |
| 1841 | DECL_HANDLER(set_key_value) |
| 1842 | { |
| 1843 | struct key *key; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1844 | struct unicode_str name; |
Alexandre Julliard | a01004d | 2000-05-14 22:57:57 +0000 | [diff] [blame] | 1845 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1846 | if (req->namelen > get_req_data_size()) |
| 1847 | { |
| 1848 | set_error( STATUS_INVALID_PARAMETER ); |
| 1849 | return; |
| 1850 | } |
| 1851 | name.str = get_req_data(); |
| 1852 | name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR); |
| 1853 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1854 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE ))) |
| 1855 | { |
Alexandre Julliard | 0f273c1 | 2006-07-26 10:43:25 +0200 | [diff] [blame] | 1856 | data_size_t datalen = get_req_data_size() - req->namelen; |
Eric Pouech | 0a25896 | 2004-11-30 21:38:57 +0000 | [diff] [blame] | 1857 | const char *data = (const char *)get_req_data() + req->namelen; |
Alexandre Julliard | bcf393a | 2000-10-01 01:44:50 +0000 | [diff] [blame] | 1858 | |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1859 | set_value( key, &name, req->type, data, datalen ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1860 | release_object( key ); |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* retrieve the value of a registry key */ |
| 1865 | DECL_HANDLER(get_key_value) |
| 1866 | { |
| 1867 | struct key *key; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1868 | struct unicode_str name; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1869 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1870 | reply->total = 0; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1871 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1872 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1873 | get_req_unicode_str( &name ); |
| 1874 | get_value( key, &name, &reply->type, &reply->total ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1875 | release_object( key ); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | /* enumerate the value of a registry key */ |
| 1880 | DECL_HANDLER(enum_key_value) |
| 1881 | { |
| 1882 | struct key *key; |
| 1883 | |
| 1884 | if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE ))) |
| 1885 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 1886 | enum_value( key, req->index, req->info_class, reply ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1887 | release_object( key ); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | /* delete a value of a registry key */ |
| 1892 | DECL_HANDLER(delete_key_value) |
| 1893 | { |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1894 | struct key *key; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1895 | struct unicode_str name; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1896 | |
| 1897 | if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE ))) |
| 1898 | { |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1899 | get_req_unicode_str( &name ); |
| 1900 | delete_value( key, &name ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1901 | release_object( key ); |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | /* load a registry branch from a file */ |
| 1906 | DECL_HANDLER(load_registry) |
| 1907 | { |
James Hawkins | 580ded6 | 2005-03-29 11:38:58 +0000 | [diff] [blame] | 1908 | struct key *key, *parent; |
Robert Shearman | b3957e3 | 2005-05-05 16:57:55 +0000 | [diff] [blame] | 1909 | struct token *token = thread_get_impersonation_token( current ); |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1910 | struct unicode_str name; |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1911 | |
Robert Shearman | b3957e3 | 2005-05-05 16:57:55 +0000 | [diff] [blame] | 1912 | const LUID_AND_ATTRIBUTES privs[] = |
| 1913 | { |
| 1914 | { SeBackupPrivilege, 0 }, |
| 1915 | { SeRestorePrivilege, 0 }, |
| 1916 | }; |
| 1917 | |
| 1918 | if (!token || !token_check_privileges( token, TRUE, privs, |
| 1919 | sizeof(privs)/sizeof(privs[0]), NULL )) |
| 1920 | { |
| 1921 | set_error( STATUS_PRIVILEGE_NOT_HELD ); |
| 1922 | return; |
| 1923 | } |
| 1924 | |
Alexandre Julliard | dd77d1a | 2006-07-10 11:53:23 +0200 | [diff] [blame] | 1925 | if ((parent = get_parent_hkey_obj( req->hkey ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1926 | { |
James Hawkins | 580ded6 | 2005-03-29 11:38:58 +0000 | [diff] [blame] | 1927 | int dummy; |
Alexandre Julliard | 7f9e281 | 2005-11-22 12:05:36 +0000 | [diff] [blame] | 1928 | get_req_path( &name, !req->hkey ); |
Alexandre Julliard | b139b93 | 2010-02-15 21:04:26 +0100 | [diff] [blame] | 1929 | if ((key = create_key( parent, &name, NULL, KEY_DIRTY, 0, 0, current_time, &dummy ))) |
James Hawkins | 580ded6 | 2005-03-29 11:38:58 +0000 | [diff] [blame] | 1930 | { |
| 1931 | load_registry( key, req->file ); |
| 1932 | release_object( key ); |
| 1933 | } |
| 1934 | release_object( parent ); |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 1938 | DECL_HANDLER(unload_registry) |
| 1939 | { |
| 1940 | struct key *key; |
Robert Shearman | b3957e3 | 2005-05-05 16:57:55 +0000 | [diff] [blame] | 1941 | struct token *token = thread_get_impersonation_token( current ); |
| 1942 | |
| 1943 | const LUID_AND_ATTRIBUTES privs[] = |
| 1944 | { |
| 1945 | { SeBackupPrivilege, 0 }, |
| 1946 | { SeRestorePrivilege, 0 }, |
| 1947 | }; |
| 1948 | |
| 1949 | if (!token || !token_check_privileges( token, TRUE, privs, |
| 1950 | sizeof(privs)/sizeof(privs[0]), NULL )) |
| 1951 | { |
| 1952 | set_error( STATUS_PRIVILEGE_NOT_HELD ); |
| 1953 | return; |
| 1954 | } |
Mike McCormack | 5ac945c | 2003-08-19 03:08:17 +0000 | [diff] [blame] | 1955 | |
| 1956 | if ((key = get_hkey_obj( req->hkey, 0 ))) |
| 1957 | { |
| 1958 | delete_key( key, 1 ); /* FIXME */ |
| 1959 | release_object( key ); |
| 1960 | } |
| 1961 | } |
| 1962 | |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1963 | /* save a registry branch to a file */ |
| 1964 | DECL_HANDLER(save_registry) |
| 1965 | { |
| 1966 | struct key *key; |
| 1967 | |
Robert Shearman | b3957e3 | 2005-05-05 16:57:55 +0000 | [diff] [blame] | 1968 | if (!thread_single_check_privilege( current, &SeBackupPrivilege )) |
| 1969 | { |
| 1970 | set_error( STATUS_PRIVILEGE_NOT_HELD ); |
| 1971 | return; |
| 1972 | } |
| 1973 | |
| 1974 | if ((key = get_hkey_obj( req->hkey, 0 ))) |
Alexandre Julliard | d7e85d6 | 1999-11-23 19:39:11 +0000 | [diff] [blame] | 1975 | { |
| 1976 | save_registry( key, req->file ); |
| 1977 | release_object( key ); |
| 1978 | } |
| 1979 | } |
| 1980 | |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 1981 | /* add a registry key change notification */ |
| 1982 | DECL_HANDLER(set_registry_notification) |
| 1983 | { |
| 1984 | struct key *key; |
| 1985 | struct event *event; |
| 1986 | struct notify *notify; |
| 1987 | |
| 1988 | key = get_hkey_obj( req->hkey, KEY_NOTIFY ); |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 1989 | if (key) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 1990 | { |
| 1991 | event = get_event_obj( current->process, req->event, SYNCHRONIZE ); |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 1992 | if (event) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 1993 | { |
Alexandre Julliard | 9b3b7ca | 2005-06-09 20:36:08 +0000 | [diff] [blame] | 1994 | notify = find_notify( key, current->process, req->hkey ); |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 1995 | if (notify) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 1996 | { |
Mike McCormack | 5fb6e0e | 2006-05-14 17:11:54 +0900 | [diff] [blame] | 1997 | if (notify->event) |
| 1998 | release_object( notify->event ); |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 1999 | grab_object( event ); |
| 2000 | notify->event = event; |
| 2001 | } |
| 2002 | else |
| 2003 | { |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 2004 | notify = mem_alloc( sizeof(*notify) ); |
Robert Shearman | 3795709 | 2005-06-10 19:54:46 +0000 | [diff] [blame] | 2005 | if (notify) |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 2006 | { |
| 2007 | grab_object( event ); |
| 2008 | notify->event = event; |
| 2009 | notify->subtree = req->subtree; |
| 2010 | notify->filter = req->filter; |
| 2011 | notify->hkey = req->hkey; |
Alexandre Julliard | 9b3b7ca | 2005-06-09 20:36:08 +0000 | [diff] [blame] | 2012 | notify->process = current->process; |
Alexandre Julliard | 68abbc8 | 2005-02-24 19:43:53 +0000 | [diff] [blame] | 2013 | list_add_head( &key->notify_list, ¬ify->entry ); |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 2014 | } |
Mike McCormack | 11f4b44 | 2002-11-25 02:47:32 +0000 | [diff] [blame] | 2015 | } |
| 2016 | release_object( event ); |
| 2017 | } |
| 2018 | release_object( key ); |
| 2019 | } |
| 2020 | } |