registry.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  1. /*
  2. * Server-side registry management
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  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
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* To do:
  21. * - behavior with deleted keys
  22. * - values larger than request buffer
  23. * - symbolic links
  24. */
  25. #include "config.h"
  26. #include "wine/port.h"
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <limits.h>
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <sys/stat.h>
  37. #include <unistd.h>
  38. #include "object.h"
  39. #include "file.h"
  40. #include "handle.h"
  41. #include "request.h"
  42. #include "unicode.h"
  43. #include "winbase.h"
  44. #include "winreg.h"
  45. #include "winternl.h"
  46. #include "wine/library.h"
  47. struct notify
  48. {
  49. struct event *event; /* event to set when changing this key */
  50. int subtree; /* true if subtree notification */
  51. unsigned int filter; /* which events to notify on */
  52. obj_handle_t hkey; /* hkey associated with this notification */
  53. struct notify *next; /* list of notifications */
  54. struct notify *prev; /* list of notifications */
  55. };
  56. /* a registry key */
  57. struct key
  58. {
  59. struct object obj; /* object header */
  60. WCHAR *name; /* key name */
  61. WCHAR *class; /* key class */
  62. struct key *parent; /* parent key */
  63. int last_subkey; /* last in use subkey */
  64. int nb_subkeys; /* count of allocated subkeys */
  65. struct key **subkeys; /* subkeys array */
  66. int last_value; /* last in use value */
  67. int nb_values; /* count of allocated values in array */
  68. struct key_value *values; /* values array */
  69. short flags; /* flags */
  70. short level; /* saving level */
  71. time_t modif; /* last modification time */
  72. struct notify *first_notify; /* list of notifications */
  73. struct notify *last_notify; /* list of notifications */
  74. };
  75. /* key flags */
  76. #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
  77. #define KEY_DELETED 0x0002 /* key has been deleted */
  78. #define KEY_DIRTY 0x0004 /* key has been modified */
  79. /* a key value */
  80. struct key_value
  81. {
  82. WCHAR *name; /* value name */
  83. int type; /* value type */
  84. size_t len; /* value data length in bytes */
  85. void *data; /* pointer to value data */
  86. };
  87. #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
  88. #define MIN_VALUES 8 /* min. number of allocated values per key */
  89. /* the root of the registry tree */
  90. static struct key *root_key;
  91. /* keys saving level */
  92. /* current_level is the level that is put into all newly created or modified keys */
  93. /* saving_level is the minimum level that a key needs in order to get saved */
  94. static int current_level;
  95. static int saving_level;
  96. static struct timeval next_save_time; /* absolute time of next periodic save */
  97. static int save_period; /* delay between periodic saves (ms) */
  98. static struct timeout_user *save_timeout_user; /* saving timer */
  99. /* information about where to save a registry branch */
  100. struct save_branch_info
  101. {
  102. struct key *key;
  103. char *path;
  104. };
  105. #define MAX_SAVE_BRANCH_INFO 3
  106. static int save_branch_count;
  107. static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
  108. /* information about a file being loaded */
  109. struct file_load_info
  110. {
  111. FILE *file; /* input file */
  112. char *buffer; /* line buffer */
  113. int len; /* buffer length */
  114. int line; /* current input line */
  115. char *tmp; /* temp buffer to use while parsing input */
  116. int tmplen; /* length of temp buffer */
  117. };
  118. static void key_dump( struct object *obj, int verbose );
  119. static void key_destroy( struct object *obj );
  120. static const struct object_ops key_ops =
  121. {
  122. sizeof(struct key), /* size */
  123. key_dump, /* dump */
  124. no_add_queue, /* add_queue */
  125. NULL, /* remove_queue */
  126. NULL, /* signaled */
  127. NULL, /* satisfied */
  128. no_get_fd, /* get_fd */
  129. key_destroy /* destroy */
  130. };
  131. /*
  132. * The registry text file format v2 used by this code is similar to the one
  133. * used by REGEDIT import/export functionality, with the following differences:
  134. * - strings and key names can contain \x escapes for Unicode
  135. * - key names use escapes too in order to support Unicode
  136. * - the modification time optionally follows the key name
  137. * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
  138. */
  139. static inline char to_hex( char ch )
  140. {
  141. if (isdigit(ch)) return ch - '0';
  142. return tolower(ch) - 'a' + 10;
  143. }
  144. /* dump the full path of a key */
  145. static void dump_path( const struct key *key, const struct key *base, FILE *f )
  146. {
  147. if (key->parent && key->parent != base)
  148. {
  149. dump_path( key->parent, base, f );
  150. fprintf( f, "\\\\" );
  151. }
  152. dump_strW( key->name, strlenW(key->name), f, "[]" );
  153. }
  154. /* dump a value to a text file */
  155. static void dump_value( const struct key_value *value, FILE *f )
  156. {
  157. unsigned int i;
  158. int count;
  159. if (value->name[0])
  160. {
  161. fputc( '\"', f );
  162. count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
  163. count += fprintf( f, "\"=" );
  164. }
  165. else count = fprintf( f, "@=" );
  166. switch(value->type)
  167. {
  168. case REG_SZ:
  169. case REG_EXPAND_SZ:
  170. case REG_MULTI_SZ:
  171. if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
  172. fputc( '\"', f );
  173. if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
  174. fputc( '\"', f );
  175. break;
  176. case REG_DWORD:
  177. if (value->len == sizeof(DWORD))
  178. {
  179. DWORD dw;
  180. memcpy( &dw, value->data, sizeof(DWORD) );
  181. fprintf( f, "dword:%08lx", dw );
  182. break;
  183. }
  184. /* else fall through */
  185. default:
  186. if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
  187. else count += fprintf( f, "hex(%x):", value->type );
  188. for (i = 0; i < value->len; i++)
  189. {
  190. count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
  191. if (i < value->len-1)
  192. {
  193. fputc( ',', f );
  194. if (++count > 76)
  195. {
  196. fprintf( f, "\\\n " );
  197. count = 2;
  198. }
  199. }
  200. }
  201. break;
  202. }
  203. fputc( '\n', f );
  204. }
  205. /* save a registry and all its subkeys to a text file */
  206. static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
  207. {
  208. int i;
  209. if (key->flags & KEY_VOLATILE) return;
  210. /* save key if it has the proper level, and has either some values or no subkeys */
  211. /* keys with no values but subkeys are saved implicitly by saving the subkeys */
  212. if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
  213. {
  214. fprintf( f, "\n[" );
  215. if (key != base) dump_path( key, base, f );
  216. fprintf( f, "] %ld\n", (long)key->modif );
  217. for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
  218. }
  219. for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
  220. }
  221. static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
  222. {
  223. fprintf( stderr, "%s key ", op );
  224. if (key) dump_path( key, NULL, stderr );
  225. else fprintf( stderr, "ERROR" );
  226. if (value)
  227. {
  228. fprintf( stderr, " value ");
  229. dump_value( value, stderr );
  230. }
  231. else fprintf( stderr, "\n" );
  232. }
  233. static void key_dump( struct object *obj, int verbose )
  234. {
  235. struct key *key = (struct key *)obj;
  236. assert( obj->ops == &key_ops );
  237. fprintf( stderr, "Key flags=%x ", key->flags );
  238. dump_path( key, NULL, stderr );
  239. fprintf( stderr, "\n" );
  240. }
  241. /* notify waiter and maybe delete the notification */
  242. static void do_notification( struct key *key, struct notify *notify, int del )
  243. {
  244. if( notify->event )
  245. {
  246. set_event( notify->event );
  247. release_object( notify->event );
  248. notify->event = NULL;
  249. }
  250. if ( !del )
  251. return;
  252. if( notify->next )
  253. notify->next->prev = notify->prev;
  254. else
  255. key->last_notify = notify->prev;
  256. if( notify->prev )
  257. notify->prev->next = notify->next;
  258. else
  259. key->first_notify = notify->next;
  260. free( notify );
  261. }
  262. static struct notify *find_notify( struct key *key, obj_handle_t hkey)
  263. {
  264. struct notify *n;
  265. for( n=key->first_notify; n; n = n->next)
  266. if( n->hkey == hkey )
  267. break;
  268. return n;
  269. }
  270. /* close the notification associated with a handle */
  271. void registry_close_handle( struct object *obj, obj_handle_t hkey )
  272. {
  273. struct key * key = (struct key *) obj;
  274. struct notify *notify;
  275. if( obj->ops != &key_ops )
  276. return;
  277. notify = find_notify( key, hkey );
  278. if( !notify )
  279. return;
  280. do_notification( key, notify, 1 );
  281. }
  282. static void key_destroy( struct object *obj )
  283. {
  284. int i;
  285. struct key *key = (struct key *)obj;
  286. assert( obj->ops == &key_ops );
  287. if (key->name) free( key->name );
  288. if (key->class) free( key->class );
  289. for (i = 0; i <= key->last_value; i++)
  290. {
  291. free( key->values[i].name );
  292. if (key->values[i].data) free( key->values[i].data );
  293. }
  294. for (i = 0; i <= key->last_subkey; i++)
  295. {
  296. key->subkeys[i]->parent = NULL;
  297. release_object( key->subkeys[i] );
  298. }
  299. /* unconditionally notify everything waiting on this key */
  300. while ( key->first_notify )
  301. do_notification( key, key->first_notify, 1 );
  302. }
  303. /* duplicate a key path */
  304. /* returns a pointer to a static buffer, so only useable once per request */
  305. static WCHAR *copy_path( const WCHAR *path, size_t len, int skip_root )
  306. {
  307. static WCHAR buffer[MAX_PATH+1];
  308. static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\',0 };
  309. if (len > sizeof(buffer)-sizeof(buffer[0]))
  310. {
  311. set_error( STATUS_BUFFER_OVERFLOW );
  312. return NULL;
  313. }
  314. memcpy( buffer, path, len );
  315. buffer[len / sizeof(WCHAR)] = 0;
  316. if (skip_root && !strncmpiW( buffer, root_name, 10 )) return buffer + 10;
  317. return buffer;
  318. }
  319. /* copy a path from the request buffer */
  320. static WCHAR *copy_req_path( size_t len, int skip_root )
  321. {
  322. const WCHAR *name_ptr = get_req_data();
  323. if (len > get_req_data_size())
  324. {
  325. fatal_protocol_error( current, "copy_req_path: invalid length %d/%d\n",
  326. len, get_req_data_size() );
  327. return NULL;
  328. }
  329. return copy_path( name_ptr, len, skip_root );
  330. }
  331. /* return the next token in a given path */
  332. /* returns a pointer to a static buffer, so only useable once per request */
  333. static WCHAR *get_path_token( WCHAR *initpath )
  334. {
  335. static WCHAR *path;
  336. WCHAR *ret;
  337. if (initpath)
  338. {
  339. /* path cannot start with a backslash */
  340. if (*initpath == '\\')
  341. {
  342. set_error( STATUS_OBJECT_PATH_INVALID );
  343. return NULL;
  344. }
  345. path = initpath;
  346. }
  347. else while (*path == '\\') path++;
  348. ret = path;
  349. while (*path && *path != '\\') path++;
  350. if (*path) *path++ = 0;
  351. return ret;
  352. }
  353. /* duplicate a Unicode string from the request buffer */
  354. static WCHAR *req_strdupW( const void *req, const WCHAR *str, size_t len )
  355. {
  356. WCHAR *name;
  357. if ((name = mem_alloc( len + sizeof(WCHAR) )) != NULL)
  358. {
  359. memcpy( name, str, len );
  360. name[len / sizeof(WCHAR)] = 0;
  361. }
  362. return name;
  363. }
  364. /* allocate a key object */
  365. static struct key *alloc_key( const WCHAR *name, time_t modif )
  366. {
  367. struct key *key;
  368. if ((key = alloc_object( &key_ops )))
  369. {
  370. key->class = NULL;
  371. key->flags = 0;
  372. key->last_subkey = -1;
  373. key->nb_subkeys = 0;
  374. key->subkeys = NULL;
  375. key->nb_values = 0;
  376. key->last_value = -1;
  377. key->values = NULL;
  378. key->level = current_level;
  379. key->modif = modif;
  380. key->parent = NULL;
  381. key->first_notify = NULL;
  382. key->last_notify = NULL;
  383. if (!(key->name = strdupW( name )))
  384. {
  385. release_object( key );
  386. key = NULL;
  387. }
  388. }
  389. return key;
  390. }
  391. /* mark a key and all its parents as dirty (modified) */
  392. static void make_dirty( struct key *key )
  393. {
  394. while (key)
  395. {
  396. if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
  397. key->flags |= KEY_DIRTY;
  398. key = key->parent;
  399. }
  400. }
  401. /* mark a key and all its subkeys as clean (not modified) */
  402. static void make_clean( struct key *key )
  403. {
  404. int i;
  405. if (key->flags & KEY_VOLATILE) return;
  406. if (!(key->flags & KEY_DIRTY)) return;
  407. key->flags &= ~KEY_DIRTY;
  408. for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
  409. }
  410. /* go through all the notifications and send them if necessary */
  411. void check_notify( struct key *key, unsigned int change, int not_subtree )
  412. {
  413. struct notify *n = key->first_notify;
  414. while (n)
  415. {
  416. struct notify *next = n->next;
  417. if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
  418. do_notification( key, n, 0 );
  419. n = next;
  420. }
  421. }
  422. /* update key modification time */
  423. static void touch_key( struct key *key, unsigned int change )
  424. {
  425. struct key *k;
  426. key->modif = time(NULL);
  427. key->level = max( key->level, current_level );
  428. make_dirty( key );
  429. /* do notifications */
  430. check_notify( key, change, 1 );
  431. for ( k = key->parent; k; k = k->parent )
  432. check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
  433. }
  434. /* try to grow the array of subkeys; return 1 if OK, 0 on error */
  435. static int grow_subkeys( struct key *key )
  436. {
  437. struct key **new_subkeys;
  438. int nb_subkeys;
  439. if (key->nb_subkeys)
  440. {
  441. nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
  442. if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
  443. {
  444. set_error( STATUS_NO_MEMORY );
  445. return 0;
  446. }
  447. }
  448. else
  449. {
  450. nb_subkeys = MIN_VALUES;
  451. if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
  452. }
  453. key->subkeys = new_subkeys;
  454. key->nb_subkeys = nb_subkeys;
  455. return 1;
  456. }
  457. /* allocate a subkey for a given key, and return its index */
  458. static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
  459. {
  460. struct key *key;
  461. int i;
  462. if (parent->last_subkey + 1 == parent->nb_subkeys)
  463. {
  464. /* need to grow the array */
  465. if (!grow_subkeys( parent )) return NULL;
  466. }
  467. if ((key = alloc_key( name, modif )) != NULL)
  468. {
  469. key->parent = parent;
  470. for (i = ++parent->last_subkey; i > index; i--)
  471. parent->subkeys[i] = parent->subkeys[i-1];
  472. parent->subkeys[index] = key;
  473. }
  474. return key;
  475. }
  476. /* free a subkey of a given key */
  477. static void free_subkey( struct key *parent, int index )
  478. {
  479. struct key *key;
  480. int i, nb_subkeys;
  481. assert( index >= 0 );
  482. assert( index <= parent->last_subkey );
  483. key = parent->subkeys[index];
  484. for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
  485. parent->last_subkey--;
  486. key->flags |= KEY_DELETED;
  487. key->parent = NULL;
  488. release_object( key );
  489. /* try to shrink the array */
  490. nb_subkeys = parent->nb_subkeys;
  491. if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
  492. {
  493. struct key **new_subkeys;
  494. nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
  495. if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
  496. if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
  497. parent->subkeys = new_subkeys;
  498. parent->nb_subkeys = nb_subkeys;
  499. }
  500. }
  501. /* find the named child of a given key and return its index */
  502. static struct key *find_subkey( const struct key *key, const WCHAR *name, int *index )
  503. {
  504. int i, min, max, res;
  505. min = 0;
  506. max = key->last_subkey;
  507. while (min <= max)
  508. {
  509. i = (min + max) / 2;
  510. if (!(res = strcmpiW( key->subkeys[i]->name, name )))
  511. {
  512. *index = i;
  513. return key->subkeys[i];
  514. }
  515. if (res > 0) max = i - 1;
  516. else min = i + 1;
  517. }
  518. *index = min; /* this is where we should insert it */
  519. return NULL;
  520. }
  521. /* open a subkey */
  522. /* warning: the key name must be writeable (use copy_path) */
  523. static struct key *open_key( struct key *key, WCHAR *name )
  524. {
  525. int index;
  526. WCHAR *path;
  527. if (!(path = get_path_token( name ))) return NULL;
  528. while (*path)
  529. {
  530. if (!(key = find_subkey( key, path, &index )))
  531. {
  532. set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  533. break;
  534. }
  535. path = get_path_token( NULL );
  536. }
  537. if (debug_level > 1) dump_operation( key, NULL, "Open" );
  538. if (key) grab_object( key );
  539. return key;
  540. }
  541. /* create a subkey */
  542. /* warning: the key name must be writeable (use copy_path) */
  543. static struct key *create_key( struct key *key, WCHAR *name, WCHAR *class,
  544. int flags, time_t modif, int *created )
  545. {
  546. struct key *base;
  547. int base_idx, index;
  548. WCHAR *path;
  549. if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
  550. {
  551. set_error( STATUS_KEY_DELETED );
  552. return NULL;
  553. }
  554. if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
  555. {
  556. set_error( STATUS_CHILD_MUST_BE_VOLATILE );
  557. return NULL;
  558. }
  559. if (!modif) modif = time(NULL);
  560. if (!(path = get_path_token( name ))) return NULL;
  561. *created = 0;
  562. while (*path)
  563. {
  564. struct key *subkey;
  565. if (!(subkey = find_subkey( key, path, &index ))) break;
  566. key = subkey;
  567. path = get_path_token( NULL );
  568. }
  569. /* create the remaining part */
  570. if (!*path) goto done;
  571. *created = 1;
  572. if (flags & KEY_DIRTY) make_dirty( key );
  573. base = key;
  574. base_idx = index;
  575. key = alloc_subkey( key, path, index, modif );
  576. while (key)
  577. {
  578. key->flags |= flags;
  579. path = get_path_token( NULL );
  580. if (!*path) goto done;
  581. /* we know the index is always 0 in a new key */
  582. key = alloc_subkey( key, path, 0, modif );
  583. }
  584. if (base_idx != -1) free_subkey( base, base_idx );
  585. return NULL;
  586. done:
  587. if (debug_level > 1) dump_operation( key, NULL, "Create" );
  588. if (class) key->class = strdupW(class);
  589. grab_object( key );
  590. return key;
  591. }
  592. /* query information about a key or a subkey */
  593. static void enum_key( const struct key *key, int index, int info_class,
  594. struct enum_key_reply *reply )
  595. {
  596. int i;
  597. size_t len, namelen, classlen;
  598. int max_subkey = 0, max_class = 0;
  599. int max_value = 0, max_data = 0;
  600. WCHAR *data;
  601. if (index != -1) /* -1 means use the specified key directly */
  602. {
  603. if ((index < 0) || (index > key->last_subkey))
  604. {
  605. set_error( STATUS_NO_MORE_ENTRIES );
  606. return;
  607. }
  608. key = key->subkeys[index];
  609. }
  610. namelen = strlenW(key->name) * sizeof(WCHAR);
  611. classlen = key->class ? strlenW(key->class) * sizeof(WCHAR) : 0;
  612. switch(info_class)
  613. {
  614. case KeyBasicInformation:
  615. classlen = 0; /* only return the name */
  616. /* fall through */
  617. case KeyNodeInformation:
  618. reply->max_subkey = 0;
  619. reply->max_class = 0;
  620. reply->max_value = 0;
  621. reply->max_data = 0;
  622. break;
  623. case KeyFullInformation:
  624. for (i = 0; i <= key->last_subkey; i++)
  625. {
  626. struct key *subkey = key->subkeys[i];
  627. len = strlenW( subkey->name );
  628. if (len > max_subkey) max_subkey = len;
  629. if (!subkey->class) continue;
  630. len = strlenW( subkey->class );
  631. if (len > max_class) max_class = len;
  632. }
  633. for (i = 0; i <= key->last_value; i++)
  634. {
  635. len = strlenW( key->values[i].name );
  636. if (len > max_value) max_value = len;
  637. len = key->values[i].len;
  638. if (len > max_data) max_data = len;
  639. }
  640. reply->max_subkey = max_subkey;
  641. reply->max_class = max_class;
  642. reply->max_value = max_value;
  643. reply->max_data = max_data;
  644. namelen = 0; /* only return the class */
  645. break;
  646. default:
  647. set_error( STATUS_INVALID_PARAMETER );
  648. return;
  649. }
  650. reply->subkeys = key->last_subkey + 1;
  651. reply->values = key->last_value + 1;
  652. reply->modif = key->modif;
  653. reply->total = namelen + classlen;
  654. len = min( reply->total, get_reply_max_size() );
  655. if (len && (data = set_reply_data_size( len )))
  656. {
  657. if (len > namelen)
  658. {
  659. reply->namelen = namelen;
  660. memcpy( data, key->name, namelen );
  661. memcpy( (char *)data + namelen, key->class, len - namelen );
  662. }
  663. else
  664. {
  665. reply->namelen = len;
  666. memcpy( data, key->name, len );
  667. }
  668. }
  669. if (debug_level > 1) dump_operation( key, NULL, "Enum" );
  670. }
  671. /* delete a key and its values */
  672. static int delete_key( struct key *key, int recurse )
  673. {
  674. int index;
  675. struct key *parent;
  676. /* must find parent and index */
  677. if (key == root_key)
  678. {
  679. set_error( STATUS_ACCESS_DENIED );
  680. return -1;
  681. }
  682. if (!(parent = key->parent) || (key->flags & KEY_DELETED))
  683. {
  684. set_error( STATUS_KEY_DELETED );
  685. return -1;
  686. }
  687. while (recurse && (key->last_subkey>=0))
  688. if(0>delete_key(key->subkeys[key->last_subkey], 1))
  689. return -1;
  690. for (index = 0; index <= parent->last_subkey; index++)
  691. if (parent->subkeys[index] == key) break;
  692. assert( index <= parent->last_subkey );
  693. /* we can only delete a key that has no subkeys */
  694. if (key->last_subkey >= 0)
  695. {
  696. set_error( STATUS_ACCESS_DENIED );
  697. return -1;
  698. }
  699. if (debug_level > 1) dump_operation( key, NULL, "Delete" );
  700. free_subkey( parent, index );
  701. touch_key( parent, REG_NOTIFY_CHANGE_NAME );
  702. return 0;
  703. }
  704. /* try to grow the array of values; return 1 if OK, 0 on error */
  705. static int grow_values( struct key *key )
  706. {
  707. struct key_value *new_val;
  708. int nb_values;
  709. if (key->nb_values)
  710. {
  711. nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
  712. if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
  713. {
  714. set_error( STATUS_NO_MEMORY );
  715. return 0;
  716. }
  717. }
  718. else
  719. {
  720. nb_values = MIN_VALUES;
  721. if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
  722. }
  723. key->values = new_val;
  724. key->nb_values = nb_values;
  725. return 1;
  726. }
  727. /* find the named value of a given key and return its index in the array */
  728. static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
  729. {
  730. int i, min, max, res;
  731. min = 0;
  732. max = key->last_value;
  733. while (min <= max)
  734. {
  735. i = (min + max) / 2;
  736. if (!(res = strcmpiW( key->values[i].name, name )))
  737. {
  738. *index = i;
  739. return &key->values[i];
  740. }
  741. if (res > 0) max = i - 1;
  742. else min = i + 1;
  743. }
  744. *index = min; /* this is where we should insert it */
  745. return NULL;
  746. }
  747. /* insert a new value; the index must have been returned by find_value */
  748. static struct key_value *insert_value( struct key *key, const WCHAR *name, int index )
  749. {
  750. struct key_value *value;
  751. WCHAR *new_name;
  752. int i;
  753. if (key->last_value + 1 == key->nb_values)
  754. {
  755. if (!grow_values( key )) return NULL;
  756. }
  757. if (!(new_name = strdupW(name))) return NULL;
  758. for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
  759. value = &key->values[index];
  760. value->name = new_name;
  761. value->len = 0;
  762. value->data = NULL;
  763. return value;
  764. }
  765. /* set a key value */
  766. static void set_value( struct key *key, WCHAR *name, int type, const void *data, size_t len )
  767. {
  768. struct key_value *value;
  769. void *ptr = NULL;
  770. int index;
  771. if ((value = find_value( key, name, &index )))
  772. {
  773. /* check if the new value is identical to the existing one */
  774. if (value->type == type && value->len == len &&
  775. value->data && !memcmp( value->data, data, len ))
  776. {
  777. if (debug_level > 1) dump_operation( key, value, "Skip setting" );
  778. return;
  779. }
  780. }
  781. if (len && !(ptr = memdup( data, len ))) return;
  782. if (!value)
  783. {
  784. if (!(value = insert_value( key, name, index )))
  785. {
  786. if (ptr) free( ptr );
  787. return;
  788. }
  789. }
  790. else if (value->data) free( value->data ); /* already existing, free previous data */
  791. value->type = type;
  792. value->len = len;
  793. value->data = ptr;
  794. touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
  795. if (debug_level > 1) dump_operation( key, value, "Set" );
  796. }
  797. /* get a key value */
  798. static void get_value( struct key *key, const WCHAR *name, int *type, int *len )
  799. {
  800. struct key_value *value;
  801. int index;
  802. if ((value = find_value( key, name, &index )))
  803. {
  804. *type = value->type;
  805. *len = value->len;
  806. if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
  807. if (debug_level > 1) dump_operation( key, value, "Get" );
  808. }
  809. else
  810. {
  811. *type = -1;
  812. set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  813. }
  814. }
  815. /* enumerate a key value */
  816. static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
  817. {
  818. struct key_value *value;
  819. if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
  820. else
  821. {
  822. void *data;
  823. size_t namelen, maxlen;
  824. value = &key->values[i];
  825. reply->type = value->type;
  826. namelen = strlenW( value->name ) * sizeof(WCHAR);
  827. switch(info_class)
  828. {
  829. case KeyValueBasicInformation:
  830. reply->total = namelen;
  831. break;
  832. case KeyValueFullInformation:
  833. reply->total = namelen + value->len;
  834. break;
  835. case KeyValuePartialInformation:
  836. reply->total = value->len;
  837. namelen = 0;
  838. break;
  839. default:
  840. set_error( STATUS_INVALID_PARAMETER );
  841. return;
  842. }
  843. maxlen = min( reply->total, get_reply_max_size() );
  844. if (maxlen && ((data = set_reply_data_size( maxlen ))))
  845. {
  846. if (maxlen > namelen)
  847. {
  848. reply->namelen = namelen;
  849. memcpy( data, value->name, namelen );
  850. memcpy( (char *)data + namelen, value->data, maxlen - namelen );
  851. }
  852. else
  853. {
  854. reply->namelen = maxlen;
  855. memcpy( data, value->name, maxlen );
  856. }
  857. }
  858. if (debug_level > 1) dump_operation( key, value, "Enum" );
  859. }
  860. }
  861. /* delete a value */
  862. static void delete_value( struct key *key, const WCHAR *name )
  863. {
  864. struct key_value *value;
  865. int i, index, nb_values;
  866. if (!(value = find_value( key, name, &index )))
  867. {
  868. set_error( STATUS_OBJECT_NAME_NOT_FOUND );
  869. return;
  870. }
  871. if (debug_level > 1) dump_operation( key, value, "Delete" );
  872. free( value->name );
  873. if (value->data) free( value->data );
  874. for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
  875. key->last_value--;
  876. touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
  877. /* try to shrink the array */
  878. nb_values = key->nb_values;
  879. if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
  880. {
  881. struct key_value *new_val;
  882. nb_values -= nb_values / 3; /* shrink by 33% */
  883. if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
  884. if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
  885. key->values = new_val;
  886. key->nb_values = nb_values;
  887. }
  888. }
  889. /* get the registry key corresponding to an hkey handle */
  890. static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
  891. {
  892. if (!hkey) return (struct key *)grab_object( root_key );
  893. return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
  894. }
  895. /* read a line from the input file */
  896. static int read_next_line( struct file_load_info *info )
  897. {
  898. char *newbuf;
  899. int newlen, pos = 0;
  900. info->line++;
  901. for (;;)
  902. {
  903. if (!fgets( info->buffer + pos, info->len - pos, info->file ))
  904. return (pos != 0); /* EOF */
  905. pos = strlen(info->buffer);
  906. if (info->buffer[pos-1] == '\n')
  907. {
  908. /* got a full line */
  909. info->buffer[--pos] = 0;
  910. if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
  911. return 1;
  912. }
  913. if (pos < info->len - 1) return 1; /* EOF but something was read */
  914. /* need to enlarge the buffer */
  915. newlen = info->len + info->len / 2;
  916. if (!(newbuf = realloc( info->buffer, newlen )))
  917. {
  918. set_error( STATUS_NO_MEMORY );
  919. return -1;
  920. }
  921. info->buffer = newbuf;
  922. info->len = newlen;
  923. }
  924. }
  925. /* make sure the temp buffer holds enough space */
  926. static int get_file_tmp_space( struct file_load_info *info, int size )
  927. {
  928. char *tmp;
  929. if (info->tmplen >= size) return 1;
  930. if (!(tmp = realloc( info->tmp, size )))
  931. {
  932. set_error( STATUS_NO_MEMORY );
  933. return 0;
  934. }
  935. info->tmp = tmp;
  936. info->tmplen = size;
  937. return 1;
  938. }
  939. /* report an error while loading an input file */
  940. static void file_read_error( const char *err, struct file_load_info *info )
  941. {
  942. fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
  943. }
  944. /* parse an escaped string back into Unicode */
  945. /* return the number of chars read from the input, or -1 on output overflow */
  946. static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
  947. {
  948. int count = sizeof(WCHAR); /* for terminating null */
  949. const char *p = src;
  950. while (*p && *p != endchar)
  951. {
  952. if (*p != '\\') *dest = (WCHAR)*p++;
  953. else
  954. {
  955. p++;
  956. switch(*p)
  957. {
  958. case 'a': *dest = '\a'; p++; break;
  959. case 'b': *dest = '\b'; p++; break;
  960. case 'e': *dest = '\e'; p++; break;
  961. case 'f': *dest = '\f'; p++; break;
  962. case 'n': *dest = '\n'; p++; break;
  963. case 'r': *dest = '\r'; p++; break;
  964. case 't': *dest = '\t'; p++; break;
  965. case 'v': *dest = '\v'; p++; break;
  966. case 'x': /* hex escape */
  967. p++;
  968. if (!isxdigit(*p)) *dest = 'x';
  969. else
  970. {
  971. *dest = to_hex(*p++);
  972. if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
  973. if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
  974. if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
  975. }
  976. break;
  977. case '0':
  978. case '1':
  979. case '2':
  980. case '3':
  981. case '4':
  982. case '5':
  983. case '6':
  984. case '7': /* octal escape */
  985. *dest = *p++ - '0';
  986. if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
  987. if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
  988. break;
  989. default:
  990. *dest = (WCHAR)*p++;
  991. break;
  992. }
  993. }
  994. if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
  995. dest++;
  996. }
  997. *dest = 0;
  998. if (!*p) return -1; /* delimiter not found */
  999. *len = count;
  1000. return p + 1 - src;
  1001. }
  1002. /* convert a data type tag to a value type */
  1003. static int get_data_type( const char *buffer, int *type, int *parse_type )
  1004. {
  1005. struct data_type { const char *tag; int len; int type; int parse_type; };
  1006. static const struct data_type data_types[] =
  1007. { /* actual type */ /* type to assume for parsing */
  1008. { "\"", 1, REG_SZ, REG_SZ },
  1009. { "str:\"", 5, REG_SZ, REG_SZ },
  1010. { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
  1011. { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
  1012. { "hex:", 4, REG_BINARY, REG_BINARY },
  1013. { "dword:", 6, REG_DWORD, REG_DWORD },
  1014. { "hex(", 4, -1, REG_BINARY },
  1015. { NULL, 0, 0, 0 }
  1016. };
  1017. const struct data_type *ptr;
  1018. char *end;
  1019. for (ptr = data_types; ptr->tag; ptr++)
  1020. {
  1021. if (memcmp( ptr->tag, buffer, ptr->len )) continue;
  1022. *parse_type = ptr->parse_type;
  1023. if ((*type = ptr->type) != -1) return ptr->len;
  1024. /* "hex(xx):" is special */
  1025. *type = (int)strtoul( buffer + 4, &end, 16 );
  1026. if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
  1027. return end + 2 - buffer;
  1028. }
  1029. return 0;
  1030. }
  1031. /* load and create a key from the input file */
  1032. static struct key *load_key( struct key *base, const char *buffer, int flags,
  1033. int prefix_len, struct file_load_info *info,
  1034. int default_modif )
  1035. {
  1036. WCHAR *p, *name;
  1037. int res, len, modif;
  1038. len = strlen(buffer) * sizeof(WCHAR);
  1039. if (!get_file_tmp_space( info, len )) return NULL;
  1040. if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
  1041. {
  1042. file_read_error( "Malformed key", info );
  1043. return NULL;
  1044. }
  1045. if (sscanf( buffer + res, " %d", &modif ) != 1) modif = default_modif;
  1046. p = (WCHAR *)info->tmp;
  1047. while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
  1048. if (!*p)
  1049. {
  1050. if (prefix_len > 1)
  1051. {
  1052. file_read_error( "Malformed key", info );
  1053. return NULL;
  1054. }
  1055. /* empty key name, return base key */
  1056. return (struct key *)grab_object( base );
  1057. }
  1058. if (!(name = copy_path( p, len - ((char *)p - info->tmp), 0 )))
  1059. {
  1060. file_read_error( "Key is too long", info );
  1061. return NULL;
  1062. }
  1063. return create_key( base, name, NULL, flags, modif, &res );
  1064. }
  1065. /* parse a comma-separated list of hex digits */
  1066. static int parse_hex( unsigned char *dest, int *len, const char *buffer )
  1067. {
  1068. const char *p = buffer;
  1069. int count = 0;
  1070. while (isxdigit(*p))
  1071. {
  1072. int val;
  1073. char buf[3];
  1074. memcpy( buf, p, 2 );
  1075. buf[2] = 0;
  1076. sscanf( buf, "%x", &val );
  1077. if (count++ >= *len) return -1; /* dest buffer overflow */
  1078. *dest++ = (unsigned char )val;
  1079. p += 2;
  1080. if (*p == ',') p++;
  1081. }
  1082. *len = count;
  1083. return p - buffer;
  1084. }
  1085. /* parse a value name and create the corresponding value */
  1086. static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
  1087. struct file_load_info *info )
  1088. {
  1089. struct key_value *value;
  1090. int index, maxlen;
  1091. maxlen = strlen(buffer) * sizeof(WCHAR);
  1092. if (!get_file_tmp_space( info, maxlen )) return NULL;
  1093. if (buffer[0] == '@')
  1094. {
  1095. info->tmp[0] = info->tmp[1] = 0;
  1096. *len = 1;
  1097. }
  1098. else
  1099. {
  1100. if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
  1101. (*len)++; /* for initial quote */
  1102. }
  1103. while (isspace(buffer[*len])) (*len)++;
  1104. if (buffer[*len] != '=') goto error;
  1105. (*len)++;
  1106. while (isspace(buffer[*len])) (*len)++;
  1107. if (!(value = find_value( key, (WCHAR *)info->tmp, &index )))
  1108. value = insert_value( key, (WCHAR *)info->tmp, index );
  1109. return value;
  1110. error:
  1111. file_read_error( "Malformed value name", info );
  1112. return NULL;
  1113. }
  1114. /* load a value from the input file */
  1115. static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
  1116. {
  1117. DWORD dw;
  1118. void *ptr, *newptr;
  1119. int maxlen, len, res;
  1120. int type, parse_type;
  1121. struct key_value *value;
  1122. if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
  1123. if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
  1124. buffer += len + res;
  1125. switch(parse_type)
  1126. {
  1127. case REG_SZ:
  1128. len = strlen(buffer) * sizeof(WCHAR);
  1129. if (!get_file_tmp_space( info, len )) return 0;
  1130. if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
  1131. ptr = info->tmp;
  1132. break;
  1133. case REG_DWORD:
  1134. dw = strtoul( buffer, NULL, 16 );
  1135. ptr = &dw;
  1136. len = sizeof(dw);
  1137. break;
  1138. case REG_BINARY: /* hex digits */
  1139. len = 0;
  1140. for (;;)
  1141. {
  1142. maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
  1143. if (!get_file_tmp_space( info, len + maxlen )) return 0;
  1144. if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
  1145. len += maxlen;
  1146. buffer += res;
  1147. while (isspace(*buffer)) buffer++;
  1148. if (!*buffer) break;
  1149. if (*buffer != '\\') goto error;
  1150. if (read_next_line( info) != 1) goto error;
  1151. buffer = info->buffer;
  1152. while (isspace(*buffer)) buffer++;
  1153. }
  1154. ptr = info->tmp;
  1155. break;
  1156. default:
  1157. assert(0);
  1158. ptr = NULL; /* keep compiler quiet */
  1159. break;
  1160. }
  1161. if (!len) newptr = NULL;
  1162. else if (!(newptr = memdup( ptr, len ))) return 0;
  1163. if (value->data) free( value->data );
  1164. value->data = newptr;
  1165. value->len = len;
  1166. value->type = type;
  1167. /* update the key level but not the modification time */
  1168. key->level = max( key->level, current_level );
  1169. make_dirty( key );
  1170. return 1;
  1171. error:
  1172. file_read_error( "Malformed value", info );
  1173. return 0;
  1174. }
  1175. /* return the length (in path elements) of name that is part of the key name */
  1176. /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
  1177. static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
  1178. {
  1179. WCHAR *p;
  1180. int res;
  1181. int len = strlen(name) * sizeof(WCHAR);
  1182. if (!get_file_tmp_space( info, len )) return 0;
  1183. if ((res = parse_strW( (WCHAR *)info->tmp, &len, name, ']' )) == -1)
  1184. {
  1185. file_read_error( "Malformed key", info );
  1186. return 0;
  1187. }
  1188. for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') break;
  1189. *p = 0;
  1190. for (res = 1; key != root_key; res++)
  1191. {
  1192. if (!strcmpiW( (WCHAR *)info->tmp, key->name )) break;
  1193. key = key->parent;
  1194. }
  1195. if (key == root_key) res = 0; /* no matching name */
  1196. return res;
  1197. }
  1198. /* load all the keys from the input file */
  1199. /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
  1200. static void load_keys( struct key *key, FILE *f, int prefix_len )
  1201. {
  1202. struct key *subkey = NULL;
  1203. struct file_load_info info;
  1204. char *p;
  1205. int default_modif = time(NULL);
  1206. int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
  1207. info.file = f;
  1208. info.len = 4;
  1209. info.tmplen = 4;
  1210. info.line = 0;
  1211. if (!(info.buffer = mem_alloc( info.len ))) return;
  1212. if (!(info.tmp = mem_alloc( info.tmplen )))
  1213. {
  1214. free( info.buffer );
  1215. return;
  1216. }
  1217. if ((read_next_line( &info ) != 1) ||
  1218. strcmp( info.buffer, "WINE REGISTRY Version 2" ))
  1219. {
  1220. set_error( STATUS_NOT_REGISTRY_FILE );
  1221. goto done;
  1222. }
  1223. while (read_next_line( &info ) == 1)
  1224. {
  1225. p = info.buffer;
  1226. while (*p && isspace(*p)) p++;
  1227. switch(*p)
  1228. {
  1229. case '[': /* new key */
  1230. if (subkey) release_object( subkey );
  1231. if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
  1232. if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info, default_modif )))
  1233. file_read_error( "Error creating key", &info );
  1234. break;
  1235. case '@': /* default value */
  1236. case '\"': /* value */
  1237. if (subkey) load_value( subkey, p, &info );
  1238. else file_read_error( "Value without key", &info );
  1239. break;
  1240. case '#': /* comment */
  1241. case ';': /* comment */
  1242. case 0: /* empty line */
  1243. break;
  1244. default:
  1245. file_read_error( "Unrecognized input", &info );
  1246. break;
  1247. }
  1248. }
  1249. done:
  1250. if (subkey) release_object( subkey );
  1251. free( info.buffer );
  1252. free( info.tmp );
  1253. }
  1254. /* load a part of the registry from a file */
  1255. static void load_registry( struct key *key, obj_handle_t handle )
  1256. {
  1257. struct file *file;
  1258. int fd;
  1259. if (!(file = get_file_obj( current->process, handle, GENERIC_READ ))) return;
  1260. fd = dup( get_file_unix_fd( file ) );
  1261. release_object( file );
  1262. if (fd != -1)
  1263. {
  1264. FILE *f = fdopen( fd, "r" );
  1265. if (f)
  1266. {
  1267. load_keys( key, f, -1 );
  1268. fclose( f );
  1269. }
  1270. else file_set_error();
  1271. }
  1272. }
  1273. /* load one of the initial registry files */
  1274. static void load_init_registry_from_file( const char *filename, struct key *key )
  1275. {
  1276. FILE *f;
  1277. if ((f = fopen( filename, "r" )))
  1278. {
  1279. load_keys( key, f, 0 );
  1280. fclose( f );
  1281. if (get_error() == STATUS_NOT_REGISTRY_FILE)
  1282. fatal_error( "%s is not a valid registry file\n", filename );
  1283. if (get_error())
  1284. fatal_error( "loading %s failed with error %x\n", filename, get_error() );
  1285. }
  1286. if (!(key->flags & KEY_VOLATILE))
  1287. {
  1288. assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
  1289. if ((save_branch_info[save_branch_count].path = strdup( filename )))
  1290. save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
  1291. }
  1292. }
  1293. /* load the user registry files */
  1294. static void load_user_registries( struct key *key_current_user )
  1295. {
  1296. static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
  1297. static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
  1298. const char *config = wine_get_config_dir();
  1299. char *p, *filename;
  1300. struct key *key;
  1301. int dummy;
  1302. if (!(filename = mem_alloc( strlen(config) + 16 ))) return;
  1303. strcpy( filename, config );
  1304. p = filename + strlen(filename);
  1305. /* load system.reg into Registry\Machine */
  1306. if (!(key = create_key( root_key, copy_path( HKLM, sizeof(HKLM), 0 ),
  1307. NULL, 0, time(NULL), &dummy )))
  1308. fatal_error( "could not create Machine registry key\n" );
  1309. strcpy( p, "/system.reg" );
  1310. load_init_registry_from_file( filename, key );
  1311. release_object( key );
  1312. /* load userdef.reg into Registry\User\.Default */
  1313. if (!(key = create_key( root_key, copy_path( HKU_default, sizeof(HKU_default), 0 ),
  1314. NULL, 0, time(NULL), &dummy )))
  1315. fatal_error( "could not create User\\.Default registry key\n" );
  1316. strcpy( p, "/userdef.reg" );
  1317. load_init_registry_from_file( filename, key );
  1318. release_object( key );
  1319. /* load user.reg into HKEY_CURRENT_USER */
  1320. strcpy( p, "/user.reg" );
  1321. load_init_registry_from_file( filename, key_current_user );
  1322. free( filename );
  1323. }
  1324. /* registry initialisation */
  1325. void init_registry(void)
  1326. {
  1327. static const WCHAR root_name[] = { 0 };
  1328. static const WCHAR config_name[] =
  1329. { 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
  1330. 'W','i','n','e','\\','W','i','n','e','\\','C','o','n','f','i','g',0 };
  1331. const char *config = wine_get_config_dir();
  1332. char *filename;
  1333. struct key *key;
  1334. int dummy;
  1335. /* create the root key */
  1336. root_key = alloc_key( root_name, time(NULL) );
  1337. assert( root_key );
  1338. /* load the config file */
  1339. if (!(filename = malloc( strlen(config) + sizeof("/config") ))) fatal_error( "out of memory\n" );
  1340. strcpy( filename, config );
  1341. strcat( filename, "/config" );
  1342. if (!(key = create_key( root_key, copy_path( config_name, sizeof(config_name), 0 ),
  1343. NULL, 0, time(NULL), &dummy )))
  1344. fatal_error( "could not create Config registry key\n" );
  1345. key->flags |= KEY_VOLATILE;
  1346. load_init_registry_from_file( filename, key );
  1347. release_object( key );
  1348. free( filename );
  1349. }
  1350. /* update the level of the parents of a key (only needed for the old format) */
  1351. static int update_level( struct key *key )
  1352. {
  1353. int i;
  1354. int max = key->level;
  1355. for (i = 0; i <= key->last_subkey; i++)
  1356. {
  1357. int sub = update_level( key->subkeys[i] );
  1358. if (sub > max) max = sub;
  1359. }
  1360. key->level = max;
  1361. return max;
  1362. }
  1363. /* save a registry branch to a file */
  1364. static void save_all_subkeys( struct key *key, FILE *f )
  1365. {
  1366. fprintf( f, "WINE REGISTRY Version 2\n" );
  1367. fprintf( f, ";; All keys relative to " );
  1368. dump_path( key, NULL, f );
  1369. fprintf( f, "\n" );
  1370. save_subkeys( key, key, f );
  1371. }
  1372. /* save a registry branch to a file handle */
  1373. static void save_registry( struct key *key, obj_handle_t handle )
  1374. {
  1375. struct file *file;
  1376. int fd;
  1377. if (key->flags & KEY_DELETED)
  1378. {
  1379. set_error( STATUS_KEY_DELETED );
  1380. return;
  1381. }
  1382. if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) return;
  1383. fd = dup( get_file_unix_fd( file ) );
  1384. release_object( file );
  1385. if (fd != -1)
  1386. {
  1387. FILE *f = fdopen( fd, "w" );
  1388. if (f)
  1389. {
  1390. save_all_subkeys( key, f );
  1391. if (fclose( f )) file_set_error();
  1392. }
  1393. else
  1394. {
  1395. file_set_error();
  1396. close( fd );
  1397. }
  1398. }
  1399. }
  1400. /* save a registry branch to a file */
  1401. static int save_branch( struct key *key, const char *path )
  1402. {
  1403. struct stat st;
  1404. char *p, *real, *tmp = NULL;
  1405. int fd, count = 0, ret = 0, by_symlink;
  1406. FILE *f;
  1407. if (!(key->flags & KEY_DIRTY))
  1408. {
  1409. if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
  1410. return 1;
  1411. }
  1412. /* get the real path */
  1413. by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
  1414. if (!(real = malloc( PATH_MAX ))) return 0;
  1415. if (!realpath( path, real ))
  1416. {
  1417. free( real );
  1418. real = NULL;
  1419. }
  1420. else path = real;
  1421. /* test the file type */
  1422. if ((fd = open( path, O_WRONLY )) != -1)
  1423. {
  1424. /* if file is not a regular file or has multiple links or is accessed
  1425. * via symbolic links, write directly into it; otherwise use a temp file */
  1426. if (by_symlink ||
  1427. (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
  1428. {
  1429. ftruncate( fd, 0 );
  1430. goto save;
  1431. }
  1432. close( fd );
  1433. }
  1434. /* create a temp file in the same directory */
  1435. if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
  1436. strcpy( tmp, path );
  1437. if ((p = strrchr( tmp, '/' ))) p++;
  1438. else p = tmp;
  1439. for (;;)
  1440. {
  1441. sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
  1442. if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
  1443. if (errno != EEXIST) goto done;
  1444. close( fd );
  1445. }
  1446. /* now save to it */
  1447. save:
  1448. if (!(f = fdopen( fd, "w" )))
  1449. {
  1450. if (tmp) unlink( tmp );
  1451. close( fd );
  1452. goto done;
  1453. }
  1454. if (debug_level > 1)
  1455. {
  1456. fprintf( stderr, "%s: ", path );
  1457. dump_operation( key, NULL, "saving" );
  1458. }
  1459. save_all_subkeys( key, f );
  1460. ret = !fclose(f);
  1461. if (tmp)
  1462. {
  1463. /* if successfully written, rename to final name */
  1464. if (ret) ret = !rename( tmp, path );
  1465. if (!ret) unlink( tmp );
  1466. free( tmp );
  1467. }
  1468. done:
  1469. if (real) free( real );
  1470. if (ret) make_clean( key );
  1471. return ret;
  1472. }
  1473. /* periodic saving of the registry */
  1474. static void periodic_save( void *arg )
  1475. {
  1476. int i;
  1477. for (i = 0; i < save_branch_count; i++)
  1478. save_branch( save_branch_info[i].key, save_branch_info[i].path );
  1479. add_timeout( &next_save_time, save_period );
  1480. save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
  1481. }
  1482. /* save the modified registry branches to disk */
  1483. void flush_registry(void)
  1484. {
  1485. int i;
  1486. for (i = 0; i < save_branch_count; i++)
  1487. {
  1488. if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
  1489. {
  1490. fprintf( stderr, "wineserver: could not save registry branch to %s",
  1491. save_branch_info[i].path );
  1492. perror( " " );
  1493. }
  1494. }
  1495. }
  1496. /* close the top-level keys; used on server exit */
  1497. void close_registry(void)
  1498. {
  1499. int i;
  1500. for (i = 0; i < save_branch_count; i++) release_object( save_branch_info[i].key );
  1501. release_object( root_key );
  1502. }
  1503. /* create a registry key */
  1504. DECL_HANDLER(create_key)
  1505. {
  1506. struct key *key = NULL, *parent;
  1507. unsigned int access = req->access;
  1508. WCHAR *name, *class;
  1509. if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
  1510. reply->hkey = 0;
  1511. if (!(name = copy_req_path( req->namelen, !req->parent ))) return;
  1512. if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
  1513. {
  1514. int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
  1515. if (req->namelen == get_req_data_size()) /* no class specified */
  1516. {
  1517. key = create_key( parent, name, NULL, flags, req->modif, &reply->created );
  1518. }
  1519. else
  1520. {
  1521. const WCHAR *class_ptr = (const WCHAR *)((const char *)get_req_data() + req->namelen);
  1522. if ((class = req_strdupW( req, class_ptr, get_req_data_size() - req->namelen )))
  1523. {
  1524. key = create_key( parent, name, class, flags, req->modif, &reply->created );
  1525. free( class );
  1526. }
  1527. }
  1528. if (key)
  1529. {
  1530. reply->hkey = alloc_handle( current->process, key, access, 0 );
  1531. release_object( key );
  1532. }
  1533. release_object( parent );
  1534. }
  1535. }
  1536. /* open a registry key */
  1537. DECL_HANDLER(open_key)
  1538. {
  1539. struct key *key, *parent;
  1540. unsigned int access = req->access;
  1541. if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
  1542. reply->hkey = 0;
  1543. if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
  1544. {
  1545. WCHAR *name = copy_path( get_req_data(), get_req_data_size(), !req->parent );
  1546. if (name && (key = open_key( parent, name )))
  1547. {
  1548. reply->hkey = alloc_handle( current->process, key, access, 0 );
  1549. release_object( key );
  1550. }
  1551. release_object( parent );
  1552. }
  1553. }
  1554. /* delete a registry key */
  1555. DECL_HANDLER(delete_key)
  1556. {
  1557. struct key *key;
  1558. if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
  1559. {
  1560. delete_key( key, 0);
  1561. release_object( key );
  1562. }
  1563. }
  1564. /* flush a registry key */
  1565. DECL_HANDLER(flush_key)
  1566. {
  1567. struct key *key = get_hkey_obj( req->hkey, 0 );
  1568. if (key)
  1569. {
  1570. /* we don't need to do anything here with the current implementation */
  1571. release_object( key );
  1572. }
  1573. }
  1574. /* enumerate registry subkeys */
  1575. DECL_HANDLER(enum_key)
  1576. {
  1577. struct key *key;
  1578. if ((key = get_hkey_obj( req->hkey,
  1579. req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
  1580. {
  1581. enum_key( key, req->index, req->info_class, reply );
  1582. release_object( key );
  1583. }
  1584. }
  1585. /* set a value of a registry key */
  1586. DECL_HANDLER(set_key_value)
  1587. {
  1588. struct key *key;
  1589. WCHAR *name;
  1590. if (!(name = copy_req_path( req->namelen, 0 ))) return;
  1591. if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
  1592. {
  1593. size_t datalen = get_req_data_size() - req->namelen;
  1594. const char *data = (const char *)get_req_data() + req->namelen;
  1595. set_value( key, name, req->type, data, datalen );
  1596. release_object( key );
  1597. }
  1598. }
  1599. /* retrieve the value of a registry key */
  1600. DECL_HANDLER(get_key_value)
  1601. {
  1602. struct key *key;
  1603. WCHAR *name;
  1604. reply->total = 0;
  1605. if (!(name = copy_path( get_req_data(), get_req_data_size(), 0 ))) return;
  1606. if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
  1607. {
  1608. get_value( key, name, &reply->type, &reply->total );
  1609. release_object( key );
  1610. }
  1611. }
  1612. /* enumerate the value of a registry key */
  1613. DECL_HANDLER(enum_key_value)
  1614. {
  1615. struct key *key;
  1616. if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
  1617. {
  1618. enum_value( key, req->index, req->info_class, reply );
  1619. release_object( key );
  1620. }
  1621. }
  1622. /* delete a value of a registry key */
  1623. DECL_HANDLER(delete_key_value)
  1624. {
  1625. WCHAR *name;
  1626. struct key *key;
  1627. if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
  1628. {
  1629. if ((name = req_strdupW( req, get_req_data(), get_req_data_size() )))
  1630. {
  1631. delete_value( key, name );
  1632. free( name );
  1633. }
  1634. release_object( key );
  1635. }
  1636. }
  1637. /* load a registry branch from a file */
  1638. DECL_HANDLER(load_registry)
  1639. {
  1640. struct key *key;
  1641. if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
  1642. {
  1643. /* FIXME: use subkey name */
  1644. load_registry( key, req->file );
  1645. release_object( key );
  1646. }
  1647. }
  1648. DECL_HANDLER(unload_registry)
  1649. {
  1650. struct key *key;
  1651. if ((key = get_hkey_obj( req->hkey, 0 )))
  1652. {
  1653. delete_key( key, 1 ); /* FIXME */
  1654. release_object( key );
  1655. }
  1656. }
  1657. /* save a registry branch to a file */
  1658. DECL_HANDLER(save_registry)
  1659. {
  1660. struct key *key;
  1661. if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
  1662. {
  1663. save_registry( key, req->file );
  1664. release_object( key );
  1665. }
  1666. }
  1667. /* load the user registry files */
  1668. DECL_HANDLER(load_user_registries)
  1669. {
  1670. struct key *key;
  1671. current_level = 1;
  1672. saving_level = req->saving;
  1673. if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
  1674. {
  1675. load_user_registries( key );
  1676. release_object( key );
  1677. }
  1678. /* set periodic save timer */
  1679. if (save_timeout_user)
  1680. {
  1681. remove_timeout_user( save_timeout_user );
  1682. save_timeout_user = NULL;
  1683. }
  1684. if ((save_period = req->period))
  1685. {
  1686. if (save_period < 10000) save_period = 10000; /* limit rate */
  1687. gettimeofday( &next_save_time, 0 );
  1688. add_timeout( &next_save_time, save_period );
  1689. save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
  1690. }
  1691. }
  1692. /* add a registry key change notification */
  1693. DECL_HANDLER(set_registry_notification)
  1694. {
  1695. struct key *key;
  1696. struct event *event;
  1697. struct notify *notify;
  1698. key = get_hkey_obj( req->hkey, KEY_NOTIFY );
  1699. if( key )
  1700. {
  1701. event = get_event_obj( current->process, req->event, SYNCHRONIZE );
  1702. if( event )
  1703. {
  1704. notify = find_notify( key, req->hkey );
  1705. if( notify )
  1706. {
  1707. release_object( notify->event );
  1708. grab_object( event );
  1709. notify->event = event;
  1710. }
  1711. else
  1712. {
  1713. notify = (struct notify *) malloc (sizeof(*notify));
  1714. if( notify )
  1715. {
  1716. grab_object( event );
  1717. notify->event = event;
  1718. notify->subtree = req->subtree;
  1719. notify->filter = req->filter;
  1720. notify->hkey = req->hkey;
  1721. /* add to linked list */
  1722. notify->prev = NULL;
  1723. notify->next = key->first_notify;
  1724. if ( notify->next )
  1725. notify->next->prev = notify;
  1726. else
  1727. key->last_notify = notify;
  1728. key->first_notify = notify;
  1729. }
  1730. else
  1731. set_error( STATUS_NO_MEMORY );
  1732. }
  1733. release_object( event );
  1734. }
  1735. release_object( key );
  1736. }
  1737. }