registry.c 74 KB

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