registry.c 74 KB

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