VirtualBox

source: vbox/trunk/src/VBox/Main/ExtPackManagerImpl.cpp@ 34575

Last change on this file since 34575 was 34569, checked in by vboxsync, 14 years ago

bug fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 75.0 KB
Line 
1/* $Id: ExtPackManagerImpl.cpp 34569 2010-12-01 13:33:06Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for Extension Packs, VBoxSVC & VBoxC.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "ExtPackManagerImpl.h"
23#include "ExtPackUtil.h"
24
25#include <iprt/buildconfig.h>
26#include <iprt/ctype.h>
27#include <iprt/dir.h>
28#include <iprt/env.h>
29#include <iprt/file.h>
30#include <iprt/ldr.h>
31#include <iprt/param.h>
32#include <iprt/path.h>
33#include <iprt/pipe.h>
34#include <iprt/process.h>
35#include <iprt/string.h>
36
37#include <VBox/com/array.h>
38#include <VBox/com/ErrorInfo.h>
39#include <VBox/log.h>
40#include <VBox/sup.h>
41#include <VBox/version.h>
42#include "AutoCaller.h"
43#include "Global.h"
44#include "SystemPropertiesImpl.h"
45#include "VirtualBoxImpl.h"
46
47
48/*******************************************************************************
49* Defined Constants And Macros *
50*******************************************************************************/
51/** @name VBOX_EXTPACK_HELPER_NAME
52 * The name of the utility application we employ to install and uninstall the
53 * extension packs. This is a set-uid-to-root binary on unixy platforms, which
54 * is why it has to be a separate application.
55 */
56#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
57# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelperApp.exe"
58#else
59# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelperApp"
60#endif
61
62
63/*******************************************************************************
64* Structures and Typedefs *
65*******************************************************************************/
66/**
67 * Private extension pack data.
68 */
69struct ExtPack::Data
70{
71public:
72 /** The extension pack descriptor (loaded from the XML, mostly). */
73 VBOXEXTPACKDESC Desc;
74 /** The file system object info of the XML file.
75 * This is for detecting changes and save time in refresh(). */
76 RTFSOBJINFO ObjInfoDesc;
77 /** Whether it's usable or not. */
78 bool fUsable;
79 /** Why it is unusable. */
80 Utf8Str strWhyUnusable;
81
82 /** Where the extension pack is located. */
83 Utf8Str strExtPackPath;
84 /** The file system object info of the extension pack directory.
85 * This is for detecting changes and save time in refresh(). */
86 RTFSOBJINFO ObjInfoExtPack;
87 /** The full path to the main module. */
88 Utf8Str strMainModPath;
89 /** The file system object info of the main module.
90 * This is used to determin whether to bother try reload it. */
91 RTFSOBJINFO ObjInfoMainMod;
92 /** The module handle of the main extension pack module. */
93 RTLDRMOD hMainMod;
94
95 /** The helper callbacks for the extension pack. */
96 VBOXEXTPACKHLP Hlp;
97 /** Pointer back to the extension pack object (for Hlp methods). */
98 ExtPack *pThis;
99 /** The extension pack registration structure. */
100 PCVBOXEXTPACKREG pReg;
101 /** The current context. */
102 VBOXEXTPACKCTX enmContext;
103 /** Set if we've made the pfnVirtualBoxReady or pfnConsoleReady call. */
104 bool fMadeReadyCall;
105};
106
107/** List of extension packs. */
108typedef std::list< ComObjPtr<ExtPack> > ExtPackList;
109
110/**
111 * Private extension pack manager data.
112 */
113struct ExtPackManager::Data
114{
115 /** The directory where the extension packs are installed. */
116 Utf8Str strBaseDir;
117 /** The directory where the extension packs can be dropped for automatic
118 * installation. */
119 Utf8Str strDropZoneDir;
120 /** The directory where the certificates this installation recognizes are
121 * stored. */
122 Utf8Str strCertificatDirPath;
123 /** The list of installed extension packs. */
124 ExtPackList llInstalledExtPacks;
125 /** Pointer to the VirtualBox object, our parent. */
126 VirtualBox *pVirtualBox;
127 /** The current context. */
128 VBOXEXTPACKCTX enmContext;
129};
130
131
132DEFINE_EMPTY_CTOR_DTOR(ExtPack)
133
134/**
135 * Called by ComObjPtr::createObject when creating the object.
136 *
137 * Just initialize the basic object state, do the rest in init().
138 *
139 * @returns S_OK.
140 */
141HRESULT ExtPack::FinalConstruct()
142{
143 m = NULL;
144 return S_OK;
145}
146
147/**
148 * Initializes the extension pack by reading its file.
149 *
150 * @returns COM status code.
151 * @param a_enmContext The context we're in.
152 * @param a_pszName The name of the extension pack. This is also the
153 * name of the subdirector under @a a_pszParentDir
154 * where the extension pack is installed.
155 * @param a_pszParentDir The parent directory.
156 */
157HRESULT ExtPack::init(VBOXEXTPACKCTX a_enmContext, const char *a_pszName, const char *a_pszParentDir)
158{
159 AutoInitSpan autoInitSpan(this);
160 AssertReturn(autoInitSpan.isOk(), E_FAIL);
161
162 static const VBOXEXTPACKHLP s_HlpTmpl =
163 {
164 /* u32Version = */ VBOXEXTPACKHLP_VERSION,
165 /* uVBoxFullVersion = */ VBOX_FULL_VERSION,
166 /* uVBoxVersionRevision = */ 0,
167 /* u32Padding = */ 0,
168 /* pszVBoxVersion = */ "",
169 /* pfnFindModule = */ ExtPack::hlpFindModule,
170 /* pfnGetFilePath = */ ExtPack::hlpGetFilePath,
171 /* pfnGetContext = */ ExtPack::hlpGetContext,
172 /* pfnReserved1 = */ ExtPack::hlpReservedN,
173 /* pfnReserved2 = */ ExtPack::hlpReservedN,
174 /* pfnReserved3 = */ ExtPack::hlpReservedN,
175 /* pfnReserved4 = */ ExtPack::hlpReservedN,
176 /* pfnReserved5 = */ ExtPack::hlpReservedN,
177 /* pfnReserved6 = */ ExtPack::hlpReservedN,
178 /* pfnReserved7 = */ ExtPack::hlpReservedN,
179 /* pfnReserved8 = */ ExtPack::hlpReservedN,
180 /* pfnReserved9 = */ ExtPack::hlpReservedN,
181 /* u32EndMarker = */ VBOXEXTPACKHLP_VERSION
182 };
183
184 /*
185 * Figure out where we live and allocate + initialize our private data.
186 */
187 char szDir[RTPATH_MAX];
188 int vrc = RTPathJoin(szDir, sizeof(szDir), a_pszParentDir, a_pszName);
189 AssertLogRelRCReturn(vrc, E_FAIL);
190
191 m = new Data;
192 m->Desc.strName = a_pszName;
193 RT_ZERO(m->ObjInfoDesc);
194 m->fUsable = false;
195 m->strWhyUnusable = tr("ExtPack::init failed");
196 m->strExtPackPath = szDir;
197 RT_ZERO(m->ObjInfoExtPack);
198 m->strMainModPath.setNull();
199 RT_ZERO(m->ObjInfoMainMod);
200 m->hMainMod = NIL_RTLDRMOD;
201 m->Hlp = s_HlpTmpl;
202 m->Hlp.pszVBoxVersion = RTBldCfgVersion();
203 m->Hlp.uVBoxInternalRevision = RTBldCfgRevision();
204 m->pThis = this;
205 m->pReg = NULL;
206 m->enmContext = a_enmContext;
207 m->fMadeReadyCall = false;
208
209 /*
210 * Probe the extension pack (this code is shared with refresh()).
211 */
212 probeAndLoad();
213
214 autoInitSpan.setSucceeded();
215 return S_OK;
216}
217
218/**
219 * COM cruft.
220 */
221void ExtPack::FinalRelease()
222{
223 uninit();
224}
225
226/**
227 * Do the actual cleanup.
228 */
229void ExtPack::uninit()
230{
231 /* Enclose the state transition Ready->InUninit->NotReady */
232 AutoUninitSpan autoUninitSpan(this);
233 if (!autoUninitSpan.uninitDone() && m != NULL)
234 {
235 if (m->hMainMod != NIL_RTLDRMOD)
236 {
237 AssertPtr(m->pReg);
238 if (m->pReg->pfnUnload != NULL)
239 m->pReg->pfnUnload(m->pReg);
240
241 RTLdrClose(m->hMainMod);
242 m->hMainMod = NIL_RTLDRMOD;
243 m->pReg = NULL;
244 }
245
246 VBoxExtPackFreeDesc(&m->Desc);
247
248 delete m;
249 m = NULL;
250 }
251}
252
253
254/**
255 * Calls the installed hook.
256 *
257 * @returns true if we left the lock, false if we didn't.
258 * @param a_pVirtualBox The VirtualBox interface.
259 * @param a_pLock The write lock held by the caller.
260 */
261bool ExtPack::callInstalledHook(IVirtualBox *a_pVirtualBox, AutoWriteLock *a_pLock)
262{
263 if ( m != NULL
264 && m->hMainMod != NIL_RTLDRMOD)
265 {
266 if (m->pReg->pfnInstalled)
267 {
268 ComPtr<ExtPack> ptrSelfRef = this;
269 a_pLock->release();
270 m->pReg->pfnInstalled(m->pReg, a_pVirtualBox);
271 a_pLock->acquire();
272 return true;
273 }
274 }
275 return false;
276}
277
278/**
279 * Calls the uninstall hook and closes the module.
280 *
281 * @returns S_OK or COM error status with error information.
282 * @param a_pVirtualBox The VirtualBox interface.
283 * @param a_fForcedRemoval When set, we'll ignore complaints from the
284 * uninstall hook.
285 * @remarks The caller holds the manager's write lock, not released.
286 */
287HRESULT ExtPack::callUninstallHookAndClose(IVirtualBox *a_pVirtualBox, bool a_fForcedRemoval)
288{
289 HRESULT hrc = S_OK;
290
291 if ( m != NULL
292 && m->hMainMod != NIL_RTLDRMOD)
293 {
294 if (m->pReg->pfnUninstall && !a_fForcedRemoval)
295 {
296 int vrc = m->pReg->pfnUninstall(m->pReg, a_pVirtualBox);
297 if (RT_FAILURE(vrc))
298 {
299 LogRel(("ExtPack pfnUninstall returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
300 if (!a_fForcedRemoval)
301 hrc = setError(E_FAIL, tr("pfnUninstall returned %Rrc"), vrc);
302 }
303 }
304 if (SUCCEEDED(hrc))
305 {
306 RTLdrClose(m->hMainMod);
307 m->hMainMod = NIL_RTLDRMOD;
308 m->pReg = NULL;
309 }
310 }
311
312 return hrc;
313}
314
315/**
316 * Calls the pfnVirtualBoxReady hook.
317 *
318 * @returns true if we left the lock, false if we didn't.
319 * @param a_pVirtualBox The VirtualBox interface.
320 * @param a_pLock The write lock held by the caller.
321 */
322bool ExtPack::callVirtualBoxReadyHook(IVirtualBox *a_pVirtualBox, AutoWriteLock *a_pLock)
323{
324 if ( m != NULL
325 && m->fUsable
326 && !m->fMadeReadyCall)
327 {
328 m->fMadeReadyCall = true;
329 if (m->pReg->pfnVirtualBoxReady)
330 {
331 ComPtr<ExtPack> ptrSelfRef = this;
332 a_pLock->release();
333 m->pReg->pfnVirtualBoxReady(m->pReg, a_pVirtualBox);
334 a_pLock->acquire();
335 return true;
336 }
337 }
338 return false;
339}
340
341/**
342 * Calls the pfnConsoleReady hook.
343 *
344 * @returns true if we left the lock, false if we didn't.
345 * @param a_pConsole The Console interface.
346 * @param a_pLock The write lock held by the caller.
347 */
348bool ExtPack::callConsoleReadyHook(IConsole *a_pConsole, AutoWriteLock *a_pLock)
349{
350 if ( m != NULL
351 && m->fUsable
352 && !m->fMadeReadyCall)
353 {
354 m->fMadeReadyCall = true;
355 if (m->pReg->pfnConsoleReady)
356 {
357 ComPtr<ExtPack> ptrSelfRef = this;
358 a_pLock->release();
359 m->pReg->pfnConsoleReady(m->pReg, a_pConsole);
360 a_pLock->acquire();
361 return true;
362 }
363 }
364 return false;
365}
366
367/**
368 * Calls the pfnVMCreate hook.
369 *
370 * @returns true if we left the lock, false if we didn't.
371 * @param a_pVirtualBox The VirtualBox interface.
372 * @param a_pMachine The machine interface of the new VM.
373 * @param a_pLock The write lock held by the caller.
374 */
375bool ExtPack::callVmCreatedHook(IVirtualBox *a_pVirtualBox, IMachine *a_pMachine, AutoWriteLock *a_pLock)
376{
377 if ( m != NULL
378 && m->fUsable)
379 {
380 if (m->pReg->pfnVMCreated)
381 {
382 ComPtr<ExtPack> ptrSelfRef = this;
383 a_pLock->release();
384 m->pReg->pfnVMCreated(m->pReg, a_pVirtualBox, a_pMachine);
385 a_pLock->acquire();
386 return true;
387 }
388 }
389 return false;
390}
391
392/**
393 * Calls the pfnVMConfigureVMM hook.
394 *
395 * @returns true if we left the lock, false if we didn't.
396 * @param a_pConsole The console interface.
397 * @param a_pVM The VM handle.
398 * @param a_pLock The write lock held by the caller.
399 * @param a_pvrc Where to return the status code of the
400 * callback. This is always set. LogRel is
401 * called on if a failure status is returned.
402 */
403bool ExtPack::callVmConfigureVmmHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock, int *a_pvrc)
404{
405 *a_pvrc = VINF_SUCCESS;
406 if ( m != NULL
407 && m->fUsable)
408 {
409 if (m->pReg->pfnVMConfigureVMM)
410 {
411 ComPtr<ExtPack> ptrSelfRef = this;
412 a_pLock->release();
413 int vrc = m->pReg->pfnVMConfigureVMM(m->pReg, a_pConsole, a_pVM);
414 *a_pvrc = vrc;
415 a_pLock->acquire();
416 if (RT_FAILURE(vrc))
417 LogRel(("ExtPack pfnVMConfigureVMM returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
418 return true;
419 }
420 }
421 return false;
422}
423
424/**
425 * Calls the pfnVMPowerOn hook.
426 *
427 * @returns true if we left the lock, false if we didn't.
428 * @param a_pConsole The console interface.
429 * @param a_pVM The VM handle.
430 * @param a_pLock The write lock held by the caller.
431 * @param a_pvrc Where to return the status code of the
432 * callback. This is always set. LogRel is
433 * called on if a failure status is returned.
434 */
435bool ExtPack::callVmPowerOnHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock, int *a_pvrc)
436{
437 *a_pvrc = VINF_SUCCESS;
438 if ( m != NULL
439 && m->fUsable)
440 {
441 if (m->pReg->pfnVMPowerOn)
442 {
443 ComPtr<ExtPack> ptrSelfRef = this;
444 a_pLock->release();
445 int vrc = m->pReg->pfnVMPowerOn(m->pReg, a_pConsole, a_pVM);
446 *a_pvrc = vrc;
447 a_pLock->acquire();
448 if (RT_FAILURE(vrc))
449 LogRel(("ExtPack pfnVMPowerOn returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
450 return true;
451 }
452 }
453 return false;
454}
455
456/**
457 * Calls the pfnVMPowerOff hook.
458 *
459 * @returns true if we left the lock, false if we didn't.
460 * @param a_pConsole The console interface.
461 * @param a_pVM The VM handle.
462 * @param a_pLock The write lock held by the caller.
463 */
464bool ExtPack::callVmPowerOffHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock)
465{
466 if ( m != NULL
467 && m->fUsable)
468 {
469 if (m->pReg->pfnVMPowerOff)
470 {
471 ComPtr<ExtPack> ptrSelfRef = this;
472 a_pLock->release();
473 m->pReg->pfnVMPowerOff(m->pReg, a_pConsole, a_pVM);
474 a_pLock->acquire();
475 return true;
476 }
477 }
478 return false;
479}
480
481/**
482 * Check if the extension pack is usable and has an VRDE module.
483 *
484 * @returns S_OK or COM error status with error information.
485 *
486 * @remarks Caller holds the extension manager lock for reading, no locking
487 * necessary.
488 */
489HRESULT ExtPack::checkVrde(void)
490{
491 HRESULT hrc;
492 if ( m != NULL
493 && m->fUsable)
494 {
495 if (m->Desc.strVrdeModule.isNotEmpty())
496 hrc = S_OK;
497 else
498 hrc = setError(E_FAIL, tr("The extension pack '%s' does not include a VRDE module"), m->Desc.strName.c_str());
499 }
500 else
501 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
502 return hrc;
503}
504
505/**
506 * Same as checkVrde(), except that it also resolves the path to the module.
507 *
508 * @returns S_OK or COM error status with error information.
509 * @param a_pstrVrdeLibrary Where to return the path on success.
510 *
511 * @remarks Caller holds the extension manager lock for reading, no locking
512 * necessary.
513 */
514HRESULT ExtPack::getVrdpLibraryName(Utf8Str *a_pstrVrdeLibrary)
515{
516 HRESULT hrc = checkVrde();
517 if (SUCCEEDED(hrc))
518 {
519 if (findModule(m->Desc.strVrdeModule.c_str(), NULL, VBOXEXTPACKMODKIND_R3,
520 a_pstrVrdeLibrary, NULL /*a_pfNative*/, NULL /*a_pObjInfo*/))
521 hrc = S_OK;
522 else
523 hrc = setError(E_FAIL, tr("Failed to locate the VRDE module '%s' in extension pack '%s'"),
524 m->Desc.strVrdeModule.c_str(), m->Desc.strName.c_str());
525 }
526 return hrc;
527}
528
529/**
530 * Check if this extension pack wishes to be the default VRDE provider.
531 *
532 * @returns @c true if it wants to and it is in a usable state, otherwise
533 * @c false.
534 *
535 * @remarks Caller holds the extension manager lock for reading, no locking
536 * necessary.
537 */
538bool ExtPack::wantsToBeDefaultVrde(void) const
539{
540 return m->fUsable
541 && m->Desc.strVrdeModule.isNotEmpty();
542}
543
544/**
545 * Refreshes the extension pack state.
546 *
547 * This is called by the manager so that the on disk changes are picked up.
548 *
549 * @returns S_OK or COM error status with error information.
550 *
551 * @param a_pfCanDelete Optional can-delete-this-object output indicator.
552 *
553 * @remarks Caller holds the extension manager lock for writing.
554 * @remarks Only called in VBoxSVC.
555 */
556HRESULT ExtPack::refresh(bool *a_pfCanDelete)
557{
558 if (a_pfCanDelete)
559 *a_pfCanDelete = false;
560
561 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS); /* for the COMGETTERs */
562
563 /*
564 * Has the module been deleted?
565 */
566 RTFSOBJINFO ObjInfoExtPack;
567 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
568 if ( RT_FAILURE(vrc)
569 || !RTFS_IS_DIRECTORY(ObjInfoExtPack.Attr.fMode))
570 {
571 if (a_pfCanDelete)
572 *a_pfCanDelete = true;
573 return S_OK;
574 }
575
576 /*
577 * We've got a directory, so try query file system object info for the
578 * files we are interested in as well.
579 */
580 RTFSOBJINFO ObjInfoDesc;
581 char szDescFilePath[RTPATH_MAX];
582 vrc = RTPathJoin(szDescFilePath, sizeof(szDescFilePath), m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME);
583 if (RT_SUCCESS(vrc))
584 vrc = RTPathQueryInfoEx(szDescFilePath, &ObjInfoDesc, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
585 if (RT_FAILURE(vrc))
586 RT_ZERO(ObjInfoDesc);
587
588 RTFSOBJINFO ObjInfoMainMod;
589 if (m->strMainModPath.isNotEmpty())
590 vrc = RTPathQueryInfoEx(m->strMainModPath.c_str(), &ObjInfoMainMod, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
591 if (m->strMainModPath.isEmpty() || RT_FAILURE(vrc))
592 RT_ZERO(ObjInfoMainMod);
593
594 /*
595 * If we have a usable module already, just verify that things haven't
596 * changed since we loaded it.
597 */
598 if (m->fUsable)
599 {
600 /** @todo not important, so it can wait. */
601 }
602 /*
603 * Ok, it is currently not usable. If anything has changed since last time
604 * reprobe the extension pack.
605 */
606 else if ( !objinfoIsEqual(&ObjInfoDesc, &m->ObjInfoDesc)
607 || !objinfoIsEqual(&ObjInfoMainMod, &m->ObjInfoMainMod)
608 || !objinfoIsEqual(&ObjInfoExtPack, &m->ObjInfoExtPack) )
609 probeAndLoad();
610
611 return S_OK;
612}
613
614/**
615 * Probes the extension pack, loading the main dll and calling its registration
616 * entry point.
617 *
618 * This updates the state accordingly, the strWhyUnusable and fUnusable members
619 * being the most important ones.
620 */
621void ExtPack::probeAndLoad(void)
622{
623 m->fUsable = false;
624 m->fMadeReadyCall = false;
625
626 /*
627 * Query the file system info for the extension pack directory. This and
628 * all other file system info we save is for the benefit of refresh().
629 */
630 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &m->ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
631 if (RT_FAILURE(vrc))
632 {
633 m->strWhyUnusable.printf(tr("RTPathQueryInfoEx on '%s' failed: %Rrc"), m->strExtPackPath.c_str(), vrc);
634 return;
635 }
636 if (!RTFS_IS_DIRECTORY(m->ObjInfoExtPack.Attr.fMode))
637 {
638 if (RTFS_IS_SYMLINK(m->ObjInfoExtPack.Attr.fMode))
639 m->strWhyUnusable.printf(tr("'%s' is a symbolic link, this is not allowed"), m->strExtPackPath.c_str(), vrc);
640 else if (RTFS_IS_FILE(m->ObjInfoExtPack.Attr.fMode))
641 m->strWhyUnusable.printf(tr("'%s' is a symbolic file, not a directory"), m->strExtPackPath.c_str(), vrc);
642 else
643 m->strWhyUnusable.printf(tr("'%s' is not a directory (fMode=%#x)"), m->strExtPackPath.c_str(), m->ObjInfoExtPack.Attr.fMode);
644 return;
645 }
646
647 char szErr[2048];
648 RT_ZERO(szErr);
649 vrc = SUPR3HardenedVerifyDir(m->strExtPackPath.c_str(), true /*fRecursive*/, true /*fCheckFiles*/, szErr, sizeof(szErr));
650 if (RT_FAILURE(vrc))
651 {
652 m->strWhyUnusable.printf(tr("%s (rc=%Rrc)"), szErr, vrc);
653 return;
654 }
655
656 /*
657 * Read the description file.
658 */
659 iprt::MiniString strSavedName(m->Desc.strName);
660 iprt::MiniString *pStrLoadErr = VBoxExtPackLoadDesc(m->strExtPackPath.c_str(), &m->Desc, &m->ObjInfoDesc);
661 if (pStrLoadErr != NULL)
662 {
663 m->strWhyUnusable.printf(tr("Failed to load '%s/%s': %s"),
664 m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME, pStrLoadErr->c_str());
665 m->Desc.strName = strSavedName;
666 delete pStrLoadErr;
667 return;
668 }
669
670 /*
671 * Make sure the XML name and directory matches.
672 */
673 if (!m->Desc.strName.equalsIgnoreCase(strSavedName))
674 {
675 m->strWhyUnusable.printf(tr("The description name ('%s') and directory name ('%s') does not match"),
676 m->Desc.strName.c_str(), strSavedName.c_str());
677 m->Desc.strName = strSavedName;
678 return;
679 }
680
681 /*
682 * Load the main DLL and call the predefined entry point.
683 */
684 bool fIsNative;
685 if (!findModule(m->Desc.strMainModule.c_str(), NULL /* default extension */, VBOXEXTPACKMODKIND_R3,
686 &m->strMainModPath, &fIsNative, &m->ObjInfoMainMod))
687 {
688 m->strWhyUnusable.printf(tr("Failed to locate the main module ('%s')"), m->Desc.strMainModule.c_str());
689 return;
690 }
691
692 vrc = SUPR3HardenedVerifyPlugIn(m->strMainModPath.c_str(), szErr, sizeof(szErr));
693 if (RT_FAILURE(vrc))
694 {
695 m->strWhyUnusable.printf(tr("%s"), szErr);
696 return;
697 }
698
699 if (fIsNative)
700 {
701 vrc = RTLdrLoad(m->strMainModPath.c_str(), &m->hMainMod);
702 if (RT_FAILURE(vrc))
703 {
704 m->hMainMod = NIL_RTLDRMOD;
705 m->strWhyUnusable.printf(tr("Failed to locate load the main module ('%s'): %Rrc"),
706 m->strMainModPath.c_str(), vrc);
707 return;
708 }
709 }
710 else
711 {
712 m->strWhyUnusable.printf(tr("Only native main modules are currently supported"));
713 return;
714 }
715
716 /*
717 * Resolve the predefined entry point.
718 */
719 PFNVBOXEXTPACKREGISTER pfnRegistration;
720 vrc = RTLdrGetSymbol(m->hMainMod, VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, (void **)&pfnRegistration);
721 if (RT_SUCCESS(vrc))
722 {
723 RT_ZERO(szErr);
724 vrc = pfnRegistration(&m->Hlp, &m->pReg, szErr, sizeof(szErr) - 16);
725 if ( RT_SUCCESS(vrc)
726 && szErr[0] == '\0'
727 && VALID_PTR(m->pReg))
728 {
729 if ( VBOXEXTPACK_IS_MAJOR_VER_EQUAL(m->pReg->u32Version, VBOXEXTPACKREG_VERSION)
730 && m->pReg->u32EndMarker == m->pReg->u32Version)
731 {
732 if ( (!m->pReg->pfnInstalled || RT_VALID_PTR(m->pReg->pfnInstalled))
733 && (!m->pReg->pfnUninstall || RT_VALID_PTR(m->pReg->pfnUninstall))
734 && (!m->pReg->pfnVirtualBoxReady || RT_VALID_PTR(m->pReg->pfnVirtualBoxReady))
735 && (!m->pReg->pfnConsoleReady || RT_VALID_PTR(m->pReg->pfnConsoleReady))
736 && (!m->pReg->pfnUnload || RT_VALID_PTR(m->pReg->pfnUnload))
737 && (!m->pReg->pfnVMCreated || RT_VALID_PTR(m->pReg->pfnVMCreated))
738 && (!m->pReg->pfnVMConfigureVMM || RT_VALID_PTR(m->pReg->pfnVMConfigureVMM))
739 && (!m->pReg->pfnVMPowerOn || RT_VALID_PTR(m->pReg->pfnVMPowerOn))
740 && (!m->pReg->pfnVMPowerOff || RT_VALID_PTR(m->pReg->pfnVMPowerOff))
741 && (!m->pReg->pfnQueryObject || RT_VALID_PTR(m->pReg->pfnQueryObject))
742 )
743 {
744 /*
745 * We're good!
746 */
747 m->fUsable = true;
748 m->strWhyUnusable.setNull();
749 return;
750 }
751
752 m->strWhyUnusable = tr("The registration structure contains on or more invalid function pointers");
753 }
754 else
755 m->strWhyUnusable.printf(tr("Unsupported registration structure version %u.%u"),
756 RT_HIWORD(m->pReg->u32Version), RT_LOWORD(m->pReg->u32Version));
757 }
758 else
759 {
760 szErr[sizeof(szErr) - 1] = '\0';
761 m->strWhyUnusable.printf(tr("%s returned %Rrc, pReg=%p szErr='%s'"),
762 VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, vrc, m->pReg, szErr);
763 }
764 m->pReg = NULL;
765 }
766 else
767 m->strWhyUnusable.printf(tr("Failed to resolve exported symbol '%s' in the main module: %Rrc"),
768 VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, vrc);
769
770 RTLdrClose(m->hMainMod);
771 m->hMainMod = NIL_RTLDRMOD;
772}
773
774/**
775 * Finds a module.
776 *
777 * @returns true if found, false if not.
778 * @param a_pszName The module base name (no extension).
779 * @param a_pszExt The extension. If NULL we use default
780 * extensions.
781 * @param a_enmKind The kind of module to locate.
782 * @param a_pStrFound Where to return the path to the module we've
783 * found.
784 * @param a_pfNative Where to return whether this is a native module
785 * or an agnostic one. Optional.
786 * @param a_pObjInfo Where to return the file system object info for
787 * the module. Optional.
788 */
789bool ExtPack::findModule(const char *a_pszName, const char *a_pszExt, VBOXEXTPACKMODKIND a_enmKind,
790 Utf8Str *a_pStrFound, bool *a_pfNative, PRTFSOBJINFO a_pObjInfo) const
791{
792 /*
793 * Try the native path first.
794 */
795 char szPath[RTPATH_MAX];
796 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetDotArch());
797 AssertLogRelRCReturn(vrc, false);
798 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
799 AssertLogRelRCReturn(vrc, false);
800 if (!a_pszExt)
801 {
802 const char *pszDefExt;
803 switch (a_enmKind)
804 {
805 case VBOXEXTPACKMODKIND_RC: pszDefExt = ".rc"; break;
806 case VBOXEXTPACKMODKIND_R0: pszDefExt = ".r0"; break;
807 case VBOXEXTPACKMODKIND_R3: pszDefExt = RTLdrGetSuff(); break;
808 default:
809 AssertFailedReturn(false);
810 }
811 vrc = RTStrCat(szPath, sizeof(szPath), pszDefExt);
812 AssertLogRelRCReturn(vrc, false);
813 }
814
815 RTFSOBJINFO ObjInfo;
816 if (!a_pObjInfo)
817 a_pObjInfo = &ObjInfo;
818 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
819 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
820 {
821 if (a_pfNative)
822 *a_pfNative = true;
823 *a_pStrFound = szPath;
824 return true;
825 }
826
827 /*
828 * Try the platform agnostic modules.
829 */
830 /* gcc.x86/module.rel */
831 char szSubDir[32];
832 RTStrPrintf(szSubDir, sizeof(szSubDir), "%s.%s", RTBldCfgCompiler(), RTBldCfgTargetArch());
833 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szSubDir);
834 AssertLogRelRCReturn(vrc, false);
835 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
836 AssertLogRelRCReturn(vrc, false);
837 if (!a_pszExt)
838 {
839 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
840 AssertLogRelRCReturn(vrc, false);
841 }
842 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
843 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
844 {
845 if (a_pfNative)
846 *a_pfNative = false;
847 *a_pStrFound = szPath;
848 return true;
849 }
850
851 /* x86/module.rel */
852 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetArch());
853 AssertLogRelRCReturn(vrc, false);
854 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
855 AssertLogRelRCReturn(vrc, false);
856 if (!a_pszExt)
857 {
858 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
859 AssertLogRelRCReturn(vrc, false);
860 }
861 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
862 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
863 {
864 if (a_pfNative)
865 *a_pfNative = false;
866 *a_pStrFound = szPath;
867 return true;
868 }
869
870 return false;
871}
872
873/**
874 * Compares two file system object info structures.
875 *
876 * @returns true if equal, false if not.
877 * @param pObjInfo1 The first.
878 * @param pObjInfo2 The second.
879 * @todo IPRT should do this, really.
880 */
881/* static */ bool ExtPack::objinfoIsEqual(PCRTFSOBJINFO pObjInfo1, PCRTFSOBJINFO pObjInfo2)
882{
883 if (!RTTimeSpecIsEqual(&pObjInfo1->ModificationTime, &pObjInfo2->ModificationTime))
884 return false;
885 if (!RTTimeSpecIsEqual(&pObjInfo1->ChangeTime, &pObjInfo2->ChangeTime))
886 return false;
887 if (!RTTimeSpecIsEqual(&pObjInfo1->BirthTime, &pObjInfo2->BirthTime))
888 return false;
889 if (pObjInfo1->cbObject != pObjInfo2->cbObject)
890 return false;
891 if (pObjInfo1->Attr.fMode != pObjInfo2->Attr.fMode)
892 return false;
893 if (pObjInfo1->Attr.enmAdditional == pObjInfo2->Attr.enmAdditional)
894 {
895 switch (pObjInfo1->Attr.enmAdditional)
896 {
897 case RTFSOBJATTRADD_UNIX:
898 if (pObjInfo1->Attr.u.Unix.uid != pObjInfo2->Attr.u.Unix.uid)
899 return false;
900 if (pObjInfo1->Attr.u.Unix.gid != pObjInfo2->Attr.u.Unix.gid)
901 return false;
902 if (pObjInfo1->Attr.u.Unix.INodeIdDevice != pObjInfo2->Attr.u.Unix.INodeIdDevice)
903 return false;
904 if (pObjInfo1->Attr.u.Unix.INodeId != pObjInfo2->Attr.u.Unix.INodeId)
905 return false;
906 if (pObjInfo1->Attr.u.Unix.GenerationId != pObjInfo2->Attr.u.Unix.GenerationId)
907 return false;
908 break;
909 default:
910 break;
911 }
912 }
913 return true;
914}
915
916
917/**
918 * @interface_method_impl{VBOXEXTPACKHLP,pfnFindModule}
919 */
920/*static*/ DECLCALLBACK(int)
921ExtPack::hlpFindModule(PCVBOXEXTPACKHLP pHlp, const char *pszName, const char *pszExt, VBOXEXTPACKMODKIND enmKind,
922 char *pszFound, size_t cbFound, bool *pfNative)
923{
924 /*
925 * Validate the input and get our bearings.
926 */
927 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
928 AssertPtrNullReturn(pszExt, VERR_INVALID_POINTER);
929 AssertPtrReturn(pszFound, VERR_INVALID_POINTER);
930 AssertPtrNullReturn(pfNative, VERR_INVALID_POINTER);
931 AssertReturn(enmKind > VBOXEXTPACKMODKIND_INVALID && enmKind < VBOXEXTPACKMODKIND_END, VERR_INVALID_PARAMETER);
932
933 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
934 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
935 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
936 AssertPtrReturn(m, VERR_INVALID_POINTER);
937 ExtPack *pThis = m->pThis;
938 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
939
940 /*
941 * This is just a wrapper around findModule.
942 */
943 Utf8Str strFound;
944 if (pThis->findModule(pszName, pszExt, enmKind, &strFound, pfNative, NULL))
945 return RTStrCopy(pszFound, cbFound, strFound.c_str());
946 return VERR_FILE_NOT_FOUND;
947}
948
949/*static*/ DECLCALLBACK(int)
950ExtPack::hlpGetFilePath(PCVBOXEXTPACKHLP pHlp, const char *pszFilename, char *pszPath, size_t cbPath)
951{
952 /*
953 * Validate the input and get our bearings.
954 */
955 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
956 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
957 AssertReturn(cbPath > 0, VERR_BUFFER_OVERFLOW);
958
959 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
960 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
961 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
962 AssertPtrReturn(m, VERR_INVALID_POINTER);
963 ExtPack *pThis = m->pThis;
964 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
965
966 /*
967 * This is a simple RTPathJoin, no checking if things exists or anything.
968 */
969 int vrc = RTPathJoin(pszPath, cbPath, pThis->m->strExtPackPath.c_str(), pszFilename);
970 if (RT_FAILURE(vrc))
971 RT_BZERO(pszPath, cbPath);
972 return vrc;
973}
974
975/*static*/ DECLCALLBACK(VBOXEXTPACKCTX)
976ExtPack::hlpGetContext(PCVBOXEXTPACKHLP pHlp)
977{
978 /*
979 * Validate the input and get our bearings.
980 */
981 AssertPtrReturn(pHlp, VBOXEXTPACKCTX_INVALID);
982 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VBOXEXTPACKCTX_INVALID);
983 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
984 AssertPtrReturn(m, VBOXEXTPACKCTX_INVALID);
985 ExtPack *pThis = m->pThis;
986 AssertPtrReturn(pThis, VBOXEXTPACKCTX_INVALID);
987
988 return pThis->m->enmContext;
989}
990
991/*static*/ DECLCALLBACK(int)
992ExtPack::hlpReservedN(PCVBOXEXTPACKHLP pHlp)
993{
994 /*
995 * Validate the input and get our bearings.
996 */
997 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
998 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
999 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1000 AssertPtrReturn(m, VERR_INVALID_POINTER);
1001 ExtPack *pThis = m->pThis;
1002 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1003
1004 return VERR_NOT_IMPLEMENTED;
1005}
1006
1007
1008
1009
1010
1011STDMETHODIMP ExtPack::COMGETTER(Name)(BSTR *a_pbstrName)
1012{
1013 CheckComArgOutPointerValid(a_pbstrName);
1014
1015 AutoCaller autoCaller(this);
1016 HRESULT hrc = autoCaller.rc();
1017 if (SUCCEEDED(hrc))
1018 {
1019 Bstr str(m->Desc.strName);
1020 str.cloneTo(a_pbstrName);
1021 }
1022 return hrc;
1023}
1024
1025STDMETHODIMP ExtPack::COMGETTER(Description)(BSTR *a_pbstrDescription)
1026{
1027 CheckComArgOutPointerValid(a_pbstrDescription);
1028
1029 AutoCaller autoCaller(this);
1030 HRESULT hrc = autoCaller.rc();
1031 if (SUCCEEDED(hrc))
1032 {
1033 Bstr str(m->Desc.strDescription);
1034 str.cloneTo(a_pbstrDescription);
1035 }
1036 return hrc;
1037}
1038
1039STDMETHODIMP ExtPack::COMGETTER(Version)(BSTR *a_pbstrVersion)
1040{
1041 CheckComArgOutPointerValid(a_pbstrVersion);
1042
1043 AutoCaller autoCaller(this);
1044 HRESULT hrc = autoCaller.rc();
1045 if (SUCCEEDED(hrc))
1046 {
1047 Bstr str(m->Desc.strVersion);
1048 str.cloneTo(a_pbstrVersion);
1049 }
1050 return hrc;
1051}
1052
1053STDMETHODIMP ExtPack::COMGETTER(Revision)(ULONG *a_puRevision)
1054{
1055 CheckComArgOutPointerValid(a_puRevision);
1056
1057 AutoCaller autoCaller(this);
1058 HRESULT hrc = autoCaller.rc();
1059 if (SUCCEEDED(hrc))
1060 *a_puRevision = m->Desc.uRevision;
1061 return hrc;
1062}
1063
1064STDMETHODIMP ExtPack::COMGETTER(VRDEModule)(BSTR *a_pbstrVrdeModule)
1065{
1066 CheckComArgOutPointerValid(a_pbstrVrdeModule);
1067
1068 AutoCaller autoCaller(this);
1069 HRESULT hrc = autoCaller.rc();
1070 if (SUCCEEDED(hrc))
1071 {
1072 Bstr str(m->Desc.strVrdeModule);
1073 str.cloneTo(a_pbstrVrdeModule);
1074 }
1075 return hrc;
1076}
1077
1078STDMETHODIMP ExtPack::COMGETTER(PlugIns)(ComSafeArrayOut(IExtPackPlugIn *, a_paPlugIns))
1079{
1080 /** @todo implement plug-ins. */
1081#ifdef VBOX_WITH_XPCOM
1082 NOREF(a_paPlugIns);
1083 NOREF(a_paPlugInsSize);
1084#endif
1085 ReturnComNotImplemented();
1086}
1087
1088STDMETHODIMP ExtPack::COMGETTER(Usable)(BOOL *a_pfUsable)
1089{
1090 CheckComArgOutPointerValid(a_pfUsable);
1091
1092 AutoCaller autoCaller(this);
1093 HRESULT hrc = autoCaller.rc();
1094 if (SUCCEEDED(hrc))
1095 *a_pfUsable = m->fUsable;
1096 return hrc;
1097}
1098
1099STDMETHODIMP ExtPack::COMGETTER(WhyUnusable)(BSTR *a_pbstrWhy)
1100{
1101 CheckComArgOutPointerValid(a_pbstrWhy);
1102
1103 AutoCaller autoCaller(this);
1104 HRESULT hrc = autoCaller.rc();
1105 if (SUCCEEDED(hrc))
1106 m->strWhyUnusable.cloneTo(a_pbstrWhy);
1107 return hrc;
1108}
1109
1110STDMETHODIMP ExtPack::QueryObject(IN_BSTR a_bstrObjectId, IUnknown **a_ppUnknown)
1111{
1112 com::Guid ObjectId;
1113 CheckComArgGuid(a_bstrObjectId, ObjectId);
1114 CheckComArgOutPointerValid(a_ppUnknown);
1115
1116 AutoCaller autoCaller(this);
1117 HRESULT hrc = autoCaller.rc();
1118 if (SUCCEEDED(hrc))
1119 {
1120 if ( m->pReg
1121 && m->pReg->pfnQueryObject)
1122 {
1123 void *pvUnknown = m->pReg->pfnQueryObject(m->pReg, ObjectId.raw());
1124 if (pvUnknown)
1125 *a_ppUnknown = (IUnknown *)pvUnknown;
1126 else
1127 hrc = E_NOINTERFACE;
1128 }
1129 else
1130 hrc = E_NOINTERFACE;
1131 }
1132 return hrc;
1133}
1134
1135
1136
1137
1138DEFINE_EMPTY_CTOR_DTOR(ExtPackManager)
1139
1140/**
1141 * Called by ComObjPtr::createObject when creating the object.
1142 *
1143 * Just initialize the basic object state, do the rest in init().
1144 *
1145 * @returns S_OK.
1146 */
1147HRESULT ExtPackManager::FinalConstruct()
1148{
1149 m = NULL;
1150 return S_OK;
1151}
1152
1153/**
1154 * Initializes the extension pack manager.
1155 *
1156 * @returns COM status code.
1157 * @param a_pVirtualBox Pointer to the VirtualBox object.
1158 * @param a_pszDropZoneDir The path to the drop zone directory.
1159 * @param a_fCheckDropZone Whether to check the drop zone for new
1160 * extensions or not. Only VBoxSVC does this
1161 * and then only when wanted.
1162 * @param a_enmContext The context we're in.
1163 */
1164HRESULT ExtPackManager::init(VirtualBox *a_pVirtualBox, const char *a_pszDropZoneDir, bool a_fCheckDropZone,
1165 VBOXEXTPACKCTX a_enmContext)
1166{
1167 AutoInitSpan autoInitSpan(this);
1168 AssertReturn(autoInitSpan.isOk(), E_FAIL);
1169
1170 /*
1171 * Figure some stuff out before creating the instance data.
1172 */
1173 char szBaseDir[RTPATH_MAX];
1174 int rc = RTPathAppPrivateArch(szBaseDir, sizeof(szBaseDir));
1175 AssertLogRelRCReturn(rc, E_FAIL);
1176 rc = RTPathAppend(szBaseDir, sizeof(szBaseDir), VBOX_EXTPACK_INSTALL_DIR);
1177 AssertLogRelRCReturn(rc, E_FAIL);
1178
1179 char szCertificatDir[RTPATH_MAX];
1180 rc = RTPathAppPrivateNoArch(szCertificatDir, sizeof(szCertificatDir));
1181 AssertLogRelRCReturn(rc, E_FAIL);
1182 rc = RTPathAppend(szCertificatDir, sizeof(szCertificatDir), VBOX_EXTPACK_CERT_DIR);
1183 AssertLogRelRCReturn(rc, E_FAIL);
1184
1185 /*
1186 * Allocate and initialize the instance data.
1187 */
1188 m = new Data;
1189 m->strBaseDir = szBaseDir;
1190 m->strCertificatDirPath = szCertificatDir;
1191 m->strDropZoneDir = a_pszDropZoneDir;
1192 m->pVirtualBox = a_pVirtualBox;
1193 m->enmContext = a_enmContext;
1194
1195 /*
1196 * Go looking for extensions. The RTDirOpen may fail if nothing has been
1197 * installed yet, or if root is paranoid and has revoked our access to them.
1198 *
1199 * We ASSUME that there are no files, directories or stuff in the directory
1200 * that exceed the max name length in RTDIRENTRYEX.
1201 */
1202 HRESULT hrc = S_OK;
1203 PRTDIR pDir;
1204 int vrc = RTDirOpen(&pDir, szBaseDir);
1205 if (RT_SUCCESS(vrc))
1206 {
1207 for (;;)
1208 {
1209 RTDIRENTRYEX Entry;
1210 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1211 if (RT_FAILURE(vrc))
1212 {
1213 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1214 break;
1215 }
1216 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
1217 && strcmp(Entry.szName, ".") != 0
1218 && strcmp(Entry.szName, "..") != 0
1219 && VBoxExtPackIsValidName(Entry.szName) )
1220 {
1221 /*
1222 * All directories are extensions, the shall be nothing but
1223 * extensions in this subdirectory.
1224 */
1225 ComObjPtr<ExtPack> NewExtPack;
1226 HRESULT hrc2 = NewExtPack.createObject();
1227 if (SUCCEEDED(hrc2))
1228 hrc2 = NewExtPack->init(a_enmContext, Entry.szName, szBaseDir);
1229 if (SUCCEEDED(hrc2))
1230 m->llInstalledExtPacks.push_back(NewExtPack);
1231 else if (SUCCEEDED(rc))
1232 hrc = hrc2;
1233 }
1234 }
1235 RTDirClose(pDir);
1236 }
1237 /* else: ignore, the directory probably does not exist or something. */
1238
1239 /*
1240 * Look for things in the drop zone.
1241 */
1242 if (SUCCEEDED(hrc) && a_fCheckDropZone)
1243 processDropZone();
1244
1245 if (SUCCEEDED(hrc))
1246 autoInitSpan.setSucceeded();
1247 return hrc;
1248}
1249
1250/**
1251 * COM cruft.
1252 */
1253void ExtPackManager::FinalRelease()
1254{
1255 uninit();
1256}
1257
1258/**
1259 * Do the actual cleanup.
1260 */
1261void ExtPackManager::uninit()
1262{
1263 /* Enclose the state transition Ready->InUninit->NotReady */
1264 AutoUninitSpan autoUninitSpan(this);
1265 if (!autoUninitSpan.uninitDone() && m != NULL)
1266 {
1267 delete m;
1268 m = NULL;
1269 }
1270}
1271
1272
1273STDMETHODIMP ExtPackManager::COMGETTER(InstalledExtPacks)(ComSafeArrayOut(IExtPack *, a_paExtPacks))
1274{
1275 CheckComArgSafeArrayNotNull(a_paExtPacks);
1276 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1277
1278 AutoCaller autoCaller(this);
1279 HRESULT hrc = autoCaller.rc();
1280 if (SUCCEEDED(hrc))
1281 {
1282 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1283
1284 SafeIfaceArray<IExtPack> SaExtPacks(m->llInstalledExtPacks);
1285 SaExtPacks.detachTo(ComSafeArrayOutArg(a_paExtPacks));
1286 }
1287
1288 return hrc;
1289}
1290
1291STDMETHODIMP ExtPackManager::Find(IN_BSTR a_bstrName, IExtPack **a_pExtPack)
1292{
1293 CheckComArgNotNull(a_bstrName);
1294 CheckComArgOutPointerValid(a_pExtPack);
1295 Utf8Str strName(a_bstrName);
1296 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1297
1298 AutoCaller autoCaller(this);
1299 HRESULT hrc = autoCaller.rc();
1300 if (SUCCEEDED(hrc))
1301 {
1302 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1303
1304 ComPtr<ExtPack> ptrExtPack = findExtPack(strName.c_str());
1305 if (!ptrExtPack.isNull())
1306 ptrExtPack.queryInterfaceTo(a_pExtPack);
1307 else
1308 hrc = VBOX_E_OBJECT_NOT_FOUND;
1309 }
1310
1311 return hrc;
1312}
1313
1314STDMETHODIMP ExtPackManager::Install(IN_BSTR a_bstrTarball, BSTR *a_pbstrName)
1315{
1316 CheckComArgNotNull(a_bstrTarball);
1317 CheckComArgOutPointerValid(a_pbstrName);
1318 Utf8Str strTarball(a_bstrTarball);
1319 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1320
1321 AutoCaller autoCaller(this);
1322 HRESULT hrc = autoCaller.rc();
1323 if (SUCCEEDED(hrc))
1324 {
1325 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1326
1327 /*
1328 * Check that the file exists and that we can access it.
1329 */
1330 if (RTFileExists(strTarball.c_str()))
1331 {
1332 RTFILE hFile;
1333 int vrc = RTFileOpen(&hFile, strTarball.c_str(),
1334 RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN | RTFILE_O_INHERIT);
1335 if (RT_SUCCESS(vrc))
1336 {
1337 RTFSOBJINFO ObjInfo;
1338 vrc = RTFileQueryInfo(hFile, &ObjInfo, RTFSOBJATTRADD_NOTHING);
1339 if ( RT_SUCCESS(vrc)
1340 && RTFS_IS_FILE(ObjInfo.Attr.fMode))
1341 {
1342 /*
1343 * Derive the name of the extension pack from the file
1344 * name. Certain restrictions are here placed on the
1345 * tarball name.
1346 */
1347 iprt::MiniString *pStrName = VBoxExtPackExtractNameFromTarballPath(strTarball.c_str());
1348 if (pStrName)
1349 {
1350 /*
1351 * Refresh the data we have on the extension pack as it
1352 * may be made stale by direct meddling or some other user.
1353 */
1354 ExtPack *pExtPack;
1355 hrc = refreshExtPack(pStrName->c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1356 if (SUCCEEDED(hrc) && !pExtPack)
1357 {
1358 /*
1359 * Run the set-uid-to-root binary that performs the actual
1360 * installation. Then create an object for the packet (we
1361 * do this even on failure, to be on the safe side).
1362 */
1363 char szTarballFd[64];
1364 RTStrPrintf(szTarballFd, sizeof(szTarballFd), "0x%RX64",
1365 (uint64_t)RTFileToNative(hFile));
1366
1367 hrc = runSetUidToRootHelper("install",
1368 "--base-dir", m->strBaseDir.c_str(),
1369 "--cert-dir", m->strCertificatDirPath.c_str(),
1370 "--name", pStrName->c_str(),
1371 "--tarball", strTarball.c_str(),
1372 "--tarball-fd", &szTarballFd[0],
1373 NULL);
1374 if (SUCCEEDED(hrc))
1375 {
1376 hrc = refreshExtPack(pStrName->c_str(), true /*a_fUnsuableIsError*/, &pExtPack);
1377 if (SUCCEEDED(hrc))
1378 {
1379 LogRel(("ExtPackManager: Successfully installed extension pack '%s'.\n", pStrName->c_str()));
1380 pExtPack->callInstalledHook(m->pVirtualBox, &autoLock);
1381 }
1382 }
1383 else
1384 {
1385 ErrorInfoKeeper Eik;
1386 refreshExtPack(pStrName->c_str(), false /*a_fUnsuableIsError*/, NULL);
1387 }
1388 }
1389 else if (SUCCEEDED(hrc))
1390 hrc = setError(E_FAIL,
1391 tr("Extension pack '%s' is already installed."
1392 " In case of a reinstallation, please uninstall it first"),
1393 pStrName->c_str());
1394 delete pStrName;
1395 }
1396 else
1397 hrc = setError(E_FAIL, tr("Malformed '%s' file name"), strTarball.c_str());
1398 }
1399 else if (RT_SUCCESS(vrc))
1400 hrc = setError(E_FAIL, tr("'%s' is not a regular file"), strTarball.c_str());
1401 else
1402 hrc = setError(E_FAIL, tr("Failed to query info on '%s' (%Rrc)"), strTarball.c_str(), vrc);
1403 RTFileClose(hFile);
1404 }
1405 else
1406 hrc = setError(E_FAIL, tr("Failed to open '%s' (%Rrc)"), strTarball.c_str(), vrc);
1407 }
1408 else if (RTPathExists(strTarball.c_str()))
1409 hrc = setError(E_FAIL, tr("'%s' is not a regular file"), strTarball.c_str());
1410 else
1411 hrc = setError(E_FAIL, tr("File '%s' was inaccessible or not found"), strTarball.c_str());
1412
1413 /*
1414 * Do VirtualBoxReady callbacks now for any freshly installed
1415 * extension pack (old ones will not be called).
1416 */
1417 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
1418 {
1419 autoLock.release();
1420 callAllVirtualBoxReadyHooks();
1421 }
1422 }
1423
1424 return hrc;
1425}
1426
1427STDMETHODIMP ExtPackManager::Uninstall(IN_BSTR a_bstrName, BOOL a_fForcedRemoval)
1428{
1429 CheckComArgNotNull(a_bstrName);
1430 Utf8Str strName(a_bstrName);
1431 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1432
1433 AutoCaller autoCaller(this);
1434 HRESULT hrc = autoCaller.rc();
1435 if (SUCCEEDED(hrc))
1436 {
1437 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1438
1439 /*
1440 * Refresh the data we have on the extension pack as it may be made
1441 * stale by direct meddling or some other user.
1442 */
1443 ExtPack *pExtPack;
1444 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1445 if (SUCCEEDED(hrc))
1446 {
1447 if (!pExtPack)
1448 {
1449 LogRel(("ExtPackManager: Extension pack '%s' is not installed, so nothing to uninstall.\n", strName.c_str()));
1450 hrc = S_OK; /* nothing to uninstall */
1451 }
1452 else
1453 {
1454 /*
1455 * Call the uninstall hook and unload the main dll.
1456 */
1457 hrc = pExtPack->callUninstallHookAndClose(m->pVirtualBox, a_fForcedRemoval != FALSE);
1458 if (SUCCEEDED(hrc))
1459 {
1460 /*
1461 * Run the set-uid-to-root binary that performs the
1462 * uninstallation. Then refresh the object.
1463 *
1464 * This refresh is theorically subject to races, but it's of
1465 * the don't-do-that variety.
1466 */
1467 const char *pszForcedOpt = a_fForcedRemoval ? "--forced" : NULL;
1468 hrc = runSetUidToRootHelper("uninstall",
1469 "--base-dir", m->strBaseDir.c_str(),
1470 "--name", strName.c_str(),
1471 pszForcedOpt, /* Last as it may be NULL. */
1472 NULL);
1473 if (SUCCEEDED(hrc))
1474 {
1475 hrc = refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, &pExtPack);
1476 if (SUCCEEDED(hrc))
1477 {
1478 if (!pExtPack)
1479 LogRel(("ExtPackManager: Successfully uninstalled extension pack '%s'.\n", strName.c_str()));
1480 else
1481 hrc = setError(E_FAIL,
1482 tr("Uninstall extension pack '%s' failed under mysterious circumstances"),
1483 strName.c_str());
1484 }
1485 }
1486 else
1487 {
1488 ErrorInfoKeeper Eik;
1489 refreshExtPack(strName.c_str(), false /*a_fUnsuableIsError*/, NULL);
1490 }
1491 }
1492 }
1493 }
1494
1495 /*
1496 * Do VirtualBoxReady callbacks now for any freshly installed
1497 * extension pack (old ones will not be called).
1498 */
1499 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
1500 {
1501 autoLock.release();
1502 callAllVirtualBoxReadyHooks();
1503 }
1504 }
1505
1506 return hrc;
1507}
1508
1509STDMETHODIMP ExtPackManager::Cleanup(void)
1510{
1511 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1512
1513 AutoCaller autoCaller(this);
1514 HRESULT hrc = autoCaller.rc();
1515 if (SUCCEEDED(hrc))
1516 {
1517 /*
1518 * Run the set-uid-to-root binary that performs the cleanup.
1519 *
1520 * Take the write lock to prevent conflicts with other calls to this
1521 * VBoxSVC instance.
1522 */
1523 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1524 hrc = runSetUidToRootHelper("cleanup",
1525 "--base-dir", m->strBaseDir.c_str(),
1526 NULL);
1527 }
1528
1529 return hrc;
1530}
1531
1532STDMETHODIMP ExtPackManager::QueryAllPlugInsForFrontend(IN_BSTR a_bstrFrontend, ComSafeArrayOut(BSTR, a_pabstrPlugInModules))
1533{
1534 CheckComArgNotNull(a_bstrFrontend);
1535 Utf8Str strName(a_bstrFrontend);
1536 CheckComArgOutSafeArrayPointerValid(a_pabstrPlugInModules);
1537 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
1538
1539 AutoCaller autoCaller(this);
1540 HRESULT hrc = autoCaller.rc();
1541 if (SUCCEEDED(hrc))
1542 {
1543 com::SafeArray<BSTR> saPaths((size_t)0);
1544 /** @todo implement plug-ins */
1545 saPaths.detachTo(ComSafeArrayOutArg(a_pabstrPlugInModules));
1546 }
1547 return hrc;
1548}
1549
1550
1551/**
1552 * Runs the helper application that does the privileged operations.
1553 *
1554 * @returns S_OK or a failure status with error information set.
1555 * @param a_pszCommand The command to execute.
1556 * @param ... The argument strings that goes along with the
1557 * command. Maximum is about 16. Terminated by a
1558 * NULL.
1559 */
1560HRESULT ExtPackManager::runSetUidToRootHelper(const char *a_pszCommand, ...)
1561{
1562 /*
1563 * Calculate the path to the helper application.
1564 */
1565 char szExecName[RTPATH_MAX];
1566 int vrc = RTPathAppPrivateArch(szExecName, sizeof(szExecName));
1567 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1568
1569 vrc = RTPathAppend(szExecName, sizeof(szExecName), VBOX_EXTPACK_HELPER_NAME);
1570 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1571
1572 /*
1573 * Convert the variable argument list to a RTProcCreate argument vector.
1574 */
1575 const char *apszArgs[20];
1576 unsigned cArgs = 0;
1577 apszArgs[cArgs++] = &szExecName[0];
1578 apszArgs[cArgs++] = a_pszCommand;
1579
1580 va_list va;
1581 va_start(va, a_pszCommand);
1582 const char *pszLastArg;
1583 LogRel(("ExtPack: Executing '%s'", szExecName));
1584 do
1585 {
1586 LogRel((" '%s'", apszArgs[cArgs - 1]));
1587 AssertReturn(cArgs < RT_ELEMENTS(apszArgs) - 1, E_UNEXPECTED);
1588 pszLastArg = va_arg(va, const char *);
1589 apszArgs[cArgs++] = pszLastArg;
1590 } while (pszLastArg != NULL);
1591 LogRel(("\n"));
1592 va_end(va);
1593 apszArgs[cArgs] = NULL;
1594
1595 /*
1596 * Create a PIPE which we attach to stderr so that we can read the error
1597 * message on failure and report it back to the caller.
1598 */
1599 RTPIPE hPipeR;
1600 RTHANDLE hStdErrPipe;
1601 hStdErrPipe.enmType = RTHANDLETYPE_PIPE;
1602 vrc = RTPipeCreate(&hPipeR, &hStdErrPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
1603 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
1604
1605 /*
1606 * Spawn the process.
1607 */
1608 HRESULT hrc;
1609 RTPROCESS hProcess;
1610 vrc = RTProcCreateEx(szExecName,
1611 apszArgs,
1612 RTENV_DEFAULT,
1613 0 /*fFlags*/,
1614 NULL /*phStdIn*/,
1615 NULL /*phStdOut*/,
1616 &hStdErrPipe,
1617 NULL /*pszAsUser*/,
1618 NULL /*pszPassword*/,
1619 &hProcess);
1620 if (RT_SUCCESS(vrc))
1621 {
1622 vrc = RTPipeClose(hStdErrPipe.u.hPipe);
1623 hStdErrPipe.u.hPipe = NIL_RTPIPE;
1624
1625 /*
1626 * Read the pipe output until the process completes.
1627 */
1628 RTPROCSTATUS ProcStatus = { -42, RTPROCEXITREASON_ABEND };
1629 size_t cbStdErrBuf = 0;
1630 size_t offStdErrBuf = 0;
1631 char *pszStdErrBuf = NULL;
1632 do
1633 {
1634 /*
1635 * Service the pipe. Block waiting for output or the pipe breaking
1636 * when the process terminates.
1637 */
1638 if (hPipeR != NIL_RTPIPE)
1639 {
1640 char achBuf[16]; ///@todo 1024
1641 size_t cbRead;
1642 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
1643 if (RT_SUCCESS(vrc))
1644 {
1645 /* grow the buffer? */
1646 size_t cbBufReq = offStdErrBuf + cbRead + 1;
1647 if ( cbBufReq > cbStdErrBuf
1648 && cbBufReq < _256K)
1649 {
1650 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
1651 void *pvNew = RTMemRealloc(pszStdErrBuf, cbNew);
1652 if (pvNew)
1653 {
1654 pszStdErrBuf = (char *)pvNew;
1655 cbStdErrBuf = cbNew;
1656 }
1657 }
1658
1659 /* append if we've got room. */
1660 if (cbBufReq <= cbStdErrBuf)
1661 {
1662 memcpy(&pszStdErrBuf[offStdErrBuf], achBuf, cbRead);
1663 offStdErrBuf = offStdErrBuf + cbRead;
1664 pszStdErrBuf[offStdErrBuf] = '\0';
1665 }
1666 }
1667 else
1668 {
1669 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
1670 RTPipeClose(hPipeR);
1671 hPipeR = NIL_RTPIPE;
1672 }
1673 }
1674
1675 /*
1676 * Service the process. Block if we have no pipe.
1677 */
1678 if (hProcess != NIL_RTPROCESS)
1679 {
1680 vrc = RTProcWait(hProcess,
1681 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
1682 &ProcStatus);
1683 if (RT_SUCCESS(vrc))
1684 hProcess = NIL_RTPROCESS;
1685 else
1686 AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProcess = NIL_RTPROCESS);
1687 }
1688 } while ( hPipeR != NIL_RTPIPE
1689 || hProcess != NIL_RTPROCESS);
1690
1691 /*
1692 * Compose the status code and, on failure, error message.
1693 */
1694 LogRel(("ExtPack: enmReason=%d iStatus=%d stderr='%s'\n",
1695 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : ""));
1696
1697 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
1698 && ProcStatus.iStatus == 0
1699 && offStdErrBuf == 0)
1700 hrc = S_OK;
1701 else if (ProcStatus.enmReason == RTPROCEXITREASON_NORMAL)
1702 {
1703 AssertMsg(ProcStatus.iStatus != 0, ("%s\n", pszStdErrBuf));
1704 hrc = setError(E_FAIL, tr("The installer failed with exit code %d: %s"),
1705 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1706 }
1707 else if (ProcStatus.enmReason == RTPROCEXITREASON_SIGNAL)
1708 hrc = setError(E_UNEXPECTED, tr("The installer was killed by signal #d (stderr: %s)"),
1709 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1710 else if (ProcStatus.enmReason == RTPROCEXITREASON_ABEND)
1711 hrc = setError(E_UNEXPECTED, tr("The installer aborted abnormally (stderr: %s)"),
1712 offStdErrBuf ? pszStdErrBuf : "");
1713 else
1714 hrc = setError(E_UNEXPECTED, tr("internal error: enmReason=%d iStatus=%d stderr='%s'"),
1715 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
1716
1717 RTMemFree(pszStdErrBuf);
1718 }
1719 else
1720 hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to launch the helper application '%s' (%Rrc)"), szExecName, vrc);
1721
1722 RTPipeClose(hPipeR);
1723 RTPipeClose(hStdErrPipe.u.hPipe);
1724
1725 return hrc;
1726}
1727
1728/**
1729 * Finds an installed extension pack.
1730 *
1731 * @returns Pointer to the extension pack if found, NULL if not. (No reference
1732 * counting problem here since the caller must be holding the lock.)
1733 * @param a_pszName The name of the extension pack.
1734 */
1735ExtPack *ExtPackManager::findExtPack(const char *a_pszName)
1736{
1737 size_t cchName = strlen(a_pszName);
1738
1739 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1740 it != m->llInstalledExtPacks.end();
1741 it++)
1742 {
1743 ExtPack::Data *pExtPackData = (*it)->m;
1744 if ( pExtPackData
1745 && pExtPackData->Desc.strName.length() == cchName
1746 && pExtPackData->Desc.strName.compare(a_pszName, iprt::MiniString::CaseInsensitive) == 0)
1747 return (*it);
1748 }
1749 return NULL;
1750}
1751
1752/**
1753 * Removes an installed extension pack from the internal list.
1754 *
1755 * The package is expected to exist!
1756 *
1757 * @param a_pszName The name of the extension pack.
1758 */
1759void ExtPackManager::removeExtPack(const char *a_pszName)
1760{
1761 size_t cchName = strlen(a_pszName);
1762
1763 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
1764 it != m->llInstalledExtPacks.end();
1765 it++)
1766 {
1767 ExtPack::Data *pExtPackData = (*it)->m;
1768 if ( pExtPackData
1769 && pExtPackData->Desc.strName.length() == cchName
1770 && pExtPackData->Desc.strName.compare(a_pszName, iprt::MiniString::CaseInsensitive) == 0)
1771 {
1772 m->llInstalledExtPacks.erase(it);
1773 return;
1774 }
1775 }
1776 AssertMsgFailed(("%s\n", a_pszName));
1777}
1778
1779/**
1780 * Refreshes the specified extension pack.
1781 *
1782 * This may remove the extension pack from the list, so any non-smart pointers
1783 * to the extension pack object may become invalid.
1784 *
1785 * @returns S_OK and *ppExtPack on success, COM status code and error message
1786 * on failure.
1787 *
1788 * @param a_pszName The extension to update..
1789 * @param a_fUnsuableIsError If @c true, report an unusable extension pack
1790 * as an error.
1791 * @param a_ppExtPack Where to store the pointer to the extension
1792 * pack of it is still around after the refresh.
1793 * This is optional.
1794 *
1795 * @remarks Caller holds the extension manager lock.
1796 * @remarks Only called in VBoxSVC.
1797 */
1798HRESULT ExtPackManager::refreshExtPack(const char *a_pszName, bool a_fUnsuableIsError, ExtPack **a_ppExtPack)
1799{
1800 Assert(m->pVirtualBox != NULL); /* Only called from VBoxSVC. */
1801
1802 HRESULT hrc;
1803 ExtPack *pExtPack = findExtPack(a_pszName);
1804 if (pExtPack)
1805 {
1806 /*
1807 * Refresh existing object.
1808 */
1809 bool fCanDelete;
1810 hrc = pExtPack->refresh(&fCanDelete);
1811 if (SUCCEEDED(hrc))
1812 {
1813 if (fCanDelete)
1814 {
1815 removeExtPack(a_pszName);
1816 pExtPack = NULL;
1817 }
1818 }
1819 }
1820 else
1821 {
1822 /*
1823 * Does the dir exist? Make some special effort to deal with case
1824 * sensitivie file systems (a_pszName is case insensitive).
1825 */
1826 char szDir[RTPATH_MAX];
1827 int vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), a_pszName);
1828 AssertLogRelRCReturn(vrc, E_FAIL);
1829
1830 RTDIRENTRYEX Entry;
1831 RTFSOBJINFO ObjInfo;
1832 vrc = RTPathQueryInfoEx(szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1833 bool fExists = RT_SUCCESS(vrc) && RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode);
1834 if (!fExists)
1835 {
1836 PRTDIR pDir;
1837 vrc = RTDirOpen(&pDir, m->strBaseDir.c_str());
1838 if (RT_SUCCESS(vrc))
1839 {
1840 for (;;)
1841 {
1842 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1843 if (RT_FAILURE(vrc))
1844 {
1845 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1846 break;
1847 }
1848 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
1849 && !RTStrICmp(Entry.szName, a_pszName))
1850 {
1851 /*
1852 * The installed extension pack has a uses different case.
1853 * Update the name and directory variables.
1854 */
1855 vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), Entry.szName); /* not really necessary */
1856 AssertLogRelRCReturnStmt(vrc, E_UNEXPECTED, RTDirClose(pDir));
1857 a_pszName = Entry.szName;
1858 fExists = true;
1859 break;
1860 }
1861 }
1862 RTDirClose(pDir);
1863 }
1864 }
1865 if (fExists)
1866 {
1867 /*
1868 * We've got something, create a new extension pack object for it.
1869 */
1870 ComObjPtr<ExtPack> NewExtPack;
1871 hrc = NewExtPack.createObject();
1872 if (SUCCEEDED(hrc))
1873 hrc = NewExtPack->init(m->enmContext, a_pszName, m->strBaseDir.c_str());
1874 if (SUCCEEDED(hrc))
1875 {
1876 m->llInstalledExtPacks.push_back(NewExtPack);
1877 if (NewExtPack->m->fUsable)
1878 LogRel(("ExtPackManager: Found extension pack '%s'.\n", a_pszName));
1879 else
1880 LogRel(("ExtPackManager: Found bad extension pack '%s': %s\n",
1881 a_pszName, NewExtPack->m->strWhyUnusable.c_str() ));
1882 pExtPack = NewExtPack;
1883 }
1884 }
1885 else
1886 hrc = S_OK;
1887 }
1888
1889 /*
1890 * Report error if not usable, if that is desired.
1891 */
1892 if ( SUCCEEDED(hrc)
1893 && pExtPack
1894 && a_fUnsuableIsError
1895 && !pExtPack->m->fUsable)
1896 hrc = setError(E_FAIL, "%s", pExtPack->m->strWhyUnusable.c_str());
1897
1898 if (a_ppExtPack)
1899 *a_ppExtPack = pExtPack;
1900 return hrc;
1901}
1902
1903
1904/**
1905 * Processes anything new in the drop zone.
1906 */
1907void ExtPackManager::processDropZone(void)
1908{
1909 AutoCaller autoCaller(this);
1910 HRESULT hrc = autoCaller.rc();
1911 if (FAILED(hrc))
1912 return;
1913 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
1914
1915 if (m->strDropZoneDir.isEmpty())
1916 return;
1917
1918 PRTDIR pDir;
1919 int vrc = RTDirOpen(&pDir, m->strDropZoneDir.c_str());
1920 if (RT_FAILURE(vrc))
1921 return;
1922 for (;;)
1923 {
1924 RTDIRENTRYEX Entry;
1925 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1926 if (RT_FAILURE(vrc))
1927 {
1928 AssertMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1929 break;
1930 }
1931
1932 /*
1933 * We're looking for files with the right extension. Symbolic links
1934 * will be ignored.
1935 */
1936 if ( RTFS_IS_FILE(Entry.Info.Attr.fMode)
1937 && RTStrICmp(RTPathExt(Entry.szName), VBOX_EXTPACK_SUFFIX) == 0)
1938 {
1939 /* We create (and check for) a blocker file to prevent this
1940 extension pack from being installed more than once. */
1941 char szPath[RTPATH_MAX];
1942 vrc = RTPathJoin(szPath, sizeof(szPath), m->strDropZoneDir.c_str(), Entry.szName);
1943 if (RT_SUCCESS(vrc))
1944 vrc = RTPathAppend(szPath, sizeof(szPath), "-done");
1945 AssertRC(vrc);
1946 if (RT_SUCCESS(vrc))
1947 {
1948 RTFILE hFile;
1949 vrc = RTFileOpen(&hFile, szPath, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE);
1950 if (RT_SUCCESS(vrc))
1951 {
1952 /* Construct the full path to the extension pack and invoke
1953 the Install method to install it. Write errors to the
1954 done file. */
1955 vrc = RTPathJoin(szPath, sizeof(szPath), m->strDropZoneDir.c_str(), Entry.szName); AssertRC(vrc);
1956 Bstr strName;
1957 hrc = Install(Bstr(szPath).raw(), strName.asOutParam());
1958 if (SUCCEEDED(hrc))
1959 RTFileWrite(hFile, "succeeded\n", sizeof("succeeded\n"), NULL);
1960 else
1961 {
1962 Utf8Str strErr;
1963 com::ErrorInfo Info;
1964 if (Info.isFullAvailable())
1965 strErr.printf("failed\n"
1966 "%ls\n"
1967 "Details: code %Rhrc (%#RX32), component %ls, interface %ls, callee %ls\n"
1968 ,
1969 Info.getText().raw(),
1970 Info.getResultCode(),
1971 Info.getResultCode(),
1972 Info.getComponent().raw(),
1973 Info.getInterfaceName().raw(),
1974 Info.getCalleeName().raw());
1975 else
1976 strErr.printf("failed\n"
1977 "hrc=%Rhrc (%#RX32)\n", hrc, hrc);
1978 RTFileWrite(hFile, strErr.c_str(), strErr.length(), NULL);
1979 }
1980 RTFileClose(hFile);
1981 }
1982 }
1983 }
1984 } /* foreach dir entry */
1985 RTDirClose(pDir);
1986}
1987
1988
1989/**
1990 * Calls the pfnVirtualBoxReady hook for all working extension packs.
1991 *
1992 * @remarks The caller must not hold any locks.
1993 */
1994void ExtPackManager::callAllVirtualBoxReadyHooks(void)
1995{
1996 AutoCaller autoCaller(this);
1997 HRESULT hrc = autoCaller.rc();
1998 if (FAILED(hrc))
1999 return;
2000 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2001 ComPtr<ExtPackManager> ptrSelfRef = this;
2002
2003 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2004 it != m->llInstalledExtPacks.end();
2005 /* advancing below */)
2006 {
2007 if ((*it)->callVirtualBoxReadyHook(m->pVirtualBox, &autoLock))
2008 it = m->llInstalledExtPacks.begin();
2009 else
2010 it++;
2011 }
2012}
2013
2014/**
2015 * Calls the pfnConsoleReady hook for all working extension packs.
2016 *
2017 * @param a_pConsole The console interface.
2018 * @remarks The caller must not hold any locks.
2019 */
2020void ExtPackManager::callAllConsoleReadyHooks(IConsole *a_pConsole)
2021{
2022 AutoCaller autoCaller(this);
2023 HRESULT hrc = autoCaller.rc();
2024 if (FAILED(hrc))
2025 return;
2026 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2027 ComPtr<ExtPackManager> ptrSelfRef = this;
2028
2029 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2030 it != m->llInstalledExtPacks.end();
2031 /* advancing below */)
2032 {
2033 if ((*it)->callConsoleReadyHook(a_pConsole, &autoLock))
2034 it = m->llInstalledExtPacks.begin();
2035 else
2036 it++;
2037 }
2038}
2039
2040/**
2041 * Calls the pfnVMCreated hook for all working extension packs.
2042 *
2043 * @param a_pMachine The machine interface of the new VM.
2044 */
2045void ExtPackManager::callAllVmCreatedHooks(IMachine *a_pMachine)
2046{
2047 AutoCaller autoCaller(this);
2048 HRESULT hrc = autoCaller.rc();
2049 if (FAILED(hrc))
2050 return;
2051 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2052 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2053 ExtPackList llExtPacks = m->llInstalledExtPacks;
2054
2055 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2056 (*it)->callVmCreatedHook(m->pVirtualBox, a_pMachine, &autoLock);
2057}
2058
2059/**
2060 * Calls the pfnVMConfigureVMM hook for all working extension packs.
2061 *
2062 * @returns VBox status code. Stops on the first failure, expecting the caller
2063 * to signal this to the caller of the CFGM constructor.
2064 * @param a_pConsole The console interface for the VM.
2065 * @param a_pVM The VM handle.
2066 */
2067int ExtPackManager::callAllVmConfigureVmmHooks(IConsole *a_pConsole, PVM a_pVM)
2068{
2069 AutoCaller autoCaller(this);
2070 HRESULT hrc = autoCaller.rc();
2071 if (FAILED(hrc))
2072 return Global::vboxStatusCodeFromCOM(hrc);
2073 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2074 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2075 ExtPackList llExtPacks = m->llInstalledExtPacks;
2076
2077 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2078 {
2079 int vrc;
2080 (*it)->callVmConfigureVmmHook(a_pConsole, a_pVM, &autoLock, &vrc);
2081 if (RT_FAILURE(vrc))
2082 return vrc;
2083 }
2084
2085 return VINF_SUCCESS;
2086}
2087
2088/**
2089 * Calls the pfnVMPowerOn hook for all working extension packs.
2090 *
2091 * @returns VBox status code. Stops on the first failure, expecting the caller
2092 * to not power on the VM.
2093 * @param a_pConsole The console interface for the VM.
2094 * @param a_pVM The VM handle.
2095 */
2096int ExtPackManager::callAllVmPowerOnHooks(IConsole *a_pConsole, PVM a_pVM)
2097{
2098 AutoCaller autoCaller(this);
2099 HRESULT hrc = autoCaller.rc();
2100 if (FAILED(hrc))
2101 return Global::vboxStatusCodeFromCOM(hrc);
2102 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2103 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2104 ExtPackList llExtPacks = m->llInstalledExtPacks;
2105
2106 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2107 {
2108 int vrc;
2109 (*it)->callVmPowerOnHook(a_pConsole, a_pVM, &autoLock, &vrc);
2110 if (RT_FAILURE(vrc))
2111 return vrc;
2112 }
2113
2114 return VINF_SUCCESS;
2115}
2116
2117/**
2118 * Calls the pfnVMPowerOff hook for all working extension packs.
2119 *
2120 * @param a_pConsole The console interface for the VM.
2121 * @param a_pVM The VM handle. Can be NULL.
2122 */
2123void ExtPackManager::callAllVmPowerOffHooks(IConsole *a_pConsole, PVM a_pVM)
2124{
2125 AutoCaller autoCaller(this);
2126 HRESULT hrc = autoCaller.rc();
2127 if (FAILED(hrc))
2128 return;
2129 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2130 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2131 ExtPackList llExtPacks = m->llInstalledExtPacks;
2132
2133 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); it++)
2134 (*it)->callVmPowerOffHook(a_pConsole, a_pVM, &autoLock);
2135}
2136
2137
2138/**
2139 * Checks that the specified extension pack contains a VRDE module and that it
2140 * is shipshape.
2141 *
2142 * @returns S_OK if ok, appropriate failure status code with details.
2143 * @param a_pstrExtPack The name of the extension pack.
2144 */
2145HRESULT ExtPackManager::checkVrdeExtPack(Utf8Str const *a_pstrExtPack)
2146{
2147 AutoCaller autoCaller(this);
2148 HRESULT hrc = autoCaller.rc();
2149 if (SUCCEEDED(hrc))
2150 {
2151 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2152
2153 ExtPack *pExtPack = findExtPack(a_pstrExtPack->c_str());
2154 if (pExtPack)
2155 hrc = pExtPack->checkVrde();
2156 else
2157 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
2158 }
2159
2160 return hrc;
2161}
2162
2163/**
2164 * Gets the full path to the VRDE library of the specified extension pack.
2165 *
2166 * This will do extacly the same as checkVrdeExtPack and then resolve the
2167 * library path.
2168 *
2169 * @returns S_OK if a path is returned, COM error status and message return if
2170 * not.
2171 * @param a_pstrExtPack The extension pack.
2172 * @param a_pstrVrdeLibrary Where to return the path.
2173 */
2174int ExtPackManager::getVrdeLibraryPathForExtPack(Utf8Str const *a_pstrExtPack, Utf8Str *a_pstrVrdeLibrary)
2175{
2176 AutoCaller autoCaller(this);
2177 HRESULT hrc = autoCaller.rc();
2178 if (SUCCEEDED(hrc))
2179 {
2180 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2181
2182 ExtPack *pExtPack = findExtPack(a_pstrExtPack->c_str());
2183 if (pExtPack)
2184 hrc = pExtPack->getVrdpLibraryName(a_pstrVrdeLibrary);
2185 else
2186 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
2187 }
2188
2189 return hrc;
2190}
2191
2192/**
2193 * Gets the name of the default VRDE extension pack.
2194 *
2195 * @returns S_OK or some COM error status on red tape failure.
2196 * @param a_pstrExtPack Where to return the extension pack name. Returns
2197 * empty if no extension pack wishes to be the default
2198 * VRDP provider.
2199 */
2200HRESULT ExtPackManager::getDefaultVrdeExtPack(Utf8Str *a_pstrExtPack)
2201{
2202 a_pstrExtPack->setNull();
2203
2204 AutoCaller autoCaller(this);
2205 HRESULT hrc = autoCaller.rc();
2206 if (SUCCEEDED(hrc))
2207 {
2208 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2209
2210 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2211 it != m->llInstalledExtPacks.end();
2212 it++)
2213 {
2214 if ((*it)->wantsToBeDefaultVrde())
2215 {
2216 *a_pstrExtPack = (*it)->m->Desc.strName;
2217 break;
2218 }
2219 }
2220 }
2221 return hrc;
2222}
2223
2224/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette