registry.c 71 KB

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