VirtualBox

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

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

ExtPackManagerImpl.cpp: Removed extpack version check as it is made unnecessary by the VBOXEXTPACKREG change.

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