registry.c 71 KB

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