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