VirtualBox

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

Last change on this file since 51978 was 51978, checked in by vboxsync, 10 years ago

Need to initialize the SUPR3HardenedVerify* bits in the extension pack manager too, so split it (the init code) out into a separate API.

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