VirtualBox

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

Last change on this file since 75323 was 74814, checked in by vboxsync, 6 years ago

typo

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