msi: Avoid leaking custom action data.
[wine.git] / dlls / msi / custom.c
blob07916ac0714632d639b7bb09d02a1f723392d97f
1 /*
2 * Custom Action processing for the Microsoft Installer (msi.dll)
4 * Copyright 2005 Aric Stewart for CodeWeavers
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #define COBJMACROS
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "msidefs.h"
32 #include "winuser.h"
33 #include "objbase.h"
34 #include "oleauto.h"
36 #include "msipriv.h"
37 #include "winemsi.h"
38 #include "wine/heap.h"
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41 #include "wine/exception.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msi);
45 #define CUSTOM_ACTION_TYPE_MASK 0x3F
47 typedef struct tagMSIRUNNINGACTION
49 struct list entry;
50 HANDLE handle;
51 BOOL process;
52 LPWSTR name;
53 } MSIRUNNINGACTION;
55 typedef UINT (WINAPI *MsiCustomActionEntryPoint)( MSIHANDLE );
57 static CRITICAL_SECTION msi_custom_action_cs;
58 static CRITICAL_SECTION_DEBUG msi_custom_action_cs_debug =
60 0, 0, &msi_custom_action_cs,
61 { &msi_custom_action_cs_debug.ProcessLocksList,
62 &msi_custom_action_cs_debug.ProcessLocksList },
63 0, 0, { (DWORD_PTR)(__FILE__ ": msi_custom_action_cs") }
65 static CRITICAL_SECTION msi_custom_action_cs = { &msi_custom_action_cs_debug, -1, 0, 0, 0, 0 };
67 static struct list msi_pending_custom_actions = LIST_INIT( msi_pending_custom_actions );
69 void __RPC_FAR * __RPC_USER MIDL_user_allocate(SIZE_T len)
71 return heap_alloc(len);
74 void __RPC_USER MIDL_user_free(void __RPC_FAR * ptr)
76 heap_free(ptr);
79 UINT msi_schedule_action( MSIPACKAGE *package, UINT script, const WCHAR *action )
81 UINT count;
82 WCHAR **newbuf = NULL;
84 if (script >= SCRIPT_MAX)
86 FIXME("Unknown script requested %u\n", script);
87 return ERROR_FUNCTION_FAILED;
89 TRACE("Scheduling action %s in script %u\n", debugstr_w(action), script);
91 count = package->script_actions_count[script];
92 package->script_actions_count[script]++;
93 if (count != 0) newbuf = msi_realloc( package->script_actions[script],
94 package->script_actions_count[script] * sizeof(WCHAR *) );
95 else newbuf = msi_alloc( sizeof(WCHAR *) );
97 newbuf[count] = strdupW( action );
98 package->script_actions[script] = newbuf;
99 return ERROR_SUCCESS;
102 UINT msi_register_unique_action( MSIPACKAGE *package, const WCHAR *action )
104 UINT count;
105 WCHAR **newbuf = NULL;
107 TRACE("Registering %s as unique action\n", debugstr_w(action));
109 count = package->unique_actions_count;
110 package->unique_actions_count++;
111 if (count != 0) newbuf = msi_realloc( package->unique_actions,
112 package->unique_actions_count * sizeof(WCHAR *) );
113 else newbuf = msi_alloc( sizeof(WCHAR *) );
115 newbuf[count] = strdupW( action );
116 package->unique_actions = newbuf;
117 return ERROR_SUCCESS;
120 BOOL msi_action_is_unique( const MSIPACKAGE *package, const WCHAR *action )
122 UINT i;
124 for (i = 0; i < package->unique_actions_count; i++)
126 if (!strcmpW( package->unique_actions[i], action )) return TRUE;
128 return FALSE;
131 static BOOL check_execution_scheduling_options(MSIPACKAGE *package, LPCWSTR action, UINT options)
133 if ((options & msidbCustomActionTypeClientRepeat) ==
134 msidbCustomActionTypeClientRepeat)
136 if (!(package->InWhatSequence & SEQUENCE_UI &&
137 package->InWhatSequence & SEQUENCE_EXEC))
139 TRACE("Skipping action due to dbCustomActionTypeClientRepeat option.\n");
140 return FALSE;
143 else if (options & msidbCustomActionTypeFirstSequence)
145 if (package->InWhatSequence & SEQUENCE_UI &&
146 package->InWhatSequence & SEQUENCE_EXEC )
148 TRACE("Skipping action due to msidbCustomActionTypeFirstSequence option.\n");
149 return FALSE;
152 else if (options & msidbCustomActionTypeOncePerProcess)
154 if (msi_action_is_unique(package, action))
156 TRACE("Skipping action due to msidbCustomActionTypeOncePerProcess option.\n");
157 return FALSE;
159 else
160 msi_register_unique_action(package, action);
163 return TRUE;
166 /* stores the following properties before the action:
168 * [CustomActionData<=>UserSID<=>ProductCode]Action
170 static LPWSTR msi_get_deferred_action(LPCWSTR action, LPCWSTR actiondata,
171 LPCWSTR usersid, LPCWSTR prodcode)
173 LPWSTR deferred;
174 DWORD len;
176 static const WCHAR format[] = {
177 '[','%','s','<','=','>','%','s','<','=','>','%','s',']','%','s',0
180 if (!actiondata)
181 return strdupW(action);
183 len = lstrlenW(action) + lstrlenW(actiondata) +
184 lstrlenW(usersid) + lstrlenW(prodcode) +
185 lstrlenW(format) - 7;
186 deferred = msi_alloc(len * sizeof(WCHAR));
188 sprintfW(deferred, format, actiondata, usersid, prodcode, action);
189 return deferred;
192 static void set_deferred_action_props( MSIPACKAGE *package, const WCHAR *deferred_data )
194 static const WCHAR sep[] = {'<','=','>',0};
195 const WCHAR *end, *beg = deferred_data + 1;
197 end = strstrW(beg, sep);
198 msi_set_property( package->db, szCustomActionData, beg, end - beg );
199 beg = end + 3;
201 end = strstrW(beg, sep);
202 msi_set_property( package->db, szUserSID, beg, end - beg );
203 beg = end + 3;
205 end = strchrW(beg, ']');
206 msi_set_property( package->db, szProductCode, beg, end - beg );
209 WCHAR *msi_create_temp_file( MSIDATABASE *db )
211 WCHAR *ret;
213 if (!db->tempfolder)
215 WCHAR tmp[MAX_PATH];
216 UINT len = sizeof(tmp)/sizeof(tmp[0]);
218 if (msi_get_property( db, szTempFolder, tmp, &len ) ||
219 GetFileAttributesW( tmp ) != FILE_ATTRIBUTE_DIRECTORY)
221 GetTempPathW( MAX_PATH, tmp );
223 if (!(db->tempfolder = strdupW( tmp ))) return NULL;
226 if ((ret = msi_alloc( (strlenW( db->tempfolder ) + 20) * sizeof(WCHAR) )))
228 if (!GetTempFileNameW( db->tempfolder, szMsi, 0, ret ))
230 msi_free( ret );
231 return NULL;
235 return ret;
238 static MSIBINARY *create_temp_binary(MSIPACKAGE *package, LPCWSTR source)
240 static const WCHAR query[] = {
241 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
242 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
243 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
244 MSIRECORD *row;
245 MSIBINARY *binary = NULL;
246 HANDLE file;
247 CHAR buffer[1024];
248 WCHAR *tmpfile;
249 DWORD sz, write;
250 UINT r;
252 if (!(tmpfile = msi_create_temp_file( package->db ))) return NULL;
254 if (!(row = MSI_QueryGetRecord( package->db, query, source ))) goto error;
255 if (!(binary = msi_alloc_zero( sizeof(MSIBINARY) ))) goto error;
257 file = CreateFileW( tmpfile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
258 if (file == INVALID_HANDLE_VALUE) goto error;
262 sz = sizeof(buffer);
263 r = MSI_RecordReadStream( row, 2, buffer, &sz );
264 if (r != ERROR_SUCCESS)
266 ERR("Failed to get stream\n");
267 break;
269 WriteFile( file, buffer, sz, &write, NULL );
270 } while (sz == sizeof buffer);
272 CloseHandle( file );
273 if (r != ERROR_SUCCESS) goto error;
275 binary->source = strdupW( source );
276 binary->tmpfile = tmpfile;
277 list_add_tail( &package->binaries, &binary->entry );
279 msiobj_release( &row->hdr );
280 return binary;
282 error:
283 if (row) msiobj_release( &row->hdr );
284 DeleteFileW( tmpfile );
285 msi_free( tmpfile );
286 msi_free( binary );
287 return NULL;
290 static MSIBINARY *get_temp_binary(MSIPACKAGE *package, LPCWSTR source)
292 MSIBINARY *binary;
294 LIST_FOR_EACH_ENTRY( binary, &package->binaries, MSIBINARY, entry )
296 if (!strcmpW( binary->source, source ))
297 return binary;
300 return create_temp_binary(package, source);
303 static void file_running_action(MSIPACKAGE* package, HANDLE Handle,
304 BOOL process, LPCWSTR name)
306 MSIRUNNINGACTION *action;
308 action = msi_alloc( sizeof(MSIRUNNINGACTION) );
310 action->handle = Handle;
311 action->process = process;
312 action->name = strdupW(name);
314 list_add_tail( &package->RunningActions, &action->entry );
317 static UINT custom_get_process_return( HANDLE process )
319 DWORD rc = 0;
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;
328 static UINT custom_get_thread_return( MSIPACKAGE *package, HANDLE thread )
330 DWORD rc = 0;
332 GetExitCodeThread( thread, &rc );
334 switch (rc)
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;
352 static UINT wait_process_handle(MSIPACKAGE* package, UINT type,
353 HANDLE ProcessHandle, LPCWSTR name)
355 UINT rc = ERROR_SUCCESS;
357 if (!(type & msidbCustomActionTypeAsync))
359 TRACE("waiting for %s\n", debugstr_w(name));
361 msi_dialog_check_messages(ProcessHandle);
363 if (!(type & msidbCustomActionTypeContinue))
364 rc = custom_get_process_return(ProcessHandle);
366 CloseHandle(ProcessHandle);
368 else
370 TRACE("%s running in background\n", debugstr_w(name));
372 if (!(type & msidbCustomActionTypeContinue))
373 file_running_action(package, ProcessHandle, TRUE, name);
374 else
375 CloseHandle(ProcessHandle);
378 return rc;
381 typedef struct _msi_custom_action_info {
382 struct list entry;
383 LONG refs;
384 MSIPACKAGE *package;
385 LPWSTR source;
386 LPWSTR target;
387 HANDLE handle;
388 LPWSTR action;
389 INT type;
390 GUID guid;
391 } msi_custom_action_info;
393 static void release_custom_action_data( msi_custom_action_info *info )
395 EnterCriticalSection( &msi_custom_action_cs );
397 if (!--info->refs)
399 list_remove( &info->entry );
400 if (info->handle)
401 CloseHandle( info->handle );
402 msi_free( info->action );
403 msi_free( info->source );
404 msi_free( info->target );
405 msiobj_release( &info->package->hdr );
406 msi_free( info );
409 LeaveCriticalSection( &msi_custom_action_cs );
412 /* must be called inside msi_custom_action_cs if info is in the pending custom actions list */
413 static void addref_custom_action_data( msi_custom_action_info *info )
415 info->refs++;
418 static UINT wait_thread_handle( msi_custom_action_info *info )
420 UINT rc = ERROR_SUCCESS;
422 if (!(info->type & msidbCustomActionTypeAsync))
424 TRACE("waiting for %s\n", debugstr_w( info->action ));
426 msi_dialog_check_messages( info->handle );
428 if (!(info->type & msidbCustomActionTypeContinue))
429 rc = custom_get_thread_return( info->package, info->handle );
431 release_custom_action_data( info );
433 else
435 TRACE("%s running in background\n", debugstr_w( info->action ));
438 return rc;
441 static msi_custom_action_info *find_action_by_guid( const GUID *guid )
443 msi_custom_action_info *info;
444 BOOL found = FALSE;
446 EnterCriticalSection( &msi_custom_action_cs );
448 LIST_FOR_EACH_ENTRY( info, &msi_pending_custom_actions, msi_custom_action_info, entry )
450 if (IsEqualGUID( &info->guid, guid ))
452 addref_custom_action_data( info );
453 found = TRUE;
454 break;
458 LeaveCriticalSection( &msi_custom_action_cs );
460 if (!found)
461 return NULL;
463 return info;
466 static void handle_msi_break(LPCSTR target)
468 char format[] = "To debug your custom action, attach your debugger to "
469 "process %i (0x%X) and press OK";
470 char val[MAX_PATH];
471 char msg[100];
473 if (!GetEnvironmentVariableA("MsiBreak", val, MAX_PATH))
474 return;
476 if (strcasecmp(val, target))
477 return;
479 sprintf(msg, format, GetCurrentProcessId(), GetCurrentProcessId());
480 MessageBoxA(NULL, msg, "Windows Installer", MB_OK);
481 DebugBreak();
484 static WCHAR ncalrpcW[] = {'n','c','a','l','r','p','c',0};
485 static WCHAR endpoint_lrpcW[] = {'m','s','i',0};
487 UINT __wine_msi_call_dll_function(const GUID *guid)
489 MsiCustomActionEntryPoint fn;
490 MSIHANDLE remote_package = 0;
491 RPC_WSTR binding_str;
492 MSIHANDLE hPackage;
493 RPC_STATUS status;
494 LPWSTR dll = NULL;
495 LPSTR proc = NULL;
496 HANDLE hModule;
497 HANDLE thread;
498 INT type;
499 UINT r;
501 TRACE("%s\n", debugstr_guid( guid ));
503 status = RpcStringBindingComposeW(NULL, ncalrpcW, NULL, endpoint_lrpcW, NULL, &binding_str);
504 if (status != RPC_S_OK)
506 ERR("RpcStringBindingCompose failed: %#x\n", status);
507 return status;
509 status = RpcBindingFromStringBindingW(binding_str, &rpc_handle);
510 if (status != RPC_S_OK)
512 ERR("RpcBindingFromStringBinding failed: %#x\n", status);
513 return status;
515 RpcStringFreeW(&binding_str);
517 /* We need this to unmarshal streams, and some apps expect it to be present. */
518 CoInitializeEx(NULL, COINIT_MULTITHREADED);
520 r = remote_GetActionInfo(guid, &type, &dll, &proc, &remote_package);
521 if (r != ERROR_SUCCESS)
522 return r;
524 hModule = LoadLibraryW( dll );
525 if (!hModule)
527 ERR( "failed to load dll %s (%u)\n", debugstr_w( dll ), GetLastError() );
528 return ERROR_SUCCESS;
531 fn = (MsiCustomActionEntryPoint) GetProcAddress( hModule, proc );
532 if (fn)
534 hPackage = alloc_msi_remote_handle( remote_package );
535 if (hPackage)
537 TRACE("calling %s\n", debugstr_a(proc));
538 handle_msi_break(proc);
540 __TRY
542 thread = CreateThread(NULL, 0, (void *)fn, (void *)(ULONG_PTR) hPackage, 0, NULL);
543 WaitForSingleObject(thread, INFINITE);
544 GetExitCodeThread(thread, &r);
546 __EXCEPT_PAGE_FAULT
548 ERR("Custom action (%s:%s) caused a page fault: %08x\n",
549 debugstr_w(dll), debugstr_a(proc), GetExceptionCode());
550 r = ERROR_SUCCESS;
552 __ENDTRY;
554 MsiCloseHandle( hPackage );
556 else
557 ERR("failed to create handle for %x\n", remote_package );
559 else
560 ERR("GetProcAddress(%s) failed\n", debugstr_a(proc));
562 FreeLibrary(hModule);
564 MsiCloseHandle(hPackage);
565 midl_user_free(dll);
566 midl_user_free(proc);
568 CoUninitialize();
570 RpcBindingFree(&rpc_handle);
572 return r;
575 static DWORD WINAPI DllThread( LPVOID arg )
577 static const WCHAR msiexecW[] = {'\\','m','s','i','e','x','e','c','.','e','x','e',0};
578 static const WCHAR argsW[] = {' ','-','E','m','b','e','d','d','i','n','g',' ',0};
579 msi_custom_action_info *info;
580 PROCESS_INFORMATION pi = {0};
581 STARTUPINFOW si = {0};
582 WCHAR buffer[MAX_PATH], cmdline[MAX_PATH + 60];
583 RPC_STATUS status;
584 GUID *guid = arg;
585 void *cookie;
586 BOOL wow64;
587 DWORD arch;
588 BOOL ret;
589 DWORD rc;
591 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
593 CoInitializeEx(NULL, COINIT_MULTITHREADED); /* needed to marshal streams */
595 status = RpcServerUseProtseqEpW(ncalrpcW, RPC_C_PROTSEQ_MAX_REQS_DEFAULT, endpoint_lrpcW, NULL);
596 if (status != RPC_S_OK)
598 ERR("RpcServerUseProtseqEp failed: %#x\n", status);
599 return status;
602 status = RpcServerRegisterIfEx(s_IWineMsiRemote_v0_0_s_ifspec, NULL, NULL,
603 RPC_IF_AUTOLISTEN, RPC_C_LISTEN_MAX_CALLS_DEFAULT, NULL);
604 if (status != RPC_S_OK)
606 ERR("RpcServerRegisterIfEx failed: %#x\n", status);
607 return status;
610 info = find_action_by_guid(guid);
611 ret = GetBinaryTypeW(info->source, &arch);
612 release_custom_action_data(info);
614 if (sizeof(void *) == 8 && ret && arch == SCS_32BIT_BINARY)
615 GetSystemWow64DirectoryW(buffer, MAX_PATH - sizeof(msiexecW)/sizeof(WCHAR));
616 else
617 GetSystemDirectoryW(buffer, MAX_PATH - sizeof(msiexecW)/sizeof(WCHAR));
618 strcatW(buffer, msiexecW);
619 strcpyW(cmdline, buffer);
620 strcatW(cmdline, argsW);
621 StringFromGUID2(guid, cmdline + strlenW(cmdline), 39);
623 if (IsWow64Process(GetCurrentProcess(), &wow64) && wow64 && arch == SCS_64BIT_BINARY)
625 Wow64DisableWow64FsRedirection(&cookie);
626 CreateProcessW(buffer, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
627 Wow64RevertWow64FsRedirection(cookie);
629 else
630 CreateProcessW(buffer, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
632 WaitForSingleObject(pi.hProcess, INFINITE);
633 GetExitCodeProcess(pi.hProcess, &rc);
634 CloseHandle(pi.hProcess);
635 CloseHandle(pi.hThread);
637 status = RpcServerUnregisterIf(s_IWineMsiRemote_v0_0_s_ifspec, NULL, FALSE);
638 if (status != RPC_S_OK)
640 ERR("RpcServerUnregisterIf failed: %#x\n", status);
641 return status;
644 CoUninitialize();
646 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
648 MsiCloseAllHandles();
649 return rc;
652 static msi_custom_action_info *do_msidbCustomActionTypeDll(
653 MSIPACKAGE *package, INT type, LPCWSTR source, LPCWSTR target, LPCWSTR action )
655 msi_custom_action_info *info;
657 info = msi_alloc( sizeof *info );
658 if (!info)
659 return NULL;
661 msiobj_addref( &package->hdr );
662 info->refs = 2; /* 1 for our caller and 1 for thread we created */
663 info->package = package;
664 info->type = type;
665 info->target = strdupW( target );
666 info->source = strdupW( source );
667 info->action = strdupW( action );
668 CoCreateGuid( &info->guid );
670 EnterCriticalSection( &msi_custom_action_cs );
671 list_add_tail( &msi_pending_custom_actions, &info->entry );
672 LeaveCriticalSection( &msi_custom_action_cs );
674 info->handle = CreateThread( NULL, 0, DllThread, &info->guid, 0, NULL );
675 if (!info->handle)
677 /* release both references */
678 release_custom_action_data( info );
679 release_custom_action_data( info );
680 return NULL;
683 return info;
686 static UINT HANDLE_CustomType1( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
687 INT type, const WCHAR *action )
689 msi_custom_action_info *info;
690 MSIBINARY *binary;
692 if (!(binary = get_temp_binary(package, source)))
693 return ERROR_FUNCTION_FAILED;
695 TRACE("Calling function %s from %s\n", debugstr_w(target), debugstr_w(binary->tmpfile));
697 info = do_msidbCustomActionTypeDll( package, type, binary->tmpfile, target, action );
698 return wait_thread_handle( info );
701 static HANDLE execute_command( const WCHAR *app, WCHAR *arg, const WCHAR *dir )
703 static const WCHAR dotexeW[] = {'.','e','x','e',0};
704 STARTUPINFOW si;
705 PROCESS_INFORMATION info;
706 WCHAR *exe = NULL, *cmd = NULL, *p;
707 BOOL ret;
709 if (app)
711 int len_arg = 0;
712 DWORD len_exe;
714 if (!(exe = msi_alloc( MAX_PATH * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
715 len_exe = SearchPathW( NULL, app, dotexeW, MAX_PATH, exe, NULL );
716 if (len_exe >= MAX_PATH)
718 msi_free( exe );
719 if (!(exe = msi_alloc( len_exe * sizeof(WCHAR) ))) return INVALID_HANDLE_VALUE;
720 len_exe = SearchPathW( NULL, app, dotexeW, len_exe, exe, NULL );
722 if (!len_exe)
724 ERR("can't find executable %u\n", GetLastError());
725 msi_free( exe );
726 return INVALID_HANDLE_VALUE;
729 if (arg) len_arg = strlenW( arg );
730 if (!(cmd = msi_alloc( (len_exe + len_arg + 4) * sizeof(WCHAR) )))
732 msi_free( exe );
733 return INVALID_HANDLE_VALUE;
735 p = cmd;
736 if (strchrW( exe, ' ' ))
738 *p++ = '\"';
739 memcpy( p, exe, len_exe * sizeof(WCHAR) );
740 p += len_exe;
741 *p++ = '\"';
742 *p = 0;
744 else
746 strcpyW( p, exe );
747 p += len_exe;
749 if (arg)
751 *p++ = ' ';
752 memcpy( p, arg, len_arg * sizeof(WCHAR) );
753 p[len_arg] = 0;
756 memset( &si, 0, sizeof(STARTUPINFOW) );
757 ret = CreateProcessW( exe, exe ? cmd : arg, NULL, NULL, FALSE, 0, NULL, dir, &si, &info );
758 msi_free( cmd );
759 msi_free( exe );
760 if (!ret)
762 ERR("unable to execute command %u\n", GetLastError());
763 return INVALID_HANDLE_VALUE;
765 CloseHandle( info.hThread );
766 return info.hProcess;
769 static UINT HANDLE_CustomType2( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
770 INT type, const WCHAR *action )
772 MSIBINARY *binary;
773 HANDLE handle;
774 WCHAR *arg;
776 if (!(binary = get_temp_binary(package, source)))
777 return ERROR_FUNCTION_FAILED;
779 deformat_string( package, target, &arg );
780 TRACE("exe %s arg %s\n", debugstr_w(binary->tmpfile), debugstr_w(arg));
782 handle = execute_command( binary->tmpfile, arg, szCRoot );
783 msi_free( arg );
784 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
785 return wait_process_handle( package, type, handle, action );
788 static UINT HANDLE_CustomType17( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
789 INT type, const WCHAR *action )
791 msi_custom_action_info *info;
792 MSIFILE *file;
794 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
796 file = msi_get_loaded_file( package, source );
797 if (!file)
799 ERR("invalid file key %s\n", debugstr_w( source ));
800 return ERROR_FUNCTION_FAILED;
803 info = do_msidbCustomActionTypeDll( package, type, file->TargetPath, target, action );
804 return wait_thread_handle( info );
807 static UINT HANDLE_CustomType18( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
808 INT type, const WCHAR *action )
810 MSIFILE *file;
811 HANDLE handle;
812 WCHAR *arg;
814 if (!(file = msi_get_loaded_file( package, source ))) return ERROR_FUNCTION_FAILED;
816 deformat_string( package, target, &arg );
817 TRACE("exe %s arg %s\n", debugstr_w(file->TargetPath), debugstr_w(arg));
819 handle = execute_command( file->TargetPath, arg, szCRoot );
820 msi_free( arg );
821 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
822 return wait_process_handle( package, type, handle, action );
825 static UINT HANDLE_CustomType19( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
826 INT type, const WCHAR *action )
828 static const WCHAR query[] = {
829 'S','E','L','E','C','T',' ','`','M','e','s','s','a','g','e','`',' ',
830 'F','R','O','M',' ','`','E','r','r','o','r','`',' ',
831 'W','H','E','R','E',' ','`','E','r','r','o','r','`',' ','=',' ',
832 '%','s',0
834 MSIRECORD *row = 0;
835 LPWSTR deformated = NULL;
837 deformat_string( package, target, &deformated );
839 /* first try treat the error as a number */
840 row = MSI_QueryGetRecord( package->db, query, deformated );
841 if( row )
843 LPCWSTR error = MSI_RecordGetString( row, 1 );
844 if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
845 MessageBoxW( NULL, error, NULL, MB_OK );
846 msiobj_release( &row->hdr );
848 else if ((package->ui_level & INSTALLUILEVEL_MASK) != INSTALLUILEVEL_NONE)
849 MessageBoxW( NULL, deformated, NULL, MB_OK );
851 msi_free( deformated );
853 return ERROR_INSTALL_FAILURE;
856 static UINT HANDLE_CustomType23( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
857 INT type, const WCHAR *action )
859 static const WCHAR msiexecW[] = {'m','s','i','e','x','e','c',0};
860 static const WCHAR paramsW[] = {'/','q','b',' ','/','i',' '};
861 WCHAR *dir, *arg, *p;
862 UINT len_src, len_dir, len_tgt, len = sizeof(paramsW)/sizeof(paramsW[0]);
863 HANDLE handle;
865 if (!(dir = msi_dup_property( package->db, szOriginalDatabase ))) return ERROR_OUTOFMEMORY;
866 if (!(p = strrchrW( dir, '\\' )) && !(p = strrchrW( dir, '/' )))
868 msi_free( dir );
869 return ERROR_FUNCTION_FAILED;
871 *p = 0;
872 len_dir = p - dir;
873 len_src = strlenW( source );
874 len_tgt = strlenW( target );
875 if (!(arg = msi_alloc( (len + len_dir + len_src + len_tgt + 5) * sizeof(WCHAR) )))
877 msi_free( dir );
878 return ERROR_OUTOFMEMORY;
880 memcpy( arg, paramsW, sizeof(paramsW) );
881 arg[len++] = '"';
882 memcpy( arg + len, dir, len_dir * sizeof(WCHAR) );
883 len += len_dir;
884 arg[len++] = '\\';
885 memcpy( arg + len, source, len_src * sizeof(WCHAR) );
886 len += len_src;
887 arg[len++] = '"';
888 arg[len++] = ' ';
889 strcpyW( arg + len, target );
891 TRACE("installing %s concurrently\n", debugstr_w(source));
893 handle = execute_command( msiexecW, arg, dir );
894 msi_free( dir );
895 msi_free( arg );
896 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
897 return wait_process_handle( package, type, handle, action );
900 static UINT HANDLE_CustomType50( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
901 INT type, const WCHAR *action )
903 WCHAR *exe, *arg;
904 HANDLE handle;
906 if (!(exe = msi_dup_property( package->db, source ))) return ERROR_SUCCESS;
908 deformat_string( package, target, &arg );
909 TRACE("exe %s arg %s\n", debugstr_w(exe), debugstr_w(arg));
911 handle = execute_command( exe, arg, szCRoot );
912 msi_free( exe );
913 msi_free( arg );
914 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
915 return wait_process_handle( package, type, handle, action );
918 static UINT HANDLE_CustomType34( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
919 INT type, const WCHAR *action )
921 const WCHAR *workingdir = NULL;
922 HANDLE handle;
923 WCHAR *cmd;
925 if (source)
927 workingdir = msi_get_target_folder( package, source );
928 if (!workingdir) return ERROR_FUNCTION_FAILED;
930 deformat_string( package, target, &cmd );
931 if (!cmd) return ERROR_FUNCTION_FAILED;
933 TRACE("cmd %s dir %s\n", debugstr_w(cmd), debugstr_w(workingdir));
935 handle = execute_command( NULL, cmd, workingdir );
936 msi_free( cmd );
937 if (handle == INVALID_HANDLE_VALUE) return ERROR_SUCCESS;
938 return wait_process_handle( package, type, handle, action );
941 static DWORD ACTION_CallScript( const GUID *guid )
943 msi_custom_action_info *info;
944 MSIHANDLE hPackage;
945 UINT r = ERROR_FUNCTION_FAILED;
947 info = find_action_by_guid( guid );
948 if (!info)
950 ERR("failed to find action %s\n", debugstr_guid( guid) );
951 return ERROR_FUNCTION_FAILED;
954 TRACE("function %s, script %s\n", debugstr_w( info->target ), debugstr_w( info->source ) );
956 hPackage = alloc_msihandle( &info->package->hdr );
957 if (hPackage)
959 r = call_script( hPackage, info->type, info->source, info->target, info->action );
960 TRACE("script returned %u\n", r);
961 MsiCloseHandle( hPackage );
963 else
964 ERR("failed to create handle for %p\n", info->package );
966 release_custom_action_data( info );
967 return r;
970 static DWORD WINAPI ScriptThread( LPVOID arg )
972 LPGUID guid = arg;
973 DWORD rc;
975 TRACE("custom action (%x) started\n", GetCurrentThreadId() );
977 rc = ACTION_CallScript( guid );
979 TRACE("custom action (%x) returned %i\n", GetCurrentThreadId(), rc );
981 MsiCloseAllHandles();
982 return rc;
985 static msi_custom_action_info *do_msidbCustomActionTypeScript(
986 MSIPACKAGE *package, INT type, LPCWSTR script, LPCWSTR function, LPCWSTR action )
988 msi_custom_action_info *info;
990 info = msi_alloc( sizeof *info );
991 if (!info)
992 return NULL;
994 msiobj_addref( &package->hdr );
995 info->refs = 2; /* 1 for our caller and 1 for thread we created */
996 info->package = package;
997 info->type = type;
998 info->target = strdupW( function );
999 info->source = strdupW( script );
1000 info->action = strdupW( action );
1001 CoCreateGuid( &info->guid );
1003 EnterCriticalSection( &msi_custom_action_cs );
1004 list_add_tail( &msi_pending_custom_actions, &info->entry );
1005 LeaveCriticalSection( &msi_custom_action_cs );
1007 info->handle = CreateThread( NULL, 0, ScriptThread, &info->guid, 0, NULL );
1008 if (!info->handle)
1010 /* release both references */
1011 release_custom_action_data( info );
1012 release_custom_action_data( info );
1013 return NULL;
1016 return info;
1019 static UINT HANDLE_CustomType37_38( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1020 INT type, const WCHAR *action )
1022 msi_custom_action_info *info;
1024 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1026 info = do_msidbCustomActionTypeScript( package, type, target, NULL, action );
1027 return wait_thread_handle( info );
1030 static UINT HANDLE_CustomType5_6( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1031 INT type, const WCHAR *action )
1033 static const WCHAR query[] = {
1034 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1035 '`','B','i' ,'n','a','r','y','`',' ','W','H','E','R','E',' ',
1036 '`','N','a','m','e','`',' ','=',' ','\'','%','s','\'',0};
1037 MSIRECORD *row = NULL;
1038 msi_custom_action_info *info;
1039 CHAR *buffer = NULL;
1040 WCHAR *bufferw = NULL;
1041 DWORD sz = 0;
1042 UINT r;
1044 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1046 row = MSI_QueryGetRecord(package->db, query, source);
1047 if (!row)
1048 return ERROR_FUNCTION_FAILED;
1050 r = MSI_RecordReadStream(row, 2, NULL, &sz);
1051 if (r != ERROR_SUCCESS) goto done;
1053 buffer = msi_alloc( sz + 1 );
1054 if (!buffer)
1056 r = ERROR_FUNCTION_FAILED;
1057 goto done;
1060 r = MSI_RecordReadStream(row, 2, buffer, &sz);
1061 if (r != ERROR_SUCCESS)
1062 goto done;
1064 buffer[sz] = 0;
1065 bufferw = strdupAtoW(buffer);
1066 if (!bufferw)
1068 r = ERROR_FUNCTION_FAILED;
1069 goto done;
1072 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1073 r = wait_thread_handle( info );
1075 done:
1076 msi_free(bufferw);
1077 msi_free(buffer);
1078 msiobj_release(&row->hdr);
1079 return r;
1082 static UINT HANDLE_CustomType21_22( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1083 INT type, const WCHAR *action )
1085 msi_custom_action_info *info;
1086 MSIFILE *file;
1087 HANDLE hFile;
1088 DWORD sz, szHighWord = 0, read;
1089 CHAR *buffer=NULL;
1090 WCHAR *bufferw=NULL;
1091 BOOL bRet;
1092 UINT r;
1094 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1096 file = msi_get_loaded_file(package, source);
1097 if (!file)
1099 ERR("invalid file key %s\n", debugstr_w(source));
1100 return ERROR_FUNCTION_FAILED;
1103 hFile = CreateFileW(file->TargetPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1104 if (hFile == INVALID_HANDLE_VALUE) return ERROR_FUNCTION_FAILED;
1106 sz = GetFileSize(hFile, &szHighWord);
1107 if (sz == INVALID_FILE_SIZE || szHighWord != 0)
1109 CloseHandle(hFile);
1110 return ERROR_FUNCTION_FAILED;
1112 buffer = msi_alloc( sz + 1 );
1113 if (!buffer)
1115 CloseHandle(hFile);
1116 return ERROR_FUNCTION_FAILED;
1118 bRet = ReadFile(hFile, buffer, sz, &read, NULL);
1119 CloseHandle(hFile);
1120 if (!bRet)
1122 r = ERROR_FUNCTION_FAILED;
1123 goto done;
1125 buffer[read] = 0;
1126 bufferw = strdupAtoW(buffer);
1127 if (!bufferw)
1129 r = ERROR_FUNCTION_FAILED;
1130 goto done;
1132 info = do_msidbCustomActionTypeScript( package, type, bufferw, target, action );
1133 r = wait_thread_handle( info );
1135 done:
1136 msi_free(bufferw);
1137 msi_free(buffer);
1138 return r;
1141 static UINT HANDLE_CustomType53_54( MSIPACKAGE *package, const WCHAR *source, const WCHAR *target,
1142 INT type, const WCHAR *action )
1144 msi_custom_action_info *info;
1145 WCHAR *prop;
1147 TRACE("%s %s\n", debugstr_w(source), debugstr_w(target));
1149 prop = msi_dup_property( package->db, source );
1150 if (!prop) return ERROR_SUCCESS;
1152 info = do_msidbCustomActionTypeScript( package, type, prop, NULL, action );
1153 msi_free(prop);
1154 return wait_thread_handle( info );
1157 static BOOL action_type_matches_script( UINT type, UINT script )
1159 switch (script)
1161 case SCRIPT_NONE:
1162 case SCRIPT_INSTALL:
1163 return !(type & msidbCustomActionTypeCommit) && !(type & msidbCustomActionTypeRollback);
1164 case SCRIPT_COMMIT:
1165 return (type & msidbCustomActionTypeCommit);
1166 case SCRIPT_ROLLBACK:
1167 return (type & msidbCustomActionTypeRollback);
1168 default:
1169 ERR("unhandled script %u\n", script);
1171 return FALSE;
1174 static UINT defer_custom_action( MSIPACKAGE *package, const WCHAR *action, UINT type )
1176 WCHAR *actiondata = msi_dup_property( package->db, action );
1177 WCHAR *usersid = msi_dup_property( package->db, szUserSID );
1178 WCHAR *prodcode = msi_dup_property( package->db, szProductCode );
1179 WCHAR *deferred = msi_get_deferred_action( action, actiondata, usersid, prodcode );
1181 if (!deferred)
1183 msi_free( actiondata );
1184 msi_free( usersid );
1185 msi_free( prodcode );
1186 return ERROR_OUTOFMEMORY;
1188 if (type & msidbCustomActionTypeCommit)
1190 TRACE("deferring commit action\n");
1191 msi_schedule_action( package, SCRIPT_COMMIT, deferred );
1193 else if (type & msidbCustomActionTypeRollback)
1195 TRACE("deferring rollback action\n");
1196 msi_schedule_action( package, SCRIPT_ROLLBACK, deferred );
1198 else
1200 TRACE("deferring install action\n");
1201 msi_schedule_action( package, SCRIPT_INSTALL, deferred );
1204 msi_free( actiondata );
1205 msi_free( usersid );
1206 msi_free( prodcode );
1207 msi_free( deferred );
1208 return ERROR_SUCCESS;
1211 UINT ACTION_CustomAction( MSIPACKAGE *package, LPCWSTR action, UINT script )
1213 static const WCHAR query[] = {
1214 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
1215 '`','C','u','s','t','o','m','A','c','t','i','o','n','`',' ','W','H','E','R','E',' ',
1216 '`','A','c','t','i' ,'o','n','`',' ','=',' ','\'','%','s','\'',0};
1217 UINT rc = ERROR_SUCCESS;
1218 MSIRECORD *row;
1219 UINT type;
1220 const WCHAR *source, *target, *ptr, *deferred_data = NULL;
1221 WCHAR *deformated = NULL;
1222 int len;
1224 /* deferred action: [properties]Action */
1225 if ((ptr = strrchrW(action, ']')))
1227 deferred_data = action;
1228 action = ptr + 1;
1231 row = MSI_QueryGetRecord( package->db, query, action );
1232 if (!row)
1233 return ERROR_FUNCTION_NOT_CALLED;
1235 type = MSI_RecordGetInteger(row,2);
1236 source = MSI_RecordGetString(row,3);
1237 target = MSI_RecordGetString(row,4);
1239 TRACE("Handling custom action %s (%x %s %s)\n",debugstr_w(action),type,
1240 debugstr_w(source), debugstr_w(target));
1242 /* handle some of the deferred actions */
1243 if (type & msidbCustomActionTypeTSAware)
1244 FIXME("msidbCustomActionTypeTSAware not handled\n");
1246 if (type & msidbCustomActionTypeInScript)
1248 if (type & msidbCustomActionTypeNoImpersonate)
1249 WARN("msidbCustomActionTypeNoImpersonate not handled\n");
1251 if (!action_type_matches_script( type, script ))
1253 rc = defer_custom_action( package, action, type );
1254 goto end;
1256 else
1258 LPWSTR actiondata = msi_dup_property( package->db, action );
1260 if (type & msidbCustomActionTypeInScript)
1261 package->scheduled_action_running = TRUE;
1263 if (type & msidbCustomActionTypeCommit)
1264 package->commit_action_running = TRUE;
1266 if (type & msidbCustomActionTypeRollback)
1267 package->rollback_action_running = TRUE;
1269 if (deferred_data)
1270 set_deferred_action_props(package, deferred_data);
1271 else if (actiondata)
1272 msi_set_property( package->db, szCustomActionData, actiondata, -1 );
1273 else
1274 msi_set_property( package->db, szCustomActionData, szEmpty, -1 );
1276 msi_free(actiondata);
1279 else if (!check_execution_scheduling_options(package,action,type))
1281 rc = ERROR_SUCCESS;
1282 goto end;
1285 switch (type & CUSTOM_ACTION_TYPE_MASK)
1287 case 1: /* DLL file stored in a Binary table stream */
1288 rc = HANDLE_CustomType1(package,source,target,type,action);
1289 break;
1290 case 2: /* EXE file stored in a Binary table stream */
1291 rc = HANDLE_CustomType2(package,source,target,type,action);
1292 break;
1293 case 18: /*EXE file installed with package */
1294 rc = HANDLE_CustomType18(package,source,target,type,action);
1295 break;
1296 case 19: /* Error that halts install */
1297 rc = HANDLE_CustomType19(package,source,target,type,action);
1298 break;
1299 case 17:
1300 rc = HANDLE_CustomType17(package,source,target,type,action);
1301 break;
1302 case 23: /* installs another package in the source tree */
1303 deformat_string(package,target,&deformated);
1304 rc = HANDLE_CustomType23(package,source,deformated,type,action);
1305 msi_free(deformated);
1306 break;
1307 case 50: /*EXE file specified by a property value */
1308 rc = HANDLE_CustomType50(package,source,target,type,action);
1309 break;
1310 case 34: /*EXE to be run in specified directory */
1311 rc = HANDLE_CustomType34(package,source,target,type,action);
1312 break;
1313 case 35: /* Directory set with formatted text. */
1314 deformat_string(package,target,&deformated);
1315 MSI_SetTargetPathW(package, source, deformated);
1316 msi_free(deformated);
1317 break;
1318 case 51: /* Property set with formatted text. */
1319 if (!source)
1320 break;
1322 len = deformat_string( package, target, &deformated );
1323 rc = msi_set_property( package->db, source, deformated, len );
1324 if (rc == ERROR_SUCCESS && !strcmpW( source, szSourceDir ))
1325 msi_reset_folders( package, TRUE );
1326 msi_free(deformated);
1327 break;
1328 case 37: /* JScript/VBScript text stored in target column. */
1329 case 38:
1330 rc = HANDLE_CustomType37_38(package,source,target,type,action);
1331 break;
1332 case 5:
1333 case 6: /* JScript/VBScript file stored in a Binary table stream. */
1334 rc = HANDLE_CustomType5_6(package,source,target,type,action);
1335 break;
1336 case 21: /* JScript/VBScript file installed with the product. */
1337 case 22:
1338 rc = HANDLE_CustomType21_22(package,source,target,type,action);
1339 break;
1340 case 53: /* JScript/VBScript text specified by a property value. */
1341 case 54:
1342 rc = HANDLE_CustomType53_54(package,source,target,type,action);
1343 break;
1344 default:
1345 FIXME("unhandled action type %u (%s %s)\n", type & CUSTOM_ACTION_TYPE_MASK,
1346 debugstr_w(source), debugstr_w(target));
1349 end:
1350 package->scheduled_action_running = FALSE;
1351 package->commit_action_running = FALSE;
1352 package->rollback_action_running = FALSE;
1353 msiobj_release(&row->hdr);
1354 return rc;
1357 void ACTION_FinishCustomActions(const MSIPACKAGE* package)
1359 struct list *item;
1360 HANDLE *wait_handles;
1361 unsigned int handle_count, i;
1362 msi_custom_action_info *info, *cursor;
1364 while ((item = list_head( &package->RunningActions )))
1366 MSIRUNNINGACTION *action = LIST_ENTRY( item, MSIRUNNINGACTION, entry );
1368 list_remove( &action->entry );
1370 TRACE("waiting for %s\n", debugstr_w( action->name ) );
1371 msi_dialog_check_messages( action->handle );
1373 CloseHandle( action->handle );
1374 msi_free( action->name );
1375 msi_free( action );
1378 EnterCriticalSection( &msi_custom_action_cs );
1380 handle_count = list_count( &msi_pending_custom_actions );
1381 wait_handles = msi_alloc( handle_count * sizeof(HANDLE) );
1383 handle_count = 0;
1384 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1386 if (info->package == package )
1388 if (DuplicateHandle(GetCurrentProcess(), info->handle, GetCurrentProcess(), &wait_handles[handle_count], SYNCHRONIZE, FALSE, 0))
1389 handle_count++;
1393 LeaveCriticalSection( &msi_custom_action_cs );
1395 for (i = 0; i < handle_count; i++)
1397 msi_dialog_check_messages( wait_handles[i] );
1398 CloseHandle( wait_handles[i] );
1400 msi_free( wait_handles );
1402 EnterCriticalSection( &msi_custom_action_cs );
1403 LIST_FOR_EACH_ENTRY_SAFE( info, cursor, &msi_pending_custom_actions, msi_custom_action_info, entry )
1405 if (info->package == package) release_custom_action_data( info );
1407 LeaveCriticalSection( &msi_custom_action_cs );
1410 UINT __cdecl s_remote_GetActionInfo(const GUID *guid, int *type, LPWSTR *dll, LPSTR *func, MSIHANDLE *hinst)
1412 msi_custom_action_info *info;
1414 info = find_action_by_guid(guid);
1415 if (!info)
1416 return ERROR_INVALID_DATA;
1418 *type = info->type;
1419 *hinst = alloc_msihandle(&info->package->hdr);
1420 *dll = strdupW(info->source);
1421 *func = strdupWtoA(info->target);
1423 release_custom_action_data(info);
1424 return ERROR_SUCCESS;