VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/ExtPackManagerImpl.cpp@ 59339

Last change on this file since 59339 was 59339, checked in by vboxsync, 9 years ago

ExtPackManagerImpl.cpp: Turns out there are more snaufs in the USB fixes and optimizations, the latest in 4.3.30. sigh.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 108.8 KB
Line 
1/* $Id: ExtPackManagerImpl.cpp 59339 2016-01-14 11:49:12Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for Extension Packs, VBoxSVC & VBoxC.
4 */
5
6/*
7 * Copyright (C) 2010-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#include "ThreadTask.h"
25
26#include <iprt/buildconfig.h>
27#include <iprt/ctype.h>
28#include <iprt/dir.h>
29#include <iprt/env.h>
30#include <iprt/file.h>
31#include <iprt/ldr.h>
32#include <iprt/manifest.h>
33#include <iprt/param.h>
34#include <iprt/path.h>
35#include <iprt/pipe.h>
36#include <iprt/process.h>
37#include <iprt/string.h>
38
39#include <VBox/com/array.h>
40#include <VBox/com/ErrorInfo.h>
41#include <VBox/err.h>
42#include <VBox/log.h>
43#include <VBox/sup.h>
44#include <VBox/version.h>
45#include "AutoCaller.h"
46#include "Global.h"
47#include "ProgressImpl.h"
48#if defined(VBOX_COM_INPROC)
49# include "ConsoleImpl.h"
50#else
51# include "VirtualBoxImpl.h"
52#endif
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58/** @def VBOX_EXTPACK_HELPER_NAME
59 * The name of the utility application we employ to install and uninstall the
60 * extension packs. This is a set-uid-to-root binary on unixy platforms, which
61 * is why it has to be a separate application.
62 */
63#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
64# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelperApp.exe"
65#else
66# define VBOX_EXTPACK_HELPER_NAME "VBoxExtPackHelperApp"
67#endif
68
69
70/*********************************************************************************************************************************
71* Structures and Typedefs *
72*********************************************************************************************************************************/
73struct ExtPackBaseData
74{
75public:
76 /** The extension pack descriptor (loaded from the XML, mostly). */
77 VBOXEXTPACKDESC Desc;
78 /** The file system object info of the XML file.
79 * This is for detecting changes and save time in refresh(). */
80 RTFSOBJINFO ObjInfoDesc;
81 /** Whether it's usable or not. */
82 bool fUsable;
83 /** Why it is unusable. */
84 Utf8Str strWhyUnusable;
85};
86
87#if !defined(VBOX_COM_INPROC)
88/**
89 * Private extension pack data.
90 */
91struct ExtPackFile::Data : public ExtPackBaseData
92{
93public:
94 /** The path to the tarball. */
95 Utf8Str strExtPackFile;
96 /** The SHA-256 hash of the file (as string). */
97 Utf8Str strDigest;
98 /** The file handle of the extension pack file. */
99 RTFILE hExtPackFile;
100 /** Our manifest for the tarball. */
101 RTMANIFEST hOurManifest;
102 /** Pointer to the extension pack manager. */
103 ComObjPtr<ExtPackManager> ptrExtPackMgr;
104 /** Pointer to the VirtualBox object so we can create a progress object. */
105 VirtualBox *pVirtualBox;
106
107 RTMEMEF_NEW_AND_DELETE_OPERATORS();
108};
109#endif
110
111/**
112 * Private extension pack data.
113 */
114struct ExtPack::Data : public ExtPackBaseData
115{
116public:
117 /** Where the extension pack is located. */
118 Utf8Str strExtPackPath;
119 /** The file system object info of the extension pack directory.
120 * This is for detecting changes and save time in refresh(). */
121 RTFSOBJINFO ObjInfoExtPack;
122 /** The full path to the main module. */
123 Utf8Str strMainModPath;
124 /** The file system object info of the main module.
125 * This is used to determin whether to bother try reload it. */
126 RTFSOBJINFO ObjInfoMainMod;
127 /** The module handle of the main extension pack module. */
128 RTLDRMOD hMainMod;
129
130 /** The helper callbacks for the extension pack. */
131 VBOXEXTPACKHLP Hlp;
132 /** Pointer back to the extension pack object (for Hlp methods). */
133 ExtPack *pThis;
134 /** The extension pack registration structure. */
135 PCVBOXEXTPACKREG pReg;
136 /** The current context. */
137 VBOXEXTPACKCTX enmContext;
138 /** Set if we've made the pfnVirtualBoxReady or pfnConsoleReady call. */
139 bool fMadeReadyCall;
140
141 RTMEMEF_NEW_AND_DELETE_OPERATORS();
142};
143
144/** List of extension packs. */
145typedef std::list< ComObjPtr<ExtPack> > ExtPackList;
146
147/**
148 * Private extension pack manager data.
149 */
150struct ExtPackManager::Data
151{
152 /** The directory where the extension packs are installed. */
153 Utf8Str strBaseDir;
154 /** The directory where the certificates this installation recognizes are
155 * stored. */
156 Utf8Str strCertificatDirPath;
157 /** The list of installed extension packs. */
158 ExtPackList llInstalledExtPacks;
159#if !defined(VBOX_COM_INPROC)
160 /** Pointer to the VirtualBox object, our parent. */
161 VirtualBox *pVirtualBox;
162#endif
163 /** The current context. */
164 VBOXEXTPACKCTX enmContext;
165#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_DARWIN)
166 /** File handle for the VBoxVMM libary which we slurp because ExtPacks depend on it. */
167 RTLDRMOD hVBoxVMM;
168#endif
169
170 RTMEMEF_NEW_AND_DELETE_OPERATORS();
171};
172
173#if !defined(VBOX_COM_INPROC)
174
175/**
176 * Extension pack installation job.
177 */
178class ExtPackInstallTask : public ThreadTask
179{
180public:
181 explicit ExtPackInstallTask() : ThreadTask("ExtPackInst") { }
182 ~ExtPackInstallTask() { }
183
184 void handler()
185 {
186 HRESULT hrc = ptrExtPackMgr->i_doInstall(ptrExtPackFile, fReplace, &strDisplayInfo);
187 ptrProgress->i_notifyComplete(hrc);
188 }
189
190 HRESULT Init(const ComPtr<ExtPackFile> &a_strExtPackFile, bool a_fReplace,
191 const Utf8Str &strDispInfo, const ComPtr<ExtPackManager> &a_ptrExtPackMgr)
192 {
193 ptrExtPackFile = a_strExtPackFile;
194 fReplace = a_fReplace;
195 strDisplayInfo = strDispInfo;
196 ptrExtPackMgr = a_ptrExtPackMgr;
197
198 HRESULT hrc = ptrProgress.createObject();
199 if (SUCCEEDED(hrc))
200 {
201 Bstr bstrDescription("Installing extension pack");
202 hrc = ptrProgress->init(ptrExtPackFile->m->pVirtualBox,
203 static_cast<IExtPackFile *>(ptrExtPackFile),
204 bstrDescription.raw(),
205 FALSE /*aCancelable*/);
206 }
207
208 return hrc;
209 }
210
211 /** Smart pointer to the progress object for this job. */
212 ComObjPtr<Progress> ptrProgress;
213private:
214 /** Smart pointer to the extension pack file. */
215 ComPtr<ExtPackFile> ptrExtPackFile;
216 /** The replace argument. */
217 bool fReplace;
218 /** The display info argument. */
219 Utf8Str strDisplayInfo;
220 /** Smart pointer to the extension manager. */
221 ComPtr<ExtPackManager> ptrExtPackMgr;
222};
223
224/**
225 * Extension pack uninstallation job.
226 */
227class ExtPackUninstallTask : public ThreadTask
228{
229public:
230 explicit ExtPackUninstallTask() : ThreadTask("ExtPackUninst") { }
231 ~ExtPackUninstallTask() { }
232
233 void handler()
234 {
235 HRESULT hrc = ptrExtPackMgr->i_doUninstall(&strName, fForcedRemoval, &strDisplayInfo);
236 ptrProgress->i_notifyComplete(hrc);
237 }
238
239 HRESULT Init(const ComPtr<ExtPackManager> &a_ptrExtPackMgr, const Utf8Str &a_strName,
240 bool a_fForcedRemoval, const Utf8Str &a_strDisplayInfo)
241 {
242 ptrExtPackMgr = a_ptrExtPackMgr;
243 strName = a_strName;
244 fForcedRemoval = a_fForcedRemoval;
245 strDisplayInfo = a_strDisplayInfo;
246
247 HRESULT hrc = ptrProgress.createObject();
248 if (SUCCEEDED(hrc))
249 {
250 Bstr bstrDescription("Uninstalling extension pack");
251 hrc = ptrProgress->init(ptrExtPackMgr->m->pVirtualBox,
252 static_cast<IExtPackManager *>(ptrExtPackMgr),
253 bstrDescription.raw(),
254 FALSE /*aCancelable*/);
255 }
256
257 return hrc;
258 }
259
260 /** Smart pointer to the progress object for this job. */
261 ComObjPtr<Progress> ptrProgress;
262private:
263 /** Smart pointer to the extension manager. */
264 ComPtr<ExtPackManager> ptrExtPackMgr;
265 /** The name of the extension pack. */
266 Utf8Str strName;
267 /** The replace argument. */
268 bool fForcedRemoval;
269 /** The display info argument. */
270 Utf8Str strDisplayInfo;
271};
272
273DEFINE_EMPTY_CTOR_DTOR(ExtPackFile)
274
275/**
276 * Called by ComObjPtr::createObject when creating the object.
277 *
278 * Just initialize the basic object state, do the rest in initWithDir().
279 *
280 * @returns S_OK.
281 */
282HRESULT ExtPackFile::FinalConstruct()
283{
284 m = NULL;
285 return BaseFinalConstruct();
286}
287
288/**
289 * Initializes the extension pack by reading its file.
290 *
291 * @returns COM status code.
292 * @param a_pszFile The path to the extension pack file.
293 * @param a_pszDigest The SHA-256 digest of the file. Or an empty string.
294 * @param a_pExtPackMgr Pointer to the extension pack manager.
295 * @param a_pVirtualBox Pointer to the VirtualBox object.
296 */
297HRESULT ExtPackFile::initWithFile(const char *a_pszFile, const char *a_pszDigest, ExtPackManager *a_pExtPackMgr,
298 VirtualBox *a_pVirtualBox)
299{
300 AutoInitSpan autoInitSpan(this);
301 AssertReturn(autoInitSpan.isOk(), E_FAIL);
302
303 /*
304 * Allocate + initialize our private data.
305 */
306 m = new ExtPackFile::Data;
307 VBoxExtPackInitDesc(&m->Desc);
308 RT_ZERO(m->ObjInfoDesc);
309 m->fUsable = false;
310 m->strWhyUnusable = tr("ExtPack::init failed");
311 m->strExtPackFile = a_pszFile;
312 m->strDigest = a_pszDigest;
313 m->hExtPackFile = NIL_RTFILE;
314 m->hOurManifest = NIL_RTMANIFEST;
315 m->ptrExtPackMgr = a_pExtPackMgr;
316 m->pVirtualBox = a_pVirtualBox;
317
318 RTCString *pstrTarName = VBoxExtPackExtractNameFromTarballPath(a_pszFile);
319 if (pstrTarName)
320 {
321 m->Desc.strName = *pstrTarName;
322 delete pstrTarName;
323 pstrTarName = NULL;
324 }
325
326 autoInitSpan.setSucceeded();
327
328 /*
329 * Try open the extension pack and check that it is a regular file.
330 */
331 int vrc = RTFileOpen(&m->hExtPackFile, a_pszFile,
332 RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN);
333 if (RT_FAILURE(vrc))
334 {
335 if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
336 return initFailed(tr("'%s' file not found"), a_pszFile);
337 return initFailed(tr("RTFileOpen('%s',,) failed with %Rrc"), a_pszFile, vrc);
338 }
339
340 RTFSOBJINFO ObjInfo;
341 vrc = RTFileQueryInfo(m->hExtPackFile, &ObjInfo, RTFSOBJATTRADD_UNIX);
342 if (RT_FAILURE(vrc))
343 return initFailed(tr("RTFileQueryInfo failed with %Rrc on '%s'"), vrc, a_pszFile);
344 if (!RTFS_IS_FILE(ObjInfo.Attr.fMode))
345 return initFailed(tr("Not a regular file: %s"), a_pszFile);
346
347 /*
348 * Validate the tarball and extract the XML file.
349 */
350 char szError[8192];
351 RTVFSFILE hXmlFile;
352 vrc = VBoxExtPackValidateTarball(m->hExtPackFile, NULL /*pszExtPackName*/, a_pszFile, a_pszDigest,
353 szError, sizeof(szError), &m->hOurManifest, &hXmlFile, &m->strDigest);
354 if (RT_FAILURE(vrc))
355 return initFailed(tr("%s"), szError);
356
357 /*
358 * Parse the XML.
359 */
360 RTCString strSavedName(m->Desc.strName);
361 RTCString *pStrLoadErr = VBoxExtPackLoadDescFromVfsFile(hXmlFile, &m->Desc, &m->ObjInfoDesc);
362 RTVfsFileRelease(hXmlFile);
363 if (pStrLoadErr != NULL)
364 {
365 m->strWhyUnusable.printf(tr("Failed to the xml file: %s"), pStrLoadErr->c_str());
366 m->Desc.strName = strSavedName;
367 delete pStrLoadErr;
368 return S_OK;
369 }
370
371 /*
372 * Match the tarball name with the name from the XML.
373 */
374 /** @todo drop this restriction after the old install interface is
375 * dropped. */
376 if (!strSavedName.equalsIgnoreCase(m->Desc.strName))
377 return initFailed(tr("Extension pack name mismatch between the downloaded file and the XML inside it (xml='%s' file='%s')"),
378 m->Desc.strName.c_str(), strSavedName.c_str());
379
380
381 m->fUsable = true;
382 m->strWhyUnusable.setNull();
383 return S_OK;
384}
385
386/**
387 * Protected helper that formats the strWhyUnusable value.
388 *
389 * @returns S_OK
390 * @param a_pszWhyFmt Why it failed, format string.
391 * @param ... The format arguments.
392 */
393HRESULT ExtPackFile::initFailed(const char *a_pszWhyFmt, ...)
394{
395 va_list va;
396 va_start(va, a_pszWhyFmt);
397 m->strWhyUnusable.printfV(a_pszWhyFmt, va);
398 va_end(va);
399 return S_OK;
400}
401
402/**
403 * COM cruft.
404 */
405void ExtPackFile::FinalRelease()
406{
407 uninit();
408 BaseFinalRelease();
409}
410
411/**
412 * Do the actual cleanup.
413 */
414void ExtPackFile::uninit()
415{
416 /* Enclose the state transition Ready->InUninit->NotReady */
417 AutoUninitSpan autoUninitSpan(this);
418 if (!autoUninitSpan.uninitDone() && m != NULL)
419 {
420 VBoxExtPackFreeDesc(&m->Desc);
421 RTFileClose(m->hExtPackFile);
422 m->hExtPackFile = NIL_RTFILE;
423 RTManifestRelease(m->hOurManifest);
424 m->hOurManifest = NIL_RTMANIFEST;
425
426 delete m;
427 m = NULL;
428 }
429}
430
431HRESULT ExtPackFile::getName(com::Utf8Str &aName)
432{
433 aName = m->Desc.strName;
434 return S_OK;
435}
436
437HRESULT ExtPackFile::getDescription(com::Utf8Str &aDescription)
438{
439 aDescription = m->Desc.strDescription;
440 return S_OK;
441}
442
443HRESULT ExtPackFile::getVersion(com::Utf8Str &aVersion)
444{
445 aVersion = m->Desc.strVersion;
446 return S_OK;
447}
448
449HRESULT ExtPackFile::getEdition(com::Utf8Str &aEdition)
450{
451 aEdition = m->Desc.strEdition;
452 return S_OK;
453}
454
455HRESULT ExtPackFile::getRevision(ULONG *aRevision)
456{
457 *aRevision = m->Desc.uRevision;
458 return S_OK;
459}
460
461HRESULT ExtPackFile::getVRDEModule(com::Utf8Str &aVRDEModule)
462{
463 aVRDEModule = m->Desc.strVrdeModule;
464 return S_OK;
465}
466
467HRESULT ExtPackFile::getPlugIns(std::vector<ComPtr<IExtPackPlugIn> > &aPlugIns)
468{
469 /** @todo implement plug-ins. */
470#ifdef VBOX_WITH_XPCOM
471 NOREF(aPlugIns);
472#endif
473 NOREF(aPlugIns);
474 ReturnComNotImplemented();
475}
476
477HRESULT ExtPackFile::getUsable(BOOL *aUsable)
478{
479 *aUsable = m->fUsable;
480 return S_OK;
481}
482
483HRESULT ExtPackFile::getWhyUnusable(com::Utf8Str &aWhyUnusable)
484{
485 aWhyUnusable = m->strWhyUnusable;
486 return S_OK;
487}
488
489HRESULT ExtPackFile::getShowLicense(BOOL *aShowLicense)
490{
491 *aShowLicense = m->Desc.fShowLicense;
492 return S_OK;
493}
494
495HRESULT ExtPackFile::getLicense(com::Utf8Str &aLicense)
496{
497 Utf8Str strHtml("html");
498 Utf8Str str("");
499 return queryLicense(str, str, strHtml, aLicense);
500}
501
502/* Same as ExtPack::QueryLicense, should really explore the subject of base classes here... */
503HRESULT ExtPackFile::queryLicense(const com::Utf8Str &aPreferredLocale, const com::Utf8Str &aPreferredLanguage,
504 const com::Utf8Str &aFormat, com::Utf8Str &aLicenseText)
505{
506 HRESULT hrc = S_OK;
507
508 /*
509 * Validate input.
510 */
511
512 if (aPreferredLocale.length() != 2 && aPreferredLocale.length() != 0)
513 return setError(E_FAIL, tr("The preferred locale is a two character string or empty."));
514
515 if (aPreferredLanguage.length() != 2 && aPreferredLanguage.length() != 0)
516 return setError(E_FAIL, tr("The preferred lanuage is a two character string or empty."));
517
518 if ( !aFormat.equals("html")
519 && !aFormat.equals("rtf")
520 && !aFormat.equals("txt"))
521 return setError(E_FAIL, tr("The license format can only have the values 'html', 'rtf' and 'txt'."));
522
523 /*
524 * Combine the options to form a file name before locking down anything.
525 */
526 char szName[sizeof(VBOX_EXTPACK_LICENSE_NAME_PREFIX "-de_DE.html") + 2];
527 if (aPreferredLocale.isNotEmpty() && aPreferredLanguage.isNotEmpty())
528 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s_%s.%s",
529 aPreferredLocale.c_str(), aPreferredLanguage.c_str(), aFormat.c_str());
530 else if (aPreferredLocale.isNotEmpty())
531 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s.%s",
532 aPreferredLocale.c_str(), aFormat.c_str());
533 else if (aPreferredLanguage.isNotEmpty())
534 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-_%s.%s",
535 aPreferredLocale.c_str(), aFormat.c_str());
536 else
537 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX ".%s",
538 aFormat.c_str());
539 /*
540 * Lock the extension pack. We need a write lock here as there must not be
541 * concurrent accesses to the tar file handle.
542 */
543 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
544
545 /*
546 * Do not permit this query on a pack that isn't considered usable (could
547 * be marked so because of bad license files).
548 */
549 if (!m->fUsable)
550 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
551 else
552 {
553 /*
554 * Look it up in the manifest before scanning the tarball for it
555 */
556 if (RTManifestEntryExists(m->hOurManifest, szName))
557 {
558 RTVFSFSSTREAM hTarFss;
559 char szError[8192];
560 int vrc = VBoxExtPackOpenTarFss(m->hExtPackFile, szError, sizeof(szError), &hTarFss, NULL);
561 if (RT_SUCCESS(vrc))
562 {
563 for (;;)
564 {
565 /* Get the first/next. */
566 char *pszName;
567 RTVFSOBJ hVfsObj;
568 RTVFSOBJTYPE enmType;
569 vrc = RTVfsFsStrmNext(hTarFss, &pszName, &enmType, &hVfsObj);
570 if (RT_FAILURE(vrc))
571 {
572 if (vrc != VERR_EOF)
573 hrc = setError(VBOX_E_IPRT_ERROR, tr("RTVfsFsStrmNext failed: %Rrc"), vrc);
574 else
575 hrc = setError(E_UNEXPECTED, tr("'%s' was found in the manifest but not in the tarball"), szName);
576 break;
577 }
578
579 /* Is this it? */
580 const char *pszAdjName = pszName[0] == '.' && pszName[1] == '/' ? &pszName[2] : pszName;
581 if ( !strcmp(pszAdjName, szName)
582 && ( enmType == RTVFSOBJTYPE_IO_STREAM
583 || enmType == RTVFSOBJTYPE_FILE))
584 {
585 RTVFSIOSTREAM hVfsIos = RTVfsObjToIoStream(hVfsObj);
586 RTVfsObjRelease(hVfsObj);
587 RTStrFree(pszName);
588
589 /* Load the file into memory. */
590 RTFSOBJINFO ObjInfo;
591 vrc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_NOTHING);
592 if (RT_SUCCESS(vrc))
593 {
594 size_t cbFile = (size_t)ObjInfo.cbObject;
595 void *pvFile = RTMemAllocZ(cbFile + 1);
596 if (pvFile)
597 {
598 vrc = RTVfsIoStrmRead(hVfsIos, pvFile, cbFile, true /*fBlocking*/, NULL);
599 if (RT_SUCCESS(vrc))
600 {
601 /* try translate it into a string we can return. */
602 Bstr bstrLicense((const char *)pvFile, cbFile);
603 if (bstrLicense.isNotEmpty())
604 {
605 aLicenseText = Utf8Str(bstrLicense);
606 hrc = S_OK;
607 }
608 else
609 hrc = setError(VBOX_E_IPRT_ERROR,
610 tr("The license file '%s' is empty or contains invalid UTF-8 encoding"),
611 szName);
612 }
613 else
614 hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to read '%s': %Rrc"), szName, vrc);
615 RTMemFree(pvFile);
616 }
617 else
618 hrc = setError(E_OUTOFMEMORY, tr("Failed to allocate %zu bytes for '%s'"), cbFile, szName);
619 }
620 else
621 hrc = setError(VBOX_E_IPRT_ERROR, tr("RTVfsIoStrmQueryInfo on '%s': %Rrc"), szName, vrc);
622 RTVfsIoStrmRelease(hVfsIos);
623 break;
624 }
625
626 /* Release current. */
627 RTVfsObjRelease(hVfsObj);
628 RTStrFree(pszName);
629 }
630 RTVfsFsStrmRelease(hTarFss);
631 }
632 else
633 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("%s"), szError);
634 }
635 else
636 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("The license file '%s' was not found in '%s'"),
637 szName, m->strExtPackFile.c_str());
638 }
639 return hrc;
640}
641
642HRESULT ExtPackFile::getFilePath(com::Utf8Str &aFilePath)
643{
644
645 aFilePath = m->strExtPackFile;
646 return S_OK;
647}
648
649HRESULT ExtPackFile::install(BOOL aReplace, const com::Utf8Str &aDisplayInfo, ComPtr<IProgress> &aProgress)
650{
651 HRESULT hrc;
652 if (m->fUsable)
653 {
654 ExtPackInstallTask *pTask = NULL;
655 try
656 {
657 pTask = new ExtPackInstallTask();
658 hrc = pTask->Init(this, aReplace != FALSE, aDisplayInfo, m->ptrExtPackMgr);
659 if (SUCCEEDED(hrc))
660 {
661 ComPtr<Progress> ptrProgress = pTask->ptrProgress;
662 hrc = pTask->createThread(NULL, RTTHREADTYPE_DEFAULT);
663 pTask = NULL; /* The _completely_ _undocumented_ createThread method always consumes pTask. */
664 if (SUCCEEDED(hrc))
665 hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
666 else
667 hrc = setError(VBOX_E_IPRT_ERROR,
668 tr("Starting thread for an extension pack installation failed with %Rrc"), hrc);
669 }
670 else
671 hrc = setError(VBOX_E_IPRT_ERROR,
672 tr("Looks like creating a progress object for ExtraPackInstallTask object failed"));
673 }
674 catch (std::bad_alloc &)
675 {
676 hrc = E_OUTOFMEMORY;
677 }
678 catch (HRESULT hrcXcpt)
679 {
680 LogFlowThisFunc(("Exception was caught in the function ExtPackFile::install() \n"));
681 hrc = hrcXcpt;
682 }
683 if (pTask)
684 delete pTask;
685 }
686 else
687 hrc = setError(E_FAIL, "%s", m->strWhyUnusable.c_str());
688 return hrc;
689}
690
691#endif /* !VBOX_COM_INPROC */
692
693
694
695
696DEFINE_EMPTY_CTOR_DTOR(ExtPack)
697
698/**
699 * Called by ComObjPtr::createObject when creating the object.
700 *
701 * Just initialize the basic object state, do the rest in initWithDir().
702 *
703 * @returns S_OK.
704 */
705HRESULT ExtPack::FinalConstruct()
706{
707 m = NULL;
708 return S_OK;
709}
710
711/**
712 * Initializes the extension pack by reading its file.
713 *
714 * @returns COM status code.
715 * @param a_enmContext The context we're in.
716 * @param a_pszName The name of the extension pack. This is also the
717 * name of the subdirector under @a a_pszParentDir
718 * where the extension pack is installed.
719 * @param a_pszDir The extension pack directory name.
720 */
721HRESULT ExtPack::initWithDir(VBOXEXTPACKCTX a_enmContext, const char *a_pszName, const char *a_pszDir)
722{
723 AutoInitSpan autoInitSpan(this);
724 AssertReturn(autoInitSpan.isOk(), E_FAIL);
725
726 static const VBOXEXTPACKHLP s_HlpTmpl =
727 {
728 /* u32Version = */ VBOXEXTPACKHLP_VERSION,
729 /* uVBoxFullVersion = */ VBOX_FULL_VERSION,
730 /* uVBoxVersionRevision = */ 0,
731 /* u32Padding = */ 0,
732 /* pszVBoxVersion = */ "",
733 /* pfnFindModule = */ ExtPack::i_hlpFindModule,
734 /* pfnGetFilePath = */ ExtPack::i_hlpGetFilePath,
735 /* pfnGetContext = */ ExtPack::i_hlpGetContext,
736 /* pfnLoadHGCMService = */ ExtPack::i_hlpLoadHGCMService,
737 /* pfnLoadVDPlugin = */ ExtPack::i_hlpLoadVDPlugin,
738 /* pfnUnloadVDPlugin = */ ExtPack::i_hlpUnloadVDPlugin,
739 /* pfnReserved1 = */ ExtPack::i_hlpReservedN,
740 /* pfnReserved2 = */ ExtPack::i_hlpReservedN,
741 /* pfnReserved3 = */ ExtPack::i_hlpReservedN,
742 /* pfnReserved4 = */ ExtPack::i_hlpReservedN,
743 /* pfnReserved5 = */ ExtPack::i_hlpReservedN,
744 /* pfnReserved6 = */ ExtPack::i_hlpReservedN,
745 /* u32EndMarker = */ VBOXEXTPACKHLP_VERSION
746 };
747
748 /*
749 * Allocate + initialize our private data.
750 */
751 m = new Data;
752 VBoxExtPackInitDesc(&m->Desc);
753 m->Desc.strName = a_pszName;
754 RT_ZERO(m->ObjInfoDesc);
755 m->fUsable = false;
756 m->strWhyUnusable = tr("ExtPack::init failed");
757 m->strExtPackPath = a_pszDir;
758 RT_ZERO(m->ObjInfoExtPack);
759 m->strMainModPath.setNull();
760 RT_ZERO(m->ObjInfoMainMod);
761 m->hMainMod = NIL_RTLDRMOD;
762 m->Hlp = s_HlpTmpl;
763 m->Hlp.pszVBoxVersion = RTBldCfgVersion();
764 m->Hlp.uVBoxInternalRevision = RTBldCfgRevision();
765 m->pThis = this;
766 m->pReg = NULL;
767 m->enmContext = a_enmContext;
768 m->fMadeReadyCall = false;
769
770 /*
771 * Make sure the SUPR3Hardened API works (ignoring errors for now).
772 */
773 int rc = SUPR3HardenedVerifyInit();
774 if (RT_FAILURE(rc))
775 LogRel(("SUPR3HardenedVerifyInit failed: %Rrc\n", rc));
776
777 /*
778 * Probe the extension pack (this code is shared with refresh()).
779 */
780 i_probeAndLoad();
781
782 autoInitSpan.setSucceeded();
783 return S_OK;
784}
785
786/**
787 * COM cruft.
788 */
789void ExtPack::FinalRelease()
790{
791 uninit();
792}
793
794/**
795 * Do the actual cleanup.
796 */
797void ExtPack::uninit()
798{
799 /* Enclose the state transition Ready->InUninit->NotReady */
800 AutoUninitSpan autoUninitSpan(this);
801 if (!autoUninitSpan.uninitDone() && m != NULL)
802 {
803 if (m->hMainMod != NIL_RTLDRMOD)
804 {
805 AssertPtr(m->pReg);
806 if (m->pReg->pfnUnload != NULL)
807 m->pReg->pfnUnload(m->pReg);
808
809 RTLdrClose(m->hMainMod);
810 m->hMainMod = NIL_RTLDRMOD;
811 m->pReg = NULL;
812 }
813
814 VBoxExtPackFreeDesc(&m->Desc);
815
816 delete m;
817 m = NULL;
818 }
819}
820
821
822/**
823 * Calls the installed hook.
824 *
825 * @returns true if we left the lock, false if we didn't.
826 * @param a_pVirtualBox The VirtualBox interface.
827 * @param a_pLock The write lock held by the caller.
828 * @param pErrInfo Where to return error information.
829 */
830bool ExtPack::i_callInstalledHook(IVirtualBox *a_pVirtualBox, AutoWriteLock *a_pLock, PRTERRINFO pErrInfo)
831{
832 if ( m != NULL
833 && m->hMainMod != NIL_RTLDRMOD)
834 {
835 if (m->pReg->pfnInstalled)
836 {
837 ComPtr<ExtPack> ptrSelfRef = this;
838 a_pLock->release();
839 pErrInfo->rc = m->pReg->pfnInstalled(m->pReg, a_pVirtualBox, pErrInfo);
840 a_pLock->acquire();
841 return true;
842 }
843 }
844 pErrInfo->rc = VINF_SUCCESS;
845 return false;
846}
847
848/**
849 * Calls the uninstall hook and closes the module.
850 *
851 * @returns S_OK or COM error status with error information.
852 * @param a_pVirtualBox The VirtualBox interface.
853 * @param a_fForcedRemoval When set, we'll ignore complaints from the
854 * uninstall hook.
855 * @remarks The caller holds the manager's write lock, not released.
856 */
857HRESULT ExtPack::i_callUninstallHookAndClose(IVirtualBox *a_pVirtualBox, bool a_fForcedRemoval)
858{
859 HRESULT hrc = S_OK;
860
861 if ( m != NULL
862 && m->hMainMod != NIL_RTLDRMOD)
863 {
864 if (m->pReg->pfnUninstall && !a_fForcedRemoval)
865 {
866 int vrc = m->pReg->pfnUninstall(m->pReg, a_pVirtualBox);
867 if (RT_FAILURE(vrc))
868 {
869 LogRel(("ExtPack pfnUninstall returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
870 if (!a_fForcedRemoval)
871 hrc = setError(E_FAIL, tr("pfnUninstall returned %Rrc"), vrc);
872 }
873 }
874 if (SUCCEEDED(hrc))
875 {
876 RTLdrClose(m->hMainMod);
877 m->hMainMod = NIL_RTLDRMOD;
878 m->pReg = NULL;
879 }
880 }
881
882 return hrc;
883}
884
885/**
886 * Calls the pfnVirtualBoxReady hook.
887 *
888 * @returns true if we left the lock, false if we didn't.
889 * @param a_pVirtualBox The VirtualBox interface.
890 * @param a_pLock The write lock held by the caller.
891 */
892bool ExtPack::i_callVirtualBoxReadyHook(IVirtualBox *a_pVirtualBox, AutoWriteLock *a_pLock)
893{
894 if ( m != NULL
895 && m->fUsable
896 && !m->fMadeReadyCall)
897 {
898 m->fMadeReadyCall = true;
899 if (m->pReg->pfnVirtualBoxReady)
900 {
901 ComPtr<ExtPack> ptrSelfRef = this;
902 a_pLock->release();
903 m->pReg->pfnVirtualBoxReady(m->pReg, a_pVirtualBox);
904 a_pLock->acquire();
905 return true;
906 }
907 }
908 return false;
909}
910
911/**
912 * Calls the pfnConsoleReady hook.
913 *
914 * @returns true if we left the lock, false if we didn't.
915 * @param a_pConsole The Console interface.
916 * @param a_pLock The write lock held by the caller.
917 */
918bool ExtPack::i_callConsoleReadyHook(IConsole *a_pConsole, AutoWriteLock *a_pLock)
919{
920 if ( m != NULL
921 && m->fUsable
922 && !m->fMadeReadyCall)
923 {
924 m->fMadeReadyCall = true;
925 if (m->pReg->pfnConsoleReady)
926 {
927 ComPtr<ExtPack> ptrSelfRef = this;
928 a_pLock->release();
929 m->pReg->pfnConsoleReady(m->pReg, a_pConsole);
930 a_pLock->acquire();
931 return true;
932 }
933 }
934 return false;
935}
936
937/**
938 * Calls the pfnVMCreate hook.
939 *
940 * @returns true if we left the lock, false if we didn't.
941 * @param a_pVirtualBox The VirtualBox interface.
942 * @param a_pMachine The machine interface of the new VM.
943 * @param a_pLock The write lock held by the caller.
944 */
945bool ExtPack::i_callVmCreatedHook(IVirtualBox *a_pVirtualBox, IMachine *a_pMachine, AutoWriteLock *a_pLock)
946{
947 if ( m != NULL
948 && m->fUsable)
949 {
950 if (m->pReg->pfnVMCreated)
951 {
952 ComPtr<ExtPack> ptrSelfRef = this;
953 a_pLock->release();
954 m->pReg->pfnVMCreated(m->pReg, a_pVirtualBox, a_pMachine);
955 a_pLock->acquire();
956 return true;
957 }
958 }
959 return false;
960}
961
962/**
963 * Calls the pfnVMConfigureVMM hook.
964 *
965 * @returns true if we left the lock, false if we didn't.
966 * @param a_pConsole The console interface.
967 * @param a_pVM The VM handle.
968 * @param a_pLock The write lock held by the caller.
969 * @param a_pvrc Where to return the status code of the
970 * callback. This is always set. LogRel is
971 * called on if a failure status is returned.
972 */
973bool ExtPack::i_callVmConfigureVmmHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock, int *a_pvrc)
974{
975 *a_pvrc = VINF_SUCCESS;
976 if ( m != NULL
977 && m->fUsable)
978 {
979 if (m->pReg->pfnVMConfigureVMM)
980 {
981 ComPtr<ExtPack> ptrSelfRef = this;
982 a_pLock->release();
983 int vrc = m->pReg->pfnVMConfigureVMM(m->pReg, a_pConsole, a_pVM);
984 *a_pvrc = vrc;
985 a_pLock->acquire();
986 if (RT_FAILURE(vrc))
987 LogRel(("ExtPack pfnVMConfigureVMM returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
988 return true;
989 }
990 }
991 return false;
992}
993
994/**
995 * Calls the pfnVMPowerOn hook.
996 *
997 * @returns true if we left the lock, false if we didn't.
998 * @param a_pConsole The console interface.
999 * @param a_pVM The VM handle.
1000 * @param a_pLock The write lock held by the caller.
1001 * @param a_pvrc Where to return the status code of the
1002 * callback. This is always set. LogRel is
1003 * called on if a failure status is returned.
1004 */
1005bool ExtPack::i_callVmPowerOnHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock, int *a_pvrc)
1006{
1007 *a_pvrc = VINF_SUCCESS;
1008 if ( m != NULL
1009 && m->fUsable)
1010 {
1011 if (m->pReg->pfnVMPowerOn)
1012 {
1013 ComPtr<ExtPack> ptrSelfRef = this;
1014 a_pLock->release();
1015 int vrc = m->pReg->pfnVMPowerOn(m->pReg, a_pConsole, a_pVM);
1016 *a_pvrc = vrc;
1017 a_pLock->acquire();
1018 if (RT_FAILURE(vrc))
1019 LogRel(("ExtPack pfnVMPowerOn returned %Rrc for %s\n", vrc, m->Desc.strName.c_str()));
1020 return true;
1021 }
1022 }
1023 return false;
1024}
1025
1026/**
1027 * Calls the pfnVMPowerOff hook.
1028 *
1029 * @returns true if we left the lock, false if we didn't.
1030 * @param a_pConsole The console interface.
1031 * @param a_pVM The VM handle.
1032 * @param a_pLock The write lock held by the caller.
1033 */
1034bool ExtPack::i_callVmPowerOffHook(IConsole *a_pConsole, PVM a_pVM, AutoWriteLock *a_pLock)
1035{
1036 if ( m != NULL
1037 && m->fUsable)
1038 {
1039 if (m->pReg->pfnVMPowerOff)
1040 {
1041 ComPtr<ExtPack> ptrSelfRef = this;
1042 a_pLock->release();
1043 m->pReg->pfnVMPowerOff(m->pReg, a_pConsole, a_pVM);
1044 a_pLock->acquire();
1045 return true;
1046 }
1047 }
1048 return false;
1049}
1050
1051/**
1052 * Check if the extension pack is usable and has an VRDE module.
1053 *
1054 * @returns S_OK or COM error status with error information.
1055 *
1056 * @remarks Caller holds the extension manager lock for reading, no locking
1057 * necessary.
1058 */
1059HRESULT ExtPack::i_checkVrde(void)
1060{
1061 HRESULT hrc;
1062 if ( m != NULL
1063 && m->fUsable)
1064 {
1065 if (m->Desc.strVrdeModule.isNotEmpty())
1066 hrc = S_OK;
1067 else
1068 hrc = setError(E_FAIL, tr("The extension pack '%s' does not include a VRDE module"), m->Desc.strName.c_str());
1069 }
1070 else
1071 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
1072 return hrc;
1073}
1074
1075/**
1076 * Same as checkVrde(), except that it also resolves the path to the module.
1077 *
1078 * @returns S_OK or COM error status with error information.
1079 * @param a_pstrVrdeLibrary Where to return the path on success.
1080 *
1081 * @remarks Caller holds the extension manager lock for reading, no locking
1082 * necessary.
1083 */
1084HRESULT ExtPack::i_getVrdpLibraryName(Utf8Str *a_pstrVrdeLibrary)
1085{
1086 HRESULT hrc = i_checkVrde();
1087 if (SUCCEEDED(hrc))
1088 {
1089 if (i_findModule(m->Desc.strVrdeModule.c_str(), NULL, VBOXEXTPACKMODKIND_R3,
1090 a_pstrVrdeLibrary, NULL /*a_pfNative*/, NULL /*a_pObjInfo*/))
1091 hrc = S_OK;
1092 else
1093 hrc = setError(E_FAIL, tr("Failed to locate the VRDE module '%s' in extension pack '%s'"),
1094 m->Desc.strVrdeModule.c_str(), m->Desc.strName.c_str());
1095 }
1096 return hrc;
1097}
1098
1099/**
1100 * Resolves the path to the module.
1101 *
1102 * @returns S_OK or COM error status with error information.
1103 * @param a_pszModuleName The library.
1104 * @param a_pstrLibrary Where to return the path on success.
1105 *
1106 * @remarks Caller holds the extension manager lock for reading, no locking
1107 * necessary.
1108 */
1109HRESULT ExtPack::i_getLibraryName(const char *a_pszModuleName, Utf8Str *a_pstrLibrary)
1110{
1111 HRESULT hrc;
1112 if (i_findModule(a_pszModuleName, NULL, VBOXEXTPACKMODKIND_R3,
1113 a_pstrLibrary, NULL /*a_pfNative*/, NULL /*a_pObjInfo*/))
1114 hrc = S_OK;
1115 else
1116 hrc = setError(E_FAIL, tr("Failed to locate the module '%s' in extension pack '%s'"),
1117 a_pszModuleName, m->Desc.strName.c_str());
1118 return hrc;
1119}
1120
1121/**
1122 * Check if this extension pack wishes to be the default VRDE provider.
1123 *
1124 * @returns @c true if it wants to and it is in a usable state, otherwise
1125 * @c false.
1126 *
1127 * @remarks Caller holds the extension manager lock for reading, no locking
1128 * necessary.
1129 */
1130bool ExtPack::i_wantsToBeDefaultVrde(void) const
1131{
1132 return m->fUsable
1133 && m->Desc.strVrdeModule.isNotEmpty();
1134}
1135
1136/**
1137 * Refreshes the extension pack state.
1138 *
1139 * This is called by the manager so that the on disk changes are picked up.
1140 *
1141 * @returns S_OK or COM error status with error information.
1142 *
1143 * @param a_pfCanDelete Optional can-delete-this-object output indicator.
1144 *
1145 * @remarks Caller holds the extension manager lock for writing.
1146 * @remarks Only called in VBoxSVC.
1147 */
1148HRESULT ExtPack::i_refresh(bool *a_pfCanDelete)
1149{
1150 if (a_pfCanDelete)
1151 *a_pfCanDelete = false;
1152
1153 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS); /* for the COMGETTERs */
1154
1155 /*
1156 * Has the module been deleted?
1157 */
1158 RTFSOBJINFO ObjInfoExtPack;
1159 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1160 if ( RT_FAILURE(vrc)
1161 || !RTFS_IS_DIRECTORY(ObjInfoExtPack.Attr.fMode))
1162 {
1163 if (a_pfCanDelete)
1164 *a_pfCanDelete = true;
1165 return S_OK;
1166 }
1167
1168 /*
1169 * We've got a directory, so try query file system object info for the
1170 * files we are interested in as well.
1171 */
1172 RTFSOBJINFO ObjInfoDesc;
1173 char szDescFilePath[RTPATH_MAX];
1174 vrc = RTPathJoin(szDescFilePath, sizeof(szDescFilePath), m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME);
1175 if (RT_SUCCESS(vrc))
1176 vrc = RTPathQueryInfoEx(szDescFilePath, &ObjInfoDesc, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1177 if (RT_FAILURE(vrc))
1178 RT_ZERO(ObjInfoDesc);
1179
1180 RTFSOBJINFO ObjInfoMainMod;
1181 if (m->strMainModPath.isNotEmpty())
1182 vrc = RTPathQueryInfoEx(m->strMainModPath.c_str(), &ObjInfoMainMod, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1183 if (m->strMainModPath.isEmpty() || RT_FAILURE(vrc))
1184 RT_ZERO(ObjInfoMainMod);
1185
1186 /*
1187 * If we have a usable module already, just verify that things haven't
1188 * changed since we loaded it.
1189 */
1190 if (m->fUsable)
1191 {
1192 if (m->hMainMod == NIL_RTLDRMOD)
1193 i_probeAndLoad();
1194 else if ( !i_objinfoIsEqual(&ObjInfoDesc, &m->ObjInfoDesc)
1195 || !i_objinfoIsEqual(&ObjInfoMainMod, &m->ObjInfoMainMod)
1196 || !i_objinfoIsEqual(&ObjInfoExtPack, &m->ObjInfoExtPack) )
1197 {
1198 /** @todo not important, so it can wait. */
1199 }
1200 }
1201 /*
1202 * Ok, it is currently not usable. If anything has changed since last time
1203 * reprobe the extension pack.
1204 */
1205 else if ( !i_objinfoIsEqual(&ObjInfoDesc, &m->ObjInfoDesc)
1206 || !i_objinfoIsEqual(&ObjInfoMainMod, &m->ObjInfoMainMod)
1207 || !i_objinfoIsEqual(&ObjInfoExtPack, &m->ObjInfoExtPack) )
1208 i_probeAndLoad();
1209
1210 return S_OK;
1211}
1212
1213/**
1214 * Probes the extension pack, loading the main dll and calling its registration
1215 * entry point.
1216 *
1217 * This updates the state accordingly, the strWhyUnusable and fUnusable members
1218 * being the most important ones.
1219 */
1220void ExtPack::i_probeAndLoad(void)
1221{
1222 m->fUsable = false;
1223 m->fMadeReadyCall = false;
1224
1225 /*
1226 * Query the file system info for the extension pack directory. This and
1227 * all other file system info we save is for the benefit of refresh().
1228 */
1229 int vrc = RTPathQueryInfoEx(m->strExtPackPath.c_str(), &m->ObjInfoExtPack, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
1230 if (RT_FAILURE(vrc))
1231 {
1232 m->strWhyUnusable.printf(tr("RTPathQueryInfoEx on '%s' failed: %Rrc"), m->strExtPackPath.c_str(), vrc);
1233 return;
1234 }
1235 if (!RTFS_IS_DIRECTORY(m->ObjInfoExtPack.Attr.fMode))
1236 {
1237 if (RTFS_IS_SYMLINK(m->ObjInfoExtPack.Attr.fMode))
1238 m->strWhyUnusable.printf(tr("'%s' is a symbolic link, this is not allowed"),
1239 m->strExtPackPath.c_str(), vrc);
1240 else if (RTFS_IS_FILE(m->ObjInfoExtPack.Attr.fMode))
1241 m->strWhyUnusable.printf(tr("'%s' is a symbolic file, not a directory"),
1242 m->strExtPackPath.c_str(), vrc);
1243 else
1244 m->strWhyUnusable.printf(tr("'%s' is not a directory (fMode=%#x)"),
1245 m->strExtPackPath.c_str(), m->ObjInfoExtPack.Attr.fMode);
1246 return;
1247 }
1248
1249 RTERRINFOSTATIC ErrInfo;
1250 RTErrInfoInitStatic(&ErrInfo);
1251 vrc = SUPR3HardenedVerifyDir(m->strExtPackPath.c_str(), true /*fRecursive*/, true /*fCheckFiles*/, &ErrInfo.Core);
1252 if (RT_FAILURE(vrc))
1253 {
1254 m->strWhyUnusable.printf(tr("%s (rc=%Rrc)"), ErrInfo.Core.pszMsg, vrc);
1255 return;
1256 }
1257
1258 /*
1259 * Read the description file.
1260 */
1261 RTCString strSavedName(m->Desc.strName);
1262 RTCString *pStrLoadErr = VBoxExtPackLoadDesc(m->strExtPackPath.c_str(), &m->Desc, &m->ObjInfoDesc);
1263 if (pStrLoadErr != NULL)
1264 {
1265 m->strWhyUnusable.printf(tr("Failed to load '%s/%s': %s"),
1266 m->strExtPackPath.c_str(), VBOX_EXTPACK_DESCRIPTION_NAME, pStrLoadErr->c_str());
1267 m->Desc.strName = strSavedName;
1268 delete pStrLoadErr;
1269 return;
1270 }
1271
1272 /*
1273 * Make sure the XML name and directory matches.
1274 */
1275 if (!m->Desc.strName.equalsIgnoreCase(strSavedName))
1276 {
1277 m->strWhyUnusable.printf(tr("The description name ('%s') and directory name ('%s') does not match"),
1278 m->Desc.strName.c_str(), strSavedName.c_str());
1279 m->Desc.strName = strSavedName;
1280 return;
1281 }
1282
1283 /*
1284 * Check for possibly incompatible extpack versions.
1285 *
1286 * - In 4.3.12 the PDMUSBREG structure was modified without updating the
1287 * version number.
1288 * - In 4.3.16 (actually r95499) the VUSBIROOTHUBCONNECTOR interface changed without
1289 * also changing the UUID, with the result that our EHCI device could crash the
1290 * host process.
1291 * - In 4.3.12 and 4.3.30 the VUSBREQ structure was updated without updating
1292 * the PDMUSBREG or any other version number.
1293 *
1294 * Since this was from before VBOXEXTPACKREG::uVBoxFullVersion was
1295 * added, the check isn't all that generic.
1296 */
1297 if ( m->Desc.strName.equals("Oracle VM VirtualBox Extension Pack")
1298 && RTStrVersionCompare(m->Desc.strVersion.c_str(), "4.3.30") < 0)
1299 {
1300 m->strWhyUnusable.printf(tr("Incompatible extension pack (version '%s'), please update"), m->Desc.strVersion.c_str());
1301 return;
1302 }
1303
1304 /*
1305 * Load the main DLL and call the predefined entry point.
1306 */
1307 bool fIsNative;
1308 if (!i_findModule(m->Desc.strMainModule.c_str(), NULL /* default extension */, VBOXEXTPACKMODKIND_R3,
1309 &m->strMainModPath, &fIsNative, &m->ObjInfoMainMod))
1310 {
1311 m->strWhyUnusable.printf(tr("Failed to locate the main module ('%s')"), m->Desc.strMainModule.c_str());
1312 return;
1313 }
1314
1315 vrc = SUPR3HardenedVerifyPlugIn(m->strMainModPath.c_str(), &ErrInfo.Core);
1316 if (RT_FAILURE(vrc))
1317 {
1318 m->strWhyUnusable.printf(tr("%s"), ErrInfo.Core.pszMsg);
1319 return;
1320 }
1321
1322 if (fIsNative)
1323 {
1324 vrc = SUPR3HardenedLdrLoadPlugIn(m->strMainModPath.c_str(), &m->hMainMod, &ErrInfo.Core);
1325 if (RT_FAILURE(vrc))
1326 {
1327 m->hMainMod = NIL_RTLDRMOD;
1328 m->strWhyUnusable.printf(tr("Failed to load the main module ('%s'): %Rrc - %s"),
1329 m->strMainModPath.c_str(), vrc, ErrInfo.Core.pszMsg);
1330 return;
1331 }
1332 }
1333 else
1334 {
1335 m->strWhyUnusable.printf(tr("Only native main modules are currently supported"));
1336 return;
1337 }
1338
1339 /*
1340 * Resolve the predefined entry point.
1341 */
1342 PFNVBOXEXTPACKREGISTER pfnRegistration;
1343 vrc = RTLdrGetSymbol(m->hMainMod, VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, (void **)&pfnRegistration);
1344 if (RT_SUCCESS(vrc))
1345 {
1346 RTErrInfoClear(&ErrInfo.Core);
1347 vrc = pfnRegistration(&m->Hlp, &m->pReg, &ErrInfo.Core);
1348 if ( RT_SUCCESS(vrc)
1349 && !RTErrInfoIsSet(&ErrInfo.Core)
1350 && VALID_PTR(m->pReg))
1351 {
1352 if ( VBOXEXTPACK_IS_MAJOR_VER_EQUAL(m->pReg->u32Version, VBOXEXTPACKREG_VERSION)
1353 && m->pReg->u32EndMarker == m->pReg->u32Version)
1354 {
1355 if ( (!m->pReg->pfnInstalled || RT_VALID_PTR(m->pReg->pfnInstalled))
1356 && (!m->pReg->pfnUninstall || RT_VALID_PTR(m->pReg->pfnUninstall))
1357 && (!m->pReg->pfnVirtualBoxReady || RT_VALID_PTR(m->pReg->pfnVirtualBoxReady))
1358 && (!m->pReg->pfnConsoleReady || RT_VALID_PTR(m->pReg->pfnConsoleReady))
1359 && (!m->pReg->pfnUnload || RT_VALID_PTR(m->pReg->pfnUnload))
1360 && (!m->pReg->pfnVMCreated || RT_VALID_PTR(m->pReg->pfnVMCreated))
1361 && (!m->pReg->pfnVMConfigureVMM || RT_VALID_PTR(m->pReg->pfnVMConfigureVMM))
1362 && (!m->pReg->pfnVMPowerOn || RT_VALID_PTR(m->pReg->pfnVMPowerOn))
1363 && (!m->pReg->pfnVMPowerOff || RT_VALID_PTR(m->pReg->pfnVMPowerOff))
1364 && (!m->pReg->pfnQueryObject || RT_VALID_PTR(m->pReg->pfnQueryObject))
1365 )
1366 {
1367 /*
1368 * We're good!
1369 */
1370 m->fUsable = true;
1371 m->strWhyUnusable.setNull();
1372 return;
1373 }
1374
1375 m->strWhyUnusable = tr("The registration structure contains on or more invalid function pointers");
1376 }
1377 else
1378 m->strWhyUnusable.printf(tr("Unsupported registration structure version %u.%u"),
1379 RT_HIWORD(m->pReg->u32Version), RT_LOWORD(m->pReg->u32Version));
1380 }
1381 else
1382 m->strWhyUnusable.printf(tr("%s returned %Rrc, pReg=%p ErrInfo='%s'"),
1383 VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, vrc, m->pReg, ErrInfo.Core.pszMsg);
1384 m->pReg = NULL;
1385 }
1386 else
1387 m->strWhyUnusable.printf(tr("Failed to resolve exported symbol '%s' in the main module: %Rrc"),
1388 VBOX_EXTPACK_MAIN_MOD_ENTRY_POINT, vrc);
1389
1390 RTLdrClose(m->hMainMod);
1391 m->hMainMod = NIL_RTLDRMOD;
1392}
1393
1394/**
1395 * Finds a module.
1396 *
1397 * @returns true if found, false if not.
1398 * @param a_pszName The module base name (no extension).
1399 * @param a_pszExt The extension. If NULL we use default
1400 * extensions.
1401 * @param a_enmKind The kind of module to locate.
1402 * @param a_pStrFound Where to return the path to the module we've
1403 * found.
1404 * @param a_pfNative Where to return whether this is a native module
1405 * or an agnostic one. Optional.
1406 * @param a_pObjInfo Where to return the file system object info for
1407 * the module. Optional.
1408 */
1409bool ExtPack::i_findModule(const char *a_pszName, const char *a_pszExt, VBOXEXTPACKMODKIND a_enmKind,
1410 Utf8Str *a_pStrFound, bool *a_pfNative, PRTFSOBJINFO a_pObjInfo) const
1411{
1412 /*
1413 * Try the native path first.
1414 */
1415 char szPath[RTPATH_MAX];
1416 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetDotArch());
1417 AssertLogRelRCReturn(vrc, false);
1418 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1419 AssertLogRelRCReturn(vrc, false);
1420 if (!a_pszExt)
1421 {
1422 const char *pszDefExt;
1423 switch (a_enmKind)
1424 {
1425 case VBOXEXTPACKMODKIND_RC: pszDefExt = ".rc"; break;
1426 case VBOXEXTPACKMODKIND_R0: pszDefExt = ".r0"; break;
1427 case VBOXEXTPACKMODKIND_R3: pszDefExt = RTLdrGetSuff(); break;
1428 default:
1429 AssertFailedReturn(false);
1430 }
1431 vrc = RTStrCat(szPath, sizeof(szPath), pszDefExt);
1432 AssertLogRelRCReturn(vrc, false);
1433 }
1434
1435 RTFSOBJINFO ObjInfo;
1436 if (!a_pObjInfo)
1437 a_pObjInfo = &ObjInfo;
1438 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1439 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1440 {
1441 if (a_pfNative)
1442 *a_pfNative = true;
1443 *a_pStrFound = szPath;
1444 return true;
1445 }
1446
1447 /*
1448 * Try the platform agnostic modules.
1449 */
1450 /* gcc.x86/module.rel */
1451 char szSubDir[32];
1452 RTStrPrintf(szSubDir, sizeof(szSubDir), "%s.%s", RTBldCfgCompiler(), RTBldCfgTargetArch());
1453 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szSubDir);
1454 AssertLogRelRCReturn(vrc, false);
1455 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1456 AssertLogRelRCReturn(vrc, false);
1457 if (!a_pszExt)
1458 {
1459 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
1460 AssertLogRelRCReturn(vrc, false);
1461 }
1462 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1463 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1464 {
1465 if (a_pfNative)
1466 *a_pfNative = false;
1467 *a_pStrFound = szPath;
1468 return true;
1469 }
1470
1471 /* x86/module.rel */
1472 vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), RTBldCfgTargetArch());
1473 AssertLogRelRCReturn(vrc, false);
1474 vrc = RTPathAppend(szPath, sizeof(szPath), a_pszName);
1475 AssertLogRelRCReturn(vrc, false);
1476 if (!a_pszExt)
1477 {
1478 vrc = RTStrCat(szPath, sizeof(szPath), ".rel");
1479 AssertLogRelRCReturn(vrc, false);
1480 }
1481 vrc = RTPathQueryInfo(szPath, a_pObjInfo, RTFSOBJATTRADD_UNIX);
1482 if (RT_SUCCESS(vrc) && RTFS_IS_FILE(a_pObjInfo->Attr.fMode))
1483 {
1484 if (a_pfNative)
1485 *a_pfNative = false;
1486 *a_pStrFound = szPath;
1487 return true;
1488 }
1489
1490 return false;
1491}
1492
1493/**
1494 * Compares two file system object info structures.
1495 *
1496 * @returns true if equal, false if not.
1497 * @param pObjInfo1 The first.
1498 * @param pObjInfo2 The second.
1499 * @todo IPRT should do this, really.
1500 */
1501/* static */ bool ExtPack::i_objinfoIsEqual(PCRTFSOBJINFO pObjInfo1, PCRTFSOBJINFO pObjInfo2)
1502{
1503 if (!RTTimeSpecIsEqual(&pObjInfo1->ModificationTime, &pObjInfo2->ModificationTime))
1504 return false;
1505 if (!RTTimeSpecIsEqual(&pObjInfo1->ChangeTime, &pObjInfo2->ChangeTime))
1506 return false;
1507 if (!RTTimeSpecIsEqual(&pObjInfo1->BirthTime, &pObjInfo2->BirthTime))
1508 return false;
1509 if (pObjInfo1->cbObject != pObjInfo2->cbObject)
1510 return false;
1511 if (pObjInfo1->Attr.fMode != pObjInfo2->Attr.fMode)
1512 return false;
1513 if (pObjInfo1->Attr.enmAdditional == pObjInfo2->Attr.enmAdditional)
1514 {
1515 switch (pObjInfo1->Attr.enmAdditional)
1516 {
1517 case RTFSOBJATTRADD_UNIX:
1518 if (pObjInfo1->Attr.u.Unix.uid != pObjInfo2->Attr.u.Unix.uid)
1519 return false;
1520 if (pObjInfo1->Attr.u.Unix.gid != pObjInfo2->Attr.u.Unix.gid)
1521 return false;
1522 if (pObjInfo1->Attr.u.Unix.INodeIdDevice != pObjInfo2->Attr.u.Unix.INodeIdDevice)
1523 return false;
1524 if (pObjInfo1->Attr.u.Unix.INodeId != pObjInfo2->Attr.u.Unix.INodeId)
1525 return false;
1526 if (pObjInfo1->Attr.u.Unix.GenerationId != pObjInfo2->Attr.u.Unix.GenerationId)
1527 return false;
1528 break;
1529 default:
1530 break;
1531 }
1532 }
1533 return true;
1534}
1535
1536
1537/**
1538 * @interface_method_impl{VBOXEXTPACKHLP,pfnFindModule}
1539 */
1540/*static*/ DECLCALLBACK(int)
1541ExtPack::i_hlpFindModule(PCVBOXEXTPACKHLP pHlp, const char *pszName, const char *pszExt, VBOXEXTPACKMODKIND enmKind,
1542 char *pszFound, size_t cbFound, bool *pfNative)
1543{
1544 /*
1545 * Validate the input and get our bearings.
1546 */
1547 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1548 AssertPtrNullReturn(pszExt, VERR_INVALID_POINTER);
1549 AssertPtrReturn(pszFound, VERR_INVALID_POINTER);
1550 AssertPtrNullReturn(pfNative, VERR_INVALID_POINTER);
1551 AssertReturn(enmKind > VBOXEXTPACKMODKIND_INVALID && enmKind < VBOXEXTPACKMODKIND_END, VERR_INVALID_PARAMETER);
1552
1553 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1554 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1555 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1556 AssertPtrReturn(m, VERR_INVALID_POINTER);
1557 ExtPack *pThis = m->pThis;
1558 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1559
1560 /*
1561 * This is just a wrapper around findModule.
1562 */
1563 Utf8Str strFound;
1564 if (pThis->i_findModule(pszName, pszExt, enmKind, &strFound, pfNative, NULL))
1565 return RTStrCopy(pszFound, cbFound, strFound.c_str());
1566 return VERR_FILE_NOT_FOUND;
1567}
1568
1569/*static*/ DECLCALLBACK(int)
1570ExtPack::i_hlpGetFilePath(PCVBOXEXTPACKHLP pHlp, const char *pszFilename, char *pszPath, size_t cbPath)
1571{
1572 /*
1573 * Validate the input and get our bearings.
1574 */
1575 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1576 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
1577 AssertReturn(cbPath > 0, VERR_BUFFER_OVERFLOW);
1578
1579 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1580 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1581 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1582 AssertPtrReturn(m, VERR_INVALID_POINTER);
1583 ExtPack *pThis = m->pThis;
1584 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1585
1586 /*
1587 * This is a simple RTPathJoin, no checking if things exists or anything.
1588 */
1589 int vrc = RTPathJoin(pszPath, cbPath, pThis->m->strExtPackPath.c_str(), pszFilename);
1590 if (RT_FAILURE(vrc))
1591 RT_BZERO(pszPath, cbPath);
1592 return vrc;
1593}
1594
1595/*static*/ DECLCALLBACK(VBOXEXTPACKCTX)
1596ExtPack::i_hlpGetContext(PCVBOXEXTPACKHLP pHlp)
1597{
1598 /*
1599 * Validate the input and get our bearings.
1600 */
1601 AssertPtrReturn(pHlp, VBOXEXTPACKCTX_INVALID);
1602 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VBOXEXTPACKCTX_INVALID);
1603 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1604 AssertPtrReturn(m, VBOXEXTPACKCTX_INVALID);
1605 ExtPack *pThis = m->pThis;
1606 AssertPtrReturn(pThis, VBOXEXTPACKCTX_INVALID);
1607
1608 return pThis->m->enmContext;
1609}
1610
1611/*static*/ DECLCALLBACK(int)
1612ExtPack::i_hlpLoadHGCMService(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IConsole) *pConsole,
1613 const char *pszServiceLibrary, const char *pszServiceName)
1614{
1615#ifdef VBOX_COM_INPROC
1616 /*
1617 * Validate the input and get our bearings.
1618 */
1619 AssertPtrReturn(pszServiceLibrary, VERR_INVALID_POINTER);
1620 AssertPtrReturn(pszServiceName, VERR_INVALID_POINTER);
1621
1622 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1623 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1624 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1625 AssertPtrReturn(m, VERR_INVALID_POINTER);
1626 ExtPack *pThis = m->pThis;
1627 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1628 AssertPtrReturn(pConsole, VERR_INVALID_POINTER);
1629
1630 Console *pCon = (Console *)pConsole;
1631 return pCon->i_hgcmLoadService(pszServiceLibrary, pszServiceName);
1632#else
1633 NOREF(pHlp); NOREF(pConsole); NOREF(pszServiceLibrary); NOREF(pszServiceName);
1634#endif
1635 return VERR_INVALID_STATE;
1636}
1637
1638/*static*/ DECLCALLBACK(int)
1639ExtPack::i_hlpLoadVDPlugin(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IVirtualBox) *pVirtualBox, const char *pszPluginLibrary)
1640{
1641#ifndef VBOX_COM_INPROC
1642 /*
1643 * Validate the input and get our bearings.
1644 */
1645 AssertPtrReturn(pszPluginLibrary, VERR_INVALID_POINTER);
1646
1647 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1648 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1649 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1650 AssertPtrReturn(m, VERR_INVALID_POINTER);
1651 ExtPack *pThis = m->pThis;
1652 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1653 AssertPtrReturn(pVirtualBox, VERR_INVALID_POINTER);
1654
1655 VirtualBox *pVBox = (VirtualBox *)pVirtualBox;
1656 return pVBox->i_loadVDPlugin(pszPluginLibrary);
1657#else
1658 NOREF(pHlp); NOREF(pVirtualBox);
1659#endif
1660 return VERR_INVALID_STATE;
1661}
1662
1663/*static*/ DECLCALLBACK(int)
1664ExtPack::i_hlpUnloadVDPlugin(PCVBOXEXTPACKHLP pHlp, VBOXEXTPACK_IF_CS(IVirtualBox) *pVirtualBox, const char *pszPluginLibrary)
1665{
1666#ifndef VBOX_COM_INPROC
1667 /*
1668 * Validate the input and get our bearings.
1669 */
1670 AssertPtrReturn(pszPluginLibrary, VERR_INVALID_POINTER);
1671
1672 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1673 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1674 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1675 AssertPtrReturn(m, VERR_INVALID_POINTER);
1676 ExtPack *pThis = m->pThis;
1677 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1678 AssertPtrReturn(pVirtualBox, VERR_INVALID_POINTER);
1679
1680 VirtualBox *pVBox = (VirtualBox *)pVirtualBox;
1681 return pVBox->i_unloadVDPlugin(pszPluginLibrary);
1682#else
1683 NOREF(pHlp); NOREF(pVirtualBox);
1684#endif
1685 return VERR_INVALID_STATE;
1686}
1687
1688/*static*/ DECLCALLBACK(int)
1689ExtPack::i_hlpReservedN(PCVBOXEXTPACKHLP pHlp)
1690{
1691 /*
1692 * Validate the input and get our bearings.
1693 */
1694 AssertPtrReturn(pHlp, VERR_INVALID_POINTER);
1695 AssertReturn(pHlp->u32Version == VBOXEXTPACKHLP_VERSION, VERR_INVALID_POINTER);
1696 ExtPack::Data *m = RT_FROM_CPP_MEMBER(pHlp, Data, Hlp);
1697 AssertPtrReturn(m, VERR_INVALID_POINTER);
1698 ExtPack *pThis = m->pThis;
1699 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1700
1701 return VERR_NOT_IMPLEMENTED;
1702}
1703
1704
1705
1706
1707HRESULT ExtPack::getName(com::Utf8Str &aName)
1708{
1709 aName = m->Desc.strName;
1710 return S_OK;
1711}
1712
1713HRESULT ExtPack::getDescription(com::Utf8Str &aDescription)
1714{
1715 aDescription = m->Desc.strDescription;
1716 return S_OK;
1717}
1718
1719HRESULT ExtPack::getVersion(com::Utf8Str &aVersion)
1720{
1721 aVersion = m->Desc.strVersion;
1722 return S_OK;
1723}
1724
1725HRESULT ExtPack::getRevision(ULONG *aRevision)
1726{
1727 *aRevision = m->Desc.uRevision;
1728 return S_OK;
1729}
1730
1731HRESULT ExtPack::getEdition(com::Utf8Str &aEdition)
1732{
1733 aEdition = m->Desc.strEdition;
1734 return S_OK;
1735}
1736
1737HRESULT ExtPack::getVRDEModule(com::Utf8Str &aVRDEModule)
1738{
1739 aVRDEModule = m->Desc.strVrdeModule;
1740 return S_OK;
1741}
1742
1743HRESULT ExtPack::getPlugIns(std::vector<ComPtr<IExtPackPlugIn> > &aPlugIns)
1744{
1745 /** @todo implement plug-ins. */
1746#ifdef VBOX_WITH_XPCOM
1747 NOREF(aPlugIns);
1748#endif
1749 NOREF(aPlugIns);
1750 ReturnComNotImplemented();
1751}
1752
1753HRESULT ExtPack::getUsable(BOOL *aUsable)
1754{
1755 *aUsable = m->fUsable;
1756 return S_OK;
1757}
1758
1759HRESULT ExtPack::getWhyUnusable(com::Utf8Str &aWhyUnusable)
1760{
1761 aWhyUnusable = m->strWhyUnusable;
1762 return S_OK;
1763}
1764
1765HRESULT ExtPack::getShowLicense(BOOL *aShowLicense)
1766{
1767 *aShowLicense = m->Desc.fShowLicense;
1768 return S_OK;
1769}
1770
1771HRESULT ExtPack::getLicense(com::Utf8Str &aLicense)
1772{
1773 Utf8Str strHtml("html");
1774 Utf8Str str("");
1775 return queryLicense(str, str, strHtml, aLicense);
1776}
1777
1778HRESULT ExtPack::queryLicense(const com::Utf8Str &aPreferredLocale, const com::Utf8Str &aPreferredLanguage,
1779 const com::Utf8Str &aFormat, com::Utf8Str &aLicenseText)
1780{
1781 HRESULT hrc = S_OK;
1782
1783 /*
1784 * Validate input.
1785 */
1786 if (aPreferredLocale.length() != 2 && aPreferredLocale.length() != 0)
1787 return setError(E_FAIL, tr("The preferred locale is a two character string or empty."));
1788
1789 if (aPreferredLanguage.length() != 2 && aPreferredLanguage.length() != 0)
1790 return setError(E_FAIL, tr("The preferred lanuage is a two character string or empty."));
1791
1792 if ( !aFormat.equals("html")
1793 && !aFormat.equals("rtf")
1794 && !aFormat.equals("txt"))
1795 return setError(E_FAIL, tr("The license format can only have the values 'html', 'rtf' and 'txt'."));
1796
1797 /*
1798 * Combine the options to form a file name before locking down anything.
1799 */
1800 char szName[sizeof(VBOX_EXTPACK_LICENSE_NAME_PREFIX "-de_DE.html") + 2];
1801 if (aPreferredLocale.isNotEmpty() && aPreferredLanguage.isNotEmpty())
1802 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s_%s.%s",
1803 aPreferredLocale.c_str(), aPreferredLanguage.c_str(), aFormat.c_str());
1804 else if (aPreferredLocale.isNotEmpty())
1805 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-%s.%s",
1806 aPreferredLocale.c_str(), aFormat.c_str());
1807 else if (aPreferredLanguage.isNotEmpty())
1808 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX "-_%s.%s",
1809 aPreferredLocale.c_str(), aFormat.c_str());
1810 else
1811 RTStrPrintf(szName, sizeof(szName), VBOX_EXTPACK_LICENSE_NAME_PREFIX ".%s",
1812 aFormat.c_str());
1813
1814 /*
1815 * Effectuate the query.
1816 */
1817 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS); /* paranoia */
1818
1819 if (!m->fUsable)
1820 hrc = setError(E_FAIL, tr("%s"), m->strWhyUnusable.c_str());
1821 else
1822 {
1823 char szPath[RTPATH_MAX];
1824 int vrc = RTPathJoin(szPath, sizeof(szPath), m->strExtPackPath.c_str(), szName);
1825 if (RT_SUCCESS(vrc))
1826 {
1827 void *pvFile;
1828 size_t cbFile;
1829 vrc = RTFileReadAllEx(szPath, 0, RTFOFF_MAX, RTFILE_RDALL_O_DENY_READ, &pvFile, &cbFile);
1830 if (RT_SUCCESS(vrc))
1831 {
1832 Bstr bstrLicense((const char *)pvFile, cbFile);
1833 if (bstrLicense.isNotEmpty())
1834 {
1835 aLicenseText = Utf8Str(bstrLicense);
1836 hrc = S_OK;
1837 }
1838 else
1839 hrc = setError(VBOX_E_IPRT_ERROR, tr("The license file '%s' is empty or contains invalid UTF-8 encoding"),
1840 szPath);
1841 RTFileReadAllFree(pvFile, cbFile);
1842 }
1843 else if (vrc == VERR_FILE_NOT_FOUND || vrc == VERR_PATH_NOT_FOUND)
1844 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("The license file '%s' was not found in extension pack '%s'"),
1845 szName, m->Desc.strName.c_str());
1846 else
1847 hrc = setError(VBOX_E_FILE_ERROR, tr("Failed to open the license file '%s': %Rrc"), szPath, vrc);
1848 }
1849 else
1850 hrc = setError(VBOX_E_IPRT_ERROR, tr("RTPathJoin failed: %Rrc"), vrc);
1851 }
1852 return hrc;
1853}
1854
1855HRESULT ExtPack::queryObject(const com::Utf8Str &aObjUuid, ComPtr<IUnknown> &aReturnInterface)
1856{
1857 com::Guid ObjectId;
1858 CheckComArgGuid(aObjUuid, ObjectId);
1859
1860 HRESULT hrc S_OK;
1861
1862 if ( m->pReg
1863 && m->pReg->pfnQueryObject)
1864 {
1865 void *pvUnknown = m->pReg->pfnQueryObject(m->pReg, ObjectId.raw());
1866 if (pvUnknown)
1867 aReturnInterface = (IUnknown *)pvUnknown;
1868 else
1869 hrc = E_NOINTERFACE;
1870 }
1871 else
1872 hrc = E_NOINTERFACE;
1873 return hrc;
1874}
1875
1876DEFINE_EMPTY_CTOR_DTOR(ExtPackManager)
1877
1878/**
1879 * Called by ComObjPtr::createObject when creating the object.
1880 *
1881 * Just initialize the basic object state, do the rest in init().
1882 *
1883 * @returns S_OK.
1884 */
1885HRESULT ExtPackManager::FinalConstruct()
1886{
1887 m = NULL;
1888 return S_OK;
1889}
1890
1891/**
1892 * Initializes the extension pack manager.
1893 *
1894 * @returns COM status code.
1895 * @param a_pVirtualBox Pointer to the VirtualBox object.
1896 * @param a_enmContext The context we're in.
1897 */
1898HRESULT ExtPackManager::initExtPackManager(VirtualBox *a_pVirtualBox, VBOXEXTPACKCTX a_enmContext)
1899{
1900 AutoInitSpan autoInitSpan(this);
1901 AssertReturn(autoInitSpan.isOk(), E_FAIL);
1902
1903 /*
1904 * Figure some stuff out before creating the instance data.
1905 */
1906 char szBaseDir[RTPATH_MAX];
1907 int rc = RTPathAppPrivateArchTop(szBaseDir, sizeof(szBaseDir));
1908 AssertLogRelRCReturn(rc, E_FAIL);
1909 rc = RTPathAppend(szBaseDir, sizeof(szBaseDir), VBOX_EXTPACK_INSTALL_DIR);
1910 AssertLogRelRCReturn(rc, E_FAIL);
1911
1912 char szCertificatDir[RTPATH_MAX];
1913 rc = RTPathAppPrivateNoArch(szCertificatDir, sizeof(szCertificatDir));
1914 AssertLogRelRCReturn(rc, E_FAIL);
1915 rc = RTPathAppend(szCertificatDir, sizeof(szCertificatDir), VBOX_EXTPACK_CERT_DIR);
1916 AssertLogRelRCReturn(rc, E_FAIL);
1917
1918 /*
1919 * Allocate and initialize the instance data.
1920 */
1921 m = new Data;
1922 m->strBaseDir = szBaseDir;
1923 m->strCertificatDirPath = szCertificatDir;
1924#if !defined(VBOX_COM_INPROC)
1925 m->pVirtualBox = a_pVirtualBox;
1926#endif
1927 m->enmContext = a_enmContext;
1928
1929 /*
1930 * Slurp in VBoxVMM which is used by VBoxPuelMain.
1931 */
1932#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_DARWIN)
1933 if (a_enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
1934 {
1935 int vrc = SUPR3HardenedLdrLoadAppPriv("VBoxVMM", &m->hVBoxVMM, RTLDRLOAD_FLAGS_GLOBAL, NULL);
1936 if (RT_FAILURE(vrc))
1937 m->hVBoxVMM = NIL_RTLDRMOD;
1938 /* cleanup in ::uninit()? */
1939 }
1940#endif
1941
1942 /*
1943 * Go looking for extensions. The RTDirOpen may fail if nothing has been
1944 * installed yet, or if root is paranoid and has revoked our access to them.
1945 *
1946 * We ASSUME that there are no files, directories or stuff in the directory
1947 * that exceed the max name length in RTDIRENTRYEX.
1948 */
1949 HRESULT hrc = S_OK;
1950 PRTDIR pDir;
1951 int vrc = RTDirOpen(&pDir, szBaseDir);
1952 if (RT_SUCCESS(vrc))
1953 {
1954 for (;;)
1955 {
1956 RTDIRENTRYEX Entry;
1957 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
1958 if (RT_FAILURE(vrc))
1959 {
1960 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
1961 break;
1962 }
1963 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
1964 && strcmp(Entry.szName, ".") != 0
1965 && strcmp(Entry.szName, "..") != 0
1966 && VBoxExtPackIsValidMangledName(Entry.szName) )
1967 {
1968 /*
1969 * All directories are extensions, the shall be nothing but
1970 * extensions in this subdirectory.
1971 */
1972 char szExtPackDir[RTPATH_MAX];
1973 vrc = RTPathJoin(szExtPackDir, sizeof(szExtPackDir), m->strBaseDir.c_str(), Entry.szName);
1974 AssertLogRelRC(vrc);
1975 if (RT_SUCCESS(vrc))
1976 {
1977 RTCString *pstrName = VBoxExtPackUnmangleName(Entry.szName, RTSTR_MAX);
1978 AssertLogRel(pstrName);
1979 if (pstrName)
1980 {
1981 ComObjPtr<ExtPack> NewExtPack;
1982 HRESULT hrc2 = NewExtPack.createObject();
1983 if (SUCCEEDED(hrc2))
1984 hrc2 = NewExtPack->initWithDir(a_enmContext, pstrName->c_str(), szExtPackDir);
1985 delete pstrName;
1986 if (SUCCEEDED(hrc2))
1987 m->llInstalledExtPacks.push_back(NewExtPack);
1988 else if (SUCCEEDED(rc))
1989 hrc = hrc2;
1990 }
1991 else
1992 hrc = E_UNEXPECTED;
1993 }
1994 else
1995 hrc = E_UNEXPECTED;
1996 }
1997 }
1998 RTDirClose(pDir);
1999 }
2000 /* else: ignore, the directory probably does not exist or something. */
2001
2002 if (SUCCEEDED(hrc))
2003 autoInitSpan.setSucceeded();
2004 return hrc;
2005}
2006
2007/**
2008 * COM cruft.
2009 */
2010void ExtPackManager::FinalRelease()
2011{
2012 uninit();
2013}
2014
2015/**
2016 * Do the actual cleanup.
2017 */
2018void ExtPackManager::uninit()
2019{
2020 /* Enclose the state transition Ready->InUninit->NotReady */
2021 AutoUninitSpan autoUninitSpan(this);
2022 if (!autoUninitSpan.uninitDone() && m != NULL)
2023 {
2024 delete m;
2025 m = NULL;
2026 }
2027}
2028
2029HRESULT ExtPackManager::getInstalledExtPacks(std::vector<ComPtr<IExtPack> > &aInstalledExtPacks)
2030{
2031 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2032
2033 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2034
2035
2036 SafeIfaceArray<IExtPack> SaExtPacks(m->llInstalledExtPacks);
2037 aInstalledExtPacks.resize(SaExtPacks.size());
2038 for(size_t i = 0; i < SaExtPacks.size(); ++i)
2039 aInstalledExtPacks[i] = SaExtPacks[i];
2040
2041 return S_OK;
2042}
2043
2044HRESULT ExtPackManager::find(const com::Utf8Str &aName, ComPtr<IExtPack> &aReturnData)
2045{
2046 HRESULT hrc = S_OK;
2047
2048 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2049
2050 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2051
2052 ComPtr<ExtPack> ptrExtPack = i_findExtPack(aName.c_str());
2053 if (!ptrExtPack.isNull())
2054 ptrExtPack.queryInterfaceTo(aReturnData.asOutParam());
2055 else
2056 hrc = VBOX_E_OBJECT_NOT_FOUND;
2057
2058 return hrc;
2059}
2060
2061HRESULT ExtPackManager::openExtPackFile(const com::Utf8Str &aPath, ComPtr<IExtPackFile> &aFile)
2062{
2063 AssertReturn(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON, E_UNEXPECTED);
2064
2065#if !defined(VBOX_COM_INPROC)
2066 /* The API can optionally take a ::SHA-256=<hex-digest> attribute at the
2067 end of the file name. This is just a temporary measure for
2068 backporting, in 4.2 we'll add another parameter to the method. */
2069 Utf8Str strTarball;
2070 Utf8Str strDigest;
2071 size_t offSha256 = aPath.find("::SHA-256=");
2072 if (offSha256 == Utf8Str::npos)
2073 strTarball = aPath;
2074 else
2075 {
2076 strTarball = aPath.substr(0, offSha256);
2077 strDigest = aPath.substr(offSha256 + sizeof("::SHA-256=") - 1);
2078 }
2079
2080 ComObjPtr<ExtPackFile> NewExtPackFile;
2081 HRESULT hrc = NewExtPackFile.createObject();
2082 if (SUCCEEDED(hrc))
2083 hrc = NewExtPackFile->initWithFile(strTarball.c_str(), strDigest.c_str(), this, m->pVirtualBox);
2084 if (SUCCEEDED(hrc))
2085 NewExtPackFile.queryInterfaceTo(aFile.asOutParam());
2086
2087 return hrc;
2088#else
2089 return E_NOTIMPL;
2090#endif
2091}
2092
2093HRESULT ExtPackManager::uninstall(const com::Utf8Str &aName, BOOL aForcedRemoval,
2094 const com::Utf8Str &aDisplayInfo, ComPtr<IProgress> &aProgress)
2095{
2096 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2097
2098#if !defined(VBOX_COM_INPROC)
2099
2100 HRESULT hrc;
2101 ExtPackUninstallTask *pTask = NULL;
2102 try
2103 {
2104 pTask = new ExtPackUninstallTask();
2105 hrc = pTask->Init(this, aName, aForcedRemoval != FALSE, aDisplayInfo);
2106 if (SUCCEEDED(hrc))
2107 {
2108 ComPtr<Progress> ptrProgress = pTask->ptrProgress;
2109 hrc = pTask->createThread(NULL, RTTHREADTYPE_DEFAULT);
2110 pTask = NULL; /* always consumed by createThread */
2111 if (SUCCEEDED(hrc))
2112 hrc = ptrProgress.queryInterfaceTo(aProgress.asOutParam());
2113 else
2114 hrc = setError(VBOX_E_IPRT_ERROR,
2115 tr("Starting thread for an extension pack uninstallation failed with %Rrc"), hrc);
2116 }
2117 else
2118 hrc = setError(VBOX_E_IPRT_ERROR,
2119 tr("Looks like creating a progress object for ExtraPackUninstallTask object failed"));
2120 }
2121 catch (std::bad_alloc &)
2122 {
2123 hrc = E_OUTOFMEMORY;
2124 }
2125 catch (HRESULT hrcXcpt)
2126 {
2127 LogFlowThisFunc(("Exception was caught in the function ExtPackManager::uninstall()\n"));
2128 hrc = hrcXcpt;
2129 }
2130 if (pTask)
2131 delete pTask;
2132 return hrc;
2133#else
2134 return E_NOTIMPL;
2135#endif
2136}
2137
2138HRESULT ExtPackManager::cleanup(void)
2139{
2140 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2141
2142 AutoCaller autoCaller(this);
2143 HRESULT hrc = autoCaller.rc();
2144 if (SUCCEEDED(hrc))
2145 {
2146 /*
2147 * Run the set-uid-to-root binary that performs the cleanup.
2148 *
2149 * Take the write lock to prevent conflicts with other calls to this
2150 * VBoxSVC instance.
2151 */
2152 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2153 hrc = i_runSetUidToRootHelper(NULL,
2154 "cleanup",
2155 "--base-dir", m->strBaseDir.c_str(),
2156 (const char *)NULL);
2157 }
2158
2159 return hrc;
2160}
2161
2162HRESULT ExtPackManager::queryAllPlugInsForFrontend(const com::Utf8Str &aFrontendName, std::vector<com::Utf8Str> &aPlugInModules)
2163{
2164 NOREF(aFrontendName);
2165 aPlugInModules.resize(0);
2166 return S_OK;
2167}
2168
2169HRESULT ExtPackManager::isExtPackUsable(const com::Utf8Str &aName, BOOL *aUsable)
2170{
2171 *aUsable = i_isExtPackUsable(aName.c_str());
2172 return S_OK;
2173}
2174
2175/**
2176 * Finds the success indicator string in the stderr output ofr hte helper app.
2177 *
2178 * @returns Pointer to the indicator.
2179 * @param psz The stderr output string. Can be NULL.
2180 * @param cch The size of the string.
2181 */
2182static char *findSuccessIndicator(char *psz, size_t cch)
2183{
2184 static const char s_szSuccessInd[] = "rcExit=RTEXITCODE_SUCCESS";
2185 Assert(!cch || strlen(psz) == cch);
2186 if (cch < sizeof(s_szSuccessInd) - 1)
2187 return NULL;
2188 char *pszInd = &psz[cch - sizeof(s_szSuccessInd) + 1];
2189 if (strcmp(s_szSuccessInd, pszInd))
2190 return NULL;
2191 return pszInd;
2192}
2193
2194/**
2195 * Runs the helper application that does the privileged operations.
2196 *
2197 * @returns S_OK or a failure status with error information set.
2198 * @param a_pstrDisplayInfo Platform specific display info hacks.
2199 * @param a_pszCommand The command to execute.
2200 * @param ... The argument strings that goes along with the
2201 * command. Maximum is about 16. Terminated by a
2202 * NULL.
2203 */
2204HRESULT ExtPackManager::i_runSetUidToRootHelper(Utf8Str const *a_pstrDisplayInfo, const char *a_pszCommand, ...)
2205{
2206 /*
2207 * Calculate the path to the helper application.
2208 */
2209 char szExecName[RTPATH_MAX];
2210 int vrc = RTPathAppPrivateArch(szExecName, sizeof(szExecName));
2211 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2212
2213 vrc = RTPathAppend(szExecName, sizeof(szExecName), VBOX_EXTPACK_HELPER_NAME);
2214 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2215
2216 /*
2217 * Convert the variable argument list to a RTProcCreate argument vector.
2218 */
2219 const char *apszArgs[20];
2220 unsigned cArgs = 0;
2221
2222 LogRel(("ExtPack: Executing '%s'", szExecName));
2223 apszArgs[cArgs++] = &szExecName[0];
2224
2225 if ( a_pstrDisplayInfo
2226 && a_pstrDisplayInfo->isNotEmpty())
2227 {
2228 LogRel((" '--display-info-hack' '%s'", a_pstrDisplayInfo->c_str()));
2229 apszArgs[cArgs++] = "--display-info-hack";
2230 apszArgs[cArgs++] = a_pstrDisplayInfo->c_str();
2231 }
2232
2233 LogRel(("'%s'", a_pszCommand));
2234 apszArgs[cArgs++] = a_pszCommand;
2235
2236 va_list va;
2237 va_start(va, a_pszCommand);
2238 const char *pszLastArg;
2239 for (;;)
2240 {
2241 AssertReturn(cArgs < RT_ELEMENTS(apszArgs) - 1, E_UNEXPECTED);
2242 pszLastArg = va_arg(va, const char *);
2243 if (!pszLastArg)
2244 break;
2245 LogRel((" '%s'", pszLastArg));
2246 apszArgs[cArgs++] = pszLastArg;
2247 };
2248 va_end(va);
2249
2250 LogRel(("\n"));
2251 apszArgs[cArgs] = NULL;
2252
2253 /*
2254 * Create a PIPE which we attach to stderr so that we can read the error
2255 * message on failure and report it back to the caller.
2256 */
2257 RTPIPE hPipeR;
2258 RTHANDLE hStdErrPipe;
2259 hStdErrPipe.enmType = RTHANDLETYPE_PIPE;
2260 vrc = RTPipeCreate(&hPipeR, &hStdErrPipe.u.hPipe, RTPIPE_C_INHERIT_WRITE);
2261 AssertLogRelRCReturn(vrc, E_UNEXPECTED);
2262
2263 /*
2264 * Spawn the process.
2265 */
2266 HRESULT hrc;
2267 RTPROCESS hProcess;
2268 vrc = RTProcCreateEx(szExecName,
2269 apszArgs,
2270 RTENV_DEFAULT,
2271 0 /*fFlags*/,
2272 NULL /*phStdIn*/,
2273 NULL /*phStdOut*/,
2274 &hStdErrPipe,
2275 NULL /*pszAsUser*/,
2276 NULL /*pszPassword*/,
2277 &hProcess);
2278 if (RT_SUCCESS(vrc))
2279 {
2280 vrc = RTPipeClose(hStdErrPipe.u.hPipe);
2281 hStdErrPipe.u.hPipe = NIL_RTPIPE;
2282
2283 /*
2284 * Read the pipe output until the process completes.
2285 */
2286 RTPROCSTATUS ProcStatus = { -42, RTPROCEXITREASON_ABEND };
2287 size_t cbStdErrBuf = 0;
2288 size_t offStdErrBuf = 0;
2289 char *pszStdErrBuf = NULL;
2290 do
2291 {
2292 /*
2293 * Service the pipe. Block waiting for output or the pipe breaking
2294 * when the process terminates.
2295 */
2296 if (hPipeR != NIL_RTPIPE)
2297 {
2298 char achBuf[1024];
2299 size_t cbRead;
2300 vrc = RTPipeReadBlocking(hPipeR, achBuf, sizeof(achBuf), &cbRead);
2301 if (RT_SUCCESS(vrc))
2302 {
2303 /* grow the buffer? */
2304 size_t cbBufReq = offStdErrBuf + cbRead + 1;
2305 if ( cbBufReq > cbStdErrBuf
2306 && cbBufReq < _256K)
2307 {
2308 size_t cbNew = RT_ALIGN_Z(cbBufReq, 16); // 1024
2309 void *pvNew = RTMemRealloc(pszStdErrBuf, cbNew);
2310 if (pvNew)
2311 {
2312 pszStdErrBuf = (char *)pvNew;
2313 cbStdErrBuf = cbNew;
2314 }
2315 }
2316
2317 /* append if we've got room. */
2318 if (cbBufReq <= cbStdErrBuf)
2319 {
2320 memcpy(&pszStdErrBuf[offStdErrBuf], achBuf, cbRead);
2321 offStdErrBuf = offStdErrBuf + cbRead;
2322 pszStdErrBuf[offStdErrBuf] = '\0';
2323 }
2324 }
2325 else
2326 {
2327 AssertLogRelMsg(vrc == VERR_BROKEN_PIPE, ("%Rrc\n", vrc));
2328 RTPipeClose(hPipeR);
2329 hPipeR = NIL_RTPIPE;
2330 }
2331 }
2332
2333 /*
2334 * Service the process. Block if we have no pipe.
2335 */
2336 if (hProcess != NIL_RTPROCESS)
2337 {
2338 vrc = RTProcWait(hProcess,
2339 hPipeR == NIL_RTPIPE ? RTPROCWAIT_FLAGS_BLOCK : RTPROCWAIT_FLAGS_NOBLOCK,
2340 &ProcStatus);
2341 if (RT_SUCCESS(vrc))
2342 hProcess = NIL_RTPROCESS;
2343 else
2344 AssertLogRelMsgStmt(vrc == VERR_PROCESS_RUNNING, ("%Rrc\n", vrc), hProcess = NIL_RTPROCESS);
2345 }
2346 } while ( hPipeR != NIL_RTPIPE
2347 || hProcess != NIL_RTPROCESS);
2348
2349 LogRel(("ExtPack: enmReason=%d iStatus=%d stderr='%s'\n",
2350 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : ""));
2351
2352 /*
2353 * Look for rcExit=RTEXITCODE_SUCCESS at the end of the error output,
2354 * cut it as it is only there to attest the success.
2355 */
2356 if (offStdErrBuf > 0)
2357 {
2358 RTStrStripR(pszStdErrBuf);
2359 offStdErrBuf = strlen(pszStdErrBuf);
2360 }
2361
2362 char *pszSuccessInd = findSuccessIndicator(pszStdErrBuf, offStdErrBuf);
2363 if (pszSuccessInd)
2364 {
2365 *pszSuccessInd = '\0';
2366 offStdErrBuf = pszSuccessInd - pszStdErrBuf;
2367 }
2368 else if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2369 && ProcStatus.iStatus == 0)
2370 ProcStatus.iStatus = offStdErrBuf ? 667 : 666;
2371
2372 /*
2373 * Compose the status code and, on failure, error message.
2374 */
2375 if ( ProcStatus.enmReason == RTPROCEXITREASON_NORMAL
2376 && ProcStatus.iStatus == 0)
2377 hrc = S_OK;
2378 else if (ProcStatus.enmReason == RTPROCEXITREASON_NORMAL)
2379 {
2380 AssertMsg(ProcStatus.iStatus != 0, ("%s\n", pszStdErrBuf));
2381 hrc = setError(E_FAIL, tr("The installer failed with exit code %d: %s"),
2382 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2383 }
2384 else if (ProcStatus.enmReason == RTPROCEXITREASON_SIGNAL)
2385 hrc = setError(E_UNEXPECTED, tr("The installer was killed by signal #d (stderr: %s)"),
2386 ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2387 else if (ProcStatus.enmReason == RTPROCEXITREASON_ABEND)
2388 hrc = setError(E_UNEXPECTED, tr("The installer aborted abnormally (stderr: %s)"),
2389 offStdErrBuf ? pszStdErrBuf : "");
2390 else
2391 hrc = setError(E_UNEXPECTED, tr("internal error: enmReason=%d iStatus=%d stderr='%s'"),
2392 ProcStatus.enmReason, ProcStatus.iStatus, offStdErrBuf ? pszStdErrBuf : "");
2393
2394 RTMemFree(pszStdErrBuf);
2395 }
2396 else
2397 hrc = setError(VBOX_E_IPRT_ERROR, tr("Failed to launch the helper application '%s' (%Rrc)"), szExecName, vrc);
2398
2399 RTPipeClose(hPipeR);
2400 RTPipeClose(hStdErrPipe.u.hPipe);
2401
2402 return hrc;
2403}
2404
2405/**
2406 * Finds an installed extension pack.
2407 *
2408 * @returns Pointer to the extension pack if found, NULL if not. (No reference
2409 * counting problem here since the caller must be holding the lock.)
2410 * @param a_pszName The name of the extension pack.
2411 */
2412ExtPack *ExtPackManager::i_findExtPack(const char *a_pszName)
2413{
2414 size_t cchName = strlen(a_pszName);
2415
2416 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2417 it != m->llInstalledExtPacks.end();
2418 ++it)
2419 {
2420 ExtPack::Data *pExtPackData = (*it)->m;
2421 if ( pExtPackData
2422 && pExtPackData->Desc.strName.length() == cchName
2423 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2424 return (*it);
2425 }
2426 return NULL;
2427}
2428
2429/**
2430 * Removes an installed extension pack from the internal list.
2431 *
2432 * The package is expected to exist!
2433 *
2434 * @param a_pszName The name of the extension pack.
2435 */
2436void ExtPackManager::i_removeExtPack(const char *a_pszName)
2437{
2438 size_t cchName = strlen(a_pszName);
2439
2440 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2441 it != m->llInstalledExtPacks.end();
2442 ++it)
2443 {
2444 ExtPack::Data *pExtPackData = (*it)->m;
2445 if ( pExtPackData
2446 && pExtPackData->Desc.strName.length() == cchName
2447 && pExtPackData->Desc.strName.equalsIgnoreCase(a_pszName))
2448 {
2449 m->llInstalledExtPacks.erase(it);
2450 return;
2451 }
2452 }
2453 AssertMsgFailed(("%s\n", a_pszName));
2454}
2455
2456#if !defined(VBOX_COM_INPROC)
2457/**
2458 * Refreshes the specified extension pack.
2459 *
2460 * This may remove the extension pack from the list, so any non-smart pointers
2461 * to the extension pack object may become invalid.
2462 *
2463 * @returns S_OK and *a_ppExtPack on success, COM status code and error
2464 * message on failure. Note that *a_ppExtPack can be NULL.
2465 *
2466 * @param a_pszName The extension to update..
2467 * @param a_fUnusableIsError If @c true, report an unusable extension pack
2468 * as an error.
2469 * @param a_ppExtPack Where to store the pointer to the extension
2470 * pack of it is still around after the refresh.
2471 * This is optional.
2472 *
2473 * @remarks Caller holds the extension manager lock.
2474 * @remarks Only called in VBoxSVC.
2475 */
2476HRESULT ExtPackManager::i_refreshExtPack(const char *a_pszName, bool a_fUnusableIsError, ExtPack **a_ppExtPack)
2477{
2478 Assert(m->pVirtualBox != NULL); /* Only called from VBoxSVC. */
2479
2480 HRESULT hrc;
2481 ExtPack *pExtPack = i_findExtPack(a_pszName);
2482 if (pExtPack)
2483 {
2484 /*
2485 * Refresh existing object.
2486 */
2487 bool fCanDelete;
2488 hrc = pExtPack->i_refresh(&fCanDelete);
2489 if (SUCCEEDED(hrc))
2490 {
2491 if (fCanDelete)
2492 {
2493 i_removeExtPack(a_pszName);
2494 pExtPack = NULL;
2495 }
2496 }
2497 }
2498 else
2499 {
2500 /*
2501 * Do this check here, otherwise VBoxExtPackCalcDir() will fail with a strange
2502 * error.
2503 */
2504 bool fValid = VBoxExtPackIsValidName(a_pszName);
2505 if (!fValid)
2506 return setError(E_FAIL, "Invalid extension pack name specified");
2507
2508 /*
2509 * Does the dir exist? Make some special effort to deal with case
2510 * sensitivie file systems (a_pszName is case insensitive and mangled).
2511 */
2512 char szDir[RTPATH_MAX];
2513 int vrc = VBoxExtPackCalcDir(szDir, sizeof(szDir), m->strBaseDir.c_str(), a_pszName);
2514 AssertLogRelRCReturn(vrc, E_FAIL);
2515
2516 RTDIRENTRYEX Entry;
2517 RTFSOBJINFO ObjInfo;
2518 vrc = RTPathQueryInfoEx(szDir, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2519 bool fExists = RT_SUCCESS(vrc) && RTFS_IS_DIRECTORY(ObjInfo.Attr.fMode);
2520 if (!fExists)
2521 {
2522 PRTDIR pDir;
2523 vrc = RTDirOpen(&pDir, m->strBaseDir.c_str());
2524 if (RT_SUCCESS(vrc))
2525 {
2526 const char *pszMangledName = RTPathFilename(szDir);
2527 for (;;)
2528 {
2529 vrc = RTDirReadEx(pDir, &Entry, NULL /*pcbDirEntry*/, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
2530 if (RT_FAILURE(vrc))
2531 {
2532 AssertLogRelMsg(vrc == VERR_NO_MORE_FILES, ("%Rrc\n", vrc));
2533 break;
2534 }
2535 if ( RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode)
2536 && !RTStrICmp(Entry.szName, pszMangledName))
2537 {
2538 /*
2539 * The installed extension pack has a uses different case.
2540 * Update the name and directory variables.
2541 */
2542 vrc = RTPathJoin(szDir, sizeof(szDir), m->strBaseDir.c_str(), Entry.szName); /* not really necessary */
2543 AssertLogRelRCReturnStmt(vrc, RTDirClose(pDir), E_UNEXPECTED);
2544 a_pszName = Entry.szName;
2545 fExists = true;
2546 break;
2547 }
2548 }
2549 RTDirClose(pDir);
2550 }
2551 }
2552 if (fExists)
2553 {
2554 /*
2555 * We've got something, create a new extension pack object for it.
2556 */
2557 ComObjPtr<ExtPack> ptrNewExtPack;
2558 hrc = ptrNewExtPack.createObject();
2559 if (SUCCEEDED(hrc))
2560 hrc = ptrNewExtPack->initWithDir(m->enmContext, a_pszName, szDir);
2561 if (SUCCEEDED(hrc))
2562 {
2563 m->llInstalledExtPacks.push_back(ptrNewExtPack);
2564 if (ptrNewExtPack->m->fUsable)
2565 LogRel(("ExtPackManager: Found extension pack '%s'.\n", a_pszName));
2566 else
2567 LogRel(("ExtPackManager: Found bad extension pack '%s': %s\n",
2568 a_pszName, ptrNewExtPack->m->strWhyUnusable.c_str() ));
2569 pExtPack = ptrNewExtPack;
2570 }
2571 }
2572 else
2573 hrc = S_OK;
2574 }
2575
2576 /*
2577 * Report error if not usable, if that is desired.
2578 */
2579 if ( SUCCEEDED(hrc)
2580 && pExtPack
2581 && a_fUnusableIsError
2582 && !pExtPack->m->fUsable)
2583 hrc = setError(E_FAIL, "%s", pExtPack->m->strWhyUnusable.c_str());
2584
2585 if (a_ppExtPack)
2586 *a_ppExtPack = pExtPack;
2587 return hrc;
2588}
2589
2590/**
2591 * Checks if there are any running VMs.
2592 *
2593 * This is called when uninstalling or replacing an extension pack.
2594 *
2595 * @returns true / false
2596 */
2597bool ExtPackManager::i_areThereAnyRunningVMs(void) const
2598{
2599 Assert(m->pVirtualBox != NULL); /* Only called from VBoxSVC. */
2600
2601 /*
2602 * Get list of machines and their states.
2603 */
2604 com::SafeIfaceArray<IMachine> SaMachines;
2605 HRESULT hrc = m->pVirtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(SaMachines));
2606 if (SUCCEEDED(hrc))
2607 {
2608 com::SafeArray<MachineState_T> SaStates;
2609 hrc = m->pVirtualBox->GetMachineStates(ComSafeArrayAsInParam(SaMachines), ComSafeArrayAsOutParam(SaStates));
2610 if (SUCCEEDED(hrc))
2611 {
2612 /*
2613 * Scan the two parallel arrays for machines in the running state.
2614 */
2615 Assert(SaStates.size() == SaMachines.size());
2616 for (size_t i = 0; i < SaMachines.size(); ++i)
2617 if (SaMachines[i] && Global::IsOnline(SaStates[i]))
2618 return true;
2619 }
2620 }
2621 return false;
2622}
2623
2624/**
2625 * Worker for IExtPackFile::Install.
2626 *
2627 * Called on a worker thread via doInstallThreadProc.
2628 *
2629 * @returns COM status code.
2630 * @param a_pExtPackFile The extension pack file, caller checks that
2631 * it's usable.
2632 * @param a_fReplace Whether to replace any existing extpack or just
2633 * fail.
2634 * @param a_pstrDisplayInfo Host specific display information hacks.
2635 * @param a_ppProgress Where to return a progress object some day. Can
2636 * be NULL.
2637 */
2638HRESULT ExtPackManager::i_doInstall(ExtPackFile *a_pExtPackFile, bool a_fReplace, Utf8Str const *a_pstrDisplayInfo)
2639{
2640 AssertReturn(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON, E_UNEXPECTED);
2641 RTCString const * const pStrName = &a_pExtPackFile->m->Desc.strName;
2642 RTCString const * const pStrTarball = &a_pExtPackFile->m->strExtPackFile;
2643 RTCString const * const pStrTarballDigest = &a_pExtPackFile->m->strDigest;
2644
2645 AutoCaller autoCaller(this);
2646 HRESULT hrc = autoCaller.rc();
2647 if (SUCCEEDED(hrc))
2648 {
2649 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2650
2651 /*
2652 * Refresh the data we have on the extension pack as it
2653 * may be made stale by direct meddling or some other user.
2654 */
2655 ExtPack *pExtPack;
2656 hrc = i_refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2657 if (SUCCEEDED(hrc))
2658 {
2659 if (pExtPack && a_fReplace)
2660 {
2661 if (!i_areThereAnyRunningVMs())
2662 hrc = pExtPack->i_callUninstallHookAndClose(m->pVirtualBox, false /*a_ForcedRemoval*/);
2663 else
2664 {
2665 LogRel(("Install extension pack '%s' failed because at least one VM is still running.", pStrName->c_str()));
2666 hrc = setError(E_FAIL, tr("Install extension pack '%s' failed because at least one VM is still running"),
2667 pStrName->c_str());
2668 }
2669 }
2670 else if (pExtPack)
2671 hrc = setError(E_FAIL,
2672 tr("Extension pack '%s' is already installed."
2673 " In case of a reinstallation, please uninstall it first"),
2674 pStrName->c_str());
2675 }
2676 if (SUCCEEDED(hrc))
2677 {
2678 /*
2679 * Run the privileged helper binary that performs the actual
2680 * installation. Then create an object for the packet (we do this
2681 * even on failure, to be on the safe side).
2682 */
2683 hrc = i_runSetUidToRootHelper(a_pstrDisplayInfo,
2684 "install",
2685 "--base-dir", m->strBaseDir.c_str(),
2686 "--cert-dir", m->strCertificatDirPath.c_str(),
2687 "--name", pStrName->c_str(),
2688 "--tarball", pStrTarball->c_str(),
2689 "--sha-256", pStrTarballDigest->c_str(),
2690 pExtPack ? "--replace" : (const char *)NULL,
2691 (const char *)NULL);
2692 if (SUCCEEDED(hrc))
2693 {
2694 hrc = i_refreshExtPack(pStrName->c_str(), true /*a_fUnusableIsError*/, &pExtPack);
2695 if (SUCCEEDED(hrc) && pExtPack)
2696 {
2697 RTERRINFOSTATIC ErrInfo;
2698 RTErrInfoInitStatic(&ErrInfo);
2699 pExtPack->i_callInstalledHook(m->pVirtualBox, &autoLock, &ErrInfo.Core);
2700 if (RT_SUCCESS(ErrInfo.Core.rc))
2701 LogRel(("ExtPackManager: Successfully installed extension pack '%s'.\n", pStrName->c_str()));
2702 else
2703 {
2704 LogRel(("ExtPackManager: Installed hook for '%s' failed: %Rrc - %s\n",
2705 pStrName->c_str(), ErrInfo.Core.rc, ErrInfo.Core.pszMsg));
2706
2707 /*
2708 * Uninstall the extpack if the error indicates that.
2709 */
2710 if (ErrInfo.Core.rc == VERR_EXTPACK_UNSUPPORTED_HOST_UNINSTALL)
2711 i_runSetUidToRootHelper(a_pstrDisplayInfo,
2712 "uninstall",
2713 "--base-dir", m->strBaseDir.c_str(),
2714 "--name", pStrName->c_str(),
2715 "--forced",
2716 (const char *)NULL);
2717 hrc = setError(E_FAIL, tr("The installation hook failed: %Rrc - %s"),
2718 ErrInfo.Core.rc, ErrInfo.Core.pszMsg);
2719 }
2720 }
2721 else if (SUCCEEDED(hrc))
2722 hrc = setError(E_FAIL, tr("Installing extension pack '%s' failed under mysterious circumstances"),
2723 pStrName->c_str());
2724 }
2725 else
2726 {
2727 ErrorInfoKeeper Eik;
2728 i_refreshExtPack(pStrName->c_str(), false /*a_fUnusableIsError*/, NULL);
2729 }
2730 }
2731
2732 /*
2733 * Do VirtualBoxReady callbacks now for any freshly installed
2734 * extension pack (old ones will not be called).
2735 */
2736 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
2737 {
2738 autoLock.release();
2739 i_callAllVirtualBoxReadyHooks();
2740 }
2741 }
2742
2743 return hrc;
2744}
2745
2746/**
2747 * Worker for IExtPackManager::Uninstall.
2748 *
2749 * Called on a worker thread via doUninstallThreadProc.
2750 *
2751 * @returns COM status code.
2752 * @param a_pstrName The name of the extension pack to uninstall.
2753 * @param a_fForcedRemoval Whether to be skip and ignore certain bits of
2754 * the extpack feedback. To deal with misbehaving
2755 * extension pack hooks.
2756 * @param a_pstrDisplayInfo Host specific display information hacks.
2757 */
2758HRESULT ExtPackManager::i_doUninstall(Utf8Str const *a_pstrName, bool a_fForcedRemoval, Utf8Str const *a_pstrDisplayInfo)
2759{
2760 Assert(m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON);
2761
2762 AutoCaller autoCaller(this);
2763 HRESULT hrc = autoCaller.rc();
2764
2765 if (SUCCEEDED(hrc))
2766 {
2767 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2768 if (a_fForcedRemoval || !i_areThereAnyRunningVMs())
2769 {
2770 /*
2771 * Refresh the data we have on the extension pack as it may be made
2772 * stale by direct meddling or some other user.
2773 */
2774 ExtPack *pExtPack;
2775 hrc = i_refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2776 if (SUCCEEDED(hrc))
2777 {
2778 if (!pExtPack)
2779 {
2780 LogRel(("ExtPackManager: Extension pack '%s' is not installed, so nothing to uninstall.\n", a_pstrName->c_str()));
2781 hrc = S_OK; /* nothing to uninstall */
2782 }
2783 else
2784 {
2785 /*
2786 * Call the uninstall hook and unload the main dll.
2787 */
2788 hrc = pExtPack->i_callUninstallHookAndClose(m->pVirtualBox, a_fForcedRemoval);
2789 if (SUCCEEDED(hrc))
2790 {
2791 /*
2792 * Run the set-uid-to-root binary that performs the
2793 * uninstallation. Then refresh the object.
2794 *
2795 * This refresh is theorically subject to races, but it's of
2796 * the don't-do-that variety.
2797 */
2798 const char *pszForcedOpt = a_fForcedRemoval ? "--forced" : NULL;
2799 hrc = i_runSetUidToRootHelper(a_pstrDisplayInfo,
2800 "uninstall",
2801 "--base-dir", m->strBaseDir.c_str(),
2802 "--name", a_pstrName->c_str(),
2803 pszForcedOpt, /* Last as it may be NULL. */
2804 (const char *)NULL);
2805 if (SUCCEEDED(hrc))
2806 {
2807 hrc = i_refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, &pExtPack);
2808 if (SUCCEEDED(hrc))
2809 {
2810 if (!pExtPack)
2811 LogRel(("ExtPackManager: Successfully uninstalled extension pack '%s'.\n", a_pstrName->c_str()));
2812 else
2813 hrc = setError(E_FAIL,
2814 tr("Uninstall extension pack '%s' failed under mysterious circumstances"),
2815 a_pstrName->c_str());
2816 }
2817 }
2818 else
2819 {
2820 ErrorInfoKeeper Eik;
2821 i_refreshExtPack(a_pstrName->c_str(), false /*a_fUnusableIsError*/, NULL);
2822 }
2823 }
2824 }
2825 }
2826 }
2827 else
2828 {
2829 LogRel(("Uninstall extension pack '%s' failed because at least one VM is still running.", a_pstrName->c_str()));
2830 hrc = setError(E_FAIL, tr("Uninstall extension pack '%s' failed because at least one VM is still running"),
2831 a_pstrName->c_str());
2832 }
2833
2834 /*
2835 * Do VirtualBoxReady callbacks now for any freshly installed
2836 * extension pack (old ones will not be called).
2837 */
2838 if (m->enmContext == VBOXEXTPACKCTX_PER_USER_DAEMON)
2839 {
2840 autoLock.release();
2841 i_callAllVirtualBoxReadyHooks();
2842 }
2843 }
2844
2845 return hrc;
2846}
2847
2848
2849/**
2850 * Calls the pfnVirtualBoxReady hook for all working extension packs.
2851 *
2852 * @remarks The caller must not hold any locks.
2853 */
2854void ExtPackManager::i_callAllVirtualBoxReadyHooks(void)
2855{
2856 AutoCaller autoCaller(this);
2857 HRESULT hrc = autoCaller.rc();
2858 if (FAILED(hrc))
2859 return;
2860 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2861 ComPtr<ExtPackManager> ptrSelfRef = this;
2862
2863 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2864 it != m->llInstalledExtPacks.end();
2865 /* advancing below */)
2866 {
2867 if ((*it)->i_callVirtualBoxReadyHook(m->pVirtualBox, &autoLock))
2868 it = m->llInstalledExtPacks.begin();
2869 else
2870 ++it;
2871 }
2872}
2873#endif
2874
2875/**
2876 * Calls the pfnConsoleReady hook for all working extension packs.
2877 *
2878 * @param a_pConsole The console interface.
2879 * @remarks The caller must not hold any locks.
2880 */
2881void ExtPackManager::i_callAllConsoleReadyHooks(IConsole *a_pConsole)
2882{
2883 AutoCaller autoCaller(this);
2884 HRESULT hrc = autoCaller.rc();
2885 if (FAILED(hrc))
2886 return;
2887 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2888 ComPtr<ExtPackManager> ptrSelfRef = this;
2889
2890 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
2891 it != m->llInstalledExtPacks.end();
2892 /* advancing below */)
2893 {
2894 if ((*it)->i_callConsoleReadyHook(a_pConsole, &autoLock))
2895 it = m->llInstalledExtPacks.begin();
2896 else
2897 ++it;
2898 }
2899}
2900
2901#if !defined(VBOX_COM_INPROC)
2902/**
2903 * Calls the pfnVMCreated hook for all working extension packs.
2904 *
2905 * @param a_pMachine The machine interface of the new VM.
2906 */
2907void ExtPackManager::i_callAllVmCreatedHooks(IMachine *a_pMachine)
2908{
2909 AutoCaller autoCaller(this);
2910 HRESULT hrc = autoCaller.rc();
2911 if (FAILED(hrc))
2912 return;
2913 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2914 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2915 ExtPackList llExtPacks = m->llInstalledExtPacks;
2916
2917 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
2918 (*it)->i_callVmCreatedHook(m->pVirtualBox, a_pMachine, &autoLock);
2919}
2920#endif
2921
2922/**
2923 * Calls the pfnVMConfigureVMM hook for all working extension packs.
2924 *
2925 * @returns VBox status code. Stops on the first failure, expecting the caller
2926 * to signal this to the caller of the CFGM constructor.
2927 * @param a_pConsole The console interface for the VM.
2928 * @param a_pVM The VM handle.
2929 */
2930int ExtPackManager::i_callAllVmConfigureVmmHooks(IConsole *a_pConsole, PVM a_pVM)
2931{
2932 AutoCaller autoCaller(this);
2933 HRESULT hrc = autoCaller.rc();
2934 if (FAILED(hrc))
2935 return Global::vboxStatusCodeFromCOM(hrc);
2936 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2937 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2938 ExtPackList llExtPacks = m->llInstalledExtPacks;
2939
2940 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
2941 {
2942 int vrc;
2943 (*it)->i_callVmConfigureVmmHook(a_pConsole, a_pVM, &autoLock, &vrc);
2944 if (RT_FAILURE(vrc))
2945 return vrc;
2946 }
2947
2948 return VINF_SUCCESS;
2949}
2950
2951/**
2952 * Calls the pfnVMPowerOn hook for all working extension packs.
2953 *
2954 * @returns VBox status code. Stops on the first failure, expecting the caller
2955 * to not power on the VM.
2956 * @param a_pConsole The console interface for the VM.
2957 * @param a_pVM The VM handle.
2958 */
2959int ExtPackManager::i_callAllVmPowerOnHooks(IConsole *a_pConsole, PVM a_pVM)
2960{
2961 AutoCaller autoCaller(this);
2962 HRESULT hrc = autoCaller.rc();
2963 if (FAILED(hrc))
2964 return Global::vboxStatusCodeFromCOM(hrc);
2965 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2966 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2967 ExtPackList llExtPacks = m->llInstalledExtPacks;
2968
2969 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
2970 {
2971 int vrc;
2972 (*it)->i_callVmPowerOnHook(a_pConsole, a_pVM, &autoLock, &vrc);
2973 if (RT_FAILURE(vrc))
2974 return vrc;
2975 }
2976
2977 return VINF_SUCCESS;
2978}
2979
2980/**
2981 * Calls the pfnVMPowerOff hook for all working extension packs.
2982 *
2983 * @param a_pConsole The console interface for the VM.
2984 * @param a_pVM The VM handle. Can be NULL.
2985 */
2986void ExtPackManager::i_callAllVmPowerOffHooks(IConsole *a_pConsole, PVM a_pVM)
2987{
2988 AutoCaller autoCaller(this);
2989 HRESULT hrc = autoCaller.rc();
2990 if (FAILED(hrc))
2991 return;
2992 AutoWriteLock autoLock(this COMMA_LOCKVAL_SRC_POS);
2993 ComPtr<ExtPackManager> ptrSelfRef = this; /* paranoia */
2994 ExtPackList llExtPacks = m->llInstalledExtPacks;
2995
2996 for (ExtPackList::iterator it = llExtPacks.begin(); it != llExtPacks.end(); ++it)
2997 (*it)->i_callVmPowerOffHook(a_pConsole, a_pVM, &autoLock);
2998}
2999
3000
3001/**
3002 * Checks that the specified extension pack contains a VRDE module and that it
3003 * is shipshape.
3004 *
3005 * @returns S_OK if ok, appropriate failure status code with details.
3006 * @param a_pstrExtPack The name of the extension pack.
3007 */
3008HRESULT ExtPackManager::i_checkVrdeExtPack(Utf8Str const *a_pstrExtPack)
3009{
3010 AutoCaller autoCaller(this);
3011 HRESULT hrc = autoCaller.rc();
3012 if (SUCCEEDED(hrc))
3013 {
3014 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3015
3016 ExtPack *pExtPack = i_findExtPack(a_pstrExtPack->c_str());
3017 if (pExtPack)
3018 hrc = pExtPack->i_checkVrde();
3019 else
3020 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
3021 }
3022
3023 return hrc;
3024}
3025
3026/**
3027 * Gets the full path to the VRDE library of the specified extension pack.
3028 *
3029 * This will do extacly the same as checkVrdeExtPack and then resolve the
3030 * library path.
3031 *
3032 * @returns S_OK if a path is returned, COM error status and message return if
3033 * not.
3034 * @param a_pstrExtPack The extension pack.
3035 * @param a_pstrVrdeLibrary Where to return the path.
3036 */
3037int ExtPackManager::i_getVrdeLibraryPathForExtPack(Utf8Str const *a_pstrExtPack, Utf8Str *a_pstrVrdeLibrary)
3038{
3039 AutoCaller autoCaller(this);
3040 HRESULT hrc = autoCaller.rc();
3041 if (SUCCEEDED(hrc))
3042 {
3043 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3044
3045 ExtPack *pExtPack = i_findExtPack(a_pstrExtPack->c_str());
3046 if (pExtPack)
3047 hrc = pExtPack->i_getVrdpLibraryName(a_pstrVrdeLibrary);
3048 else
3049 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"),
3050 a_pstrExtPack->c_str());
3051 }
3052
3053 return hrc;
3054}
3055
3056/**
3057 * Gets the full path to the specified library of the specified extension pack.
3058 *
3059 * @returns S_OK if a path is returned, COM error status and message return if
3060 * not.
3061 * @param a_pszModuleName The library.
3062 * @param a_pstrExtPack The extension pack.
3063 * @param a_pstrVrdeLibrary Where to return the path.
3064 */
3065HRESULT ExtPackManager::i_getLibraryPathForExtPack(const char *a_pszModuleName, Utf8Str const *a_pstrExtPack,
3066 Utf8Str *a_pstrLibrary)
3067{
3068 AutoCaller autoCaller(this);
3069 HRESULT hrc = autoCaller.rc();
3070 if (SUCCEEDED(hrc))
3071 {
3072 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3073
3074 ExtPack *pExtPack = i_findExtPack(a_pstrExtPack->c_str());
3075 if (pExtPack)
3076 hrc = pExtPack->i_getLibraryName(a_pszModuleName, a_pstrLibrary);
3077 else
3078 hrc = setError(VBOX_E_OBJECT_NOT_FOUND, tr("No extension pack by the name '%s' was found"), a_pstrExtPack->c_str());
3079 }
3080
3081 return hrc;
3082}
3083
3084/**
3085 * Gets the name of the default VRDE extension pack.
3086 *
3087 * @returns S_OK or some COM error status on red tape failure.
3088 * @param a_pstrExtPack Where to return the extension pack name. Returns
3089 * empty if no extension pack wishes to be the default
3090 * VRDP provider.
3091 */
3092HRESULT ExtPackManager::i_getDefaultVrdeExtPack(Utf8Str *a_pstrExtPack)
3093{
3094 a_pstrExtPack->setNull();
3095
3096 AutoCaller autoCaller(this);
3097 HRESULT hrc = autoCaller.rc();
3098 if (SUCCEEDED(hrc))
3099 {
3100 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3101
3102 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3103 it != m->llInstalledExtPacks.end();
3104 ++it)
3105 {
3106 if ((*it)->i_wantsToBeDefaultVrde())
3107 {
3108 *a_pstrExtPack = (*it)->m->Desc.strName;
3109 break;
3110 }
3111 }
3112 }
3113 return hrc;
3114}
3115
3116/**
3117 * Checks if an extension pack is (present and) usable.
3118 *
3119 * @returns @c true if it is, otherwise @c false.
3120 * @param a_pszExtPack The name of the extension pack.
3121 */
3122bool ExtPackManager::i_isExtPackUsable(const char *a_pszExtPack)
3123{
3124 AutoCaller autoCaller(this);
3125 HRESULT hrc = autoCaller.rc();
3126 if (FAILED(hrc))
3127 return false;
3128 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3129
3130 ExtPack *pExtPack = i_findExtPack(a_pszExtPack);
3131 return pExtPack != NULL
3132 && pExtPack->m->fUsable;
3133}
3134
3135/**
3136 * Dumps all extension packs to the release log.
3137 */
3138void ExtPackManager::i_dumpAllToReleaseLog(void)
3139{
3140 AutoCaller autoCaller(this);
3141 HRESULT hrc = autoCaller.rc();
3142 if (FAILED(hrc))
3143 return;
3144 AutoReadLock autoLock(this COMMA_LOCKVAL_SRC_POS);
3145
3146 LogRel(("Installed Extension Packs:\n"));
3147 for (ExtPackList::iterator it = m->llInstalledExtPacks.begin();
3148 it != m->llInstalledExtPacks.end();
3149 ++it)
3150 {
3151 ExtPack::Data *pExtPackData = (*it)->m;
3152 if (pExtPackData)
3153 {
3154 if (pExtPackData->fUsable)
3155 LogRel((" %s (Version: %s r%u%s%s; VRDE Module: %s)\n",
3156 pExtPackData->Desc.strName.c_str(),
3157 pExtPackData->Desc.strVersion.c_str(),
3158 pExtPackData->Desc.uRevision,
3159 pExtPackData->Desc.strEdition.isEmpty() ? "" : " ",
3160 pExtPackData->Desc.strEdition.c_str(),
3161 pExtPackData->Desc.strVrdeModule.c_str() ));
3162 else
3163 LogRel((" %s (Version: %s r%u%s%s; VRDE Module: %s unusable because of '%s')\n",
3164 pExtPackData->Desc.strName.c_str(),
3165 pExtPackData->Desc.strVersion.c_str(),
3166 pExtPackData->Desc.uRevision,
3167 pExtPackData->Desc.strEdition.isEmpty() ? "" : " ",
3168 pExtPackData->Desc.strEdition.c_str(),
3169 pExtPackData->Desc.strVrdeModule.c_str(),
3170 pExtPackData->strWhyUnusable.c_str() ));
3171 }
3172 else
3173 LogRel((" pExtPackData is NULL\n"));
3174 }
3175
3176 if (!m->llInstalledExtPacks.size())
3177 LogRel((" None installed!\n"));
3178}
3179
3180/* 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