registry.c 74 KB

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