main.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * Copyright 2012 Austin English
  3. * Copyright 2015 Michael Müller
  4. * Copyright 2015 Sebastian Lackner
  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. #include <windows.h>
  21. #include <fcntl.h>
  22. #include <fdi.h>
  23. #include <shlwapi.h>
  24. #include "wine/debug.h"
  25. #include "wine/list.h"
  26. #include "wusa.h"
  27. WINE_DEFAULT_DEBUG_CHANNEL(wusa);
  28. struct strbuf
  29. {
  30. WCHAR *buf;
  31. DWORD pos;
  32. DWORD len;
  33. };
  34. struct installer_tempdir
  35. {
  36. struct list entry;
  37. WCHAR *path;
  38. };
  39. struct installer_state
  40. {
  41. BOOL norestart;
  42. BOOL quiet;
  43. struct list tempdirs;
  44. struct list assemblies;
  45. struct list updates;
  46. };
  47. static void * CDECL cabinet_alloc(ULONG cb)
  48. {
  49. return heap_alloc(cb);
  50. }
  51. static void CDECL cabinet_free(void *pv)
  52. {
  53. heap_free(pv);
  54. }
  55. static INT_PTR CDECL cabinet_open(char *pszFile, int oflag, int pmode)
  56. {
  57. DWORD dwAccess = 0;
  58. DWORD dwShareMode = 0;
  59. DWORD dwCreateDisposition = OPEN_EXISTING;
  60. switch (oflag & _O_ACCMODE)
  61. {
  62. case _O_RDONLY:
  63. dwAccess = GENERIC_READ;
  64. dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
  65. break;
  66. case _O_WRONLY:
  67. dwAccess = GENERIC_WRITE;
  68. dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  69. break;
  70. case _O_RDWR:
  71. dwAccess = GENERIC_READ | GENERIC_WRITE;
  72. dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
  73. break;
  74. }
  75. if ((oflag & (_O_CREAT | _O_EXCL)) == (_O_CREAT | _O_EXCL))
  76. dwCreateDisposition = CREATE_NEW;
  77. else if (oflag & _O_CREAT)
  78. dwCreateDisposition = CREATE_ALWAYS;
  79. return (INT_PTR)CreateFileA(pszFile, dwAccess, dwShareMode, NULL, dwCreateDisposition, 0, NULL);
  80. }
  81. static UINT CDECL cabinet_read(INT_PTR hf, void *pv, UINT cb)
  82. {
  83. HANDLE handle = (HANDLE)hf;
  84. DWORD read;
  85. if (ReadFile(handle, pv, cb, &read, NULL))
  86. return read;
  87. return 0;
  88. }
  89. static UINT CDECL cabinet_write(INT_PTR hf, void *pv, UINT cb)
  90. {
  91. HANDLE handle = (HANDLE)hf;
  92. DWORD written;
  93. if (WriteFile(handle, pv, cb, &written, NULL))
  94. return written;
  95. return 0;
  96. }
  97. static int CDECL cabinet_close(INT_PTR hf)
  98. {
  99. HANDLE handle = (HANDLE)hf;
  100. return CloseHandle(handle) ? 0 : -1;
  101. }
  102. static LONG CDECL cabinet_seek(INT_PTR hf, LONG dist, int seektype)
  103. {
  104. HANDLE handle = (HANDLE)hf;
  105. /* flags are compatible and so are passed straight through */
  106. return SetFilePointer(handle, dist, NULL, seektype);
  107. }
  108. static WCHAR *path_combine(const WCHAR *path, const WCHAR *filename)
  109. {
  110. WCHAR *result;
  111. DWORD length;
  112. if (!path || !filename) return NULL;
  113. length = lstrlenW(path) + lstrlenW(filename) + 2;
  114. if (!(result = heap_alloc(length * sizeof(WCHAR)))) return NULL;
  115. lstrcpyW(result, path);
  116. if (result[0] && result[lstrlenW(result) - 1] != '\\') lstrcatW(result, L"\\");
  117. lstrcatW(result, filename);
  118. return result;
  119. }
  120. static WCHAR *get_uncompressed_path(PFDINOTIFICATION pfdin)
  121. {
  122. WCHAR *file = strdupAtoW(pfdin->psz1);
  123. WCHAR *path = path_combine(pfdin->pv, file);
  124. heap_free(file);
  125. return path;
  126. }
  127. static BOOL is_directory(const WCHAR *path)
  128. {
  129. DWORD attrs = GetFileAttributesW(path);
  130. if (attrs == INVALID_FILE_ATTRIBUTES) return FALSE;
  131. return (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
  132. }
  133. static BOOL create_directory(const WCHAR *path)
  134. {
  135. if (is_directory(path)) return TRUE;
  136. if (CreateDirectoryW(path, NULL)) return TRUE;
  137. return (GetLastError() == ERROR_ALREADY_EXISTS);
  138. }
  139. static BOOL create_parent_directory(const WCHAR *filename)
  140. {
  141. WCHAR *p, *path = strdupW(filename);
  142. BOOL ret = FALSE;
  143. if (!path) return FALSE;
  144. if (!PathRemoveFileSpecW(path)) goto done;
  145. if (is_directory(path))
  146. {
  147. ret = TRUE;
  148. goto done;
  149. }
  150. for (p = path; *p; p++)
  151. {
  152. if (*p != '\\') continue;
  153. *p = 0;
  154. if (!create_directory(path)) goto done;
  155. *p = '\\';
  156. }
  157. ret = create_directory(path);
  158. done:
  159. heap_free(path);
  160. return ret;
  161. }
  162. static INT_PTR cabinet_copy_file(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
  163. {
  164. HANDLE handle = INVALID_HANDLE_VALUE;
  165. WCHAR *file;
  166. DWORD attrs;
  167. if (!(file = get_uncompressed_path(pfdin)))
  168. return -1;
  169. TRACE("Extracting %s -> %s\n", debugstr_a(pfdin->psz1), debugstr_w(file));
  170. if (create_parent_directory(file))
  171. {
  172. attrs = pfdin->attribs;
  173. if (!attrs) attrs = FILE_ATTRIBUTE_NORMAL;
  174. handle = CreateFileW(file, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, attrs, NULL);
  175. }
  176. heap_free(file);
  177. return (handle != INVALID_HANDLE_VALUE) ? (INT_PTR)handle : -1;
  178. }
  179. static INT_PTR cabinet_close_file_info(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
  180. {
  181. HANDLE handle = (HANDLE)pfdin->hf;
  182. CloseHandle(handle);
  183. return 1;
  184. }
  185. static INT_PTR CDECL cabinet_notify(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
  186. {
  187. switch (fdint)
  188. {
  189. case fdintPARTIAL_FILE:
  190. FIXME("fdintPARTIAL_FILE not implemented\n");
  191. return 0;
  192. case fdintNEXT_CABINET:
  193. FIXME("fdintNEXT_CABINET not implemented\n");
  194. return 0;
  195. case fdintCOPY_FILE:
  196. return cabinet_copy_file(fdint, pfdin);
  197. case fdintCLOSE_FILE_INFO:
  198. return cabinet_close_file_info(fdint, pfdin);
  199. default:
  200. return 0;
  201. }
  202. }
  203. static BOOL extract_cabinet(const WCHAR *filename, const WCHAR *destination)
  204. {
  205. char *filenameA = NULL;
  206. BOOL ret = FALSE;
  207. HFDI hfdi;
  208. ERF erf;
  209. hfdi = FDICreate(cabinet_alloc, cabinet_free, cabinet_open, cabinet_read,
  210. cabinet_write, cabinet_close, cabinet_seek, 0, &erf);
  211. if (!hfdi) return FALSE;
  212. if ((filenameA = strdupWtoA(filename)))
  213. {
  214. ret = FDICopy(hfdi, filenameA, NULL, 0, cabinet_notify, NULL, (void *)destination);
  215. heap_free(filenameA);
  216. }
  217. FDIDestroy(hfdi);
  218. return ret;
  219. }
  220. static const WCHAR *create_temp_directory(struct installer_state *state)
  221. {
  222. static UINT id;
  223. struct installer_tempdir *entry;
  224. WCHAR tmp[MAX_PATH];
  225. if (!GetTempPathW(ARRAY_SIZE(tmp), tmp)) return NULL;
  226. if (!(entry = heap_alloc(sizeof(*entry)))) return NULL;
  227. if (!(entry->path = heap_alloc((MAX_PATH + 20) * sizeof(WCHAR))))
  228. {
  229. heap_free(entry);
  230. return NULL;
  231. }
  232. for (;;)
  233. {
  234. if (!GetTempFileNameW(tmp, L"msu", ++id, entry->path))
  235. {
  236. heap_free(entry->path);
  237. heap_free(entry);
  238. return NULL;
  239. }
  240. if (CreateDirectoryW(entry->path, NULL)) break;
  241. }
  242. list_add_tail(&state->tempdirs, &entry->entry);
  243. return entry->path;
  244. }
  245. static BOOL delete_directory(const WCHAR *path)
  246. {
  247. WIN32_FIND_DATAW data;
  248. WCHAR *full_path;
  249. HANDLE search;
  250. if (!(full_path = path_combine(path, L"*"))) return FALSE;
  251. search = FindFirstFileW(full_path, &data);
  252. heap_free(full_path);
  253. if (search != INVALID_HANDLE_VALUE)
  254. {
  255. do
  256. {
  257. if (!wcscmp(data.cFileName, L".")) continue;
  258. if (!wcscmp(data.cFileName, L"..")) continue;
  259. if (!(full_path = path_combine(path, data.cFileName))) continue;
  260. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  261. delete_directory(full_path);
  262. else
  263. DeleteFileW(full_path);
  264. heap_free(full_path);
  265. }
  266. while (FindNextFileW(search, &data));
  267. FindClose(search);
  268. }
  269. return RemoveDirectoryW(path);
  270. }
  271. static void installer_cleanup(struct installer_state *state)
  272. {
  273. struct installer_tempdir *tempdir, *tempdir2;
  274. struct assembly_entry *assembly, *assembly2;
  275. struct dependency_entry *dependency, *dependency2;
  276. LIST_FOR_EACH_ENTRY_SAFE(tempdir, tempdir2, &state->tempdirs, struct installer_tempdir, entry)
  277. {
  278. list_remove(&tempdir->entry);
  279. delete_directory(tempdir->path);
  280. heap_free(tempdir->path);
  281. heap_free(tempdir);
  282. }
  283. LIST_FOR_EACH_ENTRY_SAFE(assembly, assembly2, &state->assemblies, struct assembly_entry, entry)
  284. {
  285. list_remove(&assembly->entry);
  286. free_assembly(assembly);
  287. }
  288. LIST_FOR_EACH_ENTRY_SAFE(dependency, dependency2, &state->updates, struct dependency_entry, entry)
  289. {
  290. list_remove(&dependency->entry);
  291. free_dependency(dependency);
  292. }
  293. }
  294. static BOOL str_ends_with(const WCHAR *str, const WCHAR *suffix)
  295. {
  296. DWORD str_len = lstrlenW(str), suffix_len = lstrlenW(suffix);
  297. if (suffix_len > str_len) return FALSE;
  298. return !wcsicmp(str + str_len - suffix_len, suffix);
  299. }
  300. static BOOL load_assemblies_from_cab(const WCHAR *filename, struct installer_state *state)
  301. {
  302. struct assembly_entry *assembly;
  303. const WCHAR *temp_path;
  304. WIN32_FIND_DATAW data;
  305. HANDLE search;
  306. WCHAR *path;
  307. TRACE("Processing cab file %s\n", debugstr_w(filename));
  308. if (!(temp_path = create_temp_directory(state))) return FALSE;
  309. if (!extract_cabinet(filename, temp_path))
  310. {
  311. ERR("Failed to extract %s\n", debugstr_w(filename));
  312. return FALSE;
  313. }
  314. if (!(path = path_combine(temp_path, L"_manifest_.cix.xml"))) return FALSE;
  315. if (GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
  316. {
  317. FIXME("Cabinet uses proprietary msdelta file compression which is not (yet) supported\n");
  318. FIXME("Installation of msu file will most likely fail\n");
  319. }
  320. heap_free(path);
  321. if (!(path = path_combine(temp_path, L"*"))) return FALSE;
  322. search = FindFirstFileW(path, &data);
  323. heap_free(path);
  324. if (search != INVALID_HANDLE_VALUE)
  325. {
  326. do
  327. {
  328. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
  329. if (!str_ends_with(data.cFileName, L".manifest") &&
  330. !str_ends_with(data.cFileName, L".mum")) continue;
  331. if (!(path = path_combine(temp_path, data.cFileName))) continue;
  332. if ((assembly = load_manifest(path)))
  333. list_add_tail(&state->assemblies, &assembly->entry);
  334. heap_free(path);
  335. }
  336. while (FindNextFileW(search, &data));
  337. FindClose(search);
  338. }
  339. return TRUE;
  340. }
  341. static BOOL compare_assembly_string(const WCHAR *str1, const WCHAR *str2)
  342. {
  343. return !wcscmp(str1, str2) || !wcscmp(str1, L"*") || !wcscmp(str2, L"*");
  344. }
  345. static struct assembly_entry *lookup_assembly(struct list *manifest_list, struct assembly_identity *identity)
  346. {
  347. struct assembly_entry *assembly;
  348. LIST_FOR_EACH_ENTRY(assembly, manifest_list, struct assembly_entry, entry)
  349. {
  350. if (wcsicmp(assembly->identity.name, identity->name)) continue;
  351. if (!compare_assembly_string(assembly->identity.architecture, identity->architecture)) continue;
  352. if (!compare_assembly_string(assembly->identity.language, identity->language)) continue;
  353. if (!compare_assembly_string(assembly->identity.pubkey_token, identity->pubkey_token)) continue;
  354. if (!compare_assembly_string(assembly->identity.version, identity->version))
  355. {
  356. WARN("Ignoring version difference for %s (expected %s, found %s)\n",
  357. debugstr_w(identity->name), debugstr_w(identity->version), debugstr_w(assembly->identity.version));
  358. }
  359. return assembly;
  360. }
  361. return NULL;
  362. }
  363. static WCHAR *get_assembly_source(struct assembly_entry *assembly)
  364. {
  365. WCHAR *p, *path = strdupW(assembly->filename);
  366. if (path && (p = wcsrchr(path, '.'))) *p = 0;
  367. return path;
  368. }
  369. static BOOL strbuf_init(struct strbuf *buf)
  370. {
  371. buf->pos = 0;
  372. buf->len = 64;
  373. buf->buf = heap_alloc(buf->len * sizeof(WCHAR));
  374. return buf->buf != NULL;
  375. }
  376. static void strbuf_free(struct strbuf *buf)
  377. {
  378. heap_free(buf->buf);
  379. buf->buf = NULL;
  380. }
  381. static BOOL strbuf_append(struct strbuf *buf, const WCHAR *str, DWORD len)
  382. {
  383. DWORD new_len;
  384. WCHAR *new_buf;
  385. if (!buf->buf) return FALSE;
  386. if (!str) return TRUE;
  387. if (len == ~0U) len = lstrlenW(str);
  388. if (buf->pos + len + 1 > buf->len)
  389. {
  390. new_len = max(buf->pos + len + 1, buf->len * 2);
  391. new_buf = heap_realloc(buf->buf, new_len * sizeof(WCHAR));
  392. if (!new_buf)
  393. {
  394. strbuf_free(buf);
  395. return FALSE;
  396. }
  397. buf->buf = new_buf;
  398. buf->len = new_len;
  399. }
  400. memcpy(&buf->buf[buf->pos], str, len * sizeof(WCHAR));
  401. buf->buf[buf->pos + len] = 0;
  402. buf->pos += len;
  403. return TRUE;
  404. }
  405. static WCHAR *lookup_expression(struct assembly_entry *assembly, const WCHAR *key)
  406. {
  407. WCHAR path[MAX_PATH];
  408. if (!wcscmp(key, L"runtime.system32"))
  409. {
  410. #ifdef __x86_64__
  411. if (!wcscmp(assembly->identity.architecture, L"x86"))
  412. {
  413. GetSystemWow64DirectoryW(path, ARRAY_SIZE(path));
  414. return strdupW(path);
  415. }
  416. #endif
  417. GetSystemDirectoryW(path, ARRAY_SIZE(path));
  418. return strdupW(path);
  419. }
  420. if (!wcscmp(key, L"runtime.windows"))
  421. {
  422. GetWindowsDirectoryW(path, ARRAY_SIZE(path));
  423. return strdupW(path);
  424. }
  425. FIXME("Unknown expression %s\n", debugstr_w(key));
  426. return NULL;
  427. }
  428. static WCHAR *expand_expression(struct assembly_entry *assembly, const WCHAR *expression)
  429. {
  430. const WCHAR *pos, *next;
  431. WCHAR *key, *value;
  432. struct strbuf buf;
  433. if (!expression || !strbuf_init(&buf)) return NULL;
  434. for (pos = expression; (next = wcsstr(pos, L"$(")); pos = next + 1)
  435. {
  436. strbuf_append(&buf, pos, next - pos);
  437. pos = next + 2;
  438. if (!(next = wcsstr(pos, L")")))
  439. {
  440. strbuf_append(&buf, L"$(", 2);
  441. break;
  442. }
  443. if (!(key = strdupWn(pos, next - pos))) goto error;
  444. value = lookup_expression(assembly, key);
  445. heap_free(key);
  446. if (!value) goto error;
  447. strbuf_append(&buf, value, ~0U);
  448. heap_free(value);
  449. }
  450. strbuf_append(&buf, pos, ~0U);
  451. return buf.buf;
  452. error:
  453. FIXME("Couldn't resolve expression %s\n", debugstr_w(expression));
  454. strbuf_free(&buf);
  455. return NULL;
  456. }
  457. static BOOL install_files_copy(struct assembly_entry *assembly, const WCHAR *source_path, struct fileop_entry *fileop, BOOL dryrun)
  458. {
  459. WCHAR *target_path, *target, *source = NULL;
  460. BOOL ret = FALSE;
  461. if (!(target_path = expand_expression(assembly, fileop->target))) return FALSE;
  462. if (!(target = path_combine(target_path, fileop->source))) goto error;
  463. if (!(source = path_combine(source_path, fileop->source))) goto error;
  464. if (dryrun)
  465. {
  466. if (!(ret = PathFileExistsW(source)))
  467. {
  468. ERR("Required file %s not found\n", debugstr_w(source));
  469. goto error;
  470. }
  471. }
  472. else
  473. {
  474. TRACE("Copying %s -> %s\n", debugstr_w(source), debugstr_w(target));
  475. if (!create_parent_directory(target))
  476. {
  477. ERR("Failed to create parent directory for %s\n", debugstr_w(target));
  478. goto error;
  479. }
  480. if (!(ret = CopyFileExW(source, target, NULL, NULL, NULL, 0)))
  481. {
  482. ERR("Failed to copy %s to %s\n", debugstr_w(source), debugstr_w(target));
  483. goto error;
  484. }
  485. }
  486. error:
  487. heap_free(target_path);
  488. heap_free(target);
  489. heap_free(source);
  490. return ret;
  491. }
  492. static BOOL install_files(struct assembly_entry *assembly, BOOL dryrun)
  493. {
  494. struct fileop_entry *fileop;
  495. WCHAR *source_path;
  496. BOOL ret = TRUE;
  497. if (!(source_path = get_assembly_source(assembly)))
  498. {
  499. ERR("Failed to get assembly source directory\n");
  500. return FALSE;
  501. }
  502. LIST_FOR_EACH_ENTRY(fileop, &assembly->fileops, struct fileop_entry, entry)
  503. {
  504. if (!(ret = install_files_copy(assembly, source_path, fileop, dryrun))) break;
  505. }
  506. heap_free(source_path);
  507. return ret;
  508. }
  509. static WCHAR *split_registry_key(WCHAR *key, HKEY *root)
  510. {
  511. DWORD size;
  512. WCHAR *p;
  513. if (!(p = wcschr(key, '\\'))) return NULL;
  514. size = p - key;
  515. if (lstrlenW(L"HKEY_CLASSES_ROOT") == size && !wcsncmp(key, L"HKEY_CLASSES_ROOT", size))
  516. *root = HKEY_CLASSES_ROOT;
  517. else if (lstrlenW(L"HKEY_CURRENT_CONFIG") == size && !wcsncmp(key, L"HKEY_CURRENT_CONFIG", size))
  518. *root = HKEY_CURRENT_CONFIG;
  519. else if (lstrlenW(L"HKEY_CURRENT_USER") == size && !wcsncmp(key, L"HKEY_CURRENT_USER", size))
  520. *root = HKEY_CURRENT_USER;
  521. else if (lstrlenW(L"HKEY_LOCAL_MACHINE") == size && !wcsncmp(key, L"HKEY_LOCAL_MACHINE", size))
  522. *root = HKEY_LOCAL_MACHINE;
  523. else if (lstrlenW(L"HKEY_USERS") == size && !wcsncmp(key, L"HKEY_USERS", size))
  524. *root = HKEY_USERS;
  525. else
  526. {
  527. FIXME("Unknown root key %s\n", debugstr_wn(key, size));
  528. return NULL;
  529. }
  530. return p + 1;
  531. }
  532. static BOOL install_registry_string(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, DWORD type, BOOL dryrun)
  533. {
  534. DWORD value_size;
  535. WCHAR *value = expand_expression(assembly, registrykv->value);
  536. BOOL ret = TRUE;
  537. if (registrykv->value && !value)
  538. return FALSE;
  539. value_size = value ? (lstrlenW(value) + 1) * sizeof(WCHAR) : 0;
  540. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, type, (void *)value, value_size))
  541. {
  542. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  543. ret = FALSE;
  544. }
  545. heap_free(value);
  546. return ret;
  547. }
  548. static WCHAR *parse_multisz(const WCHAR *input, DWORD *size)
  549. {
  550. const WCHAR *pos, *next;
  551. struct strbuf buf;
  552. *size = 0;
  553. if (!input || !input[0] || !strbuf_init(&buf)) return NULL;
  554. for (pos = input; pos[0] == '"'; pos++)
  555. {
  556. pos++;
  557. if (!(next = wcsstr(pos, L"\""))) goto error;
  558. strbuf_append(&buf, pos, next - pos);
  559. strbuf_append(&buf, L"", ARRAY_SIZE(L""));
  560. pos = next + 1;
  561. if (!pos[0]) break;
  562. if (pos[0] != ',')
  563. {
  564. FIXME("Error while parsing REG_MULTI_SZ string: Expected comma but got '%c'\n", pos[0]);
  565. goto error;
  566. }
  567. }
  568. if (pos[0])
  569. {
  570. FIXME("Error while parsing REG_MULTI_SZ string: Garbage at end of string\n");
  571. goto error;
  572. }
  573. strbuf_append(&buf, L"", ARRAY_SIZE(L""));
  574. *size = buf.pos * sizeof(WCHAR);
  575. return buf.buf;
  576. error:
  577. strbuf_free(&buf);
  578. return NULL;
  579. }
  580. static BOOL install_registry_multisz(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  581. {
  582. DWORD value_size;
  583. WCHAR *value = parse_multisz(registrykv->value, &value_size);
  584. BOOL ret = TRUE;
  585. if (registrykv->value && registrykv->value[0] && !value)
  586. return FALSE;
  587. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, REG_MULTI_SZ, (void *)value, value_size))
  588. {
  589. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  590. ret = FALSE;
  591. }
  592. heap_free(value);
  593. return ret;
  594. }
  595. static BOOL install_registry_dword(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  596. {
  597. DWORD value = registrykv->value_type ? wcstoul(registrykv->value_type, NULL, 16) : 0;
  598. BOOL ret = TRUE;
  599. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, REG_DWORD, (void *)&value, sizeof(value)))
  600. {
  601. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  602. ret = FALSE;
  603. }
  604. return ret;
  605. }
  606. static BYTE *parse_hex(const WCHAR *input, DWORD *size)
  607. {
  608. WCHAR number[3] = {0, 0, 0};
  609. BYTE *output, *p;
  610. int length;
  611. *size = 0;
  612. if (!input) return NULL;
  613. length = lstrlenW(input);
  614. if (length & 1) return NULL;
  615. length >>= 1;
  616. if (!(output = heap_alloc(length))) return NULL;
  617. for (p = output; *input; input += 2)
  618. {
  619. number[0] = input[0];
  620. number[1] = input[1];
  621. *p++ = wcstoul(number, 0, 16);
  622. }
  623. *size = length;
  624. return output;
  625. }
  626. static BOOL install_registry_binary(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  627. {
  628. DWORD value_size;
  629. BYTE *value = parse_hex(registrykv->value, &value_size);
  630. BOOL ret = TRUE;
  631. if (registrykv->value && !value)
  632. return FALSE;
  633. if (!dryrun && RegSetValueExW(key, registrykv->name, 0, REG_BINARY, value, value_size))
  634. {
  635. ERR("Failed to set registry key %s\n", debugstr_w(registrykv->name));
  636. ret = FALSE;
  637. }
  638. heap_free(value);
  639. return ret;
  640. }
  641. static BOOL install_registry_value(struct assembly_entry *assembly, HKEY key, struct registrykv_entry *registrykv, BOOL dryrun)
  642. {
  643. TRACE("Setting registry key %s = %s\n", debugstr_w(registrykv->name), debugstr_w(registrykv->value));
  644. if (!wcscmp(registrykv->value_type, L"REG_SZ"))
  645. return install_registry_string(assembly, key, registrykv, REG_SZ, dryrun);
  646. if (!wcscmp(registrykv->value_type, L"REG_EXPAND_SZ"))
  647. return install_registry_string(assembly, key, registrykv, REG_EXPAND_SZ, dryrun);
  648. if (!wcscmp(registrykv->value_type, L"REG_MULTI_SZ"))
  649. return install_registry_multisz(assembly, key, registrykv, dryrun);
  650. if (!wcscmp(registrykv->value_type, L"REG_DWORD"))
  651. return install_registry_dword(assembly, key, registrykv, dryrun);
  652. if (!wcscmp(registrykv->value_type, L"REG_BINARY"))
  653. return install_registry_binary(assembly, key, registrykv, dryrun);
  654. FIXME("Unsupported registry value type %s\n", debugstr_w(registrykv->value_type));
  655. return FALSE;
  656. }
  657. static BOOL install_registry(struct assembly_entry *assembly, BOOL dryrun)
  658. {
  659. struct registryop_entry *registryop;
  660. struct registrykv_entry *registrykv;
  661. HKEY root, subkey;
  662. WCHAR *path;
  663. REGSAM sam = KEY_ALL_ACCESS;
  664. BOOL ret = TRUE;
  665. #ifdef __x86_64__
  666. if (!wcscmp(assembly->identity.architecture, L"x86")) sam |= KEY_WOW64_32KEY;
  667. #endif
  668. LIST_FOR_EACH_ENTRY(registryop, &assembly->registryops, struct registryop_entry, entry)
  669. {
  670. if (!(path = split_registry_key(registryop->key, &root)))
  671. {
  672. ret = FALSE;
  673. break;
  674. }
  675. TRACE("Processing registry key %s\n", debugstr_w(registryop->key));
  676. if (!dryrun && RegCreateKeyExW(root, path, 0, NULL, 0, sam, NULL, &subkey, NULL))
  677. {
  678. ERR("Failed to open registry key %s\n", debugstr_w(registryop->key));
  679. ret = FALSE;
  680. break;
  681. }
  682. LIST_FOR_EACH_ENTRY(registrykv, &registryop->keyvalues, struct registrykv_entry, entry)
  683. {
  684. if (!(ret = install_registry_value(assembly, subkey, registrykv, dryrun))) break;
  685. }
  686. if (!dryrun) RegCloseKey(subkey);
  687. if (!ret) break;
  688. }
  689. return ret;
  690. }
  691. static BOOL install_assembly(struct list *manifest_list, struct assembly_identity *identity, BOOL dryrun)
  692. {
  693. struct dependency_entry *dependency;
  694. struct assembly_entry *assembly;
  695. const WCHAR *name;
  696. if (!(assembly = lookup_assembly(manifest_list, identity)))
  697. {
  698. FIXME("Assembly %s not found\n", debugstr_w(identity->name));
  699. return FALSE;
  700. }
  701. name = assembly->identity.name;
  702. if (assembly->status == ASSEMBLY_STATUS_INSTALLED)
  703. {
  704. TRACE("Assembly %s already installed\n", debugstr_w(name));
  705. return TRUE;
  706. }
  707. if (assembly->status == ASSEMBLY_STATUS_IN_PROGRESS)
  708. {
  709. ERR("Assembly %s caused circular dependency\n", debugstr_w(name));
  710. return FALSE;
  711. }
  712. #ifdef __i386__
  713. if (!wcscmp(assembly->identity.architecture, L"amd64"))
  714. {
  715. ERR("Cannot install amd64 assembly in 32-bit prefix\n");
  716. return FALSE;
  717. }
  718. #endif
  719. assembly->status = ASSEMBLY_STATUS_IN_PROGRESS;
  720. LIST_FOR_EACH_ENTRY(dependency, &assembly->dependencies, struct dependency_entry, entry)
  721. {
  722. if (!install_assembly(manifest_list, &dependency->identity, dryrun)) return FALSE;
  723. }
  724. TRACE("Installing assembly %s%s\n", debugstr_w(name), dryrun ? " (dryrun)" : "");
  725. if (!install_files(assembly, dryrun))
  726. {
  727. ERR("Failed to install all files for %s\n", debugstr_w(name));
  728. return FALSE;
  729. }
  730. if (!install_registry(assembly, dryrun))
  731. {
  732. ERR("Failed to install registry keys for %s\n", debugstr_w(name));
  733. return FALSE;
  734. }
  735. TRACE("Installation of %s finished\n", debugstr_w(name));
  736. assembly->status = ASSEMBLY_STATUS_INSTALLED;
  737. return TRUE;
  738. }
  739. static BOOL install_updates(struct installer_state *state, BOOL dryrun)
  740. {
  741. struct dependency_entry *dependency;
  742. LIST_FOR_EACH_ENTRY(dependency, &state->updates, struct dependency_entry, entry)
  743. {
  744. if (!install_assembly(&state->assemblies, &dependency->identity, dryrun))
  745. {
  746. ERR("Failed to install update %s\n", debugstr_w(dependency->identity.name));
  747. return FALSE;
  748. }
  749. }
  750. return TRUE;
  751. }
  752. static void set_assembly_status(struct list *manifest_list, DWORD status)
  753. {
  754. struct assembly_entry *assembly;
  755. LIST_FOR_EACH_ENTRY(assembly, manifest_list, struct assembly_entry, entry)
  756. {
  757. assembly->status = status;
  758. }
  759. }
  760. static BOOL install_msu(const WCHAR *filename, struct installer_state *state)
  761. {
  762. const WCHAR *temp_path;
  763. WIN32_FIND_DATAW data;
  764. HANDLE search;
  765. WCHAR *path;
  766. BOOL ret = FALSE;
  767. list_init(&state->tempdirs);
  768. list_init(&state->assemblies);
  769. list_init(&state->updates);
  770. CoInitialize(NULL);
  771. TRACE("Processing msu file %s\n", debugstr_w(filename));
  772. if (!(temp_path = create_temp_directory(state))) return FALSE;
  773. if (!extract_cabinet(filename, temp_path))
  774. {
  775. ERR("Failed to extract %s\n", debugstr_w(filename));
  776. goto done;
  777. }
  778. /* load all manifests from contained cabinet archives */
  779. if (!(path = path_combine(temp_path, L"*.cab"))) goto done;
  780. search = FindFirstFileW(path, &data);
  781. heap_free(path);
  782. if (search != INVALID_HANDLE_VALUE)
  783. {
  784. do
  785. {
  786. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
  787. if (!wcsicmp(data.cFileName, L"WSUSSCAN.cab")) continue;
  788. if (!(path = path_combine(temp_path, data.cFileName))) continue;
  789. if (!load_assemblies_from_cab(path, state))
  790. ERR("Failed to load all manifests from %s, ignoring\n", debugstr_w(path));
  791. heap_free(path);
  792. }
  793. while (FindNextFileW(search, &data));
  794. FindClose(search);
  795. }
  796. /* load all update descriptions */
  797. if (!(path = path_combine(temp_path, L"*.xml"))) goto done;
  798. search = FindFirstFileW(path, &data);
  799. heap_free(path);
  800. if (search != INVALID_HANDLE_VALUE)
  801. {
  802. do
  803. {
  804. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;
  805. if (!(path = path_combine(temp_path, data.cFileName))) continue;
  806. if (!load_update(path, &state->updates))
  807. ERR("Failed to load all updates from %s, ignoring\n", debugstr_w(path));
  808. heap_free(path);
  809. }
  810. while (FindNextFileW(search, &data));
  811. FindClose(search);
  812. }
  813. /* dump package information (for debugging) */
  814. if (TRACE_ON(wusa))
  815. {
  816. struct dependency_entry *dependency;
  817. struct assembly_entry *assembly;
  818. TRACE("List of updates:\n");
  819. LIST_FOR_EACH_ENTRY(dependency, &state->updates, struct dependency_entry, entry)
  820. TRACE(" * %s\n", debugstr_w(dependency->identity.name));
  821. TRACE("List of manifests (with dependencies):\n");
  822. LIST_FOR_EACH_ENTRY(assembly, &state->assemblies, struct assembly_entry, entry)
  823. {
  824. TRACE(" * %s\n", debugstr_w(assembly->identity.name));
  825. LIST_FOR_EACH_ENTRY(dependency, &assembly->dependencies, struct dependency_entry, entry)
  826. TRACE(" -> %s\n", debugstr_w(dependency->identity.name));
  827. }
  828. }
  829. if (list_empty(&state->updates))
  830. {
  831. ERR("No updates found, probably incompatible MSU file format?\n");
  832. goto done;
  833. }
  834. /* perform dry run */
  835. set_assembly_status(&state->assemblies, ASSEMBLY_STATUS_NONE);
  836. if (!install_updates(state, TRUE))
  837. {
  838. ERR("Dry run failed, aborting installation\n");
  839. goto done;
  840. }
  841. /* installation */
  842. set_assembly_status(&state->assemblies, ASSEMBLY_STATUS_NONE);
  843. if (!install_updates(state, FALSE))
  844. {
  845. ERR("Installation failed\n");
  846. goto done;
  847. }
  848. TRACE("Installation finished\n");
  849. ret = TRUE;
  850. done:
  851. installer_cleanup(state);
  852. return ret;
  853. }
  854. static void restart_as_x86_64(void)
  855. {
  856. WCHAR filename[MAX_PATH];
  857. PROCESS_INFORMATION pi;
  858. STARTUPINFOW si;
  859. DWORD exit_code = 1;
  860. void *redir;
  861. memset(&si, 0, sizeof(si));
  862. si.cb = sizeof(si);
  863. GetSystemDirectoryW( filename, MAX_PATH );
  864. wcscat( filename, L"\\wusa.exe" );
  865. Wow64DisableWow64FsRedirection(&redir);
  866. if (CreateProcessW(filename, GetCommandLineW(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
  867. {
  868. TRACE("Restarting %s\n", wine_dbgstr_w(filename));
  869. WaitForSingleObject(pi.hProcess, INFINITE);
  870. GetExitCodeProcess(pi.hProcess, &exit_code);
  871. CloseHandle(pi.hProcess);
  872. CloseHandle(pi.hThread);
  873. }
  874. else ERR("Failed to restart 64-bit %s, err %u\n", wine_dbgstr_w(filename), GetLastError());
  875. Wow64RevertWow64FsRedirection(redir);
  876. ExitProcess(exit_code);
  877. }
  878. int __cdecl wmain(int argc, WCHAR *argv[])
  879. {
  880. struct installer_state state;
  881. const WCHAR *filename = NULL;
  882. BOOL is_wow64;
  883. int i;
  884. if (IsWow64Process( GetCurrentProcess(), &is_wow64 ) && is_wow64) restart_as_x86_64();
  885. state.norestart = FALSE;
  886. state.quiet = FALSE;
  887. if (TRACE_ON(wusa))
  888. {
  889. TRACE("Command line:");
  890. for (i = 0; i < argc; i++)
  891. TRACE(" %s", wine_dbgstr_w(argv[i]));
  892. TRACE("\n");
  893. }
  894. for (i = 1; i < argc; i++)
  895. {
  896. if (argv[i][0] == '/')
  897. {
  898. if (!wcscmp(argv[i], L"/norestart"))
  899. state.norestart = TRUE;
  900. else if (!wcscmp(argv[i], L"/quiet"))
  901. state.quiet = TRUE;
  902. else
  903. FIXME("Unknown option: %s\n", wine_dbgstr_w(argv[i]));
  904. }
  905. else if (!filename)
  906. filename = argv[i];
  907. else
  908. FIXME("Unknown option: %s\n", wine_dbgstr_w(argv[i]));
  909. }
  910. if (!filename)
  911. {
  912. FIXME("Missing filename argument\n");
  913. return 1;
  914. }
  915. return !install_msu(filename, &state);
  916. }