VirtualBox

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

Last change on this file since 94884 was 94720, checked in by vboxsync, 3 years ago

Main: Implement loading and unloading of the cryptographic support module from the extension pack, bugref:9955

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

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