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