xref: /reactos/dll/win32/msi/custom.c (revision 74f53b4b)
1 /*
2  * Custom Action processing for the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2005 Aric Stewart for CodeWeavers
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 
21 #define COBJMACROS
22 
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "msidefs.h"
29 #include "winuser.h"
30 #include "objbase.h"
31 #include "oleauto.h"
32 
33 #include "msipriv.h"
34 #include "winemsi_s.h"
35 #include "wine/asm.h"
36 #include "wine/heap.h"
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "wine/exception.h"
40 
41 #ifdef __REACTOS__
42 #ifndef STATUS_ACCESS_VIOLATION
43 #define STATUS_ACCESS_VIOLATION ((NTSTATUS)0xC0000005)
44 #endif
45 #endif
46 
47 WINE_DEFAULT_DEBUG_CHANNEL(msi);
48 
49 #define CUSTOM_ACTION_TYPE_MASK 0x3F
50 
51 typedef struct tagMSIRUNNINGACTION
52 {
53     struct list entry;
54     HANDLE handle;
55     BOOL   process;
56     LPWSTR name;
57 } MSIRUNNINGACTION;
58 
59 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
60 
61 static CRITICAL_SECTION msi_custom_action_cs;
62 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
63 {
64     0, 0, &msi_custom_action_cs,
65     { &msi_custom_action_cs_debug.ProcessLocksList,
66       &msi_custom_action_cs_debug.ProcessLocksList },
67       0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
68 };
69 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
70 
71 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
72 
73 void  __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
74 {
75     return heap_alloc(len);
76 }
77 
78 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
79 {
80     heap_free(ptr);
81 }
82 
83 LONG WINAPI rpc_filter(EXCEPTION_POINTERS *eptr)
84 {
85     return I_RpcExceptionFilter(eptr->ExceptionRecord->ExceptionCode);
86 }
87 
88 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
89 {
90     UINT count;
91     WCHAR **newbuf = NULL;
92 
93     if (script >= SCRIPT_MAX)
94     {
95         FIXME("Unknown script requested %u\n", script);
96         return ERROR_FUNCTION_FAILED;
97     }
98     TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
99 
100     count = package->script_actions_count[script];
101     package->script_actions_count[script]++;
102     if (count != 0) newbuf = msi_realloc( package->script_actions[script],
103                                           package->script_actions_count[script] * sizeof(WCHAR *) );
104     else newbuf = msi_alloc( sizeof(WCHAR *) );
105 
106     newbuf[count] = strdupW( action );
107     package->script_actions[script] = newbuf;
108     return ERROR_SUCCESS;
109 }
110 
111 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
112 {
113     UINT count;
114     WCHAR **newbuf = NULL;
115 
116     TRACE("Registering %s as unique action\n", debugstr_w(action));
117 
118     count = package->unique_actions_count;
119     package->unique_actions_count++;
120     if (count != 0) newbuf = msi_realloc( package->unique_actions,
121                                           package->unique_actions_count * sizeof(WCHAR *) );
122     else newbuf = msi_alloc( sizeof(WCHAR *) );
123 
124     newbuf[count] = strdupW( action );
125     package->unique_actions = newbuf;
126     return ERROR_SUCCESS;
127 }
128 
129 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
130 {
131     UINT i;
132 
133     for (i = 0; i < package->unique_actions_count; i++)
134     {
135         if (!wcscmp( package->unique_actions[i], action )) return TRUE;
136     }
137     return FALSE;
138 }
139 
140 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
141 {
142     if ((options & msidbCustomActionTypeClientRepeat) ==
143             msidbCustomActionTypeClientRepeat)
144     {
145         if (!(package->InWhatSequence & SEQUENCE_UI &&
146             package->InWhatSequence & SEQUENCE_EXEC))
147         {
148             TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
149             return FALSE;
150         }
151     }
152     else if (options & msidbCustomActionTypeFirstSequence)
153     {
154         if (package->InWhatSequence & SEQUENCE_UI &&
155             package->InWhatSequence & SEQUENCE_EXEC )
156         {
157             TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
158             return FALSE;
159         }
160     }
161     else if (options & msidbCustomActionTypeOncePerProcess)
162     {
163         if (msi_action_is_unique(package, action))
164         {
165             TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
166             return FALSE;
167         }
168         else
169             msi_register_unique_action(package, action);
170     }
171 
172     return TRUE;
173 }
174 
175 /* stores the following properties before the action:
176  *
177  *    [CustomActionData<=>UserSID<=>ProductCode]Action
178  */
179 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
180                                       LPCWSTR usersid, LPCWSTR prodcode)
181 {
182     LPWSTR deferred;
183     DWORD len;
184 
185     if (!actiondata)
186         return strdupW(action);
187 
188     len = lstrlenW(action) + lstrlenW(actiondata) +
189           lstrlenW(usersid) + lstrlenW(prodcode) +
190           lstrlenW(L"[%s<=>%s<=>%s]%s") - 7;
191     deferred = msi_alloc(len * sizeof(WCHAR));
192 
193     swprintf(deferred, len, L"[%s<=>%s<=>%s]%s", actiondata, usersid, prodcode, action);
194     return deferred;
195 }
196 
197 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
198 {
199     const WCHAR *end, *beg = deferred_data + 1;
200 
201     end = wcsstr(beg, L"<=>");
202     msi_set_property( package->db, L"CustomActionData", beg, end - beg );
203     beg = end + 3;
204 
205     end = wcsstr(beg, L"<=>");
206     msi_set_property( package->db, L"UserSID", beg, end - beg );
207     beg = end + 3;
208 
209     end = wcschr(beg, ']');
210     msi_set_property( package->db, L"ProductCode", beg, end - beg );
211 }
212 
213 WCHAR *msi_create_temp_file( MSIDATABASE *db )
214 {
215     WCHAR *ret;
216 
217     if (!db->tempfolder)
218     {
219         WCHAR tmp[MAX_PATH];
220         UINT len = ARRAY_SIZE( tmp );
221 
222         if (msi_get_property( db, L"TempFolder", tmp, &len ) ||
223             GetFileAttributesW( tmp ) != FILE_ATTRIBUTE_DIRECTORY)
224         {
225             GetTempPathW( MAX_PATH, tmp );
226         }
227         if (!(db->tempfolder = strdupW( tmp ))) return NULL;
228     }
229 
230     if ((ret = msi_alloc( (lstrlenW( db->tempfolder ) + 20) * sizeof(WCHAR) )))
231     {
232         if (!GetTempFileNameW( db->tempfolder, L"msi", 0, ret ))
233         {
234             msi_free( ret );
235             return NULL;
236         }
237     }
238 
239     return ret;
240 }
241 
242 static MSIBINARY *create_temp_binary(MSIPACKAGE *package, LPCWSTR source)
243 {
244     MSIRECORD *row;
245     MSIBINARY *binary = NULL;
246     HANDLE file;
247     CHAR buffer[1024];
248     WCHAR *tmpfile;
249     DWORD sz, write;
250     UINT r;
251 
252     if (!(tmpfile = msi_create_temp_file( package->db ))) return NULL;
253 
254     if (!(row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", source ))) goto error;
255     if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) ))) goto error;
256 
257     file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
258     if (file == INVALID_HANDLE_VALUE) goto error;
259 
260     do
261     {
262         sz = sizeof(buffer);
263         r = MSI_RecordReadStream( row, 2, buffer, &sz );
264         if (r != ERROR_SUCCESS)
265         {
266             ERR("Failed to get stream\n");
267             break;
268         }
269         WriteFile( file, buffer, sz, &write, NULL );
270     } while (sz == sizeof buffer);
271 
272     CloseHandle( file );
273     if (r != ERROR_SUCCESS) goto error;
274 
275     binary->source = strdupW( source );
276     binary->tmpfile = tmpfile;
277     list_add_tail( &package->binaries, &binary->entry );
278 
279     msiobj_release( &row->hdr );
280     return binary;
281 
282 error:
283     if (row) msiobj_release( &row->hdr );
284     DeleteFileW( tmpfile );
285     msi_free( tmpfile );
286     msi_free( binary );
287     return NULL;
288 }
289 
290 static MSIBINARY *get_temp_binary(MSIPACKAGE *package, LPCWSTR source)
291 {
292     MSIBINARY *binary;
293 
294     LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
295     {
296         if (!wcscmp( binary->source, source ))
297             return binary;
298     }
299 
300     return create_temp_binary(package, source);
301 }
302 
303 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
304                                 BOOL process, LPCWSTR name)
305 {
306     MSIRUNNINGACTION *action;
307 
308     action = msi_alloc( sizeof(MSIRUNNINGACTION) );
309 
310     action->handle = Handle;
311     action->process = process;
312     action->name = strdupW(name);
313 
314     list_add_tail( &package->RunningActions, &action->entry );
315 }
316 
317 static UINT custom_get_process_return( HANDLE process )
318 {
319     DWORD rc = 0;
320 
321     GetExitCodeProcess( process, &rc );
322     TRACE("exit code is %u\n", rc);
323     if (rc != 0)
324         return ERROR_FUNCTION_FAILED;
325     return ERROR_SUCCESS;
326 }
327 
328 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
329 {
330     DWORD rc = 0;
331 
332     GetExitCodeThread( thread, &rc );
333 
334     switch (rc)
335     {
336     case ERROR_FUNCTION_NOT_CALLED:
337     case ERROR_SUCCESS:
338     case ERROR_INSTALL_USEREXIT:
339     case ERROR_INSTALL_FAILURE:
340         return rc;
341     case ERROR_NO_MORE_ITEMS:
342         return ERROR_SUCCESS;
343     case ERROR_INSTALL_SUSPEND:
344         ACTION_ForceReboot( package );
345         return ERROR_SUCCESS;
346     default:
347         ERR("Invalid Return Code %d\n",rc);
348         return ERROR_INSTALL_FAILURE;
349     }
350 }
351 
352 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
353                            HANDLE ProcessHandle, LPCWSTR name)
354 {
355     UINT rc = ERROR_SUCCESS;
356 
357     if (!(type & msidbCustomActionTypeAsync))
358     {
359         TRACE("waiting for %s\n", debugstr_w(name));
360 
361         msi_dialog_check_messages(ProcessHandle);
362 
363         if (!(type & msidbCustomActionTypeContinue))
364             rc = custom_get_process_return(ProcessHandle);
365 
366         CloseHandle(ProcessHandle);
367     }
368     else
369     {
370         TRACE("%s running in background\n", debugstr_w(name));
371 
372         if (!(type & msidbCustomActionTypeContinue))
373             file_running_action(package, ProcessHandle, TRUE, name);
374         else
375             CloseHandle(ProcessHandle);
376     }
377 
378     return rc;
379 }
380 
381 typedef struct _msi_custom_action_info {
382     struct list entry;
383     MSIPACKAGE *package;
384     LPWSTR source;
385     LPWSTR target;
386     HANDLE handle;
387     LPWSTR action;
388     INT type;
389     GUID guid;
390     DWORD arch;
391 } msi_custom_action_info;
392 
393 static void free_custom_action_data( msi_custom_action_info *info )
394 {
395     EnterCriticalSection( &msi_custom_action_cs );
396 
397     list_remove( &info->entry );
398     if (info->handle)
399         CloseHandle( info->handle );
400     msi_free( info->action );
401     msi_free( info->source );
402     msi_free( info->target );
403     msiobj_release( &info->package->hdr );
404     msi_free( info );
405 
406     LeaveCriticalSection( &msi_custom_action_cs );
407 }
408 
409 static UINT wait_thread_handle( msi_custom_action_info *info )
410 {
411     UINT rc = ERROR_SUCCESS;
412 
413     if (!(info->type & msidbCustomActionTypeAsync))
414     {
415         TRACE("waiting for %s\n", debugstr_w( info->action ));
416 
417         msi_dialog_check_messages( info->handle );
418 
419         if (!(info->type & msidbCustomActionTypeContinue))
420             rc = custom_get_thread_return( info->package, info->handle );
421 
422         free_custom_action_data( info );
423     }
424     else
425     {
426         TRACE("%s running in background\n", debugstr_w( info->action ));
427     }
428 
429     return rc;
430 }
431 
432 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
433 {
434     msi_custom_action_info *info;
435     BOOL found = FALSE;
436 
437     EnterCriticalSection( &msi_custom_action_cs );
438 
439     LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
440     {
441         if (IsEqualGUID( &info->guid, guid ))
442         {
443             found = TRUE;
444             break;
445         }
446     }
447 
448     LeaveCriticalSection( &msi_custom_action_cs );
449 
450     if (!found)
451         return NULL;
452 
453     return info;
454 }
455 
456 static void handle_msi_break(LPCSTR target)
457 {
458     char format[] = "To debug your custom action, attach your debugger to "
459                     "process %i (0x%X) and press OK";
460     char val[MAX_PATH];
461     char msg[100];
462 
463     if (!GetEnvironmentVariableA("MsiBreak", val, MAX_PATH))
464         return;
465 
466     if (strcmp(val, target))
467         return;
468 
469     sprintf(msg, format, GetCurrentProcessId(), GetCurrentProcessId());
470     MessageBoxA(NULL, msg, "Windows Installer", MB_OK);
471     DebugBreak();
472 }
473 
474 #if defined __i386__ && defined _MSC_VER
475 __declspec(naked) UINT custom_proc_wrapper(MsiCustomActionEntryPoint entry, MSIHANDLE hinst)
476 {
477     __asm
478     {
479         push ebp
480         mov ebp, esp
481         sub esp, 4
482         push [ebp+12]
483         call [ebp+8]
484         leave
485         ret
486     }
487 }
488 #elif defined __i386__ && defined __GNUC__
489 /* wrapper for apps that don't declare the thread function correctly */
490 extern UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst );
491 __ASM_GLOBAL_FUNC(custom_proc_wrapper,
492                   "pushl %ebp\n\t"
493                   __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
494                   __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
495                   "movl %esp,%ebp\n\t"
496                   __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
497                   "subl $4,%esp\n\t"
498                   "pushl 12(%ebp)\n\t"
499                   "call *8(%ebp)\n\t"
500                   "leave\n\t"
501                   __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
502                   __ASM_CFI(".cfi_same_value %ebp\n\t")
503                   "ret" )
504 #else
505 static UINT custom_proc_wrapper( MsiCustomActionEntryPoint entry, MSIHANDLE hinst )
506 {
507     return entry(hinst);
508 }
509 #endif
510 
511 UINT CDECL __wine_msi_call_dll_function(DWORD client_pid, const GUID *guid)
512 {
513     MsiCustomActionEntryPoint fn;
514     MSIHANDLE remote_package = 0;
515     RPC_WSTR binding_str;
516     MSIHANDLE hPackage;
517     RPC_STATUS status;
518     LPWSTR dll = NULL;
519     LPSTR proc = NULL;
520     HANDLE hModule;
521     INT type;
522     UINT r;
523 
524     TRACE("%s\n", debugstr_guid( guid ));
525 
526     if (!rpc_handle)
527     {
528         WCHAR endpoint[12];
529 
530         swprintf(endpoint, ARRAY_SIZE(endpoint), L"msi%x", client_pid);
531         status = RpcStringBindingComposeW(NULL, (WCHAR *)L"ncalrpc", NULL, endpoint, NULL, &binding_str);
532         if (status != RPC_S_OK)
533         {
534             ERR("RpcStringBindingCompose failed: %#x\n", status);
535             return status;
536         }
537         status = RpcBindingFromStringBindingW(binding_str, &rpc_handle);
538         if (status != RPC_S_OK)
539         {
540             ERR("RpcBindingFromStringBinding failed: %#x\n", status);
541             return status;
542         }
543         RpcStringFreeW(&binding_str);
544     }
545 
546     r = remote_GetActionInfo(guid, &type, &dll, &proc, &remote_package);
547     if (r != ERROR_SUCCESS)
548         return r;
549 
550     hPackage = alloc_msi_remote_handle( remote_package );
551     if (!hPackage)
552     {
553         ERR( "failed to create handle for %x\n", remote_package );
554         midl_user_free( dll );
555         midl_user_free( proc );
556         return ERROR_INSTALL_FAILURE;
557     }
558 
559     hModule = LoadLibraryW( dll );
560     if (!hModule)
561     {
562         ERR( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
563         midl_user_free( dll );
564         midl_user_free( proc );
565         MsiCloseHandle( hPackage );
566         return ERROR_SUCCESS;
567     }
568 
569     fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
570     if (!fn) WARN( "GetProcAddress(%s) failed\n", debugstr_a(proc) );
571     else
572     {
573         handle_msi_break(proc);
574 
575         __TRY
576         {
577             r = custom_proc_wrapper( fn, hPackage );
578         }
579         __EXCEPT_PAGE_FAULT
580         {
581             ERR( "Custom action (%s:%s) caused a page fault: %08x\n",
582                  debugstr_w(dll), debugstr_a(proc), GetExceptionCode() );
583             r = ERROR_SUCCESS;
584         }
585         __ENDTRY;
586     }
587 
588     FreeLibrary(hModule);
589 
590     midl_user_free(dll);
591     midl_user_free(proc);
592 
593     MsiCloseAllHandles();
594 
595     return r;
596 }
597 
598 static DWORD custom_start_server(MSIPACKAGE *package, DWORD arch)
599 {
600     WCHAR path[MAX_PATH], cmdline[MAX_PATH + 23];
601     PROCESS_INFORMATION pi = {0};
602     STARTUPINFOW si = {0};
603     WCHAR buffer[24];
604     void *cookie;
605     HANDLE pipe;
606     BOOL wow64;
607 
608     if ((arch == SCS_32BIT_BINARY && package->custom_server_32_process) ||
609         (arch == SCS_64BIT_BINARY && package->custom_server_64_process))
610         return ERROR_SUCCESS;
611 
612     swprintf(buffer, ARRAY_SIZE(buffer), L"\\\\.\\pipe\\msica_%x_%d",
613              GetCurrentProcessId(), arch == SCS_32BIT_BINARY ? 32 : 64);
614     pipe = CreateNamedPipeW(buffer, PIPE_ACCESS_DUPLEX, 0, 1, sizeof(DWORD64),
615         sizeof(GUID), 0, NULL);
616     if (pipe == INVALID_HANDLE_VALUE)
617         ERR("Failed to create custom action client pipe: %u\n", GetLastError());
618 
619     if (!IsWow64Process(GetCurrentProcess(), &wow64))
620         wow64 = FALSE;
621 
622     if ((sizeof(void *) == 8 || wow64) && arch == SCS_32BIT_BINARY)
623         GetSystemWow64DirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
624     else
625         GetSystemDirectoryW(path, MAX_PATH - ARRAY_SIZE(L"\\msiexec.exe"));
626     lstrcatW(path, L"\\msiexec.exe");
627     swprintf(cmdline, ARRAY_SIZE(cmdline), L"%s -Embedding %d", path, GetCurrentProcessId());
628 
629     if (wow64 && arch == SCS_64BIT_BINARY)
630     {
631         Wow64DisableWow64FsRedirection(&cookie);
632         CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
633         Wow64RevertWow64FsRedirection(cookie);
634     }
635     else
636         CreateProcessW(path, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
637 
638     CloseHandle(pi.hThread);
639 
640     if (arch == SCS_32BIT_BINARY)
641     {
642         package->custom_server_32_process = pi.hProcess;
643         package->custom_server_32_pipe = pipe;
644     }
645     else
646     {
647         package->custom_server_64_process = pi.hProcess;
648         package->custom_server_64_pipe = pipe;
649     }
650 
651     if (!ConnectNamedPipe(pipe, NULL))
652     {
653         ERR("Failed to connect to custom action server: %u\n", GetLastError());
654         return GetLastError();
655     }
656 
657     return ERROR_SUCCESS;
658 }
659 
660 void custom_stop_server(HANDLE process, HANDLE pipe)
661 {
662     DWORD size;
663 
664     WriteFile(pipe, &GUID_NULL, sizeof(GUID_NULL), &size, NULL);
665     WaitForSingleObject(process, INFINITE);
666     CloseHandle(process);
667     CloseHandle(pipe);
668 }
669 
670 static DWORD WINAPI custom_client_thread(void *arg)
671 {
672     msi_custom_action_info *info = arg;
673     DWORD64 thread64;
674     HANDLE process;
675     HANDLE thread;
676     HANDLE pipe;
677     DWORD size;
678     UINT rc;
679 
680     CoInitializeEx(NULL, COINIT_MULTITHREADED); /* needed to marshal streams */
681 
682     if (info->arch == SCS_32BIT_BINARY)
683     {
684         process = info->package->custom_server_32_process;
685         pipe = info->package->custom_server_32_pipe;
686     }
687     else
688     {
689         process = info->package->custom_server_64_process;
690         pipe = info->package->custom_server_64_pipe;
691     }
692 
693     EnterCriticalSection(&msi_custom_action_cs);
694 
695     if (!WriteFile(pipe, &info->guid, sizeof(info->guid), &size, NULL) ||
696         size != sizeof(info->guid))
697     {
698         ERR("Failed to write to custom action client pipe: %u\n", GetLastError());
699         LeaveCriticalSection(&msi_custom_action_cs);
700         return GetLastError();
701     }
702     if (!ReadFile(pipe, &thread64, sizeof(thread64), &size, NULL) || size != sizeof(thread64))
703     {
704         ERR("Failed to read from custom action client pipe: %u\n", GetLastError());
705         LeaveCriticalSection(&msi_custom_action_cs);
706         return GetLastError();
707     }
708 
709     LeaveCriticalSection(&msi_custom_action_cs);
710 
711     if (DuplicateHandle(process, (HANDLE)(DWORD_PTR)thread64, GetCurrentProcess(),
712         &thread, 0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
713     {
714         WaitForSingleObject(thread, INFINITE);
715         GetExitCodeThread(thread, &rc);
716         CloseHandle(thread);
717     }
718     else
719         rc = GetLastError();
720 
721     CoUninitialize();
722     return rc;
723 }
724 
725 static msi_custom_action_info *do_msidbCustomActionTypeDll(
726     MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
727 {
728     msi_custom_action_info *info;
729     RPC_STATUS status;
730     BOOL ret;
731 
732     info = msi_alloc( sizeof *info );
733     if (!info)
734         return NULL;
735 
736     msiobj_addref( &package->hdr );
737     info->package = package;
738     info->type = type;
739     info->target = strdupW( target );
740     info->source = strdupW( source );
741     info->action = strdupW( action );
742     CoCreateGuid( &info->guid );
743 
744     EnterCriticalSection( &msi_custom_action_cs );
745     list_add_tail( &msi_pending_custom_actions, &info->entry );
746     LeaveCriticalSection( &msi_custom_action_cs );
747 
748     if (!package->rpc_server_started)
749     {
750         WCHAR endpoint[12];
751 
752         swprintf(endpoint, ARRAY_SIZE(endpoint), L"msi%x", GetCurrentProcessId());
753         status = RpcServerUseProtseqEpW((WCHAR *)L"ncalrpc", RPC_C_PROTSEQ_MAX_REQS_DEFAULT,
754             endpoint, NULL);
755         if (status != RPC_S_OK)
756         {
757             ERR("RpcServerUseProtseqEp failed: %#x\n", status);
758             return NULL;
759         }
760 
761         status = RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec, NULL, NULL,
762             RPC_IF_AUTOLISTEN, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
763         if (status != RPC_S_OK)
764         {
765             ERR("RpcServerRegisterIfEx failed: %#x\n", status);
766             return NULL;
767         }
768 
769         info->package->rpc_server_started = 1;
770     }
771 
772     ret = GetBinaryTypeW(source, &info->arch);
773     if (!ret)
774         info->arch = (sizeof(void *) == 8 ? SCS_64BIT_BINARY : SCS_32BIT_BINARY);
775 
776     custom_start_server(package, info->arch);
777 
778     info->handle = CreateThread(NULL, 0, custom_client_thread, info, 0, NULL);
779     if (!info->handle)
780     {
781         free_custom_action_data( info );
782         return NULL;
783     }
784 
785     return info;
786 }
787 
788 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
789                                 INT type, const WCHAR *action )
790 {
791     msi_custom_action_info *info;
792     MSIBINARY *binary;
793 
794     if (!(binary = get_temp_binary(package, source)))
795         return ERROR_FUNCTION_FAILED;
796 
797     TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
798 
799     info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
800     return wait_thread_handle( info );
801 }
802 
803 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
804 {
805     STARTUPINFOW si;
806     PROCESS_INFORMATION info;
807     WCHAR *exe = NULL, *cmd = NULL, *p;
808     BOOL ret;
809 
810     if (app)
811     {
812         int len_arg = 0;
813         DWORD len_exe;
814 
815         if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
816         len_exe = SearchPathW( NULL, app, L".exe", MAX_PATH, exe, NULL );
817         if (len_exe >= MAX_PATH)
818         {
819             msi_free( exe );
820             if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
821             len_exe = SearchPathW( NULL, app, L".exe", len_exe, exe, NULL );
822         }
823         if (!len_exe)
824         {
825             ERR("can't find executable %u\n", GetLastError());
826             msi_free( exe );
827             return INVALID_HANDLE_VALUE;
828         }
829 
830         if (arg) len_arg = lstrlenW( arg );
831         if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
832         {
833             msi_free( exe );
834             return INVALID_HANDLE_VALUE;
835         }
836         p = cmd;
837         if (wcschr( exe, ' ' ))
838         {
839             *p++ = '\"';
840             memcpy( p, exe, len_exe * sizeof(WCHAR) );
841             p += len_exe;
842             *p++ = '\"';
843             *p = 0;
844         }
845         else
846         {
847             lstrcpyW( p, exe );
848             p += len_exe;
849         }
850         if (arg)
851         {
852             *p++ = ' ';
853             memcpy( p, arg, len_arg * sizeof(WCHAR) );
854             p[len_arg] = 0;
855         }
856     }
857     memset( &si, 0, sizeof(STARTUPINFOW) );
858     ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
859     msi_free( cmd );
860     msi_free( exe );
861     if (!ret)
862     {
863         ERR("unable to execute command %u\n", GetLastError());
864         return INVALID_HANDLE_VALUE;
865     }
866     CloseHandle( info.hThread );
867     return info.hProcess;
868 }
869 
870 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
871                                 INT type, const WCHAR *action )
872 {
873     MSIBINARY *binary;
874     HANDLE handle;
875     WCHAR *arg;
876 
877     if (!(binary = get_temp_binary(package, source)))
878         return ERROR_FUNCTION_FAILED;
879 
880     deformat_string( package, target, &arg );
881     TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
882 
883     handle = execute_command( binary->tmpfile, arg, L"C:\\" );
884     msi_free( arg );
885     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
886     return wait_process_handle( package, type, handle, action );
887 }
888 
889 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
890                                  INT type, const WCHAR *action )
891 {
892     msi_custom_action_info *info;
893     MSIFILE *file;
894 
895     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
896 
897     file = msi_get_loaded_file( package, source );
898     if (!file)
899     {
900         ERR("invalid file key %s\n", debugstr_w( source ));
901         return ERROR_FUNCTION_FAILED;
902     }
903 
904     info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
905     return wait_thread_handle( info );
906 }
907 
908 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
909                                  INT type, const WCHAR *action )
910 {
911     MSIFILE *file;
912     HANDLE handle;
913     WCHAR *arg;
914 
915     if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
916 
917     deformat_string( package, target, &arg );
918     TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
919 
920     handle = execute_command( file->TargetPath, arg, L"C:\\" );
921     msi_free( arg );
922     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
923     return wait_process_handle( package, type, handle, action );
924 }
925 
926 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
927                                  INT type, const WCHAR *action )
928 {
929     MSIRECORD *row = 0;
930     LPWSTR deformated = NULL;
931 
932     deformat_string( package, target, &deformated );
933 
934     /* first try treat the error as a number */
935     row = MSI_QueryGetRecord( package->db, L"SELECT `Message` FROM `Error` WHERE `Error` = '%s'", deformated );
936     if( row )
937     {
938         LPCWSTR error = MSI_RecordGetString( row, 1 );
939         if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
940             MessageBoxW( NULL, error, NULL, MB_OK );
941         msiobj_release( &row->hdr );
942     }
943     else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
944         MessageBoxW( NULL, deformated, NULL, MB_OK );
945 
946     msi_free( deformated );
947 
948     return ERROR_INSTALL_FAILURE;
949 }
950 
951 static WCHAR *build_msiexec_args( const WCHAR *filename, const WCHAR *params )
952 {
953     UINT len_filename = lstrlenW( filename ), len_params = lstrlenW( params );
954     UINT len = ARRAY_SIZE(L"/qb /i ") - 1;
955     WCHAR *ret;
956 
957     if (!(ret = msi_alloc( (len + len_filename + len_params + 4) * sizeof(WCHAR) ))) return NULL;
958     memcpy( ret, L"/qb /i ", sizeof(L"/qb /i ") );
959     ret[len++] = '"';
960     memcpy( ret + len, filename, len_filename * sizeof(WCHAR) );
961     len += len_filename;
962     ret[len++] = '"';
963     ret[len++] = ' ';
964     lstrcpyW( ret + len, params );
965     return ret;
966 }
967 
968 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
969                                  INT type, const WCHAR *action )
970 {
971     WCHAR *dir, *filename, *args, *p;
972     UINT len_dir, len_source = lstrlenW( source );
973     HANDLE handle;
974 
975     if (!(dir = msi_dup_property( package->db, L"OriginalDatabase" ))) return ERROR_OUTOFMEMORY;
976     if (!(p = wcsrchr( dir, '\\' )) && !(p = wcsrchr( dir, '/' )))
977     {
978         msi_free( dir );
979         return ERROR_FUNCTION_FAILED;
980     }
981     *p = 0;
982     len_dir = p - dir;
983     if (!(filename = msi_alloc( (len_dir + len_source + 2) * sizeof(WCHAR) )))
984     {
985         msi_free( dir );
986         return ERROR_OUTOFMEMORY;
987     }
988     memcpy( filename, dir, len_dir * sizeof(WCHAR) );
989     filename[len_dir++] = '\\';
990     memcpy( filename + len_dir, source, len_source * sizeof(WCHAR) );
991     filename[len_dir + len_source] = 0;
992 
993     if (!(args = build_msiexec_args( filename, target )))
994     {
995         msi_free( dir );
996         return ERROR_OUTOFMEMORY;
997     }
998 
999     TRACE("installing %s concurrently\n", debugstr_w(source));
1000 
1001     handle = execute_command( L"msiexec", args, dir );
1002     msi_free( dir );
1003     msi_free( args );
1004     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1005     return wait_process_handle( package, type, handle, action );
1006 }
1007 
1008 static UINT write_substorage_to_file( MSIPACKAGE *package, const WCHAR *source, const WCHAR *filename )
1009 {
1010     IStorage *src = NULL, *dst = NULL;
1011     UINT r = ERROR_FUNCTION_FAILED;
1012     HRESULT hr;
1013 
1014     hr = StgCreateDocfile( filename, STGM_CREATE|STGM_TRANSACTED|STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, &dst );
1015     if (FAILED( hr ))
1016     {
1017         WARN( "can't open destination storage %s (%08x)\n", debugstr_w(filename), hr );
1018         goto done;
1019     }
1020 
1021     hr = IStorage_OpenStorage( package->db->storage, source, NULL, STGM_SHARE_EXCLUSIVE, NULL, 0, &src );
1022     if (FAILED( hr ))
1023     {
1024         WARN( "can't open source storage %s (%08x)\n", debugstr_w(source), hr );
1025         goto done;
1026     }
1027 
1028     hr = IStorage_CopyTo( src, 0, NULL, NULL, dst );
1029     if (FAILED( hr ))
1030     {
1031         ERR( "failed to copy storage %s (%08x)\n", debugstr_w(source), hr );
1032         goto done;
1033     }
1034 
1035     hr = IStorage_Commit( dst, 0 );
1036     if (FAILED( hr ))
1037         ERR( "failed to commit storage (%08x)\n", hr );
1038     else
1039         r = ERROR_SUCCESS;
1040 
1041 done:
1042     if (src) IStorage_Release( src );
1043     if (dst) IStorage_Release( dst );
1044     return r;
1045 }
1046 
1047 static UINT HANDLE_CustomType7( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1048                                 INT type, const WCHAR *action )
1049 {
1050     WCHAR *tmpfile, *args;
1051     MSIBINARY *binary = NULL;
1052     HANDLE handle;
1053     UINT r;
1054 
1055     if (!(tmpfile = msi_create_temp_file( package->db ))) return ERROR_FUNCTION_FAILED;
1056 
1057     r = write_substorage_to_file( package, source, tmpfile );
1058     if (r != ERROR_SUCCESS)
1059         goto error;
1060 
1061     if (!(binary = msi_alloc( sizeof(*binary) ))) goto error;
1062     binary->source  = NULL;
1063     binary->tmpfile = tmpfile;
1064     list_add_tail( &package->binaries, &binary->entry );
1065 
1066     if (!(args = build_msiexec_args( tmpfile, target ))) return ERROR_OUTOFMEMORY;
1067 
1068     TRACE("installing %s concurrently\n", debugstr_w(source));
1069 
1070     handle = execute_command( L"msiexec", args, L"C:\\" );
1071     msi_free( args );
1072     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1073     return wait_process_handle( package, type, handle, action );
1074 
1075 error:
1076     DeleteFileW( tmpfile );
1077     msi_free( tmpfile );
1078     return ERROR_FUNCTION_FAILED;
1079 }
1080 
1081 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1082                                  INT type, const WCHAR *action )
1083 {
1084     WCHAR *exe, *arg;
1085     HANDLE handle;
1086 
1087     if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
1088 
1089     deformat_string( package, target, &arg );
1090     TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
1091 
1092     handle = execute_command( exe, arg, L"C:\\" );
1093     msi_free( exe );
1094     msi_free( arg );
1095     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1096     return wait_process_handle( package, type, handle, action );
1097 }
1098 
1099 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1100                                  INT type, const WCHAR *action )
1101 {
1102     const WCHAR *workingdir = NULL;
1103     HANDLE handle;
1104     WCHAR *cmd;
1105 
1106     if (source)
1107     {
1108         workingdir = msi_get_target_folder( package, source );
1109         if (!workingdir) return ERROR_FUNCTION_FAILED;
1110     }
1111     deformat_string( package, target, &cmd );
1112     if (!cmd) return ERROR_FUNCTION_FAILED;
1113 
1114     TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
1115 
1116     handle = execute_command( NULL, cmd, workingdir );
1117     msi_free( cmd );
1118     if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
1119     return wait_process_handle( package, type, handle, action );
1120 }
1121 
1122 static DWORD ACTION_CallScript( const GUID *guid )
1123 {
1124     msi_custom_action_info *info;
1125     MSIHANDLE hPackage;
1126     UINT r = ERROR_FUNCTION_FAILED;
1127 
1128     info = find_action_by_guid( guid );
1129     if (!info)
1130     {
1131         ERR("failed to find action %s\n", debugstr_guid( guid) );
1132         return ERROR_FUNCTION_FAILED;
1133     }
1134 
1135     TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
1136 
1137     hPackage = alloc_msihandle( &info->package->hdr );
1138     if (hPackage)
1139     {
1140         r = call_script( hPackage, info->type, info->source, info->target, info->action );
1141         TRACE("script returned %u\n", r);
1142         MsiCloseHandle( hPackage );
1143     }
1144     else
1145         ERR("failed to create handle for %p\n", info->package );
1146 
1147     return r;
1148 }
1149 
1150 static DWORD WINAPI ScriptThread( LPVOID arg )
1151 {
1152     LPGUID guid = arg;
1153     DWORD rc;
1154 
1155     TRACE("custom action (%x) started\n", GetCurrentThreadId() );
1156 
1157     rc = ACTION_CallScript( guid );
1158 
1159     TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
1160 
1161     MsiCloseAllHandles();
1162     return rc;
1163 }
1164 
1165 static msi_custom_action_info *do_msidbCustomActionTypeScript(
1166     MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
1167 {
1168     msi_custom_action_info *info;
1169 
1170     info = msi_alloc( sizeof *info );
1171     if (!info)
1172         return NULL;
1173 
1174     msiobj_addref( &package->hdr );
1175     info->package = package;
1176     info->type = type;
1177     info->target = strdupW( function );
1178     info->source = strdupW( script );
1179     info->action = strdupW( action );
1180     CoCreateGuid( &info->guid );
1181 
1182     EnterCriticalSection( &msi_custom_action_cs );
1183     list_add_tail( &msi_pending_custom_actions, &info->entry );
1184     LeaveCriticalSection( &msi_custom_action_cs );
1185 
1186     info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1187     if (!info->handle)
1188     {
1189         free_custom_action_data( info );
1190         return NULL;
1191     }
1192 
1193     return info;
1194 }
1195 
1196 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1197                                     INT type, const WCHAR *action )
1198 {
1199     msi_custom_action_info *info;
1200 
1201     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1202 
1203     info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1204     return wait_thread_handle( info );
1205 }
1206 
1207 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1208                                   INT type, const WCHAR *action )
1209 {
1210     MSIRECORD *row = NULL;
1211     msi_custom_action_info *info;
1212     CHAR *buffer = NULL;
1213     WCHAR *bufferw = NULL;
1214     DWORD sz = 0;
1215     UINT r;
1216 
1217     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1218 
1219     row = MSI_QueryGetRecord(package->db, L"SELECT * FROM `Binary` WHERE `Name` = '%s'", source);
1220     if (!row)
1221         return ERROR_FUNCTION_FAILED;
1222 
1223     r = MSI_RecordReadStream(row, 2, NULL, &sz);
1224     if (r != ERROR_SUCCESS) goto done;
1225 
1226     buffer = msi_alloc( sz + 1 );
1227     if (!buffer)
1228     {
1229        r = ERROR_FUNCTION_FAILED;
1230        goto done;
1231     }
1232 
1233     r = MSI_RecordReadStream(row, 2, buffer, &sz);
1234     if (r != ERROR_SUCCESS)
1235         goto done;
1236 
1237     buffer[sz] = 0;
1238     bufferw = strdupAtoW(buffer);
1239     if (!bufferw)
1240     {
1241         r = ERROR_FUNCTION_FAILED;
1242         goto done;
1243     }
1244 
1245     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1246     r = wait_thread_handle( info );
1247 
1248 done:
1249     msi_free(bufferw);
1250     msi_free(buffer);
1251     msiobj_release(&row->hdr);
1252     return r;
1253 }
1254 
1255 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1256                                     INT type, const WCHAR *action )
1257 {
1258     msi_custom_action_info *info;
1259     MSIFILE *file;
1260     HANDLE hFile;
1261     DWORD sz, szHighWord = 0, read;
1262     CHAR *buffer=NULL;
1263     WCHAR *bufferw=NULL;
1264     BOOL bRet;
1265     UINT r;
1266 
1267     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1268 
1269     file = msi_get_loaded_file(package, source);
1270     if (!file)
1271     {
1272         ERR("invalid file key %s\n", debugstr_w(source));
1273         return ERROR_FUNCTION_FAILED;
1274     }
1275 
1276     hFile = msi_create_file( package, file->TargetPath, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, 0 );
1277     if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1278 
1279     sz = GetFileSize(hFile, &szHighWord);
1280     if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1281     {
1282         CloseHandle(hFile);
1283         return ERROR_FUNCTION_FAILED;
1284     }
1285     buffer = msi_alloc( sz + 1 );
1286     if (!buffer)
1287     {
1288         CloseHandle(hFile);
1289         return ERROR_FUNCTION_FAILED;
1290     }
1291     bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1292     CloseHandle(hFile);
1293     if (!bRet)
1294     {
1295         r = ERROR_FUNCTION_FAILED;
1296         goto done;
1297     }
1298     buffer[read] = 0;
1299     bufferw = strdupAtoW(buffer);
1300     if (!bufferw)
1301     {
1302         r = ERROR_FUNCTION_FAILED;
1303         goto done;
1304     }
1305     info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1306     r = wait_thread_handle( info );
1307 
1308 done:
1309     msi_free(bufferw);
1310     msi_free(buffer);
1311     return r;
1312 }
1313 
1314 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1315                                     INT type, const WCHAR *action )
1316 {
1317     msi_custom_action_info *info;
1318     WCHAR *prop;
1319 
1320     TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1321 
1322     prop = msi_dup_property( package->db, source );
1323     if (!prop) return ERROR_SUCCESS;
1324 
1325     info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1326     msi_free(prop);
1327     return wait_thread_handle( info );
1328 }
1329 
1330 static BOOL action_type_matches_script( UINT type, UINT script )
1331 {
1332     switch (script)
1333     {
1334     case SCRIPT_NONE:
1335         return FALSE;
1336     case SCRIPT_INSTALL:
1337         return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1338     case SCRIPT_COMMIT:
1339         return (type & msidbCustomActionTypeCommit);
1340     case SCRIPT_ROLLBACK:
1341         return (type & msidbCustomActionTypeRollback);
1342     default:
1343         ERR("unhandled script %u\n", script);
1344     }
1345     return FALSE;
1346 }
1347 
1348 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1349 {
1350     WCHAR *actiondata = msi_dup_property( package->db, action );
1351     WCHAR *usersid = msi_dup_property( package->db, L"UserSID" );
1352     WCHAR *prodcode = msi_dup_property( package->db, L"ProductCode" );
1353     WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1354 
1355     if (!deferred)
1356     {
1357         msi_free( actiondata );
1358         msi_free( usersid );
1359         msi_free( prodcode );
1360         return ERROR_OUTOFMEMORY;
1361     }
1362     if (type & msidbCustomActionTypeCommit)
1363     {
1364         TRACE("deferring commit action\n");
1365         msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1366     }
1367     else if (type & msidbCustomActionTypeRollback)
1368     {
1369         TRACE("deferring rollback action\n");
1370         msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1371     }
1372     else
1373     {
1374         TRACE("deferring install action\n");
1375         msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1376     }
1377 
1378     msi_free( actiondata );
1379     msi_free( usersid );
1380     msi_free( prodcode );
1381     msi_free( deferred );
1382     return ERROR_SUCCESS;
1383 }
1384 
1385 UINT ACTION_CustomAction(MSIPACKAGE *package, const WCHAR *action)
1386 {
1387     UINT rc = ERROR_SUCCESS;
1388     MSIRECORD *row;
1389     UINT type;
1390     const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1391     WCHAR *deformated = NULL;
1392     int len;
1393 
1394     /* deferred action: [properties]Action */
1395     if ((ptr = wcsrchr(action, ']')))
1396     {
1397         deferred_data = action;
1398         action = ptr + 1;
1399     }
1400 
1401     row = MSI_QueryGetRecord( package->db, L"SELECT * FROM `CustomAction` WHERE `Action` = '%s'", action );
1402     if (!row)
1403         return ERROR_FUNCTION_NOT_CALLED;
1404 
1405     type = MSI_RecordGetInteger(row,2);
1406     source = MSI_RecordGetString(row,3);
1407     target = MSI_RecordGetString(row,4);
1408 
1409     TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1410           debugstr_w(source), debugstr_w(target));
1411 
1412     /* handle some of the deferred actions */
1413     if (type & msidbCustomActionTypeTSAware)
1414         FIXME("msidbCustomActionTypeTSAware not handled\n");
1415 
1416     if (type & msidbCustomActionTypeInScript)
1417     {
1418         if (type & msidbCustomActionTypeNoImpersonate)
1419             WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1420 
1421         if (!action_type_matches_script(type, package->script))
1422         {
1423             rc = defer_custom_action( package, action, type );
1424             goto end;
1425         }
1426         else
1427         {
1428             LPWSTR actiondata = msi_dup_property( package->db, action );
1429 
1430             if (type & msidbCustomActionTypeInScript)
1431                 package->scheduled_action_running = TRUE;
1432 
1433             if (type & msidbCustomActionTypeCommit)
1434                 package->commit_action_running = TRUE;
1435 
1436             if (type & msidbCustomActionTypeRollback)
1437                 package->rollback_action_running = TRUE;
1438 
1439             if (deferred_data)
1440                 set_deferred_action_props(package, deferred_data);
1441             else if (actiondata)
1442                 msi_set_property( package->db, L"CustomActionData", actiondata, -1 );
1443             else
1444                 msi_set_property( package->db, L"CustomActionData", L"", -1 );
1445 
1446             msi_free(actiondata);
1447         }
1448     }
1449     else if (!check_execution_scheduling_options(package,action,type))
1450     {
1451         rc = ERROR_SUCCESS;
1452         goto end;
1453     }
1454 
1455     switch (type & CUSTOM_ACTION_TYPE_MASK)
1456     {
1457     case 1: /* DLL file stored in a Binary table stream */
1458         rc = HANDLE_CustomType1( package, source, target, type, action );
1459         break;
1460     case 2: /* EXE file stored in a Binary table stream */
1461         rc = HANDLE_CustomType2( package, source, target, type, action );
1462         break;
1463     case 5:
1464     case 6: /* JScript/VBScript file stored in a Binary table stream */
1465         rc = HANDLE_CustomType5_6( package, source, target, type, action );
1466         break;
1467     case 7: /* Concurrent install from substorage */
1468         deformat_string( package, target, &deformated );
1469         rc = HANDLE_CustomType7( package, source, target, type, action );
1470         msi_free( deformated );
1471         break;
1472     case 17:
1473         rc = HANDLE_CustomType17( package, source, target, type, action );
1474         break;
1475     case 18: /* EXE file installed with package */
1476         rc = HANDLE_CustomType18( package, source, target, type, action );
1477         break;
1478     case 19: /* Error that halts install */
1479         rc = HANDLE_CustomType19( package, source, target, type, action );
1480         break;
1481     case 21: /* JScript/VBScript file installed with the product */
1482     case 22:
1483         rc = HANDLE_CustomType21_22( package, source, target, type, action );
1484         break;
1485     case 23: /* Installs another package in the source tree */
1486         deformat_string( package, target, &deformated );
1487         rc = HANDLE_CustomType23( package, source, deformated, type, action );
1488         msi_free( deformated );
1489         break;
1490     case 34: /* EXE to be run in specified directory */
1491         rc = HANDLE_CustomType34( package, source, target, type, action );
1492         break;
1493     case 35: /* Directory set with formatted text */
1494         deformat_string( package, target, &deformated );
1495         MSI_SetTargetPathW( package, source, deformated );
1496         msi_free( deformated );
1497         break;
1498     case 37: /* JScript/VBScript text stored in target column */
1499     case 38:
1500         rc = HANDLE_CustomType37_38( package, source, target, type, action );
1501         break;
1502     case 50: /* EXE file specified by a property value */
1503         rc = HANDLE_CustomType50( package, source, target, type, action );
1504         break;
1505     case 51: /* Property set with formatted text */
1506         if (!source) break;
1507         len = deformat_string( package, target, &deformated );
1508         rc = msi_set_property( package->db, source, deformated, len );
1509         if (rc == ERROR_SUCCESS && !wcscmp( source, L"SourceDir" )) msi_reset_source_folders( package );
1510         msi_free( deformated );
1511         break;
1512     case 53: /* JScript/VBScript text specified by a property value */
1513     case 54:
1514         rc = HANDLE_CustomType53_54( package, source, target, type, action );
1515         break;
1516     default:
1517         FIXME( "unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK, debugstr_w(source),
1518                debugstr_w(target) );
1519     }
1520 
1521 end:
1522     package->scheduled_action_running = FALSE;
1523     package->commit_action_running = FALSE;
1524     package->rollback_action_running = FALSE;
1525     msiobj_release(&row->hdr);
1526     return rc;
1527 }
1528 
1529 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1530 {
1531     struct list *item;
1532     HANDLE *wait_handles;
1533     unsigned int handle_count, i;
1534     msi_custom_action_info *info, *cursor;
1535 
1536     while ((item = list_head( &package->RunningActions )))
1537     {
1538         MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1539 
1540         list_remove( &action->entry );
1541 
1542         TRACE("waiting for %s\n", debugstr_w( action->name ) );
1543         msi_dialog_check_messages( action->handle );
1544 
1545         CloseHandle( action->handle );
1546         msi_free( action->name );
1547         msi_free( action );
1548     }
1549 
1550     EnterCriticalSection( &msi_custom_action_cs );
1551 
1552     handle_count = list_count( &msi_pending_custom_actions );
1553     wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1554 
1555     handle_count = 0;
1556     LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1557     {
1558         if (info->package == package )
1559         {
1560             if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1561                 handle_count++;
1562         }
1563     }
1564 
1565     LeaveCriticalSection( &msi_custom_action_cs );
1566 
1567     for (i = 0; i < handle_count; i++)
1568     {
1569         msi_dialog_check_messages( wait_handles[i] );
1570         CloseHandle( wait_handles[i] );
1571     }
1572     msi_free( wait_handles );
1573 
1574     EnterCriticalSection( &msi_custom_action_cs );
1575     LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1576     {
1577         if (info->package == package)
1578             free_custom_action_data( info );
1579     }
1580     LeaveCriticalSection( &msi_custom_action_cs );
1581 }
1582 
1583 UINT __cdecl s_remote_GetActionInfo(const GUID *guid, int *type, LPWSTR *dll, LPSTR *func, MSIHANDLE *hinst)
1584 {
1585     msi_custom_action_info *info;
1586 
1587     info = find_action_by_guid(guid);
1588     if (!info)
1589         return ERROR_INVALID_DATA;
1590 
1591     *type = info->type;
1592     *hinst = alloc_msihandle(&info->package->hdr);
1593     *dll = strdupW(info->source);
1594     *func = strdupWtoA(info->target);
1595 
1596     return ERROR_SUCCESS;
1597 }
1598